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"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
namespace orc {
|
|
|
|
|
|
|
|
IRLayer::IRLayer(ExecutionSession &ES) : ES(ES) {}
|
|
|
|
IRLayer::~IRLayer() {}
|
|
|
|
|
|
|
|
Error IRLayer::add(VSO &V, VModuleKey K, std::unique_ptr<Module> M) {
|
|
|
|
return V.define(llvm::make_unique<BasicIRLayerMaterializationUnit>(
|
|
|
|
*this, std::move(K), std::move(M)));
|
|
|
|
}
|
|
|
|
|
2018-05-23 00:15:38 +08:00
|
|
|
IRMaterializationUnit::IRMaterializationUnit(ExecutionSession &ES,
|
|
|
|
std::unique_ptr<Module> M)
|
|
|
|
: MaterializationUnit(SymbolFlagsMap()), M(std::move(M)) {
|
2018-05-22 05:11:13 +08:00
|
|
|
|
|
|
|
MangleAndInterner Mangle(ES, this->M->getDataLayout());
|
|
|
|
for (auto &G : this->M->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(
|
|
|
|
std::unique_ptr<Module> M, SymbolFlagsMap SymbolFlags,
|
|
|
|
SymbolNameToDefinitionMap SymbolToDefinition)
|
|
|
|
: MaterializationUnit(std::move(SymbolFlags)), M(std::move(M)),
|
|
|
|
SymbolToDefinition(std::move(SymbolToDefinition)) {}
|
|
|
|
|
2018-05-23 00:15:38 +08:00
|
|
|
void IRMaterializationUnit::discard(const VSO &V, SymbolStringPtr Name) {
|
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(
|
|
|
|
IRLayer &L, VModuleKey K, std::unique_ptr<Module> M)
|
|
|
|
: IRMaterializationUnit(L.getExecutionSession(), std::move(M)),
|
|
|
|
L(L), K(std::move(K)) {}
|
|
|
|
|
|
|
|
void BasicIRLayerMaterializationUnit::materialize(
|
|
|
|
MaterializationResponsibility R) {
|
|
|
|
L.emit(std::move(R), std::move(K), std::move(M));
|
|
|
|
}
|
|
|
|
|
2018-05-22 05:11:13 +08:00
|
|
|
ObjectLayer::ObjectLayer(ExecutionSession &ES) : ES(ES) {}
|
|
|
|
|
|
|
|
ObjectLayer::~ObjectLayer() {}
|
|
|
|
|
|
|
|
Error ObjectLayer::add(VSO &V, VModuleKey K, std::unique_ptr<MemoryBuffer> O) {
|
|
|
|
return V.define(llvm::make_unique<BasicObjectLayerMaterializationUnit>(
|
|
|
|
*this, std::move(K), std::move(O)));
|
|
|
|
}
|
|
|
|
|
|
|
|
BasicObjectLayerMaterializationUnit::BasicObjectLayerMaterializationUnit(
|
|
|
|
ObjectLayer &L, VModuleKey K, std::unique_ptr<MemoryBuffer> O)
|
|
|
|
: MaterializationUnit(SymbolFlagsMap()), L(L), K(std::move(K)),
|
|
|
|
O(std::move(O)) {
|
|
|
|
|
|
|
|
auto &ES = L.getExecutionSession();
|
|
|
|
auto Obj = cantFail(
|
|
|
|
object::ObjectFile::createObjectFile(this->O->getMemBufferRef()));
|
|
|
|
|
|
|
|
for (auto &Sym : Obj->symbols()) {
|
|
|
|
if (!(Sym.getFlags() & object::BasicSymbolRef::SF_Undefined) &&
|
|
|
|
(Sym.getFlags() & object::BasicSymbolRef::SF_Exported)) {
|
|
|
|
auto InternedName =
|
|
|
|
ES.getSymbolStringPool().intern(cantFail(Sym.getName()));
|
|
|
|
SymbolFlags[InternedName] = JITSymbolFlags::fromObjectSymbol(Sym);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BasicObjectLayerMaterializationUnit::materialize(
|
|
|
|
MaterializationResponsibility R) {
|
|
|
|
L.emit(std::move(R), std::move(K), std::move(O));
|
|
|
|
}
|
|
|
|
|
|
|
|
void BasicObjectLayerMaterializationUnit::discard(const VSO &V,
|
|
|
|
SymbolStringPtr Name) {
|
|
|
|
// 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.
|
|
|
|
}
|
|
|
|
|
|
|
|
} // End namespace orc.
|
|
|
|
} // End namespace llvm.
|