2 * Copyright 2014 Advanced Micro Devices, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sub license, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
15 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
16 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
17 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
18 * USE OR OTHER DEALINGS IN THE SOFTWARE.
20 * The above copyright notice and this permission notice (including the
21 * next paragraph) shall be included in all copies or substantial portions
25 /* based on pieces from si_pipe.c and radeon_llvm_emit.c */
26 #include "ac_llvm_build.h"
28 #include <llvm-c/Core.h>
30 #include "c11/threads.h"
35 #include "ac_llvm_util.h"
36 #include "ac_exp_param.h"
37 #include "util/bitscan.h"
38 #include "util/macros.h"
39 #include "util/u_atomic.h"
42 #include "shader_enums.h"
44 /* Initialize module-independent parts of the context.
46 * The caller is responsible for initializing ctx::module and ctx::builder.
49 ac_llvm_context_init(struct ac_llvm_context
*ctx
, LLVMContextRef context
,
50 enum chip_class chip_class
, enum radeon_family family
)
54 ctx
->chip_class
= chip_class
;
57 ctx
->context
= context
;
61 ctx
->voidt
= LLVMVoidTypeInContext(ctx
->context
);
62 ctx
->i1
= LLVMInt1TypeInContext(ctx
->context
);
63 ctx
->i8
= LLVMInt8TypeInContext(ctx
->context
);
64 ctx
->i16
= LLVMIntTypeInContext(ctx
->context
, 16);
65 ctx
->i32
= LLVMIntTypeInContext(ctx
->context
, 32);
66 ctx
->i64
= LLVMIntTypeInContext(ctx
->context
, 64);
67 ctx
->f16
= LLVMHalfTypeInContext(ctx
->context
);
68 ctx
->f32
= LLVMFloatTypeInContext(ctx
->context
);
69 ctx
->f64
= LLVMDoubleTypeInContext(ctx
->context
);
70 ctx
->v2i16
= LLVMVectorType(ctx
->i16
, 2);
71 ctx
->v2i32
= LLVMVectorType(ctx
->i32
, 2);
72 ctx
->v3i32
= LLVMVectorType(ctx
->i32
, 3);
73 ctx
->v4i32
= LLVMVectorType(ctx
->i32
, 4);
74 ctx
->v2f32
= LLVMVectorType(ctx
->f32
, 2);
75 ctx
->v4f32
= LLVMVectorType(ctx
->f32
, 4);
76 ctx
->v8i32
= LLVMVectorType(ctx
->i32
, 8);
78 ctx
->i32_0
= LLVMConstInt(ctx
->i32
, 0, false);
79 ctx
->i32_1
= LLVMConstInt(ctx
->i32
, 1, false);
80 ctx
->i64_0
= LLVMConstInt(ctx
->i64
, 0, false);
81 ctx
->i64_1
= LLVMConstInt(ctx
->i64
, 1, false);
82 ctx
->f32_0
= LLVMConstReal(ctx
->f32
, 0.0);
83 ctx
->f32_1
= LLVMConstReal(ctx
->f32
, 1.0);
84 ctx
->f64_0
= LLVMConstReal(ctx
->f64
, 0.0);
85 ctx
->f64_1
= LLVMConstReal(ctx
->f64
, 1.0);
87 ctx
->i1false
= LLVMConstInt(ctx
->i1
, 0, false);
88 ctx
->i1true
= LLVMConstInt(ctx
->i1
, 1, false);
90 ctx
->range_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
93 ctx
->invariant_load_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
94 "invariant.load", 14);
96 ctx
->fpmath_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
, "fpmath", 6);
98 args
[0] = LLVMConstReal(ctx
->f32
, 2.5);
99 ctx
->fpmath_md_2p5_ulp
= LLVMMDNodeInContext(ctx
->context
, args
, 1);
101 ctx
->uniform_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
102 "amdgpu.uniform", 14);
104 ctx
->empty_md
= LLVMMDNodeInContext(ctx
->context
, NULL
, 0);
108 ac_get_llvm_num_components(LLVMValueRef value
)
110 LLVMTypeRef type
= LLVMTypeOf(value
);
111 unsigned num_components
= LLVMGetTypeKind(type
) == LLVMVectorTypeKind
112 ? LLVMGetVectorSize(type
)
114 return num_components
;
118 ac_llvm_extract_elem(struct ac_llvm_context
*ac
,
122 if (LLVMGetTypeKind(LLVMTypeOf(value
)) != LLVMVectorTypeKind
) {
127 return LLVMBuildExtractElement(ac
->builder
, value
,
128 LLVMConstInt(ac
->i32
, index
, false), "");
132 ac_get_type_size(LLVMTypeRef type
)
134 LLVMTypeKind kind
= LLVMGetTypeKind(type
);
137 case LLVMIntegerTypeKind
:
138 return LLVMGetIntTypeWidth(type
) / 8;
139 case LLVMFloatTypeKind
:
141 case LLVMDoubleTypeKind
:
142 case LLVMPointerTypeKind
:
144 case LLVMVectorTypeKind
:
145 return LLVMGetVectorSize(type
) *
146 ac_get_type_size(LLVMGetElementType(type
));
147 case LLVMArrayTypeKind
:
148 return LLVMGetArrayLength(type
) *
149 ac_get_type_size(LLVMGetElementType(type
));
156 static LLVMTypeRef
to_integer_type_scalar(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
158 if (t
== ctx
->f16
|| t
== ctx
->i16
)
160 else if (t
== ctx
->f32
|| t
== ctx
->i32
)
162 else if (t
== ctx
->f64
|| t
== ctx
->i64
)
165 unreachable("Unhandled integer size");
169 ac_to_integer_type(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
171 if (LLVMGetTypeKind(t
) == LLVMVectorTypeKind
) {
172 LLVMTypeRef elem_type
= LLVMGetElementType(t
);
173 return LLVMVectorType(to_integer_type_scalar(ctx
, elem_type
),
174 LLVMGetVectorSize(t
));
176 return to_integer_type_scalar(ctx
, t
);
180 ac_to_integer(struct ac_llvm_context
*ctx
, LLVMValueRef v
)
182 LLVMTypeRef type
= LLVMTypeOf(v
);
183 return LLVMBuildBitCast(ctx
->builder
, v
, ac_to_integer_type(ctx
, type
), "");
186 static LLVMTypeRef
to_float_type_scalar(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
188 if (t
== ctx
->i16
|| t
== ctx
->f16
)
190 else if (t
== ctx
->i32
|| t
== ctx
->f32
)
192 else if (t
== ctx
->i64
|| t
== ctx
->f64
)
195 unreachable("Unhandled float size");
199 ac_to_float_type(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
201 if (LLVMGetTypeKind(t
) == LLVMVectorTypeKind
) {
202 LLVMTypeRef elem_type
= LLVMGetElementType(t
);
203 return LLVMVectorType(to_float_type_scalar(ctx
, elem_type
),
204 LLVMGetVectorSize(t
));
206 return to_float_type_scalar(ctx
, t
);
210 ac_to_float(struct ac_llvm_context
*ctx
, LLVMValueRef v
)
212 LLVMTypeRef type
= LLVMTypeOf(v
);
213 return LLVMBuildBitCast(ctx
->builder
, v
, ac_to_float_type(ctx
, type
), "");
218 ac_build_intrinsic(struct ac_llvm_context
*ctx
, const char *name
,
219 LLVMTypeRef return_type
, LLVMValueRef
*params
,
220 unsigned param_count
, unsigned attrib_mask
)
222 LLVMValueRef function
, call
;
223 bool set_callsite_attrs
= HAVE_LLVM
>= 0x0400 &&
224 !(attrib_mask
& AC_FUNC_ATTR_LEGACY
);
226 function
= LLVMGetNamedFunction(ctx
->module
, name
);
228 LLVMTypeRef param_types
[32], function_type
;
231 assert(param_count
<= 32);
233 for (i
= 0; i
< param_count
; ++i
) {
235 param_types
[i
] = LLVMTypeOf(params
[i
]);
238 LLVMFunctionType(return_type
, param_types
, param_count
, 0);
239 function
= LLVMAddFunction(ctx
->module
, name
, function_type
);
241 LLVMSetFunctionCallConv(function
, LLVMCCallConv
);
242 LLVMSetLinkage(function
, LLVMExternalLinkage
);
244 if (!set_callsite_attrs
)
245 ac_add_func_attributes(ctx
->context
, function
, attrib_mask
);
248 call
= LLVMBuildCall(ctx
->builder
, function
, params
, param_count
, "");
249 if (set_callsite_attrs
)
250 ac_add_func_attributes(ctx
->context
, call
, attrib_mask
);
255 * Given the i32 or vNi32 \p type, generate the textual name (e.g. for use with
258 void ac_build_type_name_for_intr(LLVMTypeRef type
, char *buf
, unsigned bufsize
)
260 LLVMTypeRef elem_type
= type
;
262 assert(bufsize
>= 8);
264 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
) {
265 int ret
= snprintf(buf
, bufsize
, "v%u",
266 LLVMGetVectorSize(type
));
268 char *type_name
= LLVMPrintTypeToString(type
);
269 fprintf(stderr
, "Error building type name for: %s\n",
273 elem_type
= LLVMGetElementType(type
);
277 switch (LLVMGetTypeKind(elem_type
)) {
279 case LLVMIntegerTypeKind
:
280 snprintf(buf
, bufsize
, "i%d", LLVMGetIntTypeWidth(elem_type
));
282 case LLVMFloatTypeKind
:
283 snprintf(buf
, bufsize
, "f32");
285 case LLVMDoubleTypeKind
:
286 snprintf(buf
, bufsize
, "f64");
292 * Helper function that builds an LLVM IR PHI node and immediately adds
296 ac_build_phi(struct ac_llvm_context
*ctx
, LLVMTypeRef type
,
297 unsigned count_incoming
, LLVMValueRef
*values
,
298 LLVMBasicBlockRef
*blocks
)
300 LLVMValueRef phi
= LLVMBuildPhi(ctx
->builder
, type
, "");
301 LLVMAddIncoming(phi
, values
, blocks
, count_incoming
);
305 /* Prevent optimizations (at least of memory accesses) across the current
306 * point in the program by emitting empty inline assembly that is marked as
307 * having side effects.
309 * Optionally, a value can be passed through the inline assembly to prevent
310 * LLVM from hoisting calls to ReadNone functions.
313 ac_build_optimization_barrier(struct ac_llvm_context
*ctx
,
316 static int counter
= 0;
318 LLVMBuilderRef builder
= ctx
->builder
;
321 snprintf(code
, sizeof(code
), "; %d", p_atomic_inc_return(&counter
));
324 LLVMTypeRef ftype
= LLVMFunctionType(ctx
->voidt
, NULL
, 0, false);
325 LLVMValueRef inlineasm
= LLVMConstInlineAsm(ftype
, code
, "", true, false);
326 LLVMBuildCall(builder
, inlineasm
, NULL
, 0, "");
328 LLVMTypeRef ftype
= LLVMFunctionType(ctx
->i32
, &ctx
->i32
, 1, false);
329 LLVMValueRef inlineasm
= LLVMConstInlineAsm(ftype
, code
, "=v,0", true, false);
330 LLVMValueRef vgpr
= *pvgpr
;
331 LLVMTypeRef vgpr_type
= LLVMTypeOf(vgpr
);
332 unsigned vgpr_size
= ac_get_type_size(vgpr_type
);
335 assert(vgpr_size
% 4 == 0);
337 vgpr
= LLVMBuildBitCast(builder
, vgpr
, LLVMVectorType(ctx
->i32
, vgpr_size
/ 4), "");
338 vgpr0
= LLVMBuildExtractElement(builder
, vgpr
, ctx
->i32_0
, "");
339 vgpr0
= LLVMBuildCall(builder
, inlineasm
, &vgpr0
, 1, "");
340 vgpr
= LLVMBuildInsertElement(builder
, vgpr
, vgpr0
, ctx
->i32_0
, "");
341 vgpr
= LLVMBuildBitCast(builder
, vgpr
, vgpr_type
, "");
348 ac_build_ballot(struct ac_llvm_context
*ctx
,
351 LLVMValueRef args
[3] = {
354 LLVMConstInt(ctx
->i32
, LLVMIntNE
, 0)
357 /* We currently have no other way to prevent LLVM from lifting the icmp
358 * calls to a dominating basic block.
360 ac_build_optimization_barrier(ctx
, &args
[0]);
362 if (LLVMTypeOf(args
[0]) != ctx
->i32
)
363 args
[0] = LLVMBuildBitCast(ctx
->builder
, args
[0], ctx
->i32
, "");
365 return ac_build_intrinsic(ctx
,
366 "llvm.amdgcn.icmp.i32",
368 AC_FUNC_ATTR_NOUNWIND
|
369 AC_FUNC_ATTR_READNONE
|
370 AC_FUNC_ATTR_CONVERGENT
);
374 ac_build_vote_all(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
376 LLVMValueRef active_set
= ac_build_ballot(ctx
, ctx
->i32_1
);
377 LLVMValueRef vote_set
= ac_build_ballot(ctx
, value
);
378 return LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, vote_set
, active_set
, "");
382 ac_build_vote_any(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
384 LLVMValueRef vote_set
= ac_build_ballot(ctx
, value
);
385 return LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, vote_set
,
386 LLVMConstInt(ctx
->i64
, 0, 0), "");
390 ac_build_vote_eq(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
392 LLVMValueRef active_set
= ac_build_ballot(ctx
, ctx
->i32_1
);
393 LLVMValueRef vote_set
= ac_build_ballot(ctx
, value
);
395 LLVMValueRef all
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
396 vote_set
, active_set
, "");
397 LLVMValueRef none
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
399 LLVMConstInt(ctx
->i64
, 0, 0), "");
400 return LLVMBuildOr(ctx
->builder
, all
, none
, "");
404 ac_build_varying_gather_values(struct ac_llvm_context
*ctx
, LLVMValueRef
*values
,
405 unsigned value_count
, unsigned component
)
407 LLVMValueRef vec
= NULL
;
409 if (value_count
== 1) {
410 return values
[component
];
411 } else if (!value_count
)
412 unreachable("value_count is 0");
414 for (unsigned i
= component
; i
< value_count
+ component
; i
++) {
415 LLVMValueRef value
= values
[i
];
418 vec
= LLVMGetUndef( LLVMVectorType(LLVMTypeOf(value
), value_count
));
419 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, i
- component
, false);
420 vec
= LLVMBuildInsertElement(ctx
->builder
, vec
, value
, index
, "");
426 ac_build_gather_values_extended(struct ac_llvm_context
*ctx
,
427 LLVMValueRef
*values
,
428 unsigned value_count
,
429 unsigned value_stride
,
433 LLVMBuilderRef builder
= ctx
->builder
;
434 LLVMValueRef vec
= NULL
;
437 if (value_count
== 1 && !always_vector
) {
439 return LLVMBuildLoad(builder
, values
[0], "");
441 } else if (!value_count
)
442 unreachable("value_count is 0");
444 for (i
= 0; i
< value_count
; i
++) {
445 LLVMValueRef value
= values
[i
* value_stride
];
447 value
= LLVMBuildLoad(builder
, value
, "");
450 vec
= LLVMGetUndef( LLVMVectorType(LLVMTypeOf(value
), value_count
));
451 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, i
, false);
452 vec
= LLVMBuildInsertElement(builder
, vec
, value
, index
, "");
458 ac_build_gather_values(struct ac_llvm_context
*ctx
,
459 LLVMValueRef
*values
,
460 unsigned value_count
)
462 return ac_build_gather_values_extended(ctx
, values
, value_count
, 1, false, false);
465 /* Expand a scalar or vector to <4 x type> by filling the remaining channels
466 * with undef. Extract at most num_channels components from the input.
468 LLVMValueRef
ac_build_expand_to_vec4(struct ac_llvm_context
*ctx
,
470 unsigned num_channels
)
472 LLVMTypeRef elemtype
;
473 LLVMValueRef chan
[4];
475 if (LLVMGetTypeKind(LLVMTypeOf(value
)) == LLVMVectorTypeKind
) {
476 unsigned vec_size
= LLVMGetVectorSize(LLVMTypeOf(value
));
477 num_channels
= MIN2(num_channels
, vec_size
);
479 if (num_channels
>= 4)
482 for (unsigned i
= 0; i
< num_channels
; i
++)
483 chan
[i
] = ac_llvm_extract_elem(ctx
, value
, i
);
485 elemtype
= LLVMGetElementType(LLVMTypeOf(value
));
488 assert(num_channels
== 1);
491 elemtype
= LLVMTypeOf(value
);
494 while (num_channels
< 4)
495 chan
[num_channels
++] = LLVMGetUndef(elemtype
);
497 return ac_build_gather_values(ctx
, chan
, 4);
501 ac_build_fdiv(struct ac_llvm_context
*ctx
,
505 LLVMValueRef ret
= LLVMBuildFDiv(ctx
->builder
, num
, den
, "");
507 /* Use v_rcp_f32 instead of precise division. */
508 if (!LLVMIsConstant(ret
))
509 LLVMSetMetadata(ret
, ctx
->fpmath_md_kind
, ctx
->fpmath_md_2p5_ulp
);
513 /* Coordinates for cube map selection. sc, tc, and ma are as in Table 8.27
514 * of the OpenGL 4.5 (Compatibility Profile) specification, except ma is
515 * already multiplied by two. id is the cube face number.
517 struct cube_selection_coords
{
524 build_cube_intrinsic(struct ac_llvm_context
*ctx
,
526 struct cube_selection_coords
*out
)
528 LLVMTypeRef f32
= ctx
->f32
;
530 out
->stc
[1] = ac_build_intrinsic(ctx
, "llvm.amdgcn.cubetc",
531 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
532 out
->stc
[0] = ac_build_intrinsic(ctx
, "llvm.amdgcn.cubesc",
533 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
534 out
->ma
= ac_build_intrinsic(ctx
, "llvm.amdgcn.cubema",
535 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
536 out
->id
= ac_build_intrinsic(ctx
, "llvm.amdgcn.cubeid",
537 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
541 * Build a manual selection sequence for cube face sc/tc coordinates and
542 * major axis vector (multiplied by 2 for consistency) for the given
543 * vec3 \p coords, for the face implied by \p selcoords.
545 * For the major axis, we always adjust the sign to be in the direction of
546 * selcoords.ma; i.e., a positive out_ma means that coords is pointed towards
547 * the selcoords major axis.
549 static void build_cube_select(struct ac_llvm_context
*ctx
,
550 const struct cube_selection_coords
*selcoords
,
551 const LLVMValueRef
*coords
,
552 LLVMValueRef
*out_st
,
553 LLVMValueRef
*out_ma
)
555 LLVMBuilderRef builder
= ctx
->builder
;
556 LLVMTypeRef f32
= LLVMTypeOf(coords
[0]);
557 LLVMValueRef is_ma_positive
;
559 LLVMValueRef is_ma_z
, is_not_ma_z
;
560 LLVMValueRef is_ma_y
;
561 LLVMValueRef is_ma_x
;
565 is_ma_positive
= LLVMBuildFCmp(builder
, LLVMRealUGE
,
566 selcoords
->ma
, LLVMConstReal(f32
, 0.0), "");
567 sgn_ma
= LLVMBuildSelect(builder
, is_ma_positive
,
568 LLVMConstReal(f32
, 1.0), LLVMConstReal(f32
, -1.0), "");
570 is_ma_z
= LLVMBuildFCmp(builder
, LLVMRealUGE
, selcoords
->id
, LLVMConstReal(f32
, 4.0), "");
571 is_not_ma_z
= LLVMBuildNot(builder
, is_ma_z
, "");
572 is_ma_y
= LLVMBuildAnd(builder
, is_not_ma_z
,
573 LLVMBuildFCmp(builder
, LLVMRealUGE
, selcoords
->id
, LLVMConstReal(f32
, 2.0), ""), "");
574 is_ma_x
= LLVMBuildAnd(builder
, is_not_ma_z
, LLVMBuildNot(builder
, is_ma_y
, ""), "");
577 tmp
= LLVMBuildSelect(builder
, is_ma_x
, coords
[2], coords
[0], "");
578 sgn
= LLVMBuildSelect(builder
, is_ma_y
, LLVMConstReal(f32
, 1.0),
579 LLVMBuildSelect(builder
, is_ma_z
, sgn_ma
,
580 LLVMBuildFNeg(builder
, sgn_ma
, ""), ""), "");
581 out_st
[0] = LLVMBuildFMul(builder
, tmp
, sgn
, "");
584 tmp
= LLVMBuildSelect(builder
, is_ma_y
, coords
[2], coords
[1], "");
585 sgn
= LLVMBuildSelect(builder
, is_ma_y
, sgn_ma
,
586 LLVMConstReal(f32
, -1.0), "");
587 out_st
[1] = LLVMBuildFMul(builder
, tmp
, sgn
, "");
590 tmp
= LLVMBuildSelect(builder
, is_ma_z
, coords
[2],
591 LLVMBuildSelect(builder
, is_ma_y
, coords
[1], coords
[0], ""), "");
592 tmp
= ac_build_intrinsic(ctx
, "llvm.fabs.f32",
593 ctx
->f32
, &tmp
, 1, AC_FUNC_ATTR_READNONE
);
594 *out_ma
= LLVMBuildFMul(builder
, tmp
, LLVMConstReal(f32
, 2.0), "");
598 ac_prepare_cube_coords(struct ac_llvm_context
*ctx
,
599 bool is_deriv
, bool is_array
, bool is_lod
,
600 LLVMValueRef
*coords_arg
,
601 LLVMValueRef
*derivs_arg
)
604 LLVMBuilderRef builder
= ctx
->builder
;
605 struct cube_selection_coords selcoords
;
606 LLVMValueRef coords
[3];
609 if (is_array
&& !is_lod
) {
610 LLVMValueRef tmp
= coords_arg
[3];
611 tmp
= ac_build_intrinsic(ctx
, "llvm.rint.f32", ctx
->f32
, &tmp
, 1, 0);
613 /* Section 8.9 (Texture Functions) of the GLSL 4.50 spec says:
615 * "For Array forms, the array layer used will be
617 * max(0, min(d−1, floor(layer+0.5)))
619 * where d is the depth of the texture array and layer
620 * comes from the component indicated in the tables below.
621 * Workaroudn for an issue where the layer is taken from a
622 * helper invocation which happens to fall on a different
623 * layer due to extrapolation."
625 * VI and earlier attempt to implement this in hardware by
626 * clamping the value of coords[2] = (8 * layer) + face.
627 * Unfortunately, this means that the we end up with the wrong
628 * face when clamping occurs.
630 * Clamp the layer earlier to work around the issue.
632 if (ctx
->chip_class
<= VI
) {
634 ge0
= LLVMBuildFCmp(builder
, LLVMRealOGE
, tmp
, ctx
->f32_0
, "");
635 tmp
= LLVMBuildSelect(builder
, ge0
, tmp
, ctx
->f32_0
, "");
641 build_cube_intrinsic(ctx
, coords_arg
, &selcoords
);
643 invma
= ac_build_intrinsic(ctx
, "llvm.fabs.f32",
644 ctx
->f32
, &selcoords
.ma
, 1, AC_FUNC_ATTR_READNONE
);
645 invma
= ac_build_fdiv(ctx
, LLVMConstReal(ctx
->f32
, 1.0), invma
);
647 for (int i
= 0; i
< 2; ++i
)
648 coords
[i
] = LLVMBuildFMul(builder
, selcoords
.stc
[i
], invma
, "");
650 coords
[2] = selcoords
.id
;
652 if (is_deriv
&& derivs_arg
) {
653 LLVMValueRef derivs
[4];
656 /* Convert cube derivatives to 2D derivatives. */
657 for (axis
= 0; axis
< 2; axis
++) {
658 LLVMValueRef deriv_st
[2];
659 LLVMValueRef deriv_ma
;
661 /* Transform the derivative alongside the texture
662 * coordinate. Mathematically, the correct formula is
663 * as follows. Assume we're projecting onto the +Z face
664 * and denote by dx/dh the derivative of the (original)
665 * X texture coordinate with respect to horizontal
666 * window coordinates. The projection onto the +Z face
671 * Then df/dh = df/dx * dx/dh + df/dz * dz/dh
672 * = 1/z * dx/dh - x/z * 1/z * dz/dh.
674 * This motivatives the implementation below.
676 * Whether this actually gives the expected results for
677 * apps that might feed in derivatives obtained via
678 * finite differences is anyone's guess. The OpenGL spec
679 * seems awfully quiet about how textureGrad for cube
680 * maps should be handled.
682 build_cube_select(ctx
, &selcoords
, &derivs_arg
[axis
* 3],
683 deriv_st
, &deriv_ma
);
685 deriv_ma
= LLVMBuildFMul(builder
, deriv_ma
, invma
, "");
687 for (int i
= 0; i
< 2; ++i
)
688 derivs
[axis
* 2 + i
] =
689 LLVMBuildFSub(builder
,
690 LLVMBuildFMul(builder
, deriv_st
[i
], invma
, ""),
691 LLVMBuildFMul(builder
, deriv_ma
, coords
[i
], ""), "");
694 memcpy(derivs_arg
, derivs
, sizeof(derivs
));
697 /* Shift the texture coordinate. This must be applied after the
698 * derivative calculation.
700 for (int i
= 0; i
< 2; ++i
)
701 coords
[i
] = LLVMBuildFAdd(builder
, coords
[i
], LLVMConstReal(ctx
->f32
, 1.5), "");
704 /* for cube arrays coord.z = coord.w(array_index) * 8 + face */
705 /* coords_arg.w component - array_index for cube arrays */
706 LLVMValueRef tmp
= LLVMBuildFMul(ctx
->builder
, coords_arg
[3], LLVMConstReal(ctx
->f32
, 8.0), "");
707 coords
[2] = LLVMBuildFAdd(ctx
->builder
, tmp
, coords
[2], "");
710 memcpy(coords_arg
, coords
, sizeof(coords
));
715 ac_build_fs_interp(struct ac_llvm_context
*ctx
,
716 LLVMValueRef llvm_chan
,
717 LLVMValueRef attr_number
,
722 LLVMValueRef args
[5];
725 if (HAVE_LLVM
< 0x0400) {
727 ij
[0] = LLVMBuildBitCast(ctx
->builder
, i
, ctx
->i32
, "");
728 ij
[1] = LLVMBuildBitCast(ctx
->builder
, j
, ctx
->i32
, "");
731 args
[1] = attr_number
;
733 args
[3] = ac_build_gather_values(ctx
, ij
, 2);
734 return ac_build_intrinsic(ctx
, "llvm.SI.fs.interp",
736 AC_FUNC_ATTR_READNONE
);
741 args
[2] = attr_number
;
744 p1
= ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p1",
745 ctx
->f32
, args
, 4, AC_FUNC_ATTR_READNONE
);
750 args
[3] = attr_number
;
753 return ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p2",
754 ctx
->f32
, args
, 5, AC_FUNC_ATTR_READNONE
);
758 ac_build_fs_interp_mov(struct ac_llvm_context
*ctx
,
759 LLVMValueRef parameter
,
760 LLVMValueRef llvm_chan
,
761 LLVMValueRef attr_number
,
764 LLVMValueRef args
[4];
765 if (HAVE_LLVM
< 0x0400) {
767 args
[1] = attr_number
;
770 return ac_build_intrinsic(ctx
,
771 "llvm.SI.fs.constant",
773 AC_FUNC_ATTR_READNONE
);
778 args
[2] = attr_number
;
781 return ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.mov",
782 ctx
->f32
, args
, 4, AC_FUNC_ATTR_READNONE
);
786 ac_build_gep0(struct ac_llvm_context
*ctx
,
787 LLVMValueRef base_ptr
,
790 LLVMValueRef indices
[2] = {
791 LLVMConstInt(ctx
->i32
, 0, 0),
794 return LLVMBuildGEP(ctx
->builder
, base_ptr
,
799 ac_build_indexed_store(struct ac_llvm_context
*ctx
,
800 LLVMValueRef base_ptr
, LLVMValueRef index
,
803 LLVMBuildStore(ctx
->builder
, value
,
804 ac_build_gep0(ctx
, base_ptr
, index
));
808 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad.
809 * It's equivalent to doing a load from &base_ptr[index].
811 * \param base_ptr Where the array starts.
812 * \param index The element index into the array.
813 * \param uniform Whether the base_ptr and index can be assumed to be
814 * dynamically uniform (i.e. load to an SGPR)
815 * \param invariant Whether the load is invariant (no other opcodes affect it)
818 ac_build_load_custom(struct ac_llvm_context
*ctx
, LLVMValueRef base_ptr
,
819 LLVMValueRef index
, bool uniform
, bool invariant
)
821 LLVMValueRef pointer
, result
;
823 pointer
= ac_build_gep0(ctx
, base_ptr
, index
);
825 LLVMSetMetadata(pointer
, ctx
->uniform_md_kind
, ctx
->empty_md
);
826 result
= LLVMBuildLoad(ctx
->builder
, pointer
, "");
828 LLVMSetMetadata(result
, ctx
->invariant_load_md_kind
, ctx
->empty_md
);
832 LLVMValueRef
ac_build_load(struct ac_llvm_context
*ctx
, LLVMValueRef base_ptr
,
835 return ac_build_load_custom(ctx
, base_ptr
, index
, false, false);
838 LLVMValueRef
ac_build_load_invariant(struct ac_llvm_context
*ctx
,
839 LLVMValueRef base_ptr
, LLVMValueRef index
)
841 return ac_build_load_custom(ctx
, base_ptr
, index
, false, true);
844 LLVMValueRef
ac_build_load_to_sgpr(struct ac_llvm_context
*ctx
,
845 LLVMValueRef base_ptr
, LLVMValueRef index
)
847 return ac_build_load_custom(ctx
, base_ptr
, index
, true, true);
850 /* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
851 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
852 * or v4i32 (num_channels=3,4).
855 ac_build_buffer_store_dword(struct ac_llvm_context
*ctx
,
858 unsigned num_channels
,
859 LLVMValueRef voffset
,
860 LLVMValueRef soffset
,
861 unsigned inst_offset
,
864 bool writeonly_memory
,
865 bool swizzle_enable_hint
)
867 /* SWIZZLE_ENABLE requires that soffset isn't folded into voffset
868 * (voffset is swizzled, but soffset isn't swizzled).
869 * llvm.amdgcn.buffer.store doesn't have a separate soffset parameter.
871 if (!swizzle_enable_hint
) {
872 /* Split 3 channel stores, becase LLVM doesn't support 3-channel
874 if (num_channels
== 3) {
875 LLVMValueRef v
[3], v01
;
877 for (int i
= 0; i
< 3; i
++) {
878 v
[i
] = LLVMBuildExtractElement(ctx
->builder
, vdata
,
879 LLVMConstInt(ctx
->i32
, i
, 0), "");
881 v01
= ac_build_gather_values(ctx
, v
, 2);
883 ac_build_buffer_store_dword(ctx
, rsrc
, v01
, 2, voffset
,
884 soffset
, inst_offset
, glc
, slc
,
885 writeonly_memory
, swizzle_enable_hint
);
886 ac_build_buffer_store_dword(ctx
, rsrc
, v
[2], 1, voffset
,
887 soffset
, inst_offset
+ 8,
889 writeonly_memory
, swizzle_enable_hint
);
893 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
894 static const char *types
[] = {"f32", "v2f32", "v4f32"};
896 LLVMValueRef offset
= soffset
;
899 offset
= LLVMBuildAdd(ctx
->builder
, offset
,
900 LLVMConstInt(ctx
->i32
, inst_offset
, 0), "");
902 offset
= LLVMBuildAdd(ctx
->builder
, offset
, voffset
, "");
904 LLVMValueRef args
[] = {
905 ac_to_float(ctx
, vdata
),
906 LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, ""),
907 LLVMConstInt(ctx
->i32
, 0, 0),
909 LLVMConstInt(ctx
->i1
, glc
, 0),
910 LLVMConstInt(ctx
->i1
, slc
, 0),
913 snprintf(name
, sizeof(name
), "llvm.amdgcn.buffer.store.%s",
916 ac_build_intrinsic(ctx
, name
, ctx
->voidt
,
917 args
, ARRAY_SIZE(args
),
919 AC_FUNC_ATTR_INACCESSIBLE_MEM_ONLY
:
920 AC_FUNC_ATTR_WRITEONLY
);
924 static unsigned dfmt
[] = {
925 V_008F0C_BUF_DATA_FORMAT_32
,
926 V_008F0C_BUF_DATA_FORMAT_32_32
,
927 V_008F0C_BUF_DATA_FORMAT_32_32_32
,
928 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
930 assert(num_channels
>= 1 && num_channels
<= 4);
932 LLVMValueRef args
[] = {
935 LLVMConstInt(ctx
->i32
, num_channels
, 0),
936 voffset
? voffset
: LLVMGetUndef(ctx
->i32
),
938 LLVMConstInt(ctx
->i32
, inst_offset
, 0),
939 LLVMConstInt(ctx
->i32
, dfmt
[num_channels
- 1], 0),
940 LLVMConstInt(ctx
->i32
, V_008F0C_BUF_NUM_FORMAT_UINT
, 0),
941 LLVMConstInt(ctx
->i32
, voffset
!= NULL
, 0),
942 LLVMConstInt(ctx
->i32
, 0, 0), /* idxen */
943 LLVMConstInt(ctx
->i32
, glc
, 0),
944 LLVMConstInt(ctx
->i32
, slc
, 0),
945 LLVMConstInt(ctx
->i32
, 0, 0), /* tfe*/
948 /* The instruction offset field has 12 bits */
949 assert(voffset
|| inst_offset
< (1 << 12));
951 /* The intrinsic is overloaded, we need to add a type suffix for overloading to work. */
952 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
953 const char *types
[] = {"i32", "v2i32", "v4i32"};
955 snprintf(name
, sizeof(name
), "llvm.SI.tbuffer.store.%s", types
[func
]);
957 ac_build_intrinsic(ctx
, name
, ctx
->voidt
,
958 args
, ARRAY_SIZE(args
),
959 AC_FUNC_ATTR_LEGACY
);
963 ac_build_buffer_load_common(struct ac_llvm_context
*ctx
,
966 LLVMValueRef voffset
,
967 unsigned num_channels
,
973 LLVMValueRef args
[] = {
974 LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, ""),
975 vindex
? vindex
: LLVMConstInt(ctx
->i32
, 0, 0),
977 LLVMConstInt(ctx
->i1
, glc
, 0),
978 LLVMConstInt(ctx
->i1
, slc
, 0)
980 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
982 LLVMTypeRef types
[] = {ctx
->f32
, ctx
->v2f32
, ctx
->v4f32
};
983 const char *type_names
[] = {"f32", "v2f32", "v4f32"};
987 snprintf(name
, sizeof(name
), "llvm.amdgcn.buffer.load.format.%s",
990 snprintf(name
, sizeof(name
), "llvm.amdgcn.buffer.load.%s",
994 return ac_build_intrinsic(ctx
, name
, types
[func
], args
,
996 ac_get_load_intr_attribs(can_speculate
));
1000 ac_build_buffer_load(struct ac_llvm_context
*ctx
,
1003 LLVMValueRef vindex
,
1004 LLVMValueRef voffset
,
1005 LLVMValueRef soffset
,
1006 unsigned inst_offset
,
1012 LLVMValueRef offset
= LLVMConstInt(ctx
->i32
, inst_offset
, 0);
1014 offset
= LLVMBuildAdd(ctx
->builder
, offset
, voffset
, "");
1016 offset
= LLVMBuildAdd(ctx
->builder
, offset
, soffset
, "");
1018 /* TODO: VI and later generations can use SMEM with GLC=1.*/
1019 if (allow_smem
&& !glc
&& !slc
) {
1020 assert(vindex
== NULL
);
1022 LLVMValueRef result
[8];
1024 for (int i
= 0; i
< num_channels
; i
++) {
1026 offset
= LLVMBuildAdd(ctx
->builder
, offset
,
1027 LLVMConstInt(ctx
->i32
, 4, 0), "");
1029 LLVMValueRef args
[2] = {rsrc
, offset
};
1030 result
[i
] = ac_build_intrinsic(ctx
, "llvm.SI.load.const.v4i32",
1032 AC_FUNC_ATTR_READNONE
|
1033 AC_FUNC_ATTR_LEGACY
);
1035 if (num_channels
== 1)
1038 if (num_channels
== 3)
1039 result
[num_channels
++] = LLVMGetUndef(ctx
->f32
);
1040 return ac_build_gather_values(ctx
, result
, num_channels
);
1043 return ac_build_buffer_load_common(ctx
, rsrc
, vindex
, offset
,
1044 num_channels
, glc
, slc
,
1045 can_speculate
, false);
1048 LLVMValueRef
ac_build_buffer_load_format(struct ac_llvm_context
*ctx
,
1050 LLVMValueRef vindex
,
1051 LLVMValueRef voffset
,
1052 unsigned num_channels
,
1056 return ac_build_buffer_load_common(ctx
, rsrc
, vindex
, voffset
,
1057 num_channels
, glc
, false,
1058 can_speculate
, true);
1062 * Set range metadata on an instruction. This can only be used on load and
1063 * call instructions. If you know an instruction can only produce the values
1064 * 0, 1, 2, you would do set_range_metadata(value, 0, 3);
1065 * \p lo is the minimum value inclusive.
1066 * \p hi is the maximum value exclusive.
1068 static void set_range_metadata(struct ac_llvm_context
*ctx
,
1069 LLVMValueRef value
, unsigned lo
, unsigned hi
)
1071 LLVMValueRef range_md
, md_args
[2];
1072 LLVMTypeRef type
= LLVMTypeOf(value
);
1073 LLVMContextRef context
= LLVMGetTypeContext(type
);
1075 md_args
[0] = LLVMConstInt(type
, lo
, false);
1076 md_args
[1] = LLVMConstInt(type
, hi
, false);
1077 range_md
= LLVMMDNodeInContext(context
, md_args
, 2);
1078 LLVMSetMetadata(value
, ctx
->range_md_kind
, range_md
);
1082 ac_get_thread_id(struct ac_llvm_context
*ctx
)
1086 LLVMValueRef tid_args
[2];
1087 tid_args
[0] = LLVMConstInt(ctx
->i32
, 0xffffffff, false);
1088 tid_args
[1] = LLVMConstInt(ctx
->i32
, 0, false);
1089 tid_args
[1] = ac_build_intrinsic(ctx
,
1090 "llvm.amdgcn.mbcnt.lo", ctx
->i32
,
1091 tid_args
, 2, AC_FUNC_ATTR_READNONE
);
1093 tid
= ac_build_intrinsic(ctx
, "llvm.amdgcn.mbcnt.hi",
1095 2, AC_FUNC_ATTR_READNONE
);
1096 set_range_metadata(ctx
, tid
, 0, 64);
1101 * SI implements derivatives using the local data store (LDS)
1102 * All writes to the LDS happen in all executing threads at
1103 * the same time. TID is the Thread ID for the current
1104 * thread and is a value between 0 and 63, representing
1105 * the thread's position in the wavefront.
1107 * For the pixel shader threads are grouped into quads of four pixels.
1108 * The TIDs of the pixels of a quad are:
1116 * So, masking the TID with 0xfffffffc yields the TID of the top left pixel
1117 * of the quad, masking with 0xfffffffd yields the TID of the top pixel of
1118 * the current pixel's column, and masking with 0xfffffffe yields the TID
1119 * of the left pixel of the current pixel's row.
1121 * Adding 1 yields the TID of the pixel to the right of the left pixel, and
1122 * adding 2 yields the TID of the pixel below the top pixel.
1125 ac_build_ddxy(struct ac_llvm_context
*ctx
,
1130 LLVMValueRef tl
, trbl
, args
[2];
1131 LLVMValueRef result
;
1133 if (ctx
->chip_class
>= VI
) {
1134 LLVMValueRef thread_id
, tl_tid
, trbl_tid
;
1135 thread_id
= ac_get_thread_id(ctx
);
1137 tl_tid
= LLVMBuildAnd(ctx
->builder
, thread_id
,
1138 LLVMConstInt(ctx
->i32
, mask
, false), "");
1140 trbl_tid
= LLVMBuildAdd(ctx
->builder
, tl_tid
,
1141 LLVMConstInt(ctx
->i32
, idx
, false), "");
1143 args
[0] = LLVMBuildMul(ctx
->builder
, tl_tid
,
1144 LLVMConstInt(ctx
->i32
, 4, false), "");
1146 tl
= ac_build_intrinsic(ctx
,
1147 "llvm.amdgcn.ds.bpermute", ctx
->i32
,
1149 AC_FUNC_ATTR_READNONE
|
1150 AC_FUNC_ATTR_CONVERGENT
);
1152 args
[0] = LLVMBuildMul(ctx
->builder
, trbl_tid
,
1153 LLVMConstInt(ctx
->i32
, 4, false), "");
1154 trbl
= ac_build_intrinsic(ctx
,
1155 "llvm.amdgcn.ds.bpermute", ctx
->i32
,
1157 AC_FUNC_ATTR_READNONE
|
1158 AC_FUNC_ATTR_CONVERGENT
);
1160 uint32_t masks
[2] = {};
1163 case AC_TID_MASK_TOP_LEFT
:
1171 case AC_TID_MASK_TOP
:
1175 case AC_TID_MASK_LEFT
:
1184 args
[1] = LLVMConstInt(ctx
->i32
, masks
[0], false);
1186 tl
= ac_build_intrinsic(ctx
,
1187 "llvm.amdgcn.ds.swizzle", ctx
->i32
,
1189 AC_FUNC_ATTR_READNONE
|
1190 AC_FUNC_ATTR_CONVERGENT
);
1192 args
[1] = LLVMConstInt(ctx
->i32
, masks
[1], false);
1193 trbl
= ac_build_intrinsic(ctx
,
1194 "llvm.amdgcn.ds.swizzle", ctx
->i32
,
1196 AC_FUNC_ATTR_READNONE
|
1197 AC_FUNC_ATTR_CONVERGENT
);
1200 tl
= LLVMBuildBitCast(ctx
->builder
, tl
, ctx
->f32
, "");
1201 trbl
= LLVMBuildBitCast(ctx
->builder
, trbl
, ctx
->f32
, "");
1202 result
= LLVMBuildFSub(ctx
->builder
, trbl
, tl
, "");
1207 ac_build_sendmsg(struct ac_llvm_context
*ctx
,
1209 LLVMValueRef wave_id
)
1211 LLVMValueRef args
[2];
1212 const char *intr_name
= (HAVE_LLVM
< 0x0400) ? "llvm.SI.sendmsg" : "llvm.amdgcn.s.sendmsg";
1213 args
[0] = LLVMConstInt(ctx
->i32
, msg
, false);
1215 ac_build_intrinsic(ctx
, intr_name
, ctx
->voidt
, args
, 2, 0);
1219 ac_build_imsb(struct ac_llvm_context
*ctx
,
1221 LLVMTypeRef dst_type
)
1223 const char *intr_name
= (HAVE_LLVM
< 0x0400) ? "llvm.AMDGPU.flbit.i32" :
1224 "llvm.amdgcn.sffbh.i32";
1225 LLVMValueRef msb
= ac_build_intrinsic(ctx
, intr_name
,
1227 AC_FUNC_ATTR_READNONE
);
1229 /* The HW returns the last bit index from MSB, but NIR/TGSI wants
1230 * the index from LSB. Invert it by doing "31 - msb". */
1231 msb
= LLVMBuildSub(ctx
->builder
, LLVMConstInt(ctx
->i32
, 31, false),
1234 LLVMValueRef all_ones
= LLVMConstInt(ctx
->i32
, -1, true);
1235 LLVMValueRef cond
= LLVMBuildOr(ctx
->builder
,
1236 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
1237 arg
, LLVMConstInt(ctx
->i32
, 0, 0), ""),
1238 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
1239 arg
, all_ones
, ""), "");
1241 return LLVMBuildSelect(ctx
->builder
, cond
, all_ones
, msb
, "");
1245 ac_build_umsb(struct ac_llvm_context
*ctx
,
1247 LLVMTypeRef dst_type
)
1249 LLVMValueRef args
[2] = {
1253 LLVMValueRef msb
= ac_build_intrinsic(ctx
, "llvm.ctlz.i32",
1254 dst_type
, args
, ARRAY_SIZE(args
),
1255 AC_FUNC_ATTR_READNONE
);
1257 /* The HW returns the last bit index from MSB, but TGSI/NIR wants
1258 * the index from LSB. Invert it by doing "31 - msb". */
1259 msb
= LLVMBuildSub(ctx
->builder
, LLVMConstInt(ctx
->i32
, 31, false),
1262 /* check for zero */
1263 return LLVMBuildSelect(ctx
->builder
,
1264 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, arg
,
1265 LLVMConstInt(ctx
->i32
, 0, 0), ""),
1266 LLVMConstInt(ctx
->i32
, -1, true), msb
, "");
1269 LLVMValueRef
ac_build_fmin(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
1272 LLVMValueRef args
[2] = {a
, b
};
1273 return ac_build_intrinsic(ctx
, "llvm.minnum.f32", ctx
->f32
, args
, 2,
1274 AC_FUNC_ATTR_READNONE
);
1277 LLVMValueRef
ac_build_fmax(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
1280 LLVMValueRef args
[2] = {a
, b
};
1281 return ac_build_intrinsic(ctx
, "llvm.maxnum.f32", ctx
->f32
, args
, 2,
1282 AC_FUNC_ATTR_READNONE
);
1285 LLVMValueRef
ac_build_imin(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
1288 LLVMValueRef cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSLE
, a
, b
, "");
1289 return LLVMBuildSelect(ctx
->builder
, cmp
, a
, b
, "");
1292 LLVMValueRef
ac_build_imax(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
1295 LLVMValueRef cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGT
, a
, b
, "");
1296 return LLVMBuildSelect(ctx
->builder
, cmp
, a
, b
, "");
1299 LLVMValueRef
ac_build_umin(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
1302 LLVMValueRef cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntULE
, a
, b
, "");
1303 return LLVMBuildSelect(ctx
->builder
, cmp
, a
, b
, "");
1306 LLVMValueRef
ac_build_clamp(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
1308 if (HAVE_LLVM
>= 0x0500) {
1309 return ac_build_fmin(ctx
, ac_build_fmax(ctx
, value
, ctx
->f32_0
),
1313 LLVMValueRef args
[3] = {
1315 LLVMConstReal(ctx
->f32
, 0),
1316 LLVMConstReal(ctx
->f32
, 1),
1319 return ac_build_intrinsic(ctx
, "llvm.AMDGPU.clamp.", ctx
->f32
, args
, 3,
1320 AC_FUNC_ATTR_READNONE
|
1321 AC_FUNC_ATTR_LEGACY
);
1324 void ac_build_export(struct ac_llvm_context
*ctx
, struct ac_export_args
*a
)
1326 LLVMValueRef args
[9];
1328 if (HAVE_LLVM
>= 0x0500) {
1329 args
[0] = LLVMConstInt(ctx
->i32
, a
->target
, 0);
1330 args
[1] = LLVMConstInt(ctx
->i32
, a
->enabled_channels
, 0);
1333 LLVMTypeRef i16
= LLVMInt16TypeInContext(ctx
->context
);
1334 LLVMTypeRef v2i16
= LLVMVectorType(i16
, 2);
1336 args
[2] = LLVMBuildBitCast(ctx
->builder
, a
->out
[0],
1338 args
[3] = LLVMBuildBitCast(ctx
->builder
, a
->out
[1],
1340 args
[4] = LLVMConstInt(ctx
->i1
, a
->done
, 0);
1341 args
[5] = LLVMConstInt(ctx
->i1
, a
->valid_mask
, 0);
1343 ac_build_intrinsic(ctx
, "llvm.amdgcn.exp.compr.v2i16",
1344 ctx
->voidt
, args
, 6, 0);
1346 args
[2] = a
->out
[0];
1347 args
[3] = a
->out
[1];
1348 args
[4] = a
->out
[2];
1349 args
[5] = a
->out
[3];
1350 args
[6] = LLVMConstInt(ctx
->i1
, a
->done
, 0);
1351 args
[7] = LLVMConstInt(ctx
->i1
, a
->valid_mask
, 0);
1353 ac_build_intrinsic(ctx
, "llvm.amdgcn.exp.f32",
1354 ctx
->voidt
, args
, 8, 0);
1359 args
[0] = LLVMConstInt(ctx
->i32
, a
->enabled_channels
, 0);
1360 args
[1] = LLVMConstInt(ctx
->i32
, a
->valid_mask
, 0);
1361 args
[2] = LLVMConstInt(ctx
->i32
, a
->done
, 0);
1362 args
[3] = LLVMConstInt(ctx
->i32
, a
->target
, 0);
1363 args
[4] = LLVMConstInt(ctx
->i32
, a
->compr
, 0);
1364 memcpy(args
+ 5, a
->out
, sizeof(a
->out
[0]) * 4);
1366 ac_build_intrinsic(ctx
, "llvm.SI.export", ctx
->voidt
, args
, 9,
1367 AC_FUNC_ATTR_LEGACY
);
1370 LLVMValueRef
ac_build_image_opcode(struct ac_llvm_context
*ctx
,
1371 struct ac_image_args
*a
)
1373 LLVMTypeRef dst_type
;
1374 LLVMValueRef args
[11];
1375 unsigned num_args
= 0;
1376 const char *name
= NULL
;
1377 char intr_name
[128], type
[64];
1379 if (HAVE_LLVM
>= 0x0400) {
1380 bool sample
= a
->opcode
== ac_image_sample
||
1381 a
->opcode
== ac_image_gather4
||
1382 a
->opcode
== ac_image_get_lod
;
1385 args
[num_args
++] = ac_to_float(ctx
, a
->addr
);
1387 args
[num_args
++] = a
->addr
;
1389 args
[num_args
++] = a
->resource
;
1391 args
[num_args
++] = a
->sampler
;
1392 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->dmask
, 0);
1394 args
[num_args
++] = LLVMConstInt(ctx
->i1
, a
->unorm
, 0);
1395 args
[num_args
++] = ctx
->i1false
; /* glc */
1396 args
[num_args
++] = ctx
->i1false
; /* slc */
1397 args
[num_args
++] = ctx
->i1false
; /* lwe */
1398 args
[num_args
++] = LLVMConstInt(ctx
->i1
, a
->da
, 0);
1400 switch (a
->opcode
) {
1401 case ac_image_sample
:
1402 name
= "llvm.amdgcn.image.sample";
1404 case ac_image_gather4
:
1405 name
= "llvm.amdgcn.image.gather4";
1408 name
= "llvm.amdgcn.image.load";
1410 case ac_image_load_mip
:
1411 name
= "llvm.amdgcn.image.load.mip";
1413 case ac_image_get_lod
:
1414 name
= "llvm.amdgcn.image.getlod";
1416 case ac_image_get_resinfo
:
1417 name
= "llvm.amdgcn.image.getresinfo";
1420 unreachable("invalid image opcode");
1423 ac_build_type_name_for_intr(LLVMTypeOf(args
[0]), type
,
1426 snprintf(intr_name
, sizeof(intr_name
), "%s%s%s%s.v4f32.%s.v8i32",
1428 a
->compare
? ".c" : "",
1432 a
->level_zero
? ".lz" : "",
1433 a
->offset
? ".o" : "",
1436 LLVMValueRef result
=
1437 ac_build_intrinsic(ctx
, intr_name
,
1438 ctx
->v4f32
, args
, num_args
,
1439 AC_FUNC_ATTR_READNONE
);
1441 result
= LLVMBuildBitCast(ctx
->builder
, result
,
1447 args
[num_args
++] = a
->addr
;
1448 args
[num_args
++] = a
->resource
;
1450 if (a
->opcode
== ac_image_load
||
1451 a
->opcode
== ac_image_load_mip
||
1452 a
->opcode
== ac_image_get_resinfo
) {
1453 dst_type
= ctx
->v4i32
;
1455 dst_type
= ctx
->v4f32
;
1456 args
[num_args
++] = a
->sampler
;
1459 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->dmask
, 0);
1460 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->unorm
, 0);
1461 args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* r128 */
1462 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->da
, 0);
1463 args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* glc */
1464 args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* slc */
1465 args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* tfe */
1466 args
[num_args
++] = LLVMConstInt(ctx
->i32
, 0, 0); /* lwe */
1468 switch (a
->opcode
) {
1469 case ac_image_sample
:
1470 name
= "llvm.SI.image.sample";
1472 case ac_image_gather4
:
1473 name
= "llvm.SI.gather4";
1476 name
= "llvm.SI.image.load";
1478 case ac_image_load_mip
:
1479 name
= "llvm.SI.image.load.mip";
1481 case ac_image_get_lod
:
1482 name
= "llvm.SI.getlod";
1484 case ac_image_get_resinfo
:
1485 name
= "llvm.SI.getresinfo";
1489 ac_build_type_name_for_intr(LLVMTypeOf(a
->addr
), type
, sizeof(type
));
1490 snprintf(intr_name
, sizeof(intr_name
), "%s%s%s%s.%s",
1492 a
->compare
? ".c" : "",
1496 a
->level_zero
? ".lz" : "",
1497 a
->offset
? ".o" : "",
1500 return ac_build_intrinsic(ctx
, intr_name
,
1501 dst_type
, args
, num_args
,
1502 AC_FUNC_ATTR_READNONE
|
1503 AC_FUNC_ATTR_LEGACY
);
1506 LLVMValueRef
ac_build_cvt_pkrtz_f16(struct ac_llvm_context
*ctx
,
1507 LLVMValueRef args
[2])
1509 if (HAVE_LLVM
>= 0x0500) {
1511 LLVMVectorType(LLVMHalfTypeInContext(ctx
->context
), 2);
1513 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pkrtz",
1515 AC_FUNC_ATTR_READNONE
);
1516 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
1519 return ac_build_intrinsic(ctx
, "llvm.SI.packf16", ctx
->i32
, args
, 2,
1520 AC_FUNC_ATTR_READNONE
|
1521 AC_FUNC_ATTR_LEGACY
);
1524 /* Upper 16 bits must be zero. */
1525 static LLVMValueRef
ac_llvm_pack_two_int16(struct ac_llvm_context
*ctx
,
1526 LLVMValueRef val
[2])
1528 return LLVMBuildOr(ctx
->builder
, val
[0],
1529 LLVMBuildShl(ctx
->builder
, val
[1],
1530 LLVMConstInt(ctx
->i32
, 16, 0),
1534 /* Upper 16 bits are ignored and will be dropped. */
1535 static LLVMValueRef
ac_llvm_pack_two_int32_as_int16(struct ac_llvm_context
*ctx
,
1536 LLVMValueRef val
[2])
1538 LLVMValueRef v
[2] = {
1539 LLVMBuildAnd(ctx
->builder
, val
[0],
1540 LLVMConstInt(ctx
->i32
, 0xffff, 0), ""),
1543 return ac_llvm_pack_two_int16(ctx
, v
);
1546 LLVMValueRef
ac_build_cvt_pknorm_i16(struct ac_llvm_context
*ctx
,
1547 LLVMValueRef args
[2])
1549 if (HAVE_LLVM
>= 0x0600) {
1551 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pknorm.i16",
1552 ctx
->v2i16
, args
, 2,
1553 AC_FUNC_ATTR_READNONE
);
1554 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
1557 LLVMValueRef val
[2];
1559 for (int chan
= 0; chan
< 2; chan
++) {
1560 /* Clamp between [-1, 1]. */
1561 val
[chan
] = ac_build_fmin(ctx
, args
[chan
], ctx
->f32_1
);
1562 val
[chan
] = ac_build_fmax(ctx
, val
[chan
], LLVMConstReal(ctx
->f32
, -1));
1563 /* Convert to a signed integer in [-32767, 32767]. */
1564 val
[chan
] = LLVMBuildFMul(ctx
->builder
, val
[chan
],
1565 LLVMConstReal(ctx
->f32
, 32767), "");
1566 /* If positive, add 0.5, else add -0.5. */
1567 val
[chan
] = LLVMBuildFAdd(ctx
->builder
, val
[chan
],
1568 LLVMBuildSelect(ctx
->builder
,
1569 LLVMBuildFCmp(ctx
->builder
, LLVMRealOGE
,
1570 val
[chan
], ctx
->f32_0
, ""),
1571 LLVMConstReal(ctx
->f32
, 0.5),
1572 LLVMConstReal(ctx
->f32
, -0.5), ""), "");
1573 val
[chan
] = LLVMBuildFPToSI(ctx
->builder
, val
[chan
], ctx
->i32
, "");
1575 return ac_llvm_pack_two_int32_as_int16(ctx
, val
);
1578 LLVMValueRef
ac_build_cvt_pknorm_u16(struct ac_llvm_context
*ctx
,
1579 LLVMValueRef args
[2])
1581 if (HAVE_LLVM
>= 0x0600) {
1583 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pknorm.u16",
1584 ctx
->v2i16
, args
, 2,
1585 AC_FUNC_ATTR_READNONE
);
1586 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
1589 LLVMValueRef val
[2];
1591 for (int chan
= 0; chan
< 2; chan
++) {
1592 val
[chan
] = ac_build_clamp(ctx
, args
[chan
]);
1593 val
[chan
] = LLVMBuildFMul(ctx
->builder
, val
[chan
],
1594 LLVMConstReal(ctx
->f32
, 65535), "");
1595 val
[chan
] = LLVMBuildFAdd(ctx
->builder
, val
[chan
],
1596 LLVMConstReal(ctx
->f32
, 0.5), "");
1597 val
[chan
] = LLVMBuildFPToUI(ctx
->builder
, val
[chan
],
1600 return ac_llvm_pack_two_int32_as_int16(ctx
, val
);
1603 /* The 8-bit and 10-bit clamping is for HW workarounds. */
1604 LLVMValueRef
ac_build_cvt_pk_i16(struct ac_llvm_context
*ctx
,
1605 LLVMValueRef args
[2], unsigned bits
, bool hi
)
1607 assert(bits
== 8 || bits
== 10 || bits
== 16);
1609 LLVMValueRef max_rgb
= LLVMConstInt(ctx
->i32
,
1610 bits
== 8 ? 127 : bits
== 10 ? 511 : 32767, 0);
1611 LLVMValueRef min_rgb
= LLVMConstInt(ctx
->i32
,
1612 bits
== 8 ? -128 : bits
== 10 ? -512 : -32768, 0);
1613 LLVMValueRef max_alpha
=
1614 bits
!= 10 ? max_rgb
: ctx
->i32_1
;
1615 LLVMValueRef min_alpha
=
1616 bits
!= 10 ? min_rgb
: LLVMConstInt(ctx
->i32
, -2, 0);
1617 bool has_intrinsic
= HAVE_LLVM
>= 0x0600;
1620 if (!has_intrinsic
|| bits
!= 16) {
1621 for (int i
= 0; i
< 2; i
++) {
1622 bool alpha
= hi
&& i
== 1;
1623 args
[i
] = ac_build_imin(ctx
, args
[i
],
1624 alpha
? max_alpha
: max_rgb
);
1625 args
[i
] = ac_build_imax(ctx
, args
[i
],
1626 alpha
? min_alpha
: min_rgb
);
1630 if (has_intrinsic
) {
1632 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pk.i16",
1633 ctx
->v2i16
, args
, 2,
1634 AC_FUNC_ATTR_READNONE
);
1635 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
1638 return ac_llvm_pack_two_int32_as_int16(ctx
, args
);
1641 /* The 8-bit and 10-bit clamping is for HW workarounds. */
1642 LLVMValueRef
ac_build_cvt_pk_u16(struct ac_llvm_context
*ctx
,
1643 LLVMValueRef args
[2], unsigned bits
, bool hi
)
1645 assert(bits
== 8 || bits
== 10 || bits
== 16);
1647 LLVMValueRef max_rgb
= LLVMConstInt(ctx
->i32
,
1648 bits
== 8 ? 255 : bits
== 10 ? 1023 : 65535, 0);
1649 LLVMValueRef max_alpha
=
1650 bits
!= 10 ? max_rgb
: LLVMConstInt(ctx
->i32
, 3, 0);
1651 bool has_intrinsic
= HAVE_LLVM
>= 0x0600;
1654 if (!has_intrinsic
|| bits
!= 16) {
1655 for (int i
= 0; i
< 2; i
++) {
1656 bool alpha
= hi
&& i
== 1;
1657 args
[i
] = ac_build_umin(ctx
, args
[i
],
1658 alpha
? max_alpha
: max_rgb
);
1662 if (has_intrinsic
) {
1664 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pk.u16",
1665 ctx
->v2i16
, args
, 2,
1666 AC_FUNC_ATTR_READNONE
);
1667 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
1670 return ac_llvm_pack_two_int16(ctx
, args
);
1673 LLVMValueRef
ac_build_wqm_vote(struct ac_llvm_context
*ctx
, LLVMValueRef i1
)
1675 assert(HAVE_LLVM
>= 0x0600);
1676 return ac_build_intrinsic(ctx
, "llvm.amdgcn.wqm.vote", ctx
->i1
,
1677 &i1
, 1, AC_FUNC_ATTR_READNONE
);
1680 void ac_build_kill_if_false(struct ac_llvm_context
*ctx
, LLVMValueRef i1
)
1682 if (HAVE_LLVM
>= 0x0600) {
1683 ac_build_intrinsic(ctx
, "llvm.amdgcn.kill", ctx
->voidt
,
1688 LLVMValueRef value
= LLVMBuildSelect(ctx
->builder
, i1
,
1689 LLVMConstReal(ctx
->f32
, 1),
1690 LLVMConstReal(ctx
->f32
, -1), "");
1691 ac_build_intrinsic(ctx
, "llvm.AMDGPU.kill", ctx
->voidt
,
1692 &value
, 1, AC_FUNC_ATTR_LEGACY
);
1695 LLVMValueRef
ac_build_bfe(struct ac_llvm_context
*ctx
, LLVMValueRef input
,
1696 LLVMValueRef offset
, LLVMValueRef width
,
1699 LLVMValueRef args
[] = {
1705 if (HAVE_LLVM
>= 0x0500) {
1706 return ac_build_intrinsic(ctx
,
1707 is_signed
? "llvm.amdgcn.sbfe.i32" :
1708 "llvm.amdgcn.ubfe.i32",
1710 AC_FUNC_ATTR_READNONE
);
1713 return ac_build_intrinsic(ctx
,
1714 is_signed
? "llvm.AMDGPU.bfe.i32" :
1715 "llvm.AMDGPU.bfe.u32",
1717 AC_FUNC_ATTR_READNONE
|
1718 AC_FUNC_ATTR_LEGACY
);
1721 void ac_build_waitcnt(struct ac_llvm_context
*ctx
, unsigned simm16
)
1723 LLVMValueRef args
[1] = {
1724 LLVMConstInt(ctx
->i32
, simm16
, false),
1726 ac_build_intrinsic(ctx
, "llvm.amdgcn.s.waitcnt",
1727 ctx
->voidt
, args
, 1, 0);
1730 void ac_get_image_intr_name(const char *base_name
,
1731 LLVMTypeRef data_type
,
1732 LLVMTypeRef coords_type
,
1733 LLVMTypeRef rsrc_type
,
1734 char *out_name
, unsigned out_len
)
1736 char coords_type_name
[8];
1738 ac_build_type_name_for_intr(coords_type
, coords_type_name
,
1739 sizeof(coords_type_name
));
1741 if (HAVE_LLVM
<= 0x0309) {
1742 snprintf(out_name
, out_len
, "%s.%s", base_name
, coords_type_name
);
1744 char data_type_name
[8];
1745 char rsrc_type_name
[8];
1747 ac_build_type_name_for_intr(data_type
, data_type_name
,
1748 sizeof(data_type_name
));
1749 ac_build_type_name_for_intr(rsrc_type
, rsrc_type_name
,
1750 sizeof(rsrc_type_name
));
1751 snprintf(out_name
, out_len
, "%s.%s.%s.%s", base_name
,
1752 data_type_name
, coords_type_name
, rsrc_type_name
);
1756 #define AC_EXP_TARGET (HAVE_LLVM >= 0x0500 ? 0 : 3)
1757 #define AC_EXP_OUT0 (HAVE_LLVM >= 0x0500 ? 2 : 5)
1765 struct ac_vs_exp_chan
1769 enum ac_ir_type type
;
1772 struct ac_vs_exp_inst
{
1775 struct ac_vs_exp_chan chan
[4];
1778 struct ac_vs_exports
{
1780 struct ac_vs_exp_inst exp
[VARYING_SLOT_MAX
];
1783 /* Return true if the PARAM export has been eliminated. */
1784 static bool ac_eliminate_const_output(uint8_t *vs_output_param_offset
,
1785 uint32_t num_outputs
,
1786 struct ac_vs_exp_inst
*exp
)
1788 unsigned i
, default_val
; /* SPI_PS_INPUT_CNTL_i.DEFAULT_VAL */
1789 bool is_zero
[4] = {}, is_one
[4] = {};
1791 for (i
= 0; i
< 4; i
++) {
1792 /* It's a constant expression. Undef outputs are eliminated too. */
1793 if (exp
->chan
[i
].type
== AC_IR_UNDEF
) {
1796 } else if (exp
->chan
[i
].type
== AC_IR_CONST
) {
1797 if (exp
->chan
[i
].const_float
== 0)
1799 else if (exp
->chan
[i
].const_float
== 1)
1802 return false; /* other constant */
1807 /* Only certain combinations of 0 and 1 can be eliminated. */
1808 if (is_zero
[0] && is_zero
[1] && is_zero
[2])
1809 default_val
= is_zero
[3] ? 0 : 1;
1810 else if (is_one
[0] && is_one
[1] && is_one
[2])
1811 default_val
= is_zero
[3] ? 2 : 3;
1815 /* The PARAM export can be represented as DEFAULT_VAL. Kill it. */
1816 LLVMInstructionEraseFromParent(exp
->inst
);
1818 /* Change OFFSET to DEFAULT_VAL. */
1819 for (i
= 0; i
< num_outputs
; i
++) {
1820 if (vs_output_param_offset
[i
] == exp
->offset
) {
1821 vs_output_param_offset
[i
] =
1822 AC_EXP_PARAM_DEFAULT_VAL_0000
+ default_val
;
1829 static bool ac_eliminate_duplicated_output(uint8_t *vs_output_param_offset
,
1830 uint32_t num_outputs
,
1831 struct ac_vs_exports
*processed
,
1832 struct ac_vs_exp_inst
*exp
)
1834 unsigned p
, copy_back_channels
= 0;
1836 /* See if the output is already in the list of processed outputs.
1837 * The LLVMValueRef comparison relies on SSA.
1839 for (p
= 0; p
< processed
->num
; p
++) {
1840 bool different
= false;
1842 for (unsigned j
= 0; j
< 4; j
++) {
1843 struct ac_vs_exp_chan
*c1
= &processed
->exp
[p
].chan
[j
];
1844 struct ac_vs_exp_chan
*c2
= &exp
->chan
[j
];
1846 /* Treat undef as a match. */
1847 if (c2
->type
== AC_IR_UNDEF
)
1850 /* If c1 is undef but c2 isn't, we can copy c2 to c1
1851 * and consider the instruction duplicated.
1853 if (c1
->type
== AC_IR_UNDEF
) {
1854 copy_back_channels
|= 1 << j
;
1858 /* Test whether the channels are not equal. */
1859 if (c1
->type
!= c2
->type
||
1860 (c1
->type
== AC_IR_CONST
&&
1861 c1
->const_float
!= c2
->const_float
) ||
1862 (c1
->type
== AC_IR_VALUE
&&
1863 c1
->value
!= c2
->value
)) {
1871 copy_back_channels
= 0;
1873 if (p
== processed
->num
)
1876 /* If a match was found, but the matching export has undef where the new
1877 * one has a normal value, copy the normal value to the undef channel.
1879 struct ac_vs_exp_inst
*match
= &processed
->exp
[p
];
1881 while (copy_back_channels
) {
1882 unsigned chan
= u_bit_scan(©_back_channels
);
1884 assert(match
->chan
[chan
].type
== AC_IR_UNDEF
);
1885 LLVMSetOperand(match
->inst
, AC_EXP_OUT0
+ chan
,
1886 exp
->chan
[chan
].value
);
1887 match
->chan
[chan
] = exp
->chan
[chan
];
1890 /* The PARAM export is duplicated. Kill it. */
1891 LLVMInstructionEraseFromParent(exp
->inst
);
1893 /* Change OFFSET to the matching export. */
1894 for (unsigned i
= 0; i
< num_outputs
; i
++) {
1895 if (vs_output_param_offset
[i
] == exp
->offset
) {
1896 vs_output_param_offset
[i
] = match
->offset
;
1903 void ac_optimize_vs_outputs(struct ac_llvm_context
*ctx
,
1904 LLVMValueRef main_fn
,
1905 uint8_t *vs_output_param_offset
,
1906 uint32_t num_outputs
,
1907 uint8_t *num_param_exports
)
1909 LLVMBasicBlockRef bb
;
1910 bool removed_any
= false;
1911 struct ac_vs_exports exports
;
1915 /* Process all LLVM instructions. */
1916 bb
= LLVMGetFirstBasicBlock(main_fn
);
1918 LLVMValueRef inst
= LLVMGetFirstInstruction(bb
);
1921 LLVMValueRef cur
= inst
;
1922 inst
= LLVMGetNextInstruction(inst
);
1923 struct ac_vs_exp_inst exp
;
1925 if (LLVMGetInstructionOpcode(cur
) != LLVMCall
)
1928 LLVMValueRef callee
= ac_llvm_get_called_value(cur
);
1930 if (!ac_llvm_is_function(callee
))
1933 const char *name
= LLVMGetValueName(callee
);
1934 unsigned num_args
= LLVMCountParams(callee
);
1936 /* Check if this is an export instruction. */
1937 if ((num_args
!= 9 && num_args
!= 8) ||
1938 (strcmp(name
, "llvm.SI.export") &&
1939 strcmp(name
, "llvm.amdgcn.exp.f32")))
1942 LLVMValueRef arg
= LLVMGetOperand(cur
, AC_EXP_TARGET
);
1943 unsigned target
= LLVMConstIntGetZExtValue(arg
);
1945 if (target
< V_008DFC_SQ_EXP_PARAM
)
1948 target
-= V_008DFC_SQ_EXP_PARAM
;
1950 /* Parse the instruction. */
1951 memset(&exp
, 0, sizeof(exp
));
1952 exp
.offset
= target
;
1955 for (unsigned i
= 0; i
< 4; i
++) {
1956 LLVMValueRef v
= LLVMGetOperand(cur
, AC_EXP_OUT0
+ i
);
1958 exp
.chan
[i
].value
= v
;
1960 if (LLVMIsUndef(v
)) {
1961 exp
.chan
[i
].type
= AC_IR_UNDEF
;
1962 } else if (LLVMIsAConstantFP(v
)) {
1963 LLVMBool loses_info
;
1964 exp
.chan
[i
].type
= AC_IR_CONST
;
1965 exp
.chan
[i
].const_float
=
1966 LLVMConstRealGetDouble(v
, &loses_info
);
1968 exp
.chan
[i
].type
= AC_IR_VALUE
;
1972 /* Eliminate constant and duplicated PARAM exports. */
1973 if (ac_eliminate_const_output(vs_output_param_offset
,
1974 num_outputs
, &exp
) ||
1975 ac_eliminate_duplicated_output(vs_output_param_offset
,
1976 num_outputs
, &exports
,
1980 exports
.exp
[exports
.num
++] = exp
;
1983 bb
= LLVMGetNextBasicBlock(bb
);
1986 /* Remove holes in export memory due to removed PARAM exports.
1987 * This is done by renumbering all PARAM exports.
1990 uint8_t old_offset
[VARYING_SLOT_MAX
];
1993 /* Make a copy of the offsets. We need the old version while
1994 * we are modifying some of them. */
1995 memcpy(old_offset
, vs_output_param_offset
,
1996 sizeof(old_offset
));
1998 for (i
= 0; i
< exports
.num
; i
++) {
1999 unsigned offset
= exports
.exp
[i
].offset
;
2001 /* Update vs_output_param_offset. Multiple outputs can
2002 * have the same offset.
2004 for (out
= 0; out
< num_outputs
; out
++) {
2005 if (old_offset
[out
] == offset
)
2006 vs_output_param_offset
[out
] = i
;
2009 /* Change the PARAM offset in the instruction. */
2010 LLVMSetOperand(exports
.exp
[i
].inst
, AC_EXP_TARGET
,
2011 LLVMConstInt(ctx
->i32
,
2012 V_008DFC_SQ_EXP_PARAM
+ i
, 0));
2014 *num_param_exports
= exports
.num
;
2018 void ac_init_exec_full_mask(struct ac_llvm_context
*ctx
)
2020 LLVMValueRef full_mask
= LLVMConstInt(ctx
->i64
, ~0ull, 0);
2021 ac_build_intrinsic(ctx
,
2022 "llvm.amdgcn.init.exec", ctx
->voidt
,
2023 &full_mask
, 1, AC_FUNC_ATTR_CONVERGENT
);
2026 void ac_declare_lds_as_pointer(struct ac_llvm_context
*ctx
)
2028 unsigned lds_size
= ctx
->chip_class
>= CIK
? 65536 : 32768;
2029 ctx
->lds
= LLVMBuildIntToPtr(ctx
->builder
, ctx
->i32_0
,
2030 LLVMPointerType(LLVMArrayType(ctx
->i32
, lds_size
/ 4), AC_LOCAL_ADDR_SPACE
),
2034 LLVMValueRef
ac_lds_load(struct ac_llvm_context
*ctx
,
2035 LLVMValueRef dw_addr
)
2037 return ac_build_load(ctx
, ctx
->lds
, dw_addr
);
2040 void ac_lds_store(struct ac_llvm_context
*ctx
,
2041 LLVMValueRef dw_addr
,
2044 value
= ac_to_integer(ctx
, value
);
2045 ac_build_indexed_store(ctx
, ctx
->lds
,
2049 LLVMValueRef
ac_find_lsb(struct ac_llvm_context
*ctx
,
2050 LLVMTypeRef dst_type
,
2053 LLVMValueRef params
[2] = {
2056 /* The value of 1 means that ffs(x=0) = undef, so LLVM won't
2057 * add special code to check for x=0. The reason is that
2058 * the LLVM behavior for x=0 is different from what we
2059 * need here. However, LLVM also assumes that ffs(x) is
2060 * in [0, 31], but GLSL expects that ffs(0) = -1, so
2061 * a conditional assignment to handle 0 is still required.
2063 * The hardware already implements the correct behavior.
2065 LLVMConstInt(ctx
->i1
, 1, false),
2068 LLVMValueRef lsb
= ac_build_intrinsic(ctx
, "llvm.cttz.i32", ctx
->i32
,
2070 AC_FUNC_ATTR_READNONE
);
2072 /* TODO: We need an intrinsic to skip this conditional. */
2073 /* Check for zero: */
2074 return LLVMBuildSelect(ctx
->builder
, LLVMBuildICmp(ctx
->builder
,
2077 LLVMConstInt(ctx
->i32
, -1, 0), lsb
, "");
2080 LLVMTypeRef
ac_array_in_const_addr_space(LLVMTypeRef elem_type
)
2082 return LLVMPointerType(LLVMArrayType(elem_type
, 0),
2083 AC_CONST_ADDR_SPACE
);