1 //===-- AMDGPULibFunc.h ----------------------------------------*- C++ -*--===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #ifndef _AMDGPU_LIBFUNC_H_
10 #define _AMDGPU_LIBFUNC_H_
12 #include "llvm/ADT/StringRef.h"
21 class AMDGPULibFuncBase
{
26 // IMPORTANT: enums below should go in ascending by 1 value order
27 // because they are used as indexes in the mangling rules table.
28 // don't use explicit value assignment.
30 // There are two types of library functions: those with mangled
31 // name and those with unmangled name. The enums for the library
32 // functions with mangled name are defined before enums for the
33 // library functions with unmangled name. The enum for the last
34 // library function with mangled name is EI_LAST_MANGLED.
36 // Library functions with mangled name.
48 EI_ASYNC_WORK_GROUP_COPY
,
49 EI_ASYNC_WORK_GROUP_STRIDED_COPY
,
101 EI_GET_IMAGE_ARRAY_SIZE
,
102 EI_GET_IMAGE_CHANNEL_DATA_TYPE
,
103 EI_GET_IMAGE_CHANNEL_ORDER
,
107 EI_GET_PIPE_MAX_PACKETS
,
108 EI_GET_PIPE_NUM_PACKETS
,
158 EI_RESERVE_READ_PIPE
,
159 EI_RESERVE_WRITE_PIPE
,
178 EI_SUB_GROUP_BROADCAST
,
179 EI_SUB_GROUP_COMMIT_READ_PIPE
,
180 EI_SUB_GROUP_COMMIT_WRITE_PIPE
,
181 EI_SUB_GROUP_REDUCE_ADD
,
182 EI_SUB_GROUP_REDUCE_MAX
,
183 EI_SUB_GROUP_REDUCE_MIN
,
184 EI_SUB_GROUP_RESERVE_READ_PIPE
,
185 EI_SUB_GROUP_RESERVE_WRITE_PIPE
,
186 EI_SUB_GROUP_SCAN_EXCLUSIVE_ADD
,
187 EI_SUB_GROUP_SCAN_EXCLUSIVE_MAX
,
188 EI_SUB_GROUP_SCAN_EXCLUSIVE_MIN
,
189 EI_SUB_GROUP_SCAN_INCLUSIVE_ADD
,
190 EI_SUB_GROUP_SCAN_INCLUSIVE_MAX
,
191 EI_SUB_GROUP_SCAN_INCLUSIVE_MIN
,
206 EI_WORK_GROUP_COMMIT_READ_PIPE
,
207 EI_WORK_GROUP_COMMIT_WRITE_PIPE
,
208 EI_WORK_GROUP_REDUCE_ADD
,
209 EI_WORK_GROUP_REDUCE_MAX
,
210 EI_WORK_GROUP_REDUCE_MIN
,
211 EI_WORK_GROUP_RESERVE_READ_PIPE
,
212 EI_WORK_GROUP_RESERVE_WRITE_PIPE
,
213 EI_WORK_GROUP_SCAN_EXCLUSIVE_ADD
,
214 EI_WORK_GROUP_SCAN_EXCLUSIVE_MAX
,
215 EI_WORK_GROUP_SCAN_EXCLUSIVE_MIN
,
216 EI_WORK_GROUP_SCAN_INCLUSIVE_ADD
,
217 EI_WORK_GROUP_SCAN_INCLUSIVE_MAX
,
218 EI_WORK_GROUP_SCAN_INCLUSIVE_MIN
,
235 EI_RCBRT
, /* The last library function with mangled name */
237 // Library functions with unmangled name.
261 BASE_TYPE_MASK
= 0x30,
286 ADDR_SPACE
= 0xF, // Address space takes value 0x1 ~ 0xF.
292 unsigned char ArgType
;
293 unsigned char VectorSize
;
294 unsigned char PtrKind
;
296 unsigned char Reserved
;
305 template <typename Stream
>
306 void mangleItanium(Stream
& os
);
308 static bool isMangled(EFuncId Id
) {
309 return static_cast<unsigned>(Id
) <= static_cast<unsigned>(EI_LAST_MANGLED
);
312 static unsigned getEPtrKindFromAddrSpace(unsigned AS
) {
313 assert(((AS
+ 1) & ~ADDR_SPACE
) == 0);
317 static unsigned getAddrSpaceFromEPtrKind(unsigned Kind
) {
318 Kind
= Kind
& ADDR_SPACE
;
324 class AMDGPULibFuncImpl
: public AMDGPULibFuncBase
{
326 AMDGPULibFuncImpl() {}
327 virtual ~AMDGPULibFuncImpl() {}
329 /// Get unmangled name for mangled library function and name for unmangled
330 /// library function.
331 virtual std::string
getName() const = 0;
332 virtual unsigned getNumArgs() const = 0;
333 EFuncId
getId() const { return FuncId
; }
334 ENamePrefix
getPrefix() const { return FKind
; }
336 bool isMangled() const { return AMDGPULibFuncBase::isMangled(FuncId
); }
338 void setId(EFuncId id
) { FuncId
= id
; }
339 virtual bool parseFuncName(StringRef
&mangledName
) = 0;
341 /// \return The mangled function name for mangled library functions
342 /// and unmangled function name for unmangled library functions.
343 virtual std::string
mangle() const = 0;
345 void setName(StringRef N
) { Name
= std::string(N
); }
346 void setPrefix(ENamePrefix pfx
) { FKind
= pfx
; }
348 virtual FunctionType
*getFunctionType(Module
&M
) const = 0;
356 /// Wrapper class for AMDGPULIbFuncImpl
357 class AMDGPULibFunc
: public AMDGPULibFuncBase
{
359 explicit AMDGPULibFunc() : Impl(std::unique_ptr
<AMDGPULibFuncImpl
>()) {}
360 AMDGPULibFunc(const AMDGPULibFunc
&F
);
361 /// Clone a mangled library func with the Id \p Id and argument info from \p
363 explicit AMDGPULibFunc(EFuncId Id
, const AMDGPULibFunc
&CopyFrom
);
364 /// Construct an unmangled library function on the fly.
365 explicit AMDGPULibFunc(StringRef FName
, FunctionType
*FT
);
367 AMDGPULibFunc
&operator=(const AMDGPULibFunc
&F
);
369 /// Get unmangled name for mangled library function and name for unmangled
370 /// library function.
371 std::string
getName() const { return Impl
->getName(); }
372 unsigned getNumArgs() const { return Impl
->getNumArgs(); }
373 EFuncId
getId() const { return Impl
->getId(); }
374 ENamePrefix
getPrefix() const { return Impl
->getPrefix(); }
375 /// Get leading parameters for mangled lib functions.
377 const Param
*getLeads() const;
379 bool isMangled() const { return Impl
->isMangled(); }
380 void setId(EFuncId Id
) { Impl
->setId(Id
); }
381 bool parseFuncName(StringRef
&MangledName
) {
382 return Impl
->parseFuncName(MangledName
);
385 /// \return The mangled function name for mangled library functions
386 /// and unmangled function name for unmangled library functions.
387 std::string
mangle() const { return Impl
->mangle(); }
389 void setName(StringRef N
) { Impl
->setName(N
); }
390 void setPrefix(ENamePrefix PFX
) { Impl
->setPrefix(PFX
); }
392 FunctionType
*getFunctionType(Module
&M
) const {
393 return Impl
->getFunctionType(M
);
395 static Function
*getFunction(llvm::Module
*M
, const AMDGPULibFunc
&fInfo
);
397 static FunctionCallee
getOrInsertFunction(llvm::Module
*M
,
398 const AMDGPULibFunc
&fInfo
);
399 static bool parse(StringRef MangledName
, AMDGPULibFunc
&Ptr
);
402 /// Initialize as a mangled library function.
404 std::unique_ptr
<AMDGPULibFuncImpl
> Impl
;
407 class AMDGPUMangledLibFunc
: public AMDGPULibFuncImpl
{
411 explicit AMDGPUMangledLibFunc();
412 explicit AMDGPUMangledLibFunc(EFuncId id
,
413 const AMDGPUMangledLibFunc
©From
);
415 std::string
getName() const override
;
416 unsigned getNumArgs() const override
;
417 FunctionType
*getFunctionType(Module
&M
) const override
;
418 static StringRef
getUnmangledName(StringRef MangledName
);
420 bool parseFuncName(StringRef
&mangledName
) override
;
422 // Methods for support type inquiry through isa, cast, and dyn_cast:
423 static bool classof(const AMDGPULibFuncImpl
*F
) { return F
->isMangled(); }
425 std::string
mangle() const override
;
428 std::string
mangleNameItanium() const;
430 std::string
mangleName(StringRef Name
) const;
431 bool parseUnmangledName(StringRef MangledName
);
433 template <typename Stream
> void writeName(Stream
&OS
) const;
436 class AMDGPUUnmangledLibFunc
: public AMDGPULibFuncImpl
{
437 FunctionType
*FuncTy
;
440 explicit AMDGPUUnmangledLibFunc();
441 explicit AMDGPUUnmangledLibFunc(StringRef FName
, FunctionType
*FT
) {
442 Name
= std::string(FName
);
445 std::string
getName() const override
{ return Name
; }
446 unsigned getNumArgs() const override
;
447 FunctionType
*getFunctionType(Module
&M
) const override
{ return FuncTy
; }
449 bool parseFuncName(StringRef
&Name
) override
;
451 // Methods for support type inquiry through isa, cast, and dyn_cast:
452 static bool classof(const AMDGPULibFuncImpl
*F
) { return !F
->isMangled(); }
454 std::string
mangle() const override
{ return Name
; }
456 void setFunctionType(FunctionType
*FT
) { FuncTy
= FT
; }
459 #endif // _AMDGPU_LIBFUNC_H_