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