Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 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 | |
| 17 | #include "llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h" |
| 18 | #include "llvm/ExecutionEngine/Orc/CompileUtils.h" |
| 19 | #include "llvm/ExecutionEngine/Orc/ExecutionUtils.h" |
| 20 | #include "llvm/ExecutionEngine/Orc/IRCompileLayer.h" |
| 21 | #include "llvm/ExecutionEngine/Orc/IRTransformLayer.h" |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 22 | #include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h" |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 23 | #include "llvm/ExecutionEngine/Orc/ObjectTransformLayer.h" |
| 24 | #include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h" |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 25 | #include "llvm/ExecutionEngine/Orc/ThreadSafeModule.h" |
| 26 | #include "llvm/Support/ThreadPool.h" |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 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: |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 34 | |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 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. |
| 42 | static Expected<std::unique_ptr<LLJIT>> |
| 43 | Create(JITTargetMachineBuilder JTMB, DataLayout DL, |
| 44 | unsigned NumCompileThreads = 0); |
| 45 | |
| 46 | /// Returns the ExecutionSession for this instance. |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 47 | ExecutionSession &getExecutionSession() { return *ES; } |
| 48 | |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 49 | /// Returns a reference to the JITDylib representing the JIT'd main program. |
| 50 | JITDylib &getMainJITDylib() { return Main; } |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 51 | |
| 52 | /// Convenience method for defining an absolute symbol. |
| 53 | Error defineAbsolute(StringRef Name, JITEvaluatedSymbol Address); |
| 54 | |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 55 | /// Convenience method for defining an |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 56 | |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 57 | /// Adds an IR module to the given JITDylib. |
| 58 | Error addIRModule(JITDylib &JD, ThreadSafeModule TSM); |
| 59 | |
| 60 | /// Adds an IR module to the Main JITDylib. |
| 61 | Error addIRModule(ThreadSafeModule TSM) { |
| 62 | return addIRModule(Main, std::move(TSM)); |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 63 | } |
| 64 | |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 65 | /// Adds an object file to the given JITDylib. |
| 66 | Error addObjectFile(JITDylib &JD, std::unique_ptr<MemoryBuffer> Obj); |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 67 | |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 68 | /// Adds an object file to the given JITDylib. |
| 69 | Error addObjectFile(std::unique_ptr<MemoryBuffer> Obj) { |
| 70 | return addObjectFile(Main, std::move(Obj)); |
| 71 | } |
| 72 | |
| 73 | /// Look up a symbol in JITDylib JD by the symbol's linker-mangled name (to |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 74 | /// look up symbols based on their IR name use the lookup function instead). |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 75 | Expected<JITEvaluatedSymbol> lookupLinkerMangled(JITDylib &JD, |
| 76 | StringRef Name); |
| 77 | |
| 78 | /// Look up a symbol in the main JITDylib by the symbol's linker-mangled name |
| 79 | /// (to look up symbols based on their IR name use the lookup function |
| 80 | /// instead). |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 81 | Expected<JITEvaluatedSymbol> lookupLinkerMangled(StringRef Name) { |
| 82 | return lookupLinkerMangled(Main, Name); |
| 83 | } |
| 84 | |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 85 | /// Look up a symbol in JITDylib JD based on its IR symbol name. |
| 86 | Expected<JITEvaluatedSymbol> lookup(JITDylib &JD, StringRef UnmangledName) { |
| 87 | return lookupLinkerMangled(JD, mangle(UnmangledName)); |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 88 | } |
| 89 | |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 90 | /// Look up a symbol in the main JITDylib based on its IR symbol name. |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 91 | Expected<JITEvaluatedSymbol> lookup(StringRef UnmangledName) { |
| 92 | return lookup(Main, UnmangledName); |
| 93 | } |
| 94 | |
| 95 | /// Runs all not-yet-run static constructors. |
| 96 | Error runConstructors() { return CtorRunner.run(); } |
| 97 | |
| 98 | /// Runs all not-yet-run static destructors. |
| 99 | Error runDestructors() { return DtorRunner.run(); } |
| 100 | |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 101 | /// Returns a reference to the ObjLinkingLayer |
| 102 | RTDyldObjectLinkingLayer2 &getObjLinkingLayer() { return ObjLinkingLayer; } |
| 103 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 104 | protected: |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 105 | |
| 106 | /// Create an LLJIT instance with a single compile thread. |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 107 | LLJIT(std::unique_ptr<ExecutionSession> ES, std::unique_ptr<TargetMachine> TM, |
| 108 | DataLayout DL); |
| 109 | |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 110 | /// Create an LLJIT instance with multiple compile threads. |
| 111 | LLJIT(std::unique_ptr<ExecutionSession> ES, JITTargetMachineBuilder JTMB, |
| 112 | DataLayout DL, unsigned NumCompileThreads); |
| 113 | |
| 114 | std::unique_ptr<RuntimeDyld::MemoryManager> getMemoryManager(VModuleKey K); |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 115 | |
| 116 | std::string mangle(StringRef UnmangledName); |
| 117 | |
| 118 | Error applyDataLayout(Module &M); |
| 119 | |
| 120 | void recordCtorDtors(Module &M); |
| 121 | |
| 122 | std::unique_ptr<ExecutionSession> ES; |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 123 | JITDylib &Main; |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 124 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 125 | DataLayout DL; |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 126 | std::unique_ptr<ThreadPool> CompileThreads; |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 127 | |
| 128 | RTDyldObjectLinkingLayer2 ObjLinkingLayer; |
| 129 | IRCompileLayer2 CompileLayer; |
| 130 | |
| 131 | CtorDtorRunner2 CtorRunner, DtorRunner; |
| 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 | public: |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 138 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 139 | /// Create an LLLazyJIT instance. |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 140 | /// If NumCompileThreads is not equal to zero, creates a multi-threaded |
| 141 | /// LLLazyJIT with the given number of compile threads. |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 142 | static Expected<std::unique_ptr<LLLazyJIT>> |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 143 | Create(JITTargetMachineBuilder JTMB, DataLayout DL, |
| 144 | unsigned NumCompileThreads = 0); |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 145 | |
| 146 | /// Set an IR transform (e.g. pass manager pipeline) to run on each function |
| 147 | /// when it is compiled. |
| 148 | void setLazyCompileTransform(IRTransformLayer2::TransformFunction Transform) { |
| 149 | TransformLayer.setTransform(std::move(Transform)); |
| 150 | } |
| 151 | |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 152 | /// Sets the partition function. |
| 153 | void |
| 154 | setPartitionFunction(CompileOnDemandLayer2::PartitionFunction Partition) { |
| 155 | CODLayer.setPartitionFunction(std::move(Partition)); |
| 156 | } |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 157 | |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 158 | /// Add a module to be lazily compiled to JITDylib JD. |
| 159 | Error addLazyIRModule(JITDylib &JD, ThreadSafeModule M); |
| 160 | |
| 161 | /// Add a module to be lazily compiled to the main JITDylib. |
| 162 | Error addLazyIRModule(ThreadSafeModule M) { |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 163 | return addLazyIRModule(Main, std::move(M)); |
| 164 | } |
| 165 | |
| 166 | private: |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 167 | |
| 168 | // Create a single-threaded LLLazyJIT instance. |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 169 | LLLazyJIT(std::unique_ptr<ExecutionSession> ES, |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 170 | std::unique_ptr<TargetMachine> TM, DataLayout DL, |
| 171 | std::unique_ptr<LazyCallThroughManager> LCTMgr, |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 172 | std::function<std::unique_ptr<IndirectStubsManager>()> ISMBuilder); |
| 173 | |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 174 | // Create a multi-threaded LLLazyJIT instance. |
| 175 | LLLazyJIT(std::unique_ptr<ExecutionSession> ES, JITTargetMachineBuilder JTMB, |
| 176 | DataLayout DL, unsigned NumCompileThreads, |
| 177 | std::unique_ptr<LazyCallThroughManager> LCTMgr, |
| 178 | std::function<std::unique_ptr<IndirectStubsManager>()> ISMBuilder); |
| 179 | |
| 180 | std::unique_ptr<LazyCallThroughManager> LCTMgr; |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 181 | std::function<std::unique_ptr<IndirectStubsManager>()> ISMBuilder; |
| 182 | |
| 183 | IRTransformLayer2 TransformLayer; |
| 184 | CompileOnDemandLayer2 CODLayer; |
| 185 | }; |
| 186 | |
| 187 | } // End namespace orc |
| 188 | } // End namespace llvm |
| 189 | |
| 190 | #endif // LLVM_EXECUTIONENGINE_ORC_LLJIT_H |