1 //===----- LLJIT.h -- An ORC-based JIT for compiling LLVM IR ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for 3Bdetails.
8 //===----------------------------------------------------------------------===//
10 // An ORC-based JIT for compiling LLVM IR.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_EXECUTIONENGINE_ORC_LLJIT_H
15 #define LLVM_EXECUTIONENGINE_ORC_LLJIT_H
17 #include "llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h"
18 #include "llvm/ExecutionEngine/Orc/CompileUtils.h"
19 #include "llvm/ExecutionEngine/Orc/ExecutionUtils.h"
20 #include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
21 #include "llvm/ExecutionEngine/Orc/IRTransformLayer.h"
22 #include "llvm/ExecutionEngine/Orc/ObjectTransformLayer.h"
23 #include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
24 #include "llvm/Target/TargetMachine.h"
29 /// A pre-fabricated ORC JIT stack that can serve as an alternative to MCJIT.
32 /// Create an LLJIT instance.
33 static Expected<std::unique_ptr<LLJIT>>
34 Create(std::unique_ptr<ExecutionSession> ES,
35 std::unique_ptr<TargetMachine> TM, DataLayout DL);
37 /// Returns a reference to the ExecutionSession for this JIT instance.
38 ExecutionSession &getExecutionSession() { return *ES; }
40 /// Returns a reference to the VSO representing the JIT'd main program.
41 VSO &getMainVSO() { return Main; }
43 /// Convenience method for defining an absolute symbol.
44 Error defineAbsolute(StringRef Name, JITEvaluatedSymbol Address);
46 /// Adds an IR module to the given VSO.
47 Error addIRModule(VSO &V, std::unique_ptr<Module> M);
49 /// Adds an IR module to the Main VSO.
50 Error addIRModule(std::unique_ptr<Module> M) {
51 return addIRModule(Main, std::move(M));
54 /// Look up a symbol in VSO V by the symbol's linker-mangled name (to look up
55 /// symbols based on their IR name use the lookup function instead).
56 Expected<JITEvaluatedSymbol> lookupLinkerMangled(VSO &V, StringRef Name);
58 /// Look up a symbol in the main VSO by the symbol's linker-mangled name (to
59 /// look up symbols based on their IR name use the lookup function instead).
60 Expected<JITEvaluatedSymbol> lookupLinkerMangled(StringRef Name) {
61 return lookupLinkerMangled(Main, Name);
64 /// Look up a symbol in VSO V based on its IR symbol name.
65 Expected<JITEvaluatedSymbol> lookup(VSO &V, StringRef UnmangledName) {
66 return lookupLinkerMangled(V, mangle(UnmangledName));
69 /// Look up a symbol in the main VSO based on its IR symbol name.
70 Expected<JITEvaluatedSymbol> lookup(StringRef UnmangledName) {
71 return lookup(Main, UnmangledName);
74 /// Runs all not-yet-run static constructors.
75 Error runConstructors() { return CtorRunner.run(); }
77 /// Runs all not-yet-run static destructors.
78 Error runDestructors() { return DtorRunner.run(); }
81 LLJIT(std::unique_ptr<ExecutionSession> ES, std::unique_ptr<TargetMachine> TM,
84 std::shared_ptr<RuntimeDyld::MemoryManager> getMemoryManager(VModuleKey K);
86 std::string mangle(StringRef UnmangledName);
88 Error applyDataLayout(Module &M);
90 void recordCtorDtors(Module &M);
92 std::unique_ptr<ExecutionSession> ES;
95 std::unique_ptr<TargetMachine> TM;
98 RTDyldObjectLinkingLayer2 ObjLinkingLayer;
99 IRCompileLayer2 CompileLayer;
101 CtorDtorRunner2 CtorRunner, DtorRunner;
104 /// An extended version of LLJIT that supports lazy function-at-a-time
105 /// compilation of LLVM IR.
106 class LLLazyJIT : public LLJIT {
108 /// Create an LLLazyJIT instance.
109 static Expected<std::unique_ptr<LLLazyJIT>>
110 Create(std::unique_ptr<ExecutionSession> ES,
111 std::unique_ptr<TargetMachine> TM, DataLayout DL, LLVMContext &Ctx);
113 /// Set an IR transform (e.g. pass manager pipeline) to run on each function
114 /// when it is compiled.
115 void setLazyCompileTransform(IRTransformLayer2::TransformFunction Transform) {
116 TransformLayer.setTransform(std::move(Transform));
119 /// Add a module to be lazily compiled to VSO V.
120 Error addLazyIRModule(VSO &V, std::unique_ptr<Module> M);
122 /// Add a module to be lazily compiled to the main VSO.
123 Error addLazyIRModule(std::unique_ptr<Module> M) {
124 return addLazyIRModule(Main, std::move(M));
128 LLLazyJIT(std::unique_ptr<ExecutionSession> ES,
129 std::unique_ptr<TargetMachine> TM, DataLayout DL, LLVMContext &Ctx,
130 std::unique_ptr<JITCompileCallbackManager> CCMgr,
131 std::function<std::unique_ptr<IndirectStubsManager>()> ISMBuilder);
133 std::unique_ptr<JITCompileCallbackManager> CCMgr;
134 std::function<std::unique_ptr<IndirectStubsManager>()> ISMBuilder;
136 IRTransformLayer2 TransformLayer;
137 CompileOnDemandLayer2 CODLayer;
140 } // End namespace orc
141 } // End namespace llvm
143 #endif // LLVM_EXECUTIONENGINE_ORC_LLJIT_H