LLVM  9.0.0svn
LLJIT.h
Go to the documentation of this file.
1 //===----- LLJIT.h -- An ORC-based JIT for compiling LLVM IR ----*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // An ORC-based JIT for compiling LLVM IR.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_EXECUTIONENGINE_ORC_LLJIT_H
14 #define LLVM_EXECUTIONENGINE_ORC_LLJIT_H
15 
25 
26 namespace llvm {
27 namespace orc {
28 
29 class LLJITBuilderState;
30 class LLLazyJITBuilderState;
31 
32 /// A pre-fabricated ORC JIT stack that can serve as an alternative to MCJIT.
33 ///
34 /// Create instances using LLJITBuilder.
35 class LLJIT {
36  template <typename, typename, typename> friend class LLJITBuilderSetters;
37 
38 public:
40 
41  /// Destruct this instance. If a multi-threaded instance, waits for all
42  /// compile threads to complete.
43  ~LLJIT();
44 
45  /// Returns the ExecutionSession for this instance.
47 
48  /// Returns a reference to the DataLayout for this instance.
49  const DataLayout &getDataLayout() const { return DL; }
50 
51  /// Returns a reference to the JITDylib representing the JIT'd main program.
52  JITDylib &getMainJITDylib() { return Main; }
53 
54  /// Create a new JITDylib with the given name and return a reference to it.
55  JITDylib &createJITDylib(std::string Name) {
56  return ES->createJITDylib(std::move(Name));
57  }
58 
59  /// Convenience method for defining an absolute symbol.
61 
62  /// Adds an IR module to the given JITDylib.
64 
65  /// Adds an IR module to the Main JITDylib.
67  return addIRModule(Main, std::move(TSM));
68  }
69 
70  /// Adds an object file to the given JITDylib.
71  Error addObjectFile(JITDylib &JD, std::unique_ptr<MemoryBuffer> Obj);
72 
73  /// Adds an object file to the given JITDylib.
74  Error addObjectFile(std::unique_ptr<MemoryBuffer> Obj) {
75  return addObjectFile(Main, std::move(Obj));
76  }
77 
78  /// Look up a symbol in JITDylib JD by the symbol's linker-mangled name (to
79  /// look up symbols based on their IR name use the lookup function instead).
81  StringRef Name);
82 
83  /// Look up a symbol in the main JITDylib by the symbol's linker-mangled name
84  /// (to look up symbols based on their IR name use the lookup function
85  /// instead).
87  return lookupLinkerMangled(Main, Name);
88  }
89 
90  /// Look up a symbol in JITDylib JD based on its IR symbol name.
92  return lookupLinkerMangled(JD, mangle(UnmangledName));
93  }
94 
95  /// Look up a symbol in the main JITDylib based on its IR symbol name.
97  return lookup(Main, UnmangledName);
98  }
99 
100  /// Runs all not-yet-run static constructors.
102 
103  /// Runs all not-yet-run static destructors.
105 
106  /// Returns a reference to the ObjLinkingLayer
108 
109 protected:
110  static std::unique_ptr<ObjectLayer>
112 
113  /// Create an LLJIT instance with a single compile thread.
114  LLJIT(LLJITBuilderState &S, Error &Err);
115 
116  std::string mangle(StringRef UnmangledName);
117 
119 
120  void recordCtorDtors(Module &M);
121 
122  std::unique_ptr<ExecutionSession> ES;
124 
126  std::unique_ptr<ThreadPool> CompileThreads;
127 
128  std::unique_ptr<ObjectLayer> ObjLinkingLayer;
129  std::unique_ptr<IRCompileLayer> CompileLayer;
130 
132 };
133 
134 /// An extended version of LLJIT that supports lazy function-at-a-time
135 /// compilation of LLVM IR.
136 class LLLazyJIT : public LLJIT {
137  template <typename, typename, typename> friend class LLJITBuilderSetters;
138 
139 public:
140 
141  /// Set an IR transform (e.g. pass manager pipeline) to run on each function
142  /// when it is compiled.
144  TransformLayer->setTransform(std::move(Transform));
145  }
146 
147  /// Sets the partition function.
148  void
150  CODLayer->setPartitionFunction(std::move(Partition));
151  }
152 
153  /// Add a module to be lazily compiled to JITDylib JD.
154  Error addLazyIRModule(JITDylib &JD, ThreadSafeModule M);
155 
156  /// Add a module to be lazily compiled to the main JITDylib.
158  return addLazyIRModule(Main, std::move(M));
159  }
160 
161 private:
162 
163  // Create a single-threaded LLLazyJIT instance.
165 
166  std::unique_ptr<LazyCallThroughManager> LCTMgr;
167  std::unique_ptr<IRTransformLayer> TransformLayer;
168  std::unique_ptr<CompileOnDemandLayer> CODLayer;
169 };
170 
172 public:
174  std::function<std::unique_ptr<ObjectLayer>(ExecutionSession &)>;
175 
176  std::unique_ptr<ExecutionSession> ES;
179  unsigned NumCompileThreads = 0;
180 
181  /// Called prior to JIT class construcion to fix up defaults.
182  Error prepareForConstruction();
183 };
184 
185 template <typename JITType, typename SetterImpl, typename State>
187 public:
188  /// Set the JITTargetMachineBuilder for this instance.
189  ///
190  /// If this method is not called, JITTargetMachineBuilder::detectHost will be
191  /// used to construct a default target machine builder for the host platform.
193  impl().JTMB = std::move(JTMB);
194  return impl();
195  }
196 
197  /// Return a reference to the JITTargetMachineBuilder.
198  ///
200  return impl().JTMB;
201  }
202 
203  /// Set an ObjectLinkingLayer creation function.
204  ///
205  /// If this method is not called, a default creation function will be used
206  /// that will construct an RTDyldObjectLinkingLayer.
209  CreateObjectLinkingLayer) {
210  impl().CreateObjectLinkingLayer = std::move(CreateObjectLinkingLayer);
211  return impl();
212  }
213 
214  /// Set the number of compile threads to use.
215  ///
216  /// If set to zero, compilation will be performed on the execution thread when
217  /// JITing in-process. If set to any other number N, a thread pool of N
218  /// threads will be created for compilation.
219  ///
220  /// If this method is not called, behavior will be as if it were called with
221  /// a zero argument.
222  SetterImpl &setNumCompileThreads(unsigned NumCompileThreads) {
223  impl().NumCompileThreads = NumCompileThreads;
224  return impl();
225  }
226 
227  /// Create an instance of the JIT.
229  if (auto Err = impl().prepareForConstruction())
230  return std::move(Err);
231 
232  Error Err = Error::success();
233  std::unique_ptr<JITType> J(new JITType(impl(), Err));
234  if (Err)
235  return std::move(Err);
236  return std::move(J);
237  }
238 
239 protected:
240  SetterImpl &impl() { return static_cast<SetterImpl &>(*this); }
241 };
242 
243 /// Constructs LLJIT instances.
245  : public LLJITBuilderState,
246  public LLJITBuilderSetters<LLJIT, LLJITBuilder, LLJITBuilderState> {};
247 
249  friend class LLLazyJIT;
250 
251 public:
253  std::function<std::unique_ptr<IndirectStubsManager>()>;
254 
256  JITTargetAddress LazyCompileFailureAddr = 0;
257  std::unique_ptr<LazyCallThroughManager> LCTMgr;
259 
260  Error prepareForConstruction();
261 };
262 
263 template <typename JITType, typename SetterImpl, typename State>
265  : public LLJITBuilderSetters<JITType, SetterImpl, State> {
266 public:
267  /// Set the address in the target address to call if a lazy compile fails.
268  ///
269  /// If this method is not called then the value will default to 0.
271  this->impl().LazyCompileFailureAddr = Addr;
272  return this->impl();
273  }
274 
275  /// Set the lazy-callthrough manager.
276  ///
277  /// If this method is not called then a default, in-process lazy callthrough
278  /// manager for the host platform will be used.
279  SetterImpl &
280  setLazyCallthroughManager(std::unique_ptr<LazyCallThroughManager> LCTMgr) {
281  this->impl().LCTMgr = std::move(LCTMgr);
282  return this->impl();
283  }
284 
285  /// Set the IndirectStubsManager builder function.
286  ///
287  /// If this method is not called then a default, in-process
288  /// IndirectStubsManager builder for the host platform will be used.
291  this->impl().ISMBuilder = std::move(ISMBuilder);
292  return this->impl();
293  }
294 };
295 
296 /// Constructs LLLazyJIT instances.
298  : public LLLazyJITBuilderState,
299  public LLLazyJITBuilderSetters<LLLazyJIT, LLLazyJITBuilder,
300  LLLazyJITBuilderState> {};
301 
302 } // End namespace orc
303 } // End namespace llvm
304 
305 #endif // LLVM_EXECUTIONENGINE_ORC_LLJIT_H
Constructs LLLazyJIT instances.
Definition: LLJIT.h:297
ExecutionSession & getExecutionSession()
Returns the ExecutionSession for this instance.
Definition: LLJIT.h:46
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:110
SetterImpl & setLazyCallthroughManager(std::unique_ptr< LazyCallThroughManager > LCTMgr)
Set the lazy-callthrough manager.
Definition: LLJIT.h:280
Error addObjectFile(std::unique_ptr< MemoryBuffer > Obj)
Adds an object file to the given JITDylib.
Definition: LLJIT.h:74
JITDylib & createJITDylib(std::string Name)
Create a new JITDylib with the given name and return a reference to it.
Definition: LLJIT.h:55
This class represents lattice values for constants.
Definition: AllocatorList.h:23
Error defineAbsolute(StringRef Name, JITEvaluatedSymbol Address)
Convenience method for defining an absolute symbol.
Definition: LLJIT.cpp:50
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:65
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
LLJIT(LLJITBuilderState &S, Error &Err)
Create an LLJIT instance with a single compile thread.
Definition: LLJIT.cpp:89
std::unique_ptr< ExecutionSession > ES
Definition: LLJIT.h:176
std::function< std::unique_ptr< ObjectLayer >(ExecutionSession &)> CreateObjectLinkingLayerFunction
Definition: LLJIT.h:174
const DataLayout & getDataLayout() const
Returns a reference to the DataLayout for this instance.
Definition: LLJIT.h:49
Error addIRModule(JITDylib &JD, ThreadSafeModule TSM)
Adds an IR module to the given JITDylib.
Definition: LLJIT.cpp:56
Optional< JITTargetMachineBuilder > & getJITTargetMachineBuilder()
Return a reference to the JITTargetMachineBuilder.
Definition: LLJIT.h:199
JITDylib & Main
Definition: LLJIT.h:123
Error runConstructors()
Runs all not-yet-run static constructors.
Definition: LLJIT.h:101
std::function< Optional< GlobalValueSet >(GlobalValueSet Requested)> PartitionFunction
Partitioning function.
Tagged union holding either a T or a Error.
Definition: CachePruning.h:22
SetterImpl & setIndirectStubsManagerBuilder(LLLazyJITBuilderState::IndirectStubsManagerBuilderFunction ISMBuilder)
Set the IndirectStubsManager builder function.
Definition: LLJIT.h:289
place backedge safepoints impl
CtorDtorRunner DtorRunner
Definition: LLJIT.h:131
CreateObjectLinkingLayerFunction CreateObjectLinkingLayer
Definition: LLJIT.h:178
ObjectLayer & getObjLinkingLayer()
Returns a reference to the ObjLinkingLayer.
Definition: LLJIT.h:107
SetterImpl & setNumCompileThreads(unsigned NumCompileThreads)
Set the number of compile threads to use.
Definition: LLJIT.h:222
uint64_t JITTargetAddress
Represents an address in the target process&#39;s address space.
Definition: JITSymbol.h:40
Error addLazyIRModule(ThreadSafeModule M)
Add a module to be lazily compiled to the main JITDylib.
Definition: LLJIT.h:157
Error runDestructors()
Runs all not-yet-run static destructors.
Definition: LLJIT.h:104
Expected< JITEvaluatedSymbol > lookup(StringRef UnmangledName)
Look up a symbol in the main JITDylib based on its IR symbol name.
Definition: LLJIT.h:96
std::string mangle(StringRef UnmangledName)
Definition: LLJIT.cpp:142
std::unique_ptr< IRCompileLayer > CompileLayer
Definition: LLJIT.h:129
An LLVM Module together with a shared ThreadSafeContext.
CtorDtorRunner CtorRunner
Definition: LLJIT.h:131
Expected< JITEvaluatedSymbol > lookup(JITDylib &JD, StringRef UnmangledName)
Look up a symbol in JITDylib JD based on its IR symbol name.
Definition: LLJIT.h:91
SetterImpl & setCreateObjectLinkingLayer(LLJITBuilderState::CreateObjectLinkingLayerFunction CreateObjectLinkingLayer)
Set an ObjectLinkingLayer creation function.
Definition: LLJIT.h:207
void setLazyCompileTransform(IRTransformLayer::TransformFunction Transform)
Set an IR transform (e.g.
Definition: LLJIT.h:143
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:43
SetterImpl & setJITTargetMachineBuilder(JITTargetMachineBuilder JTMB)
Set the JITTargetMachineBuilder for this instance.
Definition: LLJIT.h:192
SetterImpl & setLazyCompileFailureAddr(JITTargetAddress Addr)
Set the address in the target address to call if a lazy compile fails.
Definition: LLJIT.h:270
std::unique_ptr< ThreadPool > CompileThreads
Definition: LLJIT.h:126
std::function< Expected< ThreadSafeModule >(ThreadSafeModule, const MaterializationResponsibility &R)> TransformFunction
std::unique_ptr< ExecutionSession > ES
Definition: LLJIT.h:122
static ErrorSuccess success()
Create a success value.
Definition: Error.h:326
void recordCtorDtors(Module &M)
Definition: LLJIT.cpp:163
Expected< JITEvaluatedSymbol > lookupLinkerMangled(StringRef Name)
Look up a symbol in the main JITDylib by the symbol&#39;s linker-mangled name (to look up symbols based o...
Definition: LLJIT.h:86
static Expected< std::unique_ptr< LLJIT > > Create(LLJITBuilderState &S)
Expected< std::unique_ptr< JITType > > create()
Create an instance of the JIT.
Definition: LLJIT.h:228
std::function< std::unique_ptr< IndirectStubsManager >()> IndirectStubsManagerBuilderFunction
Definition: LLJIT.h:253
A pre-fabricated ORC JIT stack that can serve as an alternative to MCJIT.
Definition: LLJIT.h:35
An ExecutionSession represents a running JIT program.
Definition: Core.h:696
An extended version of LLJIT that supports lazy function-at-a-time compilation of LLVM IR...
Definition: LLJIT.h:136
std::unique_ptr< ObjectLayer > ObjLinkingLayer
Definition: LLJIT.h:128
void setPartitionFunction(CompileOnDemandLayer::PartitionFunction Partition)
Sets the partition function.
Definition: LLJIT.h:149
Represents a symbol that has been evaluated to an address already.
Definition: JITSymbol.h:208
Error addIRModule(ThreadSafeModule TSM)
Adds an IR module to the Main JITDylib.
Definition: LLJIT.h:66
Expected< JITEvaluatedSymbol > lookupLinkerMangled(JITDylib &JD, StringRef Name)
Look up a symbol in JITDylib JD by the symbol&#39;s linker-mangled name (to look up symbols based on thei...
Definition: LLJIT.cpp:71
Error addObjectFile(JITDylib &JD, std::unique_ptr< MemoryBuffer > Obj)
Adds an object file to the given JITDylib.
Definition: LLJIT.cpp:65
Constructs LLJIT instances.
Definition: LLJIT.h:244
Error applyDataLayout(Module &M)
Definition: LLJIT.cpp:151
~LLJIT()
Destruct this instance.
Definition: LLJIT.cpp:45
std::unique_ptr< LazyCallThroughManager > LCTMgr
Definition: LLJIT.h:257
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
Interface for Layers that accept object files.
Definition: Layer.h:113
DataLayout DL
Definition: LLJIT.h:125
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
JITDylib & getMainJITDylib()
Returns a reference to the JITDylib representing the JIT&#39;d main program.
Definition: LLJIT.h:52
static std::unique_ptr< ObjectLayer > createObjectLinkingLayer(LLJITBuilderState &S, ExecutionSession &ES)
Definition: LLJIT.cpp:77
Optional< JITTargetMachineBuilder > JTMB
Definition: LLJIT.h:177
IndirectStubsManagerBuilderFunction ISMBuilder
Definition: LLJIT.h:258
A symbol table that supports asynchoronous symbol queries.
Definition: Core.h:495
A utility class for building TargetMachines for JITs.