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