2 * Copyright 2014 Advanced Micro Devices, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sub license, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
15 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
16 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
17 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
18 * USE OR OTHER DEALINGS IN THE SOFTWARE.
20 * The above copyright notice and this permission notice (including the
21 * next paragraph) shall be included in all copies or substantial portions
25 /* based on pieces from si_pipe.c and radeon_llvm_emit.c */
26 #include "ac_llvm_build.h"
28 #include <llvm-c/Core.h>
29 #include <llvm/Config/llvm-config.h>
31 #include "c11/threads.h"
36 #include "ac_llvm_util.h"
37 #include "ac_shader_util.h"
38 #include "ac_exp_param.h"
39 #include "util/bitscan.h"
40 #include "util/macros.h"
41 #include "util/u_atomic.h"
42 #include "util/u_math.h"
45 #include "shader_enums.h"
47 #define AC_LLVM_INITIAL_CF_DEPTH 4
49 /* Data for if/else/endif and bgnloop/endloop control flow structures.
52 /* Loop exit or next part of if/else/endif. */
53 LLVMBasicBlockRef next_block
;
54 LLVMBasicBlockRef loop_entry_block
;
57 /* Initialize module-independent parts of the context.
59 * The caller is responsible for initializing ctx::module and ctx::builder.
62 ac_llvm_context_init(struct ac_llvm_context
*ctx
,
63 struct ac_llvm_compiler
*compiler
,
64 enum chip_class chip_class
, enum radeon_family family
,
65 enum ac_float_mode float_mode
, unsigned wave_size
,
66 unsigned ballot_mask_bits
)
70 ctx
->context
= LLVMContextCreate();
72 ctx
->chip_class
= chip_class
;
74 ctx
->wave_size
= wave_size
;
75 ctx
->ballot_mask_bits
= ballot_mask_bits
;
76 ctx
->float_mode
= float_mode
;
77 ctx
->module
= ac_create_module(wave_size
== 32 ? compiler
->tm_wave32
80 ctx
->builder
= ac_create_builder(ctx
->context
, float_mode
);
82 ctx
->voidt
= LLVMVoidTypeInContext(ctx
->context
);
83 ctx
->i1
= LLVMInt1TypeInContext(ctx
->context
);
84 ctx
->i8
= LLVMInt8TypeInContext(ctx
->context
);
85 ctx
->i16
= LLVMIntTypeInContext(ctx
->context
, 16);
86 ctx
->i32
= LLVMIntTypeInContext(ctx
->context
, 32);
87 ctx
->i64
= LLVMIntTypeInContext(ctx
->context
, 64);
88 ctx
->intptr
= ctx
->i32
;
89 ctx
->f16
= LLVMHalfTypeInContext(ctx
->context
);
90 ctx
->f32
= LLVMFloatTypeInContext(ctx
->context
);
91 ctx
->f64
= LLVMDoubleTypeInContext(ctx
->context
);
92 ctx
->v2i16
= LLVMVectorType(ctx
->i16
, 2);
93 ctx
->v2i32
= LLVMVectorType(ctx
->i32
, 2);
94 ctx
->v3i32
= LLVMVectorType(ctx
->i32
, 3);
95 ctx
->v4i32
= LLVMVectorType(ctx
->i32
, 4);
96 ctx
->v2f32
= LLVMVectorType(ctx
->f32
, 2);
97 ctx
->v3f32
= LLVMVectorType(ctx
->f32
, 3);
98 ctx
->v4f32
= LLVMVectorType(ctx
->f32
, 4);
99 ctx
->v8i32
= LLVMVectorType(ctx
->i32
, 8);
100 ctx
->iN_wavemask
= LLVMIntTypeInContext(ctx
->context
, ctx
->wave_size
);
101 ctx
->iN_ballotmask
= LLVMIntTypeInContext(ctx
->context
, ballot_mask_bits
);
103 ctx
->i8_0
= LLVMConstInt(ctx
->i8
, 0, false);
104 ctx
->i8_1
= LLVMConstInt(ctx
->i8
, 1, false);
105 ctx
->i16_0
= LLVMConstInt(ctx
->i16
, 0, false);
106 ctx
->i16_1
= LLVMConstInt(ctx
->i16
, 1, false);
107 ctx
->i32_0
= LLVMConstInt(ctx
->i32
, 0, false);
108 ctx
->i32_1
= LLVMConstInt(ctx
->i32
, 1, false);
109 ctx
->i64_0
= LLVMConstInt(ctx
->i64
, 0, false);
110 ctx
->i64_1
= LLVMConstInt(ctx
->i64
, 1, false);
111 ctx
->f16_0
= LLVMConstReal(ctx
->f16
, 0.0);
112 ctx
->f16_1
= LLVMConstReal(ctx
->f16
, 1.0);
113 ctx
->f32_0
= LLVMConstReal(ctx
->f32
, 0.0);
114 ctx
->f32_1
= LLVMConstReal(ctx
->f32
, 1.0);
115 ctx
->f64_0
= LLVMConstReal(ctx
->f64
, 0.0);
116 ctx
->f64_1
= LLVMConstReal(ctx
->f64
, 1.0);
118 ctx
->i1false
= LLVMConstInt(ctx
->i1
, 0, false);
119 ctx
->i1true
= LLVMConstInt(ctx
->i1
, 1, false);
121 ctx
->range_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
124 ctx
->invariant_load_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
125 "invariant.load", 14);
127 ctx
->fpmath_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
, "fpmath", 6);
129 args
[0] = LLVMConstReal(ctx
->f32
, 2.5);
130 ctx
->fpmath_md_2p5_ulp
= LLVMMDNodeInContext(ctx
->context
, args
, 1);
132 ctx
->uniform_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
133 "amdgpu.uniform", 14);
135 ctx
->empty_md
= LLVMMDNodeInContext(ctx
->context
, NULL
, 0);
136 ctx
->flow
= calloc(1, sizeof(*ctx
->flow
));
140 ac_llvm_context_dispose(struct ac_llvm_context
*ctx
)
142 free(ctx
->flow
->stack
);
148 ac_get_llvm_num_components(LLVMValueRef value
)
150 LLVMTypeRef type
= LLVMTypeOf(value
);
151 unsigned num_components
= LLVMGetTypeKind(type
) == LLVMVectorTypeKind
152 ? LLVMGetVectorSize(type
)
154 return num_components
;
158 ac_llvm_extract_elem(struct ac_llvm_context
*ac
,
162 if (LLVMGetTypeKind(LLVMTypeOf(value
)) != LLVMVectorTypeKind
) {
167 return LLVMBuildExtractElement(ac
->builder
, value
,
168 LLVMConstInt(ac
->i32
, index
, false), "");
172 ac_get_elem_bits(struct ac_llvm_context
*ctx
, LLVMTypeRef type
)
174 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
)
175 type
= LLVMGetElementType(type
);
177 if (LLVMGetTypeKind(type
) == LLVMIntegerTypeKind
)
178 return LLVMGetIntTypeWidth(type
);
180 if (LLVMGetTypeKind(type
) == LLVMPointerTypeKind
) {
181 if (LLVMGetPointerAddressSpace(type
) == AC_ADDR_SPACE_LDS
)
185 if (type
== ctx
->f16
)
187 if (type
== ctx
->f32
)
189 if (type
== ctx
->f64
)
192 unreachable("Unhandled type kind in get_elem_bits");
196 ac_get_type_size(LLVMTypeRef type
)
198 LLVMTypeKind kind
= LLVMGetTypeKind(type
);
201 case LLVMIntegerTypeKind
:
202 return LLVMGetIntTypeWidth(type
) / 8;
203 case LLVMHalfTypeKind
:
205 case LLVMFloatTypeKind
:
207 case LLVMDoubleTypeKind
:
209 case LLVMPointerTypeKind
:
210 if (LLVMGetPointerAddressSpace(type
) == AC_ADDR_SPACE_CONST_32BIT
)
213 case LLVMVectorTypeKind
:
214 return LLVMGetVectorSize(type
) *
215 ac_get_type_size(LLVMGetElementType(type
));
216 case LLVMArrayTypeKind
:
217 return LLVMGetArrayLength(type
) *
218 ac_get_type_size(LLVMGetElementType(type
));
225 static LLVMTypeRef
to_integer_type_scalar(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
229 else if (t
== ctx
->f16
|| t
== ctx
->i16
)
231 else if (t
== ctx
->f32
|| t
== ctx
->i32
)
233 else if (t
== ctx
->f64
|| t
== ctx
->i64
)
236 unreachable("Unhandled integer size");
240 ac_to_integer_type(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
242 if (LLVMGetTypeKind(t
) == LLVMVectorTypeKind
) {
243 LLVMTypeRef elem_type
= LLVMGetElementType(t
);
244 return LLVMVectorType(to_integer_type_scalar(ctx
, elem_type
),
245 LLVMGetVectorSize(t
));
247 if (LLVMGetTypeKind(t
) == LLVMPointerTypeKind
) {
248 switch (LLVMGetPointerAddressSpace(t
)) {
249 case AC_ADDR_SPACE_GLOBAL
:
251 case AC_ADDR_SPACE_CONST_32BIT
:
252 case AC_ADDR_SPACE_LDS
:
255 unreachable("unhandled address space");
258 return to_integer_type_scalar(ctx
, t
);
262 ac_to_integer(struct ac_llvm_context
*ctx
, LLVMValueRef v
)
264 LLVMTypeRef type
= LLVMTypeOf(v
);
265 if (LLVMGetTypeKind(type
) == LLVMPointerTypeKind
) {
266 return LLVMBuildPtrToInt(ctx
->builder
, v
, ac_to_integer_type(ctx
, type
), "");
268 return LLVMBuildBitCast(ctx
->builder
, v
, ac_to_integer_type(ctx
, type
), "");
272 ac_to_integer_or_pointer(struct ac_llvm_context
*ctx
, LLVMValueRef v
)
274 LLVMTypeRef type
= LLVMTypeOf(v
);
275 if (LLVMGetTypeKind(type
) == LLVMPointerTypeKind
)
277 return ac_to_integer(ctx
, v
);
280 static LLVMTypeRef
to_float_type_scalar(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
284 else if (t
== ctx
->i16
|| t
== ctx
->f16
)
286 else if (t
== ctx
->i32
|| t
== ctx
->f32
)
288 else if (t
== ctx
->i64
|| t
== ctx
->f64
)
291 unreachable("Unhandled float size");
295 ac_to_float_type(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
297 if (LLVMGetTypeKind(t
) == LLVMVectorTypeKind
) {
298 LLVMTypeRef elem_type
= LLVMGetElementType(t
);
299 return LLVMVectorType(to_float_type_scalar(ctx
, elem_type
),
300 LLVMGetVectorSize(t
));
302 return to_float_type_scalar(ctx
, t
);
306 ac_to_float(struct ac_llvm_context
*ctx
, LLVMValueRef v
)
308 LLVMTypeRef type
= LLVMTypeOf(v
);
309 return LLVMBuildBitCast(ctx
->builder
, v
, ac_to_float_type(ctx
, type
), "");
314 ac_build_intrinsic(struct ac_llvm_context
*ctx
, const char *name
,
315 LLVMTypeRef return_type
, LLVMValueRef
*params
,
316 unsigned param_count
, unsigned attrib_mask
)
318 LLVMValueRef function
, call
;
319 bool set_callsite_attrs
= !(attrib_mask
& AC_FUNC_ATTR_LEGACY
);
321 function
= LLVMGetNamedFunction(ctx
->module
, name
);
323 LLVMTypeRef param_types
[32], function_type
;
326 assert(param_count
<= 32);
328 for (i
= 0; i
< param_count
; ++i
) {
330 param_types
[i
] = LLVMTypeOf(params
[i
]);
333 LLVMFunctionType(return_type
, param_types
, param_count
, 0);
334 function
= LLVMAddFunction(ctx
->module
, name
, function_type
);
336 LLVMSetFunctionCallConv(function
, LLVMCCallConv
);
337 LLVMSetLinkage(function
, LLVMExternalLinkage
);
339 if (!set_callsite_attrs
)
340 ac_add_func_attributes(ctx
->context
, function
, attrib_mask
);
343 call
= LLVMBuildCall(ctx
->builder
, function
, params
, param_count
, "");
344 if (set_callsite_attrs
)
345 ac_add_func_attributes(ctx
->context
, call
, attrib_mask
);
350 * Given the i32 or vNi32 \p type, generate the textual name (e.g. for use with
353 void ac_build_type_name_for_intr(LLVMTypeRef type
, char *buf
, unsigned bufsize
)
355 LLVMTypeRef elem_type
= type
;
357 assert(bufsize
>= 8);
359 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
) {
360 int ret
= snprintf(buf
, bufsize
, "v%u",
361 LLVMGetVectorSize(type
));
363 char *type_name
= LLVMPrintTypeToString(type
);
364 fprintf(stderr
, "Error building type name for: %s\n",
366 LLVMDisposeMessage(type_name
);
369 elem_type
= LLVMGetElementType(type
);
373 switch (LLVMGetTypeKind(elem_type
)) {
375 case LLVMIntegerTypeKind
:
376 snprintf(buf
, bufsize
, "i%d", LLVMGetIntTypeWidth(elem_type
));
378 case LLVMHalfTypeKind
:
379 snprintf(buf
, bufsize
, "f16");
381 case LLVMFloatTypeKind
:
382 snprintf(buf
, bufsize
, "f32");
384 case LLVMDoubleTypeKind
:
385 snprintf(buf
, bufsize
, "f64");
391 * Helper function that builds an LLVM IR PHI node and immediately adds
395 ac_build_phi(struct ac_llvm_context
*ctx
, LLVMTypeRef type
,
396 unsigned count_incoming
, LLVMValueRef
*values
,
397 LLVMBasicBlockRef
*blocks
)
399 LLVMValueRef phi
= LLVMBuildPhi(ctx
->builder
, type
, "");
400 LLVMAddIncoming(phi
, values
, blocks
, count_incoming
);
404 void ac_build_s_barrier(struct ac_llvm_context
*ctx
)
406 ac_build_intrinsic(ctx
, "llvm.amdgcn.s.barrier", ctx
->voidt
, NULL
,
407 0, AC_FUNC_ATTR_CONVERGENT
);
410 /* Prevent optimizations (at least of memory accesses) across the current
411 * point in the program by emitting empty inline assembly that is marked as
412 * having side effects.
414 * Optionally, a value can be passed through the inline assembly to prevent
415 * LLVM from hoisting calls to ReadNone functions.
418 ac_build_optimization_barrier(struct ac_llvm_context
*ctx
,
421 static int counter
= 0;
423 LLVMBuilderRef builder
= ctx
->builder
;
426 snprintf(code
, sizeof(code
), "; %d", p_atomic_inc_return(&counter
));
429 LLVMTypeRef ftype
= LLVMFunctionType(ctx
->voidt
, NULL
, 0, false);
430 LLVMValueRef inlineasm
= LLVMConstInlineAsm(ftype
, code
, "", true, false);
431 LLVMBuildCall(builder
, inlineasm
, NULL
, 0, "");
433 LLVMTypeRef ftype
= LLVMFunctionType(ctx
->i32
, &ctx
->i32
, 1, false);
434 LLVMValueRef inlineasm
= LLVMConstInlineAsm(ftype
, code
, "=v,0", true, false);
435 LLVMTypeRef type
= LLVMTypeOf(*pvgpr
);
436 unsigned bitsize
= ac_get_elem_bits(ctx
, type
);
437 LLVMValueRef vgpr
= *pvgpr
;
438 LLVMTypeRef vgpr_type
;
443 vgpr
= LLVMBuildZExt(ctx
->builder
, vgpr
, ctx
->i32
, "");
445 vgpr_type
= LLVMTypeOf(vgpr
);
446 vgpr_size
= ac_get_type_size(vgpr_type
);
448 assert(vgpr_size
% 4 == 0);
450 vgpr
= LLVMBuildBitCast(builder
, vgpr
, LLVMVectorType(ctx
->i32
, vgpr_size
/ 4), "");
451 vgpr0
= LLVMBuildExtractElement(builder
, vgpr
, ctx
->i32_0
, "");
452 vgpr0
= LLVMBuildCall(builder
, inlineasm
, &vgpr0
, 1, "");
453 vgpr
= LLVMBuildInsertElement(builder
, vgpr
, vgpr0
, ctx
->i32_0
, "");
454 vgpr
= LLVMBuildBitCast(builder
, vgpr
, vgpr_type
, "");
457 vgpr
= LLVMBuildTrunc(builder
, vgpr
, type
, "");
464 ac_build_shader_clock(struct ac_llvm_context
*ctx
)
466 const char *intr
= LLVM_VERSION_MAJOR
>= 9 && ctx
->chip_class
>= GFX8
?
467 "llvm.amdgcn.s.memrealtime" : "llvm.readcyclecounter";
468 LLVMValueRef tmp
= ac_build_intrinsic(ctx
, intr
, ctx
->i64
, NULL
, 0, 0);
469 return LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->v2i32
, "");
473 ac_build_ballot(struct ac_llvm_context
*ctx
,
478 if (LLVM_VERSION_MAJOR
>= 9) {
479 if (ctx
->wave_size
== 64)
480 name
= "llvm.amdgcn.icmp.i64.i32";
482 name
= "llvm.amdgcn.icmp.i32.i32";
484 name
= "llvm.amdgcn.icmp.i32";
486 LLVMValueRef args
[3] = {
489 LLVMConstInt(ctx
->i32
, LLVMIntNE
, 0)
492 /* We currently have no other way to prevent LLVM from lifting the icmp
493 * calls to a dominating basic block.
495 ac_build_optimization_barrier(ctx
, &args
[0]);
497 args
[0] = ac_to_integer(ctx
, args
[0]);
499 return ac_build_intrinsic(ctx
, name
, ctx
->iN_wavemask
, args
, 3,
500 AC_FUNC_ATTR_NOUNWIND
|
501 AC_FUNC_ATTR_READNONE
|
502 AC_FUNC_ATTR_CONVERGENT
);
505 LLVMValueRef
ac_get_i1_sgpr_mask(struct ac_llvm_context
*ctx
,
508 const char *name
= LLVM_VERSION_MAJOR
>= 9 ? "llvm.amdgcn.icmp.i64.i1" : "llvm.amdgcn.icmp.i1";
509 LLVMValueRef args
[3] = {
512 LLVMConstInt(ctx
->i32
, LLVMIntNE
, 0),
515 return ac_build_intrinsic(ctx
, name
, ctx
->i64
, args
, 3,
516 AC_FUNC_ATTR_NOUNWIND
|
517 AC_FUNC_ATTR_READNONE
|
518 AC_FUNC_ATTR_CONVERGENT
);
522 ac_build_vote_all(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
524 LLVMValueRef active_set
= ac_build_ballot(ctx
, ctx
->i32_1
);
525 LLVMValueRef vote_set
= ac_build_ballot(ctx
, value
);
526 return LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, vote_set
, active_set
, "");
530 ac_build_vote_any(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
532 LLVMValueRef vote_set
= ac_build_ballot(ctx
, value
);
533 return LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, vote_set
,
534 LLVMConstInt(ctx
->iN_wavemask
, 0, 0), "");
538 ac_build_vote_eq(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
540 LLVMValueRef active_set
= ac_build_ballot(ctx
, ctx
->i32_1
);
541 LLVMValueRef vote_set
= ac_build_ballot(ctx
, value
);
543 LLVMValueRef all
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
544 vote_set
, active_set
, "");
545 LLVMValueRef none
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
547 LLVMConstInt(ctx
->iN_wavemask
, 0, 0), "");
548 return LLVMBuildOr(ctx
->builder
, all
, none
, "");
552 ac_build_varying_gather_values(struct ac_llvm_context
*ctx
, LLVMValueRef
*values
,
553 unsigned value_count
, unsigned component
)
555 LLVMValueRef vec
= NULL
;
557 if (value_count
== 1) {
558 return values
[component
];
559 } else if (!value_count
)
560 unreachable("value_count is 0");
562 for (unsigned i
= component
; i
< value_count
+ component
; i
++) {
563 LLVMValueRef value
= values
[i
];
566 vec
= LLVMGetUndef( LLVMVectorType(LLVMTypeOf(value
), value_count
));
567 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, i
- component
, false);
568 vec
= LLVMBuildInsertElement(ctx
->builder
, vec
, value
, index
, "");
574 ac_build_gather_values_extended(struct ac_llvm_context
*ctx
,
575 LLVMValueRef
*values
,
576 unsigned value_count
,
577 unsigned value_stride
,
581 LLVMBuilderRef builder
= ctx
->builder
;
582 LLVMValueRef vec
= NULL
;
585 if (value_count
== 1 && !always_vector
) {
587 return LLVMBuildLoad(builder
, values
[0], "");
589 } else if (!value_count
)
590 unreachable("value_count is 0");
592 for (i
= 0; i
< value_count
; i
++) {
593 LLVMValueRef value
= values
[i
* value_stride
];
595 value
= LLVMBuildLoad(builder
, value
, "");
598 vec
= LLVMGetUndef( LLVMVectorType(LLVMTypeOf(value
), value_count
));
599 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, i
, false);
600 vec
= LLVMBuildInsertElement(builder
, vec
, value
, index
, "");
606 ac_build_gather_values(struct ac_llvm_context
*ctx
,
607 LLVMValueRef
*values
,
608 unsigned value_count
)
610 return ac_build_gather_values_extended(ctx
, values
, value_count
, 1, false, false);
613 /* Expand a scalar or vector to <dst_channels x type> by filling the remaining
614 * channels with undef. Extract at most src_channels components from the input.
617 ac_build_expand(struct ac_llvm_context
*ctx
,
619 unsigned src_channels
,
620 unsigned dst_channels
)
622 LLVMTypeRef elemtype
;
623 LLVMValueRef chan
[dst_channels
];
625 if (LLVMGetTypeKind(LLVMTypeOf(value
)) == LLVMVectorTypeKind
) {
626 unsigned vec_size
= LLVMGetVectorSize(LLVMTypeOf(value
));
628 if (src_channels
== dst_channels
&& vec_size
== dst_channels
)
631 src_channels
= MIN2(src_channels
, vec_size
);
633 for (unsigned i
= 0; i
< src_channels
; i
++)
634 chan
[i
] = ac_llvm_extract_elem(ctx
, value
, i
);
636 elemtype
= LLVMGetElementType(LLVMTypeOf(value
));
639 assert(src_channels
== 1);
642 elemtype
= LLVMTypeOf(value
);
645 for (unsigned i
= src_channels
; i
< dst_channels
; i
++)
646 chan
[i
] = LLVMGetUndef(elemtype
);
648 return ac_build_gather_values(ctx
, chan
, dst_channels
);
651 /* Extract components [start, start + channels) from a vector.
654 ac_extract_components(struct ac_llvm_context
*ctx
,
659 LLVMValueRef chan
[channels
];
661 for (unsigned i
= 0; i
< channels
; i
++)
662 chan
[i
] = ac_llvm_extract_elem(ctx
, value
, i
+ start
);
664 return ac_build_gather_values(ctx
, chan
, channels
);
667 /* Expand a scalar or vector to <4 x type> by filling the remaining channels
668 * with undef. Extract at most num_channels components from the input.
670 LLVMValueRef
ac_build_expand_to_vec4(struct ac_llvm_context
*ctx
,
672 unsigned num_channels
)
674 return ac_build_expand(ctx
, value
, num_channels
, 4);
677 LLVMValueRef
ac_build_round(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
679 unsigned type_size
= ac_get_type_size(LLVMTypeOf(value
));
683 name
= "llvm.rint.f16";
684 else if (type_size
== 4)
685 name
= "llvm.rint.f32";
687 name
= "llvm.rint.f64";
689 return ac_build_intrinsic(ctx
, name
, LLVMTypeOf(value
), &value
, 1,
690 AC_FUNC_ATTR_READNONE
);
694 ac_build_fdiv(struct ac_llvm_context
*ctx
,
698 /* If we do (num / den), LLVM >= 7.0 does:
699 * return num * v_rcp_f32(den * (fabs(den) > 0x1.0p+96f ? 0x1.0p-32f : 1.0f));
701 * If we do (num * (1 / den)), LLVM does:
702 * return num * v_rcp_f32(den);
704 LLVMValueRef one
= LLVMConstReal(LLVMTypeOf(num
), 1.0);
705 LLVMValueRef rcp
= LLVMBuildFDiv(ctx
->builder
, one
, den
, "");
706 LLVMValueRef ret
= LLVMBuildFMul(ctx
->builder
, num
, rcp
, "");
708 /* Use v_rcp_f32 instead of precise division. */
709 if (!LLVMIsConstant(ret
))
710 LLVMSetMetadata(ret
, ctx
->fpmath_md_kind
, ctx
->fpmath_md_2p5_ulp
);
714 /* See fast_idiv_by_const.h. */
715 /* Set: increment = util_fast_udiv_info::increment ? multiplier : 0; */
716 LLVMValueRef
ac_build_fast_udiv(struct ac_llvm_context
*ctx
,
718 LLVMValueRef multiplier
,
719 LLVMValueRef pre_shift
,
720 LLVMValueRef post_shift
,
721 LLVMValueRef increment
)
723 LLVMBuilderRef builder
= ctx
->builder
;
725 num
= LLVMBuildLShr(builder
, num
, pre_shift
, "");
726 num
= LLVMBuildMul(builder
,
727 LLVMBuildZExt(builder
, num
, ctx
->i64
, ""),
728 LLVMBuildZExt(builder
, multiplier
, ctx
->i64
, ""), "");
729 num
= LLVMBuildAdd(builder
, num
,
730 LLVMBuildZExt(builder
, increment
, ctx
->i64
, ""), "");
731 num
= LLVMBuildLShr(builder
, num
, LLVMConstInt(ctx
->i64
, 32, 0), "");
732 num
= LLVMBuildTrunc(builder
, num
, ctx
->i32
, "");
733 return LLVMBuildLShr(builder
, num
, post_shift
, "");
736 /* See fast_idiv_by_const.h. */
737 /* If num != UINT_MAX, this more efficient version can be used. */
738 /* Set: increment = util_fast_udiv_info::increment; */
739 LLVMValueRef
ac_build_fast_udiv_nuw(struct ac_llvm_context
*ctx
,
741 LLVMValueRef multiplier
,
742 LLVMValueRef pre_shift
,
743 LLVMValueRef post_shift
,
744 LLVMValueRef increment
)
746 LLVMBuilderRef builder
= ctx
->builder
;
748 num
= LLVMBuildLShr(builder
, num
, pre_shift
, "");
749 num
= LLVMBuildNUWAdd(builder
, num
, increment
, "");
750 num
= LLVMBuildMul(builder
,
751 LLVMBuildZExt(builder
, num
, ctx
->i64
, ""),
752 LLVMBuildZExt(builder
, multiplier
, ctx
->i64
, ""), "");
753 num
= LLVMBuildLShr(builder
, num
, LLVMConstInt(ctx
->i64
, 32, 0), "");
754 num
= LLVMBuildTrunc(builder
, num
, ctx
->i32
, "");
755 return LLVMBuildLShr(builder
, num
, post_shift
, "");
758 /* See fast_idiv_by_const.h. */
759 /* Both operands must fit in 31 bits and the divisor must not be 1. */
760 LLVMValueRef
ac_build_fast_udiv_u31_d_not_one(struct ac_llvm_context
*ctx
,
762 LLVMValueRef multiplier
,
763 LLVMValueRef post_shift
)
765 LLVMBuilderRef builder
= ctx
->builder
;
767 num
= LLVMBuildMul(builder
,
768 LLVMBuildZExt(builder
, num
, ctx
->i64
, ""),
769 LLVMBuildZExt(builder
, multiplier
, ctx
->i64
, ""), "");
770 num
= LLVMBuildLShr(builder
, num
, LLVMConstInt(ctx
->i64
, 32, 0), "");
771 num
= LLVMBuildTrunc(builder
, num
, ctx
->i32
, "");
772 return LLVMBuildLShr(builder
, num
, post_shift
, "");
775 /* Coordinates for cube map selection. sc, tc, and ma are as in Table 8.27
776 * of the OpenGL 4.5 (Compatibility Profile) specification, except ma is
777 * already multiplied by two. id is the cube face number.
779 struct cube_selection_coords
{
786 build_cube_intrinsic(struct ac_llvm_context
*ctx
,
788 struct cube_selection_coords
*out
)
790 LLVMTypeRef f32
= ctx
->f32
;
792 out
->stc
[1] = ac_build_intrinsic(ctx
, "llvm.amdgcn.cubetc",
793 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
794 out
->stc
[0] = ac_build_intrinsic(ctx
, "llvm.amdgcn.cubesc",
795 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
796 out
->ma
= ac_build_intrinsic(ctx
, "llvm.amdgcn.cubema",
797 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
798 out
->id
= ac_build_intrinsic(ctx
, "llvm.amdgcn.cubeid",
799 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
803 * Build a manual selection sequence for cube face sc/tc coordinates and
804 * major axis vector (multiplied by 2 for consistency) for the given
805 * vec3 \p coords, for the face implied by \p selcoords.
807 * For the major axis, we always adjust the sign to be in the direction of
808 * selcoords.ma; i.e., a positive out_ma means that coords is pointed towards
809 * the selcoords major axis.
811 static void build_cube_select(struct ac_llvm_context
*ctx
,
812 const struct cube_selection_coords
*selcoords
,
813 const LLVMValueRef
*coords
,
814 LLVMValueRef
*out_st
,
815 LLVMValueRef
*out_ma
)
817 LLVMBuilderRef builder
= ctx
->builder
;
818 LLVMTypeRef f32
= LLVMTypeOf(coords
[0]);
819 LLVMValueRef is_ma_positive
;
821 LLVMValueRef is_ma_z
, is_not_ma_z
;
822 LLVMValueRef is_ma_y
;
823 LLVMValueRef is_ma_x
;
827 is_ma_positive
= LLVMBuildFCmp(builder
, LLVMRealUGE
,
828 selcoords
->ma
, LLVMConstReal(f32
, 0.0), "");
829 sgn_ma
= LLVMBuildSelect(builder
, is_ma_positive
,
830 LLVMConstReal(f32
, 1.0), LLVMConstReal(f32
, -1.0), "");
832 is_ma_z
= LLVMBuildFCmp(builder
, LLVMRealUGE
, selcoords
->id
, LLVMConstReal(f32
, 4.0), "");
833 is_not_ma_z
= LLVMBuildNot(builder
, is_ma_z
, "");
834 is_ma_y
= LLVMBuildAnd(builder
, is_not_ma_z
,
835 LLVMBuildFCmp(builder
, LLVMRealUGE
, selcoords
->id
, LLVMConstReal(f32
, 2.0), ""), "");
836 is_ma_x
= LLVMBuildAnd(builder
, is_not_ma_z
, LLVMBuildNot(builder
, is_ma_y
, ""), "");
839 tmp
= LLVMBuildSelect(builder
, is_ma_x
, coords
[2], coords
[0], "");
840 sgn
= LLVMBuildSelect(builder
, is_ma_y
, LLVMConstReal(f32
, 1.0),
841 LLVMBuildSelect(builder
, is_ma_z
, sgn_ma
,
842 LLVMBuildFNeg(builder
, sgn_ma
, ""), ""), "");
843 out_st
[0] = LLVMBuildFMul(builder
, tmp
, sgn
, "");
846 tmp
= LLVMBuildSelect(builder
, is_ma_y
, coords
[2], coords
[1], "");
847 sgn
= LLVMBuildSelect(builder
, is_ma_y
, sgn_ma
,
848 LLVMConstReal(f32
, -1.0), "");
849 out_st
[1] = LLVMBuildFMul(builder
, tmp
, sgn
, "");
852 tmp
= LLVMBuildSelect(builder
, is_ma_z
, coords
[2],
853 LLVMBuildSelect(builder
, is_ma_y
, coords
[1], coords
[0], ""), "");
854 tmp
= ac_build_intrinsic(ctx
, "llvm.fabs.f32",
855 ctx
->f32
, &tmp
, 1, AC_FUNC_ATTR_READNONE
);
856 *out_ma
= LLVMBuildFMul(builder
, tmp
, LLVMConstReal(f32
, 2.0), "");
860 ac_prepare_cube_coords(struct ac_llvm_context
*ctx
,
861 bool is_deriv
, bool is_array
, bool is_lod
,
862 LLVMValueRef
*coords_arg
,
863 LLVMValueRef
*derivs_arg
)
866 LLVMBuilderRef builder
= ctx
->builder
;
867 struct cube_selection_coords selcoords
;
868 LLVMValueRef coords
[3];
871 if (is_array
&& !is_lod
) {
872 LLVMValueRef tmp
= ac_build_round(ctx
, coords_arg
[3]);
874 /* Section 8.9 (Texture Functions) of the GLSL 4.50 spec says:
876 * "For Array forms, the array layer used will be
878 * max(0, min(d−1, floor(layer+0.5)))
880 * where d is the depth of the texture array and layer
881 * comes from the component indicated in the tables below.
882 * Workaroudn for an issue where the layer is taken from a
883 * helper invocation which happens to fall on a different
884 * layer due to extrapolation."
886 * GFX8 and earlier attempt to implement this in hardware by
887 * clamping the value of coords[2] = (8 * layer) + face.
888 * Unfortunately, this means that the we end up with the wrong
889 * face when clamping occurs.
891 * Clamp the layer earlier to work around the issue.
893 if (ctx
->chip_class
<= GFX8
) {
895 ge0
= LLVMBuildFCmp(builder
, LLVMRealOGE
, tmp
, ctx
->f32_0
, "");
896 tmp
= LLVMBuildSelect(builder
, ge0
, tmp
, ctx
->f32_0
, "");
902 build_cube_intrinsic(ctx
, coords_arg
, &selcoords
);
904 invma
= ac_build_intrinsic(ctx
, "llvm.fabs.f32",
905 ctx
->f32
, &selcoords
.ma
, 1, AC_FUNC_ATTR_READNONE
);
906 invma
= ac_build_fdiv(ctx
, LLVMConstReal(ctx
->f32
, 1.0), invma
);
908 for (int i
= 0; i
< 2; ++i
)
909 coords
[i
] = LLVMBuildFMul(builder
, selcoords
.stc
[i
], invma
, "");
911 coords
[2] = selcoords
.id
;
913 if (is_deriv
&& derivs_arg
) {
914 LLVMValueRef derivs
[4];
917 /* Convert cube derivatives to 2D derivatives. */
918 for (axis
= 0; axis
< 2; axis
++) {
919 LLVMValueRef deriv_st
[2];
920 LLVMValueRef deriv_ma
;
922 /* Transform the derivative alongside the texture
923 * coordinate. Mathematically, the correct formula is
924 * as follows. Assume we're projecting onto the +Z face
925 * and denote by dx/dh the derivative of the (original)
926 * X texture coordinate with respect to horizontal
927 * window coordinates. The projection onto the +Z face
932 * Then df/dh = df/dx * dx/dh + df/dz * dz/dh
933 * = 1/z * dx/dh - x/z * 1/z * dz/dh.
935 * This motivatives the implementation below.
937 * Whether this actually gives the expected results for
938 * apps that might feed in derivatives obtained via
939 * finite differences is anyone's guess. The OpenGL spec
940 * seems awfully quiet about how textureGrad for cube
941 * maps should be handled.
943 build_cube_select(ctx
, &selcoords
, &derivs_arg
[axis
* 3],
944 deriv_st
, &deriv_ma
);
946 deriv_ma
= LLVMBuildFMul(builder
, deriv_ma
, invma
, "");
948 for (int i
= 0; i
< 2; ++i
)
949 derivs
[axis
* 2 + i
] =
950 LLVMBuildFSub(builder
,
951 LLVMBuildFMul(builder
, deriv_st
[i
], invma
, ""),
952 LLVMBuildFMul(builder
, deriv_ma
, coords
[i
], ""), "");
955 memcpy(derivs_arg
, derivs
, sizeof(derivs
));
958 /* Shift the texture coordinate. This must be applied after the
959 * derivative calculation.
961 for (int i
= 0; i
< 2; ++i
)
962 coords
[i
] = LLVMBuildFAdd(builder
, coords
[i
], LLVMConstReal(ctx
->f32
, 1.5), "");
965 /* for cube arrays coord.z = coord.w(array_index) * 8 + face */
966 /* coords_arg.w component - array_index for cube arrays */
967 coords
[2] = ac_build_fmad(ctx
, coords_arg
[3], LLVMConstReal(ctx
->f32
, 8.0), coords
[2]);
970 memcpy(coords_arg
, coords
, sizeof(coords
));
975 ac_build_fs_interp(struct ac_llvm_context
*ctx
,
976 LLVMValueRef llvm_chan
,
977 LLVMValueRef attr_number
,
982 LLVMValueRef args
[5];
987 args
[2] = attr_number
;
990 p1
= ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p1",
991 ctx
->f32
, args
, 4, AC_FUNC_ATTR_READNONE
);
996 args
[3] = attr_number
;
999 return ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p2",
1000 ctx
->f32
, args
, 5, AC_FUNC_ATTR_READNONE
);
1004 ac_build_fs_interp_f16(struct ac_llvm_context
*ctx
,
1005 LLVMValueRef llvm_chan
,
1006 LLVMValueRef attr_number
,
1007 LLVMValueRef params
,
1011 LLVMValueRef args
[6];
1015 args
[1] = llvm_chan
;
1016 args
[2] = attr_number
;
1017 args
[3] = ctx
->i1false
;
1020 p1
= ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p1.f16",
1021 ctx
->f32
, args
, 5, AC_FUNC_ATTR_READNONE
);
1025 args
[2] = llvm_chan
;
1026 args
[3] = attr_number
;
1027 args
[4] = ctx
->i1false
;
1030 return ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p2.f16",
1031 ctx
->f16
, args
, 6, AC_FUNC_ATTR_READNONE
);
1035 ac_build_fs_interp_mov(struct ac_llvm_context
*ctx
,
1036 LLVMValueRef parameter
,
1037 LLVMValueRef llvm_chan
,
1038 LLVMValueRef attr_number
,
1039 LLVMValueRef params
)
1041 LLVMValueRef args
[4];
1043 args
[0] = parameter
;
1044 args
[1] = llvm_chan
;
1045 args
[2] = attr_number
;
1048 return ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.mov",
1049 ctx
->f32
, args
, 4, AC_FUNC_ATTR_READNONE
);
1053 ac_build_gep_ptr(struct ac_llvm_context
*ctx
,
1054 LLVMValueRef base_ptr
,
1057 return LLVMBuildGEP(ctx
->builder
, base_ptr
, &index
, 1, "");
1061 ac_build_gep0(struct ac_llvm_context
*ctx
,
1062 LLVMValueRef base_ptr
,
1065 LLVMValueRef indices
[2] = {
1069 return LLVMBuildGEP(ctx
->builder
, base_ptr
, indices
, 2, "");
1072 LLVMValueRef
ac_build_pointer_add(struct ac_llvm_context
*ctx
, LLVMValueRef ptr
,
1075 return LLVMBuildPointerCast(ctx
->builder
,
1076 LLVMBuildGEP(ctx
->builder
, ptr
, &index
, 1, ""),
1077 LLVMTypeOf(ptr
), "");
1081 ac_build_indexed_store(struct ac_llvm_context
*ctx
,
1082 LLVMValueRef base_ptr
, LLVMValueRef index
,
1085 LLVMBuildStore(ctx
->builder
, value
,
1086 ac_build_gep0(ctx
, base_ptr
, index
));
1090 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad.
1091 * It's equivalent to doing a load from &base_ptr[index].
1093 * \param base_ptr Where the array starts.
1094 * \param index The element index into the array.
1095 * \param uniform Whether the base_ptr and index can be assumed to be
1096 * dynamically uniform (i.e. load to an SGPR)
1097 * \param invariant Whether the load is invariant (no other opcodes affect it)
1098 * \param no_unsigned_wraparound
1099 * For all possible re-associations and re-distributions of an expression
1100 * "base_ptr + index * elemsize" into "addr + offset" (excluding GEPs
1101 * without inbounds in base_ptr), this parameter is true if "addr + offset"
1102 * does not result in an unsigned integer wraparound. This is used for
1103 * optimal code generation of 32-bit pointer arithmetic.
1105 * For example, a 32-bit immediate offset that causes a 32-bit unsigned
1106 * integer wraparound can't be an imm offset in s_load_dword, because
1107 * the instruction performs "addr + offset" in 64 bits.
1109 * Expected usage for bindless textures by chaining GEPs:
1110 * // possible unsigned wraparound, don't use InBounds:
1111 * ptr1 = LLVMBuildGEP(base_ptr, index);
1112 * image = load(ptr1); // becomes "s_load ptr1, 0"
1114 * ptr2 = LLVMBuildInBoundsGEP(ptr1, 32 / elemsize);
1115 * sampler = load(ptr2); // becomes "s_load ptr1, 32" thanks to InBounds
1118 ac_build_load_custom(struct ac_llvm_context
*ctx
, LLVMValueRef base_ptr
,
1119 LLVMValueRef index
, bool uniform
, bool invariant
,
1120 bool no_unsigned_wraparound
)
1122 LLVMValueRef pointer
, result
;
1124 if (no_unsigned_wraparound
&&
1125 LLVMGetPointerAddressSpace(LLVMTypeOf(base_ptr
)) == AC_ADDR_SPACE_CONST_32BIT
)
1126 pointer
= LLVMBuildInBoundsGEP(ctx
->builder
, base_ptr
, &index
, 1, "");
1128 pointer
= LLVMBuildGEP(ctx
->builder
, base_ptr
, &index
, 1, "");
1131 LLVMSetMetadata(pointer
, ctx
->uniform_md_kind
, ctx
->empty_md
);
1132 result
= LLVMBuildLoad(ctx
->builder
, pointer
, "");
1134 LLVMSetMetadata(result
, ctx
->invariant_load_md_kind
, ctx
->empty_md
);
1138 LLVMValueRef
ac_build_load(struct ac_llvm_context
*ctx
, LLVMValueRef base_ptr
,
1141 return ac_build_load_custom(ctx
, base_ptr
, index
, false, false, false);
1144 LLVMValueRef
ac_build_load_invariant(struct ac_llvm_context
*ctx
,
1145 LLVMValueRef base_ptr
, LLVMValueRef index
)
1147 return ac_build_load_custom(ctx
, base_ptr
, index
, false, true, false);
1150 /* This assumes that there is no unsigned integer wraparound during the address
1151 * computation, excluding all GEPs within base_ptr. */
1152 LLVMValueRef
ac_build_load_to_sgpr(struct ac_llvm_context
*ctx
,
1153 LLVMValueRef base_ptr
, LLVMValueRef index
)
1155 return ac_build_load_custom(ctx
, base_ptr
, index
, true, true, true);
1158 /* See ac_build_load_custom() documentation. */
1159 LLVMValueRef
ac_build_load_to_sgpr_uint_wraparound(struct ac_llvm_context
*ctx
,
1160 LLVMValueRef base_ptr
, LLVMValueRef index
)
1162 return ac_build_load_custom(ctx
, base_ptr
, index
, true, true, false);
1165 static unsigned get_load_cache_policy(struct ac_llvm_context
*ctx
,
1166 unsigned cache_policy
)
1168 return cache_policy
|
1169 (ctx
->chip_class
>= GFX10
&& cache_policy
& ac_glc
? ac_dlc
: 0);
1173 ac_build_buffer_store_common(struct ac_llvm_context
*ctx
,
1176 LLVMValueRef vindex
,
1177 LLVMValueRef voffset
,
1178 LLVMValueRef soffset
,
1179 unsigned num_channels
,
1180 LLVMTypeRef return_channel_type
,
1181 unsigned cache_policy
,
1185 LLVMValueRef args
[6];
1188 args
[idx
++] = LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, "");
1190 args
[idx
++] = vindex
? vindex
: ctx
->i32_0
;
1191 args
[idx
++] = voffset
? voffset
: ctx
->i32_0
;
1192 args
[idx
++] = soffset
? soffset
: ctx
->i32_0
;
1193 args
[idx
++] = LLVMConstInt(ctx
->i32
, cache_policy
, 0);
1194 unsigned func
= !ac_has_vec3_support(ctx
->chip_class
, use_format
) && num_channels
== 3 ? 4 : num_channels
;
1195 const char *indexing_kind
= structurized
? "struct" : "raw";
1196 char name
[256], type_name
[8];
1198 LLVMTypeRef type
= func
> 1 ? LLVMVectorType(return_channel_type
, func
) : return_channel_type
;
1199 ac_build_type_name_for_intr(type
, type_name
, sizeof(type_name
));
1202 snprintf(name
, sizeof(name
), "llvm.amdgcn.%s.buffer.store.format.%s",
1203 indexing_kind
, type_name
);
1205 snprintf(name
, sizeof(name
), "llvm.amdgcn.%s.buffer.store.%s",
1206 indexing_kind
, type_name
);
1209 ac_build_intrinsic(ctx
, name
, ctx
->voidt
, args
, idx
,
1210 AC_FUNC_ATTR_INACCESSIBLE_MEM_ONLY
);
1214 ac_build_buffer_store_format(struct ac_llvm_context
*ctx
,
1217 LLVMValueRef vindex
,
1218 LLVMValueRef voffset
,
1219 unsigned num_channels
,
1220 unsigned cache_policy
)
1222 ac_build_buffer_store_common(ctx
, rsrc
, data
, vindex
,
1223 voffset
, NULL
, num_channels
,
1224 ctx
->f32
, cache_policy
,
1228 /* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
1229 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
1230 * or v4i32 (num_channels=3,4).
1233 ac_build_buffer_store_dword(struct ac_llvm_context
*ctx
,
1236 unsigned num_channels
,
1237 LLVMValueRef voffset
,
1238 LLVMValueRef soffset
,
1239 unsigned inst_offset
,
1240 unsigned cache_policy
,
1241 bool swizzle_enable_hint
)
1243 /* Split 3 channel stores, because only LLVM 9+ support 3-channel
1245 if (num_channels
== 3 && !ac_has_vec3_support(ctx
->chip_class
, false)) {
1246 LLVMValueRef v
[3], v01
;
1248 for (int i
= 0; i
< 3; i
++) {
1249 v
[i
] = LLVMBuildExtractElement(ctx
->builder
, vdata
,
1250 LLVMConstInt(ctx
->i32
, i
, 0), "");
1252 v01
= ac_build_gather_values(ctx
, v
, 2);
1254 ac_build_buffer_store_dword(ctx
, rsrc
, v01
, 2, voffset
,
1255 soffset
, inst_offset
, cache_policy
,
1256 swizzle_enable_hint
);
1257 ac_build_buffer_store_dword(ctx
, rsrc
, v
[2], 1, voffset
,
1258 soffset
, inst_offset
+ 8,
1260 swizzle_enable_hint
);
1264 /* SWIZZLE_ENABLE requires that soffset isn't folded into voffset
1265 * (voffset is swizzled, but soffset isn't swizzled).
1266 * llvm.amdgcn.buffer.store doesn't have a separate soffset parameter.
1268 if (!swizzle_enable_hint
) {
1269 LLVMValueRef offset
= soffset
;
1272 offset
= LLVMBuildAdd(ctx
->builder
, offset
,
1273 LLVMConstInt(ctx
->i32
, inst_offset
, 0), "");
1275 ac_build_buffer_store_common(ctx
, rsrc
, ac_to_float(ctx
, vdata
),
1276 ctx
->i32_0
, voffset
, offset
,
1277 num_channels
, ctx
->f32
,
1278 cache_policy
, false, false);
1282 static const unsigned dfmts
[] = {
1283 V_008F0C_BUF_DATA_FORMAT_32
,
1284 V_008F0C_BUF_DATA_FORMAT_32_32
,
1285 V_008F0C_BUF_DATA_FORMAT_32_32_32
,
1286 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
1288 unsigned dfmt
= dfmts
[num_channels
- 1];
1289 unsigned nfmt
= V_008F0C_BUF_NUM_FORMAT_UINT
;
1290 LLVMValueRef immoffset
= LLVMConstInt(ctx
->i32
, inst_offset
, 0);
1292 ac_build_raw_tbuffer_store(ctx
, rsrc
, vdata
, voffset
, soffset
,
1293 immoffset
, num_channels
, dfmt
, nfmt
, cache_policy
);
1297 ac_build_buffer_load_common(struct ac_llvm_context
*ctx
,
1299 LLVMValueRef vindex
,
1300 LLVMValueRef voffset
,
1301 LLVMValueRef soffset
,
1302 unsigned num_channels
,
1303 LLVMTypeRef channel_type
,
1304 unsigned cache_policy
,
1309 LLVMValueRef args
[5];
1311 args
[idx
++] = LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, "");
1313 args
[idx
++] = vindex
? vindex
: ctx
->i32_0
;
1314 args
[idx
++] = voffset
? voffset
: ctx
->i32_0
;
1315 args
[idx
++] = soffset
? soffset
: ctx
->i32_0
;
1316 args
[idx
++] = LLVMConstInt(ctx
->i32
, get_load_cache_policy(ctx
, cache_policy
), 0);
1317 unsigned func
= !ac_has_vec3_support(ctx
->chip_class
, use_format
) && num_channels
== 3 ? 4 : num_channels
;
1318 const char *indexing_kind
= structurized
? "struct" : "raw";
1319 char name
[256], type_name
[8];
1321 LLVMTypeRef type
= func
> 1 ? LLVMVectorType(channel_type
, func
) : channel_type
;
1322 ac_build_type_name_for_intr(type
, type_name
, sizeof(type_name
));
1325 snprintf(name
, sizeof(name
), "llvm.amdgcn.%s.buffer.load.format.%s",
1326 indexing_kind
, type_name
);
1328 snprintf(name
, sizeof(name
), "llvm.amdgcn.%s.buffer.load.%s",
1329 indexing_kind
, type_name
);
1332 return ac_build_intrinsic(ctx
, name
, type
, args
, idx
,
1333 ac_get_load_intr_attribs(can_speculate
));
1337 ac_build_buffer_load(struct ac_llvm_context
*ctx
,
1340 LLVMValueRef vindex
,
1341 LLVMValueRef voffset
,
1342 LLVMValueRef soffset
,
1343 unsigned inst_offset
,
1344 unsigned cache_policy
,
1348 LLVMValueRef offset
= LLVMConstInt(ctx
->i32
, inst_offset
, 0);
1350 offset
= LLVMBuildAdd(ctx
->builder
, offset
, voffset
, "");
1352 offset
= LLVMBuildAdd(ctx
->builder
, offset
, soffset
, "");
1354 if (allow_smem
&& !(cache_policy
& ac_slc
) &&
1355 (!(cache_policy
& ac_glc
) || ctx
->chip_class
>= GFX8
)) {
1356 assert(vindex
== NULL
);
1358 LLVMValueRef result
[8];
1360 for (int i
= 0; i
< num_channels
; i
++) {
1362 offset
= LLVMBuildAdd(ctx
->builder
, offset
,
1363 LLVMConstInt(ctx
->i32
, 4, 0), "");
1365 LLVMValueRef args
[3] = {
1368 LLVMConstInt(ctx
->i32
, get_load_cache_policy(ctx
, cache_policy
), 0),
1370 result
[i
] = ac_build_intrinsic(ctx
,
1371 "llvm.amdgcn.s.buffer.load.f32",
1373 AC_FUNC_ATTR_READNONE
);
1375 if (num_channels
== 1)
1378 if (num_channels
== 3 && !ac_has_vec3_support(ctx
->chip_class
, false))
1379 result
[num_channels
++] = LLVMGetUndef(ctx
->f32
);
1380 return ac_build_gather_values(ctx
, result
, num_channels
);
1383 return ac_build_buffer_load_common(ctx
, rsrc
, vindex
,
1385 num_channels
, ctx
->f32
,
1387 can_speculate
, false, false);
1390 LLVMValueRef
ac_build_buffer_load_format(struct ac_llvm_context
*ctx
,
1392 LLVMValueRef vindex
,
1393 LLVMValueRef voffset
,
1394 unsigned num_channels
,
1395 unsigned cache_policy
,
1398 return ac_build_buffer_load_common(ctx
, rsrc
, vindex
, voffset
,
1399 ctx
->i32_0
, num_channels
, ctx
->f32
,
1400 cache_policy
, can_speculate
,
1405 ac_build_tbuffer_load(struct ac_llvm_context
*ctx
,
1407 LLVMValueRef vindex
,
1408 LLVMValueRef voffset
,
1409 LLVMValueRef soffset
,
1410 LLVMValueRef immoffset
,
1411 unsigned num_channels
,
1414 unsigned cache_policy
,
1418 voffset
= LLVMBuildAdd(ctx
->builder
, voffset
, immoffset
, "");
1420 LLVMValueRef args
[6];
1422 args
[idx
++] = LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, "");
1424 args
[idx
++] = vindex
? vindex
: ctx
->i32_0
;
1425 args
[idx
++] = voffset
? voffset
: ctx
->i32_0
;
1426 args
[idx
++] = soffset
? soffset
: ctx
->i32_0
;
1427 args
[idx
++] = LLVMConstInt(ctx
->i32
, ac_get_tbuffer_format(ctx
->chip_class
, dfmt
, nfmt
), 0);
1428 args
[idx
++] = LLVMConstInt(ctx
->i32
, get_load_cache_policy(ctx
, cache_policy
), 0);
1429 unsigned func
= !ac_has_vec3_support(ctx
->chip_class
, true) && num_channels
== 3 ? 4 : num_channels
;
1430 const char *indexing_kind
= structurized
? "struct" : "raw";
1431 char name
[256], type_name
[8];
1433 LLVMTypeRef type
= func
> 1 ? LLVMVectorType(ctx
->i32
, func
) : ctx
->i32
;
1434 ac_build_type_name_for_intr(type
, type_name
, sizeof(type_name
));
1436 snprintf(name
, sizeof(name
), "llvm.amdgcn.%s.tbuffer.load.%s",
1437 indexing_kind
, type_name
);
1439 return ac_build_intrinsic(ctx
, name
, type
, args
, idx
,
1440 ac_get_load_intr_attribs(can_speculate
));
1444 ac_build_struct_tbuffer_load(struct ac_llvm_context
*ctx
,
1446 LLVMValueRef vindex
,
1447 LLVMValueRef voffset
,
1448 LLVMValueRef soffset
,
1449 LLVMValueRef immoffset
,
1450 unsigned num_channels
,
1453 unsigned cache_policy
,
1456 return ac_build_tbuffer_load(ctx
, rsrc
, vindex
, voffset
, soffset
,
1457 immoffset
, num_channels
, dfmt
, nfmt
,
1458 cache_policy
, can_speculate
, true);
1462 ac_build_raw_tbuffer_load(struct ac_llvm_context
*ctx
,
1464 LLVMValueRef voffset
,
1465 LLVMValueRef soffset
,
1466 LLVMValueRef immoffset
,
1467 unsigned num_channels
,
1470 unsigned cache_policy
,
1473 return ac_build_tbuffer_load(ctx
, rsrc
, NULL
, voffset
, soffset
,
1474 immoffset
, num_channels
, dfmt
, nfmt
,
1475 cache_policy
, can_speculate
, false);
1479 ac_build_tbuffer_load_short(struct ac_llvm_context
*ctx
,
1481 LLVMValueRef voffset
,
1482 LLVMValueRef soffset
,
1483 LLVMValueRef immoffset
,
1484 unsigned cache_policy
)
1488 if (LLVM_VERSION_MAJOR
>= 9) {
1489 voffset
= LLVMBuildAdd(ctx
->builder
, voffset
, immoffset
, "");
1491 /* LLVM 9+ supports i8/i16 with struct/raw intrinsics. */
1492 res
= ac_build_buffer_load_common(ctx
, rsrc
, NULL
,
1494 1, ctx
->i16
, cache_policy
,
1495 false, false, false);
1497 unsigned dfmt
= V_008F0C_BUF_DATA_FORMAT_16
;
1498 unsigned nfmt
= V_008F0C_BUF_NUM_FORMAT_UINT
;
1500 res
= ac_build_raw_tbuffer_load(ctx
, rsrc
, voffset
, soffset
,
1501 immoffset
, 1, dfmt
, nfmt
, cache_policy
,
1504 res
= LLVMBuildTrunc(ctx
->builder
, res
, ctx
->i16
, "");
1511 ac_build_tbuffer_load_byte(struct ac_llvm_context
*ctx
,
1513 LLVMValueRef voffset
,
1514 LLVMValueRef soffset
,
1515 LLVMValueRef immoffset
,
1516 unsigned cache_policy
)
1520 if (LLVM_VERSION_MAJOR
>= 9) {
1521 voffset
= LLVMBuildAdd(ctx
->builder
, voffset
, immoffset
, "");
1523 /* LLVM 9+ supports i8/i16 with struct/raw intrinsics. */
1524 res
= ac_build_buffer_load_common(ctx
, rsrc
, NULL
,
1526 1, ctx
->i8
, cache_policy
,
1527 false, false, false);
1529 unsigned dfmt
= V_008F0C_BUF_DATA_FORMAT_8
;
1530 unsigned nfmt
= V_008F0C_BUF_NUM_FORMAT_UINT
;
1532 res
= ac_build_raw_tbuffer_load(ctx
, rsrc
, voffset
, soffset
,
1533 immoffset
, 1, dfmt
, nfmt
, cache_policy
,
1536 res
= LLVMBuildTrunc(ctx
->builder
, res
, ctx
->i8
, "");
1543 * Convert an 11- or 10-bit unsigned floating point number to an f32.
1545 * The input exponent is expected to be biased analogous to IEEE-754, i.e. by
1546 * 2^(exp_bits-1) - 1 (as defined in OpenGL and other graphics APIs).
1549 ac_ufN_to_float(struct ac_llvm_context
*ctx
, LLVMValueRef src
, unsigned exp_bits
, unsigned mant_bits
)
1551 assert(LLVMTypeOf(src
) == ctx
->i32
);
1554 LLVMValueRef mantissa
;
1555 mantissa
= LLVMBuildAnd(ctx
->builder
, src
, LLVMConstInt(ctx
->i32
, (1 << mant_bits
) - 1, false), "");
1557 /* Converting normal numbers is just a shift + correcting the exponent bias */
1558 unsigned normal_shift
= 23 - mant_bits
;
1559 unsigned bias_shift
= 127 - ((1 << (exp_bits
- 1)) - 1);
1560 LLVMValueRef shifted
, normal
;
1562 shifted
= LLVMBuildShl(ctx
->builder
, src
, LLVMConstInt(ctx
->i32
, normal_shift
, false), "");
1563 normal
= LLVMBuildAdd(ctx
->builder
, shifted
, LLVMConstInt(ctx
->i32
, bias_shift
<< 23, false), "");
1565 /* Converting nan/inf numbers is the same, but with a different exponent update */
1566 LLVMValueRef naninf
;
1567 naninf
= LLVMBuildOr(ctx
->builder
, normal
, LLVMConstInt(ctx
->i32
, 0xff << 23, false), "");
1569 /* Converting denormals is the complex case: determine the leading zeros of the
1570 * mantissa to obtain the correct shift for the mantissa and exponent correction.
1572 LLVMValueRef denormal
;
1573 LLVMValueRef params
[2] = {
1575 ctx
->i1true
, /* result can be undef when arg is 0 */
1577 LLVMValueRef ctlz
= ac_build_intrinsic(ctx
, "llvm.ctlz.i32", ctx
->i32
,
1578 params
, 2, AC_FUNC_ATTR_READNONE
);
1580 /* Shift such that the leading 1 ends up as the LSB of the exponent field. */
1581 tmp
= LLVMBuildSub(ctx
->builder
, ctlz
, LLVMConstInt(ctx
->i32
, 8, false), "");
1582 denormal
= LLVMBuildShl(ctx
->builder
, mantissa
, tmp
, "");
1584 unsigned denormal_exp
= bias_shift
+ (32 - mant_bits
) - 1;
1585 tmp
= LLVMBuildSub(ctx
->builder
, LLVMConstInt(ctx
->i32
, denormal_exp
, false), ctlz
, "");
1586 tmp
= LLVMBuildShl(ctx
->builder
, tmp
, LLVMConstInt(ctx
->i32
, 23, false), "");
1587 denormal
= LLVMBuildAdd(ctx
->builder
, denormal
, tmp
, "");
1589 /* Select the final result. */
1590 LLVMValueRef result
;
1592 tmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntUGE
, src
,
1593 LLVMConstInt(ctx
->i32
, ((1 << exp_bits
) - 1) << mant_bits
, false), "");
1594 result
= LLVMBuildSelect(ctx
->builder
, tmp
, naninf
, normal
, "");
1596 tmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntUGE
, src
,
1597 LLVMConstInt(ctx
->i32
, 1 << mant_bits
, false), "");
1598 result
= LLVMBuildSelect(ctx
->builder
, tmp
, result
, denormal
, "");
1600 tmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src
, ctx
->i32_0
, "");
1601 result
= LLVMBuildSelect(ctx
->builder
, tmp
, result
, ctx
->i32_0
, "");
1603 return ac_to_float(ctx
, result
);
1607 * Generate a fully general open coded buffer format fetch with all required
1608 * fixups suitable for vertex fetch, using non-format buffer loads.
1610 * Some combinations of argument values have special interpretations:
1611 * - size = 8 bytes, format = fixed indicates PIPE_FORMAT_R11G11B10_FLOAT
1612 * - size = 8 bytes, format != {float,fixed} indicates a 2_10_10_10 data format
1614 * \param log_size log(size of channel in bytes)
1615 * \param num_channels number of channels (1 to 4)
1616 * \param format AC_FETCH_FORMAT_xxx value
1617 * \param reverse whether XYZ channels are reversed
1618 * \param known_aligned whether the source is known to be aligned to hardware's
1619 * effective element size for loading the given format
1620 * (note: this means dword alignment for 8_8_8_8, 16_16, etc.)
1621 * \param rsrc buffer resource descriptor
1622 * \return the resulting vector of floats or integers bitcast to <4 x i32>
1625 ac_build_opencoded_load_format(struct ac_llvm_context
*ctx
,
1627 unsigned num_channels
,
1632 LLVMValueRef vindex
,
1633 LLVMValueRef voffset
,
1634 LLVMValueRef soffset
,
1635 unsigned cache_policy
,
1639 unsigned load_log_size
= log_size
;
1640 unsigned load_num_channels
= num_channels
;
1641 if (log_size
== 3) {
1643 if (format
== AC_FETCH_FORMAT_FLOAT
) {
1644 load_num_channels
= 2 * num_channels
;
1646 load_num_channels
= 1; /* 10_11_11 or 2_10_10_10 */
1650 int log_recombine
= 0;
1651 if (ctx
->chip_class
== GFX6
&& !known_aligned
) {
1652 /* Avoid alignment restrictions by loading one byte at a time. */
1653 load_num_channels
<<= load_log_size
;
1654 log_recombine
= load_log_size
;
1656 } else if (load_num_channels
== 2 || load_num_channels
== 4) {
1657 log_recombine
= -util_logbase2(load_num_channels
);
1658 load_num_channels
= 1;
1659 load_log_size
+= -log_recombine
;
1662 assert(load_log_size
>= 2 || LLVM_VERSION_MAJOR
>= 9);
1664 LLVMValueRef loads
[32]; /* up to 32 bytes */
1665 for (unsigned i
= 0; i
< load_num_channels
; ++i
) {
1666 tmp
= LLVMBuildAdd(ctx
->builder
, soffset
,
1667 LLVMConstInt(ctx
->i32
, i
<< load_log_size
, false), "");
1668 LLVMTypeRef channel_type
= load_log_size
== 0 ? ctx
->i8
:
1669 load_log_size
== 1 ? ctx
->i16
: ctx
->i32
;
1670 unsigned num_channels
= 1 << (MAX2(load_log_size
, 2) - 2);
1671 loads
[i
] = ac_build_buffer_load_common(
1672 ctx
, rsrc
, vindex
, voffset
, tmp
,
1673 num_channels
, channel_type
, cache_policy
,
1674 can_speculate
, false, true);
1675 if (load_log_size
>= 2)
1676 loads
[i
] = ac_to_integer(ctx
, loads
[i
]);
1679 if (log_recombine
> 0) {
1680 /* Recombine bytes if necessary (GFX6 only) */
1681 LLVMTypeRef dst_type
= log_recombine
== 2 ? ctx
->i32
: ctx
->i16
;
1683 for (unsigned src
= 0, dst
= 0; src
< load_num_channels
; ++dst
) {
1684 LLVMValueRef accum
= NULL
;
1685 for (unsigned i
= 0; i
< (1 << log_recombine
); ++i
, ++src
) {
1686 tmp
= LLVMBuildZExt(ctx
->builder
, loads
[src
], dst_type
, "");
1690 tmp
= LLVMBuildShl(ctx
->builder
, tmp
,
1691 LLVMConstInt(dst_type
, 8 * i
, false), "");
1692 accum
= LLVMBuildOr(ctx
->builder
, accum
, tmp
, "");
1697 } else if (log_recombine
< 0) {
1698 /* Split vectors of dwords */
1699 if (load_log_size
> 2) {
1700 assert(load_num_channels
== 1);
1701 LLVMValueRef loaded
= loads
[0];
1702 unsigned log_split
= load_log_size
- 2;
1703 log_recombine
+= log_split
;
1704 load_num_channels
= 1 << log_split
;
1706 for (unsigned i
= 0; i
< load_num_channels
; ++i
) {
1707 tmp
= LLVMConstInt(ctx
->i32
, i
, false);
1708 loads
[i
] = LLVMBuildExtractElement(ctx
->builder
, loaded
, tmp
, "");
1712 /* Further split dwords and shorts if required */
1713 if (log_recombine
< 0) {
1714 for (unsigned src
= load_num_channels
,
1715 dst
= load_num_channels
<< -log_recombine
;
1717 unsigned dst_bits
= 1 << (3 + load_log_size
+ log_recombine
);
1718 LLVMTypeRef dst_type
= LLVMIntTypeInContext(ctx
->context
, dst_bits
);
1719 LLVMValueRef loaded
= loads
[src
- 1];
1720 LLVMTypeRef loaded_type
= LLVMTypeOf(loaded
);
1721 for (unsigned i
= 1 << -log_recombine
; i
> 0; --i
, --dst
) {
1722 tmp
= LLVMConstInt(loaded_type
, dst_bits
* (i
- 1), false);
1723 tmp
= LLVMBuildLShr(ctx
->builder
, loaded
, tmp
, "");
1724 loads
[dst
- 1] = LLVMBuildTrunc(ctx
->builder
, tmp
, dst_type
, "");
1730 if (log_size
== 3) {
1731 if (format
== AC_FETCH_FORMAT_FLOAT
) {
1732 for (unsigned i
= 0; i
< num_channels
; ++i
) {
1733 tmp
= ac_build_gather_values(ctx
, &loads
[2 * i
], 2);
1734 loads
[i
] = LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f64
, "");
1736 } else if (format
== AC_FETCH_FORMAT_FIXED
) {
1737 /* 10_11_11_FLOAT */
1738 LLVMValueRef data
= loads
[0];
1739 LLVMValueRef i32_2047
= LLVMConstInt(ctx
->i32
, 2047, false);
1740 LLVMValueRef r
= LLVMBuildAnd(ctx
->builder
, data
, i32_2047
, "");
1741 tmp
= LLVMBuildLShr(ctx
->builder
, data
, LLVMConstInt(ctx
->i32
, 11, false), "");
1742 LLVMValueRef g
= LLVMBuildAnd(ctx
->builder
, tmp
, i32_2047
, "");
1743 LLVMValueRef b
= LLVMBuildLShr(ctx
->builder
, data
, LLVMConstInt(ctx
->i32
, 22, false), "");
1745 loads
[0] = ac_to_integer(ctx
, ac_ufN_to_float(ctx
, r
, 5, 6));
1746 loads
[1] = ac_to_integer(ctx
, ac_ufN_to_float(ctx
, g
, 5, 6));
1747 loads
[2] = ac_to_integer(ctx
, ac_ufN_to_float(ctx
, b
, 5, 5));
1751 format
= AC_FETCH_FORMAT_FLOAT
;
1753 /* 2_10_10_10 data formats */
1754 LLVMValueRef data
= loads
[0];
1755 LLVMTypeRef i10
= LLVMIntTypeInContext(ctx
->context
, 10);
1756 LLVMTypeRef i2
= LLVMIntTypeInContext(ctx
->context
, 2);
1757 loads
[0] = LLVMBuildTrunc(ctx
->builder
, data
, i10
, "");
1758 tmp
= LLVMBuildLShr(ctx
->builder
, data
, LLVMConstInt(ctx
->i32
, 10, false), "");
1759 loads
[1] = LLVMBuildTrunc(ctx
->builder
, tmp
, i10
, "");
1760 tmp
= LLVMBuildLShr(ctx
->builder
, data
, LLVMConstInt(ctx
->i32
, 20, false), "");
1761 loads
[2] = LLVMBuildTrunc(ctx
->builder
, tmp
, i10
, "");
1762 tmp
= LLVMBuildLShr(ctx
->builder
, data
, LLVMConstInt(ctx
->i32
, 30, false), "");
1763 loads
[3] = LLVMBuildTrunc(ctx
->builder
, tmp
, i2
, "");
1769 if (format
== AC_FETCH_FORMAT_FLOAT
) {
1770 if (log_size
!= 2) {
1771 for (unsigned chan
= 0; chan
< num_channels
; ++chan
) {
1772 tmp
= ac_to_float(ctx
, loads
[chan
]);
1774 tmp
= LLVMBuildFPTrunc(ctx
->builder
, tmp
, ctx
->f32
, "");
1775 else if (log_size
== 1)
1776 tmp
= LLVMBuildFPExt(ctx
->builder
, tmp
, ctx
->f32
, "");
1777 loads
[chan
] = ac_to_integer(ctx
, tmp
);
1780 } else if (format
== AC_FETCH_FORMAT_UINT
) {
1781 if (log_size
!= 2) {
1782 for (unsigned chan
= 0; chan
< num_channels
; ++chan
)
1783 loads
[chan
] = LLVMBuildZExt(ctx
->builder
, loads
[chan
], ctx
->i32
, "");
1785 } else if (format
== AC_FETCH_FORMAT_SINT
) {
1786 if (log_size
!= 2) {
1787 for (unsigned chan
= 0; chan
< num_channels
; ++chan
)
1788 loads
[chan
] = LLVMBuildSExt(ctx
->builder
, loads
[chan
], ctx
->i32
, "");
1791 bool unsign
= format
== AC_FETCH_FORMAT_UNORM
||
1792 format
== AC_FETCH_FORMAT_USCALED
||
1793 format
== AC_FETCH_FORMAT_UINT
;
1795 for (unsigned chan
= 0; chan
< num_channels
; ++chan
) {
1797 tmp
= LLVMBuildUIToFP(ctx
->builder
, loads
[chan
], ctx
->f32
, "");
1799 tmp
= LLVMBuildSIToFP(ctx
->builder
, loads
[chan
], ctx
->f32
, "");
1802 LLVMValueRef scale
= NULL
;
1803 if (format
== AC_FETCH_FORMAT_FIXED
) {
1804 assert(log_size
== 2);
1805 scale
= LLVMConstReal(ctx
->f32
, 1.0 / 0x10000);
1806 } else if (format
== AC_FETCH_FORMAT_UNORM
) {
1807 unsigned bits
= LLVMGetIntTypeWidth(LLVMTypeOf(loads
[chan
]));
1808 scale
= LLVMConstReal(ctx
->f32
, 1.0 / (((uint64_t)1 << bits
) - 1));
1809 } else if (format
== AC_FETCH_FORMAT_SNORM
) {
1810 unsigned bits
= LLVMGetIntTypeWidth(LLVMTypeOf(loads
[chan
]));
1811 scale
= LLVMConstReal(ctx
->f32
, 1.0 / (((uint64_t)1 << (bits
- 1)) - 1));
1814 tmp
= LLVMBuildFMul(ctx
->builder
, tmp
, scale
, "");
1816 if (format
== AC_FETCH_FORMAT_SNORM
) {
1817 /* Clamp to [-1, 1] */
1818 LLVMValueRef neg_one
= LLVMConstReal(ctx
->f32
, -1.0);
1819 LLVMValueRef clamp
=
1820 LLVMBuildFCmp(ctx
->builder
, LLVMRealULT
, tmp
, neg_one
, "");
1821 tmp
= LLVMBuildSelect(ctx
->builder
, clamp
, neg_one
, tmp
, "");
1824 loads
[chan
] = ac_to_integer(ctx
, tmp
);
1828 while (num_channels
< 4) {
1829 if (format
== AC_FETCH_FORMAT_UINT
|| format
== AC_FETCH_FORMAT_SINT
) {
1830 loads
[num_channels
] = num_channels
== 3 ? ctx
->i32_1
: ctx
->i32_0
;
1832 loads
[num_channels
] = ac_to_integer(ctx
, num_channels
== 3 ? ctx
->f32_1
: ctx
->f32_0
);
1839 loads
[0] = loads
[2];
1843 return ac_build_gather_values(ctx
, loads
, 4);
1847 ac_build_tbuffer_store(struct ac_llvm_context
*ctx
,
1850 LLVMValueRef vindex
,
1851 LLVMValueRef voffset
,
1852 LLVMValueRef soffset
,
1853 LLVMValueRef immoffset
,
1854 unsigned num_channels
,
1857 unsigned cache_policy
,
1860 voffset
= LLVMBuildAdd(ctx
->builder
, voffset
? voffset
: ctx
->i32_0
,
1863 LLVMValueRef args
[7];
1865 args
[idx
++] = vdata
;
1866 args
[idx
++] = LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, "");
1868 args
[idx
++] = vindex
? vindex
: ctx
->i32_0
;
1869 args
[idx
++] = voffset
? voffset
: ctx
->i32_0
;
1870 args
[idx
++] = soffset
? soffset
: ctx
->i32_0
;
1871 args
[idx
++] = LLVMConstInt(ctx
->i32
, ac_get_tbuffer_format(ctx
->chip_class
, dfmt
, nfmt
), 0);
1872 args
[idx
++] = LLVMConstInt(ctx
->i32
, cache_policy
, 0);
1873 unsigned func
= !ac_has_vec3_support(ctx
->chip_class
, true) && num_channels
== 3 ? 4 : num_channels
;
1874 const char *indexing_kind
= structurized
? "struct" : "raw";
1875 char name
[256], type_name
[8];
1877 LLVMTypeRef type
= func
> 1 ? LLVMVectorType(ctx
->i32
, func
) : ctx
->i32
;
1878 ac_build_type_name_for_intr(type
, type_name
, sizeof(type_name
));
1880 snprintf(name
, sizeof(name
), "llvm.amdgcn.%s.tbuffer.store.%s",
1881 indexing_kind
, type_name
);
1883 ac_build_intrinsic(ctx
, name
, ctx
->voidt
, args
, idx
,
1884 AC_FUNC_ATTR_INACCESSIBLE_MEM_ONLY
);
1888 ac_build_struct_tbuffer_store(struct ac_llvm_context
*ctx
,
1891 LLVMValueRef vindex
,
1892 LLVMValueRef voffset
,
1893 LLVMValueRef soffset
,
1894 LLVMValueRef immoffset
,
1895 unsigned num_channels
,
1898 unsigned cache_policy
)
1900 ac_build_tbuffer_store(ctx
, rsrc
, vdata
, vindex
, voffset
, soffset
,
1901 immoffset
, num_channels
, dfmt
, nfmt
, cache_policy
,
1906 ac_build_raw_tbuffer_store(struct ac_llvm_context
*ctx
,
1909 LLVMValueRef voffset
,
1910 LLVMValueRef soffset
,
1911 LLVMValueRef immoffset
,
1912 unsigned num_channels
,
1915 unsigned cache_policy
)
1917 ac_build_tbuffer_store(ctx
, rsrc
, vdata
, NULL
, voffset
, soffset
,
1918 immoffset
, num_channels
, dfmt
, nfmt
, cache_policy
,
1923 ac_build_tbuffer_store_short(struct ac_llvm_context
*ctx
,
1926 LLVMValueRef voffset
,
1927 LLVMValueRef soffset
,
1928 unsigned cache_policy
)
1930 vdata
= LLVMBuildBitCast(ctx
->builder
, vdata
, ctx
->i16
, "");
1932 if (LLVM_VERSION_MAJOR
>= 9) {
1933 /* LLVM 9+ supports i8/i16 with struct/raw intrinsics. */
1934 ac_build_buffer_store_common(ctx
, rsrc
, vdata
, NULL
,
1935 voffset
, soffset
, 1,
1936 ctx
->i16
, cache_policy
,
1939 unsigned dfmt
= V_008F0C_BUF_DATA_FORMAT_16
;
1940 unsigned nfmt
= V_008F0C_BUF_NUM_FORMAT_UINT
;
1942 vdata
= LLVMBuildZExt(ctx
->builder
, vdata
, ctx
->i32
, "");
1944 ac_build_raw_tbuffer_store(ctx
, rsrc
, vdata
, voffset
, soffset
,
1945 ctx
->i32_0
, 1, dfmt
, nfmt
, cache_policy
);
1950 ac_build_tbuffer_store_byte(struct ac_llvm_context
*ctx
,
1953 LLVMValueRef voffset
,
1954 LLVMValueRef soffset
,
1955 unsigned cache_policy
)
1957 vdata
= LLVMBuildBitCast(ctx
->builder
, vdata
, ctx
->i8
, "");
1959 if (LLVM_VERSION_MAJOR
>= 9) {
1960 /* LLVM 9+ supports i8/i16 with struct/raw intrinsics. */
1961 ac_build_buffer_store_common(ctx
, rsrc
, vdata
, NULL
,
1962 voffset
, soffset
, 1,
1963 ctx
->i8
, cache_policy
,
1966 unsigned dfmt
= V_008F0C_BUF_DATA_FORMAT_8
;
1967 unsigned nfmt
= V_008F0C_BUF_NUM_FORMAT_UINT
;
1969 vdata
= LLVMBuildZExt(ctx
->builder
, vdata
, ctx
->i32
, "");
1971 ac_build_raw_tbuffer_store(ctx
, rsrc
, vdata
, voffset
, soffset
,
1972 ctx
->i32_0
, 1, dfmt
, nfmt
, cache_policy
);
1976 * Set range metadata on an instruction. This can only be used on load and
1977 * call instructions. If you know an instruction can only produce the values
1978 * 0, 1, 2, you would do set_range_metadata(value, 0, 3);
1979 * \p lo is the minimum value inclusive.
1980 * \p hi is the maximum value exclusive.
1982 static void set_range_metadata(struct ac_llvm_context
*ctx
,
1983 LLVMValueRef value
, unsigned lo
, unsigned hi
)
1985 LLVMValueRef range_md
, md_args
[2];
1986 LLVMTypeRef type
= LLVMTypeOf(value
);
1987 LLVMContextRef context
= LLVMGetTypeContext(type
);
1989 md_args
[0] = LLVMConstInt(type
, lo
, false);
1990 md_args
[1] = LLVMConstInt(type
, hi
, false);
1991 range_md
= LLVMMDNodeInContext(context
, md_args
, 2);
1992 LLVMSetMetadata(value
, ctx
->range_md_kind
, range_md
);
1996 ac_get_thread_id(struct ac_llvm_context
*ctx
)
2000 LLVMValueRef tid_args
[2];
2001 tid_args
[0] = LLVMConstInt(ctx
->i32
, 0xffffffff, false);
2002 tid_args
[1] = ctx
->i32_0
;
2003 tid_args
[1] = ac_build_intrinsic(ctx
,
2004 "llvm.amdgcn.mbcnt.lo", ctx
->i32
,
2005 tid_args
, 2, AC_FUNC_ATTR_READNONE
);
2007 if (ctx
->wave_size
== 32) {
2010 tid
= ac_build_intrinsic(ctx
, "llvm.amdgcn.mbcnt.hi",
2012 2, AC_FUNC_ATTR_READNONE
);
2014 set_range_metadata(ctx
, tid
, 0, ctx
->wave_size
);
2019 * AMD GCN implements derivatives using the local data store (LDS)
2020 * All writes to the LDS happen in all executing threads at
2021 * the same time. TID is the Thread ID for the current
2022 * thread and is a value between 0 and 63, representing
2023 * the thread's position in the wavefront.
2025 * For the pixel shader threads are grouped into quads of four pixels.
2026 * The TIDs of the pixels of a quad are:
2034 * So, masking the TID with 0xfffffffc yields the TID of the top left pixel
2035 * of the quad, masking with 0xfffffffd yields the TID of the top pixel of
2036 * the current pixel's column, and masking with 0xfffffffe yields the TID
2037 * of the left pixel of the current pixel's row.
2039 * Adding 1 yields the TID of the pixel to the right of the left pixel, and
2040 * adding 2 yields the TID of the pixel below the top pixel.
2043 ac_build_ddxy(struct ac_llvm_context
*ctx
,
2048 unsigned tl_lanes
[4], trbl_lanes
[4];
2049 char name
[32], type
[8];
2050 LLVMValueRef tl
, trbl
;
2051 LLVMTypeRef result_type
;
2052 LLVMValueRef result
;
2054 result_type
= ac_to_float_type(ctx
, LLVMTypeOf(val
));
2056 if (result_type
== ctx
->f16
)
2057 val
= LLVMBuildZExt(ctx
->builder
, val
, ctx
->i32
, "");
2059 for (unsigned i
= 0; i
< 4; ++i
) {
2060 tl_lanes
[i
] = i
& mask
;
2061 trbl_lanes
[i
] = (i
& mask
) + idx
;
2064 tl
= ac_build_quad_swizzle(ctx
, val
,
2065 tl_lanes
[0], tl_lanes
[1],
2066 tl_lanes
[2], tl_lanes
[3]);
2067 trbl
= ac_build_quad_swizzle(ctx
, val
,
2068 trbl_lanes
[0], trbl_lanes
[1],
2069 trbl_lanes
[2], trbl_lanes
[3]);
2071 if (result_type
== ctx
->f16
) {
2072 tl
= LLVMBuildTrunc(ctx
->builder
, tl
, ctx
->i16
, "");
2073 trbl
= LLVMBuildTrunc(ctx
->builder
, trbl
, ctx
->i16
, "");
2076 tl
= LLVMBuildBitCast(ctx
->builder
, tl
, result_type
, "");
2077 trbl
= LLVMBuildBitCast(ctx
->builder
, trbl
, result_type
, "");
2078 result
= LLVMBuildFSub(ctx
->builder
, trbl
, tl
, "");
2080 ac_build_type_name_for_intr(result_type
, type
, sizeof(type
));
2081 snprintf(name
, sizeof(name
), "llvm.amdgcn.wqm.%s", type
);
2083 return ac_build_intrinsic(ctx
, name
, result_type
, &result
, 1, 0);
2087 ac_build_sendmsg(struct ac_llvm_context
*ctx
,
2089 LLVMValueRef wave_id
)
2091 LLVMValueRef args
[2];
2092 args
[0] = LLVMConstInt(ctx
->i32
, msg
, false);
2094 ac_build_intrinsic(ctx
, "llvm.amdgcn.s.sendmsg", ctx
->voidt
, args
, 2, 0);
2098 ac_build_imsb(struct ac_llvm_context
*ctx
,
2100 LLVMTypeRef dst_type
)
2102 LLVMValueRef msb
= ac_build_intrinsic(ctx
, "llvm.amdgcn.sffbh.i32",
2104 AC_FUNC_ATTR_READNONE
);
2106 /* The HW returns the last bit index from MSB, but NIR/TGSI wants
2107 * the index from LSB. Invert it by doing "31 - msb". */
2108 msb
= LLVMBuildSub(ctx
->builder
, LLVMConstInt(ctx
->i32
, 31, false),
2111 LLVMValueRef all_ones
= LLVMConstInt(ctx
->i32
, -1, true);
2112 LLVMValueRef cond
= LLVMBuildOr(ctx
->builder
,
2113 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
2114 arg
, ctx
->i32_0
, ""),
2115 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
2116 arg
, all_ones
, ""), "");
2118 return LLVMBuildSelect(ctx
->builder
, cond
, all_ones
, msb
, "");
2122 ac_build_umsb(struct ac_llvm_context
*ctx
,
2124 LLVMTypeRef dst_type
)
2126 const char *intrin_name
;
2128 LLVMValueRef highest_bit
;
2132 bitsize
= ac_get_elem_bits(ctx
, LLVMTypeOf(arg
));
2135 intrin_name
= "llvm.ctlz.i64";
2137 highest_bit
= LLVMConstInt(ctx
->i64
, 63, false);
2141 intrin_name
= "llvm.ctlz.i32";
2143 highest_bit
= LLVMConstInt(ctx
->i32
, 31, false);
2147 intrin_name
= "llvm.ctlz.i16";
2149 highest_bit
= LLVMConstInt(ctx
->i16
, 15, false);
2153 intrin_name
= "llvm.ctlz.i8";
2155 highest_bit
= LLVMConstInt(ctx
->i8
, 7, false);
2159 unreachable(!"invalid bitsize");
2163 LLVMValueRef params
[2] = {
2168 LLVMValueRef msb
= ac_build_intrinsic(ctx
, intrin_name
, type
,
2170 AC_FUNC_ATTR_READNONE
);
2172 /* The HW returns the last bit index from MSB, but TGSI/NIR wants
2173 * the index from LSB. Invert it by doing "31 - msb". */
2174 msb
= LLVMBuildSub(ctx
->builder
, highest_bit
, msb
, "");
2176 if (bitsize
== 64) {
2177 msb
= LLVMBuildTrunc(ctx
->builder
, msb
, ctx
->i32
, "");
2178 } else if (bitsize
< 32) {
2179 msb
= LLVMBuildSExt(ctx
->builder
, msb
, ctx
->i32
, "");
2182 /* check for zero */
2183 return LLVMBuildSelect(ctx
->builder
,
2184 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, arg
, zero
, ""),
2185 LLVMConstInt(ctx
->i32
, -1, true), msb
, "");
2188 LLVMValueRef
ac_build_fmin(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
2192 snprintf(name
, sizeof(name
), "llvm.minnum.f%d", ac_get_elem_bits(ctx
, LLVMTypeOf(a
)));
2193 LLVMValueRef args
[2] = {a
, b
};
2194 return ac_build_intrinsic(ctx
, name
, LLVMTypeOf(a
), args
, 2,
2195 AC_FUNC_ATTR_READNONE
);
2198 LLVMValueRef
ac_build_fmax(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
2202 snprintf(name
, sizeof(name
), "llvm.maxnum.f%d", ac_get_elem_bits(ctx
, LLVMTypeOf(a
)));
2203 LLVMValueRef args
[2] = {a
, b
};
2204 return ac_build_intrinsic(ctx
, name
, LLVMTypeOf(a
), args
, 2,
2205 AC_FUNC_ATTR_READNONE
);
2208 LLVMValueRef
ac_build_imin(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
2211 LLVMValueRef cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSLE
, a
, b
, "");
2212 return LLVMBuildSelect(ctx
->builder
, cmp
, a
, b
, "");
2215 LLVMValueRef
ac_build_imax(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
2218 LLVMValueRef cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGT
, a
, b
, "");
2219 return LLVMBuildSelect(ctx
->builder
, cmp
, a
, b
, "");
2222 LLVMValueRef
ac_build_umin(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
2225 LLVMValueRef cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntULE
, a
, b
, "");
2226 return LLVMBuildSelect(ctx
->builder
, cmp
, a
, b
, "");
2229 LLVMValueRef
ac_build_umax(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
2232 LLVMValueRef cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntUGE
, a
, b
, "");
2233 return LLVMBuildSelect(ctx
->builder
, cmp
, a
, b
, "");
2236 LLVMValueRef
ac_build_clamp(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
2238 LLVMTypeRef t
= LLVMTypeOf(value
);
2239 return ac_build_fmin(ctx
, ac_build_fmax(ctx
, value
, LLVMConstReal(t
, 0.0)),
2240 LLVMConstReal(t
, 1.0));
2243 void ac_build_export(struct ac_llvm_context
*ctx
, struct ac_export_args
*a
)
2245 LLVMValueRef args
[9];
2247 args
[0] = LLVMConstInt(ctx
->i32
, a
->target
, 0);
2248 args
[1] = LLVMConstInt(ctx
->i32
, a
->enabled_channels
, 0);
2251 LLVMTypeRef i16
= LLVMInt16TypeInContext(ctx
->context
);
2252 LLVMTypeRef v2i16
= LLVMVectorType(i16
, 2);
2254 args
[2] = LLVMBuildBitCast(ctx
->builder
, a
->out
[0],
2256 args
[3] = LLVMBuildBitCast(ctx
->builder
, a
->out
[1],
2258 args
[4] = LLVMConstInt(ctx
->i1
, a
->done
, 0);
2259 args
[5] = LLVMConstInt(ctx
->i1
, a
->valid_mask
, 0);
2261 ac_build_intrinsic(ctx
, "llvm.amdgcn.exp.compr.v2i16",
2262 ctx
->voidt
, args
, 6, 0);
2264 args
[2] = a
->out
[0];
2265 args
[3] = a
->out
[1];
2266 args
[4] = a
->out
[2];
2267 args
[5] = a
->out
[3];
2268 args
[6] = LLVMConstInt(ctx
->i1
, a
->done
, 0);
2269 args
[7] = LLVMConstInt(ctx
->i1
, a
->valid_mask
, 0);
2271 ac_build_intrinsic(ctx
, "llvm.amdgcn.exp.f32",
2272 ctx
->voidt
, args
, 8, 0);
2276 void ac_build_export_null(struct ac_llvm_context
*ctx
)
2278 struct ac_export_args args
;
2280 args
.enabled_channels
= 0x0; /* enabled channels */
2281 args
.valid_mask
= 1; /* whether the EXEC mask is valid */
2282 args
.done
= 1; /* DONE bit */
2283 args
.target
= V_008DFC_SQ_EXP_NULL
;
2284 args
.compr
= 0; /* COMPR flag (0 = 32-bit export) */
2285 args
.out
[0] = LLVMGetUndef(ctx
->f32
); /* R */
2286 args
.out
[1] = LLVMGetUndef(ctx
->f32
); /* G */
2287 args
.out
[2] = LLVMGetUndef(ctx
->f32
); /* B */
2288 args
.out
[3] = LLVMGetUndef(ctx
->f32
); /* A */
2290 ac_build_export(ctx
, &args
);
2293 static unsigned ac_num_coords(enum ac_image_dim dim
)
2299 case ac_image_1darray
:
2303 case ac_image_2darray
:
2304 case ac_image_2dmsaa
:
2306 case ac_image_2darraymsaa
:
2309 unreachable("ac_num_coords: bad dim");
2313 static unsigned ac_num_derivs(enum ac_image_dim dim
)
2317 case ac_image_1darray
:
2320 case ac_image_2darray
:
2325 case ac_image_2dmsaa
:
2326 case ac_image_2darraymsaa
:
2328 unreachable("derivatives not supported");
2332 static const char *get_atomic_name(enum ac_atomic_op op
)
2335 case ac_atomic_swap
: return "swap";
2336 case ac_atomic_add
: return "add";
2337 case ac_atomic_sub
: return "sub";
2338 case ac_atomic_smin
: return "smin";
2339 case ac_atomic_umin
: return "umin";
2340 case ac_atomic_smax
: return "smax";
2341 case ac_atomic_umax
: return "umax";
2342 case ac_atomic_and
: return "and";
2343 case ac_atomic_or
: return "or";
2344 case ac_atomic_xor
: return "xor";
2345 case ac_atomic_inc_wrap
: return "inc";
2346 case ac_atomic_dec_wrap
: return "dec";
2348 unreachable("bad atomic op");
2351 LLVMValueRef
ac_build_image_opcode(struct ac_llvm_context
*ctx
,
2352 struct ac_image_args
*a
)
2354 const char *overload
[3] = { "", "", "" };
2355 unsigned num_overloads
= 0;
2356 LLVMValueRef args
[18];
2357 unsigned num_args
= 0;
2358 enum ac_image_dim dim
= a
->dim
;
2360 assert(!a
->lod
|| a
->lod
== ctx
->i32_0
|| a
->lod
== ctx
->f32_0
||
2362 assert((a
->opcode
!= ac_image_get_resinfo
&& a
->opcode
!= ac_image_load_mip
&&
2363 a
->opcode
!= ac_image_store_mip
) ||
2365 assert(a
->opcode
== ac_image_sample
|| a
->opcode
== ac_image_gather4
||
2366 (!a
->compare
&& !a
->offset
));
2367 assert((a
->opcode
== ac_image_sample
|| a
->opcode
== ac_image_gather4
||
2368 a
->opcode
== ac_image_get_lod
) ||
2370 assert((a
->bias
? 1 : 0) +
2372 (a
->level_zero
? 1 : 0) +
2373 (a
->derivs
[0] ? 1 : 0) <= 1);
2375 if (a
->opcode
== ac_image_get_lod
) {
2377 case ac_image_1darray
:
2380 case ac_image_2darray
:
2389 bool sample
= a
->opcode
== ac_image_sample
||
2390 a
->opcode
== ac_image_gather4
||
2391 a
->opcode
== ac_image_get_lod
;
2392 bool atomic
= a
->opcode
== ac_image_atomic
||
2393 a
->opcode
== ac_image_atomic_cmpswap
;
2394 bool load
= a
->opcode
== ac_image_sample
||
2395 a
->opcode
== ac_image_gather4
||
2396 a
->opcode
== ac_image_load
||
2397 a
->opcode
== ac_image_load_mip
;
2398 LLVMTypeRef coord_type
= sample
? ctx
->f32
: ctx
->i32
;
2400 if (atomic
|| a
->opcode
== ac_image_store
|| a
->opcode
== ac_image_store_mip
) {
2401 args
[num_args
++] = a
->data
[0];
2402 if (a
->opcode
== ac_image_atomic_cmpswap
)
2403 args
[num_args
++] = a
->data
[1];
2407 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->dmask
, false);
2410 args
[num_args
++] = ac_to_integer(ctx
, a
->offset
);
2412 args
[num_args
++] = ac_to_float(ctx
, a
->bias
);
2413 overload
[num_overloads
++] = ".f32";
2416 args
[num_args
++] = ac_to_float(ctx
, a
->compare
);
2418 unsigned count
= ac_num_derivs(dim
);
2419 for (unsigned i
= 0; i
< count
; ++i
)
2420 args
[num_args
++] = ac_to_float(ctx
, a
->derivs
[i
]);
2421 overload
[num_overloads
++] = ".f32";
2423 unsigned num_coords
=
2424 a
->opcode
!= ac_image_get_resinfo
? ac_num_coords(dim
) : 0;
2425 for (unsigned i
= 0; i
< num_coords
; ++i
)
2426 args
[num_args
++] = LLVMBuildBitCast(ctx
->builder
, a
->coords
[i
], coord_type
, "");
2428 args
[num_args
++] = LLVMBuildBitCast(ctx
->builder
, a
->lod
, coord_type
, "");
2429 overload
[num_overloads
++] = sample
? ".f32" : ".i32";
2431 args
[num_args
++] = a
->resource
;
2433 args
[num_args
++] = a
->sampler
;
2434 args
[num_args
++] = LLVMConstInt(ctx
->i1
, a
->unorm
, false);
2437 args
[num_args
++] = ctx
->i32_0
; /* texfailctrl */
2438 args
[num_args
++] = LLVMConstInt(ctx
->i32
,
2439 load
? get_load_cache_policy(ctx
, a
->cache_policy
) :
2440 a
->cache_policy
, false);
2443 const char *atomic_subop
= "";
2444 switch (a
->opcode
) {
2445 case ac_image_sample
: name
= "sample"; break;
2446 case ac_image_gather4
: name
= "gather4"; break;
2447 case ac_image_load
: name
= "load"; break;
2448 case ac_image_load_mip
: name
= "load.mip"; break;
2449 case ac_image_store
: name
= "store"; break;
2450 case ac_image_store_mip
: name
= "store.mip"; break;
2451 case ac_image_atomic
:
2453 atomic_subop
= get_atomic_name(a
->atomic
);
2455 case ac_image_atomic_cmpswap
:
2457 atomic_subop
= "cmpswap";
2459 case ac_image_get_lod
: name
= "getlod"; break;
2460 case ac_image_get_resinfo
: name
= "getresinfo"; break;
2461 default: unreachable("invalid image opcode");
2464 const char *dimname
;
2466 case ac_image_1d
: dimname
= "1d"; break;
2467 case ac_image_2d
: dimname
= "2d"; break;
2468 case ac_image_3d
: dimname
= "3d"; break;
2469 case ac_image_cube
: dimname
= "cube"; break;
2470 case ac_image_1darray
: dimname
= "1darray"; break;
2471 case ac_image_2darray
: dimname
= "2darray"; break;
2472 case ac_image_2dmsaa
: dimname
= "2dmsaa"; break;
2473 case ac_image_2darraymsaa
: dimname
= "2darraymsaa"; break;
2474 default: unreachable("invalid dim");
2478 a
->lod
&& (a
->opcode
== ac_image_sample
|| a
->opcode
== ac_image_gather4
);
2480 snprintf(intr_name
, sizeof(intr_name
),
2481 "llvm.amdgcn.image.%s%s" /* base name */
2482 "%s%s%s" /* sample/gather modifiers */
2483 ".%s.%s%s%s%s", /* dimension and type overloads */
2485 a
->compare
? ".c" : "",
2488 a
->derivs
[0] ? ".d" :
2489 a
->level_zero
? ".lz" : "",
2490 a
->offset
? ".o" : "",
2492 atomic
? "i32" : "v4f32",
2493 overload
[0], overload
[1], overload
[2]);
2498 else if (a
->opcode
== ac_image_store
|| a
->opcode
== ac_image_store_mip
)
2503 LLVMValueRef result
=
2504 ac_build_intrinsic(ctx
, intr_name
, retty
, args
, num_args
,
2506 if (!sample
&& retty
== ctx
->v4f32
) {
2507 result
= LLVMBuildBitCast(ctx
->builder
, result
,
2513 LLVMValueRef
ac_build_image_get_sample_count(struct ac_llvm_context
*ctx
,
2516 LLVMValueRef samples
;
2518 /* Read the samples from the descriptor directly.
2519 * Hardware doesn't have any instruction for this.
2521 samples
= LLVMBuildExtractElement(ctx
->builder
, rsrc
,
2522 LLVMConstInt(ctx
->i32
, 3, 0), "");
2523 samples
= LLVMBuildLShr(ctx
->builder
, samples
,
2524 LLVMConstInt(ctx
->i32
, 16, 0), "");
2525 samples
= LLVMBuildAnd(ctx
->builder
, samples
,
2526 LLVMConstInt(ctx
->i32
, 0xf, 0), "");
2527 samples
= LLVMBuildShl(ctx
->builder
, ctx
->i32_1
,
2532 LLVMValueRef
ac_build_cvt_pkrtz_f16(struct ac_llvm_context
*ctx
,
2533 LLVMValueRef args
[2])
2536 LLVMVectorType(LLVMHalfTypeInContext(ctx
->context
), 2);
2538 return ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pkrtz", v2f16
,
2539 args
, 2, AC_FUNC_ATTR_READNONE
);
2542 LLVMValueRef
ac_build_cvt_pknorm_i16(struct ac_llvm_context
*ctx
,
2543 LLVMValueRef args
[2])
2546 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pknorm.i16",
2547 ctx
->v2i16
, args
, 2,
2548 AC_FUNC_ATTR_READNONE
);
2549 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
2552 LLVMValueRef
ac_build_cvt_pknorm_u16(struct ac_llvm_context
*ctx
,
2553 LLVMValueRef args
[2])
2556 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pknorm.u16",
2557 ctx
->v2i16
, args
, 2,
2558 AC_FUNC_ATTR_READNONE
);
2559 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
2562 /* The 8-bit and 10-bit clamping is for HW workarounds. */
2563 LLVMValueRef
ac_build_cvt_pk_i16(struct ac_llvm_context
*ctx
,
2564 LLVMValueRef args
[2], unsigned bits
, bool hi
)
2566 assert(bits
== 8 || bits
== 10 || bits
== 16);
2568 LLVMValueRef max_rgb
= LLVMConstInt(ctx
->i32
,
2569 bits
== 8 ? 127 : bits
== 10 ? 511 : 32767, 0);
2570 LLVMValueRef min_rgb
= LLVMConstInt(ctx
->i32
,
2571 bits
== 8 ? -128 : bits
== 10 ? -512 : -32768, 0);
2572 LLVMValueRef max_alpha
=
2573 bits
!= 10 ? max_rgb
: ctx
->i32_1
;
2574 LLVMValueRef min_alpha
=
2575 bits
!= 10 ? min_rgb
: LLVMConstInt(ctx
->i32
, -2, 0);
2579 for (int i
= 0; i
< 2; i
++) {
2580 bool alpha
= hi
&& i
== 1;
2581 args
[i
] = ac_build_imin(ctx
, args
[i
],
2582 alpha
? max_alpha
: max_rgb
);
2583 args
[i
] = ac_build_imax(ctx
, args
[i
],
2584 alpha
? min_alpha
: min_rgb
);
2589 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pk.i16",
2590 ctx
->v2i16
, args
, 2,
2591 AC_FUNC_ATTR_READNONE
);
2592 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
2595 /* The 8-bit and 10-bit clamping is for HW workarounds. */
2596 LLVMValueRef
ac_build_cvt_pk_u16(struct ac_llvm_context
*ctx
,
2597 LLVMValueRef args
[2], unsigned bits
, bool hi
)
2599 assert(bits
== 8 || bits
== 10 || bits
== 16);
2601 LLVMValueRef max_rgb
= LLVMConstInt(ctx
->i32
,
2602 bits
== 8 ? 255 : bits
== 10 ? 1023 : 65535, 0);
2603 LLVMValueRef max_alpha
=
2604 bits
!= 10 ? max_rgb
: LLVMConstInt(ctx
->i32
, 3, 0);
2608 for (int i
= 0; i
< 2; i
++) {
2609 bool alpha
= hi
&& i
== 1;
2610 args
[i
] = ac_build_umin(ctx
, args
[i
],
2611 alpha
? max_alpha
: max_rgb
);
2616 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pk.u16",
2617 ctx
->v2i16
, args
, 2,
2618 AC_FUNC_ATTR_READNONE
);
2619 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
2622 LLVMValueRef
ac_build_wqm_vote(struct ac_llvm_context
*ctx
, LLVMValueRef i1
)
2624 return ac_build_intrinsic(ctx
, "llvm.amdgcn.wqm.vote", ctx
->i1
,
2625 &i1
, 1, AC_FUNC_ATTR_READNONE
);
2628 void ac_build_kill_if_false(struct ac_llvm_context
*ctx
, LLVMValueRef i1
)
2630 ac_build_intrinsic(ctx
, "llvm.amdgcn.kill", ctx
->voidt
,
2634 LLVMValueRef
ac_build_bfe(struct ac_llvm_context
*ctx
, LLVMValueRef input
,
2635 LLVMValueRef offset
, LLVMValueRef width
,
2638 LLVMValueRef args
[] = {
2644 return ac_build_intrinsic(ctx
, is_signed
? "llvm.amdgcn.sbfe.i32" :
2645 "llvm.amdgcn.ubfe.i32",
2646 ctx
->i32
, args
, 3, AC_FUNC_ATTR_READNONE
);
2650 LLVMValueRef
ac_build_imad(struct ac_llvm_context
*ctx
, LLVMValueRef s0
,
2651 LLVMValueRef s1
, LLVMValueRef s2
)
2653 return LLVMBuildAdd(ctx
->builder
,
2654 LLVMBuildMul(ctx
->builder
, s0
, s1
, ""), s2
, "");
2657 LLVMValueRef
ac_build_fmad(struct ac_llvm_context
*ctx
, LLVMValueRef s0
,
2658 LLVMValueRef s1
, LLVMValueRef s2
)
2660 /* FMA is better on GFX10, because it has FMA units instead of MUL-ADD units. */
2661 if (ctx
->chip_class
>= GFX10
) {
2662 return ac_build_intrinsic(ctx
, "llvm.fma.f32", ctx
->f32
,
2663 (LLVMValueRef
[]) {s0
, s1
, s2
}, 3,
2664 AC_FUNC_ATTR_READNONE
);
2667 return LLVMBuildFAdd(ctx
->builder
,
2668 LLVMBuildFMul(ctx
->builder
, s0
, s1
, ""), s2
, "");
2671 void ac_build_waitcnt(struct ac_llvm_context
*ctx
, unsigned wait_flags
)
2676 unsigned lgkmcnt
= 63;
2677 unsigned vmcnt
= ctx
->chip_class
>= GFX9
? 63 : 15;
2678 unsigned vscnt
= 63;
2680 if (wait_flags
& AC_WAIT_LGKM
)
2682 if (wait_flags
& AC_WAIT_VLOAD
)
2685 if (wait_flags
& AC_WAIT_VSTORE
) {
2686 if (ctx
->chip_class
>= GFX10
)
2692 /* There is no intrinsic for vscnt(0), so use a fence. */
2693 if ((wait_flags
& AC_WAIT_LGKM
&&
2694 wait_flags
& AC_WAIT_VLOAD
&&
2695 wait_flags
& AC_WAIT_VSTORE
) ||
2697 LLVMBuildFence(ctx
->builder
, LLVMAtomicOrderingRelease
, false, "");
2701 unsigned simm16
= (lgkmcnt
<< 8) |
2702 (7 << 4) | /* expcnt */
2704 ((vmcnt
>> 4) << 14);
2706 LLVMValueRef args
[1] = {
2707 LLVMConstInt(ctx
->i32
, simm16
, false),
2709 ac_build_intrinsic(ctx
, "llvm.amdgcn.s.waitcnt",
2710 ctx
->voidt
, args
, 1, 0);
2713 LLVMValueRef
ac_build_fmed3(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
2714 LLVMValueRef src1
, LLVMValueRef src2
,
2720 if (bitsize
== 16) {
2721 intr
= "llvm.amdgcn.fmed3.f16";
2723 } else if (bitsize
== 32) {
2724 intr
= "llvm.amdgcn.fmed3.f32";
2727 intr
= "llvm.amdgcn.fmed3.f64";
2731 LLVMValueRef params
[] = {
2736 return ac_build_intrinsic(ctx
, intr
, type
, params
, 3,
2737 AC_FUNC_ATTR_READNONE
);
2740 LLVMValueRef
ac_build_fract(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
2746 if (bitsize
== 16) {
2747 intr
= "llvm.amdgcn.fract.f16";
2749 } else if (bitsize
== 32) {
2750 intr
= "llvm.amdgcn.fract.f32";
2753 intr
= "llvm.amdgcn.fract.f64";
2757 LLVMValueRef params
[] = {
2760 return ac_build_intrinsic(ctx
, intr
, type
, params
, 1,
2761 AC_FUNC_ATTR_READNONE
);
2764 LLVMValueRef
ac_build_isign(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
2767 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->context
, bitsize
);
2768 LLVMValueRef zero
= LLVMConstInt(type
, 0, false);
2769 LLVMValueRef one
= LLVMConstInt(type
, 1, false);
2771 LLVMValueRef cmp
, val
;
2772 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGT
, src0
, zero
, "");
2773 val
= LLVMBuildSelect(ctx
->builder
, cmp
, one
, src0
, "");
2774 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGE
, val
, zero
, "");
2775 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstInt(type
, -1, true), "");
2779 LLVMValueRef
ac_build_fsign(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
2782 LLVMValueRef cmp
, val
, zero
, one
;
2785 if (bitsize
== 16) {
2789 } else if (bitsize
== 32) {
2799 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGT
, src0
, zero
, "");
2800 val
= LLVMBuildSelect(ctx
->builder
, cmp
, one
, src0
, "");
2801 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGE
, val
, zero
, "");
2802 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstReal(type
, -1.0), "");
2806 LLVMValueRef
ac_build_bit_count(struct ac_llvm_context
*ctx
, LLVMValueRef src0
)
2808 LLVMValueRef result
;
2811 bitsize
= ac_get_elem_bits(ctx
, LLVMTypeOf(src0
));
2815 result
= ac_build_intrinsic(ctx
, "llvm.ctpop.i64", ctx
->i64
,
2816 (LLVMValueRef
[]) { src0
}, 1,
2817 AC_FUNC_ATTR_READNONE
);
2819 result
= LLVMBuildTrunc(ctx
->builder
, result
, ctx
->i32
, "");
2822 result
= ac_build_intrinsic(ctx
, "llvm.ctpop.i32", ctx
->i32
,
2823 (LLVMValueRef
[]) { src0
}, 1,
2824 AC_FUNC_ATTR_READNONE
);
2827 result
= ac_build_intrinsic(ctx
, "llvm.ctpop.i16", ctx
->i16
,
2828 (LLVMValueRef
[]) { src0
}, 1,
2829 AC_FUNC_ATTR_READNONE
);
2831 result
= LLVMBuildZExt(ctx
->builder
, result
, ctx
->i32
, "");
2834 result
= ac_build_intrinsic(ctx
, "llvm.ctpop.i8", ctx
->i8
,
2835 (LLVMValueRef
[]) { src0
}, 1,
2836 AC_FUNC_ATTR_READNONE
);
2838 result
= LLVMBuildZExt(ctx
->builder
, result
, ctx
->i32
, "");
2841 unreachable(!"invalid bitsize");
2848 LLVMValueRef
ac_build_bitfield_reverse(struct ac_llvm_context
*ctx
,
2851 LLVMValueRef result
;
2854 bitsize
= ac_get_elem_bits(ctx
, LLVMTypeOf(src0
));
2858 result
= ac_build_intrinsic(ctx
, "llvm.bitreverse.i64", ctx
->i64
,
2859 (LLVMValueRef
[]) { src0
}, 1,
2860 AC_FUNC_ATTR_READNONE
);
2862 result
= LLVMBuildTrunc(ctx
->builder
, result
, ctx
->i32
, "");
2865 result
= ac_build_intrinsic(ctx
, "llvm.bitreverse.i32", ctx
->i32
,
2866 (LLVMValueRef
[]) { src0
}, 1,
2867 AC_FUNC_ATTR_READNONE
);
2870 result
= ac_build_intrinsic(ctx
, "llvm.bitreverse.i16", ctx
->i16
,
2871 (LLVMValueRef
[]) { src0
}, 1,
2872 AC_FUNC_ATTR_READNONE
);
2874 result
= LLVMBuildZExt(ctx
->builder
, result
, ctx
->i32
, "");
2877 result
= ac_build_intrinsic(ctx
, "llvm.bitreverse.i8", ctx
->i8
,
2878 (LLVMValueRef
[]) { src0
}, 1,
2879 AC_FUNC_ATTR_READNONE
);
2881 result
= LLVMBuildZExt(ctx
->builder
, result
, ctx
->i32
, "");
2884 unreachable(!"invalid bitsize");
2891 #define AC_EXP_TARGET 0
2892 #define AC_EXP_ENABLED_CHANNELS 1
2893 #define AC_EXP_OUT0 2
2901 struct ac_vs_exp_chan
2905 enum ac_ir_type type
;
2908 struct ac_vs_exp_inst
{
2911 struct ac_vs_exp_chan chan
[4];
2914 struct ac_vs_exports
{
2916 struct ac_vs_exp_inst exp
[VARYING_SLOT_MAX
];
2919 /* Return true if the PARAM export has been eliminated. */
2920 static bool ac_eliminate_const_output(uint8_t *vs_output_param_offset
,
2921 uint32_t num_outputs
,
2922 struct ac_vs_exp_inst
*exp
)
2924 unsigned i
, default_val
; /* SPI_PS_INPUT_CNTL_i.DEFAULT_VAL */
2925 bool is_zero
[4] = {}, is_one
[4] = {};
2927 for (i
= 0; i
< 4; i
++) {
2928 /* It's a constant expression. Undef outputs are eliminated too. */
2929 if (exp
->chan
[i
].type
== AC_IR_UNDEF
) {
2932 } else if (exp
->chan
[i
].type
== AC_IR_CONST
) {
2933 if (exp
->chan
[i
].const_float
== 0)
2935 else if (exp
->chan
[i
].const_float
== 1)
2938 return false; /* other constant */
2943 /* Only certain combinations of 0 and 1 can be eliminated. */
2944 if (is_zero
[0] && is_zero
[1] && is_zero
[2])
2945 default_val
= is_zero
[3] ? 0 : 1;
2946 else if (is_one
[0] && is_one
[1] && is_one
[2])
2947 default_val
= is_zero
[3] ? 2 : 3;
2951 /* The PARAM export can be represented as DEFAULT_VAL. Kill it. */
2952 LLVMInstructionEraseFromParent(exp
->inst
);
2954 /* Change OFFSET to DEFAULT_VAL. */
2955 for (i
= 0; i
< num_outputs
; i
++) {
2956 if (vs_output_param_offset
[i
] == exp
->offset
) {
2957 vs_output_param_offset
[i
] =
2958 AC_EXP_PARAM_DEFAULT_VAL_0000
+ default_val
;
2965 static bool ac_eliminate_duplicated_output(struct ac_llvm_context
*ctx
,
2966 uint8_t *vs_output_param_offset
,
2967 uint32_t num_outputs
,
2968 struct ac_vs_exports
*processed
,
2969 struct ac_vs_exp_inst
*exp
)
2971 unsigned p
, copy_back_channels
= 0;
2973 /* See if the output is already in the list of processed outputs.
2974 * The LLVMValueRef comparison relies on SSA.
2976 for (p
= 0; p
< processed
->num
; p
++) {
2977 bool different
= false;
2979 for (unsigned j
= 0; j
< 4; j
++) {
2980 struct ac_vs_exp_chan
*c1
= &processed
->exp
[p
].chan
[j
];
2981 struct ac_vs_exp_chan
*c2
= &exp
->chan
[j
];
2983 /* Treat undef as a match. */
2984 if (c2
->type
== AC_IR_UNDEF
)
2987 /* If c1 is undef but c2 isn't, we can copy c2 to c1
2988 * and consider the instruction duplicated.
2990 if (c1
->type
== AC_IR_UNDEF
) {
2991 copy_back_channels
|= 1 << j
;
2995 /* Test whether the channels are not equal. */
2996 if (c1
->type
!= c2
->type
||
2997 (c1
->type
== AC_IR_CONST
&&
2998 c1
->const_float
!= c2
->const_float
) ||
2999 (c1
->type
== AC_IR_VALUE
&&
3000 c1
->value
!= c2
->value
)) {
3008 copy_back_channels
= 0;
3010 if (p
== processed
->num
)
3013 /* If a match was found, but the matching export has undef where the new
3014 * one has a normal value, copy the normal value to the undef channel.
3016 struct ac_vs_exp_inst
*match
= &processed
->exp
[p
];
3018 /* Get current enabled channels mask. */
3019 LLVMValueRef arg
= LLVMGetOperand(match
->inst
, AC_EXP_ENABLED_CHANNELS
);
3020 unsigned enabled_channels
= LLVMConstIntGetZExtValue(arg
);
3022 while (copy_back_channels
) {
3023 unsigned chan
= u_bit_scan(©_back_channels
);
3025 assert(match
->chan
[chan
].type
== AC_IR_UNDEF
);
3026 LLVMSetOperand(match
->inst
, AC_EXP_OUT0
+ chan
,
3027 exp
->chan
[chan
].value
);
3028 match
->chan
[chan
] = exp
->chan
[chan
];
3030 /* Update number of enabled channels because the original mask
3031 * is not always 0xf.
3033 enabled_channels
|= (1 << chan
);
3034 LLVMSetOperand(match
->inst
, AC_EXP_ENABLED_CHANNELS
,
3035 LLVMConstInt(ctx
->i32
, enabled_channels
, 0));
3038 /* The PARAM export is duplicated. Kill it. */
3039 LLVMInstructionEraseFromParent(exp
->inst
);
3041 /* Change OFFSET to the matching export. */
3042 for (unsigned i
= 0; i
< num_outputs
; i
++) {
3043 if (vs_output_param_offset
[i
] == exp
->offset
) {
3044 vs_output_param_offset
[i
] = match
->offset
;
3051 void ac_optimize_vs_outputs(struct ac_llvm_context
*ctx
,
3052 LLVMValueRef main_fn
,
3053 uint8_t *vs_output_param_offset
,
3054 uint32_t num_outputs
,
3055 uint8_t *num_param_exports
)
3057 LLVMBasicBlockRef bb
;
3058 bool removed_any
= false;
3059 struct ac_vs_exports exports
;
3063 /* Process all LLVM instructions. */
3064 bb
= LLVMGetFirstBasicBlock(main_fn
);
3066 LLVMValueRef inst
= LLVMGetFirstInstruction(bb
);
3069 LLVMValueRef cur
= inst
;
3070 inst
= LLVMGetNextInstruction(inst
);
3071 struct ac_vs_exp_inst exp
;
3073 if (LLVMGetInstructionOpcode(cur
) != LLVMCall
)
3076 LLVMValueRef callee
= ac_llvm_get_called_value(cur
);
3078 if (!ac_llvm_is_function(callee
))
3081 const char *name
= LLVMGetValueName(callee
);
3082 unsigned num_args
= LLVMCountParams(callee
);
3084 /* Check if this is an export instruction. */
3085 if ((num_args
!= 9 && num_args
!= 8) ||
3086 (strcmp(name
, "llvm.SI.export") &&
3087 strcmp(name
, "llvm.amdgcn.exp.f32")))
3090 LLVMValueRef arg
= LLVMGetOperand(cur
, AC_EXP_TARGET
);
3091 unsigned target
= LLVMConstIntGetZExtValue(arg
);
3093 if (target
< V_008DFC_SQ_EXP_PARAM
)
3096 target
-= V_008DFC_SQ_EXP_PARAM
;
3098 /* Parse the instruction. */
3099 memset(&exp
, 0, sizeof(exp
));
3100 exp
.offset
= target
;
3103 for (unsigned i
= 0; i
< 4; i
++) {
3104 LLVMValueRef v
= LLVMGetOperand(cur
, AC_EXP_OUT0
+ i
);
3106 exp
.chan
[i
].value
= v
;
3108 if (LLVMIsUndef(v
)) {
3109 exp
.chan
[i
].type
= AC_IR_UNDEF
;
3110 } else if (LLVMIsAConstantFP(v
)) {
3111 LLVMBool loses_info
;
3112 exp
.chan
[i
].type
= AC_IR_CONST
;
3113 exp
.chan
[i
].const_float
=
3114 LLVMConstRealGetDouble(v
, &loses_info
);
3116 exp
.chan
[i
].type
= AC_IR_VALUE
;
3120 /* Eliminate constant and duplicated PARAM exports. */
3121 if (ac_eliminate_const_output(vs_output_param_offset
,
3122 num_outputs
, &exp
) ||
3123 ac_eliminate_duplicated_output(ctx
,
3124 vs_output_param_offset
,
3125 num_outputs
, &exports
,
3129 exports
.exp
[exports
.num
++] = exp
;
3132 bb
= LLVMGetNextBasicBlock(bb
);
3135 /* Remove holes in export memory due to removed PARAM exports.
3136 * This is done by renumbering all PARAM exports.
3139 uint8_t old_offset
[VARYING_SLOT_MAX
];
3142 /* Make a copy of the offsets. We need the old version while
3143 * we are modifying some of them. */
3144 memcpy(old_offset
, vs_output_param_offset
,
3145 sizeof(old_offset
));
3147 for (i
= 0; i
< exports
.num
; i
++) {
3148 unsigned offset
= exports
.exp
[i
].offset
;
3150 /* Update vs_output_param_offset. Multiple outputs can
3151 * have the same offset.
3153 for (out
= 0; out
< num_outputs
; out
++) {
3154 if (old_offset
[out
] == offset
)
3155 vs_output_param_offset
[out
] = i
;
3158 /* Change the PARAM offset in the instruction. */
3159 LLVMSetOperand(exports
.exp
[i
].inst
, AC_EXP_TARGET
,
3160 LLVMConstInt(ctx
->i32
,
3161 V_008DFC_SQ_EXP_PARAM
+ i
, 0));
3163 *num_param_exports
= exports
.num
;
3167 void ac_init_exec_full_mask(struct ac_llvm_context
*ctx
)
3169 LLVMValueRef full_mask
= LLVMConstInt(ctx
->i64
, ~0ull, 0);
3170 ac_build_intrinsic(ctx
,
3171 "llvm.amdgcn.init.exec", ctx
->voidt
,
3172 &full_mask
, 1, AC_FUNC_ATTR_CONVERGENT
);
3175 void ac_declare_lds_as_pointer(struct ac_llvm_context
*ctx
)
3177 unsigned lds_size
= ctx
->chip_class
>= GFX7
? 65536 : 32768;
3178 ctx
->lds
= LLVMBuildIntToPtr(ctx
->builder
, ctx
->i32_0
,
3179 LLVMPointerType(LLVMArrayType(ctx
->i32
, lds_size
/ 4), AC_ADDR_SPACE_LDS
),
3183 LLVMValueRef
ac_lds_load(struct ac_llvm_context
*ctx
,
3184 LLVMValueRef dw_addr
)
3186 return LLVMBuildLoad(ctx
->builder
, ac_build_gep0(ctx
, ctx
->lds
, dw_addr
), "");
3189 void ac_lds_store(struct ac_llvm_context
*ctx
,
3190 LLVMValueRef dw_addr
,
3193 value
= ac_to_integer(ctx
, value
);
3194 ac_build_indexed_store(ctx
, ctx
->lds
,
3198 LLVMValueRef
ac_find_lsb(struct ac_llvm_context
*ctx
,
3199 LLVMTypeRef dst_type
,
3202 unsigned src0_bitsize
= ac_get_elem_bits(ctx
, LLVMTypeOf(src0
));
3203 const char *intrin_name
;
3207 switch (src0_bitsize
) {
3209 intrin_name
= "llvm.cttz.i64";
3214 intrin_name
= "llvm.cttz.i32";
3219 intrin_name
= "llvm.cttz.i16";
3224 intrin_name
= "llvm.cttz.i8";
3229 unreachable(!"invalid bitsize");
3232 LLVMValueRef params
[2] = {
3235 /* The value of 1 means that ffs(x=0) = undef, so LLVM won't
3236 * add special code to check for x=0. The reason is that
3237 * the LLVM behavior for x=0 is different from what we
3238 * need here. However, LLVM also assumes that ffs(x) is
3239 * in [0, 31], but GLSL expects that ffs(0) = -1, so
3240 * a conditional assignment to handle 0 is still required.
3242 * The hardware already implements the correct behavior.
3247 LLVMValueRef lsb
= ac_build_intrinsic(ctx
, intrin_name
, type
,
3249 AC_FUNC_ATTR_READNONE
);
3251 if (src0_bitsize
== 64) {
3252 lsb
= LLVMBuildTrunc(ctx
->builder
, lsb
, ctx
->i32
, "");
3253 } else if (src0_bitsize
< 32) {
3254 lsb
= LLVMBuildSExt(ctx
->builder
, lsb
, ctx
->i32
, "");
3257 /* TODO: We need an intrinsic to skip this conditional. */
3258 /* Check for zero: */
3259 return LLVMBuildSelect(ctx
->builder
, LLVMBuildICmp(ctx
->builder
,
3262 LLVMConstInt(ctx
->i32
, -1, 0), lsb
, "");
3265 LLVMTypeRef
ac_array_in_const_addr_space(LLVMTypeRef elem_type
)
3267 return LLVMPointerType(elem_type
, AC_ADDR_SPACE_CONST
);
3270 LLVMTypeRef
ac_array_in_const32_addr_space(LLVMTypeRef elem_type
)
3272 return LLVMPointerType(elem_type
, AC_ADDR_SPACE_CONST_32BIT
);
3275 static struct ac_llvm_flow
*
3276 get_current_flow(struct ac_llvm_context
*ctx
)
3278 if (ctx
->flow
->depth
> 0)
3279 return &ctx
->flow
->stack
[ctx
->flow
->depth
- 1];
3283 static struct ac_llvm_flow
*
3284 get_innermost_loop(struct ac_llvm_context
*ctx
)
3286 for (unsigned i
= ctx
->flow
->depth
; i
> 0; --i
) {
3287 if (ctx
->flow
->stack
[i
- 1].loop_entry_block
)
3288 return &ctx
->flow
->stack
[i
- 1];
3293 static struct ac_llvm_flow
*
3294 push_flow(struct ac_llvm_context
*ctx
)
3296 struct ac_llvm_flow
*flow
;
3298 if (ctx
->flow
->depth
>= ctx
->flow
->depth_max
) {
3299 unsigned new_max
= MAX2(ctx
->flow
->depth
<< 1,
3300 AC_LLVM_INITIAL_CF_DEPTH
);
3302 ctx
->flow
->stack
= realloc(ctx
->flow
->stack
, new_max
* sizeof(*ctx
->flow
->stack
));
3303 ctx
->flow
->depth_max
= new_max
;
3306 flow
= &ctx
->flow
->stack
[ctx
->flow
->depth
];
3309 flow
->next_block
= NULL
;
3310 flow
->loop_entry_block
= NULL
;
3314 static void set_basicblock_name(LLVMBasicBlockRef bb
, const char *base
,
3318 snprintf(buf
, sizeof(buf
), "%s%d", base
, label_id
);
3319 LLVMSetValueName(LLVMBasicBlockAsValue(bb
), buf
);
3322 /* Append a basic block at the level of the parent flow.
3324 static LLVMBasicBlockRef
append_basic_block(struct ac_llvm_context
*ctx
,
3327 assert(ctx
->flow
->depth
>= 1);
3329 if (ctx
->flow
->depth
>= 2) {
3330 struct ac_llvm_flow
*flow
= &ctx
->flow
->stack
[ctx
->flow
->depth
- 2];
3332 return LLVMInsertBasicBlockInContext(ctx
->context
,
3333 flow
->next_block
, name
);
3336 LLVMValueRef main_fn
=
3337 LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
->builder
));
3338 return LLVMAppendBasicBlockInContext(ctx
->context
, main_fn
, name
);
3341 /* Emit a branch to the given default target for the current block if
3342 * applicable -- that is, if the current block does not already contain a
3343 * branch from a break or continue.
3345 static void emit_default_branch(LLVMBuilderRef builder
,
3346 LLVMBasicBlockRef target
)
3348 if (!LLVMGetBasicBlockTerminator(LLVMGetInsertBlock(builder
)))
3349 LLVMBuildBr(builder
, target
);
3352 void ac_build_bgnloop(struct ac_llvm_context
*ctx
, int label_id
)
3354 struct ac_llvm_flow
*flow
= push_flow(ctx
);
3355 flow
->loop_entry_block
= append_basic_block(ctx
, "LOOP");
3356 flow
->next_block
= append_basic_block(ctx
, "ENDLOOP");
3357 set_basicblock_name(flow
->loop_entry_block
, "loop", label_id
);
3358 LLVMBuildBr(ctx
->builder
, flow
->loop_entry_block
);
3359 LLVMPositionBuilderAtEnd(ctx
->builder
, flow
->loop_entry_block
);
3362 void ac_build_break(struct ac_llvm_context
*ctx
)
3364 struct ac_llvm_flow
*flow
= get_innermost_loop(ctx
);
3365 LLVMBuildBr(ctx
->builder
, flow
->next_block
);
3368 void ac_build_continue(struct ac_llvm_context
*ctx
)
3370 struct ac_llvm_flow
*flow
= get_innermost_loop(ctx
);
3371 LLVMBuildBr(ctx
->builder
, flow
->loop_entry_block
);
3374 void ac_build_else(struct ac_llvm_context
*ctx
, int label_id
)
3376 struct ac_llvm_flow
*current_branch
= get_current_flow(ctx
);
3377 LLVMBasicBlockRef endif_block
;
3379 assert(!current_branch
->loop_entry_block
);
3381 endif_block
= append_basic_block(ctx
, "ENDIF");
3382 emit_default_branch(ctx
->builder
, endif_block
);
3384 LLVMPositionBuilderAtEnd(ctx
->builder
, current_branch
->next_block
);
3385 set_basicblock_name(current_branch
->next_block
, "else", label_id
);
3387 current_branch
->next_block
= endif_block
;
3390 void ac_build_endif(struct ac_llvm_context
*ctx
, int label_id
)
3392 struct ac_llvm_flow
*current_branch
= get_current_flow(ctx
);
3394 assert(!current_branch
->loop_entry_block
);
3396 emit_default_branch(ctx
->builder
, current_branch
->next_block
);
3397 LLVMPositionBuilderAtEnd(ctx
->builder
, current_branch
->next_block
);
3398 set_basicblock_name(current_branch
->next_block
, "endif", label_id
);
3403 void ac_build_endloop(struct ac_llvm_context
*ctx
, int label_id
)
3405 struct ac_llvm_flow
*current_loop
= get_current_flow(ctx
);
3407 assert(current_loop
->loop_entry_block
);
3409 emit_default_branch(ctx
->builder
, current_loop
->loop_entry_block
);
3411 LLVMPositionBuilderAtEnd(ctx
->builder
, current_loop
->next_block
);
3412 set_basicblock_name(current_loop
->next_block
, "endloop", label_id
);
3416 void ac_build_ifcc(struct ac_llvm_context
*ctx
, LLVMValueRef cond
, int label_id
)
3418 struct ac_llvm_flow
*flow
= push_flow(ctx
);
3419 LLVMBasicBlockRef if_block
;
3421 if_block
= append_basic_block(ctx
, "IF");
3422 flow
->next_block
= append_basic_block(ctx
, "ELSE");
3423 set_basicblock_name(if_block
, "if", label_id
);
3424 LLVMBuildCondBr(ctx
->builder
, cond
, if_block
, flow
->next_block
);
3425 LLVMPositionBuilderAtEnd(ctx
->builder
, if_block
);
3428 void ac_build_if(struct ac_llvm_context
*ctx
, LLVMValueRef value
,
3431 LLVMValueRef cond
= LLVMBuildFCmp(ctx
->builder
, LLVMRealUNE
,
3432 value
, ctx
->f32_0
, "");
3433 ac_build_ifcc(ctx
, cond
, label_id
);
3436 void ac_build_uif(struct ac_llvm_context
*ctx
, LLVMValueRef value
,
3439 LLVMValueRef cond
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
3440 ac_to_integer(ctx
, value
),
3442 ac_build_ifcc(ctx
, cond
, label_id
);
3445 LLVMValueRef
ac_build_alloca_undef(struct ac_llvm_context
*ac
, LLVMTypeRef type
,
3448 LLVMBuilderRef builder
= ac
->builder
;
3449 LLVMBasicBlockRef current_block
= LLVMGetInsertBlock(builder
);
3450 LLVMValueRef function
= LLVMGetBasicBlockParent(current_block
);
3451 LLVMBasicBlockRef first_block
= LLVMGetEntryBasicBlock(function
);
3452 LLVMValueRef first_instr
= LLVMGetFirstInstruction(first_block
);
3453 LLVMBuilderRef first_builder
= LLVMCreateBuilderInContext(ac
->context
);
3457 LLVMPositionBuilderBefore(first_builder
, first_instr
);
3459 LLVMPositionBuilderAtEnd(first_builder
, first_block
);
3462 res
= LLVMBuildAlloca(first_builder
, type
, name
);
3463 LLVMDisposeBuilder(first_builder
);
3467 LLVMValueRef
ac_build_alloca(struct ac_llvm_context
*ac
,
3468 LLVMTypeRef type
, const char *name
)
3470 LLVMValueRef ptr
= ac_build_alloca_undef(ac
, type
, name
);
3471 LLVMBuildStore(ac
->builder
, LLVMConstNull(type
), ptr
);
3475 LLVMValueRef
ac_cast_ptr(struct ac_llvm_context
*ctx
, LLVMValueRef ptr
,
3478 int addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
3479 return LLVMBuildBitCast(ctx
->builder
, ptr
,
3480 LLVMPointerType(type
, addr_space
), "");
3483 LLVMValueRef
ac_trim_vector(struct ac_llvm_context
*ctx
, LLVMValueRef value
,
3486 unsigned num_components
= ac_get_llvm_num_components(value
);
3487 if (count
== num_components
)
3490 LLVMValueRef masks
[MAX2(count
, 2)];
3491 masks
[0] = ctx
->i32_0
;
3492 masks
[1] = ctx
->i32_1
;
3493 for (unsigned i
= 2; i
< count
; i
++)
3494 masks
[i
] = LLVMConstInt(ctx
->i32
, i
, false);
3497 return LLVMBuildExtractElement(ctx
->builder
, value
, masks
[0],
3500 LLVMValueRef swizzle
= LLVMConstVector(masks
, count
);
3501 return LLVMBuildShuffleVector(ctx
->builder
, value
, value
, swizzle
, "");
3504 LLVMValueRef
ac_unpack_param(struct ac_llvm_context
*ctx
, LLVMValueRef param
,
3505 unsigned rshift
, unsigned bitwidth
)
3507 LLVMValueRef value
= param
;
3509 value
= LLVMBuildLShr(ctx
->builder
, value
,
3510 LLVMConstInt(ctx
->i32
, rshift
, false), "");
3512 if (rshift
+ bitwidth
< 32) {
3513 unsigned mask
= (1 << bitwidth
) - 1;
3514 value
= LLVMBuildAnd(ctx
->builder
, value
,
3515 LLVMConstInt(ctx
->i32
, mask
, false), "");
3520 /* Adjust the sample index according to FMASK.
3522 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
3523 * which is the identity mapping. Each nibble says which physical sample
3524 * should be fetched to get that sample.
3526 * For example, 0x11111100 means there are only 2 samples stored and
3527 * the second sample covers 3/4 of the pixel. When reading samples 0
3528 * and 1, return physical sample 0 (determined by the first two 0s
3529 * in FMASK), otherwise return physical sample 1.
3531 * The sample index should be adjusted as follows:
3532 * addr[sample_index] = (fmask >> (addr[sample_index] * 4)) & 0xF;
3534 void ac_apply_fmask_to_sample(struct ac_llvm_context
*ac
, LLVMValueRef fmask
,
3535 LLVMValueRef
*addr
, bool is_array_tex
)
3537 struct ac_image_args fmask_load
= {};
3538 fmask_load
.opcode
= ac_image_load
;
3539 fmask_load
.resource
= fmask
;
3540 fmask_load
.dmask
= 0xf;
3541 fmask_load
.dim
= is_array_tex
? ac_image_2darray
: ac_image_2d
;
3542 fmask_load
.attributes
= AC_FUNC_ATTR_READNONE
;
3544 fmask_load
.coords
[0] = addr
[0];
3545 fmask_load
.coords
[1] = addr
[1];
3547 fmask_load
.coords
[2] = addr
[2];
3549 LLVMValueRef fmask_value
= ac_build_image_opcode(ac
, &fmask_load
);
3550 fmask_value
= LLVMBuildExtractElement(ac
->builder
, fmask_value
,
3553 /* Apply the formula. */
3554 unsigned sample_chan
= is_array_tex
? 3 : 2;
3555 LLVMValueRef final_sample
;
3556 final_sample
= LLVMBuildMul(ac
->builder
, addr
[sample_chan
],
3557 LLVMConstInt(ac
->i32
, 4, 0), "");
3558 final_sample
= LLVMBuildLShr(ac
->builder
, fmask_value
, final_sample
, "");
3559 /* Mask the sample index by 0x7, because 0x8 means an unknown value
3560 * with EQAA, so those will map to 0. */
3561 final_sample
= LLVMBuildAnd(ac
->builder
, final_sample
,
3562 LLVMConstInt(ac
->i32
, 0x7, 0), "");
3564 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
3565 * resource descriptor is 0 (invalid).
3568 tmp
= LLVMBuildBitCast(ac
->builder
, fmask
, ac
->v8i32
, "");
3569 tmp
= LLVMBuildExtractElement(ac
->builder
, tmp
, ac
->i32_1
, "");
3570 tmp
= LLVMBuildICmp(ac
->builder
, LLVMIntNE
, tmp
, ac
->i32_0
, "");
3572 /* Replace the MSAA sample index. */
3573 addr
[sample_chan
] = LLVMBuildSelect(ac
->builder
, tmp
, final_sample
,
3574 addr
[sample_chan
], "");
3578 _ac_build_readlane(struct ac_llvm_context
*ctx
, LLVMValueRef src
, LLVMValueRef lane
)
3580 LLVMTypeRef type
= LLVMTypeOf(src
);
3581 LLVMValueRef result
;
3583 ac_build_optimization_barrier(ctx
, &src
);
3585 src
= LLVMBuildZExt(ctx
->builder
, src
, ctx
->i32
, "");
3587 lane
= LLVMBuildZExt(ctx
->builder
, lane
, ctx
->i32
, "");
3589 result
= ac_build_intrinsic(ctx
,
3590 lane
== NULL
? "llvm.amdgcn.readfirstlane" : "llvm.amdgcn.readlane",
3591 ctx
->i32
, (LLVMValueRef
[]) { src
, lane
},
3592 lane
== NULL
? 1 : 2,
3593 AC_FUNC_ATTR_READNONE
|
3594 AC_FUNC_ATTR_CONVERGENT
);
3596 return LLVMBuildTrunc(ctx
->builder
, result
, type
, "");
3600 * Builds the "llvm.amdgcn.readlane" or "llvm.amdgcn.readfirstlane" intrinsic.
3603 * @param lane - id of the lane or NULL for the first active lane
3604 * @return value of the lane
3607 ac_build_readlane(struct ac_llvm_context
*ctx
, LLVMValueRef src
, LLVMValueRef lane
)
3609 LLVMTypeRef src_type
= LLVMTypeOf(src
);
3610 src
= ac_to_integer(ctx
, src
);
3611 unsigned bits
= LLVMGetIntTypeWidth(LLVMTypeOf(src
));
3615 assert(bits
% 32 == 0);
3616 LLVMTypeRef vec_type
= LLVMVectorType(ctx
->i32
, bits
/ 32);
3617 LLVMValueRef src_vector
=
3618 LLVMBuildBitCast(ctx
->builder
, src
, vec_type
, "");
3619 ret
= LLVMGetUndef(vec_type
);
3620 for (unsigned i
= 0; i
< bits
/ 32; i
++) {
3621 src
= LLVMBuildExtractElement(ctx
->builder
, src_vector
,
3622 LLVMConstInt(ctx
->i32
, i
, 0), "");
3623 LLVMValueRef ret_comp
= _ac_build_readlane(ctx
, src
, lane
);
3624 ret
= LLVMBuildInsertElement(ctx
->builder
, ret
, ret_comp
,
3625 LLVMConstInt(ctx
->i32
, i
, 0), "");
3628 ret
= _ac_build_readlane(ctx
, src
, lane
);
3631 if (LLVMGetTypeKind(src_type
) == LLVMPointerTypeKind
)
3632 return LLVMBuildIntToPtr(ctx
->builder
, ret
, src_type
, "");
3633 return LLVMBuildBitCast(ctx
->builder
, ret
, src_type
, "");
3637 ac_build_writelane(struct ac_llvm_context
*ctx
, LLVMValueRef src
, LLVMValueRef value
, LLVMValueRef lane
)
3639 return ac_build_intrinsic(ctx
, "llvm.amdgcn.writelane", ctx
->i32
,
3640 (LLVMValueRef
[]) {value
, lane
, src
}, 3,
3641 AC_FUNC_ATTR_READNONE
| AC_FUNC_ATTR_CONVERGENT
);
3645 ac_build_mbcnt(struct ac_llvm_context
*ctx
, LLVMValueRef mask
)
3647 if (ctx
->wave_size
== 32) {
3648 return ac_build_intrinsic(ctx
, "llvm.amdgcn.mbcnt.lo", ctx
->i32
,
3649 (LLVMValueRef
[]) { mask
, ctx
->i32_0
},
3650 2, AC_FUNC_ATTR_READNONE
);
3652 LLVMValueRef mask_vec
= LLVMBuildBitCast(ctx
->builder
, mask
,
3653 LLVMVectorType(ctx
->i32
, 2),
3655 LLVMValueRef mask_lo
= LLVMBuildExtractElement(ctx
->builder
, mask_vec
,
3657 LLVMValueRef mask_hi
= LLVMBuildExtractElement(ctx
->builder
, mask_vec
,
3660 ac_build_intrinsic(ctx
, "llvm.amdgcn.mbcnt.lo", ctx
->i32
,
3661 (LLVMValueRef
[]) { mask_lo
, ctx
->i32_0
},
3662 2, AC_FUNC_ATTR_READNONE
);
3663 val
= ac_build_intrinsic(ctx
, "llvm.amdgcn.mbcnt.hi", ctx
->i32
,
3664 (LLVMValueRef
[]) { mask_hi
, val
},
3665 2, AC_FUNC_ATTR_READNONE
);
3670 _dpp_quad_perm
= 0x000,
3671 _dpp_row_sl
= 0x100,
3672 _dpp_row_sr
= 0x110,
3673 _dpp_row_rr
= 0x120,
3678 dpp_row_mirror
= 0x140,
3679 dpp_row_half_mirror
= 0x141,
3680 dpp_row_bcast15
= 0x142,
3681 dpp_row_bcast31
= 0x143
3684 static inline enum dpp_ctrl
3685 dpp_quad_perm(unsigned lane0
, unsigned lane1
, unsigned lane2
, unsigned lane3
)
3687 assert(lane0
< 4 && lane1
< 4 && lane2
< 4 && lane3
< 4);
3688 return _dpp_quad_perm
| lane0
| (lane1
<< 2) | (lane2
<< 4) | (lane3
<< 6);
3691 static inline enum dpp_ctrl
3692 dpp_row_sl(unsigned amount
)
3694 assert(amount
> 0 && amount
< 16);
3695 return _dpp_row_sl
| amount
;
3698 static inline enum dpp_ctrl
3699 dpp_row_sr(unsigned amount
)
3701 assert(amount
> 0 && amount
< 16);
3702 return _dpp_row_sr
| amount
;
3706 _ac_build_dpp(struct ac_llvm_context
*ctx
, LLVMValueRef old
, LLVMValueRef src
,
3707 enum dpp_ctrl dpp_ctrl
, unsigned row_mask
, unsigned bank_mask
,
3710 LLVMTypeRef type
= LLVMTypeOf(src
);
3713 old
= LLVMBuildZExt(ctx
->builder
, old
, ctx
->i32
, "");
3714 src
= LLVMBuildZExt(ctx
->builder
, src
, ctx
->i32
, "");
3716 res
= ac_build_intrinsic(ctx
, "llvm.amdgcn.update.dpp.i32", ctx
->i32
,
3719 LLVMConstInt(ctx
->i32
, dpp_ctrl
, 0),
3720 LLVMConstInt(ctx
->i32
, row_mask
, 0),
3721 LLVMConstInt(ctx
->i32
, bank_mask
, 0),
3722 LLVMConstInt(ctx
->i1
, bound_ctrl
, 0) },
3723 6, AC_FUNC_ATTR_READNONE
| AC_FUNC_ATTR_CONVERGENT
);
3725 return LLVMBuildTrunc(ctx
->builder
, res
, type
, "");
3729 ac_build_dpp(struct ac_llvm_context
*ctx
, LLVMValueRef old
, LLVMValueRef src
,
3730 enum dpp_ctrl dpp_ctrl
, unsigned row_mask
, unsigned bank_mask
,
3733 LLVMTypeRef src_type
= LLVMTypeOf(src
);
3734 src
= ac_to_integer(ctx
, src
);
3735 old
= ac_to_integer(ctx
, old
);
3736 unsigned bits
= LLVMGetIntTypeWidth(LLVMTypeOf(src
));
3739 assert(bits
% 32 == 0);
3740 LLVMTypeRef vec_type
= LLVMVectorType(ctx
->i32
, bits
/ 32);
3741 LLVMValueRef src_vector
=
3742 LLVMBuildBitCast(ctx
->builder
, src
, vec_type
, "");
3743 LLVMValueRef old_vector
=
3744 LLVMBuildBitCast(ctx
->builder
, old
, vec_type
, "");
3745 ret
= LLVMGetUndef(vec_type
);
3746 for (unsigned i
= 0; i
< bits
/ 32; i
++) {
3747 src
= LLVMBuildExtractElement(ctx
->builder
, src_vector
,
3748 LLVMConstInt(ctx
->i32
, i
,
3750 old
= LLVMBuildExtractElement(ctx
->builder
, old_vector
,
3751 LLVMConstInt(ctx
->i32
, i
,
3753 LLVMValueRef ret_comp
= _ac_build_dpp(ctx
, old
, src
,
3758 ret
= LLVMBuildInsertElement(ctx
->builder
, ret
,
3760 LLVMConstInt(ctx
->i32
, i
,
3764 ret
= _ac_build_dpp(ctx
, old
, src
, dpp_ctrl
, row_mask
,
3765 bank_mask
, bound_ctrl
);
3767 return LLVMBuildBitCast(ctx
->builder
, ret
, src_type
, "");
3771 _ac_build_permlane16(struct ac_llvm_context
*ctx
, LLVMValueRef src
, uint64_t sel
,
3772 bool exchange_rows
, bool bound_ctrl
)
3774 LLVMValueRef args
[6] = {
3777 LLVMConstInt(ctx
->i32
, sel
, false),
3778 LLVMConstInt(ctx
->i32
, sel
>> 32, false),
3779 ctx
->i1true
, /* fi */
3780 bound_ctrl
? ctx
->i1true
: ctx
->i1false
,
3782 return ac_build_intrinsic(ctx
, exchange_rows
? "llvm.amdgcn.permlanex16"
3783 : "llvm.amdgcn.permlane16",
3785 AC_FUNC_ATTR_READNONE
| AC_FUNC_ATTR_CONVERGENT
);
3789 ac_build_permlane16(struct ac_llvm_context
*ctx
, LLVMValueRef src
, uint64_t sel
,
3790 bool exchange_rows
, bool bound_ctrl
)
3792 LLVMTypeRef src_type
= LLVMTypeOf(src
);
3793 src
= ac_to_integer(ctx
, src
);
3794 unsigned bits
= LLVMGetIntTypeWidth(LLVMTypeOf(src
));
3797 ret
= _ac_build_permlane16(ctx
, src
, sel
, exchange_rows
,
3800 assert(bits
% 32 == 0);
3801 LLVMTypeRef vec_type
= LLVMVectorType(ctx
->i32
, bits
/ 32);
3802 LLVMValueRef src_vector
=
3803 LLVMBuildBitCast(ctx
->builder
, src
, vec_type
, "");
3804 ret
= LLVMGetUndef(vec_type
);
3805 for (unsigned i
= 0; i
< bits
/ 32; i
++) {
3806 src
= LLVMBuildExtractElement(ctx
->builder
, src_vector
,
3807 LLVMConstInt(ctx
->i32
, i
,
3809 LLVMValueRef ret_comp
=
3810 _ac_build_permlane16(ctx
, src
, sel
,
3813 ret
= LLVMBuildInsertElement(ctx
->builder
, ret
,
3815 LLVMConstInt(ctx
->i32
, i
,
3819 return LLVMBuildBitCast(ctx
->builder
, ret
, src_type
, "");
3822 static inline unsigned
3823 ds_pattern_bitmode(unsigned and_mask
, unsigned or_mask
, unsigned xor_mask
)
3825 assert(and_mask
< 32 && or_mask
< 32 && xor_mask
< 32);
3826 return and_mask
| (or_mask
<< 5) | (xor_mask
<< 10);
3830 _ac_build_ds_swizzle(struct ac_llvm_context
*ctx
, LLVMValueRef src
, unsigned mask
)
3832 LLVMTypeRef src_type
= LLVMTypeOf(src
);
3835 src
= LLVMBuildZExt(ctx
->builder
, src
, ctx
->i32
, "");
3837 ret
= ac_build_intrinsic(ctx
, "llvm.amdgcn.ds.swizzle", ctx
->i32
,
3839 src
, LLVMConstInt(ctx
->i32
, mask
, 0) },
3840 2, AC_FUNC_ATTR_READNONE
| AC_FUNC_ATTR_CONVERGENT
);
3842 return LLVMBuildTrunc(ctx
->builder
, ret
, src_type
, "");
3846 ac_build_ds_swizzle(struct ac_llvm_context
*ctx
, LLVMValueRef src
, unsigned mask
)
3848 LLVMTypeRef src_type
= LLVMTypeOf(src
);
3849 src
= ac_to_integer(ctx
, src
);
3850 unsigned bits
= LLVMGetIntTypeWidth(LLVMTypeOf(src
));
3853 assert(bits
% 32 == 0);
3854 LLVMTypeRef vec_type
= LLVMVectorType(ctx
->i32
, bits
/ 32);
3855 LLVMValueRef src_vector
=
3856 LLVMBuildBitCast(ctx
->builder
, src
, vec_type
, "");
3857 ret
= LLVMGetUndef(vec_type
);
3858 for (unsigned i
= 0; i
< bits
/ 32; i
++) {
3859 src
= LLVMBuildExtractElement(ctx
->builder
, src_vector
,
3860 LLVMConstInt(ctx
->i32
, i
,
3862 LLVMValueRef ret_comp
= _ac_build_ds_swizzle(ctx
, src
,
3864 ret
= LLVMBuildInsertElement(ctx
->builder
, ret
,
3866 LLVMConstInt(ctx
->i32
, i
,
3870 ret
= _ac_build_ds_swizzle(ctx
, src
, mask
);
3872 return LLVMBuildBitCast(ctx
->builder
, ret
, src_type
, "");
3876 ac_build_wwm(struct ac_llvm_context
*ctx
, LLVMValueRef src
)
3878 LLVMTypeRef src_type
= LLVMTypeOf(src
);
3879 unsigned bitsize
= ac_get_elem_bits(ctx
, src_type
);
3880 char name
[32], type
[8];
3883 src
= ac_to_integer(ctx
, src
);
3886 src
= LLVMBuildZExt(ctx
->builder
, src
, ctx
->i32
, "");
3888 ac_build_type_name_for_intr(LLVMTypeOf(src
), type
, sizeof(type
));
3889 snprintf(name
, sizeof(name
), "llvm.amdgcn.wwm.%s", type
);
3890 ret
= ac_build_intrinsic(ctx
, name
, LLVMTypeOf(src
),
3891 (LLVMValueRef
[]) { src
}, 1,
3892 AC_FUNC_ATTR_READNONE
);
3895 ret
= LLVMBuildTrunc(ctx
->builder
, ret
,
3896 ac_to_integer_type(ctx
, src_type
), "");
3898 return LLVMBuildBitCast(ctx
->builder
, ret
, src_type
, "");
3902 ac_build_set_inactive(struct ac_llvm_context
*ctx
, LLVMValueRef src
,
3903 LLVMValueRef inactive
)
3905 char name
[33], type
[8];
3906 LLVMTypeRef src_type
= LLVMTypeOf(src
);
3907 unsigned bitsize
= ac_get_elem_bits(ctx
, src_type
);
3908 src
= ac_to_integer(ctx
, src
);
3909 inactive
= ac_to_integer(ctx
, inactive
);
3912 src
= LLVMBuildZExt(ctx
->builder
, src
, ctx
->i32
, "");
3913 inactive
= LLVMBuildZExt(ctx
->builder
, inactive
, ctx
->i32
, "");
3916 ac_build_type_name_for_intr(LLVMTypeOf(src
), type
, sizeof(type
));
3917 snprintf(name
, sizeof(name
), "llvm.amdgcn.set.inactive.%s", type
);
3919 ac_build_intrinsic(ctx
, name
,
3920 LLVMTypeOf(src
), (LLVMValueRef
[]) {
3922 AC_FUNC_ATTR_READNONE
|
3923 AC_FUNC_ATTR_CONVERGENT
);
3925 ret
= LLVMBuildTrunc(ctx
->builder
, ret
, src_type
, "");
3931 get_reduction_identity(struct ac_llvm_context
*ctx
, nir_op op
, unsigned type_size
)
3933 if (type_size
== 1) {
3935 case nir_op_iadd
: return ctx
->i8_0
;
3936 case nir_op_imul
: return ctx
->i8_1
;
3937 case nir_op_imin
: return LLVMConstInt(ctx
->i8
, INT8_MAX
, 0);
3938 case nir_op_umin
: return LLVMConstInt(ctx
->i8
, UINT8_MAX
, 0);
3939 case nir_op_imax
: return LLVMConstInt(ctx
->i8
, INT8_MIN
, 0);
3940 case nir_op_umax
: return ctx
->i8_0
;
3941 case nir_op_iand
: return LLVMConstInt(ctx
->i8
, -1, 0);
3942 case nir_op_ior
: return ctx
->i8_0
;
3943 case nir_op_ixor
: return ctx
->i8_0
;
3945 unreachable("bad reduction intrinsic");
3947 } else if (type_size
== 2) {
3949 case nir_op_iadd
: return ctx
->i16_0
;
3950 case nir_op_fadd
: return ctx
->f16_0
;
3951 case nir_op_imul
: return ctx
->i16_1
;
3952 case nir_op_fmul
: return ctx
->f16_1
;
3953 case nir_op_imin
: return LLVMConstInt(ctx
->i16
, INT16_MAX
, 0);
3954 case nir_op_umin
: return LLVMConstInt(ctx
->i16
, UINT16_MAX
, 0);
3955 case nir_op_fmin
: return LLVMConstReal(ctx
->f16
, INFINITY
);
3956 case nir_op_imax
: return LLVMConstInt(ctx
->i16
, INT16_MIN
, 0);
3957 case nir_op_umax
: return ctx
->i16_0
;
3958 case nir_op_fmax
: return LLVMConstReal(ctx
->f16
, -INFINITY
);
3959 case nir_op_iand
: return LLVMConstInt(ctx
->i16
, -1, 0);
3960 case nir_op_ior
: return ctx
->i16_0
;
3961 case nir_op_ixor
: return ctx
->i16_0
;
3963 unreachable("bad reduction intrinsic");
3965 } else if (type_size
== 4) {
3967 case nir_op_iadd
: return ctx
->i32_0
;
3968 case nir_op_fadd
: return ctx
->f32_0
;
3969 case nir_op_imul
: return ctx
->i32_1
;
3970 case nir_op_fmul
: return ctx
->f32_1
;
3971 case nir_op_imin
: return LLVMConstInt(ctx
->i32
, INT32_MAX
, 0);
3972 case nir_op_umin
: return LLVMConstInt(ctx
->i32
, UINT32_MAX
, 0);
3973 case nir_op_fmin
: return LLVMConstReal(ctx
->f32
, INFINITY
);
3974 case nir_op_imax
: return LLVMConstInt(ctx
->i32
, INT32_MIN
, 0);
3975 case nir_op_umax
: return ctx
->i32_0
;
3976 case nir_op_fmax
: return LLVMConstReal(ctx
->f32
, -INFINITY
);
3977 case nir_op_iand
: return LLVMConstInt(ctx
->i32
, -1, 0);
3978 case nir_op_ior
: return ctx
->i32_0
;
3979 case nir_op_ixor
: return ctx
->i32_0
;
3981 unreachable("bad reduction intrinsic");
3983 } else { /* type_size == 64bit */
3985 case nir_op_iadd
: return ctx
->i64_0
;
3986 case nir_op_fadd
: return ctx
->f64_0
;
3987 case nir_op_imul
: return ctx
->i64_1
;
3988 case nir_op_fmul
: return ctx
->f64_1
;
3989 case nir_op_imin
: return LLVMConstInt(ctx
->i64
, INT64_MAX
, 0);
3990 case nir_op_umin
: return LLVMConstInt(ctx
->i64
, UINT64_MAX
, 0);
3991 case nir_op_fmin
: return LLVMConstReal(ctx
->f64
, INFINITY
);
3992 case nir_op_imax
: return LLVMConstInt(ctx
->i64
, INT64_MIN
, 0);
3993 case nir_op_umax
: return ctx
->i64_0
;
3994 case nir_op_fmax
: return LLVMConstReal(ctx
->f64
, -INFINITY
);
3995 case nir_op_iand
: return LLVMConstInt(ctx
->i64
, -1, 0);
3996 case nir_op_ior
: return ctx
->i64_0
;
3997 case nir_op_ixor
: return ctx
->i64_0
;
3999 unreachable("bad reduction intrinsic");
4005 ac_build_alu_op(struct ac_llvm_context
*ctx
, LLVMValueRef lhs
, LLVMValueRef rhs
, nir_op op
)
4007 bool _64bit
= ac_get_type_size(LLVMTypeOf(lhs
)) == 8;
4008 bool _32bit
= ac_get_type_size(LLVMTypeOf(lhs
)) == 4;
4010 case nir_op_iadd
: return LLVMBuildAdd(ctx
->builder
, lhs
, rhs
, "");
4011 case nir_op_fadd
: return LLVMBuildFAdd(ctx
->builder
, lhs
, rhs
, "");
4012 case nir_op_imul
: return LLVMBuildMul(ctx
->builder
, lhs
, rhs
, "");
4013 case nir_op_fmul
: return LLVMBuildFMul(ctx
->builder
, lhs
, rhs
, "");
4014 case nir_op_imin
: return LLVMBuildSelect(ctx
->builder
,
4015 LLVMBuildICmp(ctx
->builder
, LLVMIntSLT
, lhs
, rhs
, ""),
4017 case nir_op_umin
: return LLVMBuildSelect(ctx
->builder
,
4018 LLVMBuildICmp(ctx
->builder
, LLVMIntULT
, lhs
, rhs
, ""),
4020 case nir_op_fmin
: return ac_build_intrinsic(ctx
,
4021 _64bit
? "llvm.minnum.f64" : _32bit
? "llvm.minnum.f32" : "llvm.minnum.f16",
4022 _64bit
? ctx
->f64
: _32bit
? ctx
->f32
: ctx
->f16
,
4023 (LLVMValueRef
[]){lhs
, rhs
}, 2, AC_FUNC_ATTR_READNONE
);
4024 case nir_op_imax
: return LLVMBuildSelect(ctx
->builder
,
4025 LLVMBuildICmp(ctx
->builder
, LLVMIntSGT
, lhs
, rhs
, ""),
4027 case nir_op_umax
: return LLVMBuildSelect(ctx
->builder
,
4028 LLVMBuildICmp(ctx
->builder
, LLVMIntUGT
, lhs
, rhs
, ""),
4030 case nir_op_fmax
: return ac_build_intrinsic(ctx
,
4031 _64bit
? "llvm.maxnum.f64" : _32bit
? "llvm.maxnum.f32" : "llvm.maxnum.f16",
4032 _64bit
? ctx
->f64
: _32bit
? ctx
->f32
: ctx
->f16
,
4033 (LLVMValueRef
[]){lhs
, rhs
}, 2, AC_FUNC_ATTR_READNONE
);
4034 case nir_op_iand
: return LLVMBuildAnd(ctx
->builder
, lhs
, rhs
, "");
4035 case nir_op_ior
: return LLVMBuildOr(ctx
->builder
, lhs
, rhs
, "");
4036 case nir_op_ixor
: return LLVMBuildXor(ctx
->builder
, lhs
, rhs
, "");
4038 unreachable("bad reduction intrinsic");
4043 * \param maxprefix specifies that the result only needs to be correct for a
4044 * prefix of this many threads
4046 * TODO: add inclusive and excluse scan functions for GFX6.
4049 ac_build_scan(struct ac_llvm_context
*ctx
, nir_op op
, LLVMValueRef src
, LLVMValueRef identity
,
4050 unsigned maxprefix
, bool inclusive
)
4052 LLVMValueRef result
, tmp
;
4054 if (ctx
->chip_class
>= GFX10
) {
4055 result
= inclusive
? src
: identity
;
4058 src
= ac_build_dpp(ctx
, identity
, src
, dpp_wf_sr1
, 0xf, 0xf, false);
4063 tmp
= ac_build_dpp(ctx
, identity
, src
, dpp_row_sr(1), 0xf, 0xf, false);
4064 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
4067 tmp
= ac_build_dpp(ctx
, identity
, src
, dpp_row_sr(2), 0xf, 0xf, false);
4068 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
4071 tmp
= ac_build_dpp(ctx
, identity
, src
, dpp_row_sr(3), 0xf, 0xf, false);
4072 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
4075 tmp
= ac_build_dpp(ctx
, identity
, result
, dpp_row_sr(4), 0xf, 0xe, false);
4076 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
4079 tmp
= ac_build_dpp(ctx
, identity
, result
, dpp_row_sr(8), 0xf, 0xc, false);
4080 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
4081 if (maxprefix
<= 16)
4084 if (ctx
->chip_class
>= GFX10
) {
4085 /* dpp_row_bcast{15,31} are not supported on gfx10. */
4086 LLVMBuilderRef builder
= ctx
->builder
;
4087 LLVMValueRef tid
= ac_get_thread_id(ctx
);
4089 /* TODO-GFX10: Can we get better code-gen by putting this into
4090 * a branch so that LLVM generates EXEC mask manipulations? */
4094 tmp
= ac_build_alu_op(ctx
, result
, src
, op
);
4095 tmp
= ac_build_permlane16(ctx
, tmp
, ~(uint64_t)0, true, false);
4096 tmp
= ac_build_alu_op(ctx
, result
, tmp
, op
);
4097 cc
= LLVMBuildAnd(builder
, tid
, LLVMConstInt(ctx
->i32
, 16, false), "");
4098 cc
= LLVMBuildICmp(builder
, LLVMIntNE
, cc
, ctx
->i32_0
, "");
4099 result
= LLVMBuildSelect(builder
, cc
, tmp
, result
, "");
4100 if (maxprefix
<= 32)
4106 tmp
= ac_build_alu_op(ctx
, result
, src
, op
);
4107 tmp
= ac_build_readlane(ctx
, tmp
, LLVMConstInt(ctx
->i32
, 31, false));
4108 tmp
= ac_build_alu_op(ctx
, result
, tmp
, op
);
4109 cc
= LLVMBuildICmp(builder
, LLVMIntUGE
, tid
,
4110 LLVMConstInt(ctx
->i32
, 32, false), "");
4111 result
= LLVMBuildSelect(builder
, cc
, tmp
, result
, "");
4115 tmp
= ac_build_dpp(ctx
, identity
, result
, dpp_row_bcast15
, 0xa, 0xf, false);
4116 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
4117 if (maxprefix
<= 32)
4119 tmp
= ac_build_dpp(ctx
, identity
, result
, dpp_row_bcast31
, 0xc, 0xf, false);
4120 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
4125 ac_build_inclusive_scan(struct ac_llvm_context
*ctx
, LLVMValueRef src
, nir_op op
)
4127 LLVMValueRef result
;
4129 if (LLVMTypeOf(src
) == ctx
->i1
&& op
== nir_op_iadd
) {
4130 LLVMBuilderRef builder
= ctx
->builder
;
4131 src
= LLVMBuildZExt(builder
, src
, ctx
->i32
, "");
4132 result
= ac_build_ballot(ctx
, src
);
4133 result
= ac_build_mbcnt(ctx
, result
);
4134 result
= LLVMBuildAdd(builder
, result
, src
, "");
4138 ac_build_optimization_barrier(ctx
, &src
);
4140 LLVMValueRef identity
=
4141 get_reduction_identity(ctx
, op
, ac_get_type_size(LLVMTypeOf(src
)));
4142 result
= LLVMBuildBitCast(ctx
->builder
, ac_build_set_inactive(ctx
, src
, identity
),
4143 LLVMTypeOf(identity
), "");
4144 result
= ac_build_scan(ctx
, op
, result
, identity
, ctx
->wave_size
, true);
4146 return ac_build_wwm(ctx
, result
);
4150 ac_build_exclusive_scan(struct ac_llvm_context
*ctx
, LLVMValueRef src
, nir_op op
)
4152 LLVMValueRef result
;
4154 if (LLVMTypeOf(src
) == ctx
->i1
&& op
== nir_op_iadd
) {
4155 LLVMBuilderRef builder
= ctx
->builder
;
4156 src
= LLVMBuildZExt(builder
, src
, ctx
->i32
, "");
4157 result
= ac_build_ballot(ctx
, src
);
4158 result
= ac_build_mbcnt(ctx
, result
);
4162 ac_build_optimization_barrier(ctx
, &src
);
4164 LLVMValueRef identity
=
4165 get_reduction_identity(ctx
, op
, ac_get_type_size(LLVMTypeOf(src
)));
4166 result
= LLVMBuildBitCast(ctx
->builder
, ac_build_set_inactive(ctx
, src
, identity
),
4167 LLVMTypeOf(identity
), "");
4168 result
= ac_build_scan(ctx
, op
, result
, identity
, ctx
->wave_size
, false);
4170 return ac_build_wwm(ctx
, result
);
4174 ac_build_reduce(struct ac_llvm_context
*ctx
, LLVMValueRef src
, nir_op op
, unsigned cluster_size
)
4176 if (cluster_size
== 1) return src
;
4177 ac_build_optimization_barrier(ctx
, &src
);
4178 LLVMValueRef result
, swap
;
4179 LLVMValueRef identity
= get_reduction_identity(ctx
, op
,
4180 ac_get_type_size(LLVMTypeOf(src
)));
4181 result
= LLVMBuildBitCast(ctx
->builder
,
4182 ac_build_set_inactive(ctx
, src
, identity
),
4183 LLVMTypeOf(identity
), "");
4184 swap
= ac_build_quad_swizzle(ctx
, result
, 1, 0, 3, 2);
4185 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
4186 if (cluster_size
== 2) return ac_build_wwm(ctx
, result
);
4188 swap
= ac_build_quad_swizzle(ctx
, result
, 2, 3, 0, 1);
4189 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
4190 if (cluster_size
== 4) return ac_build_wwm(ctx
, result
);
4192 if (ctx
->chip_class
>= GFX8
)
4193 swap
= ac_build_dpp(ctx
, identity
, result
, dpp_row_half_mirror
, 0xf, 0xf, false);
4195 swap
= ac_build_ds_swizzle(ctx
, result
, ds_pattern_bitmode(0x1f, 0, 0x04));
4196 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
4197 if (cluster_size
== 8) return ac_build_wwm(ctx
, result
);
4199 if (ctx
->chip_class
>= GFX8
)
4200 swap
= ac_build_dpp(ctx
, identity
, result
, dpp_row_mirror
, 0xf, 0xf, false);
4202 swap
= ac_build_ds_swizzle(ctx
, result
, ds_pattern_bitmode(0x1f, 0, 0x08));
4203 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
4204 if (cluster_size
== 16) return ac_build_wwm(ctx
, result
);
4206 if (ctx
->chip_class
>= GFX10
)
4207 swap
= ac_build_permlane16(ctx
, result
, 0, true, false);
4208 else if (ctx
->chip_class
>= GFX8
&& cluster_size
!= 32)
4209 swap
= ac_build_dpp(ctx
, identity
, result
, dpp_row_bcast15
, 0xa, 0xf, false);
4211 swap
= ac_build_ds_swizzle(ctx
, result
, ds_pattern_bitmode(0x1f, 0, 0x10));
4212 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
4213 if (cluster_size
== 32) return ac_build_wwm(ctx
, result
);
4215 if (ctx
->chip_class
>= GFX8
) {
4216 if (ctx
->chip_class
>= GFX10
)
4217 swap
= ac_build_readlane(ctx
, result
, LLVMConstInt(ctx
->i32
, 31, false));
4219 swap
= ac_build_dpp(ctx
, identity
, result
, dpp_row_bcast31
, 0xc, 0xf, false);
4220 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
4221 result
= ac_build_readlane(ctx
, result
, LLVMConstInt(ctx
->i32
, 63, 0));
4222 return ac_build_wwm(ctx
, result
);
4224 swap
= ac_build_readlane(ctx
, result
, ctx
->i32_0
);
4225 result
= ac_build_readlane(ctx
, result
, LLVMConstInt(ctx
->i32
, 32, 0));
4226 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
4227 return ac_build_wwm(ctx
, result
);
4232 * "Top half" of a scan that reduces per-wave values across an entire
4235 * The source value must be present in the highest lane of the wave, and the
4236 * highest lane must be live.
4239 ac_build_wg_wavescan_top(struct ac_llvm_context
*ctx
, struct ac_wg_scan
*ws
)
4241 if (ws
->maxwaves
<= 1)
4244 const LLVMValueRef last_lane
= LLVMConstInt(ctx
->i32
, ctx
->wave_size
- 1, false);
4245 LLVMBuilderRef builder
= ctx
->builder
;
4246 LLVMValueRef tid
= ac_get_thread_id(ctx
);
4249 tmp
= LLVMBuildICmp(builder
, LLVMIntEQ
, tid
, last_lane
, "");
4250 ac_build_ifcc(ctx
, tmp
, 1000);
4251 LLVMBuildStore(builder
, ws
->src
, LLVMBuildGEP(builder
, ws
->scratch
, &ws
->waveidx
, 1, ""));
4252 ac_build_endif(ctx
, 1000);
4256 * "Bottom half" of a scan that reduces per-wave values across an entire
4259 * The caller must place a barrier between the top and bottom halves.
4262 ac_build_wg_wavescan_bottom(struct ac_llvm_context
*ctx
, struct ac_wg_scan
*ws
)
4264 const LLVMTypeRef type
= LLVMTypeOf(ws
->src
);
4265 const LLVMValueRef identity
=
4266 get_reduction_identity(ctx
, ws
->op
, ac_get_type_size(type
));
4268 if (ws
->maxwaves
<= 1) {
4269 ws
->result_reduce
= ws
->src
;
4270 ws
->result_inclusive
= ws
->src
;
4271 ws
->result_exclusive
= identity
;
4274 assert(ws
->maxwaves
<= 32);
4276 LLVMBuilderRef builder
= ctx
->builder
;
4277 LLVMValueRef tid
= ac_get_thread_id(ctx
);
4278 LLVMBasicBlockRef bbs
[2];
4279 LLVMValueRef phivalues_scan
[2];
4280 LLVMValueRef tmp
, tmp2
;
4282 bbs
[0] = LLVMGetInsertBlock(builder
);
4283 phivalues_scan
[0] = LLVMGetUndef(type
);
4285 if (ws
->enable_reduce
)
4286 tmp
= LLVMBuildICmp(builder
, LLVMIntULT
, tid
, ws
->numwaves
, "");
4287 else if (ws
->enable_inclusive
)
4288 tmp
= LLVMBuildICmp(builder
, LLVMIntULE
, tid
, ws
->waveidx
, "");
4290 tmp
= LLVMBuildICmp(builder
, LLVMIntULT
, tid
, ws
->waveidx
, "");
4291 ac_build_ifcc(ctx
, tmp
, 1001);
4293 tmp
= LLVMBuildLoad(builder
, LLVMBuildGEP(builder
, ws
->scratch
, &tid
, 1, ""), "");
4295 ac_build_optimization_barrier(ctx
, &tmp
);
4297 bbs
[1] = LLVMGetInsertBlock(builder
);
4298 phivalues_scan
[1] = ac_build_scan(ctx
, ws
->op
, tmp
, identity
, ws
->maxwaves
, true);
4300 ac_build_endif(ctx
, 1001);
4302 const LLVMValueRef scan
= ac_build_phi(ctx
, type
, 2, phivalues_scan
, bbs
);
4304 if (ws
->enable_reduce
) {
4305 tmp
= LLVMBuildSub(builder
, ws
->numwaves
, ctx
->i32_1
, "");
4306 ws
->result_reduce
= ac_build_readlane(ctx
, scan
, tmp
);
4308 if (ws
->enable_inclusive
)
4309 ws
->result_inclusive
= ac_build_readlane(ctx
, scan
, ws
->waveidx
);
4310 if (ws
->enable_exclusive
) {
4311 tmp
= LLVMBuildSub(builder
, ws
->waveidx
, ctx
->i32_1
, "");
4312 tmp
= ac_build_readlane(ctx
, scan
, tmp
);
4313 tmp2
= LLVMBuildICmp(builder
, LLVMIntEQ
, ws
->waveidx
, ctx
->i32_0
, "");
4314 ws
->result_exclusive
= LLVMBuildSelect(builder
, tmp2
, identity
, tmp
, "");
4319 * Inclusive scan of a per-wave value across an entire workgroup.
4321 * This implies an s_barrier instruction.
4323 * Unlike ac_build_inclusive_scan, the caller \em must ensure that all threads
4324 * of the workgroup are live. (This requirement cannot easily be relaxed in a
4325 * useful manner because of the barrier in the algorithm.)
4328 ac_build_wg_wavescan(struct ac_llvm_context
*ctx
, struct ac_wg_scan
*ws
)
4330 ac_build_wg_wavescan_top(ctx
, ws
);
4331 ac_build_s_barrier(ctx
);
4332 ac_build_wg_wavescan_bottom(ctx
, ws
);
4336 * "Top half" of a scan that reduces per-thread values across an entire
4339 * All lanes must be active when this code runs.
4342 ac_build_wg_scan_top(struct ac_llvm_context
*ctx
, struct ac_wg_scan
*ws
)
4344 if (ws
->enable_exclusive
) {
4345 ws
->extra
= ac_build_exclusive_scan(ctx
, ws
->src
, ws
->op
);
4346 if (LLVMTypeOf(ws
->src
) == ctx
->i1
&& ws
->op
== nir_op_iadd
)
4347 ws
->src
= LLVMBuildZExt(ctx
->builder
, ws
->src
, ctx
->i32
, "");
4348 ws
->src
= ac_build_alu_op(ctx
, ws
->extra
, ws
->src
, ws
->op
);
4350 ws
->src
= ac_build_inclusive_scan(ctx
, ws
->src
, ws
->op
);
4353 bool enable_inclusive
= ws
->enable_inclusive
;
4354 bool enable_exclusive
= ws
->enable_exclusive
;
4355 ws
->enable_inclusive
= false;
4356 ws
->enable_exclusive
= ws
->enable_exclusive
|| enable_inclusive
;
4357 ac_build_wg_wavescan_top(ctx
, ws
);
4358 ws
->enable_inclusive
= enable_inclusive
;
4359 ws
->enable_exclusive
= enable_exclusive
;
4363 * "Bottom half" of a scan that reduces per-thread values across an entire
4366 * The caller must place a barrier between the top and bottom halves.
4369 ac_build_wg_scan_bottom(struct ac_llvm_context
*ctx
, struct ac_wg_scan
*ws
)
4371 bool enable_inclusive
= ws
->enable_inclusive
;
4372 bool enable_exclusive
= ws
->enable_exclusive
;
4373 ws
->enable_inclusive
= false;
4374 ws
->enable_exclusive
= ws
->enable_exclusive
|| enable_inclusive
;
4375 ac_build_wg_wavescan_bottom(ctx
, ws
);
4376 ws
->enable_inclusive
= enable_inclusive
;
4377 ws
->enable_exclusive
= enable_exclusive
;
4379 /* ws->result_reduce is already the correct value */
4380 if (ws
->enable_inclusive
)
4381 ws
->result_inclusive
= ac_build_alu_op(ctx
, ws
->result_inclusive
, ws
->src
, ws
->op
);
4382 if (ws
->enable_exclusive
)
4383 ws
->result_exclusive
= ac_build_alu_op(ctx
, ws
->result_exclusive
, ws
->extra
, ws
->op
);
4387 * A scan that reduces per-thread values across an entire workgroup.
4389 * The caller must ensure that all lanes are active when this code runs
4390 * (WWM is insufficient!), because there is an implied barrier.
4393 ac_build_wg_scan(struct ac_llvm_context
*ctx
, struct ac_wg_scan
*ws
)
4395 ac_build_wg_scan_top(ctx
, ws
);
4396 ac_build_s_barrier(ctx
);
4397 ac_build_wg_scan_bottom(ctx
, ws
);
4401 ac_build_quad_swizzle(struct ac_llvm_context
*ctx
, LLVMValueRef src
,
4402 unsigned lane0
, unsigned lane1
, unsigned lane2
, unsigned lane3
)
4404 unsigned mask
= dpp_quad_perm(lane0
, lane1
, lane2
, lane3
);
4405 if (ctx
->chip_class
>= GFX8
) {
4406 return ac_build_dpp(ctx
, src
, src
, mask
, 0xf, 0xf, false);
4408 return ac_build_ds_swizzle(ctx
, src
, (1 << 15) | mask
);
4413 ac_build_shuffle(struct ac_llvm_context
*ctx
, LLVMValueRef src
, LLVMValueRef index
)
4415 LLVMTypeRef type
= LLVMTypeOf(src
);
4416 LLVMValueRef result
;
4418 index
= LLVMBuildMul(ctx
->builder
, index
, LLVMConstInt(ctx
->i32
, 4, 0), "");
4419 src
= LLVMBuildZExt(ctx
->builder
, src
, ctx
->i32
, "");
4421 result
= ac_build_intrinsic(ctx
, "llvm.amdgcn.ds.bpermute", ctx
->i32
,
4422 (LLVMValueRef
[]) {index
, src
}, 2,
4423 AC_FUNC_ATTR_READNONE
|
4424 AC_FUNC_ATTR_CONVERGENT
);
4425 return LLVMBuildTrunc(ctx
->builder
, result
, type
, "");
4429 ac_build_frexp_exp(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
4435 if (bitsize
== 16) {
4436 intr
= "llvm.amdgcn.frexp.exp.i16.f16";
4438 } else if (bitsize
== 32) {
4439 intr
= "llvm.amdgcn.frexp.exp.i32.f32";
4442 intr
= "llvm.amdgcn.frexp.exp.i32.f64";
4446 LLVMValueRef params
[] = {
4449 return ac_build_intrinsic(ctx
, intr
, type
, params
, 1,
4450 AC_FUNC_ATTR_READNONE
);
4453 ac_build_frexp_mant(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
4459 if (bitsize
== 16) {
4460 intr
= "llvm.amdgcn.frexp.mant.f16";
4462 } else if (bitsize
== 32) {
4463 intr
= "llvm.amdgcn.frexp.mant.f32";
4466 intr
= "llvm.amdgcn.frexp.mant.f64";
4470 LLVMValueRef params
[] = {
4473 return ac_build_intrinsic(ctx
, intr
, type
, params
, 1,
4474 AC_FUNC_ATTR_READNONE
);
4478 ac_build_canonicalize(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
4484 if (bitsize
== 16) {
4485 intr
= "llvm.canonicalize.f16";
4487 } else if (bitsize
== 32) {
4488 intr
= "llvm.canonicalize.f32";
4490 } else if (bitsize
== 64) {
4491 intr
= "llvm.canonicalize.f64";
4495 LLVMValueRef params
[] = {
4498 return ac_build_intrinsic(ctx
, intr
, type
, params
, 1,
4499 AC_FUNC_ATTR_READNONE
);
4503 * this takes an I,J coordinate pair,
4504 * and works out the X and Y derivatives.
4505 * it returns DDX(I), DDX(J), DDY(I), DDY(J).
4508 ac_build_ddxy_interp(struct ac_llvm_context
*ctx
, LLVMValueRef interp_ij
)
4510 LLVMValueRef result
[4], a
;
4513 for (i
= 0; i
< 2; i
++) {
4514 a
= LLVMBuildExtractElement(ctx
->builder
, interp_ij
,
4515 LLVMConstInt(ctx
->i32
, i
, false), "");
4516 result
[i
] = ac_build_ddxy(ctx
, AC_TID_MASK_TOP_LEFT
, 1, a
);
4517 result
[2+i
] = ac_build_ddxy(ctx
, AC_TID_MASK_TOP_LEFT
, 2, a
);
4519 return ac_build_gather_values(ctx
, result
, 4);
4523 ac_build_load_helper_invocation(struct ac_llvm_context
*ctx
)
4525 LLVMValueRef result
= ac_build_intrinsic(ctx
, "llvm.amdgcn.ps.live",
4527 AC_FUNC_ATTR_READNONE
);
4528 result
= LLVMBuildNot(ctx
->builder
, result
, "");
4529 return LLVMBuildSExt(ctx
->builder
, result
, ctx
->i32
, "");
4532 LLVMValueRef
ac_build_call(struct ac_llvm_context
*ctx
, LLVMValueRef func
,
4533 LLVMValueRef
*args
, unsigned num_args
)
4535 LLVMValueRef ret
= LLVMBuildCall(ctx
->builder
, func
, args
, num_args
, "");
4536 LLVMSetInstructionCallConv(ret
, LLVMGetFunctionCallConv(func
));
4541 ac_export_mrt_z(struct ac_llvm_context
*ctx
, LLVMValueRef depth
,
4542 LLVMValueRef stencil
, LLVMValueRef samplemask
,
4543 struct ac_export_args
*args
)
4546 unsigned format
= ac_get_spi_shader_z_format(depth
!= NULL
,
4548 samplemask
!= NULL
);
4550 assert(depth
|| stencil
|| samplemask
);
4552 memset(args
, 0, sizeof(*args
));
4554 args
->valid_mask
= 1; /* whether the EXEC mask is valid */
4555 args
->done
= 1; /* DONE bit */
4557 /* Specify the target we are exporting */
4558 args
->target
= V_008DFC_SQ_EXP_MRTZ
;
4560 args
->compr
= 0; /* COMP flag */
4561 args
->out
[0] = LLVMGetUndef(ctx
->f32
); /* R, depth */
4562 args
->out
[1] = LLVMGetUndef(ctx
->f32
); /* G, stencil test val[0:7], stencil op val[8:15] */
4563 args
->out
[2] = LLVMGetUndef(ctx
->f32
); /* B, sample mask */
4564 args
->out
[3] = LLVMGetUndef(ctx
->f32
); /* A, alpha to mask */
4566 if (format
== V_028710_SPI_SHADER_UINT16_ABGR
) {
4568 args
->compr
= 1; /* COMPR flag */
4571 /* Stencil should be in X[23:16]. */
4572 stencil
= ac_to_integer(ctx
, stencil
);
4573 stencil
= LLVMBuildShl(ctx
->builder
, stencil
,
4574 LLVMConstInt(ctx
->i32
, 16, 0), "");
4575 args
->out
[0] = ac_to_float(ctx
, stencil
);
4579 /* SampleMask should be in Y[15:0]. */
4580 args
->out
[1] = samplemask
;
4585 args
->out
[0] = depth
;
4589 args
->out
[1] = stencil
;
4593 args
->out
[2] = samplemask
;
4598 /* GFX6 (except OLAND and HAINAN) has a bug that it only looks
4599 * at the X writemask component. */
4600 if (ctx
->chip_class
== GFX6
&&
4601 ctx
->family
!= CHIP_OLAND
&&
4602 ctx
->family
!= CHIP_HAINAN
)
4605 /* Specify which components to enable */
4606 args
->enabled_channels
= mask
;