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
)
70 ctx
->context
= LLVMContextCreate();
72 ctx
->chip_class
= chip_class
;
74 ctx
->wave_size
= wave_size
;
75 ctx
->ballot_mask_bits
= ballot_mask_bits
;
76 ctx
->float_mode
= float_mode
;
77 ctx
->module
= ac_create_module(wave_size
== 32 ? compiler
->tm_wave32
80 ctx
->builder
= ac_create_builder(ctx
->context
, float_mode
);
82 ctx
->voidt
= LLVMVoidTypeInContext(ctx
->context
);
83 ctx
->i1
= LLVMInt1TypeInContext(ctx
->context
);
84 ctx
->i8
= LLVMInt8TypeInContext(ctx
->context
);
85 ctx
->i16
= LLVMIntTypeInContext(ctx
->context
, 16);
86 ctx
->i32
= LLVMIntTypeInContext(ctx
->context
, 32);
87 ctx
->i64
= LLVMIntTypeInContext(ctx
->context
, 64);
88 ctx
->intptr
= ctx
->i32
;
89 ctx
->f16
= LLVMHalfTypeInContext(ctx
->context
);
90 ctx
->f32
= LLVMFloatTypeInContext(ctx
->context
);
91 ctx
->f64
= LLVMDoubleTypeInContext(ctx
->context
);
92 ctx
->v2i16
= LLVMVectorType(ctx
->i16
, 2);
93 ctx
->v2i32
= LLVMVectorType(ctx
->i32
, 2);
94 ctx
->v3i32
= LLVMVectorType(ctx
->i32
, 3);
95 ctx
->v4i32
= LLVMVectorType(ctx
->i32
, 4);
96 ctx
->v2f32
= LLVMVectorType(ctx
->f32
, 2);
97 ctx
->v3f32
= LLVMVectorType(ctx
->f32
, 3);
98 ctx
->v4f32
= LLVMVectorType(ctx
->f32
, 4);
99 ctx
->v8i32
= LLVMVectorType(ctx
->i32
, 8);
100 ctx
->iN_wavemask
= LLVMIntTypeInContext(ctx
->context
, ctx
->wave_size
);
101 ctx
->iN_ballotmask
= LLVMIntTypeInContext(ctx
->context
, ballot_mask_bits
);
103 ctx
->i8_0
= LLVMConstInt(ctx
->i8
, 0, false);
104 ctx
->i8_1
= LLVMConstInt(ctx
->i8
, 1, false);
105 ctx
->i16_0
= LLVMConstInt(ctx
->i16
, 0, false);
106 ctx
->i16_1
= LLVMConstInt(ctx
->i16
, 1, false);
107 ctx
->i32_0
= LLVMConstInt(ctx
->i32
, 0, false);
108 ctx
->i32_1
= LLVMConstInt(ctx
->i32
, 1, false);
109 ctx
->i64_0
= LLVMConstInt(ctx
->i64
, 0, false);
110 ctx
->i64_1
= LLVMConstInt(ctx
->i64
, 1, false);
111 ctx
->f16_0
= LLVMConstReal(ctx
->f16
, 0.0);
112 ctx
->f16_1
= LLVMConstReal(ctx
->f16
, 1.0);
113 ctx
->f32_0
= LLVMConstReal(ctx
->f32
, 0.0);
114 ctx
->f32_1
= LLVMConstReal(ctx
->f32
, 1.0);
115 ctx
->f64_0
= LLVMConstReal(ctx
->f64
, 0.0);
116 ctx
->f64_1
= LLVMConstReal(ctx
->f64
, 1.0);
118 ctx
->i1false
= LLVMConstInt(ctx
->i1
, 0, false);
119 ctx
->i1true
= LLVMConstInt(ctx
->i1
, 1, false);
121 ctx
->range_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
124 ctx
->invariant_load_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
125 "invariant.load", 14);
127 ctx
->fpmath_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
, "fpmath", 6);
129 args
[0] = LLVMConstReal(ctx
->f32
, 2.5);
130 ctx
->fpmath_md_2p5_ulp
= LLVMMDNodeInContext(ctx
->context
, args
, 1);
132 ctx
->uniform_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
133 "amdgpu.uniform", 14);
135 ctx
->empty_md
= LLVMMDNodeInContext(ctx
->context
, NULL
, 0);
136 ctx
->flow
= calloc(1, sizeof(*ctx
->flow
));
140 ac_llvm_context_dispose(struct ac_llvm_context
*ctx
)
142 free(ctx
->flow
->stack
);
148 ac_get_llvm_num_components(LLVMValueRef value
)
150 LLVMTypeRef type
= LLVMTypeOf(value
);
151 unsigned num_components
= LLVMGetTypeKind(type
) == LLVMVectorTypeKind
152 ? LLVMGetVectorSize(type
)
154 return num_components
;
158 ac_llvm_extract_elem(struct ac_llvm_context
*ac
,
162 if (LLVMGetTypeKind(LLVMTypeOf(value
)) != LLVMVectorTypeKind
) {
167 return LLVMBuildExtractElement(ac
->builder
, value
,
168 LLVMConstInt(ac
->i32
, index
, false), "");
172 ac_get_elem_bits(struct ac_llvm_context
*ctx
, LLVMTypeRef type
)
174 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
)
175 type
= LLVMGetElementType(type
);
177 if (LLVMGetTypeKind(type
) == LLVMIntegerTypeKind
)
178 return LLVMGetIntTypeWidth(type
);
180 if (type
== ctx
->f16
)
182 if (type
== ctx
->f32
)
184 if (type
== ctx
->f64
)
187 unreachable("Unhandled type kind in get_elem_bits");
191 ac_get_type_size(LLVMTypeRef type
)
193 LLVMTypeKind kind
= LLVMGetTypeKind(type
);
196 case LLVMIntegerTypeKind
:
197 return LLVMGetIntTypeWidth(type
) / 8;
198 case LLVMHalfTypeKind
:
200 case LLVMFloatTypeKind
:
202 case LLVMDoubleTypeKind
:
204 case LLVMPointerTypeKind
:
205 if (LLVMGetPointerAddressSpace(type
) == AC_ADDR_SPACE_CONST_32BIT
)
208 case LLVMVectorTypeKind
:
209 return LLVMGetVectorSize(type
) *
210 ac_get_type_size(LLVMGetElementType(type
));
211 case LLVMArrayTypeKind
:
212 return LLVMGetArrayLength(type
) *
213 ac_get_type_size(LLVMGetElementType(type
));
220 static LLVMTypeRef
to_integer_type_scalar(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
224 else if (t
== ctx
->f16
|| t
== ctx
->i16
)
226 else if (t
== ctx
->f32
|| t
== ctx
->i32
)
228 else if (t
== ctx
->f64
|| t
== ctx
->i64
)
231 unreachable("Unhandled integer size");
235 ac_to_integer_type(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
237 if (LLVMGetTypeKind(t
) == LLVMVectorTypeKind
) {
238 LLVMTypeRef elem_type
= LLVMGetElementType(t
);
239 return LLVMVectorType(to_integer_type_scalar(ctx
, elem_type
),
240 LLVMGetVectorSize(t
));
242 if (LLVMGetTypeKind(t
) == LLVMPointerTypeKind
) {
243 switch (LLVMGetPointerAddressSpace(t
)) {
244 case AC_ADDR_SPACE_GLOBAL
:
246 case AC_ADDR_SPACE_LDS
:
249 unreachable("unhandled address space");
252 return to_integer_type_scalar(ctx
, t
);
256 ac_to_integer(struct ac_llvm_context
*ctx
, LLVMValueRef v
)
258 LLVMTypeRef type
= LLVMTypeOf(v
);
259 if (LLVMGetTypeKind(type
) == LLVMPointerTypeKind
) {
260 return LLVMBuildPtrToInt(ctx
->builder
, v
, ac_to_integer_type(ctx
, type
), "");
262 return LLVMBuildBitCast(ctx
->builder
, v
, ac_to_integer_type(ctx
, type
), "");
266 ac_to_integer_or_pointer(struct ac_llvm_context
*ctx
, LLVMValueRef v
)
268 LLVMTypeRef type
= LLVMTypeOf(v
);
269 if (LLVMGetTypeKind(type
) == LLVMPointerTypeKind
)
271 return ac_to_integer(ctx
, v
);
274 static LLVMTypeRef
to_float_type_scalar(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
278 else if (t
== ctx
->i16
|| t
== ctx
->f16
)
280 else if (t
== ctx
->i32
|| t
== ctx
->f32
)
282 else if (t
== ctx
->i64
|| t
== ctx
->f64
)
285 unreachable("Unhandled float size");
289 ac_to_float_type(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
291 if (LLVMGetTypeKind(t
) == LLVMVectorTypeKind
) {
292 LLVMTypeRef elem_type
= LLVMGetElementType(t
);
293 return LLVMVectorType(to_float_type_scalar(ctx
, elem_type
),
294 LLVMGetVectorSize(t
));
296 return to_float_type_scalar(ctx
, t
);
300 ac_to_float(struct ac_llvm_context
*ctx
, LLVMValueRef v
)
302 LLVMTypeRef type
= LLVMTypeOf(v
);
303 return LLVMBuildBitCast(ctx
->builder
, v
, ac_to_float_type(ctx
, type
), "");
308 ac_build_intrinsic(struct ac_llvm_context
*ctx
, const char *name
,
309 LLVMTypeRef return_type
, LLVMValueRef
*params
,
310 unsigned param_count
, unsigned attrib_mask
)
312 LLVMValueRef function
, call
;
313 bool set_callsite_attrs
= !(attrib_mask
& AC_FUNC_ATTR_LEGACY
);
315 function
= LLVMGetNamedFunction(ctx
->module
, name
);
317 LLVMTypeRef param_types
[32], function_type
;
320 assert(param_count
<= 32);
322 for (i
= 0; i
< param_count
; ++i
) {
324 param_types
[i
] = LLVMTypeOf(params
[i
]);
327 LLVMFunctionType(return_type
, param_types
, param_count
, 0);
328 function
= LLVMAddFunction(ctx
->module
, name
, function_type
);
330 LLVMSetFunctionCallConv(function
, LLVMCCallConv
);
331 LLVMSetLinkage(function
, LLVMExternalLinkage
);
333 if (!set_callsite_attrs
)
334 ac_add_func_attributes(ctx
->context
, function
, attrib_mask
);
337 call
= LLVMBuildCall(ctx
->builder
, function
, params
, param_count
, "");
338 if (set_callsite_attrs
)
339 ac_add_func_attributes(ctx
->context
, call
, attrib_mask
);
344 * Given the i32 or vNi32 \p type, generate the textual name (e.g. for use with
347 void ac_build_type_name_for_intr(LLVMTypeRef type
, char *buf
, unsigned bufsize
)
349 LLVMTypeRef elem_type
= type
;
351 assert(bufsize
>= 8);
353 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
) {
354 int ret
= snprintf(buf
, bufsize
, "v%u",
355 LLVMGetVectorSize(type
));
357 char *type_name
= LLVMPrintTypeToString(type
);
358 fprintf(stderr
, "Error building type name for: %s\n",
360 LLVMDisposeMessage(type_name
);
363 elem_type
= LLVMGetElementType(type
);
367 switch (LLVMGetTypeKind(elem_type
)) {
369 case LLVMIntegerTypeKind
:
370 snprintf(buf
, bufsize
, "i%d", LLVMGetIntTypeWidth(elem_type
));
372 case LLVMHalfTypeKind
:
373 snprintf(buf
, bufsize
, "f16");
375 case LLVMFloatTypeKind
:
376 snprintf(buf
, bufsize
, "f32");
378 case LLVMDoubleTypeKind
:
379 snprintf(buf
, bufsize
, "f64");
385 * Helper function that builds an LLVM IR PHI node and immediately adds
389 ac_build_phi(struct ac_llvm_context
*ctx
, LLVMTypeRef type
,
390 unsigned count_incoming
, LLVMValueRef
*values
,
391 LLVMBasicBlockRef
*blocks
)
393 LLVMValueRef phi
= LLVMBuildPhi(ctx
->builder
, type
, "");
394 LLVMAddIncoming(phi
, values
, blocks
, count_incoming
);
398 void ac_build_s_barrier(struct ac_llvm_context
*ctx
)
400 ac_build_intrinsic(ctx
, "llvm.amdgcn.s.barrier", ctx
->voidt
, NULL
,
401 0, AC_FUNC_ATTR_CONVERGENT
);
404 /* Prevent optimizations (at least of memory accesses) across the current
405 * point in the program by emitting empty inline assembly that is marked as
406 * having side effects.
408 * Optionally, a value can be passed through the inline assembly to prevent
409 * LLVM from hoisting calls to ReadNone functions.
412 ac_build_optimization_barrier(struct ac_llvm_context
*ctx
,
415 static int counter
= 0;
417 LLVMBuilderRef builder
= ctx
->builder
;
420 snprintf(code
, sizeof(code
), "; %d", p_atomic_inc_return(&counter
));
423 LLVMTypeRef ftype
= LLVMFunctionType(ctx
->voidt
, NULL
, 0, false);
424 LLVMValueRef inlineasm
= LLVMConstInlineAsm(ftype
, code
, "", true, false);
425 LLVMBuildCall(builder
, inlineasm
, NULL
, 0, "");
427 LLVMTypeRef ftype
= LLVMFunctionType(ctx
->i32
, &ctx
->i32
, 1, false);
428 LLVMValueRef inlineasm
= LLVMConstInlineAsm(ftype
, code
, "=v,0", true, false);
429 LLVMValueRef vgpr
= *pvgpr
;
430 LLVMTypeRef vgpr_type
= LLVMTypeOf(vgpr
);
431 unsigned vgpr_size
= ac_get_type_size(vgpr_type
);
434 assert(vgpr_size
% 4 == 0);
436 vgpr
= LLVMBuildBitCast(builder
, vgpr
, LLVMVectorType(ctx
->i32
, vgpr_size
/ 4), "");
437 vgpr0
= LLVMBuildExtractElement(builder
, vgpr
, ctx
->i32_0
, "");
438 vgpr0
= LLVMBuildCall(builder
, inlineasm
, &vgpr0
, 1, "");
439 vgpr
= LLVMBuildInsertElement(builder
, vgpr
, vgpr0
, ctx
->i32_0
, "");
440 vgpr
= LLVMBuildBitCast(builder
, vgpr
, vgpr_type
, "");
447 ac_build_shader_clock(struct ac_llvm_context
*ctx
)
449 const char *intr
= LLVM_VERSION_MAJOR
>= 9 && ctx
->chip_class
>= GFX8
?
450 "llvm.amdgcn.s.memrealtime" : "llvm.readcyclecounter";
451 LLVMValueRef tmp
= ac_build_intrinsic(ctx
, intr
, ctx
->i64
, NULL
, 0, 0);
452 return LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->v2i32
, "");
456 ac_build_ballot(struct ac_llvm_context
*ctx
,
461 if (LLVM_VERSION_MAJOR
>= 9) {
462 if (ctx
->wave_size
== 64)
463 name
= "llvm.amdgcn.icmp.i64.i32";
465 name
= "llvm.amdgcn.icmp.i32.i32";
467 name
= "llvm.amdgcn.icmp.i32";
469 LLVMValueRef args
[3] = {
472 LLVMConstInt(ctx
->i32
, LLVMIntNE
, 0)
475 /* We currently have no other way to prevent LLVM from lifting the icmp
476 * calls to a dominating basic block.
478 ac_build_optimization_barrier(ctx
, &args
[0]);
480 args
[0] = ac_to_integer(ctx
, args
[0]);
482 return ac_build_intrinsic(ctx
, name
, ctx
->iN_wavemask
, args
, 3,
483 AC_FUNC_ATTR_NOUNWIND
|
484 AC_FUNC_ATTR_READNONE
|
485 AC_FUNC_ATTR_CONVERGENT
);
488 LLVMValueRef
ac_get_i1_sgpr_mask(struct ac_llvm_context
*ctx
,
491 const char *name
= LLVM_VERSION_MAJOR
>= 9 ? "llvm.amdgcn.icmp.i64.i1" : "llvm.amdgcn.icmp.i1";
492 LLVMValueRef args
[3] = {
495 LLVMConstInt(ctx
->i32
, LLVMIntNE
, 0),
498 return ac_build_intrinsic(ctx
, name
, ctx
->i64
, args
, 3,
499 AC_FUNC_ATTR_NOUNWIND
|
500 AC_FUNC_ATTR_READNONE
|
501 AC_FUNC_ATTR_CONVERGENT
);
505 ac_build_vote_all(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
507 LLVMValueRef active_set
= ac_build_ballot(ctx
, ctx
->i32_1
);
508 LLVMValueRef vote_set
= ac_build_ballot(ctx
, value
);
509 return LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, vote_set
, active_set
, "");
513 ac_build_vote_any(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
515 LLVMValueRef vote_set
= ac_build_ballot(ctx
, value
);
516 return LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, vote_set
,
517 LLVMConstInt(ctx
->iN_wavemask
, 0, 0), "");
521 ac_build_vote_eq(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
523 LLVMValueRef active_set
= ac_build_ballot(ctx
, ctx
->i32_1
);
524 LLVMValueRef vote_set
= ac_build_ballot(ctx
, value
);
526 LLVMValueRef all
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
527 vote_set
, active_set
, "");
528 LLVMValueRef none
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
530 LLVMConstInt(ctx
->iN_wavemask
, 0, 0), "");
531 return LLVMBuildOr(ctx
->builder
, all
, none
, "");
535 ac_build_varying_gather_values(struct ac_llvm_context
*ctx
, LLVMValueRef
*values
,
536 unsigned value_count
, unsigned component
)
538 LLVMValueRef vec
= NULL
;
540 if (value_count
== 1) {
541 return values
[component
];
542 } else if (!value_count
)
543 unreachable("value_count is 0");
545 for (unsigned i
= component
; i
< value_count
+ component
; i
++) {
546 LLVMValueRef value
= values
[i
];
549 vec
= LLVMGetUndef( LLVMVectorType(LLVMTypeOf(value
), value_count
));
550 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, i
- component
, false);
551 vec
= LLVMBuildInsertElement(ctx
->builder
, vec
, value
, index
, "");
557 ac_build_gather_values_extended(struct ac_llvm_context
*ctx
,
558 LLVMValueRef
*values
,
559 unsigned value_count
,
560 unsigned value_stride
,
564 LLVMBuilderRef builder
= ctx
->builder
;
565 LLVMValueRef vec
= NULL
;
568 if (value_count
== 1 && !always_vector
) {
570 return LLVMBuildLoad(builder
, values
[0], "");
572 } else if (!value_count
)
573 unreachable("value_count is 0");
575 for (i
= 0; i
< value_count
; i
++) {
576 LLVMValueRef value
= values
[i
* value_stride
];
578 value
= LLVMBuildLoad(builder
, value
, "");
581 vec
= LLVMGetUndef( LLVMVectorType(LLVMTypeOf(value
), value_count
));
582 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, i
, false);
583 vec
= LLVMBuildInsertElement(builder
, vec
, value
, index
, "");
589 ac_build_gather_values(struct ac_llvm_context
*ctx
,
590 LLVMValueRef
*values
,
591 unsigned value_count
)
593 return ac_build_gather_values_extended(ctx
, values
, value_count
, 1, false, false);
596 /* Expand a scalar or vector to <dst_channels x type> by filling the remaining
597 * channels with undef. Extract at most src_channels components from the input.
600 ac_build_expand(struct ac_llvm_context
*ctx
,
602 unsigned src_channels
,
603 unsigned dst_channels
)
605 LLVMTypeRef elemtype
;
606 LLVMValueRef chan
[dst_channels
];
608 if (LLVMGetTypeKind(LLVMTypeOf(value
)) == LLVMVectorTypeKind
) {
609 unsigned vec_size
= LLVMGetVectorSize(LLVMTypeOf(value
));
611 if (src_channels
== dst_channels
&& vec_size
== dst_channels
)
614 src_channels
= MIN2(src_channels
, vec_size
);
616 for (unsigned i
= 0; i
< src_channels
; i
++)
617 chan
[i
] = ac_llvm_extract_elem(ctx
, value
, i
);
619 elemtype
= LLVMGetElementType(LLVMTypeOf(value
));
622 assert(src_channels
== 1);
625 elemtype
= LLVMTypeOf(value
);
628 for (unsigned i
= src_channels
; i
< dst_channels
; i
++)
629 chan
[i
] = LLVMGetUndef(elemtype
);
631 return ac_build_gather_values(ctx
, chan
, dst_channels
);
634 /* Extract components [start, start + channels) from a vector.
637 ac_extract_components(struct ac_llvm_context
*ctx
,
642 LLVMValueRef chan
[channels
];
644 for (unsigned i
= 0; i
< channels
; i
++)
645 chan
[i
] = ac_llvm_extract_elem(ctx
, value
, i
+ start
);
647 return ac_build_gather_values(ctx
, chan
, channels
);
650 /* Expand a scalar or vector to <4 x type> by filling the remaining channels
651 * with undef. Extract at most num_channels components from the input.
653 LLVMValueRef
ac_build_expand_to_vec4(struct ac_llvm_context
*ctx
,
655 unsigned num_channels
)
657 return ac_build_expand(ctx
, value
, num_channels
, 4);
660 LLVMValueRef
ac_build_round(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
662 unsigned type_size
= ac_get_type_size(LLVMTypeOf(value
));
666 name
= "llvm.rint.f16";
667 else if (type_size
== 4)
668 name
= "llvm.rint.f32";
670 name
= "llvm.rint.f64";
672 return ac_build_intrinsic(ctx
, name
, LLVMTypeOf(value
), &value
, 1,
673 AC_FUNC_ATTR_READNONE
);
677 ac_build_fdiv(struct ac_llvm_context
*ctx
,
681 /* If we do (num / den), LLVM >= 7.0 does:
682 * return num * v_rcp_f32(den * (fabs(den) > 0x1.0p+96f ? 0x1.0p-32f : 1.0f));
684 * If we do (num * (1 / den)), LLVM does:
685 * return num * v_rcp_f32(den);
687 LLVMValueRef one
= LLVMConstReal(LLVMTypeOf(num
), 1.0);
688 LLVMValueRef rcp
= LLVMBuildFDiv(ctx
->builder
, one
, den
, "");
689 LLVMValueRef ret
= LLVMBuildFMul(ctx
->builder
, num
, rcp
, "");
691 /* Use v_rcp_f32 instead of precise division. */
692 if (!LLVMIsConstant(ret
))
693 LLVMSetMetadata(ret
, ctx
->fpmath_md_kind
, ctx
->fpmath_md_2p5_ulp
);
697 /* See fast_idiv_by_const.h. */
698 /* Set: increment = util_fast_udiv_info::increment ? multiplier : 0; */
699 LLVMValueRef
ac_build_fast_udiv(struct ac_llvm_context
*ctx
,
701 LLVMValueRef multiplier
,
702 LLVMValueRef pre_shift
,
703 LLVMValueRef post_shift
,
704 LLVMValueRef increment
)
706 LLVMBuilderRef builder
= ctx
->builder
;
708 num
= LLVMBuildLShr(builder
, num
, pre_shift
, "");
709 num
= LLVMBuildMul(builder
,
710 LLVMBuildZExt(builder
, num
, ctx
->i64
, ""),
711 LLVMBuildZExt(builder
, multiplier
, ctx
->i64
, ""), "");
712 num
= LLVMBuildAdd(builder
, num
,
713 LLVMBuildZExt(builder
, increment
, ctx
->i64
, ""), "");
714 num
= LLVMBuildLShr(builder
, num
, LLVMConstInt(ctx
->i64
, 32, 0), "");
715 num
= LLVMBuildTrunc(builder
, num
, ctx
->i32
, "");
716 return LLVMBuildLShr(builder
, num
, post_shift
, "");
719 /* See fast_idiv_by_const.h. */
720 /* If num != UINT_MAX, this more efficient version can be used. */
721 /* Set: increment = util_fast_udiv_info::increment; */
722 LLVMValueRef
ac_build_fast_udiv_nuw(struct ac_llvm_context
*ctx
,
724 LLVMValueRef multiplier
,
725 LLVMValueRef pre_shift
,
726 LLVMValueRef post_shift
,
727 LLVMValueRef increment
)
729 LLVMBuilderRef builder
= ctx
->builder
;
731 num
= LLVMBuildLShr(builder
, num
, pre_shift
, "");
732 num
= LLVMBuildNUWAdd(builder
, num
, increment
, "");
733 num
= LLVMBuildMul(builder
,
734 LLVMBuildZExt(builder
, num
, ctx
->i64
, ""),
735 LLVMBuildZExt(builder
, multiplier
, ctx
->i64
, ""), "");
736 num
= LLVMBuildLShr(builder
, num
, LLVMConstInt(ctx
->i64
, 32, 0), "");
737 num
= LLVMBuildTrunc(builder
, num
, ctx
->i32
, "");
738 return LLVMBuildLShr(builder
, num
, post_shift
, "");
741 /* See fast_idiv_by_const.h. */
742 /* Both operands must fit in 31 bits and the divisor must not be 1. */
743 LLVMValueRef
ac_build_fast_udiv_u31_d_not_one(struct ac_llvm_context
*ctx
,
745 LLVMValueRef multiplier
,
746 LLVMValueRef post_shift
)
748 LLVMBuilderRef builder
= ctx
->builder
;
750 num
= LLVMBuildMul(builder
,
751 LLVMBuildZExt(builder
, num
, ctx
->i64
, ""),
752 LLVMBuildZExt(builder
, multiplier
, ctx
->i64
, ""), "");
753 num
= LLVMBuildLShr(builder
, num
, LLVMConstInt(ctx
->i64
, 32, 0), "");
754 num
= LLVMBuildTrunc(builder
, num
, ctx
->i32
, "");
755 return LLVMBuildLShr(builder
, num
, post_shift
, "");
758 /* Coordinates for cube map selection. sc, tc, and ma are as in Table 8.27
759 * of the OpenGL 4.5 (Compatibility Profile) specification, except ma is
760 * already multiplied by two. id is the cube face number.
762 struct cube_selection_coords
{
769 build_cube_intrinsic(struct ac_llvm_context
*ctx
,
771 struct cube_selection_coords
*out
)
773 LLVMTypeRef f32
= ctx
->f32
;
775 out
->stc
[1] = ac_build_intrinsic(ctx
, "llvm.amdgcn.cubetc",
776 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
777 out
->stc
[0] = ac_build_intrinsic(ctx
, "llvm.amdgcn.cubesc",
778 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
779 out
->ma
= ac_build_intrinsic(ctx
, "llvm.amdgcn.cubema",
780 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
781 out
->id
= ac_build_intrinsic(ctx
, "llvm.amdgcn.cubeid",
782 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
786 * Build a manual selection sequence for cube face sc/tc coordinates and
787 * major axis vector (multiplied by 2 for consistency) for the given
788 * vec3 \p coords, for the face implied by \p selcoords.
790 * For the major axis, we always adjust the sign to be in the direction of
791 * selcoords.ma; i.e., a positive out_ma means that coords is pointed towards
792 * the selcoords major axis.
794 static void build_cube_select(struct ac_llvm_context
*ctx
,
795 const struct cube_selection_coords
*selcoords
,
796 const LLVMValueRef
*coords
,
797 LLVMValueRef
*out_st
,
798 LLVMValueRef
*out_ma
)
800 LLVMBuilderRef builder
= ctx
->builder
;
801 LLVMTypeRef f32
= LLVMTypeOf(coords
[0]);
802 LLVMValueRef is_ma_positive
;
804 LLVMValueRef is_ma_z
, is_not_ma_z
;
805 LLVMValueRef is_ma_y
;
806 LLVMValueRef is_ma_x
;
810 is_ma_positive
= LLVMBuildFCmp(builder
, LLVMRealUGE
,
811 selcoords
->ma
, LLVMConstReal(f32
, 0.0), "");
812 sgn_ma
= LLVMBuildSelect(builder
, is_ma_positive
,
813 LLVMConstReal(f32
, 1.0), LLVMConstReal(f32
, -1.0), "");
815 is_ma_z
= LLVMBuildFCmp(builder
, LLVMRealUGE
, selcoords
->id
, LLVMConstReal(f32
, 4.0), "");
816 is_not_ma_z
= LLVMBuildNot(builder
, is_ma_z
, "");
817 is_ma_y
= LLVMBuildAnd(builder
, is_not_ma_z
,
818 LLVMBuildFCmp(builder
, LLVMRealUGE
, selcoords
->id
, LLVMConstReal(f32
, 2.0), ""), "");
819 is_ma_x
= LLVMBuildAnd(builder
, is_not_ma_z
, LLVMBuildNot(builder
, is_ma_y
, ""), "");
822 tmp
= LLVMBuildSelect(builder
, is_ma_x
, coords
[2], coords
[0], "");
823 sgn
= LLVMBuildSelect(builder
, is_ma_y
, LLVMConstReal(f32
, 1.0),
824 LLVMBuildSelect(builder
, is_ma_z
, sgn_ma
,
825 LLVMBuildFNeg(builder
, sgn_ma
, ""), ""), "");
826 out_st
[0] = LLVMBuildFMul(builder
, tmp
, sgn
, "");
829 tmp
= LLVMBuildSelect(builder
, is_ma_y
, coords
[2], coords
[1], "");
830 sgn
= LLVMBuildSelect(builder
, is_ma_y
, sgn_ma
,
831 LLVMConstReal(f32
, -1.0), "");
832 out_st
[1] = LLVMBuildFMul(builder
, tmp
, sgn
, "");
835 tmp
= LLVMBuildSelect(builder
, is_ma_z
, coords
[2],
836 LLVMBuildSelect(builder
, is_ma_y
, coords
[1], coords
[0], ""), "");
837 tmp
= ac_build_intrinsic(ctx
, "llvm.fabs.f32",
838 ctx
->f32
, &tmp
, 1, AC_FUNC_ATTR_READNONE
);
839 *out_ma
= LLVMBuildFMul(builder
, tmp
, LLVMConstReal(f32
, 2.0), "");
843 ac_prepare_cube_coords(struct ac_llvm_context
*ctx
,
844 bool is_deriv
, bool is_array
, bool is_lod
,
845 LLVMValueRef
*coords_arg
,
846 LLVMValueRef
*derivs_arg
)
849 LLVMBuilderRef builder
= ctx
->builder
;
850 struct cube_selection_coords selcoords
;
851 LLVMValueRef coords
[3];
854 if (is_array
&& !is_lod
) {
855 LLVMValueRef tmp
= ac_build_round(ctx
, coords_arg
[3]);
857 /* Section 8.9 (Texture Functions) of the GLSL 4.50 spec says:
859 * "For Array forms, the array layer used will be
861 * max(0, min(d−1, floor(layer+0.5)))
863 * where d is the depth of the texture array and layer
864 * comes from the component indicated in the tables below.
865 * Workaroudn for an issue where the layer is taken from a
866 * helper invocation which happens to fall on a different
867 * layer due to extrapolation."
869 * GFX8 and earlier attempt to implement this in hardware by
870 * clamping the value of coords[2] = (8 * layer) + face.
871 * Unfortunately, this means that the we end up with the wrong
872 * face when clamping occurs.
874 * Clamp the layer earlier to work around the issue.
876 if (ctx
->chip_class
<= GFX8
) {
878 ge0
= LLVMBuildFCmp(builder
, LLVMRealOGE
, tmp
, ctx
->f32_0
, "");
879 tmp
= LLVMBuildSelect(builder
, ge0
, tmp
, ctx
->f32_0
, "");
885 build_cube_intrinsic(ctx
, coords_arg
, &selcoords
);
887 invma
= ac_build_intrinsic(ctx
, "llvm.fabs.f32",
888 ctx
->f32
, &selcoords
.ma
, 1, AC_FUNC_ATTR_READNONE
);
889 invma
= ac_build_fdiv(ctx
, LLVMConstReal(ctx
->f32
, 1.0), invma
);
891 for (int i
= 0; i
< 2; ++i
)
892 coords
[i
] = LLVMBuildFMul(builder
, selcoords
.stc
[i
], invma
, "");
894 coords
[2] = selcoords
.id
;
896 if (is_deriv
&& derivs_arg
) {
897 LLVMValueRef derivs
[4];
900 /* Convert cube derivatives to 2D derivatives. */
901 for (axis
= 0; axis
< 2; axis
++) {
902 LLVMValueRef deriv_st
[2];
903 LLVMValueRef deriv_ma
;
905 /* Transform the derivative alongside the texture
906 * coordinate. Mathematically, the correct formula is
907 * as follows. Assume we're projecting onto the +Z face
908 * and denote by dx/dh the derivative of the (original)
909 * X texture coordinate with respect to horizontal
910 * window coordinates. The projection onto the +Z face
915 * Then df/dh = df/dx * dx/dh + df/dz * dz/dh
916 * = 1/z * dx/dh - x/z * 1/z * dz/dh.
918 * This motivatives the implementation below.
920 * Whether this actually gives the expected results for
921 * apps that might feed in derivatives obtained via
922 * finite differences is anyone's guess. The OpenGL spec
923 * seems awfully quiet about how textureGrad for cube
924 * maps should be handled.
926 build_cube_select(ctx
, &selcoords
, &derivs_arg
[axis
* 3],
927 deriv_st
, &deriv_ma
);
929 deriv_ma
= LLVMBuildFMul(builder
, deriv_ma
, invma
, "");
931 for (int i
= 0; i
< 2; ++i
)
932 derivs
[axis
* 2 + i
] =
933 LLVMBuildFSub(builder
,
934 LLVMBuildFMul(builder
, deriv_st
[i
], invma
, ""),
935 LLVMBuildFMul(builder
, deriv_ma
, coords
[i
], ""), "");
938 memcpy(derivs_arg
, derivs
, sizeof(derivs
));
941 /* Shift the texture coordinate. This must be applied after the
942 * derivative calculation.
944 for (int i
= 0; i
< 2; ++i
)
945 coords
[i
] = LLVMBuildFAdd(builder
, coords
[i
], LLVMConstReal(ctx
->f32
, 1.5), "");
948 /* for cube arrays coord.z = coord.w(array_index) * 8 + face */
949 /* coords_arg.w component - array_index for cube arrays */
950 coords
[2] = ac_build_fmad(ctx
, coords_arg
[3], LLVMConstReal(ctx
->f32
, 8.0), coords
[2]);
953 memcpy(coords_arg
, coords
, sizeof(coords
));
958 ac_build_fs_interp(struct ac_llvm_context
*ctx
,
959 LLVMValueRef llvm_chan
,
960 LLVMValueRef attr_number
,
965 LLVMValueRef args
[5];
970 args
[2] = attr_number
;
973 p1
= ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p1",
974 ctx
->f32
, args
, 4, AC_FUNC_ATTR_READNONE
);
979 args
[3] = attr_number
;
982 return ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p2",
983 ctx
->f32
, args
, 5, AC_FUNC_ATTR_READNONE
);
987 ac_build_fs_interp_f16(struct ac_llvm_context
*ctx
,
988 LLVMValueRef llvm_chan
,
989 LLVMValueRef attr_number
,
994 LLVMValueRef args
[6];
999 args
[2] = attr_number
;
1000 args
[3] = ctx
->i1false
;
1003 p1
= ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p1.f16",
1004 ctx
->f32
, args
, 5, AC_FUNC_ATTR_READNONE
);
1008 args
[2] = llvm_chan
;
1009 args
[3] = attr_number
;
1010 args
[4] = ctx
->i1false
;
1013 return ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p2.f16",
1014 ctx
->f16
, args
, 6, AC_FUNC_ATTR_READNONE
);
1018 ac_build_fs_interp_mov(struct ac_llvm_context
*ctx
,
1019 LLVMValueRef parameter
,
1020 LLVMValueRef llvm_chan
,
1021 LLVMValueRef attr_number
,
1022 LLVMValueRef params
)
1024 LLVMValueRef args
[4];
1026 args
[0] = parameter
;
1027 args
[1] = llvm_chan
;
1028 args
[2] = attr_number
;
1031 return ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.mov",
1032 ctx
->f32
, args
, 4, AC_FUNC_ATTR_READNONE
);
1036 ac_build_gep_ptr(struct ac_llvm_context
*ctx
,
1037 LLVMValueRef base_ptr
,
1040 return LLVMBuildGEP(ctx
->builder
, base_ptr
, &index
, 1, "");
1044 ac_build_gep0(struct ac_llvm_context
*ctx
,
1045 LLVMValueRef base_ptr
,
1048 LLVMValueRef indices
[2] = {
1052 return LLVMBuildGEP(ctx
->builder
, base_ptr
, indices
, 2, "");
1055 LLVMValueRef
ac_build_pointer_add(struct ac_llvm_context
*ctx
, LLVMValueRef ptr
,
1058 return LLVMBuildPointerCast(ctx
->builder
,
1059 LLVMBuildGEP(ctx
->builder
, ptr
, &index
, 1, ""),
1060 LLVMTypeOf(ptr
), "");
1064 ac_build_indexed_store(struct ac_llvm_context
*ctx
,
1065 LLVMValueRef base_ptr
, LLVMValueRef index
,
1068 LLVMBuildStore(ctx
->builder
, value
,
1069 ac_build_gep0(ctx
, base_ptr
, index
));
1073 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad.
1074 * It's equivalent to doing a load from &base_ptr[index].
1076 * \param base_ptr Where the array starts.
1077 * \param index The element index into the array.
1078 * \param uniform Whether the base_ptr and index can be assumed to be
1079 * dynamically uniform (i.e. load to an SGPR)
1080 * \param invariant Whether the load is invariant (no other opcodes affect it)
1081 * \param no_unsigned_wraparound
1082 * For all possible re-associations and re-distributions of an expression
1083 * "base_ptr + index * elemsize" into "addr + offset" (excluding GEPs
1084 * without inbounds in base_ptr), this parameter is true if "addr + offset"
1085 * does not result in an unsigned integer wraparound. This is used for
1086 * optimal code generation of 32-bit pointer arithmetic.
1088 * For example, a 32-bit immediate offset that causes a 32-bit unsigned
1089 * integer wraparound can't be an imm offset in s_load_dword, because
1090 * the instruction performs "addr + offset" in 64 bits.
1092 * Expected usage for bindless textures by chaining GEPs:
1093 * // possible unsigned wraparound, don't use InBounds:
1094 * ptr1 = LLVMBuildGEP(base_ptr, index);
1095 * image = load(ptr1); // becomes "s_load ptr1, 0"
1097 * ptr2 = LLVMBuildInBoundsGEP(ptr1, 32 / elemsize);
1098 * sampler = load(ptr2); // becomes "s_load ptr1, 32" thanks to InBounds
1101 ac_build_load_custom(struct ac_llvm_context
*ctx
, LLVMValueRef base_ptr
,
1102 LLVMValueRef index
, bool uniform
, bool invariant
,
1103 bool no_unsigned_wraparound
)
1105 LLVMValueRef pointer
, result
;
1107 if (no_unsigned_wraparound
&&
1108 LLVMGetPointerAddressSpace(LLVMTypeOf(base_ptr
)) == AC_ADDR_SPACE_CONST_32BIT
)
1109 pointer
= LLVMBuildInBoundsGEP(ctx
->builder
, base_ptr
, &index
, 1, "");
1111 pointer
= LLVMBuildGEP(ctx
->builder
, base_ptr
, &index
, 1, "");
1114 LLVMSetMetadata(pointer
, ctx
->uniform_md_kind
, ctx
->empty_md
);
1115 result
= LLVMBuildLoad(ctx
->builder
, pointer
, "");
1117 LLVMSetMetadata(result
, ctx
->invariant_load_md_kind
, ctx
->empty_md
);
1121 LLVMValueRef
ac_build_load(struct ac_llvm_context
*ctx
, LLVMValueRef base_ptr
,
1124 return ac_build_load_custom(ctx
, base_ptr
, index
, false, false, false);
1127 LLVMValueRef
ac_build_load_invariant(struct ac_llvm_context
*ctx
,
1128 LLVMValueRef base_ptr
, LLVMValueRef index
)
1130 return ac_build_load_custom(ctx
, base_ptr
, index
, false, true, false);
1133 /* This assumes that there is no unsigned integer wraparound during the address
1134 * computation, excluding all GEPs within base_ptr. */
1135 LLVMValueRef
ac_build_load_to_sgpr(struct ac_llvm_context
*ctx
,
1136 LLVMValueRef base_ptr
, LLVMValueRef index
)
1138 return ac_build_load_custom(ctx
, base_ptr
, index
, true, true, true);
1141 /* See ac_build_load_custom() documentation. */
1142 LLVMValueRef
ac_build_load_to_sgpr_uint_wraparound(struct ac_llvm_context
*ctx
,
1143 LLVMValueRef base_ptr
, LLVMValueRef index
)
1145 return ac_build_load_custom(ctx
, base_ptr
, index
, true, true, false);
1148 static unsigned get_load_cache_policy(struct ac_llvm_context
*ctx
,
1149 unsigned cache_policy
)
1151 return cache_policy
|
1152 (ctx
->chip_class
>= GFX10
&& cache_policy
& ac_glc
? ac_dlc
: 0);
1156 ac_build_buffer_store_common(struct ac_llvm_context
*ctx
,
1159 LLVMValueRef vindex
,
1160 LLVMValueRef voffset
,
1161 LLVMValueRef soffset
,
1162 unsigned num_channels
,
1163 LLVMTypeRef return_channel_type
,
1164 unsigned cache_policy
,
1168 LLVMValueRef args
[6];
1171 args
[idx
++] = LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, "");
1173 args
[idx
++] = vindex
? vindex
: ctx
->i32_0
;
1174 args
[idx
++] = voffset
? voffset
: ctx
->i32_0
;
1175 args
[idx
++] = soffset
? soffset
: ctx
->i32_0
;
1176 args
[idx
++] = LLVMConstInt(ctx
->i32
, cache_policy
, 0);
1177 unsigned func
= !ac_has_vec3_support(ctx
->chip_class
, use_format
) && num_channels
== 3 ? 4 : num_channels
;
1178 const char *indexing_kind
= structurized
? "struct" : "raw";
1179 char name
[256], type_name
[8];
1181 LLVMTypeRef type
= func
> 1 ? LLVMVectorType(return_channel_type
, func
) : return_channel_type
;
1182 ac_build_type_name_for_intr(type
, type_name
, sizeof(type_name
));
1185 snprintf(name
, sizeof(name
), "llvm.amdgcn.%s.buffer.store.format.%s",
1186 indexing_kind
, type_name
);
1188 snprintf(name
, sizeof(name
), "llvm.amdgcn.%s.buffer.store.%s",
1189 indexing_kind
, type_name
);
1192 ac_build_intrinsic(ctx
, name
, ctx
->voidt
, args
, idx
,
1193 AC_FUNC_ATTR_INACCESSIBLE_MEM_ONLY
);
1197 ac_build_buffer_store_format(struct ac_llvm_context
*ctx
,
1200 LLVMValueRef vindex
,
1201 LLVMValueRef voffset
,
1202 unsigned num_channels
,
1203 unsigned cache_policy
)
1205 ac_build_buffer_store_common(ctx
, rsrc
, data
, vindex
,
1206 voffset
, NULL
, num_channels
,
1207 ctx
->f32
, cache_policy
,
1211 /* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
1212 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
1213 * or v4i32 (num_channels=3,4).
1216 ac_build_buffer_store_dword(struct ac_llvm_context
*ctx
,
1219 unsigned num_channels
,
1220 LLVMValueRef voffset
,
1221 LLVMValueRef soffset
,
1222 unsigned inst_offset
,
1223 unsigned cache_policy
,
1224 bool swizzle_enable_hint
)
1226 /* Split 3 channel stores, because only LLVM 9+ support 3-channel
1228 if (num_channels
== 3 && !ac_has_vec3_support(ctx
->chip_class
, false)) {
1229 LLVMValueRef v
[3], v01
;
1231 for (int i
= 0; i
< 3; i
++) {
1232 v
[i
] = LLVMBuildExtractElement(ctx
->builder
, vdata
,
1233 LLVMConstInt(ctx
->i32
, i
, 0), "");
1235 v01
= ac_build_gather_values(ctx
, v
, 2);
1237 ac_build_buffer_store_dword(ctx
, rsrc
, v01
, 2, voffset
,
1238 soffset
, inst_offset
, cache_policy
,
1239 swizzle_enable_hint
);
1240 ac_build_buffer_store_dword(ctx
, rsrc
, v
[2], 1, voffset
,
1241 soffset
, inst_offset
+ 8,
1243 swizzle_enable_hint
);
1247 /* SWIZZLE_ENABLE requires that soffset isn't folded into voffset
1248 * (voffset is swizzled, but soffset isn't swizzled).
1249 * llvm.amdgcn.buffer.store doesn't have a separate soffset parameter.
1251 if (!swizzle_enable_hint
) {
1252 LLVMValueRef offset
= soffset
;
1255 offset
= LLVMBuildAdd(ctx
->builder
, offset
,
1256 LLVMConstInt(ctx
->i32
, inst_offset
, 0), "");
1258 ac_build_buffer_store_common(ctx
, rsrc
, ac_to_float(ctx
, vdata
),
1259 ctx
->i32_0
, voffset
, offset
,
1260 num_channels
, ctx
->f32
,
1261 cache_policy
, false, false);
1265 static const unsigned dfmts
[] = {
1266 V_008F0C_BUF_DATA_FORMAT_32
,
1267 V_008F0C_BUF_DATA_FORMAT_32_32
,
1268 V_008F0C_BUF_DATA_FORMAT_32_32_32
,
1269 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
1271 unsigned dfmt
= dfmts
[num_channels
- 1];
1272 unsigned nfmt
= V_008F0C_BUF_NUM_FORMAT_UINT
;
1273 LLVMValueRef immoffset
= LLVMConstInt(ctx
->i32
, inst_offset
, 0);
1275 ac_build_raw_tbuffer_store(ctx
, rsrc
, vdata
, voffset
, soffset
,
1276 immoffset
, num_channels
, dfmt
, nfmt
, cache_policy
);
1280 ac_build_buffer_load_common(struct ac_llvm_context
*ctx
,
1282 LLVMValueRef vindex
,
1283 LLVMValueRef voffset
,
1284 LLVMValueRef soffset
,
1285 unsigned num_channels
,
1286 LLVMTypeRef channel_type
,
1287 unsigned cache_policy
,
1292 LLVMValueRef args
[5];
1294 args
[idx
++] = LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, "");
1296 args
[idx
++] = vindex
? vindex
: ctx
->i32_0
;
1297 args
[idx
++] = voffset
? voffset
: ctx
->i32_0
;
1298 args
[idx
++] = soffset
? soffset
: ctx
->i32_0
;
1299 args
[idx
++] = LLVMConstInt(ctx
->i32
, get_load_cache_policy(ctx
, cache_policy
), 0);
1300 unsigned func
= !ac_has_vec3_support(ctx
->chip_class
, use_format
) && num_channels
== 3 ? 4 : num_channels
;
1301 const char *indexing_kind
= structurized
? "struct" : "raw";
1302 char name
[256], type_name
[8];
1304 LLVMTypeRef type
= func
> 1 ? LLVMVectorType(channel_type
, func
) : channel_type
;
1305 ac_build_type_name_for_intr(type
, type_name
, sizeof(type_name
));
1308 snprintf(name
, sizeof(name
), "llvm.amdgcn.%s.buffer.load.format.%s",
1309 indexing_kind
, type_name
);
1311 snprintf(name
, sizeof(name
), "llvm.amdgcn.%s.buffer.load.%s",
1312 indexing_kind
, type_name
);
1315 return ac_build_intrinsic(ctx
, name
, type
, args
, idx
,
1316 ac_get_load_intr_attribs(can_speculate
));
1320 ac_build_buffer_load(struct ac_llvm_context
*ctx
,
1323 LLVMValueRef vindex
,
1324 LLVMValueRef voffset
,
1325 LLVMValueRef soffset
,
1326 unsigned inst_offset
,
1327 unsigned cache_policy
,
1331 LLVMValueRef offset
= LLVMConstInt(ctx
->i32
, inst_offset
, 0);
1333 offset
= LLVMBuildAdd(ctx
->builder
, offset
, voffset
, "");
1335 offset
= LLVMBuildAdd(ctx
->builder
, offset
, soffset
, "");
1337 if (allow_smem
&& !(cache_policy
& ac_slc
) &&
1338 (!(cache_policy
& ac_glc
) || ctx
->chip_class
>= GFX8
)) {
1339 assert(vindex
== NULL
);
1341 LLVMValueRef result
[8];
1343 for (int i
= 0; i
< num_channels
; i
++) {
1345 offset
= LLVMBuildAdd(ctx
->builder
, offset
,
1346 LLVMConstInt(ctx
->i32
, 4, 0), "");
1348 LLVMValueRef args
[3] = {
1351 LLVMConstInt(ctx
->i32
, get_load_cache_policy(ctx
, cache_policy
), 0),
1353 result
[i
] = ac_build_intrinsic(ctx
,
1354 "llvm.amdgcn.s.buffer.load.f32",
1356 AC_FUNC_ATTR_READNONE
);
1358 if (num_channels
== 1)
1361 if (num_channels
== 3 && !ac_has_vec3_support(ctx
->chip_class
, false))
1362 result
[num_channels
++] = LLVMGetUndef(ctx
->f32
);
1363 return ac_build_gather_values(ctx
, result
, num_channels
);
1366 return ac_build_buffer_load_common(ctx
, rsrc
, vindex
,
1368 num_channels
, ctx
->f32
,
1370 can_speculate
, false, false);
1373 LLVMValueRef
ac_build_buffer_load_format(struct ac_llvm_context
*ctx
,
1375 LLVMValueRef vindex
,
1376 LLVMValueRef voffset
,
1377 unsigned num_channels
,
1378 unsigned cache_policy
,
1381 return ac_build_buffer_load_common(ctx
, rsrc
, vindex
, voffset
,
1382 ctx
->i32_0
, num_channels
, ctx
->f32
,
1383 cache_policy
, can_speculate
,
1388 ac_build_tbuffer_load(struct ac_llvm_context
*ctx
,
1390 LLVMValueRef vindex
,
1391 LLVMValueRef voffset
,
1392 LLVMValueRef soffset
,
1393 LLVMValueRef immoffset
,
1394 unsigned num_channels
,
1397 unsigned cache_policy
,
1401 voffset
= LLVMBuildAdd(ctx
->builder
, voffset
, immoffset
, "");
1403 LLVMValueRef args
[6];
1405 args
[idx
++] = LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, "");
1407 args
[idx
++] = vindex
? vindex
: ctx
->i32_0
;
1408 args
[idx
++] = voffset
? voffset
: ctx
->i32_0
;
1409 args
[idx
++] = soffset
? soffset
: ctx
->i32_0
;
1410 args
[idx
++] = LLVMConstInt(ctx
->i32
, ac_get_tbuffer_format(ctx
->chip_class
, dfmt
, nfmt
), 0);
1411 args
[idx
++] = LLVMConstInt(ctx
->i32
, get_load_cache_policy(ctx
, cache_policy
), 0);
1412 unsigned func
= !ac_has_vec3_support(ctx
->chip_class
, true) && num_channels
== 3 ? 4 : num_channels
;
1413 const char *indexing_kind
= structurized
? "struct" : "raw";
1414 char name
[256], type_name
[8];
1416 LLVMTypeRef type
= func
> 1 ? LLVMVectorType(ctx
->i32
, func
) : ctx
->i32
;
1417 ac_build_type_name_for_intr(type
, type_name
, sizeof(type_name
));
1419 snprintf(name
, sizeof(name
), "llvm.amdgcn.%s.tbuffer.load.%s",
1420 indexing_kind
, type_name
);
1422 return ac_build_intrinsic(ctx
, name
, type
, args
, idx
,
1423 ac_get_load_intr_attribs(can_speculate
));
1427 ac_build_struct_tbuffer_load(struct ac_llvm_context
*ctx
,
1429 LLVMValueRef vindex
,
1430 LLVMValueRef voffset
,
1431 LLVMValueRef soffset
,
1432 LLVMValueRef immoffset
,
1433 unsigned num_channels
,
1436 unsigned cache_policy
,
1439 return ac_build_tbuffer_load(ctx
, rsrc
, vindex
, voffset
, soffset
,
1440 immoffset
, num_channels
, dfmt
, nfmt
,
1441 cache_policy
, can_speculate
, true);
1445 ac_build_raw_tbuffer_load(struct ac_llvm_context
*ctx
,
1447 LLVMValueRef voffset
,
1448 LLVMValueRef soffset
,
1449 LLVMValueRef immoffset
,
1450 unsigned num_channels
,
1453 unsigned cache_policy
,
1456 return ac_build_tbuffer_load(ctx
, rsrc
, NULL
, voffset
, soffset
,
1457 immoffset
, num_channels
, dfmt
, nfmt
,
1458 cache_policy
, can_speculate
, false);
1462 ac_build_tbuffer_load_short(struct ac_llvm_context
*ctx
,
1464 LLVMValueRef voffset
,
1465 LLVMValueRef soffset
,
1466 LLVMValueRef immoffset
,
1467 unsigned cache_policy
)
1471 if (LLVM_VERSION_MAJOR
>= 9) {
1472 voffset
= LLVMBuildAdd(ctx
->builder
, voffset
, immoffset
, "");
1474 /* LLVM 9+ supports i8/i16 with struct/raw intrinsics. */
1475 res
= ac_build_buffer_load_common(ctx
, rsrc
, NULL
,
1477 1, ctx
->i16
, cache_policy
,
1478 false, false, false);
1480 unsigned dfmt
= V_008F0C_BUF_DATA_FORMAT_16
;
1481 unsigned nfmt
= V_008F0C_BUF_NUM_FORMAT_UINT
;
1483 res
= ac_build_raw_tbuffer_load(ctx
, rsrc
, voffset
, soffset
,
1484 immoffset
, 1, dfmt
, nfmt
, cache_policy
,
1487 res
= LLVMBuildTrunc(ctx
->builder
, res
, ctx
->i16
, "");
1494 ac_build_tbuffer_load_byte(struct ac_llvm_context
*ctx
,
1496 LLVMValueRef voffset
,
1497 LLVMValueRef soffset
,
1498 LLVMValueRef immoffset
,
1499 unsigned cache_policy
)
1503 if (LLVM_VERSION_MAJOR
>= 9) {
1504 voffset
= LLVMBuildAdd(ctx
->builder
, voffset
, immoffset
, "");
1506 /* LLVM 9+ supports i8/i16 with struct/raw intrinsics. */
1507 res
= ac_build_buffer_load_common(ctx
, rsrc
, NULL
,
1509 1, ctx
->i8
, cache_policy
,
1510 false, false, false);
1512 unsigned dfmt
= V_008F0C_BUF_DATA_FORMAT_8
;
1513 unsigned nfmt
= V_008F0C_BUF_NUM_FORMAT_UINT
;
1515 res
= ac_build_raw_tbuffer_load(ctx
, rsrc
, voffset
, soffset
,
1516 immoffset
, 1, dfmt
, nfmt
, cache_policy
,
1519 res
= LLVMBuildTrunc(ctx
->builder
, res
, ctx
->i8
, "");
1526 * Convert an 11- or 10-bit unsigned floating point number to an f32.
1528 * The input exponent is expected to be biased analogous to IEEE-754, i.e. by
1529 * 2^(exp_bits-1) - 1 (as defined in OpenGL and other graphics APIs).
1532 ac_ufN_to_float(struct ac_llvm_context
*ctx
, LLVMValueRef src
, unsigned exp_bits
, unsigned mant_bits
)
1534 assert(LLVMTypeOf(src
) == ctx
->i32
);
1537 LLVMValueRef mantissa
;
1538 mantissa
= LLVMBuildAnd(ctx
->builder
, src
, LLVMConstInt(ctx
->i32
, (1 << mant_bits
) - 1, false), "");
1540 /* Converting normal numbers is just a shift + correcting the exponent bias */
1541 unsigned normal_shift
= 23 - mant_bits
;
1542 unsigned bias_shift
= 127 - ((1 << (exp_bits
- 1)) - 1);
1543 LLVMValueRef shifted
, normal
;
1545 shifted
= LLVMBuildShl(ctx
->builder
, src
, LLVMConstInt(ctx
->i32
, normal_shift
, false), "");
1546 normal
= LLVMBuildAdd(ctx
->builder
, shifted
, LLVMConstInt(ctx
->i32
, bias_shift
<< 23, false), "");
1548 /* Converting nan/inf numbers is the same, but with a different exponent update */
1549 LLVMValueRef naninf
;
1550 naninf
= LLVMBuildOr(ctx
->builder
, normal
, LLVMConstInt(ctx
->i32
, 0xff << 23, false), "");
1552 /* Converting denormals is the complex case: determine the leading zeros of the
1553 * mantissa to obtain the correct shift for the mantissa and exponent correction.
1555 LLVMValueRef denormal
;
1556 LLVMValueRef params
[2] = {
1558 ctx
->i1true
, /* result can be undef when arg is 0 */
1560 LLVMValueRef ctlz
= ac_build_intrinsic(ctx
, "llvm.ctlz.i32", ctx
->i32
,
1561 params
, 2, AC_FUNC_ATTR_READNONE
);
1563 /* Shift such that the leading 1 ends up as the LSB of the exponent field. */
1564 tmp
= LLVMBuildSub(ctx
->builder
, ctlz
, LLVMConstInt(ctx
->i32
, 8, false), "");
1565 denormal
= LLVMBuildShl(ctx
->builder
, mantissa
, tmp
, "");
1567 unsigned denormal_exp
= bias_shift
+ (32 - mant_bits
) - 1;
1568 tmp
= LLVMBuildSub(ctx
->builder
, LLVMConstInt(ctx
->i32
, denormal_exp
, false), ctlz
, "");
1569 tmp
= LLVMBuildShl(ctx
->builder
, tmp
, LLVMConstInt(ctx
->i32
, 23, false), "");
1570 denormal
= LLVMBuildAdd(ctx
->builder
, denormal
, tmp
, "");
1572 /* Select the final result. */
1573 LLVMValueRef result
;
1575 tmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntUGE
, src
,
1576 LLVMConstInt(ctx
->i32
, ((1 << exp_bits
) - 1) << mant_bits
, false), "");
1577 result
= LLVMBuildSelect(ctx
->builder
, tmp
, naninf
, normal
, "");
1579 tmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntUGE
, src
,
1580 LLVMConstInt(ctx
->i32
, 1 << mant_bits
, false), "");
1581 result
= LLVMBuildSelect(ctx
->builder
, tmp
, result
, denormal
, "");
1583 tmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src
, ctx
->i32_0
, "");
1584 result
= LLVMBuildSelect(ctx
->builder
, tmp
, result
, ctx
->i32_0
, "");
1586 return ac_to_float(ctx
, result
);
1590 * Generate a fully general open coded buffer format fetch with all required
1591 * fixups suitable for vertex fetch, using non-format buffer loads.
1593 * Some combinations of argument values have special interpretations:
1594 * - size = 8 bytes, format = fixed indicates PIPE_FORMAT_R11G11B10_FLOAT
1595 * - size = 8 bytes, format != {float,fixed} indicates a 2_10_10_10 data format
1597 * \param log_size log(size of channel in bytes)
1598 * \param num_channels number of channels (1 to 4)
1599 * \param format AC_FETCH_FORMAT_xxx value
1600 * \param reverse whether XYZ channels are reversed
1601 * \param known_aligned whether the source is known to be aligned to hardware's
1602 * effective element size for loading the given format
1603 * (note: this means dword alignment for 8_8_8_8, 16_16, etc.)
1604 * \param rsrc buffer resource descriptor
1605 * \return the resulting vector of floats or integers bitcast to <4 x i32>
1608 ac_build_opencoded_load_format(struct ac_llvm_context
*ctx
,
1610 unsigned num_channels
,
1615 LLVMValueRef vindex
,
1616 LLVMValueRef voffset
,
1617 LLVMValueRef soffset
,
1618 unsigned cache_policy
,
1622 unsigned load_log_size
= log_size
;
1623 unsigned load_num_channels
= num_channels
;
1624 if (log_size
== 3) {
1626 if (format
== AC_FETCH_FORMAT_FLOAT
) {
1627 load_num_channels
= 2 * num_channels
;
1629 load_num_channels
= 1; /* 10_11_11 or 2_10_10_10 */
1633 int log_recombine
= 0;
1634 if (ctx
->chip_class
== GFX6
&& !known_aligned
) {
1635 /* Avoid alignment restrictions by loading one byte at a time. */
1636 load_num_channels
<<= load_log_size
;
1637 log_recombine
= load_log_size
;
1639 } else if (load_num_channels
== 2 || load_num_channels
== 4) {
1640 log_recombine
= -util_logbase2(load_num_channels
);
1641 load_num_channels
= 1;
1642 load_log_size
+= -log_recombine
;
1645 assert(load_log_size
>= 2 || LLVM_VERSION_MAJOR
>= 9);
1647 LLVMValueRef loads
[32]; /* up to 32 bytes */
1648 for (unsigned i
= 0; i
< load_num_channels
; ++i
) {
1649 tmp
= LLVMBuildAdd(ctx
->builder
, soffset
,
1650 LLVMConstInt(ctx
->i32
, i
<< load_log_size
, false), "");
1651 LLVMTypeRef channel_type
= load_log_size
== 0 ? ctx
->i8
:
1652 load_log_size
== 1 ? ctx
->i16
: ctx
->i32
;
1653 unsigned num_channels
= 1 << (MAX2(load_log_size
, 2) - 2);
1654 loads
[i
] = ac_build_buffer_load_common(
1655 ctx
, rsrc
, vindex
, voffset
, tmp
,
1656 num_channels
, channel_type
, cache_policy
,
1657 can_speculate
, false, true);
1658 if (load_log_size
>= 2)
1659 loads
[i
] = ac_to_integer(ctx
, loads
[i
]);
1662 if (log_recombine
> 0) {
1663 /* Recombine bytes if necessary (GFX6 only) */
1664 LLVMTypeRef dst_type
= log_recombine
== 2 ? ctx
->i32
: ctx
->i16
;
1666 for (unsigned src
= 0, dst
= 0; src
< load_num_channels
; ++dst
) {
1667 LLVMValueRef accum
= NULL
;
1668 for (unsigned i
= 0; i
< (1 << log_recombine
); ++i
, ++src
) {
1669 tmp
= LLVMBuildZExt(ctx
->builder
, loads
[src
], dst_type
, "");
1673 tmp
= LLVMBuildShl(ctx
->builder
, tmp
,
1674 LLVMConstInt(dst_type
, 8 * i
, false), "");
1675 accum
= LLVMBuildOr(ctx
->builder
, accum
, tmp
, "");
1680 } else if (log_recombine
< 0) {
1681 /* Split vectors of dwords */
1682 if (load_log_size
> 2) {
1683 assert(load_num_channels
== 1);
1684 LLVMValueRef loaded
= loads
[0];
1685 unsigned log_split
= load_log_size
- 2;
1686 log_recombine
+= log_split
;
1687 load_num_channels
= 1 << log_split
;
1689 for (unsigned i
= 0; i
< load_num_channels
; ++i
) {
1690 tmp
= LLVMConstInt(ctx
->i32
, i
, false);
1691 loads
[i
] = LLVMBuildExtractElement(ctx
->builder
, loaded
, tmp
, "");
1695 /* Further split dwords and shorts if required */
1696 if (log_recombine
< 0) {
1697 for (unsigned src
= load_num_channels
,
1698 dst
= load_num_channels
<< -log_recombine
;
1700 unsigned dst_bits
= 1 << (3 + load_log_size
+ log_recombine
);
1701 LLVMTypeRef dst_type
= LLVMIntTypeInContext(ctx
->context
, dst_bits
);
1702 LLVMValueRef loaded
= loads
[src
- 1];
1703 LLVMTypeRef loaded_type
= LLVMTypeOf(loaded
);
1704 for (unsigned i
= 1 << -log_recombine
; i
> 0; --i
, --dst
) {
1705 tmp
= LLVMConstInt(loaded_type
, dst_bits
* (i
- 1), false);
1706 tmp
= LLVMBuildLShr(ctx
->builder
, loaded
, tmp
, "");
1707 loads
[dst
- 1] = LLVMBuildTrunc(ctx
->builder
, tmp
, dst_type
, "");
1713 if (log_size
== 3) {
1714 if (format
== AC_FETCH_FORMAT_FLOAT
) {
1715 for (unsigned i
= 0; i
< num_channels
; ++i
) {
1716 tmp
= ac_build_gather_values(ctx
, &loads
[2 * i
], 2);
1717 loads
[i
] = LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f64
, "");
1719 } else if (format
== AC_FETCH_FORMAT_FIXED
) {
1720 /* 10_11_11_FLOAT */
1721 LLVMValueRef data
= loads
[0];
1722 LLVMValueRef i32_2047
= LLVMConstInt(ctx
->i32
, 2047, false);
1723 LLVMValueRef r
= LLVMBuildAnd(ctx
->builder
, data
, i32_2047
, "");
1724 tmp
= LLVMBuildLShr(ctx
->builder
, data
, LLVMConstInt(ctx
->i32
, 11, false), "");
1725 LLVMValueRef g
= LLVMBuildAnd(ctx
->builder
, tmp
, i32_2047
, "");
1726 LLVMValueRef b
= LLVMBuildLShr(ctx
->builder
, data
, LLVMConstInt(ctx
->i32
, 22, false), "");
1728 loads
[0] = ac_to_integer(ctx
, ac_ufN_to_float(ctx
, r
, 5, 6));
1729 loads
[1] = ac_to_integer(ctx
, ac_ufN_to_float(ctx
, g
, 5, 6));
1730 loads
[2] = ac_to_integer(ctx
, ac_ufN_to_float(ctx
, b
, 5, 5));
1734 format
= AC_FETCH_FORMAT_FLOAT
;
1736 /* 2_10_10_10 data formats */
1737 LLVMValueRef data
= loads
[0];
1738 LLVMTypeRef i10
= LLVMIntTypeInContext(ctx
->context
, 10);
1739 LLVMTypeRef i2
= LLVMIntTypeInContext(ctx
->context
, 2);
1740 loads
[0] = LLVMBuildTrunc(ctx
->builder
, data
, i10
, "");
1741 tmp
= LLVMBuildLShr(ctx
->builder
, data
, LLVMConstInt(ctx
->i32
, 10, false), "");
1742 loads
[1] = LLVMBuildTrunc(ctx
->builder
, tmp
, i10
, "");
1743 tmp
= LLVMBuildLShr(ctx
->builder
, data
, LLVMConstInt(ctx
->i32
, 20, false), "");
1744 loads
[2] = LLVMBuildTrunc(ctx
->builder
, tmp
, i10
, "");
1745 tmp
= LLVMBuildLShr(ctx
->builder
, data
, LLVMConstInt(ctx
->i32
, 30, false), "");
1746 loads
[3] = LLVMBuildTrunc(ctx
->builder
, tmp
, i2
, "");
1752 if (format
== AC_FETCH_FORMAT_FLOAT
) {
1753 if (log_size
!= 2) {
1754 for (unsigned chan
= 0; chan
< num_channels
; ++chan
) {
1755 tmp
= ac_to_float(ctx
, loads
[chan
]);
1757 tmp
= LLVMBuildFPTrunc(ctx
->builder
, tmp
, ctx
->f32
, "");
1758 else if (log_size
== 1)
1759 tmp
= LLVMBuildFPExt(ctx
->builder
, tmp
, ctx
->f32
, "");
1760 loads
[chan
] = ac_to_integer(ctx
, tmp
);
1763 } else if (format
== AC_FETCH_FORMAT_UINT
) {
1764 if (log_size
!= 2) {
1765 for (unsigned chan
= 0; chan
< num_channels
; ++chan
)
1766 loads
[chan
] = LLVMBuildZExt(ctx
->builder
, loads
[chan
], ctx
->i32
, "");
1768 } else if (format
== AC_FETCH_FORMAT_SINT
) {
1769 if (log_size
!= 2) {
1770 for (unsigned chan
= 0; chan
< num_channels
; ++chan
)
1771 loads
[chan
] = LLVMBuildSExt(ctx
->builder
, loads
[chan
], ctx
->i32
, "");
1774 bool unsign
= format
== AC_FETCH_FORMAT_UNORM
||
1775 format
== AC_FETCH_FORMAT_USCALED
||
1776 format
== AC_FETCH_FORMAT_UINT
;
1778 for (unsigned chan
= 0; chan
< num_channels
; ++chan
) {
1780 tmp
= LLVMBuildUIToFP(ctx
->builder
, loads
[chan
], ctx
->f32
, "");
1782 tmp
= LLVMBuildSIToFP(ctx
->builder
, loads
[chan
], ctx
->f32
, "");
1785 LLVMValueRef scale
= NULL
;
1786 if (format
== AC_FETCH_FORMAT_FIXED
) {
1787 assert(log_size
== 2);
1788 scale
= LLVMConstReal(ctx
->f32
, 1.0 / 0x10000);
1789 } else if (format
== AC_FETCH_FORMAT_UNORM
) {
1790 unsigned bits
= LLVMGetIntTypeWidth(LLVMTypeOf(loads
[chan
]));
1791 scale
= LLVMConstReal(ctx
->f32
, 1.0 / (((uint64_t)1 << bits
) - 1));
1792 } else if (format
== AC_FETCH_FORMAT_SNORM
) {
1793 unsigned bits
= LLVMGetIntTypeWidth(LLVMTypeOf(loads
[chan
]));
1794 scale
= LLVMConstReal(ctx
->f32
, 1.0 / (((uint64_t)1 << (bits
- 1)) - 1));
1797 tmp
= LLVMBuildFMul(ctx
->builder
, tmp
, scale
, "");
1799 if (format
== AC_FETCH_FORMAT_SNORM
) {
1800 /* Clamp to [-1, 1] */
1801 LLVMValueRef neg_one
= LLVMConstReal(ctx
->f32
, -1.0);
1802 LLVMValueRef clamp
=
1803 LLVMBuildFCmp(ctx
->builder
, LLVMRealULT
, tmp
, neg_one
, "");
1804 tmp
= LLVMBuildSelect(ctx
->builder
, clamp
, neg_one
, tmp
, "");
1807 loads
[chan
] = ac_to_integer(ctx
, tmp
);
1811 while (num_channels
< 4) {
1812 if (format
== AC_FETCH_FORMAT_UINT
|| format
== AC_FETCH_FORMAT_SINT
) {
1813 loads
[num_channels
] = num_channels
== 3 ? ctx
->i32_1
: ctx
->i32_0
;
1815 loads
[num_channels
] = ac_to_integer(ctx
, num_channels
== 3 ? ctx
->f32_1
: ctx
->f32_0
);
1822 loads
[0] = loads
[2];
1826 return ac_build_gather_values(ctx
, loads
, 4);
1830 ac_build_tbuffer_store(struct ac_llvm_context
*ctx
,
1833 LLVMValueRef vindex
,
1834 LLVMValueRef voffset
,
1835 LLVMValueRef soffset
,
1836 LLVMValueRef immoffset
,
1837 unsigned num_channels
,
1840 unsigned cache_policy
,
1843 voffset
= LLVMBuildAdd(ctx
->builder
, voffset
? voffset
: ctx
->i32_0
,
1846 LLVMValueRef args
[7];
1848 args
[idx
++] = vdata
;
1849 args
[idx
++] = LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, "");
1851 args
[idx
++] = vindex
? vindex
: ctx
->i32_0
;
1852 args
[idx
++] = voffset
? voffset
: ctx
->i32_0
;
1853 args
[idx
++] = soffset
? soffset
: ctx
->i32_0
;
1854 args
[idx
++] = LLVMConstInt(ctx
->i32
, ac_get_tbuffer_format(ctx
->chip_class
, dfmt
, nfmt
), 0);
1855 args
[idx
++] = LLVMConstInt(ctx
->i32
, cache_policy
, 0);
1856 unsigned func
= !ac_has_vec3_support(ctx
->chip_class
, true) && num_channels
== 3 ? 4 : num_channels
;
1857 const char *indexing_kind
= structurized
? "struct" : "raw";
1858 char name
[256], type_name
[8];
1860 LLVMTypeRef type
= func
> 1 ? LLVMVectorType(ctx
->i32
, func
) : ctx
->i32
;
1861 ac_build_type_name_for_intr(type
, type_name
, sizeof(type_name
));
1863 snprintf(name
, sizeof(name
), "llvm.amdgcn.%s.tbuffer.store.%s",
1864 indexing_kind
, type_name
);
1866 ac_build_intrinsic(ctx
, name
, ctx
->voidt
, args
, idx
,
1867 AC_FUNC_ATTR_INACCESSIBLE_MEM_ONLY
);
1871 ac_build_struct_tbuffer_store(struct ac_llvm_context
*ctx
,
1874 LLVMValueRef vindex
,
1875 LLVMValueRef voffset
,
1876 LLVMValueRef soffset
,
1877 LLVMValueRef immoffset
,
1878 unsigned num_channels
,
1881 unsigned cache_policy
)
1883 ac_build_tbuffer_store(ctx
, rsrc
, vdata
, vindex
, voffset
, soffset
,
1884 immoffset
, num_channels
, dfmt
, nfmt
, cache_policy
,
1889 ac_build_raw_tbuffer_store(struct ac_llvm_context
*ctx
,
1892 LLVMValueRef voffset
,
1893 LLVMValueRef soffset
,
1894 LLVMValueRef immoffset
,
1895 unsigned num_channels
,
1898 unsigned cache_policy
)
1900 ac_build_tbuffer_store(ctx
, rsrc
, vdata
, NULL
, voffset
, soffset
,
1901 immoffset
, num_channels
, dfmt
, nfmt
, cache_policy
,
1906 ac_build_tbuffer_store_short(struct ac_llvm_context
*ctx
,
1909 LLVMValueRef voffset
,
1910 LLVMValueRef soffset
,
1911 unsigned cache_policy
)
1913 vdata
= LLVMBuildBitCast(ctx
->builder
, vdata
, ctx
->i16
, "");
1915 if (LLVM_VERSION_MAJOR
>= 9) {
1916 /* LLVM 9+ supports i8/i16 with struct/raw intrinsics. */
1917 ac_build_buffer_store_common(ctx
, rsrc
, vdata
, NULL
,
1918 voffset
, soffset
, 1,
1919 ctx
->i16
, cache_policy
,
1922 unsigned dfmt
= V_008F0C_BUF_DATA_FORMAT_16
;
1923 unsigned nfmt
= V_008F0C_BUF_NUM_FORMAT_UINT
;
1925 vdata
= LLVMBuildZExt(ctx
->builder
, vdata
, ctx
->i32
, "");
1927 ac_build_raw_tbuffer_store(ctx
, rsrc
, vdata
, voffset
, soffset
,
1928 ctx
->i32_0
, 1, dfmt
, nfmt
, cache_policy
);
1933 ac_build_tbuffer_store_byte(struct ac_llvm_context
*ctx
,
1936 LLVMValueRef voffset
,
1937 LLVMValueRef soffset
,
1938 unsigned cache_policy
)
1940 vdata
= LLVMBuildBitCast(ctx
->builder
, vdata
, ctx
->i8
, "");
1942 if (LLVM_VERSION_MAJOR
>= 9) {
1943 /* LLVM 9+ supports i8/i16 with struct/raw intrinsics. */
1944 ac_build_buffer_store_common(ctx
, rsrc
, vdata
, NULL
,
1945 voffset
, soffset
, 1,
1946 ctx
->i8
, cache_policy
,
1949 unsigned dfmt
= V_008F0C_BUF_DATA_FORMAT_8
;
1950 unsigned nfmt
= V_008F0C_BUF_NUM_FORMAT_UINT
;
1952 vdata
= LLVMBuildZExt(ctx
->builder
, vdata
, ctx
->i32
, "");
1954 ac_build_raw_tbuffer_store(ctx
, rsrc
, vdata
, voffset
, soffset
,
1955 ctx
->i32_0
, 1, dfmt
, nfmt
, cache_policy
);
1959 * Set range metadata on an instruction. This can only be used on load and
1960 * call instructions. If you know an instruction can only produce the values
1961 * 0, 1, 2, you would do set_range_metadata(value, 0, 3);
1962 * \p lo is the minimum value inclusive.
1963 * \p hi is the maximum value exclusive.
1965 static void set_range_metadata(struct ac_llvm_context
*ctx
,
1966 LLVMValueRef value
, unsigned lo
, unsigned hi
)
1968 LLVMValueRef range_md
, md_args
[2];
1969 LLVMTypeRef type
= LLVMTypeOf(value
);
1970 LLVMContextRef context
= LLVMGetTypeContext(type
);
1972 md_args
[0] = LLVMConstInt(type
, lo
, false);
1973 md_args
[1] = LLVMConstInt(type
, hi
, false);
1974 range_md
= LLVMMDNodeInContext(context
, md_args
, 2);
1975 LLVMSetMetadata(value
, ctx
->range_md_kind
, range_md
);
1979 ac_get_thread_id(struct ac_llvm_context
*ctx
)
1983 LLVMValueRef tid_args
[2];
1984 tid_args
[0] = LLVMConstInt(ctx
->i32
, 0xffffffff, false);
1985 tid_args
[1] = ctx
->i32_0
;
1986 tid_args
[1] = ac_build_intrinsic(ctx
,
1987 "llvm.amdgcn.mbcnt.lo", ctx
->i32
,
1988 tid_args
, 2, AC_FUNC_ATTR_READNONE
);
1990 if (ctx
->wave_size
== 32) {
1993 tid
= ac_build_intrinsic(ctx
, "llvm.amdgcn.mbcnt.hi",
1995 2, AC_FUNC_ATTR_READNONE
);
1997 set_range_metadata(ctx
, tid
, 0, ctx
->wave_size
);
2002 * AMD GCN implements derivatives using the local data store (LDS)
2003 * All writes to the LDS happen in all executing threads at
2004 * the same time. TID is the Thread ID for the current
2005 * thread and is a value between 0 and 63, representing
2006 * the thread's position in the wavefront.
2008 * For the pixel shader threads are grouped into quads of four pixels.
2009 * The TIDs of the pixels of a quad are:
2017 * So, masking the TID with 0xfffffffc yields the TID of the top left pixel
2018 * of the quad, masking with 0xfffffffd yields the TID of the top pixel of
2019 * the current pixel's column, and masking with 0xfffffffe yields the TID
2020 * of the left pixel of the current pixel's row.
2022 * Adding 1 yields the TID of the pixel to the right of the left pixel, and
2023 * adding 2 yields the TID of the pixel below the top pixel.
2026 ac_build_ddxy(struct ac_llvm_context
*ctx
,
2031 unsigned tl_lanes
[4], trbl_lanes
[4];
2032 char name
[32], type
[8];
2033 LLVMValueRef tl
, trbl
;
2034 LLVMTypeRef result_type
;
2035 LLVMValueRef result
;
2037 result_type
= ac_to_float_type(ctx
, LLVMTypeOf(val
));
2039 if (result_type
== ctx
->f16
)
2040 val
= LLVMBuildZExt(ctx
->builder
, val
, ctx
->i32
, "");
2042 for (unsigned i
= 0; i
< 4; ++i
) {
2043 tl_lanes
[i
] = i
& mask
;
2044 trbl_lanes
[i
] = (i
& mask
) + idx
;
2047 tl
= ac_build_quad_swizzle(ctx
, val
,
2048 tl_lanes
[0], tl_lanes
[1],
2049 tl_lanes
[2], tl_lanes
[3]);
2050 trbl
= ac_build_quad_swizzle(ctx
, val
,
2051 trbl_lanes
[0], trbl_lanes
[1],
2052 trbl_lanes
[2], trbl_lanes
[3]);
2054 if (result_type
== ctx
->f16
) {
2055 tl
= LLVMBuildTrunc(ctx
->builder
, tl
, ctx
->i16
, "");
2056 trbl
= LLVMBuildTrunc(ctx
->builder
, trbl
, ctx
->i16
, "");
2059 tl
= LLVMBuildBitCast(ctx
->builder
, tl
, result_type
, "");
2060 trbl
= LLVMBuildBitCast(ctx
->builder
, trbl
, result_type
, "");
2061 result
= LLVMBuildFSub(ctx
->builder
, trbl
, tl
, "");
2063 ac_build_type_name_for_intr(result_type
, type
, sizeof(type
));
2064 snprintf(name
, sizeof(name
), "llvm.amdgcn.wqm.%s", type
);
2066 return ac_build_intrinsic(ctx
, name
, result_type
, &result
, 1, 0);
2070 ac_build_sendmsg(struct ac_llvm_context
*ctx
,
2072 LLVMValueRef wave_id
)
2074 LLVMValueRef args
[2];
2075 args
[0] = LLVMConstInt(ctx
->i32
, msg
, false);
2077 ac_build_intrinsic(ctx
, "llvm.amdgcn.s.sendmsg", ctx
->voidt
, args
, 2, 0);
2081 ac_build_imsb(struct ac_llvm_context
*ctx
,
2083 LLVMTypeRef dst_type
)
2085 LLVMValueRef msb
= ac_build_intrinsic(ctx
, "llvm.amdgcn.sffbh.i32",
2087 AC_FUNC_ATTR_READNONE
);
2089 /* The HW returns the last bit index from MSB, but NIR/TGSI wants
2090 * the index from LSB. Invert it by doing "31 - msb". */
2091 msb
= LLVMBuildSub(ctx
->builder
, LLVMConstInt(ctx
->i32
, 31, false),
2094 LLVMValueRef all_ones
= LLVMConstInt(ctx
->i32
, -1, true);
2095 LLVMValueRef cond
= LLVMBuildOr(ctx
->builder
,
2096 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
2097 arg
, ctx
->i32_0
, ""),
2098 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
2099 arg
, all_ones
, ""), "");
2101 return LLVMBuildSelect(ctx
->builder
, cond
, all_ones
, msb
, "");
2105 ac_build_umsb(struct ac_llvm_context
*ctx
,
2107 LLVMTypeRef dst_type
)
2109 const char *intrin_name
;
2111 LLVMValueRef highest_bit
;
2115 bitsize
= ac_get_elem_bits(ctx
, LLVMTypeOf(arg
));
2118 intrin_name
= "llvm.ctlz.i64";
2120 highest_bit
= LLVMConstInt(ctx
->i64
, 63, false);
2124 intrin_name
= "llvm.ctlz.i32";
2126 highest_bit
= LLVMConstInt(ctx
->i32
, 31, false);
2130 intrin_name
= "llvm.ctlz.i16";
2132 highest_bit
= LLVMConstInt(ctx
->i16
, 15, false);
2136 intrin_name
= "llvm.ctlz.i8";
2138 highest_bit
= LLVMConstInt(ctx
->i8
, 7, false);
2142 unreachable(!"invalid bitsize");
2146 LLVMValueRef params
[2] = {
2151 LLVMValueRef msb
= ac_build_intrinsic(ctx
, intrin_name
, type
,
2153 AC_FUNC_ATTR_READNONE
);
2155 /* The HW returns the last bit index from MSB, but TGSI/NIR wants
2156 * the index from LSB. Invert it by doing "31 - msb". */
2157 msb
= LLVMBuildSub(ctx
->builder
, highest_bit
, msb
, "");
2159 if (bitsize
== 64) {
2160 msb
= LLVMBuildTrunc(ctx
->builder
, msb
, ctx
->i32
, "");
2161 } else if (bitsize
< 32) {
2162 msb
= LLVMBuildSExt(ctx
->builder
, msb
, ctx
->i32
, "");
2165 /* check for zero */
2166 return LLVMBuildSelect(ctx
->builder
,
2167 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, arg
, zero
, ""),
2168 LLVMConstInt(ctx
->i32
, -1, true), msb
, "");
2171 LLVMValueRef
ac_build_fmin(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
2175 snprintf(name
, sizeof(name
), "llvm.minnum.f%d", ac_get_elem_bits(ctx
, LLVMTypeOf(a
)));
2176 LLVMValueRef args
[2] = {a
, b
};
2177 return ac_build_intrinsic(ctx
, name
, LLVMTypeOf(a
), args
, 2,
2178 AC_FUNC_ATTR_READNONE
);
2181 LLVMValueRef
ac_build_fmax(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
2185 snprintf(name
, sizeof(name
), "llvm.maxnum.f%d", ac_get_elem_bits(ctx
, LLVMTypeOf(a
)));
2186 LLVMValueRef args
[2] = {a
, b
};
2187 return ac_build_intrinsic(ctx
, name
, LLVMTypeOf(a
), args
, 2,
2188 AC_FUNC_ATTR_READNONE
);
2191 LLVMValueRef
ac_build_imin(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
2194 LLVMValueRef cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSLE
, a
, b
, "");
2195 return LLVMBuildSelect(ctx
->builder
, cmp
, a
, b
, "");
2198 LLVMValueRef
ac_build_imax(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
2201 LLVMValueRef cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGT
, a
, b
, "");
2202 return LLVMBuildSelect(ctx
->builder
, cmp
, a
, b
, "");
2205 LLVMValueRef
ac_build_umin(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
2208 LLVMValueRef cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntULE
, a
, b
, "");
2209 return LLVMBuildSelect(ctx
->builder
, cmp
, a
, b
, "");
2212 LLVMValueRef
ac_build_umax(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
2215 LLVMValueRef cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntUGE
, a
, b
, "");
2216 return LLVMBuildSelect(ctx
->builder
, cmp
, a
, b
, "");
2219 LLVMValueRef
ac_build_clamp(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
2221 LLVMTypeRef t
= LLVMTypeOf(value
);
2222 return ac_build_fmin(ctx
, ac_build_fmax(ctx
, value
, LLVMConstReal(t
, 0.0)),
2223 LLVMConstReal(t
, 1.0));
2226 void ac_build_export(struct ac_llvm_context
*ctx
, struct ac_export_args
*a
)
2228 LLVMValueRef args
[9];
2230 args
[0] = LLVMConstInt(ctx
->i32
, a
->target
, 0);
2231 args
[1] = LLVMConstInt(ctx
->i32
, a
->enabled_channels
, 0);
2234 LLVMTypeRef i16
= LLVMInt16TypeInContext(ctx
->context
);
2235 LLVMTypeRef v2i16
= LLVMVectorType(i16
, 2);
2237 args
[2] = LLVMBuildBitCast(ctx
->builder
, a
->out
[0],
2239 args
[3] = LLVMBuildBitCast(ctx
->builder
, a
->out
[1],
2241 args
[4] = LLVMConstInt(ctx
->i1
, a
->done
, 0);
2242 args
[5] = LLVMConstInt(ctx
->i1
, a
->valid_mask
, 0);
2244 ac_build_intrinsic(ctx
, "llvm.amdgcn.exp.compr.v2i16",
2245 ctx
->voidt
, args
, 6, 0);
2247 args
[2] = a
->out
[0];
2248 args
[3] = a
->out
[1];
2249 args
[4] = a
->out
[2];
2250 args
[5] = a
->out
[3];
2251 args
[6] = LLVMConstInt(ctx
->i1
, a
->done
, 0);
2252 args
[7] = LLVMConstInt(ctx
->i1
, a
->valid_mask
, 0);
2254 ac_build_intrinsic(ctx
, "llvm.amdgcn.exp.f32",
2255 ctx
->voidt
, args
, 8, 0);
2259 void ac_build_export_null(struct ac_llvm_context
*ctx
)
2261 struct ac_export_args args
;
2263 args
.enabled_channels
= 0x0; /* enabled channels */
2264 args
.valid_mask
= 1; /* whether the EXEC mask is valid */
2265 args
.done
= 1; /* DONE bit */
2266 args
.target
= V_008DFC_SQ_EXP_NULL
;
2267 args
.compr
= 0; /* COMPR flag (0 = 32-bit export) */
2268 args
.out
[0] = LLVMGetUndef(ctx
->f32
); /* R */
2269 args
.out
[1] = LLVMGetUndef(ctx
->f32
); /* G */
2270 args
.out
[2] = LLVMGetUndef(ctx
->f32
); /* B */
2271 args
.out
[3] = LLVMGetUndef(ctx
->f32
); /* A */
2273 ac_build_export(ctx
, &args
);
2276 static unsigned ac_num_coords(enum ac_image_dim dim
)
2282 case ac_image_1darray
:
2286 case ac_image_2darray
:
2287 case ac_image_2dmsaa
:
2289 case ac_image_2darraymsaa
:
2292 unreachable("ac_num_coords: bad dim");
2296 static unsigned ac_num_derivs(enum ac_image_dim dim
)
2300 case ac_image_1darray
:
2303 case ac_image_2darray
:
2308 case ac_image_2dmsaa
:
2309 case ac_image_2darraymsaa
:
2311 unreachable("derivatives not supported");
2315 static const char *get_atomic_name(enum ac_atomic_op op
)
2318 case ac_atomic_swap
: return "swap";
2319 case ac_atomic_add
: return "add";
2320 case ac_atomic_sub
: return "sub";
2321 case ac_atomic_smin
: return "smin";
2322 case ac_atomic_umin
: return "umin";
2323 case ac_atomic_smax
: return "smax";
2324 case ac_atomic_umax
: return "umax";
2325 case ac_atomic_and
: return "and";
2326 case ac_atomic_or
: return "or";
2327 case ac_atomic_xor
: return "xor";
2328 case ac_atomic_inc_wrap
: return "inc";
2329 case ac_atomic_dec_wrap
: return "dec";
2331 unreachable("bad atomic op");
2334 LLVMValueRef
ac_build_image_opcode(struct ac_llvm_context
*ctx
,
2335 struct ac_image_args
*a
)
2337 const char *overload
[3] = { "", "", "" };
2338 unsigned num_overloads
= 0;
2339 LLVMValueRef args
[18];
2340 unsigned num_args
= 0;
2341 enum ac_image_dim dim
= a
->dim
;
2343 assert(!a
->lod
|| a
->lod
== ctx
->i32_0
|| a
->lod
== ctx
->f32_0
||
2345 assert((a
->opcode
!= ac_image_get_resinfo
&& a
->opcode
!= ac_image_load_mip
&&
2346 a
->opcode
!= ac_image_store_mip
) ||
2348 assert(a
->opcode
== ac_image_sample
|| a
->opcode
== ac_image_gather4
||
2349 (!a
->compare
&& !a
->offset
));
2350 assert((a
->opcode
== ac_image_sample
|| a
->opcode
== ac_image_gather4
||
2351 a
->opcode
== ac_image_get_lod
) ||
2353 assert((a
->bias
? 1 : 0) +
2355 (a
->level_zero
? 1 : 0) +
2356 (a
->derivs
[0] ? 1 : 0) <= 1);
2358 if (a
->opcode
== ac_image_get_lod
) {
2360 case ac_image_1darray
:
2363 case ac_image_2darray
:
2372 bool sample
= a
->opcode
== ac_image_sample
||
2373 a
->opcode
== ac_image_gather4
||
2374 a
->opcode
== ac_image_get_lod
;
2375 bool atomic
= a
->opcode
== ac_image_atomic
||
2376 a
->opcode
== ac_image_atomic_cmpswap
;
2377 bool load
= a
->opcode
== ac_image_sample
||
2378 a
->opcode
== ac_image_gather4
||
2379 a
->opcode
== ac_image_load
||
2380 a
->opcode
== ac_image_load_mip
;
2381 LLVMTypeRef coord_type
= sample
? ctx
->f32
: ctx
->i32
;
2383 if (atomic
|| a
->opcode
== ac_image_store
|| a
->opcode
== ac_image_store_mip
) {
2384 args
[num_args
++] = a
->data
[0];
2385 if (a
->opcode
== ac_image_atomic_cmpswap
)
2386 args
[num_args
++] = a
->data
[1];
2390 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->dmask
, false);
2393 args
[num_args
++] = ac_to_integer(ctx
, a
->offset
);
2395 args
[num_args
++] = ac_to_float(ctx
, a
->bias
);
2396 overload
[num_overloads
++] = ".f32";
2399 args
[num_args
++] = ac_to_float(ctx
, a
->compare
);
2401 unsigned count
= ac_num_derivs(dim
);
2402 for (unsigned i
= 0; i
< count
; ++i
)
2403 args
[num_args
++] = ac_to_float(ctx
, a
->derivs
[i
]);
2404 overload
[num_overloads
++] = ".f32";
2406 unsigned num_coords
=
2407 a
->opcode
!= ac_image_get_resinfo
? ac_num_coords(dim
) : 0;
2408 for (unsigned i
= 0; i
< num_coords
; ++i
)
2409 args
[num_args
++] = LLVMBuildBitCast(ctx
->builder
, a
->coords
[i
], coord_type
, "");
2411 args
[num_args
++] = LLVMBuildBitCast(ctx
->builder
, a
->lod
, coord_type
, "");
2412 overload
[num_overloads
++] = sample
? ".f32" : ".i32";
2414 args
[num_args
++] = a
->resource
;
2416 args
[num_args
++] = a
->sampler
;
2417 args
[num_args
++] = LLVMConstInt(ctx
->i1
, a
->unorm
, false);
2420 args
[num_args
++] = ctx
->i32_0
; /* texfailctrl */
2421 args
[num_args
++] = LLVMConstInt(ctx
->i32
,
2422 load
? get_load_cache_policy(ctx
, a
->cache_policy
) :
2423 a
->cache_policy
, false);
2426 const char *atomic_subop
= "";
2427 switch (a
->opcode
) {
2428 case ac_image_sample
: name
= "sample"; break;
2429 case ac_image_gather4
: name
= "gather4"; break;
2430 case ac_image_load
: name
= "load"; break;
2431 case ac_image_load_mip
: name
= "load.mip"; break;
2432 case ac_image_store
: name
= "store"; break;
2433 case ac_image_store_mip
: name
= "store.mip"; break;
2434 case ac_image_atomic
:
2436 atomic_subop
= get_atomic_name(a
->atomic
);
2438 case ac_image_atomic_cmpswap
:
2440 atomic_subop
= "cmpswap";
2442 case ac_image_get_lod
: name
= "getlod"; break;
2443 case ac_image_get_resinfo
: name
= "getresinfo"; break;
2444 default: unreachable("invalid image opcode");
2447 const char *dimname
;
2449 case ac_image_1d
: dimname
= "1d"; break;
2450 case ac_image_2d
: dimname
= "2d"; break;
2451 case ac_image_3d
: dimname
= "3d"; break;
2452 case ac_image_cube
: dimname
= "cube"; break;
2453 case ac_image_1darray
: dimname
= "1darray"; break;
2454 case ac_image_2darray
: dimname
= "2darray"; break;
2455 case ac_image_2dmsaa
: dimname
= "2dmsaa"; break;
2456 case ac_image_2darraymsaa
: dimname
= "2darraymsaa"; break;
2457 default: unreachable("invalid dim");
2461 a
->lod
&& (a
->opcode
== ac_image_sample
|| a
->opcode
== ac_image_gather4
);
2463 snprintf(intr_name
, sizeof(intr_name
),
2464 "llvm.amdgcn.image.%s%s" /* base name */
2465 "%s%s%s" /* sample/gather modifiers */
2466 ".%s.%s%s%s%s", /* dimension and type overloads */
2468 a
->compare
? ".c" : "",
2471 a
->derivs
[0] ? ".d" :
2472 a
->level_zero
? ".lz" : "",
2473 a
->offset
? ".o" : "",
2475 atomic
? "i32" : "v4f32",
2476 overload
[0], overload
[1], overload
[2]);
2481 else if (a
->opcode
== ac_image_store
|| a
->opcode
== ac_image_store_mip
)
2486 LLVMValueRef result
=
2487 ac_build_intrinsic(ctx
, intr_name
, retty
, args
, num_args
,
2489 if (!sample
&& retty
== ctx
->v4f32
) {
2490 result
= LLVMBuildBitCast(ctx
->builder
, result
,
2496 LLVMValueRef
ac_build_image_get_sample_count(struct ac_llvm_context
*ctx
,
2499 LLVMValueRef samples
;
2501 /* Read the samples from the descriptor directly.
2502 * Hardware doesn't have any instruction for this.
2504 samples
= LLVMBuildExtractElement(ctx
->builder
, rsrc
,
2505 LLVMConstInt(ctx
->i32
, 3, 0), "");
2506 samples
= LLVMBuildLShr(ctx
->builder
, samples
,
2507 LLVMConstInt(ctx
->i32
, 16, 0), "");
2508 samples
= LLVMBuildAnd(ctx
->builder
, samples
,
2509 LLVMConstInt(ctx
->i32
, 0xf, 0), "");
2510 samples
= LLVMBuildShl(ctx
->builder
, ctx
->i32_1
,
2515 LLVMValueRef
ac_build_cvt_pkrtz_f16(struct ac_llvm_context
*ctx
,
2516 LLVMValueRef args
[2])
2519 LLVMVectorType(LLVMHalfTypeInContext(ctx
->context
), 2);
2521 return ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pkrtz", v2f16
,
2522 args
, 2, AC_FUNC_ATTR_READNONE
);
2525 LLVMValueRef
ac_build_cvt_pknorm_i16(struct ac_llvm_context
*ctx
,
2526 LLVMValueRef args
[2])
2529 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pknorm.i16",
2530 ctx
->v2i16
, args
, 2,
2531 AC_FUNC_ATTR_READNONE
);
2532 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
2535 LLVMValueRef
ac_build_cvt_pknorm_u16(struct ac_llvm_context
*ctx
,
2536 LLVMValueRef args
[2])
2539 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pknorm.u16",
2540 ctx
->v2i16
, args
, 2,
2541 AC_FUNC_ATTR_READNONE
);
2542 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
2545 /* The 8-bit and 10-bit clamping is for HW workarounds. */
2546 LLVMValueRef
ac_build_cvt_pk_i16(struct ac_llvm_context
*ctx
,
2547 LLVMValueRef args
[2], unsigned bits
, bool hi
)
2549 assert(bits
== 8 || bits
== 10 || bits
== 16);
2551 LLVMValueRef max_rgb
= LLVMConstInt(ctx
->i32
,
2552 bits
== 8 ? 127 : bits
== 10 ? 511 : 32767, 0);
2553 LLVMValueRef min_rgb
= LLVMConstInt(ctx
->i32
,
2554 bits
== 8 ? -128 : bits
== 10 ? -512 : -32768, 0);
2555 LLVMValueRef max_alpha
=
2556 bits
!= 10 ? max_rgb
: ctx
->i32_1
;
2557 LLVMValueRef min_alpha
=
2558 bits
!= 10 ? min_rgb
: LLVMConstInt(ctx
->i32
, -2, 0);
2562 for (int i
= 0; i
< 2; i
++) {
2563 bool alpha
= hi
&& i
== 1;
2564 args
[i
] = ac_build_imin(ctx
, args
[i
],
2565 alpha
? max_alpha
: max_rgb
);
2566 args
[i
] = ac_build_imax(ctx
, args
[i
],
2567 alpha
? min_alpha
: min_rgb
);
2572 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pk.i16",
2573 ctx
->v2i16
, args
, 2,
2574 AC_FUNC_ATTR_READNONE
);
2575 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
2578 /* The 8-bit and 10-bit clamping is for HW workarounds. */
2579 LLVMValueRef
ac_build_cvt_pk_u16(struct ac_llvm_context
*ctx
,
2580 LLVMValueRef args
[2], unsigned bits
, bool hi
)
2582 assert(bits
== 8 || bits
== 10 || bits
== 16);
2584 LLVMValueRef max_rgb
= LLVMConstInt(ctx
->i32
,
2585 bits
== 8 ? 255 : bits
== 10 ? 1023 : 65535, 0);
2586 LLVMValueRef max_alpha
=
2587 bits
!= 10 ? max_rgb
: LLVMConstInt(ctx
->i32
, 3, 0);
2591 for (int i
= 0; i
< 2; i
++) {
2592 bool alpha
= hi
&& i
== 1;
2593 args
[i
] = ac_build_umin(ctx
, args
[i
],
2594 alpha
? max_alpha
: max_rgb
);
2599 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pk.u16",
2600 ctx
->v2i16
, args
, 2,
2601 AC_FUNC_ATTR_READNONE
);
2602 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
2605 LLVMValueRef
ac_build_wqm_vote(struct ac_llvm_context
*ctx
, LLVMValueRef i1
)
2607 return ac_build_intrinsic(ctx
, "llvm.amdgcn.wqm.vote", ctx
->i1
,
2608 &i1
, 1, AC_FUNC_ATTR_READNONE
);
2611 void ac_build_kill_if_false(struct ac_llvm_context
*ctx
, LLVMValueRef i1
)
2613 ac_build_intrinsic(ctx
, "llvm.amdgcn.kill", ctx
->voidt
,
2617 LLVMValueRef
ac_build_bfe(struct ac_llvm_context
*ctx
, LLVMValueRef input
,
2618 LLVMValueRef offset
, LLVMValueRef width
,
2621 LLVMValueRef args
[] = {
2627 return ac_build_intrinsic(ctx
, is_signed
? "llvm.amdgcn.sbfe.i32" :
2628 "llvm.amdgcn.ubfe.i32",
2629 ctx
->i32
, args
, 3, AC_FUNC_ATTR_READNONE
);
2633 LLVMValueRef
ac_build_imad(struct ac_llvm_context
*ctx
, LLVMValueRef s0
,
2634 LLVMValueRef s1
, LLVMValueRef s2
)
2636 return LLVMBuildAdd(ctx
->builder
,
2637 LLVMBuildMul(ctx
->builder
, s0
, s1
, ""), s2
, "");
2640 LLVMValueRef
ac_build_fmad(struct ac_llvm_context
*ctx
, LLVMValueRef s0
,
2641 LLVMValueRef s1
, LLVMValueRef s2
)
2643 /* FMA is better on GFX10, because it has FMA units instead of MUL-ADD units. */
2644 if (ctx
->chip_class
>= GFX10
) {
2645 return ac_build_intrinsic(ctx
, "llvm.fma.f32", ctx
->f32
,
2646 (LLVMValueRef
[]) {s0
, s1
, s2
}, 3,
2647 AC_FUNC_ATTR_READNONE
);
2650 return LLVMBuildFAdd(ctx
->builder
,
2651 LLVMBuildFMul(ctx
->builder
, s0
, s1
, ""), s2
, "");
2654 void ac_build_waitcnt(struct ac_llvm_context
*ctx
, unsigned wait_flags
)
2659 unsigned lgkmcnt
= 63;
2660 unsigned vmcnt
= ctx
->chip_class
>= GFX9
? 63 : 15;
2661 unsigned vscnt
= 63;
2663 if (wait_flags
& AC_WAIT_LGKM
)
2665 if (wait_flags
& AC_WAIT_VLOAD
)
2668 if (wait_flags
& AC_WAIT_VSTORE
) {
2669 if (ctx
->chip_class
>= GFX10
)
2675 /* There is no intrinsic for vscnt(0), so use a fence. */
2676 if ((wait_flags
& AC_WAIT_LGKM
&&
2677 wait_flags
& AC_WAIT_VLOAD
&&
2678 wait_flags
& AC_WAIT_VSTORE
) ||
2680 LLVMBuildFence(ctx
->builder
, LLVMAtomicOrderingRelease
, false, "");
2684 unsigned simm16
= (lgkmcnt
<< 8) |
2685 (7 << 4) | /* expcnt */
2687 ((vmcnt
>> 4) << 14);
2689 LLVMValueRef args
[1] = {
2690 LLVMConstInt(ctx
->i32
, simm16
, false),
2692 ac_build_intrinsic(ctx
, "llvm.amdgcn.s.waitcnt",
2693 ctx
->voidt
, args
, 1, 0);
2696 LLVMValueRef
ac_build_fmed3(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
2697 LLVMValueRef src1
, LLVMValueRef src2
,
2703 if (bitsize
== 16) {
2704 intr
= "llvm.amdgcn.fmed3.f16";
2706 } else if (bitsize
== 32) {
2707 intr
= "llvm.amdgcn.fmed3.f32";
2710 intr
= "llvm.amdgcn.fmed3.f64";
2714 LLVMValueRef params
[] = {
2719 return ac_build_intrinsic(ctx
, intr
, type
, params
, 3,
2720 AC_FUNC_ATTR_READNONE
);
2723 LLVMValueRef
ac_build_fract(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
2729 if (bitsize
== 16) {
2730 intr
= "llvm.amdgcn.fract.f16";
2732 } else if (bitsize
== 32) {
2733 intr
= "llvm.amdgcn.fract.f32";
2736 intr
= "llvm.amdgcn.fract.f64";
2740 LLVMValueRef params
[] = {
2743 return ac_build_intrinsic(ctx
, intr
, type
, params
, 1,
2744 AC_FUNC_ATTR_READNONE
);
2747 LLVMValueRef
ac_build_isign(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
2750 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->context
, bitsize
);
2751 LLVMValueRef zero
= LLVMConstInt(type
, 0, false);
2752 LLVMValueRef one
= LLVMConstInt(type
, 1, false);
2754 LLVMValueRef cmp
, val
;
2755 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGT
, src0
, zero
, "");
2756 val
= LLVMBuildSelect(ctx
->builder
, cmp
, one
, src0
, "");
2757 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGE
, val
, zero
, "");
2758 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstInt(type
, -1, true), "");
2762 LLVMValueRef
ac_build_fsign(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
2765 LLVMValueRef cmp
, val
, zero
, one
;
2768 if (bitsize
== 16) {
2772 } else if (bitsize
== 32) {
2782 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGT
, src0
, zero
, "");
2783 val
= LLVMBuildSelect(ctx
->builder
, cmp
, one
, src0
, "");
2784 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGE
, val
, zero
, "");
2785 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstReal(type
, -1.0), "");
2789 LLVMValueRef
ac_build_bit_count(struct ac_llvm_context
*ctx
, LLVMValueRef src0
)
2791 LLVMValueRef result
;
2794 bitsize
= ac_get_elem_bits(ctx
, LLVMTypeOf(src0
));
2798 result
= ac_build_intrinsic(ctx
, "llvm.ctpop.i64", ctx
->i64
,
2799 (LLVMValueRef
[]) { src0
}, 1,
2800 AC_FUNC_ATTR_READNONE
);
2802 result
= LLVMBuildTrunc(ctx
->builder
, result
, ctx
->i32
, "");
2805 result
= ac_build_intrinsic(ctx
, "llvm.ctpop.i32", ctx
->i32
,
2806 (LLVMValueRef
[]) { src0
}, 1,
2807 AC_FUNC_ATTR_READNONE
);
2810 result
= ac_build_intrinsic(ctx
, "llvm.ctpop.i16", ctx
->i16
,
2811 (LLVMValueRef
[]) { src0
}, 1,
2812 AC_FUNC_ATTR_READNONE
);
2814 result
= LLVMBuildZExt(ctx
->builder
, result
, ctx
->i32
, "");
2817 result
= ac_build_intrinsic(ctx
, "llvm.ctpop.i8", ctx
->i8
,
2818 (LLVMValueRef
[]) { src0
}, 1,
2819 AC_FUNC_ATTR_READNONE
);
2821 result
= LLVMBuildZExt(ctx
->builder
, result
, ctx
->i32
, "");
2824 unreachable(!"invalid bitsize");
2831 LLVMValueRef
ac_build_bitfield_reverse(struct ac_llvm_context
*ctx
,
2834 LLVMValueRef result
;
2837 bitsize
= ac_get_elem_bits(ctx
, LLVMTypeOf(src0
));
2841 result
= ac_build_intrinsic(ctx
, "llvm.bitreverse.i64", ctx
->i64
,
2842 (LLVMValueRef
[]) { src0
}, 1,
2843 AC_FUNC_ATTR_READNONE
);
2845 result
= LLVMBuildTrunc(ctx
->builder
, result
, ctx
->i32
, "");
2848 result
= ac_build_intrinsic(ctx
, "llvm.bitreverse.i32", ctx
->i32
,
2849 (LLVMValueRef
[]) { src0
}, 1,
2850 AC_FUNC_ATTR_READNONE
);
2853 result
= ac_build_intrinsic(ctx
, "llvm.bitreverse.i16", ctx
->i16
,
2854 (LLVMValueRef
[]) { src0
}, 1,
2855 AC_FUNC_ATTR_READNONE
);
2857 result
= LLVMBuildZExt(ctx
->builder
, result
, ctx
->i32
, "");
2860 result
= ac_build_intrinsic(ctx
, "llvm.bitreverse.i8", ctx
->i8
,
2861 (LLVMValueRef
[]) { src0
}, 1,
2862 AC_FUNC_ATTR_READNONE
);
2864 result
= LLVMBuildZExt(ctx
->builder
, result
, ctx
->i32
, "");
2867 unreachable(!"invalid bitsize");
2874 #define AC_EXP_TARGET 0
2875 #define AC_EXP_ENABLED_CHANNELS 1
2876 #define AC_EXP_OUT0 2
2884 struct ac_vs_exp_chan
2888 enum ac_ir_type type
;
2891 struct ac_vs_exp_inst
{
2894 struct ac_vs_exp_chan chan
[4];
2897 struct ac_vs_exports
{
2899 struct ac_vs_exp_inst exp
[VARYING_SLOT_MAX
];
2902 /* Return true if the PARAM export has been eliminated. */
2903 static bool ac_eliminate_const_output(uint8_t *vs_output_param_offset
,
2904 uint32_t num_outputs
,
2905 struct ac_vs_exp_inst
*exp
)
2907 unsigned i
, default_val
; /* SPI_PS_INPUT_CNTL_i.DEFAULT_VAL */
2908 bool is_zero
[4] = {}, is_one
[4] = {};
2910 for (i
= 0; i
< 4; i
++) {
2911 /* It's a constant expression. Undef outputs are eliminated too. */
2912 if (exp
->chan
[i
].type
== AC_IR_UNDEF
) {
2915 } else if (exp
->chan
[i
].type
== AC_IR_CONST
) {
2916 if (exp
->chan
[i
].const_float
== 0)
2918 else if (exp
->chan
[i
].const_float
== 1)
2921 return false; /* other constant */
2926 /* Only certain combinations of 0 and 1 can be eliminated. */
2927 if (is_zero
[0] && is_zero
[1] && is_zero
[2])
2928 default_val
= is_zero
[3] ? 0 : 1;
2929 else if (is_one
[0] && is_one
[1] && is_one
[2])
2930 default_val
= is_zero
[3] ? 2 : 3;
2934 /* The PARAM export can be represented as DEFAULT_VAL. Kill it. */
2935 LLVMInstructionEraseFromParent(exp
->inst
);
2937 /* Change OFFSET to DEFAULT_VAL. */
2938 for (i
= 0; i
< num_outputs
; i
++) {
2939 if (vs_output_param_offset
[i
] == exp
->offset
) {
2940 vs_output_param_offset
[i
] =
2941 AC_EXP_PARAM_DEFAULT_VAL_0000
+ default_val
;
2948 static bool ac_eliminate_duplicated_output(struct ac_llvm_context
*ctx
,
2949 uint8_t *vs_output_param_offset
,
2950 uint32_t num_outputs
,
2951 struct ac_vs_exports
*processed
,
2952 struct ac_vs_exp_inst
*exp
)
2954 unsigned p
, copy_back_channels
= 0;
2956 /* See if the output is already in the list of processed outputs.
2957 * The LLVMValueRef comparison relies on SSA.
2959 for (p
= 0; p
< processed
->num
; p
++) {
2960 bool different
= false;
2962 for (unsigned j
= 0; j
< 4; j
++) {
2963 struct ac_vs_exp_chan
*c1
= &processed
->exp
[p
].chan
[j
];
2964 struct ac_vs_exp_chan
*c2
= &exp
->chan
[j
];
2966 /* Treat undef as a match. */
2967 if (c2
->type
== AC_IR_UNDEF
)
2970 /* If c1 is undef but c2 isn't, we can copy c2 to c1
2971 * and consider the instruction duplicated.
2973 if (c1
->type
== AC_IR_UNDEF
) {
2974 copy_back_channels
|= 1 << j
;
2978 /* Test whether the channels are not equal. */
2979 if (c1
->type
!= c2
->type
||
2980 (c1
->type
== AC_IR_CONST
&&
2981 c1
->const_float
!= c2
->const_float
) ||
2982 (c1
->type
== AC_IR_VALUE
&&
2983 c1
->value
!= c2
->value
)) {
2991 copy_back_channels
= 0;
2993 if (p
== processed
->num
)
2996 /* If a match was found, but the matching export has undef where the new
2997 * one has a normal value, copy the normal value to the undef channel.
2999 struct ac_vs_exp_inst
*match
= &processed
->exp
[p
];
3001 /* Get current enabled channels mask. */
3002 LLVMValueRef arg
= LLVMGetOperand(match
->inst
, AC_EXP_ENABLED_CHANNELS
);
3003 unsigned enabled_channels
= LLVMConstIntGetZExtValue(arg
);
3005 while (copy_back_channels
) {
3006 unsigned chan
= u_bit_scan(©_back_channels
);
3008 assert(match
->chan
[chan
].type
== AC_IR_UNDEF
);
3009 LLVMSetOperand(match
->inst
, AC_EXP_OUT0
+ chan
,
3010 exp
->chan
[chan
].value
);
3011 match
->chan
[chan
] = exp
->chan
[chan
];
3013 /* Update number of enabled channels because the original mask
3014 * is not always 0xf.
3016 enabled_channels
|= (1 << chan
);
3017 LLVMSetOperand(match
->inst
, AC_EXP_ENABLED_CHANNELS
,
3018 LLVMConstInt(ctx
->i32
, enabled_channels
, 0));
3021 /* The PARAM export is duplicated. Kill it. */
3022 LLVMInstructionEraseFromParent(exp
->inst
);
3024 /* Change OFFSET to the matching export. */
3025 for (unsigned i
= 0; i
< num_outputs
; i
++) {
3026 if (vs_output_param_offset
[i
] == exp
->offset
) {
3027 vs_output_param_offset
[i
] = match
->offset
;
3034 void ac_optimize_vs_outputs(struct ac_llvm_context
*ctx
,
3035 LLVMValueRef main_fn
,
3036 uint8_t *vs_output_param_offset
,
3037 uint32_t num_outputs
,
3038 uint8_t *num_param_exports
)
3040 LLVMBasicBlockRef bb
;
3041 bool removed_any
= false;
3042 struct ac_vs_exports exports
;
3046 /* Process all LLVM instructions. */
3047 bb
= LLVMGetFirstBasicBlock(main_fn
);
3049 LLVMValueRef inst
= LLVMGetFirstInstruction(bb
);
3052 LLVMValueRef cur
= inst
;
3053 inst
= LLVMGetNextInstruction(inst
);
3054 struct ac_vs_exp_inst exp
;
3056 if (LLVMGetInstructionOpcode(cur
) != LLVMCall
)
3059 LLVMValueRef callee
= ac_llvm_get_called_value(cur
);
3061 if (!ac_llvm_is_function(callee
))
3064 const char *name
= LLVMGetValueName(callee
);
3065 unsigned num_args
= LLVMCountParams(callee
);
3067 /* Check if this is an export instruction. */
3068 if ((num_args
!= 9 && num_args
!= 8) ||
3069 (strcmp(name
, "llvm.SI.export") &&
3070 strcmp(name
, "llvm.amdgcn.exp.f32")))
3073 LLVMValueRef arg
= LLVMGetOperand(cur
, AC_EXP_TARGET
);
3074 unsigned target
= LLVMConstIntGetZExtValue(arg
);
3076 if (target
< V_008DFC_SQ_EXP_PARAM
)
3079 target
-= V_008DFC_SQ_EXP_PARAM
;
3081 /* Parse the instruction. */
3082 memset(&exp
, 0, sizeof(exp
));
3083 exp
.offset
= target
;
3086 for (unsigned i
= 0; i
< 4; i
++) {
3087 LLVMValueRef v
= LLVMGetOperand(cur
, AC_EXP_OUT0
+ i
);
3089 exp
.chan
[i
].value
= v
;
3091 if (LLVMIsUndef(v
)) {
3092 exp
.chan
[i
].type
= AC_IR_UNDEF
;
3093 } else if (LLVMIsAConstantFP(v
)) {
3094 LLVMBool loses_info
;
3095 exp
.chan
[i
].type
= AC_IR_CONST
;
3096 exp
.chan
[i
].const_float
=
3097 LLVMConstRealGetDouble(v
, &loses_info
);
3099 exp
.chan
[i
].type
= AC_IR_VALUE
;
3103 /* Eliminate constant and duplicated PARAM exports. */
3104 if (ac_eliminate_const_output(vs_output_param_offset
,
3105 num_outputs
, &exp
) ||
3106 ac_eliminate_duplicated_output(ctx
,
3107 vs_output_param_offset
,
3108 num_outputs
, &exports
,
3112 exports
.exp
[exports
.num
++] = exp
;
3115 bb
= LLVMGetNextBasicBlock(bb
);
3118 /* Remove holes in export memory due to removed PARAM exports.
3119 * This is done by renumbering all PARAM exports.
3122 uint8_t old_offset
[VARYING_SLOT_MAX
];
3125 /* Make a copy of the offsets. We need the old version while
3126 * we are modifying some of them. */
3127 memcpy(old_offset
, vs_output_param_offset
,
3128 sizeof(old_offset
));
3130 for (i
= 0; i
< exports
.num
; i
++) {
3131 unsigned offset
= exports
.exp
[i
].offset
;
3133 /* Update vs_output_param_offset. Multiple outputs can
3134 * have the same offset.
3136 for (out
= 0; out
< num_outputs
; out
++) {
3137 if (old_offset
[out
] == offset
)
3138 vs_output_param_offset
[out
] = i
;
3141 /* Change the PARAM offset in the instruction. */
3142 LLVMSetOperand(exports
.exp
[i
].inst
, AC_EXP_TARGET
,
3143 LLVMConstInt(ctx
->i32
,
3144 V_008DFC_SQ_EXP_PARAM
+ i
, 0));
3146 *num_param_exports
= exports
.num
;
3150 void ac_init_exec_full_mask(struct ac_llvm_context
*ctx
)
3152 LLVMValueRef full_mask
= LLVMConstInt(ctx
->i64
, ~0ull, 0);
3153 ac_build_intrinsic(ctx
,
3154 "llvm.amdgcn.init.exec", ctx
->voidt
,
3155 &full_mask
, 1, AC_FUNC_ATTR_CONVERGENT
);
3158 void ac_declare_lds_as_pointer(struct ac_llvm_context
*ctx
)
3160 unsigned lds_size
= ctx
->chip_class
>= GFX7
? 65536 : 32768;
3161 ctx
->lds
= LLVMBuildIntToPtr(ctx
->builder
, ctx
->i32_0
,
3162 LLVMPointerType(LLVMArrayType(ctx
->i32
, lds_size
/ 4), AC_ADDR_SPACE_LDS
),
3166 LLVMValueRef
ac_lds_load(struct ac_llvm_context
*ctx
,
3167 LLVMValueRef dw_addr
)
3169 return LLVMBuildLoad(ctx
->builder
, ac_build_gep0(ctx
, ctx
->lds
, dw_addr
), "");
3172 void ac_lds_store(struct ac_llvm_context
*ctx
,
3173 LLVMValueRef dw_addr
,
3176 value
= ac_to_integer(ctx
, value
);
3177 ac_build_indexed_store(ctx
, ctx
->lds
,
3181 LLVMValueRef
ac_find_lsb(struct ac_llvm_context
*ctx
,
3182 LLVMTypeRef dst_type
,
3185 unsigned src0_bitsize
= ac_get_elem_bits(ctx
, LLVMTypeOf(src0
));
3186 const char *intrin_name
;
3190 switch (src0_bitsize
) {
3192 intrin_name
= "llvm.cttz.i64";
3197 intrin_name
= "llvm.cttz.i32";
3202 intrin_name
= "llvm.cttz.i16";
3207 intrin_name
= "llvm.cttz.i8";
3212 unreachable(!"invalid bitsize");
3215 LLVMValueRef params
[2] = {
3218 /* The value of 1 means that ffs(x=0) = undef, so LLVM won't
3219 * add special code to check for x=0. The reason is that
3220 * the LLVM behavior for x=0 is different from what we
3221 * need here. However, LLVM also assumes that ffs(x) is
3222 * in [0, 31], but GLSL expects that ffs(0) = -1, so
3223 * a conditional assignment to handle 0 is still required.
3225 * The hardware already implements the correct behavior.
3230 LLVMValueRef lsb
= ac_build_intrinsic(ctx
, intrin_name
, type
,
3232 AC_FUNC_ATTR_READNONE
);
3234 if (src0_bitsize
== 64) {
3235 lsb
= LLVMBuildTrunc(ctx
->builder
, lsb
, ctx
->i32
, "");
3236 } else if (src0_bitsize
< 32) {
3237 lsb
= LLVMBuildSExt(ctx
->builder
, lsb
, ctx
->i32
, "");
3240 /* TODO: We need an intrinsic to skip this conditional. */
3241 /* Check for zero: */
3242 return LLVMBuildSelect(ctx
->builder
, LLVMBuildICmp(ctx
->builder
,
3245 LLVMConstInt(ctx
->i32
, -1, 0), lsb
, "");
3248 LLVMTypeRef
ac_array_in_const_addr_space(LLVMTypeRef elem_type
)
3250 return LLVMPointerType(elem_type
, AC_ADDR_SPACE_CONST
);
3253 LLVMTypeRef
ac_array_in_const32_addr_space(LLVMTypeRef elem_type
)
3255 return LLVMPointerType(elem_type
, AC_ADDR_SPACE_CONST_32BIT
);
3258 static struct ac_llvm_flow
*
3259 get_current_flow(struct ac_llvm_context
*ctx
)
3261 if (ctx
->flow
->depth
> 0)
3262 return &ctx
->flow
->stack
[ctx
->flow
->depth
- 1];
3266 static struct ac_llvm_flow
*
3267 get_innermost_loop(struct ac_llvm_context
*ctx
)
3269 for (unsigned i
= ctx
->flow
->depth
; i
> 0; --i
) {
3270 if (ctx
->flow
->stack
[i
- 1].loop_entry_block
)
3271 return &ctx
->flow
->stack
[i
- 1];
3276 static struct ac_llvm_flow
*
3277 push_flow(struct ac_llvm_context
*ctx
)
3279 struct ac_llvm_flow
*flow
;
3281 if (ctx
->flow
->depth
>= ctx
->flow
->depth_max
) {
3282 unsigned new_max
= MAX2(ctx
->flow
->depth
<< 1,
3283 AC_LLVM_INITIAL_CF_DEPTH
);
3285 ctx
->flow
->stack
= realloc(ctx
->flow
->stack
, new_max
* sizeof(*ctx
->flow
->stack
));
3286 ctx
->flow
->depth_max
= new_max
;
3289 flow
= &ctx
->flow
->stack
[ctx
->flow
->depth
];
3292 flow
->next_block
= NULL
;
3293 flow
->loop_entry_block
= NULL
;
3297 static void set_basicblock_name(LLVMBasicBlockRef bb
, const char *base
,
3301 snprintf(buf
, sizeof(buf
), "%s%d", base
, label_id
);
3302 LLVMSetValueName(LLVMBasicBlockAsValue(bb
), buf
);
3305 /* Append a basic block at the level of the parent flow.
3307 static LLVMBasicBlockRef
append_basic_block(struct ac_llvm_context
*ctx
,
3310 assert(ctx
->flow
->depth
>= 1);
3312 if (ctx
->flow
->depth
>= 2) {
3313 struct ac_llvm_flow
*flow
= &ctx
->flow
->stack
[ctx
->flow
->depth
- 2];
3315 return LLVMInsertBasicBlockInContext(ctx
->context
,
3316 flow
->next_block
, name
);
3319 LLVMValueRef main_fn
=
3320 LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
->builder
));
3321 return LLVMAppendBasicBlockInContext(ctx
->context
, main_fn
, name
);
3324 /* Emit a branch to the given default target for the current block if
3325 * applicable -- that is, if the current block does not already contain a
3326 * branch from a break or continue.
3328 static void emit_default_branch(LLVMBuilderRef builder
,
3329 LLVMBasicBlockRef target
)
3331 if (!LLVMGetBasicBlockTerminator(LLVMGetInsertBlock(builder
)))
3332 LLVMBuildBr(builder
, target
);
3335 void ac_build_bgnloop(struct ac_llvm_context
*ctx
, int label_id
)
3337 struct ac_llvm_flow
*flow
= push_flow(ctx
);
3338 flow
->loop_entry_block
= append_basic_block(ctx
, "LOOP");
3339 flow
->next_block
= append_basic_block(ctx
, "ENDLOOP");
3340 set_basicblock_name(flow
->loop_entry_block
, "loop", label_id
);
3341 LLVMBuildBr(ctx
->builder
, flow
->loop_entry_block
);
3342 LLVMPositionBuilderAtEnd(ctx
->builder
, flow
->loop_entry_block
);
3345 void ac_build_break(struct ac_llvm_context
*ctx
)
3347 struct ac_llvm_flow
*flow
= get_innermost_loop(ctx
);
3348 LLVMBuildBr(ctx
->builder
, flow
->next_block
);
3351 void ac_build_continue(struct ac_llvm_context
*ctx
)
3353 struct ac_llvm_flow
*flow
= get_innermost_loop(ctx
);
3354 LLVMBuildBr(ctx
->builder
, flow
->loop_entry_block
);
3357 void ac_build_else(struct ac_llvm_context
*ctx
, int label_id
)
3359 struct ac_llvm_flow
*current_branch
= get_current_flow(ctx
);
3360 LLVMBasicBlockRef endif_block
;
3362 assert(!current_branch
->loop_entry_block
);
3364 endif_block
= append_basic_block(ctx
, "ENDIF");
3365 emit_default_branch(ctx
->builder
, endif_block
);
3367 LLVMPositionBuilderAtEnd(ctx
->builder
, current_branch
->next_block
);
3368 set_basicblock_name(current_branch
->next_block
, "else", label_id
);
3370 current_branch
->next_block
= endif_block
;
3373 void ac_build_endif(struct ac_llvm_context
*ctx
, int label_id
)
3375 struct ac_llvm_flow
*current_branch
= get_current_flow(ctx
);
3377 assert(!current_branch
->loop_entry_block
);
3379 emit_default_branch(ctx
->builder
, current_branch
->next_block
);
3380 LLVMPositionBuilderAtEnd(ctx
->builder
, current_branch
->next_block
);
3381 set_basicblock_name(current_branch
->next_block
, "endif", label_id
);
3386 void ac_build_endloop(struct ac_llvm_context
*ctx
, int label_id
)
3388 struct ac_llvm_flow
*current_loop
= get_current_flow(ctx
);
3390 assert(current_loop
->loop_entry_block
);
3392 emit_default_branch(ctx
->builder
, current_loop
->loop_entry_block
);
3394 LLVMPositionBuilderAtEnd(ctx
->builder
, current_loop
->next_block
);
3395 set_basicblock_name(current_loop
->next_block
, "endloop", label_id
);
3399 void ac_build_ifcc(struct ac_llvm_context
*ctx
, LLVMValueRef cond
, int label_id
)
3401 struct ac_llvm_flow
*flow
= push_flow(ctx
);
3402 LLVMBasicBlockRef if_block
;
3404 if_block
= append_basic_block(ctx
, "IF");
3405 flow
->next_block
= append_basic_block(ctx
, "ELSE");
3406 set_basicblock_name(if_block
, "if", label_id
);
3407 LLVMBuildCondBr(ctx
->builder
, cond
, if_block
, flow
->next_block
);
3408 LLVMPositionBuilderAtEnd(ctx
->builder
, if_block
);
3411 void ac_build_if(struct ac_llvm_context
*ctx
, LLVMValueRef value
,
3414 LLVMValueRef cond
= LLVMBuildFCmp(ctx
->builder
, LLVMRealUNE
,
3415 value
, ctx
->f32_0
, "");
3416 ac_build_ifcc(ctx
, cond
, label_id
);
3419 void ac_build_uif(struct ac_llvm_context
*ctx
, LLVMValueRef value
,
3422 LLVMValueRef cond
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
3423 ac_to_integer(ctx
, value
),
3425 ac_build_ifcc(ctx
, cond
, label_id
);
3428 LLVMValueRef
ac_build_alloca_undef(struct ac_llvm_context
*ac
, LLVMTypeRef type
,
3431 LLVMBuilderRef builder
= ac
->builder
;
3432 LLVMBasicBlockRef current_block
= LLVMGetInsertBlock(builder
);
3433 LLVMValueRef function
= LLVMGetBasicBlockParent(current_block
);
3434 LLVMBasicBlockRef first_block
= LLVMGetEntryBasicBlock(function
);
3435 LLVMValueRef first_instr
= LLVMGetFirstInstruction(first_block
);
3436 LLVMBuilderRef first_builder
= LLVMCreateBuilderInContext(ac
->context
);
3440 LLVMPositionBuilderBefore(first_builder
, first_instr
);
3442 LLVMPositionBuilderAtEnd(first_builder
, first_block
);
3445 res
= LLVMBuildAlloca(first_builder
, type
, name
);
3446 LLVMDisposeBuilder(first_builder
);
3450 LLVMValueRef
ac_build_alloca(struct ac_llvm_context
*ac
,
3451 LLVMTypeRef type
, const char *name
)
3453 LLVMValueRef ptr
= ac_build_alloca_undef(ac
, type
, name
);
3454 LLVMBuildStore(ac
->builder
, LLVMConstNull(type
), ptr
);
3458 LLVMValueRef
ac_cast_ptr(struct ac_llvm_context
*ctx
, LLVMValueRef ptr
,
3461 int addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
3462 return LLVMBuildBitCast(ctx
->builder
, ptr
,
3463 LLVMPointerType(type
, addr_space
), "");
3466 LLVMValueRef
ac_trim_vector(struct ac_llvm_context
*ctx
, LLVMValueRef value
,
3469 unsigned num_components
= ac_get_llvm_num_components(value
);
3470 if (count
== num_components
)
3473 LLVMValueRef masks
[MAX2(count
, 2)];
3474 masks
[0] = ctx
->i32_0
;
3475 masks
[1] = ctx
->i32_1
;
3476 for (unsigned i
= 2; i
< count
; i
++)
3477 masks
[i
] = LLVMConstInt(ctx
->i32
, i
, false);
3480 return LLVMBuildExtractElement(ctx
->builder
, value
, masks
[0],
3483 LLVMValueRef swizzle
= LLVMConstVector(masks
, count
);
3484 return LLVMBuildShuffleVector(ctx
->builder
, value
, value
, swizzle
, "");
3487 LLVMValueRef
ac_unpack_param(struct ac_llvm_context
*ctx
, LLVMValueRef param
,
3488 unsigned rshift
, unsigned bitwidth
)
3490 LLVMValueRef value
= param
;
3492 value
= LLVMBuildLShr(ctx
->builder
, value
,
3493 LLVMConstInt(ctx
->i32
, rshift
, false), "");
3495 if (rshift
+ bitwidth
< 32) {
3496 unsigned mask
= (1 << bitwidth
) - 1;
3497 value
= LLVMBuildAnd(ctx
->builder
, value
,
3498 LLVMConstInt(ctx
->i32
, mask
, false), "");
3503 /* Adjust the sample index according to FMASK.
3505 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
3506 * which is the identity mapping. Each nibble says which physical sample
3507 * should be fetched to get that sample.
3509 * For example, 0x11111100 means there are only 2 samples stored and
3510 * the second sample covers 3/4 of the pixel. When reading samples 0
3511 * and 1, return physical sample 0 (determined by the first two 0s
3512 * in FMASK), otherwise return physical sample 1.
3514 * The sample index should be adjusted as follows:
3515 * addr[sample_index] = (fmask >> (addr[sample_index] * 4)) & 0xF;
3517 void ac_apply_fmask_to_sample(struct ac_llvm_context
*ac
, LLVMValueRef fmask
,
3518 LLVMValueRef
*addr
, bool is_array_tex
)
3520 struct ac_image_args fmask_load
= {};
3521 fmask_load
.opcode
= ac_image_load
;
3522 fmask_load
.resource
= fmask
;
3523 fmask_load
.dmask
= 0xf;
3524 fmask_load
.dim
= is_array_tex
? ac_image_2darray
: ac_image_2d
;
3525 fmask_load
.attributes
= AC_FUNC_ATTR_READNONE
;
3527 fmask_load
.coords
[0] = addr
[0];
3528 fmask_load
.coords
[1] = addr
[1];
3530 fmask_load
.coords
[2] = addr
[2];
3532 LLVMValueRef fmask_value
= ac_build_image_opcode(ac
, &fmask_load
);
3533 fmask_value
= LLVMBuildExtractElement(ac
->builder
, fmask_value
,
3536 /* Apply the formula. */
3537 unsigned sample_chan
= is_array_tex
? 3 : 2;
3538 LLVMValueRef final_sample
;
3539 final_sample
= LLVMBuildMul(ac
->builder
, addr
[sample_chan
],
3540 LLVMConstInt(ac
->i32
, 4, 0), "");
3541 final_sample
= LLVMBuildLShr(ac
->builder
, fmask_value
, final_sample
, "");
3542 /* Mask the sample index by 0x7, because 0x8 means an unknown value
3543 * with EQAA, so those will map to 0. */
3544 final_sample
= LLVMBuildAnd(ac
->builder
, final_sample
,
3545 LLVMConstInt(ac
->i32
, 0x7, 0), "");
3547 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
3548 * resource descriptor is 0 (invalid).
3551 tmp
= LLVMBuildBitCast(ac
->builder
, fmask
, ac
->v8i32
, "");
3552 tmp
= LLVMBuildExtractElement(ac
->builder
, tmp
, ac
->i32_1
, "");
3553 tmp
= LLVMBuildICmp(ac
->builder
, LLVMIntNE
, tmp
, ac
->i32_0
, "");
3555 /* Replace the MSAA sample index. */
3556 addr
[sample_chan
] = LLVMBuildSelect(ac
->builder
, tmp
, final_sample
,
3557 addr
[sample_chan
], "");
3561 _ac_build_readlane(struct ac_llvm_context
*ctx
, LLVMValueRef src
, LLVMValueRef lane
)
3563 ac_build_optimization_barrier(ctx
, &src
);
3564 return ac_build_intrinsic(ctx
,
3565 lane
== NULL
? "llvm.amdgcn.readfirstlane" : "llvm.amdgcn.readlane",
3566 LLVMTypeOf(src
), (LLVMValueRef
[]) {
3568 lane
== NULL
? 1 : 2,
3569 AC_FUNC_ATTR_READNONE
|
3570 AC_FUNC_ATTR_CONVERGENT
);
3574 * Builds the "llvm.amdgcn.readlane" or "llvm.amdgcn.readfirstlane" intrinsic.
3577 * @param lane - id of the lane or NULL for the first active lane
3578 * @return value of the lane
3581 ac_build_readlane(struct ac_llvm_context
*ctx
, LLVMValueRef src
, LLVMValueRef lane
)
3583 LLVMTypeRef src_type
= LLVMTypeOf(src
);
3584 src
= ac_to_integer(ctx
, src
);
3585 unsigned bits
= LLVMGetIntTypeWidth(LLVMTypeOf(src
));
3589 ret
= _ac_build_readlane(ctx
, src
, lane
);
3591 assert(bits
% 32 == 0);
3592 LLVMTypeRef vec_type
= LLVMVectorType(ctx
->i32
, bits
/ 32);
3593 LLVMValueRef src_vector
=
3594 LLVMBuildBitCast(ctx
->builder
, src
, vec_type
, "");
3595 ret
= LLVMGetUndef(vec_type
);
3596 for (unsigned i
= 0; i
< bits
/ 32; i
++) {
3597 src
= LLVMBuildExtractElement(ctx
->builder
, src_vector
,
3598 LLVMConstInt(ctx
->i32
, i
, 0), "");
3599 LLVMValueRef ret_comp
= _ac_build_readlane(ctx
, src
, lane
);
3600 ret
= LLVMBuildInsertElement(ctx
->builder
, ret
, ret_comp
,
3601 LLVMConstInt(ctx
->i32
, i
, 0), "");
3604 if (LLVMGetTypeKind(src_type
) == LLVMPointerTypeKind
)
3605 return LLVMBuildIntToPtr(ctx
->builder
, ret
, src_type
, "");
3606 return LLVMBuildBitCast(ctx
->builder
, ret
, src_type
, "");
3610 ac_build_writelane(struct ac_llvm_context
*ctx
, LLVMValueRef src
, LLVMValueRef value
, LLVMValueRef lane
)
3612 return ac_build_intrinsic(ctx
, "llvm.amdgcn.writelane", ctx
->i32
,
3613 (LLVMValueRef
[]) {value
, lane
, src
}, 3,
3614 AC_FUNC_ATTR_READNONE
| AC_FUNC_ATTR_CONVERGENT
);
3618 ac_build_mbcnt(struct ac_llvm_context
*ctx
, LLVMValueRef mask
)
3620 if (ctx
->wave_size
== 32) {
3621 return ac_build_intrinsic(ctx
, "llvm.amdgcn.mbcnt.lo", ctx
->i32
,
3622 (LLVMValueRef
[]) { mask
, ctx
->i32_0
},
3623 2, AC_FUNC_ATTR_READNONE
);
3625 LLVMValueRef mask_vec
= LLVMBuildBitCast(ctx
->builder
, mask
,
3626 LLVMVectorType(ctx
->i32
, 2),
3628 LLVMValueRef mask_lo
= LLVMBuildExtractElement(ctx
->builder
, mask_vec
,
3630 LLVMValueRef mask_hi
= LLVMBuildExtractElement(ctx
->builder
, mask_vec
,
3633 ac_build_intrinsic(ctx
, "llvm.amdgcn.mbcnt.lo", ctx
->i32
,
3634 (LLVMValueRef
[]) { mask_lo
, ctx
->i32_0
},
3635 2, AC_FUNC_ATTR_READNONE
);
3636 val
= ac_build_intrinsic(ctx
, "llvm.amdgcn.mbcnt.hi", ctx
->i32
,
3637 (LLVMValueRef
[]) { mask_hi
, val
},
3638 2, AC_FUNC_ATTR_READNONE
);
3643 _dpp_quad_perm
= 0x000,
3644 _dpp_row_sl
= 0x100,
3645 _dpp_row_sr
= 0x110,
3646 _dpp_row_rr
= 0x120,
3651 dpp_row_mirror
= 0x140,
3652 dpp_row_half_mirror
= 0x141,
3653 dpp_row_bcast15
= 0x142,
3654 dpp_row_bcast31
= 0x143
3657 static inline enum dpp_ctrl
3658 dpp_quad_perm(unsigned lane0
, unsigned lane1
, unsigned lane2
, unsigned lane3
)
3660 assert(lane0
< 4 && lane1
< 4 && lane2
< 4 && lane3
< 4);
3661 return _dpp_quad_perm
| lane0
| (lane1
<< 2) | (lane2
<< 4) | (lane3
<< 6);
3664 static inline enum dpp_ctrl
3665 dpp_row_sl(unsigned amount
)
3667 assert(amount
> 0 && amount
< 16);
3668 return _dpp_row_sl
| amount
;
3671 static inline enum dpp_ctrl
3672 dpp_row_sr(unsigned amount
)
3674 assert(amount
> 0 && amount
< 16);
3675 return _dpp_row_sr
| amount
;
3679 _ac_build_dpp(struct ac_llvm_context
*ctx
, LLVMValueRef old
, LLVMValueRef src
,
3680 enum dpp_ctrl dpp_ctrl
, unsigned row_mask
, unsigned bank_mask
,
3683 return ac_build_intrinsic(ctx
, "llvm.amdgcn.update.dpp.i32",
3687 LLVMConstInt(ctx
->i32
, dpp_ctrl
, 0),
3688 LLVMConstInt(ctx
->i32
, row_mask
, 0),
3689 LLVMConstInt(ctx
->i32
, bank_mask
, 0),
3690 LLVMConstInt(ctx
->i1
, bound_ctrl
, 0) },
3691 6, AC_FUNC_ATTR_READNONE
| AC_FUNC_ATTR_CONVERGENT
);
3695 ac_build_dpp(struct ac_llvm_context
*ctx
, LLVMValueRef old
, LLVMValueRef src
,
3696 enum dpp_ctrl dpp_ctrl
, unsigned row_mask
, unsigned bank_mask
,
3699 LLVMTypeRef src_type
= LLVMTypeOf(src
);
3700 src
= ac_to_integer(ctx
, src
);
3701 old
= ac_to_integer(ctx
, old
);
3702 unsigned bits
= LLVMGetIntTypeWidth(LLVMTypeOf(src
));
3705 ret
= _ac_build_dpp(ctx
, old
, src
, dpp_ctrl
, row_mask
,
3706 bank_mask
, bound_ctrl
);
3708 assert(bits
% 32 == 0);
3709 LLVMTypeRef vec_type
= LLVMVectorType(ctx
->i32
, bits
/ 32);
3710 LLVMValueRef src_vector
=
3711 LLVMBuildBitCast(ctx
->builder
, src
, vec_type
, "");
3712 LLVMValueRef old_vector
=
3713 LLVMBuildBitCast(ctx
->builder
, old
, vec_type
, "");
3714 ret
= LLVMGetUndef(vec_type
);
3715 for (unsigned i
= 0; i
< bits
/ 32; i
++) {
3716 src
= LLVMBuildExtractElement(ctx
->builder
, src_vector
,
3717 LLVMConstInt(ctx
->i32
, i
,
3719 old
= LLVMBuildExtractElement(ctx
->builder
, old_vector
,
3720 LLVMConstInt(ctx
->i32
, i
,
3722 LLVMValueRef ret_comp
= _ac_build_dpp(ctx
, old
, src
,
3727 ret
= LLVMBuildInsertElement(ctx
->builder
, ret
,
3729 LLVMConstInt(ctx
->i32
, i
,
3733 return LLVMBuildBitCast(ctx
->builder
, ret
, src_type
, "");
3737 _ac_build_permlane16(struct ac_llvm_context
*ctx
, LLVMValueRef src
, uint64_t sel
,
3738 bool exchange_rows
, bool bound_ctrl
)
3740 LLVMValueRef args
[6] = {
3743 LLVMConstInt(ctx
->i32
, sel
, false),
3744 LLVMConstInt(ctx
->i32
, sel
>> 32, false),
3745 ctx
->i1true
, /* fi */
3746 bound_ctrl
? ctx
->i1true
: ctx
->i1false
,
3748 return ac_build_intrinsic(ctx
, exchange_rows
? "llvm.amdgcn.permlanex16"
3749 : "llvm.amdgcn.permlane16",
3751 AC_FUNC_ATTR_READNONE
| AC_FUNC_ATTR_CONVERGENT
);
3755 ac_build_permlane16(struct ac_llvm_context
*ctx
, LLVMValueRef src
, uint64_t sel
,
3756 bool exchange_rows
, bool bound_ctrl
)
3758 LLVMTypeRef src_type
= LLVMTypeOf(src
);
3759 src
= ac_to_integer(ctx
, src
);
3760 unsigned bits
= LLVMGetIntTypeWidth(LLVMTypeOf(src
));
3763 ret
= _ac_build_permlane16(ctx
, src
, sel
, exchange_rows
,
3766 assert(bits
% 32 == 0);
3767 LLVMTypeRef vec_type
= LLVMVectorType(ctx
->i32
, bits
/ 32);
3768 LLVMValueRef src_vector
=
3769 LLVMBuildBitCast(ctx
->builder
, src
, vec_type
, "");
3770 ret
= LLVMGetUndef(vec_type
);
3771 for (unsigned i
= 0; i
< bits
/ 32; i
++) {
3772 src
= LLVMBuildExtractElement(ctx
->builder
, src_vector
,
3773 LLVMConstInt(ctx
->i32
, i
,
3775 LLVMValueRef ret_comp
=
3776 _ac_build_permlane16(ctx
, src
, sel
,
3779 ret
= LLVMBuildInsertElement(ctx
->builder
, ret
,
3781 LLVMConstInt(ctx
->i32
, i
,
3785 return LLVMBuildBitCast(ctx
->builder
, ret
, src_type
, "");
3788 static inline unsigned
3789 ds_pattern_bitmode(unsigned and_mask
, unsigned or_mask
, unsigned xor_mask
)
3791 assert(and_mask
< 32 && or_mask
< 32 && xor_mask
< 32);
3792 return and_mask
| (or_mask
<< 5) | (xor_mask
<< 10);
3796 _ac_build_ds_swizzle(struct ac_llvm_context
*ctx
, LLVMValueRef src
, unsigned mask
)
3798 return ac_build_intrinsic(ctx
, "llvm.amdgcn.ds.swizzle",
3799 LLVMTypeOf(src
), (LLVMValueRef
[]) {
3800 src
, LLVMConstInt(ctx
->i32
, mask
, 0) },
3801 2, AC_FUNC_ATTR_READNONE
| AC_FUNC_ATTR_CONVERGENT
);
3805 ac_build_ds_swizzle(struct ac_llvm_context
*ctx
, LLVMValueRef src
, unsigned mask
)
3807 LLVMTypeRef src_type
= LLVMTypeOf(src
);
3808 src
= ac_to_integer(ctx
, src
);
3809 unsigned bits
= LLVMGetIntTypeWidth(LLVMTypeOf(src
));
3812 ret
= _ac_build_ds_swizzle(ctx
, src
, mask
);
3814 assert(bits
% 32 == 0);
3815 LLVMTypeRef vec_type
= LLVMVectorType(ctx
->i32
, bits
/ 32);
3816 LLVMValueRef src_vector
=
3817 LLVMBuildBitCast(ctx
->builder
, src
, vec_type
, "");
3818 ret
= LLVMGetUndef(vec_type
);
3819 for (unsigned i
= 0; i
< bits
/ 32; i
++) {
3820 src
= LLVMBuildExtractElement(ctx
->builder
, src_vector
,
3821 LLVMConstInt(ctx
->i32
, i
,
3823 LLVMValueRef ret_comp
= _ac_build_ds_swizzle(ctx
, src
,
3825 ret
= LLVMBuildInsertElement(ctx
->builder
, ret
,
3827 LLVMConstInt(ctx
->i32
, i
,
3831 return LLVMBuildBitCast(ctx
->builder
, ret
, src_type
, "");
3835 ac_build_wwm(struct ac_llvm_context
*ctx
, LLVMValueRef src
)
3837 char name
[32], type
[8];
3838 ac_build_type_name_for_intr(LLVMTypeOf(src
), type
, sizeof(type
));
3839 snprintf(name
, sizeof(name
), "llvm.amdgcn.wwm.%s", type
);
3840 return ac_build_intrinsic(ctx
, name
, LLVMTypeOf(src
),
3841 (LLVMValueRef
[]) { src
}, 1,
3842 AC_FUNC_ATTR_READNONE
);
3846 ac_build_set_inactive(struct ac_llvm_context
*ctx
, LLVMValueRef src
,
3847 LLVMValueRef inactive
)
3849 char name
[33], type
[8];
3850 LLVMTypeRef src_type
= LLVMTypeOf(src
);
3851 src
= ac_to_integer(ctx
, src
);
3852 inactive
= ac_to_integer(ctx
, inactive
);
3853 ac_build_type_name_for_intr(LLVMTypeOf(src
), type
, sizeof(type
));
3854 snprintf(name
, sizeof(name
), "llvm.amdgcn.set.inactive.%s", type
);
3856 ac_build_intrinsic(ctx
, name
,
3857 LLVMTypeOf(src
), (LLVMValueRef
[]) {
3859 AC_FUNC_ATTR_READNONE
|
3860 AC_FUNC_ATTR_CONVERGENT
);
3861 return LLVMBuildBitCast(ctx
->builder
, ret
, src_type
, "");
3865 get_reduction_identity(struct ac_llvm_context
*ctx
, nir_op op
, unsigned type_size
)
3867 if (type_size
== 4) {
3869 case nir_op_iadd
: return ctx
->i32_0
;
3870 case nir_op_fadd
: return ctx
->f32_0
;
3871 case nir_op_imul
: return ctx
->i32_1
;
3872 case nir_op_fmul
: return ctx
->f32_1
;
3873 case nir_op_imin
: return LLVMConstInt(ctx
->i32
, INT32_MAX
, 0);
3874 case nir_op_umin
: return LLVMConstInt(ctx
->i32
, UINT32_MAX
, 0);
3875 case nir_op_fmin
: return LLVMConstReal(ctx
->f32
, INFINITY
);
3876 case nir_op_imax
: return LLVMConstInt(ctx
->i32
, INT32_MIN
, 0);
3877 case nir_op_umax
: return ctx
->i32_0
;
3878 case nir_op_fmax
: return LLVMConstReal(ctx
->f32
, -INFINITY
);
3879 case nir_op_iand
: return LLVMConstInt(ctx
->i32
, -1, 0);
3880 case nir_op_ior
: return ctx
->i32_0
;
3881 case nir_op_ixor
: return ctx
->i32_0
;
3883 unreachable("bad reduction intrinsic");
3885 } else { /* type_size == 64bit */
3887 case nir_op_iadd
: return ctx
->i64_0
;
3888 case nir_op_fadd
: return ctx
->f64_0
;
3889 case nir_op_imul
: return ctx
->i64_1
;
3890 case nir_op_fmul
: return ctx
->f64_1
;
3891 case nir_op_imin
: return LLVMConstInt(ctx
->i64
, INT64_MAX
, 0);
3892 case nir_op_umin
: return LLVMConstInt(ctx
->i64
, UINT64_MAX
, 0);
3893 case nir_op_fmin
: return LLVMConstReal(ctx
->f64
, INFINITY
);
3894 case nir_op_imax
: return LLVMConstInt(ctx
->i64
, INT64_MIN
, 0);
3895 case nir_op_umax
: return ctx
->i64_0
;
3896 case nir_op_fmax
: return LLVMConstReal(ctx
->f64
, -INFINITY
);
3897 case nir_op_iand
: return LLVMConstInt(ctx
->i64
, -1, 0);
3898 case nir_op_ior
: return ctx
->i64_0
;
3899 case nir_op_ixor
: return ctx
->i64_0
;
3901 unreachable("bad reduction intrinsic");
3907 ac_build_alu_op(struct ac_llvm_context
*ctx
, LLVMValueRef lhs
, LLVMValueRef rhs
, nir_op op
)
3909 bool _64bit
= ac_get_type_size(LLVMTypeOf(lhs
)) == 8;
3911 case nir_op_iadd
: return LLVMBuildAdd(ctx
->builder
, lhs
, rhs
, "");
3912 case nir_op_fadd
: return LLVMBuildFAdd(ctx
->builder
, lhs
, rhs
, "");
3913 case nir_op_imul
: return LLVMBuildMul(ctx
->builder
, lhs
, rhs
, "");
3914 case nir_op_fmul
: return LLVMBuildFMul(ctx
->builder
, lhs
, rhs
, "");
3915 case nir_op_imin
: return LLVMBuildSelect(ctx
->builder
,
3916 LLVMBuildICmp(ctx
->builder
, LLVMIntSLT
, lhs
, rhs
, ""),
3918 case nir_op_umin
: return LLVMBuildSelect(ctx
->builder
,
3919 LLVMBuildICmp(ctx
->builder
, LLVMIntULT
, lhs
, rhs
, ""),
3921 case nir_op_fmin
: return ac_build_intrinsic(ctx
,
3922 _64bit
? "llvm.minnum.f64" : "llvm.minnum.f32",
3923 _64bit
? ctx
->f64
: ctx
->f32
,
3924 (LLVMValueRef
[]){lhs
, rhs
}, 2, AC_FUNC_ATTR_READNONE
);
3925 case nir_op_imax
: return LLVMBuildSelect(ctx
->builder
,
3926 LLVMBuildICmp(ctx
->builder
, LLVMIntSGT
, lhs
, rhs
, ""),
3928 case nir_op_umax
: return LLVMBuildSelect(ctx
->builder
,
3929 LLVMBuildICmp(ctx
->builder
, LLVMIntUGT
, lhs
, rhs
, ""),
3931 case nir_op_fmax
: return ac_build_intrinsic(ctx
,
3932 _64bit
? "llvm.maxnum.f64" : "llvm.maxnum.f32",
3933 _64bit
? ctx
->f64
: ctx
->f32
,
3934 (LLVMValueRef
[]){lhs
, rhs
}, 2, AC_FUNC_ATTR_READNONE
);
3935 case nir_op_iand
: return LLVMBuildAnd(ctx
->builder
, lhs
, rhs
, "");
3936 case nir_op_ior
: return LLVMBuildOr(ctx
->builder
, lhs
, rhs
, "");
3937 case nir_op_ixor
: return LLVMBuildXor(ctx
->builder
, lhs
, rhs
, "");
3939 unreachable("bad reduction intrinsic");
3944 * \param maxprefix specifies that the result only needs to be correct for a
3945 * prefix of this many threads
3947 * TODO: add inclusive and excluse scan functions for GFX6.
3950 ac_build_scan(struct ac_llvm_context
*ctx
, nir_op op
, LLVMValueRef src
, LLVMValueRef identity
,
3951 unsigned maxprefix
, bool inclusive
)
3953 LLVMValueRef result
, tmp
;
3955 if (ctx
->chip_class
>= GFX10
) {
3956 result
= inclusive
? src
: identity
;
3959 src
= ac_build_dpp(ctx
, identity
, src
, dpp_wf_sr1
, 0xf, 0xf, false);
3964 tmp
= ac_build_dpp(ctx
, identity
, src
, dpp_row_sr(1), 0xf, 0xf, false);
3965 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
3968 tmp
= ac_build_dpp(ctx
, identity
, src
, dpp_row_sr(2), 0xf, 0xf, false);
3969 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
3972 tmp
= ac_build_dpp(ctx
, identity
, src
, dpp_row_sr(3), 0xf, 0xf, false);
3973 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
3976 tmp
= ac_build_dpp(ctx
, identity
, result
, dpp_row_sr(4), 0xf, 0xe, false);
3977 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
3980 tmp
= ac_build_dpp(ctx
, identity
, result
, dpp_row_sr(8), 0xf, 0xc, false);
3981 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
3982 if (maxprefix
<= 16)
3985 if (ctx
->chip_class
>= GFX10
) {
3986 /* dpp_row_bcast{15,31} are not supported on gfx10. */
3987 LLVMBuilderRef builder
= ctx
->builder
;
3988 LLVMValueRef tid
= ac_get_thread_id(ctx
);
3990 /* TODO-GFX10: Can we get better code-gen by putting this into
3991 * a branch so that LLVM generates EXEC mask manipulations? */
3995 tmp
= ac_build_alu_op(ctx
, result
, src
, op
);
3996 tmp
= ac_build_permlane16(ctx
, tmp
, ~(uint64_t)0, true, false);
3997 tmp
= ac_build_alu_op(ctx
, result
, tmp
, op
);
3998 cc
= LLVMBuildAnd(builder
, tid
, LLVMConstInt(ctx
->i32
, 16, false), "");
3999 cc
= LLVMBuildICmp(builder
, LLVMIntNE
, cc
, ctx
->i32_0
, "");
4000 result
= LLVMBuildSelect(builder
, cc
, tmp
, result
, "");
4001 if (maxprefix
<= 32)
4007 tmp
= ac_build_alu_op(ctx
, result
, src
, op
);
4008 tmp
= ac_build_readlane(ctx
, tmp
, LLVMConstInt(ctx
->i32
, 31, false));
4009 tmp
= ac_build_alu_op(ctx
, result
, tmp
, op
);
4010 cc
= LLVMBuildICmp(builder
, LLVMIntUGE
, tid
,
4011 LLVMConstInt(ctx
->i32
, 32, false), "");
4012 result
= LLVMBuildSelect(builder
, cc
, tmp
, result
, "");
4016 tmp
= ac_build_dpp(ctx
, identity
, result
, dpp_row_bcast15
, 0xa, 0xf, false);
4017 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
4018 if (maxprefix
<= 32)
4020 tmp
= ac_build_dpp(ctx
, identity
, result
, dpp_row_bcast31
, 0xc, 0xf, false);
4021 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
4026 ac_build_inclusive_scan(struct ac_llvm_context
*ctx
, LLVMValueRef src
, nir_op op
)
4028 LLVMValueRef result
;
4030 if (LLVMTypeOf(src
) == ctx
->i1
&& op
== nir_op_iadd
) {
4031 LLVMBuilderRef builder
= ctx
->builder
;
4032 src
= LLVMBuildZExt(builder
, src
, ctx
->i32
, "");
4033 result
= ac_build_ballot(ctx
, src
);
4034 result
= ac_build_mbcnt(ctx
, result
);
4035 result
= LLVMBuildAdd(builder
, result
, src
, "");
4039 ac_build_optimization_barrier(ctx
, &src
);
4041 LLVMValueRef identity
=
4042 get_reduction_identity(ctx
, op
, ac_get_type_size(LLVMTypeOf(src
)));
4043 result
= LLVMBuildBitCast(ctx
->builder
, ac_build_set_inactive(ctx
, src
, identity
),
4044 LLVMTypeOf(identity
), "");
4045 result
= ac_build_scan(ctx
, op
, result
, identity
, ctx
->wave_size
, true);
4047 return ac_build_wwm(ctx
, result
);
4051 ac_build_exclusive_scan(struct ac_llvm_context
*ctx
, LLVMValueRef src
, nir_op op
)
4053 LLVMValueRef result
;
4055 if (LLVMTypeOf(src
) == ctx
->i1
&& op
== nir_op_iadd
) {
4056 LLVMBuilderRef builder
= ctx
->builder
;
4057 src
= LLVMBuildZExt(builder
, src
, ctx
->i32
, "");
4058 result
= ac_build_ballot(ctx
, src
);
4059 result
= ac_build_mbcnt(ctx
, result
);
4063 ac_build_optimization_barrier(ctx
, &src
);
4065 LLVMValueRef identity
=
4066 get_reduction_identity(ctx
, op
, ac_get_type_size(LLVMTypeOf(src
)));
4067 result
= LLVMBuildBitCast(ctx
->builder
, ac_build_set_inactive(ctx
, src
, identity
),
4068 LLVMTypeOf(identity
), "");
4069 result
= ac_build_scan(ctx
, op
, result
, identity
, ctx
->wave_size
, false);
4071 return ac_build_wwm(ctx
, result
);
4075 ac_build_reduce(struct ac_llvm_context
*ctx
, LLVMValueRef src
, nir_op op
, unsigned cluster_size
)
4077 if (cluster_size
== 1) return src
;
4078 ac_build_optimization_barrier(ctx
, &src
);
4079 LLVMValueRef result
, swap
;
4080 LLVMValueRef identity
= get_reduction_identity(ctx
, op
,
4081 ac_get_type_size(LLVMTypeOf(src
)));
4082 result
= LLVMBuildBitCast(ctx
->builder
,
4083 ac_build_set_inactive(ctx
, src
, identity
),
4084 LLVMTypeOf(identity
), "");
4085 swap
= ac_build_quad_swizzle(ctx
, result
, 1, 0, 3, 2);
4086 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
4087 if (cluster_size
== 2) return ac_build_wwm(ctx
, result
);
4089 swap
= ac_build_quad_swizzle(ctx
, result
, 2, 3, 0, 1);
4090 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
4091 if (cluster_size
== 4) return ac_build_wwm(ctx
, result
);
4093 if (ctx
->chip_class
>= GFX8
)
4094 swap
= ac_build_dpp(ctx
, identity
, result
, dpp_row_half_mirror
, 0xf, 0xf, false);
4096 swap
= ac_build_ds_swizzle(ctx
, result
, ds_pattern_bitmode(0x1f, 0, 0x04));
4097 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
4098 if (cluster_size
== 8) return ac_build_wwm(ctx
, result
);
4100 if (ctx
->chip_class
>= GFX8
)
4101 swap
= ac_build_dpp(ctx
, identity
, result
, dpp_row_mirror
, 0xf, 0xf, false);
4103 swap
= ac_build_ds_swizzle(ctx
, result
, ds_pattern_bitmode(0x1f, 0, 0x08));
4104 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
4105 if (cluster_size
== 16) return ac_build_wwm(ctx
, result
);
4107 if (ctx
->chip_class
>= GFX10
)
4108 swap
= ac_build_permlane16(ctx
, result
, 0, true, false);
4109 else if (ctx
->chip_class
>= GFX8
&& cluster_size
!= 32)
4110 swap
= ac_build_dpp(ctx
, identity
, result
, dpp_row_bcast15
, 0xa, 0xf, false);
4112 swap
= ac_build_ds_swizzle(ctx
, result
, ds_pattern_bitmode(0x1f, 0, 0x10));
4113 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
4114 if (cluster_size
== 32) return ac_build_wwm(ctx
, result
);
4116 if (ctx
->chip_class
>= GFX8
) {
4117 if (ctx
->chip_class
>= GFX10
)
4118 swap
= ac_build_readlane(ctx
, result
, LLVMConstInt(ctx
->i32
, 31, false));
4120 swap
= ac_build_dpp(ctx
, identity
, result
, dpp_row_bcast31
, 0xc, 0xf, false);
4121 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
4122 result
= ac_build_readlane(ctx
, result
, LLVMConstInt(ctx
->i32
, 63, 0));
4123 return ac_build_wwm(ctx
, result
);
4125 swap
= ac_build_readlane(ctx
, result
, ctx
->i32_0
);
4126 result
= ac_build_readlane(ctx
, result
, LLVMConstInt(ctx
->i32
, 32, 0));
4127 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
4128 return ac_build_wwm(ctx
, result
);
4133 * "Top half" of a scan that reduces per-wave values across an entire
4136 * The source value must be present in the highest lane of the wave, and the
4137 * highest lane must be live.
4140 ac_build_wg_wavescan_top(struct ac_llvm_context
*ctx
, struct ac_wg_scan
*ws
)
4142 if (ws
->maxwaves
<= 1)
4145 const LLVMValueRef last_lane
= LLVMConstInt(ctx
->i32
, ctx
->wave_size
- 1, false);
4146 LLVMBuilderRef builder
= ctx
->builder
;
4147 LLVMValueRef tid
= ac_get_thread_id(ctx
);
4150 tmp
= LLVMBuildICmp(builder
, LLVMIntEQ
, tid
, last_lane
, "");
4151 ac_build_ifcc(ctx
, tmp
, 1000);
4152 LLVMBuildStore(builder
, ws
->src
, LLVMBuildGEP(builder
, ws
->scratch
, &ws
->waveidx
, 1, ""));
4153 ac_build_endif(ctx
, 1000);
4157 * "Bottom half" of a scan that reduces per-wave values across an entire
4160 * The caller must place a barrier between the top and bottom halves.
4163 ac_build_wg_wavescan_bottom(struct ac_llvm_context
*ctx
, struct ac_wg_scan
*ws
)
4165 const LLVMTypeRef type
= LLVMTypeOf(ws
->src
);
4166 const LLVMValueRef identity
=
4167 get_reduction_identity(ctx
, ws
->op
, ac_get_type_size(type
));
4169 if (ws
->maxwaves
<= 1) {
4170 ws
->result_reduce
= ws
->src
;
4171 ws
->result_inclusive
= ws
->src
;
4172 ws
->result_exclusive
= identity
;
4175 assert(ws
->maxwaves
<= 32);
4177 LLVMBuilderRef builder
= ctx
->builder
;
4178 LLVMValueRef tid
= ac_get_thread_id(ctx
);
4179 LLVMBasicBlockRef bbs
[2];
4180 LLVMValueRef phivalues_scan
[2];
4181 LLVMValueRef tmp
, tmp2
;
4183 bbs
[0] = LLVMGetInsertBlock(builder
);
4184 phivalues_scan
[0] = LLVMGetUndef(type
);
4186 if (ws
->enable_reduce
)
4187 tmp
= LLVMBuildICmp(builder
, LLVMIntULT
, tid
, ws
->numwaves
, "");
4188 else if (ws
->enable_inclusive
)
4189 tmp
= LLVMBuildICmp(builder
, LLVMIntULE
, tid
, ws
->waveidx
, "");
4191 tmp
= LLVMBuildICmp(builder
, LLVMIntULT
, tid
, ws
->waveidx
, "");
4192 ac_build_ifcc(ctx
, tmp
, 1001);
4194 tmp
= LLVMBuildLoad(builder
, LLVMBuildGEP(builder
, ws
->scratch
, &tid
, 1, ""), "");
4196 ac_build_optimization_barrier(ctx
, &tmp
);
4198 bbs
[1] = LLVMGetInsertBlock(builder
);
4199 phivalues_scan
[1] = ac_build_scan(ctx
, ws
->op
, tmp
, identity
, ws
->maxwaves
, true);
4201 ac_build_endif(ctx
, 1001);
4203 const LLVMValueRef scan
= ac_build_phi(ctx
, type
, 2, phivalues_scan
, bbs
);
4205 if (ws
->enable_reduce
) {
4206 tmp
= LLVMBuildSub(builder
, ws
->numwaves
, ctx
->i32_1
, "");
4207 ws
->result_reduce
= ac_build_readlane(ctx
, scan
, tmp
);
4209 if (ws
->enable_inclusive
)
4210 ws
->result_inclusive
= ac_build_readlane(ctx
, scan
, ws
->waveidx
);
4211 if (ws
->enable_exclusive
) {
4212 tmp
= LLVMBuildSub(builder
, ws
->waveidx
, ctx
->i32_1
, "");
4213 tmp
= ac_build_readlane(ctx
, scan
, tmp
);
4214 tmp2
= LLVMBuildICmp(builder
, LLVMIntEQ
, ws
->waveidx
, ctx
->i32_0
, "");
4215 ws
->result_exclusive
= LLVMBuildSelect(builder
, tmp2
, identity
, tmp
, "");
4220 * Inclusive scan of a per-wave value across an entire workgroup.
4222 * This implies an s_barrier instruction.
4224 * Unlike ac_build_inclusive_scan, the caller \em must ensure that all threads
4225 * of the workgroup are live. (This requirement cannot easily be relaxed in a
4226 * useful manner because of the barrier in the algorithm.)
4229 ac_build_wg_wavescan(struct ac_llvm_context
*ctx
, struct ac_wg_scan
*ws
)
4231 ac_build_wg_wavescan_top(ctx
, ws
);
4232 ac_build_s_barrier(ctx
);
4233 ac_build_wg_wavescan_bottom(ctx
, ws
);
4237 * "Top half" of a scan that reduces per-thread values across an entire
4240 * All lanes must be active when this code runs.
4243 ac_build_wg_scan_top(struct ac_llvm_context
*ctx
, struct ac_wg_scan
*ws
)
4245 if (ws
->enable_exclusive
) {
4246 ws
->extra
= ac_build_exclusive_scan(ctx
, ws
->src
, ws
->op
);
4247 if (LLVMTypeOf(ws
->src
) == ctx
->i1
&& ws
->op
== nir_op_iadd
)
4248 ws
->src
= LLVMBuildZExt(ctx
->builder
, ws
->src
, ctx
->i32
, "");
4249 ws
->src
= ac_build_alu_op(ctx
, ws
->extra
, ws
->src
, ws
->op
);
4251 ws
->src
= ac_build_inclusive_scan(ctx
, ws
->src
, ws
->op
);
4254 bool enable_inclusive
= ws
->enable_inclusive
;
4255 bool enable_exclusive
= ws
->enable_exclusive
;
4256 ws
->enable_inclusive
= false;
4257 ws
->enable_exclusive
= ws
->enable_exclusive
|| enable_inclusive
;
4258 ac_build_wg_wavescan_top(ctx
, ws
);
4259 ws
->enable_inclusive
= enable_inclusive
;
4260 ws
->enable_exclusive
= enable_exclusive
;
4264 * "Bottom half" of a scan that reduces per-thread values across an entire
4267 * The caller must place a barrier between the top and bottom halves.
4270 ac_build_wg_scan_bottom(struct ac_llvm_context
*ctx
, struct ac_wg_scan
*ws
)
4272 bool enable_inclusive
= ws
->enable_inclusive
;
4273 bool enable_exclusive
= ws
->enable_exclusive
;
4274 ws
->enable_inclusive
= false;
4275 ws
->enable_exclusive
= ws
->enable_exclusive
|| enable_inclusive
;
4276 ac_build_wg_wavescan_bottom(ctx
, ws
);
4277 ws
->enable_inclusive
= enable_inclusive
;
4278 ws
->enable_exclusive
= enable_exclusive
;
4280 /* ws->result_reduce is already the correct value */
4281 if (ws
->enable_inclusive
)
4282 ws
->result_inclusive
= ac_build_alu_op(ctx
, ws
->result_inclusive
, ws
->src
, ws
->op
);
4283 if (ws
->enable_exclusive
)
4284 ws
->result_exclusive
= ac_build_alu_op(ctx
, ws
->result_exclusive
, ws
->extra
, ws
->op
);
4288 * A scan that reduces per-thread values across an entire workgroup.
4290 * The caller must ensure that all lanes are active when this code runs
4291 * (WWM is insufficient!), because there is an implied barrier.
4294 ac_build_wg_scan(struct ac_llvm_context
*ctx
, struct ac_wg_scan
*ws
)
4296 ac_build_wg_scan_top(ctx
, ws
);
4297 ac_build_s_barrier(ctx
);
4298 ac_build_wg_scan_bottom(ctx
, ws
);
4302 ac_build_quad_swizzle(struct ac_llvm_context
*ctx
, LLVMValueRef src
,
4303 unsigned lane0
, unsigned lane1
, unsigned lane2
, unsigned lane3
)
4305 unsigned mask
= dpp_quad_perm(lane0
, lane1
, lane2
, lane3
);
4306 if (ctx
->chip_class
>= GFX8
) {
4307 return ac_build_dpp(ctx
, src
, src
, mask
, 0xf, 0xf, false);
4309 return ac_build_ds_swizzle(ctx
, src
, (1 << 15) | mask
);
4314 ac_build_shuffle(struct ac_llvm_context
*ctx
, LLVMValueRef src
, LLVMValueRef index
)
4316 index
= LLVMBuildMul(ctx
->builder
, index
, LLVMConstInt(ctx
->i32
, 4, 0), "");
4317 return ac_build_intrinsic(ctx
,
4318 "llvm.amdgcn.ds.bpermute", ctx
->i32
,
4319 (LLVMValueRef
[]) {index
, src
}, 2,
4320 AC_FUNC_ATTR_READNONE
|
4321 AC_FUNC_ATTR_CONVERGENT
);
4325 ac_build_frexp_exp(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
4331 if (bitsize
== 16) {
4332 intr
= "llvm.amdgcn.frexp.exp.i16.f16";
4334 } else if (bitsize
== 32) {
4335 intr
= "llvm.amdgcn.frexp.exp.i32.f32";
4338 intr
= "llvm.amdgcn.frexp.exp.i32.f64";
4342 LLVMValueRef params
[] = {
4345 return ac_build_intrinsic(ctx
, intr
, type
, params
, 1,
4346 AC_FUNC_ATTR_READNONE
);
4349 ac_build_frexp_mant(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
4355 if (bitsize
== 16) {
4356 intr
= "llvm.amdgcn.frexp.mant.f16";
4358 } else if (bitsize
== 32) {
4359 intr
= "llvm.amdgcn.frexp.mant.f32";
4362 intr
= "llvm.amdgcn.frexp.mant.f64";
4366 LLVMValueRef params
[] = {
4369 return ac_build_intrinsic(ctx
, intr
, type
, params
, 1,
4370 AC_FUNC_ATTR_READNONE
);
4374 ac_build_canonicalize(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
4380 if (bitsize
== 16) {
4381 intr
= "llvm.canonicalize.f16";
4383 } else if (bitsize
== 32) {
4384 intr
= "llvm.canonicalize.f32";
4386 } else if (bitsize
== 64) {
4387 intr
= "llvm.canonicalize.f64";
4391 LLVMValueRef params
[] = {
4394 return ac_build_intrinsic(ctx
, intr
, type
, params
, 1,
4395 AC_FUNC_ATTR_READNONE
);
4399 * this takes an I,J coordinate pair,
4400 * and works out the X and Y derivatives.
4401 * it returns DDX(I), DDX(J), DDY(I), DDY(J).
4404 ac_build_ddxy_interp(struct ac_llvm_context
*ctx
, LLVMValueRef interp_ij
)
4406 LLVMValueRef result
[4], a
;
4409 for (i
= 0; i
< 2; i
++) {
4410 a
= LLVMBuildExtractElement(ctx
->builder
, interp_ij
,
4411 LLVMConstInt(ctx
->i32
, i
, false), "");
4412 result
[i
] = ac_build_ddxy(ctx
, AC_TID_MASK_TOP_LEFT
, 1, a
);
4413 result
[2+i
] = ac_build_ddxy(ctx
, AC_TID_MASK_TOP_LEFT
, 2, a
);
4415 return ac_build_gather_values(ctx
, result
, 4);
4419 ac_build_load_helper_invocation(struct ac_llvm_context
*ctx
)
4421 LLVMValueRef result
= ac_build_intrinsic(ctx
, "llvm.amdgcn.ps.live",
4423 AC_FUNC_ATTR_READNONE
);
4424 result
= LLVMBuildNot(ctx
->builder
, result
, "");
4425 return LLVMBuildSExt(ctx
->builder
, result
, ctx
->i32
, "");
4428 LLVMValueRef
ac_build_call(struct ac_llvm_context
*ctx
, LLVMValueRef func
,
4429 LLVMValueRef
*args
, unsigned num_args
)
4431 LLVMValueRef ret
= LLVMBuildCall(ctx
->builder
, func
, args
, num_args
, "");
4432 LLVMSetInstructionCallConv(ret
, LLVMGetFunctionCallConv(func
));
4437 ac_export_mrt_z(struct ac_llvm_context
*ctx
, LLVMValueRef depth
,
4438 LLVMValueRef stencil
, LLVMValueRef samplemask
,
4439 struct ac_export_args
*args
)
4442 unsigned format
= ac_get_spi_shader_z_format(depth
!= NULL
,
4444 samplemask
!= NULL
);
4446 assert(depth
|| stencil
|| samplemask
);
4448 memset(args
, 0, sizeof(*args
));
4450 args
->valid_mask
= 1; /* whether the EXEC mask is valid */
4451 args
->done
= 1; /* DONE bit */
4453 /* Specify the target we are exporting */
4454 args
->target
= V_008DFC_SQ_EXP_MRTZ
;
4456 args
->compr
= 0; /* COMP flag */
4457 args
->out
[0] = LLVMGetUndef(ctx
->f32
); /* R, depth */
4458 args
->out
[1] = LLVMGetUndef(ctx
->f32
); /* G, stencil test val[0:7], stencil op val[8:15] */
4459 args
->out
[2] = LLVMGetUndef(ctx
->f32
); /* B, sample mask */
4460 args
->out
[3] = LLVMGetUndef(ctx
->f32
); /* A, alpha to mask */
4462 if (format
== V_028710_SPI_SHADER_UINT16_ABGR
) {
4464 args
->compr
= 1; /* COMPR flag */
4467 /* Stencil should be in X[23:16]. */
4468 stencil
= ac_to_integer(ctx
, stencil
);
4469 stencil
= LLVMBuildShl(ctx
->builder
, stencil
,
4470 LLVMConstInt(ctx
->i32
, 16, 0), "");
4471 args
->out
[0] = ac_to_float(ctx
, stencil
);
4475 /* SampleMask should be in Y[15:0]. */
4476 args
->out
[1] = samplemask
;
4481 args
->out
[0] = depth
;
4485 args
->out
[1] = stencil
;
4489 args
->out
[2] = samplemask
;
4494 /* GFX6 (except OLAND and HAINAN) has a bug that it only looks
4495 * at the X writemask component. */
4496 if (ctx
->chip_class
== GFX6
&&
4497 ctx
->family
!= CHIP_OLAND
&&
4498 ctx
->family
!= CHIP_HAINAN
)
4501 /* Specify which components to enable */
4502 args
->enabled_channels
= mask
;