]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm-project/llvm/lib/Target/AMDGPU/AMDGPULibFunc.cpp
Merge ^/head r364251 through r364263.
[FreeBSD/FreeBSD.git] / contrib / llvm-project / llvm / lib / Target / AMDGPU / AMDGPULibFunc.cpp
1 //===-- AMDGPULibFunc.cpp -------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 //  This file contains utility functions to work with Itanium mangled names
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include "AMDGPULibFunc.h"
14 #include "AMDGPU.h"
15 #include "llvm/ADT/SmallString.h"
16 #include "llvm/ADT/SmallVector.h"
17 #include "llvm/ADT/StringExtras.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"
25 #include <string>
26
27 using namespace llvm;
28
29 namespace {
30
31 enum EManglingParam {
32     E_NONE,
33     EX_EVENT,
34     EX_FLOAT4,
35     EX_INTV4,
36     EX_RESERVEDID,
37     EX_SAMPLER,
38     EX_SIZET,
39     EX_UINT,
40     EX_UINTV4,
41     E_ANY,
42     E_CONSTPTR_ANY,
43     E_CONSTPTR_SWAPGL,
44     E_COPY,
45     E_IMAGECOORDS,
46     E_POINTEE,
47     E_SETBASE_I32,
48     E_SETBASE_U32,
49     E_MAKEBASE_UNS,
50     E_V16_OF_POINTEE,
51     E_V2_OF_POINTEE,
52     E_V3_OF_POINTEE,
53     E_V4_OF_POINTEE,
54     E_V8_OF_POINTEE,
55     E_VLTLPTR_ANY,
56 };
57
58 struct ManglingRule {
59    const char *Name;
60    unsigned char Lead[2];
61    unsigned char Param[5];
62
63    int maxLeadIndex() const { return (std::max)(Lead[0], Lead[1]); }
64    int getNumLeads() const { return (Lead[0] ? 1 : 0) + (Lead[1] ? 1 : 0); }
65
66    unsigned getNumArgs() const;
67
68    static StringMap<int> buildManglingRulesMap();
69 };
70
71 // Information about library functions with unmangled names.
72 class UnmangledFuncInfo {
73   const char *Name;
74   unsigned NumArgs;
75
76   // Table for all lib functions with unmangled names.
77   static const UnmangledFuncInfo Table[];
78
79   // Number of entries in Table.
80   static const unsigned TableSize;
81
82   static StringMap<unsigned> buildNameMap();
83
84 public:
85   using ID = AMDGPULibFunc::EFuncId;
86   constexpr UnmangledFuncInfo(const char *_Name, unsigned _NumArgs)
87       : Name(_Name), NumArgs(_NumArgs) {}
88   // Get index to Table by function name.
89   static bool lookup(StringRef Name, ID &Id);
90   static unsigned toIndex(ID Id) {
91     assert(static_cast<unsigned>(Id) >
92                static_cast<unsigned>(AMDGPULibFunc::EI_LAST_MANGLED) &&
93            "Invalid unmangled library function");
94     return static_cast<unsigned>(Id) - 1 -
95            static_cast<unsigned>(AMDGPULibFunc::EI_LAST_MANGLED);
96   }
97   static ID toFuncId(unsigned Index) {
98     assert(Index < TableSize &&
99            "Invalid unmangled library function");
100     return static_cast<ID>(
101         Index + 1 + static_cast<unsigned>(AMDGPULibFunc::EI_LAST_MANGLED));
102   }
103   static unsigned getNumArgs(ID Id) { return Table[toIndex(Id)].NumArgs; }
104   static StringRef getName(ID Id) { return Table[toIndex(Id)].Name; }
105 };
106
107 unsigned ManglingRule::getNumArgs() const {
108    unsigned I=0;
109    while (I < (sizeof Param/sizeof Param[0]) && Param[I]) ++I;
110    return I;
111 }
112
113 // This table describes function formal argument type rules. The order of rules
114 // corresponds to the EFuncId enum at AMDGPULibFunc.h
115 //
116 // "<func name>", { <leads> }, { <param rules> }
117 // where:
118 //  <leads> - list of integers that are one-based indexes of formal argument
119 //    used to mangle a function name. Other argument types are derived from types
120 //    of these 'leads'. The order of integers in this list correspond to the
121 //    order in which these arguments are mangled in the EDG mangling scheme. The
122 //    same order should be preserved for arguments in the AMDGPULibFunc structure
123 //    when it is used for mangling. For example:
124 //    { "vstorea_half", {3,1}, {E_ANY,EX_SIZET,E_ANY}},
125 //    will be mangled in EDG scheme as  vstorea_half_<3dparam>_<1stparam>
126 //    When mangling from code use:
127 //    AMDGPULibFunc insc;
128 //    insc.param[0] = ... // describe 3rd parameter
129 //    insc.param[1] = ... // describe 1rd parameter
130 //
131 // <param rules> - list of rules used to derive all of the function formal
132 //    argument types. EX_ prefixed are simple types, other derived from the
133 //    latest 'lead' argument type in the order of encoding from first to last.
134 //    E_ANY - use prev lead type, E_CONSTPTR_ANY - make const pointer out of
135 //    prev lead type, etc. see ParamIterator::getNextParam() for details.
136
137 static constexpr ManglingRule manglingRules[] = {
138 { "", {0}, {0} },
139 { "abs"                             , {1},   {E_ANY}},
140 { "abs_diff"                        , {1},   {E_ANY,E_COPY}},
141 { "acos"                            , {1},   {E_ANY}},
142 { "acosh"                           , {1},   {E_ANY}},
143 { "acospi"                          , {1},   {E_ANY}},
144 { "add_sat"                         , {1},   {E_ANY,E_COPY}},
145 { "all"                             , {1},   {E_ANY}},
146 { "any"                             , {1},   {E_ANY}},
147 { "asin"                            , {1},   {E_ANY}},
148 { "asinh"                           , {1},   {E_ANY}},
149 { "asinpi"                          , {1},   {E_ANY}},
150 { "async_work_group_copy"           , {1},   {E_ANY,E_CONSTPTR_SWAPGL,EX_SIZET,EX_EVENT}},
151 { "async_work_group_strided_copy"   , {1},   {E_ANY,E_CONSTPTR_SWAPGL,EX_SIZET,EX_SIZET,EX_EVENT}},
152 { "atan"                            , {1},   {E_ANY}},
153 { "atan2"                           , {1},   {E_ANY,E_COPY}},
154 { "atan2pi"                         , {1},   {E_ANY,E_COPY}},
155 { "atanh"                           , {1},   {E_ANY}},
156 { "atanpi"                          , {1},   {E_ANY}},
157 { "atomic_add"                      , {1},   {E_VLTLPTR_ANY,E_POINTEE}},
158 { "atomic_and"                      , {1},   {E_VLTLPTR_ANY,E_POINTEE}},
159 { "atomic_cmpxchg"                  , {1},   {E_VLTLPTR_ANY,E_POINTEE,E_POINTEE}},
160 { "atomic_dec"                      , {1},   {E_VLTLPTR_ANY}},
161 { "atomic_inc"                      , {1},   {E_VLTLPTR_ANY}},
162 { "atomic_max"                      , {1},   {E_VLTLPTR_ANY,E_POINTEE}},
163 { "atomic_min"                      , {1},   {E_VLTLPTR_ANY,E_POINTEE}},
164 { "atomic_or"                       , {1},   {E_VLTLPTR_ANY,E_POINTEE}},
165 { "atomic_sub"                      , {1},   {E_VLTLPTR_ANY,E_POINTEE}},
166 { "atomic_xchg"                     , {1},   {E_VLTLPTR_ANY,E_POINTEE}},
167 { "atomic_xor"                      , {1},   {E_VLTLPTR_ANY,E_POINTEE}},
168 { "bitselect"                       , {1},   {E_ANY,E_COPY,E_COPY}},
169 { "cbrt"                            , {1},   {E_ANY}},
170 { "ceil"                            , {1},   {E_ANY}},
171 { "clamp"                           , {1},   {E_ANY,E_COPY,E_COPY}},
172 { "clz"                             , {1},   {E_ANY}},
173 { "commit_read_pipe"                , {1},   {E_ANY,EX_RESERVEDID}},
174 { "commit_write_pipe"               , {1},   {E_ANY,EX_RESERVEDID}},
175 { "copysign"                        , {1},   {E_ANY,E_COPY}},
176 { "cos"                             , {1},   {E_ANY}},
177 { "cosh"                            , {1},   {E_ANY}},
178 { "cospi"                           , {1},   {E_ANY}},
179 { "cross"                           , {1},   {E_ANY,E_COPY}},
180 { "ctz"                             , {1},   {E_ANY}},
181 { "degrees"                         , {1},   {E_ANY}},
182 { "distance"                        , {1},   {E_ANY,E_COPY}},
183 { "divide"                          , {1},   {E_ANY,E_COPY}},
184 { "dot"                             , {1},   {E_ANY,E_COPY}},
185 { "erf"                             , {1},   {E_ANY}},
186 { "erfc"                            , {1},   {E_ANY}},
187 { "exp"                             , {1},   {E_ANY}},
188 { "exp10"                           , {1},   {E_ANY}},
189 { "exp2"                            , {1},   {E_ANY}},
190 { "expm1"                           , {1},   {E_ANY}},
191 { "fabs"                            , {1},   {E_ANY}},
192 { "fast_distance"                   , {1},   {E_ANY,E_COPY}},
193 { "fast_length"                     , {1},   {E_ANY}},
194 { "fast_normalize"                  , {1},   {E_ANY}},
195 { "fdim"                            , {1},   {E_ANY,E_COPY}},
196 { "floor"                           , {1},   {E_ANY}},
197 { "fma"                             , {1},   {E_ANY,E_COPY,E_COPY}},
198 { "fmax"                            , {1},   {E_ANY,E_COPY}},
199 { "fmin"                            , {1},   {E_ANY,E_COPY}},
200 { "fmod"                            , {1},   {E_ANY,E_COPY}},
201 { "fract"                           , {2},   {E_POINTEE,E_ANY}},
202 { "frexp"                           , {1,2}, {E_ANY,E_ANY}},
203 { "get_image_array_size"            , {1},   {E_ANY}},
204 { "get_image_channel_data_type"     , {1},   {E_ANY}},
205 { "get_image_channel_order"         , {1},   {E_ANY}},
206 { "get_image_dim"                   , {1},   {E_ANY}},
207 { "get_image_height"                , {1},   {E_ANY}},
208 { "get_image_width"                 , {1},   {E_ANY}},
209 { "get_pipe_max_packets"            , {1},   {E_ANY}},
210 { "get_pipe_num_packets"            , {1},   {E_ANY}},
211 { "hadd"                            , {1},   {E_ANY,E_COPY}},
212 { "hypot"                           , {1},   {E_ANY,E_COPY}},
213 { "ilogb"                           , {1},   {E_ANY}},
214 { "isequal"                         , {1},   {E_ANY,E_COPY}},
215 { "isfinite"                        , {1},   {E_ANY}},
216 { "isgreater"                       , {1},   {E_ANY,E_COPY}},
217 { "isgreaterequal"                  , {1},   {E_ANY,E_COPY}},
218 { "isinf"                           , {1},   {E_ANY}},
219 { "isless"                          , {1},   {E_ANY,E_COPY}},
220 { "islessequal"                     , {1},   {E_ANY,E_COPY}},
221 { "islessgreater"                   , {1},   {E_ANY,E_COPY}},
222 { "isnan"                           , {1},   {E_ANY}},
223 { "isnormal"                        , {1},   {E_ANY}},
224 { "isnotequal"                      , {1},   {E_ANY,E_COPY}},
225 { "isordered"                       , {1},   {E_ANY,E_COPY}},
226 { "isunordered"                     , {1},   {E_ANY,E_COPY}},
227 { "ldexp"                           , {1},   {E_ANY,E_SETBASE_I32}},
228 { "length"                          , {1},   {E_ANY}},
229 { "lgamma"                          , {1},   {E_ANY}},
230 { "lgamma_r"                        , {1,2}, {E_ANY,E_ANY}},
231 { "log"                             , {1},   {E_ANY}},
232 { "log10"                           , {1},   {E_ANY}},
233 { "log1p"                           , {1},   {E_ANY}},
234 { "log2"                            , {1},   {E_ANY}},
235 { "logb"                            , {1},   {E_ANY}},
236 { "mad"                             , {1},   {E_ANY,E_COPY,E_COPY}},
237 { "mad24"                           , {1},   {E_ANY,E_COPY,E_COPY}},
238 { "mad_hi"                          , {1},   {E_ANY,E_COPY,E_COPY}},
239 { "mad_sat"                         , {1},   {E_ANY,E_COPY,E_COPY}},
240 { "max"                             , {1},   {E_ANY,E_COPY}},
241 { "maxmag"                          , {1},   {E_ANY,E_COPY}},
242 { "min"                             , {1},   {E_ANY,E_COPY}},
243 { "minmag"                          , {1},   {E_ANY,E_COPY}},
244 { "mix"                             , {1},   {E_ANY,E_COPY,E_COPY}},
245 { "modf"                            , {2},   {E_POINTEE,E_ANY}},
246 { "mul24"                           , {1},   {E_ANY,E_COPY}},
247 { "mul_hi"                          , {1},   {E_ANY,E_COPY}},
248 { "nan"                             , {1},   {E_ANY}},
249 { "nextafter"                       , {1},   {E_ANY,E_COPY}},
250 { "normalize"                       , {1},   {E_ANY}},
251 { "popcount"                        , {1},   {E_ANY}},
252 { "pow"                             , {1},   {E_ANY,E_COPY}},
253 { "pown"                            , {1},   {E_ANY,E_SETBASE_I32}},
254 { "powr"                            , {1},   {E_ANY,E_COPY}},
255 { "prefetch"                        , {1},   {E_CONSTPTR_ANY,EX_SIZET}},
256 { "radians"                         , {1},   {E_ANY}},
257 { "recip"                           , {1},   {E_ANY}},
258 { "remainder"                       , {1},   {E_ANY,E_COPY}},
259 { "remquo"                          , {1,3}, {E_ANY,E_COPY,E_ANY}},
260 { "reserve_read_pipe"               , {1},   {E_ANY,EX_UINT}},
261 { "reserve_write_pipe"              , {1},   {E_ANY,EX_UINT}},
262 { "rhadd"                           , {1},   {E_ANY,E_COPY}},
263 { "rint"                            , {1},   {E_ANY}},
264 { "rootn"                           , {1},   {E_ANY,E_SETBASE_I32}},
265 { "rotate"                          , {1},   {E_ANY,E_COPY}},
266 { "round"                           , {1},   {E_ANY}},
267 { "rsqrt"                           , {1},   {E_ANY}},
268 { "select"                          , {1,3}, {E_ANY,E_COPY,E_ANY}},
269 { "shuffle"                         , {1,2}, {E_ANY,E_ANY}},
270 { "shuffle2"                        , {1,3}, {E_ANY,E_COPY,E_ANY}},
271 { "sign"                            , {1},   {E_ANY}},
272 { "signbit"                         , {1},   {E_ANY}},
273 { "sin"                             , {1},   {E_ANY}},
274 { "sincos"                          , {2},   {E_POINTEE,E_ANY}},
275 { "sinh"                            , {1},   {E_ANY}},
276 { "sinpi"                           , {1},   {E_ANY}},
277 { "smoothstep"                      , {1},   {E_ANY,E_COPY,E_COPY}},
278 { "sqrt"                            , {1},   {E_ANY}},
279 { "step"                            , {1},   {E_ANY,E_COPY}},
280 { "sub_group_broadcast"             , {1},   {E_ANY,EX_UINT}},
281 { "sub_group_commit_read_pipe"      , {1},   {E_ANY,EX_RESERVEDID}},
282 { "sub_group_commit_write_pipe"     , {1},   {E_ANY,EX_RESERVEDID}},
283 { "sub_group_reduce_add"            , {1},   {E_ANY}},
284 { "sub_group_reduce_max"            , {1},   {E_ANY}},
285 { "sub_group_reduce_min"            , {1},   {E_ANY}},
286 { "sub_group_reserve_read_pipe"     , {1},   {E_ANY,EX_UINT}},
287 { "sub_group_reserve_write_pipe"    , {1},   {E_ANY,EX_UINT}},
288 { "sub_group_scan_exclusive_add"    , {1},   {E_ANY}},
289 { "sub_group_scan_exclusive_max"    , {1},   {E_ANY}},
290 { "sub_group_scan_exclusive_min"    , {1},   {E_ANY}},
291 { "sub_group_scan_inclusive_add"    , {1},   {E_ANY}},
292 { "sub_group_scan_inclusive_max"    , {1},   {E_ANY}},
293 { "sub_group_scan_inclusive_min"    , {1},   {E_ANY}},
294 { "sub_sat"                         , {1},   {E_ANY,E_COPY}},
295 { "tan"                             , {1},   {E_ANY}},
296 { "tanh"                            , {1},   {E_ANY}},
297 { "tanpi"                           , {1},   {E_ANY}},
298 { "tgamma"                          , {1},   {E_ANY}},
299 { "trunc"                           , {1},   {E_ANY}},
300 { "upsample"                        , {1},   {E_ANY,E_MAKEBASE_UNS}},
301 { "vec_step"                        , {1},   {E_ANY}},
302 { "vstore"                          , {3},   {E_POINTEE,EX_SIZET,E_ANY}},
303 { "vstore16"                        , {3},   {E_V16_OF_POINTEE,EX_SIZET,E_ANY}},
304 { "vstore2"                         , {3},   {E_V2_OF_POINTEE,EX_SIZET,E_ANY}},
305 { "vstore3"                         , {3},   {E_V3_OF_POINTEE,EX_SIZET,E_ANY}},
306 { "vstore4"                         , {3},   {E_V4_OF_POINTEE,EX_SIZET,E_ANY}},
307 { "vstore8"                         , {3},   {E_V8_OF_POINTEE,EX_SIZET,E_ANY}},
308 { "work_group_commit_read_pipe"     , {1},   {E_ANY,EX_RESERVEDID}},
309 { "work_group_commit_write_pipe"    , {1},   {E_ANY,EX_RESERVEDID}},
310 { "work_group_reduce_add"           , {1},   {E_ANY}},
311 { "work_group_reduce_max"           , {1},   {E_ANY}},
312 { "work_group_reduce_min"           , {1},   {E_ANY}},
313 { "work_group_reserve_read_pipe"    , {1},   {E_ANY,EX_UINT}},
314 { "work_group_reserve_write_pipe"   , {1},   {E_ANY,EX_UINT}},
315 { "work_group_scan_exclusive_add"   , {1},   {E_ANY}},
316 { "work_group_scan_exclusive_max"   , {1},   {E_ANY}},
317 { "work_group_scan_exclusive_min"   , {1},   {E_ANY}},
318 { "work_group_scan_inclusive_add"   , {1},   {E_ANY}},
319 { "work_group_scan_inclusive_max"   , {1},   {E_ANY}},
320 { "work_group_scan_inclusive_min"   , {1},   {E_ANY}},
321 { "write_imagef"                    , {1},   {E_ANY,E_IMAGECOORDS,EX_FLOAT4}},
322 { "write_imagei"                    , {1},   {E_ANY,E_IMAGECOORDS,EX_INTV4}},
323 { "write_imageui"                   , {1},   {E_ANY,E_IMAGECOORDS,EX_UINTV4}},
324 { "ncos"                            , {1},   {E_ANY} },
325 { "nexp2"                           , {1},   {E_ANY} },
326 { "nfma"                            , {1},   {E_ANY, E_COPY, E_COPY} },
327 { "nlog2"                           , {1},   {E_ANY} },
328 { "nrcp"                            , {1},   {E_ANY} },
329 { "nrsqrt"                          , {1},   {E_ANY} },
330 { "nsin"                            , {1},   {E_ANY} },
331 { "nsqrt"                           , {1},   {E_ANY} },
332 { "ftz"                             , {1},   {E_ANY} },
333 { "fldexp"                          , {1},   {E_ANY, EX_UINT} },
334 { "class"                           , {1},   {E_ANY, EX_UINT} },
335 { "rcbrt"                           , {1},   {E_ANY} },
336 };
337
338 // Library functions with unmangled name.
339 const UnmangledFuncInfo UnmangledFuncInfo::Table[] = {
340     {"__read_pipe_2", 4},
341     {"__read_pipe_4", 6},
342     {"__write_pipe_2", 4},
343     {"__write_pipe_4", 6},
344 };
345
346 const unsigned UnmangledFuncInfo::TableSize =
347     array_lengthof(UnmangledFuncInfo::Table);
348
349 static AMDGPULibFunc::Param getRetType(AMDGPULibFunc::EFuncId id,
350                                        const AMDGPULibFunc::Param (&Leads)[2]) {
351   AMDGPULibFunc::Param Res = Leads[0];
352   // TBD - This switch may require to be extended for other intriniscs
353   switch (id) {
354   case AMDGPULibFunc::EI_SINCOS:
355     Res.PtrKind = AMDGPULibFunc::BYVALUE;
356     break;
357   default:
358     break;
359   }
360   return Res;
361 }
362
363 class ParamIterator {
364   const AMDGPULibFunc::Param (&Leads)[2];
365   const ManglingRule& Rule;
366   int Index;
367 public:
368   ParamIterator(const AMDGPULibFunc::Param (&leads)[2],
369                 const ManglingRule& rule)
370     : Leads(leads), Rule(rule), Index(0) {}
371
372   AMDGPULibFunc::Param getNextParam();
373 };
374
375 AMDGPULibFunc::Param ParamIterator::getNextParam() {
376   AMDGPULibFunc::Param P;
377   if (Index >= int(sizeof Rule.Param/sizeof Rule.Param[0])) return P;
378
379   const char R = Rule.Param[Index];
380   switch (R) {
381   case E_NONE:     break;
382   case EX_UINT:
383     P.ArgType = AMDGPULibFunc::U32; break;
384   case EX_INTV4:
385     P.ArgType = AMDGPULibFunc::I32; P.VectorSize = 4; break;
386   case EX_UINTV4:
387     P.ArgType = AMDGPULibFunc::U32; P.VectorSize = 4; break;
388   case EX_FLOAT4:
389     P.ArgType = AMDGPULibFunc::F32; P.VectorSize = 4; break;
390   case EX_SIZET:
391     P.ArgType = AMDGPULibFunc::U64; break;
392   case EX_EVENT:
393     P.ArgType = AMDGPULibFunc::EVENT;   break;
394   case EX_SAMPLER:
395     P.ArgType = AMDGPULibFunc::SAMPLER; break;
396   case EX_RESERVEDID: break; // TBD
397   default:
398     if (Index == (Rule.Lead[1] - 1)) P = Leads[1];
399     else P = Leads[0];
400
401     switch (R) {
402     case E_ANY:
403     case E_COPY: break;
404
405     case E_POINTEE:
406       P.PtrKind = AMDGPULibFunc::BYVALUE; break;
407     case E_V2_OF_POINTEE:
408       P.VectorSize = 2; P.PtrKind = AMDGPULibFunc::BYVALUE; break;
409     case E_V3_OF_POINTEE:
410       P.VectorSize = 3; P.PtrKind = AMDGPULibFunc::BYVALUE; break;
411     case E_V4_OF_POINTEE:
412       P.VectorSize = 4; P.PtrKind = AMDGPULibFunc::BYVALUE; break;
413     case E_V8_OF_POINTEE:
414       P.VectorSize = 8; P.PtrKind = AMDGPULibFunc::BYVALUE; break;
415     case E_V16_OF_POINTEE:
416       P.VectorSize = 16; P.PtrKind = AMDGPULibFunc::BYVALUE; break;
417     case E_CONSTPTR_ANY:
418       P.PtrKind |= AMDGPULibFunc::CONST; break;
419     case E_VLTLPTR_ANY:
420       P.PtrKind |= AMDGPULibFunc::VOLATILE; break;
421     case E_SETBASE_I32:
422       P.ArgType = AMDGPULibFunc::I32; break;
423     case E_SETBASE_U32:
424       P.ArgType = AMDGPULibFunc::U32; break;
425
426     case E_MAKEBASE_UNS:
427       P.ArgType &= ~AMDGPULibFunc::BASE_TYPE_MASK;
428       P.ArgType |= AMDGPULibFunc::UINT;
429       break;
430
431     case E_IMAGECOORDS:
432       switch (P.ArgType) {
433       case AMDGPULibFunc::IMG1DA: P.VectorSize = 2; break;
434       case AMDGPULibFunc::IMG1DB: P.VectorSize = 1; break;
435       case AMDGPULibFunc::IMG2DA: P.VectorSize = 4; break;
436       case AMDGPULibFunc::IMG1D:  P.VectorSize = 1; break;
437       case AMDGPULibFunc::IMG2D:  P.VectorSize = 2; break;
438       case AMDGPULibFunc::IMG3D:  P.VectorSize = 4; break;
439       }
440       P.PtrKind = AMDGPULibFunc::BYVALUE;
441       P.ArgType = AMDGPULibFunc::I32;
442       break;
443
444     case E_CONSTPTR_SWAPGL: {
445       unsigned AS = AMDGPULibFunc::getAddrSpaceFromEPtrKind(P.PtrKind);
446       switch (AS) {
447       case AMDGPUAS::GLOBAL_ADDRESS: AS = AMDGPUAS::LOCAL_ADDRESS; break;
448       case AMDGPUAS::LOCAL_ADDRESS:  AS = AMDGPUAS::GLOBAL_ADDRESS; break;
449       }
450       P.PtrKind = AMDGPULibFunc::getEPtrKindFromAddrSpace(AS);
451       P.PtrKind |= AMDGPULibFunc::CONST;
452       break;
453     }
454
455     default: llvm_unreachable("Unhandeled param rule");
456     }
457   }
458   ++Index;
459   return P;
460 }
461
462 inline static void drop_front(StringRef& str, size_t n = 1) {
463   str = str.drop_front(n);
464 }
465
466 static bool eatTerm(StringRef& mangledName, const char c) {
467   if (mangledName.front() == c) {
468     drop_front(mangledName);
469     return true;
470   }
471   return false;
472 }
473
474 template <size_t N>
475 static bool eatTerm(StringRef& mangledName, const char (&str)[N]) {
476   if (mangledName.startswith(StringRef(str, N-1))) {
477     drop_front(mangledName, N-1);
478     return true;
479   }
480   return false;
481 }
482
483 static int eatNumber(StringRef& s) {
484   size_t const savedSize = s.size();
485   int n = 0;
486   while (!s.empty() && isDigit(s.front())) {
487     n = n*10 + s.front() - '0';
488     drop_front(s);
489   }
490   return s.size() < savedSize ? n : -1;
491 }
492
493 static StringRef eatLengthPrefixedName(StringRef& mangledName) {
494   int const Len = eatNumber(mangledName);
495   if (Len <= 0 || static_cast<size_t>(Len) > mangledName.size())
496     return StringRef();
497   StringRef Res = mangledName.substr(0, Len);
498   drop_front(mangledName, Len);
499   return Res;
500 }
501
502 } // end anonymous namespace
503
504 AMDGPUMangledLibFunc::AMDGPUMangledLibFunc() {
505   FuncId = EI_NONE;
506   FKind = NOPFX;
507   Leads[0].reset();
508   Leads[1].reset();
509   Name.clear();
510 }
511
512 AMDGPUUnmangledLibFunc::AMDGPUUnmangledLibFunc() {
513   FuncId = EI_NONE;
514   FuncTy = nullptr;
515 }
516
517 AMDGPUMangledLibFunc::AMDGPUMangledLibFunc(
518     EFuncId id, const AMDGPUMangledLibFunc &copyFrom) {
519   FuncId = id;
520   FKind = copyFrom.FKind;
521   Leads[0] = copyFrom.Leads[0];
522   Leads[1] = copyFrom.Leads[1];
523 }
524
525 ///////////////////////////////////////////////////////////////////////////////
526 // Demangling
527
528 static int parseVecSize(StringRef& mangledName) {
529   size_t const Len = eatNumber(mangledName);
530   switch (Len) {
531   case 2: case 3: case 4: case 8: case 16:
532     return Len;
533   default:
534     break;
535   }
536   return 1;
537 }
538
539 static AMDGPULibFunc::ENamePrefix parseNamePrefix(StringRef& mangledName) {
540   std::pair<StringRef, StringRef> const P = mangledName.split('_');
541   AMDGPULibFunc::ENamePrefix Pfx =
542     StringSwitch<AMDGPULibFunc::ENamePrefix>(P.first)
543     .Case("native", AMDGPULibFunc::NATIVE)
544     .Case("half"  , AMDGPULibFunc::HALF)
545     .Default(AMDGPULibFunc::NOPFX);
546
547   if (Pfx != AMDGPULibFunc::NOPFX)
548     mangledName = P.second;
549
550   return Pfx;
551 }
552
553 StringMap<int> ManglingRule::buildManglingRulesMap() {
554   StringMap<int> Map(array_lengthof(manglingRules));
555   int Id = 0;
556   for (auto Rule : manglingRules)
557     Map.insert({Rule.Name, Id++});
558   return Map;
559 }
560
561 bool AMDGPUMangledLibFunc::parseUnmangledName(StringRef FullName) {
562   static const StringMap<int> manglingRulesMap =
563       ManglingRule::buildManglingRulesMap();
564   FuncId = static_cast<EFuncId>(manglingRulesMap.lookup(FullName));
565   return FuncId != EI_NONE;
566 }
567
568 ///////////////////////////////////////////////////////////////////////////////
569 // Itanium Demangling
570
571 namespace {
572 struct ItaniumParamParser {
573   AMDGPULibFunc::Param Prev;
574   bool parseItaniumParam(StringRef& param, AMDGPULibFunc::Param &res);
575 };
576 } // namespace
577
578 bool ItaniumParamParser::parseItaniumParam(StringRef& param,
579                                            AMDGPULibFunc::Param &res) {
580   res.reset();
581   if (param.empty()) return false;
582
583   // parse pointer prefix
584   if (eatTerm(param, 'P')) {
585     if (eatTerm(param, 'K')) res.PtrKind |= AMDGPULibFunc::CONST;
586     if (eatTerm(param, 'V')) res.PtrKind |= AMDGPULibFunc::VOLATILE;
587     unsigned AS;
588     if (!eatTerm(param, "U3AS")) {
589       AS = 0;
590     } else {
591       AS = param.front() - '0';
592       drop_front(param, 1);
593     }
594     res.PtrKind |= AMDGPULibFuncBase::getEPtrKindFromAddrSpace(AS);
595   } else {
596     res.PtrKind = AMDGPULibFunc::BYVALUE;
597   }
598
599   // parse vector size
600   if (eatTerm(param,"Dv")) {
601     res.VectorSize = parseVecSize(param);
602     if (res.VectorSize==1 || !eatTerm(param, '_')) return false;
603   }
604
605   // parse type
606   char const TC = param.front();
607   if (isDigit(TC)) {
608     res.ArgType = StringSwitch<AMDGPULibFunc::EType>
609       (eatLengthPrefixedName(param))
610       .Case("ocl_image1darray" , AMDGPULibFunc::IMG1DA)
611       .Case("ocl_image1dbuffer", AMDGPULibFunc::IMG1DB)
612       .Case("ocl_image2darray" , AMDGPULibFunc::IMG2DA)
613       .Case("ocl_image1d"      , AMDGPULibFunc::IMG1D)
614       .Case("ocl_image2d"      , AMDGPULibFunc::IMG2D)
615       .Case("ocl_image3d"      , AMDGPULibFunc::IMG3D)
616       .Case("ocl_event"        , AMDGPULibFunc::DUMMY)
617       .Case("ocl_sampler"      , AMDGPULibFunc::DUMMY)
618       .Default(AMDGPULibFunc::DUMMY);
619   } else {
620     drop_front(param);
621     switch (TC) {
622     case 'h': res.ArgType =  AMDGPULibFunc::U8; break;
623     case 't': res.ArgType = AMDGPULibFunc::U16; break;
624     case 'j': res.ArgType = AMDGPULibFunc::U32; break;
625     case 'm': res.ArgType = AMDGPULibFunc::U64; break;
626     case 'c': res.ArgType =  AMDGPULibFunc::I8; break;
627     case 's': res.ArgType = AMDGPULibFunc::I16; break;
628     case 'i': res.ArgType = AMDGPULibFunc::I32; break;
629     case 'l': res.ArgType = AMDGPULibFunc::I64; break;
630     case 'f': res.ArgType = AMDGPULibFunc::F32; break;
631     case 'd': res.ArgType = AMDGPULibFunc::F64; break;
632     case 'D': if (!eatTerm(param, 'h')) return false;
633               res.ArgType = AMDGPULibFunc::F16; break;
634     case 'S':
635       if (!eatTerm(param, '_')) {
636         eatNumber(param);
637         if (!eatTerm(param, '_')) return false;
638       }
639       res.VectorSize = Prev.VectorSize;
640       res.ArgType    = Prev.ArgType;
641       break;
642     default:;
643     }
644   }
645   if (res.ArgType == 0) return false;
646   Prev.VectorSize = res.VectorSize;
647   Prev.ArgType    = res.ArgType;
648   return true;
649 }
650
651 bool AMDGPUMangledLibFunc::parseFuncName(StringRef &mangledName) {
652   StringRef Name = eatLengthPrefixedName(mangledName);
653   FKind = parseNamePrefix(Name);
654   if (!parseUnmangledName(Name))
655     return false;
656
657   const ManglingRule& Rule = manglingRules[FuncId];
658   ItaniumParamParser Parser;
659   for (int I=0; I < Rule.maxLeadIndex(); ++I) {
660     Param P;
661     if (!Parser.parseItaniumParam(mangledName, P))
662       return false;
663
664     if ((I + 1) == Rule.Lead[0]) Leads[0] = P;
665     if ((I + 1) == Rule.Lead[1]) Leads[1] = P;
666   }
667   return true;
668 }
669
670 bool AMDGPUUnmangledLibFunc::parseFuncName(StringRef &Name) {
671   if (!UnmangledFuncInfo::lookup(Name, FuncId))
672     return false;
673   setName(Name);
674   return true;
675 }
676
677 bool AMDGPULibFunc::parse(StringRef FuncName, AMDGPULibFunc &F) {
678   if (FuncName.empty()) {
679     F.Impl = std::unique_ptr<AMDGPULibFuncImpl>();
680     return false;
681   }
682
683   if (eatTerm(FuncName, "_Z"))
684     F.Impl = std::make_unique<AMDGPUMangledLibFunc>();
685   else
686     F.Impl = std::make_unique<AMDGPUUnmangledLibFunc>();
687   if (F.Impl->parseFuncName(FuncName))
688     return true;
689
690   F.Impl = std::unique_ptr<AMDGPULibFuncImpl>();
691   return false;
692 }
693
694 StringRef AMDGPUMangledLibFunc::getUnmangledName(StringRef mangledName) {
695   StringRef S = mangledName;
696   if (eatTerm(S, "_Z"))
697     return eatLengthPrefixedName(S);
698   return StringRef();
699 }
700
701 ///////////////////////////////////////////////////////////////////////////////
702 // Mangling
703
704 template <typename Stream>
705 void AMDGPUMangledLibFunc::writeName(Stream &OS) const {
706   const char *Pfx = "";
707   switch (FKind) {
708   case NATIVE: Pfx = "native_"; break;
709   case HALF:   Pfx = "half_";   break;
710   default: break;
711   }
712   if (!Name.empty()) {
713     OS << Pfx << Name;
714   } else if (FuncId != EI_NONE) {
715     OS << Pfx;
716     const StringRef& S = manglingRules[FuncId].Name;
717     OS.write(S.data(), S.size());
718   }
719 }
720
721 std::string AMDGPUMangledLibFunc::mangle() const { return mangleNameItanium(); }
722
723 ///////////////////////////////////////////////////////////////////////////////
724 // Itanium Mangling
725
726 static const char *getItaniumTypeName(AMDGPULibFunc::EType T) {
727   switch (T) {
728   case AMDGPULibFunc::U8:      return "h";
729   case AMDGPULibFunc::U16:     return "t";
730   case AMDGPULibFunc::U32:     return "j";
731   case AMDGPULibFunc::U64:     return "m";
732   case AMDGPULibFunc::I8:      return "c";
733   case AMDGPULibFunc::I16:     return "s";
734   case AMDGPULibFunc::I32:     return "i";
735   case AMDGPULibFunc::I64:     return "l";
736   case AMDGPULibFunc::F16:     return "Dh";
737   case AMDGPULibFunc::F32:     return "f";
738   case AMDGPULibFunc::F64:     return "d";
739   case AMDGPULibFunc::IMG1DA:  return "16ocl_image1darray";
740   case AMDGPULibFunc::IMG1DB:  return "17ocl_image1dbuffer";
741   case AMDGPULibFunc::IMG2DA:  return "16ocl_image2darray";
742   case AMDGPULibFunc::IMG1D:   return "11ocl_image1d";
743   case AMDGPULibFunc::IMG2D:   return "11ocl_image2d";
744   case AMDGPULibFunc::IMG3D:   return "11ocl_image3d";
745   case AMDGPULibFunc::SAMPLER: return "11ocl_sampler";
746   case AMDGPULibFunc::EVENT:   return "9ocl_event";
747   default: llvm_unreachable("Unhandeled param type");
748   }
749   return nullptr;
750 }
751
752 namespace {
753 // Itanium mangling ABI says:
754 // "5.1.8. Compression
755 // ... Each non-terminal in the grammar for which <substitution> appears on the
756 // right-hand side is both a source of future substitutions and a candidate
757 // for being substituted. There are two exceptions that appear to be
758 // substitution candidates from the grammar, but are explicitly excluded:
759 // 1. <builtin-type> other than vendor extended types ..."
760
761 // For the purpose of functions the following productions make sence for the
762 // substitution:
763 //  <type> ::= <builtin-type>
764 //    ::= <class-enum-type>
765 //    ::= <array-type>
766 //    ::=<CV-qualifiers> <type>
767 //    ::= P <type>                # pointer-to
768 //    ::= <substitution>
769 //
770 // Note that while types like images, samplers and events are by the ABI encoded
771 // using <class-enum-type> production rule they're not used for substitution
772 // because clang consider them as builtin types.
773 //
774 // DvNN_ type is GCC extension for vectors and is a subject for the substitution.
775
776
777 class ItaniumMangler {
778   SmallVector<AMDGPULibFunc::Param, 10> Str; // list of accumulated substituions
779   bool  UseAddrSpace;
780
781   int findSubst(const AMDGPULibFunc::Param& P) const {
782     for(unsigned I = 0; I < Str.size(); ++I) {
783       const AMDGPULibFunc::Param& T = Str[I];
784       if (P.PtrKind    == T.PtrKind &&
785           P.VectorSize == T.VectorSize &&
786           P.ArgType    == T.ArgType) {
787         return I;
788       }
789     }
790     return -1;
791   }
792
793   template <typename Stream>
794   bool trySubst(Stream& os, const AMDGPULibFunc::Param& p) {
795     int const subst = findSubst(p);
796     if (subst < 0) return false;
797     // Substitutions are mangled as S(XX)?_ where XX is a hexadecimal number
798     // 0   1    2
799     // S_  S0_  S1_
800     if (subst == 0) os << "S_";
801     else os << 'S' << (subst-1) << '_';
802     return true;
803   }
804
805 public:
806   ItaniumMangler(bool useAddrSpace)
807     : UseAddrSpace(useAddrSpace) {}
808
809   template <typename Stream>
810   void operator()(Stream& os, AMDGPULibFunc::Param p) {
811
812     // Itanium mangling ABI 5.1.8. Compression:
813     // Logically, the substitutable components of a mangled name are considered
814     // left-to-right, components before the composite structure of which they
815     // are a part. If a component has been encountered before, it is substituted
816     // as described below. This decision is independent of whether its components
817     // have been substituted, so an implementation may optimize by considering
818     // large structures for substitution before their components. If a component
819     // has not been encountered before, its mangling is identified, and it is
820     // added to a dictionary of substitution candidates. No entity is added to
821     // the dictionary twice.
822     AMDGPULibFunc::Param Ptr;
823
824     if (p.PtrKind) {
825       if (trySubst(os, p)) return;
826       os << 'P';
827       if (p.PtrKind & AMDGPULibFunc::CONST) os << 'K';
828       if (p.PtrKind & AMDGPULibFunc::VOLATILE) os << 'V';
829       unsigned AS = UseAddrSpace
830                         ? AMDGPULibFuncBase::getAddrSpaceFromEPtrKind(p.PtrKind)
831                         : 0;
832       if (AS != 0) os << "U3AS" << AS;
833       Ptr = p;
834       p.PtrKind = 0;
835     }
836
837     if (p.VectorSize > 1) {
838       if (trySubst(os, p)) goto exit;
839       Str.push_back(p);
840       os << "Dv" << static_cast<unsigned>(p.VectorSize) << '_';
841     }
842
843     os << getItaniumTypeName((AMDGPULibFunc::EType)p.ArgType);
844
845   exit:
846     if (Ptr.ArgType) Str.push_back(Ptr);
847   }
848 };
849 } // namespace
850
851 std::string AMDGPUMangledLibFunc::mangleNameItanium() const {
852   SmallString<128> Buf;
853   raw_svector_ostream S(Buf);
854   SmallString<128> NameBuf;
855   raw_svector_ostream Name(NameBuf);
856   writeName(Name);
857   const StringRef& NameStr = Name.str();
858   S << "_Z" << static_cast<int>(NameStr.size()) << NameStr;
859
860   ItaniumMangler Mangler(true);
861   ParamIterator I(Leads, manglingRules[FuncId]);
862   Param P;
863   while ((P = I.getNextParam()).ArgType != 0)
864     Mangler(S, P);
865   return std::string(S.str());
866 }
867
868 ///////////////////////////////////////////////////////////////////////////////
869 // Misc
870
871 static Type* getIntrinsicParamType(
872   LLVMContext& C,
873   const AMDGPULibFunc::Param& P,
874   bool useAddrSpace) {
875   Type* T = nullptr;
876   switch (P.ArgType) {
877   case AMDGPULibFunc::U8:
878   case AMDGPULibFunc::I8:   T = Type::getInt8Ty(C);   break;
879   case AMDGPULibFunc::U16:
880   case AMDGPULibFunc::I16:  T = Type::getInt16Ty(C);  break;
881   case AMDGPULibFunc::U32:
882   case AMDGPULibFunc::I32:  T = Type::getInt32Ty(C);  break;
883   case AMDGPULibFunc::U64:
884   case AMDGPULibFunc::I64:  T = Type::getInt64Ty(C);  break;
885   case AMDGPULibFunc::F16:  T = Type::getHalfTy(C);   break;
886   case AMDGPULibFunc::F32:  T = Type::getFloatTy(C);  break;
887   case AMDGPULibFunc::F64:  T = Type::getDoubleTy(C); break;
888
889   case AMDGPULibFunc::IMG1DA:
890   case AMDGPULibFunc::IMG1DB:
891   case AMDGPULibFunc::IMG2DA:
892   case AMDGPULibFunc::IMG1D:
893   case AMDGPULibFunc::IMG2D:
894   case AMDGPULibFunc::IMG3D:
895     T = StructType::create(C,"ocl_image")->getPointerTo(); break;
896   case AMDGPULibFunc::SAMPLER:
897     T = StructType::create(C,"ocl_sampler")->getPointerTo(); break;
898   case AMDGPULibFunc::EVENT:
899     T = StructType::create(C,"ocl_event")->getPointerTo(); break;
900   default:
901     llvm_unreachable("Unhandeled param type");
902     return nullptr;
903   }
904   if (P.VectorSize > 1)
905     T = FixedVectorType::get(T, P.VectorSize);
906   if (P.PtrKind != AMDGPULibFunc::BYVALUE)
907     T = useAddrSpace ? T->getPointerTo((P.PtrKind & AMDGPULibFunc::ADDR_SPACE)
908                                        - 1)
909                      : T->getPointerTo();
910   return T;
911 }
912
913 FunctionType *AMDGPUMangledLibFunc::getFunctionType(Module &M) const {
914   LLVMContext& C = M.getContext();
915   std::vector<Type*> Args;
916   ParamIterator I(Leads, manglingRules[FuncId]);
917   Param P;
918   while ((P=I.getNextParam()).ArgType != 0)
919     Args.push_back(getIntrinsicParamType(C, P, true));
920
921   return FunctionType::get(
922     getIntrinsicParamType(C, getRetType(FuncId, Leads), true),
923     Args, false);
924 }
925
926 unsigned AMDGPUMangledLibFunc::getNumArgs() const {
927   return manglingRules[FuncId].getNumArgs();
928 }
929
930 unsigned AMDGPUUnmangledLibFunc::getNumArgs() const {
931   return UnmangledFuncInfo::getNumArgs(FuncId);
932 }
933
934 std::string AMDGPUMangledLibFunc::getName() const {
935   SmallString<128> Buf;
936   raw_svector_ostream OS(Buf);
937   writeName(OS);
938   return std::string(OS.str());
939 }
940
941 Function *AMDGPULibFunc::getFunction(Module *M, const AMDGPULibFunc &fInfo) {
942   std::string FuncName = fInfo.mangle();
943   Function *F = dyn_cast_or_null<Function>(
944     M->getValueSymbolTable().lookup(FuncName));
945
946   // check formal with actual types conformance
947   if (F && !F->isDeclaration()
948         && !F->isVarArg()
949         && F->arg_size() == fInfo.getNumArgs()) {
950     return F;
951   }
952   return nullptr;
953 }
954
955 FunctionCallee AMDGPULibFunc::getOrInsertFunction(Module *M,
956                                                   const AMDGPULibFunc &fInfo) {
957   std::string const FuncName = fInfo.mangle();
958   Function *F = dyn_cast_or_null<Function>(
959     M->getValueSymbolTable().lookup(FuncName));
960
961   // check formal with actual types conformance
962   if (F && !F->isDeclaration()
963         && !F->isVarArg()
964         && F->arg_size() == fInfo.getNumArgs()) {
965     return F;
966   }
967
968   FunctionType *FuncTy = fInfo.getFunctionType(*M);
969
970   bool hasPtr = false;
971   for (FunctionType::param_iterator
972          PI = FuncTy->param_begin(),
973          PE = FuncTy->param_end();
974        PI != PE; ++PI) {
975     const Type* argTy = static_cast<const Type*>(*PI);
976     if (argTy->isPointerTy()) {
977       hasPtr = true;
978       break;
979     }
980   }
981
982   FunctionCallee C;
983   if (hasPtr) {
984     // Do not set extra attributes for functions with pointer arguments.
985     C = M->getOrInsertFunction(FuncName, FuncTy);
986   } else {
987     AttributeList Attr;
988     LLVMContext &Ctx = M->getContext();
989     Attr = Attr.addAttribute(Ctx, AttributeList::FunctionIndex,
990                              Attribute::ReadOnly);
991     Attr = Attr.addAttribute(Ctx, AttributeList::FunctionIndex,
992                              Attribute::NoUnwind);
993     C = M->getOrInsertFunction(FuncName, FuncTy, Attr);
994   }
995
996   return C;
997 }
998
999 StringMap<unsigned> UnmangledFuncInfo::buildNameMap() {
1000   StringMap<unsigned> Map;
1001   for (unsigned I = 0; I != TableSize; ++I)
1002     Map[Table[I].Name] = I;
1003   return Map;
1004 }
1005
1006 bool UnmangledFuncInfo::lookup(StringRef Name, ID &Id) {
1007   static const StringMap<unsigned> Map = buildNameMap();
1008   auto Loc = Map.find(Name);
1009   if (Loc != Map.end()) {
1010     Id = toFuncId(Loc->second);
1011     return true;
1012   }
1013   Id = AMDGPULibFunc::EI_NONE;
1014   return false;
1015 }
1016
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));
1022   else
1023     Impl = std::unique_ptr<AMDGPULibFuncImpl>();
1024 }
1025
1026 AMDGPULibFunc &AMDGPULibFunc::operator=(const AMDGPULibFunc &F) {
1027   if (this == &F)
1028     return *this;
1029   new (this) AMDGPULibFunc(F);
1030   return *this;
1031 }
1032
1033 AMDGPULibFunc::AMDGPULibFunc(EFuncId Id, const AMDGPULibFunc &CopyFrom) {
1034   assert(AMDGPULibFuncBase::isMangled(Id) && CopyFrom.isMangled() &&
1035          "not supported");
1036   Impl.reset(new AMDGPUMangledLibFunc(
1037       Id, *cast<AMDGPUMangledLibFunc>(CopyFrom.Impl.get())));
1038 }
1039
1040 AMDGPULibFunc::AMDGPULibFunc(StringRef Name, FunctionType *FT) {
1041   Impl.reset(new AMDGPUUnmangledLibFunc(Name, FT));
1042 }
1043
1044 void AMDGPULibFunc::initMangled() { Impl.reset(new AMDGPUMangledLibFunc()); }
1045
1046 AMDGPULibFunc::Param *AMDGPULibFunc::getLeads() {
1047   if (!Impl)
1048     initMangled();
1049   return cast<AMDGPUMangledLibFunc>(Impl.get())->Leads;
1050 }
1051
1052 const AMDGPULibFunc::Param *AMDGPULibFunc::getLeads() const {
1053   return cast<const AMDGPUMangledLibFunc>(Impl.get())->Leads;
1054 }