2018-06-19 02:01:43 +08:00
|
|
|
//===----- CompileOnDemandLayer.cpp - Lazily emit IR on first call --------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h"
|
|
|
|
#include "llvm/IR/Mangler.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::orc;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename MaterializerFtor>
|
|
|
|
LambdaValueMaterializer<MaterializerFtor>
|
|
|
|
createLambdaValueMaterializer(MaterializerFtor M) {
|
|
|
|
return LambdaValueMaterializer<MaterializerFtor>(std::move(M));
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2018-06-27 05:35:48 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-10 04:54:36 +08:00
|
|
|
R.replace(symbolAliases(std::move(Aliases)));
|
2018-06-27 05:35:48 +08:00
|
|
|
}
|
|
|
|
|
2018-09-26 09:24:12 +08:00
|
|
|
static ThreadSafeModule extractAndClone(ThreadSafeModule &TSM, StringRef Suffix,
|
|
|
|
GVPredicate ShouldCloneDefinition) {
|
2018-06-19 02:01:43 +08:00
|
|
|
|
2018-09-26 09:24:12 +08:00
|
|
|
auto DeleteClonedDefsAndPromoteDeclLinkages = [](GlobalValue &GV) {
|
|
|
|
// 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);
|
|
|
|
} else
|
|
|
|
llvm_unreachable("Unsupported global type");
|
2018-06-19 02:01:43 +08:00
|
|
|
|
2018-09-26 09:24:12 +08:00
|
|
|
GV.setLinkage(GlobalValue::ExternalLinkage);
|
|
|
|
};
|
2018-06-19 02:01:43 +08:00
|
|
|
|
2018-09-26 09:24:12 +08:00
|
|
|
auto NewTSMod = cloneToNewContext(TSM, ShouldCloneDefinition,
|
|
|
|
DeleteClonedDefsAndPromoteDeclLinkages);
|
|
|
|
auto &M = *NewTSMod.getModule();
|
|
|
|
M.setModuleIdentifier((M.getModuleIdentifier() + Suffix).str());
|
2018-06-19 02:01:43 +08:00
|
|
|
|
2018-09-26 09:24:12 +08:00
|
|
|
return NewTSMod;
|
2018-07-06 03:01:27 +08:00
|
|
|
}
|
2018-06-19 02:01:43 +08:00
|
|
|
|
2018-09-26 09:24:12 +08:00
|
|
|
static ThreadSafeModule extractGlobals(ThreadSafeModule &TSM) {
|
|
|
|
return extractAndClone(TSM, ".globals", [](const GlobalValue &GV) {
|
2018-07-06 03:01:27 +08:00
|
|
|
return isa<GlobalVariable>(GV);
|
|
|
|
});
|
2018-06-19 02:01:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
namespace orc {
|
|
|
|
|
|
|
|
class ExtractingIRMaterializationUnit : public IRMaterializationUnit {
|
|
|
|
public:
|
2018-07-21 02:31:50 +08:00
|
|
|
ExtractingIRMaterializationUnit(ExecutionSession &ES,
|
|
|
|
CompileOnDemandLayer2 &Parent,
|
2018-09-26 09:24:12 +08:00
|
|
|
ThreadSafeModule TSM)
|
|
|
|
: IRMaterializationUnit(ES, std::move(TSM)), Parent(Parent) {}
|
2018-07-21 02:31:50 +08:00
|
|
|
|
2018-09-26 09:24:12 +08:00
|
|
|
ExtractingIRMaterializationUnit(ThreadSafeModule TSM,
|
2018-07-21 02:31:50 +08:00
|
|
|
SymbolFlagsMap SymbolFlags,
|
|
|
|
SymbolNameToDefinitionMap SymbolToDefinition,
|
|
|
|
CompileOnDemandLayer2 &Parent)
|
2018-09-26 09:24:12 +08:00
|
|
|
: IRMaterializationUnit(std::move(TSM), 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 {
|
|
|
|
// 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();
|
|
|
|
|
2018-07-06 03:01:27 +08:00
|
|
|
// Extract the requested functions into a new module.
|
2018-09-26 09:24:12 +08:00
|
|
|
ThreadSafeModule ExtractedFunctionsModule;
|
2018-07-06 03:01:27 +08:00
|
|
|
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 =
|
2018-09-26 09:24:12 +08:00
|
|
|
extractAndClone(TSM, Suffix, [&](const GlobalValue &GV) -> bool {
|
|
|
|
return FunctionsToClone.count(&GV);
|
|
|
|
});
|
2018-07-06 03:01:27 +08:00
|
|
|
}
|
2018-06-19 02:01:43 +08:00
|
|
|
|
|
|
|
// 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");
|
2018-07-10 04:54:36 +08:00
|
|
|
R.replace(llvm::make_unique<ExtractingIRMaterializationUnit>(
|
2018-09-26 09:24:12 +08:00
|
|
|
std::move(TSM), std::move(DelegatedSymbolFlags),
|
2018-07-21 02:31:50 +08:00
|
|
|
std::move(DelegatedSymbolToDefinition), Parent));
|
2018-06-19 02:01:43 +08:00
|
|
|
}
|
|
|
|
|
2018-07-06 03:01:27 +08:00
|
|
|
if (ExtractedFunctionsModule)
|
2018-07-21 02:31:50 +08:00
|
|
|
Parent.emitExtractedFunctionsModule(std::move(R),
|
|
|
|
std::move(ExtractedFunctionsModule));
|
2018-06-19 02:01:43 +08:00
|
|
|
}
|
|
|
|
|
2018-08-18 05:18:18 +08:00
|
|
|
void discard(const JITDylib &V, 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-06-19 02:01:43 +08:00
|
|
|
CompileOnDemandLayer2 &Parent;
|
|
|
|
};
|
|
|
|
|
|
|
|
CompileOnDemandLayer2::CompileOnDemandLayer2(
|
|
|
|
ExecutionSession &ES, IRLayer &BaseLayer, JITCompileCallbackManager &CCMgr,
|
2018-09-26 09:24:12 +08:00
|
|
|
IndirectStubsManagerBuilder BuildIndirectStubsManager)
|
2018-06-19 02:01:43 +08:00
|
|
|
: IRLayer(ES), BaseLayer(BaseLayer), CCMgr(CCMgr),
|
2018-09-26 09:24:12 +08:00
|
|
|
BuildIndirectStubsManager(std::move(BuildIndirectStubsManager)) {}
|
2018-06-19 02:01:43 +08:00
|
|
|
|
2018-08-18 05:18:18 +08:00
|
|
|
Error CompileOnDemandLayer2::add(JITDylib &V, VModuleKey K,
|
2018-09-26 09:24:12 +08:00
|
|
|
ThreadSafeModule TSM) {
|
|
|
|
return IRLayer::add(V, K, std::move(TSM));
|
2018-06-19 02:01:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CompileOnDemandLayer2::emit(MaterializationResponsibility R, VModuleKey K,
|
2018-09-26 09:24:12 +08:00
|
|
|
ThreadSafeModule TSM) {
|
2018-06-19 02:01:43 +08:00
|
|
|
auto &ES = getExecutionSession();
|
2018-09-26 09:24:12 +08:00
|
|
|
assert(TSM && "M should not be null");
|
|
|
|
auto &M = *TSM.getModule();
|
2018-06-19 02:01:43 +08:00
|
|
|
|
2018-09-26 09:24:12 +08:00
|
|
|
for (auto &GV : M.global_values())
|
2018-06-19 02:01:43 +08:00
|
|
|
if (GV.hasWeakLinkage())
|
|
|
|
GV.setLinkage(GlobalValue::ExternalLinkage);
|
|
|
|
|
2018-09-26 09:24:12 +08:00
|
|
|
MangleAndInterner Mangle(ES, M.getDataLayout());
|
2018-06-19 02:01:43 +08:00
|
|
|
|
2018-09-26 09:24:12 +08:00
|
|
|
extractAliases(R, *TSM.getModule(), Mangle);
|
2018-06-27 05:35:48 +08:00
|
|
|
|
2018-09-26 09:24:12 +08:00
|
|
|
auto GlobalsModule = extractGlobals(TSM);
|
2018-06-27 05:35:48 +08:00
|
|
|
|
2018-06-19 02:01:43 +08:00
|
|
|
// Delete the bodies of any available externally functions, rename the
|
|
|
|
// rest, and build the compile callbacks.
|
|
|
|
std::map<SymbolStringPtr, std::pair<JITTargetAddress, JITSymbolFlags>>
|
|
|
|
StubCallbacksAndLinkages;
|
2018-08-18 05:18:18 +08:00
|
|
|
auto &TargetJD = R.getTargetJITDylib();
|
2018-06-19 02:01:43 +08:00
|
|
|
|
2018-09-26 09:24:12 +08:00
|
|
|
for (auto &F : M.functions()) {
|
2018-06-19 02:01:43 +08:00
|
|
|
if (F.isDeclaration())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (F.hasAvailableExternallyLinkage()) {
|
|
|
|
F.deleteBody();
|
2018-07-06 03:01:27 +08:00
|
|
|
F.setPersonalityFn(nullptr);
|
2018-06-19 02:01:43 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(F.hasName() && "Function should have a name");
|
2018-06-27 05:35:48 +08:00
|
|
|
std::string StubUnmangledName = F.getName();
|
2018-06-19 02:01:43 +08:00
|
|
|
F.setName(F.getName() + "$body");
|
2018-09-26 09:24:12 +08:00
|
|
|
auto StubDecl = cloneFunctionDecl(*TSM.getModule(), F);
|
2018-06-27 05:35:48 +08:00
|
|
|
StubDecl->setName(StubUnmangledName);
|
2018-07-06 03:01:27 +08:00
|
|
|
StubDecl->setPersonalityFn(nullptr);
|
|
|
|
StubDecl->setLinkage(GlobalValue::ExternalLinkage);
|
2018-06-27 05:35:48 +08:00
|
|
|
F.replaceAllUsesWith(StubDecl);
|
2018-07-06 03:01:27 +08:00
|
|
|
|
2018-06-27 05:35:48 +08:00
|
|
|
auto StubName = Mangle(StubUnmangledName);
|
2018-06-19 02:01:43 +08:00
|
|
|
auto BodyName = Mangle(F.getName());
|
|
|
|
if (auto CallbackAddr = CCMgr.getCompileCallback(
|
2018-08-18 05:18:18 +08:00
|
|
|
[BodyName, &TargetJD, &ES]() -> JITTargetAddress {
|
|
|
|
if (auto Sym = lookup({&TargetJD}, BodyName))
|
2018-06-19 02:01:43 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build the stub inits map.
|
|
|
|
IndirectStubsManager::StubInitsMap StubInits;
|
|
|
|
for (auto &KV : StubCallbacksAndLinkages)
|
|
|
|
StubInits[*KV.first] = KV.second;
|
|
|
|
|
|
|
|
// Build the function-body-extracting materialization unit.
|
2018-08-18 05:18:18 +08:00
|
|
|
if (auto Err = R.getTargetJITDylib().define(
|
2018-07-21 02:31:50 +08:00
|
|
|
llvm::make_unique<ExtractingIRMaterializationUnit>(ES, *this,
|
2018-09-26 09:24:12 +08:00
|
|
|
std::move(TSM)))) {
|
2018-06-19 02:01:43 +08:00
|
|
|
ES.reportError(std::move(Err));
|
|
|
|
R.failMaterialization();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build the stubs.
|
|
|
|
// FIXME: Remove function bodies materialization unit if stub creation fails.
|
2018-08-18 05:18:18 +08:00
|
|
|
auto &StubsMgr = getStubsManager(TargetJD);
|
2018-06-19 02:01:43 +08:00
|
|
|
if (auto Err = StubsMgr.createStubs(StubInits)) {
|
|
|
|
ES.reportError(std::move(Err));
|
|
|
|
R.failMaterialization();
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
|
|
|
R.resolve(ResolvedStubs);
|
|
|
|
|
|
|
|
BaseLayer.emit(std::move(R), std::move(K), std::move(GlobalsModule));
|
|
|
|
}
|
|
|
|
|
2018-08-18 05:18:18 +08:00
|
|
|
IndirectStubsManager &
|
|
|
|
CompileOnDemandLayer2::getStubsManager(const JITDylib &V) {
|
2018-06-19 02:01:43 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CompileOnDemandLayer2::emitExtractedFunctionsModule(
|
2018-09-26 09:24:12 +08:00
|
|
|
MaterializationResponsibility R, ThreadSafeModule TSM) {
|
2018-06-19 02:01:43 +08:00
|
|
|
auto K = getExecutionSession().allocateVModule();
|
2018-09-26 09:24:12 +08:00
|
|
|
BaseLayer.emit(std::move(R), std::move(K), std::move(TSM));
|
2018-06-19 02:01:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // end namespace orc
|
|
|
|
} // end namespace llvm
|