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
->i32
= LLVMIntTypeInContext(ctx
->context
, 32);
60 ctx
->f32
= LLVMFloatTypeInContext(ctx
->context
);
61 ctx
->v4i32
= LLVMVectorType(ctx
->i32
, 4);
62 ctx
->v4f32
= LLVMVectorType(ctx
->f32
, 4);
63 ctx
->v16i8
= LLVMVectorType(ctx
->i8
, 16);
65 ctx
->range_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
68 ctx
->invariant_load_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
69 "invariant.load", 14);
71 ctx
->fpmath_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
, "fpmath", 6);
73 args
[0] = LLVMConstReal(ctx
->f32
, 2.5);
74 ctx
->fpmath_md_2p5_ulp
= LLVMMDNodeInContext(ctx
->context
, args
, 1);
76 ctx
->uniform_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
77 "amdgpu.uniform", 14);
79 ctx
->empty_md
= LLVMMDNodeInContext(ctx
->context
, NULL
, 0);
83 ac_build_intrinsic(struct ac_llvm_context
*ctx
, const char *name
,
84 LLVMTypeRef return_type
, LLVMValueRef
*params
,
85 unsigned param_count
, unsigned attrib_mask
)
87 LLVMValueRef function
, call
;
88 bool set_callsite_attrs
= HAVE_LLVM
>= 0x0400 &&
89 !(attrib_mask
& AC_FUNC_ATTR_LEGACY
);
91 function
= LLVMGetNamedFunction(ctx
->module
, name
);
93 LLVMTypeRef param_types
[32], function_type
;
96 assert(param_count
<= 32);
98 for (i
= 0; i
< param_count
; ++i
) {
100 param_types
[i
] = LLVMTypeOf(params
[i
]);
103 LLVMFunctionType(return_type
, param_types
, param_count
, 0);
104 function
= LLVMAddFunction(ctx
->module
, name
, function_type
);
106 LLVMSetFunctionCallConv(function
, LLVMCCallConv
);
107 LLVMSetLinkage(function
, LLVMExternalLinkage
);
109 if (!set_callsite_attrs
)
110 ac_add_func_attributes(ctx
->context
, function
, attrib_mask
);
113 call
= LLVMBuildCall(ctx
->builder
, function
, params
, param_count
, "");
114 if (set_callsite_attrs
)
115 ac_add_func_attributes(ctx
->context
, call
, attrib_mask
);
119 static LLVMValueRef
bitcast_to_float(struct ac_llvm_context
*ctx
,
122 LLVMTypeRef type
= LLVMTypeOf(value
);
123 LLVMTypeRef new_type
;
125 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
)
126 new_type
= LLVMVectorType(ctx
->f32
, LLVMGetVectorSize(type
));
130 return LLVMBuildBitCast(ctx
->builder
, value
, new_type
, "");
134 * Given the i32 or vNi32 \p type, generate the textual name (e.g. for use with
137 void ac_build_type_name_for_intr(LLVMTypeRef type
, char *buf
, unsigned bufsize
)
139 LLVMTypeRef elem_type
= type
;
141 assert(bufsize
>= 8);
143 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
) {
144 int ret
= snprintf(buf
, bufsize
, "v%u",
145 LLVMGetVectorSize(type
));
147 char *type_name
= LLVMPrintTypeToString(type
);
148 fprintf(stderr
, "Error building type name for: %s\n",
152 elem_type
= LLVMGetElementType(type
);
156 switch (LLVMGetTypeKind(elem_type
)) {
158 case LLVMIntegerTypeKind
:
159 snprintf(buf
, bufsize
, "i%d", LLVMGetIntTypeWidth(elem_type
));
161 case LLVMFloatTypeKind
:
162 snprintf(buf
, bufsize
, "f32");
164 case LLVMDoubleTypeKind
:
165 snprintf(buf
, bufsize
, "f64");
171 ac_build_gather_values_extended(struct ac_llvm_context
*ctx
,
172 LLVMValueRef
*values
,
173 unsigned value_count
,
174 unsigned value_stride
,
177 LLVMBuilderRef builder
= ctx
->builder
;
178 LLVMValueRef vec
= NULL
;
181 if (value_count
== 1) {
183 return LLVMBuildLoad(builder
, values
[0], "");
185 } else if (!value_count
)
186 unreachable("value_count is 0");
188 for (i
= 0; i
< value_count
; i
++) {
189 LLVMValueRef value
= values
[i
* value_stride
];
191 value
= LLVMBuildLoad(builder
, value
, "");
194 vec
= LLVMGetUndef( LLVMVectorType(LLVMTypeOf(value
), value_count
));
195 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, i
, false);
196 vec
= LLVMBuildInsertElement(builder
, vec
, value
, index
, "");
202 ac_build_gather_values(struct ac_llvm_context
*ctx
,
203 LLVMValueRef
*values
,
204 unsigned value_count
)
206 return ac_build_gather_values_extended(ctx
, values
, value_count
, 1, false);
210 ac_build_fdiv(struct ac_llvm_context
*ctx
,
214 LLVMValueRef ret
= LLVMBuildFDiv(ctx
->builder
, num
, den
, "");
216 if (!LLVMIsConstant(ret
))
217 LLVMSetMetadata(ret
, ctx
->fpmath_md_kind
, ctx
->fpmath_md_2p5_ulp
);
221 /* Coordinates for cube map selection. sc, tc, and ma are as in Table 8.27
222 * of the OpenGL 4.5 (Compatibility Profile) specification, except ma is
223 * already multiplied by two. id is the cube face number.
225 struct cube_selection_coords
{
232 build_cube_intrinsic(struct ac_llvm_context
*ctx
,
234 struct cube_selection_coords
*out
)
236 LLVMTypeRef f32
= ctx
->f32
;
238 out
->stc
[1] = ac_build_intrinsic(ctx
, "llvm.amdgcn.cubetc",
239 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
240 out
->stc
[0] = ac_build_intrinsic(ctx
, "llvm.amdgcn.cubesc",
241 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
242 out
->ma
= ac_build_intrinsic(ctx
, "llvm.amdgcn.cubema",
243 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
244 out
->id
= ac_build_intrinsic(ctx
, "llvm.amdgcn.cubeid",
245 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
249 * Build a manual selection sequence for cube face sc/tc coordinates and
250 * major axis vector (multiplied by 2 for consistency) for the given
251 * vec3 \p coords, for the face implied by \p selcoords.
253 * For the major axis, we always adjust the sign to be in the direction of
254 * selcoords.ma; i.e., a positive out_ma means that coords is pointed towards
255 * the selcoords major axis.
257 static void build_cube_select(LLVMBuilderRef builder
,
258 const struct cube_selection_coords
*selcoords
,
259 const LLVMValueRef
*coords
,
260 LLVMValueRef
*out_st
,
261 LLVMValueRef
*out_ma
)
263 LLVMTypeRef f32
= LLVMTypeOf(coords
[0]);
264 LLVMValueRef is_ma_positive
;
266 LLVMValueRef is_ma_z
, is_not_ma_z
;
267 LLVMValueRef is_ma_y
;
268 LLVMValueRef is_ma_x
;
272 is_ma_positive
= LLVMBuildFCmp(builder
, LLVMRealUGE
,
273 selcoords
->ma
, LLVMConstReal(f32
, 0.0), "");
274 sgn_ma
= LLVMBuildSelect(builder
, is_ma_positive
,
275 LLVMConstReal(f32
, 1.0), LLVMConstReal(f32
, -1.0), "");
277 is_ma_z
= LLVMBuildFCmp(builder
, LLVMRealUGE
, selcoords
->id
, LLVMConstReal(f32
, 4.0), "");
278 is_not_ma_z
= LLVMBuildNot(builder
, is_ma_z
, "");
279 is_ma_y
= LLVMBuildAnd(builder
, is_not_ma_z
,
280 LLVMBuildFCmp(builder
, LLVMRealUGE
, selcoords
->id
, LLVMConstReal(f32
, 2.0), ""), "");
281 is_ma_x
= LLVMBuildAnd(builder
, is_not_ma_z
, LLVMBuildNot(builder
, is_ma_y
, ""), "");
284 tmp
= LLVMBuildSelect(builder
, is_ma_z
, coords
[2], coords
[0], "");
285 sgn
= LLVMBuildSelect(builder
, is_ma_y
, LLVMConstReal(f32
, 1.0),
286 LLVMBuildSelect(builder
, is_ma_x
, sgn_ma
,
287 LLVMBuildFNeg(builder
, sgn_ma
, ""), ""), "");
288 out_st
[0] = LLVMBuildFMul(builder
, tmp
, sgn
, "");
291 tmp
= LLVMBuildSelect(builder
, is_ma_y
, coords
[2], coords
[1], "");
292 sgn
= LLVMBuildSelect(builder
, is_ma_y
, LLVMBuildFNeg(builder
, sgn_ma
, ""),
293 LLVMConstReal(f32
, -1.0), "");
294 out_st
[1] = LLVMBuildFMul(builder
, tmp
, sgn
, "");
297 tmp
= LLVMBuildSelect(builder
, is_ma_z
, coords
[2],
298 LLVMBuildSelect(builder
, is_ma_y
, coords
[1], coords
[0], ""), "");
299 sgn
= LLVMBuildSelect(builder
, is_ma_positive
,
300 LLVMConstReal(f32
, 2.0), LLVMConstReal(f32
, -2.0), "");
301 *out_ma
= LLVMBuildFMul(builder
, tmp
, sgn
, "");
305 ac_prepare_cube_coords(struct ac_llvm_context
*ctx
,
306 bool is_deriv
, bool is_array
,
307 LLVMValueRef
*coords_arg
,
308 LLVMValueRef
*derivs_arg
)
311 LLVMBuilderRef builder
= ctx
->builder
;
312 struct cube_selection_coords selcoords
;
313 LLVMValueRef coords
[3];
316 build_cube_intrinsic(ctx
, coords_arg
, &selcoords
);
318 invma
= ac_build_intrinsic(ctx
, "llvm.fabs.f32",
319 ctx
->f32
, &selcoords
.ma
, 1, AC_FUNC_ATTR_READNONE
);
320 invma
= ac_build_fdiv(ctx
, LLVMConstReal(ctx
->f32
, 1.0), invma
);
322 for (int i
= 0; i
< 2; ++i
)
323 coords
[i
] = LLVMBuildFMul(builder
, selcoords
.stc
[i
], invma
, "");
325 coords
[2] = selcoords
.id
;
327 if (is_deriv
&& derivs_arg
) {
328 LLVMValueRef derivs
[4];
331 /* Convert cube derivatives to 2D derivatives. */
332 for (axis
= 0; axis
< 2; axis
++) {
333 LLVMValueRef deriv_st
[2];
334 LLVMValueRef deriv_ma
;
336 /* Transform the derivative alongside the texture
337 * coordinate. Mathematically, the correct formula is
338 * as follows. Assume we're projecting onto the +Z face
339 * and denote by dx/dh the derivative of the (original)
340 * X texture coordinate with respect to horizontal
341 * window coordinates. The projection onto the +Z face
346 * Then df/dh = df/dx * dx/dh + df/dz * dz/dh
347 * = 1/z * dx/dh - x/z * 1/z * dz/dh.
349 * This motivatives the implementation below.
351 * Whether this actually gives the expected results for
352 * apps that might feed in derivatives obtained via
353 * finite differences is anyone's guess. The OpenGL spec
354 * seems awfully quiet about how textureGrad for cube
355 * maps should be handled.
357 build_cube_select(builder
, &selcoords
, &derivs_arg
[axis
* 3],
358 deriv_st
, &deriv_ma
);
360 deriv_ma
= LLVMBuildFMul(builder
, deriv_ma
, invma
, "");
362 for (int i
= 0; i
< 2; ++i
)
363 derivs
[axis
* 2 + i
] =
364 LLVMBuildFSub(builder
,
365 LLVMBuildFMul(builder
, deriv_st
[i
], invma
, ""),
366 LLVMBuildFMul(builder
, deriv_ma
, coords
[i
], ""), "");
369 memcpy(derivs_arg
, derivs
, sizeof(derivs
));
372 /* Shift the texture coordinate. This must be applied after the
373 * derivative calculation.
375 for (int i
= 0; i
< 2; ++i
)
376 coords
[i
] = LLVMBuildFAdd(builder
, coords
[i
], LLVMConstReal(ctx
->f32
, 1.5), "");
379 /* for cube arrays coord.z = coord.w(array_index) * 8 + face */
380 /* coords_arg.w component - array_index for cube arrays */
381 LLVMValueRef tmp
= LLVMBuildFMul(ctx
->builder
, coords_arg
[3], LLVMConstReal(ctx
->f32
, 8.0), "");
382 coords
[2] = LLVMBuildFAdd(ctx
->builder
, tmp
, coords
[2], "");
385 memcpy(coords_arg
, coords
, sizeof(coords
));
390 ac_build_fs_interp(struct ac_llvm_context
*ctx
,
391 LLVMValueRef llvm_chan
,
392 LLVMValueRef attr_number
,
397 LLVMValueRef args
[5];
400 if (HAVE_LLVM
< 0x0400) {
402 ij
[0] = LLVMBuildBitCast(ctx
->builder
, i
, ctx
->i32
, "");
403 ij
[1] = LLVMBuildBitCast(ctx
->builder
, j
, ctx
->i32
, "");
406 args
[1] = attr_number
;
408 args
[3] = ac_build_gather_values(ctx
, ij
, 2);
409 return ac_build_intrinsic(ctx
, "llvm.SI.fs.interp",
411 AC_FUNC_ATTR_READNONE
);
416 args
[2] = attr_number
;
419 p1
= ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p1",
420 ctx
->f32
, args
, 4, AC_FUNC_ATTR_READNONE
);
425 args
[3] = attr_number
;
428 return ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p2",
429 ctx
->f32
, args
, 5, AC_FUNC_ATTR_READNONE
);
433 ac_build_fs_interp_mov(struct ac_llvm_context
*ctx
,
434 LLVMValueRef parameter
,
435 LLVMValueRef llvm_chan
,
436 LLVMValueRef attr_number
,
439 LLVMValueRef args
[4];
440 if (HAVE_LLVM
< 0x0400) {
442 args
[1] = attr_number
;
445 return ac_build_intrinsic(ctx
,
446 "llvm.SI.fs.constant",
448 AC_FUNC_ATTR_READNONE
);
453 args
[2] = attr_number
;
456 return ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.mov",
457 ctx
->f32
, args
, 4, AC_FUNC_ATTR_READNONE
);
461 ac_build_gep0(struct ac_llvm_context
*ctx
,
462 LLVMValueRef base_ptr
,
465 LLVMValueRef indices
[2] = {
466 LLVMConstInt(ctx
->i32
, 0, 0),
469 return LLVMBuildGEP(ctx
->builder
, base_ptr
,
474 ac_build_indexed_store(struct ac_llvm_context
*ctx
,
475 LLVMValueRef base_ptr
, LLVMValueRef index
,
478 LLVMBuildStore(ctx
->builder
, value
,
479 ac_build_gep0(ctx
, base_ptr
, index
));
483 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad.
484 * It's equivalent to doing a load from &base_ptr[index].
486 * \param base_ptr Where the array starts.
487 * \param index The element index into the array.
488 * \param uniform Whether the base_ptr and index can be assumed to be
489 * dynamically uniform
492 ac_build_indexed_load(struct ac_llvm_context
*ctx
,
493 LLVMValueRef base_ptr
, LLVMValueRef index
,
496 LLVMValueRef pointer
;
498 pointer
= ac_build_gep0(ctx
, base_ptr
, index
);
500 LLVMSetMetadata(pointer
, ctx
->uniform_md_kind
, ctx
->empty_md
);
501 return LLVMBuildLoad(ctx
->builder
, pointer
, "");
505 * Do a load from &base_ptr[index], but also add a flag that it's loading
506 * a constant from a dynamically uniform index.
509 ac_build_indexed_load_const(struct ac_llvm_context
*ctx
,
510 LLVMValueRef base_ptr
, LLVMValueRef index
)
512 LLVMValueRef result
= ac_build_indexed_load(ctx
, base_ptr
, index
, true);
513 LLVMSetMetadata(result
, ctx
->invariant_load_md_kind
, ctx
->empty_md
);
517 /* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
518 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
519 * or v4i32 (num_channels=3,4).
522 ac_build_buffer_store_dword(struct ac_llvm_context
*ctx
,
525 unsigned num_channels
,
526 LLVMValueRef voffset
,
527 LLVMValueRef soffset
,
528 unsigned inst_offset
,
531 bool writeonly_memory
,
534 /* TODO: Fix stores with ADD_TID and remove the "has_add_tid" flag. */
536 /* Split 3 channel stores, becase LLVM doesn't support 3-channel
538 if (num_channels
== 3) {
539 LLVMValueRef v
[3], v01
;
541 for (int i
= 0; i
< 3; i
++) {
542 v
[i
] = LLVMBuildExtractElement(ctx
->builder
, vdata
,
543 LLVMConstInt(ctx
->i32
, i
, 0), "");
545 v01
= ac_build_gather_values(ctx
, v
, 2);
547 ac_build_buffer_store_dword(ctx
, rsrc
, v01
, 2, voffset
,
548 soffset
, inst_offset
, glc
, slc
,
549 writeonly_memory
, has_add_tid
);
550 ac_build_buffer_store_dword(ctx
, rsrc
, v
[2], 1, voffset
,
551 soffset
, inst_offset
+ 8,
553 writeonly_memory
, has_add_tid
);
557 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
558 static const char *types
[] = {"f32", "v2f32", "v4f32"};
560 LLVMValueRef offset
= soffset
;
563 offset
= LLVMBuildAdd(ctx
->builder
, offset
,
564 LLVMConstInt(ctx
->i32
, inst_offset
, 0), "");
566 offset
= LLVMBuildAdd(ctx
->builder
, offset
, voffset
, "");
568 LLVMValueRef args
[] = {
569 bitcast_to_float(ctx
, vdata
),
570 LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, ""),
571 LLVMConstInt(ctx
->i32
, 0, 0),
573 LLVMConstInt(ctx
->i1
, glc
, 0),
574 LLVMConstInt(ctx
->i1
, slc
, 0),
577 snprintf(name
, sizeof(name
), "llvm.amdgcn.buffer.store.%s",
580 ac_build_intrinsic(ctx
, name
, ctx
->voidt
,
581 args
, ARRAY_SIZE(args
),
583 AC_FUNC_ATTR_INACCESSIBLE_MEM_ONLY
:
584 AC_FUNC_ATTR_WRITEONLY
);
588 static unsigned dfmt
[] = {
589 V_008F0C_BUF_DATA_FORMAT_32
,
590 V_008F0C_BUF_DATA_FORMAT_32_32
,
591 V_008F0C_BUF_DATA_FORMAT_32_32_32
,
592 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
594 assert(num_channels
>= 1 && num_channels
<= 4);
596 LLVMValueRef args
[] = {
599 LLVMConstInt(ctx
->i32
, num_channels
, 0),
600 voffset
? voffset
: LLVMGetUndef(ctx
->i32
),
602 LLVMConstInt(ctx
->i32
, inst_offset
, 0),
603 LLVMConstInt(ctx
->i32
, dfmt
[num_channels
- 1], 0),
604 LLVMConstInt(ctx
->i32
, V_008F0C_BUF_NUM_FORMAT_UINT
, 0),
605 LLVMConstInt(ctx
->i32
, voffset
!= NULL
, 0),
606 LLVMConstInt(ctx
->i32
, 0, 0), /* idxen */
607 LLVMConstInt(ctx
->i32
, glc
, 0),
608 LLVMConstInt(ctx
->i32
, slc
, 0),
609 LLVMConstInt(ctx
->i32
, 0, 0), /* tfe*/
612 /* The instruction offset field has 12 bits */
613 assert(voffset
|| inst_offset
< (1 << 12));
615 /* The intrinsic is overloaded, we need to add a type suffix for overloading to work. */
616 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
617 const char *types
[] = {"i32", "v2i32", "v4i32"};
619 snprintf(name
, sizeof(name
), "llvm.SI.tbuffer.store.%s", types
[func
]);
621 ac_build_intrinsic(ctx
, name
, ctx
->voidt
,
622 args
, ARRAY_SIZE(args
),
623 AC_FUNC_ATTR_LEGACY
);
627 ac_build_buffer_load(struct ac_llvm_context
*ctx
,
631 LLVMValueRef voffset
,
632 LLVMValueRef soffset
,
633 unsigned inst_offset
,
636 bool readonly_memory
)
638 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
640 LLVMValueRef args
[] = {
641 LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, ""),
642 vindex
? vindex
: LLVMConstInt(ctx
->i32
, 0, 0),
643 LLVMConstInt(ctx
->i32
, inst_offset
, 0),
644 LLVMConstInt(ctx
->i1
, glc
, 0),
645 LLVMConstInt(ctx
->i1
, slc
, 0)
648 LLVMTypeRef types
[] = {ctx
->f32
, LLVMVectorType(ctx
->f32
, 2),
650 const char *type_names
[] = {"f32", "v2f32", "v4f32"};
654 args
[2] = LLVMBuildAdd(ctx
->builder
, args
[2], voffset
,
659 args
[2] = LLVMBuildAdd(ctx
->builder
, args
[2], soffset
,
663 snprintf(name
, sizeof(name
), "llvm.amdgcn.buffer.load.%s",
666 return ac_build_intrinsic(ctx
, name
, types
[func
], args
,
668 /* READNONE means writes can't affect it, while
669 * READONLY means that writes can affect it. */
670 readonly_memory
&& HAVE_LLVM
>= 0x0400 ?
671 AC_FUNC_ATTR_READNONE
:
672 AC_FUNC_ATTR_READONLY
);
675 LLVMValueRef
ac_build_buffer_load_format(struct ac_llvm_context
*ctx
,
678 LLVMValueRef voffset
,
679 bool readonly_memory
)
681 LLVMValueRef args
[] = {
682 LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, ""),
685 LLVMConstInt(ctx
->i1
, 0, 0), /* glc */
686 LLVMConstInt(ctx
->i1
, 0, 0), /* slc */
689 return ac_build_intrinsic(ctx
,
690 "llvm.amdgcn.buffer.load.format.v4f32",
691 ctx
->v4f32
, args
, ARRAY_SIZE(args
),
692 /* READNONE means writes can't affect it, while
693 * READONLY means that writes can affect it. */
694 readonly_memory
&& HAVE_LLVM
>= 0x0400 ?
695 AC_FUNC_ATTR_READNONE
:
696 AC_FUNC_ATTR_READONLY
);
700 * Set range metadata on an instruction. This can only be used on load and
701 * call instructions. If you know an instruction can only produce the values
702 * 0, 1, 2, you would do set_range_metadata(value, 0, 3);
703 * \p lo is the minimum value inclusive.
704 * \p hi is the maximum value exclusive.
706 static void set_range_metadata(struct ac_llvm_context
*ctx
,
707 LLVMValueRef value
, unsigned lo
, unsigned hi
)
709 LLVMValueRef range_md
, md_args
[2];
710 LLVMTypeRef type
= LLVMTypeOf(value
);
711 LLVMContextRef context
= LLVMGetTypeContext(type
);
713 md_args
[0] = LLVMConstInt(type
, lo
, false);
714 md_args
[1] = LLVMConstInt(type
, hi
, false);
715 range_md
= LLVMMDNodeInContext(context
, md_args
, 2);
716 LLVMSetMetadata(value
, ctx
->range_md_kind
, range_md
);
720 ac_get_thread_id(struct ac_llvm_context
*ctx
)
724 LLVMValueRef tid_args
[2];
725 tid_args
[0] = LLVMConstInt(ctx
->i32
, 0xffffffff, false);
726 tid_args
[1] = LLVMConstInt(ctx
->i32
, 0, false);
727 tid_args
[1] = ac_build_intrinsic(ctx
,
728 "llvm.amdgcn.mbcnt.lo", ctx
->i32
,
729 tid_args
, 2, AC_FUNC_ATTR_READNONE
);
731 tid
= ac_build_intrinsic(ctx
, "llvm.amdgcn.mbcnt.hi",
733 2, AC_FUNC_ATTR_READNONE
);
734 set_range_metadata(ctx
, tid
, 0, 64);
739 * SI implements derivatives using the local data store (LDS)
740 * All writes to the LDS happen in all executing threads at
741 * the same time. TID is the Thread ID for the current
742 * thread and is a value between 0 and 63, representing
743 * the thread's position in the wavefront.
745 * For the pixel shader threads are grouped into quads of four pixels.
746 * The TIDs of the pixels of a quad are:
754 * So, masking the TID with 0xfffffffc yields the TID of the top left pixel
755 * of the quad, masking with 0xfffffffd yields the TID of the top pixel of
756 * the current pixel's column, and masking with 0xfffffffe yields the TID
757 * of the left pixel of the current pixel's row.
759 * Adding 1 yields the TID of the pixel to the right of the left pixel, and
760 * adding 2 yields the TID of the pixel below the top pixel.
763 ac_build_ddxy(struct ac_llvm_context
*ctx
,
764 bool has_ds_bpermute
,
770 LLVMValueRef thread_id
, tl
, trbl
, tl_tid
, trbl_tid
, args
[2];
773 thread_id
= ac_get_thread_id(ctx
);
775 tl_tid
= LLVMBuildAnd(ctx
->builder
, thread_id
,
776 LLVMConstInt(ctx
->i32
, mask
, false), "");
778 trbl_tid
= LLVMBuildAdd(ctx
->builder
, tl_tid
,
779 LLVMConstInt(ctx
->i32
, idx
, false), "");
781 if (has_ds_bpermute
) {
782 args
[0] = LLVMBuildMul(ctx
->builder
, tl_tid
,
783 LLVMConstInt(ctx
->i32
, 4, false), "");
785 tl
= ac_build_intrinsic(ctx
,
786 "llvm.amdgcn.ds.bpermute", ctx
->i32
,
788 AC_FUNC_ATTR_READNONE
|
789 AC_FUNC_ATTR_CONVERGENT
);
791 args
[0] = LLVMBuildMul(ctx
->builder
, trbl_tid
,
792 LLVMConstInt(ctx
->i32
, 4, false), "");
793 trbl
= ac_build_intrinsic(ctx
,
794 "llvm.amdgcn.ds.bpermute", ctx
->i32
,
796 AC_FUNC_ATTR_READNONE
|
797 AC_FUNC_ATTR_CONVERGENT
);
799 LLVMValueRef store_ptr
, load_ptr0
, load_ptr1
;
801 store_ptr
= ac_build_gep0(ctx
, lds
, thread_id
);
802 load_ptr0
= ac_build_gep0(ctx
, lds
, tl_tid
);
803 load_ptr1
= ac_build_gep0(ctx
, lds
, trbl_tid
);
805 LLVMBuildStore(ctx
->builder
, val
, store_ptr
);
806 tl
= LLVMBuildLoad(ctx
->builder
, load_ptr0
, "");
807 trbl
= LLVMBuildLoad(ctx
->builder
, load_ptr1
, "");
810 tl
= LLVMBuildBitCast(ctx
->builder
, tl
, ctx
->f32
, "");
811 trbl
= LLVMBuildBitCast(ctx
->builder
, trbl
, ctx
->f32
, "");
812 result
= LLVMBuildFSub(ctx
->builder
, trbl
, tl
, "");
817 ac_build_sendmsg(struct ac_llvm_context
*ctx
,
819 LLVMValueRef wave_id
)
821 LLVMValueRef args
[2];
822 const char *intr_name
= (HAVE_LLVM
< 0x0400) ? "llvm.SI.sendmsg" : "llvm.amdgcn.s.sendmsg";
823 args
[0] = LLVMConstInt(ctx
->i32
, msg
, false);
825 ac_build_intrinsic(ctx
, intr_name
, ctx
->voidt
, args
, 2, 0);
829 ac_build_imsb(struct ac_llvm_context
*ctx
,
831 LLVMTypeRef dst_type
)
833 const char *intr_name
= (HAVE_LLVM
< 0x0400) ? "llvm.AMDGPU.flbit.i32" :
834 "llvm.amdgcn.sffbh.i32";
835 LLVMValueRef msb
= ac_build_intrinsic(ctx
, intr_name
,
837 AC_FUNC_ATTR_READNONE
);
839 /* The HW returns the last bit index from MSB, but NIR/TGSI wants
840 * the index from LSB. Invert it by doing "31 - msb". */
841 msb
= LLVMBuildSub(ctx
->builder
, LLVMConstInt(ctx
->i32
, 31, false),
844 LLVMValueRef all_ones
= LLVMConstInt(ctx
->i32
, -1, true);
845 LLVMValueRef cond
= LLVMBuildOr(ctx
->builder
,
846 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
847 arg
, LLVMConstInt(ctx
->i32
, 0, 0), ""),
848 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
849 arg
, all_ones
, ""), "");
851 return LLVMBuildSelect(ctx
->builder
, cond
, all_ones
, msb
, "");
855 ac_build_umsb(struct ac_llvm_context
*ctx
,
857 LLVMTypeRef dst_type
)
859 LLVMValueRef args
[2] = {
861 LLVMConstInt(ctx
->i1
, 1, 0),
863 LLVMValueRef msb
= ac_build_intrinsic(ctx
, "llvm.ctlz.i32",
864 dst_type
, args
, ARRAY_SIZE(args
),
865 AC_FUNC_ATTR_READNONE
);
867 /* The HW returns the last bit index from MSB, but TGSI/NIR wants
868 * the index from LSB. Invert it by doing "31 - msb". */
869 msb
= LLVMBuildSub(ctx
->builder
, LLVMConstInt(ctx
->i32
, 31, false),
873 return LLVMBuildSelect(ctx
->builder
,
874 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, arg
,
875 LLVMConstInt(ctx
->i32
, 0, 0), ""),
876 LLVMConstInt(ctx
->i32
, -1, true), msb
, "");
879 LLVMValueRef
ac_build_clamp(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
881 if (HAVE_LLVM
>= 0x0500) {
882 LLVMValueRef max
[2] = {
884 LLVMConstReal(ctx
->f32
, 0),
886 LLVMValueRef min
[2] = {
887 LLVMConstReal(ctx
->f32
, 1),
890 min
[1] = ac_build_intrinsic(ctx
, "llvm.maxnum.f32",
892 AC_FUNC_ATTR_READNONE
);
893 return ac_build_intrinsic(ctx
, "llvm.minnum.f32",
895 AC_FUNC_ATTR_READNONE
);
898 LLVMValueRef args
[3] = {
900 LLVMConstReal(ctx
->f32
, 0),
901 LLVMConstReal(ctx
->f32
, 1),
904 return ac_build_intrinsic(ctx
, "llvm.AMDGPU.clamp.", ctx
->f32
, args
, 3,
905 AC_FUNC_ATTR_READNONE
|
906 AC_FUNC_ATTR_LEGACY
);
909 void ac_build_export(struct ac_llvm_context
*ctx
, struct ac_export_args
*a
)
911 LLVMValueRef args
[9];
913 if (HAVE_LLVM
>= 0x0500) {
914 args
[0] = LLVMConstInt(ctx
->i32
, a
->target
, 0);
915 args
[1] = LLVMConstInt(ctx
->i32
, a
->enabled_channels
, 0);
918 LLVMTypeRef i16
= LLVMInt16TypeInContext(ctx
->context
);
919 LLVMTypeRef v2i16
= LLVMVectorType(i16
, 2);
921 args
[2] = LLVMBuildBitCast(ctx
->builder
, a
->out
[0],
923 args
[3] = LLVMBuildBitCast(ctx
->builder
, a
->out
[1],
925 args
[4] = LLVMConstInt(ctx
->i1
, a
->done
, 0);
926 args
[5] = LLVMConstInt(ctx
->i1
, a
->valid_mask
, 0);
928 ac_build_intrinsic(ctx
, "llvm.amdgcn.exp.compr.v2i16",
929 ctx
->voidt
, args
, 6, 0);
935 args
[6] = LLVMConstInt(ctx
->i1
, a
->done
, 0);
936 args
[7] = LLVMConstInt(ctx
->i1
, a
->valid_mask
, 0);
938 ac_build_intrinsic(ctx
, "llvm.amdgcn.exp.f32",
939 ctx
->voidt
, args
, 8, 0);
944 args
[0] = LLVMConstInt(ctx
->i32
, a
->enabled_channels
, 0);
945 args
[1] = LLVMConstInt(ctx
->i32
, a
->valid_mask
, 0);
946 args
[2] = LLVMConstInt(ctx
->i32
, a
->done
, 0);
947 args
[3] = LLVMConstInt(ctx
->i32
, a
->target
, 0);
948 args
[4] = LLVMConstInt(ctx
->i32
, a
->compr
, 0);
949 memcpy(args
+ 5, a
->out
, sizeof(a
->out
[0]) * 4);
951 ac_build_intrinsic(ctx
, "llvm.SI.export", ctx
->voidt
, args
, 9,
952 AC_FUNC_ATTR_LEGACY
);
955 LLVMValueRef
ac_build_image_opcode(struct ac_llvm_context
*ctx
,
956 struct ac_image_args
*a
)
958 LLVMTypeRef dst_type
;
959 LLVMValueRef args
[11];
960 unsigned num_args
= 0;
962 char intr_name
[128], type
[64];
964 if (HAVE_LLVM
>= 0x0400) {
965 bool sample
= a
->opcode
== ac_image_sample
||
966 a
->opcode
== ac_image_gather4
||
967 a
->opcode
== ac_image_get_lod
;
970 args
[num_args
++] = bitcast_to_float(ctx
, a
->addr
);
972 args
[num_args
++] = a
->addr
;
974 args
[num_args
++] = a
->resource
;
976 args
[num_args
++] = a
->sampler
;
977 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->dmask
, 0);
979 args
[num_args
++] = LLVMConstInt(ctx
->i1
, a
->unorm
, 0);
980 args
[num_args
++] = LLVMConstInt(ctx
->i1
, 0, 0); /* glc */
981 args
[num_args
++] = LLVMConstInt(ctx
->i1
, 0, 0); /* slc */
982 args
[num_args
++] = LLVMConstInt(ctx
->i1
, 0, 0); /* lwe */
983 args
[num_args
++] = LLVMConstInt(ctx
->i1
, a
->da
, 0);
986 case ac_image_sample
:
987 name
= "llvm.amdgcn.image.sample";
989 case ac_image_gather4
:
990 name
= "llvm.amdgcn.image.gather4";
993 name
= "llvm.amdgcn.image.load";
995 case ac_image_load_mip
:
996 name
= "llvm.amdgcn.image.load.mip";
998 case ac_image_get_lod
:
999 name
= "llvm.amdgcn.image.getlod";
1001 case ac_image_get_resinfo
:
1002 name
= "llvm.amdgcn.image.getresinfo";
1005 unreachable("invalid image opcode");
1008 ac_build_type_name_for_intr(LLVMTypeOf(args
[0]), type
,
1011 snprintf(intr_name
, sizeof(intr_name
), "%s%s%s%s.v4f32.%s.v8i32",
1013 a
->compare
? ".c" : "",
1017 a
->level_zero
? ".lz" : "",
1018 a
->offset
? ".o" : "",
1021 LLVMValueRef result
=
1022 ac_build_intrinsic(ctx
, intr_name
,
1023 ctx
->v4f32
, args
, num_args
,
1024 AC_FUNC_ATTR_READNONE
);
1026 result
= LLVMBuildBitCast(ctx
->builder
, result
,
1032 args
[num_args
++] = a
->addr
;
1033 args
[num_args
++] = a
->resource
;
1035 if (a
->opcode
== ac_image_load
||
1036 a
->opcode
== ac_image_load_mip
||
1037 a
->opcode
== ac_image_get_resinfo
) {
1038 dst_type
= ctx
->v4i32
;
1040 dst_type
= ctx
->v4f32
;
1041 args
[num_args
++] = a
->sampler
;
1044 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->dmask
, 0);
1045 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->unorm
, 0);
1046 args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* r128 */
1047 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->da
, 0);
1048 args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* glc */
1049 args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* slc */
1050 args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* tfe */
1051 args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* lwe */
1053 switch (a
->opcode
) {
1054 case ac_image_sample
:
1055 name
= "llvm.SI.image.sample";
1057 case ac_image_gather4
:
1058 name
= "llvm.SI.gather4";
1061 name
= "llvm.SI.image.load";
1063 case ac_image_load_mip
:
1064 name
= "llvm.SI.image.load.mip";
1066 case ac_image_get_lod
:
1067 name
= "llvm.SI.getlod";
1069 case ac_image_get_resinfo
:
1070 name
= "llvm.SI.getresinfo";
1074 ac_build_type_name_for_intr(LLVMTypeOf(a
->addr
), type
, sizeof(type
));
1075 snprintf(intr_name
, sizeof(intr_name
), "%s%s%s%s.%s",
1077 a
->compare
? ".c" : "",
1081 a
->level_zero
? ".lz" : "",
1082 a
->offset
? ".o" : "",
1085 return ac_build_intrinsic(ctx
, intr_name
,
1086 dst_type
, args
, num_args
,
1087 AC_FUNC_ATTR_READNONE
|
1088 AC_FUNC_ATTR_LEGACY
);
1091 LLVMValueRef
ac_build_cvt_pkrtz_f16(struct ac_llvm_context
*ctx
,
1092 LLVMValueRef args
[2])
1094 if (HAVE_LLVM
>= 0x0500) {
1096 LLVMVectorType(LLVMHalfTypeInContext(ctx
->context
), 2);
1098 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pkrtz",
1100 AC_FUNC_ATTR_READNONE
);
1101 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
1104 return ac_build_intrinsic(ctx
, "llvm.SI.packf16", ctx
->i32
, args
, 2,
1105 AC_FUNC_ATTR_READNONE
|
1106 AC_FUNC_ATTR_LEGACY
);
1110 * KILL, AKA discard in GLSL.
1112 * \param value kill if value < 0.0 or value == NULL.
1114 void ac_build_kill(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
1117 ac_build_intrinsic(ctx
, "llvm.AMDGPU.kill", ctx
->voidt
,
1118 &value
, 1, AC_FUNC_ATTR_LEGACY
);
1120 ac_build_intrinsic(ctx
, "llvm.AMDGPU.kilp", ctx
->voidt
,
1121 NULL
, 0, AC_FUNC_ATTR_LEGACY
);
1125 LLVMValueRef
ac_build_bfe(struct ac_llvm_context
*ctx
, LLVMValueRef input
,
1126 LLVMValueRef offset
, LLVMValueRef width
,
1129 LLVMValueRef args
[] = {
1135 if (HAVE_LLVM
>= 0x0500) {
1136 return ac_build_intrinsic(ctx
,
1137 is_signed
? "llvm.amdgcn.sbfe.i32" :
1138 "llvm.amdgcn.ubfe.i32",
1140 AC_FUNC_ATTR_READNONE
);
1143 return ac_build_intrinsic(ctx
,
1144 is_signed
? "llvm.AMDGPU.bfe.i32" :
1145 "llvm.AMDGPU.bfe.u32",
1147 AC_FUNC_ATTR_READNONE
|
1148 AC_FUNC_ATTR_LEGACY
);
1151 void ac_get_image_intr_name(const char *base_name
,
1152 LLVMTypeRef data_type
,
1153 LLVMTypeRef coords_type
,
1154 LLVMTypeRef rsrc_type
,
1155 char *out_name
, unsigned out_len
)
1157 char coords_type_name
[8];
1159 ac_build_type_name_for_intr(coords_type
, coords_type_name
,
1160 sizeof(coords_type_name
));
1162 if (HAVE_LLVM
<= 0x0309) {
1163 snprintf(out_name
, out_len
, "%s.%s", base_name
, coords_type_name
);
1165 char data_type_name
[8];
1166 char rsrc_type_name
[8];
1168 ac_build_type_name_for_intr(data_type
, data_type_name
,
1169 sizeof(data_type_name
));
1170 ac_build_type_name_for_intr(rsrc_type
, rsrc_type_name
,
1171 sizeof(rsrc_type_name
));
1172 snprintf(out_name
, out_len
, "%s.%s.%s.%s", base_name
,
1173 data_type_name
, coords_type_name
, rsrc_type_name
);
1177 #define AC_EXP_TARGET (HAVE_LLVM >= 0x0500 ? 0 : 3)
1178 #define AC_EXP_OUT0 (HAVE_LLVM >= 0x0500 ? 2 : 5)
1186 struct ac_vs_exp_chan
1190 enum ac_ir_type type
;
1193 struct ac_vs_exp_inst
{
1196 struct ac_vs_exp_chan chan
[4];
1199 struct ac_vs_exports
{
1201 struct ac_vs_exp_inst exp
[VARYING_SLOT_MAX
];
1204 /* Return true if the PARAM export has been eliminated. */
1205 static bool ac_eliminate_const_output(uint8_t *vs_output_param_offset
,
1206 uint32_t num_outputs
,
1207 struct ac_vs_exp_inst
*exp
)
1209 unsigned i
, default_val
; /* SPI_PS_INPUT_CNTL_i.DEFAULT_VAL */
1210 bool is_zero
[4] = {}, is_one
[4] = {};
1212 for (i
= 0; i
< 4; i
++) {
1213 /* It's a constant expression. Undef outputs are eliminated too. */
1214 if (exp
->chan
[i
].type
== AC_IR_UNDEF
) {
1217 } else if (exp
->chan
[i
].type
== AC_IR_CONST
) {
1218 if (exp
->chan
[i
].const_float
== 0)
1220 else if (exp
->chan
[i
].const_float
== 1)
1223 return false; /* other constant */
1228 /* Only certain combinations of 0 and 1 can be eliminated. */
1229 if (is_zero
[0] && is_zero
[1] && is_zero
[2])
1230 default_val
= is_zero
[3] ? 0 : 1;
1231 else if (is_one
[0] && is_one
[1] && is_one
[2])
1232 default_val
= is_zero
[3] ? 2 : 3;
1236 /* The PARAM export can be represented as DEFAULT_VAL. Kill it. */
1237 LLVMInstructionEraseFromParent(exp
->inst
);
1239 /* Change OFFSET to DEFAULT_VAL. */
1240 for (i
= 0; i
< num_outputs
; i
++) {
1241 if (vs_output_param_offset
[i
] == exp
->offset
) {
1242 vs_output_param_offset
[i
] =
1243 AC_EXP_PARAM_DEFAULT_VAL_0000
+ default_val
;
1250 static bool ac_eliminate_duplicated_output(uint8_t *vs_output_param_offset
,
1251 uint32_t num_outputs
,
1252 struct ac_vs_exports
*processed
,
1253 struct ac_vs_exp_inst
*exp
)
1255 unsigned p
, copy_back_channels
= 0;
1257 /* See if the output is already in the list of processed outputs.
1258 * The LLVMValueRef comparison relies on SSA.
1260 for (p
= 0; p
< processed
->num
; p
++) {
1261 bool different
= false;
1263 for (unsigned j
= 0; j
< 4; j
++) {
1264 struct ac_vs_exp_chan
*c1
= &processed
->exp
[p
].chan
[j
];
1265 struct ac_vs_exp_chan
*c2
= &exp
->chan
[j
];
1267 /* Treat undef as a match. */
1268 if (c2
->type
== AC_IR_UNDEF
)
1271 /* If c1 is undef but c2 isn't, we can copy c2 to c1
1272 * and consider the instruction duplicated.
1274 if (c1
->type
== AC_IR_UNDEF
) {
1275 copy_back_channels
|= 1 << j
;
1279 /* Test whether the channels are not equal. */
1280 if (c1
->type
!= c2
->type
||
1281 (c1
->type
== AC_IR_CONST
&&
1282 c1
->const_float
!= c2
->const_float
) ||
1283 (c1
->type
== AC_IR_VALUE
&&
1284 c1
->value
!= c2
->value
)) {
1292 copy_back_channels
= 0;
1294 if (p
== processed
->num
)
1297 /* If a match was found, but the matching export has undef where the new
1298 * one has a normal value, copy the normal value to the undef channel.
1300 struct ac_vs_exp_inst
*match
= &processed
->exp
[p
];
1302 while (copy_back_channels
) {
1303 unsigned chan
= u_bit_scan(©_back_channels
);
1305 assert(match
->chan
[chan
].type
== AC_IR_UNDEF
);
1306 LLVMSetOperand(match
->inst
, AC_EXP_OUT0
+ chan
,
1307 exp
->chan
[chan
].value
);
1308 match
->chan
[chan
] = exp
->chan
[chan
];
1311 /* The PARAM export is duplicated. Kill it. */
1312 LLVMInstructionEraseFromParent(exp
->inst
);
1314 /* Change OFFSET to the matching export. */
1315 for (unsigned i
= 0; i
< num_outputs
; i
++) {
1316 if (vs_output_param_offset
[i
] == exp
->offset
) {
1317 vs_output_param_offset
[i
] = match
->offset
;
1324 void ac_optimize_vs_outputs(struct ac_llvm_context
*ctx
,
1325 LLVMValueRef main_fn
,
1326 uint8_t *vs_output_param_offset
,
1327 uint32_t num_outputs
,
1328 uint8_t *num_param_exports
)
1330 LLVMBasicBlockRef bb
;
1331 bool removed_any
= false;
1332 struct ac_vs_exports exports
;
1336 /* Process all LLVM instructions. */
1337 bb
= LLVMGetFirstBasicBlock(main_fn
);
1339 LLVMValueRef inst
= LLVMGetFirstInstruction(bb
);
1342 LLVMValueRef cur
= inst
;
1343 inst
= LLVMGetNextInstruction(inst
);
1344 struct ac_vs_exp_inst exp
;
1346 if (LLVMGetInstructionOpcode(cur
) != LLVMCall
)
1349 LLVMValueRef callee
= ac_llvm_get_called_value(cur
);
1351 if (!ac_llvm_is_function(callee
))
1354 const char *name
= LLVMGetValueName(callee
);
1355 unsigned num_args
= LLVMCountParams(callee
);
1357 /* Check if this is an export instruction. */
1358 if ((num_args
!= 9 && num_args
!= 8) ||
1359 (strcmp(name
, "llvm.SI.export") &&
1360 strcmp(name
, "llvm.amdgcn.exp.f32")))
1363 LLVMValueRef arg
= LLVMGetOperand(cur
, AC_EXP_TARGET
);
1364 unsigned target
= LLVMConstIntGetZExtValue(arg
);
1366 if (target
< V_008DFC_SQ_EXP_PARAM
)
1369 target
-= V_008DFC_SQ_EXP_PARAM
;
1371 /* Parse the instruction. */
1372 memset(&exp
, 0, sizeof(exp
));
1373 exp
.offset
= target
;
1376 for (unsigned i
= 0; i
< 4; i
++) {
1377 LLVMValueRef v
= LLVMGetOperand(cur
, AC_EXP_OUT0
+ i
);
1379 exp
.chan
[i
].value
= v
;
1381 if (LLVMIsUndef(v
)) {
1382 exp
.chan
[i
].type
= AC_IR_UNDEF
;
1383 } else if (LLVMIsAConstantFP(v
)) {
1384 LLVMBool loses_info
;
1385 exp
.chan
[i
].type
= AC_IR_CONST
;
1386 exp
.chan
[i
].const_float
=
1387 LLVMConstRealGetDouble(v
, &loses_info
);
1389 exp
.chan
[i
].type
= AC_IR_VALUE
;
1393 /* Eliminate constant and duplicated PARAM exports. */
1394 if (ac_eliminate_const_output(vs_output_param_offset
,
1395 num_outputs
, &exp
) ||
1396 ac_eliminate_duplicated_output(vs_output_param_offset
,
1397 num_outputs
, &exports
,
1401 exports
.exp
[exports
.num
++] = exp
;
1404 bb
= LLVMGetNextBasicBlock(bb
);
1407 /* Remove holes in export memory due to removed PARAM exports.
1408 * This is done by renumbering all PARAM exports.
1411 uint8_t old_offset
[VARYING_SLOT_MAX
];
1414 /* Make a copy of the offsets. We need the old version while
1415 * we are modifying some of them. */
1416 memcpy(old_offset
, vs_output_param_offset
,
1417 sizeof(old_offset
));
1419 for (i
= 0; i
< exports
.num
; i
++) {
1420 unsigned offset
= exports
.exp
[i
].offset
;
1422 /* Update vs_output_param_offset. Multiple outputs can
1423 * have the same offset.
1425 for (out
= 0; out
< num_outputs
; out
++) {
1426 if (old_offset
[out
] == offset
)
1427 vs_output_param_offset
[out
] = i
;
1430 /* Change the PARAM offset in the instruction. */
1431 LLVMSetOperand(exports
.exp
[i
].inst
, AC_EXP_TARGET
,
1432 LLVMConstInt(ctx
->i32
,
1433 V_008DFC_SQ_EXP_PARAM
+ i
, 0));
1435 *num_param_exports
= exports
.num
;