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"
20 class AMDGPULibFuncBase {
25 // IMPORTANT: enums below should go in ascending by 1 value order
26 // because they are used as indexes in the mangling rules table.
27 // don't use explicit value assignment.
29 // There are two types of library functions: those with mangled
30 // name and those with unmangled name. The enums for the library
31 // functions with mangled name are defined before enums for the
32 // library functions with unmangled name. The enum for the last
33 // library function with mangled name is EI_LAST_MANGLED.
35 // Library functions with mangled name.
47 EI_ASYNC_WORK_GROUP_COPY,
48 EI_ASYNC_WORK_GROUP_STRIDED_COPY,
100 EI_GET_IMAGE_ARRAY_SIZE,
101 EI_GET_IMAGE_CHANNEL_DATA_TYPE,
102 EI_GET_IMAGE_CHANNEL_ORDER,
106 EI_GET_PIPE_MAX_PACKETS,
107 EI_GET_PIPE_NUM_PACKETS,
157 EI_RESERVE_READ_PIPE,
158 EI_RESERVE_WRITE_PIPE,
177 EI_SUB_GROUP_BROADCAST,
178 EI_SUB_GROUP_COMMIT_READ_PIPE,
179 EI_SUB_GROUP_COMMIT_WRITE_PIPE,
180 EI_SUB_GROUP_REDUCE_ADD,
181 EI_SUB_GROUP_REDUCE_MAX,
182 EI_SUB_GROUP_REDUCE_MIN,
183 EI_SUB_GROUP_RESERVE_READ_PIPE,
184 EI_SUB_GROUP_RESERVE_WRITE_PIPE,
185 EI_SUB_GROUP_SCAN_EXCLUSIVE_ADD,
186 EI_SUB_GROUP_SCAN_EXCLUSIVE_MAX,
187 EI_SUB_GROUP_SCAN_EXCLUSIVE_MIN,
188 EI_SUB_GROUP_SCAN_INCLUSIVE_ADD,
189 EI_SUB_GROUP_SCAN_INCLUSIVE_MAX,
190 EI_SUB_GROUP_SCAN_INCLUSIVE_MIN,
205 EI_WORK_GROUP_COMMIT_READ_PIPE,
206 EI_WORK_GROUP_COMMIT_WRITE_PIPE,
207 EI_WORK_GROUP_REDUCE_ADD,
208 EI_WORK_GROUP_REDUCE_MAX,
209 EI_WORK_GROUP_REDUCE_MIN,
210 EI_WORK_GROUP_RESERVE_READ_PIPE,
211 EI_WORK_GROUP_RESERVE_WRITE_PIPE,
212 EI_WORK_GROUP_SCAN_EXCLUSIVE_ADD,
213 EI_WORK_GROUP_SCAN_EXCLUSIVE_MAX,
214 EI_WORK_GROUP_SCAN_EXCLUSIVE_MIN,
215 EI_WORK_GROUP_SCAN_INCLUSIVE_ADD,
216 EI_WORK_GROUP_SCAN_INCLUSIVE_MAX,
217 EI_WORK_GROUP_SCAN_INCLUSIVE_MIN,
234 EI_RCBRT, /* The last library function with mangled name */
236 // Library functions with unmangled name.
260 BASE_TYPE_MASK = 0x30,
285 ADDR_SPACE = 0xF, // Address space takes value 0x1 ~ 0xF.
291 unsigned char ArgType;
292 unsigned char VectorSize;
293 unsigned char PtrKind;
295 unsigned char Reserved;
304 template <typename Stream>
305 void mangleItanium(Stream& os);
307 static bool isMangled(EFuncId Id) {
308 return static_cast<unsigned>(Id) <= static_cast<unsigned>(EI_LAST_MANGLED);
311 static unsigned getEPtrKindFromAddrSpace(unsigned AS) {
312 assert(((AS + 1) & ~ADDR_SPACE) == 0);
316 static unsigned getAddrSpaceFromEPtrKind(unsigned Kind) {
317 Kind = Kind & ADDR_SPACE;
323 class AMDGPULibFuncImpl : public AMDGPULibFuncBase {
325 AMDGPULibFuncImpl() {}
326 virtual ~AMDGPULibFuncImpl() {}
328 /// Get unmangled name for mangled library function and name for unmangled
329 /// library function.
330 virtual std::string getName() const = 0;
331 virtual unsigned getNumArgs() const = 0;
332 EFuncId getId() const { return FuncId; }
333 ENamePrefix getPrefix() const { return FKind; }
335 bool isMangled() const { return AMDGPULibFuncBase::isMangled(FuncId); }
337 void setId(EFuncId id) { FuncId = id; }
338 virtual bool parseFuncName(StringRef &mangledName) = 0;
340 /// \return The mangled function name for mangled library functions
341 /// and unmangled function name for unmangled library functions.
342 virtual std::string mangle() const = 0;
344 void setName(StringRef N) { Name = N; }
345 void setPrefix(ENamePrefix pfx) { FKind = pfx; }
347 virtual FunctionType *getFunctionType(Module &M) const = 0;
355 /// Wrapper class for AMDGPULIbFuncImpl
356 class AMDGPULibFunc : public AMDGPULibFuncBase {
358 explicit AMDGPULibFunc() : Impl(std::unique_ptr<AMDGPULibFuncImpl>()) {}
359 AMDGPULibFunc(const AMDGPULibFunc &F);
360 /// Clone a mangled library func with the Id \p Id and argument info from \p
362 explicit AMDGPULibFunc(EFuncId Id, const AMDGPULibFunc &CopyFrom);
363 /// Construct an unmangled library function on the fly.
364 explicit AMDGPULibFunc(StringRef FName, FunctionType *FT);
366 AMDGPULibFunc &operator=(const AMDGPULibFunc &F);
368 /// Get unmangled name for mangled library function and name for unmangled
369 /// library function.
370 std::string getName() const { return Impl->getName(); }
371 unsigned getNumArgs() const { return Impl->getNumArgs(); }
372 EFuncId getId() const { return Impl->getId(); }
373 ENamePrefix getPrefix() const { return Impl->getPrefix(); }
374 /// Get leading parameters for mangled lib functions.
376 const Param *getLeads() const;
378 bool isMangled() const { return Impl->isMangled(); }
379 void setId(EFuncId Id) { Impl->setId(Id); }
380 bool parseFuncName(StringRef &MangledName) {
381 return Impl->parseFuncName(MangledName);
384 /// \return The mangled function name for mangled library functions
385 /// and unmangled function name for unmangled library functions.
386 std::string mangle() const { return Impl->mangle(); }
388 void setName(StringRef N) { Impl->setName(N); }
389 void setPrefix(ENamePrefix PFX) { Impl->setPrefix(PFX); }
391 FunctionType *getFunctionType(Module &M) const {
392 return Impl->getFunctionType(M);
394 static Function *getFunction(llvm::Module *M, const AMDGPULibFunc &fInfo);
396 static FunctionCallee getOrInsertFunction(llvm::Module *M,
397 const AMDGPULibFunc &fInfo);
398 static bool parse(StringRef MangledName, AMDGPULibFunc &Ptr);
401 /// Initialize as a mangled library function.
403 std::unique_ptr<AMDGPULibFuncImpl> Impl;
406 class AMDGPUMangledLibFunc : public AMDGPULibFuncImpl {
410 explicit AMDGPUMangledLibFunc();
411 explicit AMDGPUMangledLibFunc(EFuncId id,
412 const AMDGPUMangledLibFunc ©From);
414 std::string getName() const override;
415 unsigned getNumArgs() const override;
416 FunctionType *getFunctionType(Module &M) const override;
417 static StringRef getUnmangledName(StringRef MangledName);
419 bool parseFuncName(StringRef &mangledName) override;
421 // Methods for support type inquiry through isa, cast, and dyn_cast:
422 static bool classof(const AMDGPULibFuncImpl *F) { return F->isMangled(); }
424 std::string mangle() const override;
427 std::string mangleNameItanium() const;
429 std::string mangleName(StringRef Name) const;
430 bool parseUnmangledName(StringRef MangledName);
432 template <typename Stream> void writeName(Stream &OS) const;
435 class AMDGPUUnmangledLibFunc : public AMDGPULibFuncImpl {
436 FunctionType *FuncTy;
439 explicit AMDGPUUnmangledLibFunc();
440 explicit AMDGPUUnmangledLibFunc(StringRef FName, FunctionType *FT) {
444 std::string getName() const override { return Name; }
445 unsigned getNumArgs() const override;
446 FunctionType *getFunctionType(Module &M) const override { return FuncTy; }
448 bool parseFuncName(StringRef &Name) override;
450 // Methods for support type inquiry through isa, cast, and dyn_cast:
451 static bool classof(const AMDGPULibFuncImpl *F) { return !F->isMangled(); }
453 std::string mangle() const override { return Name; }
455 void setFunctionType(FunctionType *FT) { FuncTy = FT; }
458 #endif // _AMDGPU_LIBFUNC_H_