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 | // |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame^] | 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 |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 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 | |
| 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" |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 21 | #include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h" |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 22 | #include "llvm/ExecutionEngine/Orc/ObjectTransformLayer.h" |
| 23 | #include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h" |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 24 | #include "llvm/ExecutionEngine/Orc/ThreadSafeModule.h" |
| 25 | #include "llvm/Support/ThreadPool.h" |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 26 | |
| 27 | namespace llvm { |
| 28 | namespace orc { |
| 29 | |
| 30 | /// A pre-fabricated ORC JIT stack that can serve as an alternative to MCJIT. |
| 31 | class LLJIT { |
| 32 | public: |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 33 | |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 34 | /// Destruct this instance. If a multi-threaded instance, waits for all |
| 35 | /// compile threads to complete. |
| 36 | ~LLJIT(); |
| 37 | |
| 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); |
| 44 | |
| 45 | /// Returns the ExecutionSession for this instance. |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 46 | ExecutionSession &getExecutionSession() { return *ES; } |
| 47 | |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 48 | /// Returns a reference to the JITDylib representing the JIT'd main program. |
| 49 | JITDylib &getMainJITDylib() { return Main; } |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 50 | |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame^] | 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)); |
| 54 | } |
| 55 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 56 | /// Convenience method for defining an absolute symbol. |
| 57 | Error defineAbsolute(StringRef Name, JITEvaluatedSymbol Address); |
| 58 | |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 59 | /// Convenience method for defining an |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 60 | |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 61 | /// Adds an IR module to the given JITDylib. |
| 62 | Error addIRModule(JITDylib &JD, ThreadSafeModule TSM); |
| 63 | |
| 64 | /// Adds an IR module to the Main JITDylib. |
| 65 | Error addIRModule(ThreadSafeModule TSM) { |
| 66 | return addIRModule(Main, std::move(TSM)); |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 67 | } |
| 68 | |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 69 | /// Adds an object file to the given JITDylib. |
| 70 | Error addObjectFile(JITDylib &JD, std::unique_ptr<MemoryBuffer> Obj); |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 71 | |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 72 | /// Adds an object file to the given JITDylib. |
| 73 | Error addObjectFile(std::unique_ptr<MemoryBuffer> Obj) { |
| 74 | return addObjectFile(Main, std::move(Obj)); |
| 75 | } |
| 76 | |
| 77 | /// 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] | 78 | /// 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] | 79 | Expected<JITEvaluatedSymbol> lookupLinkerMangled(JITDylib &JD, |
| 80 | StringRef Name); |
| 81 | |
| 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). |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 85 | Expected<JITEvaluatedSymbol> lookupLinkerMangled(StringRef Name) { |
| 86 | return lookupLinkerMangled(Main, Name); |
| 87 | } |
| 88 | |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 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)); |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 92 | } |
| 93 | |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 94 | /// 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] | 95 | Expected<JITEvaluatedSymbol> lookup(StringRef UnmangledName) { |
| 96 | return lookup(Main, UnmangledName); |
| 97 | } |
| 98 | |
| 99 | /// Runs all not-yet-run static constructors. |
| 100 | Error runConstructors() { return CtorRunner.run(); } |
| 101 | |
| 102 | /// Runs all not-yet-run static destructors. |
| 103 | Error runDestructors() { return DtorRunner.run(); } |
| 104 | |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 105 | /// Returns a reference to the ObjLinkingLayer |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame^] | 106 | RTDyldObjectLinkingLayer &getObjLinkingLayer() { return ObjLinkingLayer; } |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 107 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 108 | protected: |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 109 | |
| 110 | /// Create an LLJIT instance with a single compile thread. |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 111 | LLJIT(std::unique_ptr<ExecutionSession> ES, std::unique_ptr<TargetMachine> TM, |
| 112 | DataLayout DL); |
| 113 | |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 114 | /// Create an LLJIT instance with multiple compile threads. |
| 115 | LLJIT(std::unique_ptr<ExecutionSession> ES, JITTargetMachineBuilder JTMB, |
| 116 | DataLayout DL, unsigned NumCompileThreads); |
| 117 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 118 | std::string mangle(StringRef UnmangledName); |
| 119 | |
| 120 | Error applyDataLayout(Module &M); |
| 121 | |
| 122 | void recordCtorDtors(Module &M); |
| 123 | |
| 124 | std::unique_ptr<ExecutionSession> ES; |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 125 | JITDylib &Main; |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 126 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 127 | DataLayout DL; |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 128 | std::unique_ptr<ThreadPool> CompileThreads; |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 129 | |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame^] | 130 | RTDyldObjectLinkingLayer ObjLinkingLayer; |
| 131 | IRCompileLayer CompileLayer; |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 132 | |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame^] | 133 | CtorDtorRunner CtorRunner, DtorRunner; |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 134 | }; |
| 135 | |
| 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: |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 140 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 141 | /// Create an LLLazyJIT instance. |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 142 | /// If NumCompileThreads is not equal to zero, creates a multi-threaded |
| 143 | /// LLLazyJIT with the given number of compile threads. |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 144 | static Expected<std::unique_ptr<LLLazyJIT>> |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 145 | Create(JITTargetMachineBuilder JTMB, DataLayout DL, |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame^] | 146 | JITTargetAddress ErrorAddr, unsigned NumCompileThreads = 0); |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 147 | |
| 148 | /// Set an IR transform (e.g. pass manager pipeline) to run on each function |
| 149 | /// when it is compiled. |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame^] | 150 | void setLazyCompileTransform(IRTransformLayer::TransformFunction Transform) { |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 151 | TransformLayer.setTransform(std::move(Transform)); |
| 152 | } |
| 153 | |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 154 | /// Sets the partition function. |
| 155 | void |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame^] | 156 | setPartitionFunction(CompileOnDemandLayer::PartitionFunction Partition) { |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 157 | CODLayer.setPartitionFunction(std::move(Partition)); |
| 158 | } |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 159 | |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 160 | /// Add a module to be lazily compiled to JITDylib JD. |
| 161 | Error addLazyIRModule(JITDylib &JD, ThreadSafeModule M); |
| 162 | |
| 163 | /// Add a module to be lazily compiled to the main JITDylib. |
| 164 | Error addLazyIRModule(ThreadSafeModule M) { |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 165 | return addLazyIRModule(Main, std::move(M)); |
| 166 | } |
| 167 | |
| 168 | private: |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 169 | |
| 170 | // Create a single-threaded LLLazyJIT instance. |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 171 | LLLazyJIT(std::unique_ptr<ExecutionSession> ES, |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 172 | std::unique_ptr<TargetMachine> TM, DataLayout DL, |
| 173 | std::unique_ptr<LazyCallThroughManager> LCTMgr, |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 174 | std::function<std::unique_ptr<IndirectStubsManager>()> ISMBuilder); |
| 175 | |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 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); |
| 181 | |
| 182 | std::unique_ptr<LazyCallThroughManager> LCTMgr; |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 183 | std::function<std::unique_ptr<IndirectStubsManager>()> ISMBuilder; |
| 184 | |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame^] | 185 | IRTransformLayer TransformLayer; |
| 186 | CompileOnDemandLayer CODLayer; |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 187 | }; |
| 188 | |
| 189 | } // End namespace orc |
| 190 | } // End namespace llvm |
| 191 | |
| 192 | #endif // LLVM_EXECUTIONENGINE_ORC_LLJIT_H |