2 * Copyright 2014 Advanced Micro Devices, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sub license, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
15 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
16 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
17 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
18 * USE OR OTHER DEALINGS IN THE SOFTWARE.
20 * The above copyright notice and this permission notice (including the
21 * next paragraph) shall be included in all copies or substantial portions
25 /* based on pieces from si_pipe.c and radeon_llvm_emit.c */
26 #include "ac_llvm_build.h"
28 #include <llvm-c/Core.h>
30 #include "c11/threads.h"
35 #include "ac_llvm_util.h"
36 #include "ac_exp_param.h"
37 #include "util/bitscan.h"
38 #include "util/macros.h"
39 #include "util/u_atomic.h"
40 #include "util/u_math.h"
43 #include "shader_enums.h"
45 #define AC_LLVM_INITIAL_CF_DEPTH 4
47 /* Data for if/else/endif and bgnloop/endloop control flow structures.
50 /* Loop exit or next part of if/else/endif. */
51 LLVMBasicBlockRef next_block
;
52 LLVMBasicBlockRef loop_entry_block
;
55 /* Initialize module-independent parts of the context.
57 * The caller is responsible for initializing ctx::module and ctx::builder.
60 ac_llvm_context_init(struct ac_llvm_context
*ctx
,
61 struct ac_llvm_compiler
*compiler
,
62 enum chip_class chip_class
, enum radeon_family family
,
63 enum ac_float_mode float_mode
, unsigned wave_size
)
67 ctx
->context
= LLVMContextCreate();
69 ctx
->chip_class
= chip_class
;
71 ctx
->wave_size
= wave_size
;
72 ctx
->module
= ac_create_module(wave_size
== 32 ? compiler
->tm_wave32
75 ctx
->builder
= ac_create_builder(ctx
->context
, float_mode
);
77 ctx
->voidt
= LLVMVoidTypeInContext(ctx
->context
);
78 ctx
->i1
= LLVMInt1TypeInContext(ctx
->context
);
79 ctx
->i8
= LLVMInt8TypeInContext(ctx
->context
);
80 ctx
->i16
= LLVMIntTypeInContext(ctx
->context
, 16);
81 ctx
->i32
= LLVMIntTypeInContext(ctx
->context
, 32);
82 ctx
->i64
= LLVMIntTypeInContext(ctx
->context
, 64);
83 ctx
->intptr
= ctx
->i32
;
84 ctx
->f16
= LLVMHalfTypeInContext(ctx
->context
);
85 ctx
->f32
= LLVMFloatTypeInContext(ctx
->context
);
86 ctx
->f64
= LLVMDoubleTypeInContext(ctx
->context
);
87 ctx
->v2i16
= LLVMVectorType(ctx
->i16
, 2);
88 ctx
->v2i32
= LLVMVectorType(ctx
->i32
, 2);
89 ctx
->v3i32
= LLVMVectorType(ctx
->i32
, 3);
90 ctx
->v4i32
= LLVMVectorType(ctx
->i32
, 4);
91 ctx
->v2f32
= LLVMVectorType(ctx
->f32
, 2);
92 ctx
->v3f32
= LLVMVectorType(ctx
->f32
, 3);
93 ctx
->v4f32
= LLVMVectorType(ctx
->f32
, 4);
94 ctx
->v8i32
= LLVMVectorType(ctx
->i32
, 8);
95 ctx
->iN_wavemask
= LLVMIntTypeInContext(ctx
->context
, ctx
->wave_size
);
97 ctx
->i8_0
= LLVMConstInt(ctx
->i8
, 0, false);
98 ctx
->i8_1
= LLVMConstInt(ctx
->i8
, 1, false);
99 ctx
->i16_0
= LLVMConstInt(ctx
->i16
, 0, false);
100 ctx
->i16_1
= LLVMConstInt(ctx
->i16
, 1, false);
101 ctx
->i32_0
= LLVMConstInt(ctx
->i32
, 0, false);
102 ctx
->i32_1
= LLVMConstInt(ctx
->i32
, 1, false);
103 ctx
->i64_0
= LLVMConstInt(ctx
->i64
, 0, false);
104 ctx
->i64_1
= LLVMConstInt(ctx
->i64
, 1, false);
105 ctx
->f16_0
= LLVMConstReal(ctx
->f16
, 0.0);
106 ctx
->f16_1
= LLVMConstReal(ctx
->f16
, 1.0);
107 ctx
->f32_0
= LLVMConstReal(ctx
->f32
, 0.0);
108 ctx
->f32_1
= LLVMConstReal(ctx
->f32
, 1.0);
109 ctx
->f64_0
= LLVMConstReal(ctx
->f64
, 0.0);
110 ctx
->f64_1
= LLVMConstReal(ctx
->f64
, 1.0);
112 ctx
->i1false
= LLVMConstInt(ctx
->i1
, 0, false);
113 ctx
->i1true
= LLVMConstInt(ctx
->i1
, 1, false);
115 ctx
->range_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
118 ctx
->invariant_load_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
119 "invariant.load", 14);
121 ctx
->fpmath_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
, "fpmath", 6);
123 args
[0] = LLVMConstReal(ctx
->f32
, 2.5);
124 ctx
->fpmath_md_2p5_ulp
= LLVMMDNodeInContext(ctx
->context
, args
, 1);
126 ctx
->uniform_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
127 "amdgpu.uniform", 14);
129 ctx
->empty_md
= LLVMMDNodeInContext(ctx
->context
, NULL
, 0);
133 ac_llvm_context_dispose(struct ac_llvm_context
*ctx
)
137 ctx
->flow_depth_max
= 0;
141 ac_get_llvm_num_components(LLVMValueRef value
)
143 LLVMTypeRef type
= LLVMTypeOf(value
);
144 unsigned num_components
= LLVMGetTypeKind(type
) == LLVMVectorTypeKind
145 ? LLVMGetVectorSize(type
)
147 return num_components
;
151 ac_llvm_extract_elem(struct ac_llvm_context
*ac
,
155 if (LLVMGetTypeKind(LLVMTypeOf(value
)) != LLVMVectorTypeKind
) {
160 return LLVMBuildExtractElement(ac
->builder
, value
,
161 LLVMConstInt(ac
->i32
, index
, false), "");
165 ac_get_elem_bits(struct ac_llvm_context
*ctx
, LLVMTypeRef type
)
167 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
)
168 type
= LLVMGetElementType(type
);
170 if (LLVMGetTypeKind(type
) == LLVMIntegerTypeKind
)
171 return LLVMGetIntTypeWidth(type
);
173 if (type
== ctx
->f16
)
175 if (type
== ctx
->f32
)
177 if (type
== ctx
->f64
)
180 unreachable("Unhandled type kind in get_elem_bits");
184 ac_get_type_size(LLVMTypeRef type
)
186 LLVMTypeKind kind
= LLVMGetTypeKind(type
);
189 case LLVMIntegerTypeKind
:
190 return LLVMGetIntTypeWidth(type
) / 8;
191 case LLVMHalfTypeKind
:
193 case LLVMFloatTypeKind
:
195 case LLVMDoubleTypeKind
:
197 case LLVMPointerTypeKind
:
198 if (LLVMGetPointerAddressSpace(type
) == AC_ADDR_SPACE_CONST_32BIT
)
201 case LLVMVectorTypeKind
:
202 return LLVMGetVectorSize(type
) *
203 ac_get_type_size(LLVMGetElementType(type
));
204 case LLVMArrayTypeKind
:
205 return LLVMGetArrayLength(type
) *
206 ac_get_type_size(LLVMGetElementType(type
));
213 static LLVMTypeRef
to_integer_type_scalar(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
217 else if (t
== ctx
->f16
|| t
== ctx
->i16
)
219 else if (t
== ctx
->f32
|| t
== ctx
->i32
)
221 else if (t
== ctx
->f64
|| t
== ctx
->i64
)
224 unreachable("Unhandled integer size");
228 ac_to_integer_type(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
230 if (LLVMGetTypeKind(t
) == LLVMVectorTypeKind
) {
231 LLVMTypeRef elem_type
= LLVMGetElementType(t
);
232 return LLVMVectorType(to_integer_type_scalar(ctx
, elem_type
),
233 LLVMGetVectorSize(t
));
235 if (LLVMGetTypeKind(t
) == LLVMPointerTypeKind
) {
236 switch (LLVMGetPointerAddressSpace(t
)) {
237 case AC_ADDR_SPACE_GLOBAL
:
239 case AC_ADDR_SPACE_LDS
:
242 unreachable("unhandled address space");
245 return to_integer_type_scalar(ctx
, t
);
249 ac_to_integer(struct ac_llvm_context
*ctx
, LLVMValueRef v
)
251 LLVMTypeRef type
= LLVMTypeOf(v
);
252 if (LLVMGetTypeKind(type
) == LLVMPointerTypeKind
) {
253 return LLVMBuildPtrToInt(ctx
->builder
, v
, ac_to_integer_type(ctx
, type
), "");
255 return LLVMBuildBitCast(ctx
->builder
, v
, ac_to_integer_type(ctx
, type
), "");
259 ac_to_integer_or_pointer(struct ac_llvm_context
*ctx
, LLVMValueRef v
)
261 LLVMTypeRef type
= LLVMTypeOf(v
);
262 if (LLVMGetTypeKind(type
) == LLVMPointerTypeKind
)
264 return ac_to_integer(ctx
, v
);
267 static LLVMTypeRef
to_float_type_scalar(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
271 else if (t
== ctx
->i16
|| t
== ctx
->f16
)
273 else if (t
== ctx
->i32
|| t
== ctx
->f32
)
275 else if (t
== ctx
->i64
|| t
== ctx
->f64
)
278 unreachable("Unhandled float size");
282 ac_to_float_type(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
284 if (LLVMGetTypeKind(t
) == LLVMVectorTypeKind
) {
285 LLVMTypeRef elem_type
= LLVMGetElementType(t
);
286 return LLVMVectorType(to_float_type_scalar(ctx
, elem_type
),
287 LLVMGetVectorSize(t
));
289 return to_float_type_scalar(ctx
, t
);
293 ac_to_float(struct ac_llvm_context
*ctx
, LLVMValueRef v
)
295 LLVMTypeRef type
= LLVMTypeOf(v
);
296 return LLVMBuildBitCast(ctx
->builder
, v
, ac_to_float_type(ctx
, type
), "");
301 ac_build_intrinsic(struct ac_llvm_context
*ctx
, const char *name
,
302 LLVMTypeRef return_type
, LLVMValueRef
*params
,
303 unsigned param_count
, unsigned attrib_mask
)
305 LLVMValueRef function
, call
;
306 bool set_callsite_attrs
= !(attrib_mask
& AC_FUNC_ATTR_LEGACY
);
308 function
= LLVMGetNamedFunction(ctx
->module
, name
);
310 LLVMTypeRef param_types
[32], function_type
;
313 assert(param_count
<= 32);
315 for (i
= 0; i
< param_count
; ++i
) {
317 param_types
[i
] = LLVMTypeOf(params
[i
]);
320 LLVMFunctionType(return_type
, param_types
, param_count
, 0);
321 function
= LLVMAddFunction(ctx
->module
, name
, function_type
);
323 LLVMSetFunctionCallConv(function
, LLVMCCallConv
);
324 LLVMSetLinkage(function
, LLVMExternalLinkage
);
326 if (!set_callsite_attrs
)
327 ac_add_func_attributes(ctx
->context
, function
, attrib_mask
);
330 call
= LLVMBuildCall(ctx
->builder
, function
, params
, param_count
, "");
331 if (set_callsite_attrs
)
332 ac_add_func_attributes(ctx
->context
, call
, attrib_mask
);
337 * Given the i32 or vNi32 \p type, generate the textual name (e.g. for use with
340 void ac_build_type_name_for_intr(LLVMTypeRef type
, char *buf
, unsigned bufsize
)
342 LLVMTypeRef elem_type
= type
;
344 assert(bufsize
>= 8);
346 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
) {
347 int ret
= snprintf(buf
, bufsize
, "v%u",
348 LLVMGetVectorSize(type
));
350 char *type_name
= LLVMPrintTypeToString(type
);
351 fprintf(stderr
, "Error building type name for: %s\n",
355 elem_type
= LLVMGetElementType(type
);
359 switch (LLVMGetTypeKind(elem_type
)) {
361 case LLVMIntegerTypeKind
:
362 snprintf(buf
, bufsize
, "i%d", LLVMGetIntTypeWidth(elem_type
));
364 case LLVMHalfTypeKind
:
365 snprintf(buf
, bufsize
, "f16");
367 case LLVMFloatTypeKind
:
368 snprintf(buf
, bufsize
, "f32");
370 case LLVMDoubleTypeKind
:
371 snprintf(buf
, bufsize
, "f64");
377 * Helper function that builds an LLVM IR PHI node and immediately adds
381 ac_build_phi(struct ac_llvm_context
*ctx
, LLVMTypeRef type
,
382 unsigned count_incoming
, LLVMValueRef
*values
,
383 LLVMBasicBlockRef
*blocks
)
385 LLVMValueRef phi
= LLVMBuildPhi(ctx
->builder
, type
, "");
386 LLVMAddIncoming(phi
, values
, blocks
, count_incoming
);
390 void ac_build_s_barrier(struct ac_llvm_context
*ctx
)
392 ac_build_intrinsic(ctx
, "llvm.amdgcn.s.barrier", ctx
->voidt
, NULL
,
393 0, AC_FUNC_ATTR_CONVERGENT
);
396 /* Prevent optimizations (at least of memory accesses) across the current
397 * point in the program by emitting empty inline assembly that is marked as
398 * having side effects.
400 * Optionally, a value can be passed through the inline assembly to prevent
401 * LLVM from hoisting calls to ReadNone functions.
404 ac_build_optimization_barrier(struct ac_llvm_context
*ctx
,
407 static int counter
= 0;
409 LLVMBuilderRef builder
= ctx
->builder
;
412 snprintf(code
, sizeof(code
), "; %d", p_atomic_inc_return(&counter
));
415 LLVMTypeRef ftype
= LLVMFunctionType(ctx
->voidt
, NULL
, 0, false);
416 LLVMValueRef inlineasm
= LLVMConstInlineAsm(ftype
, code
, "", true, false);
417 LLVMBuildCall(builder
, inlineasm
, NULL
, 0, "");
419 LLVMTypeRef ftype
= LLVMFunctionType(ctx
->i32
, &ctx
->i32
, 1, false);
420 LLVMValueRef inlineasm
= LLVMConstInlineAsm(ftype
, code
, "=v,0", true, false);
421 LLVMValueRef vgpr
= *pvgpr
;
422 LLVMTypeRef vgpr_type
= LLVMTypeOf(vgpr
);
423 unsigned vgpr_size
= ac_get_type_size(vgpr_type
);
426 assert(vgpr_size
% 4 == 0);
428 vgpr
= LLVMBuildBitCast(builder
, vgpr
, LLVMVectorType(ctx
->i32
, vgpr_size
/ 4), "");
429 vgpr0
= LLVMBuildExtractElement(builder
, vgpr
, ctx
->i32_0
, "");
430 vgpr0
= LLVMBuildCall(builder
, inlineasm
, &vgpr0
, 1, "");
431 vgpr
= LLVMBuildInsertElement(builder
, vgpr
, vgpr0
, ctx
->i32_0
, "");
432 vgpr
= LLVMBuildBitCast(builder
, vgpr
, vgpr_type
, "");
439 ac_build_shader_clock(struct ac_llvm_context
*ctx
)
441 const char *intr
= HAVE_LLVM
>= 0x0900 && ctx
->chip_class
>= GFX8
?
442 "llvm.amdgcn.s.memrealtime" : "llvm.readcyclecounter";
443 LLVMValueRef tmp
= ac_build_intrinsic(ctx
, intr
, ctx
->i64
, NULL
, 0, 0);
444 return LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->v2i32
, "");
448 ac_build_ballot(struct ac_llvm_context
*ctx
,
453 if (HAVE_LLVM
>= 0x900) {
454 if (ctx
->wave_size
== 64)
455 name
= "llvm.amdgcn.icmp.i64.i32";
457 name
= "llvm.amdgcn.icmp.i32.i32";
459 name
= "llvm.amdgcn.icmp.i32";
461 LLVMValueRef args
[3] = {
464 LLVMConstInt(ctx
->i32
, LLVMIntNE
, 0)
467 /* We currently have no other way to prevent LLVM from lifting the icmp
468 * calls to a dominating basic block.
470 ac_build_optimization_barrier(ctx
, &args
[0]);
472 args
[0] = ac_to_integer(ctx
, args
[0]);
474 return ac_build_intrinsic(ctx
, name
, ctx
->iN_wavemask
, args
, 3,
475 AC_FUNC_ATTR_NOUNWIND
|
476 AC_FUNC_ATTR_READNONE
|
477 AC_FUNC_ATTR_CONVERGENT
);
480 LLVMValueRef
ac_get_i1_sgpr_mask(struct ac_llvm_context
*ctx
,
483 const char *name
= HAVE_LLVM
>= 0x900 ? "llvm.amdgcn.icmp.i64.i1" : "llvm.amdgcn.icmp.i1";
484 LLVMValueRef args
[3] = {
487 LLVMConstInt(ctx
->i32
, LLVMIntNE
, 0),
490 assert(HAVE_LLVM
>= 0x0800);
491 return ac_build_intrinsic(ctx
, name
, ctx
->i64
, args
, 3,
492 AC_FUNC_ATTR_NOUNWIND
|
493 AC_FUNC_ATTR_READNONE
|
494 AC_FUNC_ATTR_CONVERGENT
);
498 ac_build_vote_all(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
500 LLVMValueRef active_set
= ac_build_ballot(ctx
, ctx
->i32_1
);
501 LLVMValueRef vote_set
= ac_build_ballot(ctx
, value
);
502 return LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, vote_set
, active_set
, "");
506 ac_build_vote_any(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
508 LLVMValueRef vote_set
= ac_build_ballot(ctx
, value
);
509 return LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, vote_set
,
510 LLVMConstInt(ctx
->iN_wavemask
, 0, 0), "");
514 ac_build_vote_eq(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
516 LLVMValueRef active_set
= ac_build_ballot(ctx
, ctx
->i32_1
);
517 LLVMValueRef vote_set
= ac_build_ballot(ctx
, value
);
519 LLVMValueRef all
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
520 vote_set
, active_set
, "");
521 LLVMValueRef none
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
523 LLVMConstInt(ctx
->iN_wavemask
, 0, 0), "");
524 return LLVMBuildOr(ctx
->builder
, all
, none
, "");
528 ac_build_varying_gather_values(struct ac_llvm_context
*ctx
, LLVMValueRef
*values
,
529 unsigned value_count
, unsigned component
)
531 LLVMValueRef vec
= NULL
;
533 if (value_count
== 1) {
534 return values
[component
];
535 } else if (!value_count
)
536 unreachable("value_count is 0");
538 for (unsigned i
= component
; i
< value_count
+ component
; i
++) {
539 LLVMValueRef value
= values
[i
];
542 vec
= LLVMGetUndef( LLVMVectorType(LLVMTypeOf(value
), value_count
));
543 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, i
- component
, false);
544 vec
= LLVMBuildInsertElement(ctx
->builder
, vec
, value
, index
, "");
550 ac_build_gather_values_extended(struct ac_llvm_context
*ctx
,
551 LLVMValueRef
*values
,
552 unsigned value_count
,
553 unsigned value_stride
,
557 LLVMBuilderRef builder
= ctx
->builder
;
558 LLVMValueRef vec
= NULL
;
561 if (value_count
== 1 && !always_vector
) {
563 return LLVMBuildLoad(builder
, values
[0], "");
565 } else if (!value_count
)
566 unreachable("value_count is 0");
568 for (i
= 0; i
< value_count
; i
++) {
569 LLVMValueRef value
= values
[i
* value_stride
];
571 value
= LLVMBuildLoad(builder
, value
, "");
574 vec
= LLVMGetUndef( LLVMVectorType(LLVMTypeOf(value
), value_count
));
575 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, i
, false);
576 vec
= LLVMBuildInsertElement(builder
, vec
, value
, index
, "");
582 ac_build_gather_values(struct ac_llvm_context
*ctx
,
583 LLVMValueRef
*values
,
584 unsigned value_count
)
586 return ac_build_gather_values_extended(ctx
, values
, value_count
, 1, false, false);
589 /* Expand a scalar or vector to <dst_channels x type> by filling the remaining
590 * channels with undef. Extract at most src_channels components from the input.
593 ac_build_expand(struct ac_llvm_context
*ctx
,
595 unsigned src_channels
,
596 unsigned dst_channels
)
598 LLVMTypeRef elemtype
;
599 LLVMValueRef chan
[dst_channels
];
601 if (LLVMGetTypeKind(LLVMTypeOf(value
)) == LLVMVectorTypeKind
) {
602 unsigned vec_size
= LLVMGetVectorSize(LLVMTypeOf(value
));
604 if (src_channels
== dst_channels
&& vec_size
== dst_channels
)
607 src_channels
= MIN2(src_channels
, vec_size
);
609 for (unsigned i
= 0; i
< src_channels
; i
++)
610 chan
[i
] = ac_llvm_extract_elem(ctx
, value
, i
);
612 elemtype
= LLVMGetElementType(LLVMTypeOf(value
));
615 assert(src_channels
== 1);
618 elemtype
= LLVMTypeOf(value
);
621 for (unsigned i
= src_channels
; i
< dst_channels
; i
++)
622 chan
[i
] = LLVMGetUndef(elemtype
);
624 return ac_build_gather_values(ctx
, chan
, dst_channels
);
627 /* Expand a scalar or vector to <4 x type> by filling the remaining channels
628 * with undef. Extract at most num_channels components from the input.
630 LLVMValueRef
ac_build_expand_to_vec4(struct ac_llvm_context
*ctx
,
632 unsigned num_channels
)
634 return ac_build_expand(ctx
, value
, num_channels
, 4);
637 LLVMValueRef
ac_build_round(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
639 unsigned type_size
= ac_get_type_size(LLVMTypeOf(value
));
643 name
= "llvm.rint.f16";
644 else if (type_size
== 4)
645 name
= "llvm.rint.f32";
647 name
= "llvm.rint.f64";
649 return ac_build_intrinsic(ctx
, name
, LLVMTypeOf(value
), &value
, 1,
650 AC_FUNC_ATTR_READNONE
);
654 ac_build_fdiv(struct ac_llvm_context
*ctx
,
658 /* If we do (num / den), LLVM >= 7.0 does:
659 * return num * v_rcp_f32(den * (fabs(den) > 0x1.0p+96f ? 0x1.0p-32f : 1.0f));
661 * If we do (num * (1 / den)), LLVM does:
662 * return num * v_rcp_f32(den);
664 LLVMValueRef one
= LLVMConstReal(LLVMTypeOf(num
), 1.0);
665 LLVMValueRef rcp
= LLVMBuildFDiv(ctx
->builder
, one
, den
, "");
666 LLVMValueRef ret
= LLVMBuildFMul(ctx
->builder
, num
, rcp
, "");
668 /* Use v_rcp_f32 instead of precise division. */
669 if (!LLVMIsConstant(ret
))
670 LLVMSetMetadata(ret
, ctx
->fpmath_md_kind
, ctx
->fpmath_md_2p5_ulp
);
674 /* See fast_idiv_by_const.h. */
675 /* Set: increment = util_fast_udiv_info::increment ? multiplier : 0; */
676 LLVMValueRef
ac_build_fast_udiv(struct ac_llvm_context
*ctx
,
678 LLVMValueRef multiplier
,
679 LLVMValueRef pre_shift
,
680 LLVMValueRef post_shift
,
681 LLVMValueRef increment
)
683 LLVMBuilderRef builder
= ctx
->builder
;
685 num
= LLVMBuildLShr(builder
, num
, pre_shift
, "");
686 num
= LLVMBuildMul(builder
,
687 LLVMBuildZExt(builder
, num
, ctx
->i64
, ""),
688 LLVMBuildZExt(builder
, multiplier
, ctx
->i64
, ""), "");
689 num
= LLVMBuildAdd(builder
, num
,
690 LLVMBuildZExt(builder
, increment
, ctx
->i64
, ""), "");
691 num
= LLVMBuildLShr(builder
, num
, LLVMConstInt(ctx
->i64
, 32, 0), "");
692 num
= LLVMBuildTrunc(builder
, num
, ctx
->i32
, "");
693 return LLVMBuildLShr(builder
, num
, post_shift
, "");
696 /* See fast_idiv_by_const.h. */
697 /* If num != UINT_MAX, this more efficient version can be used. */
698 /* Set: increment = util_fast_udiv_info::increment; */
699 LLVMValueRef
ac_build_fast_udiv_nuw(struct ac_llvm_context
*ctx
,
701 LLVMValueRef multiplier
,
702 LLVMValueRef pre_shift
,
703 LLVMValueRef post_shift
,
704 LLVMValueRef increment
)
706 LLVMBuilderRef builder
= ctx
->builder
;
708 num
= LLVMBuildLShr(builder
, num
, pre_shift
, "");
709 num
= LLVMBuildNUWAdd(builder
, num
, increment
, "");
710 num
= LLVMBuildMul(builder
,
711 LLVMBuildZExt(builder
, num
, ctx
->i64
, ""),
712 LLVMBuildZExt(builder
, multiplier
, ctx
->i64
, ""), "");
713 num
= LLVMBuildLShr(builder
, num
, LLVMConstInt(ctx
->i64
, 32, 0), "");
714 num
= LLVMBuildTrunc(builder
, num
, ctx
->i32
, "");
715 return LLVMBuildLShr(builder
, num
, post_shift
, "");
718 /* See fast_idiv_by_const.h. */
719 /* Both operands must fit in 31 bits and the divisor must not be 1. */
720 LLVMValueRef
ac_build_fast_udiv_u31_d_not_one(struct ac_llvm_context
*ctx
,
722 LLVMValueRef multiplier
,
723 LLVMValueRef post_shift
)
725 LLVMBuilderRef builder
= ctx
->builder
;
727 num
= LLVMBuildMul(builder
,
728 LLVMBuildZExt(builder
, num
, ctx
->i64
, ""),
729 LLVMBuildZExt(builder
, multiplier
, ctx
->i64
, ""), "");
730 num
= LLVMBuildLShr(builder
, num
, LLVMConstInt(ctx
->i64
, 32, 0), "");
731 num
= LLVMBuildTrunc(builder
, num
, ctx
->i32
, "");
732 return LLVMBuildLShr(builder
, num
, post_shift
, "");
735 /* Coordinates for cube map selection. sc, tc, and ma are as in Table 8.27
736 * of the OpenGL 4.5 (Compatibility Profile) specification, except ma is
737 * already multiplied by two. id is the cube face number.
739 struct cube_selection_coords
{
746 build_cube_intrinsic(struct ac_llvm_context
*ctx
,
748 struct cube_selection_coords
*out
)
750 LLVMTypeRef f32
= ctx
->f32
;
752 out
->stc
[1] = ac_build_intrinsic(ctx
, "llvm.amdgcn.cubetc",
753 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
754 out
->stc
[0] = ac_build_intrinsic(ctx
, "llvm.amdgcn.cubesc",
755 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
756 out
->ma
= ac_build_intrinsic(ctx
, "llvm.amdgcn.cubema",
757 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
758 out
->id
= ac_build_intrinsic(ctx
, "llvm.amdgcn.cubeid",
759 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
763 * Build a manual selection sequence for cube face sc/tc coordinates and
764 * major axis vector (multiplied by 2 for consistency) for the given
765 * vec3 \p coords, for the face implied by \p selcoords.
767 * For the major axis, we always adjust the sign to be in the direction of
768 * selcoords.ma; i.e., a positive out_ma means that coords is pointed towards
769 * the selcoords major axis.
771 static void build_cube_select(struct ac_llvm_context
*ctx
,
772 const struct cube_selection_coords
*selcoords
,
773 const LLVMValueRef
*coords
,
774 LLVMValueRef
*out_st
,
775 LLVMValueRef
*out_ma
)
777 LLVMBuilderRef builder
= ctx
->builder
;
778 LLVMTypeRef f32
= LLVMTypeOf(coords
[0]);
779 LLVMValueRef is_ma_positive
;
781 LLVMValueRef is_ma_z
, is_not_ma_z
;
782 LLVMValueRef is_ma_y
;
783 LLVMValueRef is_ma_x
;
787 is_ma_positive
= LLVMBuildFCmp(builder
, LLVMRealUGE
,
788 selcoords
->ma
, LLVMConstReal(f32
, 0.0), "");
789 sgn_ma
= LLVMBuildSelect(builder
, is_ma_positive
,
790 LLVMConstReal(f32
, 1.0), LLVMConstReal(f32
, -1.0), "");
792 is_ma_z
= LLVMBuildFCmp(builder
, LLVMRealUGE
, selcoords
->id
, LLVMConstReal(f32
, 4.0), "");
793 is_not_ma_z
= LLVMBuildNot(builder
, is_ma_z
, "");
794 is_ma_y
= LLVMBuildAnd(builder
, is_not_ma_z
,
795 LLVMBuildFCmp(builder
, LLVMRealUGE
, selcoords
->id
, LLVMConstReal(f32
, 2.0), ""), "");
796 is_ma_x
= LLVMBuildAnd(builder
, is_not_ma_z
, LLVMBuildNot(builder
, is_ma_y
, ""), "");
799 tmp
= LLVMBuildSelect(builder
, is_ma_x
, coords
[2], coords
[0], "");
800 sgn
= LLVMBuildSelect(builder
, is_ma_y
, LLVMConstReal(f32
, 1.0),
801 LLVMBuildSelect(builder
, is_ma_z
, sgn_ma
,
802 LLVMBuildFNeg(builder
, sgn_ma
, ""), ""), "");
803 out_st
[0] = LLVMBuildFMul(builder
, tmp
, sgn
, "");
806 tmp
= LLVMBuildSelect(builder
, is_ma_y
, coords
[2], coords
[1], "");
807 sgn
= LLVMBuildSelect(builder
, is_ma_y
, sgn_ma
,
808 LLVMConstReal(f32
, -1.0), "");
809 out_st
[1] = LLVMBuildFMul(builder
, tmp
, sgn
, "");
812 tmp
= LLVMBuildSelect(builder
, is_ma_z
, coords
[2],
813 LLVMBuildSelect(builder
, is_ma_y
, coords
[1], coords
[0], ""), "");
814 tmp
= ac_build_intrinsic(ctx
, "llvm.fabs.f32",
815 ctx
->f32
, &tmp
, 1, AC_FUNC_ATTR_READNONE
);
816 *out_ma
= LLVMBuildFMul(builder
, tmp
, LLVMConstReal(f32
, 2.0), "");
820 ac_prepare_cube_coords(struct ac_llvm_context
*ctx
,
821 bool is_deriv
, bool is_array
, bool is_lod
,
822 LLVMValueRef
*coords_arg
,
823 LLVMValueRef
*derivs_arg
)
826 LLVMBuilderRef builder
= ctx
->builder
;
827 struct cube_selection_coords selcoords
;
828 LLVMValueRef coords
[3];
831 if (is_array
&& !is_lod
) {
832 LLVMValueRef tmp
= ac_build_round(ctx
, coords_arg
[3]);
834 /* Section 8.9 (Texture Functions) of the GLSL 4.50 spec says:
836 * "For Array forms, the array layer used will be
838 * max(0, min(d−1, floor(layer+0.5)))
840 * where d is the depth of the texture array and layer
841 * comes from the component indicated in the tables below.
842 * Workaroudn for an issue where the layer is taken from a
843 * helper invocation which happens to fall on a different
844 * layer due to extrapolation."
846 * GFX8 and earlier attempt to implement this in hardware by
847 * clamping the value of coords[2] = (8 * layer) + face.
848 * Unfortunately, this means that the we end up with the wrong
849 * face when clamping occurs.
851 * Clamp the layer earlier to work around the issue.
853 if (ctx
->chip_class
<= GFX8
) {
855 ge0
= LLVMBuildFCmp(builder
, LLVMRealOGE
, tmp
, ctx
->f32_0
, "");
856 tmp
= LLVMBuildSelect(builder
, ge0
, tmp
, ctx
->f32_0
, "");
862 build_cube_intrinsic(ctx
, coords_arg
, &selcoords
);
864 invma
= ac_build_intrinsic(ctx
, "llvm.fabs.f32",
865 ctx
->f32
, &selcoords
.ma
, 1, AC_FUNC_ATTR_READNONE
);
866 invma
= ac_build_fdiv(ctx
, LLVMConstReal(ctx
->f32
, 1.0), invma
);
868 for (int i
= 0; i
< 2; ++i
)
869 coords
[i
] = LLVMBuildFMul(builder
, selcoords
.stc
[i
], invma
, "");
871 coords
[2] = selcoords
.id
;
873 if (is_deriv
&& derivs_arg
) {
874 LLVMValueRef derivs
[4];
877 /* Convert cube derivatives to 2D derivatives. */
878 for (axis
= 0; axis
< 2; axis
++) {
879 LLVMValueRef deriv_st
[2];
880 LLVMValueRef deriv_ma
;
882 /* Transform the derivative alongside the texture
883 * coordinate. Mathematically, the correct formula is
884 * as follows. Assume we're projecting onto the +Z face
885 * and denote by dx/dh the derivative of the (original)
886 * X texture coordinate with respect to horizontal
887 * window coordinates. The projection onto the +Z face
892 * Then df/dh = df/dx * dx/dh + df/dz * dz/dh
893 * = 1/z * dx/dh - x/z * 1/z * dz/dh.
895 * This motivatives the implementation below.
897 * Whether this actually gives the expected results for
898 * apps that might feed in derivatives obtained via
899 * finite differences is anyone's guess. The OpenGL spec
900 * seems awfully quiet about how textureGrad for cube
901 * maps should be handled.
903 build_cube_select(ctx
, &selcoords
, &derivs_arg
[axis
* 3],
904 deriv_st
, &deriv_ma
);
906 deriv_ma
= LLVMBuildFMul(builder
, deriv_ma
, invma
, "");
908 for (int i
= 0; i
< 2; ++i
)
909 derivs
[axis
* 2 + i
] =
910 LLVMBuildFSub(builder
,
911 LLVMBuildFMul(builder
, deriv_st
[i
], invma
, ""),
912 LLVMBuildFMul(builder
, deriv_ma
, coords
[i
], ""), "");
915 memcpy(derivs_arg
, derivs
, sizeof(derivs
));
918 /* Shift the texture coordinate. This must be applied after the
919 * derivative calculation.
921 for (int i
= 0; i
< 2; ++i
)
922 coords
[i
] = LLVMBuildFAdd(builder
, coords
[i
], LLVMConstReal(ctx
->f32
, 1.5), "");
925 /* for cube arrays coord.z = coord.w(array_index) * 8 + face */
926 /* coords_arg.w component - array_index for cube arrays */
927 coords
[2] = ac_build_fmad(ctx
, coords_arg
[3], LLVMConstReal(ctx
->f32
, 8.0), coords
[2]);
930 memcpy(coords_arg
, coords
, sizeof(coords
));
935 ac_build_fs_interp(struct ac_llvm_context
*ctx
,
936 LLVMValueRef llvm_chan
,
937 LLVMValueRef attr_number
,
942 LLVMValueRef args
[5];
947 args
[2] = attr_number
;
950 p1
= ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p1",
951 ctx
->f32
, args
, 4, AC_FUNC_ATTR_READNONE
);
956 args
[3] = attr_number
;
959 return ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p2",
960 ctx
->f32
, args
, 5, AC_FUNC_ATTR_READNONE
);
964 ac_build_fs_interp_f16(struct ac_llvm_context
*ctx
,
965 LLVMValueRef llvm_chan
,
966 LLVMValueRef attr_number
,
971 LLVMValueRef args
[6];
976 args
[2] = attr_number
;
977 args
[3] = ctx
->i1false
;
980 p1
= ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p1.f16",
981 ctx
->f32
, args
, 5, AC_FUNC_ATTR_READNONE
);
986 args
[3] = attr_number
;
987 args
[4] = ctx
->i1false
;
990 return ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p2.f16",
991 ctx
->f16
, args
, 6, AC_FUNC_ATTR_READNONE
);
995 ac_build_fs_interp_mov(struct ac_llvm_context
*ctx
,
996 LLVMValueRef parameter
,
997 LLVMValueRef llvm_chan
,
998 LLVMValueRef attr_number
,
1001 LLVMValueRef args
[4];
1003 args
[0] = parameter
;
1004 args
[1] = llvm_chan
;
1005 args
[2] = attr_number
;
1008 return ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.mov",
1009 ctx
->f32
, args
, 4, AC_FUNC_ATTR_READNONE
);
1013 ac_build_gep_ptr(struct ac_llvm_context
*ctx
,
1014 LLVMValueRef base_ptr
,
1017 return LLVMBuildGEP(ctx
->builder
, base_ptr
, &index
, 1, "");
1021 ac_build_gep0(struct ac_llvm_context
*ctx
,
1022 LLVMValueRef base_ptr
,
1025 LLVMValueRef indices
[2] = {
1029 return LLVMBuildGEP(ctx
->builder
, base_ptr
, indices
, 2, "");
1032 LLVMValueRef
ac_build_pointer_add(struct ac_llvm_context
*ctx
, LLVMValueRef ptr
,
1035 return LLVMBuildPointerCast(ctx
->builder
,
1036 LLVMBuildGEP(ctx
->builder
, ptr
, &index
, 1, ""),
1037 LLVMTypeOf(ptr
), "");
1041 ac_build_indexed_store(struct ac_llvm_context
*ctx
,
1042 LLVMValueRef base_ptr
, LLVMValueRef index
,
1045 LLVMBuildStore(ctx
->builder
, value
,
1046 ac_build_gep0(ctx
, base_ptr
, index
));
1050 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad.
1051 * It's equivalent to doing a load from &base_ptr[index].
1053 * \param base_ptr Where the array starts.
1054 * \param index The element index into the array.
1055 * \param uniform Whether the base_ptr and index can be assumed to be
1056 * dynamically uniform (i.e. load to an SGPR)
1057 * \param invariant Whether the load is invariant (no other opcodes affect it)
1058 * \param no_unsigned_wraparound
1059 * For all possible re-associations and re-distributions of an expression
1060 * "base_ptr + index * elemsize" into "addr + offset" (excluding GEPs
1061 * without inbounds in base_ptr), this parameter is true if "addr + offset"
1062 * does not result in an unsigned integer wraparound. This is used for
1063 * optimal code generation of 32-bit pointer arithmetic.
1065 * For example, a 32-bit immediate offset that causes a 32-bit unsigned
1066 * integer wraparound can't be an imm offset in s_load_dword, because
1067 * the instruction performs "addr + offset" in 64 bits.
1069 * Expected usage for bindless textures by chaining GEPs:
1070 * // possible unsigned wraparound, don't use InBounds:
1071 * ptr1 = LLVMBuildGEP(base_ptr, index);
1072 * image = load(ptr1); // becomes "s_load ptr1, 0"
1074 * ptr2 = LLVMBuildInBoundsGEP(ptr1, 32 / elemsize);
1075 * sampler = load(ptr2); // becomes "s_load ptr1, 32" thanks to InBounds
1078 ac_build_load_custom(struct ac_llvm_context
*ctx
, LLVMValueRef base_ptr
,
1079 LLVMValueRef index
, bool uniform
, bool invariant
,
1080 bool no_unsigned_wraparound
)
1082 LLVMValueRef pointer
, result
;
1084 if (no_unsigned_wraparound
&&
1085 LLVMGetPointerAddressSpace(LLVMTypeOf(base_ptr
)) == AC_ADDR_SPACE_CONST_32BIT
)
1086 pointer
= LLVMBuildInBoundsGEP(ctx
->builder
, base_ptr
, &index
, 1, "");
1088 pointer
= LLVMBuildGEP(ctx
->builder
, base_ptr
, &index
, 1, "");
1091 LLVMSetMetadata(pointer
, ctx
->uniform_md_kind
, ctx
->empty_md
);
1092 result
= LLVMBuildLoad(ctx
->builder
, pointer
, "");
1094 LLVMSetMetadata(result
, ctx
->invariant_load_md_kind
, ctx
->empty_md
);
1098 LLVMValueRef
ac_build_load(struct ac_llvm_context
*ctx
, LLVMValueRef base_ptr
,
1101 return ac_build_load_custom(ctx
, base_ptr
, index
, false, false, false);
1104 LLVMValueRef
ac_build_load_invariant(struct ac_llvm_context
*ctx
,
1105 LLVMValueRef base_ptr
, LLVMValueRef index
)
1107 return ac_build_load_custom(ctx
, base_ptr
, index
, false, true, false);
1110 /* This assumes that there is no unsigned integer wraparound during the address
1111 * computation, excluding all GEPs within base_ptr. */
1112 LLVMValueRef
ac_build_load_to_sgpr(struct ac_llvm_context
*ctx
,
1113 LLVMValueRef base_ptr
, LLVMValueRef index
)
1115 return ac_build_load_custom(ctx
, base_ptr
, index
, true, true, true);
1118 /* See ac_build_load_custom() documentation. */
1119 LLVMValueRef
ac_build_load_to_sgpr_uint_wraparound(struct ac_llvm_context
*ctx
,
1120 LLVMValueRef base_ptr
, LLVMValueRef index
)
1122 return ac_build_load_custom(ctx
, base_ptr
, index
, true, true, false);
1125 static unsigned get_load_cache_policy(struct ac_llvm_context
*ctx
,
1126 unsigned cache_policy
)
1128 return cache_policy
|
1129 (ctx
->chip_class
>= GFX10
&& cache_policy
& ac_glc
? ac_dlc
: 0);
1133 ac_build_llvm7_buffer_store_common(struct ac_llvm_context
*ctx
,
1136 LLVMValueRef vindex
,
1137 LLVMValueRef voffset
,
1138 unsigned num_channels
,
1139 unsigned cache_policy
,
1142 LLVMValueRef args
[] = {
1144 LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, ""),
1145 vindex
? vindex
: ctx
->i32_0
,
1147 LLVMConstInt(ctx
->i1
, !!(cache_policy
& ac_glc
), 0),
1148 LLVMConstInt(ctx
->i1
, !!(cache_policy
& ac_slc
), 0)
1150 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
1152 const char *type_names
[] = {"f32", "v2f32", "v4f32"};
1156 snprintf(name
, sizeof(name
), "llvm.amdgcn.buffer.store.format.%s",
1159 snprintf(name
, sizeof(name
), "llvm.amdgcn.buffer.store.%s",
1163 ac_build_intrinsic(ctx
, name
, ctx
->voidt
, args
, ARRAY_SIZE(args
),
1164 AC_FUNC_ATTR_INACCESSIBLE_MEM_ONLY
);
1168 ac_build_llvm8_buffer_store_common(struct ac_llvm_context
*ctx
,
1171 LLVMValueRef vindex
,
1172 LLVMValueRef voffset
,
1173 LLVMValueRef soffset
,
1174 unsigned num_channels
,
1175 LLVMTypeRef return_channel_type
,
1176 unsigned cache_policy
,
1180 LLVMValueRef args
[6];
1183 args
[idx
++] = LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, "");
1185 args
[idx
++] = vindex
? vindex
: ctx
->i32_0
;
1186 args
[idx
++] = voffset
? voffset
: ctx
->i32_0
;
1187 args
[idx
++] = soffset
? soffset
: ctx
->i32_0
;
1188 args
[idx
++] = LLVMConstInt(ctx
->i32
, cache_policy
, 0);
1189 unsigned func
= !ac_has_vec3_support(ctx
->chip_class
, use_format
) && num_channels
== 3 ? 4 : num_channels
;
1190 const char *indexing_kind
= structurized
? "struct" : "raw";
1191 char name
[256], type_name
[8];
1193 LLVMTypeRef type
= func
> 1 ? LLVMVectorType(return_channel_type
, func
) : return_channel_type
;
1194 ac_build_type_name_for_intr(type
, type_name
, sizeof(type_name
));
1197 snprintf(name
, sizeof(name
), "llvm.amdgcn.%s.buffer.store.format.%s",
1198 indexing_kind
, type_name
);
1200 snprintf(name
, sizeof(name
), "llvm.amdgcn.%s.buffer.store.%s",
1201 indexing_kind
, type_name
);
1204 ac_build_intrinsic(ctx
, name
, ctx
->voidt
, args
, idx
,
1205 AC_FUNC_ATTR_INACCESSIBLE_MEM_ONLY
);
1209 ac_build_buffer_store_format(struct ac_llvm_context
*ctx
,
1212 LLVMValueRef vindex
,
1213 LLVMValueRef voffset
,
1214 unsigned num_channels
,
1215 unsigned cache_policy
)
1217 if (HAVE_LLVM
>= 0x800) {
1218 ac_build_llvm8_buffer_store_common(ctx
, rsrc
, data
, vindex
,
1219 voffset
, NULL
, num_channels
,
1220 ctx
->f32
, cache_policy
,
1223 ac_build_llvm7_buffer_store_common(ctx
, rsrc
, data
, vindex
, voffset
,
1224 num_channels
, cache_policy
,
1229 /* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
1230 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
1231 * or v4i32 (num_channels=3,4).
1234 ac_build_buffer_store_dword(struct ac_llvm_context
*ctx
,
1237 unsigned num_channels
,
1238 LLVMValueRef voffset
,
1239 LLVMValueRef soffset
,
1240 unsigned inst_offset
,
1241 unsigned cache_policy
,
1242 bool swizzle_enable_hint
)
1244 /* Split 3 channel stores, because only LLVM 9+ support 3-channel
1246 if (num_channels
== 3 && !ac_has_vec3_support(ctx
->chip_class
, false)) {
1247 LLVMValueRef v
[3], v01
;
1249 for (int i
= 0; i
< 3; i
++) {
1250 v
[i
] = LLVMBuildExtractElement(ctx
->builder
, vdata
,
1251 LLVMConstInt(ctx
->i32
, i
, 0), "");
1253 v01
= ac_build_gather_values(ctx
, v
, 2);
1255 ac_build_buffer_store_dword(ctx
, rsrc
, v01
, 2, voffset
,
1256 soffset
, inst_offset
, cache_policy
,
1257 swizzle_enable_hint
);
1258 ac_build_buffer_store_dword(ctx
, rsrc
, v
[2], 1, voffset
,
1259 soffset
, inst_offset
+ 8,
1261 swizzle_enable_hint
);
1265 /* SWIZZLE_ENABLE requires that soffset isn't folded into voffset
1266 * (voffset is swizzled, but soffset isn't swizzled).
1267 * llvm.amdgcn.buffer.store doesn't have a separate soffset parameter.
1269 if (!swizzle_enable_hint
) {
1270 LLVMValueRef offset
= soffset
;
1273 offset
= LLVMBuildAdd(ctx
->builder
, offset
,
1274 LLVMConstInt(ctx
->i32
, inst_offset
, 0), "");
1276 if (HAVE_LLVM
>= 0x800) {
1277 ac_build_llvm8_buffer_store_common(ctx
, rsrc
,
1278 ac_to_float(ctx
, vdata
),
1287 offset
= LLVMBuildAdd(ctx
->builder
, offset
, voffset
, "");
1289 ac_build_llvm7_buffer_store_common(ctx
, rsrc
,
1290 ac_to_float(ctx
, vdata
),
1292 num_channels
, cache_policy
,
1298 static const unsigned dfmts
[] = {
1299 V_008F0C_BUF_DATA_FORMAT_32
,
1300 V_008F0C_BUF_DATA_FORMAT_32_32
,
1301 V_008F0C_BUF_DATA_FORMAT_32_32_32
,
1302 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
1304 unsigned dfmt
= dfmts
[num_channels
- 1];
1305 unsigned nfmt
= V_008F0C_BUF_NUM_FORMAT_UINT
;
1306 LLVMValueRef immoffset
= LLVMConstInt(ctx
->i32
, inst_offset
, 0);
1308 ac_build_raw_tbuffer_store(ctx
, rsrc
, vdata
, voffset
, soffset
,
1309 immoffset
, num_channels
, dfmt
, nfmt
, cache_policy
);
1313 ac_build_llvm7_buffer_load_common(struct ac_llvm_context
*ctx
,
1315 LLVMValueRef vindex
,
1316 LLVMValueRef voffset
,
1317 unsigned num_channels
,
1318 unsigned cache_policy
,
1322 LLVMValueRef args
[] = {
1323 LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, ""),
1324 vindex
? vindex
: ctx
->i32_0
,
1326 LLVMConstInt(ctx
->i1
, !!(cache_policy
& ac_glc
), 0),
1327 LLVMConstInt(ctx
->i1
, !!(cache_policy
& ac_slc
), 0)
1329 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
1331 LLVMTypeRef types
[] = {ctx
->f32
, ctx
->v2f32
, ctx
->v4f32
};
1332 const char *type_names
[] = {"f32", "v2f32", "v4f32"};
1336 snprintf(name
, sizeof(name
), "llvm.amdgcn.buffer.load.format.%s",
1339 snprintf(name
, sizeof(name
), "llvm.amdgcn.buffer.load.%s",
1343 return ac_build_intrinsic(ctx
, name
, types
[func
], args
,
1345 ac_get_load_intr_attribs(can_speculate
));
1349 ac_build_llvm8_buffer_load_common(struct ac_llvm_context
*ctx
,
1351 LLVMValueRef vindex
,
1352 LLVMValueRef voffset
,
1353 LLVMValueRef soffset
,
1354 unsigned num_channels
,
1355 LLVMTypeRef channel_type
,
1356 unsigned cache_policy
,
1361 LLVMValueRef args
[5];
1363 args
[idx
++] = LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, "");
1365 args
[idx
++] = vindex
? vindex
: ctx
->i32_0
;
1366 args
[idx
++] = voffset
? voffset
: ctx
->i32_0
;
1367 args
[idx
++] = soffset
? soffset
: ctx
->i32_0
;
1368 args
[idx
++] = LLVMConstInt(ctx
->i32
, get_load_cache_policy(ctx
, cache_policy
), 0);
1369 unsigned func
= !ac_has_vec3_support(ctx
->chip_class
, use_format
) && num_channels
== 3 ? 4 : num_channels
;
1370 const char *indexing_kind
= structurized
? "struct" : "raw";
1371 char name
[256], type_name
[8];
1373 LLVMTypeRef type
= func
> 1 ? LLVMVectorType(channel_type
, func
) : channel_type
;
1374 ac_build_type_name_for_intr(type
, type_name
, sizeof(type_name
));
1377 snprintf(name
, sizeof(name
), "llvm.amdgcn.%s.buffer.load.format.%s",
1378 indexing_kind
, type_name
);
1380 snprintf(name
, sizeof(name
), "llvm.amdgcn.%s.buffer.load.%s",
1381 indexing_kind
, type_name
);
1384 return ac_build_intrinsic(ctx
, name
, type
, args
, idx
,
1385 ac_get_load_intr_attribs(can_speculate
));
1389 ac_build_buffer_load(struct ac_llvm_context
*ctx
,
1392 LLVMValueRef vindex
,
1393 LLVMValueRef voffset
,
1394 LLVMValueRef soffset
,
1395 unsigned inst_offset
,
1396 unsigned cache_policy
,
1400 LLVMValueRef offset
= LLVMConstInt(ctx
->i32
, inst_offset
, 0);
1402 offset
= LLVMBuildAdd(ctx
->builder
, offset
, voffset
, "");
1404 offset
= LLVMBuildAdd(ctx
->builder
, offset
, soffset
, "");
1406 if (allow_smem
&& !(cache_policy
& ac_slc
) &&
1407 (!(cache_policy
& ac_glc
) || (HAVE_LLVM
>= 0x0800 && ctx
->chip_class
>= GFX8
))) {
1408 assert(vindex
== NULL
);
1410 LLVMValueRef result
[8];
1412 for (int i
= 0; i
< num_channels
; i
++) {
1414 offset
= LLVMBuildAdd(ctx
->builder
, offset
,
1415 LLVMConstInt(ctx
->i32
, 4, 0), "");
1417 const char *intrname
=
1418 HAVE_LLVM
>= 0x0800 ? "llvm.amdgcn.s.buffer.load.f32"
1419 : "llvm.SI.load.const.v4i32";
1420 unsigned num_args
= HAVE_LLVM
>= 0x0800 ? 3 : 2;
1421 LLVMValueRef args
[3] = {
1424 LLVMConstInt(ctx
->i32
, get_load_cache_policy(ctx
, cache_policy
), 0),
1426 result
[i
] = ac_build_intrinsic(ctx
, intrname
,
1427 ctx
->f32
, args
, num_args
,
1428 AC_FUNC_ATTR_READNONE
|
1429 (HAVE_LLVM
< 0x0800 ? AC_FUNC_ATTR_LEGACY
: 0));
1431 if (num_channels
== 1)
1434 if (num_channels
== 3 && !ac_has_vec3_support(ctx
->chip_class
, false))
1435 result
[num_channels
++] = LLVMGetUndef(ctx
->f32
);
1436 return ac_build_gather_values(ctx
, result
, num_channels
);
1439 if (HAVE_LLVM
>= 0x0800) {
1440 return ac_build_llvm8_buffer_load_common(ctx
, rsrc
, vindex
,
1442 num_channels
, ctx
->f32
,
1444 can_speculate
, false,
1448 return ac_build_llvm7_buffer_load_common(ctx
, rsrc
, vindex
, offset
,
1449 num_channels
, cache_policy
,
1450 can_speculate
, false);
1453 LLVMValueRef
ac_build_buffer_load_format(struct ac_llvm_context
*ctx
,
1455 LLVMValueRef vindex
,
1456 LLVMValueRef voffset
,
1457 unsigned num_channels
,
1458 unsigned cache_policy
,
1461 if (HAVE_LLVM
>= 0x800) {
1462 return ac_build_llvm8_buffer_load_common(ctx
, rsrc
, vindex
, voffset
, ctx
->i32_0
,
1463 num_channels
, ctx
->f32
,
1464 cache_policy
, can_speculate
, true, true);
1466 return ac_build_llvm7_buffer_load_common(ctx
, rsrc
, vindex
, voffset
,
1467 num_channels
, cache_policy
,
1468 can_speculate
, true);
1471 LLVMValueRef
ac_build_buffer_load_format_gfx9_safe(struct ac_llvm_context
*ctx
,
1473 LLVMValueRef vindex
,
1474 LLVMValueRef voffset
,
1475 unsigned num_channels
,
1476 unsigned cache_policy
,
1479 if (HAVE_LLVM
>= 0x800) {
1480 return ac_build_llvm8_buffer_load_common(ctx
, rsrc
, vindex
, voffset
, ctx
->i32_0
,
1481 num_channels
, ctx
->f32
,
1482 cache_policy
, can_speculate
, true, true);
1485 LLVMValueRef elem_count
= LLVMBuildExtractElement(ctx
->builder
, rsrc
, LLVMConstInt(ctx
->i32
, 2, 0), "");
1486 LLVMValueRef stride
= LLVMBuildExtractElement(ctx
->builder
, rsrc
, ctx
->i32_1
, "");
1487 stride
= LLVMBuildLShr(ctx
->builder
, stride
, LLVMConstInt(ctx
->i32
, 16, 0), "");
1489 LLVMValueRef new_elem_count
= LLVMBuildSelect(ctx
->builder
,
1490 LLVMBuildICmp(ctx
->builder
, LLVMIntUGT
, elem_count
, stride
, ""),
1491 elem_count
, stride
, "");
1493 LLVMValueRef new_rsrc
= LLVMBuildInsertElement(ctx
->builder
, rsrc
, new_elem_count
,
1494 LLVMConstInt(ctx
->i32
, 2, 0), "");
1496 return ac_build_llvm7_buffer_load_common(ctx
, new_rsrc
, vindex
, voffset
,
1497 num_channels
, cache_policy
,
1498 can_speculate
, true);
1501 /// Translate a (dfmt, nfmt) pair into a chip-appropriate combined format
1502 /// value for LLVM8+ tbuffer intrinsics.
1504 ac_get_tbuffer_format(struct ac_llvm_context
*ctx
,
1505 unsigned dfmt
, unsigned nfmt
)
1507 if (ctx
->chip_class
>= GFX10
) {
1510 default: unreachable("bad dfmt");
1511 case V_008F0C_BUF_DATA_FORMAT_INVALID
: format
= V_008F0C_IMG_FORMAT_INVALID
; break;
1512 case V_008F0C_BUF_DATA_FORMAT_8
: format
= V_008F0C_IMG_FORMAT_8_UINT
; break;
1513 case V_008F0C_BUF_DATA_FORMAT_8_8
: format
= V_008F0C_IMG_FORMAT_8_8_UINT
; break;
1514 case V_008F0C_BUF_DATA_FORMAT_8_8_8_8
: format
= V_008F0C_IMG_FORMAT_8_8_8_8_UINT
; break;
1515 case V_008F0C_BUF_DATA_FORMAT_16
: format
= V_008F0C_IMG_FORMAT_16_UINT
; break;
1516 case V_008F0C_BUF_DATA_FORMAT_16_16
: format
= V_008F0C_IMG_FORMAT_16_16_UINT
; break;
1517 case V_008F0C_BUF_DATA_FORMAT_16_16_16_16
: format
= V_008F0C_IMG_FORMAT_16_16_16_16_UINT
; break;
1518 case V_008F0C_BUF_DATA_FORMAT_32
: format
= V_008F0C_IMG_FORMAT_32_UINT
; break;
1519 case V_008F0C_BUF_DATA_FORMAT_32_32
: format
= V_008F0C_IMG_FORMAT_32_32_UINT
; break;
1520 case V_008F0C_BUF_DATA_FORMAT_32_32_32
: format
= V_008F0C_IMG_FORMAT_32_32_32_UINT
; break;
1521 case V_008F0C_BUF_DATA_FORMAT_32_32_32_32
: format
= V_008F0C_IMG_FORMAT_32_32_32_32_UINT
; break;
1522 case V_008F0C_BUF_DATA_FORMAT_2_10_10_10
: format
= V_008F0C_IMG_FORMAT_2_10_10_10_UINT
; break;
1525 // Use the regularity properties of the combined format enum.
1527 // Note: float is incompatible with 8-bit data formats,
1528 // [us]{norm,scaled} are incomparible with 32-bit data formats.
1529 // [us]scaled are not writable.
1531 case V_008F0C_BUF_NUM_FORMAT_UNORM
: format
-= 4; break;
1532 case V_008F0C_BUF_NUM_FORMAT_SNORM
: format
-= 3; break;
1533 case V_008F0C_BUF_NUM_FORMAT_USCALED
: format
-= 2; break;
1534 case V_008F0C_BUF_NUM_FORMAT_SSCALED
: format
-= 1; break;
1535 default: unreachable("bad nfmt");
1536 case V_008F0C_BUF_NUM_FORMAT_UINT
: break;
1537 case V_008F0C_BUF_NUM_FORMAT_SINT
: format
+= 1; break;
1538 case V_008F0C_BUF_NUM_FORMAT_FLOAT
: format
+= 2; break;
1543 return dfmt
| (nfmt
<< 4);
1548 ac_build_llvm8_tbuffer_load(struct ac_llvm_context
*ctx
,
1550 LLVMValueRef vindex
,
1551 LLVMValueRef voffset
,
1552 LLVMValueRef soffset
,
1553 unsigned num_channels
,
1556 unsigned cache_policy
,
1560 LLVMValueRef args
[6];
1562 args
[idx
++] = LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, "");
1564 args
[idx
++] = vindex
? vindex
: ctx
->i32_0
;
1565 args
[idx
++] = voffset
? voffset
: ctx
->i32_0
;
1566 args
[idx
++] = soffset
? soffset
: ctx
->i32_0
;
1567 args
[idx
++] = LLVMConstInt(ctx
->i32
, ac_get_tbuffer_format(ctx
, dfmt
, nfmt
), 0);
1568 args
[idx
++] = LLVMConstInt(ctx
->i32
, get_load_cache_policy(ctx
, cache_policy
), 0);
1569 unsigned func
= !ac_has_vec3_support(ctx
->chip_class
, true) && num_channels
== 3 ? 4 : num_channels
;
1570 const char *indexing_kind
= structurized
? "struct" : "raw";
1571 char name
[256], type_name
[8];
1573 LLVMTypeRef type
= func
> 1 ? LLVMVectorType(ctx
->i32
, func
) : ctx
->i32
;
1574 ac_build_type_name_for_intr(type
, type_name
, sizeof(type_name
));
1576 snprintf(name
, sizeof(name
), "llvm.amdgcn.%s.tbuffer.load.%s",
1577 indexing_kind
, type_name
);
1579 return ac_build_intrinsic(ctx
, name
, type
, args
, idx
,
1580 ac_get_load_intr_attribs(can_speculate
));
1584 ac_build_tbuffer_load(struct ac_llvm_context
*ctx
,
1586 LLVMValueRef vindex
,
1587 LLVMValueRef voffset
,
1588 LLVMValueRef soffset
,
1589 LLVMValueRef immoffset
,
1590 unsigned num_channels
,
1593 unsigned cache_policy
,
1595 bool structurized
) /* only matters for LLVM 8+ */
1597 if (HAVE_LLVM
>= 0x800) {
1598 voffset
= LLVMBuildAdd(ctx
->builder
, voffset
, immoffset
, "");
1600 return ac_build_llvm8_tbuffer_load(ctx
, rsrc
, vindex
, voffset
,
1601 soffset
, num_channels
,
1602 dfmt
, nfmt
, cache_policy
,
1603 can_speculate
, structurized
);
1606 LLVMValueRef args
[] = {
1608 vindex
? vindex
: ctx
->i32_0
,
1612 LLVMConstInt(ctx
->i32
, dfmt
, false),
1613 LLVMConstInt(ctx
->i32
, nfmt
, false),
1614 LLVMConstInt(ctx
->i1
, !!(cache_policy
& ac_glc
), false),
1615 LLVMConstInt(ctx
->i1
, !!(cache_policy
& ac_slc
), false),
1617 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
1618 LLVMTypeRef types
[] = {ctx
->i32
, ctx
->v2i32
, ctx
->v4i32
};
1619 const char *type_names
[] = {"i32", "v2i32", "v4i32"};
1622 snprintf(name
, sizeof(name
), "llvm.amdgcn.tbuffer.load.%s",
1625 return ac_build_intrinsic(ctx
, name
, types
[func
], args
, 9,
1626 ac_get_load_intr_attribs(can_speculate
));
1630 ac_build_struct_tbuffer_load(struct ac_llvm_context
*ctx
,
1632 LLVMValueRef vindex
,
1633 LLVMValueRef voffset
,
1634 LLVMValueRef soffset
,
1635 LLVMValueRef immoffset
,
1636 unsigned num_channels
,
1639 unsigned cache_policy
,
1642 return ac_build_tbuffer_load(ctx
, rsrc
, vindex
, voffset
, soffset
,
1643 immoffset
, num_channels
, dfmt
, nfmt
,
1644 cache_policy
, can_speculate
, true);
1648 ac_build_raw_tbuffer_load(struct ac_llvm_context
*ctx
,
1650 LLVMValueRef voffset
,
1651 LLVMValueRef soffset
,
1652 LLVMValueRef immoffset
,
1653 unsigned num_channels
,
1656 unsigned cache_policy
,
1659 return ac_build_tbuffer_load(ctx
, rsrc
, NULL
, voffset
, soffset
,
1660 immoffset
, num_channels
, dfmt
, nfmt
,
1661 cache_policy
, can_speculate
, false);
1665 ac_build_tbuffer_load_short(struct ac_llvm_context
*ctx
,
1667 LLVMValueRef voffset
,
1668 LLVMValueRef soffset
,
1669 LLVMValueRef immoffset
,
1670 unsigned cache_policy
)
1674 if (HAVE_LLVM
>= 0x900) {
1675 voffset
= LLVMBuildAdd(ctx
->builder
, voffset
, immoffset
, "");
1677 /* LLVM 9+ supports i8/i16 with struct/raw intrinsics. */
1678 res
= ac_build_llvm8_buffer_load_common(ctx
, rsrc
, NULL
,
1680 1, ctx
->i16
, cache_policy
,
1681 false, false, false);
1683 unsigned dfmt
= V_008F0C_BUF_DATA_FORMAT_16
;
1684 unsigned nfmt
= V_008F0C_BUF_NUM_FORMAT_UINT
;
1686 res
= ac_build_raw_tbuffer_load(ctx
, rsrc
, voffset
, soffset
,
1687 immoffset
, 1, dfmt
, nfmt
, cache_policy
,
1690 res
= LLVMBuildTrunc(ctx
->builder
, res
, ctx
->i16
, "");
1697 ac_build_tbuffer_load_byte(struct ac_llvm_context
*ctx
,
1699 LLVMValueRef voffset
,
1700 LLVMValueRef soffset
,
1701 LLVMValueRef immoffset
,
1702 unsigned cache_policy
)
1706 if (HAVE_LLVM
>= 0x900) {
1707 voffset
= LLVMBuildAdd(ctx
->builder
, voffset
, immoffset
, "");
1709 /* LLVM 9+ supports i8/i16 with struct/raw intrinsics. */
1710 res
= ac_build_llvm8_buffer_load_common(ctx
, rsrc
, NULL
,
1712 1, ctx
->i8
, cache_policy
,
1713 false, false, false);
1715 unsigned dfmt
= V_008F0C_BUF_DATA_FORMAT_8
;
1716 unsigned nfmt
= V_008F0C_BUF_NUM_FORMAT_UINT
;
1718 res
= ac_build_raw_tbuffer_load(ctx
, rsrc
, voffset
, soffset
,
1719 immoffset
, 1, dfmt
, nfmt
, cache_policy
,
1722 res
= LLVMBuildTrunc(ctx
->builder
, res
, ctx
->i8
, "");
1729 * Convert an 11- or 10-bit unsigned floating point number to an f32.
1731 * The input exponent is expected to be biased analogous to IEEE-754, i.e. by
1732 * 2^(exp_bits-1) - 1 (as defined in OpenGL and other graphics APIs).
1735 ac_ufN_to_float(struct ac_llvm_context
*ctx
, LLVMValueRef src
, unsigned exp_bits
, unsigned mant_bits
)
1737 assert(LLVMTypeOf(src
) == ctx
->i32
);
1740 LLVMValueRef mantissa
;
1741 mantissa
= LLVMBuildAnd(ctx
->builder
, src
, LLVMConstInt(ctx
->i32
, (1 << mant_bits
) - 1, false), "");
1743 /* Converting normal numbers is just a shift + correcting the exponent bias */
1744 unsigned normal_shift
= 23 - mant_bits
;
1745 unsigned bias_shift
= 127 - ((1 << (exp_bits
- 1)) - 1);
1746 LLVMValueRef shifted
, normal
;
1748 shifted
= LLVMBuildShl(ctx
->builder
, src
, LLVMConstInt(ctx
->i32
, normal_shift
, false), "");
1749 normal
= LLVMBuildAdd(ctx
->builder
, shifted
, LLVMConstInt(ctx
->i32
, bias_shift
<< 23, false), "");
1751 /* Converting nan/inf numbers is the same, but with a different exponent update */
1752 LLVMValueRef naninf
;
1753 naninf
= LLVMBuildOr(ctx
->builder
, normal
, LLVMConstInt(ctx
->i32
, 0xff << 23, false), "");
1755 /* Converting denormals is the complex case: determine the leading zeros of the
1756 * mantissa to obtain the correct shift for the mantissa and exponent correction.
1758 LLVMValueRef denormal
;
1759 LLVMValueRef params
[2] = {
1761 ctx
->i1true
, /* result can be undef when arg is 0 */
1763 LLVMValueRef ctlz
= ac_build_intrinsic(ctx
, "llvm.ctlz.i32", ctx
->i32
,
1764 params
, 2, AC_FUNC_ATTR_READNONE
);
1766 /* Shift such that the leading 1 ends up as the LSB of the exponent field. */
1767 tmp
= LLVMBuildSub(ctx
->builder
, ctlz
, LLVMConstInt(ctx
->i32
, 8, false), "");
1768 denormal
= LLVMBuildShl(ctx
->builder
, mantissa
, tmp
, "");
1770 unsigned denormal_exp
= bias_shift
+ (32 - mant_bits
) - 1;
1771 tmp
= LLVMBuildSub(ctx
->builder
, LLVMConstInt(ctx
->i32
, denormal_exp
, false), ctlz
, "");
1772 tmp
= LLVMBuildShl(ctx
->builder
, tmp
, LLVMConstInt(ctx
->i32
, 23, false), "");
1773 denormal
= LLVMBuildAdd(ctx
->builder
, denormal
, tmp
, "");
1775 /* Select the final result. */
1776 LLVMValueRef result
;
1778 tmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntUGE
, src
,
1779 LLVMConstInt(ctx
->i32
, ((1 << exp_bits
) - 1) << mant_bits
, false), "");
1780 result
= LLVMBuildSelect(ctx
->builder
, tmp
, naninf
, normal
, "");
1782 tmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntUGE
, src
,
1783 LLVMConstInt(ctx
->i32
, 1 << mant_bits
, false), "");
1784 result
= LLVMBuildSelect(ctx
->builder
, tmp
, result
, denormal
, "");
1786 tmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src
, ctx
->i32_0
, "");
1787 result
= LLVMBuildSelect(ctx
->builder
, tmp
, result
, ctx
->i32_0
, "");
1789 return ac_to_float(ctx
, result
);
1793 * Generate a fully general open coded buffer format fetch with all required
1794 * fixups suitable for vertex fetch, using non-format buffer loads.
1796 * Some combinations of argument values have special interpretations:
1797 * - size = 8 bytes, format = fixed indicates PIPE_FORMAT_R11G11B10_FLOAT
1798 * - size = 8 bytes, format != {float,fixed} indicates a 2_10_10_10 data format
1800 * \param log_size log(size of channel in bytes)
1801 * \param num_channels number of channels (1 to 4)
1802 * \param format AC_FETCH_FORMAT_xxx value
1803 * \param reverse whether XYZ channels are reversed
1804 * \param known_aligned whether the source is known to be aligned to hardware's
1805 * effective element size for loading the given format
1806 * (note: this means dword alignment for 8_8_8_8, 16_16, etc.)
1807 * \param rsrc buffer resource descriptor
1808 * \return the resulting vector of floats or integers bitcast to <4 x i32>
1811 ac_build_opencoded_load_format(struct ac_llvm_context
*ctx
,
1813 unsigned num_channels
,
1818 LLVMValueRef vindex
,
1819 LLVMValueRef voffset
,
1820 LLVMValueRef soffset
,
1821 unsigned cache_policy
,
1825 unsigned load_log_size
= log_size
;
1826 unsigned load_num_channels
= num_channels
;
1827 if (log_size
== 3) {
1829 if (format
== AC_FETCH_FORMAT_FLOAT
) {
1830 load_num_channels
= 2 * num_channels
;
1832 load_num_channels
= 1; /* 10_11_11 or 2_10_10_10 */
1836 int log_recombine
= 0;
1837 if (ctx
->chip_class
== GFX6
&& !known_aligned
) {
1838 /* Avoid alignment restrictions by loading one byte at a time. */
1839 load_num_channels
<<= load_log_size
;
1840 log_recombine
= load_log_size
;
1842 } else if (load_num_channels
== 2 || load_num_channels
== 4) {
1843 log_recombine
= -util_logbase2(load_num_channels
);
1844 load_num_channels
= 1;
1845 load_log_size
+= -log_recombine
;
1848 assert(load_log_size
>= 2 || HAVE_LLVM
>= 0x0900);
1850 LLVMValueRef loads
[32]; /* up to 32 bytes */
1851 for (unsigned i
= 0; i
< load_num_channels
; ++i
) {
1852 tmp
= LLVMBuildAdd(ctx
->builder
, soffset
,
1853 LLVMConstInt(ctx
->i32
, i
<< load_log_size
, false), "");
1854 if (HAVE_LLVM
>= 0x0800) {
1855 LLVMTypeRef channel_type
= load_log_size
== 0 ? ctx
->i8
:
1856 load_log_size
== 1 ? ctx
->i16
: ctx
->i32
;
1857 unsigned num_channels
= 1 << (MAX2(load_log_size
, 2) - 2);
1858 loads
[i
] = ac_build_llvm8_buffer_load_common(
1859 ctx
, rsrc
, vindex
, voffset
, tmp
,
1860 num_channels
, channel_type
, cache_policy
,
1861 can_speculate
, false, true);
1863 tmp
= LLVMBuildAdd(ctx
->builder
, voffset
, tmp
, "");
1864 loads
[i
] = ac_build_llvm7_buffer_load_common(
1865 ctx
, rsrc
, vindex
, tmp
,
1866 1 << (load_log_size
- 2), cache_policy
, can_speculate
, false);
1868 if (load_log_size
>= 2)
1869 loads
[i
] = ac_to_integer(ctx
, loads
[i
]);
1872 if (log_recombine
> 0) {
1873 /* Recombine bytes if necessary (GFX6 only) */
1874 LLVMTypeRef dst_type
= log_recombine
== 2 ? ctx
->i32
: ctx
->i16
;
1876 for (unsigned src
= 0, dst
= 0; src
< load_num_channels
; ++dst
) {
1877 LLVMValueRef accum
= NULL
;
1878 for (unsigned i
= 0; i
< (1 << log_recombine
); ++i
, ++src
) {
1879 tmp
= LLVMBuildZExt(ctx
->builder
, loads
[src
], dst_type
, "");
1883 tmp
= LLVMBuildShl(ctx
->builder
, tmp
,
1884 LLVMConstInt(dst_type
, 8 * i
, false), "");
1885 accum
= LLVMBuildOr(ctx
->builder
, accum
, tmp
, "");
1890 } else if (log_recombine
< 0) {
1891 /* Split vectors of dwords */
1892 if (load_log_size
> 2) {
1893 assert(load_num_channels
== 1);
1894 LLVMValueRef loaded
= loads
[0];
1895 unsigned log_split
= load_log_size
- 2;
1896 log_recombine
+= log_split
;
1897 load_num_channels
= 1 << log_split
;
1899 for (unsigned i
= 0; i
< load_num_channels
; ++i
) {
1900 tmp
= LLVMConstInt(ctx
->i32
, i
, false);
1901 loads
[i
] = LLVMBuildExtractElement(ctx
->builder
, loaded
, tmp
, "");
1905 /* Further split dwords and shorts if required */
1906 if (log_recombine
< 0) {
1907 for (unsigned src
= load_num_channels
,
1908 dst
= load_num_channels
<< -log_recombine
;
1910 unsigned dst_bits
= 1 << (3 + load_log_size
+ log_recombine
);
1911 LLVMTypeRef dst_type
= LLVMIntTypeInContext(ctx
->context
, dst_bits
);
1912 LLVMValueRef loaded
= loads
[src
- 1];
1913 LLVMTypeRef loaded_type
= LLVMTypeOf(loaded
);
1914 for (unsigned i
= 1 << -log_recombine
; i
> 0; --i
, --dst
) {
1915 tmp
= LLVMConstInt(loaded_type
, dst_bits
* (i
- 1), false);
1916 tmp
= LLVMBuildLShr(ctx
->builder
, loaded
, tmp
, "");
1917 loads
[dst
- 1] = LLVMBuildTrunc(ctx
->builder
, tmp
, dst_type
, "");
1923 if (log_size
== 3) {
1924 if (format
== AC_FETCH_FORMAT_FLOAT
) {
1925 for (unsigned i
= 0; i
< num_channels
; ++i
) {
1926 tmp
= ac_build_gather_values(ctx
, &loads
[2 * i
], 2);
1927 loads
[i
] = LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f64
, "");
1929 } else if (format
== AC_FETCH_FORMAT_FIXED
) {
1930 /* 10_11_11_FLOAT */
1931 LLVMValueRef data
= loads
[0];
1932 LLVMValueRef i32_2047
= LLVMConstInt(ctx
->i32
, 2047, false);
1933 LLVMValueRef r
= LLVMBuildAnd(ctx
->builder
, data
, i32_2047
, "");
1934 tmp
= LLVMBuildLShr(ctx
->builder
, data
, LLVMConstInt(ctx
->i32
, 11, false), "");
1935 LLVMValueRef g
= LLVMBuildAnd(ctx
->builder
, tmp
, i32_2047
, "");
1936 LLVMValueRef b
= LLVMBuildLShr(ctx
->builder
, data
, LLVMConstInt(ctx
->i32
, 22, false), "");
1938 loads
[0] = ac_to_integer(ctx
, ac_ufN_to_float(ctx
, r
, 5, 6));
1939 loads
[1] = ac_to_integer(ctx
, ac_ufN_to_float(ctx
, g
, 5, 6));
1940 loads
[2] = ac_to_integer(ctx
, ac_ufN_to_float(ctx
, b
, 5, 5));
1944 format
= AC_FETCH_FORMAT_FLOAT
;
1946 /* 2_10_10_10 data formats */
1947 LLVMValueRef data
= loads
[0];
1948 LLVMTypeRef i10
= LLVMIntTypeInContext(ctx
->context
, 10);
1949 LLVMTypeRef i2
= LLVMIntTypeInContext(ctx
->context
, 2);
1950 loads
[0] = LLVMBuildTrunc(ctx
->builder
, data
, i10
, "");
1951 tmp
= LLVMBuildLShr(ctx
->builder
, data
, LLVMConstInt(ctx
->i32
, 10, false), "");
1952 loads
[1] = LLVMBuildTrunc(ctx
->builder
, tmp
, i10
, "");
1953 tmp
= LLVMBuildLShr(ctx
->builder
, data
, LLVMConstInt(ctx
->i32
, 20, false), "");
1954 loads
[2] = LLVMBuildTrunc(ctx
->builder
, tmp
, i10
, "");
1955 tmp
= LLVMBuildLShr(ctx
->builder
, data
, LLVMConstInt(ctx
->i32
, 30, false), "");
1956 loads
[3] = LLVMBuildTrunc(ctx
->builder
, tmp
, i2
, "");
1962 if (format
== AC_FETCH_FORMAT_FLOAT
) {
1963 if (log_size
!= 2) {
1964 for (unsigned chan
= 0; chan
< num_channels
; ++chan
) {
1965 tmp
= ac_to_float(ctx
, loads
[chan
]);
1967 tmp
= LLVMBuildFPTrunc(ctx
->builder
, tmp
, ctx
->f32
, "");
1968 else if (log_size
== 1)
1969 tmp
= LLVMBuildFPExt(ctx
->builder
, tmp
, ctx
->f32
, "");
1970 loads
[chan
] = ac_to_integer(ctx
, tmp
);
1973 } else if (format
== AC_FETCH_FORMAT_UINT
) {
1974 if (log_size
!= 2) {
1975 for (unsigned chan
= 0; chan
< num_channels
; ++chan
)
1976 loads
[chan
] = LLVMBuildZExt(ctx
->builder
, loads
[chan
], ctx
->i32
, "");
1978 } else if (format
== AC_FETCH_FORMAT_SINT
) {
1979 if (log_size
!= 2) {
1980 for (unsigned chan
= 0; chan
< num_channels
; ++chan
)
1981 loads
[chan
] = LLVMBuildSExt(ctx
->builder
, loads
[chan
], ctx
->i32
, "");
1984 bool unsign
= format
== AC_FETCH_FORMAT_UNORM
||
1985 format
== AC_FETCH_FORMAT_USCALED
||
1986 format
== AC_FETCH_FORMAT_UINT
;
1988 for (unsigned chan
= 0; chan
< num_channels
; ++chan
) {
1990 tmp
= LLVMBuildUIToFP(ctx
->builder
, loads
[chan
], ctx
->f32
, "");
1992 tmp
= LLVMBuildSIToFP(ctx
->builder
, loads
[chan
], ctx
->f32
, "");
1995 LLVMValueRef scale
= NULL
;
1996 if (format
== AC_FETCH_FORMAT_FIXED
) {
1997 assert(log_size
== 2);
1998 scale
= LLVMConstReal(ctx
->f32
, 1.0 / 0x10000);
1999 } else if (format
== AC_FETCH_FORMAT_UNORM
) {
2000 unsigned bits
= LLVMGetIntTypeWidth(LLVMTypeOf(loads
[chan
]));
2001 scale
= LLVMConstReal(ctx
->f32
, 1.0 / (((uint64_t)1 << bits
) - 1));
2002 } else if (format
== AC_FETCH_FORMAT_SNORM
) {
2003 unsigned bits
= LLVMGetIntTypeWidth(LLVMTypeOf(loads
[chan
]));
2004 scale
= LLVMConstReal(ctx
->f32
, 1.0 / (((uint64_t)1 << (bits
- 1)) - 1));
2007 tmp
= LLVMBuildFMul(ctx
->builder
, tmp
, scale
, "");
2009 if (format
== AC_FETCH_FORMAT_SNORM
) {
2010 /* Clamp to [-1, 1] */
2011 LLVMValueRef neg_one
= LLVMConstReal(ctx
->f32
, -1.0);
2012 LLVMValueRef clamp
=
2013 LLVMBuildFCmp(ctx
->builder
, LLVMRealULT
, tmp
, neg_one
, "");
2014 tmp
= LLVMBuildSelect(ctx
->builder
, clamp
, neg_one
, tmp
, "");
2017 loads
[chan
] = ac_to_integer(ctx
, tmp
);
2021 while (num_channels
< 4) {
2022 if (format
== AC_FETCH_FORMAT_UINT
|| format
== AC_FETCH_FORMAT_SINT
) {
2023 loads
[num_channels
] = num_channels
== 3 ? ctx
->i32_1
: ctx
->i32_0
;
2025 loads
[num_channels
] = ac_to_integer(ctx
, num_channels
== 3 ? ctx
->f32_1
: ctx
->f32_0
);
2032 loads
[0] = loads
[2];
2036 return ac_build_gather_values(ctx
, loads
, 4);
2040 ac_build_llvm8_tbuffer_store(struct ac_llvm_context
*ctx
,
2043 LLVMValueRef vindex
,
2044 LLVMValueRef voffset
,
2045 LLVMValueRef soffset
,
2046 unsigned num_channels
,
2049 unsigned cache_policy
,
2052 LLVMValueRef args
[7];
2054 args
[idx
++] = vdata
;
2055 args
[idx
++] = LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, "");
2057 args
[idx
++] = vindex
? vindex
: ctx
->i32_0
;
2058 args
[idx
++] = voffset
? voffset
: ctx
->i32_0
;
2059 args
[idx
++] = soffset
? soffset
: ctx
->i32_0
;
2060 args
[idx
++] = LLVMConstInt(ctx
->i32
, ac_get_tbuffer_format(ctx
, dfmt
, nfmt
), 0);
2061 args
[idx
++] = LLVMConstInt(ctx
->i32
, cache_policy
, 0);
2062 unsigned func
= !ac_has_vec3_support(ctx
->chip_class
, true) && num_channels
== 3 ? 4 : num_channels
;
2063 const char *indexing_kind
= structurized
? "struct" : "raw";
2064 char name
[256], type_name
[8];
2066 LLVMTypeRef type
= func
> 1 ? LLVMVectorType(ctx
->i32
, func
) : ctx
->i32
;
2067 ac_build_type_name_for_intr(type
, type_name
, sizeof(type_name
));
2069 snprintf(name
, sizeof(name
), "llvm.amdgcn.%s.tbuffer.store.%s",
2070 indexing_kind
, type_name
);
2072 ac_build_intrinsic(ctx
, name
, ctx
->voidt
, args
, idx
,
2073 AC_FUNC_ATTR_INACCESSIBLE_MEM_ONLY
);
2077 ac_build_tbuffer_store(struct ac_llvm_context
*ctx
,
2080 LLVMValueRef vindex
,
2081 LLVMValueRef voffset
,
2082 LLVMValueRef soffset
,
2083 LLVMValueRef immoffset
,
2084 unsigned num_channels
,
2087 unsigned cache_policy
,
2088 bool structurized
) /* only matters for LLVM 8+ */
2090 if (HAVE_LLVM
>= 0x800) {
2091 voffset
= LLVMBuildAdd(ctx
->builder
,
2092 voffset
? voffset
: ctx
->i32_0
,
2095 ac_build_llvm8_tbuffer_store(ctx
, rsrc
, vdata
, vindex
, voffset
,
2096 soffset
, num_channels
, dfmt
, nfmt
,
2097 cache_policy
, structurized
);
2099 LLVMValueRef params
[] = {
2102 vindex
? vindex
: ctx
->i32_0
,
2103 voffset
? voffset
: ctx
->i32_0
,
2104 soffset
? soffset
: ctx
->i32_0
,
2106 LLVMConstInt(ctx
->i32
, dfmt
, false),
2107 LLVMConstInt(ctx
->i32
, nfmt
, false),
2108 LLVMConstInt(ctx
->i1
, !!(cache_policy
& ac_glc
), false),
2109 LLVMConstInt(ctx
->i1
, !!(cache_policy
& ac_slc
), false),
2111 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
2112 const char *type_names
[] = {"i32", "v2i32", "v4i32"};
2115 snprintf(name
, sizeof(name
), "llvm.amdgcn.tbuffer.store.%s",
2118 ac_build_intrinsic(ctx
, name
, ctx
->voidt
, params
, 10,
2119 AC_FUNC_ATTR_INACCESSIBLE_MEM_ONLY
);
2124 ac_build_struct_tbuffer_store(struct ac_llvm_context
*ctx
,
2127 LLVMValueRef vindex
,
2128 LLVMValueRef voffset
,
2129 LLVMValueRef soffset
,
2130 LLVMValueRef immoffset
,
2131 unsigned num_channels
,
2134 unsigned cache_policy
)
2136 ac_build_tbuffer_store(ctx
, rsrc
, vdata
, vindex
, voffset
, soffset
,
2137 immoffset
, num_channels
, dfmt
, nfmt
, cache_policy
,
2142 ac_build_raw_tbuffer_store(struct ac_llvm_context
*ctx
,
2145 LLVMValueRef voffset
,
2146 LLVMValueRef soffset
,
2147 LLVMValueRef immoffset
,
2148 unsigned num_channels
,
2151 unsigned cache_policy
)
2153 ac_build_tbuffer_store(ctx
, rsrc
, vdata
, NULL
, voffset
, soffset
,
2154 immoffset
, num_channels
, dfmt
, nfmt
, cache_policy
,
2159 ac_build_tbuffer_store_short(struct ac_llvm_context
*ctx
,
2162 LLVMValueRef voffset
,
2163 LLVMValueRef soffset
,
2164 unsigned cache_policy
)
2166 vdata
= LLVMBuildBitCast(ctx
->builder
, vdata
, ctx
->i16
, "");
2168 if (HAVE_LLVM
>= 0x900) {
2169 /* LLVM 9+ supports i8/i16 with struct/raw intrinsics. */
2170 ac_build_llvm8_buffer_store_common(ctx
, rsrc
, vdata
, NULL
,
2171 voffset
, soffset
, 1,
2172 ctx
->i16
, cache_policy
,
2175 unsigned dfmt
= V_008F0C_BUF_DATA_FORMAT_16
;
2176 unsigned nfmt
= V_008F0C_BUF_NUM_FORMAT_UINT
;
2178 vdata
= LLVMBuildZExt(ctx
->builder
, vdata
, ctx
->i32
, "");
2180 ac_build_raw_tbuffer_store(ctx
, rsrc
, vdata
, voffset
, soffset
,
2181 ctx
->i32_0
, 1, dfmt
, nfmt
, cache_policy
);
2186 ac_build_tbuffer_store_byte(struct ac_llvm_context
*ctx
,
2189 LLVMValueRef voffset
,
2190 LLVMValueRef soffset
,
2191 unsigned cache_policy
)
2193 vdata
= LLVMBuildBitCast(ctx
->builder
, vdata
, ctx
->i8
, "");
2195 if (HAVE_LLVM
>= 0x900) {
2196 /* LLVM 9+ supports i8/i16 with struct/raw intrinsics. */
2197 ac_build_llvm8_buffer_store_common(ctx
, rsrc
, vdata
, NULL
,
2198 voffset
, soffset
, 1,
2199 ctx
->i8
, cache_policy
,
2202 unsigned dfmt
= V_008F0C_BUF_DATA_FORMAT_8
;
2203 unsigned nfmt
= V_008F0C_BUF_NUM_FORMAT_UINT
;
2205 vdata
= LLVMBuildZExt(ctx
->builder
, vdata
, ctx
->i32
, "");
2207 ac_build_raw_tbuffer_store(ctx
, rsrc
, vdata
, voffset
, soffset
,
2208 ctx
->i32_0
, 1, dfmt
, nfmt
, cache_policy
);
2212 * Set range metadata on an instruction. This can only be used on load and
2213 * call instructions. If you know an instruction can only produce the values
2214 * 0, 1, 2, you would do set_range_metadata(value, 0, 3);
2215 * \p lo is the minimum value inclusive.
2216 * \p hi is the maximum value exclusive.
2218 static void set_range_metadata(struct ac_llvm_context
*ctx
,
2219 LLVMValueRef value
, unsigned lo
, unsigned hi
)
2221 LLVMValueRef range_md
, md_args
[2];
2222 LLVMTypeRef type
= LLVMTypeOf(value
);
2223 LLVMContextRef context
= LLVMGetTypeContext(type
);
2225 md_args
[0] = LLVMConstInt(type
, lo
, false);
2226 md_args
[1] = LLVMConstInt(type
, hi
, false);
2227 range_md
= LLVMMDNodeInContext(context
, md_args
, 2);
2228 LLVMSetMetadata(value
, ctx
->range_md_kind
, range_md
);
2232 ac_get_thread_id(struct ac_llvm_context
*ctx
)
2236 LLVMValueRef tid_args
[2];
2237 tid_args
[0] = LLVMConstInt(ctx
->i32
, 0xffffffff, false);
2238 tid_args
[1] = ctx
->i32_0
;
2239 tid_args
[1] = ac_build_intrinsic(ctx
,
2240 "llvm.amdgcn.mbcnt.lo", ctx
->i32
,
2241 tid_args
, 2, AC_FUNC_ATTR_READNONE
);
2243 if (ctx
->wave_size
== 32) {
2246 tid
= ac_build_intrinsic(ctx
, "llvm.amdgcn.mbcnt.hi",
2248 2, AC_FUNC_ATTR_READNONE
);
2250 set_range_metadata(ctx
, tid
, 0, ctx
->wave_size
);
2255 * AMD GCN implements derivatives using the local data store (LDS)
2256 * All writes to the LDS happen in all executing threads at
2257 * the same time. TID is the Thread ID for the current
2258 * thread and is a value between 0 and 63, representing
2259 * the thread's position in the wavefront.
2261 * For the pixel shader threads are grouped into quads of four pixels.
2262 * The TIDs of the pixels of a quad are:
2270 * So, masking the TID with 0xfffffffc yields the TID of the top left pixel
2271 * of the quad, masking with 0xfffffffd yields the TID of the top pixel of
2272 * the current pixel's column, and masking with 0xfffffffe yields the TID
2273 * of the left pixel of the current pixel's row.
2275 * Adding 1 yields the TID of the pixel to the right of the left pixel, and
2276 * adding 2 yields the TID of the pixel below the top pixel.
2279 ac_build_ddxy(struct ac_llvm_context
*ctx
,
2284 unsigned tl_lanes
[4], trbl_lanes
[4];
2285 char name
[32], type
[8];
2286 LLVMValueRef tl
, trbl
;
2287 LLVMTypeRef result_type
;
2288 LLVMValueRef result
;
2290 result_type
= ac_to_float_type(ctx
, LLVMTypeOf(val
));
2292 if (result_type
== ctx
->f16
)
2293 val
= LLVMBuildZExt(ctx
->builder
, val
, ctx
->i32
, "");
2295 for (unsigned i
= 0; i
< 4; ++i
) {
2296 tl_lanes
[i
] = i
& mask
;
2297 trbl_lanes
[i
] = (i
& mask
) + idx
;
2300 tl
= ac_build_quad_swizzle(ctx
, val
,
2301 tl_lanes
[0], tl_lanes
[1],
2302 tl_lanes
[2], tl_lanes
[3]);
2303 trbl
= ac_build_quad_swizzle(ctx
, val
,
2304 trbl_lanes
[0], trbl_lanes
[1],
2305 trbl_lanes
[2], trbl_lanes
[3]);
2307 if (result_type
== ctx
->f16
) {
2308 tl
= LLVMBuildTrunc(ctx
->builder
, tl
, ctx
->i16
, "");
2309 trbl
= LLVMBuildTrunc(ctx
->builder
, trbl
, ctx
->i16
, "");
2312 tl
= LLVMBuildBitCast(ctx
->builder
, tl
, result_type
, "");
2313 trbl
= LLVMBuildBitCast(ctx
->builder
, trbl
, result_type
, "");
2314 result
= LLVMBuildFSub(ctx
->builder
, trbl
, tl
, "");
2316 ac_build_type_name_for_intr(result_type
, type
, sizeof(type
));
2317 snprintf(name
, sizeof(name
), "llvm.amdgcn.wqm.%s", type
);
2319 return ac_build_intrinsic(ctx
, name
, result_type
, &result
, 1, 0);
2323 ac_build_sendmsg(struct ac_llvm_context
*ctx
,
2325 LLVMValueRef wave_id
)
2327 LLVMValueRef args
[2];
2328 args
[0] = LLVMConstInt(ctx
->i32
, msg
, false);
2330 ac_build_intrinsic(ctx
, "llvm.amdgcn.s.sendmsg", ctx
->voidt
, args
, 2, 0);
2334 ac_build_imsb(struct ac_llvm_context
*ctx
,
2336 LLVMTypeRef dst_type
)
2338 LLVMValueRef msb
= ac_build_intrinsic(ctx
, "llvm.amdgcn.sffbh.i32",
2340 AC_FUNC_ATTR_READNONE
);
2342 /* The HW returns the last bit index from MSB, but NIR/TGSI wants
2343 * the index from LSB. Invert it by doing "31 - msb". */
2344 msb
= LLVMBuildSub(ctx
->builder
, LLVMConstInt(ctx
->i32
, 31, false),
2347 LLVMValueRef all_ones
= LLVMConstInt(ctx
->i32
, -1, true);
2348 LLVMValueRef cond
= LLVMBuildOr(ctx
->builder
,
2349 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
2350 arg
, ctx
->i32_0
, ""),
2351 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
2352 arg
, all_ones
, ""), "");
2354 return LLVMBuildSelect(ctx
->builder
, cond
, all_ones
, msb
, "");
2358 ac_build_umsb(struct ac_llvm_context
*ctx
,
2360 LLVMTypeRef dst_type
)
2362 const char *intrin_name
;
2364 LLVMValueRef highest_bit
;
2368 bitsize
= ac_get_elem_bits(ctx
, LLVMTypeOf(arg
));
2371 intrin_name
= "llvm.ctlz.i64";
2373 highest_bit
= LLVMConstInt(ctx
->i64
, 63, false);
2377 intrin_name
= "llvm.ctlz.i32";
2379 highest_bit
= LLVMConstInt(ctx
->i32
, 31, false);
2383 intrin_name
= "llvm.ctlz.i16";
2385 highest_bit
= LLVMConstInt(ctx
->i16
, 15, false);
2389 intrin_name
= "llvm.ctlz.i8";
2391 highest_bit
= LLVMConstInt(ctx
->i8
, 7, false);
2395 unreachable(!"invalid bitsize");
2399 LLVMValueRef params
[2] = {
2404 LLVMValueRef msb
= ac_build_intrinsic(ctx
, intrin_name
, type
,
2406 AC_FUNC_ATTR_READNONE
);
2408 /* The HW returns the last bit index from MSB, but TGSI/NIR wants
2409 * the index from LSB. Invert it by doing "31 - msb". */
2410 msb
= LLVMBuildSub(ctx
->builder
, highest_bit
, msb
, "");
2412 if (bitsize
== 64) {
2413 msb
= LLVMBuildTrunc(ctx
->builder
, msb
, ctx
->i32
, "");
2414 } else if (bitsize
< 32) {
2415 msb
= LLVMBuildSExt(ctx
->builder
, msb
, ctx
->i32
, "");
2418 /* check for zero */
2419 return LLVMBuildSelect(ctx
->builder
,
2420 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, arg
, zero
, ""),
2421 LLVMConstInt(ctx
->i32
, -1, true), msb
, "");
2424 LLVMValueRef
ac_build_fmin(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
2428 snprintf(name
, sizeof(name
), "llvm.minnum.f%d", ac_get_elem_bits(ctx
, LLVMTypeOf(a
)));
2429 LLVMValueRef args
[2] = {a
, b
};
2430 return ac_build_intrinsic(ctx
, name
, LLVMTypeOf(a
), args
, 2,
2431 AC_FUNC_ATTR_READNONE
);
2434 LLVMValueRef
ac_build_fmax(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
2438 snprintf(name
, sizeof(name
), "llvm.maxnum.f%d", ac_get_elem_bits(ctx
, LLVMTypeOf(a
)));
2439 LLVMValueRef args
[2] = {a
, b
};
2440 return ac_build_intrinsic(ctx
, name
, LLVMTypeOf(a
), args
, 2,
2441 AC_FUNC_ATTR_READNONE
);
2444 LLVMValueRef
ac_build_imin(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
2447 LLVMValueRef cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSLE
, a
, b
, "");
2448 return LLVMBuildSelect(ctx
->builder
, cmp
, a
, b
, "");
2451 LLVMValueRef
ac_build_imax(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
2454 LLVMValueRef cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGT
, a
, b
, "");
2455 return LLVMBuildSelect(ctx
->builder
, cmp
, a
, b
, "");
2458 LLVMValueRef
ac_build_umin(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
2461 LLVMValueRef cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntULE
, a
, b
, "");
2462 return LLVMBuildSelect(ctx
->builder
, cmp
, a
, b
, "");
2465 LLVMValueRef
ac_build_umax(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
2468 LLVMValueRef cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntUGE
, a
, b
, "");
2469 return LLVMBuildSelect(ctx
->builder
, cmp
, a
, b
, "");
2472 LLVMValueRef
ac_build_clamp(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
2474 LLVMTypeRef t
= LLVMTypeOf(value
);
2475 return ac_build_fmin(ctx
, ac_build_fmax(ctx
, value
, LLVMConstReal(t
, 0.0)),
2476 LLVMConstReal(t
, 1.0));
2479 void ac_build_export(struct ac_llvm_context
*ctx
, struct ac_export_args
*a
)
2481 LLVMValueRef args
[9];
2483 args
[0] = LLVMConstInt(ctx
->i32
, a
->target
, 0);
2484 args
[1] = LLVMConstInt(ctx
->i32
, a
->enabled_channels
, 0);
2487 LLVMTypeRef i16
= LLVMInt16TypeInContext(ctx
->context
);
2488 LLVMTypeRef v2i16
= LLVMVectorType(i16
, 2);
2490 args
[2] = LLVMBuildBitCast(ctx
->builder
, a
->out
[0],
2492 args
[3] = LLVMBuildBitCast(ctx
->builder
, a
->out
[1],
2494 args
[4] = LLVMConstInt(ctx
->i1
, a
->done
, 0);
2495 args
[5] = LLVMConstInt(ctx
->i1
, a
->valid_mask
, 0);
2497 ac_build_intrinsic(ctx
, "llvm.amdgcn.exp.compr.v2i16",
2498 ctx
->voidt
, args
, 6, 0);
2500 args
[2] = a
->out
[0];
2501 args
[3] = a
->out
[1];
2502 args
[4] = a
->out
[2];
2503 args
[5] = a
->out
[3];
2504 args
[6] = LLVMConstInt(ctx
->i1
, a
->done
, 0);
2505 args
[7] = LLVMConstInt(ctx
->i1
, a
->valid_mask
, 0);
2507 ac_build_intrinsic(ctx
, "llvm.amdgcn.exp.f32",
2508 ctx
->voidt
, args
, 8, 0);
2512 void ac_build_export_null(struct ac_llvm_context
*ctx
)
2514 struct ac_export_args args
;
2516 args
.enabled_channels
= 0x0; /* enabled channels */
2517 args
.valid_mask
= 1; /* whether the EXEC mask is valid */
2518 args
.done
= 1; /* DONE bit */
2519 args
.target
= V_008DFC_SQ_EXP_NULL
;
2520 args
.compr
= 0; /* COMPR flag (0 = 32-bit export) */
2521 args
.out
[0] = LLVMGetUndef(ctx
->f32
); /* R */
2522 args
.out
[1] = LLVMGetUndef(ctx
->f32
); /* G */
2523 args
.out
[2] = LLVMGetUndef(ctx
->f32
); /* B */
2524 args
.out
[3] = LLVMGetUndef(ctx
->f32
); /* A */
2526 ac_build_export(ctx
, &args
);
2529 static unsigned ac_num_coords(enum ac_image_dim dim
)
2535 case ac_image_1darray
:
2539 case ac_image_2darray
:
2540 case ac_image_2dmsaa
:
2542 case ac_image_2darraymsaa
:
2545 unreachable("ac_num_coords: bad dim");
2549 static unsigned ac_num_derivs(enum ac_image_dim dim
)
2553 case ac_image_1darray
:
2556 case ac_image_2darray
:
2561 case ac_image_2dmsaa
:
2562 case ac_image_2darraymsaa
:
2564 unreachable("derivatives not supported");
2568 static const char *get_atomic_name(enum ac_atomic_op op
)
2571 case ac_atomic_swap
: return "swap";
2572 case ac_atomic_add
: return "add";
2573 case ac_atomic_sub
: return "sub";
2574 case ac_atomic_smin
: return "smin";
2575 case ac_atomic_umin
: return "umin";
2576 case ac_atomic_smax
: return "smax";
2577 case ac_atomic_umax
: return "umax";
2578 case ac_atomic_and
: return "and";
2579 case ac_atomic_or
: return "or";
2580 case ac_atomic_xor
: return "xor";
2582 unreachable("bad atomic op");
2585 LLVMValueRef
ac_build_image_opcode(struct ac_llvm_context
*ctx
,
2586 struct ac_image_args
*a
)
2588 const char *overload
[3] = { "", "", "" };
2589 unsigned num_overloads
= 0;
2590 LLVMValueRef args
[18];
2591 unsigned num_args
= 0;
2592 enum ac_image_dim dim
= a
->dim
;
2594 assert(!a
->lod
|| a
->lod
== ctx
->i32_0
|| a
->lod
== ctx
->f32_0
||
2596 assert((a
->opcode
!= ac_image_get_resinfo
&& a
->opcode
!= ac_image_load_mip
&&
2597 a
->opcode
!= ac_image_store_mip
) ||
2599 assert(a
->opcode
== ac_image_sample
|| a
->opcode
== ac_image_gather4
||
2600 (!a
->compare
&& !a
->offset
));
2601 assert((a
->opcode
== ac_image_sample
|| a
->opcode
== ac_image_gather4
||
2602 a
->opcode
== ac_image_get_lod
) ||
2604 assert((a
->bias
? 1 : 0) +
2606 (a
->level_zero
? 1 : 0) +
2607 (a
->derivs
[0] ? 1 : 0) <= 1);
2609 if (a
->opcode
== ac_image_get_lod
) {
2611 case ac_image_1darray
:
2614 case ac_image_2darray
:
2623 bool sample
= a
->opcode
== ac_image_sample
||
2624 a
->opcode
== ac_image_gather4
||
2625 a
->opcode
== ac_image_get_lod
;
2626 bool atomic
= a
->opcode
== ac_image_atomic
||
2627 a
->opcode
== ac_image_atomic_cmpswap
;
2628 bool load
= a
->opcode
== ac_image_sample
||
2629 a
->opcode
== ac_image_gather4
||
2630 a
->opcode
== ac_image_load
||
2631 a
->opcode
== ac_image_load_mip
;
2632 LLVMTypeRef coord_type
= sample
? ctx
->f32
: ctx
->i32
;
2634 if (atomic
|| a
->opcode
== ac_image_store
|| a
->opcode
== ac_image_store_mip
) {
2635 args
[num_args
++] = a
->data
[0];
2636 if (a
->opcode
== ac_image_atomic_cmpswap
)
2637 args
[num_args
++] = a
->data
[1];
2641 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->dmask
, false);
2644 args
[num_args
++] = ac_to_integer(ctx
, a
->offset
);
2646 args
[num_args
++] = ac_to_float(ctx
, a
->bias
);
2647 overload
[num_overloads
++] = ".f32";
2650 args
[num_args
++] = ac_to_float(ctx
, a
->compare
);
2652 unsigned count
= ac_num_derivs(dim
);
2653 for (unsigned i
= 0; i
< count
; ++i
)
2654 args
[num_args
++] = ac_to_float(ctx
, a
->derivs
[i
]);
2655 overload
[num_overloads
++] = ".f32";
2657 unsigned num_coords
=
2658 a
->opcode
!= ac_image_get_resinfo
? ac_num_coords(dim
) : 0;
2659 for (unsigned i
= 0; i
< num_coords
; ++i
)
2660 args
[num_args
++] = LLVMBuildBitCast(ctx
->builder
, a
->coords
[i
], coord_type
, "");
2662 args
[num_args
++] = LLVMBuildBitCast(ctx
->builder
, a
->lod
, coord_type
, "");
2663 overload
[num_overloads
++] = sample
? ".f32" : ".i32";
2665 args
[num_args
++] = a
->resource
;
2667 args
[num_args
++] = a
->sampler
;
2668 args
[num_args
++] = LLVMConstInt(ctx
->i1
, a
->unorm
, false);
2671 args
[num_args
++] = ctx
->i32_0
; /* texfailctrl */
2672 args
[num_args
++] = LLVMConstInt(ctx
->i32
,
2673 load
? get_load_cache_policy(ctx
, a
->cache_policy
) :
2674 a
->cache_policy
, false);
2677 const char *atomic_subop
= "";
2678 switch (a
->opcode
) {
2679 case ac_image_sample
: name
= "sample"; break;
2680 case ac_image_gather4
: name
= "gather4"; break;
2681 case ac_image_load
: name
= "load"; break;
2682 case ac_image_load_mip
: name
= "load.mip"; break;
2683 case ac_image_store
: name
= "store"; break;
2684 case ac_image_store_mip
: name
= "store.mip"; break;
2685 case ac_image_atomic
:
2687 atomic_subop
= get_atomic_name(a
->atomic
);
2689 case ac_image_atomic_cmpswap
:
2691 atomic_subop
= "cmpswap";
2693 case ac_image_get_lod
: name
= "getlod"; break;
2694 case ac_image_get_resinfo
: name
= "getresinfo"; break;
2695 default: unreachable("invalid image opcode");
2698 const char *dimname
;
2700 case ac_image_1d
: dimname
= "1d"; break;
2701 case ac_image_2d
: dimname
= "2d"; break;
2702 case ac_image_3d
: dimname
= "3d"; break;
2703 case ac_image_cube
: dimname
= "cube"; break;
2704 case ac_image_1darray
: dimname
= "1darray"; break;
2705 case ac_image_2darray
: dimname
= "2darray"; break;
2706 case ac_image_2dmsaa
: dimname
= "2dmsaa"; break;
2707 case ac_image_2darraymsaa
: dimname
= "2darraymsaa"; break;
2708 default: unreachable("invalid dim");
2712 a
->lod
&& (a
->opcode
== ac_image_sample
|| a
->opcode
== ac_image_gather4
);
2714 snprintf(intr_name
, sizeof(intr_name
),
2715 "llvm.amdgcn.image.%s%s" /* base name */
2716 "%s%s%s" /* sample/gather modifiers */
2717 ".%s.%s%s%s%s", /* dimension and type overloads */
2719 a
->compare
? ".c" : "",
2722 a
->derivs
[0] ? ".d" :
2723 a
->level_zero
? ".lz" : "",
2724 a
->offset
? ".o" : "",
2726 atomic
? "i32" : "v4f32",
2727 overload
[0], overload
[1], overload
[2]);
2732 else if (a
->opcode
== ac_image_store
|| a
->opcode
== ac_image_store_mip
)
2737 LLVMValueRef result
=
2738 ac_build_intrinsic(ctx
, intr_name
, retty
, args
, num_args
,
2740 if (!sample
&& retty
== ctx
->v4f32
) {
2741 result
= LLVMBuildBitCast(ctx
->builder
, result
,
2747 LLVMValueRef
ac_build_cvt_pkrtz_f16(struct ac_llvm_context
*ctx
,
2748 LLVMValueRef args
[2])
2751 LLVMVectorType(LLVMHalfTypeInContext(ctx
->context
), 2);
2753 return ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pkrtz", v2f16
,
2754 args
, 2, AC_FUNC_ATTR_READNONE
);
2757 LLVMValueRef
ac_build_cvt_pknorm_i16(struct ac_llvm_context
*ctx
,
2758 LLVMValueRef args
[2])
2761 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pknorm.i16",
2762 ctx
->v2i16
, args
, 2,
2763 AC_FUNC_ATTR_READNONE
);
2764 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
2767 LLVMValueRef
ac_build_cvt_pknorm_u16(struct ac_llvm_context
*ctx
,
2768 LLVMValueRef args
[2])
2771 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pknorm.u16",
2772 ctx
->v2i16
, args
, 2,
2773 AC_FUNC_ATTR_READNONE
);
2774 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
2777 /* The 8-bit and 10-bit clamping is for HW workarounds. */
2778 LLVMValueRef
ac_build_cvt_pk_i16(struct ac_llvm_context
*ctx
,
2779 LLVMValueRef args
[2], unsigned bits
, bool hi
)
2781 assert(bits
== 8 || bits
== 10 || bits
== 16);
2783 LLVMValueRef max_rgb
= LLVMConstInt(ctx
->i32
,
2784 bits
== 8 ? 127 : bits
== 10 ? 511 : 32767, 0);
2785 LLVMValueRef min_rgb
= LLVMConstInt(ctx
->i32
,
2786 bits
== 8 ? -128 : bits
== 10 ? -512 : -32768, 0);
2787 LLVMValueRef max_alpha
=
2788 bits
!= 10 ? max_rgb
: ctx
->i32_1
;
2789 LLVMValueRef min_alpha
=
2790 bits
!= 10 ? min_rgb
: LLVMConstInt(ctx
->i32
, -2, 0);
2794 for (int i
= 0; i
< 2; i
++) {
2795 bool alpha
= hi
&& i
== 1;
2796 args
[i
] = ac_build_imin(ctx
, args
[i
],
2797 alpha
? max_alpha
: max_rgb
);
2798 args
[i
] = ac_build_imax(ctx
, args
[i
],
2799 alpha
? min_alpha
: min_rgb
);
2804 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pk.i16",
2805 ctx
->v2i16
, args
, 2,
2806 AC_FUNC_ATTR_READNONE
);
2807 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
2810 /* The 8-bit and 10-bit clamping is for HW workarounds. */
2811 LLVMValueRef
ac_build_cvt_pk_u16(struct ac_llvm_context
*ctx
,
2812 LLVMValueRef args
[2], unsigned bits
, bool hi
)
2814 assert(bits
== 8 || bits
== 10 || bits
== 16);
2816 LLVMValueRef max_rgb
= LLVMConstInt(ctx
->i32
,
2817 bits
== 8 ? 255 : bits
== 10 ? 1023 : 65535, 0);
2818 LLVMValueRef max_alpha
=
2819 bits
!= 10 ? max_rgb
: LLVMConstInt(ctx
->i32
, 3, 0);
2823 for (int i
= 0; i
< 2; i
++) {
2824 bool alpha
= hi
&& i
== 1;
2825 args
[i
] = ac_build_umin(ctx
, args
[i
],
2826 alpha
? max_alpha
: max_rgb
);
2831 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pk.u16",
2832 ctx
->v2i16
, args
, 2,
2833 AC_FUNC_ATTR_READNONE
);
2834 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
2837 LLVMValueRef
ac_build_wqm_vote(struct ac_llvm_context
*ctx
, LLVMValueRef i1
)
2839 return ac_build_intrinsic(ctx
, "llvm.amdgcn.wqm.vote", ctx
->i1
,
2840 &i1
, 1, AC_FUNC_ATTR_READNONE
);
2843 void ac_build_kill_if_false(struct ac_llvm_context
*ctx
, LLVMValueRef i1
)
2845 ac_build_intrinsic(ctx
, "llvm.amdgcn.kill", ctx
->voidt
,
2849 LLVMValueRef
ac_build_bfe(struct ac_llvm_context
*ctx
, LLVMValueRef input
,
2850 LLVMValueRef offset
, LLVMValueRef width
,
2853 LLVMValueRef args
[] = {
2859 LLVMValueRef result
= ac_build_intrinsic(ctx
,
2860 is_signed
? "llvm.amdgcn.sbfe.i32" :
2861 "llvm.amdgcn.ubfe.i32",
2863 AC_FUNC_ATTR_READNONE
);
2865 if (HAVE_LLVM
< 0x0800) {
2866 /* FIXME: LLVM 7+ returns incorrect result when count is 0.
2867 * https://bugs.freedesktop.org/show_bug.cgi?id=107276
2869 LLVMValueRef zero
= ctx
->i32_0
;
2870 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, width
, zero
, "");
2871 result
= LLVMBuildSelect(ctx
->builder
, icond
, zero
, result
, "");
2877 LLVMValueRef
ac_build_imad(struct ac_llvm_context
*ctx
, LLVMValueRef s0
,
2878 LLVMValueRef s1
, LLVMValueRef s2
)
2880 return LLVMBuildAdd(ctx
->builder
,
2881 LLVMBuildMul(ctx
->builder
, s0
, s1
, ""), s2
, "");
2884 LLVMValueRef
ac_build_fmad(struct ac_llvm_context
*ctx
, LLVMValueRef s0
,
2885 LLVMValueRef s1
, LLVMValueRef s2
)
2887 return LLVMBuildFAdd(ctx
->builder
,
2888 LLVMBuildFMul(ctx
->builder
, s0
, s1
, ""), s2
, "");
2891 void ac_build_waitcnt(struct ac_llvm_context
*ctx
, unsigned wait_flags
)
2896 unsigned lgkmcnt
= 63;
2897 unsigned vmcnt
= ctx
->chip_class
>= GFX9
? 63 : 15;
2898 unsigned vscnt
= 63;
2900 if (wait_flags
& AC_WAIT_LGKM
)
2902 if (wait_flags
& AC_WAIT_VLOAD
)
2905 if (wait_flags
& AC_WAIT_VSTORE
) {
2906 if (ctx
->chip_class
>= GFX10
)
2912 /* There is no intrinsic for vscnt(0), so use a fence. */
2913 if ((wait_flags
& AC_WAIT_LGKM
&&
2914 wait_flags
& AC_WAIT_VLOAD
&&
2915 wait_flags
& AC_WAIT_VSTORE
) ||
2917 LLVMBuildFence(ctx
->builder
, LLVMAtomicOrderingRelease
, false, "");
2921 unsigned simm16
= (lgkmcnt
<< 8) |
2922 (7 << 4) | /* expcnt */
2924 ((vmcnt
>> 4) << 14);
2926 LLVMValueRef args
[1] = {
2927 LLVMConstInt(ctx
->i32
, simm16
, false),
2929 ac_build_intrinsic(ctx
, "llvm.amdgcn.s.waitcnt",
2930 ctx
->voidt
, args
, 1, 0);
2933 LLVMValueRef
ac_build_fmed3(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
2934 LLVMValueRef src1
, LLVMValueRef src2
,
2940 if (bitsize
== 16) {
2941 intr
= "llvm.amdgcn.fmed3.f16";
2943 } else if (bitsize
== 32) {
2944 intr
= "llvm.amdgcn.fmed3.f32";
2947 intr
= "llvm.amdgcn.fmed3.f64";
2951 LLVMValueRef params
[] = {
2956 return ac_build_intrinsic(ctx
, intr
, type
, params
, 3,
2957 AC_FUNC_ATTR_READNONE
);
2960 LLVMValueRef
ac_build_fract(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
2966 if (bitsize
== 16) {
2967 intr
= "llvm.amdgcn.fract.f16";
2969 } else if (bitsize
== 32) {
2970 intr
= "llvm.amdgcn.fract.f32";
2973 intr
= "llvm.amdgcn.fract.f64";
2977 LLVMValueRef params
[] = {
2980 return ac_build_intrinsic(ctx
, intr
, type
, params
, 1,
2981 AC_FUNC_ATTR_READNONE
);
2984 LLVMValueRef
ac_build_isign(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
2987 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->context
, bitsize
);
2988 LLVMValueRef zero
= LLVMConstInt(type
, 0, false);
2989 LLVMValueRef one
= LLVMConstInt(type
, 1, false);
2991 LLVMValueRef cmp
, val
;
2992 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGT
, src0
, zero
, "");
2993 val
= LLVMBuildSelect(ctx
->builder
, cmp
, one
, src0
, "");
2994 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGE
, val
, zero
, "");
2995 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstInt(type
, -1, true), "");
2999 LLVMValueRef
ac_build_fsign(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
3002 LLVMValueRef cmp
, val
, zero
, one
;
3005 if (bitsize
== 16) {
3009 } else if (bitsize
== 32) {
3019 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGT
, src0
, zero
, "");
3020 val
= LLVMBuildSelect(ctx
->builder
, cmp
, one
, src0
, "");
3021 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGE
, val
, zero
, "");
3022 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstReal(type
, -1.0), "");
3026 LLVMValueRef
ac_build_bit_count(struct ac_llvm_context
*ctx
, LLVMValueRef src0
)
3028 LLVMValueRef result
;
3031 bitsize
= ac_get_elem_bits(ctx
, LLVMTypeOf(src0
));
3035 result
= ac_build_intrinsic(ctx
, "llvm.ctpop.i64", ctx
->i64
,
3036 (LLVMValueRef
[]) { src0
}, 1,
3037 AC_FUNC_ATTR_READNONE
);
3039 result
= LLVMBuildTrunc(ctx
->builder
, result
, ctx
->i32
, "");
3042 result
= ac_build_intrinsic(ctx
, "llvm.ctpop.i32", ctx
->i32
,
3043 (LLVMValueRef
[]) { src0
}, 1,
3044 AC_FUNC_ATTR_READNONE
);
3047 result
= ac_build_intrinsic(ctx
, "llvm.ctpop.i16", ctx
->i16
,
3048 (LLVMValueRef
[]) { src0
}, 1,
3049 AC_FUNC_ATTR_READNONE
);
3051 result
= LLVMBuildZExt(ctx
->builder
, result
, ctx
->i32
, "");
3054 result
= ac_build_intrinsic(ctx
, "llvm.ctpop.i8", ctx
->i8
,
3055 (LLVMValueRef
[]) { src0
}, 1,
3056 AC_FUNC_ATTR_READNONE
);
3058 result
= LLVMBuildZExt(ctx
->builder
, result
, ctx
->i32
, "");
3061 unreachable(!"invalid bitsize");
3068 LLVMValueRef
ac_build_bitfield_reverse(struct ac_llvm_context
*ctx
,
3071 LLVMValueRef result
;
3074 bitsize
= ac_get_elem_bits(ctx
, LLVMTypeOf(src0
));
3078 result
= ac_build_intrinsic(ctx
, "llvm.bitreverse.i64", ctx
->i64
,
3079 (LLVMValueRef
[]) { src0
}, 1,
3080 AC_FUNC_ATTR_READNONE
);
3082 result
= LLVMBuildTrunc(ctx
->builder
, result
, ctx
->i32
, "");
3085 result
= ac_build_intrinsic(ctx
, "llvm.bitreverse.i32", ctx
->i32
,
3086 (LLVMValueRef
[]) { src0
}, 1,
3087 AC_FUNC_ATTR_READNONE
);
3090 result
= ac_build_intrinsic(ctx
, "llvm.bitreverse.i16", ctx
->i16
,
3091 (LLVMValueRef
[]) { src0
}, 1,
3092 AC_FUNC_ATTR_READNONE
);
3094 result
= LLVMBuildZExt(ctx
->builder
, result
, ctx
->i32
, "");
3097 result
= ac_build_intrinsic(ctx
, "llvm.bitreverse.i8", ctx
->i8
,
3098 (LLVMValueRef
[]) { src0
}, 1,
3099 AC_FUNC_ATTR_READNONE
);
3101 result
= LLVMBuildZExt(ctx
->builder
, result
, ctx
->i32
, "");
3104 unreachable(!"invalid bitsize");
3111 #define AC_EXP_TARGET 0
3112 #define AC_EXP_ENABLED_CHANNELS 1
3113 #define AC_EXP_OUT0 2
3121 struct ac_vs_exp_chan
3125 enum ac_ir_type type
;
3128 struct ac_vs_exp_inst
{
3131 struct ac_vs_exp_chan chan
[4];
3134 struct ac_vs_exports
{
3136 struct ac_vs_exp_inst exp
[VARYING_SLOT_MAX
];
3139 /* Return true if the PARAM export has been eliminated. */
3140 static bool ac_eliminate_const_output(uint8_t *vs_output_param_offset
,
3141 uint32_t num_outputs
,
3142 struct ac_vs_exp_inst
*exp
)
3144 unsigned i
, default_val
; /* SPI_PS_INPUT_CNTL_i.DEFAULT_VAL */
3145 bool is_zero
[4] = {}, is_one
[4] = {};
3147 for (i
= 0; i
< 4; i
++) {
3148 /* It's a constant expression. Undef outputs are eliminated too. */
3149 if (exp
->chan
[i
].type
== AC_IR_UNDEF
) {
3152 } else if (exp
->chan
[i
].type
== AC_IR_CONST
) {
3153 if (exp
->chan
[i
].const_float
== 0)
3155 else if (exp
->chan
[i
].const_float
== 1)
3158 return false; /* other constant */
3163 /* Only certain combinations of 0 and 1 can be eliminated. */
3164 if (is_zero
[0] && is_zero
[1] && is_zero
[2])
3165 default_val
= is_zero
[3] ? 0 : 1;
3166 else if (is_one
[0] && is_one
[1] && is_one
[2])
3167 default_val
= is_zero
[3] ? 2 : 3;
3171 /* The PARAM export can be represented as DEFAULT_VAL. Kill it. */
3172 LLVMInstructionEraseFromParent(exp
->inst
);
3174 /* Change OFFSET to DEFAULT_VAL. */
3175 for (i
= 0; i
< num_outputs
; i
++) {
3176 if (vs_output_param_offset
[i
] == exp
->offset
) {
3177 vs_output_param_offset
[i
] =
3178 AC_EXP_PARAM_DEFAULT_VAL_0000
+ default_val
;
3185 static bool ac_eliminate_duplicated_output(struct ac_llvm_context
*ctx
,
3186 uint8_t *vs_output_param_offset
,
3187 uint32_t num_outputs
,
3188 struct ac_vs_exports
*processed
,
3189 struct ac_vs_exp_inst
*exp
)
3191 unsigned p
, copy_back_channels
= 0;
3193 /* See if the output is already in the list of processed outputs.
3194 * The LLVMValueRef comparison relies on SSA.
3196 for (p
= 0; p
< processed
->num
; p
++) {
3197 bool different
= false;
3199 for (unsigned j
= 0; j
< 4; j
++) {
3200 struct ac_vs_exp_chan
*c1
= &processed
->exp
[p
].chan
[j
];
3201 struct ac_vs_exp_chan
*c2
= &exp
->chan
[j
];
3203 /* Treat undef as a match. */
3204 if (c2
->type
== AC_IR_UNDEF
)
3207 /* If c1 is undef but c2 isn't, we can copy c2 to c1
3208 * and consider the instruction duplicated.
3210 if (c1
->type
== AC_IR_UNDEF
) {
3211 copy_back_channels
|= 1 << j
;
3215 /* Test whether the channels are not equal. */
3216 if (c1
->type
!= c2
->type
||
3217 (c1
->type
== AC_IR_CONST
&&
3218 c1
->const_float
!= c2
->const_float
) ||
3219 (c1
->type
== AC_IR_VALUE
&&
3220 c1
->value
!= c2
->value
)) {
3228 copy_back_channels
= 0;
3230 if (p
== processed
->num
)
3233 /* If a match was found, but the matching export has undef where the new
3234 * one has a normal value, copy the normal value to the undef channel.
3236 struct ac_vs_exp_inst
*match
= &processed
->exp
[p
];
3238 /* Get current enabled channels mask. */
3239 LLVMValueRef arg
= LLVMGetOperand(match
->inst
, AC_EXP_ENABLED_CHANNELS
);
3240 unsigned enabled_channels
= LLVMConstIntGetZExtValue(arg
);
3242 while (copy_back_channels
) {
3243 unsigned chan
= u_bit_scan(©_back_channels
);
3245 assert(match
->chan
[chan
].type
== AC_IR_UNDEF
);
3246 LLVMSetOperand(match
->inst
, AC_EXP_OUT0
+ chan
,
3247 exp
->chan
[chan
].value
);
3248 match
->chan
[chan
] = exp
->chan
[chan
];
3250 /* Update number of enabled channels because the original mask
3251 * is not always 0xf.
3253 enabled_channels
|= (1 << chan
);
3254 LLVMSetOperand(match
->inst
, AC_EXP_ENABLED_CHANNELS
,
3255 LLVMConstInt(ctx
->i32
, enabled_channels
, 0));
3258 /* The PARAM export is duplicated. Kill it. */
3259 LLVMInstructionEraseFromParent(exp
->inst
);
3261 /* Change OFFSET to the matching export. */
3262 for (unsigned i
= 0; i
< num_outputs
; i
++) {
3263 if (vs_output_param_offset
[i
] == exp
->offset
) {
3264 vs_output_param_offset
[i
] = match
->offset
;
3271 void ac_optimize_vs_outputs(struct ac_llvm_context
*ctx
,
3272 LLVMValueRef main_fn
,
3273 uint8_t *vs_output_param_offset
,
3274 uint32_t num_outputs
,
3275 uint8_t *num_param_exports
)
3277 LLVMBasicBlockRef bb
;
3278 bool removed_any
= false;
3279 struct ac_vs_exports exports
;
3283 /* Process all LLVM instructions. */
3284 bb
= LLVMGetFirstBasicBlock(main_fn
);
3286 LLVMValueRef inst
= LLVMGetFirstInstruction(bb
);
3289 LLVMValueRef cur
= inst
;
3290 inst
= LLVMGetNextInstruction(inst
);
3291 struct ac_vs_exp_inst exp
;
3293 if (LLVMGetInstructionOpcode(cur
) != LLVMCall
)
3296 LLVMValueRef callee
= ac_llvm_get_called_value(cur
);
3298 if (!ac_llvm_is_function(callee
))
3301 const char *name
= LLVMGetValueName(callee
);
3302 unsigned num_args
= LLVMCountParams(callee
);
3304 /* Check if this is an export instruction. */
3305 if ((num_args
!= 9 && num_args
!= 8) ||
3306 (strcmp(name
, "llvm.SI.export") &&
3307 strcmp(name
, "llvm.amdgcn.exp.f32")))
3310 LLVMValueRef arg
= LLVMGetOperand(cur
, AC_EXP_TARGET
);
3311 unsigned target
= LLVMConstIntGetZExtValue(arg
);
3313 if (target
< V_008DFC_SQ_EXP_PARAM
)
3316 target
-= V_008DFC_SQ_EXP_PARAM
;
3318 /* Parse the instruction. */
3319 memset(&exp
, 0, sizeof(exp
));
3320 exp
.offset
= target
;
3323 for (unsigned i
= 0; i
< 4; i
++) {
3324 LLVMValueRef v
= LLVMGetOperand(cur
, AC_EXP_OUT0
+ i
);
3326 exp
.chan
[i
].value
= v
;
3328 if (LLVMIsUndef(v
)) {
3329 exp
.chan
[i
].type
= AC_IR_UNDEF
;
3330 } else if (LLVMIsAConstantFP(v
)) {
3331 LLVMBool loses_info
;
3332 exp
.chan
[i
].type
= AC_IR_CONST
;
3333 exp
.chan
[i
].const_float
=
3334 LLVMConstRealGetDouble(v
, &loses_info
);
3336 exp
.chan
[i
].type
= AC_IR_VALUE
;
3340 /* Eliminate constant and duplicated PARAM exports. */
3341 if (ac_eliminate_const_output(vs_output_param_offset
,
3342 num_outputs
, &exp
) ||
3343 ac_eliminate_duplicated_output(ctx
,
3344 vs_output_param_offset
,
3345 num_outputs
, &exports
,
3349 exports
.exp
[exports
.num
++] = exp
;
3352 bb
= LLVMGetNextBasicBlock(bb
);
3355 /* Remove holes in export memory due to removed PARAM exports.
3356 * This is done by renumbering all PARAM exports.
3359 uint8_t old_offset
[VARYING_SLOT_MAX
];
3362 /* Make a copy of the offsets. We need the old version while
3363 * we are modifying some of them. */
3364 memcpy(old_offset
, vs_output_param_offset
,
3365 sizeof(old_offset
));
3367 for (i
= 0; i
< exports
.num
; i
++) {
3368 unsigned offset
= exports
.exp
[i
].offset
;
3370 /* Update vs_output_param_offset. Multiple outputs can
3371 * have the same offset.
3373 for (out
= 0; out
< num_outputs
; out
++) {
3374 if (old_offset
[out
] == offset
)
3375 vs_output_param_offset
[out
] = i
;
3378 /* Change the PARAM offset in the instruction. */
3379 LLVMSetOperand(exports
.exp
[i
].inst
, AC_EXP_TARGET
,
3380 LLVMConstInt(ctx
->i32
,
3381 V_008DFC_SQ_EXP_PARAM
+ i
, 0));
3383 *num_param_exports
= exports
.num
;
3387 void ac_init_exec_full_mask(struct ac_llvm_context
*ctx
)
3389 LLVMValueRef full_mask
= LLVMConstInt(ctx
->i64
, ~0ull, 0);
3390 ac_build_intrinsic(ctx
,
3391 "llvm.amdgcn.init.exec", ctx
->voidt
,
3392 &full_mask
, 1, AC_FUNC_ATTR_CONVERGENT
);
3395 void ac_declare_lds_as_pointer(struct ac_llvm_context
*ctx
)
3397 unsigned lds_size
= ctx
->chip_class
>= GFX7
? 65536 : 32768;
3398 ctx
->lds
= LLVMBuildIntToPtr(ctx
->builder
, ctx
->i32_0
,
3399 LLVMPointerType(LLVMArrayType(ctx
->i32
, lds_size
/ 4), AC_ADDR_SPACE_LDS
),
3403 LLVMValueRef
ac_lds_load(struct ac_llvm_context
*ctx
,
3404 LLVMValueRef dw_addr
)
3406 return LLVMBuildLoad(ctx
->builder
, ac_build_gep0(ctx
, ctx
->lds
, dw_addr
), "");
3409 void ac_lds_store(struct ac_llvm_context
*ctx
,
3410 LLVMValueRef dw_addr
,
3413 value
= ac_to_integer(ctx
, value
);
3414 ac_build_indexed_store(ctx
, ctx
->lds
,
3418 LLVMValueRef
ac_find_lsb(struct ac_llvm_context
*ctx
,
3419 LLVMTypeRef dst_type
,
3422 unsigned src0_bitsize
= ac_get_elem_bits(ctx
, LLVMTypeOf(src0
));
3423 const char *intrin_name
;
3427 switch (src0_bitsize
) {
3429 intrin_name
= "llvm.cttz.i64";
3434 intrin_name
= "llvm.cttz.i32";
3439 intrin_name
= "llvm.cttz.i16";
3444 intrin_name
= "llvm.cttz.i8";
3449 unreachable(!"invalid bitsize");
3452 LLVMValueRef params
[2] = {
3455 /* The value of 1 means that ffs(x=0) = undef, so LLVM won't
3456 * add special code to check for x=0. The reason is that
3457 * the LLVM behavior for x=0 is different from what we
3458 * need here. However, LLVM also assumes that ffs(x) is
3459 * in [0, 31], but GLSL expects that ffs(0) = -1, so
3460 * a conditional assignment to handle 0 is still required.
3462 * The hardware already implements the correct behavior.
3467 LLVMValueRef lsb
= ac_build_intrinsic(ctx
, intrin_name
, type
,
3469 AC_FUNC_ATTR_READNONE
);
3471 if (src0_bitsize
== 64) {
3472 lsb
= LLVMBuildTrunc(ctx
->builder
, lsb
, ctx
->i32
, "");
3473 } else if (src0_bitsize
< 32) {
3474 lsb
= LLVMBuildSExt(ctx
->builder
, lsb
, ctx
->i32
, "");
3477 /* TODO: We need an intrinsic to skip this conditional. */
3478 /* Check for zero: */
3479 return LLVMBuildSelect(ctx
->builder
, LLVMBuildICmp(ctx
->builder
,
3482 LLVMConstInt(ctx
->i32
, -1, 0), lsb
, "");
3485 LLVMTypeRef
ac_array_in_const_addr_space(LLVMTypeRef elem_type
)
3487 return LLVMPointerType(elem_type
, AC_ADDR_SPACE_CONST
);
3490 LLVMTypeRef
ac_array_in_const32_addr_space(LLVMTypeRef elem_type
)
3492 return LLVMPointerType(elem_type
, AC_ADDR_SPACE_CONST_32BIT
);
3495 static struct ac_llvm_flow
*
3496 get_current_flow(struct ac_llvm_context
*ctx
)
3498 if (ctx
->flow_depth
> 0)
3499 return &ctx
->flow
[ctx
->flow_depth
- 1];
3503 static struct ac_llvm_flow
*
3504 get_innermost_loop(struct ac_llvm_context
*ctx
)
3506 for (unsigned i
= ctx
->flow_depth
; i
> 0; --i
) {
3507 if (ctx
->flow
[i
- 1].loop_entry_block
)
3508 return &ctx
->flow
[i
- 1];
3513 static struct ac_llvm_flow
*
3514 push_flow(struct ac_llvm_context
*ctx
)
3516 struct ac_llvm_flow
*flow
;
3518 if (ctx
->flow_depth
>= ctx
->flow_depth_max
) {
3519 unsigned new_max
= MAX2(ctx
->flow_depth
<< 1,
3520 AC_LLVM_INITIAL_CF_DEPTH
);
3522 ctx
->flow
= realloc(ctx
->flow
, new_max
* sizeof(*ctx
->flow
));
3523 ctx
->flow_depth_max
= new_max
;
3526 flow
= &ctx
->flow
[ctx
->flow_depth
];
3529 flow
->next_block
= NULL
;
3530 flow
->loop_entry_block
= NULL
;
3534 static void set_basicblock_name(LLVMBasicBlockRef bb
, const char *base
,
3538 snprintf(buf
, sizeof(buf
), "%s%d", base
, label_id
);
3539 LLVMSetValueName(LLVMBasicBlockAsValue(bb
), buf
);
3542 /* Append a basic block at the level of the parent flow.
3544 static LLVMBasicBlockRef
append_basic_block(struct ac_llvm_context
*ctx
,
3547 assert(ctx
->flow_depth
>= 1);
3549 if (ctx
->flow_depth
>= 2) {
3550 struct ac_llvm_flow
*flow
= &ctx
->flow
[ctx
->flow_depth
- 2];
3552 return LLVMInsertBasicBlockInContext(ctx
->context
,
3553 flow
->next_block
, name
);
3556 LLVMValueRef main_fn
=
3557 LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
->builder
));
3558 return LLVMAppendBasicBlockInContext(ctx
->context
, main_fn
, name
);
3561 /* Emit a branch to the given default target for the current block if
3562 * applicable -- that is, if the current block does not already contain a
3563 * branch from a break or continue.
3565 static void emit_default_branch(LLVMBuilderRef builder
,
3566 LLVMBasicBlockRef target
)
3568 if (!LLVMGetBasicBlockTerminator(LLVMGetInsertBlock(builder
)))
3569 LLVMBuildBr(builder
, target
);
3572 void ac_build_bgnloop(struct ac_llvm_context
*ctx
, int label_id
)
3574 struct ac_llvm_flow
*flow
= push_flow(ctx
);
3575 flow
->loop_entry_block
= append_basic_block(ctx
, "LOOP");
3576 flow
->next_block
= append_basic_block(ctx
, "ENDLOOP");
3577 set_basicblock_name(flow
->loop_entry_block
, "loop", label_id
);
3578 LLVMBuildBr(ctx
->builder
, flow
->loop_entry_block
);
3579 LLVMPositionBuilderAtEnd(ctx
->builder
, flow
->loop_entry_block
);
3582 void ac_build_break(struct ac_llvm_context
*ctx
)
3584 struct ac_llvm_flow
*flow
= get_innermost_loop(ctx
);
3585 LLVMBuildBr(ctx
->builder
, flow
->next_block
);
3588 void ac_build_continue(struct ac_llvm_context
*ctx
)
3590 struct ac_llvm_flow
*flow
= get_innermost_loop(ctx
);
3591 LLVMBuildBr(ctx
->builder
, flow
->loop_entry_block
);
3594 void ac_build_else(struct ac_llvm_context
*ctx
, int label_id
)
3596 struct ac_llvm_flow
*current_branch
= get_current_flow(ctx
);
3597 LLVMBasicBlockRef endif_block
;
3599 assert(!current_branch
->loop_entry_block
);
3601 endif_block
= append_basic_block(ctx
, "ENDIF");
3602 emit_default_branch(ctx
->builder
, endif_block
);
3604 LLVMPositionBuilderAtEnd(ctx
->builder
, current_branch
->next_block
);
3605 set_basicblock_name(current_branch
->next_block
, "else", label_id
);
3607 current_branch
->next_block
= endif_block
;
3610 void ac_build_endif(struct ac_llvm_context
*ctx
, int label_id
)
3612 struct ac_llvm_flow
*current_branch
= get_current_flow(ctx
);
3614 assert(!current_branch
->loop_entry_block
);
3616 emit_default_branch(ctx
->builder
, current_branch
->next_block
);
3617 LLVMPositionBuilderAtEnd(ctx
->builder
, current_branch
->next_block
);
3618 set_basicblock_name(current_branch
->next_block
, "endif", label_id
);
3623 void ac_build_endloop(struct ac_llvm_context
*ctx
, int label_id
)
3625 struct ac_llvm_flow
*current_loop
= get_current_flow(ctx
);
3627 assert(current_loop
->loop_entry_block
);
3629 emit_default_branch(ctx
->builder
, current_loop
->loop_entry_block
);
3631 LLVMPositionBuilderAtEnd(ctx
->builder
, current_loop
->next_block
);
3632 set_basicblock_name(current_loop
->next_block
, "endloop", label_id
);
3636 void ac_build_ifcc(struct ac_llvm_context
*ctx
, LLVMValueRef cond
, int label_id
)
3638 struct ac_llvm_flow
*flow
= push_flow(ctx
);
3639 LLVMBasicBlockRef if_block
;
3641 if_block
= append_basic_block(ctx
, "IF");
3642 flow
->next_block
= append_basic_block(ctx
, "ELSE");
3643 set_basicblock_name(if_block
, "if", label_id
);
3644 LLVMBuildCondBr(ctx
->builder
, cond
, if_block
, flow
->next_block
);
3645 LLVMPositionBuilderAtEnd(ctx
->builder
, if_block
);
3648 void ac_build_if(struct ac_llvm_context
*ctx
, LLVMValueRef value
,
3651 LLVMValueRef cond
= LLVMBuildFCmp(ctx
->builder
, LLVMRealUNE
,
3652 value
, ctx
->f32_0
, "");
3653 ac_build_ifcc(ctx
, cond
, label_id
);
3656 void ac_build_uif(struct ac_llvm_context
*ctx
, LLVMValueRef value
,
3659 LLVMValueRef cond
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
3660 ac_to_integer(ctx
, value
),
3662 ac_build_ifcc(ctx
, cond
, label_id
);
3665 LLVMValueRef
ac_build_alloca_undef(struct ac_llvm_context
*ac
, LLVMTypeRef type
,
3668 LLVMBuilderRef builder
= ac
->builder
;
3669 LLVMBasicBlockRef current_block
= LLVMGetInsertBlock(builder
);
3670 LLVMValueRef function
= LLVMGetBasicBlockParent(current_block
);
3671 LLVMBasicBlockRef first_block
= LLVMGetEntryBasicBlock(function
);
3672 LLVMValueRef first_instr
= LLVMGetFirstInstruction(first_block
);
3673 LLVMBuilderRef first_builder
= LLVMCreateBuilderInContext(ac
->context
);
3677 LLVMPositionBuilderBefore(first_builder
, first_instr
);
3679 LLVMPositionBuilderAtEnd(first_builder
, first_block
);
3682 res
= LLVMBuildAlloca(first_builder
, type
, name
);
3683 LLVMDisposeBuilder(first_builder
);
3687 LLVMValueRef
ac_build_alloca(struct ac_llvm_context
*ac
,
3688 LLVMTypeRef type
, const char *name
)
3690 LLVMValueRef ptr
= ac_build_alloca_undef(ac
, type
, name
);
3691 LLVMBuildStore(ac
->builder
, LLVMConstNull(type
), ptr
);
3695 LLVMValueRef
ac_cast_ptr(struct ac_llvm_context
*ctx
, LLVMValueRef ptr
,
3698 int addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
3699 return LLVMBuildBitCast(ctx
->builder
, ptr
,
3700 LLVMPointerType(type
, addr_space
), "");
3703 LLVMValueRef
ac_trim_vector(struct ac_llvm_context
*ctx
, LLVMValueRef value
,
3706 unsigned num_components
= ac_get_llvm_num_components(value
);
3707 if (count
== num_components
)
3710 LLVMValueRef masks
[MAX2(count
, 2)];
3711 masks
[0] = ctx
->i32_0
;
3712 masks
[1] = ctx
->i32_1
;
3713 for (unsigned i
= 2; i
< count
; i
++)
3714 masks
[i
] = LLVMConstInt(ctx
->i32
, i
, false);
3717 return LLVMBuildExtractElement(ctx
->builder
, value
, masks
[0],
3720 LLVMValueRef swizzle
= LLVMConstVector(masks
, count
);
3721 return LLVMBuildShuffleVector(ctx
->builder
, value
, value
, swizzle
, "");
3724 LLVMValueRef
ac_unpack_param(struct ac_llvm_context
*ctx
, LLVMValueRef param
,
3725 unsigned rshift
, unsigned bitwidth
)
3727 LLVMValueRef value
= param
;
3729 value
= LLVMBuildLShr(ctx
->builder
, value
,
3730 LLVMConstInt(ctx
->i32
, rshift
, false), "");
3732 if (rshift
+ bitwidth
< 32) {
3733 unsigned mask
= (1 << bitwidth
) - 1;
3734 value
= LLVMBuildAnd(ctx
->builder
, value
,
3735 LLVMConstInt(ctx
->i32
, mask
, false), "");
3740 /* Adjust the sample index according to FMASK.
3742 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
3743 * which is the identity mapping. Each nibble says which physical sample
3744 * should be fetched to get that sample.
3746 * For example, 0x11111100 means there are only 2 samples stored and
3747 * the second sample covers 3/4 of the pixel. When reading samples 0
3748 * and 1, return physical sample 0 (determined by the first two 0s
3749 * in FMASK), otherwise return physical sample 1.
3751 * The sample index should be adjusted as follows:
3752 * addr[sample_index] = (fmask >> (addr[sample_index] * 4)) & 0xF;
3754 void ac_apply_fmask_to_sample(struct ac_llvm_context
*ac
, LLVMValueRef fmask
,
3755 LLVMValueRef
*addr
, bool is_array_tex
)
3757 struct ac_image_args fmask_load
= {};
3758 fmask_load
.opcode
= ac_image_load
;
3759 fmask_load
.resource
= fmask
;
3760 fmask_load
.dmask
= 0xf;
3761 fmask_load
.dim
= is_array_tex
? ac_image_2darray
: ac_image_2d
;
3762 fmask_load
.attributes
= AC_FUNC_ATTR_READNONE
;
3764 fmask_load
.coords
[0] = addr
[0];
3765 fmask_load
.coords
[1] = addr
[1];
3767 fmask_load
.coords
[2] = addr
[2];
3769 LLVMValueRef fmask_value
= ac_build_image_opcode(ac
, &fmask_load
);
3770 fmask_value
= LLVMBuildExtractElement(ac
->builder
, fmask_value
,
3773 /* Apply the formula. */
3774 unsigned sample_chan
= is_array_tex
? 3 : 2;
3775 LLVMValueRef final_sample
;
3776 final_sample
= LLVMBuildMul(ac
->builder
, addr
[sample_chan
],
3777 LLVMConstInt(ac
->i32
, 4, 0), "");
3778 final_sample
= LLVMBuildLShr(ac
->builder
, fmask_value
, final_sample
, "");
3779 /* Mask the sample index by 0x7, because 0x8 means an unknown value
3780 * with EQAA, so those will map to 0. */
3781 final_sample
= LLVMBuildAnd(ac
->builder
, final_sample
,
3782 LLVMConstInt(ac
->i32
, 0x7, 0), "");
3784 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
3785 * resource descriptor is 0 (invalid).
3788 tmp
= LLVMBuildBitCast(ac
->builder
, fmask
, ac
->v8i32
, "");
3789 tmp
= LLVMBuildExtractElement(ac
->builder
, tmp
, ac
->i32_1
, "");
3790 tmp
= LLVMBuildICmp(ac
->builder
, LLVMIntNE
, tmp
, ac
->i32_0
, "");
3792 /* Replace the MSAA sample index. */
3793 addr
[sample_chan
] = LLVMBuildSelect(ac
->builder
, tmp
, final_sample
,
3794 addr
[sample_chan
], "");
3798 _ac_build_readlane(struct ac_llvm_context
*ctx
, LLVMValueRef src
, LLVMValueRef lane
)
3800 ac_build_optimization_barrier(ctx
, &src
);
3801 return ac_build_intrinsic(ctx
,
3802 lane
== NULL
? "llvm.amdgcn.readfirstlane" : "llvm.amdgcn.readlane",
3803 LLVMTypeOf(src
), (LLVMValueRef
[]) {
3805 lane
== NULL
? 1 : 2,
3806 AC_FUNC_ATTR_READNONE
|
3807 AC_FUNC_ATTR_CONVERGENT
);
3811 * Builds the "llvm.amdgcn.readlane" or "llvm.amdgcn.readfirstlane" intrinsic.
3814 * @param lane - id of the lane or NULL for the first active lane
3815 * @return value of the lane
3818 ac_build_readlane(struct ac_llvm_context
*ctx
, LLVMValueRef src
, LLVMValueRef lane
)
3820 LLVMTypeRef src_type
= LLVMTypeOf(src
);
3821 src
= ac_to_integer(ctx
, src
);
3822 unsigned bits
= LLVMGetIntTypeWidth(LLVMTypeOf(src
));
3826 ret
= _ac_build_readlane(ctx
, src
, lane
);
3828 assert(bits
% 32 == 0);
3829 LLVMTypeRef vec_type
= LLVMVectorType(ctx
->i32
, bits
/ 32);
3830 LLVMValueRef src_vector
=
3831 LLVMBuildBitCast(ctx
->builder
, src
, vec_type
, "");
3832 ret
= LLVMGetUndef(vec_type
);
3833 for (unsigned i
= 0; i
< bits
/ 32; i
++) {
3834 src
= LLVMBuildExtractElement(ctx
->builder
, src_vector
,
3835 LLVMConstInt(ctx
->i32
, i
, 0), "");
3836 LLVMValueRef ret_comp
= _ac_build_readlane(ctx
, src
, lane
);
3837 ret
= LLVMBuildInsertElement(ctx
->builder
, ret
, ret_comp
,
3838 LLVMConstInt(ctx
->i32
, i
, 0), "");
3841 return LLVMBuildBitCast(ctx
->builder
, ret
, src_type
, "");
3845 ac_build_writelane(struct ac_llvm_context
*ctx
, LLVMValueRef src
, LLVMValueRef value
, LLVMValueRef lane
)
3847 if (HAVE_LLVM
>= 0x0800) {
3848 return ac_build_intrinsic(ctx
, "llvm.amdgcn.writelane", ctx
->i32
,
3849 (LLVMValueRef
[]) {value
, lane
, src
}, 3,
3850 AC_FUNC_ATTR_READNONE
| AC_FUNC_ATTR_CONVERGENT
);
3853 LLVMValueRef pred
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, lane
,
3854 ac_get_thread_id(ctx
), "");
3855 return LLVMBuildSelect(ctx
->builder
, pred
, value
, src
, "");
3859 ac_build_mbcnt(struct ac_llvm_context
*ctx
, LLVMValueRef mask
)
3861 if (ctx
->wave_size
== 32) {
3862 return ac_build_intrinsic(ctx
, "llvm.amdgcn.mbcnt.lo", ctx
->i32
,
3863 (LLVMValueRef
[]) { mask
, ctx
->i32_0
},
3864 2, AC_FUNC_ATTR_READNONE
);
3866 LLVMValueRef mask_vec
= LLVMBuildBitCast(ctx
->builder
, mask
,
3867 LLVMVectorType(ctx
->i32
, 2),
3869 LLVMValueRef mask_lo
= LLVMBuildExtractElement(ctx
->builder
, mask_vec
,
3871 LLVMValueRef mask_hi
= LLVMBuildExtractElement(ctx
->builder
, mask_vec
,
3874 ac_build_intrinsic(ctx
, "llvm.amdgcn.mbcnt.lo", ctx
->i32
,
3875 (LLVMValueRef
[]) { mask_lo
, ctx
->i32_0
},
3876 2, AC_FUNC_ATTR_READNONE
);
3877 val
= ac_build_intrinsic(ctx
, "llvm.amdgcn.mbcnt.hi", ctx
->i32
,
3878 (LLVMValueRef
[]) { mask_hi
, val
},
3879 2, AC_FUNC_ATTR_READNONE
);
3884 _dpp_quad_perm
= 0x000,
3885 _dpp_row_sl
= 0x100,
3886 _dpp_row_sr
= 0x110,
3887 _dpp_row_rr
= 0x120,
3892 dpp_row_mirror
= 0x140,
3893 dpp_row_half_mirror
= 0x141,
3894 dpp_row_bcast15
= 0x142,
3895 dpp_row_bcast31
= 0x143
3898 static inline enum dpp_ctrl
3899 dpp_quad_perm(unsigned lane0
, unsigned lane1
, unsigned lane2
, unsigned lane3
)
3901 assert(lane0
< 4 && lane1
< 4 && lane2
< 4 && lane3
< 4);
3902 return _dpp_quad_perm
| lane0
| (lane1
<< 2) | (lane2
<< 4) | (lane3
<< 6);
3905 static inline enum dpp_ctrl
3906 dpp_row_sl(unsigned amount
)
3908 assert(amount
> 0 && amount
< 16);
3909 return _dpp_row_sl
| amount
;
3912 static inline enum dpp_ctrl
3913 dpp_row_sr(unsigned amount
)
3915 assert(amount
> 0 && amount
< 16);
3916 return _dpp_row_sr
| amount
;
3920 _ac_build_dpp(struct ac_llvm_context
*ctx
, LLVMValueRef old
, LLVMValueRef src
,
3921 enum dpp_ctrl dpp_ctrl
, unsigned row_mask
, unsigned bank_mask
,
3924 return ac_build_intrinsic(ctx
, "llvm.amdgcn.update.dpp.i32",
3928 LLVMConstInt(ctx
->i32
, dpp_ctrl
, 0),
3929 LLVMConstInt(ctx
->i32
, row_mask
, 0),
3930 LLVMConstInt(ctx
->i32
, bank_mask
, 0),
3931 LLVMConstInt(ctx
->i1
, bound_ctrl
, 0) },
3932 6, AC_FUNC_ATTR_READNONE
| AC_FUNC_ATTR_CONVERGENT
);
3936 ac_build_dpp(struct ac_llvm_context
*ctx
, LLVMValueRef old
, LLVMValueRef src
,
3937 enum dpp_ctrl dpp_ctrl
, unsigned row_mask
, unsigned bank_mask
,
3940 LLVMTypeRef src_type
= LLVMTypeOf(src
);
3941 src
= ac_to_integer(ctx
, src
);
3942 old
= ac_to_integer(ctx
, old
);
3943 unsigned bits
= LLVMGetIntTypeWidth(LLVMTypeOf(src
));
3946 ret
= _ac_build_dpp(ctx
, old
, src
, dpp_ctrl
, row_mask
,
3947 bank_mask
, bound_ctrl
);
3949 assert(bits
% 32 == 0);
3950 LLVMTypeRef vec_type
= LLVMVectorType(ctx
->i32
, bits
/ 32);
3951 LLVMValueRef src_vector
=
3952 LLVMBuildBitCast(ctx
->builder
, src
, vec_type
, "");
3953 LLVMValueRef old_vector
=
3954 LLVMBuildBitCast(ctx
->builder
, old
, vec_type
, "");
3955 ret
= LLVMGetUndef(vec_type
);
3956 for (unsigned i
= 0; i
< bits
/ 32; i
++) {
3957 src
= LLVMBuildExtractElement(ctx
->builder
, src_vector
,
3958 LLVMConstInt(ctx
->i32
, i
,
3960 old
= LLVMBuildExtractElement(ctx
->builder
, old_vector
,
3961 LLVMConstInt(ctx
->i32
, i
,
3963 LLVMValueRef ret_comp
= _ac_build_dpp(ctx
, old
, src
,
3968 ret
= LLVMBuildInsertElement(ctx
->builder
, ret
,
3970 LLVMConstInt(ctx
->i32
, i
,
3974 return LLVMBuildBitCast(ctx
->builder
, ret
, src_type
, "");
3978 _ac_build_permlane16(struct ac_llvm_context
*ctx
, LLVMValueRef src
, uint64_t sel
,
3979 bool exchange_rows
, bool bound_ctrl
)
3981 LLVMValueRef args
[6] = {
3984 LLVMConstInt(ctx
->i32
, sel
, false),
3985 LLVMConstInt(ctx
->i32
, sel
>> 32, false),
3986 ctx
->i1true
, /* fi */
3987 bound_ctrl
? ctx
->i1true
: ctx
->i1false
,
3989 return ac_build_intrinsic(ctx
, exchange_rows
? "llvm.amdgcn.permlanex16"
3990 : "llvm.amdgcn.permlane16",
3992 AC_FUNC_ATTR_READNONE
| AC_FUNC_ATTR_CONVERGENT
);
3996 ac_build_permlane16(struct ac_llvm_context
*ctx
, LLVMValueRef src
, uint64_t sel
,
3997 bool exchange_rows
, bool bound_ctrl
)
3999 LLVMTypeRef src_type
= LLVMTypeOf(src
);
4000 src
= ac_to_integer(ctx
, src
);
4001 unsigned bits
= LLVMGetIntTypeWidth(LLVMTypeOf(src
));
4004 ret
= _ac_build_permlane16(ctx
, src
, sel
, exchange_rows
,
4007 assert(bits
% 32 == 0);
4008 LLVMTypeRef vec_type
= LLVMVectorType(ctx
->i32
, bits
/ 32);
4009 LLVMValueRef src_vector
=
4010 LLVMBuildBitCast(ctx
->builder
, src
, vec_type
, "");
4011 ret
= LLVMGetUndef(vec_type
);
4012 for (unsigned i
= 0; i
< bits
/ 32; i
++) {
4013 src
= LLVMBuildExtractElement(ctx
->builder
, src_vector
,
4014 LLVMConstInt(ctx
->i32
, i
,
4016 LLVMValueRef ret_comp
=
4017 _ac_build_permlane16(ctx
, src
, sel
,
4020 ret
= LLVMBuildInsertElement(ctx
->builder
, ret
,
4022 LLVMConstInt(ctx
->i32
, i
,
4026 return LLVMBuildBitCast(ctx
->builder
, ret
, src_type
, "");
4029 static inline unsigned
4030 ds_pattern_bitmode(unsigned and_mask
, unsigned or_mask
, unsigned xor_mask
)
4032 assert(and_mask
< 32 && or_mask
< 32 && xor_mask
< 32);
4033 return and_mask
| (or_mask
<< 5) | (xor_mask
<< 10);
4037 _ac_build_ds_swizzle(struct ac_llvm_context
*ctx
, LLVMValueRef src
, unsigned mask
)
4039 return ac_build_intrinsic(ctx
, "llvm.amdgcn.ds.swizzle",
4040 LLVMTypeOf(src
), (LLVMValueRef
[]) {
4041 src
, LLVMConstInt(ctx
->i32
, mask
, 0) },
4042 2, AC_FUNC_ATTR_READNONE
| AC_FUNC_ATTR_CONVERGENT
);
4046 ac_build_ds_swizzle(struct ac_llvm_context
*ctx
, LLVMValueRef src
, unsigned mask
)
4048 LLVMTypeRef src_type
= LLVMTypeOf(src
);
4049 src
= ac_to_integer(ctx
, src
);
4050 unsigned bits
= LLVMGetIntTypeWidth(LLVMTypeOf(src
));
4053 ret
= _ac_build_ds_swizzle(ctx
, src
, mask
);
4055 assert(bits
% 32 == 0);
4056 LLVMTypeRef vec_type
= LLVMVectorType(ctx
->i32
, bits
/ 32);
4057 LLVMValueRef src_vector
=
4058 LLVMBuildBitCast(ctx
->builder
, src
, vec_type
, "");
4059 ret
= LLVMGetUndef(vec_type
);
4060 for (unsigned i
= 0; i
< bits
/ 32; i
++) {
4061 src
= LLVMBuildExtractElement(ctx
->builder
, src_vector
,
4062 LLVMConstInt(ctx
->i32
, i
,
4064 LLVMValueRef ret_comp
= _ac_build_ds_swizzle(ctx
, src
,
4066 ret
= LLVMBuildInsertElement(ctx
->builder
, ret
,
4068 LLVMConstInt(ctx
->i32
, i
,
4072 return LLVMBuildBitCast(ctx
->builder
, ret
, src_type
, "");
4076 ac_build_wwm(struct ac_llvm_context
*ctx
, LLVMValueRef src
)
4078 char name
[32], type
[8];
4079 ac_build_type_name_for_intr(LLVMTypeOf(src
), type
, sizeof(type
));
4080 snprintf(name
, sizeof(name
), "llvm.amdgcn.wwm.%s", type
);
4081 return ac_build_intrinsic(ctx
, name
, LLVMTypeOf(src
),
4082 (LLVMValueRef
[]) { src
}, 1,
4083 AC_FUNC_ATTR_READNONE
);
4087 ac_build_set_inactive(struct ac_llvm_context
*ctx
, LLVMValueRef src
,
4088 LLVMValueRef inactive
)
4090 char name
[33], type
[8];
4091 LLVMTypeRef src_type
= LLVMTypeOf(src
);
4092 src
= ac_to_integer(ctx
, src
);
4093 inactive
= ac_to_integer(ctx
, inactive
);
4094 ac_build_type_name_for_intr(LLVMTypeOf(src
), type
, sizeof(type
));
4095 snprintf(name
, sizeof(name
), "llvm.amdgcn.set.inactive.%s", type
);
4097 ac_build_intrinsic(ctx
, name
,
4098 LLVMTypeOf(src
), (LLVMValueRef
[]) {
4100 AC_FUNC_ATTR_READNONE
|
4101 AC_FUNC_ATTR_CONVERGENT
);
4102 return LLVMBuildBitCast(ctx
->builder
, ret
, src_type
, "");
4106 get_reduction_identity(struct ac_llvm_context
*ctx
, nir_op op
, unsigned type_size
)
4108 if (type_size
== 4) {
4110 case nir_op_iadd
: return ctx
->i32_0
;
4111 case nir_op_fadd
: return ctx
->f32_0
;
4112 case nir_op_imul
: return ctx
->i32_1
;
4113 case nir_op_fmul
: return ctx
->f32_1
;
4114 case nir_op_imin
: return LLVMConstInt(ctx
->i32
, INT32_MAX
, 0);
4115 case nir_op_umin
: return LLVMConstInt(ctx
->i32
, UINT32_MAX
, 0);
4116 case nir_op_fmin
: return LLVMConstReal(ctx
->f32
, INFINITY
);
4117 case nir_op_imax
: return LLVMConstInt(ctx
->i32
, INT32_MIN
, 0);
4118 case nir_op_umax
: return ctx
->i32_0
;
4119 case nir_op_fmax
: return LLVMConstReal(ctx
->f32
, -INFINITY
);
4120 case nir_op_iand
: return LLVMConstInt(ctx
->i32
, -1, 0);
4121 case nir_op_ior
: return ctx
->i32_0
;
4122 case nir_op_ixor
: return ctx
->i32_0
;
4124 unreachable("bad reduction intrinsic");
4126 } else { /* type_size == 64bit */
4128 case nir_op_iadd
: return ctx
->i64_0
;
4129 case nir_op_fadd
: return ctx
->f64_0
;
4130 case nir_op_imul
: return ctx
->i64_1
;
4131 case nir_op_fmul
: return ctx
->f64_1
;
4132 case nir_op_imin
: return LLVMConstInt(ctx
->i64
, INT64_MAX
, 0);
4133 case nir_op_umin
: return LLVMConstInt(ctx
->i64
, UINT64_MAX
, 0);
4134 case nir_op_fmin
: return LLVMConstReal(ctx
->f64
, INFINITY
);
4135 case nir_op_imax
: return LLVMConstInt(ctx
->i64
, INT64_MIN
, 0);
4136 case nir_op_umax
: return ctx
->i64_0
;
4137 case nir_op_fmax
: return LLVMConstReal(ctx
->f64
, -INFINITY
);
4138 case nir_op_iand
: return LLVMConstInt(ctx
->i64
, -1, 0);
4139 case nir_op_ior
: return ctx
->i64_0
;
4140 case nir_op_ixor
: return ctx
->i64_0
;
4142 unreachable("bad reduction intrinsic");
4148 ac_build_alu_op(struct ac_llvm_context
*ctx
, LLVMValueRef lhs
, LLVMValueRef rhs
, nir_op op
)
4150 bool _64bit
= ac_get_type_size(LLVMTypeOf(lhs
)) == 8;
4152 case nir_op_iadd
: return LLVMBuildAdd(ctx
->builder
, lhs
, rhs
, "");
4153 case nir_op_fadd
: return LLVMBuildFAdd(ctx
->builder
, lhs
, rhs
, "");
4154 case nir_op_imul
: return LLVMBuildMul(ctx
->builder
, lhs
, rhs
, "");
4155 case nir_op_fmul
: return LLVMBuildFMul(ctx
->builder
, lhs
, rhs
, "");
4156 case nir_op_imin
: return LLVMBuildSelect(ctx
->builder
,
4157 LLVMBuildICmp(ctx
->builder
, LLVMIntSLT
, lhs
, rhs
, ""),
4159 case nir_op_umin
: return LLVMBuildSelect(ctx
->builder
,
4160 LLVMBuildICmp(ctx
->builder
, LLVMIntULT
, lhs
, rhs
, ""),
4162 case nir_op_fmin
: return ac_build_intrinsic(ctx
,
4163 _64bit
? "llvm.minnum.f64" : "llvm.minnum.f32",
4164 _64bit
? ctx
->f64
: ctx
->f32
,
4165 (LLVMValueRef
[]){lhs
, rhs
}, 2, AC_FUNC_ATTR_READNONE
);
4166 case nir_op_imax
: return LLVMBuildSelect(ctx
->builder
,
4167 LLVMBuildICmp(ctx
->builder
, LLVMIntSGT
, lhs
, rhs
, ""),
4169 case nir_op_umax
: return LLVMBuildSelect(ctx
->builder
,
4170 LLVMBuildICmp(ctx
->builder
, LLVMIntUGT
, lhs
, rhs
, ""),
4172 case nir_op_fmax
: return ac_build_intrinsic(ctx
,
4173 _64bit
? "llvm.maxnum.f64" : "llvm.maxnum.f32",
4174 _64bit
? ctx
->f64
: ctx
->f32
,
4175 (LLVMValueRef
[]){lhs
, rhs
}, 2, AC_FUNC_ATTR_READNONE
);
4176 case nir_op_iand
: return LLVMBuildAnd(ctx
->builder
, lhs
, rhs
, "");
4177 case nir_op_ior
: return LLVMBuildOr(ctx
->builder
, lhs
, rhs
, "");
4178 case nir_op_ixor
: return LLVMBuildXor(ctx
->builder
, lhs
, rhs
, "");
4180 unreachable("bad reduction intrinsic");
4185 * \param maxprefix specifies that the result only needs to be correct for a
4186 * prefix of this many threads
4188 * TODO: add inclusive and excluse scan functions for GFX6.
4191 ac_build_scan(struct ac_llvm_context
*ctx
, nir_op op
, LLVMValueRef src
, LLVMValueRef identity
,
4192 unsigned maxprefix
, bool inclusive
)
4194 LLVMValueRef result
, tmp
;
4196 if (ctx
->chip_class
>= GFX10
) {
4197 result
= inclusive
? src
: identity
;
4202 result
= ac_build_dpp(ctx
, identity
, src
, dpp_wf_sr1
, 0xf, 0xf, false);
4206 tmp
= ac_build_dpp(ctx
, identity
, src
, dpp_row_sr(1), 0xf, 0xf, false);
4207 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
4210 tmp
= ac_build_dpp(ctx
, identity
, src
, dpp_row_sr(2), 0xf, 0xf, false);
4211 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
4214 tmp
= ac_build_dpp(ctx
, identity
, src
, dpp_row_sr(3), 0xf, 0xf, false);
4215 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
4218 tmp
= ac_build_dpp(ctx
, identity
, result
, dpp_row_sr(4), 0xf, 0xe, false);
4219 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
4222 tmp
= ac_build_dpp(ctx
, identity
, result
, dpp_row_sr(8), 0xf, 0xc, false);
4223 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
4224 if (maxprefix
<= 16)
4227 if (ctx
->chip_class
>= GFX10
) {
4228 /* dpp_row_bcast{15,31} are not supported on gfx10. */
4229 LLVMBuilderRef builder
= ctx
->builder
;
4230 LLVMValueRef tid
= ac_get_thread_id(ctx
);
4232 /* TODO-GFX10: Can we get better code-gen by putting this into
4233 * a branch so that LLVM generates EXEC mask manipulations? */
4237 tmp
= ac_build_alu_op(ctx
, result
, src
, op
);
4238 tmp
= ac_build_permlane16(ctx
, tmp
, ~(uint64_t)0, true, false);
4239 tmp
= ac_build_alu_op(ctx
, result
, tmp
, op
);
4240 cc
= LLVMBuildAnd(builder
, tid
, LLVMConstInt(ctx
->i32
, 16, false), "");
4241 cc
= LLVMBuildICmp(builder
, LLVMIntNE
, cc
, ctx
->i32_0
, "");
4242 result
= LLVMBuildSelect(builder
, cc
, tmp
, result
, "");
4243 if (maxprefix
<= 32)
4249 tmp
= ac_build_alu_op(ctx
, result
, src
, op
);
4250 tmp
= ac_build_readlane(ctx
, tmp
, LLVMConstInt(ctx
->i32
, 31, false));
4251 tmp
= ac_build_alu_op(ctx
, result
, tmp
, op
);
4252 cc
= LLVMBuildICmp(builder
, LLVMIntUGE
, tid
,
4253 LLVMConstInt(ctx
->i32
, 32, false), "");
4254 result
= LLVMBuildSelect(builder
, cc
, tmp
, result
, "");
4258 tmp
= ac_build_dpp(ctx
, identity
, result
, dpp_row_bcast15
, 0xa, 0xf, false);
4259 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
4260 if (maxprefix
<= 32)
4262 tmp
= ac_build_dpp(ctx
, identity
, result
, dpp_row_bcast31
, 0xc, 0xf, false);
4263 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
4268 ac_build_inclusive_scan(struct ac_llvm_context
*ctx
, LLVMValueRef src
, nir_op op
)
4270 LLVMValueRef result
;
4272 if (LLVMTypeOf(src
) == ctx
->i1
&& op
== nir_op_iadd
) {
4273 LLVMBuilderRef builder
= ctx
->builder
;
4274 src
= LLVMBuildZExt(builder
, src
, ctx
->i32
, "");
4275 result
= ac_build_ballot(ctx
, src
);
4276 result
= ac_build_mbcnt(ctx
, result
);
4277 result
= LLVMBuildAdd(builder
, result
, src
, "");
4281 ac_build_optimization_barrier(ctx
, &src
);
4283 LLVMValueRef identity
=
4284 get_reduction_identity(ctx
, op
, ac_get_type_size(LLVMTypeOf(src
)));
4285 result
= LLVMBuildBitCast(ctx
->builder
, ac_build_set_inactive(ctx
, src
, identity
),
4286 LLVMTypeOf(identity
), "");
4287 result
= ac_build_scan(ctx
, op
, result
, identity
, ctx
->wave_size
, true);
4289 return ac_build_wwm(ctx
, result
);
4293 ac_build_exclusive_scan(struct ac_llvm_context
*ctx
, LLVMValueRef src
, nir_op op
)
4295 LLVMValueRef result
;
4297 if (LLVMTypeOf(src
) == ctx
->i1
&& op
== nir_op_iadd
) {
4298 LLVMBuilderRef builder
= ctx
->builder
;
4299 src
= LLVMBuildZExt(builder
, src
, ctx
->i32
, "");
4300 result
= ac_build_ballot(ctx
, src
);
4301 result
= ac_build_mbcnt(ctx
, result
);
4305 ac_build_optimization_barrier(ctx
, &src
);
4307 LLVMValueRef identity
=
4308 get_reduction_identity(ctx
, op
, ac_get_type_size(LLVMTypeOf(src
)));
4309 result
= LLVMBuildBitCast(ctx
->builder
, ac_build_set_inactive(ctx
, src
, identity
),
4310 LLVMTypeOf(identity
), "");
4311 result
= ac_build_scan(ctx
, op
, result
, identity
, ctx
->wave_size
, false);
4313 return ac_build_wwm(ctx
, result
);
4317 ac_build_reduce(struct ac_llvm_context
*ctx
, LLVMValueRef src
, nir_op op
, unsigned cluster_size
)
4319 if (cluster_size
== 1) return src
;
4320 ac_build_optimization_barrier(ctx
, &src
);
4321 LLVMValueRef result
, swap
;
4322 LLVMValueRef identity
= get_reduction_identity(ctx
, op
,
4323 ac_get_type_size(LLVMTypeOf(src
)));
4324 result
= LLVMBuildBitCast(ctx
->builder
,
4325 ac_build_set_inactive(ctx
, src
, identity
),
4326 LLVMTypeOf(identity
), "");
4327 swap
= ac_build_quad_swizzle(ctx
, result
, 1, 0, 3, 2);
4328 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
4329 if (cluster_size
== 2) return ac_build_wwm(ctx
, result
);
4331 swap
= ac_build_quad_swizzle(ctx
, result
, 2, 3, 0, 1);
4332 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
4333 if (cluster_size
== 4) return ac_build_wwm(ctx
, result
);
4335 if (ctx
->chip_class
>= GFX8
)
4336 swap
= ac_build_dpp(ctx
, identity
, result
, dpp_row_half_mirror
, 0xf, 0xf, false);
4338 swap
= ac_build_ds_swizzle(ctx
, result
, ds_pattern_bitmode(0x1f, 0, 0x04));
4339 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
4340 if (cluster_size
== 8) return ac_build_wwm(ctx
, result
);
4342 if (ctx
->chip_class
>= GFX8
)
4343 swap
= ac_build_dpp(ctx
, identity
, result
, dpp_row_mirror
, 0xf, 0xf, false);
4345 swap
= ac_build_ds_swizzle(ctx
, result
, ds_pattern_bitmode(0x1f, 0, 0x08));
4346 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
4347 if (cluster_size
== 16) return ac_build_wwm(ctx
, result
);
4349 if (ctx
->chip_class
>= GFX10
)
4350 swap
= ac_build_permlane16(ctx
, result
, 0, true, false);
4351 else if (ctx
->chip_class
>= GFX8
&& cluster_size
!= 32)
4352 swap
= ac_build_dpp(ctx
, identity
, result
, dpp_row_bcast15
, 0xa, 0xf, false);
4354 swap
= ac_build_ds_swizzle(ctx
, result
, ds_pattern_bitmode(0x1f, 0, 0x10));
4355 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
4356 if (cluster_size
== 32) return ac_build_wwm(ctx
, result
);
4358 if (ctx
->chip_class
>= GFX8
) {
4359 if (ctx
->chip_class
>= GFX10
)
4360 swap
= ac_build_readlane(ctx
, result
, LLVMConstInt(ctx
->i32
, 31, false));
4362 swap
= ac_build_dpp(ctx
, identity
, result
, dpp_row_bcast31
, 0xc, 0xf, false);
4363 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
4364 result
= ac_build_readlane(ctx
, result
, LLVMConstInt(ctx
->i32
, 63, 0));
4365 return ac_build_wwm(ctx
, result
);
4367 swap
= ac_build_readlane(ctx
, result
, ctx
->i32_0
);
4368 result
= ac_build_readlane(ctx
, result
, LLVMConstInt(ctx
->i32
, 32, 0));
4369 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
4370 return ac_build_wwm(ctx
, result
);
4375 * "Top half" of a scan that reduces per-wave values across an entire
4378 * The source value must be present in the highest lane of the wave, and the
4379 * highest lane must be live.
4382 ac_build_wg_wavescan_top(struct ac_llvm_context
*ctx
, struct ac_wg_scan
*ws
)
4384 if (ws
->maxwaves
<= 1)
4387 const LLVMValueRef last_lane
= LLVMConstInt(ctx
->i32
, ctx
->wave_size
- 1, false);
4388 LLVMBuilderRef builder
= ctx
->builder
;
4389 LLVMValueRef tid
= ac_get_thread_id(ctx
);
4392 tmp
= LLVMBuildICmp(builder
, LLVMIntEQ
, tid
, last_lane
, "");
4393 ac_build_ifcc(ctx
, tmp
, 1000);
4394 LLVMBuildStore(builder
, ws
->src
, LLVMBuildGEP(builder
, ws
->scratch
, &ws
->waveidx
, 1, ""));
4395 ac_build_endif(ctx
, 1000);
4399 * "Bottom half" of a scan that reduces per-wave values across an entire
4402 * The caller must place a barrier between the top and bottom halves.
4405 ac_build_wg_wavescan_bottom(struct ac_llvm_context
*ctx
, struct ac_wg_scan
*ws
)
4407 const LLVMTypeRef type
= LLVMTypeOf(ws
->src
);
4408 const LLVMValueRef identity
=
4409 get_reduction_identity(ctx
, ws
->op
, ac_get_type_size(type
));
4411 if (ws
->maxwaves
<= 1) {
4412 ws
->result_reduce
= ws
->src
;
4413 ws
->result_inclusive
= ws
->src
;
4414 ws
->result_exclusive
= identity
;
4417 assert(ws
->maxwaves
<= 32);
4419 LLVMBuilderRef builder
= ctx
->builder
;
4420 LLVMValueRef tid
= ac_get_thread_id(ctx
);
4421 LLVMBasicBlockRef bbs
[2];
4422 LLVMValueRef phivalues_scan
[2];
4423 LLVMValueRef tmp
, tmp2
;
4425 bbs
[0] = LLVMGetInsertBlock(builder
);
4426 phivalues_scan
[0] = LLVMGetUndef(type
);
4428 if (ws
->enable_reduce
)
4429 tmp
= LLVMBuildICmp(builder
, LLVMIntULT
, tid
, ws
->numwaves
, "");
4430 else if (ws
->enable_inclusive
)
4431 tmp
= LLVMBuildICmp(builder
, LLVMIntULE
, tid
, ws
->waveidx
, "");
4433 tmp
= LLVMBuildICmp(builder
, LLVMIntULT
, tid
, ws
->waveidx
, "");
4434 ac_build_ifcc(ctx
, tmp
, 1001);
4436 tmp
= LLVMBuildLoad(builder
, LLVMBuildGEP(builder
, ws
->scratch
, &tid
, 1, ""), "");
4438 ac_build_optimization_barrier(ctx
, &tmp
);
4440 bbs
[1] = LLVMGetInsertBlock(builder
);
4441 phivalues_scan
[1] = ac_build_scan(ctx
, ws
->op
, tmp
, identity
, ws
->maxwaves
, true);
4443 ac_build_endif(ctx
, 1001);
4445 const LLVMValueRef scan
= ac_build_phi(ctx
, type
, 2, phivalues_scan
, bbs
);
4447 if (ws
->enable_reduce
) {
4448 tmp
= LLVMBuildSub(builder
, ws
->numwaves
, ctx
->i32_1
, "");
4449 ws
->result_reduce
= ac_build_readlane(ctx
, scan
, tmp
);
4451 if (ws
->enable_inclusive
)
4452 ws
->result_inclusive
= ac_build_readlane(ctx
, scan
, ws
->waveidx
);
4453 if (ws
->enable_exclusive
) {
4454 tmp
= LLVMBuildSub(builder
, ws
->waveidx
, ctx
->i32_1
, "");
4455 tmp
= ac_build_readlane(ctx
, scan
, tmp
);
4456 tmp2
= LLVMBuildICmp(builder
, LLVMIntEQ
, ws
->waveidx
, ctx
->i32_0
, "");
4457 ws
->result_exclusive
= LLVMBuildSelect(builder
, tmp2
, identity
, tmp
, "");
4462 * Inclusive scan of a per-wave value across an entire workgroup.
4464 * This implies an s_barrier instruction.
4466 * Unlike ac_build_inclusive_scan, the caller \em must ensure that all threads
4467 * of the workgroup are live. (This requirement cannot easily be relaxed in a
4468 * useful manner because of the barrier in the algorithm.)
4471 ac_build_wg_wavescan(struct ac_llvm_context
*ctx
, struct ac_wg_scan
*ws
)
4473 ac_build_wg_wavescan_top(ctx
, ws
);
4474 ac_build_s_barrier(ctx
);
4475 ac_build_wg_wavescan_bottom(ctx
, ws
);
4479 * "Top half" of a scan that reduces per-thread values across an entire
4482 * All lanes must be active when this code runs.
4485 ac_build_wg_scan_top(struct ac_llvm_context
*ctx
, struct ac_wg_scan
*ws
)
4487 if (ws
->enable_exclusive
) {
4488 ws
->extra
= ac_build_exclusive_scan(ctx
, ws
->src
, ws
->op
);
4489 if (LLVMTypeOf(ws
->src
) == ctx
->i1
&& ws
->op
== nir_op_iadd
)
4490 ws
->src
= LLVMBuildZExt(ctx
->builder
, ws
->src
, ctx
->i32
, "");
4491 ws
->src
= ac_build_alu_op(ctx
, ws
->extra
, ws
->src
, ws
->op
);
4493 ws
->src
= ac_build_inclusive_scan(ctx
, ws
->src
, ws
->op
);
4496 bool enable_inclusive
= ws
->enable_inclusive
;
4497 bool enable_exclusive
= ws
->enable_exclusive
;
4498 ws
->enable_inclusive
= false;
4499 ws
->enable_exclusive
= ws
->enable_exclusive
|| enable_inclusive
;
4500 ac_build_wg_wavescan_top(ctx
, ws
);
4501 ws
->enable_inclusive
= enable_inclusive
;
4502 ws
->enable_exclusive
= enable_exclusive
;
4506 * "Bottom half" of a scan that reduces per-thread values across an entire
4509 * The caller must place a barrier between the top and bottom halves.
4512 ac_build_wg_scan_bottom(struct ac_llvm_context
*ctx
, struct ac_wg_scan
*ws
)
4514 bool enable_inclusive
= ws
->enable_inclusive
;
4515 bool enable_exclusive
= ws
->enable_exclusive
;
4516 ws
->enable_inclusive
= false;
4517 ws
->enable_exclusive
= ws
->enable_exclusive
|| enable_inclusive
;
4518 ac_build_wg_wavescan_bottom(ctx
, ws
);
4519 ws
->enable_inclusive
= enable_inclusive
;
4520 ws
->enable_exclusive
= enable_exclusive
;
4522 /* ws->result_reduce is already the correct value */
4523 if (ws
->enable_inclusive
)
4524 ws
->result_inclusive
= ac_build_alu_op(ctx
, ws
->result_inclusive
, ws
->src
, ws
->op
);
4525 if (ws
->enable_exclusive
)
4526 ws
->result_exclusive
= ac_build_alu_op(ctx
, ws
->result_exclusive
, ws
->extra
, ws
->op
);
4530 * A scan that reduces per-thread values across an entire workgroup.
4532 * The caller must ensure that all lanes are active when this code runs
4533 * (WWM is insufficient!), because there is an implied barrier.
4536 ac_build_wg_scan(struct ac_llvm_context
*ctx
, struct ac_wg_scan
*ws
)
4538 ac_build_wg_scan_top(ctx
, ws
);
4539 ac_build_s_barrier(ctx
);
4540 ac_build_wg_scan_bottom(ctx
, ws
);
4544 ac_build_quad_swizzle(struct ac_llvm_context
*ctx
, LLVMValueRef src
,
4545 unsigned lane0
, unsigned lane1
, unsigned lane2
, unsigned lane3
)
4547 unsigned mask
= dpp_quad_perm(lane0
, lane1
, lane2
, lane3
);
4548 if (ctx
->chip_class
>= GFX8
) {
4549 return ac_build_dpp(ctx
, src
, src
, mask
, 0xf, 0xf, false);
4551 return ac_build_ds_swizzle(ctx
, src
, (1 << 15) | mask
);
4556 ac_build_shuffle(struct ac_llvm_context
*ctx
, LLVMValueRef src
, LLVMValueRef index
)
4558 index
= LLVMBuildMul(ctx
->builder
, index
, LLVMConstInt(ctx
->i32
, 4, 0), "");
4559 return ac_build_intrinsic(ctx
,
4560 "llvm.amdgcn.ds.bpermute", ctx
->i32
,
4561 (LLVMValueRef
[]) {index
, src
}, 2,
4562 AC_FUNC_ATTR_READNONE
|
4563 AC_FUNC_ATTR_CONVERGENT
);
4567 ac_build_frexp_exp(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
4573 if (bitsize
== 16) {
4574 intr
= "llvm.amdgcn.frexp.exp.i16.f16";
4576 } else if (bitsize
== 32) {
4577 intr
= "llvm.amdgcn.frexp.exp.i32.f32";
4580 intr
= "llvm.amdgcn.frexp.exp.i32.f64";
4584 LLVMValueRef params
[] = {
4587 return ac_build_intrinsic(ctx
, intr
, type
, params
, 1,
4588 AC_FUNC_ATTR_READNONE
);
4591 ac_build_frexp_mant(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
4597 if (bitsize
== 16) {
4598 intr
= "llvm.amdgcn.frexp.mant.f16";
4600 } else if (bitsize
== 32) {
4601 intr
= "llvm.amdgcn.frexp.mant.f32";
4604 intr
= "llvm.amdgcn.frexp.mant.f64";
4608 LLVMValueRef params
[] = {
4611 return ac_build_intrinsic(ctx
, intr
, type
, params
, 1,
4612 AC_FUNC_ATTR_READNONE
);
4616 * this takes an I,J coordinate pair,
4617 * and works out the X and Y derivatives.
4618 * it returns DDX(I), DDX(J), DDY(I), DDY(J).
4621 ac_build_ddxy_interp(struct ac_llvm_context
*ctx
, LLVMValueRef interp_ij
)
4623 LLVMValueRef result
[4], a
;
4626 for (i
= 0; i
< 2; i
++) {
4627 a
= LLVMBuildExtractElement(ctx
->builder
, interp_ij
,
4628 LLVMConstInt(ctx
->i32
, i
, false), "");
4629 result
[i
] = ac_build_ddxy(ctx
, AC_TID_MASK_TOP_LEFT
, 1, a
);
4630 result
[2+i
] = ac_build_ddxy(ctx
, AC_TID_MASK_TOP_LEFT
, 2, a
);
4632 return ac_build_gather_values(ctx
, result
, 4);
4636 ac_build_load_helper_invocation(struct ac_llvm_context
*ctx
)
4638 LLVMValueRef result
= ac_build_intrinsic(ctx
, "llvm.amdgcn.ps.live",
4640 AC_FUNC_ATTR_READNONE
);
4641 result
= LLVMBuildNot(ctx
->builder
, result
, "");
4642 return LLVMBuildSExt(ctx
->builder
, result
, ctx
->i32
, "");
4645 LLVMValueRef
ac_build_call(struct ac_llvm_context
*ctx
, LLVMValueRef func
,
4646 LLVMValueRef
*args
, unsigned num_args
)
4648 LLVMValueRef ret
= LLVMBuildCall(ctx
->builder
, func
, args
, num_args
, "");
4649 LLVMSetInstructionCallConv(ret
, LLVMGetFunctionCallConv(func
));