LLVM 17.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;
21
23public:
24 enum EFuncId {
26
27 // IMPORTANT: enums below should go in ascending by 1 value order
28 // because they are used as indexes in the mangling rules table.
29 // don't use explicit value assignment.
30 //
31 // There are two types of library functions: those with mangled
32 // name and those with unmangled name. The enums for the library
33 // functions with mangled name are defined before enums for the
34 // library functions with unmangled name. The enum for the last
35 // library function with mangled name is EI_LAST_MANGLED.
36 //
37 // Library functions with mangled name.
236 EI_RCBRT, /* The last library function with mangled name */
237
238 // Library functions with unmangled name.
243
245 };
246
250 HALF
251 };
252
253 enum EType {
254 B8 = 1,
255 B16 = 2,
256 B32 = 3,
257 B64 = 4,
259 FLOAT = 0x10,
260 INT = 0x20,
261 UINT = 0x30,
263 U8 = UINT | B8,
267 I8 = INT | B8,
274 IMG1DA = 0x80,
282 DUMMY
283 };
284
285 enum EPtrKind {
287 ADDR_SPACE = 0xF, // Address space takes value 0x1 ~ 0xF.
288 CONST = 0x10,
289 VOLATILE = 0x20
290 };
291
292 struct Param {
293 unsigned char ArgType;
294 unsigned char VectorSize;
295 unsigned char PtrKind;
296
297 unsigned char Reserved;
298
299 void reset() {
300 ArgType = 0;
301 VectorSize = 1;
302 PtrKind = 0;
303 }
304 Param() { reset(); }
305
306 template <typename Stream>
307 void mangleItanium(Stream& os);
308 };
309 static bool isMangled(EFuncId Id) {
310 return static_cast<unsigned>(Id) <= static_cast<unsigned>(EI_LAST_MANGLED);
311 }
312
313 static unsigned getEPtrKindFromAddrSpace(unsigned AS) {
314 assert(((AS + 1) & ~ADDR_SPACE) == 0);
315 return AS + 1;
316 }
317
318 static unsigned getAddrSpaceFromEPtrKind(unsigned Kind) {
319 Kind = Kind & ADDR_SPACE;
320 assert(Kind >= 1);
321 return Kind - 1;
322 }
323};
324
326public:
327 AMDGPULibFuncImpl() = default;
328 virtual ~AMDGPULibFuncImpl() = default;
329
330 /// Get unmangled name for mangled library function and name for unmangled
331 /// library function.
332 virtual std::string getName() const = 0;
333 virtual unsigned getNumArgs() const = 0;
334 EFuncId getId() const { return FuncId; }
335 ENamePrefix getPrefix() const { return FKind; }
336
338
339 void setId(EFuncId id) { FuncId = id; }
340 virtual bool parseFuncName(StringRef &mangledName) = 0;
341
342 /// \return The mangled function name for mangled library functions
343 /// and unmangled function name for unmangled library functions.
344 virtual std::string mangle() const = 0;
345
346 void setName(StringRef N) { Name = std::string(N); }
347 void setPrefix(ENamePrefix pfx) { FKind = pfx; }
348
349 virtual FunctionType *getFunctionType(Module &M) const = 0;
350
351protected:
353 std::string Name;
355};
356
357/// Wrapper class for AMDGPULIbFuncImpl
359public:
360 explicit AMDGPULibFunc() : Impl(std::unique_ptr<AMDGPULibFuncImpl>()) {}
362 /// Clone a mangled library func with the Id \p Id and argument info from \p
363 /// CopyFrom.
364 explicit AMDGPULibFunc(EFuncId Id, const AMDGPULibFunc &CopyFrom);
365 /// Construct an unmangled library function on the fly.
366 explicit AMDGPULibFunc(StringRef FName, FunctionType *FT);
367
369
370 /// Get unmangled name for mangled library function and name for unmangled
371 /// library function.
372 std::string getName() const { return Impl->getName(); }
373 unsigned getNumArgs() const { return Impl->getNumArgs(); }
374 EFuncId getId() const { return Impl->getId(); }
375 ENamePrefix getPrefix() const { return Impl->getPrefix(); }
376 /// Get leading parameters for mangled lib functions.
377 Param *getLeads();
378 const Param *getLeads() const;
379
380 bool isMangled() const { return Impl->isMangled(); }
381 void setId(EFuncId Id) { Impl->setId(Id); }
382 bool parseFuncName(StringRef &MangledName) {
383 return Impl->parseFuncName(MangledName);
384 }
385
386 /// \return The mangled function name for mangled library functions
387 /// and unmangled function name for unmangled library functions.
388 std::string mangle() const { return Impl->mangle(); }
389
390 void setName(StringRef N) { Impl->setName(N); }
391 void setPrefix(ENamePrefix PFX) { Impl->setPrefix(PFX); }
392
394 return Impl->getFunctionType(M);
395 }
396 static Function *getFunction(llvm::Module *M, const AMDGPULibFunc &fInfo);
397
399 const AMDGPULibFunc &fInfo);
400 static bool parse(StringRef MangledName, AMDGPULibFunc &Ptr);
401
402private:
403 /// Initialize as a mangled library function.
404 void initMangled();
405 std::unique_ptr<AMDGPULibFuncImpl> Impl;
406};
407
409public:
411
412 explicit AMDGPUMangledLibFunc();
413 explicit AMDGPUMangledLibFunc(EFuncId id,
414 const AMDGPUMangledLibFunc &copyFrom);
415
416 std::string getName() const override;
417 unsigned getNumArgs() const override;
418 FunctionType *getFunctionType(Module &M) const override;
419 static StringRef getUnmangledName(StringRef MangledName);
420
421 bool parseFuncName(StringRef &mangledName) override;
422
423 // Methods for support type inquiry through isa, cast, and dyn_cast:
424 static bool classof(const AMDGPULibFuncImpl *F) { return F->isMangled(); }
425
426 std::string mangle() const override;
427
428private:
429 std::string mangleNameItanium() const;
430
431 std::string mangleName(StringRef Name) const;
432 bool parseUnmangledName(StringRef MangledName);
433
434 template <typename Stream> void writeName(Stream &OS) const;
435};
436
438 FunctionType *FuncTy;
439
440public:
441 explicit AMDGPUUnmangledLibFunc();
443 Name = std::string(FName);
444 FuncTy = FT;
445 }
446 std::string getName() const override { return Name; }
447 unsigned getNumArgs() const override;
448 FunctionType *getFunctionType(Module &M) const override { return FuncTy; }
449
450 bool parseFuncName(StringRef &Name) override;
451
452 // Methods for support type inquiry through isa, cast, and dyn_cast:
453 static bool classof(const AMDGPULibFuncImpl *F) { return !F->isMangled(); }
454
455 std::string mangle() const override { return Name; }
456
457 void setFunctionType(FunctionType *FT) { FuncTy = FT; }
458};
459}
460#endif // _AMDGPU_LIBFUNC_H_
#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 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:165
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
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
Definition: BitVector.h:858
#define N
void mangleItanium(Stream &os)
Definition: regcomp.c:192