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_gather_values_extended(struct ac_llvm_context
*ctx
,
248 LLVMValueRef
*values
,
249 unsigned value_count
,
250 unsigned value_stride
,
254 LLVMBuilderRef builder
= ctx
->builder
;
255 LLVMValueRef vec
= NULL
;
258 if (value_count
== 1 && !always_vector
) {
260 return LLVMBuildLoad(builder
, values
[0], "");
262 } else if (!value_count
)
263 unreachable("value_count is 0");
265 for (i
= 0; i
< value_count
; i
++) {
266 LLVMValueRef value
= values
[i
* value_stride
];
268 value
= LLVMBuildLoad(builder
, value
, "");
271 vec
= LLVMGetUndef( LLVMVectorType(LLVMTypeOf(value
), value_count
));
272 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, i
, false);
273 vec
= LLVMBuildInsertElement(builder
, vec
, value
, index
, "");
279 ac_build_gather_values(struct ac_llvm_context
*ctx
,
280 LLVMValueRef
*values
,
281 unsigned value_count
)
283 return ac_build_gather_values_extended(ctx
, values
, value_count
, 1, false, false);
287 ac_build_fdiv(struct ac_llvm_context
*ctx
,
291 LLVMValueRef ret
= LLVMBuildFDiv(ctx
->builder
, num
, den
, "");
293 if (!LLVMIsConstant(ret
))
294 LLVMSetMetadata(ret
, ctx
->fpmath_md_kind
, ctx
->fpmath_md_2p5_ulp
);
298 /* Coordinates for cube map selection. sc, tc, and ma are as in Table 8.27
299 * of the OpenGL 4.5 (Compatibility Profile) specification, except ma is
300 * already multiplied by two. id is the cube face number.
302 struct cube_selection_coords
{
309 build_cube_intrinsic(struct ac_llvm_context
*ctx
,
311 struct cube_selection_coords
*out
)
313 LLVMTypeRef f32
= ctx
->f32
;
315 out
->stc
[1] = ac_build_intrinsic(ctx
, "llvm.amdgcn.cubetc",
316 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
317 out
->stc
[0] = ac_build_intrinsic(ctx
, "llvm.amdgcn.cubesc",
318 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
319 out
->ma
= ac_build_intrinsic(ctx
, "llvm.amdgcn.cubema",
320 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
321 out
->id
= ac_build_intrinsic(ctx
, "llvm.amdgcn.cubeid",
322 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
326 * Build a manual selection sequence for cube face sc/tc coordinates and
327 * major axis vector (multiplied by 2 for consistency) for the given
328 * vec3 \p coords, for the face implied by \p selcoords.
330 * For the major axis, we always adjust the sign to be in the direction of
331 * selcoords.ma; i.e., a positive out_ma means that coords is pointed towards
332 * the selcoords major axis.
334 static void build_cube_select(LLVMBuilderRef builder
,
335 const struct cube_selection_coords
*selcoords
,
336 const LLVMValueRef
*coords
,
337 LLVMValueRef
*out_st
,
338 LLVMValueRef
*out_ma
)
340 LLVMTypeRef f32
= LLVMTypeOf(coords
[0]);
341 LLVMValueRef is_ma_positive
;
343 LLVMValueRef is_ma_z
, is_not_ma_z
;
344 LLVMValueRef is_ma_y
;
345 LLVMValueRef is_ma_x
;
349 is_ma_positive
= LLVMBuildFCmp(builder
, LLVMRealUGE
,
350 selcoords
->ma
, LLVMConstReal(f32
, 0.0), "");
351 sgn_ma
= LLVMBuildSelect(builder
, is_ma_positive
,
352 LLVMConstReal(f32
, 1.0), LLVMConstReal(f32
, -1.0), "");
354 is_ma_z
= LLVMBuildFCmp(builder
, LLVMRealUGE
, selcoords
->id
, LLVMConstReal(f32
, 4.0), "");
355 is_not_ma_z
= LLVMBuildNot(builder
, is_ma_z
, "");
356 is_ma_y
= LLVMBuildAnd(builder
, is_not_ma_z
,
357 LLVMBuildFCmp(builder
, LLVMRealUGE
, selcoords
->id
, LLVMConstReal(f32
, 2.0), ""), "");
358 is_ma_x
= LLVMBuildAnd(builder
, is_not_ma_z
, LLVMBuildNot(builder
, is_ma_y
, ""), "");
361 tmp
= LLVMBuildSelect(builder
, is_ma_z
, coords
[2], coords
[0], "");
362 sgn
= LLVMBuildSelect(builder
, is_ma_y
, LLVMConstReal(f32
, 1.0),
363 LLVMBuildSelect(builder
, is_ma_x
, sgn_ma
,
364 LLVMBuildFNeg(builder
, sgn_ma
, ""), ""), "");
365 out_st
[0] = LLVMBuildFMul(builder
, tmp
, sgn
, "");
368 tmp
= LLVMBuildSelect(builder
, is_ma_y
, coords
[2], coords
[1], "");
369 sgn
= LLVMBuildSelect(builder
, is_ma_y
, LLVMBuildFNeg(builder
, sgn_ma
, ""),
370 LLVMConstReal(f32
, -1.0), "");
371 out_st
[1] = LLVMBuildFMul(builder
, tmp
, sgn
, "");
374 tmp
= LLVMBuildSelect(builder
, is_ma_z
, coords
[2],
375 LLVMBuildSelect(builder
, is_ma_y
, coords
[1], coords
[0], ""), "");
376 sgn
= LLVMBuildSelect(builder
, is_ma_positive
,
377 LLVMConstReal(f32
, 2.0), LLVMConstReal(f32
, -2.0), "");
378 *out_ma
= LLVMBuildFMul(builder
, tmp
, sgn
, "");
382 ac_prepare_cube_coords(struct ac_llvm_context
*ctx
,
383 bool is_deriv
, bool is_array
,
384 LLVMValueRef
*coords_arg
,
385 LLVMValueRef
*derivs_arg
)
388 LLVMBuilderRef builder
= ctx
->builder
;
389 struct cube_selection_coords selcoords
;
390 LLVMValueRef coords
[3];
393 build_cube_intrinsic(ctx
, coords_arg
, &selcoords
);
395 invma
= ac_build_intrinsic(ctx
, "llvm.fabs.f32",
396 ctx
->f32
, &selcoords
.ma
, 1, AC_FUNC_ATTR_READNONE
);
397 invma
= ac_build_fdiv(ctx
, LLVMConstReal(ctx
->f32
, 1.0), invma
);
399 for (int i
= 0; i
< 2; ++i
)
400 coords
[i
] = LLVMBuildFMul(builder
, selcoords
.stc
[i
], invma
, "");
402 coords
[2] = selcoords
.id
;
404 if (is_deriv
&& derivs_arg
) {
405 LLVMValueRef derivs
[4];
408 /* Convert cube derivatives to 2D derivatives. */
409 for (axis
= 0; axis
< 2; axis
++) {
410 LLVMValueRef deriv_st
[2];
411 LLVMValueRef deriv_ma
;
413 /* Transform the derivative alongside the texture
414 * coordinate. Mathematically, the correct formula is
415 * as follows. Assume we're projecting onto the +Z face
416 * and denote by dx/dh the derivative of the (original)
417 * X texture coordinate with respect to horizontal
418 * window coordinates. The projection onto the +Z face
423 * Then df/dh = df/dx * dx/dh + df/dz * dz/dh
424 * = 1/z * dx/dh - x/z * 1/z * dz/dh.
426 * This motivatives the implementation below.
428 * Whether this actually gives the expected results for
429 * apps that might feed in derivatives obtained via
430 * finite differences is anyone's guess. The OpenGL spec
431 * seems awfully quiet about how textureGrad for cube
432 * maps should be handled.
434 build_cube_select(builder
, &selcoords
, &derivs_arg
[axis
* 3],
435 deriv_st
, &deriv_ma
);
437 deriv_ma
= LLVMBuildFMul(builder
, deriv_ma
, invma
, "");
439 for (int i
= 0; i
< 2; ++i
)
440 derivs
[axis
* 2 + i
] =
441 LLVMBuildFSub(builder
,
442 LLVMBuildFMul(builder
, deriv_st
[i
], invma
, ""),
443 LLVMBuildFMul(builder
, deriv_ma
, coords
[i
], ""), "");
446 memcpy(derivs_arg
, derivs
, sizeof(derivs
));
449 /* Shift the texture coordinate. This must be applied after the
450 * derivative calculation.
452 for (int i
= 0; i
< 2; ++i
)
453 coords
[i
] = LLVMBuildFAdd(builder
, coords
[i
], LLVMConstReal(ctx
->f32
, 1.5), "");
456 /* for cube arrays coord.z = coord.w(array_index) * 8 + face */
457 /* coords_arg.w component - array_index for cube arrays */
458 LLVMValueRef tmp
= LLVMBuildFMul(ctx
->builder
, coords_arg
[3], LLVMConstReal(ctx
->f32
, 8.0), "");
459 coords
[2] = LLVMBuildFAdd(ctx
->builder
, tmp
, coords
[2], "");
462 memcpy(coords_arg
, coords
, sizeof(coords
));
467 ac_build_fs_interp(struct ac_llvm_context
*ctx
,
468 LLVMValueRef llvm_chan
,
469 LLVMValueRef attr_number
,
474 LLVMValueRef args
[5];
477 if (HAVE_LLVM
< 0x0400) {
479 ij
[0] = LLVMBuildBitCast(ctx
->builder
, i
, ctx
->i32
, "");
480 ij
[1] = LLVMBuildBitCast(ctx
->builder
, j
, ctx
->i32
, "");
483 args
[1] = attr_number
;
485 args
[3] = ac_build_gather_values(ctx
, ij
, 2);
486 return ac_build_intrinsic(ctx
, "llvm.SI.fs.interp",
488 AC_FUNC_ATTR_READNONE
);
493 args
[2] = attr_number
;
496 p1
= ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p1",
497 ctx
->f32
, args
, 4, AC_FUNC_ATTR_READNONE
);
502 args
[3] = attr_number
;
505 return ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p2",
506 ctx
->f32
, args
, 5, AC_FUNC_ATTR_READNONE
);
510 ac_build_fs_interp_mov(struct ac_llvm_context
*ctx
,
511 LLVMValueRef parameter
,
512 LLVMValueRef llvm_chan
,
513 LLVMValueRef attr_number
,
516 LLVMValueRef args
[4];
517 if (HAVE_LLVM
< 0x0400) {
519 args
[1] = attr_number
;
522 return ac_build_intrinsic(ctx
,
523 "llvm.SI.fs.constant",
525 AC_FUNC_ATTR_READNONE
);
530 args
[2] = attr_number
;
533 return ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.mov",
534 ctx
->f32
, args
, 4, AC_FUNC_ATTR_READNONE
);
538 ac_build_gep0(struct ac_llvm_context
*ctx
,
539 LLVMValueRef base_ptr
,
542 LLVMValueRef indices
[2] = {
543 LLVMConstInt(ctx
->i32
, 0, 0),
546 return LLVMBuildGEP(ctx
->builder
, base_ptr
,
551 ac_build_indexed_store(struct ac_llvm_context
*ctx
,
552 LLVMValueRef base_ptr
, LLVMValueRef index
,
555 LLVMBuildStore(ctx
->builder
, value
,
556 ac_build_gep0(ctx
, base_ptr
, index
));
560 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad.
561 * It's equivalent to doing a load from &base_ptr[index].
563 * \param base_ptr Where the array starts.
564 * \param index The element index into the array.
565 * \param uniform Whether the base_ptr and index can be assumed to be
566 * dynamically uniform
569 ac_build_indexed_load(struct ac_llvm_context
*ctx
,
570 LLVMValueRef base_ptr
, LLVMValueRef index
,
573 LLVMValueRef pointer
;
575 pointer
= ac_build_gep0(ctx
, base_ptr
, index
);
577 LLVMSetMetadata(pointer
, ctx
->uniform_md_kind
, ctx
->empty_md
);
578 return LLVMBuildLoad(ctx
->builder
, pointer
, "");
582 * Do a load from &base_ptr[index], but also add a flag that it's loading
583 * a constant from a dynamically uniform index.
586 ac_build_indexed_load_const(struct ac_llvm_context
*ctx
,
587 LLVMValueRef base_ptr
, LLVMValueRef index
)
589 LLVMValueRef result
= ac_build_indexed_load(ctx
, base_ptr
, index
, true);
590 LLVMSetMetadata(result
, ctx
->invariant_load_md_kind
, ctx
->empty_md
);
594 /* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
595 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
596 * or v4i32 (num_channels=3,4).
599 ac_build_buffer_store_dword(struct ac_llvm_context
*ctx
,
602 unsigned num_channels
,
603 LLVMValueRef voffset
,
604 LLVMValueRef soffset
,
605 unsigned inst_offset
,
608 bool writeonly_memory
,
611 /* TODO: Fix stores with ADD_TID and remove the "has_add_tid" flag. */
613 /* Split 3 channel stores, becase LLVM doesn't support 3-channel
615 if (num_channels
== 3) {
616 LLVMValueRef v
[3], v01
;
618 for (int i
= 0; i
< 3; i
++) {
619 v
[i
] = LLVMBuildExtractElement(ctx
->builder
, vdata
,
620 LLVMConstInt(ctx
->i32
, i
, 0), "");
622 v01
= ac_build_gather_values(ctx
, v
, 2);
624 ac_build_buffer_store_dword(ctx
, rsrc
, v01
, 2, voffset
,
625 soffset
, inst_offset
, glc
, slc
,
626 writeonly_memory
, has_add_tid
);
627 ac_build_buffer_store_dword(ctx
, rsrc
, v
[2], 1, voffset
,
628 soffset
, inst_offset
+ 8,
630 writeonly_memory
, has_add_tid
);
634 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
635 static const char *types
[] = {"f32", "v2f32", "v4f32"};
637 LLVMValueRef offset
= soffset
;
640 offset
= LLVMBuildAdd(ctx
->builder
, offset
,
641 LLVMConstInt(ctx
->i32
, inst_offset
, 0), "");
643 offset
= LLVMBuildAdd(ctx
->builder
, offset
, voffset
, "");
645 LLVMValueRef args
[] = {
646 bitcast_to_float(ctx
, vdata
),
647 LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, ""),
648 LLVMConstInt(ctx
->i32
, 0, 0),
650 LLVMConstInt(ctx
->i1
, glc
, 0),
651 LLVMConstInt(ctx
->i1
, slc
, 0),
654 snprintf(name
, sizeof(name
), "llvm.amdgcn.buffer.store.%s",
657 ac_build_intrinsic(ctx
, name
, ctx
->voidt
,
658 args
, ARRAY_SIZE(args
),
660 AC_FUNC_ATTR_INACCESSIBLE_MEM_ONLY
:
661 AC_FUNC_ATTR_WRITEONLY
);
665 static unsigned dfmt
[] = {
666 V_008F0C_BUF_DATA_FORMAT_32
,
667 V_008F0C_BUF_DATA_FORMAT_32_32
,
668 V_008F0C_BUF_DATA_FORMAT_32_32_32
,
669 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
671 assert(num_channels
>= 1 && num_channels
<= 4);
673 LLVMValueRef args
[] = {
676 LLVMConstInt(ctx
->i32
, num_channels
, 0),
677 voffset
? voffset
: LLVMGetUndef(ctx
->i32
),
679 LLVMConstInt(ctx
->i32
, inst_offset
, 0),
680 LLVMConstInt(ctx
->i32
, dfmt
[num_channels
- 1], 0),
681 LLVMConstInt(ctx
->i32
, V_008F0C_BUF_NUM_FORMAT_UINT
, 0),
682 LLVMConstInt(ctx
->i32
, voffset
!= NULL
, 0),
683 LLVMConstInt(ctx
->i32
, 0, 0), /* idxen */
684 LLVMConstInt(ctx
->i32
, glc
, 0),
685 LLVMConstInt(ctx
->i32
, slc
, 0),
686 LLVMConstInt(ctx
->i32
, 0, 0), /* tfe*/
689 /* The instruction offset field has 12 bits */
690 assert(voffset
|| inst_offset
< (1 << 12));
692 /* The intrinsic is overloaded, we need to add a type suffix for overloading to work. */
693 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
694 const char *types
[] = {"i32", "v2i32", "v4i32"};
696 snprintf(name
, sizeof(name
), "llvm.SI.tbuffer.store.%s", types
[func
]);
698 ac_build_intrinsic(ctx
, name
, ctx
->voidt
,
699 args
, ARRAY_SIZE(args
),
700 AC_FUNC_ATTR_LEGACY
);
704 ac_build_buffer_load(struct ac_llvm_context
*ctx
,
708 LLVMValueRef voffset
,
709 LLVMValueRef soffset
,
710 unsigned inst_offset
,
716 LLVMValueRef offset
= LLVMConstInt(ctx
->i32
, inst_offset
, 0);
718 offset
= LLVMBuildAdd(ctx
->builder
, offset
, voffset
, "");
720 offset
= LLVMBuildAdd(ctx
->builder
, offset
, soffset
, "");
722 /* TODO: VI and later generations can use SMEM with GLC=1.*/
723 if (allow_smem
&& !glc
&& !slc
) {
724 assert(vindex
== NULL
);
726 LLVMValueRef result
[4];
728 for (int i
= 0; i
< num_channels
; i
++) {
730 offset
= LLVMBuildAdd(ctx
->builder
, offset
,
731 LLVMConstInt(ctx
->i32
, 4, 0), "");
733 LLVMValueRef args
[2] = {rsrc
, offset
};
734 result
[i
] = ac_build_intrinsic(ctx
, "llvm.SI.load.const.v4i32",
736 AC_FUNC_ATTR_READNONE
|
737 AC_FUNC_ATTR_LEGACY
);
739 if (num_channels
== 1)
742 if (num_channels
== 3)
743 result
[num_channels
++] = LLVMGetUndef(ctx
->f32
);
744 return ac_build_gather_values(ctx
, result
, num_channels
);
747 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
749 LLVMValueRef args
[] = {
750 LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, ""),
751 vindex
? vindex
: LLVMConstInt(ctx
->i32
, 0, 0),
753 LLVMConstInt(ctx
->i1
, glc
, 0),
754 LLVMConstInt(ctx
->i1
, slc
, 0)
757 LLVMTypeRef types
[] = {ctx
->f32
, LLVMVectorType(ctx
->f32
, 2),
759 const char *type_names
[] = {"f32", "v2f32", "v4f32"};
762 snprintf(name
, sizeof(name
), "llvm.amdgcn.buffer.load.%s",
765 return ac_build_intrinsic(ctx
, name
, types
[func
], args
,
767 /* READNONE means writes can't affect it, while
768 * READONLY means that writes can affect it. */
769 can_speculate
&& HAVE_LLVM
>= 0x0400 ?
770 AC_FUNC_ATTR_READNONE
:
771 AC_FUNC_ATTR_READONLY
);
774 LLVMValueRef
ac_build_buffer_load_format(struct ac_llvm_context
*ctx
,
777 LLVMValueRef voffset
,
780 LLVMValueRef args
[] = {
781 LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, ""),
784 LLVMConstInt(ctx
->i1
, 0, 0), /* glc */
785 LLVMConstInt(ctx
->i1
, 0, 0), /* slc */
788 return ac_build_intrinsic(ctx
,
789 "llvm.amdgcn.buffer.load.format.v4f32",
790 ctx
->v4f32
, args
, ARRAY_SIZE(args
),
791 /* READNONE means writes can't affect it, while
792 * READONLY means that writes can affect it. */
793 can_speculate
&& HAVE_LLVM
>= 0x0400 ?
794 AC_FUNC_ATTR_READNONE
:
795 AC_FUNC_ATTR_READONLY
);
799 * Set range metadata on an instruction. This can only be used on load and
800 * call instructions. If you know an instruction can only produce the values
801 * 0, 1, 2, you would do set_range_metadata(value, 0, 3);
802 * \p lo is the minimum value inclusive.
803 * \p hi is the maximum value exclusive.
805 static void set_range_metadata(struct ac_llvm_context
*ctx
,
806 LLVMValueRef value
, unsigned lo
, unsigned hi
)
808 LLVMValueRef range_md
, md_args
[2];
809 LLVMTypeRef type
= LLVMTypeOf(value
);
810 LLVMContextRef context
= LLVMGetTypeContext(type
);
812 md_args
[0] = LLVMConstInt(type
, lo
, false);
813 md_args
[1] = LLVMConstInt(type
, hi
, false);
814 range_md
= LLVMMDNodeInContext(context
, md_args
, 2);
815 LLVMSetMetadata(value
, ctx
->range_md_kind
, range_md
);
819 ac_get_thread_id(struct ac_llvm_context
*ctx
)
823 LLVMValueRef tid_args
[2];
824 tid_args
[0] = LLVMConstInt(ctx
->i32
, 0xffffffff, false);
825 tid_args
[1] = LLVMConstInt(ctx
->i32
, 0, false);
826 tid_args
[1] = ac_build_intrinsic(ctx
,
827 "llvm.amdgcn.mbcnt.lo", ctx
->i32
,
828 tid_args
, 2, AC_FUNC_ATTR_READNONE
);
830 tid
= ac_build_intrinsic(ctx
, "llvm.amdgcn.mbcnt.hi",
832 2, AC_FUNC_ATTR_READNONE
);
833 set_range_metadata(ctx
, tid
, 0, 64);
838 * SI implements derivatives using the local data store (LDS)
839 * All writes to the LDS happen in all executing threads at
840 * the same time. TID is the Thread ID for the current
841 * thread and is a value between 0 and 63, representing
842 * the thread's position in the wavefront.
844 * For the pixel shader threads are grouped into quads of four pixels.
845 * The TIDs of the pixels of a quad are:
853 * So, masking the TID with 0xfffffffc yields the TID of the top left pixel
854 * of the quad, masking with 0xfffffffd yields the TID of the top pixel of
855 * the current pixel's column, and masking with 0xfffffffe yields the TID
856 * of the left pixel of the current pixel's row.
858 * Adding 1 yields the TID of the pixel to the right of the left pixel, and
859 * adding 2 yields the TID of the pixel below the top pixel.
862 ac_build_ddxy(struct ac_llvm_context
*ctx
,
863 bool has_ds_bpermute
,
868 LLVMValueRef tl
, trbl
, args
[2];
871 if (has_ds_bpermute
) {
872 LLVMValueRef thread_id
, tl_tid
, trbl_tid
;
873 thread_id
= ac_get_thread_id(ctx
);
875 tl_tid
= LLVMBuildAnd(ctx
->builder
, thread_id
,
876 LLVMConstInt(ctx
->i32
, mask
, false), "");
878 trbl_tid
= LLVMBuildAdd(ctx
->builder
, tl_tid
,
879 LLVMConstInt(ctx
->i32
, idx
, false), "");
881 args
[0] = LLVMBuildMul(ctx
->builder
, tl_tid
,
882 LLVMConstInt(ctx
->i32
, 4, false), "");
884 tl
= ac_build_intrinsic(ctx
,
885 "llvm.amdgcn.ds.bpermute", ctx
->i32
,
887 AC_FUNC_ATTR_READNONE
|
888 AC_FUNC_ATTR_CONVERGENT
);
890 args
[0] = LLVMBuildMul(ctx
->builder
, trbl_tid
,
891 LLVMConstInt(ctx
->i32
, 4, false), "");
892 trbl
= ac_build_intrinsic(ctx
,
893 "llvm.amdgcn.ds.bpermute", ctx
->i32
,
895 AC_FUNC_ATTR_READNONE
|
896 AC_FUNC_ATTR_CONVERGENT
);
901 case AC_TID_MASK_TOP_LEFT
:
909 case AC_TID_MASK_TOP
:
913 case AC_TID_MASK_LEFT
:
920 args
[1] = LLVMConstInt(ctx
->i32
, masks
[0], false);
922 tl
= ac_build_intrinsic(ctx
,
923 "llvm.amdgcn.ds.swizzle", ctx
->i32
,
925 AC_FUNC_ATTR_READNONE
|
926 AC_FUNC_ATTR_CONVERGENT
);
928 args
[1] = LLVMConstInt(ctx
->i32
, masks
[1], false);
929 trbl
= ac_build_intrinsic(ctx
,
930 "llvm.amdgcn.ds.swizzle", ctx
->i32
,
932 AC_FUNC_ATTR_READNONE
|
933 AC_FUNC_ATTR_CONVERGENT
);
936 tl
= LLVMBuildBitCast(ctx
->builder
, tl
, ctx
->f32
, "");
937 trbl
= LLVMBuildBitCast(ctx
->builder
, trbl
, ctx
->f32
, "");
938 result
= LLVMBuildFSub(ctx
->builder
, trbl
, tl
, "");
943 ac_build_sendmsg(struct ac_llvm_context
*ctx
,
945 LLVMValueRef wave_id
)
947 LLVMValueRef args
[2];
948 const char *intr_name
= (HAVE_LLVM
< 0x0400) ? "llvm.SI.sendmsg" : "llvm.amdgcn.s.sendmsg";
949 args
[0] = LLVMConstInt(ctx
->i32
, msg
, false);
951 ac_build_intrinsic(ctx
, intr_name
, ctx
->voidt
, args
, 2, 0);
955 ac_build_imsb(struct ac_llvm_context
*ctx
,
957 LLVMTypeRef dst_type
)
959 const char *intr_name
= (HAVE_LLVM
< 0x0400) ? "llvm.AMDGPU.flbit.i32" :
960 "llvm.amdgcn.sffbh.i32";
961 LLVMValueRef msb
= ac_build_intrinsic(ctx
, intr_name
,
963 AC_FUNC_ATTR_READNONE
);
965 /* The HW returns the last bit index from MSB, but NIR/TGSI wants
966 * the index from LSB. Invert it by doing "31 - msb". */
967 msb
= LLVMBuildSub(ctx
->builder
, LLVMConstInt(ctx
->i32
, 31, false),
970 LLVMValueRef all_ones
= LLVMConstInt(ctx
->i32
, -1, true);
971 LLVMValueRef cond
= LLVMBuildOr(ctx
->builder
,
972 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
973 arg
, LLVMConstInt(ctx
->i32
, 0, 0), ""),
974 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
975 arg
, all_ones
, ""), "");
977 return LLVMBuildSelect(ctx
->builder
, cond
, all_ones
, msb
, "");
981 ac_build_umsb(struct ac_llvm_context
*ctx
,
983 LLVMTypeRef dst_type
)
985 LLVMValueRef args
[2] = {
987 LLVMConstInt(ctx
->i1
, 1, 0),
989 LLVMValueRef msb
= ac_build_intrinsic(ctx
, "llvm.ctlz.i32",
990 dst_type
, args
, ARRAY_SIZE(args
),
991 AC_FUNC_ATTR_READNONE
);
993 /* The HW returns the last bit index from MSB, but TGSI/NIR wants
994 * the index from LSB. Invert it by doing "31 - msb". */
995 msb
= LLVMBuildSub(ctx
->builder
, LLVMConstInt(ctx
->i32
, 31, false),
999 return LLVMBuildSelect(ctx
->builder
,
1000 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, arg
,
1001 LLVMConstInt(ctx
->i32
, 0, 0), ""),
1002 LLVMConstInt(ctx
->i32
, -1, true), msb
, "");
1005 LLVMValueRef
ac_build_umin(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
1008 LLVMValueRef cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntULE
, a
, b
, "");
1009 return LLVMBuildSelect(ctx
->builder
, cmp
, a
, b
, "");
1012 LLVMValueRef
ac_build_clamp(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
1014 if (HAVE_LLVM
>= 0x0500) {
1015 LLVMValueRef max
[2] = {
1017 LLVMConstReal(ctx
->f32
, 0),
1019 LLVMValueRef min
[2] = {
1020 LLVMConstReal(ctx
->f32
, 1),
1023 min
[1] = ac_build_intrinsic(ctx
, "llvm.maxnum.f32",
1025 AC_FUNC_ATTR_READNONE
);
1026 return ac_build_intrinsic(ctx
, "llvm.minnum.f32",
1028 AC_FUNC_ATTR_READNONE
);
1031 LLVMValueRef args
[3] = {
1033 LLVMConstReal(ctx
->f32
, 0),
1034 LLVMConstReal(ctx
->f32
, 1),
1037 return ac_build_intrinsic(ctx
, "llvm.AMDGPU.clamp.", ctx
->f32
, args
, 3,
1038 AC_FUNC_ATTR_READNONE
|
1039 AC_FUNC_ATTR_LEGACY
);
1042 void ac_build_export(struct ac_llvm_context
*ctx
, struct ac_export_args
*a
)
1044 LLVMValueRef args
[9];
1046 if (HAVE_LLVM
>= 0x0500) {
1047 args
[0] = LLVMConstInt(ctx
->i32
, a
->target
, 0);
1048 args
[1] = LLVMConstInt(ctx
->i32
, a
->enabled_channels
, 0);
1051 LLVMTypeRef i16
= LLVMInt16TypeInContext(ctx
->context
);
1052 LLVMTypeRef v2i16
= LLVMVectorType(i16
, 2);
1054 args
[2] = LLVMBuildBitCast(ctx
->builder
, a
->out
[0],
1056 args
[3] = LLVMBuildBitCast(ctx
->builder
, a
->out
[1],
1058 args
[4] = LLVMConstInt(ctx
->i1
, a
->done
, 0);
1059 args
[5] = LLVMConstInt(ctx
->i1
, a
->valid_mask
, 0);
1061 ac_build_intrinsic(ctx
, "llvm.amdgcn.exp.compr.v2i16",
1062 ctx
->voidt
, args
, 6, 0);
1064 args
[2] = a
->out
[0];
1065 args
[3] = a
->out
[1];
1066 args
[4] = a
->out
[2];
1067 args
[5] = a
->out
[3];
1068 args
[6] = LLVMConstInt(ctx
->i1
, a
->done
, 0);
1069 args
[7] = LLVMConstInt(ctx
->i1
, a
->valid_mask
, 0);
1071 ac_build_intrinsic(ctx
, "llvm.amdgcn.exp.f32",
1072 ctx
->voidt
, args
, 8, 0);
1077 args
[0] = LLVMConstInt(ctx
->i32
, a
->enabled_channels
, 0);
1078 args
[1] = LLVMConstInt(ctx
->i32
, a
->valid_mask
, 0);
1079 args
[2] = LLVMConstInt(ctx
->i32
, a
->done
, 0);
1080 args
[3] = LLVMConstInt(ctx
->i32
, a
->target
, 0);
1081 args
[4] = LLVMConstInt(ctx
->i32
, a
->compr
, 0);
1082 memcpy(args
+ 5, a
->out
, sizeof(a
->out
[0]) * 4);
1084 ac_build_intrinsic(ctx
, "llvm.SI.export", ctx
->voidt
, args
, 9,
1085 AC_FUNC_ATTR_LEGACY
);
1088 LLVMValueRef
ac_build_image_opcode(struct ac_llvm_context
*ctx
,
1089 struct ac_image_args
*a
)
1091 LLVMTypeRef dst_type
;
1092 LLVMValueRef args
[11];
1093 unsigned num_args
= 0;
1095 char intr_name
[128], type
[64];
1097 if (HAVE_LLVM
>= 0x0400) {
1098 bool sample
= a
->opcode
== ac_image_sample
||
1099 a
->opcode
== ac_image_gather4
||
1100 a
->opcode
== ac_image_get_lod
;
1103 args
[num_args
++] = bitcast_to_float(ctx
, a
->addr
);
1105 args
[num_args
++] = a
->addr
;
1107 args
[num_args
++] = a
->resource
;
1109 args
[num_args
++] = a
->sampler
;
1110 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->dmask
, 0);
1112 args
[num_args
++] = LLVMConstInt(ctx
->i1
, a
->unorm
, 0);
1113 args
[num_args
++] = LLVMConstInt(ctx
->i1
, 0, 0); /* glc */
1114 args
[num_args
++] = LLVMConstInt(ctx
->i1
, 0, 0); /* slc */
1115 args
[num_args
++] = LLVMConstInt(ctx
->i1
, 0, 0); /* lwe */
1116 args
[num_args
++] = LLVMConstInt(ctx
->i1
, a
->da
, 0);
1118 switch (a
->opcode
) {
1119 case ac_image_sample
:
1120 name
= "llvm.amdgcn.image.sample";
1122 case ac_image_gather4
:
1123 name
= "llvm.amdgcn.image.gather4";
1126 name
= "llvm.amdgcn.image.load";
1128 case ac_image_load_mip
:
1129 name
= "llvm.amdgcn.image.load.mip";
1131 case ac_image_get_lod
:
1132 name
= "llvm.amdgcn.image.getlod";
1134 case ac_image_get_resinfo
:
1135 name
= "llvm.amdgcn.image.getresinfo";
1138 unreachable("invalid image opcode");
1141 ac_build_type_name_for_intr(LLVMTypeOf(args
[0]), type
,
1144 snprintf(intr_name
, sizeof(intr_name
), "%s%s%s%s.v4f32.%s.v8i32",
1146 a
->compare
? ".c" : "",
1150 a
->level_zero
? ".lz" : "",
1151 a
->offset
? ".o" : "",
1154 LLVMValueRef result
=
1155 ac_build_intrinsic(ctx
, intr_name
,
1156 ctx
->v4f32
, args
, num_args
,
1157 AC_FUNC_ATTR_READNONE
);
1159 result
= LLVMBuildBitCast(ctx
->builder
, result
,
1165 args
[num_args
++] = a
->addr
;
1166 args
[num_args
++] = a
->resource
;
1168 if (a
->opcode
== ac_image_load
||
1169 a
->opcode
== ac_image_load_mip
||
1170 a
->opcode
== ac_image_get_resinfo
) {
1171 dst_type
= ctx
->v4i32
;
1173 dst_type
= ctx
->v4f32
;
1174 args
[num_args
++] = a
->sampler
;
1177 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->dmask
, 0);
1178 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->unorm
, 0);
1179 args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* r128 */
1180 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->da
, 0);
1181 args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* glc */
1182 args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* slc */
1183 args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* tfe */
1184 args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* lwe */
1186 switch (a
->opcode
) {
1187 case ac_image_sample
:
1188 name
= "llvm.SI.image.sample";
1190 case ac_image_gather4
:
1191 name
= "llvm.SI.gather4";
1194 name
= "llvm.SI.image.load";
1196 case ac_image_load_mip
:
1197 name
= "llvm.SI.image.load.mip";
1199 case ac_image_get_lod
:
1200 name
= "llvm.SI.getlod";
1202 case ac_image_get_resinfo
:
1203 name
= "llvm.SI.getresinfo";
1207 ac_build_type_name_for_intr(LLVMTypeOf(a
->addr
), type
, sizeof(type
));
1208 snprintf(intr_name
, sizeof(intr_name
), "%s%s%s%s.%s",
1210 a
->compare
? ".c" : "",
1214 a
->level_zero
? ".lz" : "",
1215 a
->offset
? ".o" : "",
1218 return ac_build_intrinsic(ctx
, intr_name
,
1219 dst_type
, args
, num_args
,
1220 AC_FUNC_ATTR_READNONE
|
1221 AC_FUNC_ATTR_LEGACY
);
1224 LLVMValueRef
ac_build_cvt_pkrtz_f16(struct ac_llvm_context
*ctx
,
1225 LLVMValueRef args
[2])
1227 if (HAVE_LLVM
>= 0x0500) {
1229 LLVMVectorType(LLVMHalfTypeInContext(ctx
->context
), 2);
1231 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pkrtz",
1233 AC_FUNC_ATTR_READNONE
);
1234 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
1237 return ac_build_intrinsic(ctx
, "llvm.SI.packf16", ctx
->i32
, args
, 2,
1238 AC_FUNC_ATTR_READNONE
|
1239 AC_FUNC_ATTR_LEGACY
);
1243 * KILL, AKA discard in GLSL.
1245 * \param value kill if value < 0.0 or value == NULL.
1247 void ac_build_kill(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
1250 ac_build_intrinsic(ctx
, "llvm.AMDGPU.kill", ctx
->voidt
,
1251 &value
, 1, AC_FUNC_ATTR_LEGACY
);
1253 ac_build_intrinsic(ctx
, "llvm.AMDGPU.kilp", ctx
->voidt
,
1254 NULL
, 0, AC_FUNC_ATTR_LEGACY
);
1258 LLVMValueRef
ac_build_bfe(struct ac_llvm_context
*ctx
, LLVMValueRef input
,
1259 LLVMValueRef offset
, LLVMValueRef width
,
1262 LLVMValueRef args
[] = {
1268 if (HAVE_LLVM
>= 0x0500) {
1269 return ac_build_intrinsic(ctx
,
1270 is_signed
? "llvm.amdgcn.sbfe.i32" :
1271 "llvm.amdgcn.ubfe.i32",
1273 AC_FUNC_ATTR_READNONE
);
1276 return ac_build_intrinsic(ctx
,
1277 is_signed
? "llvm.AMDGPU.bfe.i32" :
1278 "llvm.AMDGPU.bfe.u32",
1280 AC_FUNC_ATTR_READNONE
|
1281 AC_FUNC_ATTR_LEGACY
);
1284 void ac_get_image_intr_name(const char *base_name
,
1285 LLVMTypeRef data_type
,
1286 LLVMTypeRef coords_type
,
1287 LLVMTypeRef rsrc_type
,
1288 char *out_name
, unsigned out_len
)
1290 char coords_type_name
[8];
1292 ac_build_type_name_for_intr(coords_type
, coords_type_name
,
1293 sizeof(coords_type_name
));
1295 if (HAVE_LLVM
<= 0x0309) {
1296 snprintf(out_name
, out_len
, "%s.%s", base_name
, coords_type_name
);
1298 char data_type_name
[8];
1299 char rsrc_type_name
[8];
1301 ac_build_type_name_for_intr(data_type
, data_type_name
,
1302 sizeof(data_type_name
));
1303 ac_build_type_name_for_intr(rsrc_type
, rsrc_type_name
,
1304 sizeof(rsrc_type_name
));
1305 snprintf(out_name
, out_len
, "%s.%s.%s.%s", base_name
,
1306 data_type_name
, coords_type_name
, rsrc_type_name
);
1310 #define AC_EXP_TARGET (HAVE_LLVM >= 0x0500 ? 0 : 3)
1311 #define AC_EXP_OUT0 (HAVE_LLVM >= 0x0500 ? 2 : 5)
1319 struct ac_vs_exp_chan
1323 enum ac_ir_type type
;
1326 struct ac_vs_exp_inst
{
1329 struct ac_vs_exp_chan chan
[4];
1332 struct ac_vs_exports
{
1334 struct ac_vs_exp_inst exp
[VARYING_SLOT_MAX
];
1337 /* Return true if the PARAM export has been eliminated. */
1338 static bool ac_eliminate_const_output(uint8_t *vs_output_param_offset
,
1339 uint32_t num_outputs
,
1340 struct ac_vs_exp_inst
*exp
)
1342 unsigned i
, default_val
; /* SPI_PS_INPUT_CNTL_i.DEFAULT_VAL */
1343 bool is_zero
[4] = {}, is_one
[4] = {};
1345 for (i
= 0; i
< 4; i
++) {
1346 /* It's a constant expression. Undef outputs are eliminated too. */
1347 if (exp
->chan
[i
].type
== AC_IR_UNDEF
) {
1350 } else if (exp
->chan
[i
].type
== AC_IR_CONST
) {
1351 if (exp
->chan
[i
].const_float
== 0)
1353 else if (exp
->chan
[i
].const_float
== 1)
1356 return false; /* other constant */
1361 /* Only certain combinations of 0 and 1 can be eliminated. */
1362 if (is_zero
[0] && is_zero
[1] && is_zero
[2])
1363 default_val
= is_zero
[3] ? 0 : 1;
1364 else if (is_one
[0] && is_one
[1] && is_one
[2])
1365 default_val
= is_zero
[3] ? 2 : 3;
1369 /* The PARAM export can be represented as DEFAULT_VAL. Kill it. */
1370 LLVMInstructionEraseFromParent(exp
->inst
);
1372 /* Change OFFSET to DEFAULT_VAL. */
1373 for (i
= 0; i
< num_outputs
; i
++) {
1374 if (vs_output_param_offset
[i
] == exp
->offset
) {
1375 vs_output_param_offset
[i
] =
1376 AC_EXP_PARAM_DEFAULT_VAL_0000
+ default_val
;
1383 static bool ac_eliminate_duplicated_output(uint8_t *vs_output_param_offset
,
1384 uint32_t num_outputs
,
1385 struct ac_vs_exports
*processed
,
1386 struct ac_vs_exp_inst
*exp
)
1388 unsigned p
, copy_back_channels
= 0;
1390 /* See if the output is already in the list of processed outputs.
1391 * The LLVMValueRef comparison relies on SSA.
1393 for (p
= 0; p
< processed
->num
; p
++) {
1394 bool different
= false;
1396 for (unsigned j
= 0; j
< 4; j
++) {
1397 struct ac_vs_exp_chan
*c1
= &processed
->exp
[p
].chan
[j
];
1398 struct ac_vs_exp_chan
*c2
= &exp
->chan
[j
];
1400 /* Treat undef as a match. */
1401 if (c2
->type
== AC_IR_UNDEF
)
1404 /* If c1 is undef but c2 isn't, we can copy c2 to c1
1405 * and consider the instruction duplicated.
1407 if (c1
->type
== AC_IR_UNDEF
) {
1408 copy_back_channels
|= 1 << j
;
1412 /* Test whether the channels are not equal. */
1413 if (c1
->type
!= c2
->type
||
1414 (c1
->type
== AC_IR_CONST
&&
1415 c1
->const_float
!= c2
->const_float
) ||
1416 (c1
->type
== AC_IR_VALUE
&&
1417 c1
->value
!= c2
->value
)) {
1425 copy_back_channels
= 0;
1427 if (p
== processed
->num
)
1430 /* If a match was found, but the matching export has undef where the new
1431 * one has a normal value, copy the normal value to the undef channel.
1433 struct ac_vs_exp_inst
*match
= &processed
->exp
[p
];
1435 while (copy_back_channels
) {
1436 unsigned chan
= u_bit_scan(©_back_channels
);
1438 assert(match
->chan
[chan
].type
== AC_IR_UNDEF
);
1439 LLVMSetOperand(match
->inst
, AC_EXP_OUT0
+ chan
,
1440 exp
->chan
[chan
].value
);
1441 match
->chan
[chan
] = exp
->chan
[chan
];
1444 /* The PARAM export is duplicated. Kill it. */
1445 LLVMInstructionEraseFromParent(exp
->inst
);
1447 /* Change OFFSET to the matching export. */
1448 for (unsigned i
= 0; i
< num_outputs
; i
++) {
1449 if (vs_output_param_offset
[i
] == exp
->offset
) {
1450 vs_output_param_offset
[i
] = match
->offset
;
1457 void ac_optimize_vs_outputs(struct ac_llvm_context
*ctx
,
1458 LLVMValueRef main_fn
,
1459 uint8_t *vs_output_param_offset
,
1460 uint32_t num_outputs
,
1461 uint8_t *num_param_exports
)
1463 LLVMBasicBlockRef bb
;
1464 bool removed_any
= false;
1465 struct ac_vs_exports exports
;
1469 /* Process all LLVM instructions. */
1470 bb
= LLVMGetFirstBasicBlock(main_fn
);
1472 LLVMValueRef inst
= LLVMGetFirstInstruction(bb
);
1475 LLVMValueRef cur
= inst
;
1476 inst
= LLVMGetNextInstruction(inst
);
1477 struct ac_vs_exp_inst exp
;
1479 if (LLVMGetInstructionOpcode(cur
) != LLVMCall
)
1482 LLVMValueRef callee
= ac_llvm_get_called_value(cur
);
1484 if (!ac_llvm_is_function(callee
))
1487 const char *name
= LLVMGetValueName(callee
);
1488 unsigned num_args
= LLVMCountParams(callee
);
1490 /* Check if this is an export instruction. */
1491 if ((num_args
!= 9 && num_args
!= 8) ||
1492 (strcmp(name
, "llvm.SI.export") &&
1493 strcmp(name
, "llvm.amdgcn.exp.f32")))
1496 LLVMValueRef arg
= LLVMGetOperand(cur
, AC_EXP_TARGET
);
1497 unsigned target
= LLVMConstIntGetZExtValue(arg
);
1499 if (target
< V_008DFC_SQ_EXP_PARAM
)
1502 target
-= V_008DFC_SQ_EXP_PARAM
;
1504 /* Parse the instruction. */
1505 memset(&exp
, 0, sizeof(exp
));
1506 exp
.offset
= target
;
1509 for (unsigned i
= 0; i
< 4; i
++) {
1510 LLVMValueRef v
= LLVMGetOperand(cur
, AC_EXP_OUT0
+ i
);
1512 exp
.chan
[i
].value
= v
;
1514 if (LLVMIsUndef(v
)) {
1515 exp
.chan
[i
].type
= AC_IR_UNDEF
;
1516 } else if (LLVMIsAConstantFP(v
)) {
1517 LLVMBool loses_info
;
1518 exp
.chan
[i
].type
= AC_IR_CONST
;
1519 exp
.chan
[i
].const_float
=
1520 LLVMConstRealGetDouble(v
, &loses_info
);
1522 exp
.chan
[i
].type
= AC_IR_VALUE
;
1526 /* Eliminate constant and duplicated PARAM exports. */
1527 if (ac_eliminate_const_output(vs_output_param_offset
,
1528 num_outputs
, &exp
) ||
1529 ac_eliminate_duplicated_output(vs_output_param_offset
,
1530 num_outputs
, &exports
,
1534 exports
.exp
[exports
.num
++] = exp
;
1537 bb
= LLVMGetNextBasicBlock(bb
);
1540 /* Remove holes in export memory due to removed PARAM exports.
1541 * This is done by renumbering all PARAM exports.
1544 uint8_t old_offset
[VARYING_SLOT_MAX
];
1547 /* Make a copy of the offsets. We need the old version while
1548 * we are modifying some of them. */
1549 memcpy(old_offset
, vs_output_param_offset
,
1550 sizeof(old_offset
));
1552 for (i
= 0; i
< exports
.num
; i
++) {
1553 unsigned offset
= exports
.exp
[i
].offset
;
1555 /* Update vs_output_param_offset. Multiple outputs can
1556 * have the same offset.
1558 for (out
= 0; out
< num_outputs
; out
++) {
1559 if (old_offset
[out
] == offset
)
1560 vs_output_param_offset
[out
] = i
;
1563 /* Change the PARAM offset in the instruction. */
1564 LLVMSetOperand(exports
.exp
[i
].inst
, AC_EXP_TARGET
,
1565 LLVMConstInt(ctx
->i32
,
1566 V_008DFC_SQ_EXP_PARAM
+ i
, 0));
1568 *num_param_exports
= exports
.num
;