1 /**************************************************************************
2 *
3 * Copyright 2009 VMware, Inc.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28
29 /**
30 * @file
31 * Helpers for emiting intrinsic calls.
32 *
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.
36 *
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.
39 *
40 * These functions are also used for calling C functions provided by us from
41 * generated LLVM code.
42 *
43 * @author Jose Fonseca <[email protected]>
44 */
45
46 #include <llvm/Config/llvm-config.h>
47
48 #include "util/u_debug.h"
49 #include "util/u_string.h"
50 #include "util/bitscan.h"
51
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"
57
58
59 void
lp_format_intrinsic(char * name,size_t size,const char * name_root,LLVMTypeRef type)60 lp_format_intrinsic(char *name,
61 size_t size,
62 const char *name_root,
63 LLVMTypeRef type)
64 {
65 unsigned length = 0;
66 unsigned width;
67 char c;
68
69 LLVMTypeKind kind = LLVMGetTypeKind(type);
70 if (kind == LLVMVectorTypeKind) {
71 length = LLVMGetVectorSize(type);
72 type = LLVMGetElementType(type);
73 kind = LLVMGetTypeKind(type);
74 }
75
76 switch (kind) {
77 case LLVMIntegerTypeKind:
78 c = 'i';
79 width = LLVMGetIntTypeWidth(type);
80 break;
81 case LLVMFloatTypeKind:
82 c = 'f';
83 width = 32;
84 break;
85 case LLVMDoubleTypeKind:
86 c = 'f';
87 width = 64;
88 break;
89 case LLVMHalfTypeKind:
90 c = 'f';
91 width = 16;
92 break;
93 default:
94 unreachable("unexpected LLVMTypeKind");
95 }
96
97 if (length) {
98 snprintf(name, size, "%s.v%u%c%u", name_root, length, c, width);
99 } else {
100 snprintf(name, size, "%s.%c%u", name_root, c, width);
101 }
102 }
103
104
105 LLVMValueRef
lp_declare_intrinsic_with_type(LLVMModuleRef module,const char * name,LLVMTypeRef function_type)106 lp_declare_intrinsic_with_type(LLVMModuleRef module,
107 const char *name,
108 LLVMTypeRef function_type)
109 {
110 assert(!LLVMGetNamedFunction(module, name));
111
112 LLVMValueRef function = LLVMAddFunction(module, name, function_type);
113
114 LLVMSetFunctionCallConv(function, LLVMCCallConv);
115 LLVMSetLinkage(function, LLVMExternalLinkage);
116
117 assert(LLVMIsDeclaration(function));
118
119 return function;
120 }
121
122
123 LLVMValueRef
lp_declare_intrinsic(LLVMModuleRef module,const char * name,LLVMTypeRef ret_type,LLVMTypeRef * arg_types,unsigned num_args)124 lp_declare_intrinsic(LLVMModuleRef module,
125 const char *name,
126 LLVMTypeRef ret_type,
127 LLVMTypeRef *arg_types,
128 unsigned num_args)
129 {
130 LLVMTypeRef function_type = LLVMFunctionType(ret_type, arg_types, num_args, 0);
131 return lp_declare_intrinsic_with_type(module, name, function_type);
132 }
133
attr_to_str(enum lp_func_attr attr)134 static const char *attr_to_str(enum lp_func_attr attr)
135 {
136 switch (attr) {
137 case LP_FUNC_ATTR_ALWAYSINLINE: return "alwaysinline";
138 case LP_FUNC_ATTR_INREG: return "inreg";
139 case LP_FUNC_ATTR_NOALIAS: return "noalias";
140 case LP_FUNC_ATTR_NOUNWIND: return "nounwind";
141 case LP_FUNC_ATTR_CONVERGENT: return "convergent";
142 case LP_FUNC_ATTR_PRESPLITCORO: return "presplitcoroutine";
143 default:
144 _debug_printf("Unhandled function attribute: %x\n", attr);
145 return 0;
146 }
147 }
148
149 void
lp_add_function_attr(LLVMValueRef function_or_call,int attr_idx,enum lp_func_attr attr)150 lp_add_function_attr(LLVMValueRef function_or_call,
151 int attr_idx, enum lp_func_attr attr)
152 {
153 LLVMModuleRef module;
154 if (LLVMIsAFunction(function_or_call)) {
155 module = LLVMGetGlobalParent(function_or_call);
156 } else {
157 LLVMBasicBlockRef bb = LLVMGetInstructionParent(function_or_call);
158 LLVMValueRef function = LLVMGetBasicBlockParent(bb);
159 module = LLVMGetGlobalParent(function);
160 }
161 LLVMContextRef ctx = LLVMGetModuleContext(module);
162
163 const char *attr_name = attr_to_str(attr);
164 unsigned kind_id = LLVMGetEnumAttributeKindForName(attr_name,
165 strlen(attr_name));
166 LLVMAttributeRef llvm_attr = LLVMCreateEnumAttribute(ctx, kind_id, 0);
167
168 if (LLVMIsAFunction(function_or_call))
169 LLVMAddAttributeAtIndex(function_or_call, attr_idx, llvm_attr);
170 else
171 LLVMAddCallSiteAttribute(function_or_call, attr_idx, llvm_attr);
172 }
173
174 static void
lp_add_func_attributes(LLVMValueRef function,unsigned attrib_mask)175 lp_add_func_attributes(LLVMValueRef function, unsigned attrib_mask)
176 {
177 /* NoUnwind indicates that the intrinsic never raises a C++ exception.
178 * Set it for all intrinsics.
179 */
180 attrib_mask |= LP_FUNC_ATTR_NOUNWIND;
181
182 while (attrib_mask) {
183 enum lp_func_attr attr = 1u << u_bit_scan(&attrib_mask);
184 lp_add_function_attr(function, -1, attr);
185 }
186 }
187
188 LLVMValueRef
lp_build_intrinsic(LLVMBuilderRef builder,const char * name,LLVMTypeRef ret_type,LLVMValueRef * args,unsigned num_args,unsigned attr_mask)189 lp_build_intrinsic(LLVMBuilderRef builder,
190 const char *name,
191 LLVMTypeRef ret_type,
192 LLVMValueRef *args,
193 unsigned num_args,
194 unsigned attr_mask)
195 {
196 LLVMModuleRef module = LLVMGetGlobalParent(LLVMGetBasicBlockParent(LLVMGetInsertBlock(builder)));
197 LLVMValueRef function, call;
198
199 LLVMTypeRef arg_types[LP_MAX_FUNC_ARGS];
200
201 assert(num_args <= LP_MAX_FUNC_ARGS);
202
203 for(unsigned i = 0; i < num_args; ++i) {
204 assert(args[i]);
205 arg_types[i] = LLVMTypeOf(args[i]);
206 }
207
208 LLVMTypeRef function_type = LLVMFunctionType(ret_type, arg_types, num_args, 0);
209
210 function = LLVMGetNamedFunction(module, name);
211
212 if(!function) {
213 function = lp_declare_intrinsic_with_type(module, name, function_type);
214
215 /*
216 * If llvm removes an intrinsic we use, we'll hit this abort (rather
217 * than a call to address zero in the jited code).
218 */
219 if (LLVMGetIntrinsicID(function) == 0) {
220 _debug_printf("llvm (version " MESA_LLVM_VERSION_STRING
221 ") found no intrinsic for %s, going to crash...\n",
222 name);
223 abort();
224 }
225
226 if (gallivm_debug & GALLIVM_DEBUG_IR) {
227 lp_debug_dump_value(function);
228 }
229 }
230
231 call = LLVMBuildCall2(builder, function_type, function, args, num_args, "");
232 lp_add_func_attributes(call, attr_mask);
233 return call;
234 }
235
236
237 LLVMValueRef
lp_build_intrinsic_unary(LLVMBuilderRef builder,const char * name,LLVMTypeRef ret_type,LLVMValueRef a)238 lp_build_intrinsic_unary(LLVMBuilderRef builder,
239 const char *name,
240 LLVMTypeRef ret_type,
241 LLVMValueRef a)
242 {
243 return lp_build_intrinsic(builder, name, ret_type, &a, 1, 0);
244 }
245
246
247 LLVMValueRef
lp_build_intrinsic_binary(LLVMBuilderRef builder,const char * name,LLVMTypeRef ret_type,LLVMValueRef a,LLVMValueRef b)248 lp_build_intrinsic_binary(LLVMBuilderRef builder,
249 const char *name,
250 LLVMTypeRef ret_type,
251 LLVMValueRef a,
252 LLVMValueRef b)
253 {
254 LLVMValueRef args[2];
255
256 args[0] = a;
257 args[1] = b;
258
259 return lp_build_intrinsic(builder, name, ret_type, args, 2, 0);
260 }
261
262
263 /**
264 * Call intrinsic with arguments adapted to intrinsic vector length.
265 *
266 * Split vectors which are too large for the hw, or expand them if they
267 * are too small, so a caller calling a function which might use intrinsics
268 * doesn't need to do splitting/expansion on its own.
269 * This only supports intrinsics where src and dst types match.
270 */
271 LLVMValueRef
lp_build_intrinsic_binary_anylength(struct gallivm_state * gallivm,const char * name,struct lp_type src_type,unsigned intr_size,LLVMValueRef a,LLVMValueRef b)272 lp_build_intrinsic_binary_anylength(struct gallivm_state *gallivm,
273 const char *name,
274 struct lp_type src_type,
275 unsigned intr_size,
276 LLVMValueRef a,
277 LLVMValueRef b)
278 {
279 unsigned i;
280 struct lp_type intrin_type = src_type;
281 LLVMBuilderRef builder = gallivm->builder;
282 LLVMValueRef i32undef = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
283 LLVMValueRef anative, bnative;
284 unsigned intrin_length = intr_size / src_type.width;
285
286 intrin_type.length = intrin_length;
287
288 if (intrin_length > src_type.length) {
289 LLVMValueRef elems[LP_MAX_VECTOR_LENGTH];
290 LLVMValueRef constvec, tmp;
291
292 for (i = 0; i < src_type.length; i++) {
293 elems[i] = lp_build_const_int32(gallivm, i);
294 }
295 for (; i < intrin_length; i++) {
296 elems[i] = i32undef;
297 }
298 if (src_type.length == 1) {
299 LLVMTypeRef elem_type = lp_build_elem_type(gallivm, intrin_type);
300 a = LLVMBuildBitCast(builder, a, LLVMVectorType(elem_type, 1), "");
301 b = LLVMBuildBitCast(builder, b, LLVMVectorType(elem_type, 1), "");
302 }
303 constvec = LLVMConstVector(elems, intrin_length);
304 anative = LLVMBuildShuffleVector(builder, a, a, constvec, "");
305 bnative = LLVMBuildShuffleVector(builder, b, b, constvec, "");
306 tmp = lp_build_intrinsic_binary(builder, name,
307 lp_build_vec_type(gallivm, intrin_type),
308 anative, bnative);
309 if (src_type.length > 1) {
310 constvec = LLVMConstVector(elems, src_type.length);
311 return LLVMBuildShuffleVector(builder, tmp, tmp, constvec, "");
312 }
313 else {
314 return LLVMBuildExtractElement(builder, tmp, elems[0], "");
315 }
316 }
317 else if (intrin_length < src_type.length) {
318 unsigned num_vec = src_type.length / intrin_length;
319 LLVMValueRef tmp[LP_MAX_VECTOR_LENGTH];
320
321 /* don't support arbitrary size here as this is so yuck */
322 if (src_type.length % intrin_length) {
323 /* FIXME: This is something which should be supported
324 * but there doesn't seem to be any need for it currently
325 * so crash and burn.
326 */
327 debug_printf("%s: should handle arbitrary vector size\n",
328 __func__);
329 assert(0);
330 return NULL;
331 }
332
333 for (i = 0; i < num_vec; i++) {
334 anative = lp_build_extract_range(gallivm, a, i*intrin_length,
335 intrin_length);
336 bnative = lp_build_extract_range(gallivm, b, i*intrin_length,
337 intrin_length);
338 tmp[i] = lp_build_intrinsic_binary(builder, name,
339 lp_build_vec_type(gallivm, intrin_type),
340 anative, bnative);
341 }
342 return lp_build_concat(gallivm, tmp, intrin_type, num_vec);
343 }
344 else {
345 return lp_build_intrinsic_binary(builder, name,
346 lp_build_vec_type(gallivm, src_type),
347 a, b);
348 }
349 }
350
351
352 LLVMValueRef
lp_build_intrinsic_map(struct gallivm_state * gallivm,const char * name,LLVMTypeRef ret_type,LLVMValueRef * args,unsigned num_args)353 lp_build_intrinsic_map(struct gallivm_state *gallivm,
354 const char *name,
355 LLVMTypeRef ret_type,
356 LLVMValueRef *args,
357 unsigned num_args)
358 {
359 LLVMBuilderRef builder = gallivm->builder;
360 LLVMTypeRef ret_elem_type = LLVMGetElementType(ret_type);
361 unsigned n = LLVMGetVectorSize(ret_type);
362 unsigned i, j;
363 LLVMValueRef res;
364
365 assert(num_args <= LP_MAX_FUNC_ARGS);
366
367 res = LLVMGetUndef(ret_type);
368 for(i = 0; i < n; ++i) {
369 LLVMValueRef index = lp_build_const_int32(gallivm, i);
370 LLVMValueRef arg_elems[LP_MAX_FUNC_ARGS];
371 LLVMValueRef res_elem;
372 for(j = 0; j < num_args; ++j)
373 arg_elems[j] = LLVMBuildExtractElement(builder, args[j], index, "");
374 res_elem = lp_build_intrinsic(builder, name, ret_elem_type, arg_elems, num_args, 0);
375 res = LLVMBuildInsertElement(builder, res, res_elem, index, "");
376 }
377
378 return res;
379 }
380
381
382 LLVMValueRef
lp_build_intrinsic_map_unary(struct gallivm_state * gallivm,const char * name,LLVMTypeRef ret_type,LLVMValueRef a)383 lp_build_intrinsic_map_unary(struct gallivm_state *gallivm,
384 const char *name,
385 LLVMTypeRef ret_type,
386 LLVMValueRef a)
387 {
388 return lp_build_intrinsic_map(gallivm, name, ret_type, &a, 1);
389 }
390
391
392 LLVMValueRef
lp_build_intrinsic_map_binary(struct gallivm_state * gallivm,const char * name,LLVMTypeRef ret_type,LLVMValueRef a,LLVMValueRef b)393 lp_build_intrinsic_map_binary(struct gallivm_state *gallivm,
394 const char *name,
395 LLVMTypeRef ret_type,
396 LLVMValueRef a,
397 LLVMValueRef b)
398 {
399 LLVMValueRef args[2];
400
401 args[0] = a;
402 args[1] = b;
403
404 return lp_build_intrinsic_map(gallivm, name, ret_type, args, 2);
405 }
406
407
408