1 /**************************************************************************
3 * Copyright 2009 VMware, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
31 * Helpers for emiting intrinsic calls.
33 * LLVM vanilla IR doesn't represent all basic arithmetic operations we care
34 * about, and it is often necessary to resort target-specific intrinsics for
35 * performance, convenience.
37 * Ideally we would like to stay away from target specific intrinsics and
38 * move all the instruction selection logic into upstream LLVM where it belongs.
40 * These functions are also used for calling C functions provided by us from
41 * generated LLVM code.
43 * @author Jose Fonseca <jfonseca@vmware.com>
46 #include <llvm/Config/llvm-config.h>
48 #include "util/u_debug.h"
49 #include "util/u_string.h"
50 #include "util/bitscan.h"
52 #include "lp_bld_const.h"
53 #include "lp_bld_intr.h"
54 #include "lp_bld_type.h"
55 #include "lp_bld_pack.h"
56 #include "lp_bld_debug.h"
60 lp_format_intrinsic(char *name
,
62 const char *name_root
,
69 LLVMTypeKind kind
= LLVMGetTypeKind(type
);
70 if (kind
== LLVMVectorTypeKind
) {
71 length
= LLVMGetVectorSize(type
);
72 type
= LLVMGetElementType(type
);
73 kind
= LLVMGetTypeKind(type
);
77 case LLVMIntegerTypeKind
:
79 width
= LLVMGetIntTypeWidth(type
);
81 case LLVMFloatTypeKind
:
85 case LLVMDoubleTypeKind
:
90 unreachable("unexpected LLVMTypeKind");
94 snprintf(name
, size
, "%s.v%u%c%u", name_root
, length
, c
, width
);
96 snprintf(name
, size
, "%s.%c%u", name_root
, c
, width
);
102 lp_declare_intrinsic(LLVMModuleRef module
,
104 LLVMTypeRef ret_type
,
105 LLVMTypeRef
*arg_types
,
108 LLVMTypeRef function_type
;
109 LLVMValueRef function
;
111 assert(!LLVMGetNamedFunction(module
, name
));
113 function_type
= LLVMFunctionType(ret_type
, arg_types
, num_args
, 0);
114 function
= LLVMAddFunction(module
, name
, function_type
);
116 LLVMSetFunctionCallConv(function
, LLVMCCallConv
);
117 LLVMSetLinkage(function
, LLVMExternalLinkage
);
119 assert(LLVMIsDeclaration(function
));
125 #if LLVM_VERSION_MAJOR < 4
126 static LLVMAttribute
lp_attr_to_llvm_attr(enum lp_func_attr attr
)
129 case LP_FUNC_ATTR_ALWAYSINLINE
: return LLVMAlwaysInlineAttribute
;
130 case LP_FUNC_ATTR_INREG
: return LLVMInRegAttribute
;
131 case LP_FUNC_ATTR_NOALIAS
: return LLVMNoAliasAttribute
;
132 case LP_FUNC_ATTR_NOUNWIND
: return LLVMNoUnwindAttribute
;
133 case LP_FUNC_ATTR_READNONE
: return LLVMReadNoneAttribute
;
134 case LP_FUNC_ATTR_READONLY
: return LLVMReadOnlyAttribute
;
136 _debug_printf("Unhandled function attribute: %x\n", attr
);
143 static const char *attr_to_str(enum lp_func_attr attr
)
146 case LP_FUNC_ATTR_ALWAYSINLINE
: return "alwaysinline";
147 case LP_FUNC_ATTR_INREG
: return "inreg";
148 case LP_FUNC_ATTR_NOALIAS
: return "noalias";
149 case LP_FUNC_ATTR_NOUNWIND
: return "nounwind";
150 case LP_FUNC_ATTR_READNONE
: return "readnone";
151 case LP_FUNC_ATTR_READONLY
: return "readonly";
152 case LP_FUNC_ATTR_WRITEONLY
: return "writeonly";
153 case LP_FUNC_ATTR_INACCESSIBLE_MEM_ONLY
: return "inaccessiblememonly";
154 case LP_FUNC_ATTR_CONVERGENT
: return "convergent";
156 _debug_printf("Unhandled function attribute: %x\n", attr
);
164 lp_add_function_attr(LLVMValueRef function_or_call
,
165 int attr_idx
, enum lp_func_attr attr
)
168 #if LLVM_VERSION_MAJOR < 4
169 LLVMAttribute llvm_attr
= lp_attr_to_llvm_attr(attr
);
170 if (LLVMIsAFunction(function_or_call
)) {
171 if (attr_idx
== -1) {
172 LLVMAddFunctionAttr(function_or_call
, llvm_attr
);
174 LLVMAddAttribute(LLVMGetParam(function_or_call
, attr_idx
- 1), llvm_attr
);
177 LLVMAddInstrAttribute(function_or_call
, attr_idx
, llvm_attr
);
181 LLVMModuleRef module
;
182 if (LLVMIsAFunction(function_or_call
)) {
183 module
= LLVMGetGlobalParent(function_or_call
);
185 LLVMBasicBlockRef bb
= LLVMGetInstructionParent(function_or_call
);
186 LLVMValueRef function
= LLVMGetBasicBlockParent(bb
);
187 module
= LLVMGetGlobalParent(function
);
189 LLVMContextRef ctx
= LLVMGetModuleContext(module
);
191 const char *attr_name
= attr_to_str(attr
);
192 unsigned kind_id
= LLVMGetEnumAttributeKindForName(attr_name
,
194 LLVMAttributeRef llvm_attr
= LLVMCreateEnumAttribute(ctx
, kind_id
, 0);
196 if (LLVMIsAFunction(function_or_call
))
197 LLVMAddAttributeAtIndex(function_or_call
, attr_idx
, llvm_attr
);
199 LLVMAddCallSiteAttribute(function_or_call
, attr_idx
, llvm_attr
);
204 lp_add_func_attributes(LLVMValueRef function
, unsigned attrib_mask
)
206 /* NoUnwind indicates that the intrinsic never raises a C++ exception.
207 * Set it for all intrinsics.
209 attrib_mask
|= LP_FUNC_ATTR_NOUNWIND
;
210 attrib_mask
&= ~LP_FUNC_ATTR_LEGACY
;
212 while (attrib_mask
) {
213 enum lp_func_attr attr
= 1u << u_bit_scan(&attrib_mask
);
214 lp_add_function_attr(function
, -1, attr
);
219 lp_build_intrinsic(LLVMBuilderRef builder
,
221 LLVMTypeRef ret_type
,
226 LLVMModuleRef module
= LLVMGetGlobalParent(LLVMGetBasicBlockParent(LLVMGetInsertBlock(builder
)));
227 LLVMValueRef function
, call
;
228 bool set_callsite_attrs
= LLVM_VERSION_MAJOR
>= 4 &&
229 !(attr_mask
& LP_FUNC_ATTR_LEGACY
);
231 function
= LLVMGetNamedFunction(module
, name
);
233 LLVMTypeRef arg_types
[LP_MAX_FUNC_ARGS
];
236 assert(num_args
<= LP_MAX_FUNC_ARGS
);
238 for(i
= 0; i
< num_args
; ++i
) {
240 arg_types
[i
] = LLVMTypeOf(args
[i
]);
243 function
= lp_declare_intrinsic(module
, name
, ret_type
, arg_types
, num_args
);
246 * If llvm removes an intrinsic we use, we'll hit this abort (rather
247 * than a call to address zero in the jited code).
249 if (LLVMGetIntrinsicID(function
) == 0) {
250 _debug_printf("llvm (version " MESA_LLVM_VERSION_STRING
251 ") found no intrinsic for %s, going to crash...\n",
256 if (!set_callsite_attrs
)
257 lp_add_func_attributes(function
, attr_mask
);
259 if (gallivm_debug
& GALLIVM_DEBUG_IR
) {
260 lp_debug_dump_value(function
);
264 call
= LLVMBuildCall(builder
, function
, args
, num_args
, "");
265 if (set_callsite_attrs
)
266 lp_add_func_attributes(call
, attr_mask
);
272 lp_build_intrinsic_unary(LLVMBuilderRef builder
,
274 LLVMTypeRef ret_type
,
277 return lp_build_intrinsic(builder
, name
, ret_type
, &a
, 1, 0);
282 lp_build_intrinsic_binary(LLVMBuilderRef builder
,
284 LLVMTypeRef ret_type
,
288 LLVMValueRef args
[2];
293 return lp_build_intrinsic(builder
, name
, ret_type
, args
, 2, 0);
298 * Call intrinsic with arguments adapted to intrinsic vector length.
300 * Split vectors which are too large for the hw, or expand them if they
301 * are too small, so a caller calling a function which might use intrinsics
302 * doesn't need to do splitting/expansion on its own.
303 * This only supports intrinsics where src and dst types match.
306 lp_build_intrinsic_binary_anylength(struct gallivm_state
*gallivm
,
308 struct lp_type src_type
,
314 struct lp_type intrin_type
= src_type
;
315 LLVMBuilderRef builder
= gallivm
->builder
;
316 LLVMValueRef i32undef
= LLVMGetUndef(LLVMInt32TypeInContext(gallivm
->context
));
317 LLVMValueRef anative
, bnative
;
318 unsigned intrin_length
= intr_size
/ src_type
.width
;
320 intrin_type
.length
= intrin_length
;
322 if (intrin_length
> src_type
.length
) {
323 LLVMValueRef elems
[LP_MAX_VECTOR_LENGTH
];
324 LLVMValueRef constvec
, tmp
;
326 for (i
= 0; i
< src_type
.length
; i
++) {
327 elems
[i
] = lp_build_const_int32(gallivm
, i
);
329 for (; i
< intrin_length
; i
++) {
332 if (src_type
.length
== 1) {
333 LLVMTypeRef elem_type
= lp_build_elem_type(gallivm
, intrin_type
);
334 a
= LLVMBuildBitCast(builder
, a
, LLVMVectorType(elem_type
, 1), "");
335 b
= LLVMBuildBitCast(builder
, b
, LLVMVectorType(elem_type
, 1), "");
337 constvec
= LLVMConstVector(elems
, intrin_length
);
338 anative
= LLVMBuildShuffleVector(builder
, a
, a
, constvec
, "");
339 bnative
= LLVMBuildShuffleVector(builder
, b
, b
, constvec
, "");
340 tmp
= lp_build_intrinsic_binary(builder
, name
,
341 lp_build_vec_type(gallivm
, intrin_type
),
343 if (src_type
.length
> 1) {
344 constvec
= LLVMConstVector(elems
, src_type
.length
);
345 return LLVMBuildShuffleVector(builder
, tmp
, tmp
, constvec
, "");
348 return LLVMBuildExtractElement(builder
, tmp
, elems
[0], "");
351 else if (intrin_length
< src_type
.length
) {
352 unsigned num_vec
= src_type
.length
/ intrin_length
;
353 LLVMValueRef tmp
[LP_MAX_VECTOR_LENGTH
];
355 /* don't support arbitrary size here as this is so yuck */
356 if (src_type
.length
% intrin_length
) {
357 /* FIXME: This is something which should be supported
358 * but there doesn't seem to be any need for it currently
361 debug_printf("%s: should handle arbitrary vector size\n",
367 for (i
= 0; i
< num_vec
; i
++) {
368 anative
= lp_build_extract_range(gallivm
, a
, i
*intrin_length
,
370 bnative
= lp_build_extract_range(gallivm
, b
, i
*intrin_length
,
372 tmp
[i
] = lp_build_intrinsic_binary(builder
, name
,
373 lp_build_vec_type(gallivm
, intrin_type
),
376 return lp_build_concat(gallivm
, tmp
, intrin_type
, num_vec
);
379 return lp_build_intrinsic_binary(builder
, name
,
380 lp_build_vec_type(gallivm
, src_type
),
387 lp_build_intrinsic_map(struct gallivm_state
*gallivm
,
389 LLVMTypeRef ret_type
,
393 LLVMBuilderRef builder
= gallivm
->builder
;
394 LLVMTypeRef ret_elem_type
= LLVMGetElementType(ret_type
);
395 unsigned n
= LLVMGetVectorSize(ret_type
);
399 assert(num_args
<= LP_MAX_FUNC_ARGS
);
401 res
= LLVMGetUndef(ret_type
);
402 for(i
= 0; i
< n
; ++i
) {
403 LLVMValueRef index
= lp_build_const_int32(gallivm
, i
);
404 LLVMValueRef arg_elems
[LP_MAX_FUNC_ARGS
];
405 LLVMValueRef res_elem
;
406 for(j
= 0; j
< num_args
; ++j
)
407 arg_elems
[j
] = LLVMBuildExtractElement(builder
, args
[j
], index
, "");
408 res_elem
= lp_build_intrinsic(builder
, name
, ret_elem_type
, arg_elems
, num_args
, 0);
409 res
= LLVMBuildInsertElement(builder
, res
, res_elem
, index
, "");
417 lp_build_intrinsic_map_unary(struct gallivm_state
*gallivm
,
419 LLVMTypeRef ret_type
,
422 return lp_build_intrinsic_map(gallivm
, name
, ret_type
, &a
, 1);
427 lp_build_intrinsic_map_binary(struct gallivm_state
*gallivm
,
429 LLVMTypeRef ret_type
,
433 LLVMValueRef args
[2];
438 return lp_build_intrinsic_map(gallivm
, name
, ret_type
, args
, 2);