1 //===----- CompileOnDemandLayer.cpp - Lazily emit IR on first call --------===// 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/CompileOnDemandLayer.h" 10 11 #include "llvm/ADT/Hashing.h" 12 #include "llvm/ExecutionEngine/Orc/ExecutionUtils.h" 13 #include "llvm/IR/Mangler.h" 14 #include "llvm/IR/Module.h" 15 #include "llvm/Support/FormatVariadic.h" 16 17 using namespace llvm; 18 using namespace llvm::orc; 19 20 static ThreadSafeModule extractSubModule(ThreadSafeModule &TSM, 21 StringRef Suffix, 22 GVPredicate ShouldExtract) { 23 24 auto DeleteExtractedDefs = [](GlobalValue &GV) { 25 // Bump the linkage: this global will be provided by the external module. 26 GV.setLinkage(GlobalValue::ExternalLinkage); 27 28 // Delete the definition in the source module. 29 if (isa<Function>(GV)) { 30 auto &F = cast<Function>(GV); 31 F.deleteBody(); 32 F.setPersonalityFn(nullptr); 33 } else if (isa<GlobalVariable>(GV)) { 34 cast<GlobalVariable>(GV).setInitializer(nullptr); 35 } else if (isa<GlobalAlias>(GV)) { 36 // We need to turn deleted aliases into function or variable decls based 37 // on the type of their aliasee. 38 auto &A = cast<GlobalAlias>(GV); 39 Constant *Aliasee = A.getAliasee(); 40 assert(A.hasName() && "Anonymous alias?"); 41 assert(Aliasee->hasName() && "Anonymous aliasee"); 42 std::string AliasName = std::string(A.getName()); 43 44 if (isa<Function>(Aliasee)) { 45 auto *F = cloneFunctionDecl(*A.getParent(), *cast<Function>(Aliasee)); 46 A.replaceAllUsesWith(F); 47 A.eraseFromParent(); 48 F->setName(AliasName); 49 } else if (isa<GlobalVariable>(Aliasee)) { 50 auto *G = cloneGlobalVariableDecl(*A.getParent(), 51 *cast<GlobalVariable>(Aliasee)); 52 A.replaceAllUsesWith(G); 53 A.eraseFromParent(); 54 G->setName(AliasName); 55 } else 56 llvm_unreachable("Alias to unsupported type"); 57 } else 58 llvm_unreachable("Unsupported global type"); 59 }; 60 61 auto NewTSM = cloneToNewContext(TSM, ShouldExtract, DeleteExtractedDefs); 62 NewTSM.withModuleDo([&](Module &M) { 63 M.setModuleIdentifier((M.getModuleIdentifier() + Suffix).str()); 64 }); 65 66 return NewTSM; 67 } 68 69 namespace llvm { 70 namespace orc { 71 72 class PartitioningIRMaterializationUnit : public IRMaterializationUnit { 73 public: 74 PartitioningIRMaterializationUnit(ExecutionSession &ES, 75 const IRSymbolMapper::ManglingOptions &MO, 76 ThreadSafeModule TSM, VModuleKey K, 77 CompileOnDemandLayer &Parent) 78 : IRMaterializationUnit(ES, MO, std::move(TSM), std::move(K)), 79 Parent(Parent) {} 80 81 PartitioningIRMaterializationUnit( 82 ThreadSafeModule TSM, VModuleKey K, SymbolFlagsMap SymbolFlags, 83 SymbolStringPtr InitSymbol, SymbolNameToDefinitionMap SymbolToDefinition, 84 CompileOnDemandLayer &Parent) 85 : IRMaterializationUnit(std::move(TSM), std::move(K), 86 std::move(SymbolFlags), std::move(InitSymbol), 87 std::move(SymbolToDefinition)), 88 Parent(Parent) {} 89 90 private: 91 void materialize(MaterializationResponsibility R) override { 92 Parent.emitPartition(std::move(R), std::move(TSM), 93 std::move(SymbolToDefinition)); 94 } 95 96 void discard(const JITDylib &V, const SymbolStringPtr &Name) override { 97 // All original symbols were materialized by the CODLayer and should be 98 // final. The function bodies provided by M should never be overridden. 99 llvm_unreachable("Discard should never be called on an " 100 "ExtractingIRMaterializationUnit"); 101 } 102 103 mutable std::mutex SourceModuleMutex; 104 CompileOnDemandLayer &Parent; 105 }; 106 107 Optional<CompileOnDemandLayer::GlobalValueSet> 108 CompileOnDemandLayer::compileRequested(GlobalValueSet Requested) { 109 return std::move(Requested); 110 } 111 112 Optional<CompileOnDemandLayer::GlobalValueSet> 113 CompileOnDemandLayer::compileWholeModule(GlobalValueSet Requested) { 114 return None; 115 } 116 117 CompileOnDemandLayer::CompileOnDemandLayer( 118 ExecutionSession &ES, IRLayer &BaseLayer, LazyCallThroughManager &LCTMgr, 119 IndirectStubsManagerBuilder BuildIndirectStubsManager) 120 : IRLayer(ES, BaseLayer.getManglingOptions()), BaseLayer(BaseLayer), 121 LCTMgr(LCTMgr), 122 BuildIndirectStubsManager(std::move(BuildIndirectStubsManager)) {} 123 124 void CompileOnDemandLayer::setPartitionFunction(PartitionFunction Partition) { 125 this->Partition = std::move(Partition); 126 } 127 128 void CompileOnDemandLayer::setImplMap(ImplSymbolMap *Imp) { 129 this->AliaseeImpls = Imp; 130 } 131 void CompileOnDemandLayer::emit(MaterializationResponsibility R, 132 ThreadSafeModule TSM) { 133 assert(TSM && "Null module"); 134 135 auto &ES = getExecutionSession(); 136 137 // Sort the callables and non-callables, build re-exports and lodge the 138 // actual module with the implementation dylib. 139 auto &PDR = getPerDylibResources(R.getTargetJITDylib()); 140 141 SymbolAliasMap NonCallables; 142 SymbolAliasMap Callables; 143 TSM.withModuleDo([&](Module &M) { 144 // First, do some cleanup on the module: 145 cleanUpModule(M); 146 }); 147 148 for (auto &KV : R.getSymbols()) { 149 auto &Name = KV.first; 150 auto &Flags = KV.second; 151 if (Flags.isCallable()) 152 Callables[Name] = SymbolAliasMapEntry(Name, Flags); 153 else 154 NonCallables[Name] = SymbolAliasMapEntry(Name, Flags); 155 } 156 157 // Create a partitioning materialization unit and lodge it with the 158 // implementation dylib. 159 if (auto Err = PDR.getImplDylib().define( 160 std::make_unique<PartitioningIRMaterializationUnit>( 161 ES, *getManglingOptions(), std::move(TSM), R.getVModuleKey(), 162 *this))) { 163 ES.reportError(std::move(Err)); 164 R.failMaterialization(); 165 return; 166 } 167 168 R.replace(reexports(PDR.getImplDylib(), std::move(NonCallables), 169 JITDylibLookupFlags::MatchAllSymbols)); 170 R.replace(lazyReexports(LCTMgr, PDR.getISManager(), PDR.getImplDylib(), 171 std::move(Callables), AliaseeImpls)); 172 } 173 174 CompileOnDemandLayer::PerDylibResources & 175 CompileOnDemandLayer::getPerDylibResources(JITDylib &TargetD) { 176 auto I = DylibResources.find(&TargetD); 177 if (I == DylibResources.end()) { 178 auto &ImplD = 179 getExecutionSession().createBareJITDylib(TargetD.getName() + ".impl"); 180 JITDylibSearchOrder NewSearchOrder; 181 TargetD.withSearchOrderDo( 182 [&](const JITDylibSearchOrder &TargetSearchOrder) { 183 NewSearchOrder = TargetSearchOrder; 184 }); 185 186 assert( 187 !NewSearchOrder.empty() && NewSearchOrder.front().first == &TargetD && 188 NewSearchOrder.front().second == JITDylibLookupFlags::MatchAllSymbols && 189 "TargetD must be at the front of its own search order and match " 190 "non-exported symbol"); 191 NewSearchOrder.insert(std::next(NewSearchOrder.begin()), 192 {&ImplD, JITDylibLookupFlags::MatchAllSymbols}); 193 ImplD.setSearchOrder(NewSearchOrder, false); 194 TargetD.setSearchOrder(std::move(NewSearchOrder), false); 195 196 PerDylibResources PDR(ImplD, BuildIndirectStubsManager()); 197 I = DylibResources.insert(std::make_pair(&TargetD, std::move(PDR))).first; 198 } 199 200 return I->second; 201 } 202 203 void CompileOnDemandLayer::cleanUpModule(Module &M) { 204 for (auto &F : M.functions()) { 205 if (F.isDeclaration()) 206 continue; 207 208 if (F.hasAvailableExternallyLinkage()) { 209 F.deleteBody(); 210 F.setPersonalityFn(nullptr); 211 continue; 212 } 213 } 214 } 215 216 void CompileOnDemandLayer::expandPartition(GlobalValueSet &Partition) { 217 // Expands the partition to ensure the following rules hold: 218 // (1) If any alias is in the partition, its aliasee is also in the partition. 219 // (2) If any aliasee is in the partition, its aliases are also in the 220 // partiton. 221 // (3) If any global variable is in the partition then all global variables 222 // are in the partition. 223 assert(!Partition.empty() && "Unexpected empty partition"); 224 225 const Module &M = *(*Partition.begin())->getParent(); 226 bool ContainsGlobalVariables = false; 227 std::vector<const GlobalValue *> GVsToAdd; 228 229 for (auto *GV : Partition) 230 if (isa<GlobalAlias>(GV)) 231 GVsToAdd.push_back( 232 cast<GlobalValue>(cast<GlobalAlias>(GV)->getAliasee())); 233 else if (isa<GlobalVariable>(GV)) 234 ContainsGlobalVariables = true; 235 236 for (auto &A : M.aliases()) 237 if (Partition.count(cast<GlobalValue>(A.getAliasee()))) 238 GVsToAdd.push_back(&A); 239 240 if (ContainsGlobalVariables) 241 for (auto &G : M.globals()) 242 GVsToAdd.push_back(&G); 243 244 for (auto *GV : GVsToAdd) 245 Partition.insert(GV); 246 } 247 248 void CompileOnDemandLayer::emitPartition( 249 MaterializationResponsibility R, ThreadSafeModule TSM, 250 IRMaterializationUnit::SymbolNameToDefinitionMap Defs) { 251 252 // FIXME: Need a 'notify lazy-extracting/emitting' callback to tie the 253 // extracted module key, extracted module, and source module key 254 // together. This could be used, for example, to provide a specific 255 // memory manager instance to the linking layer. 256 257 auto &ES = getExecutionSession(); 258 GlobalValueSet RequestedGVs; 259 for (auto &Name : R.getRequestedSymbols()) { 260 if (Name == R.getInitializerSymbol()) 261 TSM.withModuleDo([&](Module &M) { 262 for (auto &GV : getStaticInitGVs(M)) 263 RequestedGVs.insert(&GV); 264 }); 265 else { 266 assert(Defs.count(Name) && "No definition for symbol"); 267 RequestedGVs.insert(Defs[Name]); 268 } 269 } 270 271 /// Perform partitioning with the context lock held, since the partition 272 /// function is allowed to access the globals to compute the partition. 273 auto GVsToExtract = 274 TSM.withModuleDo([&](Module &M) { return Partition(RequestedGVs); }); 275 276 // Take a 'None' partition to mean the whole module (as opposed to an empty 277 // partition, which means "materialize nothing"). Emit the whole module 278 // unmodified to the base layer. 279 if (GVsToExtract == None) { 280 Defs.clear(); 281 BaseLayer.emit(std::move(R), std::move(TSM)); 282 return; 283 } 284 285 // If the partition is empty, return the whole module to the symbol table. 286 if (GVsToExtract->empty()) { 287 R.replace(std::make_unique<PartitioningIRMaterializationUnit>( 288 std::move(TSM), R.getVModuleKey(), R.getSymbols(), 289 R.getInitializerSymbol(), std::move(Defs), *this)); 290 return; 291 } 292 293 // Ok -- we actually need to partition the symbols. Promote the symbol 294 // linkages/names, expand the partition to include any required symbols 295 // (i.e. symbols that can't be separated from our partition), and 296 // then extract the partition. 297 // 298 // FIXME: We apply this promotion once per partitioning. It's safe, but 299 // overkill. 300 auto ExtractedTSM = 301 TSM.withModuleDo([&](Module &M) -> Expected<ThreadSafeModule> { 302 auto PromotedGlobals = PromoteSymbols(M); 303 if (!PromotedGlobals.empty()) { 304 305 MangleAndInterner Mangle(ES, M.getDataLayout()); 306 SymbolFlagsMap SymbolFlags; 307 IRSymbolMapper::add(ES, *getManglingOptions(), 308 PromotedGlobals, SymbolFlags); 309 310 if (auto Err = R.defineMaterializing(SymbolFlags)) 311 return std::move(Err); 312 } 313 314 expandPartition(*GVsToExtract); 315 316 // Submodule name is given by hashing the names of the globals. 317 std::string SubModuleName; 318 { 319 std::vector<const GlobalValue*> HashGVs; 320 HashGVs.reserve(GVsToExtract->size()); 321 for (auto *GV : *GVsToExtract) 322 HashGVs.push_back(GV); 323 llvm::sort(HashGVs, [](const GlobalValue *LHS, const GlobalValue *RHS) { 324 return LHS->getName() < RHS->getName(); 325 }); 326 hash_code HC(0); 327 for (auto *GV : HashGVs) { 328 assert(GV->hasName() && "All GVs to extract should be named by now"); 329 auto GVName = GV->getName(); 330 HC = hash_combine(HC, hash_combine_range(GVName.begin(), GVName.end())); 331 } 332 raw_string_ostream(SubModuleName) 333 << ".submodule." 334 << formatv(sizeof(size_t) == 8 ? "{0:x16}" : "{0:x8}", 335 static_cast<size_t>(HC)) 336 << ".ll"; 337 } 338 339 // Extract the requested partiton (plus any necessary aliases) and 340 // put the rest back into the impl dylib. 341 auto ShouldExtract = [&](const GlobalValue &GV) -> bool { 342 return GVsToExtract->count(&GV); 343 }; 344 345 return extractSubModule(TSM, SubModuleName , ShouldExtract); 346 }); 347 348 if (!ExtractedTSM) { 349 ES.reportError(ExtractedTSM.takeError()); 350 R.failMaterialization(); 351 return; 352 } 353 354 R.replace(std::make_unique<PartitioningIRMaterializationUnit>( 355 ES, *getManglingOptions(), std::move(TSM), R.getVModuleKey(), *this)); 356 BaseLayer.emit(std::move(R), std::move(*ExtractedTSM)); 357 } 358 359 } // end namespace orc 360 } // end namespace llvm 361