diff options
Diffstat (limited to 'lib/ExecutionEngine/Orc/CompileOnDemandLayer.cpp')
-rw-r--r-- | lib/ExecutionEngine/Orc/CompileOnDemandLayer.cpp | 488 |
1 files changed, 224 insertions, 264 deletions
diff --git a/lib/ExecutionEngine/Orc/CompileOnDemandLayer.cpp b/lib/ExecutionEngine/Orc/CompileOnDemandLayer.cpp index d42e7b05ba67..241eb3600da7 100644 --- a/lib/ExecutionEngine/Orc/CompileOnDemandLayer.cpp +++ b/lib/ExecutionEngine/Orc/CompileOnDemandLayer.cpp @@ -8,201 +8,86 @@ //===----------------------------------------------------------------------===// #include "llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h" -#include "llvm/Bitcode/BitcodeReader.h" -#include "llvm/Bitcode/BitcodeWriter.h" #include "llvm/IR/Mangler.h" #include "llvm/IR/Module.h" -#include "llvm/Support/raw_ostream.h" -#include "llvm/Transforms/Utils/Cloning.h" using namespace llvm; using namespace llvm::orc; -namespace { +static ThreadSafeModule extractSubModule(ThreadSafeModule &TSM, + StringRef Suffix, + GVPredicate ShouldExtract) { -template <typename MaterializerFtor> -class LambdaValueMaterializer final : public ValueMaterializer { -public: - LambdaValueMaterializer(MaterializerFtor M) : M(std::move(M)) {} - - Value *materialize(Value *V) final { return M(V); } - -private: - MaterializerFtor M; -}; + auto DeleteExtractedDefs = [](GlobalValue &GV) { + // Bump the linkage: this global will be provided by the external module. + GV.setLinkage(GlobalValue::ExternalLinkage); -template <typename MaterializerFtor> -LambdaValueMaterializer<MaterializerFtor> -createLambdaValueMaterializer(MaterializerFtor M) { - return LambdaValueMaterializer<MaterializerFtor>(std::move(M)); -} -} // namespace - -static void extractAliases(MaterializationResponsibility &R, Module &M, - MangleAndInterner &Mangle) { - SymbolAliasMap Aliases; - - std::vector<GlobalAlias *> ModAliases; - for (auto &A : M.aliases()) - ModAliases.push_back(&A); - - for (auto *A : ModAliases) { - Constant *Aliasee = A->getAliasee(); - assert(A->hasName() && "Anonymous alias?"); - assert(Aliasee->hasName() && "Anonymous aliasee"); - std::string AliasName = A->getName(); - - Aliases[Mangle(AliasName)] = SymbolAliasMapEntry( - {Mangle(Aliasee->getName()), JITSymbolFlags::fromGlobalValue(*A)}); - - if (isa<Function>(Aliasee)) { - auto *F = cloneFunctionDecl(M, *cast<Function>(Aliasee)); - A->replaceAllUsesWith(F); - A->eraseFromParent(); - F->setName(AliasName); - } else if (isa<GlobalValue>(Aliasee)) { - auto *G = cloneGlobalVariableDecl(M, *cast<GlobalVariable>(Aliasee)); - A->replaceAllUsesWith(G); - A->eraseFromParent(); - G->setName(AliasName); - } - } - - R.replace(symbolAliases(std::move(Aliases))); -} - -static std::unique_ptr<Module> -extractAndClone(Module &M, LLVMContext &NewContext, StringRef Suffix, - function_ref<bool(const GlobalValue *)> ShouldCloneDefinition) { - SmallVector<char, 1> ClonedModuleBuffer; - - { - std::set<GlobalValue *> ClonedDefsInSrc; - ValueToValueMapTy VMap; - auto Tmp = CloneModule(M, VMap, [&](const GlobalValue *GV) { - if (ShouldCloneDefinition(GV)) { - ClonedDefsInSrc.insert(const_cast<GlobalValue *>(GV)); - return true; - } - return false; - }); - - for (auto *GV : ClonedDefsInSrc) { - // Delete the definition and bump the linkage in the source module. - if (isa<Function>(GV)) { - auto &F = *cast<Function>(GV); - F.deleteBody(); - F.setPersonalityFn(nullptr); - } else if (isa<GlobalVariable>(GV)) { - cast<GlobalVariable>(GV)->setInitializer(nullptr); + // Delete the definition in the source module. + if (isa<Function>(GV)) { + auto &F = cast<Function>(GV); + F.deleteBody(); + F.setPersonalityFn(nullptr); + } else if (isa<GlobalVariable>(GV)) { + cast<GlobalVariable>(GV).setInitializer(nullptr); + } else if (isa<GlobalAlias>(GV)) { + // We need to turn deleted aliases into function or variable decls based + // on the type of their aliasee. + auto &A = cast<GlobalAlias>(GV); + Constant *Aliasee = A.getAliasee(); + assert(A.hasName() && "Anonymous alias?"); + assert(Aliasee->hasName() && "Anonymous aliasee"); + std::string AliasName = A.getName(); + + if (isa<Function>(Aliasee)) { + auto *F = cloneFunctionDecl(*A.getParent(), *cast<Function>(Aliasee)); + A.replaceAllUsesWith(F); + A.eraseFromParent(); + F->setName(AliasName); + } else if (isa<GlobalVariable>(Aliasee)) { + auto *G = cloneGlobalVariableDecl(*A.getParent(), + *cast<GlobalVariable>(Aliasee)); + A.replaceAllUsesWith(G); + A.eraseFromParent(); + G->setName(AliasName); } else - llvm_unreachable("Unsupported global type"); + llvm_unreachable("Alias to unsupported type"); + } else + llvm_unreachable("Unsupported global type"); + }; - GV->setLinkage(GlobalValue::ExternalLinkage); - } - - BitcodeWriter BCWriter(ClonedModuleBuffer); - - BCWriter.writeModule(*Tmp); - BCWriter.writeSymtab(); - BCWriter.writeStrtab(); - } - - MemoryBufferRef ClonedModuleBufferRef( - StringRef(ClonedModuleBuffer.data(), ClonedModuleBuffer.size()), - "cloned module buffer"); + auto NewTSMod = cloneToNewContext(TSM, ShouldExtract, DeleteExtractedDefs); + auto &M = *NewTSMod.getModule(); + M.setModuleIdentifier((M.getModuleIdentifier() + Suffix).str()); - auto ClonedModule = - cantFail(parseBitcodeFile(ClonedModuleBufferRef, NewContext)); - ClonedModule->setModuleIdentifier((M.getName() + Suffix).str()); - return ClonedModule; -} - -static std::unique_ptr<Module> extractGlobals(Module &M, - LLVMContext &NewContext) { - return extractAndClone(M, NewContext, ".globals", [](const GlobalValue *GV) { - return isa<GlobalVariable>(GV); - }); + return NewTSMod; } namespace llvm { namespace orc { -class ExtractingIRMaterializationUnit : public IRMaterializationUnit { +class PartitioningIRMaterializationUnit : public IRMaterializationUnit { public: - ExtractingIRMaterializationUnit(ExecutionSession &ES, - CompileOnDemandLayer2 &Parent, - std::unique_ptr<Module> M) - : IRMaterializationUnit(ES, std::move(M)), Parent(Parent) {} - - ExtractingIRMaterializationUnit(std::unique_ptr<Module> M, - SymbolFlagsMap SymbolFlags, - SymbolNameToDefinitionMap SymbolToDefinition, - CompileOnDemandLayer2 &Parent) - : IRMaterializationUnit(std::move(M), std::move(SymbolFlags), + PartitioningIRMaterializationUnit(ExecutionSession &ES, ThreadSafeModule TSM, + VModuleKey K, CompileOnDemandLayer &Parent) + : IRMaterializationUnit(ES, std::move(TSM), std::move(K)), + Parent(Parent) {} + + PartitioningIRMaterializationUnit( + ThreadSafeModule TSM, SymbolFlagsMap SymbolFlags, + SymbolNameToDefinitionMap SymbolToDefinition, + CompileOnDemandLayer &Parent) + : IRMaterializationUnit(std::move(TSM), std::move(K), + std::move(SymbolFlags), std::move(SymbolToDefinition)), Parent(Parent) {} private: void materialize(MaterializationResponsibility R) override { - // FIXME: Need a 'notify lazy-extracting/emitting' callback to tie the - // extracted module key, extracted module, and source module key - // together. This could be used, for example, to provide a specific - // memory manager instance to the linking layer. - - auto RequestedSymbols = R.getRequestedSymbols(); - - // Extract the requested functions into a new module. - std::unique_ptr<Module> ExtractedFunctionsModule; - if (!RequestedSymbols.empty()) { - std::string Suffix; - std::set<const GlobalValue *> FunctionsToClone; - for (auto &Name : RequestedSymbols) { - auto I = SymbolToDefinition.find(Name); - assert(I != SymbolToDefinition.end() && I->second != nullptr && - "Should have a non-null definition"); - FunctionsToClone.insert(I->second); - Suffix += "."; - Suffix += *Name; - } - - std::lock_guard<std::mutex> Lock(SourceModuleMutex); - ExtractedFunctionsModule = - extractAndClone(*M, Parent.GetAvailableContext(), Suffix, - [&](const GlobalValue *GV) -> bool { - return FunctionsToClone.count(GV); - }); - } - - // Build a new ExtractingIRMaterializationUnit to delegate the unrequested - // symbols to. - SymbolFlagsMap DelegatedSymbolFlags; - IRMaterializationUnit::SymbolNameToDefinitionMap - DelegatedSymbolToDefinition; - for (auto &KV : SymbolToDefinition) { - if (RequestedSymbols.count(KV.first)) - continue; - DelegatedSymbolFlags[KV.first] = - JITSymbolFlags::fromGlobalValue(*KV.second); - DelegatedSymbolToDefinition[KV.first] = KV.second; - } - - if (!DelegatedSymbolFlags.empty()) { - assert(DelegatedSymbolFlags.size() == - DelegatedSymbolToDefinition.size() && - "SymbolFlags and SymbolToDefinition should have the same number " - "of entries"); - R.replace(llvm::make_unique<ExtractingIRMaterializationUnit>( - std::move(M), std::move(DelegatedSymbolFlags), - std::move(DelegatedSymbolToDefinition), Parent)); - } - - if (ExtractedFunctionsModule) - Parent.emitExtractedFunctionsModule(std::move(R), - std::move(ExtractedFunctionsModule)); + Parent.emitPartition(std::move(R), std::move(TSM), + std::move(SymbolToDefinition)); } - void discard(const VSO &V, SymbolStringPtr Name) override { + void discard(const JITDylib &V, const SymbolStringPtr &Name) override { // All original symbols were materialized by the CODLayer and should be // final. The function bodies provided by M should never be overridden. llvm_unreachable("Discard should never be called on an " @@ -210,44 +95,98 @@ private: } mutable std::mutex SourceModuleMutex; - CompileOnDemandLayer2 &Parent; + CompileOnDemandLayer &Parent; }; -CompileOnDemandLayer2::CompileOnDemandLayer2( - ExecutionSession &ES, IRLayer &BaseLayer, JITCompileCallbackManager &CCMgr, - IndirectStubsManagerBuilder BuildIndirectStubsManager, - GetAvailableContextFunction GetAvailableContext) - : IRLayer(ES), BaseLayer(BaseLayer), CCMgr(CCMgr), - BuildIndirectStubsManager(std::move(BuildIndirectStubsManager)), - GetAvailableContext(std::move(GetAvailableContext)) {} - -Error CompileOnDemandLayer2::add(VSO &V, VModuleKey K, - std::unique_ptr<Module> M) { - return IRLayer::add(V, K, std::move(M)); +Optional<CompileOnDemandLayer::GlobalValueSet> +CompileOnDemandLayer::compileRequested(GlobalValueSet Requested) { + return std::move(Requested); } -void CompileOnDemandLayer2::emit(MaterializationResponsibility R, VModuleKey K, - std::unique_ptr<Module> M) { +Optional<CompileOnDemandLayer::GlobalValueSet> +CompileOnDemandLayer::compileWholeModule(GlobalValueSet Requested) { + return None; +} + +CompileOnDemandLayer::CompileOnDemandLayer( + ExecutionSession &ES, IRLayer &BaseLayer, LazyCallThroughManager &LCTMgr, + IndirectStubsManagerBuilder BuildIndirectStubsManager) + : IRLayer(ES), BaseLayer(BaseLayer), LCTMgr(LCTMgr), + BuildIndirectStubsManager(std::move(BuildIndirectStubsManager)) {} + +void CompileOnDemandLayer::setPartitionFunction(PartitionFunction Partition) { + this->Partition = std::move(Partition); +} + +void CompileOnDemandLayer::emit(MaterializationResponsibility R, + ThreadSafeModule TSM) { + assert(TSM.getModule() && "Null module"); + auto &ES = getExecutionSession(); - assert(M && "M should not be null"); + auto &M = *TSM.getModule(); + + // First, do some cleanup on the module: + cleanUpModule(M); + + // Now sort the callables and non-callables, build re-exports and lodge the + // actual module with the implementation dylib. + auto &PDR = getPerDylibResources(R.getTargetJITDylib()); - for (auto &GV : M->global_values()) - if (GV.hasWeakLinkage()) - GV.setLinkage(GlobalValue::ExternalLinkage); + MangleAndInterner Mangle(ES, M.getDataLayout()); + SymbolAliasMap NonCallables; + SymbolAliasMap Callables; + for (auto &GV : M.global_values()) { + if (GV.isDeclaration() || GV.hasLocalLinkage() || GV.hasAppendingLinkage()) + continue; - MangleAndInterner Mangle(ES, M->getDataLayout()); + auto Name = Mangle(GV.getName()); + auto Flags = JITSymbolFlags::fromGlobalValue(GV); + if (Flags.isCallable()) + Callables[Name] = SymbolAliasMapEntry(Name, Flags); + else + NonCallables[Name] = SymbolAliasMapEntry(Name, Flags); + } + + // Create a partitioning materialization unit and lodge it with the + // implementation dylib. + if (auto Err = PDR.getImplDylib().define( + llvm::make_unique<PartitioningIRMaterializationUnit>( + ES, std::move(TSM), R.getVModuleKey(), *this))) { + ES.reportError(std::move(Err)); + R.failMaterialization(); + return; + } - extractAliases(R, *M, Mangle); + R.replace(reexports(PDR.getImplDylib(), std::move(NonCallables), true)); + R.replace(lazyReexports(LCTMgr, PDR.getISManager(), PDR.getImplDylib(), + std::move(Callables))); +} - auto GlobalsModule = extractGlobals(*M, GetAvailableContext()); +CompileOnDemandLayer::PerDylibResources & +CompileOnDemandLayer::getPerDylibResources(JITDylib &TargetD) { + auto I = DylibResources.find(&TargetD); + if (I == DylibResources.end()) { + auto &ImplD = getExecutionSession().createJITDylib( + TargetD.getName() + ".impl", false); + TargetD.withSearchOrderDo([&](const JITDylibSearchList &TargetSearchOrder) { + auto NewSearchOrder = TargetSearchOrder; + assert(!NewSearchOrder.empty() && + NewSearchOrder.front().first == &TargetD && + NewSearchOrder.front().second == true && + "TargetD must be at the front of its own search order and match " + "non-exported symbol"); + NewSearchOrder.insert(std::next(NewSearchOrder.begin()), {&ImplD, true}); + ImplD.setSearchOrder(std::move(NewSearchOrder), false); + }); + PerDylibResources PDR(ImplD, BuildIndirectStubsManager()); + I = DylibResources.insert(std::make_pair(&TargetD, std::move(PDR))).first; + } - // Delete the bodies of any available externally functions, rename the - // rest, and build the compile callbacks. - std::map<SymbolStringPtr, std::pair<JITTargetAddress, JITSymbolFlags>> - StubCallbacksAndLinkages; - auto &TargetVSO = R.getTargetVSO(); + return I->second; +} - for (auto &F : M->functions()) { +void CompileOnDemandLayer::cleanUpModule(Module &M) { + for (auto &F : M.functions()) { if (F.isDeclaration()) continue; @@ -256,87 +195,108 @@ void CompileOnDemandLayer2::emit(MaterializationResponsibility R, VModuleKey K, F.setPersonalityFn(nullptr); continue; } + } +} - assert(F.hasName() && "Function should have a name"); - std::string StubUnmangledName = F.getName(); - F.setName(F.getName() + "$body"); - auto StubDecl = cloneFunctionDecl(*M, F); - StubDecl->setName(StubUnmangledName); - StubDecl->setPersonalityFn(nullptr); - StubDecl->setLinkage(GlobalValue::ExternalLinkage); - F.replaceAllUsesWith(StubDecl); - - auto StubName = Mangle(StubUnmangledName); - auto BodyName = Mangle(F.getName()); - if (auto CallbackAddr = CCMgr.getCompileCallback( - [BodyName, &TargetVSO, &ES]() -> JITTargetAddress { - if (auto Sym = lookup({&TargetVSO}, BodyName)) - return Sym->getAddress(); - else { - ES.reportError(Sym.takeError()); - return 0; - } - })) { - auto Flags = JITSymbolFlags::fromGlobalValue(F); - Flags &= ~JITSymbolFlags::Weak; - StubCallbacksAndLinkages[std::move(StubName)] = - std::make_pair(*CallbackAddr, Flags); - } else { - ES.reportError(CallbackAddr.takeError()); - R.failMaterialization(); - return; - } +void CompileOnDemandLayer::expandPartition(GlobalValueSet &Partition) { + // Expands the partition to ensure the following rules hold: + // (1) If any alias is in the partition, its aliasee is also in the partition. + // (2) If any aliasee is in the partition, its aliases are also in the + // partiton. + // (3) If any global variable is in the partition then all global variables + // are in the partition. + assert(!Partition.empty() && "Unexpected empty partition"); + + const Module &M = *(*Partition.begin())->getParent(); + bool ContainsGlobalVariables = false; + std::vector<const GlobalValue *> GVsToAdd; + + for (auto *GV : Partition) + if (isa<GlobalAlias>(GV)) + GVsToAdd.push_back( + cast<GlobalValue>(cast<GlobalAlias>(GV)->getAliasee())); + else if (isa<GlobalVariable>(GV)) + ContainsGlobalVariables = true; + + for (auto &A : M.aliases()) + if (Partition.count(cast<GlobalValue>(A.getAliasee()))) + GVsToAdd.push_back(&A); + + if (ContainsGlobalVariables) + for (auto &G : M.globals()) + GVsToAdd.push_back(&G); + + for (auto *GV : GVsToAdd) + Partition.insert(GV); +} + +void CompileOnDemandLayer::emitPartition( + MaterializationResponsibility R, ThreadSafeModule TSM, + IRMaterializationUnit::SymbolNameToDefinitionMap Defs) { + + // FIXME: Need a 'notify lazy-extracting/emitting' callback to tie the + // extracted module key, extracted module, and source module key + // together. This could be used, for example, to provide a specific + // memory manager instance to the linking layer. + + auto &ES = getExecutionSession(); + + GlobalValueSet RequestedGVs; + for (auto &Name : R.getRequestedSymbols()) { + assert(Defs.count(Name) && "No definition for symbol"); + RequestedGVs.insert(Defs[Name]); } - // Build the stub inits map. - IndirectStubsManager::StubInitsMap StubInits; - for (auto &KV : StubCallbacksAndLinkages) - StubInits[*KV.first] = KV.second; + auto GVsToExtract = Partition(RequestedGVs); - // Build the function-body-extracting materialization unit. - if (auto Err = R.getTargetVSO().define( - llvm::make_unique<ExtractingIRMaterializationUnit>(ES, *this, - std::move(M)))) { - ES.reportError(std::move(Err)); - R.failMaterialization(); + // Take a 'None' partition to mean the whole module (as opposed to an empty + // partition, which means "materialize nothing"). Emit the whole module + // unmodified to the base layer. + if (GVsToExtract == None) { + Defs.clear(); + BaseLayer.emit(std::move(R), std::move(TSM)); return; } - // Build the stubs. - // FIXME: Remove function bodies materialization unit if stub creation fails. - auto &StubsMgr = getStubsManager(TargetVSO); - if (auto Err = StubsMgr.createStubs(StubInits)) { - ES.reportError(std::move(Err)); - R.failMaterialization(); + // If the partition is empty, return the whole module to the symbol table. + if (GVsToExtract->empty()) { + R.replace(llvm::make_unique<PartitioningIRMaterializationUnit>( + std::move(TSM), R.getSymbols(), std::move(Defs), *this)); return; } - // Resolve and finalize stubs. - SymbolMap ResolvedStubs; - for (auto &KV : StubCallbacksAndLinkages) { - if (auto Sym = StubsMgr.findStub(*KV.first, false)) - ResolvedStubs[KV.first] = Sym; - else - llvm_unreachable("Stub went missing"); + // Ok -- we actually need to partition the symbols. Promote the symbol + // linkages/names. + // FIXME: We apply this once per partitioning. It's safe, but overkill. + { + auto PromotedGlobals = PromoteSymbols(*TSM.getModule()); + if (!PromotedGlobals.empty()) { + MangleAndInterner Mangle(ES, TSM.getModule()->getDataLayout()); + SymbolFlagsMap SymbolFlags; + for (auto &GV : PromotedGlobals) + SymbolFlags[Mangle(GV->getName())] = + JITSymbolFlags::fromGlobalValue(*GV); + if (auto Err = R.defineMaterializing(SymbolFlags)) { + ES.reportError(std::move(Err)); + R.failMaterialization(); + return; + } + } } - R.resolve(ResolvedStubs); + expandPartition(*GVsToExtract); - BaseLayer.emit(std::move(R), std::move(K), std::move(GlobalsModule)); -} + // Extract the requested partiton (plus any necessary aliases) and + // put the rest back into the impl dylib. + auto ShouldExtract = [&](const GlobalValue &GV) -> bool { + return GVsToExtract->count(&GV); + }; -IndirectStubsManager &CompileOnDemandLayer2::getStubsManager(const VSO &V) { - std::lock_guard<std::mutex> Lock(CODLayerMutex); - StubManagersMap::iterator I = StubsMgrs.find(&V); - if (I == StubsMgrs.end()) - I = StubsMgrs.insert(std::make_pair(&V, BuildIndirectStubsManager())).first; - return *I->second; -} + auto ExtractedTSM = extractSubModule(TSM, ".submodule", ShouldExtract); + R.replace(llvm::make_unique<PartitioningIRMaterializationUnit>( + ES, std::move(TSM), R.getVModuleKey(), *this)); -void CompileOnDemandLayer2::emitExtractedFunctionsModule( - MaterializationResponsibility R, std::unique_ptr<Module> M) { - auto K = getExecutionSession().allocateVModule(); - BaseLayer.emit(std::move(R), std::move(K), std::move(M)); + BaseLayer.emit(std::move(R), std::move(ExtractedTSM)); } } // end namespace orc |