1//===-- AMDGPULibFunc.cpp -------------------------------------------------===//
2//
3// 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
6//
7//===----------------------------------------------------------------------===//
8//
9// This file contains utility functions to work with Itanium mangled names
10//
11//===----------------------------------------------------------------------===//
12
13#include "AMDGPULibFunc.h"
14#include "AMDGPU.h"
15#include "AMDGPUSubtarget.h"
16#include "llvm/ADT/StringExtras.h"
17#include "llvm/ADT/StringMap.h"
18#include "llvm/ADT/StringSwitch.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/ModRef.h"
24#include "llvm/Support/raw_ostream.h"
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 {
58 const char *Name;
59 unsigned char Lead[2];
60 unsigned char Param[5];
61
62 int maxLeadIndex() const { return (std::max)(a: Lead[0], b: Lead[1]); }
63
64 unsigned getNumArgs() const;
65
66 static StringMap<int> buildManglingRulesMap();
67};
68
69// Information about library functions with unmangled names.
70class UnmangledFuncInfo {
71 const char *Name;
72 unsigned NumArgs;
73
74 // Table for all lib functions with unmangled names.
75 static const UnmangledFuncInfo Table[];
76
77 // Number of entries in Table.
78 static const unsigned TableSize;
79
80 static StringMap<unsigned> buildNameMap();
81
82public:
83 using ID = AMDGPULibFunc::EFuncId;
84 constexpr UnmangledFuncInfo(const char *_Name, unsigned _NumArgs)
85 : Name(_Name), NumArgs(_NumArgs) {}
86 // Get index to Table by function name.
87 static bool lookup(StringRef Name, ID &Id);
88 static unsigned toIndex(ID Id) {
89 assert(static_cast<unsigned>(Id) >
90 static_cast<unsigned>(AMDGPULibFunc::EI_LAST_MANGLED) &&
91 "Invalid unmangled library function");
92 return static_cast<unsigned>(Id) - 1 -
93 static_cast<unsigned>(AMDGPULibFunc::EI_LAST_MANGLED);
94 }
95 static ID toFuncId(unsigned Index) {
96 assert(Index < TableSize &&
97 "Invalid unmangled library function");
98 return static_cast<ID>(
99 Index + 1 + static_cast<unsigned>(AMDGPULibFunc::EI_LAST_MANGLED));
100 }
101 static unsigned getNumArgs(ID Id) { return Table[toIndex(Id)].NumArgs; }
102};
103
104unsigned ManglingRule::getNumArgs() const {
105 unsigned I=0;
106 while (I < (sizeof Param/sizeof Param[0]) && Param[I]) ++I;
107 return I;
108}
109
110// This table describes function formal argument type rules. The order of rules
111// corresponds to the EFuncId enum at AMDGPULibFunc.h
112//
113// "<func name>", { <leads> }, { <param rules> }
114// where:
115// <leads> - list of integers that are one-based indexes of formal argument
116// used to mangle a function name. Other argument types are derived from types
117// of these 'leads'. The order of integers in this list correspond to the
118// order in which these arguments are mangled in the EDG mangling scheme. The
119// same order should be preserved for arguments in the AMDGPULibFunc structure
120// when it is used for mangling. For example:
121// { "vstorea_half", {3,1}, {E_ANY,EX_SIZET,E_ANY}},
122// will be mangled in EDG scheme as vstorea_half_<3dparam>_<1stparam>
123// When mangling from code use:
124// AMDGPULibFunc insc;
125// insc.param[0] = ... // describe 3rd parameter
126// insc.param[1] = ... // describe 1rd parameter
127//
128// <param rules> - list of rules used to derive all of the function formal
129// argument types. EX_ prefixed are simple types, other derived from the
130// latest 'lead' argument type in the order of encoding from first to last.
131// E_ANY - use prev lead type, E_CONSTPTR_ANY - make const pointer out of
132// prev lead type, etc. see ParamIterator::getNextParam() for details.
133
134static constexpr ManglingRule manglingRules[] = {
135{ .Name: "", .Lead: {0}, .Param: {0} },
136{ .Name: "abs" , .Lead: {1}, .Param: {E_ANY}},
137{ .Name: "abs_diff" , .Lead: {1}, .Param: {E_ANY,E_COPY}},
138{ .Name: "acos" , .Lead: {1}, .Param: {E_ANY}},
139{ .Name: "acosh" , .Lead: {1}, .Param: {E_ANY}},
140{ .Name: "acospi" , .Lead: {1}, .Param: {E_ANY}},
141{ .Name: "add_sat" , .Lead: {1}, .Param: {E_ANY,E_COPY}},
142{ .Name: "all" , .Lead: {1}, .Param: {E_ANY}},
143{ .Name: "any" , .Lead: {1}, .Param: {E_ANY}},
144{ .Name: "asin" , .Lead: {1}, .Param: {E_ANY}},
145{ .Name: "asinh" , .Lead: {1}, .Param: {E_ANY}},
146{ .Name: "asinpi" , .Lead: {1}, .Param: {E_ANY}},
147{ .Name: "async_work_group_copy" , .Lead: {1}, .Param: {E_ANY,E_CONSTPTR_SWAPGL,EX_SIZET,EX_EVENT}},
148{ .Name: "async_work_group_strided_copy" , .Lead: {1}, .Param: {E_ANY,E_CONSTPTR_SWAPGL,EX_SIZET,EX_SIZET,EX_EVENT}},
149{ .Name: "atan" , .Lead: {1}, .Param: {E_ANY}},
150{ .Name: "atan2" , .Lead: {1}, .Param: {E_ANY,E_COPY}},
151{ .Name: "atan2pi" , .Lead: {1}, .Param: {E_ANY,E_COPY}},
152{ .Name: "atanh" , .Lead: {1}, .Param: {E_ANY}},
153{ .Name: "atanpi" , .Lead: {1}, .Param: {E_ANY}},
154{ .Name: "atomic_add" , .Lead: {1}, .Param: {E_VLTLPTR_ANY,E_POINTEE}},
155{ .Name: "atomic_and" , .Lead: {1}, .Param: {E_VLTLPTR_ANY,E_POINTEE}},
156{ .Name: "atomic_cmpxchg" , .Lead: {1}, .Param: {E_VLTLPTR_ANY,E_POINTEE,E_POINTEE}},
157{ .Name: "atomic_dec" , .Lead: {1}, .Param: {E_VLTLPTR_ANY}},
158{ .Name: "atomic_inc" , .Lead: {1}, .Param: {E_VLTLPTR_ANY}},
159{ .Name: "atomic_max" , .Lead: {1}, .Param: {E_VLTLPTR_ANY,E_POINTEE}},
160{ .Name: "atomic_min" , .Lead: {1}, .Param: {E_VLTLPTR_ANY,E_POINTEE}},
161{ .Name: "atomic_or" , .Lead: {1}, .Param: {E_VLTLPTR_ANY,E_POINTEE}},
162{ .Name: "atomic_sub" , .Lead: {1}, .Param: {E_VLTLPTR_ANY,E_POINTEE}},
163{ .Name: "atomic_xchg" , .Lead: {1}, .Param: {E_VLTLPTR_ANY,E_POINTEE}},
164{ .Name: "atomic_xor" , .Lead: {1}, .Param: {E_VLTLPTR_ANY,E_POINTEE}},
165{ .Name: "bitselect" , .Lead: {1}, .Param: {E_ANY,E_COPY,E_COPY}},
166{ .Name: "cbrt" , .Lead: {1}, .Param: {E_ANY}},
167{ .Name: "ceil" , .Lead: {1}, .Param: {E_ANY}},
168{ .Name: "clamp" , .Lead: {1}, .Param: {E_ANY,E_COPY,E_COPY}},
169{ .Name: "clz" , .Lead: {1}, .Param: {E_ANY}},
170{ .Name: "commit_read_pipe" , .Lead: {1}, .Param: {E_ANY,EX_RESERVEDID}},
171{ .Name: "commit_write_pipe" , .Lead: {1}, .Param: {E_ANY,EX_RESERVEDID}},
172{ .Name: "copysign" , .Lead: {1}, .Param: {E_ANY,E_COPY}},
173{ .Name: "cos" , .Lead: {1}, .Param: {E_ANY}},
174{ .Name: "cosh" , .Lead: {1}, .Param: {E_ANY}},
175{ .Name: "cospi" , .Lead: {1}, .Param: {E_ANY}},
176{ .Name: "cross" , .Lead: {1}, .Param: {E_ANY,E_COPY}},
177{ .Name: "ctz" , .Lead: {1}, .Param: {E_ANY}},
178{ .Name: "degrees" , .Lead: {1}, .Param: {E_ANY}},
179{ .Name: "distance" , .Lead: {1}, .Param: {E_ANY,E_COPY}},
180{ .Name: "divide" , .Lead: {1}, .Param: {E_ANY,E_COPY}},
181{ .Name: "dot" , .Lead: {1}, .Param: {E_ANY,E_COPY}},
182{ .Name: "erf" , .Lead: {1}, .Param: {E_ANY}},
183{ .Name: "erfc" , .Lead: {1}, .Param: {E_ANY}},
184{ .Name: "exp" , .Lead: {1}, .Param: {E_ANY}},
185{ .Name: "exp10" , .Lead: {1}, .Param: {E_ANY}},
186{ .Name: "exp2" , .Lead: {1}, .Param: {E_ANY}},
187{ .Name: "expm1" , .Lead: {1}, .Param: {E_ANY}},
188{ .Name: "fabs" , .Lead: {1}, .Param: {E_ANY}},
189{ .Name: "fast_distance" , .Lead: {1}, .Param: {E_ANY,E_COPY}},
190{ .Name: "fast_length" , .Lead: {1}, .Param: {E_ANY}},
191{ .Name: "fast_normalize" , .Lead: {1}, .Param: {E_ANY}},
192{ .Name: "fdim" , .Lead: {1}, .Param: {E_ANY,E_COPY}},
193{ .Name: "floor" , .Lead: {1}, .Param: {E_ANY}},
194{ .Name: "fma" , .Lead: {1}, .Param: {E_ANY,E_COPY,E_COPY}},
195{ .Name: "fmax" , .Lead: {1}, .Param: {E_ANY,E_COPY}},
196{ .Name: "fmin" , .Lead: {1}, .Param: {E_ANY,E_COPY}},
197{ .Name: "fmod" , .Lead: {1}, .Param: {E_ANY,E_COPY}},
198{ .Name: "fract" , .Lead: {2}, .Param: {E_POINTEE,E_ANY}},
199{ .Name: "frexp" , .Lead: {1,2}, .Param: {E_ANY,E_ANY}},
200{ .Name: "get_image_array_size" , .Lead: {1}, .Param: {E_ANY}},
201{ .Name: "get_image_channel_data_type" , .Lead: {1}, .Param: {E_ANY}},
202{ .Name: "get_image_channel_order" , .Lead: {1}, .Param: {E_ANY}},
203{ .Name: "get_image_dim" , .Lead: {1}, .Param: {E_ANY}},
204{ .Name: "get_image_height" , .Lead: {1}, .Param: {E_ANY}},
205{ .Name: "get_image_width" , .Lead: {1}, .Param: {E_ANY}},
206{ .Name: "get_pipe_max_packets" , .Lead: {1}, .Param: {E_ANY}},
207{ .Name: "get_pipe_num_packets" , .Lead: {1}, .Param: {E_ANY}},
208{ .Name: "hadd" , .Lead: {1}, .Param: {E_ANY,E_COPY}},
209{ .Name: "hypot" , .Lead: {1}, .Param: {E_ANY,E_COPY}},
210{ .Name: "ilogb" , .Lead: {1}, .Param: {E_ANY}},
211{ .Name: "isequal" , .Lead: {1}, .Param: {E_ANY,E_COPY}},
212{ .Name: "isfinite" , .Lead: {1}, .Param: {E_ANY}},
213{ .Name: "isgreater" , .Lead: {1}, .Param: {E_ANY,E_COPY}},
214{ .Name: "isgreaterequal" , .Lead: {1}, .Param: {E_ANY,E_COPY}},
215{ .Name: "isinf" , .Lead: {1}, .Param: {E_ANY}},
216{ .Name: "isless" , .Lead: {1}, .Param: {E_ANY,E_COPY}},
217{ .Name: "islessequal" , .Lead: {1}, .Param: {E_ANY,E_COPY}},
218{ .Name: "islessgreater" , .Lead: {1}, .Param: {E_ANY,E_COPY}},
219{ .Name: "isnan" , .Lead: {1}, .Param: {E_ANY}},
220{ .Name: "isnormal" , .Lead: {1}, .Param: {E_ANY}},
221{ .Name: "isnotequal" , .Lead: {1}, .Param: {E_ANY,E_COPY}},
222{ .Name: "isordered" , .Lead: {1}, .Param: {E_ANY,E_COPY}},
223{ .Name: "isunordered" , .Lead: {1}, .Param: {E_ANY,E_COPY}},
224{ .Name: "ldexp" , .Lead: {1}, .Param: {E_ANY,E_SETBASE_I32}},
225{ .Name: "length" , .Lead: {1}, .Param: {E_ANY}},
226{ .Name: "lgamma" , .Lead: {1}, .Param: {E_ANY}},
227{ .Name: "lgamma_r" , .Lead: {1,2}, .Param: {E_ANY,E_ANY}},
228{ .Name: "log" , .Lead: {1}, .Param: {E_ANY}},
229{ .Name: "log10" , .Lead: {1}, .Param: {E_ANY}},
230{ .Name: "log1p" , .Lead: {1}, .Param: {E_ANY}},
231{ .Name: "log2" , .Lead: {1}, .Param: {E_ANY}},
232{ .Name: "logb" , .Lead: {1}, .Param: {E_ANY}},
233{ .Name: "mad" , .Lead: {1}, .Param: {E_ANY,E_COPY,E_COPY}},
234{ .Name: "mad24" , .Lead: {1}, .Param: {E_ANY,E_COPY,E_COPY}},
235{ .Name: "mad_hi" , .Lead: {1}, .Param: {E_ANY,E_COPY,E_COPY}},
236{ .Name: "mad_sat" , .Lead: {1}, .Param: {E_ANY,E_COPY,E_COPY}},
237{ .Name: "max" , .Lead: {1}, .Param: {E_ANY,E_COPY}},
238{ .Name: "maxmag" , .Lead: {1}, .Param: {E_ANY,E_COPY}},
239{ .Name: "min" , .Lead: {1}, .Param: {E_ANY,E_COPY}},
240{ .Name: "minmag" , .Lead: {1}, .Param: {E_ANY,E_COPY}},
241{ .Name: "mix" , .Lead: {1}, .Param: {E_ANY,E_COPY,E_COPY}},
242{ .Name: "modf" , .Lead: {2}, .Param: {E_POINTEE,E_ANY}},
243{ .Name: "mul24" , .Lead: {1}, .Param: {E_ANY,E_COPY}},
244{ .Name: "mul_hi" , .Lead: {1}, .Param: {E_ANY,E_COPY}},
245{ .Name: "nan" , .Lead: {1}, .Param: {E_ANY}},
246{ .Name: "nextafter" , .Lead: {1}, .Param: {E_ANY,E_COPY}},
247{ .Name: "normalize" , .Lead: {1}, .Param: {E_ANY}},
248{ .Name: "popcount" , .Lead: {1}, .Param: {E_ANY}},
249{ .Name: "pow" , .Lead: {1}, .Param: {E_ANY,E_COPY}},
250{ .Name: "__pow_fast" , .Lead: {1}, .Param: {E_ANY,E_COPY}},
251{ .Name: "pown" , .Lead: {1}, .Param: {E_ANY,E_SETBASE_I32}},
252{ .Name: "__pown_fast" , .Lead: {1}, .Param: {E_ANY,E_SETBASE_I32}},
253{ .Name: "powr" , .Lead: {1}, .Param: {E_ANY,E_COPY}},
254{ .Name: "__powr_fast" , .Lead: {1}, .Param: {E_ANY,E_COPY}},
255{ .Name: "prefetch" , .Lead: {1}, .Param: {E_CONSTPTR_ANY,EX_SIZET}},
256{ .Name: "radians" , .Lead: {1}, .Param: {E_ANY}},
257{ .Name: "recip" , .Lead: {1}, .Param: {E_ANY}},
258{ .Name: "remainder" , .Lead: {1}, .Param: {E_ANY,E_COPY}},
259{ .Name: "remquo" , .Lead: {1,3}, .Param: {E_ANY,E_COPY,E_ANY}},
260{ .Name: "reserve_read_pipe" , .Lead: {1}, .Param: {E_ANY,EX_UINT}},
261{ .Name: "reserve_write_pipe" , .Lead: {1}, .Param: {E_ANY,EX_UINT}},
262{ .Name: "rhadd" , .Lead: {1}, .Param: {E_ANY,E_COPY}},
263{ .Name: "rint" , .Lead: {1}, .Param: {E_ANY}},
264{ .Name: "rootn" , .Lead: {1}, .Param: {E_ANY,E_SETBASE_I32}},
265{ .Name: "__rootn_fast" , .Lead: {1}, .Param: {E_ANY,E_SETBASE_I32}},
266{ .Name: "rotate" , .Lead: {1}, .Param: {E_ANY,E_COPY}},
267{ .Name: "round" , .Lead: {1}, .Param: {E_ANY}},
268{ .Name: "rsqrt" , .Lead: {1}, .Param: {E_ANY}},
269{ .Name: "select" , .Lead: {1,3}, .Param: {E_ANY,E_COPY,E_ANY}},
270{ .Name: "shuffle" , .Lead: {1,2}, .Param: {E_ANY,E_ANY}},
271{ .Name: "shuffle2" , .Lead: {1,3}, .Param: {E_ANY,E_COPY,E_ANY}},
272{ .Name: "sign" , .Lead: {1}, .Param: {E_ANY}},
273{ .Name: "signbit" , .Lead: {1}, .Param: {E_ANY}},
274{ .Name: "sin" , .Lead: {1}, .Param: {E_ANY}},
275{ .Name: "sincos" , .Lead: {2}, .Param: {E_POINTEE,E_ANY}},
276{ .Name: "sinh" , .Lead: {1}, .Param: {E_ANY}},
277{ .Name: "sinpi" , .Lead: {1}, .Param: {E_ANY}},
278{ .Name: "smoothstep" , .Lead: {1}, .Param: {E_ANY,E_COPY,E_COPY}},
279{ .Name: "sqrt" , .Lead: {1}, .Param: {E_ANY}},
280{ .Name: "step" , .Lead: {1}, .Param: {E_ANY,E_COPY}},
281{ .Name: "sub_group_broadcast" , .Lead: {1}, .Param: {E_ANY,EX_UINT}},
282{ .Name: "sub_group_commit_read_pipe" , .Lead: {1}, .Param: {E_ANY,EX_RESERVEDID}},
283{ .Name: "sub_group_commit_write_pipe" , .Lead: {1}, .Param: {E_ANY,EX_RESERVEDID}},
284{ .Name: "sub_group_reduce_add" , .Lead: {1}, .Param: {E_ANY}},
285{ .Name: "sub_group_reduce_max" , .Lead: {1}, .Param: {E_ANY}},
286{ .Name: "sub_group_reduce_min" , .Lead: {1}, .Param: {E_ANY}},
287{ .Name: "sub_group_reserve_read_pipe" , .Lead: {1}, .Param: {E_ANY,EX_UINT}},
288{ .Name: "sub_group_reserve_write_pipe" , .Lead: {1}, .Param: {E_ANY,EX_UINT}},
289{ .Name: "sub_group_scan_exclusive_add" , .Lead: {1}, .Param: {E_ANY}},
290{ .Name: "sub_group_scan_exclusive_max" , .Lead: {1}, .Param: {E_ANY}},
291{ .Name: "sub_group_scan_exclusive_min" , .Lead: {1}, .Param: {E_ANY}},
292{ .Name: "sub_group_scan_inclusive_add" , .Lead: {1}, .Param: {E_ANY}},
293{ .Name: "sub_group_scan_inclusive_max" , .Lead: {1}, .Param: {E_ANY}},
294{ .Name: "sub_group_scan_inclusive_min" , .Lead: {1}, .Param: {E_ANY}},
295{ .Name: "sub_sat" , .Lead: {1}, .Param: {E_ANY,E_COPY}},
296{ .Name: "tan" , .Lead: {1}, .Param: {E_ANY}},
297{ .Name: "tanh" , .Lead: {1}, .Param: {E_ANY}},
298{ .Name: "tanpi" , .Lead: {1}, .Param: {E_ANY}},
299{ .Name: "tgamma" , .Lead: {1}, .Param: {E_ANY}},
300{ .Name: "trunc" , .Lead: {1}, .Param: {E_ANY}},
301{ .Name: "upsample" , .Lead: {1}, .Param: {E_ANY,E_MAKEBASE_UNS}},
302{ .Name: "vec_step" , .Lead: {1}, .Param: {E_ANY}},
303{ .Name: "vstore" , .Lead: {3}, .Param: {E_POINTEE,EX_SIZET,E_ANY}},
304{ .Name: "vstore16" , .Lead: {3}, .Param: {E_V16_OF_POINTEE,EX_SIZET,E_ANY}},
305{ .Name: "vstore2" , .Lead: {3}, .Param: {E_V2_OF_POINTEE,EX_SIZET,E_ANY}},
306{ .Name: "vstore3" , .Lead: {3}, .Param: {E_V3_OF_POINTEE,EX_SIZET,E_ANY}},
307{ .Name: "vstore4" , .Lead: {3}, .Param: {E_V4_OF_POINTEE,EX_SIZET,E_ANY}},
308{ .Name: "vstore8" , .Lead: {3}, .Param: {E_V8_OF_POINTEE,EX_SIZET,E_ANY}},
309{ .Name: "work_group_commit_read_pipe" , .Lead: {1}, .Param: {E_ANY,EX_RESERVEDID}},
310{ .Name: "work_group_commit_write_pipe" , .Lead: {1}, .Param: {E_ANY,EX_RESERVEDID}},
311{ .Name: "work_group_reduce_add" , .Lead: {1}, .Param: {E_ANY}},
312{ .Name: "work_group_reduce_max" , .Lead: {1}, .Param: {E_ANY}},
313{ .Name: "work_group_reduce_min" , .Lead: {1}, .Param: {E_ANY}},
314{ .Name: "work_group_reserve_read_pipe" , .Lead: {1}, .Param: {E_ANY,EX_UINT}},
315{ .Name: "work_group_reserve_write_pipe" , .Lead: {1}, .Param: {E_ANY,EX_UINT}},
316{ .Name: "work_group_scan_exclusive_add" , .Lead: {1}, .Param: {E_ANY}},
317{ .Name: "work_group_scan_exclusive_max" , .Lead: {1}, .Param: {E_ANY}},
318{ .Name: "work_group_scan_exclusive_min" , .Lead: {1}, .Param: {E_ANY}},
319{ .Name: "work_group_scan_inclusive_add" , .Lead: {1}, .Param: {E_ANY}},
320{ .Name: "work_group_scan_inclusive_max" , .Lead: {1}, .Param: {E_ANY}},
321{ .Name: "work_group_scan_inclusive_min" , .Lead: {1}, .Param: {E_ANY}},
322{ .Name: "write_imagef" , .Lead: {1}, .Param: {E_ANY,E_IMAGECOORDS,EX_FLOAT4}},
323{ .Name: "write_imagei" , .Lead: {1}, .Param: {E_ANY,E_IMAGECOORDS,EX_INTV4}},
324{ .Name: "write_imageui" , .Lead: {1}, .Param: {E_ANY,E_IMAGECOORDS,EX_UINTV4}},
325{ .Name: "ncos" , .Lead: {1}, .Param: {E_ANY} },
326{ .Name: "nexp2" , .Lead: {1}, .Param: {E_ANY} },
327{ .Name: "nfma" , .Lead: {1}, .Param: {E_ANY, E_COPY, E_COPY} },
328{ .Name: "nlog2" , .Lead: {1}, .Param: {E_ANY} },
329{ .Name: "nrcp" , .Lead: {1}, .Param: {E_ANY} },
330{ .Name: "nrsqrt" , .Lead: {1}, .Param: {E_ANY} },
331{ .Name: "nsin" , .Lead: {1}, .Param: {E_ANY} },
332{ .Name: "nsqrt" , .Lead: {1}, .Param: {E_ANY} },
333{ .Name: "ftz" , .Lead: {1}, .Param: {E_ANY} },
334{ .Name: "fldexp" , .Lead: {1}, .Param: {E_ANY, EX_UINT} },
335{ .Name: "class" , .Lead: {1}, .Param: {E_ANY, EX_UINT} },
336{ .Name: "rcbrt" , .Lead: {1}, .Param: {E_ANY} },
337};
338
339// Library functions with unmangled name.
340const UnmangledFuncInfo UnmangledFuncInfo::Table[] = {
341 {"__read_pipe_2", 4},
342 {"__read_pipe_4", 6},
343 {"__write_pipe_2", 4},
344 {"__write_pipe_4", 6},
345};
346
347const unsigned UnmangledFuncInfo::TableSize =
348 std::size(UnmangledFuncInfo::Table);
349
350static AMDGPULibFunc::Param getRetType(AMDGPULibFunc::EFuncId id,
351 const AMDGPULibFunc::Param (&Leads)[2]) {
352 AMDGPULibFunc::Param Res = Leads[0];
353 // TBD - This switch may require to be extended for other intrinsics
354 switch (id) {
355 case AMDGPULibFunc::EI_SINCOS:
356 Res.PtrKind = AMDGPULibFunc::BYVALUE;
357 break;
358 default:
359 break;
360 }
361 return Res;
362}
363
364class ParamIterator {
365 const AMDGPULibFunc::Param (&Leads)[2];
366 const ManglingRule& Rule;
367 int Index = 0;
368public:
369 ParamIterator(const AMDGPULibFunc::Param (&leads)[2],
370 const ManglingRule& rule)
371 : Leads(leads), Rule(rule) {}
372
373 AMDGPULibFunc::Param getNextParam();
374};
375
376AMDGPULibFunc::Param ParamIterator::getNextParam() {
377 AMDGPULibFunc::Param P;
378 if (Index >= int(sizeof Rule.Param/sizeof Rule.Param[0])) return P;
379
380 const char R = Rule.Param[Index];
381 switch (R) {
382 case E_NONE: break;
383 case EX_UINT:
384 P.ArgType = AMDGPULibFunc::U32; break;
385 case EX_INTV4:
386 P.ArgType = AMDGPULibFunc::I32; P.VectorSize = 4; break;
387 case EX_UINTV4:
388 P.ArgType = AMDGPULibFunc::U32; P.VectorSize = 4; break;
389 case EX_FLOAT4:
390 P.ArgType = AMDGPULibFunc::F32; P.VectorSize = 4; break;
391 case EX_SIZET:
392 P.ArgType = AMDGPULibFunc::U64; break;
393 case EX_EVENT:
394 P.ArgType = AMDGPULibFunc::EVENT; break;
395 case EX_SAMPLER:
396 P.ArgType = AMDGPULibFunc::SAMPLER; break;
397 case EX_RESERVEDID: break; // TBD
398 default:
399 if (Index == (Rule.Lead[1] - 1)) P = Leads[1];
400 else P = Leads[0];
401
402 switch (R) {
403 case E_ANY:
404 case E_COPY: break;
405
406 case E_POINTEE:
407 P.PtrKind = AMDGPULibFunc::BYVALUE; break;
408 case E_V2_OF_POINTEE:
409 P.VectorSize = 2; P.PtrKind = AMDGPULibFunc::BYVALUE; break;
410 case E_V3_OF_POINTEE:
411 P.VectorSize = 3; P.PtrKind = AMDGPULibFunc::BYVALUE; break;
412 case E_V4_OF_POINTEE:
413 P.VectorSize = 4; P.PtrKind = AMDGPULibFunc::BYVALUE; break;
414 case E_V8_OF_POINTEE:
415 P.VectorSize = 8; P.PtrKind = AMDGPULibFunc::BYVALUE; break;
416 case E_V16_OF_POINTEE:
417 P.VectorSize = 16; P.PtrKind = AMDGPULibFunc::BYVALUE; break;
418 case E_CONSTPTR_ANY:
419 P.PtrKind |= AMDGPULibFunc::CONST; break;
420 case E_VLTLPTR_ANY:
421 P.PtrKind |= AMDGPULibFunc::VOLATILE; break;
422 case E_SETBASE_I32:
423 P.ArgType = AMDGPULibFunc::I32; break;
424 case E_SETBASE_U32:
425 P.ArgType = AMDGPULibFunc::U32; break;
426
427 case E_MAKEBASE_UNS:
428 P.ArgType &= ~AMDGPULibFunc::BASE_TYPE_MASK;
429 P.ArgType |= AMDGPULibFunc::UINT;
430 break;
431
432 case E_IMAGECOORDS:
433 switch (P.ArgType) {
434 case AMDGPULibFunc::IMG1DA: P.VectorSize = 2; break;
435 case AMDGPULibFunc::IMG1DB: P.VectorSize = 1; break;
436 case AMDGPULibFunc::IMG2DA: P.VectorSize = 4; break;
437 case AMDGPULibFunc::IMG1D: P.VectorSize = 1; break;
438 case AMDGPULibFunc::IMG2D: P.VectorSize = 2; break;
439 case AMDGPULibFunc::IMG3D: P.VectorSize = 4; break;
440 }
441 P.PtrKind = AMDGPULibFunc::BYVALUE;
442 P.ArgType = AMDGPULibFunc::I32;
443 break;
444
445 case E_CONSTPTR_SWAPGL: {
446 unsigned AS = AMDGPULibFunc::getAddrSpaceFromEPtrKind(Kind: P.PtrKind);
447 switch (AS) {
448 case AMDGPUAS::GLOBAL_ADDRESS: AS = AMDGPUAS::LOCAL_ADDRESS; break;
449 case AMDGPUAS::LOCAL_ADDRESS: AS = AMDGPUAS::GLOBAL_ADDRESS; break;
450 }
451 P.PtrKind = AMDGPULibFunc::getEPtrKindFromAddrSpace(AS);
452 P.PtrKind |= AMDGPULibFunc::CONST;
453 break;
454 }
455
456 default:
457 llvm_unreachable("Unhandled param rule");
458 }
459 }
460 ++Index;
461 return P;
462}
463
464inline static void drop_front(StringRef& str, size_t n = 1) {
465 str = str.drop_front(N: n);
466}
467
468static bool eatTerm(StringRef& mangledName, const char c) {
469 if (mangledName.front() == c) {
470 drop_front(str&: mangledName);
471 return true;
472 }
473 return false;
474}
475
476template <size_t N>
477static bool eatTerm(StringRef& mangledName, const char (&str)[N]) {
478 if (mangledName.starts_with(Prefix: StringRef(str, N - 1))) {
479 drop_front(str&: mangledName, n: N-1);
480 return true;
481 }
482 return false;
483}
484
485static int eatNumber(StringRef& s) {
486 size_t const savedSize = s.size();
487 int n = 0;
488 while (!s.empty() && isDigit(C: s.front())) {
489 n = n*10 + s.front() - '0';
490 drop_front(str&: s);
491 }
492 return s.size() < savedSize ? n : -1;
493}
494
495static StringRef eatLengthPrefixedName(StringRef& mangledName) {
496 int const Len = eatNumber(s&: mangledName);
497 if (Len <= 0 || static_cast<size_t>(Len) > mangledName.size())
498 return StringRef();
499 StringRef Res = mangledName.substr(Start: 0, N: Len);
500 drop_front(str&: mangledName, n: Len);
501 return Res;
502}
503
504} // end anonymous namespace
505
506AMDGPUMangledLibFunc::AMDGPUMangledLibFunc() {
507 FuncId = EI_NONE;
508 FKind = NOPFX;
509 Leads[0].reset();
510 Leads[1].reset();
511 Name.clear();
512}
513
514AMDGPUUnmangledLibFunc::AMDGPUUnmangledLibFunc() {
515 FuncId = EI_NONE;
516 FuncTy = nullptr;
517}
518
519AMDGPUMangledLibFunc::AMDGPUMangledLibFunc(
520 EFuncId id, const AMDGPUMangledLibFunc &copyFrom) {
521 FuncId = id;
522 FKind = copyFrom.FKind;
523 Leads[0] = copyFrom.Leads[0];
524 Leads[1] = copyFrom.Leads[1];
525}
526
527AMDGPUMangledLibFunc::AMDGPUMangledLibFunc(EFuncId id, FunctionType *FT,
528 bool SignedInts) {
529 FuncId = id;
530 unsigned NumArgs = FT->getNumParams();
531 if (NumArgs >= 1)
532 Leads[0] = Param::getFromTy(Ty: FT->getParamType(i: 0), Signed: SignedInts);
533 if (NumArgs >= 2)
534 Leads[1] = Param::getFromTy(Ty: FT->getParamType(i: 1), Signed: SignedInts);
535}
536
537///////////////////////////////////////////////////////////////////////////////
538// Demangling
539
540static unsigned char parseVecSize(StringRef &mangledName) {
541 int const Len = eatNumber(s&: mangledName);
542 switch (Len) {
543 case 2: case 3: case 4: case 8: case 16:
544 return static_cast<unsigned char>(Len);
545 default:
546 return 1;
547 }
548}
549
550static AMDGPULibFunc::ENamePrefix parseNamePrefix(StringRef& mangledName) {
551 std::pair<StringRef, StringRef> const P = mangledName.split(Separator: '_');
552 AMDGPULibFunc::ENamePrefix Pfx =
553 StringSwitch<AMDGPULibFunc::ENamePrefix>(P.first)
554 .Case(S: "native", Value: AMDGPULibFunc::NATIVE)
555 .Case(S: "half" , Value: AMDGPULibFunc::HALF)
556 .Default(Value: AMDGPULibFunc::NOPFX);
557
558 if (Pfx != AMDGPULibFunc::NOPFX)
559 mangledName = P.second;
560
561 return Pfx;
562}
563
564StringMap<int> ManglingRule::buildManglingRulesMap() {
565 StringMap<int> Map(std::size(manglingRules));
566 int Id = 0;
567 for (auto Rule : manglingRules)
568 Map.insert(KV: {Rule.Name, Id++});
569 return Map;
570}
571
572bool AMDGPUMangledLibFunc::parseUnmangledName(StringRef FullName) {
573 static const StringMap<int> manglingRulesMap =
574 ManglingRule::buildManglingRulesMap();
575 FuncId = static_cast<EFuncId>(manglingRulesMap.lookup(Key: FullName));
576 return FuncId != EI_NONE;
577}
578
579///////////////////////////////////////////////////////////////////////////////
580// Itanium Demangling
581
582namespace {
583struct ItaniumParamParser {
584 AMDGPULibFunc::Param Prev;
585 bool parseItaniumParam(StringRef& param, AMDGPULibFunc::Param &res);
586};
587} // namespace
588
589bool ItaniumParamParser::parseItaniumParam(StringRef& param,
590 AMDGPULibFunc::Param &res) {
591 res.reset();
592 if (param.empty()) return false;
593
594 // parse pointer prefix
595 if (eatTerm(mangledName&: param, c: 'P')) {
596 if (eatTerm(mangledName&: param, c: 'K')) res.PtrKind |= AMDGPULibFunc::CONST;
597 if (eatTerm(mangledName&: param, c: 'V')) res.PtrKind |= AMDGPULibFunc::VOLATILE;
598 unsigned AS;
599 if (!eatTerm(mangledName&: param, str: "U3AS")) {
600 AS = 0;
601 } else {
602 AS = param.front() - '0';
603 drop_front(str&: param, n: 1);
604 }
605 res.PtrKind |= AMDGPULibFuncBase::getEPtrKindFromAddrSpace(AS);
606 } else {
607 res.PtrKind = AMDGPULibFunc::BYVALUE;
608 }
609
610 // parse vector size
611 if (eatTerm(mangledName&: param,str: "Dv")) {
612 res.VectorSize = parseVecSize(mangledName&: param);
613 if (res.VectorSize==1 || !eatTerm(mangledName&: param, c: '_')) return false;
614 }
615
616 // parse type
617 char const TC = param.front();
618 if (isDigit(C: TC)) {
619 res.ArgType =
620 StringSwitch<AMDGPULibFunc::EType>(eatLengthPrefixedName(mangledName&: param))
621 .StartsWith(S: "ocl_image1d_array", Value: AMDGPULibFunc::IMG1DA)
622 .StartsWith(S: "ocl_image1d_buffer", Value: AMDGPULibFunc::IMG1DB)
623 .StartsWith(S: "ocl_image2d_array", Value: AMDGPULibFunc::IMG2DA)
624 .StartsWith(S: "ocl_image1d", Value: AMDGPULibFunc::IMG1D)
625 .StartsWith(S: "ocl_image2d", Value: AMDGPULibFunc::IMG2D)
626 .StartsWith(S: "ocl_image3d", Value: AMDGPULibFunc::IMG3D)
627 .Case(S: "ocl_event", Value: AMDGPULibFunc::DUMMY)
628 .Case(S: "ocl_sampler", Value: AMDGPULibFunc::DUMMY)
629 .Default(Value: AMDGPULibFunc::DUMMY);
630 } else {
631 drop_front(str&: param);
632 switch (TC) {
633 case 'h': res.ArgType = AMDGPULibFunc::U8; break;
634 case 't': res.ArgType = AMDGPULibFunc::U16; break;
635 case 'j': res.ArgType = AMDGPULibFunc::U32; break;
636 case 'm': res.ArgType = AMDGPULibFunc::U64; break;
637 case 'c': res.ArgType = AMDGPULibFunc::I8; break;
638 case 's': res.ArgType = AMDGPULibFunc::I16; break;
639 case 'i': res.ArgType = AMDGPULibFunc::I32; break;
640 case 'l': res.ArgType = AMDGPULibFunc::I64; break;
641 case 'f': res.ArgType = AMDGPULibFunc::F32; break;
642 case 'd': res.ArgType = AMDGPULibFunc::F64; break;
643 case 'D': if (!eatTerm(mangledName&: param, c: 'h')) return false;
644 res.ArgType = AMDGPULibFunc::F16; break;
645 case 'S':
646 if (!eatTerm(mangledName&: param, c: '_')) {
647 eatNumber(s&: param);
648 if (!eatTerm(mangledName&: param, c: '_')) return false;
649 }
650 res.VectorSize = Prev.VectorSize;
651 res.ArgType = Prev.ArgType;
652 break;
653 default:;
654 }
655 }
656 if (res.ArgType == 0) return false;
657 Prev.VectorSize = res.VectorSize;
658 Prev.ArgType = res.ArgType;
659 return true;
660}
661
662bool AMDGPUMangledLibFunc::parseFuncName(StringRef &mangledName) {
663 StringRef Name = eatLengthPrefixedName(mangledName);
664 FKind = parseNamePrefix(mangledName&: Name);
665 if (!parseUnmangledName(FullName: Name))
666 return false;
667
668 const ManglingRule& Rule = manglingRules[FuncId];
669 ItaniumParamParser Parser;
670 for (int I=0; I < Rule.maxLeadIndex(); ++I) {
671 Param P;
672 if (!Parser.parseItaniumParam(param&: mangledName, res&: P))
673 return false;
674
675 if ((I + 1) == Rule.Lead[0]) Leads[0] = P;
676 if ((I + 1) == Rule.Lead[1]) Leads[1] = P;
677 }
678 return true;
679}
680
681bool AMDGPUUnmangledLibFunc::parseFuncName(StringRef &Name) {
682 if (!UnmangledFuncInfo::lookup(Name, Id&: FuncId))
683 return false;
684 setName(Name);
685 return true;
686}
687
688bool AMDGPULibFunc::parse(StringRef FuncName, AMDGPULibFunc &F) {
689 if (FuncName.empty()) {
690 F.Impl = std::unique_ptr<AMDGPULibFuncImpl>();
691 return false;
692 }
693
694 if (eatTerm(mangledName&: FuncName, str: "_Z"))
695 F.Impl = std::make_unique<AMDGPUMangledLibFunc>();
696 else
697 F.Impl = std::make_unique<AMDGPUUnmangledLibFunc>();
698 if (F.Impl->parseFuncName(mangledName&: FuncName))
699 return true;
700
701 F.Impl = std::unique_ptr<AMDGPULibFuncImpl>();
702 return false;
703}
704
705///////////////////////////////////////////////////////////////////////////////
706// Mangling
707
708template <typename Stream>
709void AMDGPUMangledLibFunc::writeName(Stream &OS) const {
710 const char *Pfx = "";
711 switch (FKind) {
712 case NATIVE: Pfx = "native_"; break;
713 case HALF: Pfx = "half_"; break;
714 default: break;
715 }
716 if (!Name.empty()) {
717 OS << Pfx << Name;
718 } else if (FuncId != EI_NONE) {
719 OS << Pfx;
720 const StringRef& S = manglingRules[FuncId].Name;
721 OS.write(S.data(), S.size());
722 }
723}
724
725std::string AMDGPUMangledLibFunc::mangle(const AMDGPUOptions &CLOpts) const {
726 return mangleNameItanium(CLOpts);
727}
728
729///////////////////////////////////////////////////////////////////////////////
730// Itanium Mangling
731
732static const char *getItaniumTypeName(AMDGPULibFunc::EType T) {
733 switch (T) {
734 case AMDGPULibFunc::U8: return "h";
735 case AMDGPULibFunc::U16: return "t";
736 case AMDGPULibFunc::U32: return "j";
737 case AMDGPULibFunc::U64: return "m";
738 case AMDGPULibFunc::I8: return "c";
739 case AMDGPULibFunc::I16: return "s";
740 case AMDGPULibFunc::I32: return "i";
741 case AMDGPULibFunc::I64: return "l";
742 case AMDGPULibFunc::F16: return "Dh";
743 case AMDGPULibFunc::F32: return "f";
744 case AMDGPULibFunc::F64: return "d";
745 case AMDGPULibFunc::IMG1DA: return "16ocl_image1darray";
746 case AMDGPULibFunc::IMG1DB: return "17ocl_image1dbuffer";
747 case AMDGPULibFunc::IMG2DA: return "16ocl_image2darray";
748 case AMDGPULibFunc::IMG1D: return "11ocl_image1d";
749 case AMDGPULibFunc::IMG2D: return "11ocl_image2d";
750 case AMDGPULibFunc::IMG3D: return "11ocl_image3d";
751 case AMDGPULibFunc::SAMPLER: return "11ocl_sampler";
752 case AMDGPULibFunc::EVENT: return "9ocl_event";
753 default:
754 llvm_unreachable("Unhandled param type");
755 }
756 return nullptr;
757}
758
759namespace {
760// Itanium mangling ABI says:
761// "5.1.8. Compression
762// ... Each non-terminal in the grammar for which <substitution> appears on the
763// right-hand side is both a source of future substitutions and a candidate
764// for being substituted. There are two exceptions that appear to be
765// substitution candidates from the grammar, but are explicitly excluded:
766// 1. <builtin-type> other than vendor extended types ..."
767
768// For the purpose of functions the following productions make sense for the
769// substitution:
770// <type> ::= <builtin-type>
771// ::= <class-enum-type>
772// ::= <array-type>
773// ::=<CV-qualifiers> <type>
774// ::= P <type> # pointer-to
775// ::= <substitution>
776//
777// Note that while types like images, samplers and events are by the ABI encoded
778// using <class-enum-type> production rule they're not used for substitution
779// because clang consider them as builtin types.
780//
781// DvNN_ type is GCC extension for vectors and is a subject for the
782// substitution.
783
784class ItaniumMangler {
785 SmallVector<AMDGPULibFunc::Param, 10> Str; // list of accumulated substitutions
786 bool UseAddrSpace;
787 bool OCLManglingMismatchWA;
788
789 int findSubst(const AMDGPULibFunc::Param& P) const {
790 for(unsigned I = 0; I < Str.size(); ++I) {
791 const AMDGPULibFunc::Param& T = Str[I];
792 if (P.PtrKind == T.PtrKind &&
793 P.VectorSize == T.VectorSize &&
794 P.ArgType == T.ArgType) {
795 return I;
796 }
797 }
798 return -1;
799 }
800
801 template <typename Stream>
802 bool trySubst(Stream& os, const AMDGPULibFunc::Param& p) {
803 int const subst = findSubst(P: p);
804 if (subst < 0) return false;
805 // Substitutions are mangled as S(XX)?_ where XX is a hexadecimal number
806 // 0 1 2
807 // S_ S0_ S1_
808 if (subst == 0) os << "S_";
809 else os << 'S' << (subst-1) << '_';
810 return true;
811 }
812
813public:
814 ItaniumMangler(bool useAddrSpace, bool OCLManglingMismatchWA)
815 : UseAddrSpace(useAddrSpace),
816 OCLManglingMismatchWA(OCLManglingMismatchWA) {}
817
818 template <typename Stream>
819 void operator()(Stream& os, AMDGPULibFunc::Param p) {
820
821 // Itanium mangling ABI 5.1.8. Compression:
822 // Logically, the substitutable components of a mangled name are considered
823 // left-to-right, components before the composite structure of which they
824 // are a part. If a component has been encountered before, it is substituted
825 // as described below. This decision is independent of whether its components
826 // have been substituted, so an implementation may optimize by considering
827 // large structures for substitution before their components. If a component
828 // has not been encountered before, its mangling is identified, and it is
829 // added to a dictionary of substitution candidates. No entity is added to
830 // the dictionary twice.
831 AMDGPULibFunc::Param Ptr;
832
833 if (p.PtrKind) {
834 if (trySubst(os, p)) return;
835 os << 'P';
836 if (p.PtrKind & AMDGPULibFunc::CONST) os << 'K';
837 if (p.PtrKind & AMDGPULibFunc::VOLATILE) os << 'V';
838 unsigned AS = UseAddrSpace
839 ? AMDGPULibFuncBase::getAddrSpaceFromEPtrKind(Kind: p.PtrKind)
840 : 0;
841 if (OCLManglingMismatchWA || AS != 0)
842 os << "U3AS" << AS;
843 Ptr = p;
844 p.PtrKind = 0;
845 }
846
847 if (p.VectorSize > 1) {
848 if (trySubst(os, p)) goto exit;
849 Str.push_back(Elt: p);
850 os << "Dv" << static_cast<unsigned>(p.VectorSize) << '_';
851 }
852
853 os << getItaniumTypeName(T: (AMDGPULibFunc::EType)p.ArgType);
854
855 exit:
856 if (Ptr.ArgType) Str.push_back(Elt: Ptr);
857 }
858};
859} // namespace
860
861std::string
862AMDGPUMangledLibFunc::mangleNameItanium(const AMDGPUOptions &CLOpts) const {
863 SmallString<128> Buf;
864 raw_svector_ostream S(Buf);
865 SmallString<128> NameBuf;
866 raw_svector_ostream Name(NameBuf);
867 writeName(OS&: Name);
868 const StringRef& NameStr = Name.str();
869 S << "_Z" << static_cast<int>(NameStr.size()) << NameStr;
870
871 ItaniumMangler Mangler(true, CLOpts.enable_ocl_mangling_mismatch_workaround);
872 ParamIterator I(Leads, manglingRules[FuncId]);
873 Param P;
874 while ((P = I.getNextParam()).ArgType != 0)
875 Mangler(S, P);
876 return std::string(S.str());
877}
878
879///////////////////////////////////////////////////////////////////////////////
880// Misc
881
882AMDGPULibFuncBase::Param AMDGPULibFuncBase::Param::getFromTy(Type *Ty,
883 bool Signed) {
884 Param P;
885 if (FixedVectorType *VT = dyn_cast<FixedVectorType>(Val: Ty)) {
886 P.VectorSize = static_cast<unsigned char>(VT->getNumElements());
887 Ty = VT->getElementType();
888 }
889
890 switch (Ty->getTypeID()) {
891 case Type::FloatTyID:
892 P.ArgType = AMDGPULibFunc::F32;
893 break;
894 case Type::DoubleTyID:
895 P.ArgType = AMDGPULibFunc::F64;
896 break;
897 case Type::HalfTyID:
898 P.ArgType = AMDGPULibFunc::F16;
899 break;
900 case Type::IntegerTyID:
901 switch (cast<IntegerType>(Val: Ty)->getBitWidth()) {
902 case 8:
903 P.ArgType = Signed ? AMDGPULibFunc::I8 : AMDGPULibFunc::U8;
904 break;
905 case 16:
906 P.ArgType = Signed ? AMDGPULibFunc::I16 : AMDGPULibFunc::U16;
907 break;
908 case 32:
909 P.ArgType = Signed ? AMDGPULibFunc::I32 : AMDGPULibFunc::U32;
910 break;
911 case 64:
912 P.ArgType = Signed ? AMDGPULibFunc::I64 : AMDGPULibFunc::U64;
913 break;
914 default:
915 llvm_unreachable("unhandled libcall argument type");
916 }
917
918 break;
919 default:
920 llvm_unreachable("unhandled libcall argument type");
921 }
922
923 return P;
924}
925
926static Type *getIntrinsicParamType(LLVMContext &C,
927 const AMDGPULibFunc::Param &P,
928 bool UseAddrSpace) {
929 Type *T = nullptr;
930 switch (P.ArgType) {
931 default:
932 return nullptr;
933 case AMDGPULibFunc::U8:
934 case AMDGPULibFunc::I8:
935 T = Type::getInt8Ty(C);
936 break;
937 case AMDGPULibFunc::U16:
938 case AMDGPULibFunc::I16:
939 T = Type::getInt16Ty(C);
940 break;
941 case AMDGPULibFunc::U32:
942 case AMDGPULibFunc::I32:
943 T = Type::getInt32Ty(C);
944 break;
945 case AMDGPULibFunc::U64:
946 case AMDGPULibFunc::I64:
947 T = Type::getInt64Ty(C);
948 break;
949 case AMDGPULibFunc::F16:
950 T = Type::getHalfTy(C);
951 break;
952 case AMDGPULibFunc::F32:
953 T = Type::getFloatTy(C);
954 break;
955 case AMDGPULibFunc::F64:
956 T = Type::getDoubleTy(C);
957 break;
958
959 case AMDGPULibFunc::IMG1DA:
960 case AMDGPULibFunc::IMG1DB:
961 case AMDGPULibFunc::IMG2DA:
962 case AMDGPULibFunc::IMG1D:
963 case AMDGPULibFunc::IMG2D:
964 case AMDGPULibFunc::IMG3D:
965 case AMDGPULibFunc::SAMPLER:
966 case AMDGPULibFunc::EVENT:
967 T = PointerType::getUnqual(C);
968 break;
969 case AMDGPULibFunc::B8:
970 case AMDGPULibFunc::B16:
971 case AMDGPULibFunc::B32:
972 case AMDGPULibFunc::B64:
973 case AMDGPULibFunc::SIZE_MASK:
974 case AMDGPULibFunc::FLOAT:
975 case AMDGPULibFunc::INT:
976 case AMDGPULibFunc::UINT:
977 case AMDGPULibFunc::DUMMY:
978 return nullptr;
979 }
980 if (P.VectorSize > 1)
981 T = FixedVectorType::get(ElementType: T, NumElts: P.VectorSize);
982 if (P.PtrKind != AMDGPULibFunc::BYVALUE)
983 T = PointerType::get(
984 C, AddressSpace: UseAddrSpace ? ((P.PtrKind & AMDGPULibFunc::ADDR_SPACE) - 1) : 0);
985 return T;
986}
987
988FunctionType *AMDGPUMangledLibFunc::getFunctionType(const Module &M) const {
989 LLVMContext& C = M.getContext();
990 std::vector<Type*> Args;
991 ParamIterator I(Leads, manglingRules[FuncId]);
992 Param P;
993 while ((P = I.getNextParam()).ArgType != 0) {
994 Type *ParamTy = getIntrinsicParamType(C, P, UseAddrSpace: true);
995 if (!ParamTy)
996 return nullptr;
997
998 Args.push_back(x: ParamTy);
999 }
1000
1001 Type *RetTy = getIntrinsicParamType(C, P: getRetType(id: FuncId, Leads), UseAddrSpace: true);
1002 if (!RetTy)
1003 return nullptr;
1004
1005 return FunctionType::get(Result: RetTy, Params: Args, isVarArg: false);
1006}
1007
1008unsigned AMDGPUMangledLibFunc::getNumArgs() const {
1009 return manglingRules[FuncId].getNumArgs();
1010}
1011
1012unsigned AMDGPUUnmangledLibFunc::getNumArgs() const {
1013 return UnmangledFuncInfo::getNumArgs(Id: FuncId);
1014}
1015
1016std::string AMDGPUMangledLibFunc::getName() const {
1017 SmallString<128> Buf;
1018 raw_svector_ostream OS(Buf);
1019 writeName(OS);
1020 return std::string(OS.str());
1021}
1022
1023bool AMDGPULibFunc::isCompatibleSignature(const Module &M,
1024 const FunctionType *CallTy) const {
1025 const FunctionType *FuncTy = getFunctionType(M);
1026
1027 if (!FuncTy) {
1028 // Give up on mangled functions with unexpected types.
1029 if (AMDGPULibFuncBase::isMangled(Id: getId()))
1030 return false;
1031
1032 // FIXME: UnmangledFuncInfo does not have any type information other than
1033 // the number of arguments.
1034 return getNumArgs() == CallTy->getNumParams();
1035 }
1036
1037 // Normally the types should exactly match.
1038 if (FuncTy == CallTy)
1039 return true;
1040
1041 const unsigned NumParams = FuncTy->getNumParams();
1042 if (NumParams != CallTy->getNumParams())
1043 return false;
1044
1045 for (unsigned I = 0; I != NumParams; ++I) {
1046 Type *FuncArgTy = FuncTy->getParamType(i: I);
1047 Type *CallArgTy = CallTy->getParamType(i: I);
1048 if (FuncArgTy == CallArgTy)
1049 continue;
1050
1051 // Some cases permit implicit splatting a scalar value to a vector argument.
1052 auto *FuncVecTy = dyn_cast<VectorType>(Val: FuncArgTy);
1053 if (FuncVecTy && FuncVecTy->getElementType() == CallArgTy &&
1054 allowsImplicitVectorSplat(ArgIdx: I))
1055 continue;
1056
1057 return false;
1058 }
1059
1060 return true;
1061}
1062
1063Function *AMDGPULibFunc::getFunction(Module *M, const AMDGPULibFunc &fInfo) {
1064 std::string FuncName =
1065 fInfo.mangle(CLOpts: M->getContext().getLibraryOptions().get<AMDGPUOptions>());
1066 Function *F = dyn_cast_or_null<Function>(
1067 Val: M->getValueSymbolTable().lookup(Name: FuncName));
1068 if (!F || F->isDeclaration())
1069 return nullptr;
1070
1071 if (F->hasFnAttribute(Kind: Attribute::NoBuiltin))
1072 return nullptr;
1073
1074 if (!fInfo.isCompatibleSignature(M: *M, CallTy: F->getFunctionType()))
1075 return nullptr;
1076
1077 switch (fInfo.getId()) {
1078 case AMDGPULibFunc::EI_POW_FAST:
1079 case AMDGPULibFunc::EI_POWR_FAST:
1080 case AMDGPULibFunc::EI_POWN_FAST:
1081 case AMDGPULibFunc::EI_ROOTN_FAST:
1082 // TODO: Remove this. This is not a real module flag used anywhere. This is
1083 // a bringup hack so this transform is testable prior to the library
1084 // functions existing.
1085 if (!M->getModuleFlag(Key: "amdgpu-libcall-have-fast-pow"))
1086 return nullptr;
1087 break;
1088 default:
1089 break;
1090 }
1091
1092 return F;
1093}
1094
1095FunctionCallee AMDGPULibFunc::getOrInsertFunction(Module *M,
1096 const AMDGPULibFunc &fInfo) {
1097 std::string const FuncName =
1098 fInfo.mangle(CLOpts: M->getContext().getLibraryOptions().get<AMDGPUOptions>());
1099 Function *F = dyn_cast_or_null<Function>(
1100 Val: M->getValueSymbolTable().lookup(Name: FuncName));
1101
1102 if (F) {
1103 if (F->hasFnAttribute(Kind: Attribute::NoBuiltin))
1104 return nullptr;
1105 if (!F->isDeclaration() &&
1106 fInfo.isCompatibleSignature(M: *M, CallTy: F->getFunctionType()))
1107 return F;
1108 }
1109
1110 FunctionType *FuncTy = fInfo.getFunctionType(M: *M);
1111 assert(FuncTy);
1112
1113 bool hasPtr = false;
1114 for (FunctionType::param_iterator
1115 PI = FuncTy->param_begin(),
1116 PE = FuncTy->param_end();
1117 PI != PE; ++PI) {
1118 const Type* argTy = static_cast<const Type*>(*PI);
1119 if (argTy->isPointerTy()) {
1120 hasPtr = true;
1121 break;
1122 }
1123 }
1124
1125 FunctionCallee C;
1126 if (hasPtr) {
1127 // Do not set extra attributes for functions with pointer arguments.
1128 C = M->getOrInsertFunction(Name: FuncName, T: FuncTy);
1129 } else {
1130 AttributeList Attr;
1131 LLVMContext &Ctx = M->getContext();
1132 Attr = Attr.addFnAttribute(
1133 C&: Ctx, Attr: Attribute::getWithMemoryEffects(Context&: Ctx, ME: MemoryEffects::readOnly()));
1134 Attr = Attr.addFnAttribute(C&: Ctx, Kind: Attribute::NoUnwind);
1135 C = M->getOrInsertFunction(Name: FuncName, T: FuncTy, AttributeList: Attr);
1136 }
1137
1138 return C;
1139}
1140
1141StringMap<unsigned> UnmangledFuncInfo::buildNameMap() {
1142 StringMap<unsigned> Map;
1143 for (unsigned I = 0; I != TableSize; ++I)
1144 Map[Table[I].Name] = I;
1145 return Map;
1146}
1147
1148bool UnmangledFuncInfo::lookup(StringRef Name, ID &Id) {
1149 static const StringMap<unsigned> Map = buildNameMap();
1150 auto Loc = Map.find(Key: Name);
1151 if (Loc != Map.end()) {
1152 Id = toFuncId(Index: Loc->second);
1153 return true;
1154 }
1155 Id = AMDGPULibFunc::EI_NONE;
1156 return false;
1157}
1158
1159AMDGPULibFunc::AMDGPULibFunc(const AMDGPULibFunc &F) {
1160 if (auto *MF = dyn_cast<AMDGPUMangledLibFunc>(Val: F.Impl.get()))
1161 Impl = std::make_unique<AMDGPUMangledLibFunc>(args&: *MF);
1162 else if (auto *UMF = dyn_cast<AMDGPUUnmangledLibFunc>(Val: F.Impl.get()))
1163 Impl = std::make_unique<AMDGPUUnmangledLibFunc>(args&: *UMF);
1164 else
1165 Impl = std::unique_ptr<AMDGPULibFuncImpl>();
1166}
1167
1168AMDGPULibFunc &AMDGPULibFunc::operator=(const AMDGPULibFunc &F) {
1169 if (this == &F)
1170 return *this;
1171
1172 this->~AMDGPULibFunc();
1173 new (this) AMDGPULibFunc(F);
1174 return *this;
1175}
1176
1177AMDGPULibFunc::AMDGPULibFunc(EFuncId Id, const AMDGPULibFunc &CopyFrom) {
1178 assert(AMDGPULibFuncBase::isMangled(Id) && CopyFrom.isMangled() &&
1179 "not supported");
1180 Impl = std::make_unique<AMDGPUMangledLibFunc>(
1181 args&: Id, args&: *cast<AMDGPUMangledLibFunc>(Val: CopyFrom.Impl.get()));
1182}
1183
1184AMDGPULibFunc::AMDGPULibFunc(EFuncId Id, FunctionType *FT, bool SignedInts) {
1185 Impl = std::make_unique<AMDGPUMangledLibFunc>(args&: Id, args&: FT, args&: SignedInts);
1186}
1187
1188AMDGPULibFunc::AMDGPULibFunc(StringRef Name, FunctionType *FT) {
1189 Impl = std::make_unique<AMDGPUUnmangledLibFunc>(args&: Name, args&: FT);
1190}
1191
1192void AMDGPULibFunc::initMangled() {
1193 Impl = std::make_unique<AMDGPUMangledLibFunc>();
1194}
1195
1196AMDGPULibFunc::Param *AMDGPULibFunc::getLeads() {
1197 if (!Impl)
1198 initMangled();
1199 return cast<AMDGPUMangledLibFunc>(Val: Impl.get())->Leads;
1200}
1201
1202const AMDGPULibFunc::Param *AMDGPULibFunc::getLeads() const {
1203 return cast<const AMDGPUMangledLibFunc>(Val: Impl.get())->Leads;
1204}
1205