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
->module
= ac_create_module(wave_size
== 32 ? compiler
->tm_wave32
79 ctx
->builder
= ac_create_builder(ctx
->context
, float_mode
);
81 ctx
->voidt
= LLVMVoidTypeInContext(ctx
->context
);
82 ctx
->i1
= LLVMInt1TypeInContext(ctx
->context
);
83 ctx
->i8
= LLVMInt8TypeInContext(ctx
->context
);
84 ctx
->i16
= LLVMIntTypeInContext(ctx
->context
, 16);
85 ctx
->i32
= LLVMIntTypeInContext(ctx
->context
, 32);
86 ctx
->i64
= LLVMIntTypeInContext(ctx
->context
, 64);
87 ctx
->intptr
= ctx
->i32
;
88 ctx
->f16
= LLVMHalfTypeInContext(ctx
->context
);
89 ctx
->f32
= LLVMFloatTypeInContext(ctx
->context
);
90 ctx
->f64
= LLVMDoubleTypeInContext(ctx
->context
);
91 ctx
->v2i16
= LLVMVectorType(ctx
->i16
, 2);
92 ctx
->v2i32
= LLVMVectorType(ctx
->i32
, 2);
93 ctx
->v3i32
= LLVMVectorType(ctx
->i32
, 3);
94 ctx
->v4i32
= LLVMVectorType(ctx
->i32
, 4);
95 ctx
->v2f32
= LLVMVectorType(ctx
->f32
, 2);
96 ctx
->v3f32
= LLVMVectorType(ctx
->f32
, 3);
97 ctx
->v4f32
= LLVMVectorType(ctx
->f32
, 4);
98 ctx
->v8i32
= LLVMVectorType(ctx
->i32
, 8);
99 ctx
->iN_wavemask
= LLVMIntTypeInContext(ctx
->context
, ctx
->wave_size
);
100 ctx
->iN_ballotmask
= LLVMIntTypeInContext(ctx
->context
, ballot_mask_bits
);
102 ctx
->i8_0
= LLVMConstInt(ctx
->i8
, 0, false);
103 ctx
->i8_1
= LLVMConstInt(ctx
->i8
, 1, false);
104 ctx
->i16_0
= LLVMConstInt(ctx
->i16
, 0, false);
105 ctx
->i16_1
= LLVMConstInt(ctx
->i16
, 1, false);
106 ctx
->i32_0
= LLVMConstInt(ctx
->i32
, 0, false);
107 ctx
->i32_1
= LLVMConstInt(ctx
->i32
, 1, false);
108 ctx
->i64_0
= LLVMConstInt(ctx
->i64
, 0, false);
109 ctx
->i64_1
= LLVMConstInt(ctx
->i64
, 1, false);
110 ctx
->f16_0
= LLVMConstReal(ctx
->f16
, 0.0);
111 ctx
->f16_1
= LLVMConstReal(ctx
->f16
, 1.0);
112 ctx
->f32_0
= LLVMConstReal(ctx
->f32
, 0.0);
113 ctx
->f32_1
= LLVMConstReal(ctx
->f32
, 1.0);
114 ctx
->f64_0
= LLVMConstReal(ctx
->f64
, 0.0);
115 ctx
->f64_1
= LLVMConstReal(ctx
->f64
, 1.0);
117 ctx
->i1false
= LLVMConstInt(ctx
->i1
, 0, false);
118 ctx
->i1true
= LLVMConstInt(ctx
->i1
, 1, false);
120 ctx
->range_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
123 ctx
->invariant_load_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
124 "invariant.load", 14);
126 ctx
->fpmath_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
, "fpmath", 6);
128 args
[0] = LLVMConstReal(ctx
->f32
, 2.5);
129 ctx
->fpmath_md_2p5_ulp
= LLVMMDNodeInContext(ctx
->context
, args
, 1);
131 ctx
->uniform_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
132 "amdgpu.uniform", 14);
134 ctx
->empty_md
= LLVMMDNodeInContext(ctx
->context
, NULL
, 0);
135 ctx
->flow
= calloc(1, sizeof(*ctx
->flow
));
139 ac_llvm_context_dispose(struct ac_llvm_context
*ctx
)
141 free(ctx
->flow
->stack
);
147 ac_get_llvm_num_components(LLVMValueRef value
)
149 LLVMTypeRef type
= LLVMTypeOf(value
);
150 unsigned num_components
= LLVMGetTypeKind(type
) == LLVMVectorTypeKind
151 ? LLVMGetVectorSize(type
)
153 return num_components
;
157 ac_llvm_extract_elem(struct ac_llvm_context
*ac
,
161 if (LLVMGetTypeKind(LLVMTypeOf(value
)) != LLVMVectorTypeKind
) {
166 return LLVMBuildExtractElement(ac
->builder
, value
,
167 LLVMConstInt(ac
->i32
, index
, false), "");
171 ac_get_elem_bits(struct ac_llvm_context
*ctx
, LLVMTypeRef type
)
173 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
)
174 type
= LLVMGetElementType(type
);
176 if (LLVMGetTypeKind(type
) == LLVMIntegerTypeKind
)
177 return LLVMGetIntTypeWidth(type
);
179 if (type
== ctx
->f16
)
181 if (type
== ctx
->f32
)
183 if (type
== ctx
->f64
)
186 unreachable("Unhandled type kind in get_elem_bits");
190 ac_get_type_size(LLVMTypeRef type
)
192 LLVMTypeKind kind
= LLVMGetTypeKind(type
);
195 case LLVMIntegerTypeKind
:
196 return LLVMGetIntTypeWidth(type
) / 8;
197 case LLVMHalfTypeKind
:
199 case LLVMFloatTypeKind
:
201 case LLVMDoubleTypeKind
:
203 case LLVMPointerTypeKind
:
204 if (LLVMGetPointerAddressSpace(type
) == AC_ADDR_SPACE_CONST_32BIT
)
207 case LLVMVectorTypeKind
:
208 return LLVMGetVectorSize(type
) *
209 ac_get_type_size(LLVMGetElementType(type
));
210 case LLVMArrayTypeKind
:
211 return LLVMGetArrayLength(type
) *
212 ac_get_type_size(LLVMGetElementType(type
));
219 static LLVMTypeRef
to_integer_type_scalar(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
223 else if (t
== ctx
->f16
|| t
== ctx
->i16
)
225 else if (t
== ctx
->f32
|| t
== ctx
->i32
)
227 else if (t
== ctx
->f64
|| t
== ctx
->i64
)
230 unreachable("Unhandled integer size");
234 ac_to_integer_type(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
236 if (LLVMGetTypeKind(t
) == LLVMVectorTypeKind
) {
237 LLVMTypeRef elem_type
= LLVMGetElementType(t
);
238 return LLVMVectorType(to_integer_type_scalar(ctx
, elem_type
),
239 LLVMGetVectorSize(t
));
241 if (LLVMGetTypeKind(t
) == LLVMPointerTypeKind
) {
242 switch (LLVMGetPointerAddressSpace(t
)) {
243 case AC_ADDR_SPACE_GLOBAL
:
245 case AC_ADDR_SPACE_LDS
:
248 unreachable("unhandled address space");
251 return to_integer_type_scalar(ctx
, t
);
255 ac_to_integer(struct ac_llvm_context
*ctx
, LLVMValueRef v
)
257 LLVMTypeRef type
= LLVMTypeOf(v
);
258 if (LLVMGetTypeKind(type
) == LLVMPointerTypeKind
) {
259 return LLVMBuildPtrToInt(ctx
->builder
, v
, ac_to_integer_type(ctx
, type
), "");
261 return LLVMBuildBitCast(ctx
->builder
, v
, ac_to_integer_type(ctx
, type
), "");
265 ac_to_integer_or_pointer(struct ac_llvm_context
*ctx
, LLVMValueRef v
)
267 LLVMTypeRef type
= LLVMTypeOf(v
);
268 if (LLVMGetTypeKind(type
) == LLVMPointerTypeKind
)
270 return ac_to_integer(ctx
, v
);
273 static LLVMTypeRef
to_float_type_scalar(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
277 else if (t
== ctx
->i16
|| t
== ctx
->f16
)
279 else if (t
== ctx
->i32
|| t
== ctx
->f32
)
281 else if (t
== ctx
->i64
|| t
== ctx
->f64
)
284 unreachable("Unhandled float size");
288 ac_to_float_type(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
290 if (LLVMGetTypeKind(t
) == LLVMVectorTypeKind
) {
291 LLVMTypeRef elem_type
= LLVMGetElementType(t
);
292 return LLVMVectorType(to_float_type_scalar(ctx
, elem_type
),
293 LLVMGetVectorSize(t
));
295 return to_float_type_scalar(ctx
, t
);
299 ac_to_float(struct ac_llvm_context
*ctx
, LLVMValueRef v
)
301 LLVMTypeRef type
= LLVMTypeOf(v
);
302 return LLVMBuildBitCast(ctx
->builder
, v
, ac_to_float_type(ctx
, type
), "");
307 ac_build_intrinsic(struct ac_llvm_context
*ctx
, const char *name
,
308 LLVMTypeRef return_type
, LLVMValueRef
*params
,
309 unsigned param_count
, unsigned attrib_mask
)
311 LLVMValueRef function
, call
;
312 bool set_callsite_attrs
= !(attrib_mask
& AC_FUNC_ATTR_LEGACY
);
314 function
= LLVMGetNamedFunction(ctx
->module
, name
);
316 LLVMTypeRef param_types
[32], function_type
;
319 assert(param_count
<= 32);
321 for (i
= 0; i
< param_count
; ++i
) {
323 param_types
[i
] = LLVMTypeOf(params
[i
]);
326 LLVMFunctionType(return_type
, param_types
, param_count
, 0);
327 function
= LLVMAddFunction(ctx
->module
, name
, function_type
);
329 LLVMSetFunctionCallConv(function
, LLVMCCallConv
);
330 LLVMSetLinkage(function
, LLVMExternalLinkage
);
332 if (!set_callsite_attrs
)
333 ac_add_func_attributes(ctx
->context
, function
, attrib_mask
);
336 call
= LLVMBuildCall(ctx
->builder
, function
, params
, param_count
, "");
337 if (set_callsite_attrs
)
338 ac_add_func_attributes(ctx
->context
, call
, attrib_mask
);
343 * Given the i32 or vNi32 \p type, generate the textual name (e.g. for use with
346 void ac_build_type_name_for_intr(LLVMTypeRef type
, char *buf
, unsigned bufsize
)
348 LLVMTypeRef elem_type
= type
;
350 assert(bufsize
>= 8);
352 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
) {
353 int ret
= snprintf(buf
, bufsize
, "v%u",
354 LLVMGetVectorSize(type
));
356 char *type_name
= LLVMPrintTypeToString(type
);
357 fprintf(stderr
, "Error building type name for: %s\n",
359 LLVMDisposeMessage(type_name
);
362 elem_type
= LLVMGetElementType(type
);
366 switch (LLVMGetTypeKind(elem_type
)) {
368 case LLVMIntegerTypeKind
:
369 snprintf(buf
, bufsize
, "i%d", LLVMGetIntTypeWidth(elem_type
));
371 case LLVMHalfTypeKind
:
372 snprintf(buf
, bufsize
, "f16");
374 case LLVMFloatTypeKind
:
375 snprintf(buf
, bufsize
, "f32");
377 case LLVMDoubleTypeKind
:
378 snprintf(buf
, bufsize
, "f64");
384 * Helper function that builds an LLVM IR PHI node and immediately adds
388 ac_build_phi(struct ac_llvm_context
*ctx
, LLVMTypeRef type
,
389 unsigned count_incoming
, LLVMValueRef
*values
,
390 LLVMBasicBlockRef
*blocks
)
392 LLVMValueRef phi
= LLVMBuildPhi(ctx
->builder
, type
, "");
393 LLVMAddIncoming(phi
, values
, blocks
, count_incoming
);
397 void ac_build_s_barrier(struct ac_llvm_context
*ctx
)
399 ac_build_intrinsic(ctx
, "llvm.amdgcn.s.barrier", ctx
->voidt
, NULL
,
400 0, AC_FUNC_ATTR_CONVERGENT
);
403 /* Prevent optimizations (at least of memory accesses) across the current
404 * point in the program by emitting empty inline assembly that is marked as
405 * having side effects.
407 * Optionally, a value can be passed through the inline assembly to prevent
408 * LLVM from hoisting calls to ReadNone functions.
411 ac_build_optimization_barrier(struct ac_llvm_context
*ctx
,
414 static int counter
= 0;
416 LLVMBuilderRef builder
= ctx
->builder
;
419 snprintf(code
, sizeof(code
), "; %d", p_atomic_inc_return(&counter
));
422 LLVMTypeRef ftype
= LLVMFunctionType(ctx
->voidt
, NULL
, 0, false);
423 LLVMValueRef inlineasm
= LLVMConstInlineAsm(ftype
, code
, "", true, false);
424 LLVMBuildCall(builder
, inlineasm
, NULL
, 0, "");
426 LLVMTypeRef ftype
= LLVMFunctionType(ctx
->i32
, &ctx
->i32
, 1, false);
427 LLVMValueRef inlineasm
= LLVMConstInlineAsm(ftype
, code
, "=v,0", true, false);
428 LLVMValueRef vgpr
= *pvgpr
;
429 LLVMTypeRef vgpr_type
= LLVMTypeOf(vgpr
);
430 unsigned vgpr_size
= ac_get_type_size(vgpr_type
);
433 assert(vgpr_size
% 4 == 0);
435 vgpr
= LLVMBuildBitCast(builder
, vgpr
, LLVMVectorType(ctx
->i32
, vgpr_size
/ 4), "");
436 vgpr0
= LLVMBuildExtractElement(builder
, vgpr
, ctx
->i32_0
, "");
437 vgpr0
= LLVMBuildCall(builder
, inlineasm
, &vgpr0
, 1, "");
438 vgpr
= LLVMBuildInsertElement(builder
, vgpr
, vgpr0
, ctx
->i32_0
, "");
439 vgpr
= LLVMBuildBitCast(builder
, vgpr
, vgpr_type
, "");
446 ac_build_shader_clock(struct ac_llvm_context
*ctx
)
448 const char *intr
= LLVM_VERSION_MAJOR
>= 9 && ctx
->chip_class
>= GFX8
?
449 "llvm.amdgcn.s.memrealtime" : "llvm.readcyclecounter";
450 LLVMValueRef tmp
= ac_build_intrinsic(ctx
, intr
, ctx
->i64
, NULL
, 0, 0);
451 return LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->v2i32
, "");
455 ac_build_ballot(struct ac_llvm_context
*ctx
,
460 if (LLVM_VERSION_MAJOR
>= 9) {
461 if (ctx
->wave_size
== 64)
462 name
= "llvm.amdgcn.icmp.i64.i32";
464 name
= "llvm.amdgcn.icmp.i32.i32";
466 name
= "llvm.amdgcn.icmp.i32";
468 LLVMValueRef args
[3] = {
471 LLVMConstInt(ctx
->i32
, LLVMIntNE
, 0)
474 /* We currently have no other way to prevent LLVM from lifting the icmp
475 * calls to a dominating basic block.
477 ac_build_optimization_barrier(ctx
, &args
[0]);
479 args
[0] = ac_to_integer(ctx
, args
[0]);
481 return ac_build_intrinsic(ctx
, name
, ctx
->iN_wavemask
, args
, 3,
482 AC_FUNC_ATTR_NOUNWIND
|
483 AC_FUNC_ATTR_READNONE
|
484 AC_FUNC_ATTR_CONVERGENT
);
487 LLVMValueRef
ac_get_i1_sgpr_mask(struct ac_llvm_context
*ctx
,
490 const char *name
= LLVM_VERSION_MAJOR
>= 9 ? "llvm.amdgcn.icmp.i64.i1" : "llvm.amdgcn.icmp.i1";
491 LLVMValueRef args
[3] = {
494 LLVMConstInt(ctx
->i32
, LLVMIntNE
, 0),
497 return ac_build_intrinsic(ctx
, name
, ctx
->i64
, args
, 3,
498 AC_FUNC_ATTR_NOUNWIND
|
499 AC_FUNC_ATTR_READNONE
|
500 AC_FUNC_ATTR_CONVERGENT
);
504 ac_build_vote_all(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
506 LLVMValueRef active_set
= ac_build_ballot(ctx
, ctx
->i32_1
);
507 LLVMValueRef vote_set
= ac_build_ballot(ctx
, value
);
508 return LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, vote_set
, active_set
, "");
512 ac_build_vote_any(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
514 LLVMValueRef vote_set
= ac_build_ballot(ctx
, value
);
515 return LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, vote_set
,
516 LLVMConstInt(ctx
->iN_wavemask
, 0, 0), "");
520 ac_build_vote_eq(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
522 LLVMValueRef active_set
= ac_build_ballot(ctx
, ctx
->i32_1
);
523 LLVMValueRef vote_set
= ac_build_ballot(ctx
, value
);
525 LLVMValueRef all
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
526 vote_set
, active_set
, "");
527 LLVMValueRef none
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
529 LLVMConstInt(ctx
->iN_wavemask
, 0, 0), "");
530 return LLVMBuildOr(ctx
->builder
, all
, none
, "");
534 ac_build_varying_gather_values(struct ac_llvm_context
*ctx
, LLVMValueRef
*values
,
535 unsigned value_count
, unsigned component
)
537 LLVMValueRef vec
= NULL
;
539 if (value_count
== 1) {
540 return values
[component
];
541 } else if (!value_count
)
542 unreachable("value_count is 0");
544 for (unsigned i
= component
; i
< value_count
+ component
; i
++) {
545 LLVMValueRef value
= values
[i
];
548 vec
= LLVMGetUndef( LLVMVectorType(LLVMTypeOf(value
), value_count
));
549 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, i
- component
, false);
550 vec
= LLVMBuildInsertElement(ctx
->builder
, vec
, value
, index
, "");
556 ac_build_gather_values_extended(struct ac_llvm_context
*ctx
,
557 LLVMValueRef
*values
,
558 unsigned value_count
,
559 unsigned value_stride
,
563 LLVMBuilderRef builder
= ctx
->builder
;
564 LLVMValueRef vec
= NULL
;
567 if (value_count
== 1 && !always_vector
) {
569 return LLVMBuildLoad(builder
, values
[0], "");
571 } else if (!value_count
)
572 unreachable("value_count is 0");
574 for (i
= 0; i
< value_count
; i
++) {
575 LLVMValueRef value
= values
[i
* value_stride
];
577 value
= LLVMBuildLoad(builder
, value
, "");
580 vec
= LLVMGetUndef( LLVMVectorType(LLVMTypeOf(value
), value_count
));
581 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, i
, false);
582 vec
= LLVMBuildInsertElement(builder
, vec
, value
, index
, "");
588 ac_build_gather_values(struct ac_llvm_context
*ctx
,
589 LLVMValueRef
*values
,
590 unsigned value_count
)
592 return ac_build_gather_values_extended(ctx
, values
, value_count
, 1, false, false);
595 /* Expand a scalar or vector to <dst_channels x type> by filling the remaining
596 * channels with undef. Extract at most src_channels components from the input.
599 ac_build_expand(struct ac_llvm_context
*ctx
,
601 unsigned src_channels
,
602 unsigned dst_channels
)
604 LLVMTypeRef elemtype
;
605 LLVMValueRef chan
[dst_channels
];
607 if (LLVMGetTypeKind(LLVMTypeOf(value
)) == LLVMVectorTypeKind
) {
608 unsigned vec_size
= LLVMGetVectorSize(LLVMTypeOf(value
));
610 if (src_channels
== dst_channels
&& vec_size
== dst_channels
)
613 src_channels
= MIN2(src_channels
, vec_size
);
615 for (unsigned i
= 0; i
< src_channels
; i
++)
616 chan
[i
] = ac_llvm_extract_elem(ctx
, value
, i
);
618 elemtype
= LLVMGetElementType(LLVMTypeOf(value
));
621 assert(src_channels
== 1);
624 elemtype
= LLVMTypeOf(value
);
627 for (unsigned i
= src_channels
; i
< dst_channels
; i
++)
628 chan
[i
] = LLVMGetUndef(elemtype
);
630 return ac_build_gather_values(ctx
, chan
, dst_channels
);
633 /* Extract components [start, start + channels) from a vector.
636 ac_extract_components(struct ac_llvm_context
*ctx
,
641 LLVMValueRef chan
[channels
];
643 for (unsigned i
= 0; i
< channels
; i
++)
644 chan
[i
] = ac_llvm_extract_elem(ctx
, value
, i
+ start
);
646 return ac_build_gather_values(ctx
, chan
, channels
);
649 /* Expand a scalar or vector to <4 x type> by filling the remaining channels
650 * with undef. Extract at most num_channels components from the input.
652 LLVMValueRef
ac_build_expand_to_vec4(struct ac_llvm_context
*ctx
,
654 unsigned num_channels
)
656 return ac_build_expand(ctx
, value
, num_channels
, 4);
659 LLVMValueRef
ac_build_round(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
661 unsigned type_size
= ac_get_type_size(LLVMTypeOf(value
));
665 name
= "llvm.rint.f16";
666 else if (type_size
== 4)
667 name
= "llvm.rint.f32";
669 name
= "llvm.rint.f64";
671 return ac_build_intrinsic(ctx
, name
, LLVMTypeOf(value
), &value
, 1,
672 AC_FUNC_ATTR_READNONE
);
676 ac_build_fdiv(struct ac_llvm_context
*ctx
,
680 /* If we do (num / den), LLVM >= 7.0 does:
681 * return num * v_rcp_f32(den * (fabs(den) > 0x1.0p+96f ? 0x1.0p-32f : 1.0f));
683 * If we do (num * (1 / den)), LLVM does:
684 * return num * v_rcp_f32(den);
686 LLVMValueRef one
= LLVMConstReal(LLVMTypeOf(num
), 1.0);
687 LLVMValueRef rcp
= LLVMBuildFDiv(ctx
->builder
, one
, den
, "");
688 LLVMValueRef ret
= LLVMBuildFMul(ctx
->builder
, num
, rcp
, "");
690 /* Use v_rcp_f32 instead of precise division. */
691 if (!LLVMIsConstant(ret
))
692 LLVMSetMetadata(ret
, ctx
->fpmath_md_kind
, ctx
->fpmath_md_2p5_ulp
);
696 /* See fast_idiv_by_const.h. */
697 /* Set: increment = util_fast_udiv_info::increment ? multiplier : 0; */
698 LLVMValueRef
ac_build_fast_udiv(struct ac_llvm_context
*ctx
,
700 LLVMValueRef multiplier
,
701 LLVMValueRef pre_shift
,
702 LLVMValueRef post_shift
,
703 LLVMValueRef increment
)
705 LLVMBuilderRef builder
= ctx
->builder
;
707 num
= LLVMBuildLShr(builder
, num
, pre_shift
, "");
708 num
= LLVMBuildMul(builder
,
709 LLVMBuildZExt(builder
, num
, ctx
->i64
, ""),
710 LLVMBuildZExt(builder
, multiplier
, ctx
->i64
, ""), "");
711 num
= LLVMBuildAdd(builder
, num
,
712 LLVMBuildZExt(builder
, increment
, ctx
->i64
, ""), "");
713 num
= LLVMBuildLShr(builder
, num
, LLVMConstInt(ctx
->i64
, 32, 0), "");
714 num
= LLVMBuildTrunc(builder
, num
, ctx
->i32
, "");
715 return LLVMBuildLShr(builder
, num
, post_shift
, "");
718 /* See fast_idiv_by_const.h. */
719 /* If num != UINT_MAX, this more efficient version can be used. */
720 /* Set: increment = util_fast_udiv_info::increment; */
721 LLVMValueRef
ac_build_fast_udiv_nuw(struct ac_llvm_context
*ctx
,
723 LLVMValueRef multiplier
,
724 LLVMValueRef pre_shift
,
725 LLVMValueRef post_shift
,
726 LLVMValueRef increment
)
728 LLVMBuilderRef builder
= ctx
->builder
;
730 num
= LLVMBuildLShr(builder
, num
, pre_shift
, "");
731 num
= LLVMBuildNUWAdd(builder
, num
, increment
, "");
732 num
= LLVMBuildMul(builder
,
733 LLVMBuildZExt(builder
, num
, ctx
->i64
, ""),
734 LLVMBuildZExt(builder
, multiplier
, ctx
->i64
, ""), "");
735 num
= LLVMBuildLShr(builder
, num
, LLVMConstInt(ctx
->i64
, 32, 0), "");
736 num
= LLVMBuildTrunc(builder
, num
, ctx
->i32
, "");
737 return LLVMBuildLShr(builder
, num
, post_shift
, "");
740 /* See fast_idiv_by_const.h. */
741 /* Both operands must fit in 31 bits and the divisor must not be 1. */
742 LLVMValueRef
ac_build_fast_udiv_u31_d_not_one(struct ac_llvm_context
*ctx
,
744 LLVMValueRef multiplier
,
745 LLVMValueRef post_shift
)
747 LLVMBuilderRef builder
= ctx
->builder
;
749 num
= LLVMBuildMul(builder
,
750 LLVMBuildZExt(builder
, num
, ctx
->i64
, ""),
751 LLVMBuildZExt(builder
, multiplier
, ctx
->i64
, ""), "");
752 num
= LLVMBuildLShr(builder
, num
, LLVMConstInt(ctx
->i64
, 32, 0), "");
753 num
= LLVMBuildTrunc(builder
, num
, ctx
->i32
, "");
754 return LLVMBuildLShr(builder
, num
, post_shift
, "");
757 /* Coordinates for cube map selection. sc, tc, and ma are as in Table 8.27
758 * of the OpenGL 4.5 (Compatibility Profile) specification, except ma is
759 * already multiplied by two. id is the cube face number.
761 struct cube_selection_coords
{
768 build_cube_intrinsic(struct ac_llvm_context
*ctx
,
770 struct cube_selection_coords
*out
)
772 LLVMTypeRef f32
= ctx
->f32
;
774 out
->stc
[1] = ac_build_intrinsic(ctx
, "llvm.amdgcn.cubetc",
775 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
776 out
->stc
[0] = ac_build_intrinsic(ctx
, "llvm.amdgcn.cubesc",
777 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
778 out
->ma
= ac_build_intrinsic(ctx
, "llvm.amdgcn.cubema",
779 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
780 out
->id
= ac_build_intrinsic(ctx
, "llvm.amdgcn.cubeid",
781 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
785 * Build a manual selection sequence for cube face sc/tc coordinates and
786 * major axis vector (multiplied by 2 for consistency) for the given
787 * vec3 \p coords, for the face implied by \p selcoords.
789 * For the major axis, we always adjust the sign to be in the direction of
790 * selcoords.ma; i.e., a positive out_ma means that coords is pointed towards
791 * the selcoords major axis.
793 static void build_cube_select(struct ac_llvm_context
*ctx
,
794 const struct cube_selection_coords
*selcoords
,
795 const LLVMValueRef
*coords
,
796 LLVMValueRef
*out_st
,
797 LLVMValueRef
*out_ma
)
799 LLVMBuilderRef builder
= ctx
->builder
;
800 LLVMTypeRef f32
= LLVMTypeOf(coords
[0]);
801 LLVMValueRef is_ma_positive
;
803 LLVMValueRef is_ma_z
, is_not_ma_z
;
804 LLVMValueRef is_ma_y
;
805 LLVMValueRef is_ma_x
;
809 is_ma_positive
= LLVMBuildFCmp(builder
, LLVMRealUGE
,
810 selcoords
->ma
, LLVMConstReal(f32
, 0.0), "");
811 sgn_ma
= LLVMBuildSelect(builder
, is_ma_positive
,
812 LLVMConstReal(f32
, 1.0), LLVMConstReal(f32
, -1.0), "");
814 is_ma_z
= LLVMBuildFCmp(builder
, LLVMRealUGE
, selcoords
->id
, LLVMConstReal(f32
, 4.0), "");
815 is_not_ma_z
= LLVMBuildNot(builder
, is_ma_z
, "");
816 is_ma_y
= LLVMBuildAnd(builder
, is_not_ma_z
,
817 LLVMBuildFCmp(builder
, LLVMRealUGE
, selcoords
->id
, LLVMConstReal(f32
, 2.0), ""), "");
818 is_ma_x
= LLVMBuildAnd(builder
, is_not_ma_z
, LLVMBuildNot(builder
, is_ma_y
, ""), "");
821 tmp
= LLVMBuildSelect(builder
, is_ma_x
, coords
[2], coords
[0], "");
822 sgn
= LLVMBuildSelect(builder
, is_ma_y
, LLVMConstReal(f32
, 1.0),
823 LLVMBuildSelect(builder
, is_ma_z
, sgn_ma
,
824 LLVMBuildFNeg(builder
, sgn_ma
, ""), ""), "");
825 out_st
[0] = LLVMBuildFMul(builder
, tmp
, sgn
, "");
828 tmp
= LLVMBuildSelect(builder
, is_ma_y
, coords
[2], coords
[1], "");
829 sgn
= LLVMBuildSelect(builder
, is_ma_y
, sgn_ma
,
830 LLVMConstReal(f32
, -1.0), "");
831 out_st
[1] = LLVMBuildFMul(builder
, tmp
, sgn
, "");
834 tmp
= LLVMBuildSelect(builder
, is_ma_z
, coords
[2],
835 LLVMBuildSelect(builder
, is_ma_y
, coords
[1], coords
[0], ""), "");
836 tmp
= ac_build_intrinsic(ctx
, "llvm.fabs.f32",
837 ctx
->f32
, &tmp
, 1, AC_FUNC_ATTR_READNONE
);
838 *out_ma
= LLVMBuildFMul(builder
, tmp
, LLVMConstReal(f32
, 2.0), "");
842 ac_prepare_cube_coords(struct ac_llvm_context
*ctx
,
843 bool is_deriv
, bool is_array
, bool is_lod
,
844 LLVMValueRef
*coords_arg
,
845 LLVMValueRef
*derivs_arg
)
848 LLVMBuilderRef builder
= ctx
->builder
;
849 struct cube_selection_coords selcoords
;
850 LLVMValueRef coords
[3];
853 if (is_array
&& !is_lod
) {
854 LLVMValueRef tmp
= ac_build_round(ctx
, coords_arg
[3]);
856 /* Section 8.9 (Texture Functions) of the GLSL 4.50 spec says:
858 * "For Array forms, the array layer used will be
860 * max(0, min(d−1, floor(layer+0.5)))
862 * where d is the depth of the texture array and layer
863 * comes from the component indicated in the tables below.
864 * Workaroudn for an issue where the layer is taken from a
865 * helper invocation which happens to fall on a different
866 * layer due to extrapolation."
868 * GFX8 and earlier attempt to implement this in hardware by
869 * clamping the value of coords[2] = (8 * layer) + face.
870 * Unfortunately, this means that the we end up with the wrong
871 * face when clamping occurs.
873 * Clamp the layer earlier to work around the issue.
875 if (ctx
->chip_class
<= GFX8
) {
877 ge0
= LLVMBuildFCmp(builder
, LLVMRealOGE
, tmp
, ctx
->f32_0
, "");
878 tmp
= LLVMBuildSelect(builder
, ge0
, tmp
, ctx
->f32_0
, "");
884 build_cube_intrinsic(ctx
, coords_arg
, &selcoords
);
886 invma
= ac_build_intrinsic(ctx
, "llvm.fabs.f32",
887 ctx
->f32
, &selcoords
.ma
, 1, AC_FUNC_ATTR_READNONE
);
888 invma
= ac_build_fdiv(ctx
, LLVMConstReal(ctx
->f32
, 1.0), invma
);
890 for (int i
= 0; i
< 2; ++i
)
891 coords
[i
] = LLVMBuildFMul(builder
, selcoords
.stc
[i
], invma
, "");
893 coords
[2] = selcoords
.id
;
895 if (is_deriv
&& derivs_arg
) {
896 LLVMValueRef derivs
[4];
899 /* Convert cube derivatives to 2D derivatives. */
900 for (axis
= 0; axis
< 2; axis
++) {
901 LLVMValueRef deriv_st
[2];
902 LLVMValueRef deriv_ma
;
904 /* Transform the derivative alongside the texture
905 * coordinate. Mathematically, the correct formula is
906 * as follows. Assume we're projecting onto the +Z face
907 * and denote by dx/dh the derivative of the (original)
908 * X texture coordinate with respect to horizontal
909 * window coordinates. The projection onto the +Z face
914 * Then df/dh = df/dx * dx/dh + df/dz * dz/dh
915 * = 1/z * dx/dh - x/z * 1/z * dz/dh.
917 * This motivatives the implementation below.
919 * Whether this actually gives the expected results for
920 * apps that might feed in derivatives obtained via
921 * finite differences is anyone's guess. The OpenGL spec
922 * seems awfully quiet about how textureGrad for cube
923 * maps should be handled.
925 build_cube_select(ctx
, &selcoords
, &derivs_arg
[axis
* 3],
926 deriv_st
, &deriv_ma
);
928 deriv_ma
= LLVMBuildFMul(builder
, deriv_ma
, invma
, "");
930 for (int i
= 0; i
< 2; ++i
)
931 derivs
[axis
* 2 + i
] =
932 LLVMBuildFSub(builder
,
933 LLVMBuildFMul(builder
, deriv_st
[i
], invma
, ""),
934 LLVMBuildFMul(builder
, deriv_ma
, coords
[i
], ""), "");
937 memcpy(derivs_arg
, derivs
, sizeof(derivs
));
940 /* Shift the texture coordinate. This must be applied after the
941 * derivative calculation.
943 for (int i
= 0; i
< 2; ++i
)
944 coords
[i
] = LLVMBuildFAdd(builder
, coords
[i
], LLVMConstReal(ctx
->f32
, 1.5), "");
947 /* for cube arrays coord.z = coord.w(array_index) * 8 + face */
948 /* coords_arg.w component - array_index for cube arrays */
949 coords
[2] = ac_build_fmad(ctx
, coords_arg
[3], LLVMConstReal(ctx
->f32
, 8.0), coords
[2]);
952 memcpy(coords_arg
, coords
, sizeof(coords
));
957 ac_build_fs_interp(struct ac_llvm_context
*ctx
,
958 LLVMValueRef llvm_chan
,
959 LLVMValueRef attr_number
,
964 LLVMValueRef args
[5];
969 args
[2] = attr_number
;
972 p1
= ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p1",
973 ctx
->f32
, args
, 4, AC_FUNC_ATTR_READNONE
);
978 args
[3] = attr_number
;
981 return ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p2",
982 ctx
->f32
, args
, 5, AC_FUNC_ATTR_READNONE
);
986 ac_build_fs_interp_f16(struct ac_llvm_context
*ctx
,
987 LLVMValueRef llvm_chan
,
988 LLVMValueRef attr_number
,
993 LLVMValueRef args
[6];
998 args
[2] = attr_number
;
999 args
[3] = ctx
->i1false
;
1002 p1
= ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p1.f16",
1003 ctx
->f32
, args
, 5, AC_FUNC_ATTR_READNONE
);
1007 args
[2] = llvm_chan
;
1008 args
[3] = attr_number
;
1009 args
[4] = ctx
->i1false
;
1012 return ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p2.f16",
1013 ctx
->f16
, args
, 6, AC_FUNC_ATTR_READNONE
);
1017 ac_build_fs_interp_mov(struct ac_llvm_context
*ctx
,
1018 LLVMValueRef parameter
,
1019 LLVMValueRef llvm_chan
,
1020 LLVMValueRef attr_number
,
1021 LLVMValueRef params
)
1023 LLVMValueRef args
[4];
1025 args
[0] = parameter
;
1026 args
[1] = llvm_chan
;
1027 args
[2] = attr_number
;
1030 return ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.mov",
1031 ctx
->f32
, args
, 4, AC_FUNC_ATTR_READNONE
);
1035 ac_build_gep_ptr(struct ac_llvm_context
*ctx
,
1036 LLVMValueRef base_ptr
,
1039 return LLVMBuildGEP(ctx
->builder
, base_ptr
, &index
, 1, "");
1043 ac_build_gep0(struct ac_llvm_context
*ctx
,
1044 LLVMValueRef base_ptr
,
1047 LLVMValueRef indices
[2] = {
1051 return LLVMBuildGEP(ctx
->builder
, base_ptr
, indices
, 2, "");
1054 LLVMValueRef
ac_build_pointer_add(struct ac_llvm_context
*ctx
, LLVMValueRef ptr
,
1057 return LLVMBuildPointerCast(ctx
->builder
,
1058 LLVMBuildGEP(ctx
->builder
, ptr
, &index
, 1, ""),
1059 LLVMTypeOf(ptr
), "");
1063 ac_build_indexed_store(struct ac_llvm_context
*ctx
,
1064 LLVMValueRef base_ptr
, LLVMValueRef index
,
1067 LLVMBuildStore(ctx
->builder
, value
,
1068 ac_build_gep0(ctx
, base_ptr
, index
));
1072 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad.
1073 * It's equivalent to doing a load from &base_ptr[index].
1075 * \param base_ptr Where the array starts.
1076 * \param index The element index into the array.
1077 * \param uniform Whether the base_ptr and index can be assumed to be
1078 * dynamically uniform (i.e. load to an SGPR)
1079 * \param invariant Whether the load is invariant (no other opcodes affect it)
1080 * \param no_unsigned_wraparound
1081 * For all possible re-associations and re-distributions of an expression
1082 * "base_ptr + index * elemsize" into "addr + offset" (excluding GEPs
1083 * without inbounds in base_ptr), this parameter is true if "addr + offset"
1084 * does not result in an unsigned integer wraparound. This is used for
1085 * optimal code generation of 32-bit pointer arithmetic.
1087 * For example, a 32-bit immediate offset that causes a 32-bit unsigned
1088 * integer wraparound can't be an imm offset in s_load_dword, because
1089 * the instruction performs "addr + offset" in 64 bits.
1091 * Expected usage for bindless textures by chaining GEPs:
1092 * // possible unsigned wraparound, don't use InBounds:
1093 * ptr1 = LLVMBuildGEP(base_ptr, index);
1094 * image = load(ptr1); // becomes "s_load ptr1, 0"
1096 * ptr2 = LLVMBuildInBoundsGEP(ptr1, 32 / elemsize);
1097 * sampler = load(ptr2); // becomes "s_load ptr1, 32" thanks to InBounds
1100 ac_build_load_custom(struct ac_llvm_context
*ctx
, LLVMValueRef base_ptr
,
1101 LLVMValueRef index
, bool uniform
, bool invariant
,
1102 bool no_unsigned_wraparound
)
1104 LLVMValueRef pointer
, result
;
1106 if (no_unsigned_wraparound
&&
1107 LLVMGetPointerAddressSpace(LLVMTypeOf(base_ptr
)) == AC_ADDR_SPACE_CONST_32BIT
)
1108 pointer
= LLVMBuildInBoundsGEP(ctx
->builder
, base_ptr
, &index
, 1, "");
1110 pointer
= LLVMBuildGEP(ctx
->builder
, base_ptr
, &index
, 1, "");
1113 LLVMSetMetadata(pointer
, ctx
->uniform_md_kind
, ctx
->empty_md
);
1114 result
= LLVMBuildLoad(ctx
->builder
, pointer
, "");
1116 LLVMSetMetadata(result
, ctx
->invariant_load_md_kind
, ctx
->empty_md
);
1120 LLVMValueRef
ac_build_load(struct ac_llvm_context
*ctx
, LLVMValueRef base_ptr
,
1123 return ac_build_load_custom(ctx
, base_ptr
, index
, false, false, false);
1126 LLVMValueRef
ac_build_load_invariant(struct ac_llvm_context
*ctx
,
1127 LLVMValueRef base_ptr
, LLVMValueRef index
)
1129 return ac_build_load_custom(ctx
, base_ptr
, index
, false, true, false);
1132 /* This assumes that there is no unsigned integer wraparound during the address
1133 * computation, excluding all GEPs within base_ptr. */
1134 LLVMValueRef
ac_build_load_to_sgpr(struct ac_llvm_context
*ctx
,
1135 LLVMValueRef base_ptr
, LLVMValueRef index
)
1137 return ac_build_load_custom(ctx
, base_ptr
, index
, true, true, true);
1140 /* See ac_build_load_custom() documentation. */
1141 LLVMValueRef
ac_build_load_to_sgpr_uint_wraparound(struct ac_llvm_context
*ctx
,
1142 LLVMValueRef base_ptr
, LLVMValueRef index
)
1144 return ac_build_load_custom(ctx
, base_ptr
, index
, true, true, false);
1147 static unsigned get_load_cache_policy(struct ac_llvm_context
*ctx
,
1148 unsigned cache_policy
)
1150 return cache_policy
|
1151 (ctx
->chip_class
>= GFX10
&& cache_policy
& ac_glc
? ac_dlc
: 0);
1155 ac_build_buffer_store_common(struct ac_llvm_context
*ctx
,
1158 LLVMValueRef vindex
,
1159 LLVMValueRef voffset
,
1160 LLVMValueRef soffset
,
1161 unsigned num_channels
,
1162 LLVMTypeRef return_channel_type
,
1163 unsigned cache_policy
,
1167 LLVMValueRef args
[6];
1170 args
[idx
++] = LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, "");
1172 args
[idx
++] = vindex
? vindex
: ctx
->i32_0
;
1173 args
[idx
++] = voffset
? voffset
: ctx
->i32_0
;
1174 args
[idx
++] = soffset
? soffset
: ctx
->i32_0
;
1175 args
[idx
++] = LLVMConstInt(ctx
->i32
, cache_policy
, 0);
1176 unsigned func
= !ac_has_vec3_support(ctx
->chip_class
, use_format
) && num_channels
== 3 ? 4 : num_channels
;
1177 const char *indexing_kind
= structurized
? "struct" : "raw";
1178 char name
[256], type_name
[8];
1180 LLVMTypeRef type
= func
> 1 ? LLVMVectorType(return_channel_type
, func
) : return_channel_type
;
1181 ac_build_type_name_for_intr(type
, type_name
, sizeof(type_name
));
1184 snprintf(name
, sizeof(name
), "llvm.amdgcn.%s.buffer.store.format.%s",
1185 indexing_kind
, type_name
);
1187 snprintf(name
, sizeof(name
), "llvm.amdgcn.%s.buffer.store.%s",
1188 indexing_kind
, type_name
);
1191 ac_build_intrinsic(ctx
, name
, ctx
->voidt
, args
, idx
,
1192 AC_FUNC_ATTR_INACCESSIBLE_MEM_ONLY
);
1196 ac_build_buffer_store_format(struct ac_llvm_context
*ctx
,
1199 LLVMValueRef vindex
,
1200 LLVMValueRef voffset
,
1201 unsigned num_channels
,
1202 unsigned cache_policy
)
1204 ac_build_buffer_store_common(ctx
, rsrc
, data
, vindex
,
1205 voffset
, NULL
, num_channels
,
1206 ctx
->f32
, cache_policy
,
1210 /* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
1211 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
1212 * or v4i32 (num_channels=3,4).
1215 ac_build_buffer_store_dword(struct ac_llvm_context
*ctx
,
1218 unsigned num_channels
,
1219 LLVMValueRef voffset
,
1220 LLVMValueRef soffset
,
1221 unsigned inst_offset
,
1222 unsigned cache_policy
,
1223 bool swizzle_enable_hint
)
1225 /* Split 3 channel stores, because only LLVM 9+ support 3-channel
1227 if (num_channels
== 3 && !ac_has_vec3_support(ctx
->chip_class
, false)) {
1228 LLVMValueRef v
[3], v01
;
1230 for (int i
= 0; i
< 3; i
++) {
1231 v
[i
] = LLVMBuildExtractElement(ctx
->builder
, vdata
,
1232 LLVMConstInt(ctx
->i32
, i
, 0), "");
1234 v01
= ac_build_gather_values(ctx
, v
, 2);
1236 ac_build_buffer_store_dword(ctx
, rsrc
, v01
, 2, voffset
,
1237 soffset
, inst_offset
, cache_policy
,
1238 swizzle_enable_hint
);
1239 ac_build_buffer_store_dword(ctx
, rsrc
, v
[2], 1, voffset
,
1240 soffset
, inst_offset
+ 8,
1242 swizzle_enable_hint
);
1246 /* SWIZZLE_ENABLE requires that soffset isn't folded into voffset
1247 * (voffset is swizzled, but soffset isn't swizzled).
1248 * llvm.amdgcn.buffer.store doesn't have a separate soffset parameter.
1250 if (!swizzle_enable_hint
) {
1251 LLVMValueRef offset
= soffset
;
1254 offset
= LLVMBuildAdd(ctx
->builder
, offset
,
1255 LLVMConstInt(ctx
->i32
, inst_offset
, 0), "");
1257 ac_build_buffer_store_common(ctx
, rsrc
, ac_to_float(ctx
, vdata
),
1258 ctx
->i32_0
, voffset
, offset
,
1259 num_channels
, ctx
->f32
,
1260 cache_policy
, false, false);
1264 static const unsigned dfmts
[] = {
1265 V_008F0C_BUF_DATA_FORMAT_32
,
1266 V_008F0C_BUF_DATA_FORMAT_32_32
,
1267 V_008F0C_BUF_DATA_FORMAT_32_32_32
,
1268 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
1270 unsigned dfmt
= dfmts
[num_channels
- 1];
1271 unsigned nfmt
= V_008F0C_BUF_NUM_FORMAT_UINT
;
1272 LLVMValueRef immoffset
= LLVMConstInt(ctx
->i32
, inst_offset
, 0);
1274 ac_build_raw_tbuffer_store(ctx
, rsrc
, vdata
, voffset
, soffset
,
1275 immoffset
, num_channels
, dfmt
, nfmt
, cache_policy
);
1279 ac_build_buffer_load_common(struct ac_llvm_context
*ctx
,
1281 LLVMValueRef vindex
,
1282 LLVMValueRef voffset
,
1283 LLVMValueRef soffset
,
1284 unsigned num_channels
,
1285 LLVMTypeRef channel_type
,
1286 unsigned cache_policy
,
1291 LLVMValueRef args
[5];
1293 args
[idx
++] = LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, "");
1295 args
[idx
++] = vindex
? vindex
: ctx
->i32_0
;
1296 args
[idx
++] = voffset
? voffset
: ctx
->i32_0
;
1297 args
[idx
++] = soffset
? soffset
: ctx
->i32_0
;
1298 args
[idx
++] = LLVMConstInt(ctx
->i32
, get_load_cache_policy(ctx
, cache_policy
), 0);
1299 unsigned func
= !ac_has_vec3_support(ctx
->chip_class
, use_format
) && num_channels
== 3 ? 4 : num_channels
;
1300 const char *indexing_kind
= structurized
? "struct" : "raw";
1301 char name
[256], type_name
[8];
1303 LLVMTypeRef type
= func
> 1 ? LLVMVectorType(channel_type
, func
) : channel_type
;
1304 ac_build_type_name_for_intr(type
, type_name
, sizeof(type_name
));
1307 snprintf(name
, sizeof(name
), "llvm.amdgcn.%s.buffer.load.format.%s",
1308 indexing_kind
, type_name
);
1310 snprintf(name
, sizeof(name
), "llvm.amdgcn.%s.buffer.load.%s",
1311 indexing_kind
, type_name
);
1314 return ac_build_intrinsic(ctx
, name
, type
, args
, idx
,
1315 ac_get_load_intr_attribs(can_speculate
));
1319 ac_build_buffer_load(struct ac_llvm_context
*ctx
,
1322 LLVMValueRef vindex
,
1323 LLVMValueRef voffset
,
1324 LLVMValueRef soffset
,
1325 unsigned inst_offset
,
1326 unsigned cache_policy
,
1330 LLVMValueRef offset
= LLVMConstInt(ctx
->i32
, inst_offset
, 0);
1332 offset
= LLVMBuildAdd(ctx
->builder
, offset
, voffset
, "");
1334 offset
= LLVMBuildAdd(ctx
->builder
, offset
, soffset
, "");
1336 if (allow_smem
&& !(cache_policy
& ac_slc
) &&
1337 (!(cache_policy
& ac_glc
) || ctx
->chip_class
>= GFX8
)) {
1338 assert(vindex
== NULL
);
1340 LLVMValueRef result
[8];
1342 for (int i
= 0; i
< num_channels
; i
++) {
1344 offset
= LLVMBuildAdd(ctx
->builder
, offset
,
1345 LLVMConstInt(ctx
->i32
, 4, 0), "");
1347 LLVMValueRef args
[3] = {
1350 LLVMConstInt(ctx
->i32
, get_load_cache_policy(ctx
, cache_policy
), 0),
1352 result
[i
] = ac_build_intrinsic(ctx
,
1353 "llvm.amdgcn.s.buffer.load.f32",
1355 AC_FUNC_ATTR_READNONE
);
1357 if (num_channels
== 1)
1360 if (num_channels
== 3 && !ac_has_vec3_support(ctx
->chip_class
, false))
1361 result
[num_channels
++] = LLVMGetUndef(ctx
->f32
);
1362 return ac_build_gather_values(ctx
, result
, num_channels
);
1365 return ac_build_buffer_load_common(ctx
, rsrc
, vindex
,
1367 num_channels
, ctx
->f32
,
1369 can_speculate
, false, false);
1372 LLVMValueRef
ac_build_buffer_load_format(struct ac_llvm_context
*ctx
,
1374 LLVMValueRef vindex
,
1375 LLVMValueRef voffset
,
1376 unsigned num_channels
,
1377 unsigned cache_policy
,
1380 return ac_build_buffer_load_common(ctx
, rsrc
, vindex
, voffset
,
1381 ctx
->i32_0
, num_channels
, ctx
->f32
,
1382 cache_policy
, can_speculate
,
1387 ac_build_tbuffer_load(struct ac_llvm_context
*ctx
,
1389 LLVMValueRef vindex
,
1390 LLVMValueRef voffset
,
1391 LLVMValueRef soffset
,
1392 LLVMValueRef immoffset
,
1393 unsigned num_channels
,
1396 unsigned cache_policy
,
1400 voffset
= LLVMBuildAdd(ctx
->builder
, voffset
, immoffset
, "");
1402 LLVMValueRef args
[6];
1404 args
[idx
++] = LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, "");
1406 args
[idx
++] = vindex
? vindex
: ctx
->i32_0
;
1407 args
[idx
++] = voffset
? voffset
: ctx
->i32_0
;
1408 args
[idx
++] = soffset
? soffset
: ctx
->i32_0
;
1409 args
[idx
++] = LLVMConstInt(ctx
->i32
, ac_get_tbuffer_format(ctx
->chip_class
, dfmt
, nfmt
), 0);
1410 args
[idx
++] = LLVMConstInt(ctx
->i32
, get_load_cache_policy(ctx
, cache_policy
), 0);
1411 unsigned func
= !ac_has_vec3_support(ctx
->chip_class
, true) && num_channels
== 3 ? 4 : num_channels
;
1412 const char *indexing_kind
= structurized
? "struct" : "raw";
1413 char name
[256], type_name
[8];
1415 LLVMTypeRef type
= func
> 1 ? LLVMVectorType(ctx
->i32
, func
) : ctx
->i32
;
1416 ac_build_type_name_for_intr(type
, type_name
, sizeof(type_name
));
1418 snprintf(name
, sizeof(name
), "llvm.amdgcn.%s.tbuffer.load.%s",
1419 indexing_kind
, type_name
);
1421 return ac_build_intrinsic(ctx
, name
, type
, args
, idx
,
1422 ac_get_load_intr_attribs(can_speculate
));
1426 ac_build_struct_tbuffer_load(struct ac_llvm_context
*ctx
,
1428 LLVMValueRef vindex
,
1429 LLVMValueRef voffset
,
1430 LLVMValueRef soffset
,
1431 LLVMValueRef immoffset
,
1432 unsigned num_channels
,
1435 unsigned cache_policy
,
1438 return ac_build_tbuffer_load(ctx
, rsrc
, vindex
, voffset
, soffset
,
1439 immoffset
, num_channels
, dfmt
, nfmt
,
1440 cache_policy
, can_speculate
, true);
1444 ac_build_raw_tbuffer_load(struct ac_llvm_context
*ctx
,
1446 LLVMValueRef voffset
,
1447 LLVMValueRef soffset
,
1448 LLVMValueRef immoffset
,
1449 unsigned num_channels
,
1452 unsigned cache_policy
,
1455 return ac_build_tbuffer_load(ctx
, rsrc
, NULL
, voffset
, soffset
,
1456 immoffset
, num_channels
, dfmt
, nfmt
,
1457 cache_policy
, can_speculate
, false);
1461 ac_build_tbuffer_load_short(struct ac_llvm_context
*ctx
,
1463 LLVMValueRef voffset
,
1464 LLVMValueRef soffset
,
1465 LLVMValueRef immoffset
,
1466 unsigned cache_policy
)
1470 if (LLVM_VERSION_MAJOR
>= 9) {
1471 voffset
= LLVMBuildAdd(ctx
->builder
, voffset
, immoffset
, "");
1473 /* LLVM 9+ supports i8/i16 with struct/raw intrinsics. */
1474 res
= ac_build_buffer_load_common(ctx
, rsrc
, NULL
,
1476 1, ctx
->i16
, cache_policy
,
1477 false, false, false);
1479 unsigned dfmt
= V_008F0C_BUF_DATA_FORMAT_16
;
1480 unsigned nfmt
= V_008F0C_BUF_NUM_FORMAT_UINT
;
1482 res
= ac_build_raw_tbuffer_load(ctx
, rsrc
, voffset
, soffset
,
1483 immoffset
, 1, dfmt
, nfmt
, cache_policy
,
1486 res
= LLVMBuildTrunc(ctx
->builder
, res
, ctx
->i16
, "");
1493 ac_build_tbuffer_load_byte(struct ac_llvm_context
*ctx
,
1495 LLVMValueRef voffset
,
1496 LLVMValueRef soffset
,
1497 LLVMValueRef immoffset
,
1498 unsigned cache_policy
)
1502 if (LLVM_VERSION_MAJOR
>= 9) {
1503 voffset
= LLVMBuildAdd(ctx
->builder
, voffset
, immoffset
, "");
1505 /* LLVM 9+ supports i8/i16 with struct/raw intrinsics. */
1506 res
= ac_build_buffer_load_common(ctx
, rsrc
, NULL
,
1508 1, ctx
->i8
, cache_policy
,
1509 false, false, false);
1511 unsigned dfmt
= V_008F0C_BUF_DATA_FORMAT_8
;
1512 unsigned nfmt
= V_008F0C_BUF_NUM_FORMAT_UINT
;
1514 res
= ac_build_raw_tbuffer_load(ctx
, rsrc
, voffset
, soffset
,
1515 immoffset
, 1, dfmt
, nfmt
, cache_policy
,
1518 res
= LLVMBuildTrunc(ctx
->builder
, res
, ctx
->i8
, "");
1525 * Convert an 11- or 10-bit unsigned floating point number to an f32.
1527 * The input exponent is expected to be biased analogous to IEEE-754, i.e. by
1528 * 2^(exp_bits-1) - 1 (as defined in OpenGL and other graphics APIs).
1531 ac_ufN_to_float(struct ac_llvm_context
*ctx
, LLVMValueRef src
, unsigned exp_bits
, unsigned mant_bits
)
1533 assert(LLVMTypeOf(src
) == ctx
->i32
);
1536 LLVMValueRef mantissa
;
1537 mantissa
= LLVMBuildAnd(ctx
->builder
, src
, LLVMConstInt(ctx
->i32
, (1 << mant_bits
) - 1, false), "");
1539 /* Converting normal numbers is just a shift + correcting the exponent bias */
1540 unsigned normal_shift
= 23 - mant_bits
;
1541 unsigned bias_shift
= 127 - ((1 << (exp_bits
- 1)) - 1);
1542 LLVMValueRef shifted
, normal
;
1544 shifted
= LLVMBuildShl(ctx
->builder
, src
, LLVMConstInt(ctx
->i32
, normal_shift
, false), "");
1545 normal
= LLVMBuildAdd(ctx
->builder
, shifted
, LLVMConstInt(ctx
->i32
, bias_shift
<< 23, false), "");
1547 /* Converting nan/inf numbers is the same, but with a different exponent update */
1548 LLVMValueRef naninf
;
1549 naninf
= LLVMBuildOr(ctx
->builder
, normal
, LLVMConstInt(ctx
->i32
, 0xff << 23, false), "");
1551 /* Converting denormals is the complex case: determine the leading zeros of the
1552 * mantissa to obtain the correct shift for the mantissa and exponent correction.
1554 LLVMValueRef denormal
;
1555 LLVMValueRef params
[2] = {
1557 ctx
->i1true
, /* result can be undef when arg is 0 */
1559 LLVMValueRef ctlz
= ac_build_intrinsic(ctx
, "llvm.ctlz.i32", ctx
->i32
,
1560 params
, 2, AC_FUNC_ATTR_READNONE
);
1562 /* Shift such that the leading 1 ends up as the LSB of the exponent field. */
1563 tmp
= LLVMBuildSub(ctx
->builder
, ctlz
, LLVMConstInt(ctx
->i32
, 8, false), "");
1564 denormal
= LLVMBuildShl(ctx
->builder
, mantissa
, tmp
, "");
1566 unsigned denormal_exp
= bias_shift
+ (32 - mant_bits
) - 1;
1567 tmp
= LLVMBuildSub(ctx
->builder
, LLVMConstInt(ctx
->i32
, denormal_exp
, false), ctlz
, "");
1568 tmp
= LLVMBuildShl(ctx
->builder
, tmp
, LLVMConstInt(ctx
->i32
, 23, false), "");
1569 denormal
= LLVMBuildAdd(ctx
->builder
, denormal
, tmp
, "");
1571 /* Select the final result. */
1572 LLVMValueRef result
;
1574 tmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntUGE
, src
,
1575 LLVMConstInt(ctx
->i32
, ((1 << exp_bits
) - 1) << mant_bits
, false), "");
1576 result
= LLVMBuildSelect(ctx
->builder
, tmp
, naninf
, normal
, "");
1578 tmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntUGE
, src
,
1579 LLVMConstInt(ctx
->i32
, 1 << mant_bits
, false), "");
1580 result
= LLVMBuildSelect(ctx
->builder
, tmp
, result
, denormal
, "");
1582 tmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src
, ctx
->i32_0
, "");
1583 result
= LLVMBuildSelect(ctx
->builder
, tmp
, result
, ctx
->i32_0
, "");
1585 return ac_to_float(ctx
, result
);
1589 * Generate a fully general open coded buffer format fetch with all required
1590 * fixups suitable for vertex fetch, using non-format buffer loads.
1592 * Some combinations of argument values have special interpretations:
1593 * - size = 8 bytes, format = fixed indicates PIPE_FORMAT_R11G11B10_FLOAT
1594 * - size = 8 bytes, format != {float,fixed} indicates a 2_10_10_10 data format
1596 * \param log_size log(size of channel in bytes)
1597 * \param num_channels number of channels (1 to 4)
1598 * \param format AC_FETCH_FORMAT_xxx value
1599 * \param reverse whether XYZ channels are reversed
1600 * \param known_aligned whether the source is known to be aligned to hardware's
1601 * effective element size for loading the given format
1602 * (note: this means dword alignment for 8_8_8_8, 16_16, etc.)
1603 * \param rsrc buffer resource descriptor
1604 * \return the resulting vector of floats or integers bitcast to <4 x i32>
1607 ac_build_opencoded_load_format(struct ac_llvm_context
*ctx
,
1609 unsigned num_channels
,
1614 LLVMValueRef vindex
,
1615 LLVMValueRef voffset
,
1616 LLVMValueRef soffset
,
1617 unsigned cache_policy
,
1621 unsigned load_log_size
= log_size
;
1622 unsigned load_num_channels
= num_channels
;
1623 if (log_size
== 3) {
1625 if (format
== AC_FETCH_FORMAT_FLOAT
) {
1626 load_num_channels
= 2 * num_channels
;
1628 load_num_channels
= 1; /* 10_11_11 or 2_10_10_10 */
1632 int log_recombine
= 0;
1633 if (ctx
->chip_class
== GFX6
&& !known_aligned
) {
1634 /* Avoid alignment restrictions by loading one byte at a time. */
1635 load_num_channels
<<= load_log_size
;
1636 log_recombine
= load_log_size
;
1638 } else if (load_num_channels
== 2 || load_num_channels
== 4) {
1639 log_recombine
= -util_logbase2(load_num_channels
);
1640 load_num_channels
= 1;
1641 load_log_size
+= -log_recombine
;
1644 assert(load_log_size
>= 2 || LLVM_VERSION_MAJOR
>= 9);
1646 LLVMValueRef loads
[32]; /* up to 32 bytes */
1647 for (unsigned i
= 0; i
< load_num_channels
; ++i
) {
1648 tmp
= LLVMBuildAdd(ctx
->builder
, soffset
,
1649 LLVMConstInt(ctx
->i32
, i
<< load_log_size
, false), "");
1650 LLVMTypeRef channel_type
= load_log_size
== 0 ? ctx
->i8
:
1651 load_log_size
== 1 ? ctx
->i16
: ctx
->i32
;
1652 unsigned num_channels
= 1 << (MAX2(load_log_size
, 2) - 2);
1653 loads
[i
] = ac_build_buffer_load_common(
1654 ctx
, rsrc
, vindex
, voffset
, tmp
,
1655 num_channels
, channel_type
, cache_policy
,
1656 can_speculate
, false, true);
1657 if (load_log_size
>= 2)
1658 loads
[i
] = ac_to_integer(ctx
, loads
[i
]);
1661 if (log_recombine
> 0) {
1662 /* Recombine bytes if necessary (GFX6 only) */
1663 LLVMTypeRef dst_type
= log_recombine
== 2 ? ctx
->i32
: ctx
->i16
;
1665 for (unsigned src
= 0, dst
= 0; src
< load_num_channels
; ++dst
) {
1666 LLVMValueRef accum
= NULL
;
1667 for (unsigned i
= 0; i
< (1 << log_recombine
); ++i
, ++src
) {
1668 tmp
= LLVMBuildZExt(ctx
->builder
, loads
[src
], dst_type
, "");
1672 tmp
= LLVMBuildShl(ctx
->builder
, tmp
,
1673 LLVMConstInt(dst_type
, 8 * i
, false), "");
1674 accum
= LLVMBuildOr(ctx
->builder
, accum
, tmp
, "");
1679 } else if (log_recombine
< 0) {
1680 /* Split vectors of dwords */
1681 if (load_log_size
> 2) {
1682 assert(load_num_channels
== 1);
1683 LLVMValueRef loaded
= loads
[0];
1684 unsigned log_split
= load_log_size
- 2;
1685 log_recombine
+= log_split
;
1686 load_num_channels
= 1 << log_split
;
1688 for (unsigned i
= 0; i
< load_num_channels
; ++i
) {
1689 tmp
= LLVMConstInt(ctx
->i32
, i
, false);
1690 loads
[i
] = LLVMBuildExtractElement(ctx
->builder
, loaded
, tmp
, "");
1694 /* Further split dwords and shorts if required */
1695 if (log_recombine
< 0) {
1696 for (unsigned src
= load_num_channels
,
1697 dst
= load_num_channels
<< -log_recombine
;
1699 unsigned dst_bits
= 1 << (3 + load_log_size
+ log_recombine
);
1700 LLVMTypeRef dst_type
= LLVMIntTypeInContext(ctx
->context
, dst_bits
);
1701 LLVMValueRef loaded
= loads
[src
- 1];
1702 LLVMTypeRef loaded_type
= LLVMTypeOf(loaded
);
1703 for (unsigned i
= 1 << -log_recombine
; i
> 0; --i
, --dst
) {
1704 tmp
= LLVMConstInt(loaded_type
, dst_bits
* (i
- 1), false);
1705 tmp
= LLVMBuildLShr(ctx
->builder
, loaded
, tmp
, "");
1706 loads
[dst
- 1] = LLVMBuildTrunc(ctx
->builder
, tmp
, dst_type
, "");
1712 if (log_size
== 3) {
1713 if (format
== AC_FETCH_FORMAT_FLOAT
) {
1714 for (unsigned i
= 0; i
< num_channels
; ++i
) {
1715 tmp
= ac_build_gather_values(ctx
, &loads
[2 * i
], 2);
1716 loads
[i
] = LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f64
, "");
1718 } else if (format
== AC_FETCH_FORMAT_FIXED
) {
1719 /* 10_11_11_FLOAT */
1720 LLVMValueRef data
= loads
[0];
1721 LLVMValueRef i32_2047
= LLVMConstInt(ctx
->i32
, 2047, false);
1722 LLVMValueRef r
= LLVMBuildAnd(ctx
->builder
, data
, i32_2047
, "");
1723 tmp
= LLVMBuildLShr(ctx
->builder
, data
, LLVMConstInt(ctx
->i32
, 11, false), "");
1724 LLVMValueRef g
= LLVMBuildAnd(ctx
->builder
, tmp
, i32_2047
, "");
1725 LLVMValueRef b
= LLVMBuildLShr(ctx
->builder
, data
, LLVMConstInt(ctx
->i32
, 22, false), "");
1727 loads
[0] = ac_to_integer(ctx
, ac_ufN_to_float(ctx
, r
, 5, 6));
1728 loads
[1] = ac_to_integer(ctx
, ac_ufN_to_float(ctx
, g
, 5, 6));
1729 loads
[2] = ac_to_integer(ctx
, ac_ufN_to_float(ctx
, b
, 5, 5));
1733 format
= AC_FETCH_FORMAT_FLOAT
;
1735 /* 2_10_10_10 data formats */
1736 LLVMValueRef data
= loads
[0];
1737 LLVMTypeRef i10
= LLVMIntTypeInContext(ctx
->context
, 10);
1738 LLVMTypeRef i2
= LLVMIntTypeInContext(ctx
->context
, 2);
1739 loads
[0] = LLVMBuildTrunc(ctx
->builder
, data
, i10
, "");
1740 tmp
= LLVMBuildLShr(ctx
->builder
, data
, LLVMConstInt(ctx
->i32
, 10, false), "");
1741 loads
[1] = LLVMBuildTrunc(ctx
->builder
, tmp
, i10
, "");
1742 tmp
= LLVMBuildLShr(ctx
->builder
, data
, LLVMConstInt(ctx
->i32
, 20, false), "");
1743 loads
[2] = LLVMBuildTrunc(ctx
->builder
, tmp
, i10
, "");
1744 tmp
= LLVMBuildLShr(ctx
->builder
, data
, LLVMConstInt(ctx
->i32
, 30, false), "");
1745 loads
[3] = LLVMBuildTrunc(ctx
->builder
, tmp
, i2
, "");
1751 if (format
== AC_FETCH_FORMAT_FLOAT
) {
1752 if (log_size
!= 2) {
1753 for (unsigned chan
= 0; chan
< num_channels
; ++chan
) {
1754 tmp
= ac_to_float(ctx
, loads
[chan
]);
1756 tmp
= LLVMBuildFPTrunc(ctx
->builder
, tmp
, ctx
->f32
, "");
1757 else if (log_size
== 1)
1758 tmp
= LLVMBuildFPExt(ctx
->builder
, tmp
, ctx
->f32
, "");
1759 loads
[chan
] = ac_to_integer(ctx
, tmp
);
1762 } else if (format
== AC_FETCH_FORMAT_UINT
) {
1763 if (log_size
!= 2) {
1764 for (unsigned chan
= 0; chan
< num_channels
; ++chan
)
1765 loads
[chan
] = LLVMBuildZExt(ctx
->builder
, loads
[chan
], ctx
->i32
, "");
1767 } else if (format
== AC_FETCH_FORMAT_SINT
) {
1768 if (log_size
!= 2) {
1769 for (unsigned chan
= 0; chan
< num_channels
; ++chan
)
1770 loads
[chan
] = LLVMBuildSExt(ctx
->builder
, loads
[chan
], ctx
->i32
, "");
1773 bool unsign
= format
== AC_FETCH_FORMAT_UNORM
||
1774 format
== AC_FETCH_FORMAT_USCALED
||
1775 format
== AC_FETCH_FORMAT_UINT
;
1777 for (unsigned chan
= 0; chan
< num_channels
; ++chan
) {
1779 tmp
= LLVMBuildUIToFP(ctx
->builder
, loads
[chan
], ctx
->f32
, "");
1781 tmp
= LLVMBuildSIToFP(ctx
->builder
, loads
[chan
], ctx
->f32
, "");
1784 LLVMValueRef scale
= NULL
;
1785 if (format
== AC_FETCH_FORMAT_FIXED
) {
1786 assert(log_size
== 2);
1787 scale
= LLVMConstReal(ctx
->f32
, 1.0 / 0x10000);
1788 } else if (format
== AC_FETCH_FORMAT_UNORM
) {
1789 unsigned bits
= LLVMGetIntTypeWidth(LLVMTypeOf(loads
[chan
]));
1790 scale
= LLVMConstReal(ctx
->f32
, 1.0 / (((uint64_t)1 << bits
) - 1));
1791 } else if (format
== AC_FETCH_FORMAT_SNORM
) {
1792 unsigned bits
= LLVMGetIntTypeWidth(LLVMTypeOf(loads
[chan
]));
1793 scale
= LLVMConstReal(ctx
->f32
, 1.0 / (((uint64_t)1 << (bits
- 1)) - 1));
1796 tmp
= LLVMBuildFMul(ctx
->builder
, tmp
, scale
, "");
1798 if (format
== AC_FETCH_FORMAT_SNORM
) {
1799 /* Clamp to [-1, 1] */
1800 LLVMValueRef neg_one
= LLVMConstReal(ctx
->f32
, -1.0);
1801 LLVMValueRef clamp
=
1802 LLVMBuildFCmp(ctx
->builder
, LLVMRealULT
, tmp
, neg_one
, "");
1803 tmp
= LLVMBuildSelect(ctx
->builder
, clamp
, neg_one
, tmp
, "");
1806 loads
[chan
] = ac_to_integer(ctx
, tmp
);
1810 while (num_channels
< 4) {
1811 if (format
== AC_FETCH_FORMAT_UINT
|| format
== AC_FETCH_FORMAT_SINT
) {
1812 loads
[num_channels
] = num_channels
== 3 ? ctx
->i32_1
: ctx
->i32_0
;
1814 loads
[num_channels
] = ac_to_integer(ctx
, num_channels
== 3 ? ctx
->f32_1
: ctx
->f32_0
);
1821 loads
[0] = loads
[2];
1825 return ac_build_gather_values(ctx
, loads
, 4);
1829 ac_build_tbuffer_store(struct ac_llvm_context
*ctx
,
1832 LLVMValueRef vindex
,
1833 LLVMValueRef voffset
,
1834 LLVMValueRef soffset
,
1835 LLVMValueRef immoffset
,
1836 unsigned num_channels
,
1839 unsigned cache_policy
,
1842 voffset
= LLVMBuildAdd(ctx
->builder
, voffset
? voffset
: ctx
->i32_0
,
1845 LLVMValueRef args
[7];
1847 args
[idx
++] = vdata
;
1848 args
[idx
++] = LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, "");
1850 args
[idx
++] = vindex
? vindex
: ctx
->i32_0
;
1851 args
[idx
++] = voffset
? voffset
: ctx
->i32_0
;
1852 args
[idx
++] = soffset
? soffset
: ctx
->i32_0
;
1853 args
[idx
++] = LLVMConstInt(ctx
->i32
, ac_get_tbuffer_format(ctx
->chip_class
, dfmt
, nfmt
), 0);
1854 args
[idx
++] = LLVMConstInt(ctx
->i32
, cache_policy
, 0);
1855 unsigned func
= !ac_has_vec3_support(ctx
->chip_class
, true) && num_channels
== 3 ? 4 : num_channels
;
1856 const char *indexing_kind
= structurized
? "struct" : "raw";
1857 char name
[256], type_name
[8];
1859 LLVMTypeRef type
= func
> 1 ? LLVMVectorType(ctx
->i32
, func
) : ctx
->i32
;
1860 ac_build_type_name_for_intr(type
, type_name
, sizeof(type_name
));
1862 snprintf(name
, sizeof(name
), "llvm.amdgcn.%s.tbuffer.store.%s",
1863 indexing_kind
, type_name
);
1865 ac_build_intrinsic(ctx
, name
, ctx
->voidt
, args
, idx
,
1866 AC_FUNC_ATTR_INACCESSIBLE_MEM_ONLY
);
1870 ac_build_struct_tbuffer_store(struct ac_llvm_context
*ctx
,
1873 LLVMValueRef vindex
,
1874 LLVMValueRef voffset
,
1875 LLVMValueRef soffset
,
1876 LLVMValueRef immoffset
,
1877 unsigned num_channels
,
1880 unsigned cache_policy
)
1882 ac_build_tbuffer_store(ctx
, rsrc
, vdata
, vindex
, voffset
, soffset
,
1883 immoffset
, num_channels
, dfmt
, nfmt
, cache_policy
,
1888 ac_build_raw_tbuffer_store(struct ac_llvm_context
*ctx
,
1891 LLVMValueRef voffset
,
1892 LLVMValueRef soffset
,
1893 LLVMValueRef immoffset
,
1894 unsigned num_channels
,
1897 unsigned cache_policy
)
1899 ac_build_tbuffer_store(ctx
, rsrc
, vdata
, NULL
, voffset
, soffset
,
1900 immoffset
, num_channels
, dfmt
, nfmt
, cache_policy
,
1905 ac_build_tbuffer_store_short(struct ac_llvm_context
*ctx
,
1908 LLVMValueRef voffset
,
1909 LLVMValueRef soffset
,
1910 unsigned cache_policy
)
1912 vdata
= LLVMBuildBitCast(ctx
->builder
, vdata
, ctx
->i16
, "");
1914 if (LLVM_VERSION_MAJOR
>= 9) {
1915 /* LLVM 9+ supports i8/i16 with struct/raw intrinsics. */
1916 ac_build_buffer_store_common(ctx
, rsrc
, vdata
, NULL
,
1917 voffset
, soffset
, 1,
1918 ctx
->i16
, cache_policy
,
1921 unsigned dfmt
= V_008F0C_BUF_DATA_FORMAT_16
;
1922 unsigned nfmt
= V_008F0C_BUF_NUM_FORMAT_UINT
;
1924 vdata
= LLVMBuildZExt(ctx
->builder
, vdata
, ctx
->i32
, "");
1926 ac_build_raw_tbuffer_store(ctx
, rsrc
, vdata
, voffset
, soffset
,
1927 ctx
->i32_0
, 1, dfmt
, nfmt
, cache_policy
);
1932 ac_build_tbuffer_store_byte(struct ac_llvm_context
*ctx
,
1935 LLVMValueRef voffset
,
1936 LLVMValueRef soffset
,
1937 unsigned cache_policy
)
1939 vdata
= LLVMBuildBitCast(ctx
->builder
, vdata
, ctx
->i8
, "");
1941 if (LLVM_VERSION_MAJOR
>= 9) {
1942 /* LLVM 9+ supports i8/i16 with struct/raw intrinsics. */
1943 ac_build_buffer_store_common(ctx
, rsrc
, vdata
, NULL
,
1944 voffset
, soffset
, 1,
1945 ctx
->i8
, cache_policy
,
1948 unsigned dfmt
= V_008F0C_BUF_DATA_FORMAT_8
;
1949 unsigned nfmt
= V_008F0C_BUF_NUM_FORMAT_UINT
;
1951 vdata
= LLVMBuildZExt(ctx
->builder
, vdata
, ctx
->i32
, "");
1953 ac_build_raw_tbuffer_store(ctx
, rsrc
, vdata
, voffset
, soffset
,
1954 ctx
->i32_0
, 1, dfmt
, nfmt
, cache_policy
);
1958 * Set range metadata on an instruction. This can only be used on load and
1959 * call instructions. If you know an instruction can only produce the values
1960 * 0, 1, 2, you would do set_range_metadata(value, 0, 3);
1961 * \p lo is the minimum value inclusive.
1962 * \p hi is the maximum value exclusive.
1964 static void set_range_metadata(struct ac_llvm_context
*ctx
,
1965 LLVMValueRef value
, unsigned lo
, unsigned hi
)
1967 LLVMValueRef range_md
, md_args
[2];
1968 LLVMTypeRef type
= LLVMTypeOf(value
);
1969 LLVMContextRef context
= LLVMGetTypeContext(type
);
1971 md_args
[0] = LLVMConstInt(type
, lo
, false);
1972 md_args
[1] = LLVMConstInt(type
, hi
, false);
1973 range_md
= LLVMMDNodeInContext(context
, md_args
, 2);
1974 LLVMSetMetadata(value
, ctx
->range_md_kind
, range_md
);
1978 ac_get_thread_id(struct ac_llvm_context
*ctx
)
1982 LLVMValueRef tid_args
[2];
1983 tid_args
[0] = LLVMConstInt(ctx
->i32
, 0xffffffff, false);
1984 tid_args
[1] = ctx
->i32_0
;
1985 tid_args
[1] = ac_build_intrinsic(ctx
,
1986 "llvm.amdgcn.mbcnt.lo", ctx
->i32
,
1987 tid_args
, 2, AC_FUNC_ATTR_READNONE
);
1989 if (ctx
->wave_size
== 32) {
1992 tid
= ac_build_intrinsic(ctx
, "llvm.amdgcn.mbcnt.hi",
1994 2, AC_FUNC_ATTR_READNONE
);
1996 set_range_metadata(ctx
, tid
, 0, ctx
->wave_size
);
2001 * AMD GCN implements derivatives using the local data store (LDS)
2002 * All writes to the LDS happen in all executing threads at
2003 * the same time. TID is the Thread ID for the current
2004 * thread and is a value between 0 and 63, representing
2005 * the thread's position in the wavefront.
2007 * For the pixel shader threads are grouped into quads of four pixels.
2008 * The TIDs of the pixels of a quad are:
2016 * So, masking the TID with 0xfffffffc yields the TID of the top left pixel
2017 * of the quad, masking with 0xfffffffd yields the TID of the top pixel of
2018 * the current pixel's column, and masking with 0xfffffffe yields the TID
2019 * of the left pixel of the current pixel's row.
2021 * Adding 1 yields the TID of the pixel to the right of the left pixel, and
2022 * adding 2 yields the TID of the pixel below the top pixel.
2025 ac_build_ddxy(struct ac_llvm_context
*ctx
,
2030 unsigned tl_lanes
[4], trbl_lanes
[4];
2031 char name
[32], type
[8];
2032 LLVMValueRef tl
, trbl
;
2033 LLVMTypeRef result_type
;
2034 LLVMValueRef result
;
2036 result_type
= ac_to_float_type(ctx
, LLVMTypeOf(val
));
2038 if (result_type
== ctx
->f16
)
2039 val
= LLVMBuildZExt(ctx
->builder
, val
, ctx
->i32
, "");
2041 for (unsigned i
= 0; i
< 4; ++i
) {
2042 tl_lanes
[i
] = i
& mask
;
2043 trbl_lanes
[i
] = (i
& mask
) + idx
;
2046 tl
= ac_build_quad_swizzle(ctx
, val
,
2047 tl_lanes
[0], tl_lanes
[1],
2048 tl_lanes
[2], tl_lanes
[3]);
2049 trbl
= ac_build_quad_swizzle(ctx
, val
,
2050 trbl_lanes
[0], trbl_lanes
[1],
2051 trbl_lanes
[2], trbl_lanes
[3]);
2053 if (result_type
== ctx
->f16
) {
2054 tl
= LLVMBuildTrunc(ctx
->builder
, tl
, ctx
->i16
, "");
2055 trbl
= LLVMBuildTrunc(ctx
->builder
, trbl
, ctx
->i16
, "");
2058 tl
= LLVMBuildBitCast(ctx
->builder
, tl
, result_type
, "");
2059 trbl
= LLVMBuildBitCast(ctx
->builder
, trbl
, result_type
, "");
2060 result
= LLVMBuildFSub(ctx
->builder
, trbl
, tl
, "");
2062 ac_build_type_name_for_intr(result_type
, type
, sizeof(type
));
2063 snprintf(name
, sizeof(name
), "llvm.amdgcn.wqm.%s", type
);
2065 return ac_build_intrinsic(ctx
, name
, result_type
, &result
, 1, 0);
2069 ac_build_sendmsg(struct ac_llvm_context
*ctx
,
2071 LLVMValueRef wave_id
)
2073 LLVMValueRef args
[2];
2074 args
[0] = LLVMConstInt(ctx
->i32
, msg
, false);
2076 ac_build_intrinsic(ctx
, "llvm.amdgcn.s.sendmsg", ctx
->voidt
, args
, 2, 0);
2080 ac_build_imsb(struct ac_llvm_context
*ctx
,
2082 LLVMTypeRef dst_type
)
2084 LLVMValueRef msb
= ac_build_intrinsic(ctx
, "llvm.amdgcn.sffbh.i32",
2086 AC_FUNC_ATTR_READNONE
);
2088 /* The HW returns the last bit index from MSB, but NIR/TGSI wants
2089 * the index from LSB. Invert it by doing "31 - msb". */
2090 msb
= LLVMBuildSub(ctx
->builder
, LLVMConstInt(ctx
->i32
, 31, false),
2093 LLVMValueRef all_ones
= LLVMConstInt(ctx
->i32
, -1, true);
2094 LLVMValueRef cond
= LLVMBuildOr(ctx
->builder
,
2095 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
2096 arg
, ctx
->i32_0
, ""),
2097 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
2098 arg
, all_ones
, ""), "");
2100 return LLVMBuildSelect(ctx
->builder
, cond
, all_ones
, msb
, "");
2104 ac_build_umsb(struct ac_llvm_context
*ctx
,
2106 LLVMTypeRef dst_type
)
2108 const char *intrin_name
;
2110 LLVMValueRef highest_bit
;
2114 bitsize
= ac_get_elem_bits(ctx
, LLVMTypeOf(arg
));
2117 intrin_name
= "llvm.ctlz.i64";
2119 highest_bit
= LLVMConstInt(ctx
->i64
, 63, false);
2123 intrin_name
= "llvm.ctlz.i32";
2125 highest_bit
= LLVMConstInt(ctx
->i32
, 31, false);
2129 intrin_name
= "llvm.ctlz.i16";
2131 highest_bit
= LLVMConstInt(ctx
->i16
, 15, false);
2135 intrin_name
= "llvm.ctlz.i8";
2137 highest_bit
= LLVMConstInt(ctx
->i8
, 7, false);
2141 unreachable(!"invalid bitsize");
2145 LLVMValueRef params
[2] = {
2150 LLVMValueRef msb
= ac_build_intrinsic(ctx
, intrin_name
, type
,
2152 AC_FUNC_ATTR_READNONE
);
2154 /* The HW returns the last bit index from MSB, but TGSI/NIR wants
2155 * the index from LSB. Invert it by doing "31 - msb". */
2156 msb
= LLVMBuildSub(ctx
->builder
, highest_bit
, msb
, "");
2158 if (bitsize
== 64) {
2159 msb
= LLVMBuildTrunc(ctx
->builder
, msb
, ctx
->i32
, "");
2160 } else if (bitsize
< 32) {
2161 msb
= LLVMBuildSExt(ctx
->builder
, msb
, ctx
->i32
, "");
2164 /* check for zero */
2165 return LLVMBuildSelect(ctx
->builder
,
2166 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, arg
, zero
, ""),
2167 LLVMConstInt(ctx
->i32
, -1, true), msb
, "");
2170 LLVMValueRef
ac_build_fmin(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
2174 snprintf(name
, sizeof(name
), "llvm.minnum.f%d", ac_get_elem_bits(ctx
, LLVMTypeOf(a
)));
2175 LLVMValueRef args
[2] = {a
, b
};
2176 return ac_build_intrinsic(ctx
, name
, LLVMTypeOf(a
), args
, 2,
2177 AC_FUNC_ATTR_READNONE
);
2180 LLVMValueRef
ac_build_fmax(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
2184 snprintf(name
, sizeof(name
), "llvm.maxnum.f%d", ac_get_elem_bits(ctx
, LLVMTypeOf(a
)));
2185 LLVMValueRef args
[2] = {a
, b
};
2186 return ac_build_intrinsic(ctx
, name
, LLVMTypeOf(a
), args
, 2,
2187 AC_FUNC_ATTR_READNONE
);
2190 LLVMValueRef
ac_build_imin(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
2193 LLVMValueRef cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSLE
, a
, b
, "");
2194 return LLVMBuildSelect(ctx
->builder
, cmp
, a
, b
, "");
2197 LLVMValueRef
ac_build_imax(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
2200 LLVMValueRef cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGT
, a
, b
, "");
2201 return LLVMBuildSelect(ctx
->builder
, cmp
, a
, b
, "");
2204 LLVMValueRef
ac_build_umin(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
2207 LLVMValueRef cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntULE
, a
, b
, "");
2208 return LLVMBuildSelect(ctx
->builder
, cmp
, a
, b
, "");
2211 LLVMValueRef
ac_build_umax(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
2214 LLVMValueRef cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntUGE
, a
, b
, "");
2215 return LLVMBuildSelect(ctx
->builder
, cmp
, a
, b
, "");
2218 LLVMValueRef
ac_build_clamp(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
2220 LLVMTypeRef t
= LLVMTypeOf(value
);
2221 return ac_build_fmin(ctx
, ac_build_fmax(ctx
, value
, LLVMConstReal(t
, 0.0)),
2222 LLVMConstReal(t
, 1.0));
2225 void ac_build_export(struct ac_llvm_context
*ctx
, struct ac_export_args
*a
)
2227 LLVMValueRef args
[9];
2229 args
[0] = LLVMConstInt(ctx
->i32
, a
->target
, 0);
2230 args
[1] = LLVMConstInt(ctx
->i32
, a
->enabled_channels
, 0);
2233 LLVMTypeRef i16
= LLVMInt16TypeInContext(ctx
->context
);
2234 LLVMTypeRef v2i16
= LLVMVectorType(i16
, 2);
2236 args
[2] = LLVMBuildBitCast(ctx
->builder
, a
->out
[0],
2238 args
[3] = LLVMBuildBitCast(ctx
->builder
, a
->out
[1],
2240 args
[4] = LLVMConstInt(ctx
->i1
, a
->done
, 0);
2241 args
[5] = LLVMConstInt(ctx
->i1
, a
->valid_mask
, 0);
2243 ac_build_intrinsic(ctx
, "llvm.amdgcn.exp.compr.v2i16",
2244 ctx
->voidt
, args
, 6, 0);
2246 args
[2] = a
->out
[0];
2247 args
[3] = a
->out
[1];
2248 args
[4] = a
->out
[2];
2249 args
[5] = a
->out
[3];
2250 args
[6] = LLVMConstInt(ctx
->i1
, a
->done
, 0);
2251 args
[7] = LLVMConstInt(ctx
->i1
, a
->valid_mask
, 0);
2253 ac_build_intrinsic(ctx
, "llvm.amdgcn.exp.f32",
2254 ctx
->voidt
, args
, 8, 0);
2258 void ac_build_export_null(struct ac_llvm_context
*ctx
)
2260 struct ac_export_args args
;
2262 args
.enabled_channels
= 0x0; /* enabled channels */
2263 args
.valid_mask
= 1; /* whether the EXEC mask is valid */
2264 args
.done
= 1; /* DONE bit */
2265 args
.target
= V_008DFC_SQ_EXP_NULL
;
2266 args
.compr
= 0; /* COMPR flag (0 = 32-bit export) */
2267 args
.out
[0] = LLVMGetUndef(ctx
->f32
); /* R */
2268 args
.out
[1] = LLVMGetUndef(ctx
->f32
); /* G */
2269 args
.out
[2] = LLVMGetUndef(ctx
->f32
); /* B */
2270 args
.out
[3] = LLVMGetUndef(ctx
->f32
); /* A */
2272 ac_build_export(ctx
, &args
);
2275 static unsigned ac_num_coords(enum ac_image_dim dim
)
2281 case ac_image_1darray
:
2285 case ac_image_2darray
:
2286 case ac_image_2dmsaa
:
2288 case ac_image_2darraymsaa
:
2291 unreachable("ac_num_coords: bad dim");
2295 static unsigned ac_num_derivs(enum ac_image_dim dim
)
2299 case ac_image_1darray
:
2302 case ac_image_2darray
:
2307 case ac_image_2dmsaa
:
2308 case ac_image_2darraymsaa
:
2310 unreachable("derivatives not supported");
2314 static const char *get_atomic_name(enum ac_atomic_op op
)
2317 case ac_atomic_swap
: return "swap";
2318 case ac_atomic_add
: return "add";
2319 case ac_atomic_sub
: return "sub";
2320 case ac_atomic_smin
: return "smin";
2321 case ac_atomic_umin
: return "umin";
2322 case ac_atomic_smax
: return "smax";
2323 case ac_atomic_umax
: return "umax";
2324 case ac_atomic_and
: return "and";
2325 case ac_atomic_or
: return "or";
2326 case ac_atomic_xor
: return "xor";
2327 case ac_atomic_inc_wrap
: return "inc";
2328 case ac_atomic_dec_wrap
: return "dec";
2330 unreachable("bad atomic op");
2333 LLVMValueRef
ac_build_image_opcode(struct ac_llvm_context
*ctx
,
2334 struct ac_image_args
*a
)
2336 const char *overload
[3] = { "", "", "" };
2337 unsigned num_overloads
= 0;
2338 LLVMValueRef args
[18];
2339 unsigned num_args
= 0;
2340 enum ac_image_dim dim
= a
->dim
;
2342 assert(!a
->lod
|| a
->lod
== ctx
->i32_0
|| a
->lod
== ctx
->f32_0
||
2344 assert((a
->opcode
!= ac_image_get_resinfo
&& a
->opcode
!= ac_image_load_mip
&&
2345 a
->opcode
!= ac_image_store_mip
) ||
2347 assert(a
->opcode
== ac_image_sample
|| a
->opcode
== ac_image_gather4
||
2348 (!a
->compare
&& !a
->offset
));
2349 assert((a
->opcode
== ac_image_sample
|| a
->opcode
== ac_image_gather4
||
2350 a
->opcode
== ac_image_get_lod
) ||
2352 assert((a
->bias
? 1 : 0) +
2354 (a
->level_zero
? 1 : 0) +
2355 (a
->derivs
[0] ? 1 : 0) <= 1);
2357 if (a
->opcode
== ac_image_get_lod
) {
2359 case ac_image_1darray
:
2362 case ac_image_2darray
:
2371 bool sample
= a
->opcode
== ac_image_sample
||
2372 a
->opcode
== ac_image_gather4
||
2373 a
->opcode
== ac_image_get_lod
;
2374 bool atomic
= a
->opcode
== ac_image_atomic
||
2375 a
->opcode
== ac_image_atomic_cmpswap
;
2376 bool load
= a
->opcode
== ac_image_sample
||
2377 a
->opcode
== ac_image_gather4
||
2378 a
->opcode
== ac_image_load
||
2379 a
->opcode
== ac_image_load_mip
;
2380 LLVMTypeRef coord_type
= sample
? ctx
->f32
: ctx
->i32
;
2382 if (atomic
|| a
->opcode
== ac_image_store
|| a
->opcode
== ac_image_store_mip
) {
2383 args
[num_args
++] = a
->data
[0];
2384 if (a
->opcode
== ac_image_atomic_cmpswap
)
2385 args
[num_args
++] = a
->data
[1];
2389 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->dmask
, false);
2392 args
[num_args
++] = ac_to_integer(ctx
, a
->offset
);
2394 args
[num_args
++] = ac_to_float(ctx
, a
->bias
);
2395 overload
[num_overloads
++] = ".f32";
2398 args
[num_args
++] = ac_to_float(ctx
, a
->compare
);
2400 unsigned count
= ac_num_derivs(dim
);
2401 for (unsigned i
= 0; i
< count
; ++i
)
2402 args
[num_args
++] = ac_to_float(ctx
, a
->derivs
[i
]);
2403 overload
[num_overloads
++] = ".f32";
2405 unsigned num_coords
=
2406 a
->opcode
!= ac_image_get_resinfo
? ac_num_coords(dim
) : 0;
2407 for (unsigned i
= 0; i
< num_coords
; ++i
)
2408 args
[num_args
++] = LLVMBuildBitCast(ctx
->builder
, a
->coords
[i
], coord_type
, "");
2410 args
[num_args
++] = LLVMBuildBitCast(ctx
->builder
, a
->lod
, coord_type
, "");
2411 overload
[num_overloads
++] = sample
? ".f32" : ".i32";
2413 args
[num_args
++] = a
->resource
;
2415 args
[num_args
++] = a
->sampler
;
2416 args
[num_args
++] = LLVMConstInt(ctx
->i1
, a
->unorm
, false);
2419 args
[num_args
++] = ctx
->i32_0
; /* texfailctrl */
2420 args
[num_args
++] = LLVMConstInt(ctx
->i32
,
2421 load
? get_load_cache_policy(ctx
, a
->cache_policy
) :
2422 a
->cache_policy
, false);
2425 const char *atomic_subop
= "";
2426 switch (a
->opcode
) {
2427 case ac_image_sample
: name
= "sample"; break;
2428 case ac_image_gather4
: name
= "gather4"; break;
2429 case ac_image_load
: name
= "load"; break;
2430 case ac_image_load_mip
: name
= "load.mip"; break;
2431 case ac_image_store
: name
= "store"; break;
2432 case ac_image_store_mip
: name
= "store.mip"; break;
2433 case ac_image_atomic
:
2435 atomic_subop
= get_atomic_name(a
->atomic
);
2437 case ac_image_atomic_cmpswap
:
2439 atomic_subop
= "cmpswap";
2441 case ac_image_get_lod
: name
= "getlod"; break;
2442 case ac_image_get_resinfo
: name
= "getresinfo"; break;
2443 default: unreachable("invalid image opcode");
2446 const char *dimname
;
2448 case ac_image_1d
: dimname
= "1d"; break;
2449 case ac_image_2d
: dimname
= "2d"; break;
2450 case ac_image_3d
: dimname
= "3d"; break;
2451 case ac_image_cube
: dimname
= "cube"; break;
2452 case ac_image_1darray
: dimname
= "1darray"; break;
2453 case ac_image_2darray
: dimname
= "2darray"; break;
2454 case ac_image_2dmsaa
: dimname
= "2dmsaa"; break;
2455 case ac_image_2darraymsaa
: dimname
= "2darraymsaa"; break;
2456 default: unreachable("invalid dim");
2460 a
->lod
&& (a
->opcode
== ac_image_sample
|| a
->opcode
== ac_image_gather4
);
2462 snprintf(intr_name
, sizeof(intr_name
),
2463 "llvm.amdgcn.image.%s%s" /* base name */
2464 "%s%s%s" /* sample/gather modifiers */
2465 ".%s.%s%s%s%s", /* dimension and type overloads */
2467 a
->compare
? ".c" : "",
2470 a
->derivs
[0] ? ".d" :
2471 a
->level_zero
? ".lz" : "",
2472 a
->offset
? ".o" : "",
2474 atomic
? "i32" : "v4f32",
2475 overload
[0], overload
[1], overload
[2]);
2480 else if (a
->opcode
== ac_image_store
|| a
->opcode
== ac_image_store_mip
)
2485 LLVMValueRef result
=
2486 ac_build_intrinsic(ctx
, intr_name
, retty
, args
, num_args
,
2488 if (!sample
&& retty
== ctx
->v4f32
) {
2489 result
= LLVMBuildBitCast(ctx
->builder
, result
,
2495 LLVMValueRef
ac_build_image_get_sample_count(struct ac_llvm_context
*ctx
,
2498 LLVMValueRef samples
;
2500 /* Read the samples from the descriptor directly.
2501 * Hardware doesn't have any instruction for this.
2503 samples
= LLVMBuildExtractElement(ctx
->builder
, rsrc
,
2504 LLVMConstInt(ctx
->i32
, 3, 0), "");
2505 samples
= LLVMBuildLShr(ctx
->builder
, samples
,
2506 LLVMConstInt(ctx
->i32
, 16, 0), "");
2507 samples
= LLVMBuildAnd(ctx
->builder
, samples
,
2508 LLVMConstInt(ctx
->i32
, 0xf, 0), "");
2509 samples
= LLVMBuildShl(ctx
->builder
, ctx
->i32_1
,
2514 LLVMValueRef
ac_build_cvt_pkrtz_f16(struct ac_llvm_context
*ctx
,
2515 LLVMValueRef args
[2])
2518 LLVMVectorType(LLVMHalfTypeInContext(ctx
->context
), 2);
2520 return ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pkrtz", v2f16
,
2521 args
, 2, AC_FUNC_ATTR_READNONE
);
2524 LLVMValueRef
ac_build_cvt_pknorm_i16(struct ac_llvm_context
*ctx
,
2525 LLVMValueRef args
[2])
2528 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pknorm.i16",
2529 ctx
->v2i16
, args
, 2,
2530 AC_FUNC_ATTR_READNONE
);
2531 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
2534 LLVMValueRef
ac_build_cvt_pknorm_u16(struct ac_llvm_context
*ctx
,
2535 LLVMValueRef args
[2])
2538 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pknorm.u16",
2539 ctx
->v2i16
, args
, 2,
2540 AC_FUNC_ATTR_READNONE
);
2541 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
2544 /* The 8-bit and 10-bit clamping is for HW workarounds. */
2545 LLVMValueRef
ac_build_cvt_pk_i16(struct ac_llvm_context
*ctx
,
2546 LLVMValueRef args
[2], unsigned bits
, bool hi
)
2548 assert(bits
== 8 || bits
== 10 || bits
== 16);
2550 LLVMValueRef max_rgb
= LLVMConstInt(ctx
->i32
,
2551 bits
== 8 ? 127 : bits
== 10 ? 511 : 32767, 0);
2552 LLVMValueRef min_rgb
= LLVMConstInt(ctx
->i32
,
2553 bits
== 8 ? -128 : bits
== 10 ? -512 : -32768, 0);
2554 LLVMValueRef max_alpha
=
2555 bits
!= 10 ? max_rgb
: ctx
->i32_1
;
2556 LLVMValueRef min_alpha
=
2557 bits
!= 10 ? min_rgb
: LLVMConstInt(ctx
->i32
, -2, 0);
2561 for (int i
= 0; i
< 2; i
++) {
2562 bool alpha
= hi
&& i
== 1;
2563 args
[i
] = ac_build_imin(ctx
, args
[i
],
2564 alpha
? max_alpha
: max_rgb
);
2565 args
[i
] = ac_build_imax(ctx
, args
[i
],
2566 alpha
? min_alpha
: min_rgb
);
2571 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pk.i16",
2572 ctx
->v2i16
, args
, 2,
2573 AC_FUNC_ATTR_READNONE
);
2574 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
2577 /* The 8-bit and 10-bit clamping is for HW workarounds. */
2578 LLVMValueRef
ac_build_cvt_pk_u16(struct ac_llvm_context
*ctx
,
2579 LLVMValueRef args
[2], unsigned bits
, bool hi
)
2581 assert(bits
== 8 || bits
== 10 || bits
== 16);
2583 LLVMValueRef max_rgb
= LLVMConstInt(ctx
->i32
,
2584 bits
== 8 ? 255 : bits
== 10 ? 1023 : 65535, 0);
2585 LLVMValueRef max_alpha
=
2586 bits
!= 10 ? max_rgb
: LLVMConstInt(ctx
->i32
, 3, 0);
2590 for (int i
= 0; i
< 2; i
++) {
2591 bool alpha
= hi
&& i
== 1;
2592 args
[i
] = ac_build_umin(ctx
, args
[i
],
2593 alpha
? max_alpha
: max_rgb
);
2598 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pk.u16",
2599 ctx
->v2i16
, args
, 2,
2600 AC_FUNC_ATTR_READNONE
);
2601 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
2604 LLVMValueRef
ac_build_wqm_vote(struct ac_llvm_context
*ctx
, LLVMValueRef i1
)
2606 return ac_build_intrinsic(ctx
, "llvm.amdgcn.wqm.vote", ctx
->i1
,
2607 &i1
, 1, AC_FUNC_ATTR_READNONE
);
2610 void ac_build_kill_if_false(struct ac_llvm_context
*ctx
, LLVMValueRef i1
)
2612 ac_build_intrinsic(ctx
, "llvm.amdgcn.kill", ctx
->voidt
,
2616 LLVMValueRef
ac_build_bfe(struct ac_llvm_context
*ctx
, LLVMValueRef input
,
2617 LLVMValueRef offset
, LLVMValueRef width
,
2620 LLVMValueRef args
[] = {
2626 return ac_build_intrinsic(ctx
, is_signed
? "llvm.amdgcn.sbfe.i32" :
2627 "llvm.amdgcn.ubfe.i32",
2628 ctx
->i32
, args
, 3, AC_FUNC_ATTR_READNONE
);
2632 LLVMValueRef
ac_build_imad(struct ac_llvm_context
*ctx
, LLVMValueRef s0
,
2633 LLVMValueRef s1
, LLVMValueRef s2
)
2635 return LLVMBuildAdd(ctx
->builder
,
2636 LLVMBuildMul(ctx
->builder
, s0
, s1
, ""), s2
, "");
2639 LLVMValueRef
ac_build_fmad(struct ac_llvm_context
*ctx
, LLVMValueRef s0
,
2640 LLVMValueRef s1
, LLVMValueRef s2
)
2642 /* FMA is better on GFX10, because it has FMA units instead of MUL-ADD units. */
2643 if (ctx
->chip_class
>= GFX10
) {
2644 return ac_build_intrinsic(ctx
, "llvm.fma.f32", ctx
->f32
,
2645 (LLVMValueRef
[]) {s0
, s1
, s2
}, 3,
2646 AC_FUNC_ATTR_READNONE
);
2649 return LLVMBuildFAdd(ctx
->builder
,
2650 LLVMBuildFMul(ctx
->builder
, s0
, s1
, ""), s2
, "");
2653 void ac_build_waitcnt(struct ac_llvm_context
*ctx
, unsigned wait_flags
)
2658 unsigned lgkmcnt
= 63;
2659 unsigned vmcnt
= ctx
->chip_class
>= GFX9
? 63 : 15;
2660 unsigned vscnt
= 63;
2662 if (wait_flags
& AC_WAIT_LGKM
)
2664 if (wait_flags
& AC_WAIT_VLOAD
)
2667 if (wait_flags
& AC_WAIT_VSTORE
) {
2668 if (ctx
->chip_class
>= GFX10
)
2674 /* There is no intrinsic for vscnt(0), so use a fence. */
2675 if ((wait_flags
& AC_WAIT_LGKM
&&
2676 wait_flags
& AC_WAIT_VLOAD
&&
2677 wait_flags
& AC_WAIT_VSTORE
) ||
2679 LLVMBuildFence(ctx
->builder
, LLVMAtomicOrderingRelease
, false, "");
2683 unsigned simm16
= (lgkmcnt
<< 8) |
2684 (7 << 4) | /* expcnt */
2686 ((vmcnt
>> 4) << 14);
2688 LLVMValueRef args
[1] = {
2689 LLVMConstInt(ctx
->i32
, simm16
, false),
2691 ac_build_intrinsic(ctx
, "llvm.amdgcn.s.waitcnt",
2692 ctx
->voidt
, args
, 1, 0);
2695 LLVMValueRef
ac_build_fmed3(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
2696 LLVMValueRef src1
, LLVMValueRef src2
,
2702 if (bitsize
== 16) {
2703 intr
= "llvm.amdgcn.fmed3.f16";
2705 } else if (bitsize
== 32) {
2706 intr
= "llvm.amdgcn.fmed3.f32";
2709 intr
= "llvm.amdgcn.fmed3.f64";
2713 LLVMValueRef params
[] = {
2718 return ac_build_intrinsic(ctx
, intr
, type
, params
, 3,
2719 AC_FUNC_ATTR_READNONE
);
2722 LLVMValueRef
ac_build_fract(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
2728 if (bitsize
== 16) {
2729 intr
= "llvm.amdgcn.fract.f16";
2731 } else if (bitsize
== 32) {
2732 intr
= "llvm.amdgcn.fract.f32";
2735 intr
= "llvm.amdgcn.fract.f64";
2739 LLVMValueRef params
[] = {
2742 return ac_build_intrinsic(ctx
, intr
, type
, params
, 1,
2743 AC_FUNC_ATTR_READNONE
);
2746 LLVMValueRef
ac_build_isign(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
2749 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->context
, bitsize
);
2750 LLVMValueRef zero
= LLVMConstInt(type
, 0, false);
2751 LLVMValueRef one
= LLVMConstInt(type
, 1, false);
2753 LLVMValueRef cmp
, val
;
2754 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGT
, src0
, zero
, "");
2755 val
= LLVMBuildSelect(ctx
->builder
, cmp
, one
, src0
, "");
2756 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGE
, val
, zero
, "");
2757 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstInt(type
, -1, true), "");
2761 LLVMValueRef
ac_build_fsign(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
2764 LLVMValueRef cmp
, val
, zero
, one
;
2767 if (bitsize
== 16) {
2771 } else if (bitsize
== 32) {
2781 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGT
, src0
, zero
, "");
2782 val
= LLVMBuildSelect(ctx
->builder
, cmp
, one
, src0
, "");
2783 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGE
, val
, zero
, "");
2784 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstReal(type
, -1.0), "");
2788 LLVMValueRef
ac_build_bit_count(struct ac_llvm_context
*ctx
, LLVMValueRef src0
)
2790 LLVMValueRef result
;
2793 bitsize
= ac_get_elem_bits(ctx
, LLVMTypeOf(src0
));
2797 result
= ac_build_intrinsic(ctx
, "llvm.ctpop.i64", ctx
->i64
,
2798 (LLVMValueRef
[]) { src0
}, 1,
2799 AC_FUNC_ATTR_READNONE
);
2801 result
= LLVMBuildTrunc(ctx
->builder
, result
, ctx
->i32
, "");
2804 result
= ac_build_intrinsic(ctx
, "llvm.ctpop.i32", ctx
->i32
,
2805 (LLVMValueRef
[]) { src0
}, 1,
2806 AC_FUNC_ATTR_READNONE
);
2809 result
= ac_build_intrinsic(ctx
, "llvm.ctpop.i16", ctx
->i16
,
2810 (LLVMValueRef
[]) { src0
}, 1,
2811 AC_FUNC_ATTR_READNONE
);
2813 result
= LLVMBuildZExt(ctx
->builder
, result
, ctx
->i32
, "");
2816 result
= ac_build_intrinsic(ctx
, "llvm.ctpop.i8", ctx
->i8
,
2817 (LLVMValueRef
[]) { src0
}, 1,
2818 AC_FUNC_ATTR_READNONE
);
2820 result
= LLVMBuildZExt(ctx
->builder
, result
, ctx
->i32
, "");
2823 unreachable(!"invalid bitsize");
2830 LLVMValueRef
ac_build_bitfield_reverse(struct ac_llvm_context
*ctx
,
2833 LLVMValueRef result
;
2836 bitsize
= ac_get_elem_bits(ctx
, LLVMTypeOf(src0
));
2840 result
= ac_build_intrinsic(ctx
, "llvm.bitreverse.i64", ctx
->i64
,
2841 (LLVMValueRef
[]) { src0
}, 1,
2842 AC_FUNC_ATTR_READNONE
);
2844 result
= LLVMBuildTrunc(ctx
->builder
, result
, ctx
->i32
, "");
2847 result
= ac_build_intrinsic(ctx
, "llvm.bitreverse.i32", ctx
->i32
,
2848 (LLVMValueRef
[]) { src0
}, 1,
2849 AC_FUNC_ATTR_READNONE
);
2852 result
= ac_build_intrinsic(ctx
, "llvm.bitreverse.i16", ctx
->i16
,
2853 (LLVMValueRef
[]) { src0
}, 1,
2854 AC_FUNC_ATTR_READNONE
);
2856 result
= LLVMBuildZExt(ctx
->builder
, result
, ctx
->i32
, "");
2859 result
= ac_build_intrinsic(ctx
, "llvm.bitreverse.i8", ctx
->i8
,
2860 (LLVMValueRef
[]) { src0
}, 1,
2861 AC_FUNC_ATTR_READNONE
);
2863 result
= LLVMBuildZExt(ctx
->builder
, result
, ctx
->i32
, "");
2866 unreachable(!"invalid bitsize");
2873 #define AC_EXP_TARGET 0
2874 #define AC_EXP_ENABLED_CHANNELS 1
2875 #define AC_EXP_OUT0 2
2883 struct ac_vs_exp_chan
2887 enum ac_ir_type type
;
2890 struct ac_vs_exp_inst
{
2893 struct ac_vs_exp_chan chan
[4];
2896 struct ac_vs_exports
{
2898 struct ac_vs_exp_inst exp
[VARYING_SLOT_MAX
];
2901 /* Return true if the PARAM export has been eliminated. */
2902 static bool ac_eliminate_const_output(uint8_t *vs_output_param_offset
,
2903 uint32_t num_outputs
,
2904 struct ac_vs_exp_inst
*exp
)
2906 unsigned i
, default_val
; /* SPI_PS_INPUT_CNTL_i.DEFAULT_VAL */
2907 bool is_zero
[4] = {}, is_one
[4] = {};
2909 for (i
= 0; i
< 4; i
++) {
2910 /* It's a constant expression. Undef outputs are eliminated too. */
2911 if (exp
->chan
[i
].type
== AC_IR_UNDEF
) {
2914 } else if (exp
->chan
[i
].type
== AC_IR_CONST
) {
2915 if (exp
->chan
[i
].const_float
== 0)
2917 else if (exp
->chan
[i
].const_float
== 1)
2920 return false; /* other constant */
2925 /* Only certain combinations of 0 and 1 can be eliminated. */
2926 if (is_zero
[0] && is_zero
[1] && is_zero
[2])
2927 default_val
= is_zero
[3] ? 0 : 1;
2928 else if (is_one
[0] && is_one
[1] && is_one
[2])
2929 default_val
= is_zero
[3] ? 2 : 3;
2933 /* The PARAM export can be represented as DEFAULT_VAL. Kill it. */
2934 LLVMInstructionEraseFromParent(exp
->inst
);
2936 /* Change OFFSET to DEFAULT_VAL. */
2937 for (i
= 0; i
< num_outputs
; i
++) {
2938 if (vs_output_param_offset
[i
] == exp
->offset
) {
2939 vs_output_param_offset
[i
] =
2940 AC_EXP_PARAM_DEFAULT_VAL_0000
+ default_val
;
2947 static bool ac_eliminate_duplicated_output(struct ac_llvm_context
*ctx
,
2948 uint8_t *vs_output_param_offset
,
2949 uint32_t num_outputs
,
2950 struct ac_vs_exports
*processed
,
2951 struct ac_vs_exp_inst
*exp
)
2953 unsigned p
, copy_back_channels
= 0;
2955 /* See if the output is already in the list of processed outputs.
2956 * The LLVMValueRef comparison relies on SSA.
2958 for (p
= 0; p
< processed
->num
; p
++) {
2959 bool different
= false;
2961 for (unsigned j
= 0; j
< 4; j
++) {
2962 struct ac_vs_exp_chan
*c1
= &processed
->exp
[p
].chan
[j
];
2963 struct ac_vs_exp_chan
*c2
= &exp
->chan
[j
];
2965 /* Treat undef as a match. */
2966 if (c2
->type
== AC_IR_UNDEF
)
2969 /* If c1 is undef but c2 isn't, we can copy c2 to c1
2970 * and consider the instruction duplicated.
2972 if (c1
->type
== AC_IR_UNDEF
) {
2973 copy_back_channels
|= 1 << j
;
2977 /* Test whether the channels are not equal. */
2978 if (c1
->type
!= c2
->type
||
2979 (c1
->type
== AC_IR_CONST
&&
2980 c1
->const_float
!= c2
->const_float
) ||
2981 (c1
->type
== AC_IR_VALUE
&&
2982 c1
->value
!= c2
->value
)) {
2990 copy_back_channels
= 0;
2992 if (p
== processed
->num
)
2995 /* If a match was found, but the matching export has undef where the new
2996 * one has a normal value, copy the normal value to the undef channel.
2998 struct ac_vs_exp_inst
*match
= &processed
->exp
[p
];
3000 /* Get current enabled channels mask. */
3001 LLVMValueRef arg
= LLVMGetOperand(match
->inst
, AC_EXP_ENABLED_CHANNELS
);
3002 unsigned enabled_channels
= LLVMConstIntGetZExtValue(arg
);
3004 while (copy_back_channels
) {
3005 unsigned chan
= u_bit_scan(©_back_channels
);
3007 assert(match
->chan
[chan
].type
== AC_IR_UNDEF
);
3008 LLVMSetOperand(match
->inst
, AC_EXP_OUT0
+ chan
,
3009 exp
->chan
[chan
].value
);
3010 match
->chan
[chan
] = exp
->chan
[chan
];
3012 /* Update number of enabled channels because the original mask
3013 * is not always 0xf.
3015 enabled_channels
|= (1 << chan
);
3016 LLVMSetOperand(match
->inst
, AC_EXP_ENABLED_CHANNELS
,
3017 LLVMConstInt(ctx
->i32
, enabled_channels
, 0));
3020 /* The PARAM export is duplicated. Kill it. */
3021 LLVMInstructionEraseFromParent(exp
->inst
);
3023 /* Change OFFSET to the matching export. */
3024 for (unsigned i
= 0; i
< num_outputs
; i
++) {
3025 if (vs_output_param_offset
[i
] == exp
->offset
) {
3026 vs_output_param_offset
[i
] = match
->offset
;
3033 void ac_optimize_vs_outputs(struct ac_llvm_context
*ctx
,
3034 LLVMValueRef main_fn
,
3035 uint8_t *vs_output_param_offset
,
3036 uint32_t num_outputs
,
3037 uint8_t *num_param_exports
)
3039 LLVMBasicBlockRef bb
;
3040 bool removed_any
= false;
3041 struct ac_vs_exports exports
;
3045 /* Process all LLVM instructions. */
3046 bb
= LLVMGetFirstBasicBlock(main_fn
);
3048 LLVMValueRef inst
= LLVMGetFirstInstruction(bb
);
3051 LLVMValueRef cur
= inst
;
3052 inst
= LLVMGetNextInstruction(inst
);
3053 struct ac_vs_exp_inst exp
;
3055 if (LLVMGetInstructionOpcode(cur
) != LLVMCall
)
3058 LLVMValueRef callee
= ac_llvm_get_called_value(cur
);
3060 if (!ac_llvm_is_function(callee
))
3063 const char *name
= LLVMGetValueName(callee
);
3064 unsigned num_args
= LLVMCountParams(callee
);
3066 /* Check if this is an export instruction. */
3067 if ((num_args
!= 9 && num_args
!= 8) ||
3068 (strcmp(name
, "llvm.SI.export") &&
3069 strcmp(name
, "llvm.amdgcn.exp.f32")))
3072 LLVMValueRef arg
= LLVMGetOperand(cur
, AC_EXP_TARGET
);
3073 unsigned target
= LLVMConstIntGetZExtValue(arg
);
3075 if (target
< V_008DFC_SQ_EXP_PARAM
)
3078 target
-= V_008DFC_SQ_EXP_PARAM
;
3080 /* Parse the instruction. */
3081 memset(&exp
, 0, sizeof(exp
));
3082 exp
.offset
= target
;
3085 for (unsigned i
= 0; i
< 4; i
++) {
3086 LLVMValueRef v
= LLVMGetOperand(cur
, AC_EXP_OUT0
+ i
);
3088 exp
.chan
[i
].value
= v
;
3090 if (LLVMIsUndef(v
)) {
3091 exp
.chan
[i
].type
= AC_IR_UNDEF
;
3092 } else if (LLVMIsAConstantFP(v
)) {
3093 LLVMBool loses_info
;
3094 exp
.chan
[i
].type
= AC_IR_CONST
;
3095 exp
.chan
[i
].const_float
=
3096 LLVMConstRealGetDouble(v
, &loses_info
);
3098 exp
.chan
[i
].type
= AC_IR_VALUE
;
3102 /* Eliminate constant and duplicated PARAM exports. */
3103 if (ac_eliminate_const_output(vs_output_param_offset
,
3104 num_outputs
, &exp
) ||
3105 ac_eliminate_duplicated_output(ctx
,
3106 vs_output_param_offset
,
3107 num_outputs
, &exports
,
3111 exports
.exp
[exports
.num
++] = exp
;
3114 bb
= LLVMGetNextBasicBlock(bb
);
3117 /* Remove holes in export memory due to removed PARAM exports.
3118 * This is done by renumbering all PARAM exports.
3121 uint8_t old_offset
[VARYING_SLOT_MAX
];
3124 /* Make a copy of the offsets. We need the old version while
3125 * we are modifying some of them. */
3126 memcpy(old_offset
, vs_output_param_offset
,
3127 sizeof(old_offset
));
3129 for (i
= 0; i
< exports
.num
; i
++) {
3130 unsigned offset
= exports
.exp
[i
].offset
;
3132 /* Update vs_output_param_offset. Multiple outputs can
3133 * have the same offset.
3135 for (out
= 0; out
< num_outputs
; out
++) {
3136 if (old_offset
[out
] == offset
)
3137 vs_output_param_offset
[out
] = i
;
3140 /* Change the PARAM offset in the instruction. */
3141 LLVMSetOperand(exports
.exp
[i
].inst
, AC_EXP_TARGET
,
3142 LLVMConstInt(ctx
->i32
,
3143 V_008DFC_SQ_EXP_PARAM
+ i
, 0));
3145 *num_param_exports
= exports
.num
;
3149 void ac_init_exec_full_mask(struct ac_llvm_context
*ctx
)
3151 LLVMValueRef full_mask
= LLVMConstInt(ctx
->i64
, ~0ull, 0);
3152 ac_build_intrinsic(ctx
,
3153 "llvm.amdgcn.init.exec", ctx
->voidt
,
3154 &full_mask
, 1, AC_FUNC_ATTR_CONVERGENT
);
3157 void ac_declare_lds_as_pointer(struct ac_llvm_context
*ctx
)
3159 unsigned lds_size
= ctx
->chip_class
>= GFX7
? 65536 : 32768;
3160 ctx
->lds
= LLVMBuildIntToPtr(ctx
->builder
, ctx
->i32_0
,
3161 LLVMPointerType(LLVMArrayType(ctx
->i32
, lds_size
/ 4), AC_ADDR_SPACE_LDS
),
3165 LLVMValueRef
ac_lds_load(struct ac_llvm_context
*ctx
,
3166 LLVMValueRef dw_addr
)
3168 return LLVMBuildLoad(ctx
->builder
, ac_build_gep0(ctx
, ctx
->lds
, dw_addr
), "");
3171 void ac_lds_store(struct ac_llvm_context
*ctx
,
3172 LLVMValueRef dw_addr
,
3175 value
= ac_to_integer(ctx
, value
);
3176 ac_build_indexed_store(ctx
, ctx
->lds
,
3180 LLVMValueRef
ac_find_lsb(struct ac_llvm_context
*ctx
,
3181 LLVMTypeRef dst_type
,
3184 unsigned src0_bitsize
= ac_get_elem_bits(ctx
, LLVMTypeOf(src0
));
3185 const char *intrin_name
;
3189 switch (src0_bitsize
) {
3191 intrin_name
= "llvm.cttz.i64";
3196 intrin_name
= "llvm.cttz.i32";
3201 intrin_name
= "llvm.cttz.i16";
3206 intrin_name
= "llvm.cttz.i8";
3211 unreachable(!"invalid bitsize");
3214 LLVMValueRef params
[2] = {
3217 /* The value of 1 means that ffs(x=0) = undef, so LLVM won't
3218 * add special code to check for x=0. The reason is that
3219 * the LLVM behavior for x=0 is different from what we
3220 * need here. However, LLVM also assumes that ffs(x) is
3221 * in [0, 31], but GLSL expects that ffs(0) = -1, so
3222 * a conditional assignment to handle 0 is still required.
3224 * The hardware already implements the correct behavior.
3229 LLVMValueRef lsb
= ac_build_intrinsic(ctx
, intrin_name
, type
,
3231 AC_FUNC_ATTR_READNONE
);
3233 if (src0_bitsize
== 64) {
3234 lsb
= LLVMBuildTrunc(ctx
->builder
, lsb
, ctx
->i32
, "");
3235 } else if (src0_bitsize
< 32) {
3236 lsb
= LLVMBuildSExt(ctx
->builder
, lsb
, ctx
->i32
, "");
3239 /* TODO: We need an intrinsic to skip this conditional. */
3240 /* Check for zero: */
3241 return LLVMBuildSelect(ctx
->builder
, LLVMBuildICmp(ctx
->builder
,
3244 LLVMConstInt(ctx
->i32
, -1, 0), lsb
, "");
3247 LLVMTypeRef
ac_array_in_const_addr_space(LLVMTypeRef elem_type
)
3249 return LLVMPointerType(elem_type
, AC_ADDR_SPACE_CONST
);
3252 LLVMTypeRef
ac_array_in_const32_addr_space(LLVMTypeRef elem_type
)
3254 return LLVMPointerType(elem_type
, AC_ADDR_SPACE_CONST_32BIT
);
3257 static struct ac_llvm_flow
*
3258 get_current_flow(struct ac_llvm_context
*ctx
)
3260 if (ctx
->flow
->depth
> 0)
3261 return &ctx
->flow
->stack
[ctx
->flow
->depth
- 1];
3265 static struct ac_llvm_flow
*
3266 get_innermost_loop(struct ac_llvm_context
*ctx
)
3268 for (unsigned i
= ctx
->flow
->depth
; i
> 0; --i
) {
3269 if (ctx
->flow
->stack
[i
- 1].loop_entry_block
)
3270 return &ctx
->flow
->stack
[i
- 1];
3275 static struct ac_llvm_flow
*
3276 push_flow(struct ac_llvm_context
*ctx
)
3278 struct ac_llvm_flow
*flow
;
3280 if (ctx
->flow
->depth
>= ctx
->flow
->depth_max
) {
3281 unsigned new_max
= MAX2(ctx
->flow
->depth
<< 1,
3282 AC_LLVM_INITIAL_CF_DEPTH
);
3284 ctx
->flow
->stack
= realloc(ctx
->flow
->stack
, new_max
* sizeof(*ctx
->flow
->stack
));
3285 ctx
->flow
->depth_max
= new_max
;
3288 flow
= &ctx
->flow
->stack
[ctx
->flow
->depth
];
3291 flow
->next_block
= NULL
;
3292 flow
->loop_entry_block
= NULL
;
3296 static void set_basicblock_name(LLVMBasicBlockRef bb
, const char *base
,
3300 snprintf(buf
, sizeof(buf
), "%s%d", base
, label_id
);
3301 LLVMSetValueName(LLVMBasicBlockAsValue(bb
), buf
);
3304 /* Append a basic block at the level of the parent flow.
3306 static LLVMBasicBlockRef
append_basic_block(struct ac_llvm_context
*ctx
,
3309 assert(ctx
->flow
->depth
>= 1);
3311 if (ctx
->flow
->depth
>= 2) {
3312 struct ac_llvm_flow
*flow
= &ctx
->flow
->stack
[ctx
->flow
->depth
- 2];
3314 return LLVMInsertBasicBlockInContext(ctx
->context
,
3315 flow
->next_block
, name
);
3318 LLVMValueRef main_fn
=
3319 LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
->builder
));
3320 return LLVMAppendBasicBlockInContext(ctx
->context
, main_fn
, name
);
3323 /* Emit a branch to the given default target for the current block if
3324 * applicable -- that is, if the current block does not already contain a
3325 * branch from a break or continue.
3327 static void emit_default_branch(LLVMBuilderRef builder
,
3328 LLVMBasicBlockRef target
)
3330 if (!LLVMGetBasicBlockTerminator(LLVMGetInsertBlock(builder
)))
3331 LLVMBuildBr(builder
, target
);
3334 void ac_build_bgnloop(struct ac_llvm_context
*ctx
, int label_id
)
3336 struct ac_llvm_flow
*flow
= push_flow(ctx
);
3337 flow
->loop_entry_block
= append_basic_block(ctx
, "LOOP");
3338 flow
->next_block
= append_basic_block(ctx
, "ENDLOOP");
3339 set_basicblock_name(flow
->loop_entry_block
, "loop", label_id
);
3340 LLVMBuildBr(ctx
->builder
, flow
->loop_entry_block
);
3341 LLVMPositionBuilderAtEnd(ctx
->builder
, flow
->loop_entry_block
);
3344 void ac_build_break(struct ac_llvm_context
*ctx
)
3346 struct ac_llvm_flow
*flow
= get_innermost_loop(ctx
);
3347 LLVMBuildBr(ctx
->builder
, flow
->next_block
);
3350 void ac_build_continue(struct ac_llvm_context
*ctx
)
3352 struct ac_llvm_flow
*flow
= get_innermost_loop(ctx
);
3353 LLVMBuildBr(ctx
->builder
, flow
->loop_entry_block
);
3356 void ac_build_else(struct ac_llvm_context
*ctx
, int label_id
)
3358 struct ac_llvm_flow
*current_branch
= get_current_flow(ctx
);
3359 LLVMBasicBlockRef endif_block
;
3361 assert(!current_branch
->loop_entry_block
);
3363 endif_block
= append_basic_block(ctx
, "ENDIF");
3364 emit_default_branch(ctx
->builder
, endif_block
);
3366 LLVMPositionBuilderAtEnd(ctx
->builder
, current_branch
->next_block
);
3367 set_basicblock_name(current_branch
->next_block
, "else", label_id
);
3369 current_branch
->next_block
= endif_block
;
3372 void ac_build_endif(struct ac_llvm_context
*ctx
, int label_id
)
3374 struct ac_llvm_flow
*current_branch
= get_current_flow(ctx
);
3376 assert(!current_branch
->loop_entry_block
);
3378 emit_default_branch(ctx
->builder
, current_branch
->next_block
);
3379 LLVMPositionBuilderAtEnd(ctx
->builder
, current_branch
->next_block
);
3380 set_basicblock_name(current_branch
->next_block
, "endif", label_id
);
3385 void ac_build_endloop(struct ac_llvm_context
*ctx
, int label_id
)
3387 struct ac_llvm_flow
*current_loop
= get_current_flow(ctx
);
3389 assert(current_loop
->loop_entry_block
);
3391 emit_default_branch(ctx
->builder
, current_loop
->loop_entry_block
);
3393 LLVMPositionBuilderAtEnd(ctx
->builder
, current_loop
->next_block
);
3394 set_basicblock_name(current_loop
->next_block
, "endloop", label_id
);
3398 void ac_build_ifcc(struct ac_llvm_context
*ctx
, LLVMValueRef cond
, int label_id
)
3400 struct ac_llvm_flow
*flow
= push_flow(ctx
);
3401 LLVMBasicBlockRef if_block
;
3403 if_block
= append_basic_block(ctx
, "IF");
3404 flow
->next_block
= append_basic_block(ctx
, "ELSE");
3405 set_basicblock_name(if_block
, "if", label_id
);
3406 LLVMBuildCondBr(ctx
->builder
, cond
, if_block
, flow
->next_block
);
3407 LLVMPositionBuilderAtEnd(ctx
->builder
, if_block
);
3410 void ac_build_if(struct ac_llvm_context
*ctx
, LLVMValueRef value
,
3413 LLVMValueRef cond
= LLVMBuildFCmp(ctx
->builder
, LLVMRealUNE
,
3414 value
, ctx
->f32_0
, "");
3415 ac_build_ifcc(ctx
, cond
, label_id
);
3418 void ac_build_uif(struct ac_llvm_context
*ctx
, LLVMValueRef value
,
3421 LLVMValueRef cond
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
3422 ac_to_integer(ctx
, value
),
3424 ac_build_ifcc(ctx
, cond
, label_id
);
3427 LLVMValueRef
ac_build_alloca_undef(struct ac_llvm_context
*ac
, LLVMTypeRef type
,
3430 LLVMBuilderRef builder
= ac
->builder
;
3431 LLVMBasicBlockRef current_block
= LLVMGetInsertBlock(builder
);
3432 LLVMValueRef function
= LLVMGetBasicBlockParent(current_block
);
3433 LLVMBasicBlockRef first_block
= LLVMGetEntryBasicBlock(function
);
3434 LLVMValueRef first_instr
= LLVMGetFirstInstruction(first_block
);
3435 LLVMBuilderRef first_builder
= LLVMCreateBuilderInContext(ac
->context
);
3439 LLVMPositionBuilderBefore(first_builder
, first_instr
);
3441 LLVMPositionBuilderAtEnd(first_builder
, first_block
);
3444 res
= LLVMBuildAlloca(first_builder
, type
, name
);
3445 LLVMDisposeBuilder(first_builder
);
3449 LLVMValueRef
ac_build_alloca(struct ac_llvm_context
*ac
,
3450 LLVMTypeRef type
, const char *name
)
3452 LLVMValueRef ptr
= ac_build_alloca_undef(ac
, type
, name
);
3453 LLVMBuildStore(ac
->builder
, LLVMConstNull(type
), ptr
);
3457 LLVMValueRef
ac_cast_ptr(struct ac_llvm_context
*ctx
, LLVMValueRef ptr
,
3460 int addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
3461 return LLVMBuildBitCast(ctx
->builder
, ptr
,
3462 LLVMPointerType(type
, addr_space
), "");
3465 LLVMValueRef
ac_trim_vector(struct ac_llvm_context
*ctx
, LLVMValueRef value
,
3468 unsigned num_components
= ac_get_llvm_num_components(value
);
3469 if (count
== num_components
)
3472 LLVMValueRef masks
[MAX2(count
, 2)];
3473 masks
[0] = ctx
->i32_0
;
3474 masks
[1] = ctx
->i32_1
;
3475 for (unsigned i
= 2; i
< count
; i
++)
3476 masks
[i
] = LLVMConstInt(ctx
->i32
, i
, false);
3479 return LLVMBuildExtractElement(ctx
->builder
, value
, masks
[0],
3482 LLVMValueRef swizzle
= LLVMConstVector(masks
, count
);
3483 return LLVMBuildShuffleVector(ctx
->builder
, value
, value
, swizzle
, "");
3486 LLVMValueRef
ac_unpack_param(struct ac_llvm_context
*ctx
, LLVMValueRef param
,
3487 unsigned rshift
, unsigned bitwidth
)
3489 LLVMValueRef value
= param
;
3491 value
= LLVMBuildLShr(ctx
->builder
, value
,
3492 LLVMConstInt(ctx
->i32
, rshift
, false), "");
3494 if (rshift
+ bitwidth
< 32) {
3495 unsigned mask
= (1 << bitwidth
) - 1;
3496 value
= LLVMBuildAnd(ctx
->builder
, value
,
3497 LLVMConstInt(ctx
->i32
, mask
, false), "");
3502 /* Adjust the sample index according to FMASK.
3504 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
3505 * which is the identity mapping. Each nibble says which physical sample
3506 * should be fetched to get that sample.
3508 * For example, 0x11111100 means there are only 2 samples stored and
3509 * the second sample covers 3/4 of the pixel. When reading samples 0
3510 * and 1, return physical sample 0 (determined by the first two 0s
3511 * in FMASK), otherwise return physical sample 1.
3513 * The sample index should be adjusted as follows:
3514 * addr[sample_index] = (fmask >> (addr[sample_index] * 4)) & 0xF;
3516 void ac_apply_fmask_to_sample(struct ac_llvm_context
*ac
, LLVMValueRef fmask
,
3517 LLVMValueRef
*addr
, bool is_array_tex
)
3519 struct ac_image_args fmask_load
= {};
3520 fmask_load
.opcode
= ac_image_load
;
3521 fmask_load
.resource
= fmask
;
3522 fmask_load
.dmask
= 0xf;
3523 fmask_load
.dim
= is_array_tex
? ac_image_2darray
: ac_image_2d
;
3524 fmask_load
.attributes
= AC_FUNC_ATTR_READNONE
;
3526 fmask_load
.coords
[0] = addr
[0];
3527 fmask_load
.coords
[1] = addr
[1];
3529 fmask_load
.coords
[2] = addr
[2];
3531 LLVMValueRef fmask_value
= ac_build_image_opcode(ac
, &fmask_load
);
3532 fmask_value
= LLVMBuildExtractElement(ac
->builder
, fmask_value
,
3535 /* Apply the formula. */
3536 unsigned sample_chan
= is_array_tex
? 3 : 2;
3537 LLVMValueRef final_sample
;
3538 final_sample
= LLVMBuildMul(ac
->builder
, addr
[sample_chan
],
3539 LLVMConstInt(ac
->i32
, 4, 0), "");
3540 final_sample
= LLVMBuildLShr(ac
->builder
, fmask_value
, final_sample
, "");
3541 /* Mask the sample index by 0x7, because 0x8 means an unknown value
3542 * with EQAA, so those will map to 0. */
3543 final_sample
= LLVMBuildAnd(ac
->builder
, final_sample
,
3544 LLVMConstInt(ac
->i32
, 0x7, 0), "");
3546 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
3547 * resource descriptor is 0 (invalid).
3550 tmp
= LLVMBuildBitCast(ac
->builder
, fmask
, ac
->v8i32
, "");
3551 tmp
= LLVMBuildExtractElement(ac
->builder
, tmp
, ac
->i32_1
, "");
3552 tmp
= LLVMBuildICmp(ac
->builder
, LLVMIntNE
, tmp
, ac
->i32_0
, "");
3554 /* Replace the MSAA sample index. */
3555 addr
[sample_chan
] = LLVMBuildSelect(ac
->builder
, tmp
, final_sample
,
3556 addr
[sample_chan
], "");
3560 _ac_build_readlane(struct ac_llvm_context
*ctx
, LLVMValueRef src
, LLVMValueRef lane
)
3562 ac_build_optimization_barrier(ctx
, &src
);
3563 return ac_build_intrinsic(ctx
,
3564 lane
== NULL
? "llvm.amdgcn.readfirstlane" : "llvm.amdgcn.readlane",
3565 LLVMTypeOf(src
), (LLVMValueRef
[]) {
3567 lane
== NULL
? 1 : 2,
3568 AC_FUNC_ATTR_READNONE
|
3569 AC_FUNC_ATTR_CONVERGENT
);
3573 * Builds the "llvm.amdgcn.readlane" or "llvm.amdgcn.readfirstlane" intrinsic.
3576 * @param lane - id of the lane or NULL for the first active lane
3577 * @return value of the lane
3580 ac_build_readlane(struct ac_llvm_context
*ctx
, LLVMValueRef src
, LLVMValueRef lane
)
3582 LLVMTypeRef src_type
= LLVMTypeOf(src
);
3583 src
= ac_to_integer(ctx
, src
);
3584 unsigned bits
= LLVMGetIntTypeWidth(LLVMTypeOf(src
));
3588 ret
= _ac_build_readlane(ctx
, src
, lane
);
3590 assert(bits
% 32 == 0);
3591 LLVMTypeRef vec_type
= LLVMVectorType(ctx
->i32
, bits
/ 32);
3592 LLVMValueRef src_vector
=
3593 LLVMBuildBitCast(ctx
->builder
, src
, vec_type
, "");
3594 ret
= LLVMGetUndef(vec_type
);
3595 for (unsigned i
= 0; i
< bits
/ 32; i
++) {
3596 src
= LLVMBuildExtractElement(ctx
->builder
, src_vector
,
3597 LLVMConstInt(ctx
->i32
, i
, 0), "");
3598 LLVMValueRef ret_comp
= _ac_build_readlane(ctx
, src
, lane
);
3599 ret
= LLVMBuildInsertElement(ctx
->builder
, ret
, ret_comp
,
3600 LLVMConstInt(ctx
->i32
, i
, 0), "");
3603 if (LLVMGetTypeKind(src_type
) == LLVMPointerTypeKind
)
3604 return LLVMBuildIntToPtr(ctx
->builder
, ret
, src_type
, "");
3605 return LLVMBuildBitCast(ctx
->builder
, ret
, src_type
, "");
3609 ac_build_writelane(struct ac_llvm_context
*ctx
, LLVMValueRef src
, LLVMValueRef value
, LLVMValueRef lane
)
3611 return ac_build_intrinsic(ctx
, "llvm.amdgcn.writelane", ctx
->i32
,
3612 (LLVMValueRef
[]) {value
, lane
, src
}, 3,
3613 AC_FUNC_ATTR_READNONE
| AC_FUNC_ATTR_CONVERGENT
);
3617 ac_build_mbcnt(struct ac_llvm_context
*ctx
, LLVMValueRef mask
)
3619 if (ctx
->wave_size
== 32) {
3620 return ac_build_intrinsic(ctx
, "llvm.amdgcn.mbcnt.lo", ctx
->i32
,
3621 (LLVMValueRef
[]) { mask
, ctx
->i32_0
},
3622 2, AC_FUNC_ATTR_READNONE
);
3624 LLVMValueRef mask_vec
= LLVMBuildBitCast(ctx
->builder
, mask
,
3625 LLVMVectorType(ctx
->i32
, 2),
3627 LLVMValueRef mask_lo
= LLVMBuildExtractElement(ctx
->builder
, mask_vec
,
3629 LLVMValueRef mask_hi
= LLVMBuildExtractElement(ctx
->builder
, mask_vec
,
3632 ac_build_intrinsic(ctx
, "llvm.amdgcn.mbcnt.lo", ctx
->i32
,
3633 (LLVMValueRef
[]) { mask_lo
, ctx
->i32_0
},
3634 2, AC_FUNC_ATTR_READNONE
);
3635 val
= ac_build_intrinsic(ctx
, "llvm.amdgcn.mbcnt.hi", ctx
->i32
,
3636 (LLVMValueRef
[]) { mask_hi
, val
},
3637 2, AC_FUNC_ATTR_READNONE
);
3642 _dpp_quad_perm
= 0x000,
3643 _dpp_row_sl
= 0x100,
3644 _dpp_row_sr
= 0x110,
3645 _dpp_row_rr
= 0x120,
3650 dpp_row_mirror
= 0x140,
3651 dpp_row_half_mirror
= 0x141,
3652 dpp_row_bcast15
= 0x142,
3653 dpp_row_bcast31
= 0x143
3656 static inline enum dpp_ctrl
3657 dpp_quad_perm(unsigned lane0
, unsigned lane1
, unsigned lane2
, unsigned lane3
)
3659 assert(lane0
< 4 && lane1
< 4 && lane2
< 4 && lane3
< 4);
3660 return _dpp_quad_perm
| lane0
| (lane1
<< 2) | (lane2
<< 4) | (lane3
<< 6);
3663 static inline enum dpp_ctrl
3664 dpp_row_sl(unsigned amount
)
3666 assert(amount
> 0 && amount
< 16);
3667 return _dpp_row_sl
| amount
;
3670 static inline enum dpp_ctrl
3671 dpp_row_sr(unsigned amount
)
3673 assert(amount
> 0 && amount
< 16);
3674 return _dpp_row_sr
| amount
;
3678 _ac_build_dpp(struct ac_llvm_context
*ctx
, LLVMValueRef old
, LLVMValueRef src
,
3679 enum dpp_ctrl dpp_ctrl
, unsigned row_mask
, unsigned bank_mask
,
3682 return ac_build_intrinsic(ctx
, "llvm.amdgcn.update.dpp.i32",
3686 LLVMConstInt(ctx
->i32
, dpp_ctrl
, 0),
3687 LLVMConstInt(ctx
->i32
, row_mask
, 0),
3688 LLVMConstInt(ctx
->i32
, bank_mask
, 0),
3689 LLVMConstInt(ctx
->i1
, bound_ctrl
, 0) },
3690 6, AC_FUNC_ATTR_READNONE
| AC_FUNC_ATTR_CONVERGENT
);
3694 ac_build_dpp(struct ac_llvm_context
*ctx
, LLVMValueRef old
, LLVMValueRef src
,
3695 enum dpp_ctrl dpp_ctrl
, unsigned row_mask
, unsigned bank_mask
,
3698 LLVMTypeRef src_type
= LLVMTypeOf(src
);
3699 src
= ac_to_integer(ctx
, src
);
3700 old
= ac_to_integer(ctx
, old
);
3701 unsigned bits
= LLVMGetIntTypeWidth(LLVMTypeOf(src
));
3704 ret
= _ac_build_dpp(ctx
, old
, src
, dpp_ctrl
, row_mask
,
3705 bank_mask
, bound_ctrl
);
3707 assert(bits
% 32 == 0);
3708 LLVMTypeRef vec_type
= LLVMVectorType(ctx
->i32
, bits
/ 32);
3709 LLVMValueRef src_vector
=
3710 LLVMBuildBitCast(ctx
->builder
, src
, vec_type
, "");
3711 LLVMValueRef old_vector
=
3712 LLVMBuildBitCast(ctx
->builder
, old
, vec_type
, "");
3713 ret
= LLVMGetUndef(vec_type
);
3714 for (unsigned i
= 0; i
< bits
/ 32; i
++) {
3715 src
= LLVMBuildExtractElement(ctx
->builder
, src_vector
,
3716 LLVMConstInt(ctx
->i32
, i
,
3718 old
= LLVMBuildExtractElement(ctx
->builder
, old_vector
,
3719 LLVMConstInt(ctx
->i32
, i
,
3721 LLVMValueRef ret_comp
= _ac_build_dpp(ctx
, old
, src
,
3726 ret
= LLVMBuildInsertElement(ctx
->builder
, ret
,
3728 LLVMConstInt(ctx
->i32
, i
,
3732 return LLVMBuildBitCast(ctx
->builder
, ret
, src_type
, "");
3736 _ac_build_permlane16(struct ac_llvm_context
*ctx
, LLVMValueRef src
, uint64_t sel
,
3737 bool exchange_rows
, bool bound_ctrl
)
3739 LLVMValueRef args
[6] = {
3742 LLVMConstInt(ctx
->i32
, sel
, false),
3743 LLVMConstInt(ctx
->i32
, sel
>> 32, false),
3744 ctx
->i1true
, /* fi */
3745 bound_ctrl
? ctx
->i1true
: ctx
->i1false
,
3747 return ac_build_intrinsic(ctx
, exchange_rows
? "llvm.amdgcn.permlanex16"
3748 : "llvm.amdgcn.permlane16",
3750 AC_FUNC_ATTR_READNONE
| AC_FUNC_ATTR_CONVERGENT
);
3754 ac_build_permlane16(struct ac_llvm_context
*ctx
, LLVMValueRef src
, uint64_t sel
,
3755 bool exchange_rows
, bool bound_ctrl
)
3757 LLVMTypeRef src_type
= LLVMTypeOf(src
);
3758 src
= ac_to_integer(ctx
, src
);
3759 unsigned bits
= LLVMGetIntTypeWidth(LLVMTypeOf(src
));
3762 ret
= _ac_build_permlane16(ctx
, src
, sel
, exchange_rows
,
3765 assert(bits
% 32 == 0);
3766 LLVMTypeRef vec_type
= LLVMVectorType(ctx
->i32
, bits
/ 32);
3767 LLVMValueRef src_vector
=
3768 LLVMBuildBitCast(ctx
->builder
, src
, vec_type
, "");
3769 ret
= LLVMGetUndef(vec_type
);
3770 for (unsigned i
= 0; i
< bits
/ 32; i
++) {
3771 src
= LLVMBuildExtractElement(ctx
->builder
, src_vector
,
3772 LLVMConstInt(ctx
->i32
, i
,
3774 LLVMValueRef ret_comp
=
3775 _ac_build_permlane16(ctx
, src
, sel
,
3778 ret
= LLVMBuildInsertElement(ctx
->builder
, ret
,
3780 LLVMConstInt(ctx
->i32
, i
,
3784 return LLVMBuildBitCast(ctx
->builder
, ret
, src_type
, "");
3787 static inline unsigned
3788 ds_pattern_bitmode(unsigned and_mask
, unsigned or_mask
, unsigned xor_mask
)
3790 assert(and_mask
< 32 && or_mask
< 32 && xor_mask
< 32);
3791 return and_mask
| (or_mask
<< 5) | (xor_mask
<< 10);
3795 _ac_build_ds_swizzle(struct ac_llvm_context
*ctx
, LLVMValueRef src
, unsigned mask
)
3797 return ac_build_intrinsic(ctx
, "llvm.amdgcn.ds.swizzle",
3798 LLVMTypeOf(src
), (LLVMValueRef
[]) {
3799 src
, LLVMConstInt(ctx
->i32
, mask
, 0) },
3800 2, AC_FUNC_ATTR_READNONE
| AC_FUNC_ATTR_CONVERGENT
);
3804 ac_build_ds_swizzle(struct ac_llvm_context
*ctx
, LLVMValueRef src
, unsigned mask
)
3806 LLVMTypeRef src_type
= LLVMTypeOf(src
);
3807 src
= ac_to_integer(ctx
, src
);
3808 unsigned bits
= LLVMGetIntTypeWidth(LLVMTypeOf(src
));
3811 ret
= _ac_build_ds_swizzle(ctx
, src
, mask
);
3813 assert(bits
% 32 == 0);
3814 LLVMTypeRef vec_type
= LLVMVectorType(ctx
->i32
, bits
/ 32);
3815 LLVMValueRef src_vector
=
3816 LLVMBuildBitCast(ctx
->builder
, src
, vec_type
, "");
3817 ret
= LLVMGetUndef(vec_type
);
3818 for (unsigned i
= 0; i
< bits
/ 32; i
++) {
3819 src
= LLVMBuildExtractElement(ctx
->builder
, src_vector
,
3820 LLVMConstInt(ctx
->i32
, i
,
3822 LLVMValueRef ret_comp
= _ac_build_ds_swizzle(ctx
, src
,
3824 ret
= LLVMBuildInsertElement(ctx
->builder
, ret
,
3826 LLVMConstInt(ctx
->i32
, i
,
3830 return LLVMBuildBitCast(ctx
->builder
, ret
, src_type
, "");
3834 ac_build_wwm(struct ac_llvm_context
*ctx
, LLVMValueRef src
)
3836 char name
[32], type
[8];
3837 ac_build_type_name_for_intr(LLVMTypeOf(src
), type
, sizeof(type
));
3838 snprintf(name
, sizeof(name
), "llvm.amdgcn.wwm.%s", type
);
3839 return ac_build_intrinsic(ctx
, name
, LLVMTypeOf(src
),
3840 (LLVMValueRef
[]) { src
}, 1,
3841 AC_FUNC_ATTR_READNONE
);
3845 ac_build_set_inactive(struct ac_llvm_context
*ctx
, LLVMValueRef src
,
3846 LLVMValueRef inactive
)
3848 char name
[33], type
[8];
3849 LLVMTypeRef src_type
= LLVMTypeOf(src
);
3850 src
= ac_to_integer(ctx
, src
);
3851 inactive
= ac_to_integer(ctx
, inactive
);
3852 ac_build_type_name_for_intr(LLVMTypeOf(src
), type
, sizeof(type
));
3853 snprintf(name
, sizeof(name
), "llvm.amdgcn.set.inactive.%s", type
);
3855 ac_build_intrinsic(ctx
, name
,
3856 LLVMTypeOf(src
), (LLVMValueRef
[]) {
3858 AC_FUNC_ATTR_READNONE
|
3859 AC_FUNC_ATTR_CONVERGENT
);
3860 return LLVMBuildBitCast(ctx
->builder
, ret
, src_type
, "");
3864 get_reduction_identity(struct ac_llvm_context
*ctx
, nir_op op
, unsigned type_size
)
3866 if (type_size
== 4) {
3868 case nir_op_iadd
: return ctx
->i32_0
;
3869 case nir_op_fadd
: return ctx
->f32_0
;
3870 case nir_op_imul
: return ctx
->i32_1
;
3871 case nir_op_fmul
: return ctx
->f32_1
;
3872 case nir_op_imin
: return LLVMConstInt(ctx
->i32
, INT32_MAX
, 0);
3873 case nir_op_umin
: return LLVMConstInt(ctx
->i32
, UINT32_MAX
, 0);
3874 case nir_op_fmin
: return LLVMConstReal(ctx
->f32
, INFINITY
);
3875 case nir_op_imax
: return LLVMConstInt(ctx
->i32
, INT32_MIN
, 0);
3876 case nir_op_umax
: return ctx
->i32_0
;
3877 case nir_op_fmax
: return LLVMConstReal(ctx
->f32
, -INFINITY
);
3878 case nir_op_iand
: return LLVMConstInt(ctx
->i32
, -1, 0);
3879 case nir_op_ior
: return ctx
->i32_0
;
3880 case nir_op_ixor
: return ctx
->i32_0
;
3882 unreachable("bad reduction intrinsic");
3884 } else { /* type_size == 64bit */
3886 case nir_op_iadd
: return ctx
->i64_0
;
3887 case nir_op_fadd
: return ctx
->f64_0
;
3888 case nir_op_imul
: return ctx
->i64_1
;
3889 case nir_op_fmul
: return ctx
->f64_1
;
3890 case nir_op_imin
: return LLVMConstInt(ctx
->i64
, INT64_MAX
, 0);
3891 case nir_op_umin
: return LLVMConstInt(ctx
->i64
, UINT64_MAX
, 0);
3892 case nir_op_fmin
: return LLVMConstReal(ctx
->f64
, INFINITY
);
3893 case nir_op_imax
: return LLVMConstInt(ctx
->i64
, INT64_MIN
, 0);
3894 case nir_op_umax
: return ctx
->i64_0
;
3895 case nir_op_fmax
: return LLVMConstReal(ctx
->f64
, -INFINITY
);
3896 case nir_op_iand
: return LLVMConstInt(ctx
->i64
, -1, 0);
3897 case nir_op_ior
: return ctx
->i64_0
;
3898 case nir_op_ixor
: return ctx
->i64_0
;
3900 unreachable("bad reduction intrinsic");
3906 ac_build_alu_op(struct ac_llvm_context
*ctx
, LLVMValueRef lhs
, LLVMValueRef rhs
, nir_op op
)
3908 bool _64bit
= ac_get_type_size(LLVMTypeOf(lhs
)) == 8;
3910 case nir_op_iadd
: return LLVMBuildAdd(ctx
->builder
, lhs
, rhs
, "");
3911 case nir_op_fadd
: return LLVMBuildFAdd(ctx
->builder
, lhs
, rhs
, "");
3912 case nir_op_imul
: return LLVMBuildMul(ctx
->builder
, lhs
, rhs
, "");
3913 case nir_op_fmul
: return LLVMBuildFMul(ctx
->builder
, lhs
, rhs
, "");
3914 case nir_op_imin
: return LLVMBuildSelect(ctx
->builder
,
3915 LLVMBuildICmp(ctx
->builder
, LLVMIntSLT
, lhs
, rhs
, ""),
3917 case nir_op_umin
: return LLVMBuildSelect(ctx
->builder
,
3918 LLVMBuildICmp(ctx
->builder
, LLVMIntULT
, lhs
, rhs
, ""),
3920 case nir_op_fmin
: return ac_build_intrinsic(ctx
,
3921 _64bit
? "llvm.minnum.f64" : "llvm.minnum.f32",
3922 _64bit
? ctx
->f64
: ctx
->f32
,
3923 (LLVMValueRef
[]){lhs
, rhs
}, 2, AC_FUNC_ATTR_READNONE
);
3924 case nir_op_imax
: return LLVMBuildSelect(ctx
->builder
,
3925 LLVMBuildICmp(ctx
->builder
, LLVMIntSGT
, lhs
, rhs
, ""),
3927 case nir_op_umax
: return LLVMBuildSelect(ctx
->builder
,
3928 LLVMBuildICmp(ctx
->builder
, LLVMIntUGT
, lhs
, rhs
, ""),
3930 case nir_op_fmax
: return ac_build_intrinsic(ctx
,
3931 _64bit
? "llvm.maxnum.f64" : "llvm.maxnum.f32",
3932 _64bit
? ctx
->f64
: ctx
->f32
,
3933 (LLVMValueRef
[]){lhs
, rhs
}, 2, AC_FUNC_ATTR_READNONE
);
3934 case nir_op_iand
: return LLVMBuildAnd(ctx
->builder
, lhs
, rhs
, "");
3935 case nir_op_ior
: return LLVMBuildOr(ctx
->builder
, lhs
, rhs
, "");
3936 case nir_op_ixor
: return LLVMBuildXor(ctx
->builder
, lhs
, rhs
, "");
3938 unreachable("bad reduction intrinsic");
3943 * \param maxprefix specifies that the result only needs to be correct for a
3944 * prefix of this many threads
3946 * TODO: add inclusive and excluse scan functions for GFX6.
3949 ac_build_scan(struct ac_llvm_context
*ctx
, nir_op op
, LLVMValueRef src
, LLVMValueRef identity
,
3950 unsigned maxprefix
, bool inclusive
)
3952 LLVMValueRef result
, tmp
;
3954 if (ctx
->chip_class
>= GFX10
) {
3955 result
= inclusive
? src
: identity
;
3958 src
= ac_build_dpp(ctx
, identity
, src
, dpp_wf_sr1
, 0xf, 0xf, false);
3963 tmp
= ac_build_dpp(ctx
, identity
, src
, dpp_row_sr(1), 0xf, 0xf, false);
3964 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
3967 tmp
= ac_build_dpp(ctx
, identity
, src
, dpp_row_sr(2), 0xf, 0xf, false);
3968 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
3971 tmp
= ac_build_dpp(ctx
, identity
, src
, dpp_row_sr(3), 0xf, 0xf, false);
3972 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
3975 tmp
= ac_build_dpp(ctx
, identity
, result
, dpp_row_sr(4), 0xf, 0xe, false);
3976 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
3979 tmp
= ac_build_dpp(ctx
, identity
, result
, dpp_row_sr(8), 0xf, 0xc, false);
3980 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
3981 if (maxprefix
<= 16)
3984 if (ctx
->chip_class
>= GFX10
) {
3985 /* dpp_row_bcast{15,31} are not supported on gfx10. */
3986 LLVMBuilderRef builder
= ctx
->builder
;
3987 LLVMValueRef tid
= ac_get_thread_id(ctx
);
3989 /* TODO-GFX10: Can we get better code-gen by putting this into
3990 * a branch so that LLVM generates EXEC mask manipulations? */
3994 tmp
= ac_build_alu_op(ctx
, result
, src
, op
);
3995 tmp
= ac_build_permlane16(ctx
, tmp
, ~(uint64_t)0, true, false);
3996 tmp
= ac_build_alu_op(ctx
, result
, tmp
, op
);
3997 cc
= LLVMBuildAnd(builder
, tid
, LLVMConstInt(ctx
->i32
, 16, false), "");
3998 cc
= LLVMBuildICmp(builder
, LLVMIntNE
, cc
, ctx
->i32_0
, "");
3999 result
= LLVMBuildSelect(builder
, cc
, tmp
, result
, "");
4000 if (maxprefix
<= 32)
4006 tmp
= ac_build_alu_op(ctx
, result
, src
, op
);
4007 tmp
= ac_build_readlane(ctx
, tmp
, LLVMConstInt(ctx
->i32
, 31, false));
4008 tmp
= ac_build_alu_op(ctx
, result
, tmp
, op
);
4009 cc
= LLVMBuildICmp(builder
, LLVMIntUGE
, tid
,
4010 LLVMConstInt(ctx
->i32
, 32, false), "");
4011 result
= LLVMBuildSelect(builder
, cc
, tmp
, result
, "");
4015 tmp
= ac_build_dpp(ctx
, identity
, result
, dpp_row_bcast15
, 0xa, 0xf, false);
4016 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
4017 if (maxprefix
<= 32)
4019 tmp
= ac_build_dpp(ctx
, identity
, result
, dpp_row_bcast31
, 0xc, 0xf, false);
4020 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
4025 ac_build_inclusive_scan(struct ac_llvm_context
*ctx
, LLVMValueRef src
, nir_op op
)
4027 LLVMValueRef result
;
4029 if (LLVMTypeOf(src
) == ctx
->i1
&& op
== nir_op_iadd
) {
4030 LLVMBuilderRef builder
= ctx
->builder
;
4031 src
= LLVMBuildZExt(builder
, src
, ctx
->i32
, "");
4032 result
= ac_build_ballot(ctx
, src
);
4033 result
= ac_build_mbcnt(ctx
, result
);
4034 result
= LLVMBuildAdd(builder
, result
, src
, "");
4038 ac_build_optimization_barrier(ctx
, &src
);
4040 LLVMValueRef identity
=
4041 get_reduction_identity(ctx
, op
, ac_get_type_size(LLVMTypeOf(src
)));
4042 result
= LLVMBuildBitCast(ctx
->builder
, ac_build_set_inactive(ctx
, src
, identity
),
4043 LLVMTypeOf(identity
), "");
4044 result
= ac_build_scan(ctx
, op
, result
, identity
, ctx
->wave_size
, true);
4046 return ac_build_wwm(ctx
, result
);
4050 ac_build_exclusive_scan(struct ac_llvm_context
*ctx
, LLVMValueRef src
, nir_op op
)
4052 LLVMValueRef result
;
4054 if (LLVMTypeOf(src
) == ctx
->i1
&& op
== nir_op_iadd
) {
4055 LLVMBuilderRef builder
= ctx
->builder
;
4056 src
= LLVMBuildZExt(builder
, src
, ctx
->i32
, "");
4057 result
= ac_build_ballot(ctx
, src
);
4058 result
= ac_build_mbcnt(ctx
, result
);
4062 ac_build_optimization_barrier(ctx
, &src
);
4064 LLVMValueRef identity
=
4065 get_reduction_identity(ctx
, op
, ac_get_type_size(LLVMTypeOf(src
)));
4066 result
= LLVMBuildBitCast(ctx
->builder
, ac_build_set_inactive(ctx
, src
, identity
),
4067 LLVMTypeOf(identity
), "");
4068 result
= ac_build_scan(ctx
, op
, result
, identity
, ctx
->wave_size
, false);
4070 return ac_build_wwm(ctx
, result
);
4074 ac_build_reduce(struct ac_llvm_context
*ctx
, LLVMValueRef src
, nir_op op
, unsigned cluster_size
)
4076 if (cluster_size
== 1) return src
;
4077 ac_build_optimization_barrier(ctx
, &src
);
4078 LLVMValueRef result
, swap
;
4079 LLVMValueRef identity
= get_reduction_identity(ctx
, op
,
4080 ac_get_type_size(LLVMTypeOf(src
)));
4081 result
= LLVMBuildBitCast(ctx
->builder
,
4082 ac_build_set_inactive(ctx
, src
, identity
),
4083 LLVMTypeOf(identity
), "");
4084 swap
= ac_build_quad_swizzle(ctx
, result
, 1, 0, 3, 2);
4085 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
4086 if (cluster_size
== 2) return ac_build_wwm(ctx
, result
);
4088 swap
= ac_build_quad_swizzle(ctx
, result
, 2, 3, 0, 1);
4089 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
4090 if (cluster_size
== 4) return ac_build_wwm(ctx
, result
);
4092 if (ctx
->chip_class
>= GFX8
)
4093 swap
= ac_build_dpp(ctx
, identity
, result
, dpp_row_half_mirror
, 0xf, 0xf, false);
4095 swap
= ac_build_ds_swizzle(ctx
, result
, ds_pattern_bitmode(0x1f, 0, 0x04));
4096 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
4097 if (cluster_size
== 8) return ac_build_wwm(ctx
, result
);
4099 if (ctx
->chip_class
>= GFX8
)
4100 swap
= ac_build_dpp(ctx
, identity
, result
, dpp_row_mirror
, 0xf, 0xf, false);
4102 swap
= ac_build_ds_swizzle(ctx
, result
, ds_pattern_bitmode(0x1f, 0, 0x08));
4103 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
4104 if (cluster_size
== 16) return ac_build_wwm(ctx
, result
);
4106 if (ctx
->chip_class
>= GFX10
)
4107 swap
= ac_build_permlane16(ctx
, result
, 0, true, false);
4108 else if (ctx
->chip_class
>= GFX8
&& cluster_size
!= 32)
4109 swap
= ac_build_dpp(ctx
, identity
, result
, dpp_row_bcast15
, 0xa, 0xf, false);
4111 swap
= ac_build_ds_swizzle(ctx
, result
, ds_pattern_bitmode(0x1f, 0, 0x10));
4112 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
4113 if (cluster_size
== 32) return ac_build_wwm(ctx
, result
);
4115 if (ctx
->chip_class
>= GFX8
) {
4116 if (ctx
->chip_class
>= GFX10
)
4117 swap
= ac_build_readlane(ctx
, result
, LLVMConstInt(ctx
->i32
, 31, false));
4119 swap
= ac_build_dpp(ctx
, identity
, result
, dpp_row_bcast31
, 0xc, 0xf, false);
4120 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
4121 result
= ac_build_readlane(ctx
, result
, LLVMConstInt(ctx
->i32
, 63, 0));
4122 return ac_build_wwm(ctx
, result
);
4124 swap
= ac_build_readlane(ctx
, result
, ctx
->i32_0
);
4125 result
= ac_build_readlane(ctx
, result
, LLVMConstInt(ctx
->i32
, 32, 0));
4126 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
4127 return ac_build_wwm(ctx
, result
);
4132 * "Top half" of a scan that reduces per-wave values across an entire
4135 * The source value must be present in the highest lane of the wave, and the
4136 * highest lane must be live.
4139 ac_build_wg_wavescan_top(struct ac_llvm_context
*ctx
, struct ac_wg_scan
*ws
)
4141 if (ws
->maxwaves
<= 1)
4144 const LLVMValueRef last_lane
= LLVMConstInt(ctx
->i32
, ctx
->wave_size
- 1, false);
4145 LLVMBuilderRef builder
= ctx
->builder
;
4146 LLVMValueRef tid
= ac_get_thread_id(ctx
);
4149 tmp
= LLVMBuildICmp(builder
, LLVMIntEQ
, tid
, last_lane
, "");
4150 ac_build_ifcc(ctx
, tmp
, 1000);
4151 LLVMBuildStore(builder
, ws
->src
, LLVMBuildGEP(builder
, ws
->scratch
, &ws
->waveidx
, 1, ""));
4152 ac_build_endif(ctx
, 1000);
4156 * "Bottom half" of a scan that reduces per-wave values across an entire
4159 * The caller must place a barrier between the top and bottom halves.
4162 ac_build_wg_wavescan_bottom(struct ac_llvm_context
*ctx
, struct ac_wg_scan
*ws
)
4164 const LLVMTypeRef type
= LLVMTypeOf(ws
->src
);
4165 const LLVMValueRef identity
=
4166 get_reduction_identity(ctx
, ws
->op
, ac_get_type_size(type
));
4168 if (ws
->maxwaves
<= 1) {
4169 ws
->result_reduce
= ws
->src
;
4170 ws
->result_inclusive
= ws
->src
;
4171 ws
->result_exclusive
= identity
;
4174 assert(ws
->maxwaves
<= 32);
4176 LLVMBuilderRef builder
= ctx
->builder
;
4177 LLVMValueRef tid
= ac_get_thread_id(ctx
);
4178 LLVMBasicBlockRef bbs
[2];
4179 LLVMValueRef phivalues_scan
[2];
4180 LLVMValueRef tmp
, tmp2
;
4182 bbs
[0] = LLVMGetInsertBlock(builder
);
4183 phivalues_scan
[0] = LLVMGetUndef(type
);
4185 if (ws
->enable_reduce
)
4186 tmp
= LLVMBuildICmp(builder
, LLVMIntULT
, tid
, ws
->numwaves
, "");
4187 else if (ws
->enable_inclusive
)
4188 tmp
= LLVMBuildICmp(builder
, LLVMIntULE
, tid
, ws
->waveidx
, "");
4190 tmp
= LLVMBuildICmp(builder
, LLVMIntULT
, tid
, ws
->waveidx
, "");
4191 ac_build_ifcc(ctx
, tmp
, 1001);
4193 tmp
= LLVMBuildLoad(builder
, LLVMBuildGEP(builder
, ws
->scratch
, &tid
, 1, ""), "");
4195 ac_build_optimization_barrier(ctx
, &tmp
);
4197 bbs
[1] = LLVMGetInsertBlock(builder
);
4198 phivalues_scan
[1] = ac_build_scan(ctx
, ws
->op
, tmp
, identity
, ws
->maxwaves
, true);
4200 ac_build_endif(ctx
, 1001);
4202 const LLVMValueRef scan
= ac_build_phi(ctx
, type
, 2, phivalues_scan
, bbs
);
4204 if (ws
->enable_reduce
) {
4205 tmp
= LLVMBuildSub(builder
, ws
->numwaves
, ctx
->i32_1
, "");
4206 ws
->result_reduce
= ac_build_readlane(ctx
, scan
, tmp
);
4208 if (ws
->enable_inclusive
)
4209 ws
->result_inclusive
= ac_build_readlane(ctx
, scan
, ws
->waveidx
);
4210 if (ws
->enable_exclusive
) {
4211 tmp
= LLVMBuildSub(builder
, ws
->waveidx
, ctx
->i32_1
, "");
4212 tmp
= ac_build_readlane(ctx
, scan
, tmp
);
4213 tmp2
= LLVMBuildICmp(builder
, LLVMIntEQ
, ws
->waveidx
, ctx
->i32_0
, "");
4214 ws
->result_exclusive
= LLVMBuildSelect(builder
, tmp2
, identity
, tmp
, "");
4219 * Inclusive scan of a per-wave value across an entire workgroup.
4221 * This implies an s_barrier instruction.
4223 * Unlike ac_build_inclusive_scan, the caller \em must ensure that all threads
4224 * of the workgroup are live. (This requirement cannot easily be relaxed in a
4225 * useful manner because of the barrier in the algorithm.)
4228 ac_build_wg_wavescan(struct ac_llvm_context
*ctx
, struct ac_wg_scan
*ws
)
4230 ac_build_wg_wavescan_top(ctx
, ws
);
4231 ac_build_s_barrier(ctx
);
4232 ac_build_wg_wavescan_bottom(ctx
, ws
);
4236 * "Top half" of a scan that reduces per-thread values across an entire
4239 * All lanes must be active when this code runs.
4242 ac_build_wg_scan_top(struct ac_llvm_context
*ctx
, struct ac_wg_scan
*ws
)
4244 if (ws
->enable_exclusive
) {
4245 ws
->extra
= ac_build_exclusive_scan(ctx
, ws
->src
, ws
->op
);
4246 if (LLVMTypeOf(ws
->src
) == ctx
->i1
&& ws
->op
== nir_op_iadd
)
4247 ws
->src
= LLVMBuildZExt(ctx
->builder
, ws
->src
, ctx
->i32
, "");
4248 ws
->src
= ac_build_alu_op(ctx
, ws
->extra
, ws
->src
, ws
->op
);
4250 ws
->src
= ac_build_inclusive_scan(ctx
, ws
->src
, ws
->op
);
4253 bool enable_inclusive
= ws
->enable_inclusive
;
4254 bool enable_exclusive
= ws
->enable_exclusive
;
4255 ws
->enable_inclusive
= false;
4256 ws
->enable_exclusive
= ws
->enable_exclusive
|| enable_inclusive
;
4257 ac_build_wg_wavescan_top(ctx
, ws
);
4258 ws
->enable_inclusive
= enable_inclusive
;
4259 ws
->enable_exclusive
= enable_exclusive
;
4263 * "Bottom half" of a scan that reduces per-thread values across an entire
4266 * The caller must place a barrier between the top and bottom halves.
4269 ac_build_wg_scan_bottom(struct ac_llvm_context
*ctx
, struct ac_wg_scan
*ws
)
4271 bool enable_inclusive
= ws
->enable_inclusive
;
4272 bool enable_exclusive
= ws
->enable_exclusive
;
4273 ws
->enable_inclusive
= false;
4274 ws
->enable_exclusive
= ws
->enable_exclusive
|| enable_inclusive
;
4275 ac_build_wg_wavescan_bottom(ctx
, ws
);
4276 ws
->enable_inclusive
= enable_inclusive
;
4277 ws
->enable_exclusive
= enable_exclusive
;
4279 /* ws->result_reduce is already the correct value */
4280 if (ws
->enable_inclusive
)
4281 ws
->result_inclusive
= ac_build_alu_op(ctx
, ws
->result_inclusive
, ws
->src
, ws
->op
);
4282 if (ws
->enable_exclusive
)
4283 ws
->result_exclusive
= ac_build_alu_op(ctx
, ws
->result_exclusive
, ws
->extra
, ws
->op
);
4287 * A scan that reduces per-thread values across an entire workgroup.
4289 * The caller must ensure that all lanes are active when this code runs
4290 * (WWM is insufficient!), because there is an implied barrier.
4293 ac_build_wg_scan(struct ac_llvm_context
*ctx
, struct ac_wg_scan
*ws
)
4295 ac_build_wg_scan_top(ctx
, ws
);
4296 ac_build_s_barrier(ctx
);
4297 ac_build_wg_scan_bottom(ctx
, ws
);
4301 ac_build_quad_swizzle(struct ac_llvm_context
*ctx
, LLVMValueRef src
,
4302 unsigned lane0
, unsigned lane1
, unsigned lane2
, unsigned lane3
)
4304 unsigned mask
= dpp_quad_perm(lane0
, lane1
, lane2
, lane3
);
4305 if (ctx
->chip_class
>= GFX8
) {
4306 return ac_build_dpp(ctx
, src
, src
, mask
, 0xf, 0xf, false);
4308 return ac_build_ds_swizzle(ctx
, src
, (1 << 15) | mask
);
4313 ac_build_shuffle(struct ac_llvm_context
*ctx
, LLVMValueRef src
, LLVMValueRef index
)
4315 index
= LLVMBuildMul(ctx
->builder
, index
, LLVMConstInt(ctx
->i32
, 4, 0), "");
4316 return ac_build_intrinsic(ctx
,
4317 "llvm.amdgcn.ds.bpermute", ctx
->i32
,
4318 (LLVMValueRef
[]) {index
, src
}, 2,
4319 AC_FUNC_ATTR_READNONE
|
4320 AC_FUNC_ATTR_CONVERGENT
);
4324 ac_build_frexp_exp(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
4330 if (bitsize
== 16) {
4331 intr
= "llvm.amdgcn.frexp.exp.i16.f16";
4333 } else if (bitsize
== 32) {
4334 intr
= "llvm.amdgcn.frexp.exp.i32.f32";
4337 intr
= "llvm.amdgcn.frexp.exp.i32.f64";
4341 LLVMValueRef params
[] = {
4344 return ac_build_intrinsic(ctx
, intr
, type
, params
, 1,
4345 AC_FUNC_ATTR_READNONE
);
4348 ac_build_frexp_mant(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
4354 if (bitsize
== 16) {
4355 intr
= "llvm.amdgcn.frexp.mant.f16";
4357 } else if (bitsize
== 32) {
4358 intr
= "llvm.amdgcn.frexp.mant.f32";
4361 intr
= "llvm.amdgcn.frexp.mant.f64";
4365 LLVMValueRef params
[] = {
4368 return ac_build_intrinsic(ctx
, intr
, type
, params
, 1,
4369 AC_FUNC_ATTR_READNONE
);
4373 * this takes an I,J coordinate pair,
4374 * and works out the X and Y derivatives.
4375 * it returns DDX(I), DDX(J), DDY(I), DDY(J).
4378 ac_build_ddxy_interp(struct ac_llvm_context
*ctx
, LLVMValueRef interp_ij
)
4380 LLVMValueRef result
[4], a
;
4383 for (i
= 0; i
< 2; i
++) {
4384 a
= LLVMBuildExtractElement(ctx
->builder
, interp_ij
,
4385 LLVMConstInt(ctx
->i32
, i
, false), "");
4386 result
[i
] = ac_build_ddxy(ctx
, AC_TID_MASK_TOP_LEFT
, 1, a
);
4387 result
[2+i
] = ac_build_ddxy(ctx
, AC_TID_MASK_TOP_LEFT
, 2, a
);
4389 return ac_build_gather_values(ctx
, result
, 4);
4393 ac_build_load_helper_invocation(struct ac_llvm_context
*ctx
)
4395 LLVMValueRef result
= ac_build_intrinsic(ctx
, "llvm.amdgcn.ps.live",
4397 AC_FUNC_ATTR_READNONE
);
4398 result
= LLVMBuildNot(ctx
->builder
, result
, "");
4399 return LLVMBuildSExt(ctx
->builder
, result
, ctx
->i32
, "");
4402 LLVMValueRef
ac_build_call(struct ac_llvm_context
*ctx
, LLVMValueRef func
,
4403 LLVMValueRef
*args
, unsigned num_args
)
4405 LLVMValueRef ret
= LLVMBuildCall(ctx
->builder
, func
, args
, num_args
, "");
4406 LLVMSetInstructionCallConv(ret
, LLVMGetFunctionCallConv(func
));
4411 ac_export_mrt_z(struct ac_llvm_context
*ctx
, LLVMValueRef depth
,
4412 LLVMValueRef stencil
, LLVMValueRef samplemask
,
4413 struct ac_export_args
*args
)
4416 unsigned format
= ac_get_spi_shader_z_format(depth
!= NULL
,
4418 samplemask
!= NULL
);
4420 assert(depth
|| stencil
|| samplemask
);
4422 memset(args
, 0, sizeof(*args
));
4424 args
->valid_mask
= 1; /* whether the EXEC mask is valid */
4425 args
->done
= 1; /* DONE bit */
4427 /* Specify the target we are exporting */
4428 args
->target
= V_008DFC_SQ_EXP_MRTZ
;
4430 args
->compr
= 0; /* COMP flag */
4431 args
->out
[0] = LLVMGetUndef(ctx
->f32
); /* R, depth */
4432 args
->out
[1] = LLVMGetUndef(ctx
->f32
); /* G, stencil test val[0:7], stencil op val[8:15] */
4433 args
->out
[2] = LLVMGetUndef(ctx
->f32
); /* B, sample mask */
4434 args
->out
[3] = LLVMGetUndef(ctx
->f32
); /* A, alpha to mask */
4436 if (format
== V_028710_SPI_SHADER_UINT16_ABGR
) {
4438 args
->compr
= 1; /* COMPR flag */
4441 /* Stencil should be in X[23:16]. */
4442 stencil
= ac_to_integer(ctx
, stencil
);
4443 stencil
= LLVMBuildShl(ctx
->builder
, stencil
,
4444 LLVMConstInt(ctx
->i32
, 16, 0), "");
4445 args
->out
[0] = ac_to_float(ctx
, stencil
);
4449 /* SampleMask should be in Y[15:0]. */
4450 args
->out
[1] = samplemask
;
4455 args
->out
[0] = depth
;
4459 args
->out
[1] = stencil
;
4463 args
->out
[2] = samplemask
;
4468 /* GFX6 (except OLAND and HAINAN) has a bug that it only looks
4469 * at the X writemask component. */
4470 if (ctx
->chip_class
== GFX6
&&
4471 ctx
->family
!= CHIP_OLAND
&&
4472 ctx
->family
!= CHIP_HAINAN
)
4475 /* Specify which components to enable */
4476 args
->enabled_channels
= mask
;