1 //===-- AMDGPULibFunc.cpp -------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains utility functions to work with Itanium mangled names
12 //===----------------------------------------------------------------------===//
15 #include "AMDGPULibFunc.h"
16 #include <llvm/ADT/SmallString.h>
17 #include <llvm/ADT/SmallVector.h>
18 #include <llvm/ADT/StringSwitch.h>
19 #include "llvm/IR/Attributes.h"
20 #include "llvm/IR/DerivedTypes.h"
21 #include "llvm/IR/Function.h"
22 #include "llvm/IR/Module.h"
23 #include "llvm/IR/ValueSymbolTable.h"
24 #include <llvm/Support/raw_ostream.h>
60 unsigned char Lead[2];
61 unsigned char Param[5];
63 int maxLeadIndex() const { return (std::max)(Lead[0], Lead[1]); }
64 int getNumLeads() const { return (Lead[0] ? 1 : 0) + (Lead[1] ? 1 : 0); }
66 unsigned getNumArgs() const;
69 // Information about library functions with unmangled names.
70 class UnmangledFuncInfo {
74 // Table for all lib functions with unmangled names.
75 static const UnmangledFuncInfo Table[];
77 // Number of entries in Table.
78 static const unsigned TableSize;
80 // Map function name to index.
81 class NameMap : public StringMap<unsigned> {
84 for (unsigned I = 0; I != TableSize; ++I)
85 (*this)[Table[I].Name] = I;
92 using ID = AMDGPULibFunc::EFuncId;
93 UnmangledFuncInfo() = default;
94 UnmangledFuncInfo(StringRef _Name, unsigned _NumArgs)
95 : Name(_Name), NumArgs(_NumArgs) {}
96 // Get index to Table by function name.
97 static bool lookup(StringRef Name, ID &Id);
98 static unsigned toIndex(ID Id) {
99 assert(static_cast<unsigned>(Id) >
100 static_cast<unsigned>(AMDGPULibFunc::EI_LAST_MANGLED) &&
101 "Invalid unmangled library function");
102 return static_cast<unsigned>(Id) - 1 -
103 static_cast<unsigned>(AMDGPULibFunc::EI_LAST_MANGLED);
105 static ID toFuncId(unsigned Index) {
106 assert(Index < TableSize && "Invalid unmangled library function");
107 return static_cast<ID>(
108 Index + 1 + static_cast<unsigned>(AMDGPULibFunc::EI_LAST_MANGLED));
110 static unsigned getNumArgs(ID Id) { return Table[toIndex(Id)].NumArgs; }
111 static StringRef getName(ID Id) { return Table[toIndex(Id)].Name; }
114 unsigned ManglingRule::getNumArgs() const {
116 while (I < (sizeof Param/sizeof Param[0]) && Param[I]) ++I;
120 // This table describes function formal argument type rules. The order of rules
121 // corresponds to the EFuncId enum at AMDGPULibFunc.h
123 // "<func name>", { <leads> }, { <param rules> }
125 // <leads> - list of integers that are one-based indexes of formal argument
126 // used to mangle a function name. Other argument types are derived from types
127 // of these 'leads'. The order of integers in this list correspond to the
128 // order in which these arguments are mangled in the EDG mangling scheme. The
129 // same order should be preserved for arguments in the AMDGPULibFunc structure
130 // when it is used for mangling. For example:
131 // { "vstorea_half", {3,1}, {E_ANY,EX_SIZET,E_ANY}},
132 // will be mangled in EDG scheme as vstorea_half_<3dparam>_<1stparam>
133 // When mangling from code use:
134 // AMDGPULibFunc insc;
135 // insc.param[0] = ... // describe 3rd parameter
136 // insc.param[1] = ... // describe 1rd parameter
138 // <param rules> - list of rules used to derive all of the function formal
139 // argument types. EX_ prefixed are simple types, other derived from the
140 // latest 'lead' argument type in the order of encoding from first to last.
141 // E_ANY - use prev lead type, E_CONSTPTR_ANY - make const pointer out of
142 // prev lead type, etc. see ParamIterator::getNextParam() for details.
144 static const ManglingRule manglingRules[] = {
145 { StringRef(), {0}, {0} },
146 { "abs" , {1}, {E_ANY}},
147 { "abs_diff" , {1}, {E_ANY,E_COPY}},
148 { "acos" , {1}, {E_ANY}},
149 { "acosh" , {1}, {E_ANY}},
150 { "acospi" , {1}, {E_ANY}},
151 { "add_sat" , {1}, {E_ANY,E_COPY}},
152 { "all" , {1}, {E_ANY}},
153 { "any" , {1}, {E_ANY}},
154 { "asin" , {1}, {E_ANY}},
155 { "asinh" , {1}, {E_ANY}},
156 { "asinpi" , {1}, {E_ANY}},
157 { "async_work_group_copy" , {1}, {E_ANY,E_CONSTPTR_SWAPGL,EX_SIZET,EX_EVENT}},
158 { "async_work_group_strided_copy" , {1}, {E_ANY,E_CONSTPTR_SWAPGL,EX_SIZET,EX_SIZET,EX_EVENT}},
159 { "atan" , {1}, {E_ANY}},
160 { "atan2" , {1}, {E_ANY,E_COPY}},
161 { "atan2pi" , {1}, {E_ANY,E_COPY}},
162 { "atanh" , {1}, {E_ANY}},
163 { "atanpi" , {1}, {E_ANY}},
164 { "atomic_add" , {1}, {E_VLTLPTR_ANY,E_POINTEE}},
165 { "atomic_and" , {1}, {E_VLTLPTR_ANY,E_POINTEE}},
166 { "atomic_cmpxchg" , {1}, {E_VLTLPTR_ANY,E_POINTEE,E_POINTEE}},
167 { "atomic_dec" , {1}, {E_VLTLPTR_ANY}},
168 { "atomic_inc" , {1}, {E_VLTLPTR_ANY}},
169 { "atomic_max" , {1}, {E_VLTLPTR_ANY,E_POINTEE}},
170 { "atomic_min" , {1}, {E_VLTLPTR_ANY,E_POINTEE}},
171 { "atomic_or" , {1}, {E_VLTLPTR_ANY,E_POINTEE}},
172 { "atomic_sub" , {1}, {E_VLTLPTR_ANY,E_POINTEE}},
173 { "atomic_xchg" , {1}, {E_VLTLPTR_ANY,E_POINTEE}},
174 { "atomic_xor" , {1}, {E_VLTLPTR_ANY,E_POINTEE}},
175 { "bitselect" , {1}, {E_ANY,E_COPY,E_COPY}},
176 { "cbrt" , {1}, {E_ANY}},
177 { "ceil" , {1}, {E_ANY}},
178 { "clamp" , {1}, {E_ANY,E_COPY,E_COPY}},
179 { "clz" , {1}, {E_ANY}},
180 { "commit_read_pipe" , {1}, {E_ANY,EX_RESERVEDID}},
181 { "commit_write_pipe" , {1}, {E_ANY,EX_RESERVEDID}},
182 { "copysign" , {1}, {E_ANY,E_COPY}},
183 { "cos" , {1}, {E_ANY}},
184 { "cosh" , {1}, {E_ANY}},
185 { "cospi" , {1}, {E_ANY}},
186 { "cross" , {1}, {E_ANY,E_COPY}},
187 { "ctz" , {1}, {E_ANY}},
188 { "degrees" , {1}, {E_ANY}},
189 { "distance" , {1}, {E_ANY,E_COPY}},
190 { "divide" , {1}, {E_ANY,E_COPY}},
191 { "dot" , {1}, {E_ANY,E_COPY}},
192 { "erf" , {1}, {E_ANY}},
193 { "erfc" , {1}, {E_ANY}},
194 { "exp" , {1}, {E_ANY}},
195 { "exp10" , {1}, {E_ANY}},
196 { "exp2" , {1}, {E_ANY}},
197 { "expm1" , {1}, {E_ANY}},
198 { "fabs" , {1}, {E_ANY}},
199 { "fast_distance" , {1}, {E_ANY,E_COPY}},
200 { "fast_length" , {1}, {E_ANY}},
201 { "fast_normalize" , {1}, {E_ANY}},
202 { "fdim" , {1}, {E_ANY,E_COPY}},
203 { "floor" , {1}, {E_ANY}},
204 { "fma" , {1}, {E_ANY,E_COPY,E_COPY}},
205 { "fmax" , {1}, {E_ANY,E_COPY}},
206 { "fmin" , {1}, {E_ANY,E_COPY}},
207 { "fmod" , {1}, {E_ANY,E_COPY}},
208 { "fract" , {2}, {E_POINTEE,E_ANY}},
209 { "frexp" , {1,2}, {E_ANY,E_ANY}},
210 { "get_image_array_size" , {1}, {E_ANY}},
211 { "get_image_channel_data_type" , {1}, {E_ANY}},
212 { "get_image_channel_order" , {1}, {E_ANY}},
213 { "get_image_dim" , {1}, {E_ANY}},
214 { "get_image_height" , {1}, {E_ANY}},
215 { "get_image_width" , {1}, {E_ANY}},
216 { "get_pipe_max_packets" , {1}, {E_ANY}},
217 { "get_pipe_num_packets" , {1}, {E_ANY}},
218 { "hadd" , {1}, {E_ANY,E_COPY}},
219 { "hypot" , {1}, {E_ANY,E_COPY}},
220 { "ilogb" , {1}, {E_ANY}},
221 { "isequal" , {1}, {E_ANY,E_COPY}},
222 { "isfinite" , {1}, {E_ANY}},
223 { "isgreater" , {1}, {E_ANY,E_COPY}},
224 { "isgreaterequal" , {1}, {E_ANY,E_COPY}},
225 { "isinf" , {1}, {E_ANY}},
226 { "isless" , {1}, {E_ANY,E_COPY}},
227 { "islessequal" , {1}, {E_ANY,E_COPY}},
228 { "islessgreater" , {1}, {E_ANY,E_COPY}},
229 { "isnan" , {1}, {E_ANY}},
230 { "isnormal" , {1}, {E_ANY}},
231 { "isnotequal" , {1}, {E_ANY,E_COPY}},
232 { "isordered" , {1}, {E_ANY,E_COPY}},
233 { "isunordered" , {1}, {E_ANY,E_COPY}},
234 { "ldexp" , {1}, {E_ANY,E_SETBASE_I32}},
235 { "length" , {1}, {E_ANY}},
236 { "lgamma" , {1}, {E_ANY}},
237 { "lgamma_r" , {1,2}, {E_ANY,E_ANY}},
238 { "log" , {1}, {E_ANY}},
239 { "log10" , {1}, {E_ANY}},
240 { "log1p" , {1}, {E_ANY}},
241 { "log2" , {1}, {E_ANY}},
242 { "logb" , {1}, {E_ANY}},
243 { "mad" , {1}, {E_ANY,E_COPY,E_COPY}},
244 { "mad24" , {1}, {E_ANY,E_COPY,E_COPY}},
245 { "mad_hi" , {1}, {E_ANY,E_COPY,E_COPY}},
246 { "mad_sat" , {1}, {E_ANY,E_COPY,E_COPY}},
247 { "max" , {1}, {E_ANY,E_COPY}},
248 { "maxmag" , {1}, {E_ANY,E_COPY}},
249 { "min" , {1}, {E_ANY,E_COPY}},
250 { "minmag" , {1}, {E_ANY,E_COPY}},
251 { "mix" , {1}, {E_ANY,E_COPY,E_COPY}},
252 { "modf" , {2}, {E_POINTEE,E_ANY}},
253 { "mul24" , {1}, {E_ANY,E_COPY}},
254 { "mul_hi" , {1}, {E_ANY,E_COPY}},
255 { "nan" , {1}, {E_ANY}},
256 { "nextafter" , {1}, {E_ANY,E_COPY}},
257 { "normalize" , {1}, {E_ANY}},
258 { "popcount" , {1}, {E_ANY}},
259 { "pow" , {1}, {E_ANY,E_COPY}},
260 { "pown" , {1}, {E_ANY,E_SETBASE_I32}},
261 { "powr" , {1}, {E_ANY,E_COPY}},
262 { "prefetch" , {1}, {E_CONSTPTR_ANY,EX_SIZET}},
263 { "radians" , {1}, {E_ANY}},
264 { "recip" , {1}, {E_ANY}},
265 { "remainder" , {1}, {E_ANY,E_COPY}},
266 { "remquo" , {1,3}, {E_ANY,E_COPY,E_ANY}},
267 { "reserve_read_pipe" , {1}, {E_ANY,EX_UINT}},
268 { "reserve_write_pipe" , {1}, {E_ANY,EX_UINT}},
269 { "rhadd" , {1}, {E_ANY,E_COPY}},
270 { "rint" , {1}, {E_ANY}},
271 { "rootn" , {1}, {E_ANY,E_SETBASE_I32}},
272 { "rotate" , {1}, {E_ANY,E_COPY}},
273 { "round" , {1}, {E_ANY}},
274 { "rsqrt" , {1}, {E_ANY}},
275 { "select" , {1,3}, {E_ANY,E_COPY,E_ANY}},
276 { "shuffle" , {1,2}, {E_ANY,E_ANY}},
277 { "shuffle2" , {1,3}, {E_ANY,E_COPY,E_ANY}},
278 { "sign" , {1}, {E_ANY}},
279 { "signbit" , {1}, {E_ANY}},
280 { "sin" , {1}, {E_ANY}},
281 { "sincos" , {2}, {E_POINTEE,E_ANY}},
282 { "sinh" , {1}, {E_ANY}},
283 { "sinpi" , {1}, {E_ANY}},
284 { "smoothstep" , {1}, {E_ANY,E_COPY,E_COPY}},
285 { "sqrt" , {1}, {E_ANY}},
286 { "step" , {1}, {E_ANY,E_COPY}},
287 { "sub_group_broadcast" , {1}, {E_ANY,EX_UINT}},
288 { "sub_group_commit_read_pipe" , {1}, {E_ANY,EX_RESERVEDID}},
289 { "sub_group_commit_write_pipe" , {1}, {E_ANY,EX_RESERVEDID}},
290 { "sub_group_reduce_add" , {1}, {E_ANY}},
291 { "sub_group_reduce_max" , {1}, {E_ANY}},
292 { "sub_group_reduce_min" , {1}, {E_ANY}},
293 { "sub_group_reserve_read_pipe" , {1}, {E_ANY,EX_UINT}},
294 { "sub_group_reserve_write_pipe" , {1}, {E_ANY,EX_UINT}},
295 { "sub_group_scan_exclusive_add" , {1}, {E_ANY}},
296 { "sub_group_scan_exclusive_max" , {1}, {E_ANY}},
297 { "sub_group_scan_exclusive_min" , {1}, {E_ANY}},
298 { "sub_group_scan_inclusive_add" , {1}, {E_ANY}},
299 { "sub_group_scan_inclusive_max" , {1}, {E_ANY}},
300 { "sub_group_scan_inclusive_min" , {1}, {E_ANY}},
301 { "sub_sat" , {1}, {E_ANY,E_COPY}},
302 { "tan" , {1}, {E_ANY}},
303 { "tanh" , {1}, {E_ANY}},
304 { "tanpi" , {1}, {E_ANY}},
305 { "tgamma" , {1}, {E_ANY}},
306 { "trunc" , {1}, {E_ANY}},
307 { "upsample" , {1}, {E_ANY,E_MAKEBASE_UNS}},
308 { "vec_step" , {1}, {E_ANY}},
309 { "vstore" , {3}, {E_POINTEE,EX_SIZET,E_ANY}},
310 { "vstore16" , {3}, {E_V16_OF_POINTEE,EX_SIZET,E_ANY}},
311 { "vstore2" , {3}, {E_V2_OF_POINTEE,EX_SIZET,E_ANY}},
312 { "vstore3" , {3}, {E_V3_OF_POINTEE,EX_SIZET,E_ANY}},
313 { "vstore4" , {3}, {E_V4_OF_POINTEE,EX_SIZET,E_ANY}},
314 { "vstore8" , {3}, {E_V8_OF_POINTEE,EX_SIZET,E_ANY}},
315 { "work_group_commit_read_pipe" , {1}, {E_ANY,EX_RESERVEDID}},
316 { "work_group_commit_write_pipe" , {1}, {E_ANY,EX_RESERVEDID}},
317 { "work_group_reduce_add" , {1}, {E_ANY}},
318 { "work_group_reduce_max" , {1}, {E_ANY}},
319 { "work_group_reduce_min" , {1}, {E_ANY}},
320 { "work_group_reserve_read_pipe" , {1}, {E_ANY,EX_UINT}},
321 { "work_group_reserve_write_pipe" , {1}, {E_ANY,EX_UINT}},
322 { "work_group_scan_exclusive_add" , {1}, {E_ANY}},
323 { "work_group_scan_exclusive_max" , {1}, {E_ANY}},
324 { "work_group_scan_exclusive_min" , {1}, {E_ANY}},
325 { "work_group_scan_inclusive_add" , {1}, {E_ANY}},
326 { "work_group_scan_inclusive_max" , {1}, {E_ANY}},
327 { "work_group_scan_inclusive_min" , {1}, {E_ANY}},
328 { "write_imagef" , {1}, {E_ANY,E_IMAGECOORDS,EX_FLOAT4}},
329 { "write_imagei" , {1}, {E_ANY,E_IMAGECOORDS,EX_INTV4}},
330 { "write_imageui" , {1}, {E_ANY,E_IMAGECOORDS,EX_UINTV4}},
331 { "ncos" , {1}, {E_ANY} },
332 { "nexp2" , {1}, {E_ANY} },
333 { "nfma" , {1}, {E_ANY, E_COPY, E_COPY} },
334 { "nlog2" , {1}, {E_ANY} },
335 { "nrcp" , {1}, {E_ANY} },
336 { "nrsqrt" , {1}, {E_ANY} },
337 { "nsin" , {1}, {E_ANY} },
338 { "nsqrt" , {1}, {E_ANY} },
339 { "ftz" , {1}, {E_ANY} },
340 { "fldexp" , {1}, {E_ANY, EX_UINT} },
341 { "class" , {1}, {E_ANY, EX_UINT} },
342 { "rcbrt" , {1}, {E_ANY} },
345 // Library functions with unmangled name.
346 const UnmangledFuncInfo UnmangledFuncInfo::Table[] = {
347 {"__read_pipe_2", 4},
348 {"__read_pipe_4", 6},
349 {"__write_pipe_2", 4},
350 {"__write_pipe_4", 6},
353 const unsigned UnmangledFuncInfo::TableSize =
354 sizeof(UnmangledFuncInfo::Table) / sizeof(UnmangledFuncInfo::Table[0]);
356 UnmangledFuncInfo::NameMap UnmangledFuncInfo::Map;
358 static const struct ManglingRulesMap : public StringMap<int> {
360 : StringMap<int>(sizeof(manglingRules)/sizeof(manglingRules[0])) {
362 for (auto Rule : manglingRules)
363 insert({ Rule.Name, Id++ });
367 static AMDGPULibFunc::Param getRetType(AMDGPULibFunc::EFuncId id,
368 const AMDGPULibFunc::Param (&Leads)[2]) {
369 AMDGPULibFunc::Param Res = Leads[0];
370 // TBD - This switch may require to be extended for other intriniscs
372 case AMDGPULibFunc::EI_SINCOS:
373 Res.PtrKind = AMDGPULibFunc::BYVALUE;
381 class ParamIterator {
382 const AMDGPULibFunc::Param (&Leads)[2];
383 const ManglingRule& Rule;
386 ParamIterator(const AMDGPULibFunc::Param (&leads)[2],
387 const ManglingRule& rule)
388 : Leads(leads), Rule(rule), Index(0) {}
390 AMDGPULibFunc::Param getNextParam();
393 AMDGPULibFunc::Param ParamIterator::getNextParam() {
394 AMDGPULibFunc::Param P;
395 if (Index >= int(sizeof Rule.Param/sizeof Rule.Param[0])) return P;
397 const char R = Rule.Param[Index];
401 P.ArgType = AMDGPULibFunc::U32; break;
403 P.ArgType = AMDGPULibFunc::I32; P.VectorSize = 4; break;
405 P.ArgType = AMDGPULibFunc::U32; P.VectorSize = 4; break;
407 P.ArgType = AMDGPULibFunc::F32; P.VectorSize = 4; break;
409 P.ArgType = AMDGPULibFunc::U64; break;
411 P.ArgType = AMDGPULibFunc::EVENT; break;
413 P.ArgType = AMDGPULibFunc::SAMPLER; break;
414 case EX_RESERVEDID: break; // TBD
416 if (Index == (Rule.Lead[1] - 1)) P = Leads[1];
424 P.PtrKind = AMDGPULibFunc::BYVALUE; break;
425 case E_V2_OF_POINTEE:
426 P.VectorSize = 2; P.PtrKind = AMDGPULibFunc::BYVALUE; break;
427 case E_V3_OF_POINTEE:
428 P.VectorSize = 3; P.PtrKind = AMDGPULibFunc::BYVALUE; break;
429 case E_V4_OF_POINTEE:
430 P.VectorSize = 4; P.PtrKind = AMDGPULibFunc::BYVALUE; break;
431 case E_V8_OF_POINTEE:
432 P.VectorSize = 8; P.PtrKind = AMDGPULibFunc::BYVALUE; break;
433 case E_V16_OF_POINTEE:
434 P.VectorSize = 16; P.PtrKind = AMDGPULibFunc::BYVALUE; break;
436 P.PtrKind |= AMDGPULibFunc::CONST; break;
438 P.PtrKind |= AMDGPULibFunc::VOLATILE; break;
440 P.ArgType = AMDGPULibFunc::I32; break;
442 P.ArgType = AMDGPULibFunc::U32; break;
445 P.ArgType &= ~AMDGPULibFunc::BASE_TYPE_MASK;
446 P.ArgType |= AMDGPULibFunc::UINT;
451 case AMDGPULibFunc::IMG1DA: P.VectorSize = 2; break;
452 case AMDGPULibFunc::IMG1DB: P.VectorSize = 1; break;
453 case AMDGPULibFunc::IMG2DA: P.VectorSize = 4; break;
454 case AMDGPULibFunc::IMG1D: P.VectorSize = 1; break;
455 case AMDGPULibFunc::IMG2D: P.VectorSize = 2; break;
456 case AMDGPULibFunc::IMG3D: P.VectorSize = 4; break;
458 P.PtrKind = AMDGPULibFunc::BYVALUE;
459 P.ArgType = AMDGPULibFunc::I32;
462 case E_CONSTPTR_SWAPGL: {
463 unsigned AS = AMDGPULibFunc::getAddrSpaceFromEPtrKind(P.PtrKind);
465 case AMDGPUAS::GLOBAL_ADDRESS: AS = AMDGPUAS::LOCAL_ADDRESS; break;
466 case AMDGPUAS::LOCAL_ADDRESS: AS = AMDGPUAS::GLOBAL_ADDRESS; break;
468 P.PtrKind = AMDGPULibFunc::getEPtrKindFromAddrSpace(AS);
469 P.PtrKind |= AMDGPULibFunc::CONST;
473 default: llvm_unreachable("Unhandeled param rule");
480 inline static void drop_front(StringRef& str, size_t n = 1) {
481 str = str.drop_front(n);
484 static bool eatTerm(StringRef& mangledName, const char c) {
485 if (mangledName.front() == c) {
486 drop_front(mangledName);
493 static bool eatTerm(StringRef& mangledName, const char (&str)[N]) {
494 if (mangledName.startswith(StringRef(str, N-1))) {
495 drop_front(mangledName, N-1);
501 static inline bool isDigit(char c) { return c >= '0' && c <= '9'; }
503 static int eatNumber(StringRef& s) {
504 size_t const savedSize = s.size();
506 while (!s.empty() && isDigit(s.front())) {
507 n = n*10 + s.front() - '0';
510 return s.size() < savedSize ? n : -1;
513 static StringRef eatLengthPrefixedName(StringRef& mangledName) {
514 int const Len = eatNumber(mangledName);
515 if (Len <= 0 || static_cast<size_t>(Len) > mangledName.size())
517 StringRef Res = mangledName.substr(0, Len);
518 drop_front(mangledName, Len);
522 } // end anonymous namespace
524 AMDGPUMangledLibFunc::AMDGPUMangledLibFunc() {
532 AMDGPUUnmangledLibFunc::AMDGPUUnmangledLibFunc() {
537 AMDGPUMangledLibFunc::AMDGPUMangledLibFunc(
538 EFuncId id, const AMDGPUMangledLibFunc ©From) {
540 FKind = copyFrom.FKind;
541 Leads[0] = copyFrom.Leads[0];
542 Leads[1] = copyFrom.Leads[1];
545 ///////////////////////////////////////////////////////////////////////////////
548 static int parseVecSize(StringRef& mangledName) {
549 size_t const Len = eatNumber(mangledName);
551 case 2: case 3: case 4: case 8: case 16:
559 static AMDGPULibFunc::ENamePrefix parseNamePrefix(StringRef& mangledName) {
560 std::pair<StringRef, StringRef> const P = mangledName.split('_');
561 AMDGPULibFunc::ENamePrefix Pfx =
562 StringSwitch<AMDGPULibFunc::ENamePrefix>(P.first)
563 .Case("native", AMDGPULibFunc::NATIVE)
564 .Case("half" , AMDGPULibFunc::HALF)
565 .Default(AMDGPULibFunc::NOPFX);
567 if (Pfx != AMDGPULibFunc::NOPFX)
568 mangledName = P.second;
573 bool AMDGPUMangledLibFunc::parseUnmangledName(StringRef FullName) {
574 FuncId = static_cast<EFuncId>(manglingRulesMap.lookup(FullName));
575 return FuncId != EI_NONE;
578 ///////////////////////////////////////////////////////////////////////////////
579 // Itanium Demangling
582 struct ItaniumParamParser {
583 AMDGPULibFunc::Param Prev;
584 bool parseItaniumParam(StringRef& param, AMDGPULibFunc::Param &res);
588 bool ItaniumParamParser::parseItaniumParam(StringRef& param,
589 AMDGPULibFunc::Param &res) {
591 if (param.empty()) return false;
593 // parse pointer prefix
594 if (eatTerm(param, 'P')) {
595 if (eatTerm(param, 'K')) res.PtrKind |= AMDGPULibFunc::CONST;
596 if (eatTerm(param, 'V')) res.PtrKind |= AMDGPULibFunc::VOLATILE;
598 if (!eatTerm(param, "U3AS")) {
601 AS = param.front() - '0';
602 drop_front(param, 1);
604 res.PtrKind |= AMDGPULibFuncBase::getEPtrKindFromAddrSpace(AS);
606 res.PtrKind = AMDGPULibFunc::BYVALUE;
610 if (eatTerm(param,"Dv")) {
611 res.VectorSize = parseVecSize(param);
612 if (res.VectorSize==1 || !eatTerm(param, '_')) return false;
616 char const TC = param.front();
618 res.ArgType = StringSwitch<AMDGPULibFunc::EType>
619 (eatLengthPrefixedName(param))
620 .Case("ocl_image1darray" , AMDGPULibFunc::IMG1DA)
621 .Case("ocl_image1dbuffer", AMDGPULibFunc::IMG1DB)
622 .Case("ocl_image2darray" , AMDGPULibFunc::IMG2DA)
623 .Case("ocl_image1d" , AMDGPULibFunc::IMG1D)
624 .Case("ocl_image2d" , AMDGPULibFunc::IMG2D)
625 .Case("ocl_image3d" , AMDGPULibFunc::IMG3D)
626 .Case("ocl_event" , AMDGPULibFunc::DUMMY)
627 .Case("ocl_sampler" , AMDGPULibFunc::DUMMY)
628 .Default(AMDGPULibFunc::DUMMY);
632 case 'h': res.ArgType = AMDGPULibFunc::U8; break;
633 case 't': res.ArgType = AMDGPULibFunc::U16; break;
634 case 'j': res.ArgType = AMDGPULibFunc::U32; break;
635 case 'm': res.ArgType = AMDGPULibFunc::U64; break;
636 case 'c': res.ArgType = AMDGPULibFunc::I8; break;
637 case 's': res.ArgType = AMDGPULibFunc::I16; break;
638 case 'i': res.ArgType = AMDGPULibFunc::I32; break;
639 case 'l': res.ArgType = AMDGPULibFunc::I64; break;
640 case 'f': res.ArgType = AMDGPULibFunc::F32; break;
641 case 'd': res.ArgType = AMDGPULibFunc::F64; break;
642 case 'D': if (!eatTerm(param, 'h')) return false;
643 res.ArgType = AMDGPULibFunc::F16; break;
645 if (!eatTerm(param, '_')) {
647 if (!eatTerm(param, '_')) return false;
649 res.VectorSize = Prev.VectorSize;
650 res.ArgType = Prev.ArgType;
655 if (res.ArgType == 0) return false;
656 Prev.VectorSize = res.VectorSize;
657 Prev.ArgType = res.ArgType;
661 bool AMDGPUMangledLibFunc::parseFuncName(StringRef &mangledName) {
662 StringRef Name = eatLengthPrefixedName(mangledName);
663 FKind = parseNamePrefix(Name);
664 if (!parseUnmangledName(Name))
667 const ManglingRule& Rule = manglingRules[FuncId];
668 ItaniumParamParser Parser;
669 for (int I=0; I < Rule.maxLeadIndex(); ++I) {
671 if (!Parser.parseItaniumParam(mangledName, P))
674 if ((I + 1) == Rule.Lead[0]) Leads[0] = P;
675 if ((I + 1) == Rule.Lead[1]) Leads[1] = P;
680 bool AMDGPUUnmangledLibFunc::parseFuncName(StringRef &Name) {
681 if (!UnmangledFuncInfo::lookup(Name, FuncId))
687 bool AMDGPULibFunc::parse(StringRef FuncName, AMDGPULibFunc &F) {
688 if (FuncName.empty()) {
689 F.Impl = std::unique_ptr<AMDGPULibFuncImpl>();
693 if (eatTerm(FuncName, "_Z"))
694 F.Impl = make_unique<AMDGPUMangledLibFunc>();
696 F.Impl = make_unique<AMDGPUUnmangledLibFunc>();
697 if (F.Impl->parseFuncName(FuncName))
700 F.Impl = std::unique_ptr<AMDGPULibFuncImpl>();
704 StringRef AMDGPUMangledLibFunc::getUnmangledName(StringRef mangledName) {
705 StringRef S = mangledName;
706 if (eatTerm(S, "_Z"))
707 return eatLengthPrefixedName(S);
711 ///////////////////////////////////////////////////////////////////////////////
714 template <typename Stream>
715 void AMDGPUMangledLibFunc::writeName(Stream &OS) const {
716 const char *Pfx = "";
718 case NATIVE: Pfx = "native_"; break;
719 case HALF: Pfx = "half_"; break;
724 } else if (FuncId != EI_NONE) {
726 const StringRef& S = manglingRules[FuncId].Name;
727 OS.write(S.data(), S.size());
731 std::string AMDGPUMangledLibFunc::mangle() const { return mangleNameItanium(); }
733 ///////////////////////////////////////////////////////////////////////////////
736 static const char *getItaniumTypeName(AMDGPULibFunc::EType T) {
738 case AMDGPULibFunc::U8: return "h";
739 case AMDGPULibFunc::U16: return "t";
740 case AMDGPULibFunc::U32: return "j";
741 case AMDGPULibFunc::U64: return "m";
742 case AMDGPULibFunc::I8: return "c";
743 case AMDGPULibFunc::I16: return "s";
744 case AMDGPULibFunc::I32: return "i";
745 case AMDGPULibFunc::I64: return "l";
746 case AMDGPULibFunc::F16: return "Dh";
747 case AMDGPULibFunc::F32: return "f";
748 case AMDGPULibFunc::F64: return "d";
749 case AMDGPULibFunc::IMG1DA: return "16ocl_image1darray";
750 case AMDGPULibFunc::IMG1DB: return "17ocl_image1dbuffer";
751 case AMDGPULibFunc::IMG2DA: return "16ocl_image2darray";
752 case AMDGPULibFunc::IMG1D: return "11ocl_image1d";
753 case AMDGPULibFunc::IMG2D: return "11ocl_image2d";
754 case AMDGPULibFunc::IMG3D: return "11ocl_image3d";
755 case AMDGPULibFunc::SAMPLER: return "11ocl_sampler";
756 case AMDGPULibFunc::EVENT: return "9ocl_event";
757 default: llvm_unreachable("Unhandeled param type");
763 // Itanium mangling ABI says:
764 // "5.1.8. Compression
765 // ... Each non-terminal in the grammar for which <substitution> appears on the
766 // right-hand side is both a source of future substitutions and a candidate
767 // for being substituted. There are two exceptions that appear to be
768 // substitution candidates from the grammar, but are explicitly excluded:
769 // 1. <builtin-type> other than vendor extended types ..."
771 // For the purpose of functions the following productions make sence for the
773 // <type> ::= <builtin-type>
774 // ::= <class-enum-type>
776 // ::=<CV-qualifiers> <type>
777 // ::= P <type> # pointer-to
778 // ::= <substitution>
780 // Note that while types like images, samplers and events are by the ABI encoded
781 // using <class-enum-type> production rule they're not used for substitution
782 // because clang consider them as builtin types.
784 // DvNN_ type is GCC extension for vectors and is a subject for the substitution.
787 class ItaniumMangler {
788 SmallVector<AMDGPULibFunc::Param, 10> Str; // list of accumulated substituions
791 int findSubst(const AMDGPULibFunc::Param& P) const {
792 for(unsigned I = 0; I < Str.size(); ++I) {
793 const AMDGPULibFunc::Param& T = Str[I];
794 if (P.PtrKind == T.PtrKind &&
795 P.VectorSize == T.VectorSize &&
796 P.ArgType == T.ArgType) {
803 template <typename Stream>
804 bool trySubst(Stream& os, const AMDGPULibFunc::Param& p) {
805 int const subst = findSubst(p);
806 if (subst < 0) return false;
807 // Substitutions are mangled as S(XX)?_ where XX is a hexadecimal number
810 if (subst == 0) os << "S_";
811 else os << 'S' << (subst-1) << '_';
816 ItaniumMangler(bool useAddrSpace)
817 : UseAddrSpace(useAddrSpace) {}
819 template <typename Stream>
820 void operator()(Stream& os, AMDGPULibFunc::Param p) {
822 // Itanium mangling ABI 5.1.8. Compression:
823 // Logically, the substitutable components of a mangled name are considered
824 // left-to-right, components before the composite structure of which they
825 // are a part. If a component has been encountered before, it is substituted
826 // as described below. This decision is independent of whether its components
827 // have been substituted, so an implementation may optimize by considering
828 // large structures for substitution before their components. If a component
829 // has not been encountered before, its mangling is identified, and it is
830 // added to a dictionary of substitution candidates. No entity is added to
831 // the dictionary twice.
832 AMDGPULibFunc::Param Ptr;
835 if (trySubst(os, p)) return;
837 if (p.PtrKind & AMDGPULibFunc::CONST) os << 'K';
838 if (p.PtrKind & AMDGPULibFunc::VOLATILE) os << 'V';
839 unsigned AS = UseAddrSpace
840 ? AMDGPULibFuncBase::getAddrSpaceFromEPtrKind(p.PtrKind)
842 if (AS != 0) os << "U3AS" << AS;
847 if (p.VectorSize > 1) {
848 if (trySubst(os, p)) goto exit;
850 os << "Dv" << static_cast<unsigned>(p.VectorSize) << '_';
853 os << getItaniumTypeName((AMDGPULibFunc::EType)p.ArgType);
856 if (Ptr.ArgType) Str.push_back(Ptr);
861 std::string AMDGPUMangledLibFunc::mangleNameItanium() const {
862 SmallString<128> Buf;
863 raw_svector_ostream S(Buf);
864 SmallString<128> NameBuf;
865 raw_svector_ostream Name(NameBuf);
867 const StringRef& NameStr = Name.str();
868 S << "_Z" << static_cast<int>(NameStr.size()) << NameStr;
870 ItaniumMangler Mangler(true);
871 ParamIterator I(Leads, manglingRules[FuncId]);
873 while ((P = I.getNextParam()).ArgType != 0)
878 ///////////////////////////////////////////////////////////////////////////////
881 static Type* getIntrinsicParamType(
883 const AMDGPULibFunc::Param& P,
887 case AMDGPULibFunc::U8:
888 case AMDGPULibFunc::I8: T = Type::getInt8Ty(C); break;
889 case AMDGPULibFunc::U16:
890 case AMDGPULibFunc::I16: T = Type::getInt16Ty(C); break;
891 case AMDGPULibFunc::U32:
892 case AMDGPULibFunc::I32: T = Type::getInt32Ty(C); break;
893 case AMDGPULibFunc::U64:
894 case AMDGPULibFunc::I64: T = Type::getInt64Ty(C); break;
895 case AMDGPULibFunc::F16: T = Type::getHalfTy(C); break;
896 case AMDGPULibFunc::F32: T = Type::getFloatTy(C); break;
897 case AMDGPULibFunc::F64: T = Type::getDoubleTy(C); break;
899 case AMDGPULibFunc::IMG1DA:
900 case AMDGPULibFunc::IMG1DB:
901 case AMDGPULibFunc::IMG2DA:
902 case AMDGPULibFunc::IMG1D:
903 case AMDGPULibFunc::IMG2D:
904 case AMDGPULibFunc::IMG3D:
905 T = StructType::create(C,"ocl_image")->getPointerTo(); break;
906 case AMDGPULibFunc::SAMPLER:
907 T = StructType::create(C,"ocl_sampler")->getPointerTo(); break;
908 case AMDGPULibFunc::EVENT:
909 T = StructType::create(C,"ocl_event")->getPointerTo(); break;
911 llvm_unreachable("Unhandeled param type");
914 if (P.VectorSize > 1)
915 T = VectorType::get(T, P.VectorSize);
916 if (P.PtrKind != AMDGPULibFunc::BYVALUE)
917 T = useAddrSpace ? T->getPointerTo((P.PtrKind & AMDGPULibFunc::ADDR_SPACE)
923 FunctionType *AMDGPUMangledLibFunc::getFunctionType(Module &M) const {
924 LLVMContext& C = M.getContext();
925 std::vector<Type*> Args;
926 ParamIterator I(Leads, manglingRules[FuncId]);
928 while ((P=I.getNextParam()).ArgType != 0)
929 Args.push_back(getIntrinsicParamType(C, P, true));
931 return FunctionType::get(
932 getIntrinsicParamType(C, getRetType(FuncId, Leads), true),
936 unsigned AMDGPUMangledLibFunc::getNumArgs() const {
937 return manglingRules[FuncId].getNumArgs();
940 unsigned AMDGPUUnmangledLibFunc::getNumArgs() const {
941 return UnmangledFuncInfo::getNumArgs(FuncId);
944 std::string AMDGPUMangledLibFunc::getName() const {
945 SmallString<128> Buf;
946 raw_svector_ostream OS(Buf);
951 Function *AMDGPULibFunc::getFunction(Module *M, const AMDGPULibFunc &fInfo) {
952 std::string FuncName = fInfo.mangle();
953 Function *F = dyn_cast_or_null<Function>(
954 M->getValueSymbolTable().lookup(FuncName));
956 // check formal with actual types conformance
957 if (F && !F->isDeclaration()
959 && F->arg_size() == fInfo.getNumArgs()) {
965 Function *AMDGPULibFunc::getOrInsertFunction(Module *M,
966 const AMDGPULibFunc &fInfo) {
967 std::string const FuncName = fInfo.mangle();
968 Function *F = dyn_cast_or_null<Function>(
969 M->getValueSymbolTable().lookup(FuncName));
971 // check formal with actual types conformance
972 if (F && !F->isDeclaration()
974 && F->arg_size() == fInfo.getNumArgs()) {
978 FunctionType *FuncTy = fInfo.getFunctionType(*M);
981 for (FunctionType::param_iterator
982 PI = FuncTy->param_begin(),
983 PE = FuncTy->param_end();
985 const Type* argTy = static_cast<const Type*>(*PI);
986 if (argTy->isPointerTy()) {
992 Constant *C = nullptr;
994 // Do not set extra attributes for functions with pointer arguments.
995 C = M->getOrInsertFunction(FuncName, FuncTy);
998 LLVMContext &Ctx = M->getContext();
999 Attr.addAttribute(Ctx, AttributeList::FunctionIndex, Attribute::ReadOnly);
1000 Attr.addAttribute(Ctx, AttributeList::FunctionIndex, Attribute::NoUnwind);
1001 C = M->getOrInsertFunction(FuncName, FuncTy, Attr);
1004 return cast<Function>(C);
1007 bool UnmangledFuncInfo::lookup(StringRef Name, ID &Id) {
1008 auto Loc = Map.find(Name);
1009 if (Loc != Map.end()) {
1010 Id = toFuncId(Loc->second);
1013 Id = AMDGPULibFunc::EI_NONE;
1017 AMDGPULibFunc::AMDGPULibFunc(const AMDGPULibFunc &F) {
1018 if (auto *MF = dyn_cast<AMDGPUMangledLibFunc>(F.Impl.get()))
1019 Impl.reset(new AMDGPUMangledLibFunc(*MF));
1020 else if (auto *UMF = dyn_cast<AMDGPUUnmangledLibFunc>(F.Impl.get()))
1021 Impl.reset(new AMDGPUUnmangledLibFunc(*UMF));
1023 Impl = std::unique_ptr<AMDGPULibFuncImpl>();
1026 AMDGPULibFunc &AMDGPULibFunc::operator=(const AMDGPULibFunc &F) {
1029 new (this) AMDGPULibFunc(F);
1033 AMDGPULibFunc::AMDGPULibFunc(EFuncId Id, const AMDGPULibFunc &CopyFrom) {
1034 assert(AMDGPULibFuncBase::isMangled(Id) && CopyFrom.isMangled() &&
1036 Impl.reset(new AMDGPUMangledLibFunc(
1037 Id, *cast<AMDGPUMangledLibFunc>(CopyFrom.Impl.get())));
1040 AMDGPULibFunc::AMDGPULibFunc(StringRef Name, FunctionType *FT) {
1041 Impl.reset(new AMDGPUUnmangledLibFunc(Name, FT));
1044 void AMDGPULibFunc::initMangled() { Impl.reset(new AMDGPUMangledLibFunc()); }
1046 AMDGPULibFunc::Param *AMDGPULibFunc::getLeads() {
1049 return cast<AMDGPUMangledLibFunc>(Impl.get())->Leads;
1052 const AMDGPULibFunc::Param *AMDGPULibFunc::getLeads() const {
1053 return cast<const AMDGPUMangledLibFunc>(Impl.get())->Leads;