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;
68 // Information about library functions with unmangled names.
69 class UnmangledFuncInfo
{
73 // Table for all lib functions with unmangled names.
74 static const UnmangledFuncInfo Table
[];
76 // Number of entries in Table.
77 static const unsigned TableSize
;
79 // Map function name to index.
80 class NameMap
: public StringMap
<unsigned> {
83 for (unsigned I
= 0; I
!= TableSize
; ++I
)
84 (*this)[Table
[I
].Name
] = I
;
91 using ID
= AMDGPULibFunc::EFuncId
;
92 UnmangledFuncInfo(StringRef _Name
, unsigned _NumArgs
)
93 : Name(_Name
), NumArgs(_NumArgs
) {}
94 // Get index to Table by function name.
95 static bool lookup(StringRef Name
, ID
&Id
);
96 static unsigned toIndex(ID Id
) {
97 assert(static_cast<unsigned>(Id
) >
98 static_cast<unsigned>(AMDGPULibFunc::EI_LAST_MANGLED
) &&
99 "Invalid unmangled library function");
100 return static_cast<unsigned>(Id
) - 1 -
101 static_cast<unsigned>(AMDGPULibFunc::EI_LAST_MANGLED
);
103 static ID
toFuncId(unsigned Index
) {
104 assert(Index
< TableSize
&& "Invalid unmangled library function");
105 return static_cast<ID
>(
106 Index
+ 1 + static_cast<unsigned>(AMDGPULibFunc::EI_LAST_MANGLED
));
108 static unsigned getNumArgs(ID Id
) { return Table
[toIndex(Id
)].NumArgs
; }
109 static StringRef
getName(ID Id
) { return Table
[toIndex(Id
)].Name
; }
112 unsigned ManglingRule::getNumArgs() const {
114 while (I
< (sizeof Param
/sizeof Param
[0]) && Param
[I
]) ++I
;
118 // This table describes function formal argument type rules. The order of rules
119 // corresponds to the EFuncId enum at AMDGPULibFunc.h
121 // "<func name>", { <leads> }, { <param rules> }
123 // <leads> - list of integers that are one-based indexes of formal argument
124 // used to mangle a function name. Other argument types are derived from types
125 // of these 'leads'. The order of integers in this list correspond to the
126 // order in which these arguments are mangled in the EDG mangling scheme. The
127 // same order should be preserved for arguments in the AMDGPULibFunc structure
128 // when it is used for mangling. For example:
129 // { "vstorea_half", {3,1}, {E_ANY,EX_SIZET,E_ANY}},
130 // will be mangled in EDG scheme as vstorea_half_<3dparam>_<1stparam>
131 // When mangling from code use:
132 // AMDGPULibFunc insc;
133 // insc.param[0] = ... // describe 3rd parameter
134 // insc.param[1] = ... // describe 1rd parameter
136 // <param rules> - list of rules used to derive all of the function formal
137 // argument types. EX_ prefixed are simple types, other derived from the
138 // latest 'lead' argument type in the order of encoding from first to last.
139 // E_ANY - use prev lead type, E_CONSTPTR_ANY - make const pointer out of
140 // prev lead type, etc. see ParamIterator::getNextParam() for details.
142 static const ManglingRule manglingRules
[] = {
143 { StringRef(), {0}, {0} },
144 { "abs" , {1}, {E_ANY
}},
145 { "abs_diff" , {1}, {E_ANY
,E_COPY
}},
146 { "acos" , {1}, {E_ANY
}},
147 { "acosh" , {1}, {E_ANY
}},
148 { "acospi" , {1}, {E_ANY
}},
149 { "add_sat" , {1}, {E_ANY
,E_COPY
}},
150 { "all" , {1}, {E_ANY
}},
151 { "any" , {1}, {E_ANY
}},
152 { "asin" , {1}, {E_ANY
}},
153 { "asinh" , {1}, {E_ANY
}},
154 { "asinpi" , {1}, {E_ANY
}},
155 { "async_work_group_copy" , {1}, {E_ANY
,E_CONSTPTR_SWAPGL
,EX_SIZET
,EX_EVENT
}},
156 { "async_work_group_strided_copy" , {1}, {E_ANY
,E_CONSTPTR_SWAPGL
,EX_SIZET
,EX_SIZET
,EX_EVENT
}},
157 { "atan" , {1}, {E_ANY
}},
158 { "atan2" , {1}, {E_ANY
,E_COPY
}},
159 { "atan2pi" , {1}, {E_ANY
,E_COPY
}},
160 { "atanh" , {1}, {E_ANY
}},
161 { "atanpi" , {1}, {E_ANY
}},
162 { "atomic_add" , {1}, {E_VLTLPTR_ANY
,E_POINTEE
}},
163 { "atomic_and" , {1}, {E_VLTLPTR_ANY
,E_POINTEE
}},
164 { "atomic_cmpxchg" , {1}, {E_VLTLPTR_ANY
,E_POINTEE
,E_POINTEE
}},
165 { "atomic_dec" , {1}, {E_VLTLPTR_ANY
}},
166 { "atomic_inc" , {1}, {E_VLTLPTR_ANY
}},
167 { "atomic_max" , {1}, {E_VLTLPTR_ANY
,E_POINTEE
}},
168 { "atomic_min" , {1}, {E_VLTLPTR_ANY
,E_POINTEE
}},
169 { "atomic_or" , {1}, {E_VLTLPTR_ANY
,E_POINTEE
}},
170 { "atomic_sub" , {1}, {E_VLTLPTR_ANY
,E_POINTEE
}},
171 { "atomic_xchg" , {1}, {E_VLTLPTR_ANY
,E_POINTEE
}},
172 { "atomic_xor" , {1}, {E_VLTLPTR_ANY
,E_POINTEE
}},
173 { "bitselect" , {1}, {E_ANY
,E_COPY
,E_COPY
}},
174 { "cbrt" , {1}, {E_ANY
}},
175 { "ceil" , {1}, {E_ANY
}},
176 { "clamp" , {1}, {E_ANY
,E_COPY
,E_COPY
}},
177 { "clz" , {1}, {E_ANY
}},
178 { "commit_read_pipe" , {1}, {E_ANY
,EX_RESERVEDID
}},
179 { "commit_write_pipe" , {1}, {E_ANY
,EX_RESERVEDID
}},
180 { "copysign" , {1}, {E_ANY
,E_COPY
}},
181 { "cos" , {1}, {E_ANY
}},
182 { "cosh" , {1}, {E_ANY
}},
183 { "cospi" , {1}, {E_ANY
}},
184 { "cross" , {1}, {E_ANY
,E_COPY
}},
185 { "ctz" , {1}, {E_ANY
}},
186 { "degrees" , {1}, {E_ANY
}},
187 { "distance" , {1}, {E_ANY
,E_COPY
}},
188 { "divide" , {1}, {E_ANY
,E_COPY
}},
189 { "dot" , {1}, {E_ANY
,E_COPY
}},
190 { "erf" , {1}, {E_ANY
}},
191 { "erfc" , {1}, {E_ANY
}},
192 { "exp" , {1}, {E_ANY
}},
193 { "exp10" , {1}, {E_ANY
}},
194 { "exp2" , {1}, {E_ANY
}},
195 { "expm1" , {1}, {E_ANY
}},
196 { "fabs" , {1}, {E_ANY
}},
197 { "fast_distance" , {1}, {E_ANY
,E_COPY
}},
198 { "fast_length" , {1}, {E_ANY
}},
199 { "fast_normalize" , {1}, {E_ANY
}},
200 { "fdim" , {1}, {E_ANY
,E_COPY
}},
201 { "floor" , {1}, {E_ANY
}},
202 { "fma" , {1}, {E_ANY
,E_COPY
,E_COPY
}},
203 { "fmax" , {1}, {E_ANY
,E_COPY
}},
204 { "fmin" , {1}, {E_ANY
,E_COPY
}},
205 { "fmod" , {1}, {E_ANY
,E_COPY
}},
206 { "fract" , {2}, {E_POINTEE
,E_ANY
}},
207 { "frexp" , {1,2}, {E_ANY
,E_ANY
}},
208 { "get_image_array_size" , {1}, {E_ANY
}},
209 { "get_image_channel_data_type" , {1}, {E_ANY
}},
210 { "get_image_channel_order" , {1}, {E_ANY
}},
211 { "get_image_dim" , {1}, {E_ANY
}},
212 { "get_image_height" , {1}, {E_ANY
}},
213 { "get_image_width" , {1}, {E_ANY
}},
214 { "get_pipe_max_packets" , {1}, {E_ANY
}},
215 { "get_pipe_num_packets" , {1}, {E_ANY
}},
216 { "hadd" , {1}, {E_ANY
,E_COPY
}},
217 { "hypot" , {1}, {E_ANY
,E_COPY
}},
218 { "ilogb" , {1}, {E_ANY
}},
219 { "isequal" , {1}, {E_ANY
,E_COPY
}},
220 { "isfinite" , {1}, {E_ANY
}},
221 { "isgreater" , {1}, {E_ANY
,E_COPY
}},
222 { "isgreaterequal" , {1}, {E_ANY
,E_COPY
}},
223 { "isinf" , {1}, {E_ANY
}},
224 { "isless" , {1}, {E_ANY
,E_COPY
}},
225 { "islessequal" , {1}, {E_ANY
,E_COPY
}},
226 { "islessgreater" , {1}, {E_ANY
,E_COPY
}},
227 { "isnan" , {1}, {E_ANY
}},
228 { "isnormal" , {1}, {E_ANY
}},
229 { "isnotequal" , {1}, {E_ANY
,E_COPY
}},
230 { "isordered" , {1}, {E_ANY
,E_COPY
}},
231 { "isunordered" , {1}, {E_ANY
,E_COPY
}},
232 { "ldexp" , {1}, {E_ANY
,E_SETBASE_I32
}},
233 { "length" , {1}, {E_ANY
}},
234 { "lgamma" , {1}, {E_ANY
}},
235 { "lgamma_r" , {1,2}, {E_ANY
,E_ANY
}},
236 { "log" , {1}, {E_ANY
}},
237 { "log10" , {1}, {E_ANY
}},
238 { "log1p" , {1}, {E_ANY
}},
239 { "log2" , {1}, {E_ANY
}},
240 { "logb" , {1}, {E_ANY
}},
241 { "mad" , {1}, {E_ANY
,E_COPY
,E_COPY
}},
242 { "mad24" , {1}, {E_ANY
,E_COPY
,E_COPY
}},
243 { "mad_hi" , {1}, {E_ANY
,E_COPY
,E_COPY
}},
244 { "mad_sat" , {1}, {E_ANY
,E_COPY
,E_COPY
}},
245 { "max" , {1}, {E_ANY
,E_COPY
}},
246 { "maxmag" , {1}, {E_ANY
,E_COPY
}},
247 { "min" , {1}, {E_ANY
,E_COPY
}},
248 { "minmag" , {1}, {E_ANY
,E_COPY
}},
249 { "mix" , {1}, {E_ANY
,E_COPY
,E_COPY
}},
250 { "modf" , {2}, {E_POINTEE
,E_ANY
}},
251 { "mul24" , {1}, {E_ANY
,E_COPY
}},
252 { "mul_hi" , {1}, {E_ANY
,E_COPY
}},
253 { "nan" , {1}, {E_ANY
}},
254 { "nextafter" , {1}, {E_ANY
,E_COPY
}},
255 { "normalize" , {1}, {E_ANY
}},
256 { "popcount" , {1}, {E_ANY
}},
257 { "pow" , {1}, {E_ANY
,E_COPY
}},
258 { "pown" , {1}, {E_ANY
,E_SETBASE_I32
}},
259 { "powr" , {1}, {E_ANY
,E_COPY
}},
260 { "prefetch" , {1}, {E_CONSTPTR_ANY
,EX_SIZET
}},
261 { "radians" , {1}, {E_ANY
}},
262 { "recip" , {1}, {E_ANY
}},
263 { "remainder" , {1}, {E_ANY
,E_COPY
}},
264 { "remquo" , {1,3}, {E_ANY
,E_COPY
,E_ANY
}},
265 { "reserve_read_pipe" , {1}, {E_ANY
,EX_UINT
}},
266 { "reserve_write_pipe" , {1}, {E_ANY
,EX_UINT
}},
267 { "rhadd" , {1}, {E_ANY
,E_COPY
}},
268 { "rint" , {1}, {E_ANY
}},
269 { "rootn" , {1}, {E_ANY
,E_SETBASE_I32
}},
270 { "rotate" , {1}, {E_ANY
,E_COPY
}},
271 { "round" , {1}, {E_ANY
}},
272 { "rsqrt" , {1}, {E_ANY
}},
273 { "select" , {1,3}, {E_ANY
,E_COPY
,E_ANY
}},
274 { "shuffle" , {1,2}, {E_ANY
,E_ANY
}},
275 { "shuffle2" , {1,3}, {E_ANY
,E_COPY
,E_ANY
}},
276 { "sign" , {1}, {E_ANY
}},
277 { "signbit" , {1}, {E_ANY
}},
278 { "sin" , {1}, {E_ANY
}},
279 { "sincos" , {2}, {E_POINTEE
,E_ANY
}},
280 { "sinh" , {1}, {E_ANY
}},
281 { "sinpi" , {1}, {E_ANY
}},
282 { "smoothstep" , {1}, {E_ANY
,E_COPY
,E_COPY
}},
283 { "sqrt" , {1}, {E_ANY
}},
284 { "step" , {1}, {E_ANY
,E_COPY
}},
285 { "sub_group_broadcast" , {1}, {E_ANY
,EX_UINT
}},
286 { "sub_group_commit_read_pipe" , {1}, {E_ANY
,EX_RESERVEDID
}},
287 { "sub_group_commit_write_pipe" , {1}, {E_ANY
,EX_RESERVEDID
}},
288 { "sub_group_reduce_add" , {1}, {E_ANY
}},
289 { "sub_group_reduce_max" , {1}, {E_ANY
}},
290 { "sub_group_reduce_min" , {1}, {E_ANY
}},
291 { "sub_group_reserve_read_pipe" , {1}, {E_ANY
,EX_UINT
}},
292 { "sub_group_reserve_write_pipe" , {1}, {E_ANY
,EX_UINT
}},
293 { "sub_group_scan_exclusive_add" , {1}, {E_ANY
}},
294 { "sub_group_scan_exclusive_max" , {1}, {E_ANY
}},
295 { "sub_group_scan_exclusive_min" , {1}, {E_ANY
}},
296 { "sub_group_scan_inclusive_add" , {1}, {E_ANY
}},
297 { "sub_group_scan_inclusive_max" , {1}, {E_ANY
}},
298 { "sub_group_scan_inclusive_min" , {1}, {E_ANY
}},
299 { "sub_sat" , {1}, {E_ANY
,E_COPY
}},
300 { "tan" , {1}, {E_ANY
}},
301 { "tanh" , {1}, {E_ANY
}},
302 { "tanpi" , {1}, {E_ANY
}},
303 { "tgamma" , {1}, {E_ANY
}},
304 { "trunc" , {1}, {E_ANY
}},
305 { "upsample" , {1}, {E_ANY
,E_MAKEBASE_UNS
}},
306 { "vec_step" , {1}, {E_ANY
}},
307 { "vstore" , {3}, {E_POINTEE
,EX_SIZET
,E_ANY
}},
308 { "vstore16" , {3}, {E_V16_OF_POINTEE
,EX_SIZET
,E_ANY
}},
309 { "vstore2" , {3}, {E_V2_OF_POINTEE
,EX_SIZET
,E_ANY
}},
310 { "vstore3" , {3}, {E_V3_OF_POINTEE
,EX_SIZET
,E_ANY
}},
311 { "vstore4" , {3}, {E_V4_OF_POINTEE
,EX_SIZET
,E_ANY
}},
312 { "vstore8" , {3}, {E_V8_OF_POINTEE
,EX_SIZET
,E_ANY
}},
313 { "work_group_commit_read_pipe" , {1}, {E_ANY
,EX_RESERVEDID
}},
314 { "work_group_commit_write_pipe" , {1}, {E_ANY
,EX_RESERVEDID
}},
315 { "work_group_reduce_add" , {1}, {E_ANY
}},
316 { "work_group_reduce_max" , {1}, {E_ANY
}},
317 { "work_group_reduce_min" , {1}, {E_ANY
}},
318 { "work_group_reserve_read_pipe" , {1}, {E_ANY
,EX_UINT
}},
319 { "work_group_reserve_write_pipe" , {1}, {E_ANY
,EX_UINT
}},
320 { "work_group_scan_exclusive_add" , {1}, {E_ANY
}},
321 { "work_group_scan_exclusive_max" , {1}, {E_ANY
}},
322 { "work_group_scan_exclusive_min" , {1}, {E_ANY
}},
323 { "work_group_scan_inclusive_add" , {1}, {E_ANY
}},
324 { "work_group_scan_inclusive_max" , {1}, {E_ANY
}},
325 { "work_group_scan_inclusive_min" , {1}, {E_ANY
}},
326 { "write_imagef" , {1}, {E_ANY
,E_IMAGECOORDS
,EX_FLOAT4
}},
327 { "write_imagei" , {1}, {E_ANY
,E_IMAGECOORDS
,EX_INTV4
}},
328 { "write_imageui" , {1}, {E_ANY
,E_IMAGECOORDS
,EX_UINTV4
}},
329 { "ncos" , {1}, {E_ANY
} },
330 { "nexp2" , {1}, {E_ANY
} },
331 { "nfma" , {1}, {E_ANY
, E_COPY
, E_COPY
} },
332 { "nlog2" , {1}, {E_ANY
} },
333 { "nrcp" , {1}, {E_ANY
} },
334 { "nrsqrt" , {1}, {E_ANY
} },
335 { "nsin" , {1}, {E_ANY
} },
336 { "nsqrt" , {1}, {E_ANY
} },
337 { "ftz" , {1}, {E_ANY
} },
338 { "fldexp" , {1}, {E_ANY
, EX_UINT
} },
339 { "class" , {1}, {E_ANY
, EX_UINT
} },
340 { "rcbrt" , {1}, {E_ANY
} },
343 // Library functions with unmangled name.
344 const UnmangledFuncInfo
UnmangledFuncInfo::Table
[] = {
345 {"__read_pipe_2", 4},
346 {"__read_pipe_4", 6},
347 {"__write_pipe_2", 4},
348 {"__write_pipe_4", 6},
351 const unsigned UnmangledFuncInfo::TableSize
=
352 sizeof(UnmangledFuncInfo::Table
) / sizeof(UnmangledFuncInfo::Table
[0]);
354 UnmangledFuncInfo::NameMap
UnmangledFuncInfo::Map
;
356 static const struct ManglingRulesMap
: public StringMap
<int> {
358 : StringMap
<int>(sizeof(manglingRules
)/sizeof(manglingRules
[0])) {
360 for (auto Rule
: manglingRules
)
361 insert({ Rule
.Name
, Id
++ });
365 static AMDGPULibFunc::Param
getRetType(AMDGPULibFunc::EFuncId id
,
366 const AMDGPULibFunc::Param (&Leads
)[2]) {
367 AMDGPULibFunc::Param Res
= Leads
[0];
368 // TBD - This switch may require to be extended for other intriniscs
370 case AMDGPULibFunc::EI_SINCOS
:
371 Res
.PtrKind
= AMDGPULibFunc::BYVALUE
;
379 class ParamIterator
{
380 const AMDGPULibFunc::Param (&Leads
)[2];
381 const ManglingRule
& Rule
;
384 ParamIterator(const AMDGPULibFunc::Param (&leads
)[2],
385 const ManglingRule
& rule
)
386 : Leads(leads
), Rule(rule
), Index(0) {}
388 AMDGPULibFunc::Param
getNextParam();
391 AMDGPULibFunc::Param
ParamIterator::getNextParam() {
392 AMDGPULibFunc::Param P
;
393 if (Index
>= int(sizeof Rule
.Param
/sizeof Rule
.Param
[0])) return P
;
395 const char R
= Rule
.Param
[Index
];
399 P
.ArgType
= AMDGPULibFunc::U32
; break;
401 P
.ArgType
= AMDGPULibFunc::I32
; P
.VectorSize
= 4; break;
403 P
.ArgType
= AMDGPULibFunc::U32
; P
.VectorSize
= 4; break;
405 P
.ArgType
= AMDGPULibFunc::F32
; P
.VectorSize
= 4; break;
407 P
.ArgType
= AMDGPULibFunc::U64
; break;
409 P
.ArgType
= AMDGPULibFunc::EVENT
; break;
411 P
.ArgType
= AMDGPULibFunc::SAMPLER
; break;
412 case EX_RESERVEDID
: break; // TBD
414 if (Index
== (Rule
.Lead
[1] - 1)) P
= Leads
[1];
422 P
.PtrKind
= AMDGPULibFunc::BYVALUE
; break;
423 case E_V2_OF_POINTEE
:
424 P
.VectorSize
= 2; P
.PtrKind
= AMDGPULibFunc::BYVALUE
; break;
425 case E_V3_OF_POINTEE
:
426 P
.VectorSize
= 3; P
.PtrKind
= AMDGPULibFunc::BYVALUE
; break;
427 case E_V4_OF_POINTEE
:
428 P
.VectorSize
= 4; P
.PtrKind
= AMDGPULibFunc::BYVALUE
; break;
429 case E_V8_OF_POINTEE
:
430 P
.VectorSize
= 8; P
.PtrKind
= AMDGPULibFunc::BYVALUE
; break;
431 case E_V16_OF_POINTEE
:
432 P
.VectorSize
= 16; P
.PtrKind
= AMDGPULibFunc::BYVALUE
; break;
434 P
.PtrKind
|= AMDGPULibFunc::CONST
; break;
436 P
.PtrKind
|= AMDGPULibFunc::VOLATILE
; break;
438 P
.ArgType
= AMDGPULibFunc::I32
; break;
440 P
.ArgType
= AMDGPULibFunc::U32
; break;
443 P
.ArgType
&= ~AMDGPULibFunc::BASE_TYPE_MASK
;
444 P
.ArgType
|= AMDGPULibFunc::UINT
;
449 case AMDGPULibFunc::IMG1DA
: P
.VectorSize
= 2; break;
450 case AMDGPULibFunc::IMG1DB
: P
.VectorSize
= 1; break;
451 case AMDGPULibFunc::IMG2DA
: P
.VectorSize
= 4; break;
452 case AMDGPULibFunc::IMG1D
: P
.VectorSize
= 1; break;
453 case AMDGPULibFunc::IMG2D
: P
.VectorSize
= 2; break;
454 case AMDGPULibFunc::IMG3D
: P
.VectorSize
= 4; break;
456 P
.PtrKind
= AMDGPULibFunc::BYVALUE
;
457 P
.ArgType
= AMDGPULibFunc::I32
;
460 case E_CONSTPTR_SWAPGL
: {
461 unsigned AS
= AMDGPULibFunc::getAddrSpaceFromEPtrKind(P
.PtrKind
);
463 case AMDGPUAS::GLOBAL_ADDRESS
: AS
= AMDGPUAS::LOCAL_ADDRESS
; break;
464 case AMDGPUAS::LOCAL_ADDRESS
: AS
= AMDGPUAS::GLOBAL_ADDRESS
; break;
466 P
.PtrKind
= AMDGPULibFunc::getEPtrKindFromAddrSpace(AS
);
467 P
.PtrKind
|= AMDGPULibFunc::CONST
;
471 default: llvm_unreachable("Unhandeled param rule");
478 inline static void drop_front(StringRef
& str
, size_t n
= 1) {
479 str
= str
.drop_front(n
);
482 static bool eatTerm(StringRef
& mangledName
, const char c
) {
483 if (mangledName
.front() == c
) {
484 drop_front(mangledName
);
491 static bool eatTerm(StringRef
& mangledName
, const char (&str
)[N
]) {
492 if (mangledName
.startswith(StringRef(str
, N
-1))) {
493 drop_front(mangledName
, N
-1);
499 static inline bool isDigit(char c
) { return c
>= '0' && c
<= '9'; }
501 static int eatNumber(StringRef
& s
) {
502 size_t const savedSize
= s
.size();
504 while (!s
.empty() && isDigit(s
.front())) {
505 n
= n
*10 + s
.front() - '0';
508 return s
.size() < savedSize
? n
: -1;
511 static StringRef
eatLengthPrefixedName(StringRef
& mangledName
) {
512 int const Len
= eatNumber(mangledName
);
513 if (Len
<= 0 || static_cast<size_t>(Len
) > mangledName
.size())
515 StringRef Res
= mangledName
.substr(0, Len
);
516 drop_front(mangledName
, Len
);
520 } // end anonymous namespace
522 AMDGPUMangledLibFunc::AMDGPUMangledLibFunc() {
530 AMDGPUUnmangledLibFunc::AMDGPUUnmangledLibFunc() {
535 AMDGPUMangledLibFunc::AMDGPUMangledLibFunc(
536 EFuncId id
, const AMDGPUMangledLibFunc
©From
) {
538 FKind
= copyFrom
.FKind
;
539 Leads
[0] = copyFrom
.Leads
[0];
540 Leads
[1] = copyFrom
.Leads
[1];
543 ///////////////////////////////////////////////////////////////////////////////
546 static int parseVecSize(StringRef
& mangledName
) {
547 size_t const Len
= eatNumber(mangledName
);
549 case 2: case 3: case 4: case 8: case 16:
557 static AMDGPULibFunc::ENamePrefix
parseNamePrefix(StringRef
& mangledName
) {
558 std::pair
<StringRef
, StringRef
> const P
= mangledName
.split('_');
559 AMDGPULibFunc::ENamePrefix Pfx
=
560 StringSwitch
<AMDGPULibFunc::ENamePrefix
>(P
.first
)
561 .Case("native", AMDGPULibFunc::NATIVE
)
562 .Case("half" , AMDGPULibFunc::HALF
)
563 .Default(AMDGPULibFunc::NOPFX
);
565 if (Pfx
!= AMDGPULibFunc::NOPFX
)
566 mangledName
= P
.second
;
571 bool AMDGPUMangledLibFunc::parseUnmangledName(StringRef FullName
) {
572 FuncId
= static_cast<EFuncId
>(manglingRulesMap
.lookup(FullName
));
573 return FuncId
!= EI_NONE
;
576 ///////////////////////////////////////////////////////////////////////////////
577 // Itanium Demangling
580 struct ItaniumParamParser
{
581 AMDGPULibFunc::Param Prev
;
582 bool parseItaniumParam(StringRef
& param
, AMDGPULibFunc::Param
&res
);
586 bool ItaniumParamParser::parseItaniumParam(StringRef
& param
,
587 AMDGPULibFunc::Param
&res
) {
589 if (param
.empty()) return false;
591 // parse pointer prefix
592 if (eatTerm(param
, 'P')) {
593 if (eatTerm(param
, 'K')) res
.PtrKind
|= AMDGPULibFunc::CONST
;
594 if (eatTerm(param
, 'V')) res
.PtrKind
|= AMDGPULibFunc::VOLATILE
;
596 if (!eatTerm(param
, "U3AS")) {
599 AS
= param
.front() - '0';
600 drop_front(param
, 1);
602 res
.PtrKind
|= AMDGPULibFuncBase::getEPtrKindFromAddrSpace(AS
);
604 res
.PtrKind
= AMDGPULibFunc::BYVALUE
;
608 if (eatTerm(param
,"Dv")) {
609 res
.VectorSize
= parseVecSize(param
);
610 if (res
.VectorSize
==1 || !eatTerm(param
, '_')) return false;
614 char const TC
= param
.front();
616 res
.ArgType
= StringSwitch
<AMDGPULibFunc::EType
>
617 (eatLengthPrefixedName(param
))
618 .Case("ocl_image1darray" , AMDGPULibFunc::IMG1DA
)
619 .Case("ocl_image1dbuffer", AMDGPULibFunc::IMG1DB
)
620 .Case("ocl_image2darray" , AMDGPULibFunc::IMG2DA
)
621 .Case("ocl_image1d" , AMDGPULibFunc::IMG1D
)
622 .Case("ocl_image2d" , AMDGPULibFunc::IMG2D
)
623 .Case("ocl_image3d" , AMDGPULibFunc::IMG3D
)
624 .Case("ocl_event" , AMDGPULibFunc::DUMMY
)
625 .Case("ocl_sampler" , AMDGPULibFunc::DUMMY
)
626 .Default(AMDGPULibFunc::DUMMY
);
630 case 'h': res
.ArgType
= AMDGPULibFunc::U8
; break;
631 case 't': res
.ArgType
= AMDGPULibFunc::U16
; break;
632 case 'j': res
.ArgType
= AMDGPULibFunc::U32
; break;
633 case 'm': res
.ArgType
= AMDGPULibFunc::U64
; break;
634 case 'c': res
.ArgType
= AMDGPULibFunc::I8
; break;
635 case 's': res
.ArgType
= AMDGPULibFunc::I16
; break;
636 case 'i': res
.ArgType
= AMDGPULibFunc::I32
; break;
637 case 'l': res
.ArgType
= AMDGPULibFunc::I64
; break;
638 case 'f': res
.ArgType
= AMDGPULibFunc::F32
; break;
639 case 'd': res
.ArgType
= AMDGPULibFunc::F64
; break;
640 case 'D': if (!eatTerm(param
, 'h')) return false;
641 res
.ArgType
= AMDGPULibFunc::F16
; break;
643 if (!eatTerm(param
, '_')) {
645 if (!eatTerm(param
, '_')) return false;
647 res
.VectorSize
= Prev
.VectorSize
;
648 res
.ArgType
= Prev
.ArgType
;
653 if (res
.ArgType
== 0) return false;
654 Prev
.VectorSize
= res
.VectorSize
;
655 Prev
.ArgType
= res
.ArgType
;
659 bool AMDGPUMangledLibFunc::parseFuncName(StringRef
&mangledName
) {
660 StringRef Name
= eatLengthPrefixedName(mangledName
);
661 FKind
= parseNamePrefix(Name
);
662 if (!parseUnmangledName(Name
))
665 const ManglingRule
& Rule
= manglingRules
[FuncId
];
666 ItaniumParamParser Parser
;
667 for (int I
=0; I
< Rule
.maxLeadIndex(); ++I
) {
669 if (!Parser
.parseItaniumParam(mangledName
, P
))
672 if ((I
+ 1) == Rule
.Lead
[0]) Leads
[0] = P
;
673 if ((I
+ 1) == Rule
.Lead
[1]) Leads
[1] = P
;
678 bool AMDGPUUnmangledLibFunc::parseFuncName(StringRef
&Name
) {
679 if (!UnmangledFuncInfo::lookup(Name
, FuncId
))
685 bool AMDGPULibFunc::parse(StringRef FuncName
, AMDGPULibFunc
&F
) {
686 if (FuncName
.empty()) {
687 F
.Impl
= std::unique_ptr
<AMDGPULibFuncImpl
>();
691 if (eatTerm(FuncName
, "_Z"))
692 F
.Impl
= make_unique
<AMDGPUMangledLibFunc
>();
694 F
.Impl
= make_unique
<AMDGPUUnmangledLibFunc
>();
695 if (F
.Impl
->parseFuncName(FuncName
))
698 F
.Impl
= std::unique_ptr
<AMDGPULibFuncImpl
>();
702 StringRef
AMDGPUMangledLibFunc::getUnmangledName(StringRef mangledName
) {
703 StringRef S
= mangledName
;
704 if (eatTerm(S
, "_Z"))
705 return eatLengthPrefixedName(S
);
709 ///////////////////////////////////////////////////////////////////////////////
712 template <typename Stream
>
713 void AMDGPUMangledLibFunc::writeName(Stream
&OS
) const {
714 const char *Pfx
= "";
716 case NATIVE
: Pfx
= "native_"; break;
717 case HALF
: Pfx
= "half_"; break;
722 } else if (FuncId
!= EI_NONE
) {
724 const StringRef
& S
= manglingRules
[FuncId
].Name
;
725 OS
.write(S
.data(), S
.size());
729 std::string
AMDGPUMangledLibFunc::mangle() const { return mangleNameItanium(); }
731 ///////////////////////////////////////////////////////////////////////////////
734 static const char *getItaniumTypeName(AMDGPULibFunc::EType T
) {
736 case AMDGPULibFunc::U8
: return "h";
737 case AMDGPULibFunc::U16
: return "t";
738 case AMDGPULibFunc::U32
: return "j";
739 case AMDGPULibFunc::U64
: return "m";
740 case AMDGPULibFunc::I8
: return "c";
741 case AMDGPULibFunc::I16
: return "s";
742 case AMDGPULibFunc::I32
: return "i";
743 case AMDGPULibFunc::I64
: return "l";
744 case AMDGPULibFunc::F16
: return "Dh";
745 case AMDGPULibFunc::F32
: return "f";
746 case AMDGPULibFunc::F64
: return "d";
747 case AMDGPULibFunc::IMG1DA
: return "16ocl_image1darray";
748 case AMDGPULibFunc::IMG1DB
: return "17ocl_image1dbuffer";
749 case AMDGPULibFunc::IMG2DA
: return "16ocl_image2darray";
750 case AMDGPULibFunc::IMG1D
: return "11ocl_image1d";
751 case AMDGPULibFunc::IMG2D
: return "11ocl_image2d";
752 case AMDGPULibFunc::IMG3D
: return "11ocl_image3d";
753 case AMDGPULibFunc::SAMPLER
: return "11ocl_sampler";
754 case AMDGPULibFunc::EVENT
: return "9ocl_event";
755 default: llvm_unreachable("Unhandeled param type");
761 // Itanium mangling ABI says:
762 // "5.1.8. Compression
763 // ... Each non-terminal in the grammar for which <substitution> appears on the
764 // right-hand side is both a source of future substitutions and a candidate
765 // for being substituted. There are two exceptions that appear to be
766 // substitution candidates from the grammar, but are explicitly excluded:
767 // 1. <builtin-type> other than vendor extended types ..."
769 // For the purpose of functions the following productions make sence for the
771 // <type> ::= <builtin-type>
772 // ::= <class-enum-type>
774 // ::=<CV-qualifiers> <type>
775 // ::= P <type> # pointer-to
776 // ::= <substitution>
778 // Note that while types like images, samplers and events are by the ABI encoded
779 // using <class-enum-type> production rule they're not used for substitution
780 // because clang consider them as builtin types.
782 // DvNN_ type is GCC extension for vectors and is a subject for the substitution.
785 class ItaniumMangler
{
786 SmallVector
<AMDGPULibFunc::Param
, 10> Str
; // list of accumulated substituions
789 int findSubst(const AMDGPULibFunc::Param
& P
) const {
790 for(unsigned I
= 0; I
< Str
.size(); ++I
) {
791 const AMDGPULibFunc::Param
& T
= Str
[I
];
792 if (P
.PtrKind
== T
.PtrKind
&&
793 P
.VectorSize
== T
.VectorSize
&&
794 P
.ArgType
== T
.ArgType
) {
801 template <typename Stream
>
802 bool trySubst(Stream
& os
, const AMDGPULibFunc::Param
& p
) {
803 int const subst
= findSubst(p
);
804 if (subst
< 0) return false;
805 // Substitutions are mangled as S(XX)?_ where XX is a hexadecimal number
808 if (subst
== 0) os
<< "S_";
809 else os
<< 'S' << (subst
-1) << '_';
814 ItaniumMangler(bool useAddrSpace
)
815 : UseAddrSpace(useAddrSpace
) {}
817 template <typename Stream
>
818 void operator()(Stream
& os
, AMDGPULibFunc::Param p
) {
820 // Itanium mangling ABI 5.1.8. Compression:
821 // Logically, the substitutable components of a mangled name are considered
822 // left-to-right, components before the composite structure of which they
823 // are a part. If a component has been encountered before, it is substituted
824 // as described below. This decision is independent of whether its components
825 // have been substituted, so an implementation may optimize by considering
826 // large structures for substitution before their components. If a component
827 // has not been encountered before, its mangling is identified, and it is
828 // added to a dictionary of substitution candidates. No entity is added to
829 // the dictionary twice.
830 AMDGPULibFunc::Param Ptr
;
833 if (trySubst(os
, p
)) return;
835 if (p
.PtrKind
& AMDGPULibFunc::CONST
) os
<< 'K';
836 if (p
.PtrKind
& AMDGPULibFunc::VOLATILE
) os
<< 'V';
837 unsigned AS
= UseAddrSpace
838 ? AMDGPULibFuncBase::getAddrSpaceFromEPtrKind(p
.PtrKind
)
840 if (AS
!= 0) os
<< "U3AS" << AS
;
845 if (p
.VectorSize
> 1) {
846 if (trySubst(os
, p
)) goto exit
;
848 os
<< "Dv" << static_cast<unsigned>(p
.VectorSize
) << '_';
851 os
<< getItaniumTypeName((AMDGPULibFunc::EType
)p
.ArgType
);
854 if (Ptr
.ArgType
) Str
.push_back(Ptr
);
859 std::string
AMDGPUMangledLibFunc::mangleNameItanium() const {
860 SmallString
<128> Buf
;
861 raw_svector_ostream
S(Buf
);
862 SmallString
<128> NameBuf
;
863 raw_svector_ostream
Name(NameBuf
);
865 const StringRef
& NameStr
= Name
.str();
866 S
<< "_Z" << static_cast<int>(NameStr
.size()) << NameStr
;
868 ItaniumMangler
Mangler(true);
869 ParamIterator
I(Leads
, manglingRules
[FuncId
]);
871 while ((P
= I
.getNextParam()).ArgType
!= 0)
876 ///////////////////////////////////////////////////////////////////////////////
879 static Type
* getIntrinsicParamType(
881 const AMDGPULibFunc::Param
& P
,
885 case AMDGPULibFunc::U8
:
886 case AMDGPULibFunc::I8
: T
= Type::getInt8Ty(C
); break;
887 case AMDGPULibFunc::U16
:
888 case AMDGPULibFunc::I16
: T
= Type::getInt16Ty(C
); break;
889 case AMDGPULibFunc::U32
:
890 case AMDGPULibFunc::I32
: T
= Type::getInt32Ty(C
); break;
891 case AMDGPULibFunc::U64
:
892 case AMDGPULibFunc::I64
: T
= Type::getInt64Ty(C
); break;
893 case AMDGPULibFunc::F16
: T
= Type::getHalfTy(C
); break;
894 case AMDGPULibFunc::F32
: T
= Type::getFloatTy(C
); break;
895 case AMDGPULibFunc::F64
: T
= Type::getDoubleTy(C
); break;
897 case AMDGPULibFunc::IMG1DA
:
898 case AMDGPULibFunc::IMG1DB
:
899 case AMDGPULibFunc::IMG2DA
:
900 case AMDGPULibFunc::IMG1D
:
901 case AMDGPULibFunc::IMG2D
:
902 case AMDGPULibFunc::IMG3D
:
903 T
= StructType::create(C
,"ocl_image")->getPointerTo(); break;
904 case AMDGPULibFunc::SAMPLER
:
905 T
= StructType::create(C
,"ocl_sampler")->getPointerTo(); break;
906 case AMDGPULibFunc::EVENT
:
907 T
= StructType::create(C
,"ocl_event")->getPointerTo(); break;
909 llvm_unreachable("Unhandeled param type");
912 if (P
.VectorSize
> 1)
913 T
= VectorType::get(T
, P
.VectorSize
);
914 if (P
.PtrKind
!= AMDGPULibFunc::BYVALUE
)
915 T
= useAddrSpace
? T
->getPointerTo((P
.PtrKind
& AMDGPULibFunc::ADDR_SPACE
)
921 FunctionType
*AMDGPUMangledLibFunc::getFunctionType(Module
&M
) const {
922 LLVMContext
& C
= M
.getContext();
923 std::vector
<Type
*> Args
;
924 ParamIterator
I(Leads
, manglingRules
[FuncId
]);
926 while ((P
=I
.getNextParam()).ArgType
!= 0)
927 Args
.push_back(getIntrinsicParamType(C
, P
, true));
929 return FunctionType::get(
930 getIntrinsicParamType(C
, getRetType(FuncId
, Leads
), true),
934 unsigned AMDGPUMangledLibFunc::getNumArgs() const {
935 return manglingRules
[FuncId
].getNumArgs();
938 unsigned AMDGPUUnmangledLibFunc::getNumArgs() const {
939 return UnmangledFuncInfo::getNumArgs(FuncId
);
942 std::string
AMDGPUMangledLibFunc::getName() const {
943 SmallString
<128> Buf
;
944 raw_svector_ostream
OS(Buf
);
949 Function
*AMDGPULibFunc::getFunction(Module
*M
, const AMDGPULibFunc
&fInfo
) {
950 std::string FuncName
= fInfo
.mangle();
951 Function
*F
= dyn_cast_or_null
<Function
>(
952 M
->getValueSymbolTable().lookup(FuncName
));
954 // check formal with actual types conformance
955 if (F
&& !F
->isDeclaration()
957 && F
->arg_size() == fInfo
.getNumArgs()) {
963 FunctionCallee
AMDGPULibFunc::getOrInsertFunction(Module
*M
,
964 const AMDGPULibFunc
&fInfo
) {
965 std::string
const FuncName
= fInfo
.mangle();
966 Function
*F
= dyn_cast_or_null
<Function
>(
967 M
->getValueSymbolTable().lookup(FuncName
));
969 // check formal with actual types conformance
970 if (F
&& !F
->isDeclaration()
972 && F
->arg_size() == fInfo
.getNumArgs()) {
976 FunctionType
*FuncTy
= fInfo
.getFunctionType(*M
);
979 for (FunctionType::param_iterator
980 PI
= FuncTy
->param_begin(),
981 PE
= FuncTy
->param_end();
983 const Type
* argTy
= static_cast<const Type
*>(*PI
);
984 if (argTy
->isPointerTy()) {
992 // Do not set extra attributes for functions with pointer arguments.
993 C
= M
->getOrInsertFunction(FuncName
, FuncTy
);
996 LLVMContext
&Ctx
= M
->getContext();
997 Attr
= Attr
.addAttribute(Ctx
, AttributeList::FunctionIndex
,
998 Attribute::ReadOnly
);
999 Attr
= Attr
.addAttribute(Ctx
, AttributeList::FunctionIndex
,
1000 Attribute::NoUnwind
);
1001 C
= M
->getOrInsertFunction(FuncName
, FuncTy
, Attr
);
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
;