1 //===-------------------- Layer.cpp - Layer interfaces --------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "llvm/ExecutionEngine/Orc/Layer.h"
11 #include "llvm/Object/ObjectFile.h"
12 #include "llvm/Support/MemoryBuffer.h"
13
14 namespace llvm {
15 namespace orc {
16
IRLayer(ExecutionSession & ES)17 IRLayer::IRLayer(ExecutionSession &ES) : ES(ES) {}
~IRLayer()18 IRLayer::~IRLayer() {}
19
add(VSO & V,VModuleKey K,std::unique_ptr<Module> M)20 Error IRLayer::add(VSO &V, VModuleKey K, std::unique_ptr<Module> M) {
21 return V.define(llvm::make_unique<BasicIRLayerMaterializationUnit>(
22 *this, std::move(K), std::move(M)));
23 }
24
IRMaterializationUnit(ExecutionSession & ES,std::unique_ptr<Module> M)25 IRMaterializationUnit::IRMaterializationUnit(ExecutionSession &ES,
26 std::unique_ptr<Module> M)
27 : MaterializationUnit(SymbolFlagsMap()), M(std::move(M)) {
28
29 MangleAndInterner Mangle(ES, this->M->getDataLayout());
30 for (auto &G : this->M->global_values()) {
31 if (G.hasName() && !G.isDeclaration() && !G.hasLocalLinkage() &&
32 !G.hasAvailableExternallyLinkage() && !G.hasAppendingLinkage()) {
33 auto MangledName = Mangle(G.getName());
34 SymbolFlags[MangledName] = JITSymbolFlags::fromGlobalValue(G);
35 SymbolToDefinition[MangledName] = &G;
36 }
37 }
38 }
39
IRMaterializationUnit(std::unique_ptr<Module> M,SymbolFlagsMap SymbolFlags,SymbolNameToDefinitionMap SymbolToDefinition)40 IRMaterializationUnit::IRMaterializationUnit(
41 std::unique_ptr<Module> M, SymbolFlagsMap SymbolFlags,
42 SymbolNameToDefinitionMap SymbolToDefinition)
43 : MaterializationUnit(std::move(SymbolFlags)), M(std::move(M)),
44 SymbolToDefinition(std::move(SymbolToDefinition)) {}
45
discard(const VSO & V,SymbolStringPtr Name)46 void IRMaterializationUnit::discard(const VSO &V, SymbolStringPtr Name) {
47 auto I = SymbolToDefinition.find(Name);
48 assert(I != SymbolToDefinition.end() &&
49 "Symbol not provided by this MU, or previously discarded");
50 assert(!I->second->isDeclaration() &&
51 "Discard should only apply to definitions");
52 I->second->setLinkage(GlobalValue::AvailableExternallyLinkage);
53 SymbolToDefinition.erase(I);
54 }
55
BasicIRLayerMaterializationUnit(IRLayer & L,VModuleKey K,std::unique_ptr<Module> M)56 BasicIRLayerMaterializationUnit::BasicIRLayerMaterializationUnit(
57 IRLayer &L, VModuleKey K, std::unique_ptr<Module> M)
58 : IRMaterializationUnit(L.getExecutionSession(), std::move(M)),
59 L(L), K(std::move(K)) {}
60
materialize(MaterializationResponsibility R)61 void BasicIRLayerMaterializationUnit::materialize(
62 MaterializationResponsibility R) {
63 L.emit(std::move(R), std::move(K), std::move(M));
64 }
65
ObjectLayer(ExecutionSession & ES)66 ObjectLayer::ObjectLayer(ExecutionSession &ES) : ES(ES) {}
67
~ObjectLayer()68 ObjectLayer::~ObjectLayer() {}
69
add(VSO & V,VModuleKey K,std::unique_ptr<MemoryBuffer> O)70 Error ObjectLayer::add(VSO &V, VModuleKey K, std::unique_ptr<MemoryBuffer> O) {
71 return V.define(llvm::make_unique<BasicObjectLayerMaterializationUnit>(
72 *this, std::move(K), std::move(O)));
73 }
74
BasicObjectLayerMaterializationUnit(ObjectLayer & L,VModuleKey K,std::unique_ptr<MemoryBuffer> O)75 BasicObjectLayerMaterializationUnit::BasicObjectLayerMaterializationUnit(
76 ObjectLayer &L, VModuleKey K, std::unique_ptr<MemoryBuffer> O)
77 : MaterializationUnit(SymbolFlagsMap()), L(L), K(std::move(K)),
78 O(std::move(O)) {
79
80 auto &ES = L.getExecutionSession();
81 auto Obj = cantFail(
82 object::ObjectFile::createObjectFile(this->O->getMemBufferRef()));
83
84 for (auto &Sym : Obj->symbols()) {
85 if (!(Sym.getFlags() & object::BasicSymbolRef::SF_Undefined) &&
86 (Sym.getFlags() & object::BasicSymbolRef::SF_Exported)) {
87 auto InternedName =
88 ES.getSymbolStringPool().intern(cantFail(Sym.getName()));
89 SymbolFlags[InternedName] = JITSymbolFlags::fromObjectSymbol(Sym);
90 }
91 }
92 }
93
materialize(MaterializationResponsibility R)94 void BasicObjectLayerMaterializationUnit::materialize(
95 MaterializationResponsibility R) {
96 L.emit(std::move(R), std::move(K), std::move(O));
97 }
98
discard(const VSO & V,SymbolStringPtr Name)99 void BasicObjectLayerMaterializationUnit::discard(const VSO &V,
100 SymbolStringPtr Name) {
101 // FIXME: Support object file level discard. This could be done by building a
102 // filter to pass to the object layer along with the object itself.
103 }
104
105 } // End namespace orc.
106 } // End namespace llvm.
107