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
,
187 LLVMBuilderRef builder
= ctx
->builder
;
188 LLVMValueRef vec
= NULL
;
191 if (value_count
== 1 && !always_vector
) {
193 return LLVMBuildLoad(builder
, values
[0], "");
195 } else if (!value_count
)
196 unreachable("value_count is 0");
198 for (i
= 0; i
< value_count
; i
++) {
199 LLVMValueRef value
= values
[i
* value_stride
];
201 value
= LLVMBuildLoad(builder
, value
, "");
204 vec
= LLVMGetUndef( LLVMVectorType(LLVMTypeOf(value
), value_count
));
205 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, i
, false);
206 vec
= LLVMBuildInsertElement(builder
, vec
, value
, index
, "");
212 ac_build_gather_values(struct ac_llvm_context
*ctx
,
213 LLVMValueRef
*values
,
214 unsigned value_count
)
216 return ac_build_gather_values_extended(ctx
, values
, value_count
, 1, false, false);
220 ac_build_fdiv(struct ac_llvm_context
*ctx
,
224 LLVMValueRef ret
= LLVMBuildFDiv(ctx
->builder
, num
, den
, "");
226 if (!LLVMIsConstant(ret
))
227 LLVMSetMetadata(ret
, ctx
->fpmath_md_kind
, ctx
->fpmath_md_2p5_ulp
);
231 /* Coordinates for cube map selection. sc, tc, and ma are as in Table 8.27
232 * of the OpenGL 4.5 (Compatibility Profile) specification, except ma is
233 * already multiplied by two. id is the cube face number.
235 struct cube_selection_coords
{
242 build_cube_intrinsic(struct ac_llvm_context
*ctx
,
244 struct cube_selection_coords
*out
)
246 LLVMTypeRef f32
= ctx
->f32
;
248 out
->stc
[1] = ac_build_intrinsic(ctx
, "llvm.amdgcn.cubetc",
249 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
250 out
->stc
[0] = ac_build_intrinsic(ctx
, "llvm.amdgcn.cubesc",
251 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
252 out
->ma
= ac_build_intrinsic(ctx
, "llvm.amdgcn.cubema",
253 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
254 out
->id
= ac_build_intrinsic(ctx
, "llvm.amdgcn.cubeid",
255 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
259 * Build a manual selection sequence for cube face sc/tc coordinates and
260 * major axis vector (multiplied by 2 for consistency) for the given
261 * vec3 \p coords, for the face implied by \p selcoords.
263 * For the major axis, we always adjust the sign to be in the direction of
264 * selcoords.ma; i.e., a positive out_ma means that coords is pointed towards
265 * the selcoords major axis.
267 static void build_cube_select(LLVMBuilderRef builder
,
268 const struct cube_selection_coords
*selcoords
,
269 const LLVMValueRef
*coords
,
270 LLVMValueRef
*out_st
,
271 LLVMValueRef
*out_ma
)
273 LLVMTypeRef f32
= LLVMTypeOf(coords
[0]);
274 LLVMValueRef is_ma_positive
;
276 LLVMValueRef is_ma_z
, is_not_ma_z
;
277 LLVMValueRef is_ma_y
;
278 LLVMValueRef is_ma_x
;
282 is_ma_positive
= LLVMBuildFCmp(builder
, LLVMRealUGE
,
283 selcoords
->ma
, LLVMConstReal(f32
, 0.0), "");
284 sgn_ma
= LLVMBuildSelect(builder
, is_ma_positive
,
285 LLVMConstReal(f32
, 1.0), LLVMConstReal(f32
, -1.0), "");
287 is_ma_z
= LLVMBuildFCmp(builder
, LLVMRealUGE
, selcoords
->id
, LLVMConstReal(f32
, 4.0), "");
288 is_not_ma_z
= LLVMBuildNot(builder
, is_ma_z
, "");
289 is_ma_y
= LLVMBuildAnd(builder
, is_not_ma_z
,
290 LLVMBuildFCmp(builder
, LLVMRealUGE
, selcoords
->id
, LLVMConstReal(f32
, 2.0), ""), "");
291 is_ma_x
= LLVMBuildAnd(builder
, is_not_ma_z
, LLVMBuildNot(builder
, is_ma_y
, ""), "");
294 tmp
= LLVMBuildSelect(builder
, is_ma_z
, coords
[2], coords
[0], "");
295 sgn
= LLVMBuildSelect(builder
, is_ma_y
, LLVMConstReal(f32
, 1.0),
296 LLVMBuildSelect(builder
, is_ma_x
, sgn_ma
,
297 LLVMBuildFNeg(builder
, sgn_ma
, ""), ""), "");
298 out_st
[0] = LLVMBuildFMul(builder
, tmp
, sgn
, "");
301 tmp
= LLVMBuildSelect(builder
, is_ma_y
, coords
[2], coords
[1], "");
302 sgn
= LLVMBuildSelect(builder
, is_ma_y
, LLVMBuildFNeg(builder
, sgn_ma
, ""),
303 LLVMConstReal(f32
, -1.0), "");
304 out_st
[1] = LLVMBuildFMul(builder
, tmp
, sgn
, "");
307 tmp
= LLVMBuildSelect(builder
, is_ma_z
, coords
[2],
308 LLVMBuildSelect(builder
, is_ma_y
, coords
[1], coords
[0], ""), "");
309 sgn
= LLVMBuildSelect(builder
, is_ma_positive
,
310 LLVMConstReal(f32
, 2.0), LLVMConstReal(f32
, -2.0), "");
311 *out_ma
= LLVMBuildFMul(builder
, tmp
, sgn
, "");
315 ac_prepare_cube_coords(struct ac_llvm_context
*ctx
,
316 bool is_deriv
, bool is_array
,
317 LLVMValueRef
*coords_arg
,
318 LLVMValueRef
*derivs_arg
)
321 LLVMBuilderRef builder
= ctx
->builder
;
322 struct cube_selection_coords selcoords
;
323 LLVMValueRef coords
[3];
326 build_cube_intrinsic(ctx
, coords_arg
, &selcoords
);
328 invma
= ac_build_intrinsic(ctx
, "llvm.fabs.f32",
329 ctx
->f32
, &selcoords
.ma
, 1, AC_FUNC_ATTR_READNONE
);
330 invma
= ac_build_fdiv(ctx
, LLVMConstReal(ctx
->f32
, 1.0), invma
);
332 for (int i
= 0; i
< 2; ++i
)
333 coords
[i
] = LLVMBuildFMul(builder
, selcoords
.stc
[i
], invma
, "");
335 coords
[2] = selcoords
.id
;
337 if (is_deriv
&& derivs_arg
) {
338 LLVMValueRef derivs
[4];
341 /* Convert cube derivatives to 2D derivatives. */
342 for (axis
= 0; axis
< 2; axis
++) {
343 LLVMValueRef deriv_st
[2];
344 LLVMValueRef deriv_ma
;
346 /* Transform the derivative alongside the texture
347 * coordinate. Mathematically, the correct formula is
348 * as follows. Assume we're projecting onto the +Z face
349 * and denote by dx/dh the derivative of the (original)
350 * X texture coordinate with respect to horizontal
351 * window coordinates. The projection onto the +Z face
356 * Then df/dh = df/dx * dx/dh + df/dz * dz/dh
357 * = 1/z * dx/dh - x/z * 1/z * dz/dh.
359 * This motivatives the implementation below.
361 * Whether this actually gives the expected results for
362 * apps that might feed in derivatives obtained via
363 * finite differences is anyone's guess. The OpenGL spec
364 * seems awfully quiet about how textureGrad for cube
365 * maps should be handled.
367 build_cube_select(builder
, &selcoords
, &derivs_arg
[axis
* 3],
368 deriv_st
, &deriv_ma
);
370 deriv_ma
= LLVMBuildFMul(builder
, deriv_ma
, invma
, "");
372 for (int i
= 0; i
< 2; ++i
)
373 derivs
[axis
* 2 + i
] =
374 LLVMBuildFSub(builder
,
375 LLVMBuildFMul(builder
, deriv_st
[i
], invma
, ""),
376 LLVMBuildFMul(builder
, deriv_ma
, coords
[i
], ""), "");
379 memcpy(derivs_arg
, derivs
, sizeof(derivs
));
382 /* Shift the texture coordinate. This must be applied after the
383 * derivative calculation.
385 for (int i
= 0; i
< 2; ++i
)
386 coords
[i
] = LLVMBuildFAdd(builder
, coords
[i
], LLVMConstReal(ctx
->f32
, 1.5), "");
389 /* for cube arrays coord.z = coord.w(array_index) * 8 + face */
390 /* coords_arg.w component - array_index for cube arrays */
391 LLVMValueRef tmp
= LLVMBuildFMul(ctx
->builder
, coords_arg
[3], LLVMConstReal(ctx
->f32
, 8.0), "");
392 coords
[2] = LLVMBuildFAdd(ctx
->builder
, tmp
, coords
[2], "");
395 memcpy(coords_arg
, coords
, sizeof(coords
));
400 ac_build_fs_interp(struct ac_llvm_context
*ctx
,
401 LLVMValueRef llvm_chan
,
402 LLVMValueRef attr_number
,
407 LLVMValueRef args
[5];
410 if (HAVE_LLVM
< 0x0400) {
412 ij
[0] = LLVMBuildBitCast(ctx
->builder
, i
, ctx
->i32
, "");
413 ij
[1] = LLVMBuildBitCast(ctx
->builder
, j
, ctx
->i32
, "");
416 args
[1] = attr_number
;
418 args
[3] = ac_build_gather_values(ctx
, ij
, 2);
419 return ac_build_intrinsic(ctx
, "llvm.SI.fs.interp",
421 AC_FUNC_ATTR_READNONE
);
426 args
[2] = attr_number
;
429 p1
= ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p1",
430 ctx
->f32
, args
, 4, AC_FUNC_ATTR_READNONE
);
435 args
[3] = attr_number
;
438 return ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p2",
439 ctx
->f32
, args
, 5, AC_FUNC_ATTR_READNONE
);
443 ac_build_fs_interp_mov(struct ac_llvm_context
*ctx
,
444 LLVMValueRef parameter
,
445 LLVMValueRef llvm_chan
,
446 LLVMValueRef attr_number
,
449 LLVMValueRef args
[4];
450 if (HAVE_LLVM
< 0x0400) {
452 args
[1] = attr_number
;
455 return ac_build_intrinsic(ctx
,
456 "llvm.SI.fs.constant",
458 AC_FUNC_ATTR_READNONE
);
463 args
[2] = attr_number
;
466 return ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.mov",
467 ctx
->f32
, args
, 4, AC_FUNC_ATTR_READNONE
);
471 ac_build_gep0(struct ac_llvm_context
*ctx
,
472 LLVMValueRef base_ptr
,
475 LLVMValueRef indices
[2] = {
476 LLVMConstInt(ctx
->i32
, 0, 0),
479 return LLVMBuildGEP(ctx
->builder
, base_ptr
,
484 ac_build_indexed_store(struct ac_llvm_context
*ctx
,
485 LLVMValueRef base_ptr
, LLVMValueRef index
,
488 LLVMBuildStore(ctx
->builder
, value
,
489 ac_build_gep0(ctx
, base_ptr
, index
));
493 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad.
494 * It's equivalent to doing a load from &base_ptr[index].
496 * \param base_ptr Where the array starts.
497 * \param index The element index into the array.
498 * \param uniform Whether the base_ptr and index can be assumed to be
499 * dynamically uniform
502 ac_build_indexed_load(struct ac_llvm_context
*ctx
,
503 LLVMValueRef base_ptr
, LLVMValueRef index
,
506 LLVMValueRef pointer
;
508 pointer
= ac_build_gep0(ctx
, base_ptr
, index
);
510 LLVMSetMetadata(pointer
, ctx
->uniform_md_kind
, ctx
->empty_md
);
511 return LLVMBuildLoad(ctx
->builder
, pointer
, "");
515 * Do a load from &base_ptr[index], but also add a flag that it's loading
516 * a constant from a dynamically uniform index.
519 ac_build_indexed_load_const(struct ac_llvm_context
*ctx
,
520 LLVMValueRef base_ptr
, LLVMValueRef index
)
522 LLVMValueRef result
= ac_build_indexed_load(ctx
, base_ptr
, index
, true);
523 LLVMSetMetadata(result
, ctx
->invariant_load_md_kind
, ctx
->empty_md
);
527 /* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
528 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
529 * or v4i32 (num_channels=3,4).
532 ac_build_buffer_store_dword(struct ac_llvm_context
*ctx
,
535 unsigned num_channels
,
536 LLVMValueRef voffset
,
537 LLVMValueRef soffset
,
538 unsigned inst_offset
,
541 bool writeonly_memory
,
544 /* TODO: Fix stores with ADD_TID and remove the "has_add_tid" flag. */
546 /* Split 3 channel stores, becase LLVM doesn't support 3-channel
548 if (num_channels
== 3) {
549 LLVMValueRef v
[3], v01
;
551 for (int i
= 0; i
< 3; i
++) {
552 v
[i
] = LLVMBuildExtractElement(ctx
->builder
, vdata
,
553 LLVMConstInt(ctx
->i32
, i
, 0), "");
555 v01
= ac_build_gather_values(ctx
, v
, 2);
557 ac_build_buffer_store_dword(ctx
, rsrc
, v01
, 2, voffset
,
558 soffset
, inst_offset
, glc
, slc
,
559 writeonly_memory
, has_add_tid
);
560 ac_build_buffer_store_dword(ctx
, rsrc
, v
[2], 1, voffset
,
561 soffset
, inst_offset
+ 8,
563 writeonly_memory
, has_add_tid
);
567 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
568 static const char *types
[] = {"f32", "v2f32", "v4f32"};
570 LLVMValueRef offset
= soffset
;
573 offset
= LLVMBuildAdd(ctx
->builder
, offset
,
574 LLVMConstInt(ctx
->i32
, inst_offset
, 0), "");
576 offset
= LLVMBuildAdd(ctx
->builder
, offset
, voffset
, "");
578 LLVMValueRef args
[] = {
579 bitcast_to_float(ctx
, vdata
),
580 LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, ""),
581 LLVMConstInt(ctx
->i32
, 0, 0),
583 LLVMConstInt(ctx
->i1
, glc
, 0),
584 LLVMConstInt(ctx
->i1
, slc
, 0),
587 snprintf(name
, sizeof(name
), "llvm.amdgcn.buffer.store.%s",
590 ac_build_intrinsic(ctx
, name
, ctx
->voidt
,
591 args
, ARRAY_SIZE(args
),
593 AC_FUNC_ATTR_INACCESSIBLE_MEM_ONLY
:
594 AC_FUNC_ATTR_WRITEONLY
);
598 static unsigned dfmt
[] = {
599 V_008F0C_BUF_DATA_FORMAT_32
,
600 V_008F0C_BUF_DATA_FORMAT_32_32
,
601 V_008F0C_BUF_DATA_FORMAT_32_32_32
,
602 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
604 assert(num_channels
>= 1 && num_channels
<= 4);
606 LLVMValueRef args
[] = {
609 LLVMConstInt(ctx
->i32
, num_channels
, 0),
610 voffset
? voffset
: LLVMGetUndef(ctx
->i32
),
612 LLVMConstInt(ctx
->i32
, inst_offset
, 0),
613 LLVMConstInt(ctx
->i32
, dfmt
[num_channels
- 1], 0),
614 LLVMConstInt(ctx
->i32
, V_008F0C_BUF_NUM_FORMAT_UINT
, 0),
615 LLVMConstInt(ctx
->i32
, voffset
!= NULL
, 0),
616 LLVMConstInt(ctx
->i32
, 0, 0), /* idxen */
617 LLVMConstInt(ctx
->i32
, glc
, 0),
618 LLVMConstInt(ctx
->i32
, slc
, 0),
619 LLVMConstInt(ctx
->i32
, 0, 0), /* tfe*/
622 /* The instruction offset field has 12 bits */
623 assert(voffset
|| inst_offset
< (1 << 12));
625 /* The intrinsic is overloaded, we need to add a type suffix for overloading to work. */
626 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
627 const char *types
[] = {"i32", "v2i32", "v4i32"};
629 snprintf(name
, sizeof(name
), "llvm.SI.tbuffer.store.%s", types
[func
]);
631 ac_build_intrinsic(ctx
, name
, ctx
->voidt
,
632 args
, ARRAY_SIZE(args
),
633 AC_FUNC_ATTR_LEGACY
);
637 ac_build_buffer_load(struct ac_llvm_context
*ctx
,
641 LLVMValueRef voffset
,
642 LLVMValueRef soffset
,
643 unsigned inst_offset
,
649 LLVMValueRef offset
= LLVMConstInt(ctx
->i32
, inst_offset
, 0);
651 offset
= LLVMBuildAdd(ctx
->builder
, offset
, voffset
, "");
653 offset
= LLVMBuildAdd(ctx
->builder
, offset
, soffset
, "");
655 /* TODO: VI and later generations can use SMEM with GLC=1.*/
656 if (allow_smem
&& !glc
&& !slc
) {
657 assert(vindex
== NULL
);
659 LLVMValueRef result
[4];
661 for (int i
= 0; i
< num_channels
; i
++) {
663 offset
= LLVMBuildAdd(ctx
->builder
, offset
,
664 LLVMConstInt(ctx
->i32
, 4, 0), "");
666 LLVMValueRef args
[2] = {rsrc
, offset
};
667 result
[i
] = ac_build_intrinsic(ctx
, "llvm.SI.load.const.v4i32",
669 AC_FUNC_ATTR_READNONE
|
670 AC_FUNC_ATTR_LEGACY
);
672 if (num_channels
== 1)
675 if (num_channels
== 3)
676 result
[num_channels
++] = LLVMGetUndef(ctx
->f32
);
677 return ac_build_gather_values(ctx
, result
, num_channels
);
680 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
682 LLVMValueRef args
[] = {
683 LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, ""),
684 vindex
? vindex
: LLVMConstInt(ctx
->i32
, 0, 0),
686 LLVMConstInt(ctx
->i1
, glc
, 0),
687 LLVMConstInt(ctx
->i1
, slc
, 0)
690 LLVMTypeRef types
[] = {ctx
->f32
, LLVMVectorType(ctx
->f32
, 2),
692 const char *type_names
[] = {"f32", "v2f32", "v4f32"};
695 snprintf(name
, sizeof(name
), "llvm.amdgcn.buffer.load.%s",
698 return ac_build_intrinsic(ctx
, name
, types
[func
], args
,
700 /* READNONE means writes can't affect it, while
701 * READONLY means that writes can affect it. */
702 can_speculate
&& HAVE_LLVM
>= 0x0400 ?
703 AC_FUNC_ATTR_READNONE
:
704 AC_FUNC_ATTR_READONLY
);
707 LLVMValueRef
ac_build_buffer_load_format(struct ac_llvm_context
*ctx
,
710 LLVMValueRef voffset
,
713 LLVMValueRef args
[] = {
714 LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, ""),
717 LLVMConstInt(ctx
->i1
, 0, 0), /* glc */
718 LLVMConstInt(ctx
->i1
, 0, 0), /* slc */
721 return ac_build_intrinsic(ctx
,
722 "llvm.amdgcn.buffer.load.format.v4f32",
723 ctx
->v4f32
, args
, ARRAY_SIZE(args
),
724 /* READNONE means writes can't affect it, while
725 * READONLY means that writes can affect it. */
726 can_speculate
&& HAVE_LLVM
>= 0x0400 ?
727 AC_FUNC_ATTR_READNONE
:
728 AC_FUNC_ATTR_READONLY
);
732 * Set range metadata on an instruction. This can only be used on load and
733 * call instructions. If you know an instruction can only produce the values
734 * 0, 1, 2, you would do set_range_metadata(value, 0, 3);
735 * \p lo is the minimum value inclusive.
736 * \p hi is the maximum value exclusive.
738 static void set_range_metadata(struct ac_llvm_context
*ctx
,
739 LLVMValueRef value
, unsigned lo
, unsigned hi
)
741 LLVMValueRef range_md
, md_args
[2];
742 LLVMTypeRef type
= LLVMTypeOf(value
);
743 LLVMContextRef context
= LLVMGetTypeContext(type
);
745 md_args
[0] = LLVMConstInt(type
, lo
, false);
746 md_args
[1] = LLVMConstInt(type
, hi
, false);
747 range_md
= LLVMMDNodeInContext(context
, md_args
, 2);
748 LLVMSetMetadata(value
, ctx
->range_md_kind
, range_md
);
752 ac_get_thread_id(struct ac_llvm_context
*ctx
)
756 LLVMValueRef tid_args
[2];
757 tid_args
[0] = LLVMConstInt(ctx
->i32
, 0xffffffff, false);
758 tid_args
[1] = LLVMConstInt(ctx
->i32
, 0, false);
759 tid_args
[1] = ac_build_intrinsic(ctx
,
760 "llvm.amdgcn.mbcnt.lo", ctx
->i32
,
761 tid_args
, 2, AC_FUNC_ATTR_READNONE
);
763 tid
= ac_build_intrinsic(ctx
, "llvm.amdgcn.mbcnt.hi",
765 2, AC_FUNC_ATTR_READNONE
);
766 set_range_metadata(ctx
, tid
, 0, 64);
771 * SI implements derivatives using the local data store (LDS)
772 * All writes to the LDS happen in all executing threads at
773 * the same time. TID is the Thread ID for the current
774 * thread and is a value between 0 and 63, representing
775 * the thread's position in the wavefront.
777 * For the pixel shader threads are grouped into quads of four pixels.
778 * The TIDs of the pixels of a quad are:
786 * So, masking the TID with 0xfffffffc yields the TID of the top left pixel
787 * of the quad, masking with 0xfffffffd yields the TID of the top pixel of
788 * the current pixel's column, and masking with 0xfffffffe yields the TID
789 * of the left pixel of the current pixel's row.
791 * Adding 1 yields the TID of the pixel to the right of the left pixel, and
792 * adding 2 yields the TID of the pixel below the top pixel.
795 ac_build_ddxy(struct ac_llvm_context
*ctx
,
796 bool has_ds_bpermute
,
801 LLVMValueRef tl
, trbl
, args
[2];
804 if (has_ds_bpermute
) {
805 LLVMValueRef thread_id
, tl_tid
, trbl_tid
;
806 thread_id
= ac_get_thread_id(ctx
);
808 tl_tid
= LLVMBuildAnd(ctx
->builder
, thread_id
,
809 LLVMConstInt(ctx
->i32
, mask
, false), "");
811 trbl_tid
= LLVMBuildAdd(ctx
->builder
, tl_tid
,
812 LLVMConstInt(ctx
->i32
, idx
, false), "");
814 args
[0] = LLVMBuildMul(ctx
->builder
, tl_tid
,
815 LLVMConstInt(ctx
->i32
, 4, false), "");
817 tl
= ac_build_intrinsic(ctx
,
818 "llvm.amdgcn.ds.bpermute", ctx
->i32
,
820 AC_FUNC_ATTR_READNONE
|
821 AC_FUNC_ATTR_CONVERGENT
);
823 args
[0] = LLVMBuildMul(ctx
->builder
, trbl_tid
,
824 LLVMConstInt(ctx
->i32
, 4, false), "");
825 trbl
= ac_build_intrinsic(ctx
,
826 "llvm.amdgcn.ds.bpermute", ctx
->i32
,
828 AC_FUNC_ATTR_READNONE
|
829 AC_FUNC_ATTR_CONVERGENT
);
834 case AC_TID_MASK_TOP_LEFT
:
842 case AC_TID_MASK_TOP
:
846 case AC_TID_MASK_LEFT
:
853 args
[1] = LLVMConstInt(ctx
->i32
, masks
[0], false);
855 tl
= ac_build_intrinsic(ctx
,
856 "llvm.amdgcn.ds.swizzle", ctx
->i32
,
858 AC_FUNC_ATTR_READNONE
|
859 AC_FUNC_ATTR_CONVERGENT
);
861 args
[1] = LLVMConstInt(ctx
->i32
, masks
[1], false);
862 trbl
= ac_build_intrinsic(ctx
,
863 "llvm.amdgcn.ds.swizzle", ctx
->i32
,
865 AC_FUNC_ATTR_READNONE
|
866 AC_FUNC_ATTR_CONVERGENT
);
869 tl
= LLVMBuildBitCast(ctx
->builder
, tl
, ctx
->f32
, "");
870 trbl
= LLVMBuildBitCast(ctx
->builder
, trbl
, ctx
->f32
, "");
871 result
= LLVMBuildFSub(ctx
->builder
, trbl
, tl
, "");
876 ac_build_sendmsg(struct ac_llvm_context
*ctx
,
878 LLVMValueRef wave_id
)
880 LLVMValueRef args
[2];
881 const char *intr_name
= (HAVE_LLVM
< 0x0400) ? "llvm.SI.sendmsg" : "llvm.amdgcn.s.sendmsg";
882 args
[0] = LLVMConstInt(ctx
->i32
, msg
, false);
884 ac_build_intrinsic(ctx
, intr_name
, ctx
->voidt
, args
, 2, 0);
888 ac_build_imsb(struct ac_llvm_context
*ctx
,
890 LLVMTypeRef dst_type
)
892 const char *intr_name
= (HAVE_LLVM
< 0x0400) ? "llvm.AMDGPU.flbit.i32" :
893 "llvm.amdgcn.sffbh.i32";
894 LLVMValueRef msb
= ac_build_intrinsic(ctx
, intr_name
,
896 AC_FUNC_ATTR_READNONE
);
898 /* The HW returns the last bit index from MSB, but NIR/TGSI wants
899 * the index from LSB. Invert it by doing "31 - msb". */
900 msb
= LLVMBuildSub(ctx
->builder
, LLVMConstInt(ctx
->i32
, 31, false),
903 LLVMValueRef all_ones
= LLVMConstInt(ctx
->i32
, -1, true);
904 LLVMValueRef cond
= LLVMBuildOr(ctx
->builder
,
905 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
906 arg
, LLVMConstInt(ctx
->i32
, 0, 0), ""),
907 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
908 arg
, all_ones
, ""), "");
910 return LLVMBuildSelect(ctx
->builder
, cond
, all_ones
, msb
, "");
914 ac_build_umsb(struct ac_llvm_context
*ctx
,
916 LLVMTypeRef dst_type
)
918 LLVMValueRef args
[2] = {
920 LLVMConstInt(ctx
->i1
, 1, 0),
922 LLVMValueRef msb
= ac_build_intrinsic(ctx
, "llvm.ctlz.i32",
923 dst_type
, args
, ARRAY_SIZE(args
),
924 AC_FUNC_ATTR_READNONE
);
926 /* The HW returns the last bit index from MSB, but TGSI/NIR wants
927 * the index from LSB. Invert it by doing "31 - msb". */
928 msb
= LLVMBuildSub(ctx
->builder
, LLVMConstInt(ctx
->i32
, 31, false),
932 return LLVMBuildSelect(ctx
->builder
,
933 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, arg
,
934 LLVMConstInt(ctx
->i32
, 0, 0), ""),
935 LLVMConstInt(ctx
->i32
, -1, true), msb
, "");
938 LLVMValueRef
ac_build_umin(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
941 LLVMValueRef cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntULE
, a
, b
, "");
942 return LLVMBuildSelect(ctx
->builder
, cmp
, a
, b
, "");
945 LLVMValueRef
ac_build_clamp(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
947 if (HAVE_LLVM
>= 0x0500) {
948 LLVMValueRef max
[2] = {
950 LLVMConstReal(ctx
->f32
, 0),
952 LLVMValueRef min
[2] = {
953 LLVMConstReal(ctx
->f32
, 1),
956 min
[1] = ac_build_intrinsic(ctx
, "llvm.maxnum.f32",
958 AC_FUNC_ATTR_READNONE
);
959 return ac_build_intrinsic(ctx
, "llvm.minnum.f32",
961 AC_FUNC_ATTR_READNONE
);
964 LLVMValueRef args
[3] = {
966 LLVMConstReal(ctx
->f32
, 0),
967 LLVMConstReal(ctx
->f32
, 1),
970 return ac_build_intrinsic(ctx
, "llvm.AMDGPU.clamp.", ctx
->f32
, args
, 3,
971 AC_FUNC_ATTR_READNONE
|
972 AC_FUNC_ATTR_LEGACY
);
975 void ac_build_export(struct ac_llvm_context
*ctx
, struct ac_export_args
*a
)
977 LLVMValueRef args
[9];
979 if (HAVE_LLVM
>= 0x0500) {
980 args
[0] = LLVMConstInt(ctx
->i32
, a
->target
, 0);
981 args
[1] = LLVMConstInt(ctx
->i32
, a
->enabled_channels
, 0);
984 LLVMTypeRef i16
= LLVMInt16TypeInContext(ctx
->context
);
985 LLVMTypeRef v2i16
= LLVMVectorType(i16
, 2);
987 args
[2] = LLVMBuildBitCast(ctx
->builder
, a
->out
[0],
989 args
[3] = LLVMBuildBitCast(ctx
->builder
, a
->out
[1],
991 args
[4] = LLVMConstInt(ctx
->i1
, a
->done
, 0);
992 args
[5] = LLVMConstInt(ctx
->i1
, a
->valid_mask
, 0);
994 ac_build_intrinsic(ctx
, "llvm.amdgcn.exp.compr.v2i16",
995 ctx
->voidt
, args
, 6, 0);
1000 args
[5] = a
->out
[3];
1001 args
[6] = LLVMConstInt(ctx
->i1
, a
->done
, 0);
1002 args
[7] = LLVMConstInt(ctx
->i1
, a
->valid_mask
, 0);
1004 ac_build_intrinsic(ctx
, "llvm.amdgcn.exp.f32",
1005 ctx
->voidt
, args
, 8, 0);
1010 args
[0] = LLVMConstInt(ctx
->i32
, a
->enabled_channels
, 0);
1011 args
[1] = LLVMConstInt(ctx
->i32
, a
->valid_mask
, 0);
1012 args
[2] = LLVMConstInt(ctx
->i32
, a
->done
, 0);
1013 args
[3] = LLVMConstInt(ctx
->i32
, a
->target
, 0);
1014 args
[4] = LLVMConstInt(ctx
->i32
, a
->compr
, 0);
1015 memcpy(args
+ 5, a
->out
, sizeof(a
->out
[0]) * 4);
1017 ac_build_intrinsic(ctx
, "llvm.SI.export", ctx
->voidt
, args
, 9,
1018 AC_FUNC_ATTR_LEGACY
);
1021 LLVMValueRef
ac_build_image_opcode(struct ac_llvm_context
*ctx
,
1022 struct ac_image_args
*a
)
1024 LLVMTypeRef dst_type
;
1025 LLVMValueRef args
[11];
1026 unsigned num_args
= 0;
1028 char intr_name
[128], type
[64];
1030 if (HAVE_LLVM
>= 0x0400) {
1031 bool sample
= a
->opcode
== ac_image_sample
||
1032 a
->opcode
== ac_image_gather4
||
1033 a
->opcode
== ac_image_get_lod
;
1036 args
[num_args
++] = bitcast_to_float(ctx
, a
->addr
);
1038 args
[num_args
++] = a
->addr
;
1040 args
[num_args
++] = a
->resource
;
1042 args
[num_args
++] = a
->sampler
;
1043 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->dmask
, 0);
1045 args
[num_args
++] = LLVMConstInt(ctx
->i1
, a
->unorm
, 0);
1046 args
[num_args
++] = LLVMConstInt(ctx
->i1
, 0, 0); /* glc */
1047 args
[num_args
++] = LLVMConstInt(ctx
->i1
, 0, 0); /* slc */
1048 args
[num_args
++] = LLVMConstInt(ctx
->i1
, 0, 0); /* lwe */
1049 args
[num_args
++] = LLVMConstInt(ctx
->i1
, a
->da
, 0);
1051 switch (a
->opcode
) {
1052 case ac_image_sample
:
1053 name
= "llvm.amdgcn.image.sample";
1055 case ac_image_gather4
:
1056 name
= "llvm.amdgcn.image.gather4";
1059 name
= "llvm.amdgcn.image.load";
1061 case ac_image_load_mip
:
1062 name
= "llvm.amdgcn.image.load.mip";
1064 case ac_image_get_lod
:
1065 name
= "llvm.amdgcn.image.getlod";
1067 case ac_image_get_resinfo
:
1068 name
= "llvm.amdgcn.image.getresinfo";
1071 unreachable("invalid image opcode");
1074 ac_build_type_name_for_intr(LLVMTypeOf(args
[0]), type
,
1077 snprintf(intr_name
, sizeof(intr_name
), "%s%s%s%s.v4f32.%s.v8i32",
1079 a
->compare
? ".c" : "",
1083 a
->level_zero
? ".lz" : "",
1084 a
->offset
? ".o" : "",
1087 LLVMValueRef result
=
1088 ac_build_intrinsic(ctx
, intr_name
,
1089 ctx
->v4f32
, args
, num_args
,
1090 AC_FUNC_ATTR_READNONE
);
1092 result
= LLVMBuildBitCast(ctx
->builder
, result
,
1098 args
[num_args
++] = a
->addr
;
1099 args
[num_args
++] = a
->resource
;
1101 if (a
->opcode
== ac_image_load
||
1102 a
->opcode
== ac_image_load_mip
||
1103 a
->opcode
== ac_image_get_resinfo
) {
1104 dst_type
= ctx
->v4i32
;
1106 dst_type
= ctx
->v4f32
;
1107 args
[num_args
++] = a
->sampler
;
1110 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->dmask
, 0);
1111 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->unorm
, 0);
1112 args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* r128 */
1113 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->da
, 0);
1114 args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* glc */
1115 args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* slc */
1116 args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* tfe */
1117 args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* lwe */
1119 switch (a
->opcode
) {
1120 case ac_image_sample
:
1121 name
= "llvm.SI.image.sample";
1123 case ac_image_gather4
:
1124 name
= "llvm.SI.gather4";
1127 name
= "llvm.SI.image.load";
1129 case ac_image_load_mip
:
1130 name
= "llvm.SI.image.load.mip";
1132 case ac_image_get_lod
:
1133 name
= "llvm.SI.getlod";
1135 case ac_image_get_resinfo
:
1136 name
= "llvm.SI.getresinfo";
1140 ac_build_type_name_for_intr(LLVMTypeOf(a
->addr
), type
, sizeof(type
));
1141 snprintf(intr_name
, sizeof(intr_name
), "%s%s%s%s.%s",
1143 a
->compare
? ".c" : "",
1147 a
->level_zero
? ".lz" : "",
1148 a
->offset
? ".o" : "",
1151 return ac_build_intrinsic(ctx
, intr_name
,
1152 dst_type
, args
, num_args
,
1153 AC_FUNC_ATTR_READNONE
|
1154 AC_FUNC_ATTR_LEGACY
);
1157 LLVMValueRef
ac_build_cvt_pkrtz_f16(struct ac_llvm_context
*ctx
,
1158 LLVMValueRef args
[2])
1160 if (HAVE_LLVM
>= 0x0500) {
1162 LLVMVectorType(LLVMHalfTypeInContext(ctx
->context
), 2);
1164 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pkrtz",
1166 AC_FUNC_ATTR_READNONE
);
1167 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
1170 return ac_build_intrinsic(ctx
, "llvm.SI.packf16", ctx
->i32
, args
, 2,
1171 AC_FUNC_ATTR_READNONE
|
1172 AC_FUNC_ATTR_LEGACY
);
1176 * KILL, AKA discard in GLSL.
1178 * \param value kill if value < 0.0 or value == NULL.
1180 void ac_build_kill(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
1183 ac_build_intrinsic(ctx
, "llvm.AMDGPU.kill", ctx
->voidt
,
1184 &value
, 1, AC_FUNC_ATTR_LEGACY
);
1186 ac_build_intrinsic(ctx
, "llvm.AMDGPU.kilp", ctx
->voidt
,
1187 NULL
, 0, AC_FUNC_ATTR_LEGACY
);
1191 LLVMValueRef
ac_build_bfe(struct ac_llvm_context
*ctx
, LLVMValueRef input
,
1192 LLVMValueRef offset
, LLVMValueRef width
,
1195 LLVMValueRef args
[] = {
1201 if (HAVE_LLVM
>= 0x0500) {
1202 return ac_build_intrinsic(ctx
,
1203 is_signed
? "llvm.amdgcn.sbfe.i32" :
1204 "llvm.amdgcn.ubfe.i32",
1206 AC_FUNC_ATTR_READNONE
);
1209 return ac_build_intrinsic(ctx
,
1210 is_signed
? "llvm.AMDGPU.bfe.i32" :
1211 "llvm.AMDGPU.bfe.u32",
1213 AC_FUNC_ATTR_READNONE
|
1214 AC_FUNC_ATTR_LEGACY
);
1217 void ac_get_image_intr_name(const char *base_name
,
1218 LLVMTypeRef data_type
,
1219 LLVMTypeRef coords_type
,
1220 LLVMTypeRef rsrc_type
,
1221 char *out_name
, unsigned out_len
)
1223 char coords_type_name
[8];
1225 ac_build_type_name_for_intr(coords_type
, coords_type_name
,
1226 sizeof(coords_type_name
));
1228 if (HAVE_LLVM
<= 0x0309) {
1229 snprintf(out_name
, out_len
, "%s.%s", base_name
, coords_type_name
);
1231 char data_type_name
[8];
1232 char rsrc_type_name
[8];
1234 ac_build_type_name_for_intr(data_type
, data_type_name
,
1235 sizeof(data_type_name
));
1236 ac_build_type_name_for_intr(rsrc_type
, rsrc_type_name
,
1237 sizeof(rsrc_type_name
));
1238 snprintf(out_name
, out_len
, "%s.%s.%s.%s", base_name
,
1239 data_type_name
, coords_type_name
, rsrc_type_name
);
1243 #define AC_EXP_TARGET (HAVE_LLVM >= 0x0500 ? 0 : 3)
1244 #define AC_EXP_OUT0 (HAVE_LLVM >= 0x0500 ? 2 : 5)
1252 struct ac_vs_exp_chan
1256 enum ac_ir_type type
;
1259 struct ac_vs_exp_inst
{
1262 struct ac_vs_exp_chan chan
[4];
1265 struct ac_vs_exports
{
1267 struct ac_vs_exp_inst exp
[VARYING_SLOT_MAX
];
1270 /* Return true if the PARAM export has been eliminated. */
1271 static bool ac_eliminate_const_output(uint8_t *vs_output_param_offset
,
1272 uint32_t num_outputs
,
1273 struct ac_vs_exp_inst
*exp
)
1275 unsigned i
, default_val
; /* SPI_PS_INPUT_CNTL_i.DEFAULT_VAL */
1276 bool is_zero
[4] = {}, is_one
[4] = {};
1278 for (i
= 0; i
< 4; i
++) {
1279 /* It's a constant expression. Undef outputs are eliminated too. */
1280 if (exp
->chan
[i
].type
== AC_IR_UNDEF
) {
1283 } else if (exp
->chan
[i
].type
== AC_IR_CONST
) {
1284 if (exp
->chan
[i
].const_float
== 0)
1286 else if (exp
->chan
[i
].const_float
== 1)
1289 return false; /* other constant */
1294 /* Only certain combinations of 0 and 1 can be eliminated. */
1295 if (is_zero
[0] && is_zero
[1] && is_zero
[2])
1296 default_val
= is_zero
[3] ? 0 : 1;
1297 else if (is_one
[0] && is_one
[1] && is_one
[2])
1298 default_val
= is_zero
[3] ? 2 : 3;
1302 /* The PARAM export can be represented as DEFAULT_VAL. Kill it. */
1303 LLVMInstructionEraseFromParent(exp
->inst
);
1305 /* Change OFFSET to DEFAULT_VAL. */
1306 for (i
= 0; i
< num_outputs
; i
++) {
1307 if (vs_output_param_offset
[i
] == exp
->offset
) {
1308 vs_output_param_offset
[i
] =
1309 AC_EXP_PARAM_DEFAULT_VAL_0000
+ default_val
;
1316 static bool ac_eliminate_duplicated_output(uint8_t *vs_output_param_offset
,
1317 uint32_t num_outputs
,
1318 struct ac_vs_exports
*processed
,
1319 struct ac_vs_exp_inst
*exp
)
1321 unsigned p
, copy_back_channels
= 0;
1323 /* See if the output is already in the list of processed outputs.
1324 * The LLVMValueRef comparison relies on SSA.
1326 for (p
= 0; p
< processed
->num
; p
++) {
1327 bool different
= false;
1329 for (unsigned j
= 0; j
< 4; j
++) {
1330 struct ac_vs_exp_chan
*c1
= &processed
->exp
[p
].chan
[j
];
1331 struct ac_vs_exp_chan
*c2
= &exp
->chan
[j
];
1333 /* Treat undef as a match. */
1334 if (c2
->type
== AC_IR_UNDEF
)
1337 /* If c1 is undef but c2 isn't, we can copy c2 to c1
1338 * and consider the instruction duplicated.
1340 if (c1
->type
== AC_IR_UNDEF
) {
1341 copy_back_channels
|= 1 << j
;
1345 /* Test whether the channels are not equal. */
1346 if (c1
->type
!= c2
->type
||
1347 (c1
->type
== AC_IR_CONST
&&
1348 c1
->const_float
!= c2
->const_float
) ||
1349 (c1
->type
== AC_IR_VALUE
&&
1350 c1
->value
!= c2
->value
)) {
1358 copy_back_channels
= 0;
1360 if (p
== processed
->num
)
1363 /* If a match was found, but the matching export has undef where the new
1364 * one has a normal value, copy the normal value to the undef channel.
1366 struct ac_vs_exp_inst
*match
= &processed
->exp
[p
];
1368 while (copy_back_channels
) {
1369 unsigned chan
= u_bit_scan(©_back_channels
);
1371 assert(match
->chan
[chan
].type
== AC_IR_UNDEF
);
1372 LLVMSetOperand(match
->inst
, AC_EXP_OUT0
+ chan
,
1373 exp
->chan
[chan
].value
);
1374 match
->chan
[chan
] = exp
->chan
[chan
];
1377 /* The PARAM export is duplicated. Kill it. */
1378 LLVMInstructionEraseFromParent(exp
->inst
);
1380 /* Change OFFSET to the matching export. */
1381 for (unsigned i
= 0; i
< num_outputs
; i
++) {
1382 if (vs_output_param_offset
[i
] == exp
->offset
) {
1383 vs_output_param_offset
[i
] = match
->offset
;
1390 void ac_optimize_vs_outputs(struct ac_llvm_context
*ctx
,
1391 LLVMValueRef main_fn
,
1392 uint8_t *vs_output_param_offset
,
1393 uint32_t num_outputs
,
1394 uint8_t *num_param_exports
)
1396 LLVMBasicBlockRef bb
;
1397 bool removed_any
= false;
1398 struct ac_vs_exports exports
;
1402 /* Process all LLVM instructions. */
1403 bb
= LLVMGetFirstBasicBlock(main_fn
);
1405 LLVMValueRef inst
= LLVMGetFirstInstruction(bb
);
1408 LLVMValueRef cur
= inst
;
1409 inst
= LLVMGetNextInstruction(inst
);
1410 struct ac_vs_exp_inst exp
;
1412 if (LLVMGetInstructionOpcode(cur
) != LLVMCall
)
1415 LLVMValueRef callee
= ac_llvm_get_called_value(cur
);
1417 if (!ac_llvm_is_function(callee
))
1420 const char *name
= LLVMGetValueName(callee
);
1421 unsigned num_args
= LLVMCountParams(callee
);
1423 /* Check if this is an export instruction. */
1424 if ((num_args
!= 9 && num_args
!= 8) ||
1425 (strcmp(name
, "llvm.SI.export") &&
1426 strcmp(name
, "llvm.amdgcn.exp.f32")))
1429 LLVMValueRef arg
= LLVMGetOperand(cur
, AC_EXP_TARGET
);
1430 unsigned target
= LLVMConstIntGetZExtValue(arg
);
1432 if (target
< V_008DFC_SQ_EXP_PARAM
)
1435 target
-= V_008DFC_SQ_EXP_PARAM
;
1437 /* Parse the instruction. */
1438 memset(&exp
, 0, sizeof(exp
));
1439 exp
.offset
= target
;
1442 for (unsigned i
= 0; i
< 4; i
++) {
1443 LLVMValueRef v
= LLVMGetOperand(cur
, AC_EXP_OUT0
+ i
);
1445 exp
.chan
[i
].value
= v
;
1447 if (LLVMIsUndef(v
)) {
1448 exp
.chan
[i
].type
= AC_IR_UNDEF
;
1449 } else if (LLVMIsAConstantFP(v
)) {
1450 LLVMBool loses_info
;
1451 exp
.chan
[i
].type
= AC_IR_CONST
;
1452 exp
.chan
[i
].const_float
=
1453 LLVMConstRealGetDouble(v
, &loses_info
);
1455 exp
.chan
[i
].type
= AC_IR_VALUE
;
1459 /* Eliminate constant and duplicated PARAM exports. */
1460 if (ac_eliminate_const_output(vs_output_param_offset
,
1461 num_outputs
, &exp
) ||
1462 ac_eliminate_duplicated_output(vs_output_param_offset
,
1463 num_outputs
, &exports
,
1467 exports
.exp
[exports
.num
++] = exp
;
1470 bb
= LLVMGetNextBasicBlock(bb
);
1473 /* Remove holes in export memory due to removed PARAM exports.
1474 * This is done by renumbering all PARAM exports.
1477 uint8_t old_offset
[VARYING_SLOT_MAX
];
1480 /* Make a copy of the offsets. We need the old version while
1481 * we are modifying some of them. */
1482 memcpy(old_offset
, vs_output_param_offset
,
1483 sizeof(old_offset
));
1485 for (i
= 0; i
< exports
.num
; i
++) {
1486 unsigned offset
= exports
.exp
[i
].offset
;
1488 /* Update vs_output_param_offset. Multiple outputs can
1489 * have the same offset.
1491 for (out
= 0; out
< num_outputs
; out
++) {
1492 if (old_offset
[out
] == offset
)
1493 vs_output_param_offset
[out
] = i
;
1496 /* Change the PARAM offset in the instruction. */
1497 LLVMSetOperand(exports
.exp
[i
].inst
, AC_EXP_TARGET
,
1498 LLVMConstInt(ctx
->i32
,
1499 V_008DFC_SQ_EXP_PARAM
+ i
, 0));
1501 *num_param_exports
= exports
.num
;