2018-05-22 05:11:13 +08:00
|
|
|
//===-------------------- Layer.cpp - Layer interfaces --------------------===//
|
|
|
|
//
|
|
|
|
// 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/Layer.h"
|
|
|
|
#include "llvm/Object/ObjectFile.h"
|
2018-09-28 23:03:11 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "orc"
|
2018-05-22 05:11:13 +08:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
namespace orc {
|
|
|
|
|
|
|
|
IRLayer::IRLayer(ExecutionSession &ES) : ES(ES) {}
|
|
|
|
IRLayer::~IRLayer() {}
|
|
|
|
|
2018-10-17 04:13:06 +08:00
|
|
|
Error IRLayer::add(JITDylib &JD, ThreadSafeModule TSM, VModuleKey K) {
|
2018-08-18 05:18:18 +08:00
|
|
|
return JD.define(llvm::make_unique<BasicIRLayerMaterializationUnit>(
|
2018-09-26 09:24:12 +08:00
|
|
|
*this, std::move(K), std::move(TSM)));
|
2018-05-22 05:11:13 +08:00
|
|
|
}
|
|
|
|
|
2018-05-23 00:15:38 +08:00
|
|
|
IRMaterializationUnit::IRMaterializationUnit(ExecutionSession &ES,
|
2018-10-17 04:13:06 +08:00
|
|
|
ThreadSafeModule TSM, VModuleKey K)
|
|
|
|
: MaterializationUnit(SymbolFlagsMap(), std::move(K)), TSM(std::move(TSM)) {
|
2018-05-22 05:11:13 +08:00
|
|
|
|
2018-09-26 09:24:12 +08:00
|
|
|
assert(this->TSM && "Module must not be null");
|
|
|
|
|
|
|
|
MangleAndInterner Mangle(ES, this->TSM.getModule()->getDataLayout());
|
|
|
|
for (auto &G : this->TSM.getModule()->global_values()) {
|
2018-06-27 05:35:48 +08:00
|
|
|
if (G.hasName() && !G.isDeclaration() && !G.hasLocalLinkage() &&
|
|
|
|
!G.hasAvailableExternallyLinkage() && !G.hasAppendingLinkage()) {
|
2018-05-22 05:11:13 +08:00
|
|
|
auto MangledName = Mangle(G.getName());
|
|
|
|
SymbolFlags[MangledName] = JITSymbolFlags::fromGlobalValue(G);
|
2018-06-01 03:29:01 +08:00
|
|
|
SymbolToDefinition[MangledName] = &G;
|
2018-05-22 05:11:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-04 03:22:48 +08:00
|
|
|
IRMaterializationUnit::IRMaterializationUnit(
|
2018-10-17 04:13:06 +08:00
|
|
|
ThreadSafeModule TSM, VModuleKey K, SymbolFlagsMap SymbolFlags,
|
2018-06-04 03:22:48 +08:00
|
|
|
SymbolNameToDefinitionMap SymbolToDefinition)
|
2018-10-17 04:13:06 +08:00
|
|
|
: MaterializationUnit(std::move(SymbolFlags), std::move(K)),
|
|
|
|
TSM(std::move(TSM)), SymbolToDefinition(std::move(SymbolToDefinition)) {}
|
2018-06-04 03:22:48 +08:00
|
|
|
|
2018-09-28 23:03:11 +08:00
|
|
|
StringRef IRMaterializationUnit::getName() const {
|
|
|
|
if (TSM.getModule())
|
|
|
|
return TSM.getModule()->getModuleIdentifier();
|
|
|
|
return "<null module>";
|
|
|
|
}
|
|
|
|
|
2018-10-07 07:02:06 +08:00
|
|
|
void IRMaterializationUnit::discard(const JITDylib &JD,
|
|
|
|
const SymbolStringPtr &Name) {
|
2018-09-28 23:03:11 +08:00
|
|
|
LLVM_DEBUG(JD.getExecutionSession().runSessionLocked([&]() {
|
|
|
|
dbgs() << "In " << JD.getName() << " discarding " << *Name << " from MU@"
|
|
|
|
<< this << " (" << getName() << ")\n";
|
|
|
|
}););
|
|
|
|
|
2018-06-01 03:29:01 +08:00
|
|
|
auto I = SymbolToDefinition.find(Name);
|
|
|
|
assert(I != SymbolToDefinition.end() &&
|
2018-05-22 05:11:13 +08:00
|
|
|
"Symbol not provided by this MU, or previously discarded");
|
2018-06-01 03:29:01 +08:00
|
|
|
assert(!I->second->isDeclaration() &&
|
|
|
|
"Discard should only apply to definitions");
|
2018-05-22 05:11:13 +08:00
|
|
|
I->second->setLinkage(GlobalValue::AvailableExternallyLinkage);
|
2018-06-01 03:29:01 +08:00
|
|
|
SymbolToDefinition.erase(I);
|
2018-05-22 05:11:13 +08:00
|
|
|
}
|
|
|
|
|
2018-05-23 00:15:38 +08:00
|
|
|
BasicIRLayerMaterializationUnit::BasicIRLayerMaterializationUnit(
|
2018-09-26 09:24:12 +08:00
|
|
|
IRLayer &L, VModuleKey K, ThreadSafeModule TSM)
|
2018-10-17 04:13:06 +08:00
|
|
|
: IRMaterializationUnit(L.getExecutionSession(), std::move(TSM),
|
|
|
|
std::move(K)),
|
|
|
|
L(L), K(std::move(K)) {}
|
2018-05-23 00:15:38 +08:00
|
|
|
|
|
|
|
void BasicIRLayerMaterializationUnit::materialize(
|
|
|
|
MaterializationResponsibility R) {
|
2018-09-26 09:24:12 +08:00
|
|
|
|
2018-09-30 07:49:56 +08:00
|
|
|
// Throw away the SymbolToDefinition map: it's not usable after we hand
|
|
|
|
// off the module.
|
|
|
|
SymbolToDefinition.clear();
|
|
|
|
|
|
|
|
// If cloneToNewContextOnEmit is set, clone the module now.
|
2018-09-26 09:24:12 +08:00
|
|
|
if (L.getCloneToNewContextOnEmit())
|
|
|
|
TSM = cloneToNewContext(TSM);
|
|
|
|
|
2018-09-28 23:03:11 +08:00
|
|
|
#ifndef NDEBUG
|
|
|
|
auto &ES = R.getTargetJITDylib().getExecutionSession();
|
|
|
|
#endif // NDEBUG
|
|
|
|
|
2018-09-26 09:24:12 +08:00
|
|
|
auto Lock = TSM.getContextLock();
|
2018-09-28 23:03:11 +08:00
|
|
|
LLVM_DEBUG(ES.runSessionLocked([&]() {
|
|
|
|
dbgs() << "Emitting, for " << R.getTargetJITDylib().getName() << ", "
|
|
|
|
<< *this << "\n";
|
|
|
|
}););
|
2018-10-17 04:13:06 +08:00
|
|
|
L.emit(std::move(R), std::move(TSM));
|
2018-09-28 23:03:11 +08:00
|
|
|
LLVM_DEBUG(ES.runSessionLocked([&]() {
|
|
|
|
dbgs() << "Finished emitting, for " << R.getTargetJITDylib().getName()
|
|
|
|
<< ", " << *this << "\n";
|
|
|
|
}););
|
2018-05-23 00:15:38 +08:00
|
|
|
}
|
|
|
|
|
2018-05-22 05:11:13 +08:00
|
|
|
ObjectLayer::ObjectLayer(ExecutionSession &ES) : ES(ES) {}
|
|
|
|
|
|
|
|
ObjectLayer::~ObjectLayer() {}
|
|
|
|
|
2018-10-17 04:13:06 +08:00
|
|
|
Error ObjectLayer::add(JITDylib &JD, std::unique_ptr<MemoryBuffer> O,
|
|
|
|
VModuleKey K) {
|
2018-08-02 06:42:23 +08:00
|
|
|
auto ObjMU = BasicObjectLayerMaterializationUnit::Create(*this, std::move(K),
|
|
|
|
std::move(O));
|
|
|
|
if (!ObjMU)
|
|
|
|
return ObjMU.takeError();
|
2018-08-18 05:18:18 +08:00
|
|
|
return JD.define(std::move(*ObjMU));
|
2018-05-22 05:11:13 +08:00
|
|
|
}
|
|
|
|
|
2018-08-02 06:42:23 +08:00
|
|
|
Expected<std::unique_ptr<BasicObjectLayerMaterializationUnit>>
|
|
|
|
BasicObjectLayerMaterializationUnit::Create(ObjectLayer &L, VModuleKey K,
|
|
|
|
std::unique_ptr<MemoryBuffer> O) {
|
2018-08-06 06:35:37 +08:00
|
|
|
auto SymbolFlags =
|
|
|
|
getObjectSymbolFlags(L.getExecutionSession(), O->getMemBufferRef());
|
2018-08-02 06:42:23 +08:00
|
|
|
|
2018-08-06 06:35:37 +08:00
|
|
|
if (!SymbolFlags)
|
|
|
|
return SymbolFlags.takeError();
|
2018-08-02 06:42:23 +08:00
|
|
|
|
|
|
|
return std::unique_ptr<BasicObjectLayerMaterializationUnit>(
|
2018-08-06 06:35:37 +08:00
|
|
|
new BasicObjectLayerMaterializationUnit(L, K, std::move(O),
|
|
|
|
std::move(*SymbolFlags)));
|
2018-05-22 05:11:13 +08:00
|
|
|
}
|
|
|
|
|
2018-08-02 06:42:23 +08:00
|
|
|
BasicObjectLayerMaterializationUnit::BasicObjectLayerMaterializationUnit(
|
2018-08-06 06:35:37 +08:00
|
|
|
ObjectLayer &L, VModuleKey K, std::unique_ptr<MemoryBuffer> O,
|
|
|
|
SymbolFlagsMap SymbolFlags)
|
2018-10-17 04:13:06 +08:00
|
|
|
: MaterializationUnit(std::move(SymbolFlags), std::move(K)), L(L),
|
2018-08-02 06:42:23 +08:00
|
|
|
O(std::move(O)) {}
|
|
|
|
|
2018-09-28 23:03:11 +08:00
|
|
|
StringRef BasicObjectLayerMaterializationUnit::getName() const {
|
|
|
|
if (O)
|
|
|
|
return O->getBufferIdentifier();
|
|
|
|
return "<null object>";
|
|
|
|
}
|
|
|
|
|
2018-05-22 05:11:13 +08:00
|
|
|
void BasicObjectLayerMaterializationUnit::materialize(
|
|
|
|
MaterializationResponsibility R) {
|
2018-10-17 04:13:06 +08:00
|
|
|
L.emit(std::move(R), std::move(O));
|
2018-05-22 05:11:13 +08:00
|
|
|
}
|
|
|
|
|
2018-08-18 05:18:18 +08:00
|
|
|
void BasicObjectLayerMaterializationUnit::discard(const JITDylib &JD,
|
2018-10-07 07:02:06 +08:00
|
|
|
const SymbolStringPtr &Name) {
|
2018-05-22 05:11:13 +08:00
|
|
|
// FIXME: Support object file level discard. This could be done by building a
|
|
|
|
// filter to pass to the object layer along with the object itself.
|
|
|
|
}
|
|
|
|
|
2018-08-06 06:35:37 +08:00
|
|
|
Expected<SymbolFlagsMap> getObjectSymbolFlags(ExecutionSession &ES,
|
|
|
|
MemoryBufferRef ObjBuffer) {
|
|
|
|
auto Obj = object::ObjectFile::createObjectFile(ObjBuffer);
|
|
|
|
|
|
|
|
if (!Obj)
|
|
|
|
return Obj.takeError();
|
|
|
|
|
|
|
|
SymbolFlagsMap SymbolFlags;
|
|
|
|
for (auto &Sym : (*Obj)->symbols()) {
|
2018-08-27 00:46:02 +08:00
|
|
|
// Skip symbols not defined in this object file.
|
|
|
|
if (Sym.getFlags() & object::BasicSymbolRef::SF_Undefined)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Skip symbols that are not global.
|
|
|
|
if (!(Sym.getFlags() & object::BasicSymbolRef::SF_Global))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
auto Name = Sym.getName();
|
|
|
|
if (!Name)
|
|
|
|
return Name.takeError();
|
2018-10-01 07:18:24 +08:00
|
|
|
auto InternedName = ES.intern(*Name);
|
2018-08-27 00:46:02 +08:00
|
|
|
auto SymFlags = JITSymbolFlags::fromObjectSymbol(Sym);
|
|
|
|
if (!SymFlags)
|
|
|
|
return SymFlags.takeError();
|
|
|
|
SymbolFlags[InternedName] = std::move(*SymFlags);
|
2018-08-06 06:35:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return SymbolFlags;
|
|
|
|
}
|
|
|
|
|
2018-05-22 05:11:13 +08:00
|
|
|
} // End namespace orc.
|
|
|
|
} // End namespace llvm.
|