Debian Codegraph @

src/gallium/auxiliary/gallivm/lp_bld_intr.c

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