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"
40 #include "util/u_math.h"
43 #include "shader_enums.h"
45 #define AC_LLVM_INITIAL_CF_DEPTH 4
47 /* Data for if/else/endif and bgnloop/endloop control flow structures.
50 /* Loop exit or next part of if/else/endif. */
51 LLVMBasicBlockRef next_block
;
52 LLVMBasicBlockRef loop_entry_block
;
55 /* Initialize module-independent parts of the context.
57 * The caller is responsible for initializing ctx::module and ctx::builder.
60 ac_llvm_context_init(struct ac_llvm_context
*ctx
,
61 enum chip_class chip_class
, enum radeon_family family
)
65 ctx
->context
= LLVMContextCreate();
67 ctx
->chip_class
= chip_class
;
72 ctx
->voidt
= LLVMVoidTypeInContext(ctx
->context
);
73 ctx
->i1
= LLVMInt1TypeInContext(ctx
->context
);
74 ctx
->i8
= LLVMInt8TypeInContext(ctx
->context
);
75 ctx
->i16
= LLVMIntTypeInContext(ctx
->context
, 16);
76 ctx
->i32
= LLVMIntTypeInContext(ctx
->context
, 32);
77 ctx
->i64
= LLVMIntTypeInContext(ctx
->context
, 64);
78 ctx
->intptr
= ctx
->i32
;
79 ctx
->f16
= LLVMHalfTypeInContext(ctx
->context
);
80 ctx
->f32
= LLVMFloatTypeInContext(ctx
->context
);
81 ctx
->f64
= LLVMDoubleTypeInContext(ctx
->context
);
82 ctx
->v2i16
= LLVMVectorType(ctx
->i16
, 2);
83 ctx
->v2i32
= LLVMVectorType(ctx
->i32
, 2);
84 ctx
->v3i32
= LLVMVectorType(ctx
->i32
, 3);
85 ctx
->v4i32
= LLVMVectorType(ctx
->i32
, 4);
86 ctx
->v2f32
= LLVMVectorType(ctx
->f32
, 2);
87 ctx
->v3f32
= LLVMVectorType(ctx
->f32
, 3);
88 ctx
->v4f32
= LLVMVectorType(ctx
->f32
, 4);
89 ctx
->v8i32
= LLVMVectorType(ctx
->i32
, 8);
91 ctx
->i8_0
= LLVMConstInt(ctx
->i8
, 0, false);
92 ctx
->i8_1
= LLVMConstInt(ctx
->i8
, 1, false);
93 ctx
->i16_0
= LLVMConstInt(ctx
->i16
, 0, false);
94 ctx
->i16_1
= LLVMConstInt(ctx
->i16
, 1, false);
95 ctx
->i32_0
= LLVMConstInt(ctx
->i32
, 0, false);
96 ctx
->i32_1
= LLVMConstInt(ctx
->i32
, 1, false);
97 ctx
->i64_0
= LLVMConstInt(ctx
->i64
, 0, false);
98 ctx
->i64_1
= LLVMConstInt(ctx
->i64
, 1, false);
99 ctx
->f16_0
= LLVMConstReal(ctx
->f16
, 0.0);
100 ctx
->f16_1
= LLVMConstReal(ctx
->f16
, 1.0);
101 ctx
->f32_0
= LLVMConstReal(ctx
->f32
, 0.0);
102 ctx
->f32_1
= LLVMConstReal(ctx
->f32
, 1.0);
103 ctx
->f64_0
= LLVMConstReal(ctx
->f64
, 0.0);
104 ctx
->f64_1
= LLVMConstReal(ctx
->f64
, 1.0);
106 ctx
->i1false
= LLVMConstInt(ctx
->i1
, 0, false);
107 ctx
->i1true
= LLVMConstInt(ctx
->i1
, 1, false);
109 ctx
->range_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
112 ctx
->invariant_load_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
113 "invariant.load", 14);
115 ctx
->fpmath_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
, "fpmath", 6);
117 args
[0] = LLVMConstReal(ctx
->f32
, 2.5);
118 ctx
->fpmath_md_2p5_ulp
= LLVMMDNodeInContext(ctx
->context
, args
, 1);
120 ctx
->uniform_md_kind
= LLVMGetMDKindIDInContext(ctx
->context
,
121 "amdgpu.uniform", 14);
123 ctx
->empty_md
= LLVMMDNodeInContext(ctx
->context
, NULL
, 0);
127 ac_llvm_context_dispose(struct ac_llvm_context
*ctx
)
131 ctx
->flow_depth_max
= 0;
135 ac_get_llvm_num_components(LLVMValueRef value
)
137 LLVMTypeRef type
= LLVMTypeOf(value
);
138 unsigned num_components
= LLVMGetTypeKind(type
) == LLVMVectorTypeKind
139 ? LLVMGetVectorSize(type
)
141 return num_components
;
145 ac_llvm_extract_elem(struct ac_llvm_context
*ac
,
149 if (LLVMGetTypeKind(LLVMTypeOf(value
)) != LLVMVectorTypeKind
) {
154 return LLVMBuildExtractElement(ac
->builder
, value
,
155 LLVMConstInt(ac
->i32
, index
, false), "");
159 ac_get_elem_bits(struct ac_llvm_context
*ctx
, LLVMTypeRef type
)
161 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
)
162 type
= LLVMGetElementType(type
);
164 if (LLVMGetTypeKind(type
) == LLVMIntegerTypeKind
)
165 return LLVMGetIntTypeWidth(type
);
167 if (type
== ctx
->f16
)
169 if (type
== ctx
->f32
)
171 if (type
== ctx
->f64
)
174 unreachable("Unhandled type kind in get_elem_bits");
178 ac_get_type_size(LLVMTypeRef type
)
180 LLVMTypeKind kind
= LLVMGetTypeKind(type
);
183 case LLVMIntegerTypeKind
:
184 return LLVMGetIntTypeWidth(type
) / 8;
185 case LLVMHalfTypeKind
:
187 case LLVMFloatTypeKind
:
189 case LLVMDoubleTypeKind
:
191 case LLVMPointerTypeKind
:
192 if (LLVMGetPointerAddressSpace(type
) == AC_ADDR_SPACE_CONST_32BIT
)
195 case LLVMVectorTypeKind
:
196 return LLVMGetVectorSize(type
) *
197 ac_get_type_size(LLVMGetElementType(type
));
198 case LLVMArrayTypeKind
:
199 return LLVMGetArrayLength(type
) *
200 ac_get_type_size(LLVMGetElementType(type
));
207 static LLVMTypeRef
to_integer_type_scalar(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
211 else if (t
== ctx
->f16
|| t
== ctx
->i16
)
213 else if (t
== ctx
->f32
|| t
== ctx
->i32
)
215 else if (t
== ctx
->f64
|| t
== ctx
->i64
)
218 unreachable("Unhandled integer size");
222 ac_to_integer_type(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
224 if (LLVMGetTypeKind(t
) == LLVMVectorTypeKind
) {
225 LLVMTypeRef elem_type
= LLVMGetElementType(t
);
226 return LLVMVectorType(to_integer_type_scalar(ctx
, elem_type
),
227 LLVMGetVectorSize(t
));
229 if (LLVMGetTypeKind(t
) == LLVMPointerTypeKind
) {
230 switch (LLVMGetPointerAddressSpace(t
)) {
231 case AC_ADDR_SPACE_GLOBAL
:
233 case AC_ADDR_SPACE_LDS
:
236 unreachable("unhandled address space");
239 return to_integer_type_scalar(ctx
, t
);
243 ac_to_integer(struct ac_llvm_context
*ctx
, LLVMValueRef v
)
245 LLVMTypeRef type
= LLVMTypeOf(v
);
246 if (LLVMGetTypeKind(type
) == LLVMPointerTypeKind
) {
247 return LLVMBuildPtrToInt(ctx
->builder
, v
, ac_to_integer_type(ctx
, type
), "");
249 return LLVMBuildBitCast(ctx
->builder
, v
, ac_to_integer_type(ctx
, type
), "");
253 ac_to_integer_or_pointer(struct ac_llvm_context
*ctx
, LLVMValueRef v
)
255 LLVMTypeRef type
= LLVMTypeOf(v
);
256 if (LLVMGetTypeKind(type
) == LLVMPointerTypeKind
)
258 return ac_to_integer(ctx
, v
);
261 static LLVMTypeRef
to_float_type_scalar(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
265 else if (t
== ctx
->i16
|| t
== ctx
->f16
)
267 else if (t
== ctx
->i32
|| t
== ctx
->f32
)
269 else if (t
== ctx
->i64
|| t
== ctx
->f64
)
272 unreachable("Unhandled float size");
276 ac_to_float_type(struct ac_llvm_context
*ctx
, LLVMTypeRef t
)
278 if (LLVMGetTypeKind(t
) == LLVMVectorTypeKind
) {
279 LLVMTypeRef elem_type
= LLVMGetElementType(t
);
280 return LLVMVectorType(to_float_type_scalar(ctx
, elem_type
),
281 LLVMGetVectorSize(t
));
283 return to_float_type_scalar(ctx
, t
);
287 ac_to_float(struct ac_llvm_context
*ctx
, LLVMValueRef v
)
289 LLVMTypeRef type
= LLVMTypeOf(v
);
290 return LLVMBuildBitCast(ctx
->builder
, v
, ac_to_float_type(ctx
, type
), "");
295 ac_build_intrinsic(struct ac_llvm_context
*ctx
, const char *name
,
296 LLVMTypeRef return_type
, LLVMValueRef
*params
,
297 unsigned param_count
, unsigned attrib_mask
)
299 LLVMValueRef function
, call
;
300 bool set_callsite_attrs
= !(attrib_mask
& AC_FUNC_ATTR_LEGACY
);
302 function
= LLVMGetNamedFunction(ctx
->module
, name
);
304 LLVMTypeRef param_types
[32], function_type
;
307 assert(param_count
<= 32);
309 for (i
= 0; i
< param_count
; ++i
) {
311 param_types
[i
] = LLVMTypeOf(params
[i
]);
314 LLVMFunctionType(return_type
, param_types
, param_count
, 0);
315 function
= LLVMAddFunction(ctx
->module
, name
, function_type
);
317 LLVMSetFunctionCallConv(function
, LLVMCCallConv
);
318 LLVMSetLinkage(function
, LLVMExternalLinkage
);
320 if (!set_callsite_attrs
)
321 ac_add_func_attributes(ctx
->context
, function
, attrib_mask
);
324 call
= LLVMBuildCall(ctx
->builder
, function
, params
, param_count
, "");
325 if (set_callsite_attrs
)
326 ac_add_func_attributes(ctx
->context
, call
, attrib_mask
);
331 * Given the i32 or vNi32 \p type, generate the textual name (e.g. for use with
334 void ac_build_type_name_for_intr(LLVMTypeRef type
, char *buf
, unsigned bufsize
)
336 LLVMTypeRef elem_type
= type
;
338 assert(bufsize
>= 8);
340 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
) {
341 int ret
= snprintf(buf
, bufsize
, "v%u",
342 LLVMGetVectorSize(type
));
344 char *type_name
= LLVMPrintTypeToString(type
);
345 fprintf(stderr
, "Error building type name for: %s\n",
349 elem_type
= LLVMGetElementType(type
);
353 switch (LLVMGetTypeKind(elem_type
)) {
355 case LLVMIntegerTypeKind
:
356 snprintf(buf
, bufsize
, "i%d", LLVMGetIntTypeWidth(elem_type
));
358 case LLVMHalfTypeKind
:
359 snprintf(buf
, bufsize
, "f16");
361 case LLVMFloatTypeKind
:
362 snprintf(buf
, bufsize
, "f32");
364 case LLVMDoubleTypeKind
:
365 snprintf(buf
, bufsize
, "f64");
371 * Helper function that builds an LLVM IR PHI node and immediately adds
375 ac_build_phi(struct ac_llvm_context
*ctx
, LLVMTypeRef type
,
376 unsigned count_incoming
, LLVMValueRef
*values
,
377 LLVMBasicBlockRef
*blocks
)
379 LLVMValueRef phi
= LLVMBuildPhi(ctx
->builder
, type
, "");
380 LLVMAddIncoming(phi
, values
, blocks
, count_incoming
);
384 void ac_build_s_barrier(struct ac_llvm_context
*ctx
)
386 ac_build_intrinsic(ctx
, "llvm.amdgcn.s.barrier", ctx
->voidt
, NULL
,
387 0, AC_FUNC_ATTR_CONVERGENT
);
390 /* Prevent optimizations (at least of memory accesses) across the current
391 * point in the program by emitting empty inline assembly that is marked as
392 * having side effects.
394 * Optionally, a value can be passed through the inline assembly to prevent
395 * LLVM from hoisting calls to ReadNone functions.
398 ac_build_optimization_barrier(struct ac_llvm_context
*ctx
,
401 static int counter
= 0;
403 LLVMBuilderRef builder
= ctx
->builder
;
406 snprintf(code
, sizeof(code
), "; %d", p_atomic_inc_return(&counter
));
409 LLVMTypeRef ftype
= LLVMFunctionType(ctx
->voidt
, NULL
, 0, false);
410 LLVMValueRef inlineasm
= LLVMConstInlineAsm(ftype
, code
, "", true, false);
411 LLVMBuildCall(builder
, inlineasm
, NULL
, 0, "");
413 LLVMTypeRef ftype
= LLVMFunctionType(ctx
->i32
, &ctx
->i32
, 1, false);
414 LLVMValueRef inlineasm
= LLVMConstInlineAsm(ftype
, code
, "=v,0", true, false);
415 LLVMValueRef vgpr
= *pvgpr
;
416 LLVMTypeRef vgpr_type
= LLVMTypeOf(vgpr
);
417 unsigned vgpr_size
= ac_get_type_size(vgpr_type
);
420 assert(vgpr_size
% 4 == 0);
422 vgpr
= LLVMBuildBitCast(builder
, vgpr
, LLVMVectorType(ctx
->i32
, vgpr_size
/ 4), "");
423 vgpr0
= LLVMBuildExtractElement(builder
, vgpr
, ctx
->i32_0
, "");
424 vgpr0
= LLVMBuildCall(builder
, inlineasm
, &vgpr0
, 1, "");
425 vgpr
= LLVMBuildInsertElement(builder
, vgpr
, vgpr0
, ctx
->i32_0
, "");
426 vgpr
= LLVMBuildBitCast(builder
, vgpr
, vgpr_type
, "");
433 ac_build_shader_clock(struct ac_llvm_context
*ctx
)
435 LLVMValueRef tmp
= ac_build_intrinsic(ctx
, "llvm.readcyclecounter",
436 ctx
->i64
, NULL
, 0, 0);
437 return LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->v2i32
, "");
441 ac_build_ballot(struct ac_llvm_context
*ctx
,
444 LLVMValueRef args
[3] = {
447 LLVMConstInt(ctx
->i32
, LLVMIntNE
, 0)
450 /* We currently have no other way to prevent LLVM from lifting the icmp
451 * calls to a dominating basic block.
453 ac_build_optimization_barrier(ctx
, &args
[0]);
455 args
[0] = ac_to_integer(ctx
, args
[0]);
457 return ac_build_intrinsic(ctx
,
458 "llvm.amdgcn.icmp.i32",
460 AC_FUNC_ATTR_NOUNWIND
|
461 AC_FUNC_ATTR_READNONE
|
462 AC_FUNC_ATTR_CONVERGENT
);
465 LLVMValueRef
ac_get_i1_sgpr_mask(struct ac_llvm_context
*ctx
,
468 LLVMValueRef args
[3] = {
471 LLVMConstInt(ctx
->i32
, LLVMIntNE
, 0),
474 assert(HAVE_LLVM
>= 0x0800);
475 return ac_build_intrinsic(ctx
, "llvm.amdgcn.icmp.i1", ctx
->i64
, args
, 3,
476 AC_FUNC_ATTR_NOUNWIND
|
477 AC_FUNC_ATTR_READNONE
|
478 AC_FUNC_ATTR_CONVERGENT
);
482 ac_build_vote_all(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
484 LLVMValueRef active_set
= ac_build_ballot(ctx
, ctx
->i32_1
);
485 LLVMValueRef vote_set
= ac_build_ballot(ctx
, value
);
486 return LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, vote_set
, active_set
, "");
490 ac_build_vote_any(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
492 LLVMValueRef vote_set
= ac_build_ballot(ctx
, value
);
493 return LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, vote_set
,
494 LLVMConstInt(ctx
->i64
, 0, 0), "");
498 ac_build_vote_eq(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
500 LLVMValueRef active_set
= ac_build_ballot(ctx
, ctx
->i32_1
);
501 LLVMValueRef vote_set
= ac_build_ballot(ctx
, value
);
503 LLVMValueRef all
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
504 vote_set
, active_set
, "");
505 LLVMValueRef none
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
507 LLVMConstInt(ctx
->i64
, 0, 0), "");
508 return LLVMBuildOr(ctx
->builder
, all
, none
, "");
512 ac_build_varying_gather_values(struct ac_llvm_context
*ctx
, LLVMValueRef
*values
,
513 unsigned value_count
, unsigned component
)
515 LLVMValueRef vec
= NULL
;
517 if (value_count
== 1) {
518 return values
[component
];
519 } else if (!value_count
)
520 unreachable("value_count is 0");
522 for (unsigned i
= component
; i
< value_count
+ component
; i
++) {
523 LLVMValueRef value
= values
[i
];
526 vec
= LLVMGetUndef( LLVMVectorType(LLVMTypeOf(value
), value_count
));
527 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, i
- component
, false);
528 vec
= LLVMBuildInsertElement(ctx
->builder
, vec
, value
, index
, "");
534 ac_build_gather_values_extended(struct ac_llvm_context
*ctx
,
535 LLVMValueRef
*values
,
536 unsigned value_count
,
537 unsigned value_stride
,
541 LLVMBuilderRef builder
= ctx
->builder
;
542 LLVMValueRef vec
= NULL
;
545 if (value_count
== 1 && !always_vector
) {
547 return LLVMBuildLoad(builder
, values
[0], "");
549 } else if (!value_count
)
550 unreachable("value_count is 0");
552 for (i
= 0; i
< value_count
; i
++) {
553 LLVMValueRef value
= values
[i
* value_stride
];
555 value
= LLVMBuildLoad(builder
, value
, "");
558 vec
= LLVMGetUndef( LLVMVectorType(LLVMTypeOf(value
), value_count
));
559 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, i
, false);
560 vec
= LLVMBuildInsertElement(builder
, vec
, value
, index
, "");
566 ac_build_gather_values(struct ac_llvm_context
*ctx
,
567 LLVMValueRef
*values
,
568 unsigned value_count
)
570 return ac_build_gather_values_extended(ctx
, values
, value_count
, 1, false, false);
573 /* Expand a scalar or vector to <dst_channels x type> by filling the remaining
574 * channels with undef. Extract at most src_channels components from the input.
577 ac_build_expand(struct ac_llvm_context
*ctx
,
579 unsigned src_channels
,
580 unsigned dst_channels
)
582 LLVMTypeRef elemtype
;
583 LLVMValueRef chan
[dst_channels
];
585 if (LLVMGetTypeKind(LLVMTypeOf(value
)) == LLVMVectorTypeKind
) {
586 unsigned vec_size
= LLVMGetVectorSize(LLVMTypeOf(value
));
588 if (src_channels
== dst_channels
&& vec_size
== dst_channels
)
591 src_channels
= MIN2(src_channels
, vec_size
);
593 for (unsigned i
= 0; i
< src_channels
; i
++)
594 chan
[i
] = ac_llvm_extract_elem(ctx
, value
, i
);
596 elemtype
= LLVMGetElementType(LLVMTypeOf(value
));
599 assert(src_channels
== 1);
602 elemtype
= LLVMTypeOf(value
);
605 for (unsigned i
= src_channels
; i
< dst_channels
; i
++)
606 chan
[i
] = LLVMGetUndef(elemtype
);
608 return ac_build_gather_values(ctx
, chan
, dst_channels
);
611 /* Expand a scalar or vector to <4 x type> by filling the remaining channels
612 * with undef. Extract at most num_channels components from the input.
614 LLVMValueRef
ac_build_expand_to_vec4(struct ac_llvm_context
*ctx
,
616 unsigned num_channels
)
618 return ac_build_expand(ctx
, value
, num_channels
, 4);
621 LLVMValueRef
ac_build_round(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
623 unsigned type_size
= ac_get_type_size(LLVMTypeOf(value
));
627 name
= "llvm.rint.f16";
628 else if (type_size
== 4)
629 name
= "llvm.rint.f32";
631 name
= "llvm.rint.f64";
633 return ac_build_intrinsic(ctx
, name
, LLVMTypeOf(value
), &value
, 1,
634 AC_FUNC_ATTR_READNONE
);
638 ac_build_fdiv(struct ac_llvm_context
*ctx
,
642 /* If we do (num / den), LLVM >= 7.0 does:
643 * return num * v_rcp_f32(den * (fabs(den) > 0x1.0p+96f ? 0x1.0p-32f : 1.0f));
645 * If we do (num * (1 / den)), LLVM does:
646 * return num * v_rcp_f32(den);
648 LLVMValueRef one
= LLVMConstReal(LLVMTypeOf(num
), 1.0);
649 LLVMValueRef rcp
= LLVMBuildFDiv(ctx
->builder
, one
, den
, "");
650 LLVMValueRef ret
= LLVMBuildFMul(ctx
->builder
, num
, rcp
, "");
652 /* Use v_rcp_f32 instead of precise division. */
653 if (!LLVMIsConstant(ret
))
654 LLVMSetMetadata(ret
, ctx
->fpmath_md_kind
, ctx
->fpmath_md_2p5_ulp
);
658 /* See fast_idiv_by_const.h. */
659 /* Set: increment = util_fast_udiv_info::increment ? multiplier : 0; */
660 LLVMValueRef
ac_build_fast_udiv(struct ac_llvm_context
*ctx
,
662 LLVMValueRef multiplier
,
663 LLVMValueRef pre_shift
,
664 LLVMValueRef post_shift
,
665 LLVMValueRef increment
)
667 LLVMBuilderRef builder
= ctx
->builder
;
669 num
= LLVMBuildLShr(builder
, num
, pre_shift
, "");
670 num
= LLVMBuildMul(builder
,
671 LLVMBuildZExt(builder
, num
, ctx
->i64
, ""),
672 LLVMBuildZExt(builder
, multiplier
, ctx
->i64
, ""), "");
673 num
= LLVMBuildAdd(builder
, num
,
674 LLVMBuildZExt(builder
, increment
, ctx
->i64
, ""), "");
675 num
= LLVMBuildLShr(builder
, num
, LLVMConstInt(ctx
->i64
, 32, 0), "");
676 num
= LLVMBuildTrunc(builder
, num
, ctx
->i32
, "");
677 return LLVMBuildLShr(builder
, num
, post_shift
, "");
680 /* See fast_idiv_by_const.h. */
681 /* If num != UINT_MAX, this more efficient version can be used. */
682 /* Set: increment = util_fast_udiv_info::increment; */
683 LLVMValueRef
ac_build_fast_udiv_nuw(struct ac_llvm_context
*ctx
,
685 LLVMValueRef multiplier
,
686 LLVMValueRef pre_shift
,
687 LLVMValueRef post_shift
,
688 LLVMValueRef increment
)
690 LLVMBuilderRef builder
= ctx
->builder
;
692 num
= LLVMBuildLShr(builder
, num
, pre_shift
, "");
693 num
= LLVMBuildNUWAdd(builder
, num
, increment
, "");
694 num
= LLVMBuildMul(builder
,
695 LLVMBuildZExt(builder
, num
, ctx
->i64
, ""),
696 LLVMBuildZExt(builder
, multiplier
, ctx
->i64
, ""), "");
697 num
= LLVMBuildLShr(builder
, num
, LLVMConstInt(ctx
->i64
, 32, 0), "");
698 num
= LLVMBuildTrunc(builder
, num
, ctx
->i32
, "");
699 return LLVMBuildLShr(builder
, num
, post_shift
, "");
702 /* See fast_idiv_by_const.h. */
703 /* Both operands must fit in 31 bits and the divisor must not be 1. */
704 LLVMValueRef
ac_build_fast_udiv_u31_d_not_one(struct ac_llvm_context
*ctx
,
706 LLVMValueRef multiplier
,
707 LLVMValueRef post_shift
)
709 LLVMBuilderRef builder
= ctx
->builder
;
711 num
= LLVMBuildMul(builder
,
712 LLVMBuildZExt(builder
, num
, ctx
->i64
, ""),
713 LLVMBuildZExt(builder
, multiplier
, ctx
->i64
, ""), "");
714 num
= LLVMBuildLShr(builder
, num
, LLVMConstInt(ctx
->i64
, 32, 0), "");
715 num
= LLVMBuildTrunc(builder
, num
, ctx
->i32
, "");
716 return LLVMBuildLShr(builder
, num
, post_shift
, "");
719 /* Coordinates for cube map selection. sc, tc, and ma are as in Table 8.27
720 * of the OpenGL 4.5 (Compatibility Profile) specification, except ma is
721 * already multiplied by two. id is the cube face number.
723 struct cube_selection_coords
{
730 build_cube_intrinsic(struct ac_llvm_context
*ctx
,
732 struct cube_selection_coords
*out
)
734 LLVMTypeRef f32
= ctx
->f32
;
736 out
->stc
[1] = ac_build_intrinsic(ctx
, "llvm.amdgcn.cubetc",
737 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
738 out
->stc
[0] = ac_build_intrinsic(ctx
, "llvm.amdgcn.cubesc",
739 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
740 out
->ma
= ac_build_intrinsic(ctx
, "llvm.amdgcn.cubema",
741 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
742 out
->id
= ac_build_intrinsic(ctx
, "llvm.amdgcn.cubeid",
743 f32
, in
, 3, AC_FUNC_ATTR_READNONE
);
747 * Build a manual selection sequence for cube face sc/tc coordinates and
748 * major axis vector (multiplied by 2 for consistency) for the given
749 * vec3 \p coords, for the face implied by \p selcoords.
751 * For the major axis, we always adjust the sign to be in the direction of
752 * selcoords.ma; i.e., a positive out_ma means that coords is pointed towards
753 * the selcoords major axis.
755 static void build_cube_select(struct ac_llvm_context
*ctx
,
756 const struct cube_selection_coords
*selcoords
,
757 const LLVMValueRef
*coords
,
758 LLVMValueRef
*out_st
,
759 LLVMValueRef
*out_ma
)
761 LLVMBuilderRef builder
= ctx
->builder
;
762 LLVMTypeRef f32
= LLVMTypeOf(coords
[0]);
763 LLVMValueRef is_ma_positive
;
765 LLVMValueRef is_ma_z
, is_not_ma_z
;
766 LLVMValueRef is_ma_y
;
767 LLVMValueRef is_ma_x
;
771 is_ma_positive
= LLVMBuildFCmp(builder
, LLVMRealUGE
,
772 selcoords
->ma
, LLVMConstReal(f32
, 0.0), "");
773 sgn_ma
= LLVMBuildSelect(builder
, is_ma_positive
,
774 LLVMConstReal(f32
, 1.0), LLVMConstReal(f32
, -1.0), "");
776 is_ma_z
= LLVMBuildFCmp(builder
, LLVMRealUGE
, selcoords
->id
, LLVMConstReal(f32
, 4.0), "");
777 is_not_ma_z
= LLVMBuildNot(builder
, is_ma_z
, "");
778 is_ma_y
= LLVMBuildAnd(builder
, is_not_ma_z
,
779 LLVMBuildFCmp(builder
, LLVMRealUGE
, selcoords
->id
, LLVMConstReal(f32
, 2.0), ""), "");
780 is_ma_x
= LLVMBuildAnd(builder
, is_not_ma_z
, LLVMBuildNot(builder
, is_ma_y
, ""), "");
783 tmp
= LLVMBuildSelect(builder
, is_ma_x
, coords
[2], coords
[0], "");
784 sgn
= LLVMBuildSelect(builder
, is_ma_y
, LLVMConstReal(f32
, 1.0),
785 LLVMBuildSelect(builder
, is_ma_z
, sgn_ma
,
786 LLVMBuildFNeg(builder
, sgn_ma
, ""), ""), "");
787 out_st
[0] = LLVMBuildFMul(builder
, tmp
, sgn
, "");
790 tmp
= LLVMBuildSelect(builder
, is_ma_y
, coords
[2], coords
[1], "");
791 sgn
= LLVMBuildSelect(builder
, is_ma_y
, sgn_ma
,
792 LLVMConstReal(f32
, -1.0), "");
793 out_st
[1] = LLVMBuildFMul(builder
, tmp
, sgn
, "");
796 tmp
= LLVMBuildSelect(builder
, is_ma_z
, coords
[2],
797 LLVMBuildSelect(builder
, is_ma_y
, coords
[1], coords
[0], ""), "");
798 tmp
= ac_build_intrinsic(ctx
, "llvm.fabs.f32",
799 ctx
->f32
, &tmp
, 1, AC_FUNC_ATTR_READNONE
);
800 *out_ma
= LLVMBuildFMul(builder
, tmp
, LLVMConstReal(f32
, 2.0), "");
804 ac_prepare_cube_coords(struct ac_llvm_context
*ctx
,
805 bool is_deriv
, bool is_array
, bool is_lod
,
806 LLVMValueRef
*coords_arg
,
807 LLVMValueRef
*derivs_arg
)
810 LLVMBuilderRef builder
= ctx
->builder
;
811 struct cube_selection_coords selcoords
;
812 LLVMValueRef coords
[3];
815 if (is_array
&& !is_lod
) {
816 LLVMValueRef tmp
= ac_build_round(ctx
, coords_arg
[3]);
818 /* Section 8.9 (Texture Functions) of the GLSL 4.50 spec says:
820 * "For Array forms, the array layer used will be
822 * max(0, min(d−1, floor(layer+0.5)))
824 * where d is the depth of the texture array and layer
825 * comes from the component indicated in the tables below.
826 * Workaroudn for an issue where the layer is taken from a
827 * helper invocation which happens to fall on a different
828 * layer due to extrapolation."
830 * GFX8 and earlier attempt to implement this in hardware by
831 * clamping the value of coords[2] = (8 * layer) + face.
832 * Unfortunately, this means that the we end up with the wrong
833 * face when clamping occurs.
835 * Clamp the layer earlier to work around the issue.
837 if (ctx
->chip_class
<= GFX8
) {
839 ge0
= LLVMBuildFCmp(builder
, LLVMRealOGE
, tmp
, ctx
->f32_0
, "");
840 tmp
= LLVMBuildSelect(builder
, ge0
, tmp
, ctx
->f32_0
, "");
846 build_cube_intrinsic(ctx
, coords_arg
, &selcoords
);
848 invma
= ac_build_intrinsic(ctx
, "llvm.fabs.f32",
849 ctx
->f32
, &selcoords
.ma
, 1, AC_FUNC_ATTR_READNONE
);
850 invma
= ac_build_fdiv(ctx
, LLVMConstReal(ctx
->f32
, 1.0), invma
);
852 for (int i
= 0; i
< 2; ++i
)
853 coords
[i
] = LLVMBuildFMul(builder
, selcoords
.stc
[i
], invma
, "");
855 coords
[2] = selcoords
.id
;
857 if (is_deriv
&& derivs_arg
) {
858 LLVMValueRef derivs
[4];
861 /* Convert cube derivatives to 2D derivatives. */
862 for (axis
= 0; axis
< 2; axis
++) {
863 LLVMValueRef deriv_st
[2];
864 LLVMValueRef deriv_ma
;
866 /* Transform the derivative alongside the texture
867 * coordinate. Mathematically, the correct formula is
868 * as follows. Assume we're projecting onto the +Z face
869 * and denote by dx/dh the derivative of the (original)
870 * X texture coordinate with respect to horizontal
871 * window coordinates. The projection onto the +Z face
876 * Then df/dh = df/dx * dx/dh + df/dz * dz/dh
877 * = 1/z * dx/dh - x/z * 1/z * dz/dh.
879 * This motivatives the implementation below.
881 * Whether this actually gives the expected results for
882 * apps that might feed in derivatives obtained via
883 * finite differences is anyone's guess. The OpenGL spec
884 * seems awfully quiet about how textureGrad for cube
885 * maps should be handled.
887 build_cube_select(ctx
, &selcoords
, &derivs_arg
[axis
* 3],
888 deriv_st
, &deriv_ma
);
890 deriv_ma
= LLVMBuildFMul(builder
, deriv_ma
, invma
, "");
892 for (int i
= 0; i
< 2; ++i
)
893 derivs
[axis
* 2 + i
] =
894 LLVMBuildFSub(builder
,
895 LLVMBuildFMul(builder
, deriv_st
[i
], invma
, ""),
896 LLVMBuildFMul(builder
, deriv_ma
, coords
[i
], ""), "");
899 memcpy(derivs_arg
, derivs
, sizeof(derivs
));
902 /* Shift the texture coordinate. This must be applied after the
903 * derivative calculation.
905 for (int i
= 0; i
< 2; ++i
)
906 coords
[i
] = LLVMBuildFAdd(builder
, coords
[i
], LLVMConstReal(ctx
->f32
, 1.5), "");
909 /* for cube arrays coord.z = coord.w(array_index) * 8 + face */
910 /* coords_arg.w component - array_index for cube arrays */
911 coords
[2] = ac_build_fmad(ctx
, coords_arg
[3], LLVMConstReal(ctx
->f32
, 8.0), coords
[2]);
914 memcpy(coords_arg
, coords
, sizeof(coords
));
919 ac_build_fs_interp(struct ac_llvm_context
*ctx
,
920 LLVMValueRef llvm_chan
,
921 LLVMValueRef attr_number
,
926 LLVMValueRef args
[5];
931 args
[2] = attr_number
;
934 p1
= ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p1",
935 ctx
->f32
, args
, 4, AC_FUNC_ATTR_READNONE
);
940 args
[3] = attr_number
;
943 return ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p2",
944 ctx
->f32
, args
, 5, AC_FUNC_ATTR_READNONE
);
948 ac_build_fs_interp_f16(struct ac_llvm_context
*ctx
,
949 LLVMValueRef llvm_chan
,
950 LLVMValueRef attr_number
,
955 LLVMValueRef args
[6];
960 args
[2] = attr_number
;
961 args
[3] = ctx
->i1false
;
964 p1
= ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p1.f16",
965 ctx
->f32
, args
, 5, AC_FUNC_ATTR_READNONE
);
970 args
[3] = attr_number
;
971 args
[4] = ctx
->i1false
;
974 return ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.p2.f16",
975 ctx
->f16
, args
, 6, AC_FUNC_ATTR_READNONE
);
979 ac_build_fs_interp_mov(struct ac_llvm_context
*ctx
,
980 LLVMValueRef parameter
,
981 LLVMValueRef llvm_chan
,
982 LLVMValueRef attr_number
,
985 LLVMValueRef args
[4];
989 args
[2] = attr_number
;
992 return ac_build_intrinsic(ctx
, "llvm.amdgcn.interp.mov",
993 ctx
->f32
, args
, 4, AC_FUNC_ATTR_READNONE
);
997 ac_build_gep_ptr(struct ac_llvm_context
*ctx
,
998 LLVMValueRef base_ptr
,
1001 return LLVMBuildGEP(ctx
->builder
, base_ptr
, &index
, 1, "");
1005 ac_build_gep0(struct ac_llvm_context
*ctx
,
1006 LLVMValueRef base_ptr
,
1009 LLVMValueRef indices
[2] = {
1013 return LLVMBuildGEP(ctx
->builder
, base_ptr
, indices
, 2, "");
1016 LLVMValueRef
ac_build_pointer_add(struct ac_llvm_context
*ctx
, LLVMValueRef ptr
,
1019 return LLVMBuildPointerCast(ctx
->builder
,
1020 LLVMBuildGEP(ctx
->builder
, ptr
, &index
, 1, ""),
1021 LLVMTypeOf(ptr
), "");
1025 ac_build_indexed_store(struct ac_llvm_context
*ctx
,
1026 LLVMValueRef base_ptr
, LLVMValueRef index
,
1029 LLVMBuildStore(ctx
->builder
, value
,
1030 ac_build_gep0(ctx
, base_ptr
, index
));
1034 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad.
1035 * It's equivalent to doing a load from &base_ptr[index].
1037 * \param base_ptr Where the array starts.
1038 * \param index The element index into the array.
1039 * \param uniform Whether the base_ptr and index can be assumed to be
1040 * dynamically uniform (i.e. load to an SGPR)
1041 * \param invariant Whether the load is invariant (no other opcodes affect it)
1042 * \param no_unsigned_wraparound
1043 * For all possible re-associations and re-distributions of an expression
1044 * "base_ptr + index * elemsize" into "addr + offset" (excluding GEPs
1045 * without inbounds in base_ptr), this parameter is true if "addr + offset"
1046 * does not result in an unsigned integer wraparound. This is used for
1047 * optimal code generation of 32-bit pointer arithmetic.
1049 * For example, a 32-bit immediate offset that causes a 32-bit unsigned
1050 * integer wraparound can't be an imm offset in s_load_dword, because
1051 * the instruction performs "addr + offset" in 64 bits.
1053 * Expected usage for bindless textures by chaining GEPs:
1054 * // possible unsigned wraparound, don't use InBounds:
1055 * ptr1 = LLVMBuildGEP(base_ptr, index);
1056 * image = load(ptr1); // becomes "s_load ptr1, 0"
1058 * ptr2 = LLVMBuildInBoundsGEP(ptr1, 32 / elemsize);
1059 * sampler = load(ptr2); // becomes "s_load ptr1, 32" thanks to InBounds
1062 ac_build_load_custom(struct ac_llvm_context
*ctx
, LLVMValueRef base_ptr
,
1063 LLVMValueRef index
, bool uniform
, bool invariant
,
1064 bool no_unsigned_wraparound
)
1066 LLVMValueRef pointer
, result
;
1068 if (no_unsigned_wraparound
&&
1069 LLVMGetPointerAddressSpace(LLVMTypeOf(base_ptr
)) == AC_ADDR_SPACE_CONST_32BIT
)
1070 pointer
= LLVMBuildInBoundsGEP(ctx
->builder
, base_ptr
, &index
, 1, "");
1072 pointer
= LLVMBuildGEP(ctx
->builder
, base_ptr
, &index
, 1, "");
1075 LLVMSetMetadata(pointer
, ctx
->uniform_md_kind
, ctx
->empty_md
);
1076 result
= LLVMBuildLoad(ctx
->builder
, pointer
, "");
1078 LLVMSetMetadata(result
, ctx
->invariant_load_md_kind
, ctx
->empty_md
);
1082 LLVMValueRef
ac_build_load(struct ac_llvm_context
*ctx
, LLVMValueRef base_ptr
,
1085 return ac_build_load_custom(ctx
, base_ptr
, index
, false, false, false);
1088 LLVMValueRef
ac_build_load_invariant(struct ac_llvm_context
*ctx
,
1089 LLVMValueRef base_ptr
, LLVMValueRef index
)
1091 return ac_build_load_custom(ctx
, base_ptr
, index
, false, true, false);
1094 /* This assumes that there is no unsigned integer wraparound during the address
1095 * computation, excluding all GEPs within base_ptr. */
1096 LLVMValueRef
ac_build_load_to_sgpr(struct ac_llvm_context
*ctx
,
1097 LLVMValueRef base_ptr
, LLVMValueRef index
)
1099 return ac_build_load_custom(ctx
, base_ptr
, index
, true, true, true);
1102 /* See ac_build_load_custom() documentation. */
1103 LLVMValueRef
ac_build_load_to_sgpr_uint_wraparound(struct ac_llvm_context
*ctx
,
1104 LLVMValueRef base_ptr
, LLVMValueRef index
)
1106 return ac_build_load_custom(ctx
, base_ptr
, index
, true, true, false);
1110 ac_build_buffer_store_common(struct ac_llvm_context
*ctx
,
1113 LLVMValueRef vindex
,
1114 LLVMValueRef voffset
,
1115 unsigned num_channels
,
1118 bool writeonly_memory
,
1121 LLVMValueRef args
[] = {
1123 LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, ""),
1124 vindex
? vindex
: ctx
->i32_0
,
1126 LLVMConstInt(ctx
->i1
, glc
, 0),
1127 LLVMConstInt(ctx
->i1
, slc
, 0)
1129 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
1131 const char *type_names
[] = {"f32", "v2f32", "v4f32"};
1135 snprintf(name
, sizeof(name
), "llvm.amdgcn.buffer.store.format.%s",
1138 snprintf(name
, sizeof(name
), "llvm.amdgcn.buffer.store.%s",
1142 ac_build_intrinsic(ctx
, name
, ctx
->voidt
, args
, ARRAY_SIZE(args
),
1143 ac_get_store_intr_attribs(writeonly_memory
));
1147 ac_build_llvm8_buffer_store_common(struct ac_llvm_context
*ctx
,
1150 LLVMValueRef vindex
,
1151 LLVMValueRef voffset
,
1152 LLVMValueRef soffset
,
1153 unsigned num_channels
,
1154 LLVMTypeRef return_channel_type
,
1157 bool writeonly_memory
,
1161 LLVMValueRef args
[6];
1164 args
[idx
++] = LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, "");
1166 args
[idx
++] = vindex
? vindex
: ctx
->i32_0
;
1167 args
[idx
++] = voffset
? voffset
: ctx
->i32_0
;
1168 args
[idx
++] = soffset
? soffset
: ctx
->i32_0
;
1169 args
[idx
++] = LLVMConstInt(ctx
->i32
, (glc
? 1 : 0) + (slc
? 2 : 0), 0);
1170 unsigned func
= !ac_has_vec3_support(ctx
->chip_class
, use_format
) && num_channels
== 3 ? 4 : num_channels
;
1171 const char *indexing_kind
= structurized
? "struct" : "raw";
1172 char name
[256], type_name
[8];
1174 LLVMTypeRef type
= func
> 1 ? LLVMVectorType(return_channel_type
, func
) : return_channel_type
;
1175 ac_build_type_name_for_intr(type
, type_name
, sizeof(type_name
));
1178 snprintf(name
, sizeof(name
), "llvm.amdgcn.%s.buffer.store.format.%s",
1179 indexing_kind
, type_name
);
1181 snprintf(name
, sizeof(name
), "llvm.amdgcn.%s.buffer.store.%s",
1182 indexing_kind
, type_name
);
1185 ac_build_intrinsic(ctx
, name
, ctx
->voidt
, args
, idx
,
1186 ac_get_store_intr_attribs(writeonly_memory
));
1190 ac_build_buffer_store_format(struct ac_llvm_context
*ctx
,
1193 LLVMValueRef vindex
,
1194 LLVMValueRef voffset
,
1195 unsigned num_channels
,
1197 bool writeonly_memory
)
1199 if (HAVE_LLVM
>= 0x800) {
1200 ac_build_llvm8_buffer_store_common(ctx
, rsrc
, data
, vindex
,
1201 voffset
, NULL
, num_channels
,
1202 ctx
->f32
, glc
, false,
1203 writeonly_memory
, true, true);
1205 ac_build_buffer_store_common(ctx
, rsrc
, data
, vindex
, voffset
,
1206 num_channels
, glc
, false,
1207 writeonly_memory
, true);
1211 /* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
1212 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
1213 * or v4i32 (num_channels=3,4).
1216 ac_build_buffer_store_dword(struct ac_llvm_context
*ctx
,
1219 unsigned num_channels
,
1220 LLVMValueRef voffset
,
1221 LLVMValueRef soffset
,
1222 unsigned inst_offset
,
1225 bool writeonly_memory
,
1226 bool swizzle_enable_hint
)
1228 /* Split 3 channel stores, because only LLVM 9+ support 3-channel
1230 if (num_channels
== 3 && !ac_has_vec3_support(ctx
->chip_class
, false)) {
1231 LLVMValueRef v
[3], v01
;
1233 for (int i
= 0; i
< 3; i
++) {
1234 v
[i
] = LLVMBuildExtractElement(ctx
->builder
, vdata
,
1235 LLVMConstInt(ctx
->i32
, i
, 0), "");
1237 v01
= ac_build_gather_values(ctx
, v
, 2);
1239 ac_build_buffer_store_dword(ctx
, rsrc
, v01
, 2, voffset
,
1240 soffset
, inst_offset
, glc
, slc
,
1241 writeonly_memory
, swizzle_enable_hint
);
1242 ac_build_buffer_store_dword(ctx
, rsrc
, v
[2], 1, voffset
,
1243 soffset
, inst_offset
+ 8,
1245 writeonly_memory
, swizzle_enable_hint
);
1249 /* SWIZZLE_ENABLE requires that soffset isn't folded into voffset
1250 * (voffset is swizzled, but soffset isn't swizzled).
1251 * llvm.amdgcn.buffer.store doesn't have a separate soffset parameter.
1253 if (!swizzle_enable_hint
) {
1254 LLVMValueRef offset
= soffset
;
1257 offset
= LLVMBuildAdd(ctx
->builder
, offset
,
1258 LLVMConstInt(ctx
->i32
, inst_offset
, 0), "");
1260 if (HAVE_LLVM
>= 0x800) {
1261 ac_build_llvm8_buffer_store_common(ctx
, rsrc
,
1262 ac_to_float(ctx
, vdata
),
1272 offset
= LLVMBuildAdd(ctx
->builder
, offset
, voffset
, "");
1274 ac_build_buffer_store_common(ctx
, rsrc
,
1275 ac_to_float(ctx
, vdata
),
1277 num_channels
, glc
, slc
,
1278 writeonly_memory
, false);
1283 static const unsigned dfmts
[] = {
1284 V_008F0C_BUF_DATA_FORMAT_32
,
1285 V_008F0C_BUF_DATA_FORMAT_32_32
,
1286 V_008F0C_BUF_DATA_FORMAT_32_32_32
,
1287 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
1289 unsigned dfmt
= dfmts
[num_channels
- 1];
1290 unsigned nfmt
= V_008F0C_BUF_NUM_FORMAT_UINT
;
1291 LLVMValueRef immoffset
= LLVMConstInt(ctx
->i32
, inst_offset
, 0);
1293 ac_build_raw_tbuffer_store(ctx
, rsrc
, vdata
, voffset
, soffset
,
1294 immoffset
, num_channels
, dfmt
, nfmt
, glc
,
1295 slc
, writeonly_memory
);
1299 ac_build_buffer_load_common(struct ac_llvm_context
*ctx
,
1301 LLVMValueRef vindex
,
1302 LLVMValueRef voffset
,
1303 unsigned num_channels
,
1309 LLVMValueRef args
[] = {
1310 LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, ""),
1311 vindex
? vindex
: ctx
->i32_0
,
1313 LLVMConstInt(ctx
->i1
, glc
, 0),
1314 LLVMConstInt(ctx
->i1
, slc
, 0)
1316 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
1318 LLVMTypeRef types
[] = {ctx
->f32
, ctx
->v2f32
, ctx
->v4f32
};
1319 const char *type_names
[] = {"f32", "v2f32", "v4f32"};
1323 snprintf(name
, sizeof(name
), "llvm.amdgcn.buffer.load.format.%s",
1326 snprintf(name
, sizeof(name
), "llvm.amdgcn.buffer.load.%s",
1330 return ac_build_intrinsic(ctx
, name
, types
[func
], args
,
1332 ac_get_load_intr_attribs(can_speculate
));
1336 ac_build_llvm8_buffer_load_common(struct ac_llvm_context
*ctx
,
1338 LLVMValueRef vindex
,
1339 LLVMValueRef voffset
,
1340 LLVMValueRef soffset
,
1341 unsigned num_channels
,
1342 LLVMTypeRef channel_type
,
1349 LLVMValueRef args
[5];
1351 args
[idx
++] = LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, "");
1353 args
[idx
++] = vindex
? vindex
: ctx
->i32_0
;
1354 args
[idx
++] = voffset
? voffset
: ctx
->i32_0
;
1355 args
[idx
++] = soffset
? soffset
: ctx
->i32_0
;
1356 args
[idx
++] = LLVMConstInt(ctx
->i32
, (glc
? 1 : 0) + (slc
? 2 : 0), 0);
1357 unsigned func
= !ac_has_vec3_support(ctx
->chip_class
, use_format
) && num_channels
== 3 ? 4 : num_channels
;
1358 const char *indexing_kind
= structurized
? "struct" : "raw";
1359 char name
[256], type_name
[8];
1361 LLVMTypeRef type
= func
> 1 ? LLVMVectorType(channel_type
, func
) : channel_type
;
1362 ac_build_type_name_for_intr(type
, type_name
, sizeof(type_name
));
1365 snprintf(name
, sizeof(name
), "llvm.amdgcn.%s.buffer.load.format.%s",
1366 indexing_kind
, type_name
);
1368 snprintf(name
, sizeof(name
), "llvm.amdgcn.%s.buffer.load.%s",
1369 indexing_kind
, type_name
);
1372 return ac_build_intrinsic(ctx
, name
, type
, args
, idx
,
1373 ac_get_load_intr_attribs(can_speculate
));
1377 ac_build_buffer_load(struct ac_llvm_context
*ctx
,
1380 LLVMValueRef vindex
,
1381 LLVMValueRef voffset
,
1382 LLVMValueRef soffset
,
1383 unsigned inst_offset
,
1389 LLVMValueRef offset
= LLVMConstInt(ctx
->i32
, inst_offset
, 0);
1391 offset
= LLVMBuildAdd(ctx
->builder
, offset
, voffset
, "");
1393 offset
= LLVMBuildAdd(ctx
->builder
, offset
, soffset
, "");
1395 if (allow_smem
&& !slc
&&
1396 (!glc
|| (HAVE_LLVM
>= 0x0800 && ctx
->chip_class
>= GFX8
))) {
1397 assert(vindex
== NULL
);
1399 LLVMValueRef result
[8];
1401 for (int i
= 0; i
< num_channels
; i
++) {
1403 offset
= LLVMBuildAdd(ctx
->builder
, offset
,
1404 LLVMConstInt(ctx
->i32
, 4, 0), "");
1406 const char *intrname
=
1407 HAVE_LLVM
>= 0x0800 ? "llvm.amdgcn.s.buffer.load.f32"
1408 : "llvm.SI.load.const.v4i32";
1409 unsigned num_args
= HAVE_LLVM
>= 0x0800 ? 3 : 2;
1410 LLVMValueRef args
[3] = {
1413 glc
? ctx
->i32_1
: ctx
->i32_0
,
1415 result
[i
] = ac_build_intrinsic(ctx
, intrname
,
1416 ctx
->f32
, args
, num_args
,
1417 AC_FUNC_ATTR_READNONE
|
1418 (HAVE_LLVM
< 0x0800 ? AC_FUNC_ATTR_LEGACY
: 0));
1420 if (num_channels
== 1)
1423 if (num_channels
== 3 && !ac_has_vec3_support(ctx
->chip_class
, false))
1424 result
[num_channels
++] = LLVMGetUndef(ctx
->f32
);
1425 return ac_build_gather_values(ctx
, result
, num_channels
);
1428 if (HAVE_LLVM
>= 0x0800) {
1429 return ac_build_llvm8_buffer_load_common(ctx
, rsrc
, vindex
,
1431 num_channels
, ctx
->f32
,
1433 can_speculate
, false,
1437 return ac_build_buffer_load_common(ctx
, rsrc
, vindex
, offset
,
1438 num_channels
, glc
, slc
,
1439 can_speculate
, false);
1442 LLVMValueRef
ac_build_buffer_load_format(struct ac_llvm_context
*ctx
,
1444 LLVMValueRef vindex
,
1445 LLVMValueRef voffset
,
1446 unsigned num_channels
,
1450 if (HAVE_LLVM
>= 0x800) {
1451 return ac_build_llvm8_buffer_load_common(ctx
, rsrc
, vindex
, voffset
, ctx
->i32_0
,
1452 num_channels
, ctx
->f32
,
1454 can_speculate
, true, true);
1456 return ac_build_buffer_load_common(ctx
, rsrc
, vindex
, voffset
,
1457 num_channels
, glc
, false,
1458 can_speculate
, true);
1461 LLVMValueRef
ac_build_buffer_load_format_gfx9_safe(struct ac_llvm_context
*ctx
,
1463 LLVMValueRef vindex
,
1464 LLVMValueRef voffset
,
1465 unsigned num_channels
,
1469 if (HAVE_LLVM
>= 0x800) {
1470 return ac_build_llvm8_buffer_load_common(ctx
, rsrc
, vindex
, voffset
, ctx
->i32_0
,
1471 num_channels
, ctx
->f32
,
1473 can_speculate
, true, true);
1476 LLVMValueRef elem_count
= LLVMBuildExtractElement(ctx
->builder
, rsrc
, LLVMConstInt(ctx
->i32
, 2, 0), "");
1477 LLVMValueRef stride
= LLVMBuildExtractElement(ctx
->builder
, rsrc
, ctx
->i32_1
, "");
1478 stride
= LLVMBuildLShr(ctx
->builder
, stride
, LLVMConstInt(ctx
->i32
, 16, 0), "");
1480 LLVMValueRef new_elem_count
= LLVMBuildSelect(ctx
->builder
,
1481 LLVMBuildICmp(ctx
->builder
, LLVMIntUGT
, elem_count
, stride
, ""),
1482 elem_count
, stride
, "");
1484 LLVMValueRef new_rsrc
= LLVMBuildInsertElement(ctx
->builder
, rsrc
, new_elem_count
,
1485 LLVMConstInt(ctx
->i32
, 2, 0), "");
1487 return ac_build_buffer_load_common(ctx
, new_rsrc
, vindex
, voffset
,
1488 num_channels
, glc
, false,
1489 can_speculate
, true);
1493 ac_build_llvm8_tbuffer_load(struct ac_llvm_context
*ctx
,
1495 LLVMValueRef vindex
,
1496 LLVMValueRef voffset
,
1497 LLVMValueRef soffset
,
1498 unsigned num_channels
,
1506 LLVMValueRef args
[6];
1508 args
[idx
++] = LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, "");
1510 args
[idx
++] = vindex
? vindex
: ctx
->i32_0
;
1511 args
[idx
++] = voffset
? voffset
: ctx
->i32_0
;
1512 args
[idx
++] = soffset
? soffset
: ctx
->i32_0
;
1513 args
[idx
++] = LLVMConstInt(ctx
->i32
, dfmt
| (nfmt
<< 4), 0);
1514 args
[idx
++] = LLVMConstInt(ctx
->i32
, (glc
? 1 : 0) + (slc
? 2 : 0), 0);
1515 unsigned func
= !ac_has_vec3_support(ctx
->chip_class
, true) && num_channels
== 3 ? 4 : num_channels
;
1516 const char *indexing_kind
= structurized
? "struct" : "raw";
1517 char name
[256], type_name
[8];
1519 LLVMTypeRef type
= func
> 1 ? LLVMVectorType(ctx
->i32
, func
) : ctx
->i32
;
1520 ac_build_type_name_for_intr(type
, type_name
, sizeof(type_name
));
1522 snprintf(name
, sizeof(name
), "llvm.amdgcn.%s.tbuffer.load.%s",
1523 indexing_kind
, type_name
);
1525 return ac_build_intrinsic(ctx
, name
, type
, args
, idx
,
1526 ac_get_load_intr_attribs(can_speculate
));
1530 ac_build_tbuffer_load(struct ac_llvm_context
*ctx
,
1532 LLVMValueRef vindex
,
1533 LLVMValueRef voffset
,
1534 LLVMValueRef soffset
,
1535 LLVMValueRef immoffset
,
1536 unsigned num_channels
,
1542 bool structurized
) /* only matters for LLVM 8+ */
1544 if (HAVE_LLVM
>= 0x800) {
1545 voffset
= LLVMBuildAdd(ctx
->builder
, voffset
, immoffset
, "");
1547 return ac_build_llvm8_tbuffer_load(ctx
, rsrc
, vindex
, voffset
,
1548 soffset
, num_channels
,
1549 dfmt
, nfmt
, glc
, slc
,
1550 can_speculate
, structurized
);
1553 LLVMValueRef args
[] = {
1555 vindex
? vindex
: ctx
->i32_0
,
1559 LLVMConstInt(ctx
->i32
, dfmt
, false),
1560 LLVMConstInt(ctx
->i32
, nfmt
, false),
1561 LLVMConstInt(ctx
->i1
, glc
, false),
1562 LLVMConstInt(ctx
->i1
, slc
, false),
1564 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
1565 LLVMTypeRef types
[] = {ctx
->i32
, ctx
->v2i32
, ctx
->v4i32
};
1566 const char *type_names
[] = {"i32", "v2i32", "v4i32"};
1569 snprintf(name
, sizeof(name
), "llvm.amdgcn.tbuffer.load.%s",
1572 return ac_build_intrinsic(ctx
, name
, types
[func
], args
, 9,
1573 ac_get_load_intr_attribs(can_speculate
));
1577 ac_build_struct_tbuffer_load(struct ac_llvm_context
*ctx
,
1579 LLVMValueRef vindex
,
1580 LLVMValueRef voffset
,
1581 LLVMValueRef soffset
,
1582 LLVMValueRef immoffset
,
1583 unsigned num_channels
,
1590 return ac_build_tbuffer_load(ctx
, rsrc
, vindex
, voffset
, soffset
,
1591 immoffset
, num_channels
, dfmt
, nfmt
, glc
,
1592 slc
, can_speculate
, true);
1596 ac_build_raw_tbuffer_load(struct ac_llvm_context
*ctx
,
1598 LLVMValueRef voffset
,
1599 LLVMValueRef soffset
,
1600 LLVMValueRef immoffset
,
1601 unsigned num_channels
,
1608 return ac_build_tbuffer_load(ctx
, rsrc
, NULL
, voffset
, soffset
,
1609 immoffset
, num_channels
, dfmt
, nfmt
, glc
,
1610 slc
, can_speculate
, false);
1614 ac_build_tbuffer_load_short(struct ac_llvm_context
*ctx
,
1616 LLVMValueRef voffset
,
1617 LLVMValueRef soffset
,
1618 LLVMValueRef immoffset
,
1623 if (HAVE_LLVM
>= 0x900) {
1624 voffset
= LLVMBuildAdd(ctx
->builder
, voffset
, immoffset
, "");
1626 /* LLVM 9+ supports i8/i16 with struct/raw intrinsics. */
1627 res
= ac_build_llvm8_buffer_load_common(ctx
, rsrc
, NULL
,
1629 1, ctx
->i16
, glc
, false,
1630 false, false, false);
1632 unsigned dfmt
= V_008F0C_BUF_DATA_FORMAT_16
;
1633 unsigned nfmt
= V_008F0C_BUF_NUM_FORMAT_UINT
;
1635 res
= ac_build_raw_tbuffer_load(ctx
, rsrc
, voffset
, soffset
,
1636 immoffset
, 1, dfmt
, nfmt
, glc
, false,
1639 res
= LLVMBuildTrunc(ctx
->builder
, res
, ctx
->i16
, "");
1646 ac_build_tbuffer_load_byte(struct ac_llvm_context
*ctx
,
1648 LLVMValueRef voffset
,
1649 LLVMValueRef soffset
,
1650 LLVMValueRef immoffset
,
1655 if (HAVE_LLVM
>= 0x900) {
1656 voffset
= LLVMBuildAdd(ctx
->builder
, voffset
, immoffset
, "");
1658 /* LLVM 9+ supports i8/i16 with struct/raw intrinsics. */
1659 res
= ac_build_llvm8_buffer_load_common(ctx
, rsrc
, NULL
,
1661 1, ctx
->i8
, glc
, false,
1662 false, false, false);
1664 unsigned dfmt
= V_008F0C_BUF_DATA_FORMAT_8
;
1665 unsigned nfmt
= V_008F0C_BUF_NUM_FORMAT_UINT
;
1667 res
= ac_build_raw_tbuffer_load(ctx
, rsrc
, voffset
, soffset
,
1668 immoffset
, 1, dfmt
, nfmt
, glc
, false,
1671 res
= LLVMBuildTrunc(ctx
->builder
, res
, ctx
->i8
, "");
1678 * Convert an 11- or 10-bit unsigned floating point number to an f32.
1680 * The input exponent is expected to be biased analogous to IEEE-754, i.e. by
1681 * 2^(exp_bits-1) - 1 (as defined in OpenGL and other graphics APIs).
1684 ac_ufN_to_float(struct ac_llvm_context
*ctx
, LLVMValueRef src
, unsigned exp_bits
, unsigned mant_bits
)
1686 assert(LLVMTypeOf(src
) == ctx
->i32
);
1689 LLVMValueRef mantissa
;
1690 mantissa
= LLVMBuildAnd(ctx
->builder
, src
, LLVMConstInt(ctx
->i32
, (1 << mant_bits
) - 1, false), "");
1692 /* Converting normal numbers is just a shift + correcting the exponent bias */
1693 unsigned normal_shift
= 23 - mant_bits
;
1694 unsigned bias_shift
= 127 - ((1 << (exp_bits
- 1)) - 1);
1695 LLVMValueRef shifted
, normal
;
1697 shifted
= LLVMBuildShl(ctx
->builder
, src
, LLVMConstInt(ctx
->i32
, normal_shift
, false), "");
1698 normal
= LLVMBuildAdd(ctx
->builder
, shifted
, LLVMConstInt(ctx
->i32
, bias_shift
<< 23, false), "");
1700 /* Converting nan/inf numbers is the same, but with a different exponent update */
1701 LLVMValueRef naninf
;
1702 naninf
= LLVMBuildOr(ctx
->builder
, normal
, LLVMConstInt(ctx
->i32
, 0xff << 23, false), "");
1704 /* Converting denormals is the complex case: determine the leading zeros of the
1705 * mantissa to obtain the correct shift for the mantissa and exponent correction.
1707 LLVMValueRef denormal
;
1708 LLVMValueRef params
[2] = {
1710 ctx
->i1true
, /* result can be undef when arg is 0 */
1712 LLVMValueRef ctlz
= ac_build_intrinsic(ctx
, "llvm.ctlz.i32", ctx
->i32
,
1713 params
, 2, AC_FUNC_ATTR_READNONE
);
1715 /* Shift such that the leading 1 ends up as the LSB of the exponent field. */
1716 tmp
= LLVMBuildSub(ctx
->builder
, ctlz
, LLVMConstInt(ctx
->i32
, 8, false), "");
1717 denormal
= LLVMBuildShl(ctx
->builder
, mantissa
, tmp
, "");
1719 unsigned denormal_exp
= bias_shift
+ (32 - mant_bits
) - 1;
1720 tmp
= LLVMBuildSub(ctx
->builder
, LLVMConstInt(ctx
->i32
, denormal_exp
, false), ctlz
, "");
1721 tmp
= LLVMBuildShl(ctx
->builder
, tmp
, LLVMConstInt(ctx
->i32
, 23, false), "");
1722 denormal
= LLVMBuildAdd(ctx
->builder
, denormal
, tmp
, "");
1724 /* Select the final result. */
1725 LLVMValueRef result
;
1727 tmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntUGE
, src
,
1728 LLVMConstInt(ctx
->i32
, ((1 << exp_bits
) - 1) << mant_bits
, false), "");
1729 result
= LLVMBuildSelect(ctx
->builder
, tmp
, naninf
, normal
, "");
1731 tmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntUGE
, src
,
1732 LLVMConstInt(ctx
->i32
, 1 << mant_bits
, false), "");
1733 result
= LLVMBuildSelect(ctx
->builder
, tmp
, result
, denormal
, "");
1735 tmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
, src
, ctx
->i32_0
, "");
1736 result
= LLVMBuildSelect(ctx
->builder
, tmp
, result
, ctx
->i32_0
, "");
1738 return ac_to_float(ctx
, result
);
1742 * Generate a fully general open coded buffer format fetch with all required
1743 * fixups suitable for vertex fetch, using non-format buffer loads.
1745 * Some combinations of argument values have special interpretations:
1746 * - size = 8 bytes, format = fixed indicates PIPE_FORMAT_R11G11B10_FLOAT
1747 * - size = 8 bytes, format != {float,fixed} indicates a 2_10_10_10 data format
1749 * \param log_size log(size of channel in bytes)
1750 * \param num_channels number of channels (1 to 4)
1751 * \param format AC_FETCH_FORMAT_xxx value
1752 * \param reverse whether XYZ channels are reversed
1753 * \param known_aligned whether the source is known to be aligned to hardware's
1754 * effective element size for loading the given format
1755 * (note: this means dword alignment for 8_8_8_8, 16_16, etc.)
1756 * \param rsrc buffer resource descriptor
1757 * \return the resulting vector of floats or integers bitcast to <4 x i32>
1760 ac_build_opencoded_load_format(struct ac_llvm_context
*ctx
,
1762 unsigned num_channels
,
1767 LLVMValueRef vindex
,
1768 LLVMValueRef voffset
,
1769 LLVMValueRef soffset
,
1775 unsigned load_log_size
= log_size
;
1776 unsigned load_num_channels
= num_channels
;
1777 if (log_size
== 3) {
1779 if (format
== AC_FETCH_FORMAT_FLOAT
) {
1780 load_num_channels
= 2 * num_channels
;
1782 load_num_channels
= 1; /* 10_11_11 or 2_10_10_10 */
1786 int log_recombine
= 0;
1787 if (ctx
->chip_class
== GFX6
&& !known_aligned
) {
1788 /* Avoid alignment restrictions by loading one byte at a time. */
1789 load_num_channels
<<= load_log_size
;
1790 log_recombine
= load_log_size
;
1792 } else if (load_num_channels
== 2 || load_num_channels
== 4) {
1793 log_recombine
= -util_logbase2(load_num_channels
);
1794 load_num_channels
= 1;
1795 load_log_size
+= -log_recombine
;
1798 assert(load_log_size
>= 2 || HAVE_LLVM
>= 0x0900);
1800 LLVMValueRef loads
[32]; /* up to 32 bytes */
1801 for (unsigned i
= 0; i
< load_num_channels
; ++i
) {
1802 tmp
= LLVMBuildAdd(ctx
->builder
, soffset
,
1803 LLVMConstInt(ctx
->i32
, i
<< load_log_size
, false), "");
1804 if (HAVE_LLVM
>= 0x0800) {
1805 LLVMTypeRef channel_type
= load_log_size
== 0 ? ctx
->i8
:
1806 load_log_size
== 1 ? ctx
->i16
: ctx
->i32
;
1807 unsigned num_channels
= 1 << (MAX2(load_log_size
, 2) - 2);
1808 loads
[i
] = ac_build_llvm8_buffer_load_common(
1809 ctx
, rsrc
, vindex
, voffset
, tmp
,
1810 num_channels
, channel_type
, glc
, slc
,
1811 can_speculate
, false, true);
1813 tmp
= LLVMBuildAdd(ctx
->builder
, voffset
, tmp
, "");
1814 loads
[i
] = ac_build_buffer_load_common(
1815 ctx
, rsrc
, vindex
, tmp
,
1816 1 << (load_log_size
- 2), glc
, slc
, can_speculate
, false);
1818 if (load_log_size
>= 2)
1819 loads
[i
] = ac_to_integer(ctx
, loads
[i
]);
1822 if (log_recombine
> 0) {
1823 /* Recombine bytes if necessary (GFX6 only) */
1824 LLVMTypeRef dst_type
= log_recombine
== 2 ? ctx
->i32
: ctx
->i16
;
1826 for (unsigned src
= 0, dst
= 0; src
< load_num_channels
; ++dst
) {
1827 LLVMValueRef accum
= NULL
;
1828 for (unsigned i
= 0; i
< (1 << log_recombine
); ++i
, ++src
) {
1829 tmp
= LLVMBuildZExt(ctx
->builder
, loads
[src
], dst_type
, "");
1833 tmp
= LLVMBuildShl(ctx
->builder
, tmp
,
1834 LLVMConstInt(dst_type
, 8 * i
, false), "");
1835 accum
= LLVMBuildOr(ctx
->builder
, accum
, tmp
, "");
1840 } else if (log_recombine
< 0) {
1841 /* Split vectors of dwords */
1842 if (load_log_size
> 2) {
1843 assert(load_num_channels
== 1);
1844 LLVMValueRef loaded
= loads
[0];
1845 unsigned log_split
= load_log_size
- 2;
1846 log_recombine
+= log_split
;
1847 load_num_channels
= 1 << log_split
;
1849 for (unsigned i
= 0; i
< load_num_channels
; ++i
) {
1850 tmp
= LLVMConstInt(ctx
->i32
, i
, false);
1851 loads
[i
] = LLVMBuildExtractElement(ctx
->builder
, loaded
, tmp
, "");
1855 /* Further split dwords and shorts if required */
1856 if (log_recombine
< 0) {
1857 for (unsigned src
= load_num_channels
,
1858 dst
= load_num_channels
<< -log_recombine
;
1860 unsigned dst_bits
= 1 << (3 + load_log_size
+ log_recombine
);
1861 LLVMTypeRef dst_type
= LLVMIntTypeInContext(ctx
->context
, dst_bits
);
1862 LLVMValueRef loaded
= loads
[src
- 1];
1863 LLVMTypeRef loaded_type
= LLVMTypeOf(loaded
);
1864 for (unsigned i
= 1 << -log_recombine
; i
> 0; --i
, --dst
) {
1865 tmp
= LLVMConstInt(loaded_type
, dst_bits
* (i
- 1), false);
1866 tmp
= LLVMBuildLShr(ctx
->builder
, loaded
, tmp
, "");
1867 loads
[dst
- 1] = LLVMBuildTrunc(ctx
->builder
, tmp
, dst_type
, "");
1873 if (log_size
== 3) {
1874 if (format
== AC_FETCH_FORMAT_FLOAT
) {
1875 for (unsigned i
= 0; i
< num_channels
; ++i
) {
1876 tmp
= ac_build_gather_values(ctx
, &loads
[2 * i
], 2);
1877 loads
[i
] = LLVMBuildBitCast(ctx
->builder
, tmp
, ctx
->f64
, "");
1879 } else if (format
== AC_FETCH_FORMAT_FIXED
) {
1880 /* 10_11_11_FLOAT */
1881 LLVMValueRef data
= loads
[0];
1882 LLVMValueRef i32_2047
= LLVMConstInt(ctx
->i32
, 2047, false);
1883 LLVMValueRef r
= LLVMBuildAnd(ctx
->builder
, data
, i32_2047
, "");
1884 tmp
= LLVMBuildLShr(ctx
->builder
, data
, LLVMConstInt(ctx
->i32
, 11, false), "");
1885 LLVMValueRef g
= LLVMBuildAnd(ctx
->builder
, tmp
, i32_2047
, "");
1886 LLVMValueRef b
= LLVMBuildLShr(ctx
->builder
, data
, LLVMConstInt(ctx
->i32
, 22, false), "");
1888 loads
[0] = ac_to_integer(ctx
, ac_ufN_to_float(ctx
, r
, 5, 6));
1889 loads
[1] = ac_to_integer(ctx
, ac_ufN_to_float(ctx
, g
, 5, 6));
1890 loads
[2] = ac_to_integer(ctx
, ac_ufN_to_float(ctx
, b
, 5, 5));
1894 format
= AC_FETCH_FORMAT_FLOAT
;
1896 /* 2_10_10_10 data formats */
1897 LLVMValueRef data
= loads
[0];
1898 LLVMTypeRef i10
= LLVMIntTypeInContext(ctx
->context
, 10);
1899 LLVMTypeRef i2
= LLVMIntTypeInContext(ctx
->context
, 2);
1900 loads
[0] = LLVMBuildTrunc(ctx
->builder
, data
, i10
, "");
1901 tmp
= LLVMBuildLShr(ctx
->builder
, data
, LLVMConstInt(ctx
->i32
, 10, false), "");
1902 loads
[1] = LLVMBuildTrunc(ctx
->builder
, tmp
, i10
, "");
1903 tmp
= LLVMBuildLShr(ctx
->builder
, data
, LLVMConstInt(ctx
->i32
, 20, false), "");
1904 loads
[2] = LLVMBuildTrunc(ctx
->builder
, tmp
, i10
, "");
1905 tmp
= LLVMBuildLShr(ctx
->builder
, data
, LLVMConstInt(ctx
->i32
, 30, false), "");
1906 loads
[3] = LLVMBuildTrunc(ctx
->builder
, tmp
, i2
, "");
1912 if (format
== AC_FETCH_FORMAT_FLOAT
) {
1913 if (log_size
!= 2) {
1914 for (unsigned chan
= 0; chan
< num_channels
; ++chan
) {
1915 tmp
= ac_to_float(ctx
, loads
[chan
]);
1917 tmp
= LLVMBuildFPTrunc(ctx
->builder
, tmp
, ctx
->f32
, "");
1918 else if (log_size
== 1)
1919 tmp
= LLVMBuildFPExt(ctx
->builder
, tmp
, ctx
->f32
, "");
1920 loads
[chan
] = ac_to_integer(ctx
, tmp
);
1923 } else if (format
== AC_FETCH_FORMAT_UINT
) {
1924 if (log_size
!= 2) {
1925 for (unsigned chan
= 0; chan
< num_channels
; ++chan
)
1926 loads
[chan
] = LLVMBuildZExt(ctx
->builder
, loads
[chan
], ctx
->i32
, "");
1928 } else if (format
== AC_FETCH_FORMAT_SINT
) {
1929 if (log_size
!= 2) {
1930 for (unsigned chan
= 0; chan
< num_channels
; ++chan
)
1931 loads
[chan
] = LLVMBuildSExt(ctx
->builder
, loads
[chan
], ctx
->i32
, "");
1934 bool unsign
= format
== AC_FETCH_FORMAT_UNORM
||
1935 format
== AC_FETCH_FORMAT_USCALED
||
1936 format
== AC_FETCH_FORMAT_UINT
;
1938 for (unsigned chan
= 0; chan
< num_channels
; ++chan
) {
1940 tmp
= LLVMBuildUIToFP(ctx
->builder
, loads
[chan
], ctx
->f32
, "");
1942 tmp
= LLVMBuildSIToFP(ctx
->builder
, loads
[chan
], ctx
->f32
, "");
1945 LLVMValueRef scale
= NULL
;
1946 if (format
== AC_FETCH_FORMAT_FIXED
) {
1947 assert(log_size
== 2);
1948 scale
= LLVMConstReal(ctx
->f32
, 1.0 / 0x10000);
1949 } else if (format
== AC_FETCH_FORMAT_UNORM
) {
1950 unsigned bits
= LLVMGetIntTypeWidth(LLVMTypeOf(loads
[chan
]));
1951 scale
= LLVMConstReal(ctx
->f32
, 1.0 / (((uint64_t)1 << bits
) - 1));
1952 } else if (format
== AC_FETCH_FORMAT_SNORM
) {
1953 unsigned bits
= LLVMGetIntTypeWidth(LLVMTypeOf(loads
[chan
]));
1954 scale
= LLVMConstReal(ctx
->f32
, 1.0 / (((uint64_t)1 << (bits
- 1)) - 1));
1957 tmp
= LLVMBuildFMul(ctx
->builder
, tmp
, scale
, "");
1959 if (format
== AC_FETCH_FORMAT_SNORM
) {
1960 /* Clamp to [-1, 1] */
1961 LLVMValueRef neg_one
= LLVMConstReal(ctx
->f32
, -1.0);
1962 LLVMValueRef clamp
=
1963 LLVMBuildFCmp(ctx
->builder
, LLVMRealULT
, tmp
, neg_one
, "");
1964 tmp
= LLVMBuildSelect(ctx
->builder
, clamp
, neg_one
, tmp
, "");
1967 loads
[chan
] = ac_to_integer(ctx
, tmp
);
1971 while (num_channels
< 4) {
1972 if (format
== AC_FETCH_FORMAT_UINT
|| format
== AC_FETCH_FORMAT_SINT
) {
1973 loads
[num_channels
] = num_channels
== 3 ? ctx
->i32_1
: ctx
->i32_0
;
1975 loads
[num_channels
] = ac_to_integer(ctx
, num_channels
== 3 ? ctx
->f32_1
: ctx
->f32_0
);
1982 loads
[0] = loads
[2];
1986 return ac_build_gather_values(ctx
, loads
, 4);
1990 ac_build_llvm8_tbuffer_store(struct ac_llvm_context
*ctx
,
1993 LLVMValueRef vindex
,
1994 LLVMValueRef voffset
,
1995 LLVMValueRef soffset
,
1996 unsigned num_channels
,
2001 bool writeonly_memory
,
2004 LLVMValueRef args
[7];
2006 args
[idx
++] = vdata
;
2007 args
[idx
++] = LLVMBuildBitCast(ctx
->builder
, rsrc
, ctx
->v4i32
, "");
2009 args
[idx
++] = vindex
? vindex
: ctx
->i32_0
;
2010 args
[idx
++] = voffset
? voffset
: ctx
->i32_0
;
2011 args
[idx
++] = soffset
? soffset
: ctx
->i32_0
;
2012 args
[idx
++] = LLVMConstInt(ctx
->i32
, dfmt
| (nfmt
<< 4), 0);
2013 args
[idx
++] = LLVMConstInt(ctx
->i32
, (glc
? 1 : 0) + (slc
? 2 : 0), 0);
2014 unsigned func
= !ac_has_vec3_support(ctx
->chip_class
, true) && num_channels
== 3 ? 4 : num_channels
;
2015 const char *indexing_kind
= structurized
? "struct" : "raw";
2016 char name
[256], type_name
[8];
2018 LLVMTypeRef type
= func
> 1 ? LLVMVectorType(ctx
->i32
, func
) : ctx
->i32
;
2019 ac_build_type_name_for_intr(type
, type_name
, sizeof(type_name
));
2021 snprintf(name
, sizeof(name
), "llvm.amdgcn.%s.tbuffer.store.%s",
2022 indexing_kind
, type_name
);
2024 ac_build_intrinsic(ctx
, name
, ctx
->voidt
, args
, idx
,
2025 ac_get_store_intr_attribs(writeonly_memory
));
2029 ac_build_tbuffer_store(struct ac_llvm_context
*ctx
,
2032 LLVMValueRef vindex
,
2033 LLVMValueRef voffset
,
2034 LLVMValueRef soffset
,
2035 LLVMValueRef immoffset
,
2036 unsigned num_channels
,
2041 bool writeonly_memory
,
2042 bool structurized
) /* only matters for LLVM 8+ */
2044 if (HAVE_LLVM
>= 0x800) {
2045 voffset
= LLVMBuildAdd(ctx
->builder
,
2046 voffset
? voffset
: ctx
->i32_0
,
2049 ac_build_llvm8_tbuffer_store(ctx
, rsrc
, vdata
, vindex
, voffset
,
2050 soffset
, num_channels
, dfmt
, nfmt
,
2051 glc
, slc
, writeonly_memory
,
2054 LLVMValueRef params
[] = {
2057 vindex
? vindex
: ctx
->i32_0
,
2058 voffset
? voffset
: ctx
->i32_0
,
2059 soffset
? soffset
: ctx
->i32_0
,
2061 LLVMConstInt(ctx
->i32
, dfmt
, false),
2062 LLVMConstInt(ctx
->i32
, nfmt
, false),
2063 LLVMConstInt(ctx
->i1
, glc
, false),
2064 LLVMConstInt(ctx
->i1
, slc
, false),
2066 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
2067 const char *type_names
[] = {"i32", "v2i32", "v4i32"};
2070 snprintf(name
, sizeof(name
), "llvm.amdgcn.tbuffer.store.%s",
2073 ac_build_intrinsic(ctx
, name
, ctx
->voidt
, params
, 10,
2074 ac_get_store_intr_attribs(writeonly_memory
));
2079 ac_build_struct_tbuffer_store(struct ac_llvm_context
*ctx
,
2082 LLVMValueRef vindex
,
2083 LLVMValueRef voffset
,
2084 LLVMValueRef soffset
,
2085 LLVMValueRef immoffset
,
2086 unsigned num_channels
,
2091 bool writeonly_memory
)
2093 ac_build_tbuffer_store(ctx
, rsrc
, vdata
, vindex
, voffset
, soffset
,
2094 immoffset
, num_channels
, dfmt
, nfmt
, glc
, slc
,
2095 writeonly_memory
, true);
2099 ac_build_raw_tbuffer_store(struct ac_llvm_context
*ctx
,
2102 LLVMValueRef voffset
,
2103 LLVMValueRef soffset
,
2104 LLVMValueRef immoffset
,
2105 unsigned num_channels
,
2110 bool writeonly_memory
)
2112 ac_build_tbuffer_store(ctx
, rsrc
, vdata
, NULL
, voffset
, soffset
,
2113 immoffset
, num_channels
, dfmt
, nfmt
, glc
, slc
,
2114 writeonly_memory
, false);
2118 ac_build_tbuffer_store_short(struct ac_llvm_context
*ctx
,
2121 LLVMValueRef voffset
,
2122 LLVMValueRef soffset
,
2124 bool writeonly_memory
)
2126 vdata
= LLVMBuildBitCast(ctx
->builder
, vdata
, ctx
->i16
, "");
2128 if (HAVE_LLVM
>= 0x900) {
2129 /* LLVM 9+ supports i8/i16 with struct/raw intrinsics. */
2130 ac_build_llvm8_buffer_store_common(ctx
, rsrc
, vdata
, NULL
,
2131 voffset
, soffset
, 1,
2132 ctx
->i16
, glc
, false,
2133 writeonly_memory
, false,
2136 unsigned dfmt
= V_008F0C_BUF_DATA_FORMAT_16
;
2137 unsigned nfmt
= V_008F0C_BUF_NUM_FORMAT_UINT
;
2139 vdata
= LLVMBuildZExt(ctx
->builder
, vdata
, ctx
->i32
, "");
2141 ac_build_raw_tbuffer_store(ctx
, rsrc
, vdata
, voffset
, soffset
,
2142 ctx
->i32_0
, 1, dfmt
, nfmt
, glc
, false,
2148 ac_build_tbuffer_store_byte(struct ac_llvm_context
*ctx
,
2151 LLVMValueRef voffset
,
2152 LLVMValueRef soffset
,
2154 bool writeonly_memory
)
2156 vdata
= LLVMBuildBitCast(ctx
->builder
, vdata
, ctx
->i8
, "");
2158 if (HAVE_LLVM
>= 0x900) {
2159 /* LLVM 9+ supports i8/i16 with struct/raw intrinsics. */
2160 ac_build_llvm8_buffer_store_common(ctx
, rsrc
, vdata
, NULL
,
2161 voffset
, soffset
, 1,
2162 ctx
->i8
, glc
, false,
2163 writeonly_memory
, false,
2166 unsigned dfmt
= V_008F0C_BUF_DATA_FORMAT_8
;
2167 unsigned nfmt
= V_008F0C_BUF_NUM_FORMAT_UINT
;
2169 vdata
= LLVMBuildZExt(ctx
->builder
, vdata
, ctx
->i32
, "");
2171 ac_build_raw_tbuffer_store(ctx
, rsrc
, vdata
, voffset
, soffset
,
2172 ctx
->i32_0
, 1, dfmt
, nfmt
, glc
, false,
2177 * Set range metadata on an instruction. This can only be used on load and
2178 * call instructions. If you know an instruction can only produce the values
2179 * 0, 1, 2, you would do set_range_metadata(value, 0, 3);
2180 * \p lo is the minimum value inclusive.
2181 * \p hi is the maximum value exclusive.
2183 static void set_range_metadata(struct ac_llvm_context
*ctx
,
2184 LLVMValueRef value
, unsigned lo
, unsigned hi
)
2186 LLVMValueRef range_md
, md_args
[2];
2187 LLVMTypeRef type
= LLVMTypeOf(value
);
2188 LLVMContextRef context
= LLVMGetTypeContext(type
);
2190 md_args
[0] = LLVMConstInt(type
, lo
, false);
2191 md_args
[1] = LLVMConstInt(type
, hi
, false);
2192 range_md
= LLVMMDNodeInContext(context
, md_args
, 2);
2193 LLVMSetMetadata(value
, ctx
->range_md_kind
, range_md
);
2197 ac_get_thread_id(struct ac_llvm_context
*ctx
)
2201 LLVMValueRef tid_args
[2];
2202 tid_args
[0] = LLVMConstInt(ctx
->i32
, 0xffffffff, false);
2203 tid_args
[1] = ctx
->i32_0
;
2204 tid_args
[1] = ac_build_intrinsic(ctx
,
2205 "llvm.amdgcn.mbcnt.lo", ctx
->i32
,
2206 tid_args
, 2, AC_FUNC_ATTR_READNONE
);
2208 tid
= ac_build_intrinsic(ctx
, "llvm.amdgcn.mbcnt.hi",
2210 2, AC_FUNC_ATTR_READNONE
);
2211 set_range_metadata(ctx
, tid
, 0, 64);
2216 * AMD GCN implements derivatives using the local data store (LDS)
2217 * All writes to the LDS happen in all executing threads at
2218 * the same time. TID is the Thread ID for the current
2219 * thread and is a value between 0 and 63, representing
2220 * the thread's position in the wavefront.
2222 * For the pixel shader threads are grouped into quads of four pixels.
2223 * The TIDs of the pixels of a quad are:
2231 * So, masking the TID with 0xfffffffc yields the TID of the top left pixel
2232 * of the quad, masking with 0xfffffffd yields the TID of the top pixel of
2233 * the current pixel's column, and masking with 0xfffffffe yields the TID
2234 * of the left pixel of the current pixel's row.
2236 * Adding 1 yields the TID of the pixel to the right of the left pixel, and
2237 * adding 2 yields the TID of the pixel below the top pixel.
2240 ac_build_ddxy(struct ac_llvm_context
*ctx
,
2245 unsigned tl_lanes
[4], trbl_lanes
[4];
2246 char name
[32], type
[8];
2247 LLVMValueRef tl
, trbl
;
2248 LLVMTypeRef result_type
;
2249 LLVMValueRef result
;
2251 result_type
= ac_to_float_type(ctx
, LLVMTypeOf(val
));
2253 if (result_type
== ctx
->f16
)
2254 val
= LLVMBuildZExt(ctx
->builder
, val
, ctx
->i32
, "");
2256 for (unsigned i
= 0; i
< 4; ++i
) {
2257 tl_lanes
[i
] = i
& mask
;
2258 trbl_lanes
[i
] = (i
& mask
) + idx
;
2261 tl
= ac_build_quad_swizzle(ctx
, val
,
2262 tl_lanes
[0], tl_lanes
[1],
2263 tl_lanes
[2], tl_lanes
[3]);
2264 trbl
= ac_build_quad_swizzle(ctx
, val
,
2265 trbl_lanes
[0], trbl_lanes
[1],
2266 trbl_lanes
[2], trbl_lanes
[3]);
2268 if (result_type
== ctx
->f16
) {
2269 tl
= LLVMBuildTrunc(ctx
->builder
, tl
, ctx
->i16
, "");
2270 trbl
= LLVMBuildTrunc(ctx
->builder
, trbl
, ctx
->i16
, "");
2273 tl
= LLVMBuildBitCast(ctx
->builder
, tl
, result_type
, "");
2274 trbl
= LLVMBuildBitCast(ctx
->builder
, trbl
, result_type
, "");
2275 result
= LLVMBuildFSub(ctx
->builder
, trbl
, tl
, "");
2277 ac_build_type_name_for_intr(result_type
, type
, sizeof(type
));
2278 snprintf(name
, sizeof(name
), "llvm.amdgcn.wqm.%s", type
);
2280 return ac_build_intrinsic(ctx
, name
, result_type
, &result
, 1, 0);
2284 ac_build_sendmsg(struct ac_llvm_context
*ctx
,
2286 LLVMValueRef wave_id
)
2288 LLVMValueRef args
[2];
2289 args
[0] = LLVMConstInt(ctx
->i32
, msg
, false);
2291 ac_build_intrinsic(ctx
, "llvm.amdgcn.s.sendmsg", ctx
->voidt
, args
, 2, 0);
2295 ac_build_imsb(struct ac_llvm_context
*ctx
,
2297 LLVMTypeRef dst_type
)
2299 LLVMValueRef msb
= ac_build_intrinsic(ctx
, "llvm.amdgcn.sffbh.i32",
2301 AC_FUNC_ATTR_READNONE
);
2303 /* The HW returns the last bit index from MSB, but NIR/TGSI wants
2304 * the index from LSB. Invert it by doing "31 - msb". */
2305 msb
= LLVMBuildSub(ctx
->builder
, LLVMConstInt(ctx
->i32
, 31, false),
2308 LLVMValueRef all_ones
= LLVMConstInt(ctx
->i32
, -1, true);
2309 LLVMValueRef cond
= LLVMBuildOr(ctx
->builder
,
2310 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
2311 arg
, ctx
->i32_0
, ""),
2312 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
,
2313 arg
, all_ones
, ""), "");
2315 return LLVMBuildSelect(ctx
->builder
, cond
, all_ones
, msb
, "");
2319 ac_build_umsb(struct ac_llvm_context
*ctx
,
2321 LLVMTypeRef dst_type
)
2323 const char *intrin_name
;
2325 LLVMValueRef highest_bit
;
2329 bitsize
= ac_get_elem_bits(ctx
, LLVMTypeOf(arg
));
2332 intrin_name
= "llvm.ctlz.i64";
2334 highest_bit
= LLVMConstInt(ctx
->i64
, 63, false);
2338 intrin_name
= "llvm.ctlz.i32";
2340 highest_bit
= LLVMConstInt(ctx
->i32
, 31, false);
2344 intrin_name
= "llvm.ctlz.i16";
2346 highest_bit
= LLVMConstInt(ctx
->i16
, 15, false);
2350 intrin_name
= "llvm.ctlz.i8";
2352 highest_bit
= LLVMConstInt(ctx
->i8
, 7, false);
2356 unreachable(!"invalid bitsize");
2360 LLVMValueRef params
[2] = {
2365 LLVMValueRef msb
= ac_build_intrinsic(ctx
, intrin_name
, type
,
2367 AC_FUNC_ATTR_READNONE
);
2369 /* The HW returns the last bit index from MSB, but TGSI/NIR wants
2370 * the index from LSB. Invert it by doing "31 - msb". */
2371 msb
= LLVMBuildSub(ctx
->builder
, highest_bit
, msb
, "");
2373 if (bitsize
== 64) {
2374 msb
= LLVMBuildTrunc(ctx
->builder
, msb
, ctx
->i32
, "");
2375 } else if (bitsize
< 32) {
2376 msb
= LLVMBuildSExt(ctx
->builder
, msb
, ctx
->i32
, "");
2379 /* check for zero */
2380 return LLVMBuildSelect(ctx
->builder
,
2381 LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, arg
, zero
, ""),
2382 LLVMConstInt(ctx
->i32
, -1, true), msb
, "");
2385 LLVMValueRef
ac_build_fmin(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
2389 snprintf(name
, sizeof(name
), "llvm.minnum.f%d", ac_get_elem_bits(ctx
, LLVMTypeOf(a
)));
2390 LLVMValueRef args
[2] = {a
, b
};
2391 return ac_build_intrinsic(ctx
, name
, LLVMTypeOf(a
), args
, 2,
2392 AC_FUNC_ATTR_READNONE
);
2395 LLVMValueRef
ac_build_fmax(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
2399 snprintf(name
, sizeof(name
), "llvm.maxnum.f%d", ac_get_elem_bits(ctx
, LLVMTypeOf(a
)));
2400 LLVMValueRef args
[2] = {a
, b
};
2401 return ac_build_intrinsic(ctx
, name
, LLVMTypeOf(a
), args
, 2,
2402 AC_FUNC_ATTR_READNONE
);
2405 LLVMValueRef
ac_build_imin(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
2408 LLVMValueRef cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSLE
, a
, b
, "");
2409 return LLVMBuildSelect(ctx
->builder
, cmp
, a
, b
, "");
2412 LLVMValueRef
ac_build_imax(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
2415 LLVMValueRef cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGT
, a
, b
, "");
2416 return LLVMBuildSelect(ctx
->builder
, cmp
, a
, b
, "");
2419 LLVMValueRef
ac_build_umin(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
2422 LLVMValueRef cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntULE
, a
, b
, "");
2423 return LLVMBuildSelect(ctx
->builder
, cmp
, a
, b
, "");
2426 LLVMValueRef
ac_build_umax(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
2429 LLVMValueRef cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntUGE
, a
, b
, "");
2430 return LLVMBuildSelect(ctx
->builder
, cmp
, a
, b
, "");
2433 LLVMValueRef
ac_build_clamp(struct ac_llvm_context
*ctx
, LLVMValueRef value
)
2435 LLVMTypeRef t
= LLVMTypeOf(value
);
2436 return ac_build_fmin(ctx
, ac_build_fmax(ctx
, value
, LLVMConstReal(t
, 0.0)),
2437 LLVMConstReal(t
, 1.0));
2440 void ac_build_export(struct ac_llvm_context
*ctx
, struct ac_export_args
*a
)
2442 LLVMValueRef args
[9];
2444 args
[0] = LLVMConstInt(ctx
->i32
, a
->target
, 0);
2445 args
[1] = LLVMConstInt(ctx
->i32
, a
->enabled_channels
, 0);
2448 LLVMTypeRef i16
= LLVMInt16TypeInContext(ctx
->context
);
2449 LLVMTypeRef v2i16
= LLVMVectorType(i16
, 2);
2451 args
[2] = LLVMBuildBitCast(ctx
->builder
, a
->out
[0],
2453 args
[3] = LLVMBuildBitCast(ctx
->builder
, a
->out
[1],
2455 args
[4] = LLVMConstInt(ctx
->i1
, a
->done
, 0);
2456 args
[5] = LLVMConstInt(ctx
->i1
, a
->valid_mask
, 0);
2458 ac_build_intrinsic(ctx
, "llvm.amdgcn.exp.compr.v2i16",
2459 ctx
->voidt
, args
, 6, 0);
2461 args
[2] = a
->out
[0];
2462 args
[3] = a
->out
[1];
2463 args
[4] = a
->out
[2];
2464 args
[5] = a
->out
[3];
2465 args
[6] = LLVMConstInt(ctx
->i1
, a
->done
, 0);
2466 args
[7] = LLVMConstInt(ctx
->i1
, a
->valid_mask
, 0);
2468 ac_build_intrinsic(ctx
, "llvm.amdgcn.exp.f32",
2469 ctx
->voidt
, args
, 8, 0);
2473 void ac_build_export_null(struct ac_llvm_context
*ctx
)
2475 struct ac_export_args args
;
2477 args
.enabled_channels
= 0x0; /* enabled channels */
2478 args
.valid_mask
= 1; /* whether the EXEC mask is valid */
2479 args
.done
= 1; /* DONE bit */
2480 args
.target
= V_008DFC_SQ_EXP_NULL
;
2481 args
.compr
= 0; /* COMPR flag (0 = 32-bit export) */
2482 args
.out
[0] = LLVMGetUndef(ctx
->f32
); /* R */
2483 args
.out
[1] = LLVMGetUndef(ctx
->f32
); /* G */
2484 args
.out
[2] = LLVMGetUndef(ctx
->f32
); /* B */
2485 args
.out
[3] = LLVMGetUndef(ctx
->f32
); /* A */
2487 ac_build_export(ctx
, &args
);
2490 static unsigned ac_num_coords(enum ac_image_dim dim
)
2496 case ac_image_1darray
:
2500 case ac_image_2darray
:
2501 case ac_image_2dmsaa
:
2503 case ac_image_2darraymsaa
:
2506 unreachable("ac_num_coords: bad dim");
2510 static unsigned ac_num_derivs(enum ac_image_dim dim
)
2514 case ac_image_1darray
:
2517 case ac_image_2darray
:
2522 case ac_image_2dmsaa
:
2523 case ac_image_2darraymsaa
:
2525 unreachable("derivatives not supported");
2529 static const char *get_atomic_name(enum ac_atomic_op op
)
2532 case ac_atomic_swap
: return "swap";
2533 case ac_atomic_add
: return "add";
2534 case ac_atomic_sub
: return "sub";
2535 case ac_atomic_smin
: return "smin";
2536 case ac_atomic_umin
: return "umin";
2537 case ac_atomic_smax
: return "smax";
2538 case ac_atomic_umax
: return "umax";
2539 case ac_atomic_and
: return "and";
2540 case ac_atomic_or
: return "or";
2541 case ac_atomic_xor
: return "xor";
2543 unreachable("bad atomic op");
2546 LLVMValueRef
ac_build_image_opcode(struct ac_llvm_context
*ctx
,
2547 struct ac_image_args
*a
)
2549 const char *overload
[3] = { "", "", "" };
2550 unsigned num_overloads
= 0;
2551 LLVMValueRef args
[18];
2552 unsigned num_args
= 0;
2553 enum ac_image_dim dim
= a
->dim
;
2555 assert(!a
->lod
|| a
->lod
== ctx
->i32_0
|| a
->lod
== ctx
->f32_0
||
2557 assert((a
->opcode
!= ac_image_get_resinfo
&& a
->opcode
!= ac_image_load_mip
&&
2558 a
->opcode
!= ac_image_store_mip
) ||
2560 assert(a
->opcode
== ac_image_sample
|| a
->opcode
== ac_image_gather4
||
2561 (!a
->compare
&& !a
->offset
));
2562 assert((a
->opcode
== ac_image_sample
|| a
->opcode
== ac_image_gather4
||
2563 a
->opcode
== ac_image_get_lod
) ||
2565 assert((a
->bias
? 1 : 0) +
2567 (a
->level_zero
? 1 : 0) +
2568 (a
->derivs
[0] ? 1 : 0) <= 1);
2570 if (a
->opcode
== ac_image_get_lod
) {
2572 case ac_image_1darray
:
2575 case ac_image_2darray
:
2584 bool sample
= a
->opcode
== ac_image_sample
||
2585 a
->opcode
== ac_image_gather4
||
2586 a
->opcode
== ac_image_get_lod
;
2587 bool atomic
= a
->opcode
== ac_image_atomic
||
2588 a
->opcode
== ac_image_atomic_cmpswap
;
2589 LLVMTypeRef coord_type
= sample
? ctx
->f32
: ctx
->i32
;
2591 if (atomic
|| a
->opcode
== ac_image_store
|| a
->opcode
== ac_image_store_mip
) {
2592 args
[num_args
++] = a
->data
[0];
2593 if (a
->opcode
== ac_image_atomic_cmpswap
)
2594 args
[num_args
++] = a
->data
[1];
2598 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->dmask
, false);
2601 args
[num_args
++] = ac_to_integer(ctx
, a
->offset
);
2603 args
[num_args
++] = ac_to_float(ctx
, a
->bias
);
2604 overload
[num_overloads
++] = ".f32";
2607 args
[num_args
++] = ac_to_float(ctx
, a
->compare
);
2609 unsigned count
= ac_num_derivs(dim
);
2610 for (unsigned i
= 0; i
< count
; ++i
)
2611 args
[num_args
++] = ac_to_float(ctx
, a
->derivs
[i
]);
2612 overload
[num_overloads
++] = ".f32";
2614 unsigned num_coords
=
2615 a
->opcode
!= ac_image_get_resinfo
? ac_num_coords(dim
) : 0;
2616 for (unsigned i
= 0; i
< num_coords
; ++i
)
2617 args
[num_args
++] = LLVMBuildBitCast(ctx
->builder
, a
->coords
[i
], coord_type
, "");
2619 args
[num_args
++] = LLVMBuildBitCast(ctx
->builder
, a
->lod
, coord_type
, "");
2620 overload
[num_overloads
++] = sample
? ".f32" : ".i32";
2622 args
[num_args
++] = a
->resource
;
2624 args
[num_args
++] = a
->sampler
;
2625 args
[num_args
++] = LLVMConstInt(ctx
->i1
, a
->unorm
, false);
2628 args
[num_args
++] = ctx
->i32_0
; /* texfailctrl */
2629 args
[num_args
++] = LLVMConstInt(ctx
->i32
, a
->cache_policy
, false);
2632 const char *atomic_subop
= "";
2633 switch (a
->opcode
) {
2634 case ac_image_sample
: name
= "sample"; break;
2635 case ac_image_gather4
: name
= "gather4"; break;
2636 case ac_image_load
: name
= "load"; break;
2637 case ac_image_load_mip
: name
= "load.mip"; break;
2638 case ac_image_store
: name
= "store"; break;
2639 case ac_image_store_mip
: name
= "store.mip"; break;
2640 case ac_image_atomic
:
2642 atomic_subop
= get_atomic_name(a
->atomic
);
2644 case ac_image_atomic_cmpswap
:
2646 atomic_subop
= "cmpswap";
2648 case ac_image_get_lod
: name
= "getlod"; break;
2649 case ac_image_get_resinfo
: name
= "getresinfo"; break;
2650 default: unreachable("invalid image opcode");
2653 const char *dimname
;
2655 case ac_image_1d
: dimname
= "1d"; break;
2656 case ac_image_2d
: dimname
= "2d"; break;
2657 case ac_image_3d
: dimname
= "3d"; break;
2658 case ac_image_cube
: dimname
= "cube"; break;
2659 case ac_image_1darray
: dimname
= "1darray"; break;
2660 case ac_image_2darray
: dimname
= "2darray"; break;
2661 case ac_image_2dmsaa
: dimname
= "2dmsaa"; break;
2662 case ac_image_2darraymsaa
: dimname
= "2darraymsaa"; break;
2663 default: unreachable("invalid dim");
2667 a
->lod
&& (a
->opcode
== ac_image_sample
|| a
->opcode
== ac_image_gather4
);
2669 snprintf(intr_name
, sizeof(intr_name
),
2670 "llvm.amdgcn.image.%s%s" /* base name */
2671 "%s%s%s" /* sample/gather modifiers */
2672 ".%s.%s%s%s%s", /* dimension and type overloads */
2674 a
->compare
? ".c" : "",
2677 a
->derivs
[0] ? ".d" :
2678 a
->level_zero
? ".lz" : "",
2679 a
->offset
? ".o" : "",
2681 atomic
? "i32" : "v4f32",
2682 overload
[0], overload
[1], overload
[2]);
2687 else if (a
->opcode
== ac_image_store
|| a
->opcode
== ac_image_store_mip
)
2692 LLVMValueRef result
=
2693 ac_build_intrinsic(ctx
, intr_name
, retty
, args
, num_args
,
2695 if (!sample
&& retty
== ctx
->v4f32
) {
2696 result
= LLVMBuildBitCast(ctx
->builder
, result
,
2702 LLVMValueRef
ac_build_cvt_pkrtz_f16(struct ac_llvm_context
*ctx
,
2703 LLVMValueRef args
[2])
2706 LLVMVectorType(LLVMHalfTypeInContext(ctx
->context
), 2);
2708 return ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pkrtz", v2f16
,
2709 args
, 2, AC_FUNC_ATTR_READNONE
);
2712 LLVMValueRef
ac_build_cvt_pknorm_i16(struct ac_llvm_context
*ctx
,
2713 LLVMValueRef args
[2])
2716 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pknorm.i16",
2717 ctx
->v2i16
, args
, 2,
2718 AC_FUNC_ATTR_READNONE
);
2719 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
2722 LLVMValueRef
ac_build_cvt_pknorm_u16(struct ac_llvm_context
*ctx
,
2723 LLVMValueRef args
[2])
2726 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pknorm.u16",
2727 ctx
->v2i16
, args
, 2,
2728 AC_FUNC_ATTR_READNONE
);
2729 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
2732 /* The 8-bit and 10-bit clamping is for HW workarounds. */
2733 LLVMValueRef
ac_build_cvt_pk_i16(struct ac_llvm_context
*ctx
,
2734 LLVMValueRef args
[2], unsigned bits
, bool hi
)
2736 assert(bits
== 8 || bits
== 10 || bits
== 16);
2738 LLVMValueRef max_rgb
= LLVMConstInt(ctx
->i32
,
2739 bits
== 8 ? 127 : bits
== 10 ? 511 : 32767, 0);
2740 LLVMValueRef min_rgb
= LLVMConstInt(ctx
->i32
,
2741 bits
== 8 ? -128 : bits
== 10 ? -512 : -32768, 0);
2742 LLVMValueRef max_alpha
=
2743 bits
!= 10 ? max_rgb
: ctx
->i32_1
;
2744 LLVMValueRef min_alpha
=
2745 bits
!= 10 ? min_rgb
: LLVMConstInt(ctx
->i32
, -2, 0);
2749 for (int i
= 0; i
< 2; i
++) {
2750 bool alpha
= hi
&& i
== 1;
2751 args
[i
] = ac_build_imin(ctx
, args
[i
],
2752 alpha
? max_alpha
: max_rgb
);
2753 args
[i
] = ac_build_imax(ctx
, args
[i
],
2754 alpha
? min_alpha
: min_rgb
);
2759 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pk.i16",
2760 ctx
->v2i16
, args
, 2,
2761 AC_FUNC_ATTR_READNONE
);
2762 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
2765 /* The 8-bit and 10-bit clamping is for HW workarounds. */
2766 LLVMValueRef
ac_build_cvt_pk_u16(struct ac_llvm_context
*ctx
,
2767 LLVMValueRef args
[2], unsigned bits
, bool hi
)
2769 assert(bits
== 8 || bits
== 10 || bits
== 16);
2771 LLVMValueRef max_rgb
= LLVMConstInt(ctx
->i32
,
2772 bits
== 8 ? 255 : bits
== 10 ? 1023 : 65535, 0);
2773 LLVMValueRef max_alpha
=
2774 bits
!= 10 ? max_rgb
: LLVMConstInt(ctx
->i32
, 3, 0);
2778 for (int i
= 0; i
< 2; i
++) {
2779 bool alpha
= hi
&& i
== 1;
2780 args
[i
] = ac_build_umin(ctx
, args
[i
],
2781 alpha
? max_alpha
: max_rgb
);
2786 ac_build_intrinsic(ctx
, "llvm.amdgcn.cvt.pk.u16",
2787 ctx
->v2i16
, args
, 2,
2788 AC_FUNC_ATTR_READNONE
);
2789 return LLVMBuildBitCast(ctx
->builder
, res
, ctx
->i32
, "");
2792 LLVMValueRef
ac_build_wqm_vote(struct ac_llvm_context
*ctx
, LLVMValueRef i1
)
2794 return ac_build_intrinsic(ctx
, "llvm.amdgcn.wqm.vote", ctx
->i1
,
2795 &i1
, 1, AC_FUNC_ATTR_READNONE
);
2798 void ac_build_kill_if_false(struct ac_llvm_context
*ctx
, LLVMValueRef i1
)
2800 ac_build_intrinsic(ctx
, "llvm.amdgcn.kill", ctx
->voidt
,
2804 LLVMValueRef
ac_build_bfe(struct ac_llvm_context
*ctx
, LLVMValueRef input
,
2805 LLVMValueRef offset
, LLVMValueRef width
,
2808 LLVMValueRef args
[] = {
2814 return ac_build_intrinsic(ctx
,
2815 is_signed
? "llvm.amdgcn.sbfe.i32" :
2816 "llvm.amdgcn.ubfe.i32",
2818 AC_FUNC_ATTR_READNONE
);
2821 LLVMValueRef
ac_build_imad(struct ac_llvm_context
*ctx
, LLVMValueRef s0
,
2822 LLVMValueRef s1
, LLVMValueRef s2
)
2824 return LLVMBuildAdd(ctx
->builder
,
2825 LLVMBuildMul(ctx
->builder
, s0
, s1
, ""), s2
, "");
2828 LLVMValueRef
ac_build_fmad(struct ac_llvm_context
*ctx
, LLVMValueRef s0
,
2829 LLVMValueRef s1
, LLVMValueRef s2
)
2831 return LLVMBuildFAdd(ctx
->builder
,
2832 LLVMBuildFMul(ctx
->builder
, s0
, s1
, ""), s2
, "");
2835 void ac_build_waitcnt(struct ac_llvm_context
*ctx
, unsigned simm16
)
2837 LLVMValueRef args
[1] = {
2838 LLVMConstInt(ctx
->i32
, simm16
, false),
2840 ac_build_intrinsic(ctx
, "llvm.amdgcn.s.waitcnt",
2841 ctx
->voidt
, args
, 1, 0);
2844 LLVMValueRef
ac_build_fmed3(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
2845 LLVMValueRef src1
, LLVMValueRef src2
,
2851 if (bitsize
== 16) {
2852 intr
= "llvm.amdgcn.fmed3.f16";
2854 } else if (bitsize
== 32) {
2855 intr
= "llvm.amdgcn.fmed3.f32";
2858 intr
= "llvm.amdgcn.fmed3.f64";
2862 LLVMValueRef params
[] = {
2867 return ac_build_intrinsic(ctx
, intr
, type
, params
, 3,
2868 AC_FUNC_ATTR_READNONE
);
2871 LLVMValueRef
ac_build_fract(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
2877 if (bitsize
== 16) {
2878 intr
= "llvm.amdgcn.fract.f16";
2880 } else if (bitsize
== 32) {
2881 intr
= "llvm.amdgcn.fract.f32";
2884 intr
= "llvm.amdgcn.fract.f64";
2888 LLVMValueRef params
[] = {
2891 return ac_build_intrinsic(ctx
, intr
, type
, params
, 1,
2892 AC_FUNC_ATTR_READNONE
);
2895 LLVMValueRef
ac_build_isign(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
2898 LLVMTypeRef type
= LLVMIntTypeInContext(ctx
->context
, bitsize
);
2899 LLVMValueRef zero
= LLVMConstInt(type
, 0, false);
2900 LLVMValueRef one
= LLVMConstInt(type
, 1, false);
2902 LLVMValueRef cmp
, val
;
2903 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGT
, src0
, zero
, "");
2904 val
= LLVMBuildSelect(ctx
->builder
, cmp
, one
, src0
, "");
2905 cmp
= LLVMBuildICmp(ctx
->builder
, LLVMIntSGE
, val
, zero
, "");
2906 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstInt(type
, -1, true), "");
2910 LLVMValueRef
ac_build_fsign(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
2913 LLVMValueRef cmp
, val
, zero
, one
;
2916 if (bitsize
== 16) {
2920 } else if (bitsize
== 32) {
2930 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGT
, src0
, zero
, "");
2931 val
= LLVMBuildSelect(ctx
->builder
, cmp
, one
, src0
, "");
2932 cmp
= LLVMBuildFCmp(ctx
->builder
, LLVMRealOGE
, val
, zero
, "");
2933 val
= LLVMBuildSelect(ctx
->builder
, cmp
, val
, LLVMConstReal(type
, -1.0), "");
2937 LLVMValueRef
ac_build_bit_count(struct ac_llvm_context
*ctx
, LLVMValueRef src0
)
2939 LLVMValueRef result
;
2942 bitsize
= ac_get_elem_bits(ctx
, LLVMTypeOf(src0
));
2946 result
= ac_build_intrinsic(ctx
, "llvm.ctpop.i64", ctx
->i64
,
2947 (LLVMValueRef
[]) { src0
}, 1,
2948 AC_FUNC_ATTR_READNONE
);
2950 result
= LLVMBuildTrunc(ctx
->builder
, result
, ctx
->i32
, "");
2953 result
= ac_build_intrinsic(ctx
, "llvm.ctpop.i32", ctx
->i32
,
2954 (LLVMValueRef
[]) { src0
}, 1,
2955 AC_FUNC_ATTR_READNONE
);
2958 result
= ac_build_intrinsic(ctx
, "llvm.ctpop.i16", ctx
->i16
,
2959 (LLVMValueRef
[]) { src0
}, 1,
2960 AC_FUNC_ATTR_READNONE
);
2962 result
= LLVMBuildZExt(ctx
->builder
, result
, ctx
->i32
, "");
2965 result
= ac_build_intrinsic(ctx
, "llvm.ctpop.i8", ctx
->i8
,
2966 (LLVMValueRef
[]) { src0
}, 1,
2967 AC_FUNC_ATTR_READNONE
);
2969 result
= LLVMBuildZExt(ctx
->builder
, result
, ctx
->i32
, "");
2972 unreachable(!"invalid bitsize");
2979 LLVMValueRef
ac_build_bitfield_reverse(struct ac_llvm_context
*ctx
,
2982 LLVMValueRef result
;
2985 bitsize
= ac_get_elem_bits(ctx
, LLVMTypeOf(src0
));
2989 result
= ac_build_intrinsic(ctx
, "llvm.bitreverse.i64", ctx
->i64
,
2990 (LLVMValueRef
[]) { src0
}, 1,
2991 AC_FUNC_ATTR_READNONE
);
2993 result
= LLVMBuildTrunc(ctx
->builder
, result
, ctx
->i32
, "");
2996 result
= ac_build_intrinsic(ctx
, "llvm.bitreverse.i32", ctx
->i32
,
2997 (LLVMValueRef
[]) { src0
}, 1,
2998 AC_FUNC_ATTR_READNONE
);
3001 result
= ac_build_intrinsic(ctx
, "llvm.bitreverse.i16", ctx
->i16
,
3002 (LLVMValueRef
[]) { src0
}, 1,
3003 AC_FUNC_ATTR_READNONE
);
3005 result
= LLVMBuildZExt(ctx
->builder
, result
, ctx
->i32
, "");
3008 result
= ac_build_intrinsic(ctx
, "llvm.bitreverse.i8", ctx
->i8
,
3009 (LLVMValueRef
[]) { src0
}, 1,
3010 AC_FUNC_ATTR_READNONE
);
3012 result
= LLVMBuildZExt(ctx
->builder
, result
, ctx
->i32
, "");
3015 unreachable(!"invalid bitsize");
3022 #define AC_EXP_TARGET 0
3023 #define AC_EXP_ENABLED_CHANNELS 1
3024 #define AC_EXP_OUT0 2
3032 struct ac_vs_exp_chan
3036 enum ac_ir_type type
;
3039 struct ac_vs_exp_inst
{
3042 struct ac_vs_exp_chan chan
[4];
3045 struct ac_vs_exports
{
3047 struct ac_vs_exp_inst exp
[VARYING_SLOT_MAX
];
3050 /* Return true if the PARAM export has been eliminated. */
3051 static bool ac_eliminate_const_output(uint8_t *vs_output_param_offset
,
3052 uint32_t num_outputs
,
3053 struct ac_vs_exp_inst
*exp
)
3055 unsigned i
, default_val
; /* SPI_PS_INPUT_CNTL_i.DEFAULT_VAL */
3056 bool is_zero
[4] = {}, is_one
[4] = {};
3058 for (i
= 0; i
< 4; i
++) {
3059 /* It's a constant expression. Undef outputs are eliminated too. */
3060 if (exp
->chan
[i
].type
== AC_IR_UNDEF
) {
3063 } else if (exp
->chan
[i
].type
== AC_IR_CONST
) {
3064 if (exp
->chan
[i
].const_float
== 0)
3066 else if (exp
->chan
[i
].const_float
== 1)
3069 return false; /* other constant */
3074 /* Only certain combinations of 0 and 1 can be eliminated. */
3075 if (is_zero
[0] && is_zero
[1] && is_zero
[2])
3076 default_val
= is_zero
[3] ? 0 : 1;
3077 else if (is_one
[0] && is_one
[1] && is_one
[2])
3078 default_val
= is_zero
[3] ? 2 : 3;
3082 /* The PARAM export can be represented as DEFAULT_VAL. Kill it. */
3083 LLVMInstructionEraseFromParent(exp
->inst
);
3085 /* Change OFFSET to DEFAULT_VAL. */
3086 for (i
= 0; i
< num_outputs
; i
++) {
3087 if (vs_output_param_offset
[i
] == exp
->offset
) {
3088 vs_output_param_offset
[i
] =
3089 AC_EXP_PARAM_DEFAULT_VAL_0000
+ default_val
;
3096 static bool ac_eliminate_duplicated_output(struct ac_llvm_context
*ctx
,
3097 uint8_t *vs_output_param_offset
,
3098 uint32_t num_outputs
,
3099 struct ac_vs_exports
*processed
,
3100 struct ac_vs_exp_inst
*exp
)
3102 unsigned p
, copy_back_channels
= 0;
3104 /* See if the output is already in the list of processed outputs.
3105 * The LLVMValueRef comparison relies on SSA.
3107 for (p
= 0; p
< processed
->num
; p
++) {
3108 bool different
= false;
3110 for (unsigned j
= 0; j
< 4; j
++) {
3111 struct ac_vs_exp_chan
*c1
= &processed
->exp
[p
].chan
[j
];
3112 struct ac_vs_exp_chan
*c2
= &exp
->chan
[j
];
3114 /* Treat undef as a match. */
3115 if (c2
->type
== AC_IR_UNDEF
)
3118 /* If c1 is undef but c2 isn't, we can copy c2 to c1
3119 * and consider the instruction duplicated.
3121 if (c1
->type
== AC_IR_UNDEF
) {
3122 copy_back_channels
|= 1 << j
;
3126 /* Test whether the channels are not equal. */
3127 if (c1
->type
!= c2
->type
||
3128 (c1
->type
== AC_IR_CONST
&&
3129 c1
->const_float
!= c2
->const_float
) ||
3130 (c1
->type
== AC_IR_VALUE
&&
3131 c1
->value
!= c2
->value
)) {
3139 copy_back_channels
= 0;
3141 if (p
== processed
->num
)
3144 /* If a match was found, but the matching export has undef where the new
3145 * one has a normal value, copy the normal value to the undef channel.
3147 struct ac_vs_exp_inst
*match
= &processed
->exp
[p
];
3149 /* Get current enabled channels mask. */
3150 LLVMValueRef arg
= LLVMGetOperand(match
->inst
, AC_EXP_ENABLED_CHANNELS
);
3151 unsigned enabled_channels
= LLVMConstIntGetZExtValue(arg
);
3153 while (copy_back_channels
) {
3154 unsigned chan
= u_bit_scan(©_back_channels
);
3156 assert(match
->chan
[chan
].type
== AC_IR_UNDEF
);
3157 LLVMSetOperand(match
->inst
, AC_EXP_OUT0
+ chan
,
3158 exp
->chan
[chan
].value
);
3159 match
->chan
[chan
] = exp
->chan
[chan
];
3161 /* Update number of enabled channels because the original mask
3162 * is not always 0xf.
3164 enabled_channels
|= (1 << chan
);
3165 LLVMSetOperand(match
->inst
, AC_EXP_ENABLED_CHANNELS
,
3166 LLVMConstInt(ctx
->i32
, enabled_channels
, 0));
3169 /* The PARAM export is duplicated. Kill it. */
3170 LLVMInstructionEraseFromParent(exp
->inst
);
3172 /* Change OFFSET to the matching export. */
3173 for (unsigned i
= 0; i
< num_outputs
; i
++) {
3174 if (vs_output_param_offset
[i
] == exp
->offset
) {
3175 vs_output_param_offset
[i
] = match
->offset
;
3182 void ac_optimize_vs_outputs(struct ac_llvm_context
*ctx
,
3183 LLVMValueRef main_fn
,
3184 uint8_t *vs_output_param_offset
,
3185 uint32_t num_outputs
,
3186 uint8_t *num_param_exports
)
3188 LLVMBasicBlockRef bb
;
3189 bool removed_any
= false;
3190 struct ac_vs_exports exports
;
3194 /* Process all LLVM instructions. */
3195 bb
= LLVMGetFirstBasicBlock(main_fn
);
3197 LLVMValueRef inst
= LLVMGetFirstInstruction(bb
);
3200 LLVMValueRef cur
= inst
;
3201 inst
= LLVMGetNextInstruction(inst
);
3202 struct ac_vs_exp_inst exp
;
3204 if (LLVMGetInstructionOpcode(cur
) != LLVMCall
)
3207 LLVMValueRef callee
= ac_llvm_get_called_value(cur
);
3209 if (!ac_llvm_is_function(callee
))
3212 const char *name
= LLVMGetValueName(callee
);
3213 unsigned num_args
= LLVMCountParams(callee
);
3215 /* Check if this is an export instruction. */
3216 if ((num_args
!= 9 && num_args
!= 8) ||
3217 (strcmp(name
, "llvm.SI.export") &&
3218 strcmp(name
, "llvm.amdgcn.exp.f32")))
3221 LLVMValueRef arg
= LLVMGetOperand(cur
, AC_EXP_TARGET
);
3222 unsigned target
= LLVMConstIntGetZExtValue(arg
);
3224 if (target
< V_008DFC_SQ_EXP_PARAM
)
3227 target
-= V_008DFC_SQ_EXP_PARAM
;
3229 /* Parse the instruction. */
3230 memset(&exp
, 0, sizeof(exp
));
3231 exp
.offset
= target
;
3234 for (unsigned i
= 0; i
< 4; i
++) {
3235 LLVMValueRef v
= LLVMGetOperand(cur
, AC_EXP_OUT0
+ i
);
3237 exp
.chan
[i
].value
= v
;
3239 if (LLVMIsUndef(v
)) {
3240 exp
.chan
[i
].type
= AC_IR_UNDEF
;
3241 } else if (LLVMIsAConstantFP(v
)) {
3242 LLVMBool loses_info
;
3243 exp
.chan
[i
].type
= AC_IR_CONST
;
3244 exp
.chan
[i
].const_float
=
3245 LLVMConstRealGetDouble(v
, &loses_info
);
3247 exp
.chan
[i
].type
= AC_IR_VALUE
;
3251 /* Eliminate constant and duplicated PARAM exports. */
3252 if (ac_eliminate_const_output(vs_output_param_offset
,
3253 num_outputs
, &exp
) ||
3254 ac_eliminate_duplicated_output(ctx
,
3255 vs_output_param_offset
,
3256 num_outputs
, &exports
,
3260 exports
.exp
[exports
.num
++] = exp
;
3263 bb
= LLVMGetNextBasicBlock(bb
);
3266 /* Remove holes in export memory due to removed PARAM exports.
3267 * This is done by renumbering all PARAM exports.
3270 uint8_t old_offset
[VARYING_SLOT_MAX
];
3273 /* Make a copy of the offsets. We need the old version while
3274 * we are modifying some of them. */
3275 memcpy(old_offset
, vs_output_param_offset
,
3276 sizeof(old_offset
));
3278 for (i
= 0; i
< exports
.num
; i
++) {
3279 unsigned offset
= exports
.exp
[i
].offset
;
3281 /* Update vs_output_param_offset. Multiple outputs can
3282 * have the same offset.
3284 for (out
= 0; out
< num_outputs
; out
++) {
3285 if (old_offset
[out
] == offset
)
3286 vs_output_param_offset
[out
] = i
;
3289 /* Change the PARAM offset in the instruction. */
3290 LLVMSetOperand(exports
.exp
[i
].inst
, AC_EXP_TARGET
,
3291 LLVMConstInt(ctx
->i32
,
3292 V_008DFC_SQ_EXP_PARAM
+ i
, 0));
3294 *num_param_exports
= exports
.num
;
3298 void ac_init_exec_full_mask(struct ac_llvm_context
*ctx
)
3300 LLVMValueRef full_mask
= LLVMConstInt(ctx
->i64
, ~0ull, 0);
3301 ac_build_intrinsic(ctx
,
3302 "llvm.amdgcn.init.exec", ctx
->voidt
,
3303 &full_mask
, 1, AC_FUNC_ATTR_CONVERGENT
);
3306 void ac_declare_lds_as_pointer(struct ac_llvm_context
*ctx
)
3308 unsigned lds_size
= ctx
->chip_class
>= GFX7
? 65536 : 32768;
3309 ctx
->lds
= LLVMBuildIntToPtr(ctx
->builder
, ctx
->i32_0
,
3310 LLVMPointerType(LLVMArrayType(ctx
->i32
, lds_size
/ 4), AC_ADDR_SPACE_LDS
),
3314 LLVMValueRef
ac_lds_load(struct ac_llvm_context
*ctx
,
3315 LLVMValueRef dw_addr
)
3317 return LLVMBuildLoad(ctx
->builder
, ac_build_gep0(ctx
, ctx
->lds
, dw_addr
), "");
3320 void ac_lds_store(struct ac_llvm_context
*ctx
,
3321 LLVMValueRef dw_addr
,
3324 value
= ac_to_integer(ctx
, value
);
3325 ac_build_indexed_store(ctx
, ctx
->lds
,
3329 LLVMValueRef
ac_find_lsb(struct ac_llvm_context
*ctx
,
3330 LLVMTypeRef dst_type
,
3333 unsigned src0_bitsize
= ac_get_elem_bits(ctx
, LLVMTypeOf(src0
));
3334 const char *intrin_name
;
3338 switch (src0_bitsize
) {
3340 intrin_name
= "llvm.cttz.i64";
3345 intrin_name
= "llvm.cttz.i32";
3350 intrin_name
= "llvm.cttz.i16";
3355 intrin_name
= "llvm.cttz.i8";
3360 unreachable(!"invalid bitsize");
3363 LLVMValueRef params
[2] = {
3366 /* The value of 1 means that ffs(x=0) = undef, so LLVM won't
3367 * add special code to check for x=0. The reason is that
3368 * the LLVM behavior for x=0 is different from what we
3369 * need here. However, LLVM also assumes that ffs(x) is
3370 * in [0, 31], but GLSL expects that ffs(0) = -1, so
3371 * a conditional assignment to handle 0 is still required.
3373 * The hardware already implements the correct behavior.
3378 LLVMValueRef lsb
= ac_build_intrinsic(ctx
, intrin_name
, type
,
3380 AC_FUNC_ATTR_READNONE
);
3382 if (src0_bitsize
== 64) {
3383 lsb
= LLVMBuildTrunc(ctx
->builder
, lsb
, ctx
->i32
, "");
3384 } else if (src0_bitsize
< 32) {
3385 lsb
= LLVMBuildSExt(ctx
->builder
, lsb
, ctx
->i32
, "");
3388 /* TODO: We need an intrinsic to skip this conditional. */
3389 /* Check for zero: */
3390 return LLVMBuildSelect(ctx
->builder
, LLVMBuildICmp(ctx
->builder
,
3393 LLVMConstInt(ctx
->i32
, -1, 0), lsb
, "");
3396 LLVMTypeRef
ac_array_in_const_addr_space(LLVMTypeRef elem_type
)
3398 return LLVMPointerType(elem_type
, AC_ADDR_SPACE_CONST
);
3401 LLVMTypeRef
ac_array_in_const32_addr_space(LLVMTypeRef elem_type
)
3403 return LLVMPointerType(elem_type
, AC_ADDR_SPACE_CONST_32BIT
);
3406 static struct ac_llvm_flow
*
3407 get_current_flow(struct ac_llvm_context
*ctx
)
3409 if (ctx
->flow_depth
> 0)
3410 return &ctx
->flow
[ctx
->flow_depth
- 1];
3414 static struct ac_llvm_flow
*
3415 get_innermost_loop(struct ac_llvm_context
*ctx
)
3417 for (unsigned i
= ctx
->flow_depth
; i
> 0; --i
) {
3418 if (ctx
->flow
[i
- 1].loop_entry_block
)
3419 return &ctx
->flow
[i
- 1];
3424 static struct ac_llvm_flow
*
3425 push_flow(struct ac_llvm_context
*ctx
)
3427 struct ac_llvm_flow
*flow
;
3429 if (ctx
->flow_depth
>= ctx
->flow_depth_max
) {
3430 unsigned new_max
= MAX2(ctx
->flow_depth
<< 1,
3431 AC_LLVM_INITIAL_CF_DEPTH
);
3433 ctx
->flow
= realloc(ctx
->flow
, new_max
* sizeof(*ctx
->flow
));
3434 ctx
->flow_depth_max
= new_max
;
3437 flow
= &ctx
->flow
[ctx
->flow_depth
];
3440 flow
->next_block
= NULL
;
3441 flow
->loop_entry_block
= NULL
;
3445 static void set_basicblock_name(LLVMBasicBlockRef bb
, const char *base
,
3449 snprintf(buf
, sizeof(buf
), "%s%d", base
, label_id
);
3450 LLVMSetValueName(LLVMBasicBlockAsValue(bb
), buf
);
3453 /* Append a basic block at the level of the parent flow.
3455 static LLVMBasicBlockRef
append_basic_block(struct ac_llvm_context
*ctx
,
3458 assert(ctx
->flow_depth
>= 1);
3460 if (ctx
->flow_depth
>= 2) {
3461 struct ac_llvm_flow
*flow
= &ctx
->flow
[ctx
->flow_depth
- 2];
3463 return LLVMInsertBasicBlockInContext(ctx
->context
,
3464 flow
->next_block
, name
);
3467 LLVMValueRef main_fn
=
3468 LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
->builder
));
3469 return LLVMAppendBasicBlockInContext(ctx
->context
, main_fn
, name
);
3472 /* Emit a branch to the given default target for the current block if
3473 * applicable -- that is, if the current block does not already contain a
3474 * branch from a break or continue.
3476 static void emit_default_branch(LLVMBuilderRef builder
,
3477 LLVMBasicBlockRef target
)
3479 if (!LLVMGetBasicBlockTerminator(LLVMGetInsertBlock(builder
)))
3480 LLVMBuildBr(builder
, target
);
3483 void ac_build_bgnloop(struct ac_llvm_context
*ctx
, int label_id
)
3485 struct ac_llvm_flow
*flow
= push_flow(ctx
);
3486 flow
->loop_entry_block
= append_basic_block(ctx
, "LOOP");
3487 flow
->next_block
= append_basic_block(ctx
, "ENDLOOP");
3488 set_basicblock_name(flow
->loop_entry_block
, "loop", label_id
);
3489 LLVMBuildBr(ctx
->builder
, flow
->loop_entry_block
);
3490 LLVMPositionBuilderAtEnd(ctx
->builder
, flow
->loop_entry_block
);
3493 void ac_build_break(struct ac_llvm_context
*ctx
)
3495 struct ac_llvm_flow
*flow
= get_innermost_loop(ctx
);
3496 LLVMBuildBr(ctx
->builder
, flow
->next_block
);
3499 void ac_build_continue(struct ac_llvm_context
*ctx
)
3501 struct ac_llvm_flow
*flow
= get_innermost_loop(ctx
);
3502 LLVMBuildBr(ctx
->builder
, flow
->loop_entry_block
);
3505 void ac_build_else(struct ac_llvm_context
*ctx
, int label_id
)
3507 struct ac_llvm_flow
*current_branch
= get_current_flow(ctx
);
3508 LLVMBasicBlockRef endif_block
;
3510 assert(!current_branch
->loop_entry_block
);
3512 endif_block
= append_basic_block(ctx
, "ENDIF");
3513 emit_default_branch(ctx
->builder
, endif_block
);
3515 LLVMPositionBuilderAtEnd(ctx
->builder
, current_branch
->next_block
);
3516 set_basicblock_name(current_branch
->next_block
, "else", label_id
);
3518 current_branch
->next_block
= endif_block
;
3521 void ac_build_endif(struct ac_llvm_context
*ctx
, int label_id
)
3523 struct ac_llvm_flow
*current_branch
= get_current_flow(ctx
);
3525 assert(!current_branch
->loop_entry_block
);
3527 emit_default_branch(ctx
->builder
, current_branch
->next_block
);
3528 LLVMPositionBuilderAtEnd(ctx
->builder
, current_branch
->next_block
);
3529 set_basicblock_name(current_branch
->next_block
, "endif", label_id
);
3534 void ac_build_endloop(struct ac_llvm_context
*ctx
, int label_id
)
3536 struct ac_llvm_flow
*current_loop
= get_current_flow(ctx
);
3538 assert(current_loop
->loop_entry_block
);
3540 emit_default_branch(ctx
->builder
, current_loop
->loop_entry_block
);
3542 LLVMPositionBuilderAtEnd(ctx
->builder
, current_loop
->next_block
);
3543 set_basicblock_name(current_loop
->next_block
, "endloop", label_id
);
3547 void ac_build_ifcc(struct ac_llvm_context
*ctx
, LLVMValueRef cond
, int label_id
)
3549 struct ac_llvm_flow
*flow
= push_flow(ctx
);
3550 LLVMBasicBlockRef if_block
;
3552 if_block
= append_basic_block(ctx
, "IF");
3553 flow
->next_block
= append_basic_block(ctx
, "ELSE");
3554 set_basicblock_name(if_block
, "if", label_id
);
3555 LLVMBuildCondBr(ctx
->builder
, cond
, if_block
, flow
->next_block
);
3556 LLVMPositionBuilderAtEnd(ctx
->builder
, if_block
);
3559 void ac_build_if(struct ac_llvm_context
*ctx
, LLVMValueRef value
,
3562 LLVMValueRef cond
= LLVMBuildFCmp(ctx
->builder
, LLVMRealUNE
,
3563 value
, ctx
->f32_0
, "");
3564 ac_build_ifcc(ctx
, cond
, label_id
);
3567 void ac_build_uif(struct ac_llvm_context
*ctx
, LLVMValueRef value
,
3570 LLVMValueRef cond
= LLVMBuildICmp(ctx
->builder
, LLVMIntNE
,
3571 ac_to_integer(ctx
, value
),
3573 ac_build_ifcc(ctx
, cond
, label_id
);
3576 LLVMValueRef
ac_build_alloca_undef(struct ac_llvm_context
*ac
, LLVMTypeRef type
,
3579 LLVMBuilderRef builder
= ac
->builder
;
3580 LLVMBasicBlockRef current_block
= LLVMGetInsertBlock(builder
);
3581 LLVMValueRef function
= LLVMGetBasicBlockParent(current_block
);
3582 LLVMBasicBlockRef first_block
= LLVMGetEntryBasicBlock(function
);
3583 LLVMValueRef first_instr
= LLVMGetFirstInstruction(first_block
);
3584 LLVMBuilderRef first_builder
= LLVMCreateBuilderInContext(ac
->context
);
3588 LLVMPositionBuilderBefore(first_builder
, first_instr
);
3590 LLVMPositionBuilderAtEnd(first_builder
, first_block
);
3593 res
= LLVMBuildAlloca(first_builder
, type
, name
);
3594 LLVMDisposeBuilder(first_builder
);
3598 LLVMValueRef
ac_build_alloca(struct ac_llvm_context
*ac
,
3599 LLVMTypeRef type
, const char *name
)
3601 LLVMValueRef ptr
= ac_build_alloca_undef(ac
, type
, name
);
3602 LLVMBuildStore(ac
->builder
, LLVMConstNull(type
), ptr
);
3606 LLVMValueRef
ac_cast_ptr(struct ac_llvm_context
*ctx
, LLVMValueRef ptr
,
3609 int addr_space
= LLVMGetPointerAddressSpace(LLVMTypeOf(ptr
));
3610 return LLVMBuildBitCast(ctx
->builder
, ptr
,
3611 LLVMPointerType(type
, addr_space
), "");
3614 LLVMValueRef
ac_trim_vector(struct ac_llvm_context
*ctx
, LLVMValueRef value
,
3617 unsigned num_components
= ac_get_llvm_num_components(value
);
3618 if (count
== num_components
)
3621 LLVMValueRef masks
[MAX2(count
, 2)];
3622 masks
[0] = ctx
->i32_0
;
3623 masks
[1] = ctx
->i32_1
;
3624 for (unsigned i
= 2; i
< count
; i
++)
3625 masks
[i
] = LLVMConstInt(ctx
->i32
, i
, false);
3628 return LLVMBuildExtractElement(ctx
->builder
, value
, masks
[0],
3631 LLVMValueRef swizzle
= LLVMConstVector(masks
, count
);
3632 return LLVMBuildShuffleVector(ctx
->builder
, value
, value
, swizzle
, "");
3635 LLVMValueRef
ac_unpack_param(struct ac_llvm_context
*ctx
, LLVMValueRef param
,
3636 unsigned rshift
, unsigned bitwidth
)
3638 LLVMValueRef value
= param
;
3640 value
= LLVMBuildLShr(ctx
->builder
, value
,
3641 LLVMConstInt(ctx
->i32
, rshift
, false), "");
3643 if (rshift
+ bitwidth
< 32) {
3644 unsigned mask
= (1 << bitwidth
) - 1;
3645 value
= LLVMBuildAnd(ctx
->builder
, value
,
3646 LLVMConstInt(ctx
->i32
, mask
, false), "");
3651 /* Adjust the sample index according to FMASK.
3653 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
3654 * which is the identity mapping. Each nibble says which physical sample
3655 * should be fetched to get that sample.
3657 * For example, 0x11111100 means there are only 2 samples stored and
3658 * the second sample covers 3/4 of the pixel. When reading samples 0
3659 * and 1, return physical sample 0 (determined by the first two 0s
3660 * in FMASK), otherwise return physical sample 1.
3662 * The sample index should be adjusted as follows:
3663 * addr[sample_index] = (fmask >> (addr[sample_index] * 4)) & 0xF;
3665 void ac_apply_fmask_to_sample(struct ac_llvm_context
*ac
, LLVMValueRef fmask
,
3666 LLVMValueRef
*addr
, bool is_array_tex
)
3668 struct ac_image_args fmask_load
= {};
3669 fmask_load
.opcode
= ac_image_load
;
3670 fmask_load
.resource
= fmask
;
3671 fmask_load
.dmask
= 0xf;
3672 fmask_load
.dim
= is_array_tex
? ac_image_2darray
: ac_image_2d
;
3673 fmask_load
.attributes
= AC_FUNC_ATTR_READNONE
;
3675 fmask_load
.coords
[0] = addr
[0];
3676 fmask_load
.coords
[1] = addr
[1];
3678 fmask_load
.coords
[2] = addr
[2];
3680 LLVMValueRef fmask_value
= ac_build_image_opcode(ac
, &fmask_load
);
3681 fmask_value
= LLVMBuildExtractElement(ac
->builder
, fmask_value
,
3684 /* Apply the formula. */
3685 unsigned sample_chan
= is_array_tex
? 3 : 2;
3686 LLVMValueRef final_sample
;
3687 final_sample
= LLVMBuildMul(ac
->builder
, addr
[sample_chan
],
3688 LLVMConstInt(ac
->i32
, 4, 0), "");
3689 final_sample
= LLVMBuildLShr(ac
->builder
, fmask_value
, final_sample
, "");
3690 /* Mask the sample index by 0x7, because 0x8 means an unknown value
3691 * with EQAA, so those will map to 0. */
3692 final_sample
= LLVMBuildAnd(ac
->builder
, final_sample
,
3693 LLVMConstInt(ac
->i32
, 0x7, 0), "");
3695 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
3696 * resource descriptor is 0 (invalid).
3699 tmp
= LLVMBuildBitCast(ac
->builder
, fmask
, ac
->v8i32
, "");
3700 tmp
= LLVMBuildExtractElement(ac
->builder
, tmp
, ac
->i32_1
, "");
3701 tmp
= LLVMBuildICmp(ac
->builder
, LLVMIntNE
, tmp
, ac
->i32_0
, "");
3703 /* Replace the MSAA sample index. */
3704 addr
[sample_chan
] = LLVMBuildSelect(ac
->builder
, tmp
, final_sample
,
3705 addr
[sample_chan
], "");
3709 _ac_build_readlane(struct ac_llvm_context
*ctx
, LLVMValueRef src
, LLVMValueRef lane
)
3711 ac_build_optimization_barrier(ctx
, &src
);
3712 return ac_build_intrinsic(ctx
,
3713 lane
== NULL
? "llvm.amdgcn.readfirstlane" : "llvm.amdgcn.readlane",
3714 LLVMTypeOf(src
), (LLVMValueRef
[]) {
3716 lane
== NULL
? 1 : 2,
3717 AC_FUNC_ATTR_READNONE
|
3718 AC_FUNC_ATTR_CONVERGENT
);
3722 * Builds the "llvm.amdgcn.readlane" or "llvm.amdgcn.readfirstlane" intrinsic.
3725 * @param lane - id of the lane or NULL for the first active lane
3726 * @return value of the lane
3729 ac_build_readlane(struct ac_llvm_context
*ctx
, LLVMValueRef src
, LLVMValueRef lane
)
3731 LLVMTypeRef src_type
= LLVMTypeOf(src
);
3732 src
= ac_to_integer(ctx
, src
);
3733 unsigned bits
= LLVMGetIntTypeWidth(LLVMTypeOf(src
));
3737 ret
= _ac_build_readlane(ctx
, src
, lane
);
3739 assert(bits
% 32 == 0);
3740 LLVMTypeRef vec_type
= LLVMVectorType(ctx
->i32
, bits
/ 32);
3741 LLVMValueRef src_vector
=
3742 LLVMBuildBitCast(ctx
->builder
, src
, vec_type
, "");
3743 ret
= LLVMGetUndef(vec_type
);
3744 for (unsigned i
= 0; i
< bits
/ 32; i
++) {
3745 src
= LLVMBuildExtractElement(ctx
->builder
, src_vector
,
3746 LLVMConstInt(ctx
->i32
, i
, 0), "");
3747 LLVMValueRef ret_comp
= _ac_build_readlane(ctx
, src
, lane
);
3748 ret
= LLVMBuildInsertElement(ctx
->builder
, ret
, ret_comp
,
3749 LLVMConstInt(ctx
->i32
, i
, 0), "");
3752 return LLVMBuildBitCast(ctx
->builder
, ret
, src_type
, "");
3756 ac_build_writelane(struct ac_llvm_context
*ctx
, LLVMValueRef src
, LLVMValueRef value
, LLVMValueRef lane
)
3758 /* TODO: Use the actual instruction when LLVM adds an intrinsic for it.
3760 LLVMValueRef pred
= LLVMBuildICmp(ctx
->builder
, LLVMIntEQ
, lane
,
3761 ac_get_thread_id(ctx
), "");
3762 return LLVMBuildSelect(ctx
->builder
, pred
, value
, src
, "");
3766 ac_build_mbcnt(struct ac_llvm_context
*ctx
, LLVMValueRef mask
)
3768 LLVMValueRef mask_vec
= LLVMBuildBitCast(ctx
->builder
, mask
,
3769 LLVMVectorType(ctx
->i32
, 2),
3771 LLVMValueRef mask_lo
= LLVMBuildExtractElement(ctx
->builder
, mask_vec
,
3773 LLVMValueRef mask_hi
= LLVMBuildExtractElement(ctx
->builder
, mask_vec
,
3776 ac_build_intrinsic(ctx
, "llvm.amdgcn.mbcnt.lo", ctx
->i32
,
3777 (LLVMValueRef
[]) { mask_lo
, ctx
->i32_0
},
3778 2, AC_FUNC_ATTR_READNONE
);
3779 val
= ac_build_intrinsic(ctx
, "llvm.amdgcn.mbcnt.hi", ctx
->i32
,
3780 (LLVMValueRef
[]) { mask_hi
, val
},
3781 2, AC_FUNC_ATTR_READNONE
);
3786 _dpp_quad_perm
= 0x000,
3787 _dpp_row_sl
= 0x100,
3788 _dpp_row_sr
= 0x110,
3789 _dpp_row_rr
= 0x120,
3794 dpp_row_mirror
= 0x140,
3795 dpp_row_half_mirror
= 0x141,
3796 dpp_row_bcast15
= 0x142,
3797 dpp_row_bcast31
= 0x143
3800 static inline enum dpp_ctrl
3801 dpp_quad_perm(unsigned lane0
, unsigned lane1
, unsigned lane2
, unsigned lane3
)
3803 assert(lane0
< 4 && lane1
< 4 && lane2
< 4 && lane3
< 4);
3804 return _dpp_quad_perm
| lane0
| (lane1
<< 2) | (lane2
<< 4) | (lane3
<< 6);
3807 static inline enum dpp_ctrl
3808 dpp_row_sl(unsigned amount
)
3810 assert(amount
> 0 && amount
< 16);
3811 return _dpp_row_sl
| amount
;
3814 static inline enum dpp_ctrl
3815 dpp_row_sr(unsigned amount
)
3817 assert(amount
> 0 && amount
< 16);
3818 return _dpp_row_sr
| amount
;
3822 _ac_build_dpp(struct ac_llvm_context
*ctx
, LLVMValueRef old
, LLVMValueRef src
,
3823 enum dpp_ctrl dpp_ctrl
, unsigned row_mask
, unsigned bank_mask
,
3826 return ac_build_intrinsic(ctx
, "llvm.amdgcn.update.dpp.i32",
3830 LLVMConstInt(ctx
->i32
, dpp_ctrl
, 0),
3831 LLVMConstInt(ctx
->i32
, row_mask
, 0),
3832 LLVMConstInt(ctx
->i32
, bank_mask
, 0),
3833 LLVMConstInt(ctx
->i1
, bound_ctrl
, 0) },
3834 6, AC_FUNC_ATTR_READNONE
| AC_FUNC_ATTR_CONVERGENT
);
3838 ac_build_dpp(struct ac_llvm_context
*ctx
, LLVMValueRef old
, LLVMValueRef src
,
3839 enum dpp_ctrl dpp_ctrl
, unsigned row_mask
, unsigned bank_mask
,
3842 LLVMTypeRef src_type
= LLVMTypeOf(src
);
3843 src
= ac_to_integer(ctx
, src
);
3844 old
= ac_to_integer(ctx
, old
);
3845 unsigned bits
= LLVMGetIntTypeWidth(LLVMTypeOf(src
));
3848 ret
= _ac_build_dpp(ctx
, old
, src
, dpp_ctrl
, row_mask
,
3849 bank_mask
, bound_ctrl
);
3851 assert(bits
% 32 == 0);
3852 LLVMTypeRef vec_type
= LLVMVectorType(ctx
->i32
, bits
/ 32);
3853 LLVMValueRef src_vector
=
3854 LLVMBuildBitCast(ctx
->builder
, src
, vec_type
, "");
3855 LLVMValueRef old_vector
=
3856 LLVMBuildBitCast(ctx
->builder
, old
, vec_type
, "");
3857 ret
= LLVMGetUndef(vec_type
);
3858 for (unsigned i
= 0; i
< bits
/ 32; i
++) {
3859 src
= LLVMBuildExtractElement(ctx
->builder
, src_vector
,
3860 LLVMConstInt(ctx
->i32
, i
,
3862 old
= LLVMBuildExtractElement(ctx
->builder
, old_vector
,
3863 LLVMConstInt(ctx
->i32
, i
,
3865 LLVMValueRef ret_comp
= _ac_build_dpp(ctx
, old
, src
,
3870 ret
= LLVMBuildInsertElement(ctx
->builder
, ret
,
3872 LLVMConstInt(ctx
->i32
, i
,
3876 return LLVMBuildBitCast(ctx
->builder
, ret
, src_type
, "");
3879 static inline unsigned
3880 ds_pattern_bitmode(unsigned and_mask
, unsigned or_mask
, unsigned xor_mask
)
3882 assert(and_mask
< 32 && or_mask
< 32 && xor_mask
< 32);
3883 return and_mask
| (or_mask
<< 5) | (xor_mask
<< 10);
3887 _ac_build_ds_swizzle(struct ac_llvm_context
*ctx
, LLVMValueRef src
, unsigned mask
)
3889 return ac_build_intrinsic(ctx
, "llvm.amdgcn.ds.swizzle",
3890 LLVMTypeOf(src
), (LLVMValueRef
[]) {
3891 src
, LLVMConstInt(ctx
->i32
, mask
, 0) },
3892 2, AC_FUNC_ATTR_READNONE
| AC_FUNC_ATTR_CONVERGENT
);
3896 ac_build_ds_swizzle(struct ac_llvm_context
*ctx
, LLVMValueRef src
, unsigned mask
)
3898 LLVMTypeRef src_type
= LLVMTypeOf(src
);
3899 src
= ac_to_integer(ctx
, src
);
3900 unsigned bits
= LLVMGetIntTypeWidth(LLVMTypeOf(src
));
3903 ret
= _ac_build_ds_swizzle(ctx
, src
, mask
);
3905 assert(bits
% 32 == 0);
3906 LLVMTypeRef vec_type
= LLVMVectorType(ctx
->i32
, bits
/ 32);
3907 LLVMValueRef src_vector
=
3908 LLVMBuildBitCast(ctx
->builder
, src
, vec_type
, "");
3909 ret
= LLVMGetUndef(vec_type
);
3910 for (unsigned i
= 0; i
< bits
/ 32; i
++) {
3911 src
= LLVMBuildExtractElement(ctx
->builder
, src_vector
,
3912 LLVMConstInt(ctx
->i32
, i
,
3914 LLVMValueRef ret_comp
= _ac_build_ds_swizzle(ctx
, src
,
3916 ret
= LLVMBuildInsertElement(ctx
->builder
, ret
,
3918 LLVMConstInt(ctx
->i32
, i
,
3922 return LLVMBuildBitCast(ctx
->builder
, ret
, src_type
, "");
3926 ac_build_wwm(struct ac_llvm_context
*ctx
, LLVMValueRef src
)
3928 char name
[32], type
[8];
3929 ac_build_type_name_for_intr(LLVMTypeOf(src
), type
, sizeof(type
));
3930 snprintf(name
, sizeof(name
), "llvm.amdgcn.wwm.%s", type
);
3931 return ac_build_intrinsic(ctx
, name
, LLVMTypeOf(src
),
3932 (LLVMValueRef
[]) { src
}, 1,
3933 AC_FUNC_ATTR_READNONE
);
3937 ac_build_set_inactive(struct ac_llvm_context
*ctx
, LLVMValueRef src
,
3938 LLVMValueRef inactive
)
3940 char name
[33], type
[8];
3941 LLVMTypeRef src_type
= LLVMTypeOf(src
);
3942 src
= ac_to_integer(ctx
, src
);
3943 inactive
= ac_to_integer(ctx
, inactive
);
3944 ac_build_type_name_for_intr(LLVMTypeOf(src
), type
, sizeof(type
));
3945 snprintf(name
, sizeof(name
), "llvm.amdgcn.set.inactive.%s", type
);
3947 ac_build_intrinsic(ctx
, name
,
3948 LLVMTypeOf(src
), (LLVMValueRef
[]) {
3950 AC_FUNC_ATTR_READNONE
|
3951 AC_FUNC_ATTR_CONVERGENT
);
3952 return LLVMBuildBitCast(ctx
->builder
, ret
, src_type
, "");
3956 get_reduction_identity(struct ac_llvm_context
*ctx
, nir_op op
, unsigned type_size
)
3958 if (type_size
== 4) {
3960 case nir_op_iadd
: return ctx
->i32_0
;
3961 case nir_op_fadd
: return ctx
->f32_0
;
3962 case nir_op_imul
: return ctx
->i32_1
;
3963 case nir_op_fmul
: return ctx
->f32_1
;
3964 case nir_op_imin
: return LLVMConstInt(ctx
->i32
, INT32_MAX
, 0);
3965 case nir_op_umin
: return LLVMConstInt(ctx
->i32
, UINT32_MAX
, 0);
3966 case nir_op_fmin
: return LLVMConstReal(ctx
->f32
, INFINITY
);
3967 case nir_op_imax
: return LLVMConstInt(ctx
->i32
, INT32_MIN
, 0);
3968 case nir_op_umax
: return ctx
->i32_0
;
3969 case nir_op_fmax
: return LLVMConstReal(ctx
->f32
, -INFINITY
);
3970 case nir_op_iand
: return LLVMConstInt(ctx
->i32
, -1, 0);
3971 case nir_op_ior
: return ctx
->i32_0
;
3972 case nir_op_ixor
: return ctx
->i32_0
;
3974 unreachable("bad reduction intrinsic");
3976 } else { /* type_size == 64bit */
3978 case nir_op_iadd
: return ctx
->i64_0
;
3979 case nir_op_fadd
: return ctx
->f64_0
;
3980 case nir_op_imul
: return ctx
->i64_1
;
3981 case nir_op_fmul
: return ctx
->f64_1
;
3982 case nir_op_imin
: return LLVMConstInt(ctx
->i64
, INT64_MAX
, 0);
3983 case nir_op_umin
: return LLVMConstInt(ctx
->i64
, UINT64_MAX
, 0);
3984 case nir_op_fmin
: return LLVMConstReal(ctx
->f64
, INFINITY
);
3985 case nir_op_imax
: return LLVMConstInt(ctx
->i64
, INT64_MIN
, 0);
3986 case nir_op_umax
: return ctx
->i64_0
;
3987 case nir_op_fmax
: return LLVMConstReal(ctx
->f64
, -INFINITY
);
3988 case nir_op_iand
: return LLVMConstInt(ctx
->i64
, -1, 0);
3989 case nir_op_ior
: return ctx
->i64_0
;
3990 case nir_op_ixor
: return ctx
->i64_0
;
3992 unreachable("bad reduction intrinsic");
3998 ac_build_alu_op(struct ac_llvm_context
*ctx
, LLVMValueRef lhs
, LLVMValueRef rhs
, nir_op op
)
4000 bool _64bit
= ac_get_type_size(LLVMTypeOf(lhs
)) == 8;
4002 case nir_op_iadd
: return LLVMBuildAdd(ctx
->builder
, lhs
, rhs
, "");
4003 case nir_op_fadd
: return LLVMBuildFAdd(ctx
->builder
, lhs
, rhs
, "");
4004 case nir_op_imul
: return LLVMBuildMul(ctx
->builder
, lhs
, rhs
, "");
4005 case nir_op_fmul
: return LLVMBuildFMul(ctx
->builder
, lhs
, rhs
, "");
4006 case nir_op_imin
: return LLVMBuildSelect(ctx
->builder
,
4007 LLVMBuildICmp(ctx
->builder
, LLVMIntSLT
, lhs
, rhs
, ""),
4009 case nir_op_umin
: return LLVMBuildSelect(ctx
->builder
,
4010 LLVMBuildICmp(ctx
->builder
, LLVMIntULT
, lhs
, rhs
, ""),
4012 case nir_op_fmin
: return ac_build_intrinsic(ctx
,
4013 _64bit
? "llvm.minnum.f64" : "llvm.minnum.f32",
4014 _64bit
? ctx
->f64
: ctx
->f32
,
4015 (LLVMValueRef
[]){lhs
, rhs
}, 2, AC_FUNC_ATTR_READNONE
);
4016 case nir_op_imax
: return LLVMBuildSelect(ctx
->builder
,
4017 LLVMBuildICmp(ctx
->builder
, LLVMIntSGT
, lhs
, rhs
, ""),
4019 case nir_op_umax
: return LLVMBuildSelect(ctx
->builder
,
4020 LLVMBuildICmp(ctx
->builder
, LLVMIntUGT
, lhs
, rhs
, ""),
4022 case nir_op_fmax
: return ac_build_intrinsic(ctx
,
4023 _64bit
? "llvm.maxnum.f64" : "llvm.maxnum.f32",
4024 _64bit
? ctx
->f64
: ctx
->f32
,
4025 (LLVMValueRef
[]){lhs
, rhs
}, 2, AC_FUNC_ATTR_READNONE
);
4026 case nir_op_iand
: return LLVMBuildAnd(ctx
->builder
, lhs
, rhs
, "");
4027 case nir_op_ior
: return LLVMBuildOr(ctx
->builder
, lhs
, rhs
, "");
4028 case nir_op_ixor
: return LLVMBuildXor(ctx
->builder
, lhs
, rhs
, "");
4030 unreachable("bad reduction intrinsic");
4035 * \param maxprefix specifies that the result only needs to be correct for a
4036 * prefix of this many threads
4038 * TODO: add inclusive and excluse scan functions for GFX6.
4041 ac_build_scan(struct ac_llvm_context
*ctx
, nir_op op
, LLVMValueRef src
, LLVMValueRef identity
,
4044 LLVMValueRef result
, tmp
;
4048 tmp
= ac_build_dpp(ctx
, identity
, src
, dpp_row_sr(1), 0xf, 0xf, false);
4049 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
4052 tmp
= ac_build_dpp(ctx
, identity
, src
, dpp_row_sr(2), 0xf, 0xf, false);
4053 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
4056 tmp
= ac_build_dpp(ctx
, identity
, src
, dpp_row_sr(3), 0xf, 0xf, false);
4057 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
4060 tmp
= ac_build_dpp(ctx
, identity
, result
, dpp_row_sr(4), 0xf, 0xe, false);
4061 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
4064 tmp
= ac_build_dpp(ctx
, identity
, result
, dpp_row_sr(8), 0xf, 0xc, false);
4065 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
4066 if (maxprefix
<= 16)
4068 tmp
= ac_build_dpp(ctx
, identity
, result
, dpp_row_bcast15
, 0xa, 0xf, false);
4069 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
4070 if (maxprefix
<= 32)
4072 tmp
= ac_build_dpp(ctx
, identity
, result
, dpp_row_bcast31
, 0xc, 0xf, false);
4073 result
= ac_build_alu_op(ctx
, result
, tmp
, op
);
4078 ac_build_inclusive_scan(struct ac_llvm_context
*ctx
, LLVMValueRef src
, nir_op op
)
4080 LLVMValueRef result
;
4082 if (LLVMTypeOf(src
) == ctx
->i1
&& op
== nir_op_iadd
) {
4083 LLVMBuilderRef builder
= ctx
->builder
;
4084 src
= LLVMBuildZExt(builder
, src
, ctx
->i32
, "");
4085 result
= ac_build_ballot(ctx
, src
);
4086 result
= ac_build_mbcnt(ctx
, result
);
4087 result
= LLVMBuildAdd(builder
, result
, src
, "");
4091 ac_build_optimization_barrier(ctx
, &src
);
4093 LLVMValueRef identity
=
4094 get_reduction_identity(ctx
, op
, ac_get_type_size(LLVMTypeOf(src
)));
4095 result
= LLVMBuildBitCast(ctx
->builder
, ac_build_set_inactive(ctx
, src
, identity
),
4096 LLVMTypeOf(identity
), "");
4097 result
= ac_build_scan(ctx
, op
, result
, identity
, 64);
4099 return ac_build_wwm(ctx
, result
);
4103 ac_build_exclusive_scan(struct ac_llvm_context
*ctx
, LLVMValueRef src
, nir_op op
)
4105 LLVMValueRef result
;
4107 if (LLVMTypeOf(src
) == ctx
->i1
&& op
== nir_op_iadd
) {
4108 LLVMBuilderRef builder
= ctx
->builder
;
4109 src
= LLVMBuildZExt(builder
, src
, ctx
->i32
, "");
4110 result
= ac_build_ballot(ctx
, src
);
4111 result
= ac_build_mbcnt(ctx
, result
);
4115 ac_build_optimization_barrier(ctx
, &src
);
4117 LLVMValueRef identity
=
4118 get_reduction_identity(ctx
, op
, ac_get_type_size(LLVMTypeOf(src
)));
4119 result
= LLVMBuildBitCast(ctx
->builder
, ac_build_set_inactive(ctx
, src
, identity
),
4120 LLVMTypeOf(identity
), "");
4121 result
= ac_build_dpp(ctx
, identity
, result
, dpp_wf_sr1
, 0xf, 0xf, false);
4122 result
= ac_build_scan(ctx
, op
, result
, identity
, 64);
4124 return ac_build_wwm(ctx
, result
);
4128 ac_build_reduce(struct ac_llvm_context
*ctx
, LLVMValueRef src
, nir_op op
, unsigned cluster_size
)
4130 if (cluster_size
== 1) return src
;
4131 ac_build_optimization_barrier(ctx
, &src
);
4132 LLVMValueRef result
, swap
;
4133 LLVMValueRef identity
= get_reduction_identity(ctx
, op
,
4134 ac_get_type_size(LLVMTypeOf(src
)));
4135 result
= LLVMBuildBitCast(ctx
->builder
,
4136 ac_build_set_inactive(ctx
, src
, identity
),
4137 LLVMTypeOf(identity
), "");
4138 swap
= ac_build_quad_swizzle(ctx
, result
, 1, 0, 3, 2);
4139 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
4140 if (cluster_size
== 2) return ac_build_wwm(ctx
, result
);
4142 swap
= ac_build_quad_swizzle(ctx
, result
, 2, 3, 0, 1);
4143 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
4144 if (cluster_size
== 4) return ac_build_wwm(ctx
, result
);
4146 if (ctx
->chip_class
>= GFX8
)
4147 swap
= ac_build_dpp(ctx
, identity
, result
, dpp_row_half_mirror
, 0xf, 0xf, false);
4149 swap
= ac_build_ds_swizzle(ctx
, result
, ds_pattern_bitmode(0x1f, 0, 0x04));
4150 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
4151 if (cluster_size
== 8) return ac_build_wwm(ctx
, result
);
4153 if (ctx
->chip_class
>= GFX8
)
4154 swap
= ac_build_dpp(ctx
, identity
, result
, dpp_row_mirror
, 0xf, 0xf, false);
4156 swap
= ac_build_ds_swizzle(ctx
, result
, ds_pattern_bitmode(0x1f, 0, 0x08));
4157 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
4158 if (cluster_size
== 16) return ac_build_wwm(ctx
, result
);
4160 if (ctx
->chip_class
>= GFX8
&& cluster_size
!= 32)
4161 swap
= ac_build_dpp(ctx
, identity
, result
, dpp_row_bcast15
, 0xa, 0xf, false);
4163 swap
= ac_build_ds_swizzle(ctx
, result
, ds_pattern_bitmode(0x1f, 0, 0x10));
4164 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
4165 if (cluster_size
== 32) return ac_build_wwm(ctx
, result
);
4167 if (ctx
->chip_class
>= GFX8
) {
4168 swap
= ac_build_dpp(ctx
, identity
, result
, dpp_row_bcast31
, 0xc, 0xf, false);
4169 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
4170 result
= ac_build_readlane(ctx
, result
, LLVMConstInt(ctx
->i32
, 63, 0));
4171 return ac_build_wwm(ctx
, result
);
4173 swap
= ac_build_readlane(ctx
, result
, ctx
->i32_0
);
4174 result
= ac_build_readlane(ctx
, result
, LLVMConstInt(ctx
->i32
, 32, 0));
4175 result
= ac_build_alu_op(ctx
, result
, swap
, op
);
4176 return ac_build_wwm(ctx
, result
);
4181 * "Top half" of a scan that reduces per-wave values across an entire
4184 * The source value must be present in the highest lane of the wave, and the
4185 * highest lane must be live.
4188 ac_build_wg_wavescan_top(struct ac_llvm_context
*ctx
, struct ac_wg_scan
*ws
)
4190 if (ws
->maxwaves
<= 1)
4193 const LLVMValueRef i32_63
= LLVMConstInt(ctx
->i32
, 63, false);
4194 LLVMBuilderRef builder
= ctx
->builder
;
4195 LLVMValueRef tid
= ac_get_thread_id(ctx
);
4198 tmp
= LLVMBuildICmp(builder
, LLVMIntEQ
, tid
, i32_63
, "");
4199 ac_build_ifcc(ctx
, tmp
, 1000);
4200 LLVMBuildStore(builder
, ws
->src
, LLVMBuildGEP(builder
, ws
->scratch
, &ws
->waveidx
, 1, ""));
4201 ac_build_endif(ctx
, 1000);
4205 * "Bottom half" of a scan that reduces per-wave values across an entire
4208 * The caller must place a barrier between the top and bottom halves.
4211 ac_build_wg_wavescan_bottom(struct ac_llvm_context
*ctx
, struct ac_wg_scan
*ws
)
4213 const LLVMTypeRef type
= LLVMTypeOf(ws
->src
);
4214 const LLVMValueRef identity
=
4215 get_reduction_identity(ctx
, ws
->op
, ac_get_type_size(type
));
4217 if (ws
->maxwaves
<= 1) {
4218 ws
->result_reduce
= ws
->src
;
4219 ws
->result_inclusive
= ws
->src
;
4220 ws
->result_exclusive
= identity
;
4223 assert(ws
->maxwaves
<= 32);
4225 LLVMBuilderRef builder
= ctx
->builder
;
4226 LLVMValueRef tid
= ac_get_thread_id(ctx
);
4227 LLVMBasicBlockRef bbs
[2];
4228 LLVMValueRef phivalues_scan
[2];
4229 LLVMValueRef tmp
, tmp2
;
4231 bbs
[0] = LLVMGetInsertBlock(builder
);
4232 phivalues_scan
[0] = LLVMGetUndef(type
);
4234 if (ws
->enable_reduce
)
4235 tmp
= LLVMBuildICmp(builder
, LLVMIntULT
, tid
, ws
->numwaves
, "");
4236 else if (ws
->enable_inclusive
)
4237 tmp
= LLVMBuildICmp(builder
, LLVMIntULE
, tid
, ws
->waveidx
, "");
4239 tmp
= LLVMBuildICmp(builder
, LLVMIntULT
, tid
, ws
->waveidx
, "");
4240 ac_build_ifcc(ctx
, tmp
, 1001);
4242 tmp
= LLVMBuildLoad(builder
, LLVMBuildGEP(builder
, ws
->scratch
, &tid
, 1, ""), "");
4244 ac_build_optimization_barrier(ctx
, &tmp
);
4246 bbs
[1] = LLVMGetInsertBlock(builder
);
4247 phivalues_scan
[1] = ac_build_scan(ctx
, ws
->op
, tmp
, identity
, ws
->maxwaves
);
4249 ac_build_endif(ctx
, 1001);
4251 const LLVMValueRef scan
= ac_build_phi(ctx
, type
, 2, phivalues_scan
, bbs
);
4253 if (ws
->enable_reduce
) {
4254 tmp
= LLVMBuildSub(builder
, ws
->numwaves
, ctx
->i32_1
, "");
4255 ws
->result_reduce
= ac_build_readlane(ctx
, scan
, tmp
);
4257 if (ws
->enable_inclusive
)
4258 ws
->result_inclusive
= ac_build_readlane(ctx
, scan
, ws
->waveidx
);
4259 if (ws
->enable_exclusive
) {
4260 tmp
= LLVMBuildSub(builder
, ws
->waveidx
, ctx
->i32_1
, "");
4261 tmp
= ac_build_readlane(ctx
, scan
, tmp
);
4262 tmp2
= LLVMBuildICmp(builder
, LLVMIntEQ
, ws
->waveidx
, ctx
->i32_0
, "");
4263 ws
->result_exclusive
= LLVMBuildSelect(builder
, tmp2
, identity
, tmp
, "");
4268 * Inclusive scan of a per-wave value across an entire workgroup.
4270 * This implies an s_barrier instruction.
4272 * Unlike ac_build_inclusive_scan, the caller \em must ensure that all threads
4273 * of the workgroup are live. (This requirement cannot easily be relaxed in a
4274 * useful manner because of the barrier in the algorithm.)
4277 ac_build_wg_wavescan(struct ac_llvm_context
*ctx
, struct ac_wg_scan
*ws
)
4279 ac_build_wg_wavescan_top(ctx
, ws
);
4280 ac_build_s_barrier(ctx
);
4281 ac_build_wg_wavescan_bottom(ctx
, ws
);
4285 * "Top half" of a scan that reduces per-thread values across an entire
4288 * All lanes must be active when this code runs.
4291 ac_build_wg_scan_top(struct ac_llvm_context
*ctx
, struct ac_wg_scan
*ws
)
4293 if (ws
->enable_exclusive
) {
4294 ws
->extra
= ac_build_exclusive_scan(ctx
, ws
->src
, ws
->op
);
4295 if (LLVMTypeOf(ws
->src
) == ctx
->i1
&& ws
->op
== nir_op_iadd
)
4296 ws
->src
= LLVMBuildZExt(ctx
->builder
, ws
->src
, ctx
->i32
, "");
4297 ws
->src
= ac_build_alu_op(ctx
, ws
->extra
, ws
->src
, ws
->op
);
4299 ws
->src
= ac_build_inclusive_scan(ctx
, ws
->src
, ws
->op
);
4302 bool enable_inclusive
= ws
->enable_inclusive
;
4303 bool enable_exclusive
= ws
->enable_exclusive
;
4304 ws
->enable_inclusive
= false;
4305 ws
->enable_exclusive
= ws
->enable_exclusive
|| enable_inclusive
;
4306 ac_build_wg_wavescan_top(ctx
, ws
);
4307 ws
->enable_inclusive
= enable_inclusive
;
4308 ws
->enable_exclusive
= enable_exclusive
;
4312 * "Bottom half" of a scan that reduces per-thread values across an entire
4315 * The caller must place a barrier between the top and bottom halves.
4318 ac_build_wg_scan_bottom(struct ac_llvm_context
*ctx
, struct ac_wg_scan
*ws
)
4320 bool enable_inclusive
= ws
->enable_inclusive
;
4321 bool enable_exclusive
= ws
->enable_exclusive
;
4322 ws
->enable_inclusive
= false;
4323 ws
->enable_exclusive
= ws
->enable_exclusive
|| enable_inclusive
;
4324 ac_build_wg_wavescan_bottom(ctx
, ws
);
4325 ws
->enable_inclusive
= enable_inclusive
;
4326 ws
->enable_exclusive
= enable_exclusive
;
4328 /* ws->result_reduce is already the correct value */
4329 if (ws
->enable_inclusive
)
4330 ws
->result_inclusive
= ac_build_alu_op(ctx
, ws
->result_exclusive
, ws
->src
, ws
->op
);
4331 if (ws
->enable_exclusive
)
4332 ws
->result_exclusive
= ac_build_alu_op(ctx
, ws
->result_exclusive
, ws
->extra
, ws
->op
);
4336 * A scan that reduces per-thread values across an entire workgroup.
4338 * The caller must ensure that all lanes are active when this code runs
4339 * (WWM is insufficient!), because there is an implied barrier.
4342 ac_build_wg_scan(struct ac_llvm_context
*ctx
, struct ac_wg_scan
*ws
)
4344 ac_build_wg_scan_top(ctx
, ws
);
4345 ac_build_s_barrier(ctx
);
4346 ac_build_wg_scan_bottom(ctx
, ws
);
4350 ac_build_quad_swizzle(struct ac_llvm_context
*ctx
, LLVMValueRef src
,
4351 unsigned lane0
, unsigned lane1
, unsigned lane2
, unsigned lane3
)
4353 unsigned mask
= dpp_quad_perm(lane0
, lane1
, lane2
, lane3
);
4354 if (ctx
->chip_class
>= GFX8
) {
4355 return ac_build_dpp(ctx
, src
, src
, mask
, 0xf, 0xf, false);
4357 return ac_build_ds_swizzle(ctx
, src
, (1 << 15) | mask
);
4362 ac_build_shuffle(struct ac_llvm_context
*ctx
, LLVMValueRef src
, LLVMValueRef index
)
4364 index
= LLVMBuildMul(ctx
->builder
, index
, LLVMConstInt(ctx
->i32
, 4, 0), "");
4365 return ac_build_intrinsic(ctx
,
4366 "llvm.amdgcn.ds.bpermute", ctx
->i32
,
4367 (LLVMValueRef
[]) {index
, src
}, 2,
4368 AC_FUNC_ATTR_READNONE
|
4369 AC_FUNC_ATTR_CONVERGENT
);
4373 ac_build_frexp_exp(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
4379 if (bitsize
== 16) {
4380 intr
= "llvm.amdgcn.frexp.exp.i16.f16";
4382 } else if (bitsize
== 32) {
4383 intr
= "llvm.amdgcn.frexp.exp.i32.f32";
4386 intr
= "llvm.amdgcn.frexp.exp.i32.f64";
4390 LLVMValueRef params
[] = {
4393 return ac_build_intrinsic(ctx
, intr
, type
, params
, 1,
4394 AC_FUNC_ATTR_READNONE
);
4397 ac_build_frexp_mant(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
4403 if (bitsize
== 16) {
4404 intr
= "llvm.amdgcn.frexp.mant.f16";
4406 } else if (bitsize
== 32) {
4407 intr
= "llvm.amdgcn.frexp.mant.f32";
4410 intr
= "llvm.amdgcn.frexp.mant.f64";
4414 LLVMValueRef params
[] = {
4417 return ac_build_intrinsic(ctx
, intr
, type
, params
, 1,
4418 AC_FUNC_ATTR_READNONE
);
4422 * this takes an I,J coordinate pair,
4423 * and works out the X and Y derivatives.
4424 * it returns DDX(I), DDX(J), DDY(I), DDY(J).
4427 ac_build_ddxy_interp(struct ac_llvm_context
*ctx
, LLVMValueRef interp_ij
)
4429 LLVMValueRef result
[4], a
;
4432 for (i
= 0; i
< 2; i
++) {
4433 a
= LLVMBuildExtractElement(ctx
->builder
, interp_ij
,
4434 LLVMConstInt(ctx
->i32
, i
, false), "");
4435 result
[i
] = ac_build_ddxy(ctx
, AC_TID_MASK_TOP_LEFT
, 1, a
);
4436 result
[2+i
] = ac_build_ddxy(ctx
, AC_TID_MASK_TOP_LEFT
, 2, a
);
4438 return ac_build_gather_values(ctx
, result
, 4);
4442 ac_build_load_helper_invocation(struct ac_llvm_context
*ctx
)
4444 LLVMValueRef result
= ac_build_intrinsic(ctx
, "llvm.amdgcn.ps.live",
4446 AC_FUNC_ATTR_READNONE
);
4447 result
= LLVMBuildNot(ctx
->builder
, result
, "");
4448 return LLVMBuildSExt(ctx
->builder
, result
, ctx
->i32
, "");