LLVM 20.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.
237 EI_RCBRT, /* The last library function with mangled name */
238
239 // Library functions with unmangled name.
244
246 };
247
251 HALF
252 };
253
254 enum EType {
255 B8 = 1,
256 B16 = 2,
257 B32 = 3,
258 B64 = 4,
260 FLOAT = 0x10,
261 INT = 0x20,
262 UINT = 0x30,
264 U8 = UINT | B8,
268 I8 = INT | B8,
275 IMG1DA = 0x80,
283 DUMMY
284 };
285
286 enum EPtrKind {
288 ADDR_SPACE = 0xF, // Address space takes value 0x1 ~ 0xF.
289 CONST = 0x10,
290 VOLATILE = 0x20
291 };
292
293 struct Param {
294 unsigned char ArgType = 0;
295 unsigned char VectorSize = 1;
296 unsigned char PtrKind = 0;
297
298 unsigned char Reserved = 0;
299
300 void reset() {
301 ArgType = 0;
302 VectorSize = 1;
303 PtrKind = 0;
304 }
305
306 static Param getIntN(unsigned char NumElts) {
307 return Param{I32, NumElts, 0, 0};
308 }
309
310 static Param getFromTy(Type *Ty, bool Signed);
311
312 template <typename Stream>
313 void mangleItanium(Stream& os);
314 };
315 static bool isMangled(EFuncId Id) {
316 return static_cast<unsigned>(Id) <= static_cast<unsigned>(EI_LAST_MANGLED);
317 }
318
319 static unsigned getEPtrKindFromAddrSpace(unsigned AS) {
320 assert(((AS + 1) & ~ADDR_SPACE) == 0);
321 return AS + 1;
322 }
323
324 static unsigned getAddrSpaceFromEPtrKind(unsigned Kind) {
325 Kind = Kind & ADDR_SPACE;
326 assert(Kind >= 1);
327 return Kind - 1;
328 }
329};
330
332public:
333 AMDGPULibFuncImpl() = default;
334 virtual ~AMDGPULibFuncImpl() = default;
335
336 /// Get unmangled name for mangled library function and name for unmangled
337 /// library function.
338 virtual std::string getName() const = 0;
339 virtual unsigned getNumArgs() const = 0;
340 EFuncId getId() const { return FuncId; }
341 ENamePrefix getPrefix() const { return FKind; }
342
344
345 void setId(EFuncId id) { FuncId = id; }
346 virtual bool parseFuncName(StringRef &mangledName) = 0;
347
348 /// \return The mangled function name for mangled library functions
349 /// and unmangled function name for unmangled library functions.
350 virtual std::string mangle() const = 0;
351
352 void setName(StringRef N) { Name = std::string(N); }
353 void setPrefix(ENamePrefix pfx) { FKind = pfx; }
354
355 virtual FunctionType *getFunctionType(Module &M) const = 0;
356
357protected:
359 std::string Name;
361};
362
363/// Wrapper class for AMDGPULIbFuncImpl
365public:
366 explicit AMDGPULibFunc() : Impl(std::unique_ptr<AMDGPULibFuncImpl>()) {}
368 /// Clone a mangled library func with the Id \p Id and argument info from \p
369 /// CopyFrom.
370 explicit AMDGPULibFunc(EFuncId Id, const AMDGPULibFunc &CopyFrom);
371 explicit AMDGPULibFunc(EFuncId Id, FunctionType *FT, bool SignedInts);
372
373 /// Construct an unmangled library function on the fly.
374 explicit AMDGPULibFunc(StringRef FName, FunctionType *FT);
375
377
378 /// Get unmangled name for mangled library function and name for unmangled
379 /// library function.
380 std::string getName() const { return Impl->getName(); }
381 unsigned getNumArgs() const { return Impl->getNumArgs(); }
382 EFuncId getId() const { return Impl->getId(); }
383 ENamePrefix getPrefix() const { return Impl->getPrefix(); }
384 /// Get leading parameters for mangled lib functions.
385 Param *getLeads();
386 const Param *getLeads() const;
387
388 bool isMangled() const { return Impl->isMangled(); }
389 void setId(EFuncId Id) { Impl->setId(Id); }
390 bool parseFuncName(StringRef &MangledName) {
391 return Impl->parseFuncName(MangledName);
392 }
393
394 // Validate the call type matches the expected libfunc type.
395 bool isCompatibleSignature(const FunctionType *FuncTy) const;
396
397 /// \return The mangled function name for mangled library functions
398 /// and unmangled function name for unmangled library functions.
399 std::string mangle() const { return Impl->mangle(); }
400
401 void setName(StringRef N) { Impl->setName(N); }
402 void setPrefix(ENamePrefix PFX) { Impl->setPrefix(PFX); }
403
405 return Impl->getFunctionType(M);
406 }
407 static Function *getFunction(llvm::Module *M, const AMDGPULibFunc &fInfo);
408
410 const AMDGPULibFunc &fInfo);
411 static bool parse(StringRef MangledName, AMDGPULibFunc &Ptr);
412
413private:
414 /// Initialize as a mangled library function.
415 void initMangled();
416 std::unique_ptr<AMDGPULibFuncImpl> Impl;
417};
418
420public:
422
423 explicit AMDGPUMangledLibFunc();
424 explicit AMDGPUMangledLibFunc(EFuncId id,
425 const AMDGPUMangledLibFunc &copyFrom);
427 bool SignedInts = true);
428
429 std::string getName() const override;
430 unsigned getNumArgs() const override;
431 FunctionType *getFunctionType(Module &M) const override;
432 static StringRef getUnmangledName(StringRef MangledName);
433
434 bool parseFuncName(StringRef &mangledName) override;
435
436 // Methods for support type inquiry through isa, cast, and dyn_cast:
437 static bool classof(const AMDGPULibFuncImpl *F) { return F->isMangled(); }
438
439 std::string mangle() const override;
440
441private:
442 std::string mangleNameItanium() const;
443
444 std::string mangleName(StringRef Name) const;
445 bool parseUnmangledName(StringRef MangledName);
446
447 template <typename Stream> void writeName(Stream &OS) const;
448};
449
451 FunctionType *FuncTy;
452
453public:
454 explicit AMDGPUUnmangledLibFunc();
456 Name = std::string(FName);
457 FuncTy = FT;
458 }
459 std::string getName() const override { return Name; }
460 unsigned getNumArgs() const override;
461 FunctionType *getFunctionType(Module &M) const override { return FuncTy; }
462
463 bool parseFuncName(StringRef &Name) override;
464
465 // Methods for support type inquiry through isa, cast, and dyn_cast:
466 static bool classof(const AMDGPULibFuncImpl *F) { return !F->isMangled(); }
467
468 std::string mangle() const override { return Name; }
469
470 void setFunctionType(FunctionType *FT) { FuncTy = FT; }
471};
472}
473#endif // _AMDGPU_LIBFUNC_H_
RelocType Type
Definition: COFFYAML.cpp:391
#define PFX
#define F(x, y, z)
Definition: MD5.cpp:55
Machine Check Debug Module
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
static unsigned getEPtrKindFromAddrSpace(unsigned AS)
static unsigned getAddrSpaceFromEPtrKind(unsigned Kind)
static bool isMangled(EFuncId Id)
virtual std::string mangle() const =0
EFuncId getId() const
virtual bool parseFuncName(StringRef &mangledName)=0
virtual unsigned getNumArgs() const =0
ENamePrefix getPrefix() const
virtual FunctionType * getFunctionType(Module &M) const =0
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)
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(Module &M) const
void setName(StringRef N)
EFuncId getId() const
bool isCompatibleSignature(const FunctionType *FuncTy) const
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
FunctionType * getFunctionType(Module &M) 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)
std::string mangle() const override
static bool classof(const AMDGPULibFuncImpl *F)
unsigned getNumArgs() const override
std::string mangle() const override
void setFunctionType(FunctionType *FT)
bool parseFuncName(StringRef &Name) override
FunctionType * getFunctionType(Module &M) 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...
Definition: DerivedTypes.h:168
Class to represent function types.
Definition: DerivedTypes.h:103
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
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: AddressRanges.h:18
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858
#define N
void mangleItanium(Stream &os)
static Param getIntN(unsigned char NumElts)
static Param getFromTy(Type *Ty, bool Signed)
Definition: regcomp.c:192