[obj2yaml] - Fix BB after r373315.
[llvm-complete.git] / lib / Target / AMDGPU / AMDGPULibFunc.cpp
blobe1ae496d9cbca2c3c45db35ebda59dcc8d17e2a6
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
11 //===----------------------------------------------------------------------===//
13 #include "AMDGPU.h"
14 #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>
26 using namespace llvm;
28 namespace {
30 enum 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,
57 struct ManglingRule {
58 const char *Name;
59 unsigned char Lead[2];
60 unsigned char Param[5];
62 int maxLeadIndex() const { return (std::max)(Lead[0], Lead[1]); }
63 int getNumLeads() const { return (Lead[0] ? 1 : 0) + (Lead[1] ? 1 : 0); }
65 unsigned getNumArgs() const;
67 static StringMap<int> buildManglingRulesMap();
70 // Information about library functions with unmangled names.
71 class UnmangledFuncInfo {
72 const char *Name;
73 unsigned NumArgs;
75 // Table for all lib functions with unmangled names.
76 static const UnmangledFuncInfo Table[];
78 // Number of entries in Table.
79 static const unsigned TableSize;
81 static StringMap<unsigned> buildNameMap();
83 public:
84 using ID = AMDGPULibFunc::EFuncId;
85 constexpr UnmangledFuncInfo(const char *_Name, unsigned _NumArgs)
86 : 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);
96 static ID toFuncId(unsigned Index) {
97 assert(Index < TableSize &&
98 "Invalid unmangled library function");
99 return static_cast<ID>(
100 Index + 1 + static_cast<unsigned>(AMDGPULibFunc::EI_LAST_MANGLED));
102 static unsigned getNumArgs(ID Id) { return Table[toIndex(Id)].NumArgs; }
103 static StringRef getName(ID Id) { return Table[toIndex(Id)].Name; }
106 unsigned ManglingRule::getNumArgs() const {
107 unsigned I=0;
108 while (I < (sizeof Param/sizeof Param[0]) && Param[I]) ++I;
109 return I;
112 // This table describes function formal argument type rules. The order of rules
113 // corresponds to the EFuncId enum at AMDGPULibFunc.h
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
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.
136 static constexpr ManglingRule manglingRules[] = {
137 { "", {0}, {0} },
138 { "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}},
256 { "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}},
323 { "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} },
337 // Library functions with unmangled name.
338 const UnmangledFuncInfo UnmangledFuncInfo::Table[] = {
339 {"__read_pipe_2", 4},
340 {"__read_pipe_4", 6},
341 {"__write_pipe_2", 4},
342 {"__write_pipe_4", 6},
345 const unsigned UnmangledFuncInfo::TableSize =
346 array_lengthof(UnmangledFuncInfo::Table);
348 static 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;
359 return Res;
362 class ParamIterator {
363 const AMDGPULibFunc::Param (&Leads)[2];
364 const ManglingRule& Rule;
365 int Index;
366 public:
367 ParamIterator(const AMDGPULibFunc::Param (&leads)[2],
368 const ManglingRule& rule)
369 : Leads(leads), Rule(rule), Index(0) {}
371 AMDGPULibFunc::Param getNextParam();
374 AMDGPULibFunc::Param ParamIterator::getNextParam() {
375 AMDGPULibFunc::Param P;
376 if (Index >= int(sizeof Rule.Param/sizeof Rule.Param[0])) return P;
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];
400 switch (R) {
401 case E_ANY:
402 case E_COPY: break;
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;
425 case E_MAKEBASE_UNS:
426 P.ArgType &= ~AMDGPULibFunc::BASE_TYPE_MASK;
427 P.ArgType |= AMDGPULibFunc::UINT;
428 break;
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;
439 P.PtrKind = AMDGPULibFunc::BYVALUE;
440 P.ArgType = AMDGPULibFunc::I32;
441 break;
443 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;
449 P.PtrKind = AMDGPULibFunc::getEPtrKindFromAddrSpace(AS);
450 P.PtrKind |= AMDGPULibFunc::CONST;
451 break;
454 default: llvm_unreachable("Unhandeled param rule");
457 ++Index;
458 return P;
461 inline static void drop_front(StringRef& str, size_t n = 1) {
462 str = str.drop_front(n);
465 static bool eatTerm(StringRef& mangledName, const char c) {
466 if (mangledName.front() == c) {
467 drop_front(mangledName);
468 return true;
470 return false;
473 template <size_t N>
474 static 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;
479 return false;
482 static inline bool isDigit(char c) { return c >= '0' && c <= '9'; }
484 static 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);
491 return s.size() < savedSize ? n : -1;
494 static 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;
503 } // end anonymous namespace
505 AMDGPUMangledLibFunc::AMDGPUMangledLibFunc() {
506 FuncId = EI_NONE;
507 FKind = NOPFX;
508 Leads[0].reset();
509 Leads[1].reset();
510 Name.clear();
513 AMDGPUUnmangledLibFunc::AMDGPUUnmangledLibFunc() {
514 FuncId = EI_NONE;
515 FuncTy = nullptr;
518 AMDGPUMangledLibFunc::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];
526 ///////////////////////////////////////////////////////////////////////////////
527 // Demangling
529 static 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;
537 return 1;
540 static 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);
548 if (Pfx != AMDGPULibFunc::NOPFX)
549 mangledName = P.second;
551 return Pfx;
554 StringMap<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;
562 bool AMDGPUMangledLibFunc::parseUnmangledName(StringRef FullName) {
563 static const StringMap<int> manglingRulesMap =
564 ManglingRule::buildManglingRulesMap();
565 FuncId = static_cast<EFuncId>(manglingRulesMap.lookup(FullName));
566 return FuncId != EI_NONE;
569 ///////////////////////////////////////////////////////////////////////////////
570 // Itanium Demangling
572 namespace {
573 struct ItaniumParamParser {
574 AMDGPULibFunc::Param Prev;
575 bool parseItaniumParam(StringRef& param, AMDGPULibFunc::Param &res);
577 } // namespace
579 bool ItaniumParamParser::parseItaniumParam(StringRef& param,
580 AMDGPULibFunc::Param &res) {
581 res.reset();
582 if (param.empty()) return false;
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;
588 unsigned AS;
589 if (!eatTerm(param, "U3AS")) {
590 AS = 0;
591 } else {
592 AS = param.front() - '0';
593 drop_front(param, 1);
595 res.PtrKind |= AMDGPULibFuncBase::getEPtrKindFromAddrSpace(AS);
596 } else {
597 res.PtrKind = AMDGPULibFunc::BYVALUE;
600 // parse vector size
601 if (eatTerm(param,"Dv")) {
602 res.VectorSize = parseVecSize(param);
603 if (res.VectorSize==1 || !eatTerm(param, '_')) return false;
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;
640 res.VectorSize = Prev.VectorSize;
641 res.ArgType = Prev.ArgType;
642 break;
643 default:;
646 if (res.ArgType == 0) return false;
647 Prev.VectorSize = res.VectorSize;
648 Prev.ArgType = res.ArgType;
649 return true;
652 bool AMDGPUMangledLibFunc::parseFuncName(StringRef &mangledName) {
653 StringRef Name = eatLengthPrefixedName(mangledName);
654 FKind = parseNamePrefix(Name);
655 if (!parseUnmangledName(Name))
656 return false;
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;
665 if ((I + 1) == Rule.Lead[0]) Leads[0] = P;
666 if ((I + 1) == Rule.Lead[1]) Leads[1] = P;
668 return true;
671 bool AMDGPUUnmangledLibFunc::parseFuncName(StringRef &Name) {
672 if (!UnmangledFuncInfo::lookup(Name, FuncId))
673 return false;
674 setName(Name);
675 return true;
678 bool AMDGPULibFunc::parse(StringRef FuncName, AMDGPULibFunc &F) {
679 if (FuncName.empty()) {
680 F.Impl = std::unique_ptr<AMDGPULibFuncImpl>();
681 return false;
684 if (eatTerm(FuncName, "_Z"))
685 F.Impl = std::make_unique<AMDGPUMangledLibFunc>();
686 else
687 F.Impl = std::make_unique<AMDGPUUnmangledLibFunc>();
688 if (F.Impl->parseFuncName(FuncName))
689 return true;
691 F.Impl = std::unique_ptr<AMDGPULibFuncImpl>();
692 return false;
695 StringRef AMDGPUMangledLibFunc::getUnmangledName(StringRef mangledName) {
696 StringRef S = mangledName;
697 if (eatTerm(S, "_Z"))
698 return eatLengthPrefixedName(S);
699 return StringRef();
702 ///////////////////////////////////////////////////////////////////////////////
703 // Mangling
705 template <typename Stream>
706 void AMDGPUMangledLibFunc::writeName(Stream &OS) const {
707 const char *Pfx = "";
708 switch (FKind) {
709 case NATIVE: Pfx = "native_"; break;
710 case HALF: Pfx = "half_"; break;
711 default: break;
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());
722 std::string AMDGPUMangledLibFunc::mangle() const { return mangleNameItanium(); }
724 ///////////////////////////////////////////////////////////////////////////////
725 // Itanium Mangling
727 static 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");
750 return nullptr;
753 namespace {
754 // 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 ..."
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>
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.
775 // DvNN_ type is GCC extension for vectors and is a subject for the substitution.
778 class ItaniumMangler {
779 SmallVector<AMDGPULibFunc::Param, 10> Str; // list of accumulated substituions
780 bool UseAddrSpace;
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;
791 return -1;
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;
806 public:
807 ItaniumMangler(bool useAddrSpace)
808 : UseAddrSpace(useAddrSpace) {}
810 template <typename Stream>
811 void operator()(Stream& os, AMDGPULibFunc::Param p) {
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;
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';
830 unsigned AS = UseAddrSpace
831 ? AMDGPULibFuncBase::getAddrSpaceFromEPtrKind(p.PtrKind)
832 : 0;
833 if (AS != 0) os << "U3AS" << AS;
834 Ptr = p;
835 p.PtrKind = 0;
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) << '_';
844 os << getItaniumTypeName((AMDGPULibFunc::EType)p.ArgType);
846 exit:
847 if (Ptr.ArgType) Str.push_back(Ptr);
850 } // namespace
852 std::string AMDGPUMangledLibFunc::mangleNameItanium() const {
853 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;
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();
869 ///////////////////////////////////////////////////////////////////////////////
870 // Misc
872 static 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;
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;
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;
914 FunctionType *AMDGPUMangledLibFunc::getFunctionType(Module &M) const {
915 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));
922 return FunctionType::get(
923 getIntrinsicParamType(C, getRetType(FuncId, Leads), true),
924 Args, false);
927 unsigned AMDGPUMangledLibFunc::getNumArgs() const {
928 return manglingRules[FuncId].getNumArgs();
931 unsigned AMDGPUUnmangledLibFunc::getNumArgs() const {
932 return UnmangledFuncInfo::getNumArgs(FuncId);
935 std::string AMDGPUMangledLibFunc::getName() const {
936 SmallString<128> Buf;
937 raw_svector_ostream OS(Buf);
938 writeName(OS);
939 return OS.str();
942 Function *AMDGPULibFunc::getFunction(Module *M, const AMDGPULibFunc &fInfo) {
943 std::string FuncName = fInfo.mangle();
944 Function *F = dyn_cast_or_null<Function>(
945 M->getValueSymbolTable().lookup(FuncName));
947 // check formal with actual types conformance
948 if (F && !F->isDeclaration()
949 && !F->isVarArg()
950 && F->arg_size() == fInfo.getNumArgs()) {
951 return F;
953 return nullptr;
956 FunctionCallee AMDGPULibFunc::getOrInsertFunction(Module *M,
957 const AMDGPULibFunc &fInfo) {
958 std::string const FuncName = fInfo.mangle();
959 Function *F = dyn_cast_or_null<Function>(
960 M->getValueSymbolTable().lookup(FuncName));
962 // check formal with actual types conformance
963 if (F && !F->isDeclaration()
964 && !F->isVarArg()
965 && F->arg_size() == fInfo.getNumArgs()) {
966 return F;
969 FunctionType *FuncTy = fInfo.getFunctionType(*M);
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;
983 FunctionCallee C;
984 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();
990 Attr = Attr.addAttribute(Ctx, AttributeList::FunctionIndex,
991 Attribute::ReadOnly);
992 Attr = Attr.addAttribute(Ctx, AttributeList::FunctionIndex,
993 Attribute::NoUnwind);
994 C = M->getOrInsertFunction(FuncName, FuncTy, Attr);
997 return C;
1000 StringMap<unsigned> UnmangledFuncInfo::buildNameMap() {
1001 StringMap<unsigned> Map;
1002 for (unsigned I = 0; I != TableSize; ++I)
1003 Map[Table[I].Name] = I;
1004 return Map;
1007 bool UnmangledFuncInfo::lookup(StringRef Name, ID &Id) {
1008 static const StringMap<unsigned> Map = buildNameMap();
1009 auto Loc = Map.find(Name);
1010 if (Loc != Map.end()) {
1011 Id = toFuncId(Loc->second);
1012 return true;
1014 Id = AMDGPULibFunc::EI_NONE;
1015 return false;
1018 AMDGPULibFunc::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>();
1027 AMDGPULibFunc &AMDGPULibFunc::operator=(const AMDGPULibFunc &F) {
1028 if (this == &F)
1029 return *this;
1030 new (this) AMDGPULibFunc(F);
1031 return *this;
1034 AMDGPULibFunc::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())));
1041 AMDGPULibFunc::AMDGPULibFunc(StringRef Name, FunctionType *FT) {
1042 Impl.reset(new AMDGPUUnmangledLibFunc(Name, FT));
1045 void AMDGPULibFunc::initMangled() { Impl.reset(new AMDGPUMangledLibFunc()); }
1047 AMDGPULibFunc::Param *AMDGPULibFunc::getLeads() {
1048 if (!Impl)
1049 initMangled();
1050 return cast<AMDGPUMangledLibFunc>(Impl.get())->Leads;
1053 const AMDGPULibFunc::Param *AMDGPULibFunc::getLeads() const {
1054 return cast<const AMDGPUMangledLibFunc>(Impl.get())->Leads;