forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jitlayers.h
209 lines (184 loc) · 7.49 KB
/
jitlayers.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// This file is a part of Julia. License is MIT: https://julialang.org/license
#include <llvm/IR/LLVMContext.h>
#include <llvm/IR/Constants.h>
#include <llvm/IR/Module.h>
#include <llvm/IR/Value.h>
#include <llvm/ExecutionEngine/SectionMemoryManager.h>
#include "llvm/ExecutionEngine/Orc/CompileUtils.h"
#include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
#include "llvm/ExecutionEngine/Orc/LambdaResolver.h"
#include "llvm/ExecutionEngine/Orc/LazyEmittingLayer.h"
#include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
#include "llvm/ExecutionEngine/JITEventListener.h"
#if JL_LLVM_VERSION < 70000
#include "llvm/ExecutionEngine/ObjectMemoryBuffer.h"
#endif
#include "llvm/IR/LegacyPassManager.h"
extern legacy::PassManager *jl_globalPM;
#include <llvm/Target/TargetMachine.h>
#include "julia_assert.h"
extern "C" {
extern int globalUnique;
}
extern TargetMachine *jl_TargetMachine;
extern Module *shadow_output;
extern bool imaging_mode;
#if defined(_OS_WINDOWS_) && defined(_CPU_X86_64_)
extern Function *juliapersonality_func;
#endif
typedef struct {Value *gv; int32_t index;} jl_value_llvm; // uses 1-based indexing
void addTargetPasses(legacy::PassManagerBase *PM, TargetMachine *TM);
void addOptimizationPasses(legacy::PassManagerBase *PM, int opt_level, bool lower_intrinsics=true, bool dump_native=false);
void** jl_emit_and_add_to_shadow(GlobalVariable *gv, void *gvarinit = NULL);
void* jl_get_globalvar(GlobalVariable *gv);
GlobalVariable *jl_get_global_for(const char *cname, void *addr, Module *M);
void jl_add_to_shadow(Module *m);
void jl_init_function(Function *f);
bool jl_can_finalize_function(StringRef F);
void jl_finalize_function(StringRef F);
void jl_finalize_module(Module *m, bool shadow);
// Connect Modules via prototypes, each owned by module `M`
static inline GlobalVariable *global_proto(GlobalVariable *G, Module *M = NULL)
{
// Copy the GlobalVariable, but without the initializer, so it becomes a declaration
GlobalVariable *proto = new GlobalVariable(G->getType()->getElementType(),
G->isConstant(), GlobalVariable::ExternalLinkage,
NULL, G->getName(), G->getThreadLocalMode());
proto->copyAttributesFrom(G);
// DLLImport only needs to be set for the shadow module
// it just gets annoying in the JIT
proto->setDLLStorageClass(GlobalValue::DefaultStorageClass);
if (M)
M->getGlobalList().push_back(proto);
return proto;
}
static inline GlobalVariable *prepare_global_in(Module *M, GlobalVariable *G)
{
if (G->getParent() == M)
return G;
GlobalValue *local = M->getNamedValue(G->getName());
if (!local) {
local = global_proto(G, M);
}
return cast<GlobalVariable>(local);
}
void add_named_global(GlobalObject *gv, void *addr, bool dllimport);
template<typename T>
static inline void add_named_global(GlobalObject *gv, T *addr, bool dllimport = true)
{
// cast through integer to avoid c++ pedantic warning about casting between
// data and code pointers
add_named_global(gv, (void*)(uintptr_t)addr, dllimport);
}
void jl_init_jit(Type *T_pjlvalue_);
typedef JITSymbol JL_JITSymbol;
// The type that is similar to SymbolInfo on LLVM 4.0 is actually
// `JITEvaluatedSymbol`. However, we only use this type when a JITSymbol
// is expected.
typedef JITSymbol JL_SymbolInfo;
#if JL_LLVM_VERSION >= 70000
using RTDyldObjHandleT = orc::VModuleKey;
#else
using RTDyldObjHandleT = orc::RTDyldObjectLinkingLayerBase::ObjHandleT;
#endif
#if JL_LLVM_VERSION >= 70000
using CompilerResultT = std::unique_ptr<llvm::MemoryBuffer>;
#else
using CompilerResultT = object::OwningBinary<object::ObjectFile>;
#endif
class JuliaOJIT {
// Custom object emission notification handler for the JuliaOJIT
class DebugObjectRegistrar {
public:
DebugObjectRegistrar(JuliaOJIT &JIT);
template <typename ObjSetT, typename LoadResult>
void operator()(RTDyldObjHandleT H, const ObjSetT &Object, const LoadResult &LOS);
private:
template <typename ObjT, typename LoadResult>
void registerObject(RTDyldObjHandleT H, const ObjT &Obj, const LoadResult &LO);
std::vector<object::OwningBinary<object::ObjectFile>> SavedObjects;
std::unique_ptr<JITEventListener> JuliaListener;
JuliaOJIT &JIT;
};
struct CompilerT {
CompilerT(JuliaOJIT *pjit)
: jit(*pjit)
{}
CompilerResultT operator()(Module &M);
private:
JuliaOJIT &jit;
};
public:
#if JL_LLVM_VERSION >= 80000
typedef orc::LegacyRTDyldObjectLinkingLayer ObjLayerT;
typedef orc::LegacyIRCompileLayer<ObjLayerT,CompilerT> CompileLayerT;
typedef orc::VModuleKey ModuleHandleT;
#elif JL_LLVM_VERSION >= 70000
typedef orc::RTDyldObjectLinkingLayer ObjLayerT;
typedef orc::IRCompileLayer<ObjLayerT,CompilerT> CompileLayerT;
typedef orc::VModuleKey ModuleHandleT;
#else
typedef orc::RTDyldObjectLinkingLayer ObjLayerT;
typedef orc::IRCompileLayer<ObjLayerT,CompilerT> CompileLayerT;
typedef CompileLayerT::ModuleHandleT ModuleHandleT;
#endif
typedef StringMap<void*> SymbolTableT;
typedef object::OwningBinary<object::ObjectFile> OwningObj;
JuliaOJIT(TargetMachine &TM);
void RegisterJITEventListener(JITEventListener *L);
std::vector<JITEventListener *> EventListeners;
void NotifyFinalizer(RTDyldObjHandleT Key,
const object::ObjectFile &Obj,
const RuntimeDyld::LoadedObjectInfo &LoadedObjectInfo);
void addGlobalMapping(StringRef Name, uint64_t Addr);
void addGlobalMapping(const GlobalValue *GV, void *Addr);
void *getPointerToGlobalIfAvailable(StringRef S);
void *getPointerToGlobalIfAvailable(const GlobalValue *GV);
void addModule(std::unique_ptr<Module> M);
void removeModule(ModuleHandleT H);
JL_JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly);
JL_JITSymbol findUnmangledSymbol(const std::string Name);
JL_JITSymbol resolveSymbol(const std::string& Name);
uint64_t getGlobalValueAddress(const std::string &Name);
uint64_t getFunctionAddress(const std::string &Name);
Function *FindFunctionNamed(const std::string &Name);
const DataLayout& getDataLayout() const;
const Triple& getTargetTriple() const;
private:
std::string getMangledName(const std::string &Name);
std::string getMangledName(const GlobalValue *GV);
TargetMachine &TM;
const DataLayout DL;
// Should be big enough that in the common case, The
// object fits in its entirety
SmallVector<char, 4096> ObjBufferSV;
raw_svector_ostream ObjStream;
legacy::PassManager PM;
MCContext *Ctx;
std::shared_ptr<RTDyldMemoryManager> MemMgr;
DebugObjectRegistrar registrar;
#if JL_LLVM_VERSION >= 70000
llvm::orc::ExecutionSession ES;
std::shared_ptr<llvm::orc::SymbolResolver> SymbolResolver;
#endif
ObjLayerT ObjectLayer;
CompileLayerT CompileLayer;
SymbolTableT GlobalSymbolTable;
SymbolTableT LocalSymbolTable;
};
extern JuliaOJIT *jl_ExecutionEngine;
JL_DLLEXPORT extern LLVMContext &jl_LLVMContext;
Pass *createLowerPTLSPass(bool imaging_mode);
Pass *createCombineMulAddPass();
Pass *createFinalLowerGCPass();
Pass *createLateLowerGCFramePass();
Pass *createLowerExcHandlersPass();
Pass *createGCInvariantVerifierPass(bool Strong);
Pass *createPropagateJuliaAddrspaces();
Pass *createMultiVersioningPass();
Pass *createAllocOptPass();
// Whether the Function is an llvm or julia intrinsic.
static inline bool isIntrinsicFunction(Function *F)
{
return F->isIntrinsic() || F->getName().startswith("julia.");
}