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