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
->i64_0
= LLVMConstInt(ctx
->i64
, 0, false);
80 ctx
->i64_1
= LLVMConstInt(ctx
->i64
, 1, false);
81 ctx
->f32_0
= LLVMConstReal(ctx
->f32
, 0.0);
82 ctx
->f32_1
= LLVMConstReal(ctx
->f32
, 1.0);
83 ctx
->f64_0
= LLVMConstReal(ctx
->f64
, 0.0);
84 ctx
->f64_1
= LLVMConstReal(ctx
->f64
, 1.0);
86 ctx
->i1false
= LLVMConstInt(ctx
->i1
, 0, false);
87 ctx
->i1true
= LLVMConstInt(ctx
->i1
, 1, false);
89 ctx
->range_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
92 ctx
->invariant_load_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
93 "invariant.load", 14);
95 ctx
->fpmath_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
, "fpmath", 6);
97 args
[0] = LLVMConstReal(ctx
->f32
, 2.5);
98 ctx
->fpmath_md_2p5_ulp
= LLVMMDNodeInContext(ctx
->context
, args
, 1);
100 ctx
->uniform_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
101 "amdgpu.uniform", 14);
103 ctx
->empty_md
= LLVMMDNodeInContext(ctx
->context
, NULL
, 0);
107 ac_get_llvm_num_components(LLVMValueRef value
)
109 LLVMTypeRef type
= LLVMTypeOf(value
);
110 unsigned num_components
= LLVMGetTypeKind(type
) == LLVMVectorTypeKind
111 ? LLVMGetVectorSize(type
)
113 return num_components
;
117 ac_llvm_extract_elem(struct ac_llvm_context
*ac
,
121 if (LLVMGetTypeKind(LLVMTypeOf(value
)) != LLVMVectorTypeKind
) {
126 return LLVMBuildExtractElement(ac
->builder
, value
,
127 LLVMConstInt(ac
->i32
, index
, false), "");
131 ac_get_type_size(LLVMTypeRef type
)
133 LLVMTypeKind kind
= LLVMGetTypeKind(type
);
136 case LLVMIntegerTypeKind
:
137 return LLVMGetIntTypeWidth(type
) / 8;
138 case LLVMFloatTypeKind
:
140 case LLVMDoubleTypeKind
:
141 case LLVMPointerTypeKind
:
143 case LLVMVectorTypeKind
:
144 return LLVMGetVectorSize(type
) *
145 ac_get_type_size(LLVMGetElementType(type
));
146 case LLVMArrayTypeKind
:
147 return LLVMGetArrayLength(type
) *
148 ac_get_type_size(LLVMGetElementType(type
));
155 static LLVMTypeRef
to_integer_type_scalar(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
157 if (t
== ctx
->f16
|| t
== ctx
->i16
)
159 else if (t
== ctx
->f32
|| t
== ctx
->i32
)
161 else if (t
== ctx
->f64
|| t
== ctx
->i64
)
164 unreachable("Unhandled integer size");
168 ac_to_integer_type(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
170 if (LLVMGetTypeKind(t
) == LLVMVectorTypeKind
) {
171 LLVMTypeRef elem_type
= LLVMGetElementType(t
);
172 return LLVMVectorType(to_integer_type_scalar(ctx
, elem_type
),
173 LLVMGetVectorSize(t
));
175 return to_integer_type_scalar(ctx
, t
);
179 ac_to_integer(struct ac_llvm_context
*ctx
, LLVMValueRef v
)
181 LLVMTypeRef type
= LLVMTypeOf(v
);
182 return LLVMBuildBitCast(ctx
->builder
, v
, ac_to_integer_type(ctx
, type
), "");
185 static LLVMTypeRef
to_float_type_scalar(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
187 if (t
== ctx
->i16
|| t
== ctx
->f16
)
189 else if (t
== ctx
->i32
|| t
== ctx
->f32
)
191 else if (t
== ctx
->i64
|| t
== ctx
->f64
)
194 unreachable("Unhandled float size");
198 ac_to_float_type(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
200 if (LLVMGetTypeKind(t
) == LLVMVectorTypeKind
) {
201 LLVMTypeRef elem_type
= LLVMGetElementType(t
);
202 return LLVMVectorType(to_float_type_scalar(ctx
, elem_type
),
203 LLVMGetVectorSize(t
));
205 return to_float_type_scalar(ctx
, t
);
209 ac_to_float(struct ac_llvm_context
*ctx
, LLVMValueRef v
)
211 LLVMTypeRef type
= LLVMTypeOf(v
);
212 return LLVMBuildBitCast(ctx
->builder
, v
, ac_to_float_type(ctx
, type
), "");
217 ac_build_intrinsic(struct ac_llvm_context
*ctx
, const char *name
,
218 LLVMTypeRef return_type
, LLVMValueRef
*params
,
219 unsigned param_count
, unsigned attrib_mask
)
221 LLVMValueRef function
, call
;
222 bool set_callsite_attrs
= HAVE_LLVM
>= 0x0400 &&
223 !(attrib_mask
& AC_FUNC_ATTR_LEGACY
);
225 function
= LLVMGetNamedFunction(ctx
->module
, name
);
227 LLVMTypeRef param_types
[32], function_type
;
230 assert(param_count
<= 32);
232 for (i
= 0; i
< param_count
; ++i
) {
234 param_types
[i
] = LLVMTypeOf(params
[i
]);
237 LLVMFunctionType(return_type
, param_types
, param_count
, 0);
238 function
= LLVMAddFunction(ctx
->module
, name
, function_type
);
240 LLVMSetFunctionCallConv(function
, LLVMCCallConv
);
241 LLVMSetLinkage(function
, LLVMExternalLinkage
);
243 if (!set_callsite_attrs
)
244 ac_add_func_attributes(ctx
->context
, function
, attrib_mask
);
247 call
= LLVMBuildCall(ctx
->builder
, function
, params
, param_count
, "");
248 if (set_callsite_attrs
)
249 ac_add_func_attributes(ctx
->context
, call
, attrib_mask
);
254 * Given the i32 or vNi32 \p type, generate the textual name (e.g. for use with
257 void ac_build_type_name_for_intr(LLVMTypeRef type
, char *buf
, unsigned bufsize
)
259 LLVMTypeRef elem_type
= type
;
261 assert(bufsize
>= 8);
263 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
) {
264 int ret
= snprintf(buf
, bufsize
, "v%u",
265 LLVMGetVectorSize(type
));
267 char *type_name
= LLVMPrintTypeToString(type
);
268 fprintf(stderr
, "Error building type name for: %s\n",
272 elem_type
= LLVMGetElementType(type
);
276 switch (LLVMGetTypeKind(elem_type
)) {
278 case LLVMIntegerTypeKind
:
279 snprintf(buf
, bufsize
, "i%d", LLVMGetIntTypeWidth(elem_type
));
281 case LLVMFloatTypeKind
:
282 snprintf(buf
, bufsize
, "f32");
284 case LLVMDoubleTypeKind
:
285 snprintf(buf
, bufsize
, "f64");
291 * Helper function that builds an LLVM IR PHI node and immediately adds
295 ac_build_phi(struct ac_llvm_context
*ctx
, LLVMTypeRef type
,
296 unsigned count_incoming
, LLVMValueRef
*values
,
297 LLVMBasicBlockRef
*blocks
)
299 LLVMValueRef phi
= LLVMBuildPhi(ctx
->builder
, type
, "");
300 LLVMAddIncoming(phi
, values
, blocks
, count_incoming
);
304 /* Prevent optimizations (at least of memory accesses) across the current
305 * point in the program by emitting empty inline assembly that is marked as
306 * having side effects.
308 * Optionally, a value can be passed through the inline assembly to prevent
309 * LLVM from hoisting calls to ReadNone functions.
312 ac_build_optimization_barrier(struct ac_llvm_context
*ctx
,
315 static int counter
= 0;
317 LLVMBuilderRef builder
= ctx
->builder
;
320 snprintf(code
, sizeof(code
), "; %d", p_atomic_inc_return(&counter
));
323 LLVMTypeRef ftype
= LLVMFunctionType(ctx
->voidt
, NULL
, 0, false);
324 LLVMValueRef inlineasm
= LLVMConstInlineAsm(ftype
, code
, "", true, false);
325 LLVMBuildCall(builder
, inlineasm
, NULL
, 0, "");
327 LLVMTypeRef ftype
= LLVMFunctionType(ctx
->i32
, &ctx
->i32
, 1, false);
328 LLVMValueRef inlineasm
= LLVMConstInlineAsm(ftype
, code
, "=v,0", true, false);
329 LLVMValueRef vgpr
= *pvgpr
;
330 LLVMTypeRef vgpr_type
= LLVMTypeOf(vgpr
);
331 unsigned vgpr_size
= ac_get_type_size(vgpr_type
);
334 assert(vgpr_size
% 4 == 0);
336 vgpr
= LLVMBuildBitCast(builder
, vgpr
, LLVMVectorType(ctx
->i32
, vgpr_size
/ 4), "");
337 vgpr0
= LLVMBuildExtractElement(builder
, vgpr
, ctx
->i32_0
, "");
338 vgpr0
= LLVMBuildCall(builder
, inlineasm
, &vgpr0
, 1, "");
339 vgpr
= LLVMBuildInsertElement(builder
, vgpr
, vgpr0
, ctx
->i32_0
, "");
340 vgpr
= LLVMBuildBitCast(builder
, vgpr
, vgpr_type
, "");
347 ac_build_ballot(struct ac_llvm_context
*ctx
,
350 LLVMValueRef args
[3] = {
353 LLVMConstInt(ctx
->i32
, LLVMIntNE
, 0)
356 /* We currently have no other way to prevent LLVM from lifting the icmp
357 * calls to a dominating basic block.
359 ac_build_optimization_barrier(ctx
, &args
[0]);
361 if (LLVMTypeOf(args
[0]) != ctx
->i32
)
362 args
[0] = LLVMBuildBitCast(ctx
->builder
, args
[0], ctx
->i32
, "");
364 return ac_build_intrinsic(ctx
,
365 "llvm.amdgcn.icmp.i32",
367 AC_FUNC_ATTR_NOUNWIND
|
368 AC_FUNC_ATTR_READNONE
|
369 AC_FUNC_ATTR_CONVERGENT
);
373 ac_build_vote_all(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
375 LLVMValueRef active_set
= ac_build_ballot(ctx
, ctx
->i32_1
);
376 LLVMValueRef vote_set
= ac_build_ballot(ctx
, value
);
377 return LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, vote_set
, active_set
, "");
381 ac_build_vote_any(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
383 LLVMValueRef vote_set
= ac_build_ballot(ctx
, value
);
384 return LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, vote_set
,
385 LLVMConstInt(ctx
->i64
, 0, 0), "");
389 ac_build_vote_eq(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
391 LLVMValueRef active_set
= ac_build_ballot(ctx
, ctx
->i32_1
);
392 LLVMValueRef vote_set
= ac_build_ballot(ctx
, value
);
394 LLVMValueRef all
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
395 vote_set
, active_set
, "");
396 LLVMValueRef none
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
398 LLVMConstInt(ctx
->i64
, 0, 0), "");
399 return LLVMBuildOr(ctx
->builder
, all
, none
, "");
403 ac_build_varying_gather_values(struct ac_llvm_context
*ctx
, LLVMValueRef
*values
,
404 unsigned value_count
, unsigned component
)
406 LLVMValueRef vec
= NULL
;
408 if (value_count
== 1) {
409 return values
[component
];
410 } else if (!value_count
)
411 unreachable("value_count is 0");
413 for (unsigned i
= component
; i
< value_count
+ component
; i
++) {
414 LLVMValueRef value
= values
[i
];
417 vec
= LLVMGetUndef( LLVMVectorType(LLVMTypeOf(value
), value_count
));
418 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, i
- component
, false);
419 vec
= LLVMBuildInsertElement(ctx
->builder
, vec
, value
, index
, "");
425 ac_build_gather_values_extended(struct ac_llvm_context
*ctx
,
426 LLVMValueRef
*values
,
427 unsigned value_count
,
428 unsigned value_stride
,
432 LLVMBuilderRef builder
= ctx
->builder
;
433 LLVMValueRef vec
= NULL
;
436 if (value_count
== 1 && !always_vector
) {
438 return LLVMBuildLoad(builder
, values
[0], "");
440 } else if (!value_count
)
441 unreachable("value_count is 0");
443 for (i
= 0; i
< value_count
; i
++) {
444 LLVMValueRef value
= values
[i
* value_stride
];
446 value
= LLVMBuildLoad(builder
, value
, "");
449 vec
= LLVMGetUndef( LLVMVectorType(LLVMTypeOf(value
), value_count
));
450 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, i
, false);
451 vec
= LLVMBuildInsertElement(builder
, vec
, value
, index
, "");
457 ac_build_gather_values(struct ac_llvm_context
*ctx
,
458 LLVMValueRef
*values
,
459 unsigned value_count
)
461 return ac_build_gather_values_extended(ctx
, values
, value_count
, 1, false, false);
465 ac_build_fdiv(struct ac_llvm_context
*ctx
,
469 LLVMValueRef ret
= LLVMBuildFDiv(ctx
->builder
, num
, den
, "");
471 /* Use v_rcp_f32 instead of precise division. */
472 if (!LLVMIsConstant(ret
))
473 LLVMSetMetadata(ret
, ctx
->fpmath_md_kind
, ctx
->fpmath_md_2p5_ulp
);
477 /* Coordinates for cube map selection. sc, tc, and ma are as in Table 8.27
478 * of the OpenGL 4.5 (Compatibility Profile) specification, except ma is
479 * already multiplied by two. id is the cube face number.
481 struct cube_selection_coords
{
488 build_cube_intrinsic(struct ac_llvm_context
*ctx
,
490 struct cube_selection_coords
*out
)
492 LLVMTypeRef f32
= ctx
->f32
;
494 out
->stc
[1] = ac_build_intrinsic(ctx
, "llvm.amdgcn.cubetc",
495 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
496 out
->stc
[0] = ac_build_intrinsic(ctx
, "llvm.amdgcn.cubesc",
497 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
498 out
->ma
= ac_build_intrinsic(ctx
, "llvm.amdgcn.cubema",
499 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
500 out
->id
= ac_build_intrinsic(ctx
, "llvm.amdgcn.cubeid",
501 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
505 * Build a manual selection sequence for cube face sc/tc coordinates and
506 * major axis vector (multiplied by 2 for consistency) for the given
507 * vec3 \p coords, for the face implied by \p selcoords.
509 * For the major axis, we always adjust the sign to be in the direction of
510 * selcoords.ma; i.e., a positive out_ma means that coords is pointed towards
511 * the selcoords major axis.
513 static void build_cube_select(struct ac_llvm_context
*ctx
,
514 const struct cube_selection_coords
*selcoords
,
515 const LLVMValueRef
*coords
,
516 LLVMValueRef
*out_st
,
517 LLVMValueRef
*out_ma
)
519 LLVMBuilderRef builder
= ctx
->builder
;
520 LLVMTypeRef f32
= LLVMTypeOf(coords
[0]);
521 LLVMValueRef is_ma_positive
;
523 LLVMValueRef is_ma_z
, is_not_ma_z
;
524 LLVMValueRef is_ma_y
;
525 LLVMValueRef is_ma_x
;
529 is_ma_positive
= LLVMBuildFCmp(builder
, LLVMRealUGE
,
530 selcoords
->ma
, LLVMConstReal(f32
, 0.0), "");
531 sgn_ma
= LLVMBuildSelect(builder
, is_ma_positive
,
532 LLVMConstReal(f32
, 1.0), LLVMConstReal(f32
, -1.0), "");
534 is_ma_z
= LLVMBuildFCmp(builder
, LLVMRealUGE
, selcoords
->id
, LLVMConstReal(f32
, 4.0), "");
535 is_not_ma_z
= LLVMBuildNot(builder
, is_ma_z
, "");
536 is_ma_y
= LLVMBuildAnd(builder
, is_not_ma_z
,
537 LLVMBuildFCmp(builder
, LLVMRealUGE
, selcoords
->id
, LLVMConstReal(f32
, 2.0), ""), "");
538 is_ma_x
= LLVMBuildAnd(builder
, is_not_ma_z
, LLVMBuildNot(builder
, is_ma_y
, ""), "");
541 tmp
= LLVMBuildSelect(builder
, is_ma_x
, coords
[2], coords
[0], "");
542 sgn
= LLVMBuildSelect(builder
, is_ma_y
, LLVMConstReal(f32
, 1.0),
543 LLVMBuildSelect(builder
, is_ma_z
, sgn_ma
,
544 LLVMBuildFNeg(builder
, sgn_ma
, ""), ""), "");
545 out_st
[0] = LLVMBuildFMul(builder
, tmp
, sgn
, "");
548 tmp
= LLVMBuildSelect(builder
, is_ma_y
, coords
[2], coords
[1], "");
549 sgn
= LLVMBuildSelect(builder
, is_ma_y
, sgn_ma
,
550 LLVMConstReal(f32
, -1.0), "");
551 out_st
[1] = LLVMBuildFMul(builder
, tmp
, sgn
, "");
554 tmp
= LLVMBuildSelect(builder
, is_ma_z
, coords
[2],
555 LLVMBuildSelect(builder
, is_ma_y
, coords
[1], coords
[0], ""), "");
556 tmp
= ac_build_intrinsic(ctx
, "llvm.fabs.f32",
557 ctx
->f32
, &tmp
, 1, AC_FUNC_ATTR_READNONE
);
558 *out_ma
= LLVMBuildFMul(builder
, tmp
, LLVMConstReal(f32
, 2.0), "");
562 ac_prepare_cube_coords(struct ac_llvm_context
*ctx
,
563 bool is_deriv
, bool is_array
, bool is_lod
,
564 LLVMValueRef
*coords_arg
,
565 LLVMValueRef
*derivs_arg
)
568 LLVMBuilderRef builder
= ctx
->builder
;
569 struct cube_selection_coords selcoords
;
570 LLVMValueRef coords
[3];
573 if (is_array
&& !is_lod
) {
574 LLVMValueRef tmp
= coords_arg
[3];
575 tmp
= ac_build_intrinsic(ctx
, "llvm.rint.f32", ctx
->f32
, &tmp
, 1, 0);
577 /* Section 8.9 (Texture Functions) of the GLSL 4.50 spec says:
579 * "For Array forms, the array layer used will be
581 * max(0, min(d−1, floor(layer+0.5)))
583 * where d is the depth of the texture array and layer
584 * comes from the component indicated in the tables below.
585 * Workaroudn for an issue where the layer is taken from a
586 * helper invocation which happens to fall on a different
587 * layer due to extrapolation."
589 * VI and earlier attempt to implement this in hardware by
590 * clamping the value of coords[2] = (8 * layer) + face.
591 * Unfortunately, this means that the we end up with the wrong
592 * face when clamping occurs.
594 * Clamp the layer earlier to work around the issue.
596 if (ctx
->chip_class
<= VI
) {
598 ge0
= LLVMBuildFCmp(builder
, LLVMRealOGE
, tmp
, ctx
->f32_0
, "");
599 tmp
= LLVMBuildSelect(builder
, ge0
, tmp
, ctx
->f32_0
, "");
605 build_cube_intrinsic(ctx
, coords_arg
, &selcoords
);
607 invma
= ac_build_intrinsic(ctx
, "llvm.fabs.f32",
608 ctx
->f32
, &selcoords
.ma
, 1, AC_FUNC_ATTR_READNONE
);
609 invma
= ac_build_fdiv(ctx
, LLVMConstReal(ctx
->f32
, 1.0), invma
);
611 for (int i
= 0; i
< 2; ++i
)
612 coords
[i
] = LLVMBuildFMul(builder
, selcoords
.stc
[i
], invma
, "");
614 coords
[2] = selcoords
.id
;
616 if (is_deriv
&& derivs_arg
) {
617 LLVMValueRef derivs
[4];
620 /* Convert cube derivatives to 2D derivatives. */
621 for (axis
= 0; axis
< 2; axis
++) {
622 LLVMValueRef deriv_st
[2];
623 LLVMValueRef deriv_ma
;
625 /* Transform the derivative alongside the texture
626 * coordinate. Mathematically, the correct formula is
627 * as follows. Assume we're projecting onto the +Z face
628 * and denote by dx/dh the derivative of the (original)
629 * X texture coordinate with respect to horizontal
630 * window coordinates. The projection onto the +Z face
635 * Then df/dh = df/dx * dx/dh + df/dz * dz/dh
636 * = 1/z * dx/dh - x/z * 1/z * dz/dh.
638 * This motivatives the implementation below.
640 * Whether this actually gives the expected results for
641 * apps that might feed in derivatives obtained via
642 * finite differences is anyone's guess. The OpenGL spec
643 * seems awfully quiet about how textureGrad for cube
644 * maps should be handled.
646 build_cube_select(ctx
, &selcoords
, &derivs_arg
[axis
* 3],
647 deriv_st
, &deriv_ma
);
649 deriv_ma
= LLVMBuildFMul(builder
, deriv_ma
, invma
, "");
651 for (int i
= 0; i
< 2; ++i
)
652 derivs
[axis
* 2 + i
] =
653 LLVMBuildFSub(builder
,
654 LLVMBuildFMul(builder
, deriv_st
[i
], invma
, ""),
655 LLVMBuildFMul(builder
, deriv_ma
, coords
[i
], ""), "");
658 memcpy(derivs_arg
, derivs
, sizeof(derivs
));
661 /* Shift the texture coordinate. This must be applied after the
662 * derivative calculation.
664 for (int i
= 0; i
< 2; ++i
)
665 coords
[i
] = LLVMBuildFAdd(builder
, coords
[i
], LLVMConstReal(ctx
->f32
, 1.5), "");
668 /* for cube arrays coord.z = coord.w(array_index) * 8 + face */
669 /* coords_arg.w component - array_index for cube arrays */
670 LLVMValueRef tmp
= LLVMBuildFMul(ctx
->builder
, coords_arg
[3], LLVMConstReal(ctx
->f32
, 8.0), "");
671 coords
[2] = LLVMBuildFAdd(ctx
->builder
, tmp
, coords
[2], "");
674 memcpy(coords_arg
, coords
, sizeof(coords
));
679 ac_build_fs_interp(struct ac_llvm_context
*ctx
,
680 LLVMValueRef llvm_chan
,
681 LLVMValueRef attr_number
,
686 LLVMValueRef args
[5];
689 if (HAVE_LLVM
< 0x0400) {
691 ij
[0] = LLVMBuildBitCast(ctx
->builder
, i
, ctx
->i32
, "");
692 ij
[1] = LLVMBuildBitCast(ctx
->builder
, j
, ctx
->i32
, "");
695 args
[1] = attr_number
;
697 args
[3] = ac_build_gather_values(ctx
, ij
, 2);
698 return ac_build_intrinsic(ctx
, "llvm.SI.fs.interp",
700 AC_FUNC_ATTR_READNONE
);
705 args
[2] = attr_number
;
708 p1
= ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p1",
709 ctx
->f32
, args
, 4, AC_FUNC_ATTR_READNONE
);
714 args
[3] = attr_number
;
717 return ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p2",
718 ctx
->f32
, args
, 5, AC_FUNC_ATTR_READNONE
);
722 ac_build_fs_interp_mov(struct ac_llvm_context
*ctx
,
723 LLVMValueRef parameter
,
724 LLVMValueRef llvm_chan
,
725 LLVMValueRef attr_number
,
728 LLVMValueRef args
[4];
729 if (HAVE_LLVM
< 0x0400) {
731 args
[1] = attr_number
;
734 return ac_build_intrinsic(ctx
,
735 "llvm.SI.fs.constant",
737 AC_FUNC_ATTR_READNONE
);
742 args
[2] = attr_number
;
745 return ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.mov",
746 ctx
->f32
, args
, 4, AC_FUNC_ATTR_READNONE
);
750 ac_build_gep0(struct ac_llvm_context
*ctx
,
751 LLVMValueRef base_ptr
,
754 LLVMValueRef indices
[2] = {
755 LLVMConstInt(ctx
->i32
, 0, 0),
758 return LLVMBuildGEP(ctx
->builder
, base_ptr
,
763 ac_build_indexed_store(struct ac_llvm_context
*ctx
,
764 LLVMValueRef base_ptr
, LLVMValueRef index
,
767 LLVMBuildStore(ctx
->builder
, value
,
768 ac_build_gep0(ctx
, base_ptr
, index
));
772 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad.
773 * It's equivalent to doing a load from &base_ptr[index].
775 * \param base_ptr Where the array starts.
776 * \param index The element index into the array.
777 * \param uniform Whether the base_ptr and index can be assumed to be
778 * dynamically uniform (i.e. load to an SGPR)
779 * \param invariant Whether the load is invariant (no other opcodes affect it)
782 ac_build_load_custom(struct ac_llvm_context
*ctx
, LLVMValueRef base_ptr
,
783 LLVMValueRef index
, bool uniform
, bool invariant
)
785 LLVMValueRef pointer
, result
;
787 pointer
= ac_build_gep0(ctx
, base_ptr
, index
);
789 LLVMSetMetadata(pointer
, ctx
->uniform_md_kind
, ctx
->empty_md
);
790 result
= LLVMBuildLoad(ctx
->builder
, pointer
, "");
792 LLVMSetMetadata(result
, ctx
->invariant_load_md_kind
, ctx
->empty_md
);
796 LLVMValueRef
ac_build_load(struct ac_llvm_context
*ctx
, LLVMValueRef base_ptr
,
799 return ac_build_load_custom(ctx
, base_ptr
, index
, false, false);
802 LLVMValueRef
ac_build_load_invariant(struct ac_llvm_context
*ctx
,
803 LLVMValueRef base_ptr
, LLVMValueRef index
)
805 return ac_build_load_custom(ctx
, base_ptr
, index
, false, true);
808 LLVMValueRef
ac_build_load_to_sgpr(struct ac_llvm_context
*ctx
,
809 LLVMValueRef base_ptr
, LLVMValueRef index
)
811 return ac_build_load_custom(ctx
, base_ptr
, index
, true, true);
814 /* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
815 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
816 * or v4i32 (num_channels=3,4).
819 ac_build_buffer_store_dword(struct ac_llvm_context
*ctx
,
822 unsigned num_channels
,
823 LLVMValueRef voffset
,
824 LLVMValueRef soffset
,
825 unsigned inst_offset
,
828 bool writeonly_memory
,
829 bool swizzle_enable_hint
)
831 /* SWIZZLE_ENABLE requires that soffset isn't folded into voffset
832 * (voffset is swizzled, but soffset isn't swizzled).
833 * llvm.amdgcn.buffer.store doesn't have a separate soffset parameter.
835 if (!swizzle_enable_hint
) {
836 /* Split 3 channel stores, becase LLVM doesn't support 3-channel
838 if (num_channels
== 3) {
839 LLVMValueRef v
[3], v01
;
841 for (int i
= 0; i
< 3; i
++) {
842 v
[i
] = LLVMBuildExtractElement(ctx
->builder
, vdata
,
843 LLVMConstInt(ctx
->i32
, i
, 0), "");
845 v01
= ac_build_gather_values(ctx
, v
, 2);
847 ac_build_buffer_store_dword(ctx
, rsrc
, v01
, 2, voffset
,
848 soffset
, inst_offset
, glc
, slc
,
849 writeonly_memory
, swizzle_enable_hint
);
850 ac_build_buffer_store_dword(ctx
, rsrc
, v
[2], 1, voffset
,
851 soffset
, inst_offset
+ 8,
853 writeonly_memory
, swizzle_enable_hint
);
857 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
858 static const char *types
[] = {"f32", "v2f32", "v4f32"};
860 LLVMValueRef offset
= soffset
;
863 offset
= LLVMBuildAdd(ctx
->builder
, offset
,
864 LLVMConstInt(ctx
->i32
, inst_offset
, 0), "");
866 offset
= LLVMBuildAdd(ctx
->builder
, offset
, voffset
, "");
868 LLVMValueRef args
[] = {
869 ac_to_float(ctx
, vdata
),
870 LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, ""),
871 LLVMConstInt(ctx
->i32
, 0, 0),
873 LLVMConstInt(ctx
->i1
, glc
, 0),
874 LLVMConstInt(ctx
->i1
, slc
, 0),
877 snprintf(name
, sizeof(name
), "llvm.amdgcn.buffer.store.%s",
880 ac_build_intrinsic(ctx
, name
, ctx
->voidt
,
881 args
, ARRAY_SIZE(args
),
883 AC_FUNC_ATTR_INACCESSIBLE_MEM_ONLY
:
884 AC_FUNC_ATTR_WRITEONLY
);
888 static unsigned dfmt
[] = {
889 V_008F0C_BUF_DATA_FORMAT_32
,
890 V_008F0C_BUF_DATA_FORMAT_32_32
,
891 V_008F0C_BUF_DATA_FORMAT_32_32_32
,
892 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
894 assert(num_channels
>= 1 && num_channels
<= 4);
896 LLVMValueRef args
[] = {
899 LLVMConstInt(ctx
->i32
, num_channels
, 0),
900 voffset
? voffset
: LLVMGetUndef(ctx
->i32
),
902 LLVMConstInt(ctx
->i32
, inst_offset
, 0),
903 LLVMConstInt(ctx
->i32
, dfmt
[num_channels
- 1], 0),
904 LLVMConstInt(ctx
->i32
, V_008F0C_BUF_NUM_FORMAT_UINT
, 0),
905 LLVMConstInt(ctx
->i32
, voffset
!= NULL
, 0),
906 LLVMConstInt(ctx
->i32
, 0, 0), /* idxen */
907 LLVMConstInt(ctx
->i32
, glc
, 0),
908 LLVMConstInt(ctx
->i32
, slc
, 0),
909 LLVMConstInt(ctx
->i32
, 0, 0), /* tfe*/
912 /* The instruction offset field has 12 bits */
913 assert(voffset
|| inst_offset
< (1 << 12));
915 /* The intrinsic is overloaded, we need to add a type suffix for overloading to work. */
916 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
917 const char *types
[] = {"i32", "v2i32", "v4i32"};
919 snprintf(name
, sizeof(name
), "llvm.SI.tbuffer.store.%s", types
[func
]);
921 ac_build_intrinsic(ctx
, name
, ctx
->voidt
,
922 args
, ARRAY_SIZE(args
),
923 AC_FUNC_ATTR_LEGACY
);
927 ac_build_buffer_load_common(struct ac_llvm_context
*ctx
,
930 LLVMValueRef voffset
,
931 unsigned num_channels
,
937 LLVMValueRef args
[] = {
938 LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, ""),
939 vindex
? vindex
: LLVMConstInt(ctx
->i32
, 0, 0),
941 LLVMConstInt(ctx
->i1
, glc
, 0),
942 LLVMConstInt(ctx
->i1
, slc
, 0)
944 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
946 LLVMTypeRef types
[] = {ctx
->f32
, ctx
->v2f32
, ctx
->v4f32
};
947 const char *type_names
[] = {"f32", "v2f32", "v4f32"};
951 snprintf(name
, sizeof(name
), "llvm.amdgcn.buffer.load.format.%s",
954 snprintf(name
, sizeof(name
), "llvm.amdgcn.buffer.load.%s",
958 return ac_build_intrinsic(ctx
, name
, types
[func
], args
,
960 ac_get_load_intr_attribs(can_speculate
));
964 ac_build_buffer_load(struct ac_llvm_context
*ctx
,
968 LLVMValueRef voffset
,
969 LLVMValueRef soffset
,
970 unsigned inst_offset
,
976 LLVMValueRef offset
= LLVMConstInt(ctx
->i32
, inst_offset
, 0);
978 offset
= LLVMBuildAdd(ctx
->builder
, offset
, voffset
, "");
980 offset
= LLVMBuildAdd(ctx
->builder
, offset
, soffset
, "");
982 /* TODO: VI and later generations can use SMEM with GLC=1.*/
983 if (allow_smem
&& !glc
&& !slc
) {
984 assert(vindex
== NULL
);
986 LLVMValueRef result
[8];
988 for (int i
= 0; i
< num_channels
; i
++) {
990 offset
= LLVMBuildAdd(ctx
->builder
, offset
,
991 LLVMConstInt(ctx
->i32
, 4, 0), "");
993 LLVMValueRef args
[2] = {rsrc
, offset
};
994 result
[i
] = ac_build_intrinsic(ctx
, "llvm.SI.load.const.v4i32",
996 AC_FUNC_ATTR_READNONE
|
997 AC_FUNC_ATTR_LEGACY
);
999 if (num_channels
== 1)
1002 if (num_channels
== 3)
1003 result
[num_channels
++] = LLVMGetUndef(ctx
->f32
);
1004 return ac_build_gather_values(ctx
, result
, num_channels
);
1007 return ac_build_buffer_load_common(ctx
, rsrc
, vindex
, offset
,
1008 num_channels
, glc
, slc
,
1009 can_speculate
, false);
1012 LLVMValueRef
ac_build_buffer_load_format(struct ac_llvm_context
*ctx
,
1014 LLVMValueRef vindex
,
1015 LLVMValueRef voffset
,
1016 unsigned num_channels
,
1019 return ac_build_buffer_load_common(ctx
, rsrc
, vindex
, voffset
,
1020 num_channels
, false, false,
1021 can_speculate
, true);
1025 * Set range metadata on an instruction. This can only be used on load and
1026 * call instructions. If you know an instruction can only produce the values
1027 * 0, 1, 2, you would do set_range_metadata(value, 0, 3);
1028 * \p lo is the minimum value inclusive.
1029 * \p hi is the maximum value exclusive.
1031 static void set_range_metadata(struct ac_llvm_context
*ctx
,
1032 LLVMValueRef value
, unsigned lo
, unsigned hi
)
1034 LLVMValueRef range_md
, md_args
[2];
1035 LLVMTypeRef type
= LLVMTypeOf(value
);
1036 LLVMContextRef context
= LLVMGetTypeContext(type
);
1038 md_args
[0] = LLVMConstInt(type
, lo
, false);
1039 md_args
[1] = LLVMConstInt(type
, hi
, false);
1040 range_md
= LLVMMDNodeInContext(context
, md_args
, 2);
1041 LLVMSetMetadata(value
, ctx
->range_md_kind
, range_md
);
1045 ac_get_thread_id(struct ac_llvm_context
*ctx
)
1049 LLVMValueRef tid_args
[2];
1050 tid_args
[0] = LLVMConstInt(ctx
->i32
, 0xffffffff, false);
1051 tid_args
[1] = LLVMConstInt(ctx
->i32
, 0, false);
1052 tid_args
[1] = ac_build_intrinsic(ctx
,
1053 "llvm.amdgcn.mbcnt.lo", ctx
->i32
,
1054 tid_args
, 2, AC_FUNC_ATTR_READNONE
);
1056 tid
= ac_build_intrinsic(ctx
, "llvm.amdgcn.mbcnt.hi",
1058 2, AC_FUNC_ATTR_READNONE
);
1059 set_range_metadata(ctx
, tid
, 0, 64);
1064 * SI implements derivatives using the local data store (LDS)
1065 * All writes to the LDS happen in all executing threads at
1066 * the same time. TID is the Thread ID for the current
1067 * thread and is a value between 0 and 63, representing
1068 * the thread's position in the wavefront.
1070 * For the pixel shader threads are grouped into quads of four pixels.
1071 * The TIDs of the pixels of a quad are:
1079 * So, masking the TID with 0xfffffffc yields the TID of the top left pixel
1080 * of the quad, masking with 0xfffffffd yields the TID of the top pixel of
1081 * the current pixel's column, and masking with 0xfffffffe yields the TID
1082 * of the left pixel of the current pixel's row.
1084 * Adding 1 yields the TID of the pixel to the right of the left pixel, and
1085 * adding 2 yields the TID of the pixel below the top pixel.
1088 ac_build_ddxy(struct ac_llvm_context
*ctx
,
1093 LLVMValueRef tl
, trbl
, args
[2];
1094 LLVMValueRef result
;
1096 if (ctx
->chip_class
>= VI
) {
1097 LLVMValueRef thread_id
, tl_tid
, trbl_tid
;
1098 thread_id
= ac_get_thread_id(ctx
);
1100 tl_tid
= LLVMBuildAnd(ctx
->builder
, thread_id
,
1101 LLVMConstInt(ctx
->i32
, mask
, false), "");
1103 trbl_tid
= LLVMBuildAdd(ctx
->builder
, tl_tid
,
1104 LLVMConstInt(ctx
->i32
, idx
, false), "");
1106 args
[0] = LLVMBuildMul(ctx
->builder
, tl_tid
,
1107 LLVMConstInt(ctx
->i32
, 4, false), "");
1109 tl
= ac_build_intrinsic(ctx
,
1110 "llvm.amdgcn.ds.bpermute", ctx
->i32
,
1112 AC_FUNC_ATTR_READNONE
|
1113 AC_FUNC_ATTR_CONVERGENT
);
1115 args
[0] = LLVMBuildMul(ctx
->builder
, trbl_tid
,
1116 LLVMConstInt(ctx
->i32
, 4, false), "");
1117 trbl
= ac_build_intrinsic(ctx
,
1118 "llvm.amdgcn.ds.bpermute", ctx
->i32
,
1120 AC_FUNC_ATTR_READNONE
|
1121 AC_FUNC_ATTR_CONVERGENT
);
1123 uint32_t masks
[2] = {};
1126 case AC_TID_MASK_TOP_LEFT
:
1134 case AC_TID_MASK_TOP
:
1138 case AC_TID_MASK_LEFT
:
1147 args
[1] = LLVMConstInt(ctx
->i32
, masks
[0], false);
1149 tl
= ac_build_intrinsic(ctx
,
1150 "llvm.amdgcn.ds.swizzle", ctx
->i32
,
1152 AC_FUNC_ATTR_READNONE
|
1153 AC_FUNC_ATTR_CONVERGENT
);
1155 args
[1] = LLVMConstInt(ctx
->i32
, masks
[1], false);
1156 trbl
= ac_build_intrinsic(ctx
,
1157 "llvm.amdgcn.ds.swizzle", ctx
->i32
,
1159 AC_FUNC_ATTR_READNONE
|
1160 AC_FUNC_ATTR_CONVERGENT
);
1163 tl
= LLVMBuildBitCast(ctx
->builder
, tl
, ctx
->f32
, "");
1164 trbl
= LLVMBuildBitCast(ctx
->builder
, trbl
, ctx
->f32
, "");
1165 result
= LLVMBuildFSub(ctx
->builder
, trbl
, tl
, "");
1170 ac_build_sendmsg(struct ac_llvm_context
*ctx
,
1172 LLVMValueRef wave_id
)
1174 LLVMValueRef args
[2];
1175 const char *intr_name
= (HAVE_LLVM
< 0x0400) ? "llvm.SI.sendmsg" : "llvm.amdgcn.s.sendmsg";
1176 args
[0] = LLVMConstInt(ctx
->i32
, msg
, false);
1178 ac_build_intrinsic(ctx
, intr_name
, ctx
->voidt
, args
, 2, 0);
1182 ac_build_imsb(struct ac_llvm_context
*ctx
,
1184 LLVMTypeRef dst_type
)
1186 const char *intr_name
= (HAVE_LLVM
< 0x0400) ? "llvm.AMDGPU.flbit.i32" :
1187 "llvm.amdgcn.sffbh.i32";
1188 LLVMValueRef msb
= ac_build_intrinsic(ctx
, intr_name
,
1190 AC_FUNC_ATTR_READNONE
);
1192 /* The HW returns the last bit index from MSB, but NIR/TGSI wants
1193 * the index from LSB. Invert it by doing "31 - msb". */
1194 msb
= LLVMBuildSub(ctx
->builder
, LLVMConstInt(ctx
->i32
, 31, false),
1197 LLVMValueRef all_ones
= LLVMConstInt(ctx
->i32
, -1, true);
1198 LLVMValueRef cond
= LLVMBuildOr(ctx
->builder
,
1199 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
1200 arg
, LLVMConstInt(ctx
->i32
, 0, 0), ""),
1201 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
1202 arg
, all_ones
, ""), "");
1204 return LLVMBuildSelect(ctx
->builder
, cond
, all_ones
, msb
, "");
1208 ac_build_umsb(struct ac_llvm_context
*ctx
,
1210 LLVMTypeRef dst_type
)
1212 LLVMValueRef args
[2] = {
1216 LLVMValueRef msb
= ac_build_intrinsic(ctx
, "llvm.ctlz.i32",
1217 dst_type
, args
, ARRAY_SIZE(args
),
1218 AC_FUNC_ATTR_READNONE
);
1220 /* The HW returns the last bit index from MSB, but TGSI/NIR wants
1221 * the index from LSB. Invert it by doing "31 - msb". */
1222 msb
= LLVMBuildSub(ctx
->builder
, LLVMConstInt(ctx
->i32
, 31, false),
1225 /* check for zero */
1226 return LLVMBuildSelect(ctx
->builder
,
1227 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, arg
,
1228 LLVMConstInt(ctx
->i32
, 0, 0), ""),
1229 LLVMConstInt(ctx
->i32
, -1, true), msb
, "");
1232 LLVMValueRef
ac_build_fmin(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
1235 LLVMValueRef args
[2] = {a
, b
};
1236 return ac_build_intrinsic(ctx
, "llvm.minnum.f32", ctx
->f32
, args
, 2,
1237 AC_FUNC_ATTR_READNONE
);
1240 LLVMValueRef
ac_build_fmax(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
1243 LLVMValueRef args
[2] = {a
, b
};
1244 return ac_build_intrinsic(ctx
, "llvm.maxnum.f32", ctx
->f32
, args
, 2,
1245 AC_FUNC_ATTR_READNONE
);
1248 LLVMValueRef
ac_build_umin(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
1251 LLVMValueRef cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntULE
, a
, b
, "");
1252 return LLVMBuildSelect(ctx
->builder
, cmp
, a
, b
, "");
1255 LLVMValueRef
ac_build_clamp(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
1257 if (HAVE_LLVM
>= 0x0500) {
1258 return ac_build_fmin(ctx
, ac_build_fmax(ctx
, value
, ctx
->f32_0
),
1262 LLVMValueRef args
[3] = {
1264 LLVMConstReal(ctx
->f32
, 0),
1265 LLVMConstReal(ctx
->f32
, 1),
1268 return ac_build_intrinsic(ctx
, "llvm.AMDGPU.clamp.", ctx
->f32
, args
, 3,
1269 AC_FUNC_ATTR_READNONE
|
1270 AC_FUNC_ATTR_LEGACY
);
1273 void ac_build_export(struct ac_llvm_context
*ctx
, struct ac_export_args
*a
)
1275 LLVMValueRef args
[9];
1277 if (HAVE_LLVM
>= 0x0500) {
1278 args
[0] = LLVMConstInt(ctx
->i32
, a
->target
, 0);
1279 args
[1] = LLVMConstInt(ctx
->i32
, a
->enabled_channels
, 0);
1282 LLVMTypeRef i16
= LLVMInt16TypeInContext(ctx
->context
);
1283 LLVMTypeRef v2i16
= LLVMVectorType(i16
, 2);
1285 args
[2] = LLVMBuildBitCast(ctx
->builder
, a
->out
[0],
1287 args
[3] = LLVMBuildBitCast(ctx
->builder
, a
->out
[1],
1289 args
[4] = LLVMConstInt(ctx
->i1
, a
->done
, 0);
1290 args
[5] = LLVMConstInt(ctx
->i1
, a
->valid_mask
, 0);
1292 ac_build_intrinsic(ctx
, "llvm.amdgcn.exp.compr.v2i16",
1293 ctx
->voidt
, args
, 6, 0);
1295 args
[2] = a
->out
[0];
1296 args
[3] = a
->out
[1];
1297 args
[4] = a
->out
[2];
1298 args
[5] = a
->out
[3];
1299 args
[6] = LLVMConstInt(ctx
->i1
, a
->done
, 0);
1300 args
[7] = LLVMConstInt(ctx
->i1
, a
->valid_mask
, 0);
1302 ac_build_intrinsic(ctx
, "llvm.amdgcn.exp.f32",
1303 ctx
->voidt
, args
, 8, 0);
1308 args
[0] = LLVMConstInt(ctx
->i32
, a
->enabled_channels
, 0);
1309 args
[1] = LLVMConstInt(ctx
->i32
, a
->valid_mask
, 0);
1310 args
[2] = LLVMConstInt(ctx
->i32
, a
->done
, 0);
1311 args
[3] = LLVMConstInt(ctx
->i32
, a
->target
, 0);
1312 args
[4] = LLVMConstInt(ctx
->i32
, a
->compr
, 0);
1313 memcpy(args
+ 5, a
->out
, sizeof(a
->out
[0]) * 4);
1315 ac_build_intrinsic(ctx
, "llvm.SI.export", ctx
->voidt
, args
, 9,
1316 AC_FUNC_ATTR_LEGACY
);
1319 LLVMValueRef
ac_build_image_opcode(struct ac_llvm_context
*ctx
,
1320 struct ac_image_args
*a
)
1322 LLVMTypeRef dst_type
;
1323 LLVMValueRef args
[11];
1324 unsigned num_args
= 0;
1325 const char *name
= NULL
;
1326 char intr_name
[128], type
[64];
1328 if (HAVE_LLVM
>= 0x0400) {
1329 bool sample
= a
->opcode
== ac_image_sample
||
1330 a
->opcode
== ac_image_gather4
||
1331 a
->opcode
== ac_image_get_lod
;
1334 args
[num_args
++] = ac_to_float(ctx
, a
->addr
);
1336 args
[num_args
++] = a
->addr
;
1338 args
[num_args
++] = a
->resource
;
1340 args
[num_args
++] = a
->sampler
;
1341 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->dmask
, 0);
1343 args
[num_args
++] = LLVMConstInt(ctx
->i1
, a
->unorm
, 0);
1344 args
[num_args
++] = ctx
->i1false
; /* glc */
1345 args
[num_args
++] = ctx
->i1false
; /* slc */
1346 args
[num_args
++] = ctx
->i1false
; /* lwe */
1347 args
[num_args
++] = LLVMConstInt(ctx
->i1
, a
->da
, 0);
1349 switch (a
->opcode
) {
1350 case ac_image_sample
:
1351 name
= "llvm.amdgcn.image.sample";
1353 case ac_image_gather4
:
1354 name
= "llvm.amdgcn.image.gather4";
1357 name
= "llvm.amdgcn.image.load";
1359 case ac_image_load_mip
:
1360 name
= "llvm.amdgcn.image.load.mip";
1362 case ac_image_get_lod
:
1363 name
= "llvm.amdgcn.image.getlod";
1365 case ac_image_get_resinfo
:
1366 name
= "llvm.amdgcn.image.getresinfo";
1369 unreachable("invalid image opcode");
1372 ac_build_type_name_for_intr(LLVMTypeOf(args
[0]), type
,
1375 snprintf(intr_name
, sizeof(intr_name
), "%s%s%s%s.v4f32.%s.v8i32",
1377 a
->compare
? ".c" : "",
1381 a
->level_zero
? ".lz" : "",
1382 a
->offset
? ".o" : "",
1385 LLVMValueRef result
=
1386 ac_build_intrinsic(ctx
, intr_name
,
1387 ctx
->v4f32
, args
, num_args
,
1388 AC_FUNC_ATTR_READNONE
);
1390 result
= LLVMBuildBitCast(ctx
->builder
, result
,
1396 args
[num_args
++] = a
->addr
;
1397 args
[num_args
++] = a
->resource
;
1399 if (a
->opcode
== ac_image_load
||
1400 a
->opcode
== ac_image_load_mip
||
1401 a
->opcode
== ac_image_get_resinfo
) {
1402 dst_type
= ctx
->v4i32
;
1404 dst_type
= ctx
->v4f32
;
1405 args
[num_args
++] = a
->sampler
;
1408 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->dmask
, 0);
1409 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->unorm
, 0);
1410 args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* r128 */
1411 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->da
, 0);
1412 args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* glc */
1413 args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* slc */
1414 args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* tfe */
1415 args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* lwe */
1417 switch (a
->opcode
) {
1418 case ac_image_sample
:
1419 name
= "llvm.SI.image.sample";
1421 case ac_image_gather4
:
1422 name
= "llvm.SI.gather4";
1425 name
= "llvm.SI.image.load";
1427 case ac_image_load_mip
:
1428 name
= "llvm.SI.image.load.mip";
1430 case ac_image_get_lod
:
1431 name
= "llvm.SI.getlod";
1433 case ac_image_get_resinfo
:
1434 name
= "llvm.SI.getresinfo";
1438 ac_build_type_name_for_intr(LLVMTypeOf(a
->addr
), type
, sizeof(type
));
1439 snprintf(intr_name
, sizeof(intr_name
), "%s%s%s%s.%s",
1441 a
->compare
? ".c" : "",
1445 a
->level_zero
? ".lz" : "",
1446 a
->offset
? ".o" : "",
1449 return ac_build_intrinsic(ctx
, intr_name
,
1450 dst_type
, args
, num_args
,
1451 AC_FUNC_ATTR_READNONE
|
1452 AC_FUNC_ATTR_LEGACY
);
1455 LLVMValueRef
ac_build_cvt_pkrtz_f16(struct ac_llvm_context
*ctx
,
1456 LLVMValueRef args
[2])
1458 if (HAVE_LLVM
>= 0x0500) {
1460 LLVMVectorType(LLVMHalfTypeInContext(ctx
->context
), 2);
1462 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pkrtz",
1464 AC_FUNC_ATTR_READNONE
);
1465 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
1468 return ac_build_intrinsic(ctx
, "llvm.SI.packf16", ctx
->i32
, args
, 2,
1469 AC_FUNC_ATTR_READNONE
|
1470 AC_FUNC_ATTR_LEGACY
);
1473 LLVMValueRef
ac_build_wqm_vote(struct ac_llvm_context
*ctx
, LLVMValueRef i1
)
1475 assert(HAVE_LLVM
>= 0x0600);
1476 return ac_build_intrinsic(ctx
, "llvm.amdgcn.wqm.vote", ctx
->i1
,
1477 &i1
, 1, AC_FUNC_ATTR_READNONE
);
1480 void ac_build_kill_if_false(struct ac_llvm_context
*ctx
, LLVMValueRef i1
)
1482 if (HAVE_LLVM
>= 0x0600) {
1483 ac_build_intrinsic(ctx
, "llvm.amdgcn.kill", ctx
->voidt
,
1488 LLVMValueRef value
= LLVMBuildSelect(ctx
->builder
, i1
,
1489 LLVMConstReal(ctx
->f32
, 1),
1490 LLVMConstReal(ctx
->f32
, -1), "");
1491 ac_build_intrinsic(ctx
, "llvm.AMDGPU.kill", ctx
->voidt
,
1492 &value
, 1, AC_FUNC_ATTR_LEGACY
);
1495 LLVMValueRef
ac_build_bfe(struct ac_llvm_context
*ctx
, LLVMValueRef input
,
1496 LLVMValueRef offset
, LLVMValueRef width
,
1499 LLVMValueRef args
[] = {
1505 if (HAVE_LLVM
>= 0x0500) {
1506 return ac_build_intrinsic(ctx
,
1507 is_signed
? "llvm.amdgcn.sbfe.i32" :
1508 "llvm.amdgcn.ubfe.i32",
1510 AC_FUNC_ATTR_READNONE
);
1513 return ac_build_intrinsic(ctx
,
1514 is_signed
? "llvm.AMDGPU.bfe.i32" :
1515 "llvm.AMDGPU.bfe.u32",
1517 AC_FUNC_ATTR_READNONE
|
1518 AC_FUNC_ATTR_LEGACY
);
1521 void ac_build_waitcnt(struct ac_llvm_context
*ctx
, unsigned simm16
)
1523 LLVMValueRef args
[1] = {
1524 LLVMConstInt(ctx
->i32
, simm16
, false),
1526 ac_build_intrinsic(ctx
, "llvm.amdgcn.s.waitcnt",
1527 ctx
->voidt
, args
, 1, 0);
1530 void ac_get_image_intr_name(const char *base_name
,
1531 LLVMTypeRef data_type
,
1532 LLVMTypeRef coords_type
,
1533 LLVMTypeRef rsrc_type
,
1534 char *out_name
, unsigned out_len
)
1536 char coords_type_name
[8];
1538 ac_build_type_name_for_intr(coords_type
, coords_type_name
,
1539 sizeof(coords_type_name
));
1541 if (HAVE_LLVM
<= 0x0309) {
1542 snprintf(out_name
, out_len
, "%s.%s", base_name
, coords_type_name
);
1544 char data_type_name
[8];
1545 char rsrc_type_name
[8];
1547 ac_build_type_name_for_intr(data_type
, data_type_name
,
1548 sizeof(data_type_name
));
1549 ac_build_type_name_for_intr(rsrc_type
, rsrc_type_name
,
1550 sizeof(rsrc_type_name
));
1551 snprintf(out_name
, out_len
, "%s.%s.%s.%s", base_name
,
1552 data_type_name
, coords_type_name
, rsrc_type_name
);
1556 #define AC_EXP_TARGET (HAVE_LLVM >= 0x0500 ? 0 : 3)
1557 #define AC_EXP_OUT0 (HAVE_LLVM >= 0x0500 ? 2 : 5)
1565 struct ac_vs_exp_chan
1569 enum ac_ir_type type
;
1572 struct ac_vs_exp_inst
{
1575 struct ac_vs_exp_chan chan
[4];
1578 struct ac_vs_exports
{
1580 struct ac_vs_exp_inst exp
[VARYING_SLOT_MAX
];
1583 /* Return true if the PARAM export has been eliminated. */
1584 static bool ac_eliminate_const_output(uint8_t *vs_output_param_offset
,
1585 uint32_t num_outputs
,
1586 struct ac_vs_exp_inst
*exp
)
1588 unsigned i
, default_val
; /* SPI_PS_INPUT_CNTL_i.DEFAULT_VAL */
1589 bool is_zero
[4] = {}, is_one
[4] = {};
1591 for (i
= 0; i
< 4; i
++) {
1592 /* It's a constant expression. Undef outputs are eliminated too. */
1593 if (exp
->chan
[i
].type
== AC_IR_UNDEF
) {
1596 } else if (exp
->chan
[i
].type
== AC_IR_CONST
) {
1597 if (exp
->chan
[i
].const_float
== 0)
1599 else if (exp
->chan
[i
].const_float
== 1)
1602 return false; /* other constant */
1607 /* Only certain combinations of 0 and 1 can be eliminated. */
1608 if (is_zero
[0] && is_zero
[1] && is_zero
[2])
1609 default_val
= is_zero
[3] ? 0 : 1;
1610 else if (is_one
[0] && is_one
[1] && is_one
[2])
1611 default_val
= is_zero
[3] ? 2 : 3;
1615 /* The PARAM export can be represented as DEFAULT_VAL. Kill it. */
1616 LLVMInstructionEraseFromParent(exp
->inst
);
1618 /* Change OFFSET to DEFAULT_VAL. */
1619 for (i
= 0; i
< num_outputs
; i
++) {
1620 if (vs_output_param_offset
[i
] == exp
->offset
) {
1621 vs_output_param_offset
[i
] =
1622 AC_EXP_PARAM_DEFAULT_VAL_0000
+ default_val
;
1629 static bool ac_eliminate_duplicated_output(uint8_t *vs_output_param_offset
,
1630 uint32_t num_outputs
,
1631 struct ac_vs_exports
*processed
,
1632 struct ac_vs_exp_inst
*exp
)
1634 unsigned p
, copy_back_channels
= 0;
1636 /* See if the output is already in the list of processed outputs.
1637 * The LLVMValueRef comparison relies on SSA.
1639 for (p
= 0; p
< processed
->num
; p
++) {
1640 bool different
= false;
1642 for (unsigned j
= 0; j
< 4; j
++) {
1643 struct ac_vs_exp_chan
*c1
= &processed
->exp
[p
].chan
[j
];
1644 struct ac_vs_exp_chan
*c2
= &exp
->chan
[j
];
1646 /* Treat undef as a match. */
1647 if (c2
->type
== AC_IR_UNDEF
)
1650 /* If c1 is undef but c2 isn't, we can copy c2 to c1
1651 * and consider the instruction duplicated.
1653 if (c1
->type
== AC_IR_UNDEF
) {
1654 copy_back_channels
|= 1 << j
;
1658 /* Test whether the channels are not equal. */
1659 if (c1
->type
!= c2
->type
||
1660 (c1
->type
== AC_IR_CONST
&&
1661 c1
->const_float
!= c2
->const_float
) ||
1662 (c1
->type
== AC_IR_VALUE
&&
1663 c1
->value
!= c2
->value
)) {
1671 copy_back_channels
= 0;
1673 if (p
== processed
->num
)
1676 /* If a match was found, but the matching export has undef where the new
1677 * one has a normal value, copy the normal value to the undef channel.
1679 struct ac_vs_exp_inst
*match
= &processed
->exp
[p
];
1681 while (copy_back_channels
) {
1682 unsigned chan
= u_bit_scan(©_back_channels
);
1684 assert(match
->chan
[chan
].type
== AC_IR_UNDEF
);
1685 LLVMSetOperand(match
->inst
, AC_EXP_OUT0
+ chan
,
1686 exp
->chan
[chan
].value
);
1687 match
->chan
[chan
] = exp
->chan
[chan
];
1690 /* The PARAM export is duplicated. Kill it. */
1691 LLVMInstructionEraseFromParent(exp
->inst
);
1693 /* Change OFFSET to the matching export. */
1694 for (unsigned i
= 0; i
< num_outputs
; i
++) {
1695 if (vs_output_param_offset
[i
] == exp
->offset
) {
1696 vs_output_param_offset
[i
] = match
->offset
;
1703 void ac_optimize_vs_outputs(struct ac_llvm_context
*ctx
,
1704 LLVMValueRef main_fn
,
1705 uint8_t *vs_output_param_offset
,
1706 uint32_t num_outputs
,
1707 uint8_t *num_param_exports
)
1709 LLVMBasicBlockRef bb
;
1710 bool removed_any
= false;
1711 struct ac_vs_exports exports
;
1715 /* Process all LLVM instructions. */
1716 bb
= LLVMGetFirstBasicBlock(main_fn
);
1718 LLVMValueRef inst
= LLVMGetFirstInstruction(bb
);
1721 LLVMValueRef cur
= inst
;
1722 inst
= LLVMGetNextInstruction(inst
);
1723 struct ac_vs_exp_inst exp
;
1725 if (LLVMGetInstructionOpcode(cur
) != LLVMCall
)
1728 LLVMValueRef callee
= ac_llvm_get_called_value(cur
);
1730 if (!ac_llvm_is_function(callee
))
1733 const char *name
= LLVMGetValueName(callee
);
1734 unsigned num_args
= LLVMCountParams(callee
);
1736 /* Check if this is an export instruction. */
1737 if ((num_args
!= 9 && num_args
!= 8) ||
1738 (strcmp(name
, "llvm.SI.export") &&
1739 strcmp(name
, "llvm.amdgcn.exp.f32")))
1742 LLVMValueRef arg
= LLVMGetOperand(cur
, AC_EXP_TARGET
);
1743 unsigned target
= LLVMConstIntGetZExtValue(arg
);
1745 if (target
< V_008DFC_SQ_EXP_PARAM
)
1748 target
-= V_008DFC_SQ_EXP_PARAM
;
1750 /* Parse the instruction. */
1751 memset(&exp
, 0, sizeof(exp
));
1752 exp
.offset
= target
;
1755 for (unsigned i
= 0; i
< 4; i
++) {
1756 LLVMValueRef v
= LLVMGetOperand(cur
, AC_EXP_OUT0
+ i
);
1758 exp
.chan
[i
].value
= v
;
1760 if (LLVMIsUndef(v
)) {
1761 exp
.chan
[i
].type
= AC_IR_UNDEF
;
1762 } else if (LLVMIsAConstantFP(v
)) {
1763 LLVMBool loses_info
;
1764 exp
.chan
[i
].type
= AC_IR_CONST
;
1765 exp
.chan
[i
].const_float
=
1766 LLVMConstRealGetDouble(v
, &loses_info
);
1768 exp
.chan
[i
].type
= AC_IR_VALUE
;
1772 /* Eliminate constant and duplicated PARAM exports. */
1773 if (ac_eliminate_const_output(vs_output_param_offset
,
1774 num_outputs
, &exp
) ||
1775 ac_eliminate_duplicated_output(vs_output_param_offset
,
1776 num_outputs
, &exports
,
1780 exports
.exp
[exports
.num
++] = exp
;
1783 bb
= LLVMGetNextBasicBlock(bb
);
1786 /* Remove holes in export memory due to removed PARAM exports.
1787 * This is done by renumbering all PARAM exports.
1790 uint8_t old_offset
[VARYING_SLOT_MAX
];
1793 /* Make a copy of the offsets. We need the old version while
1794 * we are modifying some of them. */
1795 memcpy(old_offset
, vs_output_param_offset
,
1796 sizeof(old_offset
));
1798 for (i
= 0; i
< exports
.num
; i
++) {
1799 unsigned offset
= exports
.exp
[i
].offset
;
1801 /* Update vs_output_param_offset. Multiple outputs can
1802 * have the same offset.
1804 for (out
= 0; out
< num_outputs
; out
++) {
1805 if (old_offset
[out
] == offset
)
1806 vs_output_param_offset
[out
] = i
;
1809 /* Change the PARAM offset in the instruction. */
1810 LLVMSetOperand(exports
.exp
[i
].inst
, AC_EXP_TARGET
,
1811 LLVMConstInt(ctx
->i32
,
1812 V_008DFC_SQ_EXP_PARAM
+ i
, 0));
1814 *num_param_exports
= exports
.num
;
1818 void ac_init_exec_full_mask(struct ac_llvm_context
*ctx
)
1820 LLVMValueRef full_mask
= LLVMConstInt(ctx
->i64
, ~0ull, 0);
1821 ac_build_intrinsic(ctx
,
1822 "llvm.amdgcn.init.exec", ctx
->voidt
,
1823 &full_mask
, 1, AC_FUNC_ATTR_CONVERGENT
);
1826 void ac_declare_lds_as_pointer(struct ac_llvm_context
*ctx
)
1828 unsigned lds_size
= ctx
->chip_class
>= CIK
? 65536 : 32768;
1829 ctx
->lds
= LLVMBuildIntToPtr(ctx
->builder
, ctx
->i32_0
,
1830 LLVMPointerType(LLVMArrayType(ctx
->i32
, lds_size
/ 4), AC_LOCAL_ADDR_SPACE
),
1834 LLVMValueRef
ac_lds_load(struct ac_llvm_context
*ctx
,
1835 LLVMValueRef dw_addr
)
1837 return ac_build_load(ctx
, ctx
->lds
, dw_addr
);
1840 void ac_lds_store(struct ac_llvm_context
*ctx
,
1841 LLVMValueRef dw_addr
,
1844 value
= ac_to_integer(ctx
, value
);
1845 ac_build_indexed_store(ctx
, ctx
->lds
,
1849 LLVMValueRef
ac_find_lsb(struct ac_llvm_context
*ctx
,
1850 LLVMTypeRef dst_type
,
1853 LLVMValueRef params
[2] = {
1856 /* The value of 1 means that ffs(x=0) = undef, so LLVM won't
1857 * add special code to check for x=0. The reason is that
1858 * the LLVM behavior for x=0 is different from what we
1859 * need here. However, LLVM also assumes that ffs(x) is
1860 * in [0, 31], but GLSL expects that ffs(0) = -1, so
1861 * a conditional assignment to handle 0 is still required.
1863 * The hardware already implements the correct behavior.
1865 LLVMConstInt(ctx
->i1
, 1, false),
1868 LLVMValueRef lsb
= ac_build_intrinsic(ctx
, "llvm.cttz.i32", ctx
->i32
,
1870 AC_FUNC_ATTR_READNONE
);
1872 /* TODO: We need an intrinsic to skip this conditional. */
1873 /* Check for zero: */
1874 return LLVMBuildSelect(ctx
->builder
, LLVMBuildICmp(ctx
->builder
,
1877 LLVMConstInt(ctx
->i32
, -1, 0), lsb
, "");
1880 LLVMTypeRef
ac_array_in_const_addr_space(LLVMTypeRef elem_type
)
1882 return LLVMPointerType(LLVMArrayType(elem_type
, 0),
1883 AC_CONST_ADDR_SPACE
);