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
,
66 ctx
->context
= LLVMContextCreate();
68 ctx
->chip_class
= chip_class
;
70 ctx
->wave_size
= wave_size
;
74 ctx
->voidt
= LLVMVoidTypeInContext(ctx
->context
);
75 ctx
->i1
= LLVMInt1TypeInContext(ctx
->context
);
76 ctx
->i8
= LLVMInt8TypeInContext(ctx
->context
);
77 ctx
->i16
= LLVMIntTypeInContext(ctx
->context
, 16);
78 ctx
->i32
= LLVMIntTypeInContext(ctx
->context
, 32);
79 ctx
->i64
= LLVMIntTypeInContext(ctx
->context
, 64);
80 ctx
->intptr
= ctx
->i32
;
81 ctx
->f16
= LLVMHalfTypeInContext(ctx
->context
);
82 ctx
->f32
= LLVMFloatTypeInContext(ctx
->context
);
83 ctx
->f64
= LLVMDoubleTypeInContext(ctx
->context
);
84 ctx
->v2i16
= LLVMVectorType(ctx
->i16
, 2);
85 ctx
->v2i32
= LLVMVectorType(ctx
->i32
, 2);
86 ctx
->v3i32
= LLVMVectorType(ctx
->i32
, 3);
87 ctx
->v4i32
= LLVMVectorType(ctx
->i32
, 4);
88 ctx
->v2f32
= LLVMVectorType(ctx
->f32
, 2);
89 ctx
->v3f32
= LLVMVectorType(ctx
->f32
, 3);
90 ctx
->v4f32
= LLVMVectorType(ctx
->f32
, 4);
91 ctx
->v8i32
= LLVMVectorType(ctx
->i32
, 8);
93 ctx
->i8_0
= LLVMConstInt(ctx
->i8
, 0, false);
94 ctx
->i8_1
= LLVMConstInt(ctx
->i8
, 1, false);
95 ctx
->i16_0
= LLVMConstInt(ctx
->i16
, 0, false);
96 ctx
->i16_1
= LLVMConstInt(ctx
->i16
, 1, false);
97 ctx
->i32_0
= LLVMConstInt(ctx
->i32
, 0, false);
98 ctx
->i32_1
= LLVMConstInt(ctx
->i32
, 1, false);
99 ctx
->i64_0
= LLVMConstInt(ctx
->i64
, 0, false);
100 ctx
->i64_1
= LLVMConstInt(ctx
->i64
, 1, false);
101 ctx
->f16_0
= LLVMConstReal(ctx
->f16
, 0.0);
102 ctx
->f16_1
= LLVMConstReal(ctx
->f16
, 1.0);
103 ctx
->f32_0
= LLVMConstReal(ctx
->f32
, 0.0);
104 ctx
->f32_1
= LLVMConstReal(ctx
->f32
, 1.0);
105 ctx
->f64_0
= LLVMConstReal(ctx
->f64
, 0.0);
106 ctx
->f64_1
= LLVMConstReal(ctx
->f64
, 1.0);
108 ctx
->i1false
= LLVMConstInt(ctx
->i1
, 0, false);
109 ctx
->i1true
= LLVMConstInt(ctx
->i1
, 1, false);
111 ctx
->range_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
114 ctx
->invariant_load_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
115 "invariant.load", 14);
117 ctx
->fpmath_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
, "fpmath", 6);
119 args
[0] = LLVMConstReal(ctx
->f32
, 2.5);
120 ctx
->fpmath_md_2p5_ulp
= LLVMMDNodeInContext(ctx
->context
, args
, 1);
122 ctx
->uniform_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
123 "amdgpu.uniform", 14);
125 ctx
->empty_md
= LLVMMDNodeInContext(ctx
->context
, NULL
, 0);
129 ac_llvm_context_dispose(struct ac_llvm_context
*ctx
)
133 ctx
->flow_depth_max
= 0;
137 ac_get_llvm_num_components(LLVMValueRef value
)
139 LLVMTypeRef type
= LLVMTypeOf(value
);
140 unsigned num_components
= LLVMGetTypeKind(type
) == LLVMVectorTypeKind
141 ? LLVMGetVectorSize(type
)
143 return num_components
;
147 ac_llvm_extract_elem(struct ac_llvm_context
*ac
,
151 if (LLVMGetTypeKind(LLVMTypeOf(value
)) != LLVMVectorTypeKind
) {
156 return LLVMBuildExtractElement(ac
->builder
, value
,
157 LLVMConstInt(ac
->i32
, index
, false), "");
161 ac_get_elem_bits(struct ac_llvm_context
*ctx
, LLVMTypeRef type
)
163 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
)
164 type
= LLVMGetElementType(type
);
166 if (LLVMGetTypeKind(type
) == LLVMIntegerTypeKind
)
167 return LLVMGetIntTypeWidth(type
);
169 if (type
== ctx
->f16
)
171 if (type
== ctx
->f32
)
173 if (type
== ctx
->f64
)
176 unreachable("Unhandled type kind in get_elem_bits");
180 ac_get_type_size(LLVMTypeRef type
)
182 LLVMTypeKind kind
= LLVMGetTypeKind(type
);
185 case LLVMIntegerTypeKind
:
186 return LLVMGetIntTypeWidth(type
) / 8;
187 case LLVMHalfTypeKind
:
189 case LLVMFloatTypeKind
:
191 case LLVMDoubleTypeKind
:
193 case LLVMPointerTypeKind
:
194 if (LLVMGetPointerAddressSpace(type
) == AC_ADDR_SPACE_CONST_32BIT
)
197 case LLVMVectorTypeKind
:
198 return LLVMGetVectorSize(type
) *
199 ac_get_type_size(LLVMGetElementType(type
));
200 case LLVMArrayTypeKind
:
201 return LLVMGetArrayLength(type
) *
202 ac_get_type_size(LLVMGetElementType(type
));
209 static LLVMTypeRef
to_integer_type_scalar(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
213 else if (t
== ctx
->f16
|| t
== ctx
->i16
)
215 else if (t
== ctx
->f32
|| t
== ctx
->i32
)
217 else if (t
== ctx
->f64
|| t
== ctx
->i64
)
220 unreachable("Unhandled integer size");
224 ac_to_integer_type(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
226 if (LLVMGetTypeKind(t
) == LLVMVectorTypeKind
) {
227 LLVMTypeRef elem_type
= LLVMGetElementType(t
);
228 return LLVMVectorType(to_integer_type_scalar(ctx
, elem_type
),
229 LLVMGetVectorSize(t
));
231 if (LLVMGetTypeKind(t
) == LLVMPointerTypeKind
) {
232 switch (LLVMGetPointerAddressSpace(t
)) {
233 case AC_ADDR_SPACE_GLOBAL
:
235 case AC_ADDR_SPACE_LDS
:
238 unreachable("unhandled address space");
241 return to_integer_type_scalar(ctx
, t
);
245 ac_to_integer(struct ac_llvm_context
*ctx
, LLVMValueRef v
)
247 LLVMTypeRef type
= LLVMTypeOf(v
);
248 if (LLVMGetTypeKind(type
) == LLVMPointerTypeKind
) {
249 return LLVMBuildPtrToInt(ctx
->builder
, v
, ac_to_integer_type(ctx
, type
), "");
251 return LLVMBuildBitCast(ctx
->builder
, v
, ac_to_integer_type(ctx
, type
), "");
255 ac_to_integer_or_pointer(struct ac_llvm_context
*ctx
, LLVMValueRef v
)
257 LLVMTypeRef type
= LLVMTypeOf(v
);
258 if (LLVMGetTypeKind(type
) == LLVMPointerTypeKind
)
260 return ac_to_integer(ctx
, v
);
263 static LLVMTypeRef
to_float_type_scalar(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
267 else if (t
== ctx
->i16
|| t
== ctx
->f16
)
269 else if (t
== ctx
->i32
|| t
== ctx
->f32
)
271 else if (t
== ctx
->i64
|| t
== ctx
->f64
)
274 unreachable("Unhandled float size");
278 ac_to_float_type(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
280 if (LLVMGetTypeKind(t
) == LLVMVectorTypeKind
) {
281 LLVMTypeRef elem_type
= LLVMGetElementType(t
);
282 return LLVMVectorType(to_float_type_scalar(ctx
, elem_type
),
283 LLVMGetVectorSize(t
));
285 return to_float_type_scalar(ctx
, t
);
289 ac_to_float(struct ac_llvm_context
*ctx
, LLVMValueRef v
)
291 LLVMTypeRef type
= LLVMTypeOf(v
);
292 return LLVMBuildBitCast(ctx
->builder
, v
, ac_to_float_type(ctx
, type
), "");
297 ac_build_intrinsic(struct ac_llvm_context
*ctx
, const char *name
,
298 LLVMTypeRef return_type
, LLVMValueRef
*params
,
299 unsigned param_count
, unsigned attrib_mask
)
301 LLVMValueRef function
, call
;
302 bool set_callsite_attrs
= !(attrib_mask
& AC_FUNC_ATTR_LEGACY
);
304 function
= LLVMGetNamedFunction(ctx
->module
, name
);
306 LLVMTypeRef param_types
[32], function_type
;
309 assert(param_count
<= 32);
311 for (i
= 0; i
< param_count
; ++i
) {
313 param_types
[i
] = LLVMTypeOf(params
[i
]);
316 LLVMFunctionType(return_type
, param_types
, param_count
, 0);
317 function
= LLVMAddFunction(ctx
->module
, name
, function_type
);
319 LLVMSetFunctionCallConv(function
, LLVMCCallConv
);
320 LLVMSetLinkage(function
, LLVMExternalLinkage
);
322 if (!set_callsite_attrs
)
323 ac_add_func_attributes(ctx
->context
, function
, attrib_mask
);
326 call
= LLVMBuildCall(ctx
->builder
, function
, params
, param_count
, "");
327 if (set_callsite_attrs
)
328 ac_add_func_attributes(ctx
->context
, call
, attrib_mask
);
333 * Given the i32 or vNi32 \p type, generate the textual name (e.g. for use with
336 void ac_build_type_name_for_intr(LLVMTypeRef type
, char *buf
, unsigned bufsize
)
338 LLVMTypeRef elem_type
= type
;
340 assert(bufsize
>= 8);
342 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
) {
343 int ret
= snprintf(buf
, bufsize
, "v%u",
344 LLVMGetVectorSize(type
));
346 char *type_name
= LLVMPrintTypeToString(type
);
347 fprintf(stderr
, "Error building type name for: %s\n",
351 elem_type
= LLVMGetElementType(type
);
355 switch (LLVMGetTypeKind(elem_type
)) {
357 case LLVMIntegerTypeKind
:
358 snprintf(buf
, bufsize
, "i%d", LLVMGetIntTypeWidth(elem_type
));
360 case LLVMHalfTypeKind
:
361 snprintf(buf
, bufsize
, "f16");
363 case LLVMFloatTypeKind
:
364 snprintf(buf
, bufsize
, "f32");
366 case LLVMDoubleTypeKind
:
367 snprintf(buf
, bufsize
, "f64");
373 * Helper function that builds an LLVM IR PHI node and immediately adds
377 ac_build_phi(struct ac_llvm_context
*ctx
, LLVMTypeRef type
,
378 unsigned count_incoming
, LLVMValueRef
*values
,
379 LLVMBasicBlockRef
*blocks
)
381 LLVMValueRef phi
= LLVMBuildPhi(ctx
->builder
, type
, "");
382 LLVMAddIncoming(phi
, values
, blocks
, count_incoming
);
386 void ac_build_s_barrier(struct ac_llvm_context
*ctx
)
388 ac_build_intrinsic(ctx
, "llvm.amdgcn.s.barrier", ctx
->voidt
, NULL
,
389 0, AC_FUNC_ATTR_CONVERGENT
);
392 /* Prevent optimizations (at least of memory accesses) across the current
393 * point in the program by emitting empty inline assembly that is marked as
394 * having side effects.
396 * Optionally, a value can be passed through the inline assembly to prevent
397 * LLVM from hoisting calls to ReadNone functions.
400 ac_build_optimization_barrier(struct ac_llvm_context
*ctx
,
403 static int counter
= 0;
405 LLVMBuilderRef builder
= ctx
->builder
;
408 snprintf(code
, sizeof(code
), "; %d", p_atomic_inc_return(&counter
));
411 LLVMTypeRef ftype
= LLVMFunctionType(ctx
->voidt
, NULL
, 0, false);
412 LLVMValueRef inlineasm
= LLVMConstInlineAsm(ftype
, code
, "", true, false);
413 LLVMBuildCall(builder
, inlineasm
, NULL
, 0, "");
415 LLVMTypeRef ftype
= LLVMFunctionType(ctx
->i32
, &ctx
->i32
, 1, false);
416 LLVMValueRef inlineasm
= LLVMConstInlineAsm(ftype
, code
, "=v,0", true, false);
417 LLVMValueRef vgpr
= *pvgpr
;
418 LLVMTypeRef vgpr_type
= LLVMTypeOf(vgpr
);
419 unsigned vgpr_size
= ac_get_type_size(vgpr_type
);
422 assert(vgpr_size
% 4 == 0);
424 vgpr
= LLVMBuildBitCast(builder
, vgpr
, LLVMVectorType(ctx
->i32
, vgpr_size
/ 4), "");
425 vgpr0
= LLVMBuildExtractElement(builder
, vgpr
, ctx
->i32_0
, "");
426 vgpr0
= LLVMBuildCall(builder
, inlineasm
, &vgpr0
, 1, "");
427 vgpr
= LLVMBuildInsertElement(builder
, vgpr
, vgpr0
, ctx
->i32_0
, "");
428 vgpr
= LLVMBuildBitCast(builder
, vgpr
, vgpr_type
, "");
435 ac_build_shader_clock(struct ac_llvm_context
*ctx
)
437 const char *intr
= HAVE_LLVM
>= 0x0900 && ctx
->chip_class
>= GFX8
?
438 "llvm.amdgcn.s.memrealtime" : "llvm.readcyclecounter";
439 LLVMValueRef tmp
= ac_build_intrinsic(ctx
, intr
, ctx
->i64
, NULL
, 0, 0);
440 return LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->v2i32
, "");
444 ac_build_ballot(struct ac_llvm_context
*ctx
,
447 const char *name
= HAVE_LLVM
>= 0x900 ? "llvm.amdgcn.icmp.i64.i32" : "llvm.amdgcn.icmp.i32";
448 LLVMValueRef args
[3] = {
451 LLVMConstInt(ctx
->i32
, LLVMIntNE
, 0)
454 /* We currently have no other way to prevent LLVM from lifting the icmp
455 * calls to a dominating basic block.
457 ac_build_optimization_barrier(ctx
, &args
[0]);
459 args
[0] = ac_to_integer(ctx
, args
[0]);
461 return ac_build_intrinsic(ctx
, name
,
463 AC_FUNC_ATTR_NOUNWIND
|
464 AC_FUNC_ATTR_READNONE
|
465 AC_FUNC_ATTR_CONVERGENT
);
468 LLVMValueRef
ac_get_i1_sgpr_mask(struct ac_llvm_context
*ctx
,
471 const char *name
= HAVE_LLVM
>= 0x900 ? "llvm.amdgcn.icmp.i64.i1" : "llvm.amdgcn.icmp.i1";
472 LLVMValueRef args
[3] = {
475 LLVMConstInt(ctx
->i32
, LLVMIntNE
, 0),
478 assert(HAVE_LLVM
>= 0x0800);
479 return ac_build_intrinsic(ctx
, name
, ctx
->i64
, args
, 3,
480 AC_FUNC_ATTR_NOUNWIND
|
481 AC_FUNC_ATTR_READNONE
|
482 AC_FUNC_ATTR_CONVERGENT
);
486 ac_build_vote_all(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
488 LLVMValueRef active_set
= ac_build_ballot(ctx
, ctx
->i32_1
);
489 LLVMValueRef vote_set
= ac_build_ballot(ctx
, value
);
490 return LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, vote_set
, active_set
, "");
494 ac_build_vote_any(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
496 LLVMValueRef vote_set
= ac_build_ballot(ctx
, value
);
497 return LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, vote_set
,
498 LLVMConstInt(ctx
->i64
, 0, 0), "");
502 ac_build_vote_eq(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
504 LLVMValueRef active_set
= ac_build_ballot(ctx
, ctx
->i32_1
);
505 LLVMValueRef vote_set
= ac_build_ballot(ctx
, value
);
507 LLVMValueRef all
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
508 vote_set
, active_set
, "");
509 LLVMValueRef none
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
511 LLVMConstInt(ctx
->i64
, 0, 0), "");
512 return LLVMBuildOr(ctx
->builder
, all
, none
, "");
516 ac_build_varying_gather_values(struct ac_llvm_context
*ctx
, LLVMValueRef
*values
,
517 unsigned value_count
, unsigned component
)
519 LLVMValueRef vec
= NULL
;
521 if (value_count
== 1) {
522 return values
[component
];
523 } else if (!value_count
)
524 unreachable("value_count is 0");
526 for (unsigned i
= component
; i
< value_count
+ component
; i
++) {
527 LLVMValueRef value
= values
[i
];
530 vec
= LLVMGetUndef( LLVMVectorType(LLVMTypeOf(value
), value_count
));
531 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, i
- component
, false);
532 vec
= LLVMBuildInsertElement(ctx
->builder
, vec
, value
, index
, "");
538 ac_build_gather_values_extended(struct ac_llvm_context
*ctx
,
539 LLVMValueRef
*values
,
540 unsigned value_count
,
541 unsigned value_stride
,
545 LLVMBuilderRef builder
= ctx
->builder
;
546 LLVMValueRef vec
= NULL
;
549 if (value_count
== 1 && !always_vector
) {
551 return LLVMBuildLoad(builder
, values
[0], "");
553 } else if (!value_count
)
554 unreachable("value_count is 0");
556 for (i
= 0; i
< value_count
; i
++) {
557 LLVMValueRef value
= values
[i
* value_stride
];
559 value
= LLVMBuildLoad(builder
, value
, "");
562 vec
= LLVMGetUndef( LLVMVectorType(LLVMTypeOf(value
), value_count
));
563 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, i
, false);
564 vec
= LLVMBuildInsertElement(builder
, vec
, value
, index
, "");
570 ac_build_gather_values(struct ac_llvm_context
*ctx
,
571 LLVMValueRef
*values
,
572 unsigned value_count
)
574 return ac_build_gather_values_extended(ctx
, values
, value_count
, 1, false, false);
577 /* Expand a scalar or vector to <dst_channels x type> by filling the remaining
578 * channels with undef. Extract at most src_channels components from the input.
581 ac_build_expand(struct ac_llvm_context
*ctx
,
583 unsigned src_channels
,
584 unsigned dst_channels
)
586 LLVMTypeRef elemtype
;
587 LLVMValueRef chan
[dst_channels
];
589 if (LLVMGetTypeKind(LLVMTypeOf(value
)) == LLVMVectorTypeKind
) {
590 unsigned vec_size
= LLVMGetVectorSize(LLVMTypeOf(value
));
592 if (src_channels
== dst_channels
&& vec_size
== dst_channels
)
595 src_channels
= MIN2(src_channels
, vec_size
);
597 for (unsigned i
= 0; i
< src_channels
; i
++)
598 chan
[i
] = ac_llvm_extract_elem(ctx
, value
, i
);
600 elemtype
= LLVMGetElementType(LLVMTypeOf(value
));
603 assert(src_channels
== 1);
606 elemtype
= LLVMTypeOf(value
);
609 for (unsigned i
= src_channels
; i
< dst_channels
; i
++)
610 chan
[i
] = LLVMGetUndef(elemtype
);
612 return ac_build_gather_values(ctx
, chan
, dst_channels
);
615 /* Expand a scalar or vector to <4 x type> by filling the remaining channels
616 * with undef. Extract at most num_channels components from the input.
618 LLVMValueRef
ac_build_expand_to_vec4(struct ac_llvm_context
*ctx
,
620 unsigned num_channels
)
622 return ac_build_expand(ctx
, value
, num_channels
, 4);
625 LLVMValueRef
ac_build_round(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
627 unsigned type_size
= ac_get_type_size(LLVMTypeOf(value
));
631 name
= "llvm.rint.f16";
632 else if (type_size
== 4)
633 name
= "llvm.rint.f32";
635 name
= "llvm.rint.f64";
637 return ac_build_intrinsic(ctx
, name
, LLVMTypeOf(value
), &value
, 1,
638 AC_FUNC_ATTR_READNONE
);
642 ac_build_fdiv(struct ac_llvm_context
*ctx
,
646 /* If we do (num / den), LLVM >= 7.0 does:
647 * return num * v_rcp_f32(den * (fabs(den) > 0x1.0p+96f ? 0x1.0p-32f : 1.0f));
649 * If we do (num * (1 / den)), LLVM does:
650 * return num * v_rcp_f32(den);
652 LLVMValueRef one
= LLVMConstReal(LLVMTypeOf(num
), 1.0);
653 LLVMValueRef rcp
= LLVMBuildFDiv(ctx
->builder
, one
, den
, "");
654 LLVMValueRef ret
= LLVMBuildFMul(ctx
->builder
, num
, rcp
, "");
656 /* Use v_rcp_f32 instead of precise division. */
657 if (!LLVMIsConstant(ret
))
658 LLVMSetMetadata(ret
, ctx
->fpmath_md_kind
, ctx
->fpmath_md_2p5_ulp
);
662 /* See fast_idiv_by_const.h. */
663 /* Set: increment = util_fast_udiv_info::increment ? multiplier : 0; */
664 LLVMValueRef
ac_build_fast_udiv(struct ac_llvm_context
*ctx
,
666 LLVMValueRef multiplier
,
667 LLVMValueRef pre_shift
,
668 LLVMValueRef post_shift
,
669 LLVMValueRef increment
)
671 LLVMBuilderRef builder
= ctx
->builder
;
673 num
= LLVMBuildLShr(builder
, num
, pre_shift
, "");
674 num
= LLVMBuildMul(builder
,
675 LLVMBuildZExt(builder
, num
, ctx
->i64
, ""),
676 LLVMBuildZExt(builder
, multiplier
, ctx
->i64
, ""), "");
677 num
= LLVMBuildAdd(builder
, num
,
678 LLVMBuildZExt(builder
, increment
, ctx
->i64
, ""), "");
679 num
= LLVMBuildLShr(builder
, num
, LLVMConstInt(ctx
->i64
, 32, 0), "");
680 num
= LLVMBuildTrunc(builder
, num
, ctx
->i32
, "");
681 return LLVMBuildLShr(builder
, num
, post_shift
, "");
684 /* See fast_idiv_by_const.h. */
685 /* If num != UINT_MAX, this more efficient version can be used. */
686 /* Set: increment = util_fast_udiv_info::increment; */
687 LLVMValueRef
ac_build_fast_udiv_nuw(struct ac_llvm_context
*ctx
,
689 LLVMValueRef multiplier
,
690 LLVMValueRef pre_shift
,
691 LLVMValueRef post_shift
,
692 LLVMValueRef increment
)
694 LLVMBuilderRef builder
= ctx
->builder
;
696 num
= LLVMBuildLShr(builder
, num
, pre_shift
, "");
697 num
= LLVMBuildNUWAdd(builder
, num
, increment
, "");
698 num
= LLVMBuildMul(builder
,
699 LLVMBuildZExt(builder
, num
, ctx
->i64
, ""),
700 LLVMBuildZExt(builder
, multiplier
, ctx
->i64
, ""), "");
701 num
= LLVMBuildLShr(builder
, num
, LLVMConstInt(ctx
->i64
, 32, 0), "");
702 num
= LLVMBuildTrunc(builder
, num
, ctx
->i32
, "");
703 return LLVMBuildLShr(builder
, num
, post_shift
, "");
706 /* See fast_idiv_by_const.h. */
707 /* Both operands must fit in 31 bits and the divisor must not be 1. */
708 LLVMValueRef
ac_build_fast_udiv_u31_d_not_one(struct ac_llvm_context
*ctx
,
710 LLVMValueRef multiplier
,
711 LLVMValueRef post_shift
)
713 LLVMBuilderRef builder
= ctx
->builder
;
715 num
= LLVMBuildMul(builder
,
716 LLVMBuildZExt(builder
, num
, ctx
->i64
, ""),
717 LLVMBuildZExt(builder
, multiplier
, ctx
->i64
, ""), "");
718 num
= LLVMBuildLShr(builder
, num
, LLVMConstInt(ctx
->i64
, 32, 0), "");
719 num
= LLVMBuildTrunc(builder
, num
, ctx
->i32
, "");
720 return LLVMBuildLShr(builder
, num
, post_shift
, "");
723 /* Coordinates for cube map selection. sc, tc, and ma are as in Table 8.27
724 * of the OpenGL 4.5 (Compatibility Profile) specification, except ma is
725 * already multiplied by two. id is the cube face number.
727 struct cube_selection_coords
{
734 build_cube_intrinsic(struct ac_llvm_context
*ctx
,
736 struct cube_selection_coords
*out
)
738 LLVMTypeRef f32
= ctx
->f32
;
740 out
->stc
[1] = ac_build_intrinsic(ctx
, "llvm.amdgcn.cubetc",
741 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
742 out
->stc
[0] = ac_build_intrinsic(ctx
, "llvm.amdgcn.cubesc",
743 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
744 out
->ma
= ac_build_intrinsic(ctx
, "llvm.amdgcn.cubema",
745 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
746 out
->id
= ac_build_intrinsic(ctx
, "llvm.amdgcn.cubeid",
747 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
751 * Build a manual selection sequence for cube face sc/tc coordinates and
752 * major axis vector (multiplied by 2 for consistency) for the given
753 * vec3 \p coords, for the face implied by \p selcoords.
755 * For the major axis, we always adjust the sign to be in the direction of
756 * selcoords.ma; i.e., a positive out_ma means that coords is pointed towards
757 * the selcoords major axis.
759 static void build_cube_select(struct ac_llvm_context
*ctx
,
760 const struct cube_selection_coords
*selcoords
,
761 const LLVMValueRef
*coords
,
762 LLVMValueRef
*out_st
,
763 LLVMValueRef
*out_ma
)
765 LLVMBuilderRef builder
= ctx
->builder
;
766 LLVMTypeRef f32
= LLVMTypeOf(coords
[0]);
767 LLVMValueRef is_ma_positive
;
769 LLVMValueRef is_ma_z
, is_not_ma_z
;
770 LLVMValueRef is_ma_y
;
771 LLVMValueRef is_ma_x
;
775 is_ma_positive
= LLVMBuildFCmp(builder
, LLVMRealUGE
,
776 selcoords
->ma
, LLVMConstReal(f32
, 0.0), "");
777 sgn_ma
= LLVMBuildSelect(builder
, is_ma_positive
,
778 LLVMConstReal(f32
, 1.0), LLVMConstReal(f32
, -1.0), "");
780 is_ma_z
= LLVMBuildFCmp(builder
, LLVMRealUGE
, selcoords
->id
, LLVMConstReal(f32
, 4.0), "");
781 is_not_ma_z
= LLVMBuildNot(builder
, is_ma_z
, "");
782 is_ma_y
= LLVMBuildAnd(builder
, is_not_ma_z
,
783 LLVMBuildFCmp(builder
, LLVMRealUGE
, selcoords
->id
, LLVMConstReal(f32
, 2.0), ""), "");
784 is_ma_x
= LLVMBuildAnd(builder
, is_not_ma_z
, LLVMBuildNot(builder
, is_ma_y
, ""), "");
787 tmp
= LLVMBuildSelect(builder
, is_ma_x
, coords
[2], coords
[0], "");
788 sgn
= LLVMBuildSelect(builder
, is_ma_y
, LLVMConstReal(f32
, 1.0),
789 LLVMBuildSelect(builder
, is_ma_z
, sgn_ma
,
790 LLVMBuildFNeg(builder
, sgn_ma
, ""), ""), "");
791 out_st
[0] = LLVMBuildFMul(builder
, tmp
, sgn
, "");
794 tmp
= LLVMBuildSelect(builder
, is_ma_y
, coords
[2], coords
[1], "");
795 sgn
= LLVMBuildSelect(builder
, is_ma_y
, sgn_ma
,
796 LLVMConstReal(f32
, -1.0), "");
797 out_st
[1] = LLVMBuildFMul(builder
, tmp
, sgn
, "");
800 tmp
= LLVMBuildSelect(builder
, is_ma_z
, coords
[2],
801 LLVMBuildSelect(builder
, is_ma_y
, coords
[1], coords
[0], ""), "");
802 tmp
= ac_build_intrinsic(ctx
, "llvm.fabs.f32",
803 ctx
->f32
, &tmp
, 1, AC_FUNC_ATTR_READNONE
);
804 *out_ma
= LLVMBuildFMul(builder
, tmp
, LLVMConstReal(f32
, 2.0), "");
808 ac_prepare_cube_coords(struct ac_llvm_context
*ctx
,
809 bool is_deriv
, bool is_array
, bool is_lod
,
810 LLVMValueRef
*coords_arg
,
811 LLVMValueRef
*derivs_arg
)
814 LLVMBuilderRef builder
= ctx
->builder
;
815 struct cube_selection_coords selcoords
;
816 LLVMValueRef coords
[3];
819 if (is_array
&& !is_lod
) {
820 LLVMValueRef tmp
= ac_build_round(ctx
, coords_arg
[3]);
822 /* Section 8.9 (Texture Functions) of the GLSL 4.50 spec says:
824 * "For Array forms, the array layer used will be
826 * max(0, min(d−1, floor(layer+0.5)))
828 * where d is the depth of the texture array and layer
829 * comes from the component indicated in the tables below.
830 * Workaroudn for an issue where the layer is taken from a
831 * helper invocation which happens to fall on a different
832 * layer due to extrapolation."
834 * GFX8 and earlier attempt to implement this in hardware by
835 * clamping the value of coords[2] = (8 * layer) + face.
836 * Unfortunately, this means that the we end up with the wrong
837 * face when clamping occurs.
839 * Clamp the layer earlier to work around the issue.
841 if (ctx
->chip_class
<= GFX8
) {
843 ge0
= LLVMBuildFCmp(builder
, LLVMRealOGE
, tmp
, ctx
->f32_0
, "");
844 tmp
= LLVMBuildSelect(builder
, ge0
, tmp
, ctx
->f32_0
, "");
850 build_cube_intrinsic(ctx
, coords_arg
, &selcoords
);
852 invma
= ac_build_intrinsic(ctx
, "llvm.fabs.f32",
853 ctx
->f32
, &selcoords
.ma
, 1, AC_FUNC_ATTR_READNONE
);
854 invma
= ac_build_fdiv(ctx
, LLVMConstReal(ctx
->f32
, 1.0), invma
);
856 for (int i
= 0; i
< 2; ++i
)
857 coords
[i
] = LLVMBuildFMul(builder
, selcoords
.stc
[i
], invma
, "");
859 coords
[2] = selcoords
.id
;
861 if (is_deriv
&& derivs_arg
) {
862 LLVMValueRef derivs
[4];
865 /* Convert cube derivatives to 2D derivatives. */
866 for (axis
= 0; axis
< 2; axis
++) {
867 LLVMValueRef deriv_st
[2];
868 LLVMValueRef deriv_ma
;
870 /* Transform the derivative alongside the texture
871 * coordinate. Mathematically, the correct formula is
872 * as follows. Assume we're projecting onto the +Z face
873 * and denote by dx/dh the derivative of the (original)
874 * X texture coordinate with respect to horizontal
875 * window coordinates. The projection onto the +Z face
880 * Then df/dh = df/dx * dx/dh + df/dz * dz/dh
881 * = 1/z * dx/dh - x/z * 1/z * dz/dh.
883 * This motivatives the implementation below.
885 * Whether this actually gives the expected results for
886 * apps that might feed in derivatives obtained via
887 * finite differences is anyone's guess. The OpenGL spec
888 * seems awfully quiet about how textureGrad for cube
889 * maps should be handled.
891 build_cube_select(ctx
, &selcoords
, &derivs_arg
[axis
* 3],
892 deriv_st
, &deriv_ma
);
894 deriv_ma
= LLVMBuildFMul(builder
, deriv_ma
, invma
, "");
896 for (int i
= 0; i
< 2; ++i
)
897 derivs
[axis
* 2 + i
] =
898 LLVMBuildFSub(builder
,
899 LLVMBuildFMul(builder
, deriv_st
[i
], invma
, ""),
900 LLVMBuildFMul(builder
, deriv_ma
, coords
[i
], ""), "");
903 memcpy(derivs_arg
, derivs
, sizeof(derivs
));
906 /* Shift the texture coordinate. This must be applied after the
907 * derivative calculation.
909 for (int i
= 0; i
< 2; ++i
)
910 coords
[i
] = LLVMBuildFAdd(builder
, coords
[i
], LLVMConstReal(ctx
->f32
, 1.5), "");
913 /* for cube arrays coord.z = coord.w(array_index) * 8 + face */
914 /* coords_arg.w component - array_index for cube arrays */
915 coords
[2] = ac_build_fmad(ctx
, coords_arg
[3], LLVMConstReal(ctx
->f32
, 8.0), coords
[2]);
918 memcpy(coords_arg
, coords
, sizeof(coords
));
923 ac_build_fs_interp(struct ac_llvm_context
*ctx
,
924 LLVMValueRef llvm_chan
,
925 LLVMValueRef attr_number
,
930 LLVMValueRef args
[5];
935 args
[2] = attr_number
;
938 p1
= ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p1",
939 ctx
->f32
, args
, 4, AC_FUNC_ATTR_READNONE
);
944 args
[3] = attr_number
;
947 return ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p2",
948 ctx
->f32
, args
, 5, AC_FUNC_ATTR_READNONE
);
952 ac_build_fs_interp_f16(struct ac_llvm_context
*ctx
,
953 LLVMValueRef llvm_chan
,
954 LLVMValueRef attr_number
,
959 LLVMValueRef args
[6];
964 args
[2] = attr_number
;
965 args
[3] = ctx
->i1false
;
968 p1
= ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p1.f16",
969 ctx
->f32
, args
, 5, AC_FUNC_ATTR_READNONE
);
974 args
[3] = attr_number
;
975 args
[4] = ctx
->i1false
;
978 return ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p2.f16",
979 ctx
->f16
, args
, 6, AC_FUNC_ATTR_READNONE
);
983 ac_build_fs_interp_mov(struct ac_llvm_context
*ctx
,
984 LLVMValueRef parameter
,
985 LLVMValueRef llvm_chan
,
986 LLVMValueRef attr_number
,
989 LLVMValueRef args
[4];
993 args
[2] = attr_number
;
996 return ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.mov",
997 ctx
->f32
, args
, 4, AC_FUNC_ATTR_READNONE
);
1001 ac_build_gep_ptr(struct ac_llvm_context
*ctx
,
1002 LLVMValueRef base_ptr
,
1005 return LLVMBuildGEP(ctx
->builder
, base_ptr
, &index
, 1, "");
1009 ac_build_gep0(struct ac_llvm_context
*ctx
,
1010 LLVMValueRef base_ptr
,
1013 LLVMValueRef indices
[2] = {
1017 return LLVMBuildGEP(ctx
->builder
, base_ptr
, indices
, 2, "");
1020 LLVMValueRef
ac_build_pointer_add(struct ac_llvm_context
*ctx
, LLVMValueRef ptr
,
1023 return LLVMBuildPointerCast(ctx
->builder
,
1024 LLVMBuildGEP(ctx
->builder
, ptr
, &index
, 1, ""),
1025 LLVMTypeOf(ptr
), "");
1029 ac_build_indexed_store(struct ac_llvm_context
*ctx
,
1030 LLVMValueRef base_ptr
, LLVMValueRef index
,
1033 LLVMBuildStore(ctx
->builder
, value
,
1034 ac_build_gep0(ctx
, base_ptr
, index
));
1038 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad.
1039 * It's equivalent to doing a load from &base_ptr[index].
1041 * \param base_ptr Where the array starts.
1042 * \param index The element index into the array.
1043 * \param uniform Whether the base_ptr and index can be assumed to be
1044 * dynamically uniform (i.e. load to an SGPR)
1045 * \param invariant Whether the load is invariant (no other opcodes affect it)
1046 * \param no_unsigned_wraparound
1047 * For all possible re-associations and re-distributions of an expression
1048 * "base_ptr + index * elemsize" into "addr + offset" (excluding GEPs
1049 * without inbounds in base_ptr), this parameter is true if "addr + offset"
1050 * does not result in an unsigned integer wraparound. This is used for
1051 * optimal code generation of 32-bit pointer arithmetic.
1053 * For example, a 32-bit immediate offset that causes a 32-bit unsigned
1054 * integer wraparound can't be an imm offset in s_load_dword, because
1055 * the instruction performs "addr + offset" in 64 bits.
1057 * Expected usage for bindless textures by chaining GEPs:
1058 * // possible unsigned wraparound, don't use InBounds:
1059 * ptr1 = LLVMBuildGEP(base_ptr, index);
1060 * image = load(ptr1); // becomes "s_load ptr1, 0"
1062 * ptr2 = LLVMBuildInBoundsGEP(ptr1, 32 / elemsize);
1063 * sampler = load(ptr2); // becomes "s_load ptr1, 32" thanks to InBounds
1066 ac_build_load_custom(struct ac_llvm_context
*ctx
, LLVMValueRef base_ptr
,
1067 LLVMValueRef index
, bool uniform
, bool invariant
,
1068 bool no_unsigned_wraparound
)
1070 LLVMValueRef pointer
, result
;
1072 if (no_unsigned_wraparound
&&
1073 LLVMGetPointerAddressSpace(LLVMTypeOf(base_ptr
)) == AC_ADDR_SPACE_CONST_32BIT
)
1074 pointer
= LLVMBuildInBoundsGEP(ctx
->builder
, base_ptr
, &index
, 1, "");
1076 pointer
= LLVMBuildGEP(ctx
->builder
, base_ptr
, &index
, 1, "");
1079 LLVMSetMetadata(pointer
, ctx
->uniform_md_kind
, ctx
->empty_md
);
1080 result
= LLVMBuildLoad(ctx
->builder
, pointer
, "");
1082 LLVMSetMetadata(result
, ctx
->invariant_load_md_kind
, ctx
->empty_md
);
1086 LLVMValueRef
ac_build_load(struct ac_llvm_context
*ctx
, LLVMValueRef base_ptr
,
1089 return ac_build_load_custom(ctx
, base_ptr
, index
, false, false, false);
1092 LLVMValueRef
ac_build_load_invariant(struct ac_llvm_context
*ctx
,
1093 LLVMValueRef base_ptr
, LLVMValueRef index
)
1095 return ac_build_load_custom(ctx
, base_ptr
, index
, false, true, false);
1098 /* This assumes that there is no unsigned integer wraparound during the address
1099 * computation, excluding all GEPs within base_ptr. */
1100 LLVMValueRef
ac_build_load_to_sgpr(struct ac_llvm_context
*ctx
,
1101 LLVMValueRef base_ptr
, LLVMValueRef index
)
1103 return ac_build_load_custom(ctx
, base_ptr
, index
, true, true, true);
1106 /* See ac_build_load_custom() documentation. */
1107 LLVMValueRef
ac_build_load_to_sgpr_uint_wraparound(struct ac_llvm_context
*ctx
,
1108 LLVMValueRef base_ptr
, LLVMValueRef index
)
1110 return ac_build_load_custom(ctx
, base_ptr
, index
, true, true, false);
1113 static unsigned get_load_cache_policy(struct ac_llvm_context
*ctx
,
1114 unsigned cache_policy
)
1116 return cache_policy
|
1117 (ctx
->chip_class
>= GFX10
&& cache_policy
& ac_glc
? ac_dlc
: 0);
1121 ac_build_llvm7_buffer_store_common(struct ac_llvm_context
*ctx
,
1124 LLVMValueRef vindex
,
1125 LLVMValueRef voffset
,
1126 unsigned num_channels
,
1127 unsigned cache_policy
,
1130 LLVMValueRef args
[] = {
1132 LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, ""),
1133 vindex
? vindex
: ctx
->i32_0
,
1135 LLVMConstInt(ctx
->i1
, !!(cache_policy
& ac_glc
), 0),
1136 LLVMConstInt(ctx
->i1
, !!(cache_policy
& ac_slc
), 0)
1138 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
1140 const char *type_names
[] = {"f32", "v2f32", "v4f32"};
1144 snprintf(name
, sizeof(name
), "llvm.amdgcn.buffer.store.format.%s",
1147 snprintf(name
, sizeof(name
), "llvm.amdgcn.buffer.store.%s",
1151 ac_build_intrinsic(ctx
, name
, ctx
->voidt
, args
, ARRAY_SIZE(args
),
1152 AC_FUNC_ATTR_INACCESSIBLE_MEM_ONLY
);
1156 ac_build_llvm8_buffer_store_common(struct ac_llvm_context
*ctx
,
1159 LLVMValueRef vindex
,
1160 LLVMValueRef voffset
,
1161 LLVMValueRef soffset
,
1162 unsigned num_channels
,
1163 LLVMTypeRef return_channel_type
,
1164 unsigned cache_policy
,
1168 LLVMValueRef args
[6];
1171 args
[idx
++] = LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, "");
1173 args
[idx
++] = vindex
? vindex
: ctx
->i32_0
;
1174 args
[idx
++] = voffset
? voffset
: ctx
->i32_0
;
1175 args
[idx
++] = soffset
? soffset
: ctx
->i32_0
;
1176 args
[idx
++] = LLVMConstInt(ctx
->i32
, cache_policy
, 0);
1177 unsigned func
= !ac_has_vec3_support(ctx
->chip_class
, use_format
) && num_channels
== 3 ? 4 : num_channels
;
1178 const char *indexing_kind
= structurized
? "struct" : "raw";
1179 char name
[256], type_name
[8];
1181 LLVMTypeRef type
= func
> 1 ? LLVMVectorType(return_channel_type
, func
) : return_channel_type
;
1182 ac_build_type_name_for_intr(type
, type_name
, sizeof(type_name
));
1185 snprintf(name
, sizeof(name
), "llvm.amdgcn.%s.buffer.store.format.%s",
1186 indexing_kind
, type_name
);
1188 snprintf(name
, sizeof(name
), "llvm.amdgcn.%s.buffer.store.%s",
1189 indexing_kind
, type_name
);
1192 ac_build_intrinsic(ctx
, name
, ctx
->voidt
, args
, idx
,
1193 AC_FUNC_ATTR_INACCESSIBLE_MEM_ONLY
);
1197 ac_build_buffer_store_format(struct ac_llvm_context
*ctx
,
1200 LLVMValueRef vindex
,
1201 LLVMValueRef voffset
,
1202 unsigned num_channels
,
1203 unsigned cache_policy
)
1205 if (HAVE_LLVM
>= 0x800) {
1206 ac_build_llvm8_buffer_store_common(ctx
, rsrc
, data
, vindex
,
1207 voffset
, NULL
, num_channels
,
1208 ctx
->f32
, cache_policy
,
1211 ac_build_llvm7_buffer_store_common(ctx
, rsrc
, data
, vindex
, voffset
,
1212 num_channels
, cache_policy
,
1217 /* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
1218 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
1219 * or v4i32 (num_channels=3,4).
1222 ac_build_buffer_store_dword(struct ac_llvm_context
*ctx
,
1225 unsigned num_channels
,
1226 LLVMValueRef voffset
,
1227 LLVMValueRef soffset
,
1228 unsigned inst_offset
,
1229 unsigned cache_policy
,
1230 bool swizzle_enable_hint
)
1232 /* Split 3 channel stores, because only LLVM 9+ support 3-channel
1234 if (num_channels
== 3 && !ac_has_vec3_support(ctx
->chip_class
, false)) {
1235 LLVMValueRef v
[3], v01
;
1237 for (int i
= 0; i
< 3; i
++) {
1238 v
[i
] = LLVMBuildExtractElement(ctx
->builder
, vdata
,
1239 LLVMConstInt(ctx
->i32
, i
, 0), "");
1241 v01
= ac_build_gather_values(ctx
, v
, 2);
1243 ac_build_buffer_store_dword(ctx
, rsrc
, v01
, 2, voffset
,
1244 soffset
, inst_offset
, cache_policy
,
1245 swizzle_enable_hint
);
1246 ac_build_buffer_store_dword(ctx
, rsrc
, v
[2], 1, voffset
,
1247 soffset
, inst_offset
+ 8,
1249 swizzle_enable_hint
);
1253 /* SWIZZLE_ENABLE requires that soffset isn't folded into voffset
1254 * (voffset is swizzled, but soffset isn't swizzled).
1255 * llvm.amdgcn.buffer.store doesn't have a separate soffset parameter.
1257 if (!swizzle_enable_hint
) {
1258 LLVMValueRef offset
= soffset
;
1261 offset
= LLVMBuildAdd(ctx
->builder
, offset
,
1262 LLVMConstInt(ctx
->i32
, inst_offset
, 0), "");
1264 if (HAVE_LLVM
>= 0x800) {
1265 ac_build_llvm8_buffer_store_common(ctx
, rsrc
,
1266 ac_to_float(ctx
, vdata
),
1275 offset
= LLVMBuildAdd(ctx
->builder
, offset
, voffset
, "");
1277 ac_build_llvm7_buffer_store_common(ctx
, rsrc
,
1278 ac_to_float(ctx
, vdata
),
1280 num_channels
, cache_policy
,
1286 static const unsigned dfmts
[] = {
1287 V_008F0C_BUF_DATA_FORMAT_32
,
1288 V_008F0C_BUF_DATA_FORMAT_32_32
,
1289 V_008F0C_BUF_DATA_FORMAT_32_32_32
,
1290 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
1292 unsigned dfmt
= dfmts
[num_channels
- 1];
1293 unsigned nfmt
= V_008F0C_BUF_NUM_FORMAT_UINT
;
1294 LLVMValueRef immoffset
= LLVMConstInt(ctx
->i32
, inst_offset
, 0);
1296 ac_build_raw_tbuffer_store(ctx
, rsrc
, vdata
, voffset
, soffset
,
1297 immoffset
, num_channels
, dfmt
, nfmt
, cache_policy
);
1301 ac_build_llvm7_buffer_load_common(struct ac_llvm_context
*ctx
,
1303 LLVMValueRef vindex
,
1304 LLVMValueRef voffset
,
1305 unsigned num_channels
,
1306 unsigned cache_policy
,
1310 LLVMValueRef args
[] = {
1311 LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, ""),
1312 vindex
? vindex
: ctx
->i32_0
,
1314 LLVMConstInt(ctx
->i1
, !!(cache_policy
& ac_glc
), 0),
1315 LLVMConstInt(ctx
->i1
, !!(cache_policy
& ac_slc
), 0)
1317 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
1319 LLVMTypeRef types
[] = {ctx
->f32
, ctx
->v2f32
, ctx
->v4f32
};
1320 const char *type_names
[] = {"f32", "v2f32", "v4f32"};
1324 snprintf(name
, sizeof(name
), "llvm.amdgcn.buffer.load.format.%s",
1327 snprintf(name
, sizeof(name
), "llvm.amdgcn.buffer.load.%s",
1331 return ac_build_intrinsic(ctx
, name
, types
[func
], args
,
1333 ac_get_load_intr_attribs(can_speculate
));
1337 ac_build_llvm8_buffer_load_common(struct ac_llvm_context
*ctx
,
1339 LLVMValueRef vindex
,
1340 LLVMValueRef voffset
,
1341 LLVMValueRef soffset
,
1342 unsigned num_channels
,
1343 LLVMTypeRef channel_type
,
1344 unsigned cache_policy
,
1349 LLVMValueRef args
[5];
1351 args
[idx
++] = LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, "");
1353 args
[idx
++] = vindex
? vindex
: ctx
->i32_0
;
1354 args
[idx
++] = voffset
? voffset
: ctx
->i32_0
;
1355 args
[idx
++] = soffset
? soffset
: ctx
->i32_0
;
1356 args
[idx
++] = LLVMConstInt(ctx
->i32
, get_load_cache_policy(ctx
, cache_policy
), 0);
1357 unsigned func
= !ac_has_vec3_support(ctx
->chip_class
, use_format
) && num_channels
== 3 ? 4 : num_channels
;
1358 const char *indexing_kind
= structurized
? "struct" : "raw";
1359 char name
[256], type_name
[8];
1361 LLVMTypeRef type
= func
> 1 ? LLVMVectorType(channel_type
, func
) : channel_type
;
1362 ac_build_type_name_for_intr(type
, type_name
, sizeof(type_name
));
1365 snprintf(name
, sizeof(name
), "llvm.amdgcn.%s.buffer.load.format.%s",
1366 indexing_kind
, type_name
);
1368 snprintf(name
, sizeof(name
), "llvm.amdgcn.%s.buffer.load.%s",
1369 indexing_kind
, type_name
);
1372 return ac_build_intrinsic(ctx
, name
, type
, args
, idx
,
1373 ac_get_load_intr_attribs(can_speculate
));
1377 ac_build_buffer_load(struct ac_llvm_context
*ctx
,
1380 LLVMValueRef vindex
,
1381 LLVMValueRef voffset
,
1382 LLVMValueRef soffset
,
1383 unsigned inst_offset
,
1384 unsigned cache_policy
,
1388 LLVMValueRef offset
= LLVMConstInt(ctx
->i32
, inst_offset
, 0);
1390 offset
= LLVMBuildAdd(ctx
->builder
, offset
, voffset
, "");
1392 offset
= LLVMBuildAdd(ctx
->builder
, offset
, soffset
, "");
1394 if (allow_smem
&& !(cache_policy
& ac_slc
) &&
1395 (!(cache_policy
& ac_glc
) || (HAVE_LLVM
>= 0x0800 && ctx
->chip_class
>= GFX8
))) {
1396 assert(vindex
== NULL
);
1398 LLVMValueRef result
[8];
1400 for (int i
= 0; i
< num_channels
; i
++) {
1402 offset
= LLVMBuildAdd(ctx
->builder
, offset
,
1403 LLVMConstInt(ctx
->i32
, 4, 0), "");
1405 const char *intrname
=
1406 HAVE_LLVM
>= 0x0800 ? "llvm.amdgcn.s.buffer.load.f32"
1407 : "llvm.SI.load.const.v4i32";
1408 unsigned num_args
= HAVE_LLVM
>= 0x0800 ? 3 : 2;
1409 LLVMValueRef args
[3] = {
1412 LLVMConstInt(ctx
->i32
, get_load_cache_policy(ctx
, cache_policy
), 0),
1414 result
[i
] = ac_build_intrinsic(ctx
, intrname
,
1415 ctx
->f32
, args
, num_args
,
1416 AC_FUNC_ATTR_READNONE
|
1417 (HAVE_LLVM
< 0x0800 ? AC_FUNC_ATTR_LEGACY
: 0));
1419 if (num_channels
== 1)
1422 if (num_channels
== 3 && !ac_has_vec3_support(ctx
->chip_class
, false))
1423 result
[num_channels
++] = LLVMGetUndef(ctx
->f32
);
1424 return ac_build_gather_values(ctx
, result
, num_channels
);
1427 if (HAVE_LLVM
>= 0x0800) {
1428 return ac_build_llvm8_buffer_load_common(ctx
, rsrc
, vindex
,
1430 num_channels
, ctx
->f32
,
1432 can_speculate
, false,
1436 return ac_build_llvm7_buffer_load_common(ctx
, rsrc
, vindex
, offset
,
1437 num_channels
, cache_policy
,
1438 can_speculate
, false);
1441 LLVMValueRef
ac_build_buffer_load_format(struct ac_llvm_context
*ctx
,
1443 LLVMValueRef vindex
,
1444 LLVMValueRef voffset
,
1445 unsigned num_channels
,
1446 unsigned cache_policy
,
1449 if (HAVE_LLVM
>= 0x800) {
1450 return ac_build_llvm8_buffer_load_common(ctx
, rsrc
, vindex
, voffset
, ctx
->i32_0
,
1451 num_channels
, ctx
->f32
,
1452 cache_policy
, can_speculate
, true, true);
1454 return ac_build_llvm7_buffer_load_common(ctx
, rsrc
, vindex
, voffset
,
1455 num_channels
, cache_policy
,
1456 can_speculate
, true);
1459 LLVMValueRef
ac_build_buffer_load_format_gfx9_safe(struct ac_llvm_context
*ctx
,
1461 LLVMValueRef vindex
,
1462 LLVMValueRef voffset
,
1463 unsigned num_channels
,
1464 unsigned cache_policy
,
1467 if (HAVE_LLVM
>= 0x800) {
1468 return ac_build_llvm8_buffer_load_common(ctx
, rsrc
, vindex
, voffset
, ctx
->i32_0
,
1469 num_channels
, ctx
->f32
,
1470 cache_policy
, can_speculate
, true, true);
1473 LLVMValueRef elem_count
= LLVMBuildExtractElement(ctx
->builder
, rsrc
, LLVMConstInt(ctx
->i32
, 2, 0), "");
1474 LLVMValueRef stride
= LLVMBuildExtractElement(ctx
->builder
, rsrc
, ctx
->i32_1
, "");
1475 stride
= LLVMBuildLShr(ctx
->builder
, stride
, LLVMConstInt(ctx
->i32
, 16, 0), "");
1477 LLVMValueRef new_elem_count
= LLVMBuildSelect(ctx
->builder
,
1478 LLVMBuildICmp(ctx
->builder
, LLVMIntUGT
, elem_count
, stride
, ""),
1479 elem_count
, stride
, "");
1481 LLVMValueRef new_rsrc
= LLVMBuildInsertElement(ctx
->builder
, rsrc
, new_elem_count
,
1482 LLVMConstInt(ctx
->i32
, 2, 0), "");
1484 return ac_build_llvm7_buffer_load_common(ctx
, new_rsrc
, vindex
, voffset
,
1485 num_channels
, cache_policy
,
1486 can_speculate
, true);
1489 /// Translate a (dfmt, nfmt) pair into a chip-appropriate combined format
1490 /// value for LLVM8+ tbuffer intrinsics.
1492 ac_get_tbuffer_format(struct ac_llvm_context
*ctx
,
1493 unsigned dfmt
, unsigned nfmt
)
1495 if (ctx
->chip_class
>= GFX10
) {
1498 default: unreachable("bad dfmt");
1499 case V_008F0C_BUF_DATA_FORMAT_8
: format
= V_008F0C_IMG_FORMAT_8_UINT
; break;
1500 case V_008F0C_BUF_DATA_FORMAT_8_8
: format
= V_008F0C_IMG_FORMAT_8_8_UINT
; break;
1501 case V_008F0C_BUF_DATA_FORMAT_8_8_8_8
: format
= V_008F0C_IMG_FORMAT_8_8_8_8_UINT
; break;
1502 case V_008F0C_BUF_DATA_FORMAT_16
: format
= V_008F0C_IMG_FORMAT_16_UINT
; break;
1503 case V_008F0C_BUF_DATA_FORMAT_16_16
: format
= V_008F0C_IMG_FORMAT_16_16_UINT
; break;
1504 case V_008F0C_BUF_DATA_FORMAT_16_16_16_16
: format
= V_008F0C_IMG_FORMAT_16_16_16_16_UINT
; break;
1505 case V_008F0C_BUF_DATA_FORMAT_32
: format
= V_008F0C_IMG_FORMAT_32_UINT
; break;
1506 case V_008F0C_BUF_DATA_FORMAT_32_32
: format
= V_008F0C_IMG_FORMAT_32_32_UINT
; break;
1507 case V_008F0C_BUF_DATA_FORMAT_32_32_32
: format
= V_008F0C_IMG_FORMAT_32_32_32_UINT
; break;
1508 case V_008F0C_BUF_DATA_FORMAT_32_32_32_32
: format
= V_008F0C_IMG_FORMAT_32_32_32_32_UINT
; break;
1509 case V_008F0C_BUF_DATA_FORMAT_2_10_10_10
: format
= V_008F0C_IMG_FORMAT_2_10_10_10_UINT
; break;
1512 // Use the regularity properties of the combined format enum.
1514 // Note: float is incompatible with 8-bit data formats,
1515 // [us]{norm,scaled} are incomparible with 32-bit data formats.
1516 // [us]scaled are not writable.
1518 case V_008F0C_BUF_NUM_FORMAT_UNORM
: format
-= 4; break;
1519 case V_008F0C_BUF_NUM_FORMAT_SNORM
: format
-= 3; break;
1520 case V_008F0C_BUF_NUM_FORMAT_USCALED
: format
-= 2; break;
1521 case V_008F0C_BUF_NUM_FORMAT_SSCALED
: format
-= 1; break;
1522 default: unreachable("bad nfmt");
1523 case V_008F0C_BUF_NUM_FORMAT_UINT
: break;
1524 case V_008F0C_BUF_NUM_FORMAT_SINT
: format
+= 1; break;
1525 case V_008F0C_BUF_NUM_FORMAT_FLOAT
: format
+= 2; break;
1530 return dfmt
| (nfmt
<< 4);
1535 ac_build_llvm8_tbuffer_load(struct ac_llvm_context
*ctx
,
1537 LLVMValueRef vindex
,
1538 LLVMValueRef voffset
,
1539 LLVMValueRef soffset
,
1540 unsigned num_channels
,
1543 unsigned cache_policy
,
1547 LLVMValueRef args
[6];
1549 args
[idx
++] = LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, "");
1551 args
[idx
++] = vindex
? vindex
: ctx
->i32_0
;
1552 args
[idx
++] = voffset
? voffset
: ctx
->i32_0
;
1553 args
[idx
++] = soffset
? soffset
: ctx
->i32_0
;
1554 args
[idx
++] = LLVMConstInt(ctx
->i32
, ac_get_tbuffer_format(ctx
, dfmt
, nfmt
), 0);
1555 args
[idx
++] = LLVMConstInt(ctx
->i32
, get_load_cache_policy(ctx
, cache_policy
), 0);
1556 unsigned func
= !ac_has_vec3_support(ctx
->chip_class
, true) && num_channels
== 3 ? 4 : num_channels
;
1557 const char *indexing_kind
= structurized
? "struct" : "raw";
1558 char name
[256], type_name
[8];
1560 LLVMTypeRef type
= func
> 1 ? LLVMVectorType(ctx
->i32
, func
) : ctx
->i32
;
1561 ac_build_type_name_for_intr(type
, type_name
, sizeof(type_name
));
1563 snprintf(name
, sizeof(name
), "llvm.amdgcn.%s.tbuffer.load.%s",
1564 indexing_kind
, type_name
);
1566 return ac_build_intrinsic(ctx
, name
, type
, args
, idx
,
1567 ac_get_load_intr_attribs(can_speculate
));
1571 ac_build_tbuffer_load(struct ac_llvm_context
*ctx
,
1573 LLVMValueRef vindex
,
1574 LLVMValueRef voffset
,
1575 LLVMValueRef soffset
,
1576 LLVMValueRef immoffset
,
1577 unsigned num_channels
,
1580 unsigned cache_policy
,
1582 bool structurized
) /* only matters for LLVM 8+ */
1584 if (HAVE_LLVM
>= 0x800) {
1585 voffset
= LLVMBuildAdd(ctx
->builder
, voffset
, immoffset
, "");
1587 return ac_build_llvm8_tbuffer_load(ctx
, rsrc
, vindex
, voffset
,
1588 soffset
, num_channels
,
1589 dfmt
, nfmt
, cache_policy
,
1590 can_speculate
, structurized
);
1593 LLVMValueRef args
[] = {
1595 vindex
? vindex
: ctx
->i32_0
,
1599 LLVMConstInt(ctx
->i32
, dfmt
, false),
1600 LLVMConstInt(ctx
->i32
, nfmt
, false),
1601 LLVMConstInt(ctx
->i1
, !!(cache_policy
& ac_glc
), false),
1602 LLVMConstInt(ctx
->i1
, !!(cache_policy
& ac_slc
), false),
1604 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
1605 LLVMTypeRef types
[] = {ctx
->i32
, ctx
->v2i32
, ctx
->v4i32
};
1606 const char *type_names
[] = {"i32", "v2i32", "v4i32"};
1609 snprintf(name
, sizeof(name
), "llvm.amdgcn.tbuffer.load.%s",
1612 return ac_build_intrinsic(ctx
, name
, types
[func
], args
, 9,
1613 ac_get_load_intr_attribs(can_speculate
));
1617 ac_build_struct_tbuffer_load(struct ac_llvm_context
*ctx
,
1619 LLVMValueRef vindex
,
1620 LLVMValueRef voffset
,
1621 LLVMValueRef soffset
,
1622 LLVMValueRef immoffset
,
1623 unsigned num_channels
,
1626 unsigned cache_policy
,
1629 return ac_build_tbuffer_load(ctx
, rsrc
, vindex
, voffset
, soffset
,
1630 immoffset
, num_channels
, dfmt
, nfmt
,
1631 cache_policy
, can_speculate
, true);
1635 ac_build_raw_tbuffer_load(struct ac_llvm_context
*ctx
,
1637 LLVMValueRef voffset
,
1638 LLVMValueRef soffset
,
1639 LLVMValueRef immoffset
,
1640 unsigned num_channels
,
1643 unsigned cache_policy
,
1646 return ac_build_tbuffer_load(ctx
, rsrc
, NULL
, voffset
, soffset
,
1647 immoffset
, num_channels
, dfmt
, nfmt
,
1648 cache_policy
, can_speculate
, false);
1652 ac_build_tbuffer_load_short(struct ac_llvm_context
*ctx
,
1654 LLVMValueRef voffset
,
1655 LLVMValueRef soffset
,
1656 LLVMValueRef immoffset
,
1657 unsigned cache_policy
)
1661 if (HAVE_LLVM
>= 0x900) {
1662 voffset
= LLVMBuildAdd(ctx
->builder
, voffset
, immoffset
, "");
1664 /* LLVM 9+ supports i8/i16 with struct/raw intrinsics. */
1665 res
= ac_build_llvm8_buffer_load_common(ctx
, rsrc
, NULL
,
1667 1, ctx
->i16
, cache_policy
,
1668 false, false, false);
1670 unsigned dfmt
= V_008F0C_BUF_DATA_FORMAT_16
;
1671 unsigned nfmt
= V_008F0C_BUF_NUM_FORMAT_UINT
;
1673 res
= ac_build_raw_tbuffer_load(ctx
, rsrc
, voffset
, soffset
,
1674 immoffset
, 1, dfmt
, nfmt
, cache_policy
,
1677 res
= LLVMBuildTrunc(ctx
->builder
, res
, ctx
->i16
, "");
1684 ac_build_tbuffer_load_byte(struct ac_llvm_context
*ctx
,
1686 LLVMValueRef voffset
,
1687 LLVMValueRef soffset
,
1688 LLVMValueRef immoffset
,
1689 unsigned cache_policy
)
1693 if (HAVE_LLVM
>= 0x900) {
1694 voffset
= LLVMBuildAdd(ctx
->builder
, voffset
, immoffset
, "");
1696 /* LLVM 9+ supports i8/i16 with struct/raw intrinsics. */
1697 res
= ac_build_llvm8_buffer_load_common(ctx
, rsrc
, NULL
,
1699 1, ctx
->i8
, cache_policy
,
1700 false, false, false);
1702 unsigned dfmt
= V_008F0C_BUF_DATA_FORMAT_8
;
1703 unsigned nfmt
= V_008F0C_BUF_NUM_FORMAT_UINT
;
1705 res
= ac_build_raw_tbuffer_load(ctx
, rsrc
, voffset
, soffset
,
1706 immoffset
, 1, dfmt
, nfmt
, cache_policy
,
1709 res
= LLVMBuildTrunc(ctx
->builder
, res
, ctx
->i8
, "");
1716 * Convert an 11- or 10-bit unsigned floating point number to an f32.
1718 * The input exponent is expected to be biased analogous to IEEE-754, i.e. by
1719 * 2^(exp_bits-1) - 1 (as defined in OpenGL and other graphics APIs).
1722 ac_ufN_to_float(struct ac_llvm_context
*ctx
, LLVMValueRef src
, unsigned exp_bits
, unsigned mant_bits
)
1724 assert(LLVMTypeOf(src
) == ctx
->i32
);
1727 LLVMValueRef mantissa
;
1728 mantissa
= LLVMBuildAnd(ctx
->builder
, src
, LLVMConstInt(ctx
->i32
, (1 << mant_bits
) - 1, false), "");
1730 /* Converting normal numbers is just a shift + correcting the exponent bias */
1731 unsigned normal_shift
= 23 - mant_bits
;
1732 unsigned bias_shift
= 127 - ((1 << (exp_bits
- 1)) - 1);
1733 LLVMValueRef shifted
, normal
;
1735 shifted
= LLVMBuildShl(ctx
->builder
, src
, LLVMConstInt(ctx
->i32
, normal_shift
, false), "");
1736 normal
= LLVMBuildAdd(ctx
->builder
, shifted
, LLVMConstInt(ctx
->i32
, bias_shift
<< 23, false), "");
1738 /* Converting nan/inf numbers is the same, but with a different exponent update */
1739 LLVMValueRef naninf
;
1740 naninf
= LLVMBuildOr(ctx
->builder
, normal
, LLVMConstInt(ctx
->i32
, 0xff << 23, false), "");
1742 /* Converting denormals is the complex case: determine the leading zeros of the
1743 * mantissa to obtain the correct shift for the mantissa and exponent correction.
1745 LLVMValueRef denormal
;
1746 LLVMValueRef params
[2] = {
1748 ctx
->i1true
, /* result can be undef when arg is 0 */
1750 LLVMValueRef ctlz
= ac_build_intrinsic(ctx
, "llvm.ctlz.i32", ctx
->i32
,
1751 params
, 2, AC_FUNC_ATTR_READNONE
);
1753 /* Shift such that the leading 1 ends up as the LSB of the exponent field. */
1754 tmp
= LLVMBuildSub(ctx
->builder
, ctlz
, LLVMConstInt(ctx
->i32
, 8, false), "");
1755 denormal
= LLVMBuildShl(ctx
->builder
, mantissa
, tmp
, "");
1757 unsigned denormal_exp
= bias_shift
+ (32 - mant_bits
) - 1;
1758 tmp
= LLVMBuildSub(ctx
->builder
, LLVMConstInt(ctx
->i32
, denormal_exp
, false), ctlz
, "");
1759 tmp
= LLVMBuildShl(ctx
->builder
, tmp
, LLVMConstInt(ctx
->i32
, 23, false), "");
1760 denormal
= LLVMBuildAdd(ctx
->builder
, denormal
, tmp
, "");
1762 /* Select the final result. */
1763 LLVMValueRef result
;
1765 tmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntUGE
, src
,
1766 LLVMConstInt(ctx
->i32
, ((1 << exp_bits
) - 1) << mant_bits
, false), "");
1767 result
= LLVMBuildSelect(ctx
->builder
, tmp
, naninf
, normal
, "");
1769 tmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntUGE
, src
,
1770 LLVMConstInt(ctx
->i32
, 1 << mant_bits
, false), "");
1771 result
= LLVMBuildSelect(ctx
->builder
, tmp
, result
, denormal
, "");
1773 tmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src
, ctx
->i32_0
, "");
1774 result
= LLVMBuildSelect(ctx
->builder
, tmp
, result
, ctx
->i32_0
, "");
1776 return ac_to_float(ctx
, result
);
1780 * Generate a fully general open coded buffer format fetch with all required
1781 * fixups suitable for vertex fetch, using non-format buffer loads.
1783 * Some combinations of argument values have special interpretations:
1784 * - size = 8 bytes, format = fixed indicates PIPE_FORMAT_R11G11B10_FLOAT
1785 * - size = 8 bytes, format != {float,fixed} indicates a 2_10_10_10 data format
1787 * \param log_size log(size of channel in bytes)
1788 * \param num_channels number of channels (1 to 4)
1789 * \param format AC_FETCH_FORMAT_xxx value
1790 * \param reverse whether XYZ channels are reversed
1791 * \param known_aligned whether the source is known to be aligned to hardware's
1792 * effective element size for loading the given format
1793 * (note: this means dword alignment for 8_8_8_8, 16_16, etc.)
1794 * \param rsrc buffer resource descriptor
1795 * \return the resulting vector of floats or integers bitcast to <4 x i32>
1798 ac_build_opencoded_load_format(struct ac_llvm_context
*ctx
,
1800 unsigned num_channels
,
1805 LLVMValueRef vindex
,
1806 LLVMValueRef voffset
,
1807 LLVMValueRef soffset
,
1808 unsigned cache_policy
,
1812 unsigned load_log_size
= log_size
;
1813 unsigned load_num_channels
= num_channels
;
1814 if (log_size
== 3) {
1816 if (format
== AC_FETCH_FORMAT_FLOAT
) {
1817 load_num_channels
= 2 * num_channels
;
1819 load_num_channels
= 1; /* 10_11_11 or 2_10_10_10 */
1823 int log_recombine
= 0;
1824 if (ctx
->chip_class
== GFX6
&& !known_aligned
) {
1825 /* Avoid alignment restrictions by loading one byte at a time. */
1826 load_num_channels
<<= load_log_size
;
1827 log_recombine
= load_log_size
;
1829 } else if (load_num_channels
== 2 || load_num_channels
== 4) {
1830 log_recombine
= -util_logbase2(load_num_channels
);
1831 load_num_channels
= 1;
1832 load_log_size
+= -log_recombine
;
1835 assert(load_log_size
>= 2 || HAVE_LLVM
>= 0x0900);
1837 LLVMValueRef loads
[32]; /* up to 32 bytes */
1838 for (unsigned i
= 0; i
< load_num_channels
; ++i
) {
1839 tmp
= LLVMBuildAdd(ctx
->builder
, soffset
,
1840 LLVMConstInt(ctx
->i32
, i
<< load_log_size
, false), "");
1841 if (HAVE_LLVM
>= 0x0800) {
1842 LLVMTypeRef channel_type
= load_log_size
== 0 ? ctx
->i8
:
1843 load_log_size
== 1 ? ctx
->i16
: ctx
->i32
;
1844 unsigned num_channels
= 1 << (MAX2(load_log_size
, 2) - 2);
1845 loads
[i
] = ac_build_llvm8_buffer_load_common(
1846 ctx
, rsrc
, vindex
, voffset
, tmp
,
1847 num_channels
, channel_type
, cache_policy
,
1848 can_speculate
, false, true);
1850 tmp
= LLVMBuildAdd(ctx
->builder
, voffset
, tmp
, "");
1851 loads
[i
] = ac_build_llvm7_buffer_load_common(
1852 ctx
, rsrc
, vindex
, tmp
,
1853 1 << (load_log_size
- 2), cache_policy
, can_speculate
, false);
1855 if (load_log_size
>= 2)
1856 loads
[i
] = ac_to_integer(ctx
, loads
[i
]);
1859 if (log_recombine
> 0) {
1860 /* Recombine bytes if necessary (GFX6 only) */
1861 LLVMTypeRef dst_type
= log_recombine
== 2 ? ctx
->i32
: ctx
->i16
;
1863 for (unsigned src
= 0, dst
= 0; src
< load_num_channels
; ++dst
) {
1864 LLVMValueRef accum
= NULL
;
1865 for (unsigned i
= 0; i
< (1 << log_recombine
); ++i
, ++src
) {
1866 tmp
= LLVMBuildZExt(ctx
->builder
, loads
[src
], dst_type
, "");
1870 tmp
= LLVMBuildShl(ctx
->builder
, tmp
,
1871 LLVMConstInt(dst_type
, 8 * i
, false), "");
1872 accum
= LLVMBuildOr(ctx
->builder
, accum
, tmp
, "");
1877 } else if (log_recombine
< 0) {
1878 /* Split vectors of dwords */
1879 if (load_log_size
> 2) {
1880 assert(load_num_channels
== 1);
1881 LLVMValueRef loaded
= loads
[0];
1882 unsigned log_split
= load_log_size
- 2;
1883 log_recombine
+= log_split
;
1884 load_num_channels
= 1 << log_split
;
1886 for (unsigned i
= 0; i
< load_num_channels
; ++i
) {
1887 tmp
= LLVMConstInt(ctx
->i32
, i
, false);
1888 loads
[i
] = LLVMBuildExtractElement(ctx
->builder
, loaded
, tmp
, "");
1892 /* Further split dwords and shorts if required */
1893 if (log_recombine
< 0) {
1894 for (unsigned src
= load_num_channels
,
1895 dst
= load_num_channels
<< -log_recombine
;
1897 unsigned dst_bits
= 1 << (3 + load_log_size
+ log_recombine
);
1898 LLVMTypeRef dst_type
= LLVMIntTypeInContext(ctx
->context
, dst_bits
);
1899 LLVMValueRef loaded
= loads
[src
- 1];
1900 LLVMTypeRef loaded_type
= LLVMTypeOf(loaded
);
1901 for (unsigned i
= 1 << -log_recombine
; i
> 0; --i
, --dst
) {
1902 tmp
= LLVMConstInt(loaded_type
, dst_bits
* (i
- 1), false);
1903 tmp
= LLVMBuildLShr(ctx
->builder
, loaded
, tmp
, "");
1904 loads
[dst
- 1] = LLVMBuildTrunc(ctx
->builder
, tmp
, dst_type
, "");
1910 if (log_size
== 3) {
1911 if (format
== AC_FETCH_FORMAT_FLOAT
) {
1912 for (unsigned i
= 0; i
< num_channels
; ++i
) {
1913 tmp
= ac_build_gather_values(ctx
, &loads
[2 * i
], 2);
1914 loads
[i
] = LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f64
, "");
1916 } else if (format
== AC_FETCH_FORMAT_FIXED
) {
1917 /* 10_11_11_FLOAT */
1918 LLVMValueRef data
= loads
[0];
1919 LLVMValueRef i32_2047
= LLVMConstInt(ctx
->i32
, 2047, false);
1920 LLVMValueRef r
= LLVMBuildAnd(ctx
->builder
, data
, i32_2047
, "");
1921 tmp
= LLVMBuildLShr(ctx
->builder
, data
, LLVMConstInt(ctx
->i32
, 11, false), "");
1922 LLVMValueRef g
= LLVMBuildAnd(ctx
->builder
, tmp
, i32_2047
, "");
1923 LLVMValueRef b
= LLVMBuildLShr(ctx
->builder
, data
, LLVMConstInt(ctx
->i32
, 22, false), "");
1925 loads
[0] = ac_to_integer(ctx
, ac_ufN_to_float(ctx
, r
, 5, 6));
1926 loads
[1] = ac_to_integer(ctx
, ac_ufN_to_float(ctx
, g
, 5, 6));
1927 loads
[2] = ac_to_integer(ctx
, ac_ufN_to_float(ctx
, b
, 5, 5));
1931 format
= AC_FETCH_FORMAT_FLOAT
;
1933 /* 2_10_10_10 data formats */
1934 LLVMValueRef data
= loads
[0];
1935 LLVMTypeRef i10
= LLVMIntTypeInContext(ctx
->context
, 10);
1936 LLVMTypeRef i2
= LLVMIntTypeInContext(ctx
->context
, 2);
1937 loads
[0] = LLVMBuildTrunc(ctx
->builder
, data
, i10
, "");
1938 tmp
= LLVMBuildLShr(ctx
->builder
, data
, LLVMConstInt(ctx
->i32
, 10, false), "");
1939 loads
[1] = LLVMBuildTrunc(ctx
->builder
, tmp
, i10
, "");
1940 tmp
= LLVMBuildLShr(ctx
->builder
, data
, LLVMConstInt(ctx
->i32
, 20, false), "");
1941 loads
[2] = LLVMBuildTrunc(ctx
->builder
, tmp
, i10
, "");
1942 tmp
= LLVMBuildLShr(ctx
->builder
, data
, LLVMConstInt(ctx
->i32
, 30, false), "");
1943 loads
[3] = LLVMBuildTrunc(ctx
->builder
, tmp
, i2
, "");
1949 if (format
== AC_FETCH_FORMAT_FLOAT
) {
1950 if (log_size
!= 2) {
1951 for (unsigned chan
= 0; chan
< num_channels
; ++chan
) {
1952 tmp
= ac_to_float(ctx
, loads
[chan
]);
1954 tmp
= LLVMBuildFPTrunc(ctx
->builder
, tmp
, ctx
->f32
, "");
1955 else if (log_size
== 1)
1956 tmp
= LLVMBuildFPExt(ctx
->builder
, tmp
, ctx
->f32
, "");
1957 loads
[chan
] = ac_to_integer(ctx
, tmp
);
1960 } else if (format
== AC_FETCH_FORMAT_UINT
) {
1961 if (log_size
!= 2) {
1962 for (unsigned chan
= 0; chan
< num_channels
; ++chan
)
1963 loads
[chan
] = LLVMBuildZExt(ctx
->builder
, loads
[chan
], ctx
->i32
, "");
1965 } else if (format
== AC_FETCH_FORMAT_SINT
) {
1966 if (log_size
!= 2) {
1967 for (unsigned chan
= 0; chan
< num_channels
; ++chan
)
1968 loads
[chan
] = LLVMBuildSExt(ctx
->builder
, loads
[chan
], ctx
->i32
, "");
1971 bool unsign
= format
== AC_FETCH_FORMAT_UNORM
||
1972 format
== AC_FETCH_FORMAT_USCALED
||
1973 format
== AC_FETCH_FORMAT_UINT
;
1975 for (unsigned chan
= 0; chan
< num_channels
; ++chan
) {
1977 tmp
= LLVMBuildUIToFP(ctx
->builder
, loads
[chan
], ctx
->f32
, "");
1979 tmp
= LLVMBuildSIToFP(ctx
->builder
, loads
[chan
], ctx
->f32
, "");
1982 LLVMValueRef scale
= NULL
;
1983 if (format
== AC_FETCH_FORMAT_FIXED
) {
1984 assert(log_size
== 2);
1985 scale
= LLVMConstReal(ctx
->f32
, 1.0 / 0x10000);
1986 } else if (format
== AC_FETCH_FORMAT_UNORM
) {
1987 unsigned bits
= LLVMGetIntTypeWidth(LLVMTypeOf(loads
[chan
]));
1988 scale
= LLVMConstReal(ctx
->f32
, 1.0 / (((uint64_t)1 << bits
) - 1));
1989 } else if (format
== AC_FETCH_FORMAT_SNORM
) {
1990 unsigned bits
= LLVMGetIntTypeWidth(LLVMTypeOf(loads
[chan
]));
1991 scale
= LLVMConstReal(ctx
->f32
, 1.0 / (((uint64_t)1 << (bits
- 1)) - 1));
1994 tmp
= LLVMBuildFMul(ctx
->builder
, tmp
, scale
, "");
1996 if (format
== AC_FETCH_FORMAT_SNORM
) {
1997 /* Clamp to [-1, 1] */
1998 LLVMValueRef neg_one
= LLVMConstReal(ctx
->f32
, -1.0);
1999 LLVMValueRef clamp
=
2000 LLVMBuildFCmp(ctx
->builder
, LLVMRealULT
, tmp
, neg_one
, "");
2001 tmp
= LLVMBuildSelect(ctx
->builder
, clamp
, neg_one
, tmp
, "");
2004 loads
[chan
] = ac_to_integer(ctx
, tmp
);
2008 while (num_channels
< 4) {
2009 if (format
== AC_FETCH_FORMAT_UINT
|| format
== AC_FETCH_FORMAT_SINT
) {
2010 loads
[num_channels
] = num_channels
== 3 ? ctx
->i32_1
: ctx
->i32_0
;
2012 loads
[num_channels
] = ac_to_integer(ctx
, num_channels
== 3 ? ctx
->f32_1
: ctx
->f32_0
);
2019 loads
[0] = loads
[2];
2023 return ac_build_gather_values(ctx
, loads
, 4);
2027 ac_build_llvm8_tbuffer_store(struct ac_llvm_context
*ctx
,
2030 LLVMValueRef vindex
,
2031 LLVMValueRef voffset
,
2032 LLVMValueRef soffset
,
2033 unsigned num_channels
,
2036 unsigned cache_policy
,
2039 LLVMValueRef args
[7];
2041 args
[idx
++] = vdata
;
2042 args
[idx
++] = LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, "");
2044 args
[idx
++] = vindex
? vindex
: ctx
->i32_0
;
2045 args
[idx
++] = voffset
? voffset
: ctx
->i32_0
;
2046 args
[idx
++] = soffset
? soffset
: ctx
->i32_0
;
2047 args
[idx
++] = LLVMConstInt(ctx
->i32
, ac_get_tbuffer_format(ctx
, dfmt
, nfmt
), 0);
2048 args
[idx
++] = LLVMConstInt(ctx
->i32
, cache_policy
, 0);
2049 unsigned func
= !ac_has_vec3_support(ctx
->chip_class
, true) && num_channels
== 3 ? 4 : num_channels
;
2050 const char *indexing_kind
= structurized
? "struct" : "raw";
2051 char name
[256], type_name
[8];
2053 LLVMTypeRef type
= func
> 1 ? LLVMVectorType(ctx
->i32
, func
) : ctx
->i32
;
2054 ac_build_type_name_for_intr(type
, type_name
, sizeof(type_name
));
2056 snprintf(name
, sizeof(name
), "llvm.amdgcn.%s.tbuffer.store.%s",
2057 indexing_kind
, type_name
);
2059 ac_build_intrinsic(ctx
, name
, ctx
->voidt
, args
, idx
,
2060 AC_FUNC_ATTR_INACCESSIBLE_MEM_ONLY
);
2064 ac_build_tbuffer_store(struct ac_llvm_context
*ctx
,
2067 LLVMValueRef vindex
,
2068 LLVMValueRef voffset
,
2069 LLVMValueRef soffset
,
2070 LLVMValueRef immoffset
,
2071 unsigned num_channels
,
2074 unsigned cache_policy
,
2075 bool structurized
) /* only matters for LLVM 8+ */
2077 if (HAVE_LLVM
>= 0x800) {
2078 voffset
= LLVMBuildAdd(ctx
->builder
,
2079 voffset
? voffset
: ctx
->i32_0
,
2082 ac_build_llvm8_tbuffer_store(ctx
, rsrc
, vdata
, vindex
, voffset
,
2083 soffset
, num_channels
, dfmt
, nfmt
,
2084 cache_policy
, structurized
);
2086 LLVMValueRef params
[] = {
2089 vindex
? vindex
: ctx
->i32_0
,
2090 voffset
? voffset
: ctx
->i32_0
,
2091 soffset
? soffset
: ctx
->i32_0
,
2093 LLVMConstInt(ctx
->i32
, dfmt
, false),
2094 LLVMConstInt(ctx
->i32
, nfmt
, false),
2095 LLVMConstInt(ctx
->i1
, !!(cache_policy
& ac_glc
), false),
2096 LLVMConstInt(ctx
->i1
, !!(cache_policy
& ac_slc
), false),
2098 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
2099 const char *type_names
[] = {"i32", "v2i32", "v4i32"};
2102 snprintf(name
, sizeof(name
), "llvm.amdgcn.tbuffer.store.%s",
2105 ac_build_intrinsic(ctx
, name
, ctx
->voidt
, params
, 10,
2106 AC_FUNC_ATTR_INACCESSIBLE_MEM_ONLY
);
2111 ac_build_struct_tbuffer_store(struct ac_llvm_context
*ctx
,
2114 LLVMValueRef vindex
,
2115 LLVMValueRef voffset
,
2116 LLVMValueRef soffset
,
2117 LLVMValueRef immoffset
,
2118 unsigned num_channels
,
2121 unsigned cache_policy
)
2123 ac_build_tbuffer_store(ctx
, rsrc
, vdata
, vindex
, voffset
, soffset
,
2124 immoffset
, num_channels
, dfmt
, nfmt
, cache_policy
,
2129 ac_build_raw_tbuffer_store(struct ac_llvm_context
*ctx
,
2132 LLVMValueRef voffset
,
2133 LLVMValueRef soffset
,
2134 LLVMValueRef immoffset
,
2135 unsigned num_channels
,
2138 unsigned cache_policy
)
2140 ac_build_tbuffer_store(ctx
, rsrc
, vdata
, NULL
, voffset
, soffset
,
2141 immoffset
, num_channels
, dfmt
, nfmt
, cache_policy
,
2146 ac_build_tbuffer_store_short(struct ac_llvm_context
*ctx
,
2149 LLVMValueRef voffset
,
2150 LLVMValueRef soffset
,
2151 unsigned cache_policy
)
2153 vdata
= LLVMBuildBitCast(ctx
->builder
, vdata
, ctx
->i16
, "");
2155 if (HAVE_LLVM
>= 0x900) {
2156 /* LLVM 9+ supports i8/i16 with struct/raw intrinsics. */
2157 ac_build_llvm8_buffer_store_common(ctx
, rsrc
, vdata
, NULL
,
2158 voffset
, soffset
, 1,
2159 ctx
->i16
, cache_policy
,
2162 unsigned dfmt
= V_008F0C_BUF_DATA_FORMAT_16
;
2163 unsigned nfmt
= V_008F0C_BUF_NUM_FORMAT_UINT
;
2165 vdata
= LLVMBuildZExt(ctx
->builder
, vdata
, ctx
->i32
, "");
2167 ac_build_raw_tbuffer_store(ctx
, rsrc
, vdata
, voffset
, soffset
,
2168 ctx
->i32_0
, 1, dfmt
, nfmt
, cache_policy
);
2173 ac_build_tbuffer_store_byte(struct ac_llvm_context
*ctx
,
2176 LLVMValueRef voffset
,
2177 LLVMValueRef soffset
,
2178 unsigned cache_policy
)
2180 vdata
= LLVMBuildBitCast(ctx
->builder
, vdata
, ctx
->i8
, "");
2182 if (HAVE_LLVM
>= 0x900) {
2183 /* LLVM 9+ supports i8/i16 with struct/raw intrinsics. */
2184 ac_build_llvm8_buffer_store_common(ctx
, rsrc
, vdata
, NULL
,
2185 voffset
, soffset
, 1,
2186 ctx
->i8
, cache_policy
,
2189 unsigned dfmt
= V_008F0C_BUF_DATA_FORMAT_8
;
2190 unsigned nfmt
= V_008F0C_BUF_NUM_FORMAT_UINT
;
2192 vdata
= LLVMBuildZExt(ctx
->builder
, vdata
, ctx
->i32
, "");
2194 ac_build_raw_tbuffer_store(ctx
, rsrc
, vdata
, voffset
, soffset
,
2195 ctx
->i32_0
, 1, dfmt
, nfmt
, cache_policy
);
2199 * Set range metadata on an instruction. This can only be used on load and
2200 * call instructions. If you know an instruction can only produce the values
2201 * 0, 1, 2, you would do set_range_metadata(value, 0, 3);
2202 * \p lo is the minimum value inclusive.
2203 * \p hi is the maximum value exclusive.
2205 static void set_range_metadata(struct ac_llvm_context
*ctx
,
2206 LLVMValueRef value
, unsigned lo
, unsigned hi
)
2208 LLVMValueRef range_md
, md_args
[2];
2209 LLVMTypeRef type
= LLVMTypeOf(value
);
2210 LLVMContextRef context
= LLVMGetTypeContext(type
);
2212 md_args
[0] = LLVMConstInt(type
, lo
, false);
2213 md_args
[1] = LLVMConstInt(type
, hi
, false);
2214 range_md
= LLVMMDNodeInContext(context
, md_args
, 2);
2215 LLVMSetMetadata(value
, ctx
->range_md_kind
, range_md
);
2219 ac_get_thread_id(struct ac_llvm_context
*ctx
)
2223 LLVMValueRef tid_args
[2];
2224 tid_args
[0] = LLVMConstInt(ctx
->i32
, 0xffffffff, false);
2225 tid_args
[1] = ctx
->i32_0
;
2226 tid_args
[1] = ac_build_intrinsic(ctx
,
2227 "llvm.amdgcn.mbcnt.lo", ctx
->i32
,
2228 tid_args
, 2, AC_FUNC_ATTR_READNONE
);
2230 if (ctx
->wave_size
== 32) {
2233 tid
= ac_build_intrinsic(ctx
, "llvm.amdgcn.mbcnt.hi",
2235 2, AC_FUNC_ATTR_READNONE
);
2237 set_range_metadata(ctx
, tid
, 0, ctx
->wave_size
);
2242 * AMD GCN implements derivatives using the local data store (LDS)
2243 * All writes to the LDS happen in all executing threads at
2244 * the same time. TID is the Thread ID for the current
2245 * thread and is a value between 0 and 63, representing
2246 * the thread's position in the wavefront.
2248 * For the pixel shader threads are grouped into quads of four pixels.
2249 * The TIDs of the pixels of a quad are:
2257 * So, masking the TID with 0xfffffffc yields the TID of the top left pixel
2258 * of the quad, masking with 0xfffffffd yields the TID of the top pixel of
2259 * the current pixel's column, and masking with 0xfffffffe yields the TID
2260 * of the left pixel of the current pixel's row.
2262 * Adding 1 yields the TID of the pixel to the right of the left pixel, and
2263 * adding 2 yields the TID of the pixel below the top pixel.
2266 ac_build_ddxy(struct ac_llvm_context
*ctx
,
2271 unsigned tl_lanes
[4], trbl_lanes
[4];
2272 char name
[32], type
[8];
2273 LLVMValueRef tl
, trbl
;
2274 LLVMTypeRef result_type
;
2275 LLVMValueRef result
;
2277 result_type
= ac_to_float_type(ctx
, LLVMTypeOf(val
));
2279 if (result_type
== ctx
->f16
)
2280 val
= LLVMBuildZExt(ctx
->builder
, val
, ctx
->i32
, "");
2282 for (unsigned i
= 0; i
< 4; ++i
) {
2283 tl_lanes
[i
] = i
& mask
;
2284 trbl_lanes
[i
] = (i
& mask
) + idx
;
2287 tl
= ac_build_quad_swizzle(ctx
, val
,
2288 tl_lanes
[0], tl_lanes
[1],
2289 tl_lanes
[2], tl_lanes
[3]);
2290 trbl
= ac_build_quad_swizzle(ctx
, val
,
2291 trbl_lanes
[0], trbl_lanes
[1],
2292 trbl_lanes
[2], trbl_lanes
[3]);
2294 if (result_type
== ctx
->f16
) {
2295 tl
= LLVMBuildTrunc(ctx
->builder
, tl
, ctx
->i16
, "");
2296 trbl
= LLVMBuildTrunc(ctx
->builder
, trbl
, ctx
->i16
, "");
2299 tl
= LLVMBuildBitCast(ctx
->builder
, tl
, result_type
, "");
2300 trbl
= LLVMBuildBitCast(ctx
->builder
, trbl
, result_type
, "");
2301 result
= LLVMBuildFSub(ctx
->builder
, trbl
, tl
, "");
2303 ac_build_type_name_for_intr(result_type
, type
, sizeof(type
));
2304 snprintf(name
, sizeof(name
), "llvm.amdgcn.wqm.%s", type
);
2306 return ac_build_intrinsic(ctx
, name
, result_type
, &result
, 1, 0);
2310 ac_build_sendmsg(struct ac_llvm_context
*ctx
,
2312 LLVMValueRef wave_id
)
2314 LLVMValueRef args
[2];
2315 args
[0] = LLVMConstInt(ctx
->i32
, msg
, false);
2317 ac_build_intrinsic(ctx
, "llvm.amdgcn.s.sendmsg", ctx
->voidt
, args
, 2, 0);
2321 ac_build_imsb(struct ac_llvm_context
*ctx
,
2323 LLVMTypeRef dst_type
)
2325 LLVMValueRef msb
= ac_build_intrinsic(ctx
, "llvm.amdgcn.sffbh.i32",
2327 AC_FUNC_ATTR_READNONE
);
2329 /* The HW returns the last bit index from MSB, but NIR/TGSI wants
2330 * the index from LSB. Invert it by doing "31 - msb". */
2331 msb
= LLVMBuildSub(ctx
->builder
, LLVMConstInt(ctx
->i32
, 31, false),
2334 LLVMValueRef all_ones
= LLVMConstInt(ctx
->i32
, -1, true);
2335 LLVMValueRef cond
= LLVMBuildOr(ctx
->builder
,
2336 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
2337 arg
, ctx
->i32_0
, ""),
2338 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
2339 arg
, all_ones
, ""), "");
2341 return LLVMBuildSelect(ctx
->builder
, cond
, all_ones
, msb
, "");
2345 ac_build_umsb(struct ac_llvm_context
*ctx
,
2347 LLVMTypeRef dst_type
)
2349 const char *intrin_name
;
2351 LLVMValueRef highest_bit
;
2355 bitsize
= ac_get_elem_bits(ctx
, LLVMTypeOf(arg
));
2358 intrin_name
= "llvm.ctlz.i64";
2360 highest_bit
= LLVMConstInt(ctx
->i64
, 63, false);
2364 intrin_name
= "llvm.ctlz.i32";
2366 highest_bit
= LLVMConstInt(ctx
->i32
, 31, false);
2370 intrin_name
= "llvm.ctlz.i16";
2372 highest_bit
= LLVMConstInt(ctx
->i16
, 15, false);
2376 intrin_name
= "llvm.ctlz.i8";
2378 highest_bit
= LLVMConstInt(ctx
->i8
, 7, false);
2382 unreachable(!"invalid bitsize");
2386 LLVMValueRef params
[2] = {
2391 LLVMValueRef msb
= ac_build_intrinsic(ctx
, intrin_name
, type
,
2393 AC_FUNC_ATTR_READNONE
);
2395 /* The HW returns the last bit index from MSB, but TGSI/NIR wants
2396 * the index from LSB. Invert it by doing "31 - msb". */
2397 msb
= LLVMBuildSub(ctx
->builder
, highest_bit
, msb
, "");
2399 if (bitsize
== 64) {
2400 msb
= LLVMBuildTrunc(ctx
->builder
, msb
, ctx
->i32
, "");
2401 } else if (bitsize
< 32) {
2402 msb
= LLVMBuildSExt(ctx
->builder
, msb
, ctx
->i32
, "");
2405 /* check for zero */
2406 return LLVMBuildSelect(ctx
->builder
,
2407 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, arg
, zero
, ""),
2408 LLVMConstInt(ctx
->i32
, -1, true), msb
, "");
2411 LLVMValueRef
ac_build_fmin(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
2415 snprintf(name
, sizeof(name
), "llvm.minnum.f%d", ac_get_elem_bits(ctx
, LLVMTypeOf(a
)));
2416 LLVMValueRef args
[2] = {a
, b
};
2417 return ac_build_intrinsic(ctx
, name
, LLVMTypeOf(a
), args
, 2,
2418 AC_FUNC_ATTR_READNONE
);
2421 LLVMValueRef
ac_build_fmax(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
2425 snprintf(name
, sizeof(name
), "llvm.maxnum.f%d", ac_get_elem_bits(ctx
, LLVMTypeOf(a
)));
2426 LLVMValueRef args
[2] = {a
, b
};
2427 return ac_build_intrinsic(ctx
, name
, LLVMTypeOf(a
), args
, 2,
2428 AC_FUNC_ATTR_READNONE
);
2431 LLVMValueRef
ac_build_imin(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
2434 LLVMValueRef cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSLE
, a
, b
, "");
2435 return LLVMBuildSelect(ctx
->builder
, cmp
, a
, b
, "");
2438 LLVMValueRef
ac_build_imax(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
2441 LLVMValueRef cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGT
, a
, b
, "");
2442 return LLVMBuildSelect(ctx
->builder
, cmp
, a
, b
, "");
2445 LLVMValueRef
ac_build_umin(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
2448 LLVMValueRef cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntULE
, a
, b
, "");
2449 return LLVMBuildSelect(ctx
->builder
, cmp
, a
, b
, "");
2452 LLVMValueRef
ac_build_umax(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
2455 LLVMValueRef cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntUGE
, a
, b
, "");
2456 return LLVMBuildSelect(ctx
->builder
, cmp
, a
, b
, "");
2459 LLVMValueRef
ac_build_clamp(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
2461 LLVMTypeRef t
= LLVMTypeOf(value
);
2462 return ac_build_fmin(ctx
, ac_build_fmax(ctx
, value
, LLVMConstReal(t
, 0.0)),
2463 LLVMConstReal(t
, 1.0));
2466 void ac_build_export(struct ac_llvm_context
*ctx
, struct ac_export_args
*a
)
2468 LLVMValueRef args
[9];
2470 args
[0] = LLVMConstInt(ctx
->i32
, a
->target
, 0);
2471 args
[1] = LLVMConstInt(ctx
->i32
, a
->enabled_channels
, 0);
2474 LLVMTypeRef i16
= LLVMInt16TypeInContext(ctx
->context
);
2475 LLVMTypeRef v2i16
= LLVMVectorType(i16
, 2);
2477 args
[2] = LLVMBuildBitCast(ctx
->builder
, a
->out
[0],
2479 args
[3] = LLVMBuildBitCast(ctx
->builder
, a
->out
[1],
2481 args
[4] = LLVMConstInt(ctx
->i1
, a
->done
, 0);
2482 args
[5] = LLVMConstInt(ctx
->i1
, a
->valid_mask
, 0);
2484 ac_build_intrinsic(ctx
, "llvm.amdgcn.exp.compr.v2i16",
2485 ctx
->voidt
, args
, 6, 0);
2487 args
[2] = a
->out
[0];
2488 args
[3] = a
->out
[1];
2489 args
[4] = a
->out
[2];
2490 args
[5] = a
->out
[3];
2491 args
[6] = LLVMConstInt(ctx
->i1
, a
->done
, 0);
2492 args
[7] = LLVMConstInt(ctx
->i1
, a
->valid_mask
, 0);
2494 ac_build_intrinsic(ctx
, "llvm.amdgcn.exp.f32",
2495 ctx
->voidt
, args
, 8, 0);
2499 void ac_build_export_null(struct ac_llvm_context
*ctx
)
2501 struct ac_export_args args
;
2503 args
.enabled_channels
= 0x0; /* enabled channels */
2504 args
.valid_mask
= 1; /* whether the EXEC mask is valid */
2505 args
.done
= 1; /* DONE bit */
2506 args
.target
= V_008DFC_SQ_EXP_NULL
;
2507 args
.compr
= 0; /* COMPR flag (0 = 32-bit export) */
2508 args
.out
[0] = LLVMGetUndef(ctx
->f32
); /* R */
2509 args
.out
[1] = LLVMGetUndef(ctx
->f32
); /* G */
2510 args
.out
[2] = LLVMGetUndef(ctx
->f32
); /* B */
2511 args
.out
[3] = LLVMGetUndef(ctx
->f32
); /* A */
2513 ac_build_export(ctx
, &args
);
2516 static unsigned ac_num_coords(enum ac_image_dim dim
)
2522 case ac_image_1darray
:
2526 case ac_image_2darray
:
2527 case ac_image_2dmsaa
:
2529 case ac_image_2darraymsaa
:
2532 unreachable("ac_num_coords: bad dim");
2536 static unsigned ac_num_derivs(enum ac_image_dim dim
)
2540 case ac_image_1darray
:
2543 case ac_image_2darray
:
2548 case ac_image_2dmsaa
:
2549 case ac_image_2darraymsaa
:
2551 unreachable("derivatives not supported");
2555 static const char *get_atomic_name(enum ac_atomic_op op
)
2558 case ac_atomic_swap
: return "swap";
2559 case ac_atomic_add
: return "add";
2560 case ac_atomic_sub
: return "sub";
2561 case ac_atomic_smin
: return "smin";
2562 case ac_atomic_umin
: return "umin";
2563 case ac_atomic_smax
: return "smax";
2564 case ac_atomic_umax
: return "umax";
2565 case ac_atomic_and
: return "and";
2566 case ac_atomic_or
: return "or";
2567 case ac_atomic_xor
: return "xor";
2569 unreachable("bad atomic op");
2572 LLVMValueRef
ac_build_image_opcode(struct ac_llvm_context
*ctx
,
2573 struct ac_image_args
*a
)
2575 const char *overload
[3] = { "", "", "" };
2576 unsigned num_overloads
= 0;
2577 LLVMValueRef args
[18];
2578 unsigned num_args
= 0;
2579 enum ac_image_dim dim
= a
->dim
;
2581 assert(!a
->lod
|| a
->lod
== ctx
->i32_0
|| a
->lod
== ctx
->f32_0
||
2583 assert((a
->opcode
!= ac_image_get_resinfo
&& a
->opcode
!= ac_image_load_mip
&&
2584 a
->opcode
!= ac_image_store_mip
) ||
2586 assert(a
->opcode
== ac_image_sample
|| a
->opcode
== ac_image_gather4
||
2587 (!a
->compare
&& !a
->offset
));
2588 assert((a
->opcode
== ac_image_sample
|| a
->opcode
== ac_image_gather4
||
2589 a
->opcode
== ac_image_get_lod
) ||
2591 assert((a
->bias
? 1 : 0) +
2593 (a
->level_zero
? 1 : 0) +
2594 (a
->derivs
[0] ? 1 : 0) <= 1);
2596 if (a
->opcode
== ac_image_get_lod
) {
2598 case ac_image_1darray
:
2601 case ac_image_2darray
:
2610 bool sample
= a
->opcode
== ac_image_sample
||
2611 a
->opcode
== ac_image_gather4
||
2612 a
->opcode
== ac_image_get_lod
;
2613 bool atomic
= a
->opcode
== ac_image_atomic
||
2614 a
->opcode
== ac_image_atomic_cmpswap
;
2615 bool load
= a
->opcode
== ac_image_sample
||
2616 a
->opcode
== ac_image_gather4
||
2617 a
->opcode
== ac_image_load
||
2618 a
->opcode
== ac_image_load_mip
;
2619 LLVMTypeRef coord_type
= sample
? ctx
->f32
: ctx
->i32
;
2621 if (atomic
|| a
->opcode
== ac_image_store
|| a
->opcode
== ac_image_store_mip
) {
2622 args
[num_args
++] = a
->data
[0];
2623 if (a
->opcode
== ac_image_atomic_cmpswap
)
2624 args
[num_args
++] = a
->data
[1];
2628 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->dmask
, false);
2631 args
[num_args
++] = ac_to_integer(ctx
, a
->offset
);
2633 args
[num_args
++] = ac_to_float(ctx
, a
->bias
);
2634 overload
[num_overloads
++] = ".f32";
2637 args
[num_args
++] = ac_to_float(ctx
, a
->compare
);
2639 unsigned count
= ac_num_derivs(dim
);
2640 for (unsigned i
= 0; i
< count
; ++i
)
2641 args
[num_args
++] = ac_to_float(ctx
, a
->derivs
[i
]);
2642 overload
[num_overloads
++] = ".f32";
2644 unsigned num_coords
=
2645 a
->opcode
!= ac_image_get_resinfo
? ac_num_coords(dim
) : 0;
2646 for (unsigned i
= 0; i
< num_coords
; ++i
)
2647 args
[num_args
++] = LLVMBuildBitCast(ctx
->builder
, a
->coords
[i
], coord_type
, "");
2649 args
[num_args
++] = LLVMBuildBitCast(ctx
->builder
, a
->lod
, coord_type
, "");
2650 overload
[num_overloads
++] = sample
? ".f32" : ".i32";
2652 args
[num_args
++] = a
->resource
;
2654 args
[num_args
++] = a
->sampler
;
2655 args
[num_args
++] = LLVMConstInt(ctx
->i1
, a
->unorm
, false);
2658 args
[num_args
++] = ctx
->i32_0
; /* texfailctrl */
2659 args
[num_args
++] = LLVMConstInt(ctx
->i32
,
2660 load
? get_load_cache_policy(ctx
, a
->cache_policy
) :
2661 a
->cache_policy
, false);
2664 const char *atomic_subop
= "";
2665 switch (a
->opcode
) {
2666 case ac_image_sample
: name
= "sample"; break;
2667 case ac_image_gather4
: name
= "gather4"; break;
2668 case ac_image_load
: name
= "load"; break;
2669 case ac_image_load_mip
: name
= "load.mip"; break;
2670 case ac_image_store
: name
= "store"; break;
2671 case ac_image_store_mip
: name
= "store.mip"; break;
2672 case ac_image_atomic
:
2674 atomic_subop
= get_atomic_name(a
->atomic
);
2676 case ac_image_atomic_cmpswap
:
2678 atomic_subop
= "cmpswap";
2680 case ac_image_get_lod
: name
= "getlod"; break;
2681 case ac_image_get_resinfo
: name
= "getresinfo"; break;
2682 default: unreachable("invalid image opcode");
2685 const char *dimname
;
2687 case ac_image_1d
: dimname
= "1d"; break;
2688 case ac_image_2d
: dimname
= "2d"; break;
2689 case ac_image_3d
: dimname
= "3d"; break;
2690 case ac_image_cube
: dimname
= "cube"; break;
2691 case ac_image_1darray
: dimname
= "1darray"; break;
2692 case ac_image_2darray
: dimname
= "2darray"; break;
2693 case ac_image_2dmsaa
: dimname
= "2dmsaa"; break;
2694 case ac_image_2darraymsaa
: dimname
= "2darraymsaa"; break;
2695 default: unreachable("invalid dim");
2699 a
->lod
&& (a
->opcode
== ac_image_sample
|| a
->opcode
== ac_image_gather4
);
2701 snprintf(intr_name
, sizeof(intr_name
),
2702 "llvm.amdgcn.image.%s%s" /* base name */
2703 "%s%s%s" /* sample/gather modifiers */
2704 ".%s.%s%s%s%s", /* dimension and type overloads */
2706 a
->compare
? ".c" : "",
2709 a
->derivs
[0] ? ".d" :
2710 a
->level_zero
? ".lz" : "",
2711 a
->offset
? ".o" : "",
2713 atomic
? "i32" : "v4f32",
2714 overload
[0], overload
[1], overload
[2]);
2719 else if (a
->opcode
== ac_image_store
|| a
->opcode
== ac_image_store_mip
)
2724 LLVMValueRef result
=
2725 ac_build_intrinsic(ctx
, intr_name
, retty
, args
, num_args
,
2727 if (!sample
&& retty
== ctx
->v4f32
) {
2728 result
= LLVMBuildBitCast(ctx
->builder
, result
,
2734 LLVMValueRef
ac_build_cvt_pkrtz_f16(struct ac_llvm_context
*ctx
,
2735 LLVMValueRef args
[2])
2738 LLVMVectorType(LLVMHalfTypeInContext(ctx
->context
), 2);
2740 return ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pkrtz", v2f16
,
2741 args
, 2, AC_FUNC_ATTR_READNONE
);
2744 LLVMValueRef
ac_build_cvt_pknorm_i16(struct ac_llvm_context
*ctx
,
2745 LLVMValueRef args
[2])
2748 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pknorm.i16",
2749 ctx
->v2i16
, args
, 2,
2750 AC_FUNC_ATTR_READNONE
);
2751 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
2754 LLVMValueRef
ac_build_cvt_pknorm_u16(struct ac_llvm_context
*ctx
,
2755 LLVMValueRef args
[2])
2758 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pknorm.u16",
2759 ctx
->v2i16
, args
, 2,
2760 AC_FUNC_ATTR_READNONE
);
2761 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
2764 /* The 8-bit and 10-bit clamping is for HW workarounds. */
2765 LLVMValueRef
ac_build_cvt_pk_i16(struct ac_llvm_context
*ctx
,
2766 LLVMValueRef args
[2], unsigned bits
, bool hi
)
2768 assert(bits
== 8 || bits
== 10 || bits
== 16);
2770 LLVMValueRef max_rgb
= LLVMConstInt(ctx
->i32
,
2771 bits
== 8 ? 127 : bits
== 10 ? 511 : 32767, 0);
2772 LLVMValueRef min_rgb
= LLVMConstInt(ctx
->i32
,
2773 bits
== 8 ? -128 : bits
== 10 ? -512 : -32768, 0);
2774 LLVMValueRef max_alpha
=
2775 bits
!= 10 ? max_rgb
: ctx
->i32_1
;
2776 LLVMValueRef min_alpha
=
2777 bits
!= 10 ? min_rgb
: LLVMConstInt(ctx
->i32
, -2, 0);
2781 for (int i
= 0; i
< 2; i
++) {
2782 bool alpha
= hi
&& i
== 1;
2783 args
[i
] = ac_build_imin(ctx
, args
[i
],
2784 alpha
? max_alpha
: max_rgb
);
2785 args
[i
] = ac_build_imax(ctx
, args
[i
],
2786 alpha
? min_alpha
: min_rgb
);
2791 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pk.i16",
2792 ctx
->v2i16
, args
, 2,
2793 AC_FUNC_ATTR_READNONE
);
2794 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
2797 /* The 8-bit and 10-bit clamping is for HW workarounds. */
2798 LLVMValueRef
ac_build_cvt_pk_u16(struct ac_llvm_context
*ctx
,
2799 LLVMValueRef args
[2], unsigned bits
, bool hi
)
2801 assert(bits
== 8 || bits
== 10 || bits
== 16);
2803 LLVMValueRef max_rgb
= LLVMConstInt(ctx
->i32
,
2804 bits
== 8 ? 255 : bits
== 10 ? 1023 : 65535, 0);
2805 LLVMValueRef max_alpha
=
2806 bits
!= 10 ? max_rgb
: LLVMConstInt(ctx
->i32
, 3, 0);
2810 for (int i
= 0; i
< 2; i
++) {
2811 bool alpha
= hi
&& i
== 1;
2812 args
[i
] = ac_build_umin(ctx
, args
[i
],
2813 alpha
? max_alpha
: max_rgb
);
2818 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pk.u16",
2819 ctx
->v2i16
, args
, 2,
2820 AC_FUNC_ATTR_READNONE
);
2821 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
2824 LLVMValueRef
ac_build_wqm_vote(struct ac_llvm_context
*ctx
, LLVMValueRef i1
)
2826 return ac_build_intrinsic(ctx
, "llvm.amdgcn.wqm.vote", ctx
->i1
,
2827 &i1
, 1, AC_FUNC_ATTR_READNONE
);
2830 void ac_build_kill_if_false(struct ac_llvm_context
*ctx
, LLVMValueRef i1
)
2832 ac_build_intrinsic(ctx
, "llvm.amdgcn.kill", ctx
->voidt
,
2836 LLVMValueRef
ac_build_bfe(struct ac_llvm_context
*ctx
, LLVMValueRef input
,
2837 LLVMValueRef offset
, LLVMValueRef width
,
2840 LLVMValueRef args
[] = {
2846 LLVMValueRef result
= ac_build_intrinsic(ctx
,
2847 is_signed
? "llvm.amdgcn.sbfe.i32" :
2848 "llvm.amdgcn.ubfe.i32",
2850 AC_FUNC_ATTR_READNONE
);
2852 if (HAVE_LLVM
< 0x0800) {
2853 /* FIXME: LLVM 7+ returns incorrect result when count is 0.
2854 * https://bugs.freedesktop.org/show_bug.cgi?id=107276
2856 LLVMValueRef zero
= ctx
->i32_0
;
2857 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, width
, zero
, "");
2858 result
= LLVMBuildSelect(ctx
->builder
, icond
, zero
, result
, "");
2864 LLVMValueRef
ac_build_imad(struct ac_llvm_context
*ctx
, LLVMValueRef s0
,
2865 LLVMValueRef s1
, LLVMValueRef s2
)
2867 return LLVMBuildAdd(ctx
->builder
,
2868 LLVMBuildMul(ctx
->builder
, s0
, s1
, ""), s2
, "");
2871 LLVMValueRef
ac_build_fmad(struct ac_llvm_context
*ctx
, LLVMValueRef s0
,
2872 LLVMValueRef s1
, LLVMValueRef s2
)
2874 return LLVMBuildFAdd(ctx
->builder
,
2875 LLVMBuildFMul(ctx
->builder
, s0
, s1
, ""), s2
, "");
2878 void ac_build_waitcnt(struct ac_llvm_context
*ctx
, unsigned wait_flags
)
2883 unsigned lgkmcnt
= 63;
2884 unsigned vmcnt
= ctx
->chip_class
>= GFX9
? 63 : 15;
2885 unsigned vscnt
= 63;
2887 if (wait_flags
& AC_WAIT_LGKM
)
2889 if (wait_flags
& AC_WAIT_VLOAD
)
2892 if (wait_flags
& AC_WAIT_VSTORE
) {
2893 if (ctx
->chip_class
>= GFX10
)
2899 /* There is no intrinsic for vscnt(0), so use a fence. */
2900 if ((wait_flags
& AC_WAIT_LGKM
&&
2901 wait_flags
& AC_WAIT_VLOAD
&&
2902 wait_flags
& AC_WAIT_VSTORE
) ||
2904 LLVMBuildFence(ctx
->builder
, LLVMAtomicOrderingRelease
, false, "");
2908 unsigned simm16
= (lgkmcnt
<< 8) |
2909 (7 << 4) | /* expcnt */
2911 ((vmcnt
>> 4) << 14);
2913 LLVMValueRef args
[1] = {
2914 LLVMConstInt(ctx
->i32
, simm16
, false),
2916 ac_build_intrinsic(ctx
, "llvm.amdgcn.s.waitcnt",
2917 ctx
->voidt
, args
, 1, 0);
2920 LLVMValueRef
ac_build_fmed3(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
2921 LLVMValueRef src1
, LLVMValueRef src2
,
2927 if (bitsize
== 16) {
2928 intr
= "llvm.amdgcn.fmed3.f16";
2930 } else if (bitsize
== 32) {
2931 intr
= "llvm.amdgcn.fmed3.f32";
2934 intr
= "llvm.amdgcn.fmed3.f64";
2938 LLVMValueRef params
[] = {
2943 return ac_build_intrinsic(ctx
, intr
, type
, params
, 3,
2944 AC_FUNC_ATTR_READNONE
);
2947 LLVMValueRef
ac_build_fract(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
2953 if (bitsize
== 16) {
2954 intr
= "llvm.amdgcn.fract.f16";
2956 } else if (bitsize
== 32) {
2957 intr
= "llvm.amdgcn.fract.f32";
2960 intr
= "llvm.amdgcn.fract.f64";
2964 LLVMValueRef params
[] = {
2967 return ac_build_intrinsic(ctx
, intr
, type
, params
, 1,
2968 AC_FUNC_ATTR_READNONE
);
2971 LLVMValueRef
ac_build_isign(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
2974 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->context
, bitsize
);
2975 LLVMValueRef zero
= LLVMConstInt(type
, 0, false);
2976 LLVMValueRef one
= LLVMConstInt(type
, 1, false);
2978 LLVMValueRef cmp
, val
;
2979 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGT
, src0
, zero
, "");
2980 val
= LLVMBuildSelect(ctx
->builder
, cmp
, one
, src0
, "");
2981 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGE
, val
, zero
, "");
2982 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstInt(type
, -1, true), "");
2986 LLVMValueRef
ac_build_fsign(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
2989 LLVMValueRef cmp
, val
, zero
, one
;
2992 if (bitsize
== 16) {
2996 } else if (bitsize
== 32) {
3006 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGT
, src0
, zero
, "");
3007 val
= LLVMBuildSelect(ctx
->builder
, cmp
, one
, src0
, "");
3008 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGE
, val
, zero
, "");
3009 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstReal(type
, -1.0), "");
3013 LLVMValueRef
ac_build_bit_count(struct ac_llvm_context
*ctx
, LLVMValueRef src0
)
3015 LLVMValueRef result
;
3018 bitsize
= ac_get_elem_bits(ctx
, LLVMTypeOf(src0
));
3022 result
= ac_build_intrinsic(ctx
, "llvm.ctpop.i64", ctx
->i64
,
3023 (LLVMValueRef
[]) { src0
}, 1,
3024 AC_FUNC_ATTR_READNONE
);
3026 result
= LLVMBuildTrunc(ctx
->builder
, result
, ctx
->i32
, "");
3029 result
= ac_build_intrinsic(ctx
, "llvm.ctpop.i32", ctx
->i32
,
3030 (LLVMValueRef
[]) { src0
}, 1,
3031 AC_FUNC_ATTR_READNONE
);
3034 result
= ac_build_intrinsic(ctx
, "llvm.ctpop.i16", ctx
->i16
,
3035 (LLVMValueRef
[]) { src0
}, 1,
3036 AC_FUNC_ATTR_READNONE
);
3038 result
= LLVMBuildZExt(ctx
->builder
, result
, ctx
->i32
, "");
3041 result
= ac_build_intrinsic(ctx
, "llvm.ctpop.i8", ctx
->i8
,
3042 (LLVMValueRef
[]) { src0
}, 1,
3043 AC_FUNC_ATTR_READNONE
);
3045 result
= LLVMBuildZExt(ctx
->builder
, result
, ctx
->i32
, "");
3048 unreachable(!"invalid bitsize");
3055 LLVMValueRef
ac_build_bitfield_reverse(struct ac_llvm_context
*ctx
,
3058 LLVMValueRef result
;
3061 bitsize
= ac_get_elem_bits(ctx
, LLVMTypeOf(src0
));
3065 result
= ac_build_intrinsic(ctx
, "llvm.bitreverse.i64", ctx
->i64
,
3066 (LLVMValueRef
[]) { src0
}, 1,
3067 AC_FUNC_ATTR_READNONE
);
3069 result
= LLVMBuildTrunc(ctx
->builder
, result
, ctx
->i32
, "");
3072 result
= ac_build_intrinsic(ctx
, "llvm.bitreverse.i32", ctx
->i32
,
3073 (LLVMValueRef
[]) { src0
}, 1,
3074 AC_FUNC_ATTR_READNONE
);
3077 result
= ac_build_intrinsic(ctx
, "llvm.bitreverse.i16", ctx
->i16
,
3078 (LLVMValueRef
[]) { src0
}, 1,
3079 AC_FUNC_ATTR_READNONE
);
3081 result
= LLVMBuildZExt(ctx
->builder
, result
, ctx
->i32
, "");
3084 result
= ac_build_intrinsic(ctx
, "llvm.bitreverse.i8", ctx
->i8
,
3085 (LLVMValueRef
[]) { src0
}, 1,
3086 AC_FUNC_ATTR_READNONE
);
3088 result
= LLVMBuildZExt(ctx
->builder
, result
, ctx
->i32
, "");
3091 unreachable(!"invalid bitsize");
3098 #define AC_EXP_TARGET 0
3099 #define AC_EXP_ENABLED_CHANNELS 1
3100 #define AC_EXP_OUT0 2
3108 struct ac_vs_exp_chan
3112 enum ac_ir_type type
;
3115 struct ac_vs_exp_inst
{
3118 struct ac_vs_exp_chan chan
[4];
3121 struct ac_vs_exports
{
3123 struct ac_vs_exp_inst exp
[VARYING_SLOT_MAX
];
3126 /* Return true if the PARAM export has been eliminated. */
3127 static bool ac_eliminate_const_output(uint8_t *vs_output_param_offset
,
3128 uint32_t num_outputs
,
3129 struct ac_vs_exp_inst
*exp
)
3131 unsigned i
, default_val
; /* SPI_PS_INPUT_CNTL_i.DEFAULT_VAL */
3132 bool is_zero
[4] = {}, is_one
[4] = {};
3134 for (i
= 0; i
< 4; i
++) {
3135 /* It's a constant expression. Undef outputs are eliminated too. */
3136 if (exp
->chan
[i
].type
== AC_IR_UNDEF
) {
3139 } else if (exp
->chan
[i
].type
== AC_IR_CONST
) {
3140 if (exp
->chan
[i
].const_float
== 0)
3142 else if (exp
->chan
[i
].const_float
== 1)
3145 return false; /* other constant */
3150 /* Only certain combinations of 0 and 1 can be eliminated. */
3151 if (is_zero
[0] && is_zero
[1] && is_zero
[2])
3152 default_val
= is_zero
[3] ? 0 : 1;
3153 else if (is_one
[0] && is_one
[1] && is_one
[2])
3154 default_val
= is_zero
[3] ? 2 : 3;
3158 /* The PARAM export can be represented as DEFAULT_VAL. Kill it. */
3159 LLVMInstructionEraseFromParent(exp
->inst
);
3161 /* Change OFFSET to DEFAULT_VAL. */
3162 for (i
= 0; i
< num_outputs
; i
++) {
3163 if (vs_output_param_offset
[i
] == exp
->offset
) {
3164 vs_output_param_offset
[i
] =
3165 AC_EXP_PARAM_DEFAULT_VAL_0000
+ default_val
;
3172 static bool ac_eliminate_duplicated_output(struct ac_llvm_context
*ctx
,
3173 uint8_t *vs_output_param_offset
,
3174 uint32_t num_outputs
,
3175 struct ac_vs_exports
*processed
,
3176 struct ac_vs_exp_inst
*exp
)
3178 unsigned p
, copy_back_channels
= 0;
3180 /* See if the output is already in the list of processed outputs.
3181 * The LLVMValueRef comparison relies on SSA.
3183 for (p
= 0; p
< processed
->num
; p
++) {
3184 bool different
= false;
3186 for (unsigned j
= 0; j
< 4; j
++) {
3187 struct ac_vs_exp_chan
*c1
= &processed
->exp
[p
].chan
[j
];
3188 struct ac_vs_exp_chan
*c2
= &exp
->chan
[j
];
3190 /* Treat undef as a match. */
3191 if (c2
->type
== AC_IR_UNDEF
)
3194 /* If c1 is undef but c2 isn't, we can copy c2 to c1
3195 * and consider the instruction duplicated.
3197 if (c1
->type
== AC_IR_UNDEF
) {
3198 copy_back_channels
|= 1 << j
;
3202 /* Test whether the channels are not equal. */
3203 if (c1
->type
!= c2
->type
||
3204 (c1
->type
== AC_IR_CONST
&&
3205 c1
->const_float
!= c2
->const_float
) ||
3206 (c1
->type
== AC_IR_VALUE
&&
3207 c1
->value
!= c2
->value
)) {
3215 copy_back_channels
= 0;
3217 if (p
== processed
->num
)
3220 /* If a match was found, but the matching export has undef where the new
3221 * one has a normal value, copy the normal value to the undef channel.
3223 struct ac_vs_exp_inst
*match
= &processed
->exp
[p
];
3225 /* Get current enabled channels mask. */
3226 LLVMValueRef arg
= LLVMGetOperand(match
->inst
, AC_EXP_ENABLED_CHANNELS
);
3227 unsigned enabled_channels
= LLVMConstIntGetZExtValue(arg
);
3229 while (copy_back_channels
) {
3230 unsigned chan
= u_bit_scan(©_back_channels
);
3232 assert(match
->chan
[chan
].type
== AC_IR_UNDEF
);
3233 LLVMSetOperand(match
->inst
, AC_EXP_OUT0
+ chan
,
3234 exp
->chan
[chan
].value
);
3235 match
->chan
[chan
] = exp
->chan
[chan
];
3237 /* Update number of enabled channels because the original mask
3238 * is not always 0xf.
3240 enabled_channels
|= (1 << chan
);
3241 LLVMSetOperand(match
->inst
, AC_EXP_ENABLED_CHANNELS
,
3242 LLVMConstInt(ctx
->i32
, enabled_channels
, 0));
3245 /* The PARAM export is duplicated. Kill it. */
3246 LLVMInstructionEraseFromParent(exp
->inst
);
3248 /* Change OFFSET to the matching export. */
3249 for (unsigned i
= 0; i
< num_outputs
; i
++) {
3250 if (vs_output_param_offset
[i
] == exp
->offset
) {
3251 vs_output_param_offset
[i
] = match
->offset
;
3258 void ac_optimize_vs_outputs(struct ac_llvm_context
*ctx
,
3259 LLVMValueRef main_fn
,
3260 uint8_t *vs_output_param_offset
,
3261 uint32_t num_outputs
,
3262 uint8_t *num_param_exports
)
3264 LLVMBasicBlockRef bb
;
3265 bool removed_any
= false;
3266 struct ac_vs_exports exports
;
3270 /* Process all LLVM instructions. */
3271 bb
= LLVMGetFirstBasicBlock(main_fn
);
3273 LLVMValueRef inst
= LLVMGetFirstInstruction(bb
);
3276 LLVMValueRef cur
= inst
;
3277 inst
= LLVMGetNextInstruction(inst
);
3278 struct ac_vs_exp_inst exp
;
3280 if (LLVMGetInstructionOpcode(cur
) != LLVMCall
)
3283 LLVMValueRef callee
= ac_llvm_get_called_value(cur
);
3285 if (!ac_llvm_is_function(callee
))
3288 const char *name
= LLVMGetValueName(callee
);
3289 unsigned num_args
= LLVMCountParams(callee
);
3291 /* Check if this is an export instruction. */
3292 if ((num_args
!= 9 && num_args
!= 8) ||
3293 (strcmp(name
, "llvm.SI.export") &&
3294 strcmp(name
, "llvm.amdgcn.exp.f32")))
3297 LLVMValueRef arg
= LLVMGetOperand(cur
, AC_EXP_TARGET
);
3298 unsigned target
= LLVMConstIntGetZExtValue(arg
);
3300 if (target
< V_008DFC_SQ_EXP_PARAM
)
3303 target
-= V_008DFC_SQ_EXP_PARAM
;
3305 /* Parse the instruction. */
3306 memset(&exp
, 0, sizeof(exp
));
3307 exp
.offset
= target
;
3310 for (unsigned i
= 0; i
< 4; i
++) {
3311 LLVMValueRef v
= LLVMGetOperand(cur
, AC_EXP_OUT0
+ i
);
3313 exp
.chan
[i
].value
= v
;
3315 if (LLVMIsUndef(v
)) {
3316 exp
.chan
[i
].type
= AC_IR_UNDEF
;
3317 } else if (LLVMIsAConstantFP(v
)) {
3318 LLVMBool loses_info
;
3319 exp
.chan
[i
].type
= AC_IR_CONST
;
3320 exp
.chan
[i
].const_float
=
3321 LLVMConstRealGetDouble(v
, &loses_info
);
3323 exp
.chan
[i
].type
= AC_IR_VALUE
;
3327 /* Eliminate constant and duplicated PARAM exports. */
3328 if (ac_eliminate_const_output(vs_output_param_offset
,
3329 num_outputs
, &exp
) ||
3330 ac_eliminate_duplicated_output(ctx
,
3331 vs_output_param_offset
,
3332 num_outputs
, &exports
,
3336 exports
.exp
[exports
.num
++] = exp
;
3339 bb
= LLVMGetNextBasicBlock(bb
);
3342 /* Remove holes in export memory due to removed PARAM exports.
3343 * This is done by renumbering all PARAM exports.
3346 uint8_t old_offset
[VARYING_SLOT_MAX
];
3349 /* Make a copy of the offsets. We need the old version while
3350 * we are modifying some of them. */
3351 memcpy(old_offset
, vs_output_param_offset
,
3352 sizeof(old_offset
));
3354 for (i
= 0; i
< exports
.num
; i
++) {
3355 unsigned offset
= exports
.exp
[i
].offset
;
3357 /* Update vs_output_param_offset. Multiple outputs can
3358 * have the same offset.
3360 for (out
= 0; out
< num_outputs
; out
++) {
3361 if (old_offset
[out
] == offset
)
3362 vs_output_param_offset
[out
] = i
;
3365 /* Change the PARAM offset in the instruction. */
3366 LLVMSetOperand(exports
.exp
[i
].inst
, AC_EXP_TARGET
,
3367 LLVMConstInt(ctx
->i32
,
3368 V_008DFC_SQ_EXP_PARAM
+ i
, 0));
3370 *num_param_exports
= exports
.num
;
3374 void ac_init_exec_full_mask(struct ac_llvm_context
*ctx
)
3376 LLVMValueRef full_mask
= LLVMConstInt(ctx
->i64
, ~0ull, 0);
3377 ac_build_intrinsic(ctx
,
3378 "llvm.amdgcn.init.exec", ctx
->voidt
,
3379 &full_mask
, 1, AC_FUNC_ATTR_CONVERGENT
);
3382 void ac_declare_lds_as_pointer(struct ac_llvm_context
*ctx
)
3384 unsigned lds_size
= ctx
->chip_class
>= GFX7
? 65536 : 32768;
3385 ctx
->lds
= LLVMBuildIntToPtr(ctx
->builder
, ctx
->i32_0
,
3386 LLVMPointerType(LLVMArrayType(ctx
->i32
, lds_size
/ 4), AC_ADDR_SPACE_LDS
),
3390 LLVMValueRef
ac_lds_load(struct ac_llvm_context
*ctx
,
3391 LLVMValueRef dw_addr
)
3393 return LLVMBuildLoad(ctx
->builder
, ac_build_gep0(ctx
, ctx
->lds
, dw_addr
), "");
3396 void ac_lds_store(struct ac_llvm_context
*ctx
,
3397 LLVMValueRef dw_addr
,
3400 value
= ac_to_integer(ctx
, value
);
3401 ac_build_indexed_store(ctx
, ctx
->lds
,
3405 LLVMValueRef
ac_find_lsb(struct ac_llvm_context
*ctx
,
3406 LLVMTypeRef dst_type
,
3409 unsigned src0_bitsize
= ac_get_elem_bits(ctx
, LLVMTypeOf(src0
));
3410 const char *intrin_name
;
3414 switch (src0_bitsize
) {
3416 intrin_name
= "llvm.cttz.i64";
3421 intrin_name
= "llvm.cttz.i32";
3426 intrin_name
= "llvm.cttz.i16";
3431 intrin_name
= "llvm.cttz.i8";
3436 unreachable(!"invalid bitsize");
3439 LLVMValueRef params
[2] = {
3442 /* The value of 1 means that ffs(x=0) = undef, so LLVM won't
3443 * add special code to check for x=0. The reason is that
3444 * the LLVM behavior for x=0 is different from what we
3445 * need here. However, LLVM also assumes that ffs(x) is
3446 * in [0, 31], but GLSL expects that ffs(0) = -1, so
3447 * a conditional assignment to handle 0 is still required.
3449 * The hardware already implements the correct behavior.
3454 LLVMValueRef lsb
= ac_build_intrinsic(ctx
, intrin_name
, type
,
3456 AC_FUNC_ATTR_READNONE
);
3458 if (src0_bitsize
== 64) {
3459 lsb
= LLVMBuildTrunc(ctx
->builder
, lsb
, ctx
->i32
, "");
3460 } else if (src0_bitsize
< 32) {
3461 lsb
= LLVMBuildSExt(ctx
->builder
, lsb
, ctx
->i32
, "");
3464 /* TODO: We need an intrinsic to skip this conditional. */
3465 /* Check for zero: */
3466 return LLVMBuildSelect(ctx
->builder
, LLVMBuildICmp(ctx
->builder
,
3469 LLVMConstInt(ctx
->i32
, -1, 0), lsb
, "");
3472 LLVMTypeRef
ac_array_in_const_addr_space(LLVMTypeRef elem_type
)
3474 return LLVMPointerType(elem_type
, AC_ADDR_SPACE_CONST
);
3477 LLVMTypeRef
ac_array_in_const32_addr_space(LLVMTypeRef elem_type
)
3479 return LLVMPointerType(elem_type
, AC_ADDR_SPACE_CONST_32BIT
);
3482 static struct ac_llvm_flow
*
3483 get_current_flow(struct ac_llvm_context
*ctx
)
3485 if (ctx
->flow_depth
> 0)
3486 return &ctx
->flow
[ctx
->flow_depth
- 1];
3490 static struct ac_llvm_flow
*
3491 get_innermost_loop(struct ac_llvm_context
*ctx
)
3493 for (unsigned i
= ctx
->flow_depth
; i
> 0; --i
) {
3494 if (ctx
->flow
[i
- 1].loop_entry_block
)
3495 return &ctx
->flow
[i
- 1];
3500 static struct ac_llvm_flow
*
3501 push_flow(struct ac_llvm_context
*ctx
)
3503 struct ac_llvm_flow
*flow
;
3505 if (ctx
->flow_depth
>= ctx
->flow_depth_max
) {
3506 unsigned new_max
= MAX2(ctx
->flow_depth
<< 1,
3507 AC_LLVM_INITIAL_CF_DEPTH
);
3509 ctx
->flow
= realloc(ctx
->flow
, new_max
* sizeof(*ctx
->flow
));
3510 ctx
->flow_depth_max
= new_max
;
3513 flow
= &ctx
->flow
[ctx
->flow_depth
];
3516 flow
->next_block
= NULL
;
3517 flow
->loop_entry_block
= NULL
;
3521 static void set_basicblock_name(LLVMBasicBlockRef bb
, const char *base
,
3525 snprintf(buf
, sizeof(buf
), "%s%d", base
, label_id
);
3526 LLVMSetValueName(LLVMBasicBlockAsValue(bb
), buf
);
3529 /* Append a basic block at the level of the parent flow.
3531 static LLVMBasicBlockRef
append_basic_block(struct ac_llvm_context
*ctx
,
3534 assert(ctx
->flow_depth
>= 1);
3536 if (ctx
->flow_depth
>= 2) {
3537 struct ac_llvm_flow
*flow
= &ctx
->flow
[ctx
->flow_depth
- 2];
3539 return LLVMInsertBasicBlockInContext(ctx
->context
,
3540 flow
->next_block
, name
);
3543 LLVMValueRef main_fn
=
3544 LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
->builder
));
3545 return LLVMAppendBasicBlockInContext(ctx
->context
, main_fn
, name
);
3548 /* Emit a branch to the given default target for the current block if
3549 * applicable -- that is, if the current block does not already contain a
3550 * branch from a break or continue.
3552 static void emit_default_branch(LLVMBuilderRef builder
,
3553 LLVMBasicBlockRef target
)
3555 if (!LLVMGetBasicBlockTerminator(LLVMGetInsertBlock(builder
)))
3556 LLVMBuildBr(builder
, target
);
3559 void ac_build_bgnloop(struct ac_llvm_context
*ctx
, int label_id
)
3561 struct ac_llvm_flow
*flow
= push_flow(ctx
);
3562 flow
->loop_entry_block
= append_basic_block(ctx
, "LOOP");
3563 flow
->next_block
= append_basic_block(ctx
, "ENDLOOP");
3564 set_basicblock_name(flow
->loop_entry_block
, "loop", label_id
);
3565 LLVMBuildBr(ctx
->builder
, flow
->loop_entry_block
);
3566 LLVMPositionBuilderAtEnd(ctx
->builder
, flow
->loop_entry_block
);
3569 void ac_build_break(struct ac_llvm_context
*ctx
)
3571 struct ac_llvm_flow
*flow
= get_innermost_loop(ctx
);
3572 LLVMBuildBr(ctx
->builder
, flow
->next_block
);
3575 void ac_build_continue(struct ac_llvm_context
*ctx
)
3577 struct ac_llvm_flow
*flow
= get_innermost_loop(ctx
);
3578 LLVMBuildBr(ctx
->builder
, flow
->loop_entry_block
);
3581 void ac_build_else(struct ac_llvm_context
*ctx
, int label_id
)
3583 struct ac_llvm_flow
*current_branch
= get_current_flow(ctx
);
3584 LLVMBasicBlockRef endif_block
;
3586 assert(!current_branch
->loop_entry_block
);
3588 endif_block
= append_basic_block(ctx
, "ENDIF");
3589 emit_default_branch(ctx
->builder
, endif_block
);
3591 LLVMPositionBuilderAtEnd(ctx
->builder
, current_branch
->next_block
);
3592 set_basicblock_name(current_branch
->next_block
, "else", label_id
);
3594 current_branch
->next_block
= endif_block
;
3597 void ac_build_endif(struct ac_llvm_context
*ctx
, int label_id
)
3599 struct ac_llvm_flow
*current_branch
= get_current_flow(ctx
);
3601 assert(!current_branch
->loop_entry_block
);
3603 emit_default_branch(ctx
->builder
, current_branch
->next_block
);
3604 LLVMPositionBuilderAtEnd(ctx
->builder
, current_branch
->next_block
);
3605 set_basicblock_name(current_branch
->next_block
, "endif", label_id
);
3610 void ac_build_endloop(struct ac_llvm_context
*ctx
, int label_id
)
3612 struct ac_llvm_flow
*current_loop
= get_current_flow(ctx
);
3614 assert(current_loop
->loop_entry_block
);
3616 emit_default_branch(ctx
->builder
, current_loop
->loop_entry_block
);
3618 LLVMPositionBuilderAtEnd(ctx
->builder
, current_loop
->next_block
);
3619 set_basicblock_name(current_loop
->next_block
, "endloop", label_id
);
3623 void ac_build_ifcc(struct ac_llvm_context
*ctx
, LLVMValueRef cond
, int label_id
)
3625 struct ac_llvm_flow
*flow
= push_flow(ctx
);
3626 LLVMBasicBlockRef if_block
;
3628 if_block
= append_basic_block(ctx
, "IF");
3629 flow
->next_block
= append_basic_block(ctx
, "ELSE");
3630 set_basicblock_name(if_block
, "if", label_id
);
3631 LLVMBuildCondBr(ctx
->builder
, cond
, if_block
, flow
->next_block
);
3632 LLVMPositionBuilderAtEnd(ctx
->builder
, if_block
);
3635 void ac_build_if(struct ac_llvm_context
*ctx
, LLVMValueRef value
,
3638 LLVMValueRef cond
= LLVMBuildFCmp(ctx
->builder
, LLVMRealUNE
,
3639 value
, ctx
->f32_0
, "");
3640 ac_build_ifcc(ctx
, cond
, label_id
);
3643 void ac_build_uif(struct ac_llvm_context
*ctx
, LLVMValueRef value
,
3646 LLVMValueRef cond
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
3647 ac_to_integer(ctx
, value
),
3649 ac_build_ifcc(ctx
, cond
, label_id
);
3652 LLVMValueRef
ac_build_alloca_undef(struct ac_llvm_context
*ac
, LLVMTypeRef type
,
3655 LLVMBuilderRef builder
= ac
->builder
;
3656 LLVMBasicBlockRef current_block
= LLVMGetInsertBlock(builder
);
3657 LLVMValueRef function
= LLVMGetBasicBlockParent(current_block
);
3658 LLVMBasicBlockRef first_block
= LLVMGetEntryBasicBlock(function
);
3659 LLVMValueRef first_instr
= LLVMGetFirstInstruction(first_block
);
3660 LLVMBuilderRef first_builder
= LLVMCreateBuilderInContext(ac
->context
);
3664 LLVMPositionBuilderBefore(first_builder
, first_instr
);
3666 LLVMPositionBuilderAtEnd(first_builder
, first_block
);
3669 res
= LLVMBuildAlloca(first_builder
, type
, name
);
3670 LLVMDisposeBuilder(first_builder
);
3674 LLVMValueRef
ac_build_alloca(struct ac_llvm_context
*ac
,
3675 LLVMTypeRef type
, const char *name
)
3677 LLVMValueRef ptr
= ac_build_alloca_undef(ac
, type
, name
);
3678 LLVMBuildStore(ac
->builder
, LLVMConstNull(type
), ptr
);
3682 LLVMValueRef
ac_cast_ptr(struct ac_llvm_context
*ctx
, LLVMValueRef ptr
,
3685 int addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
3686 return LLVMBuildBitCast(ctx
->builder
, ptr
,
3687 LLVMPointerType(type
, addr_space
), "");
3690 LLVMValueRef
ac_trim_vector(struct ac_llvm_context
*ctx
, LLVMValueRef value
,
3693 unsigned num_components
= ac_get_llvm_num_components(value
);
3694 if (count
== num_components
)
3697 LLVMValueRef masks
[MAX2(count
, 2)];
3698 masks
[0] = ctx
->i32_0
;
3699 masks
[1] = ctx
->i32_1
;
3700 for (unsigned i
= 2; i
< count
; i
++)
3701 masks
[i
] = LLVMConstInt(ctx
->i32
, i
, false);
3704 return LLVMBuildExtractElement(ctx
->builder
, value
, masks
[0],
3707 LLVMValueRef swizzle
= LLVMConstVector(masks
, count
);
3708 return LLVMBuildShuffleVector(ctx
->builder
, value
, value
, swizzle
, "");
3711 LLVMValueRef
ac_unpack_param(struct ac_llvm_context
*ctx
, LLVMValueRef param
,
3712 unsigned rshift
, unsigned bitwidth
)
3714 LLVMValueRef value
= param
;
3716 value
= LLVMBuildLShr(ctx
->builder
, value
,
3717 LLVMConstInt(ctx
->i32
, rshift
, false), "");
3719 if (rshift
+ bitwidth
< 32) {
3720 unsigned mask
= (1 << bitwidth
) - 1;
3721 value
= LLVMBuildAnd(ctx
->builder
, value
,
3722 LLVMConstInt(ctx
->i32
, mask
, false), "");
3727 /* Adjust the sample index according to FMASK.
3729 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
3730 * which is the identity mapping. Each nibble says which physical sample
3731 * should be fetched to get that sample.
3733 * For example, 0x11111100 means there are only 2 samples stored and
3734 * the second sample covers 3/4 of the pixel. When reading samples 0
3735 * and 1, return physical sample 0 (determined by the first two 0s
3736 * in FMASK), otherwise return physical sample 1.
3738 * The sample index should be adjusted as follows:
3739 * addr[sample_index] = (fmask >> (addr[sample_index] * 4)) & 0xF;
3741 void ac_apply_fmask_to_sample(struct ac_llvm_context
*ac
, LLVMValueRef fmask
,
3742 LLVMValueRef
*addr
, bool is_array_tex
)
3744 struct ac_image_args fmask_load
= {};
3745 fmask_load
.opcode
= ac_image_load
;
3746 fmask_load
.resource
= fmask
;
3747 fmask_load
.dmask
= 0xf;
3748 fmask_load
.dim
= is_array_tex
? ac_image_2darray
: ac_image_2d
;
3749 fmask_load
.attributes
= AC_FUNC_ATTR_READNONE
;
3751 fmask_load
.coords
[0] = addr
[0];
3752 fmask_load
.coords
[1] = addr
[1];
3754 fmask_load
.coords
[2] = addr
[2];
3756 LLVMValueRef fmask_value
= ac_build_image_opcode(ac
, &fmask_load
);
3757 fmask_value
= LLVMBuildExtractElement(ac
->builder
, fmask_value
,
3760 /* Apply the formula. */
3761 unsigned sample_chan
= is_array_tex
? 3 : 2;
3762 LLVMValueRef final_sample
;
3763 final_sample
= LLVMBuildMul(ac
->builder
, addr
[sample_chan
],
3764 LLVMConstInt(ac
->i32
, 4, 0), "");
3765 final_sample
= LLVMBuildLShr(ac
->builder
, fmask_value
, final_sample
, "");
3766 /* Mask the sample index by 0x7, because 0x8 means an unknown value
3767 * with EQAA, so those will map to 0. */
3768 final_sample
= LLVMBuildAnd(ac
->builder
, final_sample
,
3769 LLVMConstInt(ac
->i32
, 0x7, 0), "");
3771 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
3772 * resource descriptor is 0 (invalid).
3775 tmp
= LLVMBuildBitCast(ac
->builder
, fmask
, ac
->v8i32
, "");
3776 tmp
= LLVMBuildExtractElement(ac
->builder
, tmp
, ac
->i32_1
, "");
3777 tmp
= LLVMBuildICmp(ac
->builder
, LLVMIntNE
, tmp
, ac
->i32_0
, "");
3779 /* Replace the MSAA sample index. */
3780 addr
[sample_chan
] = LLVMBuildSelect(ac
->builder
, tmp
, final_sample
,
3781 addr
[sample_chan
], "");
3785 _ac_build_readlane(struct ac_llvm_context
*ctx
, LLVMValueRef src
, LLVMValueRef lane
)
3787 ac_build_optimization_barrier(ctx
, &src
);
3788 return ac_build_intrinsic(ctx
,
3789 lane
== NULL
? "llvm.amdgcn.readfirstlane" : "llvm.amdgcn.readlane",
3790 LLVMTypeOf(src
), (LLVMValueRef
[]) {
3792 lane
== NULL
? 1 : 2,
3793 AC_FUNC_ATTR_READNONE
|
3794 AC_FUNC_ATTR_CONVERGENT
);
3798 * Builds the "llvm.amdgcn.readlane" or "llvm.amdgcn.readfirstlane" intrinsic.
3801 * @param lane - id of the lane or NULL for the first active lane
3802 * @return value of the lane
3805 ac_build_readlane(struct ac_llvm_context
*ctx
, LLVMValueRef src
, LLVMValueRef lane
)
3807 LLVMTypeRef src_type
= LLVMTypeOf(src
);
3808 src
= ac_to_integer(ctx
, src
);
3809 unsigned bits
= LLVMGetIntTypeWidth(LLVMTypeOf(src
));
3813 ret
= _ac_build_readlane(ctx
, src
, lane
);
3815 assert(bits
% 32 == 0);
3816 LLVMTypeRef vec_type
= LLVMVectorType(ctx
->i32
, bits
/ 32);
3817 LLVMValueRef src_vector
=
3818 LLVMBuildBitCast(ctx
->builder
, src
, vec_type
, "");
3819 ret
= LLVMGetUndef(vec_type
);
3820 for (unsigned i
= 0; i
< bits
/ 32; i
++) {
3821 src
= LLVMBuildExtractElement(ctx
->builder
, src_vector
,
3822 LLVMConstInt(ctx
->i32
, i
, 0), "");
3823 LLVMValueRef ret_comp
= _ac_build_readlane(ctx
, src
, lane
);
3824 ret
= LLVMBuildInsertElement(ctx
->builder
, ret
, ret_comp
,
3825 LLVMConstInt(ctx
->i32
, i
, 0), "");
3828 return LLVMBuildBitCast(ctx
->builder
, ret
, src_type
, "");
3832 ac_build_writelane(struct ac_llvm_context
*ctx
, LLVMValueRef src
, LLVMValueRef value
, LLVMValueRef lane
)
3834 if (HAVE_LLVM
>= 0x0800) {
3835 return ac_build_intrinsic(ctx
, "llvm.amdgcn.writelane", ctx
->i32
,
3836 (LLVMValueRef
[]) {value
, lane
, src
}, 3,
3837 AC_FUNC_ATTR_READNONE
| AC_FUNC_ATTR_CONVERGENT
);
3840 LLVMValueRef pred
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, lane
,
3841 ac_get_thread_id(ctx
), "");
3842 return LLVMBuildSelect(ctx
->builder
, pred
, value
, src
, "");
3846 ac_build_mbcnt(struct ac_llvm_context
*ctx
, LLVMValueRef mask
)
3848 LLVMValueRef mask_vec
= LLVMBuildBitCast(ctx
->builder
, mask
,
3849 LLVMVectorType(ctx
->i32
, 2),
3851 LLVMValueRef mask_lo
= LLVMBuildExtractElement(ctx
->builder
, mask_vec
,
3853 LLVMValueRef mask_hi
= LLVMBuildExtractElement(ctx
->builder
, mask_vec
,
3856 ac_build_intrinsic(ctx
, "llvm.amdgcn.mbcnt.lo", ctx
->i32
,
3857 (LLVMValueRef
[]) { mask_lo
, ctx
->i32_0
},
3858 2, AC_FUNC_ATTR_READNONE
);
3859 val
= ac_build_intrinsic(ctx
, "llvm.amdgcn.mbcnt.hi", ctx
->i32
,
3860 (LLVMValueRef
[]) { mask_hi
, val
},
3861 2, AC_FUNC_ATTR_READNONE
);
3866 _dpp_quad_perm
= 0x000,
3867 _dpp_row_sl
= 0x100,
3868 _dpp_row_sr
= 0x110,
3869 _dpp_row_rr
= 0x120,
3874 dpp_row_mirror
= 0x140,
3875 dpp_row_half_mirror
= 0x141,
3876 dpp_row_bcast15
= 0x142,
3877 dpp_row_bcast31
= 0x143
3880 static inline enum dpp_ctrl
3881 dpp_quad_perm(unsigned lane0
, unsigned lane1
, unsigned lane2
, unsigned lane3
)
3883 assert(lane0
< 4 && lane1
< 4 && lane2
< 4 && lane3
< 4);
3884 return _dpp_quad_perm
| lane0
| (lane1
<< 2) | (lane2
<< 4) | (lane3
<< 6);
3887 static inline enum dpp_ctrl
3888 dpp_row_sl(unsigned amount
)
3890 assert(amount
> 0 && amount
< 16);
3891 return _dpp_row_sl
| amount
;
3894 static inline enum dpp_ctrl
3895 dpp_row_sr(unsigned amount
)
3897 assert(amount
> 0 && amount
< 16);
3898 return _dpp_row_sr
| amount
;
3902 _ac_build_dpp(struct ac_llvm_context
*ctx
, LLVMValueRef old
, LLVMValueRef src
,
3903 enum dpp_ctrl dpp_ctrl
, unsigned row_mask
, unsigned bank_mask
,
3906 return ac_build_intrinsic(ctx
, "llvm.amdgcn.update.dpp.i32",
3910 LLVMConstInt(ctx
->i32
, dpp_ctrl
, 0),
3911 LLVMConstInt(ctx
->i32
, row_mask
, 0),
3912 LLVMConstInt(ctx
->i32
, bank_mask
, 0),
3913 LLVMConstInt(ctx
->i1
, bound_ctrl
, 0) },
3914 6, AC_FUNC_ATTR_READNONE
| AC_FUNC_ATTR_CONVERGENT
);
3918 ac_build_dpp(struct ac_llvm_context
*ctx
, LLVMValueRef old
, LLVMValueRef src
,
3919 enum dpp_ctrl dpp_ctrl
, unsigned row_mask
, unsigned bank_mask
,
3922 LLVMTypeRef src_type
= LLVMTypeOf(src
);
3923 src
= ac_to_integer(ctx
, src
);
3924 old
= ac_to_integer(ctx
, old
);
3925 unsigned bits
= LLVMGetIntTypeWidth(LLVMTypeOf(src
));
3928 ret
= _ac_build_dpp(ctx
, old
, src
, dpp_ctrl
, row_mask
,
3929 bank_mask
, bound_ctrl
);
3931 assert(bits
% 32 == 0);
3932 LLVMTypeRef vec_type
= LLVMVectorType(ctx
->i32
, bits
/ 32);
3933 LLVMValueRef src_vector
=
3934 LLVMBuildBitCast(ctx
->builder
, src
, vec_type
, "");
3935 LLVMValueRef old_vector
=
3936 LLVMBuildBitCast(ctx
->builder
, old
, vec_type
, "");
3937 ret
= LLVMGetUndef(vec_type
);
3938 for (unsigned i
= 0; i
< bits
/ 32; i
++) {
3939 src
= LLVMBuildExtractElement(ctx
->builder
, src_vector
,
3940 LLVMConstInt(ctx
->i32
, i
,
3942 old
= LLVMBuildExtractElement(ctx
->builder
, old_vector
,
3943 LLVMConstInt(ctx
->i32
, i
,
3945 LLVMValueRef ret_comp
= _ac_build_dpp(ctx
, old
, src
,
3950 ret
= LLVMBuildInsertElement(ctx
->builder
, ret
,
3952 LLVMConstInt(ctx
->i32
, i
,
3956 return LLVMBuildBitCast(ctx
->builder
, ret
, src_type
, "");
3960 _ac_build_permlane16(struct ac_llvm_context
*ctx
, LLVMValueRef src
, uint64_t sel
,
3961 bool exchange_rows
, bool bound_ctrl
)
3963 LLVMValueRef args
[6] = {
3966 LLVMConstInt(ctx
->i32
, sel
, false),
3967 LLVMConstInt(ctx
->i32
, sel
>> 32, false),
3968 ctx
->i1true
, /* fi */
3969 bound_ctrl
? ctx
->i1true
: ctx
->i1false
,
3971 return ac_build_intrinsic(ctx
, exchange_rows
? "llvm.amdgcn.permlanex16"
3972 : "llvm.amdgcn.permlane16",
3974 AC_FUNC_ATTR_READNONE
| AC_FUNC_ATTR_CONVERGENT
);
3978 ac_build_permlane16(struct ac_llvm_context
*ctx
, LLVMValueRef src
, uint64_t sel
,
3979 bool exchange_rows
, bool bound_ctrl
)
3981 LLVMTypeRef src_type
= LLVMTypeOf(src
);
3982 src
= ac_to_integer(ctx
, src
);
3983 unsigned bits
= LLVMGetIntTypeWidth(LLVMTypeOf(src
));
3986 ret
= _ac_build_permlane16(ctx
, src
, sel
, exchange_rows
,
3989 assert(bits
% 32 == 0);
3990 LLVMTypeRef vec_type
= LLVMVectorType(ctx
->i32
, bits
/ 32);
3991 LLVMValueRef src_vector
=
3992 LLVMBuildBitCast(ctx
->builder
, src
, vec_type
, "");
3993 ret
= LLVMGetUndef(vec_type
);
3994 for (unsigned i
= 0; i
< bits
/ 32; i
++) {
3995 src
= LLVMBuildExtractElement(ctx
->builder
, src_vector
,
3996 LLVMConstInt(ctx
->i32
, i
,
3998 LLVMValueRef ret_comp
=
3999 _ac_build_permlane16(ctx
, src
, sel
,
4002 ret
= LLVMBuildInsertElement(ctx
->builder
, ret
,
4004 LLVMConstInt(ctx
->i32
, i
,
4008 return LLVMBuildBitCast(ctx
->builder
, ret
, src_type
, "");
4011 static inline unsigned
4012 ds_pattern_bitmode(unsigned and_mask
, unsigned or_mask
, unsigned xor_mask
)
4014 assert(and_mask
< 32 && or_mask
< 32 && xor_mask
< 32);
4015 return and_mask
| (or_mask
<< 5) | (xor_mask
<< 10);
4019 _ac_build_ds_swizzle(struct ac_llvm_context
*ctx
, LLVMValueRef src
, unsigned mask
)
4021 return ac_build_intrinsic(ctx
, "llvm.amdgcn.ds.swizzle",
4022 LLVMTypeOf(src
), (LLVMValueRef
[]) {
4023 src
, LLVMConstInt(ctx
->i32
, mask
, 0) },
4024 2, AC_FUNC_ATTR_READNONE
| AC_FUNC_ATTR_CONVERGENT
);
4028 ac_build_ds_swizzle(struct ac_llvm_context
*ctx
, LLVMValueRef src
, unsigned mask
)
4030 LLVMTypeRef src_type
= LLVMTypeOf(src
);
4031 src
= ac_to_integer(ctx
, src
);
4032 unsigned bits
= LLVMGetIntTypeWidth(LLVMTypeOf(src
));
4035 ret
= _ac_build_ds_swizzle(ctx
, src
, mask
);
4037 assert(bits
% 32 == 0);
4038 LLVMTypeRef vec_type
= LLVMVectorType(ctx
->i32
, bits
/ 32);
4039 LLVMValueRef src_vector
=
4040 LLVMBuildBitCast(ctx
->builder
, src
, vec_type
, "");
4041 ret
= LLVMGetUndef(vec_type
);
4042 for (unsigned i
= 0; i
< bits
/ 32; i
++) {
4043 src
= LLVMBuildExtractElement(ctx
->builder
, src_vector
,
4044 LLVMConstInt(ctx
->i32
, i
,
4046 LLVMValueRef ret_comp
= _ac_build_ds_swizzle(ctx
, src
,
4048 ret
= LLVMBuildInsertElement(ctx
->builder
, ret
,
4050 LLVMConstInt(ctx
->i32
, i
,
4054 return LLVMBuildBitCast(ctx
->builder
, ret
, src_type
, "");
4058 ac_build_wwm(struct ac_llvm_context
*ctx
, LLVMValueRef src
)
4060 char name
[32], type
[8];
4061 ac_build_type_name_for_intr(LLVMTypeOf(src
), type
, sizeof(type
));
4062 snprintf(name
, sizeof(name
), "llvm.amdgcn.wwm.%s", type
);
4063 return ac_build_intrinsic(ctx
, name
, LLVMTypeOf(src
),
4064 (LLVMValueRef
[]) { src
}, 1,
4065 AC_FUNC_ATTR_READNONE
);
4069 ac_build_set_inactive(struct ac_llvm_context
*ctx
, LLVMValueRef src
,
4070 LLVMValueRef inactive
)
4072 char name
[33], type
[8];
4073 LLVMTypeRef src_type
= LLVMTypeOf(src
);
4074 src
= ac_to_integer(ctx
, src
);
4075 inactive
= ac_to_integer(ctx
, inactive
);
4076 ac_build_type_name_for_intr(LLVMTypeOf(src
), type
, sizeof(type
));
4077 snprintf(name
, sizeof(name
), "llvm.amdgcn.set.inactive.%s", type
);
4079 ac_build_intrinsic(ctx
, name
,
4080 LLVMTypeOf(src
), (LLVMValueRef
[]) {
4082 AC_FUNC_ATTR_READNONE
|
4083 AC_FUNC_ATTR_CONVERGENT
);
4084 return LLVMBuildBitCast(ctx
->builder
, ret
, src_type
, "");
4088 get_reduction_identity(struct ac_llvm_context
*ctx
, nir_op op
, unsigned type_size
)
4090 if (type_size
== 4) {
4092 case nir_op_iadd
: return ctx
->i32_0
;
4093 case nir_op_fadd
: return ctx
->f32_0
;
4094 case nir_op_imul
: return ctx
->i32_1
;
4095 case nir_op_fmul
: return ctx
->f32_1
;
4096 case nir_op_imin
: return LLVMConstInt(ctx
->i32
, INT32_MAX
, 0);
4097 case nir_op_umin
: return LLVMConstInt(ctx
->i32
, UINT32_MAX
, 0);
4098 case nir_op_fmin
: return LLVMConstReal(ctx
->f32
, INFINITY
);
4099 case nir_op_imax
: return LLVMConstInt(ctx
->i32
, INT32_MIN
, 0);
4100 case nir_op_umax
: return ctx
->i32_0
;
4101 case nir_op_fmax
: return LLVMConstReal(ctx
->f32
, -INFINITY
);
4102 case nir_op_iand
: return LLVMConstInt(ctx
->i32
, -1, 0);
4103 case nir_op_ior
: return ctx
->i32_0
;
4104 case nir_op_ixor
: return ctx
->i32_0
;
4106 unreachable("bad reduction intrinsic");
4108 } else { /* type_size == 64bit */
4110 case nir_op_iadd
: return ctx
->i64_0
;
4111 case nir_op_fadd
: return ctx
->f64_0
;
4112 case nir_op_imul
: return ctx
->i64_1
;
4113 case nir_op_fmul
: return ctx
->f64_1
;
4114 case nir_op_imin
: return LLVMConstInt(ctx
->i64
, INT64_MAX
, 0);
4115 case nir_op_umin
: return LLVMConstInt(ctx
->i64
, UINT64_MAX
, 0);
4116 case nir_op_fmin
: return LLVMConstReal(ctx
->f64
, INFINITY
);
4117 case nir_op_imax
: return LLVMConstInt(ctx
->i64
, INT64_MIN
, 0);
4118 case nir_op_umax
: return ctx
->i64_0
;
4119 case nir_op_fmax
: return LLVMConstReal(ctx
->f64
, -INFINITY
);
4120 case nir_op_iand
: return LLVMConstInt(ctx
->i64
, -1, 0);
4121 case nir_op_ior
: return ctx
->i64_0
;
4122 case nir_op_ixor
: return ctx
->i64_0
;
4124 unreachable("bad reduction intrinsic");
4130 ac_build_alu_op(struct ac_llvm_context
*ctx
, LLVMValueRef lhs
, LLVMValueRef rhs
, nir_op op
)
4132 bool _64bit
= ac_get_type_size(LLVMTypeOf(lhs
)) == 8;
4134 case nir_op_iadd
: return LLVMBuildAdd(ctx
->builder
, lhs
, rhs
, "");
4135 case nir_op_fadd
: return LLVMBuildFAdd(ctx
->builder
, lhs
, rhs
, "");
4136 case nir_op_imul
: return LLVMBuildMul(ctx
->builder
, lhs
, rhs
, "");
4137 case nir_op_fmul
: return LLVMBuildFMul(ctx
->builder
, lhs
, rhs
, "");
4138 case nir_op_imin
: return LLVMBuildSelect(ctx
->builder
,
4139 LLVMBuildICmp(ctx
->builder
, LLVMIntSLT
, lhs
, rhs
, ""),
4141 case nir_op_umin
: return LLVMBuildSelect(ctx
->builder
,
4142 LLVMBuildICmp(ctx
->builder
, LLVMIntULT
, lhs
, rhs
, ""),
4144 case nir_op_fmin
: return ac_build_intrinsic(ctx
,
4145 _64bit
? "llvm.minnum.f64" : "llvm.minnum.f32",
4146 _64bit
? ctx
->f64
: ctx
->f32
,
4147 (LLVMValueRef
[]){lhs
, rhs
}, 2, AC_FUNC_ATTR_READNONE
);
4148 case nir_op_imax
: return LLVMBuildSelect(ctx
->builder
,
4149 LLVMBuildICmp(ctx
->builder
, LLVMIntSGT
, lhs
, rhs
, ""),
4151 case nir_op_umax
: return LLVMBuildSelect(ctx
->builder
,
4152 LLVMBuildICmp(ctx
->builder
, LLVMIntUGT
, lhs
, rhs
, ""),
4154 case nir_op_fmax
: return ac_build_intrinsic(ctx
,
4155 _64bit
? "llvm.maxnum.f64" : "llvm.maxnum.f32",
4156 _64bit
? ctx
->f64
: ctx
->f32
,
4157 (LLVMValueRef
[]){lhs
, rhs
}, 2, AC_FUNC_ATTR_READNONE
);
4158 case nir_op_iand
: return LLVMBuildAnd(ctx
->builder
, lhs
, rhs
, "");
4159 case nir_op_ior
: return LLVMBuildOr(ctx
->builder
, lhs
, rhs
, "");
4160 case nir_op_ixor
: return LLVMBuildXor(ctx
->builder
, lhs
, rhs
, "");
4162 unreachable("bad reduction intrinsic");
4167 * \param maxprefix specifies that the result only needs to be correct for a
4168 * prefix of this many threads
4170 * TODO: add inclusive and excluse scan functions for GFX6.
4173 ac_build_scan(struct ac_llvm_context
*ctx
, nir_op op
, LLVMValueRef src
, LLVMValueRef identity
,
4174 unsigned maxprefix
, bool inclusive
)
4176 LLVMValueRef result
, tmp
;
4178 if (ctx
->chip_class
>= GFX10
) {
4179 result
= inclusive
? src
: identity
;
4184 result
= ac_build_dpp(ctx
, identity
, src
, dpp_wf_sr1
, 0xf, 0xf, false);
4188 tmp
= ac_build_dpp(ctx
, identity
, src
, dpp_row_sr(1), 0xf, 0xf, false);
4189 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
4192 tmp
= ac_build_dpp(ctx
, identity
, src
, dpp_row_sr(2), 0xf, 0xf, false);
4193 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
4196 tmp
= ac_build_dpp(ctx
, identity
, src
, dpp_row_sr(3), 0xf, 0xf, false);
4197 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
4200 tmp
= ac_build_dpp(ctx
, identity
, result
, dpp_row_sr(4), 0xf, 0xe, false);
4201 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
4204 tmp
= ac_build_dpp(ctx
, identity
, result
, dpp_row_sr(8), 0xf, 0xc, false);
4205 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
4206 if (maxprefix
<= 16)
4209 if (ctx
->chip_class
>= GFX10
) {
4210 /* dpp_row_bcast{15,31} are not supported on gfx10. */
4211 LLVMBuilderRef builder
= ctx
->builder
;
4212 LLVMValueRef tid
= ac_get_thread_id(ctx
);
4214 /* TODO-GFX10: Can we get better code-gen by putting this into
4215 * a branch so that LLVM generates EXEC mask manipulations? */
4219 tmp
= ac_build_alu_op(ctx
, result
, src
, op
);
4220 tmp
= ac_build_permlane16(ctx
, tmp
, ~(uint64_t)0, true, false);
4221 tmp
= ac_build_alu_op(ctx
, result
, tmp
, op
);
4222 cc
= LLVMBuildAnd(builder
, tid
, LLVMConstInt(ctx
->i32
, 16, false), "");
4223 cc
= LLVMBuildICmp(builder
, LLVMIntNE
, cc
, ctx
->i32_0
, "");
4224 result
= LLVMBuildSelect(builder
, cc
, tmp
, result
, "");
4225 if (maxprefix
<= 32)
4231 tmp
= ac_build_alu_op(ctx
, result
, src
, op
);
4232 tmp
= ac_build_readlane(ctx
, tmp
, LLVMConstInt(ctx
->i32
, 31, false));
4233 tmp
= ac_build_alu_op(ctx
, result
, tmp
, op
);
4234 cc
= LLVMBuildICmp(builder
, LLVMIntUGE
, tid
,
4235 LLVMConstInt(ctx
->i32
, 32, false), "");
4236 result
= LLVMBuildSelect(builder
, cc
, tmp
, result
, "");
4240 tmp
= ac_build_dpp(ctx
, identity
, result
, dpp_row_bcast15
, 0xa, 0xf, false);
4241 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
4242 if (maxprefix
<= 32)
4244 tmp
= ac_build_dpp(ctx
, identity
, result
, dpp_row_bcast31
, 0xc, 0xf, false);
4245 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
4250 ac_build_inclusive_scan(struct ac_llvm_context
*ctx
, LLVMValueRef src
, nir_op op
)
4252 LLVMValueRef result
;
4254 if (LLVMTypeOf(src
) == ctx
->i1
&& op
== nir_op_iadd
) {
4255 LLVMBuilderRef builder
= ctx
->builder
;
4256 src
= LLVMBuildZExt(builder
, src
, ctx
->i32
, "");
4257 result
= ac_build_ballot(ctx
, src
);
4258 result
= ac_build_mbcnt(ctx
, result
);
4259 result
= LLVMBuildAdd(builder
, result
, src
, "");
4263 ac_build_optimization_barrier(ctx
, &src
);
4265 LLVMValueRef identity
=
4266 get_reduction_identity(ctx
, op
, ac_get_type_size(LLVMTypeOf(src
)));
4267 result
= LLVMBuildBitCast(ctx
->builder
, ac_build_set_inactive(ctx
, src
, identity
),
4268 LLVMTypeOf(identity
), "");
4269 result
= ac_build_scan(ctx
, op
, result
, identity
, ctx
->wave_size
, true);
4271 return ac_build_wwm(ctx
, result
);
4275 ac_build_exclusive_scan(struct ac_llvm_context
*ctx
, LLVMValueRef src
, nir_op op
)
4277 LLVMValueRef result
;
4279 if (LLVMTypeOf(src
) == ctx
->i1
&& op
== nir_op_iadd
) {
4280 LLVMBuilderRef builder
= ctx
->builder
;
4281 src
= LLVMBuildZExt(builder
, src
, ctx
->i32
, "");
4282 result
= ac_build_ballot(ctx
, src
);
4283 result
= ac_build_mbcnt(ctx
, result
);
4287 ac_build_optimization_barrier(ctx
, &src
);
4289 LLVMValueRef identity
=
4290 get_reduction_identity(ctx
, op
, ac_get_type_size(LLVMTypeOf(src
)));
4291 result
= LLVMBuildBitCast(ctx
->builder
, ac_build_set_inactive(ctx
, src
, identity
),
4292 LLVMTypeOf(identity
), "");
4293 result
= ac_build_scan(ctx
, op
, result
, identity
, ctx
->wave_size
, false);
4295 return ac_build_wwm(ctx
, result
);
4299 ac_build_reduce(struct ac_llvm_context
*ctx
, LLVMValueRef src
, nir_op op
, unsigned cluster_size
)
4301 if (cluster_size
== 1) return src
;
4302 ac_build_optimization_barrier(ctx
, &src
);
4303 LLVMValueRef result
, swap
;
4304 LLVMValueRef identity
= get_reduction_identity(ctx
, op
,
4305 ac_get_type_size(LLVMTypeOf(src
)));
4306 result
= LLVMBuildBitCast(ctx
->builder
,
4307 ac_build_set_inactive(ctx
, src
, identity
),
4308 LLVMTypeOf(identity
), "");
4309 swap
= ac_build_quad_swizzle(ctx
, result
, 1, 0, 3, 2);
4310 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
4311 if (cluster_size
== 2) return ac_build_wwm(ctx
, result
);
4313 swap
= ac_build_quad_swizzle(ctx
, result
, 2, 3, 0, 1);
4314 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
4315 if (cluster_size
== 4) return ac_build_wwm(ctx
, result
);
4317 if (ctx
->chip_class
>= GFX8
)
4318 swap
= ac_build_dpp(ctx
, identity
, result
, dpp_row_half_mirror
, 0xf, 0xf, false);
4320 swap
= ac_build_ds_swizzle(ctx
, result
, ds_pattern_bitmode(0x1f, 0, 0x04));
4321 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
4322 if (cluster_size
== 8) return ac_build_wwm(ctx
, result
);
4324 if (ctx
->chip_class
>= GFX8
)
4325 swap
= ac_build_dpp(ctx
, identity
, result
, dpp_row_mirror
, 0xf, 0xf, false);
4327 swap
= ac_build_ds_swizzle(ctx
, result
, ds_pattern_bitmode(0x1f, 0, 0x08));
4328 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
4329 if (cluster_size
== 16) return ac_build_wwm(ctx
, result
);
4331 if (ctx
->chip_class
>= GFX10
)
4332 swap
= ac_build_permlane16(ctx
, result
, 0, true, false);
4333 else if (ctx
->chip_class
>= GFX8
&& cluster_size
!= 32)
4334 swap
= ac_build_dpp(ctx
, identity
, result
, dpp_row_bcast15
, 0xa, 0xf, false);
4336 swap
= ac_build_ds_swizzle(ctx
, result
, ds_pattern_bitmode(0x1f, 0, 0x10));
4337 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
4338 if (cluster_size
== 32) return ac_build_wwm(ctx
, result
);
4340 if (ctx
->chip_class
>= GFX8
) {
4341 if (ctx
->chip_class
>= GFX10
)
4342 swap
= ac_build_readlane(ctx
, result
, LLVMConstInt(ctx
->i32
, 31, false));
4344 swap
= ac_build_dpp(ctx
, identity
, result
, dpp_row_bcast31
, 0xc, 0xf, false);
4345 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
4346 result
= ac_build_readlane(ctx
, result
, LLVMConstInt(ctx
->i32
, 63, 0));
4347 return ac_build_wwm(ctx
, result
);
4349 swap
= ac_build_readlane(ctx
, result
, ctx
->i32_0
);
4350 result
= ac_build_readlane(ctx
, result
, LLVMConstInt(ctx
->i32
, 32, 0));
4351 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
4352 return ac_build_wwm(ctx
, result
);
4357 * "Top half" of a scan that reduces per-wave values across an entire
4360 * The source value must be present in the highest lane of the wave, and the
4361 * highest lane must be live.
4364 ac_build_wg_wavescan_top(struct ac_llvm_context
*ctx
, struct ac_wg_scan
*ws
)
4366 if (ws
->maxwaves
<= 1)
4369 const LLVMValueRef last_lane
= LLVMConstInt(ctx
->i32
, ctx
->wave_size
- 1, false);
4370 LLVMBuilderRef builder
= ctx
->builder
;
4371 LLVMValueRef tid
= ac_get_thread_id(ctx
);
4374 tmp
= LLVMBuildICmp(builder
, LLVMIntEQ
, tid
, last_lane
, "");
4375 ac_build_ifcc(ctx
, tmp
, 1000);
4376 LLVMBuildStore(builder
, ws
->src
, LLVMBuildGEP(builder
, ws
->scratch
, &ws
->waveidx
, 1, ""));
4377 ac_build_endif(ctx
, 1000);
4381 * "Bottom half" of a scan that reduces per-wave values across an entire
4384 * The caller must place a barrier between the top and bottom halves.
4387 ac_build_wg_wavescan_bottom(struct ac_llvm_context
*ctx
, struct ac_wg_scan
*ws
)
4389 const LLVMTypeRef type
= LLVMTypeOf(ws
->src
);
4390 const LLVMValueRef identity
=
4391 get_reduction_identity(ctx
, ws
->op
, ac_get_type_size(type
));
4393 if (ws
->maxwaves
<= 1) {
4394 ws
->result_reduce
= ws
->src
;
4395 ws
->result_inclusive
= ws
->src
;
4396 ws
->result_exclusive
= identity
;
4399 assert(ws
->maxwaves
<= 32);
4401 LLVMBuilderRef builder
= ctx
->builder
;
4402 LLVMValueRef tid
= ac_get_thread_id(ctx
);
4403 LLVMBasicBlockRef bbs
[2];
4404 LLVMValueRef phivalues_scan
[2];
4405 LLVMValueRef tmp
, tmp2
;
4407 bbs
[0] = LLVMGetInsertBlock(builder
);
4408 phivalues_scan
[0] = LLVMGetUndef(type
);
4410 if (ws
->enable_reduce
)
4411 tmp
= LLVMBuildICmp(builder
, LLVMIntULT
, tid
, ws
->numwaves
, "");
4412 else if (ws
->enable_inclusive
)
4413 tmp
= LLVMBuildICmp(builder
, LLVMIntULE
, tid
, ws
->waveidx
, "");
4415 tmp
= LLVMBuildICmp(builder
, LLVMIntULT
, tid
, ws
->waveidx
, "");
4416 ac_build_ifcc(ctx
, tmp
, 1001);
4418 tmp
= LLVMBuildLoad(builder
, LLVMBuildGEP(builder
, ws
->scratch
, &tid
, 1, ""), "");
4420 ac_build_optimization_barrier(ctx
, &tmp
);
4422 bbs
[1] = LLVMGetInsertBlock(builder
);
4423 phivalues_scan
[1] = ac_build_scan(ctx
, ws
->op
, tmp
, identity
, ws
->maxwaves
, true);
4425 ac_build_endif(ctx
, 1001);
4427 const LLVMValueRef scan
= ac_build_phi(ctx
, type
, 2, phivalues_scan
, bbs
);
4429 if (ws
->enable_reduce
) {
4430 tmp
= LLVMBuildSub(builder
, ws
->numwaves
, ctx
->i32_1
, "");
4431 ws
->result_reduce
= ac_build_readlane(ctx
, scan
, tmp
);
4433 if (ws
->enable_inclusive
)
4434 ws
->result_inclusive
= ac_build_readlane(ctx
, scan
, ws
->waveidx
);
4435 if (ws
->enable_exclusive
) {
4436 tmp
= LLVMBuildSub(builder
, ws
->waveidx
, ctx
->i32_1
, "");
4437 tmp
= ac_build_readlane(ctx
, scan
, tmp
);
4438 tmp2
= LLVMBuildICmp(builder
, LLVMIntEQ
, ws
->waveidx
, ctx
->i32_0
, "");
4439 ws
->result_exclusive
= LLVMBuildSelect(builder
, tmp2
, identity
, tmp
, "");
4444 * Inclusive scan of a per-wave value across an entire workgroup.
4446 * This implies an s_barrier instruction.
4448 * Unlike ac_build_inclusive_scan, the caller \em must ensure that all threads
4449 * of the workgroup are live. (This requirement cannot easily be relaxed in a
4450 * useful manner because of the barrier in the algorithm.)
4453 ac_build_wg_wavescan(struct ac_llvm_context
*ctx
, struct ac_wg_scan
*ws
)
4455 ac_build_wg_wavescan_top(ctx
, ws
);
4456 ac_build_s_barrier(ctx
);
4457 ac_build_wg_wavescan_bottom(ctx
, ws
);
4461 * "Top half" of a scan that reduces per-thread values across an entire
4464 * All lanes must be active when this code runs.
4467 ac_build_wg_scan_top(struct ac_llvm_context
*ctx
, struct ac_wg_scan
*ws
)
4469 if (ws
->enable_exclusive
) {
4470 ws
->extra
= ac_build_exclusive_scan(ctx
, ws
->src
, ws
->op
);
4471 if (LLVMTypeOf(ws
->src
) == ctx
->i1
&& ws
->op
== nir_op_iadd
)
4472 ws
->src
= LLVMBuildZExt(ctx
->builder
, ws
->src
, ctx
->i32
, "");
4473 ws
->src
= ac_build_alu_op(ctx
, ws
->extra
, ws
->src
, ws
->op
);
4475 ws
->src
= ac_build_inclusive_scan(ctx
, ws
->src
, ws
->op
);
4478 bool enable_inclusive
= ws
->enable_inclusive
;
4479 bool enable_exclusive
= ws
->enable_exclusive
;
4480 ws
->enable_inclusive
= false;
4481 ws
->enable_exclusive
= ws
->enable_exclusive
|| enable_inclusive
;
4482 ac_build_wg_wavescan_top(ctx
, ws
);
4483 ws
->enable_inclusive
= enable_inclusive
;
4484 ws
->enable_exclusive
= enable_exclusive
;
4488 * "Bottom half" of a scan that reduces per-thread values across an entire
4491 * The caller must place a barrier between the top and bottom halves.
4494 ac_build_wg_scan_bottom(struct ac_llvm_context
*ctx
, struct ac_wg_scan
*ws
)
4496 bool enable_inclusive
= ws
->enable_inclusive
;
4497 bool enable_exclusive
= ws
->enable_exclusive
;
4498 ws
->enable_inclusive
= false;
4499 ws
->enable_exclusive
= ws
->enable_exclusive
|| enable_inclusive
;
4500 ac_build_wg_wavescan_bottom(ctx
, ws
);
4501 ws
->enable_inclusive
= enable_inclusive
;
4502 ws
->enable_exclusive
= enable_exclusive
;
4504 /* ws->result_reduce is already the correct value */
4505 if (ws
->enable_inclusive
)
4506 ws
->result_inclusive
= ac_build_alu_op(ctx
, ws
->result_inclusive
, ws
->src
, ws
->op
);
4507 if (ws
->enable_exclusive
)
4508 ws
->result_exclusive
= ac_build_alu_op(ctx
, ws
->result_exclusive
, ws
->extra
, ws
->op
);
4512 * A scan that reduces per-thread values across an entire workgroup.
4514 * The caller must ensure that all lanes are active when this code runs
4515 * (WWM is insufficient!), because there is an implied barrier.
4518 ac_build_wg_scan(struct ac_llvm_context
*ctx
, struct ac_wg_scan
*ws
)
4520 ac_build_wg_scan_top(ctx
, ws
);
4521 ac_build_s_barrier(ctx
);
4522 ac_build_wg_scan_bottom(ctx
, ws
);
4526 ac_build_quad_swizzle(struct ac_llvm_context
*ctx
, LLVMValueRef src
,
4527 unsigned lane0
, unsigned lane1
, unsigned lane2
, unsigned lane3
)
4529 unsigned mask
= dpp_quad_perm(lane0
, lane1
, lane2
, lane3
);
4530 if (ctx
->chip_class
>= GFX8
) {
4531 return ac_build_dpp(ctx
, src
, src
, mask
, 0xf, 0xf, false);
4533 return ac_build_ds_swizzle(ctx
, src
, (1 << 15) | mask
);
4538 ac_build_shuffle(struct ac_llvm_context
*ctx
, LLVMValueRef src
, LLVMValueRef index
)
4540 index
= LLVMBuildMul(ctx
->builder
, index
, LLVMConstInt(ctx
->i32
, 4, 0), "");
4541 return ac_build_intrinsic(ctx
,
4542 "llvm.amdgcn.ds.bpermute", ctx
->i32
,
4543 (LLVMValueRef
[]) {index
, src
}, 2,
4544 AC_FUNC_ATTR_READNONE
|
4545 AC_FUNC_ATTR_CONVERGENT
);
4549 ac_build_frexp_exp(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
4555 if (bitsize
== 16) {
4556 intr
= "llvm.amdgcn.frexp.exp.i16.f16";
4558 } else if (bitsize
== 32) {
4559 intr
= "llvm.amdgcn.frexp.exp.i32.f32";
4562 intr
= "llvm.amdgcn.frexp.exp.i32.f64";
4566 LLVMValueRef params
[] = {
4569 return ac_build_intrinsic(ctx
, intr
, type
, params
, 1,
4570 AC_FUNC_ATTR_READNONE
);
4573 ac_build_frexp_mant(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
4579 if (bitsize
== 16) {
4580 intr
= "llvm.amdgcn.frexp.mant.f16";
4582 } else if (bitsize
== 32) {
4583 intr
= "llvm.amdgcn.frexp.mant.f32";
4586 intr
= "llvm.amdgcn.frexp.mant.f64";
4590 LLVMValueRef params
[] = {
4593 return ac_build_intrinsic(ctx
, intr
, type
, params
, 1,
4594 AC_FUNC_ATTR_READNONE
);
4598 * this takes an I,J coordinate pair,
4599 * and works out the X and Y derivatives.
4600 * it returns DDX(I), DDX(J), DDY(I), DDY(J).
4603 ac_build_ddxy_interp(struct ac_llvm_context
*ctx
, LLVMValueRef interp_ij
)
4605 LLVMValueRef result
[4], a
;
4608 for (i
= 0; i
< 2; i
++) {
4609 a
= LLVMBuildExtractElement(ctx
->builder
, interp_ij
,
4610 LLVMConstInt(ctx
->i32
, i
, false), "");
4611 result
[i
] = ac_build_ddxy(ctx
, AC_TID_MASK_TOP_LEFT
, 1, a
);
4612 result
[2+i
] = ac_build_ddxy(ctx
, AC_TID_MASK_TOP_LEFT
, 2, a
);
4614 return ac_build_gather_values(ctx
, result
, 4);
4618 ac_build_load_helper_invocation(struct ac_llvm_context
*ctx
)
4620 LLVMValueRef result
= ac_build_intrinsic(ctx
, "llvm.amdgcn.ps.live",
4622 AC_FUNC_ATTR_READNONE
);
4623 result
= LLVMBuildNot(ctx
->builder
, result
, "");
4624 return LLVMBuildSExt(ctx
->builder
, result
, ctx
->i32
, "");
4627 LLVMValueRef
ac_build_call(struct ac_llvm_context
*ctx
, LLVMValueRef func
,
4628 LLVMValueRef
*args
, unsigned num_args
)
4630 LLVMValueRef ret
= LLVMBuildCall(ctx
->builder
, func
, args
, num_args
, "");
4631 LLVMSetInstructionCallConv(ret
, LLVMGetFunctionCallConv(func
));