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"
42 #include "shader_enums.h"
44 /* Initialize module-independent parts of the context.
46 * The caller is responsible for initializing ctx::module and ctx::builder.
49 ac_llvm_context_init(struct ac_llvm_context
*ctx
, LLVMContextRef context
,
50 enum chip_class chip_class
)
54 ctx
->chip_class
= chip_class
;
56 ctx
->context
= context
;
60 ctx
->voidt
= LLVMVoidTypeInContext(ctx
->context
);
61 ctx
->i1
= LLVMInt1TypeInContext(ctx
->context
);
62 ctx
->i8
= LLVMInt8TypeInContext(ctx
->context
);
63 ctx
->i16
= LLVMIntTypeInContext(ctx
->context
, 16);
64 ctx
->i32
= LLVMIntTypeInContext(ctx
->context
, 32);
65 ctx
->i64
= LLVMIntTypeInContext(ctx
->context
, 64);
66 ctx
->f16
= LLVMHalfTypeInContext(ctx
->context
);
67 ctx
->f32
= LLVMFloatTypeInContext(ctx
->context
);
68 ctx
->f64
= LLVMDoubleTypeInContext(ctx
->context
);
69 ctx
->v2i32
= LLVMVectorType(ctx
->i32
, 2);
70 ctx
->v3i32
= LLVMVectorType(ctx
->i32
, 3);
71 ctx
->v4i32
= LLVMVectorType(ctx
->i32
, 4);
72 ctx
->v2f32
= LLVMVectorType(ctx
->f32
, 2);
73 ctx
->v4f32
= LLVMVectorType(ctx
->f32
, 4);
74 ctx
->v8i32
= LLVMVectorType(ctx
->i32
, 8);
76 ctx
->i32_0
= LLVMConstInt(ctx
->i32
, 0, false);
77 ctx
->i32_1
= LLVMConstInt(ctx
->i32
, 1, false);
78 ctx
->f32_0
= LLVMConstReal(ctx
->f32
, 0.0);
79 ctx
->f32_1
= LLVMConstReal(ctx
->f32
, 1.0);
81 ctx
->i1false
= LLVMConstInt(ctx
->i1
, 0, false);
82 ctx
->i1true
= LLVMConstInt(ctx
->i1
, 1, false);
84 ctx
->range_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
87 ctx
->invariant_load_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
88 "invariant.load", 14);
90 ctx
->fpmath_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
, "fpmath", 6);
92 args
[0] = LLVMConstReal(ctx
->f32
, 2.5);
93 ctx
->fpmath_md_2p5_ulp
= LLVMMDNodeInContext(ctx
->context
, args
, 1);
95 ctx
->uniform_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
96 "amdgpu.uniform", 14);
98 ctx
->empty_md
= LLVMMDNodeInContext(ctx
->context
, NULL
, 0);
102 ac_get_type_size(LLVMTypeRef type
)
104 LLVMTypeKind kind
= LLVMGetTypeKind(type
);
107 case LLVMIntegerTypeKind
:
108 return LLVMGetIntTypeWidth(type
) / 8;
109 case LLVMFloatTypeKind
:
111 case LLVMDoubleTypeKind
:
112 case LLVMPointerTypeKind
:
114 case LLVMVectorTypeKind
:
115 return LLVMGetVectorSize(type
) *
116 ac_get_type_size(LLVMGetElementType(type
));
117 case LLVMArrayTypeKind
:
118 return LLVMGetArrayLength(type
) *
119 ac_get_type_size(LLVMGetElementType(type
));
126 static LLVMTypeRef
to_integer_type_scalar(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
128 if (t
== ctx
->f16
|| t
== ctx
->i16
)
130 else if (t
== ctx
->f32
|| t
== ctx
->i32
)
132 else if (t
== ctx
->f64
|| t
== ctx
->i64
)
135 unreachable("Unhandled integer size");
139 ac_to_integer_type(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
141 if (LLVMGetTypeKind(t
) == LLVMVectorTypeKind
) {
142 LLVMTypeRef elem_type
= LLVMGetElementType(t
);
143 return LLVMVectorType(to_integer_type_scalar(ctx
, elem_type
),
144 LLVMGetVectorSize(t
));
146 return to_integer_type_scalar(ctx
, t
);
150 ac_to_integer(struct ac_llvm_context
*ctx
, LLVMValueRef v
)
152 LLVMTypeRef type
= LLVMTypeOf(v
);
153 return LLVMBuildBitCast(ctx
->builder
, v
, ac_to_integer_type(ctx
, type
), "");
156 static LLVMTypeRef
to_float_type_scalar(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
158 if (t
== ctx
->i16
|| t
== ctx
->f16
)
160 else if (t
== ctx
->i32
|| t
== ctx
->f32
)
162 else if (t
== ctx
->i64
|| t
== ctx
->f64
)
165 unreachable("Unhandled float size");
169 ac_to_float_type(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
171 if (LLVMGetTypeKind(t
) == LLVMVectorTypeKind
) {
172 LLVMTypeRef elem_type
= LLVMGetElementType(t
);
173 return LLVMVectorType(to_float_type_scalar(ctx
, elem_type
),
174 LLVMGetVectorSize(t
));
176 return to_float_type_scalar(ctx
, t
);
180 ac_to_float(struct ac_llvm_context
*ctx
, LLVMValueRef v
)
182 LLVMTypeRef type
= LLVMTypeOf(v
);
183 return LLVMBuildBitCast(ctx
->builder
, v
, ac_to_float_type(ctx
, type
), "");
188 ac_build_intrinsic(struct ac_llvm_context
*ctx
, const char *name
,
189 LLVMTypeRef return_type
, LLVMValueRef
*params
,
190 unsigned param_count
, unsigned attrib_mask
)
192 LLVMValueRef function
, call
;
193 bool set_callsite_attrs
= HAVE_LLVM
>= 0x0400 &&
194 !(attrib_mask
& AC_FUNC_ATTR_LEGACY
);
196 function
= LLVMGetNamedFunction(ctx
->module
, name
);
198 LLVMTypeRef param_types
[32], function_type
;
201 assert(param_count
<= 32);
203 for (i
= 0; i
< param_count
; ++i
) {
205 param_types
[i
] = LLVMTypeOf(params
[i
]);
208 LLVMFunctionType(return_type
, param_types
, param_count
, 0);
209 function
= LLVMAddFunction(ctx
->module
, name
, function_type
);
211 LLVMSetFunctionCallConv(function
, LLVMCCallConv
);
212 LLVMSetLinkage(function
, LLVMExternalLinkage
);
214 if (!set_callsite_attrs
)
215 ac_add_func_attributes(ctx
->context
, function
, attrib_mask
);
218 call
= LLVMBuildCall(ctx
->builder
, function
, params
, param_count
, "");
219 if (set_callsite_attrs
)
220 ac_add_func_attributes(ctx
->context
, call
, attrib_mask
);
225 * Given the i32 or vNi32 \p type, generate the textual name (e.g. for use with
228 void ac_build_type_name_for_intr(LLVMTypeRef type
, char *buf
, unsigned bufsize
)
230 LLVMTypeRef elem_type
= type
;
232 assert(bufsize
>= 8);
234 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
) {
235 int ret
= snprintf(buf
, bufsize
, "v%u",
236 LLVMGetVectorSize(type
));
238 char *type_name
= LLVMPrintTypeToString(type
);
239 fprintf(stderr
, "Error building type name for: %s\n",
243 elem_type
= LLVMGetElementType(type
);
247 switch (LLVMGetTypeKind(elem_type
)) {
249 case LLVMIntegerTypeKind
:
250 snprintf(buf
, bufsize
, "i%d", LLVMGetIntTypeWidth(elem_type
));
252 case LLVMFloatTypeKind
:
253 snprintf(buf
, bufsize
, "f32");
255 case LLVMDoubleTypeKind
:
256 snprintf(buf
, bufsize
, "f64");
262 * Helper function that builds an LLVM IR PHI node and immediately adds
266 ac_build_phi(struct ac_llvm_context
*ctx
, LLVMTypeRef type
,
267 unsigned count_incoming
, LLVMValueRef
*values
,
268 LLVMBasicBlockRef
*blocks
)
270 LLVMValueRef phi
= LLVMBuildPhi(ctx
->builder
, type
, "");
271 LLVMAddIncoming(phi
, values
, blocks
, count_incoming
);
275 /* Prevent optimizations (at least of memory accesses) across the current
276 * point in the program by emitting empty inline assembly that is marked as
277 * having side effects.
279 * Optionally, a value can be passed through the inline assembly to prevent
280 * LLVM from hoisting calls to ReadNone functions.
283 ac_build_optimization_barrier(struct ac_llvm_context
*ctx
,
286 static int counter
= 0;
288 LLVMBuilderRef builder
= ctx
->builder
;
291 snprintf(code
, sizeof(code
), "; %d", p_atomic_inc_return(&counter
));
294 LLVMTypeRef ftype
= LLVMFunctionType(ctx
->voidt
, NULL
, 0, false);
295 LLVMValueRef inlineasm
= LLVMConstInlineAsm(ftype
, code
, "", true, false);
296 LLVMBuildCall(builder
, inlineasm
, NULL
, 0, "");
298 LLVMTypeRef ftype
= LLVMFunctionType(ctx
->i32
, &ctx
->i32
, 1, false);
299 LLVMValueRef inlineasm
= LLVMConstInlineAsm(ftype
, code
, "=v,0", true, false);
300 LLVMValueRef vgpr
= *pvgpr
;
301 LLVMTypeRef vgpr_type
= LLVMTypeOf(vgpr
);
302 unsigned vgpr_size
= ac_get_type_size(vgpr_type
);
305 assert(vgpr_size
% 4 == 0);
307 vgpr
= LLVMBuildBitCast(builder
, vgpr
, LLVMVectorType(ctx
->i32
, vgpr_size
/ 4), "");
308 vgpr0
= LLVMBuildExtractElement(builder
, vgpr
, ctx
->i32_0
, "");
309 vgpr0
= LLVMBuildCall(builder
, inlineasm
, &vgpr0
, 1, "");
310 vgpr
= LLVMBuildInsertElement(builder
, vgpr
, vgpr0
, ctx
->i32_0
, "");
311 vgpr
= LLVMBuildBitCast(builder
, vgpr
, vgpr_type
, "");
318 ac_build_ballot(struct ac_llvm_context
*ctx
,
321 LLVMValueRef args
[3] = {
324 LLVMConstInt(ctx
->i32
, LLVMIntNE
, 0)
327 /* We currently have no other way to prevent LLVM from lifting the icmp
328 * calls to a dominating basic block.
330 ac_build_optimization_barrier(ctx
, &args
[0]);
332 if (LLVMTypeOf(args
[0]) != ctx
->i32
)
333 args
[0] = LLVMBuildBitCast(ctx
->builder
, args
[0], ctx
->i32
, "");
335 return ac_build_intrinsic(ctx
,
336 "llvm.amdgcn.icmp.i32",
338 AC_FUNC_ATTR_NOUNWIND
|
339 AC_FUNC_ATTR_READNONE
|
340 AC_FUNC_ATTR_CONVERGENT
);
344 ac_build_vote_all(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
346 LLVMValueRef active_set
= ac_build_ballot(ctx
, ctx
->i32_1
);
347 LLVMValueRef vote_set
= ac_build_ballot(ctx
, value
);
348 return LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, vote_set
, active_set
, "");
352 ac_build_vote_any(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
354 LLVMValueRef vote_set
= ac_build_ballot(ctx
, value
);
355 return LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, vote_set
,
356 LLVMConstInt(ctx
->i64
, 0, 0), "");
360 ac_build_vote_eq(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
362 LLVMValueRef active_set
= ac_build_ballot(ctx
, ctx
->i32_1
);
363 LLVMValueRef vote_set
= ac_build_ballot(ctx
, value
);
365 LLVMValueRef all
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
366 vote_set
, active_set
, "");
367 LLVMValueRef none
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
369 LLVMConstInt(ctx
->i64
, 0, 0), "");
370 return LLVMBuildOr(ctx
->builder
, all
, none
, "");
374 ac_build_gather_values_extended(struct ac_llvm_context
*ctx
,
375 LLVMValueRef
*values
,
376 unsigned value_count
,
377 unsigned value_stride
,
381 LLVMBuilderRef builder
= ctx
->builder
;
382 LLVMValueRef vec
= NULL
;
385 if (value_count
== 1 && !always_vector
) {
387 return LLVMBuildLoad(builder
, values
[0], "");
389 } else if (!value_count
)
390 unreachable("value_count is 0");
392 for (i
= 0; i
< value_count
; i
++) {
393 LLVMValueRef value
= values
[i
* value_stride
];
395 value
= LLVMBuildLoad(builder
, value
, "");
398 vec
= LLVMGetUndef( LLVMVectorType(LLVMTypeOf(value
), value_count
));
399 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, i
, false);
400 vec
= LLVMBuildInsertElement(builder
, vec
, value
, index
, "");
406 ac_build_gather_values(struct ac_llvm_context
*ctx
,
407 LLVMValueRef
*values
,
408 unsigned value_count
)
410 return ac_build_gather_values_extended(ctx
, values
, value_count
, 1, false, false);
414 ac_build_fdiv(struct ac_llvm_context
*ctx
,
418 LLVMValueRef ret
= LLVMBuildFDiv(ctx
->builder
, num
, den
, "");
420 if (!LLVMIsConstant(ret
))
421 LLVMSetMetadata(ret
, ctx
->fpmath_md_kind
, ctx
->fpmath_md_2p5_ulp
);
425 /* Coordinates for cube map selection. sc, tc, and ma are as in Table 8.27
426 * of the OpenGL 4.5 (Compatibility Profile) specification, except ma is
427 * already multiplied by two. id is the cube face number.
429 struct cube_selection_coords
{
436 build_cube_intrinsic(struct ac_llvm_context
*ctx
,
438 struct cube_selection_coords
*out
)
440 LLVMTypeRef f32
= ctx
->f32
;
442 out
->stc
[1] = ac_build_intrinsic(ctx
, "llvm.amdgcn.cubetc",
443 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
444 out
->stc
[0] = ac_build_intrinsic(ctx
, "llvm.amdgcn.cubesc",
445 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
446 out
->ma
= ac_build_intrinsic(ctx
, "llvm.amdgcn.cubema",
447 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
448 out
->id
= ac_build_intrinsic(ctx
, "llvm.amdgcn.cubeid",
449 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
453 * Build a manual selection sequence for cube face sc/tc coordinates and
454 * major axis vector (multiplied by 2 for consistency) for the given
455 * vec3 \p coords, for the face implied by \p selcoords.
457 * For the major axis, we always adjust the sign to be in the direction of
458 * selcoords.ma; i.e., a positive out_ma means that coords is pointed towards
459 * the selcoords major axis.
461 static void build_cube_select(struct ac_llvm_context
*ctx
,
462 const struct cube_selection_coords
*selcoords
,
463 const LLVMValueRef
*coords
,
464 LLVMValueRef
*out_st
,
465 LLVMValueRef
*out_ma
)
467 LLVMBuilderRef builder
= ctx
->builder
;
468 LLVMTypeRef f32
= LLVMTypeOf(coords
[0]);
469 LLVMValueRef is_ma_positive
;
471 LLVMValueRef is_ma_z
, is_not_ma_z
;
472 LLVMValueRef is_ma_y
;
473 LLVMValueRef is_ma_x
;
477 is_ma_positive
= LLVMBuildFCmp(builder
, LLVMRealUGE
,
478 selcoords
->ma
, LLVMConstReal(f32
, 0.0), "");
479 sgn_ma
= LLVMBuildSelect(builder
, is_ma_positive
,
480 LLVMConstReal(f32
, 1.0), LLVMConstReal(f32
, -1.0), "");
482 is_ma_z
= LLVMBuildFCmp(builder
, LLVMRealUGE
, selcoords
->id
, LLVMConstReal(f32
, 4.0), "");
483 is_not_ma_z
= LLVMBuildNot(builder
, is_ma_z
, "");
484 is_ma_y
= LLVMBuildAnd(builder
, is_not_ma_z
,
485 LLVMBuildFCmp(builder
, LLVMRealUGE
, selcoords
->id
, LLVMConstReal(f32
, 2.0), ""), "");
486 is_ma_x
= LLVMBuildAnd(builder
, is_not_ma_z
, LLVMBuildNot(builder
, is_ma_y
, ""), "");
489 tmp
= LLVMBuildSelect(builder
, is_ma_x
, coords
[2], coords
[0], "");
490 sgn
= LLVMBuildSelect(builder
, is_ma_y
, LLVMConstReal(f32
, 1.0),
491 LLVMBuildSelect(builder
, is_ma_z
, sgn_ma
,
492 LLVMBuildFNeg(builder
, sgn_ma
, ""), ""), "");
493 out_st
[0] = LLVMBuildFMul(builder
, tmp
, sgn
, "");
496 tmp
= LLVMBuildSelect(builder
, is_ma_y
, coords
[2], coords
[1], "");
497 sgn
= LLVMBuildSelect(builder
, is_ma_y
, sgn_ma
,
498 LLVMConstReal(f32
, -1.0), "");
499 out_st
[1] = LLVMBuildFMul(builder
, tmp
, sgn
, "");
502 tmp
= LLVMBuildSelect(builder
, is_ma_z
, coords
[2],
503 LLVMBuildSelect(builder
, is_ma_y
, coords
[1], coords
[0], ""), "");
504 tmp
= ac_build_intrinsic(ctx
, "llvm.fabs.f32",
505 ctx
->f32
, &tmp
, 1, AC_FUNC_ATTR_READNONE
);
506 *out_ma
= LLVMBuildFMul(builder
, tmp
, LLVMConstReal(f32
, 2.0), "");
510 ac_prepare_cube_coords(struct ac_llvm_context
*ctx
,
511 bool is_deriv
, bool is_array
, bool is_lod
,
512 LLVMValueRef
*coords_arg
,
513 LLVMValueRef
*derivs_arg
)
516 LLVMBuilderRef builder
= ctx
->builder
;
517 struct cube_selection_coords selcoords
;
518 LLVMValueRef coords
[3];
521 if (is_array
&& !is_lod
) {
522 LLVMValueRef tmp
= coords_arg
[3];
523 tmp
= ac_build_intrinsic(ctx
, "llvm.rint.f32", ctx
->f32
, &tmp
, 1, 0);
525 /* Section 8.9 (Texture Functions) of the GLSL 4.50 spec says:
527 * "For Array forms, the array layer used will be
529 * max(0, min(d−1, floor(layer+0.5)))
531 * where d is the depth of the texture array and layer
532 * comes from the component indicated in the tables below.
533 * Workaroudn for an issue where the layer is taken from a
534 * helper invocation which happens to fall on a different
535 * layer due to extrapolation."
537 * VI and earlier attempt to implement this in hardware by
538 * clamping the value of coords[2] = (8 * layer) + face.
539 * Unfortunately, this means that the we end up with the wrong
540 * face when clamping occurs.
542 * Clamp the layer earlier to work around the issue.
544 if (ctx
->chip_class
<= VI
) {
546 ge0
= LLVMBuildFCmp(builder
, LLVMRealOGE
, tmp
, ctx
->f32_0
, "");
547 tmp
= LLVMBuildSelect(builder
, ge0
, tmp
, ctx
->f32_0
, "");
553 build_cube_intrinsic(ctx
, coords_arg
, &selcoords
);
555 invma
= ac_build_intrinsic(ctx
, "llvm.fabs.f32",
556 ctx
->f32
, &selcoords
.ma
, 1, AC_FUNC_ATTR_READNONE
);
557 invma
= ac_build_fdiv(ctx
, LLVMConstReal(ctx
->f32
, 1.0), invma
);
559 for (int i
= 0; i
< 2; ++i
)
560 coords
[i
] = LLVMBuildFMul(builder
, selcoords
.stc
[i
], invma
, "");
562 coords
[2] = selcoords
.id
;
564 if (is_deriv
&& derivs_arg
) {
565 LLVMValueRef derivs
[4];
568 /* Convert cube derivatives to 2D derivatives. */
569 for (axis
= 0; axis
< 2; axis
++) {
570 LLVMValueRef deriv_st
[2];
571 LLVMValueRef deriv_ma
;
573 /* Transform the derivative alongside the texture
574 * coordinate. Mathematically, the correct formula is
575 * as follows. Assume we're projecting onto the +Z face
576 * and denote by dx/dh the derivative of the (original)
577 * X texture coordinate with respect to horizontal
578 * window coordinates. The projection onto the +Z face
583 * Then df/dh = df/dx * dx/dh + df/dz * dz/dh
584 * = 1/z * dx/dh - x/z * 1/z * dz/dh.
586 * This motivatives the implementation below.
588 * Whether this actually gives the expected results for
589 * apps that might feed in derivatives obtained via
590 * finite differences is anyone's guess. The OpenGL spec
591 * seems awfully quiet about how textureGrad for cube
592 * maps should be handled.
594 build_cube_select(ctx
, &selcoords
, &derivs_arg
[axis
* 3],
595 deriv_st
, &deriv_ma
);
597 deriv_ma
= LLVMBuildFMul(builder
, deriv_ma
, invma
, "");
599 for (int i
= 0; i
< 2; ++i
)
600 derivs
[axis
* 2 + i
] =
601 LLVMBuildFSub(builder
,
602 LLVMBuildFMul(builder
, deriv_st
[i
], invma
, ""),
603 LLVMBuildFMul(builder
, deriv_ma
, coords
[i
], ""), "");
606 memcpy(derivs_arg
, derivs
, sizeof(derivs
));
609 /* Shift the texture coordinate. This must be applied after the
610 * derivative calculation.
612 for (int i
= 0; i
< 2; ++i
)
613 coords
[i
] = LLVMBuildFAdd(builder
, coords
[i
], LLVMConstReal(ctx
->f32
, 1.5), "");
616 /* for cube arrays coord.z = coord.w(array_index) * 8 + face */
617 /* coords_arg.w component - array_index for cube arrays */
618 LLVMValueRef tmp
= LLVMBuildFMul(ctx
->builder
, coords_arg
[3], LLVMConstReal(ctx
->f32
, 8.0), "");
619 coords
[2] = LLVMBuildFAdd(ctx
->builder
, tmp
, coords
[2], "");
622 memcpy(coords_arg
, coords
, sizeof(coords
));
627 ac_build_fs_interp(struct ac_llvm_context
*ctx
,
628 LLVMValueRef llvm_chan
,
629 LLVMValueRef attr_number
,
634 LLVMValueRef args
[5];
637 if (HAVE_LLVM
< 0x0400) {
639 ij
[0] = LLVMBuildBitCast(ctx
->builder
, i
, ctx
->i32
, "");
640 ij
[1] = LLVMBuildBitCast(ctx
->builder
, j
, ctx
->i32
, "");
643 args
[1] = attr_number
;
645 args
[3] = ac_build_gather_values(ctx
, ij
, 2);
646 return ac_build_intrinsic(ctx
, "llvm.SI.fs.interp",
648 AC_FUNC_ATTR_READNONE
);
653 args
[2] = attr_number
;
656 p1
= ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p1",
657 ctx
->f32
, args
, 4, AC_FUNC_ATTR_READNONE
);
662 args
[3] = attr_number
;
665 return ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p2",
666 ctx
->f32
, args
, 5, AC_FUNC_ATTR_READNONE
);
670 ac_build_fs_interp_mov(struct ac_llvm_context
*ctx
,
671 LLVMValueRef parameter
,
672 LLVMValueRef llvm_chan
,
673 LLVMValueRef attr_number
,
676 LLVMValueRef args
[4];
677 if (HAVE_LLVM
< 0x0400) {
679 args
[1] = attr_number
;
682 return ac_build_intrinsic(ctx
,
683 "llvm.SI.fs.constant",
685 AC_FUNC_ATTR_READNONE
);
690 args
[2] = attr_number
;
693 return ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.mov",
694 ctx
->f32
, args
, 4, AC_FUNC_ATTR_READNONE
);
698 ac_build_gep0(struct ac_llvm_context
*ctx
,
699 LLVMValueRef base_ptr
,
702 LLVMValueRef indices
[2] = {
703 LLVMConstInt(ctx
->i32
, 0, 0),
706 return LLVMBuildGEP(ctx
->builder
, base_ptr
,
711 ac_build_indexed_store(struct ac_llvm_context
*ctx
,
712 LLVMValueRef base_ptr
, LLVMValueRef index
,
715 LLVMBuildStore(ctx
->builder
, value
,
716 ac_build_gep0(ctx
, base_ptr
, index
));
720 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad.
721 * It's equivalent to doing a load from &base_ptr[index].
723 * \param base_ptr Where the array starts.
724 * \param index The element index into the array.
725 * \param uniform Whether the base_ptr and index can be assumed to be
726 * dynamically uniform (i.e. load to an SGPR)
727 * \param invariant Whether the load is invariant (no other opcodes affect it)
730 ac_build_load_custom(struct ac_llvm_context
*ctx
, LLVMValueRef base_ptr
,
731 LLVMValueRef index
, bool uniform
, bool invariant
)
733 LLVMValueRef pointer
, result
;
735 pointer
= ac_build_gep0(ctx
, base_ptr
, index
);
737 LLVMSetMetadata(pointer
, ctx
->uniform_md_kind
, ctx
->empty_md
);
738 result
= LLVMBuildLoad(ctx
->builder
, pointer
, "");
740 LLVMSetMetadata(result
, ctx
->invariant_load_md_kind
, ctx
->empty_md
);
744 LLVMValueRef
ac_build_load(struct ac_llvm_context
*ctx
, LLVMValueRef base_ptr
,
747 return ac_build_load_custom(ctx
, base_ptr
, index
, false, false);
750 LLVMValueRef
ac_build_load_invariant(struct ac_llvm_context
*ctx
,
751 LLVMValueRef base_ptr
, LLVMValueRef index
)
753 return ac_build_load_custom(ctx
, base_ptr
, index
, false, true);
756 LLVMValueRef
ac_build_load_to_sgpr(struct ac_llvm_context
*ctx
,
757 LLVMValueRef base_ptr
, LLVMValueRef index
)
759 return ac_build_load_custom(ctx
, base_ptr
, index
, true, true);
762 /* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
763 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
764 * or v4i32 (num_channels=3,4).
767 ac_build_buffer_store_dword(struct ac_llvm_context
*ctx
,
770 unsigned num_channels
,
771 LLVMValueRef voffset
,
772 LLVMValueRef soffset
,
773 unsigned inst_offset
,
776 bool writeonly_memory
,
777 bool swizzle_enable_hint
)
779 /* SWIZZLE_ENABLE requires that soffset isn't folded into voffset
780 * (voffset is swizzled, but soffset isn't swizzled).
781 * llvm.amdgcn.buffer.store doesn't have a separate soffset parameter.
783 if (!swizzle_enable_hint
) {
784 /* Split 3 channel stores, becase LLVM doesn't support 3-channel
786 if (num_channels
== 3) {
787 LLVMValueRef v
[3], v01
;
789 for (int i
= 0; i
< 3; i
++) {
790 v
[i
] = LLVMBuildExtractElement(ctx
->builder
, vdata
,
791 LLVMConstInt(ctx
->i32
, i
, 0), "");
793 v01
= ac_build_gather_values(ctx
, v
, 2);
795 ac_build_buffer_store_dword(ctx
, rsrc
, v01
, 2, voffset
,
796 soffset
, inst_offset
, glc
, slc
,
797 writeonly_memory
, swizzle_enable_hint
);
798 ac_build_buffer_store_dword(ctx
, rsrc
, v
[2], 1, voffset
,
799 soffset
, inst_offset
+ 8,
801 writeonly_memory
, swizzle_enable_hint
);
805 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
806 static const char *types
[] = {"f32", "v2f32", "v4f32"};
808 LLVMValueRef offset
= soffset
;
811 offset
= LLVMBuildAdd(ctx
->builder
, offset
,
812 LLVMConstInt(ctx
->i32
, inst_offset
, 0), "");
814 offset
= LLVMBuildAdd(ctx
->builder
, offset
, voffset
, "");
816 LLVMValueRef args
[] = {
817 ac_to_float(ctx
, vdata
),
818 LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, ""),
819 LLVMConstInt(ctx
->i32
, 0, 0),
821 LLVMConstInt(ctx
->i1
, glc
, 0),
822 LLVMConstInt(ctx
->i1
, slc
, 0),
825 snprintf(name
, sizeof(name
), "llvm.amdgcn.buffer.store.%s",
828 ac_build_intrinsic(ctx
, name
, ctx
->voidt
,
829 args
, ARRAY_SIZE(args
),
831 AC_FUNC_ATTR_INACCESSIBLE_MEM_ONLY
:
832 AC_FUNC_ATTR_WRITEONLY
);
836 static unsigned dfmt
[] = {
837 V_008F0C_BUF_DATA_FORMAT_32
,
838 V_008F0C_BUF_DATA_FORMAT_32_32
,
839 V_008F0C_BUF_DATA_FORMAT_32_32_32
,
840 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
842 assert(num_channels
>= 1 && num_channels
<= 4);
844 LLVMValueRef args
[] = {
847 LLVMConstInt(ctx
->i32
, num_channels
, 0),
848 voffset
? voffset
: LLVMGetUndef(ctx
->i32
),
850 LLVMConstInt(ctx
->i32
, inst_offset
, 0),
851 LLVMConstInt(ctx
->i32
, dfmt
[num_channels
- 1], 0),
852 LLVMConstInt(ctx
->i32
, V_008F0C_BUF_NUM_FORMAT_UINT
, 0),
853 LLVMConstInt(ctx
->i32
, voffset
!= NULL
, 0),
854 LLVMConstInt(ctx
->i32
, 0, 0), /* idxen */
855 LLVMConstInt(ctx
->i32
, glc
, 0),
856 LLVMConstInt(ctx
->i32
, slc
, 0),
857 LLVMConstInt(ctx
->i32
, 0, 0), /* tfe*/
860 /* The instruction offset field has 12 bits */
861 assert(voffset
|| inst_offset
< (1 << 12));
863 /* The intrinsic is overloaded, we need to add a type suffix for overloading to work. */
864 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
865 const char *types
[] = {"i32", "v2i32", "v4i32"};
867 snprintf(name
, sizeof(name
), "llvm.SI.tbuffer.store.%s", types
[func
]);
869 ac_build_intrinsic(ctx
, name
, ctx
->voidt
,
870 args
, ARRAY_SIZE(args
),
871 AC_FUNC_ATTR_LEGACY
);
875 ac_build_buffer_load(struct ac_llvm_context
*ctx
,
879 LLVMValueRef voffset
,
880 LLVMValueRef soffset
,
881 unsigned inst_offset
,
887 LLVMValueRef offset
= LLVMConstInt(ctx
->i32
, inst_offset
, 0);
889 offset
= LLVMBuildAdd(ctx
->builder
, offset
, voffset
, "");
891 offset
= LLVMBuildAdd(ctx
->builder
, offset
, soffset
, "");
893 /* TODO: VI and later generations can use SMEM with GLC=1.*/
894 if (allow_smem
&& !glc
&& !slc
) {
895 assert(vindex
== NULL
);
897 LLVMValueRef result
[4];
899 for (int i
= 0; i
< num_channels
; i
++) {
901 offset
= LLVMBuildAdd(ctx
->builder
, offset
,
902 LLVMConstInt(ctx
->i32
, 4, 0), "");
904 LLVMValueRef args
[2] = {rsrc
, offset
};
905 result
[i
] = ac_build_intrinsic(ctx
, "llvm.SI.load.const.v4i32",
907 AC_FUNC_ATTR_READNONE
|
908 AC_FUNC_ATTR_LEGACY
);
910 if (num_channels
== 1)
913 if (num_channels
== 3)
914 result
[num_channels
++] = LLVMGetUndef(ctx
->f32
);
915 return ac_build_gather_values(ctx
, result
, num_channels
);
918 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
920 LLVMValueRef args
[] = {
921 LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, ""),
922 vindex
? vindex
: LLVMConstInt(ctx
->i32
, 0, 0),
924 LLVMConstInt(ctx
->i1
, glc
, 0),
925 LLVMConstInt(ctx
->i1
, slc
, 0)
928 LLVMTypeRef types
[] = {ctx
->f32
, LLVMVectorType(ctx
->f32
, 2),
930 const char *type_names
[] = {"f32", "v2f32", "v4f32"};
933 snprintf(name
, sizeof(name
), "llvm.amdgcn.buffer.load.%s",
936 return ac_build_intrinsic(ctx
, name
, types
[func
], args
,
938 /* READNONE means writes can't affect it, while
939 * READONLY means that writes can affect it. */
940 can_speculate
&& HAVE_LLVM
>= 0x0400 ?
941 AC_FUNC_ATTR_READNONE
:
942 AC_FUNC_ATTR_READONLY
);
945 LLVMValueRef
ac_build_buffer_load_format(struct ac_llvm_context
*ctx
,
948 LLVMValueRef voffset
,
951 LLVMValueRef args
[] = {
952 LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, ""),
955 ctx
->i1false
, /* glc */
956 ctx
->i1false
, /* slc */
959 return ac_build_intrinsic(ctx
,
960 "llvm.amdgcn.buffer.load.format.v4f32",
961 ctx
->v4f32
, args
, ARRAY_SIZE(args
),
962 /* READNONE means writes can't affect it, while
963 * READONLY means that writes can affect it. */
964 can_speculate
&& HAVE_LLVM
>= 0x0400 ?
965 AC_FUNC_ATTR_READNONE
:
966 AC_FUNC_ATTR_READONLY
);
970 * Set range metadata on an instruction. This can only be used on load and
971 * call instructions. If you know an instruction can only produce the values
972 * 0, 1, 2, you would do set_range_metadata(value, 0, 3);
973 * \p lo is the minimum value inclusive.
974 * \p hi is the maximum value exclusive.
976 static void set_range_metadata(struct ac_llvm_context
*ctx
,
977 LLVMValueRef value
, unsigned lo
, unsigned hi
)
979 LLVMValueRef range_md
, md_args
[2];
980 LLVMTypeRef type
= LLVMTypeOf(value
);
981 LLVMContextRef context
= LLVMGetTypeContext(type
);
983 md_args
[0] = LLVMConstInt(type
, lo
, false);
984 md_args
[1] = LLVMConstInt(type
, hi
, false);
985 range_md
= LLVMMDNodeInContext(context
, md_args
, 2);
986 LLVMSetMetadata(value
, ctx
->range_md_kind
, range_md
);
990 ac_get_thread_id(struct ac_llvm_context
*ctx
)
994 LLVMValueRef tid_args
[2];
995 tid_args
[0] = LLVMConstInt(ctx
->i32
, 0xffffffff, false);
996 tid_args
[1] = LLVMConstInt(ctx
->i32
, 0, false);
997 tid_args
[1] = ac_build_intrinsic(ctx
,
998 "llvm.amdgcn.mbcnt.lo", ctx
->i32
,
999 tid_args
, 2, AC_FUNC_ATTR_READNONE
);
1001 tid
= ac_build_intrinsic(ctx
, "llvm.amdgcn.mbcnt.hi",
1003 2, AC_FUNC_ATTR_READNONE
);
1004 set_range_metadata(ctx
, tid
, 0, 64);
1009 * SI implements derivatives using the local data store (LDS)
1010 * All writes to the LDS happen in all executing threads at
1011 * the same time. TID is the Thread ID for the current
1012 * thread and is a value between 0 and 63, representing
1013 * the thread's position in the wavefront.
1015 * For the pixel shader threads are grouped into quads of four pixels.
1016 * The TIDs of the pixels of a quad are:
1024 * So, masking the TID with 0xfffffffc yields the TID of the top left pixel
1025 * of the quad, masking with 0xfffffffd yields the TID of the top pixel of
1026 * the current pixel's column, and masking with 0xfffffffe yields the TID
1027 * of the left pixel of the current pixel's row.
1029 * Adding 1 yields the TID of the pixel to the right of the left pixel, and
1030 * adding 2 yields the TID of the pixel below the top pixel.
1033 ac_build_ddxy(struct ac_llvm_context
*ctx
,
1038 LLVMValueRef tl
, trbl
, args
[2];
1039 LLVMValueRef result
;
1041 if (ctx
->chip_class
>= VI
) {
1042 LLVMValueRef thread_id
, tl_tid
, trbl_tid
;
1043 thread_id
= ac_get_thread_id(ctx
);
1045 tl_tid
= LLVMBuildAnd(ctx
->builder
, thread_id
,
1046 LLVMConstInt(ctx
->i32
, mask
, false), "");
1048 trbl_tid
= LLVMBuildAdd(ctx
->builder
, tl_tid
,
1049 LLVMConstInt(ctx
->i32
, idx
, false), "");
1051 args
[0] = LLVMBuildMul(ctx
->builder
, tl_tid
,
1052 LLVMConstInt(ctx
->i32
, 4, false), "");
1054 tl
= ac_build_intrinsic(ctx
,
1055 "llvm.amdgcn.ds.bpermute", ctx
->i32
,
1057 AC_FUNC_ATTR_READNONE
|
1058 AC_FUNC_ATTR_CONVERGENT
);
1060 args
[0] = LLVMBuildMul(ctx
->builder
, trbl_tid
,
1061 LLVMConstInt(ctx
->i32
, 4, false), "");
1062 trbl
= ac_build_intrinsic(ctx
,
1063 "llvm.amdgcn.ds.bpermute", ctx
->i32
,
1065 AC_FUNC_ATTR_READNONE
|
1066 AC_FUNC_ATTR_CONVERGENT
);
1068 uint32_t masks
[2] = {};
1071 case AC_TID_MASK_TOP_LEFT
:
1079 case AC_TID_MASK_TOP
:
1083 case AC_TID_MASK_LEFT
:
1092 args
[1] = LLVMConstInt(ctx
->i32
, masks
[0], false);
1094 tl
= ac_build_intrinsic(ctx
,
1095 "llvm.amdgcn.ds.swizzle", ctx
->i32
,
1097 AC_FUNC_ATTR_READNONE
|
1098 AC_FUNC_ATTR_CONVERGENT
);
1100 args
[1] = LLVMConstInt(ctx
->i32
, masks
[1], false);
1101 trbl
= ac_build_intrinsic(ctx
,
1102 "llvm.amdgcn.ds.swizzle", ctx
->i32
,
1104 AC_FUNC_ATTR_READNONE
|
1105 AC_FUNC_ATTR_CONVERGENT
);
1108 tl
= LLVMBuildBitCast(ctx
->builder
, tl
, ctx
->f32
, "");
1109 trbl
= LLVMBuildBitCast(ctx
->builder
, trbl
, ctx
->f32
, "");
1110 result
= LLVMBuildFSub(ctx
->builder
, trbl
, tl
, "");
1115 ac_build_sendmsg(struct ac_llvm_context
*ctx
,
1117 LLVMValueRef wave_id
)
1119 LLVMValueRef args
[2];
1120 const char *intr_name
= (HAVE_LLVM
< 0x0400) ? "llvm.SI.sendmsg" : "llvm.amdgcn.s.sendmsg";
1121 args
[0] = LLVMConstInt(ctx
->i32
, msg
, false);
1123 ac_build_intrinsic(ctx
, intr_name
, ctx
->voidt
, args
, 2, 0);
1127 ac_build_imsb(struct ac_llvm_context
*ctx
,
1129 LLVMTypeRef dst_type
)
1131 const char *intr_name
= (HAVE_LLVM
< 0x0400) ? "llvm.AMDGPU.flbit.i32" :
1132 "llvm.amdgcn.sffbh.i32";
1133 LLVMValueRef msb
= ac_build_intrinsic(ctx
, intr_name
,
1135 AC_FUNC_ATTR_READNONE
);
1137 /* The HW returns the last bit index from MSB, but NIR/TGSI wants
1138 * the index from LSB. Invert it by doing "31 - msb". */
1139 msb
= LLVMBuildSub(ctx
->builder
, LLVMConstInt(ctx
->i32
, 31, false),
1142 LLVMValueRef all_ones
= LLVMConstInt(ctx
->i32
, -1, true);
1143 LLVMValueRef cond
= LLVMBuildOr(ctx
->builder
,
1144 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
1145 arg
, LLVMConstInt(ctx
->i32
, 0, 0), ""),
1146 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
1147 arg
, all_ones
, ""), "");
1149 return LLVMBuildSelect(ctx
->builder
, cond
, all_ones
, msb
, "");
1153 ac_build_umsb(struct ac_llvm_context
*ctx
,
1155 LLVMTypeRef dst_type
)
1157 LLVMValueRef args
[2] = {
1161 LLVMValueRef msb
= ac_build_intrinsic(ctx
, "llvm.ctlz.i32",
1162 dst_type
, args
, ARRAY_SIZE(args
),
1163 AC_FUNC_ATTR_READNONE
);
1165 /* The HW returns the last bit index from MSB, but TGSI/NIR wants
1166 * the index from LSB. Invert it by doing "31 - msb". */
1167 msb
= LLVMBuildSub(ctx
->builder
, LLVMConstInt(ctx
->i32
, 31, false),
1170 /* check for zero */
1171 return LLVMBuildSelect(ctx
->builder
,
1172 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, arg
,
1173 LLVMConstInt(ctx
->i32
, 0, 0), ""),
1174 LLVMConstInt(ctx
->i32
, -1, true), msb
, "");
1177 LLVMValueRef
ac_build_umin(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
1180 LLVMValueRef cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntULE
, a
, b
, "");
1181 return LLVMBuildSelect(ctx
->builder
, cmp
, a
, b
, "");
1184 LLVMValueRef
ac_build_clamp(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
1186 if (HAVE_LLVM
>= 0x0500) {
1187 LLVMValueRef max
[2] = {
1189 LLVMConstReal(ctx
->f32
, 0),
1191 LLVMValueRef min
[2] = {
1192 LLVMConstReal(ctx
->f32
, 1),
1195 min
[1] = ac_build_intrinsic(ctx
, "llvm.maxnum.f32",
1197 AC_FUNC_ATTR_READNONE
);
1198 return ac_build_intrinsic(ctx
, "llvm.minnum.f32",
1200 AC_FUNC_ATTR_READNONE
);
1203 LLVMValueRef args
[3] = {
1205 LLVMConstReal(ctx
->f32
, 0),
1206 LLVMConstReal(ctx
->f32
, 1),
1209 return ac_build_intrinsic(ctx
, "llvm.AMDGPU.clamp.", ctx
->f32
, args
, 3,
1210 AC_FUNC_ATTR_READNONE
|
1211 AC_FUNC_ATTR_LEGACY
);
1214 void ac_build_export(struct ac_llvm_context
*ctx
, struct ac_export_args
*a
)
1216 LLVMValueRef args
[9];
1218 if (HAVE_LLVM
>= 0x0500) {
1219 args
[0] = LLVMConstInt(ctx
->i32
, a
->target
, 0);
1220 args
[1] = LLVMConstInt(ctx
->i32
, a
->enabled_channels
, 0);
1223 LLVMTypeRef i16
= LLVMInt16TypeInContext(ctx
->context
);
1224 LLVMTypeRef v2i16
= LLVMVectorType(i16
, 2);
1226 args
[2] = LLVMBuildBitCast(ctx
->builder
, a
->out
[0],
1228 args
[3] = LLVMBuildBitCast(ctx
->builder
, a
->out
[1],
1230 args
[4] = LLVMConstInt(ctx
->i1
, a
->done
, 0);
1231 args
[5] = LLVMConstInt(ctx
->i1
, a
->valid_mask
, 0);
1233 ac_build_intrinsic(ctx
, "llvm.amdgcn.exp.compr.v2i16",
1234 ctx
->voidt
, args
, 6, 0);
1236 args
[2] = a
->out
[0];
1237 args
[3] = a
->out
[1];
1238 args
[4] = a
->out
[2];
1239 args
[5] = a
->out
[3];
1240 args
[6] = LLVMConstInt(ctx
->i1
, a
->done
, 0);
1241 args
[7] = LLVMConstInt(ctx
->i1
, a
->valid_mask
, 0);
1243 ac_build_intrinsic(ctx
, "llvm.amdgcn.exp.f32",
1244 ctx
->voidt
, args
, 8, 0);
1249 args
[0] = LLVMConstInt(ctx
->i32
, a
->enabled_channels
, 0);
1250 args
[1] = LLVMConstInt(ctx
->i32
, a
->valid_mask
, 0);
1251 args
[2] = LLVMConstInt(ctx
->i32
, a
->done
, 0);
1252 args
[3] = LLVMConstInt(ctx
->i32
, a
->target
, 0);
1253 args
[4] = LLVMConstInt(ctx
->i32
, a
->compr
, 0);
1254 memcpy(args
+ 5, a
->out
, sizeof(a
->out
[0]) * 4);
1256 ac_build_intrinsic(ctx
, "llvm.SI.export", ctx
->voidt
, args
, 9,
1257 AC_FUNC_ATTR_LEGACY
);
1260 LLVMValueRef
ac_build_image_opcode(struct ac_llvm_context
*ctx
,
1261 struct ac_image_args
*a
)
1263 LLVMTypeRef dst_type
;
1264 LLVMValueRef args
[11];
1265 unsigned num_args
= 0;
1266 const char *name
= NULL
;
1267 char intr_name
[128], type
[64];
1269 if (HAVE_LLVM
>= 0x0400) {
1270 bool sample
= a
->opcode
== ac_image_sample
||
1271 a
->opcode
== ac_image_gather4
||
1272 a
->opcode
== ac_image_get_lod
;
1275 args
[num_args
++] = ac_to_float(ctx
, a
->addr
);
1277 args
[num_args
++] = a
->addr
;
1279 args
[num_args
++] = a
->resource
;
1281 args
[num_args
++] = a
->sampler
;
1282 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->dmask
, 0);
1284 args
[num_args
++] = LLVMConstInt(ctx
->i1
, a
->unorm
, 0);
1285 args
[num_args
++] = ctx
->i1false
; /* glc */
1286 args
[num_args
++] = ctx
->i1false
; /* slc */
1287 args
[num_args
++] = ctx
->i1false
; /* lwe */
1288 args
[num_args
++] = LLVMConstInt(ctx
->i1
, a
->da
, 0);
1290 switch (a
->opcode
) {
1291 case ac_image_sample
:
1292 name
= "llvm.amdgcn.image.sample";
1294 case ac_image_gather4
:
1295 name
= "llvm.amdgcn.image.gather4";
1298 name
= "llvm.amdgcn.image.load";
1300 case ac_image_load_mip
:
1301 name
= "llvm.amdgcn.image.load.mip";
1303 case ac_image_get_lod
:
1304 name
= "llvm.amdgcn.image.getlod";
1306 case ac_image_get_resinfo
:
1307 name
= "llvm.amdgcn.image.getresinfo";
1310 unreachable("invalid image opcode");
1313 ac_build_type_name_for_intr(LLVMTypeOf(args
[0]), type
,
1316 snprintf(intr_name
, sizeof(intr_name
), "%s%s%s%s.v4f32.%s.v8i32",
1318 a
->compare
? ".c" : "",
1322 a
->level_zero
? ".lz" : "",
1323 a
->offset
? ".o" : "",
1326 LLVMValueRef result
=
1327 ac_build_intrinsic(ctx
, intr_name
,
1328 ctx
->v4f32
, args
, num_args
,
1329 AC_FUNC_ATTR_READNONE
);
1331 result
= LLVMBuildBitCast(ctx
->builder
, result
,
1337 args
[num_args
++] = a
->addr
;
1338 args
[num_args
++] = a
->resource
;
1340 if (a
->opcode
== ac_image_load
||
1341 a
->opcode
== ac_image_load_mip
||
1342 a
->opcode
== ac_image_get_resinfo
) {
1343 dst_type
= ctx
->v4i32
;
1345 dst_type
= ctx
->v4f32
;
1346 args
[num_args
++] = a
->sampler
;
1349 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->dmask
, 0);
1350 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->unorm
, 0);
1351 args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* r128 */
1352 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->da
, 0);
1353 args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* glc */
1354 args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* slc */
1355 args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* tfe */
1356 args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* lwe */
1358 switch (a
->opcode
) {
1359 case ac_image_sample
:
1360 name
= "llvm.SI.image.sample";
1362 case ac_image_gather4
:
1363 name
= "llvm.SI.gather4";
1366 name
= "llvm.SI.image.load";
1368 case ac_image_load_mip
:
1369 name
= "llvm.SI.image.load.mip";
1371 case ac_image_get_lod
:
1372 name
= "llvm.SI.getlod";
1374 case ac_image_get_resinfo
:
1375 name
= "llvm.SI.getresinfo";
1379 ac_build_type_name_for_intr(LLVMTypeOf(a
->addr
), type
, sizeof(type
));
1380 snprintf(intr_name
, sizeof(intr_name
), "%s%s%s%s.%s",
1382 a
->compare
? ".c" : "",
1386 a
->level_zero
? ".lz" : "",
1387 a
->offset
? ".o" : "",
1390 return ac_build_intrinsic(ctx
, intr_name
,
1391 dst_type
, args
, num_args
,
1392 AC_FUNC_ATTR_READNONE
|
1393 AC_FUNC_ATTR_LEGACY
);
1396 LLVMValueRef
ac_build_cvt_pkrtz_f16(struct ac_llvm_context
*ctx
,
1397 LLVMValueRef args
[2])
1399 if (HAVE_LLVM
>= 0x0500) {
1401 LLVMVectorType(LLVMHalfTypeInContext(ctx
->context
), 2);
1403 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pkrtz",
1405 AC_FUNC_ATTR_READNONE
);
1406 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
1409 return ac_build_intrinsic(ctx
, "llvm.SI.packf16", ctx
->i32
, args
, 2,
1410 AC_FUNC_ATTR_READNONE
|
1411 AC_FUNC_ATTR_LEGACY
);
1414 LLVMValueRef
ac_build_wqm_vote(struct ac_llvm_context
*ctx
, LLVMValueRef i1
)
1416 assert(HAVE_LLVM
>= 0x0600);
1417 return ac_build_intrinsic(ctx
, "llvm.amdgcn.wqm.vote", ctx
->i1
,
1418 &i1
, 1, AC_FUNC_ATTR_READNONE
);
1421 void ac_build_kill_if_false(struct ac_llvm_context
*ctx
, LLVMValueRef i1
)
1423 if (HAVE_LLVM
>= 0x0600) {
1424 ac_build_intrinsic(ctx
, "llvm.amdgcn.kill", ctx
->voidt
,
1429 LLVMValueRef value
= LLVMBuildSelect(ctx
->builder
, i1
,
1430 LLVMConstReal(ctx
->f32
, 1),
1431 LLVMConstReal(ctx
->f32
, -1), "");
1432 ac_build_intrinsic(ctx
, "llvm.AMDGPU.kill", ctx
->voidt
,
1433 &value
, 1, AC_FUNC_ATTR_LEGACY
);
1436 LLVMValueRef
ac_build_bfe(struct ac_llvm_context
*ctx
, LLVMValueRef input
,
1437 LLVMValueRef offset
, LLVMValueRef width
,
1440 LLVMValueRef args
[] = {
1446 if (HAVE_LLVM
>= 0x0500) {
1447 return ac_build_intrinsic(ctx
,
1448 is_signed
? "llvm.amdgcn.sbfe.i32" :
1449 "llvm.amdgcn.ubfe.i32",
1451 AC_FUNC_ATTR_READNONE
);
1454 return ac_build_intrinsic(ctx
,
1455 is_signed
? "llvm.AMDGPU.bfe.i32" :
1456 "llvm.AMDGPU.bfe.u32",
1458 AC_FUNC_ATTR_READNONE
|
1459 AC_FUNC_ATTR_LEGACY
);
1462 void ac_get_image_intr_name(const char *base_name
,
1463 LLVMTypeRef data_type
,
1464 LLVMTypeRef coords_type
,
1465 LLVMTypeRef rsrc_type
,
1466 char *out_name
, unsigned out_len
)
1468 char coords_type_name
[8];
1470 ac_build_type_name_for_intr(coords_type
, coords_type_name
,
1471 sizeof(coords_type_name
));
1473 if (HAVE_LLVM
<= 0x0309) {
1474 snprintf(out_name
, out_len
, "%s.%s", base_name
, coords_type_name
);
1476 char data_type_name
[8];
1477 char rsrc_type_name
[8];
1479 ac_build_type_name_for_intr(data_type
, data_type_name
,
1480 sizeof(data_type_name
));
1481 ac_build_type_name_for_intr(rsrc_type
, rsrc_type_name
,
1482 sizeof(rsrc_type_name
));
1483 snprintf(out_name
, out_len
, "%s.%s.%s.%s", base_name
,
1484 data_type_name
, coords_type_name
, rsrc_type_name
);
1488 #define AC_EXP_TARGET (HAVE_LLVM >= 0x0500 ? 0 : 3)
1489 #define AC_EXP_OUT0 (HAVE_LLVM >= 0x0500 ? 2 : 5)
1497 struct ac_vs_exp_chan
1501 enum ac_ir_type type
;
1504 struct ac_vs_exp_inst
{
1507 struct ac_vs_exp_chan chan
[4];
1510 struct ac_vs_exports
{
1512 struct ac_vs_exp_inst exp
[VARYING_SLOT_MAX
];
1515 /* Return true if the PARAM export has been eliminated. */
1516 static bool ac_eliminate_const_output(uint8_t *vs_output_param_offset
,
1517 uint32_t num_outputs
,
1518 struct ac_vs_exp_inst
*exp
)
1520 unsigned i
, default_val
; /* SPI_PS_INPUT_CNTL_i.DEFAULT_VAL */
1521 bool is_zero
[4] = {}, is_one
[4] = {};
1523 for (i
= 0; i
< 4; i
++) {
1524 /* It's a constant expression. Undef outputs are eliminated too. */
1525 if (exp
->chan
[i
].type
== AC_IR_UNDEF
) {
1528 } else if (exp
->chan
[i
].type
== AC_IR_CONST
) {
1529 if (exp
->chan
[i
].const_float
== 0)
1531 else if (exp
->chan
[i
].const_float
== 1)
1534 return false; /* other constant */
1539 /* Only certain combinations of 0 and 1 can be eliminated. */
1540 if (is_zero
[0] && is_zero
[1] && is_zero
[2])
1541 default_val
= is_zero
[3] ? 0 : 1;
1542 else if (is_one
[0] && is_one
[1] && is_one
[2])
1543 default_val
= is_zero
[3] ? 2 : 3;
1547 /* The PARAM export can be represented as DEFAULT_VAL. Kill it. */
1548 LLVMInstructionEraseFromParent(exp
->inst
);
1550 /* Change OFFSET to DEFAULT_VAL. */
1551 for (i
= 0; i
< num_outputs
; i
++) {
1552 if (vs_output_param_offset
[i
] == exp
->offset
) {
1553 vs_output_param_offset
[i
] =
1554 AC_EXP_PARAM_DEFAULT_VAL_0000
+ default_val
;
1561 static bool ac_eliminate_duplicated_output(uint8_t *vs_output_param_offset
,
1562 uint32_t num_outputs
,
1563 struct ac_vs_exports
*processed
,
1564 struct ac_vs_exp_inst
*exp
)
1566 unsigned p
, copy_back_channels
= 0;
1568 /* See if the output is already in the list of processed outputs.
1569 * The LLVMValueRef comparison relies on SSA.
1571 for (p
= 0; p
< processed
->num
; p
++) {
1572 bool different
= false;
1574 for (unsigned j
= 0; j
< 4; j
++) {
1575 struct ac_vs_exp_chan
*c1
= &processed
->exp
[p
].chan
[j
];
1576 struct ac_vs_exp_chan
*c2
= &exp
->chan
[j
];
1578 /* Treat undef as a match. */
1579 if (c2
->type
== AC_IR_UNDEF
)
1582 /* If c1 is undef but c2 isn't, we can copy c2 to c1
1583 * and consider the instruction duplicated.
1585 if (c1
->type
== AC_IR_UNDEF
) {
1586 copy_back_channels
|= 1 << j
;
1590 /* Test whether the channels are not equal. */
1591 if (c1
->type
!= c2
->type
||
1592 (c1
->type
== AC_IR_CONST
&&
1593 c1
->const_float
!= c2
->const_float
) ||
1594 (c1
->type
== AC_IR_VALUE
&&
1595 c1
->value
!= c2
->value
)) {
1603 copy_back_channels
= 0;
1605 if (p
== processed
->num
)
1608 /* If a match was found, but the matching export has undef where the new
1609 * one has a normal value, copy the normal value to the undef channel.
1611 struct ac_vs_exp_inst
*match
= &processed
->exp
[p
];
1613 while (copy_back_channels
) {
1614 unsigned chan
= u_bit_scan(©_back_channels
);
1616 assert(match
->chan
[chan
].type
== AC_IR_UNDEF
);
1617 LLVMSetOperand(match
->inst
, AC_EXP_OUT0
+ chan
,
1618 exp
->chan
[chan
].value
);
1619 match
->chan
[chan
] = exp
->chan
[chan
];
1622 /* The PARAM export is duplicated. Kill it. */
1623 LLVMInstructionEraseFromParent(exp
->inst
);
1625 /* Change OFFSET to the matching export. */
1626 for (unsigned i
= 0; i
< num_outputs
; i
++) {
1627 if (vs_output_param_offset
[i
] == exp
->offset
) {
1628 vs_output_param_offset
[i
] = match
->offset
;
1635 void ac_optimize_vs_outputs(struct ac_llvm_context
*ctx
,
1636 LLVMValueRef main_fn
,
1637 uint8_t *vs_output_param_offset
,
1638 uint32_t num_outputs
,
1639 uint8_t *num_param_exports
)
1641 LLVMBasicBlockRef bb
;
1642 bool removed_any
= false;
1643 struct ac_vs_exports exports
;
1647 /* Process all LLVM instructions. */
1648 bb
= LLVMGetFirstBasicBlock(main_fn
);
1650 LLVMValueRef inst
= LLVMGetFirstInstruction(bb
);
1653 LLVMValueRef cur
= inst
;
1654 inst
= LLVMGetNextInstruction(inst
);
1655 struct ac_vs_exp_inst exp
;
1657 if (LLVMGetInstructionOpcode(cur
) != LLVMCall
)
1660 LLVMValueRef callee
= ac_llvm_get_called_value(cur
);
1662 if (!ac_llvm_is_function(callee
))
1665 const char *name
= LLVMGetValueName(callee
);
1666 unsigned num_args
= LLVMCountParams(callee
);
1668 /* Check if this is an export instruction. */
1669 if ((num_args
!= 9 && num_args
!= 8) ||
1670 (strcmp(name
, "llvm.SI.export") &&
1671 strcmp(name
, "llvm.amdgcn.exp.f32")))
1674 LLVMValueRef arg
= LLVMGetOperand(cur
, AC_EXP_TARGET
);
1675 unsigned target
= LLVMConstIntGetZExtValue(arg
);
1677 if (target
< V_008DFC_SQ_EXP_PARAM
)
1680 target
-= V_008DFC_SQ_EXP_PARAM
;
1682 /* Parse the instruction. */
1683 memset(&exp
, 0, sizeof(exp
));
1684 exp
.offset
= target
;
1687 for (unsigned i
= 0; i
< 4; i
++) {
1688 LLVMValueRef v
= LLVMGetOperand(cur
, AC_EXP_OUT0
+ i
);
1690 exp
.chan
[i
].value
= v
;
1692 if (LLVMIsUndef(v
)) {
1693 exp
.chan
[i
].type
= AC_IR_UNDEF
;
1694 } else if (LLVMIsAConstantFP(v
)) {
1695 LLVMBool loses_info
;
1696 exp
.chan
[i
].type
= AC_IR_CONST
;
1697 exp
.chan
[i
].const_float
=
1698 LLVMConstRealGetDouble(v
, &loses_info
);
1700 exp
.chan
[i
].type
= AC_IR_VALUE
;
1704 /* Eliminate constant and duplicated PARAM exports. */
1705 if (ac_eliminate_const_output(vs_output_param_offset
,
1706 num_outputs
, &exp
) ||
1707 ac_eliminate_duplicated_output(vs_output_param_offset
,
1708 num_outputs
, &exports
,
1712 exports
.exp
[exports
.num
++] = exp
;
1715 bb
= LLVMGetNextBasicBlock(bb
);
1718 /* Remove holes in export memory due to removed PARAM exports.
1719 * This is done by renumbering all PARAM exports.
1722 uint8_t old_offset
[VARYING_SLOT_MAX
];
1725 /* Make a copy of the offsets. We need the old version while
1726 * we are modifying some of them. */
1727 memcpy(old_offset
, vs_output_param_offset
,
1728 sizeof(old_offset
));
1730 for (i
= 0; i
< exports
.num
; i
++) {
1731 unsigned offset
= exports
.exp
[i
].offset
;
1733 /* Update vs_output_param_offset. Multiple outputs can
1734 * have the same offset.
1736 for (out
= 0; out
< num_outputs
; out
++) {
1737 if (old_offset
[out
] == offset
)
1738 vs_output_param_offset
[out
] = i
;
1741 /* Change the PARAM offset in the instruction. */
1742 LLVMSetOperand(exports
.exp
[i
].inst
, AC_EXP_TARGET
,
1743 LLVMConstInt(ctx
->i32
,
1744 V_008DFC_SQ_EXP_PARAM
+ i
, 0));
1746 *num_param_exports
= exports
.num
;
1750 void ac_init_exec_full_mask(struct ac_llvm_context
*ctx
)
1752 LLVMValueRef full_mask
= LLVMConstInt(ctx
->i64
, ~0ull, 0);
1753 ac_build_intrinsic(ctx
,
1754 "llvm.amdgcn.init.exec", ctx
->voidt
,
1755 &full_mask
, 1, AC_FUNC_ATTR_CONVERGENT
);
1758 void ac_declare_lds_as_pointer(struct ac_llvm_context
*ctx
)
1760 unsigned lds_size
= ctx
->chip_class
>= CIK
? 65536 : 32768;
1761 ctx
->lds
= LLVMBuildIntToPtr(ctx
->builder
, ctx
->i32_0
,
1762 LLVMPointerType(LLVMArrayType(ctx
->i32
, lds_size
/ 4), AC_LOCAL_ADDR_SPACE
),
1766 LLVMValueRef
ac_lds_load(struct ac_llvm_context
*ctx
,
1767 LLVMValueRef dw_addr
)
1769 return ac_build_load(ctx
, ctx
->lds
, dw_addr
);
1772 void ac_lds_store(struct ac_llvm_context
*ctx
,
1773 LLVMValueRef dw_addr
,
1776 value
= ac_to_integer(ctx
, value
);
1777 ac_build_indexed_store(ctx
, ctx
->lds
,
1781 LLVMValueRef
ac_find_lsb(struct ac_llvm_context
*ctx
,
1782 LLVMTypeRef dst_type
,
1785 LLVMValueRef params
[2] = {
1788 /* The value of 1 means that ffs(x=0) = undef, so LLVM won't
1789 * add special code to check for x=0. The reason is that
1790 * the LLVM behavior for x=0 is different from what we
1791 * need here. However, LLVM also assumes that ffs(x) is
1792 * in [0, 31], but GLSL expects that ffs(0) = -1, so
1793 * a conditional assignment to handle 0 is still required.
1795 * The hardware already implements the correct behavior.
1797 LLVMConstInt(ctx
->i1
, 1, false),
1800 LLVMValueRef lsb
= ac_build_intrinsic(ctx
, "llvm.cttz.i32", ctx
->i32
,
1802 AC_FUNC_ATTR_READNONE
);
1804 /* TODO: We need an intrinsic to skip this conditional. */
1805 /* Check for zero: */
1806 return LLVMBuildSelect(ctx
->builder
, LLVMBuildICmp(ctx
->builder
,
1809 LLVMConstInt(ctx
->i32
, -1, 0), lsb
, "");