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
)
53 ctx
->context
= context
;
57 ctx
->voidt
= LLVMVoidTypeInContext(ctx
->context
);
58 ctx
->i1
= LLVMInt1TypeInContext(ctx
->context
);
59 ctx
->i8
= LLVMInt8TypeInContext(ctx
->context
);
60 ctx
->i16
= LLVMIntTypeInContext(ctx
->context
, 16);
61 ctx
->i32
= LLVMIntTypeInContext(ctx
->context
, 32);
62 ctx
->i64
= LLVMIntTypeInContext(ctx
->context
, 64);
63 ctx
->f16
= LLVMHalfTypeInContext(ctx
->context
);
64 ctx
->f32
= LLVMFloatTypeInContext(ctx
->context
);
65 ctx
->f64
= LLVMDoubleTypeInContext(ctx
->context
);
66 ctx
->v4i32
= LLVMVectorType(ctx
->i32
, 4);
67 ctx
->v4f32
= LLVMVectorType(ctx
->f32
, 4);
68 ctx
->v8i32
= LLVMVectorType(ctx
->i32
, 8);
70 ctx
->i32_0
= LLVMConstInt(ctx
->i32
, 0, false);
71 ctx
->i32_1
= LLVMConstInt(ctx
->i32
, 1, false);
72 ctx
->f32_0
= LLVMConstReal(ctx
->f32
, 0.0);
73 ctx
->f32_1
= LLVMConstReal(ctx
->f32
, 1.0);
75 ctx
->range_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
78 ctx
->invariant_load_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
79 "invariant.load", 14);
81 ctx
->fpmath_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
, "fpmath", 6);
83 args
[0] = LLVMConstReal(ctx
->f32
, 2.5);
84 ctx
->fpmath_md_2p5_ulp
= LLVMMDNodeInContext(ctx
->context
, args
, 1);
86 ctx
->uniform_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
87 "amdgpu.uniform", 14);
89 ctx
->empty_md
= LLVMMDNodeInContext(ctx
->context
, NULL
, 0);
93 ac_get_type_size(LLVMTypeRef type
)
95 LLVMTypeKind kind
= LLVMGetTypeKind(type
);
98 case LLVMIntegerTypeKind
:
99 return LLVMGetIntTypeWidth(type
) / 8;
100 case LLVMFloatTypeKind
:
102 case LLVMPointerTypeKind
:
104 case LLVMVectorTypeKind
:
105 return LLVMGetVectorSize(type
) *
106 ac_get_type_size(LLVMGetElementType(type
));
107 case LLVMArrayTypeKind
:
108 return LLVMGetArrayLength(type
) *
109 ac_get_type_size(LLVMGetElementType(type
));
117 ac_build_intrinsic(struct ac_llvm_context
*ctx
, const char *name
,
118 LLVMTypeRef return_type
, LLVMValueRef
*params
,
119 unsigned param_count
, unsigned attrib_mask
)
121 LLVMValueRef function
, call
;
122 bool set_callsite_attrs
= HAVE_LLVM
>= 0x0400 &&
123 !(attrib_mask
& AC_FUNC_ATTR_LEGACY
);
125 function
= LLVMGetNamedFunction(ctx
->module
, name
);
127 LLVMTypeRef param_types
[32], function_type
;
130 assert(param_count
<= 32);
132 for (i
= 0; i
< param_count
; ++i
) {
134 param_types
[i
] = LLVMTypeOf(params
[i
]);
137 LLVMFunctionType(return_type
, param_types
, param_count
, 0);
138 function
= LLVMAddFunction(ctx
->module
, name
, function_type
);
140 LLVMSetFunctionCallConv(function
, LLVMCCallConv
);
141 LLVMSetLinkage(function
, LLVMExternalLinkage
);
143 if (!set_callsite_attrs
)
144 ac_add_func_attributes(ctx
->context
, function
, attrib_mask
);
147 call
= LLVMBuildCall(ctx
->builder
, function
, params
, param_count
, "");
148 if (set_callsite_attrs
)
149 ac_add_func_attributes(ctx
->context
, call
, attrib_mask
);
153 static LLVMValueRef
bitcast_to_float(struct ac_llvm_context
*ctx
,
156 LLVMTypeRef type
= LLVMTypeOf(value
);
157 LLVMTypeRef new_type
;
159 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
)
160 new_type
= LLVMVectorType(ctx
->f32
, LLVMGetVectorSize(type
));
164 return LLVMBuildBitCast(ctx
->builder
, value
, new_type
, "");
168 * Given the i32 or vNi32 \p type, generate the textual name (e.g. for use with
171 void ac_build_type_name_for_intr(LLVMTypeRef type
, char *buf
, unsigned bufsize
)
173 LLVMTypeRef elem_type
= type
;
175 assert(bufsize
>= 8);
177 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
) {
178 int ret
= snprintf(buf
, bufsize
, "v%u",
179 LLVMGetVectorSize(type
));
181 char *type_name
= LLVMPrintTypeToString(type
);
182 fprintf(stderr
, "Error building type name for: %s\n",
186 elem_type
= LLVMGetElementType(type
);
190 switch (LLVMGetTypeKind(elem_type
)) {
192 case LLVMIntegerTypeKind
:
193 snprintf(buf
, bufsize
, "i%d", LLVMGetIntTypeWidth(elem_type
));
195 case LLVMFloatTypeKind
:
196 snprintf(buf
, bufsize
, "f32");
198 case LLVMDoubleTypeKind
:
199 snprintf(buf
, bufsize
, "f64");
204 /* Prevent optimizations (at least of memory accesses) across the current
205 * point in the program by emitting empty inline assembly that is marked as
206 * having side effects.
208 * Optionally, a value can be passed through the inline assembly to prevent
209 * LLVM from hoisting calls to ReadNone functions.
212 ac_build_optimization_barrier(struct ac_llvm_context
*ctx
,
215 static int counter
= 0;
217 LLVMBuilderRef builder
= ctx
->builder
;
220 snprintf(code
, sizeof(code
), "; %d", p_atomic_inc_return(&counter
));
223 LLVMTypeRef ftype
= LLVMFunctionType(ctx
->voidt
, NULL
, 0, false);
224 LLVMValueRef inlineasm
= LLVMConstInlineAsm(ftype
, code
, "", true, false);
225 LLVMBuildCall(builder
, inlineasm
, NULL
, 0, "");
227 LLVMTypeRef ftype
= LLVMFunctionType(ctx
->i32
, &ctx
->i32
, 1, false);
228 LLVMValueRef inlineasm
= LLVMConstInlineAsm(ftype
, code
, "=v,0", true, false);
229 LLVMValueRef vgpr
= *pvgpr
;
230 LLVMTypeRef vgpr_type
= LLVMTypeOf(vgpr
);
231 unsigned vgpr_size
= ac_get_type_size(vgpr_type
);
234 assert(vgpr_size
% 4 == 0);
236 vgpr
= LLVMBuildBitCast(builder
, vgpr
, LLVMVectorType(ctx
->i32
, vgpr_size
/ 4), "");
237 vgpr0
= LLVMBuildExtractElement(builder
, vgpr
, ctx
->i32_0
, "");
238 vgpr0
= LLVMBuildCall(builder
, inlineasm
, &vgpr0
, 1, "");
239 vgpr
= LLVMBuildInsertElement(builder
, vgpr
, vgpr0
, ctx
->i32_0
, "");
240 vgpr
= LLVMBuildBitCast(builder
, vgpr
, vgpr_type
, "");
247 ac_build_ballot(struct ac_llvm_context
*ctx
,
250 LLVMValueRef args
[3] = {
253 LLVMConstInt(ctx
->i32
, LLVMIntNE
, 0)
256 /* We currently have no other way to prevent LLVM from lifting the icmp
257 * calls to a dominating basic block.
259 ac_build_optimization_barrier(ctx
, &args
[0]);
261 if (LLVMTypeOf(args
[0]) != ctx
->i32
)
262 args
[0] = LLVMBuildBitCast(ctx
->builder
, args
[0], ctx
->i32
, "");
264 return ac_build_intrinsic(ctx
,
265 "llvm.amdgcn.icmp.i32",
267 AC_FUNC_ATTR_NOUNWIND
|
268 AC_FUNC_ATTR_READNONE
|
269 AC_FUNC_ATTR_CONVERGENT
);
273 ac_build_vote_all(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
275 LLVMValueRef active_set
= ac_build_ballot(ctx
, ctx
->i32_1
);
276 LLVMValueRef vote_set
= ac_build_ballot(ctx
, value
);
277 return LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, vote_set
, active_set
, "");
281 ac_build_vote_any(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
283 LLVMValueRef vote_set
= ac_build_ballot(ctx
, value
);
284 return LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, vote_set
,
285 LLVMConstInt(ctx
->i64
, 0, 0), "");
289 ac_build_vote_eq(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
291 LLVMValueRef active_set
= ac_build_ballot(ctx
, ctx
->i32_1
);
292 LLVMValueRef vote_set
= ac_build_ballot(ctx
, value
);
294 LLVMValueRef all
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
295 vote_set
, active_set
, "");
296 LLVMValueRef none
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
298 LLVMConstInt(ctx
->i64
, 0, 0), "");
299 return LLVMBuildOr(ctx
->builder
, all
, none
, "");
303 ac_build_gather_values_extended(struct ac_llvm_context
*ctx
,
304 LLVMValueRef
*values
,
305 unsigned value_count
,
306 unsigned value_stride
,
310 LLVMBuilderRef builder
= ctx
->builder
;
311 LLVMValueRef vec
= NULL
;
314 if (value_count
== 1 && !always_vector
) {
316 return LLVMBuildLoad(builder
, values
[0], "");
318 } else if (!value_count
)
319 unreachable("value_count is 0");
321 for (i
= 0; i
< value_count
; i
++) {
322 LLVMValueRef value
= values
[i
* value_stride
];
324 value
= LLVMBuildLoad(builder
, value
, "");
327 vec
= LLVMGetUndef( LLVMVectorType(LLVMTypeOf(value
), value_count
));
328 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, i
, false);
329 vec
= LLVMBuildInsertElement(builder
, vec
, value
, index
, "");
335 ac_build_gather_values(struct ac_llvm_context
*ctx
,
336 LLVMValueRef
*values
,
337 unsigned value_count
)
339 return ac_build_gather_values_extended(ctx
, values
, value_count
, 1, false, false);
343 ac_build_fdiv(struct ac_llvm_context
*ctx
,
347 LLVMValueRef ret
= LLVMBuildFDiv(ctx
->builder
, num
, den
, "");
349 if (!LLVMIsConstant(ret
))
350 LLVMSetMetadata(ret
, ctx
->fpmath_md_kind
, ctx
->fpmath_md_2p5_ulp
);
354 /* Coordinates for cube map selection. sc, tc, and ma are as in Table 8.27
355 * of the OpenGL 4.5 (Compatibility Profile) specification, except ma is
356 * already multiplied by two. id is the cube face number.
358 struct cube_selection_coords
{
365 build_cube_intrinsic(struct ac_llvm_context
*ctx
,
367 struct cube_selection_coords
*out
)
369 LLVMTypeRef f32
= ctx
->f32
;
371 out
->stc
[1] = ac_build_intrinsic(ctx
, "llvm.amdgcn.cubetc",
372 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
373 out
->stc
[0] = ac_build_intrinsic(ctx
, "llvm.amdgcn.cubesc",
374 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
375 out
->ma
= ac_build_intrinsic(ctx
, "llvm.amdgcn.cubema",
376 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
377 out
->id
= ac_build_intrinsic(ctx
, "llvm.amdgcn.cubeid",
378 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
382 * Build a manual selection sequence for cube face sc/tc coordinates and
383 * major axis vector (multiplied by 2 for consistency) for the given
384 * vec3 \p coords, for the face implied by \p selcoords.
386 * For the major axis, we always adjust the sign to be in the direction of
387 * selcoords.ma; i.e., a positive out_ma means that coords is pointed towards
388 * the selcoords major axis.
390 static void build_cube_select(LLVMBuilderRef builder
,
391 const struct cube_selection_coords
*selcoords
,
392 const LLVMValueRef
*coords
,
393 LLVMValueRef
*out_st
,
394 LLVMValueRef
*out_ma
)
396 LLVMTypeRef f32
= LLVMTypeOf(coords
[0]);
397 LLVMValueRef is_ma_positive
;
399 LLVMValueRef is_ma_z
, is_not_ma_z
;
400 LLVMValueRef is_ma_y
;
401 LLVMValueRef is_ma_x
;
405 is_ma_positive
= LLVMBuildFCmp(builder
, LLVMRealUGE
,
406 selcoords
->ma
, LLVMConstReal(f32
, 0.0), "");
407 sgn_ma
= LLVMBuildSelect(builder
, is_ma_positive
,
408 LLVMConstReal(f32
, 1.0), LLVMConstReal(f32
, -1.0), "");
410 is_ma_z
= LLVMBuildFCmp(builder
, LLVMRealUGE
, selcoords
->id
, LLVMConstReal(f32
, 4.0), "");
411 is_not_ma_z
= LLVMBuildNot(builder
, is_ma_z
, "");
412 is_ma_y
= LLVMBuildAnd(builder
, is_not_ma_z
,
413 LLVMBuildFCmp(builder
, LLVMRealUGE
, selcoords
->id
, LLVMConstReal(f32
, 2.0), ""), "");
414 is_ma_x
= LLVMBuildAnd(builder
, is_not_ma_z
, LLVMBuildNot(builder
, is_ma_y
, ""), "");
417 tmp
= LLVMBuildSelect(builder
, is_ma_z
, coords
[2], coords
[0], "");
418 sgn
= LLVMBuildSelect(builder
, is_ma_y
, LLVMConstReal(f32
, 1.0),
419 LLVMBuildSelect(builder
, is_ma_x
, sgn_ma
,
420 LLVMBuildFNeg(builder
, sgn_ma
, ""), ""), "");
421 out_st
[0] = LLVMBuildFMul(builder
, tmp
, sgn
, "");
424 tmp
= LLVMBuildSelect(builder
, is_ma_y
, coords
[2], coords
[1], "");
425 sgn
= LLVMBuildSelect(builder
, is_ma_y
, LLVMBuildFNeg(builder
, sgn_ma
, ""),
426 LLVMConstReal(f32
, -1.0), "");
427 out_st
[1] = LLVMBuildFMul(builder
, tmp
, sgn
, "");
430 tmp
= LLVMBuildSelect(builder
, is_ma_z
, coords
[2],
431 LLVMBuildSelect(builder
, is_ma_y
, coords
[1], coords
[0], ""), "");
432 sgn
= LLVMBuildSelect(builder
, is_ma_positive
,
433 LLVMConstReal(f32
, 2.0), LLVMConstReal(f32
, -2.0), "");
434 *out_ma
= LLVMBuildFMul(builder
, tmp
, sgn
, "");
438 ac_prepare_cube_coords(struct ac_llvm_context
*ctx
,
439 bool is_deriv
, bool is_array
,
440 LLVMValueRef
*coords_arg
,
441 LLVMValueRef
*derivs_arg
)
444 LLVMBuilderRef builder
= ctx
->builder
;
445 struct cube_selection_coords selcoords
;
446 LLVMValueRef coords
[3];
449 build_cube_intrinsic(ctx
, coords_arg
, &selcoords
);
451 invma
= ac_build_intrinsic(ctx
, "llvm.fabs.f32",
452 ctx
->f32
, &selcoords
.ma
, 1, AC_FUNC_ATTR_READNONE
);
453 invma
= ac_build_fdiv(ctx
, LLVMConstReal(ctx
->f32
, 1.0), invma
);
455 for (int i
= 0; i
< 2; ++i
)
456 coords
[i
] = LLVMBuildFMul(builder
, selcoords
.stc
[i
], invma
, "");
458 coords
[2] = selcoords
.id
;
460 if (is_deriv
&& derivs_arg
) {
461 LLVMValueRef derivs
[4];
464 /* Convert cube derivatives to 2D derivatives. */
465 for (axis
= 0; axis
< 2; axis
++) {
466 LLVMValueRef deriv_st
[2];
467 LLVMValueRef deriv_ma
;
469 /* Transform the derivative alongside the texture
470 * coordinate. Mathematically, the correct formula is
471 * as follows. Assume we're projecting onto the +Z face
472 * and denote by dx/dh the derivative of the (original)
473 * X texture coordinate with respect to horizontal
474 * window coordinates. The projection onto the +Z face
479 * Then df/dh = df/dx * dx/dh + df/dz * dz/dh
480 * = 1/z * dx/dh - x/z * 1/z * dz/dh.
482 * This motivatives the implementation below.
484 * Whether this actually gives the expected results for
485 * apps that might feed in derivatives obtained via
486 * finite differences is anyone's guess. The OpenGL spec
487 * seems awfully quiet about how textureGrad for cube
488 * maps should be handled.
490 build_cube_select(builder
, &selcoords
, &derivs_arg
[axis
* 3],
491 deriv_st
, &deriv_ma
);
493 deriv_ma
= LLVMBuildFMul(builder
, deriv_ma
, invma
, "");
495 for (int i
= 0; i
< 2; ++i
)
496 derivs
[axis
* 2 + i
] =
497 LLVMBuildFSub(builder
,
498 LLVMBuildFMul(builder
, deriv_st
[i
], invma
, ""),
499 LLVMBuildFMul(builder
, deriv_ma
, coords
[i
], ""), "");
502 memcpy(derivs_arg
, derivs
, sizeof(derivs
));
505 /* Shift the texture coordinate. This must be applied after the
506 * derivative calculation.
508 for (int i
= 0; i
< 2; ++i
)
509 coords
[i
] = LLVMBuildFAdd(builder
, coords
[i
], LLVMConstReal(ctx
->f32
, 1.5), "");
512 /* for cube arrays coord.z = coord.w(array_index) * 8 + face */
513 /* coords_arg.w component - array_index for cube arrays */
514 LLVMValueRef tmp
= LLVMBuildFMul(ctx
->builder
, coords_arg
[3], LLVMConstReal(ctx
->f32
, 8.0), "");
515 coords
[2] = LLVMBuildFAdd(ctx
->builder
, tmp
, coords
[2], "");
518 memcpy(coords_arg
, coords
, sizeof(coords
));
523 ac_build_fs_interp(struct ac_llvm_context
*ctx
,
524 LLVMValueRef llvm_chan
,
525 LLVMValueRef attr_number
,
530 LLVMValueRef args
[5];
533 if (HAVE_LLVM
< 0x0400) {
535 ij
[0] = LLVMBuildBitCast(ctx
->builder
, i
, ctx
->i32
, "");
536 ij
[1] = LLVMBuildBitCast(ctx
->builder
, j
, ctx
->i32
, "");
539 args
[1] = attr_number
;
541 args
[3] = ac_build_gather_values(ctx
, ij
, 2);
542 return ac_build_intrinsic(ctx
, "llvm.SI.fs.interp",
544 AC_FUNC_ATTR_READNONE
);
549 args
[2] = attr_number
;
552 p1
= ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p1",
553 ctx
->f32
, args
, 4, AC_FUNC_ATTR_READNONE
);
558 args
[3] = attr_number
;
561 return ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p2",
562 ctx
->f32
, args
, 5, AC_FUNC_ATTR_READNONE
);
566 ac_build_fs_interp_mov(struct ac_llvm_context
*ctx
,
567 LLVMValueRef parameter
,
568 LLVMValueRef llvm_chan
,
569 LLVMValueRef attr_number
,
572 LLVMValueRef args
[4];
573 if (HAVE_LLVM
< 0x0400) {
575 args
[1] = attr_number
;
578 return ac_build_intrinsic(ctx
,
579 "llvm.SI.fs.constant",
581 AC_FUNC_ATTR_READNONE
);
586 args
[2] = attr_number
;
589 return ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.mov",
590 ctx
->f32
, args
, 4, AC_FUNC_ATTR_READNONE
);
594 ac_build_gep0(struct ac_llvm_context
*ctx
,
595 LLVMValueRef base_ptr
,
598 LLVMValueRef indices
[2] = {
599 LLVMConstInt(ctx
->i32
, 0, 0),
602 return LLVMBuildGEP(ctx
->builder
, base_ptr
,
607 ac_build_indexed_store(struct ac_llvm_context
*ctx
,
608 LLVMValueRef base_ptr
, LLVMValueRef index
,
611 LLVMBuildStore(ctx
->builder
, value
,
612 ac_build_gep0(ctx
, base_ptr
, index
));
616 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad.
617 * It's equivalent to doing a load from &base_ptr[index].
619 * \param base_ptr Where the array starts.
620 * \param index The element index into the array.
621 * \param uniform Whether the base_ptr and index can be assumed to be
622 * dynamically uniform
625 ac_build_indexed_load(struct ac_llvm_context
*ctx
,
626 LLVMValueRef base_ptr
, LLVMValueRef index
,
629 LLVMValueRef pointer
;
631 pointer
= ac_build_gep0(ctx
, base_ptr
, index
);
633 LLVMSetMetadata(pointer
, ctx
->uniform_md_kind
, ctx
->empty_md
);
634 return LLVMBuildLoad(ctx
->builder
, pointer
, "");
638 * Do a load from &base_ptr[index], but also add a flag that it's loading
639 * a constant from a dynamically uniform index.
642 ac_build_indexed_load_const(struct ac_llvm_context
*ctx
,
643 LLVMValueRef base_ptr
, LLVMValueRef index
)
645 LLVMValueRef result
= ac_build_indexed_load(ctx
, base_ptr
, index
, true);
646 LLVMSetMetadata(result
, ctx
->invariant_load_md_kind
, ctx
->empty_md
);
650 /* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
651 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
652 * or v4i32 (num_channels=3,4).
655 ac_build_buffer_store_dword(struct ac_llvm_context
*ctx
,
658 unsigned num_channels
,
659 LLVMValueRef voffset
,
660 LLVMValueRef soffset
,
661 unsigned inst_offset
,
664 bool writeonly_memory
,
667 /* TODO: Fix stores with ADD_TID and remove the "has_add_tid" flag. */
669 /* Split 3 channel stores, becase LLVM doesn't support 3-channel
671 if (num_channels
== 3) {
672 LLVMValueRef v
[3], v01
;
674 for (int i
= 0; i
< 3; i
++) {
675 v
[i
] = LLVMBuildExtractElement(ctx
->builder
, vdata
,
676 LLVMConstInt(ctx
->i32
, i
, 0), "");
678 v01
= ac_build_gather_values(ctx
, v
, 2);
680 ac_build_buffer_store_dword(ctx
, rsrc
, v01
, 2, voffset
,
681 soffset
, inst_offset
, glc
, slc
,
682 writeonly_memory
, has_add_tid
);
683 ac_build_buffer_store_dword(ctx
, rsrc
, v
[2], 1, voffset
,
684 soffset
, inst_offset
+ 8,
686 writeonly_memory
, has_add_tid
);
690 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
691 static const char *types
[] = {"f32", "v2f32", "v4f32"};
693 LLVMValueRef offset
= soffset
;
696 offset
= LLVMBuildAdd(ctx
->builder
, offset
,
697 LLVMConstInt(ctx
->i32
, inst_offset
, 0), "");
699 offset
= LLVMBuildAdd(ctx
->builder
, offset
, voffset
, "");
701 LLVMValueRef args
[] = {
702 bitcast_to_float(ctx
, vdata
),
703 LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, ""),
704 LLVMConstInt(ctx
->i32
, 0, 0),
706 LLVMConstInt(ctx
->i1
, glc
, 0),
707 LLVMConstInt(ctx
->i1
, slc
, 0),
710 snprintf(name
, sizeof(name
), "llvm.amdgcn.buffer.store.%s",
713 ac_build_intrinsic(ctx
, name
, ctx
->voidt
,
714 args
, ARRAY_SIZE(args
),
716 AC_FUNC_ATTR_INACCESSIBLE_MEM_ONLY
:
717 AC_FUNC_ATTR_WRITEONLY
);
721 static unsigned dfmt
[] = {
722 V_008F0C_BUF_DATA_FORMAT_32
,
723 V_008F0C_BUF_DATA_FORMAT_32_32
,
724 V_008F0C_BUF_DATA_FORMAT_32_32_32
,
725 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
727 assert(num_channels
>= 1 && num_channels
<= 4);
729 LLVMValueRef args
[] = {
732 LLVMConstInt(ctx
->i32
, num_channels
, 0),
733 voffset
? voffset
: LLVMGetUndef(ctx
->i32
),
735 LLVMConstInt(ctx
->i32
, inst_offset
, 0),
736 LLVMConstInt(ctx
->i32
, dfmt
[num_channels
- 1], 0),
737 LLVMConstInt(ctx
->i32
, V_008F0C_BUF_NUM_FORMAT_UINT
, 0),
738 LLVMConstInt(ctx
->i32
, voffset
!= NULL
, 0),
739 LLVMConstInt(ctx
->i32
, 0, 0), /* idxen */
740 LLVMConstInt(ctx
->i32
, glc
, 0),
741 LLVMConstInt(ctx
->i32
, slc
, 0),
742 LLVMConstInt(ctx
->i32
, 0, 0), /* tfe*/
745 /* The instruction offset field has 12 bits */
746 assert(voffset
|| inst_offset
< (1 << 12));
748 /* The intrinsic is overloaded, we need to add a type suffix for overloading to work. */
749 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
750 const char *types
[] = {"i32", "v2i32", "v4i32"};
752 snprintf(name
, sizeof(name
), "llvm.SI.tbuffer.store.%s", types
[func
]);
754 ac_build_intrinsic(ctx
, name
, ctx
->voidt
,
755 args
, ARRAY_SIZE(args
),
756 AC_FUNC_ATTR_LEGACY
);
760 ac_build_buffer_load(struct ac_llvm_context
*ctx
,
764 LLVMValueRef voffset
,
765 LLVMValueRef soffset
,
766 unsigned inst_offset
,
772 LLVMValueRef offset
= LLVMConstInt(ctx
->i32
, inst_offset
, 0);
774 offset
= LLVMBuildAdd(ctx
->builder
, offset
, voffset
, "");
776 offset
= LLVMBuildAdd(ctx
->builder
, offset
, soffset
, "");
778 /* TODO: VI and later generations can use SMEM with GLC=1.*/
779 if (allow_smem
&& !glc
&& !slc
) {
780 assert(vindex
== NULL
);
782 LLVMValueRef result
[4];
784 for (int i
= 0; i
< num_channels
; i
++) {
786 offset
= LLVMBuildAdd(ctx
->builder
, offset
,
787 LLVMConstInt(ctx
->i32
, 4, 0), "");
789 LLVMValueRef args
[2] = {rsrc
, offset
};
790 result
[i
] = ac_build_intrinsic(ctx
, "llvm.SI.load.const.v4i32",
792 AC_FUNC_ATTR_READNONE
|
793 AC_FUNC_ATTR_LEGACY
);
795 if (num_channels
== 1)
798 if (num_channels
== 3)
799 result
[num_channels
++] = LLVMGetUndef(ctx
->f32
);
800 return ac_build_gather_values(ctx
, result
, num_channels
);
803 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
805 LLVMValueRef args
[] = {
806 LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, ""),
807 vindex
? vindex
: LLVMConstInt(ctx
->i32
, 0, 0),
809 LLVMConstInt(ctx
->i1
, glc
, 0),
810 LLVMConstInt(ctx
->i1
, slc
, 0)
813 LLVMTypeRef types
[] = {ctx
->f32
, LLVMVectorType(ctx
->f32
, 2),
815 const char *type_names
[] = {"f32", "v2f32", "v4f32"};
818 snprintf(name
, sizeof(name
), "llvm.amdgcn.buffer.load.%s",
821 return ac_build_intrinsic(ctx
, name
, types
[func
], args
,
823 /* READNONE means writes can't affect it, while
824 * READONLY means that writes can affect it. */
825 can_speculate
&& HAVE_LLVM
>= 0x0400 ?
826 AC_FUNC_ATTR_READNONE
:
827 AC_FUNC_ATTR_READONLY
);
830 LLVMValueRef
ac_build_buffer_load_format(struct ac_llvm_context
*ctx
,
833 LLVMValueRef voffset
,
836 LLVMValueRef args
[] = {
837 LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, ""),
840 LLVMConstInt(ctx
->i1
, 0, 0), /* glc */
841 LLVMConstInt(ctx
->i1
, 0, 0), /* slc */
844 return ac_build_intrinsic(ctx
,
845 "llvm.amdgcn.buffer.load.format.v4f32",
846 ctx
->v4f32
, args
, ARRAY_SIZE(args
),
847 /* READNONE means writes can't affect it, while
848 * READONLY means that writes can affect it. */
849 can_speculate
&& HAVE_LLVM
>= 0x0400 ?
850 AC_FUNC_ATTR_READNONE
:
851 AC_FUNC_ATTR_READONLY
);
855 * Set range metadata on an instruction. This can only be used on load and
856 * call instructions. If you know an instruction can only produce the values
857 * 0, 1, 2, you would do set_range_metadata(value, 0, 3);
858 * \p lo is the minimum value inclusive.
859 * \p hi is the maximum value exclusive.
861 static void set_range_metadata(struct ac_llvm_context
*ctx
,
862 LLVMValueRef value
, unsigned lo
, unsigned hi
)
864 LLVMValueRef range_md
, md_args
[2];
865 LLVMTypeRef type
= LLVMTypeOf(value
);
866 LLVMContextRef context
= LLVMGetTypeContext(type
);
868 md_args
[0] = LLVMConstInt(type
, lo
, false);
869 md_args
[1] = LLVMConstInt(type
, hi
, false);
870 range_md
= LLVMMDNodeInContext(context
, md_args
, 2);
871 LLVMSetMetadata(value
, ctx
->range_md_kind
, range_md
);
875 ac_get_thread_id(struct ac_llvm_context
*ctx
)
879 LLVMValueRef tid_args
[2];
880 tid_args
[0] = LLVMConstInt(ctx
->i32
, 0xffffffff, false);
881 tid_args
[1] = LLVMConstInt(ctx
->i32
, 0, false);
882 tid_args
[1] = ac_build_intrinsic(ctx
,
883 "llvm.amdgcn.mbcnt.lo", ctx
->i32
,
884 tid_args
, 2, AC_FUNC_ATTR_READNONE
);
886 tid
= ac_build_intrinsic(ctx
, "llvm.amdgcn.mbcnt.hi",
888 2, AC_FUNC_ATTR_READNONE
);
889 set_range_metadata(ctx
, tid
, 0, 64);
894 * SI implements derivatives using the local data store (LDS)
895 * All writes to the LDS happen in all executing threads at
896 * the same time. TID is the Thread ID for the current
897 * thread and is a value between 0 and 63, representing
898 * the thread's position in the wavefront.
900 * For the pixel shader threads are grouped into quads of four pixels.
901 * The TIDs of the pixels of a quad are:
909 * So, masking the TID with 0xfffffffc yields the TID of the top left pixel
910 * of the quad, masking with 0xfffffffd yields the TID of the top pixel of
911 * the current pixel's column, and masking with 0xfffffffe yields the TID
912 * of the left pixel of the current pixel's row.
914 * Adding 1 yields the TID of the pixel to the right of the left pixel, and
915 * adding 2 yields the TID of the pixel below the top pixel.
918 ac_build_ddxy(struct ac_llvm_context
*ctx
,
919 bool has_ds_bpermute
,
924 LLVMValueRef tl
, trbl
, args
[2];
927 if (has_ds_bpermute
) {
928 LLVMValueRef thread_id
, tl_tid
, trbl_tid
;
929 thread_id
= ac_get_thread_id(ctx
);
931 tl_tid
= LLVMBuildAnd(ctx
->builder
, thread_id
,
932 LLVMConstInt(ctx
->i32
, mask
, false), "");
934 trbl_tid
= LLVMBuildAdd(ctx
->builder
, tl_tid
,
935 LLVMConstInt(ctx
->i32
, idx
, false), "");
937 args
[0] = LLVMBuildMul(ctx
->builder
, tl_tid
,
938 LLVMConstInt(ctx
->i32
, 4, false), "");
940 tl
= ac_build_intrinsic(ctx
,
941 "llvm.amdgcn.ds.bpermute", ctx
->i32
,
943 AC_FUNC_ATTR_READNONE
|
944 AC_FUNC_ATTR_CONVERGENT
);
946 args
[0] = LLVMBuildMul(ctx
->builder
, trbl_tid
,
947 LLVMConstInt(ctx
->i32
, 4, false), "");
948 trbl
= ac_build_intrinsic(ctx
,
949 "llvm.amdgcn.ds.bpermute", ctx
->i32
,
951 AC_FUNC_ATTR_READNONE
|
952 AC_FUNC_ATTR_CONVERGENT
);
957 case AC_TID_MASK_TOP_LEFT
:
965 case AC_TID_MASK_TOP
:
969 case AC_TID_MASK_LEFT
:
976 args
[1] = LLVMConstInt(ctx
->i32
, masks
[0], false);
978 tl
= ac_build_intrinsic(ctx
,
979 "llvm.amdgcn.ds.swizzle", ctx
->i32
,
981 AC_FUNC_ATTR_READNONE
|
982 AC_FUNC_ATTR_CONVERGENT
);
984 args
[1] = LLVMConstInt(ctx
->i32
, masks
[1], false);
985 trbl
= ac_build_intrinsic(ctx
,
986 "llvm.amdgcn.ds.swizzle", ctx
->i32
,
988 AC_FUNC_ATTR_READNONE
|
989 AC_FUNC_ATTR_CONVERGENT
);
992 tl
= LLVMBuildBitCast(ctx
->builder
, tl
, ctx
->f32
, "");
993 trbl
= LLVMBuildBitCast(ctx
->builder
, trbl
, ctx
->f32
, "");
994 result
= LLVMBuildFSub(ctx
->builder
, trbl
, tl
, "");
999 ac_build_sendmsg(struct ac_llvm_context
*ctx
,
1001 LLVMValueRef wave_id
)
1003 LLVMValueRef args
[2];
1004 const char *intr_name
= (HAVE_LLVM
< 0x0400) ? "llvm.SI.sendmsg" : "llvm.amdgcn.s.sendmsg";
1005 args
[0] = LLVMConstInt(ctx
->i32
, msg
, false);
1007 ac_build_intrinsic(ctx
, intr_name
, ctx
->voidt
, args
, 2, 0);
1011 ac_build_imsb(struct ac_llvm_context
*ctx
,
1013 LLVMTypeRef dst_type
)
1015 const char *intr_name
= (HAVE_LLVM
< 0x0400) ? "llvm.AMDGPU.flbit.i32" :
1016 "llvm.amdgcn.sffbh.i32";
1017 LLVMValueRef msb
= ac_build_intrinsic(ctx
, intr_name
,
1019 AC_FUNC_ATTR_READNONE
);
1021 /* The HW returns the last bit index from MSB, but NIR/TGSI wants
1022 * the index from LSB. Invert it by doing "31 - msb". */
1023 msb
= LLVMBuildSub(ctx
->builder
, LLVMConstInt(ctx
->i32
, 31, false),
1026 LLVMValueRef all_ones
= LLVMConstInt(ctx
->i32
, -1, true);
1027 LLVMValueRef cond
= LLVMBuildOr(ctx
->builder
,
1028 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
1029 arg
, LLVMConstInt(ctx
->i32
, 0, 0), ""),
1030 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
1031 arg
, all_ones
, ""), "");
1033 return LLVMBuildSelect(ctx
->builder
, cond
, all_ones
, msb
, "");
1037 ac_build_umsb(struct ac_llvm_context
*ctx
,
1039 LLVMTypeRef dst_type
)
1041 LLVMValueRef args
[2] = {
1043 LLVMConstInt(ctx
->i1
, 1, 0),
1045 LLVMValueRef msb
= ac_build_intrinsic(ctx
, "llvm.ctlz.i32",
1046 dst_type
, args
, ARRAY_SIZE(args
),
1047 AC_FUNC_ATTR_READNONE
);
1049 /* The HW returns the last bit index from MSB, but TGSI/NIR wants
1050 * the index from LSB. Invert it by doing "31 - msb". */
1051 msb
= LLVMBuildSub(ctx
->builder
, LLVMConstInt(ctx
->i32
, 31, false),
1054 /* check for zero */
1055 return LLVMBuildSelect(ctx
->builder
,
1056 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, arg
,
1057 LLVMConstInt(ctx
->i32
, 0, 0), ""),
1058 LLVMConstInt(ctx
->i32
, -1, true), msb
, "");
1061 LLVMValueRef
ac_build_umin(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
1064 LLVMValueRef cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntULE
, a
, b
, "");
1065 return LLVMBuildSelect(ctx
->builder
, cmp
, a
, b
, "");
1068 LLVMValueRef
ac_build_clamp(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
1070 if (HAVE_LLVM
>= 0x0500) {
1071 LLVMValueRef max
[2] = {
1073 LLVMConstReal(ctx
->f32
, 0),
1075 LLVMValueRef min
[2] = {
1076 LLVMConstReal(ctx
->f32
, 1),
1079 min
[1] = ac_build_intrinsic(ctx
, "llvm.maxnum.f32",
1081 AC_FUNC_ATTR_READNONE
);
1082 return ac_build_intrinsic(ctx
, "llvm.minnum.f32",
1084 AC_FUNC_ATTR_READNONE
);
1087 LLVMValueRef args
[3] = {
1089 LLVMConstReal(ctx
->f32
, 0),
1090 LLVMConstReal(ctx
->f32
, 1),
1093 return ac_build_intrinsic(ctx
, "llvm.AMDGPU.clamp.", ctx
->f32
, args
, 3,
1094 AC_FUNC_ATTR_READNONE
|
1095 AC_FUNC_ATTR_LEGACY
);
1098 void ac_build_export(struct ac_llvm_context
*ctx
, struct ac_export_args
*a
)
1100 LLVMValueRef args
[9];
1102 if (HAVE_LLVM
>= 0x0500) {
1103 args
[0] = LLVMConstInt(ctx
->i32
, a
->target
, 0);
1104 args
[1] = LLVMConstInt(ctx
->i32
, a
->enabled_channels
, 0);
1107 LLVMTypeRef i16
= LLVMInt16TypeInContext(ctx
->context
);
1108 LLVMTypeRef v2i16
= LLVMVectorType(i16
, 2);
1110 args
[2] = LLVMBuildBitCast(ctx
->builder
, a
->out
[0],
1112 args
[3] = LLVMBuildBitCast(ctx
->builder
, a
->out
[1],
1114 args
[4] = LLVMConstInt(ctx
->i1
, a
->done
, 0);
1115 args
[5] = LLVMConstInt(ctx
->i1
, a
->valid_mask
, 0);
1117 ac_build_intrinsic(ctx
, "llvm.amdgcn.exp.compr.v2i16",
1118 ctx
->voidt
, args
, 6, 0);
1120 args
[2] = a
->out
[0];
1121 args
[3] = a
->out
[1];
1122 args
[4] = a
->out
[2];
1123 args
[5] = a
->out
[3];
1124 args
[6] = LLVMConstInt(ctx
->i1
, a
->done
, 0);
1125 args
[7] = LLVMConstInt(ctx
->i1
, a
->valid_mask
, 0);
1127 ac_build_intrinsic(ctx
, "llvm.amdgcn.exp.f32",
1128 ctx
->voidt
, args
, 8, 0);
1133 args
[0] = LLVMConstInt(ctx
->i32
, a
->enabled_channels
, 0);
1134 args
[1] = LLVMConstInt(ctx
->i32
, a
->valid_mask
, 0);
1135 args
[2] = LLVMConstInt(ctx
->i32
, a
->done
, 0);
1136 args
[3] = LLVMConstInt(ctx
->i32
, a
->target
, 0);
1137 args
[4] = LLVMConstInt(ctx
->i32
, a
->compr
, 0);
1138 memcpy(args
+ 5, a
->out
, sizeof(a
->out
[0]) * 4);
1140 ac_build_intrinsic(ctx
, "llvm.SI.export", ctx
->voidt
, args
, 9,
1141 AC_FUNC_ATTR_LEGACY
);
1144 LLVMValueRef
ac_build_image_opcode(struct ac_llvm_context
*ctx
,
1145 struct ac_image_args
*a
)
1147 LLVMTypeRef dst_type
;
1148 LLVMValueRef args
[11];
1149 unsigned num_args
= 0;
1151 char intr_name
[128], type
[64];
1153 if (HAVE_LLVM
>= 0x0400) {
1154 bool sample
= a
->opcode
== ac_image_sample
||
1155 a
->opcode
== ac_image_gather4
||
1156 a
->opcode
== ac_image_get_lod
;
1159 args
[num_args
++] = bitcast_to_float(ctx
, a
->addr
);
1161 args
[num_args
++] = a
->addr
;
1163 args
[num_args
++] = a
->resource
;
1165 args
[num_args
++] = a
->sampler
;
1166 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->dmask
, 0);
1168 args
[num_args
++] = LLVMConstInt(ctx
->i1
, a
->unorm
, 0);
1169 args
[num_args
++] = LLVMConstInt(ctx
->i1
, 0, 0); /* glc */
1170 args
[num_args
++] = LLVMConstInt(ctx
->i1
, 0, 0); /* slc */
1171 args
[num_args
++] = LLVMConstInt(ctx
->i1
, 0, 0); /* lwe */
1172 args
[num_args
++] = LLVMConstInt(ctx
->i1
, a
->da
, 0);
1174 switch (a
->opcode
) {
1175 case ac_image_sample
:
1176 name
= "llvm.amdgcn.image.sample";
1178 case ac_image_gather4
:
1179 name
= "llvm.amdgcn.image.gather4";
1182 name
= "llvm.amdgcn.image.load";
1184 case ac_image_load_mip
:
1185 name
= "llvm.amdgcn.image.load.mip";
1187 case ac_image_get_lod
:
1188 name
= "llvm.amdgcn.image.getlod";
1190 case ac_image_get_resinfo
:
1191 name
= "llvm.amdgcn.image.getresinfo";
1194 unreachable("invalid image opcode");
1197 ac_build_type_name_for_intr(LLVMTypeOf(args
[0]), type
,
1200 snprintf(intr_name
, sizeof(intr_name
), "%s%s%s%s.v4f32.%s.v8i32",
1202 a
->compare
? ".c" : "",
1206 a
->level_zero
? ".lz" : "",
1207 a
->offset
? ".o" : "",
1210 LLVMValueRef result
=
1211 ac_build_intrinsic(ctx
, intr_name
,
1212 ctx
->v4f32
, args
, num_args
,
1213 AC_FUNC_ATTR_READNONE
);
1215 result
= LLVMBuildBitCast(ctx
->builder
, result
,
1221 args
[num_args
++] = a
->addr
;
1222 args
[num_args
++] = a
->resource
;
1224 if (a
->opcode
== ac_image_load
||
1225 a
->opcode
== ac_image_load_mip
||
1226 a
->opcode
== ac_image_get_resinfo
) {
1227 dst_type
= ctx
->v4i32
;
1229 dst_type
= ctx
->v4f32
;
1230 args
[num_args
++] = a
->sampler
;
1233 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->dmask
, 0);
1234 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->unorm
, 0);
1235 args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* r128 */
1236 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->da
, 0);
1237 args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* glc */
1238 args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* slc */
1239 args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* tfe */
1240 args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* lwe */
1242 switch (a
->opcode
) {
1243 case ac_image_sample
:
1244 name
= "llvm.SI.image.sample";
1246 case ac_image_gather4
:
1247 name
= "llvm.SI.gather4";
1250 name
= "llvm.SI.image.load";
1252 case ac_image_load_mip
:
1253 name
= "llvm.SI.image.load.mip";
1255 case ac_image_get_lod
:
1256 name
= "llvm.SI.getlod";
1258 case ac_image_get_resinfo
:
1259 name
= "llvm.SI.getresinfo";
1263 ac_build_type_name_for_intr(LLVMTypeOf(a
->addr
), type
, sizeof(type
));
1264 snprintf(intr_name
, sizeof(intr_name
), "%s%s%s%s.%s",
1266 a
->compare
? ".c" : "",
1270 a
->level_zero
? ".lz" : "",
1271 a
->offset
? ".o" : "",
1274 return ac_build_intrinsic(ctx
, intr_name
,
1275 dst_type
, args
, num_args
,
1276 AC_FUNC_ATTR_READNONE
|
1277 AC_FUNC_ATTR_LEGACY
);
1280 LLVMValueRef
ac_build_cvt_pkrtz_f16(struct ac_llvm_context
*ctx
,
1281 LLVMValueRef args
[2])
1283 if (HAVE_LLVM
>= 0x0500) {
1285 LLVMVectorType(LLVMHalfTypeInContext(ctx
->context
), 2);
1287 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pkrtz",
1289 AC_FUNC_ATTR_READNONE
);
1290 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
1293 return ac_build_intrinsic(ctx
, "llvm.SI.packf16", ctx
->i32
, args
, 2,
1294 AC_FUNC_ATTR_READNONE
|
1295 AC_FUNC_ATTR_LEGACY
);
1299 * KILL, AKA discard in GLSL.
1301 * \param value kill if value < 0.0 or value == NULL.
1303 void ac_build_kill(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
1306 ac_build_intrinsic(ctx
, "llvm.AMDGPU.kill", ctx
->voidt
,
1307 &value
, 1, AC_FUNC_ATTR_LEGACY
);
1309 ac_build_intrinsic(ctx
, "llvm.AMDGPU.kilp", ctx
->voidt
,
1310 NULL
, 0, AC_FUNC_ATTR_LEGACY
);
1314 LLVMValueRef
ac_build_bfe(struct ac_llvm_context
*ctx
, LLVMValueRef input
,
1315 LLVMValueRef offset
, LLVMValueRef width
,
1318 LLVMValueRef args
[] = {
1324 if (HAVE_LLVM
>= 0x0500) {
1325 return ac_build_intrinsic(ctx
,
1326 is_signed
? "llvm.amdgcn.sbfe.i32" :
1327 "llvm.amdgcn.ubfe.i32",
1329 AC_FUNC_ATTR_READNONE
);
1332 return ac_build_intrinsic(ctx
,
1333 is_signed
? "llvm.AMDGPU.bfe.i32" :
1334 "llvm.AMDGPU.bfe.u32",
1336 AC_FUNC_ATTR_READNONE
|
1337 AC_FUNC_ATTR_LEGACY
);
1340 void ac_get_image_intr_name(const char *base_name
,
1341 LLVMTypeRef data_type
,
1342 LLVMTypeRef coords_type
,
1343 LLVMTypeRef rsrc_type
,
1344 char *out_name
, unsigned out_len
)
1346 char coords_type_name
[8];
1348 ac_build_type_name_for_intr(coords_type
, coords_type_name
,
1349 sizeof(coords_type_name
));
1351 if (HAVE_LLVM
<= 0x0309) {
1352 snprintf(out_name
, out_len
, "%s.%s", base_name
, coords_type_name
);
1354 char data_type_name
[8];
1355 char rsrc_type_name
[8];
1357 ac_build_type_name_for_intr(data_type
, data_type_name
,
1358 sizeof(data_type_name
));
1359 ac_build_type_name_for_intr(rsrc_type
, rsrc_type_name
,
1360 sizeof(rsrc_type_name
));
1361 snprintf(out_name
, out_len
, "%s.%s.%s.%s", base_name
,
1362 data_type_name
, coords_type_name
, rsrc_type_name
);
1366 #define AC_EXP_TARGET (HAVE_LLVM >= 0x0500 ? 0 : 3)
1367 #define AC_EXP_OUT0 (HAVE_LLVM >= 0x0500 ? 2 : 5)
1375 struct ac_vs_exp_chan
1379 enum ac_ir_type type
;
1382 struct ac_vs_exp_inst
{
1385 struct ac_vs_exp_chan chan
[4];
1388 struct ac_vs_exports
{
1390 struct ac_vs_exp_inst exp
[VARYING_SLOT_MAX
];
1393 /* Return true if the PARAM export has been eliminated. */
1394 static bool ac_eliminate_const_output(uint8_t *vs_output_param_offset
,
1395 uint32_t num_outputs
,
1396 struct ac_vs_exp_inst
*exp
)
1398 unsigned i
, default_val
; /* SPI_PS_INPUT_CNTL_i.DEFAULT_VAL */
1399 bool is_zero
[4] = {}, is_one
[4] = {};
1401 for (i
= 0; i
< 4; i
++) {
1402 /* It's a constant expression. Undef outputs are eliminated too. */
1403 if (exp
->chan
[i
].type
== AC_IR_UNDEF
) {
1406 } else if (exp
->chan
[i
].type
== AC_IR_CONST
) {
1407 if (exp
->chan
[i
].const_float
== 0)
1409 else if (exp
->chan
[i
].const_float
== 1)
1412 return false; /* other constant */
1417 /* Only certain combinations of 0 and 1 can be eliminated. */
1418 if (is_zero
[0] && is_zero
[1] && is_zero
[2])
1419 default_val
= is_zero
[3] ? 0 : 1;
1420 else if (is_one
[0] && is_one
[1] && is_one
[2])
1421 default_val
= is_zero
[3] ? 2 : 3;
1425 /* The PARAM export can be represented as DEFAULT_VAL. Kill it. */
1426 LLVMInstructionEraseFromParent(exp
->inst
);
1428 /* Change OFFSET to DEFAULT_VAL. */
1429 for (i
= 0; i
< num_outputs
; i
++) {
1430 if (vs_output_param_offset
[i
] == exp
->offset
) {
1431 vs_output_param_offset
[i
] =
1432 AC_EXP_PARAM_DEFAULT_VAL_0000
+ default_val
;
1439 static bool ac_eliminate_duplicated_output(uint8_t *vs_output_param_offset
,
1440 uint32_t num_outputs
,
1441 struct ac_vs_exports
*processed
,
1442 struct ac_vs_exp_inst
*exp
)
1444 unsigned p
, copy_back_channels
= 0;
1446 /* See if the output is already in the list of processed outputs.
1447 * The LLVMValueRef comparison relies on SSA.
1449 for (p
= 0; p
< processed
->num
; p
++) {
1450 bool different
= false;
1452 for (unsigned j
= 0; j
< 4; j
++) {
1453 struct ac_vs_exp_chan
*c1
= &processed
->exp
[p
].chan
[j
];
1454 struct ac_vs_exp_chan
*c2
= &exp
->chan
[j
];
1456 /* Treat undef as a match. */
1457 if (c2
->type
== AC_IR_UNDEF
)
1460 /* If c1 is undef but c2 isn't, we can copy c2 to c1
1461 * and consider the instruction duplicated.
1463 if (c1
->type
== AC_IR_UNDEF
) {
1464 copy_back_channels
|= 1 << j
;
1468 /* Test whether the channels are not equal. */
1469 if (c1
->type
!= c2
->type
||
1470 (c1
->type
== AC_IR_CONST
&&
1471 c1
->const_float
!= c2
->const_float
) ||
1472 (c1
->type
== AC_IR_VALUE
&&
1473 c1
->value
!= c2
->value
)) {
1481 copy_back_channels
= 0;
1483 if (p
== processed
->num
)
1486 /* If a match was found, but the matching export has undef where the new
1487 * one has a normal value, copy the normal value to the undef channel.
1489 struct ac_vs_exp_inst
*match
= &processed
->exp
[p
];
1491 while (copy_back_channels
) {
1492 unsigned chan
= u_bit_scan(©_back_channels
);
1494 assert(match
->chan
[chan
].type
== AC_IR_UNDEF
);
1495 LLVMSetOperand(match
->inst
, AC_EXP_OUT0
+ chan
,
1496 exp
->chan
[chan
].value
);
1497 match
->chan
[chan
] = exp
->chan
[chan
];
1500 /* The PARAM export is duplicated. Kill it. */
1501 LLVMInstructionEraseFromParent(exp
->inst
);
1503 /* Change OFFSET to the matching export. */
1504 for (unsigned i
= 0; i
< num_outputs
; i
++) {
1505 if (vs_output_param_offset
[i
] == exp
->offset
) {
1506 vs_output_param_offset
[i
] = match
->offset
;
1513 void ac_optimize_vs_outputs(struct ac_llvm_context
*ctx
,
1514 LLVMValueRef main_fn
,
1515 uint8_t *vs_output_param_offset
,
1516 uint32_t num_outputs
,
1517 uint8_t *num_param_exports
)
1519 LLVMBasicBlockRef bb
;
1520 bool removed_any
= false;
1521 struct ac_vs_exports exports
;
1525 /* Process all LLVM instructions. */
1526 bb
= LLVMGetFirstBasicBlock(main_fn
);
1528 LLVMValueRef inst
= LLVMGetFirstInstruction(bb
);
1531 LLVMValueRef cur
= inst
;
1532 inst
= LLVMGetNextInstruction(inst
);
1533 struct ac_vs_exp_inst exp
;
1535 if (LLVMGetInstructionOpcode(cur
) != LLVMCall
)
1538 LLVMValueRef callee
= ac_llvm_get_called_value(cur
);
1540 if (!ac_llvm_is_function(callee
))
1543 const char *name
= LLVMGetValueName(callee
);
1544 unsigned num_args
= LLVMCountParams(callee
);
1546 /* Check if this is an export instruction. */
1547 if ((num_args
!= 9 && num_args
!= 8) ||
1548 (strcmp(name
, "llvm.SI.export") &&
1549 strcmp(name
, "llvm.amdgcn.exp.f32")))
1552 LLVMValueRef arg
= LLVMGetOperand(cur
, AC_EXP_TARGET
);
1553 unsigned target
= LLVMConstIntGetZExtValue(arg
);
1555 if (target
< V_008DFC_SQ_EXP_PARAM
)
1558 target
-= V_008DFC_SQ_EXP_PARAM
;
1560 /* Parse the instruction. */
1561 memset(&exp
, 0, sizeof(exp
));
1562 exp
.offset
= target
;
1565 for (unsigned i
= 0; i
< 4; i
++) {
1566 LLVMValueRef v
= LLVMGetOperand(cur
, AC_EXP_OUT0
+ i
);
1568 exp
.chan
[i
].value
= v
;
1570 if (LLVMIsUndef(v
)) {
1571 exp
.chan
[i
].type
= AC_IR_UNDEF
;
1572 } else if (LLVMIsAConstantFP(v
)) {
1573 LLVMBool loses_info
;
1574 exp
.chan
[i
].type
= AC_IR_CONST
;
1575 exp
.chan
[i
].const_float
=
1576 LLVMConstRealGetDouble(v
, &loses_info
);
1578 exp
.chan
[i
].type
= AC_IR_VALUE
;
1582 /* Eliminate constant and duplicated PARAM exports. */
1583 if (ac_eliminate_const_output(vs_output_param_offset
,
1584 num_outputs
, &exp
) ||
1585 ac_eliminate_duplicated_output(vs_output_param_offset
,
1586 num_outputs
, &exports
,
1590 exports
.exp
[exports
.num
++] = exp
;
1593 bb
= LLVMGetNextBasicBlock(bb
);
1596 /* Remove holes in export memory due to removed PARAM exports.
1597 * This is done by renumbering all PARAM exports.
1600 uint8_t old_offset
[VARYING_SLOT_MAX
];
1603 /* Make a copy of the offsets. We need the old version while
1604 * we are modifying some of them. */
1605 memcpy(old_offset
, vs_output_param_offset
,
1606 sizeof(old_offset
));
1608 for (i
= 0; i
< exports
.num
; i
++) {
1609 unsigned offset
= exports
.exp
[i
].offset
;
1611 /* Update vs_output_param_offset. Multiple outputs can
1612 * have the same offset.
1614 for (out
= 0; out
< num_outputs
; out
++) {
1615 if (old_offset
[out
] == offset
)
1616 vs_output_param_offset
[out
] = i
;
1619 /* Change the PARAM offset in the instruction. */
1620 LLVMSetOperand(exports
.exp
[i
].inst
, AC_EXP_TARGET
,
1621 LLVMConstInt(ctx
->i32
,
1622 V_008DFC_SQ_EXP_PARAM
+ i
, 0));
1624 *num_param_exports
= exports
.num
;