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
, enum radeon_family family
)
54 ctx
->chip_class
= chip_class
;
57 ctx
->context
= context
;
61 ctx
->voidt
= LLVMVoidTypeInContext(ctx
->context
);
62 ctx
->i1
= LLVMInt1TypeInContext(ctx
->context
);
63 ctx
->i8
= LLVMInt8TypeInContext(ctx
->context
);
64 ctx
->i16
= LLVMIntTypeInContext(ctx
->context
, 16);
65 ctx
->i32
= LLVMIntTypeInContext(ctx
->context
, 32);
66 ctx
->i64
= LLVMIntTypeInContext(ctx
->context
, 64);
67 ctx
->f16
= LLVMHalfTypeInContext(ctx
->context
);
68 ctx
->f32
= LLVMFloatTypeInContext(ctx
->context
);
69 ctx
->f64
= LLVMDoubleTypeInContext(ctx
->context
);
70 ctx
->v2i32
= LLVMVectorType(ctx
->i32
, 2);
71 ctx
->v3i32
= LLVMVectorType(ctx
->i32
, 3);
72 ctx
->v4i32
= LLVMVectorType(ctx
->i32
, 4);
73 ctx
->v2f32
= LLVMVectorType(ctx
->f32
, 2);
74 ctx
->v4f32
= LLVMVectorType(ctx
->f32
, 4);
75 ctx
->v8i32
= LLVMVectorType(ctx
->i32
, 8);
77 ctx
->i32_0
= LLVMConstInt(ctx
->i32
, 0, false);
78 ctx
->i32_1
= LLVMConstInt(ctx
->i32
, 1, false);
79 ctx
->f32_0
= LLVMConstReal(ctx
->f32
, 0.0);
80 ctx
->f32_1
= LLVMConstReal(ctx
->f32
, 1.0);
81 ctx
->f64_1
= LLVMConstReal(ctx
->f64
, 1.0);
83 ctx
->i1false
= LLVMConstInt(ctx
->i1
, 0, false);
84 ctx
->i1true
= LLVMConstInt(ctx
->i1
, 1, false);
86 ctx
->range_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
89 ctx
->invariant_load_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
90 "invariant.load", 14);
92 ctx
->fpmath_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
, "fpmath", 6);
94 args
[0] = LLVMConstReal(ctx
->f32
, 2.5);
95 ctx
->fpmath_md_2p5_ulp
= LLVMMDNodeInContext(ctx
->context
, args
, 1);
97 ctx
->uniform_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
98 "amdgpu.uniform", 14);
100 ctx
->empty_md
= LLVMMDNodeInContext(ctx
->context
, NULL
, 0);
104 ac_get_llvm_num_components(LLVMValueRef value
)
106 LLVMTypeRef type
= LLVMTypeOf(value
);
107 unsigned num_components
= LLVMGetTypeKind(type
) == LLVMVectorTypeKind
108 ? LLVMGetVectorSize(type
)
110 return num_components
;
114 ac_llvm_extract_elem(struct ac_llvm_context
*ac
,
118 if (LLVMGetTypeKind(LLVMTypeOf(value
)) != LLVMVectorTypeKind
) {
123 return LLVMBuildExtractElement(ac
->builder
, value
,
124 LLVMConstInt(ac
->i32
, index
, false), "");
128 ac_get_type_size(LLVMTypeRef type
)
130 LLVMTypeKind kind
= LLVMGetTypeKind(type
);
133 case LLVMIntegerTypeKind
:
134 return LLVMGetIntTypeWidth(type
) / 8;
135 case LLVMFloatTypeKind
:
137 case LLVMDoubleTypeKind
:
138 case LLVMPointerTypeKind
:
140 case LLVMVectorTypeKind
:
141 return LLVMGetVectorSize(type
) *
142 ac_get_type_size(LLVMGetElementType(type
));
143 case LLVMArrayTypeKind
:
144 return LLVMGetArrayLength(type
) *
145 ac_get_type_size(LLVMGetElementType(type
));
152 static LLVMTypeRef
to_integer_type_scalar(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
154 if (t
== ctx
->f16
|| t
== ctx
->i16
)
156 else if (t
== ctx
->f32
|| t
== ctx
->i32
)
158 else if (t
== ctx
->f64
|| t
== ctx
->i64
)
161 unreachable("Unhandled integer size");
165 ac_to_integer_type(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
167 if (LLVMGetTypeKind(t
) == LLVMVectorTypeKind
) {
168 LLVMTypeRef elem_type
= LLVMGetElementType(t
);
169 return LLVMVectorType(to_integer_type_scalar(ctx
, elem_type
),
170 LLVMGetVectorSize(t
));
172 return to_integer_type_scalar(ctx
, t
);
176 ac_to_integer(struct ac_llvm_context
*ctx
, LLVMValueRef v
)
178 LLVMTypeRef type
= LLVMTypeOf(v
);
179 return LLVMBuildBitCast(ctx
->builder
, v
, ac_to_integer_type(ctx
, type
), "");
182 static LLVMTypeRef
to_float_type_scalar(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
184 if (t
== ctx
->i16
|| t
== ctx
->f16
)
186 else if (t
== ctx
->i32
|| t
== ctx
->f32
)
188 else if (t
== ctx
->i64
|| t
== ctx
->f64
)
191 unreachable("Unhandled float size");
195 ac_to_float_type(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
197 if (LLVMGetTypeKind(t
) == LLVMVectorTypeKind
) {
198 LLVMTypeRef elem_type
= LLVMGetElementType(t
);
199 return LLVMVectorType(to_float_type_scalar(ctx
, elem_type
),
200 LLVMGetVectorSize(t
));
202 return to_float_type_scalar(ctx
, t
);
206 ac_to_float(struct ac_llvm_context
*ctx
, LLVMValueRef v
)
208 LLVMTypeRef type
= LLVMTypeOf(v
);
209 return LLVMBuildBitCast(ctx
->builder
, v
, ac_to_float_type(ctx
, type
), "");
214 ac_build_intrinsic(struct ac_llvm_context
*ctx
, const char *name
,
215 LLVMTypeRef return_type
, LLVMValueRef
*params
,
216 unsigned param_count
, unsigned attrib_mask
)
218 LLVMValueRef function
, call
;
219 bool set_callsite_attrs
= HAVE_LLVM
>= 0x0400 &&
220 !(attrib_mask
& AC_FUNC_ATTR_LEGACY
);
222 function
= LLVMGetNamedFunction(ctx
->module
, name
);
224 LLVMTypeRef param_types
[32], function_type
;
227 assert(param_count
<= 32);
229 for (i
= 0; i
< param_count
; ++i
) {
231 param_types
[i
] = LLVMTypeOf(params
[i
]);
234 LLVMFunctionType(return_type
, param_types
, param_count
, 0);
235 function
= LLVMAddFunction(ctx
->module
, name
, function_type
);
237 LLVMSetFunctionCallConv(function
, LLVMCCallConv
);
238 LLVMSetLinkage(function
, LLVMExternalLinkage
);
240 if (!set_callsite_attrs
)
241 ac_add_func_attributes(ctx
->context
, function
, attrib_mask
);
244 call
= LLVMBuildCall(ctx
->builder
, function
, params
, param_count
, "");
245 if (set_callsite_attrs
)
246 ac_add_func_attributes(ctx
->context
, call
, attrib_mask
);
251 * Given the i32 or vNi32 \p type, generate the textual name (e.g. for use with
254 void ac_build_type_name_for_intr(LLVMTypeRef type
, char *buf
, unsigned bufsize
)
256 LLVMTypeRef elem_type
= type
;
258 assert(bufsize
>= 8);
260 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
) {
261 int ret
= snprintf(buf
, bufsize
, "v%u",
262 LLVMGetVectorSize(type
));
264 char *type_name
= LLVMPrintTypeToString(type
);
265 fprintf(stderr
, "Error building type name for: %s\n",
269 elem_type
= LLVMGetElementType(type
);
273 switch (LLVMGetTypeKind(elem_type
)) {
275 case LLVMIntegerTypeKind
:
276 snprintf(buf
, bufsize
, "i%d", LLVMGetIntTypeWidth(elem_type
));
278 case LLVMFloatTypeKind
:
279 snprintf(buf
, bufsize
, "f32");
281 case LLVMDoubleTypeKind
:
282 snprintf(buf
, bufsize
, "f64");
288 * Helper function that builds an LLVM IR PHI node and immediately adds
292 ac_build_phi(struct ac_llvm_context
*ctx
, LLVMTypeRef type
,
293 unsigned count_incoming
, LLVMValueRef
*values
,
294 LLVMBasicBlockRef
*blocks
)
296 LLVMValueRef phi
= LLVMBuildPhi(ctx
->builder
, type
, "");
297 LLVMAddIncoming(phi
, values
, blocks
, count_incoming
);
301 /* Prevent optimizations (at least of memory accesses) across the current
302 * point in the program by emitting empty inline assembly that is marked as
303 * having side effects.
305 * Optionally, a value can be passed through the inline assembly to prevent
306 * LLVM from hoisting calls to ReadNone functions.
309 ac_build_optimization_barrier(struct ac_llvm_context
*ctx
,
312 static int counter
= 0;
314 LLVMBuilderRef builder
= ctx
->builder
;
317 snprintf(code
, sizeof(code
), "; %d", p_atomic_inc_return(&counter
));
320 LLVMTypeRef ftype
= LLVMFunctionType(ctx
->voidt
, NULL
, 0, false);
321 LLVMValueRef inlineasm
= LLVMConstInlineAsm(ftype
, code
, "", true, false);
322 LLVMBuildCall(builder
, inlineasm
, NULL
, 0, "");
324 LLVMTypeRef ftype
= LLVMFunctionType(ctx
->i32
, &ctx
->i32
, 1, false);
325 LLVMValueRef inlineasm
= LLVMConstInlineAsm(ftype
, code
, "=v,0", true, false);
326 LLVMValueRef vgpr
= *pvgpr
;
327 LLVMTypeRef vgpr_type
= LLVMTypeOf(vgpr
);
328 unsigned vgpr_size
= ac_get_type_size(vgpr_type
);
331 assert(vgpr_size
% 4 == 0);
333 vgpr
= LLVMBuildBitCast(builder
, vgpr
, LLVMVectorType(ctx
->i32
, vgpr_size
/ 4), "");
334 vgpr0
= LLVMBuildExtractElement(builder
, vgpr
, ctx
->i32_0
, "");
335 vgpr0
= LLVMBuildCall(builder
, inlineasm
, &vgpr0
, 1, "");
336 vgpr
= LLVMBuildInsertElement(builder
, vgpr
, vgpr0
, ctx
->i32_0
, "");
337 vgpr
= LLVMBuildBitCast(builder
, vgpr
, vgpr_type
, "");
344 ac_build_ballot(struct ac_llvm_context
*ctx
,
347 LLVMValueRef args
[3] = {
350 LLVMConstInt(ctx
->i32
, LLVMIntNE
, 0)
353 /* We currently have no other way to prevent LLVM from lifting the icmp
354 * calls to a dominating basic block.
356 ac_build_optimization_barrier(ctx
, &args
[0]);
358 if (LLVMTypeOf(args
[0]) != ctx
->i32
)
359 args
[0] = LLVMBuildBitCast(ctx
->builder
, args
[0], ctx
->i32
, "");
361 return ac_build_intrinsic(ctx
,
362 "llvm.amdgcn.icmp.i32",
364 AC_FUNC_ATTR_NOUNWIND
|
365 AC_FUNC_ATTR_READNONE
|
366 AC_FUNC_ATTR_CONVERGENT
);
370 ac_build_vote_all(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
372 LLVMValueRef active_set
= ac_build_ballot(ctx
, ctx
->i32_1
);
373 LLVMValueRef vote_set
= ac_build_ballot(ctx
, value
);
374 return LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, vote_set
, active_set
, "");
378 ac_build_vote_any(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
380 LLVMValueRef vote_set
= ac_build_ballot(ctx
, value
);
381 return LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, vote_set
,
382 LLVMConstInt(ctx
->i64
, 0, 0), "");
386 ac_build_vote_eq(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
388 LLVMValueRef active_set
= ac_build_ballot(ctx
, ctx
->i32_1
);
389 LLVMValueRef vote_set
= ac_build_ballot(ctx
, value
);
391 LLVMValueRef all
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
392 vote_set
, active_set
, "");
393 LLVMValueRef none
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
395 LLVMConstInt(ctx
->i64
, 0, 0), "");
396 return LLVMBuildOr(ctx
->builder
, all
, none
, "");
400 ac_build_varying_gather_values(struct ac_llvm_context
*ctx
, LLVMValueRef
*values
,
401 unsigned value_count
, unsigned component
)
403 LLVMValueRef vec
= NULL
;
405 if (value_count
== 1) {
406 return values
[component
];
407 } else if (!value_count
)
408 unreachable("value_count is 0");
410 for (unsigned i
= component
; i
< value_count
+ component
; i
++) {
411 LLVMValueRef value
= values
[i
];
414 vec
= LLVMGetUndef( LLVMVectorType(LLVMTypeOf(value
), value_count
));
415 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, i
- component
, false);
416 vec
= LLVMBuildInsertElement(ctx
->builder
, vec
, value
, index
, "");
422 ac_build_gather_values_extended(struct ac_llvm_context
*ctx
,
423 LLVMValueRef
*values
,
424 unsigned value_count
,
425 unsigned value_stride
,
429 LLVMBuilderRef builder
= ctx
->builder
;
430 LLVMValueRef vec
= NULL
;
433 if (value_count
== 1 && !always_vector
) {
435 return LLVMBuildLoad(builder
, values
[0], "");
437 } else if (!value_count
)
438 unreachable("value_count is 0");
440 for (i
= 0; i
< value_count
; i
++) {
441 LLVMValueRef value
= values
[i
* value_stride
];
443 value
= LLVMBuildLoad(builder
, value
, "");
446 vec
= LLVMGetUndef( LLVMVectorType(LLVMTypeOf(value
), value_count
));
447 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, i
, false);
448 vec
= LLVMBuildInsertElement(builder
, vec
, value
, index
, "");
454 ac_build_gather_values(struct ac_llvm_context
*ctx
,
455 LLVMValueRef
*values
,
456 unsigned value_count
)
458 return ac_build_gather_values_extended(ctx
, values
, value_count
, 1, false, false);
462 ac_build_fdiv(struct ac_llvm_context
*ctx
,
466 LLVMValueRef ret
= LLVMBuildFDiv(ctx
->builder
, num
, den
, "");
468 /* Use v_rcp_f32 instead of precise division. */
469 if (!LLVMIsConstant(ret
))
470 LLVMSetMetadata(ret
, ctx
->fpmath_md_kind
, ctx
->fpmath_md_2p5_ulp
);
474 /* Coordinates for cube map selection. sc, tc, and ma are as in Table 8.27
475 * of the OpenGL 4.5 (Compatibility Profile) specification, except ma is
476 * already multiplied by two. id is the cube face number.
478 struct cube_selection_coords
{
485 build_cube_intrinsic(struct ac_llvm_context
*ctx
,
487 struct cube_selection_coords
*out
)
489 LLVMTypeRef f32
= ctx
->f32
;
491 out
->stc
[1] = ac_build_intrinsic(ctx
, "llvm.amdgcn.cubetc",
492 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
493 out
->stc
[0] = ac_build_intrinsic(ctx
, "llvm.amdgcn.cubesc",
494 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
495 out
->ma
= ac_build_intrinsic(ctx
, "llvm.amdgcn.cubema",
496 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
497 out
->id
= ac_build_intrinsic(ctx
, "llvm.amdgcn.cubeid",
498 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
502 * Build a manual selection sequence for cube face sc/tc coordinates and
503 * major axis vector (multiplied by 2 for consistency) for the given
504 * vec3 \p coords, for the face implied by \p selcoords.
506 * For the major axis, we always adjust the sign to be in the direction of
507 * selcoords.ma; i.e., a positive out_ma means that coords is pointed towards
508 * the selcoords major axis.
510 static void build_cube_select(struct ac_llvm_context
*ctx
,
511 const struct cube_selection_coords
*selcoords
,
512 const LLVMValueRef
*coords
,
513 LLVMValueRef
*out_st
,
514 LLVMValueRef
*out_ma
)
516 LLVMBuilderRef builder
= ctx
->builder
;
517 LLVMTypeRef f32
= LLVMTypeOf(coords
[0]);
518 LLVMValueRef is_ma_positive
;
520 LLVMValueRef is_ma_z
, is_not_ma_z
;
521 LLVMValueRef is_ma_y
;
522 LLVMValueRef is_ma_x
;
526 is_ma_positive
= LLVMBuildFCmp(builder
, LLVMRealUGE
,
527 selcoords
->ma
, LLVMConstReal(f32
, 0.0), "");
528 sgn_ma
= LLVMBuildSelect(builder
, is_ma_positive
,
529 LLVMConstReal(f32
, 1.0), LLVMConstReal(f32
, -1.0), "");
531 is_ma_z
= LLVMBuildFCmp(builder
, LLVMRealUGE
, selcoords
->id
, LLVMConstReal(f32
, 4.0), "");
532 is_not_ma_z
= LLVMBuildNot(builder
, is_ma_z
, "");
533 is_ma_y
= LLVMBuildAnd(builder
, is_not_ma_z
,
534 LLVMBuildFCmp(builder
, LLVMRealUGE
, selcoords
->id
, LLVMConstReal(f32
, 2.0), ""), "");
535 is_ma_x
= LLVMBuildAnd(builder
, is_not_ma_z
, LLVMBuildNot(builder
, is_ma_y
, ""), "");
538 tmp
= LLVMBuildSelect(builder
, is_ma_x
, coords
[2], coords
[0], "");
539 sgn
= LLVMBuildSelect(builder
, is_ma_y
, LLVMConstReal(f32
, 1.0),
540 LLVMBuildSelect(builder
, is_ma_z
, sgn_ma
,
541 LLVMBuildFNeg(builder
, sgn_ma
, ""), ""), "");
542 out_st
[0] = LLVMBuildFMul(builder
, tmp
, sgn
, "");
545 tmp
= LLVMBuildSelect(builder
, is_ma_y
, coords
[2], coords
[1], "");
546 sgn
= LLVMBuildSelect(builder
, is_ma_y
, sgn_ma
,
547 LLVMConstReal(f32
, -1.0), "");
548 out_st
[1] = LLVMBuildFMul(builder
, tmp
, sgn
, "");
551 tmp
= LLVMBuildSelect(builder
, is_ma_z
, coords
[2],
552 LLVMBuildSelect(builder
, is_ma_y
, coords
[1], coords
[0], ""), "");
553 tmp
= ac_build_intrinsic(ctx
, "llvm.fabs.f32",
554 ctx
->f32
, &tmp
, 1, AC_FUNC_ATTR_READNONE
);
555 *out_ma
= LLVMBuildFMul(builder
, tmp
, LLVMConstReal(f32
, 2.0), "");
559 ac_prepare_cube_coords(struct ac_llvm_context
*ctx
,
560 bool is_deriv
, bool is_array
, bool is_lod
,
561 LLVMValueRef
*coords_arg
,
562 LLVMValueRef
*derivs_arg
)
565 LLVMBuilderRef builder
= ctx
->builder
;
566 struct cube_selection_coords selcoords
;
567 LLVMValueRef coords
[3];
570 if (is_array
&& !is_lod
) {
571 LLVMValueRef tmp
= coords_arg
[3];
572 tmp
= ac_build_intrinsic(ctx
, "llvm.rint.f32", ctx
->f32
, &tmp
, 1, 0);
574 /* Section 8.9 (Texture Functions) of the GLSL 4.50 spec says:
576 * "For Array forms, the array layer used will be
578 * max(0, min(d−1, floor(layer+0.5)))
580 * where d is the depth of the texture array and layer
581 * comes from the component indicated in the tables below.
582 * Workaroudn for an issue where the layer is taken from a
583 * helper invocation which happens to fall on a different
584 * layer due to extrapolation."
586 * VI and earlier attempt to implement this in hardware by
587 * clamping the value of coords[2] = (8 * layer) + face.
588 * Unfortunately, this means that the we end up with the wrong
589 * face when clamping occurs.
591 * Clamp the layer earlier to work around the issue.
593 if (ctx
->chip_class
<= VI
) {
595 ge0
= LLVMBuildFCmp(builder
, LLVMRealOGE
, tmp
, ctx
->f32_0
, "");
596 tmp
= LLVMBuildSelect(builder
, ge0
, tmp
, ctx
->f32_0
, "");
602 build_cube_intrinsic(ctx
, coords_arg
, &selcoords
);
604 invma
= ac_build_intrinsic(ctx
, "llvm.fabs.f32",
605 ctx
->f32
, &selcoords
.ma
, 1, AC_FUNC_ATTR_READNONE
);
606 invma
= ac_build_fdiv(ctx
, LLVMConstReal(ctx
->f32
, 1.0), invma
);
608 for (int i
= 0; i
< 2; ++i
)
609 coords
[i
] = LLVMBuildFMul(builder
, selcoords
.stc
[i
], invma
, "");
611 coords
[2] = selcoords
.id
;
613 if (is_deriv
&& derivs_arg
) {
614 LLVMValueRef derivs
[4];
617 /* Convert cube derivatives to 2D derivatives. */
618 for (axis
= 0; axis
< 2; axis
++) {
619 LLVMValueRef deriv_st
[2];
620 LLVMValueRef deriv_ma
;
622 /* Transform the derivative alongside the texture
623 * coordinate. Mathematically, the correct formula is
624 * as follows. Assume we're projecting onto the +Z face
625 * and denote by dx/dh the derivative of the (original)
626 * X texture coordinate with respect to horizontal
627 * window coordinates. The projection onto the +Z face
632 * Then df/dh = df/dx * dx/dh + df/dz * dz/dh
633 * = 1/z * dx/dh - x/z * 1/z * dz/dh.
635 * This motivatives the implementation below.
637 * Whether this actually gives the expected results for
638 * apps that might feed in derivatives obtained via
639 * finite differences is anyone's guess. The OpenGL spec
640 * seems awfully quiet about how textureGrad for cube
641 * maps should be handled.
643 build_cube_select(ctx
, &selcoords
, &derivs_arg
[axis
* 3],
644 deriv_st
, &deriv_ma
);
646 deriv_ma
= LLVMBuildFMul(builder
, deriv_ma
, invma
, "");
648 for (int i
= 0; i
< 2; ++i
)
649 derivs
[axis
* 2 + i
] =
650 LLVMBuildFSub(builder
,
651 LLVMBuildFMul(builder
, deriv_st
[i
], invma
, ""),
652 LLVMBuildFMul(builder
, deriv_ma
, coords
[i
], ""), "");
655 memcpy(derivs_arg
, derivs
, sizeof(derivs
));
658 /* Shift the texture coordinate. This must be applied after the
659 * derivative calculation.
661 for (int i
= 0; i
< 2; ++i
)
662 coords
[i
] = LLVMBuildFAdd(builder
, coords
[i
], LLVMConstReal(ctx
->f32
, 1.5), "");
665 /* for cube arrays coord.z = coord.w(array_index) * 8 + face */
666 /* coords_arg.w component - array_index for cube arrays */
667 LLVMValueRef tmp
= LLVMBuildFMul(ctx
->builder
, coords_arg
[3], LLVMConstReal(ctx
->f32
, 8.0), "");
668 coords
[2] = LLVMBuildFAdd(ctx
->builder
, tmp
, coords
[2], "");
671 memcpy(coords_arg
, coords
, sizeof(coords
));
676 ac_build_fs_interp(struct ac_llvm_context
*ctx
,
677 LLVMValueRef llvm_chan
,
678 LLVMValueRef attr_number
,
683 LLVMValueRef args
[5];
686 if (HAVE_LLVM
< 0x0400) {
688 ij
[0] = LLVMBuildBitCast(ctx
->builder
, i
, ctx
->i32
, "");
689 ij
[1] = LLVMBuildBitCast(ctx
->builder
, j
, ctx
->i32
, "");
692 args
[1] = attr_number
;
694 args
[3] = ac_build_gather_values(ctx
, ij
, 2);
695 return ac_build_intrinsic(ctx
, "llvm.SI.fs.interp",
697 AC_FUNC_ATTR_READNONE
);
702 args
[2] = attr_number
;
705 p1
= ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p1",
706 ctx
->f32
, args
, 4, AC_FUNC_ATTR_READNONE
);
711 args
[3] = attr_number
;
714 return ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p2",
715 ctx
->f32
, args
, 5, AC_FUNC_ATTR_READNONE
);
719 ac_build_fs_interp_mov(struct ac_llvm_context
*ctx
,
720 LLVMValueRef parameter
,
721 LLVMValueRef llvm_chan
,
722 LLVMValueRef attr_number
,
725 LLVMValueRef args
[4];
726 if (HAVE_LLVM
< 0x0400) {
728 args
[1] = attr_number
;
731 return ac_build_intrinsic(ctx
,
732 "llvm.SI.fs.constant",
734 AC_FUNC_ATTR_READNONE
);
739 args
[2] = attr_number
;
742 return ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.mov",
743 ctx
->f32
, args
, 4, AC_FUNC_ATTR_READNONE
);
747 ac_build_gep0(struct ac_llvm_context
*ctx
,
748 LLVMValueRef base_ptr
,
751 LLVMValueRef indices
[2] = {
752 LLVMConstInt(ctx
->i32
, 0, 0),
755 return LLVMBuildGEP(ctx
->builder
, base_ptr
,
760 ac_build_indexed_store(struct ac_llvm_context
*ctx
,
761 LLVMValueRef base_ptr
, LLVMValueRef index
,
764 LLVMBuildStore(ctx
->builder
, value
,
765 ac_build_gep0(ctx
, base_ptr
, index
));
769 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad.
770 * It's equivalent to doing a load from &base_ptr[index].
772 * \param base_ptr Where the array starts.
773 * \param index The element index into the array.
774 * \param uniform Whether the base_ptr and index can be assumed to be
775 * dynamically uniform (i.e. load to an SGPR)
776 * \param invariant Whether the load is invariant (no other opcodes affect it)
779 ac_build_load_custom(struct ac_llvm_context
*ctx
, LLVMValueRef base_ptr
,
780 LLVMValueRef index
, bool uniform
, bool invariant
)
782 LLVMValueRef pointer
, result
;
784 pointer
= ac_build_gep0(ctx
, base_ptr
, index
);
786 LLVMSetMetadata(pointer
, ctx
->uniform_md_kind
, ctx
->empty_md
);
787 result
= LLVMBuildLoad(ctx
->builder
, pointer
, "");
789 LLVMSetMetadata(result
, ctx
->invariant_load_md_kind
, ctx
->empty_md
);
793 LLVMValueRef
ac_build_load(struct ac_llvm_context
*ctx
, LLVMValueRef base_ptr
,
796 return ac_build_load_custom(ctx
, base_ptr
, index
, false, false);
799 LLVMValueRef
ac_build_load_invariant(struct ac_llvm_context
*ctx
,
800 LLVMValueRef base_ptr
, LLVMValueRef index
)
802 return ac_build_load_custom(ctx
, base_ptr
, index
, false, true);
805 LLVMValueRef
ac_build_load_to_sgpr(struct ac_llvm_context
*ctx
,
806 LLVMValueRef base_ptr
, LLVMValueRef index
)
808 return ac_build_load_custom(ctx
, base_ptr
, index
, true, true);
811 /* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
812 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
813 * or v4i32 (num_channels=3,4).
816 ac_build_buffer_store_dword(struct ac_llvm_context
*ctx
,
819 unsigned num_channels
,
820 LLVMValueRef voffset
,
821 LLVMValueRef soffset
,
822 unsigned inst_offset
,
825 bool writeonly_memory
,
826 bool swizzle_enable_hint
)
828 /* SWIZZLE_ENABLE requires that soffset isn't folded into voffset
829 * (voffset is swizzled, but soffset isn't swizzled).
830 * llvm.amdgcn.buffer.store doesn't have a separate soffset parameter.
832 if (!swizzle_enable_hint
) {
833 /* Split 3 channel stores, becase LLVM doesn't support 3-channel
835 if (num_channels
== 3) {
836 LLVMValueRef v
[3], v01
;
838 for (int i
= 0; i
< 3; i
++) {
839 v
[i
] = LLVMBuildExtractElement(ctx
->builder
, vdata
,
840 LLVMConstInt(ctx
->i32
, i
, 0), "");
842 v01
= ac_build_gather_values(ctx
, v
, 2);
844 ac_build_buffer_store_dword(ctx
, rsrc
, v01
, 2, voffset
,
845 soffset
, inst_offset
, glc
, slc
,
846 writeonly_memory
, swizzle_enable_hint
);
847 ac_build_buffer_store_dword(ctx
, rsrc
, v
[2], 1, voffset
,
848 soffset
, inst_offset
+ 8,
850 writeonly_memory
, swizzle_enable_hint
);
854 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
855 static const char *types
[] = {"f32", "v2f32", "v4f32"};
857 LLVMValueRef offset
= soffset
;
860 offset
= LLVMBuildAdd(ctx
->builder
, offset
,
861 LLVMConstInt(ctx
->i32
, inst_offset
, 0), "");
863 offset
= LLVMBuildAdd(ctx
->builder
, offset
, voffset
, "");
865 LLVMValueRef args
[] = {
866 ac_to_float(ctx
, vdata
),
867 LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, ""),
868 LLVMConstInt(ctx
->i32
, 0, 0),
870 LLVMConstInt(ctx
->i1
, glc
, 0),
871 LLVMConstInt(ctx
->i1
, slc
, 0),
874 snprintf(name
, sizeof(name
), "llvm.amdgcn.buffer.store.%s",
877 ac_build_intrinsic(ctx
, name
, ctx
->voidt
,
878 args
, ARRAY_SIZE(args
),
880 AC_FUNC_ATTR_INACCESSIBLE_MEM_ONLY
:
881 AC_FUNC_ATTR_WRITEONLY
);
885 static unsigned dfmt
[] = {
886 V_008F0C_BUF_DATA_FORMAT_32
,
887 V_008F0C_BUF_DATA_FORMAT_32_32
,
888 V_008F0C_BUF_DATA_FORMAT_32_32_32
,
889 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
891 assert(num_channels
>= 1 && num_channels
<= 4);
893 LLVMValueRef args
[] = {
896 LLVMConstInt(ctx
->i32
, num_channels
, 0),
897 voffset
? voffset
: LLVMGetUndef(ctx
->i32
),
899 LLVMConstInt(ctx
->i32
, inst_offset
, 0),
900 LLVMConstInt(ctx
->i32
, dfmt
[num_channels
- 1], 0),
901 LLVMConstInt(ctx
->i32
, V_008F0C_BUF_NUM_FORMAT_UINT
, 0),
902 LLVMConstInt(ctx
->i32
, voffset
!= NULL
, 0),
903 LLVMConstInt(ctx
->i32
, 0, 0), /* idxen */
904 LLVMConstInt(ctx
->i32
, glc
, 0),
905 LLVMConstInt(ctx
->i32
, slc
, 0),
906 LLVMConstInt(ctx
->i32
, 0, 0), /* tfe*/
909 /* The instruction offset field has 12 bits */
910 assert(voffset
|| inst_offset
< (1 << 12));
912 /* The intrinsic is overloaded, we need to add a type suffix for overloading to work. */
913 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
914 const char *types
[] = {"i32", "v2i32", "v4i32"};
916 snprintf(name
, sizeof(name
), "llvm.SI.tbuffer.store.%s", types
[func
]);
918 ac_build_intrinsic(ctx
, name
, ctx
->voidt
,
919 args
, ARRAY_SIZE(args
),
920 AC_FUNC_ATTR_LEGACY
);
924 ac_build_buffer_load(struct ac_llvm_context
*ctx
,
928 LLVMValueRef voffset
,
929 LLVMValueRef soffset
,
930 unsigned inst_offset
,
936 LLVMValueRef offset
= LLVMConstInt(ctx
->i32
, inst_offset
, 0);
938 offset
= LLVMBuildAdd(ctx
->builder
, offset
, voffset
, "");
940 offset
= LLVMBuildAdd(ctx
->builder
, offset
, soffset
, "");
942 /* TODO: VI and later generations can use SMEM with GLC=1.*/
943 if (allow_smem
&& !glc
&& !slc
) {
944 assert(vindex
== NULL
);
946 LLVMValueRef result
[4];
948 for (int i
= 0; i
< num_channels
; i
++) {
950 offset
= LLVMBuildAdd(ctx
->builder
, offset
,
951 LLVMConstInt(ctx
->i32
, 4, 0), "");
953 LLVMValueRef args
[2] = {rsrc
, offset
};
954 result
[i
] = ac_build_intrinsic(ctx
, "llvm.SI.load.const.v4i32",
956 AC_FUNC_ATTR_READNONE
|
957 AC_FUNC_ATTR_LEGACY
);
959 if (num_channels
== 1)
962 if (num_channels
== 3)
963 result
[num_channels
++] = LLVMGetUndef(ctx
->f32
);
964 return ac_build_gather_values(ctx
, result
, num_channels
);
967 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
969 LLVMValueRef args
[] = {
970 LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, ""),
971 vindex
? vindex
: LLVMConstInt(ctx
->i32
, 0, 0),
973 LLVMConstInt(ctx
->i1
, glc
, 0),
974 LLVMConstInt(ctx
->i1
, slc
, 0)
977 LLVMTypeRef types
[] = {ctx
->f32
, LLVMVectorType(ctx
->f32
, 2),
979 const char *type_names
[] = {"f32", "v2f32", "v4f32"};
982 snprintf(name
, sizeof(name
), "llvm.amdgcn.buffer.load.%s",
985 return ac_build_intrinsic(ctx
, name
, types
[func
], args
,
987 ac_get_load_intr_attribs(can_speculate
));
990 LLVMValueRef
ac_build_buffer_load_format(struct ac_llvm_context
*ctx
,
993 LLVMValueRef voffset
,
996 LLVMValueRef args
[] = {
997 LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, ""),
1000 ctx
->i1false
, /* glc */
1001 ctx
->i1false
, /* slc */
1004 return ac_build_intrinsic(ctx
,
1005 "llvm.amdgcn.buffer.load.format.v4f32",
1006 ctx
->v4f32
, args
, ARRAY_SIZE(args
),
1007 ac_get_load_intr_attribs(can_speculate
));
1011 * Set range metadata on an instruction. This can only be used on load and
1012 * call instructions. If you know an instruction can only produce the values
1013 * 0, 1, 2, you would do set_range_metadata(value, 0, 3);
1014 * \p lo is the minimum value inclusive.
1015 * \p hi is the maximum value exclusive.
1017 static void set_range_metadata(struct ac_llvm_context
*ctx
,
1018 LLVMValueRef value
, unsigned lo
, unsigned hi
)
1020 LLVMValueRef range_md
, md_args
[2];
1021 LLVMTypeRef type
= LLVMTypeOf(value
);
1022 LLVMContextRef context
= LLVMGetTypeContext(type
);
1024 md_args
[0] = LLVMConstInt(type
, lo
, false);
1025 md_args
[1] = LLVMConstInt(type
, hi
, false);
1026 range_md
= LLVMMDNodeInContext(context
, md_args
, 2);
1027 LLVMSetMetadata(value
, ctx
->range_md_kind
, range_md
);
1031 ac_get_thread_id(struct ac_llvm_context
*ctx
)
1035 LLVMValueRef tid_args
[2];
1036 tid_args
[0] = LLVMConstInt(ctx
->i32
, 0xffffffff, false);
1037 tid_args
[1] = LLVMConstInt(ctx
->i32
, 0, false);
1038 tid_args
[1] = ac_build_intrinsic(ctx
,
1039 "llvm.amdgcn.mbcnt.lo", ctx
->i32
,
1040 tid_args
, 2, AC_FUNC_ATTR_READNONE
);
1042 tid
= ac_build_intrinsic(ctx
, "llvm.amdgcn.mbcnt.hi",
1044 2, AC_FUNC_ATTR_READNONE
);
1045 set_range_metadata(ctx
, tid
, 0, 64);
1050 * SI implements derivatives using the local data store (LDS)
1051 * All writes to the LDS happen in all executing threads at
1052 * the same time. TID is the Thread ID for the current
1053 * thread and is a value between 0 and 63, representing
1054 * the thread's position in the wavefront.
1056 * For the pixel shader threads are grouped into quads of four pixels.
1057 * The TIDs of the pixels of a quad are:
1065 * So, masking the TID with 0xfffffffc yields the TID of the top left pixel
1066 * of the quad, masking with 0xfffffffd yields the TID of the top pixel of
1067 * the current pixel's column, and masking with 0xfffffffe yields the TID
1068 * of the left pixel of the current pixel's row.
1070 * Adding 1 yields the TID of the pixel to the right of the left pixel, and
1071 * adding 2 yields the TID of the pixel below the top pixel.
1074 ac_build_ddxy(struct ac_llvm_context
*ctx
,
1079 LLVMValueRef tl
, trbl
, args
[2];
1080 LLVMValueRef result
;
1082 if (ctx
->chip_class
>= VI
) {
1083 LLVMValueRef thread_id
, tl_tid
, trbl_tid
;
1084 thread_id
= ac_get_thread_id(ctx
);
1086 tl_tid
= LLVMBuildAnd(ctx
->builder
, thread_id
,
1087 LLVMConstInt(ctx
->i32
, mask
, false), "");
1089 trbl_tid
= LLVMBuildAdd(ctx
->builder
, tl_tid
,
1090 LLVMConstInt(ctx
->i32
, idx
, false), "");
1092 args
[0] = LLVMBuildMul(ctx
->builder
, tl_tid
,
1093 LLVMConstInt(ctx
->i32
, 4, false), "");
1095 tl
= ac_build_intrinsic(ctx
,
1096 "llvm.amdgcn.ds.bpermute", ctx
->i32
,
1098 AC_FUNC_ATTR_READNONE
|
1099 AC_FUNC_ATTR_CONVERGENT
);
1101 args
[0] = LLVMBuildMul(ctx
->builder
, trbl_tid
,
1102 LLVMConstInt(ctx
->i32
, 4, false), "");
1103 trbl
= ac_build_intrinsic(ctx
,
1104 "llvm.amdgcn.ds.bpermute", ctx
->i32
,
1106 AC_FUNC_ATTR_READNONE
|
1107 AC_FUNC_ATTR_CONVERGENT
);
1109 uint32_t masks
[2] = {};
1112 case AC_TID_MASK_TOP_LEFT
:
1120 case AC_TID_MASK_TOP
:
1124 case AC_TID_MASK_LEFT
:
1133 args
[1] = LLVMConstInt(ctx
->i32
, masks
[0], false);
1135 tl
= ac_build_intrinsic(ctx
,
1136 "llvm.amdgcn.ds.swizzle", ctx
->i32
,
1138 AC_FUNC_ATTR_READNONE
|
1139 AC_FUNC_ATTR_CONVERGENT
);
1141 args
[1] = LLVMConstInt(ctx
->i32
, masks
[1], false);
1142 trbl
= ac_build_intrinsic(ctx
,
1143 "llvm.amdgcn.ds.swizzle", ctx
->i32
,
1145 AC_FUNC_ATTR_READNONE
|
1146 AC_FUNC_ATTR_CONVERGENT
);
1149 tl
= LLVMBuildBitCast(ctx
->builder
, tl
, ctx
->f32
, "");
1150 trbl
= LLVMBuildBitCast(ctx
->builder
, trbl
, ctx
->f32
, "");
1151 result
= LLVMBuildFSub(ctx
->builder
, trbl
, tl
, "");
1156 ac_build_sendmsg(struct ac_llvm_context
*ctx
,
1158 LLVMValueRef wave_id
)
1160 LLVMValueRef args
[2];
1161 const char *intr_name
= (HAVE_LLVM
< 0x0400) ? "llvm.SI.sendmsg" : "llvm.amdgcn.s.sendmsg";
1162 args
[0] = LLVMConstInt(ctx
->i32
, msg
, false);
1164 ac_build_intrinsic(ctx
, intr_name
, ctx
->voidt
, args
, 2, 0);
1168 ac_build_imsb(struct ac_llvm_context
*ctx
,
1170 LLVMTypeRef dst_type
)
1172 const char *intr_name
= (HAVE_LLVM
< 0x0400) ? "llvm.AMDGPU.flbit.i32" :
1173 "llvm.amdgcn.sffbh.i32";
1174 LLVMValueRef msb
= ac_build_intrinsic(ctx
, intr_name
,
1176 AC_FUNC_ATTR_READNONE
);
1178 /* The HW returns the last bit index from MSB, but NIR/TGSI wants
1179 * the index from LSB. Invert it by doing "31 - msb". */
1180 msb
= LLVMBuildSub(ctx
->builder
, LLVMConstInt(ctx
->i32
, 31, false),
1183 LLVMValueRef all_ones
= LLVMConstInt(ctx
->i32
, -1, true);
1184 LLVMValueRef cond
= LLVMBuildOr(ctx
->builder
,
1185 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
1186 arg
, LLVMConstInt(ctx
->i32
, 0, 0), ""),
1187 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
1188 arg
, all_ones
, ""), "");
1190 return LLVMBuildSelect(ctx
->builder
, cond
, all_ones
, msb
, "");
1194 ac_build_umsb(struct ac_llvm_context
*ctx
,
1196 LLVMTypeRef dst_type
)
1198 LLVMValueRef args
[2] = {
1202 LLVMValueRef msb
= ac_build_intrinsic(ctx
, "llvm.ctlz.i32",
1203 dst_type
, args
, ARRAY_SIZE(args
),
1204 AC_FUNC_ATTR_READNONE
);
1206 /* The HW returns the last bit index from MSB, but TGSI/NIR wants
1207 * the index from LSB. Invert it by doing "31 - msb". */
1208 msb
= LLVMBuildSub(ctx
->builder
, LLVMConstInt(ctx
->i32
, 31, false),
1211 /* check for zero */
1212 return LLVMBuildSelect(ctx
->builder
,
1213 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, arg
,
1214 LLVMConstInt(ctx
->i32
, 0, 0), ""),
1215 LLVMConstInt(ctx
->i32
, -1, true), msb
, "");
1218 LLVMValueRef
ac_build_fmin(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
1221 LLVMValueRef args
[2] = {a
, b
};
1222 return ac_build_intrinsic(ctx
, "llvm.minnum.f32", ctx
->f32
, args
, 2,
1223 AC_FUNC_ATTR_READNONE
);
1226 LLVMValueRef
ac_build_fmax(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
1229 LLVMValueRef args
[2] = {a
, b
};
1230 return ac_build_intrinsic(ctx
, "llvm.maxnum.f32", ctx
->f32
, args
, 2,
1231 AC_FUNC_ATTR_READNONE
);
1234 LLVMValueRef
ac_build_umin(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
1237 LLVMValueRef cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntULE
, a
, b
, "");
1238 return LLVMBuildSelect(ctx
->builder
, cmp
, a
, b
, "");
1241 LLVMValueRef
ac_build_clamp(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
1243 if (HAVE_LLVM
>= 0x0500) {
1244 return ac_build_fmin(ctx
, ac_build_fmax(ctx
, value
, ctx
->f32_0
),
1248 LLVMValueRef args
[3] = {
1250 LLVMConstReal(ctx
->f32
, 0),
1251 LLVMConstReal(ctx
->f32
, 1),
1254 return ac_build_intrinsic(ctx
, "llvm.AMDGPU.clamp.", ctx
->f32
, args
, 3,
1255 AC_FUNC_ATTR_READNONE
|
1256 AC_FUNC_ATTR_LEGACY
);
1259 void ac_build_export(struct ac_llvm_context
*ctx
, struct ac_export_args
*a
)
1261 LLVMValueRef args
[9];
1263 if (HAVE_LLVM
>= 0x0500) {
1264 args
[0] = LLVMConstInt(ctx
->i32
, a
->target
, 0);
1265 args
[1] = LLVMConstInt(ctx
->i32
, a
->enabled_channels
, 0);
1268 LLVMTypeRef i16
= LLVMInt16TypeInContext(ctx
->context
);
1269 LLVMTypeRef v2i16
= LLVMVectorType(i16
, 2);
1271 args
[2] = LLVMBuildBitCast(ctx
->builder
, a
->out
[0],
1273 args
[3] = LLVMBuildBitCast(ctx
->builder
, a
->out
[1],
1275 args
[4] = LLVMConstInt(ctx
->i1
, a
->done
, 0);
1276 args
[5] = LLVMConstInt(ctx
->i1
, a
->valid_mask
, 0);
1278 ac_build_intrinsic(ctx
, "llvm.amdgcn.exp.compr.v2i16",
1279 ctx
->voidt
, args
, 6, 0);
1281 args
[2] = a
->out
[0];
1282 args
[3] = a
->out
[1];
1283 args
[4] = a
->out
[2];
1284 args
[5] = a
->out
[3];
1285 args
[6] = LLVMConstInt(ctx
->i1
, a
->done
, 0);
1286 args
[7] = LLVMConstInt(ctx
->i1
, a
->valid_mask
, 0);
1288 ac_build_intrinsic(ctx
, "llvm.amdgcn.exp.f32",
1289 ctx
->voidt
, args
, 8, 0);
1294 args
[0] = LLVMConstInt(ctx
->i32
, a
->enabled_channels
, 0);
1295 args
[1] = LLVMConstInt(ctx
->i32
, a
->valid_mask
, 0);
1296 args
[2] = LLVMConstInt(ctx
->i32
, a
->done
, 0);
1297 args
[3] = LLVMConstInt(ctx
->i32
, a
->target
, 0);
1298 args
[4] = LLVMConstInt(ctx
->i32
, a
->compr
, 0);
1299 memcpy(args
+ 5, a
->out
, sizeof(a
->out
[0]) * 4);
1301 ac_build_intrinsic(ctx
, "llvm.SI.export", ctx
->voidt
, args
, 9,
1302 AC_FUNC_ATTR_LEGACY
);
1305 LLVMValueRef
ac_build_image_opcode(struct ac_llvm_context
*ctx
,
1306 struct ac_image_args
*a
)
1308 LLVMTypeRef dst_type
;
1309 LLVMValueRef args
[11];
1310 unsigned num_args
= 0;
1311 const char *name
= NULL
;
1312 char intr_name
[128], type
[64];
1314 if (HAVE_LLVM
>= 0x0400) {
1315 bool sample
= a
->opcode
== ac_image_sample
||
1316 a
->opcode
== ac_image_gather4
||
1317 a
->opcode
== ac_image_get_lod
;
1320 args
[num_args
++] = ac_to_float(ctx
, a
->addr
);
1322 args
[num_args
++] = a
->addr
;
1324 args
[num_args
++] = a
->resource
;
1326 args
[num_args
++] = a
->sampler
;
1327 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->dmask
, 0);
1329 args
[num_args
++] = LLVMConstInt(ctx
->i1
, a
->unorm
, 0);
1330 args
[num_args
++] = ctx
->i1false
; /* glc */
1331 args
[num_args
++] = ctx
->i1false
; /* slc */
1332 args
[num_args
++] = ctx
->i1false
; /* lwe */
1333 args
[num_args
++] = LLVMConstInt(ctx
->i1
, a
->da
, 0);
1335 switch (a
->opcode
) {
1336 case ac_image_sample
:
1337 name
= "llvm.amdgcn.image.sample";
1339 case ac_image_gather4
:
1340 name
= "llvm.amdgcn.image.gather4";
1343 name
= "llvm.amdgcn.image.load";
1345 case ac_image_load_mip
:
1346 name
= "llvm.amdgcn.image.load.mip";
1348 case ac_image_get_lod
:
1349 name
= "llvm.amdgcn.image.getlod";
1351 case ac_image_get_resinfo
:
1352 name
= "llvm.amdgcn.image.getresinfo";
1355 unreachable("invalid image opcode");
1358 ac_build_type_name_for_intr(LLVMTypeOf(args
[0]), type
,
1361 snprintf(intr_name
, sizeof(intr_name
), "%s%s%s%s.v4f32.%s.v8i32",
1363 a
->compare
? ".c" : "",
1367 a
->level_zero
? ".lz" : "",
1368 a
->offset
? ".o" : "",
1371 LLVMValueRef result
=
1372 ac_build_intrinsic(ctx
, intr_name
,
1373 ctx
->v4f32
, args
, num_args
,
1374 AC_FUNC_ATTR_READNONE
);
1376 result
= LLVMBuildBitCast(ctx
->builder
, result
,
1382 args
[num_args
++] = a
->addr
;
1383 args
[num_args
++] = a
->resource
;
1385 if (a
->opcode
== ac_image_load
||
1386 a
->opcode
== ac_image_load_mip
||
1387 a
->opcode
== ac_image_get_resinfo
) {
1388 dst_type
= ctx
->v4i32
;
1390 dst_type
= ctx
->v4f32
;
1391 args
[num_args
++] = a
->sampler
;
1394 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->dmask
, 0);
1395 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->unorm
, 0);
1396 args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* r128 */
1397 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->da
, 0);
1398 args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* glc */
1399 args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* slc */
1400 args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* tfe */
1401 args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* lwe */
1403 switch (a
->opcode
) {
1404 case ac_image_sample
:
1405 name
= "llvm.SI.image.sample";
1407 case ac_image_gather4
:
1408 name
= "llvm.SI.gather4";
1411 name
= "llvm.SI.image.load";
1413 case ac_image_load_mip
:
1414 name
= "llvm.SI.image.load.mip";
1416 case ac_image_get_lod
:
1417 name
= "llvm.SI.getlod";
1419 case ac_image_get_resinfo
:
1420 name
= "llvm.SI.getresinfo";
1424 ac_build_type_name_for_intr(LLVMTypeOf(a
->addr
), type
, sizeof(type
));
1425 snprintf(intr_name
, sizeof(intr_name
), "%s%s%s%s.%s",
1427 a
->compare
? ".c" : "",
1431 a
->level_zero
? ".lz" : "",
1432 a
->offset
? ".o" : "",
1435 return ac_build_intrinsic(ctx
, intr_name
,
1436 dst_type
, args
, num_args
,
1437 AC_FUNC_ATTR_READNONE
|
1438 AC_FUNC_ATTR_LEGACY
);
1441 LLVMValueRef
ac_build_cvt_pkrtz_f16(struct ac_llvm_context
*ctx
,
1442 LLVMValueRef args
[2])
1444 if (HAVE_LLVM
>= 0x0500) {
1446 LLVMVectorType(LLVMHalfTypeInContext(ctx
->context
), 2);
1448 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pkrtz",
1450 AC_FUNC_ATTR_READNONE
);
1451 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
1454 return ac_build_intrinsic(ctx
, "llvm.SI.packf16", ctx
->i32
, args
, 2,
1455 AC_FUNC_ATTR_READNONE
|
1456 AC_FUNC_ATTR_LEGACY
);
1459 LLVMValueRef
ac_build_wqm_vote(struct ac_llvm_context
*ctx
, LLVMValueRef i1
)
1461 assert(HAVE_LLVM
>= 0x0600);
1462 return ac_build_intrinsic(ctx
, "llvm.amdgcn.wqm.vote", ctx
->i1
,
1463 &i1
, 1, AC_FUNC_ATTR_READNONE
);
1466 void ac_build_kill_if_false(struct ac_llvm_context
*ctx
, LLVMValueRef i1
)
1468 if (HAVE_LLVM
>= 0x0600) {
1469 ac_build_intrinsic(ctx
, "llvm.amdgcn.kill", ctx
->voidt
,
1474 LLVMValueRef value
= LLVMBuildSelect(ctx
->builder
, i1
,
1475 LLVMConstReal(ctx
->f32
, 1),
1476 LLVMConstReal(ctx
->f32
, -1), "");
1477 ac_build_intrinsic(ctx
, "llvm.AMDGPU.kill", ctx
->voidt
,
1478 &value
, 1, AC_FUNC_ATTR_LEGACY
);
1481 LLVMValueRef
ac_build_bfe(struct ac_llvm_context
*ctx
, LLVMValueRef input
,
1482 LLVMValueRef offset
, LLVMValueRef width
,
1485 LLVMValueRef args
[] = {
1491 if (HAVE_LLVM
>= 0x0500) {
1492 return ac_build_intrinsic(ctx
,
1493 is_signed
? "llvm.amdgcn.sbfe.i32" :
1494 "llvm.amdgcn.ubfe.i32",
1496 AC_FUNC_ATTR_READNONE
);
1499 return ac_build_intrinsic(ctx
,
1500 is_signed
? "llvm.AMDGPU.bfe.i32" :
1501 "llvm.AMDGPU.bfe.u32",
1503 AC_FUNC_ATTR_READNONE
|
1504 AC_FUNC_ATTR_LEGACY
);
1507 void ac_build_waitcnt(struct ac_llvm_context
*ctx
, unsigned simm16
)
1509 LLVMValueRef args
[1] = {
1510 LLVMConstInt(ctx
->i32
, simm16
, false),
1512 ac_build_intrinsic(ctx
, "llvm.amdgcn.s.waitcnt",
1513 ctx
->voidt
, args
, 1, 0);
1516 void ac_get_image_intr_name(const char *base_name
,
1517 LLVMTypeRef data_type
,
1518 LLVMTypeRef coords_type
,
1519 LLVMTypeRef rsrc_type
,
1520 char *out_name
, unsigned out_len
)
1522 char coords_type_name
[8];
1524 ac_build_type_name_for_intr(coords_type
, coords_type_name
,
1525 sizeof(coords_type_name
));
1527 if (HAVE_LLVM
<= 0x0309) {
1528 snprintf(out_name
, out_len
, "%s.%s", base_name
, coords_type_name
);
1530 char data_type_name
[8];
1531 char rsrc_type_name
[8];
1533 ac_build_type_name_for_intr(data_type
, data_type_name
,
1534 sizeof(data_type_name
));
1535 ac_build_type_name_for_intr(rsrc_type
, rsrc_type_name
,
1536 sizeof(rsrc_type_name
));
1537 snprintf(out_name
, out_len
, "%s.%s.%s.%s", base_name
,
1538 data_type_name
, coords_type_name
, rsrc_type_name
);
1542 #define AC_EXP_TARGET (HAVE_LLVM >= 0x0500 ? 0 : 3)
1543 #define AC_EXP_OUT0 (HAVE_LLVM >= 0x0500 ? 2 : 5)
1551 struct ac_vs_exp_chan
1555 enum ac_ir_type type
;
1558 struct ac_vs_exp_inst
{
1561 struct ac_vs_exp_chan chan
[4];
1564 struct ac_vs_exports
{
1566 struct ac_vs_exp_inst exp
[VARYING_SLOT_MAX
];
1569 /* Return true if the PARAM export has been eliminated. */
1570 static bool ac_eliminate_const_output(uint8_t *vs_output_param_offset
,
1571 uint32_t num_outputs
,
1572 struct ac_vs_exp_inst
*exp
)
1574 unsigned i
, default_val
; /* SPI_PS_INPUT_CNTL_i.DEFAULT_VAL */
1575 bool is_zero
[4] = {}, is_one
[4] = {};
1577 for (i
= 0; i
< 4; i
++) {
1578 /* It's a constant expression. Undef outputs are eliminated too. */
1579 if (exp
->chan
[i
].type
== AC_IR_UNDEF
) {
1582 } else if (exp
->chan
[i
].type
== AC_IR_CONST
) {
1583 if (exp
->chan
[i
].const_float
== 0)
1585 else if (exp
->chan
[i
].const_float
== 1)
1588 return false; /* other constant */
1593 /* Only certain combinations of 0 and 1 can be eliminated. */
1594 if (is_zero
[0] && is_zero
[1] && is_zero
[2])
1595 default_val
= is_zero
[3] ? 0 : 1;
1596 else if (is_one
[0] && is_one
[1] && is_one
[2])
1597 default_val
= is_zero
[3] ? 2 : 3;
1601 /* The PARAM export can be represented as DEFAULT_VAL. Kill it. */
1602 LLVMInstructionEraseFromParent(exp
->inst
);
1604 /* Change OFFSET to DEFAULT_VAL. */
1605 for (i
= 0; i
< num_outputs
; i
++) {
1606 if (vs_output_param_offset
[i
] == exp
->offset
) {
1607 vs_output_param_offset
[i
] =
1608 AC_EXP_PARAM_DEFAULT_VAL_0000
+ default_val
;
1615 static bool ac_eliminate_duplicated_output(uint8_t *vs_output_param_offset
,
1616 uint32_t num_outputs
,
1617 struct ac_vs_exports
*processed
,
1618 struct ac_vs_exp_inst
*exp
)
1620 unsigned p
, copy_back_channels
= 0;
1622 /* See if the output is already in the list of processed outputs.
1623 * The LLVMValueRef comparison relies on SSA.
1625 for (p
= 0; p
< processed
->num
; p
++) {
1626 bool different
= false;
1628 for (unsigned j
= 0; j
< 4; j
++) {
1629 struct ac_vs_exp_chan
*c1
= &processed
->exp
[p
].chan
[j
];
1630 struct ac_vs_exp_chan
*c2
= &exp
->chan
[j
];
1632 /* Treat undef as a match. */
1633 if (c2
->type
== AC_IR_UNDEF
)
1636 /* If c1 is undef but c2 isn't, we can copy c2 to c1
1637 * and consider the instruction duplicated.
1639 if (c1
->type
== AC_IR_UNDEF
) {
1640 copy_back_channels
|= 1 << j
;
1644 /* Test whether the channels are not equal. */
1645 if (c1
->type
!= c2
->type
||
1646 (c1
->type
== AC_IR_CONST
&&
1647 c1
->const_float
!= c2
->const_float
) ||
1648 (c1
->type
== AC_IR_VALUE
&&
1649 c1
->value
!= c2
->value
)) {
1657 copy_back_channels
= 0;
1659 if (p
== processed
->num
)
1662 /* If a match was found, but the matching export has undef where the new
1663 * one has a normal value, copy the normal value to the undef channel.
1665 struct ac_vs_exp_inst
*match
= &processed
->exp
[p
];
1667 while (copy_back_channels
) {
1668 unsigned chan
= u_bit_scan(©_back_channels
);
1670 assert(match
->chan
[chan
].type
== AC_IR_UNDEF
);
1671 LLVMSetOperand(match
->inst
, AC_EXP_OUT0
+ chan
,
1672 exp
->chan
[chan
].value
);
1673 match
->chan
[chan
] = exp
->chan
[chan
];
1676 /* The PARAM export is duplicated. Kill it. */
1677 LLVMInstructionEraseFromParent(exp
->inst
);
1679 /* Change OFFSET to the matching export. */
1680 for (unsigned i
= 0; i
< num_outputs
; i
++) {
1681 if (vs_output_param_offset
[i
] == exp
->offset
) {
1682 vs_output_param_offset
[i
] = match
->offset
;
1689 void ac_optimize_vs_outputs(struct ac_llvm_context
*ctx
,
1690 LLVMValueRef main_fn
,
1691 uint8_t *vs_output_param_offset
,
1692 uint32_t num_outputs
,
1693 uint8_t *num_param_exports
)
1695 LLVMBasicBlockRef bb
;
1696 bool removed_any
= false;
1697 struct ac_vs_exports exports
;
1701 /* Process all LLVM instructions. */
1702 bb
= LLVMGetFirstBasicBlock(main_fn
);
1704 LLVMValueRef inst
= LLVMGetFirstInstruction(bb
);
1707 LLVMValueRef cur
= inst
;
1708 inst
= LLVMGetNextInstruction(inst
);
1709 struct ac_vs_exp_inst exp
;
1711 if (LLVMGetInstructionOpcode(cur
) != LLVMCall
)
1714 LLVMValueRef callee
= ac_llvm_get_called_value(cur
);
1716 if (!ac_llvm_is_function(callee
))
1719 const char *name
= LLVMGetValueName(callee
);
1720 unsigned num_args
= LLVMCountParams(callee
);
1722 /* Check if this is an export instruction. */
1723 if ((num_args
!= 9 && num_args
!= 8) ||
1724 (strcmp(name
, "llvm.SI.export") &&
1725 strcmp(name
, "llvm.amdgcn.exp.f32")))
1728 LLVMValueRef arg
= LLVMGetOperand(cur
, AC_EXP_TARGET
);
1729 unsigned target
= LLVMConstIntGetZExtValue(arg
);
1731 if (target
< V_008DFC_SQ_EXP_PARAM
)
1734 target
-= V_008DFC_SQ_EXP_PARAM
;
1736 /* Parse the instruction. */
1737 memset(&exp
, 0, sizeof(exp
));
1738 exp
.offset
= target
;
1741 for (unsigned i
= 0; i
< 4; i
++) {
1742 LLVMValueRef v
= LLVMGetOperand(cur
, AC_EXP_OUT0
+ i
);
1744 exp
.chan
[i
].value
= v
;
1746 if (LLVMIsUndef(v
)) {
1747 exp
.chan
[i
].type
= AC_IR_UNDEF
;
1748 } else if (LLVMIsAConstantFP(v
)) {
1749 LLVMBool loses_info
;
1750 exp
.chan
[i
].type
= AC_IR_CONST
;
1751 exp
.chan
[i
].const_float
=
1752 LLVMConstRealGetDouble(v
, &loses_info
);
1754 exp
.chan
[i
].type
= AC_IR_VALUE
;
1758 /* Eliminate constant and duplicated PARAM exports. */
1759 if (ac_eliminate_const_output(vs_output_param_offset
,
1760 num_outputs
, &exp
) ||
1761 ac_eliminate_duplicated_output(vs_output_param_offset
,
1762 num_outputs
, &exports
,
1766 exports
.exp
[exports
.num
++] = exp
;
1769 bb
= LLVMGetNextBasicBlock(bb
);
1772 /* Remove holes in export memory due to removed PARAM exports.
1773 * This is done by renumbering all PARAM exports.
1776 uint8_t old_offset
[VARYING_SLOT_MAX
];
1779 /* Make a copy of the offsets. We need the old version while
1780 * we are modifying some of them. */
1781 memcpy(old_offset
, vs_output_param_offset
,
1782 sizeof(old_offset
));
1784 for (i
= 0; i
< exports
.num
; i
++) {
1785 unsigned offset
= exports
.exp
[i
].offset
;
1787 /* Update vs_output_param_offset. Multiple outputs can
1788 * have the same offset.
1790 for (out
= 0; out
< num_outputs
; out
++) {
1791 if (old_offset
[out
] == offset
)
1792 vs_output_param_offset
[out
] = i
;
1795 /* Change the PARAM offset in the instruction. */
1796 LLVMSetOperand(exports
.exp
[i
].inst
, AC_EXP_TARGET
,
1797 LLVMConstInt(ctx
->i32
,
1798 V_008DFC_SQ_EXP_PARAM
+ i
, 0));
1800 *num_param_exports
= exports
.num
;
1804 void ac_init_exec_full_mask(struct ac_llvm_context
*ctx
)
1806 LLVMValueRef full_mask
= LLVMConstInt(ctx
->i64
, ~0ull, 0);
1807 ac_build_intrinsic(ctx
,
1808 "llvm.amdgcn.init.exec", ctx
->voidt
,
1809 &full_mask
, 1, AC_FUNC_ATTR_CONVERGENT
);
1812 void ac_declare_lds_as_pointer(struct ac_llvm_context
*ctx
)
1814 unsigned lds_size
= ctx
->chip_class
>= CIK
? 65536 : 32768;
1815 ctx
->lds
= LLVMBuildIntToPtr(ctx
->builder
, ctx
->i32_0
,
1816 LLVMPointerType(LLVMArrayType(ctx
->i32
, lds_size
/ 4), AC_LOCAL_ADDR_SPACE
),
1820 LLVMValueRef
ac_lds_load(struct ac_llvm_context
*ctx
,
1821 LLVMValueRef dw_addr
)
1823 return ac_build_load(ctx
, ctx
->lds
, dw_addr
);
1826 void ac_lds_store(struct ac_llvm_context
*ctx
,
1827 LLVMValueRef dw_addr
,
1830 value
= ac_to_integer(ctx
, value
);
1831 ac_build_indexed_store(ctx
, ctx
->lds
,
1835 LLVMValueRef
ac_find_lsb(struct ac_llvm_context
*ctx
,
1836 LLVMTypeRef dst_type
,
1839 LLVMValueRef params
[2] = {
1842 /* The value of 1 means that ffs(x=0) = undef, so LLVM won't
1843 * add special code to check for x=0. The reason is that
1844 * the LLVM behavior for x=0 is different from what we
1845 * need here. However, LLVM also assumes that ffs(x) is
1846 * in [0, 31], but GLSL expects that ffs(0) = -1, so
1847 * a conditional assignment to handle 0 is still required.
1849 * The hardware already implements the correct behavior.
1851 LLVMConstInt(ctx
->i1
, 1, false),
1854 LLVMValueRef lsb
= ac_build_intrinsic(ctx
, "llvm.cttz.i32", ctx
->i32
,
1856 AC_FUNC_ATTR_READNONE
);
1858 /* TODO: We need an intrinsic to skip this conditional. */
1859 /* Check for zero: */
1860 return LLVMBuildSelect(ctx
->builder
, LLVMBuildICmp(ctx
->builder
,
1863 LLVMConstInt(ctx
->i32
, -1, 0), lsb
, "");