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
5#include "proteus/Error.h"
6#include "proteus/Logger.hpp"
7
8#include <llvm/ADT/SetVector.h>
9#include <llvm/Demangle/Demangle.h>
10#include <llvm/IR/Module.h>
11#include <llvm/TargetParser/Triple.h>
12
13#define DEBUG_TYPE "proteus-pass"
14#define DEBUG(x) \
15 do \
16 if (isDebugOutputEnabled()) { \
17 x; \
18 } \
19 while (0);
20
21#if PROTEUS_ENABLE_HIP
22constexpr char const *RegisterFunctionName = "__hipRegisterFunction";
23constexpr char const *LaunchFunctionName = "hipLaunchKernel";
24constexpr char const *RegisterVarName = "__hipRegisterVar";
25constexpr char const *RegisterFatBinaryName = "__hipRegisterFatBinary";
26#elif PROTEUS_ENABLE_CUDA
27constexpr char const *RegisterFunctionName = "__cudaRegisterFunction";
28constexpr char const *LaunchFunctionName = "cudaLaunchKernel";
29constexpr char const *RegisterVarName = "__cudaRegisterVar";
30constexpr char const *RegisterFatBinaryName = "__cudaRegisterFatBinary";
31#else
32constexpr char const *RegisterFunctionName = nullptr;
33constexpr char const *LaunchFunctionName = nullptr;
34constexpr char const *RegisterVarName = nullptr;
35constexpr char const *RegisterFatBinaryName = nullptr;
36#endif
37
38namespace proteus {
39
40using namespace llvm;
41
46
47struct ModuleInfo {
48 const Module &M;
49 ModuleInfo(const Module &M) : M(M) {}
50};
51
52inline bool isDebugOutputEnabled() {
53 auto GetEnvVar = []() {
54 const char *EnvValue = std::getenv("PROTEUS_DEBUG_OUTPUT");
55 return EnvValue ? static_cast<bool>(std::stoi(EnvValue)) : false;
56 };
57
58 static bool IsEnabled = GetEnvVar();
59 return IsEnabled;
60}
61
62bool inline isDeviceCompilation(Module &M) {
63 Triple TargetTriple(M.getTargetTriple());
64 DEBUG(Logger::logs("proteus-pass")
65 << "TargetTriple " << M.getTargetTriple() << "\n");
66 if (TargetTriple.isNVPTX() || TargetTriple.isAMDGCN())
67 return true;
68
69 return false;
70}
71
72inline std::string getUniqueFileID(Module &M) {
73 llvm::sys::fs::UniqueID ID;
74 if (auto EC = llvm::sys::fs::getUniqueID(M.getSourceFileName(), ID))
75 reportFatalError("Could not get unique id for source file " + EC.message());
76
79 OutStr << llvm::format("%x_%x", ID.getDevice(), ID.getFile());
80
81 return std::string(Out);
82}
83
84inline bool isDeviceKernel(const Function *F) {
85 if (!F)
86 reportFatalError("Expected non-null function");
87
88#if PROTEUS_ENABLE_CUDA
89#if LLVM_VERSION_MAJOR >= 20
90 return (F->getCallingConv() == CallingConv::PTX_Kernel);
91#else
92 const Module &M = *F->getParent();
93 auto GetDeviceKernels = [&M]() {
95 NamedMDNode *MD = M.getNamedMetadata("nvvm.annotations");
96
97 if (!MD)
98 return Kernels;
99
100 for (auto *Op : MD->operands()) {
101 if (Op->getNumOperands() < 2)
102 continue;
103 MDString *KindID = dyn_cast<MDString>(Op->getOperand(1));
104 if (!KindID || KindID->getString() != "kernel")
105 continue;
106
108 mdconst::dyn_extract_or_null<Function>(Op->getOperand(0));
109 if (!KernelFn)
110 continue;
111
112 Kernels.insert(KernelFn);
113 }
114
115 return Kernels;
116 };
117
118 // Create a kernel cache per module, assumes we don't insert/remove kernels
119 // after parsing nvvm.annotations.
121 auto It = KernelCache.find(&M);
122 if (It == KernelCache.end())
123 It = KernelCache.insert({&M, GetDeviceKernels()}).first;
124 const auto &KernelSet = It->second;
125 if (KernelSet.contains(F))
126 return true;
127
128 return false;
129#endif
130#endif
131
132#if PROTEUS_ENABLE_HIP
133 return (F->getCallingConv() == CallingConv::AMDGPU_KERNEL);
134#endif
135
136 return false;
137}
138
139} // namespace proteus
140
141namespace llvm {
142
143using namespace proteus;
144
148 return K;
149 }
150
153 return K;
154 }
155
156 static unsigned getHashValue(const RuntimeConstantInfo &Val) {
157 return hash_combine(Val.ArgInfo.Type, Val.ArgInfo.Pos);
158 }
159
160 static bool isEqual(const RuntimeConstantInfo &LHS,
161 const RuntimeConstantInfo &RHS) {
162 return ((LHS.ArgInfo.Type == RHS.ArgInfo.Type) &&
163 (LHS.ArgInfo.Pos == RHS.ArgInfo.Pos));
164 }
165};
166
169 if (StubToKernelMap.contains(&Fn))
170 return true;
171
172 return false;
173}
174
175} // namespace llvm
176
177#endif
constexpr char const * LaunchFunctionName
Definition Helpers.h:33
#define DEBUG(x)
Definition Helpers.h:14
constexpr char const * RegisterFunctionName
Definition Helpers.h:32
constexpr char const * RegisterVarName
Definition Helpers.h:34
constexpr char const * RegisterFatBinaryName
Definition Helpers.h:35
static llvm::raw_ostream & logs(const std::string &Name)
Definition Logger.hpp:19
Definition Helpers.h:141
bool isDeviceKernelHostStub(const DenseMap< Value *, GlobalVariable * > &StubToKernelMap, Function &Fn)
Definition Helpers.h:167
Definition ObjectCacheChain.cpp:26
@ END
Definition CompilerInterfaceTypes.h:35
@ BEGIN
Definition CompilerInterfaceTypes.h:21
bool isDeviceKernel(const Function *F)
Definition Helpers.h:84
void reportFatalError(const llvm::Twine &Reason, const char *FILE, unsigned Line)
Definition Error.cpp:14
T getRuntimeConstantValue(void *Arg)
Definition CompilerInterfaceRuntimeConstantInfo.h:113
std::string getUniqueFileID(Module &M)
Definition Helpers.h:72
bool isDebugOutputEnabled()
Definition Helpers.h:52
bool isDeviceCompilation(Module &M)
Definition Helpers.h:62
static bool isEqual(const RuntimeConstantInfo &LHS, const RuntimeConstantInfo &RHS)
Definition Helpers.h:160
static unsigned getHashValue(const RuntimeConstantInfo &Val)
Definition Helpers.h:156
static RuntimeConstantInfo getEmptyKey()
Definition Helpers.h:146
static RuntimeConstantInfo getTombstoneKey()
Definition Helpers.h:151
Definition Helpers.h:42
std::string ModuleIR
Definition Helpers.h:44
SmallSetVector< RuntimeConstantInfo, 16 > ConstantArgs
Definition Helpers.h:43
Definition Helpers.h:47
ModuleInfo(const Module &M)
Definition Helpers.h:49
const Module & M
Definition Helpers.h:48
Definition CompilerInterfaceRuntimeConstantInfo.h:60