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 return to_integer_type_scalar(ctx
, t
);
226 ac_to_integer(struct ac_llvm_context
*ctx
, LLVMValueRef v
)
228 LLVMTypeRef type
= LLVMTypeOf(v
);
229 return LLVMBuildBitCast(ctx
->builder
, v
, ac_to_integer_type(ctx
, type
), "");
233 ac_to_integer_or_pointer(struct ac_llvm_context
*ctx
, LLVMValueRef v
)
235 LLVMTypeRef type
= LLVMTypeOf(v
);
236 if (LLVMGetTypeKind(type
) == LLVMPointerTypeKind
)
238 return ac_to_integer(ctx
, v
);
241 static LLVMTypeRef
to_float_type_scalar(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
243 if (t
== ctx
->i16
|| t
== ctx
->f16
)
245 else if (t
== ctx
->i32
|| t
== ctx
->f32
)
247 else if (t
== ctx
->i64
|| t
== ctx
->f64
)
250 unreachable("Unhandled float size");
254 ac_to_float_type(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
256 if (LLVMGetTypeKind(t
) == LLVMVectorTypeKind
) {
257 LLVMTypeRef elem_type
= LLVMGetElementType(t
);
258 return LLVMVectorType(to_float_type_scalar(ctx
, elem_type
),
259 LLVMGetVectorSize(t
));
261 return to_float_type_scalar(ctx
, t
);
265 ac_to_float(struct ac_llvm_context
*ctx
, LLVMValueRef v
)
267 LLVMTypeRef type
= LLVMTypeOf(v
);
268 return LLVMBuildBitCast(ctx
->builder
, v
, ac_to_float_type(ctx
, type
), "");
273 ac_build_intrinsic(struct ac_llvm_context
*ctx
, const char *name
,
274 LLVMTypeRef return_type
, LLVMValueRef
*params
,
275 unsigned param_count
, unsigned attrib_mask
)
277 LLVMValueRef function
, call
;
278 bool set_callsite_attrs
= !(attrib_mask
& AC_FUNC_ATTR_LEGACY
);
280 function
= LLVMGetNamedFunction(ctx
->module
, name
);
282 LLVMTypeRef param_types
[32], function_type
;
285 assert(param_count
<= 32);
287 for (i
= 0; i
< param_count
; ++i
) {
289 param_types
[i
] = LLVMTypeOf(params
[i
]);
292 LLVMFunctionType(return_type
, param_types
, param_count
, 0);
293 function
= LLVMAddFunction(ctx
->module
, name
, function_type
);
295 LLVMSetFunctionCallConv(function
, LLVMCCallConv
);
296 LLVMSetLinkage(function
, LLVMExternalLinkage
);
298 if (!set_callsite_attrs
)
299 ac_add_func_attributes(ctx
->context
, function
, attrib_mask
);
302 call
= LLVMBuildCall(ctx
->builder
, function
, params
, param_count
, "");
303 if (set_callsite_attrs
)
304 ac_add_func_attributes(ctx
->context
, call
, attrib_mask
);
309 * Given the i32 or vNi32 \p type, generate the textual name (e.g. for use with
312 void ac_build_type_name_for_intr(LLVMTypeRef type
, char *buf
, unsigned bufsize
)
314 LLVMTypeRef elem_type
= type
;
316 assert(bufsize
>= 8);
318 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
) {
319 int ret
= snprintf(buf
, bufsize
, "v%u",
320 LLVMGetVectorSize(type
));
322 char *type_name
= LLVMPrintTypeToString(type
);
323 fprintf(stderr
, "Error building type name for: %s\n",
327 elem_type
= LLVMGetElementType(type
);
331 switch (LLVMGetTypeKind(elem_type
)) {
333 case LLVMIntegerTypeKind
:
334 snprintf(buf
, bufsize
, "i%d", LLVMGetIntTypeWidth(elem_type
));
336 case LLVMHalfTypeKind
:
337 snprintf(buf
, bufsize
, "f16");
339 case LLVMFloatTypeKind
:
340 snprintf(buf
, bufsize
, "f32");
342 case LLVMDoubleTypeKind
:
343 snprintf(buf
, bufsize
, "f64");
349 * Helper function that builds an LLVM IR PHI node and immediately adds
353 ac_build_phi(struct ac_llvm_context
*ctx
, LLVMTypeRef type
,
354 unsigned count_incoming
, LLVMValueRef
*values
,
355 LLVMBasicBlockRef
*blocks
)
357 LLVMValueRef phi
= LLVMBuildPhi(ctx
->builder
, type
, "");
358 LLVMAddIncoming(phi
, values
, blocks
, count_incoming
);
362 void ac_build_s_barrier(struct ac_llvm_context
*ctx
)
364 ac_build_intrinsic(ctx
, "llvm.amdgcn.s.barrier", ctx
->voidt
, NULL
,
365 0, AC_FUNC_ATTR_CONVERGENT
);
368 /* Prevent optimizations (at least of memory accesses) across the current
369 * point in the program by emitting empty inline assembly that is marked as
370 * having side effects.
372 * Optionally, a value can be passed through the inline assembly to prevent
373 * LLVM from hoisting calls to ReadNone functions.
376 ac_build_optimization_barrier(struct ac_llvm_context
*ctx
,
379 static int counter
= 0;
381 LLVMBuilderRef builder
= ctx
->builder
;
384 snprintf(code
, sizeof(code
), "; %d", p_atomic_inc_return(&counter
));
387 LLVMTypeRef ftype
= LLVMFunctionType(ctx
->voidt
, NULL
, 0, false);
388 LLVMValueRef inlineasm
= LLVMConstInlineAsm(ftype
, code
, "", true, false);
389 LLVMBuildCall(builder
, inlineasm
, NULL
, 0, "");
391 LLVMTypeRef ftype
= LLVMFunctionType(ctx
->i32
, &ctx
->i32
, 1, false);
392 LLVMValueRef inlineasm
= LLVMConstInlineAsm(ftype
, code
, "=v,0", true, false);
393 LLVMValueRef vgpr
= *pvgpr
;
394 LLVMTypeRef vgpr_type
= LLVMTypeOf(vgpr
);
395 unsigned vgpr_size
= ac_get_type_size(vgpr_type
);
398 assert(vgpr_size
% 4 == 0);
400 vgpr
= LLVMBuildBitCast(builder
, vgpr
, LLVMVectorType(ctx
->i32
, vgpr_size
/ 4), "");
401 vgpr0
= LLVMBuildExtractElement(builder
, vgpr
, ctx
->i32_0
, "");
402 vgpr0
= LLVMBuildCall(builder
, inlineasm
, &vgpr0
, 1, "");
403 vgpr
= LLVMBuildInsertElement(builder
, vgpr
, vgpr0
, ctx
->i32_0
, "");
404 vgpr
= LLVMBuildBitCast(builder
, vgpr
, vgpr_type
, "");
411 ac_build_shader_clock(struct ac_llvm_context
*ctx
)
413 LLVMValueRef tmp
= ac_build_intrinsic(ctx
, "llvm.readcyclecounter",
414 ctx
->i64
, NULL
, 0, 0);
415 return LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->v2i32
, "");
419 ac_build_ballot(struct ac_llvm_context
*ctx
,
422 LLVMValueRef args
[3] = {
425 LLVMConstInt(ctx
->i32
, LLVMIntNE
, 0)
428 /* We currently have no other way to prevent LLVM from lifting the icmp
429 * calls to a dominating basic block.
431 ac_build_optimization_barrier(ctx
, &args
[0]);
433 args
[0] = ac_to_integer(ctx
, args
[0]);
435 return ac_build_intrinsic(ctx
,
436 "llvm.amdgcn.icmp.i32",
438 AC_FUNC_ATTR_NOUNWIND
|
439 AC_FUNC_ATTR_READNONE
|
440 AC_FUNC_ATTR_CONVERGENT
);
444 ac_build_vote_all(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
446 LLVMValueRef active_set
= ac_build_ballot(ctx
, ctx
->i32_1
);
447 LLVMValueRef vote_set
= ac_build_ballot(ctx
, value
);
448 return LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, vote_set
, active_set
, "");
452 ac_build_vote_any(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
454 LLVMValueRef vote_set
= ac_build_ballot(ctx
, value
);
455 return LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, vote_set
,
456 LLVMConstInt(ctx
->i64
, 0, 0), "");
460 ac_build_vote_eq(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
462 LLVMValueRef active_set
= ac_build_ballot(ctx
, ctx
->i32_1
);
463 LLVMValueRef vote_set
= ac_build_ballot(ctx
, value
);
465 LLVMValueRef all
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
466 vote_set
, active_set
, "");
467 LLVMValueRef none
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
469 LLVMConstInt(ctx
->i64
, 0, 0), "");
470 return LLVMBuildOr(ctx
->builder
, all
, none
, "");
474 ac_build_varying_gather_values(struct ac_llvm_context
*ctx
, LLVMValueRef
*values
,
475 unsigned value_count
, unsigned component
)
477 LLVMValueRef vec
= NULL
;
479 if (value_count
== 1) {
480 return values
[component
];
481 } else if (!value_count
)
482 unreachable("value_count is 0");
484 for (unsigned i
= component
; i
< value_count
+ component
; i
++) {
485 LLVMValueRef value
= values
[i
];
488 vec
= LLVMGetUndef( LLVMVectorType(LLVMTypeOf(value
), value_count
));
489 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, i
- component
, false);
490 vec
= LLVMBuildInsertElement(ctx
->builder
, vec
, value
, index
, "");
496 ac_build_gather_values_extended(struct ac_llvm_context
*ctx
,
497 LLVMValueRef
*values
,
498 unsigned value_count
,
499 unsigned value_stride
,
503 LLVMBuilderRef builder
= ctx
->builder
;
504 LLVMValueRef vec
= NULL
;
507 if (value_count
== 1 && !always_vector
) {
509 return LLVMBuildLoad(builder
, values
[0], "");
511 } else if (!value_count
)
512 unreachable("value_count is 0");
514 for (i
= 0; i
< value_count
; i
++) {
515 LLVMValueRef value
= values
[i
* value_stride
];
517 value
= LLVMBuildLoad(builder
, value
, "");
520 vec
= LLVMGetUndef( LLVMVectorType(LLVMTypeOf(value
), value_count
));
521 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, i
, false);
522 vec
= LLVMBuildInsertElement(builder
, vec
, value
, index
, "");
528 ac_build_gather_values(struct ac_llvm_context
*ctx
,
529 LLVMValueRef
*values
,
530 unsigned value_count
)
532 return ac_build_gather_values_extended(ctx
, values
, value_count
, 1, false, false);
535 /* Expand a scalar or vector to <dst_channels x type> by filling the remaining
536 * channels with undef. Extract at most src_channels components from the input.
538 LLVMValueRef
ac_build_expand(struct ac_llvm_context
*ctx
,
540 unsigned src_channels
,
541 unsigned dst_channels
)
543 LLVMTypeRef elemtype
;
544 LLVMValueRef chan
[dst_channels
];
546 if (LLVMGetTypeKind(LLVMTypeOf(value
)) == LLVMVectorTypeKind
) {
547 unsigned vec_size
= LLVMGetVectorSize(LLVMTypeOf(value
));
549 if (src_channels
== dst_channels
&& vec_size
== dst_channels
)
552 src_channels
= MIN2(src_channels
, vec_size
);
554 for (unsigned i
= 0; i
< src_channels
; i
++)
555 chan
[i
] = ac_llvm_extract_elem(ctx
, value
, i
);
557 elemtype
= LLVMGetElementType(LLVMTypeOf(value
));
560 assert(src_channels
== 1);
563 elemtype
= LLVMTypeOf(value
);
566 for (unsigned i
= src_channels
; i
< dst_channels
; i
++)
567 chan
[i
] = LLVMGetUndef(elemtype
);
569 return ac_build_gather_values(ctx
, chan
, dst_channels
);
572 /* Expand a scalar or vector to <4 x type> by filling the remaining channels
573 * with undef. Extract at most num_channels components from the input.
575 LLVMValueRef
ac_build_expand_to_vec4(struct ac_llvm_context
*ctx
,
577 unsigned num_channels
)
579 return ac_build_expand(ctx
, value
, num_channels
, 4);
582 LLVMValueRef
ac_build_round(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
584 unsigned type_size
= ac_get_type_size(LLVMTypeOf(value
));
588 name
= "llvm.rint.f16";
589 else if (type_size
== 4)
590 name
= "llvm.rint.f32";
592 name
= "llvm.rint.f64";
594 return ac_build_intrinsic(ctx
, name
, LLVMTypeOf(value
), &value
, 1,
595 AC_FUNC_ATTR_READNONE
);
599 ac_build_fdiv(struct ac_llvm_context
*ctx
,
603 /* If we do (num / den), LLVM >= 7.0 does:
604 * return num * v_rcp_f32(den * (fabs(den) > 0x1.0p+96f ? 0x1.0p-32f : 1.0f));
606 * If we do (num * (1 / den)), LLVM does:
607 * return num * v_rcp_f32(den);
609 LLVMValueRef one
= LLVMTypeOf(num
) == ctx
->f64
? ctx
->f64_1
: ctx
->f32_1
;
610 LLVMValueRef rcp
= LLVMBuildFDiv(ctx
->builder
, one
, den
, "");
611 LLVMValueRef ret
= LLVMBuildFMul(ctx
->builder
, num
, rcp
, "");
613 /* Use v_rcp_f32 instead of precise division. */
614 if (!LLVMIsConstant(ret
))
615 LLVMSetMetadata(ret
, ctx
->fpmath_md_kind
, ctx
->fpmath_md_2p5_ulp
);
619 /* See fast_idiv_by_const.h. */
620 /* Set: increment = util_fast_udiv_info::increment ? multiplier : 0; */
621 LLVMValueRef
ac_build_fast_udiv(struct ac_llvm_context
*ctx
,
623 LLVMValueRef multiplier
,
624 LLVMValueRef pre_shift
,
625 LLVMValueRef post_shift
,
626 LLVMValueRef increment
)
628 LLVMBuilderRef builder
= ctx
->builder
;
630 num
= LLVMBuildLShr(builder
, num
, pre_shift
, "");
631 num
= LLVMBuildMul(builder
,
632 LLVMBuildZExt(builder
, num
, ctx
->i64
, ""),
633 LLVMBuildZExt(builder
, multiplier
, ctx
->i64
, ""), "");
634 num
= LLVMBuildAdd(builder
, num
,
635 LLVMBuildZExt(builder
, increment
, ctx
->i64
, ""), "");
636 num
= LLVMBuildLShr(builder
, num
, LLVMConstInt(ctx
->i64
, 32, 0), "");
637 num
= LLVMBuildTrunc(builder
, num
, ctx
->i32
, "");
638 return LLVMBuildLShr(builder
, num
, post_shift
, "");
641 /* See fast_idiv_by_const.h. */
642 /* If num != UINT_MAX, this more efficient version can be used. */
643 /* Set: increment = util_fast_udiv_info::increment; */
644 LLVMValueRef
ac_build_fast_udiv_nuw(struct ac_llvm_context
*ctx
,
646 LLVMValueRef multiplier
,
647 LLVMValueRef pre_shift
,
648 LLVMValueRef post_shift
,
649 LLVMValueRef increment
)
651 LLVMBuilderRef builder
= ctx
->builder
;
653 num
= LLVMBuildLShr(builder
, num
, pre_shift
, "");
654 num
= LLVMBuildNUWAdd(builder
, num
, increment
, "");
655 num
= LLVMBuildMul(builder
,
656 LLVMBuildZExt(builder
, num
, ctx
->i64
, ""),
657 LLVMBuildZExt(builder
, multiplier
, ctx
->i64
, ""), "");
658 num
= LLVMBuildLShr(builder
, num
, LLVMConstInt(ctx
->i64
, 32, 0), "");
659 num
= LLVMBuildTrunc(builder
, num
, ctx
->i32
, "");
660 return LLVMBuildLShr(builder
, num
, post_shift
, "");
663 /* See fast_idiv_by_const.h. */
664 /* Both operands must fit in 31 bits and the divisor must not be 1. */
665 LLVMValueRef
ac_build_fast_udiv_u31_d_not_one(struct ac_llvm_context
*ctx
,
667 LLVMValueRef multiplier
,
668 LLVMValueRef post_shift
)
670 LLVMBuilderRef builder
= ctx
->builder
;
672 num
= LLVMBuildMul(builder
,
673 LLVMBuildZExt(builder
, num
, ctx
->i64
, ""),
674 LLVMBuildZExt(builder
, multiplier
, ctx
->i64
, ""), "");
675 num
= LLVMBuildLShr(builder
, num
, LLVMConstInt(ctx
->i64
, 32, 0), "");
676 num
= LLVMBuildTrunc(builder
, num
, ctx
->i32
, "");
677 return LLVMBuildLShr(builder
, num
, post_shift
, "");
680 /* Coordinates for cube map selection. sc, tc, and ma are as in Table 8.27
681 * of the OpenGL 4.5 (Compatibility Profile) specification, except ma is
682 * already multiplied by two. id is the cube face number.
684 struct cube_selection_coords
{
691 build_cube_intrinsic(struct ac_llvm_context
*ctx
,
693 struct cube_selection_coords
*out
)
695 LLVMTypeRef f32
= ctx
->f32
;
697 out
->stc
[1] = ac_build_intrinsic(ctx
, "llvm.amdgcn.cubetc",
698 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
699 out
->stc
[0] = ac_build_intrinsic(ctx
, "llvm.amdgcn.cubesc",
700 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
701 out
->ma
= ac_build_intrinsic(ctx
, "llvm.amdgcn.cubema",
702 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
703 out
->id
= ac_build_intrinsic(ctx
, "llvm.amdgcn.cubeid",
704 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
708 * Build a manual selection sequence for cube face sc/tc coordinates and
709 * major axis vector (multiplied by 2 for consistency) for the given
710 * vec3 \p coords, for the face implied by \p selcoords.
712 * For the major axis, we always adjust the sign to be in the direction of
713 * selcoords.ma; i.e., a positive out_ma means that coords is pointed towards
714 * the selcoords major axis.
716 static void build_cube_select(struct ac_llvm_context
*ctx
,
717 const struct cube_selection_coords
*selcoords
,
718 const LLVMValueRef
*coords
,
719 LLVMValueRef
*out_st
,
720 LLVMValueRef
*out_ma
)
722 LLVMBuilderRef builder
= ctx
->builder
;
723 LLVMTypeRef f32
= LLVMTypeOf(coords
[0]);
724 LLVMValueRef is_ma_positive
;
726 LLVMValueRef is_ma_z
, is_not_ma_z
;
727 LLVMValueRef is_ma_y
;
728 LLVMValueRef is_ma_x
;
732 is_ma_positive
= LLVMBuildFCmp(builder
, LLVMRealUGE
,
733 selcoords
->ma
, LLVMConstReal(f32
, 0.0), "");
734 sgn_ma
= LLVMBuildSelect(builder
, is_ma_positive
,
735 LLVMConstReal(f32
, 1.0), LLVMConstReal(f32
, -1.0), "");
737 is_ma_z
= LLVMBuildFCmp(builder
, LLVMRealUGE
, selcoords
->id
, LLVMConstReal(f32
, 4.0), "");
738 is_not_ma_z
= LLVMBuildNot(builder
, is_ma_z
, "");
739 is_ma_y
= LLVMBuildAnd(builder
, is_not_ma_z
,
740 LLVMBuildFCmp(builder
, LLVMRealUGE
, selcoords
->id
, LLVMConstReal(f32
, 2.0), ""), "");
741 is_ma_x
= LLVMBuildAnd(builder
, is_not_ma_z
, LLVMBuildNot(builder
, is_ma_y
, ""), "");
744 tmp
= LLVMBuildSelect(builder
, is_ma_x
, coords
[2], coords
[0], "");
745 sgn
= LLVMBuildSelect(builder
, is_ma_y
, LLVMConstReal(f32
, 1.0),
746 LLVMBuildSelect(builder
, is_ma_z
, sgn_ma
,
747 LLVMBuildFNeg(builder
, sgn_ma
, ""), ""), "");
748 out_st
[0] = LLVMBuildFMul(builder
, tmp
, sgn
, "");
751 tmp
= LLVMBuildSelect(builder
, is_ma_y
, coords
[2], coords
[1], "");
752 sgn
= LLVMBuildSelect(builder
, is_ma_y
, sgn_ma
,
753 LLVMConstReal(f32
, -1.0), "");
754 out_st
[1] = LLVMBuildFMul(builder
, tmp
, sgn
, "");
757 tmp
= LLVMBuildSelect(builder
, is_ma_z
, coords
[2],
758 LLVMBuildSelect(builder
, is_ma_y
, coords
[1], coords
[0], ""), "");
759 tmp
= ac_build_intrinsic(ctx
, "llvm.fabs.f32",
760 ctx
->f32
, &tmp
, 1, AC_FUNC_ATTR_READNONE
);
761 *out_ma
= LLVMBuildFMul(builder
, tmp
, LLVMConstReal(f32
, 2.0), "");
765 ac_prepare_cube_coords(struct ac_llvm_context
*ctx
,
766 bool is_deriv
, bool is_array
, bool is_lod
,
767 LLVMValueRef
*coords_arg
,
768 LLVMValueRef
*derivs_arg
)
771 LLVMBuilderRef builder
= ctx
->builder
;
772 struct cube_selection_coords selcoords
;
773 LLVMValueRef coords
[3];
776 if (is_array
&& !is_lod
) {
777 LLVMValueRef tmp
= ac_build_round(ctx
, coords_arg
[3]);
779 /* Section 8.9 (Texture Functions) of the GLSL 4.50 spec says:
781 * "For Array forms, the array layer used will be
783 * max(0, min(d−1, floor(layer+0.5)))
785 * where d is the depth of the texture array and layer
786 * comes from the component indicated in the tables below.
787 * Workaroudn for an issue where the layer is taken from a
788 * helper invocation which happens to fall on a different
789 * layer due to extrapolation."
791 * VI and earlier attempt to implement this in hardware by
792 * clamping the value of coords[2] = (8 * layer) + face.
793 * Unfortunately, this means that the we end up with the wrong
794 * face when clamping occurs.
796 * Clamp the layer earlier to work around the issue.
798 if (ctx
->chip_class
<= VI
) {
800 ge0
= LLVMBuildFCmp(builder
, LLVMRealOGE
, tmp
, ctx
->f32_0
, "");
801 tmp
= LLVMBuildSelect(builder
, ge0
, tmp
, ctx
->f32_0
, "");
807 build_cube_intrinsic(ctx
, coords_arg
, &selcoords
);
809 invma
= ac_build_intrinsic(ctx
, "llvm.fabs.f32",
810 ctx
->f32
, &selcoords
.ma
, 1, AC_FUNC_ATTR_READNONE
);
811 invma
= ac_build_fdiv(ctx
, LLVMConstReal(ctx
->f32
, 1.0), invma
);
813 for (int i
= 0; i
< 2; ++i
)
814 coords
[i
] = LLVMBuildFMul(builder
, selcoords
.stc
[i
], invma
, "");
816 coords
[2] = selcoords
.id
;
818 if (is_deriv
&& derivs_arg
) {
819 LLVMValueRef derivs
[4];
822 /* Convert cube derivatives to 2D derivatives. */
823 for (axis
= 0; axis
< 2; axis
++) {
824 LLVMValueRef deriv_st
[2];
825 LLVMValueRef deriv_ma
;
827 /* Transform the derivative alongside the texture
828 * coordinate. Mathematically, the correct formula is
829 * as follows. Assume we're projecting onto the +Z face
830 * and denote by dx/dh the derivative of the (original)
831 * X texture coordinate with respect to horizontal
832 * window coordinates. The projection onto the +Z face
837 * Then df/dh = df/dx * dx/dh + df/dz * dz/dh
838 * = 1/z * dx/dh - x/z * 1/z * dz/dh.
840 * This motivatives the implementation below.
842 * Whether this actually gives the expected results for
843 * apps that might feed in derivatives obtained via
844 * finite differences is anyone's guess. The OpenGL spec
845 * seems awfully quiet about how textureGrad for cube
846 * maps should be handled.
848 build_cube_select(ctx
, &selcoords
, &derivs_arg
[axis
* 3],
849 deriv_st
, &deriv_ma
);
851 deriv_ma
= LLVMBuildFMul(builder
, deriv_ma
, invma
, "");
853 for (int i
= 0; i
< 2; ++i
)
854 derivs
[axis
* 2 + i
] =
855 LLVMBuildFSub(builder
,
856 LLVMBuildFMul(builder
, deriv_st
[i
], invma
, ""),
857 LLVMBuildFMul(builder
, deriv_ma
, coords
[i
], ""), "");
860 memcpy(derivs_arg
, derivs
, sizeof(derivs
));
863 /* Shift the texture coordinate. This must be applied after the
864 * derivative calculation.
866 for (int i
= 0; i
< 2; ++i
)
867 coords
[i
] = LLVMBuildFAdd(builder
, coords
[i
], LLVMConstReal(ctx
->f32
, 1.5), "");
870 /* for cube arrays coord.z = coord.w(array_index) * 8 + face */
871 /* coords_arg.w component - array_index for cube arrays */
872 coords
[2] = ac_build_fmad(ctx
, coords_arg
[3], LLVMConstReal(ctx
->f32
, 8.0), coords
[2]);
875 memcpy(coords_arg
, coords
, sizeof(coords
));
880 ac_build_fs_interp(struct ac_llvm_context
*ctx
,
881 LLVMValueRef llvm_chan
,
882 LLVMValueRef attr_number
,
887 LLVMValueRef args
[5];
892 args
[2] = attr_number
;
895 p1
= ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p1",
896 ctx
->f32
, args
, 4, AC_FUNC_ATTR_READNONE
);
901 args
[3] = attr_number
;
904 return ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p2",
905 ctx
->f32
, args
, 5, AC_FUNC_ATTR_READNONE
);
909 ac_build_fs_interp_mov(struct ac_llvm_context
*ctx
,
910 LLVMValueRef parameter
,
911 LLVMValueRef llvm_chan
,
912 LLVMValueRef attr_number
,
915 LLVMValueRef args
[4];
919 args
[2] = attr_number
;
922 return ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.mov",
923 ctx
->f32
, args
, 4, AC_FUNC_ATTR_READNONE
);
927 ac_build_gep0(struct ac_llvm_context
*ctx
,
928 LLVMValueRef base_ptr
,
931 LLVMValueRef indices
[2] = {
935 return LLVMBuildGEP(ctx
->builder
, base_ptr
, indices
, 2, "");
938 LLVMValueRef
ac_build_pointer_add(struct ac_llvm_context
*ctx
, LLVMValueRef ptr
,
941 return LLVMBuildPointerCast(ctx
->builder
,
942 ac_build_gep0(ctx
, ptr
, index
),
943 LLVMTypeOf(ptr
), "");
947 ac_build_indexed_store(struct ac_llvm_context
*ctx
,
948 LLVMValueRef base_ptr
, LLVMValueRef index
,
951 LLVMBuildStore(ctx
->builder
, value
,
952 ac_build_gep0(ctx
, base_ptr
, index
));
956 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad.
957 * It's equivalent to doing a load from &base_ptr[index].
959 * \param base_ptr Where the array starts.
960 * \param index The element index into the array.
961 * \param uniform Whether the base_ptr and index can be assumed to be
962 * dynamically uniform (i.e. load to an SGPR)
963 * \param invariant Whether the load is invariant (no other opcodes affect it)
964 * \param no_unsigned_wraparound
965 * For all possible re-associations and re-distributions of an expression
966 * "base_ptr + index * elemsize" into "addr + offset" (excluding GEPs
967 * without inbounds in base_ptr), this parameter is true if "addr + offset"
968 * does not result in an unsigned integer wraparound. This is used for
969 * optimal code generation of 32-bit pointer arithmetic.
971 * For example, a 32-bit immediate offset that causes a 32-bit unsigned
972 * integer wraparound can't be an imm offset in s_load_dword, because
973 * the instruction performs "addr + offset" in 64 bits.
975 * Expected usage for bindless textures by chaining GEPs:
976 * // possible unsigned wraparound, don't use InBounds:
977 * ptr1 = LLVMBuildGEP(base_ptr, index);
978 * image = load(ptr1); // becomes "s_load ptr1, 0"
980 * ptr2 = LLVMBuildInBoundsGEP(ptr1, 32 / elemsize);
981 * sampler = load(ptr2); // becomes "s_load ptr1, 32" thanks to InBounds
984 ac_build_load_custom(struct ac_llvm_context
*ctx
, LLVMValueRef base_ptr
,
985 LLVMValueRef index
, bool uniform
, bool invariant
,
986 bool no_unsigned_wraparound
)
988 LLVMValueRef pointer
, result
;
989 LLVMValueRef indices
[2] = {ctx
->i32_0
, index
};
991 if (no_unsigned_wraparound
&&
992 LLVMGetPointerAddressSpace(LLVMTypeOf(base_ptr
)) == AC_ADDR_SPACE_CONST_32BIT
)
993 pointer
= LLVMBuildInBoundsGEP(ctx
->builder
, base_ptr
, indices
, 2, "");
995 pointer
= LLVMBuildGEP(ctx
->builder
, base_ptr
, indices
, 2, "");
998 LLVMSetMetadata(pointer
, ctx
->uniform_md_kind
, ctx
->empty_md
);
999 result
= LLVMBuildLoad(ctx
->builder
, pointer
, "");
1001 LLVMSetMetadata(result
, ctx
->invariant_load_md_kind
, ctx
->empty_md
);
1005 LLVMValueRef
ac_build_load(struct ac_llvm_context
*ctx
, LLVMValueRef base_ptr
,
1008 return ac_build_load_custom(ctx
, base_ptr
, index
, false, false, false);
1011 LLVMValueRef
ac_build_load_invariant(struct ac_llvm_context
*ctx
,
1012 LLVMValueRef base_ptr
, LLVMValueRef index
)
1014 return ac_build_load_custom(ctx
, base_ptr
, index
, false, true, false);
1017 /* This assumes that there is no unsigned integer wraparound during the address
1018 * computation, excluding all GEPs within base_ptr. */
1019 LLVMValueRef
ac_build_load_to_sgpr(struct ac_llvm_context
*ctx
,
1020 LLVMValueRef base_ptr
, LLVMValueRef index
)
1022 return ac_build_load_custom(ctx
, base_ptr
, index
, true, true, true);
1025 /* See ac_build_load_custom() documentation. */
1026 LLVMValueRef
ac_build_load_to_sgpr_uint_wraparound(struct ac_llvm_context
*ctx
,
1027 LLVMValueRef base_ptr
, LLVMValueRef index
)
1029 return ac_build_load_custom(ctx
, base_ptr
, index
, true, true, false);
1032 /* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
1033 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
1034 * or v4i32 (num_channels=3,4).
1037 ac_build_buffer_store_dword(struct ac_llvm_context
*ctx
,
1040 unsigned num_channels
,
1041 LLVMValueRef voffset
,
1042 LLVMValueRef soffset
,
1043 unsigned inst_offset
,
1046 bool writeonly_memory
,
1047 bool swizzle_enable_hint
)
1049 /* Split 3 channel stores, becase LLVM doesn't support 3-channel
1051 if (num_channels
== 3) {
1052 LLVMValueRef v
[3], v01
;
1054 for (int i
= 0; i
< 3; i
++) {
1055 v
[i
] = LLVMBuildExtractElement(ctx
->builder
, vdata
,
1056 LLVMConstInt(ctx
->i32
, i
, 0), "");
1058 v01
= ac_build_gather_values(ctx
, v
, 2);
1060 ac_build_buffer_store_dword(ctx
, rsrc
, v01
, 2, voffset
,
1061 soffset
, inst_offset
, glc
, slc
,
1062 writeonly_memory
, swizzle_enable_hint
);
1063 ac_build_buffer_store_dword(ctx
, rsrc
, v
[2], 1, voffset
,
1064 soffset
, inst_offset
+ 8,
1066 writeonly_memory
, swizzle_enable_hint
);
1070 /* SWIZZLE_ENABLE requires that soffset isn't folded into voffset
1071 * (voffset is swizzled, but soffset isn't swizzled).
1072 * llvm.amdgcn.buffer.store doesn't have a separate soffset parameter.
1074 if (!swizzle_enable_hint
) {
1075 LLVMValueRef offset
= soffset
;
1077 static const char *types
[] = {"f32", "v2f32", "v4f32"};
1080 offset
= LLVMBuildAdd(ctx
->builder
, offset
,
1081 LLVMConstInt(ctx
->i32
, inst_offset
, 0), "");
1083 offset
= LLVMBuildAdd(ctx
->builder
, offset
, voffset
, "");
1085 LLVMValueRef args
[] = {
1086 ac_to_float(ctx
, vdata
),
1087 LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, ""),
1090 LLVMConstInt(ctx
->i1
, glc
, 0),
1091 LLVMConstInt(ctx
->i1
, slc
, 0),
1095 snprintf(name
, sizeof(name
), "llvm.amdgcn.buffer.store.%s",
1096 types
[CLAMP(num_channels
, 1, 3) - 1]);
1098 ac_build_intrinsic(ctx
, name
, ctx
->voidt
,
1099 args
, ARRAY_SIZE(args
),
1101 AC_FUNC_ATTR_INACCESSIBLE_MEM_ONLY
:
1102 AC_FUNC_ATTR_WRITEONLY
);
1106 static const unsigned dfmt
[] = {
1107 V_008F0C_BUF_DATA_FORMAT_32
,
1108 V_008F0C_BUF_DATA_FORMAT_32_32
,
1109 V_008F0C_BUF_DATA_FORMAT_32_32_32
,
1110 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
1112 static const char *types
[] = {"i32", "v2i32", "v4i32"};
1113 LLVMValueRef args
[] = {
1115 LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, ""),
1117 voffset
? voffset
: ctx
->i32_0
,
1119 LLVMConstInt(ctx
->i32
, inst_offset
, 0),
1120 LLVMConstInt(ctx
->i32
, dfmt
[num_channels
- 1], 0),
1121 LLVMConstInt(ctx
->i32
, V_008F0C_BUF_NUM_FORMAT_UINT
, 0),
1122 LLVMConstInt(ctx
->i1
, glc
, 0),
1123 LLVMConstInt(ctx
->i1
, slc
, 0),
1126 snprintf(name
, sizeof(name
), "llvm.amdgcn.tbuffer.store.%s",
1127 types
[CLAMP(num_channels
, 1, 3) - 1]);
1129 ac_build_intrinsic(ctx
, name
, ctx
->voidt
,
1130 args
, ARRAY_SIZE(args
),
1132 AC_FUNC_ATTR_INACCESSIBLE_MEM_ONLY
:
1133 AC_FUNC_ATTR_WRITEONLY
);
1137 ac_build_buffer_load_common(struct ac_llvm_context
*ctx
,
1139 LLVMValueRef vindex
,
1140 LLVMValueRef voffset
,
1141 unsigned num_channels
,
1147 LLVMValueRef args
[] = {
1148 LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, ""),
1149 vindex
? vindex
: ctx
->i32_0
,
1151 LLVMConstInt(ctx
->i1
, glc
, 0),
1152 LLVMConstInt(ctx
->i1
, slc
, 0)
1154 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
1156 LLVMTypeRef types
[] = {ctx
->f32
, ctx
->v2f32
, ctx
->v4f32
};
1157 const char *type_names
[] = {"f32", "v2f32", "v4f32"};
1161 snprintf(name
, sizeof(name
), "llvm.amdgcn.buffer.load.format.%s",
1164 snprintf(name
, sizeof(name
), "llvm.amdgcn.buffer.load.%s",
1168 return ac_build_intrinsic(ctx
, name
, types
[func
], args
,
1170 ac_get_load_intr_attribs(can_speculate
));
1174 ac_build_llvm8_buffer_load_common(struct ac_llvm_context
*ctx
,
1176 LLVMValueRef vindex
,
1177 LLVMValueRef voffset
,
1178 LLVMValueRef soffset
,
1179 unsigned num_channels
,
1186 LLVMValueRef args
[5];
1188 args
[idx
++] = LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, "");
1190 args
[idx
++] = vindex
? vindex
: ctx
->i32_0
;
1191 args
[idx
++] = voffset
? voffset
: ctx
->i32_0
;
1192 args
[idx
++] = soffset
? soffset
: ctx
->i32_0
;
1193 args
[idx
++] = LLVMConstInt(ctx
->i32
, (glc
? 1 : 0) + (slc
? 2 : 0), 0);
1194 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
1196 LLVMTypeRef types
[] = {ctx
->f32
, ctx
->v2f32
, ctx
->v4f32
};
1197 const char *type_names
[] = {"f32", "v2f32", "v4f32"};
1198 const char *indexing_kind
= structurized
? "struct" : "raw";
1202 snprintf(name
, sizeof(name
), "llvm.amdgcn.%s.buffer.load.format.%s",
1203 indexing_kind
, type_names
[func
]);
1205 snprintf(name
, sizeof(name
), "llvm.amdgcn.%s.buffer.load.%s",
1206 indexing_kind
, type_names
[func
]);
1209 return ac_build_intrinsic(ctx
, name
, types
[func
], args
,
1211 ac_get_load_intr_attribs(can_speculate
));
1215 ac_build_buffer_load(struct ac_llvm_context
*ctx
,
1218 LLVMValueRef vindex
,
1219 LLVMValueRef voffset
,
1220 LLVMValueRef soffset
,
1221 unsigned inst_offset
,
1227 LLVMValueRef offset
= LLVMConstInt(ctx
->i32
, inst_offset
, 0);
1229 offset
= LLVMBuildAdd(ctx
->builder
, offset
, voffset
, "");
1231 offset
= LLVMBuildAdd(ctx
->builder
, offset
, soffset
, "");
1233 /* TODO: VI and later generations can use SMEM with GLC=1.*/
1234 if (allow_smem
&& !glc
&& !slc
) {
1235 assert(vindex
== NULL
);
1237 LLVMValueRef result
[8];
1239 for (int i
= 0; i
< num_channels
; i
++) {
1241 offset
= LLVMBuildAdd(ctx
->builder
, offset
,
1242 LLVMConstInt(ctx
->i32
, 4, 0), "");
1244 LLVMValueRef args
[2] = {rsrc
, offset
};
1245 result
[i
] = ac_build_intrinsic(ctx
, "llvm.SI.load.const.v4i32",
1247 AC_FUNC_ATTR_READNONE
|
1248 AC_FUNC_ATTR_LEGACY
);
1250 if (num_channels
== 1)
1253 if (num_channels
== 3)
1254 result
[num_channels
++] = LLVMGetUndef(ctx
->f32
);
1255 return ac_build_gather_values(ctx
, result
, num_channels
);
1258 return ac_build_buffer_load_common(ctx
, rsrc
, vindex
, offset
,
1259 num_channels
, glc
, slc
,
1260 can_speculate
, false);
1263 LLVMValueRef
ac_build_buffer_load_format(struct ac_llvm_context
*ctx
,
1265 LLVMValueRef vindex
,
1266 LLVMValueRef voffset
,
1267 unsigned num_channels
,
1271 if (HAVE_LLVM
>= 0x800) {
1272 return ac_build_llvm8_buffer_load_common(ctx
, rsrc
, vindex
, voffset
, ctx
->i32_0
,
1273 num_channels
, glc
, false,
1274 can_speculate
, true, true);
1276 return ac_build_buffer_load_common(ctx
, rsrc
, vindex
, voffset
,
1277 num_channels
, glc
, false,
1278 can_speculate
, true);
1281 LLVMValueRef
ac_build_buffer_load_format_gfx9_safe(struct ac_llvm_context
*ctx
,
1283 LLVMValueRef vindex
,
1284 LLVMValueRef voffset
,
1285 unsigned num_channels
,
1289 if (HAVE_LLVM
>= 0x800) {
1290 return ac_build_llvm8_buffer_load_common(ctx
, rsrc
, vindex
, voffset
, ctx
->i32_0
,
1291 num_channels
, glc
, false,
1292 can_speculate
, true, true);
1295 LLVMValueRef elem_count
= LLVMBuildExtractElement(ctx
->builder
, rsrc
, LLVMConstInt(ctx
->i32
, 2, 0), "");
1296 LLVMValueRef stride
= LLVMBuildExtractElement(ctx
->builder
, rsrc
, ctx
->i32_1
, "");
1297 stride
= LLVMBuildLShr(ctx
->builder
, stride
, LLVMConstInt(ctx
->i32
, 16, 0), "");
1299 LLVMValueRef new_elem_count
= LLVMBuildSelect(ctx
->builder
,
1300 LLVMBuildICmp(ctx
->builder
, LLVMIntUGT
, elem_count
, stride
, ""),
1301 elem_count
, stride
, "");
1303 LLVMValueRef new_rsrc
= LLVMBuildInsertElement(ctx
->builder
, rsrc
, new_elem_count
,
1304 LLVMConstInt(ctx
->i32
, 2, 0), "");
1306 return ac_build_buffer_load_common(ctx
, new_rsrc
, vindex
, voffset
,
1307 num_channels
, glc
, false,
1308 can_speculate
, true);
1312 ac_build_tbuffer_load_short(struct ac_llvm_context
*ctx
,
1314 LLVMValueRef vindex
,
1315 LLVMValueRef voffset
,
1316 LLVMValueRef soffset
,
1317 LLVMValueRef immoffset
,
1320 const char *name
= "llvm.amdgcn.tbuffer.load.i32";
1321 LLVMTypeRef type
= ctx
->i32
;
1322 LLVMValueRef params
[] = {
1328 LLVMConstInt(ctx
->i32
, V_008F0C_BUF_DATA_FORMAT_16
, false),
1329 LLVMConstInt(ctx
->i32
, V_008F0C_BUF_NUM_FORMAT_UINT
, false),
1333 LLVMValueRef res
= ac_build_intrinsic(ctx
, name
, type
, params
, 9, 0);
1334 return LLVMBuildTrunc(ctx
->builder
, res
, ctx
->i16
, "");
1338 * Set range metadata on an instruction. This can only be used on load and
1339 * call instructions. If you know an instruction can only produce the values
1340 * 0, 1, 2, you would do set_range_metadata(value, 0, 3);
1341 * \p lo is the minimum value inclusive.
1342 * \p hi is the maximum value exclusive.
1344 static void set_range_metadata(struct ac_llvm_context
*ctx
,
1345 LLVMValueRef value
, unsigned lo
, unsigned hi
)
1347 LLVMValueRef range_md
, md_args
[2];
1348 LLVMTypeRef type
= LLVMTypeOf(value
);
1349 LLVMContextRef context
= LLVMGetTypeContext(type
);
1351 md_args
[0] = LLVMConstInt(type
, lo
, false);
1352 md_args
[1] = LLVMConstInt(type
, hi
, false);
1353 range_md
= LLVMMDNodeInContext(context
, md_args
, 2);
1354 LLVMSetMetadata(value
, ctx
->range_md_kind
, range_md
);
1358 ac_get_thread_id(struct ac_llvm_context
*ctx
)
1362 LLVMValueRef tid_args
[2];
1363 tid_args
[0] = LLVMConstInt(ctx
->i32
, 0xffffffff, false);
1364 tid_args
[1] = ctx
->i32_0
;
1365 tid_args
[1] = ac_build_intrinsic(ctx
,
1366 "llvm.amdgcn.mbcnt.lo", ctx
->i32
,
1367 tid_args
, 2, AC_FUNC_ATTR_READNONE
);
1369 tid
= ac_build_intrinsic(ctx
, "llvm.amdgcn.mbcnt.hi",
1371 2, AC_FUNC_ATTR_READNONE
);
1372 set_range_metadata(ctx
, tid
, 0, 64);
1377 * SI implements derivatives using the local data store (LDS)
1378 * All writes to the LDS happen in all executing threads at
1379 * the same time. TID is the Thread ID for the current
1380 * thread and is a value between 0 and 63, representing
1381 * the thread's position in the wavefront.
1383 * For the pixel shader threads are grouped into quads of four pixels.
1384 * The TIDs of the pixels of a quad are:
1392 * So, masking the TID with 0xfffffffc yields the TID of the top left pixel
1393 * of the quad, masking with 0xfffffffd yields the TID of the top pixel of
1394 * the current pixel's column, and masking with 0xfffffffe yields the TID
1395 * of the left pixel of the current pixel's row.
1397 * Adding 1 yields the TID of the pixel to the right of the left pixel, and
1398 * adding 2 yields the TID of the pixel below the top pixel.
1401 ac_build_ddxy(struct ac_llvm_context
*ctx
,
1406 unsigned tl_lanes
[4], trbl_lanes
[4];
1407 LLVMValueRef tl
, trbl
, args
[2];
1408 LLVMValueRef result
;
1410 for (unsigned i
= 0; i
< 4; ++i
) {
1411 tl_lanes
[i
] = i
& mask
;
1412 trbl_lanes
[i
] = (i
& mask
) + idx
;
1415 tl
= ac_build_quad_swizzle(ctx
, val
,
1416 tl_lanes
[0], tl_lanes
[1],
1417 tl_lanes
[2], tl_lanes
[3]);
1418 trbl
= ac_build_quad_swizzle(ctx
, val
,
1419 trbl_lanes
[0], trbl_lanes
[1],
1420 trbl_lanes
[2], trbl_lanes
[3]);
1422 tl
= LLVMBuildBitCast(ctx
->builder
, tl
, ctx
->f32
, "");
1423 trbl
= LLVMBuildBitCast(ctx
->builder
, trbl
, ctx
->f32
, "");
1424 result
= LLVMBuildFSub(ctx
->builder
, trbl
, tl
, "");
1426 result
= ac_build_intrinsic(ctx
, "llvm.amdgcn.wqm.f32", ctx
->f32
,
1433 ac_build_sendmsg(struct ac_llvm_context
*ctx
,
1435 LLVMValueRef wave_id
)
1437 LLVMValueRef args
[2];
1438 args
[0] = LLVMConstInt(ctx
->i32
, msg
, false);
1440 ac_build_intrinsic(ctx
, "llvm.amdgcn.s.sendmsg", ctx
->voidt
, args
, 2, 0);
1444 ac_build_imsb(struct ac_llvm_context
*ctx
,
1446 LLVMTypeRef dst_type
)
1448 LLVMValueRef msb
= ac_build_intrinsic(ctx
, "llvm.amdgcn.sffbh.i32",
1450 AC_FUNC_ATTR_READNONE
);
1452 /* The HW returns the last bit index from MSB, but NIR/TGSI wants
1453 * the index from LSB. Invert it by doing "31 - msb". */
1454 msb
= LLVMBuildSub(ctx
->builder
, LLVMConstInt(ctx
->i32
, 31, false),
1457 LLVMValueRef all_ones
= LLVMConstInt(ctx
->i32
, -1, true);
1458 LLVMValueRef cond
= LLVMBuildOr(ctx
->builder
,
1459 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
1460 arg
, ctx
->i32_0
, ""),
1461 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
1462 arg
, all_ones
, ""), "");
1464 return LLVMBuildSelect(ctx
->builder
, cond
, all_ones
, msb
, "");
1468 ac_build_umsb(struct ac_llvm_context
*ctx
,
1470 LLVMTypeRef dst_type
)
1472 const char *intrin_name
;
1474 LLVMValueRef highest_bit
;
1478 bitsize
= ac_get_elem_bits(ctx
, LLVMTypeOf(arg
));
1481 intrin_name
= "llvm.ctlz.i64";
1483 highest_bit
= LLVMConstInt(ctx
->i64
, 63, false);
1487 intrin_name
= "llvm.ctlz.i32";
1489 highest_bit
= LLVMConstInt(ctx
->i32
, 31, false);
1493 intrin_name
= "llvm.ctlz.i16";
1495 highest_bit
= LLVMConstInt(ctx
->i16
, 15, false);
1499 unreachable(!"invalid bitsize");
1503 LLVMValueRef params
[2] = {
1508 LLVMValueRef msb
= ac_build_intrinsic(ctx
, intrin_name
, type
,
1510 AC_FUNC_ATTR_READNONE
);
1512 /* The HW returns the last bit index from MSB, but TGSI/NIR wants
1513 * the index from LSB. Invert it by doing "31 - msb". */
1514 msb
= LLVMBuildSub(ctx
->builder
, highest_bit
, msb
, "");
1515 msb
= LLVMBuildTruncOrBitCast(ctx
->builder
, msb
, ctx
->i32
, "");
1517 /* check for zero */
1518 return LLVMBuildSelect(ctx
->builder
,
1519 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, arg
, zero
, ""),
1520 LLVMConstInt(ctx
->i32
, -1, true), msb
, "");
1523 LLVMValueRef
ac_build_fmin(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
1526 LLVMValueRef args
[2] = {a
, b
};
1527 return ac_build_intrinsic(ctx
, "llvm.minnum.f32", ctx
->f32
, args
, 2,
1528 AC_FUNC_ATTR_READNONE
);
1531 LLVMValueRef
ac_build_fmax(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
1534 LLVMValueRef args
[2] = {a
, b
};
1535 return ac_build_intrinsic(ctx
, "llvm.maxnum.f32", ctx
->f32
, args
, 2,
1536 AC_FUNC_ATTR_READNONE
);
1539 LLVMValueRef
ac_build_imin(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
1542 LLVMValueRef cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSLE
, a
, b
, "");
1543 return LLVMBuildSelect(ctx
->builder
, cmp
, a
, b
, "");
1546 LLVMValueRef
ac_build_imax(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
1549 LLVMValueRef cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGT
, a
, b
, "");
1550 return LLVMBuildSelect(ctx
->builder
, cmp
, a
, b
, "");
1553 LLVMValueRef
ac_build_umin(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
1556 LLVMValueRef cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntULE
, a
, b
, "");
1557 return LLVMBuildSelect(ctx
->builder
, cmp
, a
, b
, "");
1560 LLVMValueRef
ac_build_clamp(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
1562 return ac_build_fmin(ctx
, ac_build_fmax(ctx
, value
, ctx
->f32_0
),
1566 void ac_build_export(struct ac_llvm_context
*ctx
, struct ac_export_args
*a
)
1568 LLVMValueRef args
[9];
1570 args
[0] = LLVMConstInt(ctx
->i32
, a
->target
, 0);
1571 args
[1] = LLVMConstInt(ctx
->i32
, a
->enabled_channels
, 0);
1574 LLVMTypeRef i16
= LLVMInt16TypeInContext(ctx
->context
);
1575 LLVMTypeRef v2i16
= LLVMVectorType(i16
, 2);
1577 args
[2] = LLVMBuildBitCast(ctx
->builder
, a
->out
[0],
1579 args
[3] = LLVMBuildBitCast(ctx
->builder
, a
->out
[1],
1581 args
[4] = LLVMConstInt(ctx
->i1
, a
->done
, 0);
1582 args
[5] = LLVMConstInt(ctx
->i1
, a
->valid_mask
, 0);
1584 ac_build_intrinsic(ctx
, "llvm.amdgcn.exp.compr.v2i16",
1585 ctx
->voidt
, args
, 6, 0);
1587 args
[2] = a
->out
[0];
1588 args
[3] = a
->out
[1];
1589 args
[4] = a
->out
[2];
1590 args
[5] = a
->out
[3];
1591 args
[6] = LLVMConstInt(ctx
->i1
, a
->done
, 0);
1592 args
[7] = LLVMConstInt(ctx
->i1
, a
->valid_mask
, 0);
1594 ac_build_intrinsic(ctx
, "llvm.amdgcn.exp.f32",
1595 ctx
->voidt
, args
, 8, 0);
1599 void ac_build_export_null(struct ac_llvm_context
*ctx
)
1601 struct ac_export_args args
;
1603 args
.enabled_channels
= 0x0; /* enabled channels */
1604 args
.valid_mask
= 1; /* whether the EXEC mask is valid */
1605 args
.done
= 1; /* DONE bit */
1606 args
.target
= V_008DFC_SQ_EXP_NULL
;
1607 args
.compr
= 0; /* COMPR flag (0 = 32-bit export) */
1608 args
.out
[0] = LLVMGetUndef(ctx
->f32
); /* R */
1609 args
.out
[1] = LLVMGetUndef(ctx
->f32
); /* G */
1610 args
.out
[2] = LLVMGetUndef(ctx
->f32
); /* B */
1611 args
.out
[3] = LLVMGetUndef(ctx
->f32
); /* A */
1613 ac_build_export(ctx
, &args
);
1616 static unsigned ac_num_coords(enum ac_image_dim dim
)
1622 case ac_image_1darray
:
1626 case ac_image_2darray
:
1627 case ac_image_2dmsaa
:
1629 case ac_image_2darraymsaa
:
1632 unreachable("ac_num_coords: bad dim");
1636 static unsigned ac_num_derivs(enum ac_image_dim dim
)
1640 case ac_image_1darray
:
1643 case ac_image_2darray
:
1648 case ac_image_2dmsaa
:
1649 case ac_image_2darraymsaa
:
1651 unreachable("derivatives not supported");
1655 static const char *get_atomic_name(enum ac_atomic_op op
)
1658 case ac_atomic_swap
: return "swap";
1659 case ac_atomic_add
: return "add";
1660 case ac_atomic_sub
: return "sub";
1661 case ac_atomic_smin
: return "smin";
1662 case ac_atomic_umin
: return "umin";
1663 case ac_atomic_smax
: return "smax";
1664 case ac_atomic_umax
: return "umax";
1665 case ac_atomic_and
: return "and";
1666 case ac_atomic_or
: return "or";
1667 case ac_atomic_xor
: return "xor";
1669 unreachable("bad atomic op");
1672 LLVMValueRef
ac_build_image_opcode(struct ac_llvm_context
*ctx
,
1673 struct ac_image_args
*a
)
1675 const char *overload
[3] = { "", "", "" };
1676 unsigned num_overloads
= 0;
1677 LLVMValueRef args
[18];
1678 unsigned num_args
= 0;
1679 enum ac_image_dim dim
= a
->dim
;
1681 assert(!a
->lod
|| a
->lod
== ctx
->i32_0
|| a
->lod
== ctx
->f32_0
||
1683 assert((a
->opcode
!= ac_image_get_resinfo
&& a
->opcode
!= ac_image_load_mip
&&
1684 a
->opcode
!= ac_image_store_mip
) ||
1686 assert(a
->opcode
== ac_image_sample
|| a
->opcode
== ac_image_gather4
||
1687 (!a
->compare
&& !a
->offset
));
1688 assert((a
->opcode
== ac_image_sample
|| a
->opcode
== ac_image_gather4
||
1689 a
->opcode
== ac_image_get_lod
) ||
1691 assert((a
->bias
? 1 : 0) +
1693 (a
->level_zero
? 1 : 0) +
1694 (a
->derivs
[0] ? 1 : 0) <= 1);
1696 if (a
->opcode
== ac_image_get_lod
) {
1698 case ac_image_1darray
:
1701 case ac_image_2darray
:
1710 bool sample
= a
->opcode
== ac_image_sample
||
1711 a
->opcode
== ac_image_gather4
||
1712 a
->opcode
== ac_image_get_lod
;
1713 bool atomic
= a
->opcode
== ac_image_atomic
||
1714 a
->opcode
== ac_image_atomic_cmpswap
;
1715 LLVMTypeRef coord_type
= sample
? ctx
->f32
: ctx
->i32
;
1717 if (atomic
|| a
->opcode
== ac_image_store
|| a
->opcode
== ac_image_store_mip
) {
1718 args
[num_args
++] = a
->data
[0];
1719 if (a
->opcode
== ac_image_atomic_cmpswap
)
1720 args
[num_args
++] = a
->data
[1];
1724 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->dmask
, false);
1727 args
[num_args
++] = ac_to_integer(ctx
, a
->offset
);
1729 args
[num_args
++] = ac_to_float(ctx
, a
->bias
);
1730 overload
[num_overloads
++] = ".f32";
1733 args
[num_args
++] = ac_to_float(ctx
, a
->compare
);
1735 unsigned count
= ac_num_derivs(dim
);
1736 for (unsigned i
= 0; i
< count
; ++i
)
1737 args
[num_args
++] = ac_to_float(ctx
, a
->derivs
[i
]);
1738 overload
[num_overloads
++] = ".f32";
1740 unsigned num_coords
=
1741 a
->opcode
!= ac_image_get_resinfo
? ac_num_coords(dim
) : 0;
1742 for (unsigned i
= 0; i
< num_coords
; ++i
)
1743 args
[num_args
++] = LLVMBuildBitCast(ctx
->builder
, a
->coords
[i
], coord_type
, "");
1745 args
[num_args
++] = LLVMBuildBitCast(ctx
->builder
, a
->lod
, coord_type
, "");
1746 overload
[num_overloads
++] = sample
? ".f32" : ".i32";
1748 args
[num_args
++] = a
->resource
;
1750 args
[num_args
++] = a
->sampler
;
1751 args
[num_args
++] = LLVMConstInt(ctx
->i1
, a
->unorm
, false);
1754 args
[num_args
++] = ctx
->i32_0
; /* texfailctrl */
1755 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->cache_policy
, false);
1758 const char *atomic_subop
= "";
1759 switch (a
->opcode
) {
1760 case ac_image_sample
: name
= "sample"; break;
1761 case ac_image_gather4
: name
= "gather4"; break;
1762 case ac_image_load
: name
= "load"; break;
1763 case ac_image_load_mip
: name
= "load.mip"; break;
1764 case ac_image_store
: name
= "store"; break;
1765 case ac_image_store_mip
: name
= "store.mip"; break;
1766 case ac_image_atomic
:
1768 atomic_subop
= get_atomic_name(a
->atomic
);
1770 case ac_image_atomic_cmpswap
:
1772 atomic_subop
= "cmpswap";
1774 case ac_image_get_lod
: name
= "getlod"; break;
1775 case ac_image_get_resinfo
: name
= "getresinfo"; break;
1776 default: unreachable("invalid image opcode");
1779 const char *dimname
;
1781 case ac_image_1d
: dimname
= "1d"; break;
1782 case ac_image_2d
: dimname
= "2d"; break;
1783 case ac_image_3d
: dimname
= "3d"; break;
1784 case ac_image_cube
: dimname
= "cube"; break;
1785 case ac_image_1darray
: dimname
= "1darray"; break;
1786 case ac_image_2darray
: dimname
= "2darray"; break;
1787 case ac_image_2dmsaa
: dimname
= "2dmsaa"; break;
1788 case ac_image_2darraymsaa
: dimname
= "2darraymsaa"; break;
1789 default: unreachable("invalid dim");
1793 a
->lod
&& (a
->opcode
== ac_image_sample
|| a
->opcode
== ac_image_gather4
);
1795 snprintf(intr_name
, sizeof(intr_name
),
1796 "llvm.amdgcn.image.%s%s" /* base name */
1797 "%s%s%s" /* sample/gather modifiers */
1798 ".%s.%s%s%s%s", /* dimension and type overloads */
1800 a
->compare
? ".c" : "",
1803 a
->derivs
[0] ? ".d" :
1804 a
->level_zero
? ".lz" : "",
1805 a
->offset
? ".o" : "",
1807 atomic
? "i32" : "v4f32",
1808 overload
[0], overload
[1], overload
[2]);
1813 else if (a
->opcode
== ac_image_store
|| a
->opcode
== ac_image_store_mip
)
1818 LLVMValueRef result
=
1819 ac_build_intrinsic(ctx
, intr_name
, retty
, args
, num_args
,
1821 if (!sample
&& retty
== ctx
->v4f32
) {
1822 result
= LLVMBuildBitCast(ctx
->builder
, result
,
1828 LLVMValueRef
ac_build_cvt_pkrtz_f16(struct ac_llvm_context
*ctx
,
1829 LLVMValueRef args
[2])
1832 LLVMVectorType(LLVMHalfTypeInContext(ctx
->context
), 2);
1834 return ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pkrtz", v2f16
,
1835 args
, 2, AC_FUNC_ATTR_READNONE
);
1838 LLVMValueRef
ac_build_cvt_pknorm_i16(struct ac_llvm_context
*ctx
,
1839 LLVMValueRef args
[2])
1842 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pknorm.i16",
1843 ctx
->v2i16
, args
, 2,
1844 AC_FUNC_ATTR_READNONE
);
1845 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
1848 LLVMValueRef
ac_build_cvt_pknorm_u16(struct ac_llvm_context
*ctx
,
1849 LLVMValueRef args
[2])
1852 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pknorm.u16",
1853 ctx
->v2i16
, args
, 2,
1854 AC_FUNC_ATTR_READNONE
);
1855 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
1858 /* The 8-bit and 10-bit clamping is for HW workarounds. */
1859 LLVMValueRef
ac_build_cvt_pk_i16(struct ac_llvm_context
*ctx
,
1860 LLVMValueRef args
[2], unsigned bits
, bool hi
)
1862 assert(bits
== 8 || bits
== 10 || bits
== 16);
1864 LLVMValueRef max_rgb
= LLVMConstInt(ctx
->i32
,
1865 bits
== 8 ? 127 : bits
== 10 ? 511 : 32767, 0);
1866 LLVMValueRef min_rgb
= LLVMConstInt(ctx
->i32
,
1867 bits
== 8 ? -128 : bits
== 10 ? -512 : -32768, 0);
1868 LLVMValueRef max_alpha
=
1869 bits
!= 10 ? max_rgb
: ctx
->i32_1
;
1870 LLVMValueRef min_alpha
=
1871 bits
!= 10 ? min_rgb
: LLVMConstInt(ctx
->i32
, -2, 0);
1875 for (int i
= 0; i
< 2; i
++) {
1876 bool alpha
= hi
&& i
== 1;
1877 args
[i
] = ac_build_imin(ctx
, args
[i
],
1878 alpha
? max_alpha
: max_rgb
);
1879 args
[i
] = ac_build_imax(ctx
, args
[i
],
1880 alpha
? min_alpha
: min_rgb
);
1885 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pk.i16",
1886 ctx
->v2i16
, args
, 2,
1887 AC_FUNC_ATTR_READNONE
);
1888 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
1891 /* The 8-bit and 10-bit clamping is for HW workarounds. */
1892 LLVMValueRef
ac_build_cvt_pk_u16(struct ac_llvm_context
*ctx
,
1893 LLVMValueRef args
[2], unsigned bits
, bool hi
)
1895 assert(bits
== 8 || bits
== 10 || bits
== 16);
1897 LLVMValueRef max_rgb
= LLVMConstInt(ctx
->i32
,
1898 bits
== 8 ? 255 : bits
== 10 ? 1023 : 65535, 0);
1899 LLVMValueRef max_alpha
=
1900 bits
!= 10 ? max_rgb
: LLVMConstInt(ctx
->i32
, 3, 0);
1904 for (int i
= 0; i
< 2; i
++) {
1905 bool alpha
= hi
&& i
== 1;
1906 args
[i
] = ac_build_umin(ctx
, args
[i
],
1907 alpha
? max_alpha
: max_rgb
);
1912 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pk.u16",
1913 ctx
->v2i16
, args
, 2,
1914 AC_FUNC_ATTR_READNONE
);
1915 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
1918 LLVMValueRef
ac_build_wqm_vote(struct ac_llvm_context
*ctx
, LLVMValueRef i1
)
1920 return ac_build_intrinsic(ctx
, "llvm.amdgcn.wqm.vote", ctx
->i1
,
1921 &i1
, 1, AC_FUNC_ATTR_READNONE
);
1924 void ac_build_kill_if_false(struct ac_llvm_context
*ctx
, LLVMValueRef i1
)
1926 ac_build_intrinsic(ctx
, "llvm.amdgcn.kill", ctx
->voidt
,
1930 LLVMValueRef
ac_build_bfe(struct ac_llvm_context
*ctx
, LLVMValueRef input
,
1931 LLVMValueRef offset
, LLVMValueRef width
,
1934 LLVMValueRef args
[] = {
1940 return ac_build_intrinsic(ctx
,
1941 is_signed
? "llvm.amdgcn.sbfe.i32" :
1942 "llvm.amdgcn.ubfe.i32",
1944 AC_FUNC_ATTR_READNONE
);
1947 LLVMValueRef
ac_build_imad(struct ac_llvm_context
*ctx
, LLVMValueRef s0
,
1948 LLVMValueRef s1
, LLVMValueRef s2
)
1950 return LLVMBuildAdd(ctx
->builder
,
1951 LLVMBuildMul(ctx
->builder
, s0
, s1
, ""), s2
, "");
1954 LLVMValueRef
ac_build_fmad(struct ac_llvm_context
*ctx
, LLVMValueRef s0
,
1955 LLVMValueRef s1
, LLVMValueRef s2
)
1957 return LLVMBuildFAdd(ctx
->builder
,
1958 LLVMBuildFMul(ctx
->builder
, s0
, s1
, ""), s2
, "");
1961 void ac_build_waitcnt(struct ac_llvm_context
*ctx
, unsigned simm16
)
1963 LLVMValueRef args
[1] = {
1964 LLVMConstInt(ctx
->i32
, simm16
, false),
1966 ac_build_intrinsic(ctx
, "llvm.amdgcn.s.waitcnt",
1967 ctx
->voidt
, args
, 1, 0);
1970 LLVMValueRef
ac_build_fract(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
1976 if (bitsize
== 32) {
1977 intr
= "llvm.floor.f32";
1980 intr
= "llvm.floor.f64";
1984 LLVMValueRef params
[] = {
1987 LLVMValueRef floor
= ac_build_intrinsic(ctx
, intr
, type
, params
, 1,
1988 AC_FUNC_ATTR_READNONE
);
1989 return LLVMBuildFSub(ctx
->builder
, src0
, floor
, "");
1992 LLVMValueRef
ac_build_isign(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
1995 LLVMValueRef cmp
, val
, zero
, one
;
2015 unreachable(!"invalid bitsize");
2019 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGT
, src0
, zero
, "");
2020 val
= LLVMBuildSelect(ctx
->builder
, cmp
, one
, src0
, "");
2021 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGE
, val
, zero
, "");
2022 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstInt(type
, -1, true), "");
2026 LLVMValueRef
ac_build_fsign(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
2029 LLVMValueRef cmp
, val
, zero
, one
;
2032 if (bitsize
== 32) {
2042 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGT
, src0
, zero
, "");
2043 val
= LLVMBuildSelect(ctx
->builder
, cmp
, one
, src0
, "");
2044 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGE
, val
, zero
, "");
2045 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstReal(type
, -1.0), "");
2049 LLVMValueRef
ac_build_bit_count(struct ac_llvm_context
*ctx
, LLVMValueRef src0
)
2051 LLVMValueRef result
;
2054 bitsize
= ac_get_elem_bits(ctx
, LLVMTypeOf(src0
));
2058 result
= ac_build_intrinsic(ctx
, "llvm.ctpop.i64", ctx
->i64
,
2059 (LLVMValueRef
[]) { src0
}, 1,
2060 AC_FUNC_ATTR_READNONE
);
2062 result
= LLVMBuildTrunc(ctx
->builder
, result
, ctx
->i32
, "");
2065 result
= ac_build_intrinsic(ctx
, "llvm.ctpop.i32", ctx
->i32
,
2066 (LLVMValueRef
[]) { src0
}, 1,
2067 AC_FUNC_ATTR_READNONE
);
2070 result
= ac_build_intrinsic(ctx
, "llvm.ctpop.i16", ctx
->i16
,
2071 (LLVMValueRef
[]) { src0
}, 1,
2072 AC_FUNC_ATTR_READNONE
);
2075 unreachable(!"invalid bitsize");
2082 LLVMValueRef
ac_build_bitfield_reverse(struct ac_llvm_context
*ctx
,
2085 LLVMValueRef result
;
2088 bitsize
= ac_get_elem_bits(ctx
, LLVMTypeOf(src0
));
2092 result
= ac_build_intrinsic(ctx
, "llvm.bitreverse.i32", ctx
->i32
,
2093 (LLVMValueRef
[]) { src0
}, 1,
2094 AC_FUNC_ATTR_READNONE
);
2097 result
= ac_build_intrinsic(ctx
, "llvm.bitreverse.i16", ctx
->i16
,
2098 (LLVMValueRef
[]) { src0
}, 1,
2099 AC_FUNC_ATTR_READNONE
);
2102 unreachable(!"invalid bitsize");
2109 #define AC_EXP_TARGET 0
2110 #define AC_EXP_ENABLED_CHANNELS 1
2111 #define AC_EXP_OUT0 2
2119 struct ac_vs_exp_chan
2123 enum ac_ir_type type
;
2126 struct ac_vs_exp_inst
{
2129 struct ac_vs_exp_chan chan
[4];
2132 struct ac_vs_exports
{
2134 struct ac_vs_exp_inst exp
[VARYING_SLOT_MAX
];
2137 /* Return true if the PARAM export has been eliminated. */
2138 static bool ac_eliminate_const_output(uint8_t *vs_output_param_offset
,
2139 uint32_t num_outputs
,
2140 struct ac_vs_exp_inst
*exp
)
2142 unsigned i
, default_val
; /* SPI_PS_INPUT_CNTL_i.DEFAULT_VAL */
2143 bool is_zero
[4] = {}, is_one
[4] = {};
2145 for (i
= 0; i
< 4; i
++) {
2146 /* It's a constant expression. Undef outputs are eliminated too. */
2147 if (exp
->chan
[i
].type
== AC_IR_UNDEF
) {
2150 } else if (exp
->chan
[i
].type
== AC_IR_CONST
) {
2151 if (exp
->chan
[i
].const_float
== 0)
2153 else if (exp
->chan
[i
].const_float
== 1)
2156 return false; /* other constant */
2161 /* Only certain combinations of 0 and 1 can be eliminated. */
2162 if (is_zero
[0] && is_zero
[1] && is_zero
[2])
2163 default_val
= is_zero
[3] ? 0 : 1;
2164 else if (is_one
[0] && is_one
[1] && is_one
[2])
2165 default_val
= is_zero
[3] ? 2 : 3;
2169 /* The PARAM export can be represented as DEFAULT_VAL. Kill it. */
2170 LLVMInstructionEraseFromParent(exp
->inst
);
2172 /* Change OFFSET to DEFAULT_VAL. */
2173 for (i
= 0; i
< num_outputs
; i
++) {
2174 if (vs_output_param_offset
[i
] == exp
->offset
) {
2175 vs_output_param_offset
[i
] =
2176 AC_EXP_PARAM_DEFAULT_VAL_0000
+ default_val
;
2183 static bool ac_eliminate_duplicated_output(struct ac_llvm_context
*ctx
,
2184 uint8_t *vs_output_param_offset
,
2185 uint32_t num_outputs
,
2186 struct ac_vs_exports
*processed
,
2187 struct ac_vs_exp_inst
*exp
)
2189 unsigned p
, copy_back_channels
= 0;
2191 /* See if the output is already in the list of processed outputs.
2192 * The LLVMValueRef comparison relies on SSA.
2194 for (p
= 0; p
< processed
->num
; p
++) {
2195 bool different
= false;
2197 for (unsigned j
= 0; j
< 4; j
++) {
2198 struct ac_vs_exp_chan
*c1
= &processed
->exp
[p
].chan
[j
];
2199 struct ac_vs_exp_chan
*c2
= &exp
->chan
[j
];
2201 /* Treat undef as a match. */
2202 if (c2
->type
== AC_IR_UNDEF
)
2205 /* If c1 is undef but c2 isn't, we can copy c2 to c1
2206 * and consider the instruction duplicated.
2208 if (c1
->type
== AC_IR_UNDEF
) {
2209 copy_back_channels
|= 1 << j
;
2213 /* Test whether the channels are not equal. */
2214 if (c1
->type
!= c2
->type
||
2215 (c1
->type
== AC_IR_CONST
&&
2216 c1
->const_float
!= c2
->const_float
) ||
2217 (c1
->type
== AC_IR_VALUE
&&
2218 c1
->value
!= c2
->value
)) {
2226 copy_back_channels
= 0;
2228 if (p
== processed
->num
)
2231 /* If a match was found, but the matching export has undef where the new
2232 * one has a normal value, copy the normal value to the undef channel.
2234 struct ac_vs_exp_inst
*match
= &processed
->exp
[p
];
2236 /* Get current enabled channels mask. */
2237 LLVMValueRef arg
= LLVMGetOperand(match
->inst
, AC_EXP_ENABLED_CHANNELS
);
2238 unsigned enabled_channels
= LLVMConstIntGetZExtValue(arg
);
2240 while (copy_back_channels
) {
2241 unsigned chan
= u_bit_scan(©_back_channels
);
2243 assert(match
->chan
[chan
].type
== AC_IR_UNDEF
);
2244 LLVMSetOperand(match
->inst
, AC_EXP_OUT0
+ chan
,
2245 exp
->chan
[chan
].value
);
2246 match
->chan
[chan
] = exp
->chan
[chan
];
2248 /* Update number of enabled channels because the original mask
2249 * is not always 0xf.
2251 enabled_channels
|= (1 << chan
);
2252 LLVMSetOperand(match
->inst
, AC_EXP_ENABLED_CHANNELS
,
2253 LLVMConstInt(ctx
->i32
, enabled_channels
, 0));
2256 /* The PARAM export is duplicated. Kill it. */
2257 LLVMInstructionEraseFromParent(exp
->inst
);
2259 /* Change OFFSET to the matching export. */
2260 for (unsigned i
= 0; i
< num_outputs
; i
++) {
2261 if (vs_output_param_offset
[i
] == exp
->offset
) {
2262 vs_output_param_offset
[i
] = match
->offset
;
2269 void ac_optimize_vs_outputs(struct ac_llvm_context
*ctx
,
2270 LLVMValueRef main_fn
,
2271 uint8_t *vs_output_param_offset
,
2272 uint32_t num_outputs
,
2273 uint8_t *num_param_exports
)
2275 LLVMBasicBlockRef bb
;
2276 bool removed_any
= false;
2277 struct ac_vs_exports exports
;
2281 /* Process all LLVM instructions. */
2282 bb
= LLVMGetFirstBasicBlock(main_fn
);
2284 LLVMValueRef inst
= LLVMGetFirstInstruction(bb
);
2287 LLVMValueRef cur
= inst
;
2288 inst
= LLVMGetNextInstruction(inst
);
2289 struct ac_vs_exp_inst exp
;
2291 if (LLVMGetInstructionOpcode(cur
) != LLVMCall
)
2294 LLVMValueRef callee
= ac_llvm_get_called_value(cur
);
2296 if (!ac_llvm_is_function(callee
))
2299 const char *name
= LLVMGetValueName(callee
);
2300 unsigned num_args
= LLVMCountParams(callee
);
2302 /* Check if this is an export instruction. */
2303 if ((num_args
!= 9 && num_args
!= 8) ||
2304 (strcmp(name
, "llvm.SI.export") &&
2305 strcmp(name
, "llvm.amdgcn.exp.f32")))
2308 LLVMValueRef arg
= LLVMGetOperand(cur
, AC_EXP_TARGET
);
2309 unsigned target
= LLVMConstIntGetZExtValue(arg
);
2311 if (target
< V_008DFC_SQ_EXP_PARAM
)
2314 target
-= V_008DFC_SQ_EXP_PARAM
;
2316 /* Parse the instruction. */
2317 memset(&exp
, 0, sizeof(exp
));
2318 exp
.offset
= target
;
2321 for (unsigned i
= 0; i
< 4; i
++) {
2322 LLVMValueRef v
= LLVMGetOperand(cur
, AC_EXP_OUT0
+ i
);
2324 exp
.chan
[i
].value
= v
;
2326 if (LLVMIsUndef(v
)) {
2327 exp
.chan
[i
].type
= AC_IR_UNDEF
;
2328 } else if (LLVMIsAConstantFP(v
)) {
2329 LLVMBool loses_info
;
2330 exp
.chan
[i
].type
= AC_IR_CONST
;
2331 exp
.chan
[i
].const_float
=
2332 LLVMConstRealGetDouble(v
, &loses_info
);
2334 exp
.chan
[i
].type
= AC_IR_VALUE
;
2338 /* Eliminate constant and duplicated PARAM exports. */
2339 if (ac_eliminate_const_output(vs_output_param_offset
,
2340 num_outputs
, &exp
) ||
2341 ac_eliminate_duplicated_output(ctx
,
2342 vs_output_param_offset
,
2343 num_outputs
, &exports
,
2347 exports
.exp
[exports
.num
++] = exp
;
2350 bb
= LLVMGetNextBasicBlock(bb
);
2353 /* Remove holes in export memory due to removed PARAM exports.
2354 * This is done by renumbering all PARAM exports.
2357 uint8_t old_offset
[VARYING_SLOT_MAX
];
2360 /* Make a copy of the offsets. We need the old version while
2361 * we are modifying some of them. */
2362 memcpy(old_offset
, vs_output_param_offset
,
2363 sizeof(old_offset
));
2365 for (i
= 0; i
< exports
.num
; i
++) {
2366 unsigned offset
= exports
.exp
[i
].offset
;
2368 /* Update vs_output_param_offset. Multiple outputs can
2369 * have the same offset.
2371 for (out
= 0; out
< num_outputs
; out
++) {
2372 if (old_offset
[out
] == offset
)
2373 vs_output_param_offset
[out
] = i
;
2376 /* Change the PARAM offset in the instruction. */
2377 LLVMSetOperand(exports
.exp
[i
].inst
, AC_EXP_TARGET
,
2378 LLVMConstInt(ctx
->i32
,
2379 V_008DFC_SQ_EXP_PARAM
+ i
, 0));
2381 *num_param_exports
= exports
.num
;
2385 void ac_init_exec_full_mask(struct ac_llvm_context
*ctx
)
2387 LLVMValueRef full_mask
= LLVMConstInt(ctx
->i64
, ~0ull, 0);
2388 ac_build_intrinsic(ctx
,
2389 "llvm.amdgcn.init.exec", ctx
->voidt
,
2390 &full_mask
, 1, AC_FUNC_ATTR_CONVERGENT
);
2393 void ac_declare_lds_as_pointer(struct ac_llvm_context
*ctx
)
2395 unsigned lds_size
= ctx
->chip_class
>= CIK
? 65536 : 32768;
2396 ctx
->lds
= LLVMBuildIntToPtr(ctx
->builder
, ctx
->i32_0
,
2397 LLVMPointerType(LLVMArrayType(ctx
->i32
, lds_size
/ 4), AC_ADDR_SPACE_LDS
),
2401 LLVMValueRef
ac_lds_load(struct ac_llvm_context
*ctx
,
2402 LLVMValueRef dw_addr
)
2404 return ac_build_load(ctx
, ctx
->lds
, dw_addr
);
2407 void ac_lds_store(struct ac_llvm_context
*ctx
,
2408 LLVMValueRef dw_addr
,
2411 value
= ac_to_integer(ctx
, value
);
2412 ac_build_indexed_store(ctx
, ctx
->lds
,
2416 LLVMValueRef
ac_find_lsb(struct ac_llvm_context
*ctx
,
2417 LLVMTypeRef dst_type
,
2420 unsigned src0_bitsize
= ac_get_elem_bits(ctx
, LLVMTypeOf(src0
));
2421 const char *intrin_name
;
2425 switch (src0_bitsize
) {
2427 intrin_name
= "llvm.cttz.i64";
2432 intrin_name
= "llvm.cttz.i32";
2437 intrin_name
= "llvm.cttz.i16";
2442 unreachable(!"invalid bitsize");
2445 LLVMValueRef params
[2] = {
2448 /* The value of 1 means that ffs(x=0) = undef, so LLVM won't
2449 * add special code to check for x=0. The reason is that
2450 * the LLVM behavior for x=0 is different from what we
2451 * need here. However, LLVM also assumes that ffs(x) is
2452 * in [0, 31], but GLSL expects that ffs(0) = -1, so
2453 * a conditional assignment to handle 0 is still required.
2455 * The hardware already implements the correct behavior.
2460 LLVMValueRef lsb
= ac_build_intrinsic(ctx
, intrin_name
, type
,
2462 AC_FUNC_ATTR_READNONE
);
2464 if (src0_bitsize
== 64) {
2465 lsb
= LLVMBuildTrunc(ctx
->builder
, lsb
, ctx
->i32
, "");
2468 /* TODO: We need an intrinsic to skip this conditional. */
2469 /* Check for zero: */
2470 return LLVMBuildSelect(ctx
->builder
, LLVMBuildICmp(ctx
->builder
,
2473 LLVMConstInt(ctx
->i32
, -1, 0), lsb
, "");
2476 LLVMTypeRef
ac_array_in_const_addr_space(LLVMTypeRef elem_type
)
2478 return LLVMPointerType(LLVMArrayType(elem_type
, 0),
2479 AC_ADDR_SPACE_CONST
);
2482 LLVMTypeRef
ac_array_in_const32_addr_space(LLVMTypeRef elem_type
)
2484 return LLVMPointerType(LLVMArrayType(elem_type
, 0),
2485 AC_ADDR_SPACE_CONST_32BIT
);
2488 static struct ac_llvm_flow
*
2489 get_current_flow(struct ac_llvm_context
*ctx
)
2491 if (ctx
->flow_depth
> 0)
2492 return &ctx
->flow
[ctx
->flow_depth
- 1];
2496 static struct ac_llvm_flow
*
2497 get_innermost_loop(struct ac_llvm_context
*ctx
)
2499 for (unsigned i
= ctx
->flow_depth
; i
> 0; --i
) {
2500 if (ctx
->flow
[i
- 1].loop_entry_block
)
2501 return &ctx
->flow
[i
- 1];
2506 static struct ac_llvm_flow
*
2507 push_flow(struct ac_llvm_context
*ctx
)
2509 struct ac_llvm_flow
*flow
;
2511 if (ctx
->flow_depth
>= ctx
->flow_depth_max
) {
2512 unsigned new_max
= MAX2(ctx
->flow_depth
<< 1,
2513 AC_LLVM_INITIAL_CF_DEPTH
);
2515 ctx
->flow
= realloc(ctx
->flow
, new_max
* sizeof(*ctx
->flow
));
2516 ctx
->flow_depth_max
= new_max
;
2519 flow
= &ctx
->flow
[ctx
->flow_depth
];
2522 flow
->next_block
= NULL
;
2523 flow
->loop_entry_block
= NULL
;
2527 static void set_basicblock_name(LLVMBasicBlockRef bb
, const char *base
,
2531 snprintf(buf
, sizeof(buf
), "%s%d", base
, label_id
);
2532 LLVMSetValueName(LLVMBasicBlockAsValue(bb
), buf
);
2535 /* Append a basic block at the level of the parent flow.
2537 static LLVMBasicBlockRef
append_basic_block(struct ac_llvm_context
*ctx
,
2540 assert(ctx
->flow_depth
>= 1);
2542 if (ctx
->flow_depth
>= 2) {
2543 struct ac_llvm_flow
*flow
= &ctx
->flow
[ctx
->flow_depth
- 2];
2545 return LLVMInsertBasicBlockInContext(ctx
->context
,
2546 flow
->next_block
, name
);
2549 LLVMValueRef main_fn
=
2550 LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
->builder
));
2551 return LLVMAppendBasicBlockInContext(ctx
->context
, main_fn
, name
);
2554 /* Emit a branch to the given default target for the current block if
2555 * applicable -- that is, if the current block does not already contain a
2556 * branch from a break or continue.
2558 static void emit_default_branch(LLVMBuilderRef builder
,
2559 LLVMBasicBlockRef target
)
2561 if (!LLVMGetBasicBlockTerminator(LLVMGetInsertBlock(builder
)))
2562 LLVMBuildBr(builder
, target
);
2565 void ac_build_bgnloop(struct ac_llvm_context
*ctx
, int label_id
)
2567 struct ac_llvm_flow
*flow
= push_flow(ctx
);
2568 flow
->loop_entry_block
= append_basic_block(ctx
, "LOOP");
2569 flow
->next_block
= append_basic_block(ctx
, "ENDLOOP");
2570 set_basicblock_name(flow
->loop_entry_block
, "loop", label_id
);
2571 LLVMBuildBr(ctx
->builder
, flow
->loop_entry_block
);
2572 LLVMPositionBuilderAtEnd(ctx
->builder
, flow
->loop_entry_block
);
2575 void ac_build_break(struct ac_llvm_context
*ctx
)
2577 struct ac_llvm_flow
*flow
= get_innermost_loop(ctx
);
2578 LLVMBuildBr(ctx
->builder
, flow
->next_block
);
2581 void ac_build_continue(struct ac_llvm_context
*ctx
)
2583 struct ac_llvm_flow
*flow
= get_innermost_loop(ctx
);
2584 LLVMBuildBr(ctx
->builder
, flow
->loop_entry_block
);
2587 void ac_build_else(struct ac_llvm_context
*ctx
, int label_id
)
2589 struct ac_llvm_flow
*current_branch
= get_current_flow(ctx
);
2590 LLVMBasicBlockRef endif_block
;
2592 assert(!current_branch
->loop_entry_block
);
2594 endif_block
= append_basic_block(ctx
, "ENDIF");
2595 emit_default_branch(ctx
->builder
, endif_block
);
2597 LLVMPositionBuilderAtEnd(ctx
->builder
, current_branch
->next_block
);
2598 set_basicblock_name(current_branch
->next_block
, "else", label_id
);
2600 current_branch
->next_block
= endif_block
;
2603 void ac_build_endif(struct ac_llvm_context
*ctx
, int label_id
)
2605 struct ac_llvm_flow
*current_branch
= get_current_flow(ctx
);
2607 assert(!current_branch
->loop_entry_block
);
2609 emit_default_branch(ctx
->builder
, current_branch
->next_block
);
2610 LLVMPositionBuilderAtEnd(ctx
->builder
, current_branch
->next_block
);
2611 set_basicblock_name(current_branch
->next_block
, "endif", label_id
);
2616 void ac_build_endloop(struct ac_llvm_context
*ctx
, int label_id
)
2618 struct ac_llvm_flow
*current_loop
= get_current_flow(ctx
);
2620 assert(current_loop
->loop_entry_block
);
2622 emit_default_branch(ctx
->builder
, current_loop
->loop_entry_block
);
2624 LLVMPositionBuilderAtEnd(ctx
->builder
, current_loop
->next_block
);
2625 set_basicblock_name(current_loop
->next_block
, "endloop", label_id
);
2629 void ac_build_ifcc(struct ac_llvm_context
*ctx
, LLVMValueRef cond
, int label_id
)
2631 struct ac_llvm_flow
*flow
= push_flow(ctx
);
2632 LLVMBasicBlockRef if_block
;
2634 if_block
= append_basic_block(ctx
, "IF");
2635 flow
->next_block
= append_basic_block(ctx
, "ELSE");
2636 set_basicblock_name(if_block
, "if", label_id
);
2637 LLVMBuildCondBr(ctx
->builder
, cond
, if_block
, flow
->next_block
);
2638 LLVMPositionBuilderAtEnd(ctx
->builder
, if_block
);
2641 void ac_build_if(struct ac_llvm_context
*ctx
, LLVMValueRef value
,
2644 LLVMValueRef cond
= LLVMBuildFCmp(ctx
->builder
, LLVMRealUNE
,
2645 value
, ctx
->f32_0
, "");
2646 ac_build_ifcc(ctx
, cond
, label_id
);
2649 void ac_build_uif(struct ac_llvm_context
*ctx
, LLVMValueRef value
,
2652 LLVMValueRef cond
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
2653 ac_to_integer(ctx
, value
),
2655 ac_build_ifcc(ctx
, cond
, label_id
);
2658 LLVMValueRef
ac_build_alloca_undef(struct ac_llvm_context
*ac
, LLVMTypeRef type
,
2661 LLVMBuilderRef builder
= ac
->builder
;
2662 LLVMBasicBlockRef current_block
= LLVMGetInsertBlock(builder
);
2663 LLVMValueRef function
= LLVMGetBasicBlockParent(current_block
);
2664 LLVMBasicBlockRef first_block
= LLVMGetEntryBasicBlock(function
);
2665 LLVMValueRef first_instr
= LLVMGetFirstInstruction(first_block
);
2666 LLVMBuilderRef first_builder
= LLVMCreateBuilderInContext(ac
->context
);
2670 LLVMPositionBuilderBefore(first_builder
, first_instr
);
2672 LLVMPositionBuilderAtEnd(first_builder
, first_block
);
2675 res
= LLVMBuildAlloca(first_builder
, type
, name
);
2676 LLVMDisposeBuilder(first_builder
);
2680 LLVMValueRef
ac_build_alloca(struct ac_llvm_context
*ac
,
2681 LLVMTypeRef type
, const char *name
)
2683 LLVMValueRef ptr
= ac_build_alloca_undef(ac
, type
, name
);
2684 LLVMBuildStore(ac
->builder
, LLVMConstNull(type
), ptr
);
2688 LLVMValueRef
ac_cast_ptr(struct ac_llvm_context
*ctx
, LLVMValueRef ptr
,
2691 int addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
2692 return LLVMBuildBitCast(ctx
->builder
, ptr
,
2693 LLVMPointerType(type
, addr_space
), "");
2696 LLVMValueRef
ac_trim_vector(struct ac_llvm_context
*ctx
, LLVMValueRef value
,
2699 unsigned num_components
= ac_get_llvm_num_components(value
);
2700 if (count
== num_components
)
2703 LLVMValueRef masks
[MAX2(count
, 2)];
2704 masks
[0] = ctx
->i32_0
;
2705 masks
[1] = ctx
->i32_1
;
2706 for (unsigned i
= 2; i
< count
; i
++)
2707 masks
[i
] = LLVMConstInt(ctx
->i32
, i
, false);
2710 return LLVMBuildExtractElement(ctx
->builder
, value
, masks
[0],
2713 LLVMValueRef swizzle
= LLVMConstVector(masks
, count
);
2714 return LLVMBuildShuffleVector(ctx
->builder
, value
, value
, swizzle
, "");
2717 LLVMValueRef
ac_unpack_param(struct ac_llvm_context
*ctx
, LLVMValueRef param
,
2718 unsigned rshift
, unsigned bitwidth
)
2720 LLVMValueRef value
= param
;
2722 value
= LLVMBuildLShr(ctx
->builder
, value
,
2723 LLVMConstInt(ctx
->i32
, rshift
, false), "");
2725 if (rshift
+ bitwidth
< 32) {
2726 unsigned mask
= (1 << bitwidth
) - 1;
2727 value
= LLVMBuildAnd(ctx
->builder
, value
,
2728 LLVMConstInt(ctx
->i32
, mask
, false), "");
2733 /* Adjust the sample index according to FMASK.
2735 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
2736 * which is the identity mapping. Each nibble says which physical sample
2737 * should be fetched to get that sample.
2739 * For example, 0x11111100 means there are only 2 samples stored and
2740 * the second sample covers 3/4 of the pixel. When reading samples 0
2741 * and 1, return physical sample 0 (determined by the first two 0s
2742 * in FMASK), otherwise return physical sample 1.
2744 * The sample index should be adjusted as follows:
2745 * addr[sample_index] = (fmask >> (addr[sample_index] * 4)) & 0xF;
2747 void ac_apply_fmask_to_sample(struct ac_llvm_context
*ac
, LLVMValueRef fmask
,
2748 LLVMValueRef
*addr
, bool is_array_tex
)
2750 struct ac_image_args fmask_load
= {};
2751 fmask_load
.opcode
= ac_image_load
;
2752 fmask_load
.resource
= fmask
;
2753 fmask_load
.dmask
= 0xf;
2754 fmask_load
.dim
= is_array_tex
? ac_image_2darray
: ac_image_2d
;
2756 fmask_load
.coords
[0] = addr
[0];
2757 fmask_load
.coords
[1] = addr
[1];
2759 fmask_load
.coords
[2] = addr
[2];
2761 LLVMValueRef fmask_value
= ac_build_image_opcode(ac
, &fmask_load
);
2762 fmask_value
= LLVMBuildExtractElement(ac
->builder
, fmask_value
,
2765 /* Apply the formula. */
2766 unsigned sample_chan
= is_array_tex
? 3 : 2;
2767 LLVMValueRef final_sample
;
2768 final_sample
= LLVMBuildMul(ac
->builder
, addr
[sample_chan
],
2769 LLVMConstInt(ac
->i32
, 4, 0), "");
2770 final_sample
= LLVMBuildLShr(ac
->builder
, fmask_value
, final_sample
, "");
2771 /* Mask the sample index by 0x7, because 0x8 means an unknown value
2772 * with EQAA, so those will map to 0. */
2773 final_sample
= LLVMBuildAnd(ac
->builder
, final_sample
,
2774 LLVMConstInt(ac
->i32
, 0x7, 0), "");
2776 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
2777 * resource descriptor is 0 (invalid).
2780 tmp
= LLVMBuildBitCast(ac
->builder
, fmask
, ac
->v8i32
, "");
2781 tmp
= LLVMBuildExtractElement(ac
->builder
, tmp
, ac
->i32_1
, "");
2782 tmp
= LLVMBuildICmp(ac
->builder
, LLVMIntNE
, tmp
, ac
->i32_0
, "");
2784 /* Replace the MSAA sample index. */
2785 addr
[sample_chan
] = LLVMBuildSelect(ac
->builder
, tmp
, final_sample
,
2786 addr
[sample_chan
], "");
2790 _ac_build_readlane(struct ac_llvm_context
*ctx
, LLVMValueRef src
, LLVMValueRef lane
)
2792 ac_build_optimization_barrier(ctx
, &src
);
2793 return ac_build_intrinsic(ctx
,
2794 lane
== NULL
? "llvm.amdgcn.readfirstlane" : "llvm.amdgcn.readlane",
2795 LLVMTypeOf(src
), (LLVMValueRef
[]) {
2797 lane
== NULL
? 1 : 2,
2798 AC_FUNC_ATTR_READNONE
|
2799 AC_FUNC_ATTR_CONVERGENT
);
2803 * Builds the "llvm.amdgcn.readlane" or "llvm.amdgcn.readfirstlane" intrinsic.
2806 * @param lane - id of the lane or NULL for the first active lane
2807 * @return value of the lane
2810 ac_build_readlane(struct ac_llvm_context
*ctx
, LLVMValueRef src
, LLVMValueRef lane
)
2812 LLVMTypeRef src_type
= LLVMTypeOf(src
);
2813 src
= ac_to_integer(ctx
, src
);
2814 unsigned bits
= LLVMGetIntTypeWidth(LLVMTypeOf(src
));
2818 ret
= _ac_build_readlane(ctx
, src
, lane
);
2820 assert(bits
% 32 == 0);
2821 LLVMTypeRef vec_type
= LLVMVectorType(ctx
->i32
, bits
/ 32);
2822 LLVMValueRef src_vector
=
2823 LLVMBuildBitCast(ctx
->builder
, src
, vec_type
, "");
2824 ret
= LLVMGetUndef(vec_type
);
2825 for (unsigned i
= 0; i
< bits
/ 32; i
++) {
2826 src
= LLVMBuildExtractElement(ctx
->builder
, src_vector
,
2827 LLVMConstInt(ctx
->i32
, i
, 0), "");
2828 LLVMValueRef ret_comp
= _ac_build_readlane(ctx
, src
, lane
);
2829 ret
= LLVMBuildInsertElement(ctx
->builder
, ret
, ret_comp
,
2830 LLVMConstInt(ctx
->i32
, i
, 0), "");
2833 return LLVMBuildBitCast(ctx
->builder
, ret
, src_type
, "");
2837 ac_build_writelane(struct ac_llvm_context
*ctx
, LLVMValueRef src
, LLVMValueRef value
, LLVMValueRef lane
)
2839 /* TODO: Use the actual instruction when LLVM adds an intrinsic for it.
2841 LLVMValueRef pred
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, lane
,
2842 ac_get_thread_id(ctx
), "");
2843 return LLVMBuildSelect(ctx
->builder
, pred
, value
, src
, "");
2847 ac_build_mbcnt(struct ac_llvm_context
*ctx
, LLVMValueRef mask
)
2849 LLVMValueRef mask_vec
= LLVMBuildBitCast(ctx
->builder
, mask
,
2850 LLVMVectorType(ctx
->i32
, 2),
2852 LLVMValueRef mask_lo
= LLVMBuildExtractElement(ctx
->builder
, mask_vec
,
2854 LLVMValueRef mask_hi
= LLVMBuildExtractElement(ctx
->builder
, mask_vec
,
2857 ac_build_intrinsic(ctx
, "llvm.amdgcn.mbcnt.lo", ctx
->i32
,
2858 (LLVMValueRef
[]) { mask_lo
, ctx
->i32_0
},
2859 2, AC_FUNC_ATTR_READNONE
);
2860 val
= ac_build_intrinsic(ctx
, "llvm.amdgcn.mbcnt.hi", ctx
->i32
,
2861 (LLVMValueRef
[]) { mask_hi
, val
},
2862 2, AC_FUNC_ATTR_READNONE
);
2867 _dpp_quad_perm
= 0x000,
2868 _dpp_row_sl
= 0x100,
2869 _dpp_row_sr
= 0x110,
2870 _dpp_row_rr
= 0x120,
2875 dpp_row_mirror
= 0x140,
2876 dpp_row_half_mirror
= 0x141,
2877 dpp_row_bcast15
= 0x142,
2878 dpp_row_bcast31
= 0x143
2881 static inline enum dpp_ctrl
2882 dpp_quad_perm(unsigned lane0
, unsigned lane1
, unsigned lane2
, unsigned lane3
)
2884 assert(lane0
< 4 && lane1
< 4 && lane2
< 4 && lane3
< 4);
2885 return _dpp_quad_perm
| lane0
| (lane1
<< 2) | (lane2
<< 4) | (lane3
<< 6);
2888 static inline enum dpp_ctrl
2889 dpp_row_sl(unsigned amount
)
2891 assert(amount
> 0 && amount
< 16);
2892 return _dpp_row_sl
| amount
;
2895 static inline enum dpp_ctrl
2896 dpp_row_sr(unsigned amount
)
2898 assert(amount
> 0 && amount
< 16);
2899 return _dpp_row_sr
| amount
;
2903 _ac_build_dpp(struct ac_llvm_context
*ctx
, LLVMValueRef old
, LLVMValueRef src
,
2904 enum dpp_ctrl dpp_ctrl
, unsigned row_mask
, unsigned bank_mask
,
2907 return ac_build_intrinsic(ctx
, "llvm.amdgcn.update.dpp.i32",
2911 LLVMConstInt(ctx
->i32
, dpp_ctrl
, 0),
2912 LLVMConstInt(ctx
->i32
, row_mask
, 0),
2913 LLVMConstInt(ctx
->i32
, bank_mask
, 0),
2914 LLVMConstInt(ctx
->i1
, bound_ctrl
, 0) },
2915 6, AC_FUNC_ATTR_READNONE
| AC_FUNC_ATTR_CONVERGENT
);
2919 ac_build_dpp(struct ac_llvm_context
*ctx
, LLVMValueRef old
, LLVMValueRef src
,
2920 enum dpp_ctrl dpp_ctrl
, unsigned row_mask
, unsigned bank_mask
,
2923 LLVMTypeRef src_type
= LLVMTypeOf(src
);
2924 src
= ac_to_integer(ctx
, src
);
2925 old
= ac_to_integer(ctx
, old
);
2926 unsigned bits
= LLVMGetIntTypeWidth(LLVMTypeOf(src
));
2929 ret
= _ac_build_dpp(ctx
, old
, src
, dpp_ctrl
, row_mask
,
2930 bank_mask
, bound_ctrl
);
2932 assert(bits
% 32 == 0);
2933 LLVMTypeRef vec_type
= LLVMVectorType(ctx
->i32
, bits
/ 32);
2934 LLVMValueRef src_vector
=
2935 LLVMBuildBitCast(ctx
->builder
, src
, vec_type
, "");
2936 LLVMValueRef old_vector
=
2937 LLVMBuildBitCast(ctx
->builder
, old
, vec_type
, "");
2938 ret
= LLVMGetUndef(vec_type
);
2939 for (unsigned i
= 0; i
< bits
/ 32; i
++) {
2940 src
= LLVMBuildExtractElement(ctx
->builder
, src_vector
,
2941 LLVMConstInt(ctx
->i32
, i
,
2943 old
= LLVMBuildExtractElement(ctx
->builder
, old_vector
,
2944 LLVMConstInt(ctx
->i32
, i
,
2946 LLVMValueRef ret_comp
= _ac_build_dpp(ctx
, old
, src
,
2951 ret
= LLVMBuildInsertElement(ctx
->builder
, ret
,
2953 LLVMConstInt(ctx
->i32
, i
,
2957 return LLVMBuildBitCast(ctx
->builder
, ret
, src_type
, "");
2960 static inline unsigned
2961 ds_pattern_bitmode(unsigned and_mask
, unsigned or_mask
, unsigned xor_mask
)
2963 assert(and_mask
< 32 && or_mask
< 32 && xor_mask
< 32);
2964 return and_mask
| (or_mask
<< 5) | (xor_mask
<< 10);
2968 _ac_build_ds_swizzle(struct ac_llvm_context
*ctx
, LLVMValueRef src
, unsigned mask
)
2970 return ac_build_intrinsic(ctx
, "llvm.amdgcn.ds.swizzle",
2971 LLVMTypeOf(src
), (LLVMValueRef
[]) {
2972 src
, LLVMConstInt(ctx
->i32
, mask
, 0) },
2973 2, AC_FUNC_ATTR_READNONE
| AC_FUNC_ATTR_CONVERGENT
);
2977 ac_build_ds_swizzle(struct ac_llvm_context
*ctx
, LLVMValueRef src
, unsigned mask
)
2979 LLVMTypeRef src_type
= LLVMTypeOf(src
);
2980 src
= ac_to_integer(ctx
, src
);
2981 unsigned bits
= LLVMGetIntTypeWidth(LLVMTypeOf(src
));
2984 ret
= _ac_build_ds_swizzle(ctx
, src
, mask
);
2986 assert(bits
% 32 == 0);
2987 LLVMTypeRef vec_type
= LLVMVectorType(ctx
->i32
, bits
/ 32);
2988 LLVMValueRef src_vector
=
2989 LLVMBuildBitCast(ctx
->builder
, src
, vec_type
, "");
2990 ret
= LLVMGetUndef(vec_type
);
2991 for (unsigned i
= 0; i
< bits
/ 32; i
++) {
2992 src
= LLVMBuildExtractElement(ctx
->builder
, src_vector
,
2993 LLVMConstInt(ctx
->i32
, i
,
2995 LLVMValueRef ret_comp
= _ac_build_ds_swizzle(ctx
, src
,
2997 ret
= LLVMBuildInsertElement(ctx
->builder
, ret
,
2999 LLVMConstInt(ctx
->i32
, i
,
3003 return LLVMBuildBitCast(ctx
->builder
, ret
, src_type
, "");
3007 ac_build_wwm(struct ac_llvm_context
*ctx
, LLVMValueRef src
)
3009 char name
[32], type
[8];
3010 ac_build_type_name_for_intr(LLVMTypeOf(src
), type
, sizeof(type
));
3011 snprintf(name
, sizeof(name
), "llvm.amdgcn.wwm.%s", type
);
3012 return ac_build_intrinsic(ctx
, name
, LLVMTypeOf(src
),
3013 (LLVMValueRef
[]) { src
}, 1,
3014 AC_FUNC_ATTR_READNONE
);
3018 ac_build_set_inactive(struct ac_llvm_context
*ctx
, LLVMValueRef src
,
3019 LLVMValueRef inactive
)
3021 char name
[33], type
[8];
3022 LLVMTypeRef src_type
= LLVMTypeOf(src
);
3023 src
= ac_to_integer(ctx
, src
);
3024 inactive
= ac_to_integer(ctx
, inactive
);
3025 ac_build_type_name_for_intr(LLVMTypeOf(src
), type
, sizeof(type
));
3026 snprintf(name
, sizeof(name
), "llvm.amdgcn.set.inactive.%s", type
);
3028 ac_build_intrinsic(ctx
, name
,
3029 LLVMTypeOf(src
), (LLVMValueRef
[]) {
3031 AC_FUNC_ATTR_READNONE
|
3032 AC_FUNC_ATTR_CONVERGENT
);
3033 return LLVMBuildBitCast(ctx
->builder
, ret
, src_type
, "");
3037 get_reduction_identity(struct ac_llvm_context
*ctx
, nir_op op
, unsigned type_size
)
3039 if (type_size
== 4) {
3041 case nir_op_iadd
: return ctx
->i32_0
;
3042 case nir_op_fadd
: return ctx
->f32_0
;
3043 case nir_op_imul
: return ctx
->i32_1
;
3044 case nir_op_fmul
: return ctx
->f32_1
;
3045 case nir_op_imin
: return LLVMConstInt(ctx
->i32
, INT32_MAX
, 0);
3046 case nir_op_umin
: return LLVMConstInt(ctx
->i32
, UINT32_MAX
, 0);
3047 case nir_op_fmin
: return LLVMConstReal(ctx
->f32
, INFINITY
);
3048 case nir_op_imax
: return LLVMConstInt(ctx
->i32
, INT32_MIN
, 0);
3049 case nir_op_umax
: return ctx
->i32_0
;
3050 case nir_op_fmax
: return LLVMConstReal(ctx
->f32
, -INFINITY
);
3051 case nir_op_iand
: return LLVMConstInt(ctx
->i32
, -1, 0);
3052 case nir_op_ior
: return ctx
->i32_0
;
3053 case nir_op_ixor
: return ctx
->i32_0
;
3055 unreachable("bad reduction intrinsic");
3057 } else { /* type_size == 64bit */
3059 case nir_op_iadd
: return ctx
->i64_0
;
3060 case nir_op_fadd
: return ctx
->f64_0
;
3061 case nir_op_imul
: return ctx
->i64_1
;
3062 case nir_op_fmul
: return ctx
->f64_1
;
3063 case nir_op_imin
: return LLVMConstInt(ctx
->i64
, INT64_MAX
, 0);
3064 case nir_op_umin
: return LLVMConstInt(ctx
->i64
, UINT64_MAX
, 0);
3065 case nir_op_fmin
: return LLVMConstReal(ctx
->f64
, INFINITY
);
3066 case nir_op_imax
: return LLVMConstInt(ctx
->i64
, INT64_MIN
, 0);
3067 case nir_op_umax
: return ctx
->i64_0
;
3068 case nir_op_fmax
: return LLVMConstReal(ctx
->f64
, -INFINITY
);
3069 case nir_op_iand
: return LLVMConstInt(ctx
->i64
, -1, 0);
3070 case nir_op_ior
: return ctx
->i64_0
;
3071 case nir_op_ixor
: return ctx
->i64_0
;
3073 unreachable("bad reduction intrinsic");
3079 ac_build_alu_op(struct ac_llvm_context
*ctx
, LLVMValueRef lhs
, LLVMValueRef rhs
, nir_op op
)
3081 bool _64bit
= ac_get_type_size(LLVMTypeOf(lhs
)) == 8;
3083 case nir_op_iadd
: return LLVMBuildAdd(ctx
->builder
, lhs
, rhs
, "");
3084 case nir_op_fadd
: return LLVMBuildFAdd(ctx
->builder
, lhs
, rhs
, "");
3085 case nir_op_imul
: return LLVMBuildMul(ctx
->builder
, lhs
, rhs
, "");
3086 case nir_op_fmul
: return LLVMBuildFMul(ctx
->builder
, lhs
, rhs
, "");
3087 case nir_op_imin
: return LLVMBuildSelect(ctx
->builder
,
3088 LLVMBuildICmp(ctx
->builder
, LLVMIntSLT
, lhs
, rhs
, ""),
3090 case nir_op_umin
: return LLVMBuildSelect(ctx
->builder
,
3091 LLVMBuildICmp(ctx
->builder
, LLVMIntULT
, lhs
, rhs
, ""),
3093 case nir_op_fmin
: return ac_build_intrinsic(ctx
,
3094 _64bit
? "llvm.minnum.f64" : "llvm.minnum.f32",
3095 _64bit
? ctx
->f64
: ctx
->f32
,
3096 (LLVMValueRef
[]){lhs
, rhs
}, 2, AC_FUNC_ATTR_READNONE
);
3097 case nir_op_imax
: return LLVMBuildSelect(ctx
->builder
,
3098 LLVMBuildICmp(ctx
->builder
, LLVMIntSGT
, lhs
, rhs
, ""),
3100 case nir_op_umax
: return LLVMBuildSelect(ctx
->builder
,
3101 LLVMBuildICmp(ctx
->builder
, LLVMIntUGT
, lhs
, rhs
, ""),
3103 case nir_op_fmax
: return ac_build_intrinsic(ctx
,
3104 _64bit
? "llvm.maxnum.f64" : "llvm.maxnum.f32",
3105 _64bit
? ctx
->f64
: ctx
->f32
,
3106 (LLVMValueRef
[]){lhs
, rhs
}, 2, AC_FUNC_ATTR_READNONE
);
3107 case nir_op_iand
: return LLVMBuildAnd(ctx
->builder
, lhs
, rhs
, "");
3108 case nir_op_ior
: return LLVMBuildOr(ctx
->builder
, lhs
, rhs
, "");
3109 case nir_op_ixor
: return LLVMBuildXor(ctx
->builder
, lhs
, rhs
, "");
3111 unreachable("bad reduction intrinsic");
3116 * \param maxprefix specifies that the result only needs to be correct for a
3117 * prefix of this many threads
3119 * TODO: add inclusive and excluse scan functions for SI chip class.
3122 ac_build_scan(struct ac_llvm_context
*ctx
, nir_op op
, LLVMValueRef src
, LLVMValueRef identity
,
3125 LLVMValueRef result
, tmp
;
3129 tmp
= ac_build_dpp(ctx
, identity
, src
, dpp_row_sr(1), 0xf, 0xf, false);
3130 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
3133 tmp
= ac_build_dpp(ctx
, identity
, src
, dpp_row_sr(2), 0xf, 0xf, false);
3134 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
3137 tmp
= ac_build_dpp(ctx
, identity
, src
, dpp_row_sr(3), 0xf, 0xf, false);
3138 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
3141 tmp
= ac_build_dpp(ctx
, identity
, result
, dpp_row_sr(4), 0xf, 0xe, false);
3142 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
3145 tmp
= ac_build_dpp(ctx
, identity
, result
, dpp_row_sr(8), 0xf, 0xc, false);
3146 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
3147 if (maxprefix
<= 16)
3149 tmp
= ac_build_dpp(ctx
, identity
, result
, dpp_row_bcast15
, 0xa, 0xf, false);
3150 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
3151 if (maxprefix
<= 32)
3153 tmp
= ac_build_dpp(ctx
, identity
, result
, dpp_row_bcast31
, 0xc, 0xf, false);
3154 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
3159 ac_build_inclusive_scan(struct ac_llvm_context
*ctx
, LLVMValueRef src
, nir_op op
)
3161 LLVMValueRef result
;
3163 if (LLVMTypeOf(src
) == ctx
->i1
&& op
== nir_op_iadd
) {
3164 LLVMBuilderRef builder
= ctx
->builder
;
3165 src
= LLVMBuildZExt(builder
, src
, ctx
->i32
, "");
3166 result
= ac_build_ballot(ctx
, src
);
3167 result
= ac_build_mbcnt(ctx
, result
);
3168 result
= LLVMBuildAdd(builder
, result
, src
, "");
3172 ac_build_optimization_barrier(ctx
, &src
);
3174 LLVMValueRef identity
=
3175 get_reduction_identity(ctx
, op
, ac_get_type_size(LLVMTypeOf(src
)));
3176 result
= LLVMBuildBitCast(ctx
->builder
, ac_build_set_inactive(ctx
, src
, identity
),
3177 LLVMTypeOf(identity
), "");
3178 result
= ac_build_scan(ctx
, op
, result
, identity
, 64);
3180 return ac_build_wwm(ctx
, result
);
3184 ac_build_exclusive_scan(struct ac_llvm_context
*ctx
, LLVMValueRef src
, nir_op op
)
3186 LLVMValueRef result
;
3188 if (LLVMTypeOf(src
) == ctx
->i1
&& op
== nir_op_iadd
) {
3189 LLVMBuilderRef builder
= ctx
->builder
;
3190 src
= LLVMBuildZExt(builder
, src
, ctx
->i32
, "");
3191 result
= ac_build_ballot(ctx
, src
);
3192 result
= ac_build_mbcnt(ctx
, result
);
3196 ac_build_optimization_barrier(ctx
, &src
);
3198 LLVMValueRef identity
=
3199 get_reduction_identity(ctx
, op
, ac_get_type_size(LLVMTypeOf(src
)));
3200 result
= LLVMBuildBitCast(ctx
->builder
, ac_build_set_inactive(ctx
, src
, identity
),
3201 LLVMTypeOf(identity
), "");
3202 result
= ac_build_dpp(ctx
, identity
, result
, dpp_wf_sr1
, 0xf, 0xf, false);
3203 result
= ac_build_scan(ctx
, op
, result
, identity
, 64);
3205 return ac_build_wwm(ctx
, result
);
3209 ac_build_reduce(struct ac_llvm_context
*ctx
, LLVMValueRef src
, nir_op op
, unsigned cluster_size
)
3211 if (cluster_size
== 1) return src
;
3212 ac_build_optimization_barrier(ctx
, &src
);
3213 LLVMValueRef result
, swap
;
3214 LLVMValueRef identity
= get_reduction_identity(ctx
, op
,
3215 ac_get_type_size(LLVMTypeOf(src
)));
3216 result
= LLVMBuildBitCast(ctx
->builder
,
3217 ac_build_set_inactive(ctx
, src
, identity
),
3218 LLVMTypeOf(identity
), "");
3219 swap
= ac_build_quad_swizzle(ctx
, result
, 1, 0, 3, 2);
3220 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
3221 if (cluster_size
== 2) return ac_build_wwm(ctx
, result
);
3223 swap
= ac_build_quad_swizzle(ctx
, result
, 2, 3, 0, 1);
3224 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
3225 if (cluster_size
== 4) return ac_build_wwm(ctx
, result
);
3227 if (ctx
->chip_class
>= VI
)
3228 swap
= ac_build_dpp(ctx
, identity
, result
, dpp_row_half_mirror
, 0xf, 0xf, false);
3230 swap
= ac_build_ds_swizzle(ctx
, result
, ds_pattern_bitmode(0x1f, 0, 0x04));
3231 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
3232 if (cluster_size
== 8) return ac_build_wwm(ctx
, result
);
3234 if (ctx
->chip_class
>= VI
)
3235 swap
= ac_build_dpp(ctx
, identity
, result
, dpp_row_mirror
, 0xf, 0xf, false);
3237 swap
= ac_build_ds_swizzle(ctx
, result
, ds_pattern_bitmode(0x1f, 0, 0x08));
3238 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
3239 if (cluster_size
== 16) return ac_build_wwm(ctx
, result
);
3241 if (ctx
->chip_class
>= VI
&& cluster_size
!= 32)
3242 swap
= ac_build_dpp(ctx
, identity
, result
, dpp_row_bcast15
, 0xa, 0xf, false);
3244 swap
= ac_build_ds_swizzle(ctx
, result
, ds_pattern_bitmode(0x1f, 0, 0x10));
3245 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
3246 if (cluster_size
== 32) return ac_build_wwm(ctx
, result
);
3248 if (ctx
->chip_class
>= VI
) {
3249 swap
= ac_build_dpp(ctx
, identity
, result
, dpp_row_bcast31
, 0xc, 0xf, false);
3250 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
3251 result
= ac_build_readlane(ctx
, result
, LLVMConstInt(ctx
->i32
, 63, 0));
3252 return ac_build_wwm(ctx
, result
);
3254 swap
= ac_build_readlane(ctx
, result
, ctx
->i32_0
);
3255 result
= ac_build_readlane(ctx
, result
, LLVMConstInt(ctx
->i32
, 32, 0));
3256 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
3257 return ac_build_wwm(ctx
, result
);
3262 * "Top half" of a scan that reduces per-wave values across an entire
3265 * The source value must be present in the highest lane of the wave, and the
3266 * highest lane must be live.
3269 ac_build_wg_wavescan_top(struct ac_llvm_context
*ctx
, struct ac_wg_scan
*ws
)
3271 if (ws
->maxwaves
<= 1)
3274 const LLVMValueRef i32_63
= LLVMConstInt(ctx
->i32
, 63, false);
3275 LLVMBuilderRef builder
= ctx
->builder
;
3276 LLVMValueRef tid
= ac_get_thread_id(ctx
);
3279 tmp
= LLVMBuildICmp(builder
, LLVMIntEQ
, tid
, i32_63
, "");
3280 ac_build_ifcc(ctx
, tmp
, 1000);
3281 LLVMBuildStore(builder
, ws
->src
, LLVMBuildGEP(builder
, ws
->scratch
, &ws
->waveidx
, 1, ""));
3282 ac_build_endif(ctx
, 1000);
3286 * "Bottom half" of a scan that reduces per-wave values across an entire
3289 * The caller must place a barrier between the top and bottom halves.
3292 ac_build_wg_wavescan_bottom(struct ac_llvm_context
*ctx
, struct ac_wg_scan
*ws
)
3294 const LLVMTypeRef type
= LLVMTypeOf(ws
->src
);
3295 const LLVMValueRef identity
=
3296 get_reduction_identity(ctx
, ws
->op
, ac_get_type_size(type
));
3298 if (ws
->maxwaves
<= 1) {
3299 ws
->result_reduce
= ws
->src
;
3300 ws
->result_inclusive
= ws
->src
;
3301 ws
->result_exclusive
= identity
;
3304 assert(ws
->maxwaves
<= 32);
3306 LLVMBuilderRef builder
= ctx
->builder
;
3307 LLVMValueRef tid
= ac_get_thread_id(ctx
);
3308 LLVMBasicBlockRef bbs
[2];
3309 LLVMValueRef phivalues_scan
[2];
3310 LLVMValueRef tmp
, tmp2
;
3312 bbs
[0] = LLVMGetInsertBlock(builder
);
3313 phivalues_scan
[0] = LLVMGetUndef(type
);
3315 if (ws
->enable_reduce
)
3316 tmp
= LLVMBuildICmp(builder
, LLVMIntULT
, tid
, ws
->numwaves
, "");
3317 else if (ws
->enable_inclusive
)
3318 tmp
= LLVMBuildICmp(builder
, LLVMIntULE
, tid
, ws
->waveidx
, "");
3320 tmp
= LLVMBuildICmp(builder
, LLVMIntULT
, tid
, ws
->waveidx
, "");
3321 ac_build_ifcc(ctx
, tmp
, 1001);
3323 tmp
= LLVMBuildLoad(builder
, LLVMBuildGEP(builder
, ws
->scratch
, &tid
, 1, ""), "");
3325 ac_build_optimization_barrier(ctx
, &tmp
);
3327 bbs
[1] = LLVMGetInsertBlock(builder
);
3328 phivalues_scan
[1] = ac_build_scan(ctx
, ws
->op
, tmp
, identity
, ws
->maxwaves
);
3330 ac_build_endif(ctx
, 1001);
3332 const LLVMValueRef scan
= ac_build_phi(ctx
, type
, 2, phivalues_scan
, bbs
);
3334 if (ws
->enable_reduce
) {
3335 tmp
= LLVMBuildSub(builder
, ws
->numwaves
, ctx
->i32_1
, "");
3336 ws
->result_reduce
= ac_build_readlane(ctx
, scan
, tmp
);
3338 if (ws
->enable_inclusive
)
3339 ws
->result_inclusive
= ac_build_readlane(ctx
, scan
, ws
->waveidx
);
3340 if (ws
->enable_exclusive
) {
3341 tmp
= LLVMBuildSub(builder
, ws
->waveidx
, ctx
->i32_1
, "");
3342 tmp
= ac_build_readlane(ctx
, scan
, tmp
);
3343 tmp2
= LLVMBuildICmp(builder
, LLVMIntEQ
, ws
->waveidx
, ctx
->i32_0
, "");
3344 ws
->result_exclusive
= LLVMBuildSelect(builder
, tmp2
, identity
, tmp
, "");
3349 * Inclusive scan of a per-wave value across an entire workgroup.
3351 * This implies an s_barrier instruction.
3353 * Unlike ac_build_inclusive_scan, the caller \em must ensure that all threads
3354 * of the workgroup are live. (This requirement cannot easily be relaxed in a
3355 * useful manner because of the barrier in the algorithm.)
3358 ac_build_wg_wavescan(struct ac_llvm_context
*ctx
, struct ac_wg_scan
*ws
)
3360 ac_build_wg_wavescan_top(ctx
, ws
);
3361 ac_build_s_barrier(ctx
);
3362 ac_build_wg_wavescan_bottom(ctx
, ws
);
3366 * "Top half" of a scan that reduces per-thread values across an entire
3369 * All lanes must be active when this code runs.
3372 ac_build_wg_scan_top(struct ac_llvm_context
*ctx
, struct ac_wg_scan
*ws
)
3374 if (ws
->enable_exclusive
) {
3375 ws
->extra
= ac_build_exclusive_scan(ctx
, ws
->src
, ws
->op
);
3376 if (LLVMTypeOf(ws
->src
) == ctx
->i1
&& ws
->op
== nir_op_iadd
)
3377 ws
->src
= LLVMBuildZExt(ctx
->builder
, ws
->src
, ctx
->i32
, "");
3378 ws
->src
= ac_build_alu_op(ctx
, ws
->extra
, ws
->src
, ws
->op
);
3380 ws
->src
= ac_build_inclusive_scan(ctx
, ws
->src
, ws
->op
);
3383 bool enable_inclusive
= ws
->enable_inclusive
;
3384 bool enable_exclusive
= ws
->enable_exclusive
;
3385 ws
->enable_inclusive
= false;
3386 ws
->enable_exclusive
= ws
->enable_exclusive
|| enable_inclusive
;
3387 ac_build_wg_wavescan_top(ctx
, ws
);
3388 ws
->enable_inclusive
= enable_inclusive
;
3389 ws
->enable_exclusive
= enable_exclusive
;
3393 * "Bottom half" of a scan that reduces per-thread values across an entire
3396 * The caller must place a barrier between the top and bottom halves.
3399 ac_build_wg_scan_bottom(struct ac_llvm_context
*ctx
, struct ac_wg_scan
*ws
)
3401 bool enable_inclusive
= ws
->enable_inclusive
;
3402 bool enable_exclusive
= ws
->enable_exclusive
;
3403 ws
->enable_inclusive
= false;
3404 ws
->enable_exclusive
= ws
->enable_exclusive
|| enable_inclusive
;
3405 ac_build_wg_wavescan_bottom(ctx
, ws
);
3406 ws
->enable_inclusive
= enable_inclusive
;
3407 ws
->enable_exclusive
= enable_exclusive
;
3409 /* ws->result_reduce is already the correct value */
3410 if (ws
->enable_inclusive
)
3411 ws
->result_inclusive
= ac_build_alu_op(ctx
, ws
->result_exclusive
, ws
->src
, ws
->op
);
3412 if (ws
->enable_exclusive
)
3413 ws
->result_exclusive
= ac_build_alu_op(ctx
, ws
->result_exclusive
, ws
->extra
, ws
->op
);
3417 * A scan that reduces per-thread values across an entire workgroup.
3419 * The caller must ensure that all lanes are active when this code runs
3420 * (WWM is insufficient!), because there is an implied barrier.
3423 ac_build_wg_scan(struct ac_llvm_context
*ctx
, struct ac_wg_scan
*ws
)
3425 ac_build_wg_scan_top(ctx
, ws
);
3426 ac_build_s_barrier(ctx
);
3427 ac_build_wg_scan_bottom(ctx
, ws
);
3431 ac_build_quad_swizzle(struct ac_llvm_context
*ctx
, LLVMValueRef src
,
3432 unsigned lane0
, unsigned lane1
, unsigned lane2
, unsigned lane3
)
3434 unsigned mask
= dpp_quad_perm(lane0
, lane1
, lane2
, lane3
);
3435 if (ctx
->chip_class
>= VI
) {
3436 return ac_build_dpp(ctx
, src
, src
, mask
, 0xf, 0xf, false);
3438 return ac_build_ds_swizzle(ctx
, src
, (1 << 15) | mask
);
3443 ac_build_shuffle(struct ac_llvm_context
*ctx
, LLVMValueRef src
, LLVMValueRef index
)
3445 index
= LLVMBuildMul(ctx
->builder
, index
, LLVMConstInt(ctx
->i32
, 4, 0), "");
3446 return ac_build_intrinsic(ctx
,
3447 "llvm.amdgcn.ds.bpermute", ctx
->i32
,
3448 (LLVMValueRef
[]) {index
, src
}, 2,
3449 AC_FUNC_ATTR_READNONE
|
3450 AC_FUNC_ATTR_CONVERGENT
);