LLVM  7.0.0
LLJIT.h
Go to the documentation of this file.
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 
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.
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.
39 
40  /// Returns a reference to the VSO representing the JIT'd main program.
41  VSO &getMainVSO() { return Main; }
42 
43  /// Convenience method for defining an absolute symbol.
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.
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).
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).
61  return lookupLinkerMangled(Main, Name);
62  }
63 
64  /// Look up a symbol in VSO V based on its IR symbol name.
66  return lookupLinkerMangled(V, mangle(UnmangledName));
67  }
68 
69  /// Look up a symbol in the main VSO based on its IR symbol name.
71  return lookup(Main, UnmangledName);
72  }
73 
74  /// Runs all not-yet-run static constructors.
76 
77  /// Runs all not-yet-run static destructors.
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 
89 
90  void recordCtorDtors(Module &M);
91 
92  std::unique_ptr<ExecutionSession> ES;
94 
95  std::unique_ptr<TargetMachine> TM;
97 
100 
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.
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.
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.
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
ExecutionSession & getExecutionSession()
Returns a reference to the ExecutionSession for this JIT instance.
Definition: LLJIT.h:38
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:111
Error addIRModule(std::unique_ptr< Module > M)
Adds an IR module to the Main VSO.
Definition: LLJIT.h:50
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
std::function< Expected< std::unique_ptr< Module > >(std::unique_ptr< Module >)> TransformFunction
Error defineAbsolute(StringRef Name, JITEvaluatedSymbol Address)
Convenience method for defining an absolute symbol.
Definition: LLJIT.cpp:25
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:63
A symbol table that supports asynchoronous symbol queries.
Definition: Core.h:544
uint64_t VModuleKey
VModuleKey provides a unique identifier (allocated and managed by ExecutionSessions) for a module add...
Definition: Core.h:40
Error runConstructors()
Runs all not-yet-run static constructors.
Definition: LLJIT.h:75
CtorDtorRunner2 DtorRunner
Definition: LLJIT.h:101
IRCompileLayer2 CompileLayer
Definition: LLJIT.h:99
Tagged union holding either a T or a Error.
Definition: CachePruning.h:23
RTDyldObjectLinkingLayer2 ObjLinkingLayer
Definition: LLJIT.h:98
std::shared_ptr< RuntimeDyld::MemoryManager > getMemoryManager(VModuleKey K)
Definition: LLJIT.cpp:56
VSO & getMainVSO()
Returns a reference to the VSO representing the JIT&#39;d main program.
Definition: LLJIT.h:41
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:69
Error runDestructors()
Runs all not-yet-run static destructors.
Definition: LLJIT.h:78
Expected< JITEvaluatedSymbol > lookup(StringRef UnmangledName)
Look up a symbol in the main VSO based on its IR symbol name.
Definition: LLJIT.h:70
VSO & Main
Definition: LLJIT.h:93
std::string mangle(StringRef UnmangledName)
Definition: LLJIT.cpp:60
LLJIT(std::unique_ptr< ExecutionSession > ES, std::unique_ptr< TargetMachine > TM, DataLayout DL)
Definition: LLJIT.cpp:46
Expected< JITEvaluatedSymbol > lookupLinkerMangled(VSO &V, StringRef Name)
Look up a symbol in VSO V by the symbol&#39;s linker-mangled name (to look up symbols based on their IR n...
Definition: LLJIT.cpp:41
CtorDtorRunner2 CtorRunner
Definition: LLJIT.h:101
void setLazyCompileTransform(IRTransformLayer2::TransformFunction Transform)
Set an IR transform (e.g.
Definition: LLJIT.h:115
std::unique_ptr< ExecutionSession > ES
Definition: LLJIT.h:92
void recordCtorDtors(Module &M)
Definition: LLJIT.cpp:81
Expected< JITEvaluatedSymbol > lookupLinkerMangled(StringRef Name)
Look up a symbol in the main VSO by the symbol&#39;s linker-mangled name (to look up symbols based on the...
Definition: LLJIT.h:60
std::unique_ptr< TargetMachine > TM
Definition: LLJIT.h:95
A pre-fabricated ORC JIT stack that can serve as an alternative to MCJIT.
Definition: LLJIT.h:30
An ExecutionSession represents a running JIT program.
Definition: Core.h:735
An extended version of LLJIT that supports lazy function-at-a-time compilation of LLVM IR...
Definition: LLJIT.h:106
Represents a symbol that has been evaluated to an address already.
Definition: JITSymbol.h:157
static Expected< std::unique_ptr< LLJIT > > Create(std::unique_ptr< ExecutionSession > ES, std::unique_ptr< TargetMachine > TM, DataLayout DL)
Create an LLJIT instance.
Definition: LLJIT.cpp:19
Error applyDataLayout(Module &M)
Definition: LLJIT.cpp:69
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
Error addLazyIRModule(std::unique_ptr< Module > M)
Add a module to be lazily compiled to the main VSO.
Definition: LLJIT.h:123
print Print MemDeps of function
DataLayout DL
Definition: LLJIT.h:96
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
Expected< JITEvaluatedSymbol > lookup(VSO &V, StringRef UnmangledName)
Look up a symbol in VSO V based on its IR symbol name.
Definition: LLJIT.h:65
Error addIRModule(VSO &V, std::unique_ptr< Module > M)
Adds an IR module to the given VSO.
Definition: LLJIT.cpp:31