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"
50 #include "lp_bld_const.h"
51 #include "lp_bld_intr.h"
52 #include "lp_bld_type.h"
53 #include "lp_bld_pack.h"
54 #include "lp_bld_debug.h"
58 lp_format_intrinsic(char *name
,
60 const char *name_root
,
67 LLVMTypeKind kind
= LLVMGetTypeKind(type
);
68 if (kind
== LLVMVectorTypeKind
) {
69 length
= LLVMGetVectorSize(type
);
70 type
= LLVMGetElementType(type
);
71 kind
= LLVMGetTypeKind(type
);
75 case LLVMIntegerTypeKind
:
77 width
= LLVMGetIntTypeWidth(type
);
79 case LLVMFloatTypeKind
:
83 case LLVMDoubleTypeKind
:
88 unreachable("unexpected LLVMTypeKind");
92 util_snprintf(name
, size
, "%s.v%u%c%u", name_root
, length
, c
, width
);
94 util_snprintf(name
, size
, "%s.%c%u", name_root
, c
, width
);
100 lp_declare_intrinsic(LLVMModuleRef module
,
102 LLVMTypeRef ret_type
,
103 LLVMTypeRef
*arg_types
,
106 LLVMTypeRef function_type
;
107 LLVMValueRef function
;
109 assert(!LLVMGetNamedFunction(module
, name
));
111 function_type
= LLVMFunctionType(ret_type
, arg_types
, num_args
, 0);
112 function
= LLVMAddFunction(module
, name
, function_type
);
114 LLVMSetFunctionCallConv(function
, LLVMCCallConv
);
115 LLVMSetLinkage(function
, LLVMExternalLinkage
);
117 assert(LLVMIsDeclaration(function
));
124 lp_build_intrinsic(LLVMBuilderRef builder
,
126 LLVMTypeRef ret_type
,
131 LLVMModuleRef module
= LLVMGetGlobalParent(LLVMGetBasicBlockParent(LLVMGetInsertBlock(builder
)));
132 LLVMValueRef function
;
134 function
= LLVMGetNamedFunction(module
, name
);
136 LLVMTypeRef arg_types
[LP_MAX_FUNC_ARGS
];
139 assert(num_args
<= LP_MAX_FUNC_ARGS
);
141 for(i
= 0; i
< num_args
; ++i
) {
143 arg_types
[i
] = LLVMTypeOf(args
[i
]);
146 function
= lp_declare_intrinsic(module
, name
, ret_type
, arg_types
, num_args
);
148 /* NoUnwind indicates that the intrinsic never raises a C++ exception.
149 * Set it for all intrinsics.
151 LLVMAddFunctionAttr(function
, attr
| LLVMNoUnwindAttribute
);
153 if (gallivm_debug
& GALLIVM_DEBUG_IR
) {
154 lp_debug_dump_value(function
);
158 return LLVMBuildCall(builder
, function
, args
, num_args
, "");
163 lp_build_intrinsic_unary(LLVMBuilderRef builder
,
165 LLVMTypeRef ret_type
,
168 return lp_build_intrinsic(builder
, name
, ret_type
, &a
, 1, 0);
173 lp_build_intrinsic_binary(LLVMBuilderRef builder
,
175 LLVMTypeRef ret_type
,
179 LLVMValueRef args
[2];
184 return lp_build_intrinsic(builder
, name
, ret_type
, args
, 2, 0);
189 * Call intrinsic with arguments adapted to intrinsic vector length.
191 * Split vectors which are too large for the hw, or expand them if they
192 * are too small, so a caller calling a function which might use intrinsics
193 * doesn't need to do splitting/expansion on its own.
194 * This only supports intrinsics where src and dst types match.
197 lp_build_intrinsic_binary_anylength(struct gallivm_state
*gallivm
,
199 struct lp_type src_type
,
205 struct lp_type intrin_type
= src_type
;
206 LLVMBuilderRef builder
= gallivm
->builder
;
207 LLVMValueRef i32undef
= LLVMGetUndef(LLVMInt32TypeInContext(gallivm
->context
));
208 LLVMValueRef anative
, bnative
;
209 unsigned intrin_length
= intr_size
/ src_type
.width
;
211 intrin_type
.length
= intrin_length
;
213 if (intrin_length
> src_type
.length
) {
214 LLVMValueRef elems
[LP_MAX_VECTOR_LENGTH
];
215 LLVMValueRef constvec
, tmp
;
217 for (i
= 0; i
< src_type
.length
; i
++) {
218 elems
[i
] = lp_build_const_int32(gallivm
, i
);
220 for (; i
< intrin_length
; i
++) {
223 if (src_type
.length
== 1) {
224 LLVMTypeRef elem_type
= lp_build_elem_type(gallivm
, intrin_type
);
225 a
= LLVMBuildBitCast(builder
, a
, LLVMVectorType(elem_type
, 1), "");
226 b
= LLVMBuildBitCast(builder
, b
, LLVMVectorType(elem_type
, 1), "");
228 constvec
= LLVMConstVector(elems
, intrin_length
);
229 anative
= LLVMBuildShuffleVector(builder
, a
, a
, constvec
, "");
230 bnative
= LLVMBuildShuffleVector(builder
, b
, b
, constvec
, "");
231 tmp
= lp_build_intrinsic_binary(builder
, name
,
232 lp_build_vec_type(gallivm
, intrin_type
),
234 if (src_type
.length
> 1) {
235 constvec
= LLVMConstVector(elems
, src_type
.length
);
236 return LLVMBuildShuffleVector(builder
, tmp
, tmp
, constvec
, "");
239 return LLVMBuildExtractElement(builder
, tmp
, elems
[0], "");
242 else if (intrin_length
< src_type
.length
) {
243 unsigned num_vec
= src_type
.length
/ intrin_length
;
244 LLVMValueRef tmp
[LP_MAX_VECTOR_LENGTH
];
246 /* don't support arbitrary size here as this is so yuck */
247 if (src_type
.length
% intrin_length
) {
248 /* FIXME: This is something which should be supported
249 * but there doesn't seem to be any need for it currently
252 debug_printf("%s: should handle arbitrary vector size\n",
258 for (i
= 0; i
< num_vec
; i
++) {
259 anative
= lp_build_extract_range(gallivm
, a
, i
*intrin_length
,
261 bnative
= lp_build_extract_range(gallivm
, b
, i
*intrin_length
,
263 tmp
[i
] = lp_build_intrinsic_binary(builder
, name
,
264 lp_build_vec_type(gallivm
, intrin_type
),
267 return lp_build_concat(gallivm
, tmp
, intrin_type
, num_vec
);
270 return lp_build_intrinsic_binary(builder
, name
,
271 lp_build_vec_type(gallivm
, src_type
),
278 lp_build_intrinsic_map(struct gallivm_state
*gallivm
,
280 LLVMTypeRef ret_type
,
284 LLVMBuilderRef builder
= gallivm
->builder
;
285 LLVMTypeRef ret_elem_type
= LLVMGetElementType(ret_type
);
286 unsigned n
= LLVMGetVectorSize(ret_type
);
290 assert(num_args
<= LP_MAX_FUNC_ARGS
);
292 res
= LLVMGetUndef(ret_type
);
293 for(i
= 0; i
< n
; ++i
) {
294 LLVMValueRef index
= lp_build_const_int32(gallivm
, i
);
295 LLVMValueRef arg_elems
[LP_MAX_FUNC_ARGS
];
296 LLVMValueRef res_elem
;
297 for(j
= 0; j
< num_args
; ++j
)
298 arg_elems
[j
] = LLVMBuildExtractElement(builder
, args
[j
], index
, "");
299 res_elem
= lp_build_intrinsic(builder
, name
, ret_elem_type
, arg_elems
, num_args
, 0);
300 res
= LLVMBuildInsertElement(builder
, res
, res_elem
, index
, "");
308 lp_build_intrinsic_map_unary(struct gallivm_state
*gallivm
,
310 LLVMTypeRef ret_type
,
313 return lp_build_intrinsic_map(gallivm
, name
, ret_type
, &a
, 1);
318 lp_build_intrinsic_map_binary(struct gallivm_state
*gallivm
,
320 LLVMTypeRef ret_type
,
324 LLVMValueRef args
[2];
329 return lp_build_intrinsic_map(gallivm
, name
, ret_type
, args
, 2);