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
->v4f32
= LLVMVectorType(ctx
->f32
, 4);
73 ctx
->v8i32
= LLVMVectorType(ctx
->i32
, 8);
75 ctx
->i32_0
= LLVMConstInt(ctx
->i32
, 0, false);
76 ctx
->i32_1
= LLVMConstInt(ctx
->i32
, 1, false);
77 ctx
->f32_0
= LLVMConstReal(ctx
->f32
, 0.0);
78 ctx
->f32_1
= LLVMConstReal(ctx
->f32
, 1.0);
80 ctx
->i1false
= LLVMConstInt(ctx
->i1
, 0, false);
81 ctx
->i1true
= LLVMConstInt(ctx
->i1
, 1, false);
83 ctx
->range_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
86 ctx
->invariant_load_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
87 "invariant.load", 14);
89 ctx
->fpmath_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
, "fpmath", 6);
91 args
[0] = LLVMConstReal(ctx
->f32
, 2.5);
92 ctx
->fpmath_md_2p5_ulp
= LLVMMDNodeInContext(ctx
->context
, args
, 1);
94 ctx
->uniform_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
95 "amdgpu.uniform", 14);
97 ctx
->empty_md
= LLVMMDNodeInContext(ctx
->context
, NULL
, 0);
101 ac_get_type_size(LLVMTypeRef type
)
103 LLVMTypeKind kind
= LLVMGetTypeKind(type
);
106 case LLVMIntegerTypeKind
:
107 return LLVMGetIntTypeWidth(type
) / 8;
108 case LLVMFloatTypeKind
:
110 case LLVMDoubleTypeKind
:
111 case LLVMPointerTypeKind
:
113 case LLVMVectorTypeKind
:
114 return LLVMGetVectorSize(type
) *
115 ac_get_type_size(LLVMGetElementType(type
));
116 case LLVMArrayTypeKind
:
117 return LLVMGetArrayLength(type
) *
118 ac_get_type_size(LLVMGetElementType(type
));
125 static LLVMTypeRef
to_integer_type_scalar(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
127 if (t
== ctx
->f16
|| t
== ctx
->i16
)
129 else if (t
== ctx
->f32
|| t
== ctx
->i32
)
131 else if (t
== ctx
->f64
|| t
== ctx
->i64
)
134 unreachable("Unhandled integer size");
138 ac_to_integer_type(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
140 if (LLVMGetTypeKind(t
) == LLVMVectorTypeKind
) {
141 LLVMTypeRef elem_type
= LLVMGetElementType(t
);
142 return LLVMVectorType(to_integer_type_scalar(ctx
, elem_type
),
143 LLVMGetVectorSize(t
));
145 return to_integer_type_scalar(ctx
, t
);
149 ac_to_integer(struct ac_llvm_context
*ctx
, LLVMValueRef v
)
151 LLVMTypeRef type
= LLVMTypeOf(v
);
152 return LLVMBuildBitCast(ctx
->builder
, v
, ac_to_integer_type(ctx
, type
), "");
155 static LLVMTypeRef
to_float_type_scalar(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
157 if (t
== ctx
->i16
|| t
== ctx
->f16
)
159 else if (t
== ctx
->i32
|| t
== ctx
->f32
)
161 else if (t
== ctx
->i64
|| t
== ctx
->f64
)
164 unreachable("Unhandled float size");
168 ac_to_float_type(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
170 if (LLVMGetTypeKind(t
) == LLVMVectorTypeKind
) {
171 LLVMTypeRef elem_type
= LLVMGetElementType(t
);
172 return LLVMVectorType(to_float_type_scalar(ctx
, elem_type
),
173 LLVMGetVectorSize(t
));
175 return to_float_type_scalar(ctx
, t
);
179 ac_to_float(struct ac_llvm_context
*ctx
, LLVMValueRef v
)
181 LLVMTypeRef type
= LLVMTypeOf(v
);
182 return LLVMBuildBitCast(ctx
->builder
, v
, ac_to_float_type(ctx
, type
), "");
187 ac_build_intrinsic(struct ac_llvm_context
*ctx
, const char *name
,
188 LLVMTypeRef return_type
, LLVMValueRef
*params
,
189 unsigned param_count
, unsigned attrib_mask
)
191 LLVMValueRef function
, call
;
192 bool set_callsite_attrs
= HAVE_LLVM
>= 0x0400 &&
193 !(attrib_mask
& AC_FUNC_ATTR_LEGACY
);
195 function
= LLVMGetNamedFunction(ctx
->module
, name
);
197 LLVMTypeRef param_types
[32], function_type
;
200 assert(param_count
<= 32);
202 for (i
= 0; i
< param_count
; ++i
) {
204 param_types
[i
] = LLVMTypeOf(params
[i
]);
207 LLVMFunctionType(return_type
, param_types
, param_count
, 0);
208 function
= LLVMAddFunction(ctx
->module
, name
, function_type
);
210 LLVMSetFunctionCallConv(function
, LLVMCCallConv
);
211 LLVMSetLinkage(function
, LLVMExternalLinkage
);
213 if (!set_callsite_attrs
)
214 ac_add_func_attributes(ctx
->context
, function
, attrib_mask
);
217 call
= LLVMBuildCall(ctx
->builder
, function
, params
, param_count
, "");
218 if (set_callsite_attrs
)
219 ac_add_func_attributes(ctx
->context
, call
, attrib_mask
);
224 * Given the i32 or vNi32 \p type, generate the textual name (e.g. for use with
227 void ac_build_type_name_for_intr(LLVMTypeRef type
, char *buf
, unsigned bufsize
)
229 LLVMTypeRef elem_type
= type
;
231 assert(bufsize
>= 8);
233 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
) {
234 int ret
= snprintf(buf
, bufsize
, "v%u",
235 LLVMGetVectorSize(type
));
237 char *type_name
= LLVMPrintTypeToString(type
);
238 fprintf(stderr
, "Error building type name for: %s\n",
242 elem_type
= LLVMGetElementType(type
);
246 switch (LLVMGetTypeKind(elem_type
)) {
248 case LLVMIntegerTypeKind
:
249 snprintf(buf
, bufsize
, "i%d", LLVMGetIntTypeWidth(elem_type
));
251 case LLVMFloatTypeKind
:
252 snprintf(buf
, bufsize
, "f32");
254 case LLVMDoubleTypeKind
:
255 snprintf(buf
, bufsize
, "f64");
261 * Helper function that builds an LLVM IR PHI node and immediately adds
265 ac_build_phi(struct ac_llvm_context
*ctx
, LLVMTypeRef type
,
266 unsigned count_incoming
, LLVMValueRef
*values
,
267 LLVMBasicBlockRef
*blocks
)
269 LLVMValueRef phi
= LLVMBuildPhi(ctx
->builder
, type
, "");
270 LLVMAddIncoming(phi
, values
, blocks
, count_incoming
);
274 /* Prevent optimizations (at least of memory accesses) across the current
275 * point in the program by emitting empty inline assembly that is marked as
276 * having side effects.
278 * Optionally, a value can be passed through the inline assembly to prevent
279 * LLVM from hoisting calls to ReadNone functions.
282 ac_build_optimization_barrier(struct ac_llvm_context
*ctx
,
285 static int counter
= 0;
287 LLVMBuilderRef builder
= ctx
->builder
;
290 snprintf(code
, sizeof(code
), "; %d", p_atomic_inc_return(&counter
));
293 LLVMTypeRef ftype
= LLVMFunctionType(ctx
->voidt
, NULL
, 0, false);
294 LLVMValueRef inlineasm
= LLVMConstInlineAsm(ftype
, code
, "", true, false);
295 LLVMBuildCall(builder
, inlineasm
, NULL
, 0, "");
297 LLVMTypeRef ftype
= LLVMFunctionType(ctx
->i32
, &ctx
->i32
, 1, false);
298 LLVMValueRef inlineasm
= LLVMConstInlineAsm(ftype
, code
, "=v,0", true, false);
299 LLVMValueRef vgpr
= *pvgpr
;
300 LLVMTypeRef vgpr_type
= LLVMTypeOf(vgpr
);
301 unsigned vgpr_size
= ac_get_type_size(vgpr_type
);
304 assert(vgpr_size
% 4 == 0);
306 vgpr
= LLVMBuildBitCast(builder
, vgpr
, LLVMVectorType(ctx
->i32
, vgpr_size
/ 4), "");
307 vgpr0
= LLVMBuildExtractElement(builder
, vgpr
, ctx
->i32_0
, "");
308 vgpr0
= LLVMBuildCall(builder
, inlineasm
, &vgpr0
, 1, "");
309 vgpr
= LLVMBuildInsertElement(builder
, vgpr
, vgpr0
, ctx
->i32_0
, "");
310 vgpr
= LLVMBuildBitCast(builder
, vgpr
, vgpr_type
, "");
317 ac_build_ballot(struct ac_llvm_context
*ctx
,
320 LLVMValueRef args
[3] = {
323 LLVMConstInt(ctx
->i32
, LLVMIntNE
, 0)
326 /* We currently have no other way to prevent LLVM from lifting the icmp
327 * calls to a dominating basic block.
329 ac_build_optimization_barrier(ctx
, &args
[0]);
331 if (LLVMTypeOf(args
[0]) != ctx
->i32
)
332 args
[0] = LLVMBuildBitCast(ctx
->builder
, args
[0], ctx
->i32
, "");
334 return ac_build_intrinsic(ctx
,
335 "llvm.amdgcn.icmp.i32",
337 AC_FUNC_ATTR_NOUNWIND
|
338 AC_FUNC_ATTR_READNONE
|
339 AC_FUNC_ATTR_CONVERGENT
);
343 ac_build_vote_all(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
345 LLVMValueRef active_set
= ac_build_ballot(ctx
, ctx
->i32_1
);
346 LLVMValueRef vote_set
= ac_build_ballot(ctx
, value
);
347 return LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, vote_set
, active_set
, "");
351 ac_build_vote_any(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
353 LLVMValueRef vote_set
= ac_build_ballot(ctx
, value
);
354 return LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, vote_set
,
355 LLVMConstInt(ctx
->i64
, 0, 0), "");
359 ac_build_vote_eq(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
361 LLVMValueRef active_set
= ac_build_ballot(ctx
, ctx
->i32_1
);
362 LLVMValueRef vote_set
= ac_build_ballot(ctx
, value
);
364 LLVMValueRef all
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
365 vote_set
, active_set
, "");
366 LLVMValueRef none
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
368 LLVMConstInt(ctx
->i64
, 0, 0), "");
369 return LLVMBuildOr(ctx
->builder
, all
, none
, "");
373 ac_build_gather_values_extended(struct ac_llvm_context
*ctx
,
374 LLVMValueRef
*values
,
375 unsigned value_count
,
376 unsigned value_stride
,
380 LLVMBuilderRef builder
= ctx
->builder
;
381 LLVMValueRef vec
= NULL
;
384 if (value_count
== 1 && !always_vector
) {
386 return LLVMBuildLoad(builder
, values
[0], "");
388 } else if (!value_count
)
389 unreachable("value_count is 0");
391 for (i
= 0; i
< value_count
; i
++) {
392 LLVMValueRef value
= values
[i
* value_stride
];
394 value
= LLVMBuildLoad(builder
, value
, "");
397 vec
= LLVMGetUndef( LLVMVectorType(LLVMTypeOf(value
), value_count
));
398 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, i
, false);
399 vec
= LLVMBuildInsertElement(builder
, vec
, value
, index
, "");
405 ac_build_gather_values(struct ac_llvm_context
*ctx
,
406 LLVMValueRef
*values
,
407 unsigned value_count
)
409 return ac_build_gather_values_extended(ctx
, values
, value_count
, 1, false, false);
413 ac_build_fdiv(struct ac_llvm_context
*ctx
,
417 LLVMValueRef ret
= LLVMBuildFDiv(ctx
->builder
, num
, den
, "");
419 if (!LLVMIsConstant(ret
))
420 LLVMSetMetadata(ret
, ctx
->fpmath_md_kind
, ctx
->fpmath_md_2p5_ulp
);
424 /* Coordinates for cube map selection. sc, tc, and ma are as in Table 8.27
425 * of the OpenGL 4.5 (Compatibility Profile) specification, except ma is
426 * already multiplied by two. id is the cube face number.
428 struct cube_selection_coords
{
435 build_cube_intrinsic(struct ac_llvm_context
*ctx
,
437 struct cube_selection_coords
*out
)
439 LLVMTypeRef f32
= ctx
->f32
;
441 out
->stc
[1] = ac_build_intrinsic(ctx
, "llvm.amdgcn.cubetc",
442 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
443 out
->stc
[0] = ac_build_intrinsic(ctx
, "llvm.amdgcn.cubesc",
444 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
445 out
->ma
= ac_build_intrinsic(ctx
, "llvm.amdgcn.cubema",
446 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
447 out
->id
= ac_build_intrinsic(ctx
, "llvm.amdgcn.cubeid",
448 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
452 * Build a manual selection sequence for cube face sc/tc coordinates and
453 * major axis vector (multiplied by 2 for consistency) for the given
454 * vec3 \p coords, for the face implied by \p selcoords.
456 * For the major axis, we always adjust the sign to be in the direction of
457 * selcoords.ma; i.e., a positive out_ma means that coords is pointed towards
458 * the selcoords major axis.
460 static void build_cube_select(struct ac_llvm_context
*ctx
,
461 const struct cube_selection_coords
*selcoords
,
462 const LLVMValueRef
*coords
,
463 LLVMValueRef
*out_st
,
464 LLVMValueRef
*out_ma
)
466 LLVMBuilderRef builder
= ctx
->builder
;
467 LLVMTypeRef f32
= LLVMTypeOf(coords
[0]);
468 LLVMValueRef is_ma_positive
;
470 LLVMValueRef is_ma_z
, is_not_ma_z
;
471 LLVMValueRef is_ma_y
;
472 LLVMValueRef is_ma_x
;
476 is_ma_positive
= LLVMBuildFCmp(builder
, LLVMRealUGE
,
477 selcoords
->ma
, LLVMConstReal(f32
, 0.0), "");
478 sgn_ma
= LLVMBuildSelect(builder
, is_ma_positive
,
479 LLVMConstReal(f32
, 1.0), LLVMConstReal(f32
, -1.0), "");
481 is_ma_z
= LLVMBuildFCmp(builder
, LLVMRealUGE
, selcoords
->id
, LLVMConstReal(f32
, 4.0), "");
482 is_not_ma_z
= LLVMBuildNot(builder
, is_ma_z
, "");
483 is_ma_y
= LLVMBuildAnd(builder
, is_not_ma_z
,
484 LLVMBuildFCmp(builder
, LLVMRealUGE
, selcoords
->id
, LLVMConstReal(f32
, 2.0), ""), "");
485 is_ma_x
= LLVMBuildAnd(builder
, is_not_ma_z
, LLVMBuildNot(builder
, is_ma_y
, ""), "");
488 tmp
= LLVMBuildSelect(builder
, is_ma_x
, coords
[2], coords
[0], "");
489 sgn
= LLVMBuildSelect(builder
, is_ma_y
, LLVMConstReal(f32
, 1.0),
490 LLVMBuildSelect(builder
, is_ma_z
, sgn_ma
,
491 LLVMBuildFNeg(builder
, sgn_ma
, ""), ""), "");
492 out_st
[0] = LLVMBuildFMul(builder
, tmp
, sgn
, "");
495 tmp
= LLVMBuildSelect(builder
, is_ma_y
, coords
[2], coords
[1], "");
496 sgn
= LLVMBuildSelect(builder
, is_ma_y
, sgn_ma
,
497 LLVMConstReal(f32
, -1.0), "");
498 out_st
[1] = LLVMBuildFMul(builder
, tmp
, sgn
, "");
501 tmp
= LLVMBuildSelect(builder
, is_ma_z
, coords
[2],
502 LLVMBuildSelect(builder
, is_ma_y
, coords
[1], coords
[0], ""), "");
503 tmp
= ac_build_intrinsic(ctx
, "llvm.fabs.f32",
504 ctx
->f32
, &tmp
, 1, AC_FUNC_ATTR_READNONE
);
505 *out_ma
= LLVMBuildFMul(builder
, tmp
, LLVMConstReal(f32
, 2.0), "");
509 ac_prepare_cube_coords(struct ac_llvm_context
*ctx
,
510 bool is_deriv
, bool is_array
, bool is_lod
,
511 LLVMValueRef
*coords_arg
,
512 LLVMValueRef
*derivs_arg
)
515 LLVMBuilderRef builder
= ctx
->builder
;
516 struct cube_selection_coords selcoords
;
517 LLVMValueRef coords
[3];
520 if (is_array
&& !is_lod
) {
521 LLVMValueRef tmp
= coords_arg
[3];
522 tmp
= ac_build_intrinsic(ctx
, "llvm.rint.f32", ctx
->f32
, &tmp
, 1, 0);
524 /* Section 8.9 (Texture Functions) of the GLSL 4.50 spec says:
526 * "For Array forms, the array layer used will be
528 * max(0, min(d−1, floor(layer+0.5)))
530 * where d is the depth of the texture array and layer
531 * comes from the component indicated in the tables below.
532 * Workaroudn for an issue where the layer is taken from a
533 * helper invocation which happens to fall on a different
534 * layer due to extrapolation."
536 * VI and earlier attempt to implement this in hardware by
537 * clamping the value of coords[2] = (8 * layer) + face.
538 * Unfortunately, this means that the we end up with the wrong
539 * face when clamping occurs.
541 * Clamp the layer earlier to work around the issue.
543 if (ctx
->chip_class
<= VI
) {
545 ge0
= LLVMBuildFCmp(builder
, LLVMRealOGE
, tmp
, ctx
->f32_0
, "");
546 tmp
= LLVMBuildSelect(builder
, ge0
, tmp
, ctx
->f32_0
, "");
552 build_cube_intrinsic(ctx
, coords_arg
, &selcoords
);
554 invma
= ac_build_intrinsic(ctx
, "llvm.fabs.f32",
555 ctx
->f32
, &selcoords
.ma
, 1, AC_FUNC_ATTR_READNONE
);
556 invma
= ac_build_fdiv(ctx
, LLVMConstReal(ctx
->f32
, 1.0), invma
);
558 for (int i
= 0; i
< 2; ++i
)
559 coords
[i
] = LLVMBuildFMul(builder
, selcoords
.stc
[i
], invma
, "");
561 coords
[2] = selcoords
.id
;
563 if (is_deriv
&& derivs_arg
) {
564 LLVMValueRef derivs
[4];
567 /* Convert cube derivatives to 2D derivatives. */
568 for (axis
= 0; axis
< 2; axis
++) {
569 LLVMValueRef deriv_st
[2];
570 LLVMValueRef deriv_ma
;
572 /* Transform the derivative alongside the texture
573 * coordinate. Mathematically, the correct formula is
574 * as follows. Assume we're projecting onto the +Z face
575 * and denote by dx/dh the derivative of the (original)
576 * X texture coordinate with respect to horizontal
577 * window coordinates. The projection onto the +Z face
582 * Then df/dh = df/dx * dx/dh + df/dz * dz/dh
583 * = 1/z * dx/dh - x/z * 1/z * dz/dh.
585 * This motivatives the implementation below.
587 * Whether this actually gives the expected results for
588 * apps that might feed in derivatives obtained via
589 * finite differences is anyone's guess. The OpenGL spec
590 * seems awfully quiet about how textureGrad for cube
591 * maps should be handled.
593 build_cube_select(ctx
, &selcoords
, &derivs_arg
[axis
* 3],
594 deriv_st
, &deriv_ma
);
596 deriv_ma
= LLVMBuildFMul(builder
, deriv_ma
, invma
, "");
598 for (int i
= 0; i
< 2; ++i
)
599 derivs
[axis
* 2 + i
] =
600 LLVMBuildFSub(builder
,
601 LLVMBuildFMul(builder
, deriv_st
[i
], invma
, ""),
602 LLVMBuildFMul(builder
, deriv_ma
, coords
[i
], ""), "");
605 memcpy(derivs_arg
, derivs
, sizeof(derivs
));
608 /* Shift the texture coordinate. This must be applied after the
609 * derivative calculation.
611 for (int i
= 0; i
< 2; ++i
)
612 coords
[i
] = LLVMBuildFAdd(builder
, coords
[i
], LLVMConstReal(ctx
->f32
, 1.5), "");
615 /* for cube arrays coord.z = coord.w(array_index) * 8 + face */
616 /* coords_arg.w component - array_index for cube arrays */
617 LLVMValueRef tmp
= LLVMBuildFMul(ctx
->builder
, coords_arg
[3], LLVMConstReal(ctx
->f32
, 8.0), "");
618 coords
[2] = LLVMBuildFAdd(ctx
->builder
, tmp
, coords
[2], "");
621 memcpy(coords_arg
, coords
, sizeof(coords
));
626 ac_build_fs_interp(struct ac_llvm_context
*ctx
,
627 LLVMValueRef llvm_chan
,
628 LLVMValueRef attr_number
,
633 LLVMValueRef args
[5];
636 if (HAVE_LLVM
< 0x0400) {
638 ij
[0] = LLVMBuildBitCast(ctx
->builder
, i
, ctx
->i32
, "");
639 ij
[1] = LLVMBuildBitCast(ctx
->builder
, j
, ctx
->i32
, "");
642 args
[1] = attr_number
;
644 args
[3] = ac_build_gather_values(ctx
, ij
, 2);
645 return ac_build_intrinsic(ctx
, "llvm.SI.fs.interp",
647 AC_FUNC_ATTR_READNONE
);
652 args
[2] = attr_number
;
655 p1
= ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p1",
656 ctx
->f32
, args
, 4, AC_FUNC_ATTR_READNONE
);
661 args
[3] = attr_number
;
664 return ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p2",
665 ctx
->f32
, args
, 5, AC_FUNC_ATTR_READNONE
);
669 ac_build_fs_interp_mov(struct ac_llvm_context
*ctx
,
670 LLVMValueRef parameter
,
671 LLVMValueRef llvm_chan
,
672 LLVMValueRef attr_number
,
675 LLVMValueRef args
[4];
676 if (HAVE_LLVM
< 0x0400) {
678 args
[1] = attr_number
;
681 return ac_build_intrinsic(ctx
,
682 "llvm.SI.fs.constant",
684 AC_FUNC_ATTR_READNONE
);
689 args
[2] = attr_number
;
692 return ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.mov",
693 ctx
->f32
, args
, 4, AC_FUNC_ATTR_READNONE
);
697 ac_build_gep0(struct ac_llvm_context
*ctx
,
698 LLVMValueRef base_ptr
,
701 LLVMValueRef indices
[2] = {
702 LLVMConstInt(ctx
->i32
, 0, 0),
705 return LLVMBuildGEP(ctx
->builder
, base_ptr
,
710 ac_build_indexed_store(struct ac_llvm_context
*ctx
,
711 LLVMValueRef base_ptr
, LLVMValueRef index
,
714 LLVMBuildStore(ctx
->builder
, value
,
715 ac_build_gep0(ctx
, base_ptr
, index
));
719 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad.
720 * It's equivalent to doing a load from &base_ptr[index].
722 * \param base_ptr Where the array starts.
723 * \param index The element index into the array.
724 * \param uniform Whether the base_ptr and index can be assumed to be
725 * dynamically uniform (i.e. load to an SGPR)
726 * \param invariant Whether the load is invariant (no other opcodes affect it)
729 ac_build_load_custom(struct ac_llvm_context
*ctx
, LLVMValueRef base_ptr
,
730 LLVMValueRef index
, bool uniform
, bool invariant
)
732 LLVMValueRef pointer
, result
;
734 pointer
= ac_build_gep0(ctx
, base_ptr
, index
);
736 LLVMSetMetadata(pointer
, ctx
->uniform_md_kind
, ctx
->empty_md
);
737 result
= LLVMBuildLoad(ctx
->builder
, pointer
, "");
739 LLVMSetMetadata(result
, ctx
->invariant_load_md_kind
, ctx
->empty_md
);
743 LLVMValueRef
ac_build_load(struct ac_llvm_context
*ctx
, LLVMValueRef base_ptr
,
746 return ac_build_load_custom(ctx
, base_ptr
, index
, false, false);
749 LLVMValueRef
ac_build_load_invariant(struct ac_llvm_context
*ctx
,
750 LLVMValueRef base_ptr
, LLVMValueRef index
)
752 return ac_build_load_custom(ctx
, base_ptr
, index
, false, true);
755 LLVMValueRef
ac_build_load_to_sgpr(struct ac_llvm_context
*ctx
,
756 LLVMValueRef base_ptr
, LLVMValueRef index
)
758 return ac_build_load_custom(ctx
, base_ptr
, index
, true, true);
761 /* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
762 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
763 * or v4i32 (num_channels=3,4).
766 ac_build_buffer_store_dword(struct ac_llvm_context
*ctx
,
769 unsigned num_channels
,
770 LLVMValueRef voffset
,
771 LLVMValueRef soffset
,
772 unsigned inst_offset
,
775 bool writeonly_memory
,
776 bool swizzle_enable_hint
)
778 /* SWIZZLE_ENABLE requires that soffset isn't folded into voffset
779 * (voffset is swizzled, but soffset isn't swizzled).
780 * llvm.amdgcn.buffer.store doesn't have a separate soffset parameter.
782 if (!swizzle_enable_hint
) {
783 /* Split 3 channel stores, becase LLVM doesn't support 3-channel
785 if (num_channels
== 3) {
786 LLVMValueRef v
[3], v01
;
788 for (int i
= 0; i
< 3; i
++) {
789 v
[i
] = LLVMBuildExtractElement(ctx
->builder
, vdata
,
790 LLVMConstInt(ctx
->i32
, i
, 0), "");
792 v01
= ac_build_gather_values(ctx
, v
, 2);
794 ac_build_buffer_store_dword(ctx
, rsrc
, v01
, 2, voffset
,
795 soffset
, inst_offset
, glc
, slc
,
796 writeonly_memory
, swizzle_enable_hint
);
797 ac_build_buffer_store_dword(ctx
, rsrc
, v
[2], 1, voffset
,
798 soffset
, inst_offset
+ 8,
800 writeonly_memory
, swizzle_enable_hint
);
804 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
805 static const char *types
[] = {"f32", "v2f32", "v4f32"};
807 LLVMValueRef offset
= soffset
;
810 offset
= LLVMBuildAdd(ctx
->builder
, offset
,
811 LLVMConstInt(ctx
->i32
, inst_offset
, 0), "");
813 offset
= LLVMBuildAdd(ctx
->builder
, offset
, voffset
, "");
815 LLVMValueRef args
[] = {
816 ac_to_float(ctx
, vdata
),
817 LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, ""),
818 LLVMConstInt(ctx
->i32
, 0, 0),
820 LLVMConstInt(ctx
->i1
, glc
, 0),
821 LLVMConstInt(ctx
->i1
, slc
, 0),
824 snprintf(name
, sizeof(name
), "llvm.amdgcn.buffer.store.%s",
827 ac_build_intrinsic(ctx
, name
, ctx
->voidt
,
828 args
, ARRAY_SIZE(args
),
830 AC_FUNC_ATTR_INACCESSIBLE_MEM_ONLY
:
831 AC_FUNC_ATTR_WRITEONLY
);
835 static unsigned dfmt
[] = {
836 V_008F0C_BUF_DATA_FORMAT_32
,
837 V_008F0C_BUF_DATA_FORMAT_32_32
,
838 V_008F0C_BUF_DATA_FORMAT_32_32_32
,
839 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
841 assert(num_channels
>= 1 && num_channels
<= 4);
843 LLVMValueRef args
[] = {
846 LLVMConstInt(ctx
->i32
, num_channels
, 0),
847 voffset
? voffset
: LLVMGetUndef(ctx
->i32
),
849 LLVMConstInt(ctx
->i32
, inst_offset
, 0),
850 LLVMConstInt(ctx
->i32
, dfmt
[num_channels
- 1], 0),
851 LLVMConstInt(ctx
->i32
, V_008F0C_BUF_NUM_FORMAT_UINT
, 0),
852 LLVMConstInt(ctx
->i32
, voffset
!= NULL
, 0),
853 LLVMConstInt(ctx
->i32
, 0, 0), /* idxen */
854 LLVMConstInt(ctx
->i32
, glc
, 0),
855 LLVMConstInt(ctx
->i32
, slc
, 0),
856 LLVMConstInt(ctx
->i32
, 0, 0), /* tfe*/
859 /* The instruction offset field has 12 bits */
860 assert(voffset
|| inst_offset
< (1 << 12));
862 /* The intrinsic is overloaded, we need to add a type suffix for overloading to work. */
863 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
864 const char *types
[] = {"i32", "v2i32", "v4i32"};
866 snprintf(name
, sizeof(name
), "llvm.SI.tbuffer.store.%s", types
[func
]);
868 ac_build_intrinsic(ctx
, name
, ctx
->voidt
,
869 args
, ARRAY_SIZE(args
),
870 AC_FUNC_ATTR_LEGACY
);
874 ac_build_buffer_load(struct ac_llvm_context
*ctx
,
878 LLVMValueRef voffset
,
879 LLVMValueRef soffset
,
880 unsigned inst_offset
,
886 LLVMValueRef offset
= LLVMConstInt(ctx
->i32
, inst_offset
, 0);
888 offset
= LLVMBuildAdd(ctx
->builder
, offset
, voffset
, "");
890 offset
= LLVMBuildAdd(ctx
->builder
, offset
, soffset
, "");
892 /* TODO: VI and later generations can use SMEM with GLC=1.*/
893 if (allow_smem
&& !glc
&& !slc
) {
894 assert(vindex
== NULL
);
896 LLVMValueRef result
[4];
898 for (int i
= 0; i
< num_channels
; i
++) {
900 offset
= LLVMBuildAdd(ctx
->builder
, offset
,
901 LLVMConstInt(ctx
->i32
, 4, 0), "");
903 LLVMValueRef args
[2] = {rsrc
, offset
};
904 result
[i
] = ac_build_intrinsic(ctx
, "llvm.SI.load.const.v4i32",
906 AC_FUNC_ATTR_READNONE
|
907 AC_FUNC_ATTR_LEGACY
);
909 if (num_channels
== 1)
912 if (num_channels
== 3)
913 result
[num_channels
++] = LLVMGetUndef(ctx
->f32
);
914 return ac_build_gather_values(ctx
, result
, num_channels
);
917 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
919 LLVMValueRef args
[] = {
920 LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, ""),
921 vindex
? vindex
: LLVMConstInt(ctx
->i32
, 0, 0),
923 LLVMConstInt(ctx
->i1
, glc
, 0),
924 LLVMConstInt(ctx
->i1
, slc
, 0)
927 LLVMTypeRef types
[] = {ctx
->f32
, LLVMVectorType(ctx
->f32
, 2),
929 const char *type_names
[] = {"f32", "v2f32", "v4f32"};
932 snprintf(name
, sizeof(name
), "llvm.amdgcn.buffer.load.%s",
935 return ac_build_intrinsic(ctx
, name
, types
[func
], args
,
937 /* READNONE means writes can't affect it, while
938 * READONLY means that writes can affect it. */
939 can_speculate
&& HAVE_LLVM
>= 0x0400 ?
940 AC_FUNC_ATTR_READNONE
:
941 AC_FUNC_ATTR_READONLY
);
944 LLVMValueRef
ac_build_buffer_load_format(struct ac_llvm_context
*ctx
,
947 LLVMValueRef voffset
,
950 LLVMValueRef args
[] = {
951 LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, ""),
954 ctx
->i1false
, /* glc */
955 ctx
->i1false
, /* slc */
958 return ac_build_intrinsic(ctx
,
959 "llvm.amdgcn.buffer.load.format.v4f32",
960 ctx
->v4f32
, args
, ARRAY_SIZE(args
),
961 /* READNONE means writes can't affect it, while
962 * READONLY means that writes can affect it. */
963 can_speculate
&& HAVE_LLVM
>= 0x0400 ?
964 AC_FUNC_ATTR_READNONE
:
965 AC_FUNC_ATTR_READONLY
);
969 * Set range metadata on an instruction. This can only be used on load and
970 * call instructions. If you know an instruction can only produce the values
971 * 0, 1, 2, you would do set_range_metadata(value, 0, 3);
972 * \p lo is the minimum value inclusive.
973 * \p hi is the maximum value exclusive.
975 static void set_range_metadata(struct ac_llvm_context
*ctx
,
976 LLVMValueRef value
, unsigned lo
, unsigned hi
)
978 LLVMValueRef range_md
, md_args
[2];
979 LLVMTypeRef type
= LLVMTypeOf(value
);
980 LLVMContextRef context
= LLVMGetTypeContext(type
);
982 md_args
[0] = LLVMConstInt(type
, lo
, false);
983 md_args
[1] = LLVMConstInt(type
, hi
, false);
984 range_md
= LLVMMDNodeInContext(context
, md_args
, 2);
985 LLVMSetMetadata(value
, ctx
->range_md_kind
, range_md
);
989 ac_get_thread_id(struct ac_llvm_context
*ctx
)
993 LLVMValueRef tid_args
[2];
994 tid_args
[0] = LLVMConstInt(ctx
->i32
, 0xffffffff, false);
995 tid_args
[1] = LLVMConstInt(ctx
->i32
, 0, false);
996 tid_args
[1] = ac_build_intrinsic(ctx
,
997 "llvm.amdgcn.mbcnt.lo", ctx
->i32
,
998 tid_args
, 2, AC_FUNC_ATTR_READNONE
);
1000 tid
= ac_build_intrinsic(ctx
, "llvm.amdgcn.mbcnt.hi",
1002 2, AC_FUNC_ATTR_READNONE
);
1003 set_range_metadata(ctx
, tid
, 0, 64);
1008 * SI implements derivatives using the local data store (LDS)
1009 * All writes to the LDS happen in all executing threads at
1010 * the same time. TID is the Thread ID for the current
1011 * thread and is a value between 0 and 63, representing
1012 * the thread's position in the wavefront.
1014 * For the pixel shader threads are grouped into quads of four pixels.
1015 * The TIDs of the pixels of a quad are:
1023 * So, masking the TID with 0xfffffffc yields the TID of the top left pixel
1024 * of the quad, masking with 0xfffffffd yields the TID of the top pixel of
1025 * the current pixel's column, and masking with 0xfffffffe yields the TID
1026 * of the left pixel of the current pixel's row.
1028 * Adding 1 yields the TID of the pixel to the right of the left pixel, and
1029 * adding 2 yields the TID of the pixel below the top pixel.
1032 ac_build_ddxy(struct ac_llvm_context
*ctx
,
1037 LLVMValueRef tl
, trbl
, args
[2];
1038 LLVMValueRef result
;
1040 if (ctx
->chip_class
>= VI
) {
1041 LLVMValueRef thread_id
, tl_tid
, trbl_tid
;
1042 thread_id
= ac_get_thread_id(ctx
);
1044 tl_tid
= LLVMBuildAnd(ctx
->builder
, thread_id
,
1045 LLVMConstInt(ctx
->i32
, mask
, false), "");
1047 trbl_tid
= LLVMBuildAdd(ctx
->builder
, tl_tid
,
1048 LLVMConstInt(ctx
->i32
, idx
, false), "");
1050 args
[0] = LLVMBuildMul(ctx
->builder
, tl_tid
,
1051 LLVMConstInt(ctx
->i32
, 4, false), "");
1053 tl
= ac_build_intrinsic(ctx
,
1054 "llvm.amdgcn.ds.bpermute", ctx
->i32
,
1056 AC_FUNC_ATTR_READNONE
|
1057 AC_FUNC_ATTR_CONVERGENT
);
1059 args
[0] = LLVMBuildMul(ctx
->builder
, trbl_tid
,
1060 LLVMConstInt(ctx
->i32
, 4, false), "");
1061 trbl
= ac_build_intrinsic(ctx
,
1062 "llvm.amdgcn.ds.bpermute", ctx
->i32
,
1064 AC_FUNC_ATTR_READNONE
|
1065 AC_FUNC_ATTR_CONVERGENT
);
1067 uint32_t masks
[2] = {};
1070 case AC_TID_MASK_TOP_LEFT
:
1078 case AC_TID_MASK_TOP
:
1082 case AC_TID_MASK_LEFT
:
1091 args
[1] = LLVMConstInt(ctx
->i32
, masks
[0], false);
1093 tl
= ac_build_intrinsic(ctx
,
1094 "llvm.amdgcn.ds.swizzle", ctx
->i32
,
1096 AC_FUNC_ATTR_READNONE
|
1097 AC_FUNC_ATTR_CONVERGENT
);
1099 args
[1] = LLVMConstInt(ctx
->i32
, masks
[1], false);
1100 trbl
= ac_build_intrinsic(ctx
,
1101 "llvm.amdgcn.ds.swizzle", ctx
->i32
,
1103 AC_FUNC_ATTR_READNONE
|
1104 AC_FUNC_ATTR_CONVERGENT
);
1107 tl
= LLVMBuildBitCast(ctx
->builder
, tl
, ctx
->f32
, "");
1108 trbl
= LLVMBuildBitCast(ctx
->builder
, trbl
, ctx
->f32
, "");
1109 result
= LLVMBuildFSub(ctx
->builder
, trbl
, tl
, "");
1114 ac_build_sendmsg(struct ac_llvm_context
*ctx
,
1116 LLVMValueRef wave_id
)
1118 LLVMValueRef args
[2];
1119 const char *intr_name
= (HAVE_LLVM
< 0x0400) ? "llvm.SI.sendmsg" : "llvm.amdgcn.s.sendmsg";
1120 args
[0] = LLVMConstInt(ctx
->i32
, msg
, false);
1122 ac_build_intrinsic(ctx
, intr_name
, ctx
->voidt
, args
, 2, 0);
1126 ac_build_imsb(struct ac_llvm_context
*ctx
,
1128 LLVMTypeRef dst_type
)
1130 const char *intr_name
= (HAVE_LLVM
< 0x0400) ? "llvm.AMDGPU.flbit.i32" :
1131 "llvm.amdgcn.sffbh.i32";
1132 LLVMValueRef msb
= ac_build_intrinsic(ctx
, intr_name
,
1134 AC_FUNC_ATTR_READNONE
);
1136 /* The HW returns the last bit index from MSB, but NIR/TGSI wants
1137 * the index from LSB. Invert it by doing "31 - msb". */
1138 msb
= LLVMBuildSub(ctx
->builder
, LLVMConstInt(ctx
->i32
, 31, false),
1141 LLVMValueRef all_ones
= LLVMConstInt(ctx
->i32
, -1, true);
1142 LLVMValueRef cond
= LLVMBuildOr(ctx
->builder
,
1143 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
1144 arg
, LLVMConstInt(ctx
->i32
, 0, 0), ""),
1145 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
1146 arg
, all_ones
, ""), "");
1148 return LLVMBuildSelect(ctx
->builder
, cond
, all_ones
, msb
, "");
1152 ac_build_umsb(struct ac_llvm_context
*ctx
,
1154 LLVMTypeRef dst_type
)
1156 LLVMValueRef args
[2] = {
1160 LLVMValueRef msb
= ac_build_intrinsic(ctx
, "llvm.ctlz.i32",
1161 dst_type
, args
, ARRAY_SIZE(args
),
1162 AC_FUNC_ATTR_READNONE
);
1164 /* The HW returns the last bit index from MSB, but TGSI/NIR wants
1165 * the index from LSB. Invert it by doing "31 - msb". */
1166 msb
= LLVMBuildSub(ctx
->builder
, LLVMConstInt(ctx
->i32
, 31, false),
1169 /* check for zero */
1170 return LLVMBuildSelect(ctx
->builder
,
1171 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, arg
,
1172 LLVMConstInt(ctx
->i32
, 0, 0), ""),
1173 LLVMConstInt(ctx
->i32
, -1, true), msb
, "");
1176 LLVMValueRef
ac_build_umin(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
1179 LLVMValueRef cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntULE
, a
, b
, "");
1180 return LLVMBuildSelect(ctx
->builder
, cmp
, a
, b
, "");
1183 LLVMValueRef
ac_build_clamp(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
1185 if (HAVE_LLVM
>= 0x0500) {
1186 LLVMValueRef max
[2] = {
1188 LLVMConstReal(ctx
->f32
, 0),
1190 LLVMValueRef min
[2] = {
1191 LLVMConstReal(ctx
->f32
, 1),
1194 min
[1] = ac_build_intrinsic(ctx
, "llvm.maxnum.f32",
1196 AC_FUNC_ATTR_READNONE
);
1197 return ac_build_intrinsic(ctx
, "llvm.minnum.f32",
1199 AC_FUNC_ATTR_READNONE
);
1202 LLVMValueRef args
[3] = {
1204 LLVMConstReal(ctx
->f32
, 0),
1205 LLVMConstReal(ctx
->f32
, 1),
1208 return ac_build_intrinsic(ctx
, "llvm.AMDGPU.clamp.", ctx
->f32
, args
, 3,
1209 AC_FUNC_ATTR_READNONE
|
1210 AC_FUNC_ATTR_LEGACY
);
1213 void ac_build_export(struct ac_llvm_context
*ctx
, struct ac_export_args
*a
)
1215 LLVMValueRef args
[9];
1217 if (HAVE_LLVM
>= 0x0500) {
1218 args
[0] = LLVMConstInt(ctx
->i32
, a
->target
, 0);
1219 args
[1] = LLVMConstInt(ctx
->i32
, a
->enabled_channels
, 0);
1222 LLVMTypeRef i16
= LLVMInt16TypeInContext(ctx
->context
);
1223 LLVMTypeRef v2i16
= LLVMVectorType(i16
, 2);
1225 args
[2] = LLVMBuildBitCast(ctx
->builder
, a
->out
[0],
1227 args
[3] = LLVMBuildBitCast(ctx
->builder
, a
->out
[1],
1229 args
[4] = LLVMConstInt(ctx
->i1
, a
->done
, 0);
1230 args
[5] = LLVMConstInt(ctx
->i1
, a
->valid_mask
, 0);
1232 ac_build_intrinsic(ctx
, "llvm.amdgcn.exp.compr.v2i16",
1233 ctx
->voidt
, args
, 6, 0);
1235 args
[2] = a
->out
[0];
1236 args
[3] = a
->out
[1];
1237 args
[4] = a
->out
[2];
1238 args
[5] = a
->out
[3];
1239 args
[6] = LLVMConstInt(ctx
->i1
, a
->done
, 0);
1240 args
[7] = LLVMConstInt(ctx
->i1
, a
->valid_mask
, 0);
1242 ac_build_intrinsic(ctx
, "llvm.amdgcn.exp.f32",
1243 ctx
->voidt
, args
, 8, 0);
1248 args
[0] = LLVMConstInt(ctx
->i32
, a
->enabled_channels
, 0);
1249 args
[1] = LLVMConstInt(ctx
->i32
, a
->valid_mask
, 0);
1250 args
[2] = LLVMConstInt(ctx
->i32
, a
->done
, 0);
1251 args
[3] = LLVMConstInt(ctx
->i32
, a
->target
, 0);
1252 args
[4] = LLVMConstInt(ctx
->i32
, a
->compr
, 0);
1253 memcpy(args
+ 5, a
->out
, sizeof(a
->out
[0]) * 4);
1255 ac_build_intrinsic(ctx
, "llvm.SI.export", ctx
->voidt
, args
, 9,
1256 AC_FUNC_ATTR_LEGACY
);
1259 LLVMValueRef
ac_build_image_opcode(struct ac_llvm_context
*ctx
,
1260 struct ac_image_args
*a
)
1262 LLVMTypeRef dst_type
;
1263 LLVMValueRef args
[11];
1264 unsigned num_args
= 0;
1265 const char *name
= NULL
;
1266 char intr_name
[128], type
[64];
1268 if (HAVE_LLVM
>= 0x0400) {
1269 bool sample
= a
->opcode
== ac_image_sample
||
1270 a
->opcode
== ac_image_gather4
||
1271 a
->opcode
== ac_image_get_lod
;
1274 args
[num_args
++] = ac_to_float(ctx
, a
->addr
);
1276 args
[num_args
++] = a
->addr
;
1278 args
[num_args
++] = a
->resource
;
1280 args
[num_args
++] = a
->sampler
;
1281 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->dmask
, 0);
1283 args
[num_args
++] = LLVMConstInt(ctx
->i1
, a
->unorm
, 0);
1284 args
[num_args
++] = ctx
->i1false
; /* glc */
1285 args
[num_args
++] = ctx
->i1false
; /* slc */
1286 args
[num_args
++] = ctx
->i1false
; /* lwe */
1287 args
[num_args
++] = LLVMConstInt(ctx
->i1
, a
->da
, 0);
1289 switch (a
->opcode
) {
1290 case ac_image_sample
:
1291 name
= "llvm.amdgcn.image.sample";
1293 case ac_image_gather4
:
1294 name
= "llvm.amdgcn.image.gather4";
1297 name
= "llvm.amdgcn.image.load";
1299 case ac_image_load_mip
:
1300 name
= "llvm.amdgcn.image.load.mip";
1302 case ac_image_get_lod
:
1303 name
= "llvm.amdgcn.image.getlod";
1305 case ac_image_get_resinfo
:
1306 name
= "llvm.amdgcn.image.getresinfo";
1309 unreachable("invalid image opcode");
1312 ac_build_type_name_for_intr(LLVMTypeOf(args
[0]), type
,
1315 snprintf(intr_name
, sizeof(intr_name
), "%s%s%s%s.v4f32.%s.v8i32",
1317 a
->compare
? ".c" : "",
1321 a
->level_zero
? ".lz" : "",
1322 a
->offset
? ".o" : "",
1325 LLVMValueRef result
=
1326 ac_build_intrinsic(ctx
, intr_name
,
1327 ctx
->v4f32
, args
, num_args
,
1328 AC_FUNC_ATTR_READNONE
);
1330 result
= LLVMBuildBitCast(ctx
->builder
, result
,
1336 args
[num_args
++] = a
->addr
;
1337 args
[num_args
++] = a
->resource
;
1339 if (a
->opcode
== ac_image_load
||
1340 a
->opcode
== ac_image_load_mip
||
1341 a
->opcode
== ac_image_get_resinfo
) {
1342 dst_type
= ctx
->v4i32
;
1344 dst_type
= ctx
->v4f32
;
1345 args
[num_args
++] = a
->sampler
;
1348 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->dmask
, 0);
1349 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->unorm
, 0);
1350 args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* r128 */
1351 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->da
, 0);
1352 args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* glc */
1353 args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* slc */
1354 args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* tfe */
1355 args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* lwe */
1357 switch (a
->opcode
) {
1358 case ac_image_sample
:
1359 name
= "llvm.SI.image.sample";
1361 case ac_image_gather4
:
1362 name
= "llvm.SI.gather4";
1365 name
= "llvm.SI.image.load";
1367 case ac_image_load_mip
:
1368 name
= "llvm.SI.image.load.mip";
1370 case ac_image_get_lod
:
1371 name
= "llvm.SI.getlod";
1373 case ac_image_get_resinfo
:
1374 name
= "llvm.SI.getresinfo";
1378 ac_build_type_name_for_intr(LLVMTypeOf(a
->addr
), type
, sizeof(type
));
1379 snprintf(intr_name
, sizeof(intr_name
), "%s%s%s%s.%s",
1381 a
->compare
? ".c" : "",
1385 a
->level_zero
? ".lz" : "",
1386 a
->offset
? ".o" : "",
1389 return ac_build_intrinsic(ctx
, intr_name
,
1390 dst_type
, args
, num_args
,
1391 AC_FUNC_ATTR_READNONE
|
1392 AC_FUNC_ATTR_LEGACY
);
1395 LLVMValueRef
ac_build_cvt_pkrtz_f16(struct ac_llvm_context
*ctx
,
1396 LLVMValueRef args
[2])
1398 if (HAVE_LLVM
>= 0x0500) {
1400 LLVMVectorType(LLVMHalfTypeInContext(ctx
->context
), 2);
1402 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pkrtz",
1404 AC_FUNC_ATTR_READNONE
);
1405 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
1408 return ac_build_intrinsic(ctx
, "llvm.SI.packf16", ctx
->i32
, args
, 2,
1409 AC_FUNC_ATTR_READNONE
|
1410 AC_FUNC_ATTR_LEGACY
);
1413 LLVMValueRef
ac_build_wqm_vote(struct ac_llvm_context
*ctx
, LLVMValueRef i1
)
1415 assert(HAVE_LLVM
>= 0x0600);
1416 return ac_build_intrinsic(ctx
, "llvm.amdgcn.wqm.vote", ctx
->i1
,
1417 &i1
, 1, AC_FUNC_ATTR_READNONE
);
1420 void ac_build_kill_if_false(struct ac_llvm_context
*ctx
, LLVMValueRef i1
)
1422 if (HAVE_LLVM
>= 0x0600) {
1423 ac_build_intrinsic(ctx
, "llvm.amdgcn.kill", ctx
->voidt
,
1428 LLVMValueRef value
= LLVMBuildSelect(ctx
->builder
, i1
,
1429 LLVMConstReal(ctx
->f32
, 1),
1430 LLVMConstReal(ctx
->f32
, -1), "");
1431 ac_build_intrinsic(ctx
, "llvm.AMDGPU.kill", ctx
->voidt
,
1432 &value
, 1, AC_FUNC_ATTR_LEGACY
);
1435 LLVMValueRef
ac_build_bfe(struct ac_llvm_context
*ctx
, LLVMValueRef input
,
1436 LLVMValueRef offset
, LLVMValueRef width
,
1439 LLVMValueRef args
[] = {
1445 if (HAVE_LLVM
>= 0x0500) {
1446 return ac_build_intrinsic(ctx
,
1447 is_signed
? "llvm.amdgcn.sbfe.i32" :
1448 "llvm.amdgcn.ubfe.i32",
1450 AC_FUNC_ATTR_READNONE
);
1453 return ac_build_intrinsic(ctx
,
1454 is_signed
? "llvm.AMDGPU.bfe.i32" :
1455 "llvm.AMDGPU.bfe.u32",
1457 AC_FUNC_ATTR_READNONE
|
1458 AC_FUNC_ATTR_LEGACY
);
1461 void ac_get_image_intr_name(const char *base_name
,
1462 LLVMTypeRef data_type
,
1463 LLVMTypeRef coords_type
,
1464 LLVMTypeRef rsrc_type
,
1465 char *out_name
, unsigned out_len
)
1467 char coords_type_name
[8];
1469 ac_build_type_name_for_intr(coords_type
, coords_type_name
,
1470 sizeof(coords_type_name
));
1472 if (HAVE_LLVM
<= 0x0309) {
1473 snprintf(out_name
, out_len
, "%s.%s", base_name
, coords_type_name
);
1475 char data_type_name
[8];
1476 char rsrc_type_name
[8];
1478 ac_build_type_name_for_intr(data_type
, data_type_name
,
1479 sizeof(data_type_name
));
1480 ac_build_type_name_for_intr(rsrc_type
, rsrc_type_name
,
1481 sizeof(rsrc_type_name
));
1482 snprintf(out_name
, out_len
, "%s.%s.%s.%s", base_name
,
1483 data_type_name
, coords_type_name
, rsrc_type_name
);
1487 #define AC_EXP_TARGET (HAVE_LLVM >= 0x0500 ? 0 : 3)
1488 #define AC_EXP_OUT0 (HAVE_LLVM >= 0x0500 ? 2 : 5)
1496 struct ac_vs_exp_chan
1500 enum ac_ir_type type
;
1503 struct ac_vs_exp_inst
{
1506 struct ac_vs_exp_chan chan
[4];
1509 struct ac_vs_exports
{
1511 struct ac_vs_exp_inst exp
[VARYING_SLOT_MAX
];
1514 /* Return true if the PARAM export has been eliminated. */
1515 static bool ac_eliminate_const_output(uint8_t *vs_output_param_offset
,
1516 uint32_t num_outputs
,
1517 struct ac_vs_exp_inst
*exp
)
1519 unsigned i
, default_val
; /* SPI_PS_INPUT_CNTL_i.DEFAULT_VAL */
1520 bool is_zero
[4] = {}, is_one
[4] = {};
1522 for (i
= 0; i
< 4; i
++) {
1523 /* It's a constant expression. Undef outputs are eliminated too. */
1524 if (exp
->chan
[i
].type
== AC_IR_UNDEF
) {
1527 } else if (exp
->chan
[i
].type
== AC_IR_CONST
) {
1528 if (exp
->chan
[i
].const_float
== 0)
1530 else if (exp
->chan
[i
].const_float
== 1)
1533 return false; /* other constant */
1538 /* Only certain combinations of 0 and 1 can be eliminated. */
1539 if (is_zero
[0] && is_zero
[1] && is_zero
[2])
1540 default_val
= is_zero
[3] ? 0 : 1;
1541 else if (is_one
[0] && is_one
[1] && is_one
[2])
1542 default_val
= is_zero
[3] ? 2 : 3;
1546 /* The PARAM export can be represented as DEFAULT_VAL. Kill it. */
1547 LLVMInstructionEraseFromParent(exp
->inst
);
1549 /* Change OFFSET to DEFAULT_VAL. */
1550 for (i
= 0; i
< num_outputs
; i
++) {
1551 if (vs_output_param_offset
[i
] == exp
->offset
) {
1552 vs_output_param_offset
[i
] =
1553 AC_EXP_PARAM_DEFAULT_VAL_0000
+ default_val
;
1560 static bool ac_eliminate_duplicated_output(uint8_t *vs_output_param_offset
,
1561 uint32_t num_outputs
,
1562 struct ac_vs_exports
*processed
,
1563 struct ac_vs_exp_inst
*exp
)
1565 unsigned p
, copy_back_channels
= 0;
1567 /* See if the output is already in the list of processed outputs.
1568 * The LLVMValueRef comparison relies on SSA.
1570 for (p
= 0; p
< processed
->num
; p
++) {
1571 bool different
= false;
1573 for (unsigned j
= 0; j
< 4; j
++) {
1574 struct ac_vs_exp_chan
*c1
= &processed
->exp
[p
].chan
[j
];
1575 struct ac_vs_exp_chan
*c2
= &exp
->chan
[j
];
1577 /* Treat undef as a match. */
1578 if (c2
->type
== AC_IR_UNDEF
)
1581 /* If c1 is undef but c2 isn't, we can copy c2 to c1
1582 * and consider the instruction duplicated.
1584 if (c1
->type
== AC_IR_UNDEF
) {
1585 copy_back_channels
|= 1 << j
;
1589 /* Test whether the channels are not equal. */
1590 if (c1
->type
!= c2
->type
||
1591 (c1
->type
== AC_IR_CONST
&&
1592 c1
->const_float
!= c2
->const_float
) ||
1593 (c1
->type
== AC_IR_VALUE
&&
1594 c1
->value
!= c2
->value
)) {
1602 copy_back_channels
= 0;
1604 if (p
== processed
->num
)
1607 /* If a match was found, but the matching export has undef where the new
1608 * one has a normal value, copy the normal value to the undef channel.
1610 struct ac_vs_exp_inst
*match
= &processed
->exp
[p
];
1612 while (copy_back_channels
) {
1613 unsigned chan
= u_bit_scan(©_back_channels
);
1615 assert(match
->chan
[chan
].type
== AC_IR_UNDEF
);
1616 LLVMSetOperand(match
->inst
, AC_EXP_OUT0
+ chan
,
1617 exp
->chan
[chan
].value
);
1618 match
->chan
[chan
] = exp
->chan
[chan
];
1621 /* The PARAM export is duplicated. Kill it. */
1622 LLVMInstructionEraseFromParent(exp
->inst
);
1624 /* Change OFFSET to the matching export. */
1625 for (unsigned i
= 0; i
< num_outputs
; i
++) {
1626 if (vs_output_param_offset
[i
] == exp
->offset
) {
1627 vs_output_param_offset
[i
] = match
->offset
;
1634 void ac_optimize_vs_outputs(struct ac_llvm_context
*ctx
,
1635 LLVMValueRef main_fn
,
1636 uint8_t *vs_output_param_offset
,
1637 uint32_t num_outputs
,
1638 uint8_t *num_param_exports
)
1640 LLVMBasicBlockRef bb
;
1641 bool removed_any
= false;
1642 struct ac_vs_exports exports
;
1646 /* Process all LLVM instructions. */
1647 bb
= LLVMGetFirstBasicBlock(main_fn
);
1649 LLVMValueRef inst
= LLVMGetFirstInstruction(bb
);
1652 LLVMValueRef cur
= inst
;
1653 inst
= LLVMGetNextInstruction(inst
);
1654 struct ac_vs_exp_inst exp
;
1656 if (LLVMGetInstructionOpcode(cur
) != LLVMCall
)
1659 LLVMValueRef callee
= ac_llvm_get_called_value(cur
);
1661 if (!ac_llvm_is_function(callee
))
1664 const char *name
= LLVMGetValueName(callee
);
1665 unsigned num_args
= LLVMCountParams(callee
);
1667 /* Check if this is an export instruction. */
1668 if ((num_args
!= 9 && num_args
!= 8) ||
1669 (strcmp(name
, "llvm.SI.export") &&
1670 strcmp(name
, "llvm.amdgcn.exp.f32")))
1673 LLVMValueRef arg
= LLVMGetOperand(cur
, AC_EXP_TARGET
);
1674 unsigned target
= LLVMConstIntGetZExtValue(arg
);
1676 if (target
< V_008DFC_SQ_EXP_PARAM
)
1679 target
-= V_008DFC_SQ_EXP_PARAM
;
1681 /* Parse the instruction. */
1682 memset(&exp
, 0, sizeof(exp
));
1683 exp
.offset
= target
;
1686 for (unsigned i
= 0; i
< 4; i
++) {
1687 LLVMValueRef v
= LLVMGetOperand(cur
, AC_EXP_OUT0
+ i
);
1689 exp
.chan
[i
].value
= v
;
1691 if (LLVMIsUndef(v
)) {
1692 exp
.chan
[i
].type
= AC_IR_UNDEF
;
1693 } else if (LLVMIsAConstantFP(v
)) {
1694 LLVMBool loses_info
;
1695 exp
.chan
[i
].type
= AC_IR_CONST
;
1696 exp
.chan
[i
].const_float
=
1697 LLVMConstRealGetDouble(v
, &loses_info
);
1699 exp
.chan
[i
].type
= AC_IR_VALUE
;
1703 /* Eliminate constant and duplicated PARAM exports. */
1704 if (ac_eliminate_const_output(vs_output_param_offset
,
1705 num_outputs
, &exp
) ||
1706 ac_eliminate_duplicated_output(vs_output_param_offset
,
1707 num_outputs
, &exports
,
1711 exports
.exp
[exports
.num
++] = exp
;
1714 bb
= LLVMGetNextBasicBlock(bb
);
1717 /* Remove holes in export memory due to removed PARAM exports.
1718 * This is done by renumbering all PARAM exports.
1721 uint8_t old_offset
[VARYING_SLOT_MAX
];
1724 /* Make a copy of the offsets. We need the old version while
1725 * we are modifying some of them. */
1726 memcpy(old_offset
, vs_output_param_offset
,
1727 sizeof(old_offset
));
1729 for (i
= 0; i
< exports
.num
; i
++) {
1730 unsigned offset
= exports
.exp
[i
].offset
;
1732 /* Update vs_output_param_offset. Multiple outputs can
1733 * have the same offset.
1735 for (out
= 0; out
< num_outputs
; out
++) {
1736 if (old_offset
[out
] == offset
)
1737 vs_output_param_offset
[out
] = i
;
1740 /* Change the PARAM offset in the instruction. */
1741 LLVMSetOperand(exports
.exp
[i
].inst
, AC_EXP_TARGET
,
1742 LLVMConstInt(ctx
->i32
,
1743 V_008DFC_SQ_EXP_PARAM
+ i
, 0));
1745 *num_param_exports
= exports
.num
;
1749 void ac_init_exec_full_mask(struct ac_llvm_context
*ctx
)
1751 LLVMValueRef full_mask
= LLVMConstInt(ctx
->i64
, ~0ull, 0);
1752 ac_build_intrinsic(ctx
,
1753 "llvm.amdgcn.init.exec", ctx
->voidt
,
1754 &full_mask
, 1, AC_FUNC_ATTR_CONVERGENT
);
1757 void ac_declare_lds_as_pointer(struct ac_llvm_context
*ctx
)
1759 unsigned lds_size
= ctx
->chip_class
>= CIK
? 65536 : 32768;
1760 ctx
->lds
= LLVMBuildIntToPtr(ctx
->builder
, ctx
->i32_0
,
1761 LLVMPointerType(LLVMArrayType(ctx
->i32
, lds_size
/ 4), AC_LOCAL_ADDR_SPACE
),
1765 LLVMValueRef
ac_lds_load(struct ac_llvm_context
*ctx
,
1766 LLVMValueRef dw_addr
)
1768 return ac_build_load(ctx
, ctx
->lds
, dw_addr
);
1771 void ac_lds_store(struct ac_llvm_context
*ctx
,
1772 LLVMValueRef dw_addr
,
1775 value
= ac_to_integer(ctx
, value
);
1776 ac_build_indexed_store(ctx
, ctx
->lds
,
1780 LLVMValueRef
ac_find_lsb(struct ac_llvm_context
*ctx
,
1781 LLVMTypeRef dst_type
,
1784 LLVMValueRef params
[2] = {
1787 /* The value of 1 means that ffs(x=0) = undef, so LLVM won't
1788 * add special code to check for x=0. The reason is that
1789 * the LLVM behavior for x=0 is different from what we
1790 * need here. However, LLVM also assumes that ffs(x) is
1791 * in [0, 31], but GLSL expects that ffs(0) = -1, so
1792 * a conditional assignment to handle 0 is still required.
1794 * The hardware already implements the correct behavior.
1796 LLVMConstInt(ctx
->i1
, 1, false),
1799 LLVMValueRef lsb
= ac_build_intrinsic(ctx
, "llvm.cttz.i32", ctx
->i32
,
1801 AC_FUNC_ATTR_READNONE
);
1803 /* TODO: We need an intrinsic to skip this conditional. */
1804 /* Check for zero: */
1805 return LLVMBuildSelect(ctx
->builder
, LLVMBuildICmp(ctx
->builder
,
1808 LLVMConstInt(ctx
->i32
, -1, 0), lsb
, "");