1 //===-- AMDGPULibFunc.cpp -------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file contains utility functions to work with Itanium mangled names
11 //===----------------------------------------------------------------------===//
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>
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 {
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();
84 using ID = AMDGPULibFunc::EFuncId;
85 UnmangledFuncInfo(StringRef _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 {
108 while (I < (sizeof Param/sizeof Param[0]) && Param[I]) ++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> }
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 const ManglingRule manglingRules[] = {
137 { StringRef(), {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
353 case AMDGPULibFunc::EI_SINCOS:
354 Res.PtrKind = AMDGPULibFunc::BYVALUE;
362 class ParamIterator {
363 const AMDGPULibFunc::Param (&Leads)[2];
364 const ManglingRule& Rule;
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];
382 P.ArgType = AMDGPULibFunc::U32; break;
384 P.ArgType = AMDGPULibFunc::I32; P.VectorSize = 4; break;
386 P.ArgType = AMDGPULibFunc::U32; P.VectorSize = 4; break;
388 P.ArgType = AMDGPULibFunc::F32; P.VectorSize = 4; break;
390 P.ArgType = AMDGPULibFunc::U64; break;
392 P.ArgType = AMDGPULibFunc::EVENT; break;
394 P.ArgType = AMDGPULibFunc::SAMPLER; break;
395 case EX_RESERVEDID: break; // TBD
397 if (Index == (Rule.Lead[1] - 1)) P = Leads[1];
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;
417 P.PtrKind |= AMDGPULibFunc::CONST; break;
419 P.PtrKind |= AMDGPULibFunc::VOLATILE; break;
421 P.ArgType = AMDGPULibFunc::I32; break;
423 P.ArgType = AMDGPULibFunc::U32; break;
426 P.ArgType &= ~AMDGPULibFunc::BASE_TYPE_MASK;
427 P.ArgType |= AMDGPULibFunc::UINT;
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;
443 case E_CONSTPTR_SWAPGL: {
444 unsigned AS = AMDGPULibFunc::getAddrSpaceFromEPtrKind(P.PtrKind);
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;
454 default: llvm_unreachable("Unhandeled param rule");
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);
474 static bool eatTerm(StringRef& mangledName, const char (&str)[N]) {
475 if (mangledName.startswith(StringRef(str, N-1))) {
476 drop_front(mangledName, N-1);
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();
487 while (!s.empty() && isDigit(s.front())) {
488 n = n*10 + s.front() - '0';
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())
498 StringRef Res = mangledName.substr(0, Len);
499 drop_front(mangledName, Len);
503 } // end anonymous namespace
505 AMDGPUMangledLibFunc::AMDGPUMangledLibFunc() {
513 AMDGPUUnmangledLibFunc::AMDGPUUnmangledLibFunc() {
518 AMDGPUMangledLibFunc::AMDGPUMangledLibFunc(
519 EFuncId id, const AMDGPUMangledLibFunc ©From) {
521 FKind = copyFrom.FKind;
522 Leads[0] = copyFrom.Leads[0];
523 Leads[1] = copyFrom.Leads[1];
526 ///////////////////////////////////////////////////////////////////////////////
529 static int parseVecSize(StringRef& mangledName) {
530 size_t const Len = eatNumber(mangledName);
532 case 2: case 3: case 4: case 8: case 16:
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;
554 StringMap<int> ManglingRule::buildManglingRulesMap() {
555 StringMap<int> Map(array_lengthof(manglingRules));
557 for (auto Rule : manglingRules)
558 Map.insert({Rule.Name, Id++});
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
573 struct ItaniumParamParser {
574 AMDGPULibFunc::Param Prev;
575 bool parseItaniumParam(StringRef& param, AMDGPULibFunc::Param &res);
579 bool ItaniumParamParser::parseItaniumParam(StringRef& param,
580 AMDGPULibFunc::Param &res) {
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;
589 if (!eatTerm(param, "U3AS")) {
592 AS = param.front() - '0';
593 drop_front(param, 1);
595 res.PtrKind |= AMDGPULibFuncBase::getEPtrKindFromAddrSpace(AS);
597 res.PtrKind = AMDGPULibFunc::BYVALUE;
601 if (eatTerm(param,"Dv")) {
602 res.VectorSize = parseVecSize(param);
603 if (res.VectorSize==1 || !eatTerm(param, '_')) return false;
607 char const TC = param.front();
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);
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;
636 if (!eatTerm(param, '_')) {
638 if (!eatTerm(param, '_')) return false;
640 res.VectorSize = Prev.VectorSize;
641 res.ArgType = Prev.ArgType;
646 if (res.ArgType == 0) return false;
647 Prev.VectorSize = res.VectorSize;
648 Prev.ArgType = res.ArgType;
652 bool AMDGPUMangledLibFunc::parseFuncName(StringRef &mangledName) {
653 StringRef Name = eatLengthPrefixedName(mangledName);
654 FKind = parseNamePrefix(Name);
655 if (!parseUnmangledName(Name))
658 const ManglingRule& Rule = manglingRules[FuncId];
659 ItaniumParamParser Parser;
660 for (int I=0; I < Rule.maxLeadIndex(); ++I) {
662 if (!Parser.parseItaniumParam(mangledName, P))
665 if ((I + 1) == Rule.Lead[0]) Leads[0] = P;
666 if ((I + 1) == Rule.Lead[1]) Leads[1] = P;
671 bool AMDGPUUnmangledLibFunc::parseFuncName(StringRef &Name) {
672 if (!UnmangledFuncInfo::lookup(Name, FuncId))
678 bool AMDGPULibFunc::parse(StringRef FuncName, AMDGPULibFunc &F) {
679 if (FuncName.empty()) {
680 F.Impl = std::unique_ptr<AMDGPULibFuncImpl>();
684 if (eatTerm(FuncName, "_Z"))
685 F.Impl = make_unique<AMDGPUMangledLibFunc>();
687 F.Impl = make_unique<AMDGPUUnmangledLibFunc>();
688 if (F.Impl->parseFuncName(FuncName))
691 F.Impl = std::unique_ptr<AMDGPULibFuncImpl>();
695 StringRef AMDGPUMangledLibFunc::getUnmangledName(StringRef mangledName) {
696 StringRef S = mangledName;
697 if (eatTerm(S, "_Z"))
698 return eatLengthPrefixedName(S);
702 ///////////////////////////////////////////////////////////////////////////////
705 template <typename Stream>
706 void AMDGPUMangledLibFunc::writeName(Stream &OS) const {
707 const char *Pfx = "";
709 case NATIVE: Pfx = "native_"; break;
710 case HALF: Pfx = "half_"; break;
715 } else if (FuncId != EI_NONE) {
717 const StringRef& S = manglingRules[FuncId].Name;
718 OS.write(S.data(), S.size());
722 std::string AMDGPUMangledLibFunc::mangle() const { return mangleNameItanium(); }
724 ///////////////////////////////////////////////////////////////////////////////
727 static const char *getItaniumTypeName(AMDGPULibFunc::EType 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");
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
764 // <type> ::= <builtin-type>
765 // ::= <class-enum-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
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) {
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
801 if (subst == 0) os << "S_";
802 else os << 'S' << (subst-1) << '_';
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;
826 if (trySubst(os, p)) return;
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)
833 if (AS != 0) os << "U3AS" << AS;
838 if (p.VectorSize > 1) {
839 if (trySubst(os, p)) goto exit;
841 os << "Dv" << static_cast<unsigned>(p.VectorSize) << '_';
844 os << getItaniumTypeName((AMDGPULibFunc::EType)p.ArgType);
847 if (Ptr.ArgType) Str.push_back(Ptr);
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);
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]);
864 while ((P = I.getNextParam()).ArgType != 0)
869 ///////////////////////////////////////////////////////////////////////////////
872 static Type* getIntrinsicParamType(
874 const AMDGPULibFunc::Param& P,
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;
902 llvm_unreachable("Unhandeled param type");
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)
914 FunctionType *AMDGPUMangledLibFunc::getFunctionType(Module &M) const {
915 LLVMContext& C = M.getContext();
916 std::vector<Type*> Args;
917 ParamIterator I(Leads, manglingRules[FuncId]);
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),
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);
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()
950 && F->arg_size() == fInfo.getNumArgs()) {
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()
965 && F->arg_size() == fInfo.getNumArgs()) {
969 FunctionType *FuncTy = fInfo.getFunctionType(*M);
972 for (FunctionType::param_iterator
973 PI = FuncTy->param_begin(),
974 PE = FuncTy->param_end();
976 const Type* argTy = static_cast<const Type*>(*PI);
977 if (argTy->isPointerTy()) {
985 // Do not set extra attributes for functions with pointer arguments.
986 C = M->getOrInsertFunction(FuncName, FuncTy);
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);
1000 StringMap<unsigned> UnmangledFuncInfo::buildNameMap() {
1001 StringMap<unsigned> Map;
1002 for (unsigned I = 0; I != TableSize; ++I)
1003 Map[Table[I].Name] = I;
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);
1014 Id = AMDGPULibFunc::EI_NONE;
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));
1024 Impl = std::unique_ptr<AMDGPULibFuncImpl>();
1027 AMDGPULibFunc &AMDGPULibFunc::operator=(const AMDGPULibFunc &F) {
1030 new (this) AMDGPULibFunc(F);
1034 AMDGPULibFunc::AMDGPULibFunc(EFuncId Id, const AMDGPULibFunc &CopyFrom) {
1035 assert(AMDGPULibFuncBase::isMangled(Id) && CopyFrom.isMangled() &&
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() {
1050 return cast<AMDGPUMangledLibFunc>(Impl.get())->Leads;
1053 const AMDGPULibFunc::Param *AMDGPULibFunc::getLeads() const {
1054 return cast<const AMDGPUMangledLibFunc>(Impl.get())->Leads;