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
->v4i32
= LLVMVectorType(ctx
->i32
, 4);
70 ctx
->v4f32
= LLVMVectorType(ctx
->f32
, 4);
71 ctx
->v8i32
= LLVMVectorType(ctx
->i32
, 8);
73 ctx
->i32_0
= LLVMConstInt(ctx
->i32
, 0, false);
74 ctx
->i32_1
= LLVMConstInt(ctx
->i32
, 1, false);
75 ctx
->f32_0
= LLVMConstReal(ctx
->f32
, 0.0);
76 ctx
->f32_1
= LLVMConstReal(ctx
->f32
, 1.0);
78 ctx
->i1false
= LLVMConstInt(ctx
->i1
, 0, false);
79 ctx
->i1true
= LLVMConstInt(ctx
->i1
, 1, false);
81 ctx
->range_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
84 ctx
->invariant_load_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
85 "invariant.load", 14);
87 ctx
->fpmath_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
, "fpmath", 6);
89 args
[0] = LLVMConstReal(ctx
->f32
, 2.5);
90 ctx
->fpmath_md_2p5_ulp
= LLVMMDNodeInContext(ctx
->context
, args
, 1);
92 ctx
->uniform_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
93 "amdgpu.uniform", 14);
95 ctx
->empty_md
= LLVMMDNodeInContext(ctx
->context
, NULL
, 0);
99 ac_get_type_size(LLVMTypeRef type
)
101 LLVMTypeKind kind
= LLVMGetTypeKind(type
);
104 case LLVMIntegerTypeKind
:
105 return LLVMGetIntTypeWidth(type
) / 8;
106 case LLVMFloatTypeKind
:
108 case LLVMDoubleTypeKind
:
109 case LLVMPointerTypeKind
:
111 case LLVMVectorTypeKind
:
112 return LLVMGetVectorSize(type
) *
113 ac_get_type_size(LLVMGetElementType(type
));
114 case LLVMArrayTypeKind
:
115 return LLVMGetArrayLength(type
) *
116 ac_get_type_size(LLVMGetElementType(type
));
123 static LLVMTypeRef
to_integer_type_scalar(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
125 if (t
== ctx
->f16
|| t
== ctx
->i16
)
127 else if (t
== ctx
->f32
|| t
== ctx
->i32
)
129 else if (t
== ctx
->f64
|| t
== ctx
->i64
)
132 unreachable("Unhandled integer size");
136 ac_to_integer_type(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
138 if (LLVMGetTypeKind(t
) == LLVMVectorTypeKind
) {
139 LLVMTypeRef elem_type
= LLVMGetElementType(t
);
140 return LLVMVectorType(to_integer_type_scalar(ctx
, elem_type
),
141 LLVMGetVectorSize(t
));
143 return to_integer_type_scalar(ctx
, t
);
147 ac_to_integer(struct ac_llvm_context
*ctx
, LLVMValueRef v
)
149 LLVMTypeRef type
= LLVMTypeOf(v
);
150 return LLVMBuildBitCast(ctx
->builder
, v
, ac_to_integer_type(ctx
, type
), "");
153 static LLVMTypeRef
to_float_type_scalar(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
155 if (t
== ctx
->i16
|| t
== ctx
->f16
)
157 else if (t
== ctx
->i32
|| t
== ctx
->f32
)
159 else if (t
== ctx
->i64
|| t
== ctx
->f64
)
162 unreachable("Unhandled float size");
166 ac_to_float_type(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
168 if (LLVMGetTypeKind(t
) == LLVMVectorTypeKind
) {
169 LLVMTypeRef elem_type
= LLVMGetElementType(t
);
170 return LLVMVectorType(to_float_type_scalar(ctx
, elem_type
),
171 LLVMGetVectorSize(t
));
173 return to_float_type_scalar(ctx
, t
);
177 ac_to_float(struct ac_llvm_context
*ctx
, LLVMValueRef v
)
179 LLVMTypeRef type
= LLVMTypeOf(v
);
180 return LLVMBuildBitCast(ctx
->builder
, v
, ac_to_float_type(ctx
, type
), "");
185 ac_build_intrinsic(struct ac_llvm_context
*ctx
, const char *name
,
186 LLVMTypeRef return_type
, LLVMValueRef
*params
,
187 unsigned param_count
, unsigned attrib_mask
)
189 LLVMValueRef function
, call
;
190 bool set_callsite_attrs
= HAVE_LLVM
>= 0x0400 &&
191 !(attrib_mask
& AC_FUNC_ATTR_LEGACY
);
193 function
= LLVMGetNamedFunction(ctx
->module
, name
);
195 LLVMTypeRef param_types
[32], function_type
;
198 assert(param_count
<= 32);
200 for (i
= 0; i
< param_count
; ++i
) {
202 param_types
[i
] = LLVMTypeOf(params
[i
]);
205 LLVMFunctionType(return_type
, param_types
, param_count
, 0);
206 function
= LLVMAddFunction(ctx
->module
, name
, function_type
);
208 LLVMSetFunctionCallConv(function
, LLVMCCallConv
);
209 LLVMSetLinkage(function
, LLVMExternalLinkage
);
211 if (!set_callsite_attrs
)
212 ac_add_func_attributes(ctx
->context
, function
, attrib_mask
);
215 call
= LLVMBuildCall(ctx
->builder
, function
, params
, param_count
, "");
216 if (set_callsite_attrs
)
217 ac_add_func_attributes(ctx
->context
, call
, attrib_mask
);
222 * Given the i32 or vNi32 \p type, generate the textual name (e.g. for use with
225 void ac_build_type_name_for_intr(LLVMTypeRef type
, char *buf
, unsigned bufsize
)
227 LLVMTypeRef elem_type
= type
;
229 assert(bufsize
>= 8);
231 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
) {
232 int ret
= snprintf(buf
, bufsize
, "v%u",
233 LLVMGetVectorSize(type
));
235 char *type_name
= LLVMPrintTypeToString(type
);
236 fprintf(stderr
, "Error building type name for: %s\n",
240 elem_type
= LLVMGetElementType(type
);
244 switch (LLVMGetTypeKind(elem_type
)) {
246 case LLVMIntegerTypeKind
:
247 snprintf(buf
, bufsize
, "i%d", LLVMGetIntTypeWidth(elem_type
));
249 case LLVMFloatTypeKind
:
250 snprintf(buf
, bufsize
, "f32");
252 case LLVMDoubleTypeKind
:
253 snprintf(buf
, bufsize
, "f64");
259 * Helper function that builds an LLVM IR PHI node and immediately adds
263 ac_build_phi(struct ac_llvm_context
*ctx
, LLVMTypeRef type
,
264 unsigned count_incoming
, LLVMValueRef
*values
,
265 LLVMBasicBlockRef
*blocks
)
267 LLVMValueRef phi
= LLVMBuildPhi(ctx
->builder
, type
, "");
268 LLVMAddIncoming(phi
, values
, blocks
, count_incoming
);
272 /* Prevent optimizations (at least of memory accesses) across the current
273 * point in the program by emitting empty inline assembly that is marked as
274 * having side effects.
276 * Optionally, a value can be passed through the inline assembly to prevent
277 * LLVM from hoisting calls to ReadNone functions.
280 ac_build_optimization_barrier(struct ac_llvm_context
*ctx
,
283 static int counter
= 0;
285 LLVMBuilderRef builder
= ctx
->builder
;
288 snprintf(code
, sizeof(code
), "; %d", p_atomic_inc_return(&counter
));
291 LLVMTypeRef ftype
= LLVMFunctionType(ctx
->voidt
, NULL
, 0, false);
292 LLVMValueRef inlineasm
= LLVMConstInlineAsm(ftype
, code
, "", true, false);
293 LLVMBuildCall(builder
, inlineasm
, NULL
, 0, "");
295 LLVMTypeRef ftype
= LLVMFunctionType(ctx
->i32
, &ctx
->i32
, 1, false);
296 LLVMValueRef inlineasm
= LLVMConstInlineAsm(ftype
, code
, "=v,0", true, false);
297 LLVMValueRef vgpr
= *pvgpr
;
298 LLVMTypeRef vgpr_type
= LLVMTypeOf(vgpr
);
299 unsigned vgpr_size
= ac_get_type_size(vgpr_type
);
302 assert(vgpr_size
% 4 == 0);
304 vgpr
= LLVMBuildBitCast(builder
, vgpr
, LLVMVectorType(ctx
->i32
, vgpr_size
/ 4), "");
305 vgpr0
= LLVMBuildExtractElement(builder
, vgpr
, ctx
->i32_0
, "");
306 vgpr0
= LLVMBuildCall(builder
, inlineasm
, &vgpr0
, 1, "");
307 vgpr
= LLVMBuildInsertElement(builder
, vgpr
, vgpr0
, ctx
->i32_0
, "");
308 vgpr
= LLVMBuildBitCast(builder
, vgpr
, vgpr_type
, "");
315 ac_build_ballot(struct ac_llvm_context
*ctx
,
318 LLVMValueRef args
[3] = {
321 LLVMConstInt(ctx
->i32
, LLVMIntNE
, 0)
324 /* We currently have no other way to prevent LLVM from lifting the icmp
325 * calls to a dominating basic block.
327 ac_build_optimization_barrier(ctx
, &args
[0]);
329 if (LLVMTypeOf(args
[0]) != ctx
->i32
)
330 args
[0] = LLVMBuildBitCast(ctx
->builder
, args
[0], ctx
->i32
, "");
332 return ac_build_intrinsic(ctx
,
333 "llvm.amdgcn.icmp.i32",
335 AC_FUNC_ATTR_NOUNWIND
|
336 AC_FUNC_ATTR_READNONE
|
337 AC_FUNC_ATTR_CONVERGENT
);
341 ac_build_vote_all(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
343 LLVMValueRef active_set
= ac_build_ballot(ctx
, ctx
->i32_1
);
344 LLVMValueRef vote_set
= ac_build_ballot(ctx
, value
);
345 return LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, vote_set
, active_set
, "");
349 ac_build_vote_any(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
351 LLVMValueRef vote_set
= ac_build_ballot(ctx
, value
);
352 return LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, vote_set
,
353 LLVMConstInt(ctx
->i64
, 0, 0), "");
357 ac_build_vote_eq(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
359 LLVMValueRef active_set
= ac_build_ballot(ctx
, ctx
->i32_1
);
360 LLVMValueRef vote_set
= ac_build_ballot(ctx
, value
);
362 LLVMValueRef all
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
363 vote_set
, active_set
, "");
364 LLVMValueRef none
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
366 LLVMConstInt(ctx
->i64
, 0, 0), "");
367 return LLVMBuildOr(ctx
->builder
, all
, none
, "");
371 ac_build_gather_values_extended(struct ac_llvm_context
*ctx
,
372 LLVMValueRef
*values
,
373 unsigned value_count
,
374 unsigned value_stride
,
378 LLVMBuilderRef builder
= ctx
->builder
;
379 LLVMValueRef vec
= NULL
;
382 if (value_count
== 1 && !always_vector
) {
384 return LLVMBuildLoad(builder
, values
[0], "");
386 } else if (!value_count
)
387 unreachable("value_count is 0");
389 for (i
= 0; i
< value_count
; i
++) {
390 LLVMValueRef value
= values
[i
* value_stride
];
392 value
= LLVMBuildLoad(builder
, value
, "");
395 vec
= LLVMGetUndef( LLVMVectorType(LLVMTypeOf(value
), value_count
));
396 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, i
, false);
397 vec
= LLVMBuildInsertElement(builder
, vec
, value
, index
, "");
403 ac_build_gather_values(struct ac_llvm_context
*ctx
,
404 LLVMValueRef
*values
,
405 unsigned value_count
)
407 return ac_build_gather_values_extended(ctx
, values
, value_count
, 1, false, false);
411 ac_build_fdiv(struct ac_llvm_context
*ctx
,
415 LLVMValueRef ret
= LLVMBuildFDiv(ctx
->builder
, num
, den
, "");
417 if (!LLVMIsConstant(ret
))
418 LLVMSetMetadata(ret
, ctx
->fpmath_md_kind
, ctx
->fpmath_md_2p5_ulp
);
422 /* Coordinates for cube map selection. sc, tc, and ma are as in Table 8.27
423 * of the OpenGL 4.5 (Compatibility Profile) specification, except ma is
424 * already multiplied by two. id is the cube face number.
426 struct cube_selection_coords
{
433 build_cube_intrinsic(struct ac_llvm_context
*ctx
,
435 struct cube_selection_coords
*out
)
437 LLVMTypeRef f32
= ctx
->f32
;
439 out
->stc
[1] = ac_build_intrinsic(ctx
, "llvm.amdgcn.cubetc",
440 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
441 out
->stc
[0] = ac_build_intrinsic(ctx
, "llvm.amdgcn.cubesc",
442 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
443 out
->ma
= ac_build_intrinsic(ctx
, "llvm.amdgcn.cubema",
444 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
445 out
->id
= ac_build_intrinsic(ctx
, "llvm.amdgcn.cubeid",
446 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
450 * Build a manual selection sequence for cube face sc/tc coordinates and
451 * major axis vector (multiplied by 2 for consistency) for the given
452 * vec3 \p coords, for the face implied by \p selcoords.
454 * For the major axis, we always adjust the sign to be in the direction of
455 * selcoords.ma; i.e., a positive out_ma means that coords is pointed towards
456 * the selcoords major axis.
458 static void build_cube_select(struct ac_llvm_context
*ctx
,
459 const struct cube_selection_coords
*selcoords
,
460 const LLVMValueRef
*coords
,
461 LLVMValueRef
*out_st
,
462 LLVMValueRef
*out_ma
)
464 LLVMBuilderRef builder
= ctx
->builder
;
465 LLVMTypeRef f32
= LLVMTypeOf(coords
[0]);
466 LLVMValueRef is_ma_positive
;
468 LLVMValueRef is_ma_z
, is_not_ma_z
;
469 LLVMValueRef is_ma_y
;
470 LLVMValueRef is_ma_x
;
474 is_ma_positive
= LLVMBuildFCmp(builder
, LLVMRealUGE
,
475 selcoords
->ma
, LLVMConstReal(f32
, 0.0), "");
476 sgn_ma
= LLVMBuildSelect(builder
, is_ma_positive
,
477 LLVMConstReal(f32
, 1.0), LLVMConstReal(f32
, -1.0), "");
479 is_ma_z
= LLVMBuildFCmp(builder
, LLVMRealUGE
, selcoords
->id
, LLVMConstReal(f32
, 4.0), "");
480 is_not_ma_z
= LLVMBuildNot(builder
, is_ma_z
, "");
481 is_ma_y
= LLVMBuildAnd(builder
, is_not_ma_z
,
482 LLVMBuildFCmp(builder
, LLVMRealUGE
, selcoords
->id
, LLVMConstReal(f32
, 2.0), ""), "");
483 is_ma_x
= LLVMBuildAnd(builder
, is_not_ma_z
, LLVMBuildNot(builder
, is_ma_y
, ""), "");
486 tmp
= LLVMBuildSelect(builder
, is_ma_x
, coords
[2], coords
[0], "");
487 sgn
= LLVMBuildSelect(builder
, is_ma_y
, LLVMConstReal(f32
, 1.0),
488 LLVMBuildSelect(builder
, is_ma_z
, sgn_ma
,
489 LLVMBuildFNeg(builder
, sgn_ma
, ""), ""), "");
490 out_st
[0] = LLVMBuildFMul(builder
, tmp
, sgn
, "");
493 tmp
= LLVMBuildSelect(builder
, is_ma_y
, coords
[2], coords
[1], "");
494 sgn
= LLVMBuildSelect(builder
, is_ma_y
, sgn_ma
,
495 LLVMConstReal(f32
, -1.0), "");
496 out_st
[1] = LLVMBuildFMul(builder
, tmp
, sgn
, "");
499 tmp
= LLVMBuildSelect(builder
, is_ma_z
, coords
[2],
500 LLVMBuildSelect(builder
, is_ma_y
, coords
[1], coords
[0], ""), "");
501 tmp
= ac_build_intrinsic(ctx
, "llvm.fabs.f32",
502 ctx
->f32
, &tmp
, 1, AC_FUNC_ATTR_READNONE
);
503 *out_ma
= LLVMBuildFMul(builder
, tmp
, LLVMConstReal(f32
, 2.0), "");
507 ac_prepare_cube_coords(struct ac_llvm_context
*ctx
,
508 bool is_deriv
, bool is_array
, bool is_lod
,
509 LLVMValueRef
*coords_arg
,
510 LLVMValueRef
*derivs_arg
)
513 LLVMBuilderRef builder
= ctx
->builder
;
514 struct cube_selection_coords selcoords
;
515 LLVMValueRef coords
[3];
518 if (is_array
&& !is_lod
) {
519 LLVMValueRef tmp
= coords_arg
[3];
520 tmp
= ac_build_intrinsic(ctx
, "llvm.rint.f32", ctx
->f32
, &tmp
, 1, 0);
522 /* Section 8.9 (Texture Functions) of the GLSL 4.50 spec says:
524 * "For Array forms, the array layer used will be
526 * max(0, min(d−1, floor(layer+0.5)))
528 * where d is the depth of the texture array and layer
529 * comes from the component indicated in the tables below.
530 * Workaroudn for an issue where the layer is taken from a
531 * helper invocation which happens to fall on a different
532 * layer due to extrapolation."
534 * VI and earlier attempt to implement this in hardware by
535 * clamping the value of coords[2] = (8 * layer) + face.
536 * Unfortunately, this means that the we end up with the wrong
537 * face when clamping occurs.
539 * Clamp the layer earlier to work around the issue.
541 if (ctx
->chip_class
<= VI
) {
543 ge0
= LLVMBuildFCmp(builder
, LLVMRealOGE
, tmp
, ctx
->f32_0
, "");
544 tmp
= LLVMBuildSelect(builder
, ge0
, tmp
, ctx
->f32_0
, "");
550 build_cube_intrinsic(ctx
, coords_arg
, &selcoords
);
552 invma
= ac_build_intrinsic(ctx
, "llvm.fabs.f32",
553 ctx
->f32
, &selcoords
.ma
, 1, AC_FUNC_ATTR_READNONE
);
554 invma
= ac_build_fdiv(ctx
, LLVMConstReal(ctx
->f32
, 1.0), invma
);
556 for (int i
= 0; i
< 2; ++i
)
557 coords
[i
] = LLVMBuildFMul(builder
, selcoords
.stc
[i
], invma
, "");
559 coords
[2] = selcoords
.id
;
561 if (is_deriv
&& derivs_arg
) {
562 LLVMValueRef derivs
[4];
565 /* Convert cube derivatives to 2D derivatives. */
566 for (axis
= 0; axis
< 2; axis
++) {
567 LLVMValueRef deriv_st
[2];
568 LLVMValueRef deriv_ma
;
570 /* Transform the derivative alongside the texture
571 * coordinate. Mathematically, the correct formula is
572 * as follows. Assume we're projecting onto the +Z face
573 * and denote by dx/dh the derivative of the (original)
574 * X texture coordinate with respect to horizontal
575 * window coordinates. The projection onto the +Z face
580 * Then df/dh = df/dx * dx/dh + df/dz * dz/dh
581 * = 1/z * dx/dh - x/z * 1/z * dz/dh.
583 * This motivatives the implementation below.
585 * Whether this actually gives the expected results for
586 * apps that might feed in derivatives obtained via
587 * finite differences is anyone's guess. The OpenGL spec
588 * seems awfully quiet about how textureGrad for cube
589 * maps should be handled.
591 build_cube_select(ctx
, &selcoords
, &derivs_arg
[axis
* 3],
592 deriv_st
, &deriv_ma
);
594 deriv_ma
= LLVMBuildFMul(builder
, deriv_ma
, invma
, "");
596 for (int i
= 0; i
< 2; ++i
)
597 derivs
[axis
* 2 + i
] =
598 LLVMBuildFSub(builder
,
599 LLVMBuildFMul(builder
, deriv_st
[i
], invma
, ""),
600 LLVMBuildFMul(builder
, deriv_ma
, coords
[i
], ""), "");
603 memcpy(derivs_arg
, derivs
, sizeof(derivs
));
606 /* Shift the texture coordinate. This must be applied after the
607 * derivative calculation.
609 for (int i
= 0; i
< 2; ++i
)
610 coords
[i
] = LLVMBuildFAdd(builder
, coords
[i
], LLVMConstReal(ctx
->f32
, 1.5), "");
613 /* for cube arrays coord.z = coord.w(array_index) * 8 + face */
614 /* coords_arg.w component - array_index for cube arrays */
615 LLVMValueRef tmp
= LLVMBuildFMul(ctx
->builder
, coords_arg
[3], LLVMConstReal(ctx
->f32
, 8.0), "");
616 coords
[2] = LLVMBuildFAdd(ctx
->builder
, tmp
, coords
[2], "");
619 memcpy(coords_arg
, coords
, sizeof(coords
));
624 ac_build_fs_interp(struct ac_llvm_context
*ctx
,
625 LLVMValueRef llvm_chan
,
626 LLVMValueRef attr_number
,
631 LLVMValueRef args
[5];
634 if (HAVE_LLVM
< 0x0400) {
636 ij
[0] = LLVMBuildBitCast(ctx
->builder
, i
, ctx
->i32
, "");
637 ij
[1] = LLVMBuildBitCast(ctx
->builder
, j
, ctx
->i32
, "");
640 args
[1] = attr_number
;
642 args
[3] = ac_build_gather_values(ctx
, ij
, 2);
643 return ac_build_intrinsic(ctx
, "llvm.SI.fs.interp",
645 AC_FUNC_ATTR_READNONE
);
650 args
[2] = attr_number
;
653 p1
= ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p1",
654 ctx
->f32
, args
, 4, AC_FUNC_ATTR_READNONE
);
659 args
[3] = attr_number
;
662 return ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p2",
663 ctx
->f32
, args
, 5, AC_FUNC_ATTR_READNONE
);
667 ac_build_fs_interp_mov(struct ac_llvm_context
*ctx
,
668 LLVMValueRef parameter
,
669 LLVMValueRef llvm_chan
,
670 LLVMValueRef attr_number
,
673 LLVMValueRef args
[4];
674 if (HAVE_LLVM
< 0x0400) {
676 args
[1] = attr_number
;
679 return ac_build_intrinsic(ctx
,
680 "llvm.SI.fs.constant",
682 AC_FUNC_ATTR_READNONE
);
687 args
[2] = attr_number
;
690 return ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.mov",
691 ctx
->f32
, args
, 4, AC_FUNC_ATTR_READNONE
);
695 ac_build_gep0(struct ac_llvm_context
*ctx
,
696 LLVMValueRef base_ptr
,
699 LLVMValueRef indices
[2] = {
700 LLVMConstInt(ctx
->i32
, 0, 0),
703 return LLVMBuildGEP(ctx
->builder
, base_ptr
,
708 ac_build_indexed_store(struct ac_llvm_context
*ctx
,
709 LLVMValueRef base_ptr
, LLVMValueRef index
,
712 LLVMBuildStore(ctx
->builder
, value
,
713 ac_build_gep0(ctx
, base_ptr
, index
));
717 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad.
718 * It's equivalent to doing a load from &base_ptr[index].
720 * \param base_ptr Where the array starts.
721 * \param index The element index into the array.
722 * \param uniform Whether the base_ptr and index can be assumed to be
723 * dynamically uniform (i.e. load to an SGPR)
724 * \param invariant Whether the load is invariant (no other opcodes affect it)
727 ac_build_load_custom(struct ac_llvm_context
*ctx
, LLVMValueRef base_ptr
,
728 LLVMValueRef index
, bool uniform
, bool invariant
)
730 LLVMValueRef pointer
, result
;
732 pointer
= ac_build_gep0(ctx
, base_ptr
, index
);
734 LLVMSetMetadata(pointer
, ctx
->uniform_md_kind
, ctx
->empty_md
);
735 result
= LLVMBuildLoad(ctx
->builder
, pointer
, "");
737 LLVMSetMetadata(result
, ctx
->invariant_load_md_kind
, ctx
->empty_md
);
741 LLVMValueRef
ac_build_load(struct ac_llvm_context
*ctx
, LLVMValueRef base_ptr
,
744 return ac_build_load_custom(ctx
, base_ptr
, index
, false, false);
747 LLVMValueRef
ac_build_load_invariant(struct ac_llvm_context
*ctx
,
748 LLVMValueRef base_ptr
, LLVMValueRef index
)
750 return ac_build_load_custom(ctx
, base_ptr
, index
, false, true);
753 LLVMValueRef
ac_build_load_to_sgpr(struct ac_llvm_context
*ctx
,
754 LLVMValueRef base_ptr
, LLVMValueRef index
)
756 return ac_build_load_custom(ctx
, base_ptr
, index
, true, true);
759 /* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
760 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
761 * or v4i32 (num_channels=3,4).
764 ac_build_buffer_store_dword(struct ac_llvm_context
*ctx
,
767 unsigned num_channels
,
768 LLVMValueRef voffset
,
769 LLVMValueRef soffset
,
770 unsigned inst_offset
,
773 bool writeonly_memory
,
774 bool swizzle_enable_hint
)
776 /* SWIZZLE_ENABLE requires that soffset isn't folded into voffset
777 * (voffset is swizzled, but soffset isn't swizzled).
778 * llvm.amdgcn.buffer.store doesn't have a separate soffset parameter.
780 if (!swizzle_enable_hint
) {
781 /* Split 3 channel stores, becase LLVM doesn't support 3-channel
783 if (num_channels
== 3) {
784 LLVMValueRef v
[3], v01
;
786 for (int i
= 0; i
< 3; i
++) {
787 v
[i
] = LLVMBuildExtractElement(ctx
->builder
, vdata
,
788 LLVMConstInt(ctx
->i32
, i
, 0), "");
790 v01
= ac_build_gather_values(ctx
, v
, 2);
792 ac_build_buffer_store_dword(ctx
, rsrc
, v01
, 2, voffset
,
793 soffset
, inst_offset
, glc
, slc
,
794 writeonly_memory
, swizzle_enable_hint
);
795 ac_build_buffer_store_dword(ctx
, rsrc
, v
[2], 1, voffset
,
796 soffset
, inst_offset
+ 8,
798 writeonly_memory
, swizzle_enable_hint
);
802 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
803 static const char *types
[] = {"f32", "v2f32", "v4f32"};
805 LLVMValueRef offset
= soffset
;
808 offset
= LLVMBuildAdd(ctx
->builder
, offset
,
809 LLVMConstInt(ctx
->i32
, inst_offset
, 0), "");
811 offset
= LLVMBuildAdd(ctx
->builder
, offset
, voffset
, "");
813 LLVMValueRef args
[] = {
814 ac_to_float(ctx
, vdata
),
815 LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, ""),
816 LLVMConstInt(ctx
->i32
, 0, 0),
818 LLVMConstInt(ctx
->i1
, glc
, 0),
819 LLVMConstInt(ctx
->i1
, slc
, 0),
822 snprintf(name
, sizeof(name
), "llvm.amdgcn.buffer.store.%s",
825 ac_build_intrinsic(ctx
, name
, ctx
->voidt
,
826 args
, ARRAY_SIZE(args
),
828 AC_FUNC_ATTR_INACCESSIBLE_MEM_ONLY
:
829 AC_FUNC_ATTR_WRITEONLY
);
833 static unsigned dfmt
[] = {
834 V_008F0C_BUF_DATA_FORMAT_32
,
835 V_008F0C_BUF_DATA_FORMAT_32_32
,
836 V_008F0C_BUF_DATA_FORMAT_32_32_32
,
837 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
839 assert(num_channels
>= 1 && num_channels
<= 4);
841 LLVMValueRef args
[] = {
844 LLVMConstInt(ctx
->i32
, num_channels
, 0),
845 voffset
? voffset
: LLVMGetUndef(ctx
->i32
),
847 LLVMConstInt(ctx
->i32
, inst_offset
, 0),
848 LLVMConstInt(ctx
->i32
, dfmt
[num_channels
- 1], 0),
849 LLVMConstInt(ctx
->i32
, V_008F0C_BUF_NUM_FORMAT_UINT
, 0),
850 LLVMConstInt(ctx
->i32
, voffset
!= NULL
, 0),
851 LLVMConstInt(ctx
->i32
, 0, 0), /* idxen */
852 LLVMConstInt(ctx
->i32
, glc
, 0),
853 LLVMConstInt(ctx
->i32
, slc
, 0),
854 LLVMConstInt(ctx
->i32
, 0, 0), /* tfe*/
857 /* The instruction offset field has 12 bits */
858 assert(voffset
|| inst_offset
< (1 << 12));
860 /* The intrinsic is overloaded, we need to add a type suffix for overloading to work. */
861 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
862 const char *types
[] = {"i32", "v2i32", "v4i32"};
864 snprintf(name
, sizeof(name
), "llvm.SI.tbuffer.store.%s", types
[func
]);
866 ac_build_intrinsic(ctx
, name
, ctx
->voidt
,
867 args
, ARRAY_SIZE(args
),
868 AC_FUNC_ATTR_LEGACY
);
872 ac_build_buffer_load(struct ac_llvm_context
*ctx
,
876 LLVMValueRef voffset
,
877 LLVMValueRef soffset
,
878 unsigned inst_offset
,
884 LLVMValueRef offset
= LLVMConstInt(ctx
->i32
, inst_offset
, 0);
886 offset
= LLVMBuildAdd(ctx
->builder
, offset
, voffset
, "");
888 offset
= LLVMBuildAdd(ctx
->builder
, offset
, soffset
, "");
890 /* TODO: VI and later generations can use SMEM with GLC=1.*/
891 if (allow_smem
&& !glc
&& !slc
) {
892 assert(vindex
== NULL
);
894 LLVMValueRef result
[4];
896 for (int i
= 0; i
< num_channels
; i
++) {
898 offset
= LLVMBuildAdd(ctx
->builder
, offset
,
899 LLVMConstInt(ctx
->i32
, 4, 0), "");
901 LLVMValueRef args
[2] = {rsrc
, offset
};
902 result
[i
] = ac_build_intrinsic(ctx
, "llvm.SI.load.const.v4i32",
904 AC_FUNC_ATTR_READNONE
|
905 AC_FUNC_ATTR_LEGACY
);
907 if (num_channels
== 1)
910 if (num_channels
== 3)
911 result
[num_channels
++] = LLVMGetUndef(ctx
->f32
);
912 return ac_build_gather_values(ctx
, result
, num_channels
);
915 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
917 LLVMValueRef args
[] = {
918 LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, ""),
919 vindex
? vindex
: LLVMConstInt(ctx
->i32
, 0, 0),
921 LLVMConstInt(ctx
->i1
, glc
, 0),
922 LLVMConstInt(ctx
->i1
, slc
, 0)
925 LLVMTypeRef types
[] = {ctx
->f32
, LLVMVectorType(ctx
->f32
, 2),
927 const char *type_names
[] = {"f32", "v2f32", "v4f32"};
930 snprintf(name
, sizeof(name
), "llvm.amdgcn.buffer.load.%s",
933 return ac_build_intrinsic(ctx
, name
, types
[func
], args
,
935 /* READNONE means writes can't affect it, while
936 * READONLY means that writes can affect it. */
937 can_speculate
&& HAVE_LLVM
>= 0x0400 ?
938 AC_FUNC_ATTR_READNONE
:
939 AC_FUNC_ATTR_READONLY
);
942 LLVMValueRef
ac_build_buffer_load_format(struct ac_llvm_context
*ctx
,
945 LLVMValueRef voffset
,
948 LLVMValueRef args
[] = {
949 LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, ""),
952 ctx
->i1false
, /* glc */
953 ctx
->i1false
, /* slc */
956 return ac_build_intrinsic(ctx
,
957 "llvm.amdgcn.buffer.load.format.v4f32",
958 ctx
->v4f32
, args
, ARRAY_SIZE(args
),
959 /* READNONE means writes can't affect it, while
960 * READONLY means that writes can affect it. */
961 can_speculate
&& HAVE_LLVM
>= 0x0400 ?
962 AC_FUNC_ATTR_READNONE
:
963 AC_FUNC_ATTR_READONLY
);
967 * Set range metadata on an instruction. This can only be used on load and
968 * call instructions. If you know an instruction can only produce the values
969 * 0, 1, 2, you would do set_range_metadata(value, 0, 3);
970 * \p lo is the minimum value inclusive.
971 * \p hi is the maximum value exclusive.
973 static void set_range_metadata(struct ac_llvm_context
*ctx
,
974 LLVMValueRef value
, unsigned lo
, unsigned hi
)
976 LLVMValueRef range_md
, md_args
[2];
977 LLVMTypeRef type
= LLVMTypeOf(value
);
978 LLVMContextRef context
= LLVMGetTypeContext(type
);
980 md_args
[0] = LLVMConstInt(type
, lo
, false);
981 md_args
[1] = LLVMConstInt(type
, hi
, false);
982 range_md
= LLVMMDNodeInContext(context
, md_args
, 2);
983 LLVMSetMetadata(value
, ctx
->range_md_kind
, range_md
);
987 ac_get_thread_id(struct ac_llvm_context
*ctx
)
991 LLVMValueRef tid_args
[2];
992 tid_args
[0] = LLVMConstInt(ctx
->i32
, 0xffffffff, false);
993 tid_args
[1] = LLVMConstInt(ctx
->i32
, 0, false);
994 tid_args
[1] = ac_build_intrinsic(ctx
,
995 "llvm.amdgcn.mbcnt.lo", ctx
->i32
,
996 tid_args
, 2, AC_FUNC_ATTR_READNONE
);
998 tid
= ac_build_intrinsic(ctx
, "llvm.amdgcn.mbcnt.hi",
1000 2, AC_FUNC_ATTR_READNONE
);
1001 set_range_metadata(ctx
, tid
, 0, 64);
1006 * SI implements derivatives using the local data store (LDS)
1007 * All writes to the LDS happen in all executing threads at
1008 * the same time. TID is the Thread ID for the current
1009 * thread and is a value between 0 and 63, representing
1010 * the thread's position in the wavefront.
1012 * For the pixel shader threads are grouped into quads of four pixels.
1013 * The TIDs of the pixels of a quad are:
1021 * So, masking the TID with 0xfffffffc yields the TID of the top left pixel
1022 * of the quad, masking with 0xfffffffd yields the TID of the top pixel of
1023 * the current pixel's column, and masking with 0xfffffffe yields the TID
1024 * of the left pixel of the current pixel's row.
1026 * Adding 1 yields the TID of the pixel to the right of the left pixel, and
1027 * adding 2 yields the TID of the pixel below the top pixel.
1030 ac_build_ddxy(struct ac_llvm_context
*ctx
,
1035 LLVMValueRef tl
, trbl
, args
[2];
1036 LLVMValueRef result
;
1038 if (ctx
->chip_class
>= VI
) {
1039 LLVMValueRef thread_id
, tl_tid
, trbl_tid
;
1040 thread_id
= ac_get_thread_id(ctx
);
1042 tl_tid
= LLVMBuildAnd(ctx
->builder
, thread_id
,
1043 LLVMConstInt(ctx
->i32
, mask
, false), "");
1045 trbl_tid
= LLVMBuildAdd(ctx
->builder
, tl_tid
,
1046 LLVMConstInt(ctx
->i32
, idx
, false), "");
1048 args
[0] = LLVMBuildMul(ctx
->builder
, tl_tid
,
1049 LLVMConstInt(ctx
->i32
, 4, false), "");
1051 tl
= ac_build_intrinsic(ctx
,
1052 "llvm.amdgcn.ds.bpermute", ctx
->i32
,
1054 AC_FUNC_ATTR_READNONE
|
1055 AC_FUNC_ATTR_CONVERGENT
);
1057 args
[0] = LLVMBuildMul(ctx
->builder
, trbl_tid
,
1058 LLVMConstInt(ctx
->i32
, 4, false), "");
1059 trbl
= ac_build_intrinsic(ctx
,
1060 "llvm.amdgcn.ds.bpermute", ctx
->i32
,
1062 AC_FUNC_ATTR_READNONE
|
1063 AC_FUNC_ATTR_CONVERGENT
);
1065 uint32_t masks
[2] = {};
1068 case AC_TID_MASK_TOP_LEFT
:
1076 case AC_TID_MASK_TOP
:
1080 case AC_TID_MASK_LEFT
:
1089 args
[1] = LLVMConstInt(ctx
->i32
, masks
[0], false);
1091 tl
= ac_build_intrinsic(ctx
,
1092 "llvm.amdgcn.ds.swizzle", ctx
->i32
,
1094 AC_FUNC_ATTR_READNONE
|
1095 AC_FUNC_ATTR_CONVERGENT
);
1097 args
[1] = LLVMConstInt(ctx
->i32
, masks
[1], false);
1098 trbl
= ac_build_intrinsic(ctx
,
1099 "llvm.amdgcn.ds.swizzle", ctx
->i32
,
1101 AC_FUNC_ATTR_READNONE
|
1102 AC_FUNC_ATTR_CONVERGENT
);
1105 tl
= LLVMBuildBitCast(ctx
->builder
, tl
, ctx
->f32
, "");
1106 trbl
= LLVMBuildBitCast(ctx
->builder
, trbl
, ctx
->f32
, "");
1107 result
= LLVMBuildFSub(ctx
->builder
, trbl
, tl
, "");
1112 ac_build_sendmsg(struct ac_llvm_context
*ctx
,
1114 LLVMValueRef wave_id
)
1116 LLVMValueRef args
[2];
1117 const char *intr_name
= (HAVE_LLVM
< 0x0400) ? "llvm.SI.sendmsg" : "llvm.amdgcn.s.sendmsg";
1118 args
[0] = LLVMConstInt(ctx
->i32
, msg
, false);
1120 ac_build_intrinsic(ctx
, intr_name
, ctx
->voidt
, args
, 2, 0);
1124 ac_build_imsb(struct ac_llvm_context
*ctx
,
1126 LLVMTypeRef dst_type
)
1128 const char *intr_name
= (HAVE_LLVM
< 0x0400) ? "llvm.AMDGPU.flbit.i32" :
1129 "llvm.amdgcn.sffbh.i32";
1130 LLVMValueRef msb
= ac_build_intrinsic(ctx
, intr_name
,
1132 AC_FUNC_ATTR_READNONE
);
1134 /* The HW returns the last bit index from MSB, but NIR/TGSI wants
1135 * the index from LSB. Invert it by doing "31 - msb". */
1136 msb
= LLVMBuildSub(ctx
->builder
, LLVMConstInt(ctx
->i32
, 31, false),
1139 LLVMValueRef all_ones
= LLVMConstInt(ctx
->i32
, -1, true);
1140 LLVMValueRef cond
= LLVMBuildOr(ctx
->builder
,
1141 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
1142 arg
, LLVMConstInt(ctx
->i32
, 0, 0), ""),
1143 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
1144 arg
, all_ones
, ""), "");
1146 return LLVMBuildSelect(ctx
->builder
, cond
, all_ones
, msb
, "");
1150 ac_build_umsb(struct ac_llvm_context
*ctx
,
1152 LLVMTypeRef dst_type
)
1154 LLVMValueRef args
[2] = {
1158 LLVMValueRef msb
= ac_build_intrinsic(ctx
, "llvm.ctlz.i32",
1159 dst_type
, args
, ARRAY_SIZE(args
),
1160 AC_FUNC_ATTR_READNONE
);
1162 /* The HW returns the last bit index from MSB, but TGSI/NIR wants
1163 * the index from LSB. Invert it by doing "31 - msb". */
1164 msb
= LLVMBuildSub(ctx
->builder
, LLVMConstInt(ctx
->i32
, 31, false),
1167 /* check for zero */
1168 return LLVMBuildSelect(ctx
->builder
,
1169 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, arg
,
1170 LLVMConstInt(ctx
->i32
, 0, 0), ""),
1171 LLVMConstInt(ctx
->i32
, -1, true), msb
, "");
1174 LLVMValueRef
ac_build_umin(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
1177 LLVMValueRef cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntULE
, a
, b
, "");
1178 return LLVMBuildSelect(ctx
->builder
, cmp
, a
, b
, "");
1181 LLVMValueRef
ac_build_clamp(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
1183 if (HAVE_LLVM
>= 0x0500) {
1184 LLVMValueRef max
[2] = {
1186 LLVMConstReal(ctx
->f32
, 0),
1188 LLVMValueRef min
[2] = {
1189 LLVMConstReal(ctx
->f32
, 1),
1192 min
[1] = ac_build_intrinsic(ctx
, "llvm.maxnum.f32",
1194 AC_FUNC_ATTR_READNONE
);
1195 return ac_build_intrinsic(ctx
, "llvm.minnum.f32",
1197 AC_FUNC_ATTR_READNONE
);
1200 LLVMValueRef args
[3] = {
1202 LLVMConstReal(ctx
->f32
, 0),
1203 LLVMConstReal(ctx
->f32
, 1),
1206 return ac_build_intrinsic(ctx
, "llvm.AMDGPU.clamp.", ctx
->f32
, args
, 3,
1207 AC_FUNC_ATTR_READNONE
|
1208 AC_FUNC_ATTR_LEGACY
);
1211 void ac_build_export(struct ac_llvm_context
*ctx
, struct ac_export_args
*a
)
1213 LLVMValueRef args
[9];
1215 if (HAVE_LLVM
>= 0x0500) {
1216 args
[0] = LLVMConstInt(ctx
->i32
, a
->target
, 0);
1217 args
[1] = LLVMConstInt(ctx
->i32
, a
->enabled_channels
, 0);
1220 LLVMTypeRef i16
= LLVMInt16TypeInContext(ctx
->context
);
1221 LLVMTypeRef v2i16
= LLVMVectorType(i16
, 2);
1223 args
[2] = LLVMBuildBitCast(ctx
->builder
, a
->out
[0],
1225 args
[3] = LLVMBuildBitCast(ctx
->builder
, a
->out
[1],
1227 args
[4] = LLVMConstInt(ctx
->i1
, a
->done
, 0);
1228 args
[5] = LLVMConstInt(ctx
->i1
, a
->valid_mask
, 0);
1230 ac_build_intrinsic(ctx
, "llvm.amdgcn.exp.compr.v2i16",
1231 ctx
->voidt
, args
, 6, 0);
1233 args
[2] = a
->out
[0];
1234 args
[3] = a
->out
[1];
1235 args
[4] = a
->out
[2];
1236 args
[5] = a
->out
[3];
1237 args
[6] = LLVMConstInt(ctx
->i1
, a
->done
, 0);
1238 args
[7] = LLVMConstInt(ctx
->i1
, a
->valid_mask
, 0);
1240 ac_build_intrinsic(ctx
, "llvm.amdgcn.exp.f32",
1241 ctx
->voidt
, args
, 8, 0);
1246 args
[0] = LLVMConstInt(ctx
->i32
, a
->enabled_channels
, 0);
1247 args
[1] = LLVMConstInt(ctx
->i32
, a
->valid_mask
, 0);
1248 args
[2] = LLVMConstInt(ctx
->i32
, a
->done
, 0);
1249 args
[3] = LLVMConstInt(ctx
->i32
, a
->target
, 0);
1250 args
[4] = LLVMConstInt(ctx
->i32
, a
->compr
, 0);
1251 memcpy(args
+ 5, a
->out
, sizeof(a
->out
[0]) * 4);
1253 ac_build_intrinsic(ctx
, "llvm.SI.export", ctx
->voidt
, args
, 9,
1254 AC_FUNC_ATTR_LEGACY
);
1257 LLVMValueRef
ac_build_image_opcode(struct ac_llvm_context
*ctx
,
1258 struct ac_image_args
*a
)
1260 LLVMTypeRef dst_type
;
1261 LLVMValueRef args
[11];
1262 unsigned num_args
= 0;
1263 const char *name
= NULL
;
1264 char intr_name
[128], type
[64];
1266 if (HAVE_LLVM
>= 0x0400) {
1267 bool sample
= a
->opcode
== ac_image_sample
||
1268 a
->opcode
== ac_image_gather4
||
1269 a
->opcode
== ac_image_get_lod
;
1272 args
[num_args
++] = ac_to_float(ctx
, a
->addr
);
1274 args
[num_args
++] = a
->addr
;
1276 args
[num_args
++] = a
->resource
;
1278 args
[num_args
++] = a
->sampler
;
1279 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->dmask
, 0);
1281 args
[num_args
++] = LLVMConstInt(ctx
->i1
, a
->unorm
, 0);
1282 args
[num_args
++] = ctx
->i1false
; /* glc */
1283 args
[num_args
++] = ctx
->i1false
; /* slc */
1284 args
[num_args
++] = ctx
->i1false
; /* lwe */
1285 args
[num_args
++] = LLVMConstInt(ctx
->i1
, a
->da
, 0);
1287 switch (a
->opcode
) {
1288 case ac_image_sample
:
1289 name
= "llvm.amdgcn.image.sample";
1291 case ac_image_gather4
:
1292 name
= "llvm.amdgcn.image.gather4";
1295 name
= "llvm.amdgcn.image.load";
1297 case ac_image_load_mip
:
1298 name
= "llvm.amdgcn.image.load.mip";
1300 case ac_image_get_lod
:
1301 name
= "llvm.amdgcn.image.getlod";
1303 case ac_image_get_resinfo
:
1304 name
= "llvm.amdgcn.image.getresinfo";
1307 unreachable("invalid image opcode");
1310 ac_build_type_name_for_intr(LLVMTypeOf(args
[0]), type
,
1313 snprintf(intr_name
, sizeof(intr_name
), "%s%s%s%s.v4f32.%s.v8i32",
1315 a
->compare
? ".c" : "",
1319 a
->level_zero
? ".lz" : "",
1320 a
->offset
? ".o" : "",
1323 LLVMValueRef result
=
1324 ac_build_intrinsic(ctx
, intr_name
,
1325 ctx
->v4f32
, args
, num_args
,
1326 AC_FUNC_ATTR_READNONE
);
1328 result
= LLVMBuildBitCast(ctx
->builder
, result
,
1334 args
[num_args
++] = a
->addr
;
1335 args
[num_args
++] = a
->resource
;
1337 if (a
->opcode
== ac_image_load
||
1338 a
->opcode
== ac_image_load_mip
||
1339 a
->opcode
== ac_image_get_resinfo
) {
1340 dst_type
= ctx
->v4i32
;
1342 dst_type
= ctx
->v4f32
;
1343 args
[num_args
++] = a
->sampler
;
1346 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->dmask
, 0);
1347 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->unorm
, 0);
1348 args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* r128 */
1349 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->da
, 0);
1350 args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* glc */
1351 args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* slc */
1352 args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* tfe */
1353 args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* lwe */
1355 switch (a
->opcode
) {
1356 case ac_image_sample
:
1357 name
= "llvm.SI.image.sample";
1359 case ac_image_gather4
:
1360 name
= "llvm.SI.gather4";
1363 name
= "llvm.SI.image.load";
1365 case ac_image_load_mip
:
1366 name
= "llvm.SI.image.load.mip";
1368 case ac_image_get_lod
:
1369 name
= "llvm.SI.getlod";
1371 case ac_image_get_resinfo
:
1372 name
= "llvm.SI.getresinfo";
1376 ac_build_type_name_for_intr(LLVMTypeOf(a
->addr
), type
, sizeof(type
));
1377 snprintf(intr_name
, sizeof(intr_name
), "%s%s%s%s.%s",
1379 a
->compare
? ".c" : "",
1383 a
->level_zero
? ".lz" : "",
1384 a
->offset
? ".o" : "",
1387 return ac_build_intrinsic(ctx
, intr_name
,
1388 dst_type
, args
, num_args
,
1389 AC_FUNC_ATTR_READNONE
|
1390 AC_FUNC_ATTR_LEGACY
);
1393 LLVMValueRef
ac_build_cvt_pkrtz_f16(struct ac_llvm_context
*ctx
,
1394 LLVMValueRef args
[2])
1396 if (HAVE_LLVM
>= 0x0500) {
1398 LLVMVectorType(LLVMHalfTypeInContext(ctx
->context
), 2);
1400 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pkrtz",
1402 AC_FUNC_ATTR_READNONE
);
1403 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
1406 return ac_build_intrinsic(ctx
, "llvm.SI.packf16", ctx
->i32
, args
, 2,
1407 AC_FUNC_ATTR_READNONE
|
1408 AC_FUNC_ATTR_LEGACY
);
1411 LLVMValueRef
ac_build_wqm_vote(struct ac_llvm_context
*ctx
, LLVMValueRef i1
)
1413 assert(HAVE_LLVM
>= 0x0600);
1414 return ac_build_intrinsic(ctx
, "llvm.amdgcn.wqm.vote", ctx
->i1
,
1415 &i1
, 1, AC_FUNC_ATTR_READNONE
);
1418 void ac_build_kill_if_false(struct ac_llvm_context
*ctx
, LLVMValueRef i1
)
1420 if (HAVE_LLVM
>= 0x0600) {
1421 ac_build_intrinsic(ctx
, "llvm.amdgcn.kill", ctx
->voidt
,
1426 LLVMValueRef value
= LLVMBuildSelect(ctx
->builder
, i1
,
1427 LLVMConstReal(ctx
->f32
, 1),
1428 LLVMConstReal(ctx
->f32
, -1), "");
1429 ac_build_intrinsic(ctx
, "llvm.AMDGPU.kill", ctx
->voidt
,
1430 &value
, 1, AC_FUNC_ATTR_LEGACY
);
1433 LLVMValueRef
ac_build_bfe(struct ac_llvm_context
*ctx
, LLVMValueRef input
,
1434 LLVMValueRef offset
, LLVMValueRef width
,
1437 LLVMValueRef args
[] = {
1443 if (HAVE_LLVM
>= 0x0500) {
1444 return ac_build_intrinsic(ctx
,
1445 is_signed
? "llvm.amdgcn.sbfe.i32" :
1446 "llvm.amdgcn.ubfe.i32",
1448 AC_FUNC_ATTR_READNONE
);
1451 return ac_build_intrinsic(ctx
,
1452 is_signed
? "llvm.AMDGPU.bfe.i32" :
1453 "llvm.AMDGPU.bfe.u32",
1455 AC_FUNC_ATTR_READNONE
|
1456 AC_FUNC_ATTR_LEGACY
);
1459 void ac_get_image_intr_name(const char *base_name
,
1460 LLVMTypeRef data_type
,
1461 LLVMTypeRef coords_type
,
1462 LLVMTypeRef rsrc_type
,
1463 char *out_name
, unsigned out_len
)
1465 char coords_type_name
[8];
1467 ac_build_type_name_for_intr(coords_type
, coords_type_name
,
1468 sizeof(coords_type_name
));
1470 if (HAVE_LLVM
<= 0x0309) {
1471 snprintf(out_name
, out_len
, "%s.%s", base_name
, coords_type_name
);
1473 char data_type_name
[8];
1474 char rsrc_type_name
[8];
1476 ac_build_type_name_for_intr(data_type
, data_type_name
,
1477 sizeof(data_type_name
));
1478 ac_build_type_name_for_intr(rsrc_type
, rsrc_type_name
,
1479 sizeof(rsrc_type_name
));
1480 snprintf(out_name
, out_len
, "%s.%s.%s.%s", base_name
,
1481 data_type_name
, coords_type_name
, rsrc_type_name
);
1485 #define AC_EXP_TARGET (HAVE_LLVM >= 0x0500 ? 0 : 3)
1486 #define AC_EXP_OUT0 (HAVE_LLVM >= 0x0500 ? 2 : 5)
1494 struct ac_vs_exp_chan
1498 enum ac_ir_type type
;
1501 struct ac_vs_exp_inst
{
1504 struct ac_vs_exp_chan chan
[4];
1507 struct ac_vs_exports
{
1509 struct ac_vs_exp_inst exp
[VARYING_SLOT_MAX
];
1512 /* Return true if the PARAM export has been eliminated. */
1513 static bool ac_eliminate_const_output(uint8_t *vs_output_param_offset
,
1514 uint32_t num_outputs
,
1515 struct ac_vs_exp_inst
*exp
)
1517 unsigned i
, default_val
; /* SPI_PS_INPUT_CNTL_i.DEFAULT_VAL */
1518 bool is_zero
[4] = {}, is_one
[4] = {};
1520 for (i
= 0; i
< 4; i
++) {
1521 /* It's a constant expression. Undef outputs are eliminated too. */
1522 if (exp
->chan
[i
].type
== AC_IR_UNDEF
) {
1525 } else if (exp
->chan
[i
].type
== AC_IR_CONST
) {
1526 if (exp
->chan
[i
].const_float
== 0)
1528 else if (exp
->chan
[i
].const_float
== 1)
1531 return false; /* other constant */
1536 /* Only certain combinations of 0 and 1 can be eliminated. */
1537 if (is_zero
[0] && is_zero
[1] && is_zero
[2])
1538 default_val
= is_zero
[3] ? 0 : 1;
1539 else if (is_one
[0] && is_one
[1] && is_one
[2])
1540 default_val
= is_zero
[3] ? 2 : 3;
1544 /* The PARAM export can be represented as DEFAULT_VAL. Kill it. */
1545 LLVMInstructionEraseFromParent(exp
->inst
);
1547 /* Change OFFSET to DEFAULT_VAL. */
1548 for (i
= 0; i
< num_outputs
; i
++) {
1549 if (vs_output_param_offset
[i
] == exp
->offset
) {
1550 vs_output_param_offset
[i
] =
1551 AC_EXP_PARAM_DEFAULT_VAL_0000
+ default_val
;
1558 static bool ac_eliminate_duplicated_output(uint8_t *vs_output_param_offset
,
1559 uint32_t num_outputs
,
1560 struct ac_vs_exports
*processed
,
1561 struct ac_vs_exp_inst
*exp
)
1563 unsigned p
, copy_back_channels
= 0;
1565 /* See if the output is already in the list of processed outputs.
1566 * The LLVMValueRef comparison relies on SSA.
1568 for (p
= 0; p
< processed
->num
; p
++) {
1569 bool different
= false;
1571 for (unsigned j
= 0; j
< 4; j
++) {
1572 struct ac_vs_exp_chan
*c1
= &processed
->exp
[p
].chan
[j
];
1573 struct ac_vs_exp_chan
*c2
= &exp
->chan
[j
];
1575 /* Treat undef as a match. */
1576 if (c2
->type
== AC_IR_UNDEF
)
1579 /* If c1 is undef but c2 isn't, we can copy c2 to c1
1580 * and consider the instruction duplicated.
1582 if (c1
->type
== AC_IR_UNDEF
) {
1583 copy_back_channels
|= 1 << j
;
1587 /* Test whether the channels are not equal. */
1588 if (c1
->type
!= c2
->type
||
1589 (c1
->type
== AC_IR_CONST
&&
1590 c1
->const_float
!= c2
->const_float
) ||
1591 (c1
->type
== AC_IR_VALUE
&&
1592 c1
->value
!= c2
->value
)) {
1600 copy_back_channels
= 0;
1602 if (p
== processed
->num
)
1605 /* If a match was found, but the matching export has undef where the new
1606 * one has a normal value, copy the normal value to the undef channel.
1608 struct ac_vs_exp_inst
*match
= &processed
->exp
[p
];
1610 while (copy_back_channels
) {
1611 unsigned chan
= u_bit_scan(©_back_channels
);
1613 assert(match
->chan
[chan
].type
== AC_IR_UNDEF
);
1614 LLVMSetOperand(match
->inst
, AC_EXP_OUT0
+ chan
,
1615 exp
->chan
[chan
].value
);
1616 match
->chan
[chan
] = exp
->chan
[chan
];
1619 /* The PARAM export is duplicated. Kill it. */
1620 LLVMInstructionEraseFromParent(exp
->inst
);
1622 /* Change OFFSET to the matching export. */
1623 for (unsigned i
= 0; i
< num_outputs
; i
++) {
1624 if (vs_output_param_offset
[i
] == exp
->offset
) {
1625 vs_output_param_offset
[i
] = match
->offset
;
1632 void ac_optimize_vs_outputs(struct ac_llvm_context
*ctx
,
1633 LLVMValueRef main_fn
,
1634 uint8_t *vs_output_param_offset
,
1635 uint32_t num_outputs
,
1636 uint8_t *num_param_exports
)
1638 LLVMBasicBlockRef bb
;
1639 bool removed_any
= false;
1640 struct ac_vs_exports exports
;
1644 /* Process all LLVM instructions. */
1645 bb
= LLVMGetFirstBasicBlock(main_fn
);
1647 LLVMValueRef inst
= LLVMGetFirstInstruction(bb
);
1650 LLVMValueRef cur
= inst
;
1651 inst
= LLVMGetNextInstruction(inst
);
1652 struct ac_vs_exp_inst exp
;
1654 if (LLVMGetInstructionOpcode(cur
) != LLVMCall
)
1657 LLVMValueRef callee
= ac_llvm_get_called_value(cur
);
1659 if (!ac_llvm_is_function(callee
))
1662 const char *name
= LLVMGetValueName(callee
);
1663 unsigned num_args
= LLVMCountParams(callee
);
1665 /* Check if this is an export instruction. */
1666 if ((num_args
!= 9 && num_args
!= 8) ||
1667 (strcmp(name
, "llvm.SI.export") &&
1668 strcmp(name
, "llvm.amdgcn.exp.f32")))
1671 LLVMValueRef arg
= LLVMGetOperand(cur
, AC_EXP_TARGET
);
1672 unsigned target
= LLVMConstIntGetZExtValue(arg
);
1674 if (target
< V_008DFC_SQ_EXP_PARAM
)
1677 target
-= V_008DFC_SQ_EXP_PARAM
;
1679 /* Parse the instruction. */
1680 memset(&exp
, 0, sizeof(exp
));
1681 exp
.offset
= target
;
1684 for (unsigned i
= 0; i
< 4; i
++) {
1685 LLVMValueRef v
= LLVMGetOperand(cur
, AC_EXP_OUT0
+ i
);
1687 exp
.chan
[i
].value
= v
;
1689 if (LLVMIsUndef(v
)) {
1690 exp
.chan
[i
].type
= AC_IR_UNDEF
;
1691 } else if (LLVMIsAConstantFP(v
)) {
1692 LLVMBool loses_info
;
1693 exp
.chan
[i
].type
= AC_IR_CONST
;
1694 exp
.chan
[i
].const_float
=
1695 LLVMConstRealGetDouble(v
, &loses_info
);
1697 exp
.chan
[i
].type
= AC_IR_VALUE
;
1701 /* Eliminate constant and duplicated PARAM exports. */
1702 if (ac_eliminate_const_output(vs_output_param_offset
,
1703 num_outputs
, &exp
) ||
1704 ac_eliminate_duplicated_output(vs_output_param_offset
,
1705 num_outputs
, &exports
,
1709 exports
.exp
[exports
.num
++] = exp
;
1712 bb
= LLVMGetNextBasicBlock(bb
);
1715 /* Remove holes in export memory due to removed PARAM exports.
1716 * This is done by renumbering all PARAM exports.
1719 uint8_t old_offset
[VARYING_SLOT_MAX
];
1722 /* Make a copy of the offsets. We need the old version while
1723 * we are modifying some of them. */
1724 memcpy(old_offset
, vs_output_param_offset
,
1725 sizeof(old_offset
));
1727 for (i
= 0; i
< exports
.num
; i
++) {
1728 unsigned offset
= exports
.exp
[i
].offset
;
1730 /* Update vs_output_param_offset. Multiple outputs can
1731 * have the same offset.
1733 for (out
= 0; out
< num_outputs
; out
++) {
1734 if (old_offset
[out
] == offset
)
1735 vs_output_param_offset
[out
] = i
;
1738 /* Change the PARAM offset in the instruction. */
1739 LLVMSetOperand(exports
.exp
[i
].inst
, AC_EXP_TARGET
,
1740 LLVMConstInt(ctx
->i32
,
1741 V_008DFC_SQ_EXP_PARAM
+ i
, 0));
1743 *num_param_exports
= exports
.num
;
1747 void ac_init_exec_full_mask(struct ac_llvm_context
*ctx
)
1749 LLVMValueRef full_mask
= LLVMConstInt(ctx
->i64
, ~0ull, 0);
1750 ac_build_intrinsic(ctx
,
1751 "llvm.amdgcn.init.exec", ctx
->voidt
,
1752 &full_mask
, 1, AC_FUNC_ATTR_CONVERGENT
);
1755 void ac_declare_lds_as_pointer(struct ac_llvm_context
*ctx
)
1757 unsigned lds_size
= ctx
->chip_class
>= CIK
? 65536 : 32768;
1758 ctx
->lds
= LLVMBuildIntToPtr(ctx
->builder
, ctx
->i32_0
,
1759 LLVMPointerType(LLVMArrayType(ctx
->i32
, lds_size
/ 4), AC_LOCAL_ADDR_SPACE
),
1763 LLVMValueRef
ac_lds_load(struct ac_llvm_context
*ctx
,
1764 LLVMValueRef dw_addr
)
1766 return ac_build_load(ctx
, ctx
->lds
, dw_addr
);
1769 void ac_lds_store(struct ac_llvm_context
*ctx
,
1770 LLVMValueRef dw_addr
,
1773 value
= ac_to_integer(ctx
, value
);
1774 ac_build_indexed_store(ctx
, ctx
->lds
,
1778 LLVMValueRef
ac_find_lsb(struct ac_llvm_context
*ctx
,
1779 LLVMTypeRef dst_type
,
1782 LLVMValueRef params
[2] = {
1785 /* The value of 1 means that ffs(x=0) = undef, so LLVM won't
1786 * add special code to check for x=0. The reason is that
1787 * the LLVM behavior for x=0 is different from what we
1788 * need here. However, LLVM also assumes that ffs(x) is
1789 * in [0, 31], but GLSL expects that ffs(0) = -1, so
1790 * a conditional assignment to handle 0 is still required.
1792 * The hardware already implements the correct behavior.
1794 LLVMConstInt(ctx
->i1
, 1, false),
1797 LLVMValueRef lsb
= ac_build_intrinsic(ctx
, "llvm.cttz.i32", ctx
->i32
,
1799 AC_FUNC_ATTR_READNONE
);
1801 /* TODO: We need an intrinsic to skip this conditional. */
1802 /* Check for zero: */
1803 return LLVMBuildSelect(ctx
->builder
, LLVMBuildICmp(ctx
->builder
,
1806 LLVMConstInt(ctx
->i32
, -1, 0), lsb
, "");