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"
41 #include "shader_enums.h"
43 /* Initialize module-independent parts of the context.
45 * The caller is responsible for initializing ctx::module and ctx::builder.
48 ac_llvm_context_init(struct ac_llvm_context
*ctx
, LLVMContextRef context
)
52 ctx
->context
= context
;
56 ctx
->voidt
= LLVMVoidTypeInContext(ctx
->context
);
57 ctx
->i1
= LLVMInt1TypeInContext(ctx
->context
);
58 ctx
->i8
= LLVMInt8TypeInContext(ctx
->context
);
59 ctx
->i16
= LLVMIntTypeInContext(ctx
->context
, 16);
60 ctx
->i32
= LLVMIntTypeInContext(ctx
->context
, 32);
61 ctx
->i64
= LLVMIntTypeInContext(ctx
->context
, 64);
62 ctx
->f16
= LLVMHalfTypeInContext(ctx
->context
);
63 ctx
->f32
= LLVMFloatTypeInContext(ctx
->context
);
64 ctx
->f64
= LLVMDoubleTypeInContext(ctx
->context
);
65 ctx
->v4i32
= LLVMVectorType(ctx
->i32
, 4);
66 ctx
->v4f32
= LLVMVectorType(ctx
->f32
, 4);
67 ctx
->v8i32
= LLVMVectorType(ctx
->i32
, 8);
69 ctx
->i32_0
= LLVMConstInt(ctx
->i32
, 0, false);
70 ctx
->i32_1
= LLVMConstInt(ctx
->i32
, 1, false);
71 ctx
->f32_0
= LLVMConstReal(ctx
->f32
, 0.0);
72 ctx
->f32_1
= LLVMConstReal(ctx
->f32
, 1.0);
74 ctx
->range_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
77 ctx
->invariant_load_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
78 "invariant.load", 14);
80 ctx
->fpmath_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
, "fpmath", 6);
82 args
[0] = LLVMConstReal(ctx
->f32
, 2.5);
83 ctx
->fpmath_md_2p5_ulp
= LLVMMDNodeInContext(ctx
->context
, args
, 1);
85 ctx
->uniform_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
86 "amdgpu.uniform", 14);
88 ctx
->empty_md
= LLVMMDNodeInContext(ctx
->context
, NULL
, 0);
92 ac_build_intrinsic(struct ac_llvm_context
*ctx
, const char *name
,
93 LLVMTypeRef return_type
, LLVMValueRef
*params
,
94 unsigned param_count
, unsigned attrib_mask
)
96 LLVMValueRef function
, call
;
97 bool set_callsite_attrs
= HAVE_LLVM
>= 0x0400 &&
98 !(attrib_mask
& AC_FUNC_ATTR_LEGACY
);
100 function
= LLVMGetNamedFunction(ctx
->module
, name
);
102 LLVMTypeRef param_types
[32], function_type
;
105 assert(param_count
<= 32);
107 for (i
= 0; i
< param_count
; ++i
) {
109 param_types
[i
] = LLVMTypeOf(params
[i
]);
112 LLVMFunctionType(return_type
, param_types
, param_count
, 0);
113 function
= LLVMAddFunction(ctx
->module
, name
, function_type
);
115 LLVMSetFunctionCallConv(function
, LLVMCCallConv
);
116 LLVMSetLinkage(function
, LLVMExternalLinkage
);
118 if (!set_callsite_attrs
)
119 ac_add_func_attributes(ctx
->context
, function
, attrib_mask
);
122 call
= LLVMBuildCall(ctx
->builder
, function
, params
, param_count
, "");
123 if (set_callsite_attrs
)
124 ac_add_func_attributes(ctx
->context
, call
, attrib_mask
);
128 static LLVMValueRef
bitcast_to_float(struct ac_llvm_context
*ctx
,
131 LLVMTypeRef type
= LLVMTypeOf(value
);
132 LLVMTypeRef new_type
;
134 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
)
135 new_type
= LLVMVectorType(ctx
->f32
, LLVMGetVectorSize(type
));
139 return LLVMBuildBitCast(ctx
->builder
, value
, new_type
, "");
143 * Given the i32 or vNi32 \p type, generate the textual name (e.g. for use with
146 void ac_build_type_name_for_intr(LLVMTypeRef type
, char *buf
, unsigned bufsize
)
148 LLVMTypeRef elem_type
= type
;
150 assert(bufsize
>= 8);
152 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
) {
153 int ret
= snprintf(buf
, bufsize
, "v%u",
154 LLVMGetVectorSize(type
));
156 char *type_name
= LLVMPrintTypeToString(type
);
157 fprintf(stderr
, "Error building type name for: %s\n",
161 elem_type
= LLVMGetElementType(type
);
165 switch (LLVMGetTypeKind(elem_type
)) {
167 case LLVMIntegerTypeKind
:
168 snprintf(buf
, bufsize
, "i%d", LLVMGetIntTypeWidth(elem_type
));
170 case LLVMFloatTypeKind
:
171 snprintf(buf
, bufsize
, "f32");
173 case LLVMDoubleTypeKind
:
174 snprintf(buf
, bufsize
, "f64");
180 ac_build_gather_values_extended(struct ac_llvm_context
*ctx
,
181 LLVMValueRef
*values
,
182 unsigned value_count
,
183 unsigned value_stride
,
186 LLVMBuilderRef builder
= ctx
->builder
;
187 LLVMValueRef vec
= NULL
;
190 if (value_count
== 1) {
192 return LLVMBuildLoad(builder
, values
[0], "");
194 } else if (!value_count
)
195 unreachable("value_count is 0");
197 for (i
= 0; i
< value_count
; i
++) {
198 LLVMValueRef value
= values
[i
* value_stride
];
200 value
= LLVMBuildLoad(builder
, value
, "");
203 vec
= LLVMGetUndef( LLVMVectorType(LLVMTypeOf(value
), value_count
));
204 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, i
, false);
205 vec
= LLVMBuildInsertElement(builder
, vec
, value
, index
, "");
211 ac_build_gather_values(struct ac_llvm_context
*ctx
,
212 LLVMValueRef
*values
,
213 unsigned value_count
)
215 return ac_build_gather_values_extended(ctx
, values
, value_count
, 1, false);
219 ac_build_fdiv(struct ac_llvm_context
*ctx
,
223 LLVMValueRef ret
= LLVMBuildFDiv(ctx
->builder
, num
, den
, "");
225 if (!LLVMIsConstant(ret
))
226 LLVMSetMetadata(ret
, ctx
->fpmath_md_kind
, ctx
->fpmath_md_2p5_ulp
);
230 /* Coordinates for cube map selection. sc, tc, and ma are as in Table 8.27
231 * of the OpenGL 4.5 (Compatibility Profile) specification, except ma is
232 * already multiplied by two. id is the cube face number.
234 struct cube_selection_coords
{
241 build_cube_intrinsic(struct ac_llvm_context
*ctx
,
243 struct cube_selection_coords
*out
)
245 LLVMTypeRef f32
= ctx
->f32
;
247 out
->stc
[1] = ac_build_intrinsic(ctx
, "llvm.amdgcn.cubetc",
248 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
249 out
->stc
[0] = ac_build_intrinsic(ctx
, "llvm.amdgcn.cubesc",
250 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
251 out
->ma
= ac_build_intrinsic(ctx
, "llvm.amdgcn.cubema",
252 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
253 out
->id
= ac_build_intrinsic(ctx
, "llvm.amdgcn.cubeid",
254 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
258 * Build a manual selection sequence for cube face sc/tc coordinates and
259 * major axis vector (multiplied by 2 for consistency) for the given
260 * vec3 \p coords, for the face implied by \p selcoords.
262 * For the major axis, we always adjust the sign to be in the direction of
263 * selcoords.ma; i.e., a positive out_ma means that coords is pointed towards
264 * the selcoords major axis.
266 static void build_cube_select(LLVMBuilderRef builder
,
267 const struct cube_selection_coords
*selcoords
,
268 const LLVMValueRef
*coords
,
269 LLVMValueRef
*out_st
,
270 LLVMValueRef
*out_ma
)
272 LLVMTypeRef f32
= LLVMTypeOf(coords
[0]);
273 LLVMValueRef is_ma_positive
;
275 LLVMValueRef is_ma_z
, is_not_ma_z
;
276 LLVMValueRef is_ma_y
;
277 LLVMValueRef is_ma_x
;
281 is_ma_positive
= LLVMBuildFCmp(builder
, LLVMRealUGE
,
282 selcoords
->ma
, LLVMConstReal(f32
, 0.0), "");
283 sgn_ma
= LLVMBuildSelect(builder
, is_ma_positive
,
284 LLVMConstReal(f32
, 1.0), LLVMConstReal(f32
, -1.0), "");
286 is_ma_z
= LLVMBuildFCmp(builder
, LLVMRealUGE
, selcoords
->id
, LLVMConstReal(f32
, 4.0), "");
287 is_not_ma_z
= LLVMBuildNot(builder
, is_ma_z
, "");
288 is_ma_y
= LLVMBuildAnd(builder
, is_not_ma_z
,
289 LLVMBuildFCmp(builder
, LLVMRealUGE
, selcoords
->id
, LLVMConstReal(f32
, 2.0), ""), "");
290 is_ma_x
= LLVMBuildAnd(builder
, is_not_ma_z
, LLVMBuildNot(builder
, is_ma_y
, ""), "");
293 tmp
= LLVMBuildSelect(builder
, is_ma_z
, coords
[2], coords
[0], "");
294 sgn
= LLVMBuildSelect(builder
, is_ma_y
, LLVMConstReal(f32
, 1.0),
295 LLVMBuildSelect(builder
, is_ma_x
, sgn_ma
,
296 LLVMBuildFNeg(builder
, sgn_ma
, ""), ""), "");
297 out_st
[0] = LLVMBuildFMul(builder
, tmp
, sgn
, "");
300 tmp
= LLVMBuildSelect(builder
, is_ma_y
, coords
[2], coords
[1], "");
301 sgn
= LLVMBuildSelect(builder
, is_ma_y
, LLVMBuildFNeg(builder
, sgn_ma
, ""),
302 LLVMConstReal(f32
, -1.0), "");
303 out_st
[1] = LLVMBuildFMul(builder
, tmp
, sgn
, "");
306 tmp
= LLVMBuildSelect(builder
, is_ma_z
, coords
[2],
307 LLVMBuildSelect(builder
, is_ma_y
, coords
[1], coords
[0], ""), "");
308 sgn
= LLVMBuildSelect(builder
, is_ma_positive
,
309 LLVMConstReal(f32
, 2.0), LLVMConstReal(f32
, -2.0), "");
310 *out_ma
= LLVMBuildFMul(builder
, tmp
, sgn
, "");
314 ac_prepare_cube_coords(struct ac_llvm_context
*ctx
,
315 bool is_deriv
, bool is_array
,
316 LLVMValueRef
*coords_arg
,
317 LLVMValueRef
*derivs_arg
)
320 LLVMBuilderRef builder
= ctx
->builder
;
321 struct cube_selection_coords selcoords
;
322 LLVMValueRef coords
[3];
325 build_cube_intrinsic(ctx
, coords_arg
, &selcoords
);
327 invma
= ac_build_intrinsic(ctx
, "llvm.fabs.f32",
328 ctx
->f32
, &selcoords
.ma
, 1, AC_FUNC_ATTR_READNONE
);
329 invma
= ac_build_fdiv(ctx
, LLVMConstReal(ctx
->f32
, 1.0), invma
);
331 for (int i
= 0; i
< 2; ++i
)
332 coords
[i
] = LLVMBuildFMul(builder
, selcoords
.stc
[i
], invma
, "");
334 coords
[2] = selcoords
.id
;
336 if (is_deriv
&& derivs_arg
) {
337 LLVMValueRef derivs
[4];
340 /* Convert cube derivatives to 2D derivatives. */
341 for (axis
= 0; axis
< 2; axis
++) {
342 LLVMValueRef deriv_st
[2];
343 LLVMValueRef deriv_ma
;
345 /* Transform the derivative alongside the texture
346 * coordinate. Mathematically, the correct formula is
347 * as follows. Assume we're projecting onto the +Z face
348 * and denote by dx/dh the derivative of the (original)
349 * X texture coordinate with respect to horizontal
350 * window coordinates. The projection onto the +Z face
355 * Then df/dh = df/dx * dx/dh + df/dz * dz/dh
356 * = 1/z * dx/dh - x/z * 1/z * dz/dh.
358 * This motivatives the implementation below.
360 * Whether this actually gives the expected results for
361 * apps that might feed in derivatives obtained via
362 * finite differences is anyone's guess. The OpenGL spec
363 * seems awfully quiet about how textureGrad for cube
364 * maps should be handled.
366 build_cube_select(builder
, &selcoords
, &derivs_arg
[axis
* 3],
367 deriv_st
, &deriv_ma
);
369 deriv_ma
= LLVMBuildFMul(builder
, deriv_ma
, invma
, "");
371 for (int i
= 0; i
< 2; ++i
)
372 derivs
[axis
* 2 + i
] =
373 LLVMBuildFSub(builder
,
374 LLVMBuildFMul(builder
, deriv_st
[i
], invma
, ""),
375 LLVMBuildFMul(builder
, deriv_ma
, coords
[i
], ""), "");
378 memcpy(derivs_arg
, derivs
, sizeof(derivs
));
381 /* Shift the texture coordinate. This must be applied after the
382 * derivative calculation.
384 for (int i
= 0; i
< 2; ++i
)
385 coords
[i
] = LLVMBuildFAdd(builder
, coords
[i
], LLVMConstReal(ctx
->f32
, 1.5), "");
388 /* for cube arrays coord.z = coord.w(array_index) * 8 + face */
389 /* coords_arg.w component - array_index for cube arrays */
390 LLVMValueRef tmp
= LLVMBuildFMul(ctx
->builder
, coords_arg
[3], LLVMConstReal(ctx
->f32
, 8.0), "");
391 coords
[2] = LLVMBuildFAdd(ctx
->builder
, tmp
, coords
[2], "");
394 memcpy(coords_arg
, coords
, sizeof(coords
));
399 ac_build_fs_interp(struct ac_llvm_context
*ctx
,
400 LLVMValueRef llvm_chan
,
401 LLVMValueRef attr_number
,
406 LLVMValueRef args
[5];
409 if (HAVE_LLVM
< 0x0400) {
411 ij
[0] = LLVMBuildBitCast(ctx
->builder
, i
, ctx
->i32
, "");
412 ij
[1] = LLVMBuildBitCast(ctx
->builder
, j
, ctx
->i32
, "");
415 args
[1] = attr_number
;
417 args
[3] = ac_build_gather_values(ctx
, ij
, 2);
418 return ac_build_intrinsic(ctx
, "llvm.SI.fs.interp",
420 AC_FUNC_ATTR_READNONE
);
425 args
[2] = attr_number
;
428 p1
= ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p1",
429 ctx
->f32
, args
, 4, AC_FUNC_ATTR_READNONE
);
434 args
[3] = attr_number
;
437 return ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p2",
438 ctx
->f32
, args
, 5, AC_FUNC_ATTR_READNONE
);
442 ac_build_fs_interp_mov(struct ac_llvm_context
*ctx
,
443 LLVMValueRef parameter
,
444 LLVMValueRef llvm_chan
,
445 LLVMValueRef attr_number
,
448 LLVMValueRef args
[4];
449 if (HAVE_LLVM
< 0x0400) {
451 args
[1] = attr_number
;
454 return ac_build_intrinsic(ctx
,
455 "llvm.SI.fs.constant",
457 AC_FUNC_ATTR_READNONE
);
462 args
[2] = attr_number
;
465 return ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.mov",
466 ctx
->f32
, args
, 4, AC_FUNC_ATTR_READNONE
);
470 ac_build_gep0(struct ac_llvm_context
*ctx
,
471 LLVMValueRef base_ptr
,
474 LLVMValueRef indices
[2] = {
475 LLVMConstInt(ctx
->i32
, 0, 0),
478 return LLVMBuildGEP(ctx
->builder
, base_ptr
,
483 ac_build_indexed_store(struct ac_llvm_context
*ctx
,
484 LLVMValueRef base_ptr
, LLVMValueRef index
,
487 LLVMBuildStore(ctx
->builder
, value
,
488 ac_build_gep0(ctx
, base_ptr
, index
));
492 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad.
493 * It's equivalent to doing a load from &base_ptr[index].
495 * \param base_ptr Where the array starts.
496 * \param index The element index into the array.
497 * \param uniform Whether the base_ptr and index can be assumed to be
498 * dynamically uniform
501 ac_build_indexed_load(struct ac_llvm_context
*ctx
,
502 LLVMValueRef base_ptr
, LLVMValueRef index
,
505 LLVMValueRef pointer
;
507 pointer
= ac_build_gep0(ctx
, base_ptr
, index
);
509 LLVMSetMetadata(pointer
, ctx
->uniform_md_kind
, ctx
->empty_md
);
510 return LLVMBuildLoad(ctx
->builder
, pointer
, "");
514 * Do a load from &base_ptr[index], but also add a flag that it's loading
515 * a constant from a dynamically uniform index.
518 ac_build_indexed_load_const(struct ac_llvm_context
*ctx
,
519 LLVMValueRef base_ptr
, LLVMValueRef index
)
521 LLVMValueRef result
= ac_build_indexed_load(ctx
, base_ptr
, index
, true);
522 LLVMSetMetadata(result
, ctx
->invariant_load_md_kind
, ctx
->empty_md
);
526 /* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
527 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
528 * or v4i32 (num_channels=3,4).
531 ac_build_buffer_store_dword(struct ac_llvm_context
*ctx
,
534 unsigned num_channels
,
535 LLVMValueRef voffset
,
536 LLVMValueRef soffset
,
537 unsigned inst_offset
,
540 bool writeonly_memory
,
543 /* TODO: Fix stores with ADD_TID and remove the "has_add_tid" flag. */
545 /* Split 3 channel stores, becase LLVM doesn't support 3-channel
547 if (num_channels
== 3) {
548 LLVMValueRef v
[3], v01
;
550 for (int i
= 0; i
< 3; i
++) {
551 v
[i
] = LLVMBuildExtractElement(ctx
->builder
, vdata
,
552 LLVMConstInt(ctx
->i32
, i
, 0), "");
554 v01
= ac_build_gather_values(ctx
, v
, 2);
556 ac_build_buffer_store_dword(ctx
, rsrc
, v01
, 2, voffset
,
557 soffset
, inst_offset
, glc
, slc
,
558 writeonly_memory
, has_add_tid
);
559 ac_build_buffer_store_dword(ctx
, rsrc
, v
[2], 1, voffset
,
560 soffset
, inst_offset
+ 8,
562 writeonly_memory
, has_add_tid
);
566 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
567 static const char *types
[] = {"f32", "v2f32", "v4f32"};
569 LLVMValueRef offset
= soffset
;
572 offset
= LLVMBuildAdd(ctx
->builder
, offset
,
573 LLVMConstInt(ctx
->i32
, inst_offset
, 0), "");
575 offset
= LLVMBuildAdd(ctx
->builder
, offset
, voffset
, "");
577 LLVMValueRef args
[] = {
578 bitcast_to_float(ctx
, vdata
),
579 LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, ""),
580 LLVMConstInt(ctx
->i32
, 0, 0),
582 LLVMConstInt(ctx
->i1
, glc
, 0),
583 LLVMConstInt(ctx
->i1
, slc
, 0),
586 snprintf(name
, sizeof(name
), "llvm.amdgcn.buffer.store.%s",
589 ac_build_intrinsic(ctx
, name
, ctx
->voidt
,
590 args
, ARRAY_SIZE(args
),
592 AC_FUNC_ATTR_INACCESSIBLE_MEM_ONLY
:
593 AC_FUNC_ATTR_WRITEONLY
);
597 static unsigned dfmt
[] = {
598 V_008F0C_BUF_DATA_FORMAT_32
,
599 V_008F0C_BUF_DATA_FORMAT_32_32
,
600 V_008F0C_BUF_DATA_FORMAT_32_32_32
,
601 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
603 assert(num_channels
>= 1 && num_channels
<= 4);
605 LLVMValueRef args
[] = {
608 LLVMConstInt(ctx
->i32
, num_channels
, 0),
609 voffset
? voffset
: LLVMGetUndef(ctx
->i32
),
611 LLVMConstInt(ctx
->i32
, inst_offset
, 0),
612 LLVMConstInt(ctx
->i32
, dfmt
[num_channels
- 1], 0),
613 LLVMConstInt(ctx
->i32
, V_008F0C_BUF_NUM_FORMAT_UINT
, 0),
614 LLVMConstInt(ctx
->i32
, voffset
!= NULL
, 0),
615 LLVMConstInt(ctx
->i32
, 0, 0), /* idxen */
616 LLVMConstInt(ctx
->i32
, glc
, 0),
617 LLVMConstInt(ctx
->i32
, slc
, 0),
618 LLVMConstInt(ctx
->i32
, 0, 0), /* tfe*/
621 /* The instruction offset field has 12 bits */
622 assert(voffset
|| inst_offset
< (1 << 12));
624 /* The intrinsic is overloaded, we need to add a type suffix for overloading to work. */
625 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
626 const char *types
[] = {"i32", "v2i32", "v4i32"};
628 snprintf(name
, sizeof(name
), "llvm.SI.tbuffer.store.%s", types
[func
]);
630 ac_build_intrinsic(ctx
, name
, ctx
->voidt
,
631 args
, ARRAY_SIZE(args
),
632 AC_FUNC_ATTR_LEGACY
);
636 ac_build_buffer_load(struct ac_llvm_context
*ctx
,
640 LLVMValueRef voffset
,
641 LLVMValueRef soffset
,
642 unsigned inst_offset
,
648 LLVMValueRef offset
= LLVMConstInt(ctx
->i32
, inst_offset
, 0);
650 offset
= LLVMBuildAdd(ctx
->builder
, offset
, voffset
, "");
652 offset
= LLVMBuildAdd(ctx
->builder
, offset
, soffset
, "");
654 /* TODO: VI and later generations can use SMEM with GLC=1.*/
655 if (allow_smem
&& !glc
&& !slc
) {
656 assert(vindex
== NULL
);
658 LLVMValueRef result
[4];
660 for (int i
= 0; i
< num_channels
; i
++) {
662 offset
= LLVMBuildAdd(ctx
->builder
, offset
,
663 LLVMConstInt(ctx
->i32
, 4, 0), "");
665 LLVMValueRef args
[2] = {rsrc
, offset
};
666 result
[i
] = ac_build_intrinsic(ctx
, "llvm.SI.load.const.v4i32",
668 AC_FUNC_ATTR_READNONE
|
669 AC_FUNC_ATTR_LEGACY
);
671 if (num_channels
== 1)
674 if (num_channels
== 3)
675 result
[num_channels
++] = LLVMGetUndef(ctx
->f32
);
676 return ac_build_gather_values(ctx
, result
, num_channels
);
679 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
681 LLVMValueRef args
[] = {
682 LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, ""),
683 vindex
? vindex
: LLVMConstInt(ctx
->i32
, 0, 0),
685 LLVMConstInt(ctx
->i1
, glc
, 0),
686 LLVMConstInt(ctx
->i1
, slc
, 0)
689 LLVMTypeRef types
[] = {ctx
->f32
, LLVMVectorType(ctx
->f32
, 2),
691 const char *type_names
[] = {"f32", "v2f32", "v4f32"};
694 snprintf(name
, sizeof(name
), "llvm.amdgcn.buffer.load.%s",
697 return ac_build_intrinsic(ctx
, name
, types
[func
], args
,
699 /* READNONE means writes can't affect it, while
700 * READONLY means that writes can affect it. */
701 can_speculate
&& HAVE_LLVM
>= 0x0400 ?
702 AC_FUNC_ATTR_READNONE
:
703 AC_FUNC_ATTR_READONLY
);
706 LLVMValueRef
ac_build_buffer_load_format(struct ac_llvm_context
*ctx
,
709 LLVMValueRef voffset
,
712 LLVMValueRef args
[] = {
713 LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, ""),
716 LLVMConstInt(ctx
->i1
, 0, 0), /* glc */
717 LLVMConstInt(ctx
->i1
, 0, 0), /* slc */
720 return ac_build_intrinsic(ctx
,
721 "llvm.amdgcn.buffer.load.format.v4f32",
722 ctx
->v4f32
, args
, ARRAY_SIZE(args
),
723 /* READNONE means writes can't affect it, while
724 * READONLY means that writes can affect it. */
725 can_speculate
&& HAVE_LLVM
>= 0x0400 ?
726 AC_FUNC_ATTR_READNONE
:
727 AC_FUNC_ATTR_READONLY
);
731 * Set range metadata on an instruction. This can only be used on load and
732 * call instructions. If you know an instruction can only produce the values
733 * 0, 1, 2, you would do set_range_metadata(value, 0, 3);
734 * \p lo is the minimum value inclusive.
735 * \p hi is the maximum value exclusive.
737 static void set_range_metadata(struct ac_llvm_context
*ctx
,
738 LLVMValueRef value
, unsigned lo
, unsigned hi
)
740 LLVMValueRef range_md
, md_args
[2];
741 LLVMTypeRef type
= LLVMTypeOf(value
);
742 LLVMContextRef context
= LLVMGetTypeContext(type
);
744 md_args
[0] = LLVMConstInt(type
, lo
, false);
745 md_args
[1] = LLVMConstInt(type
, hi
, false);
746 range_md
= LLVMMDNodeInContext(context
, md_args
, 2);
747 LLVMSetMetadata(value
, ctx
->range_md_kind
, range_md
);
751 ac_get_thread_id(struct ac_llvm_context
*ctx
)
755 LLVMValueRef tid_args
[2];
756 tid_args
[0] = LLVMConstInt(ctx
->i32
, 0xffffffff, false);
757 tid_args
[1] = LLVMConstInt(ctx
->i32
, 0, false);
758 tid_args
[1] = ac_build_intrinsic(ctx
,
759 "llvm.amdgcn.mbcnt.lo", ctx
->i32
,
760 tid_args
, 2, AC_FUNC_ATTR_READNONE
);
762 tid
= ac_build_intrinsic(ctx
, "llvm.amdgcn.mbcnt.hi",
764 2, AC_FUNC_ATTR_READNONE
);
765 set_range_metadata(ctx
, tid
, 0, 64);
770 * SI implements derivatives using the local data store (LDS)
771 * All writes to the LDS happen in all executing threads at
772 * the same time. TID is the Thread ID for the current
773 * thread and is a value between 0 and 63, representing
774 * the thread's position in the wavefront.
776 * For the pixel shader threads are grouped into quads of four pixels.
777 * The TIDs of the pixels of a quad are:
785 * So, masking the TID with 0xfffffffc yields the TID of the top left pixel
786 * of the quad, masking with 0xfffffffd yields the TID of the top pixel of
787 * the current pixel's column, and masking with 0xfffffffe yields the TID
788 * of the left pixel of the current pixel's row.
790 * Adding 1 yields the TID of the pixel to the right of the left pixel, and
791 * adding 2 yields the TID of the pixel below the top pixel.
794 ac_build_ddxy(struct ac_llvm_context
*ctx
,
795 bool has_ds_bpermute
,
801 LLVMValueRef thread_id
, tl
, trbl
, tl_tid
, trbl_tid
, args
[2];
804 thread_id
= ac_get_thread_id(ctx
);
806 tl_tid
= LLVMBuildAnd(ctx
->builder
, thread_id
,
807 LLVMConstInt(ctx
->i32
, mask
, false), "");
809 trbl_tid
= LLVMBuildAdd(ctx
->builder
, tl_tid
,
810 LLVMConstInt(ctx
->i32
, idx
, false), "");
812 if (has_ds_bpermute
) {
813 args
[0] = LLVMBuildMul(ctx
->builder
, tl_tid
,
814 LLVMConstInt(ctx
->i32
, 4, false), "");
816 tl
= ac_build_intrinsic(ctx
,
817 "llvm.amdgcn.ds.bpermute", ctx
->i32
,
819 AC_FUNC_ATTR_READNONE
|
820 AC_FUNC_ATTR_CONVERGENT
);
822 args
[0] = LLVMBuildMul(ctx
->builder
, trbl_tid
,
823 LLVMConstInt(ctx
->i32
, 4, false), "");
824 trbl
= ac_build_intrinsic(ctx
,
825 "llvm.amdgcn.ds.bpermute", ctx
->i32
,
827 AC_FUNC_ATTR_READNONE
|
828 AC_FUNC_ATTR_CONVERGENT
);
830 LLVMValueRef store_ptr
, load_ptr0
, load_ptr1
;
832 store_ptr
= ac_build_gep0(ctx
, lds
, thread_id
);
833 load_ptr0
= ac_build_gep0(ctx
, lds
, tl_tid
);
834 load_ptr1
= ac_build_gep0(ctx
, lds
, trbl_tid
);
836 LLVMBuildStore(ctx
->builder
, val
, store_ptr
);
837 tl
= LLVMBuildLoad(ctx
->builder
, load_ptr0
, "");
838 trbl
= LLVMBuildLoad(ctx
->builder
, load_ptr1
, "");
841 tl
= LLVMBuildBitCast(ctx
->builder
, tl
, ctx
->f32
, "");
842 trbl
= LLVMBuildBitCast(ctx
->builder
, trbl
, ctx
->f32
, "");
843 result
= LLVMBuildFSub(ctx
->builder
, trbl
, tl
, "");
848 ac_build_sendmsg(struct ac_llvm_context
*ctx
,
850 LLVMValueRef wave_id
)
852 LLVMValueRef args
[2];
853 const char *intr_name
= (HAVE_LLVM
< 0x0400) ? "llvm.SI.sendmsg" : "llvm.amdgcn.s.sendmsg";
854 args
[0] = LLVMConstInt(ctx
->i32
, msg
, false);
856 ac_build_intrinsic(ctx
, intr_name
, ctx
->voidt
, args
, 2, 0);
860 ac_build_imsb(struct ac_llvm_context
*ctx
,
862 LLVMTypeRef dst_type
)
864 const char *intr_name
= (HAVE_LLVM
< 0x0400) ? "llvm.AMDGPU.flbit.i32" :
865 "llvm.amdgcn.sffbh.i32";
866 LLVMValueRef msb
= ac_build_intrinsic(ctx
, intr_name
,
868 AC_FUNC_ATTR_READNONE
);
870 /* The HW returns the last bit index from MSB, but NIR/TGSI wants
871 * the index from LSB. Invert it by doing "31 - msb". */
872 msb
= LLVMBuildSub(ctx
->builder
, LLVMConstInt(ctx
->i32
, 31, false),
875 LLVMValueRef all_ones
= LLVMConstInt(ctx
->i32
, -1, true);
876 LLVMValueRef cond
= LLVMBuildOr(ctx
->builder
,
877 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
878 arg
, LLVMConstInt(ctx
->i32
, 0, 0), ""),
879 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
880 arg
, all_ones
, ""), "");
882 return LLVMBuildSelect(ctx
->builder
, cond
, all_ones
, msb
, "");
886 ac_build_umsb(struct ac_llvm_context
*ctx
,
888 LLVMTypeRef dst_type
)
890 LLVMValueRef args
[2] = {
892 LLVMConstInt(ctx
->i1
, 1, 0),
894 LLVMValueRef msb
= ac_build_intrinsic(ctx
, "llvm.ctlz.i32",
895 dst_type
, args
, ARRAY_SIZE(args
),
896 AC_FUNC_ATTR_READNONE
);
898 /* The HW returns the last bit index from MSB, but TGSI/NIR wants
899 * the index from LSB. Invert it by doing "31 - msb". */
900 msb
= LLVMBuildSub(ctx
->builder
, LLVMConstInt(ctx
->i32
, 31, false),
904 return LLVMBuildSelect(ctx
->builder
,
905 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, arg
,
906 LLVMConstInt(ctx
->i32
, 0, 0), ""),
907 LLVMConstInt(ctx
->i32
, -1, true), msb
, "");
910 LLVMValueRef
ac_build_clamp(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
912 if (HAVE_LLVM
>= 0x0500) {
913 LLVMValueRef max
[2] = {
915 LLVMConstReal(ctx
->f32
, 0),
917 LLVMValueRef min
[2] = {
918 LLVMConstReal(ctx
->f32
, 1),
921 min
[1] = ac_build_intrinsic(ctx
, "llvm.maxnum.f32",
923 AC_FUNC_ATTR_READNONE
);
924 return ac_build_intrinsic(ctx
, "llvm.minnum.f32",
926 AC_FUNC_ATTR_READNONE
);
929 LLVMValueRef args
[3] = {
931 LLVMConstReal(ctx
->f32
, 0),
932 LLVMConstReal(ctx
->f32
, 1),
935 return ac_build_intrinsic(ctx
, "llvm.AMDGPU.clamp.", ctx
->f32
, args
, 3,
936 AC_FUNC_ATTR_READNONE
|
937 AC_FUNC_ATTR_LEGACY
);
940 void ac_build_export(struct ac_llvm_context
*ctx
, struct ac_export_args
*a
)
942 LLVMValueRef args
[9];
944 if (HAVE_LLVM
>= 0x0500) {
945 args
[0] = LLVMConstInt(ctx
->i32
, a
->target
, 0);
946 args
[1] = LLVMConstInt(ctx
->i32
, a
->enabled_channels
, 0);
949 LLVMTypeRef i16
= LLVMInt16TypeInContext(ctx
->context
);
950 LLVMTypeRef v2i16
= LLVMVectorType(i16
, 2);
952 args
[2] = LLVMBuildBitCast(ctx
->builder
, a
->out
[0],
954 args
[3] = LLVMBuildBitCast(ctx
->builder
, a
->out
[1],
956 args
[4] = LLVMConstInt(ctx
->i1
, a
->done
, 0);
957 args
[5] = LLVMConstInt(ctx
->i1
, a
->valid_mask
, 0);
959 ac_build_intrinsic(ctx
, "llvm.amdgcn.exp.compr.v2i16",
960 ctx
->voidt
, args
, 6, 0);
966 args
[6] = LLVMConstInt(ctx
->i1
, a
->done
, 0);
967 args
[7] = LLVMConstInt(ctx
->i1
, a
->valid_mask
, 0);
969 ac_build_intrinsic(ctx
, "llvm.amdgcn.exp.f32",
970 ctx
->voidt
, args
, 8, 0);
975 args
[0] = LLVMConstInt(ctx
->i32
, a
->enabled_channels
, 0);
976 args
[1] = LLVMConstInt(ctx
->i32
, a
->valid_mask
, 0);
977 args
[2] = LLVMConstInt(ctx
->i32
, a
->done
, 0);
978 args
[3] = LLVMConstInt(ctx
->i32
, a
->target
, 0);
979 args
[4] = LLVMConstInt(ctx
->i32
, a
->compr
, 0);
980 memcpy(args
+ 5, a
->out
, sizeof(a
->out
[0]) * 4);
982 ac_build_intrinsic(ctx
, "llvm.SI.export", ctx
->voidt
, args
, 9,
983 AC_FUNC_ATTR_LEGACY
);
986 LLVMValueRef
ac_build_image_opcode(struct ac_llvm_context
*ctx
,
987 struct ac_image_args
*a
)
989 LLVMTypeRef dst_type
;
990 LLVMValueRef args
[11];
991 unsigned num_args
= 0;
993 char intr_name
[128], type
[64];
995 if (HAVE_LLVM
>= 0x0400) {
996 bool sample
= a
->opcode
== ac_image_sample
||
997 a
->opcode
== ac_image_gather4
||
998 a
->opcode
== ac_image_get_lod
;
1001 args
[num_args
++] = bitcast_to_float(ctx
, a
->addr
);
1003 args
[num_args
++] = a
->addr
;
1005 args
[num_args
++] = a
->resource
;
1007 args
[num_args
++] = a
->sampler
;
1008 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->dmask
, 0);
1010 args
[num_args
++] = LLVMConstInt(ctx
->i1
, a
->unorm
, 0);
1011 args
[num_args
++] = LLVMConstInt(ctx
->i1
, 0, 0); /* glc */
1012 args
[num_args
++] = LLVMConstInt(ctx
->i1
, 0, 0); /* slc */
1013 args
[num_args
++] = LLVMConstInt(ctx
->i1
, 0, 0); /* lwe */
1014 args
[num_args
++] = LLVMConstInt(ctx
->i1
, a
->da
, 0);
1016 switch (a
->opcode
) {
1017 case ac_image_sample
:
1018 name
= "llvm.amdgcn.image.sample";
1020 case ac_image_gather4
:
1021 name
= "llvm.amdgcn.image.gather4";
1024 name
= "llvm.amdgcn.image.load";
1026 case ac_image_load_mip
:
1027 name
= "llvm.amdgcn.image.load.mip";
1029 case ac_image_get_lod
:
1030 name
= "llvm.amdgcn.image.getlod";
1032 case ac_image_get_resinfo
:
1033 name
= "llvm.amdgcn.image.getresinfo";
1036 unreachable("invalid image opcode");
1039 ac_build_type_name_for_intr(LLVMTypeOf(args
[0]), type
,
1042 snprintf(intr_name
, sizeof(intr_name
), "%s%s%s%s.v4f32.%s.v8i32",
1044 a
->compare
? ".c" : "",
1048 a
->level_zero
? ".lz" : "",
1049 a
->offset
? ".o" : "",
1052 LLVMValueRef result
=
1053 ac_build_intrinsic(ctx
, intr_name
,
1054 ctx
->v4f32
, args
, num_args
,
1055 AC_FUNC_ATTR_READNONE
);
1057 result
= LLVMBuildBitCast(ctx
->builder
, result
,
1063 args
[num_args
++] = a
->addr
;
1064 args
[num_args
++] = a
->resource
;
1066 if (a
->opcode
== ac_image_load
||
1067 a
->opcode
== ac_image_load_mip
||
1068 a
->opcode
== ac_image_get_resinfo
) {
1069 dst_type
= ctx
->v4i32
;
1071 dst_type
= ctx
->v4f32
;
1072 args
[num_args
++] = a
->sampler
;
1075 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->dmask
, 0);
1076 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->unorm
, 0);
1077 args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* r128 */
1078 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->da
, 0);
1079 args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* glc */
1080 args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* slc */
1081 args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* tfe */
1082 args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* lwe */
1084 switch (a
->opcode
) {
1085 case ac_image_sample
:
1086 name
= "llvm.SI.image.sample";
1088 case ac_image_gather4
:
1089 name
= "llvm.SI.gather4";
1092 name
= "llvm.SI.image.load";
1094 case ac_image_load_mip
:
1095 name
= "llvm.SI.image.load.mip";
1097 case ac_image_get_lod
:
1098 name
= "llvm.SI.getlod";
1100 case ac_image_get_resinfo
:
1101 name
= "llvm.SI.getresinfo";
1105 ac_build_type_name_for_intr(LLVMTypeOf(a
->addr
), type
, sizeof(type
));
1106 snprintf(intr_name
, sizeof(intr_name
), "%s%s%s%s.%s",
1108 a
->compare
? ".c" : "",
1112 a
->level_zero
? ".lz" : "",
1113 a
->offset
? ".o" : "",
1116 return ac_build_intrinsic(ctx
, intr_name
,
1117 dst_type
, args
, num_args
,
1118 AC_FUNC_ATTR_READNONE
|
1119 AC_FUNC_ATTR_LEGACY
);
1122 LLVMValueRef
ac_build_cvt_pkrtz_f16(struct ac_llvm_context
*ctx
,
1123 LLVMValueRef args
[2])
1125 if (HAVE_LLVM
>= 0x0500) {
1127 LLVMVectorType(LLVMHalfTypeInContext(ctx
->context
), 2);
1129 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pkrtz",
1131 AC_FUNC_ATTR_READNONE
);
1132 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
1135 return ac_build_intrinsic(ctx
, "llvm.SI.packf16", ctx
->i32
, args
, 2,
1136 AC_FUNC_ATTR_READNONE
|
1137 AC_FUNC_ATTR_LEGACY
);
1141 * KILL, AKA discard in GLSL.
1143 * \param value kill if value < 0.0 or value == NULL.
1145 void ac_build_kill(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
1148 ac_build_intrinsic(ctx
, "llvm.AMDGPU.kill", ctx
->voidt
,
1149 &value
, 1, AC_FUNC_ATTR_LEGACY
);
1151 ac_build_intrinsic(ctx
, "llvm.AMDGPU.kilp", ctx
->voidt
,
1152 NULL
, 0, AC_FUNC_ATTR_LEGACY
);
1156 LLVMValueRef
ac_build_bfe(struct ac_llvm_context
*ctx
, LLVMValueRef input
,
1157 LLVMValueRef offset
, LLVMValueRef width
,
1160 LLVMValueRef args
[] = {
1166 if (HAVE_LLVM
>= 0x0500) {
1167 return ac_build_intrinsic(ctx
,
1168 is_signed
? "llvm.amdgcn.sbfe.i32" :
1169 "llvm.amdgcn.ubfe.i32",
1171 AC_FUNC_ATTR_READNONE
);
1174 return ac_build_intrinsic(ctx
,
1175 is_signed
? "llvm.AMDGPU.bfe.i32" :
1176 "llvm.AMDGPU.bfe.u32",
1178 AC_FUNC_ATTR_READNONE
|
1179 AC_FUNC_ATTR_LEGACY
);
1182 void ac_get_image_intr_name(const char *base_name
,
1183 LLVMTypeRef data_type
,
1184 LLVMTypeRef coords_type
,
1185 LLVMTypeRef rsrc_type
,
1186 char *out_name
, unsigned out_len
)
1188 char coords_type_name
[8];
1190 ac_build_type_name_for_intr(coords_type
, coords_type_name
,
1191 sizeof(coords_type_name
));
1193 if (HAVE_LLVM
<= 0x0309) {
1194 snprintf(out_name
, out_len
, "%s.%s", base_name
, coords_type_name
);
1196 char data_type_name
[8];
1197 char rsrc_type_name
[8];
1199 ac_build_type_name_for_intr(data_type
, data_type_name
,
1200 sizeof(data_type_name
));
1201 ac_build_type_name_for_intr(rsrc_type
, rsrc_type_name
,
1202 sizeof(rsrc_type_name
));
1203 snprintf(out_name
, out_len
, "%s.%s.%s.%s", base_name
,
1204 data_type_name
, coords_type_name
, rsrc_type_name
);
1208 #define AC_EXP_TARGET (HAVE_LLVM >= 0x0500 ? 0 : 3)
1209 #define AC_EXP_OUT0 (HAVE_LLVM >= 0x0500 ? 2 : 5)
1217 struct ac_vs_exp_chan
1221 enum ac_ir_type type
;
1224 struct ac_vs_exp_inst
{
1227 struct ac_vs_exp_chan chan
[4];
1230 struct ac_vs_exports
{
1232 struct ac_vs_exp_inst exp
[VARYING_SLOT_MAX
];
1235 /* Return true if the PARAM export has been eliminated. */
1236 static bool ac_eliminate_const_output(uint8_t *vs_output_param_offset
,
1237 uint32_t num_outputs
,
1238 struct ac_vs_exp_inst
*exp
)
1240 unsigned i
, default_val
; /* SPI_PS_INPUT_CNTL_i.DEFAULT_VAL */
1241 bool is_zero
[4] = {}, is_one
[4] = {};
1243 for (i
= 0; i
< 4; i
++) {
1244 /* It's a constant expression. Undef outputs are eliminated too. */
1245 if (exp
->chan
[i
].type
== AC_IR_UNDEF
) {
1248 } else if (exp
->chan
[i
].type
== AC_IR_CONST
) {
1249 if (exp
->chan
[i
].const_float
== 0)
1251 else if (exp
->chan
[i
].const_float
== 1)
1254 return false; /* other constant */
1259 /* Only certain combinations of 0 and 1 can be eliminated. */
1260 if (is_zero
[0] && is_zero
[1] && is_zero
[2])
1261 default_val
= is_zero
[3] ? 0 : 1;
1262 else if (is_one
[0] && is_one
[1] && is_one
[2])
1263 default_val
= is_zero
[3] ? 2 : 3;
1267 /* The PARAM export can be represented as DEFAULT_VAL. Kill it. */
1268 LLVMInstructionEraseFromParent(exp
->inst
);
1270 /* Change OFFSET to DEFAULT_VAL. */
1271 for (i
= 0; i
< num_outputs
; i
++) {
1272 if (vs_output_param_offset
[i
] == exp
->offset
) {
1273 vs_output_param_offset
[i
] =
1274 AC_EXP_PARAM_DEFAULT_VAL_0000
+ default_val
;
1281 static bool ac_eliminate_duplicated_output(uint8_t *vs_output_param_offset
,
1282 uint32_t num_outputs
,
1283 struct ac_vs_exports
*processed
,
1284 struct ac_vs_exp_inst
*exp
)
1286 unsigned p
, copy_back_channels
= 0;
1288 /* See if the output is already in the list of processed outputs.
1289 * The LLVMValueRef comparison relies on SSA.
1291 for (p
= 0; p
< processed
->num
; p
++) {
1292 bool different
= false;
1294 for (unsigned j
= 0; j
< 4; j
++) {
1295 struct ac_vs_exp_chan
*c1
= &processed
->exp
[p
].chan
[j
];
1296 struct ac_vs_exp_chan
*c2
= &exp
->chan
[j
];
1298 /* Treat undef as a match. */
1299 if (c2
->type
== AC_IR_UNDEF
)
1302 /* If c1 is undef but c2 isn't, we can copy c2 to c1
1303 * and consider the instruction duplicated.
1305 if (c1
->type
== AC_IR_UNDEF
) {
1306 copy_back_channels
|= 1 << j
;
1310 /* Test whether the channels are not equal. */
1311 if (c1
->type
!= c2
->type
||
1312 (c1
->type
== AC_IR_CONST
&&
1313 c1
->const_float
!= c2
->const_float
) ||
1314 (c1
->type
== AC_IR_VALUE
&&
1315 c1
->value
!= c2
->value
)) {
1323 copy_back_channels
= 0;
1325 if (p
== processed
->num
)
1328 /* If a match was found, but the matching export has undef where the new
1329 * one has a normal value, copy the normal value to the undef channel.
1331 struct ac_vs_exp_inst
*match
= &processed
->exp
[p
];
1333 while (copy_back_channels
) {
1334 unsigned chan
= u_bit_scan(©_back_channels
);
1336 assert(match
->chan
[chan
].type
== AC_IR_UNDEF
);
1337 LLVMSetOperand(match
->inst
, AC_EXP_OUT0
+ chan
,
1338 exp
->chan
[chan
].value
);
1339 match
->chan
[chan
] = exp
->chan
[chan
];
1342 /* The PARAM export is duplicated. Kill it. */
1343 LLVMInstructionEraseFromParent(exp
->inst
);
1345 /* Change OFFSET to the matching export. */
1346 for (unsigned i
= 0; i
< num_outputs
; i
++) {
1347 if (vs_output_param_offset
[i
] == exp
->offset
) {
1348 vs_output_param_offset
[i
] = match
->offset
;
1355 void ac_optimize_vs_outputs(struct ac_llvm_context
*ctx
,
1356 LLVMValueRef main_fn
,
1357 uint8_t *vs_output_param_offset
,
1358 uint32_t num_outputs
,
1359 uint8_t *num_param_exports
)
1361 LLVMBasicBlockRef bb
;
1362 bool removed_any
= false;
1363 struct ac_vs_exports exports
;
1367 /* Process all LLVM instructions. */
1368 bb
= LLVMGetFirstBasicBlock(main_fn
);
1370 LLVMValueRef inst
= LLVMGetFirstInstruction(bb
);
1373 LLVMValueRef cur
= inst
;
1374 inst
= LLVMGetNextInstruction(inst
);
1375 struct ac_vs_exp_inst exp
;
1377 if (LLVMGetInstructionOpcode(cur
) != LLVMCall
)
1380 LLVMValueRef callee
= ac_llvm_get_called_value(cur
);
1382 if (!ac_llvm_is_function(callee
))
1385 const char *name
= LLVMGetValueName(callee
);
1386 unsigned num_args
= LLVMCountParams(callee
);
1388 /* Check if this is an export instruction. */
1389 if ((num_args
!= 9 && num_args
!= 8) ||
1390 (strcmp(name
, "llvm.SI.export") &&
1391 strcmp(name
, "llvm.amdgcn.exp.f32")))
1394 LLVMValueRef arg
= LLVMGetOperand(cur
, AC_EXP_TARGET
);
1395 unsigned target
= LLVMConstIntGetZExtValue(arg
);
1397 if (target
< V_008DFC_SQ_EXP_PARAM
)
1400 target
-= V_008DFC_SQ_EXP_PARAM
;
1402 /* Parse the instruction. */
1403 memset(&exp
, 0, sizeof(exp
));
1404 exp
.offset
= target
;
1407 for (unsigned i
= 0; i
< 4; i
++) {
1408 LLVMValueRef v
= LLVMGetOperand(cur
, AC_EXP_OUT0
+ i
);
1410 exp
.chan
[i
].value
= v
;
1412 if (LLVMIsUndef(v
)) {
1413 exp
.chan
[i
].type
= AC_IR_UNDEF
;
1414 } else if (LLVMIsAConstantFP(v
)) {
1415 LLVMBool loses_info
;
1416 exp
.chan
[i
].type
= AC_IR_CONST
;
1417 exp
.chan
[i
].const_float
=
1418 LLVMConstRealGetDouble(v
, &loses_info
);
1420 exp
.chan
[i
].type
= AC_IR_VALUE
;
1424 /* Eliminate constant and duplicated PARAM exports. */
1425 if (ac_eliminate_const_output(vs_output_param_offset
,
1426 num_outputs
, &exp
) ||
1427 ac_eliminate_duplicated_output(vs_output_param_offset
,
1428 num_outputs
, &exports
,
1432 exports
.exp
[exports
.num
++] = exp
;
1435 bb
= LLVMGetNextBasicBlock(bb
);
1438 /* Remove holes in export memory due to removed PARAM exports.
1439 * This is done by renumbering all PARAM exports.
1442 uint8_t old_offset
[VARYING_SLOT_MAX
];
1445 /* Make a copy of the offsets. We need the old version while
1446 * we are modifying some of them. */
1447 memcpy(old_offset
, vs_output_param_offset
,
1448 sizeof(old_offset
));
1450 for (i
= 0; i
< exports
.num
; i
++) {
1451 unsigned offset
= exports
.exp
[i
].offset
;
1453 /* Update vs_output_param_offset. Multiple outputs can
1454 * have the same offset.
1456 for (out
= 0; out
< num_outputs
; out
++) {
1457 if (old_offset
[out
] == offset
)
1458 vs_output_param_offset
[out
] = i
;
1461 /* Change the PARAM offset in the instruction. */
1462 LLVMSetOperand(exports
.exp
[i
].inst
, AC_EXP_TARGET
,
1463 LLVMConstInt(ctx
->i32
,
1464 V_008DFC_SQ_EXP_PARAM
+ i
, 0));
1466 *num_param_exports
= exports
.num
;