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 #define AC_LLVM_INITIAL_CF_DEPTH 4
46 /* Data for if/else/endif and bgnloop/endloop control flow structures.
49 /* Loop exit or next part of if/else/endif. */
50 LLVMBasicBlockRef next_block
;
51 LLVMBasicBlockRef loop_entry_block
;
54 /* Initialize module-independent parts of the context.
56 * The caller is responsible for initializing ctx::module and ctx::builder.
59 ac_llvm_context_init(struct ac_llvm_context
*ctx
, LLVMContextRef context
,
60 enum chip_class chip_class
, enum radeon_family family
)
64 ctx
->chip_class
= chip_class
;
67 ctx
->context
= context
;
71 ctx
->voidt
= LLVMVoidTypeInContext(ctx
->context
);
72 ctx
->i1
= LLVMInt1TypeInContext(ctx
->context
);
73 ctx
->i8
= LLVMInt8TypeInContext(ctx
->context
);
74 ctx
->i16
= LLVMIntTypeInContext(ctx
->context
, 16);
75 ctx
->i32
= LLVMIntTypeInContext(ctx
->context
, 32);
76 ctx
->i64
= LLVMIntTypeInContext(ctx
->context
, 64);
77 ctx
->intptr
= HAVE_32BIT_POINTERS
? ctx
->i32
: ctx
->i64
;
78 ctx
->f16
= LLVMHalfTypeInContext(ctx
->context
);
79 ctx
->f32
= LLVMFloatTypeInContext(ctx
->context
);
80 ctx
->f64
= LLVMDoubleTypeInContext(ctx
->context
);
81 ctx
->v2i16
= LLVMVectorType(ctx
->i16
, 2);
82 ctx
->v2i32
= LLVMVectorType(ctx
->i32
, 2);
83 ctx
->v3i32
= LLVMVectorType(ctx
->i32
, 3);
84 ctx
->v4i32
= LLVMVectorType(ctx
->i32
, 4);
85 ctx
->v2f32
= LLVMVectorType(ctx
->f32
, 2);
86 ctx
->v4f32
= LLVMVectorType(ctx
->f32
, 4);
87 ctx
->v8i32
= LLVMVectorType(ctx
->i32
, 8);
89 ctx
->i32_0
= LLVMConstInt(ctx
->i32
, 0, false);
90 ctx
->i32_1
= LLVMConstInt(ctx
->i32
, 1, false);
91 ctx
->i64_0
= LLVMConstInt(ctx
->i64
, 0, false);
92 ctx
->i64_1
= LLVMConstInt(ctx
->i64
, 1, false);
93 ctx
->f32_0
= LLVMConstReal(ctx
->f32
, 0.0);
94 ctx
->f32_1
= LLVMConstReal(ctx
->f32
, 1.0);
95 ctx
->f64_0
= LLVMConstReal(ctx
->f64
, 0.0);
96 ctx
->f64_1
= LLVMConstReal(ctx
->f64
, 1.0);
98 ctx
->i1false
= LLVMConstInt(ctx
->i1
, 0, false);
99 ctx
->i1true
= LLVMConstInt(ctx
->i1
, 1, false);
101 ctx
->range_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
104 ctx
->invariant_load_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
105 "invariant.load", 14);
107 ctx
->fpmath_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
, "fpmath", 6);
109 args
[0] = LLVMConstReal(ctx
->f32
, 2.5);
110 ctx
->fpmath_md_2p5_ulp
= LLVMMDNodeInContext(ctx
->context
, args
, 1);
112 ctx
->uniform_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
113 "amdgpu.uniform", 14);
115 ctx
->empty_md
= LLVMMDNodeInContext(ctx
->context
, NULL
, 0);
119 ac_llvm_context_dispose(struct ac_llvm_context
*ctx
)
123 ctx
->flow_depth_max
= 0;
127 ac_get_llvm_num_components(LLVMValueRef value
)
129 LLVMTypeRef type
= LLVMTypeOf(value
);
130 unsigned num_components
= LLVMGetTypeKind(type
) == LLVMVectorTypeKind
131 ? LLVMGetVectorSize(type
)
133 return num_components
;
137 ac_llvm_extract_elem(struct ac_llvm_context
*ac
,
141 if (LLVMGetTypeKind(LLVMTypeOf(value
)) != LLVMVectorTypeKind
) {
146 return LLVMBuildExtractElement(ac
->builder
, value
,
147 LLVMConstInt(ac
->i32
, index
, false), "");
151 ac_get_elem_bits(struct ac_llvm_context
*ctx
, LLVMTypeRef type
)
153 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
)
154 type
= LLVMGetElementType(type
);
156 if (LLVMGetTypeKind(type
) == LLVMIntegerTypeKind
)
157 return LLVMGetIntTypeWidth(type
);
159 if (type
== ctx
->f16
)
161 if (type
== ctx
->f32
)
163 if (type
== ctx
->f64
)
166 unreachable("Unhandled type kind in get_elem_bits");
170 ac_get_type_size(LLVMTypeRef type
)
172 LLVMTypeKind kind
= LLVMGetTypeKind(type
);
175 case LLVMIntegerTypeKind
:
176 return LLVMGetIntTypeWidth(type
) / 8;
177 case LLVMFloatTypeKind
:
179 case LLVMDoubleTypeKind
:
181 case LLVMPointerTypeKind
:
182 if (LLVMGetPointerAddressSpace(type
) == AC_CONST_32BIT_ADDR_SPACE
)
185 case LLVMVectorTypeKind
:
186 return LLVMGetVectorSize(type
) *
187 ac_get_type_size(LLVMGetElementType(type
));
188 case LLVMArrayTypeKind
:
189 return LLVMGetArrayLength(type
) *
190 ac_get_type_size(LLVMGetElementType(type
));
197 static LLVMTypeRef
to_integer_type_scalar(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
199 if (t
== ctx
->f16
|| t
== ctx
->i16
)
201 else if (t
== ctx
->f32
|| t
== ctx
->i32
)
203 else if (t
== ctx
->f64
|| t
== ctx
->i64
)
206 unreachable("Unhandled integer size");
210 ac_to_integer_type(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
212 if (LLVMGetTypeKind(t
) == LLVMVectorTypeKind
) {
213 LLVMTypeRef elem_type
= LLVMGetElementType(t
);
214 return LLVMVectorType(to_integer_type_scalar(ctx
, elem_type
),
215 LLVMGetVectorSize(t
));
217 return to_integer_type_scalar(ctx
, t
);
221 ac_to_integer(struct ac_llvm_context
*ctx
, LLVMValueRef v
)
223 LLVMTypeRef type
= LLVMTypeOf(v
);
224 return LLVMBuildBitCast(ctx
->builder
, v
, ac_to_integer_type(ctx
, type
), "");
227 static LLVMTypeRef
to_float_type_scalar(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
229 if (t
== ctx
->i16
|| t
== ctx
->f16
)
231 else if (t
== ctx
->i32
|| t
== ctx
->f32
)
233 else if (t
== ctx
->i64
|| t
== ctx
->f64
)
236 unreachable("Unhandled float size");
240 ac_to_float_type(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
242 if (LLVMGetTypeKind(t
) == LLVMVectorTypeKind
) {
243 LLVMTypeRef elem_type
= LLVMGetElementType(t
);
244 return LLVMVectorType(to_float_type_scalar(ctx
, elem_type
),
245 LLVMGetVectorSize(t
));
247 return to_float_type_scalar(ctx
, t
);
251 ac_to_float(struct ac_llvm_context
*ctx
, LLVMValueRef v
)
253 LLVMTypeRef type
= LLVMTypeOf(v
);
254 return LLVMBuildBitCast(ctx
->builder
, v
, ac_to_float_type(ctx
, type
), "");
259 ac_build_intrinsic(struct ac_llvm_context
*ctx
, const char *name
,
260 LLVMTypeRef return_type
, LLVMValueRef
*params
,
261 unsigned param_count
, unsigned attrib_mask
)
263 LLVMValueRef function
, call
;
264 bool set_callsite_attrs
= !(attrib_mask
& AC_FUNC_ATTR_LEGACY
);
266 function
= LLVMGetNamedFunction(ctx
->module
, name
);
268 LLVMTypeRef param_types
[32], function_type
;
271 assert(param_count
<= 32);
273 for (i
= 0; i
< param_count
; ++i
) {
275 param_types
[i
] = LLVMTypeOf(params
[i
]);
278 LLVMFunctionType(return_type
, param_types
, param_count
, 0);
279 function
= LLVMAddFunction(ctx
->module
, name
, function_type
);
281 LLVMSetFunctionCallConv(function
, LLVMCCallConv
);
282 LLVMSetLinkage(function
, LLVMExternalLinkage
);
284 if (!set_callsite_attrs
)
285 ac_add_func_attributes(ctx
->context
, function
, attrib_mask
);
288 call
= LLVMBuildCall(ctx
->builder
, function
, params
, param_count
, "");
289 if (set_callsite_attrs
)
290 ac_add_func_attributes(ctx
->context
, call
, attrib_mask
);
295 * Given the i32 or vNi32 \p type, generate the textual name (e.g. for use with
298 void ac_build_type_name_for_intr(LLVMTypeRef type
, char *buf
, unsigned bufsize
)
300 LLVMTypeRef elem_type
= type
;
302 assert(bufsize
>= 8);
304 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
) {
305 int ret
= snprintf(buf
, bufsize
, "v%u",
306 LLVMGetVectorSize(type
));
308 char *type_name
= LLVMPrintTypeToString(type
);
309 fprintf(stderr
, "Error building type name for: %s\n",
313 elem_type
= LLVMGetElementType(type
);
317 switch (LLVMGetTypeKind(elem_type
)) {
319 case LLVMIntegerTypeKind
:
320 snprintf(buf
, bufsize
, "i%d", LLVMGetIntTypeWidth(elem_type
));
322 case LLVMFloatTypeKind
:
323 snprintf(buf
, bufsize
, "f32");
325 case LLVMDoubleTypeKind
:
326 snprintf(buf
, bufsize
, "f64");
332 * Helper function that builds an LLVM IR PHI node and immediately adds
336 ac_build_phi(struct ac_llvm_context
*ctx
, LLVMTypeRef type
,
337 unsigned count_incoming
, LLVMValueRef
*values
,
338 LLVMBasicBlockRef
*blocks
)
340 LLVMValueRef phi
= LLVMBuildPhi(ctx
->builder
, type
, "");
341 LLVMAddIncoming(phi
, values
, blocks
, count_incoming
);
345 /* Prevent optimizations (at least of memory accesses) across the current
346 * point in the program by emitting empty inline assembly that is marked as
347 * having side effects.
349 * Optionally, a value can be passed through the inline assembly to prevent
350 * LLVM from hoisting calls to ReadNone functions.
353 ac_build_optimization_barrier(struct ac_llvm_context
*ctx
,
356 static int counter
= 0;
358 LLVMBuilderRef builder
= ctx
->builder
;
361 snprintf(code
, sizeof(code
), "; %d", p_atomic_inc_return(&counter
));
364 LLVMTypeRef ftype
= LLVMFunctionType(ctx
->voidt
, NULL
, 0, false);
365 LLVMValueRef inlineasm
= LLVMConstInlineAsm(ftype
, code
, "", true, false);
366 LLVMBuildCall(builder
, inlineasm
, NULL
, 0, "");
368 LLVMTypeRef ftype
= LLVMFunctionType(ctx
->i32
, &ctx
->i32
, 1, false);
369 LLVMValueRef inlineasm
= LLVMConstInlineAsm(ftype
, code
, "=v,0", true, false);
370 LLVMValueRef vgpr
= *pvgpr
;
371 LLVMTypeRef vgpr_type
= LLVMTypeOf(vgpr
);
372 unsigned vgpr_size
= ac_get_type_size(vgpr_type
);
375 assert(vgpr_size
% 4 == 0);
377 vgpr
= LLVMBuildBitCast(builder
, vgpr
, LLVMVectorType(ctx
->i32
, vgpr_size
/ 4), "");
378 vgpr0
= LLVMBuildExtractElement(builder
, vgpr
, ctx
->i32_0
, "");
379 vgpr0
= LLVMBuildCall(builder
, inlineasm
, &vgpr0
, 1, "");
380 vgpr
= LLVMBuildInsertElement(builder
, vgpr
, vgpr0
, ctx
->i32_0
, "");
381 vgpr
= LLVMBuildBitCast(builder
, vgpr
, vgpr_type
, "");
388 ac_build_shader_clock(struct ac_llvm_context
*ctx
)
390 LLVMValueRef tmp
= ac_build_intrinsic(ctx
, "llvm.readcyclecounter",
391 ctx
->i64
, NULL
, 0, 0);
392 return LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->v2i32
, "");
396 ac_build_ballot(struct ac_llvm_context
*ctx
,
399 LLVMValueRef args
[3] = {
402 LLVMConstInt(ctx
->i32
, LLVMIntNE
, 0)
405 /* We currently have no other way to prevent LLVM from lifting the icmp
406 * calls to a dominating basic block.
408 ac_build_optimization_barrier(ctx
, &args
[0]);
410 if (LLVMTypeOf(args
[0]) != ctx
->i32
)
411 args
[0] = LLVMBuildBitCast(ctx
->builder
, args
[0], ctx
->i32
, "");
413 return ac_build_intrinsic(ctx
,
414 "llvm.amdgcn.icmp.i32",
416 AC_FUNC_ATTR_NOUNWIND
|
417 AC_FUNC_ATTR_READNONE
|
418 AC_FUNC_ATTR_CONVERGENT
);
422 ac_build_vote_all(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
424 LLVMValueRef active_set
= ac_build_ballot(ctx
, ctx
->i32_1
);
425 LLVMValueRef vote_set
= ac_build_ballot(ctx
, value
);
426 return LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, vote_set
, active_set
, "");
430 ac_build_vote_any(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
432 LLVMValueRef vote_set
= ac_build_ballot(ctx
, value
);
433 return LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, vote_set
,
434 LLVMConstInt(ctx
->i64
, 0, 0), "");
438 ac_build_vote_eq(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
440 LLVMValueRef active_set
= ac_build_ballot(ctx
, ctx
->i32_1
);
441 LLVMValueRef vote_set
= ac_build_ballot(ctx
, value
);
443 LLVMValueRef all
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
444 vote_set
, active_set
, "");
445 LLVMValueRef none
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
447 LLVMConstInt(ctx
->i64
, 0, 0), "");
448 return LLVMBuildOr(ctx
->builder
, all
, none
, "");
452 ac_build_varying_gather_values(struct ac_llvm_context
*ctx
, LLVMValueRef
*values
,
453 unsigned value_count
, unsigned component
)
455 LLVMValueRef vec
= NULL
;
457 if (value_count
== 1) {
458 return values
[component
];
459 } else if (!value_count
)
460 unreachable("value_count is 0");
462 for (unsigned i
= component
; i
< value_count
+ component
; i
++) {
463 LLVMValueRef value
= values
[i
];
466 vec
= LLVMGetUndef( LLVMVectorType(LLVMTypeOf(value
), value_count
));
467 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, i
- component
, false);
468 vec
= LLVMBuildInsertElement(ctx
->builder
, vec
, value
, index
, "");
474 ac_build_gather_values_extended(struct ac_llvm_context
*ctx
,
475 LLVMValueRef
*values
,
476 unsigned value_count
,
477 unsigned value_stride
,
481 LLVMBuilderRef builder
= ctx
->builder
;
482 LLVMValueRef vec
= NULL
;
485 if (value_count
== 1 && !always_vector
) {
487 return LLVMBuildLoad(builder
, values
[0], "");
489 } else if (!value_count
)
490 unreachable("value_count is 0");
492 for (i
= 0; i
< value_count
; i
++) {
493 LLVMValueRef value
= values
[i
* value_stride
];
495 value
= LLVMBuildLoad(builder
, value
, "");
498 vec
= LLVMGetUndef( LLVMVectorType(LLVMTypeOf(value
), value_count
));
499 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, i
, false);
500 vec
= LLVMBuildInsertElement(builder
, vec
, value
, index
, "");
506 ac_build_gather_values(struct ac_llvm_context
*ctx
,
507 LLVMValueRef
*values
,
508 unsigned value_count
)
510 return ac_build_gather_values_extended(ctx
, values
, value_count
, 1, false, false);
513 /* Expand a scalar or vector to <4 x type> by filling the remaining channels
514 * with undef. Extract at most num_channels components from the input.
516 LLVMValueRef
ac_build_expand_to_vec4(struct ac_llvm_context
*ctx
,
518 unsigned num_channels
)
520 LLVMTypeRef elemtype
;
521 LLVMValueRef chan
[4];
523 if (LLVMGetTypeKind(LLVMTypeOf(value
)) == LLVMVectorTypeKind
) {
524 unsigned vec_size
= LLVMGetVectorSize(LLVMTypeOf(value
));
525 num_channels
= MIN2(num_channels
, vec_size
);
527 if (num_channels
>= 4)
530 for (unsigned i
= 0; i
< num_channels
; i
++)
531 chan
[i
] = ac_llvm_extract_elem(ctx
, value
, i
);
533 elemtype
= LLVMGetElementType(LLVMTypeOf(value
));
536 assert(num_channels
== 1);
539 elemtype
= LLVMTypeOf(value
);
542 while (num_channels
< 4)
543 chan
[num_channels
++] = LLVMGetUndef(elemtype
);
545 return ac_build_gather_values(ctx
, chan
, 4);
549 ac_build_fdiv(struct ac_llvm_context
*ctx
,
553 LLVMValueRef ret
= LLVMBuildFDiv(ctx
->builder
, num
, den
, "");
555 /* Use v_rcp_f32 instead of precise division. */
556 if (!LLVMIsConstant(ret
))
557 LLVMSetMetadata(ret
, ctx
->fpmath_md_kind
, ctx
->fpmath_md_2p5_ulp
);
561 /* Coordinates for cube map selection. sc, tc, and ma are as in Table 8.27
562 * of the OpenGL 4.5 (Compatibility Profile) specification, except ma is
563 * already multiplied by two. id is the cube face number.
565 struct cube_selection_coords
{
572 build_cube_intrinsic(struct ac_llvm_context
*ctx
,
574 struct cube_selection_coords
*out
)
576 LLVMTypeRef f32
= ctx
->f32
;
578 out
->stc
[1] = ac_build_intrinsic(ctx
, "llvm.amdgcn.cubetc",
579 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
580 out
->stc
[0] = ac_build_intrinsic(ctx
, "llvm.amdgcn.cubesc",
581 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
582 out
->ma
= ac_build_intrinsic(ctx
, "llvm.amdgcn.cubema",
583 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
584 out
->id
= ac_build_intrinsic(ctx
, "llvm.amdgcn.cubeid",
585 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
589 * Build a manual selection sequence for cube face sc/tc coordinates and
590 * major axis vector (multiplied by 2 for consistency) for the given
591 * vec3 \p coords, for the face implied by \p selcoords.
593 * For the major axis, we always adjust the sign to be in the direction of
594 * selcoords.ma; i.e., a positive out_ma means that coords is pointed towards
595 * the selcoords major axis.
597 static void build_cube_select(struct ac_llvm_context
*ctx
,
598 const struct cube_selection_coords
*selcoords
,
599 const LLVMValueRef
*coords
,
600 LLVMValueRef
*out_st
,
601 LLVMValueRef
*out_ma
)
603 LLVMBuilderRef builder
= ctx
->builder
;
604 LLVMTypeRef f32
= LLVMTypeOf(coords
[0]);
605 LLVMValueRef is_ma_positive
;
607 LLVMValueRef is_ma_z
, is_not_ma_z
;
608 LLVMValueRef is_ma_y
;
609 LLVMValueRef is_ma_x
;
613 is_ma_positive
= LLVMBuildFCmp(builder
, LLVMRealUGE
,
614 selcoords
->ma
, LLVMConstReal(f32
, 0.0), "");
615 sgn_ma
= LLVMBuildSelect(builder
, is_ma_positive
,
616 LLVMConstReal(f32
, 1.0), LLVMConstReal(f32
, -1.0), "");
618 is_ma_z
= LLVMBuildFCmp(builder
, LLVMRealUGE
, selcoords
->id
, LLVMConstReal(f32
, 4.0), "");
619 is_not_ma_z
= LLVMBuildNot(builder
, is_ma_z
, "");
620 is_ma_y
= LLVMBuildAnd(builder
, is_not_ma_z
,
621 LLVMBuildFCmp(builder
, LLVMRealUGE
, selcoords
->id
, LLVMConstReal(f32
, 2.0), ""), "");
622 is_ma_x
= LLVMBuildAnd(builder
, is_not_ma_z
, LLVMBuildNot(builder
, is_ma_y
, ""), "");
625 tmp
= LLVMBuildSelect(builder
, is_ma_x
, coords
[2], coords
[0], "");
626 sgn
= LLVMBuildSelect(builder
, is_ma_y
, LLVMConstReal(f32
, 1.0),
627 LLVMBuildSelect(builder
, is_ma_z
, sgn_ma
,
628 LLVMBuildFNeg(builder
, sgn_ma
, ""), ""), "");
629 out_st
[0] = LLVMBuildFMul(builder
, tmp
, sgn
, "");
632 tmp
= LLVMBuildSelect(builder
, is_ma_y
, coords
[2], coords
[1], "");
633 sgn
= LLVMBuildSelect(builder
, is_ma_y
, sgn_ma
,
634 LLVMConstReal(f32
, -1.0), "");
635 out_st
[1] = LLVMBuildFMul(builder
, tmp
, sgn
, "");
638 tmp
= LLVMBuildSelect(builder
, is_ma_z
, coords
[2],
639 LLVMBuildSelect(builder
, is_ma_y
, coords
[1], coords
[0], ""), "");
640 tmp
= ac_build_intrinsic(ctx
, "llvm.fabs.f32",
641 ctx
->f32
, &tmp
, 1, AC_FUNC_ATTR_READNONE
);
642 *out_ma
= LLVMBuildFMul(builder
, tmp
, LLVMConstReal(f32
, 2.0), "");
646 ac_prepare_cube_coords(struct ac_llvm_context
*ctx
,
647 bool is_deriv
, bool is_array
, bool is_lod
,
648 LLVMValueRef
*coords_arg
,
649 LLVMValueRef
*derivs_arg
)
652 LLVMBuilderRef builder
= ctx
->builder
;
653 struct cube_selection_coords selcoords
;
654 LLVMValueRef coords
[3];
657 if (is_array
&& !is_lod
) {
658 LLVMValueRef tmp
= coords_arg
[3];
659 tmp
= ac_build_intrinsic(ctx
, "llvm.rint.f32", ctx
->f32
, &tmp
, 1, 0);
661 /* Section 8.9 (Texture Functions) of the GLSL 4.50 spec says:
663 * "For Array forms, the array layer used will be
665 * max(0, min(d−1, floor(layer+0.5)))
667 * where d is the depth of the texture array and layer
668 * comes from the component indicated in the tables below.
669 * Workaroudn for an issue where the layer is taken from a
670 * helper invocation which happens to fall on a different
671 * layer due to extrapolation."
673 * VI and earlier attempt to implement this in hardware by
674 * clamping the value of coords[2] = (8 * layer) + face.
675 * Unfortunately, this means that the we end up with the wrong
676 * face when clamping occurs.
678 * Clamp the layer earlier to work around the issue.
680 if (ctx
->chip_class
<= VI
) {
682 ge0
= LLVMBuildFCmp(builder
, LLVMRealOGE
, tmp
, ctx
->f32_0
, "");
683 tmp
= LLVMBuildSelect(builder
, ge0
, tmp
, ctx
->f32_0
, "");
689 build_cube_intrinsic(ctx
, coords_arg
, &selcoords
);
691 invma
= ac_build_intrinsic(ctx
, "llvm.fabs.f32",
692 ctx
->f32
, &selcoords
.ma
, 1, AC_FUNC_ATTR_READNONE
);
693 invma
= ac_build_fdiv(ctx
, LLVMConstReal(ctx
->f32
, 1.0), invma
);
695 for (int i
= 0; i
< 2; ++i
)
696 coords
[i
] = LLVMBuildFMul(builder
, selcoords
.stc
[i
], invma
, "");
698 coords
[2] = selcoords
.id
;
700 if (is_deriv
&& derivs_arg
) {
701 LLVMValueRef derivs
[4];
704 /* Convert cube derivatives to 2D derivatives. */
705 for (axis
= 0; axis
< 2; axis
++) {
706 LLVMValueRef deriv_st
[2];
707 LLVMValueRef deriv_ma
;
709 /* Transform the derivative alongside the texture
710 * coordinate. Mathematically, the correct formula is
711 * as follows. Assume we're projecting onto the +Z face
712 * and denote by dx/dh the derivative of the (original)
713 * X texture coordinate with respect to horizontal
714 * window coordinates. The projection onto the +Z face
719 * Then df/dh = df/dx * dx/dh + df/dz * dz/dh
720 * = 1/z * dx/dh - x/z * 1/z * dz/dh.
722 * This motivatives the implementation below.
724 * Whether this actually gives the expected results for
725 * apps that might feed in derivatives obtained via
726 * finite differences is anyone's guess. The OpenGL spec
727 * seems awfully quiet about how textureGrad for cube
728 * maps should be handled.
730 build_cube_select(ctx
, &selcoords
, &derivs_arg
[axis
* 3],
731 deriv_st
, &deriv_ma
);
733 deriv_ma
= LLVMBuildFMul(builder
, deriv_ma
, invma
, "");
735 for (int i
= 0; i
< 2; ++i
)
736 derivs
[axis
* 2 + i
] =
737 LLVMBuildFSub(builder
,
738 LLVMBuildFMul(builder
, deriv_st
[i
], invma
, ""),
739 LLVMBuildFMul(builder
, deriv_ma
, coords
[i
], ""), "");
742 memcpy(derivs_arg
, derivs
, sizeof(derivs
));
745 /* Shift the texture coordinate. This must be applied after the
746 * derivative calculation.
748 for (int i
= 0; i
< 2; ++i
)
749 coords
[i
] = LLVMBuildFAdd(builder
, coords
[i
], LLVMConstReal(ctx
->f32
, 1.5), "");
752 /* for cube arrays coord.z = coord.w(array_index) * 8 + face */
753 /* coords_arg.w component - array_index for cube arrays */
754 LLVMValueRef tmp
= LLVMBuildFMul(ctx
->builder
, coords_arg
[3], LLVMConstReal(ctx
->f32
, 8.0), "");
755 coords
[2] = LLVMBuildFAdd(ctx
->builder
, tmp
, coords
[2], "");
758 memcpy(coords_arg
, coords
, sizeof(coords
));
763 ac_build_fs_interp(struct ac_llvm_context
*ctx
,
764 LLVMValueRef llvm_chan
,
765 LLVMValueRef attr_number
,
770 LLVMValueRef args
[5];
775 args
[2] = attr_number
;
778 p1
= ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p1",
779 ctx
->f32
, args
, 4, AC_FUNC_ATTR_READNONE
);
784 args
[3] = attr_number
;
787 return ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p2",
788 ctx
->f32
, args
, 5, AC_FUNC_ATTR_READNONE
);
792 ac_build_fs_interp_mov(struct ac_llvm_context
*ctx
,
793 LLVMValueRef parameter
,
794 LLVMValueRef llvm_chan
,
795 LLVMValueRef attr_number
,
798 LLVMValueRef args
[4];
802 args
[2] = attr_number
;
805 return ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.mov",
806 ctx
->f32
, args
, 4, AC_FUNC_ATTR_READNONE
);
810 ac_build_gep0(struct ac_llvm_context
*ctx
,
811 LLVMValueRef base_ptr
,
814 LLVMValueRef indices
[2] = {
815 LLVMConstInt(ctx
->i32
, 0, 0),
818 return LLVMBuildGEP(ctx
->builder
, base_ptr
,
823 ac_build_indexed_store(struct ac_llvm_context
*ctx
,
824 LLVMValueRef base_ptr
, LLVMValueRef index
,
827 LLVMBuildStore(ctx
->builder
, value
,
828 ac_build_gep0(ctx
, base_ptr
, index
));
832 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad.
833 * It's equivalent to doing a load from &base_ptr[index].
835 * \param base_ptr Where the array starts.
836 * \param index The element index into the array.
837 * \param uniform Whether the base_ptr and index can be assumed to be
838 * dynamically uniform (i.e. load to an SGPR)
839 * \param invariant Whether the load is invariant (no other opcodes affect it)
842 ac_build_load_custom(struct ac_llvm_context
*ctx
, LLVMValueRef base_ptr
,
843 LLVMValueRef index
, bool uniform
, bool invariant
)
845 LLVMValueRef pointer
, result
;
847 pointer
= ac_build_gep0(ctx
, base_ptr
, index
);
849 LLVMSetMetadata(pointer
, ctx
->uniform_md_kind
, ctx
->empty_md
);
850 result
= LLVMBuildLoad(ctx
->builder
, pointer
, "");
852 LLVMSetMetadata(result
, ctx
->invariant_load_md_kind
, ctx
->empty_md
);
856 LLVMValueRef
ac_build_load(struct ac_llvm_context
*ctx
, LLVMValueRef base_ptr
,
859 return ac_build_load_custom(ctx
, base_ptr
, index
, false, false);
862 LLVMValueRef
ac_build_load_invariant(struct ac_llvm_context
*ctx
,
863 LLVMValueRef base_ptr
, LLVMValueRef index
)
865 return ac_build_load_custom(ctx
, base_ptr
, index
, false, true);
868 LLVMValueRef
ac_build_load_to_sgpr(struct ac_llvm_context
*ctx
,
869 LLVMValueRef base_ptr
, LLVMValueRef index
)
871 return ac_build_load_custom(ctx
, base_ptr
, index
, true, true);
874 /* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
875 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
876 * or v4i32 (num_channels=3,4).
879 ac_build_buffer_store_dword(struct ac_llvm_context
*ctx
,
882 unsigned num_channels
,
883 LLVMValueRef voffset
,
884 LLVMValueRef soffset
,
885 unsigned inst_offset
,
888 bool writeonly_memory
,
889 bool swizzle_enable_hint
)
891 /* SWIZZLE_ENABLE requires that soffset isn't folded into voffset
892 * (voffset is swizzled, but soffset isn't swizzled).
893 * llvm.amdgcn.buffer.store doesn't have a separate soffset parameter.
895 if (!swizzle_enable_hint
) {
896 /* Split 3 channel stores, becase LLVM doesn't support 3-channel
898 if (num_channels
== 3) {
899 LLVMValueRef v
[3], v01
;
901 for (int i
= 0; i
< 3; i
++) {
902 v
[i
] = LLVMBuildExtractElement(ctx
->builder
, vdata
,
903 LLVMConstInt(ctx
->i32
, i
, 0), "");
905 v01
= ac_build_gather_values(ctx
, v
, 2);
907 ac_build_buffer_store_dword(ctx
, rsrc
, v01
, 2, voffset
,
908 soffset
, inst_offset
, glc
, slc
,
909 writeonly_memory
, swizzle_enable_hint
);
910 ac_build_buffer_store_dword(ctx
, rsrc
, v
[2], 1, voffset
,
911 soffset
, inst_offset
+ 8,
913 writeonly_memory
, swizzle_enable_hint
);
917 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
918 static const char *types
[] = {"f32", "v2f32", "v4f32"};
920 LLVMValueRef offset
= soffset
;
923 offset
= LLVMBuildAdd(ctx
->builder
, offset
,
924 LLVMConstInt(ctx
->i32
, inst_offset
, 0), "");
926 offset
= LLVMBuildAdd(ctx
->builder
, offset
, voffset
, "");
928 LLVMValueRef args
[] = {
929 ac_to_float(ctx
, vdata
),
930 LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, ""),
931 LLVMConstInt(ctx
->i32
, 0, 0),
933 LLVMConstInt(ctx
->i1
, glc
, 0),
934 LLVMConstInt(ctx
->i1
, slc
, 0),
937 snprintf(name
, sizeof(name
), "llvm.amdgcn.buffer.store.%s",
940 ac_build_intrinsic(ctx
, name
, ctx
->voidt
,
941 args
, ARRAY_SIZE(args
),
943 AC_FUNC_ATTR_INACCESSIBLE_MEM_ONLY
:
944 AC_FUNC_ATTR_WRITEONLY
);
948 static unsigned dfmt
[] = {
949 V_008F0C_BUF_DATA_FORMAT_32
,
950 V_008F0C_BUF_DATA_FORMAT_32_32
,
951 V_008F0C_BUF_DATA_FORMAT_32_32_32
,
952 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
954 assert(num_channels
>= 1 && num_channels
<= 4);
956 LLVMValueRef args
[] = {
959 LLVMConstInt(ctx
->i32
, num_channels
, 0),
960 voffset
? voffset
: LLVMGetUndef(ctx
->i32
),
962 LLVMConstInt(ctx
->i32
, inst_offset
, 0),
963 LLVMConstInt(ctx
->i32
, dfmt
[num_channels
- 1], 0),
964 LLVMConstInt(ctx
->i32
, V_008F0C_BUF_NUM_FORMAT_UINT
, 0),
965 LLVMConstInt(ctx
->i32
, voffset
!= NULL
, 0),
966 LLVMConstInt(ctx
->i32
, 0, 0), /* idxen */
967 LLVMConstInt(ctx
->i32
, glc
, 0),
968 LLVMConstInt(ctx
->i32
, slc
, 0),
969 LLVMConstInt(ctx
->i32
, 0, 0), /* tfe*/
972 /* The instruction offset field has 12 bits */
973 assert(voffset
|| inst_offset
< (1 << 12));
975 /* The intrinsic is overloaded, we need to add a type suffix for overloading to work. */
976 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
977 const char *types
[] = {"i32", "v2i32", "v4i32"};
979 snprintf(name
, sizeof(name
), "llvm.SI.tbuffer.store.%s", types
[func
]);
981 ac_build_intrinsic(ctx
, name
, ctx
->voidt
,
982 args
, ARRAY_SIZE(args
),
983 AC_FUNC_ATTR_LEGACY
);
987 ac_build_buffer_load_common(struct ac_llvm_context
*ctx
,
990 LLVMValueRef voffset
,
991 unsigned num_channels
,
997 LLVMValueRef args
[] = {
998 LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, ""),
999 vindex
? vindex
: LLVMConstInt(ctx
->i32
, 0, 0),
1001 LLVMConstInt(ctx
->i1
, glc
, 0),
1002 LLVMConstInt(ctx
->i1
, slc
, 0)
1004 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
1006 LLVMTypeRef types
[] = {ctx
->f32
, ctx
->v2f32
, ctx
->v4f32
};
1007 const char *type_names
[] = {"f32", "v2f32", "v4f32"};
1011 snprintf(name
, sizeof(name
), "llvm.amdgcn.buffer.load.format.%s",
1014 snprintf(name
, sizeof(name
), "llvm.amdgcn.buffer.load.%s",
1018 return ac_build_intrinsic(ctx
, name
, types
[func
], args
,
1020 ac_get_load_intr_attribs(can_speculate
));
1024 ac_build_buffer_load(struct ac_llvm_context
*ctx
,
1027 LLVMValueRef vindex
,
1028 LLVMValueRef voffset
,
1029 LLVMValueRef soffset
,
1030 unsigned inst_offset
,
1036 LLVMValueRef offset
= LLVMConstInt(ctx
->i32
, inst_offset
, 0);
1038 offset
= LLVMBuildAdd(ctx
->builder
, offset
, voffset
, "");
1040 offset
= LLVMBuildAdd(ctx
->builder
, offset
, soffset
, "");
1042 /* TODO: VI and later generations can use SMEM with GLC=1.*/
1043 if (allow_smem
&& !glc
&& !slc
) {
1044 assert(vindex
== NULL
);
1046 LLVMValueRef result
[8];
1048 for (int i
= 0; i
< num_channels
; i
++) {
1050 offset
= LLVMBuildAdd(ctx
->builder
, offset
,
1051 LLVMConstInt(ctx
->i32
, 4, 0), "");
1053 LLVMValueRef args
[2] = {rsrc
, offset
};
1054 result
[i
] = ac_build_intrinsic(ctx
, "llvm.SI.load.const.v4i32",
1056 AC_FUNC_ATTR_READNONE
|
1057 AC_FUNC_ATTR_LEGACY
);
1059 if (num_channels
== 1)
1062 if (num_channels
== 3)
1063 result
[num_channels
++] = LLVMGetUndef(ctx
->f32
);
1064 return ac_build_gather_values(ctx
, result
, num_channels
);
1067 return ac_build_buffer_load_common(ctx
, rsrc
, vindex
, offset
,
1068 num_channels
, glc
, slc
,
1069 can_speculate
, false);
1072 LLVMValueRef
ac_build_buffer_load_format(struct ac_llvm_context
*ctx
,
1074 LLVMValueRef vindex
,
1075 LLVMValueRef voffset
,
1076 unsigned num_channels
,
1080 return ac_build_buffer_load_common(ctx
, rsrc
, vindex
, voffset
,
1081 num_channels
, glc
, false,
1082 can_speculate
, true);
1086 * Set range metadata on an instruction. This can only be used on load and
1087 * call instructions. If you know an instruction can only produce the values
1088 * 0, 1, 2, you would do set_range_metadata(value, 0, 3);
1089 * \p lo is the minimum value inclusive.
1090 * \p hi is the maximum value exclusive.
1092 static void set_range_metadata(struct ac_llvm_context
*ctx
,
1093 LLVMValueRef value
, unsigned lo
, unsigned hi
)
1095 LLVMValueRef range_md
, md_args
[2];
1096 LLVMTypeRef type
= LLVMTypeOf(value
);
1097 LLVMContextRef context
= LLVMGetTypeContext(type
);
1099 md_args
[0] = LLVMConstInt(type
, lo
, false);
1100 md_args
[1] = LLVMConstInt(type
, hi
, false);
1101 range_md
= LLVMMDNodeInContext(context
, md_args
, 2);
1102 LLVMSetMetadata(value
, ctx
->range_md_kind
, range_md
);
1106 ac_get_thread_id(struct ac_llvm_context
*ctx
)
1110 LLVMValueRef tid_args
[2];
1111 tid_args
[0] = LLVMConstInt(ctx
->i32
, 0xffffffff, false);
1112 tid_args
[1] = LLVMConstInt(ctx
->i32
, 0, false);
1113 tid_args
[1] = ac_build_intrinsic(ctx
,
1114 "llvm.amdgcn.mbcnt.lo", ctx
->i32
,
1115 tid_args
, 2, AC_FUNC_ATTR_READNONE
);
1117 tid
= ac_build_intrinsic(ctx
, "llvm.amdgcn.mbcnt.hi",
1119 2, AC_FUNC_ATTR_READNONE
);
1120 set_range_metadata(ctx
, tid
, 0, 64);
1125 * SI implements derivatives using the local data store (LDS)
1126 * All writes to the LDS happen in all executing threads at
1127 * the same time. TID is the Thread ID for the current
1128 * thread and is a value between 0 and 63, representing
1129 * the thread's position in the wavefront.
1131 * For the pixel shader threads are grouped into quads of four pixels.
1132 * The TIDs of the pixels of a quad are:
1140 * So, masking the TID with 0xfffffffc yields the TID of the top left pixel
1141 * of the quad, masking with 0xfffffffd yields the TID of the top pixel of
1142 * the current pixel's column, and masking with 0xfffffffe yields the TID
1143 * of the left pixel of the current pixel's row.
1145 * Adding 1 yields the TID of the pixel to the right of the left pixel, and
1146 * adding 2 yields the TID of the pixel below the top pixel.
1149 ac_build_ddxy(struct ac_llvm_context
*ctx
,
1154 LLVMValueRef tl
, trbl
, args
[2];
1155 LLVMValueRef result
;
1157 if (ctx
->chip_class
>= VI
) {
1158 LLVMValueRef thread_id
, tl_tid
, trbl_tid
;
1159 thread_id
= ac_get_thread_id(ctx
);
1161 tl_tid
= LLVMBuildAnd(ctx
->builder
, thread_id
,
1162 LLVMConstInt(ctx
->i32
, mask
, false), "");
1164 trbl_tid
= LLVMBuildAdd(ctx
->builder
, tl_tid
,
1165 LLVMConstInt(ctx
->i32
, idx
, false), "");
1167 args
[0] = LLVMBuildMul(ctx
->builder
, tl_tid
,
1168 LLVMConstInt(ctx
->i32
, 4, false), "");
1170 tl
= ac_build_intrinsic(ctx
,
1171 "llvm.amdgcn.ds.bpermute", ctx
->i32
,
1173 AC_FUNC_ATTR_READNONE
|
1174 AC_FUNC_ATTR_CONVERGENT
);
1176 args
[0] = LLVMBuildMul(ctx
->builder
, trbl_tid
,
1177 LLVMConstInt(ctx
->i32
, 4, false), "");
1178 trbl
= ac_build_intrinsic(ctx
,
1179 "llvm.amdgcn.ds.bpermute", ctx
->i32
,
1181 AC_FUNC_ATTR_READNONE
|
1182 AC_FUNC_ATTR_CONVERGENT
);
1184 uint32_t masks
[2] = {};
1187 case AC_TID_MASK_TOP_LEFT
:
1195 case AC_TID_MASK_TOP
:
1199 case AC_TID_MASK_LEFT
:
1208 args
[1] = LLVMConstInt(ctx
->i32
, masks
[0], false);
1210 tl
= ac_build_intrinsic(ctx
,
1211 "llvm.amdgcn.ds.swizzle", ctx
->i32
,
1213 AC_FUNC_ATTR_READNONE
|
1214 AC_FUNC_ATTR_CONVERGENT
);
1216 args
[1] = LLVMConstInt(ctx
->i32
, masks
[1], false);
1217 trbl
= ac_build_intrinsic(ctx
,
1218 "llvm.amdgcn.ds.swizzle", ctx
->i32
,
1220 AC_FUNC_ATTR_READNONE
|
1221 AC_FUNC_ATTR_CONVERGENT
);
1224 tl
= LLVMBuildBitCast(ctx
->builder
, tl
, ctx
->f32
, "");
1225 trbl
= LLVMBuildBitCast(ctx
->builder
, trbl
, ctx
->f32
, "");
1226 result
= LLVMBuildFSub(ctx
->builder
, trbl
, tl
, "");
1231 ac_build_sendmsg(struct ac_llvm_context
*ctx
,
1233 LLVMValueRef wave_id
)
1235 LLVMValueRef args
[2];
1236 args
[0] = LLVMConstInt(ctx
->i32
, msg
, false);
1238 ac_build_intrinsic(ctx
, "llvm.amdgcn.s.sendmsg", ctx
->voidt
, args
, 2, 0);
1242 ac_build_imsb(struct ac_llvm_context
*ctx
,
1244 LLVMTypeRef dst_type
)
1246 LLVMValueRef msb
= ac_build_intrinsic(ctx
, "llvm.amdgcn.sffbh.i32",
1248 AC_FUNC_ATTR_READNONE
);
1250 /* The HW returns the last bit index from MSB, but NIR/TGSI wants
1251 * the index from LSB. Invert it by doing "31 - msb". */
1252 msb
= LLVMBuildSub(ctx
->builder
, LLVMConstInt(ctx
->i32
, 31, false),
1255 LLVMValueRef all_ones
= LLVMConstInt(ctx
->i32
, -1, true);
1256 LLVMValueRef cond
= LLVMBuildOr(ctx
->builder
,
1257 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
1258 arg
, LLVMConstInt(ctx
->i32
, 0, 0), ""),
1259 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
1260 arg
, all_ones
, ""), "");
1262 return LLVMBuildSelect(ctx
->builder
, cond
, all_ones
, msb
, "");
1266 ac_build_umsb(struct ac_llvm_context
*ctx
,
1268 LLVMTypeRef dst_type
)
1270 LLVMValueRef args
[2] = {
1274 LLVMValueRef msb
= ac_build_intrinsic(ctx
, "llvm.ctlz.i32",
1275 dst_type
, args
, ARRAY_SIZE(args
),
1276 AC_FUNC_ATTR_READNONE
);
1278 /* The HW returns the last bit index from MSB, but TGSI/NIR wants
1279 * the index from LSB. Invert it by doing "31 - msb". */
1280 msb
= LLVMBuildSub(ctx
->builder
, LLVMConstInt(ctx
->i32
, 31, false),
1283 /* check for zero */
1284 return LLVMBuildSelect(ctx
->builder
,
1285 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, arg
,
1286 LLVMConstInt(ctx
->i32
, 0, 0), ""),
1287 LLVMConstInt(ctx
->i32
, -1, true), msb
, "");
1290 LLVMValueRef
ac_build_fmin(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
1293 LLVMValueRef args
[2] = {a
, b
};
1294 return ac_build_intrinsic(ctx
, "llvm.minnum.f32", ctx
->f32
, args
, 2,
1295 AC_FUNC_ATTR_READNONE
);
1298 LLVMValueRef
ac_build_fmax(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
1301 LLVMValueRef args
[2] = {a
, b
};
1302 return ac_build_intrinsic(ctx
, "llvm.maxnum.f32", ctx
->f32
, args
, 2,
1303 AC_FUNC_ATTR_READNONE
);
1306 LLVMValueRef
ac_build_imin(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
1309 LLVMValueRef cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSLE
, a
, b
, "");
1310 return LLVMBuildSelect(ctx
->builder
, cmp
, a
, b
, "");
1313 LLVMValueRef
ac_build_imax(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
1316 LLVMValueRef cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGT
, a
, b
, "");
1317 return LLVMBuildSelect(ctx
->builder
, cmp
, a
, b
, "");
1320 LLVMValueRef
ac_build_umin(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
1323 LLVMValueRef cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntULE
, a
, b
, "");
1324 return LLVMBuildSelect(ctx
->builder
, cmp
, a
, b
, "");
1327 LLVMValueRef
ac_build_clamp(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
1329 if (HAVE_LLVM
>= 0x0500) {
1330 return ac_build_fmin(ctx
, ac_build_fmax(ctx
, value
, ctx
->f32_0
),
1334 LLVMValueRef args
[3] = {
1336 LLVMConstReal(ctx
->f32
, 0),
1337 LLVMConstReal(ctx
->f32
, 1),
1340 return ac_build_intrinsic(ctx
, "llvm.AMDGPU.clamp.", ctx
->f32
, args
, 3,
1341 AC_FUNC_ATTR_READNONE
|
1342 AC_FUNC_ATTR_LEGACY
);
1345 void ac_build_export(struct ac_llvm_context
*ctx
, struct ac_export_args
*a
)
1347 LLVMValueRef args
[9];
1349 if (HAVE_LLVM
>= 0x0500) {
1350 args
[0] = LLVMConstInt(ctx
->i32
, a
->target
, 0);
1351 args
[1] = LLVMConstInt(ctx
->i32
, a
->enabled_channels
, 0);
1354 LLVMTypeRef i16
= LLVMInt16TypeInContext(ctx
->context
);
1355 LLVMTypeRef v2i16
= LLVMVectorType(i16
, 2);
1357 args
[2] = LLVMBuildBitCast(ctx
->builder
, a
->out
[0],
1359 args
[3] = LLVMBuildBitCast(ctx
->builder
, a
->out
[1],
1361 args
[4] = LLVMConstInt(ctx
->i1
, a
->done
, 0);
1362 args
[5] = LLVMConstInt(ctx
->i1
, a
->valid_mask
, 0);
1364 ac_build_intrinsic(ctx
, "llvm.amdgcn.exp.compr.v2i16",
1365 ctx
->voidt
, args
, 6, 0);
1367 args
[2] = a
->out
[0];
1368 args
[3] = a
->out
[1];
1369 args
[4] = a
->out
[2];
1370 args
[5] = a
->out
[3];
1371 args
[6] = LLVMConstInt(ctx
->i1
, a
->done
, 0);
1372 args
[7] = LLVMConstInt(ctx
->i1
, a
->valid_mask
, 0);
1374 ac_build_intrinsic(ctx
, "llvm.amdgcn.exp.f32",
1375 ctx
->voidt
, args
, 8, 0);
1380 args
[0] = LLVMConstInt(ctx
->i32
, a
->enabled_channels
, 0);
1381 args
[1] = LLVMConstInt(ctx
->i32
, a
->valid_mask
, 0);
1382 args
[2] = LLVMConstInt(ctx
->i32
, a
->done
, 0);
1383 args
[3] = LLVMConstInt(ctx
->i32
, a
->target
, 0);
1384 args
[4] = LLVMConstInt(ctx
->i32
, a
->compr
, 0);
1385 memcpy(args
+ 5, a
->out
, sizeof(a
->out
[0]) * 4);
1387 ac_build_intrinsic(ctx
, "llvm.SI.export", ctx
->voidt
, args
, 9,
1388 AC_FUNC_ATTR_LEGACY
);
1391 void ac_build_export_null(struct ac_llvm_context
*ctx
)
1393 struct ac_export_args args
;
1395 args
.enabled_channels
= 0x0; /* enabled channels */
1396 args
.valid_mask
= 1; /* whether the EXEC mask is valid */
1397 args
.done
= 1; /* DONE bit */
1398 args
.target
= V_008DFC_SQ_EXP_NULL
;
1399 args
.compr
= 0; /* COMPR flag (0 = 32-bit export) */
1400 args
.out
[0] = LLVMGetUndef(ctx
->f32
); /* R */
1401 args
.out
[1] = LLVMGetUndef(ctx
->f32
); /* G */
1402 args
.out
[2] = LLVMGetUndef(ctx
->f32
); /* B */
1403 args
.out
[3] = LLVMGetUndef(ctx
->f32
); /* A */
1405 ac_build_export(ctx
, &args
);
1408 LLVMValueRef
ac_build_image_opcode(struct ac_llvm_context
*ctx
,
1409 struct ac_image_args
*a
)
1411 LLVMValueRef args
[11];
1412 unsigned num_args
= 0;
1413 const char *name
= NULL
;
1414 char intr_name
[128], type
[64];
1416 bool sample
= a
->opcode
== ac_image_sample
||
1417 a
->opcode
== ac_image_gather4
||
1418 a
->opcode
== ac_image_get_lod
;
1421 args
[num_args
++] = ac_to_float(ctx
, a
->addr
);
1423 args
[num_args
++] = a
->addr
;
1425 args
[num_args
++] = a
->resource
;
1427 args
[num_args
++] = a
->sampler
;
1428 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->dmask
, 0);
1430 args
[num_args
++] = LLVMConstInt(ctx
->i1
, a
->unorm
, 0);
1431 args
[num_args
++] = ctx
->i1false
; /* glc */
1432 args
[num_args
++] = ctx
->i1false
; /* slc */
1433 args
[num_args
++] = ctx
->i1false
; /* lwe */
1434 args
[num_args
++] = LLVMConstInt(ctx
->i1
, a
->da
, 0);
1436 switch (a
->opcode
) {
1437 case ac_image_sample
:
1438 name
= "llvm.amdgcn.image.sample";
1440 case ac_image_gather4
:
1441 name
= "llvm.amdgcn.image.gather4";
1444 name
= "llvm.amdgcn.image.load";
1446 case ac_image_load_mip
:
1447 name
= "llvm.amdgcn.image.load.mip";
1449 case ac_image_get_lod
:
1450 name
= "llvm.amdgcn.image.getlod";
1452 case ac_image_get_resinfo
:
1453 name
= "llvm.amdgcn.image.getresinfo";
1456 unreachable("invalid image opcode");
1459 ac_build_type_name_for_intr(LLVMTypeOf(args
[0]), type
,
1462 snprintf(intr_name
, sizeof(intr_name
), "%s%s%s%s.v4f32.%s.v8i32",
1464 a
->compare
? ".c" : "",
1468 a
->level_zero
? ".lz" : "",
1469 a
->offset
? ".o" : "",
1472 LLVMValueRef result
=
1473 ac_build_intrinsic(ctx
, intr_name
,
1474 ctx
->v4f32
, args
, num_args
,
1475 AC_FUNC_ATTR_READNONE
);
1477 result
= LLVMBuildBitCast(ctx
->builder
, result
,
1483 LLVMValueRef
ac_build_cvt_pkrtz_f16(struct ac_llvm_context
*ctx
,
1484 LLVMValueRef args
[2])
1486 if (HAVE_LLVM
>= 0x0500) {
1488 LLVMVectorType(LLVMHalfTypeInContext(ctx
->context
), 2);
1490 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pkrtz",
1492 AC_FUNC_ATTR_READNONE
);
1493 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
1496 return ac_build_intrinsic(ctx
, "llvm.SI.packf16", ctx
->i32
, args
, 2,
1497 AC_FUNC_ATTR_READNONE
|
1498 AC_FUNC_ATTR_LEGACY
);
1501 /* Upper 16 bits must be zero. */
1502 static LLVMValueRef
ac_llvm_pack_two_int16(struct ac_llvm_context
*ctx
,
1503 LLVMValueRef val
[2])
1505 return LLVMBuildOr(ctx
->builder
, val
[0],
1506 LLVMBuildShl(ctx
->builder
, val
[1],
1507 LLVMConstInt(ctx
->i32
, 16, 0),
1511 /* Upper 16 bits are ignored and will be dropped. */
1512 static LLVMValueRef
ac_llvm_pack_two_int32_as_int16(struct ac_llvm_context
*ctx
,
1513 LLVMValueRef val
[2])
1515 LLVMValueRef v
[2] = {
1516 LLVMBuildAnd(ctx
->builder
, val
[0],
1517 LLVMConstInt(ctx
->i32
, 0xffff, 0), ""),
1520 return ac_llvm_pack_two_int16(ctx
, v
);
1523 LLVMValueRef
ac_build_cvt_pknorm_i16(struct ac_llvm_context
*ctx
,
1524 LLVMValueRef args
[2])
1526 if (HAVE_LLVM
>= 0x0600) {
1528 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pknorm.i16",
1529 ctx
->v2i16
, args
, 2,
1530 AC_FUNC_ATTR_READNONE
);
1531 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
1534 LLVMValueRef val
[2];
1536 for (int chan
= 0; chan
< 2; chan
++) {
1537 /* Clamp between [-1, 1]. */
1538 val
[chan
] = ac_build_fmin(ctx
, args
[chan
], ctx
->f32_1
);
1539 val
[chan
] = ac_build_fmax(ctx
, val
[chan
], LLVMConstReal(ctx
->f32
, -1));
1540 /* Convert to a signed integer in [-32767, 32767]. */
1541 val
[chan
] = LLVMBuildFMul(ctx
->builder
, val
[chan
],
1542 LLVMConstReal(ctx
->f32
, 32767), "");
1543 /* If positive, add 0.5, else add -0.5. */
1544 val
[chan
] = LLVMBuildFAdd(ctx
->builder
, val
[chan
],
1545 LLVMBuildSelect(ctx
->builder
,
1546 LLVMBuildFCmp(ctx
->builder
, LLVMRealOGE
,
1547 val
[chan
], ctx
->f32_0
, ""),
1548 LLVMConstReal(ctx
->f32
, 0.5),
1549 LLVMConstReal(ctx
->f32
, -0.5), ""), "");
1550 val
[chan
] = LLVMBuildFPToSI(ctx
->builder
, val
[chan
], ctx
->i32
, "");
1552 return ac_llvm_pack_two_int32_as_int16(ctx
, val
);
1555 LLVMValueRef
ac_build_cvt_pknorm_u16(struct ac_llvm_context
*ctx
,
1556 LLVMValueRef args
[2])
1558 if (HAVE_LLVM
>= 0x0600) {
1560 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pknorm.u16",
1561 ctx
->v2i16
, args
, 2,
1562 AC_FUNC_ATTR_READNONE
);
1563 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
1566 LLVMValueRef val
[2];
1568 for (int chan
= 0; chan
< 2; chan
++) {
1569 val
[chan
] = ac_build_clamp(ctx
, args
[chan
]);
1570 val
[chan
] = LLVMBuildFMul(ctx
->builder
, val
[chan
],
1571 LLVMConstReal(ctx
->f32
, 65535), "");
1572 val
[chan
] = LLVMBuildFAdd(ctx
->builder
, val
[chan
],
1573 LLVMConstReal(ctx
->f32
, 0.5), "");
1574 val
[chan
] = LLVMBuildFPToUI(ctx
->builder
, val
[chan
],
1577 return ac_llvm_pack_two_int32_as_int16(ctx
, val
);
1580 /* The 8-bit and 10-bit clamping is for HW workarounds. */
1581 LLVMValueRef
ac_build_cvt_pk_i16(struct ac_llvm_context
*ctx
,
1582 LLVMValueRef args
[2], unsigned bits
, bool hi
)
1584 assert(bits
== 8 || bits
== 10 || bits
== 16);
1586 LLVMValueRef max_rgb
= LLVMConstInt(ctx
->i32
,
1587 bits
== 8 ? 127 : bits
== 10 ? 511 : 32767, 0);
1588 LLVMValueRef min_rgb
= LLVMConstInt(ctx
->i32
,
1589 bits
== 8 ? -128 : bits
== 10 ? -512 : -32768, 0);
1590 LLVMValueRef max_alpha
=
1591 bits
!= 10 ? max_rgb
: ctx
->i32_1
;
1592 LLVMValueRef min_alpha
=
1593 bits
!= 10 ? min_rgb
: LLVMConstInt(ctx
->i32
, -2, 0);
1594 bool has_intrinsic
= HAVE_LLVM
>= 0x0600;
1597 if (!has_intrinsic
|| bits
!= 16) {
1598 for (int i
= 0; i
< 2; i
++) {
1599 bool alpha
= hi
&& i
== 1;
1600 args
[i
] = ac_build_imin(ctx
, args
[i
],
1601 alpha
? max_alpha
: max_rgb
);
1602 args
[i
] = ac_build_imax(ctx
, args
[i
],
1603 alpha
? min_alpha
: min_rgb
);
1607 if (has_intrinsic
) {
1609 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pk.i16",
1610 ctx
->v2i16
, args
, 2,
1611 AC_FUNC_ATTR_READNONE
);
1612 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
1615 return ac_llvm_pack_two_int32_as_int16(ctx
, args
);
1618 /* The 8-bit and 10-bit clamping is for HW workarounds. */
1619 LLVMValueRef
ac_build_cvt_pk_u16(struct ac_llvm_context
*ctx
,
1620 LLVMValueRef args
[2], unsigned bits
, bool hi
)
1622 assert(bits
== 8 || bits
== 10 || bits
== 16);
1624 LLVMValueRef max_rgb
= LLVMConstInt(ctx
->i32
,
1625 bits
== 8 ? 255 : bits
== 10 ? 1023 : 65535, 0);
1626 LLVMValueRef max_alpha
=
1627 bits
!= 10 ? max_rgb
: LLVMConstInt(ctx
->i32
, 3, 0);
1628 bool has_intrinsic
= HAVE_LLVM
>= 0x0600;
1631 if (!has_intrinsic
|| bits
!= 16) {
1632 for (int i
= 0; i
< 2; i
++) {
1633 bool alpha
= hi
&& i
== 1;
1634 args
[i
] = ac_build_umin(ctx
, args
[i
],
1635 alpha
? max_alpha
: max_rgb
);
1639 if (has_intrinsic
) {
1641 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pk.u16",
1642 ctx
->v2i16
, args
, 2,
1643 AC_FUNC_ATTR_READNONE
);
1644 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
1647 return ac_llvm_pack_two_int16(ctx
, args
);
1650 LLVMValueRef
ac_build_wqm_vote(struct ac_llvm_context
*ctx
, LLVMValueRef i1
)
1652 assert(HAVE_LLVM
>= 0x0600);
1653 return ac_build_intrinsic(ctx
, "llvm.amdgcn.wqm.vote", ctx
->i1
,
1654 &i1
, 1, AC_FUNC_ATTR_READNONE
);
1657 void ac_build_kill_if_false(struct ac_llvm_context
*ctx
, LLVMValueRef i1
)
1659 if (HAVE_LLVM
>= 0x0600) {
1660 ac_build_intrinsic(ctx
, "llvm.amdgcn.kill", ctx
->voidt
,
1665 LLVMValueRef value
= LLVMBuildSelect(ctx
->builder
, i1
,
1666 LLVMConstReal(ctx
->f32
, 1),
1667 LLVMConstReal(ctx
->f32
, -1), "");
1668 ac_build_intrinsic(ctx
, "llvm.AMDGPU.kill", ctx
->voidt
,
1669 &value
, 1, AC_FUNC_ATTR_LEGACY
);
1672 LLVMValueRef
ac_build_bfe(struct ac_llvm_context
*ctx
, LLVMValueRef input
,
1673 LLVMValueRef offset
, LLVMValueRef width
,
1676 LLVMValueRef args
[] = {
1682 if (HAVE_LLVM
>= 0x0500) {
1683 return ac_build_intrinsic(ctx
,
1684 is_signed
? "llvm.amdgcn.sbfe.i32" :
1685 "llvm.amdgcn.ubfe.i32",
1687 AC_FUNC_ATTR_READNONE
);
1690 return ac_build_intrinsic(ctx
,
1691 is_signed
? "llvm.AMDGPU.bfe.i32" :
1692 "llvm.AMDGPU.bfe.u32",
1694 AC_FUNC_ATTR_READNONE
|
1695 AC_FUNC_ATTR_LEGACY
);
1698 void ac_build_waitcnt(struct ac_llvm_context
*ctx
, unsigned simm16
)
1700 LLVMValueRef args
[1] = {
1701 LLVMConstInt(ctx
->i32
, simm16
, false),
1703 ac_build_intrinsic(ctx
, "llvm.amdgcn.s.waitcnt",
1704 ctx
->voidt
, args
, 1, 0);
1707 LLVMValueRef
ac_build_fract(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
1713 if (bitsize
== 32) {
1714 intr
= "llvm.floor.f32";
1717 intr
= "llvm.floor.f64";
1721 LLVMValueRef params
[] = {
1724 LLVMValueRef floor
= ac_build_intrinsic(ctx
, intr
, type
, params
, 1,
1725 AC_FUNC_ATTR_READNONE
);
1726 return LLVMBuildFSub(ctx
->builder
, src0
, floor
, "");
1729 LLVMValueRef
ac_build_isign(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
1732 LLVMValueRef cmp
, val
, zero
, one
;
1735 if (bitsize
== 32) {
1745 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGT
, src0
, zero
, "");
1746 val
= LLVMBuildSelect(ctx
->builder
, cmp
, one
, src0
, "");
1747 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGE
, val
, zero
, "");
1748 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstInt(type
, -1, true), "");
1752 LLVMValueRef
ac_build_fsign(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
1755 LLVMValueRef cmp
, val
, zero
, one
;
1758 if (bitsize
== 32) {
1768 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGT
, src0
, zero
, "");
1769 val
= LLVMBuildSelect(ctx
->builder
, cmp
, one
, src0
, "");
1770 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGE
, val
, zero
, "");
1771 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstReal(type
, -1.0), "");
1775 void ac_get_image_intr_name(const char *base_name
,
1776 LLVMTypeRef data_type
,
1777 LLVMTypeRef coords_type
,
1778 LLVMTypeRef rsrc_type
,
1779 char *out_name
, unsigned out_len
)
1781 char coords_type_name
[8];
1783 ac_build_type_name_for_intr(coords_type
, coords_type_name
,
1784 sizeof(coords_type_name
));
1786 char data_type_name
[8];
1787 char rsrc_type_name
[8];
1789 ac_build_type_name_for_intr(data_type
, data_type_name
,
1790 sizeof(data_type_name
));
1791 ac_build_type_name_for_intr(rsrc_type
, rsrc_type_name
,
1792 sizeof(rsrc_type_name
));
1793 snprintf(out_name
, out_len
, "%s.%s.%s.%s", base_name
,
1794 data_type_name
, coords_type_name
, rsrc_type_name
);
1797 #define AC_EXP_TARGET (HAVE_LLVM >= 0x0500 ? 0 : 3)
1798 #define AC_EXP_ENABLED_CHANNELS (HAVE_LLVM >= 0x0500 ? 1 : 0)
1799 #define AC_EXP_OUT0 (HAVE_LLVM >= 0x0500 ? 2 : 5)
1807 struct ac_vs_exp_chan
1811 enum ac_ir_type type
;
1814 struct ac_vs_exp_inst
{
1817 struct ac_vs_exp_chan chan
[4];
1820 struct ac_vs_exports
{
1822 struct ac_vs_exp_inst exp
[VARYING_SLOT_MAX
];
1825 /* Return true if the PARAM export has been eliminated. */
1826 static bool ac_eliminate_const_output(uint8_t *vs_output_param_offset
,
1827 uint32_t num_outputs
,
1828 struct ac_vs_exp_inst
*exp
)
1830 unsigned i
, default_val
; /* SPI_PS_INPUT_CNTL_i.DEFAULT_VAL */
1831 bool is_zero
[4] = {}, is_one
[4] = {};
1833 for (i
= 0; i
< 4; i
++) {
1834 /* It's a constant expression. Undef outputs are eliminated too. */
1835 if (exp
->chan
[i
].type
== AC_IR_UNDEF
) {
1838 } else if (exp
->chan
[i
].type
== AC_IR_CONST
) {
1839 if (exp
->chan
[i
].const_float
== 0)
1841 else if (exp
->chan
[i
].const_float
== 1)
1844 return false; /* other constant */
1849 /* Only certain combinations of 0 and 1 can be eliminated. */
1850 if (is_zero
[0] && is_zero
[1] && is_zero
[2])
1851 default_val
= is_zero
[3] ? 0 : 1;
1852 else if (is_one
[0] && is_one
[1] && is_one
[2])
1853 default_val
= is_zero
[3] ? 2 : 3;
1857 /* The PARAM export can be represented as DEFAULT_VAL. Kill it. */
1858 LLVMInstructionEraseFromParent(exp
->inst
);
1860 /* Change OFFSET to DEFAULT_VAL. */
1861 for (i
= 0; i
< num_outputs
; i
++) {
1862 if (vs_output_param_offset
[i
] == exp
->offset
) {
1863 vs_output_param_offset
[i
] =
1864 AC_EXP_PARAM_DEFAULT_VAL_0000
+ default_val
;
1871 static bool ac_eliminate_duplicated_output(struct ac_llvm_context
*ctx
,
1872 uint8_t *vs_output_param_offset
,
1873 uint32_t num_outputs
,
1874 struct ac_vs_exports
*processed
,
1875 struct ac_vs_exp_inst
*exp
)
1877 unsigned p
, copy_back_channels
= 0;
1879 /* See if the output is already in the list of processed outputs.
1880 * The LLVMValueRef comparison relies on SSA.
1882 for (p
= 0; p
< processed
->num
; p
++) {
1883 bool different
= false;
1885 for (unsigned j
= 0; j
< 4; j
++) {
1886 struct ac_vs_exp_chan
*c1
= &processed
->exp
[p
].chan
[j
];
1887 struct ac_vs_exp_chan
*c2
= &exp
->chan
[j
];
1889 /* Treat undef as a match. */
1890 if (c2
->type
== AC_IR_UNDEF
)
1893 /* If c1 is undef but c2 isn't, we can copy c2 to c1
1894 * and consider the instruction duplicated.
1896 if (c1
->type
== AC_IR_UNDEF
) {
1897 copy_back_channels
|= 1 << j
;
1901 /* Test whether the channels are not equal. */
1902 if (c1
->type
!= c2
->type
||
1903 (c1
->type
== AC_IR_CONST
&&
1904 c1
->const_float
!= c2
->const_float
) ||
1905 (c1
->type
== AC_IR_VALUE
&&
1906 c1
->value
!= c2
->value
)) {
1914 copy_back_channels
= 0;
1916 if (p
== processed
->num
)
1919 /* If a match was found, but the matching export has undef where the new
1920 * one has a normal value, copy the normal value to the undef channel.
1922 struct ac_vs_exp_inst
*match
= &processed
->exp
[p
];
1924 /* Get current enabled channels mask. */
1925 LLVMValueRef arg
= LLVMGetOperand(match
->inst
, AC_EXP_ENABLED_CHANNELS
);
1926 unsigned enabled_channels
= LLVMConstIntGetZExtValue(arg
);
1928 while (copy_back_channels
) {
1929 unsigned chan
= u_bit_scan(©_back_channels
);
1931 assert(match
->chan
[chan
].type
== AC_IR_UNDEF
);
1932 LLVMSetOperand(match
->inst
, AC_EXP_OUT0
+ chan
,
1933 exp
->chan
[chan
].value
);
1934 match
->chan
[chan
] = exp
->chan
[chan
];
1936 /* Update number of enabled channels because the original mask
1937 * is not always 0xf.
1939 enabled_channels
|= (1 << chan
);
1940 LLVMSetOperand(match
->inst
, AC_EXP_ENABLED_CHANNELS
,
1941 LLVMConstInt(ctx
->i32
, enabled_channels
, 0));
1944 /* The PARAM export is duplicated. Kill it. */
1945 LLVMInstructionEraseFromParent(exp
->inst
);
1947 /* Change OFFSET to the matching export. */
1948 for (unsigned i
= 0; i
< num_outputs
; i
++) {
1949 if (vs_output_param_offset
[i
] == exp
->offset
) {
1950 vs_output_param_offset
[i
] = match
->offset
;
1957 void ac_optimize_vs_outputs(struct ac_llvm_context
*ctx
,
1958 LLVMValueRef main_fn
,
1959 uint8_t *vs_output_param_offset
,
1960 uint32_t num_outputs
,
1961 uint8_t *num_param_exports
)
1963 LLVMBasicBlockRef bb
;
1964 bool removed_any
= false;
1965 struct ac_vs_exports exports
;
1969 /* Process all LLVM instructions. */
1970 bb
= LLVMGetFirstBasicBlock(main_fn
);
1972 LLVMValueRef inst
= LLVMGetFirstInstruction(bb
);
1975 LLVMValueRef cur
= inst
;
1976 inst
= LLVMGetNextInstruction(inst
);
1977 struct ac_vs_exp_inst exp
;
1979 if (LLVMGetInstructionOpcode(cur
) != LLVMCall
)
1982 LLVMValueRef callee
= ac_llvm_get_called_value(cur
);
1984 if (!ac_llvm_is_function(callee
))
1987 const char *name
= LLVMGetValueName(callee
);
1988 unsigned num_args
= LLVMCountParams(callee
);
1990 /* Check if this is an export instruction. */
1991 if ((num_args
!= 9 && num_args
!= 8) ||
1992 (strcmp(name
, "llvm.SI.export") &&
1993 strcmp(name
, "llvm.amdgcn.exp.f32")))
1996 LLVMValueRef arg
= LLVMGetOperand(cur
, AC_EXP_TARGET
);
1997 unsigned target
= LLVMConstIntGetZExtValue(arg
);
1999 if (target
< V_008DFC_SQ_EXP_PARAM
)
2002 target
-= V_008DFC_SQ_EXP_PARAM
;
2004 /* Parse the instruction. */
2005 memset(&exp
, 0, sizeof(exp
));
2006 exp
.offset
= target
;
2009 for (unsigned i
= 0; i
< 4; i
++) {
2010 LLVMValueRef v
= LLVMGetOperand(cur
, AC_EXP_OUT0
+ i
);
2012 exp
.chan
[i
].value
= v
;
2014 if (LLVMIsUndef(v
)) {
2015 exp
.chan
[i
].type
= AC_IR_UNDEF
;
2016 } else if (LLVMIsAConstantFP(v
)) {
2017 LLVMBool loses_info
;
2018 exp
.chan
[i
].type
= AC_IR_CONST
;
2019 exp
.chan
[i
].const_float
=
2020 LLVMConstRealGetDouble(v
, &loses_info
);
2022 exp
.chan
[i
].type
= AC_IR_VALUE
;
2026 /* Eliminate constant and duplicated PARAM exports. */
2027 if (ac_eliminate_const_output(vs_output_param_offset
,
2028 num_outputs
, &exp
) ||
2029 ac_eliminate_duplicated_output(ctx
,
2030 vs_output_param_offset
,
2031 num_outputs
, &exports
,
2035 exports
.exp
[exports
.num
++] = exp
;
2038 bb
= LLVMGetNextBasicBlock(bb
);
2041 /* Remove holes in export memory due to removed PARAM exports.
2042 * This is done by renumbering all PARAM exports.
2045 uint8_t old_offset
[VARYING_SLOT_MAX
];
2048 /* Make a copy of the offsets. We need the old version while
2049 * we are modifying some of them. */
2050 memcpy(old_offset
, vs_output_param_offset
,
2051 sizeof(old_offset
));
2053 for (i
= 0; i
< exports
.num
; i
++) {
2054 unsigned offset
= exports
.exp
[i
].offset
;
2056 /* Update vs_output_param_offset. Multiple outputs can
2057 * have the same offset.
2059 for (out
= 0; out
< num_outputs
; out
++) {
2060 if (old_offset
[out
] == offset
)
2061 vs_output_param_offset
[out
] = i
;
2064 /* Change the PARAM offset in the instruction. */
2065 LLVMSetOperand(exports
.exp
[i
].inst
, AC_EXP_TARGET
,
2066 LLVMConstInt(ctx
->i32
,
2067 V_008DFC_SQ_EXP_PARAM
+ i
, 0));
2069 *num_param_exports
= exports
.num
;
2073 void ac_init_exec_full_mask(struct ac_llvm_context
*ctx
)
2075 LLVMValueRef full_mask
= LLVMConstInt(ctx
->i64
, ~0ull, 0);
2076 ac_build_intrinsic(ctx
,
2077 "llvm.amdgcn.init.exec", ctx
->voidt
,
2078 &full_mask
, 1, AC_FUNC_ATTR_CONVERGENT
);
2081 void ac_declare_lds_as_pointer(struct ac_llvm_context
*ctx
)
2083 unsigned lds_size
= ctx
->chip_class
>= CIK
? 65536 : 32768;
2084 ctx
->lds
= LLVMBuildIntToPtr(ctx
->builder
, ctx
->i32_0
,
2085 LLVMPointerType(LLVMArrayType(ctx
->i32
, lds_size
/ 4), AC_LOCAL_ADDR_SPACE
),
2089 LLVMValueRef
ac_lds_load(struct ac_llvm_context
*ctx
,
2090 LLVMValueRef dw_addr
)
2092 return ac_build_load(ctx
, ctx
->lds
, dw_addr
);
2095 void ac_lds_store(struct ac_llvm_context
*ctx
,
2096 LLVMValueRef dw_addr
,
2099 value
= ac_to_integer(ctx
, value
);
2100 ac_build_indexed_store(ctx
, ctx
->lds
,
2104 LLVMValueRef
ac_find_lsb(struct ac_llvm_context
*ctx
,
2105 LLVMTypeRef dst_type
,
2108 unsigned src0_bitsize
= ac_get_elem_bits(ctx
, LLVMTypeOf(src0
));
2109 const char *intrin_name
;
2112 if (src0_bitsize
== 64) {
2113 intrin_name
= "llvm.cttz.i64";
2117 intrin_name
= "llvm.cttz.i32";
2122 LLVMValueRef params
[2] = {
2125 /* The value of 1 means that ffs(x=0) = undef, so LLVM won't
2126 * add special code to check for x=0. The reason is that
2127 * the LLVM behavior for x=0 is different from what we
2128 * need here. However, LLVM also assumes that ffs(x) is
2129 * in [0, 31], but GLSL expects that ffs(0) = -1, so
2130 * a conditional assignment to handle 0 is still required.
2132 * The hardware already implements the correct behavior.
2134 LLVMConstInt(ctx
->i1
, 1, false),
2137 LLVMValueRef lsb
= ac_build_intrinsic(ctx
, intrin_name
, type
,
2139 AC_FUNC_ATTR_READNONE
);
2141 if (src0_bitsize
== 64) {
2142 lsb
= LLVMBuildTrunc(ctx
->builder
, lsb
, ctx
->i32
, "");
2145 /* TODO: We need an intrinsic to skip this conditional. */
2146 /* Check for zero: */
2147 return LLVMBuildSelect(ctx
->builder
, LLVMBuildICmp(ctx
->builder
,
2150 LLVMConstInt(ctx
->i32
, -1, 0), lsb
, "");
2153 LLVMTypeRef
ac_array_in_const_addr_space(LLVMTypeRef elem_type
)
2155 return LLVMPointerType(LLVMArrayType(elem_type
, 0),
2156 AC_CONST_ADDR_SPACE
);
2159 LLVMTypeRef
ac_array_in_const32_addr_space(LLVMTypeRef elem_type
)
2161 if (!HAVE_32BIT_POINTERS
)
2162 return ac_array_in_const_addr_space(elem_type
);
2164 return LLVMPointerType(LLVMArrayType(elem_type
, 0),
2165 AC_CONST_32BIT_ADDR_SPACE
);
2168 static struct ac_llvm_flow
*
2169 get_current_flow(struct ac_llvm_context
*ctx
)
2171 if (ctx
->flow_depth
> 0)
2172 return &ctx
->flow
[ctx
->flow_depth
- 1];
2176 static struct ac_llvm_flow
*
2177 get_innermost_loop(struct ac_llvm_context
*ctx
)
2179 for (unsigned i
= ctx
->flow_depth
; i
> 0; --i
) {
2180 if (ctx
->flow
[i
- 1].loop_entry_block
)
2181 return &ctx
->flow
[i
- 1];
2186 static struct ac_llvm_flow
*
2187 push_flow(struct ac_llvm_context
*ctx
)
2189 struct ac_llvm_flow
*flow
;
2191 if (ctx
->flow_depth
>= ctx
->flow_depth_max
) {
2192 unsigned new_max
= MAX2(ctx
->flow_depth
<< 1,
2193 AC_LLVM_INITIAL_CF_DEPTH
);
2195 ctx
->flow
= realloc(ctx
->flow
, new_max
* sizeof(*ctx
->flow
));
2196 ctx
->flow_depth_max
= new_max
;
2199 flow
= &ctx
->flow
[ctx
->flow_depth
];
2202 flow
->next_block
= NULL
;
2203 flow
->loop_entry_block
= NULL
;
2207 static void set_basicblock_name(LLVMBasicBlockRef bb
, const char *base
,
2211 snprintf(buf
, sizeof(buf
), "%s%d", base
, label_id
);
2212 LLVMSetValueName(LLVMBasicBlockAsValue(bb
), buf
);
2215 /* Append a basic block at the level of the parent flow.
2217 static LLVMBasicBlockRef
append_basic_block(struct ac_llvm_context
*ctx
,
2220 assert(ctx
->flow_depth
>= 1);
2222 if (ctx
->flow_depth
>= 2) {
2223 struct ac_llvm_flow
*flow
= &ctx
->flow
[ctx
->flow_depth
- 2];
2225 return LLVMInsertBasicBlockInContext(ctx
->context
,
2226 flow
->next_block
, name
);
2229 LLVMValueRef main_fn
=
2230 LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
->builder
));
2231 return LLVMAppendBasicBlockInContext(ctx
->context
, main_fn
, name
);
2234 /* Emit a branch to the given default target for the current block if
2235 * applicable -- that is, if the current block does not already contain a
2236 * branch from a break or continue.
2238 static void emit_default_branch(LLVMBuilderRef builder
,
2239 LLVMBasicBlockRef target
)
2241 if (!LLVMGetBasicBlockTerminator(LLVMGetInsertBlock(builder
)))
2242 LLVMBuildBr(builder
, target
);
2245 void ac_build_bgnloop(struct ac_llvm_context
*ctx
, int label_id
)
2247 struct ac_llvm_flow
*flow
= push_flow(ctx
);
2248 flow
->loop_entry_block
= append_basic_block(ctx
, "LOOP");
2249 flow
->next_block
= append_basic_block(ctx
, "ENDLOOP");
2250 set_basicblock_name(flow
->loop_entry_block
, "loop", label_id
);
2251 LLVMBuildBr(ctx
->builder
, flow
->loop_entry_block
);
2252 LLVMPositionBuilderAtEnd(ctx
->builder
, flow
->loop_entry_block
);
2255 void ac_build_break(struct ac_llvm_context
*ctx
)
2257 struct ac_llvm_flow
*flow
= get_innermost_loop(ctx
);
2258 LLVMBuildBr(ctx
->builder
, flow
->next_block
);
2261 void ac_build_continue(struct ac_llvm_context
*ctx
)
2263 struct ac_llvm_flow
*flow
= get_innermost_loop(ctx
);
2264 LLVMBuildBr(ctx
->builder
, flow
->loop_entry_block
);
2267 void ac_build_else(struct ac_llvm_context
*ctx
, int label_id
)
2269 struct ac_llvm_flow
*current_branch
= get_current_flow(ctx
);
2270 LLVMBasicBlockRef endif_block
;
2272 assert(!current_branch
->loop_entry_block
);
2274 endif_block
= append_basic_block(ctx
, "ENDIF");
2275 emit_default_branch(ctx
->builder
, endif_block
);
2277 LLVMPositionBuilderAtEnd(ctx
->builder
, current_branch
->next_block
);
2278 set_basicblock_name(current_branch
->next_block
, "else", label_id
);
2280 current_branch
->next_block
= endif_block
;
2283 void ac_build_endif(struct ac_llvm_context
*ctx
, int label_id
)
2285 struct ac_llvm_flow
*current_branch
= get_current_flow(ctx
);
2287 assert(!current_branch
->loop_entry_block
);
2289 emit_default_branch(ctx
->builder
, current_branch
->next_block
);
2290 LLVMPositionBuilderAtEnd(ctx
->builder
, current_branch
->next_block
);
2291 set_basicblock_name(current_branch
->next_block
, "endif", label_id
);
2296 void ac_build_endloop(struct ac_llvm_context
*ctx
, int label_id
)
2298 struct ac_llvm_flow
*current_loop
= get_current_flow(ctx
);
2300 assert(current_loop
->loop_entry_block
);
2302 emit_default_branch(ctx
->builder
, current_loop
->loop_entry_block
);
2304 LLVMPositionBuilderAtEnd(ctx
->builder
, current_loop
->next_block
);
2305 set_basicblock_name(current_loop
->next_block
, "endloop", label_id
);
2309 static void if_cond_emit(struct ac_llvm_context
*ctx
, LLVMValueRef cond
,
2312 struct ac_llvm_flow
*flow
= push_flow(ctx
);
2313 LLVMBasicBlockRef if_block
;
2315 if_block
= append_basic_block(ctx
, "IF");
2316 flow
->next_block
= append_basic_block(ctx
, "ELSE");
2317 set_basicblock_name(if_block
, "if", label_id
);
2318 LLVMBuildCondBr(ctx
->builder
, cond
, if_block
, flow
->next_block
);
2319 LLVMPositionBuilderAtEnd(ctx
->builder
, if_block
);
2322 void ac_build_if(struct ac_llvm_context
*ctx
, LLVMValueRef value
,
2325 LLVMValueRef cond
= LLVMBuildFCmp(ctx
->builder
, LLVMRealUNE
,
2326 value
, ctx
->f32_0
, "");
2327 if_cond_emit(ctx
, cond
, label_id
);
2330 void ac_build_uif(struct ac_llvm_context
*ctx
, LLVMValueRef value
,
2333 LLVMValueRef cond
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
2334 ac_to_integer(ctx
, value
),
2336 if_cond_emit(ctx
, cond
, label_id
);