LLVM  8.0.1
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 
27 
28 namespace llvm {
29 namespace orc {
30 
31 /// A pre-fabricated ORC JIT stack that can serve as an alternative to MCJIT.
32 class LLJIT {
33 public:
34 
35  /// Destruct this instance. If a multi-threaded instance, waits for all
36  /// compile threads to complete.
37  ~LLJIT();
38 
39  /// Create an LLJIT instance.
40  /// If NumCompileThreads is not equal to zero, creates a multi-threaded
41  /// LLJIT with the given number of compile threads.
44  unsigned NumCompileThreads = 0);
45 
46  /// Returns the ExecutionSession for this instance.
48 
49  /// Returns a reference to the JITDylib representing the JIT'd main program.
50  JITDylib &getMainJITDylib() { return Main; }
51 
52  /// Create a new JITDylib with the given name and return a reference to it.
53  JITDylib &createJITDylib(std::string Name) {
54  return ES->createJITDylib(std::move(Name));
55  }
56 
57  /// Convenience method for defining an absolute symbol.
59 
60  /// Convenience method for defining an
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 
111  /// Create an LLJIT instance with a single compile thread.
112  LLJIT(std::unique_ptr<ExecutionSession> ES, std::unique_ptr<TargetMachine> TM,
113  DataLayout DL);
114 
115  /// Create an LLJIT instance with multiple compile threads.
116  LLJIT(std::unique_ptr<ExecutionSession> ES, JITTargetMachineBuilder JTMB,
117  DataLayout DL, unsigned NumCompileThreads);
118 
119  std::string mangle(StringRef UnmangledName);
120 
122 
123  void recordCtorDtors(Module &M);
124 
125  std::unique_ptr<ExecutionSession> ES;
127 
129  std::unique_ptr<ThreadPool> CompileThreads;
130 
133 
135 };
136 
137 /// An extended version of LLJIT that supports lazy function-at-a-time
138 /// compilation of LLVM IR.
139 class LLLazyJIT : public LLJIT {
140 public:
141 
142  /// Create an LLLazyJIT instance.
143  /// If NumCompileThreads is not equal to zero, creates a multi-threaded
144  /// LLLazyJIT with the given number of compile threads.
147  JITTargetAddress ErrorAddr, unsigned NumCompileThreads = 0);
148 
149  /// Set an IR transform (e.g. pass manager pipeline) to run on each function
150  /// when it is compiled.
152  TransformLayer.setTransform(std::move(Transform));
153  }
154 
155  /// Sets the partition function.
156  void
158  CODLayer.setPartitionFunction(std::move(Partition));
159  }
160 
161  /// Add a module to be lazily compiled to JITDylib JD.
162  Error addLazyIRModule(JITDylib &JD, ThreadSafeModule M);
163 
164  /// Add a module to be lazily compiled to the main JITDylib.
166  return addLazyIRModule(Main, std::move(M));
167  }
168 
169 private:
170 
171  // Create a single-threaded LLLazyJIT instance.
172  LLLazyJIT(std::unique_ptr<ExecutionSession> ES,
173  std::unique_ptr<TargetMachine> TM, DataLayout DL,
174  std::unique_ptr<LazyCallThroughManager> LCTMgr,
175  std::function<std::unique_ptr<IndirectStubsManager>()> ISMBuilder);
176 
177  // Create a multi-threaded LLLazyJIT instance.
178  LLLazyJIT(std::unique_ptr<ExecutionSession> ES, JITTargetMachineBuilder JTMB,
179  DataLayout DL, unsigned NumCompileThreads,
180  std::unique_ptr<LazyCallThroughManager> LCTMgr,
181  std::function<std::unique_ptr<IndirectStubsManager>()> ISMBuilder);
182 
183  std::unique_ptr<LazyCallThroughManager> LCTMgr;
184  std::function<std::unique_ptr<IndirectStubsManager>()> ISMBuilder;
185 
186  IRTransformLayer TransformLayer;
187  CompileOnDemandLayer CODLayer;
188 };
189 
190 } // End namespace orc
191 } // End namespace llvm
192 
193 #endif // LLVM_EXECUTIONENGINE_ORC_LLJIT_H
ExecutionSession & getExecutionSession()
Returns the ExecutionSession for this instance.
Definition: LLJIT.h:47
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:111
IRCompileLayer CompileLayer
Definition: LLJIT.h:132
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:53
This class represents lattice values for constants.
Definition: AllocatorList.h:24
Error defineAbsolute(StringRef Name, JITEvaluatedSymbol Address)
Convenience method for defining an absolute symbol.
Definition: LLJIT.cpp:56
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:65
Error addIRModule(JITDylib &JD, ThreadSafeModule TSM)
Convenience method for defining an.
Definition: LLJIT.cpp:62
JITDylib & Main
Definition: LLJIT.h:126
Error runConstructors()
Runs all not-yet-run static constructors.
Definition: LLJIT.h:101
RTDyldObjectLinkingLayer ObjLinkingLayer
Definition: LLJIT.h:131
amdgpu Simplify well known AMD library false Value Value const Twine & Name
std::function< Optional< GlobalValueSet >(GlobalValueSet Requested)> PartitionFunction
Partitioning function.
Tagged union holding either a T or a Error.
Definition: CachePruning.h:23
CtorDtorRunner DtorRunner
Definition: LLJIT.h:134
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:165
Error runDestructors()
Runs all not-yet-run static destructors.
Definition: LLJIT.h:104
static Expected< std::unique_ptr< LLJIT > > Create(JITTargetMachineBuilder JTMB, DataLayout DL, unsigned NumCompileThreads=0)
Create an LLJIT instance.
Definition: LLJIT.cpp:39
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:120
LLJIT(std::unique_ptr< ExecutionSession > ES, std::unique_ptr< TargetMachine > TM, DataLayout DL)
Create an LLJIT instance with a single compile thread.
Definition: LLJIT.cpp:82
An LLVM Module together with a shared ThreadSafeContext.
CtorDtorRunner CtorRunner
Definition: LLJIT.h:134
Expected< JITEvaluatedSymbol > lookup(JITDylib &JD, StringRef UnmangledName)
Look up a symbol in JITDylib JD based on its IR symbol name.
Definition: LLJIT.h:91
void setLazyCompileTransform(IRTransformLayer::TransformFunction Transform)
Set an IR transform (e.g.
Definition: LLJIT.h:151
std::unique_ptr< ThreadPool > CompileThreads
Definition: LLJIT.h:129
std::function< Expected< ThreadSafeModule >(ThreadSafeModule, const MaterializationResponsibility &R)> TransformFunction
std::unique_ptr< ExecutionSession > ES
Definition: LLJIT.h:125
void recordCtorDtors(Module &M)
Definition: LLJIT.cpp:141
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
A pre-fabricated ORC JIT stack that can serve as an alternative to MCJIT.
Definition: LLJIT.h:32
An ExecutionSession represents a running JIT program.
Definition: Core.h:697
An extended version of LLJIT that supports lazy function-at-a-time compilation of LLVM IR...
Definition: LLJIT.h:139
void setPartitionFunction(CompileOnDemandLayer::PartitionFunction Partition)
Sets the partition function.
Definition: LLJIT.h:157
Represents a symbol that has been evaluated to an address already.
Definition: JITSymbol.h:209
Error addIRModule(ThreadSafeModule TSM)
Adds an IR module to the Main JITDylib.
Definition: LLJIT.h:66
RTDyldObjectLinkingLayer & getObjLinkingLayer()
Returns a reference to the ObjLinkingLayer.
Definition: LLJIT.h:107
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:77
Error addObjectFile(JITDylib &JD, std::unique_ptr< MemoryBuffer > Obj)
Adds an object file to the given JITDylib.
Definition: LLJIT.cpp:71
Error applyDataLayout(Module &M)
Definition: LLJIT.cpp:129
~LLJIT()
Destruct this instance.
Definition: LLJIT.cpp:33
Lightweight error class with error context and mandatory checking.
Definition: Error.h:158
print Print MemDeps of function
DataLayout DL
Definition: LLJIT.h:128
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
JITDylib & getMainJITDylib()
Returns a reference to the JITDylib representing the JIT&#39;d main program.
Definition: LLJIT.h:50
A symbol table that supports asynchoronous symbol queries.
Definition: Core.h:496
A utility class for building TargetMachines for JITs.