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>
47 #include "util/u_debug.h"
48 #include "util/u_string.h"
49 #include "util/bitscan.h"
51 #include "lp_bld_const.h"
52 #include "lp_bld_intr.h"
53 #include "lp_bld_type.h"
54 #include "lp_bld_pack.h"
55 #include "lp_bld_debug.h"
59 lp_format_intrinsic(char *name
,
61 const char *name_root
,
68 LLVMTypeKind kind
= LLVMGetTypeKind(type
);
69 if (kind
== LLVMVectorTypeKind
) {
70 length
= LLVMGetVectorSize(type
);
71 type
= LLVMGetElementType(type
);
72 kind
= LLVMGetTypeKind(type
);
76 case LLVMIntegerTypeKind
:
78 width
= LLVMGetIntTypeWidth(type
);
80 case LLVMFloatTypeKind
:
84 case LLVMDoubleTypeKind
:
89 unreachable("unexpected LLVMTypeKind");
93 util_snprintf(name
, size
, "%s.v%u%c%u", name_root
, length
, c
, width
);
95 util_snprintf(name
, size
, "%s.%c%u", name_root
, c
, width
);
101 lp_declare_intrinsic(LLVMModuleRef module
,
103 LLVMTypeRef ret_type
,
104 LLVMTypeRef
*arg_types
,
107 LLVMTypeRef function_type
;
108 LLVMValueRef function
;
110 assert(!LLVMGetNamedFunction(module
, name
));
112 function_type
= LLVMFunctionType(ret_type
, arg_types
, num_args
, 0);
113 function
= LLVMAddFunction(module
, name
, function_type
);
115 LLVMSetFunctionCallConv(function
, LLVMCCallConv
);
116 LLVMSetLinkage(function
, LLVMExternalLinkage
);
118 assert(LLVMIsDeclaration(function
));
124 #if HAVE_LLVM < 0x0400
125 static LLVMAttribute
lp_attr_to_llvm_attr(enum lp_func_attr attr
)
128 case LP_FUNC_ATTR_ALWAYSINLINE
: return LLVMAlwaysInlineAttribute
;
129 case LP_FUNC_ATTR_BYVAL
: return LLVMByValAttribute
;
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_BYVAL
: return "byval";
148 case LP_FUNC_ATTR_INREG
: return "inreg";
149 case LP_FUNC_ATTR_NOALIAS
: return "noalias";
150 case LP_FUNC_ATTR_NOUNWIND
: return "nounwind";
151 case LP_FUNC_ATTR_READNONE
: return "readnone";
152 case LP_FUNC_ATTR_READONLY
: return "readonly";
153 case LP_FUNC_ATTR_WRITEONLY
: return "writeonly";
154 case LP_FUNC_ATTR_INACCESSIBLE_MEM_ONLY
: return "inaccessiblememonly";
155 case LP_FUNC_ATTR_CONVERGENT
: return "convergent";
157 _debug_printf("Unhandled function attribute: %x\n", attr
);
165 lp_add_function_attr(LLVMValueRef function_or_call
,
166 int attr_idx
, enum lp_func_attr attr
)
169 #if HAVE_LLVM < 0x0400
170 LLVMAttribute llvm_attr
= lp_attr_to_llvm_attr(attr
);
171 if (attr_idx
== -1) {
172 LLVMAddFunctionAttr(function_or_call
, llvm_attr
);
174 LLVMAddAttribute(LLVMGetParam(function_or_call
, attr_idx
- 1), llvm_attr
);
178 LLVMModuleRef module
;
179 if (LLVMIsAFunction(function_or_call
)) {
180 module
= LLVMGetGlobalParent(function_or_call
);
182 LLVMBasicBlockRef bb
= LLVMGetInstructionParent(function_or_call
);
183 LLVMValueRef function
= LLVMGetBasicBlockParent(bb
);
184 module
= LLVMGetGlobalParent(function
);
186 LLVMContextRef ctx
= LLVMGetModuleContext(module
);
188 const char *attr_name
= attr_to_str(attr
);
189 unsigned kind_id
= LLVMGetEnumAttributeKindForName(attr_name
,
191 LLVMAttributeRef llvm_attr
= LLVMCreateEnumAttribute(ctx
, kind_id
, 0);
193 if (LLVMIsAFunction(function_or_call
))
194 LLVMAddAttributeAtIndex(function_or_call
, attr_idx
, llvm_attr
);
196 LLVMAddCallSiteAttribute(function_or_call
, attr_idx
, llvm_attr
);
201 lp_add_func_attributes(LLVMValueRef function
, unsigned attrib_mask
)
203 /* NoUnwind indicates that the intrinsic never raises a C++ exception.
204 * Set it for all intrinsics.
206 attrib_mask
|= LP_FUNC_ATTR_NOUNWIND
;
207 attrib_mask
&= ~LP_FUNC_ATTR_LEGACY
;
209 while (attrib_mask
) {
210 enum lp_func_attr attr
= 1u << u_bit_scan(&attrib_mask
);
211 lp_add_function_attr(function
, -1, attr
);
216 lp_build_intrinsic(LLVMBuilderRef builder
,
218 LLVMTypeRef ret_type
,
223 LLVMModuleRef module
= LLVMGetGlobalParent(LLVMGetBasicBlockParent(LLVMGetInsertBlock(builder
)));
224 LLVMValueRef function
, call
;
225 bool set_callsite_attrs
= HAVE_LLVM
>= 0x0400 &&
226 !(attr_mask
& LP_FUNC_ATTR_LEGACY
);
228 function
= LLVMGetNamedFunction(module
, name
);
230 LLVMTypeRef arg_types
[LP_MAX_FUNC_ARGS
];
233 assert(num_args
<= LP_MAX_FUNC_ARGS
);
235 for(i
= 0; i
< num_args
; ++i
) {
237 arg_types
[i
] = LLVMTypeOf(args
[i
]);
240 function
= lp_declare_intrinsic(module
, name
, ret_type
, arg_types
, num_args
);
242 if (!set_callsite_attrs
)
243 lp_add_func_attributes(function
, attr_mask
);
245 if (gallivm_debug
& GALLIVM_DEBUG_IR
) {
246 lp_debug_dump_value(function
);
250 call
= LLVMBuildCall(builder
, function
, args
, num_args
, "");
251 if (set_callsite_attrs
)
252 lp_add_func_attributes(call
, attr_mask
);
258 lp_build_intrinsic_unary(LLVMBuilderRef builder
,
260 LLVMTypeRef ret_type
,
263 return lp_build_intrinsic(builder
, name
, ret_type
, &a
, 1, 0);
268 lp_build_intrinsic_binary(LLVMBuilderRef builder
,
270 LLVMTypeRef ret_type
,
274 LLVMValueRef args
[2];
279 return lp_build_intrinsic(builder
, name
, ret_type
, args
, 2, 0);
284 * Call intrinsic with arguments adapted to intrinsic vector length.
286 * Split vectors which are too large for the hw, or expand them if they
287 * are too small, so a caller calling a function which might use intrinsics
288 * doesn't need to do splitting/expansion on its own.
289 * This only supports intrinsics where src and dst types match.
292 lp_build_intrinsic_binary_anylength(struct gallivm_state
*gallivm
,
294 struct lp_type src_type
,
300 struct lp_type intrin_type
= src_type
;
301 LLVMBuilderRef builder
= gallivm
->builder
;
302 LLVMValueRef i32undef
= LLVMGetUndef(LLVMInt32TypeInContext(gallivm
->context
));
303 LLVMValueRef anative
, bnative
;
304 unsigned intrin_length
= intr_size
/ src_type
.width
;
306 intrin_type
.length
= intrin_length
;
308 if (intrin_length
> src_type
.length
) {
309 LLVMValueRef elems
[LP_MAX_VECTOR_LENGTH
];
310 LLVMValueRef constvec
, tmp
;
312 for (i
= 0; i
< src_type
.length
; i
++) {
313 elems
[i
] = lp_build_const_int32(gallivm
, i
);
315 for (; i
< intrin_length
; i
++) {
318 if (src_type
.length
== 1) {
319 LLVMTypeRef elem_type
= lp_build_elem_type(gallivm
, intrin_type
);
320 a
= LLVMBuildBitCast(builder
, a
, LLVMVectorType(elem_type
, 1), "");
321 b
= LLVMBuildBitCast(builder
, b
, LLVMVectorType(elem_type
, 1), "");
323 constvec
= LLVMConstVector(elems
, intrin_length
);
324 anative
= LLVMBuildShuffleVector(builder
, a
, a
, constvec
, "");
325 bnative
= LLVMBuildShuffleVector(builder
, b
, b
, constvec
, "");
326 tmp
= lp_build_intrinsic_binary(builder
, name
,
327 lp_build_vec_type(gallivm
, intrin_type
),
329 if (src_type
.length
> 1) {
330 constvec
= LLVMConstVector(elems
, src_type
.length
);
331 return LLVMBuildShuffleVector(builder
, tmp
, tmp
, constvec
, "");
334 return LLVMBuildExtractElement(builder
, tmp
, elems
[0], "");
337 else if (intrin_length
< src_type
.length
) {
338 unsigned num_vec
= src_type
.length
/ intrin_length
;
339 LLVMValueRef tmp
[LP_MAX_VECTOR_LENGTH
];
341 /* don't support arbitrary size here as this is so yuck */
342 if (src_type
.length
% intrin_length
) {
343 /* FIXME: This is something which should be supported
344 * but there doesn't seem to be any need for it currently
347 debug_printf("%s: should handle arbitrary vector size\n",
353 for (i
= 0; i
< num_vec
; i
++) {
354 anative
= lp_build_extract_range(gallivm
, a
, i
*intrin_length
,
356 bnative
= lp_build_extract_range(gallivm
, b
, i
*intrin_length
,
358 tmp
[i
] = lp_build_intrinsic_binary(builder
, name
,
359 lp_build_vec_type(gallivm
, intrin_type
),
362 return lp_build_concat(gallivm
, tmp
, intrin_type
, num_vec
);
365 return lp_build_intrinsic_binary(builder
, name
,
366 lp_build_vec_type(gallivm
, src_type
),
373 lp_build_intrinsic_map(struct gallivm_state
*gallivm
,
375 LLVMTypeRef ret_type
,
379 LLVMBuilderRef builder
= gallivm
->builder
;
380 LLVMTypeRef ret_elem_type
= LLVMGetElementType(ret_type
);
381 unsigned n
= LLVMGetVectorSize(ret_type
);
385 assert(num_args
<= LP_MAX_FUNC_ARGS
);
387 res
= LLVMGetUndef(ret_type
);
388 for(i
= 0; i
< n
; ++i
) {
389 LLVMValueRef index
= lp_build_const_int32(gallivm
, i
);
390 LLVMValueRef arg_elems
[LP_MAX_FUNC_ARGS
];
391 LLVMValueRef res_elem
;
392 for(j
= 0; j
< num_args
; ++j
)
393 arg_elems
[j
] = LLVMBuildExtractElement(builder
, args
[j
], index
, "");
394 res_elem
= lp_build_intrinsic(builder
, name
, ret_elem_type
, arg_elems
, num_args
, 0);
395 res
= LLVMBuildInsertElement(builder
, res
, res_elem
, index
, "");
403 lp_build_intrinsic_map_unary(struct gallivm_state
*gallivm
,
405 LLVMTypeRef ret_type
,
408 return lp_build_intrinsic_map(gallivm
, name
, ret_type
, &a
, 1);
413 lp_build_intrinsic_map_binary(struct gallivm_state
*gallivm
,
415 LLVMTypeRef ret_type
,
419 LLVMValueRef args
[2];
424 return lp_build_intrinsic_map(gallivm
, name
, ret_type
, args
, 2);