LLVM 23.0.0git
AMDGPULibFunc.h
Go to the documentation of this file.
1//===-- AMDGPULibFunc.h ----------------------------------------*- C++ -*--===//
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#ifndef _AMDGPU_LIBFUNC_H_
10#define _AMDGPU_LIBFUNC_H_
11
12#include "llvm/ADT/StringRef.h"
13#include <memory>
14
15namespace llvm {
16
17class FunctionCallee;
18class FunctionType;
19class Function;
20class Module;
21class Type;
22
24public:
25 enum EFuncId {
27
28 // IMPORTANT: enums below should go in ascending by 1 value order
29 // because they are used as indexes in the mangling rules table.
30 // don't use explicit value assignment.
31 //
32 // There are two types of library functions: those with mangled
33 // name and those with unmangled name. The enums for the library
34 // functions with mangled name are defined before enums for the
35 // library functions with unmangled name. The enum for the last
36 // library function with mangled name is EI_LAST_MANGLED.
37 //
38 // Library functions with mangled name.
241 EI_RCBRT, /* The last library function with mangled name */
242
243 // Library functions with unmangled name.
248
250 };
251
257
289
290 enum EPtrKind {
292 ADDR_SPACE = 0xF, // Address space takes value 0x1 ~ 0xF.
293 CONST = 0x10,
294 VOLATILE = 0x20
295 };
296
297 struct Param {
298 unsigned char ArgType = 0;
299 unsigned char VectorSize = 1;
300 unsigned char PtrKind = 0;
301
302 unsigned char Reserved = 0;
303
304 void reset() {
305 ArgType = 0;
306 VectorSize = 1;
307 PtrKind = 0;
308 }
309
310 static Param getIntN(unsigned char NumElts) {
311 return Param{I32, NumElts, 0, 0};
312 }
313
314 static Param getFromTy(Type *Ty, bool Signed);
315
316 template <typename Stream>
317 void mangleItanium(Stream& os);
318 };
319 static bool isMangled(EFuncId Id) {
320 return static_cast<unsigned>(Id) <= static_cast<unsigned>(EI_LAST_MANGLED);
321 }
322
323 static unsigned getEPtrKindFromAddrSpace(unsigned AS) {
324 assert(((AS + 1) & ~ADDR_SPACE) == 0);
325 return AS + 1;
326 }
327
328 static unsigned getAddrSpaceFromEPtrKind(unsigned Kind) {
329 Kind = Kind & ADDR_SPACE;
330 assert(Kind >= 1);
331 return Kind - 1;
332 }
333};
334
336public:
337 AMDGPULibFuncImpl() = default;
338 virtual ~AMDGPULibFuncImpl() = default;
339
340 /// Get unmangled name for mangled library function and name for unmangled
341 /// library function.
342 virtual std::string getName() const = 0;
343 virtual unsigned getNumArgs() const = 0;
344 EFuncId getId() const { return FuncId; }
345 ENamePrefix getPrefix() const { return FKind; }
346
348
349 void setId(EFuncId id) { FuncId = id; }
350 virtual bool parseFuncName(StringRef &mangledName) = 0;
351
352 /// \return The mangled function name for mangled library functions
353 /// and unmangled function name for unmangled library functions.
354 virtual std::string mangle() const = 0;
355
356 void setName(StringRef N) { Name = std::string(N); }
357 void setPrefix(ENamePrefix pfx) { FKind = pfx; }
358
359 virtual FunctionType *getFunctionType(const Module &M) const = 0;
360
361protected:
363 std::string Name;
365};
366
367/// Wrapper class for AMDGPULIbFuncImpl
369public:
370 explicit AMDGPULibFunc() : Impl(std::unique_ptr<AMDGPULibFuncImpl>()) {}
372 /// Clone a mangled library func with the Id \p Id and argument info from \p
373 /// CopyFrom.
374 explicit AMDGPULibFunc(EFuncId Id, const AMDGPULibFunc &CopyFrom);
375 explicit AMDGPULibFunc(EFuncId Id, FunctionType *FT, bool SignedInts);
376
377 /// Construct an unmangled library function on the fly.
378 explicit AMDGPULibFunc(StringRef FName, FunctionType *FT);
379
381
382 /// Get unmangled name for mangled library function and name for unmangled
383 /// library function.
384 std::string getName() const { return Impl->getName(); }
385 unsigned getNumArgs() const { return Impl->getNumArgs(); }
386 EFuncId getId() const { return Impl->getId(); }
387 ENamePrefix getPrefix() const { return Impl->getPrefix(); }
388 /// Get leading parameters for mangled lib functions.
389 Param *getLeads();
390 const Param *getLeads() const;
391
392 bool isMangled() const { return Impl->isMangled(); }
393 void setId(EFuncId Id) { Impl->setId(Id); }
394 bool parseFuncName(StringRef &MangledName) {
395 return Impl->parseFuncName(MangledName);
396 }
397
398 /// Return true if it's legal to splat a scalar value passed in parameter \p
399 /// ArgIdx to a vector argument.
400 bool allowsImplicitVectorSplat(int ArgIdx) const {
401 switch (getId()) {
402 case EI_LDEXP:
403 return ArgIdx == 1;
404 case EI_FMIN:
405 case EI_FMAX:
406 return true;
407 default:
408 return false;
409 }
410 }
411
412 // Validate the call type matches the expected libfunc type.
413 bool isCompatibleSignature(const Module &M, const FunctionType *FuncTy) const;
414
415 /// \return The mangled function name for mangled library functions
416 /// and unmangled function name for unmangled library functions.
417 std::string mangle() const { return Impl->mangle(); }
418
419 void setName(StringRef N) { Impl->setName(N); }
420 void setPrefix(ENamePrefix PFX) { Impl->setPrefix(PFX); }
421
423 return Impl->getFunctionType(M);
424 }
425 static Function *getFunction(llvm::Module *M, const AMDGPULibFunc &fInfo);
426
428 const AMDGPULibFunc &fInfo);
429 static bool parse(StringRef MangledName, AMDGPULibFunc &Ptr);
430
431private:
432 /// Initialize as a mangled library function.
433 void initMangled();
434 std::unique_ptr<AMDGPULibFuncImpl> Impl;
435};
436
438public:
440
441 explicit AMDGPUMangledLibFunc();
442 explicit AMDGPUMangledLibFunc(EFuncId id,
443 const AMDGPUMangledLibFunc &copyFrom);
445 bool SignedInts = true);
446
447 std::string getName() const override;
448 unsigned getNumArgs() const override;
449 FunctionType *getFunctionType(const Module &M) const override;
450 static StringRef getUnmangledName(StringRef MangledName);
451
452 bool parseFuncName(StringRef &mangledName) override;
453
454 // Methods for support type inquiry through isa, cast, and dyn_cast:
455 static bool classof(const AMDGPULibFuncImpl *F) { return F->isMangled(); }
456
457 std::string mangle() const override;
458
459private:
460 std::string mangleNameItanium() const;
461
462 std::string mangleName(StringRef Name) const;
463 bool parseUnmangledName(StringRef MangledName);
464
465 template <typename Stream> void writeName(Stream &OS) const;
466};
467
469 FunctionType *FuncTy;
470
471public:
472 explicit AMDGPUUnmangledLibFunc();
474 Name = std::string(FName);
475 FuncTy = FT;
476 }
477 std::string getName() const override { return Name; }
478 unsigned getNumArgs() const override;
479 FunctionType *getFunctionType(const Module &M) const override {
480 return FuncTy;
481 }
482
483 bool parseFuncName(StringRef &Name) override;
484
485 // Methods for support type inquiry through isa, cast, and dyn_cast:
486 static bool classof(const AMDGPULibFuncImpl *F) { return !F->isMangled(); }
487
488 std::string mangle() const override { return Name; }
489
490 void setFunctionType(FunctionType *FT) { FuncTy = FT; }
491};
492}
493#endif // _AMDGPU_LIBFUNC_H_
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define PFX
#define F(x, y, z)
Definition MD5.cpp:54
static unsigned getEPtrKindFromAddrSpace(unsigned AS)
static unsigned getAddrSpaceFromEPtrKind(unsigned Kind)
static bool isMangled(EFuncId Id)
virtual std::string mangle() const =0
virtual FunctionType * getFunctionType(const Module &M) const =0
virtual bool parseFuncName(StringRef &mangledName)=0
virtual unsigned getNumArgs() const =0
ENamePrefix getPrefix() const
virtual ~AMDGPULibFuncImpl()=default
void setPrefix(ENamePrefix pfx)
void setName(StringRef N)
virtual std::string getName() const =0
Get unmangled name for mangled library function and name for unmangled library function.
void setId(EFuncId id)
Wrapper class for AMDGPULIbFuncImpl.
static Function * getFunction(llvm::Module *M, const AMDGPULibFunc &fInfo)
static bool parse(StringRef MangledName, AMDGPULibFunc &Ptr)
bool parseFuncName(StringRef &MangledName)
std::string getName() const
Get unmangled name for mangled library function and name for unmangled library function.
static FunctionCallee getOrInsertFunction(llvm::Module *M, const AMDGPULibFunc &fInfo)
void setPrefix(ENamePrefix PFX)
FunctionType * getFunctionType(const Module &M) const
void setName(StringRef N)
bool isCompatibleSignature(const Module &M, const FunctionType *FuncTy) const
EFuncId getId() const
bool allowsImplicitVectorSplat(int ArgIdx) const
Return true if it's legal to splat a scalar value passed in parameter ArgIdx to a vector argument.
bool isMangled() const
std::string mangle() const
AMDGPULibFunc & operator=(const AMDGPULibFunc &F)
Param * getLeads()
Get leading parameters for mangled lib functions.
void setId(EFuncId Id)
ENamePrefix getPrefix() const
unsigned getNumArgs() const
static StringRef getUnmangledName(StringRef MangledName)
unsigned getNumArgs() const override
bool parseFuncName(StringRef &mangledName) override
std::string getName() const override
Get unmangled name for mangled library function and name for unmangled library function.
static bool classof(const AMDGPULibFuncImpl *F)
FunctionType * getFunctionType(const Module &M) const override
std::string mangle() const override
static bool classof(const AMDGPULibFuncImpl *F)
unsigned getNumArgs() const override
std::string mangle() const override
FunctionType * getFunctionType(const Module &M) const override
void setFunctionType(FunctionType *FT)
bool parseFuncName(StringRef &Name) override
std::string getName() const override
Get unmangled name for mangled library function and name for unmangled library function.
AMDGPUUnmangledLibFunc(StringRef FName, FunctionType *FT)
A handy container for a FunctionType+Callee-pointer pair, which can be passed around as a single enti...
Class to represent function types.
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:45
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:870
#define N
void mangleItanium(Stream &os)
static Param getIntN(unsigned char NumElts)
static Param getFromTy(Type *Ty, bool Signed)