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(const 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 /// Return true if it's legal to splat a scalar value passed in parameter \p
395 /// ArgIdx to a vector argument.
396 bool allowsImplicitVectorSplat(int ArgIdx) const {
397 switch (getId()) {
398 case EI_LDEXP:
399 return ArgIdx == 1;
400 case EI_FMIN:
401 case EI_FMAX:
402 return true;
403 default:
404 return false;
405 }
406 }
407
408 // Validate the call type matches the expected libfunc type.
409 bool isCompatibleSignature(const Module &M, const FunctionType *FuncTy) const;
410
411 /// \return The mangled function name for mangled library functions
412 /// and unmangled function name for unmangled library functions.
413 std::string mangle() const { return Impl->mangle(); }
414
415 void setName(StringRef N) { Impl->setName(N); }
416 void setPrefix(ENamePrefix PFX) { Impl->setPrefix(PFX); }
417
419 return Impl->getFunctionType(M);
420 }
421 static Function *getFunction(llvm::Module *M, const AMDGPULibFunc &fInfo);
422
424 const AMDGPULibFunc &fInfo);
425 static bool parse(StringRef MangledName, AMDGPULibFunc &Ptr);
426
427private:
428 /// Initialize as a mangled library function.
429 void initMangled();
430 std::unique_ptr<AMDGPULibFuncImpl> Impl;
431};
432
434public:
436
437 explicit AMDGPUMangledLibFunc();
438 explicit AMDGPUMangledLibFunc(EFuncId id,
439 const AMDGPUMangledLibFunc &copyFrom);
441 bool SignedInts = true);
442
443 std::string getName() const override;
444 unsigned getNumArgs() const override;
445 FunctionType *getFunctionType(const Module &M) const override;
446 static StringRef getUnmangledName(StringRef MangledName);
447
448 bool parseFuncName(StringRef &mangledName) override;
449
450 // Methods for support type inquiry through isa, cast, and dyn_cast:
451 static bool classof(const AMDGPULibFuncImpl *F) { return F->isMangled(); }
452
453 std::string mangle() const override;
454
455private:
456 std::string mangleNameItanium() const;
457
458 std::string mangleName(StringRef Name) const;
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_
RelocType Type
Definition: COFFYAML.cpp:410
#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
virtual FunctionType * getFunctionType(const Module &M) const =0
EFuncId getId() const
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)
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...
Definition: DerivedTypes.h:170
Class to represent function types.
Definition: DerivedTypes.h:105
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:51
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