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/Object/ModuleSymbolTable.h"
22#include "llvm/Support/raw_ostream.h"
23#include "llvm/Transforms/IPO.h"
24#include "llvm/Transforms/IPO/FunctionAttrs.h"
25#include "llvm/Transforms/IPO/FunctionImport.h"
26#include "llvm/Transforms/IPO/LowerTypeTests.h"
27#include "llvm/Transforms/Utils/Cloning.h"
28#include "llvm/Transforms/Utils/ModuleUtils.h"
29using namespace llvm;
30
31namespace {
32
33// Determine if a promotion alias should be created for a symbol name.
34static 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
46// Promote each local-linkage entity defined by ExportM and used by ImportM by
47// changing visibility and appending the given ModuleId.
48void promoteInternals(Module &ExportM, Module &ImportM, StringRef ModuleId,
49 const SetVector<GlobalValue *> &PromoteExtra) {
50 DenseMap<const Comdat *, Comdat *> RenamedComdats;
51 for (auto &ExportGV : ExportM.global_values()) {
52 if (!ExportGV.hasLocalLinkage())
53 continue;
54
55 auto Name = ExportGV.getName();
56 GlobalValue *ImportGV = nullptr;
57 if (!PromoteExtra.count(key: &ExportGV)) {
58 ImportGV = ImportM.getNamedValue(Name);
59 if (!ImportGV)
60 continue;
61 ImportGV->removeDeadConstantUsers();
62 if (ImportGV->use_empty()) {
63 ImportGV->eraseFromParent();
64 continue;
65 }
66 }
67
68 std::string OldName = Name.str();
69 std::string NewName = (Name + ModuleId).str();
70
71 if (const auto *C = ExportGV.getComdat())
72 if (C->getName() == Name)
73 RenamedComdats.try_emplace(Key: C, Args: ExportM.getOrInsertComdat(Name: NewName));
74
75 ExportGV.setName(NewName);
76 ExportGV.setLinkage(GlobalValue::ExternalLinkage);
77 ExportGV.setVisibility(GlobalValue::HiddenVisibility);
78 // TODO: remove this reassign and instead create an alias.
79 ExportGV.reassignGUID();
80 if (ImportGV) {
81 ImportGV->setName(NewName);
82 ImportGV->setVisibility(GlobalValue::HiddenVisibility);
83 ImportGV->reassignGUID();
84 }
85
86 if (isa<Function>(Val: &ExportGV) && allowPromotionAlias(Name: OldName)) {
87 // Create a local alias with the original name to avoid breaking
88 // references from inline assembly.
89 std::string Alias =
90 ".lto_set_conditional " + OldName + "," + NewName + "\n";
91 ExportM.appendModuleInlineAsm(Fragment: Alias);
92 }
93 }
94
95 if (!RenamedComdats.empty())
96 for (auto &GO : ExportM.global_objects())
97 if (auto *C = GO.getComdat()) {
98 auto Replacement = RenamedComdats.find(Val: C);
99 if (Replacement != RenamedComdats.end())
100 GO.setComdat(Replacement->second);
101 }
102}
103
104// Promote all internal (i.e. distinct) type ids used by the module by replacing
105// them with external type ids formed using the module id.
106//
107// Note that this needs to be done before we clone the module because each clone
108// will receive its own set of distinct metadata nodes.
109void promoteTypeIds(Module &M, StringRef ModuleId) {
110 DenseMap<Metadata *, Metadata *> LocalToGlobal;
111 auto ExternalizeTypeId = [&](CallInst *CI, unsigned ArgNo) {
112 Metadata *MD =
113 cast<MetadataAsValue>(Val: CI->getArgOperand(i: ArgNo))->getMetadata();
114
115 if (isa<MDNode>(Val: MD) && cast<MDNode>(Val: MD)->isDistinct()) {
116 Metadata *&GlobalMD = LocalToGlobal[MD];
117 if (!GlobalMD) {
118 std::string NewName = (Twine(LocalToGlobal.size()) + ModuleId).str();
119 GlobalMD = MDString::get(Context&: M.getContext(), Str: NewName);
120 }
121
122 CI->setArgOperand(i: ArgNo,
123 v: MetadataAsValue::get(Context&: M.getContext(), MD: GlobalMD));
124 }
125 };
126
127 if (Function *TypeTestFunc =
128 Intrinsic::getDeclarationIfExists(M: &M, id: Intrinsic::type_test)) {
129 for (const Use &U : TypeTestFunc->uses()) {
130 auto CI = cast<CallInst>(Val: U.getUser());
131 ExternalizeTypeId(CI, 1);
132 }
133 }
134
135 if (Function *PublicTypeTestFunc =
136 Intrinsic::getDeclarationIfExists(M: &M, id: Intrinsic::public_type_test)) {
137 for (const Use &U : PublicTypeTestFunc->uses()) {
138 auto CI = cast<CallInst>(Val: U.getUser());
139 ExternalizeTypeId(CI, 1);
140 }
141 }
142
143 if (Function *TypeCheckedLoadFunc =
144 Intrinsic::getDeclarationIfExists(M: &M, id: Intrinsic::type_checked_load)) {
145 for (const Use &U : TypeCheckedLoadFunc->uses()) {
146 auto CI = cast<CallInst>(Val: U.getUser());
147 ExternalizeTypeId(CI, 2);
148 }
149 }
150
151 if (Function *TypeCheckedLoadRelativeFunc = Intrinsic::getDeclarationIfExists(
152 M: &M, id: Intrinsic::type_checked_load_relative)) {
153 for (const Use &U : TypeCheckedLoadRelativeFunc->uses()) {
154 auto CI = cast<CallInst>(Val: U.getUser());
155 ExternalizeTypeId(CI, 2);
156 }
157 }
158
159 for (GlobalObject &GO : M.global_objects()) {
160 SmallVector<MDNode *, 1> MDs;
161 GO.getMetadata(KindID: LLVMContext::MD_type, MDs);
162
163 GO.eraseMetadata(KindID: LLVMContext::MD_type);
164 for (auto *MD : MDs) {
165 auto I = LocalToGlobal.find(Val: MD->getOperand(I: 1));
166 if (I == LocalToGlobal.end()) {
167 GO.addMetadata(KindID: LLVMContext::MD_type, MD&: *MD);
168 continue;
169 }
170 GO.addMetadata(
171 KindID: LLVMContext::MD_type,
172 MD&: *MDNode::get(Context&: M.getContext(), MDs: {MD->getOperand(I: 0), I->second}));
173 }
174
175 SmallVector<MDNode *, 1> CGMDs;
176 GO.getMetadata(KindID: LLVMContext::MD_callgraph, MDs&: CGMDs);
177
178 GO.eraseMetadata(KindID: LLVMContext::MD_callgraph);
179 for (auto *MD : CGMDs) {
180 if (MD->getNumOperands() == 1) {
181 auto I = LocalToGlobal.find(Val: MD->getOperand(I: 0));
182 if (I == LocalToGlobal.end()) {
183 GO.addMetadata(KindID: LLVMContext::MD_callgraph, MD&: *MD);
184 continue;
185 }
186 GO.addMetadata(KindID: LLVMContext::MD_callgraph,
187 MD&: *MDNode::get(Context&: M.getContext(), MDs: {I->second}));
188 }
189 }
190 }
191}
192
193// Drop unused globals, and drop type information from function declarations.
194// FIXME: If we made functions typeless then there would be no need to do this.
195void simplifyExternals(Module &M) {
196 FunctionType *EmptyFT =
197 FunctionType::get(Result: Type::getVoidTy(C&: M.getContext()), isVarArg: false);
198
199 for (Function &F : llvm::make_early_inc_range(Range&: M)) {
200 if (F.isDeclaration() && F.use_empty()) {
201 F.eraseFromParent();
202 continue;
203 }
204
205 if (!F.isDeclaration() || F.getFunctionType() == EmptyFT ||
206 // Changing the type of an intrinsic may invalidate the IR.
207 F.getName().starts_with(Prefix: "llvm."))
208 continue;
209
210 Function *NewF =
211 Function::Create(Ty: EmptyFT, Linkage: GlobalValue::ExternalLinkage,
212 AddrSpace: F.getAddressSpace(), N: "", M: &M);
213 NewF->copyAttributesFrom(Src: &F);
214 // Only copy function attribtues.
215 NewF->setAttributes(AttributeList::get(C&: M.getContext(),
216 Index: AttributeList::FunctionIndex,
217 Attrs: F.getAttributes().getFnAttrs()));
218 NewF->takeName(V: &F);
219 NewF->setMetadata(KindID: LLVMContext::MD_guid,
220 Node: F.getMetadata(KindID: LLVMContext::MD_guid));
221 F.replaceAllUsesWith(V: NewF);
222 F.eraseFromParent();
223 }
224
225 for (GlobalIFunc &I : llvm::make_early_inc_range(Range: M.ifuncs())) {
226 if (I.use_empty())
227 I.eraseFromParent();
228 else
229 assert(I.getResolverFunction() && "ifunc misses its resolver function");
230 }
231
232 for (GlobalVariable &GV : llvm::make_early_inc_range(Range: M.globals())) {
233 if (GV.isDeclaration() && GV.use_empty()) {
234 GV.eraseFromParent();
235 continue;
236 }
237 }
238}
239
240static void
241filterModule(Module *M,
242 function_ref<bool(const GlobalValue *)> ShouldKeepDefinition) {
243 std::vector<GlobalValue *> V;
244 for (GlobalValue &GV : M->global_values())
245 if (!ShouldKeepDefinition(&GV))
246 V.push_back(x: &GV);
247
248 for (GlobalValue *GV : V)
249 if (!convertToDeclaration(GV&: *GV))
250 GV->eraseFromParent();
251}
252
253void forEachVirtualFunction(Constant *C, function_ref<void(Function *)> Fn) {
254 if (auto *F = dyn_cast<Function>(Val: C))
255 return Fn(F);
256 if (isa<GlobalValue>(Val: C))
257 return;
258 for (Value *Op : C->operands())
259 forEachVirtualFunction(C: cast<Constant>(Val: Op), Fn);
260}
261
262// Clone any @llvm[.compiler].used over to the new module and append
263// values whose defs were cloned into that module.
264static void cloneUsedGlobalVariables(const Module &SrcM, Module &DestM,
265 bool CompilerUsed) {
266 SmallVector<GlobalValue *, 4> Used, NewUsed;
267 // First collect those in the llvm[.compiler].used set.
268 collectUsedGlobalVariables(M: SrcM, Vec&: Used, CompilerUsed);
269 // Next build a set of the equivalent values defined in DestM.
270 for (auto *V : Used) {
271 auto *GV = DestM.getNamedValue(Name: V->getName());
272 if (GV && !GV->isDeclaration())
273 NewUsed.push_back(Elt: GV);
274 }
275 // Finally, add them to a llvm[.compiler].used variable in DestM.
276 if (CompilerUsed)
277 appendToCompilerUsed(M&: DestM, Values: NewUsed);
278 else
279 appendToUsed(M&: DestM, Values: NewUsed);
280}
281
282#ifndef NDEBUG
283static bool enableUnifiedLTO(Module &M) {
284 bool UnifiedLTO = false;
285 if (auto *MD =
286 mdconst::extract_or_null<ConstantInt>(M.getModuleFlag("UnifiedLTO")))
287 UnifiedLTO = MD->getZExtValue();
288 return UnifiedLTO;
289}
290#endif
291
292bool mustEmitToMergedModule(const GlobalValue *GV) {
293 // The __cfi_check definition is filled in by the CrossDSOCFI pass which
294 // runs only in the merged module.
295 return GV->getName() == "__cfi_check";
296}
297
298// If it's possible to split M into regular and thin LTO parts, do so and write
299// a multi-module bitcode file with the two parts to OS. Otherwise, write only a
300// regular LTO bitcode file to OS.
301void splitAndWriteThinLTOBitcode(
302 raw_ostream &OS, raw_ostream *ThinLinkOS,
303 function_ref<AAResults &(Function &)> AARGetter, Module &M,
304 const bool ShouldPreserveUseListOrder) {
305 std::string ModuleId = getUniqueModuleId(M: &M);
306 if (ModuleId.empty()) {
307 assert(!enableUnifiedLTO(M));
308 // We couldn't generate a module ID for this module, write it out as a
309 // regular LTO module with an index for summary-based dead stripping.
310 ProfileSummaryInfo PSI(M);
311 M.addModuleFlag(Behavior: Module::Error, Key: "ThinLTO", Val: uint32_t(0));
312 ModuleSummaryIndex Index = buildModuleSummaryIndex(M, GetBFICallback: nullptr, PSI: &PSI);
313 WriteBitcodeToFile(M, Out&: OS, ShouldPreserveUseListOrder, Index: &Index,
314 /*UnifiedLTO=*/GenerateHash: false);
315
316 if (ThinLinkOS)
317 // We don't have a ThinLTO part, but still write the module to the
318 // ThinLinkOS if requested so that the expected output file is produced.
319 WriteBitcodeToFile(M, Out&: *ThinLinkOS, ShouldPreserveUseListOrder, Index: &Index,
320 /*UnifiedLTO=*/GenerateHash: false);
321
322 return;
323 }
324
325 promoteTypeIds(M, ModuleId);
326
327 // Returns whether a global or its associated global has attached type
328 // metadata. The former may participate in CFI or whole-program
329 // devirtualization, so they need to appear in the merged module instead of
330 // the thin LTO module. Similarly, globals that are associated with globals
331 // with type metadata need to appear in the merged module because they will
332 // reference the global's section directly.
333 auto HasTypeMetadata = [](const GlobalObject *GO) {
334 if (MDNode *MD = GO->getMetadata(KindID: LLVMContext::MD_associated))
335 if (auto *AssocVM = dyn_cast_or_null<ValueAsMetadata>(Val: MD->getOperand(I: 0)))
336 if (auto *AssocGO = dyn_cast<GlobalObject>(Val: AssocVM->getValue()))
337 if (AssocGO->hasMetadata(KindID: LLVMContext::MD_type))
338 return true;
339 return GO->hasMetadata(KindID: LLVMContext::MD_type);
340 };
341
342 // Collect the set of virtual functions that are eligible for virtual constant
343 // propagation. Each eligible function must not access memory, must return
344 // an integer of width <=64 bits, must take at least one argument, must not
345 // use its first argument (assumed to be "this") and all arguments other than
346 // the first one must be of <=64 bit integer type.
347 //
348 // Note that we test whether this copy of the function is readnone, rather
349 // than testing function attributes, which must hold for any copy of the
350 // function, even a less optimized version substituted at link time. This is
351 // sound because the virtual constant propagation optimizations effectively
352 // inline all implementations of the virtual function into each call site,
353 // rather than using function attributes to perform local optimization.
354 DenseSet<const Function *> EligibleVirtualFns;
355 // If any member of a comdat lives in MergedM, put all members of that
356 // comdat in MergedM to keep the comdat together.
357 DenseSet<const Comdat *> MergedMComdats;
358 for (GlobalVariable &GV : M.globals())
359 if (!GV.isDeclaration() && HasTypeMetadata(&GV)) {
360 if (const auto *C = GV.getComdat())
361 MergedMComdats.insert(V: C);
362 forEachVirtualFunction(C: GV.getInitializer(), Fn: [&](Function *F) {
363 auto *RT = dyn_cast<IntegerType>(Val: F->getReturnType());
364 if (!RT || RT->getBitWidth() > 64 || F->arg_empty() ||
365 !F->arg_begin()->use_empty())
366 return;
367 for (auto &Arg : drop_begin(RangeOrContainer: F->args())) {
368 auto *ArgT = dyn_cast<IntegerType>(Val: Arg.getType());
369 if (!ArgT || ArgT->getBitWidth() > 64)
370 return;
371 }
372 if (!F->isDeclaration() &&
373 computeFunctionBodyMemoryAccess(F&: *F, AAR&: AARGetter(*F))
374 .doesNotAccessMemory())
375 EligibleVirtualFns.insert(V: F);
376 });
377 }
378
379 ValueToValueMapTy VMap;
380 std::unique_ptr<Module> MergedM(
381 CloneModule(M, VMap, ShouldCloneDefinition: [&](const GlobalValue *GV) -> bool {
382 if (const auto *C = GV->getComdat())
383 if (MergedMComdats.count(V: C))
384 return true;
385 if (mustEmitToMergedModule(GV))
386 return true;
387 if (auto *F = dyn_cast<Function>(Val: GV))
388 return EligibleVirtualFns.count(V: F);
389 if (auto *GVar =
390 dyn_cast_or_null<GlobalVariable>(Val: GV->getAliaseeObject()))
391 return HasTypeMetadata(GVar);
392 return false;
393 }));
394 StripDebugInfo(M&: *MergedM);
395 MergedM->removeModuleInlineAsm();
396
397 // Clone any llvm.*used globals to ensure the included values are
398 // not deleted.
399 cloneUsedGlobalVariables(SrcM: M, DestM&: *MergedM, /*CompilerUsed*/ false);
400 cloneUsedGlobalVariables(SrcM: M, DestM&: *MergedM, /*CompilerUsed*/ true);
401
402 for (Function &F : *MergedM)
403 if (!F.isDeclaration() && !mustEmitToMergedModule(GV: &F)) {
404 // Reset the linkage of all functions eligible for virtual constant
405 // propagation. The canonical definitions live in the thin LTO module so
406 // that they can be imported.
407 F.setLinkage(GlobalValue::AvailableExternallyLinkage);
408 F.setComdat(nullptr);
409 }
410
411 SetVector<GlobalValue *> CfiFunctions;
412 for (auto &F : M)
413 if ((!F.hasLocalLinkage() || F.hasAddressTaken()) && HasTypeMetadata(&F))
414 CfiFunctions.insert(X: &F);
415 for (auto &A : M.aliases())
416 if (auto *F = dyn_cast<Function>(Val: A.getAliasee()))
417 if (HasTypeMetadata(F))
418 CfiFunctions.insert(X: &A);
419
420 // Remove all globals with type metadata, globals with comdats that live in
421 // MergedM, and aliases pointing to such globals from the thin LTO module.
422 filterModule(M: &M, ShouldKeepDefinition: [&](const GlobalValue *GV) {
423 if (auto *GVar = dyn_cast_or_null<GlobalVariable>(Val: GV->getAliaseeObject()))
424 if (HasTypeMetadata(GVar))
425 return false;
426 if (const auto *C = GV->getComdat())
427 if (MergedMComdats.count(V: C))
428 return false;
429 if (mustEmitToMergedModule(GV))
430 return false;
431 return true;
432 });
433
434 // CfiFunctions contains only symbols from M. promoteInternals tries to find
435 // match values from its first argument (the "exporting module") in
436 // CfiFunctions. So we only need CfiFunctions for the second promotion (M ->
437 // MergedM)
438 promoteInternals(ExportM&: *MergedM, ImportM&: M, ModuleId, PromoteExtra: {});
439 promoteInternals(ExportM&: M, ImportM&: *MergedM, ModuleId, PromoteExtra: CfiFunctions);
440
441 auto &Ctx = MergedM->getContext();
442 SmallVector<MDNode *, 8> CfiFunctionMDs;
443 for (auto *V : CfiFunctions) {
444 Function &F = *cast<Function>(Val: V->getAliaseeObject());
445 SmallVector<MDNode *, 2> Types;
446 F.getMetadata(KindID: LLVMContext::MD_type, MDs&: Types);
447
448 SmallVector<Metadata *, 4> Elts;
449 Elts.push_back(Elt: MDString::get(Context&: Ctx, Str: V->getName()));
450 CfiFunctionLinkage Linkage;
451 if (lowertypetests::isJumpTableCanonical(F: &F))
452 Linkage = CFL_Definition;
453 else if (F.hasExternalWeakLinkage())
454 Linkage = CFL_WeakDeclaration;
455 else
456 Linkage = CFL_Declaration;
457 Elts.push_back(Elt: ConstantAsMetadata::get(
458 C: llvm::ConstantInt::get(Ty: Type::getInt8Ty(C&: Ctx), V: Linkage)));
459 GlobalValue::GUID GUID = V->getGUID();
460 Elts.push_back(Elt: ConstantAsMetadata::get(
461 C: llvm::ConstantInt::get(Ty: Type::getInt64Ty(C&: Ctx), V: GUID)));
462 append_range(C&: Elts, R&: Types);
463 CfiFunctionMDs.push_back(Elt: MDTuple::get(Context&: Ctx, MDs: Elts));
464 }
465
466 if(!CfiFunctionMDs.empty()) {
467 NamedMDNode *NMD = MergedM->getOrInsertNamedMetadata(Name: "cfi.functions");
468 for (auto *MD : CfiFunctionMDs)
469 NMD->addOperand(M: MD);
470 }
471
472 MapVector<Function *, std::vector<GlobalAlias *>> FunctionAliases;
473 for (auto &A : M.aliases()) {
474 if (!isa<Function>(Val: A.getAliasee()))
475 continue;
476
477 auto *F = cast<Function>(Val: A.getAliasee());
478 FunctionAliases[F].push_back(x: &A);
479 }
480
481 if (!FunctionAliases.empty()) {
482 NamedMDNode *NMD = MergedM->getOrInsertNamedMetadata(Name: "aliases");
483 for (auto &Alias : FunctionAliases) {
484 SmallVector<Metadata *> Elts;
485 Elts.push_back(Elt: MDString::get(Context&: Ctx, Str: Alias.first->getName()));
486 for (auto *A : Alias.second)
487 Elts.push_back(Elt: MDString::get(Context&: Ctx, Str: A->getName()));
488 NMD->addOperand(M: MDTuple::get(Context&: Ctx, MDs: Elts));
489 }
490 }
491
492 SmallVector<MDNode *, 8> Symvers;
493 ModuleSymbolTable::CollectAsmSymvers(M, AsmSymver: [&](StringRef Name, StringRef Alias) {
494 Function *F = M.getFunction(Name);
495 if (!F || F->use_empty())
496 return;
497
498 Symvers.push_back(Elt: MDTuple::get(
499 Context&: Ctx, MDs: {MDString::get(Context&: Ctx, Str: Name), MDString::get(Context&: Ctx, Str: Alias)}));
500 });
501
502 if (!Symvers.empty()) {
503 NamedMDNode *NMD = MergedM->getOrInsertNamedMetadata(Name: "symvers");
504 for (auto *MD : Symvers)
505 NMD->addOperand(M: MD);
506 }
507
508 simplifyExternals(M&: *MergedM);
509
510 // FIXME: Try to re-use BSI and PFI from the original module here.
511 ProfileSummaryInfo PSI(M);
512 ModuleSummaryIndex Index = buildModuleSummaryIndex(M, GetBFICallback: nullptr, PSI: &PSI);
513
514 // Mark the merged module as requiring full LTO. We still want an index for
515 // it though, so that it can participate in summary-based dead stripping.
516 MergedM->addModuleFlag(Behavior: Module::Error, Key: "ThinLTO", Val: uint32_t(0));
517 ModuleSummaryIndex MergedMIndex =
518 buildModuleSummaryIndex(M: *MergedM, GetBFICallback: nullptr, PSI: &PSI);
519
520 SmallVector<char, 0> Buffer;
521
522 BitcodeWriter W(Buffer);
523 // Save the module hash produced for the full bitcode, which will
524 // be used in the backends, and use that in the minimized bitcode
525 // produced for the full link.
526 ModuleHash ModHash = {._M_elems: {0}};
527 W.writeModule(M, ShouldPreserveUseListOrder, Index: &Index,
528 /*GenerateHash=*/true, ModHash: &ModHash);
529 W.writeModule(M: *MergedM, ShouldPreserveUseListOrder, Index: &MergedMIndex);
530 W.writeSymtab();
531 W.writeStrtab();
532 OS << Buffer;
533
534 // If a minimized bitcode module was requested for the thin link, only
535 // the information that is needed by thin link will be written in the
536 // given OS (the merged module will be written as usual).
537 if (ThinLinkOS) {
538 Buffer.clear();
539 BitcodeWriter W2(Buffer);
540 StripDebugInfo(M);
541 W2.writeThinLinkBitcode(M, Index, ModHash);
542 W2.writeModule(M: *MergedM, /*ShouldPreserveUseListOrder=*/false,
543 Index: &MergedMIndex);
544 W2.writeSymtab();
545 W2.writeStrtab();
546 *ThinLinkOS << Buffer;
547 }
548}
549
550// Check if the LTO Unit splitting has been enabled.
551bool enableSplitLTOUnit(Module &M) {
552 bool EnableSplitLTOUnit = false;
553 if (auto *MD = mdconst::extract_or_null<ConstantInt>(
554 MD: M.getModuleFlag(Key: "EnableSplitLTOUnit")))
555 EnableSplitLTOUnit = MD->getZExtValue();
556 return EnableSplitLTOUnit;
557}
558
559// Returns whether this module needs to be split (if splitting is enabled).
560bool requiresSplit(Module &M) {
561 for (auto &GO : M.global_objects()) {
562 if (GO.hasMetadata(KindID: LLVMContext::MD_type))
563 return true;
564 if (mustEmitToMergedModule(GV: &GO))
565 return true;
566 }
567 return false;
568}
569
570bool writeThinLTOBitcode(raw_ostream &OS, raw_ostream *ThinLinkOS,
571 function_ref<AAResults &(Function &)> AARGetter,
572 Module &M, const ModuleSummaryIndex *Index,
573 const bool ShouldPreserveUseListOrder) {
574 std::unique_ptr<ModuleSummaryIndex> NewIndex = nullptr;
575 // See if this module needs to be split. If so, we try to split it
576 // or at least promote type ids to enable WPD.
577 if (requiresSplit(M)) {
578 if (enableSplitLTOUnit(M)) {
579 splitAndWriteThinLTOBitcode(OS, ThinLinkOS, AARGetter, M,
580 ShouldPreserveUseListOrder);
581 return true;
582 }
583 // Promote type ids as needed for index-based WPD.
584 std::string ModuleId = getUniqueModuleId(M: &M);
585 if (!ModuleId.empty()) {
586 promoteTypeIds(M, ModuleId);
587 // Need to rebuild the index so that it contains type metadata
588 // for the newly promoted type ids.
589 // FIXME: Probably should not bother building the index at all
590 // in the caller of writeThinLTOBitcode (which does so via the
591 // ModuleSummaryIndexAnalysis pass), since we have to rebuild it
592 // anyway whenever there is type metadata (here or in
593 // splitAndWriteThinLTOBitcode). Just always build it once via the
594 // buildModuleSummaryIndex when Module(s) are ready.
595 ProfileSummaryInfo PSI(M);
596 NewIndex = std::make_unique<ModuleSummaryIndex>(
597 args: buildModuleSummaryIndex(M, GetBFICallback: nullptr, PSI: &PSI));
598 Index = NewIndex.get();
599 }
600 }
601
602 // Write it out as an unsplit ThinLTO module.
603
604 // Save the module hash produced for the full bitcode, which will
605 // be used in the backends, and use that in the minimized bitcode
606 // produced for the full link.
607 ModuleHash ModHash = {._M_elems: {0}};
608 WriteBitcodeToFile(M, Out&: OS, ShouldPreserveUseListOrder, Index,
609 /*GenerateHash=*/true, ModHash: &ModHash);
610 // If a minimized bitcode module was requested for the thin link, only
611 // the information that is needed by thin link will be written in the
612 // given OS.
613 if (ThinLinkOS && Index)
614 writeThinLinkBitcodeToFile(M, Out&: *ThinLinkOS, Index: *Index, ModHash);
615 return false;
616}
617
618} // anonymous namespace
619
620PreservedAnalyses
621llvm::ThinLTOBitcodeWriterPass::run(Module &M, ModuleAnalysisManager &AM) {
622 FunctionAnalysisManager &FAM =
623 AM.getResult<FunctionAnalysisManagerModuleProxy>(IR&: M).getManager();
624
625 bool Changed = writeThinLTOBitcode(
626 OS, ThinLinkOS,
627 AARGetter: [&FAM](Function &F) -> AAResults & {
628 return FAM.getResult<AAManager>(IR&: F);
629 },
630 M, Index: &AM.getResult<ModuleSummaryIndexAnalysis>(IR&: M),
631 ShouldPreserveUseListOrder);
632
633 return Changed ? PreservedAnalyses::none() : PreservedAnalyses::all();
634}
635