Proteus
Programmable JIT compilation and optimization for C/C++ using LLVM
Loading...
Searching...
No Matches
Helpers.h
Go to the documentation of this file.
1#ifndef PROTEUS_PASS_HELPERS_H
2#define PROTEUS_PASS_HELPERS_H
3
4#include <llvm/ADT/SetVector.h>
5#include <llvm/Demangle/Demangle.h>
6#include <llvm/IR/Module.h>
7#include <llvm/TargetParser/Triple.h>
8
10#include "proteus/Error.h"
11#include "proteus/Logger.hpp"
12
13#define DEBUG_TYPE "proteus-pass"
14#ifdef PROTEUS_ENABLE_DEBUG
15#define DEBUG(x) x
16#else
17#define DEBUG(x)
18#endif
19
20#if PROTEUS_ENABLE_HIP
21constexpr char const *RegisterFunctionName = "__hipRegisterFunction";
22constexpr char const *LaunchFunctionName = "hipLaunchKernel";
23constexpr char const *RegisterVarName = "__hipRegisterVar";
24constexpr char const *RegisterFatBinaryName = "__hipRegisterFatBinary";
25#elif PROTEUS_ENABLE_CUDA
26constexpr char const *RegisterFunctionName = "__cudaRegisterFunction";
27constexpr char const *LaunchFunctionName = "cudaLaunchKernel";
28constexpr char const *RegisterVarName = "__cudaRegisterVar";
29constexpr char const *RegisterFatBinaryName = "__cudaRegisterFatBinary";
30#else
31constexpr char const *RegisterFunctionName = nullptr;
32constexpr char const *LaunchFunctionName = nullptr;
33constexpr char const *RegisterVarName = nullptr;
34constexpr char const *RegisterFatBinaryName = nullptr;
35#endif
36
37namespace proteus {
38
39using namespace llvm;
40
42 SmallSetVector<RuntimeConstantInfo, 16> ConstantArgs;
43 std::string ModuleIR;
44};
45
46struct ModuleInfo {
47 const Module &M;
48 ModuleInfo(const Module &M) : M(M) {}
49};
50
51bool inline isDeviceCompilation(Module &M) {
52 Triple TargetTriple(M.getTargetTriple());
53 DEBUG(Logger::logs("proteus-pass")
54 << "TargetTriple " << M.getTargetTriple() << "\n");
55 if (TargetTriple.isNVPTX() || TargetTriple.isAMDGCN())
56 return true;
57
58 return false;
59}
60
61inline std::string getUniqueFileID(Module &M) {
62 llvm::sys::fs::UniqueID ID;
63 if (auto EC = llvm::sys::fs::getUniqueID(M.getSourceFileName(), ID))
64 PROTEUS_FATAL_ERROR("Could not get unique id for source file " +
65 EC.message());
66
67 SmallString<64> Out;
68 llvm::raw_svector_ostream OutStr(Out);
69 OutStr << llvm::format("%x_%x", ID.getDevice(), ID.getFile());
70
71 return std::string(Out);
72}
73
74} // namespace proteus
75
76namespace llvm {
77
78using namespace proteus;
79
80template <> struct DenseMapInfo<RuntimeConstantInfo> {
83 return K;
84 }
85
88 return K;
89 }
90
91 static unsigned getHashValue(const RuntimeConstantInfo &Val) {
92 return hash_combine(Val.ArgInfo.Type, Val.ArgInfo.Pos);
93 }
94
95 static bool isEqual(const RuntimeConstantInfo &LHS,
96 const RuntimeConstantInfo &RHS) {
97 return ((LHS.ArgInfo.Type == RHS.ArgInfo.Type) &&
98 (LHS.ArgInfo.Pos == RHS.ArgInfo.Pos));
99 }
100};
101
102} // namespace llvm
103
104#endif
#define PROTEUS_FATAL_ERROR(x)
Definition Error.h:7
constexpr char const * LaunchFunctionName
Definition Helpers.h:32
#define DEBUG(x)
Definition Helpers.h:17
constexpr char const * RegisterFunctionName
Definition Helpers.h:31
constexpr char const * RegisterVarName
Definition Helpers.h:33
constexpr char const * RegisterFatBinaryName
Definition Helpers.h:34
static llvm::raw_ostream & logs(const std::string &Name)
Definition Logger.hpp:19
Definition Helpers.h:76
Definition BuiltinsCUDA.cpp:4
@ END
Definition CompilerInterfaceTypes.h:35
@ BEGIN
Definition CompilerInterfaceTypes.h:21
std::string getUniqueFileID(Module &M)
Definition Helpers.h:61
bool isDeviceCompilation(Module &M)
Definition Helpers.h:51
static bool isEqual(const RuntimeConstantInfo &LHS, const RuntimeConstantInfo &RHS)
Definition Helpers.h:95
static unsigned getHashValue(const RuntimeConstantInfo &Val)
Definition Helpers.h:91
static RuntimeConstantInfo getEmptyKey()
Definition Helpers.h:81
static RuntimeConstantInfo getTombstoneKey()
Definition Helpers.h:86
Definition Helpers.h:41
std::string ModuleIR
Definition Helpers.h:43
SmallSetVector< RuntimeConstantInfo, 16 > ConstantArgs
Definition Helpers.h:42
Definition Helpers.h:46
ModuleInfo(const Module &M)
Definition Helpers.h:48
const Module & M
Definition Helpers.h:47
int32_t Pos
Definition CompilerInterfaceRuntimeConstantInfo.h:18
RuntimeConstantType Type
Definition CompilerInterfaceRuntimeConstantInfo.h:17
Definition CompilerInterfaceRuntimeConstantInfo.h:58
RuntimeConstantArgInfo ArgInfo
Definition CompilerInterfaceRuntimeConstantInfo.h:59