2018-06-19 02:01:43 +08:00
|
|
|
//===----- CompileOnDemandLayer.cpp - Lazily emit IR on first call --------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2018-06-19 02:01:43 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h"
|
|
|
|
#include "llvm/IR/Mangler.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::orc;
|
|
|
|
|
2018-09-30 07:49:57 +08:00
|
|
|
static ThreadSafeModule extractSubModule(ThreadSafeModule &TSM,
|
|
|
|
StringRef Suffix,
|
|
|
|
GVPredicate ShouldExtract) {
|
2018-06-27 05:35:48 +08:00
|
|
|
|
2018-09-30 07:49:57 +08:00
|
|
|
auto DeleteExtractedDefs = [](GlobalValue &GV) {
|
|
|
|
// Bump the linkage: this global will be provided by the external module.
|
|
|
|
GV.setLinkage(GlobalValue::ExternalLinkage);
|
2018-06-19 02:01:43 +08:00
|
|
|
|
2018-09-30 07:49:57 +08:00
|
|
|
// Delete the definition in the source module.
|
2018-09-26 09:24:12 +08:00
|
|
|
if (isa<Function>(GV)) {
|
|
|
|
auto &F = cast<Function>(GV);
|
|
|
|
F.deleteBody();
|
|
|
|
F.setPersonalityFn(nullptr);
|
|
|
|
} else if (isa<GlobalVariable>(GV)) {
|
|
|
|
cast<GlobalVariable>(GV).setInitializer(nullptr);
|
2018-09-30 07:49:57 +08:00
|
|
|
} 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("Alias to unsupported type");
|
2018-09-26 09:24:12 +08:00
|
|
|
} else
|
|
|
|
llvm_unreachable("Unsupported global type");
|
|
|
|
};
|
2018-06-19 02:01:43 +08:00
|
|
|
|
2019-08-02 23:21:37 +08:00
|
|
|
auto NewTSM = cloneToNewContext(TSM, ShouldExtract, DeleteExtractedDefs);
|
|
|
|
NewTSM.withModuleDo([&](Module &M) {
|
|
|
|
M.setModuleIdentifier((M.getModuleIdentifier() + Suffix).str());
|
|
|
|
});
|
2018-06-19 02:01:43 +08:00
|
|
|
|
2019-08-02 23:21:37 +08:00
|
|
|
return NewTSM;
|
2018-07-06 03:01:27 +08:00
|
|
|
}
|
2018-06-19 02:01:43 +08:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
namespace orc {
|
|
|
|
|
2018-09-30 07:49:57 +08:00
|
|
|
class PartitioningIRMaterializationUnit : public IRMaterializationUnit {
|
2018-06-19 02:01:43 +08:00
|
|
|
public:
|
2018-09-30 07:49:57 +08:00
|
|
|
PartitioningIRMaterializationUnit(ExecutionSession &ES, ThreadSafeModule TSM,
|
2018-10-17 04:13:06 +08:00
|
|
|
VModuleKey K, CompileOnDemandLayer &Parent)
|
|
|
|
: IRMaterializationUnit(ES, std::move(TSM), std::move(K)),
|
|
|
|
Parent(Parent) {}
|
2018-07-21 02:31:50 +08:00
|
|
|
|
2018-09-30 07:49:57 +08:00
|
|
|
PartitioningIRMaterializationUnit(
|
|
|
|
ThreadSafeModule TSM, SymbolFlagsMap SymbolFlags,
|
|
|
|
SymbolNameToDefinitionMap SymbolToDefinition,
|
2018-10-16 06:56:10 +08:00
|
|
|
CompileOnDemandLayer &Parent)
|
2018-10-17 04:13:06 +08:00
|
|
|
: IRMaterializationUnit(std::move(TSM), std::move(K),
|
|
|
|
std::move(SymbolFlags),
|
2018-06-19 02:01:43 +08:00
|
|
|
std::move(SymbolToDefinition)),
|
2018-07-21 02:31:50 +08:00
|
|
|
Parent(Parent) {}
|
2018-06-19 02:01:43 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
void materialize(MaterializationResponsibility R) override {
|
2018-09-30 07:49:57 +08:00
|
|
|
Parent.emitPartition(std::move(R), std::move(TSM),
|
|
|
|
std::move(SymbolToDefinition));
|
2018-06-19 02:01:43 +08:00
|
|
|
}
|
|
|
|
|
2018-10-07 07:02:06 +08:00
|
|
|
void discard(const JITDylib &V, const SymbolStringPtr &Name) override {
|
2018-06-19 02:01:43 +08:00
|
|
|
// 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 "
|
|
|
|
"ExtractingIRMaterializationUnit");
|
|
|
|
}
|
|
|
|
|
2018-07-06 03:01:27 +08:00
|
|
|
mutable std::mutex SourceModuleMutex;
|
2018-10-16 06:56:10 +08:00
|
|
|
CompileOnDemandLayer &Parent;
|
2018-06-19 02:01:43 +08:00
|
|
|
};
|
|
|
|
|
2018-10-16 06:56:10 +08:00
|
|
|
Optional<CompileOnDemandLayer::GlobalValueSet>
|
|
|
|
CompileOnDemandLayer::compileRequested(GlobalValueSet Requested) {
|
2018-09-30 07:49:57 +08:00
|
|
|
return std::move(Requested);
|
|
|
|
}
|
|
|
|
|
2018-10-16 06:56:10 +08:00
|
|
|
Optional<CompileOnDemandLayer::GlobalValueSet>
|
|
|
|
CompileOnDemandLayer::compileWholeModule(GlobalValueSet Requested) {
|
2018-09-30 07:49:57 +08:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2018-10-16 06:56:10 +08:00
|
|
|
CompileOnDemandLayer::CompileOnDemandLayer(
|
2018-09-26 13:08:29 +08:00
|
|
|
ExecutionSession &ES, IRLayer &BaseLayer, LazyCallThroughManager &LCTMgr,
|
2018-09-26 09:24:12 +08:00
|
|
|
IndirectStubsManagerBuilder BuildIndirectStubsManager)
|
2018-09-26 13:08:29 +08:00
|
|
|
: IRLayer(ES), BaseLayer(BaseLayer), LCTMgr(LCTMgr),
|
2018-09-26 09:24:12 +08:00
|
|
|
BuildIndirectStubsManager(std::move(BuildIndirectStubsManager)) {}
|
2018-06-19 02:01:43 +08:00
|
|
|
|
2018-10-16 06:56:10 +08:00
|
|
|
void CompileOnDemandLayer::setPartitionFunction(PartitionFunction Partition) {
|
2018-09-30 07:49:57 +08:00
|
|
|
this->Partition = std::move(Partition);
|
2018-06-19 02:01:43 +08:00
|
|
|
}
|
|
|
|
|
2019-08-03 22:42:13 +08:00
|
|
|
void CompileOnDemandLayer::setImplMap(ImplSymbolMap *Imp) {
|
|
|
|
this->AliaseeImpls = Imp;
|
|
|
|
}
|
2018-10-17 04:13:06 +08:00
|
|
|
void CompileOnDemandLayer::emit(MaterializationResponsibility R,
|
|
|
|
ThreadSafeModule TSM) {
|
2019-08-02 23:21:37 +08:00
|
|
|
assert(TSM && "Null module");
|
2018-09-30 07:49:57 +08:00
|
|
|
|
2018-06-19 02:01:43 +08:00
|
|
|
auto &ES = getExecutionSession();
|
|
|
|
|
2019-08-02 23:21:37 +08:00
|
|
|
// Sort the callables and non-callables, build re-exports and lodge the
|
2018-09-30 07:49:57 +08:00
|
|
|
// actual module with the implementation dylib.
|
|
|
|
auto &PDR = getPerDylibResources(R.getTargetJITDylib());
|
2018-06-19 02:01:43 +08:00
|
|
|
|
2018-09-30 07:49:57 +08:00
|
|
|
SymbolAliasMap NonCallables;
|
|
|
|
SymbolAliasMap Callables;
|
2019-08-02 23:21:37 +08:00
|
|
|
TSM.withModuleDo([&](Module &M) {
|
|
|
|
// First, do some cleanup on the module:
|
|
|
|
cleanUpModule(M);
|
|
|
|
|
|
|
|
MangleAndInterner Mangle(ES, M.getDataLayout());
|
|
|
|
for (auto &GV : M.global_values()) {
|
|
|
|
if (GV.isDeclaration() || GV.hasLocalLinkage() ||
|
|
|
|
GV.hasAppendingLinkage())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
auto Name = Mangle(GV.getName());
|
|
|
|
auto Flags = JITSymbolFlags::fromGlobalValue(GV);
|
|
|
|
if (Flags.isCallable())
|
|
|
|
Callables[Name] = SymbolAliasMapEntry(Name, Flags);
|
|
|
|
else
|
|
|
|
NonCallables[Name] = SymbolAliasMapEntry(Name, Flags);
|
|
|
|
}
|
|
|
|
});
|
2018-06-19 02:01:43 +08:00
|
|
|
|
2018-09-30 07:49:57 +08:00
|
|
|
// Create a partitioning materialization unit and lodge it with the
|
|
|
|
// implementation dylib.
|
|
|
|
if (auto Err = PDR.getImplDylib().define(
|
2019-08-15 23:54:37 +08:00
|
|
|
std::make_unique<PartitioningIRMaterializationUnit>(
|
2018-10-17 04:13:06 +08:00
|
|
|
ES, std::move(TSM), R.getVModuleKey(), *this))) {
|
2018-06-19 02:01:43 +08:00
|
|
|
ES.reportError(std::move(Err));
|
|
|
|
R.failMaterialization();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-24 07:01:39 +08:00
|
|
|
R.replace(reexports(PDR.getImplDylib(), std::move(NonCallables), true));
|
2018-09-30 07:49:57 +08:00
|
|
|
R.replace(lazyReexports(LCTMgr, PDR.getISManager(), PDR.getImplDylib(),
|
2019-08-03 22:42:13 +08:00
|
|
|
std::move(Callables), AliaseeImpls));
|
2018-06-19 02:01:43 +08:00
|
|
|
}
|
|
|
|
|
2018-10-16 06:56:10 +08:00
|
|
|
CompileOnDemandLayer::PerDylibResources &
|
|
|
|
CompileOnDemandLayer::getPerDylibResources(JITDylib &TargetD) {
|
2018-09-26 13:08:29 +08:00
|
|
|
auto I = DylibResources.find(&TargetD);
|
|
|
|
if (I == DylibResources.end()) {
|
2018-10-24 07:01:39 +08:00
|
|
|
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);
|
2018-09-26 13:08:29 +08:00
|
|
|
});
|
|
|
|
PerDylibResources PDR(ImplD, BuildIndirectStubsManager());
|
|
|
|
I = DylibResources.insert(std::make_pair(&TargetD, std::move(PDR))).first;
|
|
|
|
}
|
|
|
|
|
|
|
|
return I->second;
|
2018-06-19 02:01:43 +08:00
|
|
|
}
|
|
|
|
|
2018-10-16 06:56:10 +08:00
|
|
|
void CompileOnDemandLayer::cleanUpModule(Module &M) {
|
2018-09-30 07:49:57 +08:00
|
|
|
for (auto &F : M.functions()) {
|
|
|
|
if (F.isDeclaration())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (F.hasAvailableExternallyLinkage()) {
|
|
|
|
F.deleteBody();
|
|
|
|
F.setPersonalityFn(nullptr);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-16 06:56:10 +08:00
|
|
|
void CompileOnDemandLayer::expandPartition(GlobalValueSet &Partition) {
|
2018-09-30 07:49:57 +08:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
2018-10-16 06:56:10 +08:00
|
|
|
void CompileOnDemandLayer::emitPartition(
|
2018-09-30 07:49:57 +08:00
|
|
|
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]);
|
|
|
|
}
|
|
|
|
|
2019-08-02 23:21:37 +08:00
|
|
|
/// Perform partitioning with the context lock held, since the partition
|
|
|
|
/// function is allowed to access the globals to compute the partition.
|
|
|
|
auto GVsToExtract =
|
|
|
|
TSM.withModuleDo([&](Module &M) { return Partition(RequestedGVs); });
|
2018-09-30 07:49:57 +08:00
|
|
|
|
|
|
|
// 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();
|
2018-10-17 04:13:06 +08:00
|
|
|
BaseLayer.emit(std::move(R), std::move(TSM));
|
2018-09-30 07:49:57 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the partition is empty, return the whole module to the symbol table.
|
|
|
|
if (GVsToExtract->empty()) {
|
2019-08-15 23:54:37 +08:00
|
|
|
R.replace(std::make_unique<PartitioningIRMaterializationUnit>(
|
2018-09-30 07:49:57 +08:00
|
|
|
std::move(TSM), R.getSymbols(), std::move(Defs), *this));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-10 04:44:32 +08:00
|
|
|
// Ok -- we actually need to partition the symbols. Promote the symbol
|
2019-08-02 23:21:37 +08:00
|
|
|
// linkages/names, expand the partition to include any required symbols
|
|
|
|
// (i.e. symbols that can't be separated from our partition), and
|
|
|
|
// then extract the partition.
|
|
|
|
//
|
|
|
|
// FIXME: We apply this promotion once per partitioning. It's safe, but
|
|
|
|
// overkill.
|
|
|
|
|
|
|
|
auto ExtractedTSM =
|
|
|
|
TSM.withModuleDo([&](Module &M) -> Expected<ThreadSafeModule> {
|
|
|
|
auto PromotedGlobals = PromoteSymbols(M);
|
|
|
|
if (!PromotedGlobals.empty()) {
|
|
|
|
MangleAndInterner Mangle(ES, M.getDataLayout());
|
|
|
|
SymbolFlagsMap SymbolFlags;
|
|
|
|
for (auto &GV : PromotedGlobals)
|
|
|
|
SymbolFlags[Mangle(GV->getName())] =
|
|
|
|
JITSymbolFlags::fromGlobalValue(*GV);
|
|
|
|
if (auto Err = R.defineMaterializing(SymbolFlags))
|
|
|
|
return std::move(Err);
|
|
|
|
}
|
|
|
|
|
|
|
|
expandPartition(*GVsToExtract);
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
};
|
|
|
|
|
|
|
|
return extractSubModule(TSM, ".submodule", ShouldExtract);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!ExtractedTSM) {
|
|
|
|
ES.reportError(ExtractedTSM.takeError());
|
|
|
|
R.failMaterialization();
|
|
|
|
return;
|
2018-10-10 04:44:32 +08:00
|
|
|
}
|
|
|
|
|
2019-08-15 23:54:37 +08:00
|
|
|
R.replace(std::make_unique<PartitioningIRMaterializationUnit>(
|
2018-10-17 04:13:06 +08:00
|
|
|
ES, std::move(TSM), R.getVModuleKey(), *this));
|
2019-08-02 23:21:37 +08:00
|
|
|
BaseLayer.emit(std::move(R), std::move(*ExtractedTSM));
|
2018-06-19 02:01:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // end namespace orc
|
|
|
|
} // end namespace llvm
|