ThinLTOBitcodeWriter.cpp 22 KB

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