Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 1 | //===--------- LLJIT.cpp - An ORC-based JIT for compiling LLVM IR ---------===// |
| 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 details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "llvm/ExecutionEngine/Orc/LLJIT.h" |
| 11 | #include "llvm/ExecutionEngine/Orc/OrcError.h" |
| 12 | #include "llvm/ExecutionEngine/SectionMemoryManager.h" |
| 13 | #include "llvm/IR/Mangler.h" |
| 14 | |
Lang Hames | f0a3fd88 | 2018-09-26 16:26:59 +0000 | [diff] [blame] | 15 | namespace { |
| 16 | |
| 17 | // A SimpleCompiler that owns its TargetMachine. |
| 18 | class TMOwningSimpleCompiler : public llvm::orc::SimpleCompiler { |
| 19 | public: |
| 20 | TMOwningSimpleCompiler(std::unique_ptr<llvm::TargetMachine> TM) |
| 21 | : llvm::orc::SimpleCompiler(*TM), TM(std::move(TM)) {} |
| 22 | private: |
| 23 | // FIXME: shared because std::functions (and thus |
| 24 | // IRCompileLayer2::CompileFunction) are not moveable. |
| 25 | std::shared_ptr<llvm::TargetMachine> TM; |
| 26 | }; |
| 27 | |
| 28 | } // end anonymous namespace |
| 29 | |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 30 | namespace llvm { |
| 31 | namespace orc { |
| 32 | |
Lang Hames | f0a3fd88 | 2018-09-26 16:26:59 +0000 | [diff] [blame] | 33 | LLJIT::~LLJIT() { |
| 34 | if (CompileThreads) |
| 35 | CompileThreads->wait(); |
| 36 | } |
| 37 | |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 38 | Expected<std::unique_ptr<LLJIT>> |
Lang Hames | f0a3fd88 | 2018-09-26 16:26:59 +0000 | [diff] [blame] | 39 | LLJIT::Create(JITTargetMachineBuilder JTMB, DataLayout DL, |
| 40 | unsigned NumCompileThreads) { |
| 41 | |
| 42 | if (NumCompileThreads == 0) { |
| 43 | // If NumCompileThreads == 0 then create a single-threaded LLJIT instance. |
| 44 | auto TM = JTMB.createTargetMachine(); |
| 45 | if (!TM) |
| 46 | return TM.takeError(); |
| 47 | return std::unique_ptr<LLJIT>(new LLJIT(llvm::make_unique<ExecutionSession>(), |
| 48 | std::move(*TM), std::move(DL))); |
| 49 | } |
| 50 | |
Lang Hames | 7c48143 | 2018-09-10 22:08:57 +0000 | [diff] [blame] | 51 | return std::unique_ptr<LLJIT>(new LLJIT(llvm::make_unique<ExecutionSession>(), |
Lang Hames | f0a3fd88 | 2018-09-26 16:26:59 +0000 | [diff] [blame] | 52 | std::move(JTMB), std::move(DL), |
| 53 | NumCompileThreads)); |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | Error LLJIT::defineAbsolute(StringRef Name, JITEvaluatedSymbol Sym) { |
Lang Hames | 71d781c | 2018-09-30 23:18:24 +0000 | [diff] [blame^] | 57 | auto InternedName = ES->intern(Name); |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 58 | SymbolMap Symbols({{InternedName, Sym}}); |
| 59 | return Main.define(absoluteSymbols(std::move(Symbols))); |
| 60 | } |
| 61 | |
Lang Hames | 8d76c71 | 2018-09-26 01:24:12 +0000 | [diff] [blame] | 62 | Error LLJIT::addIRModule(JITDylib &JD, ThreadSafeModule TSM) { |
| 63 | assert(TSM && "Can not add null module"); |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 64 | |
Lang Hames | 8d76c71 | 2018-09-26 01:24:12 +0000 | [diff] [blame] | 65 | if (auto Err = applyDataLayout(*TSM.getModule())) |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 66 | return Err; |
| 67 | |
| 68 | auto K = ES->allocateVModule(); |
Lang Hames | 8d76c71 | 2018-09-26 01:24:12 +0000 | [diff] [blame] | 69 | return CompileLayer.add(JD, K, std::move(TSM)); |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Lang Hames | 37a6641 | 2018-08-28 20:20:31 +0000 | [diff] [blame] | 72 | Error LLJIT::addObjectFile(JITDylib &JD, std::unique_ptr<MemoryBuffer> Obj) { |
| 73 | assert(Obj && "Can not add null object"); |
| 74 | |
| 75 | auto K = ES->allocateVModule(); |
| 76 | return ObjLinkingLayer.add(JD, K, std::move(Obj)); |
| 77 | } |
| 78 | |
Lang Hames | d5f56c5 | 2018-08-17 21:18:18 +0000 | [diff] [blame] | 79 | Expected<JITEvaluatedSymbol> LLJIT::lookupLinkerMangled(JITDylib &JD, |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 80 | StringRef Name) { |
Lang Hames | 71d781c | 2018-09-30 23:18:24 +0000 | [diff] [blame^] | 81 | return llvm::orc::lookup({&JD}, ES->intern(Name)); |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | LLJIT::LLJIT(std::unique_ptr<ExecutionSession> ES, |
| 85 | std::unique_ptr<TargetMachine> TM, DataLayout DL) |
Lang Hames | 8b81395 | 2018-09-27 21:13:07 +0000 | [diff] [blame] | 86 | : ES(std::move(ES)), Main(this->ES->getMainJITDylib()), DL(std::move(DL)), |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 87 | ObjLinkingLayer(*this->ES, |
Lang Hames | fd0c1e71 | 2018-07-20 18:31:50 +0000 | [diff] [blame] | 88 | [this](VModuleKey K) { return getMemoryManager(K); }), |
Lang Hames | 8b81395 | 2018-09-27 21:13:07 +0000 | [diff] [blame] | 89 | CompileLayer(*this->ES, ObjLinkingLayer, |
| 90 | TMOwningSimpleCompiler(std::move(TM))), |
Lang Hames | fd0c1e71 | 2018-07-20 18:31:50 +0000 | [diff] [blame] | 91 | CtorRunner(Main), DtorRunner(Main) {} |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 92 | |
Lang Hames | 8b81395 | 2018-09-27 21:13:07 +0000 | [diff] [blame] | 93 | LLJIT::LLJIT(std::unique_ptr<ExecutionSession> ES, JITTargetMachineBuilder JTMB, |
| 94 | DataLayout DL, unsigned NumCompileThreads) |
| 95 | : ES(std::move(ES)), Main(this->ES->getMainJITDylib()), DL(std::move(DL)), |
Lang Hames | f0a3fd88 | 2018-09-26 16:26:59 +0000 | [diff] [blame] | 96 | ObjLinkingLayer(*this->ES, |
| 97 | [this](VModuleKey K) { return getMemoryManager(K); }), |
Lang Hames | 8b81395 | 2018-09-27 21:13:07 +0000 | [diff] [blame] | 98 | CompileLayer(*this->ES, ObjLinkingLayer, |
| 99 | MultiThreadedSimpleCompiler(std::move(JTMB))), |
Lang Hames | f0a3fd88 | 2018-09-26 16:26:59 +0000 | [diff] [blame] | 100 | CtorRunner(Main), DtorRunner(Main) { |
| 101 | assert(NumCompileThreads != 0 && |
| 102 | "Multithreaded LLJIT instance can not be created with 0 threads"); |
| 103 | |
Lang Hames | 8b81395 | 2018-09-27 21:13:07 +0000 | [diff] [blame] | 104 | // Move modules to new contexts when they're emitted so that we can compile |
| 105 | // them in parallel. |
| 106 | CompileLayer.setCloneToNewContextOnEmit(true); |
| 107 | |
| 108 | // Create a thread pool to compile on and set the execution session |
| 109 | // dispatcher to use the thread pool. |
Lang Hames | f0a3fd88 | 2018-09-26 16:26:59 +0000 | [diff] [blame] | 110 | CompileThreads = llvm::make_unique<ThreadPool>(NumCompileThreads); |
Lang Hames | 8b81395 | 2018-09-27 21:13:07 +0000 | [diff] [blame] | 111 | this->ES->setDispatchMaterialization( |
| 112 | [this](JITDylib &JD, std::unique_ptr<MaterializationUnit> MU) { |
| 113 | // FIXME: Switch to move capture once we have c++14. |
| 114 | auto SharedMU = std::shared_ptr<MaterializationUnit>(std::move(MU)); |
| 115 | auto Work = [SharedMU, &JD]() { SharedMU->doMaterialize(JD); }; |
| 116 | CompileThreads->async(std::move(Work)); |
| 117 | }); |
Lang Hames | f0a3fd88 | 2018-09-26 16:26:59 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Lang Hames | bf98525 | 2018-09-06 19:39:26 +0000 | [diff] [blame] | 120 | std::unique_ptr<RuntimeDyld::MemoryManager> |
Lang Hames | fd0c1e71 | 2018-07-20 18:31:50 +0000 | [diff] [blame] | 121 | LLJIT::getMemoryManager(VModuleKey K) { |
| 122 | return llvm::make_unique<SectionMemoryManager>(); |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | std::string LLJIT::mangle(StringRef UnmangledName) { |
| 126 | std::string MangledName; |
| 127 | { |
| 128 | raw_string_ostream MangledNameStream(MangledName); |
| 129 | Mangler::getNameWithPrefix(MangledNameStream, UnmangledName, DL); |
| 130 | } |
| 131 | return MangledName; |
| 132 | } |
| 133 | |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 134 | Error LLJIT::applyDataLayout(Module &M) { |
| 135 | if (M.getDataLayout().isDefault()) |
| 136 | M.setDataLayout(DL); |
| 137 | |
| 138 | if (M.getDataLayout() != DL) |
| 139 | return make_error<StringError>( |
| 140 | "Added modules have incompatible data layouts", |
| 141 | inconvertibleErrorCode()); |
| 142 | |
| 143 | return Error::success(); |
| 144 | } |
| 145 | |
| 146 | void LLJIT::recordCtorDtors(Module &M) { |
| 147 | CtorRunner.add(getConstructors(M)); |
| 148 | DtorRunner.add(getDestructors(M)); |
| 149 | } |
| 150 | |
| 151 | Expected<std::unique_ptr<LLLazyJIT>> |
Lang Hames | f0a3fd88 | 2018-09-26 16:26:59 +0000 | [diff] [blame] | 152 | LLLazyJIT::Create(JITTargetMachineBuilder JTMB, DataLayout DL, |
| 153 | unsigned NumCompileThreads) { |
Lang Hames | 7c48143 | 2018-09-10 22:08:57 +0000 | [diff] [blame] | 154 | auto ES = llvm::make_unique<ExecutionSession>(); |
| 155 | |
Lang Hames | f0a3fd88 | 2018-09-26 16:26:59 +0000 | [diff] [blame] | 156 | const Triple &TT = JTMB.getTargetTriple(); |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 157 | |
Lang Hames | d804867 | 2018-09-26 05:08:29 +0000 | [diff] [blame] | 158 | auto LCTMgr = createLocalLazyCallThroughManager(TT, *ES, 0); |
| 159 | if (!LCTMgr) |
| 160 | return LCTMgr.takeError(); |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 161 | |
| 162 | auto ISMBuilder = createLocalIndirectStubsManagerBuilder(TT); |
| 163 | if (!ISMBuilder) |
| 164 | return make_error<StringError>( |
| 165 | std::string("No indirect stubs manager builder for ") + TT.str(), |
| 166 | inconvertibleErrorCode()); |
| 167 | |
Lang Hames | f0a3fd88 | 2018-09-26 16:26:59 +0000 | [diff] [blame] | 168 | if (NumCompileThreads == 0) { |
| 169 | auto TM = JTMB.createTargetMachine(); |
| 170 | if (!TM) |
| 171 | return TM.takeError(); |
| 172 | return std::unique_ptr<LLLazyJIT>( |
| 173 | new LLLazyJIT(std::move(ES), std::move(*TM), std::move(DL), |
| 174 | std::move(*LCTMgr), std::move(ISMBuilder))); |
| 175 | } |
| 176 | |
| 177 | return std::unique_ptr<LLLazyJIT>(new LLLazyJIT( |
| 178 | std::move(ES), std::move(JTMB), std::move(DL), NumCompileThreads, |
| 179 | std::move(*LCTMgr), std::move(ISMBuilder))); |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Lang Hames | 8d76c71 | 2018-09-26 01:24:12 +0000 | [diff] [blame] | 182 | Error LLLazyJIT::addLazyIRModule(JITDylib &JD, ThreadSafeModule TSM) { |
| 183 | assert(TSM && "Can not add null module"); |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 184 | |
Lang Hames | 8d76c71 | 2018-09-26 01:24:12 +0000 | [diff] [blame] | 185 | if (auto Err = applyDataLayout(*TSM.getModule())) |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 186 | return Err; |
| 187 | |
Lang Hames | c5192f75 | 2018-09-27 19:27:20 +0000 | [diff] [blame] | 188 | PromoteSymbols(*TSM.getModule()); |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 189 | |
Lang Hames | 8d76c71 | 2018-09-26 01:24:12 +0000 | [diff] [blame] | 190 | recordCtorDtors(*TSM.getModule()); |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 191 | |
| 192 | auto K = ES->allocateVModule(); |
Lang Hames | 8d76c71 | 2018-09-26 01:24:12 +0000 | [diff] [blame] | 193 | return CODLayer.add(JD, K, std::move(TSM)); |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | LLLazyJIT::LLLazyJIT( |
| 197 | std::unique_ptr<ExecutionSession> ES, std::unique_ptr<TargetMachine> TM, |
Lang Hames | d804867 | 2018-09-26 05:08:29 +0000 | [diff] [blame] | 198 | DataLayout DL, std::unique_ptr<LazyCallThroughManager> LCTMgr, |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 199 | std::function<std::unique_ptr<IndirectStubsManager>()> ISMBuilder) |
| 200 | : LLJIT(std::move(ES), std::move(TM), std::move(DL)), |
Lang Hames | d804867 | 2018-09-26 05:08:29 +0000 | [diff] [blame] | 201 | LCTMgr(std::move(LCTMgr)), TransformLayer(*this->ES, CompileLayer), |
| 202 | CODLayer(*this->ES, TransformLayer, *this->LCTMgr, |
| 203 | std::move(ISMBuilder)) {} |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 204 | |
Lang Hames | f0a3fd88 | 2018-09-26 16:26:59 +0000 | [diff] [blame] | 205 | LLLazyJIT::LLLazyJIT( |
| 206 | std::unique_ptr<ExecutionSession> ES, JITTargetMachineBuilder JTMB, |
| 207 | DataLayout DL, unsigned NumCompileThreads, |
| 208 | std::unique_ptr<LazyCallThroughManager> LCTMgr, |
| 209 | std::function<std::unique_ptr<IndirectStubsManager>()> ISMBuilder) |
| 210 | : LLJIT(std::move(ES), std::move(JTMB), std::move(DL), NumCompileThreads), |
| 211 | LCTMgr(std::move(LCTMgr)), TransformLayer(*this->ES, CompileLayer), |
| 212 | CODLayer(*this->ES, TransformLayer, *this->LCTMgr, |
Lang Hames | 8b81395 | 2018-09-27 21:13:07 +0000 | [diff] [blame] | 213 | std::move(ISMBuilder)) { |
| 214 | CODLayer.setCloneToNewContextOnEmit(true); |
| 215 | } |
Lang Hames | f0a3fd88 | 2018-09-26 16:26:59 +0000 | [diff] [blame] | 216 | |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 217 | } // End namespace orc. |
| 218 | } // End namespace llvm. |