LLVM  10.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 // 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  /// Returns a reference to the object transform layer.
122 
123 protected:
124  static std::unique_ptr<ObjectLayer>
126 
129 
130  /// Create an LLJIT instance with a single compile thread.
131  LLJIT(LLJITBuilderState &S, Error &Err);
132 
133  std::string mangle(StringRef UnmangledName);
134 
136 
137  void recordCtorDtors(Module &M);
138 
139  std::unique_ptr<ExecutionSession> ES;
141 
143  std::unique_ptr<ThreadPool> CompileThreads;
144 
145  std::unique_ptr<ObjectLayer> ObjLinkingLayer;
147  std::unique_ptr<IRCompileLayer> CompileLayer;
148 
150 };
151 
152 /// An extended version of LLJIT that supports lazy function-at-a-time
153 /// compilation of LLVM IR.
154 class LLLazyJIT : public LLJIT {
155  template <typename, typename, typename> friend class LLJITBuilderSetters;
156 
157 public:
158 
159  /// Set an IR transform (e.g. pass manager pipeline) to run on each function
160  /// when it is compiled.
162  TransformLayer->setTransform(std::move(Transform));
163  }
164 
165  /// Sets the partition function.
166  void
168  CODLayer->setPartitionFunction(std::move(Partition));
169  }
170 
171  /// Add a module to be lazily compiled to JITDylib JD.
172  Error addLazyIRModule(JITDylib &JD, ThreadSafeModule M);
173 
174  /// Add a module to be lazily compiled to the main JITDylib.
176  return addLazyIRModule(Main, std::move(M));
177  }
178 
179 private:
180 
181  // Create a single-threaded LLLazyJIT instance.
183 
184  std::unique_ptr<LazyCallThroughManager> LCTMgr;
185  std::unique_ptr<IRTransformLayer> TransformLayer;
186  std::unique_ptr<CompileOnDemandLayer> CODLayer;
187 };
188 
190 public:
191  using ObjectLinkingLayerCreator = std::function<std::unique_ptr<ObjectLayer>(
193 
194  using CompileFunctionCreator =
195  std::function<Expected<std::unique_ptr<IRCompileLayer::IRCompiler>>(
197 
198  std::unique_ptr<ExecutionSession> ES;
201  CompileFunctionCreator CreateCompileFunction;
202  unsigned NumCompileThreads = 0;
203 
204  /// Called prior to JIT class construcion to fix up defaults.
205  Error prepareForConstruction();
206 };
207 
208 template <typename JITType, typename SetterImpl, typename State>
210 public:
211  /// Set the JITTargetMachineBuilder for this instance.
212  ///
213  /// If this method is not called, JITTargetMachineBuilder::detectHost will be
214  /// used to construct a default target machine builder for the host platform.
216  impl().JTMB = std::move(JTMB);
217  return impl();
218  }
219 
220  /// Return a reference to the JITTargetMachineBuilder.
221  ///
223  return impl().JTMB;
224  }
225 
226  /// Set an ObjectLinkingLayer creation function.
227  ///
228  /// If this method is not called, a default creation function will be used
229  /// that will construct an RTDyldObjectLinkingLayer.
231  LLJITBuilderState::ObjectLinkingLayerCreator CreateObjectLinkingLayer) {
232  impl().CreateObjectLinkingLayer = std::move(CreateObjectLinkingLayer);
233  return impl();
234  }
235 
236  /// Set a CompileFunctionCreator.
237  ///
238  /// If this method is not called, a default creation function wil be used
239  /// that will construct a basic IR compile function that is compatible with
240  /// the selected number of threads (SimpleCompiler for '0' compile threads,
241  /// ConcurrentIRCompiler otherwise).
243  LLJITBuilderState::CompileFunctionCreator CreateCompileFunction) {
244  impl().CreateCompileFunction = std::move(CreateCompileFunction);
245  return impl();
246  }
247 
248  /// Set the number of compile threads to use.
249  ///
250  /// If set to zero, compilation will be performed on the execution thread when
251  /// JITing in-process. If set to any other number N, a thread pool of N
252  /// threads will be created for compilation.
253  ///
254  /// If this method is not called, behavior will be as if it were called with
255  /// a zero argument.
256  SetterImpl &setNumCompileThreads(unsigned NumCompileThreads) {
257  impl().NumCompileThreads = NumCompileThreads;
258  return impl();
259  }
260 
261  /// Create an instance of the JIT.
263  if (auto Err = impl().prepareForConstruction())
264  return std::move(Err);
265 
266  Error Err = Error::success();
267  std::unique_ptr<JITType> J(new JITType(impl(), Err));
268  if (Err)
269  return std::move(Err);
270  return std::move(J);
271  }
272 
273 protected:
274  SetterImpl &impl() { return static_cast<SetterImpl &>(*this); }
275 };
276 
277 /// Constructs LLJIT instances.
279  : public LLJITBuilderState,
280  public LLJITBuilderSetters<LLJIT, LLJITBuilder, LLJITBuilderState> {};
281 
283  friend class LLLazyJIT;
284 
285 public:
287  std::function<std::unique_ptr<IndirectStubsManager>()>;
288 
290  JITTargetAddress LazyCompileFailureAddr = 0;
291  std::unique_ptr<LazyCallThroughManager> LCTMgr;
293 
294  Error prepareForConstruction();
295 };
296 
297 template <typename JITType, typename SetterImpl, typename State>
299  : public LLJITBuilderSetters<JITType, SetterImpl, State> {
300 public:
301  /// Set the address in the target address to call if a lazy compile fails.
302  ///
303  /// If this method is not called then the value will default to 0.
305  this->impl().LazyCompileFailureAddr = Addr;
306  return this->impl();
307  }
308 
309  /// Set the lazy-callthrough manager.
310  ///
311  /// If this method is not called then a default, in-process lazy callthrough
312  /// manager for the host platform will be used.
313  SetterImpl &
314  setLazyCallthroughManager(std::unique_ptr<LazyCallThroughManager> LCTMgr) {
315  this->impl().LCTMgr = std::move(LCTMgr);
316  return this->impl();
317  }
318 
319  /// Set the IndirectStubsManager builder function.
320  ///
321  /// If this method is not called then a default, in-process
322  /// IndirectStubsManager builder for the host platform will be used.
325  this->impl().ISMBuilder = std::move(ISMBuilder);
326  return this->impl();
327  }
328 };
329 
330 /// Constructs LLLazyJIT instances.
332  : public LLLazyJITBuilderState,
333  public LLLazyJITBuilderSetters<LLLazyJIT, LLLazyJITBuilder,
334  LLLazyJITBuilderState> {};
335 
336 } // End namespace orc
337 } // End namespace llvm
338 
339 #endif // LLVM_EXECUTIONENGINE_ORC_LLJIT_H
Constructs LLLazyJIT instances.
Definition: LLJIT.h:331
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:314
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:57
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:130
std::unique_ptr< ExecutionSession > ES
Definition: LLJIT.h:198
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:63
Optional< JITTargetMachineBuilder > & getJITTargetMachineBuilder()
Return a reference to the JITTargetMachineBuilder.
Definition: LLJIT.h:222
JITDylib & Main
Definition: LLJIT.h:140
Error runConstructors()
Runs all not-yet-run static constructors.
Definition: LLJIT.h:112
std::function< Optional< GlobalValueSet >(GlobalValueSet Requested)> PartitionFunction
Partitioning function.
ObjectTransformLayer & getObjTransformLayer()
Returns a reference to the object transform layer.
Definition: LLJIT.h:121
Tagged union holding either a T or a Error.
Definition: APFloat.h:41
SetterImpl & setIndirectStubsManagerBuilder(LLLazyJITBuilderState::IndirectStubsManagerBuilderFunction ISMBuilder)
Set the IndirectStubsManager builder function.
Definition: LLJIT.h:323
place backedge safepoints impl
CtorDtorRunner DtorRunner
Definition: LLJIT.h:149
SetterImpl & setObjectLinkingLayerCreator(LLJITBuilderState::ObjectLinkingLayerCreator CreateObjectLinkingLayer)
Set an ObjectLinkingLayer creation function.
Definition: LLJIT.h:230
ObjectLayer & getObjLinkingLayer()
Returns a reference to the ObjLinkingLayer.
Definition: LLJIT.h:118
std::function< Expected< std::unique_ptr< IRCompileLayer::IRCompiler > >(JITTargetMachineBuilder JTMB)> CompileFunctionCreator
Definition: LLJIT.h:196
SetterImpl & setNumCompileThreads(unsigned NumCompileThreads)
Set the number of compile threads to use.
Definition: LLJIT.h:256
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:175
static Expected< std::unique_ptr< IRCompileLayer::IRCompiler > > createCompileFunction(LLJITBuilderState &S, JITTargetMachineBuilder JTMB)
Definition: LLJIT.cpp:111
std::function< std::unique_ptr< ObjectLayer >(ExecutionSession &, const Triple &TT)> ObjectLinkingLayerCreator
Definition: LLJIT.h:192
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:169
std::unique_ptr< IRCompileLayer > CompileLayer
Definition: LLJIT.h:147
CompileFunctionCreator CreateCompileFunction
Definition: LLJIT.h:201
An LLVM Module together with a shared ThreadSafeContext.
CtorDtorRunner CtorRunner
Definition: LLJIT.h:149
Expected< JITEvaluatedSymbol > lookup(JITDylib &JD, StringRef UnmangledName)
Look up a symbol in JITDylib JD based on its IR symbol name.
Definition: LLJIT.h:102
void setLazyCompileTransform(IRTransformLayer::TransformFunction Transform)
Set an IR transform (e.g.
Definition: LLJIT.h:161
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:43
ObjectLinkingLayerCreator CreateObjectLinkingLayer
Definition: LLJIT.h:200
SetterImpl & setJITTargetMachineBuilder(JITTargetMachineBuilder JTMB)
Set the JITTargetMachineBuilder for this instance.
Definition: LLJIT.h:215
SetterImpl & setLazyCompileFailureAddr(JITTargetAddress Addr)
Set the address in the target address to call if a lazy compile fails.
Definition: LLJIT.h:304
std::unique_ptr< ThreadPool > CompileThreads
Definition: LLJIT.h:143
std::function< Expected< ThreadSafeModule >(ThreadSafeModule, const MaterializationResponsibility &R)> TransformFunction
std::unique_ptr< ExecutionSession > ES
Definition: LLJIT.h:139
static ErrorSuccess success()
Create a success value.
Definition: Error.h:326
void recordCtorDtors(Module &M)
Definition: LLJIT.cpp:190
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:262
std::function< std::unique_ptr< IndirectStubsManager >()> IndirectStubsManagerBuilderFunction
Definition: LLJIT.h:287
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:1058
An extended version of LLJIT that supports lazy function-at-a-time compilation of LLVM IR...
Definition: LLJIT.h:154
std::unique_ptr< ObjectLayer > ObjLinkingLayer
Definition: LLJIT.h:145
void setPartitionFunction(CompileOnDemandLayer::PartitionFunction Partition)
Sets the partition function.
Definition: LLJIT.h:167
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: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:79
Error addObjectFile(JITDylib &JD, std::unique_ptr< MemoryBuffer > Obj)
Adds an object file to the given JITDylib.
Definition: LLJIT.cpp:73
Constructs LLJIT instances.
Definition: LLJIT.h:278
Error applyDataLayout(Module &M)
Definition: LLJIT.cpp:178
~LLJIT()
Destruct this instance.
Definition: LLJIT.cpp:52
SetterImpl & setCompileFunctionCreator(LLJITBuilderState::CompileFunctionCreator CreateCompileFunction)
Set a CompileFunctionCreator.
Definition: LLJIT.h:242
std::unique_ptr< LazyCallThroughManager > LCTMgr
Definition: LLJIT.h:291
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
Interface for Layers that accept object files.
Definition: Layer.h:126
DataLayout DL
Definition: LLJIT.h:142
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:54
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:87
Optional< JITTargetMachineBuilder > JTMB
Definition: LLJIT.h:199
IndirectStubsManagerBuilderFunction ISMBuilder
Definition: LLJIT.h:292
ObjectTransformLayer ObjTransformLayer
Definition: LLJIT.h:146
A symbol table that supports asynchoronous symbol queries.
Definition: Core.h:774
A utility class for building TargetMachines for JITs.