LLVM  10.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  /// Returns the JITDylib with the given name, or nullptr if no JITDylib with
55  /// that name exists.
57  return ES->getJITDylibByName(Name);
58  }
59 
60  /// Create a new JITDylib with the given name and return a reference to it.
61  ///
62  /// JITDylib names must be unique. If the given name is derived from user
63  /// input or elsewhere in the environment then the client should check
64  /// (e.g. by calling getJITDylibByName) that the given name is not already in
65  /// use.
66  JITDylib &createJITDylib(std::string Name) {
67  return ES->createJITDylib(std::move(Name));
68  }
69 
70  /// Convenience method for defining an absolute symbol.
72 
73  /// Adds an IR module to the given JITDylib.
75 
76  /// Adds an IR module to the Main JITDylib.
78  return addIRModule(Main, std::move(TSM));
79  }
80 
81  /// Adds an object file to the given JITDylib.
82  Error addObjectFile(JITDylib &JD, std::unique_ptr<MemoryBuffer> Obj);
83 
84  /// Adds an object file to the given JITDylib.
85  Error addObjectFile(std::unique_ptr<MemoryBuffer> Obj) {
86  return addObjectFile(Main, std::move(Obj));
87  }
88 
89  /// Look up a symbol in JITDylib JD by the symbol's linker-mangled name (to
90  /// look up symbols based on their IR name use the lookup function instead).
92  StringRef Name);
93 
94  /// Look up a symbol in the main JITDylib by the symbol's linker-mangled name
95  /// (to look up symbols based on their IR name use the lookup function
96  /// instead).
98  return lookupLinkerMangled(Main, Name);
99  }
100 
101  /// Look up a symbol in JITDylib JD based on its IR symbol name.
103  return lookupLinkerMangled(JD, mangle(UnmangledName));
104  }
105 
106  /// Look up a symbol in the main JITDylib based on its IR symbol name.
108  return lookup(Main, UnmangledName);
109  }
110 
111  /// Runs all not-yet-run static constructors.
113 
114  /// Runs all not-yet-run static destructors.
116 
117  /// Returns a reference to the ObjLinkingLayer
119 
120 protected:
121  static std::unique_ptr<ObjectLayer>
123 
126 
127  /// Create an LLJIT instance with a single compile thread.
128  LLJIT(LLJITBuilderState &S, Error &Err);
129 
130  std::string mangle(StringRef UnmangledName);
131 
133 
134  void recordCtorDtors(Module &M);
135 
136  std::unique_ptr<ExecutionSession> ES;
138 
140  std::unique_ptr<ThreadPool> CompileThreads;
141 
142  std::unique_ptr<ObjectLayer> ObjLinkingLayer;
143  std::unique_ptr<IRCompileLayer> CompileLayer;
144 
146 };
147 
148 /// An extended version of LLJIT that supports lazy function-at-a-time
149 /// compilation of LLVM IR.
150 class LLLazyJIT : public LLJIT {
151  template <typename, typename, typename> friend class LLJITBuilderSetters;
152 
153 public:
154 
155  /// Set an IR transform (e.g. pass manager pipeline) to run on each function
156  /// when it is compiled.
158  TransformLayer->setTransform(std::move(Transform));
159  }
160 
161  /// Sets the partition function.
162  void
164  CODLayer->setPartitionFunction(std::move(Partition));
165  }
166 
167  /// Add a module to be lazily compiled to JITDylib JD.
168  Error addLazyIRModule(JITDylib &JD, ThreadSafeModule M);
169 
170  /// Add a module to be lazily compiled to the main JITDylib.
172  return addLazyIRModule(Main, std::move(M));
173  }
174 
175 private:
176 
177  // Create a single-threaded LLLazyJIT instance.
179 
180  std::unique_ptr<LazyCallThroughManager> LCTMgr;
181  std::unique_ptr<IRTransformLayer> TransformLayer;
182  std::unique_ptr<CompileOnDemandLayer> CODLayer;
183 };
184 
186 public:
187  using ObjectLinkingLayerCreator = std::function<std::unique_ptr<ObjectLayer>(
189 
190  using CompileFunctionCreator =
191  std::function<Expected<IRCompileLayer::CompileFunction>(
193 
194  std::unique_ptr<ExecutionSession> ES;
197  CompileFunctionCreator CreateCompileFunction;
198  unsigned NumCompileThreads = 0;
199 
200  /// Called prior to JIT class construcion to fix up defaults.
201  Error prepareForConstruction();
202 };
203 
204 template <typename JITType, typename SetterImpl, typename State>
206 public:
207  /// Set the JITTargetMachineBuilder for this instance.
208  ///
209  /// If this method is not called, JITTargetMachineBuilder::detectHost will be
210  /// used to construct a default target machine builder for the host platform.
212  impl().JTMB = std::move(JTMB);
213  return impl();
214  }
215 
216  /// Return a reference to the JITTargetMachineBuilder.
217  ///
219  return impl().JTMB;
220  }
221 
222  /// Set an ObjectLinkingLayer creation function.
223  ///
224  /// If this method is not called, a default creation function will be used
225  /// that will construct an RTDyldObjectLinkingLayer.
227  LLJITBuilderState::ObjectLinkingLayerCreator CreateObjectLinkingLayer) {
228  impl().CreateObjectLinkingLayer = std::move(CreateObjectLinkingLayer);
229  return impl();
230  }
231 
232  /// Set a CompileFunctionCreator.
233  ///
234  /// If this method is not called, a default creation function wil be used
235  /// that will construct a basic IR compile function that is compatible with
236  /// the selected number of threads (SimpleCompiler for '0' compile threads,
237  /// ConcurrentIRCompiler otherwise).
239  LLJITBuilderState::CompileFunctionCreator CreateCompileFunction) {
240  impl().CreateCompileFunction = std::move(CreateCompileFunction);
241  return impl();
242  }
243 
244  /// Set the number of compile threads to use.
245  ///
246  /// If set to zero, compilation will be performed on the execution thread when
247  /// JITing in-process. If set to any other number N, a thread pool of N
248  /// threads will be created for compilation.
249  ///
250  /// If this method is not called, behavior will be as if it were called with
251  /// a zero argument.
252  SetterImpl &setNumCompileThreads(unsigned NumCompileThreads) {
253  impl().NumCompileThreads = NumCompileThreads;
254  return impl();
255  }
256 
257  /// Create an instance of the JIT.
259  if (auto Err = impl().prepareForConstruction())
260  return std::move(Err);
261 
262  Error Err = Error::success();
263  std::unique_ptr<JITType> J(new JITType(impl(), Err));
264  if (Err)
265  return std::move(Err);
266  return std::move(J);
267  }
268 
269 protected:
270  SetterImpl &impl() { return static_cast<SetterImpl &>(*this); }
271 };
272 
273 /// Constructs LLJIT instances.
275  : public LLJITBuilderState,
276  public LLJITBuilderSetters<LLJIT, LLJITBuilder, LLJITBuilderState> {};
277 
279  friend class LLLazyJIT;
280 
281 public:
283  std::function<std::unique_ptr<IndirectStubsManager>()>;
284 
286  JITTargetAddress LazyCompileFailureAddr = 0;
287  std::unique_ptr<LazyCallThroughManager> LCTMgr;
289 
290  Error prepareForConstruction();
291 };
292 
293 template <typename JITType, typename SetterImpl, typename State>
295  : public LLJITBuilderSetters<JITType, SetterImpl, State> {
296 public:
297  /// Set the address in the target address to call if a lazy compile fails.
298  ///
299  /// If this method is not called then the value will default to 0.
301  this->impl().LazyCompileFailureAddr = Addr;
302  return this->impl();
303  }
304 
305  /// Set the lazy-callthrough manager.
306  ///
307  /// If this method is not called then a default, in-process lazy callthrough
308  /// manager for the host platform will be used.
309  SetterImpl &
310  setLazyCallthroughManager(std::unique_ptr<LazyCallThroughManager> LCTMgr) {
311  this->impl().LCTMgr = std::move(LCTMgr);
312  return this->impl();
313  }
314 
315  /// Set the IndirectStubsManager builder function.
316  ///
317  /// If this method is not called then a default, in-process
318  /// IndirectStubsManager builder for the host platform will be used.
321  this->impl().ISMBuilder = std::move(ISMBuilder);
322  return this->impl();
323  }
324 };
325 
326 /// Constructs LLLazyJIT instances.
328  : public LLLazyJITBuilderState,
329  public LLLazyJITBuilderSetters<LLLazyJIT, LLLazyJITBuilder,
330  LLLazyJITBuilderState> {};
331 
332 } // End namespace orc
333 } // End namespace llvm
334 
335 #endif // LLVM_EXECUTIONENGINE_ORC_LLJIT_H
Constructs LLLazyJIT instances.
Definition: LLJIT.h:327
static Expected< IRCompileLayer::CompileFunction > createCompileFunction(LLJITBuilderState &S, JITTargetMachineBuilder JTMB)
Definition: LLJIT.cpp:85
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:111
SetterImpl & setLazyCallthroughManager(std::unique_ptr< LazyCallThroughManager > LCTMgr)
Set the lazy-callthrough manager.
Definition: LLJIT.h:310
Error addObjectFile(std::unique_ptr< MemoryBuffer > Obj)
Adds an object file to the given JITDylib.
Definition: LLJIT.h:85
JITDylib & createJITDylib(std::string Name)
Create a new JITDylib with the given name and return a reference to it.
Definition: LLJIT.h:66
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:35
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:66
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:104
std::unique_ptr< ExecutionSession > ES
Definition: LLJIT.h:194
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:41
Optional< JITTargetMachineBuilder > & getJITTargetMachineBuilder()
Return a reference to the JITTargetMachineBuilder.
Definition: LLJIT.h:218
JITDylib & Main
Definition: LLJIT.h:137
Error runConstructors()
Runs all not-yet-run static constructors.
Definition: LLJIT.h:112
std::function< Optional< GlobalValueSet >(GlobalValueSet Requested)> PartitionFunction
Partitioning function.
Tagged union holding either a T or a Error.
Definition: yaml2obj.h:21
SetterImpl & setIndirectStubsManagerBuilder(LLLazyJITBuilderState::IndirectStubsManagerBuilderFunction ISMBuilder)
Set the IndirectStubsManager builder function.
Definition: LLJIT.h:319
place backedge safepoints impl
CtorDtorRunner DtorRunner
Definition: LLJIT.h:145
SetterImpl & setObjectLinkingLayerCreator(LLJITBuilderState::ObjectLinkingLayerCreator CreateObjectLinkingLayer)
Set an ObjectLinkingLayer creation function.
Definition: LLJIT.h:226
ObjectLayer & getObjLinkingLayer()
Returns a reference to the ObjLinkingLayer.
Definition: LLJIT.h:118
SetterImpl & setNumCompileThreads(unsigned NumCompileThreads)
Set the number of compile threads to use.
Definition: LLJIT.h:252
uint64_t JITTargetAddress
Represents an address in the target process&#39;s address space.
Definition: JITSymbol.h:41
Error addLazyIRModule(ThreadSafeModule M)
Add a module to be lazily compiled to the main JITDylib.
Definition: LLJIT.h:171
std::function< std::unique_ptr< ObjectLayer >(ExecutionSession &, const Triple &TT)> ObjectLinkingLayerCreator
Definition: LLJIT.h:188
Error runDestructors()
Runs all not-yet-run static destructors.
Definition: LLJIT.h:115
Expected< JITEvaluatedSymbol > lookup(StringRef UnmangledName)
Look up a symbol in the main JITDylib based on its IR symbol name.
Definition: LLJIT.h:107
JITDylib * getJITDylibByName(StringRef Name)
Returns the JITDylib with the given name, or nullptr if no JITDylib with that name exists...
Definition: LLJIT.h:56
std::string mangle(StringRef UnmangledName)
Definition: LLJIT.cpp:143
std::unique_ptr< IRCompileLayer > CompileLayer
Definition: LLJIT.h:143
CompileFunctionCreator CreateCompileFunction
Definition: LLJIT.h:197
An LLVM Module together with a shared ThreadSafeContext.
CtorDtorRunner CtorRunner
Definition: LLJIT.h:145
Expected< JITEvaluatedSymbol > lookup(JITDylib &JD, StringRef UnmangledName)
Look up a symbol in JITDylib JD based on its IR symbol name.
Definition: LLJIT.h:102
std::function< Expected< IRCompileLayer::CompileFunction >(JITTargetMachineBuilder JTMB)> CompileFunctionCreator
Definition: LLJIT.h:192
void setLazyCompileTransform(IRTransformLayer::TransformFunction Transform)
Set an IR transform (e.g.
Definition: LLJIT.h:157
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:43
ObjectLinkingLayerCreator CreateObjectLinkingLayer
Definition: LLJIT.h:196
SetterImpl & setJITTargetMachineBuilder(JITTargetMachineBuilder JTMB)
Set the JITTargetMachineBuilder for this instance.
Definition: LLJIT.h:211
SetterImpl & setLazyCompileFailureAddr(JITTargetAddress Addr)
Set the address in the target address to call if a lazy compile fails.
Definition: LLJIT.h:300
std::unique_ptr< ThreadPool > CompileThreads
Definition: LLJIT.h:140
std::function< Expected< ThreadSafeModule >(ThreadSafeModule, const MaterializationResponsibility &R)> TransformFunction
std::unique_ptr< ExecutionSession > ES
Definition: LLJIT.h:136
static ErrorSuccess success()
Create a success value.
Definition: Error.h:326
void recordCtorDtors(Module &M)
Definition: LLJIT.cpp:164
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:97
static Expected< std::unique_ptr< LLJIT > > Create(LLJITBuilderState &S)
Expected< std::unique_ptr< JITType > > create()
Create an instance of the JIT.
Definition: LLJIT.h:258
std::function< std::unique_ptr< IndirectStubsManager >()> IndirectStubsManagerBuilderFunction
Definition: LLJIT.h:283
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:761
An extended version of LLJIT that supports lazy function-at-a-time compilation of LLVM IR...
Definition: LLJIT.h:150
std::unique_ptr< ObjectLayer > ObjLinkingLayer
Definition: LLJIT.h:142
void setPartitionFunction(CompileOnDemandLayer::PartitionFunction Partition)
Sets the partition function.
Definition: LLJIT.h:163
Represents a symbol that has been evaluated to an address already.
Definition: JITSymbol.h:190
Error addIRModule(ThreadSafeModule TSM)
Adds an IR module to the Main JITDylib.
Definition: LLJIT.h:77
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:57
Error addObjectFile(JITDylib &JD, std::unique_ptr< MemoryBuffer > Obj)
Adds an object file to the given JITDylib.
Definition: LLJIT.cpp:51
Constructs LLJIT instances.
Definition: LLJIT.h:274
Error applyDataLayout(Module &M)
Definition: LLJIT.cpp:152
~LLJIT()
Destruct this instance.
Definition: LLJIT.cpp:30
SetterImpl & setCompileFunctionCreator(LLJITBuilderState::CompileFunctionCreator CreateCompileFunction)
Set a CompileFunctionCreator.
Definition: LLJIT.h:238
std::unique_ptr< LazyCallThroughManager > LCTMgr
Definition: LLJIT.h:287
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:139
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:63
Optional< JITTargetMachineBuilder > JTMB
Definition: LLJIT.h:195
IndirectStubsManagerBuilderFunction ISMBuilder
Definition: LLJIT.h:288
A symbol table that supports asynchoronous symbol queries.
Definition: Core.h:495
A utility class for building TargetMachines for JITs.