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#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 PROTEUS_FATAL_ERROR("Could not get unique id for source file " +
76 EC.message());
77
80 OutStr << llvm::format("%x_%x", ID.getDevice(), ID.getFile());
81
82 return std::string(Out);
83}
84
85inline bool isDeviceKernel(const Function *F) {
86 if (!F)
87 PROTEUS_FATAL_ERROR("Expected non-null function");
88
89#if PROTEUS_ENABLE_CUDA
90 const Module &M = *F->getParent();
91 auto GetDeviceKernels = [&M]() {
93 NamedMDNode *MD = M.getNamedMetadata("nvvm.annotations");
94
95 if (!MD)
96 return Kernels;
97
98 for (auto *Op : MD->operands()) {
99 if (Op->getNumOperands() < 2)
100 continue;
101 MDString *KindID = dyn_cast<MDString>(Op->getOperand(1));
102 if (!KindID || KindID->getString() != "kernel")
103 continue;
104
106 mdconst::dyn_extract_or_null<Function>(Op->getOperand(0));
107 if (!KernelFn)
108 continue;
109
110 Kernels.insert(KernelFn);
111 }
112
113 return Kernels;
114 };
115
116 // Create a kernel cache per module, assumes we don't insert/remove kernels
117 // after parsing nvvm.annotations.
119 auto It = KernelCache.find(&M);
120 if (It == KernelCache.end())
121 It = KernelCache.insert({&M, GetDeviceKernels()}).first;
122 const auto &KernelSet = It->second;
123 if (KernelSet.contains(F))
124 return true;
125
126 return false;
127#endif
128
129#if PROTEUS_ENABLE_HIP
130 return (F->getCallingConv() == CallingConv::AMDGPU_KERNEL);
131#endif
132
133 return false;
134}
135
136} // namespace proteus
137
138namespace llvm {
139
140using namespace proteus;
141
145 return K;
146 }
147
150 return K;
151 }
152
153 static unsigned getHashValue(const RuntimeConstantInfo &Val) {
154 return hash_combine(Val.ArgInfo.Type, Val.ArgInfo.Pos);
155 }
156
157 static bool isEqual(const RuntimeConstantInfo &LHS,
158 const RuntimeConstantInfo &RHS) {
159 return ((LHS.ArgInfo.Type == RHS.ArgInfo.Type) &&
160 (LHS.ArgInfo.Pos == RHS.ArgInfo.Pos));
161 }
162};
163
164} // namespace llvm
165
166#endif
#define PROTEUS_FATAL_ERROR(x)
Definition Error.h:7
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:138
Definition BuiltinsCUDA.cpp:4
@ END
Definition CompilerInterfaceTypes.h:35
@ BEGIN
Definition CompilerInterfaceTypes.h:21
bool isDeviceKernel(const Function *F)
Definition Helpers.h:85
T getRuntimeConstantValue(void *Arg)
Definition CompilerInterfaceRuntimeConstantInfo.h:114
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:157
static unsigned getHashValue(const RuntimeConstantInfo &Val)
Definition Helpers.h:153
static RuntimeConstantInfo getEmptyKey()
Definition Helpers.h:143
static RuntimeConstantInfo getTombstoneKey()
Definition Helpers.h:148
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:61