LCOV - code coverage report
Current view: top level - lib/Target/AMDGPU - AMDGPULibFunc.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 273 421 64.8 %
Date: 2017-09-14 15:23:50 Functions: 42 45 93.3 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===-- AMDGPULibFunc.cpp -------------------------------------------------===//
       2             : //
       3             : //                     The LLVM Compiler Infrastructure
       4             : //
       5             : // This file is distributed under the University of Illinois Open Source
       6             : // License. See LICENSE.TXT for details.
       7             : //
       8             : //===----------------------------------------------------------------------===//
       9             : //
      10             : //  This file contains utility functions to work with Itanium mangled names
      11             : //
      12             : //===----------------------------------------------------------------------===//
      13             : 
      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>
      24             : #include <string>
      25             : 
      26             : using namespace llvm;
      27             : 
      28             : namespace {
      29             : 
      30             : enum EManglingParam {
      31             :     E_NONE,
      32             :     EX_EVENT,
      33             :     EX_FLOAT4,
      34             :     EX_INTV4,
      35             :     EX_RESERVEDID,
      36             :     EX_SAMPLER,
      37             :     EX_SIZET,
      38             :     EX_UINT,
      39             :     EX_UINTV4,
      40             :     E_ANY,
      41             :     E_CONSTPTR_ANY,
      42             :     E_CONSTPTR_SWAPGL,
      43             :     E_COPY,
      44             :     E_IMAGECOORDS,
      45             :     E_POINTEE,
      46             :     E_SETBASE_I32,
      47             :     E_SETBASE_U32,
      48             :     E_MAKEBASE_UNS,
      49             :     E_V16_OF_POINTEE,
      50             :     E_V2_OF_POINTEE,
      51             :     E_V3_OF_POINTEE,
      52             :     E_V4_OF_POINTEE,
      53             :     E_V8_OF_POINTEE,
      54             :     E_VLTLPTR_ANY,
      55             : };
      56             : 
      57             : struct ManglingRule {
      58             :    StringRef const Name;
      59             :    unsigned char Lead[2];
      60             :    unsigned char Param[5];
      61             : 
      62        1002 :    int maxLeadIndex() const { return (std::max)(Lead[0], Lead[1]); }
      63             :    int getNumLeads() const { return (Lead[0] ? 1 : 0) + (Lead[1] ? 1 : 0); }
      64             : 
      65             :    unsigned getNumArgs() const;
      66             : };
      67             : 
      68             : // Information about library functions with unmangled names.
      69             : class UnmangledFuncInfo {
      70             :   StringRef const Name;
      71             :   unsigned NumArgs;
      72             : 
      73             :   // Table for all lib functions with unmangled names.
      74             :   static const UnmangledFuncInfo Table[];
      75             : 
      76             :   // Number of entries in Table.
      77             :   static const unsigned TableSize;
      78             : 
      79             :   // Map function name to index.
      80       72306 :   class NameMap : public StringMap<unsigned> {
      81             :   public:
      82      144612 :     NameMap() {
      83      361530 :       for (unsigned I = 0; I != TableSize; ++I)
      84      578448 :         (*this)[Table[I].Name] = I;
      85       72306 :     }
      86             :   };
      87             :   friend class NameMap;
      88             :   static NameMap Map;
      89             : 
      90             : public:
      91             :   using ID = AMDGPULibFunc::EFuncId;
      92             :   UnmangledFuncInfo() = default;
      93             :   UnmangledFuncInfo(StringRef _Name, unsigned _NumArgs)
      94      289224 :       : 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          39 :            static_cast<unsigned>(AMDGPULibFunc::EI_LAST_MANGLED);
     103             :   }
     104             :   static ID toFuncId(unsigned Index) {
     105             :     assert(Index < TableSize && "Invalid unmangled library function");
     106             :     return static_cast<ID>(
     107          52 :         Index + 1 + static_cast<unsigned>(AMDGPULibFunc::EI_LAST_MANGLED));
     108             :   }
     109          39 :   static unsigned getNumArgs(ID Id) { return Table[toIndex(Id)].NumArgs; }
     110             :   static StringRef getName(ID Id) { return Table[toIndex(Id)].Name; }
     111             : };
     112             : 
     113             : unsigned ManglingRule::getNumArgs() const {
     114         169 :    unsigned I=0;
     115         454 :    while (I < (sizeof Param/sizeof Param[0]) && Param[I]) ++I;
     116             :    return I;
     117             : }
     118             : 
     119             : // This table describes function formal argument type rules. The order of rules
     120             : // corresponds to the EFuncId enum at AMDGPULibFunc.h
     121             : //
     122             : // "<func name>", { <leads> }, { <param rules> }
     123             : // where:
     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
     136             : //
     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.
     142             : 
     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} },
     342    14316588 : };
     343             : 
     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},
     350      361530 : };
     351             : 
     352             : const unsigned UnmangledFuncInfo::TableSize =
     353             :     sizeof(UnmangledFuncInfo::Table) / sizeof(UnmangledFuncInfo::Table[0]);
     354             : 
     355       72306 : UnmangledFuncInfo::NameMap UnmangledFuncInfo::Map;
     356             : 
     357       72306 : static const struct ManglingRulesMap : public StringMap<int> {
     358       72306 :   ManglingRulesMap()
     359      144612 :     : StringMap<int>(sizeof(manglingRules)/sizeof(manglingRules[0])) {
     360       72306 :     int Id = 0;
     361    14388894 :     for (auto Rule : manglingRules)
     362    42949764 :       insert({ Rule.Name, Id++ });
     363       72306 :   }
     364       72306 : } manglingRulesMap;
     365             : 
     366             : static AMDGPULibFunc::Param getRetType(AMDGPULibFunc::EFuncId id,
     367             :                                        const AMDGPULibFunc::Param (&Leads)[2]) {
     368          63 :   AMDGPULibFunc::Param Res = Leads[0];
     369             :   // TBD - This switch may require to be extended for other intriniscs
     370          63 :   switch (id) {
     371           6 :   case AMDGPULibFunc::EI_SINCOS:
     372           6 :     Res.PtrKind = AMDGPULibFunc::BYVALUE;
     373             :     break;
     374             :   default:
     375             :     break;
     376             :   }
     377             :   return Res;
     378             : }
     379             : 
     380             : class ParamIterator {
     381             :   const AMDGPULibFunc::Param (&Leads)[2];
     382             :   const ManglingRule& Rule;
     383             :   int Index;
     384             : public:
     385             :   ParamIterator(const AMDGPULibFunc::Param (&leads)[2],
     386             :                 const ManglingRule& rule)
     387         168 :     : Leads(leads), Rule(rule), Index(0) {}
     388             : 
     389             :   AMDGPULibFunc::Param getNextParam();
     390             : };
     391             : 
     392         360 : AMDGPULibFunc::Param ParamIterator::getNextParam() {
     393         360 :   AMDGPULibFunc::Param P;
     394         360 :   if (Index >= int(sizeof Rule.Param/sizeof Rule.Param[0])) return P;
     395             : 
     396         360 :   const char R = Rule.Param[Index];
     397         360 :   switch (R) {
     398             :   case E_NONE:     break;
     399           0 :   case EX_UINT:
     400           0 :     P.ArgType = AMDGPULibFunc::U32; break;
     401           0 :   case EX_INTV4:
     402           0 :     P.ArgType = AMDGPULibFunc::I32; P.VectorSize = 4; break;
     403           0 :   case EX_UINTV4:
     404           0 :     P.ArgType = AMDGPULibFunc::U32; P.VectorSize = 4; break;
     405           0 :   case EX_FLOAT4:
     406           0 :     P.ArgType = AMDGPULibFunc::F32; P.VectorSize = 4; break;
     407           0 :   case EX_SIZET:
     408           0 :     P.ArgType = AMDGPULibFunc::U64; break;
     409           0 :   case EX_EVENT:
     410           0 :     P.ArgType = AMDGPULibFunc::EVENT;   break;
     411           0 :   case EX_SAMPLER:
     412           0 :     P.ArgType = AMDGPULibFunc::SAMPLER; break;
     413             :   case EX_RESERVEDID: break; // TBD
     414         192 :   default:
     415         192 :     if (Index == (Rule.Lead[1] - 1)) P = Leads[1];
     416         192 :     else P = Leads[0];
     417             : 
     418         192 :     switch (R) {
     419             :     case E_ANY:
     420             :     case E_COPY: break;
     421             : 
     422          18 :     case E_POINTEE:
     423          18 :       P.PtrKind = AMDGPULibFunc::BYVALUE; break;
     424           0 :     case E_V2_OF_POINTEE:
     425           0 :       P.VectorSize = 2; P.PtrKind = AMDGPULibFunc::BYVALUE; break;
     426           0 :     case E_V3_OF_POINTEE:
     427           0 :       P.VectorSize = 3; P.PtrKind = AMDGPULibFunc::BYVALUE; break;
     428           0 :     case E_V4_OF_POINTEE:
     429           0 :       P.VectorSize = 4; P.PtrKind = AMDGPULibFunc::BYVALUE; break;
     430           0 :     case E_V8_OF_POINTEE:
     431           0 :       P.VectorSize = 8; P.PtrKind = AMDGPULibFunc::BYVALUE; break;
     432           0 :     case E_V16_OF_POINTEE:
     433           0 :       P.VectorSize = 16; P.PtrKind = AMDGPULibFunc::BYVALUE; break;
     434           0 :     case E_CONSTPTR_ANY:
     435           0 :       P.PtrKind |= AMDGPULibFunc::CONST; break;
     436           0 :     case E_VLTLPTR_ANY:
     437           0 :       P.PtrKind |= AMDGPULibFunc::VOLATILE; break;
     438           0 :     case E_SETBASE_I32:
     439           0 :       P.ArgType = AMDGPULibFunc::I32; break;
     440           0 :     case E_SETBASE_U32:
     441           0 :       P.ArgType = AMDGPULibFunc::U32; break;
     442             : 
     443           0 :     case E_MAKEBASE_UNS:
     444           0 :       P.ArgType &= ~AMDGPULibFunc::BASE_TYPE_MASK;
     445           0 :       P.ArgType |= AMDGPULibFunc::UINT;
     446           0 :       break;
     447             : 
     448           0 :     case E_IMAGECOORDS:
     449           0 :       switch (P.ArgType) {
     450           0 :       case AMDGPULibFunc::IMG1DA: P.VectorSize = 2; break;
     451           0 :       case AMDGPULibFunc::IMG1DB: P.VectorSize = 1; break;
     452           0 :       case AMDGPULibFunc::IMG2DA: P.VectorSize = 4; break;
     453           0 :       case AMDGPULibFunc::IMG1D:  P.VectorSize = 1; break;
     454           0 :       case AMDGPULibFunc::IMG2D:  P.VectorSize = 2; break;
     455           0 :       case AMDGPULibFunc::IMG3D:  P.VectorSize = 4; break;
     456             :       }
     457             :       P.PtrKind = AMDGPULibFunc::BYVALUE;
     458             :       P.ArgType = AMDGPULibFunc::I32;
     459             :       break;
     460             : 
     461           0 :     case E_CONSTPTR_SWAPGL:
     462           0 :       switch (P.PtrKind & AMDGPULibFunc::ADDR_SPACE) {
     463           0 :       case AMDGPULibFunc::GLOBAL: P.PtrKind = AMDGPULibFunc::LOCAL; break;
     464           0 :       case AMDGPULibFunc::LOCAL:  P.PtrKind = AMDGPULibFunc::GLOBAL; break;
     465             :       }
     466           0 :       P.PtrKind |= AMDGPULibFunc::CONST;
     467           0 :       break;
     468             : 
     469           0 :     default: llvm_unreachable("Unhandeled param rule");
     470             :     }
     471             :   }
     472         360 :   ++Index;
     473         360 :   return P;
     474             : }
     475             : 
     476        1242 : inline static void drop_front(StringRef& str, size_t n = 1) {
     477        1242 :   str = str.drop_front(n);
     478        1242 : }
     479             : 
     480             : static bool eatTerm(StringRef& mangledName, const char c) {
     481         636 :   if (mangledName.front() == c) {
     482          63 :     drop_front(mangledName);
     483             :     return true;
     484             :   }
     485             :   return false;
     486             : }
     487             : 
     488             : template <size_t N>
     489        1210 : static bool eatTerm(StringRef& mangledName, const char (&str)[N]) {
     490        1522 :   if (mangledName.startswith(StringRef(str, N-1))) {
     491         312 :     drop_front(mangledName, N-1);
     492         312 :     return true;
     493             :   }
     494             :   return false;
     495             : }
     496             : 
     497         924 : static inline bool isDigit(char c) { return c >= '0' && c <= '9'; }
     498             : 
     499         309 : static int eatNumber(StringRef& s) {
     500         309 :   size_t const savedSize = s.size();
     501         309 :   int n = 0;
     502        2379 :   while (!s.empty() && isDigit(s.front())) {
     503         363 :     n = n*10 + s.front() - '0';
     504         363 :     drop_front(s);
     505             :   }
     506         309 :   return s.size() < savedSize ? n : -1;
     507             : }
     508             : 
     509         249 : static StringRef eatLengthPrefixedName(StringRef& mangledName) {
     510         249 :   int const Len = eatNumber(mangledName);
     511         498 :   if (Len <= 0 || static_cast<size_t>(Len) > mangledName.size())
     512           0 :     return StringRef();
     513         498 :   StringRef Res = mangledName.substr(0, Len);
     514         249 :   drop_front(mangledName, Len);
     515         249 :   return Res;
     516             : }
     517             : 
     518             : } // end anonymous namespace
     519             : 
     520        1000 : AMDGPUMangledLibFunc::AMDGPUMangledLibFunc() {
     521         250 :   FuncId = EI_NONE;
     522         250 :   FKind = NOPFX;
     523         500 :   Leads[0].reset();
     524         500 :   Leads[1].reset();
     525         500 :   Name.clear();
     526         250 : }
     527             : 
     528        1412 : AMDGPUUnmangledLibFunc::AMDGPUUnmangledLibFunc() {
     529         706 :   FuncId = EI_NONE;
     530         706 :   FuncTy = nullptr;
     531         706 : }
     532             : 
     533          55 : AMDGPUMangledLibFunc::AMDGPUMangledLibFunc(
     534         220 :     EFuncId id, const AMDGPUMangledLibFunc &copyFrom) {
     535          55 :   FuncId = id;
     536          55 :   FKind = copyFrom.FKind;
     537          55 :   Leads[0] = copyFrom.Leads[0];
     538          55 :   Leads[1] = copyFrom.Leads[1];
     539          55 : }
     540             : 
     541             : ///////////////////////////////////////////////////////////////////////////////
     542             : // Demangling
     543             : 
     544          60 : static int parseVecSize(StringRef& mangledName) {
     545          60 :   size_t const Len = eatNumber(mangledName);
     546             :   switch (Len) {
     547             :   case 2: case 3: case 4: case 8: case 16:
     548             :     return Len;
     549             :   default:
     550             :     break;
     551             :   }
     552           0 :   return 1;
     553             : }
     554             : 
     555         249 : static AMDGPULibFunc::ENamePrefix parseNamePrefix(StringRef& mangledName) {
     556         249 :   std::pair<StringRef, StringRef> const P = mangledName.split('_');
     557             :   AMDGPULibFunc::ENamePrefix Pfx =
     558         249 :     StringSwitch<AMDGPULibFunc::ENamePrefix>(P.first)
     559         747 :     .Case("native", AMDGPULibFunc::NATIVE)
     560         747 :     .Case("half"  , AMDGPULibFunc::HALF)
     561         498 :     .Default(AMDGPULibFunc::NOPFX);
     562             : 
     563          42 :   if (Pfx != AMDGPULibFunc::NOPFX)
     564          42 :     mangledName = P.second;
     565             : 
     566         249 :   return Pfx;
     567             : }
     568             : 
     569         249 : bool AMDGPUMangledLibFunc::parseUnmangledName(StringRef FullName) {
     570         249 :   FuncId = static_cast<EFuncId>(manglingRulesMap.lookup(FullName));
     571         249 :   return FuncId != EI_NONE;
     572             : }
     573             : 
     574             : ///////////////////////////////////////////////////////////////////////////////
     575             : // Itanium Demangling
     576             : 
     577             : namespace {
     578         498 : struct ItaniumParamParser {
     579             :   AMDGPULibFunc::Param Prev;
     580             :   bool parseItaniumParam(StringRef& param, AMDGPULibFunc::Param &res);
     581             : };
     582             : } // namespace
     583             : 
     584         252 : bool ItaniumParamParser::parseItaniumParam(StringRef& param,
     585             :                                            AMDGPULibFunc::Param &res) {
     586         252 :   res.reset();
     587         252 :   if (param.empty()) return false;
     588             : 
     589             :   // parse pointer prefix
     590           3 :   if (eatTerm(param, 'P')) {
     591           0 :     if (eatTerm(param, 'K')) res.PtrKind |= AMDGPULibFunc::CONST;
     592           0 :     if (eatTerm(param, 'V')) res.PtrKind |= AMDGPULibFunc::VOLATILE;
     593           3 :     if (!eatTerm(param, "U3AS")) {
     594           0 :       res.PtrKind |= AMDGPULibFunc::PRIVATE;
     595             :     } else {
     596           6 :       switch(param.front()) {
     597           0 :       case '1': res.PtrKind |= AMDGPULibFunc::GLOBAL;  break;
     598           0 :       case '2': res.PtrKind |= AMDGPULibFunc::READONLY;break;
     599           0 :       case '3': res.PtrKind |= AMDGPULibFunc::LOCAL;   break;
     600           3 :       case '4': res.PtrKind |= AMDGPULibFunc::GENERIC; break;
     601           0 :       case '5': res.PtrKind |= AMDGPULibFunc::OTHER;   break;
     602             :       default: return false;
     603             :       }
     604           3 :       drop_front(param, 1);
     605             :     }
     606             :   } else {
     607             :     res.PtrKind = AMDGPULibFunc::BYVALUE;
     608             :   }
     609             : 
     610             :   // parse vector size
     611         252 :   if (eatTerm(param,"Dv")) {
     612          60 :     res.VectorSize = parseVecSize(param);
     613         120 :     if (res.VectorSize==1 || !eatTerm(param, '_')) return false;
     614             :   }
     615             : 
     616             :   // parse type
     617         504 :   char const TC = param.front();
     618         252 :   if (::isDigit(TC)) {
     619           0 :     res.ArgType = StringSwitch<AMDGPULibFunc::EType>
     620             :       (eatLengthPrefixedName(param))
     621           0 :       .Case("ocl_image1darray" , AMDGPULibFunc::IMG1DA)
     622           0 :       .Case("ocl_image1dbuffer", AMDGPULibFunc::IMG1DB)
     623           0 :       .Case("ocl_image2darray" , AMDGPULibFunc::IMG2DA)
     624           0 :       .Case("ocl_image1d"      , AMDGPULibFunc::IMG1D)
     625           0 :       .Case("ocl_image2d"      , AMDGPULibFunc::IMG2D)
     626           0 :       .Case("ocl_image3d"      , AMDGPULibFunc::IMG3D)
     627           0 :       .Case("ocl_event"        , AMDGPULibFunc::DUMMY)
     628           0 :       .Case("ocl_sampler"      , AMDGPULibFunc::DUMMY)
     629           0 :       .Default(AMDGPULibFunc::DUMMY);
     630             :   } else {
     631         252 :     drop_front(param);
     632         252 :     switch (TC) {
     633           0 :     case 'h': res.ArgType =  AMDGPULibFunc::U8; break;
     634           0 :     case 't': res.ArgType = AMDGPULibFunc::U16; break;
     635           0 :     case 'j': res.ArgType = AMDGPULibFunc::U32; break;
     636           0 :     case 'm': res.ArgType = AMDGPULibFunc::U64; break;
     637           0 :     case 'c': res.ArgType =  AMDGPULibFunc::I8; break;
     638           0 :     case 's': res.ArgType = AMDGPULibFunc::I16; break;
     639           0 :     case 'i': res.ArgType = AMDGPULibFunc::I32; break;
     640           0 :     case 'l': res.ArgType = AMDGPULibFunc::I64; break;
     641         248 :     case 'f': res.ArgType = AMDGPULibFunc::F32; break;
     642           4 :     case 'd': res.ArgType = AMDGPULibFunc::F64; break;
     643           0 :     case 'D': if (!eatTerm(param, 'h')) return false;
     644           0 :               res.ArgType = AMDGPULibFunc::F16; break;
     645           0 :     case 'S':
     646           0 :       if (!eatTerm(param, '_')) {
     647           0 :         eatNumber(param);
     648           0 :         if (!eatTerm(param, '_')) return false;
     649             :       }
     650           0 :       res.VectorSize = Prev.VectorSize;
     651           0 :       res.ArgType    = Prev.ArgType;
     652           0 :       break;
     653         252 :     default:;
     654             :     }
     655             :   }
     656         252 :   if (res.ArgType == 0) return false;
     657         252 :   Prev.VectorSize = res.VectorSize;
     658         252 :   Prev.ArgType    = res.ArgType;
     659         252 :   return true;
     660             : }
     661             : 
     662         249 : bool AMDGPUMangledLibFunc::parseFuncName(StringRef &mangledName) {
     663         249 :   StringRef Name = eatLengthPrefixedName(mangledName);
     664         249 :   FKind = parseNamePrefix(Name);
     665         249 :   if (!parseUnmangledName(Name))
     666             :     return false;
     667             : 
     668         249 :   const ManglingRule& Rule = manglingRules[FuncId];
     669         249 :   ItaniumParamParser Parser;
     670        1002 :   for (int I=0; I < Rule.maxLeadIndex(); ++I) {
     671         252 :     Param P;
     672         252 :     if (!Parser.parseItaniumParam(mangledName, P))
     673           0 :       return false;
     674             : 
     675         252 :     if ((I + 1) == Rule.Lead[0]) Leads[0] = P;
     676         252 :     if ((I + 1) == Rule.Lead[1]) Leads[1] = P;
     677             :   }
     678             :   return true;
     679             : }
     680             : 
     681         706 : bool AMDGPUUnmangledLibFunc::parseFuncName(StringRef &Name) {
     682         706 :   if (!UnmangledFuncInfo::lookup(Name, FuncId))
     683             :     return false;
     684          52 :   setName(Name);
     685          52 :   return true;
     686             : }
     687             : 
     688         955 : bool AMDGPULibFunc::parse(StringRef FuncName, AMDGPULibFunc &F) {
     689         955 :   if (FuncName.empty()) {
     690           0 :     F.Impl = std::unique_ptr<AMDGPULibFuncImpl>();
     691             :     return false;
     692             :   }
     693             : 
     694         955 :   if (eatTerm(FuncName, "_Z"))
     695         249 :     F.Impl = make_unique<AMDGPUMangledLibFunc>();
     696             :   else
     697         706 :     F.Impl = make_unique<AMDGPUUnmangledLibFunc>();
     698        1910 :   if (F.Impl->parseFuncName(FuncName))
     699             :     return true;
     700             : 
     701         654 :   F.Impl = std::unique_ptr<AMDGPULibFuncImpl>();
     702             :   return false;
     703             : }
     704             : 
     705           0 : StringRef AMDGPUMangledLibFunc::getUnmangledName(StringRef mangledName) {
     706           0 :   StringRef S = mangledName;
     707           0 :   if (eatTerm(S, "_Z"))
     708           0 :     return eatLengthPrefixedName(S);
     709           0 :   return StringRef();
     710             : }
     711             : 
     712             : ///////////////////////////////////////////////////////////////////////////////
     713             : // Mangling
     714             : 
     715             : template <typename Stream>
     716         105 : void AMDGPUMangledLibFunc::writeName(Stream &OS) const {
     717         105 :   const char *Pfx = "";
     718         105 :   switch (FKind) {
     719          32 :   case NATIVE: Pfx = "native_"; break;
     720           0 :   case HALF:   Pfx = "half_";   break;
     721             :   default: break;
     722             :   }
     723         210 :   if (!Name.empty()) {
     724           0 :     OS << Pfx << Name;
     725         105 :   } else if (FuncId != EI_NONE) {
     726         105 :     OS << Pfx;
     727         105 :     const StringRef& S = manglingRules[FuncId].Name;
     728         315 :     OS.write(S.data(), S.size());
     729             :   }
     730         105 : }
     731             : 
     732         105 : std::string AMDGPUMangledLibFunc::mangle() const { return mangleNameItanium(); }
     733             : 
     734             : ///////////////////////////////////////////////////////////////////////////////
     735             : // Itanium Mangling
     736             : 
     737         110 : static const char *getItaniumTypeName(AMDGPULibFunc::EType T) {
     738         110 :   switch (T) {
     739             :   case AMDGPULibFunc::U8:      return "h";
     740           0 :   case AMDGPULibFunc::U16:     return "t";
     741           0 :   case AMDGPULibFunc::U32:     return "j";
     742           0 :   case AMDGPULibFunc::U64:     return "m";
     743           0 :   case AMDGPULibFunc::I8:      return "c";
     744           0 :   case AMDGPULibFunc::I16:     return "s";
     745           0 :   case AMDGPULibFunc::I32:     return "i";
     746           0 :   case AMDGPULibFunc::I64:     return "l";
     747           0 :   case AMDGPULibFunc::F16:     return "Dh";
     748         110 :   case AMDGPULibFunc::F32:     return "f";
     749           0 :   case AMDGPULibFunc::F64:     return "d";
     750           0 :   case AMDGPULibFunc::IMG1DA:  return "16ocl_image1darray";
     751           0 :   case AMDGPULibFunc::IMG1DB:  return "17ocl_image1dbuffer";
     752           0 :   case AMDGPULibFunc::IMG2DA:  return "16ocl_image2darray";
     753           0 :   case AMDGPULibFunc::IMG1D:   return "11ocl_image1d";
     754           0 :   case AMDGPULibFunc::IMG2D:   return "11ocl_image2d";
     755           0 :   case AMDGPULibFunc::IMG3D:   return "11ocl_image3d";
     756           0 :   case AMDGPULibFunc::SAMPLER: return "11ocl_sampler";
     757           0 :   case AMDGPULibFunc::EVENT:   return "9ocl_event";
     758           0 :   default: llvm_unreachable("Unhandeled param type");
     759             :   }
     760             :   return nullptr;
     761             : }
     762             : 
     763             : namespace {
     764             : // Itanium mangling ABI says:
     765             : // "5.1.8. Compression
     766             : // ... Each non-terminal in the grammar for which <substitution> appears on the
     767             : // right-hand side is both a source of future substitutions and a candidate
     768             : // for being substituted. There are two exceptions that appear to be
     769             : // substitution candidates from the grammar, but are explicitly excluded:
     770             : // 1. <builtin-type> other than vendor extended types ..."
     771             : 
     772             : // For the purpose of functions the following productions make sence for the
     773             : // substitution:
     774             : //  <type> ::= <builtin-type>
     775             : //    ::= <class-enum-type>
     776             : //    ::= <array-type>
     777             : //    ::=<CV-qualifiers> <type>
     778             : //    ::= P <type>                # pointer-to
     779             : //    ::= <substitution>
     780             : //
     781             : // Note that while types like images, samplers and events are by the ABI encoded
     782             : // using <class-enum-type> production rule they're not used for substitution
     783             : // because clang consider them as builtin types.
     784             : //
     785             : // DvNN_ type is GCC extension for vectors and is a subject for the substitution.
     786             : 
     787             : 
     788         210 : class ItaniumMangler {
     789             :   SmallVector<AMDGPULibFunc::Param, 10> Str; // list of accumulated substituions
     790             :   bool  UseAddrSpace;
     791             : 
     792          62 :   int findSubst(const AMDGPULibFunc::Param& P) const {
     793         144 :     for(unsigned I = 0; I < Str.size(); ++I) {
     794          40 :       const AMDGPULibFunc::Param& T = Str[I];
     795          30 :       if (P.PtrKind    == T.PtrKind &&
     796          20 :           P.VectorSize == T.VectorSize &&
     797          10 :           P.ArgType    == T.ArgType) {
     798          10 :         return I;
     799             :       }
     800             :     }
     801             :     return -1;
     802             :   }
     803             : 
     804             :   template <typename Stream>
     805          62 :   bool trySubst(Stream& os, const AMDGPULibFunc::Param& p) {
     806          62 :     int const subst = findSubst(p);
     807          62 :     if (subst < 0) return false;
     808             :     // Substitutions are mangled as S(XX)?_ where XX is a hexadecimal number
     809             :     // 0   1    2
     810             :     // S_  S0_  S1_
     811          10 :     if (subst == 0) os << "S_";
     812           0 :     else os << 'S' << (subst-1) << '_';
     813             :     return true;
     814             :   }
     815             : 
     816             : public:
     817             :   ItaniumMangler(bool useAddrSpace)
     818         210 :     : UseAddrSpace(useAddrSpace) {}
     819             : 
     820             :   template <typename Stream>
     821         120 :   void operator()(Stream& os, AMDGPULibFunc::Param p) {
     822             : 
     823             :     // Itanium mangling ABI 5.1.8. Compression:
     824             :     // Logically, the substitutable components of a mangled name are considered
     825             :     // left-to-right, components before the composite structure of which they
     826             :     // are a part. If a component has been encountered before, it is substituted
     827             :     // as described below. This decision is independent of whether its components
     828             :     // have been substituted, so an implementation may optimize by considering
     829             :     // large structures for substitution before their components. If a component
     830             :     // has not been encountered before, its mangling is identified, and it is
     831             :     // added to a dictionary of substitution candidates. No entity is added to
     832             :     // the dictionary twice.
     833         120 :     AMDGPULibFunc::Param Ptr;
     834             : 
     835         120 :     if (p.PtrKind) {
     836          12 :       if (trySubst(os, p)) return;
     837          24 :       os << 'P';
     838          12 :       if (p.PtrKind & AMDGPULibFunc::CONST) os << 'K';
     839          12 :       if (p.PtrKind & AMDGPULibFunc::VOLATILE) os << 'V';
     840          12 :       int AS = UseAddrSpace ? (p.PtrKind & AMDGPULibFunc::ADDR_SPACE)-1 : 0;
     841          12 :       if (AS != 0) os << "U3AS" << AS;
     842          12 :       Ptr = p;
     843          12 :       p.PtrKind = 0;
     844             :     }
     845             : 
     846         120 :     if (p.VectorSize > 1) {
     847          50 :       if (trySubst(os, p)) goto exit;
     848          40 :       Str.push_back(p);
     849          80 :       os << "Dv" << static_cast<unsigned>(p.VectorSize) << '_';
     850             :     }
     851             : 
     852         110 :     os << getItaniumTypeName((AMDGPULibFunc::EType)p.ArgType);
     853             : 
     854         130 :   exit:
     855         120 :     if (Ptr.ArgType) Str.push_back(Ptr);
     856             :   }
     857             : };
     858             : } // namespace
     859             : 
     860         105 : std::string AMDGPUMangledLibFunc::mangleNameItanium() const {
     861         210 :   SmallString<128> Buf;
     862         210 :   raw_svector_ostream S(Buf);
     863         210 :   SmallString<128> NameBuf;
     864         210 :   raw_svector_ostream Name(NameBuf);
     865         105 :   writeName(Name);
     866         210 :   const StringRef& NameStr = Name.str();
     867         210 :   S << "_Z" << static_cast<int>(NameStr.size()) << NameStr;
     868             : 
     869         210 :   ItaniumMangler Mangler(true);
     870         210 :   ParamIterator I(Leads, manglingRules[FuncId]);
     871             :   Param P;
     872         345 :   while ((P = I.getNextParam()).ArgType != 0)
     873         120 :     Mangler(S, P);
     874         420 :   return S.str();
     875             : }
     876             : 
     877             : ///////////////////////////////////////////////////////////////////////////////
     878             : // Misc
     879             : 
     880         135 : static Type* getIntrinsicParamType(
     881             :   LLVMContext& C,
     882             :   const AMDGPULibFunc::Param& P,
     883             :   bool useAddrSpace) {
     884         135 :   Type* T = nullptr;
     885         135 :   switch (P.ArgType) {
     886           0 :   case AMDGPULibFunc::U8:
     887           0 :   case AMDGPULibFunc::I8:   T = Type::getInt8Ty(C);   break;
     888           0 :   case AMDGPULibFunc::U16:
     889           0 :   case AMDGPULibFunc::I16:  T = Type::getInt16Ty(C);  break;
     890           0 :   case AMDGPULibFunc::U32:
     891           0 :   case AMDGPULibFunc::I32:  T = Type::getInt32Ty(C);  break;
     892           0 :   case AMDGPULibFunc::U64:
     893           0 :   case AMDGPULibFunc::I64:  T = Type::getInt64Ty(C);  break;
     894           0 :   case AMDGPULibFunc::F16:  T = Type::getHalfTy(C);   break;
     895         135 :   case AMDGPULibFunc::F32:  T = Type::getFloatTy(C);  break;
     896           0 :   case AMDGPULibFunc::F64:  T = Type::getDoubleTy(C); break;
     897             : 
     898           0 :   case AMDGPULibFunc::IMG1DA:
     899             :   case AMDGPULibFunc::IMG1DB:
     900             :   case AMDGPULibFunc::IMG2DA:
     901             :   case AMDGPULibFunc::IMG1D:
     902             :   case AMDGPULibFunc::IMG2D:
     903             :   case AMDGPULibFunc::IMG3D:
     904           0 :     T = StructType::create(C,"ocl_image")->getPointerTo(); break;
     905           0 :   case AMDGPULibFunc::SAMPLER:
     906           0 :     T = StructType::create(C,"ocl_sampler")->getPointerTo(); break;
     907           0 :   case AMDGPULibFunc::EVENT:
     908           0 :     T = StructType::create(C,"ocl_event")->getPointerTo(); break;
     909           0 :   default:
     910           0 :     llvm_unreachable("Unhandeled param type");
     911             :     return nullptr;
     912             :   }
     913         135 :   if (P.VectorSize > 1)
     914          35 :     T = VectorType::get(T, P.VectorSize);
     915         135 :   if (P.PtrKind != AMDGPULibFunc::BYVALUE)
     916          12 :     T = useAddrSpace ? T->getPointerTo((P.PtrKind & AMDGPULibFunc::ADDR_SPACE)
     917           6 :                                        - 1)
     918             :                      : T->getPointerTo();
     919             :   return T;
     920             : }
     921             : 
     922          63 : FunctionType *AMDGPUMangledLibFunc::getFunctionType(Module &M) const {
     923          63 :   LLVMContext& C = M.getContext();
     924         126 :   std::vector<Type*> Args;
     925         126 :   ParamIterator I(Leads, manglingRules[FuncId]);
     926             :   Param P;
     927         207 :   while ((P=I.getNextParam()).ArgType != 0)
     928         144 :     Args.push_back(getIntrinsicParamType(C, P, true));
     929             : 
     930         189 :   return FunctionType::get(
     931         189 :     getIntrinsicParamType(C, getRetType(FuncId, Leads), true),
     932         126 :     Args, false);
     933             : }
     934             : 
     935         169 : unsigned AMDGPUMangledLibFunc::getNumArgs() const {
     936         338 :   return manglingRules[FuncId].getNumArgs();
     937             : }
     938             : 
     939          39 : unsigned AMDGPUUnmangledLibFunc::getNumArgs() const {
     940          78 :   return UnmangledFuncInfo::getNumArgs(FuncId);
     941             : }
     942             : 
     943           0 : std::string AMDGPUMangledLibFunc::getName() const {
     944           0 :   SmallString<128> Buf;
     945           0 :   raw_svector_ostream OS(Buf);
     946           0 :   writeName(OS);
     947           0 :   return OS.str();
     948             : }
     949             : 
     950          18 : Function *AMDGPULibFunc::getFunction(Module *M, const AMDGPULibFunc &fInfo) {
     951          36 :   std::string FuncName = fInfo.mangle();
     952          36 :   Function *F = dyn_cast_or_null<Function>(
     953          29 :     M->getValueSymbolTable().lookup(FuncName));
     954             : 
     955             :   // check formal with actual types conformance
     956          11 :   if (F && !F->isDeclaration()
     957           0 :         && !F->isVarArg()
     958           0 :         && F->arg_size() == fInfo.getNumArgs()) {
     959             :     return F;
     960             :   }
     961             :   return nullptr;
     962             : }
     963             : 
     964          99 : Function *AMDGPULibFunc::getOrInsertFunction(Module *M,
     965             :                                              const AMDGPULibFunc &fInfo) {
     966         198 :   std::string const FuncName = fInfo.mangle();
     967         198 :   Function *F = dyn_cast_or_null<Function>(
     968         133 :     M->getValueSymbolTable().lookup(FuncName));
     969             : 
     970             :   // check formal with actual types conformance
     971          34 :   if (F && !F->isDeclaration()
     972           0 :         && !F->isVarArg()
     973           0 :         && F->arg_size() == fInfo.getNumArgs()) {
     974             :     return F;
     975             :   }
     976             : 
     977          99 :   FunctionType *FuncTy = fInfo.getFunctionType(*M);
     978             : 
     979          99 :   bool hasPtr = false;
     980          66 :   for (FunctionType::param_iterator
     981          99 :          PI = FuncTy->param_begin(),
     982          99 :          PE = FuncTy->param_end();
     983         165 :        PI != PE; ++PI) {
     984         108 :     const Type* argTy = static_cast<const Type*>(*PI);
     985         108 :     if (argTy->isPointerTy()) {
     986             :       hasPtr = true;
     987             :       break;
     988             :     }
     989             :   }
     990             : 
     991          99 :   Constant *C = nullptr;
     992          99 :   if (hasPtr) {
     993             :     // Do not set extra attributes for functions with pointer arguments.
     994          42 :     C = M->getOrInsertFunction(FuncName, FuncTy);
     995             :   } else {
     996          57 :     AttributeList Attr;
     997          57 :     LLVMContext &Ctx = M->getContext();
     998          57 :     Attr.addAttribute(Ctx, AttributeList::FunctionIndex, Attribute::ReadOnly);
     999          57 :     Attr.addAttribute(Ctx, AttributeList::FunctionIndex, Attribute::NoUnwind);
    1000          57 :     C = M->getOrInsertFunction(FuncName, FuncTy, Attr);
    1001             :   }
    1002             : 
    1003             :   return cast<Function>(C);
    1004             : }
    1005             : 
    1006         706 : bool UnmangledFuncInfo::lookup(StringRef Name, ID &Id) {
    1007         706 :   auto Loc = Map.find(Name);
    1008        2118 :   if (Loc != Map.end()) {
    1009         104 :     Id = toFuncId(Loc->second);
    1010          52 :     return true;
    1011             :   }
    1012         654 :   Id = AMDGPULibFunc::EI_NONE;
    1013         654 :   return false;
    1014             : }
    1015             : 
    1016           4 : AMDGPULibFunc::AMDGPULibFunc(const AMDGPULibFunc &F) {
    1017           6 :   if (auto *MF = dyn_cast<AMDGPUMangledLibFunc>(F.Impl.get()))
    1018           2 :     Impl.reset(new AMDGPUMangledLibFunc(*MF));
    1019           0 :   else if (auto *UMF = dyn_cast<AMDGPUUnmangledLibFunc>(F.Impl.get()))
    1020           0 :     Impl.reset(new AMDGPUUnmangledLibFunc(*UMF));
    1021             :   else
    1022             :     Impl = std::unique_ptr<AMDGPULibFuncImpl>();
    1023           2 : }
    1024             : 
    1025           0 : AMDGPULibFunc &AMDGPULibFunc::operator=(const AMDGPULibFunc &F) {
    1026           0 :   if (this == &F)
    1027             :     return *this;
    1028           0 :   new (this) AMDGPULibFunc(F);
    1029           0 :   return *this;
    1030             : }
    1031             : 
    1032         110 : AMDGPULibFunc::AMDGPULibFunc(EFuncId Id, const AMDGPULibFunc &CopyFrom) {
    1033             :   assert(AMDGPULibFuncBase::isMangled(Id) && CopyFrom.isMangled() &&
    1034             :          "not supported");
    1035          55 :   Impl.reset(new AMDGPUMangledLibFunc(
    1036         165 :       Id, *cast<AMDGPUMangledLibFunc>(CopyFrom.Impl.get())));
    1037          55 : }
    1038             : 
    1039          72 : AMDGPULibFunc::AMDGPULibFunc(StringRef Name, FunctionType *FT) {
    1040          72 :   Impl.reset(new AMDGPUUnmangledLibFunc(Name, FT));
    1041          36 : }
    1042             : 
    1043           2 : void AMDGPULibFunc::initMangled() { Impl.reset(new AMDGPUMangledLibFunc()); }
    1044             : 
    1045          14 : AMDGPULibFunc::Param *AMDGPULibFunc::getLeads() {
    1046          28 :   if (!Impl)
    1047           1 :     initMangled();
    1048          42 :   return cast<AMDGPUMangledLibFunc>(Impl.get())->Leads;
    1049             : }
    1050             : 
    1051         323 : const AMDGPULibFunc::Param *AMDGPULibFunc::getLeads() const {
    1052         969 :   return cast<const AMDGPUMangledLibFunc>(Impl.get())->Leads;
    1053      216918 : }

Generated by: LCOV version 1.13