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 | |
| 15 | namespace llvm { |
| 16 | namespace orc { |
| 17 | |
| 18 | Expected<std::unique_ptr<LLJIT>> |
| 19 | LLJIT::Create(std::unique_ptr<ExecutionSession> ES, |
| 20 | std::unique_ptr<TargetMachine> TM, DataLayout DL) { |
| 21 | return std::unique_ptr<LLJIT>( |
| 22 | new LLJIT(std::move(ES), std::move(TM), std::move(DL))); |
| 23 | } |
| 24 | |
| 25 | Error LLJIT::defineAbsolute(StringRef Name, JITEvaluatedSymbol Sym) { |
| 26 | auto InternedName = ES->getSymbolStringPool().intern(Name); |
| 27 | SymbolMap Symbols({{InternedName, Sym}}); |
| 28 | return Main.define(absoluteSymbols(std::move(Symbols))); |
| 29 | } |
| 30 | |
Lang Hames | d5f56c5 | 2018-08-17 21:18:18 +0000 | [diff] [blame] | 31 | Error LLJIT::addIRModule(JITDylib &JD, std::unique_ptr<Module> M) { |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 32 | assert(M && "Can not add null module"); |
| 33 | |
| 34 | if (auto Err = applyDataLayout(*M)) |
| 35 | return Err; |
| 36 | |
| 37 | auto K = ES->allocateVModule(); |
Lang Hames | d5f56c5 | 2018-08-17 21:18:18 +0000 | [diff] [blame] | 38 | return CompileLayer.add(JD, K, std::move(M)); |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 39 | } |
| 40 | |
Lang Hames | 37a6641 | 2018-08-28 20:20:31 +0000 | [diff] [blame^] | 41 | Error LLJIT::addObjectFile(JITDylib &JD, std::unique_ptr<MemoryBuffer> Obj) { |
| 42 | assert(Obj && "Can not add null object"); |
| 43 | |
| 44 | auto K = ES->allocateVModule(); |
| 45 | return ObjLinkingLayer.add(JD, K, std::move(Obj)); |
| 46 | } |
| 47 | |
Lang Hames | d5f56c5 | 2018-08-17 21:18:18 +0000 | [diff] [blame] | 48 | Expected<JITEvaluatedSymbol> LLJIT::lookupLinkerMangled(JITDylib &JD, |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 49 | StringRef Name) { |
Lang Hames | d5f56c5 | 2018-08-17 21:18:18 +0000 | [diff] [blame] | 50 | return llvm::orc::lookup({&JD}, ES->getSymbolStringPool().intern(Name)); |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | LLJIT::LLJIT(std::unique_ptr<ExecutionSession> ES, |
| 54 | std::unique_ptr<TargetMachine> TM, DataLayout DL) |
Lang Hames | d5f56c5 | 2018-08-17 21:18:18 +0000 | [diff] [blame] | 55 | : ES(std::move(ES)), Main(this->ES->createJITDylib("main")), |
| 56 | TM(std::move(TM)), DL(std::move(DL)), |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 57 | ObjLinkingLayer(*this->ES, |
Lang Hames | fd0c1e71 | 2018-07-20 18:31:50 +0000 | [diff] [blame] | 58 | [this](VModuleKey K) { return getMemoryManager(K); }), |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 59 | CompileLayer(*this->ES, ObjLinkingLayer, SimpleCompiler(*this->TM)), |
Lang Hames | fd0c1e71 | 2018-07-20 18:31:50 +0000 | [diff] [blame] | 60 | CtorRunner(Main), DtorRunner(Main) {} |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 61 | |
Lang Hames | fd0c1e71 | 2018-07-20 18:31:50 +0000 | [diff] [blame] | 62 | std::shared_ptr<RuntimeDyld::MemoryManager> |
| 63 | LLJIT::getMemoryManager(VModuleKey K) { |
| 64 | return llvm::make_unique<SectionMemoryManager>(); |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | std::string LLJIT::mangle(StringRef UnmangledName) { |
| 68 | std::string MangledName; |
| 69 | { |
| 70 | raw_string_ostream MangledNameStream(MangledName); |
| 71 | Mangler::getNameWithPrefix(MangledNameStream, UnmangledName, DL); |
| 72 | } |
| 73 | return MangledName; |
| 74 | } |
| 75 | |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 76 | Error LLJIT::applyDataLayout(Module &M) { |
| 77 | if (M.getDataLayout().isDefault()) |
| 78 | M.setDataLayout(DL); |
| 79 | |
| 80 | if (M.getDataLayout() != DL) |
| 81 | return make_error<StringError>( |
| 82 | "Added modules have incompatible data layouts", |
| 83 | inconvertibleErrorCode()); |
| 84 | |
| 85 | return Error::success(); |
| 86 | } |
| 87 | |
| 88 | void LLJIT::recordCtorDtors(Module &M) { |
| 89 | CtorRunner.add(getConstructors(M)); |
| 90 | DtorRunner.add(getDestructors(M)); |
| 91 | } |
| 92 | |
| 93 | Expected<std::unique_ptr<LLLazyJIT>> |
| 94 | LLLazyJIT::Create(std::unique_ptr<ExecutionSession> ES, |
| 95 | std::unique_ptr<TargetMachine> TM, DataLayout DL, |
| 96 | LLVMContext &Ctx) { |
| 97 | const Triple &TT = TM->getTargetTriple(); |
| 98 | |
| 99 | auto CCMgr = createLocalCompileCallbackManager(TT, *ES, 0); |
| 100 | if (!CCMgr) |
| 101 | return make_error<StringError>( |
| 102 | std::string("No callback manager available for ") + TT.str(), |
| 103 | inconvertibleErrorCode()); |
| 104 | |
| 105 | auto ISMBuilder = createLocalIndirectStubsManagerBuilder(TT); |
| 106 | if (!ISMBuilder) |
| 107 | return make_error<StringError>( |
| 108 | std::string("No indirect stubs manager builder for ") + TT.str(), |
| 109 | inconvertibleErrorCode()); |
| 110 | |
| 111 | return std::unique_ptr<LLLazyJIT>( |
| 112 | new LLLazyJIT(std::move(ES), std::move(TM), std::move(DL), Ctx, |
| 113 | std::move(CCMgr), std::move(ISMBuilder))); |
| 114 | } |
| 115 | |
Lang Hames | d5f56c5 | 2018-08-17 21:18:18 +0000 | [diff] [blame] | 116 | Error LLLazyJIT::addLazyIRModule(JITDylib &JD, std::unique_ptr<Module> M) { |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 117 | assert(M && "Can not add null module"); |
| 118 | |
| 119 | if (auto Err = applyDataLayout(*M)) |
| 120 | return Err; |
| 121 | |
| 122 | makeAllSymbolsExternallyAccessible(*M); |
| 123 | |
| 124 | recordCtorDtors(*M); |
| 125 | |
| 126 | auto K = ES->allocateVModule(); |
Lang Hames | d5f56c5 | 2018-08-17 21:18:18 +0000 | [diff] [blame] | 127 | return CODLayer.add(JD, K, std::move(M)); |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | LLLazyJIT::LLLazyJIT( |
| 131 | std::unique_ptr<ExecutionSession> ES, std::unique_ptr<TargetMachine> TM, |
| 132 | DataLayout DL, LLVMContext &Ctx, |
| 133 | std::unique_ptr<JITCompileCallbackManager> CCMgr, |
| 134 | std::function<std::unique_ptr<IndirectStubsManager>()> ISMBuilder) |
| 135 | : LLJIT(std::move(ES), std::move(TM), std::move(DL)), |
| 136 | CCMgr(std::move(CCMgr)), TransformLayer(*this->ES, CompileLayer), |
| 137 | CODLayer(*this->ES, TransformLayer, *this->CCMgr, std::move(ISMBuilder), |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 138 | [&]() -> LLVMContext & { return Ctx; }) {} |
| 139 | |
Lang Hames | 6a94134 | 2018-06-26 21:35:48 +0000 | [diff] [blame] | 140 | } // End namespace orc. |
| 141 | } // End namespace llvm. |