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
->range_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
81 ctx
->invariant_load_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
82 "invariant.load", 14);
84 ctx
->fpmath_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
, "fpmath", 6);
86 args
[0] = LLVMConstReal(ctx
->f32
, 2.5);
87 ctx
->fpmath_md_2p5_ulp
= LLVMMDNodeInContext(ctx
->context
, args
, 1);
89 ctx
->uniform_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
90 "amdgpu.uniform", 14);
92 ctx
->empty_md
= LLVMMDNodeInContext(ctx
->context
, NULL
, 0);
96 ac_get_type_size(LLVMTypeRef type
)
98 LLVMTypeKind kind
= LLVMGetTypeKind(type
);
101 case LLVMIntegerTypeKind
:
102 return LLVMGetIntTypeWidth(type
) / 8;
103 case LLVMFloatTypeKind
:
105 case LLVMDoubleTypeKind
:
106 case LLVMPointerTypeKind
:
108 case LLVMVectorTypeKind
:
109 return LLVMGetVectorSize(type
) *
110 ac_get_type_size(LLVMGetElementType(type
));
111 case LLVMArrayTypeKind
:
112 return LLVMGetArrayLength(type
) *
113 ac_get_type_size(LLVMGetElementType(type
));
120 static LLVMTypeRef
to_integer_type_scalar(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
122 if (t
== ctx
->f16
|| t
== ctx
->i16
)
124 else if (t
== ctx
->f32
|| t
== ctx
->i32
)
126 else if (t
== ctx
->f64
|| t
== ctx
->i64
)
129 unreachable("Unhandled integer size");
133 ac_to_integer_type(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
135 if (LLVMGetTypeKind(t
) == LLVMVectorTypeKind
) {
136 LLVMTypeRef elem_type
= LLVMGetElementType(t
);
137 return LLVMVectorType(to_integer_type_scalar(ctx
, elem_type
),
138 LLVMGetVectorSize(t
));
140 return to_integer_type_scalar(ctx
, t
);
144 ac_to_integer(struct ac_llvm_context
*ctx
, LLVMValueRef v
)
146 LLVMTypeRef type
= LLVMTypeOf(v
);
147 return LLVMBuildBitCast(ctx
->builder
, v
, ac_to_integer_type(ctx
, type
), "");
150 static LLVMTypeRef
to_float_type_scalar(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
152 if (t
== ctx
->i16
|| t
== ctx
->f16
)
154 else if (t
== ctx
->i32
|| t
== ctx
->f32
)
156 else if (t
== ctx
->i64
|| t
== ctx
->f64
)
159 unreachable("Unhandled float size");
163 ac_to_float_type(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
165 if (LLVMGetTypeKind(t
) == LLVMVectorTypeKind
) {
166 LLVMTypeRef elem_type
= LLVMGetElementType(t
);
167 return LLVMVectorType(to_float_type_scalar(ctx
, elem_type
),
168 LLVMGetVectorSize(t
));
170 return to_float_type_scalar(ctx
, t
);
174 ac_to_float(struct ac_llvm_context
*ctx
, LLVMValueRef v
)
176 LLVMTypeRef type
= LLVMTypeOf(v
);
177 return LLVMBuildBitCast(ctx
->builder
, v
, ac_to_float_type(ctx
, type
), "");
182 ac_build_intrinsic(struct ac_llvm_context
*ctx
, const char *name
,
183 LLVMTypeRef return_type
, LLVMValueRef
*params
,
184 unsigned param_count
, unsigned attrib_mask
)
186 LLVMValueRef function
, call
;
187 bool set_callsite_attrs
= HAVE_LLVM
>= 0x0400 &&
188 !(attrib_mask
& AC_FUNC_ATTR_LEGACY
);
190 function
= LLVMGetNamedFunction(ctx
->module
, name
);
192 LLVMTypeRef param_types
[32], function_type
;
195 assert(param_count
<= 32);
197 for (i
= 0; i
< param_count
; ++i
) {
199 param_types
[i
] = LLVMTypeOf(params
[i
]);
202 LLVMFunctionType(return_type
, param_types
, param_count
, 0);
203 function
= LLVMAddFunction(ctx
->module
, name
, function_type
);
205 LLVMSetFunctionCallConv(function
, LLVMCCallConv
);
206 LLVMSetLinkage(function
, LLVMExternalLinkage
);
208 if (!set_callsite_attrs
)
209 ac_add_func_attributes(ctx
->context
, function
, attrib_mask
);
212 call
= LLVMBuildCall(ctx
->builder
, function
, params
, param_count
, "");
213 if (set_callsite_attrs
)
214 ac_add_func_attributes(ctx
->context
, call
, attrib_mask
);
219 * Given the i32 or vNi32 \p type, generate the textual name (e.g. for use with
222 void ac_build_type_name_for_intr(LLVMTypeRef type
, char *buf
, unsigned bufsize
)
224 LLVMTypeRef elem_type
= type
;
226 assert(bufsize
>= 8);
228 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
) {
229 int ret
= snprintf(buf
, bufsize
, "v%u",
230 LLVMGetVectorSize(type
));
232 char *type_name
= LLVMPrintTypeToString(type
);
233 fprintf(stderr
, "Error building type name for: %s\n",
237 elem_type
= LLVMGetElementType(type
);
241 switch (LLVMGetTypeKind(elem_type
)) {
243 case LLVMIntegerTypeKind
:
244 snprintf(buf
, bufsize
, "i%d", LLVMGetIntTypeWidth(elem_type
));
246 case LLVMFloatTypeKind
:
247 snprintf(buf
, bufsize
, "f32");
249 case LLVMDoubleTypeKind
:
250 snprintf(buf
, bufsize
, "f64");
256 * Helper function that builds an LLVM IR PHI node and immediately adds
260 ac_build_phi(struct ac_llvm_context
*ctx
, LLVMTypeRef type
,
261 unsigned count_incoming
, LLVMValueRef
*values
,
262 LLVMBasicBlockRef
*blocks
)
264 LLVMValueRef phi
= LLVMBuildPhi(ctx
->builder
, type
, "");
265 LLVMAddIncoming(phi
, values
, blocks
, count_incoming
);
269 /* Prevent optimizations (at least of memory accesses) across the current
270 * point in the program by emitting empty inline assembly that is marked as
271 * having side effects.
273 * Optionally, a value can be passed through the inline assembly to prevent
274 * LLVM from hoisting calls to ReadNone functions.
277 ac_build_optimization_barrier(struct ac_llvm_context
*ctx
,
280 static int counter
= 0;
282 LLVMBuilderRef builder
= ctx
->builder
;
285 snprintf(code
, sizeof(code
), "; %d", p_atomic_inc_return(&counter
));
288 LLVMTypeRef ftype
= LLVMFunctionType(ctx
->voidt
, NULL
, 0, false);
289 LLVMValueRef inlineasm
= LLVMConstInlineAsm(ftype
, code
, "", true, false);
290 LLVMBuildCall(builder
, inlineasm
, NULL
, 0, "");
292 LLVMTypeRef ftype
= LLVMFunctionType(ctx
->i32
, &ctx
->i32
, 1, false);
293 LLVMValueRef inlineasm
= LLVMConstInlineAsm(ftype
, code
, "=v,0", true, false);
294 LLVMValueRef vgpr
= *pvgpr
;
295 LLVMTypeRef vgpr_type
= LLVMTypeOf(vgpr
);
296 unsigned vgpr_size
= ac_get_type_size(vgpr_type
);
299 assert(vgpr_size
% 4 == 0);
301 vgpr
= LLVMBuildBitCast(builder
, vgpr
, LLVMVectorType(ctx
->i32
, vgpr_size
/ 4), "");
302 vgpr0
= LLVMBuildExtractElement(builder
, vgpr
, ctx
->i32_0
, "");
303 vgpr0
= LLVMBuildCall(builder
, inlineasm
, &vgpr0
, 1, "");
304 vgpr
= LLVMBuildInsertElement(builder
, vgpr
, vgpr0
, ctx
->i32_0
, "");
305 vgpr
= LLVMBuildBitCast(builder
, vgpr
, vgpr_type
, "");
312 ac_build_ballot(struct ac_llvm_context
*ctx
,
315 LLVMValueRef args
[3] = {
318 LLVMConstInt(ctx
->i32
, LLVMIntNE
, 0)
321 /* We currently have no other way to prevent LLVM from lifting the icmp
322 * calls to a dominating basic block.
324 ac_build_optimization_barrier(ctx
, &args
[0]);
326 if (LLVMTypeOf(args
[0]) != ctx
->i32
)
327 args
[0] = LLVMBuildBitCast(ctx
->builder
, args
[0], ctx
->i32
, "");
329 return ac_build_intrinsic(ctx
,
330 "llvm.amdgcn.icmp.i32",
332 AC_FUNC_ATTR_NOUNWIND
|
333 AC_FUNC_ATTR_READNONE
|
334 AC_FUNC_ATTR_CONVERGENT
);
338 ac_build_vote_all(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
340 LLVMValueRef active_set
= ac_build_ballot(ctx
, ctx
->i32_1
);
341 LLVMValueRef vote_set
= ac_build_ballot(ctx
, value
);
342 return LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, vote_set
, active_set
, "");
346 ac_build_vote_any(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
348 LLVMValueRef vote_set
= ac_build_ballot(ctx
, value
);
349 return LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, vote_set
,
350 LLVMConstInt(ctx
->i64
, 0, 0), "");
354 ac_build_vote_eq(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
356 LLVMValueRef active_set
= ac_build_ballot(ctx
, ctx
->i32_1
);
357 LLVMValueRef vote_set
= ac_build_ballot(ctx
, value
);
359 LLVMValueRef all
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
360 vote_set
, active_set
, "");
361 LLVMValueRef none
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
363 LLVMConstInt(ctx
->i64
, 0, 0), "");
364 return LLVMBuildOr(ctx
->builder
, all
, none
, "");
368 ac_build_gather_values_extended(struct ac_llvm_context
*ctx
,
369 LLVMValueRef
*values
,
370 unsigned value_count
,
371 unsigned value_stride
,
375 LLVMBuilderRef builder
= ctx
->builder
;
376 LLVMValueRef vec
= NULL
;
379 if (value_count
== 1 && !always_vector
) {
381 return LLVMBuildLoad(builder
, values
[0], "");
383 } else if (!value_count
)
384 unreachable("value_count is 0");
386 for (i
= 0; i
< value_count
; i
++) {
387 LLVMValueRef value
= values
[i
* value_stride
];
389 value
= LLVMBuildLoad(builder
, value
, "");
392 vec
= LLVMGetUndef( LLVMVectorType(LLVMTypeOf(value
), value_count
));
393 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, i
, false);
394 vec
= LLVMBuildInsertElement(builder
, vec
, value
, index
, "");
400 ac_build_gather_values(struct ac_llvm_context
*ctx
,
401 LLVMValueRef
*values
,
402 unsigned value_count
)
404 return ac_build_gather_values_extended(ctx
, values
, value_count
, 1, false, false);
408 ac_build_fdiv(struct ac_llvm_context
*ctx
,
412 LLVMValueRef ret
= LLVMBuildFDiv(ctx
->builder
, num
, den
, "");
414 if (!LLVMIsConstant(ret
))
415 LLVMSetMetadata(ret
, ctx
->fpmath_md_kind
, ctx
->fpmath_md_2p5_ulp
);
419 /* Coordinates for cube map selection. sc, tc, and ma are as in Table 8.27
420 * of the OpenGL 4.5 (Compatibility Profile) specification, except ma is
421 * already multiplied by two. id is the cube face number.
423 struct cube_selection_coords
{
430 build_cube_intrinsic(struct ac_llvm_context
*ctx
,
432 struct cube_selection_coords
*out
)
434 LLVMTypeRef f32
= ctx
->f32
;
436 out
->stc
[1] = ac_build_intrinsic(ctx
, "llvm.amdgcn.cubetc",
437 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
438 out
->stc
[0] = ac_build_intrinsic(ctx
, "llvm.amdgcn.cubesc",
439 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
440 out
->ma
= ac_build_intrinsic(ctx
, "llvm.amdgcn.cubema",
441 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
442 out
->id
= ac_build_intrinsic(ctx
, "llvm.amdgcn.cubeid",
443 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
447 * Build a manual selection sequence for cube face sc/tc coordinates and
448 * major axis vector (multiplied by 2 for consistency) for the given
449 * vec3 \p coords, for the face implied by \p selcoords.
451 * For the major axis, we always adjust the sign to be in the direction of
452 * selcoords.ma; i.e., a positive out_ma means that coords is pointed towards
453 * the selcoords major axis.
455 static void build_cube_select(struct ac_llvm_context
*ctx
,
456 const struct cube_selection_coords
*selcoords
,
457 const LLVMValueRef
*coords
,
458 LLVMValueRef
*out_st
,
459 LLVMValueRef
*out_ma
)
461 LLVMBuilderRef builder
= ctx
->builder
;
462 LLVMTypeRef f32
= LLVMTypeOf(coords
[0]);
463 LLVMValueRef is_ma_positive
;
465 LLVMValueRef is_ma_z
, is_not_ma_z
;
466 LLVMValueRef is_ma_y
;
467 LLVMValueRef is_ma_x
;
471 is_ma_positive
= LLVMBuildFCmp(builder
, LLVMRealUGE
,
472 selcoords
->ma
, LLVMConstReal(f32
, 0.0), "");
473 sgn_ma
= LLVMBuildSelect(builder
, is_ma_positive
,
474 LLVMConstReal(f32
, 1.0), LLVMConstReal(f32
, -1.0), "");
476 is_ma_z
= LLVMBuildFCmp(builder
, LLVMRealUGE
, selcoords
->id
, LLVMConstReal(f32
, 4.0), "");
477 is_not_ma_z
= LLVMBuildNot(builder
, is_ma_z
, "");
478 is_ma_y
= LLVMBuildAnd(builder
, is_not_ma_z
,
479 LLVMBuildFCmp(builder
, LLVMRealUGE
, selcoords
->id
, LLVMConstReal(f32
, 2.0), ""), "");
480 is_ma_x
= LLVMBuildAnd(builder
, is_not_ma_z
, LLVMBuildNot(builder
, is_ma_y
, ""), "");
483 tmp
= LLVMBuildSelect(builder
, is_ma_x
, coords
[2], coords
[0], "");
484 sgn
= LLVMBuildSelect(builder
, is_ma_y
, LLVMConstReal(f32
, 1.0),
485 LLVMBuildSelect(builder
, is_ma_z
, sgn_ma
,
486 LLVMBuildFNeg(builder
, sgn_ma
, ""), ""), "");
487 out_st
[0] = LLVMBuildFMul(builder
, tmp
, sgn
, "");
490 tmp
= LLVMBuildSelect(builder
, is_ma_y
, coords
[2], coords
[1], "");
491 sgn
= LLVMBuildSelect(builder
, is_ma_y
, sgn_ma
,
492 LLVMConstReal(f32
, -1.0), "");
493 out_st
[1] = LLVMBuildFMul(builder
, tmp
, sgn
, "");
496 tmp
= LLVMBuildSelect(builder
, is_ma_z
, coords
[2],
497 LLVMBuildSelect(builder
, is_ma_y
, coords
[1], coords
[0], ""), "");
498 tmp
= ac_build_intrinsic(ctx
, "llvm.fabs.f32",
499 ctx
->f32
, &tmp
, 1, AC_FUNC_ATTR_READNONE
);
500 *out_ma
= LLVMBuildFMul(builder
, tmp
, LLVMConstReal(f32
, 2.0), "");
504 ac_prepare_cube_coords(struct ac_llvm_context
*ctx
,
505 bool is_deriv
, bool is_array
, bool is_lod
,
506 LLVMValueRef
*coords_arg
,
507 LLVMValueRef
*derivs_arg
)
510 LLVMBuilderRef builder
= ctx
->builder
;
511 struct cube_selection_coords selcoords
;
512 LLVMValueRef coords
[3];
515 if (is_array
&& !is_lod
) {
516 LLVMValueRef tmp
= coords_arg
[3];
517 tmp
= ac_build_intrinsic(ctx
, "llvm.rint.f32", ctx
->f32
, &tmp
, 1, 0);
519 /* Section 8.9 (Texture Functions) of the GLSL 4.50 spec says:
521 * "For Array forms, the array layer used will be
523 * max(0, min(d−1, floor(layer+0.5)))
525 * where d is the depth of the texture array and layer
526 * comes from the component indicated in the tables below.
527 * Workaroudn for an issue where the layer is taken from a
528 * helper invocation which happens to fall on a different
529 * layer due to extrapolation."
531 * VI and earlier attempt to implement this in hardware by
532 * clamping the value of coords[2] = (8 * layer) + face.
533 * Unfortunately, this means that the we end up with the wrong
534 * face when clamping occurs.
536 * Clamp the layer earlier to work around the issue.
538 if (ctx
->chip_class
<= VI
) {
540 ge0
= LLVMBuildFCmp(builder
, LLVMRealOGE
, tmp
, ctx
->f32_0
, "");
541 tmp
= LLVMBuildSelect(builder
, ge0
, tmp
, ctx
->f32_0
, "");
547 build_cube_intrinsic(ctx
, coords_arg
, &selcoords
);
549 invma
= ac_build_intrinsic(ctx
, "llvm.fabs.f32",
550 ctx
->f32
, &selcoords
.ma
, 1, AC_FUNC_ATTR_READNONE
);
551 invma
= ac_build_fdiv(ctx
, LLVMConstReal(ctx
->f32
, 1.0), invma
);
553 for (int i
= 0; i
< 2; ++i
)
554 coords
[i
] = LLVMBuildFMul(builder
, selcoords
.stc
[i
], invma
, "");
556 coords
[2] = selcoords
.id
;
558 if (is_deriv
&& derivs_arg
) {
559 LLVMValueRef derivs
[4];
562 /* Convert cube derivatives to 2D derivatives. */
563 for (axis
= 0; axis
< 2; axis
++) {
564 LLVMValueRef deriv_st
[2];
565 LLVMValueRef deriv_ma
;
567 /* Transform the derivative alongside the texture
568 * coordinate. Mathematically, the correct formula is
569 * as follows. Assume we're projecting onto the +Z face
570 * and denote by dx/dh the derivative of the (original)
571 * X texture coordinate with respect to horizontal
572 * window coordinates. The projection onto the +Z face
577 * Then df/dh = df/dx * dx/dh + df/dz * dz/dh
578 * = 1/z * dx/dh - x/z * 1/z * dz/dh.
580 * This motivatives the implementation below.
582 * Whether this actually gives the expected results for
583 * apps that might feed in derivatives obtained via
584 * finite differences is anyone's guess. The OpenGL spec
585 * seems awfully quiet about how textureGrad for cube
586 * maps should be handled.
588 build_cube_select(ctx
, &selcoords
, &derivs_arg
[axis
* 3],
589 deriv_st
, &deriv_ma
);
591 deriv_ma
= LLVMBuildFMul(builder
, deriv_ma
, invma
, "");
593 for (int i
= 0; i
< 2; ++i
)
594 derivs
[axis
* 2 + i
] =
595 LLVMBuildFSub(builder
,
596 LLVMBuildFMul(builder
, deriv_st
[i
], invma
, ""),
597 LLVMBuildFMul(builder
, deriv_ma
, coords
[i
], ""), "");
600 memcpy(derivs_arg
, derivs
, sizeof(derivs
));
603 /* Shift the texture coordinate. This must be applied after the
604 * derivative calculation.
606 for (int i
= 0; i
< 2; ++i
)
607 coords
[i
] = LLVMBuildFAdd(builder
, coords
[i
], LLVMConstReal(ctx
->f32
, 1.5), "");
610 /* for cube arrays coord.z = coord.w(array_index) * 8 + face */
611 /* coords_arg.w component - array_index for cube arrays */
612 LLVMValueRef tmp
= LLVMBuildFMul(ctx
->builder
, coords_arg
[3], LLVMConstReal(ctx
->f32
, 8.0), "");
613 coords
[2] = LLVMBuildFAdd(ctx
->builder
, tmp
, coords
[2], "");
616 memcpy(coords_arg
, coords
, sizeof(coords
));
621 ac_build_fs_interp(struct ac_llvm_context
*ctx
,
622 LLVMValueRef llvm_chan
,
623 LLVMValueRef attr_number
,
628 LLVMValueRef args
[5];
631 if (HAVE_LLVM
< 0x0400) {
633 ij
[0] = LLVMBuildBitCast(ctx
->builder
, i
, ctx
->i32
, "");
634 ij
[1] = LLVMBuildBitCast(ctx
->builder
, j
, ctx
->i32
, "");
637 args
[1] = attr_number
;
639 args
[3] = ac_build_gather_values(ctx
, ij
, 2);
640 return ac_build_intrinsic(ctx
, "llvm.SI.fs.interp",
642 AC_FUNC_ATTR_READNONE
);
647 args
[2] = attr_number
;
650 p1
= ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p1",
651 ctx
->f32
, args
, 4, AC_FUNC_ATTR_READNONE
);
656 args
[3] = attr_number
;
659 return ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p2",
660 ctx
->f32
, args
, 5, AC_FUNC_ATTR_READNONE
);
664 ac_build_fs_interp_mov(struct ac_llvm_context
*ctx
,
665 LLVMValueRef parameter
,
666 LLVMValueRef llvm_chan
,
667 LLVMValueRef attr_number
,
670 LLVMValueRef args
[4];
671 if (HAVE_LLVM
< 0x0400) {
673 args
[1] = attr_number
;
676 return ac_build_intrinsic(ctx
,
677 "llvm.SI.fs.constant",
679 AC_FUNC_ATTR_READNONE
);
684 args
[2] = attr_number
;
687 return ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.mov",
688 ctx
->f32
, args
, 4, AC_FUNC_ATTR_READNONE
);
692 ac_build_gep0(struct ac_llvm_context
*ctx
,
693 LLVMValueRef base_ptr
,
696 LLVMValueRef indices
[2] = {
697 LLVMConstInt(ctx
->i32
, 0, 0),
700 return LLVMBuildGEP(ctx
->builder
, base_ptr
,
705 ac_build_indexed_store(struct ac_llvm_context
*ctx
,
706 LLVMValueRef base_ptr
, LLVMValueRef index
,
709 LLVMBuildStore(ctx
->builder
, value
,
710 ac_build_gep0(ctx
, base_ptr
, index
));
714 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad.
715 * It's equivalent to doing a load from &base_ptr[index].
717 * \param base_ptr Where the array starts.
718 * \param index The element index into the array.
719 * \param uniform Whether the base_ptr and index can be assumed to be
720 * dynamically uniform
723 ac_build_indexed_load(struct ac_llvm_context
*ctx
,
724 LLVMValueRef base_ptr
, LLVMValueRef index
,
727 LLVMValueRef pointer
;
729 pointer
= ac_build_gep0(ctx
, base_ptr
, index
);
731 LLVMSetMetadata(pointer
, ctx
->uniform_md_kind
, ctx
->empty_md
);
732 return LLVMBuildLoad(ctx
->builder
, pointer
, "");
736 * Do a load from &base_ptr[index], but also add a flag that it's loading
737 * a constant from a dynamically uniform index.
740 ac_build_indexed_load_const(struct ac_llvm_context
*ctx
,
741 LLVMValueRef base_ptr
, LLVMValueRef index
)
743 LLVMValueRef result
= ac_build_indexed_load(ctx
, base_ptr
, index
, true);
744 LLVMSetMetadata(result
, ctx
->invariant_load_md_kind
, ctx
->empty_md
);
748 /* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
749 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
750 * or v4i32 (num_channels=3,4).
753 ac_build_buffer_store_dword(struct ac_llvm_context
*ctx
,
756 unsigned num_channels
,
757 LLVMValueRef voffset
,
758 LLVMValueRef soffset
,
759 unsigned inst_offset
,
762 bool writeonly_memory
,
765 /* TODO: Fix stores with ADD_TID and remove the "has_add_tid" flag. */
767 /* Split 3 channel stores, becase LLVM doesn't support 3-channel
769 if (num_channels
== 3) {
770 LLVMValueRef v
[3], v01
;
772 for (int i
= 0; i
< 3; i
++) {
773 v
[i
] = LLVMBuildExtractElement(ctx
->builder
, vdata
,
774 LLVMConstInt(ctx
->i32
, i
, 0), "");
776 v01
= ac_build_gather_values(ctx
, v
, 2);
778 ac_build_buffer_store_dword(ctx
, rsrc
, v01
, 2, voffset
,
779 soffset
, inst_offset
, glc
, slc
,
780 writeonly_memory
, has_add_tid
);
781 ac_build_buffer_store_dword(ctx
, rsrc
, v
[2], 1, voffset
,
782 soffset
, inst_offset
+ 8,
784 writeonly_memory
, has_add_tid
);
788 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
789 static const char *types
[] = {"f32", "v2f32", "v4f32"};
791 LLVMValueRef offset
= soffset
;
794 offset
= LLVMBuildAdd(ctx
->builder
, offset
,
795 LLVMConstInt(ctx
->i32
, inst_offset
, 0), "");
797 offset
= LLVMBuildAdd(ctx
->builder
, offset
, voffset
, "");
799 LLVMValueRef args
[] = {
800 ac_to_float(ctx
, vdata
),
801 LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, ""),
802 LLVMConstInt(ctx
->i32
, 0, 0),
804 LLVMConstInt(ctx
->i1
, glc
, 0),
805 LLVMConstInt(ctx
->i1
, slc
, 0),
808 snprintf(name
, sizeof(name
), "llvm.amdgcn.buffer.store.%s",
811 ac_build_intrinsic(ctx
, name
, ctx
->voidt
,
812 args
, ARRAY_SIZE(args
),
814 AC_FUNC_ATTR_INACCESSIBLE_MEM_ONLY
:
815 AC_FUNC_ATTR_WRITEONLY
);
819 static unsigned dfmt
[] = {
820 V_008F0C_BUF_DATA_FORMAT_32
,
821 V_008F0C_BUF_DATA_FORMAT_32_32
,
822 V_008F0C_BUF_DATA_FORMAT_32_32_32
,
823 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
825 assert(num_channels
>= 1 && num_channels
<= 4);
827 LLVMValueRef args
[] = {
830 LLVMConstInt(ctx
->i32
, num_channels
, 0),
831 voffset
? voffset
: LLVMGetUndef(ctx
->i32
),
833 LLVMConstInt(ctx
->i32
, inst_offset
, 0),
834 LLVMConstInt(ctx
->i32
, dfmt
[num_channels
- 1], 0),
835 LLVMConstInt(ctx
->i32
, V_008F0C_BUF_NUM_FORMAT_UINT
, 0),
836 LLVMConstInt(ctx
->i32
, voffset
!= NULL
, 0),
837 LLVMConstInt(ctx
->i32
, 0, 0), /* idxen */
838 LLVMConstInt(ctx
->i32
, glc
, 0),
839 LLVMConstInt(ctx
->i32
, slc
, 0),
840 LLVMConstInt(ctx
->i32
, 0, 0), /* tfe*/
843 /* The instruction offset field has 12 bits */
844 assert(voffset
|| inst_offset
< (1 << 12));
846 /* The intrinsic is overloaded, we need to add a type suffix for overloading to work. */
847 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
848 const char *types
[] = {"i32", "v2i32", "v4i32"};
850 snprintf(name
, sizeof(name
), "llvm.SI.tbuffer.store.%s", types
[func
]);
852 ac_build_intrinsic(ctx
, name
, ctx
->voidt
,
853 args
, ARRAY_SIZE(args
),
854 AC_FUNC_ATTR_LEGACY
);
858 ac_build_buffer_load(struct ac_llvm_context
*ctx
,
862 LLVMValueRef voffset
,
863 LLVMValueRef soffset
,
864 unsigned inst_offset
,
870 LLVMValueRef offset
= LLVMConstInt(ctx
->i32
, inst_offset
, 0);
872 offset
= LLVMBuildAdd(ctx
->builder
, offset
, voffset
, "");
874 offset
= LLVMBuildAdd(ctx
->builder
, offset
, soffset
, "");
876 /* TODO: VI and later generations can use SMEM with GLC=1.*/
877 if (allow_smem
&& !glc
&& !slc
) {
878 assert(vindex
== NULL
);
880 LLVMValueRef result
[4];
882 for (int i
= 0; i
< num_channels
; i
++) {
884 offset
= LLVMBuildAdd(ctx
->builder
, offset
,
885 LLVMConstInt(ctx
->i32
, 4, 0), "");
887 LLVMValueRef args
[2] = {rsrc
, offset
};
888 result
[i
] = ac_build_intrinsic(ctx
, "llvm.SI.load.const.v4i32",
890 AC_FUNC_ATTR_READNONE
|
891 AC_FUNC_ATTR_LEGACY
);
893 if (num_channels
== 1)
896 if (num_channels
== 3)
897 result
[num_channels
++] = LLVMGetUndef(ctx
->f32
);
898 return ac_build_gather_values(ctx
, result
, num_channels
);
901 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
903 LLVMValueRef args
[] = {
904 LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, ""),
905 vindex
? vindex
: LLVMConstInt(ctx
->i32
, 0, 0),
907 LLVMConstInt(ctx
->i1
, glc
, 0),
908 LLVMConstInt(ctx
->i1
, slc
, 0)
911 LLVMTypeRef types
[] = {ctx
->f32
, LLVMVectorType(ctx
->f32
, 2),
913 const char *type_names
[] = {"f32", "v2f32", "v4f32"};
916 snprintf(name
, sizeof(name
), "llvm.amdgcn.buffer.load.%s",
919 return ac_build_intrinsic(ctx
, name
, types
[func
], args
,
921 /* READNONE means writes can't affect it, while
922 * READONLY means that writes can affect it. */
923 can_speculate
&& HAVE_LLVM
>= 0x0400 ?
924 AC_FUNC_ATTR_READNONE
:
925 AC_FUNC_ATTR_READONLY
);
928 LLVMValueRef
ac_build_buffer_load_format(struct ac_llvm_context
*ctx
,
931 LLVMValueRef voffset
,
934 LLVMValueRef args
[] = {
935 LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, ""),
938 LLVMConstInt(ctx
->i1
, 0, 0), /* glc */
939 LLVMConstInt(ctx
->i1
, 0, 0), /* slc */
942 return ac_build_intrinsic(ctx
,
943 "llvm.amdgcn.buffer.load.format.v4f32",
944 ctx
->v4f32
, args
, ARRAY_SIZE(args
),
945 /* READNONE means writes can't affect it, while
946 * READONLY means that writes can affect it. */
947 can_speculate
&& HAVE_LLVM
>= 0x0400 ?
948 AC_FUNC_ATTR_READNONE
:
949 AC_FUNC_ATTR_READONLY
);
953 * Set range metadata on an instruction. This can only be used on load and
954 * call instructions. If you know an instruction can only produce the values
955 * 0, 1, 2, you would do set_range_metadata(value, 0, 3);
956 * \p lo is the minimum value inclusive.
957 * \p hi is the maximum value exclusive.
959 static void set_range_metadata(struct ac_llvm_context
*ctx
,
960 LLVMValueRef value
, unsigned lo
, unsigned hi
)
962 LLVMValueRef range_md
, md_args
[2];
963 LLVMTypeRef type
= LLVMTypeOf(value
);
964 LLVMContextRef context
= LLVMGetTypeContext(type
);
966 md_args
[0] = LLVMConstInt(type
, lo
, false);
967 md_args
[1] = LLVMConstInt(type
, hi
, false);
968 range_md
= LLVMMDNodeInContext(context
, md_args
, 2);
969 LLVMSetMetadata(value
, ctx
->range_md_kind
, range_md
);
973 ac_get_thread_id(struct ac_llvm_context
*ctx
)
977 LLVMValueRef tid_args
[2];
978 tid_args
[0] = LLVMConstInt(ctx
->i32
, 0xffffffff, false);
979 tid_args
[1] = LLVMConstInt(ctx
->i32
, 0, false);
980 tid_args
[1] = ac_build_intrinsic(ctx
,
981 "llvm.amdgcn.mbcnt.lo", ctx
->i32
,
982 tid_args
, 2, AC_FUNC_ATTR_READNONE
);
984 tid
= ac_build_intrinsic(ctx
, "llvm.amdgcn.mbcnt.hi",
986 2, AC_FUNC_ATTR_READNONE
);
987 set_range_metadata(ctx
, tid
, 0, 64);
992 * SI implements derivatives using the local data store (LDS)
993 * All writes to the LDS happen in all executing threads at
994 * the same time. TID is the Thread ID for the current
995 * thread and is a value between 0 and 63, representing
996 * the thread's position in the wavefront.
998 * For the pixel shader threads are grouped into quads of four pixels.
999 * The TIDs of the pixels of a quad are:
1007 * So, masking the TID with 0xfffffffc yields the TID of the top left pixel
1008 * of the quad, masking with 0xfffffffd yields the TID of the top pixel of
1009 * the current pixel's column, and masking with 0xfffffffe yields the TID
1010 * of the left pixel of the current pixel's row.
1012 * Adding 1 yields the TID of the pixel to the right of the left pixel, and
1013 * adding 2 yields the TID of the pixel below the top pixel.
1016 ac_build_ddxy(struct ac_llvm_context
*ctx
,
1021 LLVMValueRef tl
, trbl
, args
[2];
1022 LLVMValueRef result
;
1024 if (ctx
->chip_class
>= VI
) {
1025 LLVMValueRef thread_id
, tl_tid
, trbl_tid
;
1026 thread_id
= ac_get_thread_id(ctx
);
1028 tl_tid
= LLVMBuildAnd(ctx
->builder
, thread_id
,
1029 LLVMConstInt(ctx
->i32
, mask
, false), "");
1031 trbl_tid
= LLVMBuildAdd(ctx
->builder
, tl_tid
,
1032 LLVMConstInt(ctx
->i32
, idx
, false), "");
1034 args
[0] = LLVMBuildMul(ctx
->builder
, tl_tid
,
1035 LLVMConstInt(ctx
->i32
, 4, false), "");
1037 tl
= ac_build_intrinsic(ctx
,
1038 "llvm.amdgcn.ds.bpermute", ctx
->i32
,
1040 AC_FUNC_ATTR_READNONE
|
1041 AC_FUNC_ATTR_CONVERGENT
);
1043 args
[0] = LLVMBuildMul(ctx
->builder
, trbl_tid
,
1044 LLVMConstInt(ctx
->i32
, 4, false), "");
1045 trbl
= ac_build_intrinsic(ctx
,
1046 "llvm.amdgcn.ds.bpermute", ctx
->i32
,
1048 AC_FUNC_ATTR_READNONE
|
1049 AC_FUNC_ATTR_CONVERGENT
);
1054 case AC_TID_MASK_TOP_LEFT
:
1062 case AC_TID_MASK_TOP
:
1066 case AC_TID_MASK_LEFT
:
1073 args
[1] = LLVMConstInt(ctx
->i32
, masks
[0], false);
1075 tl
= ac_build_intrinsic(ctx
,
1076 "llvm.amdgcn.ds.swizzle", ctx
->i32
,
1078 AC_FUNC_ATTR_READNONE
|
1079 AC_FUNC_ATTR_CONVERGENT
);
1081 args
[1] = LLVMConstInt(ctx
->i32
, masks
[1], false);
1082 trbl
= ac_build_intrinsic(ctx
,
1083 "llvm.amdgcn.ds.swizzle", ctx
->i32
,
1085 AC_FUNC_ATTR_READNONE
|
1086 AC_FUNC_ATTR_CONVERGENT
);
1089 tl
= LLVMBuildBitCast(ctx
->builder
, tl
, ctx
->f32
, "");
1090 trbl
= LLVMBuildBitCast(ctx
->builder
, trbl
, ctx
->f32
, "");
1091 result
= LLVMBuildFSub(ctx
->builder
, trbl
, tl
, "");
1096 ac_build_sendmsg(struct ac_llvm_context
*ctx
,
1098 LLVMValueRef wave_id
)
1100 LLVMValueRef args
[2];
1101 const char *intr_name
= (HAVE_LLVM
< 0x0400) ? "llvm.SI.sendmsg" : "llvm.amdgcn.s.sendmsg";
1102 args
[0] = LLVMConstInt(ctx
->i32
, msg
, false);
1104 ac_build_intrinsic(ctx
, intr_name
, ctx
->voidt
, args
, 2, 0);
1108 ac_build_imsb(struct ac_llvm_context
*ctx
,
1110 LLVMTypeRef dst_type
)
1112 const char *intr_name
= (HAVE_LLVM
< 0x0400) ? "llvm.AMDGPU.flbit.i32" :
1113 "llvm.amdgcn.sffbh.i32";
1114 LLVMValueRef msb
= ac_build_intrinsic(ctx
, intr_name
,
1116 AC_FUNC_ATTR_READNONE
);
1118 /* The HW returns the last bit index from MSB, but NIR/TGSI wants
1119 * the index from LSB. Invert it by doing "31 - msb". */
1120 msb
= LLVMBuildSub(ctx
->builder
, LLVMConstInt(ctx
->i32
, 31, false),
1123 LLVMValueRef all_ones
= LLVMConstInt(ctx
->i32
, -1, true);
1124 LLVMValueRef cond
= LLVMBuildOr(ctx
->builder
,
1125 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
1126 arg
, LLVMConstInt(ctx
->i32
, 0, 0), ""),
1127 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
1128 arg
, all_ones
, ""), "");
1130 return LLVMBuildSelect(ctx
->builder
, cond
, all_ones
, msb
, "");
1134 ac_build_umsb(struct ac_llvm_context
*ctx
,
1136 LLVMTypeRef dst_type
)
1138 LLVMValueRef args
[2] = {
1140 LLVMConstInt(ctx
->i1
, 1, 0),
1142 LLVMValueRef msb
= ac_build_intrinsic(ctx
, "llvm.ctlz.i32",
1143 dst_type
, args
, ARRAY_SIZE(args
),
1144 AC_FUNC_ATTR_READNONE
);
1146 /* The HW returns the last bit index from MSB, but TGSI/NIR wants
1147 * the index from LSB. Invert it by doing "31 - msb". */
1148 msb
= LLVMBuildSub(ctx
->builder
, LLVMConstInt(ctx
->i32
, 31, false),
1151 /* check for zero */
1152 return LLVMBuildSelect(ctx
->builder
,
1153 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, arg
,
1154 LLVMConstInt(ctx
->i32
, 0, 0), ""),
1155 LLVMConstInt(ctx
->i32
, -1, true), msb
, "");
1158 LLVMValueRef
ac_build_umin(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
1161 LLVMValueRef cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntULE
, a
, b
, "");
1162 return LLVMBuildSelect(ctx
->builder
, cmp
, a
, b
, "");
1165 LLVMValueRef
ac_build_clamp(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
1167 if (HAVE_LLVM
>= 0x0500) {
1168 LLVMValueRef max
[2] = {
1170 LLVMConstReal(ctx
->f32
, 0),
1172 LLVMValueRef min
[2] = {
1173 LLVMConstReal(ctx
->f32
, 1),
1176 min
[1] = ac_build_intrinsic(ctx
, "llvm.maxnum.f32",
1178 AC_FUNC_ATTR_READNONE
);
1179 return ac_build_intrinsic(ctx
, "llvm.minnum.f32",
1181 AC_FUNC_ATTR_READNONE
);
1184 LLVMValueRef args
[3] = {
1186 LLVMConstReal(ctx
->f32
, 0),
1187 LLVMConstReal(ctx
->f32
, 1),
1190 return ac_build_intrinsic(ctx
, "llvm.AMDGPU.clamp.", ctx
->f32
, args
, 3,
1191 AC_FUNC_ATTR_READNONE
|
1192 AC_FUNC_ATTR_LEGACY
);
1195 void ac_build_export(struct ac_llvm_context
*ctx
, struct ac_export_args
*a
)
1197 LLVMValueRef args
[9];
1199 if (HAVE_LLVM
>= 0x0500) {
1200 args
[0] = LLVMConstInt(ctx
->i32
, a
->target
, 0);
1201 args
[1] = LLVMConstInt(ctx
->i32
, a
->enabled_channels
, 0);
1204 LLVMTypeRef i16
= LLVMInt16TypeInContext(ctx
->context
);
1205 LLVMTypeRef v2i16
= LLVMVectorType(i16
, 2);
1207 args
[2] = LLVMBuildBitCast(ctx
->builder
, a
->out
[0],
1209 args
[3] = LLVMBuildBitCast(ctx
->builder
, a
->out
[1],
1211 args
[4] = LLVMConstInt(ctx
->i1
, a
->done
, 0);
1212 args
[5] = LLVMConstInt(ctx
->i1
, a
->valid_mask
, 0);
1214 ac_build_intrinsic(ctx
, "llvm.amdgcn.exp.compr.v2i16",
1215 ctx
->voidt
, args
, 6, 0);
1217 args
[2] = a
->out
[0];
1218 args
[3] = a
->out
[1];
1219 args
[4] = a
->out
[2];
1220 args
[5] = a
->out
[3];
1221 args
[6] = LLVMConstInt(ctx
->i1
, a
->done
, 0);
1222 args
[7] = LLVMConstInt(ctx
->i1
, a
->valid_mask
, 0);
1224 ac_build_intrinsic(ctx
, "llvm.amdgcn.exp.f32",
1225 ctx
->voidt
, args
, 8, 0);
1230 args
[0] = LLVMConstInt(ctx
->i32
, a
->enabled_channels
, 0);
1231 args
[1] = LLVMConstInt(ctx
->i32
, a
->valid_mask
, 0);
1232 args
[2] = LLVMConstInt(ctx
->i32
, a
->done
, 0);
1233 args
[3] = LLVMConstInt(ctx
->i32
, a
->target
, 0);
1234 args
[4] = LLVMConstInt(ctx
->i32
, a
->compr
, 0);
1235 memcpy(args
+ 5, a
->out
, sizeof(a
->out
[0]) * 4);
1237 ac_build_intrinsic(ctx
, "llvm.SI.export", ctx
->voidt
, args
, 9,
1238 AC_FUNC_ATTR_LEGACY
);
1241 LLVMValueRef
ac_build_image_opcode(struct ac_llvm_context
*ctx
,
1242 struct ac_image_args
*a
)
1244 LLVMTypeRef dst_type
;
1245 LLVMValueRef args
[11];
1246 unsigned num_args
= 0;
1248 char intr_name
[128], type
[64];
1250 if (HAVE_LLVM
>= 0x0400) {
1251 bool sample
= a
->opcode
== ac_image_sample
||
1252 a
->opcode
== ac_image_gather4
||
1253 a
->opcode
== ac_image_get_lod
;
1256 args
[num_args
++] = ac_to_float(ctx
, a
->addr
);
1258 args
[num_args
++] = a
->addr
;
1260 args
[num_args
++] = a
->resource
;
1262 args
[num_args
++] = a
->sampler
;
1263 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->dmask
, 0);
1265 args
[num_args
++] = LLVMConstInt(ctx
->i1
, a
->unorm
, 0);
1266 args
[num_args
++] = LLVMConstInt(ctx
->i1
, 0, 0); /* glc */
1267 args
[num_args
++] = LLVMConstInt(ctx
->i1
, 0, 0); /* slc */
1268 args
[num_args
++] = LLVMConstInt(ctx
->i1
, 0, 0); /* lwe */
1269 args
[num_args
++] = LLVMConstInt(ctx
->i1
, a
->da
, 0);
1271 switch (a
->opcode
) {
1272 case ac_image_sample
:
1273 name
= "llvm.amdgcn.image.sample";
1275 case ac_image_gather4
:
1276 name
= "llvm.amdgcn.image.gather4";
1279 name
= "llvm.amdgcn.image.load";
1281 case ac_image_load_mip
:
1282 name
= "llvm.amdgcn.image.load.mip";
1284 case ac_image_get_lod
:
1285 name
= "llvm.amdgcn.image.getlod";
1287 case ac_image_get_resinfo
:
1288 name
= "llvm.amdgcn.image.getresinfo";
1291 unreachable("invalid image opcode");
1294 ac_build_type_name_for_intr(LLVMTypeOf(args
[0]), type
,
1297 snprintf(intr_name
, sizeof(intr_name
), "%s%s%s%s.v4f32.%s.v8i32",
1299 a
->compare
? ".c" : "",
1303 a
->level_zero
? ".lz" : "",
1304 a
->offset
? ".o" : "",
1307 LLVMValueRef result
=
1308 ac_build_intrinsic(ctx
, intr_name
,
1309 ctx
->v4f32
, args
, num_args
,
1310 AC_FUNC_ATTR_READNONE
);
1312 result
= LLVMBuildBitCast(ctx
->builder
, result
,
1318 args
[num_args
++] = a
->addr
;
1319 args
[num_args
++] = a
->resource
;
1321 if (a
->opcode
== ac_image_load
||
1322 a
->opcode
== ac_image_load_mip
||
1323 a
->opcode
== ac_image_get_resinfo
) {
1324 dst_type
= ctx
->v4i32
;
1326 dst_type
= ctx
->v4f32
;
1327 args
[num_args
++] = a
->sampler
;
1330 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->dmask
, 0);
1331 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->unorm
, 0);
1332 args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* r128 */
1333 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->da
, 0);
1334 args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* glc */
1335 args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* slc */
1336 args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* tfe */
1337 args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* lwe */
1339 switch (a
->opcode
) {
1340 case ac_image_sample
:
1341 name
= "llvm.SI.image.sample";
1343 case ac_image_gather4
:
1344 name
= "llvm.SI.gather4";
1347 name
= "llvm.SI.image.load";
1349 case ac_image_load_mip
:
1350 name
= "llvm.SI.image.load.mip";
1352 case ac_image_get_lod
:
1353 name
= "llvm.SI.getlod";
1355 case ac_image_get_resinfo
:
1356 name
= "llvm.SI.getresinfo";
1360 ac_build_type_name_for_intr(LLVMTypeOf(a
->addr
), type
, sizeof(type
));
1361 snprintf(intr_name
, sizeof(intr_name
), "%s%s%s%s.%s",
1363 a
->compare
? ".c" : "",
1367 a
->level_zero
? ".lz" : "",
1368 a
->offset
? ".o" : "",
1371 return ac_build_intrinsic(ctx
, intr_name
,
1372 dst_type
, args
, num_args
,
1373 AC_FUNC_ATTR_READNONE
|
1374 AC_FUNC_ATTR_LEGACY
);
1377 LLVMValueRef
ac_build_cvt_pkrtz_f16(struct ac_llvm_context
*ctx
,
1378 LLVMValueRef args
[2])
1380 if (HAVE_LLVM
>= 0x0500) {
1382 LLVMVectorType(LLVMHalfTypeInContext(ctx
->context
), 2);
1384 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pkrtz",
1386 AC_FUNC_ATTR_READNONE
);
1387 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
1390 return ac_build_intrinsic(ctx
, "llvm.SI.packf16", ctx
->i32
, args
, 2,
1391 AC_FUNC_ATTR_READNONE
|
1392 AC_FUNC_ATTR_LEGACY
);
1396 * KILL, AKA discard in GLSL.
1398 * \param value kill if value < 0.0 or value == NULL.
1400 void ac_build_kill(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
1403 ac_build_intrinsic(ctx
, "llvm.AMDGPU.kill", ctx
->voidt
,
1404 &value
, 1, AC_FUNC_ATTR_LEGACY
);
1406 ac_build_intrinsic(ctx
, "llvm.AMDGPU.kilp", ctx
->voidt
,
1407 NULL
, 0, AC_FUNC_ATTR_LEGACY
);
1411 LLVMValueRef
ac_build_bfe(struct ac_llvm_context
*ctx
, LLVMValueRef input
,
1412 LLVMValueRef offset
, LLVMValueRef width
,
1415 LLVMValueRef args
[] = {
1421 if (HAVE_LLVM
>= 0x0500) {
1422 return ac_build_intrinsic(ctx
,
1423 is_signed
? "llvm.amdgcn.sbfe.i32" :
1424 "llvm.amdgcn.ubfe.i32",
1426 AC_FUNC_ATTR_READNONE
);
1429 return ac_build_intrinsic(ctx
,
1430 is_signed
? "llvm.AMDGPU.bfe.i32" :
1431 "llvm.AMDGPU.bfe.u32",
1433 AC_FUNC_ATTR_READNONE
|
1434 AC_FUNC_ATTR_LEGACY
);
1437 void ac_get_image_intr_name(const char *base_name
,
1438 LLVMTypeRef data_type
,
1439 LLVMTypeRef coords_type
,
1440 LLVMTypeRef rsrc_type
,
1441 char *out_name
, unsigned out_len
)
1443 char coords_type_name
[8];
1445 ac_build_type_name_for_intr(coords_type
, coords_type_name
,
1446 sizeof(coords_type_name
));
1448 if (HAVE_LLVM
<= 0x0309) {
1449 snprintf(out_name
, out_len
, "%s.%s", base_name
, coords_type_name
);
1451 char data_type_name
[8];
1452 char rsrc_type_name
[8];
1454 ac_build_type_name_for_intr(data_type
, data_type_name
,
1455 sizeof(data_type_name
));
1456 ac_build_type_name_for_intr(rsrc_type
, rsrc_type_name
,
1457 sizeof(rsrc_type_name
));
1458 snprintf(out_name
, out_len
, "%s.%s.%s.%s", base_name
,
1459 data_type_name
, coords_type_name
, rsrc_type_name
);
1463 #define AC_EXP_TARGET (HAVE_LLVM >= 0x0500 ? 0 : 3)
1464 #define AC_EXP_OUT0 (HAVE_LLVM >= 0x0500 ? 2 : 5)
1472 struct ac_vs_exp_chan
1476 enum ac_ir_type type
;
1479 struct ac_vs_exp_inst
{
1482 struct ac_vs_exp_chan chan
[4];
1485 struct ac_vs_exports
{
1487 struct ac_vs_exp_inst exp
[VARYING_SLOT_MAX
];
1490 /* Return true if the PARAM export has been eliminated. */
1491 static bool ac_eliminate_const_output(uint8_t *vs_output_param_offset
,
1492 uint32_t num_outputs
,
1493 struct ac_vs_exp_inst
*exp
)
1495 unsigned i
, default_val
; /* SPI_PS_INPUT_CNTL_i.DEFAULT_VAL */
1496 bool is_zero
[4] = {}, is_one
[4] = {};
1498 for (i
= 0; i
< 4; i
++) {
1499 /* It's a constant expression. Undef outputs are eliminated too. */
1500 if (exp
->chan
[i
].type
== AC_IR_UNDEF
) {
1503 } else if (exp
->chan
[i
].type
== AC_IR_CONST
) {
1504 if (exp
->chan
[i
].const_float
== 0)
1506 else if (exp
->chan
[i
].const_float
== 1)
1509 return false; /* other constant */
1514 /* Only certain combinations of 0 and 1 can be eliminated. */
1515 if (is_zero
[0] && is_zero
[1] && is_zero
[2])
1516 default_val
= is_zero
[3] ? 0 : 1;
1517 else if (is_one
[0] && is_one
[1] && is_one
[2])
1518 default_val
= is_zero
[3] ? 2 : 3;
1522 /* The PARAM export can be represented as DEFAULT_VAL. Kill it. */
1523 LLVMInstructionEraseFromParent(exp
->inst
);
1525 /* Change OFFSET to DEFAULT_VAL. */
1526 for (i
= 0; i
< num_outputs
; i
++) {
1527 if (vs_output_param_offset
[i
] == exp
->offset
) {
1528 vs_output_param_offset
[i
] =
1529 AC_EXP_PARAM_DEFAULT_VAL_0000
+ default_val
;
1536 static bool ac_eliminate_duplicated_output(uint8_t *vs_output_param_offset
,
1537 uint32_t num_outputs
,
1538 struct ac_vs_exports
*processed
,
1539 struct ac_vs_exp_inst
*exp
)
1541 unsigned p
, copy_back_channels
= 0;
1543 /* See if the output is already in the list of processed outputs.
1544 * The LLVMValueRef comparison relies on SSA.
1546 for (p
= 0; p
< processed
->num
; p
++) {
1547 bool different
= false;
1549 for (unsigned j
= 0; j
< 4; j
++) {
1550 struct ac_vs_exp_chan
*c1
= &processed
->exp
[p
].chan
[j
];
1551 struct ac_vs_exp_chan
*c2
= &exp
->chan
[j
];
1553 /* Treat undef as a match. */
1554 if (c2
->type
== AC_IR_UNDEF
)
1557 /* If c1 is undef but c2 isn't, we can copy c2 to c1
1558 * and consider the instruction duplicated.
1560 if (c1
->type
== AC_IR_UNDEF
) {
1561 copy_back_channels
|= 1 << j
;
1565 /* Test whether the channels are not equal. */
1566 if (c1
->type
!= c2
->type
||
1567 (c1
->type
== AC_IR_CONST
&&
1568 c1
->const_float
!= c2
->const_float
) ||
1569 (c1
->type
== AC_IR_VALUE
&&
1570 c1
->value
!= c2
->value
)) {
1578 copy_back_channels
= 0;
1580 if (p
== processed
->num
)
1583 /* If a match was found, but the matching export has undef where the new
1584 * one has a normal value, copy the normal value to the undef channel.
1586 struct ac_vs_exp_inst
*match
= &processed
->exp
[p
];
1588 while (copy_back_channels
) {
1589 unsigned chan
= u_bit_scan(©_back_channels
);
1591 assert(match
->chan
[chan
].type
== AC_IR_UNDEF
);
1592 LLVMSetOperand(match
->inst
, AC_EXP_OUT0
+ chan
,
1593 exp
->chan
[chan
].value
);
1594 match
->chan
[chan
] = exp
->chan
[chan
];
1597 /* The PARAM export is duplicated. Kill it. */
1598 LLVMInstructionEraseFromParent(exp
->inst
);
1600 /* Change OFFSET to the matching export. */
1601 for (unsigned i
= 0; i
< num_outputs
; i
++) {
1602 if (vs_output_param_offset
[i
] == exp
->offset
) {
1603 vs_output_param_offset
[i
] = match
->offset
;
1610 void ac_optimize_vs_outputs(struct ac_llvm_context
*ctx
,
1611 LLVMValueRef main_fn
,
1612 uint8_t *vs_output_param_offset
,
1613 uint32_t num_outputs
,
1614 uint8_t *num_param_exports
)
1616 LLVMBasicBlockRef bb
;
1617 bool removed_any
= false;
1618 struct ac_vs_exports exports
;
1622 /* Process all LLVM instructions. */
1623 bb
= LLVMGetFirstBasicBlock(main_fn
);
1625 LLVMValueRef inst
= LLVMGetFirstInstruction(bb
);
1628 LLVMValueRef cur
= inst
;
1629 inst
= LLVMGetNextInstruction(inst
);
1630 struct ac_vs_exp_inst exp
;
1632 if (LLVMGetInstructionOpcode(cur
) != LLVMCall
)
1635 LLVMValueRef callee
= ac_llvm_get_called_value(cur
);
1637 if (!ac_llvm_is_function(callee
))
1640 const char *name
= LLVMGetValueName(callee
);
1641 unsigned num_args
= LLVMCountParams(callee
);
1643 /* Check if this is an export instruction. */
1644 if ((num_args
!= 9 && num_args
!= 8) ||
1645 (strcmp(name
, "llvm.SI.export") &&
1646 strcmp(name
, "llvm.amdgcn.exp.f32")))
1649 LLVMValueRef arg
= LLVMGetOperand(cur
, AC_EXP_TARGET
);
1650 unsigned target
= LLVMConstIntGetZExtValue(arg
);
1652 if (target
< V_008DFC_SQ_EXP_PARAM
)
1655 target
-= V_008DFC_SQ_EXP_PARAM
;
1657 /* Parse the instruction. */
1658 memset(&exp
, 0, sizeof(exp
));
1659 exp
.offset
= target
;
1662 for (unsigned i
= 0; i
< 4; i
++) {
1663 LLVMValueRef v
= LLVMGetOperand(cur
, AC_EXP_OUT0
+ i
);
1665 exp
.chan
[i
].value
= v
;
1667 if (LLVMIsUndef(v
)) {
1668 exp
.chan
[i
].type
= AC_IR_UNDEF
;
1669 } else if (LLVMIsAConstantFP(v
)) {
1670 LLVMBool loses_info
;
1671 exp
.chan
[i
].type
= AC_IR_CONST
;
1672 exp
.chan
[i
].const_float
=
1673 LLVMConstRealGetDouble(v
, &loses_info
);
1675 exp
.chan
[i
].type
= AC_IR_VALUE
;
1679 /* Eliminate constant and duplicated PARAM exports. */
1680 if (ac_eliminate_const_output(vs_output_param_offset
,
1681 num_outputs
, &exp
) ||
1682 ac_eliminate_duplicated_output(vs_output_param_offset
,
1683 num_outputs
, &exports
,
1687 exports
.exp
[exports
.num
++] = exp
;
1690 bb
= LLVMGetNextBasicBlock(bb
);
1693 /* Remove holes in export memory due to removed PARAM exports.
1694 * This is done by renumbering all PARAM exports.
1697 uint8_t old_offset
[VARYING_SLOT_MAX
];
1700 /* Make a copy of the offsets. We need the old version while
1701 * we are modifying some of them. */
1702 memcpy(old_offset
, vs_output_param_offset
,
1703 sizeof(old_offset
));
1705 for (i
= 0; i
< exports
.num
; i
++) {
1706 unsigned offset
= exports
.exp
[i
].offset
;
1708 /* Update vs_output_param_offset. Multiple outputs can
1709 * have the same offset.
1711 for (out
= 0; out
< num_outputs
; out
++) {
1712 if (old_offset
[out
] == offset
)
1713 vs_output_param_offset
[out
] = i
;
1716 /* Change the PARAM offset in the instruction. */
1717 LLVMSetOperand(exports
.exp
[i
].inst
, AC_EXP_TARGET
,
1718 LLVMConstInt(ctx
->i32
,
1719 V_008DFC_SQ_EXP_PARAM
+ i
, 0));
1721 *num_param_exports
= exports
.num
;