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