1 //===- ThinLTOBitcodeWriter.cpp - Bitcode writing pass for ThinLTO --------===// 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/Transforms/IPO/ThinLTOBitcodeWriter.h" 11 #include "llvm/Analysis/BasicAliasAnalysis.h" 12 #include "llvm/Analysis/ModuleSummaryAnalysis.h" 13 #include "llvm/Analysis/ProfileSummaryInfo.h" 14 #include "llvm/Analysis/TypeMetadataUtils.h" 15 #include "llvm/Bitcode/BitcodeWriter.h" 16 #include "llvm/IR/Constants.h" 17 #include "llvm/IR/DebugInfo.h" 18 #include "llvm/IR/Intrinsics.h" 19 #include "llvm/IR/Module.h" 20 #include "llvm/IR/PassManager.h" 21 #include "llvm/Pass.h" 22 #include "llvm/Support/FileSystem.h" 23 #include "llvm/Support/ScopedPrinter.h" 24 #include "llvm/Support/raw_ostream.h" 25 #include "llvm/Transforms/IPO.h" 26 #include "llvm/Transforms/IPO/FunctionAttrs.h" 27 #include "llvm/Transforms/Utils/Cloning.h" 28 #include "llvm/Transforms/Utils/ModuleUtils.h" 29 using namespace llvm; 30 31 namespace { 32 33 // Promote each local-linkage entity defined by ExportM and used by ImportM by 34 // changing visibility and appending the given ModuleId. 35 void promoteInternals(Module &ExportM, Module &ImportM, StringRef ModuleId, 36 SetVector<GlobalValue *> &PromoteExtra) { 37 DenseMap<const Comdat *, Comdat *> RenamedComdats; 38 for (auto &ExportGV : ExportM.global_values()) { 39 if (!ExportGV.hasLocalLinkage()) 40 continue; 41 42 auto Name = ExportGV.getName(); 43 GlobalValue *ImportGV = ImportM.getNamedValue(Name); 44 if ((!ImportGV || ImportGV->use_empty()) && !PromoteExtra.count(&ExportGV)) 45 continue; 46 47 std::string NewName = (Name + ModuleId).str(); 48 49 if (const auto *C = ExportGV.getComdat()) 50 if (C->getName() == Name) 51 RenamedComdats.try_emplace(C, ExportM.getOrInsertComdat(NewName)); 52 53 ExportGV.setName(NewName); 54 ExportGV.setLinkage(GlobalValue::ExternalLinkage); 55 ExportGV.setVisibility(GlobalValue::HiddenVisibility); 56 57 if (ImportGV) { 58 ImportGV->setName(NewName); 59 ImportGV->setVisibility(GlobalValue::HiddenVisibility); 60 } 61 } 62 63 if (!RenamedComdats.empty()) 64 for (auto &GO : ExportM.global_objects()) 65 if (auto *C = GO.getComdat()) { 66 auto Replacement = RenamedComdats.find(C); 67 if (Replacement != RenamedComdats.end()) 68 GO.setComdat(Replacement->second); 69 } 70 } 71 72 // Promote all internal (i.e. distinct) type ids used by the module by replacing 73 // them with external type ids formed using the module id. 74 // 75 // Note that this needs to be done before we clone the module because each clone 76 // will receive its own set of distinct metadata nodes. 77 void promoteTypeIds(Module &M, StringRef ModuleId) { 78 DenseMap<Metadata *, Metadata *> LocalToGlobal; 79 auto ExternalizeTypeId = [&](CallInst *CI, unsigned ArgNo) { 80 Metadata *MD = 81 cast<MetadataAsValue>(CI->getArgOperand(ArgNo))->getMetadata(); 82 83 if (isa<MDNode>(MD) && cast<MDNode>(MD)->isDistinct()) { 84 Metadata *&GlobalMD = LocalToGlobal[MD]; 85 if (!GlobalMD) { 86 std::string NewName = 87 (to_string(LocalToGlobal.size()) + ModuleId).str(); 88 GlobalMD = MDString::get(M.getContext(), NewName); 89 } 90 91 CI->setArgOperand(ArgNo, 92 MetadataAsValue::get(M.getContext(), GlobalMD)); 93 } 94 }; 95 96 if (Function *TypeTestFunc = 97 M.getFunction(Intrinsic::getName(Intrinsic::type_test))) { 98 for (const Use &U : TypeTestFunc->uses()) { 99 auto CI = cast<CallInst>(U.getUser()); 100 ExternalizeTypeId(CI, 1); 101 } 102 } 103 104 if (Function *TypeCheckedLoadFunc = 105 M.getFunction(Intrinsic::getName(Intrinsic::type_checked_load))) { 106 for (const Use &U : TypeCheckedLoadFunc->uses()) { 107 auto CI = cast<CallInst>(U.getUser()); 108 ExternalizeTypeId(CI, 2); 109 } 110 } 111 112 for (GlobalObject &GO : M.global_objects()) { 113 SmallVector<MDNode *, 1> MDs; 114 GO.getMetadata(LLVMContext::MD_type, MDs); 115 116 GO.eraseMetadata(LLVMContext::MD_type); 117 for (auto MD : MDs) { 118 auto I = LocalToGlobal.find(MD->getOperand(1)); 119 if (I == LocalToGlobal.end()) { 120 GO.addMetadata(LLVMContext::MD_type, *MD); 121 continue; 122 } 123 GO.addMetadata( 124 LLVMContext::MD_type, 125 *MDNode::get(M.getContext(), 126 ArrayRef<Metadata *>{MD->getOperand(0), I->second})); 127 } 128 } 129 } 130 131 // Drop unused globals, and drop type information from function declarations. 132 // FIXME: If we made functions typeless then there would be no need to do this. 133 void simplifyExternals(Module &M) { 134 FunctionType *EmptyFT = 135 FunctionType::get(Type::getVoidTy(M.getContext()), false); 136 137 for (auto I = M.begin(), E = M.end(); I != E;) { 138 Function &F = *I++; 139 if (F.isDeclaration() && F.use_empty()) { 140 F.eraseFromParent(); 141 continue; 142 } 143 144 if (!F.isDeclaration() || F.getFunctionType() == EmptyFT) 145 continue; 146 147 Function *NewF = 148 Function::Create(EmptyFT, GlobalValue::ExternalLinkage, "", &M); 149 NewF->setVisibility(F.getVisibility()); 150 NewF->takeName(&F); 151 F.replaceAllUsesWith(ConstantExpr::getBitCast(NewF, F.getType())); 152 F.eraseFromParent(); 153 } 154 155 for (auto I = M.global_begin(), E = M.global_end(); I != E;) { 156 GlobalVariable &GV = *I++; 157 if (GV.isDeclaration() && GV.use_empty()) { 158 GV.eraseFromParent(); 159 continue; 160 } 161 } 162 } 163 164 void filterModule( 165 Module *M, function_ref<bool(const GlobalValue *)> ShouldKeepDefinition) { 166 for (Module::alias_iterator I = M->alias_begin(), E = M->alias_end(); 167 I != E;) { 168 GlobalAlias *GA = &*I++; 169 if (ShouldKeepDefinition(GA)) 170 continue; 171 172 GlobalObject *GO; 173 if (GA->getValueType()->isFunctionTy()) 174 GO = Function::Create(cast<FunctionType>(GA->getValueType()), 175 GlobalValue::ExternalLinkage, "", M); 176 else 177 GO = new GlobalVariable( 178 *M, GA->getValueType(), false, GlobalValue::ExternalLinkage, 179 nullptr, "", nullptr, 180 GA->getThreadLocalMode(), GA->getType()->getAddressSpace()); 181 GO->takeName(GA); 182 GA->replaceAllUsesWith(GO); 183 GA->eraseFromParent(); 184 } 185 186 for (Function &F : *M) { 187 if (ShouldKeepDefinition(&F)) 188 continue; 189 190 F.deleteBody(); 191 F.setComdat(nullptr); 192 F.clearMetadata(); 193 } 194 195 for (GlobalVariable &GV : M->globals()) { 196 if (ShouldKeepDefinition(&GV)) 197 continue; 198 199 GV.setInitializer(nullptr); 200 GV.setLinkage(GlobalValue::ExternalLinkage); 201 GV.setComdat(nullptr); 202 GV.clearMetadata(); 203 } 204 } 205 206 void forEachVirtualFunction(Constant *C, function_ref<void(Function *)> Fn) { 207 if (auto *F = dyn_cast<Function>(C)) 208 return Fn(F); 209 if (isa<GlobalValue>(C)) 210 return; 211 for (Value *Op : C->operands()) 212 forEachVirtualFunction(cast<Constant>(Op), Fn); 213 } 214 215 // If it's possible to split M into regular and thin LTO parts, do so and write 216 // a multi-module bitcode file with the two parts to OS. Otherwise, write only a 217 // regular LTO bitcode file to OS. 218 void splitAndWriteThinLTOBitcode( 219 raw_ostream &OS, raw_ostream *ThinLinkOS, 220 function_ref<AAResults &(Function &)> AARGetter, Module &M) { 221 std::string ModuleId = getUniqueModuleId(&M); 222 if (ModuleId.empty()) { 223 // We couldn't generate a module ID for this module, just write it out as a 224 // regular LTO module. 225 WriteBitcodeToFile(&M, OS); 226 if (ThinLinkOS) 227 // We don't have a ThinLTO part, but still write the module to the 228 // ThinLinkOS if requested so that the expected output file is produced. 229 WriteBitcodeToFile(&M, *ThinLinkOS); 230 return; 231 } 232 233 promoteTypeIds(M, ModuleId); 234 235 // Returns whether a global has attached type metadata. Such globals may 236 // participate in CFI or whole-program devirtualization, so they need to 237 // appear in the merged module instead of the thin LTO module. 238 auto HasTypeMetadata = [&](const GlobalObject *GO) { 239 SmallVector<MDNode *, 1> MDs; 240 GO->getMetadata(LLVMContext::MD_type, MDs); 241 return !MDs.empty(); 242 }; 243 244 // Collect the set of virtual functions that are eligible for virtual constant 245 // propagation. Each eligible function must not access memory, must return 246 // an integer of width <=64 bits, must take at least one argument, must not 247 // use its first argument (assumed to be "this") and all arguments other than 248 // the first one must be of <=64 bit integer type. 249 // 250 // Note that we test whether this copy of the function is readnone, rather 251 // than testing function attributes, which must hold for any copy of the 252 // function, even a less optimized version substituted at link time. This is 253 // sound because the virtual constant propagation optimizations effectively 254 // inline all implementations of the virtual function into each call site, 255 // rather than using function attributes to perform local optimization. 256 std::set<const Function *> EligibleVirtualFns; 257 // If any member of a comdat lives in MergedM, put all members of that 258 // comdat in MergedM to keep the comdat together. 259 DenseSet<const Comdat *> MergedMComdats; 260 for (GlobalVariable &GV : M.globals()) 261 if (HasTypeMetadata(&GV)) { 262 if (const auto *C = GV.getComdat()) 263 MergedMComdats.insert(C); 264 forEachVirtualFunction(GV.getInitializer(), [&](Function *F) { 265 auto *RT = dyn_cast<IntegerType>(F->getReturnType()); 266 if (!RT || RT->getBitWidth() > 64 || F->arg_empty() || 267 !F->arg_begin()->use_empty()) 268 return; 269 for (auto &Arg : make_range(std::next(F->arg_begin()), F->arg_end())) { 270 auto *ArgT = dyn_cast<IntegerType>(Arg.getType()); 271 if (!ArgT || ArgT->getBitWidth() > 64) 272 return; 273 } 274 if (computeFunctionBodyMemoryAccess(*F, AARGetter(*F)) == MAK_ReadNone) 275 EligibleVirtualFns.insert(F); 276 }); 277 } 278 279 ValueToValueMapTy VMap; 280 std::unique_ptr<Module> MergedM( 281 CloneModule(&M, VMap, [&](const GlobalValue *GV) -> bool { 282 if (const auto *C = GV->getComdat()) 283 if (MergedMComdats.count(C)) 284 return true; 285 if (auto *F = dyn_cast<Function>(GV)) 286 return EligibleVirtualFns.count(F); 287 if (auto *GVar = dyn_cast_or_null<GlobalVariable>(GV->getBaseObject())) 288 return HasTypeMetadata(GVar); 289 return false; 290 })); 291 StripDebugInfo(*MergedM); 292 293 for (Function &F : *MergedM) 294 if (!F.isDeclaration()) { 295 // Reset the linkage of all functions eligible for virtual constant 296 // propagation. The canonical definitions live in the thin LTO module so 297 // that they can be imported. 298 F.setLinkage(GlobalValue::AvailableExternallyLinkage); 299 F.setComdat(nullptr); 300 } 301 302 SetVector<GlobalValue *> CfiFunctions; 303 for (auto &F : M) 304 if ((!F.hasLocalLinkage() || F.hasAddressTaken()) && HasTypeMetadata(&F)) 305 CfiFunctions.insert(&F); 306 307 // Remove all globals with type metadata, globals with comdats that live in 308 // MergedM, and aliases pointing to such globals from the thin LTO module. 309 filterModule(&M, [&](const GlobalValue *GV) { 310 if (auto *GVar = dyn_cast_or_null<GlobalVariable>(GV->getBaseObject())) 311 if (HasTypeMetadata(GVar)) 312 return false; 313 if (const auto *C = GV->getComdat()) 314 if (MergedMComdats.count(C)) 315 return false; 316 return true; 317 }); 318 319 promoteInternals(*MergedM, M, ModuleId, CfiFunctions); 320 promoteInternals(M, *MergedM, ModuleId, CfiFunctions); 321 322 SmallVector<MDNode *, 8> CfiFunctionMDs; 323 for (auto V : CfiFunctions) { 324 Function &F = *cast<Function>(V); 325 SmallVector<MDNode *, 2> Types; 326 F.getMetadata(LLVMContext::MD_type, Types); 327 328 auto &Ctx = MergedM->getContext(); 329 SmallVector<Metadata *, 4> Elts; 330 Elts.push_back(MDString::get(Ctx, F.getName())); 331 CfiFunctionLinkage Linkage; 332 if (!F.isDeclarationForLinker()) 333 Linkage = CFL_Definition; 334 else if (F.isWeakForLinker()) 335 Linkage = CFL_WeakDeclaration; 336 else 337 Linkage = CFL_Declaration; 338 Elts.push_back(ConstantAsMetadata::get( 339 llvm::ConstantInt::get(Type::getInt8Ty(Ctx), Linkage))); 340 for (auto Type : Types) 341 Elts.push_back(Type); 342 CfiFunctionMDs.push_back(MDTuple::get(Ctx, Elts)); 343 } 344 345 if(!CfiFunctionMDs.empty()) { 346 NamedMDNode *NMD = MergedM->getOrInsertNamedMetadata("cfi.functions"); 347 for (auto MD : CfiFunctionMDs) 348 NMD->addOperand(MD); 349 } 350 351 simplifyExternals(*MergedM); 352 353 // FIXME: Try to re-use BSI and PFI from the original module here. 354 ProfileSummaryInfo PSI(M); 355 ModuleSummaryIndex Index = buildModuleSummaryIndex(M, nullptr, &PSI); 356 357 // Mark the merged module as requiring full LTO. We still want an index for 358 // it though, so that it can participate in summary-based dead stripping. 359 MergedM->addModuleFlag(Module::Error, "ThinLTO", uint32_t(0)); 360 ModuleSummaryIndex MergedMIndex = 361 buildModuleSummaryIndex(*MergedM, nullptr, &PSI); 362 363 SmallVector<char, 0> Buffer; 364 365 BitcodeWriter W(Buffer); 366 // Save the module hash produced for the full bitcode, which will 367 // be used in the backends, and use that in the minimized bitcode 368 // produced for the full link. 369 ModuleHash ModHash = {{0}}; 370 W.writeModule(&M, /*ShouldPreserveUseListOrder=*/false, &Index, 371 /*GenerateHash=*/true, &ModHash); 372 W.writeModule(MergedM.get(), /*ShouldPreserveUseListOrder=*/false, 373 &MergedMIndex); 374 W.writeStrtab(); 375 OS << Buffer; 376 377 // If a minimized bitcode module was requested for the thin link, 378 // strip the debug info (the merged module was already stripped above) 379 // and write it to the given OS. 380 if (ThinLinkOS) { 381 Buffer.clear(); 382 BitcodeWriter W2(Buffer); 383 StripDebugInfo(M); 384 W2.writeModule(&M, /*ShouldPreserveUseListOrder=*/false, &Index, 385 /*GenerateHash=*/false, &ModHash); 386 W2.writeModule(MergedM.get(), /*ShouldPreserveUseListOrder=*/false, 387 &MergedMIndex); 388 W2.writeStrtab(); 389 *ThinLinkOS << Buffer; 390 } 391 } 392 393 // Returns whether this module needs to be split because it uses type metadata. 394 bool requiresSplit(Module &M) { 395 SmallVector<MDNode *, 1> MDs; 396 for (auto &GO : M.global_objects()) { 397 GO.getMetadata(LLVMContext::MD_type, MDs); 398 if (!MDs.empty()) 399 return true; 400 } 401 402 return false; 403 } 404 405 void writeThinLTOBitcode(raw_ostream &OS, raw_ostream *ThinLinkOS, 406 function_ref<AAResults &(Function &)> AARGetter, 407 Module &M, const ModuleSummaryIndex *Index) { 408 // See if this module has any type metadata. If so, we need to split it. 409 if (requiresSplit(M)) 410 return splitAndWriteThinLTOBitcode(OS, ThinLinkOS, AARGetter, M); 411 412 // Otherwise we can just write it out as a regular module. 413 414 // Save the module hash produced for the full bitcode, which will 415 // be used in the backends, and use that in the minimized bitcode 416 // produced for the full link. 417 ModuleHash ModHash = {{0}}; 418 WriteBitcodeToFile(&M, OS, /*ShouldPreserveUseListOrder=*/false, Index, 419 /*GenerateHash=*/true, &ModHash); 420 // If a minimized bitcode module was requested for the thin link, 421 // strip the debug info and write it to the given OS. 422 if (ThinLinkOS) { 423 StripDebugInfo(M); 424 WriteBitcodeToFile(&M, *ThinLinkOS, /*ShouldPreserveUseListOrder=*/false, 425 Index, 426 /*GenerateHash=*/false, &ModHash); 427 } 428 } 429 430 class WriteThinLTOBitcode : public ModulePass { 431 raw_ostream &OS; // raw_ostream to print on 432 // The output stream on which to emit a minimized module for use 433 // just in the thin link, if requested. 434 raw_ostream *ThinLinkOS; 435 436 public: 437 static char ID; // Pass identification, replacement for typeid 438 WriteThinLTOBitcode() : ModulePass(ID), OS(dbgs()), ThinLinkOS(nullptr) { 439 initializeWriteThinLTOBitcodePass(*PassRegistry::getPassRegistry()); 440 } 441 442 explicit WriteThinLTOBitcode(raw_ostream &o, raw_ostream *ThinLinkOS) 443 : ModulePass(ID), OS(o), ThinLinkOS(ThinLinkOS) { 444 initializeWriteThinLTOBitcodePass(*PassRegistry::getPassRegistry()); 445 } 446 447 StringRef getPassName() const override { return "ThinLTO Bitcode Writer"; } 448 449 bool runOnModule(Module &M) override { 450 const ModuleSummaryIndex *Index = 451 &(getAnalysis<ModuleSummaryIndexWrapperPass>().getIndex()); 452 writeThinLTOBitcode(OS, ThinLinkOS, LegacyAARGetter(*this), M, Index); 453 return true; 454 } 455 void getAnalysisUsage(AnalysisUsage &AU) const override { 456 AU.setPreservesAll(); 457 AU.addRequired<AssumptionCacheTracker>(); 458 AU.addRequired<ModuleSummaryIndexWrapperPass>(); 459 AU.addRequired<TargetLibraryInfoWrapperPass>(); 460 } 461 }; 462 } // anonymous namespace 463 464 char WriteThinLTOBitcode::ID = 0; 465 INITIALIZE_PASS_BEGIN(WriteThinLTOBitcode, "write-thinlto-bitcode", 466 "Write ThinLTO Bitcode", false, true) 467 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) 468 INITIALIZE_PASS_DEPENDENCY(ModuleSummaryIndexWrapperPass) 469 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) 470 INITIALIZE_PASS_END(WriteThinLTOBitcode, "write-thinlto-bitcode", 471 "Write ThinLTO Bitcode", false, true) 472 473 ModulePass *llvm::createWriteThinLTOBitcodePass(raw_ostream &Str, 474 raw_ostream *ThinLinkOS) { 475 return new WriteThinLTOBitcode(Str, ThinLinkOS); 476 } 477 478 PreservedAnalyses 479 llvm::ThinLTOBitcodeWriterPass::run(Module &M, ModuleAnalysisManager &AM) { 480 FunctionAnalysisManager &FAM = 481 AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager(); 482 writeThinLTOBitcode(OS, ThinLinkOS, 483 [&FAM](Function &F) -> AAResults & { 484 return FAM.getResult<AAManager>(F); 485 }, 486 M, &AM.getResult<ModuleSummaryIndexAnalysis>(M)); 487 return PreservedAnalyses::all(); 488 } 489