1//===-LTO.cpp - LLVM Link Time Optimizer ----------------------------------===//
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// This file implements functions and classes used to support LTO.
10//
11//===----------------------------------------------------------------------===//
12
13#include "llvm/LTO/LTO.h"
14#include "llvm/ADT/ArrayRef.h"
15#include "llvm/ADT/ScopeExit.h"
16#include "llvm/ADT/SmallSet.h"
17#include "llvm/ADT/StableHashing.h"
18#include "llvm/ADT/Statistic.h"
19#include "llvm/ADT/StringExtras.h"
20#include "llvm/Analysis/OptimizationRemarkEmitter.h"
21#include "llvm/Analysis/StackSafetyAnalysis.h"
22#include "llvm/Analysis/TargetTransformInfo.h"
23#include "llvm/Bitcode/BitcodeReader.h"
24#include "llvm/Bitcode/BitcodeWriter.h"
25#include "llvm/CGData/CodeGenData.h"
26#include "llvm/CodeGen/Analysis.h"
27#include "llvm/Config/llvm-config.h"
28#include "llvm/IR/AutoUpgrade.h"
29#include "llvm/IR/DiagnosticPrinter.h"
30#include "llvm/IR/GlobalValue.h"
31#include "llvm/IR/Intrinsics.h"
32#include "llvm/IR/LLVMRemarkStreamer.h"
33#include "llvm/IR/LegacyPassManager.h"
34#include "llvm/IR/Mangler.h"
35#include "llvm/IR/Metadata.h"
36#include "llvm/IR/RuntimeLibcalls.h"
37#include "llvm/LTO/LTOBackend.h"
38#include "llvm/Linker/IRMover.h"
39#include "llvm/MC/TargetRegistry.h"
40#include "llvm/Object/IRObjectFile.h"
41#include "llvm/Plugins/PassPlugin.h"
42#include "llvm/Support/Caching.h"
43#include "llvm/Support/CommandLine.h"
44#include "llvm/Support/Compiler.h"
45#include "llvm/Support/Error.h"
46#include "llvm/Support/FileSystem.h"
47#include "llvm/Support/MemoryBuffer.h"
48#include "llvm/Support/Path.h"
49#include "llvm/Support/Process.h"
50#include "llvm/Support/SHA1.h"
51#include "llvm/Support/Signals.h"
52#include "llvm/Support/SourceMgr.h"
53#include "llvm/Support/ThreadPool.h"
54#include "llvm/Support/Threading.h"
55#include "llvm/Support/TimeProfiler.h"
56#include "llvm/Support/ToolOutputFile.h"
57#include "llvm/Support/VCSRevision.h"
58#include "llvm/Support/raw_ostream.h"
59#include "llvm/Target/TargetOptions.h"
60#include "llvm/Transforms/IPO.h"
61#include "llvm/Transforms/IPO/MemProfContextDisambiguation.h"
62#include "llvm/Transforms/IPO/WholeProgramDevirt.h"
63#include "llvm/Transforms/Utils/FunctionImportUtils.h"
64#include "llvm/Transforms/Utils/SplitModule.h"
65
66#include <optional>
67#include <set>
68
69using namespace llvm;
70using namespace lto;
71using namespace object;
72
73#define DEBUG_TYPE "lto"
74
75Error LTO::setupOptimizationRemarks() {
76 // Setup the remark streamer according to the provided configuration.
77 auto DiagFileOrErr = lto::setupLLVMOptimizationRemarks(
78 Context&: RegularLTO.Ctx, RemarksFilename: Conf.RemarksFilename, RemarksPasses: Conf.RemarksPasses,
79 RemarksFormat: Conf.RemarksFormat, RemarksWithHotness: Conf.RemarksWithHotness,
80 RemarksHotnessThreshold: Conf.RemarksHotnessThreshold);
81 if (!DiagFileOrErr)
82 return DiagFileOrErr.takeError();
83
84 DiagnosticOutputFile = std::move(*DiagFileOrErr);
85
86 // Create a dummy function to serve as a context for LTO-link remarks.
87 // This is required because OptimizationRemark requires a valid Function,
88 // and in ThinLTO we may not have any IR functions available during the
89 // thin link. Host it in a private module to avoid interfering with the LTO
90 // process.
91 if (!LinkerRemarkFunction) {
92 DummyModule = std::make_unique<Module>(args: "remark_dummy", args&: RegularLTO.Ctx);
93 LinkerRemarkFunction = Function::Create(
94 Ty: FunctionType::get(Result: Type::getVoidTy(C&: RegularLTO.Ctx), isVarArg: false),
95 Linkage: GlobalValue::ExternalLinkage, N: "thinlto_remark_dummy",
96 M: DummyModule.get());
97 }
98
99 return Error::success();
100}
101
102void LTO::emitRemark(OptimizationRemark &Remark) {
103 const Function &F = Remark.getFunction();
104 OptimizationRemarkEmitter ORE(const_cast<Function *>(&F));
105 ORE.emit(OptDiag&: Remark);
106}
107
108static cl::opt<bool>
109 DumpThinCGSCCs("dump-thin-cg-sccs", cl::init(Val: false), cl::Hidden,
110 cl::desc("Dump the SCCs in the ThinLTO index's callgraph"));
111namespace llvm {
112extern cl::opt<bool> ForceImportAll;
113extern cl::opt<bool> AlwaysRenamePromotedLocals;
114} // end namespace llvm
115
116namespace llvm {
117/// Enable global value internalization in LTO.
118cl::opt<bool> EnableLTOInternalization(
119 "enable-lto-internalization", cl::init(Val: true), cl::Hidden,
120 cl::desc("Enable global value internalization in LTO"));
121
122static cl::opt<bool>
123 LTOKeepSymbolCopies("lto-keep-symbol-copies", cl::init(Val: false), cl::Hidden,
124 cl::desc("Keep copies of symbols in LTO indexing"));
125
126/// Indicate we are linking with an allocator that supports hot/cold operator
127/// new interfaces.
128extern cl::opt<bool> SupportsHotColdNew;
129
130/// Enable MemProf context disambiguation for thin link.
131extern cl::opt<bool> EnableMemProfContextDisambiguation;
132} // namespace llvm
133
134// Computes a unique hash for the Module considering the current list of
135// export/import and other global analysis results.
136// Returns the hash in its hexadecimal representation.
137std::string llvm::computeLTOCacheKey(
138 const Config &Conf, const ModuleSummaryIndex &Index, StringRef ModuleID,
139 const FunctionImporter::ImportMapTy &ImportList,
140 const FunctionImporter::ExportSetTy &ExportList,
141 const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
142 const GVSummaryMapTy &DefinedGlobals,
143 const DenseSet<GlobalValue::GUID> &CfiFunctionDefs,
144 const DenseSet<GlobalValue::GUID> &CfiFunctionDecls) {
145 // Compute the unique hash for this entry.
146 // This is based on the current compiler version, the module itself, the
147 // export list, the hash for every single module in the import list, the
148 // list of ResolvedODR for the module, and the list of preserved symbols.
149 SHA1 Hasher;
150
151 // Start with the compiler revision
152 Hasher.update(LLVM_VERSION_STRING);
153#ifdef LLVM_REVISION
154 Hasher.update(LLVM_REVISION);
155#endif
156
157 // Include the parts of the LTO configuration that affect code generation.
158 auto AddString = [&](StringRef Str) {
159 Hasher.update(Str);
160 Hasher.update(Data: ArrayRef<uint8_t>{0});
161 };
162 auto AddUnsigned = [&](unsigned I) {
163 uint8_t Data[4];
164 support::endian::write32le(P: Data, V: I);
165 Hasher.update(Data);
166 };
167 auto AddUint64 = [&](uint64_t I) {
168 uint8_t Data[8];
169 support::endian::write64le(P: Data, V: I);
170 Hasher.update(Data);
171 };
172 auto AddUint8 = [&](const uint8_t I) {
173 Hasher.update(Data: ArrayRef<uint8_t>(&I, 1));
174 };
175 AddString(Conf.CPU);
176 // FIXME: Hash more of Options. For now all clients initialize Options from
177 // command-line flags (which is unsupported in production), but may set
178 // X86RelaxRelocations. The clang driver can also pass FunctionSections and
179 // DataSections via command line flags.
180 AddUnsigned(Conf.Options.MCOptions.X86RelaxRelocations);
181 AddUnsigned(Conf.Options.FunctionSections);
182 AddUnsigned(Conf.Options.DataSections);
183 for (auto &A : Conf.MAttrs)
184 AddString(A);
185 if (Conf.RelocModel)
186 AddUnsigned(*Conf.RelocModel);
187 else
188 AddUnsigned(-1);
189 if (Conf.CodeModel)
190 AddUnsigned(*Conf.CodeModel);
191 else
192 AddUnsigned(-1);
193 for (const auto &S : Conf.MllvmArgs)
194 AddString(S);
195 AddUnsigned(static_cast<int>(Conf.CGOptLevel));
196 AddUnsigned(static_cast<int>(Conf.CGFileType));
197 AddUnsigned(Conf.OptLevel);
198 AddUnsigned(Conf.Freestanding);
199 AddString(Conf.OptPipeline);
200 AddString(Conf.AAPipeline);
201 AddString(Conf.OverrideTriple);
202 AddString(Conf.DefaultTriple);
203 AddString(Conf.DwoDir);
204 AddUint8(Conf.Dtlto);
205
206 // Include the hash for the current module
207 auto ModHash = Index.getModuleHash(ModPath: ModuleID);
208 Hasher.update(Data: ArrayRef<uint8_t>((uint8_t *)&ModHash[0], sizeof(ModHash)));
209
210 // TODO: `ExportList` is determined by `ImportList`. Since `ImportList` is
211 // used to compute cache key, we could omit hashing `ExportList` here.
212 std::vector<uint64_t> ExportsGUID;
213 ExportsGUID.reserve(n: ExportList.size());
214 for (const auto &VI : ExportList)
215 ExportsGUID.push_back(x: VI.getGUID());
216
217 // Sort the export list elements GUIDs.
218 llvm::sort(C&: ExportsGUID);
219 for (auto GUID : ExportsGUID)
220 Hasher.update(Data: ArrayRef<uint8_t>((uint8_t *)&GUID, sizeof(GUID)));
221
222 // Order using module hash, to be both independent of module name and
223 // module order.
224 auto Comp = [&](const std::pair<StringRef, GlobalValue::GUID> &L,
225 const std::pair<StringRef, GlobalValue::GUID> &R) {
226 return std::make_pair(x: Index.getModule(ModPath: L.first)->second, y: L.second) <
227 std::make_pair(x: Index.getModule(ModPath: R.first)->second, y: R.second);
228 };
229 FunctionImporter::SortedImportList SortedImportList(ImportList, Comp);
230
231 // Count the number of imports for each source module.
232 DenseMap<StringRef, unsigned> ModuleToNumImports;
233 for (const auto &[FromModule, GUID, Type] : SortedImportList)
234 ++ModuleToNumImports[FromModule];
235
236 std::optional<StringRef> LastModule;
237 for (const auto &[FromModule, GUID, Type] : SortedImportList) {
238 if (LastModule != FromModule) {
239 // Include the hash for every module we import functions from. The set of
240 // imported symbols for each module may affect code generation and is
241 // sensitive to link order, so include that as well.
242 LastModule = FromModule;
243 auto ModHash = Index.getModule(ModPath: FromModule)->second;
244 Hasher.update(Data: ArrayRef<uint8_t>((uint8_t *)&ModHash[0], sizeof(ModHash)));
245 AddUint64(ModuleToNumImports[FromModule]);
246 }
247 AddUint64(GUID);
248 AddUint8(Type);
249 }
250
251 // Include the hash for the resolved ODR.
252 for (auto &Entry : ResolvedODR) {
253 Hasher.update(Data: ArrayRef<uint8_t>((const uint8_t *)&Entry.first,
254 sizeof(GlobalValue::GUID)));
255 Hasher.update(Data: ArrayRef<uint8_t>((const uint8_t *)&Entry.second,
256 sizeof(GlobalValue::LinkageTypes)));
257 }
258
259 // Members of CfiFunctionDefs and CfiFunctionDecls that are referenced or
260 // defined in this module.
261 std::set<GlobalValue::GUID> UsedCfiDefs;
262 std::set<GlobalValue::GUID> UsedCfiDecls;
263
264 // Typeids used in this module.
265 std::set<GlobalValue::GUID> UsedTypeIds;
266
267 auto AddUsedCfiGlobal = [&](GlobalValue::GUID ValueGUID) {
268 if (CfiFunctionDefs.contains(V: ValueGUID))
269 UsedCfiDefs.insert(x: ValueGUID);
270 if (CfiFunctionDecls.contains(V: ValueGUID))
271 UsedCfiDecls.insert(x: ValueGUID);
272 };
273
274 auto AddUsedThings = [&](GlobalValueSummary *GS) {
275 if (!GS) return;
276 AddUnsigned(GS->getVisibility());
277 AddUnsigned(GS->isLive());
278 AddUnsigned(GS->canAutoHide());
279 for (const ValueInfo &VI : GS->refs()) {
280 AddUnsigned(VI.isDSOLocal(WithDSOLocalPropagation: Index.withDSOLocalPropagation()));
281 AddUsedCfiGlobal(VI.getGUID());
282 }
283 if (auto *GVS = dyn_cast<GlobalVarSummary>(Val: GS)) {
284 AddUnsigned(GVS->maybeReadOnly());
285 AddUnsigned(GVS->maybeWriteOnly());
286 }
287 if (auto *FS = dyn_cast<FunctionSummary>(Val: GS)) {
288 for (auto &TT : FS->type_tests())
289 UsedTypeIds.insert(x: TT);
290 for (auto &TT : FS->type_test_assume_vcalls())
291 UsedTypeIds.insert(x: TT.GUID);
292 for (auto &TT : FS->type_checked_load_vcalls())
293 UsedTypeIds.insert(x: TT.GUID);
294 for (auto &TT : FS->type_test_assume_const_vcalls())
295 UsedTypeIds.insert(x: TT.VFunc.GUID);
296 for (auto &TT : FS->type_checked_load_const_vcalls())
297 UsedTypeIds.insert(x: TT.VFunc.GUID);
298 for (auto &ET : FS->calls()) {
299 AddUnsigned(ET.first.isDSOLocal(WithDSOLocalPropagation: Index.withDSOLocalPropagation()));
300 AddUsedCfiGlobal(ET.first.getGUID());
301 }
302 }
303 };
304
305 // Sort the defined globals by GUID to be independent of the insertion order,
306 // which may depend on the order that modules are added.
307 SmallVector<std::pair<GlobalValue::GUID, GlobalValueSummary *>>
308 SortedDefinedGlobals(DefinedGlobals.begin(), DefinedGlobals.end());
309 llvm::sort(C&: SortedDefinedGlobals, Comp: llvm::less_first());
310 for (auto &GS : SortedDefinedGlobals) {
311 // Include the hash for the linkage type to reflect internalization and weak
312 // resolution, and collect any used type identifier resolutions.
313 GlobalValue::LinkageTypes Linkage = GS.second->linkage();
314 Hasher.update(
315 Data: ArrayRef<uint8_t>((const uint8_t *)&Linkage, sizeof(Linkage)));
316 AddUsedCfiGlobal(GS.first);
317 AddUsedThings(GS.second);
318 }
319
320 // Imported functions may introduce new uses of type identifier resolutions,
321 // so we need to collect their used resolutions as well.
322 for (const auto &[FromModule, GUID, Type] : SortedImportList) {
323 GlobalValueSummary *S = Index.findSummaryInModule(ValueGUID: GUID, ModuleId: FromModule);
324 AddUsedThings(S);
325 // If this is an alias, we also care about any types/etc. that the aliasee
326 // may reference.
327 if (auto *AS = dyn_cast_or_null<AliasSummary>(Val: S))
328 AddUsedThings(AS->getBaseObject());
329 }
330
331 auto AddTypeIdSummary = [&](StringRef TId, const TypeIdSummary &S) {
332 AddString(TId);
333
334 AddUnsigned(S.TTRes.TheKind);
335 AddUnsigned(S.TTRes.SizeM1BitWidth);
336
337 AddUint64(S.TTRes.AlignLog2);
338 AddUint64(S.TTRes.SizeM1);
339 AddUint64(S.TTRes.BitMask);
340 AddUint64(S.TTRes.InlineBits);
341
342 AddUint64(S.WPDRes.size());
343 for (auto &WPD : S.WPDRes) {
344 AddUnsigned(WPD.first);
345 AddUnsigned(WPD.second.TheKind);
346 AddString(WPD.second.SingleImplName);
347
348 AddUint64(WPD.second.ResByArg.size());
349 for (auto &ByArg : WPD.second.ResByArg) {
350 AddUint64(ByArg.first.size());
351 for (uint64_t Arg : ByArg.first)
352 AddUint64(Arg);
353 AddUnsigned(ByArg.second.TheKind);
354 AddUint64(ByArg.second.Info);
355 AddUnsigned(ByArg.second.Byte);
356 AddUnsigned(ByArg.second.Bit);
357 }
358 }
359 };
360
361 // Include the hash for all type identifiers used by this module.
362 for (GlobalValue::GUID TId : UsedTypeIds) {
363 auto TidIter = Index.typeIds().equal_range(x: TId);
364 for (const auto &I : make_range(p: TidIter))
365 AddTypeIdSummary(I.second.first, I.second.second);
366 }
367
368 AddUnsigned(UsedCfiDefs.size());
369 for (auto &V : UsedCfiDefs)
370 AddUint64(V);
371
372 AddUnsigned(UsedCfiDecls.size());
373 for (auto &V : UsedCfiDecls)
374 AddUint64(V);
375
376 if (!Conf.SampleProfile.empty()) {
377 auto FileOrErr = MemoryBuffer::getFile(Filename: Conf.SampleProfile);
378 if (FileOrErr) {
379 Hasher.update(Str: FileOrErr.get()->getBuffer());
380
381 if (!Conf.ProfileRemapping.empty()) {
382 FileOrErr = MemoryBuffer::getFile(Filename: Conf.ProfileRemapping);
383 if (FileOrErr)
384 Hasher.update(Str: FileOrErr.get()->getBuffer());
385 }
386 }
387 }
388
389 return toHex(Input: Hasher.result());
390}
391
392std::string llvm::recomputeLTOCacheKey(const std::string &Key,
393 StringRef ExtraID) {
394 SHA1 Hasher;
395
396 auto AddString = [&](StringRef Str) {
397 Hasher.update(Str);
398 Hasher.update(Data: ArrayRef<uint8_t>{0});
399 };
400 AddString(Key);
401 AddString(ExtraID);
402
403 return toHex(Input: Hasher.result());
404}
405
406static void thinLTOResolvePrevailingGUID(
407 const Config &C, ValueInfo VI,
408 DenseSet<GlobalValueSummary *> &GlobalInvolvedWithAlias,
409 function_ref<bool(GlobalValue::GUID, const GlobalValueSummary *)>
410 isPrevailing,
411 function_ref<void(StringRef, GlobalValue::GUID, GlobalValue::LinkageTypes)>
412 recordNewLinkage,
413 const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols) {
414 GlobalValue::VisibilityTypes Visibility =
415 C.VisibilityScheme == Config::ELF ? VI.getELFVisibility()
416 : GlobalValue::DefaultVisibility;
417 for (auto &S : VI.getSummaryList()) {
418 GlobalValue::LinkageTypes OriginalLinkage = S->linkage();
419 // Ignore local and appending linkage values since the linker
420 // doesn't resolve them.
421 if (GlobalValue::isLocalLinkage(Linkage: OriginalLinkage) ||
422 GlobalValue::isAppendingLinkage(Linkage: S->linkage()))
423 continue;
424 // We need to emit only one of these. The prevailing module will keep it,
425 // but turned into a weak, while the others will drop it when possible.
426 // This is both a compile-time optimization and a correctness
427 // transformation. This is necessary for correctness when we have exported
428 // a reference - we need to convert the linkonce to weak to
429 // ensure a copy is kept to satisfy the exported reference.
430 // FIXME: We may want to split the compile time and correctness
431 // aspects into separate routines.
432 if (isPrevailing(VI.getGUID(), S.get())) {
433 assert(!S->wasPromoted() &&
434 "promoted symbols used to be internal linkage and shouldn't have "
435 "a prevailing variant");
436 if (GlobalValue::isLinkOnceLinkage(Linkage: OriginalLinkage)) {
437 S->setLinkage(GlobalValue::getWeakLinkage(
438 ODR: GlobalValue::isLinkOnceODRLinkage(Linkage: OriginalLinkage)));
439 // The kept copy is eligible for auto-hiding (hidden visibility) if all
440 // copies were (i.e. they were all linkonce_odr global unnamed addr).
441 // If any copy is not (e.g. it was originally weak_odr), then the symbol
442 // must remain externally available (e.g. a weak_odr from an explicitly
443 // instantiated template). Additionally, if it is in the
444 // GUIDPreservedSymbols set, that means that it is visibile outside
445 // the summary (e.g. in a native object or a bitcode file without
446 // summary), and in that case we cannot hide it as it isn't possible to
447 // check all copies.
448 S->setCanAutoHide(VI.canAutoHide() &&
449 !GUIDPreservedSymbols.count(V: VI.getGUID()));
450 }
451 if (C.VisibilityScheme == Config::FromPrevailing)
452 Visibility = S->getVisibility();
453 }
454 // Alias and aliasee can't be turned into available_externally.
455 // When force-import-all is used, it indicates that object linking is not
456 // supported by the target. In this case, we can't change the linkage as
457 // well in case the global is converted to declaration.
458 // Also, if the symbol was promoted, it wouldn't have a prevailing variant,
459 // but also its linkage is set correctly (to External) already.
460 else if (!isa<AliasSummary>(Val: S.get()) &&
461 !GlobalInvolvedWithAlias.count(V: S.get()) && !ForceImportAll &&
462 !S->wasPromoted())
463 S->setLinkage(GlobalValue::AvailableExternallyLinkage);
464
465 // For ELF, set visibility to the computed visibility from summaries. We
466 // don't track visibility from declarations so this may be more relaxed than
467 // the most constraining one.
468 if (C.VisibilityScheme == Config::ELF)
469 S->setVisibility(Visibility);
470
471 if (S->linkage() != OriginalLinkage)
472 recordNewLinkage(S->modulePath(), VI.getGUID(), S->linkage());
473 }
474
475 if (C.VisibilityScheme == Config::FromPrevailing) {
476 for (auto &S : VI.getSummaryList()) {
477 GlobalValue::LinkageTypes OriginalLinkage = S->linkage();
478 if (GlobalValue::isLocalLinkage(Linkage: OriginalLinkage) ||
479 GlobalValue::isAppendingLinkage(Linkage: S->linkage()))
480 continue;
481 S->setVisibility(Visibility);
482 }
483 }
484}
485
486/// Resolve linkage for prevailing symbols in the \p Index.
487//
488// We'd like to drop these functions if they are no longer referenced in the
489// current module. However there is a chance that another module is still
490// referencing them because of the import. We make sure we always emit at least
491// one copy.
492void llvm::thinLTOResolvePrevailingInIndex(
493 const Config &C, ModuleSummaryIndex &Index,
494 function_ref<bool(GlobalValue::GUID, const GlobalValueSummary *)>
495 isPrevailing,
496 function_ref<void(StringRef, GlobalValue::GUID, GlobalValue::LinkageTypes)>
497 recordNewLinkage,
498 const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols) {
499 // We won't optimize the globals that are referenced by an alias for now
500 // Ideally we should turn the alias into a global and duplicate the definition
501 // when needed.
502 DenseSet<GlobalValueSummary *> GlobalInvolvedWithAlias;
503 for (auto &I : Index)
504 for (auto &S : I.second.getSummaryList())
505 if (auto AS = dyn_cast<AliasSummary>(Val: S.get()))
506 GlobalInvolvedWithAlias.insert(V: &AS->getAliasee());
507
508 for (auto &I : Index)
509 thinLTOResolvePrevailingGUID(C, VI: Index.getValueInfo(R: I),
510 GlobalInvolvedWithAlias, isPrevailing,
511 recordNewLinkage, GUIDPreservedSymbols);
512}
513
514static void thinLTOInternalizeAndPromoteGUID(
515 ValueInfo VI, function_ref<bool(StringRef, ValueInfo)> isExported,
516 function_ref<bool(GlobalValue::GUID, const GlobalValueSummary *)>
517 isPrevailing,
518 DenseSet<StringRef> *ExternallyVisibleSymbolNamesPtr) {
519 // Before performing index-based internalization and promotion for this GUID,
520 // the local flag should be consistent with the summary list linkage types.
521 VI.verifyLocal();
522
523 const bool SingleExternallyVisibleCopy =
524 VI.getSummaryList().size() == 1 &&
525 !GlobalValue::isLocalLinkage(Linkage: VI.getSummaryList().front()->linkage());
526
527 bool NameRecorded = false;
528 for (auto &S : VI.getSummaryList()) {
529 // First see if we need to promote an internal value because it is not
530 // exported.
531 if (isExported(S->modulePath(), VI)) {
532 if (GlobalValue::isLocalLinkage(Linkage: S->linkage())) {
533 // Only the first local GlobalValue in a list of summaries does not
534 // need renaming. In rare cases if there exist more than one summaries
535 // in the list, the rest of them must have renaming (through promotion)
536 // to avoid conflict.
537 if (ExternallyVisibleSymbolNamesPtr && !NameRecorded) {
538 NameRecorded = true;
539 if (ExternallyVisibleSymbolNamesPtr->insert(V: VI.name()).second)
540 S->setNoRenameOnPromotion(true);
541 }
542
543 S->promote();
544 }
545 continue;
546 }
547
548 // Otherwise, see if we can internalize.
549 if (!EnableLTOInternalization)
550 continue;
551
552 // Non-exported values with external linkage can be internalized.
553 if (GlobalValue::isExternalLinkage(Linkage: S->linkage())) {
554 S->setLinkage(GlobalValue::InternalLinkage);
555 continue;
556 }
557
558 // Non-exported function and variable definitions with a weak-for-linker
559 // linkage can be internalized in certain cases. The minimum legality
560 // requirements would be that they are not address taken to ensure that we
561 // don't break pointer equality checks, and that variables are either read-
562 // or write-only. For functions, this is the case if either all copies are
563 // [local_]unnamed_addr, or we can propagate reference edge attributes
564 // (which is how this is guaranteed for variables, when analyzing whether
565 // they are read or write-only).
566 //
567 // However, we only get to this code for weak-for-linkage values in one of
568 // two cases:
569 // 1) The prevailing copy is not in IR (it is in native code).
570 // 2) The prevailing copy in IR is not exported from its module.
571 // Additionally, at least for the new LTO API, case 2 will only happen if
572 // there is exactly one definition of the value (i.e. in exactly one
573 // module), as duplicate defs are result in the value being marked exported.
574 // Likely, users of the legacy LTO API are similar, however, currently there
575 // are llvm-lto based tests of the legacy LTO API that do not mark
576 // duplicate linkonce_odr copies as exported via the tool, so we need
577 // to handle that case below by checking the number of copies.
578 //
579 // Generally, we only want to internalize a weak-for-linker value in case
580 // 2, because in case 1 we cannot see how the value is used to know if it
581 // is read or write-only. We also don't want to bloat the binary with
582 // multiple internalized copies of non-prevailing linkonce/weak functions.
583 // Note if we don't internalize, we will convert non-prevailing copies to
584 // available_externally anyway, so that we drop them after inlining. The
585 // only reason to internalize such a function is if we indeed have a single
586 // copy, because internalizing it won't increase binary size, and enables
587 // use of inliner heuristics that are more aggressive in the face of a
588 // single call to a static (local). For variables, internalizing a read or
589 // write only variable can enable more aggressive optimization. However, we
590 // already perform this elsewhere in the ThinLTO backend handling for
591 // read or write-only variables (processGlobalForThinLTO).
592 //
593 // Therefore, only internalize linkonce/weak if there is a single copy, that
594 // is prevailing in this IR module. We can do so aggressively, without
595 // requiring the address to be insignificant, or that a variable be read or
596 // write-only.
597 if (!GlobalValue::isWeakForLinker(Linkage: S->linkage()) ||
598 GlobalValue::isExternalWeakLinkage(Linkage: S->linkage()))
599 continue;
600
601 // We may have a single summary copy that is externally visible but not
602 // prevailing if the prevailing copy is in a native object.
603 if (SingleExternallyVisibleCopy && isPrevailing(VI.getGUID(), S.get()))
604 S->setLinkage(GlobalValue::InternalLinkage);
605 }
606}
607
608// Update the linkages in the given \p Index to mark exported values
609// as external and non-exported values as internal.
610void llvm::thinLTOInternalizeAndPromoteInIndex(
611 ModuleSummaryIndex &Index,
612 function_ref<bool(StringRef, ValueInfo)> isExported,
613 function_ref<bool(GlobalValue::GUID, const GlobalValueSummary *)>
614 isPrevailing,
615 DenseSet<StringRef> *ExternallyVisibleSymbolNamesPtr) {
616 assert(!Index.withInternalizeAndPromote());
617
618 for (auto &I : Index)
619 thinLTOInternalizeAndPromoteGUID(VI: Index.getValueInfo(R: I), isExported,
620 isPrevailing,
621 ExternallyVisibleSymbolNamesPtr);
622 Index.setWithInternalizeAndPromote();
623}
624
625// Requires a destructor for std::vector<InputModule>.
626InputFile::~InputFile() = default;
627
628Expected<std::unique_ptr<InputFile>> InputFile::create(MemoryBufferRef Object) {
629 std::unique_ptr<InputFile> File(new InputFile);
630
631 Expected<IRSymtabFile> FOrErr = readIRSymtab(MBRef: Object);
632 if (!FOrErr)
633 return FOrErr.takeError();
634
635 File->TargetTriple = FOrErr->TheReader.getTargetTriple();
636 File->SourceFileName = FOrErr->TheReader.getSourceFileName();
637 File->COFFLinkerOpts = FOrErr->TheReader.getCOFFLinkerOpts();
638 File->DependentLibraries = FOrErr->TheReader.getDependentLibraries();
639 File->ComdatTable = FOrErr->TheReader.getComdatTable();
640 File->MbRef =
641 Object; // Save a memory buffer reference to an input file object.
642
643 for (unsigned I = 0; I != FOrErr->Mods.size(); ++I) {
644 size_t Begin = File->Symbols.size();
645 for (const irsymtab::Reader::SymbolRef &Sym :
646 FOrErr->TheReader.module_symbols(I))
647 // Skip symbols that are irrelevant to LTO. Note that this condition needs
648 // to match the one in Skip() in LTO::addRegularLTO().
649 if (Sym.isGlobal() && !Sym.isFormatSpecific())
650 File->Symbols.push_back(x: Sym);
651 File->ModuleSymIndices.push_back(x: {Begin, File->Symbols.size()});
652 }
653
654 File->Mods = FOrErr->Mods;
655 File->Strtab = std::move(FOrErr->Strtab);
656 return std::move(File);
657}
658
659bool InputFile::Symbol::isLibcall(
660 const TargetLibraryInfo &TLI,
661 const RTLIB::RuntimeLibcallsInfo &Libcalls) const {
662 if (TLI.has(F: TLI.getLibFunc(funcName: IRName)))
663 return true;
664 return Libcalls.getSupportedLibcallImpl(FuncName: IRName) != RTLIB::Unsupported;
665}
666
667StringRef InputFile::getName() const {
668 return Mods[0].getModuleIdentifier();
669}
670
671BitcodeModule &InputFile::getSingleBitcodeModule() {
672 assert(Mods.size() == 1 && "Expect only one bitcode module");
673 return Mods[0];
674}
675
676BitcodeModule &InputFile::getPrimaryBitcodeModule() { return Mods[0]; }
677
678LTO::RegularLTOState::RegularLTOState(unsigned ParallelCodeGenParallelismLevel,
679 const Config &Conf)
680 : ParallelCodeGenParallelismLevel(ParallelCodeGenParallelismLevel),
681 Ctx(Conf), CombinedModule(std::make_unique<Module>(args: "ld-temp.o", args&: Ctx)),
682 Mover(std::make_unique<IRMover>(args&: *CombinedModule)) {}
683
684LTO::ThinLTOState::ThinLTOState(ThinBackend BackendParam)
685 : Backend(std::move(BackendParam)), CombinedIndex(/*HaveGVs*/ false) {
686 if (!Backend.isValid())
687 Backend =
688 createInProcessThinBackend(Parallelism: llvm::heavyweight_hardware_concurrency());
689}
690
691LTO::LTO(Config Conf, ThinBackend Backend,
692 unsigned ParallelCodeGenParallelismLevel, LTOKind LTOMode)
693 : Conf(std::move(Conf)),
694 RegularLTO(ParallelCodeGenParallelismLevel, this->Conf),
695 ThinLTO(std::move(Backend)),
696 GlobalResolutions(
697 std::make_unique<DenseMap<StringRef, GlobalResolution>>()),
698 LTOMode(LTOMode) {
699 if (Conf.KeepSymbolNameCopies || LTOKeepSymbolCopies) {
700 Alloc = std::make_unique<BumpPtrAllocator>();
701 GlobalResolutionSymbolSaver = std::make_unique<llvm::StringSaver>(args&: *Alloc);
702 }
703}
704
705// Requires a destructor for MapVector<BitcodeModule>.
706LTO::~LTO() = default;
707
708void LTO::cleanup() {
709 DummyModule.reset();
710 LinkerRemarkFunction = nullptr;
711 consumeError(Err: finalizeOptimizationRemarks(DiagOutputFile: std::move(DiagnosticOutputFile)));
712}
713
714// Add the symbols in the given module to the GlobalResolutions map, and resolve
715// their partitions.
716void LTO::addModuleToGlobalRes(ArrayRef<InputFile::Symbol> Syms,
717 ArrayRef<SymbolResolution> Res,
718 unsigned Partition, bool InSummary,
719 const Triple &TT) {
720 llvm::TimeTraceScope timeScope("LTO add module to global resolution");
721 auto *ResI = Res.begin();
722 auto *ResE = Res.end();
723 (void)ResE;
724 RTLIB::RuntimeLibcallsInfo Libcalls(TT);
725 TargetLibraryInfoImpl TLII(TT);
726 TargetLibraryInfo TLI(TLII);
727 for (const InputFile::Symbol &Sym : Syms) {
728 assert(ResI != ResE);
729 SymbolResolution Res = *ResI++;
730
731 StringRef SymbolName = Sym.getName();
732 // Keep copies of symbols if the client of LTO says so.
733 if (GlobalResolutionSymbolSaver && !GlobalResolutions->contains(Val: SymbolName))
734 SymbolName = GlobalResolutionSymbolSaver->save(S: SymbolName);
735
736 auto &GlobalRes = (*GlobalResolutions)[SymbolName];
737 GlobalRes.UnnamedAddr &= Sym.isUnnamedAddr();
738
739 // In rare occasion, the symbol used to initialize GlobalRes has a different
740 // IRName from the inspected Symbol. This can happen on macOS + iOS, when a
741 // symbol is referenced through its mangled name, say @"\01_symbol" while
742 // the IRName is @symbol (the prefix underscore comes from MachO mangling).
743 // In that case, we have the same actual Symbol that can get two different
744 // GUID, leading to some invalid internalization. Workaround this by marking
745 // the GlobalRes external.
746 // Do this check before the IRName is rewritten below, otherwise it'll miss
747 // the case where a prevailing symbol comes first.
748
749 // FIXME: instead of this check, it would be desirable to compute GUIDs
750 // based on mangled name, but this requires an access to the Target Triple
751 // and would be relatively invasive on the codebase.
752 // FIXME: use the GUID member of GlobalRes.
753 if ((GlobalRes.Prevailing || !GlobalRes.IRName.empty()) &&
754 GlobalRes.IRName != Sym.getIRName()) {
755 GlobalRes.Partition = GlobalResolution::External;
756 GlobalRes.VisibleOutsideSummary = true;
757 }
758
759 if (Res.Prevailing) {
760 assert(!GlobalRes.Prevailing &&
761 "Multiple prevailing defs are not allowed");
762 GlobalRes.Prevailing = true;
763 GlobalRes.IRName = std::string(Sym.getIRName());
764 } else if (!GlobalRes.Prevailing && GlobalRes.IRName.empty()) {
765 // Sometimes it can be two copies of symbol in a module and prevailing
766 // symbol can have no IR name. That might happen if symbol is defined in
767 // module level inline asm block. In case we have multiple modules with
768 // the same symbol we want to use IR name of the prevailing symbol.
769 // Otherwise, if we haven't seen a prevailing symbol, set the name so that
770 // we can later use it to check if there is any prevailing copy in IR.
771 GlobalRes.IRName = std::string(Sym.getIRName());
772 }
773
774 bool IsLibcall = Sym.isLibcall(TLI, Libcalls);
775
776 // Set the partition to external if we know it is re-defined by the linker
777 // with -defsym or -wrap options, used elsewhere, e.g. it is visible to a
778 // regular object, is referenced from llvm.compiler.used/llvm.used, or was
779 // already recorded as being referenced from a different partition.
780 if (Res.LinkerRedefined || Res.VisibleToRegularObj || Sym.isUsed() ||
781 IsLibcall ||
782 (GlobalRes.Partition != GlobalResolution::Unknown &&
783 GlobalRes.Partition != Partition)) {
784 GlobalRes.Partition = GlobalResolution::External;
785 } else
786 // First recorded reference, save the current partition.
787 GlobalRes.Partition = Partition;
788
789 // Flag as visible outside of summary if visible from a regular object or
790 // from a module that does not have a summary.
791 GlobalRes.VisibleOutsideSummary |=
792 (Res.VisibleToRegularObj || Sym.isUsed() || IsLibcall || !InSummary);
793
794 GlobalRes.ExportDynamic |= Res.ExportDynamic;
795 }
796}
797
798void LTO::releaseGlobalResolutionsMemory() {
799 // Release GlobalResolutions dense-map itself.
800 GlobalResolutions.reset();
801 // Release the string saver memory.
802 GlobalResolutionSymbolSaver.reset();
803 Alloc.reset();
804}
805
806static void writeToResolutionFile(raw_ostream &OS, InputFile *Input,
807 ArrayRef<SymbolResolution> Res) {
808 StringRef Path = Input->getName();
809 OS << Path << '\n';
810 auto ResI = Res.begin();
811 for (const InputFile::Symbol &Sym : Input->symbols()) {
812 assert(ResI != Res.end());
813 SymbolResolution Res = *ResI++;
814
815 OS << "-r=" << Path << ',' << Sym.getName() << ',';
816 if (Res.Prevailing)
817 OS << 'p';
818 if (Res.FinalDefinitionInLinkageUnit)
819 OS << 'l';
820 if (Res.VisibleToRegularObj)
821 OS << 'x';
822 if (Res.LinkerRedefined)
823 OS << 'r';
824 OS << '\n';
825 }
826 OS.flush();
827 assert(ResI == Res.end());
828}
829
830Error LTO::add(std::unique_ptr<InputFile> InputPtr,
831 ArrayRef<SymbolResolution> Res) {
832 llvm::TimeTraceScope timeScope("LTO add input", InputPtr->getName());
833 assert(!CalledGetMaxTasks);
834
835 Expected<std::shared_ptr<InputFile>> InputOrErr =
836 addInput(InputPtr: std::move(InputPtr));
837 if (!InputOrErr)
838 return InputOrErr.takeError();
839 InputFile *Input = (*InputOrErr).get();
840
841 if (Conf.ResolutionFile)
842 writeToResolutionFile(OS&: *Conf.ResolutionFile, Input, Res);
843
844 if (RegularLTO.CombinedModule->getTargetTriple().empty()) {
845 Triple InputTriple(Input->getTargetTriple());
846 RegularLTO.CombinedModule->setTargetTriple(InputTriple);
847 if (InputTriple.isOSBinFormatELF())
848 Conf.VisibilityScheme = Config::ELF;
849 }
850
851 ArrayRef<SymbolResolution> InputRes = Res;
852 for (unsigned I = 0; I != Input->Mods.size(); ++I) {
853 if (auto Err = addModule(Input&: *Input, InputRes, ModI: I, Res).moveInto(Value&: Res))
854 return Err;
855 }
856
857 assert(Res.empty());
858 return Error::success();
859}
860
861void LTO::setBitcodeLibFuncs(ArrayRef<StringRef> BitcodeLibFuncs) {
862 assert(this->BitcodeLibFuncs.empty() &&
863 "bitcode libfuncs were set twice; maybe accidentally clobbered?");
864 this->BitcodeLibFuncs.append(in_start: BitcodeLibFuncs.begin(), in_end: BitcodeLibFuncs.end());
865}
866
867Expected<ArrayRef<SymbolResolution>>
868LTO::addModule(InputFile &Input, ArrayRef<SymbolResolution> InputRes,
869 unsigned ModI, ArrayRef<SymbolResolution> Res) {
870 llvm::TimeTraceScope timeScope("LTO add module", Input.getName());
871 Expected<BitcodeLTOInfo> LTOInfo = Input.Mods[ModI].getLTOInfo();
872 if (!LTOInfo)
873 return LTOInfo.takeError();
874
875 if (EnableSplitLTOUnit) {
876 // If only some modules were split, flag this in the index so that
877 // we can skip or error on optimizations that need consistently split
878 // modules (whole program devirt and lower type tests).
879 if (*EnableSplitLTOUnit != LTOInfo->EnableSplitLTOUnit)
880 ThinLTO.CombinedIndex.setPartiallySplitLTOUnits();
881 } else
882 EnableSplitLTOUnit = LTOInfo->EnableSplitLTOUnit;
883
884 BitcodeModule BM = Input.Mods[ModI];
885
886 if ((LTOMode == LTOK_UnifiedRegular || LTOMode == LTOK_UnifiedThin) &&
887 !LTOInfo->UnifiedLTO)
888 return make_error<StringError>(
889 Args: "unified LTO compilation must use "
890 "compatible bitcode modules (use -funified-lto)",
891 Args: inconvertibleErrorCode());
892
893 if (LTOInfo->UnifiedLTO && LTOMode == LTOK_Default)
894 LTOMode = LTOK_UnifiedThin;
895
896 bool IsThinLTO = LTOInfo->IsThinLTO && (LTOMode != LTOK_UnifiedRegular);
897 // If any of the modules inside of a input bitcode file was compiled with
898 // ThinLTO, we assume that the whole input file also was compiled with
899 // ThinLTO.
900 Input.IsThinLTO |= IsThinLTO;
901
902 auto ModSyms = Input.module_symbols(I: ModI);
903 addModuleToGlobalRes(Syms: ModSyms, Res,
904 Partition: IsThinLTO ? ThinLTO.ModuleMap.size() + 1 : 0,
905 InSummary: LTOInfo->HasSummary, TT: Triple(Input.getTargetTriple()));
906
907 if (IsThinLTO)
908 return addThinLTO(BM, Syms: ModSyms, Res);
909
910 RegularLTO.EmptyCombinedModule = false;
911 auto ModOrErr = addRegularLTO(Input, InputRes, BM, Syms: ModSyms, Res);
912 if (!ModOrErr)
913 return ModOrErr.takeError();
914 Res = ModOrErr->second;
915
916 if (!LTOInfo->HasSummary) {
917 if (Error Err = linkRegularLTO(Mod: std::move(ModOrErr->first),
918 /*LivenessFromIndex=*/false))
919 return Err;
920 return Res;
921 }
922
923 // Regular LTO module summaries are added to a dummy module that represents
924 // the combined regular LTO module.
925 if (Error Err = BM.readSummary(CombinedIndex&: ThinLTO.CombinedIndex, ModulePath: ""))
926 return Err;
927 RegularLTO.ModsWithSummaries.push_back(x: std::move(ModOrErr->first));
928 return Res;
929}
930
931// Checks whether the given global value is in a non-prevailing comdat
932// (comdat containing values the linker indicated were not prevailing,
933// which we then dropped to available_externally), and if so, removes
934// it from the comdat. This is called for all global values to ensure the
935// comdat is empty rather than leaving an incomplete comdat. It is needed for
936// regular LTO modules, in case we are in a mixed-LTO mode (both regular
937// and thin LTO modules) compilation. Since the regular LTO module will be
938// linked first in the final native link, we want to make sure the linker
939// doesn't select any of these incomplete comdats that would be left
940// in the regular LTO module without this cleanup.
941static void
942handleNonPrevailingComdat(GlobalValue &GV,
943 std::set<const Comdat *> &NonPrevailingComdats) {
944 Comdat *C = GV.getComdat();
945 if (!C)
946 return;
947
948 if (!NonPrevailingComdats.count(x: C))
949 return;
950
951 // Additionally need to drop all global values from the comdat to
952 // available_externally, to satisfy the COMDAT requirement that all members
953 // are discarded as a unit. The non-local linkage global values avoid
954 // duplicate definition linker errors.
955 GV.setLinkage(GlobalValue::AvailableExternallyLinkage);
956
957 if (auto GO = dyn_cast<GlobalObject>(Val: &GV))
958 GO->setComdat(nullptr);
959}
960
961// Add a regular LTO object to the link.
962// The resulting module needs to be linked into the combined LTO module with
963// linkRegularLTO.
964Expected<
965 std::pair<LTO::RegularLTOState::AddedModule, ArrayRef<SymbolResolution>>>
966LTO::addRegularLTO(InputFile &Input, ArrayRef<SymbolResolution> InputRes,
967 BitcodeModule BM, ArrayRef<InputFile::Symbol> Syms,
968 ArrayRef<SymbolResolution> Res) {
969 llvm::TimeTraceScope timeScope("LTO add regular LTO");
970 RegularLTOState::AddedModule Mod;
971 Expected<std::unique_ptr<Module>> MOrErr =
972 BM.getLazyModule(Context&: RegularLTO.Ctx, /*ShouldLazyLoadMetadata*/ true,
973 /*IsImporting*/ false);
974 if (!MOrErr)
975 return MOrErr.takeError();
976 Module &M = **MOrErr;
977 Mod.M = std::move(*MOrErr);
978
979 if (Error Err = M.materializeMetadata())
980 return std::move(Err);
981
982 if (LTOMode == LTOK_UnifiedRegular) {
983 // cfi.functions metadata is intended to be used with ThinLTO and may
984 // trigger invalid IR transformations if they are present when doing regular
985 // LTO, so delete it.
986 if (NamedMDNode *CfiFunctionsMD = M.getNamedMetadata(Name: "cfi.functions"))
987 M.eraseNamedMetadata(NMD: CfiFunctionsMD);
988 } else if (NamedMDNode *AliasesMD = M.getNamedMetadata(Name: "aliases")) {
989 // Delete aliases entries for non-prevailing symbols on the ThinLTO side of
990 // this input file.
991 DenseSet<StringRef> Prevailing;
992 for (auto [I, R] : zip(t: Input.symbols(), u&: InputRes))
993 if (R.Prevailing && !I.getIRName().empty())
994 Prevailing.insert(V: I.getIRName());
995 std::vector<MDNode *> AliasGroups;
996 for (MDNode *AliasGroup : AliasesMD->operands()) {
997 std::vector<Metadata *> Aliases;
998 for (Metadata *Alias : AliasGroup->operands()) {
999 if (isa<MDString>(Val: Alias) &&
1000 Prevailing.count(V: cast<MDString>(Val: Alias)->getString()))
1001 Aliases.push_back(x: Alias);
1002 }
1003 if (Aliases.size() > 1)
1004 AliasGroups.push_back(x: MDTuple::get(Context&: RegularLTO.Ctx, MDs: Aliases));
1005 }
1006 AliasesMD->clearOperands();
1007 for (MDNode *G : AliasGroups)
1008 AliasesMD->addOperand(M: G);
1009 }
1010
1011 UpgradeDebugInfo(M);
1012
1013 ModuleSymbolTable SymTab;
1014 SymTab.addModule(M: &M);
1015
1016 for (GlobalVariable &GV : M.globals())
1017 if (GV.hasAppendingLinkage())
1018 Mod.Keep.push_back(x: &GV);
1019
1020 DenseSet<GlobalObject *> AliasedGlobals;
1021 for (auto &GA : M.aliases())
1022 if (GlobalObject *GO = GA.getAliaseeObject())
1023 AliasedGlobals.insert(V: GO);
1024
1025 // In this function we need IR GlobalValues matching the symbols in Syms
1026 // (which is not backed by a module), so we need to enumerate them in the same
1027 // order. The symbol enumeration order of a ModuleSymbolTable intentionally
1028 // matches the order of an irsymtab, but when we read the irsymtab in
1029 // InputFile::create we omit some symbols that are irrelevant to LTO. The
1030 // Skip() function skips the same symbols from the module as InputFile does
1031 // from the symbol table.
1032 auto MsymI = SymTab.symbols().begin(), MsymE = SymTab.symbols().end();
1033 auto Skip = [&]() {
1034 while (MsymI != MsymE) {
1035 auto Flags = SymTab.getSymbolFlags(S: *MsymI);
1036 if ((Flags & object::BasicSymbolRef::SF_Global) &&
1037 !(Flags & object::BasicSymbolRef::SF_FormatSpecific))
1038 return;
1039 ++MsymI;
1040 }
1041 };
1042 Skip();
1043
1044 std::set<const Comdat *> NonPrevailingComdats;
1045 SmallSet<StringRef, 2> NonPrevailingAsmSymbols;
1046 for (const InputFile::Symbol &Sym : Syms) {
1047 assert(!Res.empty());
1048 const SymbolResolution &R = Res.consume_front();
1049
1050 assert(MsymI != MsymE);
1051 ModuleSymbolTable::Symbol Msym = *MsymI++;
1052 Skip();
1053
1054 if (GlobalValue *GV = dyn_cast_if_present<GlobalValue *>(Val&: Msym)) {
1055 if (R.Prevailing) {
1056 if (Sym.isUndefined())
1057 continue;
1058 Mod.Keep.push_back(x: GV);
1059 // For symbols re-defined with linker -wrap and -defsym options,
1060 // set the linkage to weak to inhibit IPO. The linkage will be
1061 // restored by the linker.
1062 if (R.LinkerRedefined)
1063 GV->setLinkage(GlobalValue::WeakAnyLinkage);
1064
1065 GlobalValue::LinkageTypes OriginalLinkage = GV->getLinkage();
1066 if (GlobalValue::isLinkOnceLinkage(Linkage: OriginalLinkage))
1067 GV->setLinkage(GlobalValue::getWeakLinkage(
1068 ODR: GlobalValue::isLinkOnceODRLinkage(Linkage: OriginalLinkage)));
1069 } else if (isa<GlobalObject>(Val: GV) &&
1070 (GV->hasLinkOnceODRLinkage() || GV->hasWeakODRLinkage() ||
1071 GV->hasAvailableExternallyLinkage()) &&
1072 !AliasedGlobals.count(V: cast<GlobalObject>(Val: GV))) {
1073 // Any of the above three types of linkage indicates that the
1074 // chosen prevailing symbol will have the same semantics as this copy of
1075 // the symbol, so we may be able to link it with available_externally
1076 // linkage. We will decide later whether to do that when we link this
1077 // module (in linkRegularLTO), based on whether it is undefined.
1078 Mod.Keep.push_back(x: GV);
1079 GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
1080 if (GV->hasComdat())
1081 NonPrevailingComdats.insert(x: GV->getComdat());
1082 cast<GlobalObject>(Val: GV)->setComdat(nullptr);
1083 }
1084
1085 // Set the 'local' flag based on the linker resolution for this symbol.
1086 if (R.FinalDefinitionInLinkageUnit) {
1087 GV->setDSOLocal(true);
1088 if (GV->hasDLLImportStorageClass())
1089 GV->setDLLStorageClass(GlobalValue::DLLStorageClassTypes::
1090 DefaultStorageClass);
1091 }
1092 } else if (auto *AS =
1093 dyn_cast_if_present<ModuleSymbolTable::AsmSymbol *>(Val&: Msym)) {
1094 // Collect non-prevailing symbols.
1095 if (!R.Prevailing)
1096 NonPrevailingAsmSymbols.insert(V: AS->first);
1097 } else {
1098 llvm_unreachable("unknown symbol type");
1099 }
1100
1101 // Common resolution: collect the maximum size/alignment over all commons.
1102 // We also record if we see an instance of a common as prevailing, so that
1103 // if none is prevailing we can ignore it later.
1104 if (Sym.isCommon()) {
1105 // FIXME: We should figure out what to do about commons defined by asm.
1106 // For now they aren't reported correctly by ModuleSymbolTable.
1107 auto &CommonRes = RegularLTO.Commons[std::string(Sym.getIRName())];
1108 CommonRes.Size = std::max(a: CommonRes.Size, b: Sym.getCommonSize());
1109 if (uint32_t SymAlignValue = Sym.getCommonAlignment()) {
1110 CommonRes.Alignment =
1111 std::max(a: Align(SymAlignValue), b: CommonRes.Alignment);
1112 }
1113 CommonRes.Prevailing |= R.Prevailing;
1114 }
1115 }
1116
1117 if (!M.getComdatSymbolTable().empty())
1118 for (GlobalValue &GV : M.global_values())
1119 handleNonPrevailingComdat(GV, NonPrevailingComdats);
1120
1121 // Prepend ".lto_discard <sym>, <sym>*" directive to each module inline asm
1122 // block.
1123 if (M.hasModuleInlineAsm()) {
1124 std::string NewIA = ".lto_discard";
1125 if (!NonPrevailingAsmSymbols.empty()) {
1126 // Don't dicard a symbol if there is a live .symver for it.
1127 ModuleSymbolTable::CollectAsmSymvers(
1128 M, AsmSymver: [&](StringRef Name, StringRef Alias) {
1129 if (!NonPrevailingAsmSymbols.count(V: Alias))
1130 NonPrevailingAsmSymbols.erase(V: Name);
1131 });
1132 NewIA += " " + llvm::join(R&: NonPrevailingAsmSymbols, Separator: ", ");
1133 }
1134 NewIA += "\n";
1135 M.prependModuleInlineAsm(Fragment: {NewIA, M.getModuleInlineAsm().front().Props});
1136 }
1137
1138 assert(MsymI == MsymE);
1139 return std::make_pair(x: std::move(Mod), y&: Res);
1140}
1141
1142Error LTO::linkRegularLTO(RegularLTOState::AddedModule Mod,
1143 bool LivenessFromIndex) {
1144 llvm::TimeTraceScope timeScope("LTO link regular LTO");
1145 std::vector<GlobalValue *> Keep;
1146 for (GlobalValue *GV : Mod.Keep) {
1147 if (LivenessFromIndex) {
1148 const auto GUID = GV->getGUIDOrFallback();
1149 if (!ThinLTO.CombinedIndex.isGUIDLive(GUID)) {
1150 if (Function *F = dyn_cast<Function>(Val: GV)) {
1151 if (DiagnosticOutputFile) {
1152 if (Error Err = F->materialize())
1153 return Err;
1154 auto R = OptimizationRemark(DEBUG_TYPE, "deadfunction", F);
1155 R << ore::NV("Function", F) << " not added to the combined module ";
1156 emitRemark(Remark&: R);
1157 }
1158 }
1159 continue;
1160 }
1161 }
1162
1163 if (!GV->hasAvailableExternallyLinkage()) {
1164 Keep.push_back(x: GV);
1165 continue;
1166 }
1167
1168 // Only link available_externally definitions if we don't already have a
1169 // definition.
1170 GlobalValue *CombinedGV =
1171 RegularLTO.CombinedModule->getNamedValue(Name: GV->getName());
1172 if (CombinedGV && !CombinedGV->isDeclaration())
1173 continue;
1174
1175 Keep.push_back(x: GV);
1176 }
1177
1178 return RegularLTO.Mover->move(Src: std::move(Mod.M), ValuesToLink: Keep, AddLazyFor: nullptr,
1179 /* IsPerformingImport */ false);
1180}
1181
1182// Add a ThinLTO module to the link.
1183Expected<ArrayRef<SymbolResolution>>
1184LTO::addThinLTO(BitcodeModule BM, ArrayRef<InputFile::Symbol> Syms,
1185 ArrayRef<SymbolResolution> Res) {
1186 llvm::TimeTraceScope timeScope("LTO add thin LTO");
1187 const auto BMID = BM.getModuleIdentifier();
1188 ArrayRef<SymbolResolution> ResTmp = Res;
1189 DenseSet<StringRef> Prevailing;
1190 for (const InputFile::Symbol &Sym : Syms) {
1191 assert(!ResTmp.empty());
1192 const SymbolResolution &R = ResTmp.consume_front();
1193 if (!Sym.getIRName().empty() && R.Prevailing)
1194 Prevailing.insert(V: Sym.getIRName());
1195 }
1196
1197 // Track the GUIDs stored in the bitcode GUID table.
1198 StringMap<GlobalValue::GUID> IRSpecifiedGUIDs;
1199 if (Error Err = BM.readSummary(
1200 CombinedIndex&: ThinLTO.CombinedIndex, ModulePath: BMID,
1201 IsPrevailing: [&](StringRef Name) { return (Prevailing.count(V: Name) > 0); },
1202 OnValueInfo: [&](ValueInfo VI) {
1203 auto IT = IRSpecifiedGUIDs.insert(KV: {VI.name(), VI.getGUID()});
1204 (void)IT;
1205 assert(IT.second);
1206 }))
1207 return Err;
1208 LLVM_DEBUG(dbgs() << "Module " << BMID << "\n");
1209
1210 for (const InputFile::Symbol &Sym : Syms) {
1211 assert(!Res.empty());
1212 const SymbolResolution &R = Res.consume_front();
1213 auto GUIDIter = IRSpecifiedGUIDs.find(Key: Sym.getIRName());
1214 // The bitcode GUID table might not be present if this is an old bitcode
1215 // file. For backwards-compatibility, just compute the GUID now in that
1216 // case.
1217 auto GUID =
1218 GUIDIter == IRSpecifiedGUIDs.end()
1219 ? GlobalValue::getGUIDAssumingExternalLinkage(
1220 GlobalName: GlobalValue::getGlobalIdentifier(
1221 Name: Sym.getIRName(), Linkage: GlobalValue::ExternalLinkage, FileName: ""))
1222 : GUIDIter->second;
1223 if (!Sym.getIRName().empty() &&
1224 (R.Prevailing || R.FinalDefinitionInLinkageUnit)) {
1225 if (R.Prevailing) {
1226 (*GlobalResolutions)[Sym.getName()].setGUID(GUID);
1227 ThinLTO.setPrevailingModuleForGUID(GUID, Module: BMID);
1228 // For linker redefined symbols (via --wrap or --defsym) we want to
1229 // switch the linkage to `weak` to prevent IPOs from happening.
1230 // Find the summary in the module for this very GV and record the new
1231 // linkage so that we can switch it when we import the GV.
1232 if (R.LinkerRedefined)
1233 if (auto *S = ThinLTO.CombinedIndex.findSummaryInModule(ValueGUID: GUID, ModuleId: BMID))
1234 S->setLinkage(GlobalValue::WeakAnyLinkage);
1235 }
1236
1237 // If the linker resolved the symbol to a local definition then mark it
1238 // as local in the summary for the module we are adding.
1239 if (R.FinalDefinitionInLinkageUnit) {
1240 if (auto *S = ThinLTO.CombinedIndex.findSummaryInModule(ValueGUID: GUID, ModuleId: BMID)) {
1241 S->setDSOLocal(true);
1242 }
1243 }
1244 }
1245 }
1246
1247 if (!ThinLTO.ModuleMap.insert(KV: {BMID, BM}).second)
1248 return make_error<StringError>(
1249 Args: "Expected at most one ThinLTO module per bitcode file",
1250 Args: inconvertibleErrorCode());
1251
1252 if (!Conf.ThinLTOModulesToCompile.empty()) {
1253 if (!ThinLTO.ModulesToCompile)
1254 ThinLTO.ModulesToCompile = ModuleMapType();
1255 // This is a fuzzy name matching where only modules with name containing the
1256 // specified switch values are going to be compiled.
1257 for (const std::string &Name : Conf.ThinLTOModulesToCompile) {
1258 if (BMID.contains(Other: Name)) {
1259 ThinLTO.ModulesToCompile->insert(KV: {BMID, BM});
1260 LLVM_DEBUG(dbgs() << "[ThinLTO] Selecting " << BMID << " to compile\n");
1261 break;
1262 }
1263 }
1264 }
1265
1266 return Res;
1267}
1268
1269unsigned LTO::getMaxTasks() const {
1270 CalledGetMaxTasks = true;
1271 auto ModuleCount = ThinLTO.ModulesToCompile ? ThinLTO.ModulesToCompile->size()
1272 : ThinLTO.ModuleMap.size();
1273 return RegularLTO.ParallelCodeGenParallelismLevel + ModuleCount;
1274}
1275
1276// If only some of the modules were split, we cannot correctly handle
1277// code that contains type tests or type checked loads.
1278Error LTO::checkPartiallySplit() {
1279 if (!ThinLTO.CombinedIndex.partiallySplitLTOUnits())
1280 return Error::success();
1281
1282 const Module *Combined = RegularLTO.CombinedModule.get();
1283 Function *TypeTestFunc =
1284 Intrinsic::getDeclarationIfExists(M: Combined, id: Intrinsic::type_test);
1285 Function *TypeCheckedLoadFunc =
1286 Intrinsic::getDeclarationIfExists(M: Combined, id: Intrinsic::type_checked_load);
1287 Function *TypeCheckedLoadRelativeFunc = Intrinsic::getDeclarationIfExists(
1288 M: Combined, id: Intrinsic::type_checked_load_relative);
1289
1290 // First check if there are type tests / type checked loads in the
1291 // merged regular LTO module IR.
1292 if ((TypeTestFunc && !TypeTestFunc->use_empty()) ||
1293 (TypeCheckedLoadFunc && !TypeCheckedLoadFunc->use_empty()) ||
1294 (TypeCheckedLoadRelativeFunc &&
1295 !TypeCheckedLoadRelativeFunc->use_empty()))
1296 return make_error<StringError>(
1297 Args: "inconsistent LTO Unit splitting (recompile with -fsplit-lto-unit)",
1298 Args: inconvertibleErrorCode());
1299
1300 // Otherwise check if there are any recorded in the combined summary from the
1301 // ThinLTO modules.
1302 for (auto &P : ThinLTO.CombinedIndex) {
1303 for (auto &S : P.second.getSummaryList()) {
1304 auto *FS = dyn_cast<FunctionSummary>(Val: S.get());
1305 if (!FS)
1306 continue;
1307 if (!FS->type_test_assume_vcalls().empty() ||
1308 !FS->type_checked_load_vcalls().empty() ||
1309 !FS->type_test_assume_const_vcalls().empty() ||
1310 !FS->type_checked_load_const_vcalls().empty() ||
1311 !FS->type_tests().empty())
1312 return make_error<StringError>(
1313 Args: "inconsistent LTO Unit splitting (recompile with -fsplit-lto-unit)",
1314 Args: inconvertibleErrorCode());
1315 }
1316 }
1317 return Error::success();
1318}
1319
1320Error LTO::setupPassPlugins() {
1321 // Filenames are cleared on success, so repeated calls are no-ops.
1322 if (Conf.PassPluginFilenames.empty())
1323 return Error::success();
1324
1325 // Load plugins from file. Failures leave them all unregistered.
1326 SmallVector<std::unique_ptr<PassPlugin>, 0> Loaded;
1327 for (const std::string &PluginFN : Conf.PassPluginFilenames) {
1328 Expected<PassPlugin> Plugin = PassPlugin::load(Filename: PluginFN);
1329 if (!Plugin)
1330 return Plugin.takeError();
1331 Loaded.push_back(Elt: std::make_unique<PassPlugin>(args: std::move(*Plugin)));
1332 }
1333
1334 // Register them ahead of the client loaded ones.
1335 SmallVector<PassPlugin *> FromFiles;
1336 for (std::unique_ptr<PassPlugin> &Plugin : Loaded) {
1337 FromFiles.push_back(Elt: Plugin.get());
1338 PassPlugins.push_back(Elt: std::move(Plugin));
1339 }
1340 Conf.LoadedPassPlugins.insert(position: Conf.LoadedPassPlugins.begin(),
1341 first: FromFiles.begin(), last: FromFiles.end());
1342
1343 Conf.PassPluginFilenames.clear();
1344 return Error::success();
1345}
1346
1347Error LTO::run(AddStreamFn AddStream, FileCache Cache) {
1348 // Call the base class cleanup() explicitly since run() may be invoked on a
1349 // derived LTO object.
1350 llvm::scope_exit CleanUp([this]() { LTO::cleanup(); });
1351
1352 if (Error Err = setupPassPlugins())
1353 return Err;
1354
1355 // Compute "dead" symbols, we don't want to import/export these!
1356 DenseSet<GlobalValue::GUID> GUIDPreservedSymbols;
1357 DenseMap<GlobalValue::GUID, PrevailingType> GUIDPrevailingResolutions;
1358 for (auto &Res : *GlobalResolutions) {
1359 // Normally resolution have IR name of symbol. We can do nothing here
1360 // otherwise. See comments in GlobalResolution struct for more details.
1361 if (Res.second.IRName.empty())
1362 continue;
1363
1364 GlobalValue::GUID GUID = Res.second.getGUID();
1365
1366 if (Res.second.VisibleOutsideSummary && Res.second.Prevailing)
1367 GUIDPreservedSymbols.insert(V: GUID);
1368
1369 if (Res.second.ExportDynamic)
1370 DynamicExportSymbols.insert(V: GUID);
1371
1372 GUIDPrevailingResolutions[GUID] =
1373 Res.second.Prevailing ? PrevailingType::Yes : PrevailingType::No;
1374 }
1375
1376 auto isPrevailing = [&](GlobalValue::GUID G) {
1377 auto It = GUIDPrevailingResolutions.find(Val: G);
1378 if (It == GUIDPrevailingResolutions.end())
1379 return PrevailingType::Unknown;
1380 return It->second;
1381 };
1382 computeDeadSymbolsWithConstProp(Index&: ThinLTO.CombinedIndex, GUIDPreservedSymbols,
1383 isPrevailing, ImportEnabled: Conf.OptLevel > 0);
1384
1385 // Setup output file to emit statistics.
1386 auto StatsFileOrErr = setupStatsFile(Conf.StatsFile);
1387 if (!StatsFileOrErr)
1388 return StatsFileOrErr.takeError();
1389 std::unique_ptr<ToolOutputFile> StatsFile = std::move(StatsFileOrErr.get());
1390
1391 if (Error Err = setupOptimizationRemarks())
1392 return Err;
1393
1394 // TODO: Ideally this would be controlled automatically by detecting that we
1395 // are linking with an allocator that supports these interfaces, rather than
1396 // an internal option (which would still be needed for tests, however). For
1397 // example, if the library exported a symbol like __malloc_hot_cold the linker
1398 // could recognize that and set a flag in the lto::Config.
1399 if (SupportsHotColdNew)
1400 ThinLTO.CombinedIndex.setWithSupportsHotColdNew();
1401
1402 Error Result = runRegularLTO(AddStream);
1403 if (!Result)
1404 // This will reset the GlobalResolutions optional once done with it to
1405 // reduce peak memory before importing.
1406 Result = runThinLTO(AddStream, Cache, GUIDPreservedSymbols);
1407
1408 if (StatsFile)
1409 PrintStatisticsJSON(OS&: StatsFile->os());
1410
1411 return Result;
1412}
1413
1414Error LTO::runRegularLTO(AddStreamFn AddStream) {
1415 llvm::TimeTraceScope timeScope("Run regular LTO");
1416 LLVM_DEBUG(dbgs() << "Running regular LTO\n");
1417
1418 // Finalize linking of regular LTO modules containing summaries now that
1419 // we have computed liveness information.
1420 if (!RegularLTO.ModsWithSummaries.empty()) {
1421 {
1422 llvm::TimeTraceScope timeScope("Link regular LTO");
1423 for (auto &M : RegularLTO.ModsWithSummaries)
1424 if (Error Err =
1425 linkRegularLTO(Mod: std::move(M), /*LivenessFromIndex=*/true))
1426 return Err;
1427 RegularLTO.ModsWithSummaries.clear();
1428 RegularLTO.ModsWithSummaries.shrink_to_fit();
1429 }
1430 if (Conf.PostRegularLTOLinkHook)
1431 Conf.PostRegularLTOLinkHook();
1432 }
1433
1434 // Ensure we don't have inconsistently split LTO units with type tests.
1435 // FIXME: this checks both LTO and ThinLTO. It happens to work as we take
1436 // this path both cases but eventually this should be split into two and
1437 // do the ThinLTO checks in `runThinLTO`.
1438 if (Error Err = checkPartiallySplit())
1439 return Err;
1440
1441 // Make sure commons have the right size/alignment: we kept the largest from
1442 // all the prevailing when adding the inputs, and we apply it here.
1443 const DataLayout &DL = RegularLTO.CombinedModule->getDataLayout();
1444 for (auto &I : RegularLTO.Commons) {
1445 if (!I.second.Prevailing)
1446 // Don't do anything if no instance of this common was prevailing.
1447 continue;
1448 GlobalVariable *OldGV = RegularLTO.CombinedModule->getNamedGlobal(Name: I.first);
1449 if (OldGV && OldGV->getGlobalSize(DL) == I.second.Size) {
1450 // Don't create a new global if the type is already correct, just make
1451 // sure the alignment is correct.
1452 OldGV->setAlignment(I.second.Alignment);
1453 continue;
1454 }
1455 ArrayType *Ty =
1456 ArrayType::get(ElementType: Type::getInt8Ty(C&: RegularLTO.Ctx), NumElements: I.second.Size);
1457 auto *GV = new GlobalVariable(*RegularLTO.CombinedModule, Ty, false,
1458 GlobalValue::CommonLinkage,
1459 ConstantAggregateZero::get(Ty), "");
1460 GV->setAlignment(I.second.Alignment);
1461 if (OldGV) {
1462 OldGV->replaceAllUsesWith(V: GV);
1463 GV->takeName(V: OldGV);
1464 OldGV->eraseFromParent();
1465 } else {
1466 GV->setName(I.first);
1467 }
1468 }
1469
1470 bool WholeProgramVisibilityEnabledInLTO =
1471 Conf.HasWholeProgramVisibility &&
1472 // If validation is enabled, upgrade visibility only when all vtables
1473 // have typeinfos.
1474 (!Conf.ValidateAllVtablesHaveTypeInfos || Conf.AllVtablesHaveTypeInfos);
1475
1476 // This returns true when the name is local or not defined. Locals are
1477 // expected to be handled separately.
1478 auto IsVisibleToRegularObj = [&](StringRef name) {
1479 auto It = GlobalResolutions->find(Val: name);
1480 return (It == GlobalResolutions->end() ||
1481 It->second.VisibleOutsideSummary || !It->second.Prevailing);
1482 };
1483
1484 // If allowed, upgrade public vcall visibility metadata to linkage unit
1485 // visibility before whole program devirtualization in the optimizer.
1486 updateVCallVisibilityInModule(
1487 M&: *RegularLTO.CombinedModule, WholeProgramVisibilityEnabledInLTO,
1488 DynamicExportSymbols, ValidateAllVtablesHaveTypeInfos: Conf.ValidateAllVtablesHaveTypeInfos,
1489 IsVisibleToRegularObj);
1490 updatePublicTypeTestCalls(M&: *RegularLTO.CombinedModule,
1491 WholeProgramVisibilityEnabledInLTO);
1492
1493 if (Conf.PreOptModuleHook &&
1494 !Conf.PreOptModuleHook(0, *RegularLTO.CombinedModule))
1495 return Error::success();
1496
1497 if (!Conf.CodeGenOnly) {
1498 for (const auto &R : *GlobalResolutions) {
1499 GlobalValue *GV =
1500 RegularLTO.CombinedModule->getNamedValue(Name: R.second.IRName);
1501 if (!R.second.isPrevailingIRSymbol())
1502 continue;
1503 if (R.second.Partition != 0 &&
1504 R.second.Partition != GlobalResolution::External)
1505 continue;
1506
1507 // Ignore symbols defined in other partitions.
1508 // Also skip declarations, which are not allowed to have internal linkage.
1509 if (!GV || GV->hasLocalLinkage() || GV->isDeclaration())
1510 continue;
1511
1512 // Symbols that are marked DLLImport or DLLExport should not be
1513 // internalized, as they are either externally visible or referencing
1514 // external symbols. Symbols that have AvailableExternally or Appending
1515 // linkage might be used by future passes and should be kept as is.
1516 // These linkages are seen in Unified regular LTO, because the process
1517 // of creating split LTO units introduces symbols with that linkage into
1518 // one of the created modules. Normally, only the ThinLTO backend would
1519 // compile this module, but Unified Regular LTO processes both
1520 // modules created by the splitting process as regular LTO modules.
1521 if ((LTOMode == LTOKind::LTOK_UnifiedRegular) &&
1522 ((GV->getDLLStorageClass() != GlobalValue::DefaultStorageClass) ||
1523 GV->hasAvailableExternallyLinkage() || GV->hasAppendingLinkage()))
1524 continue;
1525
1526 GV->setUnnamedAddr(R.second.UnnamedAddr ? GlobalValue::UnnamedAddr::Global
1527 : GlobalValue::UnnamedAddr::None);
1528 if (EnableLTOInternalization && R.second.Partition == 0)
1529 GV->setLinkage(GlobalValue::InternalLinkage);
1530 }
1531
1532 if (Conf.PostInternalizeModuleHook &&
1533 !Conf.PostInternalizeModuleHook(0, *RegularLTO.CombinedModule))
1534 return Error::success();
1535 }
1536
1537 if (!RegularLTO.EmptyCombinedModule || Conf.AlwaysEmitRegularLTOObj) {
1538 if (Error Err = backend(
1539 C: Conf, AddStream, ParallelCodeGenParallelismLevel: RegularLTO.ParallelCodeGenParallelismLevel,
1540 M&: *RegularLTO.CombinedModule, CombinedIndex&: ThinLTO.CombinedIndex, BitcodeLibFuncs))
1541 return Err;
1542 }
1543
1544 return Error::success();
1545}
1546
1547SmallVector<const char *> LTO::getRuntimeLibcallSymbols(const Triple &TT) {
1548 RTLIB::RuntimeLibcallsInfo Libcalls(TT);
1549 SmallVector<const char *> LibcallSymbols;
1550 LibcallSymbols.reserve(N: Libcalls.getNumAvailableLibcallImpls());
1551
1552 for (RTLIB::LibcallImpl Impl : RTLIB::libcall_impls()) {
1553 if (Libcalls.isAvailable(Impl))
1554 LibcallSymbols.push_back(Elt: Libcalls.getLibcallImplName(CallImpl: Impl).data());
1555 }
1556
1557 return LibcallSymbols;
1558}
1559
1560SmallVector<StringRef> LTO::getLibFuncSymbols(const Triple &TT,
1561 StringSaver &Saver) {
1562 auto TLII = std::make_unique<TargetLibraryInfoImpl>(args: TT);
1563 TargetLibraryInfo TLI(*TLII);
1564 SmallVector<StringRef> LibFuncSymbols;
1565 LibFuncSymbols.reserve(N: LibFunc::NumLibFuncs);
1566 for (unsigned I = LibFunc::Begin_LibFunc; I != LibFunc::End_LibFunc; ++I) {
1567 LibFunc F = static_cast<LibFunc>(I);
1568 if (TLI.has(F))
1569 LibFuncSymbols.push_back(Elt: Saver.save(S: TLI.getName(F)).data());
1570 }
1571 return LibFuncSymbols;
1572}
1573
1574Error ThinBackendProc::emitFiles(
1575 const FunctionImporter::ImportMapTy &ImportList, unsigned Task,
1576 llvm::StringRef ModulePath, const std::string &NewModulePath) const {
1577 return emitFiles(ImportList, Task, ModulePath, NewModulePath,
1578 SummaryPath: NewModulePath + ".thinlto.bc");
1579}
1580
1581Error ThinBackendProc::emitFiles(
1582 const FunctionImporter::ImportMapTy &ImportList, unsigned Task,
1583 llvm::StringRef ModulePath, const std::string &NewModulePath,
1584 StringRef SummaryPath) const {
1585 ModuleToSummariesForIndexTy ModuleToSummariesForIndex;
1586 GVSummaryPtrSet DeclarationSummaries;
1587
1588 std::error_code EC;
1589 gatherImportedSummariesForModule(ModulePath, ModuleToDefinedGVSummaries,
1590 ImportList, ModuleToSummariesForIndex,
1591 DecSummaries&: DeclarationSummaries);
1592 // Resolve the output stream (either file-backed or callback-provided) for the
1593 // index file.
1594 std::unique_ptr<raw_pwrite_stream> OS;
1595 if (Conf.GetSummaryIndexOutputStream) {
1596 OS = Conf.GetSummaryIndexOutputStream(Task);
1597 assert(OS && "GetSummaryIndexOutputStream returned null");
1598 } else {
1599 auto FileOS = std::make_unique<raw_fd_ostream>(args&: SummaryPath, args&: EC,
1600 args: sys::fs::OpenFlags::OF_None);
1601 if (EC)
1602 return createFileError(F: "cannot open " + Twine(SummaryPath), EC);
1603 OS = std::move(FileOS);
1604 }
1605
1606 writeIndexToFile(Index: CombinedIndex, Out&: *OS, ModuleToSummariesForIndex: &ModuleToSummariesForIndex,
1607 DecSummaries: &DeclarationSummaries);
1608
1609 // Emit imports files if requested, using callback if provided.
1610 if (Conf.GetImportsListOutputArray) {
1611 std::vector<std::string> &ImportsListRef =
1612 Conf.GetImportsListOutputArray(Task);
1613 processImportsFiles(
1614 ModulePath, ModuleToSummariesForIndex,
1615 F: [&](StringRef M) { ImportsListRef.push_back(x: M.str()); });
1616 } else if (ShouldEmitImportsFiles) {
1617 if (Error E = EmitImportsFiles(ModulePath, OutputFilename: NewModulePath + ".imports",
1618 ModuleToSummariesForIndex))
1619 return E;
1620 }
1621 return Error::success();
1622}
1623
1624namespace {
1625/// Base class for ThinLTO backends that perform code generation and insert the
1626/// generated files back into the link.
1627class CGThinBackend : public ThinBackendProc {
1628protected:
1629 DenseSet<GlobalValue::GUID> CfiFunctionDefs;
1630 DenseSet<GlobalValue::GUID> CfiFunctionDecls;
1631 bool ShouldEmitIndexFiles;
1632
1633public:
1634 CGThinBackend(
1635 const Config &Conf, ModuleSummaryIndex &CombinedIndex,
1636 const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1637 lto::IndexWriteCallback OnWrite, bool ShouldEmitIndexFiles,
1638 bool ShouldEmitImportsFiles, ThreadPoolStrategy ThinLTOParallelism)
1639 : ThinBackendProc(Conf, CombinedIndex, ModuleToDefinedGVSummaries,
1640 OnWrite, ShouldEmitImportsFiles, ThinLTOParallelism),
1641 ShouldEmitIndexFiles(ShouldEmitIndexFiles) {
1642 auto &Defs = CombinedIndex.cfiFunctionDefs();
1643 CfiFunctionDefs.insert_range(R: Defs.getExportedThinLTOGUIDs());
1644 auto &Decls = CombinedIndex.cfiFunctionDecls();
1645 CfiFunctionDecls.insert_range(R: Decls.getExportedThinLTOGUIDs());
1646 }
1647};
1648
1649/// This backend performs code generation by scheduling a job to run on
1650/// an in-process thread when invoked for each task.
1651class InProcessThinBackend : public CGThinBackend {
1652protected:
1653 // Callback used to add generated native object files to the link by code
1654 // generating directly into the returned output stream.
1655 AddStreamFn AddStream;
1656 FileCache Cache;
1657 ArrayRef<StringRef> BitcodeLibFuncs;
1658
1659public:
1660 InProcessThinBackend(
1661 const Config &Conf, ModuleSummaryIndex &CombinedIndex,
1662 ThreadPoolStrategy ThinLTOParallelism,
1663 const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1664 AddStreamFn AddStream, FileCache Cache, lto::IndexWriteCallback OnWrite,
1665 bool ShouldEmitIndexFiles, bool ShouldEmitImportsFiles,
1666 ArrayRef<StringRef> BitcodeLibFuncs)
1667 : CGThinBackend(Conf, CombinedIndex, ModuleToDefinedGVSummaries, OnWrite,
1668 ShouldEmitIndexFiles, ShouldEmitImportsFiles,
1669 ThinLTOParallelism),
1670 AddStream(std::move(AddStream)), Cache(std::move(Cache)),
1671 BitcodeLibFuncs(BitcodeLibFuncs) {}
1672
1673 virtual Error runThinLTOBackendThread(
1674 AddStreamFn AddStream, FileCache Cache, unsigned Task, BitcodeModule BM,
1675 ModuleSummaryIndex &CombinedIndex,
1676 const FunctionImporter::ImportMapTy &ImportList,
1677 const FunctionImporter::ExportSetTy &ExportList,
1678 const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
1679 const GVSummaryMapTy &DefinedGlobals,
1680 MapVector<StringRef, BitcodeModule> &ModuleMap) {
1681 auto ModuleID = BM.getModuleIdentifier();
1682 llvm::TimeTraceScope timeScope("Run ThinLTO backend thread (in-process)",
1683 ModuleID);
1684 auto RunThinBackend = [&](AddStreamFn AddStream) {
1685 LTOLLVMContext BackendContext(Conf);
1686 Expected<std::unique_ptr<Module>> MOrErr = BM.parseModule(Context&: BackendContext);
1687 if (!MOrErr)
1688 return MOrErr.takeError();
1689
1690 return thinBackend(C: Conf, Task, AddStream, M&: **MOrErr, CombinedIndex,
1691 ImportList, DefinedGlobals, ModuleMap: &ModuleMap,
1692 CodeGenOnly: Conf.CodeGenOnly, BitcodeLibFuncs);
1693 };
1694 if (ShouldEmitIndexFiles) {
1695 if (auto E = emitFiles(ImportList, Task, ModulePath: ModuleID, NewModulePath: ModuleID.str()))
1696 return E;
1697 }
1698
1699 if (!Cache.isValid() || !CombinedIndex.modulePaths().count(Key: ModuleID) ||
1700 all_of(Range: CombinedIndex.getModuleHash(ModPath: ModuleID),
1701 P: [](uint32_t V) { return V == 0; }))
1702 // Cache disabled or no entry for this module in the combined index or
1703 // no module hash.
1704 return RunThinBackend(AddStream);
1705
1706 // The module may be cached, this helps handling it.
1707 std::string Key = computeLTOCacheKey(
1708 Conf, Index: CombinedIndex, ModuleID, ImportList, ExportList, ResolvedODR,
1709 DefinedGlobals, CfiFunctionDefs, CfiFunctionDecls);
1710 Expected<AddStreamFn> CacheAddStreamOrErr = Cache(Task, Key, ModuleID);
1711 if (Error Err = CacheAddStreamOrErr.takeError())
1712 return Err;
1713 AddStreamFn &CacheAddStream = *CacheAddStreamOrErr;
1714 if (CacheAddStream)
1715 return RunThinBackend(CacheAddStream);
1716
1717 return Error::success();
1718 }
1719
1720 Error start(
1721 unsigned Task, BitcodeModule BM,
1722 const FunctionImporter::ImportMapTy &ImportList,
1723 const FunctionImporter::ExportSetTy &ExportList,
1724 const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
1725 MapVector<StringRef, BitcodeModule> &ModuleMap) override {
1726 StringRef ModulePath = BM.getModuleIdentifier();
1727 assert(ModuleToDefinedGVSummaries.count(ModulePath));
1728 const GVSummaryMapTy &DefinedGlobals =
1729 ModuleToDefinedGVSummaries.find(Val: ModulePath)->second;
1730 BackendThreadPool.async(
1731 F: [=](BitcodeModule BM, ModuleSummaryIndex &CombinedIndex,
1732 const FunctionImporter::ImportMapTy &ImportList,
1733 const FunctionImporter::ExportSetTy &ExportList,
1734 const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>
1735 &ResolvedODR,
1736 const GVSummaryMapTy &DefinedGlobals,
1737 MapVector<StringRef, BitcodeModule> &ModuleMap) {
1738 if (LLVM_ENABLE_THREADS && Conf.TimeTraceEnabled)
1739 timeTraceProfilerInitialize(TimeTraceGranularity: Conf.TimeTraceGranularity,
1740 ProcName: "thin backend");
1741 Error E = runThinLTOBackendThread(
1742 AddStream, Cache, Task, BM, CombinedIndex, ImportList, ExportList,
1743 ResolvedODR, DefinedGlobals, ModuleMap);
1744 if (E) {
1745 std::unique_lock<std::mutex> L(ErrMu);
1746 if (Err)
1747 Err = joinErrors(E1: std::move(*Err), E2: std::move(E));
1748 else
1749 Err = std::move(E);
1750 }
1751 if (LLVM_ENABLE_THREADS && Conf.TimeTraceEnabled)
1752 timeTraceProfilerFinishThread();
1753 },
1754 ArgList&: BM, ArgList: std::ref(t&: CombinedIndex), ArgList: std::ref(t: ImportList), ArgList: std::ref(t: ExportList),
1755 ArgList: std::ref(t: ResolvedODR), ArgList: std::ref(t: DefinedGlobals), ArgList: std::ref(t&: ModuleMap));
1756
1757 if (OnWrite)
1758 OnWrite(std::string(ModulePath));
1759 return Error::success();
1760 }
1761};
1762
1763/// This backend is utilized in the first round of a two-codegen round process.
1764/// It first saves optimized bitcode files to disk before the codegen process
1765/// begins. After codegen, it stores the resulting object files in a scratch
1766/// buffer. Note the codegen data stored in the scratch buffer will be extracted
1767/// and merged in the subsequent step.
1768class FirstRoundThinBackend : public InProcessThinBackend {
1769 AddStreamFn IRAddStream;
1770 FileCache IRCache;
1771
1772public:
1773 FirstRoundThinBackend(
1774 const Config &Conf, ModuleSummaryIndex &CombinedIndex,
1775 ThreadPoolStrategy ThinLTOParallelism,
1776 const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1777 AddStreamFn CGAddStream, FileCache CGCache,
1778 ArrayRef<StringRef> BitcodeLibFuncs, AddStreamFn IRAddStream,
1779 FileCache IRCache)
1780 : InProcessThinBackend(Conf, CombinedIndex, ThinLTOParallelism,
1781 ModuleToDefinedGVSummaries, std::move(CGAddStream),
1782 std::move(CGCache), /*OnWrite=*/nullptr,
1783 /*ShouldEmitIndexFiles=*/false,
1784 /*ShouldEmitImportsFiles=*/false, BitcodeLibFuncs),
1785 IRAddStream(std::move(IRAddStream)), IRCache(std::move(IRCache)) {}
1786
1787 Error runThinLTOBackendThread(
1788 AddStreamFn CGAddStream, FileCache CGCache, unsigned Task,
1789 BitcodeModule BM, ModuleSummaryIndex &CombinedIndex,
1790 const FunctionImporter::ImportMapTy &ImportList,
1791 const FunctionImporter::ExportSetTy &ExportList,
1792 const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
1793 const GVSummaryMapTy &DefinedGlobals,
1794 MapVector<StringRef, BitcodeModule> &ModuleMap) override {
1795 auto ModuleID = BM.getModuleIdentifier();
1796 llvm::TimeTraceScope timeScope("Run ThinLTO backend thread (first round)",
1797 ModuleID);
1798 auto RunThinBackend = [&](AddStreamFn CGAddStream,
1799 AddStreamFn IRAddStream) {
1800 LTOLLVMContext BackendContext(Conf);
1801 Expected<std::unique_ptr<Module>> MOrErr = BM.parseModule(Context&: BackendContext);
1802 if (!MOrErr)
1803 return MOrErr.takeError();
1804
1805 return thinBackend(C: Conf, Task, AddStream: CGAddStream, M&: **MOrErr, CombinedIndex,
1806 ImportList, DefinedGlobals, ModuleMap: &ModuleMap,
1807 CodeGenOnly: Conf.CodeGenOnly, BitcodeLibFuncs, IRAddStream);
1808 };
1809 // Like InProcessThinBackend, we produce index files as needed for
1810 // FirstRoundThinBackend. However, these files are not generated for
1811 // SecondRoundThinBackend.
1812 if (ShouldEmitIndexFiles) {
1813 if (auto E = emitFiles(ImportList, Task, ModulePath: ModuleID, NewModulePath: ModuleID.str()))
1814 return E;
1815 }
1816
1817 assert((CGCache.isValid() == IRCache.isValid()) &&
1818 "Both caches for CG and IR should have matching availability");
1819 if (!CGCache.isValid() || !CombinedIndex.modulePaths().count(Key: ModuleID) ||
1820 all_of(Range: CombinedIndex.getModuleHash(ModPath: ModuleID),
1821 P: [](uint32_t V) { return V == 0; }))
1822 // Cache disabled or no entry for this module in the combined index or
1823 // no module hash.
1824 return RunThinBackend(CGAddStream, IRAddStream);
1825
1826 // Get CGKey for caching object in CGCache.
1827 std::string CGKey = computeLTOCacheKey(
1828 Conf, Index: CombinedIndex, ModuleID, ImportList, ExportList, ResolvedODR,
1829 DefinedGlobals, CfiFunctionDefs, CfiFunctionDecls);
1830 Expected<AddStreamFn> CacheCGAddStreamOrErr =
1831 CGCache(Task, CGKey, ModuleID);
1832 if (Error Err = CacheCGAddStreamOrErr.takeError())
1833 return Err;
1834 AddStreamFn &CacheCGAddStream = *CacheCGAddStreamOrErr;
1835
1836 // Get IRKey for caching (optimized) IR in IRCache with an extra ID.
1837 std::string IRKey = recomputeLTOCacheKey(Key: CGKey, /*ExtraID=*/"IR");
1838 Expected<AddStreamFn> CacheIRAddStreamOrErr =
1839 IRCache(Task, IRKey, ModuleID);
1840 if (Error Err = CacheIRAddStreamOrErr.takeError())
1841 return Err;
1842 AddStreamFn &CacheIRAddStream = *CacheIRAddStreamOrErr;
1843
1844 // Ideally, both CG and IR caching should be synchronized. However, in
1845 // practice, their availability may differ due to different expiration
1846 // times. Therefore, if either cache is missing, the backend process is
1847 // triggered.
1848 if (CacheCGAddStream || CacheIRAddStream) {
1849 LLVM_DEBUG(dbgs() << "[FirstRound] Cache Miss for "
1850 << BM.getModuleIdentifier() << "\n");
1851 return RunThinBackend(CacheCGAddStream ? CacheCGAddStream : CGAddStream,
1852 CacheIRAddStream ? CacheIRAddStream : IRAddStream);
1853 }
1854
1855 return Error::success();
1856 }
1857};
1858
1859/// This backend operates in the second round of a two-codegen round process.
1860/// It starts by reading the optimized bitcode files that were saved during the
1861/// first round. The backend then executes the codegen only to further optimize
1862/// the code, utilizing the codegen data merged from the first round. Finally,
1863/// it writes the resulting object files as usual.
1864class SecondRoundThinBackend : public InProcessThinBackend {
1865 std::unique_ptr<SmallVector<StringRef>> IRFiles;
1866 stable_hash CombinedCGDataHash;
1867
1868public:
1869 SecondRoundThinBackend(
1870 const Config &Conf, ModuleSummaryIndex &CombinedIndex,
1871 ThreadPoolStrategy ThinLTOParallelism,
1872 const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1873 AddStreamFn AddStream, FileCache Cache,
1874 ArrayRef<StringRef> BitcodeLibFuncs,
1875 std::unique_ptr<SmallVector<StringRef>> IRFiles,
1876 stable_hash CombinedCGDataHash)
1877 : InProcessThinBackend(Conf, CombinedIndex, ThinLTOParallelism,
1878 ModuleToDefinedGVSummaries, std::move(AddStream),
1879 std::move(Cache),
1880 /*OnWrite=*/nullptr,
1881 /*ShouldEmitIndexFiles=*/false,
1882 /*ShouldEmitImportsFiles=*/false, BitcodeLibFuncs),
1883 IRFiles(std::move(IRFiles)), CombinedCGDataHash(CombinedCGDataHash) {}
1884
1885 Error runThinLTOBackendThread(
1886 AddStreamFn AddStream, FileCache Cache, unsigned Task, BitcodeModule BM,
1887 ModuleSummaryIndex &CombinedIndex,
1888 const FunctionImporter::ImportMapTy &ImportList,
1889 const FunctionImporter::ExportSetTy &ExportList,
1890 const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
1891 const GVSummaryMapTy &DefinedGlobals,
1892 MapVector<StringRef, BitcodeModule> &ModuleMap) override {
1893 auto ModuleID = BM.getModuleIdentifier();
1894 llvm::TimeTraceScope timeScope("Run ThinLTO backend thread (second round)",
1895 ModuleID);
1896 auto RunThinBackend = [&](AddStreamFn AddStream) {
1897 LTOLLVMContext BackendContext(Conf);
1898 std::unique_ptr<Module> LoadedModule =
1899 cgdata::loadModuleForTwoRounds(OrigModule&: BM, Task, Context&: BackendContext, IRFiles: *IRFiles);
1900
1901 return thinBackend(C: Conf, Task, AddStream, M&: *LoadedModule, CombinedIndex,
1902 ImportList, DefinedGlobals, ModuleMap: &ModuleMap,
1903 /*CodeGenOnly=*/true, BitcodeLibFuncs);
1904 };
1905 if (!Cache.isValid() || !CombinedIndex.modulePaths().count(Key: ModuleID) ||
1906 all_of(Range: CombinedIndex.getModuleHash(ModPath: ModuleID),
1907 P: [](uint32_t V) { return V == 0; }))
1908 // Cache disabled or no entry for this module in the combined index or
1909 // no module hash.
1910 return RunThinBackend(AddStream);
1911
1912 // Get Key for caching the final object file in Cache with the combined
1913 // CGData hash.
1914 std::string Key = computeLTOCacheKey(
1915 Conf, Index: CombinedIndex, ModuleID, ImportList, ExportList, ResolvedODR,
1916 DefinedGlobals, CfiFunctionDefs, CfiFunctionDecls);
1917 Key = recomputeLTOCacheKey(Key,
1918 /*ExtraID=*/std::to_string(val: CombinedCGDataHash));
1919 Expected<AddStreamFn> CacheAddStreamOrErr = Cache(Task, Key, ModuleID);
1920 if (Error Err = CacheAddStreamOrErr.takeError())
1921 return Err;
1922 AddStreamFn &CacheAddStream = *CacheAddStreamOrErr;
1923
1924 if (CacheAddStream) {
1925 LLVM_DEBUG(dbgs() << "[SecondRound] Cache Miss for "
1926 << BM.getModuleIdentifier() << "\n");
1927 return RunThinBackend(CacheAddStream);
1928 }
1929
1930 return Error::success();
1931 }
1932};
1933} // end anonymous namespace
1934
1935ThinBackend lto::createInProcessThinBackend(ThreadPoolStrategy Parallelism,
1936 lto::IndexWriteCallback OnWrite,
1937 bool ShouldEmitIndexFiles,
1938 bool ShouldEmitImportsFiles) {
1939 auto Func =
1940 [=](const Config &Conf, ModuleSummaryIndex &CombinedIndex,
1941 const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1942 AddStreamFn AddStream, FileCache Cache,
1943 ArrayRef<StringRef> BitcodeLibFuncs) {
1944 return std::make_unique<InProcessThinBackend>(
1945 args: Conf, args&: CombinedIndex, args: Parallelism, args: ModuleToDefinedGVSummaries,
1946 args&: AddStream, args&: Cache, args: OnWrite, args: ShouldEmitIndexFiles,
1947 args: ShouldEmitImportsFiles, args&: BitcodeLibFuncs);
1948 };
1949 return ThinBackend(Func, Parallelism);
1950}
1951
1952StringLiteral lto::getThinLTODefaultCPU(const Triple &TheTriple) {
1953 if (!TheTriple.isOSDarwin())
1954 return "";
1955 if (TheTriple.getArch() == Triple::x86_64)
1956 return "core2";
1957 if (TheTriple.getArch() == Triple::x86)
1958 return "yonah";
1959 if (TheTriple.isArm64e())
1960 return "apple-a12";
1961 if (TheTriple.getArch() == Triple::aarch64 ||
1962 TheTriple.getArch() == Triple::aarch64_32)
1963 return "cyclone";
1964 return "";
1965}
1966
1967// Given the original \p Path to an output file, replace any path
1968// prefix matching \p OldPrefix with \p NewPrefix. Also, create the
1969// resulting directory if it does not yet exist.
1970std::string lto::getThinLTOOutputFile(StringRef Path, StringRef OldPrefix,
1971 StringRef NewPrefix) {
1972 if (OldPrefix.empty() && NewPrefix.empty())
1973 return std::string(Path);
1974 SmallString<128> NewPath(Path);
1975 llvm::sys::path::replace_path_prefix(Path&: NewPath, OldPrefix, NewPrefix);
1976 StringRef ParentPath = llvm::sys::path::parent_path(path: NewPath.str());
1977 if (!ParentPath.empty()) {
1978 // Make sure the new directory exists, creating it if necessary.
1979 if (std::error_code EC = llvm::sys::fs::create_directories(path: ParentPath))
1980 llvm::errs() << "warning: could not create directory '" << ParentPath
1981 << "': " << EC.message() << '\n';
1982 }
1983 return std::string(NewPath);
1984}
1985
1986namespace {
1987class WriteIndexesThinBackend : public ThinBackendProc {
1988 std::string OldPrefix, NewPrefix, NativeObjectPrefix;
1989 raw_fd_ostream *LinkedObjectsFile;
1990 DenseSet<GlobalValue::GUID> CfiFunctionDefs;
1991 DenseSet<GlobalValue::GUID> CfiFunctionDecls;
1992
1993public:
1994 WriteIndexesThinBackend(
1995 const Config &Conf, ModuleSummaryIndex &CombinedIndex,
1996 ThreadPoolStrategy ThinLTOParallelism,
1997 const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1998 std::string OldPrefix, std::string NewPrefix,
1999 std::string NativeObjectPrefix, bool ShouldEmitImportsFiles,
2000 raw_fd_ostream *LinkedObjectsFile, lto::IndexWriteCallback OnWrite)
2001 : ThinBackendProc(Conf, CombinedIndex, ModuleToDefinedGVSummaries,
2002 OnWrite, ShouldEmitImportsFiles, ThinLTOParallelism),
2003 OldPrefix(OldPrefix), NewPrefix(NewPrefix),
2004 NativeObjectPrefix(NativeObjectPrefix),
2005 LinkedObjectsFile(LinkedObjectsFile) {
2006 auto Defs = CombinedIndex.cfiFunctionDefs().getExportedThinLTOGUIDs();
2007 CfiFunctionDefs.insert(I: Defs.begin(), E: Defs.end());
2008 auto Decls = CombinedIndex.cfiFunctionDecls().getExportedThinLTOGUIDs();
2009 CfiFunctionDecls.insert(I: Decls.begin(), E: Decls.end());
2010 }
2011
2012 Error start(
2013 unsigned Task, BitcodeModule BM,
2014 const FunctionImporter::ImportMapTy &ImportList,
2015 const FunctionImporter::ExportSetTy &ExportList,
2016 const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
2017 MapVector<StringRef, BitcodeModule> &ModuleMap) override {
2018 StringRef ModulePath = BM.getModuleIdentifier();
2019
2020 // The contents of this file may be used as input to a native link, and must
2021 // therefore contain the processed modules in a determinstic order that
2022 // match the order they are provided on the command line. For that reason,
2023 // we cannot include this in the asynchronously executed lambda below.
2024 if (LinkedObjectsFile) {
2025 std::string ObjectPrefix =
2026 NativeObjectPrefix.empty() ? NewPrefix : NativeObjectPrefix;
2027 std::string LinkedObjectsFilePath =
2028 getThinLTOOutputFile(Path: ModulePath, OldPrefix, NewPrefix: ObjectPrefix);
2029 *LinkedObjectsFile << LinkedObjectsFilePath << '\n';
2030 }
2031
2032 BackendThreadPool.async(
2033 F: [this](unsigned Task, const StringRef ModulePath,
2034 const FunctionImporter::ImportMapTy &ImportList,
2035 const FunctionImporter::ExportSetTy &ExportList,
2036 const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>
2037 &ResolvedODR,
2038 const std::string &OldPrefix, const std::string &NewPrefix) {
2039 std::string NewModulePath =
2040 getThinLTOOutputFile(Path: ModulePath, OldPrefix, NewPrefix);
2041 auto E = emitFiles(ImportList, Task, ModulePath, NewModulePath);
2042 if (E) {
2043 std::unique_lock<std::mutex> L(ErrMu);
2044 if (Err)
2045 Err = joinErrors(E1: std::move(*Err), E2: std::move(E));
2046 else
2047 Err = std::move(E);
2048 }
2049 assert(ModuleToDefinedGVSummaries.count(ModulePath));
2050 const GVSummaryMapTy &DefinedGlobals =
2051 ModuleToDefinedGVSummaries.find(Val: ModulePath)->second;
2052
2053 // DTLTO needs the per-module LTO cache key to probe the cache.
2054 if (Conf.GetCacheKeyOutputString) {
2055 std::string &CacheKey = Conf.GetCacheKeyOutputString(Task);
2056 CacheKey = computeLTOCacheKey(
2057 Conf, Index: CombinedIndex, ModuleID: ModulePath, ImportList, ExportList,
2058 ResolvedODR, DefinedGlobals, CfiFunctionDefs, CfiFunctionDecls);
2059 }
2060 },
2061 ArgList&: Task, ArgList&: ModulePath, ArgList: ImportList, ArgList: ExportList, ArgList: ResolvedODR, ArgList&: OldPrefix,
2062 ArgList&: NewPrefix);
2063
2064 if (OnWrite)
2065 OnWrite(std::string(ModulePath));
2066 return Error::success();
2067 }
2068
2069 bool isSensitiveToInputOrder() override {
2070 // The order which modules are written to LinkedObjectsFile should be
2071 // deterministic and match the order they are passed on the command line.
2072 return true;
2073 }
2074};
2075} // end anonymous namespace
2076
2077ThinBackend lto::createWriteIndexesThinBackend(
2078 ThreadPoolStrategy Parallelism, std::string OldPrefix,
2079 std::string NewPrefix, std::string NativeObjectPrefix,
2080 bool ShouldEmitImportsFiles, raw_fd_ostream *LinkedObjectsFile,
2081 IndexWriteCallback OnWrite) {
2082 auto Func =
2083 [=](const Config &Conf, ModuleSummaryIndex &CombinedIndex,
2084 const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
2085 AddStreamFn AddStream, FileCache Cache,
2086 ArrayRef<StringRef> BitcodeLibFuncs) {
2087 return std::make_unique<WriteIndexesThinBackend>(
2088 args: Conf, args&: CombinedIndex, args: Parallelism, args: ModuleToDefinedGVSummaries,
2089 args: OldPrefix, args: NewPrefix, args: NativeObjectPrefix, args: ShouldEmitImportsFiles,
2090 args: LinkedObjectsFile, args: OnWrite);
2091 };
2092 return ThinBackend(Func, Parallelism);
2093}
2094
2095Error LTO::runThinLTO(AddStreamFn AddStream, FileCache Cache,
2096 const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols) {
2097 llvm::TimeTraceScope timeScope("Run ThinLTO");
2098 LLVM_DEBUG(dbgs() << "Running ThinLTO\n");
2099 ThinLTO.CombinedIndex.releaseTemporaryMemory();
2100 timeTraceProfilerBegin(Name: "ThinLink", Detail: StringRef(""));
2101 llvm::scope_exit TimeTraceScopeExit([]() {
2102 if (llvm::timeTraceProfilerEnabled())
2103 llvm::timeTraceProfilerEnd();
2104 });
2105 if (ThinLTO.ModuleMap.empty())
2106 return Error::success();
2107
2108 if (ThinLTO.ModulesToCompile && ThinLTO.ModulesToCompile->empty()) {
2109 llvm::errs() << "warning: [ThinLTO] No module compiled\n";
2110 return Error::success();
2111 }
2112
2113 if (Conf.CombinedIndexHook &&
2114 !Conf.CombinedIndexHook(ThinLTO.CombinedIndex, GUIDPreservedSymbols))
2115 return Error::success();
2116
2117 // Collect for each module the list of function it defines (GUID ->
2118 // Summary).
2119 DenseMap<StringRef, GVSummaryMapTy> ModuleToDefinedGVSummaries(
2120 ThinLTO.ModuleMap.size());
2121 ThinLTO.CombinedIndex.collectDefinedGVSummariesPerModule(
2122 ModuleToDefinedGVSummaries);
2123 // Create entries for any modules that didn't have any GV summaries
2124 // (either they didn't have any GVs to start with, or we suppressed
2125 // generation of the summaries because they e.g. had inline assembly
2126 // uses that couldn't be promoted/renamed on export). This is so
2127 // InProcessThinBackend::start can still launch a backend thread, which
2128 // is passed the map of summaries for the module, without any special
2129 // handling for this case.
2130 for (auto &Mod : ThinLTO.ModuleMap)
2131 if (!ModuleToDefinedGVSummaries.count(Val: Mod.first))
2132 ModuleToDefinedGVSummaries.try_emplace(Key: Mod.first);
2133
2134 FunctionImporter::ImportListsTy ImportLists(ThinLTO.ModuleMap.size());
2135 DenseMap<StringRef, FunctionImporter::ExportSetTy> ExportLists(
2136 ThinLTO.ModuleMap.size());
2137 StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>> ResolvedODR;
2138
2139 if (DumpThinCGSCCs)
2140 ThinLTO.CombinedIndex.dumpSCCs(OS&: outs());
2141
2142 std::set<GlobalValue::GUID> ExportedGUIDs;
2143
2144 bool WholeProgramVisibilityEnabledInLTO =
2145 Conf.HasWholeProgramVisibility &&
2146 // If validation is enabled, upgrade visibility only when all vtables
2147 // have typeinfos.
2148 (!Conf.ValidateAllVtablesHaveTypeInfos || Conf.AllVtablesHaveTypeInfos);
2149 if (hasWholeProgramVisibility(WholeProgramVisibilityEnabledInLTO))
2150 ThinLTO.CombinedIndex.setWithWholeProgramVisibility();
2151
2152 // If we're validating, get the vtable symbols that should not be
2153 // upgraded because they correspond to typeIDs outside of index-based
2154 // WPD info.
2155 DenseSet<GlobalValue::GUID> VisibleToRegularObjSymbols;
2156 if (WholeProgramVisibilityEnabledInLTO &&
2157 Conf.ValidateAllVtablesHaveTypeInfos) {
2158 // This returns true when the name is local or not defined. Locals are
2159 // expected to be handled separately.
2160 auto IsVisibleToRegularObj = [&](StringRef name) {
2161 auto It = GlobalResolutions->find(Val: name);
2162 return (It == GlobalResolutions->end() ||
2163 It->second.VisibleOutsideSummary || !It->second.Prevailing);
2164 };
2165
2166 getVisibleToRegularObjVtableGUIDs(Index&: ThinLTO.CombinedIndex,
2167 VisibleToRegularObjSymbols,
2168 IsVisibleToRegularObj);
2169 }
2170
2171 // If allowed, upgrade public vcall visibility to linkage unit visibility in
2172 // the summaries before whole program devirtualization below.
2173 updateVCallVisibilityInIndex(
2174 Index&: ThinLTO.CombinedIndex, WholeProgramVisibilityEnabledInLTO,
2175 DynamicExportSymbols, VisibleToRegularObjSymbols);
2176
2177 // Perform index-based WPD. This will return immediately if there are
2178 // no index entries in the typeIdMetadata map (e.g. if we are instead
2179 // performing IR-based WPD in hybrid regular/thin LTO mode).
2180 std::map<ValueInfo, std::vector<VTableSlotSummary>> LocalWPDTargetsMap;
2181 DenseSet<StringRef> ExternallyVisibleSymbolNames;
2182
2183 // Used by the promotion-time renaming logic. When non-null, this set
2184 // identifies symbols that should not be renamed during promotion.
2185 // It is non-null only when whole-program visibility is enabled and
2186 // renaming is not forced. Otherwise, the default renaming behavior applies.
2187 DenseSet<StringRef> *ExternallyVisibleSymbolNamesPtr =
2188 (WholeProgramVisibilityEnabledInLTO && !AlwaysRenamePromotedLocals)
2189 ? &ExternallyVisibleSymbolNames
2190 : nullptr;
2191 runWholeProgramDevirtOnIndex(
2192 Summary&: ThinLTO.CombinedIndex, ExportedGUIDs, LocalWPDTargetsMap,
2193 ExternallyVisibleSymbolNamesPtr, Ctx: &RegularLTO.Ctx);
2194
2195 auto isPrevailing = [&](GlobalValue::GUID GUID, const GlobalValueSummary *S) {
2196 return ThinLTO.isPrevailingModuleForGUID(GUID, Module: S->modulePath());
2197 };
2198 if (EnableMemProfContextDisambiguation) {
2199 MemProfContextDisambiguation ContextDisambiguation;
2200 ContextDisambiguation.run(
2201 Index&: ThinLTO.CombinedIndex, isPrevailing, Ctx&: RegularLTO.Ctx,
2202 EmitRemark: [&](StringRef PassName, StringRef RemarkName, const Twine &Msg) {
2203 auto R = OptimizationRemark(PassName.data(), RemarkName,
2204 LinkerRemarkFunction);
2205 R << Msg.str();
2206 emitRemark(Remark&: R);
2207 });
2208 }
2209
2210 // Figure out which symbols need to be internalized. This also needs to happen
2211 // at -O0 because summary-based DCE is implemented using internalization, and
2212 // we must apply DCE consistently with the full LTO module in order to avoid
2213 // undefined references during the final link.
2214 for (auto &Res : *GlobalResolutions) {
2215 // If the symbol does not have external references or it is not prevailing,
2216 // then not need to mark it as exported from a ThinLTO partition.
2217 if (Res.second.Partition != GlobalResolution::External ||
2218 !Res.second.isPrevailingIRSymbol())
2219 continue;
2220 auto GUID = Res.second.getGUID();
2221 // Mark exported unless index-based analysis determined it to be dead.
2222 if (ThinLTO.CombinedIndex.isGUIDLive(GUID))
2223 ExportedGUIDs.insert(x: GUID);
2224 }
2225
2226 // Reset the GlobalResolutions to deallocate the associated memory, as there
2227 // are no further accesses. We specifically want to do this before computing
2228 // cross module importing, which adds to peak memory via the computed import
2229 // and export lists.
2230 releaseGlobalResolutionsMemory();
2231
2232 if (Conf.OptLevel > 0)
2233 ComputeCrossModuleImport(Index: ThinLTO.CombinedIndex, ModuleToDefinedGVSummaries,
2234 isPrevailing, ImportLists, ExportLists);
2235
2236 // Any functions referenced by the jump table in the regular LTO object must
2237 // be exported.
2238 auto Defs = ThinLTO.CombinedIndex.cfiFunctionDefs().getExportedThinLTOGUIDs();
2239 ExportedGUIDs.insert(first: Defs.begin(), last: Defs.end());
2240 auto Decls =
2241 ThinLTO.CombinedIndex.cfiFunctionDecls().getExportedThinLTOGUIDs();
2242 ExportedGUIDs.insert(first: Decls.begin(), last: Decls.end());
2243
2244 auto isExported = [&](StringRef ModuleIdentifier, ValueInfo VI) {
2245 const auto &ExportList = ExportLists.find(Val: ModuleIdentifier);
2246 return (ExportList != ExportLists.end() && ExportList->second.count(V: VI)) ||
2247 ExportedGUIDs.count(x: VI.getGUID());
2248 };
2249
2250 // Update local devirtualized targets that were exported by cross-module
2251 // importing or by other devirtualizations marked in the ExportedGUIDs set.
2252 updateIndexWPDForExports(Summary&: ThinLTO.CombinedIndex, isExported,
2253 LocalWPDTargetsMap, ExternallyVisibleSymbolNamesPtr);
2254
2255 if (ExternallyVisibleSymbolNamesPtr) {
2256 // Add to ExternallyVisibleSymbolNames the set of unique names used by all
2257 // externally visible symbols in the index.
2258 for (auto &I : ThinLTO.CombinedIndex) {
2259 ValueInfo VI = ThinLTO.CombinedIndex.getValueInfo(R: I);
2260 for (const auto &Summary : VI.getSummaryList()) {
2261 const GlobalValueSummary *Base = Summary->getBaseObject();
2262 if (GlobalValue::isLocalLinkage(Linkage: Base->linkage()))
2263 continue;
2264
2265 ExternallyVisibleSymbolNamesPtr->insert(V: VI.name());
2266 break;
2267 }
2268 }
2269 }
2270
2271 thinLTOInternalizeAndPromoteInIndex(Index&: ThinLTO.CombinedIndex, isExported,
2272 isPrevailing,
2273 ExternallyVisibleSymbolNamesPtr);
2274
2275 auto recordNewLinkage = [&](StringRef ModuleIdentifier,
2276 GlobalValue::GUID GUID,
2277 GlobalValue::LinkageTypes NewLinkage) {
2278 ResolvedODR[ModuleIdentifier][GUID] = NewLinkage;
2279 };
2280 thinLTOResolvePrevailingInIndex(C: Conf, Index&: ThinLTO.CombinedIndex, isPrevailing,
2281 recordNewLinkage, GUIDPreservedSymbols);
2282
2283 thinLTOPropagateFunctionAttrs(Index&: ThinLTO.CombinedIndex, isPrevailing);
2284
2285 generateParamAccessSummary(LibraryOptions: Conf.LibraryOptions, Index&: ThinLTO.CombinedIndex);
2286
2287 if (llvm::timeTraceProfilerEnabled())
2288 llvm::timeTraceProfilerEnd();
2289
2290 TimeTraceScopeExit.release();
2291
2292 auto &ModuleMap =
2293 ThinLTO.ModulesToCompile ? *ThinLTO.ModulesToCompile : ThinLTO.ModuleMap;
2294
2295 auto RunBackends = [&](ThinBackendProc *BackendProcess) -> Error {
2296 auto ProcessOneModule = [&](int I) -> Error {
2297 auto &Mod = *(ModuleMap.begin() + I);
2298 // Tasks 0 through ParallelCodeGenParallelismLevel-1 are reserved for
2299 // combined module and parallel code generation partitions.
2300 return BackendProcess->start(
2301 Task: RegularLTO.ParallelCodeGenParallelismLevel + I, BM: Mod.second,
2302 ImportList: ImportLists[Mod.first], ExportList: ExportLists[Mod.first],
2303 ResolvedODR: ResolvedODR[Mod.first], ModuleMap&: ThinLTO.ModuleMap);
2304 };
2305
2306 BackendProcess->setup(ThinLTONumTasks: ModuleMap.size(),
2307 ThinLTOTaskOffset: RegularLTO.ParallelCodeGenParallelismLevel,
2308 Triple: RegularLTO.CombinedModule->getTargetTriple());
2309
2310 if (BackendProcess->getThreadCount() == 1 ||
2311 BackendProcess->isSensitiveToInputOrder()) {
2312 // Process the modules in the order they were provided on the
2313 // command-line. It is important for this codepath to be used for
2314 // WriteIndexesThinBackend, to ensure the emitted LinkedObjectsFile lists
2315 // ThinLTO objects in the same order as the inputs, which otherwise would
2316 // affect the final link order.
2317 for (int I = 0, E = ModuleMap.size(); I != E; ++I)
2318 if (Error E = ProcessOneModule(I))
2319 return E;
2320 } else {
2321 // When executing in parallel, process largest bitsize modules first to
2322 // improve parallelism, and avoid starving the thread pool near the end.
2323 // This saves about 15 sec on a 36-core machine while link `clang.exe`
2324 // (out of 100 sec).
2325 std::vector<BitcodeModule *> ModulesVec;
2326 ModulesVec.reserve(n: ModuleMap.size());
2327 for (auto &Mod : ModuleMap)
2328 ModulesVec.push_back(x: &Mod.second);
2329 for (int I : generateModulesOrdering(R: ModulesVec))
2330 if (Error E = ProcessOneModule(I))
2331 return E;
2332 }
2333 return BackendProcess->wait();
2334 };
2335
2336 if (!cgdata::thinLTOTwoRounds(LibraryOptions: RegularLTO.Ctx.getLibraryOptions())) {
2337 std::unique_ptr<ThinBackendProc> BackendProc =
2338 ThinLTO.Backend(Conf, ThinLTO.CombinedIndex, ModuleToDefinedGVSummaries,
2339 AddStream, Cache, BitcodeLibFuncs);
2340 return RunBackends(BackendProc.get());
2341 }
2342
2343 // Perform two rounds of code generation for ThinLTO:
2344 // 1. First round: Perform optimization and code generation, outputting to
2345 // temporary scratch objects.
2346 // 2. Merge code generation data extracted from the temporary scratch objects.
2347 // 3. Second round: Execute code generation again using the merged data.
2348 LLVM_DEBUG(dbgs() << "[TwoRounds] Initializing ThinLTO two-codegen rounds\n");
2349
2350 unsigned MaxTasks = getMaxTasks();
2351 auto Parallelism = ThinLTO.Backend.getParallelism();
2352 // Set up two additional streams and caches for storing temporary scratch
2353 // objects and optimized IRs, using the same cache directory as the original.
2354 cgdata::StreamCacheData CG(MaxTasks, Cache, "CG"), IR(MaxTasks, Cache, "IR");
2355
2356 // First round: Execute optimization and code generation, outputting to
2357 // temporary scratch objects. Serialize the optimized IRs before initiating
2358 // code generation.
2359 LLVM_DEBUG(dbgs() << "[TwoRounds] Running the first round of codegen\n");
2360 auto FirstRoundLTO = std::make_unique<FirstRoundThinBackend>(
2361 args&: Conf, args&: ThinLTO.CombinedIndex, args&: Parallelism, args&: ModuleToDefinedGVSummaries,
2362 args&: CG.AddStream, args&: CG.Cache, args&: BitcodeLibFuncs, args&: IR.AddStream, args&: IR.Cache);
2363 if (Error E = RunBackends(FirstRoundLTO.get()))
2364 return E;
2365
2366 LLVM_DEBUG(dbgs() << "[TwoRounds] Merging codegen data\n");
2367 auto CombinedHashOrErr = cgdata::mergeCodeGenData(
2368 LibraryOptions: RegularLTO.Ctx.getLibraryOptions(), ObjectFiles: *CG.getResult());
2369 if (Error E = CombinedHashOrErr.takeError())
2370 return E;
2371 auto CombinedHash = *CombinedHashOrErr;
2372 LLVM_DEBUG(dbgs() << "[TwoRounds] CGData hash: " << CombinedHash << "\n");
2373
2374 // Second round: Read the optimized IRs and execute code generation using the
2375 // merged data.
2376 LLVM_DEBUG(dbgs() << "[TwoRounds] Running the second round of codegen\n");
2377 auto SecondRoundLTO = std::make_unique<SecondRoundThinBackend>(
2378 args&: Conf, args&: ThinLTO.CombinedIndex, args&: Parallelism, args&: ModuleToDefinedGVSummaries,
2379 args&: AddStream, args&: Cache, args&: BitcodeLibFuncs, args: IR.getResult(), args&: CombinedHash);
2380 return RunBackends(SecondRoundLTO.get());
2381}
2382
2383Expected<LLVMRemarkFileHandle> lto::setupLLVMOptimizationRemarks(
2384 LLVMContext &Context, StringRef RemarksFilename, StringRef RemarksPasses,
2385 StringRef RemarksFormat, bool RemarksWithHotness,
2386 std::optional<uint64_t> RemarksHotnessThreshold, int Count) {
2387 std::string Filename = std::string(RemarksFilename);
2388 // For ThinLTO, file.opt.<format> becomes
2389 // file.opt.<format>.thin.<num>.<format>.
2390 if (!Filename.empty() && Count != -1)
2391 Filename =
2392 (Twine(Filename) + ".thin." + llvm::utostr(X: Count) + "." + RemarksFormat)
2393 .str();
2394
2395 auto ResultOrErr = llvm::setupLLVMOptimizationRemarks(
2396 Context, RemarksFilename: Filename, RemarksPasses, RemarksFormat, RemarksWithHotness,
2397 RemarksHotnessThreshold);
2398 if (Error E = ResultOrErr.takeError())
2399 return std::move(E);
2400
2401 if (*ResultOrErr)
2402 (*ResultOrErr)->keep();
2403
2404 return ResultOrErr;
2405}
2406
2407Expected<std::unique_ptr<ToolOutputFile>>
2408lto::setupStatsFile(StringRef StatsFilename) {
2409 // Setup output file to emit statistics.
2410 if (StatsFilename.empty())
2411 return nullptr;
2412
2413 llvm::EnableStatistics(DoPrintOnExit: false);
2414 std::error_code EC;
2415 auto StatsFile =
2416 std::make_unique<ToolOutputFile>(args&: StatsFilename, args&: EC, args: sys::fs::OF_None);
2417 if (EC)
2418 return errorCodeToError(EC);
2419
2420 StatsFile->keep();
2421 return std::move(StatsFile);
2422}
2423
2424// Compute the ordering we will process the inputs: the rough heuristic here
2425// is to sort them per size so that the largest module get schedule as soon as
2426// possible. This is purely a compile-time optimization.
2427std::vector<int> lto::generateModulesOrdering(ArrayRef<BitcodeModule *> R) {
2428 auto Seq = llvm::seq<int>(Begin: 0, End: R.size());
2429 std::vector<int> ModulesOrdering(Seq.begin(), Seq.end());
2430 llvm::sort(C&: ModulesOrdering, Comp: [&](int LeftIndex, int RightIndex) {
2431 auto LSize = R[LeftIndex]->getBuffer().size();
2432 auto RSize = R[RightIndex]->getBuffer().size();
2433 return LSize > RSize;
2434 });
2435 return ModulesOrdering;
2436}
2437