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
17struct AMDGPUOptions;
18class FunctionCallee;
19class FunctionType;
20class Function;
21class Module;
22class Type;
23
25public:
26 enum EFuncId {
28
29 // IMPORTANT: enums below should go in ascending by 1 value order
30 // because they are used as indexes in the mangling rules table.
31 // don't use explicit value assignment.
32 //
33 // There are two types of library functions: those with mangled
34 // name and those with unmangled name. The enums for the library
35 // functions with mangled name are defined before enums for the
36 // library functions with unmangled name. The enum for the last
37 // library function with mangled name is EI_LAST_MANGLED.
38 //
39 // Library functions with mangled name.
242 EI_RCBRT, /* The last library function with mangled name */
243
244 // Library functions with unmangled name.
249
251 };
252
258
290
291 enum EPtrKind {
293 ADDR_SPACE = 0xF, // Address space takes value 0x1 ~ 0xF.
294 CONST = 0x10,
295 VOLATILE = 0x20
296 };
297
298 struct Param {
299 unsigned char ArgType = 0;
300 unsigned char VectorSize = 1;
301 unsigned char PtrKind = 0;
302
303 unsigned char Reserved = 0;
304
305 void reset() {
306 ArgType = 0;
307 VectorSize = 1;
308 PtrKind = 0;
309 }
310
311 static Param getIntN(unsigned char NumElts) {
312 return Param{I32, NumElts, 0, 0};
313 }
314
315 static Param getFromTy(Type *Ty, bool Signed);
316 };
317 static bool isMangled(EFuncId Id) {
318 return static_cast<unsigned>(Id) <= static_cast<unsigned>(EI_LAST_MANGLED);
319 }
320
321 static unsigned char getEPtrKindFromAddrSpace(unsigned AS) {
322 assert(((AS + 1) & ~ADDR_SPACE) == 0);
323 return static_cast<unsigned char>(AS + 1);
324 }
325
326 static unsigned getAddrSpaceFromEPtrKind(unsigned Kind) {
327 Kind = Kind & ADDR_SPACE;
328 assert(Kind >= 1);
329 return Kind - 1;
330 }
331};
332
334public:
335 AMDGPULibFuncImpl() = default;
336 virtual ~AMDGPULibFuncImpl() = default;
337
338 /// Get unmangled name for mangled library function and name for unmangled
339 /// library function.
340 virtual std::string getName() const = 0;
341 virtual unsigned getNumArgs() const = 0;
342 EFuncId getId() const { return FuncId; }
343 ENamePrefix getPrefix() const { return FKind; }
344
346
347 void setId(EFuncId id) { FuncId = id; }
348 virtual bool parseFuncName(StringRef &mangledName) = 0;
349
350 /// \return The mangled function name for mangled library functions
351 /// and unmangled function name for unmangled library functions.
352 virtual std::string mangle(const AMDGPUOptions &CLOpts) const = 0;
353
354 void setName(StringRef N) { Name = std::string(N); }
355 void setPrefix(ENamePrefix pfx) { FKind = pfx; }
356
357 virtual FunctionType *getFunctionType(const Module &M) const = 0;
358
359protected:
361 std::string Name;
363};
364
365/// Wrapper class for AMDGPULIbFuncImpl
367public:
368 explicit AMDGPULibFunc() : Impl(std::unique_ptr<AMDGPULibFuncImpl>()) {}
370 /// Clone a mangled library func with the Id \p Id and argument info from \p
371 /// CopyFrom.
372 explicit AMDGPULibFunc(EFuncId Id, const AMDGPULibFunc &CopyFrom);
373 explicit AMDGPULibFunc(EFuncId Id, FunctionType *FT, bool SignedInts);
374
375 /// Construct an unmangled library function on the fly.
376 explicit AMDGPULibFunc(StringRef FName, FunctionType *FT);
377
379
380 /// Get unmangled name for mangled library function and name for unmangled
381 /// library function.
382 std::string getName() const { return Impl->getName(); }
383 unsigned getNumArgs() const { return Impl->getNumArgs(); }
384 EFuncId getId() const { return Impl->getId(); }
385 ENamePrefix getPrefix() const { return Impl->getPrefix(); }
386 /// Get leading parameters for mangled lib functions.
387 Param *getLeads();
388 const Param *getLeads() const;
389
390 bool isMangled() const { return Impl->isMangled(); }
391 void setId(EFuncId Id) { Impl->setId(Id); }
392 bool parseFuncName(StringRef &MangledName) {
393 return Impl->parseFuncName(MangledName);
394 }
395
396 /// Return true if it's legal to splat a scalar value passed in parameter \p
397 /// ArgIdx to a vector argument.
398 bool allowsImplicitVectorSplat(int ArgIdx) const {
399 switch (getId()) {
400 case EI_LDEXP:
401 return ArgIdx == 1;
402 case EI_FMIN:
403 case EI_FMAX:
404 return true;
405 default:
406 return false;
407 }
408 }
409
410 // Validate the call type matches the expected libfunc type.
411 bool isCompatibleSignature(const Module &M, const FunctionType *FuncTy) const;
412
413 /// \return The mangled function name for mangled library functions
414 /// and unmangled function name for unmangled library functions.
415 std::string mangle(const AMDGPUOptions &CLOpts) const {
416 return Impl->mangle(CLOpts);
417 }
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
451 bool parseFuncName(StringRef &mangledName) override;
452
453 // Methods for support type inquiry through isa, cast, and dyn_cast:
454 static bool classof(const AMDGPULibFuncImpl *F) { return F->isMangled(); }
455
456 std::string mangle(const AMDGPUOptions &CLOpts) const override;
457
458private:
459 std::string mangleNameItanium(const AMDGPUOptions &CLOpts) const;
460
461 bool parseUnmangledName(StringRef MangledName);
462
463 template <typename Stream> void writeName(Stream &OS) const;
464};
465
467 FunctionType *FuncTy;
468
469public:
470 explicit AMDGPUUnmangledLibFunc();
472 Name = std::string(FName);
473 FuncTy = FT;
474 }
475 std::string getName() const override { return Name; }
476 unsigned getNumArgs() const override;
477 FunctionType *getFunctionType(const Module &M) const override {
478 return FuncTy;
479 }
480
481 bool parseFuncName(StringRef &Name) override;
482
483 // Methods for support type inquiry through isa, cast, and dyn_cast:
484 static bool classof(const AMDGPULibFuncImpl *F) { return !F->isMangled(); }
485
486 std::string mangle(const AMDGPUOptions &) const override { return Name; }
487
488 void setFunctionType(FunctionType *FT) { FuncTy = FT; }
489};
490}
491#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 getAddrSpaceFromEPtrKind(unsigned Kind)
static bool isMangled(EFuncId Id)
static unsigned char getEPtrKindFromAddrSpace(unsigned AS)
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
virtual std::string mangle(const AMDGPUOptions &CLOpts) const =0
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)
std::string mangle(const AMDGPUOptions &CLOpts) const
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
AMDGPULibFunc & operator=(const AMDGPULibFunc &F)
Param * getLeads()
Get leading parameters for mangled lib functions.
void setId(EFuncId Id)
ENamePrefix getPrefix() const
unsigned getNumArgs() const
std::string mangle(const AMDGPUOptions &CLOpts) const override
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
static bool classof(const AMDGPULibFuncImpl *F)
unsigned getNumArgs() const override
FunctionType * getFunctionType(const Module &M) const override
void setFunctionType(FunctionType *FT)
bool parseFuncName(StringRef &Name) override
std::string mangle(const AMDGPUOptions &) const 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:879
#define N
static Param getIntN(unsigned char NumElts)
static Param getFromTy(Type *Ty, bool Signed)