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" |
Lang Hames | eb14dc7 | 2019-04-29 22:37:27 +0000 | [diff] [blame] | 11 | #include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h" |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 12 | #include "llvm/ExecutionEngine/SectionMemoryManager.h" |
| 13 | #include "llvm/IR/Mangler.h" |
| 14 | |
| 15 | namespace llvm { |
| 16 | namespace orc { |
| 17 | |
Lang Hames | eb14dc7 | 2019-04-29 22:37:27 +0000 | [diff] [blame] | 18 | Error LLJITBuilderState::prepareForConstruction() { |
| 19 | |
| 20 | if (!JTMB) { |
| 21 | if (auto JTMBOrErr = JITTargetMachineBuilder::detectHost()) |
| 22 | JTMB = std::move(*JTMBOrErr); |
| 23 | else |
| 24 | return JTMBOrErr.takeError(); |
| 25 | } |
| 26 | |
| 27 | return Error::success(); |
| 28 | } |
| 29 | |
Lang Hames | f0a3fd88 | 2018-09-26 16:26:59 +0000 | [diff] [blame] | 30 | LLJIT::~LLJIT() { |
| 31 | if (CompileThreads) |
| 32 | CompileThreads->wait(); |
| 33 | } |
| 34 | |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 35 | Error LLJIT::defineAbsolute(StringRef Name, JITEvaluatedSymbol Sym) { |
Lang Hames | 71d781c | 2018-09-30 23:18:24 +0000 | [diff] [blame] | 36 | auto InternedName = ES->intern(Name); |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 37 | SymbolMap Symbols({{InternedName, Sym}}); |
| 38 | return Main.define(absoluteSymbols(std::move(Symbols))); |
| 39 | } |
| 40 | |
Lang Hames | 8d76c71 | 2018-09-26 01:24:12 +0000 | [diff] [blame] | 41 | Error LLJIT::addIRModule(JITDylib &JD, ThreadSafeModule TSM) { |
| 42 | assert(TSM && "Can not add null module"); |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 43 | |
Lang Hames | 8d76c71 | 2018-09-26 01:24:12 +0000 | [diff] [blame] | 44 | if (auto Err = applyDataLayout(*TSM.getModule())) |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 45 | return Err; |
| 46 | |
Lang Hames | eb14dc7 | 2019-04-29 22:37:27 +0000 | [diff] [blame] | 47 | return CompileLayer->add(JD, std::move(TSM), ES->allocateVModule()); |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Lang Hames | 37a6641 | 2018-08-28 20:20:31 +0000 | [diff] [blame] | 50 | Error LLJIT::addObjectFile(JITDylib &JD, std::unique_ptr<MemoryBuffer> Obj) { |
| 51 | assert(Obj && "Can not add null object"); |
| 52 | |
Lang Hames | eb14dc7 | 2019-04-29 22:37:27 +0000 | [diff] [blame] | 53 | return ObjLinkingLayer->add(JD, std::move(Obj), ES->allocateVModule()); |
Lang Hames | 37a6641 | 2018-08-28 20:20:31 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Lang Hames | d5f56c5 | 2018-08-17 21:18:18 +0000 | [diff] [blame] | 56 | Expected<JITEvaluatedSymbol> LLJIT::lookupLinkerMangled(JITDylib &JD, |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 57 | StringRef Name) { |
Lang Hames | 23cb2e7 | 2018-10-23 23:01:39 +0000 | [diff] [blame] | 58 | return ES->lookup(JITDylibSearchList({{&JD, true}}), ES->intern(Name)); |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Lang Hames | eb14dc7 | 2019-04-29 22:37:27 +0000 | [diff] [blame] | 61 | std::unique_ptr<ObjectLayer> |
| 62 | LLJIT::createObjectLinkingLayer(LLJITBuilderState &S, ExecutionSession &ES) { |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 63 | |
Lang Hames | eb14dc7 | 2019-04-29 22:37:27 +0000 | [diff] [blame] | 64 | // If the config state provided an ObjectLinkingLayer factory then use it. |
| 65 | if (S.CreateObjectLinkingLayer) |
| 66 | return S.CreateObjectLinkingLayer(ES); |
Lang Hames | f0a3fd88 | 2018-09-26 16:26:59 +0000 | [diff] [blame] | 67 | |
Lang Hames | eb14dc7 | 2019-04-29 22:37:27 +0000 | [diff] [blame] | 68 | // Otherwise default to creating an RTDyldObjectLinkingLayer that constructs |
| 69 | // a new SectionMemoryManager for each object. |
| 70 | auto GetMemMgr = []() { return llvm::make_unique<SectionMemoryManager>(); }; |
| 71 | return llvm::make_unique<RTDyldObjectLinkingLayer>(ES, std::move(GetMemMgr)); |
| 72 | } |
Lang Hames | 8b81395 | 2018-09-27 21:13:07 +0000 | [diff] [blame] | 73 | |
Lang Hames | 843f198 | 2019-07-10 17:24:24 +0000 | [diff] [blame^] | 74 | Expected<IRCompileLayer::CompileFunction> |
| 75 | LLJIT::createCompileFunction(LLJITBuilderState &S, |
| 76 | JITTargetMachineBuilder JTMB) { |
| 77 | |
| 78 | /// If there is a custom compile function creator set then use it. |
| 79 | if (S.CreateCompileFunction) |
| 80 | return S.CreateCompileFunction(std::move(JTMB)); |
| 81 | |
| 82 | // Otherwise default to creating a SimpleCompiler, or ConcurrentIRCompiler, |
| 83 | // depending on the number of threads requested. |
| 84 | if (S.NumCompileThreads > 0) |
| 85 | return ConcurrentIRCompiler(std::move(JTMB)); |
| 86 | |
| 87 | auto TM = JTMB.createTargetMachine(); |
| 88 | if (!TM) |
| 89 | return TM.takeError(); |
| 90 | |
| 91 | return TMOwningSimpleCompiler(std::move(*TM)); |
| 92 | } |
| 93 | |
Lang Hames | eb14dc7 | 2019-04-29 22:37:27 +0000 | [diff] [blame] | 94 | LLJIT::LLJIT(LLJITBuilderState &S, Error &Err) |
| 95 | : ES(S.ES ? std::move(S.ES) : llvm::make_unique<ExecutionSession>()), |
| 96 | Main(this->ES->getMainJITDylib()), DL(""), CtorRunner(Main), |
| 97 | DtorRunner(Main) { |
| 98 | |
| 99 | ErrorAsOutParameter _(&Err); |
| 100 | |
| 101 | ObjLinkingLayer = createObjectLinkingLayer(S, *ES); |
| 102 | |
Lang Hames | 843f198 | 2019-07-10 17:24:24 +0000 | [diff] [blame^] | 103 | if (auto DLOrErr = S.JTMB->getDefaultDataLayoutForTarget()) |
| 104 | DL = std::move(*DLOrErr); |
| 105 | else { |
| 106 | Err = DLOrErr.takeError(); |
| 107 | return; |
| 108 | } |
Lang Hames | eb14dc7 | 2019-04-29 22:37:27 +0000 | [diff] [blame] | 109 | |
Lang Hames | 843f198 | 2019-07-10 17:24:24 +0000 | [diff] [blame^] | 110 | { |
| 111 | auto CompileFunction = createCompileFunction(S, std::move(*S.JTMB)); |
| 112 | if (!CompileFunction) { |
| 113 | Err = CompileFunction.takeError(); |
Lang Hames | eb14dc7 | 2019-04-29 22:37:27 +0000 | [diff] [blame] | 114 | return; |
| 115 | } |
Lang Hames | 843f198 | 2019-07-10 17:24:24 +0000 | [diff] [blame^] | 116 | CompileLayer = llvm::make_unique<IRCompileLayer>( |
| 117 | *ES, *ObjLinkingLayer, std::move(*CompileFunction)); |
| 118 | } |
Lang Hames | eb14dc7 | 2019-04-29 22:37:27 +0000 | [diff] [blame] | 119 | |
Lang Hames | 843f198 | 2019-07-10 17:24:24 +0000 | [diff] [blame^] | 120 | if (S.NumCompileThreads > 0) { |
| 121 | CompileLayer->setCloneToNewContextOnEmit(true); |
Lang Hames | eb14dc7 | 2019-04-29 22:37:27 +0000 | [diff] [blame] | 122 | CompileThreads = llvm::make_unique<ThreadPool>(S.NumCompileThreads); |
| 123 | ES->setDispatchMaterialization( |
| 124 | [this](JITDylib &JD, std::unique_ptr<MaterializationUnit> MU) { |
| 125 | // FIXME: Switch to move capture once we have c++14. |
| 126 | auto SharedMU = std::shared_ptr<MaterializationUnit>(std::move(MU)); |
| 127 | auto Work = [SharedMU, &JD]() { SharedMU->doMaterialize(JD); }; |
| 128 | CompileThreads->async(std::move(Work)); |
| 129 | }); |
Lang Hames | eb14dc7 | 2019-04-29 22:37:27 +0000 | [diff] [blame] | 130 | } |
Lang Hames | f0a3fd88 | 2018-09-26 16:26:59 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 133 | std::string LLJIT::mangle(StringRef UnmangledName) { |
| 134 | std::string MangledName; |
| 135 | { |
| 136 | raw_string_ostream MangledNameStream(MangledName); |
| 137 | Mangler::getNameWithPrefix(MangledNameStream, UnmangledName, DL); |
| 138 | } |
| 139 | return MangledName; |
| 140 | } |
| 141 | |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 142 | Error LLJIT::applyDataLayout(Module &M) { |
| 143 | if (M.getDataLayout().isDefault()) |
| 144 | M.setDataLayout(DL); |
| 145 | |
| 146 | if (M.getDataLayout() != DL) |
| 147 | return make_error<StringError>( |
| 148 | "Added modules have incompatible data layouts", |
| 149 | inconvertibleErrorCode()); |
| 150 | |
| 151 | return Error::success(); |
| 152 | } |
| 153 | |
| 154 | void LLJIT::recordCtorDtors(Module &M) { |
| 155 | CtorRunner.add(getConstructors(M)); |
| 156 | DtorRunner.add(getDestructors(M)); |
| 157 | } |
| 158 | |
Lang Hames | eb14dc7 | 2019-04-29 22:37:27 +0000 | [diff] [blame] | 159 | Error LLLazyJITBuilderState::prepareForConstruction() { |
| 160 | if (auto Err = LLJITBuilderState::prepareForConstruction()) |
| 161 | return Err; |
| 162 | TT = JTMB->getTargetTriple(); |
| 163 | return Error::success(); |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Lang Hames | 8d76c71 | 2018-09-26 01:24:12 +0000 | [diff] [blame] | 166 | Error LLLazyJIT::addLazyIRModule(JITDylib &JD, ThreadSafeModule TSM) { |
| 167 | assert(TSM && "Can not add null module"); |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 168 | |
Lang Hames | 8d76c71 | 2018-09-26 01:24:12 +0000 | [diff] [blame] | 169 | if (auto Err = applyDataLayout(*TSM.getModule())) |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 170 | return Err; |
| 171 | |
Lang Hames | 8d76c71 | 2018-09-26 01:24:12 +0000 | [diff] [blame] | 172 | recordCtorDtors(*TSM.getModule()); |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 173 | |
Lang Hames | eb14dc7 | 2019-04-29 22:37:27 +0000 | [diff] [blame] | 174 | return CODLayer->add(JD, std::move(TSM), ES->allocateVModule()); |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Lang Hames | eb14dc7 | 2019-04-29 22:37:27 +0000 | [diff] [blame] | 177 | LLLazyJIT::LLLazyJIT(LLLazyJITBuilderState &S, Error &Err) : LLJIT(S, Err) { |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 178 | |
Lang Hames | eb14dc7 | 2019-04-29 22:37:27 +0000 | [diff] [blame] | 179 | // If LLJIT construction failed then bail out. |
| 180 | if (Err) |
| 181 | return; |
| 182 | |
| 183 | ErrorAsOutParameter _(&Err); |
| 184 | |
| 185 | /// Take/Create the lazy-compile callthrough manager. |
| 186 | if (S.LCTMgr) |
| 187 | LCTMgr = std::move(S.LCTMgr); |
| 188 | else { |
| 189 | if (auto LCTMgrOrErr = createLocalLazyCallThroughManager( |
| 190 | S.TT, *ES, S.LazyCompileFailureAddr)) |
| 191 | LCTMgr = std::move(*LCTMgrOrErr); |
| 192 | else { |
| 193 | Err = LCTMgrOrErr.takeError(); |
| 194 | return; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | // Take/Create the indirect stubs manager builder. |
| 199 | auto ISMBuilder = std::move(S.ISMBuilder); |
| 200 | |
| 201 | // If none was provided, try to build one. |
| 202 | if (!ISMBuilder) |
| 203 | ISMBuilder = createLocalIndirectStubsManagerBuilder(S.TT); |
| 204 | |
| 205 | // No luck. Bail out. |
| 206 | if (!ISMBuilder) { |
| 207 | Err = make_error<StringError>("Could not construct " |
| 208 | "IndirectStubsManagerBuilder for target " + |
| 209 | S.TT.str(), |
| 210 | inconvertibleErrorCode()); |
| 211 | return; |
| 212 | } |
| 213 | |
| 214 | // Create the transform layer. |
| 215 | TransformLayer = llvm::make_unique<IRTransformLayer>(*ES, *CompileLayer); |
| 216 | |
| 217 | // Create the COD layer. |
| 218 | CODLayer = llvm::make_unique<CompileOnDemandLayer>( |
| 219 | *ES, *TransformLayer, *LCTMgr, std::move(ISMBuilder)); |
| 220 | |
| 221 | if (S.NumCompileThreads > 0) |
| 222 | CODLayer->setCloneToNewContextOnEmit(true); |
Lang Hames | 8b81395 | 2018-09-27 21:13:07 +0000 | [diff] [blame] | 223 | } |
Lang Hames | f0a3fd88 | 2018-09-26 16:26:59 +0000 | [diff] [blame] | 224 | |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 225 | } // End namespace orc. |
| 226 | } // End namespace llvm. |