Revert r354244 "[DAGCombiner] Eliminate dead stores to stack."
[llvm-complete.git] / include / llvm / ExecutionEngine / Orc / LLJIT.h
blobb0ef20dec4d576dd5fe62d66ae0b172893715a73
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.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_EXECUTIONENGINE_ORC_LLJIT_H
14 #define LLVM_EXECUTIONENGINE_ORC_LLJIT_H
16 #include "llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h"
17 #include "llvm/ExecutionEngine/Orc/CompileUtils.h"
18 #include "llvm/ExecutionEngine/Orc/ExecutionUtils.h"
19 #include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
20 #include "llvm/ExecutionEngine/Orc/IRTransformLayer.h"
21 #include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h"
22 #include "llvm/ExecutionEngine/Orc/ObjectTransformLayer.h"
23 #include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
24 #include "llvm/ExecutionEngine/Orc/ThreadSafeModule.h"
25 #include "llvm/Support/ThreadPool.h"
27 namespace llvm {
28 namespace orc {
30 /// A pre-fabricated ORC JIT stack that can serve as an alternative to MCJIT.
31 class LLJIT {
32 public:
34 /// Destruct this instance. If a multi-threaded instance, waits for all
35 /// compile threads to complete.
36 ~LLJIT();
38 /// Create an LLJIT instance.
39 /// If NumCompileThreads is not equal to zero, creates a multi-threaded
40 /// LLJIT with the given number of compile threads.
41 static Expected<std::unique_ptr<LLJIT>>
42 Create(JITTargetMachineBuilder JTMB, DataLayout DL,
43 unsigned NumCompileThreads = 0);
45 /// Returns the ExecutionSession for this instance.
46 ExecutionSession &getExecutionSession() { return *ES; }
48 /// Returns a reference to the JITDylib representing the JIT'd main program.
49 JITDylib &getMainJITDylib() { return Main; }
51 /// Create a new JITDylib with the given name and return a reference to it.
52 JITDylib &createJITDylib(std::string Name) {
53 return ES->createJITDylib(std::move(Name));
56 /// Convenience method for defining an absolute symbol.
57 Error defineAbsolute(StringRef Name, JITEvaluatedSymbol Address);
59 /// Convenience method for defining an
61 /// Adds an IR module to the given JITDylib.
62 Error addIRModule(JITDylib &JD, ThreadSafeModule TSM);
64 /// Adds an IR module to the Main JITDylib.
65 Error addIRModule(ThreadSafeModule TSM) {
66 return addIRModule(Main, std::move(TSM));
69 /// Adds an object file to the given JITDylib.
70 Error addObjectFile(JITDylib &JD, std::unique_ptr<MemoryBuffer> Obj);
72 /// Adds an object file to the given JITDylib.
73 Error addObjectFile(std::unique_ptr<MemoryBuffer> Obj) {
74 return addObjectFile(Main, std::move(Obj));
77 /// Look up a symbol in JITDylib JD by the symbol's linker-mangled name (to
78 /// look up symbols based on their IR name use the lookup function instead).
79 Expected<JITEvaluatedSymbol> lookupLinkerMangled(JITDylib &JD,
80 StringRef Name);
82 /// Look up a symbol in the main JITDylib by the symbol's linker-mangled name
83 /// (to look up symbols based on their IR name use the lookup function
84 /// instead).
85 Expected<JITEvaluatedSymbol> lookupLinkerMangled(StringRef Name) {
86 return lookupLinkerMangled(Main, Name);
89 /// Look up a symbol in JITDylib JD based on its IR symbol name.
90 Expected<JITEvaluatedSymbol> lookup(JITDylib &JD, StringRef UnmangledName) {
91 return lookupLinkerMangled(JD, mangle(UnmangledName));
94 /// Look up a symbol in the main JITDylib based on its IR symbol name.
95 Expected<JITEvaluatedSymbol> lookup(StringRef UnmangledName) {
96 return lookup(Main, UnmangledName);
99 /// Runs all not-yet-run static constructors.
100 Error runConstructors() { return CtorRunner.run(); }
102 /// Runs all not-yet-run static destructors.
103 Error runDestructors() { return DtorRunner.run(); }
105 /// Returns a reference to the ObjLinkingLayer
106 RTDyldObjectLinkingLayer &getObjLinkingLayer() { return ObjLinkingLayer; }
108 protected:
110 /// Create an LLJIT instance with a single compile thread.
111 LLJIT(std::unique_ptr<ExecutionSession> ES, std::unique_ptr<TargetMachine> TM,
112 DataLayout DL);
114 /// Create an LLJIT instance with multiple compile threads.
115 LLJIT(std::unique_ptr<ExecutionSession> ES, JITTargetMachineBuilder JTMB,
116 DataLayout DL, unsigned NumCompileThreads);
118 std::string mangle(StringRef UnmangledName);
120 Error applyDataLayout(Module &M);
122 void recordCtorDtors(Module &M);
124 std::unique_ptr<ExecutionSession> ES;
125 JITDylib &Main;
127 DataLayout DL;
128 std::unique_ptr<ThreadPool> CompileThreads;
130 RTDyldObjectLinkingLayer ObjLinkingLayer;
131 IRCompileLayer CompileLayer;
133 CtorDtorRunner CtorRunner, DtorRunner;
136 /// An extended version of LLJIT that supports lazy function-at-a-time
137 /// compilation of LLVM IR.
138 class LLLazyJIT : public LLJIT {
139 public:
141 /// Create an LLLazyJIT instance.
142 /// If NumCompileThreads is not equal to zero, creates a multi-threaded
143 /// LLLazyJIT with the given number of compile threads.
144 static Expected<std::unique_ptr<LLLazyJIT>>
145 Create(JITTargetMachineBuilder JTMB, DataLayout DL,
146 JITTargetAddress ErrorAddr, unsigned NumCompileThreads = 0);
148 /// Set an IR transform (e.g. pass manager pipeline) to run on each function
149 /// when it is compiled.
150 void setLazyCompileTransform(IRTransformLayer::TransformFunction Transform) {
151 TransformLayer.setTransform(std::move(Transform));
154 /// Sets the partition function.
155 void
156 setPartitionFunction(CompileOnDemandLayer::PartitionFunction Partition) {
157 CODLayer.setPartitionFunction(std::move(Partition));
160 /// Add a module to be lazily compiled to JITDylib JD.
161 Error addLazyIRModule(JITDylib &JD, ThreadSafeModule M);
163 /// Add a module to be lazily compiled to the main JITDylib.
164 Error addLazyIRModule(ThreadSafeModule M) {
165 return addLazyIRModule(Main, std::move(M));
168 private:
170 // Create a single-threaded LLLazyJIT instance.
171 LLLazyJIT(std::unique_ptr<ExecutionSession> ES,
172 std::unique_ptr<TargetMachine> TM, DataLayout DL,
173 std::unique_ptr<LazyCallThroughManager> LCTMgr,
174 std::function<std::unique_ptr<IndirectStubsManager>()> ISMBuilder);
176 // Create a multi-threaded LLLazyJIT instance.
177 LLLazyJIT(std::unique_ptr<ExecutionSession> ES, JITTargetMachineBuilder JTMB,
178 DataLayout DL, unsigned NumCompileThreads,
179 std::unique_ptr<LazyCallThroughManager> LCTMgr,
180 std::function<std::unique_ptr<IndirectStubsManager>()> ISMBuilder);
182 std::unique_ptr<LazyCallThroughManager> LCTMgr;
183 std::function<std::unique_ptr<IndirectStubsManager>()> ISMBuilder;
185 IRTransformLayer TransformLayer;
186 CompileOnDemandLayer CODLayer;
189 } // End namespace orc
190 } // End namespace llvm
192 #endif // LLVM_EXECUTIONENGINE_ORC_LLJIT_H