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>
29 #include <llvm/Config/llvm-config.h>
31 #include "c11/threads.h"
36 #include "ac_llvm_util.h"
37 #include "ac_shader_util.h"
38 #include "ac_exp_param.h"
39 #include "util/bitscan.h"
40 #include "util/macros.h"
41 #include "util/u_atomic.h"
42 #include "util/u_math.h"
45 #include "shader_enums.h"
47 #define AC_LLVM_INITIAL_CF_DEPTH 4
49 /* Data for if/else/endif and bgnloop/endloop control flow structures.
52 /* Loop exit or next part of if/else/endif. */
53 LLVMBasicBlockRef next_block
;
54 LLVMBasicBlockRef loop_entry_block
;
57 /* Initialize module-independent parts of the context.
59 * The caller is responsible for initializing ctx::module and ctx::builder.
62 ac_llvm_context_init(struct ac_llvm_context
*ctx
,
63 struct ac_llvm_compiler
*compiler
,
64 enum chip_class chip_class
, enum radeon_family family
,
65 enum ac_float_mode float_mode
, unsigned wave_size
,
66 unsigned ballot_mask_bits
)
68 ctx
->context
= LLVMContextCreate();
70 ctx
->chip_class
= chip_class
;
72 ctx
->wave_size
= wave_size
;
73 ctx
->ballot_mask_bits
= ballot_mask_bits
;
74 ctx
->float_mode
= float_mode
;
75 ctx
->module
= ac_create_module(wave_size
== 32 ? compiler
->tm_wave32
78 ctx
->builder
= ac_create_builder(ctx
->context
, float_mode
);
80 ctx
->voidt
= LLVMVoidTypeInContext(ctx
->context
);
81 ctx
->i1
= LLVMInt1TypeInContext(ctx
->context
);
82 ctx
->i8
= LLVMInt8TypeInContext(ctx
->context
);
83 ctx
->i16
= LLVMIntTypeInContext(ctx
->context
, 16);
84 ctx
->i32
= LLVMIntTypeInContext(ctx
->context
, 32);
85 ctx
->i64
= LLVMIntTypeInContext(ctx
->context
, 64);
86 ctx
->i128
= LLVMIntTypeInContext(ctx
->context
, 128);
87 ctx
->intptr
= ctx
->i32
;
88 ctx
->f16
= LLVMHalfTypeInContext(ctx
->context
);
89 ctx
->f32
= LLVMFloatTypeInContext(ctx
->context
);
90 ctx
->f64
= LLVMDoubleTypeInContext(ctx
->context
);
91 ctx
->v2i16
= LLVMVectorType(ctx
->i16
, 2);
92 ctx
->v4i16
= LLVMVectorType(ctx
->i16
, 4);
93 ctx
->v2f16
= LLVMVectorType(ctx
->f16
, 2);
94 ctx
->v4f16
= LLVMVectorType(ctx
->f16
, 4);
95 ctx
->v2i32
= LLVMVectorType(ctx
->i32
, 2);
96 ctx
->v3i32
= LLVMVectorType(ctx
->i32
, 3);
97 ctx
->v4i32
= LLVMVectorType(ctx
->i32
, 4);
98 ctx
->v2f32
= LLVMVectorType(ctx
->f32
, 2);
99 ctx
->v3f32
= LLVMVectorType(ctx
->f32
, 3);
100 ctx
->v4f32
= LLVMVectorType(ctx
->f32
, 4);
101 ctx
->v8i32
= LLVMVectorType(ctx
->i32
, 8);
102 ctx
->iN_wavemask
= LLVMIntTypeInContext(ctx
->context
, ctx
->wave_size
);
103 ctx
->iN_ballotmask
= LLVMIntTypeInContext(ctx
->context
, ballot_mask_bits
);
105 ctx
->i8_0
= LLVMConstInt(ctx
->i8
, 0, false);
106 ctx
->i8_1
= LLVMConstInt(ctx
->i8
, 1, false);
107 ctx
->i16_0
= LLVMConstInt(ctx
->i16
, 0, false);
108 ctx
->i16_1
= LLVMConstInt(ctx
->i16
, 1, false);
109 ctx
->i32_0
= LLVMConstInt(ctx
->i32
, 0, false);
110 ctx
->i32_1
= LLVMConstInt(ctx
->i32
, 1, false);
111 ctx
->i64_0
= LLVMConstInt(ctx
->i64
, 0, false);
112 ctx
->i64_1
= LLVMConstInt(ctx
->i64
, 1, false);
113 ctx
->i128_0
= LLVMConstInt(ctx
->i128
, 0, false);
114 ctx
->i128_1
= LLVMConstInt(ctx
->i128
, 1, false);
115 ctx
->f16_0
= LLVMConstReal(ctx
->f16
, 0.0);
116 ctx
->f16_1
= LLVMConstReal(ctx
->f16
, 1.0);
117 ctx
->f32_0
= LLVMConstReal(ctx
->f32
, 0.0);
118 ctx
->f32_1
= LLVMConstReal(ctx
->f32
, 1.0);
119 ctx
->f64_0
= LLVMConstReal(ctx
->f64
, 0.0);
120 ctx
->f64_1
= LLVMConstReal(ctx
->f64
, 1.0);
122 ctx
->i1false
= LLVMConstInt(ctx
->i1
, 0, false);
123 ctx
->i1true
= LLVMConstInt(ctx
->i1
, 1, false);
125 ctx
->range_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
128 ctx
->invariant_load_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
129 "invariant.load", 14);
131 ctx
->uniform_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
132 "amdgpu.uniform", 14);
134 ctx
->empty_md
= LLVMMDNodeInContext(ctx
->context
, NULL
, 0);
135 ctx
->flow
= calloc(1, sizeof(*ctx
->flow
));
139 ac_llvm_context_dispose(struct ac_llvm_context
*ctx
)
141 free(ctx
->flow
->stack
);
147 ac_get_llvm_num_components(LLVMValueRef value
)
149 LLVMTypeRef type
= LLVMTypeOf(value
);
150 unsigned num_components
= LLVMGetTypeKind(type
) == LLVMVectorTypeKind
151 ? LLVMGetVectorSize(type
)
153 return num_components
;
157 ac_llvm_extract_elem(struct ac_llvm_context
*ac
,
161 if (LLVMGetTypeKind(LLVMTypeOf(value
)) != LLVMVectorTypeKind
) {
166 return LLVMBuildExtractElement(ac
->builder
, value
,
167 LLVMConstInt(ac
->i32
, index
, false), "");
171 ac_get_elem_bits(struct ac_llvm_context
*ctx
, LLVMTypeRef type
)
173 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
)
174 type
= LLVMGetElementType(type
);
176 if (LLVMGetTypeKind(type
) == LLVMIntegerTypeKind
)
177 return LLVMGetIntTypeWidth(type
);
179 if (LLVMGetTypeKind(type
) == LLVMPointerTypeKind
) {
180 if (LLVMGetPointerAddressSpace(type
) == AC_ADDR_SPACE_LDS
)
184 if (type
== ctx
->f16
)
186 if (type
== ctx
->f32
)
188 if (type
== ctx
->f64
)
191 unreachable("Unhandled type kind in get_elem_bits");
195 ac_get_type_size(LLVMTypeRef type
)
197 LLVMTypeKind kind
= LLVMGetTypeKind(type
);
200 case LLVMIntegerTypeKind
:
201 return LLVMGetIntTypeWidth(type
) / 8;
202 case LLVMHalfTypeKind
:
204 case LLVMFloatTypeKind
:
206 case LLVMDoubleTypeKind
:
208 case LLVMPointerTypeKind
:
209 if (LLVMGetPointerAddressSpace(type
) == AC_ADDR_SPACE_CONST_32BIT
)
212 case LLVMVectorTypeKind
:
213 return LLVMGetVectorSize(type
) *
214 ac_get_type_size(LLVMGetElementType(type
));
215 case LLVMArrayTypeKind
:
216 return LLVMGetArrayLength(type
) *
217 ac_get_type_size(LLVMGetElementType(type
));
224 static LLVMTypeRef
to_integer_type_scalar(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
228 else if (t
== ctx
->f16
|| t
== ctx
->i16
)
230 else if (t
== ctx
->f32
|| t
== ctx
->i32
)
232 else if (t
== ctx
->f64
|| t
== ctx
->i64
)
235 unreachable("Unhandled integer size");
239 ac_to_integer_type(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
241 if (LLVMGetTypeKind(t
) == LLVMVectorTypeKind
) {
242 LLVMTypeRef elem_type
= LLVMGetElementType(t
);
243 return LLVMVectorType(to_integer_type_scalar(ctx
, elem_type
),
244 LLVMGetVectorSize(t
));
246 if (LLVMGetTypeKind(t
) == LLVMPointerTypeKind
) {
247 switch (LLVMGetPointerAddressSpace(t
)) {
248 case AC_ADDR_SPACE_GLOBAL
:
250 case AC_ADDR_SPACE_CONST_32BIT
:
251 case AC_ADDR_SPACE_LDS
:
254 unreachable("unhandled address space");
257 return to_integer_type_scalar(ctx
, t
);
261 ac_to_integer(struct ac_llvm_context
*ctx
, LLVMValueRef v
)
263 LLVMTypeRef type
= LLVMTypeOf(v
);
264 if (LLVMGetTypeKind(type
) == LLVMPointerTypeKind
) {
265 return LLVMBuildPtrToInt(ctx
->builder
, v
, ac_to_integer_type(ctx
, type
), "");
267 return LLVMBuildBitCast(ctx
->builder
, v
, ac_to_integer_type(ctx
, type
), "");
271 ac_to_integer_or_pointer(struct ac_llvm_context
*ctx
, LLVMValueRef v
)
273 LLVMTypeRef type
= LLVMTypeOf(v
);
274 if (LLVMGetTypeKind(type
) == LLVMPointerTypeKind
)
276 return ac_to_integer(ctx
, v
);
279 static LLVMTypeRef
to_float_type_scalar(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
283 else if (t
== ctx
->i16
|| t
== ctx
->f16
)
285 else if (t
== ctx
->i32
|| t
== ctx
->f32
)
287 else if (t
== ctx
->i64
|| t
== ctx
->f64
)
290 unreachable("Unhandled float size");
294 ac_to_float_type(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
296 if (LLVMGetTypeKind(t
) == LLVMVectorTypeKind
) {
297 LLVMTypeRef elem_type
= LLVMGetElementType(t
);
298 return LLVMVectorType(to_float_type_scalar(ctx
, elem_type
),
299 LLVMGetVectorSize(t
));
301 return to_float_type_scalar(ctx
, t
);
305 ac_to_float(struct ac_llvm_context
*ctx
, LLVMValueRef v
)
307 LLVMTypeRef type
= LLVMTypeOf(v
);
308 return LLVMBuildBitCast(ctx
->builder
, v
, ac_to_float_type(ctx
, type
), "");
313 ac_build_intrinsic(struct ac_llvm_context
*ctx
, const char *name
,
314 LLVMTypeRef return_type
, LLVMValueRef
*params
,
315 unsigned param_count
, unsigned attrib_mask
)
317 LLVMValueRef function
, call
;
318 bool set_callsite_attrs
= !(attrib_mask
& AC_FUNC_ATTR_LEGACY
);
320 function
= LLVMGetNamedFunction(ctx
->module
, name
);
322 LLVMTypeRef param_types
[32], function_type
;
325 assert(param_count
<= 32);
327 for (i
= 0; i
< param_count
; ++i
) {
329 param_types
[i
] = LLVMTypeOf(params
[i
]);
332 LLVMFunctionType(return_type
, param_types
, param_count
, 0);
333 function
= LLVMAddFunction(ctx
->module
, name
, function_type
);
335 LLVMSetFunctionCallConv(function
, LLVMCCallConv
);
336 LLVMSetLinkage(function
, LLVMExternalLinkage
);
338 if (!set_callsite_attrs
)
339 ac_add_func_attributes(ctx
->context
, function
, attrib_mask
);
342 call
= LLVMBuildCall(ctx
->builder
, function
, params
, param_count
, "");
343 if (set_callsite_attrs
)
344 ac_add_func_attributes(ctx
->context
, call
, attrib_mask
);
349 * Given the i32 or vNi32 \p type, generate the textual name (e.g. for use with
352 void ac_build_type_name_for_intr(LLVMTypeRef type
, char *buf
, unsigned bufsize
)
354 LLVMTypeRef elem_type
= type
;
356 assert(bufsize
>= 8);
358 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
) {
359 int ret
= snprintf(buf
, bufsize
, "v%u",
360 LLVMGetVectorSize(type
));
362 char *type_name
= LLVMPrintTypeToString(type
);
363 fprintf(stderr
, "Error building type name for: %s\n",
365 LLVMDisposeMessage(type_name
);
368 elem_type
= LLVMGetElementType(type
);
372 switch (LLVMGetTypeKind(elem_type
)) {
374 case LLVMIntegerTypeKind
:
375 snprintf(buf
, bufsize
, "i%d", LLVMGetIntTypeWidth(elem_type
));
377 case LLVMHalfTypeKind
:
378 snprintf(buf
, bufsize
, "f16");
380 case LLVMFloatTypeKind
:
381 snprintf(buf
, bufsize
, "f32");
383 case LLVMDoubleTypeKind
:
384 snprintf(buf
, bufsize
, "f64");
390 * Helper function that builds an LLVM IR PHI node and immediately adds
394 ac_build_phi(struct ac_llvm_context
*ctx
, LLVMTypeRef type
,
395 unsigned count_incoming
, LLVMValueRef
*values
,
396 LLVMBasicBlockRef
*blocks
)
398 LLVMValueRef phi
= LLVMBuildPhi(ctx
->builder
, type
, "");
399 LLVMAddIncoming(phi
, values
, blocks
, count_incoming
);
403 void ac_build_s_barrier(struct ac_llvm_context
*ctx
)
405 ac_build_intrinsic(ctx
, "llvm.amdgcn.s.barrier", ctx
->voidt
, NULL
,
406 0, AC_FUNC_ATTR_CONVERGENT
);
409 /* Prevent optimizations (at least of memory accesses) across the current
410 * point in the program by emitting empty inline assembly that is marked as
411 * having side effects.
413 * Optionally, a value can be passed through the inline assembly to prevent
414 * LLVM from hoisting calls to ReadNone functions.
417 ac_build_optimization_barrier(struct ac_llvm_context
*ctx
,
420 static int counter
= 0;
422 LLVMBuilderRef builder
= ctx
->builder
;
425 snprintf(code
, sizeof(code
), "; %d", p_atomic_inc_return(&counter
));
428 LLVMTypeRef ftype
= LLVMFunctionType(ctx
->voidt
, NULL
, 0, false);
429 LLVMValueRef inlineasm
= LLVMConstInlineAsm(ftype
, code
, "", true, false);
430 LLVMBuildCall(builder
, inlineasm
, NULL
, 0, "");
432 LLVMTypeRef ftype
= LLVMFunctionType(ctx
->i32
, &ctx
->i32
, 1, false);
433 LLVMValueRef inlineasm
= LLVMConstInlineAsm(ftype
, code
, "=v,0", true, false);
434 LLVMTypeRef type
= LLVMTypeOf(*pvgpr
);
435 unsigned bitsize
= ac_get_elem_bits(ctx
, type
);
436 LLVMValueRef vgpr
= *pvgpr
;
437 LLVMTypeRef vgpr_type
;
442 vgpr
= LLVMBuildZExt(ctx
->builder
, vgpr
, ctx
->i32
, "");
444 vgpr_type
= LLVMTypeOf(vgpr
);
445 vgpr_size
= ac_get_type_size(vgpr_type
);
447 assert(vgpr_size
% 4 == 0);
449 vgpr
= LLVMBuildBitCast(builder
, vgpr
, LLVMVectorType(ctx
->i32
, vgpr_size
/ 4), "");
450 vgpr0
= LLVMBuildExtractElement(builder
, vgpr
, ctx
->i32_0
, "");
451 vgpr0
= LLVMBuildCall(builder
, inlineasm
, &vgpr0
, 1, "");
452 vgpr
= LLVMBuildInsertElement(builder
, vgpr
, vgpr0
, ctx
->i32_0
, "");
453 vgpr
= LLVMBuildBitCast(builder
, vgpr
, vgpr_type
, "");
456 vgpr
= LLVMBuildTrunc(builder
, vgpr
, type
, "");
463 ac_build_shader_clock(struct ac_llvm_context
*ctx
, nir_scope scope
)
465 const char *name
= scope
== NIR_SCOPE_DEVICE
? "llvm.amdgcn.s.memrealtime" : "llvm.amdgcn.s.memtime";
466 LLVMValueRef tmp
= ac_build_intrinsic(ctx
, name
, ctx
->i64
, NULL
, 0, 0);
467 return LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->v2i32
, "");
471 ac_build_ballot(struct ac_llvm_context
*ctx
,
476 if (LLVM_VERSION_MAJOR
>= 9) {
477 if (ctx
->wave_size
== 64)
478 name
= "llvm.amdgcn.icmp.i64.i32";
480 name
= "llvm.amdgcn.icmp.i32.i32";
482 name
= "llvm.amdgcn.icmp.i32";
484 LLVMValueRef args
[3] = {
487 LLVMConstInt(ctx
->i32
, LLVMIntNE
, 0)
490 /* We currently have no other way to prevent LLVM from lifting the icmp
491 * calls to a dominating basic block.
493 ac_build_optimization_barrier(ctx
, &args
[0]);
495 args
[0] = ac_to_integer(ctx
, args
[0]);
497 return ac_build_intrinsic(ctx
, name
, ctx
->iN_wavemask
, args
, 3,
498 AC_FUNC_ATTR_NOUNWIND
|
499 AC_FUNC_ATTR_READNONE
|
500 AC_FUNC_ATTR_CONVERGENT
);
503 LLVMValueRef
ac_get_i1_sgpr_mask(struct ac_llvm_context
*ctx
,
508 if (LLVM_VERSION_MAJOR
>= 9) {
509 if (ctx
->wave_size
== 64)
510 name
= "llvm.amdgcn.icmp.i64.i1";
512 name
= "llvm.amdgcn.icmp.i32.i1";
514 name
= "llvm.amdgcn.icmp.i1";
516 LLVMValueRef args
[3] = {
519 LLVMConstInt(ctx
->i32
, LLVMIntNE
, 0),
522 return ac_build_intrinsic(ctx
, name
, ctx
->iN_wavemask
, args
, 3,
523 AC_FUNC_ATTR_NOUNWIND
|
524 AC_FUNC_ATTR_READNONE
|
525 AC_FUNC_ATTR_CONVERGENT
);
529 ac_build_vote_all(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
531 LLVMValueRef active_set
= ac_build_ballot(ctx
, ctx
->i32_1
);
532 LLVMValueRef vote_set
= ac_build_ballot(ctx
, value
);
533 return LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, vote_set
, active_set
, "");
537 ac_build_vote_any(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
539 LLVMValueRef vote_set
= ac_build_ballot(ctx
, value
);
540 return LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, vote_set
,
541 LLVMConstInt(ctx
->iN_wavemask
, 0, 0), "");
545 ac_build_vote_eq(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
547 LLVMValueRef active_set
= ac_build_ballot(ctx
, ctx
->i32_1
);
548 LLVMValueRef vote_set
= ac_build_ballot(ctx
, value
);
550 LLVMValueRef all
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
551 vote_set
, active_set
, "");
552 LLVMValueRef none
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
554 LLVMConstInt(ctx
->iN_wavemask
, 0, 0), "");
555 return LLVMBuildOr(ctx
->builder
, all
, none
, "");
559 ac_build_varying_gather_values(struct ac_llvm_context
*ctx
, LLVMValueRef
*values
,
560 unsigned value_count
, unsigned component
)
562 LLVMValueRef vec
= NULL
;
564 if (value_count
== 1) {
565 return values
[component
];
566 } else if (!value_count
)
567 unreachable("value_count is 0");
569 for (unsigned i
= component
; i
< value_count
+ component
; i
++) {
570 LLVMValueRef value
= values
[i
];
573 vec
= LLVMGetUndef( LLVMVectorType(LLVMTypeOf(value
), value_count
));
574 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, i
- component
, false);
575 vec
= LLVMBuildInsertElement(ctx
->builder
, vec
, value
, index
, "");
581 ac_build_gather_values_extended(struct ac_llvm_context
*ctx
,
582 LLVMValueRef
*values
,
583 unsigned value_count
,
584 unsigned value_stride
,
588 LLVMBuilderRef builder
= ctx
->builder
;
589 LLVMValueRef vec
= NULL
;
592 if (value_count
== 1 && !always_vector
) {
594 return LLVMBuildLoad(builder
, values
[0], "");
596 } else if (!value_count
)
597 unreachable("value_count is 0");
599 for (i
= 0; i
< value_count
; i
++) {
600 LLVMValueRef value
= values
[i
* value_stride
];
602 value
= LLVMBuildLoad(builder
, value
, "");
605 vec
= LLVMGetUndef( LLVMVectorType(LLVMTypeOf(value
), value_count
));
606 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, i
, false);
607 vec
= LLVMBuildInsertElement(builder
, vec
, value
, index
, "");
613 ac_build_gather_values(struct ac_llvm_context
*ctx
,
614 LLVMValueRef
*values
,
615 unsigned value_count
)
617 return ac_build_gather_values_extended(ctx
, values
, value_count
, 1, false, false);
620 /* Expand a scalar or vector to <dst_channels x type> by filling the remaining
621 * channels with undef. Extract at most src_channels components from the input.
624 ac_build_expand(struct ac_llvm_context
*ctx
,
626 unsigned src_channels
,
627 unsigned dst_channels
)
629 LLVMTypeRef elemtype
;
630 LLVMValueRef chan
[dst_channels
];
632 if (LLVMGetTypeKind(LLVMTypeOf(value
)) == LLVMVectorTypeKind
) {
633 unsigned vec_size
= LLVMGetVectorSize(LLVMTypeOf(value
));
635 if (src_channels
== dst_channels
&& vec_size
== dst_channels
)
638 src_channels
= MIN2(src_channels
, vec_size
);
640 for (unsigned i
= 0; i
< src_channels
; i
++)
641 chan
[i
] = ac_llvm_extract_elem(ctx
, value
, i
);
643 elemtype
= LLVMGetElementType(LLVMTypeOf(value
));
646 assert(src_channels
== 1);
649 elemtype
= LLVMTypeOf(value
);
652 for (unsigned i
= src_channels
; i
< dst_channels
; i
++)
653 chan
[i
] = LLVMGetUndef(elemtype
);
655 return ac_build_gather_values(ctx
, chan
, dst_channels
);
658 /* Extract components [start, start + channels) from a vector.
661 ac_extract_components(struct ac_llvm_context
*ctx
,
666 LLVMValueRef chan
[channels
];
668 for (unsigned i
= 0; i
< channels
; i
++)
669 chan
[i
] = ac_llvm_extract_elem(ctx
, value
, i
+ start
);
671 return ac_build_gather_values(ctx
, chan
, channels
);
674 /* Expand a scalar or vector to <4 x type> by filling the remaining channels
675 * with undef. Extract at most num_channels components from the input.
677 LLVMValueRef
ac_build_expand_to_vec4(struct ac_llvm_context
*ctx
,
679 unsigned num_channels
)
681 return ac_build_expand(ctx
, value
, num_channels
, 4);
684 LLVMValueRef
ac_build_round(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
686 unsigned type_size
= ac_get_type_size(LLVMTypeOf(value
));
690 name
= "llvm.rint.f16";
691 else if (type_size
== 4)
692 name
= "llvm.rint.f32";
694 name
= "llvm.rint.f64";
696 return ac_build_intrinsic(ctx
, name
, LLVMTypeOf(value
), &value
, 1,
697 AC_FUNC_ATTR_READNONE
);
701 ac_build_fdiv(struct ac_llvm_context
*ctx
,
705 unsigned type_size
= ac_get_type_size(LLVMTypeOf(den
));
709 name
= "llvm.amdgcn.rcp.f16";
710 else if (type_size
== 4)
711 name
= "llvm.amdgcn.rcp.f32";
713 name
= "llvm.amdgcn.rcp.f64";
715 LLVMValueRef rcp
= ac_build_intrinsic(ctx
, name
, LLVMTypeOf(den
),
716 &den
, 1, AC_FUNC_ATTR_READNONE
);
718 return LLVMBuildFMul(ctx
->builder
, num
, rcp
, "");
721 /* See fast_idiv_by_const.h. */
722 /* Set: increment = util_fast_udiv_info::increment ? multiplier : 0; */
723 LLVMValueRef
ac_build_fast_udiv(struct ac_llvm_context
*ctx
,
725 LLVMValueRef multiplier
,
726 LLVMValueRef pre_shift
,
727 LLVMValueRef post_shift
,
728 LLVMValueRef increment
)
730 LLVMBuilderRef builder
= ctx
->builder
;
732 num
= LLVMBuildLShr(builder
, num
, pre_shift
, "");
733 num
= LLVMBuildMul(builder
,
734 LLVMBuildZExt(builder
, num
, ctx
->i64
, ""),
735 LLVMBuildZExt(builder
, multiplier
, ctx
->i64
, ""), "");
736 num
= LLVMBuildAdd(builder
, num
,
737 LLVMBuildZExt(builder
, increment
, ctx
->i64
, ""), "");
738 num
= LLVMBuildLShr(builder
, num
, LLVMConstInt(ctx
->i64
, 32, 0), "");
739 num
= LLVMBuildTrunc(builder
, num
, ctx
->i32
, "");
740 return LLVMBuildLShr(builder
, num
, post_shift
, "");
743 /* See fast_idiv_by_const.h. */
744 /* If num != UINT_MAX, this more efficient version can be used. */
745 /* Set: increment = util_fast_udiv_info::increment; */
746 LLVMValueRef
ac_build_fast_udiv_nuw(struct ac_llvm_context
*ctx
,
748 LLVMValueRef multiplier
,
749 LLVMValueRef pre_shift
,
750 LLVMValueRef post_shift
,
751 LLVMValueRef increment
)
753 LLVMBuilderRef builder
= ctx
->builder
;
755 num
= LLVMBuildLShr(builder
, num
, pre_shift
, "");
756 num
= LLVMBuildNUWAdd(builder
, num
, increment
, "");
757 num
= LLVMBuildMul(builder
,
758 LLVMBuildZExt(builder
, num
, ctx
->i64
, ""),
759 LLVMBuildZExt(builder
, multiplier
, ctx
->i64
, ""), "");
760 num
= LLVMBuildLShr(builder
, num
, LLVMConstInt(ctx
->i64
, 32, 0), "");
761 num
= LLVMBuildTrunc(builder
, num
, ctx
->i32
, "");
762 return LLVMBuildLShr(builder
, num
, post_shift
, "");
765 /* See fast_idiv_by_const.h. */
766 /* Both operands must fit in 31 bits and the divisor must not be 1. */
767 LLVMValueRef
ac_build_fast_udiv_u31_d_not_one(struct ac_llvm_context
*ctx
,
769 LLVMValueRef multiplier
,
770 LLVMValueRef post_shift
)
772 LLVMBuilderRef builder
= ctx
->builder
;
774 num
= LLVMBuildMul(builder
,
775 LLVMBuildZExt(builder
, num
, ctx
->i64
, ""),
776 LLVMBuildZExt(builder
, multiplier
, ctx
->i64
, ""), "");
777 num
= LLVMBuildLShr(builder
, num
, LLVMConstInt(ctx
->i64
, 32, 0), "");
778 num
= LLVMBuildTrunc(builder
, num
, ctx
->i32
, "");
779 return LLVMBuildLShr(builder
, num
, post_shift
, "");
782 /* Coordinates for cube map selection. sc, tc, and ma are as in Table 8.27
783 * of the OpenGL 4.5 (Compatibility Profile) specification, except ma is
784 * already multiplied by two. id is the cube face number.
786 struct cube_selection_coords
{
793 build_cube_intrinsic(struct ac_llvm_context
*ctx
,
795 struct cube_selection_coords
*out
)
797 LLVMTypeRef f32
= ctx
->f32
;
799 out
->stc
[1] = ac_build_intrinsic(ctx
, "llvm.amdgcn.cubetc",
800 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
801 out
->stc
[0] = ac_build_intrinsic(ctx
, "llvm.amdgcn.cubesc",
802 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
803 out
->ma
= ac_build_intrinsic(ctx
, "llvm.amdgcn.cubema",
804 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
805 out
->id
= ac_build_intrinsic(ctx
, "llvm.amdgcn.cubeid",
806 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
810 * Build a manual selection sequence for cube face sc/tc coordinates and
811 * major axis vector (multiplied by 2 for consistency) for the given
812 * vec3 \p coords, for the face implied by \p selcoords.
814 * For the major axis, we always adjust the sign to be in the direction of
815 * selcoords.ma; i.e., a positive out_ma means that coords is pointed towards
816 * the selcoords major axis.
818 static void build_cube_select(struct ac_llvm_context
*ctx
,
819 const struct cube_selection_coords
*selcoords
,
820 const LLVMValueRef
*coords
,
821 LLVMValueRef
*out_st
,
822 LLVMValueRef
*out_ma
)
824 LLVMBuilderRef builder
= ctx
->builder
;
825 LLVMTypeRef f32
= LLVMTypeOf(coords
[0]);
826 LLVMValueRef is_ma_positive
;
828 LLVMValueRef is_ma_z
, is_not_ma_z
;
829 LLVMValueRef is_ma_y
;
830 LLVMValueRef is_ma_x
;
834 is_ma_positive
= LLVMBuildFCmp(builder
, LLVMRealUGE
,
835 selcoords
->ma
, LLVMConstReal(f32
, 0.0), "");
836 sgn_ma
= LLVMBuildSelect(builder
, is_ma_positive
,
837 LLVMConstReal(f32
, 1.0), LLVMConstReal(f32
, -1.0), "");
839 is_ma_z
= LLVMBuildFCmp(builder
, LLVMRealUGE
, selcoords
->id
, LLVMConstReal(f32
, 4.0), "");
840 is_not_ma_z
= LLVMBuildNot(builder
, is_ma_z
, "");
841 is_ma_y
= LLVMBuildAnd(builder
, is_not_ma_z
,
842 LLVMBuildFCmp(builder
, LLVMRealUGE
, selcoords
->id
, LLVMConstReal(f32
, 2.0), ""), "");
843 is_ma_x
= LLVMBuildAnd(builder
, is_not_ma_z
, LLVMBuildNot(builder
, is_ma_y
, ""), "");
846 tmp
= LLVMBuildSelect(builder
, is_ma_x
, coords
[2], coords
[0], "");
847 sgn
= LLVMBuildSelect(builder
, is_ma_y
, LLVMConstReal(f32
, 1.0),
848 LLVMBuildSelect(builder
, is_ma_z
, sgn_ma
,
849 LLVMBuildFNeg(builder
, sgn_ma
, ""), ""), "");
850 out_st
[0] = LLVMBuildFMul(builder
, tmp
, sgn
, "");
853 tmp
= LLVMBuildSelect(builder
, is_ma_y
, coords
[2], coords
[1], "");
854 sgn
= LLVMBuildSelect(builder
, is_ma_y
, sgn_ma
,
855 LLVMConstReal(f32
, -1.0), "");
856 out_st
[1] = LLVMBuildFMul(builder
, tmp
, sgn
, "");
859 tmp
= LLVMBuildSelect(builder
, is_ma_z
, coords
[2],
860 LLVMBuildSelect(builder
, is_ma_y
, coords
[1], coords
[0], ""), "");
861 tmp
= ac_build_intrinsic(ctx
, "llvm.fabs.f32",
862 ctx
->f32
, &tmp
, 1, AC_FUNC_ATTR_READNONE
);
863 *out_ma
= LLVMBuildFMul(builder
, tmp
, LLVMConstReal(f32
, 2.0), "");
867 ac_prepare_cube_coords(struct ac_llvm_context
*ctx
,
868 bool is_deriv
, bool is_array
, bool is_lod
,
869 LLVMValueRef
*coords_arg
,
870 LLVMValueRef
*derivs_arg
)
873 LLVMBuilderRef builder
= ctx
->builder
;
874 struct cube_selection_coords selcoords
;
875 LLVMValueRef coords
[3];
878 if (is_array
&& !is_lod
) {
879 LLVMValueRef tmp
= ac_build_round(ctx
, coords_arg
[3]);
881 /* Section 8.9 (Texture Functions) of the GLSL 4.50 spec says:
883 * "For Array forms, the array layer used will be
885 * max(0, min(d−1, floor(layer+0.5)))
887 * where d is the depth of the texture array and layer
888 * comes from the component indicated in the tables below.
889 * Workaroudn for an issue where the layer is taken from a
890 * helper invocation which happens to fall on a different
891 * layer due to extrapolation."
893 * GFX8 and earlier attempt to implement this in hardware by
894 * clamping the value of coords[2] = (8 * layer) + face.
895 * Unfortunately, this means that the we end up with the wrong
896 * face when clamping occurs.
898 * Clamp the layer earlier to work around the issue.
900 if (ctx
->chip_class
<= GFX8
) {
902 ge0
= LLVMBuildFCmp(builder
, LLVMRealOGE
, tmp
, ctx
->f32_0
, "");
903 tmp
= LLVMBuildSelect(builder
, ge0
, tmp
, ctx
->f32_0
, "");
909 build_cube_intrinsic(ctx
, coords_arg
, &selcoords
);
911 invma
= ac_build_intrinsic(ctx
, "llvm.fabs.f32",
912 ctx
->f32
, &selcoords
.ma
, 1, AC_FUNC_ATTR_READNONE
);
913 invma
= ac_build_fdiv(ctx
, LLVMConstReal(ctx
->f32
, 1.0), invma
);
915 for (int i
= 0; i
< 2; ++i
)
916 coords
[i
] = LLVMBuildFMul(builder
, selcoords
.stc
[i
], invma
, "");
918 coords
[2] = selcoords
.id
;
920 if (is_deriv
&& derivs_arg
) {
921 LLVMValueRef derivs
[4];
924 /* Convert cube derivatives to 2D derivatives. */
925 for (axis
= 0; axis
< 2; axis
++) {
926 LLVMValueRef deriv_st
[2];
927 LLVMValueRef deriv_ma
;
929 /* Transform the derivative alongside the texture
930 * coordinate. Mathematically, the correct formula is
931 * as follows. Assume we're projecting onto the +Z face
932 * and denote by dx/dh the derivative of the (original)
933 * X texture coordinate with respect to horizontal
934 * window coordinates. The projection onto the +Z face
939 * Then df/dh = df/dx * dx/dh + df/dz * dz/dh
940 * = 1/z * dx/dh - x/z * 1/z * dz/dh.
942 * This motivatives the implementation below.
944 * Whether this actually gives the expected results for
945 * apps that might feed in derivatives obtained via
946 * finite differences is anyone's guess. The OpenGL spec
947 * seems awfully quiet about how textureGrad for cube
948 * maps should be handled.
950 build_cube_select(ctx
, &selcoords
, &derivs_arg
[axis
* 3],
951 deriv_st
, &deriv_ma
);
953 deriv_ma
= LLVMBuildFMul(builder
, deriv_ma
, invma
, "");
955 for (int i
= 0; i
< 2; ++i
)
956 derivs
[axis
* 2 + i
] =
957 LLVMBuildFSub(builder
,
958 LLVMBuildFMul(builder
, deriv_st
[i
], invma
, ""),
959 LLVMBuildFMul(builder
, deriv_ma
, coords
[i
], ""), "");
962 memcpy(derivs_arg
, derivs
, sizeof(derivs
));
965 /* Shift the texture coordinate. This must be applied after the
966 * derivative calculation.
968 for (int i
= 0; i
< 2; ++i
)
969 coords
[i
] = LLVMBuildFAdd(builder
, coords
[i
], LLVMConstReal(ctx
->f32
, 1.5), "");
972 /* for cube arrays coord.z = coord.w(array_index) * 8 + face */
973 /* coords_arg.w component - array_index for cube arrays */
974 coords
[2] = ac_build_fmad(ctx
, coords_arg
[3], LLVMConstReal(ctx
->f32
, 8.0), coords
[2]);
977 memcpy(coords_arg
, coords
, sizeof(coords
));
982 ac_build_fs_interp(struct ac_llvm_context
*ctx
,
983 LLVMValueRef llvm_chan
,
984 LLVMValueRef attr_number
,
989 LLVMValueRef args
[5];
994 args
[2] = attr_number
;
997 p1
= ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p1",
998 ctx
->f32
, args
, 4, AC_FUNC_ATTR_READNONE
);
1002 args
[2] = llvm_chan
;
1003 args
[3] = attr_number
;
1006 return ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p2",
1007 ctx
->f32
, args
, 5, AC_FUNC_ATTR_READNONE
);
1011 ac_build_fs_interp_f16(struct ac_llvm_context
*ctx
,
1012 LLVMValueRef llvm_chan
,
1013 LLVMValueRef attr_number
,
1014 LLVMValueRef params
,
1018 LLVMValueRef args
[6];
1022 args
[1] = llvm_chan
;
1023 args
[2] = attr_number
;
1024 args
[3] = ctx
->i1false
;
1027 p1
= ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p1.f16",
1028 ctx
->f32
, args
, 5, AC_FUNC_ATTR_READNONE
);
1032 args
[2] = llvm_chan
;
1033 args
[3] = attr_number
;
1034 args
[4] = ctx
->i1false
;
1037 return ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p2.f16",
1038 ctx
->f16
, args
, 6, AC_FUNC_ATTR_READNONE
);
1042 ac_build_fs_interp_mov(struct ac_llvm_context
*ctx
,
1043 LLVMValueRef parameter
,
1044 LLVMValueRef llvm_chan
,
1045 LLVMValueRef attr_number
,
1046 LLVMValueRef params
)
1048 LLVMValueRef args
[4];
1050 args
[0] = parameter
;
1051 args
[1] = llvm_chan
;
1052 args
[2] = attr_number
;
1055 return ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.mov",
1056 ctx
->f32
, args
, 4, AC_FUNC_ATTR_READNONE
);
1060 ac_build_gep_ptr(struct ac_llvm_context
*ctx
,
1061 LLVMValueRef base_ptr
,
1064 return LLVMBuildGEP(ctx
->builder
, base_ptr
, &index
, 1, "");
1068 ac_build_gep0(struct ac_llvm_context
*ctx
,
1069 LLVMValueRef base_ptr
,
1072 LLVMValueRef indices
[2] = {
1076 return LLVMBuildGEP(ctx
->builder
, base_ptr
, indices
, 2, "");
1079 LLVMValueRef
ac_build_pointer_add(struct ac_llvm_context
*ctx
, LLVMValueRef ptr
,
1082 return LLVMBuildPointerCast(ctx
->builder
,
1083 LLVMBuildGEP(ctx
->builder
, ptr
, &index
, 1, ""),
1084 LLVMTypeOf(ptr
), "");
1088 ac_build_indexed_store(struct ac_llvm_context
*ctx
,
1089 LLVMValueRef base_ptr
, LLVMValueRef index
,
1092 LLVMBuildStore(ctx
->builder
, value
,
1093 ac_build_gep0(ctx
, base_ptr
, index
));
1097 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad.
1098 * It's equivalent to doing a load from &base_ptr[index].
1100 * \param base_ptr Where the array starts.
1101 * \param index The element index into the array.
1102 * \param uniform Whether the base_ptr and index can be assumed to be
1103 * dynamically uniform (i.e. load to an SGPR)
1104 * \param invariant Whether the load is invariant (no other opcodes affect it)
1105 * \param no_unsigned_wraparound
1106 * For all possible re-associations and re-distributions of an expression
1107 * "base_ptr + index * elemsize" into "addr + offset" (excluding GEPs
1108 * without inbounds in base_ptr), this parameter is true if "addr + offset"
1109 * does not result in an unsigned integer wraparound. This is used for
1110 * optimal code generation of 32-bit pointer arithmetic.
1112 * For example, a 32-bit immediate offset that causes a 32-bit unsigned
1113 * integer wraparound can't be an imm offset in s_load_dword, because
1114 * the instruction performs "addr + offset" in 64 bits.
1116 * Expected usage for bindless textures by chaining GEPs:
1117 * // possible unsigned wraparound, don't use InBounds:
1118 * ptr1 = LLVMBuildGEP(base_ptr, index);
1119 * image = load(ptr1); // becomes "s_load ptr1, 0"
1121 * ptr2 = LLVMBuildInBoundsGEP(ptr1, 32 / elemsize);
1122 * sampler = load(ptr2); // becomes "s_load ptr1, 32" thanks to InBounds
1125 ac_build_load_custom(struct ac_llvm_context
*ctx
, LLVMValueRef base_ptr
,
1126 LLVMValueRef index
, bool uniform
, bool invariant
,
1127 bool no_unsigned_wraparound
)
1129 LLVMValueRef pointer
, result
;
1131 if (no_unsigned_wraparound
&&
1132 LLVMGetPointerAddressSpace(LLVMTypeOf(base_ptr
)) == AC_ADDR_SPACE_CONST_32BIT
)
1133 pointer
= LLVMBuildInBoundsGEP(ctx
->builder
, base_ptr
, &index
, 1, "");
1135 pointer
= LLVMBuildGEP(ctx
->builder
, base_ptr
, &index
, 1, "");
1138 LLVMSetMetadata(pointer
, ctx
->uniform_md_kind
, ctx
->empty_md
);
1139 result
= LLVMBuildLoad(ctx
->builder
, pointer
, "");
1141 LLVMSetMetadata(result
, ctx
->invariant_load_md_kind
, ctx
->empty_md
);
1145 LLVMValueRef
ac_build_load(struct ac_llvm_context
*ctx
, LLVMValueRef base_ptr
,
1148 return ac_build_load_custom(ctx
, base_ptr
, index
, false, false, false);
1151 LLVMValueRef
ac_build_load_invariant(struct ac_llvm_context
*ctx
,
1152 LLVMValueRef base_ptr
, LLVMValueRef index
)
1154 return ac_build_load_custom(ctx
, base_ptr
, index
, false, true, false);
1157 /* This assumes that there is no unsigned integer wraparound during the address
1158 * computation, excluding all GEPs within base_ptr. */
1159 LLVMValueRef
ac_build_load_to_sgpr(struct ac_llvm_context
*ctx
,
1160 LLVMValueRef base_ptr
, LLVMValueRef index
)
1162 return ac_build_load_custom(ctx
, base_ptr
, index
, true, true, true);
1165 /* See ac_build_load_custom() documentation. */
1166 LLVMValueRef
ac_build_load_to_sgpr_uint_wraparound(struct ac_llvm_context
*ctx
,
1167 LLVMValueRef base_ptr
, LLVMValueRef index
)
1169 return ac_build_load_custom(ctx
, base_ptr
, index
, true, true, false);
1172 static unsigned get_load_cache_policy(struct ac_llvm_context
*ctx
,
1173 unsigned cache_policy
)
1175 return cache_policy
|
1176 (ctx
->chip_class
>= GFX10
&& cache_policy
& ac_glc
? ac_dlc
: 0);
1180 ac_build_buffer_store_common(struct ac_llvm_context
*ctx
,
1183 LLVMValueRef vindex
,
1184 LLVMValueRef voffset
,
1185 LLVMValueRef soffset
,
1186 unsigned cache_policy
,
1190 LLVMValueRef args
[6];
1193 args
[idx
++] = LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, "");
1195 args
[idx
++] = vindex
? vindex
: ctx
->i32_0
;
1196 args
[idx
++] = voffset
? voffset
: ctx
->i32_0
;
1197 args
[idx
++] = soffset
? soffset
: ctx
->i32_0
;
1198 args
[idx
++] = LLVMConstInt(ctx
->i32
, cache_policy
, 0);
1199 const char *indexing_kind
= structurized
? "struct" : "raw";
1200 char name
[256], type_name
[8];
1202 ac_build_type_name_for_intr(LLVMTypeOf(data
), type_name
, sizeof(type_name
));
1205 snprintf(name
, sizeof(name
), "llvm.amdgcn.%s.buffer.store.format.%s",
1206 indexing_kind
, type_name
);
1208 snprintf(name
, sizeof(name
), "llvm.amdgcn.%s.buffer.store.%s",
1209 indexing_kind
, type_name
);
1212 ac_build_intrinsic(ctx
, name
, ctx
->voidt
, args
, idx
,
1213 AC_FUNC_ATTR_INACCESSIBLE_MEM_ONLY
);
1217 ac_build_buffer_store_format(struct ac_llvm_context
*ctx
,
1220 LLVMValueRef vindex
,
1221 LLVMValueRef voffset
,
1222 unsigned cache_policy
)
1224 ac_build_buffer_store_common(ctx
, rsrc
, data
, vindex
, voffset
, NULL
,
1225 cache_policy
, true, true);
1228 /* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
1229 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
1230 * or v4i32 (num_channels=3,4).
1233 ac_build_buffer_store_dword(struct ac_llvm_context
*ctx
,
1236 unsigned num_channels
,
1237 LLVMValueRef voffset
,
1238 LLVMValueRef soffset
,
1239 unsigned inst_offset
,
1240 unsigned cache_policy
)
1242 /* Split 3 channel stores, because only LLVM 9+ support 3-channel
1244 if (num_channels
== 3 && !ac_has_vec3_support(ctx
->chip_class
, false)) {
1245 LLVMValueRef v
[3], v01
;
1247 for (int i
= 0; i
< 3; i
++) {
1248 v
[i
] = LLVMBuildExtractElement(ctx
->builder
, vdata
,
1249 LLVMConstInt(ctx
->i32
, i
, 0), "");
1251 v01
= ac_build_gather_values(ctx
, v
, 2);
1253 ac_build_buffer_store_dword(ctx
, rsrc
, v01
, 2, voffset
,
1254 soffset
, inst_offset
, cache_policy
);
1255 ac_build_buffer_store_dword(ctx
, rsrc
, v
[2], 1, voffset
,
1256 soffset
, inst_offset
+ 8,
1261 /* SWIZZLE_ENABLE requires that soffset isn't folded into voffset
1262 * (voffset is swizzled, but soffset isn't swizzled).
1263 * llvm.amdgcn.buffer.store doesn't have a separate soffset parameter.
1265 if (!(cache_policy
& ac_swizzled
)) {
1266 LLVMValueRef offset
= soffset
;
1269 offset
= LLVMBuildAdd(ctx
->builder
, offset
,
1270 LLVMConstInt(ctx
->i32
, inst_offset
, 0), "");
1272 ac_build_buffer_store_common(ctx
, rsrc
, ac_to_float(ctx
, vdata
),
1273 ctx
->i32_0
, voffset
, offset
,
1274 cache_policy
, false, false);
1278 static const unsigned dfmts
[] = {
1279 V_008F0C_BUF_DATA_FORMAT_32
,
1280 V_008F0C_BUF_DATA_FORMAT_32_32
,
1281 V_008F0C_BUF_DATA_FORMAT_32_32_32
,
1282 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
1284 unsigned dfmt
= dfmts
[num_channels
- 1];
1285 unsigned nfmt
= V_008F0C_BUF_NUM_FORMAT_UINT
;
1286 LLVMValueRef immoffset
= LLVMConstInt(ctx
->i32
, inst_offset
, 0);
1288 ac_build_raw_tbuffer_store(ctx
, rsrc
, vdata
, voffset
, soffset
,
1289 immoffset
, num_channels
, dfmt
, nfmt
, cache_policy
);
1293 ac_build_buffer_load_common(struct ac_llvm_context
*ctx
,
1295 LLVMValueRef vindex
,
1296 LLVMValueRef voffset
,
1297 LLVMValueRef soffset
,
1298 unsigned num_channels
,
1299 LLVMTypeRef channel_type
,
1300 unsigned cache_policy
,
1305 LLVMValueRef args
[5];
1307 args
[idx
++] = LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, "");
1309 args
[idx
++] = vindex
? vindex
: ctx
->i32_0
;
1310 args
[idx
++] = voffset
? voffset
: ctx
->i32_0
;
1311 args
[idx
++] = soffset
? soffset
: ctx
->i32_0
;
1312 args
[idx
++] = LLVMConstInt(ctx
->i32
, get_load_cache_policy(ctx
, cache_policy
), 0);
1313 unsigned func
= !ac_has_vec3_support(ctx
->chip_class
, use_format
) && num_channels
== 3 ? 4 : num_channels
;
1314 const char *indexing_kind
= structurized
? "struct" : "raw";
1315 char name
[256], type_name
[8];
1317 /* D16 is only supported on gfx8+ */
1318 assert((channel_type
!= ctx
->f16
&& channel_type
!= ctx
->i16
) ||
1319 ctx
->chip_class
>= GFX8
);
1321 LLVMTypeRef type
= func
> 1 ? LLVMVectorType(channel_type
, func
) : channel_type
;
1322 ac_build_type_name_for_intr(type
, type_name
, sizeof(type_name
));
1325 snprintf(name
, sizeof(name
), "llvm.amdgcn.%s.buffer.load.format.%s",
1326 indexing_kind
, type_name
);
1328 snprintf(name
, sizeof(name
), "llvm.amdgcn.%s.buffer.load.%s",
1329 indexing_kind
, type_name
);
1332 return ac_build_intrinsic(ctx
, name
, type
, args
, idx
,
1333 ac_get_load_intr_attribs(can_speculate
));
1337 ac_build_buffer_load(struct ac_llvm_context
*ctx
,
1340 LLVMValueRef vindex
,
1341 LLVMValueRef voffset
,
1342 LLVMValueRef soffset
,
1343 unsigned inst_offset
,
1344 unsigned cache_policy
,
1348 LLVMValueRef offset
= LLVMConstInt(ctx
->i32
, inst_offset
, 0);
1350 offset
= LLVMBuildAdd(ctx
->builder
, offset
, voffset
, "");
1352 offset
= LLVMBuildAdd(ctx
->builder
, offset
, soffset
, "");
1354 if (allow_smem
&& !(cache_policy
& ac_slc
) &&
1355 (!(cache_policy
& ac_glc
) || ctx
->chip_class
>= GFX8
)) {
1356 assert(vindex
== NULL
);
1358 LLVMValueRef result
[8];
1360 for (int i
= 0; i
< num_channels
; i
++) {
1362 offset
= LLVMBuildAdd(ctx
->builder
, offset
,
1363 LLVMConstInt(ctx
->i32
, 4, 0), "");
1365 LLVMValueRef args
[3] = {
1368 LLVMConstInt(ctx
->i32
, get_load_cache_policy(ctx
, cache_policy
), 0),
1370 result
[i
] = ac_build_intrinsic(ctx
,
1371 "llvm.amdgcn.s.buffer.load.f32",
1373 AC_FUNC_ATTR_READNONE
);
1375 if (num_channels
== 1)
1378 if (num_channels
== 3 && !ac_has_vec3_support(ctx
->chip_class
, false))
1379 result
[num_channels
++] = LLVMGetUndef(ctx
->f32
);
1380 return ac_build_gather_values(ctx
, result
, num_channels
);
1383 return ac_build_buffer_load_common(ctx
, rsrc
, vindex
,
1385 num_channels
, ctx
->f32
,
1387 can_speculate
, false, false);
1390 LLVMValueRef
ac_build_buffer_load_format(struct ac_llvm_context
*ctx
,
1392 LLVMValueRef vindex
,
1393 LLVMValueRef voffset
,
1394 unsigned num_channels
,
1395 unsigned cache_policy
,
1399 return ac_build_buffer_load_common(ctx
, rsrc
, vindex
, voffset
,
1400 ctx
->i32_0
, num_channels
,
1401 d16
? ctx
->f16
: ctx
->f32
,
1402 cache_policy
, can_speculate
,
1407 ac_build_tbuffer_load(struct ac_llvm_context
*ctx
,
1409 LLVMValueRef vindex
,
1410 LLVMValueRef voffset
,
1411 LLVMValueRef soffset
,
1412 LLVMValueRef immoffset
,
1413 unsigned num_channels
,
1416 unsigned cache_policy
,
1420 voffset
= LLVMBuildAdd(ctx
->builder
, voffset
, immoffset
, "");
1422 LLVMValueRef args
[6];
1424 args
[idx
++] = LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, "");
1426 args
[idx
++] = vindex
? vindex
: ctx
->i32_0
;
1427 args
[idx
++] = voffset
? voffset
: ctx
->i32_0
;
1428 args
[idx
++] = soffset
? soffset
: ctx
->i32_0
;
1429 args
[idx
++] = LLVMConstInt(ctx
->i32
, ac_get_tbuffer_format(ctx
->chip_class
, dfmt
, nfmt
), 0);
1430 args
[idx
++] = LLVMConstInt(ctx
->i32
, get_load_cache_policy(ctx
, cache_policy
), 0);
1431 unsigned func
= !ac_has_vec3_support(ctx
->chip_class
, true) && num_channels
== 3 ? 4 : num_channels
;
1432 const char *indexing_kind
= structurized
? "struct" : "raw";
1433 char name
[256], type_name
[8];
1435 LLVMTypeRef type
= func
> 1 ? LLVMVectorType(ctx
->i32
, func
) : ctx
->i32
;
1436 ac_build_type_name_for_intr(type
, type_name
, sizeof(type_name
));
1438 snprintf(name
, sizeof(name
), "llvm.amdgcn.%s.tbuffer.load.%s",
1439 indexing_kind
, type_name
);
1441 return ac_build_intrinsic(ctx
, name
, type
, args
, idx
,
1442 ac_get_load_intr_attribs(can_speculate
));
1446 ac_build_struct_tbuffer_load(struct ac_llvm_context
*ctx
,
1448 LLVMValueRef vindex
,
1449 LLVMValueRef voffset
,
1450 LLVMValueRef soffset
,
1451 LLVMValueRef immoffset
,
1452 unsigned num_channels
,
1455 unsigned cache_policy
,
1458 return ac_build_tbuffer_load(ctx
, rsrc
, vindex
, voffset
, soffset
,
1459 immoffset
, num_channels
, dfmt
, nfmt
,
1460 cache_policy
, can_speculate
, true);
1464 ac_build_raw_tbuffer_load(struct ac_llvm_context
*ctx
,
1466 LLVMValueRef voffset
,
1467 LLVMValueRef soffset
,
1468 LLVMValueRef immoffset
,
1469 unsigned num_channels
,
1472 unsigned cache_policy
,
1475 return ac_build_tbuffer_load(ctx
, rsrc
, NULL
, voffset
, soffset
,
1476 immoffset
, num_channels
, dfmt
, nfmt
,
1477 cache_policy
, can_speculate
, false);
1481 ac_build_tbuffer_load_short(struct ac_llvm_context
*ctx
,
1483 LLVMValueRef voffset
,
1484 LLVMValueRef soffset
,
1485 LLVMValueRef immoffset
,
1486 unsigned cache_policy
)
1490 if (LLVM_VERSION_MAJOR
>= 9) {
1491 voffset
= LLVMBuildAdd(ctx
->builder
, voffset
, immoffset
, "");
1493 /* LLVM 9+ supports i8/i16 with struct/raw intrinsics. */
1494 res
= ac_build_buffer_load_common(ctx
, rsrc
, NULL
,
1496 1, ctx
->i16
, cache_policy
,
1497 false, false, false);
1499 unsigned dfmt
= V_008F0C_BUF_DATA_FORMAT_16
;
1500 unsigned nfmt
= V_008F0C_BUF_NUM_FORMAT_UINT
;
1502 res
= ac_build_raw_tbuffer_load(ctx
, rsrc
, voffset
, soffset
,
1503 immoffset
, 1, dfmt
, nfmt
, cache_policy
,
1506 res
= LLVMBuildTrunc(ctx
->builder
, res
, ctx
->i16
, "");
1513 ac_build_tbuffer_load_byte(struct ac_llvm_context
*ctx
,
1515 LLVMValueRef voffset
,
1516 LLVMValueRef soffset
,
1517 LLVMValueRef immoffset
,
1518 unsigned cache_policy
)
1522 if (LLVM_VERSION_MAJOR
>= 9) {
1523 voffset
= LLVMBuildAdd(ctx
->builder
, voffset
, immoffset
, "");
1525 /* LLVM 9+ supports i8/i16 with struct/raw intrinsics. */
1526 res
= ac_build_buffer_load_common(ctx
, rsrc
, NULL
,
1528 1, ctx
->i8
, cache_policy
,
1529 false, false, false);
1531 unsigned dfmt
= V_008F0C_BUF_DATA_FORMAT_8
;
1532 unsigned nfmt
= V_008F0C_BUF_NUM_FORMAT_UINT
;
1534 res
= ac_build_raw_tbuffer_load(ctx
, rsrc
, voffset
, soffset
,
1535 immoffset
, 1, dfmt
, nfmt
, cache_policy
,
1538 res
= LLVMBuildTrunc(ctx
->builder
, res
, ctx
->i8
, "");
1545 * Convert an 11- or 10-bit unsigned floating point number to an f32.
1547 * The input exponent is expected to be biased analogous to IEEE-754, i.e. by
1548 * 2^(exp_bits-1) - 1 (as defined in OpenGL and other graphics APIs).
1551 ac_ufN_to_float(struct ac_llvm_context
*ctx
, LLVMValueRef src
, unsigned exp_bits
, unsigned mant_bits
)
1553 assert(LLVMTypeOf(src
) == ctx
->i32
);
1556 LLVMValueRef mantissa
;
1557 mantissa
= LLVMBuildAnd(ctx
->builder
, src
, LLVMConstInt(ctx
->i32
, (1 << mant_bits
) - 1, false), "");
1559 /* Converting normal numbers is just a shift + correcting the exponent bias */
1560 unsigned normal_shift
= 23 - mant_bits
;
1561 unsigned bias_shift
= 127 - ((1 << (exp_bits
- 1)) - 1);
1562 LLVMValueRef shifted
, normal
;
1564 shifted
= LLVMBuildShl(ctx
->builder
, src
, LLVMConstInt(ctx
->i32
, normal_shift
, false), "");
1565 normal
= LLVMBuildAdd(ctx
->builder
, shifted
, LLVMConstInt(ctx
->i32
, bias_shift
<< 23, false), "");
1567 /* Converting nan/inf numbers is the same, but with a different exponent update */
1568 LLVMValueRef naninf
;
1569 naninf
= LLVMBuildOr(ctx
->builder
, normal
, LLVMConstInt(ctx
->i32
, 0xff << 23, false), "");
1571 /* Converting denormals is the complex case: determine the leading zeros of the
1572 * mantissa to obtain the correct shift for the mantissa and exponent correction.
1574 LLVMValueRef denormal
;
1575 LLVMValueRef params
[2] = {
1577 ctx
->i1true
, /* result can be undef when arg is 0 */
1579 LLVMValueRef ctlz
= ac_build_intrinsic(ctx
, "llvm.ctlz.i32", ctx
->i32
,
1580 params
, 2, AC_FUNC_ATTR_READNONE
);
1582 /* Shift such that the leading 1 ends up as the LSB of the exponent field. */
1583 tmp
= LLVMBuildSub(ctx
->builder
, ctlz
, LLVMConstInt(ctx
->i32
, 8, false), "");
1584 denormal
= LLVMBuildShl(ctx
->builder
, mantissa
, tmp
, "");
1586 unsigned denormal_exp
= bias_shift
+ (32 - mant_bits
) - 1;
1587 tmp
= LLVMBuildSub(ctx
->builder
, LLVMConstInt(ctx
->i32
, denormal_exp
, false), ctlz
, "");
1588 tmp
= LLVMBuildShl(ctx
->builder
, tmp
, LLVMConstInt(ctx
->i32
, 23, false), "");
1589 denormal
= LLVMBuildAdd(ctx
->builder
, denormal
, tmp
, "");
1591 /* Select the final result. */
1592 LLVMValueRef result
;
1594 tmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntUGE
, src
,
1595 LLVMConstInt(ctx
->i32
, ((1 << exp_bits
) - 1) << mant_bits
, false), "");
1596 result
= LLVMBuildSelect(ctx
->builder
, tmp
, naninf
, normal
, "");
1598 tmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntUGE
, src
,
1599 LLVMConstInt(ctx
->i32
, 1 << mant_bits
, false), "");
1600 result
= LLVMBuildSelect(ctx
->builder
, tmp
, result
, denormal
, "");
1602 tmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src
, ctx
->i32_0
, "");
1603 result
= LLVMBuildSelect(ctx
->builder
, tmp
, result
, ctx
->i32_0
, "");
1605 return ac_to_float(ctx
, result
);
1609 * Generate a fully general open coded buffer format fetch with all required
1610 * fixups suitable for vertex fetch, using non-format buffer loads.
1612 * Some combinations of argument values have special interpretations:
1613 * - size = 8 bytes, format = fixed indicates PIPE_FORMAT_R11G11B10_FLOAT
1614 * - size = 8 bytes, format != {float,fixed} indicates a 2_10_10_10 data format
1616 * \param log_size log(size of channel in bytes)
1617 * \param num_channels number of channels (1 to 4)
1618 * \param format AC_FETCH_FORMAT_xxx value
1619 * \param reverse whether XYZ channels are reversed
1620 * \param known_aligned whether the source is known to be aligned to hardware's
1621 * effective element size for loading the given format
1622 * (note: this means dword alignment for 8_8_8_8, 16_16, etc.)
1623 * \param rsrc buffer resource descriptor
1624 * \return the resulting vector of floats or integers bitcast to <4 x i32>
1627 ac_build_opencoded_load_format(struct ac_llvm_context
*ctx
,
1629 unsigned num_channels
,
1634 LLVMValueRef vindex
,
1635 LLVMValueRef voffset
,
1636 LLVMValueRef soffset
,
1637 unsigned cache_policy
,
1641 unsigned load_log_size
= log_size
;
1642 unsigned load_num_channels
= num_channels
;
1643 if (log_size
== 3) {
1645 if (format
== AC_FETCH_FORMAT_FLOAT
) {
1646 load_num_channels
= 2 * num_channels
;
1648 load_num_channels
= 1; /* 10_11_11 or 2_10_10_10 */
1652 int log_recombine
= 0;
1653 if (ctx
->chip_class
== GFX6
&& !known_aligned
) {
1654 /* Avoid alignment restrictions by loading one byte at a time. */
1655 load_num_channels
<<= load_log_size
;
1656 log_recombine
= load_log_size
;
1658 } else if (load_num_channels
== 2 || load_num_channels
== 4) {
1659 log_recombine
= -util_logbase2(load_num_channels
);
1660 load_num_channels
= 1;
1661 load_log_size
+= -log_recombine
;
1664 assert(load_log_size
>= 2 || LLVM_VERSION_MAJOR
>= 9);
1666 LLVMValueRef loads
[32]; /* up to 32 bytes */
1667 for (unsigned i
= 0; i
< load_num_channels
; ++i
) {
1668 tmp
= LLVMBuildAdd(ctx
->builder
, soffset
,
1669 LLVMConstInt(ctx
->i32
, i
<< load_log_size
, false), "");
1670 LLVMTypeRef channel_type
= load_log_size
== 0 ? ctx
->i8
:
1671 load_log_size
== 1 ? ctx
->i16
: ctx
->i32
;
1672 unsigned num_channels
= 1 << (MAX2(load_log_size
, 2) - 2);
1673 loads
[i
] = ac_build_buffer_load_common(
1674 ctx
, rsrc
, vindex
, voffset
, tmp
,
1675 num_channels
, channel_type
, cache_policy
,
1676 can_speculate
, false, true);
1677 if (load_log_size
>= 2)
1678 loads
[i
] = ac_to_integer(ctx
, loads
[i
]);
1681 if (log_recombine
> 0) {
1682 /* Recombine bytes if necessary (GFX6 only) */
1683 LLVMTypeRef dst_type
= log_recombine
== 2 ? ctx
->i32
: ctx
->i16
;
1685 for (unsigned src
= 0, dst
= 0; src
< load_num_channels
; ++dst
) {
1686 LLVMValueRef accum
= NULL
;
1687 for (unsigned i
= 0; i
< (1 << log_recombine
); ++i
, ++src
) {
1688 tmp
= LLVMBuildZExt(ctx
->builder
, loads
[src
], dst_type
, "");
1692 tmp
= LLVMBuildShl(ctx
->builder
, tmp
,
1693 LLVMConstInt(dst_type
, 8 * i
, false), "");
1694 accum
= LLVMBuildOr(ctx
->builder
, accum
, tmp
, "");
1699 } else if (log_recombine
< 0) {
1700 /* Split vectors of dwords */
1701 if (load_log_size
> 2) {
1702 assert(load_num_channels
== 1);
1703 LLVMValueRef loaded
= loads
[0];
1704 unsigned log_split
= load_log_size
- 2;
1705 log_recombine
+= log_split
;
1706 load_num_channels
= 1 << log_split
;
1708 for (unsigned i
= 0; i
< load_num_channels
; ++i
) {
1709 tmp
= LLVMConstInt(ctx
->i32
, i
, false);
1710 loads
[i
] = LLVMBuildExtractElement(ctx
->builder
, loaded
, tmp
, "");
1714 /* Further split dwords and shorts if required */
1715 if (log_recombine
< 0) {
1716 for (unsigned src
= load_num_channels
,
1717 dst
= load_num_channels
<< -log_recombine
;
1719 unsigned dst_bits
= 1 << (3 + load_log_size
+ log_recombine
);
1720 LLVMTypeRef dst_type
= LLVMIntTypeInContext(ctx
->context
, dst_bits
);
1721 LLVMValueRef loaded
= loads
[src
- 1];
1722 LLVMTypeRef loaded_type
= LLVMTypeOf(loaded
);
1723 for (unsigned i
= 1 << -log_recombine
; i
> 0; --i
, --dst
) {
1724 tmp
= LLVMConstInt(loaded_type
, dst_bits
* (i
- 1), false);
1725 tmp
= LLVMBuildLShr(ctx
->builder
, loaded
, tmp
, "");
1726 loads
[dst
- 1] = LLVMBuildTrunc(ctx
->builder
, tmp
, dst_type
, "");
1732 if (log_size
== 3) {
1733 if (format
== AC_FETCH_FORMAT_FLOAT
) {
1734 for (unsigned i
= 0; i
< num_channels
; ++i
) {
1735 tmp
= ac_build_gather_values(ctx
, &loads
[2 * i
], 2);
1736 loads
[i
] = LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f64
, "");
1738 } else if (format
== AC_FETCH_FORMAT_FIXED
) {
1739 /* 10_11_11_FLOAT */
1740 LLVMValueRef data
= loads
[0];
1741 LLVMValueRef i32_2047
= LLVMConstInt(ctx
->i32
, 2047, false);
1742 LLVMValueRef r
= LLVMBuildAnd(ctx
->builder
, data
, i32_2047
, "");
1743 tmp
= LLVMBuildLShr(ctx
->builder
, data
, LLVMConstInt(ctx
->i32
, 11, false), "");
1744 LLVMValueRef g
= LLVMBuildAnd(ctx
->builder
, tmp
, i32_2047
, "");
1745 LLVMValueRef b
= LLVMBuildLShr(ctx
->builder
, data
, LLVMConstInt(ctx
->i32
, 22, false), "");
1747 loads
[0] = ac_to_integer(ctx
, ac_ufN_to_float(ctx
, r
, 5, 6));
1748 loads
[1] = ac_to_integer(ctx
, ac_ufN_to_float(ctx
, g
, 5, 6));
1749 loads
[2] = ac_to_integer(ctx
, ac_ufN_to_float(ctx
, b
, 5, 5));
1753 format
= AC_FETCH_FORMAT_FLOAT
;
1755 /* 2_10_10_10 data formats */
1756 LLVMValueRef data
= loads
[0];
1757 LLVMTypeRef i10
= LLVMIntTypeInContext(ctx
->context
, 10);
1758 LLVMTypeRef i2
= LLVMIntTypeInContext(ctx
->context
, 2);
1759 loads
[0] = LLVMBuildTrunc(ctx
->builder
, data
, i10
, "");
1760 tmp
= LLVMBuildLShr(ctx
->builder
, data
, LLVMConstInt(ctx
->i32
, 10, false), "");
1761 loads
[1] = LLVMBuildTrunc(ctx
->builder
, tmp
, i10
, "");
1762 tmp
= LLVMBuildLShr(ctx
->builder
, data
, LLVMConstInt(ctx
->i32
, 20, false), "");
1763 loads
[2] = LLVMBuildTrunc(ctx
->builder
, tmp
, i10
, "");
1764 tmp
= LLVMBuildLShr(ctx
->builder
, data
, LLVMConstInt(ctx
->i32
, 30, false), "");
1765 loads
[3] = LLVMBuildTrunc(ctx
->builder
, tmp
, i2
, "");
1771 if (format
== AC_FETCH_FORMAT_FLOAT
) {
1772 if (log_size
!= 2) {
1773 for (unsigned chan
= 0; chan
< num_channels
; ++chan
) {
1774 tmp
= ac_to_float(ctx
, loads
[chan
]);
1776 tmp
= LLVMBuildFPTrunc(ctx
->builder
, tmp
, ctx
->f32
, "");
1777 else if (log_size
== 1)
1778 tmp
= LLVMBuildFPExt(ctx
->builder
, tmp
, ctx
->f32
, "");
1779 loads
[chan
] = ac_to_integer(ctx
, tmp
);
1782 } else if (format
== AC_FETCH_FORMAT_UINT
) {
1783 if (log_size
!= 2) {
1784 for (unsigned chan
= 0; chan
< num_channels
; ++chan
)
1785 loads
[chan
] = LLVMBuildZExt(ctx
->builder
, loads
[chan
], ctx
->i32
, "");
1787 } else if (format
== AC_FETCH_FORMAT_SINT
) {
1788 if (log_size
!= 2) {
1789 for (unsigned chan
= 0; chan
< num_channels
; ++chan
)
1790 loads
[chan
] = LLVMBuildSExt(ctx
->builder
, loads
[chan
], ctx
->i32
, "");
1793 bool unsign
= format
== AC_FETCH_FORMAT_UNORM
||
1794 format
== AC_FETCH_FORMAT_USCALED
||
1795 format
== AC_FETCH_FORMAT_UINT
;
1797 for (unsigned chan
= 0; chan
< num_channels
; ++chan
) {
1799 tmp
= LLVMBuildUIToFP(ctx
->builder
, loads
[chan
], ctx
->f32
, "");
1801 tmp
= LLVMBuildSIToFP(ctx
->builder
, loads
[chan
], ctx
->f32
, "");
1804 LLVMValueRef scale
= NULL
;
1805 if (format
== AC_FETCH_FORMAT_FIXED
) {
1806 assert(log_size
== 2);
1807 scale
= LLVMConstReal(ctx
->f32
, 1.0 / 0x10000);
1808 } else if (format
== AC_FETCH_FORMAT_UNORM
) {
1809 unsigned bits
= LLVMGetIntTypeWidth(LLVMTypeOf(loads
[chan
]));
1810 scale
= LLVMConstReal(ctx
->f32
, 1.0 / (((uint64_t)1 << bits
) - 1));
1811 } else if (format
== AC_FETCH_FORMAT_SNORM
) {
1812 unsigned bits
= LLVMGetIntTypeWidth(LLVMTypeOf(loads
[chan
]));
1813 scale
= LLVMConstReal(ctx
->f32
, 1.0 / (((uint64_t)1 << (bits
- 1)) - 1));
1816 tmp
= LLVMBuildFMul(ctx
->builder
, tmp
, scale
, "");
1818 if (format
== AC_FETCH_FORMAT_SNORM
) {
1819 /* Clamp to [-1, 1] */
1820 LLVMValueRef neg_one
= LLVMConstReal(ctx
->f32
, -1.0);
1821 LLVMValueRef clamp
=
1822 LLVMBuildFCmp(ctx
->builder
, LLVMRealULT
, tmp
, neg_one
, "");
1823 tmp
= LLVMBuildSelect(ctx
->builder
, clamp
, neg_one
, tmp
, "");
1826 loads
[chan
] = ac_to_integer(ctx
, tmp
);
1830 while (num_channels
< 4) {
1831 if (format
== AC_FETCH_FORMAT_UINT
|| format
== AC_FETCH_FORMAT_SINT
) {
1832 loads
[num_channels
] = num_channels
== 3 ? ctx
->i32_1
: ctx
->i32_0
;
1834 loads
[num_channels
] = ac_to_integer(ctx
, num_channels
== 3 ? ctx
->f32_1
: ctx
->f32_0
);
1841 loads
[0] = loads
[2];
1845 return ac_build_gather_values(ctx
, loads
, 4);
1849 ac_build_tbuffer_store(struct ac_llvm_context
*ctx
,
1852 LLVMValueRef vindex
,
1853 LLVMValueRef voffset
,
1854 LLVMValueRef soffset
,
1855 LLVMValueRef immoffset
,
1856 unsigned num_channels
,
1859 unsigned cache_policy
,
1862 voffset
= LLVMBuildAdd(ctx
->builder
, voffset
? voffset
: ctx
->i32_0
,
1865 LLVMValueRef args
[7];
1867 args
[idx
++] = vdata
;
1868 args
[idx
++] = LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, "");
1870 args
[idx
++] = vindex
? vindex
: ctx
->i32_0
;
1871 args
[idx
++] = voffset
? voffset
: ctx
->i32_0
;
1872 args
[idx
++] = soffset
? soffset
: ctx
->i32_0
;
1873 args
[idx
++] = LLVMConstInt(ctx
->i32
, ac_get_tbuffer_format(ctx
->chip_class
, dfmt
, nfmt
), 0);
1874 args
[idx
++] = LLVMConstInt(ctx
->i32
, cache_policy
, 0);
1875 unsigned func
= !ac_has_vec3_support(ctx
->chip_class
, true) && num_channels
== 3 ? 4 : num_channels
;
1876 const char *indexing_kind
= structurized
? "struct" : "raw";
1877 char name
[256], type_name
[8];
1879 LLVMTypeRef type
= func
> 1 ? LLVMVectorType(ctx
->i32
, func
) : ctx
->i32
;
1880 ac_build_type_name_for_intr(type
, type_name
, sizeof(type_name
));
1882 snprintf(name
, sizeof(name
), "llvm.amdgcn.%s.tbuffer.store.%s",
1883 indexing_kind
, type_name
);
1885 ac_build_intrinsic(ctx
, name
, ctx
->voidt
, args
, idx
,
1886 AC_FUNC_ATTR_INACCESSIBLE_MEM_ONLY
);
1890 ac_build_struct_tbuffer_store(struct ac_llvm_context
*ctx
,
1893 LLVMValueRef vindex
,
1894 LLVMValueRef voffset
,
1895 LLVMValueRef soffset
,
1896 LLVMValueRef immoffset
,
1897 unsigned num_channels
,
1900 unsigned cache_policy
)
1902 ac_build_tbuffer_store(ctx
, rsrc
, vdata
, vindex
, voffset
, soffset
,
1903 immoffset
, num_channels
, dfmt
, nfmt
, cache_policy
,
1908 ac_build_raw_tbuffer_store(struct ac_llvm_context
*ctx
,
1911 LLVMValueRef voffset
,
1912 LLVMValueRef soffset
,
1913 LLVMValueRef immoffset
,
1914 unsigned num_channels
,
1917 unsigned cache_policy
)
1919 ac_build_tbuffer_store(ctx
, rsrc
, vdata
, NULL
, voffset
, soffset
,
1920 immoffset
, num_channels
, dfmt
, nfmt
, cache_policy
,
1925 ac_build_tbuffer_store_short(struct ac_llvm_context
*ctx
,
1928 LLVMValueRef voffset
,
1929 LLVMValueRef soffset
,
1930 unsigned cache_policy
)
1932 vdata
= LLVMBuildBitCast(ctx
->builder
, vdata
, ctx
->i16
, "");
1934 if (LLVM_VERSION_MAJOR
>= 9) {
1935 /* LLVM 9+ supports i8/i16 with struct/raw intrinsics. */
1936 ac_build_buffer_store_common(ctx
, rsrc
, vdata
, NULL
,
1937 voffset
, soffset
, cache_policy
,
1940 unsigned dfmt
= V_008F0C_BUF_DATA_FORMAT_16
;
1941 unsigned nfmt
= V_008F0C_BUF_NUM_FORMAT_UINT
;
1943 vdata
= LLVMBuildZExt(ctx
->builder
, vdata
, ctx
->i32
, "");
1945 ac_build_raw_tbuffer_store(ctx
, rsrc
, vdata
, voffset
, soffset
,
1946 ctx
->i32_0
, 1, dfmt
, nfmt
, cache_policy
);
1951 ac_build_tbuffer_store_byte(struct ac_llvm_context
*ctx
,
1954 LLVMValueRef voffset
,
1955 LLVMValueRef soffset
,
1956 unsigned cache_policy
)
1958 vdata
= LLVMBuildBitCast(ctx
->builder
, vdata
, ctx
->i8
, "");
1960 if (LLVM_VERSION_MAJOR
>= 9) {
1961 /* LLVM 9+ supports i8/i16 with struct/raw intrinsics. */
1962 ac_build_buffer_store_common(ctx
, rsrc
, vdata
, NULL
,
1963 voffset
, soffset
, cache_policy
,
1966 unsigned dfmt
= V_008F0C_BUF_DATA_FORMAT_8
;
1967 unsigned nfmt
= V_008F0C_BUF_NUM_FORMAT_UINT
;
1969 vdata
= LLVMBuildZExt(ctx
->builder
, vdata
, ctx
->i32
, "");
1971 ac_build_raw_tbuffer_store(ctx
, rsrc
, vdata
, voffset
, soffset
,
1972 ctx
->i32_0
, 1, dfmt
, nfmt
, cache_policy
);
1976 * Set range metadata on an instruction. This can only be used on load and
1977 * call instructions. If you know an instruction can only produce the values
1978 * 0, 1, 2, you would do set_range_metadata(value, 0, 3);
1979 * \p lo is the minimum value inclusive.
1980 * \p hi is the maximum value exclusive.
1982 static void set_range_metadata(struct ac_llvm_context
*ctx
,
1983 LLVMValueRef value
, unsigned lo
, unsigned hi
)
1985 LLVMValueRef range_md
, md_args
[2];
1986 LLVMTypeRef type
= LLVMTypeOf(value
);
1987 LLVMContextRef context
= LLVMGetTypeContext(type
);
1989 md_args
[0] = LLVMConstInt(type
, lo
, false);
1990 md_args
[1] = LLVMConstInt(type
, hi
, false);
1991 range_md
= LLVMMDNodeInContext(context
, md_args
, 2);
1992 LLVMSetMetadata(value
, ctx
->range_md_kind
, range_md
);
1996 ac_get_thread_id(struct ac_llvm_context
*ctx
)
2000 LLVMValueRef tid_args
[2];
2001 tid_args
[0] = LLVMConstInt(ctx
->i32
, 0xffffffff, false);
2002 tid_args
[1] = ctx
->i32_0
;
2003 tid_args
[1] = ac_build_intrinsic(ctx
,
2004 "llvm.amdgcn.mbcnt.lo", ctx
->i32
,
2005 tid_args
, 2, AC_FUNC_ATTR_READNONE
);
2007 if (ctx
->wave_size
== 32) {
2010 tid
= ac_build_intrinsic(ctx
, "llvm.amdgcn.mbcnt.hi",
2012 2, AC_FUNC_ATTR_READNONE
);
2014 set_range_metadata(ctx
, tid
, 0, ctx
->wave_size
);
2019 * AMD GCN implements derivatives using the local data store (LDS)
2020 * All writes to the LDS happen in all executing threads at
2021 * the same time. TID is the Thread ID for the current
2022 * thread and is a value between 0 and 63, representing
2023 * the thread's position in the wavefront.
2025 * For the pixel shader threads are grouped into quads of four pixels.
2026 * The TIDs of the pixels of a quad are:
2034 * So, masking the TID with 0xfffffffc yields the TID of the top left pixel
2035 * of the quad, masking with 0xfffffffd yields the TID of the top pixel of
2036 * the current pixel's column, and masking with 0xfffffffe yields the TID
2037 * of the left pixel of the current pixel's row.
2039 * Adding 1 yields the TID of the pixel to the right of the left pixel, and
2040 * adding 2 yields the TID of the pixel below the top pixel.
2043 ac_build_ddxy(struct ac_llvm_context
*ctx
,
2048 unsigned tl_lanes
[4], trbl_lanes
[4];
2049 char name
[32], type
[8];
2050 LLVMValueRef tl
, trbl
;
2051 LLVMTypeRef result_type
;
2052 LLVMValueRef result
;
2054 result_type
= ac_to_float_type(ctx
, LLVMTypeOf(val
));
2056 if (result_type
== ctx
->f16
)
2057 val
= LLVMBuildZExt(ctx
->builder
, val
, ctx
->i32
, "");
2059 for (unsigned i
= 0; i
< 4; ++i
) {
2060 tl_lanes
[i
] = i
& mask
;
2061 trbl_lanes
[i
] = (i
& mask
) + idx
;
2064 tl
= ac_build_quad_swizzle(ctx
, val
,
2065 tl_lanes
[0], tl_lanes
[1],
2066 tl_lanes
[2], tl_lanes
[3]);
2067 trbl
= ac_build_quad_swizzle(ctx
, val
,
2068 trbl_lanes
[0], trbl_lanes
[1],
2069 trbl_lanes
[2], trbl_lanes
[3]);
2071 if (result_type
== ctx
->f16
) {
2072 tl
= LLVMBuildTrunc(ctx
->builder
, tl
, ctx
->i16
, "");
2073 trbl
= LLVMBuildTrunc(ctx
->builder
, trbl
, ctx
->i16
, "");
2076 tl
= LLVMBuildBitCast(ctx
->builder
, tl
, result_type
, "");
2077 trbl
= LLVMBuildBitCast(ctx
->builder
, trbl
, result_type
, "");
2078 result
= LLVMBuildFSub(ctx
->builder
, trbl
, tl
, "");
2080 ac_build_type_name_for_intr(result_type
, type
, sizeof(type
));
2081 snprintf(name
, sizeof(name
), "llvm.amdgcn.wqm.%s", type
);
2083 return ac_build_intrinsic(ctx
, name
, result_type
, &result
, 1, 0);
2087 ac_build_sendmsg(struct ac_llvm_context
*ctx
,
2089 LLVMValueRef wave_id
)
2091 LLVMValueRef args
[2];
2092 args
[0] = LLVMConstInt(ctx
->i32
, msg
, false);
2094 ac_build_intrinsic(ctx
, "llvm.amdgcn.s.sendmsg", ctx
->voidt
, args
, 2, 0);
2098 ac_build_imsb(struct ac_llvm_context
*ctx
,
2100 LLVMTypeRef dst_type
)
2102 LLVMValueRef msb
= ac_build_intrinsic(ctx
, "llvm.amdgcn.sffbh.i32",
2104 AC_FUNC_ATTR_READNONE
);
2106 /* The HW returns the last bit index from MSB, but NIR/TGSI wants
2107 * the index from LSB. Invert it by doing "31 - msb". */
2108 msb
= LLVMBuildSub(ctx
->builder
, LLVMConstInt(ctx
->i32
, 31, false),
2111 LLVMValueRef all_ones
= LLVMConstInt(ctx
->i32
, -1, true);
2112 LLVMValueRef cond
= LLVMBuildOr(ctx
->builder
,
2113 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
2114 arg
, ctx
->i32_0
, ""),
2115 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
2116 arg
, all_ones
, ""), "");
2118 return LLVMBuildSelect(ctx
->builder
, cond
, all_ones
, msb
, "");
2122 ac_build_umsb(struct ac_llvm_context
*ctx
,
2124 LLVMTypeRef dst_type
)
2126 const char *intrin_name
;
2128 LLVMValueRef highest_bit
;
2132 bitsize
= ac_get_elem_bits(ctx
, LLVMTypeOf(arg
));
2135 intrin_name
= "llvm.ctlz.i64";
2137 highest_bit
= LLVMConstInt(ctx
->i64
, 63, false);
2141 intrin_name
= "llvm.ctlz.i32";
2143 highest_bit
= LLVMConstInt(ctx
->i32
, 31, false);
2147 intrin_name
= "llvm.ctlz.i16";
2149 highest_bit
= LLVMConstInt(ctx
->i16
, 15, false);
2153 intrin_name
= "llvm.ctlz.i8";
2155 highest_bit
= LLVMConstInt(ctx
->i8
, 7, false);
2159 unreachable(!"invalid bitsize");
2163 LLVMValueRef params
[2] = {
2168 LLVMValueRef msb
= ac_build_intrinsic(ctx
, intrin_name
, type
,
2170 AC_FUNC_ATTR_READNONE
);
2172 /* The HW returns the last bit index from MSB, but TGSI/NIR wants
2173 * the index from LSB. Invert it by doing "31 - msb". */
2174 msb
= LLVMBuildSub(ctx
->builder
, highest_bit
, msb
, "");
2176 if (bitsize
== 64) {
2177 msb
= LLVMBuildTrunc(ctx
->builder
, msb
, ctx
->i32
, "");
2178 } else if (bitsize
< 32) {
2179 msb
= LLVMBuildSExt(ctx
->builder
, msb
, ctx
->i32
, "");
2182 /* check for zero */
2183 return LLVMBuildSelect(ctx
->builder
,
2184 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, arg
, zero
, ""),
2185 LLVMConstInt(ctx
->i32
, -1, true), msb
, "");
2188 LLVMValueRef
ac_build_fmin(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
2191 char name
[64], type
[64];
2193 ac_build_type_name_for_intr(LLVMTypeOf(a
), type
, sizeof(type
));
2194 snprintf(name
, sizeof(name
), "llvm.minnum.%s", type
);
2195 LLVMValueRef args
[2] = {a
, b
};
2196 return ac_build_intrinsic(ctx
, name
, LLVMTypeOf(a
), args
, 2,
2197 AC_FUNC_ATTR_READNONE
);
2200 LLVMValueRef
ac_build_fmax(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
2203 char name
[64], type
[64];
2205 ac_build_type_name_for_intr(LLVMTypeOf(a
), type
, sizeof(type
));
2206 snprintf(name
, sizeof(name
), "llvm.maxnum.%s", type
);
2207 LLVMValueRef args
[2] = {a
, b
};
2208 return ac_build_intrinsic(ctx
, name
, LLVMTypeOf(a
), args
, 2,
2209 AC_FUNC_ATTR_READNONE
);
2212 LLVMValueRef
ac_build_imin(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
2215 LLVMValueRef cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSLE
, a
, b
, "");
2216 return LLVMBuildSelect(ctx
->builder
, cmp
, a
, b
, "");
2219 LLVMValueRef
ac_build_imax(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
2222 LLVMValueRef cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGT
, a
, b
, "");
2223 return LLVMBuildSelect(ctx
->builder
, cmp
, a
, b
, "");
2226 LLVMValueRef
ac_build_umin(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
2229 LLVMValueRef cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntULE
, a
, b
, "");
2230 return LLVMBuildSelect(ctx
->builder
, cmp
, a
, b
, "");
2233 LLVMValueRef
ac_build_umax(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
2236 LLVMValueRef cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntUGE
, a
, b
, "");
2237 return LLVMBuildSelect(ctx
->builder
, cmp
, a
, b
, "");
2240 LLVMValueRef
ac_build_clamp(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
2242 LLVMTypeRef t
= LLVMTypeOf(value
);
2243 return ac_build_fmin(ctx
, ac_build_fmax(ctx
, value
, LLVMConstReal(t
, 0.0)),
2244 LLVMConstReal(t
, 1.0));
2247 void ac_build_export(struct ac_llvm_context
*ctx
, struct ac_export_args
*a
)
2249 LLVMValueRef args
[9];
2251 args
[0] = LLVMConstInt(ctx
->i32
, a
->target
, 0);
2252 args
[1] = LLVMConstInt(ctx
->i32
, a
->enabled_channels
, 0);
2255 args
[2] = LLVMBuildBitCast(ctx
->builder
, a
->out
[0],
2257 args
[3] = LLVMBuildBitCast(ctx
->builder
, a
->out
[1],
2259 args
[4] = LLVMConstInt(ctx
->i1
, a
->done
, 0);
2260 args
[5] = LLVMConstInt(ctx
->i1
, a
->valid_mask
, 0);
2262 ac_build_intrinsic(ctx
, "llvm.amdgcn.exp.compr.v2i16",
2263 ctx
->voidt
, args
, 6, 0);
2265 args
[2] = a
->out
[0];
2266 args
[3] = a
->out
[1];
2267 args
[4] = a
->out
[2];
2268 args
[5] = a
->out
[3];
2269 args
[6] = LLVMConstInt(ctx
->i1
, a
->done
, 0);
2270 args
[7] = LLVMConstInt(ctx
->i1
, a
->valid_mask
, 0);
2272 ac_build_intrinsic(ctx
, "llvm.amdgcn.exp.f32",
2273 ctx
->voidt
, args
, 8, 0);
2277 void ac_build_export_null(struct ac_llvm_context
*ctx
)
2279 struct ac_export_args args
;
2281 args
.enabled_channels
= 0x0; /* enabled channels */
2282 args
.valid_mask
= 1; /* whether the EXEC mask is valid */
2283 args
.done
= 1; /* DONE bit */
2284 args
.target
= V_008DFC_SQ_EXP_NULL
;
2285 args
.compr
= 0; /* COMPR flag (0 = 32-bit export) */
2286 args
.out
[0] = LLVMGetUndef(ctx
->f32
); /* R */
2287 args
.out
[1] = LLVMGetUndef(ctx
->f32
); /* G */
2288 args
.out
[2] = LLVMGetUndef(ctx
->f32
); /* B */
2289 args
.out
[3] = LLVMGetUndef(ctx
->f32
); /* A */
2291 ac_build_export(ctx
, &args
);
2294 static unsigned ac_num_coords(enum ac_image_dim dim
)
2300 case ac_image_1darray
:
2304 case ac_image_2darray
:
2305 case ac_image_2dmsaa
:
2307 case ac_image_2darraymsaa
:
2310 unreachable("ac_num_coords: bad dim");
2314 static unsigned ac_num_derivs(enum ac_image_dim dim
)
2318 case ac_image_1darray
:
2321 case ac_image_2darray
:
2326 case ac_image_2dmsaa
:
2327 case ac_image_2darraymsaa
:
2329 unreachable("derivatives not supported");
2333 static const char *get_atomic_name(enum ac_atomic_op op
)
2336 case ac_atomic_swap
: return "swap";
2337 case ac_atomic_add
: return "add";
2338 case ac_atomic_sub
: return "sub";
2339 case ac_atomic_smin
: return "smin";
2340 case ac_atomic_umin
: return "umin";
2341 case ac_atomic_smax
: return "smax";
2342 case ac_atomic_umax
: return "umax";
2343 case ac_atomic_and
: return "and";
2344 case ac_atomic_or
: return "or";
2345 case ac_atomic_xor
: return "xor";
2346 case ac_atomic_inc_wrap
: return "inc";
2347 case ac_atomic_dec_wrap
: return "dec";
2349 unreachable("bad atomic op");
2352 LLVMValueRef
ac_build_image_opcode(struct ac_llvm_context
*ctx
,
2353 struct ac_image_args
*a
)
2355 const char *overload
[3] = { "", "", "" };
2356 unsigned num_overloads
= 0;
2357 LLVMValueRef args
[18];
2358 unsigned num_args
= 0;
2359 enum ac_image_dim dim
= a
->dim
;
2361 assert(!a
->lod
|| a
->lod
== ctx
->i32_0
|| a
->lod
== ctx
->f32_0
||
2363 assert((a
->opcode
!= ac_image_get_resinfo
&& a
->opcode
!= ac_image_load_mip
&&
2364 a
->opcode
!= ac_image_store_mip
) ||
2366 assert(a
->opcode
== ac_image_sample
|| a
->opcode
== ac_image_gather4
||
2367 (!a
->compare
&& !a
->offset
));
2368 assert((a
->opcode
== ac_image_sample
|| a
->opcode
== ac_image_gather4
||
2369 a
->opcode
== ac_image_get_lod
) ||
2371 assert((a
->bias
? 1 : 0) +
2373 (a
->level_zero
? 1 : 0) +
2374 (a
->derivs
[0] ? 1 : 0) <= 1);
2375 assert((a
->min_lod
? 1 : 0) +
2377 (a
->level_zero
? 1 : 0) <= 1);
2379 if (a
->opcode
== ac_image_get_lod
) {
2381 case ac_image_1darray
:
2384 case ac_image_2darray
:
2393 bool sample
= a
->opcode
== ac_image_sample
||
2394 a
->opcode
== ac_image_gather4
||
2395 a
->opcode
== ac_image_get_lod
;
2396 bool atomic
= a
->opcode
== ac_image_atomic
||
2397 a
->opcode
== ac_image_atomic_cmpswap
;
2398 bool load
= a
->opcode
== ac_image_sample
||
2399 a
->opcode
== ac_image_gather4
||
2400 a
->opcode
== ac_image_load
||
2401 a
->opcode
== ac_image_load_mip
;
2402 LLVMTypeRef coord_type
= sample
? ctx
->f32
: ctx
->i32
;
2404 if (atomic
|| a
->opcode
== ac_image_store
|| a
->opcode
== ac_image_store_mip
) {
2405 args
[num_args
++] = a
->data
[0];
2406 if (a
->opcode
== ac_image_atomic_cmpswap
)
2407 args
[num_args
++] = a
->data
[1];
2411 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->dmask
, false);
2414 args
[num_args
++] = ac_to_integer(ctx
, a
->offset
);
2416 args
[num_args
++] = ac_to_float(ctx
, a
->bias
);
2417 overload
[num_overloads
++] = ".f32";
2420 args
[num_args
++] = ac_to_float(ctx
, a
->compare
);
2422 unsigned count
= ac_num_derivs(dim
);
2423 for (unsigned i
= 0; i
< count
; ++i
)
2424 args
[num_args
++] = ac_to_float(ctx
, a
->derivs
[i
]);
2425 overload
[num_overloads
++] = ".f32";
2427 unsigned num_coords
=
2428 a
->opcode
!= ac_image_get_resinfo
? ac_num_coords(dim
) : 0;
2429 for (unsigned i
= 0; i
< num_coords
; ++i
)
2430 args
[num_args
++] = LLVMBuildBitCast(ctx
->builder
, a
->coords
[i
], coord_type
, "");
2432 args
[num_args
++] = LLVMBuildBitCast(ctx
->builder
, a
->lod
, coord_type
, "");
2434 args
[num_args
++] = LLVMBuildBitCast(ctx
->builder
, a
->min_lod
, coord_type
, "");
2436 overload
[num_overloads
++] = sample
? ".f32" : ".i32";
2438 args
[num_args
++] = a
->resource
;
2440 args
[num_args
++] = a
->sampler
;
2441 args
[num_args
++] = LLVMConstInt(ctx
->i1
, a
->unorm
, false);
2444 args
[num_args
++] = ctx
->i32_0
; /* texfailctrl */
2445 args
[num_args
++] = LLVMConstInt(ctx
->i32
,
2446 load
? get_load_cache_policy(ctx
, a
->cache_policy
) :
2447 a
->cache_policy
, false);
2450 const char *atomic_subop
= "";
2451 switch (a
->opcode
) {
2452 case ac_image_sample
: name
= "sample"; break;
2453 case ac_image_gather4
: name
= "gather4"; break;
2454 case ac_image_load
: name
= "load"; break;
2455 case ac_image_load_mip
: name
= "load.mip"; break;
2456 case ac_image_store
: name
= "store"; break;
2457 case ac_image_store_mip
: name
= "store.mip"; break;
2458 case ac_image_atomic
:
2460 atomic_subop
= get_atomic_name(a
->atomic
);
2462 case ac_image_atomic_cmpswap
:
2464 atomic_subop
= "cmpswap";
2466 case ac_image_get_lod
: name
= "getlod"; break;
2467 case ac_image_get_resinfo
: name
= "getresinfo"; break;
2468 default: unreachable("invalid image opcode");
2471 const char *dimname
;
2473 case ac_image_1d
: dimname
= "1d"; break;
2474 case ac_image_2d
: dimname
= "2d"; break;
2475 case ac_image_3d
: dimname
= "3d"; break;
2476 case ac_image_cube
: dimname
= "cube"; break;
2477 case ac_image_1darray
: dimname
= "1darray"; break;
2478 case ac_image_2darray
: dimname
= "2darray"; break;
2479 case ac_image_2dmsaa
: dimname
= "2dmsaa"; break;
2480 case ac_image_2darraymsaa
: dimname
= "2darraymsaa"; break;
2481 default: unreachable("invalid dim");
2485 a
->lod
&& (a
->opcode
== ac_image_sample
|| a
->opcode
== ac_image_gather4
);
2487 snprintf(intr_name
, sizeof(intr_name
),
2488 "llvm.amdgcn.image.%s%s" /* base name */
2489 "%s%s%s%s" /* sample/gather modifiers */
2490 ".%s.%s%s%s%s", /* dimension and type overloads */
2492 a
->compare
? ".c" : "",
2495 a
->derivs
[0] ? ".d" :
2496 a
->level_zero
? ".lz" : "",
2497 a
->min_lod
? ".cl" : "",
2498 a
->offset
? ".o" : "",
2500 atomic
? "i32" : "v4f32",
2501 overload
[0], overload
[1], overload
[2]);
2506 else if (a
->opcode
== ac_image_store
|| a
->opcode
== ac_image_store_mip
)
2511 LLVMValueRef result
=
2512 ac_build_intrinsic(ctx
, intr_name
, retty
, args
, num_args
,
2514 if (!sample
&& retty
== ctx
->v4f32
) {
2515 result
= LLVMBuildBitCast(ctx
->builder
, result
,
2521 LLVMValueRef
ac_build_image_get_sample_count(struct ac_llvm_context
*ctx
,
2524 LLVMValueRef samples
;
2526 /* Read the samples from the descriptor directly.
2527 * Hardware doesn't have any instruction for this.
2529 samples
= LLVMBuildExtractElement(ctx
->builder
, rsrc
,
2530 LLVMConstInt(ctx
->i32
, 3, 0), "");
2531 samples
= LLVMBuildLShr(ctx
->builder
, samples
,
2532 LLVMConstInt(ctx
->i32
, 16, 0), "");
2533 samples
= LLVMBuildAnd(ctx
->builder
, samples
,
2534 LLVMConstInt(ctx
->i32
, 0xf, 0), "");
2535 samples
= LLVMBuildShl(ctx
->builder
, ctx
->i32_1
,
2540 LLVMValueRef
ac_build_cvt_pkrtz_f16(struct ac_llvm_context
*ctx
,
2541 LLVMValueRef args
[2])
2543 return ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pkrtz", ctx
->v2f16
,
2544 args
, 2, AC_FUNC_ATTR_READNONE
);
2547 LLVMValueRef
ac_build_cvt_pknorm_i16(struct ac_llvm_context
*ctx
,
2548 LLVMValueRef args
[2])
2551 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pknorm.i16",
2552 ctx
->v2i16
, args
, 2,
2553 AC_FUNC_ATTR_READNONE
);
2554 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
2557 LLVMValueRef
ac_build_cvt_pknorm_u16(struct ac_llvm_context
*ctx
,
2558 LLVMValueRef args
[2])
2561 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pknorm.u16",
2562 ctx
->v2i16
, args
, 2,
2563 AC_FUNC_ATTR_READNONE
);
2564 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
2567 /* The 8-bit and 10-bit clamping is for HW workarounds. */
2568 LLVMValueRef
ac_build_cvt_pk_i16(struct ac_llvm_context
*ctx
,
2569 LLVMValueRef args
[2], unsigned bits
, bool hi
)
2571 assert(bits
== 8 || bits
== 10 || bits
== 16);
2573 LLVMValueRef max_rgb
= LLVMConstInt(ctx
->i32
,
2574 bits
== 8 ? 127 : bits
== 10 ? 511 : 32767, 0);
2575 LLVMValueRef min_rgb
= LLVMConstInt(ctx
->i32
,
2576 bits
== 8 ? -128 : bits
== 10 ? -512 : -32768, 0);
2577 LLVMValueRef max_alpha
=
2578 bits
!= 10 ? max_rgb
: ctx
->i32_1
;
2579 LLVMValueRef min_alpha
=
2580 bits
!= 10 ? min_rgb
: LLVMConstInt(ctx
->i32
, -2, 0);
2584 for (int i
= 0; i
< 2; i
++) {
2585 bool alpha
= hi
&& i
== 1;
2586 args
[i
] = ac_build_imin(ctx
, args
[i
],
2587 alpha
? max_alpha
: max_rgb
);
2588 args
[i
] = ac_build_imax(ctx
, args
[i
],
2589 alpha
? min_alpha
: min_rgb
);
2594 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pk.i16",
2595 ctx
->v2i16
, args
, 2,
2596 AC_FUNC_ATTR_READNONE
);
2597 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
2600 /* The 8-bit and 10-bit clamping is for HW workarounds. */
2601 LLVMValueRef
ac_build_cvt_pk_u16(struct ac_llvm_context
*ctx
,
2602 LLVMValueRef args
[2], unsigned bits
, bool hi
)
2604 assert(bits
== 8 || bits
== 10 || bits
== 16);
2606 LLVMValueRef max_rgb
= LLVMConstInt(ctx
->i32
,
2607 bits
== 8 ? 255 : bits
== 10 ? 1023 : 65535, 0);
2608 LLVMValueRef max_alpha
=
2609 bits
!= 10 ? max_rgb
: LLVMConstInt(ctx
->i32
, 3, 0);
2613 for (int i
= 0; i
< 2; i
++) {
2614 bool alpha
= hi
&& i
== 1;
2615 args
[i
] = ac_build_umin(ctx
, args
[i
],
2616 alpha
? max_alpha
: max_rgb
);
2621 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pk.u16",
2622 ctx
->v2i16
, args
, 2,
2623 AC_FUNC_ATTR_READNONE
);
2624 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
2627 LLVMValueRef
ac_build_wqm_vote(struct ac_llvm_context
*ctx
, LLVMValueRef i1
)
2629 return ac_build_intrinsic(ctx
, "llvm.amdgcn.wqm.vote", ctx
->i1
,
2630 &i1
, 1, AC_FUNC_ATTR_READNONE
);
2633 void ac_build_kill_if_false(struct ac_llvm_context
*ctx
, LLVMValueRef i1
)
2635 ac_build_intrinsic(ctx
, "llvm.amdgcn.kill", ctx
->voidt
,
2639 LLVMValueRef
ac_build_bfe(struct ac_llvm_context
*ctx
, LLVMValueRef input
,
2640 LLVMValueRef offset
, LLVMValueRef width
,
2643 LLVMValueRef args
[] = {
2649 return ac_build_intrinsic(ctx
, is_signed
? "llvm.amdgcn.sbfe.i32" :
2650 "llvm.amdgcn.ubfe.i32",
2651 ctx
->i32
, args
, 3, AC_FUNC_ATTR_READNONE
);
2655 LLVMValueRef
ac_build_imad(struct ac_llvm_context
*ctx
, LLVMValueRef s0
,
2656 LLVMValueRef s1
, LLVMValueRef s2
)
2658 return LLVMBuildAdd(ctx
->builder
,
2659 LLVMBuildMul(ctx
->builder
, s0
, s1
, ""), s2
, "");
2662 LLVMValueRef
ac_build_fmad(struct ac_llvm_context
*ctx
, LLVMValueRef s0
,
2663 LLVMValueRef s1
, LLVMValueRef s2
)
2665 /* FMA is better on GFX10, because it has FMA units instead of MUL-ADD units. */
2666 if (ctx
->chip_class
>= GFX10
) {
2667 return ac_build_intrinsic(ctx
, "llvm.fma.f32", ctx
->f32
,
2668 (LLVMValueRef
[]) {s0
, s1
, s2
}, 3,
2669 AC_FUNC_ATTR_READNONE
);
2672 return LLVMBuildFAdd(ctx
->builder
,
2673 LLVMBuildFMul(ctx
->builder
, s0
, s1
, ""), s2
, "");
2676 void ac_build_waitcnt(struct ac_llvm_context
*ctx
, unsigned wait_flags
)
2681 unsigned lgkmcnt
= 63;
2682 unsigned vmcnt
= ctx
->chip_class
>= GFX9
? 63 : 15;
2683 unsigned vscnt
= 63;
2685 if (wait_flags
& AC_WAIT_LGKM
)
2687 if (wait_flags
& AC_WAIT_VLOAD
)
2690 if (wait_flags
& AC_WAIT_VSTORE
) {
2691 if (ctx
->chip_class
>= GFX10
)
2697 /* There is no intrinsic for vscnt(0), so use a fence. */
2698 if ((wait_flags
& AC_WAIT_LGKM
&&
2699 wait_flags
& AC_WAIT_VLOAD
&&
2700 wait_flags
& AC_WAIT_VSTORE
) ||
2702 LLVMBuildFence(ctx
->builder
, LLVMAtomicOrderingRelease
, false, "");
2706 unsigned simm16
= (lgkmcnt
<< 8) |
2707 (7 << 4) | /* expcnt */
2709 ((vmcnt
>> 4) << 14);
2711 LLVMValueRef args
[1] = {
2712 LLVMConstInt(ctx
->i32
, simm16
, false),
2714 ac_build_intrinsic(ctx
, "llvm.amdgcn.s.waitcnt",
2715 ctx
->voidt
, args
, 1, 0);
2718 LLVMValueRef
ac_build_fmed3(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
2719 LLVMValueRef src1
, LLVMValueRef src2
,
2722 LLVMValueRef result
;
2724 if (bitsize
== 64 || (bitsize
== 16 && ctx
->chip_class
<= GFX8
)) {
2725 /* Lower 64-bit fmed because LLVM doesn't expose an intrinsic,
2726 * or lower 16-bit fmed because it's only supported on GFX9+.
2728 LLVMValueRef min1
, min2
, max1
;
2730 min1
= ac_build_fmin(ctx
, src0
, src1
);
2731 max1
= ac_build_fmax(ctx
, src0
, src1
);
2732 min2
= ac_build_fmin(ctx
, max1
, src2
);
2734 result
= ac_build_fmax(ctx
, min2
, min1
);
2739 if (bitsize
== 16) {
2740 intr
= "llvm.amdgcn.fmed3.f16";
2743 assert(bitsize
== 32);
2744 intr
= "llvm.amdgcn.fmed3.f32";
2748 LLVMValueRef params
[] = {
2754 result
= ac_build_intrinsic(ctx
, intr
, type
, params
, 3,
2755 AC_FUNC_ATTR_READNONE
);
2758 if (ctx
->chip_class
< GFX9
&& bitsize
== 32) {
2759 /* Only pre-GFX9 chips do not flush denorms. */
2760 result
= ac_build_canonicalize(ctx
, result
, bitsize
);
2766 LLVMValueRef
ac_build_fract(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
2772 if (bitsize
== 16) {
2773 intr
= "llvm.amdgcn.fract.f16";
2775 } else if (bitsize
== 32) {
2776 intr
= "llvm.amdgcn.fract.f32";
2779 intr
= "llvm.amdgcn.fract.f64";
2783 LLVMValueRef params
[] = {
2786 return ac_build_intrinsic(ctx
, intr
, type
, params
, 1,
2787 AC_FUNC_ATTR_READNONE
);
2790 LLVMValueRef
ac_build_isign(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
2793 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->context
, bitsize
);
2794 LLVMValueRef zero
= LLVMConstInt(type
, 0, false);
2795 LLVMValueRef one
= LLVMConstInt(type
, 1, false);
2797 LLVMValueRef cmp
, val
;
2798 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGT
, src0
, zero
, "");
2799 val
= LLVMBuildSelect(ctx
->builder
, cmp
, one
, src0
, "");
2800 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGE
, val
, zero
, "");
2801 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstInt(type
, -1, true), "");
2805 LLVMValueRef
ac_build_fsign(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
2808 LLVMValueRef cmp
, val
, zero
, one
;
2811 if (bitsize
== 16) {
2815 } else if (bitsize
== 32) {
2825 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGT
, src0
, zero
, "");
2826 val
= LLVMBuildSelect(ctx
->builder
, cmp
, one
, src0
, "");
2827 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGE
, val
, zero
, "");
2828 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstReal(type
, -1.0), "");
2832 LLVMValueRef
ac_build_bit_count(struct ac_llvm_context
*ctx
, LLVMValueRef src0
)
2834 LLVMValueRef result
;
2837 bitsize
= ac_get_elem_bits(ctx
, LLVMTypeOf(src0
));
2841 result
= ac_build_intrinsic(ctx
, "llvm.ctpop.i128", ctx
->i128
,
2842 (LLVMValueRef
[]) { src0
}, 1,
2843 AC_FUNC_ATTR_READNONE
);
2844 result
= LLVMBuildTrunc(ctx
->builder
, result
, ctx
->i32
, "");
2847 result
= ac_build_intrinsic(ctx
, "llvm.ctpop.i64", ctx
->i64
,
2848 (LLVMValueRef
[]) { src0
}, 1,
2849 AC_FUNC_ATTR_READNONE
);
2851 result
= LLVMBuildTrunc(ctx
->builder
, result
, ctx
->i32
, "");
2854 result
= ac_build_intrinsic(ctx
, "llvm.ctpop.i32", ctx
->i32
,
2855 (LLVMValueRef
[]) { src0
}, 1,
2856 AC_FUNC_ATTR_READNONE
);
2859 result
= ac_build_intrinsic(ctx
, "llvm.ctpop.i16", ctx
->i16
,
2860 (LLVMValueRef
[]) { src0
}, 1,
2861 AC_FUNC_ATTR_READNONE
);
2863 result
= LLVMBuildZExt(ctx
->builder
, result
, ctx
->i32
, "");
2866 result
= ac_build_intrinsic(ctx
, "llvm.ctpop.i8", ctx
->i8
,
2867 (LLVMValueRef
[]) { src0
}, 1,
2868 AC_FUNC_ATTR_READNONE
);
2870 result
= LLVMBuildZExt(ctx
->builder
, result
, ctx
->i32
, "");
2873 unreachable(!"invalid bitsize");
2880 LLVMValueRef
ac_build_bitfield_reverse(struct ac_llvm_context
*ctx
,
2883 LLVMValueRef result
;
2886 bitsize
= ac_get_elem_bits(ctx
, LLVMTypeOf(src0
));
2890 result
= ac_build_intrinsic(ctx
, "llvm.bitreverse.i64", ctx
->i64
,
2891 (LLVMValueRef
[]) { src0
}, 1,
2892 AC_FUNC_ATTR_READNONE
);
2894 result
= LLVMBuildTrunc(ctx
->builder
, result
, ctx
->i32
, "");
2897 result
= ac_build_intrinsic(ctx
, "llvm.bitreverse.i32", ctx
->i32
,
2898 (LLVMValueRef
[]) { src0
}, 1,
2899 AC_FUNC_ATTR_READNONE
);
2902 result
= ac_build_intrinsic(ctx
, "llvm.bitreverse.i16", ctx
->i16
,
2903 (LLVMValueRef
[]) { src0
}, 1,
2904 AC_FUNC_ATTR_READNONE
);
2906 result
= LLVMBuildZExt(ctx
->builder
, result
, ctx
->i32
, "");
2909 result
= ac_build_intrinsic(ctx
, "llvm.bitreverse.i8", ctx
->i8
,
2910 (LLVMValueRef
[]) { src0
}, 1,
2911 AC_FUNC_ATTR_READNONE
);
2913 result
= LLVMBuildZExt(ctx
->builder
, result
, ctx
->i32
, "");
2916 unreachable(!"invalid bitsize");
2923 #define AC_EXP_TARGET 0
2924 #define AC_EXP_ENABLED_CHANNELS 1
2925 #define AC_EXP_OUT0 2
2933 struct ac_vs_exp_chan
2937 enum ac_ir_type type
;
2940 struct ac_vs_exp_inst
{
2943 struct ac_vs_exp_chan chan
[4];
2946 struct ac_vs_exports
{
2948 struct ac_vs_exp_inst exp
[VARYING_SLOT_MAX
];
2951 /* Return true if the PARAM export has been eliminated. */
2952 static bool ac_eliminate_const_output(uint8_t *vs_output_param_offset
,
2953 uint32_t num_outputs
,
2954 struct ac_vs_exp_inst
*exp
)
2956 unsigned i
, default_val
; /* SPI_PS_INPUT_CNTL_i.DEFAULT_VAL */
2957 bool is_zero
[4] = {}, is_one
[4] = {};
2959 for (i
= 0; i
< 4; i
++) {
2960 /* It's a constant expression. Undef outputs are eliminated too. */
2961 if (exp
->chan
[i
].type
== AC_IR_UNDEF
) {
2964 } else if (exp
->chan
[i
].type
== AC_IR_CONST
) {
2965 if (exp
->chan
[i
].const_float
== 0)
2967 else if (exp
->chan
[i
].const_float
== 1)
2970 return false; /* other constant */
2975 /* Only certain combinations of 0 and 1 can be eliminated. */
2976 if (is_zero
[0] && is_zero
[1] && is_zero
[2])
2977 default_val
= is_zero
[3] ? 0 : 1;
2978 else if (is_one
[0] && is_one
[1] && is_one
[2])
2979 default_val
= is_zero
[3] ? 2 : 3;
2983 /* The PARAM export can be represented as DEFAULT_VAL. Kill it. */
2984 LLVMInstructionEraseFromParent(exp
->inst
);
2986 /* Change OFFSET to DEFAULT_VAL. */
2987 for (i
= 0; i
< num_outputs
; i
++) {
2988 if (vs_output_param_offset
[i
] == exp
->offset
) {
2989 vs_output_param_offset
[i
] =
2990 AC_EXP_PARAM_DEFAULT_VAL_0000
+ default_val
;
2997 static bool ac_eliminate_duplicated_output(struct ac_llvm_context
*ctx
,
2998 uint8_t *vs_output_param_offset
,
2999 uint32_t num_outputs
,
3000 struct ac_vs_exports
*processed
,
3001 struct ac_vs_exp_inst
*exp
)
3003 unsigned p
, copy_back_channels
= 0;
3005 /* See if the output is already in the list of processed outputs.
3006 * The LLVMValueRef comparison relies on SSA.
3008 for (p
= 0; p
< processed
->num
; p
++) {
3009 bool different
= false;
3011 for (unsigned j
= 0; j
< 4; j
++) {
3012 struct ac_vs_exp_chan
*c1
= &processed
->exp
[p
].chan
[j
];
3013 struct ac_vs_exp_chan
*c2
= &exp
->chan
[j
];
3015 /* Treat undef as a match. */
3016 if (c2
->type
== AC_IR_UNDEF
)
3019 /* If c1 is undef but c2 isn't, we can copy c2 to c1
3020 * and consider the instruction duplicated.
3022 if (c1
->type
== AC_IR_UNDEF
) {
3023 copy_back_channels
|= 1 << j
;
3027 /* Test whether the channels are not equal. */
3028 if (c1
->type
!= c2
->type
||
3029 (c1
->type
== AC_IR_CONST
&&
3030 c1
->const_float
!= c2
->const_float
) ||
3031 (c1
->type
== AC_IR_VALUE
&&
3032 c1
->value
!= c2
->value
)) {
3040 copy_back_channels
= 0;
3042 if (p
== processed
->num
)
3045 /* If a match was found, but the matching export has undef where the new
3046 * one has a normal value, copy the normal value to the undef channel.
3048 struct ac_vs_exp_inst
*match
= &processed
->exp
[p
];
3050 /* Get current enabled channels mask. */
3051 LLVMValueRef arg
= LLVMGetOperand(match
->inst
, AC_EXP_ENABLED_CHANNELS
);
3052 unsigned enabled_channels
= LLVMConstIntGetZExtValue(arg
);
3054 while (copy_back_channels
) {
3055 unsigned chan
= u_bit_scan(©_back_channels
);
3057 assert(match
->chan
[chan
].type
== AC_IR_UNDEF
);
3058 LLVMSetOperand(match
->inst
, AC_EXP_OUT0
+ chan
,
3059 exp
->chan
[chan
].value
);
3060 match
->chan
[chan
] = exp
->chan
[chan
];
3062 /* Update number of enabled channels because the original mask
3063 * is not always 0xf.
3065 enabled_channels
|= (1 << chan
);
3066 LLVMSetOperand(match
->inst
, AC_EXP_ENABLED_CHANNELS
,
3067 LLVMConstInt(ctx
->i32
, enabled_channels
, 0));
3070 /* The PARAM export is duplicated. Kill it. */
3071 LLVMInstructionEraseFromParent(exp
->inst
);
3073 /* Change OFFSET to the matching export. */
3074 for (unsigned i
= 0; i
< num_outputs
; i
++) {
3075 if (vs_output_param_offset
[i
] == exp
->offset
) {
3076 vs_output_param_offset
[i
] = match
->offset
;
3083 void ac_optimize_vs_outputs(struct ac_llvm_context
*ctx
,
3084 LLVMValueRef main_fn
,
3085 uint8_t *vs_output_param_offset
,
3086 uint32_t num_outputs
,
3087 uint32_t skip_output_mask
,
3088 uint8_t *num_param_exports
)
3090 LLVMBasicBlockRef bb
;
3091 bool removed_any
= false;
3092 struct ac_vs_exports exports
;
3096 /* Process all LLVM instructions. */
3097 bb
= LLVMGetFirstBasicBlock(main_fn
);
3099 LLVMValueRef inst
= LLVMGetFirstInstruction(bb
);
3102 LLVMValueRef cur
= inst
;
3103 inst
= LLVMGetNextInstruction(inst
);
3104 struct ac_vs_exp_inst exp
;
3106 if (LLVMGetInstructionOpcode(cur
) != LLVMCall
)
3109 LLVMValueRef callee
= ac_llvm_get_called_value(cur
);
3111 if (!ac_llvm_is_function(callee
))
3114 const char *name
= LLVMGetValueName(callee
);
3115 unsigned num_args
= LLVMCountParams(callee
);
3117 /* Check if this is an export instruction. */
3118 if ((num_args
!= 9 && num_args
!= 8) ||
3119 (strcmp(name
, "llvm.SI.export") &&
3120 strcmp(name
, "llvm.amdgcn.exp.f32")))
3123 LLVMValueRef arg
= LLVMGetOperand(cur
, AC_EXP_TARGET
);
3124 unsigned target
= LLVMConstIntGetZExtValue(arg
);
3126 if (target
< V_008DFC_SQ_EXP_PARAM
)
3129 target
-= V_008DFC_SQ_EXP_PARAM
;
3131 /* Parse the instruction. */
3132 memset(&exp
, 0, sizeof(exp
));
3133 exp
.offset
= target
;
3136 for (unsigned i
= 0; i
< 4; i
++) {
3137 LLVMValueRef v
= LLVMGetOperand(cur
, AC_EXP_OUT0
+ i
);
3139 exp
.chan
[i
].value
= v
;
3141 if (LLVMIsUndef(v
)) {
3142 exp
.chan
[i
].type
= AC_IR_UNDEF
;
3143 } else if (LLVMIsAConstantFP(v
)) {
3144 LLVMBool loses_info
;
3145 exp
.chan
[i
].type
= AC_IR_CONST
;
3146 exp
.chan
[i
].const_float
=
3147 LLVMConstRealGetDouble(v
, &loses_info
);
3149 exp
.chan
[i
].type
= AC_IR_VALUE
;
3153 /* Eliminate constant and duplicated PARAM exports. */
3154 if (!((1u << target
) & skip_output_mask
) &&
3155 (ac_eliminate_const_output(vs_output_param_offset
,
3156 num_outputs
, &exp
) ||
3157 ac_eliminate_duplicated_output(ctx
,
3158 vs_output_param_offset
,
3159 num_outputs
, &exports
,
3163 exports
.exp
[exports
.num
++] = exp
;
3166 bb
= LLVMGetNextBasicBlock(bb
);
3169 /* Remove holes in export memory due to removed PARAM exports.
3170 * This is done by renumbering all PARAM exports.
3173 uint8_t old_offset
[VARYING_SLOT_MAX
];
3176 /* Make a copy of the offsets. We need the old version while
3177 * we are modifying some of them. */
3178 memcpy(old_offset
, vs_output_param_offset
,
3179 sizeof(old_offset
));
3181 for (i
= 0; i
< exports
.num
; i
++) {
3182 unsigned offset
= exports
.exp
[i
].offset
;
3184 /* Update vs_output_param_offset. Multiple outputs can
3185 * have the same offset.
3187 for (out
= 0; out
< num_outputs
; out
++) {
3188 if (old_offset
[out
] == offset
)
3189 vs_output_param_offset
[out
] = i
;
3192 /* Change the PARAM offset in the instruction. */
3193 LLVMSetOperand(exports
.exp
[i
].inst
, AC_EXP_TARGET
,
3194 LLVMConstInt(ctx
->i32
,
3195 V_008DFC_SQ_EXP_PARAM
+ i
, 0));
3197 *num_param_exports
= exports
.num
;
3201 void ac_init_exec_full_mask(struct ac_llvm_context
*ctx
)
3203 LLVMValueRef full_mask
= LLVMConstInt(ctx
->i64
, ~0ull, 0);
3204 ac_build_intrinsic(ctx
,
3205 "llvm.amdgcn.init.exec", ctx
->voidt
,
3206 &full_mask
, 1, AC_FUNC_ATTR_CONVERGENT
);
3209 void ac_declare_lds_as_pointer(struct ac_llvm_context
*ctx
)
3211 unsigned lds_size
= ctx
->chip_class
>= GFX7
? 65536 : 32768;
3212 ctx
->lds
= LLVMBuildIntToPtr(ctx
->builder
, ctx
->i32_0
,
3213 LLVMPointerType(LLVMArrayType(ctx
->i32
, lds_size
/ 4), AC_ADDR_SPACE_LDS
),
3217 LLVMValueRef
ac_lds_load(struct ac_llvm_context
*ctx
,
3218 LLVMValueRef dw_addr
)
3220 return LLVMBuildLoad(ctx
->builder
, ac_build_gep0(ctx
, ctx
->lds
, dw_addr
), "");
3223 void ac_lds_store(struct ac_llvm_context
*ctx
,
3224 LLVMValueRef dw_addr
,
3227 value
= ac_to_integer(ctx
, value
);
3228 ac_build_indexed_store(ctx
, ctx
->lds
,
3232 LLVMValueRef
ac_find_lsb(struct ac_llvm_context
*ctx
,
3233 LLVMTypeRef dst_type
,
3236 unsigned src0_bitsize
= ac_get_elem_bits(ctx
, LLVMTypeOf(src0
));
3237 const char *intrin_name
;
3241 switch (src0_bitsize
) {
3243 intrin_name
= "llvm.cttz.i64";
3248 intrin_name
= "llvm.cttz.i32";
3253 intrin_name
= "llvm.cttz.i16";
3258 intrin_name
= "llvm.cttz.i8";
3263 unreachable(!"invalid bitsize");
3266 LLVMValueRef params
[2] = {
3269 /* The value of 1 means that ffs(x=0) = undef, so LLVM won't
3270 * add special code to check for x=0. The reason is that
3271 * the LLVM behavior for x=0 is different from what we
3272 * need here. However, LLVM also assumes that ffs(x) is
3273 * in [0, 31], but GLSL expects that ffs(0) = -1, so
3274 * a conditional assignment to handle 0 is still required.
3276 * The hardware already implements the correct behavior.
3281 LLVMValueRef lsb
= ac_build_intrinsic(ctx
, intrin_name
, type
,
3283 AC_FUNC_ATTR_READNONE
);
3285 if (src0_bitsize
== 64) {
3286 lsb
= LLVMBuildTrunc(ctx
->builder
, lsb
, ctx
->i32
, "");
3287 } else if (src0_bitsize
< 32) {
3288 lsb
= LLVMBuildSExt(ctx
->builder
, lsb
, ctx
->i32
, "");
3291 /* TODO: We need an intrinsic to skip this conditional. */
3292 /* Check for zero: */
3293 return LLVMBuildSelect(ctx
->builder
, LLVMBuildICmp(ctx
->builder
,
3296 LLVMConstInt(ctx
->i32
, -1, 0), lsb
, "");
3299 LLVMTypeRef
ac_array_in_const_addr_space(LLVMTypeRef elem_type
)
3301 return LLVMPointerType(elem_type
, AC_ADDR_SPACE_CONST
);
3304 LLVMTypeRef
ac_array_in_const32_addr_space(LLVMTypeRef elem_type
)
3306 return LLVMPointerType(elem_type
, AC_ADDR_SPACE_CONST_32BIT
);
3309 static struct ac_llvm_flow
*
3310 get_current_flow(struct ac_llvm_context
*ctx
)
3312 if (ctx
->flow
->depth
> 0)
3313 return &ctx
->flow
->stack
[ctx
->flow
->depth
- 1];
3317 static struct ac_llvm_flow
*
3318 get_innermost_loop(struct ac_llvm_context
*ctx
)
3320 for (unsigned i
= ctx
->flow
->depth
; i
> 0; --i
) {
3321 if (ctx
->flow
->stack
[i
- 1].loop_entry_block
)
3322 return &ctx
->flow
->stack
[i
- 1];
3327 static struct ac_llvm_flow
*
3328 push_flow(struct ac_llvm_context
*ctx
)
3330 struct ac_llvm_flow
*flow
;
3332 if (ctx
->flow
->depth
>= ctx
->flow
->depth_max
) {
3333 unsigned new_max
= MAX2(ctx
->flow
->depth
<< 1,
3334 AC_LLVM_INITIAL_CF_DEPTH
);
3336 ctx
->flow
->stack
= realloc(ctx
->flow
->stack
, new_max
* sizeof(*ctx
->flow
->stack
));
3337 ctx
->flow
->depth_max
= new_max
;
3340 flow
= &ctx
->flow
->stack
[ctx
->flow
->depth
];
3343 flow
->next_block
= NULL
;
3344 flow
->loop_entry_block
= NULL
;
3348 static void set_basicblock_name(LLVMBasicBlockRef bb
, const char *base
,
3352 snprintf(buf
, sizeof(buf
), "%s%d", base
, label_id
);
3353 LLVMSetValueName(LLVMBasicBlockAsValue(bb
), buf
);
3356 /* Append a basic block at the level of the parent flow.
3358 static LLVMBasicBlockRef
append_basic_block(struct ac_llvm_context
*ctx
,
3361 assert(ctx
->flow
->depth
>= 1);
3363 if (ctx
->flow
->depth
>= 2) {
3364 struct ac_llvm_flow
*flow
= &ctx
->flow
->stack
[ctx
->flow
->depth
- 2];
3366 return LLVMInsertBasicBlockInContext(ctx
->context
,
3367 flow
->next_block
, name
);
3370 LLVMValueRef main_fn
=
3371 LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
->builder
));
3372 return LLVMAppendBasicBlockInContext(ctx
->context
, main_fn
, name
);
3375 /* Emit a branch to the given default target for the current block if
3376 * applicable -- that is, if the current block does not already contain a
3377 * branch from a break or continue.
3379 static void emit_default_branch(LLVMBuilderRef builder
,
3380 LLVMBasicBlockRef target
)
3382 if (!LLVMGetBasicBlockTerminator(LLVMGetInsertBlock(builder
)))
3383 LLVMBuildBr(builder
, target
);
3386 void ac_build_bgnloop(struct ac_llvm_context
*ctx
, int label_id
)
3388 struct ac_llvm_flow
*flow
= push_flow(ctx
);
3389 flow
->loop_entry_block
= append_basic_block(ctx
, "LOOP");
3390 flow
->next_block
= append_basic_block(ctx
, "ENDLOOP");
3391 set_basicblock_name(flow
->loop_entry_block
, "loop", label_id
);
3392 LLVMBuildBr(ctx
->builder
, flow
->loop_entry_block
);
3393 LLVMPositionBuilderAtEnd(ctx
->builder
, flow
->loop_entry_block
);
3396 void ac_build_break(struct ac_llvm_context
*ctx
)
3398 struct ac_llvm_flow
*flow
= get_innermost_loop(ctx
);
3399 LLVMBuildBr(ctx
->builder
, flow
->next_block
);
3402 void ac_build_continue(struct ac_llvm_context
*ctx
)
3404 struct ac_llvm_flow
*flow
= get_innermost_loop(ctx
);
3405 LLVMBuildBr(ctx
->builder
, flow
->loop_entry_block
);
3408 void ac_build_else(struct ac_llvm_context
*ctx
, int label_id
)
3410 struct ac_llvm_flow
*current_branch
= get_current_flow(ctx
);
3411 LLVMBasicBlockRef endif_block
;
3413 assert(!current_branch
->loop_entry_block
);
3415 endif_block
= append_basic_block(ctx
, "ENDIF");
3416 emit_default_branch(ctx
->builder
, endif_block
);
3418 LLVMPositionBuilderAtEnd(ctx
->builder
, current_branch
->next_block
);
3419 set_basicblock_name(current_branch
->next_block
, "else", label_id
);
3421 current_branch
->next_block
= endif_block
;
3424 void ac_build_endif(struct ac_llvm_context
*ctx
, int label_id
)
3426 struct ac_llvm_flow
*current_branch
= get_current_flow(ctx
);
3428 assert(!current_branch
->loop_entry_block
);
3430 emit_default_branch(ctx
->builder
, current_branch
->next_block
);
3431 LLVMPositionBuilderAtEnd(ctx
->builder
, current_branch
->next_block
);
3432 set_basicblock_name(current_branch
->next_block
, "endif", label_id
);
3437 void ac_build_endloop(struct ac_llvm_context
*ctx
, int label_id
)
3439 struct ac_llvm_flow
*current_loop
= get_current_flow(ctx
);
3441 assert(current_loop
->loop_entry_block
);
3443 emit_default_branch(ctx
->builder
, current_loop
->loop_entry_block
);
3445 LLVMPositionBuilderAtEnd(ctx
->builder
, current_loop
->next_block
);
3446 set_basicblock_name(current_loop
->next_block
, "endloop", label_id
);
3450 void ac_build_ifcc(struct ac_llvm_context
*ctx
, LLVMValueRef cond
, int label_id
)
3452 struct ac_llvm_flow
*flow
= push_flow(ctx
);
3453 LLVMBasicBlockRef if_block
;
3455 if_block
= append_basic_block(ctx
, "IF");
3456 flow
->next_block
= append_basic_block(ctx
, "ELSE");
3457 set_basicblock_name(if_block
, "if", label_id
);
3458 LLVMBuildCondBr(ctx
->builder
, cond
, if_block
, flow
->next_block
);
3459 LLVMPositionBuilderAtEnd(ctx
->builder
, if_block
);
3462 void ac_build_if(struct ac_llvm_context
*ctx
, LLVMValueRef value
,
3465 LLVMValueRef cond
= LLVMBuildFCmp(ctx
->builder
, LLVMRealUNE
,
3466 value
, ctx
->f32_0
, "");
3467 ac_build_ifcc(ctx
, cond
, label_id
);
3470 void ac_build_uif(struct ac_llvm_context
*ctx
, LLVMValueRef value
,
3473 LLVMValueRef cond
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
3474 ac_to_integer(ctx
, value
),
3476 ac_build_ifcc(ctx
, cond
, label_id
);
3479 LLVMValueRef
ac_build_alloca_undef(struct ac_llvm_context
*ac
, LLVMTypeRef type
,
3482 LLVMBuilderRef builder
= ac
->builder
;
3483 LLVMBasicBlockRef current_block
= LLVMGetInsertBlock(builder
);
3484 LLVMValueRef function
= LLVMGetBasicBlockParent(current_block
);
3485 LLVMBasicBlockRef first_block
= LLVMGetEntryBasicBlock(function
);
3486 LLVMValueRef first_instr
= LLVMGetFirstInstruction(first_block
);
3487 LLVMBuilderRef first_builder
= LLVMCreateBuilderInContext(ac
->context
);
3491 LLVMPositionBuilderBefore(first_builder
, first_instr
);
3493 LLVMPositionBuilderAtEnd(first_builder
, first_block
);
3496 res
= LLVMBuildAlloca(first_builder
, type
, name
);
3497 LLVMDisposeBuilder(first_builder
);
3501 LLVMValueRef
ac_build_alloca(struct ac_llvm_context
*ac
,
3502 LLVMTypeRef type
, const char *name
)
3504 LLVMValueRef ptr
= ac_build_alloca_undef(ac
, type
, name
);
3505 LLVMBuildStore(ac
->builder
, LLVMConstNull(type
), ptr
);
3509 LLVMValueRef
ac_cast_ptr(struct ac_llvm_context
*ctx
, LLVMValueRef ptr
,
3512 int addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
3513 return LLVMBuildBitCast(ctx
->builder
, ptr
,
3514 LLVMPointerType(type
, addr_space
), "");
3517 LLVMValueRef
ac_trim_vector(struct ac_llvm_context
*ctx
, LLVMValueRef value
,
3520 unsigned num_components
= ac_get_llvm_num_components(value
);
3521 if (count
== num_components
)
3524 LLVMValueRef masks
[MAX2(count
, 2)];
3525 masks
[0] = ctx
->i32_0
;
3526 masks
[1] = ctx
->i32_1
;
3527 for (unsigned i
= 2; i
< count
; i
++)
3528 masks
[i
] = LLVMConstInt(ctx
->i32
, i
, false);
3531 return LLVMBuildExtractElement(ctx
->builder
, value
, masks
[0],
3534 LLVMValueRef swizzle
= LLVMConstVector(masks
, count
);
3535 return LLVMBuildShuffleVector(ctx
->builder
, value
, value
, swizzle
, "");
3538 LLVMValueRef
ac_unpack_param(struct ac_llvm_context
*ctx
, LLVMValueRef param
,
3539 unsigned rshift
, unsigned bitwidth
)
3541 LLVMValueRef value
= param
;
3543 value
= LLVMBuildLShr(ctx
->builder
, value
,
3544 LLVMConstInt(ctx
->i32
, rshift
, false), "");
3546 if (rshift
+ bitwidth
< 32) {
3547 unsigned mask
= (1 << bitwidth
) - 1;
3548 value
= LLVMBuildAnd(ctx
->builder
, value
,
3549 LLVMConstInt(ctx
->i32
, mask
, false), "");
3554 /* Adjust the sample index according to FMASK.
3556 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
3557 * which is the identity mapping. Each nibble says which physical sample
3558 * should be fetched to get that sample.
3560 * For example, 0x11111100 means there are only 2 samples stored and
3561 * the second sample covers 3/4 of the pixel. When reading samples 0
3562 * and 1, return physical sample 0 (determined by the first two 0s
3563 * in FMASK), otherwise return physical sample 1.
3565 * The sample index should be adjusted as follows:
3566 * addr[sample_index] = (fmask >> (addr[sample_index] * 4)) & 0xF;
3568 void ac_apply_fmask_to_sample(struct ac_llvm_context
*ac
, LLVMValueRef fmask
,
3569 LLVMValueRef
*addr
, bool is_array_tex
)
3571 struct ac_image_args fmask_load
= {};
3572 fmask_load
.opcode
= ac_image_load
;
3573 fmask_load
.resource
= fmask
;
3574 fmask_load
.dmask
= 0xf;
3575 fmask_load
.dim
= is_array_tex
? ac_image_2darray
: ac_image_2d
;
3576 fmask_load
.attributes
= AC_FUNC_ATTR_READNONE
;
3578 fmask_load
.coords
[0] = addr
[0];
3579 fmask_load
.coords
[1] = addr
[1];
3581 fmask_load
.coords
[2] = addr
[2];
3583 LLVMValueRef fmask_value
= ac_build_image_opcode(ac
, &fmask_load
);
3584 fmask_value
= LLVMBuildExtractElement(ac
->builder
, fmask_value
,
3587 /* Apply the formula. */
3588 unsigned sample_chan
= is_array_tex
? 3 : 2;
3589 LLVMValueRef final_sample
;
3590 final_sample
= LLVMBuildMul(ac
->builder
, addr
[sample_chan
],
3591 LLVMConstInt(ac
->i32
, 4, 0), "");
3592 final_sample
= LLVMBuildLShr(ac
->builder
, fmask_value
, final_sample
, "");
3593 /* Mask the sample index by 0x7, because 0x8 means an unknown value
3594 * with EQAA, so those will map to 0. */
3595 final_sample
= LLVMBuildAnd(ac
->builder
, final_sample
,
3596 LLVMConstInt(ac
->i32
, 0x7, 0), "");
3598 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
3599 * resource descriptor is 0 (invalid).
3602 tmp
= LLVMBuildBitCast(ac
->builder
, fmask
, ac
->v8i32
, "");
3603 tmp
= LLVMBuildExtractElement(ac
->builder
, tmp
, ac
->i32_1
, "");
3604 tmp
= LLVMBuildICmp(ac
->builder
, LLVMIntNE
, tmp
, ac
->i32_0
, "");
3606 /* Replace the MSAA sample index. */
3607 addr
[sample_chan
] = LLVMBuildSelect(ac
->builder
, tmp
, final_sample
,
3608 addr
[sample_chan
], "");
3612 _ac_build_readlane(struct ac_llvm_context
*ctx
, LLVMValueRef src
,
3613 LLVMValueRef lane
, bool with_opt_barrier
)
3615 LLVMTypeRef type
= LLVMTypeOf(src
);
3616 LLVMValueRef result
;
3618 if (with_opt_barrier
)
3619 ac_build_optimization_barrier(ctx
, &src
);
3621 src
= LLVMBuildZExt(ctx
->builder
, src
, ctx
->i32
, "");
3623 lane
= LLVMBuildZExt(ctx
->builder
, lane
, ctx
->i32
, "");
3625 result
= ac_build_intrinsic(ctx
,
3626 lane
== NULL
? "llvm.amdgcn.readfirstlane" : "llvm.amdgcn.readlane",
3627 ctx
->i32
, (LLVMValueRef
[]) { src
, lane
},
3628 lane
== NULL
? 1 : 2,
3629 AC_FUNC_ATTR_READNONE
|
3630 AC_FUNC_ATTR_CONVERGENT
);
3632 return LLVMBuildTrunc(ctx
->builder
, result
, type
, "");
3636 ac_build_readlane_common(struct ac_llvm_context
*ctx
,
3637 LLVMValueRef src
, LLVMValueRef lane
,
3638 bool with_opt_barrier
)
3640 LLVMTypeRef src_type
= LLVMTypeOf(src
);
3641 src
= ac_to_integer(ctx
, src
);
3642 unsigned bits
= LLVMGetIntTypeWidth(LLVMTypeOf(src
));
3646 assert(bits
% 32 == 0);
3647 LLVMTypeRef vec_type
= LLVMVectorType(ctx
->i32
, bits
/ 32);
3648 LLVMValueRef src_vector
=
3649 LLVMBuildBitCast(ctx
->builder
, src
, vec_type
, "");
3650 ret
= LLVMGetUndef(vec_type
);
3651 for (unsigned i
= 0; i
< bits
/ 32; i
++) {
3652 LLVMValueRef ret_comp
;
3654 src
= LLVMBuildExtractElement(ctx
->builder
, src_vector
,
3655 LLVMConstInt(ctx
->i32
, i
, 0), "");
3657 ret_comp
= _ac_build_readlane(ctx
, src
, lane
,
3660 ret
= LLVMBuildInsertElement(ctx
->builder
, ret
, ret_comp
,
3661 LLVMConstInt(ctx
->i32
, i
, 0), "");
3664 ret
= _ac_build_readlane(ctx
, src
, lane
, with_opt_barrier
);
3667 if (LLVMGetTypeKind(src_type
) == LLVMPointerTypeKind
)
3668 return LLVMBuildIntToPtr(ctx
->builder
, ret
, src_type
, "");
3669 return LLVMBuildBitCast(ctx
->builder
, ret
, src_type
, "");
3673 * Builds the "llvm.amdgcn.readlane" or "llvm.amdgcn.readfirstlane" intrinsic.
3675 * The optimization barrier is not needed if the value is the same in all lanes
3676 * or if this is called in the outermost block.
3680 * @param lane - id of the lane or NULL for the first active lane
3681 * @return value of the lane
3683 LLVMValueRef
ac_build_readlane_no_opt_barrier(struct ac_llvm_context
*ctx
,
3684 LLVMValueRef src
, LLVMValueRef lane
)
3686 return ac_build_readlane_common(ctx
, src
, lane
, false);
3691 ac_build_readlane(struct ac_llvm_context
*ctx
, LLVMValueRef src
, LLVMValueRef lane
)
3693 return ac_build_readlane_common(ctx
, src
, lane
, true);
3697 ac_build_writelane(struct ac_llvm_context
*ctx
, LLVMValueRef src
, LLVMValueRef value
, LLVMValueRef lane
)
3699 return ac_build_intrinsic(ctx
, "llvm.amdgcn.writelane", ctx
->i32
,
3700 (LLVMValueRef
[]) {value
, lane
, src
}, 3,
3701 AC_FUNC_ATTR_READNONE
| AC_FUNC_ATTR_CONVERGENT
);
3705 ac_build_mbcnt(struct ac_llvm_context
*ctx
, LLVMValueRef mask
)
3707 if (ctx
->wave_size
== 32) {
3708 return ac_build_intrinsic(ctx
, "llvm.amdgcn.mbcnt.lo", ctx
->i32
,
3709 (LLVMValueRef
[]) { mask
, ctx
->i32_0
},
3710 2, AC_FUNC_ATTR_READNONE
);
3712 LLVMValueRef mask_vec
= LLVMBuildBitCast(ctx
->builder
, mask
, ctx
->v2i32
, "");
3713 LLVMValueRef mask_lo
= LLVMBuildExtractElement(ctx
->builder
, mask_vec
,
3715 LLVMValueRef mask_hi
= LLVMBuildExtractElement(ctx
->builder
, mask_vec
,
3718 ac_build_intrinsic(ctx
, "llvm.amdgcn.mbcnt.lo", ctx
->i32
,
3719 (LLVMValueRef
[]) { mask_lo
, ctx
->i32_0
},
3720 2, AC_FUNC_ATTR_READNONE
);
3721 val
= ac_build_intrinsic(ctx
, "llvm.amdgcn.mbcnt.hi", ctx
->i32
,
3722 (LLVMValueRef
[]) { mask_hi
, val
},
3723 2, AC_FUNC_ATTR_READNONE
);
3728 _dpp_quad_perm
= 0x000,
3729 _dpp_row_sl
= 0x100,
3730 _dpp_row_sr
= 0x110,
3731 _dpp_row_rr
= 0x120,
3736 dpp_row_mirror
= 0x140,
3737 dpp_row_half_mirror
= 0x141,
3738 dpp_row_bcast15
= 0x142,
3739 dpp_row_bcast31
= 0x143
3742 static inline enum dpp_ctrl
3743 dpp_quad_perm(unsigned lane0
, unsigned lane1
, unsigned lane2
, unsigned lane3
)
3745 assert(lane0
< 4 && lane1
< 4 && lane2
< 4 && lane3
< 4);
3746 return _dpp_quad_perm
| lane0
| (lane1
<< 2) | (lane2
<< 4) | (lane3
<< 6);
3749 static inline enum dpp_ctrl
3750 dpp_row_sl(unsigned amount
)
3752 assert(amount
> 0 && amount
< 16);
3753 return _dpp_row_sl
| amount
;
3756 static inline enum dpp_ctrl
3757 dpp_row_sr(unsigned amount
)
3759 assert(amount
> 0 && amount
< 16);
3760 return _dpp_row_sr
| amount
;
3764 _ac_build_dpp(struct ac_llvm_context
*ctx
, LLVMValueRef old
, LLVMValueRef src
,
3765 enum dpp_ctrl dpp_ctrl
, unsigned row_mask
, unsigned bank_mask
,
3768 LLVMTypeRef type
= LLVMTypeOf(src
);
3771 old
= LLVMBuildZExt(ctx
->builder
, old
, ctx
->i32
, "");
3772 src
= LLVMBuildZExt(ctx
->builder
, src
, ctx
->i32
, "");
3774 res
= ac_build_intrinsic(ctx
, "llvm.amdgcn.update.dpp.i32", ctx
->i32
,
3777 LLVMConstInt(ctx
->i32
, dpp_ctrl
, 0),
3778 LLVMConstInt(ctx
->i32
, row_mask
, 0),
3779 LLVMConstInt(ctx
->i32
, bank_mask
, 0),
3780 LLVMConstInt(ctx
->i1
, bound_ctrl
, 0) },
3781 6, AC_FUNC_ATTR_READNONE
| AC_FUNC_ATTR_CONVERGENT
);
3783 return LLVMBuildTrunc(ctx
->builder
, res
, type
, "");
3787 ac_build_dpp(struct ac_llvm_context
*ctx
, LLVMValueRef old
, LLVMValueRef src
,
3788 enum dpp_ctrl dpp_ctrl
, unsigned row_mask
, unsigned bank_mask
,
3791 LLVMTypeRef src_type
= LLVMTypeOf(src
);
3792 src
= ac_to_integer(ctx
, src
);
3793 old
= ac_to_integer(ctx
, old
);
3794 unsigned bits
= LLVMGetIntTypeWidth(LLVMTypeOf(src
));
3797 assert(bits
% 32 == 0);
3798 LLVMTypeRef vec_type
= LLVMVectorType(ctx
->i32
, bits
/ 32);
3799 LLVMValueRef src_vector
=
3800 LLVMBuildBitCast(ctx
->builder
, src
, vec_type
, "");
3801 LLVMValueRef old_vector
=
3802 LLVMBuildBitCast(ctx
->builder
, old
, vec_type
, "");
3803 ret
= LLVMGetUndef(vec_type
);
3804 for (unsigned i
= 0; i
< bits
/ 32; i
++) {
3805 src
= LLVMBuildExtractElement(ctx
->builder
, src_vector
,
3806 LLVMConstInt(ctx
->i32
, i
,
3808 old
= LLVMBuildExtractElement(ctx
->builder
, old_vector
,
3809 LLVMConstInt(ctx
->i32
, i
,
3811 LLVMValueRef ret_comp
= _ac_build_dpp(ctx
, old
, src
,
3816 ret
= LLVMBuildInsertElement(ctx
->builder
, ret
,
3818 LLVMConstInt(ctx
->i32
, i
,
3822 ret
= _ac_build_dpp(ctx
, old
, src
, dpp_ctrl
, row_mask
,
3823 bank_mask
, bound_ctrl
);
3825 return LLVMBuildBitCast(ctx
->builder
, ret
, src_type
, "");
3829 _ac_build_permlane16(struct ac_llvm_context
*ctx
, LLVMValueRef src
, uint64_t sel
,
3830 bool exchange_rows
, bool bound_ctrl
)
3832 LLVMTypeRef type
= LLVMTypeOf(src
);
3833 LLVMValueRef result
;
3835 src
= LLVMBuildZExt(ctx
->builder
, src
, ctx
->i32
, "");
3837 LLVMValueRef args
[6] = {
3840 LLVMConstInt(ctx
->i32
, sel
, false),
3841 LLVMConstInt(ctx
->i32
, sel
>> 32, false),
3842 ctx
->i1true
, /* fi */
3843 bound_ctrl
? ctx
->i1true
: ctx
->i1false
,
3846 result
= ac_build_intrinsic(ctx
, exchange_rows
? "llvm.amdgcn.permlanex16"
3847 : "llvm.amdgcn.permlane16",
3849 AC_FUNC_ATTR_READNONE
| AC_FUNC_ATTR_CONVERGENT
);
3851 return LLVMBuildTrunc(ctx
->builder
, result
, type
, "");
3855 ac_build_permlane16(struct ac_llvm_context
*ctx
, LLVMValueRef src
, uint64_t sel
,
3856 bool exchange_rows
, bool bound_ctrl
)
3858 LLVMTypeRef src_type
= LLVMTypeOf(src
);
3859 src
= ac_to_integer(ctx
, src
);
3860 unsigned bits
= LLVMGetIntTypeWidth(LLVMTypeOf(src
));
3863 assert(bits
% 32 == 0);
3864 LLVMTypeRef vec_type
= LLVMVectorType(ctx
->i32
, bits
/ 32);
3865 LLVMValueRef src_vector
=
3866 LLVMBuildBitCast(ctx
->builder
, src
, vec_type
, "");
3867 ret
= LLVMGetUndef(vec_type
);
3868 for (unsigned i
= 0; i
< bits
/ 32; i
++) {
3869 src
= LLVMBuildExtractElement(ctx
->builder
, src_vector
,
3870 LLVMConstInt(ctx
->i32
, i
,
3872 LLVMValueRef ret_comp
=
3873 _ac_build_permlane16(ctx
, src
, sel
,
3876 ret
= LLVMBuildInsertElement(ctx
->builder
, ret
,
3878 LLVMConstInt(ctx
->i32
, i
,
3882 ret
= _ac_build_permlane16(ctx
, src
, sel
, exchange_rows
,
3885 return LLVMBuildBitCast(ctx
->builder
, ret
, src_type
, "");
3888 static inline unsigned
3889 ds_pattern_bitmode(unsigned and_mask
, unsigned or_mask
, unsigned xor_mask
)
3891 assert(and_mask
< 32 && or_mask
< 32 && xor_mask
< 32);
3892 return and_mask
| (or_mask
<< 5) | (xor_mask
<< 10);
3896 _ac_build_ds_swizzle(struct ac_llvm_context
*ctx
, LLVMValueRef src
, unsigned mask
)
3898 LLVMTypeRef src_type
= LLVMTypeOf(src
);
3901 src
= LLVMBuildZExt(ctx
->builder
, src
, ctx
->i32
, "");
3903 ret
= ac_build_intrinsic(ctx
, "llvm.amdgcn.ds.swizzle", ctx
->i32
,
3905 src
, LLVMConstInt(ctx
->i32
, mask
, 0) },
3906 2, AC_FUNC_ATTR_READNONE
| AC_FUNC_ATTR_CONVERGENT
);
3908 return LLVMBuildTrunc(ctx
->builder
, ret
, src_type
, "");
3912 ac_build_ds_swizzle(struct ac_llvm_context
*ctx
, LLVMValueRef src
, unsigned mask
)
3914 LLVMTypeRef src_type
= LLVMTypeOf(src
);
3915 src
= ac_to_integer(ctx
, src
);
3916 unsigned bits
= LLVMGetIntTypeWidth(LLVMTypeOf(src
));
3919 assert(bits
% 32 == 0);
3920 LLVMTypeRef vec_type
= LLVMVectorType(ctx
->i32
, bits
/ 32);
3921 LLVMValueRef src_vector
=
3922 LLVMBuildBitCast(ctx
->builder
, src
, vec_type
, "");
3923 ret
= LLVMGetUndef(vec_type
);
3924 for (unsigned i
= 0; i
< bits
/ 32; i
++) {
3925 src
= LLVMBuildExtractElement(ctx
->builder
, src_vector
,
3926 LLVMConstInt(ctx
->i32
, i
,
3928 LLVMValueRef ret_comp
= _ac_build_ds_swizzle(ctx
, src
,
3930 ret
= LLVMBuildInsertElement(ctx
->builder
, ret
,
3932 LLVMConstInt(ctx
->i32
, i
,
3936 ret
= _ac_build_ds_swizzle(ctx
, src
, mask
);
3938 return LLVMBuildBitCast(ctx
->builder
, ret
, src_type
, "");
3942 ac_build_wwm(struct ac_llvm_context
*ctx
, LLVMValueRef src
)
3944 LLVMTypeRef src_type
= LLVMTypeOf(src
);
3945 unsigned bitsize
= ac_get_elem_bits(ctx
, src_type
);
3946 char name
[32], type
[8];
3949 src
= ac_to_integer(ctx
, src
);
3952 src
= LLVMBuildZExt(ctx
->builder
, src
, ctx
->i32
, "");
3954 ac_build_type_name_for_intr(LLVMTypeOf(src
), type
, sizeof(type
));
3955 snprintf(name
, sizeof(name
), "llvm.amdgcn.wwm.%s", type
);
3956 ret
= ac_build_intrinsic(ctx
, name
, LLVMTypeOf(src
),
3957 (LLVMValueRef
[]) { src
}, 1,
3958 AC_FUNC_ATTR_READNONE
);
3961 ret
= LLVMBuildTrunc(ctx
->builder
, ret
,
3962 ac_to_integer_type(ctx
, src_type
), "");
3964 return LLVMBuildBitCast(ctx
->builder
, ret
, src_type
, "");
3968 ac_build_set_inactive(struct ac_llvm_context
*ctx
, LLVMValueRef src
,
3969 LLVMValueRef inactive
)
3971 char name
[33], type
[8];
3972 LLVMTypeRef src_type
= LLVMTypeOf(src
);
3973 unsigned bitsize
= ac_get_elem_bits(ctx
, src_type
);
3974 src
= ac_to_integer(ctx
, src
);
3975 inactive
= ac_to_integer(ctx
, inactive
);
3978 src
= LLVMBuildZExt(ctx
->builder
, src
, ctx
->i32
, "");
3979 inactive
= LLVMBuildZExt(ctx
->builder
, inactive
, ctx
->i32
, "");
3982 ac_build_type_name_for_intr(LLVMTypeOf(src
), type
, sizeof(type
));
3983 snprintf(name
, sizeof(name
), "llvm.amdgcn.set.inactive.%s", type
);
3985 ac_build_intrinsic(ctx
, name
,
3986 LLVMTypeOf(src
), (LLVMValueRef
[]) {
3988 AC_FUNC_ATTR_READNONE
|
3989 AC_FUNC_ATTR_CONVERGENT
);
3991 ret
= LLVMBuildTrunc(ctx
->builder
, ret
, src_type
, "");
3997 get_reduction_identity(struct ac_llvm_context
*ctx
, nir_op op
, unsigned type_size
)
3999 if (type_size
== 1) {
4001 case nir_op_iadd
: return ctx
->i8_0
;
4002 case nir_op_imul
: return ctx
->i8_1
;
4003 case nir_op_imin
: return LLVMConstInt(ctx
->i8
, INT8_MAX
, 0);
4004 case nir_op_umin
: return LLVMConstInt(ctx
->i8
, UINT8_MAX
, 0);
4005 case nir_op_imax
: return LLVMConstInt(ctx
->i8
, INT8_MIN
, 0);
4006 case nir_op_umax
: return ctx
->i8_0
;
4007 case nir_op_iand
: return LLVMConstInt(ctx
->i8
, -1, 0);
4008 case nir_op_ior
: return ctx
->i8_0
;
4009 case nir_op_ixor
: return ctx
->i8_0
;
4011 unreachable("bad reduction intrinsic");
4013 } else if (type_size
== 2) {
4015 case nir_op_iadd
: return ctx
->i16_0
;
4016 case nir_op_fadd
: return ctx
->f16_0
;
4017 case nir_op_imul
: return ctx
->i16_1
;
4018 case nir_op_fmul
: return ctx
->f16_1
;
4019 case nir_op_imin
: return LLVMConstInt(ctx
->i16
, INT16_MAX
, 0);
4020 case nir_op_umin
: return LLVMConstInt(ctx
->i16
, UINT16_MAX
, 0);
4021 case nir_op_fmin
: return LLVMConstReal(ctx
->f16
, INFINITY
);
4022 case nir_op_imax
: return LLVMConstInt(ctx
->i16
, INT16_MIN
, 0);
4023 case nir_op_umax
: return ctx
->i16_0
;
4024 case nir_op_fmax
: return LLVMConstReal(ctx
->f16
, -INFINITY
);
4025 case nir_op_iand
: return LLVMConstInt(ctx
->i16
, -1, 0);
4026 case nir_op_ior
: return ctx
->i16_0
;
4027 case nir_op_ixor
: return ctx
->i16_0
;
4029 unreachable("bad reduction intrinsic");
4031 } else if (type_size
== 4) {
4033 case nir_op_iadd
: return ctx
->i32_0
;
4034 case nir_op_fadd
: return ctx
->f32_0
;
4035 case nir_op_imul
: return ctx
->i32_1
;
4036 case nir_op_fmul
: return ctx
->f32_1
;
4037 case nir_op_imin
: return LLVMConstInt(ctx
->i32
, INT32_MAX
, 0);
4038 case nir_op_umin
: return LLVMConstInt(ctx
->i32
, UINT32_MAX
, 0);
4039 case nir_op_fmin
: return LLVMConstReal(ctx
->f32
, INFINITY
);
4040 case nir_op_imax
: return LLVMConstInt(ctx
->i32
, INT32_MIN
, 0);
4041 case nir_op_umax
: return ctx
->i32_0
;
4042 case nir_op_fmax
: return LLVMConstReal(ctx
->f32
, -INFINITY
);
4043 case nir_op_iand
: return LLVMConstInt(ctx
->i32
, -1, 0);
4044 case nir_op_ior
: return ctx
->i32_0
;
4045 case nir_op_ixor
: return ctx
->i32_0
;
4047 unreachable("bad reduction intrinsic");
4049 } else { /* type_size == 64bit */
4051 case nir_op_iadd
: return ctx
->i64_0
;
4052 case nir_op_fadd
: return ctx
->f64_0
;
4053 case nir_op_imul
: return ctx
->i64_1
;
4054 case nir_op_fmul
: return ctx
->f64_1
;
4055 case nir_op_imin
: return LLVMConstInt(ctx
->i64
, INT64_MAX
, 0);
4056 case nir_op_umin
: return LLVMConstInt(ctx
->i64
, UINT64_MAX
, 0);
4057 case nir_op_fmin
: return LLVMConstReal(ctx
->f64
, INFINITY
);
4058 case nir_op_imax
: return LLVMConstInt(ctx
->i64
, INT64_MIN
, 0);
4059 case nir_op_umax
: return ctx
->i64_0
;
4060 case nir_op_fmax
: return LLVMConstReal(ctx
->f64
, -INFINITY
);
4061 case nir_op_iand
: return LLVMConstInt(ctx
->i64
, -1, 0);
4062 case nir_op_ior
: return ctx
->i64_0
;
4063 case nir_op_ixor
: return ctx
->i64_0
;
4065 unreachable("bad reduction intrinsic");
4071 ac_build_alu_op(struct ac_llvm_context
*ctx
, LLVMValueRef lhs
, LLVMValueRef rhs
, nir_op op
)
4073 bool _64bit
= ac_get_type_size(LLVMTypeOf(lhs
)) == 8;
4074 bool _32bit
= ac_get_type_size(LLVMTypeOf(lhs
)) == 4;
4076 case nir_op_iadd
: return LLVMBuildAdd(ctx
->builder
, lhs
, rhs
, "");
4077 case nir_op_fadd
: return LLVMBuildFAdd(ctx
->builder
, lhs
, rhs
, "");
4078 case nir_op_imul
: return LLVMBuildMul(ctx
->builder
, lhs
, rhs
, "");
4079 case nir_op_fmul
: return LLVMBuildFMul(ctx
->builder
, lhs
, rhs
, "");
4080 case nir_op_imin
: return LLVMBuildSelect(ctx
->builder
,
4081 LLVMBuildICmp(ctx
->builder
, LLVMIntSLT
, lhs
, rhs
, ""),
4083 case nir_op_umin
: return LLVMBuildSelect(ctx
->builder
,
4084 LLVMBuildICmp(ctx
->builder
, LLVMIntULT
, lhs
, rhs
, ""),
4086 case nir_op_fmin
: return ac_build_intrinsic(ctx
,
4087 _64bit
? "llvm.minnum.f64" : _32bit
? "llvm.minnum.f32" : "llvm.minnum.f16",
4088 _64bit
? ctx
->f64
: _32bit
? ctx
->f32
: ctx
->f16
,
4089 (LLVMValueRef
[]){lhs
, rhs
}, 2, AC_FUNC_ATTR_READNONE
);
4090 case nir_op_imax
: return LLVMBuildSelect(ctx
->builder
,
4091 LLVMBuildICmp(ctx
->builder
, LLVMIntSGT
, lhs
, rhs
, ""),
4093 case nir_op_umax
: return LLVMBuildSelect(ctx
->builder
,
4094 LLVMBuildICmp(ctx
->builder
, LLVMIntUGT
, lhs
, rhs
, ""),
4096 case nir_op_fmax
: return ac_build_intrinsic(ctx
,
4097 _64bit
? "llvm.maxnum.f64" : _32bit
? "llvm.maxnum.f32" : "llvm.maxnum.f16",
4098 _64bit
? ctx
->f64
: _32bit
? ctx
->f32
: ctx
->f16
,
4099 (LLVMValueRef
[]){lhs
, rhs
}, 2, AC_FUNC_ATTR_READNONE
);
4100 case nir_op_iand
: return LLVMBuildAnd(ctx
->builder
, lhs
, rhs
, "");
4101 case nir_op_ior
: return LLVMBuildOr(ctx
->builder
, lhs
, rhs
, "");
4102 case nir_op_ixor
: return LLVMBuildXor(ctx
->builder
, lhs
, rhs
, "");
4104 unreachable("bad reduction intrinsic");
4109 * \param src The value to shift.
4110 * \param identity The value to use the first lane.
4111 * \param maxprefix specifies that the result only needs to be correct for a
4112 * prefix of this many threads
4113 * \return src, shifted 1 lane up, and identity shifted into lane 0.
4116 ac_wavefront_shift_right_1(struct ac_llvm_context
*ctx
, LLVMValueRef src
,
4117 LLVMValueRef identity
, unsigned maxprefix
)
4119 if (ctx
->chip_class
>= GFX10
) {
4120 /* wavefront shift_right by 1 on GFX10 (emulate dpp_wf_sr1) */
4121 LLVMValueRef active
, tmp1
, tmp2
;
4122 LLVMValueRef tid
= ac_get_thread_id(ctx
);
4124 tmp1
= ac_build_dpp(ctx
, identity
, src
, dpp_row_sr(1), 0xf, 0xf, false);
4126 tmp2
= ac_build_permlane16(ctx
, src
, (uint64_t)~0, true, false);
4128 if (maxprefix
> 32) {
4129 active
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, tid
,
4130 LLVMConstInt(ctx
->i32
, 32, false), "");
4132 tmp2
= LLVMBuildSelect(ctx
->builder
, active
,
4133 ac_build_readlane(ctx
, src
,
4134 LLVMConstInt(ctx
->i32
, 31, false)),
4137 active
= LLVMBuildOr(ctx
->builder
, active
,
4138 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
4139 LLVMBuildAnd(ctx
->builder
, tid
,
4140 LLVMConstInt(ctx
->i32
, 0x1f, false), ""),
4141 LLVMConstInt(ctx
->i32
, 0x10, false), ""), "");
4142 return LLVMBuildSelect(ctx
->builder
, active
, tmp2
, tmp1
, "");
4143 } else if (maxprefix
> 16) {
4144 active
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, tid
,
4145 LLVMConstInt(ctx
->i32
, 16, false), "");
4147 return LLVMBuildSelect(ctx
->builder
, active
, tmp2
, tmp1
, "");
4149 } else if (ctx
->chip_class
>= GFX8
) {
4150 return ac_build_dpp(ctx
, identity
, src
, dpp_wf_sr1
, 0xf, 0xf, false);
4153 /* wavefront shift_right by 1 on SI/CI */
4154 LLVMValueRef active
, tmp1
, tmp2
;
4155 LLVMValueRef tid
= ac_get_thread_id(ctx
);
4156 tmp1
= ac_build_ds_swizzle(ctx
, src
, (1 << 15) | dpp_quad_perm(0, 0, 1, 2));
4157 tmp2
= ac_build_ds_swizzle(ctx
, src
, ds_pattern_bitmode(0x18, 0x03, 0x00));
4158 active
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
4159 LLVMBuildAnd(ctx
->builder
, tid
, LLVMConstInt(ctx
->i32
, 0x7, 0), ""),
4160 LLVMConstInt(ctx
->i32
, 0x4, 0), "");
4161 tmp1
= LLVMBuildSelect(ctx
->builder
, active
, tmp2
, tmp1
, "");
4162 tmp2
= ac_build_ds_swizzle(ctx
, src
, ds_pattern_bitmode(0x10, 0x07, 0x00));
4163 active
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
4164 LLVMBuildAnd(ctx
->builder
, tid
, LLVMConstInt(ctx
->i32
, 0xf, 0), ""),
4165 LLVMConstInt(ctx
->i32
, 0x8, 0), "");
4166 tmp1
= LLVMBuildSelect(ctx
->builder
, active
, tmp2
, tmp1
, "");
4167 tmp2
= ac_build_ds_swizzle(ctx
, src
, ds_pattern_bitmode(0x00, 0x0f, 0x00));
4168 active
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
4169 LLVMBuildAnd(ctx
->builder
, tid
, LLVMConstInt(ctx
->i32
, 0x1f, 0), ""),
4170 LLVMConstInt(ctx
->i32
, 0x10, 0), "");
4171 tmp1
= LLVMBuildSelect(ctx
->builder
, active
, tmp2
, tmp1
, "");
4172 tmp2
= ac_build_readlane(ctx
, src
, LLVMConstInt(ctx
->i32
, 31, 0));
4173 active
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, tid
, LLVMConstInt(ctx
->i32
, 32, 0), "");
4174 tmp1
= LLVMBuildSelect(ctx
->builder
, active
, tmp2
, tmp1
, "");
4175 active
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, tid
, LLVMConstInt(ctx
->i32
, 0, 0), "");
4176 return LLVMBuildSelect(ctx
->builder
, active
, identity
, tmp1
, "");
4180 * \param maxprefix specifies that the result only needs to be correct for a
4181 * prefix of this many threads
4184 ac_build_scan(struct ac_llvm_context
*ctx
, nir_op op
, LLVMValueRef src
, LLVMValueRef identity
,
4185 unsigned maxprefix
, bool inclusive
)
4187 LLVMValueRef result
, tmp
;
4190 src
= ac_wavefront_shift_right_1(ctx
, src
, identity
, maxprefix
);
4194 if (ctx
->chip_class
<= GFX7
) {
4195 assert(maxprefix
== 64);
4196 LLVMValueRef tid
= ac_get_thread_id(ctx
);
4197 LLVMValueRef active
;
4198 tmp
= ac_build_ds_swizzle(ctx
, src
, ds_pattern_bitmode(0x1e, 0x00, 0x00));
4199 active
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
4200 LLVMBuildAnd(ctx
->builder
, tid
, ctx
->i32_1
, ""),
4202 tmp
= LLVMBuildSelect(ctx
->builder
, active
, tmp
, identity
, "");
4203 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
4204 tmp
= ac_build_ds_swizzle(ctx
, result
, ds_pattern_bitmode(0x1c, 0x01, 0x00));
4205 active
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
4206 LLVMBuildAnd(ctx
->builder
, tid
, LLVMConstInt(ctx
->i32
, 2, 0), ""),
4208 tmp
= LLVMBuildSelect(ctx
->builder
, active
, tmp
, identity
, "");
4209 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
4210 tmp
= ac_build_ds_swizzle(ctx
, result
, ds_pattern_bitmode(0x18, 0x03, 0x00));
4211 active
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
4212 LLVMBuildAnd(ctx
->builder
, tid
, LLVMConstInt(ctx
->i32
, 4, 0), ""),
4214 tmp
= LLVMBuildSelect(ctx
->builder
, active
, tmp
, identity
, "");
4215 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
4216 tmp
= ac_build_ds_swizzle(ctx
, result
, ds_pattern_bitmode(0x10, 0x07, 0x00));
4217 active
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
4218 LLVMBuildAnd(ctx
->builder
, tid
, LLVMConstInt(ctx
->i32
, 8, 0), ""),
4220 tmp
= LLVMBuildSelect(ctx
->builder
, active
, tmp
, identity
, "");
4221 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
4222 tmp
= ac_build_ds_swizzle(ctx
, result
, ds_pattern_bitmode(0x00, 0x0f, 0x00));
4223 active
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
4224 LLVMBuildAnd(ctx
->builder
, tid
, LLVMConstInt(ctx
->i32
, 16, 0), ""),
4226 tmp
= LLVMBuildSelect(ctx
->builder
, active
, tmp
, identity
, "");
4227 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
4228 tmp
= ac_build_readlane(ctx
, result
, LLVMConstInt(ctx
->i32
, 31, 0));
4229 active
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
4230 LLVMBuildAnd(ctx
->builder
, tid
, LLVMConstInt(ctx
->i32
, 32, 0), ""),
4232 tmp
= LLVMBuildSelect(ctx
->builder
, active
, tmp
, identity
, "");
4233 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
4239 tmp
= ac_build_dpp(ctx
, identity
, src
, dpp_row_sr(1), 0xf, 0xf, false);
4240 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
4243 tmp
= ac_build_dpp(ctx
, identity
, src
, dpp_row_sr(2), 0xf, 0xf, false);
4244 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
4247 tmp
= ac_build_dpp(ctx
, identity
, src
, dpp_row_sr(3), 0xf, 0xf, false);
4248 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
4251 tmp
= ac_build_dpp(ctx
, identity
, result
, dpp_row_sr(4), 0xf, 0xe, false);
4252 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
4255 tmp
= ac_build_dpp(ctx
, identity
, result
, dpp_row_sr(8), 0xf, 0xc, false);
4256 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
4257 if (maxprefix
<= 16)
4260 if (ctx
->chip_class
>= GFX10
) {
4261 LLVMValueRef tid
= ac_get_thread_id(ctx
);
4262 LLVMValueRef active
;
4264 tmp
= ac_build_permlane16(ctx
, result
, ~(uint64_t)0, true, false);
4266 active
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
4267 LLVMBuildAnd(ctx
->builder
, tid
,
4268 LLVMConstInt(ctx
->i32
, 16, false), ""),
4271 tmp
= LLVMBuildSelect(ctx
->builder
, active
, tmp
, identity
, "");
4273 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
4275 if (maxprefix
<= 32)
4278 tmp
= ac_build_readlane(ctx
, result
, LLVMConstInt(ctx
->i32
, 31, false));
4280 active
= LLVMBuildICmp(ctx
->builder
, LLVMIntUGE
, tid
,
4281 LLVMConstInt(ctx
->i32
, 32, false), "");
4283 tmp
= LLVMBuildSelect(ctx
->builder
, active
, tmp
, identity
, "");
4285 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
4289 tmp
= ac_build_dpp(ctx
, identity
, result
, dpp_row_bcast15
, 0xa, 0xf, false);
4290 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
4291 if (maxprefix
<= 32)
4293 tmp
= ac_build_dpp(ctx
, identity
, result
, dpp_row_bcast31
, 0xc, 0xf, false);
4294 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
4299 ac_build_inclusive_scan(struct ac_llvm_context
*ctx
, LLVMValueRef src
, nir_op op
)
4301 LLVMValueRef result
;
4303 if (LLVMTypeOf(src
) == ctx
->i1
&& op
== nir_op_iadd
) {
4304 LLVMBuilderRef builder
= ctx
->builder
;
4305 src
= LLVMBuildZExt(builder
, src
, ctx
->i32
, "");
4306 result
= ac_build_ballot(ctx
, src
);
4307 result
= ac_build_mbcnt(ctx
, result
);
4308 result
= LLVMBuildAdd(builder
, result
, src
, "");
4312 ac_build_optimization_barrier(ctx
, &src
);
4314 LLVMValueRef identity
=
4315 get_reduction_identity(ctx
, op
, ac_get_type_size(LLVMTypeOf(src
)));
4316 result
= LLVMBuildBitCast(ctx
->builder
, ac_build_set_inactive(ctx
, src
, identity
),
4317 LLVMTypeOf(identity
), "");
4318 result
= ac_build_scan(ctx
, op
, result
, identity
, ctx
->wave_size
, true);
4320 return ac_build_wwm(ctx
, result
);
4324 ac_build_exclusive_scan(struct ac_llvm_context
*ctx
, LLVMValueRef src
, nir_op op
)
4326 LLVMValueRef result
;
4328 if (LLVMTypeOf(src
) == ctx
->i1
&& op
== nir_op_iadd
) {
4329 LLVMBuilderRef builder
= ctx
->builder
;
4330 src
= LLVMBuildZExt(builder
, src
, ctx
->i32
, "");
4331 result
= ac_build_ballot(ctx
, src
);
4332 result
= ac_build_mbcnt(ctx
, result
);
4336 ac_build_optimization_barrier(ctx
, &src
);
4338 LLVMValueRef identity
=
4339 get_reduction_identity(ctx
, op
, ac_get_type_size(LLVMTypeOf(src
)));
4340 result
= LLVMBuildBitCast(ctx
->builder
, ac_build_set_inactive(ctx
, src
, identity
),
4341 LLVMTypeOf(identity
), "");
4342 result
= ac_build_scan(ctx
, op
, result
, identity
, ctx
->wave_size
, false);
4344 return ac_build_wwm(ctx
, result
);
4348 ac_build_reduce(struct ac_llvm_context
*ctx
, LLVMValueRef src
, nir_op op
, unsigned cluster_size
)
4350 if (cluster_size
== 1) return src
;
4351 ac_build_optimization_barrier(ctx
, &src
);
4352 LLVMValueRef result
, swap
;
4353 LLVMValueRef identity
= get_reduction_identity(ctx
, op
,
4354 ac_get_type_size(LLVMTypeOf(src
)));
4355 result
= LLVMBuildBitCast(ctx
->builder
,
4356 ac_build_set_inactive(ctx
, src
, identity
),
4357 LLVMTypeOf(identity
), "");
4358 swap
= ac_build_quad_swizzle(ctx
, result
, 1, 0, 3, 2);
4359 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
4360 if (cluster_size
== 2) return ac_build_wwm(ctx
, result
);
4362 swap
= ac_build_quad_swizzle(ctx
, result
, 2, 3, 0, 1);
4363 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
4364 if (cluster_size
== 4) return ac_build_wwm(ctx
, result
);
4366 if (ctx
->chip_class
>= GFX8
)
4367 swap
= ac_build_dpp(ctx
, identity
, result
, dpp_row_half_mirror
, 0xf, 0xf, false);
4369 swap
= ac_build_ds_swizzle(ctx
, result
, ds_pattern_bitmode(0x1f, 0, 0x04));
4370 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
4371 if (cluster_size
== 8) return ac_build_wwm(ctx
, result
);
4373 if (ctx
->chip_class
>= GFX8
)
4374 swap
= ac_build_dpp(ctx
, identity
, result
, dpp_row_mirror
, 0xf, 0xf, false);
4376 swap
= ac_build_ds_swizzle(ctx
, result
, ds_pattern_bitmode(0x1f, 0, 0x08));
4377 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
4378 if (cluster_size
== 16) return ac_build_wwm(ctx
, result
);
4380 if (ctx
->chip_class
>= GFX10
)
4381 swap
= ac_build_permlane16(ctx
, result
, 0, true, false);
4382 else if (ctx
->chip_class
>= GFX8
&& cluster_size
!= 32)
4383 swap
= ac_build_dpp(ctx
, identity
, result
, dpp_row_bcast15
, 0xa, 0xf, false);
4385 swap
= ac_build_ds_swizzle(ctx
, result
, ds_pattern_bitmode(0x1f, 0, 0x10));
4386 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
4387 if (cluster_size
== 32) return ac_build_wwm(ctx
, result
);
4389 if (ctx
->chip_class
>= GFX8
) {
4390 if (ctx
->wave_size
== 64) {
4391 if (ctx
->chip_class
>= GFX10
)
4392 swap
= ac_build_readlane(ctx
, result
, LLVMConstInt(ctx
->i32
, 31, false));
4394 swap
= ac_build_dpp(ctx
, identity
, result
, dpp_row_bcast31
, 0xc, 0xf, false);
4395 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
4396 result
= ac_build_readlane(ctx
, result
, LLVMConstInt(ctx
->i32
, 63, 0));
4399 return ac_build_wwm(ctx
, result
);
4401 swap
= ac_build_readlane(ctx
, result
, ctx
->i32_0
);
4402 result
= ac_build_readlane(ctx
, result
, LLVMConstInt(ctx
->i32
, 32, 0));
4403 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
4404 return ac_build_wwm(ctx
, result
);
4409 * "Top half" of a scan that reduces per-wave values across an entire
4412 * The source value must be present in the highest lane of the wave, and the
4413 * highest lane must be live.
4416 ac_build_wg_wavescan_top(struct ac_llvm_context
*ctx
, struct ac_wg_scan
*ws
)
4418 if (ws
->maxwaves
<= 1)
4421 const LLVMValueRef last_lane
= LLVMConstInt(ctx
->i32
, ctx
->wave_size
- 1, false);
4422 LLVMBuilderRef builder
= ctx
->builder
;
4423 LLVMValueRef tid
= ac_get_thread_id(ctx
);
4426 tmp
= LLVMBuildICmp(builder
, LLVMIntEQ
, tid
, last_lane
, "");
4427 ac_build_ifcc(ctx
, tmp
, 1000);
4428 LLVMBuildStore(builder
, ws
->src
, LLVMBuildGEP(builder
, ws
->scratch
, &ws
->waveidx
, 1, ""));
4429 ac_build_endif(ctx
, 1000);
4433 * "Bottom half" of a scan that reduces per-wave values across an entire
4436 * The caller must place a barrier between the top and bottom halves.
4439 ac_build_wg_wavescan_bottom(struct ac_llvm_context
*ctx
, struct ac_wg_scan
*ws
)
4441 const LLVMTypeRef type
= LLVMTypeOf(ws
->src
);
4442 const LLVMValueRef identity
=
4443 get_reduction_identity(ctx
, ws
->op
, ac_get_type_size(type
));
4445 if (ws
->maxwaves
<= 1) {
4446 ws
->result_reduce
= ws
->src
;
4447 ws
->result_inclusive
= ws
->src
;
4448 ws
->result_exclusive
= identity
;
4451 assert(ws
->maxwaves
<= 32);
4453 LLVMBuilderRef builder
= ctx
->builder
;
4454 LLVMValueRef tid
= ac_get_thread_id(ctx
);
4455 LLVMBasicBlockRef bbs
[2];
4456 LLVMValueRef phivalues_scan
[2];
4457 LLVMValueRef tmp
, tmp2
;
4459 bbs
[0] = LLVMGetInsertBlock(builder
);
4460 phivalues_scan
[0] = LLVMGetUndef(type
);
4462 if (ws
->enable_reduce
)
4463 tmp
= LLVMBuildICmp(builder
, LLVMIntULT
, tid
, ws
->numwaves
, "");
4464 else if (ws
->enable_inclusive
)
4465 tmp
= LLVMBuildICmp(builder
, LLVMIntULE
, tid
, ws
->waveidx
, "");
4467 tmp
= LLVMBuildICmp(builder
, LLVMIntULT
, tid
, ws
->waveidx
, "");
4468 ac_build_ifcc(ctx
, tmp
, 1001);
4470 tmp
= LLVMBuildLoad(builder
, LLVMBuildGEP(builder
, ws
->scratch
, &tid
, 1, ""), "");
4472 ac_build_optimization_barrier(ctx
, &tmp
);
4474 bbs
[1] = LLVMGetInsertBlock(builder
);
4475 phivalues_scan
[1] = ac_build_scan(ctx
, ws
->op
, tmp
, identity
, ws
->maxwaves
, true);
4477 ac_build_endif(ctx
, 1001);
4479 const LLVMValueRef scan
= ac_build_phi(ctx
, type
, 2, phivalues_scan
, bbs
);
4481 if (ws
->enable_reduce
) {
4482 tmp
= LLVMBuildSub(builder
, ws
->numwaves
, ctx
->i32_1
, "");
4483 ws
->result_reduce
= ac_build_readlane(ctx
, scan
, tmp
);
4485 if (ws
->enable_inclusive
)
4486 ws
->result_inclusive
= ac_build_readlane(ctx
, scan
, ws
->waveidx
);
4487 if (ws
->enable_exclusive
) {
4488 tmp
= LLVMBuildSub(builder
, ws
->waveidx
, ctx
->i32_1
, "");
4489 tmp
= ac_build_readlane(ctx
, scan
, tmp
);
4490 tmp2
= LLVMBuildICmp(builder
, LLVMIntEQ
, ws
->waveidx
, ctx
->i32_0
, "");
4491 ws
->result_exclusive
= LLVMBuildSelect(builder
, tmp2
, identity
, tmp
, "");
4496 * Inclusive scan of a per-wave value across an entire workgroup.
4498 * This implies an s_barrier instruction.
4500 * Unlike ac_build_inclusive_scan, the caller \em must ensure that all threads
4501 * of the workgroup are live. (This requirement cannot easily be relaxed in a
4502 * useful manner because of the barrier in the algorithm.)
4505 ac_build_wg_wavescan(struct ac_llvm_context
*ctx
, struct ac_wg_scan
*ws
)
4507 ac_build_wg_wavescan_top(ctx
, ws
);
4508 ac_build_s_barrier(ctx
);
4509 ac_build_wg_wavescan_bottom(ctx
, ws
);
4513 * "Top half" of a scan that reduces per-thread values across an entire
4516 * All lanes must be active when this code runs.
4519 ac_build_wg_scan_top(struct ac_llvm_context
*ctx
, struct ac_wg_scan
*ws
)
4521 if (ws
->enable_exclusive
) {
4522 ws
->extra
= ac_build_exclusive_scan(ctx
, ws
->src
, ws
->op
);
4523 if (LLVMTypeOf(ws
->src
) == ctx
->i1
&& ws
->op
== nir_op_iadd
)
4524 ws
->src
= LLVMBuildZExt(ctx
->builder
, ws
->src
, ctx
->i32
, "");
4525 ws
->src
= ac_build_alu_op(ctx
, ws
->extra
, ws
->src
, ws
->op
);
4527 ws
->src
= ac_build_inclusive_scan(ctx
, ws
->src
, ws
->op
);
4530 bool enable_inclusive
= ws
->enable_inclusive
;
4531 bool enable_exclusive
= ws
->enable_exclusive
;
4532 ws
->enable_inclusive
= false;
4533 ws
->enable_exclusive
= ws
->enable_exclusive
|| enable_inclusive
;
4534 ac_build_wg_wavescan_top(ctx
, ws
);
4535 ws
->enable_inclusive
= enable_inclusive
;
4536 ws
->enable_exclusive
= enable_exclusive
;
4540 * "Bottom half" of a scan that reduces per-thread values across an entire
4543 * The caller must place a barrier between the top and bottom halves.
4546 ac_build_wg_scan_bottom(struct ac_llvm_context
*ctx
, struct ac_wg_scan
*ws
)
4548 bool enable_inclusive
= ws
->enable_inclusive
;
4549 bool enable_exclusive
= ws
->enable_exclusive
;
4550 ws
->enable_inclusive
= false;
4551 ws
->enable_exclusive
= ws
->enable_exclusive
|| enable_inclusive
;
4552 ac_build_wg_wavescan_bottom(ctx
, ws
);
4553 ws
->enable_inclusive
= enable_inclusive
;
4554 ws
->enable_exclusive
= enable_exclusive
;
4556 /* ws->result_reduce is already the correct value */
4557 if (ws
->enable_inclusive
)
4558 ws
->result_inclusive
= ac_build_alu_op(ctx
, ws
->result_inclusive
, ws
->src
, ws
->op
);
4559 if (ws
->enable_exclusive
)
4560 ws
->result_exclusive
= ac_build_alu_op(ctx
, ws
->result_exclusive
, ws
->extra
, ws
->op
);
4564 * A scan that reduces per-thread values across an entire workgroup.
4566 * The caller must ensure that all lanes are active when this code runs
4567 * (WWM is insufficient!), because there is an implied barrier.
4570 ac_build_wg_scan(struct ac_llvm_context
*ctx
, struct ac_wg_scan
*ws
)
4572 ac_build_wg_scan_top(ctx
, ws
);
4573 ac_build_s_barrier(ctx
);
4574 ac_build_wg_scan_bottom(ctx
, ws
);
4578 ac_build_quad_swizzle(struct ac_llvm_context
*ctx
, LLVMValueRef src
,
4579 unsigned lane0
, unsigned lane1
, unsigned lane2
, unsigned lane3
)
4581 unsigned mask
= dpp_quad_perm(lane0
, lane1
, lane2
, lane3
);
4582 if (ctx
->chip_class
>= GFX8
) {
4583 return ac_build_dpp(ctx
, src
, src
, mask
, 0xf, 0xf, false);
4585 return ac_build_ds_swizzle(ctx
, src
, (1 << 15) | mask
);
4590 ac_build_shuffle(struct ac_llvm_context
*ctx
, LLVMValueRef src
, LLVMValueRef index
)
4592 LLVMTypeRef type
= LLVMTypeOf(src
);
4593 LLVMValueRef result
;
4595 index
= LLVMBuildMul(ctx
->builder
, index
, LLVMConstInt(ctx
->i32
, 4, 0), "");
4596 src
= LLVMBuildZExt(ctx
->builder
, src
, ctx
->i32
, "");
4598 result
= ac_build_intrinsic(ctx
, "llvm.amdgcn.ds.bpermute", ctx
->i32
,
4599 (LLVMValueRef
[]) {index
, src
}, 2,
4600 AC_FUNC_ATTR_READNONE
|
4601 AC_FUNC_ATTR_CONVERGENT
);
4602 return LLVMBuildTrunc(ctx
->builder
, result
, type
, "");
4606 ac_build_frexp_exp(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
4612 if (bitsize
== 16) {
4613 intr
= "llvm.amdgcn.frexp.exp.i16.f16";
4615 } else if (bitsize
== 32) {
4616 intr
= "llvm.amdgcn.frexp.exp.i32.f32";
4619 intr
= "llvm.amdgcn.frexp.exp.i32.f64";
4623 LLVMValueRef params
[] = {
4626 return ac_build_intrinsic(ctx
, intr
, type
, params
, 1,
4627 AC_FUNC_ATTR_READNONE
);
4630 ac_build_frexp_mant(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
4636 if (bitsize
== 16) {
4637 intr
= "llvm.amdgcn.frexp.mant.f16";
4639 } else if (bitsize
== 32) {
4640 intr
= "llvm.amdgcn.frexp.mant.f32";
4643 intr
= "llvm.amdgcn.frexp.mant.f64";
4647 LLVMValueRef params
[] = {
4650 return ac_build_intrinsic(ctx
, intr
, type
, params
, 1,
4651 AC_FUNC_ATTR_READNONE
);
4655 ac_build_canonicalize(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
4661 if (bitsize
== 16) {
4662 intr
= "llvm.canonicalize.f16";
4664 } else if (bitsize
== 32) {
4665 intr
= "llvm.canonicalize.f32";
4668 intr
= "llvm.canonicalize.f64";
4672 LLVMValueRef params
[] = {
4675 return ac_build_intrinsic(ctx
, intr
, type
, params
, 1,
4676 AC_FUNC_ATTR_READNONE
);
4680 * this takes an I,J coordinate pair,
4681 * and works out the X and Y derivatives.
4682 * it returns DDX(I), DDX(J), DDY(I), DDY(J).
4685 ac_build_ddxy_interp(struct ac_llvm_context
*ctx
, LLVMValueRef interp_ij
)
4687 LLVMValueRef result
[4], a
;
4690 for (i
= 0; i
< 2; i
++) {
4691 a
= LLVMBuildExtractElement(ctx
->builder
, interp_ij
,
4692 LLVMConstInt(ctx
->i32
, i
, false), "");
4693 result
[i
] = ac_build_ddxy(ctx
, AC_TID_MASK_TOP_LEFT
, 1, a
);
4694 result
[2+i
] = ac_build_ddxy(ctx
, AC_TID_MASK_TOP_LEFT
, 2, a
);
4696 return ac_build_gather_values(ctx
, result
, 4);
4700 ac_build_load_helper_invocation(struct ac_llvm_context
*ctx
)
4702 LLVMValueRef result
= ac_build_intrinsic(ctx
, "llvm.amdgcn.ps.live",
4704 AC_FUNC_ATTR_READNONE
);
4705 result
= LLVMBuildNot(ctx
->builder
, result
, "");
4706 return LLVMBuildSExt(ctx
->builder
, result
, ctx
->i32
, "");
4710 ac_build_is_helper_invocation(struct ac_llvm_context
*ctx
)
4712 if (!ctx
->postponed_kill
)
4713 return ac_build_load_helper_invocation(ctx
);
4715 /* !(exact && postponed) */
4716 LLVMValueRef exact
= ac_build_intrinsic(ctx
, "llvm.amdgcn.ps.live",
4718 AC_FUNC_ATTR_READNONE
);
4720 LLVMValueRef postponed
= LLVMBuildLoad(ctx
->builder
, ctx
->postponed_kill
, "");
4721 LLVMValueRef result
= LLVMBuildAnd(ctx
->builder
, exact
, postponed
, "");
4723 return LLVMBuildSelect(ctx
->builder
, result
, ctx
->i32_0
,
4724 LLVMConstInt(ctx
->i32
, 0xFFFFFFFF, false), "");
4727 LLVMValueRef
ac_build_call(struct ac_llvm_context
*ctx
, LLVMValueRef func
,
4728 LLVMValueRef
*args
, unsigned num_args
)
4730 LLVMValueRef ret
= LLVMBuildCall(ctx
->builder
, func
, args
, num_args
, "");
4731 LLVMSetInstructionCallConv(ret
, LLVMGetFunctionCallConv(func
));
4736 ac_export_mrt_z(struct ac_llvm_context
*ctx
, LLVMValueRef depth
,
4737 LLVMValueRef stencil
, LLVMValueRef samplemask
,
4738 struct ac_export_args
*args
)
4741 unsigned format
= ac_get_spi_shader_z_format(depth
!= NULL
,
4743 samplemask
!= NULL
);
4745 assert(depth
|| stencil
|| samplemask
);
4747 memset(args
, 0, sizeof(*args
));
4749 args
->valid_mask
= 1; /* whether the EXEC mask is valid */
4750 args
->done
= 1; /* DONE bit */
4752 /* Specify the target we are exporting */
4753 args
->target
= V_008DFC_SQ_EXP_MRTZ
;
4755 args
->compr
= 0; /* COMP flag */
4756 args
->out
[0] = LLVMGetUndef(ctx
->f32
); /* R, depth */
4757 args
->out
[1] = LLVMGetUndef(ctx
->f32
); /* G, stencil test val[0:7], stencil op val[8:15] */
4758 args
->out
[2] = LLVMGetUndef(ctx
->f32
); /* B, sample mask */
4759 args
->out
[3] = LLVMGetUndef(ctx
->f32
); /* A, alpha to mask */
4761 if (format
== V_028710_SPI_SHADER_UINT16_ABGR
) {
4763 args
->compr
= 1; /* COMPR flag */
4766 /* Stencil should be in X[23:16]. */
4767 stencil
= ac_to_integer(ctx
, stencil
);
4768 stencil
= LLVMBuildShl(ctx
->builder
, stencil
,
4769 LLVMConstInt(ctx
->i32
, 16, 0), "");
4770 args
->out
[0] = ac_to_float(ctx
, stencil
);
4774 /* SampleMask should be in Y[15:0]. */
4775 args
->out
[1] = samplemask
;
4780 args
->out
[0] = depth
;
4784 args
->out
[1] = stencil
;
4788 args
->out
[2] = samplemask
;
4793 /* GFX6 (except OLAND and HAINAN) has a bug that it only looks
4794 * at the X writemask component. */
4795 if (ctx
->chip_class
== GFX6
&&
4796 ctx
->family
!= CHIP_OLAND
&&
4797 ctx
->family
!= CHIP_HAINAN
)
4800 /* Specify which components to enable */
4801 args
->enabled_channels
= mask
;
4804 /* Send GS Alloc Req message from the first wave of the group to SPI.
4805 * Message payload is:
4806 * - bits 0..10: vertices in group
4807 * - bits 12..22: primitives in group
4809 void ac_build_sendmsg_gs_alloc_req(struct ac_llvm_context
*ctx
, LLVMValueRef wave_id
,
4810 LLVMValueRef vtx_cnt
, LLVMValueRef prim_cnt
)
4812 LLVMBuilderRef builder
= ctx
->builder
;
4814 bool export_dummy_prim
= false;
4816 /* HW workaround for a GPU hang with 100% culling.
4817 * We always have to export at least 1 primitive.
4818 * Export a degenerate triangle using vertex 0 for all 3 vertices.
4820 if (prim_cnt
== ctx
->i32_0
&& ctx
->chip_class
== GFX10
) {
4821 assert(vtx_cnt
== ctx
->i32_0
);
4822 prim_cnt
= ctx
->i32_1
;
4823 vtx_cnt
= ctx
->i32_1
;
4824 export_dummy_prim
= true;
4827 ac_build_ifcc(ctx
, LLVMBuildICmp(builder
, LLVMIntEQ
, wave_id
, ctx
->i32_0
, ""), 5020);
4829 tmp
= LLVMBuildShl(builder
, prim_cnt
, LLVMConstInt(ctx
->i32
, 12, false),"");
4830 tmp
= LLVMBuildOr(builder
, tmp
, vtx_cnt
, "");
4831 ac_build_sendmsg(ctx
, AC_SENDMSG_GS_ALLOC_REQ
, tmp
);
4833 if (export_dummy_prim
) {
4834 struct ac_ngg_prim prim
= {};
4835 /* The vertex indices are 0,0,0. */
4836 prim
.passthrough
= ctx
->i32_0
;
4838 struct ac_export_args pos
= {};
4839 pos
.out
[0] = pos
.out
[1] = pos
.out
[2] = pos
.out
[3] = ctx
->f32_0
;
4840 pos
.target
= V_008DFC_SQ_EXP_POS
;
4841 pos
.enabled_channels
= 0xf;
4844 ac_build_ifcc(ctx
, LLVMBuildICmp(builder
, LLVMIntEQ
, ac_get_thread_id(ctx
),
4845 ctx
->i32_0
, ""), 5021);
4846 ac_build_export_prim(ctx
, &prim
);
4847 ac_build_export(ctx
, &pos
);
4848 ac_build_endif(ctx
, 5021);
4851 ac_build_endif(ctx
, 5020);
4854 LLVMValueRef
ac_pack_prim_export(struct ac_llvm_context
*ctx
,
4855 const struct ac_ngg_prim
*prim
)
4857 /* The prim export format is:
4858 * - bits 0..8: index 0
4859 * - bit 9: edge flag 0
4860 * - bits 10..18: index 1
4861 * - bit 19: edge flag 1
4862 * - bits 20..28: index 2
4863 * - bit 29: edge flag 2
4864 * - bit 31: null primitive (skip)
4866 LLVMBuilderRef builder
= ctx
->builder
;
4867 LLVMValueRef tmp
= LLVMBuildZExt(builder
, prim
->isnull
, ctx
->i32
, "");
4868 LLVMValueRef result
= LLVMBuildShl(builder
, tmp
, LLVMConstInt(ctx
->i32
, 31, false), "");
4870 for (unsigned i
= 0; i
< prim
->num_vertices
; ++i
) {
4871 tmp
= LLVMBuildShl(builder
, prim
->index
[i
],
4872 LLVMConstInt(ctx
->i32
, 10 * i
, false), "");
4873 result
= LLVMBuildOr(builder
, result
, tmp
, "");
4874 tmp
= LLVMBuildZExt(builder
, prim
->edgeflag
[i
], ctx
->i32
, "");
4875 tmp
= LLVMBuildShl(builder
, tmp
,
4876 LLVMConstInt(ctx
->i32
, 10 * i
+ 9, false), "");
4877 result
= LLVMBuildOr(builder
, result
, tmp
, "");
4882 void ac_build_export_prim(struct ac_llvm_context
*ctx
,
4883 const struct ac_ngg_prim
*prim
)
4885 struct ac_export_args args
;
4887 if (prim
->passthrough
) {
4888 args
.out
[0] = prim
->passthrough
;
4890 args
.out
[0] = ac_pack_prim_export(ctx
, prim
);
4893 args
.out
[0] = LLVMBuildBitCast(ctx
->builder
, args
.out
[0], ctx
->f32
, "");
4894 args
.out
[1] = LLVMGetUndef(ctx
->f32
);
4895 args
.out
[2] = LLVMGetUndef(ctx
->f32
);
4896 args
.out
[3] = LLVMGetUndef(ctx
->f32
);
4898 args
.target
= V_008DFC_SQ_EXP_PRIM
;
4899 args
.enabled_channels
= 1;
4901 args
.valid_mask
= false;
4904 ac_build_export(ctx
, &args
);
4908 arg_llvm_type(enum ac_arg_type type
, unsigned size
, struct ac_llvm_context
*ctx
)
4910 if (type
== AC_ARG_FLOAT
) {
4911 return size
== 1 ? ctx
->f32
: LLVMVectorType(ctx
->f32
, size
);
4912 } else if (type
== AC_ARG_INT
) {
4913 return size
== 1 ? ctx
->i32
: LLVMVectorType(ctx
->i32
, size
);
4915 LLVMTypeRef ptr_type
;
4917 case AC_ARG_CONST_PTR
:
4920 case AC_ARG_CONST_FLOAT_PTR
:
4921 ptr_type
= ctx
->f32
;
4923 case AC_ARG_CONST_PTR_PTR
:
4924 ptr_type
= ac_array_in_const32_addr_space(ctx
->i8
);
4926 case AC_ARG_CONST_DESC_PTR
:
4927 ptr_type
= ctx
->v4i32
;
4929 case AC_ARG_CONST_IMAGE_PTR
:
4930 ptr_type
= ctx
->v8i32
;
4933 unreachable("unknown arg type");
4936 return ac_array_in_const32_addr_space(ptr_type
);
4939 return ac_array_in_const_addr_space(ptr_type
);
4945 ac_build_main(const struct ac_shader_args
*args
,
4946 struct ac_llvm_context
*ctx
,
4947 enum ac_llvm_calling_convention convention
,
4948 const char *name
, LLVMTypeRef ret_type
,
4949 LLVMModuleRef module
)
4951 LLVMTypeRef arg_types
[AC_MAX_ARGS
];
4953 for (unsigned i
= 0; i
< args
->arg_count
; i
++) {
4954 arg_types
[i
] = arg_llvm_type(args
->args
[i
].type
,
4955 args
->args
[i
].size
, ctx
);
4958 LLVMTypeRef main_function_type
=
4959 LLVMFunctionType(ret_type
, arg_types
, args
->arg_count
, 0);
4961 LLVMValueRef main_function
=
4962 LLVMAddFunction(module
, name
, main_function_type
);
4963 LLVMBasicBlockRef main_function_body
=
4964 LLVMAppendBasicBlockInContext(ctx
->context
, main_function
, "main_body");
4965 LLVMPositionBuilderAtEnd(ctx
->builder
, main_function_body
);
4967 LLVMSetFunctionCallConv(main_function
, convention
);
4968 for (unsigned i
= 0; i
< args
->arg_count
; ++i
) {
4969 LLVMValueRef P
= LLVMGetParam(main_function
, i
);
4971 if (args
->args
[i
].file
!= AC_ARG_SGPR
)
4974 ac_add_function_attr(ctx
->context
, main_function
, i
+ 1, AC_FUNC_ATTR_INREG
);
4976 if (LLVMGetTypeKind(LLVMTypeOf(P
)) == LLVMPointerTypeKind
) {
4977 ac_add_function_attr(ctx
->context
, main_function
, i
+ 1, AC_FUNC_ATTR_NOALIAS
);
4978 ac_add_attr_dereferenceable(P
, UINT64_MAX
);
4982 ctx
->main_function
= main_function
;
4984 if (LLVM_VERSION_MAJOR
>= 11) {
4985 /* Enable denormals for FP16 and FP64: */
4986 LLVMAddTargetDependentFunctionAttr(main_function
, "denormal-fp-math",
4988 /* Disable denormals for FP32: */
4989 LLVMAddTargetDependentFunctionAttr(main_function
, "denormal-fp-math-f32",
4990 "preserve-sign,preserve-sign");
4992 return main_function
;
4995 void ac_build_s_endpgm(struct ac_llvm_context
*ctx
)
4997 LLVMTypeRef calltype
= LLVMFunctionType(ctx
->voidt
, NULL
, 0, false);
4998 LLVMValueRef code
= LLVMConstInlineAsm(calltype
, "s_endpgm", "", true, false);
4999 LLVMBuildCall(ctx
->builder
, code
, NULL
, 0, "");
5002 LLVMValueRef
ac_prefix_bitcount(struct ac_llvm_context
*ctx
,
5003 LLVMValueRef mask
, LLVMValueRef index
)
5005 LLVMBuilderRef builder
= ctx
->builder
;
5006 LLVMTypeRef type
= LLVMTypeOf(mask
);
5008 LLVMValueRef bit
= LLVMBuildShl(builder
, LLVMConstInt(type
, 1, 0),
5009 LLVMBuildZExt(builder
, index
, type
, ""), "");
5010 LLVMValueRef prefix_bits
= LLVMBuildSub(builder
, bit
, LLVMConstInt(type
, 1, 0), "");
5011 LLVMValueRef prefix_mask
= LLVMBuildAnd(builder
, mask
, prefix_bits
, "");
5012 return ac_build_bit_count(ctx
, prefix_mask
);
5015 /* Compute the prefix sum of the "mask" bit array with 128 elements (bits). */
5016 LLVMValueRef
ac_prefix_bitcount_2x64(struct ac_llvm_context
*ctx
,
5017 LLVMValueRef mask
[2], LLVMValueRef index
)
5019 LLVMBuilderRef builder
= ctx
->builder
;
5021 /* Reference version using i128. */
5022 LLVMValueRef input_mask
=
5023 LLVMBuildBitCast(builder
, ac_build_gather_values(ctx
, mask
, 2), ctx
->i128
, "");
5025 return ac_prefix_bitcount(ctx
, input_mask
, index
);
5027 /* Optimized version using 2 64-bit masks. */
5028 LLVMValueRef is_hi
, is_0
, c64
, c128
, all_bits
;
5029 LLVMValueRef prefix_mask
[2], shift
[2], mask_bcnt0
, prefix_bcnt
[2];
5031 /* Compute the 128-bit prefix mask. */
5032 c64
= LLVMConstInt(ctx
->i32
, 64, 0);
5033 c128
= LLVMConstInt(ctx
->i32
, 128, 0);
5034 all_bits
= LLVMConstInt(ctx
->i64
, UINT64_MAX
, 0);
5035 /* The first index that can have non-zero high bits in the prefix mask is 65. */
5036 is_hi
= LLVMBuildICmp(builder
, LLVMIntUGT
, index
, c64
, "");
5037 is_0
= LLVMBuildICmp(builder
, LLVMIntEQ
, index
, ctx
->i32_0
, "");
5038 mask_bcnt0
= ac_build_bit_count(ctx
, mask
[0]);
5040 for (unsigned i
= 0; i
< 2; i
++) {
5041 shift
[i
] = LLVMBuildSub(builder
, i
? c128
: c64
, index
, "");
5042 /* For i==0, index==0, the right shift by 64 doesn't give the desired result,
5043 * so we handle it by the is_0 select.
5044 * For i==1, index==64, same story, so we handle it by the last is_hi select.
5045 * For i==0, index==64, we shift by 0, which is what we want.
5047 prefix_mask
[i
] = LLVMBuildLShr(builder
, all_bits
,
5048 LLVMBuildZExt(builder
, shift
[i
], ctx
->i64
, ""), "");
5049 prefix_mask
[i
] = LLVMBuildAnd(builder
, mask
[i
], prefix_mask
[i
], "");
5050 prefix_bcnt
[i
] = ac_build_bit_count(ctx
, prefix_mask
[i
]);
5053 prefix_bcnt
[0] = LLVMBuildSelect(builder
, is_0
, ctx
->i32_0
, prefix_bcnt
[0], "");
5054 prefix_bcnt
[0] = LLVMBuildSelect(builder
, is_hi
, mask_bcnt0
, prefix_bcnt
[0], "");
5055 prefix_bcnt
[1] = LLVMBuildSelect(builder
, is_hi
, prefix_bcnt
[1], ctx
->i32_0
, "");
5057 return LLVMBuildAdd(builder
, prefix_bcnt
[0], prefix_bcnt
[1], "");
5062 * Convert triangle strip indices to triangle indices. This is used to decompose
5063 * triangle strips into triangles.
5065 void ac_build_triangle_strip_indices_to_triangle(struct ac_llvm_context
*ctx
,
5066 LLVMValueRef is_odd
,
5067 LLVMValueRef flatshade_first
,
5068 LLVMValueRef index
[3])
5070 LLVMBuilderRef builder
= ctx
->builder
;
5071 LLVMValueRef out
[3];
5073 /* We need to change the vertex order for odd triangles to get correct
5074 * front/back facing by swapping 2 vertex indices, but we also have to
5075 * keep the provoking vertex in the same place.
5077 * If the first vertex is provoking, swap index 1 and 2.
5078 * If the last vertex is provoking, swap index 0 and 1.
5080 out
[0] = LLVMBuildSelect(builder
, flatshade_first
,
5082 LLVMBuildSelect(builder
, is_odd
,
5083 index
[1], index
[0], ""), "");
5084 out
[1] = LLVMBuildSelect(builder
, flatshade_first
,
5085 LLVMBuildSelect(builder
, is_odd
,
5086 index
[2], index
[1], ""),
5087 LLVMBuildSelect(builder
, is_odd
,
5088 index
[0], index
[1], ""), "");
5089 out
[2] = LLVMBuildSelect(builder
, flatshade_first
,
5090 LLVMBuildSelect(builder
, is_odd
,
5091 index
[1], index
[2], ""),
5093 memcpy(index
, out
, sizeof(out
));