1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
|
//===----- CompileOnDemandLayer.cpp - Lazily emit IR on first call --------===//
//
// 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
//
//===----------------------------------------------------------------------===//
#include "llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h"
#include "llvm/ExecutionEngine/Orc/Layer.h"
#include "llvm/IR/Module.h"
using namespace llvm;
using namespace llvm::orc;
CompileOnDemandLayer::CompileOnDemandLayer(
ExecutionSession &ES, IRLayer &BaseLayer, LazyCallThroughManager &LCTMgr,
IndirectStubsManagerBuilder BuildIndirectStubsManager)
: IRLayer(ES, BaseLayer.getManglingOptions()), BaseLayer(BaseLayer),
LCTMgr(LCTMgr),
BuildIndirectStubsManager(std::move(BuildIndirectStubsManager)) {}
void CompileOnDemandLayer::setImplMap(ImplSymbolMap *Imp) {
this->AliaseeImpls = Imp;
}
void CompileOnDemandLayer::emit(
std::unique_ptr<MaterializationResponsibility> R, ThreadSafeModule TSM) {
assert(TSM && "Null module");
auto &ES = getExecutionSession();
// Sort the callables and non-callables, build re-exports and lodge the
// actual module with the implementation dylib.
auto &PDR = getPerDylibResources(R->getTargetJITDylib());
SymbolAliasMap NonCallables;
SymbolAliasMap Callables;
for (auto &KV : R->getSymbols()) {
auto &Name = KV.first;
auto &Flags = KV.second;
if (Flags.isCallable())
Callables[Name] = SymbolAliasMapEntry(Name, Flags);
else
NonCallables[Name] = SymbolAliasMapEntry(Name, Flags);
}
// Lodge symbols with the implementation dylib.
if (auto Err = PDR.getImplDylib().define(
std::make_unique<BasicIRLayerMaterializationUnit>(
BaseLayer, *getManglingOptions(), std::move(TSM)))) {
ES.reportError(std::move(Err));
R->failMaterialization();
return;
}
if (!NonCallables.empty())
if (auto Err =
R->replace(reexports(PDR.getImplDylib(), std::move(NonCallables),
JITDylibLookupFlags::MatchAllSymbols))) {
getExecutionSession().reportError(std::move(Err));
R->failMaterialization();
return;
}
if (!Callables.empty()) {
if (auto Err = R->replace(
lazyReexports(LCTMgr, PDR.getISManager(), PDR.getImplDylib(),
std::move(Callables), AliaseeImpls))) {
getExecutionSession().reportError(std::move(Err));
R->failMaterialization();
return;
}
}
}
CompileOnDemandLayer::PerDylibResources &
CompileOnDemandLayer::getPerDylibResources(JITDylib &TargetD) {
std::lock_guard<std::mutex> Lock(CODLayerMutex);
auto I = DylibResources.find(&TargetD);
if (I == DylibResources.end()) {
auto &ImplD =
getExecutionSession().createBareJITDylib(TargetD.getName() + ".impl");
JITDylibSearchOrder NewLinkOrder;
TargetD.withLinkOrderDo([&](const JITDylibSearchOrder &TargetLinkOrder) {
NewLinkOrder = TargetLinkOrder;
});
assert(!NewLinkOrder.empty() && NewLinkOrder.front().first == &TargetD &&
NewLinkOrder.front().second ==
JITDylibLookupFlags::MatchAllSymbols &&
"TargetD must be at the front of its own search order and match "
"non-exported symbol");
NewLinkOrder.insert(std::next(NewLinkOrder.begin()),
{&ImplD, JITDylibLookupFlags::MatchAllSymbols});
ImplD.setLinkOrder(NewLinkOrder, false);
TargetD.setLinkOrder(std::move(NewLinkOrder), false);
PerDylibResources PDR(ImplD, BuildIndirectStubsManager());
I = DylibResources.insert(std::make_pair(&TargetD, std::move(PDR))).first;
}
return I->second;
}
|