• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===----- LLJIT.h -- An ORC-based JIT for compiling LLVM IR ----*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for 3Bdetails.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // An ORC-based JIT for compiling LLVM IR.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_EXECUTIONENGINE_ORC_LLJIT_H
15 #define LLVM_EXECUTIONENGINE_ORC_LLJIT_H
16 
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"
25 
26 namespace llvm {
27 namespace orc {
28 
29 /// A pre-fabricated ORC JIT stack that can serve as an alternative to MCJIT.
30 class LLJIT {
31 public:
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);
36 
37   /// Returns a reference to the ExecutionSession for this JIT instance.
getExecutionSession()38   ExecutionSession &getExecutionSession() { return *ES; }
39 
40   /// Returns a reference to the VSO representing the JIT'd main program.
getMainVSO()41   VSO &getMainVSO() { return Main; }
42 
43   /// Convenience method for defining an absolute symbol.
44   Error defineAbsolute(StringRef Name, JITEvaluatedSymbol Address);
45 
46   /// Adds an IR module to the given VSO.
47   Error addIRModule(VSO &V, std::unique_ptr<Module> M);
48 
49   /// Adds an IR module to the Main VSO.
addIRModule(std::unique_ptr<Module> M)50   Error addIRModule(std::unique_ptr<Module> M) {
51     return addIRModule(Main, std::move(M));
52   }
53 
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);
57 
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).
lookupLinkerMangled(StringRef Name)60   Expected<JITEvaluatedSymbol> lookupLinkerMangled(StringRef Name) {
61     return lookupLinkerMangled(Main, Name);
62   }
63 
64   /// Look up a symbol in VSO V based on its IR symbol name.
lookup(VSO & V,StringRef UnmangledName)65   Expected<JITEvaluatedSymbol> lookup(VSO &V, StringRef UnmangledName) {
66     return lookupLinkerMangled(V, mangle(UnmangledName));
67   }
68 
69   /// Look up a symbol in the main VSO based on its IR symbol name.
lookup(StringRef UnmangledName)70   Expected<JITEvaluatedSymbol> lookup(StringRef UnmangledName) {
71     return lookup(Main, UnmangledName);
72   }
73 
74   /// Runs all not-yet-run static constructors.
runConstructors()75   Error runConstructors() { return CtorRunner.run(); }
76 
77   /// Runs all not-yet-run static destructors.
runDestructors()78   Error runDestructors() { return DtorRunner.run(); }
79 
80 protected:
81   LLJIT(std::unique_ptr<ExecutionSession> ES, std::unique_ptr<TargetMachine> TM,
82         DataLayout DL);
83 
84   std::shared_ptr<RuntimeDyld::MemoryManager> getMemoryManager(VModuleKey K);
85 
86   std::string mangle(StringRef UnmangledName);
87 
88   Error applyDataLayout(Module &M);
89 
90   void recordCtorDtors(Module &M);
91 
92   std::unique_ptr<ExecutionSession> ES;
93   VSO &Main;
94 
95   std::unique_ptr<TargetMachine> TM;
96   DataLayout DL;
97 
98   RTDyldObjectLinkingLayer2 ObjLinkingLayer;
99   IRCompileLayer2 CompileLayer;
100 
101   CtorDtorRunner2 CtorRunner, DtorRunner;
102 };
103 
104 /// An extended version of LLJIT that supports lazy function-at-a-time
105 /// compilation of LLVM IR.
106 class LLLazyJIT : public LLJIT {
107 public:
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);
112 
113   /// Set an IR transform (e.g. pass manager pipeline) to run on each function
114   /// when it is compiled.
setLazyCompileTransform(IRTransformLayer2::TransformFunction Transform)115   void setLazyCompileTransform(IRTransformLayer2::TransformFunction Transform) {
116     TransformLayer.setTransform(std::move(Transform));
117   }
118 
119   /// Add a module to be lazily compiled to VSO V.
120   Error addLazyIRModule(VSO &V, std::unique_ptr<Module> M);
121 
122   /// Add a module to be lazily compiled to the main VSO.
addLazyIRModule(std::unique_ptr<Module> M)123   Error addLazyIRModule(std::unique_ptr<Module> M) {
124     return addLazyIRModule(Main, std::move(M));
125   }
126 
127 private:
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);
132 
133   std::unique_ptr<JITCompileCallbackManager> CCMgr;
134   std::function<std::unique_ptr<IndirectStubsManager>()> ISMBuilder;
135 
136   IRTransformLayer2 TransformLayer;
137   CompileOnDemandLayer2 CODLayer;
138 };
139 
140 } // End namespace orc
141 } // End namespace llvm
142 
143 #endif // LLVM_EXECUTIONENGINE_ORC_LLJIT_H
144