1 //===-------------------- Layer.cpp - Layer interfaces --------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "llvm/ExecutionEngine/Orc/Layer.h" 10 11 #include "llvm/ExecutionEngine/Orc/DebugUtils.h" 12 #include "llvm/ExecutionEngine/Orc/ExecutionUtils.h" 13 #include "llvm/IR/Constants.h" 14 #include "llvm/Object/MachO.h" 15 #include "llvm/Object/ObjectFile.h" 16 #include "llvm/Support/Debug.h" 17 18 #define DEBUG_TYPE "orc" 19 20 namespace llvm { 21 namespace orc { 22 23 IRLayer::~IRLayer() {} 24 25 Error IRLayer::add(JITDylib &JD, ThreadSafeModule TSM, VModuleKey K) { 26 return JD.define(std::make_unique<BasicIRLayerMaterializationUnit>( 27 *this, *getManglingOptions(), std::move(TSM), std::move(K))); 28 } 29 30 IRMaterializationUnit::IRMaterializationUnit( 31 ExecutionSession &ES, const IRSymbolMapper::ManglingOptions &MO, 32 ThreadSafeModule TSM, VModuleKey K) 33 : MaterializationUnit(SymbolFlagsMap(), nullptr, std::move(K)), 34 TSM(std::move(TSM)) { 35 36 assert(this->TSM && "Module must not be null"); 37 38 MangleAndInterner Mangle(ES, this->TSM.getModuleUnlocked()->getDataLayout()); 39 this->TSM.withModuleDo([&](Module &M) { 40 for (auto &G : M.global_values()) { 41 // Skip globals that don't generate symbols. 42 43 if (!G.hasName() || G.isDeclaration() || G.hasLocalLinkage() || 44 G.hasAvailableExternallyLinkage() || G.hasAppendingLinkage()) 45 continue; 46 47 // thread locals generate different symbols depending on whether or not 48 // emulated TLS is enabled. 49 if (G.isThreadLocal() && MO.EmulatedTLS) { 50 auto &GV = cast<GlobalVariable>(G); 51 52 auto Flags = JITSymbolFlags::fromGlobalValue(GV); 53 54 auto EmuTLSV = Mangle(("__emutls_v." + GV.getName()).str()); 55 SymbolFlags[EmuTLSV] = Flags; 56 SymbolToDefinition[EmuTLSV] = &GV; 57 58 // If this GV has a non-zero initializer we'll need to emit an 59 // __emutls.t symbol too. 60 if (GV.hasInitializer()) { 61 const auto *InitVal = GV.getInitializer(); 62 63 // Skip zero-initializers. 64 if (isa<ConstantAggregateZero>(InitVal)) 65 continue; 66 const auto *InitIntValue = dyn_cast<ConstantInt>(InitVal); 67 if (InitIntValue && InitIntValue->isZero()) 68 continue; 69 70 auto EmuTLST = Mangle(("__emutls_t." + GV.getName()).str()); 71 SymbolFlags[EmuTLST] = Flags; 72 } 73 continue; 74 } 75 76 // Otherwise we just need a normal linker mangling. 77 auto MangledName = Mangle(G.getName()); 78 SymbolFlags[MangledName] = JITSymbolFlags::fromGlobalValue(G); 79 SymbolToDefinition[MangledName] = &G; 80 } 81 82 // If we need an init symbol for this module then create one. 83 if (!llvm::empty(getStaticInitGVs(M))) { 84 size_t Counter = 0; 85 86 do { 87 std::string InitSymbolName; 88 raw_string_ostream(InitSymbolName) 89 << "$." << M.getModuleIdentifier() << ".__inits." << Counter++; 90 InitSymbol = ES.intern(InitSymbolName); 91 } while (SymbolFlags.count(InitSymbol)); 92 93 SymbolFlags[InitSymbol] = JITSymbolFlags::MaterializationSideEffectsOnly; 94 } 95 }); 96 } 97 98 IRMaterializationUnit::IRMaterializationUnit( 99 ThreadSafeModule TSM, VModuleKey K, SymbolFlagsMap SymbolFlags, 100 SymbolStringPtr InitSymbol, SymbolNameToDefinitionMap SymbolToDefinition) 101 : MaterializationUnit(std::move(SymbolFlags), std::move(InitSymbol), 102 std::move(K)), 103 TSM(std::move(TSM)), SymbolToDefinition(std::move(SymbolToDefinition)) {} 104 105 StringRef IRMaterializationUnit::getName() const { 106 if (TSM) 107 return TSM.withModuleDo( 108 [](const Module &M) -> StringRef { return M.getModuleIdentifier(); }); 109 return "<null module>"; 110 } 111 112 void IRMaterializationUnit::discard(const JITDylib &JD, 113 const SymbolStringPtr &Name) { 114 LLVM_DEBUG(JD.getExecutionSession().runSessionLocked([&]() { 115 dbgs() << "In " << JD.getName() << " discarding " << *Name << " from MU@" 116 << this << " (" << getName() << ")\n"; 117 });); 118 119 auto I = SymbolToDefinition.find(Name); 120 assert(I != SymbolToDefinition.end() && 121 "Symbol not provided by this MU, or previously discarded"); 122 assert(!I->second->isDeclaration() && 123 "Discard should only apply to definitions"); 124 I->second->setLinkage(GlobalValue::AvailableExternallyLinkage); 125 SymbolToDefinition.erase(I); 126 } 127 128 BasicIRLayerMaterializationUnit::BasicIRLayerMaterializationUnit( 129 IRLayer &L, const IRSymbolMapper::ManglingOptions &MO, ThreadSafeModule TSM, 130 VModuleKey K) 131 : IRMaterializationUnit(L.getExecutionSession(), MO, std::move(TSM), 132 std::move(K)), 133 L(L), K(std::move(K)) {} 134 135 void BasicIRLayerMaterializationUnit::materialize( 136 std::unique_ptr<MaterializationResponsibility> R) { 137 138 // Throw away the SymbolToDefinition map: it's not usable after we hand 139 // off the module. 140 SymbolToDefinition.clear(); 141 142 // If cloneToNewContextOnEmit is set, clone the module now. 143 if (L.getCloneToNewContextOnEmit()) 144 TSM = cloneToNewContext(TSM); 145 146 #ifndef NDEBUG 147 auto &ES = R->getTargetJITDylib().getExecutionSession(); 148 auto &N = R->getTargetJITDylib().getName(); 149 #endif // NDEBUG 150 151 LLVM_DEBUG(ES.runSessionLocked( 152 [&]() { dbgs() << "Emitting, for " << N << ", " << *this << "\n"; });); 153 L.emit(std::move(R), std::move(TSM)); 154 LLVM_DEBUG(ES.runSessionLocked([&]() { 155 dbgs() << "Finished emitting, for " << N << ", " << *this << "\n"; 156 });); 157 } 158 159 ObjectLayer::ObjectLayer(ExecutionSession &ES) : ES(ES) {} 160 161 ObjectLayer::~ObjectLayer() {} 162 163 Error ObjectLayer::add(JITDylib &JD, std::unique_ptr<MemoryBuffer> O, 164 VModuleKey K) { 165 auto ObjMU = BasicObjectLayerMaterializationUnit::Create(*this, std::move(K), 166 std::move(O)); 167 if (!ObjMU) 168 return ObjMU.takeError(); 169 return JD.define(std::move(*ObjMU)); 170 } 171 172 Expected<std::unique_ptr<BasicObjectLayerMaterializationUnit>> 173 BasicObjectLayerMaterializationUnit::Create(ObjectLayer &L, VModuleKey K, 174 std::unique_ptr<MemoryBuffer> O) { 175 auto ObjSymInfo = 176 getObjectSymbolInfo(L.getExecutionSession(), O->getMemBufferRef()); 177 178 if (!ObjSymInfo) 179 return ObjSymInfo.takeError(); 180 181 auto &SymbolFlags = ObjSymInfo->first; 182 auto &InitSymbol = ObjSymInfo->second; 183 184 return std::unique_ptr<BasicObjectLayerMaterializationUnit>( 185 new BasicObjectLayerMaterializationUnit( 186 L, K, std::move(O), std::move(SymbolFlags), std::move(InitSymbol))); 187 } 188 189 BasicObjectLayerMaterializationUnit::BasicObjectLayerMaterializationUnit( 190 ObjectLayer &L, VModuleKey K, std::unique_ptr<MemoryBuffer> O, 191 SymbolFlagsMap SymbolFlags, SymbolStringPtr InitSymbol) 192 : MaterializationUnit(std::move(SymbolFlags), std::move(InitSymbol), 193 std::move(K)), 194 L(L), O(std::move(O)) {} 195 196 StringRef BasicObjectLayerMaterializationUnit::getName() const { 197 if (O) 198 return O->getBufferIdentifier(); 199 return "<null object>"; 200 } 201 202 void BasicObjectLayerMaterializationUnit::materialize( 203 std::unique_ptr<MaterializationResponsibility> R) { 204 L.emit(std::move(R), std::move(O)); 205 } 206 207 void BasicObjectLayerMaterializationUnit::discard(const JITDylib &JD, 208 const SymbolStringPtr &Name) { 209 // This is a no-op for object files: Having removed 'Name' from SymbolFlags 210 // the symbol will be dead-stripped by the JIT linker. 211 } 212 213 } // End namespace orc. 214 } // End namespace llvm. 215