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 LLVMBuilderRef builder
= ctx
->builder
;
238 if (HAVE_LLVM
>= 0x0309) {
239 LLVMTypeRef f32
= ctx
->f32
;
241 out
->stc
[1] = ac_build_intrinsic(ctx
, "llvm.amdgcn.cubetc",
242 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
243 out
->stc
[0] = ac_build_intrinsic(ctx
, "llvm.amdgcn.cubesc",
244 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
245 out
->ma
= ac_build_intrinsic(ctx
, "llvm.amdgcn.cubema",
246 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
247 out
->id
= ac_build_intrinsic(ctx
, "llvm.amdgcn.cubeid",
248 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
250 LLVMValueRef c
[4] = {
254 LLVMGetUndef(LLVMTypeOf(in
[0]))
256 LLVMValueRef vec
= ac_build_gather_values(ctx
, c
, 4);
259 ac_build_intrinsic(ctx
, "llvm.AMDGPU.cube",
260 LLVMTypeOf(vec
), &vec
, 1,
261 AC_FUNC_ATTR_READNONE
);
263 out
->stc
[1] = LLVMBuildExtractElement(builder
, tmp
,
264 LLVMConstInt(ctx
->i32
, 0, 0), "");
265 out
->stc
[0] = LLVMBuildExtractElement(builder
, tmp
,
266 LLVMConstInt(ctx
->i32
, 1, 0), "");
267 out
->ma
= LLVMBuildExtractElement(builder
, tmp
,
268 LLVMConstInt(ctx
->i32
, 2, 0), "");
269 out
->id
= LLVMBuildExtractElement(builder
, tmp
,
270 LLVMConstInt(ctx
->i32
, 3, 0), "");
275 * Build a manual selection sequence for cube face sc/tc coordinates and
276 * major axis vector (multiplied by 2 for consistency) for the given
277 * vec3 \p coords, for the face implied by \p selcoords.
279 * For the major axis, we always adjust the sign to be in the direction of
280 * selcoords.ma; i.e., a positive out_ma means that coords is pointed towards
281 * the selcoords major axis.
283 static void build_cube_select(LLVMBuilderRef builder
,
284 const struct cube_selection_coords
*selcoords
,
285 const LLVMValueRef
*coords
,
286 LLVMValueRef
*out_st
,
287 LLVMValueRef
*out_ma
)
289 LLVMTypeRef f32
= LLVMTypeOf(coords
[0]);
290 LLVMValueRef is_ma_positive
;
292 LLVMValueRef is_ma_z
, is_not_ma_z
;
293 LLVMValueRef is_ma_y
;
294 LLVMValueRef is_ma_x
;
298 is_ma_positive
= LLVMBuildFCmp(builder
, LLVMRealUGE
,
299 selcoords
->ma
, LLVMConstReal(f32
, 0.0), "");
300 sgn_ma
= LLVMBuildSelect(builder
, is_ma_positive
,
301 LLVMConstReal(f32
, 1.0), LLVMConstReal(f32
, -1.0), "");
303 is_ma_z
= LLVMBuildFCmp(builder
, LLVMRealUGE
, selcoords
->id
, LLVMConstReal(f32
, 4.0), "");
304 is_not_ma_z
= LLVMBuildNot(builder
, is_ma_z
, "");
305 is_ma_y
= LLVMBuildAnd(builder
, is_not_ma_z
,
306 LLVMBuildFCmp(builder
, LLVMRealUGE
, selcoords
->id
, LLVMConstReal(f32
, 2.0), ""), "");
307 is_ma_x
= LLVMBuildAnd(builder
, is_not_ma_z
, LLVMBuildNot(builder
, is_ma_y
, ""), "");
310 tmp
= LLVMBuildSelect(builder
, is_ma_z
, coords
[2], coords
[0], "");
311 sgn
= LLVMBuildSelect(builder
, is_ma_y
, LLVMConstReal(f32
, 1.0),
312 LLVMBuildSelect(builder
, is_ma_x
, sgn_ma
,
313 LLVMBuildFNeg(builder
, sgn_ma
, ""), ""), "");
314 out_st
[0] = LLVMBuildFMul(builder
, tmp
, sgn
, "");
317 tmp
= LLVMBuildSelect(builder
, is_ma_y
, coords
[2], coords
[1], "");
318 sgn
= LLVMBuildSelect(builder
, is_ma_y
, LLVMBuildFNeg(builder
, sgn_ma
, ""),
319 LLVMConstReal(f32
, -1.0), "");
320 out_st
[1] = LLVMBuildFMul(builder
, tmp
, sgn
, "");
323 tmp
= LLVMBuildSelect(builder
, is_ma_z
, coords
[2],
324 LLVMBuildSelect(builder
, is_ma_y
, coords
[1], coords
[0], ""), "");
325 sgn
= LLVMBuildSelect(builder
, is_ma_positive
,
326 LLVMConstReal(f32
, 2.0), LLVMConstReal(f32
, -2.0), "");
327 *out_ma
= LLVMBuildFMul(builder
, tmp
, sgn
, "");
331 ac_prepare_cube_coords(struct ac_llvm_context
*ctx
,
332 bool is_deriv
, bool is_array
,
333 LLVMValueRef
*coords_arg
,
334 LLVMValueRef
*derivs_arg
)
337 LLVMBuilderRef builder
= ctx
->builder
;
338 struct cube_selection_coords selcoords
;
339 LLVMValueRef coords
[3];
342 build_cube_intrinsic(ctx
, coords_arg
, &selcoords
);
344 invma
= ac_build_intrinsic(ctx
, "llvm.fabs.f32",
345 ctx
->f32
, &selcoords
.ma
, 1, AC_FUNC_ATTR_READNONE
);
346 invma
= ac_build_fdiv(ctx
, LLVMConstReal(ctx
->f32
, 1.0), invma
);
348 for (int i
= 0; i
< 2; ++i
)
349 coords
[i
] = LLVMBuildFMul(builder
, selcoords
.stc
[i
], invma
, "");
351 coords
[2] = selcoords
.id
;
353 if (is_deriv
&& derivs_arg
) {
354 LLVMValueRef derivs
[4];
357 /* Convert cube derivatives to 2D derivatives. */
358 for (axis
= 0; axis
< 2; axis
++) {
359 LLVMValueRef deriv_st
[2];
360 LLVMValueRef deriv_ma
;
362 /* Transform the derivative alongside the texture
363 * coordinate. Mathematically, the correct formula is
364 * as follows. Assume we're projecting onto the +Z face
365 * and denote by dx/dh the derivative of the (original)
366 * X texture coordinate with respect to horizontal
367 * window coordinates. The projection onto the +Z face
372 * Then df/dh = df/dx * dx/dh + df/dz * dz/dh
373 * = 1/z * dx/dh - x/z * 1/z * dz/dh.
375 * This motivatives the implementation below.
377 * Whether this actually gives the expected results for
378 * apps that might feed in derivatives obtained via
379 * finite differences is anyone's guess. The OpenGL spec
380 * seems awfully quiet about how textureGrad for cube
381 * maps should be handled.
383 build_cube_select(builder
, &selcoords
, &derivs_arg
[axis
* 3],
384 deriv_st
, &deriv_ma
);
386 deriv_ma
= LLVMBuildFMul(builder
, deriv_ma
, invma
, "");
388 for (int i
= 0; i
< 2; ++i
)
389 derivs
[axis
* 2 + i
] =
390 LLVMBuildFSub(builder
,
391 LLVMBuildFMul(builder
, deriv_st
[i
], invma
, ""),
392 LLVMBuildFMul(builder
, deriv_ma
, coords
[i
], ""), "");
395 memcpy(derivs_arg
, derivs
, sizeof(derivs
));
398 /* Shift the texture coordinate. This must be applied after the
399 * derivative calculation.
401 for (int i
= 0; i
< 2; ++i
)
402 coords
[i
] = LLVMBuildFAdd(builder
, coords
[i
], LLVMConstReal(ctx
->f32
, 1.5), "");
405 /* for cube arrays coord.z = coord.w(array_index) * 8 + face */
406 /* coords_arg.w component - array_index for cube arrays */
407 LLVMValueRef tmp
= LLVMBuildFMul(ctx
->builder
, coords_arg
[3], LLVMConstReal(ctx
->f32
, 8.0), "");
408 coords
[2] = LLVMBuildFAdd(ctx
->builder
, tmp
, coords
[2], "");
411 memcpy(coords_arg
, coords
, sizeof(coords
));
416 ac_build_fs_interp(struct ac_llvm_context
*ctx
,
417 LLVMValueRef llvm_chan
,
418 LLVMValueRef attr_number
,
423 LLVMValueRef args
[5];
426 if (HAVE_LLVM
< 0x0400) {
428 ij
[0] = LLVMBuildBitCast(ctx
->builder
, i
, ctx
->i32
, "");
429 ij
[1] = LLVMBuildBitCast(ctx
->builder
, j
, ctx
->i32
, "");
432 args
[1] = attr_number
;
434 args
[3] = ac_build_gather_values(ctx
, ij
, 2);
435 return ac_build_intrinsic(ctx
, "llvm.SI.fs.interp",
437 AC_FUNC_ATTR_READNONE
);
442 args
[2] = attr_number
;
445 p1
= ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p1",
446 ctx
->f32
, args
, 4, AC_FUNC_ATTR_READNONE
);
451 args
[3] = attr_number
;
454 return ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p2",
455 ctx
->f32
, args
, 5, AC_FUNC_ATTR_READNONE
);
459 ac_build_fs_interp_mov(struct ac_llvm_context
*ctx
,
460 LLVMValueRef parameter
,
461 LLVMValueRef llvm_chan
,
462 LLVMValueRef attr_number
,
465 LLVMValueRef args
[4];
466 if (HAVE_LLVM
< 0x0400) {
468 args
[1] = attr_number
;
471 return ac_build_intrinsic(ctx
,
472 "llvm.SI.fs.constant",
474 AC_FUNC_ATTR_READNONE
);
479 args
[2] = attr_number
;
482 return ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.mov",
483 ctx
->f32
, args
, 4, AC_FUNC_ATTR_READNONE
);
487 ac_build_gep0(struct ac_llvm_context
*ctx
,
488 LLVMValueRef base_ptr
,
491 LLVMValueRef indices
[2] = {
492 LLVMConstInt(ctx
->i32
, 0, 0),
495 return LLVMBuildGEP(ctx
->builder
, base_ptr
,
500 ac_build_indexed_store(struct ac_llvm_context
*ctx
,
501 LLVMValueRef base_ptr
, LLVMValueRef index
,
504 LLVMBuildStore(ctx
->builder
, value
,
505 ac_build_gep0(ctx
, base_ptr
, index
));
509 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad.
510 * It's equivalent to doing a load from &base_ptr[index].
512 * \param base_ptr Where the array starts.
513 * \param index The element index into the array.
514 * \param uniform Whether the base_ptr and index can be assumed to be
515 * dynamically uniform
518 ac_build_indexed_load(struct ac_llvm_context
*ctx
,
519 LLVMValueRef base_ptr
, LLVMValueRef index
,
522 LLVMValueRef pointer
;
524 pointer
= ac_build_gep0(ctx
, base_ptr
, index
);
526 LLVMSetMetadata(pointer
, ctx
->uniform_md_kind
, ctx
->empty_md
);
527 return LLVMBuildLoad(ctx
->builder
, pointer
, "");
531 * Do a load from &base_ptr[index], but also add a flag that it's loading
532 * a constant from a dynamically uniform index.
535 ac_build_indexed_load_const(struct ac_llvm_context
*ctx
,
536 LLVMValueRef base_ptr
, LLVMValueRef index
)
538 LLVMValueRef result
= ac_build_indexed_load(ctx
, base_ptr
, index
, true);
539 LLVMSetMetadata(result
, ctx
->invariant_load_md_kind
, ctx
->empty_md
);
543 /* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
544 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
545 * or v4i32 (num_channels=3,4).
548 ac_build_buffer_store_dword(struct ac_llvm_context
*ctx
,
551 unsigned num_channels
,
552 LLVMValueRef voffset
,
553 LLVMValueRef soffset
,
554 unsigned inst_offset
,
557 bool writeonly_memory
,
560 /* TODO: Fix stores with ADD_TID and remove the "has_add_tid" flag. */
561 if (HAVE_LLVM
>= 0x0309 && !has_add_tid
) {
562 /* Split 3 channel stores, becase LLVM doesn't support 3-channel
564 if (num_channels
== 3) {
565 LLVMValueRef v
[3], v01
;
567 for (int i
= 0; i
< 3; i
++) {
568 v
[i
] = LLVMBuildExtractElement(ctx
->builder
, vdata
,
569 LLVMConstInt(ctx
->i32
, i
, 0), "");
571 v01
= ac_build_gather_values(ctx
, v
, 2);
573 ac_build_buffer_store_dword(ctx
, rsrc
, v01
, 2, voffset
,
574 soffset
, inst_offset
, glc
, slc
,
575 writeonly_memory
, has_add_tid
);
576 ac_build_buffer_store_dword(ctx
, rsrc
, v
[2], 1, voffset
,
577 soffset
, inst_offset
+ 8,
579 writeonly_memory
, has_add_tid
);
583 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
584 static const char *types
[] = {"f32", "v2f32", "v4f32"};
586 LLVMValueRef offset
= soffset
;
589 offset
= LLVMBuildAdd(ctx
->builder
, offset
,
590 LLVMConstInt(ctx
->i32
, inst_offset
, 0), "");
592 offset
= LLVMBuildAdd(ctx
->builder
, offset
, voffset
, "");
594 LLVMValueRef args
[] = {
595 bitcast_to_float(ctx
, vdata
),
596 LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, ""),
597 LLVMConstInt(ctx
->i32
, 0, 0),
599 LLVMConstInt(ctx
->i1
, glc
, 0),
600 LLVMConstInt(ctx
->i1
, slc
, 0),
603 snprintf(name
, sizeof(name
), "llvm.amdgcn.buffer.store.%s",
606 ac_build_intrinsic(ctx
, name
, ctx
->voidt
,
607 args
, ARRAY_SIZE(args
),
609 AC_FUNC_ATTR_INACCESSIBLE_MEM_ONLY
:
610 AC_FUNC_ATTR_WRITEONLY
);
614 static unsigned dfmt
[] = {
615 V_008F0C_BUF_DATA_FORMAT_32
,
616 V_008F0C_BUF_DATA_FORMAT_32_32
,
617 V_008F0C_BUF_DATA_FORMAT_32_32_32
,
618 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
620 assert(num_channels
>= 1 && num_channels
<= 4);
622 LLVMValueRef args
[] = {
625 LLVMConstInt(ctx
->i32
, num_channels
, 0),
626 voffset
? voffset
: LLVMGetUndef(ctx
->i32
),
628 LLVMConstInt(ctx
->i32
, inst_offset
, 0),
629 LLVMConstInt(ctx
->i32
, dfmt
[num_channels
- 1], 0),
630 LLVMConstInt(ctx
->i32
, V_008F0C_BUF_NUM_FORMAT_UINT
, 0),
631 LLVMConstInt(ctx
->i32
, voffset
!= NULL
, 0),
632 LLVMConstInt(ctx
->i32
, 0, 0), /* idxen */
633 LLVMConstInt(ctx
->i32
, glc
, 0),
634 LLVMConstInt(ctx
->i32
, slc
, 0),
635 LLVMConstInt(ctx
->i32
, 0, 0), /* tfe*/
638 /* The instruction offset field has 12 bits */
639 assert(voffset
|| inst_offset
< (1 << 12));
641 /* The intrinsic is overloaded, we need to add a type suffix for overloading to work. */
642 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
643 const char *types
[] = {"i32", "v2i32", "v4i32"};
645 snprintf(name
, sizeof(name
), "llvm.SI.tbuffer.store.%s", types
[func
]);
647 ac_build_intrinsic(ctx
, name
, ctx
->voidt
,
648 args
, ARRAY_SIZE(args
),
649 AC_FUNC_ATTR_LEGACY
);
653 ac_build_buffer_load(struct ac_llvm_context
*ctx
,
657 LLVMValueRef voffset
,
658 LLVMValueRef soffset
,
659 unsigned inst_offset
,
662 bool readonly_memory
)
664 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
666 if (HAVE_LLVM
>= 0x309) {
667 LLVMValueRef args
[] = {
668 LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, ""),
669 vindex
? vindex
: LLVMConstInt(ctx
->i32
, 0, 0),
670 LLVMConstInt(ctx
->i32
, inst_offset
, 0),
671 LLVMConstInt(ctx
->i1
, glc
, 0),
672 LLVMConstInt(ctx
->i1
, slc
, 0)
675 LLVMTypeRef types
[] = {ctx
->f32
, LLVMVectorType(ctx
->f32
, 2),
677 const char *type_names
[] = {"f32", "v2f32", "v4f32"};
681 args
[2] = LLVMBuildAdd(ctx
->builder
, args
[2], voffset
,
686 args
[2] = LLVMBuildAdd(ctx
->builder
, args
[2], soffset
,
690 snprintf(name
, sizeof(name
), "llvm.amdgcn.buffer.load.%s",
693 return ac_build_intrinsic(ctx
, name
, types
[func
], args
,
695 /* READNONE means writes can't
696 * affect it, while READONLY means
697 * that writes can affect it. */
698 readonly_memory
&& HAVE_LLVM
>= 0x0400 ?
699 AC_FUNC_ATTR_READNONE
:
700 AC_FUNC_ATTR_READONLY
);
702 LLVMValueRef args
[] = {
703 LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v16i8
, ""),
704 voffset
? voffset
: vindex
,
706 LLVMConstInt(ctx
->i32
, inst_offset
, 0),
707 LLVMConstInt(ctx
->i32
, voffset
? 1 : 0, 0), // offen
708 LLVMConstInt(ctx
->i32
, vindex
? 1 : 0, 0), //idxen
709 LLVMConstInt(ctx
->i32
, glc
, 0),
710 LLVMConstInt(ctx
->i32
, slc
, 0),
711 LLVMConstInt(ctx
->i32
, 0, 0), // TFE
714 LLVMTypeRef types
[] = {ctx
->i32
, LLVMVectorType(ctx
->i32
, 2),
716 const char *type_names
[] = {"i32", "v2i32", "v4i32"};
717 const char *arg_type
= "i32";
720 if (voffset
&& vindex
) {
721 LLVMValueRef vaddr
[] = {vindex
, voffset
};
724 args
[1] = ac_build_gather_values(ctx
, vaddr
, 2);
727 snprintf(name
, sizeof(name
), "llvm.SI.buffer.load.dword.%s.%s",
728 type_names
[func
], arg_type
);
730 return ac_build_intrinsic(ctx
, name
, types
[func
], args
,
731 ARRAY_SIZE(args
), AC_FUNC_ATTR_READONLY
);
735 LLVMValueRef
ac_build_buffer_load_format(struct ac_llvm_context
*ctx
,
738 LLVMValueRef voffset
,
739 bool readonly_memory
)
741 if (HAVE_LLVM
>= 0x0309) {
742 LLVMValueRef args
[] = {
743 LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, ""),
746 LLVMConstInt(ctx
->i1
, 0, 0), /* glc */
747 LLVMConstInt(ctx
->i1
, 0, 0), /* slc */
750 return ac_build_intrinsic(ctx
,
751 "llvm.amdgcn.buffer.load.format.v4f32",
752 ctx
->v4f32
, args
, ARRAY_SIZE(args
),
753 /* READNONE means writes can't
754 * affect it, while READONLY means
755 * that writes can affect it. */
756 readonly_memory
&& HAVE_LLVM
>= 0x0400 ?
757 AC_FUNC_ATTR_READNONE
:
758 AC_FUNC_ATTR_READONLY
);
761 LLVMValueRef args
[] = {
766 return ac_build_intrinsic(ctx
, "llvm.SI.vs.load.input",
768 AC_FUNC_ATTR_READNONE
|
769 AC_FUNC_ATTR_LEGACY
);
773 * Set range metadata on an instruction. This can only be used on load and
774 * call instructions. If you know an instruction can only produce the values
775 * 0, 1, 2, you would do set_range_metadata(value, 0, 3);
776 * \p lo is the minimum value inclusive.
777 * \p hi is the maximum value exclusive.
779 static void set_range_metadata(struct ac_llvm_context
*ctx
,
780 LLVMValueRef value
, unsigned lo
, unsigned hi
)
782 LLVMValueRef range_md
, md_args
[2];
783 LLVMTypeRef type
= LLVMTypeOf(value
);
784 LLVMContextRef context
= LLVMGetTypeContext(type
);
786 md_args
[0] = LLVMConstInt(type
, lo
, false);
787 md_args
[1] = LLVMConstInt(type
, hi
, false);
788 range_md
= LLVMMDNodeInContext(context
, md_args
, 2);
789 LLVMSetMetadata(value
, ctx
->range_md_kind
, range_md
);
793 ac_get_thread_id(struct ac_llvm_context
*ctx
)
797 LLVMValueRef tid_args
[2];
798 tid_args
[0] = LLVMConstInt(ctx
->i32
, 0xffffffff, false);
799 tid_args
[1] = LLVMConstInt(ctx
->i32
, 0, false);
800 tid_args
[1] = ac_build_intrinsic(ctx
,
801 "llvm.amdgcn.mbcnt.lo", ctx
->i32
,
802 tid_args
, 2, AC_FUNC_ATTR_READNONE
);
804 tid
= ac_build_intrinsic(ctx
, "llvm.amdgcn.mbcnt.hi",
806 2, AC_FUNC_ATTR_READNONE
);
807 set_range_metadata(ctx
, tid
, 0, 64);
812 * SI implements derivatives using the local data store (LDS)
813 * All writes to the LDS happen in all executing threads at
814 * the same time. TID is the Thread ID for the current
815 * thread and is a value between 0 and 63, representing
816 * the thread's position in the wavefront.
818 * For the pixel shader threads are grouped into quads of four pixels.
819 * The TIDs of the pixels of a quad are:
827 * So, masking the TID with 0xfffffffc yields the TID of the top left pixel
828 * of the quad, masking with 0xfffffffd yields the TID of the top pixel of
829 * the current pixel's column, and masking with 0xfffffffe yields the TID
830 * of the left pixel of the current pixel's row.
832 * Adding 1 yields the TID of the pixel to the right of the left pixel, and
833 * adding 2 yields the TID of the pixel below the top pixel.
836 ac_build_ddxy(struct ac_llvm_context
*ctx
,
837 bool has_ds_bpermute
,
843 LLVMValueRef thread_id
, tl
, trbl
, tl_tid
, trbl_tid
, args
[2];
846 thread_id
= ac_get_thread_id(ctx
);
848 tl_tid
= LLVMBuildAnd(ctx
->builder
, thread_id
,
849 LLVMConstInt(ctx
->i32
, mask
, false), "");
851 trbl_tid
= LLVMBuildAdd(ctx
->builder
, tl_tid
,
852 LLVMConstInt(ctx
->i32
, idx
, false), "");
854 if (has_ds_bpermute
) {
855 args
[0] = LLVMBuildMul(ctx
->builder
, tl_tid
,
856 LLVMConstInt(ctx
->i32
, 4, false), "");
858 tl
= ac_build_intrinsic(ctx
,
859 "llvm.amdgcn.ds.bpermute", ctx
->i32
,
861 AC_FUNC_ATTR_READNONE
|
862 AC_FUNC_ATTR_CONVERGENT
);
864 args
[0] = LLVMBuildMul(ctx
->builder
, trbl_tid
,
865 LLVMConstInt(ctx
->i32
, 4, false), "");
866 trbl
= ac_build_intrinsic(ctx
,
867 "llvm.amdgcn.ds.bpermute", ctx
->i32
,
869 AC_FUNC_ATTR_READNONE
|
870 AC_FUNC_ATTR_CONVERGENT
);
872 LLVMValueRef store_ptr
, load_ptr0
, load_ptr1
;
874 store_ptr
= ac_build_gep0(ctx
, lds
, thread_id
);
875 load_ptr0
= ac_build_gep0(ctx
, lds
, tl_tid
);
876 load_ptr1
= ac_build_gep0(ctx
, lds
, trbl_tid
);
878 LLVMBuildStore(ctx
->builder
, val
, store_ptr
);
879 tl
= LLVMBuildLoad(ctx
->builder
, load_ptr0
, "");
880 trbl
= LLVMBuildLoad(ctx
->builder
, load_ptr1
, "");
883 tl
= LLVMBuildBitCast(ctx
->builder
, tl
, ctx
->f32
, "");
884 trbl
= LLVMBuildBitCast(ctx
->builder
, trbl
, ctx
->f32
, "");
885 result
= LLVMBuildFSub(ctx
->builder
, trbl
, tl
, "");
890 ac_build_sendmsg(struct ac_llvm_context
*ctx
,
892 LLVMValueRef wave_id
)
894 LLVMValueRef args
[2];
895 const char *intr_name
= (HAVE_LLVM
< 0x0400) ? "llvm.SI.sendmsg" : "llvm.amdgcn.s.sendmsg";
896 args
[0] = LLVMConstInt(ctx
->i32
, msg
, false);
898 ac_build_intrinsic(ctx
, intr_name
, ctx
->voidt
, args
, 2, 0);
902 ac_build_imsb(struct ac_llvm_context
*ctx
,
904 LLVMTypeRef dst_type
)
906 const char *intr_name
= (HAVE_LLVM
< 0x0400) ? "llvm.AMDGPU.flbit.i32" :
907 "llvm.amdgcn.sffbh.i32";
908 LLVMValueRef msb
= ac_build_intrinsic(ctx
, intr_name
,
910 AC_FUNC_ATTR_READNONE
);
912 /* The HW returns the last bit index from MSB, but NIR/TGSI wants
913 * the index from LSB. Invert it by doing "31 - msb". */
914 msb
= LLVMBuildSub(ctx
->builder
, LLVMConstInt(ctx
->i32
, 31, false),
917 LLVMValueRef all_ones
= LLVMConstInt(ctx
->i32
, -1, true);
918 LLVMValueRef cond
= LLVMBuildOr(ctx
->builder
,
919 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
920 arg
, LLVMConstInt(ctx
->i32
, 0, 0), ""),
921 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
922 arg
, all_ones
, ""), "");
924 return LLVMBuildSelect(ctx
->builder
, cond
, all_ones
, msb
, "");
928 ac_build_umsb(struct ac_llvm_context
*ctx
,
930 LLVMTypeRef dst_type
)
932 LLVMValueRef args
[2] = {
934 LLVMConstInt(ctx
->i1
, 1, 0),
936 LLVMValueRef msb
= ac_build_intrinsic(ctx
, "llvm.ctlz.i32",
937 dst_type
, args
, ARRAY_SIZE(args
),
938 AC_FUNC_ATTR_READNONE
);
940 /* The HW returns the last bit index from MSB, but TGSI/NIR wants
941 * the index from LSB. Invert it by doing "31 - msb". */
942 msb
= LLVMBuildSub(ctx
->builder
, LLVMConstInt(ctx
->i32
, 31, false),
946 return LLVMBuildSelect(ctx
->builder
,
947 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, arg
,
948 LLVMConstInt(ctx
->i32
, 0, 0), ""),
949 LLVMConstInt(ctx
->i32
, -1, true), msb
, "");
952 LLVMValueRef
ac_build_clamp(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
954 if (HAVE_LLVM
>= 0x0500) {
955 LLVMValueRef max
[2] = {
957 LLVMConstReal(ctx
->f32
, 0),
959 LLVMValueRef min
[2] = {
960 LLVMConstReal(ctx
->f32
, 1),
963 min
[1] = ac_build_intrinsic(ctx
, "llvm.maxnum.f32",
965 AC_FUNC_ATTR_READNONE
);
966 return ac_build_intrinsic(ctx
, "llvm.minnum.f32",
968 AC_FUNC_ATTR_READNONE
);
971 LLVMValueRef args
[3] = {
973 LLVMConstReal(ctx
->f32
, 0),
974 LLVMConstReal(ctx
->f32
, 1),
977 return ac_build_intrinsic(ctx
, "llvm.AMDGPU.clamp.", ctx
->f32
, args
, 3,
978 AC_FUNC_ATTR_READNONE
|
979 AC_FUNC_ATTR_LEGACY
);
982 void ac_build_export(struct ac_llvm_context
*ctx
, struct ac_export_args
*a
)
984 LLVMValueRef args
[9];
986 if (HAVE_LLVM
>= 0x0500) {
987 args
[0] = LLVMConstInt(ctx
->i32
, a
->target
, 0);
988 args
[1] = LLVMConstInt(ctx
->i32
, a
->enabled_channels
, 0);
991 LLVMTypeRef i16
= LLVMInt16TypeInContext(ctx
->context
);
992 LLVMTypeRef v2i16
= LLVMVectorType(i16
, 2);
994 args
[2] = LLVMBuildBitCast(ctx
->builder
, a
->out
[0],
996 args
[3] = LLVMBuildBitCast(ctx
->builder
, a
->out
[1],
998 args
[4] = LLVMConstInt(ctx
->i1
, a
->done
, 0);
999 args
[5] = LLVMConstInt(ctx
->i1
, a
->valid_mask
, 0);
1001 ac_build_intrinsic(ctx
, "llvm.amdgcn.exp.compr.v2i16",
1002 ctx
->voidt
, args
, 6, 0);
1004 args
[2] = a
->out
[0];
1005 args
[3] = a
->out
[1];
1006 args
[4] = a
->out
[2];
1007 args
[5] = a
->out
[3];
1008 args
[6] = LLVMConstInt(ctx
->i1
, a
->done
, 0);
1009 args
[7] = LLVMConstInt(ctx
->i1
, a
->valid_mask
, 0);
1011 ac_build_intrinsic(ctx
, "llvm.amdgcn.exp.f32",
1012 ctx
->voidt
, args
, 8, 0);
1017 args
[0] = LLVMConstInt(ctx
->i32
, a
->enabled_channels
, 0);
1018 args
[1] = LLVMConstInt(ctx
->i32
, a
->valid_mask
, 0);
1019 args
[2] = LLVMConstInt(ctx
->i32
, a
->done
, 0);
1020 args
[3] = LLVMConstInt(ctx
->i32
, a
->target
, 0);
1021 args
[4] = LLVMConstInt(ctx
->i32
, a
->compr
, 0);
1022 memcpy(args
+ 5, a
->out
, sizeof(a
->out
[0]) * 4);
1024 ac_build_intrinsic(ctx
, "llvm.SI.export", ctx
->voidt
, args
, 9,
1025 AC_FUNC_ATTR_LEGACY
);
1028 LLVMValueRef
ac_build_image_opcode(struct ac_llvm_context
*ctx
,
1029 struct ac_image_args
*a
)
1031 LLVMTypeRef dst_type
;
1032 LLVMValueRef args
[11];
1033 unsigned num_args
= 0;
1035 char intr_name
[128], type
[64];
1037 if (HAVE_LLVM
>= 0x0400) {
1038 bool sample
= a
->opcode
== ac_image_sample
||
1039 a
->opcode
== ac_image_gather4
||
1040 a
->opcode
== ac_image_get_lod
;
1043 args
[num_args
++] = bitcast_to_float(ctx
, a
->addr
);
1045 args
[num_args
++] = a
->addr
;
1047 args
[num_args
++] = a
->resource
;
1049 args
[num_args
++] = a
->sampler
;
1050 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->dmask
, 0);
1052 args
[num_args
++] = LLVMConstInt(ctx
->i1
, a
->unorm
, 0);
1053 args
[num_args
++] = LLVMConstInt(ctx
->i1
, 0, 0); /* glc */
1054 args
[num_args
++] = LLVMConstInt(ctx
->i1
, 0, 0); /* slc */
1055 args
[num_args
++] = LLVMConstInt(ctx
->i1
, 0, 0); /* lwe */
1056 args
[num_args
++] = LLVMConstInt(ctx
->i1
, a
->da
, 0);
1058 switch (a
->opcode
) {
1059 case ac_image_sample
:
1060 name
= "llvm.amdgcn.image.sample";
1062 case ac_image_gather4
:
1063 name
= "llvm.amdgcn.image.gather4";
1066 name
= "llvm.amdgcn.image.load";
1068 case ac_image_load_mip
:
1069 name
= "llvm.amdgcn.image.load.mip";
1071 case ac_image_get_lod
:
1072 name
= "llvm.amdgcn.image.getlod";
1074 case ac_image_get_resinfo
:
1075 name
= "llvm.amdgcn.image.getresinfo";
1078 unreachable("invalid image opcode");
1081 ac_build_type_name_for_intr(LLVMTypeOf(args
[0]), type
,
1084 snprintf(intr_name
, sizeof(intr_name
), "%s%s%s%s.v4f32.%s.v8i32",
1086 a
->compare
? ".c" : "",
1090 a
->level_zero
? ".lz" : "",
1091 a
->offset
? ".o" : "",
1094 LLVMValueRef result
=
1095 ac_build_intrinsic(ctx
, intr_name
,
1096 ctx
->v4f32
, args
, num_args
,
1097 AC_FUNC_ATTR_READNONE
);
1099 result
= LLVMBuildBitCast(ctx
->builder
, result
,
1105 args
[num_args
++] = a
->addr
;
1106 args
[num_args
++] = a
->resource
;
1108 if (a
->opcode
== ac_image_load
||
1109 a
->opcode
== ac_image_load_mip
||
1110 a
->opcode
== ac_image_get_resinfo
) {
1111 dst_type
= ctx
->v4i32
;
1113 dst_type
= ctx
->v4f32
;
1114 args
[num_args
++] = a
->sampler
;
1117 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->dmask
, 0);
1118 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->unorm
, 0);
1119 args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* r128 */
1120 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->da
, 0);
1121 args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* glc */
1122 args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* slc */
1123 args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* tfe */
1124 args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* lwe */
1126 switch (a
->opcode
) {
1127 case ac_image_sample
:
1128 name
= "llvm.SI.image.sample";
1130 case ac_image_gather4
:
1131 name
= "llvm.SI.gather4";
1134 name
= "llvm.SI.image.load";
1136 case ac_image_load_mip
:
1137 name
= "llvm.SI.image.load.mip";
1139 case ac_image_get_lod
:
1140 name
= "llvm.SI.getlod";
1142 case ac_image_get_resinfo
:
1143 name
= "llvm.SI.getresinfo";
1147 ac_build_type_name_for_intr(LLVMTypeOf(a
->addr
), type
, sizeof(type
));
1148 snprintf(intr_name
, sizeof(intr_name
), "%s%s%s%s.%s",
1150 a
->compare
? ".c" : "",
1154 a
->level_zero
? ".lz" : "",
1155 a
->offset
? ".o" : "",
1158 return ac_build_intrinsic(ctx
, intr_name
,
1159 dst_type
, args
, num_args
,
1160 AC_FUNC_ATTR_READNONE
|
1161 AC_FUNC_ATTR_LEGACY
);
1164 LLVMValueRef
ac_build_cvt_pkrtz_f16(struct ac_llvm_context
*ctx
,
1165 LLVMValueRef args
[2])
1167 if (HAVE_LLVM
>= 0x0500) {
1169 LLVMVectorType(LLVMHalfTypeInContext(ctx
->context
), 2);
1171 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pkrtz",
1173 AC_FUNC_ATTR_READNONE
);
1174 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
1177 return ac_build_intrinsic(ctx
, "llvm.SI.packf16", ctx
->i32
, args
, 2,
1178 AC_FUNC_ATTR_READNONE
|
1179 AC_FUNC_ATTR_LEGACY
);
1183 * KILL, AKA discard in GLSL.
1185 * \param value kill if value < 0.0 or value == NULL.
1187 void ac_build_kill(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
1190 ac_build_intrinsic(ctx
, "llvm.AMDGPU.kill", ctx
->voidt
,
1191 &value
, 1, AC_FUNC_ATTR_LEGACY
);
1193 ac_build_intrinsic(ctx
, "llvm.AMDGPU.kilp", ctx
->voidt
,
1194 NULL
, 0, AC_FUNC_ATTR_LEGACY
);
1198 LLVMValueRef
ac_build_bfe(struct ac_llvm_context
*ctx
, LLVMValueRef input
,
1199 LLVMValueRef offset
, LLVMValueRef width
,
1202 LLVMValueRef args
[] = {
1208 if (HAVE_LLVM
>= 0x0500) {
1209 return ac_build_intrinsic(ctx
,
1210 is_signed
? "llvm.amdgcn.sbfe.i32" :
1211 "llvm.amdgcn.ubfe.i32",
1213 AC_FUNC_ATTR_READNONE
);
1216 return ac_build_intrinsic(ctx
,
1217 is_signed
? "llvm.AMDGPU.bfe.i32" :
1218 "llvm.AMDGPU.bfe.u32",
1220 AC_FUNC_ATTR_READNONE
|
1221 AC_FUNC_ATTR_LEGACY
);
1224 void ac_get_image_intr_name(const char *base_name
,
1225 LLVMTypeRef data_type
,
1226 LLVMTypeRef coords_type
,
1227 LLVMTypeRef rsrc_type
,
1228 char *out_name
, unsigned out_len
)
1230 char coords_type_name
[8];
1232 ac_build_type_name_for_intr(coords_type
, coords_type_name
,
1233 sizeof(coords_type_name
));
1235 if (HAVE_LLVM
<= 0x0309) {
1236 snprintf(out_name
, out_len
, "%s.%s", base_name
, coords_type_name
);
1238 char data_type_name
[8];
1239 char rsrc_type_name
[8];
1241 ac_build_type_name_for_intr(data_type
, data_type_name
,
1242 sizeof(data_type_name
));
1243 ac_build_type_name_for_intr(rsrc_type
, rsrc_type_name
,
1244 sizeof(rsrc_type_name
));
1245 snprintf(out_name
, out_len
, "%s.%s.%s.%s", base_name
,
1246 data_type_name
, coords_type_name
, rsrc_type_name
);
1250 #define AC_EXP_TARGET (HAVE_LLVM >= 0x0500 ? 0 : 3)
1251 #define AC_EXP_OUT0 (HAVE_LLVM >= 0x0500 ? 2 : 5)
1259 struct ac_vs_exp_chan
1263 enum ac_ir_type type
;
1266 struct ac_vs_exp_inst
{
1269 struct ac_vs_exp_chan chan
[4];
1272 struct ac_vs_exports
{
1274 struct ac_vs_exp_inst exp
[VARYING_SLOT_MAX
];
1277 /* Return true if the PARAM export has been eliminated. */
1278 static bool ac_eliminate_const_output(uint8_t *vs_output_param_offset
,
1279 uint32_t num_outputs
,
1280 struct ac_vs_exp_inst
*exp
)
1282 unsigned i
, default_val
; /* SPI_PS_INPUT_CNTL_i.DEFAULT_VAL */
1283 bool is_zero
[4] = {}, is_one
[4] = {};
1285 for (i
= 0; i
< 4; i
++) {
1286 /* It's a constant expression. Undef outputs are eliminated too. */
1287 if (exp
->chan
[i
].type
== AC_IR_UNDEF
) {
1290 } else if (exp
->chan
[i
].type
== AC_IR_CONST
) {
1291 if (exp
->chan
[i
].const_float
== 0)
1293 else if (exp
->chan
[i
].const_float
== 1)
1296 return false; /* other constant */
1301 /* Only certain combinations of 0 and 1 can be eliminated. */
1302 if (is_zero
[0] && is_zero
[1] && is_zero
[2])
1303 default_val
= is_zero
[3] ? 0 : 1;
1304 else if (is_one
[0] && is_one
[1] && is_one
[2])
1305 default_val
= is_zero
[3] ? 2 : 3;
1309 /* The PARAM export can be represented as DEFAULT_VAL. Kill it. */
1310 LLVMInstructionEraseFromParent(exp
->inst
);
1312 /* Change OFFSET to DEFAULT_VAL. */
1313 for (i
= 0; i
< num_outputs
; i
++) {
1314 if (vs_output_param_offset
[i
] == exp
->offset
) {
1315 vs_output_param_offset
[i
] =
1316 AC_EXP_PARAM_DEFAULT_VAL_0000
+ default_val
;
1323 void ac_optimize_vs_outputs(struct ac_llvm_context
*ctx
,
1324 LLVMValueRef main_fn
,
1325 uint8_t *vs_output_param_offset
,
1326 uint32_t num_outputs
,
1327 uint8_t *num_param_exports
)
1329 LLVMBasicBlockRef bb
;
1330 bool removed_any
= false;
1331 struct ac_vs_exports exports
;
1335 /* Process all LLVM instructions. */
1336 bb
= LLVMGetFirstBasicBlock(main_fn
);
1338 LLVMValueRef inst
= LLVMGetFirstInstruction(bb
);
1341 LLVMValueRef cur
= inst
;
1342 inst
= LLVMGetNextInstruction(inst
);
1343 struct ac_vs_exp_inst exp
;
1345 if (LLVMGetInstructionOpcode(cur
) != LLVMCall
)
1348 LLVMValueRef callee
= ac_llvm_get_called_value(cur
);
1350 if (!ac_llvm_is_function(callee
))
1353 const char *name
= LLVMGetValueName(callee
);
1354 unsigned num_args
= LLVMCountParams(callee
);
1356 /* Check if this is an export instruction. */
1357 if ((num_args
!= 9 && num_args
!= 8) ||
1358 (strcmp(name
, "llvm.SI.export") &&
1359 strcmp(name
, "llvm.amdgcn.exp.f32")))
1362 LLVMValueRef arg
= LLVMGetOperand(cur
, AC_EXP_TARGET
);
1363 unsigned target
= LLVMConstIntGetZExtValue(arg
);
1365 if (target
< V_008DFC_SQ_EXP_PARAM
)
1368 target
-= V_008DFC_SQ_EXP_PARAM
;
1370 /* Parse the instruction. */
1371 memset(&exp
, 0, sizeof(exp
));
1372 exp
.offset
= target
;
1375 for (unsigned i
= 0; i
< 4; i
++) {
1376 LLVMValueRef v
= LLVMGetOperand(cur
, AC_EXP_OUT0
+ i
);
1378 exp
.chan
[i
].value
= v
;
1380 if (LLVMIsUndef(v
)) {
1381 exp
.chan
[i
].type
= AC_IR_UNDEF
;
1382 } else if (LLVMIsAConstantFP(v
)) {
1383 LLVMBool loses_info
;
1384 exp
.chan
[i
].type
= AC_IR_CONST
;
1385 exp
.chan
[i
].const_float
=
1386 LLVMConstRealGetDouble(v
, &loses_info
);
1388 exp
.chan
[i
].type
= AC_IR_VALUE
;
1392 /* Eliminate constant value PARAM exports. */
1393 if (ac_eliminate_const_output(vs_output_param_offset
,
1394 num_outputs
, &exp
)) {
1397 exports
.exp
[exports
.num
++] = exp
;
1400 bb
= LLVMGetNextBasicBlock(bb
);
1403 /* Remove holes in export memory due to removed PARAM exports.
1404 * This is done by renumbering all PARAM exports.
1407 uint8_t current_offset
[VARYING_SLOT_MAX
];
1408 unsigned new_count
= 0;
1411 /* Make a copy of the offsets. We need the old version while
1412 * we are modifying some of them. */
1413 memcpy(current_offset
, vs_output_param_offset
,
1414 sizeof(current_offset
));
1416 for (i
= 0; i
< exports
.num
; i
++) {
1417 unsigned offset
= exports
.exp
[i
].offset
;
1419 for (out
= 0; out
< num_outputs
; out
++) {
1420 if (current_offset
[out
] != offset
)
1423 LLVMSetOperand(exports
.exp
[i
].inst
, AC_EXP_TARGET
,
1424 LLVMConstInt(ctx
->i32
,
1425 V_008DFC_SQ_EXP_PARAM
+ new_count
, 0));
1426 vs_output_param_offset
[out
] = new_count
;
1431 *num_param_exports
= new_count
;