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(StringRef _Name
, unsigned _NumArgs
)
94 : Name(_Name
), NumArgs(_NumArgs
) {}
95 // Get index to Table by function name.
96 static bool lookup(StringRef Name
, ID
&Id
);
97 static unsigned toIndex(ID Id
) {
98 assert(static_cast<unsigned>(Id
) >
99 static_cast<unsigned>(AMDGPULibFunc::EI_LAST_MANGLED
) &&
100 "Invalid unmangled library function");
101 return static_cast<unsigned>(Id
) - 1 -
102 static_cast<unsigned>(AMDGPULibFunc::EI_LAST_MANGLED
);
104 static ID
toFuncId(unsigned Index
) {
105 assert(Index
< TableSize
&& "Invalid unmangled library function");
106 return static_cast<ID
>(
107 Index
+ 1 + static_cast<unsigned>(AMDGPULibFunc::EI_LAST_MANGLED
));
109 static unsigned getNumArgs(ID Id
) { return Table
[toIndex(Id
)].NumArgs
; }
110 static StringRef
getName(ID Id
) { return Table
[toIndex(Id
)].Name
; }
113 unsigned ManglingRule::getNumArgs() const {
115 while (I
< (sizeof Param
/sizeof Param
[0]) && Param
[I
]) ++I
;
119 // This table describes function formal argument type rules. The order of rules
120 // corresponds to the EFuncId enum at AMDGPULibFunc.h
122 // "<func name>", { <leads> }, { <param rules> }
124 // <leads> - list of integers that are one-based indexes of formal argument
125 // used to mangle a function name. Other argument types are derived from types
126 // of these 'leads'. The order of integers in this list correspond to the
127 // order in which these arguments are mangled in the EDG mangling scheme. The
128 // same order should be preserved for arguments in the AMDGPULibFunc structure
129 // when it is used for mangling. For example:
130 // { "vstorea_half", {3,1}, {E_ANY,EX_SIZET,E_ANY}},
131 // will be mangled in EDG scheme as vstorea_half_<3dparam>_<1stparam>
132 // When mangling from code use:
133 // AMDGPULibFunc insc;
134 // insc.param[0] = ... // describe 3rd parameter
135 // insc.param[1] = ... // describe 1rd parameter
137 // <param rules> - list of rules used to derive all of the function formal
138 // argument types. EX_ prefixed are simple types, other derived from the
139 // latest 'lead' argument type in the order of encoding from first to last.
140 // E_ANY - use prev lead type, E_CONSTPTR_ANY - make const pointer out of
141 // prev lead type, etc. see ParamIterator::getNextParam() for details.
143 static const ManglingRule manglingRules
[] = {
144 { StringRef(), {0}, {0} },
145 { "abs" , {1}, {E_ANY
}},
146 { "abs_diff" , {1}, {E_ANY
,E_COPY
}},
147 { "acos" , {1}, {E_ANY
}},
148 { "acosh" , {1}, {E_ANY
}},
149 { "acospi" , {1}, {E_ANY
}},
150 { "add_sat" , {1}, {E_ANY
,E_COPY
}},
151 { "all" , {1}, {E_ANY
}},
152 { "any" , {1}, {E_ANY
}},
153 { "asin" , {1}, {E_ANY
}},
154 { "asinh" , {1}, {E_ANY
}},
155 { "asinpi" , {1}, {E_ANY
}},
156 { "async_work_group_copy" , {1}, {E_ANY
,E_CONSTPTR_SWAPGL
,EX_SIZET
,EX_EVENT
}},
157 { "async_work_group_strided_copy" , {1}, {E_ANY
,E_CONSTPTR_SWAPGL
,EX_SIZET
,EX_SIZET
,EX_EVENT
}},
158 { "atan" , {1}, {E_ANY
}},
159 { "atan2" , {1}, {E_ANY
,E_COPY
}},
160 { "atan2pi" , {1}, {E_ANY
,E_COPY
}},
161 { "atanh" , {1}, {E_ANY
}},
162 { "atanpi" , {1}, {E_ANY
}},
163 { "atomic_add" , {1}, {E_VLTLPTR_ANY
,E_POINTEE
}},
164 { "atomic_and" , {1}, {E_VLTLPTR_ANY
,E_POINTEE
}},
165 { "atomic_cmpxchg" , {1}, {E_VLTLPTR_ANY
,E_POINTEE
,E_POINTEE
}},
166 { "atomic_dec" , {1}, {E_VLTLPTR_ANY
}},
167 { "atomic_inc" , {1}, {E_VLTLPTR_ANY
}},
168 { "atomic_max" , {1}, {E_VLTLPTR_ANY
,E_POINTEE
}},
169 { "atomic_min" , {1}, {E_VLTLPTR_ANY
,E_POINTEE
}},
170 { "atomic_or" , {1}, {E_VLTLPTR_ANY
,E_POINTEE
}},
171 { "atomic_sub" , {1}, {E_VLTLPTR_ANY
,E_POINTEE
}},
172 { "atomic_xchg" , {1}, {E_VLTLPTR_ANY
,E_POINTEE
}},
173 { "atomic_xor" , {1}, {E_VLTLPTR_ANY
,E_POINTEE
}},
174 { "bitselect" , {1}, {E_ANY
,E_COPY
,E_COPY
}},
175 { "cbrt" , {1}, {E_ANY
}},
176 { "ceil" , {1}, {E_ANY
}},
177 { "clamp" , {1}, {E_ANY
,E_COPY
,E_COPY
}},
178 { "clz" , {1}, {E_ANY
}},
179 { "commit_read_pipe" , {1}, {E_ANY
,EX_RESERVEDID
}},
180 { "commit_write_pipe" , {1}, {E_ANY
,EX_RESERVEDID
}},
181 { "copysign" , {1}, {E_ANY
,E_COPY
}},
182 { "cos" , {1}, {E_ANY
}},
183 { "cosh" , {1}, {E_ANY
}},
184 { "cospi" , {1}, {E_ANY
}},
185 { "cross" , {1}, {E_ANY
,E_COPY
}},
186 { "ctz" , {1}, {E_ANY
}},
187 { "degrees" , {1}, {E_ANY
}},
188 { "distance" , {1}, {E_ANY
,E_COPY
}},
189 { "divide" , {1}, {E_ANY
,E_COPY
}},
190 { "dot" , {1}, {E_ANY
,E_COPY
}},
191 { "erf" , {1}, {E_ANY
}},
192 { "erfc" , {1}, {E_ANY
}},
193 { "exp" , {1}, {E_ANY
}},
194 { "exp10" , {1}, {E_ANY
}},
195 { "exp2" , {1}, {E_ANY
}},
196 { "expm1" , {1}, {E_ANY
}},
197 { "fabs" , {1}, {E_ANY
}},
198 { "fast_distance" , {1}, {E_ANY
,E_COPY
}},
199 { "fast_length" , {1}, {E_ANY
}},
200 { "fast_normalize" , {1}, {E_ANY
}},
201 { "fdim" , {1}, {E_ANY
,E_COPY
}},
202 { "floor" , {1}, {E_ANY
}},
203 { "fma" , {1}, {E_ANY
,E_COPY
,E_COPY
}},
204 { "fmax" , {1}, {E_ANY
,E_COPY
}},
205 { "fmin" , {1}, {E_ANY
,E_COPY
}},
206 { "fmod" , {1}, {E_ANY
,E_COPY
}},
207 { "fract" , {2}, {E_POINTEE
,E_ANY
}},
208 { "frexp" , {1,2}, {E_ANY
,E_ANY
}},
209 { "get_image_array_size" , {1}, {E_ANY
}},
210 { "get_image_channel_data_type" , {1}, {E_ANY
}},
211 { "get_image_channel_order" , {1}, {E_ANY
}},
212 { "get_image_dim" , {1}, {E_ANY
}},
213 { "get_image_height" , {1}, {E_ANY
}},
214 { "get_image_width" , {1}, {E_ANY
}},
215 { "get_pipe_max_packets" , {1}, {E_ANY
}},
216 { "get_pipe_num_packets" , {1}, {E_ANY
}},
217 { "hadd" , {1}, {E_ANY
,E_COPY
}},
218 { "hypot" , {1}, {E_ANY
,E_COPY
}},
219 { "ilogb" , {1}, {E_ANY
}},
220 { "isequal" , {1}, {E_ANY
,E_COPY
}},
221 { "isfinite" , {1}, {E_ANY
}},
222 { "isgreater" , {1}, {E_ANY
,E_COPY
}},
223 { "isgreaterequal" , {1}, {E_ANY
,E_COPY
}},
224 { "isinf" , {1}, {E_ANY
}},
225 { "isless" , {1}, {E_ANY
,E_COPY
}},
226 { "islessequal" , {1}, {E_ANY
,E_COPY
}},
227 { "islessgreater" , {1}, {E_ANY
,E_COPY
}},
228 { "isnan" , {1}, {E_ANY
}},
229 { "isnormal" , {1}, {E_ANY
}},
230 { "isnotequal" , {1}, {E_ANY
,E_COPY
}},
231 { "isordered" , {1}, {E_ANY
,E_COPY
}},
232 { "isunordered" , {1}, {E_ANY
,E_COPY
}},
233 { "ldexp" , {1}, {E_ANY
,E_SETBASE_I32
}},
234 { "length" , {1}, {E_ANY
}},
235 { "lgamma" , {1}, {E_ANY
}},
236 { "lgamma_r" , {1,2}, {E_ANY
,E_ANY
}},
237 { "log" , {1}, {E_ANY
}},
238 { "log10" , {1}, {E_ANY
}},
239 { "log1p" , {1}, {E_ANY
}},
240 { "log2" , {1}, {E_ANY
}},
241 { "logb" , {1}, {E_ANY
}},
242 { "mad" , {1}, {E_ANY
,E_COPY
,E_COPY
}},
243 { "mad24" , {1}, {E_ANY
,E_COPY
,E_COPY
}},
244 { "mad_hi" , {1}, {E_ANY
,E_COPY
,E_COPY
}},
245 { "mad_sat" , {1}, {E_ANY
,E_COPY
,E_COPY
}},
246 { "max" , {1}, {E_ANY
,E_COPY
}},
247 { "maxmag" , {1}, {E_ANY
,E_COPY
}},
248 { "min" , {1}, {E_ANY
,E_COPY
}},
249 { "minmag" , {1}, {E_ANY
,E_COPY
}},
250 { "mix" , {1}, {E_ANY
,E_COPY
,E_COPY
}},
251 { "modf" , {2}, {E_POINTEE
,E_ANY
}},
252 { "mul24" , {1}, {E_ANY
,E_COPY
}},
253 { "mul_hi" , {1}, {E_ANY
,E_COPY
}},
254 { "nan" , {1}, {E_ANY
}},
255 { "nextafter" , {1}, {E_ANY
,E_COPY
}},
256 { "normalize" , {1}, {E_ANY
}},
257 { "popcount" , {1}, {E_ANY
}},
258 { "pow" , {1}, {E_ANY
,E_COPY
}},
259 { "pown" , {1}, {E_ANY
,E_SETBASE_I32
}},
260 { "powr" , {1}, {E_ANY
,E_COPY
}},
261 { "prefetch" , {1}, {E_CONSTPTR_ANY
,EX_SIZET
}},
262 { "radians" , {1}, {E_ANY
}},
263 { "recip" , {1}, {E_ANY
}},
264 { "remainder" , {1}, {E_ANY
,E_COPY
}},
265 { "remquo" , {1,3}, {E_ANY
,E_COPY
,E_ANY
}},
266 { "reserve_read_pipe" , {1}, {E_ANY
,EX_UINT
}},
267 { "reserve_write_pipe" , {1}, {E_ANY
,EX_UINT
}},
268 { "rhadd" , {1}, {E_ANY
,E_COPY
}},
269 { "rint" , {1}, {E_ANY
}},
270 { "rootn" , {1}, {E_ANY
,E_SETBASE_I32
}},
271 { "rotate" , {1}, {E_ANY
,E_COPY
}},
272 { "round" , {1}, {E_ANY
}},
273 { "rsqrt" , {1}, {E_ANY
}},
274 { "select" , {1,3}, {E_ANY
,E_COPY
,E_ANY
}},
275 { "shuffle" , {1,2}, {E_ANY
,E_ANY
}},
276 { "shuffle2" , {1,3}, {E_ANY
,E_COPY
,E_ANY
}},
277 { "sign" , {1}, {E_ANY
}},
278 { "signbit" , {1}, {E_ANY
}},
279 { "sin" , {1}, {E_ANY
}},
280 { "sincos" , {2}, {E_POINTEE
,E_ANY
}},
281 { "sinh" , {1}, {E_ANY
}},
282 { "sinpi" , {1}, {E_ANY
}},
283 { "smoothstep" , {1}, {E_ANY
,E_COPY
,E_COPY
}},
284 { "sqrt" , {1}, {E_ANY
}},
285 { "step" , {1}, {E_ANY
,E_COPY
}},
286 { "sub_group_broadcast" , {1}, {E_ANY
,EX_UINT
}},
287 { "sub_group_commit_read_pipe" , {1}, {E_ANY
,EX_RESERVEDID
}},
288 { "sub_group_commit_write_pipe" , {1}, {E_ANY
,EX_RESERVEDID
}},
289 { "sub_group_reduce_add" , {1}, {E_ANY
}},
290 { "sub_group_reduce_max" , {1}, {E_ANY
}},
291 { "sub_group_reduce_min" , {1}, {E_ANY
}},
292 { "sub_group_reserve_read_pipe" , {1}, {E_ANY
,EX_UINT
}},
293 { "sub_group_reserve_write_pipe" , {1}, {E_ANY
,EX_UINT
}},
294 { "sub_group_scan_exclusive_add" , {1}, {E_ANY
}},
295 { "sub_group_scan_exclusive_max" , {1}, {E_ANY
}},
296 { "sub_group_scan_exclusive_min" , {1}, {E_ANY
}},
297 { "sub_group_scan_inclusive_add" , {1}, {E_ANY
}},
298 { "sub_group_scan_inclusive_max" , {1}, {E_ANY
}},
299 { "sub_group_scan_inclusive_min" , {1}, {E_ANY
}},
300 { "sub_sat" , {1}, {E_ANY
,E_COPY
}},
301 { "tan" , {1}, {E_ANY
}},
302 { "tanh" , {1}, {E_ANY
}},
303 { "tanpi" , {1}, {E_ANY
}},
304 { "tgamma" , {1}, {E_ANY
}},
305 { "trunc" , {1}, {E_ANY
}},
306 { "upsample" , {1}, {E_ANY
,E_MAKEBASE_UNS
}},
307 { "vec_step" , {1}, {E_ANY
}},
308 { "vstore" , {3}, {E_POINTEE
,EX_SIZET
,E_ANY
}},
309 { "vstore16" , {3}, {E_V16_OF_POINTEE
,EX_SIZET
,E_ANY
}},
310 { "vstore2" , {3}, {E_V2_OF_POINTEE
,EX_SIZET
,E_ANY
}},
311 { "vstore3" , {3}, {E_V3_OF_POINTEE
,EX_SIZET
,E_ANY
}},
312 { "vstore4" , {3}, {E_V4_OF_POINTEE
,EX_SIZET
,E_ANY
}},
313 { "vstore8" , {3}, {E_V8_OF_POINTEE
,EX_SIZET
,E_ANY
}},
314 { "work_group_commit_read_pipe" , {1}, {E_ANY
,EX_RESERVEDID
}},
315 { "work_group_commit_write_pipe" , {1}, {E_ANY
,EX_RESERVEDID
}},
316 { "work_group_reduce_add" , {1}, {E_ANY
}},
317 { "work_group_reduce_max" , {1}, {E_ANY
}},
318 { "work_group_reduce_min" , {1}, {E_ANY
}},
319 { "work_group_reserve_read_pipe" , {1}, {E_ANY
,EX_UINT
}},
320 { "work_group_reserve_write_pipe" , {1}, {E_ANY
,EX_UINT
}},
321 { "work_group_scan_exclusive_add" , {1}, {E_ANY
}},
322 { "work_group_scan_exclusive_max" , {1}, {E_ANY
}},
323 { "work_group_scan_exclusive_min" , {1}, {E_ANY
}},
324 { "work_group_scan_inclusive_add" , {1}, {E_ANY
}},
325 { "work_group_scan_inclusive_max" , {1}, {E_ANY
}},
326 { "work_group_scan_inclusive_min" , {1}, {E_ANY
}},
327 { "write_imagef" , {1}, {E_ANY
,E_IMAGECOORDS
,EX_FLOAT4
}},
328 { "write_imagei" , {1}, {E_ANY
,E_IMAGECOORDS
,EX_INTV4
}},
329 { "write_imageui" , {1}, {E_ANY
,E_IMAGECOORDS
,EX_UINTV4
}},
330 { "ncos" , {1}, {E_ANY
} },
331 { "nexp2" , {1}, {E_ANY
} },
332 { "nfma" , {1}, {E_ANY
, E_COPY
, E_COPY
} },
333 { "nlog2" , {1}, {E_ANY
} },
334 { "nrcp" , {1}, {E_ANY
} },
335 { "nrsqrt" , {1}, {E_ANY
} },
336 { "nsin" , {1}, {E_ANY
} },
337 { "nsqrt" , {1}, {E_ANY
} },
338 { "ftz" , {1}, {E_ANY
} },
339 { "fldexp" , {1}, {E_ANY
, EX_UINT
} },
340 { "class" , {1}, {E_ANY
, EX_UINT
} },
341 { "rcbrt" , {1}, {E_ANY
} },
344 // Library functions with unmangled name.
345 const UnmangledFuncInfo
UnmangledFuncInfo::Table
[] = {
346 {"__read_pipe_2", 4},
347 {"__read_pipe_4", 6},
348 {"__write_pipe_2", 4},
349 {"__write_pipe_4", 6},
352 const unsigned UnmangledFuncInfo::TableSize
=
353 sizeof(UnmangledFuncInfo::Table
) / sizeof(UnmangledFuncInfo::Table
[0]);
355 UnmangledFuncInfo::NameMap
UnmangledFuncInfo::Map
;
357 static const struct ManglingRulesMap
: public StringMap
<int> {
359 : StringMap
<int>(sizeof(manglingRules
)/sizeof(manglingRules
[0])) {
361 for (auto Rule
: manglingRules
)
362 insert({ Rule
.Name
, Id
++ });
366 static AMDGPULibFunc::Param
getRetType(AMDGPULibFunc::EFuncId id
,
367 const AMDGPULibFunc::Param (&Leads
)[2]) {
368 AMDGPULibFunc::Param Res
= Leads
[0];
369 // TBD - This switch may require to be extended for other intriniscs
371 case AMDGPULibFunc::EI_SINCOS
:
372 Res
.PtrKind
= AMDGPULibFunc::BYVALUE
;
380 class ParamIterator
{
381 const AMDGPULibFunc::Param (&Leads
)[2];
382 const ManglingRule
& Rule
;
385 ParamIterator(const AMDGPULibFunc::Param (&leads
)[2],
386 const ManglingRule
& rule
)
387 : Leads(leads
), Rule(rule
), Index(0) {}
389 AMDGPULibFunc::Param
getNextParam();
392 AMDGPULibFunc::Param
ParamIterator::getNextParam() {
393 AMDGPULibFunc::Param P
;
394 if (Index
>= int(sizeof Rule
.Param
/sizeof Rule
.Param
[0])) return P
;
396 const char R
= Rule
.Param
[Index
];
400 P
.ArgType
= AMDGPULibFunc::U32
; break;
402 P
.ArgType
= AMDGPULibFunc::I32
; P
.VectorSize
= 4; break;
404 P
.ArgType
= AMDGPULibFunc::U32
; P
.VectorSize
= 4; break;
406 P
.ArgType
= AMDGPULibFunc::F32
; P
.VectorSize
= 4; break;
408 P
.ArgType
= AMDGPULibFunc::U64
; break;
410 P
.ArgType
= AMDGPULibFunc::EVENT
; break;
412 P
.ArgType
= AMDGPULibFunc::SAMPLER
; break;
413 case EX_RESERVEDID
: break; // TBD
415 if (Index
== (Rule
.Lead
[1] - 1)) P
= Leads
[1];
423 P
.PtrKind
= AMDGPULibFunc::BYVALUE
; break;
424 case E_V2_OF_POINTEE
:
425 P
.VectorSize
= 2; P
.PtrKind
= AMDGPULibFunc::BYVALUE
; break;
426 case E_V3_OF_POINTEE
:
427 P
.VectorSize
= 3; P
.PtrKind
= AMDGPULibFunc::BYVALUE
; break;
428 case E_V4_OF_POINTEE
:
429 P
.VectorSize
= 4; P
.PtrKind
= AMDGPULibFunc::BYVALUE
; break;
430 case E_V8_OF_POINTEE
:
431 P
.VectorSize
= 8; P
.PtrKind
= AMDGPULibFunc::BYVALUE
; break;
432 case E_V16_OF_POINTEE
:
433 P
.VectorSize
= 16; P
.PtrKind
= AMDGPULibFunc::BYVALUE
; break;
435 P
.PtrKind
|= AMDGPULibFunc::CONST
; break;
437 P
.PtrKind
|= AMDGPULibFunc::VOLATILE
; break;
439 P
.ArgType
= AMDGPULibFunc::I32
; break;
441 P
.ArgType
= AMDGPULibFunc::U32
; break;
444 P
.ArgType
&= ~AMDGPULibFunc::BASE_TYPE_MASK
;
445 P
.ArgType
|= AMDGPULibFunc::UINT
;
450 case AMDGPULibFunc::IMG1DA
: P
.VectorSize
= 2; break;
451 case AMDGPULibFunc::IMG1DB
: P
.VectorSize
= 1; break;
452 case AMDGPULibFunc::IMG2DA
: P
.VectorSize
= 4; break;
453 case AMDGPULibFunc::IMG1D
: P
.VectorSize
= 1; break;
454 case AMDGPULibFunc::IMG2D
: P
.VectorSize
= 2; break;
455 case AMDGPULibFunc::IMG3D
: P
.VectorSize
= 4; break;
457 P
.PtrKind
= AMDGPULibFunc::BYVALUE
;
458 P
.ArgType
= AMDGPULibFunc::I32
;
461 case E_CONSTPTR_SWAPGL
: {
462 unsigned AS
= AMDGPULibFunc::getAddrSpaceFromEPtrKind(P
.PtrKind
);
464 case AMDGPUAS::GLOBAL_ADDRESS
: AS
= AMDGPUAS::LOCAL_ADDRESS
; break;
465 case AMDGPUAS::LOCAL_ADDRESS
: AS
= AMDGPUAS::GLOBAL_ADDRESS
; break;
467 P
.PtrKind
= AMDGPULibFunc::getEPtrKindFromAddrSpace(AS
);
468 P
.PtrKind
|= AMDGPULibFunc::CONST
;
472 default: llvm_unreachable("Unhandeled param rule");
479 inline static void drop_front(StringRef
& str
, size_t n
= 1) {
480 str
= str
.drop_front(n
);
483 static bool eatTerm(StringRef
& mangledName
, const char c
) {
484 if (mangledName
.front() == c
) {
485 drop_front(mangledName
);
492 static bool eatTerm(StringRef
& mangledName
, const char (&str
)[N
]) {
493 if (mangledName
.startswith(StringRef(str
, N
-1))) {
494 drop_front(mangledName
, N
-1);
500 static inline bool isDigit(char c
) { return c
>= '0' && c
<= '9'; }
502 static int eatNumber(StringRef
& s
) {
503 size_t const savedSize
= s
.size();
505 while (!s
.empty() && isDigit(s
.front())) {
506 n
= n
*10 + s
.front() - '0';
509 return s
.size() < savedSize
? n
: -1;
512 static StringRef
eatLengthPrefixedName(StringRef
& mangledName
) {
513 int const Len
= eatNumber(mangledName
);
514 if (Len
<= 0 || static_cast<size_t>(Len
) > mangledName
.size())
516 StringRef Res
= mangledName
.substr(0, Len
);
517 drop_front(mangledName
, Len
);
521 } // end anonymous namespace
523 AMDGPUMangledLibFunc::AMDGPUMangledLibFunc() {
531 AMDGPUUnmangledLibFunc::AMDGPUUnmangledLibFunc() {
536 AMDGPUMangledLibFunc::AMDGPUMangledLibFunc(
537 EFuncId id
, const AMDGPUMangledLibFunc
©From
) {
539 FKind
= copyFrom
.FKind
;
540 Leads
[0] = copyFrom
.Leads
[0];
541 Leads
[1] = copyFrom
.Leads
[1];
544 ///////////////////////////////////////////////////////////////////////////////
547 static int parseVecSize(StringRef
& mangledName
) {
548 size_t const Len
= eatNumber(mangledName
);
550 case 2: case 3: case 4: case 8: case 16:
558 static AMDGPULibFunc::ENamePrefix
parseNamePrefix(StringRef
& mangledName
) {
559 std::pair
<StringRef
, StringRef
> const P
= mangledName
.split('_');
560 AMDGPULibFunc::ENamePrefix Pfx
=
561 StringSwitch
<AMDGPULibFunc::ENamePrefix
>(P
.first
)
562 .Case("native", AMDGPULibFunc::NATIVE
)
563 .Case("half" , AMDGPULibFunc::HALF
)
564 .Default(AMDGPULibFunc::NOPFX
);
566 if (Pfx
!= AMDGPULibFunc::NOPFX
)
567 mangledName
= P
.second
;
572 bool AMDGPUMangledLibFunc::parseUnmangledName(StringRef FullName
) {
573 FuncId
= static_cast<EFuncId
>(manglingRulesMap
.lookup(FullName
));
574 return FuncId
!= EI_NONE
;
577 ///////////////////////////////////////////////////////////////////////////////
578 // Itanium Demangling
581 struct ItaniumParamParser
{
582 AMDGPULibFunc::Param Prev
;
583 bool parseItaniumParam(StringRef
& param
, AMDGPULibFunc::Param
&res
);
587 bool ItaniumParamParser::parseItaniumParam(StringRef
& param
,
588 AMDGPULibFunc::Param
&res
) {
590 if (param
.empty()) return false;
592 // parse pointer prefix
593 if (eatTerm(param
, 'P')) {
594 if (eatTerm(param
, 'K')) res
.PtrKind
|= AMDGPULibFunc::CONST
;
595 if (eatTerm(param
, 'V')) res
.PtrKind
|= AMDGPULibFunc::VOLATILE
;
597 if (!eatTerm(param
, "U3AS")) {
600 AS
= param
.front() - '0';
601 drop_front(param
, 1);
603 res
.PtrKind
|= AMDGPULibFuncBase::getEPtrKindFromAddrSpace(AS
);
605 res
.PtrKind
= AMDGPULibFunc::BYVALUE
;
609 if (eatTerm(param
,"Dv")) {
610 res
.VectorSize
= parseVecSize(param
);
611 if (res
.VectorSize
==1 || !eatTerm(param
, '_')) return false;
615 char const TC
= param
.front();
617 res
.ArgType
= StringSwitch
<AMDGPULibFunc::EType
>
618 (eatLengthPrefixedName(param
))
619 .Case("ocl_image1darray" , AMDGPULibFunc::IMG1DA
)
620 .Case("ocl_image1dbuffer", AMDGPULibFunc::IMG1DB
)
621 .Case("ocl_image2darray" , AMDGPULibFunc::IMG2DA
)
622 .Case("ocl_image1d" , AMDGPULibFunc::IMG1D
)
623 .Case("ocl_image2d" , AMDGPULibFunc::IMG2D
)
624 .Case("ocl_image3d" , AMDGPULibFunc::IMG3D
)
625 .Case("ocl_event" , AMDGPULibFunc::DUMMY
)
626 .Case("ocl_sampler" , AMDGPULibFunc::DUMMY
)
627 .Default(AMDGPULibFunc::DUMMY
);
631 case 'h': res
.ArgType
= AMDGPULibFunc::U8
; break;
632 case 't': res
.ArgType
= AMDGPULibFunc::U16
; break;
633 case 'j': res
.ArgType
= AMDGPULibFunc::U32
; break;
634 case 'm': res
.ArgType
= AMDGPULibFunc::U64
; break;
635 case 'c': res
.ArgType
= AMDGPULibFunc::I8
; break;
636 case 's': res
.ArgType
= AMDGPULibFunc::I16
; break;
637 case 'i': res
.ArgType
= AMDGPULibFunc::I32
; break;
638 case 'l': res
.ArgType
= AMDGPULibFunc::I64
; break;
639 case 'f': res
.ArgType
= AMDGPULibFunc::F32
; break;
640 case 'd': res
.ArgType
= AMDGPULibFunc::F64
; break;
641 case 'D': if (!eatTerm(param
, 'h')) return false;
642 res
.ArgType
= AMDGPULibFunc::F16
; break;
644 if (!eatTerm(param
, '_')) {
646 if (!eatTerm(param
, '_')) return false;
648 res
.VectorSize
= Prev
.VectorSize
;
649 res
.ArgType
= Prev
.ArgType
;
654 if (res
.ArgType
== 0) return false;
655 Prev
.VectorSize
= res
.VectorSize
;
656 Prev
.ArgType
= res
.ArgType
;
660 bool AMDGPUMangledLibFunc::parseFuncName(StringRef
&mangledName
) {
661 StringRef Name
= eatLengthPrefixedName(mangledName
);
662 FKind
= parseNamePrefix(Name
);
663 if (!parseUnmangledName(Name
))
666 const ManglingRule
& Rule
= manglingRules
[FuncId
];
667 ItaniumParamParser Parser
;
668 for (int I
=0; I
< Rule
.maxLeadIndex(); ++I
) {
670 if (!Parser
.parseItaniumParam(mangledName
, P
))
673 if ((I
+ 1) == Rule
.Lead
[0]) Leads
[0] = P
;
674 if ((I
+ 1) == Rule
.Lead
[1]) Leads
[1] = P
;
679 bool AMDGPUUnmangledLibFunc::parseFuncName(StringRef
&Name
) {
680 if (!UnmangledFuncInfo::lookup(Name
, FuncId
))
686 bool AMDGPULibFunc::parse(StringRef FuncName
, AMDGPULibFunc
&F
) {
687 if (FuncName
.empty()) {
688 F
.Impl
= std::unique_ptr
<AMDGPULibFuncImpl
>();
692 if (eatTerm(FuncName
, "_Z"))
693 F
.Impl
= make_unique
<AMDGPUMangledLibFunc
>();
695 F
.Impl
= make_unique
<AMDGPUUnmangledLibFunc
>();
696 if (F
.Impl
->parseFuncName(FuncName
))
699 F
.Impl
= std::unique_ptr
<AMDGPULibFuncImpl
>();
703 StringRef
AMDGPUMangledLibFunc::getUnmangledName(StringRef mangledName
) {
704 StringRef S
= mangledName
;
705 if (eatTerm(S
, "_Z"))
706 return eatLengthPrefixedName(S
);
710 ///////////////////////////////////////////////////////////////////////////////
713 template <typename Stream
>
714 void AMDGPUMangledLibFunc::writeName(Stream
&OS
) const {
715 const char *Pfx
= "";
717 case NATIVE
: Pfx
= "native_"; break;
718 case HALF
: Pfx
= "half_"; break;
723 } else if (FuncId
!= EI_NONE
) {
725 const StringRef
& S
= manglingRules
[FuncId
].Name
;
726 OS
.write(S
.data(), S
.size());
730 std::string
AMDGPUMangledLibFunc::mangle() const { return mangleNameItanium(); }
732 ///////////////////////////////////////////////////////////////////////////////
735 static const char *getItaniumTypeName(AMDGPULibFunc::EType T
) {
737 case AMDGPULibFunc::U8
: return "h";
738 case AMDGPULibFunc::U16
: return "t";
739 case AMDGPULibFunc::U32
: return "j";
740 case AMDGPULibFunc::U64
: return "m";
741 case AMDGPULibFunc::I8
: return "c";
742 case AMDGPULibFunc::I16
: return "s";
743 case AMDGPULibFunc::I32
: return "i";
744 case AMDGPULibFunc::I64
: return "l";
745 case AMDGPULibFunc::F16
: return "Dh";
746 case AMDGPULibFunc::F32
: return "f";
747 case AMDGPULibFunc::F64
: return "d";
748 case AMDGPULibFunc::IMG1DA
: return "16ocl_image1darray";
749 case AMDGPULibFunc::IMG1DB
: return "17ocl_image1dbuffer";
750 case AMDGPULibFunc::IMG2DA
: return "16ocl_image2darray";
751 case AMDGPULibFunc::IMG1D
: return "11ocl_image1d";
752 case AMDGPULibFunc::IMG2D
: return "11ocl_image2d";
753 case AMDGPULibFunc::IMG3D
: return "11ocl_image3d";
754 case AMDGPULibFunc::SAMPLER
: return "11ocl_sampler";
755 case AMDGPULibFunc::EVENT
: return "9ocl_event";
756 default: llvm_unreachable("Unhandeled param type");
762 // Itanium mangling ABI says:
763 // "5.1.8. Compression
764 // ... Each non-terminal in the grammar for which <substitution> appears on the
765 // right-hand side is both a source of future substitutions and a candidate
766 // for being substituted. There are two exceptions that appear to be
767 // substitution candidates from the grammar, but are explicitly excluded:
768 // 1. <builtin-type> other than vendor extended types ..."
770 // For the purpose of functions the following productions make sence for the
772 // <type> ::= <builtin-type>
773 // ::= <class-enum-type>
775 // ::=<CV-qualifiers> <type>
776 // ::= P <type> # pointer-to
777 // ::= <substitution>
779 // Note that while types like images, samplers and events are by the ABI encoded
780 // using <class-enum-type> production rule they're not used for substitution
781 // because clang consider them as builtin types.
783 // DvNN_ type is GCC extension for vectors and is a subject for the substitution.
786 class ItaniumMangler
{
787 SmallVector
<AMDGPULibFunc::Param
, 10> Str
; // list of accumulated substituions
790 int findSubst(const AMDGPULibFunc::Param
& P
) const {
791 for(unsigned I
= 0; I
< Str
.size(); ++I
) {
792 const AMDGPULibFunc::Param
& T
= Str
[I
];
793 if (P
.PtrKind
== T
.PtrKind
&&
794 P
.VectorSize
== T
.VectorSize
&&
795 P
.ArgType
== T
.ArgType
) {
802 template <typename Stream
>
803 bool trySubst(Stream
& os
, const AMDGPULibFunc::Param
& p
) {
804 int const subst
= findSubst(p
);
805 if (subst
< 0) return false;
806 // Substitutions are mangled as S(XX)?_ where XX is a hexadecimal number
809 if (subst
== 0) os
<< "S_";
810 else os
<< 'S' << (subst
-1) << '_';
815 ItaniumMangler(bool useAddrSpace
)
816 : UseAddrSpace(useAddrSpace
) {}
818 template <typename Stream
>
819 void operator()(Stream
& os
, AMDGPULibFunc::Param p
) {
821 // Itanium mangling ABI 5.1.8. Compression:
822 // Logically, the substitutable components of a mangled name are considered
823 // left-to-right, components before the composite structure of which they
824 // are a part. If a component has been encountered before, it is substituted
825 // as described below. This decision is independent of whether its components
826 // have been substituted, so an implementation may optimize by considering
827 // large structures for substitution before their components. If a component
828 // has not been encountered before, its mangling is identified, and it is
829 // added to a dictionary of substitution candidates. No entity is added to
830 // the dictionary twice.
831 AMDGPULibFunc::Param Ptr
;
834 if (trySubst(os
, p
)) return;
836 if (p
.PtrKind
& AMDGPULibFunc::CONST
) os
<< 'K';
837 if (p
.PtrKind
& AMDGPULibFunc::VOLATILE
) os
<< 'V';
838 unsigned AS
= UseAddrSpace
839 ? AMDGPULibFuncBase::getAddrSpaceFromEPtrKind(p
.PtrKind
)
841 if (AS
!= 0) os
<< "U3AS" << AS
;
846 if (p
.VectorSize
> 1) {
847 if (trySubst(os
, p
)) goto exit
;
849 os
<< "Dv" << static_cast<unsigned>(p
.VectorSize
) << '_';
852 os
<< getItaniumTypeName((AMDGPULibFunc::EType
)p
.ArgType
);
855 if (Ptr
.ArgType
) Str
.push_back(Ptr
);
860 std::string
AMDGPUMangledLibFunc::mangleNameItanium() const {
861 SmallString
<128> Buf
;
862 raw_svector_ostream
S(Buf
);
863 SmallString
<128> NameBuf
;
864 raw_svector_ostream
Name(NameBuf
);
866 const StringRef
& NameStr
= Name
.str();
867 S
<< "_Z" << static_cast<int>(NameStr
.size()) << NameStr
;
869 ItaniumMangler
Mangler(true);
870 ParamIterator
I(Leads
, manglingRules
[FuncId
]);
872 while ((P
= I
.getNextParam()).ArgType
!= 0)
877 ///////////////////////////////////////////////////////////////////////////////
880 static Type
* getIntrinsicParamType(
882 const AMDGPULibFunc::Param
& P
,
886 case AMDGPULibFunc::U8
:
887 case AMDGPULibFunc::I8
: T
= Type::getInt8Ty(C
); break;
888 case AMDGPULibFunc::U16
:
889 case AMDGPULibFunc::I16
: T
= Type::getInt16Ty(C
); break;
890 case AMDGPULibFunc::U32
:
891 case AMDGPULibFunc::I32
: T
= Type::getInt32Ty(C
); break;
892 case AMDGPULibFunc::U64
:
893 case AMDGPULibFunc::I64
: T
= Type::getInt64Ty(C
); break;
894 case AMDGPULibFunc::F16
: T
= Type::getHalfTy(C
); break;
895 case AMDGPULibFunc::F32
: T
= Type::getFloatTy(C
); break;
896 case AMDGPULibFunc::F64
: T
= Type::getDoubleTy(C
); break;
898 case AMDGPULibFunc::IMG1DA
:
899 case AMDGPULibFunc::IMG1DB
:
900 case AMDGPULibFunc::IMG2DA
:
901 case AMDGPULibFunc::IMG1D
:
902 case AMDGPULibFunc::IMG2D
:
903 case AMDGPULibFunc::IMG3D
:
904 T
= StructType::create(C
,"ocl_image")->getPointerTo(); break;
905 case AMDGPULibFunc::SAMPLER
:
906 T
= StructType::create(C
,"ocl_sampler")->getPointerTo(); break;
907 case AMDGPULibFunc::EVENT
:
908 T
= StructType::create(C
,"ocl_event")->getPointerTo(); break;
910 llvm_unreachable("Unhandeled param type");
913 if (P
.VectorSize
> 1)
914 T
= VectorType::get(T
, P
.VectorSize
);
915 if (P
.PtrKind
!= AMDGPULibFunc::BYVALUE
)
916 T
= useAddrSpace
? T
->getPointerTo((P
.PtrKind
& AMDGPULibFunc::ADDR_SPACE
)
922 FunctionType
*AMDGPUMangledLibFunc::getFunctionType(Module
&M
) const {
923 LLVMContext
& C
= M
.getContext();
924 std::vector
<Type
*> Args
;
925 ParamIterator
I(Leads
, manglingRules
[FuncId
]);
927 while ((P
=I
.getNextParam()).ArgType
!= 0)
928 Args
.push_back(getIntrinsicParamType(C
, P
, true));
930 return FunctionType::get(
931 getIntrinsicParamType(C
, getRetType(FuncId
, Leads
), true),
935 unsigned AMDGPUMangledLibFunc::getNumArgs() const {
936 return manglingRules
[FuncId
].getNumArgs();
939 unsigned AMDGPUUnmangledLibFunc::getNumArgs() const {
940 return UnmangledFuncInfo::getNumArgs(FuncId
);
943 std::string
AMDGPUMangledLibFunc::getName() const {
944 SmallString
<128> Buf
;
945 raw_svector_ostream
OS(Buf
);
950 Function
*AMDGPULibFunc::getFunction(Module
*M
, const AMDGPULibFunc
&fInfo
) {
951 std::string FuncName
= fInfo
.mangle();
952 Function
*F
= dyn_cast_or_null
<Function
>(
953 M
->getValueSymbolTable().lookup(FuncName
));
955 // check formal with actual types conformance
956 if (F
&& !F
->isDeclaration()
958 && F
->arg_size() == fInfo
.getNumArgs()) {
964 Function
*AMDGPULibFunc::getOrInsertFunction(Module
*M
,
965 const AMDGPULibFunc
&fInfo
) {
966 std::string
const FuncName
= fInfo
.mangle();
967 Function
*F
= dyn_cast_or_null
<Function
>(
968 M
->getValueSymbolTable().lookup(FuncName
));
970 // check formal with actual types conformance
971 if (F
&& !F
->isDeclaration()
973 && F
->arg_size() == fInfo
.getNumArgs()) {
977 FunctionType
*FuncTy
= fInfo
.getFunctionType(*M
);
980 for (FunctionType::param_iterator
981 PI
= FuncTy
->param_begin(),
982 PE
= FuncTy
->param_end();
984 const Type
* argTy
= static_cast<const Type
*>(*PI
);
985 if (argTy
->isPointerTy()) {
991 Constant
*C
= nullptr;
993 // Do not set extra attributes for functions with pointer arguments.
994 C
= M
->getOrInsertFunction(FuncName
, FuncTy
);
997 LLVMContext
&Ctx
= M
->getContext();
998 Attr
.addAttribute(Ctx
, AttributeList::FunctionIndex
, Attribute::ReadOnly
);
999 Attr
.addAttribute(Ctx
, AttributeList::FunctionIndex
, Attribute::NoUnwind
);
1000 C
= M
->getOrInsertFunction(FuncName
, FuncTy
, Attr
);
1003 return cast
<Function
>(C
);
1006 bool UnmangledFuncInfo::lookup(StringRef Name
, ID
&Id
) {
1007 auto Loc
= Map
.find(Name
);
1008 if (Loc
!= Map
.end()) {
1009 Id
= toFuncId(Loc
->second
);
1012 Id
= AMDGPULibFunc::EI_NONE
;
1016 AMDGPULibFunc::AMDGPULibFunc(const AMDGPULibFunc
&F
) {
1017 if (auto *MF
= dyn_cast
<AMDGPUMangledLibFunc
>(F
.Impl
.get()))
1018 Impl
.reset(new AMDGPUMangledLibFunc(*MF
));
1019 else if (auto *UMF
= dyn_cast
<AMDGPUUnmangledLibFunc
>(F
.Impl
.get()))
1020 Impl
.reset(new AMDGPUUnmangledLibFunc(*UMF
));
1022 Impl
= std::unique_ptr
<AMDGPULibFuncImpl
>();
1025 AMDGPULibFunc
&AMDGPULibFunc::operator=(const AMDGPULibFunc
&F
) {
1028 new (this) AMDGPULibFunc(F
);
1032 AMDGPULibFunc::AMDGPULibFunc(EFuncId Id
, const AMDGPULibFunc
&CopyFrom
) {
1033 assert(AMDGPULibFuncBase::isMangled(Id
) && CopyFrom
.isMangled() &&
1035 Impl
.reset(new AMDGPUMangledLibFunc(
1036 Id
, *cast
<AMDGPUMangledLibFunc
>(CopyFrom
.Impl
.get())));
1039 AMDGPULibFunc::AMDGPULibFunc(StringRef Name
, FunctionType
*FT
) {
1040 Impl
.reset(new AMDGPUUnmangledLibFunc(Name
, FT
));
1043 void AMDGPULibFunc::initMangled() { Impl
.reset(new AMDGPUMangledLibFunc()); }
1045 AMDGPULibFunc::Param
*AMDGPULibFunc::getLeads() {
1048 return cast
<AMDGPUMangledLibFunc
>(Impl
.get())->Leads
;
1051 const AMDGPULibFunc::Param
*AMDGPULibFunc::getLeads() const {
1052 return cast
<const AMDGPUMangledLibFunc
>(Impl
.get())->Leads
;