blob: e1ae496d9cbca2c3c45db35ebda59dcc8d17e2a6 [file] [log] [blame]
Stanislav Mekhanoshin7f377942017-08-11 16:42:09 +00001//===-- AMDGPULibFunc.cpp -------------------------------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Stanislav Mekhanoshin7f377942017-08-11 16:42:09 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file contains utility functions to work with Itanium mangled names
10//
11//===----------------------------------------------------------------------===//
12
Yaxun Liu0d9673c2017-11-04 17:37:43 +000013#include "AMDGPU.h"
Stanislav Mekhanoshin7f377942017-08-11 16:42:09 +000014#include "AMDGPULibFunc.h"
15#include <llvm/ADT/SmallString.h>
16#include <llvm/ADT/SmallVector.h>
17#include <llvm/ADT/StringSwitch.h>
18#include "llvm/IR/Attributes.h"
19#include "llvm/IR/DerivedTypes.h"
20#include "llvm/IR/Function.h"
21#include "llvm/IR/Module.h"
22#include "llvm/IR/ValueSymbolTable.h"
23#include <llvm/Support/raw_ostream.h>
24#include <string>
25
26using namespace llvm;
27
28namespace {
29
30enum EManglingParam {
31 E_NONE,
32 EX_EVENT,
33 EX_FLOAT4,
34 EX_INTV4,
35 EX_RESERVEDID,
36 EX_SAMPLER,
37 EX_SIZET,
38 EX_UINT,
39 EX_UINTV4,
40 E_ANY,
41 E_CONSTPTR_ANY,
42 E_CONSTPTR_SWAPGL,
43 E_COPY,
44 E_IMAGECOORDS,
45 E_POINTEE,
46 E_SETBASE_I32,
47 E_SETBASE_U32,
48 E_MAKEBASE_UNS,
49 E_V16_OF_POINTEE,
50 E_V2_OF_POINTEE,
51 E_V3_OF_POINTEE,
52 E_V4_OF_POINTEE,
53 E_V8_OF_POINTEE,
54 E_VLTLPTR_ANY,
55};
56
57struct ManglingRule {
Benjamin Kramer55e8c912019-08-25 12:47:31 +000058 const char *Name;
Stanislav Mekhanoshin7f377942017-08-11 16:42:09 +000059 unsigned char Lead[2];
60 unsigned char Param[5];
61
62 int maxLeadIndex() const { return (std::max)(Lead[0], Lead[1]); }
63 int getNumLeads() const { return (Lead[0] ? 1 : 0) + (Lead[1] ? 1 : 0); }
64
65 unsigned getNumArgs() const;
Reid Kleckner85e2cda2019-03-28 17:33:41 +000066
67 static StringMap<int> buildManglingRulesMap();
Stanislav Mekhanoshin7f377942017-08-11 16:42:09 +000068};
69
Yaxun Liufc5121a2017-09-06 00:30:27 +000070// Information about library functions with unmangled names.
71class UnmangledFuncInfo {
Benjamin Kramer55e8c912019-08-25 12:47:31 +000072 const char *Name;
Yaxun Liufc5121a2017-09-06 00:30:27 +000073 unsigned NumArgs;
74
75 // Table for all lib functions with unmangled names.
76 static const UnmangledFuncInfo Table[];
77
78 // Number of entries in Table.
79 static const unsigned TableSize;
80
Reid Kleckner85e2cda2019-03-28 17:33:41 +000081 static StringMap<unsigned> buildNameMap();
Yaxun Liufc5121a2017-09-06 00:30:27 +000082
83public:
84 using ID = AMDGPULibFunc::EFuncId;
Benjamin Kramer55e8c912019-08-25 12:47:31 +000085 constexpr UnmangledFuncInfo(const char *_Name, unsigned _NumArgs)
Yaxun Liufc5121a2017-09-06 00:30:27 +000086 : Name(_Name), NumArgs(_NumArgs) {}
87 // Get index to Table by function name.
88 static bool lookup(StringRef Name, ID &Id);
89 static unsigned toIndex(ID Id) {
90 assert(static_cast<unsigned>(Id) >
91 static_cast<unsigned>(AMDGPULibFunc::EI_LAST_MANGLED) &&
92 "Invalid unmangled library function");
93 return static_cast<unsigned>(Id) - 1 -
94 static_cast<unsigned>(AMDGPULibFunc::EI_LAST_MANGLED);
95 }
96 static ID toFuncId(unsigned Index) {
Reid Kleckner85e2cda2019-03-28 17:33:41 +000097 assert(Index < TableSize &&
98 "Invalid unmangled library function");
Yaxun Liufc5121a2017-09-06 00:30:27 +000099 return static_cast<ID>(
100 Index + 1 + static_cast<unsigned>(AMDGPULibFunc::EI_LAST_MANGLED));
101 }
102 static unsigned getNumArgs(ID Id) { return Table[toIndex(Id)].NumArgs; }
103 static StringRef getName(ID Id) { return Table[toIndex(Id)].Name; }
104};
105
Stanislav Mekhanoshin7f377942017-08-11 16:42:09 +0000106unsigned ManglingRule::getNumArgs() const {
107 unsigned I=0;
108 while (I < (sizeof Param/sizeof Param[0]) && Param[I]) ++I;
109 return I;
110}
111
112// This table describes function formal argument type rules. The order of rules
113// corresponds to the EFuncId enum at AMDGPULibFunc.h
114//
115// "<func name>", { <leads> }, { <param rules> }
116// where:
117// <leads> - list of integers that are one-based indexes of formal argument
118// used to mangle a function name. Other argument types are derived from types
119// of these 'leads'. The order of integers in this list correspond to the
120// order in which these arguments are mangled in the EDG mangling scheme. The
121// same order should be preserved for arguments in the AMDGPULibFunc structure
122// when it is used for mangling. For example:
123// { "vstorea_half", {3,1}, {E_ANY,EX_SIZET,E_ANY}},
124// will be mangled in EDG scheme as vstorea_half_<3dparam>_<1stparam>
125// When mangling from code use:
126// AMDGPULibFunc insc;
127// insc.param[0] = ... // describe 3rd parameter
128// insc.param[1] = ... // describe 1rd parameter
129//
130// <param rules> - list of rules used to derive all of the function formal
131// argument types. EX_ prefixed are simple types, other derived from the
132// latest 'lead' argument type in the order of encoding from first to last.
133// E_ANY - use prev lead type, E_CONSTPTR_ANY - make const pointer out of
134// prev lead type, etc. see ParamIterator::getNextParam() for details.
135
Benjamin Kramer70434772019-08-24 15:24:25 +0000136static constexpr ManglingRule manglingRules[] = {
137{ "", {0}, {0} },
Stanislav Mekhanoshin7f377942017-08-11 16:42:09 +0000138{ "abs" , {1}, {E_ANY}},
139{ "abs_diff" , {1}, {E_ANY,E_COPY}},
140{ "acos" , {1}, {E_ANY}},
141{ "acosh" , {1}, {E_ANY}},
142{ "acospi" , {1}, {E_ANY}},
143{ "add_sat" , {1}, {E_ANY,E_COPY}},
144{ "all" , {1}, {E_ANY}},
145{ "any" , {1}, {E_ANY}},
146{ "asin" , {1}, {E_ANY}},
147{ "asinh" , {1}, {E_ANY}},
148{ "asinpi" , {1}, {E_ANY}},
149{ "async_work_group_copy" , {1}, {E_ANY,E_CONSTPTR_SWAPGL,EX_SIZET,EX_EVENT}},
150{ "async_work_group_strided_copy" , {1}, {E_ANY,E_CONSTPTR_SWAPGL,EX_SIZET,EX_SIZET,EX_EVENT}},
151{ "atan" , {1}, {E_ANY}},
152{ "atan2" , {1}, {E_ANY,E_COPY}},
153{ "atan2pi" , {1}, {E_ANY,E_COPY}},
154{ "atanh" , {1}, {E_ANY}},
155{ "atanpi" , {1}, {E_ANY}},
156{ "atomic_add" , {1}, {E_VLTLPTR_ANY,E_POINTEE}},
157{ "atomic_and" , {1}, {E_VLTLPTR_ANY,E_POINTEE}},
158{ "atomic_cmpxchg" , {1}, {E_VLTLPTR_ANY,E_POINTEE,E_POINTEE}},
159{ "atomic_dec" , {1}, {E_VLTLPTR_ANY}},
160{ "atomic_inc" , {1}, {E_VLTLPTR_ANY}},
161{ "atomic_max" , {1}, {E_VLTLPTR_ANY,E_POINTEE}},
162{ "atomic_min" , {1}, {E_VLTLPTR_ANY,E_POINTEE}},
163{ "atomic_or" , {1}, {E_VLTLPTR_ANY,E_POINTEE}},
164{ "atomic_sub" , {1}, {E_VLTLPTR_ANY,E_POINTEE}},
165{ "atomic_xchg" , {1}, {E_VLTLPTR_ANY,E_POINTEE}},
166{ "atomic_xor" , {1}, {E_VLTLPTR_ANY,E_POINTEE}},
167{ "bitselect" , {1}, {E_ANY,E_COPY,E_COPY}},
168{ "cbrt" , {1}, {E_ANY}},
169{ "ceil" , {1}, {E_ANY}},
170{ "clamp" , {1}, {E_ANY,E_COPY,E_COPY}},
171{ "clz" , {1}, {E_ANY}},
172{ "commit_read_pipe" , {1}, {E_ANY,EX_RESERVEDID}},
173{ "commit_write_pipe" , {1}, {E_ANY,EX_RESERVEDID}},
174{ "copysign" , {1}, {E_ANY,E_COPY}},
175{ "cos" , {1}, {E_ANY}},
176{ "cosh" , {1}, {E_ANY}},
177{ "cospi" , {1}, {E_ANY}},
178{ "cross" , {1}, {E_ANY,E_COPY}},
179{ "ctz" , {1}, {E_ANY}},
180{ "degrees" , {1}, {E_ANY}},
181{ "distance" , {1}, {E_ANY,E_COPY}},
182{ "divide" , {1}, {E_ANY,E_COPY}},
183{ "dot" , {1}, {E_ANY,E_COPY}},
184{ "erf" , {1}, {E_ANY}},
185{ "erfc" , {1}, {E_ANY}},
186{ "exp" , {1}, {E_ANY}},
187{ "exp10" , {1}, {E_ANY}},
188{ "exp2" , {1}, {E_ANY}},
189{ "expm1" , {1}, {E_ANY}},
190{ "fabs" , {1}, {E_ANY}},
191{ "fast_distance" , {1}, {E_ANY,E_COPY}},
192{ "fast_length" , {1}, {E_ANY}},
193{ "fast_normalize" , {1}, {E_ANY}},
194{ "fdim" , {1}, {E_ANY,E_COPY}},
195{ "floor" , {1}, {E_ANY}},
196{ "fma" , {1}, {E_ANY,E_COPY,E_COPY}},
197{ "fmax" , {1}, {E_ANY,E_COPY}},
198{ "fmin" , {1}, {E_ANY,E_COPY}},
199{ "fmod" , {1}, {E_ANY,E_COPY}},
200{ "fract" , {2}, {E_POINTEE,E_ANY}},
201{ "frexp" , {1,2}, {E_ANY,E_ANY}},
202{ "get_image_array_size" , {1}, {E_ANY}},
203{ "get_image_channel_data_type" , {1}, {E_ANY}},
204{ "get_image_channel_order" , {1}, {E_ANY}},
205{ "get_image_dim" , {1}, {E_ANY}},
206{ "get_image_height" , {1}, {E_ANY}},
207{ "get_image_width" , {1}, {E_ANY}},
208{ "get_pipe_max_packets" , {1}, {E_ANY}},
209{ "get_pipe_num_packets" , {1}, {E_ANY}},
210{ "hadd" , {1}, {E_ANY,E_COPY}},
211{ "hypot" , {1}, {E_ANY,E_COPY}},
212{ "ilogb" , {1}, {E_ANY}},
213{ "isequal" , {1}, {E_ANY,E_COPY}},
214{ "isfinite" , {1}, {E_ANY}},
215{ "isgreater" , {1}, {E_ANY,E_COPY}},
216{ "isgreaterequal" , {1}, {E_ANY,E_COPY}},
217{ "isinf" , {1}, {E_ANY}},
218{ "isless" , {1}, {E_ANY,E_COPY}},
219{ "islessequal" , {1}, {E_ANY,E_COPY}},
220{ "islessgreater" , {1}, {E_ANY,E_COPY}},
221{ "isnan" , {1}, {E_ANY}},
222{ "isnormal" , {1}, {E_ANY}},
223{ "isnotequal" , {1}, {E_ANY,E_COPY}},
224{ "isordered" , {1}, {E_ANY,E_COPY}},
225{ "isunordered" , {1}, {E_ANY,E_COPY}},
226{ "ldexp" , {1}, {E_ANY,E_SETBASE_I32}},
227{ "length" , {1}, {E_ANY}},
228{ "lgamma" , {1}, {E_ANY}},
229{ "lgamma_r" , {1,2}, {E_ANY,E_ANY}},
230{ "log" , {1}, {E_ANY}},
231{ "log10" , {1}, {E_ANY}},
232{ "log1p" , {1}, {E_ANY}},
233{ "log2" , {1}, {E_ANY}},
234{ "logb" , {1}, {E_ANY}},
235{ "mad" , {1}, {E_ANY,E_COPY,E_COPY}},
236{ "mad24" , {1}, {E_ANY,E_COPY,E_COPY}},
237{ "mad_hi" , {1}, {E_ANY,E_COPY,E_COPY}},
238{ "mad_sat" , {1}, {E_ANY,E_COPY,E_COPY}},
239{ "max" , {1}, {E_ANY,E_COPY}},
240{ "maxmag" , {1}, {E_ANY,E_COPY}},
241{ "min" , {1}, {E_ANY,E_COPY}},
242{ "minmag" , {1}, {E_ANY,E_COPY}},
243{ "mix" , {1}, {E_ANY,E_COPY,E_COPY}},
244{ "modf" , {2}, {E_POINTEE,E_ANY}},
245{ "mul24" , {1}, {E_ANY,E_COPY}},
246{ "mul_hi" , {1}, {E_ANY,E_COPY}},
247{ "nan" , {1}, {E_ANY}},
248{ "nextafter" , {1}, {E_ANY,E_COPY}},
249{ "normalize" , {1}, {E_ANY}},
250{ "popcount" , {1}, {E_ANY}},
251{ "pow" , {1}, {E_ANY,E_COPY}},
252{ "pown" , {1}, {E_ANY,E_SETBASE_I32}},
253{ "powr" , {1}, {E_ANY,E_COPY}},
254{ "prefetch" , {1}, {E_CONSTPTR_ANY,EX_SIZET}},
255{ "radians" , {1}, {E_ANY}},
Stanislav Mekhanoshin7f377942017-08-11 16:42:09 +0000256{ "recip" , {1}, {E_ANY}},
257{ "remainder" , {1}, {E_ANY,E_COPY}},
258{ "remquo" , {1,3}, {E_ANY,E_COPY,E_ANY}},
259{ "reserve_read_pipe" , {1}, {E_ANY,EX_UINT}},
260{ "reserve_write_pipe" , {1}, {E_ANY,EX_UINT}},
261{ "rhadd" , {1}, {E_ANY,E_COPY}},
262{ "rint" , {1}, {E_ANY}},
263{ "rootn" , {1}, {E_ANY,E_SETBASE_I32}},
264{ "rotate" , {1}, {E_ANY,E_COPY}},
265{ "round" , {1}, {E_ANY}},
266{ "rsqrt" , {1}, {E_ANY}},
267{ "select" , {1,3}, {E_ANY,E_COPY,E_ANY}},
268{ "shuffle" , {1,2}, {E_ANY,E_ANY}},
269{ "shuffle2" , {1,3}, {E_ANY,E_COPY,E_ANY}},
270{ "sign" , {1}, {E_ANY}},
271{ "signbit" , {1}, {E_ANY}},
272{ "sin" , {1}, {E_ANY}},
273{ "sincos" , {2}, {E_POINTEE,E_ANY}},
274{ "sinh" , {1}, {E_ANY}},
275{ "sinpi" , {1}, {E_ANY}},
276{ "smoothstep" , {1}, {E_ANY,E_COPY,E_COPY}},
277{ "sqrt" , {1}, {E_ANY}},
278{ "step" , {1}, {E_ANY,E_COPY}},
279{ "sub_group_broadcast" , {1}, {E_ANY,EX_UINT}},
280{ "sub_group_commit_read_pipe" , {1}, {E_ANY,EX_RESERVEDID}},
281{ "sub_group_commit_write_pipe" , {1}, {E_ANY,EX_RESERVEDID}},
282{ "sub_group_reduce_add" , {1}, {E_ANY}},
283{ "sub_group_reduce_max" , {1}, {E_ANY}},
284{ "sub_group_reduce_min" , {1}, {E_ANY}},
285{ "sub_group_reserve_read_pipe" , {1}, {E_ANY,EX_UINT}},
286{ "sub_group_reserve_write_pipe" , {1}, {E_ANY,EX_UINT}},
287{ "sub_group_scan_exclusive_add" , {1}, {E_ANY}},
288{ "sub_group_scan_exclusive_max" , {1}, {E_ANY}},
289{ "sub_group_scan_exclusive_min" , {1}, {E_ANY}},
290{ "sub_group_scan_inclusive_add" , {1}, {E_ANY}},
291{ "sub_group_scan_inclusive_max" , {1}, {E_ANY}},
292{ "sub_group_scan_inclusive_min" , {1}, {E_ANY}},
293{ "sub_sat" , {1}, {E_ANY,E_COPY}},
294{ "tan" , {1}, {E_ANY}},
295{ "tanh" , {1}, {E_ANY}},
296{ "tanpi" , {1}, {E_ANY}},
297{ "tgamma" , {1}, {E_ANY}},
298{ "trunc" , {1}, {E_ANY}},
299{ "upsample" , {1}, {E_ANY,E_MAKEBASE_UNS}},
300{ "vec_step" , {1}, {E_ANY}},
301{ "vstore" , {3}, {E_POINTEE,EX_SIZET,E_ANY}},
302{ "vstore16" , {3}, {E_V16_OF_POINTEE,EX_SIZET,E_ANY}},
303{ "vstore2" , {3}, {E_V2_OF_POINTEE,EX_SIZET,E_ANY}},
304{ "vstore3" , {3}, {E_V3_OF_POINTEE,EX_SIZET,E_ANY}},
305{ "vstore4" , {3}, {E_V4_OF_POINTEE,EX_SIZET,E_ANY}},
306{ "vstore8" , {3}, {E_V8_OF_POINTEE,EX_SIZET,E_ANY}},
307{ "work_group_commit_read_pipe" , {1}, {E_ANY,EX_RESERVEDID}},
308{ "work_group_commit_write_pipe" , {1}, {E_ANY,EX_RESERVEDID}},
309{ "work_group_reduce_add" , {1}, {E_ANY}},
310{ "work_group_reduce_max" , {1}, {E_ANY}},
311{ "work_group_reduce_min" , {1}, {E_ANY}},
312{ "work_group_reserve_read_pipe" , {1}, {E_ANY,EX_UINT}},
313{ "work_group_reserve_write_pipe" , {1}, {E_ANY,EX_UINT}},
314{ "work_group_scan_exclusive_add" , {1}, {E_ANY}},
315{ "work_group_scan_exclusive_max" , {1}, {E_ANY}},
316{ "work_group_scan_exclusive_min" , {1}, {E_ANY}},
317{ "work_group_scan_inclusive_add" , {1}, {E_ANY}},
318{ "work_group_scan_inclusive_max" , {1}, {E_ANY}},
319{ "work_group_scan_inclusive_min" , {1}, {E_ANY}},
320{ "write_imagef" , {1}, {E_ANY,E_IMAGECOORDS,EX_FLOAT4}},
321{ "write_imagei" , {1}, {E_ANY,E_IMAGECOORDS,EX_INTV4}},
322{ "write_imageui" , {1}, {E_ANY,E_IMAGECOORDS,EX_UINTV4}},
Stanislav Mekhanoshin7f377942017-08-11 16:42:09 +0000323{ "ncos" , {1}, {E_ANY} },
324{ "nexp2" , {1}, {E_ANY} },
325{ "nfma" , {1}, {E_ANY, E_COPY, E_COPY} },
326{ "nlog2" , {1}, {E_ANY} },
327{ "nrcp" , {1}, {E_ANY} },
328{ "nrsqrt" , {1}, {E_ANY} },
329{ "nsin" , {1}, {E_ANY} },
330{ "nsqrt" , {1}, {E_ANY} },
331{ "ftz" , {1}, {E_ANY} },
332{ "fldexp" , {1}, {E_ANY, EX_UINT} },
333{ "class" , {1}, {E_ANY, EX_UINT} },
334{ "rcbrt" , {1}, {E_ANY} },
335};
336
Yaxun Liufc5121a2017-09-06 00:30:27 +0000337// Library functions with unmangled name.
338const UnmangledFuncInfo UnmangledFuncInfo::Table[] = {
339 {"__read_pipe_2", 4},
340 {"__read_pipe_4", 6},
341 {"__write_pipe_2", 4},
342 {"__write_pipe_4", 6},
343};
344
345const unsigned UnmangledFuncInfo::TableSize =
Reid Kleckner85e2cda2019-03-28 17:33:41 +0000346 array_lengthof(UnmangledFuncInfo::Table);
Stanislav Mekhanoshin7f377942017-08-11 16:42:09 +0000347
348static AMDGPULibFunc::Param getRetType(AMDGPULibFunc::EFuncId id,
349 const AMDGPULibFunc::Param (&Leads)[2]) {
350 AMDGPULibFunc::Param Res = Leads[0];
351 // TBD - This switch may require to be extended for other intriniscs
352 switch (id) {
353 case AMDGPULibFunc::EI_SINCOS:
354 Res.PtrKind = AMDGPULibFunc::BYVALUE;
355 break;
356 default:
357 break;
358 }
359 return Res;
360}
361
362class ParamIterator {
363 const AMDGPULibFunc::Param (&Leads)[2];
364 const ManglingRule& Rule;
365 int Index;
366public:
367 ParamIterator(const AMDGPULibFunc::Param (&leads)[2],
368 const ManglingRule& rule)
369 : Leads(leads), Rule(rule), Index(0) {}
370
371 AMDGPULibFunc::Param getNextParam();
372};
373
374AMDGPULibFunc::Param ParamIterator::getNextParam() {
375 AMDGPULibFunc::Param P;
376 if (Index >= int(sizeof Rule.Param/sizeof Rule.Param[0])) return P;
377
378 const char R = Rule.Param[Index];
379 switch (R) {
380 case E_NONE: break;
381 case EX_UINT:
382 P.ArgType = AMDGPULibFunc::U32; break;
383 case EX_INTV4:
384 P.ArgType = AMDGPULibFunc::I32; P.VectorSize = 4; break;
385 case EX_UINTV4:
386 P.ArgType = AMDGPULibFunc::U32; P.VectorSize = 4; break;
387 case EX_FLOAT4:
388 P.ArgType = AMDGPULibFunc::F32; P.VectorSize = 4; break;
389 case EX_SIZET:
390 P.ArgType = AMDGPULibFunc::U64; break;
391 case EX_EVENT:
392 P.ArgType = AMDGPULibFunc::EVENT; break;
393 case EX_SAMPLER:
394 P.ArgType = AMDGPULibFunc::SAMPLER; break;
395 case EX_RESERVEDID: break; // TBD
396 default:
397 if (Index == (Rule.Lead[1] - 1)) P = Leads[1];
398 else P = Leads[0];
399
400 switch (R) {
401 case E_ANY:
402 case E_COPY: break;
403
404 case E_POINTEE:
405 P.PtrKind = AMDGPULibFunc::BYVALUE; break;
406 case E_V2_OF_POINTEE:
407 P.VectorSize = 2; P.PtrKind = AMDGPULibFunc::BYVALUE; break;
408 case E_V3_OF_POINTEE:
409 P.VectorSize = 3; P.PtrKind = AMDGPULibFunc::BYVALUE; break;
410 case E_V4_OF_POINTEE:
411 P.VectorSize = 4; P.PtrKind = AMDGPULibFunc::BYVALUE; break;
412 case E_V8_OF_POINTEE:
413 P.VectorSize = 8; P.PtrKind = AMDGPULibFunc::BYVALUE; break;
414 case E_V16_OF_POINTEE:
415 P.VectorSize = 16; P.PtrKind = AMDGPULibFunc::BYVALUE; break;
416 case E_CONSTPTR_ANY:
417 P.PtrKind |= AMDGPULibFunc::CONST; break;
418 case E_VLTLPTR_ANY:
419 P.PtrKind |= AMDGPULibFunc::VOLATILE; break;
420 case E_SETBASE_I32:
421 P.ArgType = AMDGPULibFunc::I32; break;
422 case E_SETBASE_U32:
423 P.ArgType = AMDGPULibFunc::U32; break;
424
425 case E_MAKEBASE_UNS:
426 P.ArgType &= ~AMDGPULibFunc::BASE_TYPE_MASK;
427 P.ArgType |= AMDGPULibFunc::UINT;
428 break;
429
430 case E_IMAGECOORDS:
431 switch (P.ArgType) {
432 case AMDGPULibFunc::IMG1DA: P.VectorSize = 2; break;
433 case AMDGPULibFunc::IMG1DB: P.VectorSize = 1; break;
434 case AMDGPULibFunc::IMG2DA: P.VectorSize = 4; break;
435 case AMDGPULibFunc::IMG1D: P.VectorSize = 1; break;
436 case AMDGPULibFunc::IMG2D: P.VectorSize = 2; break;
437 case AMDGPULibFunc::IMG3D: P.VectorSize = 4; break;
438 }
439 P.PtrKind = AMDGPULibFunc::BYVALUE;
440 P.ArgType = AMDGPULibFunc::I32;
441 break;
442
Yaxun Liu0d9673c2017-11-04 17:37:43 +0000443 case E_CONSTPTR_SWAPGL: {
444 unsigned AS = AMDGPULibFunc::getAddrSpaceFromEPtrKind(P.PtrKind);
445 switch (AS) {
446 case AMDGPUAS::GLOBAL_ADDRESS: AS = AMDGPUAS::LOCAL_ADDRESS; break;
447 case AMDGPUAS::LOCAL_ADDRESS: AS = AMDGPUAS::GLOBAL_ADDRESS; break;
Stanislav Mekhanoshin7f377942017-08-11 16:42:09 +0000448 }
Yaxun Liu0d9673c2017-11-04 17:37:43 +0000449 P.PtrKind = AMDGPULibFunc::getEPtrKindFromAddrSpace(AS);
Stanislav Mekhanoshin7f377942017-08-11 16:42:09 +0000450 P.PtrKind |= AMDGPULibFunc::CONST;
451 break;
Yaxun Liu0d9673c2017-11-04 17:37:43 +0000452 }
Stanislav Mekhanoshin7f377942017-08-11 16:42:09 +0000453
454 default: llvm_unreachable("Unhandeled param rule");
455 }
456 }
457 ++Index;
458 return P;
459}
460
461inline static void drop_front(StringRef& str, size_t n = 1) {
462 str = str.drop_front(n);
463}
464
465static bool eatTerm(StringRef& mangledName, const char c) {
466 if (mangledName.front() == c) {
467 drop_front(mangledName);
468 return true;
469 }
470 return false;
471}
472
473template <size_t N>
474static bool eatTerm(StringRef& mangledName, const char (&str)[N]) {
475 if (mangledName.startswith(StringRef(str, N-1))) {
476 drop_front(mangledName, N-1);
477 return true;
478 }
479 return false;
480}
481
482static inline bool isDigit(char c) { return c >= '0' && c <= '9'; }
483
484static int eatNumber(StringRef& s) {
485 size_t const savedSize = s.size();
486 int n = 0;
487 while (!s.empty() && isDigit(s.front())) {
488 n = n*10 + s.front() - '0';
489 drop_front(s);
490 }
491 return s.size() < savedSize ? n : -1;
492}
493
494static StringRef eatLengthPrefixedName(StringRef& mangledName) {
495 int const Len = eatNumber(mangledName);
496 if (Len <= 0 || static_cast<size_t>(Len) > mangledName.size())
497 return StringRef();
498 StringRef Res = mangledName.substr(0, Len);
499 drop_front(mangledName, Len);
500 return Res;
501}
502
503} // end anonymous namespace
504
Yaxun Liufc5121a2017-09-06 00:30:27 +0000505AMDGPUMangledLibFunc::AMDGPUMangledLibFunc() {
Stanislav Mekhanoshin7f377942017-08-11 16:42:09 +0000506 FuncId = EI_NONE;
507 FKind = NOPFX;
508 Leads[0].reset();
509 Leads[1].reset();
510 Name.clear();
511}
512
Yaxun Liufc5121a2017-09-06 00:30:27 +0000513AMDGPUUnmangledLibFunc::AMDGPUUnmangledLibFunc() {
514 FuncId = EI_NONE;
515 FuncTy = nullptr;
516}
517
518AMDGPUMangledLibFunc::AMDGPUMangledLibFunc(
519 EFuncId id, const AMDGPUMangledLibFunc &copyFrom) {
520 FuncId = id;
521 FKind = copyFrom.FKind;
522 Leads[0] = copyFrom.Leads[0];
523 Leads[1] = copyFrom.Leads[1];
524}
525
Stanislav Mekhanoshin7f377942017-08-11 16:42:09 +0000526///////////////////////////////////////////////////////////////////////////////
527// Demangling
528
529static int parseVecSize(StringRef& mangledName) {
530 size_t const Len = eatNumber(mangledName);
531 switch (Len) {
532 case 2: case 3: case 4: case 8: case 16:
533 return Len;
534 default:
535 break;
536 }
537 return 1;
538}
539
540static AMDGPULibFunc::ENamePrefix parseNamePrefix(StringRef& mangledName) {
541 std::pair<StringRef, StringRef> const P = mangledName.split('_');
542 AMDGPULibFunc::ENamePrefix Pfx =
543 StringSwitch<AMDGPULibFunc::ENamePrefix>(P.first)
544 .Case("native", AMDGPULibFunc::NATIVE)
545 .Case("half" , AMDGPULibFunc::HALF)
546 .Default(AMDGPULibFunc::NOPFX);
547
548 if (Pfx != AMDGPULibFunc::NOPFX)
549 mangledName = P.second;
550
551 return Pfx;
552}
553
Reid Kleckner85e2cda2019-03-28 17:33:41 +0000554StringMap<int> ManglingRule::buildManglingRulesMap() {
555 StringMap<int> Map(array_lengthof(manglingRules));
556 int Id = 0;
557 for (auto Rule : manglingRules)
558 Map.insert({Rule.Name, Id++});
559 return Map;
560}
561
Yaxun Liufc5121a2017-09-06 00:30:27 +0000562bool AMDGPUMangledLibFunc::parseUnmangledName(StringRef FullName) {
Reid Kleckner85e2cda2019-03-28 17:33:41 +0000563 static const StringMap<int> manglingRulesMap =
564 ManglingRule::buildManglingRulesMap();
Yaxun Liufc5121a2017-09-06 00:30:27 +0000565 FuncId = static_cast<EFuncId>(manglingRulesMap.lookup(FullName));
Stanislav Mekhanoshin7f377942017-08-11 16:42:09 +0000566 return FuncId != EI_NONE;
567}
568
569///////////////////////////////////////////////////////////////////////////////
570// Itanium Demangling
571
Benjamin Kramer49a49fe2017-08-20 13:03:48 +0000572namespace {
Stanislav Mekhanoshin7f377942017-08-11 16:42:09 +0000573struct ItaniumParamParser {
574 AMDGPULibFunc::Param Prev;
575 bool parseItaniumParam(StringRef& param, AMDGPULibFunc::Param &res);
576};
Benjamin Kramer49a49fe2017-08-20 13:03:48 +0000577} // namespace
Stanislav Mekhanoshin7f377942017-08-11 16:42:09 +0000578
579bool ItaniumParamParser::parseItaniumParam(StringRef& param,
580 AMDGPULibFunc::Param &res) {
581 res.reset();
582 if (param.empty()) return false;
583
584 // parse pointer prefix
585 if (eatTerm(param, 'P')) {
586 if (eatTerm(param, 'K')) res.PtrKind |= AMDGPULibFunc::CONST;
587 if (eatTerm(param, 'V')) res.PtrKind |= AMDGPULibFunc::VOLATILE;
Yaxun Liu0d9673c2017-11-04 17:37:43 +0000588 unsigned AS;
Stanislav Mekhanoshin7f377942017-08-11 16:42:09 +0000589 if (!eatTerm(param, "U3AS")) {
Yaxun Liu0d9673c2017-11-04 17:37:43 +0000590 AS = 0;
Stanislav Mekhanoshin7f377942017-08-11 16:42:09 +0000591 } else {
Yaxun Liu0d9673c2017-11-04 17:37:43 +0000592 AS = param.front() - '0';
Stanislav Mekhanoshin7f377942017-08-11 16:42:09 +0000593 drop_front(param, 1);
594 }
Yaxun Liu0d9673c2017-11-04 17:37:43 +0000595 res.PtrKind |= AMDGPULibFuncBase::getEPtrKindFromAddrSpace(AS);
Stanislav Mekhanoshin7f377942017-08-11 16:42:09 +0000596 } else {
597 res.PtrKind = AMDGPULibFunc::BYVALUE;
598 }
599
600 // parse vector size
601 if (eatTerm(param,"Dv")) {
602 res.VectorSize = parseVecSize(param);
603 if (res.VectorSize==1 || !eatTerm(param, '_')) return false;
604 }
605
606 // parse type
607 char const TC = param.front();
608 if (::isDigit(TC)) {
609 res.ArgType = StringSwitch<AMDGPULibFunc::EType>
610 (eatLengthPrefixedName(param))
611 .Case("ocl_image1darray" , AMDGPULibFunc::IMG1DA)
612 .Case("ocl_image1dbuffer", AMDGPULibFunc::IMG1DB)
613 .Case("ocl_image2darray" , AMDGPULibFunc::IMG2DA)
614 .Case("ocl_image1d" , AMDGPULibFunc::IMG1D)
615 .Case("ocl_image2d" , AMDGPULibFunc::IMG2D)
616 .Case("ocl_image3d" , AMDGPULibFunc::IMG3D)
617 .Case("ocl_event" , AMDGPULibFunc::DUMMY)
618 .Case("ocl_sampler" , AMDGPULibFunc::DUMMY)
619 .Default(AMDGPULibFunc::DUMMY);
620 } else {
621 drop_front(param);
622 switch (TC) {
623 case 'h': res.ArgType = AMDGPULibFunc::U8; break;
624 case 't': res.ArgType = AMDGPULibFunc::U16; break;
625 case 'j': res.ArgType = AMDGPULibFunc::U32; break;
626 case 'm': res.ArgType = AMDGPULibFunc::U64; break;
627 case 'c': res.ArgType = AMDGPULibFunc::I8; break;
628 case 's': res.ArgType = AMDGPULibFunc::I16; break;
629 case 'i': res.ArgType = AMDGPULibFunc::I32; break;
630 case 'l': res.ArgType = AMDGPULibFunc::I64; break;
631 case 'f': res.ArgType = AMDGPULibFunc::F32; break;
632 case 'd': res.ArgType = AMDGPULibFunc::F64; break;
633 case 'D': if (!eatTerm(param, 'h')) return false;
634 res.ArgType = AMDGPULibFunc::F16; break;
635 case 'S':
636 if (!eatTerm(param, '_')) {
637 eatNumber(param);
638 if (!eatTerm(param, '_')) return false;
639 }
640 res.VectorSize = Prev.VectorSize;
641 res.ArgType = Prev.ArgType;
642 break;
643 default:;
644 }
645 }
646 if (res.ArgType == 0) return false;
647 Prev.VectorSize = res.VectorSize;
648 Prev.ArgType = res.ArgType;
649 return true;
650}
651
Yaxun Liufc5121a2017-09-06 00:30:27 +0000652bool AMDGPUMangledLibFunc::parseFuncName(StringRef &mangledName) {
Stanislav Mekhanoshin7f377942017-08-11 16:42:09 +0000653 StringRef Name = eatLengthPrefixedName(mangledName);
654 FKind = parseNamePrefix(Name);
Yaxun Liufc5121a2017-09-06 00:30:27 +0000655 if (!parseUnmangledName(Name))
656 return false;
Stanislav Mekhanoshin7f377942017-08-11 16:42:09 +0000657
658 const ManglingRule& Rule = manglingRules[FuncId];
659 ItaniumParamParser Parser;
660 for (int I=0; I < Rule.maxLeadIndex(); ++I) {
661 Param P;
662 if (!Parser.parseItaniumParam(mangledName, P))
663 return false;
664
665 if ((I + 1) == Rule.Lead[0]) Leads[0] = P;
666 if ((I + 1) == Rule.Lead[1]) Leads[1] = P;
667 }
668 return true;
669}
670
Yaxun Liufc5121a2017-09-06 00:30:27 +0000671bool AMDGPUUnmangledLibFunc::parseFuncName(StringRef &Name) {
672 if (!UnmangledFuncInfo::lookup(Name, FuncId))
Stanislav Mekhanoshin7f377942017-08-11 16:42:09 +0000673 return false;
Yaxun Liufc5121a2017-09-06 00:30:27 +0000674 setName(Name);
675 return true;
676}
Stanislav Mekhanoshin7f377942017-08-11 16:42:09 +0000677
Yaxun Liufc5121a2017-09-06 00:30:27 +0000678bool AMDGPULibFunc::parse(StringRef FuncName, AMDGPULibFunc &F) {
679 if (FuncName.empty()) {
680 F.Impl = std::unique_ptr<AMDGPULibFuncImpl>();
681 return false;
Stanislav Mekhanoshin7f377942017-08-11 16:42:09 +0000682 }
Yaxun Liufc5121a2017-09-06 00:30:27 +0000683
684 if (eatTerm(FuncName, "_Z"))
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000685 F.Impl = std::make_unique<AMDGPUMangledLibFunc>();
Yaxun Liufc5121a2017-09-06 00:30:27 +0000686 else
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000687 F.Impl = std::make_unique<AMDGPUUnmangledLibFunc>();
Yaxun Liufc5121a2017-09-06 00:30:27 +0000688 if (F.Impl->parseFuncName(FuncName))
689 return true;
690
691 F.Impl = std::unique_ptr<AMDGPULibFuncImpl>();
Stanislav Mekhanoshin7f377942017-08-11 16:42:09 +0000692 return false;
693}
694
Yaxun Liufc5121a2017-09-06 00:30:27 +0000695StringRef AMDGPUMangledLibFunc::getUnmangledName(StringRef mangledName) {
Stanislav Mekhanoshin7f377942017-08-11 16:42:09 +0000696 StringRef S = mangledName;
697 if (eatTerm(S, "_Z"))
698 return eatLengthPrefixedName(S);
699 return StringRef();
700}
701
Stanislav Mekhanoshin7f377942017-08-11 16:42:09 +0000702///////////////////////////////////////////////////////////////////////////////
703// Mangling
704
705template <typename Stream>
Yaxun Liufc5121a2017-09-06 00:30:27 +0000706void AMDGPUMangledLibFunc::writeName(Stream &OS) const {
Stanislav Mekhanoshin7f377942017-08-11 16:42:09 +0000707 const char *Pfx = "";
708 switch (FKind) {
709 case NATIVE: Pfx = "native_"; break;
710 case HALF: Pfx = "half_"; break;
711 default: break;
712 }
713 if (!Name.empty()) {
714 OS << Pfx << Name;
715 } else if (FuncId != EI_NONE) {
716 OS << Pfx;
717 const StringRef& S = manglingRules[FuncId].Name;
718 OS.write(S.data(), S.size());
719 }
720}
721
Yaxun Liufc5121a2017-09-06 00:30:27 +0000722std::string AMDGPUMangledLibFunc::mangle() const { return mangleNameItanium(); }
Stanislav Mekhanoshin7f377942017-08-11 16:42:09 +0000723
724///////////////////////////////////////////////////////////////////////////////
725// Itanium Mangling
726
727static const char *getItaniumTypeName(AMDGPULibFunc::EType T) {
728 switch (T) {
729 case AMDGPULibFunc::U8: return "h";
730 case AMDGPULibFunc::U16: return "t";
731 case AMDGPULibFunc::U32: return "j";
732 case AMDGPULibFunc::U64: return "m";
733 case AMDGPULibFunc::I8: return "c";
734 case AMDGPULibFunc::I16: return "s";
735 case AMDGPULibFunc::I32: return "i";
736 case AMDGPULibFunc::I64: return "l";
737 case AMDGPULibFunc::F16: return "Dh";
738 case AMDGPULibFunc::F32: return "f";
739 case AMDGPULibFunc::F64: return "d";
740 case AMDGPULibFunc::IMG1DA: return "16ocl_image1darray";
741 case AMDGPULibFunc::IMG1DB: return "17ocl_image1dbuffer";
742 case AMDGPULibFunc::IMG2DA: return "16ocl_image2darray";
743 case AMDGPULibFunc::IMG1D: return "11ocl_image1d";
744 case AMDGPULibFunc::IMG2D: return "11ocl_image2d";
745 case AMDGPULibFunc::IMG3D: return "11ocl_image3d";
746 case AMDGPULibFunc::SAMPLER: return "11ocl_sampler";
747 case AMDGPULibFunc::EVENT: return "9ocl_event";
748 default: llvm_unreachable("Unhandeled param type");
749 }
750 return nullptr;
751}
752
Benjamin Kramer49a49fe2017-08-20 13:03:48 +0000753namespace {
Stanislav Mekhanoshin7f377942017-08-11 16:42:09 +0000754// Itanium mangling ABI says:
755// "5.1.8. Compression
756// ... Each non-terminal in the grammar for which <substitution> appears on the
757// right-hand side is both a source of future substitutions and a candidate
758// for being substituted. There are two exceptions that appear to be
759// substitution candidates from the grammar, but are explicitly excluded:
760// 1. <builtin-type> other than vendor extended types ..."
761
762// For the purpose of functions the following productions make sence for the
763// substitution:
764// <type> ::= <builtin-type>
765// ::= <class-enum-type>
766// ::= <array-type>
767// ::=<CV-qualifiers> <type>
768// ::= P <type> # pointer-to
769// ::= <substitution>
770//
771// Note that while types like images, samplers and events are by the ABI encoded
772// using <class-enum-type> production rule they're not used for substitution
773// because clang consider them as builtin types.
774//
775// DvNN_ type is GCC extension for vectors and is a subject for the substitution.
776
777
778class ItaniumMangler {
779 SmallVector<AMDGPULibFunc::Param, 10> Str; // list of accumulated substituions
780 bool UseAddrSpace;
781
782 int findSubst(const AMDGPULibFunc::Param& P) const {
783 for(unsigned I = 0; I < Str.size(); ++I) {
784 const AMDGPULibFunc::Param& T = Str[I];
785 if (P.PtrKind == T.PtrKind &&
786 P.VectorSize == T.VectorSize &&
787 P.ArgType == T.ArgType) {
788 return I;
789 }
790 }
791 return -1;
792 }
793
794 template <typename Stream>
795 bool trySubst(Stream& os, const AMDGPULibFunc::Param& p) {
796 int const subst = findSubst(p);
797 if (subst < 0) return false;
798 // Substitutions are mangled as S(XX)?_ where XX is a hexadecimal number
799 // 0 1 2
800 // S_ S0_ S1_
801 if (subst == 0) os << "S_";
802 else os << 'S' << (subst-1) << '_';
803 return true;
804 }
805
806public:
807 ItaniumMangler(bool useAddrSpace)
808 : UseAddrSpace(useAddrSpace) {}
809
810 template <typename Stream>
811 void operator()(Stream& os, AMDGPULibFunc::Param p) {
812
813 // Itanium mangling ABI 5.1.8. Compression:
814 // Logically, the substitutable components of a mangled name are considered
815 // left-to-right, components before the composite structure of which they
816 // are a part. If a component has been encountered before, it is substituted
817 // as described below. This decision is independent of whether its components
818 // have been substituted, so an implementation may optimize by considering
819 // large structures for substitution before their components. If a component
820 // has not been encountered before, its mangling is identified, and it is
821 // added to a dictionary of substitution candidates. No entity is added to
822 // the dictionary twice.
823 AMDGPULibFunc::Param Ptr;
824
825 if (p.PtrKind) {
826 if (trySubst(os, p)) return;
827 os << 'P';
828 if (p.PtrKind & AMDGPULibFunc::CONST) os << 'K';
829 if (p.PtrKind & AMDGPULibFunc::VOLATILE) os << 'V';
Yaxun Liu0d9673c2017-11-04 17:37:43 +0000830 unsigned AS = UseAddrSpace
831 ? AMDGPULibFuncBase::getAddrSpaceFromEPtrKind(p.PtrKind)
832 : 0;
Stanislav Mekhanoshin7f377942017-08-11 16:42:09 +0000833 if (AS != 0) os << "U3AS" << AS;
834 Ptr = p;
835 p.PtrKind = 0;
836 }
837
838 if (p.VectorSize > 1) {
839 if (trySubst(os, p)) goto exit;
840 Str.push_back(p);
841 os << "Dv" << static_cast<unsigned>(p.VectorSize) << '_';
842 }
843
844 os << getItaniumTypeName((AMDGPULibFunc::EType)p.ArgType);
845
846 exit:
847 if (Ptr.ArgType) Str.push_back(Ptr);
848 }
849};
Benjamin Kramer49a49fe2017-08-20 13:03:48 +0000850} // namespace
Stanislav Mekhanoshin7f377942017-08-11 16:42:09 +0000851
Yaxun Liufc5121a2017-09-06 00:30:27 +0000852std::string AMDGPUMangledLibFunc::mangleNameItanium() const {
Stanislav Mekhanoshin7f377942017-08-11 16:42:09 +0000853 SmallString<128> Buf;
854 raw_svector_ostream S(Buf);
855 SmallString<128> NameBuf;
856 raw_svector_ostream Name(NameBuf);
857 writeName(Name);
858 const StringRef& NameStr = Name.str();
859 S << "_Z" << static_cast<int>(NameStr.size()) << NameStr;
860
861 ItaniumMangler Mangler(true);
862 ParamIterator I(Leads, manglingRules[FuncId]);
863 Param P;
864 while ((P = I.getNextParam()).ArgType != 0)
865 Mangler(S, P);
866 return S.str();
867}
868
869///////////////////////////////////////////////////////////////////////////////
870// Misc
871
872static Type* getIntrinsicParamType(
873 LLVMContext& C,
874 const AMDGPULibFunc::Param& P,
875 bool useAddrSpace) {
876 Type* T = nullptr;
877 switch (P.ArgType) {
878 case AMDGPULibFunc::U8:
879 case AMDGPULibFunc::I8: T = Type::getInt8Ty(C); break;
880 case AMDGPULibFunc::U16:
881 case AMDGPULibFunc::I16: T = Type::getInt16Ty(C); break;
882 case AMDGPULibFunc::U32:
883 case AMDGPULibFunc::I32: T = Type::getInt32Ty(C); break;
884 case AMDGPULibFunc::U64:
885 case AMDGPULibFunc::I64: T = Type::getInt64Ty(C); break;
886 case AMDGPULibFunc::F16: T = Type::getHalfTy(C); break;
887 case AMDGPULibFunc::F32: T = Type::getFloatTy(C); break;
888 case AMDGPULibFunc::F64: T = Type::getDoubleTy(C); break;
889
890 case AMDGPULibFunc::IMG1DA:
891 case AMDGPULibFunc::IMG1DB:
892 case AMDGPULibFunc::IMG2DA:
893 case AMDGPULibFunc::IMG1D:
894 case AMDGPULibFunc::IMG2D:
895 case AMDGPULibFunc::IMG3D:
896 T = StructType::create(C,"ocl_image")->getPointerTo(); break;
897 case AMDGPULibFunc::SAMPLER:
898 T = StructType::create(C,"ocl_sampler")->getPointerTo(); break;
899 case AMDGPULibFunc::EVENT:
900 T = StructType::create(C,"ocl_event")->getPointerTo(); break;
901 default:
902 llvm_unreachable("Unhandeled param type");
903 return nullptr;
904 }
905 if (P.VectorSize > 1)
906 T = VectorType::get(T, P.VectorSize);
907 if (P.PtrKind != AMDGPULibFunc::BYVALUE)
908 T = useAddrSpace ? T->getPointerTo((P.PtrKind & AMDGPULibFunc::ADDR_SPACE)
909 - 1)
910 : T->getPointerTo();
911 return T;
912}
913
Yaxun Liufc5121a2017-09-06 00:30:27 +0000914FunctionType *AMDGPUMangledLibFunc::getFunctionType(Module &M) const {
Stanislav Mekhanoshin7f377942017-08-11 16:42:09 +0000915 LLVMContext& C = M.getContext();
916 std::vector<Type*> Args;
917 ParamIterator I(Leads, manglingRules[FuncId]);
918 Param P;
919 while ((P=I.getNextParam()).ArgType != 0)
920 Args.push_back(getIntrinsicParamType(C, P, true));
921
922 return FunctionType::get(
923 getIntrinsicParamType(C, getRetType(FuncId, Leads), true),
924 Args, false);
925}
926
Yaxun Liufc5121a2017-09-06 00:30:27 +0000927unsigned AMDGPUMangledLibFunc::getNumArgs() const {
Stanislav Mekhanoshin7f377942017-08-11 16:42:09 +0000928 return manglingRules[FuncId].getNumArgs();
929}
930
Yaxun Liufc5121a2017-09-06 00:30:27 +0000931unsigned AMDGPUUnmangledLibFunc::getNumArgs() const {
932 return UnmangledFuncInfo::getNumArgs(FuncId);
933}
934
935std::string AMDGPUMangledLibFunc::getName() const {
Stanislav Mekhanoshin7f377942017-08-11 16:42:09 +0000936 SmallString<128> Buf;
937 raw_svector_ostream OS(Buf);
938 writeName(OS);
939 return OS.str();
940}
941
Yaxun Liufc5121a2017-09-06 00:30:27 +0000942Function *AMDGPULibFunc::getFunction(Module *M, const AMDGPULibFunc &fInfo) {
Stanislav Mekhanoshin7f377942017-08-11 16:42:09 +0000943 std::string FuncName = fInfo.mangle();
944 Function *F = dyn_cast_or_null<Function>(
945 M->getValueSymbolTable().lookup(FuncName));
946
947 // check formal with actual types conformance
948 if (F && !F->isDeclaration()
949 && !F->isVarArg()
950 && F->arg_size() == fInfo.getNumArgs()) {
951 return F;
952 }
953 return nullptr;
954}
955
James Y Knight13680222019-02-01 02:28:03 +0000956FunctionCallee AMDGPULibFunc::getOrInsertFunction(Module *M,
957 const AMDGPULibFunc &fInfo) {
Stanislav Mekhanoshin7f377942017-08-11 16:42:09 +0000958 std::string const FuncName = fInfo.mangle();
959 Function *F = dyn_cast_or_null<Function>(
960 M->getValueSymbolTable().lookup(FuncName));
961
962 // check formal with actual types conformance
963 if (F && !F->isDeclaration()
964 && !F->isVarArg()
965 && F->arg_size() == fInfo.getNumArgs()) {
966 return F;
967 }
968
969 FunctionType *FuncTy = fInfo.getFunctionType(*M);
970
971 bool hasPtr = false;
972 for (FunctionType::param_iterator
973 PI = FuncTy->param_begin(),
974 PE = FuncTy->param_end();
975 PI != PE; ++PI) {
976 const Type* argTy = static_cast<const Type*>(*PI);
977 if (argTy->isPointerTy()) {
978 hasPtr = true;
979 break;
980 }
981 }
982
James Y Knight13680222019-02-01 02:28:03 +0000983 FunctionCallee C;
Stanislav Mekhanoshin7f377942017-08-11 16:42:09 +0000984 if (hasPtr) {
985 // Do not set extra attributes for functions with pointer arguments.
986 C = M->getOrInsertFunction(FuncName, FuncTy);
987 } else {
988 AttributeList Attr;
989 LLVMContext &Ctx = M->getContext();
Brian Gesiakb963c512018-12-09 21:56:50 +0000990 Attr = Attr.addAttribute(Ctx, AttributeList::FunctionIndex,
991 Attribute::ReadOnly);
992 Attr = Attr.addAttribute(Ctx, AttributeList::FunctionIndex,
993 Attribute::NoUnwind);
Stanislav Mekhanoshin7f377942017-08-11 16:42:09 +0000994 C = M->getOrInsertFunction(FuncName, FuncTy, Attr);
995 }
996
James Y Knight13680222019-02-01 02:28:03 +0000997 return C;
Stanislav Mekhanoshin7f377942017-08-11 16:42:09 +0000998}
Yaxun Liufc5121a2017-09-06 00:30:27 +0000999
Reid Kleckner85e2cda2019-03-28 17:33:41 +00001000StringMap<unsigned> UnmangledFuncInfo::buildNameMap() {
1001 StringMap<unsigned> Map;
1002 for (unsigned I = 0; I != TableSize; ++I)
1003 Map[Table[I].Name] = I;
1004 return Map;
1005}
1006
Yaxun Liufc5121a2017-09-06 00:30:27 +00001007bool UnmangledFuncInfo::lookup(StringRef Name, ID &Id) {
Reid Kleckner85e2cda2019-03-28 17:33:41 +00001008 static const StringMap<unsigned> Map = buildNameMap();
Yaxun Liufc5121a2017-09-06 00:30:27 +00001009 auto Loc = Map.find(Name);
1010 if (Loc != Map.end()) {
1011 Id = toFuncId(Loc->second);
1012 return true;
1013 }
1014 Id = AMDGPULibFunc::EI_NONE;
1015 return false;
1016}
1017
1018AMDGPULibFunc::AMDGPULibFunc(const AMDGPULibFunc &F) {
1019 if (auto *MF = dyn_cast<AMDGPUMangledLibFunc>(F.Impl.get()))
1020 Impl.reset(new AMDGPUMangledLibFunc(*MF));
1021 else if (auto *UMF = dyn_cast<AMDGPUUnmangledLibFunc>(F.Impl.get()))
1022 Impl.reset(new AMDGPUUnmangledLibFunc(*UMF));
1023 else
1024 Impl = std::unique_ptr<AMDGPULibFuncImpl>();
1025}
1026
1027AMDGPULibFunc &AMDGPULibFunc::operator=(const AMDGPULibFunc &F) {
1028 if (this == &F)
1029 return *this;
1030 new (this) AMDGPULibFunc(F);
1031 return *this;
1032}
1033
1034AMDGPULibFunc::AMDGPULibFunc(EFuncId Id, const AMDGPULibFunc &CopyFrom) {
1035 assert(AMDGPULibFuncBase::isMangled(Id) && CopyFrom.isMangled() &&
1036 "not supported");
1037 Impl.reset(new AMDGPUMangledLibFunc(
1038 Id, *cast<AMDGPUMangledLibFunc>(CopyFrom.Impl.get())));
1039}
1040
1041AMDGPULibFunc::AMDGPULibFunc(StringRef Name, FunctionType *FT) {
1042 Impl.reset(new AMDGPUUnmangledLibFunc(Name, FT));
1043}
1044
1045void AMDGPULibFunc::initMangled() { Impl.reset(new AMDGPUMangledLibFunc()); }
1046
1047AMDGPULibFunc::Param *AMDGPULibFunc::getLeads() {
1048 if (!Impl)
1049 initMangled();
1050 return cast<AMDGPUMangledLibFunc>(Impl.get())->Leads;
1051}
1052
1053const AMDGPULibFunc::Param *AMDGPULibFunc::getLeads() const {
1054 return cast<const AMDGPUMangledLibFunc>(Impl.get())->Leads;
1055}