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
= !(attrib_mask
& AC_FUNC_ATTR_LEGACY
);
225 function
= LLVMGetNamedFunction(ctx
->module
, name
);
227 LLVMTypeRef param_types
[32], function_type
;
230 assert(param_count
<= 32);
232 for (i
= 0; i
< param_count
; ++i
) {
234 param_types
[i
] = LLVMTypeOf(params
[i
]);
237 LLVMFunctionType(return_type
, param_types
, param_count
, 0);
238 function
= LLVMAddFunction(ctx
->module
, name
, function_type
);
240 LLVMSetFunctionCallConv(function
, LLVMCCallConv
);
241 LLVMSetLinkage(function
, LLVMExternalLinkage
);
243 if (!set_callsite_attrs
)
244 ac_add_func_attributes(ctx
->context
, function
, attrib_mask
);
247 call
= LLVMBuildCall(ctx
->builder
, function
, params
, param_count
, "");
248 if (set_callsite_attrs
)
249 ac_add_func_attributes(ctx
->context
, call
, attrib_mask
);
254 * Given the i32 or vNi32 \p type, generate the textual name (e.g. for use with
257 void ac_build_type_name_for_intr(LLVMTypeRef type
, char *buf
, unsigned bufsize
)
259 LLVMTypeRef elem_type
= type
;
261 assert(bufsize
>= 8);
263 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
) {
264 int ret
= snprintf(buf
, bufsize
, "v%u",
265 LLVMGetVectorSize(type
));
267 char *type_name
= LLVMPrintTypeToString(type
);
268 fprintf(stderr
, "Error building type name for: %s\n",
272 elem_type
= LLVMGetElementType(type
);
276 switch (LLVMGetTypeKind(elem_type
)) {
278 case LLVMIntegerTypeKind
:
279 snprintf(buf
, bufsize
, "i%d", LLVMGetIntTypeWidth(elem_type
));
281 case LLVMFloatTypeKind
:
282 snprintf(buf
, bufsize
, "f32");
284 case LLVMDoubleTypeKind
:
285 snprintf(buf
, bufsize
, "f64");
291 * Helper function that builds an LLVM IR PHI node and immediately adds
295 ac_build_phi(struct ac_llvm_context
*ctx
, LLVMTypeRef type
,
296 unsigned count_incoming
, LLVMValueRef
*values
,
297 LLVMBasicBlockRef
*blocks
)
299 LLVMValueRef phi
= LLVMBuildPhi(ctx
->builder
, type
, "");
300 LLVMAddIncoming(phi
, values
, blocks
, count_incoming
);
304 /* Prevent optimizations (at least of memory accesses) across the current
305 * point in the program by emitting empty inline assembly that is marked as
306 * having side effects.
308 * Optionally, a value can be passed through the inline assembly to prevent
309 * LLVM from hoisting calls to ReadNone functions.
312 ac_build_optimization_barrier(struct ac_llvm_context
*ctx
,
315 static int counter
= 0;
317 LLVMBuilderRef builder
= ctx
->builder
;
320 snprintf(code
, sizeof(code
), "; %d", p_atomic_inc_return(&counter
));
323 LLVMTypeRef ftype
= LLVMFunctionType(ctx
->voidt
, NULL
, 0, false);
324 LLVMValueRef inlineasm
= LLVMConstInlineAsm(ftype
, code
, "", true, false);
325 LLVMBuildCall(builder
, inlineasm
, NULL
, 0, "");
327 LLVMTypeRef ftype
= LLVMFunctionType(ctx
->i32
, &ctx
->i32
, 1, false);
328 LLVMValueRef inlineasm
= LLVMConstInlineAsm(ftype
, code
, "=v,0", true, false);
329 LLVMValueRef vgpr
= *pvgpr
;
330 LLVMTypeRef vgpr_type
= LLVMTypeOf(vgpr
);
331 unsigned vgpr_size
= ac_get_type_size(vgpr_type
);
334 assert(vgpr_size
% 4 == 0);
336 vgpr
= LLVMBuildBitCast(builder
, vgpr
, LLVMVectorType(ctx
->i32
, vgpr_size
/ 4), "");
337 vgpr0
= LLVMBuildExtractElement(builder
, vgpr
, ctx
->i32_0
, "");
338 vgpr0
= LLVMBuildCall(builder
, inlineasm
, &vgpr0
, 1, "");
339 vgpr
= LLVMBuildInsertElement(builder
, vgpr
, vgpr0
, ctx
->i32_0
, "");
340 vgpr
= LLVMBuildBitCast(builder
, vgpr
, vgpr_type
, "");
347 ac_build_shader_clock(struct ac_llvm_context
*ctx
)
349 LLVMValueRef tmp
= ac_build_intrinsic(ctx
, "llvm.readcyclecounter",
350 ctx
->i64
, NULL
, 0, 0);
351 return LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->v2i32
, "");
355 ac_build_ballot(struct ac_llvm_context
*ctx
,
358 LLVMValueRef args
[3] = {
361 LLVMConstInt(ctx
->i32
, LLVMIntNE
, 0)
364 /* We currently have no other way to prevent LLVM from lifting the icmp
365 * calls to a dominating basic block.
367 ac_build_optimization_barrier(ctx
, &args
[0]);
369 if (LLVMTypeOf(args
[0]) != ctx
->i32
)
370 args
[0] = LLVMBuildBitCast(ctx
->builder
, args
[0], ctx
->i32
, "");
372 return ac_build_intrinsic(ctx
,
373 "llvm.amdgcn.icmp.i32",
375 AC_FUNC_ATTR_NOUNWIND
|
376 AC_FUNC_ATTR_READNONE
|
377 AC_FUNC_ATTR_CONVERGENT
);
381 ac_build_vote_all(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
383 LLVMValueRef active_set
= ac_build_ballot(ctx
, ctx
->i32_1
);
384 LLVMValueRef vote_set
= ac_build_ballot(ctx
, value
);
385 return LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, vote_set
, active_set
, "");
389 ac_build_vote_any(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
391 LLVMValueRef vote_set
= ac_build_ballot(ctx
, value
);
392 return LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, vote_set
,
393 LLVMConstInt(ctx
->i64
, 0, 0), "");
397 ac_build_vote_eq(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
399 LLVMValueRef active_set
= ac_build_ballot(ctx
, ctx
->i32_1
);
400 LLVMValueRef vote_set
= ac_build_ballot(ctx
, value
);
402 LLVMValueRef all
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
403 vote_set
, active_set
, "");
404 LLVMValueRef none
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
406 LLVMConstInt(ctx
->i64
, 0, 0), "");
407 return LLVMBuildOr(ctx
->builder
, all
, none
, "");
411 ac_build_varying_gather_values(struct ac_llvm_context
*ctx
, LLVMValueRef
*values
,
412 unsigned value_count
, unsigned component
)
414 LLVMValueRef vec
= NULL
;
416 if (value_count
== 1) {
417 return values
[component
];
418 } else if (!value_count
)
419 unreachable("value_count is 0");
421 for (unsigned i
= component
; i
< value_count
+ component
; i
++) {
422 LLVMValueRef value
= values
[i
];
425 vec
= LLVMGetUndef( LLVMVectorType(LLVMTypeOf(value
), value_count
));
426 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, i
- component
, false);
427 vec
= LLVMBuildInsertElement(ctx
->builder
, vec
, value
, index
, "");
433 ac_build_gather_values_extended(struct ac_llvm_context
*ctx
,
434 LLVMValueRef
*values
,
435 unsigned value_count
,
436 unsigned value_stride
,
440 LLVMBuilderRef builder
= ctx
->builder
;
441 LLVMValueRef vec
= NULL
;
444 if (value_count
== 1 && !always_vector
) {
446 return LLVMBuildLoad(builder
, values
[0], "");
448 } else if (!value_count
)
449 unreachable("value_count is 0");
451 for (i
= 0; i
< value_count
; i
++) {
452 LLVMValueRef value
= values
[i
* value_stride
];
454 value
= LLVMBuildLoad(builder
, value
, "");
457 vec
= LLVMGetUndef( LLVMVectorType(LLVMTypeOf(value
), value_count
));
458 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, i
, false);
459 vec
= LLVMBuildInsertElement(builder
, vec
, value
, index
, "");
465 ac_build_gather_values(struct ac_llvm_context
*ctx
,
466 LLVMValueRef
*values
,
467 unsigned value_count
)
469 return ac_build_gather_values_extended(ctx
, values
, value_count
, 1, false, false);
472 /* Expand a scalar or vector to <4 x type> by filling the remaining channels
473 * with undef. Extract at most num_channels components from the input.
475 LLVMValueRef
ac_build_expand_to_vec4(struct ac_llvm_context
*ctx
,
477 unsigned num_channels
)
479 LLVMTypeRef elemtype
;
480 LLVMValueRef chan
[4];
482 if (LLVMGetTypeKind(LLVMTypeOf(value
)) == LLVMVectorTypeKind
) {
483 unsigned vec_size
= LLVMGetVectorSize(LLVMTypeOf(value
));
484 num_channels
= MIN2(num_channels
, vec_size
);
486 if (num_channels
>= 4)
489 for (unsigned i
= 0; i
< num_channels
; i
++)
490 chan
[i
] = ac_llvm_extract_elem(ctx
, value
, i
);
492 elemtype
= LLVMGetElementType(LLVMTypeOf(value
));
495 assert(num_channels
== 1);
498 elemtype
= LLVMTypeOf(value
);
501 while (num_channels
< 4)
502 chan
[num_channels
++] = LLVMGetUndef(elemtype
);
504 return ac_build_gather_values(ctx
, chan
, 4);
508 ac_build_fdiv(struct ac_llvm_context
*ctx
,
512 LLVMValueRef ret
= LLVMBuildFDiv(ctx
->builder
, num
, den
, "");
514 /* Use v_rcp_f32 instead of precise division. */
515 if (!LLVMIsConstant(ret
))
516 LLVMSetMetadata(ret
, ctx
->fpmath_md_kind
, ctx
->fpmath_md_2p5_ulp
);
520 /* Coordinates for cube map selection. sc, tc, and ma are as in Table 8.27
521 * of the OpenGL 4.5 (Compatibility Profile) specification, except ma is
522 * already multiplied by two. id is the cube face number.
524 struct cube_selection_coords
{
531 build_cube_intrinsic(struct ac_llvm_context
*ctx
,
533 struct cube_selection_coords
*out
)
535 LLVMTypeRef f32
= ctx
->f32
;
537 out
->stc
[1] = ac_build_intrinsic(ctx
, "llvm.amdgcn.cubetc",
538 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
539 out
->stc
[0] = ac_build_intrinsic(ctx
, "llvm.amdgcn.cubesc",
540 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
541 out
->ma
= ac_build_intrinsic(ctx
, "llvm.amdgcn.cubema",
542 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
543 out
->id
= ac_build_intrinsic(ctx
, "llvm.amdgcn.cubeid",
544 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
548 * Build a manual selection sequence for cube face sc/tc coordinates and
549 * major axis vector (multiplied by 2 for consistency) for the given
550 * vec3 \p coords, for the face implied by \p selcoords.
552 * For the major axis, we always adjust the sign to be in the direction of
553 * selcoords.ma; i.e., a positive out_ma means that coords is pointed towards
554 * the selcoords major axis.
556 static void build_cube_select(struct ac_llvm_context
*ctx
,
557 const struct cube_selection_coords
*selcoords
,
558 const LLVMValueRef
*coords
,
559 LLVMValueRef
*out_st
,
560 LLVMValueRef
*out_ma
)
562 LLVMBuilderRef builder
= ctx
->builder
;
563 LLVMTypeRef f32
= LLVMTypeOf(coords
[0]);
564 LLVMValueRef is_ma_positive
;
566 LLVMValueRef is_ma_z
, is_not_ma_z
;
567 LLVMValueRef is_ma_y
;
568 LLVMValueRef is_ma_x
;
572 is_ma_positive
= LLVMBuildFCmp(builder
, LLVMRealUGE
,
573 selcoords
->ma
, LLVMConstReal(f32
, 0.0), "");
574 sgn_ma
= LLVMBuildSelect(builder
, is_ma_positive
,
575 LLVMConstReal(f32
, 1.0), LLVMConstReal(f32
, -1.0), "");
577 is_ma_z
= LLVMBuildFCmp(builder
, LLVMRealUGE
, selcoords
->id
, LLVMConstReal(f32
, 4.0), "");
578 is_not_ma_z
= LLVMBuildNot(builder
, is_ma_z
, "");
579 is_ma_y
= LLVMBuildAnd(builder
, is_not_ma_z
,
580 LLVMBuildFCmp(builder
, LLVMRealUGE
, selcoords
->id
, LLVMConstReal(f32
, 2.0), ""), "");
581 is_ma_x
= LLVMBuildAnd(builder
, is_not_ma_z
, LLVMBuildNot(builder
, is_ma_y
, ""), "");
584 tmp
= LLVMBuildSelect(builder
, is_ma_x
, coords
[2], coords
[0], "");
585 sgn
= LLVMBuildSelect(builder
, is_ma_y
, LLVMConstReal(f32
, 1.0),
586 LLVMBuildSelect(builder
, is_ma_z
, sgn_ma
,
587 LLVMBuildFNeg(builder
, sgn_ma
, ""), ""), "");
588 out_st
[0] = LLVMBuildFMul(builder
, tmp
, sgn
, "");
591 tmp
= LLVMBuildSelect(builder
, is_ma_y
, coords
[2], coords
[1], "");
592 sgn
= LLVMBuildSelect(builder
, is_ma_y
, sgn_ma
,
593 LLVMConstReal(f32
, -1.0), "");
594 out_st
[1] = LLVMBuildFMul(builder
, tmp
, sgn
, "");
597 tmp
= LLVMBuildSelect(builder
, is_ma_z
, coords
[2],
598 LLVMBuildSelect(builder
, is_ma_y
, coords
[1], coords
[0], ""), "");
599 tmp
= ac_build_intrinsic(ctx
, "llvm.fabs.f32",
600 ctx
->f32
, &tmp
, 1, AC_FUNC_ATTR_READNONE
);
601 *out_ma
= LLVMBuildFMul(builder
, tmp
, LLVMConstReal(f32
, 2.0), "");
605 ac_prepare_cube_coords(struct ac_llvm_context
*ctx
,
606 bool is_deriv
, bool is_array
, bool is_lod
,
607 LLVMValueRef
*coords_arg
,
608 LLVMValueRef
*derivs_arg
)
611 LLVMBuilderRef builder
= ctx
->builder
;
612 struct cube_selection_coords selcoords
;
613 LLVMValueRef coords
[3];
616 if (is_array
&& !is_lod
) {
617 LLVMValueRef tmp
= coords_arg
[3];
618 tmp
= ac_build_intrinsic(ctx
, "llvm.rint.f32", ctx
->f32
, &tmp
, 1, 0);
620 /* Section 8.9 (Texture Functions) of the GLSL 4.50 spec says:
622 * "For Array forms, the array layer used will be
624 * max(0, min(d−1, floor(layer+0.5)))
626 * where d is the depth of the texture array and layer
627 * comes from the component indicated in the tables below.
628 * Workaroudn for an issue where the layer is taken from a
629 * helper invocation which happens to fall on a different
630 * layer due to extrapolation."
632 * VI and earlier attempt to implement this in hardware by
633 * clamping the value of coords[2] = (8 * layer) + face.
634 * Unfortunately, this means that the we end up with the wrong
635 * face when clamping occurs.
637 * Clamp the layer earlier to work around the issue.
639 if (ctx
->chip_class
<= VI
) {
641 ge0
= LLVMBuildFCmp(builder
, LLVMRealOGE
, tmp
, ctx
->f32_0
, "");
642 tmp
= LLVMBuildSelect(builder
, ge0
, tmp
, ctx
->f32_0
, "");
648 build_cube_intrinsic(ctx
, coords_arg
, &selcoords
);
650 invma
= ac_build_intrinsic(ctx
, "llvm.fabs.f32",
651 ctx
->f32
, &selcoords
.ma
, 1, AC_FUNC_ATTR_READNONE
);
652 invma
= ac_build_fdiv(ctx
, LLVMConstReal(ctx
->f32
, 1.0), invma
);
654 for (int i
= 0; i
< 2; ++i
)
655 coords
[i
] = LLVMBuildFMul(builder
, selcoords
.stc
[i
], invma
, "");
657 coords
[2] = selcoords
.id
;
659 if (is_deriv
&& derivs_arg
) {
660 LLVMValueRef derivs
[4];
663 /* Convert cube derivatives to 2D derivatives. */
664 for (axis
= 0; axis
< 2; axis
++) {
665 LLVMValueRef deriv_st
[2];
666 LLVMValueRef deriv_ma
;
668 /* Transform the derivative alongside the texture
669 * coordinate. Mathematically, the correct formula is
670 * as follows. Assume we're projecting onto the +Z face
671 * and denote by dx/dh the derivative of the (original)
672 * X texture coordinate with respect to horizontal
673 * window coordinates. The projection onto the +Z face
678 * Then df/dh = df/dx * dx/dh + df/dz * dz/dh
679 * = 1/z * dx/dh - x/z * 1/z * dz/dh.
681 * This motivatives the implementation below.
683 * Whether this actually gives the expected results for
684 * apps that might feed in derivatives obtained via
685 * finite differences is anyone's guess. The OpenGL spec
686 * seems awfully quiet about how textureGrad for cube
687 * maps should be handled.
689 build_cube_select(ctx
, &selcoords
, &derivs_arg
[axis
* 3],
690 deriv_st
, &deriv_ma
);
692 deriv_ma
= LLVMBuildFMul(builder
, deriv_ma
, invma
, "");
694 for (int i
= 0; i
< 2; ++i
)
695 derivs
[axis
* 2 + i
] =
696 LLVMBuildFSub(builder
,
697 LLVMBuildFMul(builder
, deriv_st
[i
], invma
, ""),
698 LLVMBuildFMul(builder
, deriv_ma
, coords
[i
], ""), "");
701 memcpy(derivs_arg
, derivs
, sizeof(derivs
));
704 /* Shift the texture coordinate. This must be applied after the
705 * derivative calculation.
707 for (int i
= 0; i
< 2; ++i
)
708 coords
[i
] = LLVMBuildFAdd(builder
, coords
[i
], LLVMConstReal(ctx
->f32
, 1.5), "");
711 /* for cube arrays coord.z = coord.w(array_index) * 8 + face */
712 /* coords_arg.w component - array_index for cube arrays */
713 LLVMValueRef tmp
= LLVMBuildFMul(ctx
->builder
, coords_arg
[3], LLVMConstReal(ctx
->f32
, 8.0), "");
714 coords
[2] = LLVMBuildFAdd(ctx
->builder
, tmp
, coords
[2], "");
717 memcpy(coords_arg
, coords
, sizeof(coords
));
722 ac_build_fs_interp(struct ac_llvm_context
*ctx
,
723 LLVMValueRef llvm_chan
,
724 LLVMValueRef attr_number
,
729 LLVMValueRef args
[5];
734 args
[2] = attr_number
;
737 p1
= ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p1",
738 ctx
->f32
, args
, 4, AC_FUNC_ATTR_READNONE
);
743 args
[3] = attr_number
;
746 return ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p2",
747 ctx
->f32
, args
, 5, AC_FUNC_ATTR_READNONE
);
751 ac_build_fs_interp_mov(struct ac_llvm_context
*ctx
,
752 LLVMValueRef parameter
,
753 LLVMValueRef llvm_chan
,
754 LLVMValueRef attr_number
,
757 LLVMValueRef args
[4];
761 args
[2] = attr_number
;
764 return ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.mov",
765 ctx
->f32
, args
, 4, AC_FUNC_ATTR_READNONE
);
769 ac_build_gep0(struct ac_llvm_context
*ctx
,
770 LLVMValueRef base_ptr
,
773 LLVMValueRef indices
[2] = {
774 LLVMConstInt(ctx
->i32
, 0, 0),
777 return LLVMBuildGEP(ctx
->builder
, base_ptr
,
782 ac_build_indexed_store(struct ac_llvm_context
*ctx
,
783 LLVMValueRef base_ptr
, LLVMValueRef index
,
786 LLVMBuildStore(ctx
->builder
, value
,
787 ac_build_gep0(ctx
, base_ptr
, index
));
791 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad.
792 * It's equivalent to doing a load from &base_ptr[index].
794 * \param base_ptr Where the array starts.
795 * \param index The element index into the array.
796 * \param uniform Whether the base_ptr and index can be assumed to be
797 * dynamically uniform (i.e. load to an SGPR)
798 * \param invariant Whether the load is invariant (no other opcodes affect it)
801 ac_build_load_custom(struct ac_llvm_context
*ctx
, LLVMValueRef base_ptr
,
802 LLVMValueRef index
, bool uniform
, bool invariant
)
804 LLVMValueRef pointer
, result
;
806 pointer
= ac_build_gep0(ctx
, base_ptr
, index
);
808 LLVMSetMetadata(pointer
, ctx
->uniform_md_kind
, ctx
->empty_md
);
809 result
= LLVMBuildLoad(ctx
->builder
, pointer
, "");
811 LLVMSetMetadata(result
, ctx
->invariant_load_md_kind
, ctx
->empty_md
);
815 LLVMValueRef
ac_build_load(struct ac_llvm_context
*ctx
, LLVMValueRef base_ptr
,
818 return ac_build_load_custom(ctx
, base_ptr
, index
, false, false);
821 LLVMValueRef
ac_build_load_invariant(struct ac_llvm_context
*ctx
,
822 LLVMValueRef base_ptr
, LLVMValueRef index
)
824 return ac_build_load_custom(ctx
, base_ptr
, index
, false, true);
827 LLVMValueRef
ac_build_load_to_sgpr(struct ac_llvm_context
*ctx
,
828 LLVMValueRef base_ptr
, LLVMValueRef index
)
830 return ac_build_load_custom(ctx
, base_ptr
, index
, true, true);
833 /* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
834 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
835 * or v4i32 (num_channels=3,4).
838 ac_build_buffer_store_dword(struct ac_llvm_context
*ctx
,
841 unsigned num_channels
,
842 LLVMValueRef voffset
,
843 LLVMValueRef soffset
,
844 unsigned inst_offset
,
847 bool writeonly_memory
,
848 bool swizzle_enable_hint
)
850 /* SWIZZLE_ENABLE requires that soffset isn't folded into voffset
851 * (voffset is swizzled, but soffset isn't swizzled).
852 * llvm.amdgcn.buffer.store doesn't have a separate soffset parameter.
854 if (!swizzle_enable_hint
) {
855 /* Split 3 channel stores, becase LLVM doesn't support 3-channel
857 if (num_channels
== 3) {
858 LLVMValueRef v
[3], v01
;
860 for (int i
= 0; i
< 3; i
++) {
861 v
[i
] = LLVMBuildExtractElement(ctx
->builder
, vdata
,
862 LLVMConstInt(ctx
->i32
, i
, 0), "");
864 v01
= ac_build_gather_values(ctx
, v
, 2);
866 ac_build_buffer_store_dword(ctx
, rsrc
, v01
, 2, voffset
,
867 soffset
, inst_offset
, glc
, slc
,
868 writeonly_memory
, swizzle_enable_hint
);
869 ac_build_buffer_store_dword(ctx
, rsrc
, v
[2], 1, voffset
,
870 soffset
, inst_offset
+ 8,
872 writeonly_memory
, swizzle_enable_hint
);
876 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
877 static const char *types
[] = {"f32", "v2f32", "v4f32"};
879 LLVMValueRef offset
= soffset
;
882 offset
= LLVMBuildAdd(ctx
->builder
, offset
,
883 LLVMConstInt(ctx
->i32
, inst_offset
, 0), "");
885 offset
= LLVMBuildAdd(ctx
->builder
, offset
, voffset
, "");
887 LLVMValueRef args
[] = {
888 ac_to_float(ctx
, vdata
),
889 LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, ""),
890 LLVMConstInt(ctx
->i32
, 0, 0),
892 LLVMConstInt(ctx
->i1
, glc
, 0),
893 LLVMConstInt(ctx
->i1
, slc
, 0),
896 snprintf(name
, sizeof(name
), "llvm.amdgcn.buffer.store.%s",
899 ac_build_intrinsic(ctx
, name
, ctx
->voidt
,
900 args
, ARRAY_SIZE(args
),
902 AC_FUNC_ATTR_INACCESSIBLE_MEM_ONLY
:
903 AC_FUNC_ATTR_WRITEONLY
);
907 static unsigned dfmt
[] = {
908 V_008F0C_BUF_DATA_FORMAT_32
,
909 V_008F0C_BUF_DATA_FORMAT_32_32
,
910 V_008F0C_BUF_DATA_FORMAT_32_32_32
,
911 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
913 assert(num_channels
>= 1 && num_channels
<= 4);
915 LLVMValueRef args
[] = {
918 LLVMConstInt(ctx
->i32
, num_channels
, 0),
919 voffset
? voffset
: LLVMGetUndef(ctx
->i32
),
921 LLVMConstInt(ctx
->i32
, inst_offset
, 0),
922 LLVMConstInt(ctx
->i32
, dfmt
[num_channels
- 1], 0),
923 LLVMConstInt(ctx
->i32
, V_008F0C_BUF_NUM_FORMAT_UINT
, 0),
924 LLVMConstInt(ctx
->i32
, voffset
!= NULL
, 0),
925 LLVMConstInt(ctx
->i32
, 0, 0), /* idxen */
926 LLVMConstInt(ctx
->i32
, glc
, 0),
927 LLVMConstInt(ctx
->i32
, slc
, 0),
928 LLVMConstInt(ctx
->i32
, 0, 0), /* tfe*/
931 /* The instruction offset field has 12 bits */
932 assert(voffset
|| inst_offset
< (1 << 12));
934 /* The intrinsic is overloaded, we need to add a type suffix for overloading to work. */
935 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
936 const char *types
[] = {"i32", "v2i32", "v4i32"};
938 snprintf(name
, sizeof(name
), "llvm.SI.tbuffer.store.%s", types
[func
]);
940 ac_build_intrinsic(ctx
, name
, ctx
->voidt
,
941 args
, ARRAY_SIZE(args
),
942 AC_FUNC_ATTR_LEGACY
);
946 ac_build_buffer_load_common(struct ac_llvm_context
*ctx
,
949 LLVMValueRef voffset
,
950 unsigned num_channels
,
956 LLVMValueRef args
[] = {
957 LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, ""),
958 vindex
? vindex
: LLVMConstInt(ctx
->i32
, 0, 0),
960 LLVMConstInt(ctx
->i1
, glc
, 0),
961 LLVMConstInt(ctx
->i1
, slc
, 0)
963 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
965 LLVMTypeRef types
[] = {ctx
->f32
, ctx
->v2f32
, ctx
->v4f32
};
966 const char *type_names
[] = {"f32", "v2f32", "v4f32"};
970 snprintf(name
, sizeof(name
), "llvm.amdgcn.buffer.load.format.%s",
973 snprintf(name
, sizeof(name
), "llvm.amdgcn.buffer.load.%s",
977 return ac_build_intrinsic(ctx
, name
, types
[func
], args
,
979 ac_get_load_intr_attribs(can_speculate
));
983 ac_build_buffer_load(struct ac_llvm_context
*ctx
,
987 LLVMValueRef voffset
,
988 LLVMValueRef soffset
,
989 unsigned inst_offset
,
995 LLVMValueRef offset
= LLVMConstInt(ctx
->i32
, inst_offset
, 0);
997 offset
= LLVMBuildAdd(ctx
->builder
, offset
, voffset
, "");
999 offset
= LLVMBuildAdd(ctx
->builder
, offset
, soffset
, "");
1001 /* TODO: VI and later generations can use SMEM with GLC=1.*/
1002 if (allow_smem
&& !glc
&& !slc
) {
1003 assert(vindex
== NULL
);
1005 LLVMValueRef result
[8];
1007 for (int i
= 0; i
< num_channels
; i
++) {
1009 offset
= LLVMBuildAdd(ctx
->builder
, offset
,
1010 LLVMConstInt(ctx
->i32
, 4, 0), "");
1012 LLVMValueRef args
[2] = {rsrc
, offset
};
1013 result
[i
] = ac_build_intrinsic(ctx
, "llvm.SI.load.const.v4i32",
1015 AC_FUNC_ATTR_READNONE
|
1016 AC_FUNC_ATTR_LEGACY
);
1018 if (num_channels
== 1)
1021 if (num_channels
== 3)
1022 result
[num_channels
++] = LLVMGetUndef(ctx
->f32
);
1023 return ac_build_gather_values(ctx
, result
, num_channels
);
1026 return ac_build_buffer_load_common(ctx
, rsrc
, vindex
, offset
,
1027 num_channels
, glc
, slc
,
1028 can_speculate
, false);
1031 LLVMValueRef
ac_build_buffer_load_format(struct ac_llvm_context
*ctx
,
1033 LLVMValueRef vindex
,
1034 LLVMValueRef voffset
,
1035 unsigned num_channels
,
1039 return ac_build_buffer_load_common(ctx
, rsrc
, vindex
, voffset
,
1040 num_channels
, glc
, false,
1041 can_speculate
, true);
1045 * Set range metadata on an instruction. This can only be used on load and
1046 * call instructions. If you know an instruction can only produce the values
1047 * 0, 1, 2, you would do set_range_metadata(value, 0, 3);
1048 * \p lo is the minimum value inclusive.
1049 * \p hi is the maximum value exclusive.
1051 static void set_range_metadata(struct ac_llvm_context
*ctx
,
1052 LLVMValueRef value
, unsigned lo
, unsigned hi
)
1054 LLVMValueRef range_md
, md_args
[2];
1055 LLVMTypeRef type
= LLVMTypeOf(value
);
1056 LLVMContextRef context
= LLVMGetTypeContext(type
);
1058 md_args
[0] = LLVMConstInt(type
, lo
, false);
1059 md_args
[1] = LLVMConstInt(type
, hi
, false);
1060 range_md
= LLVMMDNodeInContext(context
, md_args
, 2);
1061 LLVMSetMetadata(value
, ctx
->range_md_kind
, range_md
);
1065 ac_get_thread_id(struct ac_llvm_context
*ctx
)
1069 LLVMValueRef tid_args
[2];
1070 tid_args
[0] = LLVMConstInt(ctx
->i32
, 0xffffffff, false);
1071 tid_args
[1] = LLVMConstInt(ctx
->i32
, 0, false);
1072 tid_args
[1] = ac_build_intrinsic(ctx
,
1073 "llvm.amdgcn.mbcnt.lo", ctx
->i32
,
1074 tid_args
, 2, AC_FUNC_ATTR_READNONE
);
1076 tid
= ac_build_intrinsic(ctx
, "llvm.amdgcn.mbcnt.hi",
1078 2, AC_FUNC_ATTR_READNONE
);
1079 set_range_metadata(ctx
, tid
, 0, 64);
1084 * SI implements derivatives using the local data store (LDS)
1085 * All writes to the LDS happen in all executing threads at
1086 * the same time. TID is the Thread ID for the current
1087 * thread and is a value between 0 and 63, representing
1088 * the thread's position in the wavefront.
1090 * For the pixel shader threads are grouped into quads of four pixels.
1091 * The TIDs of the pixels of a quad are:
1099 * So, masking the TID with 0xfffffffc yields the TID of the top left pixel
1100 * of the quad, masking with 0xfffffffd yields the TID of the top pixel of
1101 * the current pixel's column, and masking with 0xfffffffe yields the TID
1102 * of the left pixel of the current pixel's row.
1104 * Adding 1 yields the TID of the pixel to the right of the left pixel, and
1105 * adding 2 yields the TID of the pixel below the top pixel.
1108 ac_build_ddxy(struct ac_llvm_context
*ctx
,
1113 LLVMValueRef tl
, trbl
, args
[2];
1114 LLVMValueRef result
;
1116 if (ctx
->chip_class
>= VI
) {
1117 LLVMValueRef thread_id
, tl_tid
, trbl_tid
;
1118 thread_id
= ac_get_thread_id(ctx
);
1120 tl_tid
= LLVMBuildAnd(ctx
->builder
, thread_id
,
1121 LLVMConstInt(ctx
->i32
, mask
, false), "");
1123 trbl_tid
= LLVMBuildAdd(ctx
->builder
, tl_tid
,
1124 LLVMConstInt(ctx
->i32
, idx
, false), "");
1126 args
[0] = LLVMBuildMul(ctx
->builder
, tl_tid
,
1127 LLVMConstInt(ctx
->i32
, 4, false), "");
1129 tl
= ac_build_intrinsic(ctx
,
1130 "llvm.amdgcn.ds.bpermute", ctx
->i32
,
1132 AC_FUNC_ATTR_READNONE
|
1133 AC_FUNC_ATTR_CONVERGENT
);
1135 args
[0] = LLVMBuildMul(ctx
->builder
, trbl_tid
,
1136 LLVMConstInt(ctx
->i32
, 4, false), "");
1137 trbl
= ac_build_intrinsic(ctx
,
1138 "llvm.amdgcn.ds.bpermute", ctx
->i32
,
1140 AC_FUNC_ATTR_READNONE
|
1141 AC_FUNC_ATTR_CONVERGENT
);
1143 uint32_t masks
[2] = {};
1146 case AC_TID_MASK_TOP_LEFT
:
1154 case AC_TID_MASK_TOP
:
1158 case AC_TID_MASK_LEFT
:
1167 args
[1] = LLVMConstInt(ctx
->i32
, masks
[0], false);
1169 tl
= ac_build_intrinsic(ctx
,
1170 "llvm.amdgcn.ds.swizzle", ctx
->i32
,
1172 AC_FUNC_ATTR_READNONE
|
1173 AC_FUNC_ATTR_CONVERGENT
);
1175 args
[1] = LLVMConstInt(ctx
->i32
, masks
[1], false);
1176 trbl
= ac_build_intrinsic(ctx
,
1177 "llvm.amdgcn.ds.swizzle", ctx
->i32
,
1179 AC_FUNC_ATTR_READNONE
|
1180 AC_FUNC_ATTR_CONVERGENT
);
1183 tl
= LLVMBuildBitCast(ctx
->builder
, tl
, ctx
->f32
, "");
1184 trbl
= LLVMBuildBitCast(ctx
->builder
, trbl
, ctx
->f32
, "");
1185 result
= LLVMBuildFSub(ctx
->builder
, trbl
, tl
, "");
1190 ac_build_sendmsg(struct ac_llvm_context
*ctx
,
1192 LLVMValueRef wave_id
)
1194 LLVMValueRef args
[2];
1195 args
[0] = LLVMConstInt(ctx
->i32
, msg
, false);
1197 ac_build_intrinsic(ctx
, "llvm.amdgcn.s.sendmsg", ctx
->voidt
, args
, 2, 0);
1201 ac_build_imsb(struct ac_llvm_context
*ctx
,
1203 LLVMTypeRef dst_type
)
1205 LLVMValueRef msb
= ac_build_intrinsic(ctx
, "llvm.amdgcn.sffbh.i32",
1207 AC_FUNC_ATTR_READNONE
);
1209 /* The HW returns the last bit index from MSB, but NIR/TGSI wants
1210 * the index from LSB. Invert it by doing "31 - msb". */
1211 msb
= LLVMBuildSub(ctx
->builder
, LLVMConstInt(ctx
->i32
, 31, false),
1214 LLVMValueRef all_ones
= LLVMConstInt(ctx
->i32
, -1, true);
1215 LLVMValueRef cond
= LLVMBuildOr(ctx
->builder
,
1216 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
1217 arg
, LLVMConstInt(ctx
->i32
, 0, 0), ""),
1218 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
1219 arg
, all_ones
, ""), "");
1221 return LLVMBuildSelect(ctx
->builder
, cond
, all_ones
, msb
, "");
1225 ac_build_umsb(struct ac_llvm_context
*ctx
,
1227 LLVMTypeRef dst_type
)
1229 LLVMValueRef args
[2] = {
1233 LLVMValueRef msb
= ac_build_intrinsic(ctx
, "llvm.ctlz.i32",
1234 dst_type
, args
, ARRAY_SIZE(args
),
1235 AC_FUNC_ATTR_READNONE
);
1237 /* The HW returns the last bit index from MSB, but TGSI/NIR wants
1238 * the index from LSB. Invert it by doing "31 - msb". */
1239 msb
= LLVMBuildSub(ctx
->builder
, LLVMConstInt(ctx
->i32
, 31, false),
1242 /* check for zero */
1243 return LLVMBuildSelect(ctx
->builder
,
1244 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, arg
,
1245 LLVMConstInt(ctx
->i32
, 0, 0), ""),
1246 LLVMConstInt(ctx
->i32
, -1, true), msb
, "");
1249 LLVMValueRef
ac_build_fmin(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
1252 LLVMValueRef args
[2] = {a
, b
};
1253 return ac_build_intrinsic(ctx
, "llvm.minnum.f32", ctx
->f32
, args
, 2,
1254 AC_FUNC_ATTR_READNONE
);
1257 LLVMValueRef
ac_build_fmax(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
1260 LLVMValueRef args
[2] = {a
, b
};
1261 return ac_build_intrinsic(ctx
, "llvm.maxnum.f32", ctx
->f32
, args
, 2,
1262 AC_FUNC_ATTR_READNONE
);
1265 LLVMValueRef
ac_build_imin(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
1268 LLVMValueRef cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSLE
, a
, b
, "");
1269 return LLVMBuildSelect(ctx
->builder
, cmp
, a
, b
, "");
1272 LLVMValueRef
ac_build_imax(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
1275 LLVMValueRef cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGT
, a
, b
, "");
1276 return LLVMBuildSelect(ctx
->builder
, cmp
, a
, b
, "");
1279 LLVMValueRef
ac_build_umin(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
1282 LLVMValueRef cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntULE
, a
, b
, "");
1283 return LLVMBuildSelect(ctx
->builder
, cmp
, a
, b
, "");
1286 LLVMValueRef
ac_build_clamp(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
1288 if (HAVE_LLVM
>= 0x0500) {
1289 return ac_build_fmin(ctx
, ac_build_fmax(ctx
, value
, ctx
->f32_0
),
1293 LLVMValueRef args
[3] = {
1295 LLVMConstReal(ctx
->f32
, 0),
1296 LLVMConstReal(ctx
->f32
, 1),
1299 return ac_build_intrinsic(ctx
, "llvm.AMDGPU.clamp.", ctx
->f32
, args
, 3,
1300 AC_FUNC_ATTR_READNONE
|
1301 AC_FUNC_ATTR_LEGACY
);
1304 void ac_build_export(struct ac_llvm_context
*ctx
, struct ac_export_args
*a
)
1306 LLVMValueRef args
[9];
1308 if (HAVE_LLVM
>= 0x0500) {
1309 args
[0] = LLVMConstInt(ctx
->i32
, a
->target
, 0);
1310 args
[1] = LLVMConstInt(ctx
->i32
, a
->enabled_channels
, 0);
1313 LLVMTypeRef i16
= LLVMInt16TypeInContext(ctx
->context
);
1314 LLVMTypeRef v2i16
= LLVMVectorType(i16
, 2);
1316 args
[2] = LLVMBuildBitCast(ctx
->builder
, a
->out
[0],
1318 args
[3] = LLVMBuildBitCast(ctx
->builder
, a
->out
[1],
1320 args
[4] = LLVMConstInt(ctx
->i1
, a
->done
, 0);
1321 args
[5] = LLVMConstInt(ctx
->i1
, a
->valid_mask
, 0);
1323 ac_build_intrinsic(ctx
, "llvm.amdgcn.exp.compr.v2i16",
1324 ctx
->voidt
, args
, 6, 0);
1326 args
[2] = a
->out
[0];
1327 args
[3] = a
->out
[1];
1328 args
[4] = a
->out
[2];
1329 args
[5] = a
->out
[3];
1330 args
[6] = LLVMConstInt(ctx
->i1
, a
->done
, 0);
1331 args
[7] = LLVMConstInt(ctx
->i1
, a
->valid_mask
, 0);
1333 ac_build_intrinsic(ctx
, "llvm.amdgcn.exp.f32",
1334 ctx
->voidt
, args
, 8, 0);
1339 args
[0] = LLVMConstInt(ctx
->i32
, a
->enabled_channels
, 0);
1340 args
[1] = LLVMConstInt(ctx
->i32
, a
->valid_mask
, 0);
1341 args
[2] = LLVMConstInt(ctx
->i32
, a
->done
, 0);
1342 args
[3] = LLVMConstInt(ctx
->i32
, a
->target
, 0);
1343 args
[4] = LLVMConstInt(ctx
->i32
, a
->compr
, 0);
1344 memcpy(args
+ 5, a
->out
, sizeof(a
->out
[0]) * 4);
1346 ac_build_intrinsic(ctx
, "llvm.SI.export", ctx
->voidt
, args
, 9,
1347 AC_FUNC_ATTR_LEGACY
);
1350 void ac_build_export_null(struct ac_llvm_context
*ctx
)
1352 struct ac_export_args args
;
1354 args
.enabled_channels
= 0x0; /* enabled channels */
1355 args
.valid_mask
= 1; /* whether the EXEC mask is valid */
1356 args
.done
= 1; /* DONE bit */
1357 args
.target
= V_008DFC_SQ_EXP_NULL
;
1358 args
.compr
= 0; /* COMPR flag (0 = 32-bit export) */
1359 args
.out
[0] = LLVMGetUndef(ctx
->f32
); /* R */
1360 args
.out
[1] = LLVMGetUndef(ctx
->f32
); /* G */
1361 args
.out
[2] = LLVMGetUndef(ctx
->f32
); /* B */
1362 args
.out
[3] = LLVMGetUndef(ctx
->f32
); /* A */
1364 ac_build_export(ctx
, &args
);
1367 LLVMValueRef
ac_build_image_opcode(struct ac_llvm_context
*ctx
,
1368 struct ac_image_args
*a
)
1370 LLVMValueRef args
[11];
1371 unsigned num_args
= 0;
1372 const char *name
= NULL
;
1373 char intr_name
[128], type
[64];
1375 bool sample
= a
->opcode
== ac_image_sample
||
1376 a
->opcode
== ac_image_gather4
||
1377 a
->opcode
== ac_image_get_lod
;
1380 args
[num_args
++] = ac_to_float(ctx
, a
->addr
);
1382 args
[num_args
++] = a
->addr
;
1384 args
[num_args
++] = a
->resource
;
1386 args
[num_args
++] = a
->sampler
;
1387 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->dmask
, 0);
1389 args
[num_args
++] = LLVMConstInt(ctx
->i1
, a
->unorm
, 0);
1390 args
[num_args
++] = ctx
->i1false
; /* glc */
1391 args
[num_args
++] = ctx
->i1false
; /* slc */
1392 args
[num_args
++] = ctx
->i1false
; /* lwe */
1393 args
[num_args
++] = LLVMConstInt(ctx
->i1
, a
->da
, 0);
1395 switch (a
->opcode
) {
1396 case ac_image_sample
:
1397 name
= "llvm.amdgcn.image.sample";
1399 case ac_image_gather4
:
1400 name
= "llvm.amdgcn.image.gather4";
1403 name
= "llvm.amdgcn.image.load";
1405 case ac_image_load_mip
:
1406 name
= "llvm.amdgcn.image.load.mip";
1408 case ac_image_get_lod
:
1409 name
= "llvm.amdgcn.image.getlod";
1411 case ac_image_get_resinfo
:
1412 name
= "llvm.amdgcn.image.getresinfo";
1415 unreachable("invalid image opcode");
1418 ac_build_type_name_for_intr(LLVMTypeOf(args
[0]), type
,
1421 snprintf(intr_name
, sizeof(intr_name
), "%s%s%s%s.v4f32.%s.v8i32",
1423 a
->compare
? ".c" : "",
1427 a
->level_zero
? ".lz" : "",
1428 a
->offset
? ".o" : "",
1431 LLVMValueRef result
=
1432 ac_build_intrinsic(ctx
, intr_name
,
1433 ctx
->v4f32
, args
, num_args
,
1434 AC_FUNC_ATTR_READNONE
);
1436 result
= LLVMBuildBitCast(ctx
->builder
, result
,
1442 LLVMValueRef
ac_build_cvt_pkrtz_f16(struct ac_llvm_context
*ctx
,
1443 LLVMValueRef args
[2])
1445 if (HAVE_LLVM
>= 0x0500) {
1447 LLVMVectorType(LLVMHalfTypeInContext(ctx
->context
), 2);
1449 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pkrtz",
1451 AC_FUNC_ATTR_READNONE
);
1452 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
1455 return ac_build_intrinsic(ctx
, "llvm.SI.packf16", ctx
->i32
, args
, 2,
1456 AC_FUNC_ATTR_READNONE
|
1457 AC_FUNC_ATTR_LEGACY
);
1460 /* Upper 16 bits must be zero. */
1461 static LLVMValueRef
ac_llvm_pack_two_int16(struct ac_llvm_context
*ctx
,
1462 LLVMValueRef val
[2])
1464 return LLVMBuildOr(ctx
->builder
, val
[0],
1465 LLVMBuildShl(ctx
->builder
, val
[1],
1466 LLVMConstInt(ctx
->i32
, 16, 0),
1470 /* Upper 16 bits are ignored and will be dropped. */
1471 static LLVMValueRef
ac_llvm_pack_two_int32_as_int16(struct ac_llvm_context
*ctx
,
1472 LLVMValueRef val
[2])
1474 LLVMValueRef v
[2] = {
1475 LLVMBuildAnd(ctx
->builder
, val
[0],
1476 LLVMConstInt(ctx
->i32
, 0xffff, 0), ""),
1479 return ac_llvm_pack_two_int16(ctx
, v
);
1482 LLVMValueRef
ac_build_cvt_pknorm_i16(struct ac_llvm_context
*ctx
,
1483 LLVMValueRef args
[2])
1485 if (HAVE_LLVM
>= 0x0600) {
1487 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pknorm.i16",
1488 ctx
->v2i16
, args
, 2,
1489 AC_FUNC_ATTR_READNONE
);
1490 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
1493 LLVMValueRef val
[2];
1495 for (int chan
= 0; chan
< 2; chan
++) {
1496 /* Clamp between [-1, 1]. */
1497 val
[chan
] = ac_build_fmin(ctx
, args
[chan
], ctx
->f32_1
);
1498 val
[chan
] = ac_build_fmax(ctx
, val
[chan
], LLVMConstReal(ctx
->f32
, -1));
1499 /* Convert to a signed integer in [-32767, 32767]. */
1500 val
[chan
] = LLVMBuildFMul(ctx
->builder
, val
[chan
],
1501 LLVMConstReal(ctx
->f32
, 32767), "");
1502 /* If positive, add 0.5, else add -0.5. */
1503 val
[chan
] = LLVMBuildFAdd(ctx
->builder
, val
[chan
],
1504 LLVMBuildSelect(ctx
->builder
,
1505 LLVMBuildFCmp(ctx
->builder
, LLVMRealOGE
,
1506 val
[chan
], ctx
->f32_0
, ""),
1507 LLVMConstReal(ctx
->f32
, 0.5),
1508 LLVMConstReal(ctx
->f32
, -0.5), ""), "");
1509 val
[chan
] = LLVMBuildFPToSI(ctx
->builder
, val
[chan
], ctx
->i32
, "");
1511 return ac_llvm_pack_two_int32_as_int16(ctx
, val
);
1514 LLVMValueRef
ac_build_cvt_pknorm_u16(struct ac_llvm_context
*ctx
,
1515 LLVMValueRef args
[2])
1517 if (HAVE_LLVM
>= 0x0600) {
1519 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pknorm.u16",
1520 ctx
->v2i16
, args
, 2,
1521 AC_FUNC_ATTR_READNONE
);
1522 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
1525 LLVMValueRef val
[2];
1527 for (int chan
= 0; chan
< 2; chan
++) {
1528 val
[chan
] = ac_build_clamp(ctx
, args
[chan
]);
1529 val
[chan
] = LLVMBuildFMul(ctx
->builder
, val
[chan
],
1530 LLVMConstReal(ctx
->f32
, 65535), "");
1531 val
[chan
] = LLVMBuildFAdd(ctx
->builder
, val
[chan
],
1532 LLVMConstReal(ctx
->f32
, 0.5), "");
1533 val
[chan
] = LLVMBuildFPToUI(ctx
->builder
, val
[chan
],
1536 return ac_llvm_pack_two_int32_as_int16(ctx
, val
);
1539 /* The 8-bit and 10-bit clamping is for HW workarounds. */
1540 LLVMValueRef
ac_build_cvt_pk_i16(struct ac_llvm_context
*ctx
,
1541 LLVMValueRef args
[2], unsigned bits
, bool hi
)
1543 assert(bits
== 8 || bits
== 10 || bits
== 16);
1545 LLVMValueRef max_rgb
= LLVMConstInt(ctx
->i32
,
1546 bits
== 8 ? 127 : bits
== 10 ? 511 : 32767, 0);
1547 LLVMValueRef min_rgb
= LLVMConstInt(ctx
->i32
,
1548 bits
== 8 ? -128 : bits
== 10 ? -512 : -32768, 0);
1549 LLVMValueRef max_alpha
=
1550 bits
!= 10 ? max_rgb
: ctx
->i32_1
;
1551 LLVMValueRef min_alpha
=
1552 bits
!= 10 ? min_rgb
: LLVMConstInt(ctx
->i32
, -2, 0);
1553 bool has_intrinsic
= HAVE_LLVM
>= 0x0600;
1556 if (!has_intrinsic
|| bits
!= 16) {
1557 for (int i
= 0; i
< 2; i
++) {
1558 bool alpha
= hi
&& i
== 1;
1559 args
[i
] = ac_build_imin(ctx
, args
[i
],
1560 alpha
? max_alpha
: max_rgb
);
1561 args
[i
] = ac_build_imax(ctx
, args
[i
],
1562 alpha
? min_alpha
: min_rgb
);
1566 if (has_intrinsic
) {
1568 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pk.i16",
1569 ctx
->v2i16
, args
, 2,
1570 AC_FUNC_ATTR_READNONE
);
1571 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
1574 return ac_llvm_pack_two_int32_as_int16(ctx
, args
);
1577 /* The 8-bit and 10-bit clamping is for HW workarounds. */
1578 LLVMValueRef
ac_build_cvt_pk_u16(struct ac_llvm_context
*ctx
,
1579 LLVMValueRef args
[2], unsigned bits
, bool hi
)
1581 assert(bits
== 8 || bits
== 10 || bits
== 16);
1583 LLVMValueRef max_rgb
= LLVMConstInt(ctx
->i32
,
1584 bits
== 8 ? 255 : bits
== 10 ? 1023 : 65535, 0);
1585 LLVMValueRef max_alpha
=
1586 bits
!= 10 ? max_rgb
: LLVMConstInt(ctx
->i32
, 3, 0);
1587 bool has_intrinsic
= HAVE_LLVM
>= 0x0600;
1590 if (!has_intrinsic
|| bits
!= 16) {
1591 for (int i
= 0; i
< 2; i
++) {
1592 bool alpha
= hi
&& i
== 1;
1593 args
[i
] = ac_build_umin(ctx
, args
[i
],
1594 alpha
? max_alpha
: max_rgb
);
1598 if (has_intrinsic
) {
1600 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pk.u16",
1601 ctx
->v2i16
, args
, 2,
1602 AC_FUNC_ATTR_READNONE
);
1603 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
1606 return ac_llvm_pack_two_int16(ctx
, args
);
1609 LLVMValueRef
ac_build_wqm_vote(struct ac_llvm_context
*ctx
, LLVMValueRef i1
)
1611 assert(HAVE_LLVM
>= 0x0600);
1612 return ac_build_intrinsic(ctx
, "llvm.amdgcn.wqm.vote", ctx
->i1
,
1613 &i1
, 1, AC_FUNC_ATTR_READNONE
);
1616 void ac_build_kill_if_false(struct ac_llvm_context
*ctx
, LLVMValueRef i1
)
1618 if (HAVE_LLVM
>= 0x0600) {
1619 ac_build_intrinsic(ctx
, "llvm.amdgcn.kill", ctx
->voidt
,
1624 LLVMValueRef value
= LLVMBuildSelect(ctx
->builder
, i1
,
1625 LLVMConstReal(ctx
->f32
, 1),
1626 LLVMConstReal(ctx
->f32
, -1), "");
1627 ac_build_intrinsic(ctx
, "llvm.AMDGPU.kill", ctx
->voidt
,
1628 &value
, 1, AC_FUNC_ATTR_LEGACY
);
1631 LLVMValueRef
ac_build_bfe(struct ac_llvm_context
*ctx
, LLVMValueRef input
,
1632 LLVMValueRef offset
, LLVMValueRef width
,
1635 LLVMValueRef args
[] = {
1641 if (HAVE_LLVM
>= 0x0500) {
1642 return ac_build_intrinsic(ctx
,
1643 is_signed
? "llvm.amdgcn.sbfe.i32" :
1644 "llvm.amdgcn.ubfe.i32",
1646 AC_FUNC_ATTR_READNONE
);
1649 return ac_build_intrinsic(ctx
,
1650 is_signed
? "llvm.AMDGPU.bfe.i32" :
1651 "llvm.AMDGPU.bfe.u32",
1653 AC_FUNC_ATTR_READNONE
|
1654 AC_FUNC_ATTR_LEGACY
);
1657 void ac_build_waitcnt(struct ac_llvm_context
*ctx
, unsigned simm16
)
1659 LLVMValueRef args
[1] = {
1660 LLVMConstInt(ctx
->i32
, simm16
, false),
1662 ac_build_intrinsic(ctx
, "llvm.amdgcn.s.waitcnt",
1663 ctx
->voidt
, args
, 1, 0);
1666 void ac_get_image_intr_name(const char *base_name
,
1667 LLVMTypeRef data_type
,
1668 LLVMTypeRef coords_type
,
1669 LLVMTypeRef rsrc_type
,
1670 char *out_name
, unsigned out_len
)
1672 char coords_type_name
[8];
1674 ac_build_type_name_for_intr(coords_type
, coords_type_name
,
1675 sizeof(coords_type_name
));
1677 char data_type_name
[8];
1678 char rsrc_type_name
[8];
1680 ac_build_type_name_for_intr(data_type
, data_type_name
,
1681 sizeof(data_type_name
));
1682 ac_build_type_name_for_intr(rsrc_type
, rsrc_type_name
,
1683 sizeof(rsrc_type_name
));
1684 snprintf(out_name
, out_len
, "%s.%s.%s.%s", base_name
,
1685 data_type_name
, coords_type_name
, rsrc_type_name
);
1688 #define AC_EXP_TARGET (HAVE_LLVM >= 0x0500 ? 0 : 3)
1689 #define AC_EXP_OUT0 (HAVE_LLVM >= 0x0500 ? 2 : 5)
1697 struct ac_vs_exp_chan
1701 enum ac_ir_type type
;
1704 struct ac_vs_exp_inst
{
1707 struct ac_vs_exp_chan chan
[4];
1710 struct ac_vs_exports
{
1712 struct ac_vs_exp_inst exp
[VARYING_SLOT_MAX
];
1715 /* Return true if the PARAM export has been eliminated. */
1716 static bool ac_eliminate_const_output(uint8_t *vs_output_param_offset
,
1717 uint32_t num_outputs
,
1718 struct ac_vs_exp_inst
*exp
)
1720 unsigned i
, default_val
; /* SPI_PS_INPUT_CNTL_i.DEFAULT_VAL */
1721 bool is_zero
[4] = {}, is_one
[4] = {};
1723 for (i
= 0; i
< 4; i
++) {
1724 /* It's a constant expression. Undef outputs are eliminated too. */
1725 if (exp
->chan
[i
].type
== AC_IR_UNDEF
) {
1728 } else if (exp
->chan
[i
].type
== AC_IR_CONST
) {
1729 if (exp
->chan
[i
].const_float
== 0)
1731 else if (exp
->chan
[i
].const_float
== 1)
1734 return false; /* other constant */
1739 /* Only certain combinations of 0 and 1 can be eliminated. */
1740 if (is_zero
[0] && is_zero
[1] && is_zero
[2])
1741 default_val
= is_zero
[3] ? 0 : 1;
1742 else if (is_one
[0] && is_one
[1] && is_one
[2])
1743 default_val
= is_zero
[3] ? 2 : 3;
1747 /* The PARAM export can be represented as DEFAULT_VAL. Kill it. */
1748 LLVMInstructionEraseFromParent(exp
->inst
);
1750 /* Change OFFSET to DEFAULT_VAL. */
1751 for (i
= 0; i
< num_outputs
; i
++) {
1752 if (vs_output_param_offset
[i
] == exp
->offset
) {
1753 vs_output_param_offset
[i
] =
1754 AC_EXP_PARAM_DEFAULT_VAL_0000
+ default_val
;
1761 static bool ac_eliminate_duplicated_output(uint8_t *vs_output_param_offset
,
1762 uint32_t num_outputs
,
1763 struct ac_vs_exports
*processed
,
1764 struct ac_vs_exp_inst
*exp
)
1766 unsigned p
, copy_back_channels
= 0;
1768 /* See if the output is already in the list of processed outputs.
1769 * The LLVMValueRef comparison relies on SSA.
1771 for (p
= 0; p
< processed
->num
; p
++) {
1772 bool different
= false;
1774 for (unsigned j
= 0; j
< 4; j
++) {
1775 struct ac_vs_exp_chan
*c1
= &processed
->exp
[p
].chan
[j
];
1776 struct ac_vs_exp_chan
*c2
= &exp
->chan
[j
];
1778 /* Treat undef as a match. */
1779 if (c2
->type
== AC_IR_UNDEF
)
1782 /* If c1 is undef but c2 isn't, we can copy c2 to c1
1783 * and consider the instruction duplicated.
1785 if (c1
->type
== AC_IR_UNDEF
) {
1786 copy_back_channels
|= 1 << j
;
1790 /* Test whether the channels are not equal. */
1791 if (c1
->type
!= c2
->type
||
1792 (c1
->type
== AC_IR_CONST
&&
1793 c1
->const_float
!= c2
->const_float
) ||
1794 (c1
->type
== AC_IR_VALUE
&&
1795 c1
->value
!= c2
->value
)) {
1803 copy_back_channels
= 0;
1805 if (p
== processed
->num
)
1808 /* If a match was found, but the matching export has undef where the new
1809 * one has a normal value, copy the normal value to the undef channel.
1811 struct ac_vs_exp_inst
*match
= &processed
->exp
[p
];
1813 while (copy_back_channels
) {
1814 unsigned chan
= u_bit_scan(©_back_channels
);
1816 assert(match
->chan
[chan
].type
== AC_IR_UNDEF
);
1817 LLVMSetOperand(match
->inst
, AC_EXP_OUT0
+ chan
,
1818 exp
->chan
[chan
].value
);
1819 match
->chan
[chan
] = exp
->chan
[chan
];
1822 /* The PARAM export is duplicated. Kill it. */
1823 LLVMInstructionEraseFromParent(exp
->inst
);
1825 /* Change OFFSET to the matching export. */
1826 for (unsigned i
= 0; i
< num_outputs
; i
++) {
1827 if (vs_output_param_offset
[i
] == exp
->offset
) {
1828 vs_output_param_offset
[i
] = match
->offset
;
1835 void ac_optimize_vs_outputs(struct ac_llvm_context
*ctx
,
1836 LLVMValueRef main_fn
,
1837 uint8_t *vs_output_param_offset
,
1838 uint32_t num_outputs
,
1839 uint8_t *num_param_exports
)
1841 LLVMBasicBlockRef bb
;
1842 bool removed_any
= false;
1843 struct ac_vs_exports exports
;
1847 /* Process all LLVM instructions. */
1848 bb
= LLVMGetFirstBasicBlock(main_fn
);
1850 LLVMValueRef inst
= LLVMGetFirstInstruction(bb
);
1853 LLVMValueRef cur
= inst
;
1854 inst
= LLVMGetNextInstruction(inst
);
1855 struct ac_vs_exp_inst exp
;
1857 if (LLVMGetInstructionOpcode(cur
) != LLVMCall
)
1860 LLVMValueRef callee
= ac_llvm_get_called_value(cur
);
1862 if (!ac_llvm_is_function(callee
))
1865 const char *name
= LLVMGetValueName(callee
);
1866 unsigned num_args
= LLVMCountParams(callee
);
1868 /* Check if this is an export instruction. */
1869 if ((num_args
!= 9 && num_args
!= 8) ||
1870 (strcmp(name
, "llvm.SI.export") &&
1871 strcmp(name
, "llvm.amdgcn.exp.f32")))
1874 LLVMValueRef arg
= LLVMGetOperand(cur
, AC_EXP_TARGET
);
1875 unsigned target
= LLVMConstIntGetZExtValue(arg
);
1877 if (target
< V_008DFC_SQ_EXP_PARAM
)
1880 target
-= V_008DFC_SQ_EXP_PARAM
;
1882 /* Parse the instruction. */
1883 memset(&exp
, 0, sizeof(exp
));
1884 exp
.offset
= target
;
1887 for (unsigned i
= 0; i
< 4; i
++) {
1888 LLVMValueRef v
= LLVMGetOperand(cur
, AC_EXP_OUT0
+ i
);
1890 exp
.chan
[i
].value
= v
;
1892 if (LLVMIsUndef(v
)) {
1893 exp
.chan
[i
].type
= AC_IR_UNDEF
;
1894 } else if (LLVMIsAConstantFP(v
)) {
1895 LLVMBool loses_info
;
1896 exp
.chan
[i
].type
= AC_IR_CONST
;
1897 exp
.chan
[i
].const_float
=
1898 LLVMConstRealGetDouble(v
, &loses_info
);
1900 exp
.chan
[i
].type
= AC_IR_VALUE
;
1904 /* Eliminate constant and duplicated PARAM exports. */
1905 if (ac_eliminate_const_output(vs_output_param_offset
,
1906 num_outputs
, &exp
) ||
1907 ac_eliminate_duplicated_output(vs_output_param_offset
,
1908 num_outputs
, &exports
,
1912 exports
.exp
[exports
.num
++] = exp
;
1915 bb
= LLVMGetNextBasicBlock(bb
);
1918 /* Remove holes in export memory due to removed PARAM exports.
1919 * This is done by renumbering all PARAM exports.
1922 uint8_t old_offset
[VARYING_SLOT_MAX
];
1925 /* Make a copy of the offsets. We need the old version while
1926 * we are modifying some of them. */
1927 memcpy(old_offset
, vs_output_param_offset
,
1928 sizeof(old_offset
));
1930 for (i
= 0; i
< exports
.num
; i
++) {
1931 unsigned offset
= exports
.exp
[i
].offset
;
1933 /* Update vs_output_param_offset. Multiple outputs can
1934 * have the same offset.
1936 for (out
= 0; out
< num_outputs
; out
++) {
1937 if (old_offset
[out
] == offset
)
1938 vs_output_param_offset
[out
] = i
;
1941 /* Change the PARAM offset in the instruction. */
1942 LLVMSetOperand(exports
.exp
[i
].inst
, AC_EXP_TARGET
,
1943 LLVMConstInt(ctx
->i32
,
1944 V_008DFC_SQ_EXP_PARAM
+ i
, 0));
1946 *num_param_exports
= exports
.num
;
1950 void ac_init_exec_full_mask(struct ac_llvm_context
*ctx
)
1952 LLVMValueRef full_mask
= LLVMConstInt(ctx
->i64
, ~0ull, 0);
1953 ac_build_intrinsic(ctx
,
1954 "llvm.amdgcn.init.exec", ctx
->voidt
,
1955 &full_mask
, 1, AC_FUNC_ATTR_CONVERGENT
);
1958 void ac_declare_lds_as_pointer(struct ac_llvm_context
*ctx
)
1960 unsigned lds_size
= ctx
->chip_class
>= CIK
? 65536 : 32768;
1961 ctx
->lds
= LLVMBuildIntToPtr(ctx
->builder
, ctx
->i32_0
,
1962 LLVMPointerType(LLVMArrayType(ctx
->i32
, lds_size
/ 4), AC_LOCAL_ADDR_SPACE
),
1966 LLVMValueRef
ac_lds_load(struct ac_llvm_context
*ctx
,
1967 LLVMValueRef dw_addr
)
1969 return ac_build_load(ctx
, ctx
->lds
, dw_addr
);
1972 void ac_lds_store(struct ac_llvm_context
*ctx
,
1973 LLVMValueRef dw_addr
,
1976 value
= ac_to_integer(ctx
, value
);
1977 ac_build_indexed_store(ctx
, ctx
->lds
,
1981 LLVMValueRef
ac_find_lsb(struct ac_llvm_context
*ctx
,
1982 LLVMTypeRef dst_type
,
1985 LLVMValueRef params
[2] = {
1988 /* The value of 1 means that ffs(x=0) = undef, so LLVM won't
1989 * add special code to check for x=0. The reason is that
1990 * the LLVM behavior for x=0 is different from what we
1991 * need here. However, LLVM also assumes that ffs(x) is
1992 * in [0, 31], but GLSL expects that ffs(0) = -1, so
1993 * a conditional assignment to handle 0 is still required.
1995 * The hardware already implements the correct behavior.
1997 LLVMConstInt(ctx
->i1
, 1, false),
2000 LLVMValueRef lsb
= ac_build_intrinsic(ctx
, "llvm.cttz.i32", ctx
->i32
,
2002 AC_FUNC_ATTR_READNONE
);
2004 /* TODO: We need an intrinsic to skip this conditional. */
2005 /* Check for zero: */
2006 return LLVMBuildSelect(ctx
->builder
, LLVMBuildICmp(ctx
->builder
,
2009 LLVMConstInt(ctx
->i32
, -1, 0), lsb
, "");
2012 LLVMTypeRef
ac_array_in_const_addr_space(LLVMTypeRef elem_type
)
2014 return LLVMPointerType(LLVMArrayType(elem_type
, 0),
2015 AC_CONST_ADDR_SPACE
);