2 * Copyright 2014 Advanced Micro Devices, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sub license, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
15 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
16 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
17 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
18 * USE OR OTHER DEALINGS IN THE SOFTWARE.
20 * The above copyright notice and this permission notice (including the
21 * next paragraph) shall be included in all copies or substantial portions
25 /* based on pieces from si_pipe.c and radeon_llvm_emit.c */
26 #include "ac_llvm_build.h"
28 #include <llvm-c/Core.h>
30 #include "c11/threads.h"
35 #include "ac_llvm_util.h"
36 #include "ac_exp_param.h"
37 #include "util/bitscan.h"
38 #include "util/macros.h"
39 #include "util/u_atomic.h"
40 #include "util/u_math.h"
43 #include "shader_enums.h"
45 #define AC_LLVM_INITIAL_CF_DEPTH 4
47 /* Data for if/else/endif and bgnloop/endloop control flow structures.
50 /* Loop exit or next part of if/else/endif. */
51 LLVMBasicBlockRef next_block
;
52 LLVMBasicBlockRef loop_entry_block
;
55 /* Initialize module-independent parts of the context.
57 * The caller is responsible for initializing ctx::module and ctx::builder.
60 ac_llvm_context_init(struct ac_llvm_context
*ctx
,
61 enum chip_class chip_class
, enum radeon_family family
)
65 ctx
->context
= LLVMContextCreate();
67 ctx
->chip_class
= chip_class
;
72 ctx
->voidt
= LLVMVoidTypeInContext(ctx
->context
);
73 ctx
->i1
= LLVMInt1TypeInContext(ctx
->context
);
74 ctx
->i8
= LLVMInt8TypeInContext(ctx
->context
);
75 ctx
->i16
= LLVMIntTypeInContext(ctx
->context
, 16);
76 ctx
->i32
= LLVMIntTypeInContext(ctx
->context
, 32);
77 ctx
->i64
= LLVMIntTypeInContext(ctx
->context
, 64);
78 ctx
->intptr
= HAVE_32BIT_POINTERS
? ctx
->i32
: ctx
->i64
;
79 ctx
->f16
= LLVMHalfTypeInContext(ctx
->context
);
80 ctx
->f32
= LLVMFloatTypeInContext(ctx
->context
);
81 ctx
->f64
= LLVMDoubleTypeInContext(ctx
->context
);
82 ctx
->v2i16
= LLVMVectorType(ctx
->i16
, 2);
83 ctx
->v2i32
= LLVMVectorType(ctx
->i32
, 2);
84 ctx
->v3i32
= LLVMVectorType(ctx
->i32
, 3);
85 ctx
->v4i32
= LLVMVectorType(ctx
->i32
, 4);
86 ctx
->v2f32
= LLVMVectorType(ctx
->f32
, 2);
87 ctx
->v4f32
= LLVMVectorType(ctx
->f32
, 4);
88 ctx
->v8i32
= LLVMVectorType(ctx
->i32
, 8);
90 ctx
->i32_0
= LLVMConstInt(ctx
->i32
, 0, false);
91 ctx
->i32_1
= LLVMConstInt(ctx
->i32
, 1, false);
92 ctx
->i64_0
= LLVMConstInt(ctx
->i64
, 0, false);
93 ctx
->i64_1
= LLVMConstInt(ctx
->i64
, 1, false);
94 ctx
->f32_0
= LLVMConstReal(ctx
->f32
, 0.0);
95 ctx
->f32_1
= LLVMConstReal(ctx
->f32
, 1.0);
96 ctx
->f64_0
= LLVMConstReal(ctx
->f64
, 0.0);
97 ctx
->f64_1
= LLVMConstReal(ctx
->f64
, 1.0);
99 ctx
->i1false
= LLVMConstInt(ctx
->i1
, 0, false);
100 ctx
->i1true
= LLVMConstInt(ctx
->i1
, 1, false);
102 ctx
->range_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
105 ctx
->invariant_load_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
106 "invariant.load", 14);
108 ctx
->fpmath_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
, "fpmath", 6);
110 args
[0] = LLVMConstReal(ctx
->f32
, 2.5);
111 ctx
->fpmath_md_2p5_ulp
= LLVMMDNodeInContext(ctx
->context
, args
, 1);
113 ctx
->uniform_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
114 "amdgpu.uniform", 14);
116 ctx
->empty_md
= LLVMMDNodeInContext(ctx
->context
, NULL
, 0);
120 ac_llvm_context_dispose(struct ac_llvm_context
*ctx
)
124 ctx
->flow_depth_max
= 0;
128 ac_get_llvm_num_components(LLVMValueRef value
)
130 LLVMTypeRef type
= LLVMTypeOf(value
);
131 unsigned num_components
= LLVMGetTypeKind(type
) == LLVMVectorTypeKind
132 ? LLVMGetVectorSize(type
)
134 return num_components
;
138 ac_llvm_extract_elem(struct ac_llvm_context
*ac
,
142 if (LLVMGetTypeKind(LLVMTypeOf(value
)) != LLVMVectorTypeKind
) {
147 return LLVMBuildExtractElement(ac
->builder
, value
,
148 LLVMConstInt(ac
->i32
, index
, false), "");
152 ac_get_elem_bits(struct ac_llvm_context
*ctx
, LLVMTypeRef type
)
154 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
)
155 type
= LLVMGetElementType(type
);
157 if (LLVMGetTypeKind(type
) == LLVMIntegerTypeKind
)
158 return LLVMGetIntTypeWidth(type
);
160 if (type
== ctx
->f16
)
162 if (type
== ctx
->f32
)
164 if (type
== ctx
->f64
)
167 unreachable("Unhandled type kind in get_elem_bits");
171 ac_get_type_size(LLVMTypeRef type
)
173 LLVMTypeKind kind
= LLVMGetTypeKind(type
);
176 case LLVMIntegerTypeKind
:
177 return LLVMGetIntTypeWidth(type
) / 8;
178 case LLVMHalfTypeKind
:
180 case LLVMFloatTypeKind
:
182 case LLVMDoubleTypeKind
:
184 case LLVMPointerTypeKind
:
185 if (LLVMGetPointerAddressSpace(type
) == AC_CONST_32BIT_ADDR_SPACE
)
188 case LLVMVectorTypeKind
:
189 return LLVMGetVectorSize(type
) *
190 ac_get_type_size(LLVMGetElementType(type
));
191 case LLVMArrayTypeKind
:
192 return LLVMGetArrayLength(type
) *
193 ac_get_type_size(LLVMGetElementType(type
));
200 static LLVMTypeRef
to_integer_type_scalar(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
202 if (t
== ctx
->f16
|| t
== ctx
->i16
)
204 else if (t
== ctx
->f32
|| t
== ctx
->i32
)
206 else if (t
== ctx
->f64
|| t
== ctx
->i64
)
209 unreachable("Unhandled integer size");
213 ac_to_integer_type(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
215 if (LLVMGetTypeKind(t
) == LLVMVectorTypeKind
) {
216 LLVMTypeRef elem_type
= LLVMGetElementType(t
);
217 return LLVMVectorType(to_integer_type_scalar(ctx
, elem_type
),
218 LLVMGetVectorSize(t
));
220 return to_integer_type_scalar(ctx
, t
);
224 ac_to_integer(struct ac_llvm_context
*ctx
, LLVMValueRef v
)
226 LLVMTypeRef type
= LLVMTypeOf(v
);
227 return LLVMBuildBitCast(ctx
->builder
, v
, ac_to_integer_type(ctx
, type
), "");
230 static LLVMTypeRef
to_float_type_scalar(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
232 if (t
== ctx
->i16
|| t
== ctx
->f16
)
234 else if (t
== ctx
->i32
|| t
== ctx
->f32
)
236 else if (t
== ctx
->i64
|| t
== ctx
->f64
)
239 unreachable("Unhandled float size");
243 ac_to_float_type(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
245 if (LLVMGetTypeKind(t
) == LLVMVectorTypeKind
) {
246 LLVMTypeRef elem_type
= LLVMGetElementType(t
);
247 return LLVMVectorType(to_float_type_scalar(ctx
, elem_type
),
248 LLVMGetVectorSize(t
));
250 return to_float_type_scalar(ctx
, t
);
254 ac_to_float(struct ac_llvm_context
*ctx
, LLVMValueRef v
)
256 LLVMTypeRef type
= LLVMTypeOf(v
);
257 return LLVMBuildBitCast(ctx
->builder
, v
, ac_to_float_type(ctx
, type
), "");
262 ac_build_intrinsic(struct ac_llvm_context
*ctx
, const char *name
,
263 LLVMTypeRef return_type
, LLVMValueRef
*params
,
264 unsigned param_count
, unsigned attrib_mask
)
266 LLVMValueRef function
, call
;
267 bool set_callsite_attrs
= !(attrib_mask
& AC_FUNC_ATTR_LEGACY
);
269 function
= LLVMGetNamedFunction(ctx
->module
, name
);
271 LLVMTypeRef param_types
[32], function_type
;
274 assert(param_count
<= 32);
276 for (i
= 0; i
< param_count
; ++i
) {
278 param_types
[i
] = LLVMTypeOf(params
[i
]);
281 LLVMFunctionType(return_type
, param_types
, param_count
, 0);
282 function
= LLVMAddFunction(ctx
->module
, name
, function_type
);
284 LLVMSetFunctionCallConv(function
, LLVMCCallConv
);
285 LLVMSetLinkage(function
, LLVMExternalLinkage
);
287 if (!set_callsite_attrs
)
288 ac_add_func_attributes(ctx
->context
, function
, attrib_mask
);
291 call
= LLVMBuildCall(ctx
->builder
, function
, params
, param_count
, "");
292 if (set_callsite_attrs
)
293 ac_add_func_attributes(ctx
->context
, call
, attrib_mask
);
298 * Given the i32 or vNi32 \p type, generate the textual name (e.g. for use with
301 void ac_build_type_name_for_intr(LLVMTypeRef type
, char *buf
, unsigned bufsize
)
303 LLVMTypeRef elem_type
= type
;
305 assert(bufsize
>= 8);
307 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
) {
308 int ret
= snprintf(buf
, bufsize
, "v%u",
309 LLVMGetVectorSize(type
));
311 char *type_name
= LLVMPrintTypeToString(type
);
312 fprintf(stderr
, "Error building type name for: %s\n",
316 elem_type
= LLVMGetElementType(type
);
320 switch (LLVMGetTypeKind(elem_type
)) {
322 case LLVMIntegerTypeKind
:
323 snprintf(buf
, bufsize
, "i%d", LLVMGetIntTypeWidth(elem_type
));
325 case LLVMHalfTypeKind
:
326 snprintf(buf
, bufsize
, "f16");
328 case LLVMFloatTypeKind
:
329 snprintf(buf
, bufsize
, "f32");
331 case LLVMDoubleTypeKind
:
332 snprintf(buf
, bufsize
, "f64");
338 * Helper function that builds an LLVM IR PHI node and immediately adds
342 ac_build_phi(struct ac_llvm_context
*ctx
, LLVMTypeRef type
,
343 unsigned count_incoming
, LLVMValueRef
*values
,
344 LLVMBasicBlockRef
*blocks
)
346 LLVMValueRef phi
= LLVMBuildPhi(ctx
->builder
, type
, "");
347 LLVMAddIncoming(phi
, values
, blocks
, count_incoming
);
351 void ac_build_s_barrier(struct ac_llvm_context
*ctx
)
353 ac_build_intrinsic(ctx
, "llvm.amdgcn.s.barrier", ctx
->voidt
, NULL
,
354 0, AC_FUNC_ATTR_CONVERGENT
);
357 /* Prevent optimizations (at least of memory accesses) across the current
358 * point in the program by emitting empty inline assembly that is marked as
359 * having side effects.
361 * Optionally, a value can be passed through the inline assembly to prevent
362 * LLVM from hoisting calls to ReadNone functions.
365 ac_build_optimization_barrier(struct ac_llvm_context
*ctx
,
368 static int counter
= 0;
370 LLVMBuilderRef builder
= ctx
->builder
;
373 snprintf(code
, sizeof(code
), "; %d", p_atomic_inc_return(&counter
));
376 LLVMTypeRef ftype
= LLVMFunctionType(ctx
->voidt
, NULL
, 0, false);
377 LLVMValueRef inlineasm
= LLVMConstInlineAsm(ftype
, code
, "", true, false);
378 LLVMBuildCall(builder
, inlineasm
, NULL
, 0, "");
380 LLVMTypeRef ftype
= LLVMFunctionType(ctx
->i32
, &ctx
->i32
, 1, false);
381 LLVMValueRef inlineasm
= LLVMConstInlineAsm(ftype
, code
, "=v,0", true, false);
382 LLVMValueRef vgpr
= *pvgpr
;
383 LLVMTypeRef vgpr_type
= LLVMTypeOf(vgpr
);
384 unsigned vgpr_size
= ac_get_type_size(vgpr_type
);
387 assert(vgpr_size
% 4 == 0);
389 vgpr
= LLVMBuildBitCast(builder
, vgpr
, LLVMVectorType(ctx
->i32
, vgpr_size
/ 4), "");
390 vgpr0
= LLVMBuildExtractElement(builder
, vgpr
, ctx
->i32_0
, "");
391 vgpr0
= LLVMBuildCall(builder
, inlineasm
, &vgpr0
, 1, "");
392 vgpr
= LLVMBuildInsertElement(builder
, vgpr
, vgpr0
, ctx
->i32_0
, "");
393 vgpr
= LLVMBuildBitCast(builder
, vgpr
, vgpr_type
, "");
400 ac_build_shader_clock(struct ac_llvm_context
*ctx
)
402 LLVMValueRef tmp
= ac_build_intrinsic(ctx
, "llvm.readcyclecounter",
403 ctx
->i64
, NULL
, 0, 0);
404 return LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->v2i32
, "");
408 ac_build_ballot(struct ac_llvm_context
*ctx
,
411 LLVMValueRef args
[3] = {
414 LLVMConstInt(ctx
->i32
, LLVMIntNE
, 0)
417 /* We currently have no other way to prevent LLVM from lifting the icmp
418 * calls to a dominating basic block.
420 ac_build_optimization_barrier(ctx
, &args
[0]);
422 args
[0] = ac_to_integer(ctx
, args
[0]);
424 return ac_build_intrinsic(ctx
,
425 "llvm.amdgcn.icmp.i32",
427 AC_FUNC_ATTR_NOUNWIND
|
428 AC_FUNC_ATTR_READNONE
|
429 AC_FUNC_ATTR_CONVERGENT
);
433 ac_build_vote_all(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
435 LLVMValueRef active_set
= ac_build_ballot(ctx
, ctx
->i32_1
);
436 LLVMValueRef vote_set
= ac_build_ballot(ctx
, value
);
437 return LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, vote_set
, active_set
, "");
441 ac_build_vote_any(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
443 LLVMValueRef vote_set
= ac_build_ballot(ctx
, value
);
444 return LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, vote_set
,
445 LLVMConstInt(ctx
->i64
, 0, 0), "");
449 ac_build_vote_eq(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
451 LLVMValueRef active_set
= ac_build_ballot(ctx
, ctx
->i32_1
);
452 LLVMValueRef vote_set
= ac_build_ballot(ctx
, value
);
454 LLVMValueRef all
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
455 vote_set
, active_set
, "");
456 LLVMValueRef none
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
458 LLVMConstInt(ctx
->i64
, 0, 0), "");
459 return LLVMBuildOr(ctx
->builder
, all
, none
, "");
463 ac_build_varying_gather_values(struct ac_llvm_context
*ctx
, LLVMValueRef
*values
,
464 unsigned value_count
, unsigned component
)
466 LLVMValueRef vec
= NULL
;
468 if (value_count
== 1) {
469 return values
[component
];
470 } else if (!value_count
)
471 unreachable("value_count is 0");
473 for (unsigned i
= component
; i
< value_count
+ component
; i
++) {
474 LLVMValueRef value
= values
[i
];
477 vec
= LLVMGetUndef( LLVMVectorType(LLVMTypeOf(value
), value_count
));
478 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, i
- component
, false);
479 vec
= LLVMBuildInsertElement(ctx
->builder
, vec
, value
, index
, "");
485 ac_build_gather_values_extended(struct ac_llvm_context
*ctx
,
486 LLVMValueRef
*values
,
487 unsigned value_count
,
488 unsigned value_stride
,
492 LLVMBuilderRef builder
= ctx
->builder
;
493 LLVMValueRef vec
= NULL
;
496 if (value_count
== 1 && !always_vector
) {
498 return LLVMBuildLoad(builder
, values
[0], "");
500 } else if (!value_count
)
501 unreachable("value_count is 0");
503 for (i
= 0; i
< value_count
; i
++) {
504 LLVMValueRef value
= values
[i
* value_stride
];
506 value
= LLVMBuildLoad(builder
, value
, "");
509 vec
= LLVMGetUndef( LLVMVectorType(LLVMTypeOf(value
), value_count
));
510 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, i
, false);
511 vec
= LLVMBuildInsertElement(builder
, vec
, value
, index
, "");
517 ac_build_gather_values(struct ac_llvm_context
*ctx
,
518 LLVMValueRef
*values
,
519 unsigned value_count
)
521 return ac_build_gather_values_extended(ctx
, values
, value_count
, 1, false, false);
524 /* Expand a scalar or vector to <4 x type> by filling the remaining channels
525 * with undef. Extract at most num_channels components from the input.
527 LLVMValueRef
ac_build_expand_to_vec4(struct ac_llvm_context
*ctx
,
529 unsigned num_channels
)
531 LLVMTypeRef elemtype
;
532 LLVMValueRef chan
[4];
534 if (LLVMGetTypeKind(LLVMTypeOf(value
)) == LLVMVectorTypeKind
) {
535 unsigned vec_size
= LLVMGetVectorSize(LLVMTypeOf(value
));
536 num_channels
= MIN2(num_channels
, vec_size
);
538 if (num_channels
>= 4)
541 for (unsigned i
= 0; i
< num_channels
; i
++)
542 chan
[i
] = ac_llvm_extract_elem(ctx
, value
, i
);
544 elemtype
= LLVMGetElementType(LLVMTypeOf(value
));
547 assert(num_channels
== 1);
550 elemtype
= LLVMTypeOf(value
);
553 while (num_channels
< 4)
554 chan
[num_channels
++] = LLVMGetUndef(elemtype
);
556 return ac_build_gather_values(ctx
, chan
, 4);
560 ac_build_fdiv(struct ac_llvm_context
*ctx
,
564 /* If we do (num / den), LLVM >= 7.0 does:
565 * return num * v_rcp_f32(den * (fabs(den) > 0x1.0p+96f ? 0x1.0p-32f : 1.0f));
567 * If we do (num * (1 / den)), LLVM does:
568 * return num * v_rcp_f32(den);
570 LLVMValueRef rcp
= LLVMBuildFDiv(ctx
->builder
, ctx
->f32_1
, den
, "");
571 LLVMValueRef ret
= LLVMBuildFMul(ctx
->builder
, num
, rcp
, "");
573 /* Use v_rcp_f32 instead of precise division. */
574 if (!LLVMIsConstant(ret
))
575 LLVMSetMetadata(ret
, ctx
->fpmath_md_kind
, ctx
->fpmath_md_2p5_ulp
);
579 /* Coordinates for cube map selection. sc, tc, and ma are as in Table 8.27
580 * of the OpenGL 4.5 (Compatibility Profile) specification, except ma is
581 * already multiplied by two. id is the cube face number.
583 struct cube_selection_coords
{
590 build_cube_intrinsic(struct ac_llvm_context
*ctx
,
592 struct cube_selection_coords
*out
)
594 LLVMTypeRef f32
= ctx
->f32
;
596 out
->stc
[1] = ac_build_intrinsic(ctx
, "llvm.amdgcn.cubetc",
597 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
598 out
->stc
[0] = ac_build_intrinsic(ctx
, "llvm.amdgcn.cubesc",
599 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
600 out
->ma
= ac_build_intrinsic(ctx
, "llvm.amdgcn.cubema",
601 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
602 out
->id
= ac_build_intrinsic(ctx
, "llvm.amdgcn.cubeid",
603 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
607 * Build a manual selection sequence for cube face sc/tc coordinates and
608 * major axis vector (multiplied by 2 for consistency) for the given
609 * vec3 \p coords, for the face implied by \p selcoords.
611 * For the major axis, we always adjust the sign to be in the direction of
612 * selcoords.ma; i.e., a positive out_ma means that coords is pointed towards
613 * the selcoords major axis.
615 static void build_cube_select(struct ac_llvm_context
*ctx
,
616 const struct cube_selection_coords
*selcoords
,
617 const LLVMValueRef
*coords
,
618 LLVMValueRef
*out_st
,
619 LLVMValueRef
*out_ma
)
621 LLVMBuilderRef builder
= ctx
->builder
;
622 LLVMTypeRef f32
= LLVMTypeOf(coords
[0]);
623 LLVMValueRef is_ma_positive
;
625 LLVMValueRef is_ma_z
, is_not_ma_z
;
626 LLVMValueRef is_ma_y
;
627 LLVMValueRef is_ma_x
;
631 is_ma_positive
= LLVMBuildFCmp(builder
, LLVMRealUGE
,
632 selcoords
->ma
, LLVMConstReal(f32
, 0.0), "");
633 sgn_ma
= LLVMBuildSelect(builder
, is_ma_positive
,
634 LLVMConstReal(f32
, 1.0), LLVMConstReal(f32
, -1.0), "");
636 is_ma_z
= LLVMBuildFCmp(builder
, LLVMRealUGE
, selcoords
->id
, LLVMConstReal(f32
, 4.0), "");
637 is_not_ma_z
= LLVMBuildNot(builder
, is_ma_z
, "");
638 is_ma_y
= LLVMBuildAnd(builder
, is_not_ma_z
,
639 LLVMBuildFCmp(builder
, LLVMRealUGE
, selcoords
->id
, LLVMConstReal(f32
, 2.0), ""), "");
640 is_ma_x
= LLVMBuildAnd(builder
, is_not_ma_z
, LLVMBuildNot(builder
, is_ma_y
, ""), "");
643 tmp
= LLVMBuildSelect(builder
, is_ma_x
, coords
[2], coords
[0], "");
644 sgn
= LLVMBuildSelect(builder
, is_ma_y
, LLVMConstReal(f32
, 1.0),
645 LLVMBuildSelect(builder
, is_ma_z
, sgn_ma
,
646 LLVMBuildFNeg(builder
, sgn_ma
, ""), ""), "");
647 out_st
[0] = LLVMBuildFMul(builder
, tmp
, sgn
, "");
650 tmp
= LLVMBuildSelect(builder
, is_ma_y
, coords
[2], coords
[1], "");
651 sgn
= LLVMBuildSelect(builder
, is_ma_y
, sgn_ma
,
652 LLVMConstReal(f32
, -1.0), "");
653 out_st
[1] = LLVMBuildFMul(builder
, tmp
, sgn
, "");
656 tmp
= LLVMBuildSelect(builder
, is_ma_z
, coords
[2],
657 LLVMBuildSelect(builder
, is_ma_y
, coords
[1], coords
[0], ""), "");
658 tmp
= ac_build_intrinsic(ctx
, "llvm.fabs.f32",
659 ctx
->f32
, &tmp
, 1, AC_FUNC_ATTR_READNONE
);
660 *out_ma
= LLVMBuildFMul(builder
, tmp
, LLVMConstReal(f32
, 2.0), "");
664 ac_prepare_cube_coords(struct ac_llvm_context
*ctx
,
665 bool is_deriv
, bool is_array
, bool is_lod
,
666 LLVMValueRef
*coords_arg
,
667 LLVMValueRef
*derivs_arg
)
670 LLVMBuilderRef builder
= ctx
->builder
;
671 struct cube_selection_coords selcoords
;
672 LLVMValueRef coords
[3];
675 if (is_array
&& !is_lod
) {
676 LLVMValueRef tmp
= coords_arg
[3];
677 tmp
= ac_build_intrinsic(ctx
, "llvm.rint.f32", ctx
->f32
, &tmp
, 1, 0);
679 /* Section 8.9 (Texture Functions) of the GLSL 4.50 spec says:
681 * "For Array forms, the array layer used will be
683 * max(0, min(d−1, floor(layer+0.5)))
685 * where d is the depth of the texture array and layer
686 * comes from the component indicated in the tables below.
687 * Workaroudn for an issue where the layer is taken from a
688 * helper invocation which happens to fall on a different
689 * layer due to extrapolation."
691 * VI and earlier attempt to implement this in hardware by
692 * clamping the value of coords[2] = (8 * layer) + face.
693 * Unfortunately, this means that the we end up with the wrong
694 * face when clamping occurs.
696 * Clamp the layer earlier to work around the issue.
698 if (ctx
->chip_class
<= VI
) {
700 ge0
= LLVMBuildFCmp(builder
, LLVMRealOGE
, tmp
, ctx
->f32_0
, "");
701 tmp
= LLVMBuildSelect(builder
, ge0
, tmp
, ctx
->f32_0
, "");
707 build_cube_intrinsic(ctx
, coords_arg
, &selcoords
);
709 invma
= ac_build_intrinsic(ctx
, "llvm.fabs.f32",
710 ctx
->f32
, &selcoords
.ma
, 1, AC_FUNC_ATTR_READNONE
);
711 invma
= ac_build_fdiv(ctx
, LLVMConstReal(ctx
->f32
, 1.0), invma
);
713 for (int i
= 0; i
< 2; ++i
)
714 coords
[i
] = LLVMBuildFMul(builder
, selcoords
.stc
[i
], invma
, "");
716 coords
[2] = selcoords
.id
;
718 if (is_deriv
&& derivs_arg
) {
719 LLVMValueRef derivs
[4];
722 /* Convert cube derivatives to 2D derivatives. */
723 for (axis
= 0; axis
< 2; axis
++) {
724 LLVMValueRef deriv_st
[2];
725 LLVMValueRef deriv_ma
;
727 /* Transform the derivative alongside the texture
728 * coordinate. Mathematically, the correct formula is
729 * as follows. Assume we're projecting onto the +Z face
730 * and denote by dx/dh the derivative of the (original)
731 * X texture coordinate with respect to horizontal
732 * window coordinates. The projection onto the +Z face
737 * Then df/dh = df/dx * dx/dh + df/dz * dz/dh
738 * = 1/z * dx/dh - x/z * 1/z * dz/dh.
740 * This motivatives the implementation below.
742 * Whether this actually gives the expected results for
743 * apps that might feed in derivatives obtained via
744 * finite differences is anyone's guess. The OpenGL spec
745 * seems awfully quiet about how textureGrad for cube
746 * maps should be handled.
748 build_cube_select(ctx
, &selcoords
, &derivs_arg
[axis
* 3],
749 deriv_st
, &deriv_ma
);
751 deriv_ma
= LLVMBuildFMul(builder
, deriv_ma
, invma
, "");
753 for (int i
= 0; i
< 2; ++i
)
754 derivs
[axis
* 2 + i
] =
755 LLVMBuildFSub(builder
,
756 LLVMBuildFMul(builder
, deriv_st
[i
], invma
, ""),
757 LLVMBuildFMul(builder
, deriv_ma
, coords
[i
], ""), "");
760 memcpy(derivs_arg
, derivs
, sizeof(derivs
));
763 /* Shift the texture coordinate. This must be applied after the
764 * derivative calculation.
766 for (int i
= 0; i
< 2; ++i
)
767 coords
[i
] = LLVMBuildFAdd(builder
, coords
[i
], LLVMConstReal(ctx
->f32
, 1.5), "");
770 /* for cube arrays coord.z = coord.w(array_index) * 8 + face */
771 /* coords_arg.w component - array_index for cube arrays */
772 coords
[2] = ac_build_fmad(ctx
, coords_arg
[3], LLVMConstReal(ctx
->f32
, 8.0), coords
[2]);
775 memcpy(coords_arg
, coords
, sizeof(coords
));
780 ac_build_fs_interp(struct ac_llvm_context
*ctx
,
781 LLVMValueRef llvm_chan
,
782 LLVMValueRef attr_number
,
787 LLVMValueRef args
[5];
792 args
[2] = attr_number
;
795 p1
= ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p1",
796 ctx
->f32
, args
, 4, AC_FUNC_ATTR_READNONE
);
801 args
[3] = attr_number
;
804 return ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p2",
805 ctx
->f32
, args
, 5, AC_FUNC_ATTR_READNONE
);
809 ac_build_fs_interp_mov(struct ac_llvm_context
*ctx
,
810 LLVMValueRef parameter
,
811 LLVMValueRef llvm_chan
,
812 LLVMValueRef attr_number
,
815 LLVMValueRef args
[4];
819 args
[2] = attr_number
;
822 return ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.mov",
823 ctx
->f32
, args
, 4, AC_FUNC_ATTR_READNONE
);
827 ac_build_gep0(struct ac_llvm_context
*ctx
,
828 LLVMValueRef base_ptr
,
831 LLVMValueRef indices
[2] = {
835 return LLVMBuildGEP(ctx
->builder
, base_ptr
, indices
, 2, "");
838 LLVMValueRef
ac_build_pointer_add(struct ac_llvm_context
*ctx
, LLVMValueRef ptr
,
841 return LLVMBuildPointerCast(ctx
->builder
,
842 ac_build_gep0(ctx
, ptr
, index
),
843 LLVMTypeOf(ptr
), "");
847 ac_build_indexed_store(struct ac_llvm_context
*ctx
,
848 LLVMValueRef base_ptr
, LLVMValueRef index
,
851 LLVMBuildStore(ctx
->builder
, value
,
852 ac_build_gep0(ctx
, base_ptr
, index
));
856 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad.
857 * It's equivalent to doing a load from &base_ptr[index].
859 * \param base_ptr Where the array starts.
860 * \param index The element index into the array.
861 * \param uniform Whether the base_ptr and index can be assumed to be
862 * dynamically uniform (i.e. load to an SGPR)
863 * \param invariant Whether the load is invariant (no other opcodes affect it)
864 * \param no_unsigned_wraparound
865 * For all possible re-associations and re-distributions of an expression
866 * "base_ptr + index * elemsize" into "addr + offset" (excluding GEPs
867 * without inbounds in base_ptr), this parameter is true if "addr + offset"
868 * does not result in an unsigned integer wraparound. This is used for
869 * optimal code generation of 32-bit pointer arithmetic.
871 * For example, a 32-bit immediate offset that causes a 32-bit unsigned
872 * integer wraparound can't be an imm offset in s_load_dword, because
873 * the instruction performs "addr + offset" in 64 bits.
875 * Expected usage for bindless textures by chaining GEPs:
876 * // possible unsigned wraparound, don't use InBounds:
877 * ptr1 = LLVMBuildGEP(base_ptr, index);
878 * image = load(ptr1); // becomes "s_load ptr1, 0"
880 * ptr2 = LLVMBuildInBoundsGEP(ptr1, 32 / elemsize);
881 * sampler = load(ptr2); // becomes "s_load ptr1, 32" thanks to InBounds
884 ac_build_load_custom(struct ac_llvm_context
*ctx
, LLVMValueRef base_ptr
,
885 LLVMValueRef index
, bool uniform
, bool invariant
,
886 bool no_unsigned_wraparound
)
888 LLVMValueRef pointer
, result
;
889 LLVMValueRef indices
[2] = {ctx
->i32_0
, index
};
891 if (no_unsigned_wraparound
&&
892 LLVMGetPointerAddressSpace(LLVMTypeOf(base_ptr
)) == AC_CONST_32BIT_ADDR_SPACE
)
893 pointer
= LLVMBuildInBoundsGEP(ctx
->builder
, base_ptr
, indices
, 2, "");
895 pointer
= LLVMBuildGEP(ctx
->builder
, base_ptr
, indices
, 2, "");
898 LLVMSetMetadata(pointer
, ctx
->uniform_md_kind
, ctx
->empty_md
);
899 result
= LLVMBuildLoad(ctx
->builder
, pointer
, "");
901 LLVMSetMetadata(result
, ctx
->invariant_load_md_kind
, ctx
->empty_md
);
905 LLVMValueRef
ac_build_load(struct ac_llvm_context
*ctx
, LLVMValueRef base_ptr
,
908 return ac_build_load_custom(ctx
, base_ptr
, index
, false, false, false);
911 LLVMValueRef
ac_build_load_invariant(struct ac_llvm_context
*ctx
,
912 LLVMValueRef base_ptr
, LLVMValueRef index
)
914 return ac_build_load_custom(ctx
, base_ptr
, index
, false, true, false);
917 /* This assumes that there is no unsigned integer wraparound during the address
918 * computation, excluding all GEPs within base_ptr. */
919 LLVMValueRef
ac_build_load_to_sgpr(struct ac_llvm_context
*ctx
,
920 LLVMValueRef base_ptr
, LLVMValueRef index
)
922 return ac_build_load_custom(ctx
, base_ptr
, index
, true, true, true);
925 /* See ac_build_load_custom() documentation. */
926 LLVMValueRef
ac_build_load_to_sgpr_uint_wraparound(struct ac_llvm_context
*ctx
,
927 LLVMValueRef base_ptr
, LLVMValueRef index
)
929 return ac_build_load_custom(ctx
, base_ptr
, index
, true, true, false);
932 /* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
933 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
934 * or v4i32 (num_channels=3,4).
937 ac_build_buffer_store_dword(struct ac_llvm_context
*ctx
,
940 unsigned num_channels
,
941 LLVMValueRef voffset
,
942 LLVMValueRef soffset
,
943 unsigned inst_offset
,
946 bool writeonly_memory
,
947 bool swizzle_enable_hint
)
949 /* Split 3 channel stores, becase LLVM doesn't support 3-channel
951 if (num_channels
== 3) {
952 LLVMValueRef v
[3], v01
;
954 for (int i
= 0; i
< 3; i
++) {
955 v
[i
] = LLVMBuildExtractElement(ctx
->builder
, vdata
,
956 LLVMConstInt(ctx
->i32
, i
, 0), "");
958 v01
= ac_build_gather_values(ctx
, v
, 2);
960 ac_build_buffer_store_dword(ctx
, rsrc
, v01
, 2, voffset
,
961 soffset
, inst_offset
, glc
, slc
,
962 writeonly_memory
, swizzle_enable_hint
);
963 ac_build_buffer_store_dword(ctx
, rsrc
, v
[2], 1, voffset
,
964 soffset
, inst_offset
+ 8,
966 writeonly_memory
, swizzle_enable_hint
);
970 /* SWIZZLE_ENABLE requires that soffset isn't folded into voffset
971 * (voffset is swizzled, but soffset isn't swizzled).
972 * llvm.amdgcn.buffer.store doesn't have a separate soffset parameter.
974 if (!swizzle_enable_hint
) {
975 LLVMValueRef offset
= soffset
;
977 static const char *types
[] = {"f32", "v2f32", "v4f32"};
980 offset
= LLVMBuildAdd(ctx
->builder
, offset
,
981 LLVMConstInt(ctx
->i32
, inst_offset
, 0), "");
983 offset
= LLVMBuildAdd(ctx
->builder
, offset
, voffset
, "");
985 LLVMValueRef args
[] = {
986 ac_to_float(ctx
, vdata
),
987 LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, ""),
990 LLVMConstInt(ctx
->i1
, glc
, 0),
991 LLVMConstInt(ctx
->i1
, slc
, 0),
995 snprintf(name
, sizeof(name
), "llvm.amdgcn.buffer.store.%s",
996 types
[CLAMP(num_channels
, 1, 3) - 1]);
998 ac_build_intrinsic(ctx
, name
, ctx
->voidt
,
999 args
, ARRAY_SIZE(args
),
1001 AC_FUNC_ATTR_INACCESSIBLE_MEM_ONLY
:
1002 AC_FUNC_ATTR_WRITEONLY
);
1006 static const unsigned dfmt
[] = {
1007 V_008F0C_BUF_DATA_FORMAT_32
,
1008 V_008F0C_BUF_DATA_FORMAT_32_32
,
1009 V_008F0C_BUF_DATA_FORMAT_32_32_32
,
1010 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
1012 static const char *types
[] = {"i32", "v2i32", "v4i32"};
1013 LLVMValueRef args
[] = {
1015 LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, ""),
1017 voffset
? voffset
: ctx
->i32_0
,
1019 LLVMConstInt(ctx
->i32
, inst_offset
, 0),
1020 LLVMConstInt(ctx
->i32
, dfmt
[num_channels
- 1], 0),
1021 LLVMConstInt(ctx
->i32
, V_008F0C_BUF_NUM_FORMAT_UINT
, 0),
1022 LLVMConstInt(ctx
->i1
, glc
, 0),
1023 LLVMConstInt(ctx
->i1
, slc
, 0),
1026 snprintf(name
, sizeof(name
), "llvm.amdgcn.tbuffer.store.%s",
1027 types
[CLAMP(num_channels
, 1, 3) - 1]);
1029 ac_build_intrinsic(ctx
, name
, ctx
->voidt
,
1030 args
, ARRAY_SIZE(args
),
1032 AC_FUNC_ATTR_INACCESSIBLE_MEM_ONLY
:
1033 AC_FUNC_ATTR_WRITEONLY
);
1037 ac_build_buffer_load_common(struct ac_llvm_context
*ctx
,
1039 LLVMValueRef vindex
,
1040 LLVMValueRef voffset
,
1041 unsigned num_channels
,
1047 LLVMValueRef args
[] = {
1048 LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, ""),
1049 vindex
? vindex
: ctx
->i32_0
,
1051 LLVMConstInt(ctx
->i1
, glc
, 0),
1052 LLVMConstInt(ctx
->i1
, slc
, 0)
1054 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
1056 LLVMTypeRef types
[] = {ctx
->f32
, ctx
->v2f32
, ctx
->v4f32
};
1057 const char *type_names
[] = {"f32", "v2f32", "v4f32"};
1061 snprintf(name
, sizeof(name
), "llvm.amdgcn.buffer.load.format.%s",
1064 snprintf(name
, sizeof(name
), "llvm.amdgcn.buffer.load.%s",
1068 return ac_build_intrinsic(ctx
, name
, types
[func
], args
,
1070 ac_get_load_intr_attribs(can_speculate
));
1074 ac_build_buffer_load(struct ac_llvm_context
*ctx
,
1077 LLVMValueRef vindex
,
1078 LLVMValueRef voffset
,
1079 LLVMValueRef soffset
,
1080 unsigned inst_offset
,
1086 LLVMValueRef offset
= LLVMConstInt(ctx
->i32
, inst_offset
, 0);
1088 offset
= LLVMBuildAdd(ctx
->builder
, offset
, voffset
, "");
1090 offset
= LLVMBuildAdd(ctx
->builder
, offset
, soffset
, "");
1092 /* TODO: VI and later generations can use SMEM with GLC=1.*/
1093 if (allow_smem
&& !glc
&& !slc
) {
1094 assert(vindex
== NULL
);
1096 LLVMValueRef result
[8];
1098 for (int i
= 0; i
< num_channels
; i
++) {
1100 offset
= LLVMBuildAdd(ctx
->builder
, offset
,
1101 LLVMConstInt(ctx
->i32
, 4, 0), "");
1103 LLVMValueRef args
[2] = {rsrc
, offset
};
1104 result
[i
] = ac_build_intrinsic(ctx
, "llvm.SI.load.const.v4i32",
1106 AC_FUNC_ATTR_READNONE
|
1107 AC_FUNC_ATTR_LEGACY
);
1109 if (num_channels
== 1)
1112 if (num_channels
== 3)
1113 result
[num_channels
++] = LLVMGetUndef(ctx
->f32
);
1114 return ac_build_gather_values(ctx
, result
, num_channels
);
1117 return ac_build_buffer_load_common(ctx
, rsrc
, vindex
, offset
,
1118 num_channels
, glc
, slc
,
1119 can_speculate
, false);
1122 LLVMValueRef
ac_build_buffer_load_format(struct ac_llvm_context
*ctx
,
1124 LLVMValueRef vindex
,
1125 LLVMValueRef voffset
,
1126 unsigned num_channels
,
1130 return ac_build_buffer_load_common(ctx
, rsrc
, vindex
, voffset
,
1131 num_channels
, glc
, false,
1132 can_speculate
, true);
1135 LLVMValueRef
ac_build_buffer_load_format_gfx9_safe(struct ac_llvm_context
*ctx
,
1137 LLVMValueRef vindex
,
1138 LLVMValueRef voffset
,
1139 unsigned num_channels
,
1143 LLVMValueRef elem_count
= LLVMBuildExtractElement(ctx
->builder
, rsrc
, LLVMConstInt(ctx
->i32
, 2, 0), "");
1144 LLVMValueRef stride
= LLVMBuildExtractElement(ctx
->builder
, rsrc
, ctx
->i32_1
, "");
1145 stride
= LLVMBuildLShr(ctx
->builder
, stride
, LLVMConstInt(ctx
->i32
, 16, 0), "");
1147 LLVMValueRef new_elem_count
= LLVMBuildSelect(ctx
->builder
,
1148 LLVMBuildICmp(ctx
->builder
, LLVMIntUGT
, elem_count
, stride
, ""),
1149 elem_count
, stride
, "");
1151 LLVMValueRef new_rsrc
= LLVMBuildInsertElement(ctx
->builder
, rsrc
, new_elem_count
,
1152 LLVMConstInt(ctx
->i32
, 2, 0), "");
1154 return ac_build_buffer_load_common(ctx
, new_rsrc
, vindex
, voffset
,
1155 num_channels
, glc
, false,
1156 can_speculate
, true);
1160 ac_build_tbuffer_load_short(struct ac_llvm_context
*ctx
,
1162 LLVMValueRef vindex
,
1163 LLVMValueRef voffset
,
1164 LLVMValueRef soffset
,
1165 LLVMValueRef immoffset
)
1167 const char *name
= "llvm.amdgcn.tbuffer.load.i32";
1168 LLVMTypeRef type
= ctx
->i32
;
1169 LLVMValueRef params
[] = {
1175 LLVMConstInt(ctx
->i32
, V_008F0C_BUF_DATA_FORMAT_16
, false),
1176 LLVMConstInt(ctx
->i32
, V_008F0C_BUF_NUM_FORMAT_UINT
, false),
1180 LLVMValueRef res
= ac_build_intrinsic(ctx
, name
, type
, params
, 9, 0);
1181 return LLVMBuildTrunc(ctx
->builder
, res
, ctx
->i16
, "");
1185 * Set range metadata on an instruction. This can only be used on load and
1186 * call instructions. If you know an instruction can only produce the values
1187 * 0, 1, 2, you would do set_range_metadata(value, 0, 3);
1188 * \p lo is the minimum value inclusive.
1189 * \p hi is the maximum value exclusive.
1191 static void set_range_metadata(struct ac_llvm_context
*ctx
,
1192 LLVMValueRef value
, unsigned lo
, unsigned hi
)
1194 LLVMValueRef range_md
, md_args
[2];
1195 LLVMTypeRef type
= LLVMTypeOf(value
);
1196 LLVMContextRef context
= LLVMGetTypeContext(type
);
1198 md_args
[0] = LLVMConstInt(type
, lo
, false);
1199 md_args
[1] = LLVMConstInt(type
, hi
, false);
1200 range_md
= LLVMMDNodeInContext(context
, md_args
, 2);
1201 LLVMSetMetadata(value
, ctx
->range_md_kind
, range_md
);
1205 ac_get_thread_id(struct ac_llvm_context
*ctx
)
1209 LLVMValueRef tid_args
[2];
1210 tid_args
[0] = LLVMConstInt(ctx
->i32
, 0xffffffff, false);
1211 tid_args
[1] = ctx
->i32_0
;
1212 tid_args
[1] = ac_build_intrinsic(ctx
,
1213 "llvm.amdgcn.mbcnt.lo", ctx
->i32
,
1214 tid_args
, 2, AC_FUNC_ATTR_READNONE
);
1216 tid
= ac_build_intrinsic(ctx
, "llvm.amdgcn.mbcnt.hi",
1218 2, AC_FUNC_ATTR_READNONE
);
1219 set_range_metadata(ctx
, tid
, 0, 64);
1224 * SI implements derivatives using the local data store (LDS)
1225 * All writes to the LDS happen in all executing threads at
1226 * the same time. TID is the Thread ID for the current
1227 * thread and is a value between 0 and 63, representing
1228 * the thread's position in the wavefront.
1230 * For the pixel shader threads are grouped into quads of four pixels.
1231 * The TIDs of the pixels of a quad are:
1239 * So, masking the TID with 0xfffffffc yields the TID of the top left pixel
1240 * of the quad, masking with 0xfffffffd yields the TID of the top pixel of
1241 * the current pixel's column, and masking with 0xfffffffe yields the TID
1242 * of the left pixel of the current pixel's row.
1244 * Adding 1 yields the TID of the pixel to the right of the left pixel, and
1245 * adding 2 yields the TID of the pixel below the top pixel.
1248 ac_build_ddxy(struct ac_llvm_context
*ctx
,
1253 LLVMValueRef tl
, trbl
, args
[2];
1254 LLVMValueRef result
;
1256 if (HAVE_LLVM
>= 0x0700) {
1257 unsigned tl_lanes
[4], trbl_lanes
[4];
1259 for (unsigned i
= 0; i
< 4; ++i
) {
1260 tl_lanes
[i
] = i
& mask
;
1261 trbl_lanes
[i
] = (i
& mask
) + idx
;
1264 tl
= ac_build_quad_swizzle(ctx
, val
,
1265 tl_lanes
[0], tl_lanes
[1],
1266 tl_lanes
[2], tl_lanes
[3]);
1267 trbl
= ac_build_quad_swizzle(ctx
, val
,
1268 trbl_lanes
[0], trbl_lanes
[1],
1269 trbl_lanes
[2], trbl_lanes
[3]);
1270 } else if (ctx
->chip_class
>= VI
) {
1271 LLVMValueRef thread_id
, tl_tid
, trbl_tid
;
1272 thread_id
= ac_get_thread_id(ctx
);
1274 tl_tid
= LLVMBuildAnd(ctx
->builder
, thread_id
,
1275 LLVMConstInt(ctx
->i32
, mask
, false), "");
1277 trbl_tid
= LLVMBuildAdd(ctx
->builder
, tl_tid
,
1278 LLVMConstInt(ctx
->i32
, idx
, false), "");
1280 args
[0] = LLVMBuildMul(ctx
->builder
, tl_tid
,
1281 LLVMConstInt(ctx
->i32
, 4, false), "");
1283 tl
= ac_build_intrinsic(ctx
,
1284 "llvm.amdgcn.ds.bpermute", ctx
->i32
,
1286 AC_FUNC_ATTR_READNONE
|
1287 AC_FUNC_ATTR_CONVERGENT
);
1289 args
[0] = LLVMBuildMul(ctx
->builder
, trbl_tid
,
1290 LLVMConstInt(ctx
->i32
, 4, false), "");
1291 trbl
= ac_build_intrinsic(ctx
,
1292 "llvm.amdgcn.ds.bpermute", ctx
->i32
,
1294 AC_FUNC_ATTR_READNONE
|
1295 AC_FUNC_ATTR_CONVERGENT
);
1297 uint32_t masks
[2] = {};
1300 case AC_TID_MASK_TOP_LEFT
:
1308 case AC_TID_MASK_TOP
:
1312 case AC_TID_MASK_LEFT
:
1321 args
[1] = LLVMConstInt(ctx
->i32
, masks
[0], false);
1323 tl
= ac_build_intrinsic(ctx
,
1324 "llvm.amdgcn.ds.swizzle", ctx
->i32
,
1326 AC_FUNC_ATTR_READNONE
|
1327 AC_FUNC_ATTR_CONVERGENT
);
1329 args
[1] = LLVMConstInt(ctx
->i32
, masks
[1], false);
1330 trbl
= ac_build_intrinsic(ctx
,
1331 "llvm.amdgcn.ds.swizzle", ctx
->i32
,
1333 AC_FUNC_ATTR_READNONE
|
1334 AC_FUNC_ATTR_CONVERGENT
);
1337 tl
= LLVMBuildBitCast(ctx
->builder
, tl
, ctx
->f32
, "");
1338 trbl
= LLVMBuildBitCast(ctx
->builder
, trbl
, ctx
->f32
, "");
1339 result
= LLVMBuildFSub(ctx
->builder
, trbl
, tl
, "");
1341 if (HAVE_LLVM
>= 0x0700) {
1342 result
= ac_build_intrinsic(ctx
,
1343 "llvm.amdgcn.wqm.f32", ctx
->f32
,
1351 ac_build_sendmsg(struct ac_llvm_context
*ctx
,
1353 LLVMValueRef wave_id
)
1355 LLVMValueRef args
[2];
1356 args
[0] = LLVMConstInt(ctx
->i32
, msg
, false);
1358 ac_build_intrinsic(ctx
, "llvm.amdgcn.s.sendmsg", ctx
->voidt
, args
, 2, 0);
1362 ac_build_imsb(struct ac_llvm_context
*ctx
,
1364 LLVMTypeRef dst_type
)
1366 LLVMValueRef msb
= ac_build_intrinsic(ctx
, "llvm.amdgcn.sffbh.i32",
1368 AC_FUNC_ATTR_READNONE
);
1370 /* The HW returns the last bit index from MSB, but NIR/TGSI wants
1371 * the index from LSB. Invert it by doing "31 - msb". */
1372 msb
= LLVMBuildSub(ctx
->builder
, LLVMConstInt(ctx
->i32
, 31, false),
1375 LLVMValueRef all_ones
= LLVMConstInt(ctx
->i32
, -1, true);
1376 LLVMValueRef cond
= LLVMBuildOr(ctx
->builder
,
1377 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
1378 arg
, ctx
->i32_0
, ""),
1379 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
1380 arg
, all_ones
, ""), "");
1382 return LLVMBuildSelect(ctx
->builder
, cond
, all_ones
, msb
, "");
1386 ac_build_umsb(struct ac_llvm_context
*ctx
,
1388 LLVMTypeRef dst_type
)
1390 const char *intrin_name
;
1392 LLVMValueRef highest_bit
;
1395 if (ac_get_elem_bits(ctx
, LLVMTypeOf(arg
)) == 64) {
1396 intrin_name
= "llvm.ctlz.i64";
1398 highest_bit
= LLVMConstInt(ctx
->i64
, 63, false);
1401 intrin_name
= "llvm.ctlz.i32";
1403 highest_bit
= LLVMConstInt(ctx
->i32
, 31, false);
1407 LLVMValueRef params
[2] = {
1412 LLVMValueRef msb
= ac_build_intrinsic(ctx
, intrin_name
, type
,
1414 AC_FUNC_ATTR_READNONE
);
1416 /* The HW returns the last bit index from MSB, but TGSI/NIR wants
1417 * the index from LSB. Invert it by doing "31 - msb". */
1418 msb
= LLVMBuildSub(ctx
->builder
, highest_bit
, msb
, "");
1419 msb
= LLVMBuildTruncOrBitCast(ctx
->builder
, msb
, ctx
->i32
, "");
1421 /* check for zero */
1422 return LLVMBuildSelect(ctx
->builder
,
1423 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, arg
, zero
, ""),
1424 LLVMConstInt(ctx
->i32
, -1, true), msb
, "");
1427 LLVMValueRef
ac_build_fmin(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
1430 LLVMValueRef args
[2] = {a
, b
};
1431 return ac_build_intrinsic(ctx
, "llvm.minnum.f32", ctx
->f32
, args
, 2,
1432 AC_FUNC_ATTR_READNONE
);
1435 LLVMValueRef
ac_build_fmax(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
1438 LLVMValueRef args
[2] = {a
, b
};
1439 return ac_build_intrinsic(ctx
, "llvm.maxnum.f32", ctx
->f32
, args
, 2,
1440 AC_FUNC_ATTR_READNONE
);
1443 LLVMValueRef
ac_build_imin(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
1446 LLVMValueRef cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSLE
, a
, b
, "");
1447 return LLVMBuildSelect(ctx
->builder
, cmp
, a
, b
, "");
1450 LLVMValueRef
ac_build_imax(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
1453 LLVMValueRef cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGT
, a
, b
, "");
1454 return LLVMBuildSelect(ctx
->builder
, cmp
, a
, b
, "");
1457 LLVMValueRef
ac_build_umin(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
1460 LLVMValueRef cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntULE
, a
, b
, "");
1461 return LLVMBuildSelect(ctx
->builder
, cmp
, a
, b
, "");
1464 LLVMValueRef
ac_build_clamp(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
1466 return ac_build_fmin(ctx
, ac_build_fmax(ctx
, value
, ctx
->f32_0
),
1470 void ac_build_export(struct ac_llvm_context
*ctx
, struct ac_export_args
*a
)
1472 LLVMValueRef args
[9];
1474 args
[0] = LLVMConstInt(ctx
->i32
, a
->target
, 0);
1475 args
[1] = LLVMConstInt(ctx
->i32
, a
->enabled_channels
, 0);
1478 LLVMTypeRef i16
= LLVMInt16TypeInContext(ctx
->context
);
1479 LLVMTypeRef v2i16
= LLVMVectorType(i16
, 2);
1481 args
[2] = LLVMBuildBitCast(ctx
->builder
, a
->out
[0],
1483 args
[3] = LLVMBuildBitCast(ctx
->builder
, a
->out
[1],
1485 args
[4] = LLVMConstInt(ctx
->i1
, a
->done
, 0);
1486 args
[5] = LLVMConstInt(ctx
->i1
, a
->valid_mask
, 0);
1488 ac_build_intrinsic(ctx
, "llvm.amdgcn.exp.compr.v2i16",
1489 ctx
->voidt
, args
, 6, 0);
1491 args
[2] = a
->out
[0];
1492 args
[3] = a
->out
[1];
1493 args
[4] = a
->out
[2];
1494 args
[5] = a
->out
[3];
1495 args
[6] = LLVMConstInt(ctx
->i1
, a
->done
, 0);
1496 args
[7] = LLVMConstInt(ctx
->i1
, a
->valid_mask
, 0);
1498 ac_build_intrinsic(ctx
, "llvm.amdgcn.exp.f32",
1499 ctx
->voidt
, args
, 8, 0);
1503 void ac_build_export_null(struct ac_llvm_context
*ctx
)
1505 struct ac_export_args args
;
1507 args
.enabled_channels
= 0x0; /* enabled channels */
1508 args
.valid_mask
= 1; /* whether the EXEC mask is valid */
1509 args
.done
= 1; /* DONE bit */
1510 args
.target
= V_008DFC_SQ_EXP_NULL
;
1511 args
.compr
= 0; /* COMPR flag (0 = 32-bit export) */
1512 args
.out
[0] = LLVMGetUndef(ctx
->f32
); /* R */
1513 args
.out
[1] = LLVMGetUndef(ctx
->f32
); /* G */
1514 args
.out
[2] = LLVMGetUndef(ctx
->f32
); /* B */
1515 args
.out
[3] = LLVMGetUndef(ctx
->f32
); /* A */
1517 ac_build_export(ctx
, &args
);
1520 static unsigned ac_num_coords(enum ac_image_dim dim
)
1526 case ac_image_1darray
:
1530 case ac_image_2darray
:
1531 case ac_image_2dmsaa
:
1533 case ac_image_2darraymsaa
:
1536 unreachable("ac_num_coords: bad dim");
1540 static unsigned ac_num_derivs(enum ac_image_dim dim
)
1544 case ac_image_1darray
:
1547 case ac_image_2darray
:
1552 case ac_image_2dmsaa
:
1553 case ac_image_2darraymsaa
:
1555 unreachable("derivatives not supported");
1559 static const char *get_atomic_name(enum ac_atomic_op op
)
1562 case ac_atomic_swap
: return "swap";
1563 case ac_atomic_add
: return "add";
1564 case ac_atomic_sub
: return "sub";
1565 case ac_atomic_smin
: return "smin";
1566 case ac_atomic_umin
: return "umin";
1567 case ac_atomic_smax
: return "smax";
1568 case ac_atomic_umax
: return "umax";
1569 case ac_atomic_and
: return "and";
1570 case ac_atomic_or
: return "or";
1571 case ac_atomic_xor
: return "xor";
1573 unreachable("bad atomic op");
1576 /* LLVM 6 and older */
1577 static LLVMValueRef
ac_build_image_opcode_llvm6(struct ac_llvm_context
*ctx
,
1578 struct ac_image_args
*a
)
1580 LLVMValueRef args
[16];
1581 LLVMTypeRef retty
= ctx
->v4f32
;
1582 const char *name
= NULL
;
1583 const char *atomic_subop
= "";
1584 char intr_name
[128], coords_type
[64];
1586 bool sample
= a
->opcode
== ac_image_sample
||
1587 a
->opcode
== ac_image_gather4
||
1588 a
->opcode
== ac_image_get_lod
;
1589 bool atomic
= a
->opcode
== ac_image_atomic
||
1590 a
->opcode
== ac_image_atomic_cmpswap
;
1591 bool da
= a
->dim
== ac_image_cube
||
1592 a
->dim
== ac_image_1darray
||
1593 a
->dim
== ac_image_2darray
||
1594 a
->dim
== ac_image_2darraymsaa
;
1595 if (a
->opcode
== ac_image_get_lod
)
1598 unsigned num_coords
=
1599 a
->opcode
!= ac_image_get_resinfo
? ac_num_coords(a
->dim
) : 0;
1601 unsigned num_addr
= 0;
1603 if (a
->opcode
== ac_image_get_lod
) {
1605 case ac_image_1darray
:
1608 case ac_image_2darray
:
1618 args
[num_addr
++] = ac_to_integer(ctx
, a
->offset
);
1620 args
[num_addr
++] = ac_to_integer(ctx
, a
->bias
);
1622 args
[num_addr
++] = ac_to_integer(ctx
, a
->compare
);
1624 unsigned num_derivs
= ac_num_derivs(a
->dim
);
1625 for (unsigned i
= 0; i
< num_derivs
; ++i
)
1626 args
[num_addr
++] = ac_to_integer(ctx
, a
->derivs
[i
]);
1628 for (unsigned i
= 0; i
< num_coords
; ++i
)
1629 args
[num_addr
++] = ac_to_integer(ctx
, a
->coords
[i
]);
1631 args
[num_addr
++] = ac_to_integer(ctx
, a
->lod
);
1633 unsigned pad_goal
= util_next_power_of_two(num_addr
);
1634 while (num_addr
< pad_goal
)
1635 args
[num_addr
++] = LLVMGetUndef(ctx
->i32
);
1637 addr
= ac_build_gather_values(ctx
, args
, num_addr
);
1639 unsigned num_args
= 0;
1640 if (atomic
|| a
->opcode
== ac_image_store
|| a
->opcode
== ac_image_store_mip
) {
1641 args
[num_args
++] = a
->data
[0];
1642 if (a
->opcode
== ac_image_atomic_cmpswap
)
1643 args
[num_args
++] = a
->data
[1];
1646 unsigned coords_arg
= num_args
;
1648 args
[num_args
++] = ac_to_float(ctx
, addr
);
1650 args
[num_args
++] = ac_to_integer(ctx
, addr
);
1652 args
[num_args
++] = a
->resource
;
1654 args
[num_args
++] = a
->sampler
;
1656 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->dmask
, 0);
1658 args
[num_args
++] = LLVMConstInt(ctx
->i1
, a
->unorm
, 0);
1659 args
[num_args
++] = a
->cache_policy
& ac_glc
? ctx
->i1true
: ctx
->i1false
;
1660 args
[num_args
++] = a
->cache_policy
& ac_slc
? ctx
->i1true
: ctx
->i1false
;
1661 args
[num_args
++] = ctx
->i1false
; /* lwe */
1662 args
[num_args
++] = LLVMConstInt(ctx
->i1
, da
, 0);
1664 args
[num_args
++] = ctx
->i1false
; /* r128 */
1665 args
[num_args
++] = LLVMConstInt(ctx
->i1
, da
, 0);
1666 args
[num_args
++] = a
->cache_policy
& ac_slc
? ctx
->i1true
: ctx
->i1false
;
1669 switch (a
->opcode
) {
1670 case ac_image_sample
:
1671 name
= "llvm.amdgcn.image.sample";
1673 case ac_image_gather4
:
1674 name
= "llvm.amdgcn.image.gather4";
1677 name
= "llvm.amdgcn.image.load";
1679 case ac_image_load_mip
:
1680 name
= "llvm.amdgcn.image.load.mip";
1682 case ac_image_store
:
1683 name
= "llvm.amdgcn.image.store";
1686 case ac_image_store_mip
:
1687 name
= "llvm.amdgcn.image.store.mip";
1690 case ac_image_atomic
:
1691 case ac_image_atomic_cmpswap
:
1692 name
= "llvm.amdgcn.image.atomic.";
1694 if (a
->opcode
== ac_image_atomic_cmpswap
) {
1695 atomic_subop
= "cmpswap";
1697 atomic_subop
= get_atomic_name(a
->atomic
);
1700 case ac_image_get_lod
:
1701 name
= "llvm.amdgcn.image.getlod";
1703 case ac_image_get_resinfo
:
1704 name
= "llvm.amdgcn.image.getresinfo";
1707 unreachable("invalid image opcode");
1710 ac_build_type_name_for_intr(LLVMTypeOf(args
[coords_arg
]), coords_type
,
1711 sizeof(coords_type
));
1714 snprintf(intr_name
, sizeof(intr_name
), "llvm.amdgcn.image.atomic.%s.%s",
1715 atomic_subop
, coords_type
);
1718 a
->lod
&& (a
->opcode
== ac_image_sample
|| a
->opcode
== ac_image_gather4
);
1720 snprintf(intr_name
, sizeof(intr_name
), "%s%s%s%s.v4f32.%s.v8i32",
1722 a
->compare
? ".c" : "",
1725 a
->derivs
[0] ? ".d" :
1726 a
->level_zero
? ".lz" : "",
1727 a
->offset
? ".o" : "",
1731 LLVMValueRef result
=
1732 ac_build_intrinsic(ctx
, intr_name
, retty
, args
, num_args
,
1734 if (!sample
&& retty
== ctx
->v4f32
) {
1735 result
= LLVMBuildBitCast(ctx
->builder
, result
,
1741 LLVMValueRef
ac_build_image_opcode(struct ac_llvm_context
*ctx
,
1742 struct ac_image_args
*a
)
1744 const char *overload
[3] = { "", "", "" };
1745 unsigned num_overloads
= 0;
1746 LLVMValueRef args
[18];
1747 unsigned num_args
= 0;
1748 enum ac_image_dim dim
= a
->dim
;
1750 assert(!a
->lod
|| a
->lod
== ctx
->i32_0
|| a
->lod
== ctx
->f32_0
||
1752 assert((a
->opcode
!= ac_image_get_resinfo
&& a
->opcode
!= ac_image_load_mip
&&
1753 a
->opcode
!= ac_image_store_mip
) ||
1755 assert(a
->opcode
== ac_image_sample
|| a
->opcode
== ac_image_gather4
||
1756 (!a
->compare
&& !a
->offset
));
1757 assert((a
->opcode
== ac_image_sample
|| a
->opcode
== ac_image_gather4
||
1758 a
->opcode
== ac_image_get_lod
) ||
1760 assert((a
->bias
? 1 : 0) +
1762 (a
->level_zero
? 1 : 0) +
1763 (a
->derivs
[0] ? 1 : 0) <= 1);
1765 if (HAVE_LLVM
< 0x0700)
1766 return ac_build_image_opcode_llvm6(ctx
, a
);
1768 if (a
->opcode
== ac_image_get_lod
) {
1770 case ac_image_1darray
:
1773 case ac_image_2darray
:
1782 bool sample
= a
->opcode
== ac_image_sample
||
1783 a
->opcode
== ac_image_gather4
||
1784 a
->opcode
== ac_image_get_lod
;
1785 bool atomic
= a
->opcode
== ac_image_atomic
||
1786 a
->opcode
== ac_image_atomic_cmpswap
;
1787 LLVMTypeRef coord_type
= sample
? ctx
->f32
: ctx
->i32
;
1789 if (atomic
|| a
->opcode
== ac_image_store
|| a
->opcode
== ac_image_store_mip
) {
1790 args
[num_args
++] = a
->data
[0];
1791 if (a
->opcode
== ac_image_atomic_cmpswap
)
1792 args
[num_args
++] = a
->data
[1];
1796 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->dmask
, false);
1799 args
[num_args
++] = ac_to_integer(ctx
, a
->offset
);
1801 args
[num_args
++] = ac_to_float(ctx
, a
->bias
);
1802 overload
[num_overloads
++] = ".f32";
1805 args
[num_args
++] = ac_to_float(ctx
, a
->compare
);
1807 unsigned count
= ac_num_derivs(dim
);
1808 for (unsigned i
= 0; i
< count
; ++i
)
1809 args
[num_args
++] = ac_to_float(ctx
, a
->derivs
[i
]);
1810 overload
[num_overloads
++] = ".f32";
1812 unsigned num_coords
=
1813 a
->opcode
!= ac_image_get_resinfo
? ac_num_coords(dim
) : 0;
1814 for (unsigned i
= 0; i
< num_coords
; ++i
)
1815 args
[num_args
++] = LLVMBuildBitCast(ctx
->builder
, a
->coords
[i
], coord_type
, "");
1817 args
[num_args
++] = LLVMBuildBitCast(ctx
->builder
, a
->lod
, coord_type
, "");
1818 overload
[num_overloads
++] = sample
? ".f32" : ".i32";
1820 args
[num_args
++] = a
->resource
;
1822 args
[num_args
++] = a
->sampler
;
1823 args
[num_args
++] = LLVMConstInt(ctx
->i1
, a
->unorm
, false);
1826 args
[num_args
++] = ctx
->i32_0
; /* texfailctrl */
1827 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->cache_policy
, false);
1830 const char *atomic_subop
= "";
1831 switch (a
->opcode
) {
1832 case ac_image_sample
: name
= "sample"; break;
1833 case ac_image_gather4
: name
= "gather4"; break;
1834 case ac_image_load
: name
= "load"; break;
1835 case ac_image_load_mip
: name
= "load.mip"; break;
1836 case ac_image_store
: name
= "store"; break;
1837 case ac_image_store_mip
: name
= "store.mip"; break;
1838 case ac_image_atomic
:
1840 atomic_subop
= get_atomic_name(a
->atomic
);
1842 case ac_image_atomic_cmpswap
:
1844 atomic_subop
= "cmpswap";
1846 case ac_image_get_lod
: name
= "getlod"; break;
1847 case ac_image_get_resinfo
: name
= "getresinfo"; break;
1848 default: unreachable("invalid image opcode");
1851 const char *dimname
;
1853 case ac_image_1d
: dimname
= "1d"; break;
1854 case ac_image_2d
: dimname
= "2d"; break;
1855 case ac_image_3d
: dimname
= "3d"; break;
1856 case ac_image_cube
: dimname
= "cube"; break;
1857 case ac_image_1darray
: dimname
= "1darray"; break;
1858 case ac_image_2darray
: dimname
= "2darray"; break;
1859 case ac_image_2dmsaa
: dimname
= "2dmsaa"; break;
1860 case ac_image_2darraymsaa
: dimname
= "2darraymsaa"; break;
1861 default: unreachable("invalid dim");
1865 a
->lod
&& (a
->opcode
== ac_image_sample
|| a
->opcode
== ac_image_gather4
);
1867 snprintf(intr_name
, sizeof(intr_name
),
1868 "llvm.amdgcn.image.%s%s" /* base name */
1869 "%s%s%s" /* sample/gather modifiers */
1870 ".%s.%s%s%s%s", /* dimension and type overloads */
1872 a
->compare
? ".c" : "",
1875 a
->derivs
[0] ? ".d" :
1876 a
->level_zero
? ".lz" : "",
1877 a
->offset
? ".o" : "",
1879 atomic
? "i32" : "v4f32",
1880 overload
[0], overload
[1], overload
[2]);
1885 else if (a
->opcode
== ac_image_store
|| a
->opcode
== ac_image_store_mip
)
1890 LLVMValueRef result
=
1891 ac_build_intrinsic(ctx
, intr_name
, retty
, args
, num_args
,
1893 if (!sample
&& retty
== ctx
->v4f32
) {
1894 result
= LLVMBuildBitCast(ctx
->builder
, result
,
1900 LLVMValueRef
ac_build_cvt_pkrtz_f16(struct ac_llvm_context
*ctx
,
1901 LLVMValueRef args
[2])
1904 LLVMVectorType(LLVMHalfTypeInContext(ctx
->context
), 2);
1906 return ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pkrtz", v2f16
,
1907 args
, 2, AC_FUNC_ATTR_READNONE
);
1910 LLVMValueRef
ac_build_cvt_pknorm_i16(struct ac_llvm_context
*ctx
,
1911 LLVMValueRef args
[2])
1914 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pknorm.i16",
1915 ctx
->v2i16
, args
, 2,
1916 AC_FUNC_ATTR_READNONE
);
1917 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
1920 LLVMValueRef
ac_build_cvt_pknorm_u16(struct ac_llvm_context
*ctx
,
1921 LLVMValueRef args
[2])
1924 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pknorm.u16",
1925 ctx
->v2i16
, args
, 2,
1926 AC_FUNC_ATTR_READNONE
);
1927 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
1930 /* The 8-bit and 10-bit clamping is for HW workarounds. */
1931 LLVMValueRef
ac_build_cvt_pk_i16(struct ac_llvm_context
*ctx
,
1932 LLVMValueRef args
[2], unsigned bits
, bool hi
)
1934 assert(bits
== 8 || bits
== 10 || bits
== 16);
1936 LLVMValueRef max_rgb
= LLVMConstInt(ctx
->i32
,
1937 bits
== 8 ? 127 : bits
== 10 ? 511 : 32767, 0);
1938 LLVMValueRef min_rgb
= LLVMConstInt(ctx
->i32
,
1939 bits
== 8 ? -128 : bits
== 10 ? -512 : -32768, 0);
1940 LLVMValueRef max_alpha
=
1941 bits
!= 10 ? max_rgb
: ctx
->i32_1
;
1942 LLVMValueRef min_alpha
=
1943 bits
!= 10 ? min_rgb
: LLVMConstInt(ctx
->i32
, -2, 0);
1947 for (int i
= 0; i
< 2; i
++) {
1948 bool alpha
= hi
&& i
== 1;
1949 args
[i
] = ac_build_imin(ctx
, args
[i
],
1950 alpha
? max_alpha
: max_rgb
);
1951 args
[i
] = ac_build_imax(ctx
, args
[i
],
1952 alpha
? min_alpha
: min_rgb
);
1957 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pk.i16",
1958 ctx
->v2i16
, args
, 2,
1959 AC_FUNC_ATTR_READNONE
);
1960 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
1963 /* The 8-bit and 10-bit clamping is for HW workarounds. */
1964 LLVMValueRef
ac_build_cvt_pk_u16(struct ac_llvm_context
*ctx
,
1965 LLVMValueRef args
[2], unsigned bits
, bool hi
)
1967 assert(bits
== 8 || bits
== 10 || bits
== 16);
1969 LLVMValueRef max_rgb
= LLVMConstInt(ctx
->i32
,
1970 bits
== 8 ? 255 : bits
== 10 ? 1023 : 65535, 0);
1971 LLVMValueRef max_alpha
=
1972 bits
!= 10 ? max_rgb
: LLVMConstInt(ctx
->i32
, 3, 0);
1976 for (int i
= 0; i
< 2; i
++) {
1977 bool alpha
= hi
&& i
== 1;
1978 args
[i
] = ac_build_umin(ctx
, args
[i
],
1979 alpha
? max_alpha
: max_rgb
);
1984 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pk.u16",
1985 ctx
->v2i16
, args
, 2,
1986 AC_FUNC_ATTR_READNONE
);
1987 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
1990 LLVMValueRef
ac_build_wqm_vote(struct ac_llvm_context
*ctx
, LLVMValueRef i1
)
1992 return ac_build_intrinsic(ctx
, "llvm.amdgcn.wqm.vote", ctx
->i1
,
1993 &i1
, 1, AC_FUNC_ATTR_READNONE
);
1996 void ac_build_kill_if_false(struct ac_llvm_context
*ctx
, LLVMValueRef i1
)
1998 ac_build_intrinsic(ctx
, "llvm.amdgcn.kill", ctx
->voidt
,
2002 LLVMValueRef
ac_build_bfe(struct ac_llvm_context
*ctx
, LLVMValueRef input
,
2003 LLVMValueRef offset
, LLVMValueRef width
,
2006 LLVMValueRef args
[] = {
2012 return ac_build_intrinsic(ctx
,
2013 is_signed
? "llvm.amdgcn.sbfe.i32" :
2014 "llvm.amdgcn.ubfe.i32",
2016 AC_FUNC_ATTR_READNONE
);
2019 LLVMValueRef
ac_build_imad(struct ac_llvm_context
*ctx
, LLVMValueRef s0
,
2020 LLVMValueRef s1
, LLVMValueRef s2
)
2022 return LLVMBuildAdd(ctx
->builder
,
2023 LLVMBuildMul(ctx
->builder
, s0
, s1
, ""), s2
, "");
2026 LLVMValueRef
ac_build_fmad(struct ac_llvm_context
*ctx
, LLVMValueRef s0
,
2027 LLVMValueRef s1
, LLVMValueRef s2
)
2029 return LLVMBuildFAdd(ctx
->builder
,
2030 LLVMBuildFMul(ctx
->builder
, s0
, s1
, ""), s2
, "");
2033 void ac_build_waitcnt(struct ac_llvm_context
*ctx
, unsigned simm16
)
2035 LLVMValueRef args
[1] = {
2036 LLVMConstInt(ctx
->i32
, simm16
, false),
2038 ac_build_intrinsic(ctx
, "llvm.amdgcn.s.waitcnt",
2039 ctx
->voidt
, args
, 1, 0);
2042 LLVMValueRef
ac_build_fract(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
2048 if (bitsize
== 32) {
2049 intr
= "llvm.floor.f32";
2052 intr
= "llvm.floor.f64";
2056 LLVMValueRef params
[] = {
2059 LLVMValueRef floor
= ac_build_intrinsic(ctx
, intr
, type
, params
, 1,
2060 AC_FUNC_ATTR_READNONE
);
2061 return LLVMBuildFSub(ctx
->builder
, src0
, floor
, "");
2064 LLVMValueRef
ac_build_isign(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
2067 LLVMValueRef cmp
, val
, zero
, one
;
2070 if (bitsize
== 32) {
2080 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGT
, src0
, zero
, "");
2081 val
= LLVMBuildSelect(ctx
->builder
, cmp
, one
, src0
, "");
2082 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGE
, val
, zero
, "");
2083 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstInt(type
, -1, true), "");
2087 LLVMValueRef
ac_build_fsign(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
2090 LLVMValueRef cmp
, val
, zero
, one
;
2093 if (bitsize
== 32) {
2103 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGT
, src0
, zero
, "");
2104 val
= LLVMBuildSelect(ctx
->builder
, cmp
, one
, src0
, "");
2105 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGE
, val
, zero
, "");
2106 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstReal(type
, -1.0), "");
2110 LLVMValueRef
ac_build_bit_count(struct ac_llvm_context
*ctx
, LLVMValueRef src0
)
2112 LLVMValueRef result
;
2115 bitsize
= ac_get_elem_bits(ctx
, LLVMTypeOf(src0
));
2119 result
= ac_build_intrinsic(ctx
, "llvm.ctpop.i64", ctx
->i64
,
2120 (LLVMValueRef
[]) { src0
}, 1,
2121 AC_FUNC_ATTR_READNONE
);
2123 result
= LLVMBuildTrunc(ctx
->builder
, result
, ctx
->i32
, "");
2126 result
= ac_build_intrinsic(ctx
, "llvm.ctpop.i32", ctx
->i32
,
2127 (LLVMValueRef
[]) { src0
}, 1,
2128 AC_FUNC_ATTR_READNONE
);
2131 unreachable(!"invalid bitsize");
2138 LLVMValueRef
ac_build_bitfield_reverse(struct ac_llvm_context
*ctx
,
2141 LLVMValueRef result
;
2144 bitsize
= ac_get_elem_bits(ctx
, LLVMTypeOf(src0
));
2148 result
= ac_build_intrinsic(ctx
, "llvm.bitreverse.i32", ctx
->i32
,
2149 (LLVMValueRef
[]) { src0
}, 1,
2150 AC_FUNC_ATTR_READNONE
);
2153 unreachable(!"invalid bitsize");
2160 #define AC_EXP_TARGET 0
2161 #define AC_EXP_ENABLED_CHANNELS 1
2162 #define AC_EXP_OUT0 2
2170 struct ac_vs_exp_chan
2174 enum ac_ir_type type
;
2177 struct ac_vs_exp_inst
{
2180 struct ac_vs_exp_chan chan
[4];
2183 struct ac_vs_exports
{
2185 struct ac_vs_exp_inst exp
[VARYING_SLOT_MAX
];
2188 /* Return true if the PARAM export has been eliminated. */
2189 static bool ac_eliminate_const_output(uint8_t *vs_output_param_offset
,
2190 uint32_t num_outputs
,
2191 struct ac_vs_exp_inst
*exp
)
2193 unsigned i
, default_val
; /* SPI_PS_INPUT_CNTL_i.DEFAULT_VAL */
2194 bool is_zero
[4] = {}, is_one
[4] = {};
2196 for (i
= 0; i
< 4; i
++) {
2197 /* It's a constant expression. Undef outputs are eliminated too. */
2198 if (exp
->chan
[i
].type
== AC_IR_UNDEF
) {
2201 } else if (exp
->chan
[i
].type
== AC_IR_CONST
) {
2202 if (exp
->chan
[i
].const_float
== 0)
2204 else if (exp
->chan
[i
].const_float
== 1)
2207 return false; /* other constant */
2212 /* Only certain combinations of 0 and 1 can be eliminated. */
2213 if (is_zero
[0] && is_zero
[1] && is_zero
[2])
2214 default_val
= is_zero
[3] ? 0 : 1;
2215 else if (is_one
[0] && is_one
[1] && is_one
[2])
2216 default_val
= is_zero
[3] ? 2 : 3;
2220 /* The PARAM export can be represented as DEFAULT_VAL. Kill it. */
2221 LLVMInstructionEraseFromParent(exp
->inst
);
2223 /* Change OFFSET to DEFAULT_VAL. */
2224 for (i
= 0; i
< num_outputs
; i
++) {
2225 if (vs_output_param_offset
[i
] == exp
->offset
) {
2226 vs_output_param_offset
[i
] =
2227 AC_EXP_PARAM_DEFAULT_VAL_0000
+ default_val
;
2234 static bool ac_eliminate_duplicated_output(struct ac_llvm_context
*ctx
,
2235 uint8_t *vs_output_param_offset
,
2236 uint32_t num_outputs
,
2237 struct ac_vs_exports
*processed
,
2238 struct ac_vs_exp_inst
*exp
)
2240 unsigned p
, copy_back_channels
= 0;
2242 /* See if the output is already in the list of processed outputs.
2243 * The LLVMValueRef comparison relies on SSA.
2245 for (p
= 0; p
< processed
->num
; p
++) {
2246 bool different
= false;
2248 for (unsigned j
= 0; j
< 4; j
++) {
2249 struct ac_vs_exp_chan
*c1
= &processed
->exp
[p
].chan
[j
];
2250 struct ac_vs_exp_chan
*c2
= &exp
->chan
[j
];
2252 /* Treat undef as a match. */
2253 if (c2
->type
== AC_IR_UNDEF
)
2256 /* If c1 is undef but c2 isn't, we can copy c2 to c1
2257 * and consider the instruction duplicated.
2259 if (c1
->type
== AC_IR_UNDEF
) {
2260 copy_back_channels
|= 1 << j
;
2264 /* Test whether the channels are not equal. */
2265 if (c1
->type
!= c2
->type
||
2266 (c1
->type
== AC_IR_CONST
&&
2267 c1
->const_float
!= c2
->const_float
) ||
2268 (c1
->type
== AC_IR_VALUE
&&
2269 c1
->value
!= c2
->value
)) {
2277 copy_back_channels
= 0;
2279 if (p
== processed
->num
)
2282 /* If a match was found, but the matching export has undef where the new
2283 * one has a normal value, copy the normal value to the undef channel.
2285 struct ac_vs_exp_inst
*match
= &processed
->exp
[p
];
2287 /* Get current enabled channels mask. */
2288 LLVMValueRef arg
= LLVMGetOperand(match
->inst
, AC_EXP_ENABLED_CHANNELS
);
2289 unsigned enabled_channels
= LLVMConstIntGetZExtValue(arg
);
2291 while (copy_back_channels
) {
2292 unsigned chan
= u_bit_scan(©_back_channels
);
2294 assert(match
->chan
[chan
].type
== AC_IR_UNDEF
);
2295 LLVMSetOperand(match
->inst
, AC_EXP_OUT0
+ chan
,
2296 exp
->chan
[chan
].value
);
2297 match
->chan
[chan
] = exp
->chan
[chan
];
2299 /* Update number of enabled channels because the original mask
2300 * is not always 0xf.
2302 enabled_channels
|= (1 << chan
);
2303 LLVMSetOperand(match
->inst
, AC_EXP_ENABLED_CHANNELS
,
2304 LLVMConstInt(ctx
->i32
, enabled_channels
, 0));
2307 /* The PARAM export is duplicated. Kill it. */
2308 LLVMInstructionEraseFromParent(exp
->inst
);
2310 /* Change OFFSET to the matching export. */
2311 for (unsigned i
= 0; i
< num_outputs
; i
++) {
2312 if (vs_output_param_offset
[i
] == exp
->offset
) {
2313 vs_output_param_offset
[i
] = match
->offset
;
2320 void ac_optimize_vs_outputs(struct ac_llvm_context
*ctx
,
2321 LLVMValueRef main_fn
,
2322 uint8_t *vs_output_param_offset
,
2323 uint32_t num_outputs
,
2324 uint8_t *num_param_exports
)
2326 LLVMBasicBlockRef bb
;
2327 bool removed_any
= false;
2328 struct ac_vs_exports exports
;
2332 /* Process all LLVM instructions. */
2333 bb
= LLVMGetFirstBasicBlock(main_fn
);
2335 LLVMValueRef inst
= LLVMGetFirstInstruction(bb
);
2338 LLVMValueRef cur
= inst
;
2339 inst
= LLVMGetNextInstruction(inst
);
2340 struct ac_vs_exp_inst exp
;
2342 if (LLVMGetInstructionOpcode(cur
) != LLVMCall
)
2345 LLVMValueRef callee
= ac_llvm_get_called_value(cur
);
2347 if (!ac_llvm_is_function(callee
))
2350 const char *name
= LLVMGetValueName(callee
);
2351 unsigned num_args
= LLVMCountParams(callee
);
2353 /* Check if this is an export instruction. */
2354 if ((num_args
!= 9 && num_args
!= 8) ||
2355 (strcmp(name
, "llvm.SI.export") &&
2356 strcmp(name
, "llvm.amdgcn.exp.f32")))
2359 LLVMValueRef arg
= LLVMGetOperand(cur
, AC_EXP_TARGET
);
2360 unsigned target
= LLVMConstIntGetZExtValue(arg
);
2362 if (target
< V_008DFC_SQ_EXP_PARAM
)
2365 target
-= V_008DFC_SQ_EXP_PARAM
;
2367 /* Parse the instruction. */
2368 memset(&exp
, 0, sizeof(exp
));
2369 exp
.offset
= target
;
2372 for (unsigned i
= 0; i
< 4; i
++) {
2373 LLVMValueRef v
= LLVMGetOperand(cur
, AC_EXP_OUT0
+ i
);
2375 exp
.chan
[i
].value
= v
;
2377 if (LLVMIsUndef(v
)) {
2378 exp
.chan
[i
].type
= AC_IR_UNDEF
;
2379 } else if (LLVMIsAConstantFP(v
)) {
2380 LLVMBool loses_info
;
2381 exp
.chan
[i
].type
= AC_IR_CONST
;
2382 exp
.chan
[i
].const_float
=
2383 LLVMConstRealGetDouble(v
, &loses_info
);
2385 exp
.chan
[i
].type
= AC_IR_VALUE
;
2389 /* Eliminate constant and duplicated PARAM exports. */
2390 if (ac_eliminate_const_output(vs_output_param_offset
,
2391 num_outputs
, &exp
) ||
2392 ac_eliminate_duplicated_output(ctx
,
2393 vs_output_param_offset
,
2394 num_outputs
, &exports
,
2398 exports
.exp
[exports
.num
++] = exp
;
2401 bb
= LLVMGetNextBasicBlock(bb
);
2404 /* Remove holes in export memory due to removed PARAM exports.
2405 * This is done by renumbering all PARAM exports.
2408 uint8_t old_offset
[VARYING_SLOT_MAX
];
2411 /* Make a copy of the offsets. We need the old version while
2412 * we are modifying some of them. */
2413 memcpy(old_offset
, vs_output_param_offset
,
2414 sizeof(old_offset
));
2416 for (i
= 0; i
< exports
.num
; i
++) {
2417 unsigned offset
= exports
.exp
[i
].offset
;
2419 /* Update vs_output_param_offset. Multiple outputs can
2420 * have the same offset.
2422 for (out
= 0; out
< num_outputs
; out
++) {
2423 if (old_offset
[out
] == offset
)
2424 vs_output_param_offset
[out
] = i
;
2427 /* Change the PARAM offset in the instruction. */
2428 LLVMSetOperand(exports
.exp
[i
].inst
, AC_EXP_TARGET
,
2429 LLVMConstInt(ctx
->i32
,
2430 V_008DFC_SQ_EXP_PARAM
+ i
, 0));
2432 *num_param_exports
= exports
.num
;
2436 void ac_init_exec_full_mask(struct ac_llvm_context
*ctx
)
2438 LLVMValueRef full_mask
= LLVMConstInt(ctx
->i64
, ~0ull, 0);
2439 ac_build_intrinsic(ctx
,
2440 "llvm.amdgcn.init.exec", ctx
->voidt
,
2441 &full_mask
, 1, AC_FUNC_ATTR_CONVERGENT
);
2444 void ac_declare_lds_as_pointer(struct ac_llvm_context
*ctx
)
2446 unsigned lds_size
= ctx
->chip_class
>= CIK
? 65536 : 32768;
2447 ctx
->lds
= LLVMBuildIntToPtr(ctx
->builder
, ctx
->i32_0
,
2448 LLVMPointerType(LLVMArrayType(ctx
->i32
, lds_size
/ 4), AC_LOCAL_ADDR_SPACE
),
2452 LLVMValueRef
ac_lds_load(struct ac_llvm_context
*ctx
,
2453 LLVMValueRef dw_addr
)
2455 return ac_build_load(ctx
, ctx
->lds
, dw_addr
);
2458 void ac_lds_store(struct ac_llvm_context
*ctx
,
2459 LLVMValueRef dw_addr
,
2462 value
= ac_to_integer(ctx
, value
);
2463 ac_build_indexed_store(ctx
, ctx
->lds
,
2467 LLVMValueRef
ac_find_lsb(struct ac_llvm_context
*ctx
,
2468 LLVMTypeRef dst_type
,
2471 unsigned src0_bitsize
= ac_get_elem_bits(ctx
, LLVMTypeOf(src0
));
2472 const char *intrin_name
;
2475 if (src0_bitsize
== 64) {
2476 intrin_name
= "llvm.cttz.i64";
2480 intrin_name
= "llvm.cttz.i32";
2485 LLVMValueRef params
[2] = {
2488 /* The value of 1 means that ffs(x=0) = undef, so LLVM won't
2489 * add special code to check for x=0. The reason is that
2490 * the LLVM behavior for x=0 is different from what we
2491 * need here. However, LLVM also assumes that ffs(x) is
2492 * in [0, 31], but GLSL expects that ffs(0) = -1, so
2493 * a conditional assignment to handle 0 is still required.
2495 * The hardware already implements the correct behavior.
2500 LLVMValueRef lsb
= ac_build_intrinsic(ctx
, intrin_name
, type
,
2502 AC_FUNC_ATTR_READNONE
);
2504 if (src0_bitsize
== 64) {
2505 lsb
= LLVMBuildTrunc(ctx
->builder
, lsb
, ctx
->i32
, "");
2508 /* TODO: We need an intrinsic to skip this conditional. */
2509 /* Check for zero: */
2510 return LLVMBuildSelect(ctx
->builder
, LLVMBuildICmp(ctx
->builder
,
2513 LLVMConstInt(ctx
->i32
, -1, 0), lsb
, "");
2516 LLVMTypeRef
ac_array_in_const_addr_space(LLVMTypeRef elem_type
)
2518 return LLVMPointerType(LLVMArrayType(elem_type
, 0),
2519 AC_CONST_ADDR_SPACE
);
2522 LLVMTypeRef
ac_array_in_const32_addr_space(LLVMTypeRef elem_type
)
2524 if (!HAVE_32BIT_POINTERS
)
2525 return ac_array_in_const_addr_space(elem_type
);
2527 return LLVMPointerType(LLVMArrayType(elem_type
, 0),
2528 AC_CONST_32BIT_ADDR_SPACE
);
2531 static struct ac_llvm_flow
*
2532 get_current_flow(struct ac_llvm_context
*ctx
)
2534 if (ctx
->flow_depth
> 0)
2535 return &ctx
->flow
[ctx
->flow_depth
- 1];
2539 static struct ac_llvm_flow
*
2540 get_innermost_loop(struct ac_llvm_context
*ctx
)
2542 for (unsigned i
= ctx
->flow_depth
; i
> 0; --i
) {
2543 if (ctx
->flow
[i
- 1].loop_entry_block
)
2544 return &ctx
->flow
[i
- 1];
2549 static struct ac_llvm_flow
*
2550 push_flow(struct ac_llvm_context
*ctx
)
2552 struct ac_llvm_flow
*flow
;
2554 if (ctx
->flow_depth
>= ctx
->flow_depth_max
) {
2555 unsigned new_max
= MAX2(ctx
->flow_depth
<< 1,
2556 AC_LLVM_INITIAL_CF_DEPTH
);
2558 ctx
->flow
= realloc(ctx
->flow
, new_max
* sizeof(*ctx
->flow
));
2559 ctx
->flow_depth_max
= new_max
;
2562 flow
= &ctx
->flow
[ctx
->flow_depth
];
2565 flow
->next_block
= NULL
;
2566 flow
->loop_entry_block
= NULL
;
2570 static void set_basicblock_name(LLVMBasicBlockRef bb
, const char *base
,
2574 snprintf(buf
, sizeof(buf
), "%s%d", base
, label_id
);
2575 LLVMSetValueName(LLVMBasicBlockAsValue(bb
), buf
);
2578 /* Append a basic block at the level of the parent flow.
2580 static LLVMBasicBlockRef
append_basic_block(struct ac_llvm_context
*ctx
,
2583 assert(ctx
->flow_depth
>= 1);
2585 if (ctx
->flow_depth
>= 2) {
2586 struct ac_llvm_flow
*flow
= &ctx
->flow
[ctx
->flow_depth
- 2];
2588 return LLVMInsertBasicBlockInContext(ctx
->context
,
2589 flow
->next_block
, name
);
2592 LLVMValueRef main_fn
=
2593 LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
->builder
));
2594 return LLVMAppendBasicBlockInContext(ctx
->context
, main_fn
, name
);
2597 /* Emit a branch to the given default target for the current block if
2598 * applicable -- that is, if the current block does not already contain a
2599 * branch from a break or continue.
2601 static void emit_default_branch(LLVMBuilderRef builder
,
2602 LLVMBasicBlockRef target
)
2604 if (!LLVMGetBasicBlockTerminator(LLVMGetInsertBlock(builder
)))
2605 LLVMBuildBr(builder
, target
);
2608 void ac_build_bgnloop(struct ac_llvm_context
*ctx
, int label_id
)
2610 struct ac_llvm_flow
*flow
= push_flow(ctx
);
2611 flow
->loop_entry_block
= append_basic_block(ctx
, "LOOP");
2612 flow
->next_block
= append_basic_block(ctx
, "ENDLOOP");
2613 set_basicblock_name(flow
->loop_entry_block
, "loop", label_id
);
2614 LLVMBuildBr(ctx
->builder
, flow
->loop_entry_block
);
2615 LLVMPositionBuilderAtEnd(ctx
->builder
, flow
->loop_entry_block
);
2618 void ac_build_break(struct ac_llvm_context
*ctx
)
2620 struct ac_llvm_flow
*flow
= get_innermost_loop(ctx
);
2621 LLVMBuildBr(ctx
->builder
, flow
->next_block
);
2624 void ac_build_continue(struct ac_llvm_context
*ctx
)
2626 struct ac_llvm_flow
*flow
= get_innermost_loop(ctx
);
2627 LLVMBuildBr(ctx
->builder
, flow
->loop_entry_block
);
2630 void ac_build_else(struct ac_llvm_context
*ctx
, int label_id
)
2632 struct ac_llvm_flow
*current_branch
= get_current_flow(ctx
);
2633 LLVMBasicBlockRef endif_block
;
2635 assert(!current_branch
->loop_entry_block
);
2637 endif_block
= append_basic_block(ctx
, "ENDIF");
2638 emit_default_branch(ctx
->builder
, endif_block
);
2640 LLVMPositionBuilderAtEnd(ctx
->builder
, current_branch
->next_block
);
2641 set_basicblock_name(current_branch
->next_block
, "else", label_id
);
2643 current_branch
->next_block
= endif_block
;
2646 void ac_build_endif(struct ac_llvm_context
*ctx
, int label_id
)
2648 struct ac_llvm_flow
*current_branch
= get_current_flow(ctx
);
2650 assert(!current_branch
->loop_entry_block
);
2652 emit_default_branch(ctx
->builder
, current_branch
->next_block
);
2653 LLVMPositionBuilderAtEnd(ctx
->builder
, current_branch
->next_block
);
2654 set_basicblock_name(current_branch
->next_block
, "endif", label_id
);
2659 void ac_build_endloop(struct ac_llvm_context
*ctx
, int label_id
)
2661 struct ac_llvm_flow
*current_loop
= get_current_flow(ctx
);
2663 assert(current_loop
->loop_entry_block
);
2665 emit_default_branch(ctx
->builder
, current_loop
->loop_entry_block
);
2667 LLVMPositionBuilderAtEnd(ctx
->builder
, current_loop
->next_block
);
2668 set_basicblock_name(current_loop
->next_block
, "endloop", label_id
);
2672 static void if_cond_emit(struct ac_llvm_context
*ctx
, LLVMValueRef cond
,
2675 struct ac_llvm_flow
*flow
= push_flow(ctx
);
2676 LLVMBasicBlockRef if_block
;
2678 if_block
= append_basic_block(ctx
, "IF");
2679 flow
->next_block
= append_basic_block(ctx
, "ELSE");
2680 set_basicblock_name(if_block
, "if", label_id
);
2681 LLVMBuildCondBr(ctx
->builder
, cond
, if_block
, flow
->next_block
);
2682 LLVMPositionBuilderAtEnd(ctx
->builder
, if_block
);
2685 void ac_build_if(struct ac_llvm_context
*ctx
, LLVMValueRef value
,
2688 LLVMValueRef cond
= LLVMBuildFCmp(ctx
->builder
, LLVMRealUNE
,
2689 value
, ctx
->f32_0
, "");
2690 if_cond_emit(ctx
, cond
, label_id
);
2693 void ac_build_uif(struct ac_llvm_context
*ctx
, LLVMValueRef value
,
2696 LLVMValueRef cond
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
2697 ac_to_integer(ctx
, value
),
2699 if_cond_emit(ctx
, cond
, label_id
);
2702 LLVMValueRef
ac_build_alloca(struct ac_llvm_context
*ac
, LLVMTypeRef type
,
2705 LLVMBuilderRef builder
= ac
->builder
;
2706 LLVMBasicBlockRef current_block
= LLVMGetInsertBlock(builder
);
2707 LLVMValueRef function
= LLVMGetBasicBlockParent(current_block
);
2708 LLVMBasicBlockRef first_block
= LLVMGetEntryBasicBlock(function
);
2709 LLVMValueRef first_instr
= LLVMGetFirstInstruction(first_block
);
2710 LLVMBuilderRef first_builder
= LLVMCreateBuilderInContext(ac
->context
);
2714 LLVMPositionBuilderBefore(first_builder
, first_instr
);
2716 LLVMPositionBuilderAtEnd(first_builder
, first_block
);
2719 res
= LLVMBuildAlloca(first_builder
, type
, name
);
2720 LLVMBuildStore(builder
, LLVMConstNull(type
), res
);
2722 LLVMDisposeBuilder(first_builder
);
2727 LLVMValueRef
ac_build_alloca_undef(struct ac_llvm_context
*ac
,
2728 LLVMTypeRef type
, const char *name
)
2730 LLVMValueRef ptr
= ac_build_alloca(ac
, type
, name
);
2731 LLVMBuildStore(ac
->builder
, LLVMGetUndef(type
), ptr
);
2735 LLVMValueRef
ac_cast_ptr(struct ac_llvm_context
*ctx
, LLVMValueRef ptr
,
2738 int addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
2739 return LLVMBuildBitCast(ctx
->builder
, ptr
,
2740 LLVMPointerType(type
, addr_space
), "");
2743 LLVMValueRef
ac_trim_vector(struct ac_llvm_context
*ctx
, LLVMValueRef value
,
2746 unsigned num_components
= ac_get_llvm_num_components(value
);
2747 if (count
== num_components
)
2750 LLVMValueRef masks
[] = {
2751 ctx
->i32_0
, ctx
->i32_1
,
2752 LLVMConstInt(ctx
->i32
, 2, false), LLVMConstInt(ctx
->i32
, 3, false)};
2755 return LLVMBuildExtractElement(ctx
->builder
, value
, masks
[0],
2758 LLVMValueRef swizzle
= LLVMConstVector(masks
, count
);
2759 return LLVMBuildShuffleVector(ctx
->builder
, value
, value
, swizzle
, "");
2762 LLVMValueRef
ac_unpack_param(struct ac_llvm_context
*ctx
, LLVMValueRef param
,
2763 unsigned rshift
, unsigned bitwidth
)
2765 LLVMValueRef value
= param
;
2767 value
= LLVMBuildLShr(ctx
->builder
, value
,
2768 LLVMConstInt(ctx
->i32
, rshift
, false), "");
2770 if (rshift
+ bitwidth
< 32) {
2771 unsigned mask
= (1 << bitwidth
) - 1;
2772 value
= LLVMBuildAnd(ctx
->builder
, value
,
2773 LLVMConstInt(ctx
->i32
, mask
, false), "");
2778 /* Adjust the sample index according to FMASK.
2780 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
2781 * which is the identity mapping. Each nibble says which physical sample
2782 * should be fetched to get that sample.
2784 * For example, 0x11111100 means there are only 2 samples stored and
2785 * the second sample covers 3/4 of the pixel. When reading samples 0
2786 * and 1, return physical sample 0 (determined by the first two 0s
2787 * in FMASK), otherwise return physical sample 1.
2789 * The sample index should be adjusted as follows:
2790 * addr[sample_index] = (fmask >> (addr[sample_index] * 4)) & 0xF;
2792 void ac_apply_fmask_to_sample(struct ac_llvm_context
*ac
, LLVMValueRef fmask
,
2793 LLVMValueRef
*addr
, bool is_array_tex
)
2795 struct ac_image_args fmask_load
= {};
2796 fmask_load
.opcode
= ac_image_load
;
2797 fmask_load
.resource
= fmask
;
2798 fmask_load
.dmask
= 0xf;
2799 fmask_load
.dim
= is_array_tex
? ac_image_2darray
: ac_image_2d
;
2801 fmask_load
.coords
[0] = addr
[0];
2802 fmask_load
.coords
[1] = addr
[1];
2804 fmask_load
.coords
[2] = addr
[2];
2806 LLVMValueRef fmask_value
= ac_build_image_opcode(ac
, &fmask_load
);
2807 fmask_value
= LLVMBuildExtractElement(ac
->builder
, fmask_value
,
2810 /* Apply the formula. */
2811 unsigned sample_chan
= is_array_tex
? 3 : 2;
2812 LLVMValueRef final_sample
;
2813 final_sample
= LLVMBuildMul(ac
->builder
, addr
[sample_chan
],
2814 LLVMConstInt(ac
->i32
, 4, 0), "");
2815 final_sample
= LLVMBuildLShr(ac
->builder
, fmask_value
, final_sample
, "");
2816 /* Mask the sample index by 0x7, because 0x8 means an unknown value
2817 * with EQAA, so those will map to 0. */
2818 final_sample
= LLVMBuildAnd(ac
->builder
, final_sample
,
2819 LLVMConstInt(ac
->i32
, 0x7, 0), "");
2821 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
2822 * resource descriptor is 0 (invalid).
2825 tmp
= LLVMBuildBitCast(ac
->builder
, fmask
, ac
->v8i32
, "");
2826 tmp
= LLVMBuildExtractElement(ac
->builder
, tmp
, ac
->i32_1
, "");
2827 tmp
= LLVMBuildICmp(ac
->builder
, LLVMIntNE
, tmp
, ac
->i32_0
, "");
2829 /* Replace the MSAA sample index. */
2830 addr
[sample_chan
] = LLVMBuildSelect(ac
->builder
, tmp
, final_sample
,
2831 addr
[sample_chan
], "");
2835 _ac_build_readlane(struct ac_llvm_context
*ctx
, LLVMValueRef src
, LLVMValueRef lane
)
2837 ac_build_optimization_barrier(ctx
, &src
);
2838 return ac_build_intrinsic(ctx
,
2839 lane
== NULL
? "llvm.amdgcn.readfirstlane" : "llvm.amdgcn.readlane",
2840 LLVMTypeOf(src
), (LLVMValueRef
[]) {
2842 lane
== NULL
? 1 : 2,
2843 AC_FUNC_ATTR_READNONE
|
2844 AC_FUNC_ATTR_CONVERGENT
);
2848 * Builds the "llvm.amdgcn.readlane" or "llvm.amdgcn.readfirstlane" intrinsic.
2851 * @param lane - id of the lane or NULL for the first active lane
2852 * @return value of the lane
2855 ac_build_readlane(struct ac_llvm_context
*ctx
, LLVMValueRef src
, LLVMValueRef lane
)
2857 LLVMTypeRef src_type
= LLVMTypeOf(src
);
2858 src
= ac_to_integer(ctx
, src
);
2859 unsigned bits
= LLVMGetIntTypeWidth(LLVMTypeOf(src
));
2863 ret
= _ac_build_readlane(ctx
, src
, lane
);
2865 assert(bits
% 32 == 0);
2866 LLVMTypeRef vec_type
= LLVMVectorType(ctx
->i32
, bits
/ 32);
2867 LLVMValueRef src_vector
=
2868 LLVMBuildBitCast(ctx
->builder
, src
, vec_type
, "");
2869 ret
= LLVMGetUndef(vec_type
);
2870 for (unsigned i
= 0; i
< bits
/ 32; i
++) {
2871 src
= LLVMBuildExtractElement(ctx
->builder
, src_vector
,
2872 LLVMConstInt(ctx
->i32
, i
, 0), "");
2873 LLVMValueRef ret_comp
= _ac_build_readlane(ctx
, src
, lane
);
2874 ret
= LLVMBuildInsertElement(ctx
->builder
, ret
, ret_comp
,
2875 LLVMConstInt(ctx
->i32
, i
, 0), "");
2878 return LLVMBuildBitCast(ctx
->builder
, ret
, src_type
, "");
2882 ac_build_writelane(struct ac_llvm_context
*ctx
, LLVMValueRef src
, LLVMValueRef value
, LLVMValueRef lane
)
2884 /* TODO: Use the actual instruction when LLVM adds an intrinsic for it.
2886 LLVMValueRef pred
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, lane
,
2887 ac_get_thread_id(ctx
), "");
2888 return LLVMBuildSelect(ctx
->builder
, pred
, value
, src
, "");
2892 ac_build_mbcnt(struct ac_llvm_context
*ctx
, LLVMValueRef mask
)
2894 LLVMValueRef mask_vec
= LLVMBuildBitCast(ctx
->builder
, mask
,
2895 LLVMVectorType(ctx
->i32
, 2),
2897 LLVMValueRef mask_lo
= LLVMBuildExtractElement(ctx
->builder
, mask_vec
,
2899 LLVMValueRef mask_hi
= LLVMBuildExtractElement(ctx
->builder
, mask_vec
,
2902 ac_build_intrinsic(ctx
, "llvm.amdgcn.mbcnt.lo", ctx
->i32
,
2903 (LLVMValueRef
[]) { mask_lo
, ctx
->i32_0
},
2904 2, AC_FUNC_ATTR_READNONE
);
2905 val
= ac_build_intrinsic(ctx
, "llvm.amdgcn.mbcnt.hi", ctx
->i32
,
2906 (LLVMValueRef
[]) { mask_hi
, val
},
2907 2, AC_FUNC_ATTR_READNONE
);
2912 _dpp_quad_perm
= 0x000,
2913 _dpp_row_sl
= 0x100,
2914 _dpp_row_sr
= 0x110,
2915 _dpp_row_rr
= 0x120,
2920 dpp_row_mirror
= 0x140,
2921 dpp_row_half_mirror
= 0x141,
2922 dpp_row_bcast15
= 0x142,
2923 dpp_row_bcast31
= 0x143
2926 static inline enum dpp_ctrl
2927 dpp_quad_perm(unsigned lane0
, unsigned lane1
, unsigned lane2
, unsigned lane3
)
2929 assert(lane0
< 4 && lane1
< 4 && lane2
< 4 && lane3
< 4);
2930 return _dpp_quad_perm
| lane0
| (lane1
<< 2) | (lane2
<< 4) | (lane3
<< 6);
2933 static inline enum dpp_ctrl
2934 dpp_row_sl(unsigned amount
)
2936 assert(amount
> 0 && amount
< 16);
2937 return _dpp_row_sl
| amount
;
2940 static inline enum dpp_ctrl
2941 dpp_row_sr(unsigned amount
)
2943 assert(amount
> 0 && amount
< 16);
2944 return _dpp_row_sr
| amount
;
2948 _ac_build_dpp(struct ac_llvm_context
*ctx
, LLVMValueRef old
, LLVMValueRef src
,
2949 enum dpp_ctrl dpp_ctrl
, unsigned row_mask
, unsigned bank_mask
,
2952 return ac_build_intrinsic(ctx
, "llvm.amdgcn.update.dpp.i32",
2956 LLVMConstInt(ctx
->i32
, dpp_ctrl
, 0),
2957 LLVMConstInt(ctx
->i32
, row_mask
, 0),
2958 LLVMConstInt(ctx
->i32
, bank_mask
, 0),
2959 LLVMConstInt(ctx
->i1
, bound_ctrl
, 0) },
2960 6, AC_FUNC_ATTR_READNONE
| AC_FUNC_ATTR_CONVERGENT
);
2964 ac_build_dpp(struct ac_llvm_context
*ctx
, LLVMValueRef old
, LLVMValueRef src
,
2965 enum dpp_ctrl dpp_ctrl
, unsigned row_mask
, unsigned bank_mask
,
2968 LLVMTypeRef src_type
= LLVMTypeOf(src
);
2969 src
= ac_to_integer(ctx
, src
);
2970 old
= ac_to_integer(ctx
, old
);
2971 unsigned bits
= LLVMGetIntTypeWidth(LLVMTypeOf(src
));
2974 ret
= _ac_build_dpp(ctx
, old
, src
, dpp_ctrl
, row_mask
,
2975 bank_mask
, bound_ctrl
);
2977 assert(bits
% 32 == 0);
2978 LLVMTypeRef vec_type
= LLVMVectorType(ctx
->i32
, bits
/ 32);
2979 LLVMValueRef src_vector
=
2980 LLVMBuildBitCast(ctx
->builder
, src
, vec_type
, "");
2981 LLVMValueRef old_vector
=
2982 LLVMBuildBitCast(ctx
->builder
, old
, vec_type
, "");
2983 ret
= LLVMGetUndef(vec_type
);
2984 for (unsigned i
= 0; i
< bits
/ 32; i
++) {
2985 src
= LLVMBuildExtractElement(ctx
->builder
, src_vector
,
2986 LLVMConstInt(ctx
->i32
, i
,
2988 old
= LLVMBuildExtractElement(ctx
->builder
, old_vector
,
2989 LLVMConstInt(ctx
->i32
, i
,
2991 LLVMValueRef ret_comp
= _ac_build_dpp(ctx
, old
, src
,
2996 ret
= LLVMBuildInsertElement(ctx
->builder
, ret
,
2998 LLVMConstInt(ctx
->i32
, i
,
3002 return LLVMBuildBitCast(ctx
->builder
, ret
, src_type
, "");
3005 static inline unsigned
3006 ds_pattern_bitmode(unsigned and_mask
, unsigned or_mask
, unsigned xor_mask
)
3008 assert(and_mask
< 32 && or_mask
< 32 && xor_mask
< 32);
3009 return and_mask
| (or_mask
<< 5) | (xor_mask
<< 10);
3013 _ac_build_ds_swizzle(struct ac_llvm_context
*ctx
, LLVMValueRef src
, unsigned mask
)
3015 return ac_build_intrinsic(ctx
, "llvm.amdgcn.ds.swizzle",
3016 LLVMTypeOf(src
), (LLVMValueRef
[]) {
3017 src
, LLVMConstInt(ctx
->i32
, mask
, 0) },
3018 2, AC_FUNC_ATTR_READNONE
| AC_FUNC_ATTR_CONVERGENT
);
3022 ac_build_ds_swizzle(struct ac_llvm_context
*ctx
, LLVMValueRef src
, unsigned mask
)
3024 LLVMTypeRef src_type
= LLVMTypeOf(src
);
3025 src
= ac_to_integer(ctx
, src
);
3026 unsigned bits
= LLVMGetIntTypeWidth(LLVMTypeOf(src
));
3029 ret
= _ac_build_ds_swizzle(ctx
, src
, mask
);
3031 assert(bits
% 32 == 0);
3032 LLVMTypeRef vec_type
= LLVMVectorType(ctx
->i32
, bits
/ 32);
3033 LLVMValueRef src_vector
=
3034 LLVMBuildBitCast(ctx
->builder
, src
, vec_type
, "");
3035 ret
= LLVMGetUndef(vec_type
);
3036 for (unsigned i
= 0; i
< bits
/ 32; i
++) {
3037 src
= LLVMBuildExtractElement(ctx
->builder
, src_vector
,
3038 LLVMConstInt(ctx
->i32
, i
,
3040 LLVMValueRef ret_comp
= _ac_build_ds_swizzle(ctx
, src
,
3042 ret
= LLVMBuildInsertElement(ctx
->builder
, ret
,
3044 LLVMConstInt(ctx
->i32
, i
,
3048 return LLVMBuildBitCast(ctx
->builder
, ret
, src_type
, "");
3052 ac_build_wwm(struct ac_llvm_context
*ctx
, LLVMValueRef src
)
3054 char name
[32], type
[8];
3055 ac_build_type_name_for_intr(LLVMTypeOf(src
), type
, sizeof(type
));
3056 snprintf(name
, sizeof(name
), "llvm.amdgcn.wwm.%s", type
);
3057 return ac_build_intrinsic(ctx
, name
, LLVMTypeOf(src
),
3058 (LLVMValueRef
[]) { src
}, 1,
3059 AC_FUNC_ATTR_READNONE
);
3063 ac_build_set_inactive(struct ac_llvm_context
*ctx
, LLVMValueRef src
,
3064 LLVMValueRef inactive
)
3066 char name
[33], type
[8];
3067 LLVMTypeRef src_type
= LLVMTypeOf(src
);
3068 src
= ac_to_integer(ctx
, src
);
3069 inactive
= ac_to_integer(ctx
, inactive
);
3070 ac_build_type_name_for_intr(LLVMTypeOf(src
), type
, sizeof(type
));
3071 snprintf(name
, sizeof(name
), "llvm.amdgcn.set.inactive.%s", type
);
3073 ac_build_intrinsic(ctx
, name
,
3074 LLVMTypeOf(src
), (LLVMValueRef
[]) {
3076 AC_FUNC_ATTR_READNONE
|
3077 AC_FUNC_ATTR_CONVERGENT
);
3078 return LLVMBuildBitCast(ctx
->builder
, ret
, src_type
, "");
3082 get_reduction_identity(struct ac_llvm_context
*ctx
, nir_op op
, unsigned type_size
)
3084 if (type_size
== 4) {
3086 case nir_op_iadd
: return ctx
->i32_0
;
3087 case nir_op_fadd
: return ctx
->f32_0
;
3088 case nir_op_imul
: return ctx
->i32_1
;
3089 case nir_op_fmul
: return ctx
->f32_1
;
3090 case nir_op_imin
: return LLVMConstInt(ctx
->i32
, INT32_MAX
, 0);
3091 case nir_op_umin
: return LLVMConstInt(ctx
->i32
, UINT32_MAX
, 0);
3092 case nir_op_fmin
: return LLVMConstReal(ctx
->f32
, INFINITY
);
3093 case nir_op_imax
: return LLVMConstInt(ctx
->i32
, INT32_MIN
, 0);
3094 case nir_op_umax
: return ctx
->i32_0
;
3095 case nir_op_fmax
: return LLVMConstReal(ctx
->f32
, -INFINITY
);
3096 case nir_op_iand
: return LLVMConstInt(ctx
->i32
, -1, 0);
3097 case nir_op_ior
: return ctx
->i32_0
;
3098 case nir_op_ixor
: return ctx
->i32_0
;
3100 unreachable("bad reduction intrinsic");
3102 } else { /* type_size == 64bit */
3104 case nir_op_iadd
: return ctx
->i64_0
;
3105 case nir_op_fadd
: return ctx
->f64_0
;
3106 case nir_op_imul
: return ctx
->i64_1
;
3107 case nir_op_fmul
: return ctx
->f64_1
;
3108 case nir_op_imin
: return LLVMConstInt(ctx
->i64
, INT64_MAX
, 0);
3109 case nir_op_umin
: return LLVMConstInt(ctx
->i64
, UINT64_MAX
, 0);
3110 case nir_op_fmin
: return LLVMConstReal(ctx
->f64
, INFINITY
);
3111 case nir_op_imax
: return LLVMConstInt(ctx
->i64
, INT64_MIN
, 0);
3112 case nir_op_umax
: return ctx
->i64_0
;
3113 case nir_op_fmax
: return LLVMConstReal(ctx
->f64
, -INFINITY
);
3114 case nir_op_iand
: return LLVMConstInt(ctx
->i64
, -1, 0);
3115 case nir_op_ior
: return ctx
->i64_0
;
3116 case nir_op_ixor
: return ctx
->i64_0
;
3118 unreachable("bad reduction intrinsic");
3124 ac_build_alu_op(struct ac_llvm_context
*ctx
, LLVMValueRef lhs
, LLVMValueRef rhs
, nir_op op
)
3126 bool _64bit
= ac_get_type_size(LLVMTypeOf(lhs
)) == 8;
3128 case nir_op_iadd
: return LLVMBuildAdd(ctx
->builder
, lhs
, rhs
, "");
3129 case nir_op_fadd
: return LLVMBuildFAdd(ctx
->builder
, lhs
, rhs
, "");
3130 case nir_op_imul
: return LLVMBuildMul(ctx
->builder
, lhs
, rhs
, "");
3131 case nir_op_fmul
: return LLVMBuildFMul(ctx
->builder
, lhs
, rhs
, "");
3132 case nir_op_imin
: return LLVMBuildSelect(ctx
->builder
,
3133 LLVMBuildICmp(ctx
->builder
, LLVMIntSLT
, lhs
, rhs
, ""),
3135 case nir_op_umin
: return LLVMBuildSelect(ctx
->builder
,
3136 LLVMBuildICmp(ctx
->builder
, LLVMIntULT
, lhs
, rhs
, ""),
3138 case nir_op_fmin
: return ac_build_intrinsic(ctx
,
3139 _64bit
? "llvm.minnum.f64" : "llvm.minnum.f32",
3140 _64bit
? ctx
->f64
: ctx
->f32
,
3141 (LLVMValueRef
[]){lhs
, rhs
}, 2, AC_FUNC_ATTR_READNONE
);
3142 case nir_op_imax
: return LLVMBuildSelect(ctx
->builder
,
3143 LLVMBuildICmp(ctx
->builder
, LLVMIntSGT
, lhs
, rhs
, ""),
3145 case nir_op_umax
: return LLVMBuildSelect(ctx
->builder
,
3146 LLVMBuildICmp(ctx
->builder
, LLVMIntUGT
, lhs
, rhs
, ""),
3148 case nir_op_fmax
: return ac_build_intrinsic(ctx
,
3149 _64bit
? "llvm.maxnum.f64" : "llvm.maxnum.f32",
3150 _64bit
? ctx
->f64
: ctx
->f32
,
3151 (LLVMValueRef
[]){lhs
, rhs
}, 2, AC_FUNC_ATTR_READNONE
);
3152 case nir_op_iand
: return LLVMBuildAnd(ctx
->builder
, lhs
, rhs
, "");
3153 case nir_op_ior
: return LLVMBuildOr(ctx
->builder
, lhs
, rhs
, "");
3154 case nir_op_ixor
: return LLVMBuildXor(ctx
->builder
, lhs
, rhs
, "");
3156 unreachable("bad reduction intrinsic");
3160 /* TODO: add inclusive and excluse scan functions for SI chip class. */
3162 ac_build_scan(struct ac_llvm_context
*ctx
, nir_op op
, LLVMValueRef src
, LLVMValueRef identity
)
3164 LLVMValueRef result
, tmp
;
3166 tmp
= ac_build_dpp(ctx
, identity
, src
, dpp_row_sr(1), 0xf, 0xf, false);
3167 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
3168 tmp
= ac_build_dpp(ctx
, identity
, src
, dpp_row_sr(2), 0xf, 0xf, false);
3169 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
3170 tmp
= ac_build_dpp(ctx
, identity
, src
, dpp_row_sr(3), 0xf, 0xf, false);
3171 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
3172 tmp
= ac_build_dpp(ctx
, identity
, result
, dpp_row_sr(4), 0xf, 0xe, false);
3173 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
3174 tmp
= ac_build_dpp(ctx
, identity
, result
, dpp_row_sr(8), 0xf, 0xc, false);
3175 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
3176 tmp
= ac_build_dpp(ctx
, identity
, result
, dpp_row_bcast15
, 0xa, 0xf, false);
3177 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
3178 tmp
= ac_build_dpp(ctx
, identity
, result
, dpp_row_bcast31
, 0xc, 0xf, false);
3179 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
3184 ac_build_inclusive_scan(struct ac_llvm_context
*ctx
, LLVMValueRef src
, nir_op op
)
3186 ac_build_optimization_barrier(ctx
, &src
);
3187 LLVMValueRef result
;
3188 LLVMValueRef identity
= get_reduction_identity(ctx
, op
,
3189 ac_get_type_size(LLVMTypeOf(src
)));
3190 result
= LLVMBuildBitCast(ctx
->builder
,
3191 ac_build_set_inactive(ctx
, src
, identity
),
3192 LLVMTypeOf(identity
), "");
3193 result
= ac_build_scan(ctx
, op
, result
, identity
);
3195 return ac_build_wwm(ctx
, result
);
3199 ac_build_exclusive_scan(struct ac_llvm_context
*ctx
, LLVMValueRef src
, nir_op op
)
3201 ac_build_optimization_barrier(ctx
, &src
);
3202 LLVMValueRef result
;
3203 LLVMValueRef identity
= get_reduction_identity(ctx
, op
,
3204 ac_get_type_size(LLVMTypeOf(src
)));
3205 result
= LLVMBuildBitCast(ctx
->builder
,
3206 ac_build_set_inactive(ctx
, src
, identity
),
3207 LLVMTypeOf(identity
), "");
3208 result
= ac_build_dpp(ctx
, identity
, result
, dpp_wf_sr1
, 0xf, 0xf, false);
3209 result
= ac_build_scan(ctx
, op
, result
, identity
);
3211 return ac_build_wwm(ctx
, result
);
3215 ac_build_reduce(struct ac_llvm_context
*ctx
, LLVMValueRef src
, nir_op op
, unsigned cluster_size
)
3217 if (cluster_size
== 1) return src
;
3218 ac_build_optimization_barrier(ctx
, &src
);
3219 LLVMValueRef result
, swap
;
3220 LLVMValueRef identity
= get_reduction_identity(ctx
, op
,
3221 ac_get_type_size(LLVMTypeOf(src
)));
3222 result
= LLVMBuildBitCast(ctx
->builder
,
3223 ac_build_set_inactive(ctx
, src
, identity
),
3224 LLVMTypeOf(identity
), "");
3225 swap
= ac_build_quad_swizzle(ctx
, result
, 1, 0, 3, 2);
3226 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
3227 if (cluster_size
== 2) return ac_build_wwm(ctx
, result
);
3229 swap
= ac_build_quad_swizzle(ctx
, result
, 2, 3, 0, 1);
3230 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
3231 if (cluster_size
== 4) return ac_build_wwm(ctx
, result
);
3233 if (ctx
->chip_class
>= VI
)
3234 swap
= ac_build_dpp(ctx
, identity
, result
, dpp_row_half_mirror
, 0xf, 0xf, false);
3236 swap
= ac_build_ds_swizzle(ctx
, result
, ds_pattern_bitmode(0x1f, 0, 0x04));
3237 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
3238 if (cluster_size
== 8) return ac_build_wwm(ctx
, result
);
3240 if (ctx
->chip_class
>= VI
)
3241 swap
= ac_build_dpp(ctx
, identity
, result
, dpp_row_mirror
, 0xf, 0xf, false);
3243 swap
= ac_build_ds_swizzle(ctx
, result
, ds_pattern_bitmode(0x1f, 0, 0x08));
3244 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
3245 if (cluster_size
== 16) return ac_build_wwm(ctx
, result
);
3247 if (ctx
->chip_class
>= VI
&& cluster_size
!= 32)
3248 swap
= ac_build_dpp(ctx
, identity
, result
, dpp_row_bcast15
, 0xa, 0xf, false);
3250 swap
= ac_build_ds_swizzle(ctx
, result
, ds_pattern_bitmode(0x1f, 0, 0x10));
3251 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
3252 if (cluster_size
== 32) return ac_build_wwm(ctx
, result
);
3254 if (ctx
->chip_class
>= VI
) {
3255 swap
= ac_build_dpp(ctx
, identity
, result
, dpp_row_bcast31
, 0xc, 0xf, false);
3256 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
3257 result
= ac_build_readlane(ctx
, result
, LLVMConstInt(ctx
->i32
, 63, 0));
3258 return ac_build_wwm(ctx
, result
);
3260 swap
= ac_build_readlane(ctx
, result
, ctx
->i32_0
);
3261 result
= ac_build_readlane(ctx
, result
, LLVMConstInt(ctx
->i32
, 32, 0));
3262 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
3263 return ac_build_wwm(ctx
, result
);
3268 ac_build_quad_swizzle(struct ac_llvm_context
*ctx
, LLVMValueRef src
,
3269 unsigned lane0
, unsigned lane1
, unsigned lane2
, unsigned lane3
)
3271 unsigned mask
= dpp_quad_perm(lane0
, lane1
, lane2
, lane3
);
3272 if (ctx
->chip_class
>= VI
) {
3273 return ac_build_dpp(ctx
, src
, src
, mask
, 0xf, 0xf, false);
3275 return ac_build_ds_swizzle(ctx
, src
, (1 << 15) | mask
);
3280 ac_build_shuffle(struct ac_llvm_context
*ctx
, LLVMValueRef src
, LLVMValueRef index
)
3282 index
= LLVMBuildMul(ctx
->builder
, index
, LLVMConstInt(ctx
->i32
, 4, 0), "");
3283 return ac_build_intrinsic(ctx
,
3284 "llvm.amdgcn.ds.bpermute", ctx
->i32
,
3285 (LLVMValueRef
[]) {index
, src
}, 2,
3286 AC_FUNC_ATTR_READNONE
|
3287 AC_FUNC_ATTR_CONVERGENT
);