LLVM 24.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 static bool isMangled(EFuncId Id) {
317 return static_cast<unsigned>(Id) <= static_cast<unsigned>(EI_LAST_MANGLED);
318 }
319
320 static unsigned getEPtrKindFromAddrSpace(unsigned AS) {
321 assert(((AS + 1) & ~ADDR_SPACE) == 0);
322 return AS + 1;
323 }
324
325 static unsigned getAddrSpaceFromEPtrKind(unsigned Kind) {
326 Kind = Kind & ADDR_SPACE;
327 assert(Kind >= 1);
328 return Kind - 1;
329 }
330};
331
333public:
334 AMDGPULibFuncImpl() = default;
335 virtual ~AMDGPULibFuncImpl() = default;
336
337 /// Get unmangled name for mangled library function and name for unmangled
338 /// library function.
339 virtual std::string getName() const = 0;
340 virtual unsigned getNumArgs() const = 0;
341 EFuncId getId() const { return FuncId; }
342 ENamePrefix getPrefix() const { return FKind; }
343
345
346 void setId(EFuncId id) { FuncId = id; }
347 virtual bool parseFuncName(StringRef &mangledName) = 0;
348
349 /// \return The mangled function name for mangled library functions
350 /// and unmangled function name for unmangled library functions.
351 virtual std::string mangle() const = 0;
352
353 void setName(StringRef N) { Name = std::string(N); }
354 void setPrefix(ENamePrefix pfx) { FKind = pfx; }
355
356 virtual FunctionType *getFunctionType(const Module &M) const = 0;
357
358protected:
360 std::string Name;
362};
363
364/// Wrapper class for AMDGPULIbFuncImpl
366public:
367 explicit AMDGPULibFunc() : Impl(std::unique_ptr<AMDGPULibFuncImpl>()) {}
369 /// Clone a mangled library func with the Id \p Id and argument info from \p
370 /// CopyFrom.
371 explicit AMDGPULibFunc(EFuncId Id, const AMDGPULibFunc &CopyFrom);
372 explicit AMDGPULibFunc(EFuncId Id, FunctionType *FT, bool SignedInts);
373
374 /// Construct an unmangled library function on the fly.
375 explicit AMDGPULibFunc(StringRef FName, FunctionType *FT);
376
378
379 /// Get unmangled name for mangled library function and name for unmangled
380 /// library function.
381 std::string getName() const { return Impl->getName(); }
382 unsigned getNumArgs() const { return Impl->getNumArgs(); }
383 EFuncId getId() const { return Impl->getId(); }
384 ENamePrefix getPrefix() const { return Impl->getPrefix(); }
385 /// Get leading parameters for mangled lib functions.
386 Param *getLeads();
387 const Param *getLeads() const;
388
389 bool isMangled() const { return Impl->isMangled(); }
390 void setId(EFuncId Id) { Impl->setId(Id); }
391 bool parseFuncName(StringRef &MangledName) {
392 return Impl->parseFuncName(MangledName);
393 }
394
395 /// Return true if it's legal to splat a scalar value passed in parameter \p
396 /// ArgIdx to a vector argument.
397 bool allowsImplicitVectorSplat(int ArgIdx) const {
398 switch (getId()) {
399 case EI_LDEXP:
400 return ArgIdx == 1;
401 case EI_FMIN:
402 case EI_FMAX:
403 return true;
404 default:
405 return false;
406 }
407 }
408
409 // Validate the call type matches the expected libfunc type.
410 bool isCompatibleSignature(const Module &M, const FunctionType *FuncTy) const;
411
412 /// \return The mangled function name for mangled library functions
413 /// and unmangled function name for unmangled library functions.
414 std::string mangle() const { return Impl->mangle(); }
415
416 void setName(StringRef N) { Impl->setName(N); }
417 void setPrefix(ENamePrefix PFX) { Impl->setPrefix(PFX); }
418
420 return Impl->getFunctionType(M);
421 }
422 static Function *getFunction(llvm::Module *M, const AMDGPULibFunc &fInfo);
423
425 const AMDGPULibFunc &fInfo);
426 static bool parse(StringRef MangledName, AMDGPULibFunc &Ptr);
427
428private:
429 /// Initialize as a mangled library function.
430 void initMangled();
431 std::unique_ptr<AMDGPULibFuncImpl> Impl;
432};
433
435public:
437
438 explicit AMDGPUMangledLibFunc();
439 explicit AMDGPUMangledLibFunc(EFuncId id,
440 const AMDGPUMangledLibFunc &copyFrom);
442 bool SignedInts = true);
443
444 std::string getName() const override;
445 unsigned getNumArgs() const override;
446 FunctionType *getFunctionType(const Module &M) const override;
447 static StringRef getUnmangledName(StringRef MangledName);
448
449 bool parseFuncName(StringRef &mangledName) override;
450
451 // Methods for support type inquiry through isa, cast, and dyn_cast:
452 static bool classof(const AMDGPULibFuncImpl *F) { return F->isMangled(); }
453
454 std::string mangle() const override;
455
456private:
457 std::string mangleNameItanium() const;
458
459 bool parseUnmangledName(StringRef MangledName);
460
461 template <typename Stream> void writeName(Stream &OS) const;
462};
463
465 FunctionType *FuncTy;
466
467public:
468 explicit AMDGPUUnmangledLibFunc();
470 Name = std::string(FName);
471 FuncTy = FT;
472 }
473 std::string getName() const override { return Name; }
474 unsigned getNumArgs() const override;
475 FunctionType *getFunctionType(const Module &M) const override {
476 return FuncTy;
477 }
478
479 bool parseFuncName(StringRef &Name) override;
480
481 // Methods for support type inquiry through isa, cast, and dyn_cast:
482 static bool classof(const AMDGPULibFuncImpl *F) { return !F->isMangled(); }
483
484 std::string mangle() const override { return Name; }
485
486 void setFunctionType(FunctionType *FT) { FuncTy = FT; }
487};
488}
489#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:68
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
This is an optimization pass for GlobalISel generic memory operations.
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:878
#define N
static Param getIntN(unsigned char NumElts)
static Param getFromTy(Type *Ty, bool Signed)