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 17 IRLayer::IRLayer(ExecutionSession &ES) : ES(ES) {} 18 IRLayer::~IRLayer() {} 19 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 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 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 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 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 61 void BasicIRLayerMaterializationUnit::materialize( 62 MaterializationResponsibility R) { 63 L.emit(std::move(R), std::move(K), std::move(M)); 64 } 65 66 ObjectLayer::ObjectLayer(ExecutionSession &ES) : ES(ES) {} 67 68 ObjectLayer::~ObjectLayer() {} 69 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 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 94 void BasicObjectLayerMaterializationUnit::materialize( 95 MaterializationResponsibility R) { 96 L.emit(std::move(R), std::move(K), std::move(O)); 97 } 98 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