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