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
,
64 unsigned ballot_mask_bits
)
68 ctx
->context
= LLVMContextCreate();
70 ctx
->chip_class
= chip_class
;
72 ctx
->wave_size
= wave_size
;
73 ctx
->ballot_mask_bits
= ballot_mask_bits
;
74 ctx
->module
= ac_create_module(wave_size
== 32 ? compiler
->tm_wave32
77 ctx
->builder
= ac_create_builder(ctx
->context
, float_mode
);
79 ctx
->voidt
= LLVMVoidTypeInContext(ctx
->context
);
80 ctx
->i1
= LLVMInt1TypeInContext(ctx
->context
);
81 ctx
->i8
= LLVMInt8TypeInContext(ctx
->context
);
82 ctx
->i16
= LLVMIntTypeInContext(ctx
->context
, 16);
83 ctx
->i32
= LLVMIntTypeInContext(ctx
->context
, 32);
84 ctx
->i64
= LLVMIntTypeInContext(ctx
->context
, 64);
85 ctx
->intptr
= ctx
->i32
;
86 ctx
->f16
= LLVMHalfTypeInContext(ctx
->context
);
87 ctx
->f32
= LLVMFloatTypeInContext(ctx
->context
);
88 ctx
->f64
= LLVMDoubleTypeInContext(ctx
->context
);
89 ctx
->v2i16
= LLVMVectorType(ctx
->i16
, 2);
90 ctx
->v2i32
= LLVMVectorType(ctx
->i32
, 2);
91 ctx
->v3i32
= LLVMVectorType(ctx
->i32
, 3);
92 ctx
->v4i32
= LLVMVectorType(ctx
->i32
, 4);
93 ctx
->v2f32
= LLVMVectorType(ctx
->f32
, 2);
94 ctx
->v3f32
= LLVMVectorType(ctx
->f32
, 3);
95 ctx
->v4f32
= LLVMVectorType(ctx
->f32
, 4);
96 ctx
->v8i32
= LLVMVectorType(ctx
->i32
, 8);
97 ctx
->iN_wavemask
= LLVMIntTypeInContext(ctx
->context
, ctx
->wave_size
);
98 ctx
->iN_ballotmask
= LLVMIntTypeInContext(ctx
->context
, ballot_mask_bits
);
100 ctx
->i8_0
= LLVMConstInt(ctx
->i8
, 0, false);
101 ctx
->i8_1
= LLVMConstInt(ctx
->i8
, 1, false);
102 ctx
->i16_0
= LLVMConstInt(ctx
->i16
, 0, false);
103 ctx
->i16_1
= LLVMConstInt(ctx
->i16
, 1, false);
104 ctx
->i32_0
= LLVMConstInt(ctx
->i32
, 0, false);
105 ctx
->i32_1
= LLVMConstInt(ctx
->i32
, 1, false);
106 ctx
->i64_0
= LLVMConstInt(ctx
->i64
, 0, false);
107 ctx
->i64_1
= LLVMConstInt(ctx
->i64
, 1, false);
108 ctx
->f16_0
= LLVMConstReal(ctx
->f16
, 0.0);
109 ctx
->f16_1
= LLVMConstReal(ctx
->f16
, 1.0);
110 ctx
->f32_0
= LLVMConstReal(ctx
->f32
, 0.0);
111 ctx
->f32_1
= LLVMConstReal(ctx
->f32
, 1.0);
112 ctx
->f64_0
= LLVMConstReal(ctx
->f64
, 0.0);
113 ctx
->f64_1
= LLVMConstReal(ctx
->f64
, 1.0);
115 ctx
->i1false
= LLVMConstInt(ctx
->i1
, 0, false);
116 ctx
->i1true
= LLVMConstInt(ctx
->i1
, 1, false);
118 ctx
->range_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
121 ctx
->invariant_load_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
122 "invariant.load", 14);
124 ctx
->fpmath_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
, "fpmath", 6);
126 args
[0] = LLVMConstReal(ctx
->f32
, 2.5);
127 ctx
->fpmath_md_2p5_ulp
= LLVMMDNodeInContext(ctx
->context
, args
, 1);
129 ctx
->uniform_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
130 "amdgpu.uniform", 14);
132 ctx
->empty_md
= LLVMMDNodeInContext(ctx
->context
, NULL
, 0);
133 ctx
->flow
= calloc(1, sizeof(*ctx
->flow
));
137 ac_llvm_context_dispose(struct ac_llvm_context
*ctx
)
139 free(ctx
->flow
->stack
);
145 ac_get_llvm_num_components(LLVMValueRef value
)
147 LLVMTypeRef type
= LLVMTypeOf(value
);
148 unsigned num_components
= LLVMGetTypeKind(type
) == LLVMVectorTypeKind
149 ? LLVMGetVectorSize(type
)
151 return num_components
;
155 ac_llvm_extract_elem(struct ac_llvm_context
*ac
,
159 if (LLVMGetTypeKind(LLVMTypeOf(value
)) != LLVMVectorTypeKind
) {
164 return LLVMBuildExtractElement(ac
->builder
, value
,
165 LLVMConstInt(ac
->i32
, index
, false), "");
169 ac_get_elem_bits(struct ac_llvm_context
*ctx
, LLVMTypeRef type
)
171 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
)
172 type
= LLVMGetElementType(type
);
174 if (LLVMGetTypeKind(type
) == LLVMIntegerTypeKind
)
175 return LLVMGetIntTypeWidth(type
);
177 if (type
== ctx
->f16
)
179 if (type
== ctx
->f32
)
181 if (type
== ctx
->f64
)
184 unreachable("Unhandled type kind in get_elem_bits");
188 ac_get_type_size(LLVMTypeRef type
)
190 LLVMTypeKind kind
= LLVMGetTypeKind(type
);
193 case LLVMIntegerTypeKind
:
194 return LLVMGetIntTypeWidth(type
) / 8;
195 case LLVMHalfTypeKind
:
197 case LLVMFloatTypeKind
:
199 case LLVMDoubleTypeKind
:
201 case LLVMPointerTypeKind
:
202 if (LLVMGetPointerAddressSpace(type
) == AC_ADDR_SPACE_CONST_32BIT
)
205 case LLVMVectorTypeKind
:
206 return LLVMGetVectorSize(type
) *
207 ac_get_type_size(LLVMGetElementType(type
));
208 case LLVMArrayTypeKind
:
209 return LLVMGetArrayLength(type
) *
210 ac_get_type_size(LLVMGetElementType(type
));
217 static LLVMTypeRef
to_integer_type_scalar(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
221 else if (t
== ctx
->f16
|| t
== ctx
->i16
)
223 else if (t
== ctx
->f32
|| t
== ctx
->i32
)
225 else if (t
== ctx
->f64
|| t
== ctx
->i64
)
228 unreachable("Unhandled integer size");
232 ac_to_integer_type(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
234 if (LLVMGetTypeKind(t
) == LLVMVectorTypeKind
) {
235 LLVMTypeRef elem_type
= LLVMGetElementType(t
);
236 return LLVMVectorType(to_integer_type_scalar(ctx
, elem_type
),
237 LLVMGetVectorSize(t
));
239 if (LLVMGetTypeKind(t
) == LLVMPointerTypeKind
) {
240 switch (LLVMGetPointerAddressSpace(t
)) {
241 case AC_ADDR_SPACE_GLOBAL
:
243 case AC_ADDR_SPACE_LDS
:
246 unreachable("unhandled address space");
249 return to_integer_type_scalar(ctx
, t
);
253 ac_to_integer(struct ac_llvm_context
*ctx
, LLVMValueRef v
)
255 LLVMTypeRef type
= LLVMTypeOf(v
);
256 if (LLVMGetTypeKind(type
) == LLVMPointerTypeKind
) {
257 return LLVMBuildPtrToInt(ctx
->builder
, v
, ac_to_integer_type(ctx
, type
), "");
259 return LLVMBuildBitCast(ctx
->builder
, v
, ac_to_integer_type(ctx
, type
), "");
263 ac_to_integer_or_pointer(struct ac_llvm_context
*ctx
, LLVMValueRef v
)
265 LLVMTypeRef type
= LLVMTypeOf(v
);
266 if (LLVMGetTypeKind(type
) == LLVMPointerTypeKind
)
268 return ac_to_integer(ctx
, v
);
271 static LLVMTypeRef
to_float_type_scalar(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
275 else if (t
== ctx
->i16
|| t
== ctx
->f16
)
277 else if (t
== ctx
->i32
|| t
== ctx
->f32
)
279 else if (t
== ctx
->i64
|| t
== ctx
->f64
)
282 unreachable("Unhandled float size");
286 ac_to_float_type(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
288 if (LLVMGetTypeKind(t
) == LLVMVectorTypeKind
) {
289 LLVMTypeRef elem_type
= LLVMGetElementType(t
);
290 return LLVMVectorType(to_float_type_scalar(ctx
, elem_type
),
291 LLVMGetVectorSize(t
));
293 return to_float_type_scalar(ctx
, t
);
297 ac_to_float(struct ac_llvm_context
*ctx
, LLVMValueRef v
)
299 LLVMTypeRef type
= LLVMTypeOf(v
);
300 return LLVMBuildBitCast(ctx
->builder
, v
, ac_to_float_type(ctx
, type
), "");
305 ac_build_intrinsic(struct ac_llvm_context
*ctx
, const char *name
,
306 LLVMTypeRef return_type
, LLVMValueRef
*params
,
307 unsigned param_count
, unsigned attrib_mask
)
309 LLVMValueRef function
, call
;
310 bool set_callsite_attrs
= !(attrib_mask
& AC_FUNC_ATTR_LEGACY
);
312 function
= LLVMGetNamedFunction(ctx
->module
, name
);
314 LLVMTypeRef param_types
[32], function_type
;
317 assert(param_count
<= 32);
319 for (i
= 0; i
< param_count
; ++i
) {
321 param_types
[i
] = LLVMTypeOf(params
[i
]);
324 LLVMFunctionType(return_type
, param_types
, param_count
, 0);
325 function
= LLVMAddFunction(ctx
->module
, name
, function_type
);
327 LLVMSetFunctionCallConv(function
, LLVMCCallConv
);
328 LLVMSetLinkage(function
, LLVMExternalLinkage
);
330 if (!set_callsite_attrs
)
331 ac_add_func_attributes(ctx
->context
, function
, attrib_mask
);
334 call
= LLVMBuildCall(ctx
->builder
, function
, params
, param_count
, "");
335 if (set_callsite_attrs
)
336 ac_add_func_attributes(ctx
->context
, call
, attrib_mask
);
341 * Given the i32 or vNi32 \p type, generate the textual name (e.g. for use with
344 void ac_build_type_name_for_intr(LLVMTypeRef type
, char *buf
, unsigned bufsize
)
346 LLVMTypeRef elem_type
= type
;
348 assert(bufsize
>= 8);
350 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
) {
351 int ret
= snprintf(buf
, bufsize
, "v%u",
352 LLVMGetVectorSize(type
));
354 char *type_name
= LLVMPrintTypeToString(type
);
355 fprintf(stderr
, "Error building type name for: %s\n",
357 LLVMDisposeMessage(type_name
);
360 elem_type
= LLVMGetElementType(type
);
364 switch (LLVMGetTypeKind(elem_type
)) {
366 case LLVMIntegerTypeKind
:
367 snprintf(buf
, bufsize
, "i%d", LLVMGetIntTypeWidth(elem_type
));
369 case LLVMHalfTypeKind
:
370 snprintf(buf
, bufsize
, "f16");
372 case LLVMFloatTypeKind
:
373 snprintf(buf
, bufsize
, "f32");
375 case LLVMDoubleTypeKind
:
376 snprintf(buf
, bufsize
, "f64");
382 * Helper function that builds an LLVM IR PHI node and immediately adds
386 ac_build_phi(struct ac_llvm_context
*ctx
, LLVMTypeRef type
,
387 unsigned count_incoming
, LLVMValueRef
*values
,
388 LLVMBasicBlockRef
*blocks
)
390 LLVMValueRef phi
= LLVMBuildPhi(ctx
->builder
, type
, "");
391 LLVMAddIncoming(phi
, values
, blocks
, count_incoming
);
395 void ac_build_s_barrier(struct ac_llvm_context
*ctx
)
397 ac_build_intrinsic(ctx
, "llvm.amdgcn.s.barrier", ctx
->voidt
, NULL
,
398 0, AC_FUNC_ATTR_CONVERGENT
);
401 /* Prevent optimizations (at least of memory accesses) across the current
402 * point in the program by emitting empty inline assembly that is marked as
403 * having side effects.
405 * Optionally, a value can be passed through the inline assembly to prevent
406 * LLVM from hoisting calls to ReadNone functions.
409 ac_build_optimization_barrier(struct ac_llvm_context
*ctx
,
412 static int counter
= 0;
414 LLVMBuilderRef builder
= ctx
->builder
;
417 snprintf(code
, sizeof(code
), "; %d", p_atomic_inc_return(&counter
));
420 LLVMTypeRef ftype
= LLVMFunctionType(ctx
->voidt
, NULL
, 0, false);
421 LLVMValueRef inlineasm
= LLVMConstInlineAsm(ftype
, code
, "", true, false);
422 LLVMBuildCall(builder
, inlineasm
, NULL
, 0, "");
424 LLVMTypeRef ftype
= LLVMFunctionType(ctx
->i32
, &ctx
->i32
, 1, false);
425 LLVMValueRef inlineasm
= LLVMConstInlineAsm(ftype
, code
, "=v,0", true, false);
426 LLVMValueRef vgpr
= *pvgpr
;
427 LLVMTypeRef vgpr_type
= LLVMTypeOf(vgpr
);
428 unsigned vgpr_size
= ac_get_type_size(vgpr_type
);
431 assert(vgpr_size
% 4 == 0);
433 vgpr
= LLVMBuildBitCast(builder
, vgpr
, LLVMVectorType(ctx
->i32
, vgpr_size
/ 4), "");
434 vgpr0
= LLVMBuildExtractElement(builder
, vgpr
, ctx
->i32_0
, "");
435 vgpr0
= LLVMBuildCall(builder
, inlineasm
, &vgpr0
, 1, "");
436 vgpr
= LLVMBuildInsertElement(builder
, vgpr
, vgpr0
, ctx
->i32_0
, "");
437 vgpr
= LLVMBuildBitCast(builder
, vgpr
, vgpr_type
, "");
444 ac_build_shader_clock(struct ac_llvm_context
*ctx
)
446 const char *intr
= HAVE_LLVM
>= 0x0900 && ctx
->chip_class
>= GFX8
?
447 "llvm.amdgcn.s.memrealtime" : "llvm.readcyclecounter";
448 LLVMValueRef tmp
= ac_build_intrinsic(ctx
, intr
, ctx
->i64
, NULL
, 0, 0);
449 return LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->v2i32
, "");
453 ac_build_ballot(struct ac_llvm_context
*ctx
,
458 if (HAVE_LLVM
>= 0x900) {
459 if (ctx
->wave_size
== 64)
460 name
= "llvm.amdgcn.icmp.i64.i32";
462 name
= "llvm.amdgcn.icmp.i32.i32";
464 name
= "llvm.amdgcn.icmp.i32";
466 LLVMValueRef args
[3] = {
469 LLVMConstInt(ctx
->i32
, LLVMIntNE
, 0)
472 /* We currently have no other way to prevent LLVM from lifting the icmp
473 * calls to a dominating basic block.
475 ac_build_optimization_barrier(ctx
, &args
[0]);
477 args
[0] = ac_to_integer(ctx
, args
[0]);
479 return ac_build_intrinsic(ctx
, name
, ctx
->iN_wavemask
, args
, 3,
480 AC_FUNC_ATTR_NOUNWIND
|
481 AC_FUNC_ATTR_READNONE
|
482 AC_FUNC_ATTR_CONVERGENT
);
485 LLVMValueRef
ac_get_i1_sgpr_mask(struct ac_llvm_context
*ctx
,
488 const char *name
= HAVE_LLVM
>= 0x900 ? "llvm.amdgcn.icmp.i64.i1" : "llvm.amdgcn.icmp.i1";
489 LLVMValueRef args
[3] = {
492 LLVMConstInt(ctx
->i32
, LLVMIntNE
, 0),
495 assert(HAVE_LLVM
>= 0x0800);
496 return ac_build_intrinsic(ctx
, name
, ctx
->i64
, args
, 3,
497 AC_FUNC_ATTR_NOUNWIND
|
498 AC_FUNC_ATTR_READNONE
|
499 AC_FUNC_ATTR_CONVERGENT
);
503 ac_build_vote_all(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
505 LLVMValueRef active_set
= ac_build_ballot(ctx
, ctx
->i32_1
);
506 LLVMValueRef vote_set
= ac_build_ballot(ctx
, value
);
507 return LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, vote_set
, active_set
, "");
511 ac_build_vote_any(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
513 LLVMValueRef vote_set
= ac_build_ballot(ctx
, value
);
514 return LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, vote_set
,
515 LLVMConstInt(ctx
->iN_wavemask
, 0, 0), "");
519 ac_build_vote_eq(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
521 LLVMValueRef active_set
= ac_build_ballot(ctx
, ctx
->i32_1
);
522 LLVMValueRef vote_set
= ac_build_ballot(ctx
, value
);
524 LLVMValueRef all
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
525 vote_set
, active_set
, "");
526 LLVMValueRef none
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
528 LLVMConstInt(ctx
->iN_wavemask
, 0, 0), "");
529 return LLVMBuildOr(ctx
->builder
, all
, none
, "");
533 ac_build_varying_gather_values(struct ac_llvm_context
*ctx
, LLVMValueRef
*values
,
534 unsigned value_count
, unsigned component
)
536 LLVMValueRef vec
= NULL
;
538 if (value_count
== 1) {
539 return values
[component
];
540 } else if (!value_count
)
541 unreachable("value_count is 0");
543 for (unsigned i
= component
; i
< value_count
+ component
; i
++) {
544 LLVMValueRef value
= values
[i
];
547 vec
= LLVMGetUndef( LLVMVectorType(LLVMTypeOf(value
), value_count
));
548 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, i
- component
, false);
549 vec
= LLVMBuildInsertElement(ctx
->builder
, vec
, value
, index
, "");
555 ac_build_gather_values_extended(struct ac_llvm_context
*ctx
,
556 LLVMValueRef
*values
,
557 unsigned value_count
,
558 unsigned value_stride
,
562 LLVMBuilderRef builder
= ctx
->builder
;
563 LLVMValueRef vec
= NULL
;
566 if (value_count
== 1 && !always_vector
) {
568 return LLVMBuildLoad(builder
, values
[0], "");
570 } else if (!value_count
)
571 unreachable("value_count is 0");
573 for (i
= 0; i
< value_count
; i
++) {
574 LLVMValueRef value
= values
[i
* value_stride
];
576 value
= LLVMBuildLoad(builder
, value
, "");
579 vec
= LLVMGetUndef( LLVMVectorType(LLVMTypeOf(value
), value_count
));
580 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, i
, false);
581 vec
= LLVMBuildInsertElement(builder
, vec
, value
, index
, "");
587 ac_build_gather_values(struct ac_llvm_context
*ctx
,
588 LLVMValueRef
*values
,
589 unsigned value_count
)
591 return ac_build_gather_values_extended(ctx
, values
, value_count
, 1, false, false);
594 /* Expand a scalar or vector to <dst_channels x type> by filling the remaining
595 * channels with undef. Extract at most src_channels components from the input.
598 ac_build_expand(struct ac_llvm_context
*ctx
,
600 unsigned src_channels
,
601 unsigned dst_channels
)
603 LLVMTypeRef elemtype
;
604 LLVMValueRef chan
[dst_channels
];
606 if (LLVMGetTypeKind(LLVMTypeOf(value
)) == LLVMVectorTypeKind
) {
607 unsigned vec_size
= LLVMGetVectorSize(LLVMTypeOf(value
));
609 if (src_channels
== dst_channels
&& vec_size
== dst_channels
)
612 src_channels
= MIN2(src_channels
, vec_size
);
614 for (unsigned i
= 0; i
< src_channels
; i
++)
615 chan
[i
] = ac_llvm_extract_elem(ctx
, value
, i
);
617 elemtype
= LLVMGetElementType(LLVMTypeOf(value
));
620 assert(src_channels
== 1);
623 elemtype
= LLVMTypeOf(value
);
626 for (unsigned i
= src_channels
; i
< dst_channels
; i
++)
627 chan
[i
] = LLVMGetUndef(elemtype
);
629 return ac_build_gather_values(ctx
, chan
, dst_channels
);
632 /* Extract components [start, start + channels) from a vector.
635 ac_extract_components(struct ac_llvm_context
*ctx
,
640 LLVMValueRef chan
[channels
];
642 for (unsigned i
= 0; i
< channels
; i
++)
643 chan
[i
] = ac_llvm_extract_elem(ctx
, value
, i
+ start
);
645 return ac_build_gather_values(ctx
, chan
, channels
);
648 /* Expand a scalar or vector to <4 x type> by filling the remaining channels
649 * with undef. Extract at most num_channels components from the input.
651 LLVMValueRef
ac_build_expand_to_vec4(struct ac_llvm_context
*ctx
,
653 unsigned num_channels
)
655 return ac_build_expand(ctx
, value
, num_channels
, 4);
658 LLVMValueRef
ac_build_round(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
660 unsigned type_size
= ac_get_type_size(LLVMTypeOf(value
));
664 name
= "llvm.rint.f16";
665 else if (type_size
== 4)
666 name
= "llvm.rint.f32";
668 name
= "llvm.rint.f64";
670 return ac_build_intrinsic(ctx
, name
, LLVMTypeOf(value
), &value
, 1,
671 AC_FUNC_ATTR_READNONE
);
675 ac_build_fdiv(struct ac_llvm_context
*ctx
,
679 /* If we do (num / den), LLVM >= 7.0 does:
680 * return num * v_rcp_f32(den * (fabs(den) > 0x1.0p+96f ? 0x1.0p-32f : 1.0f));
682 * If we do (num * (1 / den)), LLVM does:
683 * return num * v_rcp_f32(den);
685 LLVMValueRef one
= LLVMConstReal(LLVMTypeOf(num
), 1.0);
686 LLVMValueRef rcp
= LLVMBuildFDiv(ctx
->builder
, one
, den
, "");
687 LLVMValueRef ret
= LLVMBuildFMul(ctx
->builder
, num
, rcp
, "");
689 /* Use v_rcp_f32 instead of precise division. */
690 if (!LLVMIsConstant(ret
))
691 LLVMSetMetadata(ret
, ctx
->fpmath_md_kind
, ctx
->fpmath_md_2p5_ulp
);
695 /* See fast_idiv_by_const.h. */
696 /* Set: increment = util_fast_udiv_info::increment ? multiplier : 0; */
697 LLVMValueRef
ac_build_fast_udiv(struct ac_llvm_context
*ctx
,
699 LLVMValueRef multiplier
,
700 LLVMValueRef pre_shift
,
701 LLVMValueRef post_shift
,
702 LLVMValueRef increment
)
704 LLVMBuilderRef builder
= ctx
->builder
;
706 num
= LLVMBuildLShr(builder
, num
, pre_shift
, "");
707 num
= LLVMBuildMul(builder
,
708 LLVMBuildZExt(builder
, num
, ctx
->i64
, ""),
709 LLVMBuildZExt(builder
, multiplier
, ctx
->i64
, ""), "");
710 num
= LLVMBuildAdd(builder
, num
,
711 LLVMBuildZExt(builder
, increment
, ctx
->i64
, ""), "");
712 num
= LLVMBuildLShr(builder
, num
, LLVMConstInt(ctx
->i64
, 32, 0), "");
713 num
= LLVMBuildTrunc(builder
, num
, ctx
->i32
, "");
714 return LLVMBuildLShr(builder
, num
, post_shift
, "");
717 /* See fast_idiv_by_const.h. */
718 /* If num != UINT_MAX, this more efficient version can be used. */
719 /* Set: increment = util_fast_udiv_info::increment; */
720 LLVMValueRef
ac_build_fast_udiv_nuw(struct ac_llvm_context
*ctx
,
722 LLVMValueRef multiplier
,
723 LLVMValueRef pre_shift
,
724 LLVMValueRef post_shift
,
725 LLVMValueRef increment
)
727 LLVMBuilderRef builder
= ctx
->builder
;
729 num
= LLVMBuildLShr(builder
, num
, pre_shift
, "");
730 num
= LLVMBuildNUWAdd(builder
, num
, increment
, "");
731 num
= LLVMBuildMul(builder
,
732 LLVMBuildZExt(builder
, num
, ctx
->i64
, ""),
733 LLVMBuildZExt(builder
, multiplier
, ctx
->i64
, ""), "");
734 num
= LLVMBuildLShr(builder
, num
, LLVMConstInt(ctx
->i64
, 32, 0), "");
735 num
= LLVMBuildTrunc(builder
, num
, ctx
->i32
, "");
736 return LLVMBuildLShr(builder
, num
, post_shift
, "");
739 /* See fast_idiv_by_const.h. */
740 /* Both operands must fit in 31 bits and the divisor must not be 1. */
741 LLVMValueRef
ac_build_fast_udiv_u31_d_not_one(struct ac_llvm_context
*ctx
,
743 LLVMValueRef multiplier
,
744 LLVMValueRef post_shift
)
746 LLVMBuilderRef builder
= ctx
->builder
;
748 num
= LLVMBuildMul(builder
,
749 LLVMBuildZExt(builder
, num
, ctx
->i64
, ""),
750 LLVMBuildZExt(builder
, multiplier
, ctx
->i64
, ""), "");
751 num
= LLVMBuildLShr(builder
, num
, LLVMConstInt(ctx
->i64
, 32, 0), "");
752 num
= LLVMBuildTrunc(builder
, num
, ctx
->i32
, "");
753 return LLVMBuildLShr(builder
, num
, post_shift
, "");
756 /* Coordinates for cube map selection. sc, tc, and ma are as in Table 8.27
757 * of the OpenGL 4.5 (Compatibility Profile) specification, except ma is
758 * already multiplied by two. id is the cube face number.
760 struct cube_selection_coords
{
767 build_cube_intrinsic(struct ac_llvm_context
*ctx
,
769 struct cube_selection_coords
*out
)
771 LLVMTypeRef f32
= ctx
->f32
;
773 out
->stc
[1] = ac_build_intrinsic(ctx
, "llvm.amdgcn.cubetc",
774 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
775 out
->stc
[0] = ac_build_intrinsic(ctx
, "llvm.amdgcn.cubesc",
776 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
777 out
->ma
= ac_build_intrinsic(ctx
, "llvm.amdgcn.cubema",
778 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
779 out
->id
= ac_build_intrinsic(ctx
, "llvm.amdgcn.cubeid",
780 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
784 * Build a manual selection sequence for cube face sc/tc coordinates and
785 * major axis vector (multiplied by 2 for consistency) for the given
786 * vec3 \p coords, for the face implied by \p selcoords.
788 * For the major axis, we always adjust the sign to be in the direction of
789 * selcoords.ma; i.e., a positive out_ma means that coords is pointed towards
790 * the selcoords major axis.
792 static void build_cube_select(struct ac_llvm_context
*ctx
,
793 const struct cube_selection_coords
*selcoords
,
794 const LLVMValueRef
*coords
,
795 LLVMValueRef
*out_st
,
796 LLVMValueRef
*out_ma
)
798 LLVMBuilderRef builder
= ctx
->builder
;
799 LLVMTypeRef f32
= LLVMTypeOf(coords
[0]);
800 LLVMValueRef is_ma_positive
;
802 LLVMValueRef is_ma_z
, is_not_ma_z
;
803 LLVMValueRef is_ma_y
;
804 LLVMValueRef is_ma_x
;
808 is_ma_positive
= LLVMBuildFCmp(builder
, LLVMRealUGE
,
809 selcoords
->ma
, LLVMConstReal(f32
, 0.0), "");
810 sgn_ma
= LLVMBuildSelect(builder
, is_ma_positive
,
811 LLVMConstReal(f32
, 1.0), LLVMConstReal(f32
, -1.0), "");
813 is_ma_z
= LLVMBuildFCmp(builder
, LLVMRealUGE
, selcoords
->id
, LLVMConstReal(f32
, 4.0), "");
814 is_not_ma_z
= LLVMBuildNot(builder
, is_ma_z
, "");
815 is_ma_y
= LLVMBuildAnd(builder
, is_not_ma_z
,
816 LLVMBuildFCmp(builder
, LLVMRealUGE
, selcoords
->id
, LLVMConstReal(f32
, 2.0), ""), "");
817 is_ma_x
= LLVMBuildAnd(builder
, is_not_ma_z
, LLVMBuildNot(builder
, is_ma_y
, ""), "");
820 tmp
= LLVMBuildSelect(builder
, is_ma_x
, coords
[2], coords
[0], "");
821 sgn
= LLVMBuildSelect(builder
, is_ma_y
, LLVMConstReal(f32
, 1.0),
822 LLVMBuildSelect(builder
, is_ma_z
, sgn_ma
,
823 LLVMBuildFNeg(builder
, sgn_ma
, ""), ""), "");
824 out_st
[0] = LLVMBuildFMul(builder
, tmp
, sgn
, "");
827 tmp
= LLVMBuildSelect(builder
, is_ma_y
, coords
[2], coords
[1], "");
828 sgn
= LLVMBuildSelect(builder
, is_ma_y
, sgn_ma
,
829 LLVMConstReal(f32
, -1.0), "");
830 out_st
[1] = LLVMBuildFMul(builder
, tmp
, sgn
, "");
833 tmp
= LLVMBuildSelect(builder
, is_ma_z
, coords
[2],
834 LLVMBuildSelect(builder
, is_ma_y
, coords
[1], coords
[0], ""), "");
835 tmp
= ac_build_intrinsic(ctx
, "llvm.fabs.f32",
836 ctx
->f32
, &tmp
, 1, AC_FUNC_ATTR_READNONE
);
837 *out_ma
= LLVMBuildFMul(builder
, tmp
, LLVMConstReal(f32
, 2.0), "");
841 ac_prepare_cube_coords(struct ac_llvm_context
*ctx
,
842 bool is_deriv
, bool is_array
, bool is_lod
,
843 LLVMValueRef
*coords_arg
,
844 LLVMValueRef
*derivs_arg
)
847 LLVMBuilderRef builder
= ctx
->builder
;
848 struct cube_selection_coords selcoords
;
849 LLVMValueRef coords
[3];
852 if (is_array
&& !is_lod
) {
853 LLVMValueRef tmp
= ac_build_round(ctx
, coords_arg
[3]);
855 /* Section 8.9 (Texture Functions) of the GLSL 4.50 spec says:
857 * "For Array forms, the array layer used will be
859 * max(0, min(d−1, floor(layer+0.5)))
861 * where d is the depth of the texture array and layer
862 * comes from the component indicated in the tables below.
863 * Workaroudn for an issue where the layer is taken from a
864 * helper invocation which happens to fall on a different
865 * layer due to extrapolation."
867 * GFX8 and earlier attempt to implement this in hardware by
868 * clamping the value of coords[2] = (8 * layer) + face.
869 * Unfortunately, this means that the we end up with the wrong
870 * face when clamping occurs.
872 * Clamp the layer earlier to work around the issue.
874 if (ctx
->chip_class
<= GFX8
) {
876 ge0
= LLVMBuildFCmp(builder
, LLVMRealOGE
, tmp
, ctx
->f32_0
, "");
877 tmp
= LLVMBuildSelect(builder
, ge0
, tmp
, ctx
->f32_0
, "");
883 build_cube_intrinsic(ctx
, coords_arg
, &selcoords
);
885 invma
= ac_build_intrinsic(ctx
, "llvm.fabs.f32",
886 ctx
->f32
, &selcoords
.ma
, 1, AC_FUNC_ATTR_READNONE
);
887 invma
= ac_build_fdiv(ctx
, LLVMConstReal(ctx
->f32
, 1.0), invma
);
889 for (int i
= 0; i
< 2; ++i
)
890 coords
[i
] = LLVMBuildFMul(builder
, selcoords
.stc
[i
], invma
, "");
892 coords
[2] = selcoords
.id
;
894 if (is_deriv
&& derivs_arg
) {
895 LLVMValueRef derivs
[4];
898 /* Convert cube derivatives to 2D derivatives. */
899 for (axis
= 0; axis
< 2; axis
++) {
900 LLVMValueRef deriv_st
[2];
901 LLVMValueRef deriv_ma
;
903 /* Transform the derivative alongside the texture
904 * coordinate. Mathematically, the correct formula is
905 * as follows. Assume we're projecting onto the +Z face
906 * and denote by dx/dh the derivative of the (original)
907 * X texture coordinate with respect to horizontal
908 * window coordinates. The projection onto the +Z face
913 * Then df/dh = df/dx * dx/dh + df/dz * dz/dh
914 * = 1/z * dx/dh - x/z * 1/z * dz/dh.
916 * This motivatives the implementation below.
918 * Whether this actually gives the expected results for
919 * apps that might feed in derivatives obtained via
920 * finite differences is anyone's guess. The OpenGL spec
921 * seems awfully quiet about how textureGrad for cube
922 * maps should be handled.
924 build_cube_select(ctx
, &selcoords
, &derivs_arg
[axis
* 3],
925 deriv_st
, &deriv_ma
);
927 deriv_ma
= LLVMBuildFMul(builder
, deriv_ma
, invma
, "");
929 for (int i
= 0; i
< 2; ++i
)
930 derivs
[axis
* 2 + i
] =
931 LLVMBuildFSub(builder
,
932 LLVMBuildFMul(builder
, deriv_st
[i
], invma
, ""),
933 LLVMBuildFMul(builder
, deriv_ma
, coords
[i
], ""), "");
936 memcpy(derivs_arg
, derivs
, sizeof(derivs
));
939 /* Shift the texture coordinate. This must be applied after the
940 * derivative calculation.
942 for (int i
= 0; i
< 2; ++i
)
943 coords
[i
] = LLVMBuildFAdd(builder
, coords
[i
], LLVMConstReal(ctx
->f32
, 1.5), "");
946 /* for cube arrays coord.z = coord.w(array_index) * 8 + face */
947 /* coords_arg.w component - array_index for cube arrays */
948 coords
[2] = ac_build_fmad(ctx
, coords_arg
[3], LLVMConstReal(ctx
->f32
, 8.0), coords
[2]);
951 memcpy(coords_arg
, coords
, sizeof(coords
));
956 ac_build_fs_interp(struct ac_llvm_context
*ctx
,
957 LLVMValueRef llvm_chan
,
958 LLVMValueRef attr_number
,
963 LLVMValueRef args
[5];
968 args
[2] = attr_number
;
971 p1
= ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p1",
972 ctx
->f32
, args
, 4, AC_FUNC_ATTR_READNONE
);
977 args
[3] = attr_number
;
980 return ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p2",
981 ctx
->f32
, args
, 5, AC_FUNC_ATTR_READNONE
);
985 ac_build_fs_interp_f16(struct ac_llvm_context
*ctx
,
986 LLVMValueRef llvm_chan
,
987 LLVMValueRef attr_number
,
992 LLVMValueRef args
[6];
997 args
[2] = attr_number
;
998 args
[3] = ctx
->i1false
;
1001 p1
= ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p1.f16",
1002 ctx
->f32
, args
, 5, AC_FUNC_ATTR_READNONE
);
1006 args
[2] = llvm_chan
;
1007 args
[3] = attr_number
;
1008 args
[4] = ctx
->i1false
;
1011 return ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p2.f16",
1012 ctx
->f16
, args
, 6, AC_FUNC_ATTR_READNONE
);
1016 ac_build_fs_interp_mov(struct ac_llvm_context
*ctx
,
1017 LLVMValueRef parameter
,
1018 LLVMValueRef llvm_chan
,
1019 LLVMValueRef attr_number
,
1020 LLVMValueRef params
)
1022 LLVMValueRef args
[4];
1024 args
[0] = parameter
;
1025 args
[1] = llvm_chan
;
1026 args
[2] = attr_number
;
1029 return ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.mov",
1030 ctx
->f32
, args
, 4, AC_FUNC_ATTR_READNONE
);
1034 ac_build_gep_ptr(struct ac_llvm_context
*ctx
,
1035 LLVMValueRef base_ptr
,
1038 return LLVMBuildGEP(ctx
->builder
, base_ptr
, &index
, 1, "");
1042 ac_build_gep0(struct ac_llvm_context
*ctx
,
1043 LLVMValueRef base_ptr
,
1046 LLVMValueRef indices
[2] = {
1050 return LLVMBuildGEP(ctx
->builder
, base_ptr
, indices
, 2, "");
1053 LLVMValueRef
ac_build_pointer_add(struct ac_llvm_context
*ctx
, LLVMValueRef ptr
,
1056 return LLVMBuildPointerCast(ctx
->builder
,
1057 LLVMBuildGEP(ctx
->builder
, ptr
, &index
, 1, ""),
1058 LLVMTypeOf(ptr
), "");
1062 ac_build_indexed_store(struct ac_llvm_context
*ctx
,
1063 LLVMValueRef base_ptr
, LLVMValueRef index
,
1066 LLVMBuildStore(ctx
->builder
, value
,
1067 ac_build_gep0(ctx
, base_ptr
, index
));
1071 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad.
1072 * It's equivalent to doing a load from &base_ptr[index].
1074 * \param base_ptr Where the array starts.
1075 * \param index The element index into the array.
1076 * \param uniform Whether the base_ptr and index can be assumed to be
1077 * dynamically uniform (i.e. load to an SGPR)
1078 * \param invariant Whether the load is invariant (no other opcodes affect it)
1079 * \param no_unsigned_wraparound
1080 * For all possible re-associations and re-distributions of an expression
1081 * "base_ptr + index * elemsize" into "addr + offset" (excluding GEPs
1082 * without inbounds in base_ptr), this parameter is true if "addr + offset"
1083 * does not result in an unsigned integer wraparound. This is used for
1084 * optimal code generation of 32-bit pointer arithmetic.
1086 * For example, a 32-bit immediate offset that causes a 32-bit unsigned
1087 * integer wraparound can't be an imm offset in s_load_dword, because
1088 * the instruction performs "addr + offset" in 64 bits.
1090 * Expected usage for bindless textures by chaining GEPs:
1091 * // possible unsigned wraparound, don't use InBounds:
1092 * ptr1 = LLVMBuildGEP(base_ptr, index);
1093 * image = load(ptr1); // becomes "s_load ptr1, 0"
1095 * ptr2 = LLVMBuildInBoundsGEP(ptr1, 32 / elemsize);
1096 * sampler = load(ptr2); // becomes "s_load ptr1, 32" thanks to InBounds
1099 ac_build_load_custom(struct ac_llvm_context
*ctx
, LLVMValueRef base_ptr
,
1100 LLVMValueRef index
, bool uniform
, bool invariant
,
1101 bool no_unsigned_wraparound
)
1103 LLVMValueRef pointer
, result
;
1105 if (no_unsigned_wraparound
&&
1106 LLVMGetPointerAddressSpace(LLVMTypeOf(base_ptr
)) == AC_ADDR_SPACE_CONST_32BIT
)
1107 pointer
= LLVMBuildInBoundsGEP(ctx
->builder
, base_ptr
, &index
, 1, "");
1109 pointer
= LLVMBuildGEP(ctx
->builder
, base_ptr
, &index
, 1, "");
1112 LLVMSetMetadata(pointer
, ctx
->uniform_md_kind
, ctx
->empty_md
);
1113 result
= LLVMBuildLoad(ctx
->builder
, pointer
, "");
1115 LLVMSetMetadata(result
, ctx
->invariant_load_md_kind
, ctx
->empty_md
);
1119 LLVMValueRef
ac_build_load(struct ac_llvm_context
*ctx
, LLVMValueRef base_ptr
,
1122 return ac_build_load_custom(ctx
, base_ptr
, index
, false, false, false);
1125 LLVMValueRef
ac_build_load_invariant(struct ac_llvm_context
*ctx
,
1126 LLVMValueRef base_ptr
, LLVMValueRef index
)
1128 return ac_build_load_custom(ctx
, base_ptr
, index
, false, true, false);
1131 /* This assumes that there is no unsigned integer wraparound during the address
1132 * computation, excluding all GEPs within base_ptr. */
1133 LLVMValueRef
ac_build_load_to_sgpr(struct ac_llvm_context
*ctx
,
1134 LLVMValueRef base_ptr
, LLVMValueRef index
)
1136 return ac_build_load_custom(ctx
, base_ptr
, index
, true, true, true);
1139 /* See ac_build_load_custom() documentation. */
1140 LLVMValueRef
ac_build_load_to_sgpr_uint_wraparound(struct ac_llvm_context
*ctx
,
1141 LLVMValueRef base_ptr
, LLVMValueRef index
)
1143 return ac_build_load_custom(ctx
, base_ptr
, index
, true, true, false);
1146 static unsigned get_load_cache_policy(struct ac_llvm_context
*ctx
,
1147 unsigned cache_policy
)
1149 return cache_policy
|
1150 (ctx
->chip_class
>= GFX10
&& cache_policy
& ac_glc
? ac_dlc
: 0);
1154 ac_build_llvm7_buffer_store_common(struct ac_llvm_context
*ctx
,
1157 LLVMValueRef vindex
,
1158 LLVMValueRef voffset
,
1159 unsigned num_channels
,
1160 unsigned cache_policy
,
1163 LLVMValueRef args
[] = {
1165 LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, ""),
1166 vindex
? vindex
: ctx
->i32_0
,
1168 LLVMConstInt(ctx
->i1
, !!(cache_policy
& ac_glc
), 0),
1169 LLVMConstInt(ctx
->i1
, !!(cache_policy
& ac_slc
), 0)
1171 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
1173 const char *type_names
[] = {"f32", "v2f32", "v4f32"};
1177 snprintf(name
, sizeof(name
), "llvm.amdgcn.buffer.store.format.%s",
1180 snprintf(name
, sizeof(name
), "llvm.amdgcn.buffer.store.%s",
1184 ac_build_intrinsic(ctx
, name
, ctx
->voidt
, args
, ARRAY_SIZE(args
),
1185 AC_FUNC_ATTR_INACCESSIBLE_MEM_ONLY
);
1189 ac_build_llvm8_buffer_store_common(struct ac_llvm_context
*ctx
,
1192 LLVMValueRef vindex
,
1193 LLVMValueRef voffset
,
1194 LLVMValueRef soffset
,
1195 unsigned num_channels
,
1196 LLVMTypeRef return_channel_type
,
1197 unsigned cache_policy
,
1201 LLVMValueRef args
[6];
1204 args
[idx
++] = LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, "");
1206 args
[idx
++] = vindex
? vindex
: ctx
->i32_0
;
1207 args
[idx
++] = voffset
? voffset
: ctx
->i32_0
;
1208 args
[idx
++] = soffset
? soffset
: ctx
->i32_0
;
1209 args
[idx
++] = LLVMConstInt(ctx
->i32
, cache_policy
, 0);
1210 unsigned func
= !ac_has_vec3_support(ctx
->chip_class
, use_format
) && num_channels
== 3 ? 4 : num_channels
;
1211 const char *indexing_kind
= structurized
? "struct" : "raw";
1212 char name
[256], type_name
[8];
1214 LLVMTypeRef type
= func
> 1 ? LLVMVectorType(return_channel_type
, func
) : return_channel_type
;
1215 ac_build_type_name_for_intr(type
, type_name
, sizeof(type_name
));
1218 snprintf(name
, sizeof(name
), "llvm.amdgcn.%s.buffer.store.format.%s",
1219 indexing_kind
, type_name
);
1221 snprintf(name
, sizeof(name
), "llvm.amdgcn.%s.buffer.store.%s",
1222 indexing_kind
, type_name
);
1225 ac_build_intrinsic(ctx
, name
, ctx
->voidt
, args
, idx
,
1226 AC_FUNC_ATTR_INACCESSIBLE_MEM_ONLY
);
1230 ac_build_buffer_store_format(struct ac_llvm_context
*ctx
,
1233 LLVMValueRef vindex
,
1234 LLVMValueRef voffset
,
1235 unsigned num_channels
,
1236 unsigned cache_policy
)
1238 if (HAVE_LLVM
>= 0x800) {
1239 ac_build_llvm8_buffer_store_common(ctx
, rsrc
, data
, vindex
,
1240 voffset
, NULL
, num_channels
,
1241 ctx
->f32
, cache_policy
,
1244 ac_build_llvm7_buffer_store_common(ctx
, rsrc
, data
, vindex
, voffset
,
1245 num_channels
, cache_policy
,
1250 /* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
1251 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
1252 * or v4i32 (num_channels=3,4).
1255 ac_build_buffer_store_dword(struct ac_llvm_context
*ctx
,
1258 unsigned num_channels
,
1259 LLVMValueRef voffset
,
1260 LLVMValueRef soffset
,
1261 unsigned inst_offset
,
1262 unsigned cache_policy
,
1263 bool swizzle_enable_hint
)
1265 /* Split 3 channel stores, because only LLVM 9+ support 3-channel
1267 if (num_channels
== 3 && !ac_has_vec3_support(ctx
->chip_class
, false)) {
1268 LLVMValueRef v
[3], v01
;
1270 for (int i
= 0; i
< 3; i
++) {
1271 v
[i
] = LLVMBuildExtractElement(ctx
->builder
, vdata
,
1272 LLVMConstInt(ctx
->i32
, i
, 0), "");
1274 v01
= ac_build_gather_values(ctx
, v
, 2);
1276 ac_build_buffer_store_dword(ctx
, rsrc
, v01
, 2, voffset
,
1277 soffset
, inst_offset
, cache_policy
,
1278 swizzle_enable_hint
);
1279 ac_build_buffer_store_dword(ctx
, rsrc
, v
[2], 1, voffset
,
1280 soffset
, inst_offset
+ 8,
1282 swizzle_enable_hint
);
1286 /* SWIZZLE_ENABLE requires that soffset isn't folded into voffset
1287 * (voffset is swizzled, but soffset isn't swizzled).
1288 * llvm.amdgcn.buffer.store doesn't have a separate soffset parameter.
1290 if (!swizzle_enable_hint
) {
1291 LLVMValueRef offset
= soffset
;
1294 offset
= LLVMBuildAdd(ctx
->builder
, offset
,
1295 LLVMConstInt(ctx
->i32
, inst_offset
, 0), "");
1297 if (HAVE_LLVM
>= 0x800) {
1298 ac_build_llvm8_buffer_store_common(ctx
, rsrc
,
1299 ac_to_float(ctx
, vdata
),
1308 offset
= LLVMBuildAdd(ctx
->builder
, offset
, voffset
, "");
1310 ac_build_llvm7_buffer_store_common(ctx
, rsrc
,
1311 ac_to_float(ctx
, vdata
),
1313 num_channels
, cache_policy
,
1319 static const unsigned dfmts
[] = {
1320 V_008F0C_BUF_DATA_FORMAT_32
,
1321 V_008F0C_BUF_DATA_FORMAT_32_32
,
1322 V_008F0C_BUF_DATA_FORMAT_32_32_32
,
1323 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
1325 unsigned dfmt
= dfmts
[num_channels
- 1];
1326 unsigned nfmt
= V_008F0C_BUF_NUM_FORMAT_UINT
;
1327 LLVMValueRef immoffset
= LLVMConstInt(ctx
->i32
, inst_offset
, 0);
1329 ac_build_raw_tbuffer_store(ctx
, rsrc
, vdata
, voffset
, soffset
,
1330 immoffset
, num_channels
, dfmt
, nfmt
, cache_policy
);
1334 ac_build_llvm7_buffer_load_common(struct ac_llvm_context
*ctx
,
1336 LLVMValueRef vindex
,
1337 LLVMValueRef voffset
,
1338 unsigned num_channels
,
1339 unsigned cache_policy
,
1343 LLVMValueRef args
[] = {
1344 LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, ""),
1345 vindex
? vindex
: ctx
->i32_0
,
1347 LLVMConstInt(ctx
->i1
, !!(cache_policy
& ac_glc
), 0),
1348 LLVMConstInt(ctx
->i1
, !!(cache_policy
& ac_slc
), 0)
1350 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
1352 LLVMTypeRef types
[] = {ctx
->f32
, ctx
->v2f32
, ctx
->v4f32
};
1353 const char *type_names
[] = {"f32", "v2f32", "v4f32"};
1357 snprintf(name
, sizeof(name
), "llvm.amdgcn.buffer.load.format.%s",
1360 snprintf(name
, sizeof(name
), "llvm.amdgcn.buffer.load.%s",
1364 return ac_build_intrinsic(ctx
, name
, types
[func
], args
,
1366 ac_get_load_intr_attribs(can_speculate
));
1370 ac_build_llvm8_buffer_load_common(struct ac_llvm_context
*ctx
,
1372 LLVMValueRef vindex
,
1373 LLVMValueRef voffset
,
1374 LLVMValueRef soffset
,
1375 unsigned num_channels
,
1376 LLVMTypeRef channel_type
,
1377 unsigned cache_policy
,
1382 LLVMValueRef args
[5];
1384 args
[idx
++] = LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, "");
1386 args
[idx
++] = vindex
? vindex
: ctx
->i32_0
;
1387 args
[idx
++] = voffset
? voffset
: ctx
->i32_0
;
1388 args
[idx
++] = soffset
? soffset
: ctx
->i32_0
;
1389 args
[idx
++] = LLVMConstInt(ctx
->i32
, get_load_cache_policy(ctx
, cache_policy
), 0);
1390 unsigned func
= !ac_has_vec3_support(ctx
->chip_class
, use_format
) && num_channels
== 3 ? 4 : num_channels
;
1391 const char *indexing_kind
= structurized
? "struct" : "raw";
1392 char name
[256], type_name
[8];
1394 LLVMTypeRef type
= func
> 1 ? LLVMVectorType(channel_type
, func
) : channel_type
;
1395 ac_build_type_name_for_intr(type
, type_name
, sizeof(type_name
));
1398 snprintf(name
, sizeof(name
), "llvm.amdgcn.%s.buffer.load.format.%s",
1399 indexing_kind
, type_name
);
1401 snprintf(name
, sizeof(name
), "llvm.amdgcn.%s.buffer.load.%s",
1402 indexing_kind
, type_name
);
1405 return ac_build_intrinsic(ctx
, name
, type
, args
, idx
,
1406 ac_get_load_intr_attribs(can_speculate
));
1410 ac_build_buffer_load(struct ac_llvm_context
*ctx
,
1413 LLVMValueRef vindex
,
1414 LLVMValueRef voffset
,
1415 LLVMValueRef soffset
,
1416 unsigned inst_offset
,
1417 unsigned cache_policy
,
1421 LLVMValueRef offset
= LLVMConstInt(ctx
->i32
, inst_offset
, 0);
1423 offset
= LLVMBuildAdd(ctx
->builder
, offset
, voffset
, "");
1425 offset
= LLVMBuildAdd(ctx
->builder
, offset
, soffset
, "");
1427 if (allow_smem
&& !(cache_policy
& ac_slc
) &&
1428 (!(cache_policy
& ac_glc
) || (HAVE_LLVM
>= 0x0800 && ctx
->chip_class
>= GFX8
))) {
1429 assert(vindex
== NULL
);
1431 LLVMValueRef result
[8];
1433 for (int i
= 0; i
< num_channels
; i
++) {
1435 offset
= LLVMBuildAdd(ctx
->builder
, offset
,
1436 LLVMConstInt(ctx
->i32
, 4, 0), "");
1438 const char *intrname
=
1439 HAVE_LLVM
>= 0x0800 ? "llvm.amdgcn.s.buffer.load.f32"
1440 : "llvm.SI.load.const.v4i32";
1441 unsigned num_args
= HAVE_LLVM
>= 0x0800 ? 3 : 2;
1442 LLVMValueRef args
[3] = {
1445 LLVMConstInt(ctx
->i32
, get_load_cache_policy(ctx
, cache_policy
), 0),
1447 result
[i
] = ac_build_intrinsic(ctx
, intrname
,
1448 ctx
->f32
, args
, num_args
,
1449 AC_FUNC_ATTR_READNONE
|
1450 (HAVE_LLVM
< 0x0800 ? AC_FUNC_ATTR_LEGACY
: 0));
1452 if (num_channels
== 1)
1455 if (num_channels
== 3 && !ac_has_vec3_support(ctx
->chip_class
, false))
1456 result
[num_channels
++] = LLVMGetUndef(ctx
->f32
);
1457 return ac_build_gather_values(ctx
, result
, num_channels
);
1460 if (HAVE_LLVM
>= 0x0800) {
1461 return ac_build_llvm8_buffer_load_common(ctx
, rsrc
, vindex
,
1463 num_channels
, ctx
->f32
,
1465 can_speculate
, false,
1469 return ac_build_llvm7_buffer_load_common(ctx
, rsrc
, vindex
, offset
,
1470 num_channels
, cache_policy
,
1471 can_speculate
, false);
1474 LLVMValueRef
ac_build_buffer_load_format(struct ac_llvm_context
*ctx
,
1476 LLVMValueRef vindex
,
1477 LLVMValueRef voffset
,
1478 unsigned num_channels
,
1479 unsigned cache_policy
,
1482 if (HAVE_LLVM
>= 0x800) {
1483 return ac_build_llvm8_buffer_load_common(ctx
, rsrc
, vindex
, voffset
, ctx
->i32_0
,
1484 num_channels
, ctx
->f32
,
1485 cache_policy
, can_speculate
, true, true);
1487 return ac_build_llvm7_buffer_load_common(ctx
, rsrc
, vindex
, voffset
,
1488 num_channels
, cache_policy
,
1489 can_speculate
, true);
1492 LLVMValueRef
ac_build_buffer_load_format_gfx9_safe(struct ac_llvm_context
*ctx
,
1494 LLVMValueRef vindex
,
1495 LLVMValueRef voffset
,
1496 unsigned num_channels
,
1497 unsigned cache_policy
,
1500 if (HAVE_LLVM
>= 0x800) {
1501 return ac_build_llvm8_buffer_load_common(ctx
, rsrc
, vindex
, voffset
, ctx
->i32_0
,
1502 num_channels
, ctx
->f32
,
1503 cache_policy
, can_speculate
, true, true);
1506 LLVMValueRef elem_count
= LLVMBuildExtractElement(ctx
->builder
, rsrc
, LLVMConstInt(ctx
->i32
, 2, 0), "");
1507 LLVMValueRef stride
= LLVMBuildExtractElement(ctx
->builder
, rsrc
, ctx
->i32_1
, "");
1508 stride
= LLVMBuildLShr(ctx
->builder
, stride
, LLVMConstInt(ctx
->i32
, 16, 0), "");
1510 LLVMValueRef new_elem_count
= LLVMBuildSelect(ctx
->builder
,
1511 LLVMBuildICmp(ctx
->builder
, LLVMIntUGT
, elem_count
, stride
, ""),
1512 elem_count
, stride
, "");
1514 LLVMValueRef new_rsrc
= LLVMBuildInsertElement(ctx
->builder
, rsrc
, new_elem_count
,
1515 LLVMConstInt(ctx
->i32
, 2, 0), "");
1517 return ac_build_llvm7_buffer_load_common(ctx
, new_rsrc
, vindex
, voffset
,
1518 num_channels
, cache_policy
,
1519 can_speculate
, true);
1522 /// Translate a (dfmt, nfmt) pair into a chip-appropriate combined format
1523 /// value for LLVM8+ tbuffer intrinsics.
1525 ac_get_tbuffer_format(struct ac_llvm_context
*ctx
,
1526 unsigned dfmt
, unsigned nfmt
)
1528 if (ctx
->chip_class
>= GFX10
) {
1531 default: unreachable("bad dfmt");
1532 case V_008F0C_BUF_DATA_FORMAT_INVALID
: format
= V_008F0C_IMG_FORMAT_INVALID
; break;
1533 case V_008F0C_BUF_DATA_FORMAT_8
: format
= V_008F0C_IMG_FORMAT_8_UINT
; break;
1534 case V_008F0C_BUF_DATA_FORMAT_8_8
: format
= V_008F0C_IMG_FORMAT_8_8_UINT
; break;
1535 case V_008F0C_BUF_DATA_FORMAT_8_8_8_8
: format
= V_008F0C_IMG_FORMAT_8_8_8_8_UINT
; break;
1536 case V_008F0C_BUF_DATA_FORMAT_16
: format
= V_008F0C_IMG_FORMAT_16_UINT
; break;
1537 case V_008F0C_BUF_DATA_FORMAT_16_16
: format
= V_008F0C_IMG_FORMAT_16_16_UINT
; break;
1538 case V_008F0C_BUF_DATA_FORMAT_16_16_16_16
: format
= V_008F0C_IMG_FORMAT_16_16_16_16_UINT
; break;
1539 case V_008F0C_BUF_DATA_FORMAT_32
: format
= V_008F0C_IMG_FORMAT_32_UINT
; break;
1540 case V_008F0C_BUF_DATA_FORMAT_32_32
: format
= V_008F0C_IMG_FORMAT_32_32_UINT
; break;
1541 case V_008F0C_BUF_DATA_FORMAT_32_32_32
: format
= V_008F0C_IMG_FORMAT_32_32_32_UINT
; break;
1542 case V_008F0C_BUF_DATA_FORMAT_32_32_32_32
: format
= V_008F0C_IMG_FORMAT_32_32_32_32_UINT
; break;
1543 case V_008F0C_BUF_DATA_FORMAT_2_10_10_10
: format
= V_008F0C_IMG_FORMAT_2_10_10_10_UINT
; break;
1546 // Use the regularity properties of the combined format enum.
1548 // Note: float is incompatible with 8-bit data formats,
1549 // [us]{norm,scaled} are incomparible with 32-bit data formats.
1550 // [us]scaled are not writable.
1552 case V_008F0C_BUF_NUM_FORMAT_UNORM
: format
-= 4; break;
1553 case V_008F0C_BUF_NUM_FORMAT_SNORM
: format
-= 3; break;
1554 case V_008F0C_BUF_NUM_FORMAT_USCALED
: format
-= 2; break;
1555 case V_008F0C_BUF_NUM_FORMAT_SSCALED
: format
-= 1; break;
1556 default: unreachable("bad nfmt");
1557 case V_008F0C_BUF_NUM_FORMAT_UINT
: break;
1558 case V_008F0C_BUF_NUM_FORMAT_SINT
: format
+= 1; break;
1559 case V_008F0C_BUF_NUM_FORMAT_FLOAT
: format
+= 2; break;
1564 return dfmt
| (nfmt
<< 4);
1569 ac_build_llvm8_tbuffer_load(struct ac_llvm_context
*ctx
,
1571 LLVMValueRef vindex
,
1572 LLVMValueRef voffset
,
1573 LLVMValueRef soffset
,
1574 unsigned num_channels
,
1577 unsigned cache_policy
,
1581 LLVMValueRef args
[6];
1583 args
[idx
++] = LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, "");
1585 args
[idx
++] = vindex
? vindex
: ctx
->i32_0
;
1586 args
[idx
++] = voffset
? voffset
: ctx
->i32_0
;
1587 args
[idx
++] = soffset
? soffset
: ctx
->i32_0
;
1588 args
[idx
++] = LLVMConstInt(ctx
->i32
, ac_get_tbuffer_format(ctx
, dfmt
, nfmt
), 0);
1589 args
[idx
++] = LLVMConstInt(ctx
->i32
, get_load_cache_policy(ctx
, cache_policy
), 0);
1590 unsigned func
= !ac_has_vec3_support(ctx
->chip_class
, true) && num_channels
== 3 ? 4 : num_channels
;
1591 const char *indexing_kind
= structurized
? "struct" : "raw";
1592 char name
[256], type_name
[8];
1594 LLVMTypeRef type
= func
> 1 ? LLVMVectorType(ctx
->i32
, func
) : ctx
->i32
;
1595 ac_build_type_name_for_intr(type
, type_name
, sizeof(type_name
));
1597 snprintf(name
, sizeof(name
), "llvm.amdgcn.%s.tbuffer.load.%s",
1598 indexing_kind
, type_name
);
1600 return ac_build_intrinsic(ctx
, name
, type
, args
, idx
,
1601 ac_get_load_intr_attribs(can_speculate
));
1605 ac_build_tbuffer_load(struct ac_llvm_context
*ctx
,
1607 LLVMValueRef vindex
,
1608 LLVMValueRef voffset
,
1609 LLVMValueRef soffset
,
1610 LLVMValueRef immoffset
,
1611 unsigned num_channels
,
1614 unsigned cache_policy
,
1616 bool structurized
) /* only matters for LLVM 8+ */
1618 if (HAVE_LLVM
>= 0x800) {
1619 voffset
= LLVMBuildAdd(ctx
->builder
, voffset
, immoffset
, "");
1621 return ac_build_llvm8_tbuffer_load(ctx
, rsrc
, vindex
, voffset
,
1622 soffset
, num_channels
,
1623 dfmt
, nfmt
, cache_policy
,
1624 can_speculate
, structurized
);
1627 LLVMValueRef args
[] = {
1629 vindex
? vindex
: ctx
->i32_0
,
1633 LLVMConstInt(ctx
->i32
, dfmt
, false),
1634 LLVMConstInt(ctx
->i32
, nfmt
, false),
1635 LLVMConstInt(ctx
->i1
, !!(cache_policy
& ac_glc
), false),
1636 LLVMConstInt(ctx
->i1
, !!(cache_policy
& ac_slc
), false),
1638 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
1639 LLVMTypeRef types
[] = {ctx
->i32
, ctx
->v2i32
, ctx
->v4i32
};
1640 const char *type_names
[] = {"i32", "v2i32", "v4i32"};
1643 snprintf(name
, sizeof(name
), "llvm.amdgcn.tbuffer.load.%s",
1646 return ac_build_intrinsic(ctx
, name
, types
[func
], args
, 9,
1647 ac_get_load_intr_attribs(can_speculate
));
1651 ac_build_struct_tbuffer_load(struct ac_llvm_context
*ctx
,
1653 LLVMValueRef vindex
,
1654 LLVMValueRef voffset
,
1655 LLVMValueRef soffset
,
1656 LLVMValueRef immoffset
,
1657 unsigned num_channels
,
1660 unsigned cache_policy
,
1663 return ac_build_tbuffer_load(ctx
, rsrc
, vindex
, voffset
, soffset
,
1664 immoffset
, num_channels
, dfmt
, nfmt
,
1665 cache_policy
, can_speculate
, true);
1669 ac_build_raw_tbuffer_load(struct ac_llvm_context
*ctx
,
1671 LLVMValueRef voffset
,
1672 LLVMValueRef soffset
,
1673 LLVMValueRef immoffset
,
1674 unsigned num_channels
,
1677 unsigned cache_policy
,
1680 return ac_build_tbuffer_load(ctx
, rsrc
, NULL
, voffset
, soffset
,
1681 immoffset
, num_channels
, dfmt
, nfmt
,
1682 cache_policy
, can_speculate
, false);
1686 ac_build_tbuffer_load_short(struct ac_llvm_context
*ctx
,
1688 LLVMValueRef voffset
,
1689 LLVMValueRef soffset
,
1690 LLVMValueRef immoffset
,
1691 unsigned cache_policy
)
1695 if (HAVE_LLVM
>= 0x900) {
1696 voffset
= LLVMBuildAdd(ctx
->builder
, voffset
, immoffset
, "");
1698 /* LLVM 9+ supports i8/i16 with struct/raw intrinsics. */
1699 res
= ac_build_llvm8_buffer_load_common(ctx
, rsrc
, NULL
,
1701 1, ctx
->i16
, cache_policy
,
1702 false, false, false);
1704 unsigned dfmt
= V_008F0C_BUF_DATA_FORMAT_16
;
1705 unsigned nfmt
= V_008F0C_BUF_NUM_FORMAT_UINT
;
1707 res
= ac_build_raw_tbuffer_load(ctx
, rsrc
, voffset
, soffset
,
1708 immoffset
, 1, dfmt
, nfmt
, cache_policy
,
1711 res
= LLVMBuildTrunc(ctx
->builder
, res
, ctx
->i16
, "");
1718 ac_build_tbuffer_load_byte(struct ac_llvm_context
*ctx
,
1720 LLVMValueRef voffset
,
1721 LLVMValueRef soffset
,
1722 LLVMValueRef immoffset
,
1723 unsigned cache_policy
)
1727 if (HAVE_LLVM
>= 0x900) {
1728 voffset
= LLVMBuildAdd(ctx
->builder
, voffset
, immoffset
, "");
1730 /* LLVM 9+ supports i8/i16 with struct/raw intrinsics. */
1731 res
= ac_build_llvm8_buffer_load_common(ctx
, rsrc
, NULL
,
1733 1, ctx
->i8
, cache_policy
,
1734 false, false, false);
1736 unsigned dfmt
= V_008F0C_BUF_DATA_FORMAT_8
;
1737 unsigned nfmt
= V_008F0C_BUF_NUM_FORMAT_UINT
;
1739 res
= ac_build_raw_tbuffer_load(ctx
, rsrc
, voffset
, soffset
,
1740 immoffset
, 1, dfmt
, nfmt
, cache_policy
,
1743 res
= LLVMBuildTrunc(ctx
->builder
, res
, ctx
->i8
, "");
1750 * Convert an 11- or 10-bit unsigned floating point number to an f32.
1752 * The input exponent is expected to be biased analogous to IEEE-754, i.e. by
1753 * 2^(exp_bits-1) - 1 (as defined in OpenGL and other graphics APIs).
1756 ac_ufN_to_float(struct ac_llvm_context
*ctx
, LLVMValueRef src
, unsigned exp_bits
, unsigned mant_bits
)
1758 assert(LLVMTypeOf(src
) == ctx
->i32
);
1761 LLVMValueRef mantissa
;
1762 mantissa
= LLVMBuildAnd(ctx
->builder
, src
, LLVMConstInt(ctx
->i32
, (1 << mant_bits
) - 1, false), "");
1764 /* Converting normal numbers is just a shift + correcting the exponent bias */
1765 unsigned normal_shift
= 23 - mant_bits
;
1766 unsigned bias_shift
= 127 - ((1 << (exp_bits
- 1)) - 1);
1767 LLVMValueRef shifted
, normal
;
1769 shifted
= LLVMBuildShl(ctx
->builder
, src
, LLVMConstInt(ctx
->i32
, normal_shift
, false), "");
1770 normal
= LLVMBuildAdd(ctx
->builder
, shifted
, LLVMConstInt(ctx
->i32
, bias_shift
<< 23, false), "");
1772 /* Converting nan/inf numbers is the same, but with a different exponent update */
1773 LLVMValueRef naninf
;
1774 naninf
= LLVMBuildOr(ctx
->builder
, normal
, LLVMConstInt(ctx
->i32
, 0xff << 23, false), "");
1776 /* Converting denormals is the complex case: determine the leading zeros of the
1777 * mantissa to obtain the correct shift for the mantissa and exponent correction.
1779 LLVMValueRef denormal
;
1780 LLVMValueRef params
[2] = {
1782 ctx
->i1true
, /* result can be undef when arg is 0 */
1784 LLVMValueRef ctlz
= ac_build_intrinsic(ctx
, "llvm.ctlz.i32", ctx
->i32
,
1785 params
, 2, AC_FUNC_ATTR_READNONE
);
1787 /* Shift such that the leading 1 ends up as the LSB of the exponent field. */
1788 tmp
= LLVMBuildSub(ctx
->builder
, ctlz
, LLVMConstInt(ctx
->i32
, 8, false), "");
1789 denormal
= LLVMBuildShl(ctx
->builder
, mantissa
, tmp
, "");
1791 unsigned denormal_exp
= bias_shift
+ (32 - mant_bits
) - 1;
1792 tmp
= LLVMBuildSub(ctx
->builder
, LLVMConstInt(ctx
->i32
, denormal_exp
, false), ctlz
, "");
1793 tmp
= LLVMBuildShl(ctx
->builder
, tmp
, LLVMConstInt(ctx
->i32
, 23, false), "");
1794 denormal
= LLVMBuildAdd(ctx
->builder
, denormal
, tmp
, "");
1796 /* Select the final result. */
1797 LLVMValueRef result
;
1799 tmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntUGE
, src
,
1800 LLVMConstInt(ctx
->i32
, ((1 << exp_bits
) - 1) << mant_bits
, false), "");
1801 result
= LLVMBuildSelect(ctx
->builder
, tmp
, naninf
, normal
, "");
1803 tmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntUGE
, src
,
1804 LLVMConstInt(ctx
->i32
, 1 << mant_bits
, false), "");
1805 result
= LLVMBuildSelect(ctx
->builder
, tmp
, result
, denormal
, "");
1807 tmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src
, ctx
->i32_0
, "");
1808 result
= LLVMBuildSelect(ctx
->builder
, tmp
, result
, ctx
->i32_0
, "");
1810 return ac_to_float(ctx
, result
);
1814 * Generate a fully general open coded buffer format fetch with all required
1815 * fixups suitable for vertex fetch, using non-format buffer loads.
1817 * Some combinations of argument values have special interpretations:
1818 * - size = 8 bytes, format = fixed indicates PIPE_FORMAT_R11G11B10_FLOAT
1819 * - size = 8 bytes, format != {float,fixed} indicates a 2_10_10_10 data format
1821 * \param log_size log(size of channel in bytes)
1822 * \param num_channels number of channels (1 to 4)
1823 * \param format AC_FETCH_FORMAT_xxx value
1824 * \param reverse whether XYZ channels are reversed
1825 * \param known_aligned whether the source is known to be aligned to hardware's
1826 * effective element size for loading the given format
1827 * (note: this means dword alignment for 8_8_8_8, 16_16, etc.)
1828 * \param rsrc buffer resource descriptor
1829 * \return the resulting vector of floats or integers bitcast to <4 x i32>
1832 ac_build_opencoded_load_format(struct ac_llvm_context
*ctx
,
1834 unsigned num_channels
,
1839 LLVMValueRef vindex
,
1840 LLVMValueRef voffset
,
1841 LLVMValueRef soffset
,
1842 unsigned cache_policy
,
1846 unsigned load_log_size
= log_size
;
1847 unsigned load_num_channels
= num_channels
;
1848 if (log_size
== 3) {
1850 if (format
== AC_FETCH_FORMAT_FLOAT
) {
1851 load_num_channels
= 2 * num_channels
;
1853 load_num_channels
= 1; /* 10_11_11 or 2_10_10_10 */
1857 int log_recombine
= 0;
1858 if (ctx
->chip_class
== GFX6
&& !known_aligned
) {
1859 /* Avoid alignment restrictions by loading one byte at a time. */
1860 load_num_channels
<<= load_log_size
;
1861 log_recombine
= load_log_size
;
1863 } else if (load_num_channels
== 2 || load_num_channels
== 4) {
1864 log_recombine
= -util_logbase2(load_num_channels
);
1865 load_num_channels
= 1;
1866 load_log_size
+= -log_recombine
;
1869 assert(load_log_size
>= 2 || HAVE_LLVM
>= 0x0900);
1871 LLVMValueRef loads
[32]; /* up to 32 bytes */
1872 for (unsigned i
= 0; i
< load_num_channels
; ++i
) {
1873 tmp
= LLVMBuildAdd(ctx
->builder
, soffset
,
1874 LLVMConstInt(ctx
->i32
, i
<< load_log_size
, false), "");
1875 if (HAVE_LLVM
>= 0x0800) {
1876 LLVMTypeRef channel_type
= load_log_size
== 0 ? ctx
->i8
:
1877 load_log_size
== 1 ? ctx
->i16
: ctx
->i32
;
1878 unsigned num_channels
= 1 << (MAX2(load_log_size
, 2) - 2);
1879 loads
[i
] = ac_build_llvm8_buffer_load_common(
1880 ctx
, rsrc
, vindex
, voffset
, tmp
,
1881 num_channels
, channel_type
, cache_policy
,
1882 can_speculate
, false, true);
1884 tmp
= LLVMBuildAdd(ctx
->builder
, voffset
, tmp
, "");
1885 loads
[i
] = ac_build_llvm7_buffer_load_common(
1886 ctx
, rsrc
, vindex
, tmp
,
1887 1 << (load_log_size
- 2), cache_policy
, can_speculate
, false);
1889 if (load_log_size
>= 2)
1890 loads
[i
] = ac_to_integer(ctx
, loads
[i
]);
1893 if (log_recombine
> 0) {
1894 /* Recombine bytes if necessary (GFX6 only) */
1895 LLVMTypeRef dst_type
= log_recombine
== 2 ? ctx
->i32
: ctx
->i16
;
1897 for (unsigned src
= 0, dst
= 0; src
< load_num_channels
; ++dst
) {
1898 LLVMValueRef accum
= NULL
;
1899 for (unsigned i
= 0; i
< (1 << log_recombine
); ++i
, ++src
) {
1900 tmp
= LLVMBuildZExt(ctx
->builder
, loads
[src
], dst_type
, "");
1904 tmp
= LLVMBuildShl(ctx
->builder
, tmp
,
1905 LLVMConstInt(dst_type
, 8 * i
, false), "");
1906 accum
= LLVMBuildOr(ctx
->builder
, accum
, tmp
, "");
1911 } else if (log_recombine
< 0) {
1912 /* Split vectors of dwords */
1913 if (load_log_size
> 2) {
1914 assert(load_num_channels
== 1);
1915 LLVMValueRef loaded
= loads
[0];
1916 unsigned log_split
= load_log_size
- 2;
1917 log_recombine
+= log_split
;
1918 load_num_channels
= 1 << log_split
;
1920 for (unsigned i
= 0; i
< load_num_channels
; ++i
) {
1921 tmp
= LLVMConstInt(ctx
->i32
, i
, false);
1922 loads
[i
] = LLVMBuildExtractElement(ctx
->builder
, loaded
, tmp
, "");
1926 /* Further split dwords and shorts if required */
1927 if (log_recombine
< 0) {
1928 for (unsigned src
= load_num_channels
,
1929 dst
= load_num_channels
<< -log_recombine
;
1931 unsigned dst_bits
= 1 << (3 + load_log_size
+ log_recombine
);
1932 LLVMTypeRef dst_type
= LLVMIntTypeInContext(ctx
->context
, dst_bits
);
1933 LLVMValueRef loaded
= loads
[src
- 1];
1934 LLVMTypeRef loaded_type
= LLVMTypeOf(loaded
);
1935 for (unsigned i
= 1 << -log_recombine
; i
> 0; --i
, --dst
) {
1936 tmp
= LLVMConstInt(loaded_type
, dst_bits
* (i
- 1), false);
1937 tmp
= LLVMBuildLShr(ctx
->builder
, loaded
, tmp
, "");
1938 loads
[dst
- 1] = LLVMBuildTrunc(ctx
->builder
, tmp
, dst_type
, "");
1944 if (log_size
== 3) {
1945 if (format
== AC_FETCH_FORMAT_FLOAT
) {
1946 for (unsigned i
= 0; i
< num_channels
; ++i
) {
1947 tmp
= ac_build_gather_values(ctx
, &loads
[2 * i
], 2);
1948 loads
[i
] = LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f64
, "");
1950 } else if (format
== AC_FETCH_FORMAT_FIXED
) {
1951 /* 10_11_11_FLOAT */
1952 LLVMValueRef data
= loads
[0];
1953 LLVMValueRef i32_2047
= LLVMConstInt(ctx
->i32
, 2047, false);
1954 LLVMValueRef r
= LLVMBuildAnd(ctx
->builder
, data
, i32_2047
, "");
1955 tmp
= LLVMBuildLShr(ctx
->builder
, data
, LLVMConstInt(ctx
->i32
, 11, false), "");
1956 LLVMValueRef g
= LLVMBuildAnd(ctx
->builder
, tmp
, i32_2047
, "");
1957 LLVMValueRef b
= LLVMBuildLShr(ctx
->builder
, data
, LLVMConstInt(ctx
->i32
, 22, false), "");
1959 loads
[0] = ac_to_integer(ctx
, ac_ufN_to_float(ctx
, r
, 5, 6));
1960 loads
[1] = ac_to_integer(ctx
, ac_ufN_to_float(ctx
, g
, 5, 6));
1961 loads
[2] = ac_to_integer(ctx
, ac_ufN_to_float(ctx
, b
, 5, 5));
1965 format
= AC_FETCH_FORMAT_FLOAT
;
1967 /* 2_10_10_10 data formats */
1968 LLVMValueRef data
= loads
[0];
1969 LLVMTypeRef i10
= LLVMIntTypeInContext(ctx
->context
, 10);
1970 LLVMTypeRef i2
= LLVMIntTypeInContext(ctx
->context
, 2);
1971 loads
[0] = LLVMBuildTrunc(ctx
->builder
, data
, i10
, "");
1972 tmp
= LLVMBuildLShr(ctx
->builder
, data
, LLVMConstInt(ctx
->i32
, 10, false), "");
1973 loads
[1] = LLVMBuildTrunc(ctx
->builder
, tmp
, i10
, "");
1974 tmp
= LLVMBuildLShr(ctx
->builder
, data
, LLVMConstInt(ctx
->i32
, 20, false), "");
1975 loads
[2] = LLVMBuildTrunc(ctx
->builder
, tmp
, i10
, "");
1976 tmp
= LLVMBuildLShr(ctx
->builder
, data
, LLVMConstInt(ctx
->i32
, 30, false), "");
1977 loads
[3] = LLVMBuildTrunc(ctx
->builder
, tmp
, i2
, "");
1983 if (format
== AC_FETCH_FORMAT_FLOAT
) {
1984 if (log_size
!= 2) {
1985 for (unsigned chan
= 0; chan
< num_channels
; ++chan
) {
1986 tmp
= ac_to_float(ctx
, loads
[chan
]);
1988 tmp
= LLVMBuildFPTrunc(ctx
->builder
, tmp
, ctx
->f32
, "");
1989 else if (log_size
== 1)
1990 tmp
= LLVMBuildFPExt(ctx
->builder
, tmp
, ctx
->f32
, "");
1991 loads
[chan
] = ac_to_integer(ctx
, tmp
);
1994 } else if (format
== AC_FETCH_FORMAT_UINT
) {
1995 if (log_size
!= 2) {
1996 for (unsigned chan
= 0; chan
< num_channels
; ++chan
)
1997 loads
[chan
] = LLVMBuildZExt(ctx
->builder
, loads
[chan
], ctx
->i32
, "");
1999 } else if (format
== AC_FETCH_FORMAT_SINT
) {
2000 if (log_size
!= 2) {
2001 for (unsigned chan
= 0; chan
< num_channels
; ++chan
)
2002 loads
[chan
] = LLVMBuildSExt(ctx
->builder
, loads
[chan
], ctx
->i32
, "");
2005 bool unsign
= format
== AC_FETCH_FORMAT_UNORM
||
2006 format
== AC_FETCH_FORMAT_USCALED
||
2007 format
== AC_FETCH_FORMAT_UINT
;
2009 for (unsigned chan
= 0; chan
< num_channels
; ++chan
) {
2011 tmp
= LLVMBuildUIToFP(ctx
->builder
, loads
[chan
], ctx
->f32
, "");
2013 tmp
= LLVMBuildSIToFP(ctx
->builder
, loads
[chan
], ctx
->f32
, "");
2016 LLVMValueRef scale
= NULL
;
2017 if (format
== AC_FETCH_FORMAT_FIXED
) {
2018 assert(log_size
== 2);
2019 scale
= LLVMConstReal(ctx
->f32
, 1.0 / 0x10000);
2020 } else if (format
== AC_FETCH_FORMAT_UNORM
) {
2021 unsigned bits
= LLVMGetIntTypeWidth(LLVMTypeOf(loads
[chan
]));
2022 scale
= LLVMConstReal(ctx
->f32
, 1.0 / (((uint64_t)1 << bits
) - 1));
2023 } else if (format
== AC_FETCH_FORMAT_SNORM
) {
2024 unsigned bits
= LLVMGetIntTypeWidth(LLVMTypeOf(loads
[chan
]));
2025 scale
= LLVMConstReal(ctx
->f32
, 1.0 / (((uint64_t)1 << (bits
- 1)) - 1));
2028 tmp
= LLVMBuildFMul(ctx
->builder
, tmp
, scale
, "");
2030 if (format
== AC_FETCH_FORMAT_SNORM
) {
2031 /* Clamp to [-1, 1] */
2032 LLVMValueRef neg_one
= LLVMConstReal(ctx
->f32
, -1.0);
2033 LLVMValueRef clamp
=
2034 LLVMBuildFCmp(ctx
->builder
, LLVMRealULT
, tmp
, neg_one
, "");
2035 tmp
= LLVMBuildSelect(ctx
->builder
, clamp
, neg_one
, tmp
, "");
2038 loads
[chan
] = ac_to_integer(ctx
, tmp
);
2042 while (num_channels
< 4) {
2043 if (format
== AC_FETCH_FORMAT_UINT
|| format
== AC_FETCH_FORMAT_SINT
) {
2044 loads
[num_channels
] = num_channels
== 3 ? ctx
->i32_1
: ctx
->i32_0
;
2046 loads
[num_channels
] = ac_to_integer(ctx
, num_channels
== 3 ? ctx
->f32_1
: ctx
->f32_0
);
2053 loads
[0] = loads
[2];
2057 return ac_build_gather_values(ctx
, loads
, 4);
2061 ac_build_llvm8_tbuffer_store(struct ac_llvm_context
*ctx
,
2064 LLVMValueRef vindex
,
2065 LLVMValueRef voffset
,
2066 LLVMValueRef soffset
,
2067 unsigned num_channels
,
2070 unsigned cache_policy
,
2073 LLVMValueRef args
[7];
2075 args
[idx
++] = vdata
;
2076 args
[idx
++] = LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, "");
2078 args
[idx
++] = vindex
? vindex
: ctx
->i32_0
;
2079 args
[idx
++] = voffset
? voffset
: ctx
->i32_0
;
2080 args
[idx
++] = soffset
? soffset
: ctx
->i32_0
;
2081 args
[idx
++] = LLVMConstInt(ctx
->i32
, ac_get_tbuffer_format(ctx
, dfmt
, nfmt
), 0);
2082 args
[idx
++] = LLVMConstInt(ctx
->i32
, cache_policy
, 0);
2083 unsigned func
= !ac_has_vec3_support(ctx
->chip_class
, true) && num_channels
== 3 ? 4 : num_channels
;
2084 const char *indexing_kind
= structurized
? "struct" : "raw";
2085 char name
[256], type_name
[8];
2087 LLVMTypeRef type
= func
> 1 ? LLVMVectorType(ctx
->i32
, func
) : ctx
->i32
;
2088 ac_build_type_name_for_intr(type
, type_name
, sizeof(type_name
));
2090 snprintf(name
, sizeof(name
), "llvm.amdgcn.%s.tbuffer.store.%s",
2091 indexing_kind
, type_name
);
2093 ac_build_intrinsic(ctx
, name
, ctx
->voidt
, args
, idx
,
2094 AC_FUNC_ATTR_INACCESSIBLE_MEM_ONLY
);
2098 ac_build_tbuffer_store(struct ac_llvm_context
*ctx
,
2101 LLVMValueRef vindex
,
2102 LLVMValueRef voffset
,
2103 LLVMValueRef soffset
,
2104 LLVMValueRef immoffset
,
2105 unsigned num_channels
,
2108 unsigned cache_policy
,
2109 bool structurized
) /* only matters for LLVM 8+ */
2111 if (HAVE_LLVM
>= 0x800) {
2112 voffset
= LLVMBuildAdd(ctx
->builder
,
2113 voffset
? voffset
: ctx
->i32_0
,
2116 ac_build_llvm8_tbuffer_store(ctx
, rsrc
, vdata
, vindex
, voffset
,
2117 soffset
, num_channels
, dfmt
, nfmt
,
2118 cache_policy
, structurized
);
2120 LLVMValueRef params
[] = {
2123 vindex
? vindex
: ctx
->i32_0
,
2124 voffset
? voffset
: ctx
->i32_0
,
2125 soffset
? soffset
: ctx
->i32_0
,
2127 LLVMConstInt(ctx
->i32
, dfmt
, false),
2128 LLVMConstInt(ctx
->i32
, nfmt
, false),
2129 LLVMConstInt(ctx
->i1
, !!(cache_policy
& ac_glc
), false),
2130 LLVMConstInt(ctx
->i1
, !!(cache_policy
& ac_slc
), false),
2132 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
2133 const char *type_names
[] = {"i32", "v2i32", "v4i32"};
2136 snprintf(name
, sizeof(name
), "llvm.amdgcn.tbuffer.store.%s",
2139 ac_build_intrinsic(ctx
, name
, ctx
->voidt
, params
, 10,
2140 AC_FUNC_ATTR_INACCESSIBLE_MEM_ONLY
);
2145 ac_build_struct_tbuffer_store(struct ac_llvm_context
*ctx
,
2148 LLVMValueRef vindex
,
2149 LLVMValueRef voffset
,
2150 LLVMValueRef soffset
,
2151 LLVMValueRef immoffset
,
2152 unsigned num_channels
,
2155 unsigned cache_policy
)
2157 ac_build_tbuffer_store(ctx
, rsrc
, vdata
, vindex
, voffset
, soffset
,
2158 immoffset
, num_channels
, dfmt
, nfmt
, cache_policy
,
2163 ac_build_raw_tbuffer_store(struct ac_llvm_context
*ctx
,
2166 LLVMValueRef voffset
,
2167 LLVMValueRef soffset
,
2168 LLVMValueRef immoffset
,
2169 unsigned num_channels
,
2172 unsigned cache_policy
)
2174 ac_build_tbuffer_store(ctx
, rsrc
, vdata
, NULL
, voffset
, soffset
,
2175 immoffset
, num_channels
, dfmt
, nfmt
, cache_policy
,
2180 ac_build_tbuffer_store_short(struct ac_llvm_context
*ctx
,
2183 LLVMValueRef voffset
,
2184 LLVMValueRef soffset
,
2185 unsigned cache_policy
)
2187 vdata
= LLVMBuildBitCast(ctx
->builder
, vdata
, ctx
->i16
, "");
2189 if (HAVE_LLVM
>= 0x900) {
2190 /* LLVM 9+ supports i8/i16 with struct/raw intrinsics. */
2191 ac_build_llvm8_buffer_store_common(ctx
, rsrc
, vdata
, NULL
,
2192 voffset
, soffset
, 1,
2193 ctx
->i16
, cache_policy
,
2196 unsigned dfmt
= V_008F0C_BUF_DATA_FORMAT_16
;
2197 unsigned nfmt
= V_008F0C_BUF_NUM_FORMAT_UINT
;
2199 vdata
= LLVMBuildZExt(ctx
->builder
, vdata
, ctx
->i32
, "");
2201 ac_build_raw_tbuffer_store(ctx
, rsrc
, vdata
, voffset
, soffset
,
2202 ctx
->i32_0
, 1, dfmt
, nfmt
, cache_policy
);
2207 ac_build_tbuffer_store_byte(struct ac_llvm_context
*ctx
,
2210 LLVMValueRef voffset
,
2211 LLVMValueRef soffset
,
2212 unsigned cache_policy
)
2214 vdata
= LLVMBuildBitCast(ctx
->builder
, vdata
, ctx
->i8
, "");
2216 if (HAVE_LLVM
>= 0x900) {
2217 /* LLVM 9+ supports i8/i16 with struct/raw intrinsics. */
2218 ac_build_llvm8_buffer_store_common(ctx
, rsrc
, vdata
, NULL
,
2219 voffset
, soffset
, 1,
2220 ctx
->i8
, cache_policy
,
2223 unsigned dfmt
= V_008F0C_BUF_DATA_FORMAT_8
;
2224 unsigned nfmt
= V_008F0C_BUF_NUM_FORMAT_UINT
;
2226 vdata
= LLVMBuildZExt(ctx
->builder
, vdata
, ctx
->i32
, "");
2228 ac_build_raw_tbuffer_store(ctx
, rsrc
, vdata
, voffset
, soffset
,
2229 ctx
->i32_0
, 1, dfmt
, nfmt
, cache_policy
);
2233 * Set range metadata on an instruction. This can only be used on load and
2234 * call instructions. If you know an instruction can only produce the values
2235 * 0, 1, 2, you would do set_range_metadata(value, 0, 3);
2236 * \p lo is the minimum value inclusive.
2237 * \p hi is the maximum value exclusive.
2239 static void set_range_metadata(struct ac_llvm_context
*ctx
,
2240 LLVMValueRef value
, unsigned lo
, unsigned hi
)
2242 LLVMValueRef range_md
, md_args
[2];
2243 LLVMTypeRef type
= LLVMTypeOf(value
);
2244 LLVMContextRef context
= LLVMGetTypeContext(type
);
2246 md_args
[0] = LLVMConstInt(type
, lo
, false);
2247 md_args
[1] = LLVMConstInt(type
, hi
, false);
2248 range_md
= LLVMMDNodeInContext(context
, md_args
, 2);
2249 LLVMSetMetadata(value
, ctx
->range_md_kind
, range_md
);
2253 ac_get_thread_id(struct ac_llvm_context
*ctx
)
2257 LLVMValueRef tid_args
[2];
2258 tid_args
[0] = LLVMConstInt(ctx
->i32
, 0xffffffff, false);
2259 tid_args
[1] = ctx
->i32_0
;
2260 tid_args
[1] = ac_build_intrinsic(ctx
,
2261 "llvm.amdgcn.mbcnt.lo", ctx
->i32
,
2262 tid_args
, 2, AC_FUNC_ATTR_READNONE
);
2264 if (ctx
->wave_size
== 32) {
2267 tid
= ac_build_intrinsic(ctx
, "llvm.amdgcn.mbcnt.hi",
2269 2, AC_FUNC_ATTR_READNONE
);
2271 set_range_metadata(ctx
, tid
, 0, ctx
->wave_size
);
2276 * AMD GCN implements derivatives using the local data store (LDS)
2277 * All writes to the LDS happen in all executing threads at
2278 * the same time. TID is the Thread ID for the current
2279 * thread and is a value between 0 and 63, representing
2280 * the thread's position in the wavefront.
2282 * For the pixel shader threads are grouped into quads of four pixels.
2283 * The TIDs of the pixels of a quad are:
2291 * So, masking the TID with 0xfffffffc yields the TID of the top left pixel
2292 * of the quad, masking with 0xfffffffd yields the TID of the top pixel of
2293 * the current pixel's column, and masking with 0xfffffffe yields the TID
2294 * of the left pixel of the current pixel's row.
2296 * Adding 1 yields the TID of the pixel to the right of the left pixel, and
2297 * adding 2 yields the TID of the pixel below the top pixel.
2300 ac_build_ddxy(struct ac_llvm_context
*ctx
,
2305 unsigned tl_lanes
[4], trbl_lanes
[4];
2306 char name
[32], type
[8];
2307 LLVMValueRef tl
, trbl
;
2308 LLVMTypeRef result_type
;
2309 LLVMValueRef result
;
2311 result_type
= ac_to_float_type(ctx
, LLVMTypeOf(val
));
2313 if (result_type
== ctx
->f16
)
2314 val
= LLVMBuildZExt(ctx
->builder
, val
, ctx
->i32
, "");
2316 for (unsigned i
= 0; i
< 4; ++i
) {
2317 tl_lanes
[i
] = i
& mask
;
2318 trbl_lanes
[i
] = (i
& mask
) + idx
;
2321 tl
= ac_build_quad_swizzle(ctx
, val
,
2322 tl_lanes
[0], tl_lanes
[1],
2323 tl_lanes
[2], tl_lanes
[3]);
2324 trbl
= ac_build_quad_swizzle(ctx
, val
,
2325 trbl_lanes
[0], trbl_lanes
[1],
2326 trbl_lanes
[2], trbl_lanes
[3]);
2328 if (result_type
== ctx
->f16
) {
2329 tl
= LLVMBuildTrunc(ctx
->builder
, tl
, ctx
->i16
, "");
2330 trbl
= LLVMBuildTrunc(ctx
->builder
, trbl
, ctx
->i16
, "");
2333 tl
= LLVMBuildBitCast(ctx
->builder
, tl
, result_type
, "");
2334 trbl
= LLVMBuildBitCast(ctx
->builder
, trbl
, result_type
, "");
2335 result
= LLVMBuildFSub(ctx
->builder
, trbl
, tl
, "");
2337 ac_build_type_name_for_intr(result_type
, type
, sizeof(type
));
2338 snprintf(name
, sizeof(name
), "llvm.amdgcn.wqm.%s", type
);
2340 return ac_build_intrinsic(ctx
, name
, result_type
, &result
, 1, 0);
2344 ac_build_sendmsg(struct ac_llvm_context
*ctx
,
2346 LLVMValueRef wave_id
)
2348 LLVMValueRef args
[2];
2349 args
[0] = LLVMConstInt(ctx
->i32
, msg
, false);
2351 ac_build_intrinsic(ctx
, "llvm.amdgcn.s.sendmsg", ctx
->voidt
, args
, 2, 0);
2355 ac_build_imsb(struct ac_llvm_context
*ctx
,
2357 LLVMTypeRef dst_type
)
2359 LLVMValueRef msb
= ac_build_intrinsic(ctx
, "llvm.amdgcn.sffbh.i32",
2361 AC_FUNC_ATTR_READNONE
);
2363 /* The HW returns the last bit index from MSB, but NIR/TGSI wants
2364 * the index from LSB. Invert it by doing "31 - msb". */
2365 msb
= LLVMBuildSub(ctx
->builder
, LLVMConstInt(ctx
->i32
, 31, false),
2368 LLVMValueRef all_ones
= LLVMConstInt(ctx
->i32
, -1, true);
2369 LLVMValueRef cond
= LLVMBuildOr(ctx
->builder
,
2370 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
2371 arg
, ctx
->i32_0
, ""),
2372 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
2373 arg
, all_ones
, ""), "");
2375 return LLVMBuildSelect(ctx
->builder
, cond
, all_ones
, msb
, "");
2379 ac_build_umsb(struct ac_llvm_context
*ctx
,
2381 LLVMTypeRef dst_type
)
2383 const char *intrin_name
;
2385 LLVMValueRef highest_bit
;
2389 bitsize
= ac_get_elem_bits(ctx
, LLVMTypeOf(arg
));
2392 intrin_name
= "llvm.ctlz.i64";
2394 highest_bit
= LLVMConstInt(ctx
->i64
, 63, false);
2398 intrin_name
= "llvm.ctlz.i32";
2400 highest_bit
= LLVMConstInt(ctx
->i32
, 31, false);
2404 intrin_name
= "llvm.ctlz.i16";
2406 highest_bit
= LLVMConstInt(ctx
->i16
, 15, false);
2410 intrin_name
= "llvm.ctlz.i8";
2412 highest_bit
= LLVMConstInt(ctx
->i8
, 7, false);
2416 unreachable(!"invalid bitsize");
2420 LLVMValueRef params
[2] = {
2425 LLVMValueRef msb
= ac_build_intrinsic(ctx
, intrin_name
, type
,
2427 AC_FUNC_ATTR_READNONE
);
2429 /* The HW returns the last bit index from MSB, but TGSI/NIR wants
2430 * the index from LSB. Invert it by doing "31 - msb". */
2431 msb
= LLVMBuildSub(ctx
->builder
, highest_bit
, msb
, "");
2433 if (bitsize
== 64) {
2434 msb
= LLVMBuildTrunc(ctx
->builder
, msb
, ctx
->i32
, "");
2435 } else if (bitsize
< 32) {
2436 msb
= LLVMBuildSExt(ctx
->builder
, msb
, ctx
->i32
, "");
2439 /* check for zero */
2440 return LLVMBuildSelect(ctx
->builder
,
2441 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, arg
, zero
, ""),
2442 LLVMConstInt(ctx
->i32
, -1, true), msb
, "");
2445 LLVMValueRef
ac_build_fmin(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
2449 snprintf(name
, sizeof(name
), "llvm.minnum.f%d", ac_get_elem_bits(ctx
, LLVMTypeOf(a
)));
2450 LLVMValueRef args
[2] = {a
, b
};
2451 return ac_build_intrinsic(ctx
, name
, LLVMTypeOf(a
), args
, 2,
2452 AC_FUNC_ATTR_READNONE
);
2455 LLVMValueRef
ac_build_fmax(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
2459 snprintf(name
, sizeof(name
), "llvm.maxnum.f%d", ac_get_elem_bits(ctx
, LLVMTypeOf(a
)));
2460 LLVMValueRef args
[2] = {a
, b
};
2461 return ac_build_intrinsic(ctx
, name
, LLVMTypeOf(a
), args
, 2,
2462 AC_FUNC_ATTR_READNONE
);
2465 LLVMValueRef
ac_build_imin(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
2468 LLVMValueRef cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSLE
, a
, b
, "");
2469 return LLVMBuildSelect(ctx
->builder
, cmp
, a
, b
, "");
2472 LLVMValueRef
ac_build_imax(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
2475 LLVMValueRef cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGT
, a
, b
, "");
2476 return LLVMBuildSelect(ctx
->builder
, cmp
, a
, b
, "");
2479 LLVMValueRef
ac_build_umin(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
2482 LLVMValueRef cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntULE
, a
, b
, "");
2483 return LLVMBuildSelect(ctx
->builder
, cmp
, a
, b
, "");
2486 LLVMValueRef
ac_build_umax(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
2489 LLVMValueRef cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntUGE
, a
, b
, "");
2490 return LLVMBuildSelect(ctx
->builder
, cmp
, a
, b
, "");
2493 LLVMValueRef
ac_build_clamp(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
2495 LLVMTypeRef t
= LLVMTypeOf(value
);
2496 return ac_build_fmin(ctx
, ac_build_fmax(ctx
, value
, LLVMConstReal(t
, 0.0)),
2497 LLVMConstReal(t
, 1.0));
2500 void ac_build_export(struct ac_llvm_context
*ctx
, struct ac_export_args
*a
)
2502 LLVMValueRef args
[9];
2504 args
[0] = LLVMConstInt(ctx
->i32
, a
->target
, 0);
2505 args
[1] = LLVMConstInt(ctx
->i32
, a
->enabled_channels
, 0);
2508 LLVMTypeRef i16
= LLVMInt16TypeInContext(ctx
->context
);
2509 LLVMTypeRef v2i16
= LLVMVectorType(i16
, 2);
2511 args
[2] = LLVMBuildBitCast(ctx
->builder
, a
->out
[0],
2513 args
[3] = LLVMBuildBitCast(ctx
->builder
, a
->out
[1],
2515 args
[4] = LLVMConstInt(ctx
->i1
, a
->done
, 0);
2516 args
[5] = LLVMConstInt(ctx
->i1
, a
->valid_mask
, 0);
2518 ac_build_intrinsic(ctx
, "llvm.amdgcn.exp.compr.v2i16",
2519 ctx
->voidt
, args
, 6, 0);
2521 args
[2] = a
->out
[0];
2522 args
[3] = a
->out
[1];
2523 args
[4] = a
->out
[2];
2524 args
[5] = a
->out
[3];
2525 args
[6] = LLVMConstInt(ctx
->i1
, a
->done
, 0);
2526 args
[7] = LLVMConstInt(ctx
->i1
, a
->valid_mask
, 0);
2528 ac_build_intrinsic(ctx
, "llvm.amdgcn.exp.f32",
2529 ctx
->voidt
, args
, 8, 0);
2533 void ac_build_export_null(struct ac_llvm_context
*ctx
)
2535 struct ac_export_args args
;
2537 args
.enabled_channels
= 0x0; /* enabled channels */
2538 args
.valid_mask
= 1; /* whether the EXEC mask is valid */
2539 args
.done
= 1; /* DONE bit */
2540 args
.target
= V_008DFC_SQ_EXP_NULL
;
2541 args
.compr
= 0; /* COMPR flag (0 = 32-bit export) */
2542 args
.out
[0] = LLVMGetUndef(ctx
->f32
); /* R */
2543 args
.out
[1] = LLVMGetUndef(ctx
->f32
); /* G */
2544 args
.out
[2] = LLVMGetUndef(ctx
->f32
); /* B */
2545 args
.out
[3] = LLVMGetUndef(ctx
->f32
); /* A */
2547 ac_build_export(ctx
, &args
);
2550 static unsigned ac_num_coords(enum ac_image_dim dim
)
2556 case ac_image_1darray
:
2560 case ac_image_2darray
:
2561 case ac_image_2dmsaa
:
2563 case ac_image_2darraymsaa
:
2566 unreachable("ac_num_coords: bad dim");
2570 static unsigned ac_num_derivs(enum ac_image_dim dim
)
2574 case ac_image_1darray
:
2577 case ac_image_2darray
:
2582 case ac_image_2dmsaa
:
2583 case ac_image_2darraymsaa
:
2585 unreachable("derivatives not supported");
2589 static const char *get_atomic_name(enum ac_atomic_op op
)
2592 case ac_atomic_swap
: return "swap";
2593 case ac_atomic_add
: return "add";
2594 case ac_atomic_sub
: return "sub";
2595 case ac_atomic_smin
: return "smin";
2596 case ac_atomic_umin
: return "umin";
2597 case ac_atomic_smax
: return "smax";
2598 case ac_atomic_umax
: return "umax";
2599 case ac_atomic_and
: return "and";
2600 case ac_atomic_or
: return "or";
2601 case ac_atomic_xor
: return "xor";
2602 case ac_atomic_inc_wrap
: return "inc";
2603 case ac_atomic_dec_wrap
: return "dec";
2605 unreachable("bad atomic op");
2608 LLVMValueRef
ac_build_image_opcode(struct ac_llvm_context
*ctx
,
2609 struct ac_image_args
*a
)
2611 const char *overload
[3] = { "", "", "" };
2612 unsigned num_overloads
= 0;
2613 LLVMValueRef args
[18];
2614 unsigned num_args
= 0;
2615 enum ac_image_dim dim
= a
->dim
;
2617 assert(!a
->lod
|| a
->lod
== ctx
->i32_0
|| a
->lod
== ctx
->f32_0
||
2619 assert((a
->opcode
!= ac_image_get_resinfo
&& a
->opcode
!= ac_image_load_mip
&&
2620 a
->opcode
!= ac_image_store_mip
) ||
2622 assert(a
->opcode
== ac_image_sample
|| a
->opcode
== ac_image_gather4
||
2623 (!a
->compare
&& !a
->offset
));
2624 assert((a
->opcode
== ac_image_sample
|| a
->opcode
== ac_image_gather4
||
2625 a
->opcode
== ac_image_get_lod
) ||
2627 assert((a
->bias
? 1 : 0) +
2629 (a
->level_zero
? 1 : 0) +
2630 (a
->derivs
[0] ? 1 : 0) <= 1);
2632 if (a
->opcode
== ac_image_get_lod
) {
2634 case ac_image_1darray
:
2637 case ac_image_2darray
:
2646 bool sample
= a
->opcode
== ac_image_sample
||
2647 a
->opcode
== ac_image_gather4
||
2648 a
->opcode
== ac_image_get_lod
;
2649 bool atomic
= a
->opcode
== ac_image_atomic
||
2650 a
->opcode
== ac_image_atomic_cmpswap
;
2651 bool load
= a
->opcode
== ac_image_sample
||
2652 a
->opcode
== ac_image_gather4
||
2653 a
->opcode
== ac_image_load
||
2654 a
->opcode
== ac_image_load_mip
;
2655 LLVMTypeRef coord_type
= sample
? ctx
->f32
: ctx
->i32
;
2657 if (atomic
|| a
->opcode
== ac_image_store
|| a
->opcode
== ac_image_store_mip
) {
2658 args
[num_args
++] = a
->data
[0];
2659 if (a
->opcode
== ac_image_atomic_cmpswap
)
2660 args
[num_args
++] = a
->data
[1];
2664 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->dmask
, false);
2667 args
[num_args
++] = ac_to_integer(ctx
, a
->offset
);
2669 args
[num_args
++] = ac_to_float(ctx
, a
->bias
);
2670 overload
[num_overloads
++] = ".f32";
2673 args
[num_args
++] = ac_to_float(ctx
, a
->compare
);
2675 unsigned count
= ac_num_derivs(dim
);
2676 for (unsigned i
= 0; i
< count
; ++i
)
2677 args
[num_args
++] = ac_to_float(ctx
, a
->derivs
[i
]);
2678 overload
[num_overloads
++] = ".f32";
2680 unsigned num_coords
=
2681 a
->opcode
!= ac_image_get_resinfo
? ac_num_coords(dim
) : 0;
2682 for (unsigned i
= 0; i
< num_coords
; ++i
)
2683 args
[num_args
++] = LLVMBuildBitCast(ctx
->builder
, a
->coords
[i
], coord_type
, "");
2685 args
[num_args
++] = LLVMBuildBitCast(ctx
->builder
, a
->lod
, coord_type
, "");
2686 overload
[num_overloads
++] = sample
? ".f32" : ".i32";
2688 args
[num_args
++] = a
->resource
;
2690 args
[num_args
++] = a
->sampler
;
2691 args
[num_args
++] = LLVMConstInt(ctx
->i1
, a
->unorm
, false);
2694 args
[num_args
++] = ctx
->i32_0
; /* texfailctrl */
2695 args
[num_args
++] = LLVMConstInt(ctx
->i32
,
2696 load
? get_load_cache_policy(ctx
, a
->cache_policy
) :
2697 a
->cache_policy
, false);
2700 const char *atomic_subop
= "";
2701 switch (a
->opcode
) {
2702 case ac_image_sample
: name
= "sample"; break;
2703 case ac_image_gather4
: name
= "gather4"; break;
2704 case ac_image_load
: name
= "load"; break;
2705 case ac_image_load_mip
: name
= "load.mip"; break;
2706 case ac_image_store
: name
= "store"; break;
2707 case ac_image_store_mip
: name
= "store.mip"; break;
2708 case ac_image_atomic
:
2710 atomic_subop
= get_atomic_name(a
->atomic
);
2712 case ac_image_atomic_cmpswap
:
2714 atomic_subop
= "cmpswap";
2716 case ac_image_get_lod
: name
= "getlod"; break;
2717 case ac_image_get_resinfo
: name
= "getresinfo"; break;
2718 default: unreachable("invalid image opcode");
2721 const char *dimname
;
2723 case ac_image_1d
: dimname
= "1d"; break;
2724 case ac_image_2d
: dimname
= "2d"; break;
2725 case ac_image_3d
: dimname
= "3d"; break;
2726 case ac_image_cube
: dimname
= "cube"; break;
2727 case ac_image_1darray
: dimname
= "1darray"; break;
2728 case ac_image_2darray
: dimname
= "2darray"; break;
2729 case ac_image_2dmsaa
: dimname
= "2dmsaa"; break;
2730 case ac_image_2darraymsaa
: dimname
= "2darraymsaa"; break;
2731 default: unreachable("invalid dim");
2735 a
->lod
&& (a
->opcode
== ac_image_sample
|| a
->opcode
== ac_image_gather4
);
2737 snprintf(intr_name
, sizeof(intr_name
),
2738 "llvm.amdgcn.image.%s%s" /* base name */
2739 "%s%s%s" /* sample/gather modifiers */
2740 ".%s.%s%s%s%s", /* dimension and type overloads */
2742 a
->compare
? ".c" : "",
2745 a
->derivs
[0] ? ".d" :
2746 a
->level_zero
? ".lz" : "",
2747 a
->offset
? ".o" : "",
2749 atomic
? "i32" : "v4f32",
2750 overload
[0], overload
[1], overload
[2]);
2755 else if (a
->opcode
== ac_image_store
|| a
->opcode
== ac_image_store_mip
)
2760 LLVMValueRef result
=
2761 ac_build_intrinsic(ctx
, intr_name
, retty
, args
, num_args
,
2763 if (!sample
&& retty
== ctx
->v4f32
) {
2764 result
= LLVMBuildBitCast(ctx
->builder
, result
,
2770 LLVMValueRef
ac_build_cvt_pkrtz_f16(struct ac_llvm_context
*ctx
,
2771 LLVMValueRef args
[2])
2774 LLVMVectorType(LLVMHalfTypeInContext(ctx
->context
), 2);
2776 return ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pkrtz", v2f16
,
2777 args
, 2, AC_FUNC_ATTR_READNONE
);
2780 LLVMValueRef
ac_build_cvt_pknorm_i16(struct ac_llvm_context
*ctx
,
2781 LLVMValueRef args
[2])
2784 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pknorm.i16",
2785 ctx
->v2i16
, args
, 2,
2786 AC_FUNC_ATTR_READNONE
);
2787 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
2790 LLVMValueRef
ac_build_cvt_pknorm_u16(struct ac_llvm_context
*ctx
,
2791 LLVMValueRef args
[2])
2794 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pknorm.u16",
2795 ctx
->v2i16
, args
, 2,
2796 AC_FUNC_ATTR_READNONE
);
2797 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
2800 /* The 8-bit and 10-bit clamping is for HW workarounds. */
2801 LLVMValueRef
ac_build_cvt_pk_i16(struct ac_llvm_context
*ctx
,
2802 LLVMValueRef args
[2], unsigned bits
, bool hi
)
2804 assert(bits
== 8 || bits
== 10 || bits
== 16);
2806 LLVMValueRef max_rgb
= LLVMConstInt(ctx
->i32
,
2807 bits
== 8 ? 127 : bits
== 10 ? 511 : 32767, 0);
2808 LLVMValueRef min_rgb
= LLVMConstInt(ctx
->i32
,
2809 bits
== 8 ? -128 : bits
== 10 ? -512 : -32768, 0);
2810 LLVMValueRef max_alpha
=
2811 bits
!= 10 ? max_rgb
: ctx
->i32_1
;
2812 LLVMValueRef min_alpha
=
2813 bits
!= 10 ? min_rgb
: LLVMConstInt(ctx
->i32
, -2, 0);
2817 for (int i
= 0; i
< 2; i
++) {
2818 bool alpha
= hi
&& i
== 1;
2819 args
[i
] = ac_build_imin(ctx
, args
[i
],
2820 alpha
? max_alpha
: max_rgb
);
2821 args
[i
] = ac_build_imax(ctx
, args
[i
],
2822 alpha
? min_alpha
: min_rgb
);
2827 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pk.i16",
2828 ctx
->v2i16
, args
, 2,
2829 AC_FUNC_ATTR_READNONE
);
2830 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
2833 /* The 8-bit and 10-bit clamping is for HW workarounds. */
2834 LLVMValueRef
ac_build_cvt_pk_u16(struct ac_llvm_context
*ctx
,
2835 LLVMValueRef args
[2], unsigned bits
, bool hi
)
2837 assert(bits
== 8 || bits
== 10 || bits
== 16);
2839 LLVMValueRef max_rgb
= LLVMConstInt(ctx
->i32
,
2840 bits
== 8 ? 255 : bits
== 10 ? 1023 : 65535, 0);
2841 LLVMValueRef max_alpha
=
2842 bits
!= 10 ? max_rgb
: LLVMConstInt(ctx
->i32
, 3, 0);
2846 for (int i
= 0; i
< 2; i
++) {
2847 bool alpha
= hi
&& i
== 1;
2848 args
[i
] = ac_build_umin(ctx
, args
[i
],
2849 alpha
? max_alpha
: max_rgb
);
2854 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pk.u16",
2855 ctx
->v2i16
, args
, 2,
2856 AC_FUNC_ATTR_READNONE
);
2857 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
2860 LLVMValueRef
ac_build_wqm_vote(struct ac_llvm_context
*ctx
, LLVMValueRef i1
)
2862 return ac_build_intrinsic(ctx
, "llvm.amdgcn.wqm.vote", ctx
->i1
,
2863 &i1
, 1, AC_FUNC_ATTR_READNONE
);
2866 void ac_build_kill_if_false(struct ac_llvm_context
*ctx
, LLVMValueRef i1
)
2868 ac_build_intrinsic(ctx
, "llvm.amdgcn.kill", ctx
->voidt
,
2872 LLVMValueRef
ac_build_bfe(struct ac_llvm_context
*ctx
, LLVMValueRef input
,
2873 LLVMValueRef offset
, LLVMValueRef width
,
2876 LLVMValueRef args
[] = {
2882 LLVMValueRef result
= ac_build_intrinsic(ctx
,
2883 is_signed
? "llvm.amdgcn.sbfe.i32" :
2884 "llvm.amdgcn.ubfe.i32",
2886 AC_FUNC_ATTR_READNONE
);
2888 if (HAVE_LLVM
< 0x0800) {
2889 /* FIXME: LLVM 7+ returns incorrect result when count is 0.
2890 * https://bugs.freedesktop.org/show_bug.cgi?id=107276
2892 LLVMValueRef zero
= ctx
->i32_0
;
2893 LLVMValueRef icond
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, width
, zero
, "");
2894 result
= LLVMBuildSelect(ctx
->builder
, icond
, zero
, result
, "");
2900 LLVMValueRef
ac_build_imad(struct ac_llvm_context
*ctx
, LLVMValueRef s0
,
2901 LLVMValueRef s1
, LLVMValueRef s2
)
2903 return LLVMBuildAdd(ctx
->builder
,
2904 LLVMBuildMul(ctx
->builder
, s0
, s1
, ""), s2
, "");
2907 LLVMValueRef
ac_build_fmad(struct ac_llvm_context
*ctx
, LLVMValueRef s0
,
2908 LLVMValueRef s1
, LLVMValueRef s2
)
2910 return LLVMBuildFAdd(ctx
->builder
,
2911 LLVMBuildFMul(ctx
->builder
, s0
, s1
, ""), s2
, "");
2914 void ac_build_waitcnt(struct ac_llvm_context
*ctx
, unsigned wait_flags
)
2919 unsigned lgkmcnt
= 63;
2920 unsigned vmcnt
= ctx
->chip_class
>= GFX9
? 63 : 15;
2921 unsigned vscnt
= 63;
2923 if (wait_flags
& AC_WAIT_LGKM
)
2925 if (wait_flags
& AC_WAIT_VLOAD
)
2928 if (wait_flags
& AC_WAIT_VSTORE
) {
2929 if (ctx
->chip_class
>= GFX10
)
2935 /* There is no intrinsic for vscnt(0), so use a fence. */
2936 if ((wait_flags
& AC_WAIT_LGKM
&&
2937 wait_flags
& AC_WAIT_VLOAD
&&
2938 wait_flags
& AC_WAIT_VSTORE
) ||
2940 LLVMBuildFence(ctx
->builder
, LLVMAtomicOrderingRelease
, false, "");
2944 unsigned simm16
= (lgkmcnt
<< 8) |
2945 (7 << 4) | /* expcnt */
2947 ((vmcnt
>> 4) << 14);
2949 LLVMValueRef args
[1] = {
2950 LLVMConstInt(ctx
->i32
, simm16
, false),
2952 ac_build_intrinsic(ctx
, "llvm.amdgcn.s.waitcnt",
2953 ctx
->voidt
, args
, 1, 0);
2956 LLVMValueRef
ac_build_fmed3(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
2957 LLVMValueRef src1
, LLVMValueRef src2
,
2963 if (bitsize
== 16) {
2964 intr
= "llvm.amdgcn.fmed3.f16";
2966 } else if (bitsize
== 32) {
2967 intr
= "llvm.amdgcn.fmed3.f32";
2970 intr
= "llvm.amdgcn.fmed3.f64";
2974 LLVMValueRef params
[] = {
2979 return ac_build_intrinsic(ctx
, intr
, type
, params
, 3,
2980 AC_FUNC_ATTR_READNONE
);
2983 LLVMValueRef
ac_build_fract(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
2989 if (bitsize
== 16) {
2990 intr
= "llvm.amdgcn.fract.f16";
2992 } else if (bitsize
== 32) {
2993 intr
= "llvm.amdgcn.fract.f32";
2996 intr
= "llvm.amdgcn.fract.f64";
3000 LLVMValueRef params
[] = {
3003 return ac_build_intrinsic(ctx
, intr
, type
, params
, 1,
3004 AC_FUNC_ATTR_READNONE
);
3007 LLVMValueRef
ac_build_isign(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
3010 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->context
, bitsize
);
3011 LLVMValueRef zero
= LLVMConstInt(type
, 0, false);
3012 LLVMValueRef one
= LLVMConstInt(type
, 1, false);
3014 LLVMValueRef cmp
, val
;
3015 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGT
, src0
, zero
, "");
3016 val
= LLVMBuildSelect(ctx
->builder
, cmp
, one
, src0
, "");
3017 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGE
, val
, zero
, "");
3018 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstInt(type
, -1, true), "");
3022 LLVMValueRef
ac_build_fsign(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
3025 LLVMValueRef cmp
, val
, zero
, one
;
3028 if (bitsize
== 16) {
3032 } else if (bitsize
== 32) {
3042 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGT
, src0
, zero
, "");
3043 val
= LLVMBuildSelect(ctx
->builder
, cmp
, one
, src0
, "");
3044 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGE
, val
, zero
, "");
3045 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstReal(type
, -1.0), "");
3049 LLVMValueRef
ac_build_bit_count(struct ac_llvm_context
*ctx
, LLVMValueRef src0
)
3051 LLVMValueRef result
;
3054 bitsize
= ac_get_elem_bits(ctx
, LLVMTypeOf(src0
));
3058 result
= ac_build_intrinsic(ctx
, "llvm.ctpop.i64", ctx
->i64
,
3059 (LLVMValueRef
[]) { src0
}, 1,
3060 AC_FUNC_ATTR_READNONE
);
3062 result
= LLVMBuildTrunc(ctx
->builder
, result
, ctx
->i32
, "");
3065 result
= ac_build_intrinsic(ctx
, "llvm.ctpop.i32", ctx
->i32
,
3066 (LLVMValueRef
[]) { src0
}, 1,
3067 AC_FUNC_ATTR_READNONE
);
3070 result
= ac_build_intrinsic(ctx
, "llvm.ctpop.i16", ctx
->i16
,
3071 (LLVMValueRef
[]) { src0
}, 1,
3072 AC_FUNC_ATTR_READNONE
);
3074 result
= LLVMBuildZExt(ctx
->builder
, result
, ctx
->i32
, "");
3077 result
= ac_build_intrinsic(ctx
, "llvm.ctpop.i8", ctx
->i8
,
3078 (LLVMValueRef
[]) { src0
}, 1,
3079 AC_FUNC_ATTR_READNONE
);
3081 result
= LLVMBuildZExt(ctx
->builder
, result
, ctx
->i32
, "");
3084 unreachable(!"invalid bitsize");
3091 LLVMValueRef
ac_build_bitfield_reverse(struct ac_llvm_context
*ctx
,
3094 LLVMValueRef result
;
3097 bitsize
= ac_get_elem_bits(ctx
, LLVMTypeOf(src0
));
3101 result
= ac_build_intrinsic(ctx
, "llvm.bitreverse.i64", ctx
->i64
,
3102 (LLVMValueRef
[]) { src0
}, 1,
3103 AC_FUNC_ATTR_READNONE
);
3105 result
= LLVMBuildTrunc(ctx
->builder
, result
, ctx
->i32
, "");
3108 result
= ac_build_intrinsic(ctx
, "llvm.bitreverse.i32", ctx
->i32
,
3109 (LLVMValueRef
[]) { src0
}, 1,
3110 AC_FUNC_ATTR_READNONE
);
3113 result
= ac_build_intrinsic(ctx
, "llvm.bitreverse.i16", ctx
->i16
,
3114 (LLVMValueRef
[]) { src0
}, 1,
3115 AC_FUNC_ATTR_READNONE
);
3117 result
= LLVMBuildZExt(ctx
->builder
, result
, ctx
->i32
, "");
3120 result
= ac_build_intrinsic(ctx
, "llvm.bitreverse.i8", ctx
->i8
,
3121 (LLVMValueRef
[]) { src0
}, 1,
3122 AC_FUNC_ATTR_READNONE
);
3124 result
= LLVMBuildZExt(ctx
->builder
, result
, ctx
->i32
, "");
3127 unreachable(!"invalid bitsize");
3134 #define AC_EXP_TARGET 0
3135 #define AC_EXP_ENABLED_CHANNELS 1
3136 #define AC_EXP_OUT0 2
3144 struct ac_vs_exp_chan
3148 enum ac_ir_type type
;
3151 struct ac_vs_exp_inst
{
3154 struct ac_vs_exp_chan chan
[4];
3157 struct ac_vs_exports
{
3159 struct ac_vs_exp_inst exp
[VARYING_SLOT_MAX
];
3162 /* Return true if the PARAM export has been eliminated. */
3163 static bool ac_eliminate_const_output(uint8_t *vs_output_param_offset
,
3164 uint32_t num_outputs
,
3165 struct ac_vs_exp_inst
*exp
)
3167 unsigned i
, default_val
; /* SPI_PS_INPUT_CNTL_i.DEFAULT_VAL */
3168 bool is_zero
[4] = {}, is_one
[4] = {};
3170 for (i
= 0; i
< 4; i
++) {
3171 /* It's a constant expression. Undef outputs are eliminated too. */
3172 if (exp
->chan
[i
].type
== AC_IR_UNDEF
) {
3175 } else if (exp
->chan
[i
].type
== AC_IR_CONST
) {
3176 if (exp
->chan
[i
].const_float
== 0)
3178 else if (exp
->chan
[i
].const_float
== 1)
3181 return false; /* other constant */
3186 /* Only certain combinations of 0 and 1 can be eliminated. */
3187 if (is_zero
[0] && is_zero
[1] && is_zero
[2])
3188 default_val
= is_zero
[3] ? 0 : 1;
3189 else if (is_one
[0] && is_one
[1] && is_one
[2])
3190 default_val
= is_zero
[3] ? 2 : 3;
3194 /* The PARAM export can be represented as DEFAULT_VAL. Kill it. */
3195 LLVMInstructionEraseFromParent(exp
->inst
);
3197 /* Change OFFSET to DEFAULT_VAL. */
3198 for (i
= 0; i
< num_outputs
; i
++) {
3199 if (vs_output_param_offset
[i
] == exp
->offset
) {
3200 vs_output_param_offset
[i
] =
3201 AC_EXP_PARAM_DEFAULT_VAL_0000
+ default_val
;
3208 static bool ac_eliminate_duplicated_output(struct ac_llvm_context
*ctx
,
3209 uint8_t *vs_output_param_offset
,
3210 uint32_t num_outputs
,
3211 struct ac_vs_exports
*processed
,
3212 struct ac_vs_exp_inst
*exp
)
3214 unsigned p
, copy_back_channels
= 0;
3216 /* See if the output is already in the list of processed outputs.
3217 * The LLVMValueRef comparison relies on SSA.
3219 for (p
= 0; p
< processed
->num
; p
++) {
3220 bool different
= false;
3222 for (unsigned j
= 0; j
< 4; j
++) {
3223 struct ac_vs_exp_chan
*c1
= &processed
->exp
[p
].chan
[j
];
3224 struct ac_vs_exp_chan
*c2
= &exp
->chan
[j
];
3226 /* Treat undef as a match. */
3227 if (c2
->type
== AC_IR_UNDEF
)
3230 /* If c1 is undef but c2 isn't, we can copy c2 to c1
3231 * and consider the instruction duplicated.
3233 if (c1
->type
== AC_IR_UNDEF
) {
3234 copy_back_channels
|= 1 << j
;
3238 /* Test whether the channels are not equal. */
3239 if (c1
->type
!= c2
->type
||
3240 (c1
->type
== AC_IR_CONST
&&
3241 c1
->const_float
!= c2
->const_float
) ||
3242 (c1
->type
== AC_IR_VALUE
&&
3243 c1
->value
!= c2
->value
)) {
3251 copy_back_channels
= 0;
3253 if (p
== processed
->num
)
3256 /* If a match was found, but the matching export has undef where the new
3257 * one has a normal value, copy the normal value to the undef channel.
3259 struct ac_vs_exp_inst
*match
= &processed
->exp
[p
];
3261 /* Get current enabled channels mask. */
3262 LLVMValueRef arg
= LLVMGetOperand(match
->inst
, AC_EXP_ENABLED_CHANNELS
);
3263 unsigned enabled_channels
= LLVMConstIntGetZExtValue(arg
);
3265 while (copy_back_channels
) {
3266 unsigned chan
= u_bit_scan(©_back_channels
);
3268 assert(match
->chan
[chan
].type
== AC_IR_UNDEF
);
3269 LLVMSetOperand(match
->inst
, AC_EXP_OUT0
+ chan
,
3270 exp
->chan
[chan
].value
);
3271 match
->chan
[chan
] = exp
->chan
[chan
];
3273 /* Update number of enabled channels because the original mask
3274 * is not always 0xf.
3276 enabled_channels
|= (1 << chan
);
3277 LLVMSetOperand(match
->inst
, AC_EXP_ENABLED_CHANNELS
,
3278 LLVMConstInt(ctx
->i32
, enabled_channels
, 0));
3281 /* The PARAM export is duplicated. Kill it. */
3282 LLVMInstructionEraseFromParent(exp
->inst
);
3284 /* Change OFFSET to the matching export. */
3285 for (unsigned i
= 0; i
< num_outputs
; i
++) {
3286 if (vs_output_param_offset
[i
] == exp
->offset
) {
3287 vs_output_param_offset
[i
] = match
->offset
;
3294 void ac_optimize_vs_outputs(struct ac_llvm_context
*ctx
,
3295 LLVMValueRef main_fn
,
3296 uint8_t *vs_output_param_offset
,
3297 uint32_t num_outputs
,
3298 uint8_t *num_param_exports
)
3300 LLVMBasicBlockRef bb
;
3301 bool removed_any
= false;
3302 struct ac_vs_exports exports
;
3306 /* Process all LLVM instructions. */
3307 bb
= LLVMGetFirstBasicBlock(main_fn
);
3309 LLVMValueRef inst
= LLVMGetFirstInstruction(bb
);
3312 LLVMValueRef cur
= inst
;
3313 inst
= LLVMGetNextInstruction(inst
);
3314 struct ac_vs_exp_inst exp
;
3316 if (LLVMGetInstructionOpcode(cur
) != LLVMCall
)
3319 LLVMValueRef callee
= ac_llvm_get_called_value(cur
);
3321 if (!ac_llvm_is_function(callee
))
3324 const char *name
= LLVMGetValueName(callee
);
3325 unsigned num_args
= LLVMCountParams(callee
);
3327 /* Check if this is an export instruction. */
3328 if ((num_args
!= 9 && num_args
!= 8) ||
3329 (strcmp(name
, "llvm.SI.export") &&
3330 strcmp(name
, "llvm.amdgcn.exp.f32")))
3333 LLVMValueRef arg
= LLVMGetOperand(cur
, AC_EXP_TARGET
);
3334 unsigned target
= LLVMConstIntGetZExtValue(arg
);
3336 if (target
< V_008DFC_SQ_EXP_PARAM
)
3339 target
-= V_008DFC_SQ_EXP_PARAM
;
3341 /* Parse the instruction. */
3342 memset(&exp
, 0, sizeof(exp
));
3343 exp
.offset
= target
;
3346 for (unsigned i
= 0; i
< 4; i
++) {
3347 LLVMValueRef v
= LLVMGetOperand(cur
, AC_EXP_OUT0
+ i
);
3349 exp
.chan
[i
].value
= v
;
3351 if (LLVMIsUndef(v
)) {
3352 exp
.chan
[i
].type
= AC_IR_UNDEF
;
3353 } else if (LLVMIsAConstantFP(v
)) {
3354 LLVMBool loses_info
;
3355 exp
.chan
[i
].type
= AC_IR_CONST
;
3356 exp
.chan
[i
].const_float
=
3357 LLVMConstRealGetDouble(v
, &loses_info
);
3359 exp
.chan
[i
].type
= AC_IR_VALUE
;
3363 /* Eliminate constant and duplicated PARAM exports. */
3364 if (ac_eliminate_const_output(vs_output_param_offset
,
3365 num_outputs
, &exp
) ||
3366 ac_eliminate_duplicated_output(ctx
,
3367 vs_output_param_offset
,
3368 num_outputs
, &exports
,
3372 exports
.exp
[exports
.num
++] = exp
;
3375 bb
= LLVMGetNextBasicBlock(bb
);
3378 /* Remove holes in export memory due to removed PARAM exports.
3379 * This is done by renumbering all PARAM exports.
3382 uint8_t old_offset
[VARYING_SLOT_MAX
];
3385 /* Make a copy of the offsets. We need the old version while
3386 * we are modifying some of them. */
3387 memcpy(old_offset
, vs_output_param_offset
,
3388 sizeof(old_offset
));
3390 for (i
= 0; i
< exports
.num
; i
++) {
3391 unsigned offset
= exports
.exp
[i
].offset
;
3393 /* Update vs_output_param_offset. Multiple outputs can
3394 * have the same offset.
3396 for (out
= 0; out
< num_outputs
; out
++) {
3397 if (old_offset
[out
] == offset
)
3398 vs_output_param_offset
[out
] = i
;
3401 /* Change the PARAM offset in the instruction. */
3402 LLVMSetOperand(exports
.exp
[i
].inst
, AC_EXP_TARGET
,
3403 LLVMConstInt(ctx
->i32
,
3404 V_008DFC_SQ_EXP_PARAM
+ i
, 0));
3406 *num_param_exports
= exports
.num
;
3410 void ac_init_exec_full_mask(struct ac_llvm_context
*ctx
)
3412 LLVMValueRef full_mask
= LLVMConstInt(ctx
->i64
, ~0ull, 0);
3413 ac_build_intrinsic(ctx
,
3414 "llvm.amdgcn.init.exec", ctx
->voidt
,
3415 &full_mask
, 1, AC_FUNC_ATTR_CONVERGENT
);
3418 void ac_declare_lds_as_pointer(struct ac_llvm_context
*ctx
)
3420 unsigned lds_size
= ctx
->chip_class
>= GFX7
? 65536 : 32768;
3421 ctx
->lds
= LLVMBuildIntToPtr(ctx
->builder
, ctx
->i32_0
,
3422 LLVMPointerType(LLVMArrayType(ctx
->i32
, lds_size
/ 4), AC_ADDR_SPACE_LDS
),
3426 LLVMValueRef
ac_lds_load(struct ac_llvm_context
*ctx
,
3427 LLVMValueRef dw_addr
)
3429 return LLVMBuildLoad(ctx
->builder
, ac_build_gep0(ctx
, ctx
->lds
, dw_addr
), "");
3432 void ac_lds_store(struct ac_llvm_context
*ctx
,
3433 LLVMValueRef dw_addr
,
3436 value
= ac_to_integer(ctx
, value
);
3437 ac_build_indexed_store(ctx
, ctx
->lds
,
3441 LLVMValueRef
ac_find_lsb(struct ac_llvm_context
*ctx
,
3442 LLVMTypeRef dst_type
,
3445 unsigned src0_bitsize
= ac_get_elem_bits(ctx
, LLVMTypeOf(src0
));
3446 const char *intrin_name
;
3450 switch (src0_bitsize
) {
3452 intrin_name
= "llvm.cttz.i64";
3457 intrin_name
= "llvm.cttz.i32";
3462 intrin_name
= "llvm.cttz.i16";
3467 intrin_name
= "llvm.cttz.i8";
3472 unreachable(!"invalid bitsize");
3475 LLVMValueRef params
[2] = {
3478 /* The value of 1 means that ffs(x=0) = undef, so LLVM won't
3479 * add special code to check for x=0. The reason is that
3480 * the LLVM behavior for x=0 is different from what we
3481 * need here. However, LLVM also assumes that ffs(x) is
3482 * in [0, 31], but GLSL expects that ffs(0) = -1, so
3483 * a conditional assignment to handle 0 is still required.
3485 * The hardware already implements the correct behavior.
3490 LLVMValueRef lsb
= ac_build_intrinsic(ctx
, intrin_name
, type
,
3492 AC_FUNC_ATTR_READNONE
);
3494 if (src0_bitsize
== 64) {
3495 lsb
= LLVMBuildTrunc(ctx
->builder
, lsb
, ctx
->i32
, "");
3496 } else if (src0_bitsize
< 32) {
3497 lsb
= LLVMBuildSExt(ctx
->builder
, lsb
, ctx
->i32
, "");
3500 /* TODO: We need an intrinsic to skip this conditional. */
3501 /* Check for zero: */
3502 return LLVMBuildSelect(ctx
->builder
, LLVMBuildICmp(ctx
->builder
,
3505 LLVMConstInt(ctx
->i32
, -1, 0), lsb
, "");
3508 LLVMTypeRef
ac_array_in_const_addr_space(LLVMTypeRef elem_type
)
3510 return LLVMPointerType(elem_type
, AC_ADDR_SPACE_CONST
);
3513 LLVMTypeRef
ac_array_in_const32_addr_space(LLVMTypeRef elem_type
)
3515 return LLVMPointerType(elem_type
, AC_ADDR_SPACE_CONST_32BIT
);
3518 static struct ac_llvm_flow
*
3519 get_current_flow(struct ac_llvm_context
*ctx
)
3521 if (ctx
->flow
->depth
> 0)
3522 return &ctx
->flow
->stack
[ctx
->flow
->depth
- 1];
3526 static struct ac_llvm_flow
*
3527 get_innermost_loop(struct ac_llvm_context
*ctx
)
3529 for (unsigned i
= ctx
->flow
->depth
; i
> 0; --i
) {
3530 if (ctx
->flow
->stack
[i
- 1].loop_entry_block
)
3531 return &ctx
->flow
->stack
[i
- 1];
3536 static struct ac_llvm_flow
*
3537 push_flow(struct ac_llvm_context
*ctx
)
3539 struct ac_llvm_flow
*flow
;
3541 if (ctx
->flow
->depth
>= ctx
->flow
->depth_max
) {
3542 unsigned new_max
= MAX2(ctx
->flow
->depth
<< 1,
3543 AC_LLVM_INITIAL_CF_DEPTH
);
3545 ctx
->flow
->stack
= realloc(ctx
->flow
->stack
, new_max
* sizeof(*ctx
->flow
->stack
));
3546 ctx
->flow
->depth_max
= new_max
;
3549 flow
= &ctx
->flow
->stack
[ctx
->flow
->depth
];
3552 flow
->next_block
= NULL
;
3553 flow
->loop_entry_block
= NULL
;
3557 static void set_basicblock_name(LLVMBasicBlockRef bb
, const char *base
,
3561 snprintf(buf
, sizeof(buf
), "%s%d", base
, label_id
);
3562 LLVMSetValueName(LLVMBasicBlockAsValue(bb
), buf
);
3565 /* Append a basic block at the level of the parent flow.
3567 static LLVMBasicBlockRef
append_basic_block(struct ac_llvm_context
*ctx
,
3570 assert(ctx
->flow
->depth
>= 1);
3572 if (ctx
->flow
->depth
>= 2) {
3573 struct ac_llvm_flow
*flow
= &ctx
->flow
->stack
[ctx
->flow
->depth
- 2];
3575 return LLVMInsertBasicBlockInContext(ctx
->context
,
3576 flow
->next_block
, name
);
3579 LLVMValueRef main_fn
=
3580 LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
->builder
));
3581 return LLVMAppendBasicBlockInContext(ctx
->context
, main_fn
, name
);
3584 /* Emit a branch to the given default target for the current block if
3585 * applicable -- that is, if the current block does not already contain a
3586 * branch from a break or continue.
3588 static void emit_default_branch(LLVMBuilderRef builder
,
3589 LLVMBasicBlockRef target
)
3591 if (!LLVMGetBasicBlockTerminator(LLVMGetInsertBlock(builder
)))
3592 LLVMBuildBr(builder
, target
);
3595 void ac_build_bgnloop(struct ac_llvm_context
*ctx
, int label_id
)
3597 struct ac_llvm_flow
*flow
= push_flow(ctx
);
3598 flow
->loop_entry_block
= append_basic_block(ctx
, "LOOP");
3599 flow
->next_block
= append_basic_block(ctx
, "ENDLOOP");
3600 set_basicblock_name(flow
->loop_entry_block
, "loop", label_id
);
3601 LLVMBuildBr(ctx
->builder
, flow
->loop_entry_block
);
3602 LLVMPositionBuilderAtEnd(ctx
->builder
, flow
->loop_entry_block
);
3605 void ac_build_break(struct ac_llvm_context
*ctx
)
3607 struct ac_llvm_flow
*flow
= get_innermost_loop(ctx
);
3608 LLVMBuildBr(ctx
->builder
, flow
->next_block
);
3611 void ac_build_continue(struct ac_llvm_context
*ctx
)
3613 struct ac_llvm_flow
*flow
= get_innermost_loop(ctx
);
3614 LLVMBuildBr(ctx
->builder
, flow
->loop_entry_block
);
3617 void ac_build_else(struct ac_llvm_context
*ctx
, int label_id
)
3619 struct ac_llvm_flow
*current_branch
= get_current_flow(ctx
);
3620 LLVMBasicBlockRef endif_block
;
3622 assert(!current_branch
->loop_entry_block
);
3624 endif_block
= append_basic_block(ctx
, "ENDIF");
3625 emit_default_branch(ctx
->builder
, endif_block
);
3627 LLVMPositionBuilderAtEnd(ctx
->builder
, current_branch
->next_block
);
3628 set_basicblock_name(current_branch
->next_block
, "else", label_id
);
3630 current_branch
->next_block
= endif_block
;
3633 void ac_build_endif(struct ac_llvm_context
*ctx
, int label_id
)
3635 struct ac_llvm_flow
*current_branch
= get_current_flow(ctx
);
3637 assert(!current_branch
->loop_entry_block
);
3639 emit_default_branch(ctx
->builder
, current_branch
->next_block
);
3640 LLVMPositionBuilderAtEnd(ctx
->builder
, current_branch
->next_block
);
3641 set_basicblock_name(current_branch
->next_block
, "endif", label_id
);
3646 void ac_build_endloop(struct ac_llvm_context
*ctx
, int label_id
)
3648 struct ac_llvm_flow
*current_loop
= get_current_flow(ctx
);
3650 assert(current_loop
->loop_entry_block
);
3652 emit_default_branch(ctx
->builder
, current_loop
->loop_entry_block
);
3654 LLVMPositionBuilderAtEnd(ctx
->builder
, current_loop
->next_block
);
3655 set_basicblock_name(current_loop
->next_block
, "endloop", label_id
);
3659 void ac_build_ifcc(struct ac_llvm_context
*ctx
, LLVMValueRef cond
, int label_id
)
3661 struct ac_llvm_flow
*flow
= push_flow(ctx
);
3662 LLVMBasicBlockRef if_block
;
3664 if_block
= append_basic_block(ctx
, "IF");
3665 flow
->next_block
= append_basic_block(ctx
, "ELSE");
3666 set_basicblock_name(if_block
, "if", label_id
);
3667 LLVMBuildCondBr(ctx
->builder
, cond
, if_block
, flow
->next_block
);
3668 LLVMPositionBuilderAtEnd(ctx
->builder
, if_block
);
3671 void ac_build_if(struct ac_llvm_context
*ctx
, LLVMValueRef value
,
3674 LLVMValueRef cond
= LLVMBuildFCmp(ctx
->builder
, LLVMRealUNE
,
3675 value
, ctx
->f32_0
, "");
3676 ac_build_ifcc(ctx
, cond
, label_id
);
3679 void ac_build_uif(struct ac_llvm_context
*ctx
, LLVMValueRef value
,
3682 LLVMValueRef cond
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
3683 ac_to_integer(ctx
, value
),
3685 ac_build_ifcc(ctx
, cond
, label_id
);
3688 LLVMValueRef
ac_build_alloca_undef(struct ac_llvm_context
*ac
, LLVMTypeRef type
,
3691 LLVMBuilderRef builder
= ac
->builder
;
3692 LLVMBasicBlockRef current_block
= LLVMGetInsertBlock(builder
);
3693 LLVMValueRef function
= LLVMGetBasicBlockParent(current_block
);
3694 LLVMBasicBlockRef first_block
= LLVMGetEntryBasicBlock(function
);
3695 LLVMValueRef first_instr
= LLVMGetFirstInstruction(first_block
);
3696 LLVMBuilderRef first_builder
= LLVMCreateBuilderInContext(ac
->context
);
3700 LLVMPositionBuilderBefore(first_builder
, first_instr
);
3702 LLVMPositionBuilderAtEnd(first_builder
, first_block
);
3705 res
= LLVMBuildAlloca(first_builder
, type
, name
);
3706 LLVMDisposeBuilder(first_builder
);
3710 LLVMValueRef
ac_build_alloca(struct ac_llvm_context
*ac
,
3711 LLVMTypeRef type
, const char *name
)
3713 LLVMValueRef ptr
= ac_build_alloca_undef(ac
, type
, name
);
3714 LLVMBuildStore(ac
->builder
, LLVMConstNull(type
), ptr
);
3718 LLVMValueRef
ac_cast_ptr(struct ac_llvm_context
*ctx
, LLVMValueRef ptr
,
3721 int addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
3722 return LLVMBuildBitCast(ctx
->builder
, ptr
,
3723 LLVMPointerType(type
, addr_space
), "");
3726 LLVMValueRef
ac_trim_vector(struct ac_llvm_context
*ctx
, LLVMValueRef value
,
3729 unsigned num_components
= ac_get_llvm_num_components(value
);
3730 if (count
== num_components
)
3733 LLVMValueRef masks
[MAX2(count
, 2)];
3734 masks
[0] = ctx
->i32_0
;
3735 masks
[1] = ctx
->i32_1
;
3736 for (unsigned i
= 2; i
< count
; i
++)
3737 masks
[i
] = LLVMConstInt(ctx
->i32
, i
, false);
3740 return LLVMBuildExtractElement(ctx
->builder
, value
, masks
[0],
3743 LLVMValueRef swizzle
= LLVMConstVector(masks
, count
);
3744 return LLVMBuildShuffleVector(ctx
->builder
, value
, value
, swizzle
, "");
3747 LLVMValueRef
ac_unpack_param(struct ac_llvm_context
*ctx
, LLVMValueRef param
,
3748 unsigned rshift
, unsigned bitwidth
)
3750 LLVMValueRef value
= param
;
3752 value
= LLVMBuildLShr(ctx
->builder
, value
,
3753 LLVMConstInt(ctx
->i32
, rshift
, false), "");
3755 if (rshift
+ bitwidth
< 32) {
3756 unsigned mask
= (1 << bitwidth
) - 1;
3757 value
= LLVMBuildAnd(ctx
->builder
, value
,
3758 LLVMConstInt(ctx
->i32
, mask
, false), "");
3763 /* Adjust the sample index according to FMASK.
3765 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
3766 * which is the identity mapping. Each nibble says which physical sample
3767 * should be fetched to get that sample.
3769 * For example, 0x11111100 means there are only 2 samples stored and
3770 * the second sample covers 3/4 of the pixel. When reading samples 0
3771 * and 1, return physical sample 0 (determined by the first two 0s
3772 * in FMASK), otherwise return physical sample 1.
3774 * The sample index should be adjusted as follows:
3775 * addr[sample_index] = (fmask >> (addr[sample_index] * 4)) & 0xF;
3777 void ac_apply_fmask_to_sample(struct ac_llvm_context
*ac
, LLVMValueRef fmask
,
3778 LLVMValueRef
*addr
, bool is_array_tex
)
3780 struct ac_image_args fmask_load
= {};
3781 fmask_load
.opcode
= ac_image_load
;
3782 fmask_load
.resource
= fmask
;
3783 fmask_load
.dmask
= 0xf;
3784 fmask_load
.dim
= is_array_tex
? ac_image_2darray
: ac_image_2d
;
3785 fmask_load
.attributes
= AC_FUNC_ATTR_READNONE
;
3787 fmask_load
.coords
[0] = addr
[0];
3788 fmask_load
.coords
[1] = addr
[1];
3790 fmask_load
.coords
[2] = addr
[2];
3792 LLVMValueRef fmask_value
= ac_build_image_opcode(ac
, &fmask_load
);
3793 fmask_value
= LLVMBuildExtractElement(ac
->builder
, fmask_value
,
3796 /* Apply the formula. */
3797 unsigned sample_chan
= is_array_tex
? 3 : 2;
3798 LLVMValueRef final_sample
;
3799 final_sample
= LLVMBuildMul(ac
->builder
, addr
[sample_chan
],
3800 LLVMConstInt(ac
->i32
, 4, 0), "");
3801 final_sample
= LLVMBuildLShr(ac
->builder
, fmask_value
, final_sample
, "");
3802 /* Mask the sample index by 0x7, because 0x8 means an unknown value
3803 * with EQAA, so those will map to 0. */
3804 final_sample
= LLVMBuildAnd(ac
->builder
, final_sample
,
3805 LLVMConstInt(ac
->i32
, 0x7, 0), "");
3807 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
3808 * resource descriptor is 0 (invalid).
3811 tmp
= LLVMBuildBitCast(ac
->builder
, fmask
, ac
->v8i32
, "");
3812 tmp
= LLVMBuildExtractElement(ac
->builder
, tmp
, ac
->i32_1
, "");
3813 tmp
= LLVMBuildICmp(ac
->builder
, LLVMIntNE
, tmp
, ac
->i32_0
, "");
3815 /* Replace the MSAA sample index. */
3816 addr
[sample_chan
] = LLVMBuildSelect(ac
->builder
, tmp
, final_sample
,
3817 addr
[sample_chan
], "");
3821 _ac_build_readlane(struct ac_llvm_context
*ctx
, LLVMValueRef src
, LLVMValueRef lane
)
3823 ac_build_optimization_barrier(ctx
, &src
);
3824 return ac_build_intrinsic(ctx
,
3825 lane
== NULL
? "llvm.amdgcn.readfirstlane" : "llvm.amdgcn.readlane",
3826 LLVMTypeOf(src
), (LLVMValueRef
[]) {
3828 lane
== NULL
? 1 : 2,
3829 AC_FUNC_ATTR_READNONE
|
3830 AC_FUNC_ATTR_CONVERGENT
);
3834 * Builds the "llvm.amdgcn.readlane" or "llvm.amdgcn.readfirstlane" intrinsic.
3837 * @param lane - id of the lane or NULL for the first active lane
3838 * @return value of the lane
3841 ac_build_readlane(struct ac_llvm_context
*ctx
, LLVMValueRef src
, LLVMValueRef lane
)
3843 LLVMTypeRef src_type
= LLVMTypeOf(src
);
3844 src
= ac_to_integer(ctx
, src
);
3845 unsigned bits
= LLVMGetIntTypeWidth(LLVMTypeOf(src
));
3849 ret
= _ac_build_readlane(ctx
, src
, lane
);
3851 assert(bits
% 32 == 0);
3852 LLVMTypeRef vec_type
= LLVMVectorType(ctx
->i32
, bits
/ 32);
3853 LLVMValueRef src_vector
=
3854 LLVMBuildBitCast(ctx
->builder
, src
, vec_type
, "");
3855 ret
= LLVMGetUndef(vec_type
);
3856 for (unsigned i
= 0; i
< bits
/ 32; i
++) {
3857 src
= LLVMBuildExtractElement(ctx
->builder
, src_vector
,
3858 LLVMConstInt(ctx
->i32
, i
, 0), "");
3859 LLVMValueRef ret_comp
= _ac_build_readlane(ctx
, src
, lane
);
3860 ret
= LLVMBuildInsertElement(ctx
->builder
, ret
, ret_comp
,
3861 LLVMConstInt(ctx
->i32
, i
, 0), "");
3864 if (LLVMGetTypeKind(src_type
) == LLVMPointerTypeKind
)
3865 return LLVMBuildIntToPtr(ctx
->builder
, ret
, src_type
, "");
3866 return LLVMBuildBitCast(ctx
->builder
, ret
, src_type
, "");
3870 ac_build_writelane(struct ac_llvm_context
*ctx
, LLVMValueRef src
, LLVMValueRef value
, LLVMValueRef lane
)
3872 if (HAVE_LLVM
>= 0x0800) {
3873 return ac_build_intrinsic(ctx
, "llvm.amdgcn.writelane", ctx
->i32
,
3874 (LLVMValueRef
[]) {value
, lane
, src
}, 3,
3875 AC_FUNC_ATTR_READNONE
| AC_FUNC_ATTR_CONVERGENT
);
3878 LLVMValueRef pred
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, lane
,
3879 ac_get_thread_id(ctx
), "");
3880 return LLVMBuildSelect(ctx
->builder
, pred
, value
, src
, "");
3884 ac_build_mbcnt(struct ac_llvm_context
*ctx
, LLVMValueRef mask
)
3886 if (ctx
->wave_size
== 32) {
3887 return ac_build_intrinsic(ctx
, "llvm.amdgcn.mbcnt.lo", ctx
->i32
,
3888 (LLVMValueRef
[]) { mask
, ctx
->i32_0
},
3889 2, AC_FUNC_ATTR_READNONE
);
3891 LLVMValueRef mask_vec
= LLVMBuildBitCast(ctx
->builder
, mask
,
3892 LLVMVectorType(ctx
->i32
, 2),
3894 LLVMValueRef mask_lo
= LLVMBuildExtractElement(ctx
->builder
, mask_vec
,
3896 LLVMValueRef mask_hi
= LLVMBuildExtractElement(ctx
->builder
, mask_vec
,
3899 ac_build_intrinsic(ctx
, "llvm.amdgcn.mbcnt.lo", ctx
->i32
,
3900 (LLVMValueRef
[]) { mask_lo
, ctx
->i32_0
},
3901 2, AC_FUNC_ATTR_READNONE
);
3902 val
= ac_build_intrinsic(ctx
, "llvm.amdgcn.mbcnt.hi", ctx
->i32
,
3903 (LLVMValueRef
[]) { mask_hi
, val
},
3904 2, AC_FUNC_ATTR_READNONE
);
3909 _dpp_quad_perm
= 0x000,
3910 _dpp_row_sl
= 0x100,
3911 _dpp_row_sr
= 0x110,
3912 _dpp_row_rr
= 0x120,
3917 dpp_row_mirror
= 0x140,
3918 dpp_row_half_mirror
= 0x141,
3919 dpp_row_bcast15
= 0x142,
3920 dpp_row_bcast31
= 0x143
3923 static inline enum dpp_ctrl
3924 dpp_quad_perm(unsigned lane0
, unsigned lane1
, unsigned lane2
, unsigned lane3
)
3926 assert(lane0
< 4 && lane1
< 4 && lane2
< 4 && lane3
< 4);
3927 return _dpp_quad_perm
| lane0
| (lane1
<< 2) | (lane2
<< 4) | (lane3
<< 6);
3930 static inline enum dpp_ctrl
3931 dpp_row_sl(unsigned amount
)
3933 assert(amount
> 0 && amount
< 16);
3934 return _dpp_row_sl
| amount
;
3937 static inline enum dpp_ctrl
3938 dpp_row_sr(unsigned amount
)
3940 assert(amount
> 0 && amount
< 16);
3941 return _dpp_row_sr
| amount
;
3945 _ac_build_dpp(struct ac_llvm_context
*ctx
, LLVMValueRef old
, LLVMValueRef src
,
3946 enum dpp_ctrl dpp_ctrl
, unsigned row_mask
, unsigned bank_mask
,
3949 return ac_build_intrinsic(ctx
, "llvm.amdgcn.update.dpp.i32",
3953 LLVMConstInt(ctx
->i32
, dpp_ctrl
, 0),
3954 LLVMConstInt(ctx
->i32
, row_mask
, 0),
3955 LLVMConstInt(ctx
->i32
, bank_mask
, 0),
3956 LLVMConstInt(ctx
->i1
, bound_ctrl
, 0) },
3957 6, AC_FUNC_ATTR_READNONE
| AC_FUNC_ATTR_CONVERGENT
);
3961 ac_build_dpp(struct ac_llvm_context
*ctx
, LLVMValueRef old
, LLVMValueRef src
,
3962 enum dpp_ctrl dpp_ctrl
, unsigned row_mask
, unsigned bank_mask
,
3965 LLVMTypeRef src_type
= LLVMTypeOf(src
);
3966 src
= ac_to_integer(ctx
, src
);
3967 old
= ac_to_integer(ctx
, old
);
3968 unsigned bits
= LLVMGetIntTypeWidth(LLVMTypeOf(src
));
3971 ret
= _ac_build_dpp(ctx
, old
, src
, dpp_ctrl
, row_mask
,
3972 bank_mask
, bound_ctrl
);
3974 assert(bits
% 32 == 0);
3975 LLVMTypeRef vec_type
= LLVMVectorType(ctx
->i32
, bits
/ 32);
3976 LLVMValueRef src_vector
=
3977 LLVMBuildBitCast(ctx
->builder
, src
, vec_type
, "");
3978 LLVMValueRef old_vector
=
3979 LLVMBuildBitCast(ctx
->builder
, old
, vec_type
, "");
3980 ret
= LLVMGetUndef(vec_type
);
3981 for (unsigned i
= 0; i
< bits
/ 32; i
++) {
3982 src
= LLVMBuildExtractElement(ctx
->builder
, src_vector
,
3983 LLVMConstInt(ctx
->i32
, i
,
3985 old
= LLVMBuildExtractElement(ctx
->builder
, old_vector
,
3986 LLVMConstInt(ctx
->i32
, i
,
3988 LLVMValueRef ret_comp
= _ac_build_dpp(ctx
, old
, src
,
3993 ret
= LLVMBuildInsertElement(ctx
->builder
, ret
,
3995 LLVMConstInt(ctx
->i32
, i
,
3999 return LLVMBuildBitCast(ctx
->builder
, ret
, src_type
, "");
4003 _ac_build_permlane16(struct ac_llvm_context
*ctx
, LLVMValueRef src
, uint64_t sel
,
4004 bool exchange_rows
, bool bound_ctrl
)
4006 LLVMValueRef args
[6] = {
4009 LLVMConstInt(ctx
->i32
, sel
, false),
4010 LLVMConstInt(ctx
->i32
, sel
>> 32, false),
4011 ctx
->i1true
, /* fi */
4012 bound_ctrl
? ctx
->i1true
: ctx
->i1false
,
4014 return ac_build_intrinsic(ctx
, exchange_rows
? "llvm.amdgcn.permlanex16"
4015 : "llvm.amdgcn.permlane16",
4017 AC_FUNC_ATTR_READNONE
| AC_FUNC_ATTR_CONVERGENT
);
4021 ac_build_permlane16(struct ac_llvm_context
*ctx
, LLVMValueRef src
, uint64_t sel
,
4022 bool exchange_rows
, bool bound_ctrl
)
4024 LLVMTypeRef src_type
= LLVMTypeOf(src
);
4025 src
= ac_to_integer(ctx
, src
);
4026 unsigned bits
= LLVMGetIntTypeWidth(LLVMTypeOf(src
));
4029 ret
= _ac_build_permlane16(ctx
, src
, sel
, exchange_rows
,
4032 assert(bits
% 32 == 0);
4033 LLVMTypeRef vec_type
= LLVMVectorType(ctx
->i32
, bits
/ 32);
4034 LLVMValueRef src_vector
=
4035 LLVMBuildBitCast(ctx
->builder
, src
, vec_type
, "");
4036 ret
= LLVMGetUndef(vec_type
);
4037 for (unsigned i
= 0; i
< bits
/ 32; i
++) {
4038 src
= LLVMBuildExtractElement(ctx
->builder
, src_vector
,
4039 LLVMConstInt(ctx
->i32
, i
,
4041 LLVMValueRef ret_comp
=
4042 _ac_build_permlane16(ctx
, src
, sel
,
4045 ret
= LLVMBuildInsertElement(ctx
->builder
, ret
,
4047 LLVMConstInt(ctx
->i32
, i
,
4051 return LLVMBuildBitCast(ctx
->builder
, ret
, src_type
, "");
4054 static inline unsigned
4055 ds_pattern_bitmode(unsigned and_mask
, unsigned or_mask
, unsigned xor_mask
)
4057 assert(and_mask
< 32 && or_mask
< 32 && xor_mask
< 32);
4058 return and_mask
| (or_mask
<< 5) | (xor_mask
<< 10);
4062 _ac_build_ds_swizzle(struct ac_llvm_context
*ctx
, LLVMValueRef src
, unsigned mask
)
4064 return ac_build_intrinsic(ctx
, "llvm.amdgcn.ds.swizzle",
4065 LLVMTypeOf(src
), (LLVMValueRef
[]) {
4066 src
, LLVMConstInt(ctx
->i32
, mask
, 0) },
4067 2, AC_FUNC_ATTR_READNONE
| AC_FUNC_ATTR_CONVERGENT
);
4071 ac_build_ds_swizzle(struct ac_llvm_context
*ctx
, LLVMValueRef src
, unsigned mask
)
4073 LLVMTypeRef src_type
= LLVMTypeOf(src
);
4074 src
= ac_to_integer(ctx
, src
);
4075 unsigned bits
= LLVMGetIntTypeWidth(LLVMTypeOf(src
));
4078 ret
= _ac_build_ds_swizzle(ctx
, src
, mask
);
4080 assert(bits
% 32 == 0);
4081 LLVMTypeRef vec_type
= LLVMVectorType(ctx
->i32
, bits
/ 32);
4082 LLVMValueRef src_vector
=
4083 LLVMBuildBitCast(ctx
->builder
, src
, vec_type
, "");
4084 ret
= LLVMGetUndef(vec_type
);
4085 for (unsigned i
= 0; i
< bits
/ 32; i
++) {
4086 src
= LLVMBuildExtractElement(ctx
->builder
, src_vector
,
4087 LLVMConstInt(ctx
->i32
, i
,
4089 LLVMValueRef ret_comp
= _ac_build_ds_swizzle(ctx
, src
,
4091 ret
= LLVMBuildInsertElement(ctx
->builder
, ret
,
4093 LLVMConstInt(ctx
->i32
, i
,
4097 return LLVMBuildBitCast(ctx
->builder
, ret
, src_type
, "");
4101 ac_build_wwm(struct ac_llvm_context
*ctx
, LLVMValueRef src
)
4103 char name
[32], type
[8];
4104 ac_build_type_name_for_intr(LLVMTypeOf(src
), type
, sizeof(type
));
4105 snprintf(name
, sizeof(name
), "llvm.amdgcn.wwm.%s", type
);
4106 return ac_build_intrinsic(ctx
, name
, LLVMTypeOf(src
),
4107 (LLVMValueRef
[]) { src
}, 1,
4108 AC_FUNC_ATTR_READNONE
);
4112 ac_build_set_inactive(struct ac_llvm_context
*ctx
, LLVMValueRef src
,
4113 LLVMValueRef inactive
)
4115 char name
[33], type
[8];
4116 LLVMTypeRef src_type
= LLVMTypeOf(src
);
4117 src
= ac_to_integer(ctx
, src
);
4118 inactive
= ac_to_integer(ctx
, inactive
);
4119 ac_build_type_name_for_intr(LLVMTypeOf(src
), type
, sizeof(type
));
4120 snprintf(name
, sizeof(name
), "llvm.amdgcn.set.inactive.%s", type
);
4122 ac_build_intrinsic(ctx
, name
,
4123 LLVMTypeOf(src
), (LLVMValueRef
[]) {
4125 AC_FUNC_ATTR_READNONE
|
4126 AC_FUNC_ATTR_CONVERGENT
);
4127 return LLVMBuildBitCast(ctx
->builder
, ret
, src_type
, "");
4131 get_reduction_identity(struct ac_llvm_context
*ctx
, nir_op op
, unsigned type_size
)
4133 if (type_size
== 4) {
4135 case nir_op_iadd
: return ctx
->i32_0
;
4136 case nir_op_fadd
: return ctx
->f32_0
;
4137 case nir_op_imul
: return ctx
->i32_1
;
4138 case nir_op_fmul
: return ctx
->f32_1
;
4139 case nir_op_imin
: return LLVMConstInt(ctx
->i32
, INT32_MAX
, 0);
4140 case nir_op_umin
: return LLVMConstInt(ctx
->i32
, UINT32_MAX
, 0);
4141 case nir_op_fmin
: return LLVMConstReal(ctx
->f32
, INFINITY
);
4142 case nir_op_imax
: return LLVMConstInt(ctx
->i32
, INT32_MIN
, 0);
4143 case nir_op_umax
: return ctx
->i32_0
;
4144 case nir_op_fmax
: return LLVMConstReal(ctx
->f32
, -INFINITY
);
4145 case nir_op_iand
: return LLVMConstInt(ctx
->i32
, -1, 0);
4146 case nir_op_ior
: return ctx
->i32_0
;
4147 case nir_op_ixor
: return ctx
->i32_0
;
4149 unreachable("bad reduction intrinsic");
4151 } else { /* type_size == 64bit */
4153 case nir_op_iadd
: return ctx
->i64_0
;
4154 case nir_op_fadd
: return ctx
->f64_0
;
4155 case nir_op_imul
: return ctx
->i64_1
;
4156 case nir_op_fmul
: return ctx
->f64_1
;
4157 case nir_op_imin
: return LLVMConstInt(ctx
->i64
, INT64_MAX
, 0);
4158 case nir_op_umin
: return LLVMConstInt(ctx
->i64
, UINT64_MAX
, 0);
4159 case nir_op_fmin
: return LLVMConstReal(ctx
->f64
, INFINITY
);
4160 case nir_op_imax
: return LLVMConstInt(ctx
->i64
, INT64_MIN
, 0);
4161 case nir_op_umax
: return ctx
->i64_0
;
4162 case nir_op_fmax
: return LLVMConstReal(ctx
->f64
, -INFINITY
);
4163 case nir_op_iand
: return LLVMConstInt(ctx
->i64
, -1, 0);
4164 case nir_op_ior
: return ctx
->i64_0
;
4165 case nir_op_ixor
: return ctx
->i64_0
;
4167 unreachable("bad reduction intrinsic");
4173 ac_build_alu_op(struct ac_llvm_context
*ctx
, LLVMValueRef lhs
, LLVMValueRef rhs
, nir_op op
)
4175 bool _64bit
= ac_get_type_size(LLVMTypeOf(lhs
)) == 8;
4177 case nir_op_iadd
: return LLVMBuildAdd(ctx
->builder
, lhs
, rhs
, "");
4178 case nir_op_fadd
: return LLVMBuildFAdd(ctx
->builder
, lhs
, rhs
, "");
4179 case nir_op_imul
: return LLVMBuildMul(ctx
->builder
, lhs
, rhs
, "");
4180 case nir_op_fmul
: return LLVMBuildFMul(ctx
->builder
, lhs
, rhs
, "");
4181 case nir_op_imin
: return LLVMBuildSelect(ctx
->builder
,
4182 LLVMBuildICmp(ctx
->builder
, LLVMIntSLT
, lhs
, rhs
, ""),
4184 case nir_op_umin
: return LLVMBuildSelect(ctx
->builder
,
4185 LLVMBuildICmp(ctx
->builder
, LLVMIntULT
, lhs
, rhs
, ""),
4187 case nir_op_fmin
: return ac_build_intrinsic(ctx
,
4188 _64bit
? "llvm.minnum.f64" : "llvm.minnum.f32",
4189 _64bit
? ctx
->f64
: ctx
->f32
,
4190 (LLVMValueRef
[]){lhs
, rhs
}, 2, AC_FUNC_ATTR_READNONE
);
4191 case nir_op_imax
: return LLVMBuildSelect(ctx
->builder
,
4192 LLVMBuildICmp(ctx
->builder
, LLVMIntSGT
, lhs
, rhs
, ""),
4194 case nir_op_umax
: return LLVMBuildSelect(ctx
->builder
,
4195 LLVMBuildICmp(ctx
->builder
, LLVMIntUGT
, lhs
, rhs
, ""),
4197 case nir_op_fmax
: return ac_build_intrinsic(ctx
,
4198 _64bit
? "llvm.maxnum.f64" : "llvm.maxnum.f32",
4199 _64bit
? ctx
->f64
: ctx
->f32
,
4200 (LLVMValueRef
[]){lhs
, rhs
}, 2, AC_FUNC_ATTR_READNONE
);
4201 case nir_op_iand
: return LLVMBuildAnd(ctx
->builder
, lhs
, rhs
, "");
4202 case nir_op_ior
: return LLVMBuildOr(ctx
->builder
, lhs
, rhs
, "");
4203 case nir_op_ixor
: return LLVMBuildXor(ctx
->builder
, lhs
, rhs
, "");
4205 unreachable("bad reduction intrinsic");
4210 * \param maxprefix specifies that the result only needs to be correct for a
4211 * prefix of this many threads
4213 * TODO: add inclusive and excluse scan functions for GFX6.
4216 ac_build_scan(struct ac_llvm_context
*ctx
, nir_op op
, LLVMValueRef src
, LLVMValueRef identity
,
4217 unsigned maxprefix
, bool inclusive
)
4219 LLVMValueRef result
, tmp
;
4221 if (ctx
->chip_class
>= GFX10
) {
4222 result
= inclusive
? src
: identity
;
4227 result
= ac_build_dpp(ctx
, identity
, src
, dpp_wf_sr1
, 0xf, 0xf, false);
4231 tmp
= ac_build_dpp(ctx
, identity
, src
, dpp_row_sr(1), 0xf, 0xf, false);
4232 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
4235 tmp
= ac_build_dpp(ctx
, identity
, src
, dpp_row_sr(2), 0xf, 0xf, false);
4236 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
4239 tmp
= ac_build_dpp(ctx
, identity
, src
, dpp_row_sr(3), 0xf, 0xf, false);
4240 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
4243 tmp
= ac_build_dpp(ctx
, identity
, result
, dpp_row_sr(4), 0xf, 0xe, false);
4244 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
4247 tmp
= ac_build_dpp(ctx
, identity
, result
, dpp_row_sr(8), 0xf, 0xc, false);
4248 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
4249 if (maxprefix
<= 16)
4252 if (ctx
->chip_class
>= GFX10
) {
4253 /* dpp_row_bcast{15,31} are not supported on gfx10. */
4254 LLVMBuilderRef builder
= ctx
->builder
;
4255 LLVMValueRef tid
= ac_get_thread_id(ctx
);
4257 /* TODO-GFX10: Can we get better code-gen by putting this into
4258 * a branch so that LLVM generates EXEC mask manipulations? */
4262 tmp
= ac_build_alu_op(ctx
, result
, src
, op
);
4263 tmp
= ac_build_permlane16(ctx
, tmp
, ~(uint64_t)0, true, false);
4264 tmp
= ac_build_alu_op(ctx
, result
, tmp
, op
);
4265 cc
= LLVMBuildAnd(builder
, tid
, LLVMConstInt(ctx
->i32
, 16, false), "");
4266 cc
= LLVMBuildICmp(builder
, LLVMIntNE
, cc
, ctx
->i32_0
, "");
4267 result
= LLVMBuildSelect(builder
, cc
, tmp
, result
, "");
4268 if (maxprefix
<= 32)
4274 tmp
= ac_build_alu_op(ctx
, result
, src
, op
);
4275 tmp
= ac_build_readlane(ctx
, tmp
, LLVMConstInt(ctx
->i32
, 31, false));
4276 tmp
= ac_build_alu_op(ctx
, result
, tmp
, op
);
4277 cc
= LLVMBuildICmp(builder
, LLVMIntUGE
, tid
,
4278 LLVMConstInt(ctx
->i32
, 32, false), "");
4279 result
= LLVMBuildSelect(builder
, cc
, tmp
, result
, "");
4283 tmp
= ac_build_dpp(ctx
, identity
, result
, dpp_row_bcast15
, 0xa, 0xf, false);
4284 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
4285 if (maxprefix
<= 32)
4287 tmp
= ac_build_dpp(ctx
, identity
, result
, dpp_row_bcast31
, 0xc, 0xf, false);
4288 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
4293 ac_build_inclusive_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
);
4302 result
= LLVMBuildAdd(builder
, result
, src
, "");
4306 ac_build_optimization_barrier(ctx
, &src
);
4308 LLVMValueRef identity
=
4309 get_reduction_identity(ctx
, op
, ac_get_type_size(LLVMTypeOf(src
)));
4310 result
= LLVMBuildBitCast(ctx
->builder
, ac_build_set_inactive(ctx
, src
, identity
),
4311 LLVMTypeOf(identity
), "");
4312 result
= ac_build_scan(ctx
, op
, result
, identity
, ctx
->wave_size
, true);
4314 return ac_build_wwm(ctx
, result
);
4318 ac_build_exclusive_scan(struct ac_llvm_context
*ctx
, LLVMValueRef src
, nir_op op
)
4320 LLVMValueRef result
;
4322 if (LLVMTypeOf(src
) == ctx
->i1
&& op
== nir_op_iadd
) {
4323 LLVMBuilderRef builder
= ctx
->builder
;
4324 src
= LLVMBuildZExt(builder
, src
, ctx
->i32
, "");
4325 result
= ac_build_ballot(ctx
, src
);
4326 result
= ac_build_mbcnt(ctx
, result
);
4330 ac_build_optimization_barrier(ctx
, &src
);
4332 LLVMValueRef identity
=
4333 get_reduction_identity(ctx
, op
, ac_get_type_size(LLVMTypeOf(src
)));
4334 result
= LLVMBuildBitCast(ctx
->builder
, ac_build_set_inactive(ctx
, src
, identity
),
4335 LLVMTypeOf(identity
), "");
4336 result
= ac_build_scan(ctx
, op
, result
, identity
, ctx
->wave_size
, false);
4338 return ac_build_wwm(ctx
, result
);
4342 ac_build_reduce(struct ac_llvm_context
*ctx
, LLVMValueRef src
, nir_op op
, unsigned cluster_size
)
4344 if (cluster_size
== 1) return src
;
4345 ac_build_optimization_barrier(ctx
, &src
);
4346 LLVMValueRef result
, swap
;
4347 LLVMValueRef identity
= get_reduction_identity(ctx
, op
,
4348 ac_get_type_size(LLVMTypeOf(src
)));
4349 result
= LLVMBuildBitCast(ctx
->builder
,
4350 ac_build_set_inactive(ctx
, src
, identity
),
4351 LLVMTypeOf(identity
), "");
4352 swap
= ac_build_quad_swizzle(ctx
, result
, 1, 0, 3, 2);
4353 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
4354 if (cluster_size
== 2) return ac_build_wwm(ctx
, result
);
4356 swap
= ac_build_quad_swizzle(ctx
, result
, 2, 3, 0, 1);
4357 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
4358 if (cluster_size
== 4) return ac_build_wwm(ctx
, result
);
4360 if (ctx
->chip_class
>= GFX8
)
4361 swap
= ac_build_dpp(ctx
, identity
, result
, dpp_row_half_mirror
, 0xf, 0xf, false);
4363 swap
= ac_build_ds_swizzle(ctx
, result
, ds_pattern_bitmode(0x1f, 0, 0x04));
4364 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
4365 if (cluster_size
== 8) return ac_build_wwm(ctx
, result
);
4367 if (ctx
->chip_class
>= GFX8
)
4368 swap
= ac_build_dpp(ctx
, identity
, result
, dpp_row_mirror
, 0xf, 0xf, false);
4370 swap
= ac_build_ds_swizzle(ctx
, result
, ds_pattern_bitmode(0x1f, 0, 0x08));
4371 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
4372 if (cluster_size
== 16) return ac_build_wwm(ctx
, result
);
4374 if (ctx
->chip_class
>= GFX10
)
4375 swap
= ac_build_permlane16(ctx
, result
, 0, true, false);
4376 else if (ctx
->chip_class
>= GFX8
&& cluster_size
!= 32)
4377 swap
= ac_build_dpp(ctx
, identity
, result
, dpp_row_bcast15
, 0xa, 0xf, false);
4379 swap
= ac_build_ds_swizzle(ctx
, result
, ds_pattern_bitmode(0x1f, 0, 0x10));
4380 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
4381 if (cluster_size
== 32) return ac_build_wwm(ctx
, result
);
4383 if (ctx
->chip_class
>= GFX8
) {
4384 if (ctx
->chip_class
>= GFX10
)
4385 swap
= ac_build_readlane(ctx
, result
, LLVMConstInt(ctx
->i32
, 31, false));
4387 swap
= ac_build_dpp(ctx
, identity
, result
, dpp_row_bcast31
, 0xc, 0xf, false);
4388 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
4389 result
= ac_build_readlane(ctx
, result
, LLVMConstInt(ctx
->i32
, 63, 0));
4390 return ac_build_wwm(ctx
, result
);
4392 swap
= ac_build_readlane(ctx
, result
, ctx
->i32_0
);
4393 result
= ac_build_readlane(ctx
, result
, LLVMConstInt(ctx
->i32
, 32, 0));
4394 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
4395 return ac_build_wwm(ctx
, result
);
4400 * "Top half" of a scan that reduces per-wave values across an entire
4403 * The source value must be present in the highest lane of the wave, and the
4404 * highest lane must be live.
4407 ac_build_wg_wavescan_top(struct ac_llvm_context
*ctx
, struct ac_wg_scan
*ws
)
4409 if (ws
->maxwaves
<= 1)
4412 const LLVMValueRef last_lane
= LLVMConstInt(ctx
->i32
, ctx
->wave_size
- 1, false);
4413 LLVMBuilderRef builder
= ctx
->builder
;
4414 LLVMValueRef tid
= ac_get_thread_id(ctx
);
4417 tmp
= LLVMBuildICmp(builder
, LLVMIntEQ
, tid
, last_lane
, "");
4418 ac_build_ifcc(ctx
, tmp
, 1000);
4419 LLVMBuildStore(builder
, ws
->src
, LLVMBuildGEP(builder
, ws
->scratch
, &ws
->waveidx
, 1, ""));
4420 ac_build_endif(ctx
, 1000);
4424 * "Bottom half" of a scan that reduces per-wave values across an entire
4427 * The caller must place a barrier between the top and bottom halves.
4430 ac_build_wg_wavescan_bottom(struct ac_llvm_context
*ctx
, struct ac_wg_scan
*ws
)
4432 const LLVMTypeRef type
= LLVMTypeOf(ws
->src
);
4433 const LLVMValueRef identity
=
4434 get_reduction_identity(ctx
, ws
->op
, ac_get_type_size(type
));
4436 if (ws
->maxwaves
<= 1) {
4437 ws
->result_reduce
= ws
->src
;
4438 ws
->result_inclusive
= ws
->src
;
4439 ws
->result_exclusive
= identity
;
4442 assert(ws
->maxwaves
<= 32);
4444 LLVMBuilderRef builder
= ctx
->builder
;
4445 LLVMValueRef tid
= ac_get_thread_id(ctx
);
4446 LLVMBasicBlockRef bbs
[2];
4447 LLVMValueRef phivalues_scan
[2];
4448 LLVMValueRef tmp
, tmp2
;
4450 bbs
[0] = LLVMGetInsertBlock(builder
);
4451 phivalues_scan
[0] = LLVMGetUndef(type
);
4453 if (ws
->enable_reduce
)
4454 tmp
= LLVMBuildICmp(builder
, LLVMIntULT
, tid
, ws
->numwaves
, "");
4455 else if (ws
->enable_inclusive
)
4456 tmp
= LLVMBuildICmp(builder
, LLVMIntULE
, tid
, ws
->waveidx
, "");
4458 tmp
= LLVMBuildICmp(builder
, LLVMIntULT
, tid
, ws
->waveidx
, "");
4459 ac_build_ifcc(ctx
, tmp
, 1001);
4461 tmp
= LLVMBuildLoad(builder
, LLVMBuildGEP(builder
, ws
->scratch
, &tid
, 1, ""), "");
4463 ac_build_optimization_barrier(ctx
, &tmp
);
4465 bbs
[1] = LLVMGetInsertBlock(builder
);
4466 phivalues_scan
[1] = ac_build_scan(ctx
, ws
->op
, tmp
, identity
, ws
->maxwaves
, true);
4468 ac_build_endif(ctx
, 1001);
4470 const LLVMValueRef scan
= ac_build_phi(ctx
, type
, 2, phivalues_scan
, bbs
);
4472 if (ws
->enable_reduce
) {
4473 tmp
= LLVMBuildSub(builder
, ws
->numwaves
, ctx
->i32_1
, "");
4474 ws
->result_reduce
= ac_build_readlane(ctx
, scan
, tmp
);
4476 if (ws
->enable_inclusive
)
4477 ws
->result_inclusive
= ac_build_readlane(ctx
, scan
, ws
->waveidx
);
4478 if (ws
->enable_exclusive
) {
4479 tmp
= LLVMBuildSub(builder
, ws
->waveidx
, ctx
->i32_1
, "");
4480 tmp
= ac_build_readlane(ctx
, scan
, tmp
);
4481 tmp2
= LLVMBuildICmp(builder
, LLVMIntEQ
, ws
->waveidx
, ctx
->i32_0
, "");
4482 ws
->result_exclusive
= LLVMBuildSelect(builder
, tmp2
, identity
, tmp
, "");
4487 * Inclusive scan of a per-wave value across an entire workgroup.
4489 * This implies an s_barrier instruction.
4491 * Unlike ac_build_inclusive_scan, the caller \em must ensure that all threads
4492 * of the workgroup are live. (This requirement cannot easily be relaxed in a
4493 * useful manner because of the barrier in the algorithm.)
4496 ac_build_wg_wavescan(struct ac_llvm_context
*ctx
, struct ac_wg_scan
*ws
)
4498 ac_build_wg_wavescan_top(ctx
, ws
);
4499 ac_build_s_barrier(ctx
);
4500 ac_build_wg_wavescan_bottom(ctx
, ws
);
4504 * "Top half" of a scan that reduces per-thread values across an entire
4507 * All lanes must be active when this code runs.
4510 ac_build_wg_scan_top(struct ac_llvm_context
*ctx
, struct ac_wg_scan
*ws
)
4512 if (ws
->enable_exclusive
) {
4513 ws
->extra
= ac_build_exclusive_scan(ctx
, ws
->src
, ws
->op
);
4514 if (LLVMTypeOf(ws
->src
) == ctx
->i1
&& ws
->op
== nir_op_iadd
)
4515 ws
->src
= LLVMBuildZExt(ctx
->builder
, ws
->src
, ctx
->i32
, "");
4516 ws
->src
= ac_build_alu_op(ctx
, ws
->extra
, ws
->src
, ws
->op
);
4518 ws
->src
= ac_build_inclusive_scan(ctx
, ws
->src
, ws
->op
);
4521 bool enable_inclusive
= ws
->enable_inclusive
;
4522 bool enable_exclusive
= ws
->enable_exclusive
;
4523 ws
->enable_inclusive
= false;
4524 ws
->enable_exclusive
= ws
->enable_exclusive
|| enable_inclusive
;
4525 ac_build_wg_wavescan_top(ctx
, ws
);
4526 ws
->enable_inclusive
= enable_inclusive
;
4527 ws
->enable_exclusive
= enable_exclusive
;
4531 * "Bottom half" of a scan that reduces per-thread values across an entire
4534 * The caller must place a barrier between the top and bottom halves.
4537 ac_build_wg_scan_bottom(struct ac_llvm_context
*ctx
, struct ac_wg_scan
*ws
)
4539 bool enable_inclusive
= ws
->enable_inclusive
;
4540 bool enable_exclusive
= ws
->enable_exclusive
;
4541 ws
->enable_inclusive
= false;
4542 ws
->enable_exclusive
= ws
->enable_exclusive
|| enable_inclusive
;
4543 ac_build_wg_wavescan_bottom(ctx
, ws
);
4544 ws
->enable_inclusive
= enable_inclusive
;
4545 ws
->enable_exclusive
= enable_exclusive
;
4547 /* ws->result_reduce is already the correct value */
4548 if (ws
->enable_inclusive
)
4549 ws
->result_inclusive
= ac_build_alu_op(ctx
, ws
->result_inclusive
, ws
->src
, ws
->op
);
4550 if (ws
->enable_exclusive
)
4551 ws
->result_exclusive
= ac_build_alu_op(ctx
, ws
->result_exclusive
, ws
->extra
, ws
->op
);
4555 * A scan that reduces per-thread values across an entire workgroup.
4557 * The caller must ensure that all lanes are active when this code runs
4558 * (WWM is insufficient!), because there is an implied barrier.
4561 ac_build_wg_scan(struct ac_llvm_context
*ctx
, struct ac_wg_scan
*ws
)
4563 ac_build_wg_scan_top(ctx
, ws
);
4564 ac_build_s_barrier(ctx
);
4565 ac_build_wg_scan_bottom(ctx
, ws
);
4569 ac_build_quad_swizzle(struct ac_llvm_context
*ctx
, LLVMValueRef src
,
4570 unsigned lane0
, unsigned lane1
, unsigned lane2
, unsigned lane3
)
4572 unsigned mask
= dpp_quad_perm(lane0
, lane1
, lane2
, lane3
);
4573 if (ctx
->chip_class
>= GFX8
) {
4574 return ac_build_dpp(ctx
, src
, src
, mask
, 0xf, 0xf, false);
4576 return ac_build_ds_swizzle(ctx
, src
, (1 << 15) | mask
);
4581 ac_build_shuffle(struct ac_llvm_context
*ctx
, LLVMValueRef src
, LLVMValueRef index
)
4583 index
= LLVMBuildMul(ctx
->builder
, index
, LLVMConstInt(ctx
->i32
, 4, 0), "");
4584 return ac_build_intrinsic(ctx
,
4585 "llvm.amdgcn.ds.bpermute", ctx
->i32
,
4586 (LLVMValueRef
[]) {index
, src
}, 2,
4587 AC_FUNC_ATTR_READNONE
|
4588 AC_FUNC_ATTR_CONVERGENT
);
4592 ac_build_frexp_exp(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
4598 if (bitsize
== 16) {
4599 intr
= "llvm.amdgcn.frexp.exp.i16.f16";
4601 } else if (bitsize
== 32) {
4602 intr
= "llvm.amdgcn.frexp.exp.i32.f32";
4605 intr
= "llvm.amdgcn.frexp.exp.i32.f64";
4609 LLVMValueRef params
[] = {
4612 return ac_build_intrinsic(ctx
, intr
, type
, params
, 1,
4613 AC_FUNC_ATTR_READNONE
);
4616 ac_build_frexp_mant(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
4622 if (bitsize
== 16) {
4623 intr
= "llvm.amdgcn.frexp.mant.f16";
4625 } else if (bitsize
== 32) {
4626 intr
= "llvm.amdgcn.frexp.mant.f32";
4629 intr
= "llvm.amdgcn.frexp.mant.f64";
4633 LLVMValueRef params
[] = {
4636 return ac_build_intrinsic(ctx
, intr
, type
, params
, 1,
4637 AC_FUNC_ATTR_READNONE
);
4641 * this takes an I,J coordinate pair,
4642 * and works out the X and Y derivatives.
4643 * it returns DDX(I), DDX(J), DDY(I), DDY(J).
4646 ac_build_ddxy_interp(struct ac_llvm_context
*ctx
, LLVMValueRef interp_ij
)
4648 LLVMValueRef result
[4], a
;
4651 for (i
= 0; i
< 2; i
++) {
4652 a
= LLVMBuildExtractElement(ctx
->builder
, interp_ij
,
4653 LLVMConstInt(ctx
->i32
, i
, false), "");
4654 result
[i
] = ac_build_ddxy(ctx
, AC_TID_MASK_TOP_LEFT
, 1, a
);
4655 result
[2+i
] = ac_build_ddxy(ctx
, AC_TID_MASK_TOP_LEFT
, 2, a
);
4657 return ac_build_gather_values(ctx
, result
, 4);
4661 ac_build_load_helper_invocation(struct ac_llvm_context
*ctx
)
4663 LLVMValueRef result
= ac_build_intrinsic(ctx
, "llvm.amdgcn.ps.live",
4665 AC_FUNC_ATTR_READNONE
);
4666 result
= LLVMBuildNot(ctx
->builder
, result
, "");
4667 return LLVMBuildSExt(ctx
->builder
, result
, ctx
->i32
, "");
4670 LLVMValueRef
ac_build_call(struct ac_llvm_context
*ctx
, LLVMValueRef func
,
4671 LLVMValueRef
*args
, unsigned num_args
)
4673 LLVMValueRef ret
= LLVMBuildCall(ctx
->builder
, func
, args
, num_args
, "");
4674 LLVMSetInstructionCallConv(ret
, LLVMGetFunctionCallConv(func
));