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
->intptr
= HAVE_32BIT_POINTERS
? ctx
->i32
: ctx
->i64
;
68 ctx
->f16
= LLVMHalfTypeInContext(ctx
->context
);
69 ctx
->f32
= LLVMFloatTypeInContext(ctx
->context
);
70 ctx
->f64
= LLVMDoubleTypeInContext(ctx
->context
);
71 ctx
->v2i16
= LLVMVectorType(ctx
->i16
, 2);
72 ctx
->v2i32
= LLVMVectorType(ctx
->i32
, 2);
73 ctx
->v3i32
= LLVMVectorType(ctx
->i32
, 3);
74 ctx
->v4i32
= LLVMVectorType(ctx
->i32
, 4);
75 ctx
->v2f32
= LLVMVectorType(ctx
->f32
, 2);
76 ctx
->v4f32
= LLVMVectorType(ctx
->f32
, 4);
77 ctx
->v8i32
= LLVMVectorType(ctx
->i32
, 8);
79 ctx
->i32_0
= LLVMConstInt(ctx
->i32
, 0, false);
80 ctx
->i32_1
= LLVMConstInt(ctx
->i32
, 1, false);
81 ctx
->i64_0
= LLVMConstInt(ctx
->i64
, 0, false);
82 ctx
->i64_1
= LLVMConstInt(ctx
->i64
, 1, false);
83 ctx
->f32_0
= LLVMConstReal(ctx
->f32
, 0.0);
84 ctx
->f32_1
= LLVMConstReal(ctx
->f32
, 1.0);
85 ctx
->f64_0
= LLVMConstReal(ctx
->f64
, 0.0);
86 ctx
->f64_1
= LLVMConstReal(ctx
->f64
, 1.0);
88 ctx
->i1false
= LLVMConstInt(ctx
->i1
, 0, false);
89 ctx
->i1true
= LLVMConstInt(ctx
->i1
, 1, false);
91 ctx
->range_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
94 ctx
->invariant_load_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
95 "invariant.load", 14);
97 ctx
->fpmath_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
, "fpmath", 6);
99 args
[0] = LLVMConstReal(ctx
->f32
, 2.5);
100 ctx
->fpmath_md_2p5_ulp
= LLVMMDNodeInContext(ctx
->context
, args
, 1);
102 ctx
->uniform_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
103 "amdgpu.uniform", 14);
105 ctx
->empty_md
= LLVMMDNodeInContext(ctx
->context
, NULL
, 0);
109 ac_get_llvm_num_components(LLVMValueRef value
)
111 LLVMTypeRef type
= LLVMTypeOf(value
);
112 unsigned num_components
= LLVMGetTypeKind(type
) == LLVMVectorTypeKind
113 ? LLVMGetVectorSize(type
)
115 return num_components
;
119 ac_llvm_extract_elem(struct ac_llvm_context
*ac
,
123 if (LLVMGetTypeKind(LLVMTypeOf(value
)) != LLVMVectorTypeKind
) {
128 return LLVMBuildExtractElement(ac
->builder
, value
,
129 LLVMConstInt(ac
->i32
, index
, false), "");
133 ac_get_elem_bits(struct ac_llvm_context
*ctx
, LLVMTypeRef type
)
135 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
)
136 type
= LLVMGetElementType(type
);
138 if (LLVMGetTypeKind(type
) == LLVMIntegerTypeKind
)
139 return LLVMGetIntTypeWidth(type
);
141 if (type
== ctx
->f16
)
143 if (type
== ctx
->f32
)
145 if (type
== ctx
->f64
)
148 unreachable("Unhandled type kind in get_elem_bits");
152 ac_get_type_size(LLVMTypeRef type
)
154 LLVMTypeKind kind
= LLVMGetTypeKind(type
);
157 case LLVMIntegerTypeKind
:
158 return LLVMGetIntTypeWidth(type
) / 8;
159 case LLVMFloatTypeKind
:
161 case LLVMDoubleTypeKind
:
163 case LLVMPointerTypeKind
:
164 if (LLVMGetPointerAddressSpace(type
) == AC_CONST_32BIT_ADDR_SPACE
)
167 case LLVMVectorTypeKind
:
168 return LLVMGetVectorSize(type
) *
169 ac_get_type_size(LLVMGetElementType(type
));
170 case LLVMArrayTypeKind
:
171 return LLVMGetArrayLength(type
) *
172 ac_get_type_size(LLVMGetElementType(type
));
179 static LLVMTypeRef
to_integer_type_scalar(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
181 if (t
== ctx
->f16
|| t
== ctx
->i16
)
183 else if (t
== ctx
->f32
|| t
== ctx
->i32
)
185 else if (t
== ctx
->f64
|| t
== ctx
->i64
)
188 unreachable("Unhandled integer size");
192 ac_to_integer_type(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
194 if (LLVMGetTypeKind(t
) == LLVMVectorTypeKind
) {
195 LLVMTypeRef elem_type
= LLVMGetElementType(t
);
196 return LLVMVectorType(to_integer_type_scalar(ctx
, elem_type
),
197 LLVMGetVectorSize(t
));
199 return to_integer_type_scalar(ctx
, t
);
203 ac_to_integer(struct ac_llvm_context
*ctx
, LLVMValueRef v
)
205 LLVMTypeRef type
= LLVMTypeOf(v
);
206 return LLVMBuildBitCast(ctx
->builder
, v
, ac_to_integer_type(ctx
, type
), "");
209 static LLVMTypeRef
to_float_type_scalar(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
211 if (t
== ctx
->i16
|| t
== ctx
->f16
)
213 else if (t
== ctx
->i32
|| t
== ctx
->f32
)
215 else if (t
== ctx
->i64
|| t
== ctx
->f64
)
218 unreachable("Unhandled float size");
222 ac_to_float_type(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
224 if (LLVMGetTypeKind(t
) == LLVMVectorTypeKind
) {
225 LLVMTypeRef elem_type
= LLVMGetElementType(t
);
226 return LLVMVectorType(to_float_type_scalar(ctx
, elem_type
),
227 LLVMGetVectorSize(t
));
229 return to_float_type_scalar(ctx
, t
);
233 ac_to_float(struct ac_llvm_context
*ctx
, LLVMValueRef v
)
235 LLVMTypeRef type
= LLVMTypeOf(v
);
236 return LLVMBuildBitCast(ctx
->builder
, v
, ac_to_float_type(ctx
, type
), "");
241 ac_build_intrinsic(struct ac_llvm_context
*ctx
, const char *name
,
242 LLVMTypeRef return_type
, LLVMValueRef
*params
,
243 unsigned param_count
, unsigned attrib_mask
)
245 LLVMValueRef function
, call
;
246 bool set_callsite_attrs
= !(attrib_mask
& AC_FUNC_ATTR_LEGACY
);
248 function
= LLVMGetNamedFunction(ctx
->module
, name
);
250 LLVMTypeRef param_types
[32], function_type
;
253 assert(param_count
<= 32);
255 for (i
= 0; i
< param_count
; ++i
) {
257 param_types
[i
] = LLVMTypeOf(params
[i
]);
260 LLVMFunctionType(return_type
, param_types
, param_count
, 0);
261 function
= LLVMAddFunction(ctx
->module
, name
, function_type
);
263 LLVMSetFunctionCallConv(function
, LLVMCCallConv
);
264 LLVMSetLinkage(function
, LLVMExternalLinkage
);
266 if (!set_callsite_attrs
)
267 ac_add_func_attributes(ctx
->context
, function
, attrib_mask
);
270 call
= LLVMBuildCall(ctx
->builder
, function
, params
, param_count
, "");
271 if (set_callsite_attrs
)
272 ac_add_func_attributes(ctx
->context
, call
, attrib_mask
);
277 * Given the i32 or vNi32 \p type, generate the textual name (e.g. for use with
280 void ac_build_type_name_for_intr(LLVMTypeRef type
, char *buf
, unsigned bufsize
)
282 LLVMTypeRef elem_type
= type
;
284 assert(bufsize
>= 8);
286 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
) {
287 int ret
= snprintf(buf
, bufsize
, "v%u",
288 LLVMGetVectorSize(type
));
290 char *type_name
= LLVMPrintTypeToString(type
);
291 fprintf(stderr
, "Error building type name for: %s\n",
295 elem_type
= LLVMGetElementType(type
);
299 switch (LLVMGetTypeKind(elem_type
)) {
301 case LLVMIntegerTypeKind
:
302 snprintf(buf
, bufsize
, "i%d", LLVMGetIntTypeWidth(elem_type
));
304 case LLVMFloatTypeKind
:
305 snprintf(buf
, bufsize
, "f32");
307 case LLVMDoubleTypeKind
:
308 snprintf(buf
, bufsize
, "f64");
314 * Helper function that builds an LLVM IR PHI node and immediately adds
318 ac_build_phi(struct ac_llvm_context
*ctx
, LLVMTypeRef type
,
319 unsigned count_incoming
, LLVMValueRef
*values
,
320 LLVMBasicBlockRef
*blocks
)
322 LLVMValueRef phi
= LLVMBuildPhi(ctx
->builder
, type
, "");
323 LLVMAddIncoming(phi
, values
, blocks
, count_incoming
);
327 /* Prevent optimizations (at least of memory accesses) across the current
328 * point in the program by emitting empty inline assembly that is marked as
329 * having side effects.
331 * Optionally, a value can be passed through the inline assembly to prevent
332 * LLVM from hoisting calls to ReadNone functions.
335 ac_build_optimization_barrier(struct ac_llvm_context
*ctx
,
338 static int counter
= 0;
340 LLVMBuilderRef builder
= ctx
->builder
;
343 snprintf(code
, sizeof(code
), "; %d", p_atomic_inc_return(&counter
));
346 LLVMTypeRef ftype
= LLVMFunctionType(ctx
->voidt
, NULL
, 0, false);
347 LLVMValueRef inlineasm
= LLVMConstInlineAsm(ftype
, code
, "", true, false);
348 LLVMBuildCall(builder
, inlineasm
, NULL
, 0, "");
350 LLVMTypeRef ftype
= LLVMFunctionType(ctx
->i32
, &ctx
->i32
, 1, false);
351 LLVMValueRef inlineasm
= LLVMConstInlineAsm(ftype
, code
, "=v,0", true, false);
352 LLVMValueRef vgpr
= *pvgpr
;
353 LLVMTypeRef vgpr_type
= LLVMTypeOf(vgpr
);
354 unsigned vgpr_size
= ac_get_type_size(vgpr_type
);
357 assert(vgpr_size
% 4 == 0);
359 vgpr
= LLVMBuildBitCast(builder
, vgpr
, LLVMVectorType(ctx
->i32
, vgpr_size
/ 4), "");
360 vgpr0
= LLVMBuildExtractElement(builder
, vgpr
, ctx
->i32_0
, "");
361 vgpr0
= LLVMBuildCall(builder
, inlineasm
, &vgpr0
, 1, "");
362 vgpr
= LLVMBuildInsertElement(builder
, vgpr
, vgpr0
, ctx
->i32_0
, "");
363 vgpr
= LLVMBuildBitCast(builder
, vgpr
, vgpr_type
, "");
370 ac_build_shader_clock(struct ac_llvm_context
*ctx
)
372 LLVMValueRef tmp
= ac_build_intrinsic(ctx
, "llvm.readcyclecounter",
373 ctx
->i64
, NULL
, 0, 0);
374 return LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->v2i32
, "");
378 ac_build_ballot(struct ac_llvm_context
*ctx
,
381 LLVMValueRef args
[3] = {
384 LLVMConstInt(ctx
->i32
, LLVMIntNE
, 0)
387 /* We currently have no other way to prevent LLVM from lifting the icmp
388 * calls to a dominating basic block.
390 ac_build_optimization_barrier(ctx
, &args
[0]);
392 if (LLVMTypeOf(args
[0]) != ctx
->i32
)
393 args
[0] = LLVMBuildBitCast(ctx
->builder
, args
[0], ctx
->i32
, "");
395 return ac_build_intrinsic(ctx
,
396 "llvm.amdgcn.icmp.i32",
398 AC_FUNC_ATTR_NOUNWIND
|
399 AC_FUNC_ATTR_READNONE
|
400 AC_FUNC_ATTR_CONVERGENT
);
404 ac_build_vote_all(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
406 LLVMValueRef active_set
= ac_build_ballot(ctx
, ctx
->i32_1
);
407 LLVMValueRef vote_set
= ac_build_ballot(ctx
, value
);
408 return LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, vote_set
, active_set
, "");
412 ac_build_vote_any(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
414 LLVMValueRef vote_set
= ac_build_ballot(ctx
, value
);
415 return LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, vote_set
,
416 LLVMConstInt(ctx
->i64
, 0, 0), "");
420 ac_build_vote_eq(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
422 LLVMValueRef active_set
= ac_build_ballot(ctx
, ctx
->i32_1
);
423 LLVMValueRef vote_set
= ac_build_ballot(ctx
, value
);
425 LLVMValueRef all
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
426 vote_set
, active_set
, "");
427 LLVMValueRef none
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
429 LLVMConstInt(ctx
->i64
, 0, 0), "");
430 return LLVMBuildOr(ctx
->builder
, all
, none
, "");
434 ac_build_varying_gather_values(struct ac_llvm_context
*ctx
, LLVMValueRef
*values
,
435 unsigned value_count
, unsigned component
)
437 LLVMValueRef vec
= NULL
;
439 if (value_count
== 1) {
440 return values
[component
];
441 } else if (!value_count
)
442 unreachable("value_count is 0");
444 for (unsigned i
= component
; i
< value_count
+ component
; i
++) {
445 LLVMValueRef value
= values
[i
];
448 vec
= LLVMGetUndef( LLVMVectorType(LLVMTypeOf(value
), value_count
));
449 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, i
- component
, false);
450 vec
= LLVMBuildInsertElement(ctx
->builder
, vec
, value
, index
, "");
456 ac_build_gather_values_extended(struct ac_llvm_context
*ctx
,
457 LLVMValueRef
*values
,
458 unsigned value_count
,
459 unsigned value_stride
,
463 LLVMBuilderRef builder
= ctx
->builder
;
464 LLVMValueRef vec
= NULL
;
467 if (value_count
== 1 && !always_vector
) {
469 return LLVMBuildLoad(builder
, values
[0], "");
471 } else if (!value_count
)
472 unreachable("value_count is 0");
474 for (i
= 0; i
< value_count
; i
++) {
475 LLVMValueRef value
= values
[i
* value_stride
];
477 value
= LLVMBuildLoad(builder
, value
, "");
480 vec
= LLVMGetUndef( LLVMVectorType(LLVMTypeOf(value
), value_count
));
481 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, i
, false);
482 vec
= LLVMBuildInsertElement(builder
, vec
, value
, index
, "");
488 ac_build_gather_values(struct ac_llvm_context
*ctx
,
489 LLVMValueRef
*values
,
490 unsigned value_count
)
492 return ac_build_gather_values_extended(ctx
, values
, value_count
, 1, false, false);
495 /* Expand a scalar or vector to <4 x type> by filling the remaining channels
496 * with undef. Extract at most num_channels components from the input.
498 LLVMValueRef
ac_build_expand_to_vec4(struct ac_llvm_context
*ctx
,
500 unsigned num_channels
)
502 LLVMTypeRef elemtype
;
503 LLVMValueRef chan
[4];
505 if (LLVMGetTypeKind(LLVMTypeOf(value
)) == LLVMVectorTypeKind
) {
506 unsigned vec_size
= LLVMGetVectorSize(LLVMTypeOf(value
));
507 num_channels
= MIN2(num_channels
, vec_size
);
509 if (num_channels
>= 4)
512 for (unsigned i
= 0; i
< num_channels
; i
++)
513 chan
[i
] = ac_llvm_extract_elem(ctx
, value
, i
);
515 elemtype
= LLVMGetElementType(LLVMTypeOf(value
));
518 assert(num_channels
== 1);
521 elemtype
= LLVMTypeOf(value
);
524 while (num_channels
< 4)
525 chan
[num_channels
++] = LLVMGetUndef(elemtype
);
527 return ac_build_gather_values(ctx
, chan
, 4);
531 ac_build_fdiv(struct ac_llvm_context
*ctx
,
535 LLVMValueRef ret
= LLVMBuildFDiv(ctx
->builder
, num
, den
, "");
537 /* Use v_rcp_f32 instead of precise division. */
538 if (!LLVMIsConstant(ret
))
539 LLVMSetMetadata(ret
, ctx
->fpmath_md_kind
, ctx
->fpmath_md_2p5_ulp
);
543 /* Coordinates for cube map selection. sc, tc, and ma are as in Table 8.27
544 * of the OpenGL 4.5 (Compatibility Profile) specification, except ma is
545 * already multiplied by two. id is the cube face number.
547 struct cube_selection_coords
{
554 build_cube_intrinsic(struct ac_llvm_context
*ctx
,
556 struct cube_selection_coords
*out
)
558 LLVMTypeRef f32
= ctx
->f32
;
560 out
->stc
[1] = ac_build_intrinsic(ctx
, "llvm.amdgcn.cubetc",
561 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
562 out
->stc
[0] = ac_build_intrinsic(ctx
, "llvm.amdgcn.cubesc",
563 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
564 out
->ma
= ac_build_intrinsic(ctx
, "llvm.amdgcn.cubema",
565 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
566 out
->id
= ac_build_intrinsic(ctx
, "llvm.amdgcn.cubeid",
567 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
571 * Build a manual selection sequence for cube face sc/tc coordinates and
572 * major axis vector (multiplied by 2 for consistency) for the given
573 * vec3 \p coords, for the face implied by \p selcoords.
575 * For the major axis, we always adjust the sign to be in the direction of
576 * selcoords.ma; i.e., a positive out_ma means that coords is pointed towards
577 * the selcoords major axis.
579 static void build_cube_select(struct ac_llvm_context
*ctx
,
580 const struct cube_selection_coords
*selcoords
,
581 const LLVMValueRef
*coords
,
582 LLVMValueRef
*out_st
,
583 LLVMValueRef
*out_ma
)
585 LLVMBuilderRef builder
= ctx
->builder
;
586 LLVMTypeRef f32
= LLVMTypeOf(coords
[0]);
587 LLVMValueRef is_ma_positive
;
589 LLVMValueRef is_ma_z
, is_not_ma_z
;
590 LLVMValueRef is_ma_y
;
591 LLVMValueRef is_ma_x
;
595 is_ma_positive
= LLVMBuildFCmp(builder
, LLVMRealUGE
,
596 selcoords
->ma
, LLVMConstReal(f32
, 0.0), "");
597 sgn_ma
= LLVMBuildSelect(builder
, is_ma_positive
,
598 LLVMConstReal(f32
, 1.0), LLVMConstReal(f32
, -1.0), "");
600 is_ma_z
= LLVMBuildFCmp(builder
, LLVMRealUGE
, selcoords
->id
, LLVMConstReal(f32
, 4.0), "");
601 is_not_ma_z
= LLVMBuildNot(builder
, is_ma_z
, "");
602 is_ma_y
= LLVMBuildAnd(builder
, is_not_ma_z
,
603 LLVMBuildFCmp(builder
, LLVMRealUGE
, selcoords
->id
, LLVMConstReal(f32
, 2.0), ""), "");
604 is_ma_x
= LLVMBuildAnd(builder
, is_not_ma_z
, LLVMBuildNot(builder
, is_ma_y
, ""), "");
607 tmp
= LLVMBuildSelect(builder
, is_ma_x
, coords
[2], coords
[0], "");
608 sgn
= LLVMBuildSelect(builder
, is_ma_y
, LLVMConstReal(f32
, 1.0),
609 LLVMBuildSelect(builder
, is_ma_z
, sgn_ma
,
610 LLVMBuildFNeg(builder
, sgn_ma
, ""), ""), "");
611 out_st
[0] = LLVMBuildFMul(builder
, tmp
, sgn
, "");
614 tmp
= LLVMBuildSelect(builder
, is_ma_y
, coords
[2], coords
[1], "");
615 sgn
= LLVMBuildSelect(builder
, is_ma_y
, sgn_ma
,
616 LLVMConstReal(f32
, -1.0), "");
617 out_st
[1] = LLVMBuildFMul(builder
, tmp
, sgn
, "");
620 tmp
= LLVMBuildSelect(builder
, is_ma_z
, coords
[2],
621 LLVMBuildSelect(builder
, is_ma_y
, coords
[1], coords
[0], ""), "");
622 tmp
= ac_build_intrinsic(ctx
, "llvm.fabs.f32",
623 ctx
->f32
, &tmp
, 1, AC_FUNC_ATTR_READNONE
);
624 *out_ma
= LLVMBuildFMul(builder
, tmp
, LLVMConstReal(f32
, 2.0), "");
628 ac_prepare_cube_coords(struct ac_llvm_context
*ctx
,
629 bool is_deriv
, bool is_array
, bool is_lod
,
630 LLVMValueRef
*coords_arg
,
631 LLVMValueRef
*derivs_arg
)
634 LLVMBuilderRef builder
= ctx
->builder
;
635 struct cube_selection_coords selcoords
;
636 LLVMValueRef coords
[3];
639 if (is_array
&& !is_lod
) {
640 LLVMValueRef tmp
= coords_arg
[3];
641 tmp
= ac_build_intrinsic(ctx
, "llvm.rint.f32", ctx
->f32
, &tmp
, 1, 0);
643 /* Section 8.9 (Texture Functions) of the GLSL 4.50 spec says:
645 * "For Array forms, the array layer used will be
647 * max(0, min(d−1, floor(layer+0.5)))
649 * where d is the depth of the texture array and layer
650 * comes from the component indicated in the tables below.
651 * Workaroudn for an issue where the layer is taken from a
652 * helper invocation which happens to fall on a different
653 * layer due to extrapolation."
655 * VI and earlier attempt to implement this in hardware by
656 * clamping the value of coords[2] = (8 * layer) + face.
657 * Unfortunately, this means that the we end up with the wrong
658 * face when clamping occurs.
660 * Clamp the layer earlier to work around the issue.
662 if (ctx
->chip_class
<= VI
) {
664 ge0
= LLVMBuildFCmp(builder
, LLVMRealOGE
, tmp
, ctx
->f32_0
, "");
665 tmp
= LLVMBuildSelect(builder
, ge0
, tmp
, ctx
->f32_0
, "");
671 build_cube_intrinsic(ctx
, coords_arg
, &selcoords
);
673 invma
= ac_build_intrinsic(ctx
, "llvm.fabs.f32",
674 ctx
->f32
, &selcoords
.ma
, 1, AC_FUNC_ATTR_READNONE
);
675 invma
= ac_build_fdiv(ctx
, LLVMConstReal(ctx
->f32
, 1.0), invma
);
677 for (int i
= 0; i
< 2; ++i
)
678 coords
[i
] = LLVMBuildFMul(builder
, selcoords
.stc
[i
], invma
, "");
680 coords
[2] = selcoords
.id
;
682 if (is_deriv
&& derivs_arg
) {
683 LLVMValueRef derivs
[4];
686 /* Convert cube derivatives to 2D derivatives. */
687 for (axis
= 0; axis
< 2; axis
++) {
688 LLVMValueRef deriv_st
[2];
689 LLVMValueRef deriv_ma
;
691 /* Transform the derivative alongside the texture
692 * coordinate. Mathematically, the correct formula is
693 * as follows. Assume we're projecting onto the +Z face
694 * and denote by dx/dh the derivative of the (original)
695 * X texture coordinate with respect to horizontal
696 * window coordinates. The projection onto the +Z face
701 * Then df/dh = df/dx * dx/dh + df/dz * dz/dh
702 * = 1/z * dx/dh - x/z * 1/z * dz/dh.
704 * This motivatives the implementation below.
706 * Whether this actually gives the expected results for
707 * apps that might feed in derivatives obtained via
708 * finite differences is anyone's guess. The OpenGL spec
709 * seems awfully quiet about how textureGrad for cube
710 * maps should be handled.
712 build_cube_select(ctx
, &selcoords
, &derivs_arg
[axis
* 3],
713 deriv_st
, &deriv_ma
);
715 deriv_ma
= LLVMBuildFMul(builder
, deriv_ma
, invma
, "");
717 for (int i
= 0; i
< 2; ++i
)
718 derivs
[axis
* 2 + i
] =
719 LLVMBuildFSub(builder
,
720 LLVMBuildFMul(builder
, deriv_st
[i
], invma
, ""),
721 LLVMBuildFMul(builder
, deriv_ma
, coords
[i
], ""), "");
724 memcpy(derivs_arg
, derivs
, sizeof(derivs
));
727 /* Shift the texture coordinate. This must be applied after the
728 * derivative calculation.
730 for (int i
= 0; i
< 2; ++i
)
731 coords
[i
] = LLVMBuildFAdd(builder
, coords
[i
], LLVMConstReal(ctx
->f32
, 1.5), "");
734 /* for cube arrays coord.z = coord.w(array_index) * 8 + face */
735 /* coords_arg.w component - array_index for cube arrays */
736 LLVMValueRef tmp
= LLVMBuildFMul(ctx
->builder
, coords_arg
[3], LLVMConstReal(ctx
->f32
, 8.0), "");
737 coords
[2] = LLVMBuildFAdd(ctx
->builder
, tmp
, coords
[2], "");
740 memcpy(coords_arg
, coords
, sizeof(coords
));
745 ac_build_fs_interp(struct ac_llvm_context
*ctx
,
746 LLVMValueRef llvm_chan
,
747 LLVMValueRef attr_number
,
752 LLVMValueRef args
[5];
757 args
[2] = attr_number
;
760 p1
= ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p1",
761 ctx
->f32
, args
, 4, AC_FUNC_ATTR_READNONE
);
766 args
[3] = attr_number
;
769 return ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p2",
770 ctx
->f32
, args
, 5, AC_FUNC_ATTR_READNONE
);
774 ac_build_fs_interp_mov(struct ac_llvm_context
*ctx
,
775 LLVMValueRef parameter
,
776 LLVMValueRef llvm_chan
,
777 LLVMValueRef attr_number
,
780 LLVMValueRef args
[4];
784 args
[2] = attr_number
;
787 return ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.mov",
788 ctx
->f32
, args
, 4, AC_FUNC_ATTR_READNONE
);
792 ac_build_gep0(struct ac_llvm_context
*ctx
,
793 LLVMValueRef base_ptr
,
796 LLVMValueRef indices
[2] = {
797 LLVMConstInt(ctx
->i32
, 0, 0),
800 return LLVMBuildGEP(ctx
->builder
, base_ptr
,
805 ac_build_indexed_store(struct ac_llvm_context
*ctx
,
806 LLVMValueRef base_ptr
, LLVMValueRef index
,
809 LLVMBuildStore(ctx
->builder
, value
,
810 ac_build_gep0(ctx
, base_ptr
, index
));
814 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad.
815 * It's equivalent to doing a load from &base_ptr[index].
817 * \param base_ptr Where the array starts.
818 * \param index The element index into the array.
819 * \param uniform Whether the base_ptr and index can be assumed to be
820 * dynamically uniform (i.e. load to an SGPR)
821 * \param invariant Whether the load is invariant (no other opcodes affect it)
824 ac_build_load_custom(struct ac_llvm_context
*ctx
, LLVMValueRef base_ptr
,
825 LLVMValueRef index
, bool uniform
, bool invariant
)
827 LLVMValueRef pointer
, result
;
829 pointer
= ac_build_gep0(ctx
, base_ptr
, index
);
831 LLVMSetMetadata(pointer
, ctx
->uniform_md_kind
, ctx
->empty_md
);
832 result
= LLVMBuildLoad(ctx
->builder
, pointer
, "");
834 LLVMSetMetadata(result
, ctx
->invariant_load_md_kind
, ctx
->empty_md
);
838 LLVMValueRef
ac_build_load(struct ac_llvm_context
*ctx
, LLVMValueRef base_ptr
,
841 return ac_build_load_custom(ctx
, base_ptr
, index
, false, false);
844 LLVMValueRef
ac_build_load_invariant(struct ac_llvm_context
*ctx
,
845 LLVMValueRef base_ptr
, LLVMValueRef index
)
847 return ac_build_load_custom(ctx
, base_ptr
, index
, false, true);
850 LLVMValueRef
ac_build_load_to_sgpr(struct ac_llvm_context
*ctx
,
851 LLVMValueRef base_ptr
, LLVMValueRef index
)
853 return ac_build_load_custom(ctx
, base_ptr
, index
, true, true);
856 /* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
857 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
858 * or v4i32 (num_channels=3,4).
861 ac_build_buffer_store_dword(struct ac_llvm_context
*ctx
,
864 unsigned num_channels
,
865 LLVMValueRef voffset
,
866 LLVMValueRef soffset
,
867 unsigned inst_offset
,
870 bool writeonly_memory
,
871 bool swizzle_enable_hint
)
873 /* SWIZZLE_ENABLE requires that soffset isn't folded into voffset
874 * (voffset is swizzled, but soffset isn't swizzled).
875 * llvm.amdgcn.buffer.store doesn't have a separate soffset parameter.
877 if (!swizzle_enable_hint
) {
878 /* Split 3 channel stores, becase LLVM doesn't support 3-channel
880 if (num_channels
== 3) {
881 LLVMValueRef v
[3], v01
;
883 for (int i
= 0; i
< 3; i
++) {
884 v
[i
] = LLVMBuildExtractElement(ctx
->builder
, vdata
,
885 LLVMConstInt(ctx
->i32
, i
, 0), "");
887 v01
= ac_build_gather_values(ctx
, v
, 2);
889 ac_build_buffer_store_dword(ctx
, rsrc
, v01
, 2, voffset
,
890 soffset
, inst_offset
, glc
, slc
,
891 writeonly_memory
, swizzle_enable_hint
);
892 ac_build_buffer_store_dword(ctx
, rsrc
, v
[2], 1, voffset
,
893 soffset
, inst_offset
+ 8,
895 writeonly_memory
, swizzle_enable_hint
);
899 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
900 static const char *types
[] = {"f32", "v2f32", "v4f32"};
902 LLVMValueRef offset
= soffset
;
905 offset
= LLVMBuildAdd(ctx
->builder
, offset
,
906 LLVMConstInt(ctx
->i32
, inst_offset
, 0), "");
908 offset
= LLVMBuildAdd(ctx
->builder
, offset
, voffset
, "");
910 LLVMValueRef args
[] = {
911 ac_to_float(ctx
, vdata
),
912 LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, ""),
913 LLVMConstInt(ctx
->i32
, 0, 0),
915 LLVMConstInt(ctx
->i1
, glc
, 0),
916 LLVMConstInt(ctx
->i1
, slc
, 0),
919 snprintf(name
, sizeof(name
), "llvm.amdgcn.buffer.store.%s",
922 ac_build_intrinsic(ctx
, name
, ctx
->voidt
,
923 args
, ARRAY_SIZE(args
),
925 AC_FUNC_ATTR_INACCESSIBLE_MEM_ONLY
:
926 AC_FUNC_ATTR_WRITEONLY
);
930 static unsigned dfmt
[] = {
931 V_008F0C_BUF_DATA_FORMAT_32
,
932 V_008F0C_BUF_DATA_FORMAT_32_32
,
933 V_008F0C_BUF_DATA_FORMAT_32_32_32
,
934 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
936 assert(num_channels
>= 1 && num_channels
<= 4);
938 LLVMValueRef args
[] = {
941 LLVMConstInt(ctx
->i32
, num_channels
, 0),
942 voffset
? voffset
: LLVMGetUndef(ctx
->i32
),
944 LLVMConstInt(ctx
->i32
, inst_offset
, 0),
945 LLVMConstInt(ctx
->i32
, dfmt
[num_channels
- 1], 0),
946 LLVMConstInt(ctx
->i32
, V_008F0C_BUF_NUM_FORMAT_UINT
, 0),
947 LLVMConstInt(ctx
->i32
, voffset
!= NULL
, 0),
948 LLVMConstInt(ctx
->i32
, 0, 0), /* idxen */
949 LLVMConstInt(ctx
->i32
, glc
, 0),
950 LLVMConstInt(ctx
->i32
, slc
, 0),
951 LLVMConstInt(ctx
->i32
, 0, 0), /* tfe*/
954 /* The instruction offset field has 12 bits */
955 assert(voffset
|| inst_offset
< (1 << 12));
957 /* The intrinsic is overloaded, we need to add a type suffix for overloading to work. */
958 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
959 const char *types
[] = {"i32", "v2i32", "v4i32"};
961 snprintf(name
, sizeof(name
), "llvm.SI.tbuffer.store.%s", types
[func
]);
963 ac_build_intrinsic(ctx
, name
, ctx
->voidt
,
964 args
, ARRAY_SIZE(args
),
965 AC_FUNC_ATTR_LEGACY
);
969 ac_build_buffer_load_common(struct ac_llvm_context
*ctx
,
972 LLVMValueRef voffset
,
973 unsigned num_channels
,
979 LLVMValueRef args
[] = {
980 LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, ""),
981 vindex
? vindex
: LLVMConstInt(ctx
->i32
, 0, 0),
983 LLVMConstInt(ctx
->i1
, glc
, 0),
984 LLVMConstInt(ctx
->i1
, slc
, 0)
986 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
988 LLVMTypeRef types
[] = {ctx
->f32
, ctx
->v2f32
, ctx
->v4f32
};
989 const char *type_names
[] = {"f32", "v2f32", "v4f32"};
993 snprintf(name
, sizeof(name
), "llvm.amdgcn.buffer.load.format.%s",
996 snprintf(name
, sizeof(name
), "llvm.amdgcn.buffer.load.%s",
1000 return ac_build_intrinsic(ctx
, name
, types
[func
], args
,
1002 ac_get_load_intr_attribs(can_speculate
));
1006 ac_build_buffer_load(struct ac_llvm_context
*ctx
,
1009 LLVMValueRef vindex
,
1010 LLVMValueRef voffset
,
1011 LLVMValueRef soffset
,
1012 unsigned inst_offset
,
1018 LLVMValueRef offset
= LLVMConstInt(ctx
->i32
, inst_offset
, 0);
1020 offset
= LLVMBuildAdd(ctx
->builder
, offset
, voffset
, "");
1022 offset
= LLVMBuildAdd(ctx
->builder
, offset
, soffset
, "");
1024 /* TODO: VI and later generations can use SMEM with GLC=1.*/
1025 if (allow_smem
&& !glc
&& !slc
) {
1026 assert(vindex
== NULL
);
1028 LLVMValueRef result
[8];
1030 for (int i
= 0; i
< num_channels
; i
++) {
1032 offset
= LLVMBuildAdd(ctx
->builder
, offset
,
1033 LLVMConstInt(ctx
->i32
, 4, 0), "");
1035 LLVMValueRef args
[2] = {rsrc
, offset
};
1036 result
[i
] = ac_build_intrinsic(ctx
, "llvm.SI.load.const.v4i32",
1038 AC_FUNC_ATTR_READNONE
|
1039 AC_FUNC_ATTR_LEGACY
);
1041 if (num_channels
== 1)
1044 if (num_channels
== 3)
1045 result
[num_channels
++] = LLVMGetUndef(ctx
->f32
);
1046 return ac_build_gather_values(ctx
, result
, num_channels
);
1049 return ac_build_buffer_load_common(ctx
, rsrc
, vindex
, offset
,
1050 num_channels
, glc
, slc
,
1051 can_speculate
, false);
1054 LLVMValueRef
ac_build_buffer_load_format(struct ac_llvm_context
*ctx
,
1056 LLVMValueRef vindex
,
1057 LLVMValueRef voffset
,
1058 unsigned num_channels
,
1062 return ac_build_buffer_load_common(ctx
, rsrc
, vindex
, voffset
,
1063 num_channels
, glc
, false,
1064 can_speculate
, true);
1068 * Set range metadata on an instruction. This can only be used on load and
1069 * call instructions. If you know an instruction can only produce the values
1070 * 0, 1, 2, you would do set_range_metadata(value, 0, 3);
1071 * \p lo is the minimum value inclusive.
1072 * \p hi is the maximum value exclusive.
1074 static void set_range_metadata(struct ac_llvm_context
*ctx
,
1075 LLVMValueRef value
, unsigned lo
, unsigned hi
)
1077 LLVMValueRef range_md
, md_args
[2];
1078 LLVMTypeRef type
= LLVMTypeOf(value
);
1079 LLVMContextRef context
= LLVMGetTypeContext(type
);
1081 md_args
[0] = LLVMConstInt(type
, lo
, false);
1082 md_args
[1] = LLVMConstInt(type
, hi
, false);
1083 range_md
= LLVMMDNodeInContext(context
, md_args
, 2);
1084 LLVMSetMetadata(value
, ctx
->range_md_kind
, range_md
);
1088 ac_get_thread_id(struct ac_llvm_context
*ctx
)
1092 LLVMValueRef tid_args
[2];
1093 tid_args
[0] = LLVMConstInt(ctx
->i32
, 0xffffffff, false);
1094 tid_args
[1] = LLVMConstInt(ctx
->i32
, 0, false);
1095 tid_args
[1] = ac_build_intrinsic(ctx
,
1096 "llvm.amdgcn.mbcnt.lo", ctx
->i32
,
1097 tid_args
, 2, AC_FUNC_ATTR_READNONE
);
1099 tid
= ac_build_intrinsic(ctx
, "llvm.amdgcn.mbcnt.hi",
1101 2, AC_FUNC_ATTR_READNONE
);
1102 set_range_metadata(ctx
, tid
, 0, 64);
1107 * SI implements derivatives using the local data store (LDS)
1108 * All writes to the LDS happen in all executing threads at
1109 * the same time. TID is the Thread ID for the current
1110 * thread and is a value between 0 and 63, representing
1111 * the thread's position in the wavefront.
1113 * For the pixel shader threads are grouped into quads of four pixels.
1114 * The TIDs of the pixels of a quad are:
1122 * So, masking the TID with 0xfffffffc yields the TID of the top left pixel
1123 * of the quad, masking with 0xfffffffd yields the TID of the top pixel of
1124 * the current pixel's column, and masking with 0xfffffffe yields the TID
1125 * of the left pixel of the current pixel's row.
1127 * Adding 1 yields the TID of the pixel to the right of the left pixel, and
1128 * adding 2 yields the TID of the pixel below the top pixel.
1131 ac_build_ddxy(struct ac_llvm_context
*ctx
,
1136 LLVMValueRef tl
, trbl
, args
[2];
1137 LLVMValueRef result
;
1139 if (ctx
->chip_class
>= VI
) {
1140 LLVMValueRef thread_id
, tl_tid
, trbl_tid
;
1141 thread_id
= ac_get_thread_id(ctx
);
1143 tl_tid
= LLVMBuildAnd(ctx
->builder
, thread_id
,
1144 LLVMConstInt(ctx
->i32
, mask
, false), "");
1146 trbl_tid
= LLVMBuildAdd(ctx
->builder
, tl_tid
,
1147 LLVMConstInt(ctx
->i32
, idx
, false), "");
1149 args
[0] = LLVMBuildMul(ctx
->builder
, tl_tid
,
1150 LLVMConstInt(ctx
->i32
, 4, false), "");
1152 tl
= ac_build_intrinsic(ctx
,
1153 "llvm.amdgcn.ds.bpermute", ctx
->i32
,
1155 AC_FUNC_ATTR_READNONE
|
1156 AC_FUNC_ATTR_CONVERGENT
);
1158 args
[0] = LLVMBuildMul(ctx
->builder
, trbl_tid
,
1159 LLVMConstInt(ctx
->i32
, 4, false), "");
1160 trbl
= ac_build_intrinsic(ctx
,
1161 "llvm.amdgcn.ds.bpermute", ctx
->i32
,
1163 AC_FUNC_ATTR_READNONE
|
1164 AC_FUNC_ATTR_CONVERGENT
);
1166 uint32_t masks
[2] = {};
1169 case AC_TID_MASK_TOP_LEFT
:
1177 case AC_TID_MASK_TOP
:
1181 case AC_TID_MASK_LEFT
:
1190 args
[1] = LLVMConstInt(ctx
->i32
, masks
[0], false);
1192 tl
= ac_build_intrinsic(ctx
,
1193 "llvm.amdgcn.ds.swizzle", ctx
->i32
,
1195 AC_FUNC_ATTR_READNONE
|
1196 AC_FUNC_ATTR_CONVERGENT
);
1198 args
[1] = LLVMConstInt(ctx
->i32
, masks
[1], false);
1199 trbl
= ac_build_intrinsic(ctx
,
1200 "llvm.amdgcn.ds.swizzle", ctx
->i32
,
1202 AC_FUNC_ATTR_READNONE
|
1203 AC_FUNC_ATTR_CONVERGENT
);
1206 tl
= LLVMBuildBitCast(ctx
->builder
, tl
, ctx
->f32
, "");
1207 trbl
= LLVMBuildBitCast(ctx
->builder
, trbl
, ctx
->f32
, "");
1208 result
= LLVMBuildFSub(ctx
->builder
, trbl
, tl
, "");
1213 ac_build_sendmsg(struct ac_llvm_context
*ctx
,
1215 LLVMValueRef wave_id
)
1217 LLVMValueRef args
[2];
1218 args
[0] = LLVMConstInt(ctx
->i32
, msg
, false);
1220 ac_build_intrinsic(ctx
, "llvm.amdgcn.s.sendmsg", ctx
->voidt
, args
, 2, 0);
1224 ac_build_imsb(struct ac_llvm_context
*ctx
,
1226 LLVMTypeRef dst_type
)
1228 LLVMValueRef msb
= ac_build_intrinsic(ctx
, "llvm.amdgcn.sffbh.i32",
1230 AC_FUNC_ATTR_READNONE
);
1232 /* The HW returns the last bit index from MSB, but NIR/TGSI wants
1233 * the index from LSB. Invert it by doing "31 - msb". */
1234 msb
= LLVMBuildSub(ctx
->builder
, LLVMConstInt(ctx
->i32
, 31, false),
1237 LLVMValueRef all_ones
= LLVMConstInt(ctx
->i32
, -1, true);
1238 LLVMValueRef cond
= LLVMBuildOr(ctx
->builder
,
1239 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
1240 arg
, LLVMConstInt(ctx
->i32
, 0, 0), ""),
1241 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
1242 arg
, all_ones
, ""), "");
1244 return LLVMBuildSelect(ctx
->builder
, cond
, all_ones
, msb
, "");
1248 ac_build_umsb(struct ac_llvm_context
*ctx
,
1250 LLVMTypeRef dst_type
)
1252 LLVMValueRef args
[2] = {
1256 LLVMValueRef msb
= ac_build_intrinsic(ctx
, "llvm.ctlz.i32",
1257 dst_type
, args
, ARRAY_SIZE(args
),
1258 AC_FUNC_ATTR_READNONE
);
1260 /* The HW returns the last bit index from MSB, but TGSI/NIR wants
1261 * the index from LSB. Invert it by doing "31 - msb". */
1262 msb
= LLVMBuildSub(ctx
->builder
, LLVMConstInt(ctx
->i32
, 31, false),
1265 /* check for zero */
1266 return LLVMBuildSelect(ctx
->builder
,
1267 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, arg
,
1268 LLVMConstInt(ctx
->i32
, 0, 0), ""),
1269 LLVMConstInt(ctx
->i32
, -1, true), msb
, "");
1272 LLVMValueRef
ac_build_fmin(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
1275 LLVMValueRef args
[2] = {a
, b
};
1276 return ac_build_intrinsic(ctx
, "llvm.minnum.f32", ctx
->f32
, args
, 2,
1277 AC_FUNC_ATTR_READNONE
);
1280 LLVMValueRef
ac_build_fmax(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
1283 LLVMValueRef args
[2] = {a
, b
};
1284 return ac_build_intrinsic(ctx
, "llvm.maxnum.f32", ctx
->f32
, args
, 2,
1285 AC_FUNC_ATTR_READNONE
);
1288 LLVMValueRef
ac_build_imin(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
1291 LLVMValueRef cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSLE
, a
, b
, "");
1292 return LLVMBuildSelect(ctx
->builder
, cmp
, a
, b
, "");
1295 LLVMValueRef
ac_build_imax(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
1298 LLVMValueRef cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGT
, a
, b
, "");
1299 return LLVMBuildSelect(ctx
->builder
, cmp
, a
, b
, "");
1302 LLVMValueRef
ac_build_umin(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
1305 LLVMValueRef cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntULE
, a
, b
, "");
1306 return LLVMBuildSelect(ctx
->builder
, cmp
, a
, b
, "");
1309 LLVMValueRef
ac_build_clamp(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
1311 if (HAVE_LLVM
>= 0x0500) {
1312 return ac_build_fmin(ctx
, ac_build_fmax(ctx
, value
, ctx
->f32_0
),
1316 LLVMValueRef args
[3] = {
1318 LLVMConstReal(ctx
->f32
, 0),
1319 LLVMConstReal(ctx
->f32
, 1),
1322 return ac_build_intrinsic(ctx
, "llvm.AMDGPU.clamp.", ctx
->f32
, args
, 3,
1323 AC_FUNC_ATTR_READNONE
|
1324 AC_FUNC_ATTR_LEGACY
);
1327 void ac_build_export(struct ac_llvm_context
*ctx
, struct ac_export_args
*a
)
1329 LLVMValueRef args
[9];
1331 if (HAVE_LLVM
>= 0x0500) {
1332 args
[0] = LLVMConstInt(ctx
->i32
, a
->target
, 0);
1333 args
[1] = LLVMConstInt(ctx
->i32
, a
->enabled_channels
, 0);
1336 LLVMTypeRef i16
= LLVMInt16TypeInContext(ctx
->context
);
1337 LLVMTypeRef v2i16
= LLVMVectorType(i16
, 2);
1339 args
[2] = LLVMBuildBitCast(ctx
->builder
, a
->out
[0],
1341 args
[3] = LLVMBuildBitCast(ctx
->builder
, a
->out
[1],
1343 args
[4] = LLVMConstInt(ctx
->i1
, a
->done
, 0);
1344 args
[5] = LLVMConstInt(ctx
->i1
, a
->valid_mask
, 0);
1346 ac_build_intrinsic(ctx
, "llvm.amdgcn.exp.compr.v2i16",
1347 ctx
->voidt
, args
, 6, 0);
1349 args
[2] = a
->out
[0];
1350 args
[3] = a
->out
[1];
1351 args
[4] = a
->out
[2];
1352 args
[5] = a
->out
[3];
1353 args
[6] = LLVMConstInt(ctx
->i1
, a
->done
, 0);
1354 args
[7] = LLVMConstInt(ctx
->i1
, a
->valid_mask
, 0);
1356 ac_build_intrinsic(ctx
, "llvm.amdgcn.exp.f32",
1357 ctx
->voidt
, args
, 8, 0);
1362 args
[0] = LLVMConstInt(ctx
->i32
, a
->enabled_channels
, 0);
1363 args
[1] = LLVMConstInt(ctx
->i32
, a
->valid_mask
, 0);
1364 args
[2] = LLVMConstInt(ctx
->i32
, a
->done
, 0);
1365 args
[3] = LLVMConstInt(ctx
->i32
, a
->target
, 0);
1366 args
[4] = LLVMConstInt(ctx
->i32
, a
->compr
, 0);
1367 memcpy(args
+ 5, a
->out
, sizeof(a
->out
[0]) * 4);
1369 ac_build_intrinsic(ctx
, "llvm.SI.export", ctx
->voidt
, args
, 9,
1370 AC_FUNC_ATTR_LEGACY
);
1373 void ac_build_export_null(struct ac_llvm_context
*ctx
)
1375 struct ac_export_args args
;
1377 args
.enabled_channels
= 0x0; /* enabled channels */
1378 args
.valid_mask
= 1; /* whether the EXEC mask is valid */
1379 args
.done
= 1; /* DONE bit */
1380 args
.target
= V_008DFC_SQ_EXP_NULL
;
1381 args
.compr
= 0; /* COMPR flag (0 = 32-bit export) */
1382 args
.out
[0] = LLVMGetUndef(ctx
->f32
); /* R */
1383 args
.out
[1] = LLVMGetUndef(ctx
->f32
); /* G */
1384 args
.out
[2] = LLVMGetUndef(ctx
->f32
); /* B */
1385 args
.out
[3] = LLVMGetUndef(ctx
->f32
); /* A */
1387 ac_build_export(ctx
, &args
);
1390 LLVMValueRef
ac_build_image_opcode(struct ac_llvm_context
*ctx
,
1391 struct ac_image_args
*a
)
1393 LLVMValueRef args
[11];
1394 unsigned num_args
= 0;
1395 const char *name
= NULL
;
1396 char intr_name
[128], type
[64];
1398 bool sample
= a
->opcode
== ac_image_sample
||
1399 a
->opcode
== ac_image_gather4
||
1400 a
->opcode
== ac_image_get_lod
;
1403 args
[num_args
++] = ac_to_float(ctx
, a
->addr
);
1405 args
[num_args
++] = a
->addr
;
1407 args
[num_args
++] = a
->resource
;
1409 args
[num_args
++] = a
->sampler
;
1410 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->dmask
, 0);
1412 args
[num_args
++] = LLVMConstInt(ctx
->i1
, a
->unorm
, 0);
1413 args
[num_args
++] = ctx
->i1false
; /* glc */
1414 args
[num_args
++] = ctx
->i1false
; /* slc */
1415 args
[num_args
++] = ctx
->i1false
; /* lwe */
1416 args
[num_args
++] = LLVMConstInt(ctx
->i1
, a
->da
, 0);
1418 switch (a
->opcode
) {
1419 case ac_image_sample
:
1420 name
= "llvm.amdgcn.image.sample";
1422 case ac_image_gather4
:
1423 name
= "llvm.amdgcn.image.gather4";
1426 name
= "llvm.amdgcn.image.load";
1428 case ac_image_load_mip
:
1429 name
= "llvm.amdgcn.image.load.mip";
1431 case ac_image_get_lod
:
1432 name
= "llvm.amdgcn.image.getlod";
1434 case ac_image_get_resinfo
:
1435 name
= "llvm.amdgcn.image.getresinfo";
1438 unreachable("invalid image opcode");
1441 ac_build_type_name_for_intr(LLVMTypeOf(args
[0]), type
,
1444 snprintf(intr_name
, sizeof(intr_name
), "%s%s%s%s.v4f32.%s.v8i32",
1446 a
->compare
? ".c" : "",
1450 a
->level_zero
? ".lz" : "",
1451 a
->offset
? ".o" : "",
1454 LLVMValueRef result
=
1455 ac_build_intrinsic(ctx
, intr_name
,
1456 ctx
->v4f32
, args
, num_args
,
1457 AC_FUNC_ATTR_READNONE
);
1459 result
= LLVMBuildBitCast(ctx
->builder
, result
,
1465 LLVMValueRef
ac_build_cvt_pkrtz_f16(struct ac_llvm_context
*ctx
,
1466 LLVMValueRef args
[2])
1468 if (HAVE_LLVM
>= 0x0500) {
1470 LLVMVectorType(LLVMHalfTypeInContext(ctx
->context
), 2);
1472 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pkrtz",
1474 AC_FUNC_ATTR_READNONE
);
1475 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
1478 return ac_build_intrinsic(ctx
, "llvm.SI.packf16", ctx
->i32
, args
, 2,
1479 AC_FUNC_ATTR_READNONE
|
1480 AC_FUNC_ATTR_LEGACY
);
1483 /* Upper 16 bits must be zero. */
1484 static LLVMValueRef
ac_llvm_pack_two_int16(struct ac_llvm_context
*ctx
,
1485 LLVMValueRef val
[2])
1487 return LLVMBuildOr(ctx
->builder
, val
[0],
1488 LLVMBuildShl(ctx
->builder
, val
[1],
1489 LLVMConstInt(ctx
->i32
, 16, 0),
1493 /* Upper 16 bits are ignored and will be dropped. */
1494 static LLVMValueRef
ac_llvm_pack_two_int32_as_int16(struct ac_llvm_context
*ctx
,
1495 LLVMValueRef val
[2])
1497 LLVMValueRef v
[2] = {
1498 LLVMBuildAnd(ctx
->builder
, val
[0],
1499 LLVMConstInt(ctx
->i32
, 0xffff, 0), ""),
1502 return ac_llvm_pack_two_int16(ctx
, v
);
1505 LLVMValueRef
ac_build_cvt_pknorm_i16(struct ac_llvm_context
*ctx
,
1506 LLVMValueRef args
[2])
1508 if (HAVE_LLVM
>= 0x0600) {
1510 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pknorm.i16",
1511 ctx
->v2i16
, args
, 2,
1512 AC_FUNC_ATTR_READNONE
);
1513 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
1516 LLVMValueRef val
[2];
1518 for (int chan
= 0; chan
< 2; chan
++) {
1519 /* Clamp between [-1, 1]. */
1520 val
[chan
] = ac_build_fmin(ctx
, args
[chan
], ctx
->f32_1
);
1521 val
[chan
] = ac_build_fmax(ctx
, val
[chan
], LLVMConstReal(ctx
->f32
, -1));
1522 /* Convert to a signed integer in [-32767, 32767]. */
1523 val
[chan
] = LLVMBuildFMul(ctx
->builder
, val
[chan
],
1524 LLVMConstReal(ctx
->f32
, 32767), "");
1525 /* If positive, add 0.5, else add -0.5. */
1526 val
[chan
] = LLVMBuildFAdd(ctx
->builder
, val
[chan
],
1527 LLVMBuildSelect(ctx
->builder
,
1528 LLVMBuildFCmp(ctx
->builder
, LLVMRealOGE
,
1529 val
[chan
], ctx
->f32_0
, ""),
1530 LLVMConstReal(ctx
->f32
, 0.5),
1531 LLVMConstReal(ctx
->f32
, -0.5), ""), "");
1532 val
[chan
] = LLVMBuildFPToSI(ctx
->builder
, val
[chan
], ctx
->i32
, "");
1534 return ac_llvm_pack_two_int32_as_int16(ctx
, val
);
1537 LLVMValueRef
ac_build_cvt_pknorm_u16(struct ac_llvm_context
*ctx
,
1538 LLVMValueRef args
[2])
1540 if (HAVE_LLVM
>= 0x0600) {
1542 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pknorm.u16",
1543 ctx
->v2i16
, args
, 2,
1544 AC_FUNC_ATTR_READNONE
);
1545 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
1548 LLVMValueRef val
[2];
1550 for (int chan
= 0; chan
< 2; chan
++) {
1551 val
[chan
] = ac_build_clamp(ctx
, args
[chan
]);
1552 val
[chan
] = LLVMBuildFMul(ctx
->builder
, val
[chan
],
1553 LLVMConstReal(ctx
->f32
, 65535), "");
1554 val
[chan
] = LLVMBuildFAdd(ctx
->builder
, val
[chan
],
1555 LLVMConstReal(ctx
->f32
, 0.5), "");
1556 val
[chan
] = LLVMBuildFPToUI(ctx
->builder
, val
[chan
],
1559 return ac_llvm_pack_two_int32_as_int16(ctx
, val
);
1562 /* The 8-bit and 10-bit clamping is for HW workarounds. */
1563 LLVMValueRef
ac_build_cvt_pk_i16(struct ac_llvm_context
*ctx
,
1564 LLVMValueRef args
[2], unsigned bits
, bool hi
)
1566 assert(bits
== 8 || bits
== 10 || bits
== 16);
1568 LLVMValueRef max_rgb
= LLVMConstInt(ctx
->i32
,
1569 bits
== 8 ? 127 : bits
== 10 ? 511 : 32767, 0);
1570 LLVMValueRef min_rgb
= LLVMConstInt(ctx
->i32
,
1571 bits
== 8 ? -128 : bits
== 10 ? -512 : -32768, 0);
1572 LLVMValueRef max_alpha
=
1573 bits
!= 10 ? max_rgb
: ctx
->i32_1
;
1574 LLVMValueRef min_alpha
=
1575 bits
!= 10 ? min_rgb
: LLVMConstInt(ctx
->i32
, -2, 0);
1576 bool has_intrinsic
= HAVE_LLVM
>= 0x0600;
1579 if (!has_intrinsic
|| bits
!= 16) {
1580 for (int i
= 0; i
< 2; i
++) {
1581 bool alpha
= hi
&& i
== 1;
1582 args
[i
] = ac_build_imin(ctx
, args
[i
],
1583 alpha
? max_alpha
: max_rgb
);
1584 args
[i
] = ac_build_imax(ctx
, args
[i
],
1585 alpha
? min_alpha
: min_rgb
);
1589 if (has_intrinsic
) {
1591 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pk.i16",
1592 ctx
->v2i16
, args
, 2,
1593 AC_FUNC_ATTR_READNONE
);
1594 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
1597 return ac_llvm_pack_two_int32_as_int16(ctx
, args
);
1600 /* The 8-bit and 10-bit clamping is for HW workarounds. */
1601 LLVMValueRef
ac_build_cvt_pk_u16(struct ac_llvm_context
*ctx
,
1602 LLVMValueRef args
[2], unsigned bits
, bool hi
)
1604 assert(bits
== 8 || bits
== 10 || bits
== 16);
1606 LLVMValueRef max_rgb
= LLVMConstInt(ctx
->i32
,
1607 bits
== 8 ? 255 : bits
== 10 ? 1023 : 65535, 0);
1608 LLVMValueRef max_alpha
=
1609 bits
!= 10 ? max_rgb
: LLVMConstInt(ctx
->i32
, 3, 0);
1610 bool has_intrinsic
= HAVE_LLVM
>= 0x0600;
1613 if (!has_intrinsic
|| bits
!= 16) {
1614 for (int i
= 0; i
< 2; i
++) {
1615 bool alpha
= hi
&& i
== 1;
1616 args
[i
] = ac_build_umin(ctx
, args
[i
],
1617 alpha
? max_alpha
: max_rgb
);
1621 if (has_intrinsic
) {
1623 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pk.u16",
1624 ctx
->v2i16
, args
, 2,
1625 AC_FUNC_ATTR_READNONE
);
1626 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
1629 return ac_llvm_pack_two_int16(ctx
, args
);
1632 LLVMValueRef
ac_build_wqm_vote(struct ac_llvm_context
*ctx
, LLVMValueRef i1
)
1634 assert(HAVE_LLVM
>= 0x0600);
1635 return ac_build_intrinsic(ctx
, "llvm.amdgcn.wqm.vote", ctx
->i1
,
1636 &i1
, 1, AC_FUNC_ATTR_READNONE
);
1639 void ac_build_kill_if_false(struct ac_llvm_context
*ctx
, LLVMValueRef i1
)
1641 if (HAVE_LLVM
>= 0x0600) {
1642 ac_build_intrinsic(ctx
, "llvm.amdgcn.kill", ctx
->voidt
,
1647 LLVMValueRef value
= LLVMBuildSelect(ctx
->builder
, i1
,
1648 LLVMConstReal(ctx
->f32
, 1),
1649 LLVMConstReal(ctx
->f32
, -1), "");
1650 ac_build_intrinsic(ctx
, "llvm.AMDGPU.kill", ctx
->voidt
,
1651 &value
, 1, AC_FUNC_ATTR_LEGACY
);
1654 LLVMValueRef
ac_build_bfe(struct ac_llvm_context
*ctx
, LLVMValueRef input
,
1655 LLVMValueRef offset
, LLVMValueRef width
,
1658 LLVMValueRef args
[] = {
1664 if (HAVE_LLVM
>= 0x0500) {
1665 return ac_build_intrinsic(ctx
,
1666 is_signed
? "llvm.amdgcn.sbfe.i32" :
1667 "llvm.amdgcn.ubfe.i32",
1669 AC_FUNC_ATTR_READNONE
);
1672 return ac_build_intrinsic(ctx
,
1673 is_signed
? "llvm.AMDGPU.bfe.i32" :
1674 "llvm.AMDGPU.bfe.u32",
1676 AC_FUNC_ATTR_READNONE
|
1677 AC_FUNC_ATTR_LEGACY
);
1680 void ac_build_waitcnt(struct ac_llvm_context
*ctx
, unsigned simm16
)
1682 LLVMValueRef args
[1] = {
1683 LLVMConstInt(ctx
->i32
, simm16
, false),
1685 ac_build_intrinsic(ctx
, "llvm.amdgcn.s.waitcnt",
1686 ctx
->voidt
, args
, 1, 0);
1689 LLVMValueRef
ac_build_fract(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
1695 if (bitsize
== 32) {
1696 intr
= "llvm.floor.f32";
1699 intr
= "llvm.floor.f64";
1703 LLVMValueRef params
[] = {
1706 LLVMValueRef floor
= ac_build_intrinsic(ctx
, intr
, type
, params
, 1,
1707 AC_FUNC_ATTR_READNONE
);
1708 return LLVMBuildFSub(ctx
->builder
, src0
, floor
, "");
1711 LLVMValueRef
ac_build_isign(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
1714 LLVMValueRef cmp
, val
, zero
, one
;
1717 if (bitsize
== 32) {
1727 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGT
, src0
, zero
, "");
1728 val
= LLVMBuildSelect(ctx
->builder
, cmp
, one
, src0
, "");
1729 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGE
, val
, zero
, "");
1730 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstInt(type
, -1, true), "");
1734 void ac_get_image_intr_name(const char *base_name
,
1735 LLVMTypeRef data_type
,
1736 LLVMTypeRef coords_type
,
1737 LLVMTypeRef rsrc_type
,
1738 char *out_name
, unsigned out_len
)
1740 char coords_type_name
[8];
1742 ac_build_type_name_for_intr(coords_type
, coords_type_name
,
1743 sizeof(coords_type_name
));
1745 char data_type_name
[8];
1746 char rsrc_type_name
[8];
1748 ac_build_type_name_for_intr(data_type
, data_type_name
,
1749 sizeof(data_type_name
));
1750 ac_build_type_name_for_intr(rsrc_type
, rsrc_type_name
,
1751 sizeof(rsrc_type_name
));
1752 snprintf(out_name
, out_len
, "%s.%s.%s.%s", base_name
,
1753 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 unsigned src0_bitsize
= ac_get_elem_bits(ctx
, LLVMTypeOf(src0
));
2054 const char *intrin_name
;
2057 if (src0_bitsize
== 64) {
2058 intrin_name
= "llvm.cttz.i64";
2062 intrin_name
= "llvm.cttz.i32";
2067 LLVMValueRef params
[2] = {
2070 /* The value of 1 means that ffs(x=0) = undef, so LLVM won't
2071 * add special code to check for x=0. The reason is that
2072 * the LLVM behavior for x=0 is different from what we
2073 * need here. However, LLVM also assumes that ffs(x) is
2074 * in [0, 31], but GLSL expects that ffs(0) = -1, so
2075 * a conditional assignment to handle 0 is still required.
2077 * The hardware already implements the correct behavior.
2079 LLVMConstInt(ctx
->i1
, 1, false),
2082 LLVMValueRef lsb
= ac_build_intrinsic(ctx
, intrin_name
, type
,
2084 AC_FUNC_ATTR_READNONE
);
2086 if (src0_bitsize
== 64) {
2087 lsb
= LLVMBuildTrunc(ctx
->builder
, lsb
, ctx
->i32
, "");
2090 /* TODO: We need an intrinsic to skip this conditional. */
2091 /* Check for zero: */
2092 return LLVMBuildSelect(ctx
->builder
, LLVMBuildICmp(ctx
->builder
,
2095 LLVMConstInt(ctx
->i32
, -1, 0), lsb
, "");
2098 LLVMTypeRef
ac_array_in_const_addr_space(LLVMTypeRef elem_type
)
2100 return LLVMPointerType(LLVMArrayType(elem_type
, 0),
2101 AC_CONST_ADDR_SPACE
);
2104 LLVMTypeRef
ac_array_in_const32_addr_space(LLVMTypeRef elem_type
)
2106 if (!HAVE_32BIT_POINTERS
)
2107 return ac_array_in_const_addr_space(elem_type
);
2109 return LLVMPointerType(LLVMArrayType(elem_type
, 0),
2110 AC_CONST_32BIT_ADDR_SPACE
);