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_exp_param.h"
38 #include "util/bitscan.h"
39 #include "util/macros.h"
40 #include "util/u_atomic.h"
41 #include "util/u_math.h"
44 #include "shader_enums.h"
46 #define AC_LLVM_INITIAL_CF_DEPTH 4
48 /* Data for if/else/endif and bgnloop/endloop control flow structures.
51 /* Loop exit or next part of if/else/endif. */
52 LLVMBasicBlockRef next_block
;
53 LLVMBasicBlockRef loop_entry_block
;
56 /* Initialize module-independent parts of the context.
58 * The caller is responsible for initializing ctx::module and ctx::builder.
61 ac_llvm_context_init(struct ac_llvm_context
*ctx
,
62 struct ac_llvm_compiler
*compiler
,
63 enum chip_class chip_class
, enum radeon_family family
,
64 enum ac_float_mode float_mode
, unsigned wave_size
,
65 unsigned ballot_mask_bits
)
69 ctx
->context
= LLVMContextCreate();
71 ctx
->chip_class
= chip_class
;
73 ctx
->wave_size
= wave_size
;
74 ctx
->ballot_mask_bits
= ballot_mask_bits
;
75 ctx
->module
= ac_create_module(wave_size
== 32 ? compiler
->tm_wave32
78 ctx
->builder
= ac_create_builder(ctx
->context
, float_mode
);
80 ctx
->voidt
= LLVMVoidTypeInContext(ctx
->context
);
81 ctx
->i1
= LLVMInt1TypeInContext(ctx
->context
);
82 ctx
->i8
= LLVMInt8TypeInContext(ctx
->context
);
83 ctx
->i16
= LLVMIntTypeInContext(ctx
->context
, 16);
84 ctx
->i32
= LLVMIntTypeInContext(ctx
->context
, 32);
85 ctx
->i64
= LLVMIntTypeInContext(ctx
->context
, 64);
86 ctx
->intptr
= ctx
->i32
;
87 ctx
->f16
= LLVMHalfTypeInContext(ctx
->context
);
88 ctx
->f32
= LLVMFloatTypeInContext(ctx
->context
);
89 ctx
->f64
= LLVMDoubleTypeInContext(ctx
->context
);
90 ctx
->v2i16
= LLVMVectorType(ctx
->i16
, 2);
91 ctx
->v2i32
= LLVMVectorType(ctx
->i32
, 2);
92 ctx
->v3i32
= LLVMVectorType(ctx
->i32
, 3);
93 ctx
->v4i32
= LLVMVectorType(ctx
->i32
, 4);
94 ctx
->v2f32
= LLVMVectorType(ctx
->f32
, 2);
95 ctx
->v3f32
= LLVMVectorType(ctx
->f32
, 3);
96 ctx
->v4f32
= LLVMVectorType(ctx
->f32
, 4);
97 ctx
->v8i32
= LLVMVectorType(ctx
->i32
, 8);
98 ctx
->iN_wavemask
= LLVMIntTypeInContext(ctx
->context
, ctx
->wave_size
);
99 ctx
->iN_ballotmask
= LLVMIntTypeInContext(ctx
->context
, ballot_mask_bits
);
101 ctx
->i8_0
= LLVMConstInt(ctx
->i8
, 0, false);
102 ctx
->i8_1
= LLVMConstInt(ctx
->i8
, 1, false);
103 ctx
->i16_0
= LLVMConstInt(ctx
->i16
, 0, false);
104 ctx
->i16_1
= LLVMConstInt(ctx
->i16
, 1, false);
105 ctx
->i32_0
= LLVMConstInt(ctx
->i32
, 0, false);
106 ctx
->i32_1
= LLVMConstInt(ctx
->i32
, 1, false);
107 ctx
->i64_0
= LLVMConstInt(ctx
->i64
, 0, false);
108 ctx
->i64_1
= LLVMConstInt(ctx
->i64
, 1, false);
109 ctx
->f16_0
= LLVMConstReal(ctx
->f16
, 0.0);
110 ctx
->f16_1
= LLVMConstReal(ctx
->f16
, 1.0);
111 ctx
->f32_0
= LLVMConstReal(ctx
->f32
, 0.0);
112 ctx
->f32_1
= LLVMConstReal(ctx
->f32
, 1.0);
113 ctx
->f64_0
= LLVMConstReal(ctx
->f64
, 0.0);
114 ctx
->f64_1
= LLVMConstReal(ctx
->f64
, 1.0);
116 ctx
->i1false
= LLVMConstInt(ctx
->i1
, 0, false);
117 ctx
->i1true
= LLVMConstInt(ctx
->i1
, 1, false);
119 ctx
->range_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
122 ctx
->invariant_load_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
123 "invariant.load", 14);
125 ctx
->fpmath_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
, "fpmath", 6);
127 args
[0] = LLVMConstReal(ctx
->f32
, 2.5);
128 ctx
->fpmath_md_2p5_ulp
= LLVMMDNodeInContext(ctx
->context
, args
, 1);
130 ctx
->uniform_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
131 "amdgpu.uniform", 14);
133 ctx
->empty_md
= LLVMMDNodeInContext(ctx
->context
, NULL
, 0);
134 ctx
->flow
= calloc(1, sizeof(*ctx
->flow
));
138 ac_llvm_context_dispose(struct ac_llvm_context
*ctx
)
140 free(ctx
->flow
->stack
);
146 ac_get_llvm_num_components(LLVMValueRef value
)
148 LLVMTypeRef type
= LLVMTypeOf(value
);
149 unsigned num_components
= LLVMGetTypeKind(type
) == LLVMVectorTypeKind
150 ? LLVMGetVectorSize(type
)
152 return num_components
;
156 ac_llvm_extract_elem(struct ac_llvm_context
*ac
,
160 if (LLVMGetTypeKind(LLVMTypeOf(value
)) != LLVMVectorTypeKind
) {
165 return LLVMBuildExtractElement(ac
->builder
, value
,
166 LLVMConstInt(ac
->i32
, index
, false), "");
170 ac_get_elem_bits(struct ac_llvm_context
*ctx
, LLVMTypeRef type
)
172 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
)
173 type
= LLVMGetElementType(type
);
175 if (LLVMGetTypeKind(type
) == LLVMIntegerTypeKind
)
176 return LLVMGetIntTypeWidth(type
);
178 if (type
== ctx
->f16
)
180 if (type
== ctx
->f32
)
182 if (type
== ctx
->f64
)
185 unreachable("Unhandled type kind in get_elem_bits");
189 ac_get_type_size(LLVMTypeRef type
)
191 LLVMTypeKind kind
= LLVMGetTypeKind(type
);
194 case LLVMIntegerTypeKind
:
195 return LLVMGetIntTypeWidth(type
) / 8;
196 case LLVMHalfTypeKind
:
198 case LLVMFloatTypeKind
:
200 case LLVMDoubleTypeKind
:
202 case LLVMPointerTypeKind
:
203 if (LLVMGetPointerAddressSpace(type
) == AC_ADDR_SPACE_CONST_32BIT
)
206 case LLVMVectorTypeKind
:
207 return LLVMGetVectorSize(type
) *
208 ac_get_type_size(LLVMGetElementType(type
));
209 case LLVMArrayTypeKind
:
210 return LLVMGetArrayLength(type
) *
211 ac_get_type_size(LLVMGetElementType(type
));
218 static LLVMTypeRef
to_integer_type_scalar(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
222 else if (t
== ctx
->f16
|| t
== ctx
->i16
)
224 else if (t
== ctx
->f32
|| t
== ctx
->i32
)
226 else if (t
== ctx
->f64
|| t
== ctx
->i64
)
229 unreachable("Unhandled integer size");
233 ac_to_integer_type(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
235 if (LLVMGetTypeKind(t
) == LLVMVectorTypeKind
) {
236 LLVMTypeRef elem_type
= LLVMGetElementType(t
);
237 return LLVMVectorType(to_integer_type_scalar(ctx
, elem_type
),
238 LLVMGetVectorSize(t
));
240 if (LLVMGetTypeKind(t
) == LLVMPointerTypeKind
) {
241 switch (LLVMGetPointerAddressSpace(t
)) {
242 case AC_ADDR_SPACE_GLOBAL
:
244 case AC_ADDR_SPACE_LDS
:
247 unreachable("unhandled address space");
250 return to_integer_type_scalar(ctx
, t
);
254 ac_to_integer(struct ac_llvm_context
*ctx
, LLVMValueRef v
)
256 LLVMTypeRef type
= LLVMTypeOf(v
);
257 if (LLVMGetTypeKind(type
) == LLVMPointerTypeKind
) {
258 return LLVMBuildPtrToInt(ctx
->builder
, v
, ac_to_integer_type(ctx
, type
), "");
260 return LLVMBuildBitCast(ctx
->builder
, v
, ac_to_integer_type(ctx
, type
), "");
264 ac_to_integer_or_pointer(struct ac_llvm_context
*ctx
, LLVMValueRef v
)
266 LLVMTypeRef type
= LLVMTypeOf(v
);
267 if (LLVMGetTypeKind(type
) == LLVMPointerTypeKind
)
269 return ac_to_integer(ctx
, v
);
272 static LLVMTypeRef
to_float_type_scalar(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
276 else if (t
== ctx
->i16
|| t
== ctx
->f16
)
278 else if (t
== ctx
->i32
|| t
== ctx
->f32
)
280 else if (t
== ctx
->i64
|| t
== ctx
->f64
)
283 unreachable("Unhandled float size");
287 ac_to_float_type(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
289 if (LLVMGetTypeKind(t
) == LLVMVectorTypeKind
) {
290 LLVMTypeRef elem_type
= LLVMGetElementType(t
);
291 return LLVMVectorType(to_float_type_scalar(ctx
, elem_type
),
292 LLVMGetVectorSize(t
));
294 return to_float_type_scalar(ctx
, t
);
298 ac_to_float(struct ac_llvm_context
*ctx
, LLVMValueRef v
)
300 LLVMTypeRef type
= LLVMTypeOf(v
);
301 return LLVMBuildBitCast(ctx
->builder
, v
, ac_to_float_type(ctx
, type
), "");
306 ac_build_intrinsic(struct ac_llvm_context
*ctx
, const char *name
,
307 LLVMTypeRef return_type
, LLVMValueRef
*params
,
308 unsigned param_count
, unsigned attrib_mask
)
310 LLVMValueRef function
, call
;
311 bool set_callsite_attrs
= !(attrib_mask
& AC_FUNC_ATTR_LEGACY
);
313 function
= LLVMGetNamedFunction(ctx
->module
, name
);
315 LLVMTypeRef param_types
[32], function_type
;
318 assert(param_count
<= 32);
320 for (i
= 0; i
< param_count
; ++i
) {
322 param_types
[i
] = LLVMTypeOf(params
[i
]);
325 LLVMFunctionType(return_type
, param_types
, param_count
, 0);
326 function
= LLVMAddFunction(ctx
->module
, name
, function_type
);
328 LLVMSetFunctionCallConv(function
, LLVMCCallConv
);
329 LLVMSetLinkage(function
, LLVMExternalLinkage
);
331 if (!set_callsite_attrs
)
332 ac_add_func_attributes(ctx
->context
, function
, attrib_mask
);
335 call
= LLVMBuildCall(ctx
->builder
, function
, params
, param_count
, "");
336 if (set_callsite_attrs
)
337 ac_add_func_attributes(ctx
->context
, call
, attrib_mask
);
342 * Given the i32 or vNi32 \p type, generate the textual name (e.g. for use with
345 void ac_build_type_name_for_intr(LLVMTypeRef type
, char *buf
, unsigned bufsize
)
347 LLVMTypeRef elem_type
= type
;
349 assert(bufsize
>= 8);
351 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
) {
352 int ret
= snprintf(buf
, bufsize
, "v%u",
353 LLVMGetVectorSize(type
));
355 char *type_name
= LLVMPrintTypeToString(type
);
356 fprintf(stderr
, "Error building type name for: %s\n",
358 LLVMDisposeMessage(type_name
);
361 elem_type
= LLVMGetElementType(type
);
365 switch (LLVMGetTypeKind(elem_type
)) {
367 case LLVMIntegerTypeKind
:
368 snprintf(buf
, bufsize
, "i%d", LLVMGetIntTypeWidth(elem_type
));
370 case LLVMHalfTypeKind
:
371 snprintf(buf
, bufsize
, "f16");
373 case LLVMFloatTypeKind
:
374 snprintf(buf
, bufsize
, "f32");
376 case LLVMDoubleTypeKind
:
377 snprintf(buf
, bufsize
, "f64");
383 * Helper function that builds an LLVM IR PHI node and immediately adds
387 ac_build_phi(struct ac_llvm_context
*ctx
, LLVMTypeRef type
,
388 unsigned count_incoming
, LLVMValueRef
*values
,
389 LLVMBasicBlockRef
*blocks
)
391 LLVMValueRef phi
= LLVMBuildPhi(ctx
->builder
, type
, "");
392 LLVMAddIncoming(phi
, values
, blocks
, count_incoming
);
396 void ac_build_s_barrier(struct ac_llvm_context
*ctx
)
398 ac_build_intrinsic(ctx
, "llvm.amdgcn.s.barrier", ctx
->voidt
, NULL
,
399 0, AC_FUNC_ATTR_CONVERGENT
);
402 /* Prevent optimizations (at least of memory accesses) across the current
403 * point in the program by emitting empty inline assembly that is marked as
404 * having side effects.
406 * Optionally, a value can be passed through the inline assembly to prevent
407 * LLVM from hoisting calls to ReadNone functions.
410 ac_build_optimization_barrier(struct ac_llvm_context
*ctx
,
413 static int counter
= 0;
415 LLVMBuilderRef builder
= ctx
->builder
;
418 snprintf(code
, sizeof(code
), "; %d", p_atomic_inc_return(&counter
));
421 LLVMTypeRef ftype
= LLVMFunctionType(ctx
->voidt
, NULL
, 0, false);
422 LLVMValueRef inlineasm
= LLVMConstInlineAsm(ftype
, code
, "", true, false);
423 LLVMBuildCall(builder
, inlineasm
, NULL
, 0, "");
425 LLVMTypeRef ftype
= LLVMFunctionType(ctx
->i32
, &ctx
->i32
, 1, false);
426 LLVMValueRef inlineasm
= LLVMConstInlineAsm(ftype
, code
, "=v,0", true, false);
427 LLVMValueRef vgpr
= *pvgpr
;
428 LLVMTypeRef vgpr_type
= LLVMTypeOf(vgpr
);
429 unsigned vgpr_size
= ac_get_type_size(vgpr_type
);
432 assert(vgpr_size
% 4 == 0);
434 vgpr
= LLVMBuildBitCast(builder
, vgpr
, LLVMVectorType(ctx
->i32
, vgpr_size
/ 4), "");
435 vgpr0
= LLVMBuildExtractElement(builder
, vgpr
, ctx
->i32_0
, "");
436 vgpr0
= LLVMBuildCall(builder
, inlineasm
, &vgpr0
, 1, "");
437 vgpr
= LLVMBuildInsertElement(builder
, vgpr
, vgpr0
, ctx
->i32_0
, "");
438 vgpr
= LLVMBuildBitCast(builder
, vgpr
, vgpr_type
, "");
445 ac_build_shader_clock(struct ac_llvm_context
*ctx
)
447 const char *intr
= LLVM_VERSION_MAJOR
>= 9 && ctx
->chip_class
>= GFX8
?
448 "llvm.amdgcn.s.memrealtime" : "llvm.readcyclecounter";
449 LLVMValueRef tmp
= ac_build_intrinsic(ctx
, intr
, ctx
->i64
, NULL
, 0, 0);
450 return LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->v2i32
, "");
454 ac_build_ballot(struct ac_llvm_context
*ctx
,
459 if (LLVM_VERSION_MAJOR
>= 9) {
460 if (ctx
->wave_size
== 64)
461 name
= "llvm.amdgcn.icmp.i64.i32";
463 name
= "llvm.amdgcn.icmp.i32.i32";
465 name
= "llvm.amdgcn.icmp.i32";
467 LLVMValueRef args
[3] = {
470 LLVMConstInt(ctx
->i32
, LLVMIntNE
, 0)
473 /* We currently have no other way to prevent LLVM from lifting the icmp
474 * calls to a dominating basic block.
476 ac_build_optimization_barrier(ctx
, &args
[0]);
478 args
[0] = ac_to_integer(ctx
, args
[0]);
480 return ac_build_intrinsic(ctx
, name
, ctx
->iN_wavemask
, args
, 3,
481 AC_FUNC_ATTR_NOUNWIND
|
482 AC_FUNC_ATTR_READNONE
|
483 AC_FUNC_ATTR_CONVERGENT
);
486 LLVMValueRef
ac_get_i1_sgpr_mask(struct ac_llvm_context
*ctx
,
489 const char *name
= LLVM_VERSION_MAJOR
>= 9 ? "llvm.amdgcn.icmp.i64.i1" : "llvm.amdgcn.icmp.i1";
490 LLVMValueRef args
[3] = {
493 LLVMConstInt(ctx
->i32
, LLVMIntNE
, 0),
496 return ac_build_intrinsic(ctx
, name
, ctx
->i64
, args
, 3,
497 AC_FUNC_ATTR_NOUNWIND
|
498 AC_FUNC_ATTR_READNONE
|
499 AC_FUNC_ATTR_CONVERGENT
);
503 ac_build_vote_all(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
505 LLVMValueRef active_set
= ac_build_ballot(ctx
, ctx
->i32_1
);
506 LLVMValueRef vote_set
= ac_build_ballot(ctx
, value
);
507 return LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, vote_set
, active_set
, "");
511 ac_build_vote_any(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
513 LLVMValueRef vote_set
= ac_build_ballot(ctx
, value
);
514 return LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, vote_set
,
515 LLVMConstInt(ctx
->iN_wavemask
, 0, 0), "");
519 ac_build_vote_eq(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
521 LLVMValueRef active_set
= ac_build_ballot(ctx
, ctx
->i32_1
);
522 LLVMValueRef vote_set
= ac_build_ballot(ctx
, value
);
524 LLVMValueRef all
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
525 vote_set
, active_set
, "");
526 LLVMValueRef none
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
528 LLVMConstInt(ctx
->iN_wavemask
, 0, 0), "");
529 return LLVMBuildOr(ctx
->builder
, all
, none
, "");
533 ac_build_varying_gather_values(struct ac_llvm_context
*ctx
, LLVMValueRef
*values
,
534 unsigned value_count
, unsigned component
)
536 LLVMValueRef vec
= NULL
;
538 if (value_count
== 1) {
539 return values
[component
];
540 } else if (!value_count
)
541 unreachable("value_count is 0");
543 for (unsigned i
= component
; i
< value_count
+ component
; i
++) {
544 LLVMValueRef value
= values
[i
];
547 vec
= LLVMGetUndef( LLVMVectorType(LLVMTypeOf(value
), value_count
));
548 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, i
- component
, false);
549 vec
= LLVMBuildInsertElement(ctx
->builder
, vec
, value
, index
, "");
555 ac_build_gather_values_extended(struct ac_llvm_context
*ctx
,
556 LLVMValueRef
*values
,
557 unsigned value_count
,
558 unsigned value_stride
,
562 LLVMBuilderRef builder
= ctx
->builder
;
563 LLVMValueRef vec
= NULL
;
566 if (value_count
== 1 && !always_vector
) {
568 return LLVMBuildLoad(builder
, values
[0], "");
570 } else if (!value_count
)
571 unreachable("value_count is 0");
573 for (i
= 0; i
< value_count
; i
++) {
574 LLVMValueRef value
= values
[i
* value_stride
];
576 value
= LLVMBuildLoad(builder
, value
, "");
579 vec
= LLVMGetUndef( LLVMVectorType(LLVMTypeOf(value
), value_count
));
580 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, i
, false);
581 vec
= LLVMBuildInsertElement(builder
, vec
, value
, index
, "");
587 ac_build_gather_values(struct ac_llvm_context
*ctx
,
588 LLVMValueRef
*values
,
589 unsigned value_count
)
591 return ac_build_gather_values_extended(ctx
, values
, value_count
, 1, false, false);
594 /* Expand a scalar or vector to <dst_channels x type> by filling the remaining
595 * channels with undef. Extract at most src_channels components from the input.
598 ac_build_expand(struct ac_llvm_context
*ctx
,
600 unsigned src_channels
,
601 unsigned dst_channels
)
603 LLVMTypeRef elemtype
;
604 LLVMValueRef chan
[dst_channels
];
606 if (LLVMGetTypeKind(LLVMTypeOf(value
)) == LLVMVectorTypeKind
) {
607 unsigned vec_size
= LLVMGetVectorSize(LLVMTypeOf(value
));
609 if (src_channels
== dst_channels
&& vec_size
== dst_channels
)
612 src_channels
= MIN2(src_channels
, vec_size
);
614 for (unsigned i
= 0; i
< src_channels
; i
++)
615 chan
[i
] = ac_llvm_extract_elem(ctx
, value
, i
);
617 elemtype
= LLVMGetElementType(LLVMTypeOf(value
));
620 assert(src_channels
== 1);
623 elemtype
= LLVMTypeOf(value
);
626 for (unsigned i
= src_channels
; i
< dst_channels
; i
++)
627 chan
[i
] = LLVMGetUndef(elemtype
);
629 return ac_build_gather_values(ctx
, chan
, dst_channels
);
632 /* Extract components [start, start + channels) from a vector.
635 ac_extract_components(struct ac_llvm_context
*ctx
,
640 LLVMValueRef chan
[channels
];
642 for (unsigned i
= 0; i
< channels
; i
++)
643 chan
[i
] = ac_llvm_extract_elem(ctx
, value
, i
+ start
);
645 return ac_build_gather_values(ctx
, chan
, channels
);
648 /* Expand a scalar or vector to <4 x type> by filling the remaining channels
649 * with undef. Extract at most num_channels components from the input.
651 LLVMValueRef
ac_build_expand_to_vec4(struct ac_llvm_context
*ctx
,
653 unsigned num_channels
)
655 return ac_build_expand(ctx
, value
, num_channels
, 4);
658 LLVMValueRef
ac_build_round(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
660 unsigned type_size
= ac_get_type_size(LLVMTypeOf(value
));
664 name
= "llvm.rint.f16";
665 else if (type_size
== 4)
666 name
= "llvm.rint.f32";
668 name
= "llvm.rint.f64";
670 return ac_build_intrinsic(ctx
, name
, LLVMTypeOf(value
), &value
, 1,
671 AC_FUNC_ATTR_READNONE
);
675 ac_build_fdiv(struct ac_llvm_context
*ctx
,
679 /* If we do (num / den), LLVM >= 7.0 does:
680 * return num * v_rcp_f32(den * (fabs(den) > 0x1.0p+96f ? 0x1.0p-32f : 1.0f));
682 * If we do (num * (1 / den)), LLVM does:
683 * return num * v_rcp_f32(den);
685 LLVMValueRef one
= LLVMConstReal(LLVMTypeOf(num
), 1.0);
686 LLVMValueRef rcp
= LLVMBuildFDiv(ctx
->builder
, one
, den
, "");
687 LLVMValueRef ret
= LLVMBuildFMul(ctx
->builder
, num
, rcp
, "");
689 /* Use v_rcp_f32 instead of precise division. */
690 if (!LLVMIsConstant(ret
))
691 LLVMSetMetadata(ret
, ctx
->fpmath_md_kind
, ctx
->fpmath_md_2p5_ulp
);
695 /* See fast_idiv_by_const.h. */
696 /* Set: increment = util_fast_udiv_info::increment ? multiplier : 0; */
697 LLVMValueRef
ac_build_fast_udiv(struct ac_llvm_context
*ctx
,
699 LLVMValueRef multiplier
,
700 LLVMValueRef pre_shift
,
701 LLVMValueRef post_shift
,
702 LLVMValueRef increment
)
704 LLVMBuilderRef builder
= ctx
->builder
;
706 num
= LLVMBuildLShr(builder
, num
, pre_shift
, "");
707 num
= LLVMBuildMul(builder
,
708 LLVMBuildZExt(builder
, num
, ctx
->i64
, ""),
709 LLVMBuildZExt(builder
, multiplier
, ctx
->i64
, ""), "");
710 num
= LLVMBuildAdd(builder
, num
,
711 LLVMBuildZExt(builder
, increment
, ctx
->i64
, ""), "");
712 num
= LLVMBuildLShr(builder
, num
, LLVMConstInt(ctx
->i64
, 32, 0), "");
713 num
= LLVMBuildTrunc(builder
, num
, ctx
->i32
, "");
714 return LLVMBuildLShr(builder
, num
, post_shift
, "");
717 /* See fast_idiv_by_const.h. */
718 /* If num != UINT_MAX, this more efficient version can be used. */
719 /* Set: increment = util_fast_udiv_info::increment; */
720 LLVMValueRef
ac_build_fast_udiv_nuw(struct ac_llvm_context
*ctx
,
722 LLVMValueRef multiplier
,
723 LLVMValueRef pre_shift
,
724 LLVMValueRef post_shift
,
725 LLVMValueRef increment
)
727 LLVMBuilderRef builder
= ctx
->builder
;
729 num
= LLVMBuildLShr(builder
, num
, pre_shift
, "");
730 num
= LLVMBuildNUWAdd(builder
, num
, increment
, "");
731 num
= LLVMBuildMul(builder
,
732 LLVMBuildZExt(builder
, num
, ctx
->i64
, ""),
733 LLVMBuildZExt(builder
, multiplier
, ctx
->i64
, ""), "");
734 num
= LLVMBuildLShr(builder
, num
, LLVMConstInt(ctx
->i64
, 32, 0), "");
735 num
= LLVMBuildTrunc(builder
, num
, ctx
->i32
, "");
736 return LLVMBuildLShr(builder
, num
, post_shift
, "");
739 /* See fast_idiv_by_const.h. */
740 /* Both operands must fit in 31 bits and the divisor must not be 1. */
741 LLVMValueRef
ac_build_fast_udiv_u31_d_not_one(struct ac_llvm_context
*ctx
,
743 LLVMValueRef multiplier
,
744 LLVMValueRef post_shift
)
746 LLVMBuilderRef builder
= ctx
->builder
;
748 num
= LLVMBuildMul(builder
,
749 LLVMBuildZExt(builder
, num
, ctx
->i64
, ""),
750 LLVMBuildZExt(builder
, multiplier
, ctx
->i64
, ""), "");
751 num
= LLVMBuildLShr(builder
, num
, LLVMConstInt(ctx
->i64
, 32, 0), "");
752 num
= LLVMBuildTrunc(builder
, num
, ctx
->i32
, "");
753 return LLVMBuildLShr(builder
, num
, post_shift
, "");
756 /* Coordinates for cube map selection. sc, tc, and ma are as in Table 8.27
757 * of the OpenGL 4.5 (Compatibility Profile) specification, except ma is
758 * already multiplied by two. id is the cube face number.
760 struct cube_selection_coords
{
767 build_cube_intrinsic(struct ac_llvm_context
*ctx
,
769 struct cube_selection_coords
*out
)
771 LLVMTypeRef f32
= ctx
->f32
;
773 out
->stc
[1] = ac_build_intrinsic(ctx
, "llvm.amdgcn.cubetc",
774 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
775 out
->stc
[0] = ac_build_intrinsic(ctx
, "llvm.amdgcn.cubesc",
776 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
777 out
->ma
= ac_build_intrinsic(ctx
, "llvm.amdgcn.cubema",
778 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
779 out
->id
= ac_build_intrinsic(ctx
, "llvm.amdgcn.cubeid",
780 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
784 * Build a manual selection sequence for cube face sc/tc coordinates and
785 * major axis vector (multiplied by 2 for consistency) for the given
786 * vec3 \p coords, for the face implied by \p selcoords.
788 * For the major axis, we always adjust the sign to be in the direction of
789 * selcoords.ma; i.e., a positive out_ma means that coords is pointed towards
790 * the selcoords major axis.
792 static void build_cube_select(struct ac_llvm_context
*ctx
,
793 const struct cube_selection_coords
*selcoords
,
794 const LLVMValueRef
*coords
,
795 LLVMValueRef
*out_st
,
796 LLVMValueRef
*out_ma
)
798 LLVMBuilderRef builder
= ctx
->builder
;
799 LLVMTypeRef f32
= LLVMTypeOf(coords
[0]);
800 LLVMValueRef is_ma_positive
;
802 LLVMValueRef is_ma_z
, is_not_ma_z
;
803 LLVMValueRef is_ma_y
;
804 LLVMValueRef is_ma_x
;
808 is_ma_positive
= LLVMBuildFCmp(builder
, LLVMRealUGE
,
809 selcoords
->ma
, LLVMConstReal(f32
, 0.0), "");
810 sgn_ma
= LLVMBuildSelect(builder
, is_ma_positive
,
811 LLVMConstReal(f32
, 1.0), LLVMConstReal(f32
, -1.0), "");
813 is_ma_z
= LLVMBuildFCmp(builder
, LLVMRealUGE
, selcoords
->id
, LLVMConstReal(f32
, 4.0), "");
814 is_not_ma_z
= LLVMBuildNot(builder
, is_ma_z
, "");
815 is_ma_y
= LLVMBuildAnd(builder
, is_not_ma_z
,
816 LLVMBuildFCmp(builder
, LLVMRealUGE
, selcoords
->id
, LLVMConstReal(f32
, 2.0), ""), "");
817 is_ma_x
= LLVMBuildAnd(builder
, is_not_ma_z
, LLVMBuildNot(builder
, is_ma_y
, ""), "");
820 tmp
= LLVMBuildSelect(builder
, is_ma_x
, coords
[2], coords
[0], "");
821 sgn
= LLVMBuildSelect(builder
, is_ma_y
, LLVMConstReal(f32
, 1.0),
822 LLVMBuildSelect(builder
, is_ma_z
, sgn_ma
,
823 LLVMBuildFNeg(builder
, sgn_ma
, ""), ""), "");
824 out_st
[0] = LLVMBuildFMul(builder
, tmp
, sgn
, "");
827 tmp
= LLVMBuildSelect(builder
, is_ma_y
, coords
[2], coords
[1], "");
828 sgn
= LLVMBuildSelect(builder
, is_ma_y
, sgn_ma
,
829 LLVMConstReal(f32
, -1.0), "");
830 out_st
[1] = LLVMBuildFMul(builder
, tmp
, sgn
, "");
833 tmp
= LLVMBuildSelect(builder
, is_ma_z
, coords
[2],
834 LLVMBuildSelect(builder
, is_ma_y
, coords
[1], coords
[0], ""), "");
835 tmp
= ac_build_intrinsic(ctx
, "llvm.fabs.f32",
836 ctx
->f32
, &tmp
, 1, AC_FUNC_ATTR_READNONE
);
837 *out_ma
= LLVMBuildFMul(builder
, tmp
, LLVMConstReal(f32
, 2.0), "");
841 ac_prepare_cube_coords(struct ac_llvm_context
*ctx
,
842 bool is_deriv
, bool is_array
, bool is_lod
,
843 LLVMValueRef
*coords_arg
,
844 LLVMValueRef
*derivs_arg
)
847 LLVMBuilderRef builder
= ctx
->builder
;
848 struct cube_selection_coords selcoords
;
849 LLVMValueRef coords
[3];
852 if (is_array
&& !is_lod
) {
853 LLVMValueRef tmp
= ac_build_round(ctx
, coords_arg
[3]);
855 /* Section 8.9 (Texture Functions) of the GLSL 4.50 spec says:
857 * "For Array forms, the array layer used will be
859 * max(0, min(d−1, floor(layer+0.5)))
861 * where d is the depth of the texture array and layer
862 * comes from the component indicated in the tables below.
863 * Workaroudn for an issue where the layer is taken from a
864 * helper invocation which happens to fall on a different
865 * layer due to extrapolation."
867 * GFX8 and earlier attempt to implement this in hardware by
868 * clamping the value of coords[2] = (8 * layer) + face.
869 * Unfortunately, this means that the we end up with the wrong
870 * face when clamping occurs.
872 * Clamp the layer earlier to work around the issue.
874 if (ctx
->chip_class
<= GFX8
) {
876 ge0
= LLVMBuildFCmp(builder
, LLVMRealOGE
, tmp
, ctx
->f32_0
, "");
877 tmp
= LLVMBuildSelect(builder
, ge0
, tmp
, ctx
->f32_0
, "");
883 build_cube_intrinsic(ctx
, coords_arg
, &selcoords
);
885 invma
= ac_build_intrinsic(ctx
, "llvm.fabs.f32",
886 ctx
->f32
, &selcoords
.ma
, 1, AC_FUNC_ATTR_READNONE
);
887 invma
= ac_build_fdiv(ctx
, LLVMConstReal(ctx
->f32
, 1.0), invma
);
889 for (int i
= 0; i
< 2; ++i
)
890 coords
[i
] = LLVMBuildFMul(builder
, selcoords
.stc
[i
], invma
, "");
892 coords
[2] = selcoords
.id
;
894 if (is_deriv
&& derivs_arg
) {
895 LLVMValueRef derivs
[4];
898 /* Convert cube derivatives to 2D derivatives. */
899 for (axis
= 0; axis
< 2; axis
++) {
900 LLVMValueRef deriv_st
[2];
901 LLVMValueRef deriv_ma
;
903 /* Transform the derivative alongside the texture
904 * coordinate. Mathematically, the correct formula is
905 * as follows. Assume we're projecting onto the +Z face
906 * and denote by dx/dh the derivative of the (original)
907 * X texture coordinate with respect to horizontal
908 * window coordinates. The projection onto the +Z face
913 * Then df/dh = df/dx * dx/dh + df/dz * dz/dh
914 * = 1/z * dx/dh - x/z * 1/z * dz/dh.
916 * This motivatives the implementation below.
918 * Whether this actually gives the expected results for
919 * apps that might feed in derivatives obtained via
920 * finite differences is anyone's guess. The OpenGL spec
921 * seems awfully quiet about how textureGrad for cube
922 * maps should be handled.
924 build_cube_select(ctx
, &selcoords
, &derivs_arg
[axis
* 3],
925 deriv_st
, &deriv_ma
);
927 deriv_ma
= LLVMBuildFMul(builder
, deriv_ma
, invma
, "");
929 for (int i
= 0; i
< 2; ++i
)
930 derivs
[axis
* 2 + i
] =
931 LLVMBuildFSub(builder
,
932 LLVMBuildFMul(builder
, deriv_st
[i
], invma
, ""),
933 LLVMBuildFMul(builder
, deriv_ma
, coords
[i
], ""), "");
936 memcpy(derivs_arg
, derivs
, sizeof(derivs
));
939 /* Shift the texture coordinate. This must be applied after the
940 * derivative calculation.
942 for (int i
= 0; i
< 2; ++i
)
943 coords
[i
] = LLVMBuildFAdd(builder
, coords
[i
], LLVMConstReal(ctx
->f32
, 1.5), "");
946 /* for cube arrays coord.z = coord.w(array_index) * 8 + face */
947 /* coords_arg.w component - array_index for cube arrays */
948 coords
[2] = ac_build_fmad(ctx
, coords_arg
[3], LLVMConstReal(ctx
->f32
, 8.0), coords
[2]);
951 memcpy(coords_arg
, coords
, sizeof(coords
));
956 ac_build_fs_interp(struct ac_llvm_context
*ctx
,
957 LLVMValueRef llvm_chan
,
958 LLVMValueRef attr_number
,
963 LLVMValueRef args
[5];
968 args
[2] = attr_number
;
971 p1
= ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p1",
972 ctx
->f32
, args
, 4, AC_FUNC_ATTR_READNONE
);
977 args
[3] = attr_number
;
980 return ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p2",
981 ctx
->f32
, args
, 5, AC_FUNC_ATTR_READNONE
);
985 ac_build_fs_interp_f16(struct ac_llvm_context
*ctx
,
986 LLVMValueRef llvm_chan
,
987 LLVMValueRef attr_number
,
992 LLVMValueRef args
[6];
997 args
[2] = attr_number
;
998 args
[3] = ctx
->i1false
;
1001 p1
= ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p1.f16",
1002 ctx
->f32
, args
, 5, AC_FUNC_ATTR_READNONE
);
1006 args
[2] = llvm_chan
;
1007 args
[3] = attr_number
;
1008 args
[4] = ctx
->i1false
;
1011 return ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p2.f16",
1012 ctx
->f16
, args
, 6, AC_FUNC_ATTR_READNONE
);
1016 ac_build_fs_interp_mov(struct ac_llvm_context
*ctx
,
1017 LLVMValueRef parameter
,
1018 LLVMValueRef llvm_chan
,
1019 LLVMValueRef attr_number
,
1020 LLVMValueRef params
)
1022 LLVMValueRef args
[4];
1024 args
[0] = parameter
;
1025 args
[1] = llvm_chan
;
1026 args
[2] = attr_number
;
1029 return ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.mov",
1030 ctx
->f32
, args
, 4, AC_FUNC_ATTR_READNONE
);
1034 ac_build_gep_ptr(struct ac_llvm_context
*ctx
,
1035 LLVMValueRef base_ptr
,
1038 return LLVMBuildGEP(ctx
->builder
, base_ptr
, &index
, 1, "");
1042 ac_build_gep0(struct ac_llvm_context
*ctx
,
1043 LLVMValueRef base_ptr
,
1046 LLVMValueRef indices
[2] = {
1050 return LLVMBuildGEP(ctx
->builder
, base_ptr
, indices
, 2, "");
1053 LLVMValueRef
ac_build_pointer_add(struct ac_llvm_context
*ctx
, LLVMValueRef ptr
,
1056 return LLVMBuildPointerCast(ctx
->builder
,
1057 LLVMBuildGEP(ctx
->builder
, ptr
, &index
, 1, ""),
1058 LLVMTypeOf(ptr
), "");
1062 ac_build_indexed_store(struct ac_llvm_context
*ctx
,
1063 LLVMValueRef base_ptr
, LLVMValueRef index
,
1066 LLVMBuildStore(ctx
->builder
, value
,
1067 ac_build_gep0(ctx
, base_ptr
, index
));
1071 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad.
1072 * It's equivalent to doing a load from &base_ptr[index].
1074 * \param base_ptr Where the array starts.
1075 * \param index The element index into the array.
1076 * \param uniform Whether the base_ptr and index can be assumed to be
1077 * dynamically uniform (i.e. load to an SGPR)
1078 * \param invariant Whether the load is invariant (no other opcodes affect it)
1079 * \param no_unsigned_wraparound
1080 * For all possible re-associations and re-distributions of an expression
1081 * "base_ptr + index * elemsize" into "addr + offset" (excluding GEPs
1082 * without inbounds in base_ptr), this parameter is true if "addr + offset"
1083 * does not result in an unsigned integer wraparound. This is used for
1084 * optimal code generation of 32-bit pointer arithmetic.
1086 * For example, a 32-bit immediate offset that causes a 32-bit unsigned
1087 * integer wraparound can't be an imm offset in s_load_dword, because
1088 * the instruction performs "addr + offset" in 64 bits.
1090 * Expected usage for bindless textures by chaining GEPs:
1091 * // possible unsigned wraparound, don't use InBounds:
1092 * ptr1 = LLVMBuildGEP(base_ptr, index);
1093 * image = load(ptr1); // becomes "s_load ptr1, 0"
1095 * ptr2 = LLVMBuildInBoundsGEP(ptr1, 32 / elemsize);
1096 * sampler = load(ptr2); // becomes "s_load ptr1, 32" thanks to InBounds
1099 ac_build_load_custom(struct ac_llvm_context
*ctx
, LLVMValueRef base_ptr
,
1100 LLVMValueRef index
, bool uniform
, bool invariant
,
1101 bool no_unsigned_wraparound
)
1103 LLVMValueRef pointer
, result
;
1105 if (no_unsigned_wraparound
&&
1106 LLVMGetPointerAddressSpace(LLVMTypeOf(base_ptr
)) == AC_ADDR_SPACE_CONST_32BIT
)
1107 pointer
= LLVMBuildInBoundsGEP(ctx
->builder
, base_ptr
, &index
, 1, "");
1109 pointer
= LLVMBuildGEP(ctx
->builder
, base_ptr
, &index
, 1, "");
1112 LLVMSetMetadata(pointer
, ctx
->uniform_md_kind
, ctx
->empty_md
);
1113 result
= LLVMBuildLoad(ctx
->builder
, pointer
, "");
1115 LLVMSetMetadata(result
, ctx
->invariant_load_md_kind
, ctx
->empty_md
);
1119 LLVMValueRef
ac_build_load(struct ac_llvm_context
*ctx
, LLVMValueRef base_ptr
,
1122 return ac_build_load_custom(ctx
, base_ptr
, index
, false, false, false);
1125 LLVMValueRef
ac_build_load_invariant(struct ac_llvm_context
*ctx
,
1126 LLVMValueRef base_ptr
, LLVMValueRef index
)
1128 return ac_build_load_custom(ctx
, base_ptr
, index
, false, true, false);
1131 /* This assumes that there is no unsigned integer wraparound during the address
1132 * computation, excluding all GEPs within base_ptr. */
1133 LLVMValueRef
ac_build_load_to_sgpr(struct ac_llvm_context
*ctx
,
1134 LLVMValueRef base_ptr
, LLVMValueRef index
)
1136 return ac_build_load_custom(ctx
, base_ptr
, index
, true, true, true);
1139 /* See ac_build_load_custom() documentation. */
1140 LLVMValueRef
ac_build_load_to_sgpr_uint_wraparound(struct ac_llvm_context
*ctx
,
1141 LLVMValueRef base_ptr
, LLVMValueRef index
)
1143 return ac_build_load_custom(ctx
, base_ptr
, index
, true, true, false);
1146 static unsigned get_load_cache_policy(struct ac_llvm_context
*ctx
,
1147 unsigned cache_policy
)
1149 return cache_policy
|
1150 (ctx
->chip_class
>= GFX10
&& cache_policy
& ac_glc
? ac_dlc
: 0);
1154 ac_build_buffer_store_common(struct ac_llvm_context
*ctx
,
1157 LLVMValueRef vindex
,
1158 LLVMValueRef voffset
,
1159 LLVMValueRef soffset
,
1160 unsigned num_channels
,
1161 LLVMTypeRef return_channel_type
,
1162 unsigned cache_policy
,
1166 LLVMValueRef args
[6];
1169 args
[idx
++] = LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, "");
1171 args
[idx
++] = vindex
? vindex
: ctx
->i32_0
;
1172 args
[idx
++] = voffset
? voffset
: ctx
->i32_0
;
1173 args
[idx
++] = soffset
? soffset
: ctx
->i32_0
;
1174 args
[idx
++] = LLVMConstInt(ctx
->i32
, cache_policy
, 0);
1175 unsigned func
= !ac_has_vec3_support(ctx
->chip_class
, use_format
) && num_channels
== 3 ? 4 : num_channels
;
1176 const char *indexing_kind
= structurized
? "struct" : "raw";
1177 char name
[256], type_name
[8];
1179 LLVMTypeRef type
= func
> 1 ? LLVMVectorType(return_channel_type
, func
) : return_channel_type
;
1180 ac_build_type_name_for_intr(type
, type_name
, sizeof(type_name
));
1183 snprintf(name
, sizeof(name
), "llvm.amdgcn.%s.buffer.store.format.%s",
1184 indexing_kind
, type_name
);
1186 snprintf(name
, sizeof(name
), "llvm.amdgcn.%s.buffer.store.%s",
1187 indexing_kind
, type_name
);
1190 ac_build_intrinsic(ctx
, name
, ctx
->voidt
, args
, idx
,
1191 AC_FUNC_ATTR_INACCESSIBLE_MEM_ONLY
);
1195 ac_build_buffer_store_format(struct ac_llvm_context
*ctx
,
1198 LLVMValueRef vindex
,
1199 LLVMValueRef voffset
,
1200 unsigned num_channels
,
1201 unsigned cache_policy
)
1203 ac_build_buffer_store_common(ctx
, rsrc
, data
, vindex
,
1204 voffset
, NULL
, num_channels
,
1205 ctx
->f32
, cache_policy
,
1209 /* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
1210 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
1211 * or v4i32 (num_channels=3,4).
1214 ac_build_buffer_store_dword(struct ac_llvm_context
*ctx
,
1217 unsigned num_channels
,
1218 LLVMValueRef voffset
,
1219 LLVMValueRef soffset
,
1220 unsigned inst_offset
,
1221 unsigned cache_policy
,
1222 bool swizzle_enable_hint
)
1224 /* Split 3 channel stores, because only LLVM 9+ support 3-channel
1226 if (num_channels
== 3 && !ac_has_vec3_support(ctx
->chip_class
, false)) {
1227 LLVMValueRef v
[3], v01
;
1229 for (int i
= 0; i
< 3; i
++) {
1230 v
[i
] = LLVMBuildExtractElement(ctx
->builder
, vdata
,
1231 LLVMConstInt(ctx
->i32
, i
, 0), "");
1233 v01
= ac_build_gather_values(ctx
, v
, 2);
1235 ac_build_buffer_store_dword(ctx
, rsrc
, v01
, 2, voffset
,
1236 soffset
, inst_offset
, cache_policy
,
1237 swizzle_enable_hint
);
1238 ac_build_buffer_store_dword(ctx
, rsrc
, v
[2], 1, voffset
,
1239 soffset
, inst_offset
+ 8,
1241 swizzle_enable_hint
);
1245 /* SWIZZLE_ENABLE requires that soffset isn't folded into voffset
1246 * (voffset is swizzled, but soffset isn't swizzled).
1247 * llvm.amdgcn.buffer.store doesn't have a separate soffset parameter.
1249 if (!swizzle_enable_hint
) {
1250 LLVMValueRef offset
= soffset
;
1253 offset
= LLVMBuildAdd(ctx
->builder
, offset
,
1254 LLVMConstInt(ctx
->i32
, inst_offset
, 0), "");
1256 ac_build_buffer_store_common(ctx
, rsrc
, ac_to_float(ctx
, vdata
),
1257 ctx
->i32_0
, voffset
, offset
,
1258 num_channels
, ctx
->f32
,
1259 cache_policy
, false, false);
1263 static const unsigned dfmts
[] = {
1264 V_008F0C_BUF_DATA_FORMAT_32
,
1265 V_008F0C_BUF_DATA_FORMAT_32_32
,
1266 V_008F0C_BUF_DATA_FORMAT_32_32_32
,
1267 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
1269 unsigned dfmt
= dfmts
[num_channels
- 1];
1270 unsigned nfmt
= V_008F0C_BUF_NUM_FORMAT_UINT
;
1271 LLVMValueRef immoffset
= LLVMConstInt(ctx
->i32
, inst_offset
, 0);
1273 ac_build_raw_tbuffer_store(ctx
, rsrc
, vdata
, voffset
, soffset
,
1274 immoffset
, num_channels
, dfmt
, nfmt
, cache_policy
);
1278 ac_build_buffer_load_common(struct ac_llvm_context
*ctx
,
1280 LLVMValueRef vindex
,
1281 LLVMValueRef voffset
,
1282 LLVMValueRef soffset
,
1283 unsigned num_channels
,
1284 LLVMTypeRef channel_type
,
1285 unsigned cache_policy
,
1290 LLVMValueRef args
[5];
1292 args
[idx
++] = LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, "");
1294 args
[idx
++] = vindex
? vindex
: ctx
->i32_0
;
1295 args
[idx
++] = voffset
? voffset
: ctx
->i32_0
;
1296 args
[idx
++] = soffset
? soffset
: ctx
->i32_0
;
1297 args
[idx
++] = LLVMConstInt(ctx
->i32
, get_load_cache_policy(ctx
, cache_policy
), 0);
1298 unsigned func
= !ac_has_vec3_support(ctx
->chip_class
, use_format
) && num_channels
== 3 ? 4 : num_channels
;
1299 const char *indexing_kind
= structurized
? "struct" : "raw";
1300 char name
[256], type_name
[8];
1302 LLVMTypeRef type
= func
> 1 ? LLVMVectorType(channel_type
, func
) : channel_type
;
1303 ac_build_type_name_for_intr(type
, type_name
, sizeof(type_name
));
1306 snprintf(name
, sizeof(name
), "llvm.amdgcn.%s.buffer.load.format.%s",
1307 indexing_kind
, type_name
);
1309 snprintf(name
, sizeof(name
), "llvm.amdgcn.%s.buffer.load.%s",
1310 indexing_kind
, type_name
);
1313 return ac_build_intrinsic(ctx
, name
, type
, args
, idx
,
1314 ac_get_load_intr_attribs(can_speculate
));
1318 ac_build_buffer_load(struct ac_llvm_context
*ctx
,
1321 LLVMValueRef vindex
,
1322 LLVMValueRef voffset
,
1323 LLVMValueRef soffset
,
1324 unsigned inst_offset
,
1325 unsigned cache_policy
,
1329 LLVMValueRef offset
= LLVMConstInt(ctx
->i32
, inst_offset
, 0);
1331 offset
= LLVMBuildAdd(ctx
->builder
, offset
, voffset
, "");
1333 offset
= LLVMBuildAdd(ctx
->builder
, offset
, soffset
, "");
1335 if (allow_smem
&& !(cache_policy
& ac_slc
) &&
1336 (!(cache_policy
& ac_glc
) || ctx
->chip_class
>= GFX8
)) {
1337 assert(vindex
== NULL
);
1339 LLVMValueRef result
[8];
1341 for (int i
= 0; i
< num_channels
; i
++) {
1343 offset
= LLVMBuildAdd(ctx
->builder
, offset
,
1344 LLVMConstInt(ctx
->i32
, 4, 0), "");
1346 LLVMValueRef args
[3] = {
1349 LLVMConstInt(ctx
->i32
, get_load_cache_policy(ctx
, cache_policy
), 0),
1351 result
[i
] = ac_build_intrinsic(ctx
,
1352 "llvm.amdgcn.s.buffer.load.f32",
1354 AC_FUNC_ATTR_READNONE
);
1356 if (num_channels
== 1)
1359 if (num_channels
== 3 && !ac_has_vec3_support(ctx
->chip_class
, false))
1360 result
[num_channels
++] = LLVMGetUndef(ctx
->f32
);
1361 return ac_build_gather_values(ctx
, result
, num_channels
);
1364 return ac_build_buffer_load_common(ctx
, rsrc
, vindex
,
1366 num_channels
, ctx
->f32
,
1368 can_speculate
, false, false);
1371 LLVMValueRef
ac_build_buffer_load_format(struct ac_llvm_context
*ctx
,
1373 LLVMValueRef vindex
,
1374 LLVMValueRef voffset
,
1375 unsigned num_channels
,
1376 unsigned cache_policy
,
1379 return ac_build_buffer_load_common(ctx
, rsrc
, vindex
, voffset
,
1380 ctx
->i32_0
, num_channels
, ctx
->f32
,
1381 cache_policy
, can_speculate
,
1385 /// Translate a (dfmt, nfmt) pair into a chip-appropriate combined format
1386 /// value for LLVM8+ tbuffer intrinsics.
1388 ac_get_tbuffer_format(struct ac_llvm_context
*ctx
,
1389 unsigned dfmt
, unsigned nfmt
)
1391 if (ctx
->chip_class
>= GFX10
) {
1394 default: unreachable("bad dfmt");
1395 case V_008F0C_BUF_DATA_FORMAT_INVALID
: format
= V_008F0C_IMG_FORMAT_INVALID
; break;
1396 case V_008F0C_BUF_DATA_FORMAT_8
: format
= V_008F0C_IMG_FORMAT_8_UINT
; break;
1397 case V_008F0C_BUF_DATA_FORMAT_8_8
: format
= V_008F0C_IMG_FORMAT_8_8_UINT
; break;
1398 case V_008F0C_BUF_DATA_FORMAT_8_8_8_8
: format
= V_008F0C_IMG_FORMAT_8_8_8_8_UINT
; break;
1399 case V_008F0C_BUF_DATA_FORMAT_16
: format
= V_008F0C_IMG_FORMAT_16_UINT
; break;
1400 case V_008F0C_BUF_DATA_FORMAT_16_16
: format
= V_008F0C_IMG_FORMAT_16_16_UINT
; break;
1401 case V_008F0C_BUF_DATA_FORMAT_16_16_16_16
: format
= V_008F0C_IMG_FORMAT_16_16_16_16_UINT
; break;
1402 case V_008F0C_BUF_DATA_FORMAT_32
: format
= V_008F0C_IMG_FORMAT_32_UINT
; break;
1403 case V_008F0C_BUF_DATA_FORMAT_32_32
: format
= V_008F0C_IMG_FORMAT_32_32_UINT
; break;
1404 case V_008F0C_BUF_DATA_FORMAT_32_32_32
: format
= V_008F0C_IMG_FORMAT_32_32_32_UINT
; break;
1405 case V_008F0C_BUF_DATA_FORMAT_32_32_32_32
: format
= V_008F0C_IMG_FORMAT_32_32_32_32_UINT
; break;
1406 case V_008F0C_BUF_DATA_FORMAT_2_10_10_10
: format
= V_008F0C_IMG_FORMAT_2_10_10_10_UINT
; break;
1409 // Use the regularity properties of the combined format enum.
1411 // Note: float is incompatible with 8-bit data formats,
1412 // [us]{norm,scaled} are incomparible with 32-bit data formats.
1413 // [us]scaled are not writable.
1415 case V_008F0C_BUF_NUM_FORMAT_UNORM
: format
-= 4; break;
1416 case V_008F0C_BUF_NUM_FORMAT_SNORM
: format
-= 3; break;
1417 case V_008F0C_BUF_NUM_FORMAT_USCALED
: format
-= 2; break;
1418 case V_008F0C_BUF_NUM_FORMAT_SSCALED
: format
-= 1; break;
1419 default: unreachable("bad nfmt");
1420 case V_008F0C_BUF_NUM_FORMAT_UINT
: break;
1421 case V_008F0C_BUF_NUM_FORMAT_SINT
: format
+= 1; break;
1422 case V_008F0C_BUF_NUM_FORMAT_FLOAT
: format
+= 2; break;
1427 return dfmt
| (nfmt
<< 4);
1432 ac_build_tbuffer_load(struct ac_llvm_context
*ctx
,
1434 LLVMValueRef vindex
,
1435 LLVMValueRef voffset
,
1436 LLVMValueRef soffset
,
1437 LLVMValueRef immoffset
,
1438 unsigned num_channels
,
1441 unsigned cache_policy
,
1445 voffset
= LLVMBuildAdd(ctx
->builder
, voffset
, immoffset
, "");
1447 LLVMValueRef args
[6];
1449 args
[idx
++] = LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, "");
1451 args
[idx
++] = vindex
? vindex
: ctx
->i32_0
;
1452 args
[idx
++] = voffset
? voffset
: ctx
->i32_0
;
1453 args
[idx
++] = soffset
? soffset
: ctx
->i32_0
;
1454 args
[idx
++] = LLVMConstInt(ctx
->i32
, ac_get_tbuffer_format(ctx
, dfmt
, nfmt
), 0);
1455 args
[idx
++] = LLVMConstInt(ctx
->i32
, get_load_cache_policy(ctx
, cache_policy
), 0);
1456 unsigned func
= !ac_has_vec3_support(ctx
->chip_class
, true) && num_channels
== 3 ? 4 : num_channels
;
1457 const char *indexing_kind
= structurized
? "struct" : "raw";
1458 char name
[256], type_name
[8];
1460 LLVMTypeRef type
= func
> 1 ? LLVMVectorType(ctx
->i32
, func
) : ctx
->i32
;
1461 ac_build_type_name_for_intr(type
, type_name
, sizeof(type_name
));
1463 snprintf(name
, sizeof(name
), "llvm.amdgcn.%s.tbuffer.load.%s",
1464 indexing_kind
, type_name
);
1466 return ac_build_intrinsic(ctx
, name
, type
, args
, idx
,
1467 ac_get_load_intr_attribs(can_speculate
));
1471 ac_build_struct_tbuffer_load(struct ac_llvm_context
*ctx
,
1473 LLVMValueRef vindex
,
1474 LLVMValueRef voffset
,
1475 LLVMValueRef soffset
,
1476 LLVMValueRef immoffset
,
1477 unsigned num_channels
,
1480 unsigned cache_policy
,
1483 return ac_build_tbuffer_load(ctx
, rsrc
, vindex
, voffset
, soffset
,
1484 immoffset
, num_channels
, dfmt
, nfmt
,
1485 cache_policy
, can_speculate
, true);
1489 ac_build_raw_tbuffer_load(struct ac_llvm_context
*ctx
,
1491 LLVMValueRef voffset
,
1492 LLVMValueRef soffset
,
1493 LLVMValueRef immoffset
,
1494 unsigned num_channels
,
1497 unsigned cache_policy
,
1500 return ac_build_tbuffer_load(ctx
, rsrc
, NULL
, voffset
, soffset
,
1501 immoffset
, num_channels
, dfmt
, nfmt
,
1502 cache_policy
, can_speculate
, false);
1506 ac_build_tbuffer_load_short(struct ac_llvm_context
*ctx
,
1508 LLVMValueRef voffset
,
1509 LLVMValueRef soffset
,
1510 LLVMValueRef immoffset
,
1511 unsigned cache_policy
)
1515 if (LLVM_VERSION_MAJOR
>= 9) {
1516 voffset
= LLVMBuildAdd(ctx
->builder
, voffset
, immoffset
, "");
1518 /* LLVM 9+ supports i8/i16 with struct/raw intrinsics. */
1519 res
= ac_build_buffer_load_common(ctx
, rsrc
, NULL
,
1521 1, ctx
->i16
, cache_policy
,
1522 false, false, false);
1524 unsigned dfmt
= V_008F0C_BUF_DATA_FORMAT_16
;
1525 unsigned nfmt
= V_008F0C_BUF_NUM_FORMAT_UINT
;
1527 res
= ac_build_raw_tbuffer_load(ctx
, rsrc
, voffset
, soffset
,
1528 immoffset
, 1, dfmt
, nfmt
, cache_policy
,
1531 res
= LLVMBuildTrunc(ctx
->builder
, res
, ctx
->i16
, "");
1538 ac_build_tbuffer_load_byte(struct ac_llvm_context
*ctx
,
1540 LLVMValueRef voffset
,
1541 LLVMValueRef soffset
,
1542 LLVMValueRef immoffset
,
1543 unsigned cache_policy
)
1547 if (LLVM_VERSION_MAJOR
>= 9) {
1548 voffset
= LLVMBuildAdd(ctx
->builder
, voffset
, immoffset
, "");
1550 /* LLVM 9+ supports i8/i16 with struct/raw intrinsics. */
1551 res
= ac_build_buffer_load_common(ctx
, rsrc
, NULL
,
1553 1, ctx
->i8
, cache_policy
,
1554 false, false, false);
1556 unsigned dfmt
= V_008F0C_BUF_DATA_FORMAT_8
;
1557 unsigned nfmt
= V_008F0C_BUF_NUM_FORMAT_UINT
;
1559 res
= ac_build_raw_tbuffer_load(ctx
, rsrc
, voffset
, soffset
,
1560 immoffset
, 1, dfmt
, nfmt
, cache_policy
,
1563 res
= LLVMBuildTrunc(ctx
->builder
, res
, ctx
->i8
, "");
1570 * Convert an 11- or 10-bit unsigned floating point number to an f32.
1572 * The input exponent is expected to be biased analogous to IEEE-754, i.e. by
1573 * 2^(exp_bits-1) - 1 (as defined in OpenGL and other graphics APIs).
1576 ac_ufN_to_float(struct ac_llvm_context
*ctx
, LLVMValueRef src
, unsigned exp_bits
, unsigned mant_bits
)
1578 assert(LLVMTypeOf(src
) == ctx
->i32
);
1581 LLVMValueRef mantissa
;
1582 mantissa
= LLVMBuildAnd(ctx
->builder
, src
, LLVMConstInt(ctx
->i32
, (1 << mant_bits
) - 1, false), "");
1584 /* Converting normal numbers is just a shift + correcting the exponent bias */
1585 unsigned normal_shift
= 23 - mant_bits
;
1586 unsigned bias_shift
= 127 - ((1 << (exp_bits
- 1)) - 1);
1587 LLVMValueRef shifted
, normal
;
1589 shifted
= LLVMBuildShl(ctx
->builder
, src
, LLVMConstInt(ctx
->i32
, normal_shift
, false), "");
1590 normal
= LLVMBuildAdd(ctx
->builder
, shifted
, LLVMConstInt(ctx
->i32
, bias_shift
<< 23, false), "");
1592 /* Converting nan/inf numbers is the same, but with a different exponent update */
1593 LLVMValueRef naninf
;
1594 naninf
= LLVMBuildOr(ctx
->builder
, normal
, LLVMConstInt(ctx
->i32
, 0xff << 23, false), "");
1596 /* Converting denormals is the complex case: determine the leading zeros of the
1597 * mantissa to obtain the correct shift for the mantissa and exponent correction.
1599 LLVMValueRef denormal
;
1600 LLVMValueRef params
[2] = {
1602 ctx
->i1true
, /* result can be undef when arg is 0 */
1604 LLVMValueRef ctlz
= ac_build_intrinsic(ctx
, "llvm.ctlz.i32", ctx
->i32
,
1605 params
, 2, AC_FUNC_ATTR_READNONE
);
1607 /* Shift such that the leading 1 ends up as the LSB of the exponent field. */
1608 tmp
= LLVMBuildSub(ctx
->builder
, ctlz
, LLVMConstInt(ctx
->i32
, 8, false), "");
1609 denormal
= LLVMBuildShl(ctx
->builder
, mantissa
, tmp
, "");
1611 unsigned denormal_exp
= bias_shift
+ (32 - mant_bits
) - 1;
1612 tmp
= LLVMBuildSub(ctx
->builder
, LLVMConstInt(ctx
->i32
, denormal_exp
, false), ctlz
, "");
1613 tmp
= LLVMBuildShl(ctx
->builder
, tmp
, LLVMConstInt(ctx
->i32
, 23, false), "");
1614 denormal
= LLVMBuildAdd(ctx
->builder
, denormal
, tmp
, "");
1616 /* Select the final result. */
1617 LLVMValueRef result
;
1619 tmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntUGE
, src
,
1620 LLVMConstInt(ctx
->i32
, ((1 << exp_bits
) - 1) << mant_bits
, false), "");
1621 result
= LLVMBuildSelect(ctx
->builder
, tmp
, naninf
, normal
, "");
1623 tmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntUGE
, src
,
1624 LLVMConstInt(ctx
->i32
, 1 << mant_bits
, false), "");
1625 result
= LLVMBuildSelect(ctx
->builder
, tmp
, result
, denormal
, "");
1627 tmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src
, ctx
->i32_0
, "");
1628 result
= LLVMBuildSelect(ctx
->builder
, tmp
, result
, ctx
->i32_0
, "");
1630 return ac_to_float(ctx
, result
);
1634 * Generate a fully general open coded buffer format fetch with all required
1635 * fixups suitable for vertex fetch, using non-format buffer loads.
1637 * Some combinations of argument values have special interpretations:
1638 * - size = 8 bytes, format = fixed indicates PIPE_FORMAT_R11G11B10_FLOAT
1639 * - size = 8 bytes, format != {float,fixed} indicates a 2_10_10_10 data format
1641 * \param log_size log(size of channel in bytes)
1642 * \param num_channels number of channels (1 to 4)
1643 * \param format AC_FETCH_FORMAT_xxx value
1644 * \param reverse whether XYZ channels are reversed
1645 * \param known_aligned whether the source is known to be aligned to hardware's
1646 * effective element size for loading the given format
1647 * (note: this means dword alignment for 8_8_8_8, 16_16, etc.)
1648 * \param rsrc buffer resource descriptor
1649 * \return the resulting vector of floats or integers bitcast to <4 x i32>
1652 ac_build_opencoded_load_format(struct ac_llvm_context
*ctx
,
1654 unsigned num_channels
,
1659 LLVMValueRef vindex
,
1660 LLVMValueRef voffset
,
1661 LLVMValueRef soffset
,
1662 unsigned cache_policy
,
1666 unsigned load_log_size
= log_size
;
1667 unsigned load_num_channels
= num_channels
;
1668 if (log_size
== 3) {
1670 if (format
== AC_FETCH_FORMAT_FLOAT
) {
1671 load_num_channels
= 2 * num_channels
;
1673 load_num_channels
= 1; /* 10_11_11 or 2_10_10_10 */
1677 int log_recombine
= 0;
1678 if (ctx
->chip_class
== GFX6
&& !known_aligned
) {
1679 /* Avoid alignment restrictions by loading one byte at a time. */
1680 load_num_channels
<<= load_log_size
;
1681 log_recombine
= load_log_size
;
1683 } else if (load_num_channels
== 2 || load_num_channels
== 4) {
1684 log_recombine
= -util_logbase2(load_num_channels
);
1685 load_num_channels
= 1;
1686 load_log_size
+= -log_recombine
;
1689 assert(load_log_size
>= 2 || LLVM_VERSION_MAJOR
>= 9);
1691 LLVMValueRef loads
[32]; /* up to 32 bytes */
1692 for (unsigned i
= 0; i
< load_num_channels
; ++i
) {
1693 tmp
= LLVMBuildAdd(ctx
->builder
, soffset
,
1694 LLVMConstInt(ctx
->i32
, i
<< load_log_size
, false), "");
1695 LLVMTypeRef channel_type
= load_log_size
== 0 ? ctx
->i8
:
1696 load_log_size
== 1 ? ctx
->i16
: ctx
->i32
;
1697 unsigned num_channels
= 1 << (MAX2(load_log_size
, 2) - 2);
1698 loads
[i
] = ac_build_buffer_load_common(
1699 ctx
, rsrc
, vindex
, voffset
, tmp
,
1700 num_channels
, channel_type
, cache_policy
,
1701 can_speculate
, false, true);
1702 if (load_log_size
>= 2)
1703 loads
[i
] = ac_to_integer(ctx
, loads
[i
]);
1706 if (log_recombine
> 0) {
1707 /* Recombine bytes if necessary (GFX6 only) */
1708 LLVMTypeRef dst_type
= log_recombine
== 2 ? ctx
->i32
: ctx
->i16
;
1710 for (unsigned src
= 0, dst
= 0; src
< load_num_channels
; ++dst
) {
1711 LLVMValueRef accum
= NULL
;
1712 for (unsigned i
= 0; i
< (1 << log_recombine
); ++i
, ++src
) {
1713 tmp
= LLVMBuildZExt(ctx
->builder
, loads
[src
], dst_type
, "");
1717 tmp
= LLVMBuildShl(ctx
->builder
, tmp
,
1718 LLVMConstInt(dst_type
, 8 * i
, false), "");
1719 accum
= LLVMBuildOr(ctx
->builder
, accum
, tmp
, "");
1724 } else if (log_recombine
< 0) {
1725 /* Split vectors of dwords */
1726 if (load_log_size
> 2) {
1727 assert(load_num_channels
== 1);
1728 LLVMValueRef loaded
= loads
[0];
1729 unsigned log_split
= load_log_size
- 2;
1730 log_recombine
+= log_split
;
1731 load_num_channels
= 1 << log_split
;
1733 for (unsigned i
= 0; i
< load_num_channels
; ++i
) {
1734 tmp
= LLVMConstInt(ctx
->i32
, i
, false);
1735 loads
[i
] = LLVMBuildExtractElement(ctx
->builder
, loaded
, tmp
, "");
1739 /* Further split dwords and shorts if required */
1740 if (log_recombine
< 0) {
1741 for (unsigned src
= load_num_channels
,
1742 dst
= load_num_channels
<< -log_recombine
;
1744 unsigned dst_bits
= 1 << (3 + load_log_size
+ log_recombine
);
1745 LLVMTypeRef dst_type
= LLVMIntTypeInContext(ctx
->context
, dst_bits
);
1746 LLVMValueRef loaded
= loads
[src
- 1];
1747 LLVMTypeRef loaded_type
= LLVMTypeOf(loaded
);
1748 for (unsigned i
= 1 << -log_recombine
; i
> 0; --i
, --dst
) {
1749 tmp
= LLVMConstInt(loaded_type
, dst_bits
* (i
- 1), false);
1750 tmp
= LLVMBuildLShr(ctx
->builder
, loaded
, tmp
, "");
1751 loads
[dst
- 1] = LLVMBuildTrunc(ctx
->builder
, tmp
, dst_type
, "");
1757 if (log_size
== 3) {
1758 if (format
== AC_FETCH_FORMAT_FLOAT
) {
1759 for (unsigned i
= 0; i
< num_channels
; ++i
) {
1760 tmp
= ac_build_gather_values(ctx
, &loads
[2 * i
], 2);
1761 loads
[i
] = LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f64
, "");
1763 } else if (format
== AC_FETCH_FORMAT_FIXED
) {
1764 /* 10_11_11_FLOAT */
1765 LLVMValueRef data
= loads
[0];
1766 LLVMValueRef i32_2047
= LLVMConstInt(ctx
->i32
, 2047, false);
1767 LLVMValueRef r
= LLVMBuildAnd(ctx
->builder
, data
, i32_2047
, "");
1768 tmp
= LLVMBuildLShr(ctx
->builder
, data
, LLVMConstInt(ctx
->i32
, 11, false), "");
1769 LLVMValueRef g
= LLVMBuildAnd(ctx
->builder
, tmp
, i32_2047
, "");
1770 LLVMValueRef b
= LLVMBuildLShr(ctx
->builder
, data
, LLVMConstInt(ctx
->i32
, 22, false), "");
1772 loads
[0] = ac_to_integer(ctx
, ac_ufN_to_float(ctx
, r
, 5, 6));
1773 loads
[1] = ac_to_integer(ctx
, ac_ufN_to_float(ctx
, g
, 5, 6));
1774 loads
[2] = ac_to_integer(ctx
, ac_ufN_to_float(ctx
, b
, 5, 5));
1778 format
= AC_FETCH_FORMAT_FLOAT
;
1780 /* 2_10_10_10 data formats */
1781 LLVMValueRef data
= loads
[0];
1782 LLVMTypeRef i10
= LLVMIntTypeInContext(ctx
->context
, 10);
1783 LLVMTypeRef i2
= LLVMIntTypeInContext(ctx
->context
, 2);
1784 loads
[0] = LLVMBuildTrunc(ctx
->builder
, data
, i10
, "");
1785 tmp
= LLVMBuildLShr(ctx
->builder
, data
, LLVMConstInt(ctx
->i32
, 10, false), "");
1786 loads
[1] = LLVMBuildTrunc(ctx
->builder
, tmp
, i10
, "");
1787 tmp
= LLVMBuildLShr(ctx
->builder
, data
, LLVMConstInt(ctx
->i32
, 20, false), "");
1788 loads
[2] = LLVMBuildTrunc(ctx
->builder
, tmp
, i10
, "");
1789 tmp
= LLVMBuildLShr(ctx
->builder
, data
, LLVMConstInt(ctx
->i32
, 30, false), "");
1790 loads
[3] = LLVMBuildTrunc(ctx
->builder
, tmp
, i2
, "");
1796 if (format
== AC_FETCH_FORMAT_FLOAT
) {
1797 if (log_size
!= 2) {
1798 for (unsigned chan
= 0; chan
< num_channels
; ++chan
) {
1799 tmp
= ac_to_float(ctx
, loads
[chan
]);
1801 tmp
= LLVMBuildFPTrunc(ctx
->builder
, tmp
, ctx
->f32
, "");
1802 else if (log_size
== 1)
1803 tmp
= LLVMBuildFPExt(ctx
->builder
, tmp
, ctx
->f32
, "");
1804 loads
[chan
] = ac_to_integer(ctx
, tmp
);
1807 } else if (format
== AC_FETCH_FORMAT_UINT
) {
1808 if (log_size
!= 2) {
1809 for (unsigned chan
= 0; chan
< num_channels
; ++chan
)
1810 loads
[chan
] = LLVMBuildZExt(ctx
->builder
, loads
[chan
], ctx
->i32
, "");
1812 } else if (format
== AC_FETCH_FORMAT_SINT
) {
1813 if (log_size
!= 2) {
1814 for (unsigned chan
= 0; chan
< num_channels
; ++chan
)
1815 loads
[chan
] = LLVMBuildSExt(ctx
->builder
, loads
[chan
], ctx
->i32
, "");
1818 bool unsign
= format
== AC_FETCH_FORMAT_UNORM
||
1819 format
== AC_FETCH_FORMAT_USCALED
||
1820 format
== AC_FETCH_FORMAT_UINT
;
1822 for (unsigned chan
= 0; chan
< num_channels
; ++chan
) {
1824 tmp
= LLVMBuildUIToFP(ctx
->builder
, loads
[chan
], ctx
->f32
, "");
1826 tmp
= LLVMBuildSIToFP(ctx
->builder
, loads
[chan
], ctx
->f32
, "");
1829 LLVMValueRef scale
= NULL
;
1830 if (format
== AC_FETCH_FORMAT_FIXED
) {
1831 assert(log_size
== 2);
1832 scale
= LLVMConstReal(ctx
->f32
, 1.0 / 0x10000);
1833 } else if (format
== AC_FETCH_FORMAT_UNORM
) {
1834 unsigned bits
= LLVMGetIntTypeWidth(LLVMTypeOf(loads
[chan
]));
1835 scale
= LLVMConstReal(ctx
->f32
, 1.0 / (((uint64_t)1 << bits
) - 1));
1836 } else if (format
== AC_FETCH_FORMAT_SNORM
) {
1837 unsigned bits
= LLVMGetIntTypeWidth(LLVMTypeOf(loads
[chan
]));
1838 scale
= LLVMConstReal(ctx
->f32
, 1.0 / (((uint64_t)1 << (bits
- 1)) - 1));
1841 tmp
= LLVMBuildFMul(ctx
->builder
, tmp
, scale
, "");
1843 if (format
== AC_FETCH_FORMAT_SNORM
) {
1844 /* Clamp to [-1, 1] */
1845 LLVMValueRef neg_one
= LLVMConstReal(ctx
->f32
, -1.0);
1846 LLVMValueRef clamp
=
1847 LLVMBuildFCmp(ctx
->builder
, LLVMRealULT
, tmp
, neg_one
, "");
1848 tmp
= LLVMBuildSelect(ctx
->builder
, clamp
, neg_one
, tmp
, "");
1851 loads
[chan
] = ac_to_integer(ctx
, tmp
);
1855 while (num_channels
< 4) {
1856 if (format
== AC_FETCH_FORMAT_UINT
|| format
== AC_FETCH_FORMAT_SINT
) {
1857 loads
[num_channels
] = num_channels
== 3 ? ctx
->i32_1
: ctx
->i32_0
;
1859 loads
[num_channels
] = ac_to_integer(ctx
, num_channels
== 3 ? ctx
->f32_1
: ctx
->f32_0
);
1866 loads
[0] = loads
[2];
1870 return ac_build_gather_values(ctx
, loads
, 4);
1874 ac_build_tbuffer_store(struct ac_llvm_context
*ctx
,
1877 LLVMValueRef vindex
,
1878 LLVMValueRef voffset
,
1879 LLVMValueRef soffset
,
1880 LLVMValueRef immoffset
,
1881 unsigned num_channels
,
1884 unsigned cache_policy
,
1887 voffset
= LLVMBuildAdd(ctx
->builder
, voffset
? voffset
: ctx
->i32_0
,
1890 LLVMValueRef args
[7];
1892 args
[idx
++] = vdata
;
1893 args
[idx
++] = LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, "");
1895 args
[idx
++] = vindex
? vindex
: ctx
->i32_0
;
1896 args
[idx
++] = voffset
? voffset
: ctx
->i32_0
;
1897 args
[idx
++] = soffset
? soffset
: ctx
->i32_0
;
1898 args
[idx
++] = LLVMConstInt(ctx
->i32
, ac_get_tbuffer_format(ctx
, dfmt
, nfmt
), 0);
1899 args
[idx
++] = LLVMConstInt(ctx
->i32
, cache_policy
, 0);
1900 unsigned func
= !ac_has_vec3_support(ctx
->chip_class
, true) && num_channels
== 3 ? 4 : num_channels
;
1901 const char *indexing_kind
= structurized
? "struct" : "raw";
1902 char name
[256], type_name
[8];
1904 LLVMTypeRef type
= func
> 1 ? LLVMVectorType(ctx
->i32
, func
) : ctx
->i32
;
1905 ac_build_type_name_for_intr(type
, type_name
, sizeof(type_name
));
1907 snprintf(name
, sizeof(name
), "llvm.amdgcn.%s.tbuffer.store.%s",
1908 indexing_kind
, type_name
);
1910 ac_build_intrinsic(ctx
, name
, ctx
->voidt
, args
, idx
,
1911 AC_FUNC_ATTR_INACCESSIBLE_MEM_ONLY
);
1915 ac_build_struct_tbuffer_store(struct ac_llvm_context
*ctx
,
1918 LLVMValueRef vindex
,
1919 LLVMValueRef voffset
,
1920 LLVMValueRef soffset
,
1921 LLVMValueRef immoffset
,
1922 unsigned num_channels
,
1925 unsigned cache_policy
)
1927 ac_build_tbuffer_store(ctx
, rsrc
, vdata
, vindex
, voffset
, soffset
,
1928 immoffset
, num_channels
, dfmt
, nfmt
, cache_policy
,
1933 ac_build_raw_tbuffer_store(struct ac_llvm_context
*ctx
,
1936 LLVMValueRef voffset
,
1937 LLVMValueRef soffset
,
1938 LLVMValueRef immoffset
,
1939 unsigned num_channels
,
1942 unsigned cache_policy
)
1944 ac_build_tbuffer_store(ctx
, rsrc
, vdata
, NULL
, voffset
, soffset
,
1945 immoffset
, num_channels
, dfmt
, nfmt
, cache_policy
,
1950 ac_build_tbuffer_store_short(struct ac_llvm_context
*ctx
,
1953 LLVMValueRef voffset
,
1954 LLVMValueRef soffset
,
1955 unsigned cache_policy
)
1957 vdata
= LLVMBuildBitCast(ctx
->builder
, vdata
, ctx
->i16
, "");
1959 if (LLVM_VERSION_MAJOR
>= 9) {
1960 /* LLVM 9+ supports i8/i16 with struct/raw intrinsics. */
1961 ac_build_buffer_store_common(ctx
, rsrc
, vdata
, NULL
,
1962 voffset
, soffset
, 1,
1963 ctx
->i16
, cache_policy
,
1966 unsigned dfmt
= V_008F0C_BUF_DATA_FORMAT_16
;
1967 unsigned nfmt
= V_008F0C_BUF_NUM_FORMAT_UINT
;
1969 vdata
= LLVMBuildZExt(ctx
->builder
, vdata
, ctx
->i32
, "");
1971 ac_build_raw_tbuffer_store(ctx
, rsrc
, vdata
, voffset
, soffset
,
1972 ctx
->i32_0
, 1, dfmt
, nfmt
, cache_policy
);
1977 ac_build_tbuffer_store_byte(struct ac_llvm_context
*ctx
,
1980 LLVMValueRef voffset
,
1981 LLVMValueRef soffset
,
1982 unsigned cache_policy
)
1984 vdata
= LLVMBuildBitCast(ctx
->builder
, vdata
, ctx
->i8
, "");
1986 if (LLVM_VERSION_MAJOR
>= 9) {
1987 /* LLVM 9+ supports i8/i16 with struct/raw intrinsics. */
1988 ac_build_buffer_store_common(ctx
, rsrc
, vdata
, NULL
,
1989 voffset
, soffset
, 1,
1990 ctx
->i8
, cache_policy
,
1993 unsigned dfmt
= V_008F0C_BUF_DATA_FORMAT_8
;
1994 unsigned nfmt
= V_008F0C_BUF_NUM_FORMAT_UINT
;
1996 vdata
= LLVMBuildZExt(ctx
->builder
, vdata
, ctx
->i32
, "");
1998 ac_build_raw_tbuffer_store(ctx
, rsrc
, vdata
, voffset
, soffset
,
1999 ctx
->i32_0
, 1, dfmt
, nfmt
, cache_policy
);
2003 * Set range metadata on an instruction. This can only be used on load and
2004 * call instructions. If you know an instruction can only produce the values
2005 * 0, 1, 2, you would do set_range_metadata(value, 0, 3);
2006 * \p lo is the minimum value inclusive.
2007 * \p hi is the maximum value exclusive.
2009 static void set_range_metadata(struct ac_llvm_context
*ctx
,
2010 LLVMValueRef value
, unsigned lo
, unsigned hi
)
2012 LLVMValueRef range_md
, md_args
[2];
2013 LLVMTypeRef type
= LLVMTypeOf(value
);
2014 LLVMContextRef context
= LLVMGetTypeContext(type
);
2016 md_args
[0] = LLVMConstInt(type
, lo
, false);
2017 md_args
[1] = LLVMConstInt(type
, hi
, false);
2018 range_md
= LLVMMDNodeInContext(context
, md_args
, 2);
2019 LLVMSetMetadata(value
, ctx
->range_md_kind
, range_md
);
2023 ac_get_thread_id(struct ac_llvm_context
*ctx
)
2027 LLVMValueRef tid_args
[2];
2028 tid_args
[0] = LLVMConstInt(ctx
->i32
, 0xffffffff, false);
2029 tid_args
[1] = ctx
->i32_0
;
2030 tid_args
[1] = ac_build_intrinsic(ctx
,
2031 "llvm.amdgcn.mbcnt.lo", ctx
->i32
,
2032 tid_args
, 2, AC_FUNC_ATTR_READNONE
);
2034 if (ctx
->wave_size
== 32) {
2037 tid
= ac_build_intrinsic(ctx
, "llvm.amdgcn.mbcnt.hi",
2039 2, AC_FUNC_ATTR_READNONE
);
2041 set_range_metadata(ctx
, tid
, 0, ctx
->wave_size
);
2046 * AMD GCN implements derivatives using the local data store (LDS)
2047 * All writes to the LDS happen in all executing threads at
2048 * the same time. TID is the Thread ID for the current
2049 * thread and is a value between 0 and 63, representing
2050 * the thread's position in the wavefront.
2052 * For the pixel shader threads are grouped into quads of four pixels.
2053 * The TIDs of the pixels of a quad are:
2061 * So, masking the TID with 0xfffffffc yields the TID of the top left pixel
2062 * of the quad, masking with 0xfffffffd yields the TID of the top pixel of
2063 * the current pixel's column, and masking with 0xfffffffe yields the TID
2064 * of the left pixel of the current pixel's row.
2066 * Adding 1 yields the TID of the pixel to the right of the left pixel, and
2067 * adding 2 yields the TID of the pixel below the top pixel.
2070 ac_build_ddxy(struct ac_llvm_context
*ctx
,
2075 unsigned tl_lanes
[4], trbl_lanes
[4];
2076 char name
[32], type
[8];
2077 LLVMValueRef tl
, trbl
;
2078 LLVMTypeRef result_type
;
2079 LLVMValueRef result
;
2081 result_type
= ac_to_float_type(ctx
, LLVMTypeOf(val
));
2083 if (result_type
== ctx
->f16
)
2084 val
= LLVMBuildZExt(ctx
->builder
, val
, ctx
->i32
, "");
2086 for (unsigned i
= 0; i
< 4; ++i
) {
2087 tl_lanes
[i
] = i
& mask
;
2088 trbl_lanes
[i
] = (i
& mask
) + idx
;
2091 tl
= ac_build_quad_swizzle(ctx
, val
,
2092 tl_lanes
[0], tl_lanes
[1],
2093 tl_lanes
[2], tl_lanes
[3]);
2094 trbl
= ac_build_quad_swizzle(ctx
, val
,
2095 trbl_lanes
[0], trbl_lanes
[1],
2096 trbl_lanes
[2], trbl_lanes
[3]);
2098 if (result_type
== ctx
->f16
) {
2099 tl
= LLVMBuildTrunc(ctx
->builder
, tl
, ctx
->i16
, "");
2100 trbl
= LLVMBuildTrunc(ctx
->builder
, trbl
, ctx
->i16
, "");
2103 tl
= LLVMBuildBitCast(ctx
->builder
, tl
, result_type
, "");
2104 trbl
= LLVMBuildBitCast(ctx
->builder
, trbl
, result_type
, "");
2105 result
= LLVMBuildFSub(ctx
->builder
, trbl
, tl
, "");
2107 ac_build_type_name_for_intr(result_type
, type
, sizeof(type
));
2108 snprintf(name
, sizeof(name
), "llvm.amdgcn.wqm.%s", type
);
2110 return ac_build_intrinsic(ctx
, name
, result_type
, &result
, 1, 0);
2114 ac_build_sendmsg(struct ac_llvm_context
*ctx
,
2116 LLVMValueRef wave_id
)
2118 LLVMValueRef args
[2];
2119 args
[0] = LLVMConstInt(ctx
->i32
, msg
, false);
2121 ac_build_intrinsic(ctx
, "llvm.amdgcn.s.sendmsg", ctx
->voidt
, args
, 2, 0);
2125 ac_build_imsb(struct ac_llvm_context
*ctx
,
2127 LLVMTypeRef dst_type
)
2129 LLVMValueRef msb
= ac_build_intrinsic(ctx
, "llvm.amdgcn.sffbh.i32",
2131 AC_FUNC_ATTR_READNONE
);
2133 /* The HW returns the last bit index from MSB, but NIR/TGSI wants
2134 * the index from LSB. Invert it by doing "31 - msb". */
2135 msb
= LLVMBuildSub(ctx
->builder
, LLVMConstInt(ctx
->i32
, 31, false),
2138 LLVMValueRef all_ones
= LLVMConstInt(ctx
->i32
, -1, true);
2139 LLVMValueRef cond
= LLVMBuildOr(ctx
->builder
,
2140 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
2141 arg
, ctx
->i32_0
, ""),
2142 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
2143 arg
, all_ones
, ""), "");
2145 return LLVMBuildSelect(ctx
->builder
, cond
, all_ones
, msb
, "");
2149 ac_build_umsb(struct ac_llvm_context
*ctx
,
2151 LLVMTypeRef dst_type
)
2153 const char *intrin_name
;
2155 LLVMValueRef highest_bit
;
2159 bitsize
= ac_get_elem_bits(ctx
, LLVMTypeOf(arg
));
2162 intrin_name
= "llvm.ctlz.i64";
2164 highest_bit
= LLVMConstInt(ctx
->i64
, 63, false);
2168 intrin_name
= "llvm.ctlz.i32";
2170 highest_bit
= LLVMConstInt(ctx
->i32
, 31, false);
2174 intrin_name
= "llvm.ctlz.i16";
2176 highest_bit
= LLVMConstInt(ctx
->i16
, 15, false);
2180 intrin_name
= "llvm.ctlz.i8";
2182 highest_bit
= LLVMConstInt(ctx
->i8
, 7, false);
2186 unreachable(!"invalid bitsize");
2190 LLVMValueRef params
[2] = {
2195 LLVMValueRef msb
= ac_build_intrinsic(ctx
, intrin_name
, type
,
2197 AC_FUNC_ATTR_READNONE
);
2199 /* The HW returns the last bit index from MSB, but TGSI/NIR wants
2200 * the index from LSB. Invert it by doing "31 - msb". */
2201 msb
= LLVMBuildSub(ctx
->builder
, highest_bit
, msb
, "");
2203 if (bitsize
== 64) {
2204 msb
= LLVMBuildTrunc(ctx
->builder
, msb
, ctx
->i32
, "");
2205 } else if (bitsize
< 32) {
2206 msb
= LLVMBuildSExt(ctx
->builder
, msb
, ctx
->i32
, "");
2209 /* check for zero */
2210 return LLVMBuildSelect(ctx
->builder
,
2211 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, arg
, zero
, ""),
2212 LLVMConstInt(ctx
->i32
, -1, true), msb
, "");
2215 LLVMValueRef
ac_build_fmin(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
2219 snprintf(name
, sizeof(name
), "llvm.minnum.f%d", ac_get_elem_bits(ctx
, LLVMTypeOf(a
)));
2220 LLVMValueRef args
[2] = {a
, b
};
2221 return ac_build_intrinsic(ctx
, name
, LLVMTypeOf(a
), args
, 2,
2222 AC_FUNC_ATTR_READNONE
);
2225 LLVMValueRef
ac_build_fmax(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
2229 snprintf(name
, sizeof(name
), "llvm.maxnum.f%d", ac_get_elem_bits(ctx
, LLVMTypeOf(a
)));
2230 LLVMValueRef args
[2] = {a
, b
};
2231 return ac_build_intrinsic(ctx
, name
, LLVMTypeOf(a
), args
, 2,
2232 AC_FUNC_ATTR_READNONE
);
2235 LLVMValueRef
ac_build_imin(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
2238 LLVMValueRef cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSLE
, a
, b
, "");
2239 return LLVMBuildSelect(ctx
->builder
, cmp
, a
, b
, "");
2242 LLVMValueRef
ac_build_imax(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
2245 LLVMValueRef cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGT
, a
, b
, "");
2246 return LLVMBuildSelect(ctx
->builder
, cmp
, a
, b
, "");
2249 LLVMValueRef
ac_build_umin(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
2252 LLVMValueRef cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntULE
, a
, b
, "");
2253 return LLVMBuildSelect(ctx
->builder
, cmp
, a
, b
, "");
2256 LLVMValueRef
ac_build_umax(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
2259 LLVMValueRef cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntUGE
, a
, b
, "");
2260 return LLVMBuildSelect(ctx
->builder
, cmp
, a
, b
, "");
2263 LLVMValueRef
ac_build_clamp(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
2265 LLVMTypeRef t
= LLVMTypeOf(value
);
2266 return ac_build_fmin(ctx
, ac_build_fmax(ctx
, value
, LLVMConstReal(t
, 0.0)),
2267 LLVMConstReal(t
, 1.0));
2270 void ac_build_export(struct ac_llvm_context
*ctx
, struct ac_export_args
*a
)
2272 LLVMValueRef args
[9];
2274 args
[0] = LLVMConstInt(ctx
->i32
, a
->target
, 0);
2275 args
[1] = LLVMConstInt(ctx
->i32
, a
->enabled_channels
, 0);
2278 LLVMTypeRef i16
= LLVMInt16TypeInContext(ctx
->context
);
2279 LLVMTypeRef v2i16
= LLVMVectorType(i16
, 2);
2281 args
[2] = LLVMBuildBitCast(ctx
->builder
, a
->out
[0],
2283 args
[3] = LLVMBuildBitCast(ctx
->builder
, a
->out
[1],
2285 args
[4] = LLVMConstInt(ctx
->i1
, a
->done
, 0);
2286 args
[5] = LLVMConstInt(ctx
->i1
, a
->valid_mask
, 0);
2288 ac_build_intrinsic(ctx
, "llvm.amdgcn.exp.compr.v2i16",
2289 ctx
->voidt
, args
, 6, 0);
2291 args
[2] = a
->out
[0];
2292 args
[3] = a
->out
[1];
2293 args
[4] = a
->out
[2];
2294 args
[5] = a
->out
[3];
2295 args
[6] = LLVMConstInt(ctx
->i1
, a
->done
, 0);
2296 args
[7] = LLVMConstInt(ctx
->i1
, a
->valid_mask
, 0);
2298 ac_build_intrinsic(ctx
, "llvm.amdgcn.exp.f32",
2299 ctx
->voidt
, args
, 8, 0);
2303 void ac_build_export_null(struct ac_llvm_context
*ctx
)
2305 struct ac_export_args args
;
2307 args
.enabled_channels
= 0x0; /* enabled channels */
2308 args
.valid_mask
= 1; /* whether the EXEC mask is valid */
2309 args
.done
= 1; /* DONE bit */
2310 args
.target
= V_008DFC_SQ_EXP_NULL
;
2311 args
.compr
= 0; /* COMPR flag (0 = 32-bit export) */
2312 args
.out
[0] = LLVMGetUndef(ctx
->f32
); /* R */
2313 args
.out
[1] = LLVMGetUndef(ctx
->f32
); /* G */
2314 args
.out
[2] = LLVMGetUndef(ctx
->f32
); /* B */
2315 args
.out
[3] = LLVMGetUndef(ctx
->f32
); /* A */
2317 ac_build_export(ctx
, &args
);
2320 static unsigned ac_num_coords(enum ac_image_dim dim
)
2326 case ac_image_1darray
:
2330 case ac_image_2darray
:
2331 case ac_image_2dmsaa
:
2333 case ac_image_2darraymsaa
:
2336 unreachable("ac_num_coords: bad dim");
2340 static unsigned ac_num_derivs(enum ac_image_dim dim
)
2344 case ac_image_1darray
:
2347 case ac_image_2darray
:
2352 case ac_image_2dmsaa
:
2353 case ac_image_2darraymsaa
:
2355 unreachable("derivatives not supported");
2359 static const char *get_atomic_name(enum ac_atomic_op op
)
2362 case ac_atomic_swap
: return "swap";
2363 case ac_atomic_add
: return "add";
2364 case ac_atomic_sub
: return "sub";
2365 case ac_atomic_smin
: return "smin";
2366 case ac_atomic_umin
: return "umin";
2367 case ac_atomic_smax
: return "smax";
2368 case ac_atomic_umax
: return "umax";
2369 case ac_atomic_and
: return "and";
2370 case ac_atomic_or
: return "or";
2371 case ac_atomic_xor
: return "xor";
2372 case ac_atomic_inc_wrap
: return "inc";
2373 case ac_atomic_dec_wrap
: return "dec";
2375 unreachable("bad atomic op");
2378 LLVMValueRef
ac_build_image_opcode(struct ac_llvm_context
*ctx
,
2379 struct ac_image_args
*a
)
2381 const char *overload
[3] = { "", "", "" };
2382 unsigned num_overloads
= 0;
2383 LLVMValueRef args
[18];
2384 unsigned num_args
= 0;
2385 enum ac_image_dim dim
= a
->dim
;
2387 assert(!a
->lod
|| a
->lod
== ctx
->i32_0
|| a
->lod
== ctx
->f32_0
||
2389 assert((a
->opcode
!= ac_image_get_resinfo
&& a
->opcode
!= ac_image_load_mip
&&
2390 a
->opcode
!= ac_image_store_mip
) ||
2392 assert(a
->opcode
== ac_image_sample
|| a
->opcode
== ac_image_gather4
||
2393 (!a
->compare
&& !a
->offset
));
2394 assert((a
->opcode
== ac_image_sample
|| a
->opcode
== ac_image_gather4
||
2395 a
->opcode
== ac_image_get_lod
) ||
2397 assert((a
->bias
? 1 : 0) +
2399 (a
->level_zero
? 1 : 0) +
2400 (a
->derivs
[0] ? 1 : 0) <= 1);
2402 if (a
->opcode
== ac_image_get_lod
) {
2404 case ac_image_1darray
:
2407 case ac_image_2darray
:
2416 bool sample
= a
->opcode
== ac_image_sample
||
2417 a
->opcode
== ac_image_gather4
||
2418 a
->opcode
== ac_image_get_lod
;
2419 bool atomic
= a
->opcode
== ac_image_atomic
||
2420 a
->opcode
== ac_image_atomic_cmpswap
;
2421 bool load
= a
->opcode
== ac_image_sample
||
2422 a
->opcode
== ac_image_gather4
||
2423 a
->opcode
== ac_image_load
||
2424 a
->opcode
== ac_image_load_mip
;
2425 LLVMTypeRef coord_type
= sample
? ctx
->f32
: ctx
->i32
;
2427 if (atomic
|| a
->opcode
== ac_image_store
|| a
->opcode
== ac_image_store_mip
) {
2428 args
[num_args
++] = a
->data
[0];
2429 if (a
->opcode
== ac_image_atomic_cmpswap
)
2430 args
[num_args
++] = a
->data
[1];
2434 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->dmask
, false);
2437 args
[num_args
++] = ac_to_integer(ctx
, a
->offset
);
2439 args
[num_args
++] = ac_to_float(ctx
, a
->bias
);
2440 overload
[num_overloads
++] = ".f32";
2443 args
[num_args
++] = ac_to_float(ctx
, a
->compare
);
2445 unsigned count
= ac_num_derivs(dim
);
2446 for (unsigned i
= 0; i
< count
; ++i
)
2447 args
[num_args
++] = ac_to_float(ctx
, a
->derivs
[i
]);
2448 overload
[num_overloads
++] = ".f32";
2450 unsigned num_coords
=
2451 a
->opcode
!= ac_image_get_resinfo
? ac_num_coords(dim
) : 0;
2452 for (unsigned i
= 0; i
< num_coords
; ++i
)
2453 args
[num_args
++] = LLVMBuildBitCast(ctx
->builder
, a
->coords
[i
], coord_type
, "");
2455 args
[num_args
++] = LLVMBuildBitCast(ctx
->builder
, a
->lod
, coord_type
, "");
2456 overload
[num_overloads
++] = sample
? ".f32" : ".i32";
2458 args
[num_args
++] = a
->resource
;
2460 args
[num_args
++] = a
->sampler
;
2461 args
[num_args
++] = LLVMConstInt(ctx
->i1
, a
->unorm
, false);
2464 args
[num_args
++] = ctx
->i32_0
; /* texfailctrl */
2465 args
[num_args
++] = LLVMConstInt(ctx
->i32
,
2466 load
? get_load_cache_policy(ctx
, a
->cache_policy
) :
2467 a
->cache_policy
, false);
2470 const char *atomic_subop
= "";
2471 switch (a
->opcode
) {
2472 case ac_image_sample
: name
= "sample"; break;
2473 case ac_image_gather4
: name
= "gather4"; break;
2474 case ac_image_load
: name
= "load"; break;
2475 case ac_image_load_mip
: name
= "load.mip"; break;
2476 case ac_image_store
: name
= "store"; break;
2477 case ac_image_store_mip
: name
= "store.mip"; break;
2478 case ac_image_atomic
:
2480 atomic_subop
= get_atomic_name(a
->atomic
);
2482 case ac_image_atomic_cmpswap
:
2484 atomic_subop
= "cmpswap";
2486 case ac_image_get_lod
: name
= "getlod"; break;
2487 case ac_image_get_resinfo
: name
= "getresinfo"; break;
2488 default: unreachable("invalid image opcode");
2491 const char *dimname
;
2493 case ac_image_1d
: dimname
= "1d"; break;
2494 case ac_image_2d
: dimname
= "2d"; break;
2495 case ac_image_3d
: dimname
= "3d"; break;
2496 case ac_image_cube
: dimname
= "cube"; break;
2497 case ac_image_1darray
: dimname
= "1darray"; break;
2498 case ac_image_2darray
: dimname
= "2darray"; break;
2499 case ac_image_2dmsaa
: dimname
= "2dmsaa"; break;
2500 case ac_image_2darraymsaa
: dimname
= "2darraymsaa"; break;
2501 default: unreachable("invalid dim");
2505 a
->lod
&& (a
->opcode
== ac_image_sample
|| a
->opcode
== ac_image_gather4
);
2507 snprintf(intr_name
, sizeof(intr_name
),
2508 "llvm.amdgcn.image.%s%s" /* base name */
2509 "%s%s%s" /* sample/gather modifiers */
2510 ".%s.%s%s%s%s", /* dimension and type overloads */
2512 a
->compare
? ".c" : "",
2515 a
->derivs
[0] ? ".d" :
2516 a
->level_zero
? ".lz" : "",
2517 a
->offset
? ".o" : "",
2519 atomic
? "i32" : "v4f32",
2520 overload
[0], overload
[1], overload
[2]);
2525 else if (a
->opcode
== ac_image_store
|| a
->opcode
== ac_image_store_mip
)
2530 LLVMValueRef result
=
2531 ac_build_intrinsic(ctx
, intr_name
, retty
, args
, num_args
,
2533 if (!sample
&& retty
== ctx
->v4f32
) {
2534 result
= LLVMBuildBitCast(ctx
->builder
, result
,
2540 LLVMValueRef
ac_build_cvt_pkrtz_f16(struct ac_llvm_context
*ctx
,
2541 LLVMValueRef args
[2])
2544 LLVMVectorType(LLVMHalfTypeInContext(ctx
->context
), 2);
2546 return ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pkrtz", v2f16
,
2547 args
, 2, AC_FUNC_ATTR_READNONE
);
2550 LLVMValueRef
ac_build_cvt_pknorm_i16(struct ac_llvm_context
*ctx
,
2551 LLVMValueRef args
[2])
2554 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pknorm.i16",
2555 ctx
->v2i16
, args
, 2,
2556 AC_FUNC_ATTR_READNONE
);
2557 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
2560 LLVMValueRef
ac_build_cvt_pknorm_u16(struct ac_llvm_context
*ctx
,
2561 LLVMValueRef args
[2])
2564 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pknorm.u16",
2565 ctx
->v2i16
, args
, 2,
2566 AC_FUNC_ATTR_READNONE
);
2567 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
2570 /* The 8-bit and 10-bit clamping is for HW workarounds. */
2571 LLVMValueRef
ac_build_cvt_pk_i16(struct ac_llvm_context
*ctx
,
2572 LLVMValueRef args
[2], unsigned bits
, bool hi
)
2574 assert(bits
== 8 || bits
== 10 || bits
== 16);
2576 LLVMValueRef max_rgb
= LLVMConstInt(ctx
->i32
,
2577 bits
== 8 ? 127 : bits
== 10 ? 511 : 32767, 0);
2578 LLVMValueRef min_rgb
= LLVMConstInt(ctx
->i32
,
2579 bits
== 8 ? -128 : bits
== 10 ? -512 : -32768, 0);
2580 LLVMValueRef max_alpha
=
2581 bits
!= 10 ? max_rgb
: ctx
->i32_1
;
2582 LLVMValueRef min_alpha
=
2583 bits
!= 10 ? min_rgb
: LLVMConstInt(ctx
->i32
, -2, 0);
2587 for (int i
= 0; i
< 2; i
++) {
2588 bool alpha
= hi
&& i
== 1;
2589 args
[i
] = ac_build_imin(ctx
, args
[i
],
2590 alpha
? max_alpha
: max_rgb
);
2591 args
[i
] = ac_build_imax(ctx
, args
[i
],
2592 alpha
? min_alpha
: min_rgb
);
2597 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pk.i16",
2598 ctx
->v2i16
, args
, 2,
2599 AC_FUNC_ATTR_READNONE
);
2600 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
2603 /* The 8-bit and 10-bit clamping is for HW workarounds. */
2604 LLVMValueRef
ac_build_cvt_pk_u16(struct ac_llvm_context
*ctx
,
2605 LLVMValueRef args
[2], unsigned bits
, bool hi
)
2607 assert(bits
== 8 || bits
== 10 || bits
== 16);
2609 LLVMValueRef max_rgb
= LLVMConstInt(ctx
->i32
,
2610 bits
== 8 ? 255 : bits
== 10 ? 1023 : 65535, 0);
2611 LLVMValueRef max_alpha
=
2612 bits
!= 10 ? max_rgb
: LLVMConstInt(ctx
->i32
, 3, 0);
2616 for (int i
= 0; i
< 2; i
++) {
2617 bool alpha
= hi
&& i
== 1;
2618 args
[i
] = ac_build_umin(ctx
, args
[i
],
2619 alpha
? max_alpha
: max_rgb
);
2624 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pk.u16",
2625 ctx
->v2i16
, args
, 2,
2626 AC_FUNC_ATTR_READNONE
);
2627 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
2630 LLVMValueRef
ac_build_wqm_vote(struct ac_llvm_context
*ctx
, LLVMValueRef i1
)
2632 return ac_build_intrinsic(ctx
, "llvm.amdgcn.wqm.vote", ctx
->i1
,
2633 &i1
, 1, AC_FUNC_ATTR_READNONE
);
2636 void ac_build_kill_if_false(struct ac_llvm_context
*ctx
, LLVMValueRef i1
)
2638 ac_build_intrinsic(ctx
, "llvm.amdgcn.kill", ctx
->voidt
,
2642 LLVMValueRef
ac_build_bfe(struct ac_llvm_context
*ctx
, LLVMValueRef input
,
2643 LLVMValueRef offset
, LLVMValueRef width
,
2646 LLVMValueRef args
[] = {
2652 return ac_build_intrinsic(ctx
, is_signed
? "llvm.amdgcn.sbfe.i32" :
2653 "llvm.amdgcn.ubfe.i32",
2654 ctx
->i32
, args
, 3, AC_FUNC_ATTR_READNONE
);
2658 LLVMValueRef
ac_build_imad(struct ac_llvm_context
*ctx
, LLVMValueRef s0
,
2659 LLVMValueRef s1
, LLVMValueRef s2
)
2661 return LLVMBuildAdd(ctx
->builder
,
2662 LLVMBuildMul(ctx
->builder
, s0
, s1
, ""), s2
, "");
2665 LLVMValueRef
ac_build_fmad(struct ac_llvm_context
*ctx
, LLVMValueRef s0
,
2666 LLVMValueRef s1
, LLVMValueRef s2
)
2668 /* FMA is better on GFX10, because it has FMA units instead of MUL-ADD units. */
2669 if (ctx
->chip_class
>= GFX10
) {
2670 return ac_build_intrinsic(ctx
, "llvm.fma.f32", ctx
->f32
,
2671 (LLVMValueRef
[]) {s0
, s1
, s2
}, 3,
2672 AC_FUNC_ATTR_READNONE
);
2675 return LLVMBuildFAdd(ctx
->builder
,
2676 LLVMBuildFMul(ctx
->builder
, s0
, s1
, ""), s2
, "");
2679 void ac_build_waitcnt(struct ac_llvm_context
*ctx
, unsigned wait_flags
)
2684 unsigned lgkmcnt
= 63;
2685 unsigned vmcnt
= ctx
->chip_class
>= GFX9
? 63 : 15;
2686 unsigned vscnt
= 63;
2688 if (wait_flags
& AC_WAIT_LGKM
)
2690 if (wait_flags
& AC_WAIT_VLOAD
)
2693 if (wait_flags
& AC_WAIT_VSTORE
) {
2694 if (ctx
->chip_class
>= GFX10
)
2700 /* There is no intrinsic for vscnt(0), so use a fence. */
2701 if ((wait_flags
& AC_WAIT_LGKM
&&
2702 wait_flags
& AC_WAIT_VLOAD
&&
2703 wait_flags
& AC_WAIT_VSTORE
) ||
2705 LLVMBuildFence(ctx
->builder
, LLVMAtomicOrderingRelease
, false, "");
2709 unsigned simm16
= (lgkmcnt
<< 8) |
2710 (7 << 4) | /* expcnt */
2712 ((vmcnt
>> 4) << 14);
2714 LLVMValueRef args
[1] = {
2715 LLVMConstInt(ctx
->i32
, simm16
, false),
2717 ac_build_intrinsic(ctx
, "llvm.amdgcn.s.waitcnt",
2718 ctx
->voidt
, args
, 1, 0);
2721 LLVMValueRef
ac_build_fmed3(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
2722 LLVMValueRef src1
, LLVMValueRef src2
,
2728 if (bitsize
== 16) {
2729 intr
= "llvm.amdgcn.fmed3.f16";
2731 } else if (bitsize
== 32) {
2732 intr
= "llvm.amdgcn.fmed3.f32";
2735 intr
= "llvm.amdgcn.fmed3.f64";
2739 LLVMValueRef params
[] = {
2744 return ac_build_intrinsic(ctx
, intr
, type
, params
, 3,
2745 AC_FUNC_ATTR_READNONE
);
2748 LLVMValueRef
ac_build_fract(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
2754 if (bitsize
== 16) {
2755 intr
= "llvm.amdgcn.fract.f16";
2757 } else if (bitsize
== 32) {
2758 intr
= "llvm.amdgcn.fract.f32";
2761 intr
= "llvm.amdgcn.fract.f64";
2765 LLVMValueRef params
[] = {
2768 return ac_build_intrinsic(ctx
, intr
, type
, params
, 1,
2769 AC_FUNC_ATTR_READNONE
);
2772 LLVMValueRef
ac_build_isign(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
2775 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->context
, bitsize
);
2776 LLVMValueRef zero
= LLVMConstInt(type
, 0, false);
2777 LLVMValueRef one
= LLVMConstInt(type
, 1, false);
2779 LLVMValueRef cmp
, val
;
2780 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGT
, src0
, zero
, "");
2781 val
= LLVMBuildSelect(ctx
->builder
, cmp
, one
, src0
, "");
2782 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGE
, val
, zero
, "");
2783 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstInt(type
, -1, true), "");
2787 LLVMValueRef
ac_build_fsign(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
2790 LLVMValueRef cmp
, val
, zero
, one
;
2793 if (bitsize
== 16) {
2797 } else if (bitsize
== 32) {
2807 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGT
, src0
, zero
, "");
2808 val
= LLVMBuildSelect(ctx
->builder
, cmp
, one
, src0
, "");
2809 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGE
, val
, zero
, "");
2810 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstReal(type
, -1.0), "");
2814 LLVMValueRef
ac_build_bit_count(struct ac_llvm_context
*ctx
, LLVMValueRef src0
)
2816 LLVMValueRef result
;
2819 bitsize
= ac_get_elem_bits(ctx
, LLVMTypeOf(src0
));
2823 result
= ac_build_intrinsic(ctx
, "llvm.ctpop.i64", ctx
->i64
,
2824 (LLVMValueRef
[]) { src0
}, 1,
2825 AC_FUNC_ATTR_READNONE
);
2827 result
= LLVMBuildTrunc(ctx
->builder
, result
, ctx
->i32
, "");
2830 result
= ac_build_intrinsic(ctx
, "llvm.ctpop.i32", ctx
->i32
,
2831 (LLVMValueRef
[]) { src0
}, 1,
2832 AC_FUNC_ATTR_READNONE
);
2835 result
= ac_build_intrinsic(ctx
, "llvm.ctpop.i16", ctx
->i16
,
2836 (LLVMValueRef
[]) { src0
}, 1,
2837 AC_FUNC_ATTR_READNONE
);
2839 result
= LLVMBuildZExt(ctx
->builder
, result
, ctx
->i32
, "");
2842 result
= ac_build_intrinsic(ctx
, "llvm.ctpop.i8", ctx
->i8
,
2843 (LLVMValueRef
[]) { src0
}, 1,
2844 AC_FUNC_ATTR_READNONE
);
2846 result
= LLVMBuildZExt(ctx
->builder
, result
, ctx
->i32
, "");
2849 unreachable(!"invalid bitsize");
2856 LLVMValueRef
ac_build_bitfield_reverse(struct ac_llvm_context
*ctx
,
2859 LLVMValueRef result
;
2862 bitsize
= ac_get_elem_bits(ctx
, LLVMTypeOf(src0
));
2866 result
= ac_build_intrinsic(ctx
, "llvm.bitreverse.i64", ctx
->i64
,
2867 (LLVMValueRef
[]) { src0
}, 1,
2868 AC_FUNC_ATTR_READNONE
);
2870 result
= LLVMBuildTrunc(ctx
->builder
, result
, ctx
->i32
, "");
2873 result
= ac_build_intrinsic(ctx
, "llvm.bitreverse.i32", ctx
->i32
,
2874 (LLVMValueRef
[]) { src0
}, 1,
2875 AC_FUNC_ATTR_READNONE
);
2878 result
= ac_build_intrinsic(ctx
, "llvm.bitreverse.i16", ctx
->i16
,
2879 (LLVMValueRef
[]) { src0
}, 1,
2880 AC_FUNC_ATTR_READNONE
);
2882 result
= LLVMBuildZExt(ctx
->builder
, result
, ctx
->i32
, "");
2885 result
= ac_build_intrinsic(ctx
, "llvm.bitreverse.i8", ctx
->i8
,
2886 (LLVMValueRef
[]) { src0
}, 1,
2887 AC_FUNC_ATTR_READNONE
);
2889 result
= LLVMBuildZExt(ctx
->builder
, result
, ctx
->i32
, "");
2892 unreachable(!"invalid bitsize");
2899 #define AC_EXP_TARGET 0
2900 #define AC_EXP_ENABLED_CHANNELS 1
2901 #define AC_EXP_OUT0 2
2909 struct ac_vs_exp_chan
2913 enum ac_ir_type type
;
2916 struct ac_vs_exp_inst
{
2919 struct ac_vs_exp_chan chan
[4];
2922 struct ac_vs_exports
{
2924 struct ac_vs_exp_inst exp
[VARYING_SLOT_MAX
];
2927 /* Return true if the PARAM export has been eliminated. */
2928 static bool ac_eliminate_const_output(uint8_t *vs_output_param_offset
,
2929 uint32_t num_outputs
,
2930 struct ac_vs_exp_inst
*exp
)
2932 unsigned i
, default_val
; /* SPI_PS_INPUT_CNTL_i.DEFAULT_VAL */
2933 bool is_zero
[4] = {}, is_one
[4] = {};
2935 for (i
= 0; i
< 4; i
++) {
2936 /* It's a constant expression. Undef outputs are eliminated too. */
2937 if (exp
->chan
[i
].type
== AC_IR_UNDEF
) {
2940 } else if (exp
->chan
[i
].type
== AC_IR_CONST
) {
2941 if (exp
->chan
[i
].const_float
== 0)
2943 else if (exp
->chan
[i
].const_float
== 1)
2946 return false; /* other constant */
2951 /* Only certain combinations of 0 and 1 can be eliminated. */
2952 if (is_zero
[0] && is_zero
[1] && is_zero
[2])
2953 default_val
= is_zero
[3] ? 0 : 1;
2954 else if (is_one
[0] && is_one
[1] && is_one
[2])
2955 default_val
= is_zero
[3] ? 2 : 3;
2959 /* The PARAM export can be represented as DEFAULT_VAL. Kill it. */
2960 LLVMInstructionEraseFromParent(exp
->inst
);
2962 /* Change OFFSET to DEFAULT_VAL. */
2963 for (i
= 0; i
< num_outputs
; i
++) {
2964 if (vs_output_param_offset
[i
] == exp
->offset
) {
2965 vs_output_param_offset
[i
] =
2966 AC_EXP_PARAM_DEFAULT_VAL_0000
+ default_val
;
2973 static bool ac_eliminate_duplicated_output(struct ac_llvm_context
*ctx
,
2974 uint8_t *vs_output_param_offset
,
2975 uint32_t num_outputs
,
2976 struct ac_vs_exports
*processed
,
2977 struct ac_vs_exp_inst
*exp
)
2979 unsigned p
, copy_back_channels
= 0;
2981 /* See if the output is already in the list of processed outputs.
2982 * The LLVMValueRef comparison relies on SSA.
2984 for (p
= 0; p
< processed
->num
; p
++) {
2985 bool different
= false;
2987 for (unsigned j
= 0; j
< 4; j
++) {
2988 struct ac_vs_exp_chan
*c1
= &processed
->exp
[p
].chan
[j
];
2989 struct ac_vs_exp_chan
*c2
= &exp
->chan
[j
];
2991 /* Treat undef as a match. */
2992 if (c2
->type
== AC_IR_UNDEF
)
2995 /* If c1 is undef but c2 isn't, we can copy c2 to c1
2996 * and consider the instruction duplicated.
2998 if (c1
->type
== AC_IR_UNDEF
) {
2999 copy_back_channels
|= 1 << j
;
3003 /* Test whether the channels are not equal. */
3004 if (c1
->type
!= c2
->type
||
3005 (c1
->type
== AC_IR_CONST
&&
3006 c1
->const_float
!= c2
->const_float
) ||
3007 (c1
->type
== AC_IR_VALUE
&&
3008 c1
->value
!= c2
->value
)) {
3016 copy_back_channels
= 0;
3018 if (p
== processed
->num
)
3021 /* If a match was found, but the matching export has undef where the new
3022 * one has a normal value, copy the normal value to the undef channel.
3024 struct ac_vs_exp_inst
*match
= &processed
->exp
[p
];
3026 /* Get current enabled channels mask. */
3027 LLVMValueRef arg
= LLVMGetOperand(match
->inst
, AC_EXP_ENABLED_CHANNELS
);
3028 unsigned enabled_channels
= LLVMConstIntGetZExtValue(arg
);
3030 while (copy_back_channels
) {
3031 unsigned chan
= u_bit_scan(©_back_channels
);
3033 assert(match
->chan
[chan
].type
== AC_IR_UNDEF
);
3034 LLVMSetOperand(match
->inst
, AC_EXP_OUT0
+ chan
,
3035 exp
->chan
[chan
].value
);
3036 match
->chan
[chan
] = exp
->chan
[chan
];
3038 /* Update number of enabled channels because the original mask
3039 * is not always 0xf.
3041 enabled_channels
|= (1 << chan
);
3042 LLVMSetOperand(match
->inst
, AC_EXP_ENABLED_CHANNELS
,
3043 LLVMConstInt(ctx
->i32
, enabled_channels
, 0));
3046 /* The PARAM export is duplicated. Kill it. */
3047 LLVMInstructionEraseFromParent(exp
->inst
);
3049 /* Change OFFSET to the matching export. */
3050 for (unsigned i
= 0; i
< num_outputs
; i
++) {
3051 if (vs_output_param_offset
[i
] == exp
->offset
) {
3052 vs_output_param_offset
[i
] = match
->offset
;
3059 void ac_optimize_vs_outputs(struct ac_llvm_context
*ctx
,
3060 LLVMValueRef main_fn
,
3061 uint8_t *vs_output_param_offset
,
3062 uint32_t num_outputs
,
3063 uint8_t *num_param_exports
)
3065 LLVMBasicBlockRef bb
;
3066 bool removed_any
= false;
3067 struct ac_vs_exports exports
;
3071 /* Process all LLVM instructions. */
3072 bb
= LLVMGetFirstBasicBlock(main_fn
);
3074 LLVMValueRef inst
= LLVMGetFirstInstruction(bb
);
3077 LLVMValueRef cur
= inst
;
3078 inst
= LLVMGetNextInstruction(inst
);
3079 struct ac_vs_exp_inst exp
;
3081 if (LLVMGetInstructionOpcode(cur
) != LLVMCall
)
3084 LLVMValueRef callee
= ac_llvm_get_called_value(cur
);
3086 if (!ac_llvm_is_function(callee
))
3089 const char *name
= LLVMGetValueName(callee
);
3090 unsigned num_args
= LLVMCountParams(callee
);
3092 /* Check if this is an export instruction. */
3093 if ((num_args
!= 9 && num_args
!= 8) ||
3094 (strcmp(name
, "llvm.SI.export") &&
3095 strcmp(name
, "llvm.amdgcn.exp.f32")))
3098 LLVMValueRef arg
= LLVMGetOperand(cur
, AC_EXP_TARGET
);
3099 unsigned target
= LLVMConstIntGetZExtValue(arg
);
3101 if (target
< V_008DFC_SQ_EXP_PARAM
)
3104 target
-= V_008DFC_SQ_EXP_PARAM
;
3106 /* Parse the instruction. */
3107 memset(&exp
, 0, sizeof(exp
));
3108 exp
.offset
= target
;
3111 for (unsigned i
= 0; i
< 4; i
++) {
3112 LLVMValueRef v
= LLVMGetOperand(cur
, AC_EXP_OUT0
+ i
);
3114 exp
.chan
[i
].value
= v
;
3116 if (LLVMIsUndef(v
)) {
3117 exp
.chan
[i
].type
= AC_IR_UNDEF
;
3118 } else if (LLVMIsAConstantFP(v
)) {
3119 LLVMBool loses_info
;
3120 exp
.chan
[i
].type
= AC_IR_CONST
;
3121 exp
.chan
[i
].const_float
=
3122 LLVMConstRealGetDouble(v
, &loses_info
);
3124 exp
.chan
[i
].type
= AC_IR_VALUE
;
3128 /* Eliminate constant and duplicated PARAM exports. */
3129 if (ac_eliminate_const_output(vs_output_param_offset
,
3130 num_outputs
, &exp
) ||
3131 ac_eliminate_duplicated_output(ctx
,
3132 vs_output_param_offset
,
3133 num_outputs
, &exports
,
3137 exports
.exp
[exports
.num
++] = exp
;
3140 bb
= LLVMGetNextBasicBlock(bb
);
3143 /* Remove holes in export memory due to removed PARAM exports.
3144 * This is done by renumbering all PARAM exports.
3147 uint8_t old_offset
[VARYING_SLOT_MAX
];
3150 /* Make a copy of the offsets. We need the old version while
3151 * we are modifying some of them. */
3152 memcpy(old_offset
, vs_output_param_offset
,
3153 sizeof(old_offset
));
3155 for (i
= 0; i
< exports
.num
; i
++) {
3156 unsigned offset
= exports
.exp
[i
].offset
;
3158 /* Update vs_output_param_offset. Multiple outputs can
3159 * have the same offset.
3161 for (out
= 0; out
< num_outputs
; out
++) {
3162 if (old_offset
[out
] == offset
)
3163 vs_output_param_offset
[out
] = i
;
3166 /* Change the PARAM offset in the instruction. */
3167 LLVMSetOperand(exports
.exp
[i
].inst
, AC_EXP_TARGET
,
3168 LLVMConstInt(ctx
->i32
,
3169 V_008DFC_SQ_EXP_PARAM
+ i
, 0));
3171 *num_param_exports
= exports
.num
;
3175 void ac_init_exec_full_mask(struct ac_llvm_context
*ctx
)
3177 LLVMValueRef full_mask
= LLVMConstInt(ctx
->i64
, ~0ull, 0);
3178 ac_build_intrinsic(ctx
,
3179 "llvm.amdgcn.init.exec", ctx
->voidt
,
3180 &full_mask
, 1, AC_FUNC_ATTR_CONVERGENT
);
3183 void ac_declare_lds_as_pointer(struct ac_llvm_context
*ctx
)
3185 unsigned lds_size
= ctx
->chip_class
>= GFX7
? 65536 : 32768;
3186 ctx
->lds
= LLVMBuildIntToPtr(ctx
->builder
, ctx
->i32_0
,
3187 LLVMPointerType(LLVMArrayType(ctx
->i32
, lds_size
/ 4), AC_ADDR_SPACE_LDS
),
3191 LLVMValueRef
ac_lds_load(struct ac_llvm_context
*ctx
,
3192 LLVMValueRef dw_addr
)
3194 return LLVMBuildLoad(ctx
->builder
, ac_build_gep0(ctx
, ctx
->lds
, dw_addr
), "");
3197 void ac_lds_store(struct ac_llvm_context
*ctx
,
3198 LLVMValueRef dw_addr
,
3201 value
= ac_to_integer(ctx
, value
);
3202 ac_build_indexed_store(ctx
, ctx
->lds
,
3206 LLVMValueRef
ac_find_lsb(struct ac_llvm_context
*ctx
,
3207 LLVMTypeRef dst_type
,
3210 unsigned src0_bitsize
= ac_get_elem_bits(ctx
, LLVMTypeOf(src0
));
3211 const char *intrin_name
;
3215 switch (src0_bitsize
) {
3217 intrin_name
= "llvm.cttz.i64";
3222 intrin_name
= "llvm.cttz.i32";
3227 intrin_name
= "llvm.cttz.i16";
3232 intrin_name
= "llvm.cttz.i8";
3237 unreachable(!"invalid bitsize");
3240 LLVMValueRef params
[2] = {
3243 /* The value of 1 means that ffs(x=0) = undef, so LLVM won't
3244 * add special code to check for x=0. The reason is that
3245 * the LLVM behavior for x=0 is different from what we
3246 * need here. However, LLVM also assumes that ffs(x) is
3247 * in [0, 31], but GLSL expects that ffs(0) = -1, so
3248 * a conditional assignment to handle 0 is still required.
3250 * The hardware already implements the correct behavior.
3255 LLVMValueRef lsb
= ac_build_intrinsic(ctx
, intrin_name
, type
,
3257 AC_FUNC_ATTR_READNONE
);
3259 if (src0_bitsize
== 64) {
3260 lsb
= LLVMBuildTrunc(ctx
->builder
, lsb
, ctx
->i32
, "");
3261 } else if (src0_bitsize
< 32) {
3262 lsb
= LLVMBuildSExt(ctx
->builder
, lsb
, ctx
->i32
, "");
3265 /* TODO: We need an intrinsic to skip this conditional. */
3266 /* Check for zero: */
3267 return LLVMBuildSelect(ctx
->builder
, LLVMBuildICmp(ctx
->builder
,
3270 LLVMConstInt(ctx
->i32
, -1, 0), lsb
, "");
3273 LLVMTypeRef
ac_array_in_const_addr_space(LLVMTypeRef elem_type
)
3275 return LLVMPointerType(elem_type
, AC_ADDR_SPACE_CONST
);
3278 LLVMTypeRef
ac_array_in_const32_addr_space(LLVMTypeRef elem_type
)
3280 return LLVMPointerType(elem_type
, AC_ADDR_SPACE_CONST_32BIT
);
3283 static struct ac_llvm_flow
*
3284 get_current_flow(struct ac_llvm_context
*ctx
)
3286 if (ctx
->flow
->depth
> 0)
3287 return &ctx
->flow
->stack
[ctx
->flow
->depth
- 1];
3291 static struct ac_llvm_flow
*
3292 get_innermost_loop(struct ac_llvm_context
*ctx
)
3294 for (unsigned i
= ctx
->flow
->depth
; i
> 0; --i
) {
3295 if (ctx
->flow
->stack
[i
- 1].loop_entry_block
)
3296 return &ctx
->flow
->stack
[i
- 1];
3301 static struct ac_llvm_flow
*
3302 push_flow(struct ac_llvm_context
*ctx
)
3304 struct ac_llvm_flow
*flow
;
3306 if (ctx
->flow
->depth
>= ctx
->flow
->depth_max
) {
3307 unsigned new_max
= MAX2(ctx
->flow
->depth
<< 1,
3308 AC_LLVM_INITIAL_CF_DEPTH
);
3310 ctx
->flow
->stack
= realloc(ctx
->flow
->stack
, new_max
* sizeof(*ctx
->flow
->stack
));
3311 ctx
->flow
->depth_max
= new_max
;
3314 flow
= &ctx
->flow
->stack
[ctx
->flow
->depth
];
3317 flow
->next_block
= NULL
;
3318 flow
->loop_entry_block
= NULL
;
3322 static void set_basicblock_name(LLVMBasicBlockRef bb
, const char *base
,
3326 snprintf(buf
, sizeof(buf
), "%s%d", base
, label_id
);
3327 LLVMSetValueName(LLVMBasicBlockAsValue(bb
), buf
);
3330 /* Append a basic block at the level of the parent flow.
3332 static LLVMBasicBlockRef
append_basic_block(struct ac_llvm_context
*ctx
,
3335 assert(ctx
->flow
->depth
>= 1);
3337 if (ctx
->flow
->depth
>= 2) {
3338 struct ac_llvm_flow
*flow
= &ctx
->flow
->stack
[ctx
->flow
->depth
- 2];
3340 return LLVMInsertBasicBlockInContext(ctx
->context
,
3341 flow
->next_block
, name
);
3344 LLVMValueRef main_fn
=
3345 LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
->builder
));
3346 return LLVMAppendBasicBlockInContext(ctx
->context
, main_fn
, name
);
3349 /* Emit a branch to the given default target for the current block if
3350 * applicable -- that is, if the current block does not already contain a
3351 * branch from a break or continue.
3353 static void emit_default_branch(LLVMBuilderRef builder
,
3354 LLVMBasicBlockRef target
)
3356 if (!LLVMGetBasicBlockTerminator(LLVMGetInsertBlock(builder
)))
3357 LLVMBuildBr(builder
, target
);
3360 void ac_build_bgnloop(struct ac_llvm_context
*ctx
, int label_id
)
3362 struct ac_llvm_flow
*flow
= push_flow(ctx
);
3363 flow
->loop_entry_block
= append_basic_block(ctx
, "LOOP");
3364 flow
->next_block
= append_basic_block(ctx
, "ENDLOOP");
3365 set_basicblock_name(flow
->loop_entry_block
, "loop", label_id
);
3366 LLVMBuildBr(ctx
->builder
, flow
->loop_entry_block
);
3367 LLVMPositionBuilderAtEnd(ctx
->builder
, flow
->loop_entry_block
);
3370 void ac_build_break(struct ac_llvm_context
*ctx
)
3372 struct ac_llvm_flow
*flow
= get_innermost_loop(ctx
);
3373 LLVMBuildBr(ctx
->builder
, flow
->next_block
);
3376 void ac_build_continue(struct ac_llvm_context
*ctx
)
3378 struct ac_llvm_flow
*flow
= get_innermost_loop(ctx
);
3379 LLVMBuildBr(ctx
->builder
, flow
->loop_entry_block
);
3382 void ac_build_else(struct ac_llvm_context
*ctx
, int label_id
)
3384 struct ac_llvm_flow
*current_branch
= get_current_flow(ctx
);
3385 LLVMBasicBlockRef endif_block
;
3387 assert(!current_branch
->loop_entry_block
);
3389 endif_block
= append_basic_block(ctx
, "ENDIF");
3390 emit_default_branch(ctx
->builder
, endif_block
);
3392 LLVMPositionBuilderAtEnd(ctx
->builder
, current_branch
->next_block
);
3393 set_basicblock_name(current_branch
->next_block
, "else", label_id
);
3395 current_branch
->next_block
= endif_block
;
3398 void ac_build_endif(struct ac_llvm_context
*ctx
, int label_id
)
3400 struct ac_llvm_flow
*current_branch
= get_current_flow(ctx
);
3402 assert(!current_branch
->loop_entry_block
);
3404 emit_default_branch(ctx
->builder
, current_branch
->next_block
);
3405 LLVMPositionBuilderAtEnd(ctx
->builder
, current_branch
->next_block
);
3406 set_basicblock_name(current_branch
->next_block
, "endif", label_id
);
3411 void ac_build_endloop(struct ac_llvm_context
*ctx
, int label_id
)
3413 struct ac_llvm_flow
*current_loop
= get_current_flow(ctx
);
3415 assert(current_loop
->loop_entry_block
);
3417 emit_default_branch(ctx
->builder
, current_loop
->loop_entry_block
);
3419 LLVMPositionBuilderAtEnd(ctx
->builder
, current_loop
->next_block
);
3420 set_basicblock_name(current_loop
->next_block
, "endloop", label_id
);
3424 void ac_build_ifcc(struct ac_llvm_context
*ctx
, LLVMValueRef cond
, int label_id
)
3426 struct ac_llvm_flow
*flow
= push_flow(ctx
);
3427 LLVMBasicBlockRef if_block
;
3429 if_block
= append_basic_block(ctx
, "IF");
3430 flow
->next_block
= append_basic_block(ctx
, "ELSE");
3431 set_basicblock_name(if_block
, "if", label_id
);
3432 LLVMBuildCondBr(ctx
->builder
, cond
, if_block
, flow
->next_block
);
3433 LLVMPositionBuilderAtEnd(ctx
->builder
, if_block
);
3436 void ac_build_if(struct ac_llvm_context
*ctx
, LLVMValueRef value
,
3439 LLVMValueRef cond
= LLVMBuildFCmp(ctx
->builder
, LLVMRealUNE
,
3440 value
, ctx
->f32_0
, "");
3441 ac_build_ifcc(ctx
, cond
, label_id
);
3444 void ac_build_uif(struct ac_llvm_context
*ctx
, LLVMValueRef value
,
3447 LLVMValueRef cond
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
3448 ac_to_integer(ctx
, value
),
3450 ac_build_ifcc(ctx
, cond
, label_id
);
3453 LLVMValueRef
ac_build_alloca_undef(struct ac_llvm_context
*ac
, LLVMTypeRef type
,
3456 LLVMBuilderRef builder
= ac
->builder
;
3457 LLVMBasicBlockRef current_block
= LLVMGetInsertBlock(builder
);
3458 LLVMValueRef function
= LLVMGetBasicBlockParent(current_block
);
3459 LLVMBasicBlockRef first_block
= LLVMGetEntryBasicBlock(function
);
3460 LLVMValueRef first_instr
= LLVMGetFirstInstruction(first_block
);
3461 LLVMBuilderRef first_builder
= LLVMCreateBuilderInContext(ac
->context
);
3465 LLVMPositionBuilderBefore(first_builder
, first_instr
);
3467 LLVMPositionBuilderAtEnd(first_builder
, first_block
);
3470 res
= LLVMBuildAlloca(first_builder
, type
, name
);
3471 LLVMDisposeBuilder(first_builder
);
3475 LLVMValueRef
ac_build_alloca(struct ac_llvm_context
*ac
,
3476 LLVMTypeRef type
, const char *name
)
3478 LLVMValueRef ptr
= ac_build_alloca_undef(ac
, type
, name
);
3479 LLVMBuildStore(ac
->builder
, LLVMConstNull(type
), ptr
);
3483 LLVMValueRef
ac_cast_ptr(struct ac_llvm_context
*ctx
, LLVMValueRef ptr
,
3486 int addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
3487 return LLVMBuildBitCast(ctx
->builder
, ptr
,
3488 LLVMPointerType(type
, addr_space
), "");
3491 LLVMValueRef
ac_trim_vector(struct ac_llvm_context
*ctx
, LLVMValueRef value
,
3494 unsigned num_components
= ac_get_llvm_num_components(value
);
3495 if (count
== num_components
)
3498 LLVMValueRef masks
[MAX2(count
, 2)];
3499 masks
[0] = ctx
->i32_0
;
3500 masks
[1] = ctx
->i32_1
;
3501 for (unsigned i
= 2; i
< count
; i
++)
3502 masks
[i
] = LLVMConstInt(ctx
->i32
, i
, false);
3505 return LLVMBuildExtractElement(ctx
->builder
, value
, masks
[0],
3508 LLVMValueRef swizzle
= LLVMConstVector(masks
, count
);
3509 return LLVMBuildShuffleVector(ctx
->builder
, value
, value
, swizzle
, "");
3512 LLVMValueRef
ac_unpack_param(struct ac_llvm_context
*ctx
, LLVMValueRef param
,
3513 unsigned rshift
, unsigned bitwidth
)
3515 LLVMValueRef value
= param
;
3517 value
= LLVMBuildLShr(ctx
->builder
, value
,
3518 LLVMConstInt(ctx
->i32
, rshift
, false), "");
3520 if (rshift
+ bitwidth
< 32) {
3521 unsigned mask
= (1 << bitwidth
) - 1;
3522 value
= LLVMBuildAnd(ctx
->builder
, value
,
3523 LLVMConstInt(ctx
->i32
, mask
, false), "");
3528 /* Adjust the sample index according to FMASK.
3530 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
3531 * which is the identity mapping. Each nibble says which physical sample
3532 * should be fetched to get that sample.
3534 * For example, 0x11111100 means there are only 2 samples stored and
3535 * the second sample covers 3/4 of the pixel. When reading samples 0
3536 * and 1, return physical sample 0 (determined by the first two 0s
3537 * in FMASK), otherwise return physical sample 1.
3539 * The sample index should be adjusted as follows:
3540 * addr[sample_index] = (fmask >> (addr[sample_index] * 4)) & 0xF;
3542 void ac_apply_fmask_to_sample(struct ac_llvm_context
*ac
, LLVMValueRef fmask
,
3543 LLVMValueRef
*addr
, bool is_array_tex
)
3545 struct ac_image_args fmask_load
= {};
3546 fmask_load
.opcode
= ac_image_load
;
3547 fmask_load
.resource
= fmask
;
3548 fmask_load
.dmask
= 0xf;
3549 fmask_load
.dim
= is_array_tex
? ac_image_2darray
: ac_image_2d
;
3550 fmask_load
.attributes
= AC_FUNC_ATTR_READNONE
;
3552 fmask_load
.coords
[0] = addr
[0];
3553 fmask_load
.coords
[1] = addr
[1];
3555 fmask_load
.coords
[2] = addr
[2];
3557 LLVMValueRef fmask_value
= ac_build_image_opcode(ac
, &fmask_load
);
3558 fmask_value
= LLVMBuildExtractElement(ac
->builder
, fmask_value
,
3561 /* Apply the formula. */
3562 unsigned sample_chan
= is_array_tex
? 3 : 2;
3563 LLVMValueRef final_sample
;
3564 final_sample
= LLVMBuildMul(ac
->builder
, addr
[sample_chan
],
3565 LLVMConstInt(ac
->i32
, 4, 0), "");
3566 final_sample
= LLVMBuildLShr(ac
->builder
, fmask_value
, final_sample
, "");
3567 /* Mask the sample index by 0x7, because 0x8 means an unknown value
3568 * with EQAA, so those will map to 0. */
3569 final_sample
= LLVMBuildAnd(ac
->builder
, final_sample
,
3570 LLVMConstInt(ac
->i32
, 0x7, 0), "");
3572 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
3573 * resource descriptor is 0 (invalid).
3576 tmp
= LLVMBuildBitCast(ac
->builder
, fmask
, ac
->v8i32
, "");
3577 tmp
= LLVMBuildExtractElement(ac
->builder
, tmp
, ac
->i32_1
, "");
3578 tmp
= LLVMBuildICmp(ac
->builder
, LLVMIntNE
, tmp
, ac
->i32_0
, "");
3580 /* Replace the MSAA sample index. */
3581 addr
[sample_chan
] = LLVMBuildSelect(ac
->builder
, tmp
, final_sample
,
3582 addr
[sample_chan
], "");
3586 _ac_build_readlane(struct ac_llvm_context
*ctx
, LLVMValueRef src
, LLVMValueRef lane
)
3588 ac_build_optimization_barrier(ctx
, &src
);
3589 return ac_build_intrinsic(ctx
,
3590 lane
== NULL
? "llvm.amdgcn.readfirstlane" : "llvm.amdgcn.readlane",
3591 LLVMTypeOf(src
), (LLVMValueRef
[]) {
3593 lane
== NULL
? 1 : 2,
3594 AC_FUNC_ATTR_READNONE
|
3595 AC_FUNC_ATTR_CONVERGENT
);
3599 * Builds the "llvm.amdgcn.readlane" or "llvm.amdgcn.readfirstlane" intrinsic.
3602 * @param lane - id of the lane or NULL for the first active lane
3603 * @return value of the lane
3606 ac_build_readlane(struct ac_llvm_context
*ctx
, LLVMValueRef src
, LLVMValueRef lane
)
3608 LLVMTypeRef src_type
= LLVMTypeOf(src
);
3609 src
= ac_to_integer(ctx
, src
);
3610 unsigned bits
= LLVMGetIntTypeWidth(LLVMTypeOf(src
));
3614 ret
= _ac_build_readlane(ctx
, src
, lane
);
3616 assert(bits
% 32 == 0);
3617 LLVMTypeRef vec_type
= LLVMVectorType(ctx
->i32
, bits
/ 32);
3618 LLVMValueRef src_vector
=
3619 LLVMBuildBitCast(ctx
->builder
, src
, vec_type
, "");
3620 ret
= LLVMGetUndef(vec_type
);
3621 for (unsigned i
= 0; i
< bits
/ 32; i
++) {
3622 src
= LLVMBuildExtractElement(ctx
->builder
, src_vector
,
3623 LLVMConstInt(ctx
->i32
, i
, 0), "");
3624 LLVMValueRef ret_comp
= _ac_build_readlane(ctx
, src
, lane
);
3625 ret
= LLVMBuildInsertElement(ctx
->builder
, ret
, ret_comp
,
3626 LLVMConstInt(ctx
->i32
, i
, 0), "");
3629 if (LLVMGetTypeKind(src_type
) == LLVMPointerTypeKind
)
3630 return LLVMBuildIntToPtr(ctx
->builder
, ret
, src_type
, "");
3631 return LLVMBuildBitCast(ctx
->builder
, ret
, src_type
, "");
3635 ac_build_writelane(struct ac_llvm_context
*ctx
, LLVMValueRef src
, LLVMValueRef value
, LLVMValueRef lane
)
3637 return ac_build_intrinsic(ctx
, "llvm.amdgcn.writelane", ctx
->i32
,
3638 (LLVMValueRef
[]) {value
, lane
, src
}, 3,
3639 AC_FUNC_ATTR_READNONE
| AC_FUNC_ATTR_CONVERGENT
);
3643 ac_build_mbcnt(struct ac_llvm_context
*ctx
, LLVMValueRef mask
)
3645 if (ctx
->wave_size
== 32) {
3646 return ac_build_intrinsic(ctx
, "llvm.amdgcn.mbcnt.lo", ctx
->i32
,
3647 (LLVMValueRef
[]) { mask
, ctx
->i32_0
},
3648 2, AC_FUNC_ATTR_READNONE
);
3650 LLVMValueRef mask_vec
= LLVMBuildBitCast(ctx
->builder
, mask
,
3651 LLVMVectorType(ctx
->i32
, 2),
3653 LLVMValueRef mask_lo
= LLVMBuildExtractElement(ctx
->builder
, mask_vec
,
3655 LLVMValueRef mask_hi
= LLVMBuildExtractElement(ctx
->builder
, mask_vec
,
3658 ac_build_intrinsic(ctx
, "llvm.amdgcn.mbcnt.lo", ctx
->i32
,
3659 (LLVMValueRef
[]) { mask_lo
, ctx
->i32_0
},
3660 2, AC_FUNC_ATTR_READNONE
);
3661 val
= ac_build_intrinsic(ctx
, "llvm.amdgcn.mbcnt.hi", ctx
->i32
,
3662 (LLVMValueRef
[]) { mask_hi
, val
},
3663 2, AC_FUNC_ATTR_READNONE
);
3668 _dpp_quad_perm
= 0x000,
3669 _dpp_row_sl
= 0x100,
3670 _dpp_row_sr
= 0x110,
3671 _dpp_row_rr
= 0x120,
3676 dpp_row_mirror
= 0x140,
3677 dpp_row_half_mirror
= 0x141,
3678 dpp_row_bcast15
= 0x142,
3679 dpp_row_bcast31
= 0x143
3682 static inline enum dpp_ctrl
3683 dpp_quad_perm(unsigned lane0
, unsigned lane1
, unsigned lane2
, unsigned lane3
)
3685 assert(lane0
< 4 && lane1
< 4 && lane2
< 4 && lane3
< 4);
3686 return _dpp_quad_perm
| lane0
| (lane1
<< 2) | (lane2
<< 4) | (lane3
<< 6);
3689 static inline enum dpp_ctrl
3690 dpp_row_sl(unsigned amount
)
3692 assert(amount
> 0 && amount
< 16);
3693 return _dpp_row_sl
| amount
;
3696 static inline enum dpp_ctrl
3697 dpp_row_sr(unsigned amount
)
3699 assert(amount
> 0 && amount
< 16);
3700 return _dpp_row_sr
| amount
;
3704 _ac_build_dpp(struct ac_llvm_context
*ctx
, LLVMValueRef old
, LLVMValueRef src
,
3705 enum dpp_ctrl dpp_ctrl
, unsigned row_mask
, unsigned bank_mask
,
3708 return ac_build_intrinsic(ctx
, "llvm.amdgcn.update.dpp.i32",
3712 LLVMConstInt(ctx
->i32
, dpp_ctrl
, 0),
3713 LLVMConstInt(ctx
->i32
, row_mask
, 0),
3714 LLVMConstInt(ctx
->i32
, bank_mask
, 0),
3715 LLVMConstInt(ctx
->i1
, bound_ctrl
, 0) },
3716 6, AC_FUNC_ATTR_READNONE
| AC_FUNC_ATTR_CONVERGENT
);
3720 ac_build_dpp(struct ac_llvm_context
*ctx
, LLVMValueRef old
, LLVMValueRef src
,
3721 enum dpp_ctrl dpp_ctrl
, unsigned row_mask
, unsigned bank_mask
,
3724 LLVMTypeRef src_type
= LLVMTypeOf(src
);
3725 src
= ac_to_integer(ctx
, src
);
3726 old
= ac_to_integer(ctx
, old
);
3727 unsigned bits
= LLVMGetIntTypeWidth(LLVMTypeOf(src
));
3730 ret
= _ac_build_dpp(ctx
, old
, src
, dpp_ctrl
, row_mask
,
3731 bank_mask
, bound_ctrl
);
3733 assert(bits
% 32 == 0);
3734 LLVMTypeRef vec_type
= LLVMVectorType(ctx
->i32
, bits
/ 32);
3735 LLVMValueRef src_vector
=
3736 LLVMBuildBitCast(ctx
->builder
, src
, vec_type
, "");
3737 LLVMValueRef old_vector
=
3738 LLVMBuildBitCast(ctx
->builder
, old
, vec_type
, "");
3739 ret
= LLVMGetUndef(vec_type
);
3740 for (unsigned i
= 0; i
< bits
/ 32; i
++) {
3741 src
= LLVMBuildExtractElement(ctx
->builder
, src_vector
,
3742 LLVMConstInt(ctx
->i32
, i
,
3744 old
= LLVMBuildExtractElement(ctx
->builder
, old_vector
,
3745 LLVMConstInt(ctx
->i32
, i
,
3747 LLVMValueRef ret_comp
= _ac_build_dpp(ctx
, old
, src
,
3752 ret
= LLVMBuildInsertElement(ctx
->builder
, ret
,
3754 LLVMConstInt(ctx
->i32
, i
,
3758 return LLVMBuildBitCast(ctx
->builder
, ret
, src_type
, "");
3762 _ac_build_permlane16(struct ac_llvm_context
*ctx
, LLVMValueRef src
, uint64_t sel
,
3763 bool exchange_rows
, bool bound_ctrl
)
3765 LLVMValueRef args
[6] = {
3768 LLVMConstInt(ctx
->i32
, sel
, false),
3769 LLVMConstInt(ctx
->i32
, sel
>> 32, false),
3770 ctx
->i1true
, /* fi */
3771 bound_ctrl
? ctx
->i1true
: ctx
->i1false
,
3773 return ac_build_intrinsic(ctx
, exchange_rows
? "llvm.amdgcn.permlanex16"
3774 : "llvm.amdgcn.permlane16",
3776 AC_FUNC_ATTR_READNONE
| AC_FUNC_ATTR_CONVERGENT
);
3780 ac_build_permlane16(struct ac_llvm_context
*ctx
, LLVMValueRef src
, uint64_t sel
,
3781 bool exchange_rows
, bool bound_ctrl
)
3783 LLVMTypeRef src_type
= LLVMTypeOf(src
);
3784 src
= ac_to_integer(ctx
, src
);
3785 unsigned bits
= LLVMGetIntTypeWidth(LLVMTypeOf(src
));
3788 ret
= _ac_build_permlane16(ctx
, src
, sel
, exchange_rows
,
3791 assert(bits
% 32 == 0);
3792 LLVMTypeRef vec_type
= LLVMVectorType(ctx
->i32
, bits
/ 32);
3793 LLVMValueRef src_vector
=
3794 LLVMBuildBitCast(ctx
->builder
, src
, vec_type
, "");
3795 ret
= LLVMGetUndef(vec_type
);
3796 for (unsigned i
= 0; i
< bits
/ 32; i
++) {
3797 src
= LLVMBuildExtractElement(ctx
->builder
, src_vector
,
3798 LLVMConstInt(ctx
->i32
, i
,
3800 LLVMValueRef ret_comp
=
3801 _ac_build_permlane16(ctx
, src
, sel
,
3804 ret
= LLVMBuildInsertElement(ctx
->builder
, ret
,
3806 LLVMConstInt(ctx
->i32
, i
,
3810 return LLVMBuildBitCast(ctx
->builder
, ret
, src_type
, "");
3813 static inline unsigned
3814 ds_pattern_bitmode(unsigned and_mask
, unsigned or_mask
, unsigned xor_mask
)
3816 assert(and_mask
< 32 && or_mask
< 32 && xor_mask
< 32);
3817 return and_mask
| (or_mask
<< 5) | (xor_mask
<< 10);
3821 _ac_build_ds_swizzle(struct ac_llvm_context
*ctx
, LLVMValueRef src
, unsigned mask
)
3823 return ac_build_intrinsic(ctx
, "llvm.amdgcn.ds.swizzle",
3824 LLVMTypeOf(src
), (LLVMValueRef
[]) {
3825 src
, LLVMConstInt(ctx
->i32
, mask
, 0) },
3826 2, AC_FUNC_ATTR_READNONE
| AC_FUNC_ATTR_CONVERGENT
);
3830 ac_build_ds_swizzle(struct ac_llvm_context
*ctx
, LLVMValueRef src
, unsigned mask
)
3832 LLVMTypeRef src_type
= LLVMTypeOf(src
);
3833 src
= ac_to_integer(ctx
, src
);
3834 unsigned bits
= LLVMGetIntTypeWidth(LLVMTypeOf(src
));
3837 ret
= _ac_build_ds_swizzle(ctx
, src
, mask
);
3839 assert(bits
% 32 == 0);
3840 LLVMTypeRef vec_type
= LLVMVectorType(ctx
->i32
, bits
/ 32);
3841 LLVMValueRef src_vector
=
3842 LLVMBuildBitCast(ctx
->builder
, src
, vec_type
, "");
3843 ret
= LLVMGetUndef(vec_type
);
3844 for (unsigned i
= 0; i
< bits
/ 32; i
++) {
3845 src
= LLVMBuildExtractElement(ctx
->builder
, src_vector
,
3846 LLVMConstInt(ctx
->i32
, i
,
3848 LLVMValueRef ret_comp
= _ac_build_ds_swizzle(ctx
, src
,
3850 ret
= LLVMBuildInsertElement(ctx
->builder
, ret
,
3852 LLVMConstInt(ctx
->i32
, i
,
3856 return LLVMBuildBitCast(ctx
->builder
, ret
, src_type
, "");
3860 ac_build_wwm(struct ac_llvm_context
*ctx
, LLVMValueRef src
)
3862 char name
[32], type
[8];
3863 ac_build_type_name_for_intr(LLVMTypeOf(src
), type
, sizeof(type
));
3864 snprintf(name
, sizeof(name
), "llvm.amdgcn.wwm.%s", type
);
3865 return ac_build_intrinsic(ctx
, name
, LLVMTypeOf(src
),
3866 (LLVMValueRef
[]) { src
}, 1,
3867 AC_FUNC_ATTR_READNONE
);
3871 ac_build_set_inactive(struct ac_llvm_context
*ctx
, LLVMValueRef src
,
3872 LLVMValueRef inactive
)
3874 char name
[33], type
[8];
3875 LLVMTypeRef src_type
= LLVMTypeOf(src
);
3876 src
= ac_to_integer(ctx
, src
);
3877 inactive
= ac_to_integer(ctx
, inactive
);
3878 ac_build_type_name_for_intr(LLVMTypeOf(src
), type
, sizeof(type
));
3879 snprintf(name
, sizeof(name
), "llvm.amdgcn.set.inactive.%s", type
);
3881 ac_build_intrinsic(ctx
, name
,
3882 LLVMTypeOf(src
), (LLVMValueRef
[]) {
3884 AC_FUNC_ATTR_READNONE
|
3885 AC_FUNC_ATTR_CONVERGENT
);
3886 return LLVMBuildBitCast(ctx
->builder
, ret
, src_type
, "");
3890 get_reduction_identity(struct ac_llvm_context
*ctx
, nir_op op
, unsigned type_size
)
3892 if (type_size
== 4) {
3894 case nir_op_iadd
: return ctx
->i32_0
;
3895 case nir_op_fadd
: return ctx
->f32_0
;
3896 case nir_op_imul
: return ctx
->i32_1
;
3897 case nir_op_fmul
: return ctx
->f32_1
;
3898 case nir_op_imin
: return LLVMConstInt(ctx
->i32
, INT32_MAX
, 0);
3899 case nir_op_umin
: return LLVMConstInt(ctx
->i32
, UINT32_MAX
, 0);
3900 case nir_op_fmin
: return LLVMConstReal(ctx
->f32
, INFINITY
);
3901 case nir_op_imax
: return LLVMConstInt(ctx
->i32
, INT32_MIN
, 0);
3902 case nir_op_umax
: return ctx
->i32_0
;
3903 case nir_op_fmax
: return LLVMConstReal(ctx
->f32
, -INFINITY
);
3904 case nir_op_iand
: return LLVMConstInt(ctx
->i32
, -1, 0);
3905 case nir_op_ior
: return ctx
->i32_0
;
3906 case nir_op_ixor
: return ctx
->i32_0
;
3908 unreachable("bad reduction intrinsic");
3910 } else { /* type_size == 64bit */
3912 case nir_op_iadd
: return ctx
->i64_0
;
3913 case nir_op_fadd
: return ctx
->f64_0
;
3914 case nir_op_imul
: return ctx
->i64_1
;
3915 case nir_op_fmul
: return ctx
->f64_1
;
3916 case nir_op_imin
: return LLVMConstInt(ctx
->i64
, INT64_MAX
, 0);
3917 case nir_op_umin
: return LLVMConstInt(ctx
->i64
, UINT64_MAX
, 0);
3918 case nir_op_fmin
: return LLVMConstReal(ctx
->f64
, INFINITY
);
3919 case nir_op_imax
: return LLVMConstInt(ctx
->i64
, INT64_MIN
, 0);
3920 case nir_op_umax
: return ctx
->i64_0
;
3921 case nir_op_fmax
: return LLVMConstReal(ctx
->f64
, -INFINITY
);
3922 case nir_op_iand
: return LLVMConstInt(ctx
->i64
, -1, 0);
3923 case nir_op_ior
: return ctx
->i64_0
;
3924 case nir_op_ixor
: return ctx
->i64_0
;
3926 unreachable("bad reduction intrinsic");
3932 ac_build_alu_op(struct ac_llvm_context
*ctx
, LLVMValueRef lhs
, LLVMValueRef rhs
, nir_op op
)
3934 bool _64bit
= ac_get_type_size(LLVMTypeOf(lhs
)) == 8;
3936 case nir_op_iadd
: return LLVMBuildAdd(ctx
->builder
, lhs
, rhs
, "");
3937 case nir_op_fadd
: return LLVMBuildFAdd(ctx
->builder
, lhs
, rhs
, "");
3938 case nir_op_imul
: return LLVMBuildMul(ctx
->builder
, lhs
, rhs
, "");
3939 case nir_op_fmul
: return LLVMBuildFMul(ctx
->builder
, lhs
, rhs
, "");
3940 case nir_op_imin
: return LLVMBuildSelect(ctx
->builder
,
3941 LLVMBuildICmp(ctx
->builder
, LLVMIntSLT
, lhs
, rhs
, ""),
3943 case nir_op_umin
: return LLVMBuildSelect(ctx
->builder
,
3944 LLVMBuildICmp(ctx
->builder
, LLVMIntULT
, lhs
, rhs
, ""),
3946 case nir_op_fmin
: return ac_build_intrinsic(ctx
,
3947 _64bit
? "llvm.minnum.f64" : "llvm.minnum.f32",
3948 _64bit
? ctx
->f64
: ctx
->f32
,
3949 (LLVMValueRef
[]){lhs
, rhs
}, 2, AC_FUNC_ATTR_READNONE
);
3950 case nir_op_imax
: return LLVMBuildSelect(ctx
->builder
,
3951 LLVMBuildICmp(ctx
->builder
, LLVMIntSGT
, lhs
, rhs
, ""),
3953 case nir_op_umax
: return LLVMBuildSelect(ctx
->builder
,
3954 LLVMBuildICmp(ctx
->builder
, LLVMIntUGT
, lhs
, rhs
, ""),
3956 case nir_op_fmax
: return ac_build_intrinsic(ctx
,
3957 _64bit
? "llvm.maxnum.f64" : "llvm.maxnum.f32",
3958 _64bit
? ctx
->f64
: ctx
->f32
,
3959 (LLVMValueRef
[]){lhs
, rhs
}, 2, AC_FUNC_ATTR_READNONE
);
3960 case nir_op_iand
: return LLVMBuildAnd(ctx
->builder
, lhs
, rhs
, "");
3961 case nir_op_ior
: return LLVMBuildOr(ctx
->builder
, lhs
, rhs
, "");
3962 case nir_op_ixor
: return LLVMBuildXor(ctx
->builder
, lhs
, rhs
, "");
3964 unreachable("bad reduction intrinsic");
3969 * \param maxprefix specifies that the result only needs to be correct for a
3970 * prefix of this many threads
3972 * TODO: add inclusive and excluse scan functions for GFX6.
3975 ac_build_scan(struct ac_llvm_context
*ctx
, nir_op op
, LLVMValueRef src
, LLVMValueRef identity
,
3976 unsigned maxprefix
, bool inclusive
)
3978 LLVMValueRef result
, tmp
;
3980 if (ctx
->chip_class
>= GFX10
) {
3981 result
= inclusive
? src
: identity
;
3984 src
= ac_build_dpp(ctx
, identity
, src
, dpp_wf_sr1
, 0xf, 0xf, false);
3989 tmp
= ac_build_dpp(ctx
, identity
, src
, dpp_row_sr(1), 0xf, 0xf, false);
3990 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
3993 tmp
= ac_build_dpp(ctx
, identity
, src
, dpp_row_sr(2), 0xf, 0xf, false);
3994 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
3997 tmp
= ac_build_dpp(ctx
, identity
, src
, dpp_row_sr(3), 0xf, 0xf, false);
3998 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
4001 tmp
= ac_build_dpp(ctx
, identity
, result
, dpp_row_sr(4), 0xf, 0xe, false);
4002 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
4005 tmp
= ac_build_dpp(ctx
, identity
, result
, dpp_row_sr(8), 0xf, 0xc, false);
4006 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
4007 if (maxprefix
<= 16)
4010 if (ctx
->chip_class
>= GFX10
) {
4011 /* dpp_row_bcast{15,31} are not supported on gfx10. */
4012 LLVMBuilderRef builder
= ctx
->builder
;
4013 LLVMValueRef tid
= ac_get_thread_id(ctx
);
4015 /* TODO-GFX10: Can we get better code-gen by putting this into
4016 * a branch so that LLVM generates EXEC mask manipulations? */
4020 tmp
= ac_build_alu_op(ctx
, result
, src
, op
);
4021 tmp
= ac_build_permlane16(ctx
, tmp
, ~(uint64_t)0, true, false);
4022 tmp
= ac_build_alu_op(ctx
, result
, tmp
, op
);
4023 cc
= LLVMBuildAnd(builder
, tid
, LLVMConstInt(ctx
->i32
, 16, false), "");
4024 cc
= LLVMBuildICmp(builder
, LLVMIntNE
, cc
, ctx
->i32_0
, "");
4025 result
= LLVMBuildSelect(builder
, cc
, tmp
, result
, "");
4026 if (maxprefix
<= 32)
4032 tmp
= ac_build_alu_op(ctx
, result
, src
, op
);
4033 tmp
= ac_build_readlane(ctx
, tmp
, LLVMConstInt(ctx
->i32
, 31, false));
4034 tmp
= ac_build_alu_op(ctx
, result
, tmp
, op
);
4035 cc
= LLVMBuildICmp(builder
, LLVMIntUGE
, tid
,
4036 LLVMConstInt(ctx
->i32
, 32, false), "");
4037 result
= LLVMBuildSelect(builder
, cc
, tmp
, result
, "");
4041 tmp
= ac_build_dpp(ctx
, identity
, result
, dpp_row_bcast15
, 0xa, 0xf, false);
4042 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
4043 if (maxprefix
<= 32)
4045 tmp
= ac_build_dpp(ctx
, identity
, result
, dpp_row_bcast31
, 0xc, 0xf, false);
4046 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
4051 ac_build_inclusive_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
);
4060 result
= LLVMBuildAdd(builder
, result
, src
, "");
4064 ac_build_optimization_barrier(ctx
, &src
);
4066 LLVMValueRef identity
=
4067 get_reduction_identity(ctx
, op
, ac_get_type_size(LLVMTypeOf(src
)));
4068 result
= LLVMBuildBitCast(ctx
->builder
, ac_build_set_inactive(ctx
, src
, identity
),
4069 LLVMTypeOf(identity
), "");
4070 result
= ac_build_scan(ctx
, op
, result
, identity
, ctx
->wave_size
, true);
4072 return ac_build_wwm(ctx
, result
);
4076 ac_build_exclusive_scan(struct ac_llvm_context
*ctx
, LLVMValueRef src
, nir_op op
)
4078 LLVMValueRef result
;
4080 if (LLVMTypeOf(src
) == ctx
->i1
&& op
== nir_op_iadd
) {
4081 LLVMBuilderRef builder
= ctx
->builder
;
4082 src
= LLVMBuildZExt(builder
, src
, ctx
->i32
, "");
4083 result
= ac_build_ballot(ctx
, src
);
4084 result
= ac_build_mbcnt(ctx
, result
);
4088 ac_build_optimization_barrier(ctx
, &src
);
4090 LLVMValueRef identity
=
4091 get_reduction_identity(ctx
, op
, ac_get_type_size(LLVMTypeOf(src
)));
4092 result
= LLVMBuildBitCast(ctx
->builder
, ac_build_set_inactive(ctx
, src
, identity
),
4093 LLVMTypeOf(identity
), "");
4094 result
= ac_build_scan(ctx
, op
, result
, identity
, ctx
->wave_size
, false);
4096 return ac_build_wwm(ctx
, result
);
4100 ac_build_reduce(struct ac_llvm_context
*ctx
, LLVMValueRef src
, nir_op op
, unsigned cluster_size
)
4102 if (cluster_size
== 1) return src
;
4103 ac_build_optimization_barrier(ctx
, &src
);
4104 LLVMValueRef result
, swap
;
4105 LLVMValueRef identity
= get_reduction_identity(ctx
, op
,
4106 ac_get_type_size(LLVMTypeOf(src
)));
4107 result
= LLVMBuildBitCast(ctx
->builder
,
4108 ac_build_set_inactive(ctx
, src
, identity
),
4109 LLVMTypeOf(identity
), "");
4110 swap
= ac_build_quad_swizzle(ctx
, result
, 1, 0, 3, 2);
4111 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
4112 if (cluster_size
== 2) return ac_build_wwm(ctx
, result
);
4114 swap
= ac_build_quad_swizzle(ctx
, result
, 2, 3, 0, 1);
4115 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
4116 if (cluster_size
== 4) return ac_build_wwm(ctx
, result
);
4118 if (ctx
->chip_class
>= GFX8
)
4119 swap
= ac_build_dpp(ctx
, identity
, result
, dpp_row_half_mirror
, 0xf, 0xf, false);
4121 swap
= ac_build_ds_swizzle(ctx
, result
, ds_pattern_bitmode(0x1f, 0, 0x04));
4122 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
4123 if (cluster_size
== 8) return ac_build_wwm(ctx
, result
);
4125 if (ctx
->chip_class
>= GFX8
)
4126 swap
= ac_build_dpp(ctx
, identity
, result
, dpp_row_mirror
, 0xf, 0xf, false);
4128 swap
= ac_build_ds_swizzle(ctx
, result
, ds_pattern_bitmode(0x1f, 0, 0x08));
4129 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
4130 if (cluster_size
== 16) return ac_build_wwm(ctx
, result
);
4132 if (ctx
->chip_class
>= GFX10
)
4133 swap
= ac_build_permlane16(ctx
, result
, 0, true, false);
4134 else if (ctx
->chip_class
>= GFX8
&& cluster_size
!= 32)
4135 swap
= ac_build_dpp(ctx
, identity
, result
, dpp_row_bcast15
, 0xa, 0xf, false);
4137 swap
= ac_build_ds_swizzle(ctx
, result
, ds_pattern_bitmode(0x1f, 0, 0x10));
4138 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
4139 if (cluster_size
== 32) return ac_build_wwm(ctx
, result
);
4141 if (ctx
->chip_class
>= GFX8
) {
4142 if (ctx
->chip_class
>= GFX10
)
4143 swap
= ac_build_readlane(ctx
, result
, LLVMConstInt(ctx
->i32
, 31, false));
4145 swap
= ac_build_dpp(ctx
, identity
, result
, dpp_row_bcast31
, 0xc, 0xf, false);
4146 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
4147 result
= ac_build_readlane(ctx
, result
, LLVMConstInt(ctx
->i32
, 63, 0));
4148 return ac_build_wwm(ctx
, result
);
4150 swap
= ac_build_readlane(ctx
, result
, ctx
->i32_0
);
4151 result
= ac_build_readlane(ctx
, result
, LLVMConstInt(ctx
->i32
, 32, 0));
4152 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
4153 return ac_build_wwm(ctx
, result
);
4158 * "Top half" of a scan that reduces per-wave values across an entire
4161 * The source value must be present in the highest lane of the wave, and the
4162 * highest lane must be live.
4165 ac_build_wg_wavescan_top(struct ac_llvm_context
*ctx
, struct ac_wg_scan
*ws
)
4167 if (ws
->maxwaves
<= 1)
4170 const LLVMValueRef last_lane
= LLVMConstInt(ctx
->i32
, ctx
->wave_size
- 1, false);
4171 LLVMBuilderRef builder
= ctx
->builder
;
4172 LLVMValueRef tid
= ac_get_thread_id(ctx
);
4175 tmp
= LLVMBuildICmp(builder
, LLVMIntEQ
, tid
, last_lane
, "");
4176 ac_build_ifcc(ctx
, tmp
, 1000);
4177 LLVMBuildStore(builder
, ws
->src
, LLVMBuildGEP(builder
, ws
->scratch
, &ws
->waveidx
, 1, ""));
4178 ac_build_endif(ctx
, 1000);
4182 * "Bottom half" of a scan that reduces per-wave values across an entire
4185 * The caller must place a barrier between the top and bottom halves.
4188 ac_build_wg_wavescan_bottom(struct ac_llvm_context
*ctx
, struct ac_wg_scan
*ws
)
4190 const LLVMTypeRef type
= LLVMTypeOf(ws
->src
);
4191 const LLVMValueRef identity
=
4192 get_reduction_identity(ctx
, ws
->op
, ac_get_type_size(type
));
4194 if (ws
->maxwaves
<= 1) {
4195 ws
->result_reduce
= ws
->src
;
4196 ws
->result_inclusive
= ws
->src
;
4197 ws
->result_exclusive
= identity
;
4200 assert(ws
->maxwaves
<= 32);
4202 LLVMBuilderRef builder
= ctx
->builder
;
4203 LLVMValueRef tid
= ac_get_thread_id(ctx
);
4204 LLVMBasicBlockRef bbs
[2];
4205 LLVMValueRef phivalues_scan
[2];
4206 LLVMValueRef tmp
, tmp2
;
4208 bbs
[0] = LLVMGetInsertBlock(builder
);
4209 phivalues_scan
[0] = LLVMGetUndef(type
);
4211 if (ws
->enable_reduce
)
4212 tmp
= LLVMBuildICmp(builder
, LLVMIntULT
, tid
, ws
->numwaves
, "");
4213 else if (ws
->enable_inclusive
)
4214 tmp
= LLVMBuildICmp(builder
, LLVMIntULE
, tid
, ws
->waveidx
, "");
4216 tmp
= LLVMBuildICmp(builder
, LLVMIntULT
, tid
, ws
->waveidx
, "");
4217 ac_build_ifcc(ctx
, tmp
, 1001);
4219 tmp
= LLVMBuildLoad(builder
, LLVMBuildGEP(builder
, ws
->scratch
, &tid
, 1, ""), "");
4221 ac_build_optimization_barrier(ctx
, &tmp
);
4223 bbs
[1] = LLVMGetInsertBlock(builder
);
4224 phivalues_scan
[1] = ac_build_scan(ctx
, ws
->op
, tmp
, identity
, ws
->maxwaves
, true);
4226 ac_build_endif(ctx
, 1001);
4228 const LLVMValueRef scan
= ac_build_phi(ctx
, type
, 2, phivalues_scan
, bbs
);
4230 if (ws
->enable_reduce
) {
4231 tmp
= LLVMBuildSub(builder
, ws
->numwaves
, ctx
->i32_1
, "");
4232 ws
->result_reduce
= ac_build_readlane(ctx
, scan
, tmp
);
4234 if (ws
->enable_inclusive
)
4235 ws
->result_inclusive
= ac_build_readlane(ctx
, scan
, ws
->waveidx
);
4236 if (ws
->enable_exclusive
) {
4237 tmp
= LLVMBuildSub(builder
, ws
->waveidx
, ctx
->i32_1
, "");
4238 tmp
= ac_build_readlane(ctx
, scan
, tmp
);
4239 tmp2
= LLVMBuildICmp(builder
, LLVMIntEQ
, ws
->waveidx
, ctx
->i32_0
, "");
4240 ws
->result_exclusive
= LLVMBuildSelect(builder
, tmp2
, identity
, tmp
, "");
4245 * Inclusive scan of a per-wave value across an entire workgroup.
4247 * This implies an s_barrier instruction.
4249 * Unlike ac_build_inclusive_scan, the caller \em must ensure that all threads
4250 * of the workgroup are live. (This requirement cannot easily be relaxed in a
4251 * useful manner because of the barrier in the algorithm.)
4254 ac_build_wg_wavescan(struct ac_llvm_context
*ctx
, struct ac_wg_scan
*ws
)
4256 ac_build_wg_wavescan_top(ctx
, ws
);
4257 ac_build_s_barrier(ctx
);
4258 ac_build_wg_wavescan_bottom(ctx
, ws
);
4262 * "Top half" of a scan that reduces per-thread values across an entire
4265 * All lanes must be active when this code runs.
4268 ac_build_wg_scan_top(struct ac_llvm_context
*ctx
, struct ac_wg_scan
*ws
)
4270 if (ws
->enable_exclusive
) {
4271 ws
->extra
= ac_build_exclusive_scan(ctx
, ws
->src
, ws
->op
);
4272 if (LLVMTypeOf(ws
->src
) == ctx
->i1
&& ws
->op
== nir_op_iadd
)
4273 ws
->src
= LLVMBuildZExt(ctx
->builder
, ws
->src
, ctx
->i32
, "");
4274 ws
->src
= ac_build_alu_op(ctx
, ws
->extra
, ws
->src
, ws
->op
);
4276 ws
->src
= ac_build_inclusive_scan(ctx
, ws
->src
, ws
->op
);
4279 bool enable_inclusive
= ws
->enable_inclusive
;
4280 bool enable_exclusive
= ws
->enable_exclusive
;
4281 ws
->enable_inclusive
= false;
4282 ws
->enable_exclusive
= ws
->enable_exclusive
|| enable_inclusive
;
4283 ac_build_wg_wavescan_top(ctx
, ws
);
4284 ws
->enable_inclusive
= enable_inclusive
;
4285 ws
->enable_exclusive
= enable_exclusive
;
4289 * "Bottom half" of a scan that reduces per-thread values across an entire
4292 * The caller must place a barrier between the top and bottom halves.
4295 ac_build_wg_scan_bottom(struct ac_llvm_context
*ctx
, struct ac_wg_scan
*ws
)
4297 bool enable_inclusive
= ws
->enable_inclusive
;
4298 bool enable_exclusive
= ws
->enable_exclusive
;
4299 ws
->enable_inclusive
= false;
4300 ws
->enable_exclusive
= ws
->enable_exclusive
|| enable_inclusive
;
4301 ac_build_wg_wavescan_bottom(ctx
, ws
);
4302 ws
->enable_inclusive
= enable_inclusive
;
4303 ws
->enable_exclusive
= enable_exclusive
;
4305 /* ws->result_reduce is already the correct value */
4306 if (ws
->enable_inclusive
)
4307 ws
->result_inclusive
= ac_build_alu_op(ctx
, ws
->result_inclusive
, ws
->src
, ws
->op
);
4308 if (ws
->enable_exclusive
)
4309 ws
->result_exclusive
= ac_build_alu_op(ctx
, ws
->result_exclusive
, ws
->extra
, ws
->op
);
4313 * A scan that reduces per-thread values across an entire workgroup.
4315 * The caller must ensure that all lanes are active when this code runs
4316 * (WWM is insufficient!), because there is an implied barrier.
4319 ac_build_wg_scan(struct ac_llvm_context
*ctx
, struct ac_wg_scan
*ws
)
4321 ac_build_wg_scan_top(ctx
, ws
);
4322 ac_build_s_barrier(ctx
);
4323 ac_build_wg_scan_bottom(ctx
, ws
);
4327 ac_build_quad_swizzle(struct ac_llvm_context
*ctx
, LLVMValueRef src
,
4328 unsigned lane0
, unsigned lane1
, unsigned lane2
, unsigned lane3
)
4330 unsigned mask
= dpp_quad_perm(lane0
, lane1
, lane2
, lane3
);
4331 if (ctx
->chip_class
>= GFX8
) {
4332 return ac_build_dpp(ctx
, src
, src
, mask
, 0xf, 0xf, false);
4334 return ac_build_ds_swizzle(ctx
, src
, (1 << 15) | mask
);
4339 ac_build_shuffle(struct ac_llvm_context
*ctx
, LLVMValueRef src
, LLVMValueRef index
)
4341 index
= LLVMBuildMul(ctx
->builder
, index
, LLVMConstInt(ctx
->i32
, 4, 0), "");
4342 return ac_build_intrinsic(ctx
,
4343 "llvm.amdgcn.ds.bpermute", ctx
->i32
,
4344 (LLVMValueRef
[]) {index
, src
}, 2,
4345 AC_FUNC_ATTR_READNONE
|
4346 AC_FUNC_ATTR_CONVERGENT
);
4350 ac_build_frexp_exp(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
4356 if (bitsize
== 16) {
4357 intr
= "llvm.amdgcn.frexp.exp.i16.f16";
4359 } else if (bitsize
== 32) {
4360 intr
= "llvm.amdgcn.frexp.exp.i32.f32";
4363 intr
= "llvm.amdgcn.frexp.exp.i32.f64";
4367 LLVMValueRef params
[] = {
4370 return ac_build_intrinsic(ctx
, intr
, type
, params
, 1,
4371 AC_FUNC_ATTR_READNONE
);
4374 ac_build_frexp_mant(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
4380 if (bitsize
== 16) {
4381 intr
= "llvm.amdgcn.frexp.mant.f16";
4383 } else if (bitsize
== 32) {
4384 intr
= "llvm.amdgcn.frexp.mant.f32";
4387 intr
= "llvm.amdgcn.frexp.mant.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
));