LLVM 19.0.0git
LTO.cpp
Go to the documentation of this file.
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/ScopeExit.h"
15#include "llvm/ADT/SmallSet.h"
16#include "llvm/ADT/Statistic.h"
25#include "llvm/Config/llvm-config.h"
26#include "llvm/IR/AutoUpgrade.h"
28#include "llvm/IR/Intrinsics.h"
31#include "llvm/IR/Mangler.h"
32#include "llvm/IR/Metadata.h"
33#include "llvm/LTO/LTOBackend.h"
35#include "llvm/Linker/IRMover.h"
39#include "llvm/Support/Error.h"
43#include "llvm/Support/Path.h"
44#include "llvm/Support/SHA1.h"
50#include "llvm/Support/VCSRevision.h"
53#include "llvm/Transforms/IPO.h"
58
59#include <optional>
60#include <set>
61
62using namespace llvm;
63using namespace lto;
64using namespace object;
65
66#define DEBUG_TYPE "lto"
67
69
70static cl::opt<bool>
71 DumpThinCGSCCs("dump-thin-cg-sccs", cl::init(false), cl::Hidden,
72 cl::desc("Dump the SCCs in the ThinLTO index's callgraph"));
73
74namespace llvm {
75/// Enable global value internalization in LTO.
77 "enable-lto-internalization", cl::init(true), cl::Hidden,
78 cl::desc("Enable global value internalization in LTO"));
79
80/// Indicate we are linking with an allocator that supports hot/cold operator
81/// new interfaces.
83
84/// Enable MemProf context disambiguation for thin link.
86} // namespace llvm
87
88// Computes a unique hash for the Module considering the current list of
89// export/import and other global analysis results.
90// The hash is produced in \p Key.
92 SmallString<40> &Key, const Config &Conf, const ModuleSummaryIndex &Index,
93 StringRef ModuleID, const FunctionImporter::ImportMapTy &ImportList,
94 const FunctionImporter::ExportSetTy &ExportList,
95 const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
96 const GVSummaryMapTy &DefinedGlobals,
97 const std::set<GlobalValue::GUID> &CfiFunctionDefs,
98 const std::set<GlobalValue::GUID> &CfiFunctionDecls) {
99 // Compute the unique hash for this entry.
100 // This is based on the current compiler version, the module itself, the
101 // export list, the hash for every single module in the import list, the
102 // list of ResolvedODR for the module, and the list of preserved symbols.
103 SHA1 Hasher;
104
105 // Start with the compiler revision
106 Hasher.update(LLVM_VERSION_STRING);
107#ifdef LLVM_REVISION
108 Hasher.update(LLVM_REVISION);
109#endif
110
111 // Include the parts of the LTO configuration that affect code generation.
112 auto AddString = [&](StringRef Str) {
113 Hasher.update(Str);
114 Hasher.update(ArrayRef<uint8_t>{0});
115 };
116 auto AddUnsigned = [&](unsigned I) {
117 uint8_t Data[4];
119 Hasher.update(Data);
120 };
121 auto AddUint64 = [&](uint64_t I) {
122 uint8_t Data[8];
124 Hasher.update(Data);
125 };
126 auto AddUint8 = [&](const uint8_t I) {
127 Hasher.update(ArrayRef<uint8_t>((const uint8_t *)&I, 1));
128 };
129 AddString(Conf.CPU);
130 // FIXME: Hash more of Options. For now all clients initialize Options from
131 // command-line flags (which is unsupported in production), but may set
132 // X86RelaxRelocations. The clang driver can also pass FunctionSections,
133 // DataSections and DebuggerTuning via command line flags.
134 AddUnsigned(Conf.Options.MCOptions.X86RelaxRelocations);
135 AddUnsigned(Conf.Options.FunctionSections);
136 AddUnsigned(Conf.Options.DataSections);
137 AddUnsigned((unsigned)Conf.Options.DebuggerTuning);
138 for (auto &A : Conf.MAttrs)
139 AddString(A);
140 if (Conf.RelocModel)
141 AddUnsigned(*Conf.RelocModel);
142 else
143 AddUnsigned(-1);
144 if (Conf.CodeModel)
145 AddUnsigned(*Conf.CodeModel);
146 else
147 AddUnsigned(-1);
148 for (const auto &S : Conf.MllvmArgs)
149 AddString(S);
150 AddUnsigned(static_cast<int>(Conf.CGOptLevel));
151 AddUnsigned(static_cast<int>(Conf.CGFileType));
152 AddUnsigned(Conf.OptLevel);
153 AddUnsigned(Conf.Freestanding);
154 AddString(Conf.OptPipeline);
155 AddString(Conf.AAPipeline);
156 AddString(Conf.OverrideTriple);
157 AddString(Conf.DefaultTriple);
158 AddString(Conf.DwoDir);
159
160 // Include the hash for the current module
161 auto ModHash = Index.getModuleHash(ModuleID);
162 Hasher.update(ArrayRef<uint8_t>((uint8_t *)&ModHash[0], sizeof(ModHash)));
163
164 // TODO: `ExportList` is determined by `ImportList`. Since `ImportList` is
165 // used to compute cache key, we could omit hashing `ExportList` here.
166 std::vector<uint64_t> ExportsGUID;
167 ExportsGUID.reserve(ExportList.size());
168 for (const auto &VI : ExportList)
169 ExportsGUID.push_back(VI.getGUID());
170
171 // Sort the export list elements GUIDs.
172 llvm::sort(ExportsGUID);
173 for (auto GUID : ExportsGUID)
174 Hasher.update(ArrayRef<uint8_t>((uint8_t *)&GUID, sizeof(GUID)));
175
176 // Include the hash for every module we import functions from. The set of
177 // imported symbols for each module may affect code generation and is
178 // sensitive to link order, so include that as well.
179 using ImportMapIteratorTy = FunctionImporter::ImportMapTy::const_iterator;
180 struct ImportModule {
181 ImportMapIteratorTy ModIt;
182 const ModuleSummaryIndex::ModuleInfo *ModInfo;
183
184 StringRef getIdentifier() const { return ModIt->getFirst(); }
185 const FunctionImporter::FunctionsToImportTy &getFunctions() const {
186 return ModIt->second;
187 }
188
189 const ModuleHash &getHash() const { return ModInfo->second; }
190 };
191
192 std::vector<ImportModule> ImportModulesVector;
193 ImportModulesVector.reserve(ImportList.size());
194
195 for (ImportMapIteratorTy It = ImportList.begin(); It != ImportList.end();
196 ++It) {
197 ImportModulesVector.push_back({It, Index.getModule(It->getFirst())});
198 }
199 // Order using module hash, to be both independent of module name and
200 // module order.
201 llvm::sort(ImportModulesVector,
202 [](const ImportModule &Lhs, const ImportModule &Rhs) -> bool {
203 return Lhs.getHash() < Rhs.getHash();
204 });
205 std::vector<std::pair<uint64_t, uint8_t>> ImportedGUIDs;
206 for (const ImportModule &Entry : ImportModulesVector) {
207 auto ModHash = Entry.getHash();
208 Hasher.update(ArrayRef<uint8_t>((uint8_t *)&ModHash[0], sizeof(ModHash)));
209
210 AddUint64(Entry.getFunctions().size());
211
212 ImportedGUIDs.clear();
213 for (auto &[Fn, ImportType] : Entry.getFunctions())
214 ImportedGUIDs.push_back(std::make_pair(Fn, ImportType));
215 llvm::sort(ImportedGUIDs);
216 for (auto &[GUID, Type] : ImportedGUIDs) {
217 AddUint64(GUID);
218 AddUint8(Type);
219 }
220 }
221
222 // Include the hash for the resolved ODR.
223 for (auto &Entry : ResolvedODR) {
224 Hasher.update(ArrayRef<uint8_t>((const uint8_t *)&Entry.first,
225 sizeof(GlobalValue::GUID)));
226 Hasher.update(ArrayRef<uint8_t>((const uint8_t *)&Entry.second,
228 }
229
230 // Members of CfiFunctionDefs and CfiFunctionDecls that are referenced or
231 // defined in this module.
232 std::set<GlobalValue::GUID> UsedCfiDefs;
233 std::set<GlobalValue::GUID> UsedCfiDecls;
234
235 // Typeids used in this module.
236 std::set<GlobalValue::GUID> UsedTypeIds;
237
238 auto AddUsedCfiGlobal = [&](GlobalValue::GUID ValueGUID) {
239 if (CfiFunctionDefs.count(ValueGUID))
240 UsedCfiDefs.insert(ValueGUID);
241 if (CfiFunctionDecls.count(ValueGUID))
242 UsedCfiDecls.insert(ValueGUID);
243 };
244
245 auto AddUsedThings = [&](GlobalValueSummary *GS) {
246 if (!GS) return;
247 AddUnsigned(GS->getVisibility());
248 AddUnsigned(GS->isLive());
249 AddUnsigned(GS->canAutoHide());
250 for (const ValueInfo &VI : GS->refs()) {
251 AddUnsigned(VI.isDSOLocal(Index.withDSOLocalPropagation()));
252 AddUsedCfiGlobal(VI.getGUID());
253 }
254 if (auto *GVS = dyn_cast<GlobalVarSummary>(GS)) {
255 AddUnsigned(GVS->maybeReadOnly());
256 AddUnsigned(GVS->maybeWriteOnly());
257 }
258 if (auto *FS = dyn_cast<FunctionSummary>(GS)) {
259 for (auto &TT : FS->type_tests())
260 UsedTypeIds.insert(TT);
261 for (auto &TT : FS->type_test_assume_vcalls())
262 UsedTypeIds.insert(TT.GUID);
263 for (auto &TT : FS->type_checked_load_vcalls())
264 UsedTypeIds.insert(TT.GUID);
265 for (auto &TT : FS->type_test_assume_const_vcalls())
266 UsedTypeIds.insert(TT.VFunc.GUID);
267 for (auto &TT : FS->type_checked_load_const_vcalls())
268 UsedTypeIds.insert(TT.VFunc.GUID);
269 for (auto &ET : FS->calls()) {
270 AddUnsigned(ET.first.isDSOLocal(Index.withDSOLocalPropagation()));
271 AddUsedCfiGlobal(ET.first.getGUID());
272 }
273 }
274 };
275
276 // Include the hash for the linkage type to reflect internalization and weak
277 // resolution, and collect any used type identifier resolutions.
278 for (auto &GS : DefinedGlobals) {
279 GlobalValue::LinkageTypes Linkage = GS.second->linkage();
280 Hasher.update(
281 ArrayRef<uint8_t>((const uint8_t *)&Linkage, sizeof(Linkage)));
282 AddUsedCfiGlobal(GS.first);
283 AddUsedThings(GS.second);
284 }
285
286 // Imported functions may introduce new uses of type identifier resolutions,
287 // so we need to collect their used resolutions as well.
288 for (const ImportModule &ImpM : ImportModulesVector)
289 for (auto &[GUID, UnusedImportType] : ImpM.getFunctions()) {
291 Index.findSummaryInModule(GUID, ImpM.getIdentifier());
292 AddUsedThings(S);
293 // If this is an alias, we also care about any types/etc. that the aliasee
294 // may reference.
295 if (auto *AS = dyn_cast_or_null<AliasSummary>(S))
296 AddUsedThings(AS->getBaseObject());
297 }
298
299 auto AddTypeIdSummary = [&](StringRef TId, const TypeIdSummary &S) {
300 AddString(TId);
301
302 AddUnsigned(S.TTRes.TheKind);
303 AddUnsigned(S.TTRes.SizeM1BitWidth);
304
305 AddUint64(S.TTRes.AlignLog2);
306 AddUint64(S.TTRes.SizeM1);
307 AddUint64(S.TTRes.BitMask);
308 AddUint64(S.TTRes.InlineBits);
309
310 AddUint64(S.WPDRes.size());
311 for (auto &WPD : S.WPDRes) {
312 AddUnsigned(WPD.first);
313 AddUnsigned(WPD.second.TheKind);
314 AddString(WPD.second.SingleImplName);
315
316 AddUint64(WPD.second.ResByArg.size());
317 for (auto &ByArg : WPD.second.ResByArg) {
318 AddUint64(ByArg.first.size());
319 for (uint64_t Arg : ByArg.first)
320 AddUint64(Arg);
321 AddUnsigned(ByArg.second.TheKind);
322 AddUint64(ByArg.second.Info);
323 AddUnsigned(ByArg.second.Byte);
324 AddUnsigned(ByArg.second.Bit);
325 }
326 }
327 };
328
329 // Include the hash for all type identifiers used by this module.
330 for (GlobalValue::GUID TId : UsedTypeIds) {
331 auto TidIter = Index.typeIds().equal_range(TId);
332 for (auto It = TidIter.first; It != TidIter.second; ++It)
333 AddTypeIdSummary(It->second.first, It->second.second);
334 }
335
336 AddUnsigned(UsedCfiDefs.size());
337 for (auto &V : UsedCfiDefs)
338 AddUint64(V);
339
340 AddUnsigned(UsedCfiDecls.size());
341 for (auto &V : UsedCfiDecls)
342 AddUint64(V);
343
344 if (!Conf.SampleProfile.empty()) {
345 auto FileOrErr = MemoryBuffer::getFile(Conf.SampleProfile);
346 if (FileOrErr) {
347 Hasher.update(FileOrErr.get()->getBuffer());
348
349 if (!Conf.ProfileRemapping.empty()) {
350 FileOrErr = MemoryBuffer::getFile(Conf.ProfileRemapping);
351 if (FileOrErr)
352 Hasher.update(FileOrErr.get()->getBuffer());
353 }
354 }
355 }
356
357 Key = toHex(Hasher.result());
358}
359
361 const Config &C, ValueInfo VI,
362 DenseSet<GlobalValueSummary *> &GlobalInvolvedWithAlias,
364 isPrevailing,
366 recordNewLinkage,
367 const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols) {
369 C.VisibilityScheme == Config::ELF ? VI.getELFVisibility()
371 for (auto &S : VI.getSummaryList()) {
372 GlobalValue::LinkageTypes OriginalLinkage = S->linkage();
373 // Ignore local and appending linkage values since the linker
374 // doesn't resolve them.
375 if (GlobalValue::isLocalLinkage(OriginalLinkage) ||
377 continue;
378 // We need to emit only one of these. The prevailing module will keep it,
379 // but turned into a weak, while the others will drop it when possible.
380 // This is both a compile-time optimization and a correctness
381 // transformation. This is necessary for correctness when we have exported
382 // a reference - we need to convert the linkonce to weak to
383 // ensure a copy is kept to satisfy the exported reference.
384 // FIXME: We may want to split the compile time and correctness
385 // aspects into separate routines.
386 if (isPrevailing(VI.getGUID(), S.get())) {
387 if (GlobalValue::isLinkOnceLinkage(OriginalLinkage)) {
388 S->setLinkage(GlobalValue::getWeakLinkage(
389 GlobalValue::isLinkOnceODRLinkage(OriginalLinkage)));
390 // The kept copy is eligible for auto-hiding (hidden visibility) if all
391 // copies were (i.e. they were all linkonce_odr global unnamed addr).
392 // If any copy is not (e.g. it was originally weak_odr), then the symbol
393 // must remain externally available (e.g. a weak_odr from an explicitly
394 // instantiated template). Additionally, if it is in the
395 // GUIDPreservedSymbols set, that means that it is visibile outside
396 // the summary (e.g. in a native object or a bitcode file without
397 // summary), and in that case we cannot hide it as it isn't possible to
398 // check all copies.
399 S->setCanAutoHide(VI.canAutoHide() &&
400 !GUIDPreservedSymbols.count(VI.getGUID()));
401 }
402 if (C.VisibilityScheme == Config::FromPrevailing)
403 Visibility = S->getVisibility();
404 }
405 // Alias and aliasee can't be turned into available_externally.
406 else if (!isa<AliasSummary>(S.get()) &&
407 !GlobalInvolvedWithAlias.count(S.get()))
409
410 // For ELF, set visibility to the computed visibility from summaries. We
411 // don't track visibility from declarations so this may be more relaxed than
412 // the most constraining one.
413 if (C.VisibilityScheme == Config::ELF)
414 S->setVisibility(Visibility);
415
416 if (S->linkage() != OriginalLinkage)
417 recordNewLinkage(S->modulePath(), VI.getGUID(), S->linkage());
418 }
419
420 if (C.VisibilityScheme == Config::FromPrevailing) {
421 for (auto &S : VI.getSummaryList()) {
422 GlobalValue::LinkageTypes OriginalLinkage = S->linkage();
423 if (GlobalValue::isLocalLinkage(OriginalLinkage) ||
425 continue;
426 S->setVisibility(Visibility);
427 }
428 }
429}
430
431/// Resolve linkage for prevailing symbols in the \p Index.
432//
433// We'd like to drop these functions if they are no longer referenced in the
434// current module. However there is a chance that another module is still
435// referencing them because of the import. We make sure we always emit at least
436// one copy.
440 isPrevailing,
442 recordNewLinkage,
443 const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols) {
444 // We won't optimize the globals that are referenced by an alias for now
445 // Ideally we should turn the alias into a global and duplicate the definition
446 // when needed.
447 DenseSet<GlobalValueSummary *> GlobalInvolvedWithAlias;
448 for (auto &I : Index)
449 for (auto &S : I.second.SummaryList)
450 if (auto AS = dyn_cast<AliasSummary>(S.get()))
451 GlobalInvolvedWithAlias.insert(&AS->getAliasee());
452
453 for (auto &I : Index)
454 thinLTOResolvePrevailingGUID(C, Index.getValueInfo(I),
455 GlobalInvolvedWithAlias, isPrevailing,
456 recordNewLinkage, GUIDPreservedSymbols);
457}
458
460 ValueInfo VI, function_ref<bool(StringRef, ValueInfo)> isExported,
462 isPrevailing) {
463 auto ExternallyVisibleCopies =
464 llvm::count_if(VI.getSummaryList(),
465 [](const std::unique_ptr<GlobalValueSummary> &Summary) {
466 return !GlobalValue::isLocalLinkage(Summary->linkage());
467 });
468
469 for (auto &S : VI.getSummaryList()) {
470 // First see if we need to promote an internal value because it is not
471 // exported.
472 if (isExported(S->modulePath(), VI)) {
473 if (GlobalValue::isLocalLinkage(S->linkage()))
474 S->setLinkage(GlobalValue::ExternalLinkage);
475 continue;
476 }
477
478 // Otherwise, see if we can internalize.
480 continue;
481
482 // Non-exported values with external linkage can be internalized.
483 if (GlobalValue::isExternalLinkage(S->linkage())) {
484 S->setLinkage(GlobalValue::InternalLinkage);
485 continue;
486 }
487
488 // Non-exported function and variable definitions with a weak-for-linker
489 // linkage can be internalized in certain cases. The minimum legality
490 // requirements would be that they are not address taken to ensure that we
491 // don't break pointer equality checks, and that variables are either read-
492 // or write-only. For functions, this is the case if either all copies are
493 // [local_]unnamed_addr, or we can propagate reference edge attributes
494 // (which is how this is guaranteed for variables, when analyzing whether
495 // they are read or write-only).
496 //
497 // However, we only get to this code for weak-for-linkage values in one of
498 // two cases:
499 // 1) The prevailing copy is not in IR (it is in native code).
500 // 2) The prevailing copy in IR is not exported from its module.
501 // Additionally, at least for the new LTO API, case 2 will only happen if
502 // there is exactly one definition of the value (i.e. in exactly one
503 // module), as duplicate defs are result in the value being marked exported.
504 // Likely, users of the legacy LTO API are similar, however, currently there
505 // are llvm-lto based tests of the legacy LTO API that do not mark
506 // duplicate linkonce_odr copies as exported via the tool, so we need
507 // to handle that case below by checking the number of copies.
508 //
509 // Generally, we only want to internalize a weak-for-linker value in case
510 // 2, because in case 1 we cannot see how the value is used to know if it
511 // is read or write-only. We also don't want to bloat the binary with
512 // multiple internalized copies of non-prevailing linkonce/weak functions.
513 // Note if we don't internalize, we will convert non-prevailing copies to
514 // available_externally anyway, so that we drop them after inlining. The
515 // only reason to internalize such a function is if we indeed have a single
516 // copy, because internalizing it won't increase binary size, and enables
517 // use of inliner heuristics that are more aggressive in the face of a
518 // single call to a static (local). For variables, internalizing a read or
519 // write only variable can enable more aggressive optimization. However, we
520 // already perform this elsewhere in the ThinLTO backend handling for
521 // read or write-only variables (processGlobalForThinLTO).
522 //
523 // Therefore, only internalize linkonce/weak if there is a single copy, that
524 // is prevailing in this IR module. We can do so aggressively, without
525 // requiring the address to be insignificant, or that a variable be read or
526 // write-only.
527 if (!GlobalValue::isWeakForLinker(S->linkage()) ||
529 continue;
530
531 if (isPrevailing(VI.getGUID(), S.get()) && ExternallyVisibleCopies == 1)
532 S->setLinkage(GlobalValue::InternalLinkage);
533 }
534}
535
536// Update the linkages in the given \p Index to mark exported values
537// as external and non-exported values as internal.
540 function_ref<bool(StringRef, ValueInfo)> isExported,
542 isPrevailing) {
543 for (auto &I : Index)
544 thinLTOInternalizeAndPromoteGUID(Index.getValueInfo(I), isExported,
545 isPrevailing);
546}
547
548// Requires a destructor for std::vector<InputModule>.
549InputFile::~InputFile() = default;
550
552 std::unique_ptr<InputFile> File(new InputFile);
553
554 Expected<IRSymtabFile> FOrErr = readIRSymtab(Object);
555 if (!FOrErr)
556 return FOrErr.takeError();
557
558 File->TargetTriple = FOrErr->TheReader.getTargetTriple();
559 File->SourceFileName = FOrErr->TheReader.getSourceFileName();
560 File->COFFLinkerOpts = FOrErr->TheReader.getCOFFLinkerOpts();
561 File->DependentLibraries = FOrErr->TheReader.getDependentLibraries();
562 File->ComdatTable = FOrErr->TheReader.getComdatTable();
563
564 for (unsigned I = 0; I != FOrErr->Mods.size(); ++I) {
565 size_t Begin = File->Symbols.size();
566 for (const irsymtab::Reader::SymbolRef &Sym :
567 FOrErr->TheReader.module_symbols(I))
568 // Skip symbols that are irrelevant to LTO. Note that this condition needs
569 // to match the one in Skip() in LTO::addRegularLTO().
570 if (Sym.isGlobal() && !Sym.isFormatSpecific())
571 File->Symbols.push_back(Sym);
572 File->ModuleSymIndices.push_back({Begin, File->Symbols.size()});
573 }
574
575 File->Mods = FOrErr->Mods;
576 File->Strtab = std::move(FOrErr->Strtab);
577 return std::move(File);
578}
579
581 return Mods[0].getModuleIdentifier();
582}
583
585 assert(Mods.size() == 1 && "Expect only one bitcode module");
586 return Mods[0];
587}
588
589LTO::RegularLTOState::RegularLTOState(unsigned ParallelCodeGenParallelismLevel,
590 const Config &Conf)
591 : ParallelCodeGenParallelismLevel(ParallelCodeGenParallelismLevel),
592 Ctx(Conf), CombinedModule(std::make_unique<Module>("ld-temp.o", Ctx)),
593 Mover(std::make_unique<IRMover>(*CombinedModule)) {
594 CombinedModule->IsNewDbgInfoFormat = UseNewDbgInfoFormat;
595}
596
597LTO::ThinLTOState::ThinLTOState(ThinBackend Backend)
598 : Backend(Backend), CombinedIndex(/*HaveGVs*/ false) {
599 if (!Backend)
600 this->Backend =
602}
603
605 unsigned ParallelCodeGenParallelismLevel, LTOKind LTOMode)
606 : Conf(std::move(Conf)),
607 RegularLTO(ParallelCodeGenParallelismLevel, this->Conf),
608 ThinLTO(std::move(Backend)),
609 GlobalResolutions(std::make_optional<StringMap<GlobalResolution>>()),
610 LTOMode(LTOMode) {}
611
612// Requires a destructor for MapVector<BitcodeModule>.
613LTO::~LTO() = default;
614
615// Add the symbols in the given module to the GlobalResolutions map, and resolve
616// their partitions.
617void LTO::addModuleToGlobalRes(ArrayRef<InputFile::Symbol> Syms,
619 unsigned Partition, bool InSummary) {
620 auto *ResI = Res.begin();
621 auto *ResE = Res.end();
622 (void)ResE;
623 const Triple TT(RegularLTO.CombinedModule->getTargetTriple());
624 for (const InputFile::Symbol &Sym : Syms) {
625 assert(ResI != ResE);
626 SymbolResolution Res = *ResI++;
627
628 auto &GlobalRes = (*GlobalResolutions)[Sym.getName()];
629 GlobalRes.UnnamedAddr &= Sym.isUnnamedAddr();
630 if (Res.Prevailing) {
631 assert(!GlobalRes.Prevailing &&
632 "Multiple prevailing defs are not allowed");
633 GlobalRes.Prevailing = true;
634 GlobalRes.IRName = std::string(Sym.getIRName());
635 } else if (!GlobalRes.Prevailing && GlobalRes.IRName.empty()) {
636 // Sometimes it can be two copies of symbol in a module and prevailing
637 // symbol can have no IR name. That might happen if symbol is defined in
638 // module level inline asm block. In case we have multiple modules with
639 // the same symbol we want to use IR name of the prevailing symbol.
640 // Otherwise, if we haven't seen a prevailing symbol, set the name so that
641 // we can later use it to check if there is any prevailing copy in IR.
642 GlobalRes.IRName = std::string(Sym.getIRName());
643 }
644
645 // In rare occasion, the symbol used to initialize GlobalRes has a different
646 // IRName from the inspected Symbol. This can happen on macOS + iOS, when a
647 // symbol is referenced through its mangled name, say @"\01_symbol" while
648 // the IRName is @symbol (the prefix underscore comes from MachO mangling).
649 // In that case, we have the same actual Symbol that can get two different
650 // GUID, leading to some invalid internalization. Workaround this by marking
651 // the GlobalRes external.
652
653 // FIXME: instead of this check, it would be desirable to compute GUIDs
654 // based on mangled name, but this requires an access to the Target Triple
655 // and would be relatively invasive on the codebase.
656 if (GlobalRes.IRName != Sym.getIRName()) {
657 GlobalRes.Partition = GlobalResolution::External;
658 GlobalRes.VisibleOutsideSummary = true;
659 }
660
661 // Set the partition to external if we know it is re-defined by the linker
662 // with -defsym or -wrap options, used elsewhere, e.g. it is visible to a
663 // regular object, is referenced from llvm.compiler.used/llvm.used, or was
664 // already recorded as being referenced from a different partition.
665 if (Res.LinkerRedefined || Res.VisibleToRegularObj || Sym.isUsed() ||
666 (GlobalRes.Partition != GlobalResolution::Unknown &&
667 GlobalRes.Partition != Partition)) {
668 GlobalRes.Partition = GlobalResolution::External;
669 } else
670 // First recorded reference, save the current partition.
671 GlobalRes.Partition = Partition;
672
673 // Flag as visible outside of summary if visible from a regular object or
674 // from a module that does not have a summary.
675 GlobalRes.VisibleOutsideSummary |=
676 (Res.VisibleToRegularObj || Sym.isUsed() || !InSummary);
677
678 GlobalRes.ExportDynamic |= Res.ExportDynamic;
679 }
680}
681
684 StringRef Path = Input->getName();
685 OS << Path << '\n';
686 auto ResI = Res.begin();
687 for (const InputFile::Symbol &Sym : Input->symbols()) {
688 assert(ResI != Res.end());
689 SymbolResolution Res = *ResI++;
690
691 OS << "-r=" << Path << ',' << Sym.getName() << ',';
692 if (Res.Prevailing)
693 OS << 'p';
695 OS << 'l';
696 if (Res.VisibleToRegularObj)
697 OS << 'x';
698 if (Res.LinkerRedefined)
699 OS << 'r';
700 OS << '\n';
701 }
702 OS.flush();
703 assert(ResI == Res.end());
704}
705
706Error LTO::add(std::unique_ptr<InputFile> Input,
708 assert(!CalledGetMaxTasks);
709
710 if (Conf.ResolutionFile)
711 writeToResolutionFile(*Conf.ResolutionFile, Input.get(), Res);
712
713 if (RegularLTO.CombinedModule->getTargetTriple().empty()) {
714 RegularLTO.CombinedModule->setTargetTriple(Input->getTargetTriple());
715 if (Triple(Input->getTargetTriple()).isOSBinFormatELF())
717 }
718
719 const SymbolResolution *ResI = Res.begin();
720 for (unsigned I = 0; I != Input->Mods.size(); ++I)
721 if (Error Err = addModule(*Input, I, ResI, Res.end()))
722 return Err;
723
724 assert(ResI == Res.end());
725 return Error::success();
726}
727
728Error LTO::addModule(InputFile &Input, unsigned ModI,
729 const SymbolResolution *&ResI,
730 const SymbolResolution *ResE) {
731 Expected<BitcodeLTOInfo> LTOInfo = Input.Mods[ModI].getLTOInfo();
732 if (!LTOInfo)
733 return LTOInfo.takeError();
734
735 if (EnableSplitLTOUnit) {
736 // If only some modules were split, flag this in the index so that
737 // we can skip or error on optimizations that need consistently split
738 // modules (whole program devirt and lower type tests).
739 if (*EnableSplitLTOUnit != LTOInfo->EnableSplitLTOUnit)
740 ThinLTO.CombinedIndex.setPartiallySplitLTOUnits();
741 } else
742 EnableSplitLTOUnit = LTOInfo->EnableSplitLTOUnit;
743
744 BitcodeModule BM = Input.Mods[ModI];
745
746 if ((LTOMode == LTOK_UnifiedRegular || LTOMode == LTOK_UnifiedThin) &&
747 !LTOInfo->UnifiedLTO)
748 return make_error<StringError>(
749 "unified LTO compilation must use "
750 "compatible bitcode modules (use -funified-lto)",
752
753 if (LTOInfo->UnifiedLTO && LTOMode == LTOK_Default)
754 LTOMode = LTOK_UnifiedThin;
755
756 bool IsThinLTO = LTOInfo->IsThinLTO && (LTOMode != LTOK_UnifiedRegular);
757
758 auto ModSyms = Input.module_symbols(ModI);
759 addModuleToGlobalRes(ModSyms, {ResI, ResE},
760 IsThinLTO ? ThinLTO.ModuleMap.size() + 1 : 0,
761 LTOInfo->HasSummary);
762
763 if (IsThinLTO)
764 return addThinLTO(BM, ModSyms, ResI, ResE);
765
766 RegularLTO.EmptyCombinedModule = false;
768 addRegularLTO(BM, ModSyms, ResI, ResE);
769 if (!ModOrErr)
770 return ModOrErr.takeError();
771
772 if (!LTOInfo->HasSummary)
773 return linkRegularLTO(std::move(*ModOrErr), /*LivenessFromIndex=*/false);
774
775 // Regular LTO module summaries are added to a dummy module that represents
776 // the combined regular LTO module.
777 if (Error Err = BM.readSummary(ThinLTO.CombinedIndex, ""))
778 return Err;
779 RegularLTO.ModsWithSummaries.push_back(std::move(*ModOrErr));
780 return Error::success();
781}
782
783// Checks whether the given global value is in a non-prevailing comdat
784// (comdat containing values the linker indicated were not prevailing,
785// which we then dropped to available_externally), and if so, removes
786// it from the comdat. This is called for all global values to ensure the
787// comdat is empty rather than leaving an incomplete comdat. It is needed for
788// regular LTO modules, in case we are in a mixed-LTO mode (both regular
789// and thin LTO modules) compilation. Since the regular LTO module will be
790// linked first in the final native link, we want to make sure the linker
791// doesn't select any of these incomplete comdats that would be left
792// in the regular LTO module without this cleanup.
793static void
795 std::set<const Comdat *> &NonPrevailingComdats) {
796 Comdat *C = GV.getComdat();
797 if (!C)
798 return;
799
800 if (!NonPrevailingComdats.count(C))
801 return;
802
803 // Additionally need to drop all global values from the comdat to
804 // available_externally, to satisfy the COMDAT requirement that all members
805 // are discarded as a unit. The non-local linkage global values avoid
806 // duplicate definition linker errors.
808
809 if (auto GO = dyn_cast<GlobalObject>(&GV))
810 GO->setComdat(nullptr);
811}
812
813// Add a regular LTO object to the link.
814// The resulting module needs to be linked into the combined LTO module with
815// linkRegularLTO.
817LTO::addRegularLTO(BitcodeModule BM, ArrayRef<InputFile::Symbol> Syms,
818 const SymbolResolution *&ResI,
819 const SymbolResolution *ResE) {
820 RegularLTOState::AddedModule Mod;
822 BM.getLazyModule(RegularLTO.Ctx, /*ShouldLazyLoadMetadata*/ true,
823 /*IsImporting*/ false);
824 if (!MOrErr)
825 return MOrErr.takeError();
826 Module &M = **MOrErr;
827 Mod.M = std::move(*MOrErr);
828
829 if (Error Err = M.materializeMetadata())
830 return std::move(Err);
831
832 // If cfi.functions is present and we are in regular LTO mode, LowerTypeTests
833 // will rename local functions in the merged module as "<function name>.1".
834 // This causes linking errors, since other parts of the module expect the
835 // original function name.
836 if (LTOMode == LTOK_UnifiedRegular)
837 if (NamedMDNode *CfiFunctionsMD = M.getNamedMetadata("cfi.functions"))
838 M.eraseNamedMetadata(CfiFunctionsMD);
839
841
842 ModuleSymbolTable SymTab;
843 SymTab.addModule(&M);
844
845 for (GlobalVariable &GV : M.globals())
846 if (GV.hasAppendingLinkage())
847 Mod.Keep.push_back(&GV);
848
849 DenseSet<GlobalObject *> AliasedGlobals;
850 for (auto &GA : M.aliases())
851 if (GlobalObject *GO = GA.getAliaseeObject())
852 AliasedGlobals.insert(GO);
853
854 // In this function we need IR GlobalValues matching the symbols in Syms
855 // (which is not backed by a module), so we need to enumerate them in the same
856 // order. The symbol enumeration order of a ModuleSymbolTable intentionally
857 // matches the order of an irsymtab, but when we read the irsymtab in
858 // InputFile::create we omit some symbols that are irrelevant to LTO. The
859 // Skip() function skips the same symbols from the module as InputFile does
860 // from the symbol table.
861 auto MsymI = SymTab.symbols().begin(), MsymE = SymTab.symbols().end();
862 auto Skip = [&]() {
863 while (MsymI != MsymE) {
864 auto Flags = SymTab.getSymbolFlags(*MsymI);
865 if ((Flags & object::BasicSymbolRef::SF_Global) &&
867 return;
868 ++MsymI;
869 }
870 };
871 Skip();
872
873 std::set<const Comdat *> NonPrevailingComdats;
874 SmallSet<StringRef, 2> NonPrevailingAsmSymbols;
875 for (const InputFile::Symbol &Sym : Syms) {
876 assert(ResI != ResE);
877 SymbolResolution Res = *ResI++;
878
879 assert(MsymI != MsymE);
880 ModuleSymbolTable::Symbol Msym = *MsymI++;
881 Skip();
882
883 if (GlobalValue *GV = dyn_cast_if_present<GlobalValue *>(Msym)) {
884 if (Res.Prevailing) {
885 if (Sym.isUndefined())
886 continue;
887 Mod.Keep.push_back(GV);
888 // For symbols re-defined with linker -wrap and -defsym options,
889 // set the linkage to weak to inhibit IPO. The linkage will be
890 // restored by the linker.
891 if (Res.LinkerRedefined)
892 GV->setLinkage(GlobalValue::WeakAnyLinkage);
893
894 GlobalValue::LinkageTypes OriginalLinkage = GV->getLinkage();
895 if (GlobalValue::isLinkOnceLinkage(OriginalLinkage))
896 GV->setLinkage(GlobalValue::getWeakLinkage(
897 GlobalValue::isLinkOnceODRLinkage(OriginalLinkage)));
898 } else if (isa<GlobalObject>(GV) &&
899 (GV->hasLinkOnceODRLinkage() || GV->hasWeakODRLinkage() ||
900 GV->hasAvailableExternallyLinkage()) &&
901 !AliasedGlobals.count(cast<GlobalObject>(GV))) {
902 // Any of the above three types of linkage indicates that the
903 // chosen prevailing symbol will have the same semantics as this copy of
904 // the symbol, so we may be able to link it with available_externally
905 // linkage. We will decide later whether to do that when we link this
906 // module (in linkRegularLTO), based on whether it is undefined.
907 Mod.Keep.push_back(GV);
909 if (GV->hasComdat())
910 NonPrevailingComdats.insert(GV->getComdat());
911 cast<GlobalObject>(GV)->setComdat(nullptr);
912 }
913
914 // Set the 'local' flag based on the linker resolution for this symbol.
916 GV->setDSOLocal(true);
917 if (GV->hasDLLImportStorageClass())
918 GV->setDLLStorageClass(GlobalValue::DLLStorageClassTypes::
919 DefaultStorageClass);
920 }
921 } else if (auto *AS =
922 dyn_cast_if_present<ModuleSymbolTable::AsmSymbol *>(Msym)) {
923 // Collect non-prevailing symbols.
924 if (!Res.Prevailing)
925 NonPrevailingAsmSymbols.insert(AS->first);
926 } else {
927 llvm_unreachable("unknown symbol type");
928 }
929
930 // Common resolution: collect the maximum size/alignment over all commons.
931 // We also record if we see an instance of a common as prevailing, so that
932 // if none is prevailing we can ignore it later.
933 if (Sym.isCommon()) {
934 // FIXME: We should figure out what to do about commons defined by asm.
935 // For now they aren't reported correctly by ModuleSymbolTable.
936 auto &CommonRes = RegularLTO.Commons[std::string(Sym.getIRName())];
937 CommonRes.Size = std::max(CommonRes.Size, Sym.getCommonSize());
938 if (uint32_t SymAlignValue = Sym.getCommonAlignment()) {
939 CommonRes.Alignment =
940 std::max(Align(SymAlignValue), CommonRes.Alignment);
941 }
942 CommonRes.Prevailing |= Res.Prevailing;
943 }
944 }
945
946 if (!M.getComdatSymbolTable().empty())
947 for (GlobalValue &GV : M.global_values())
948 handleNonPrevailingComdat(GV, NonPrevailingComdats);
949
950 // Prepend ".lto_discard <sym>, <sym>*" directive to each module inline asm
951 // block.
952 if (!M.getModuleInlineAsm().empty()) {
953 std::string NewIA = ".lto_discard";
954 if (!NonPrevailingAsmSymbols.empty()) {
955 // Don't dicard a symbol if there is a live .symver for it.
957 M, [&](StringRef Name, StringRef Alias) {
958 if (!NonPrevailingAsmSymbols.count(Alias))
959 NonPrevailingAsmSymbols.erase(Name);
960 });
961 NewIA += " " + llvm::join(NonPrevailingAsmSymbols, ", ");
962 }
963 NewIA += "\n";
964 M.setModuleInlineAsm(NewIA + M.getModuleInlineAsm());
965 }
966
967 assert(MsymI == MsymE);
968 return std::move(Mod);
969}
970
971Error LTO::linkRegularLTO(RegularLTOState::AddedModule Mod,
972 bool LivenessFromIndex) {
973 std::vector<GlobalValue *> Keep;
974 for (GlobalValue *GV : Mod.Keep) {
975 if (LivenessFromIndex && !ThinLTO.CombinedIndex.isGUIDLive(GV->getGUID())) {
976 if (Function *F = dyn_cast<Function>(GV)) {
977 if (DiagnosticOutputFile) {
978 if (Error Err = F->materialize())
979 return Err;
980 OptimizationRemarkEmitter ORE(F, nullptr);
981 ORE.emit(OptimizationRemark(DEBUG_TYPE, "deadfunction", F)
982 << ore::NV("Function", F)
983 << " not added to the combined module ");
984 }
985 }
986 continue;
987 }
988
990 Keep.push_back(GV);
991 continue;
992 }
993
994 // Only link available_externally definitions if we don't already have a
995 // definition.
996 GlobalValue *CombinedGV =
997 RegularLTO.CombinedModule->getNamedValue(GV->getName());
998 if (CombinedGV && !CombinedGV->isDeclaration())
999 continue;
1000
1001 Keep.push_back(GV);
1002 }
1003
1004 return RegularLTO.Mover->move(std::move(Mod.M), Keep, nullptr,
1005 /* IsPerformingImport */ false);
1006}
1007
1008// Add a ThinLTO module to the link.
1009Error LTO::addThinLTO(BitcodeModule BM, ArrayRef<InputFile::Symbol> Syms,
1010 const SymbolResolution *&ResI,
1011 const SymbolResolution *ResE) {
1012 const SymbolResolution *ResITmp = ResI;
1013 for (const InputFile::Symbol &Sym : Syms) {
1014 assert(ResITmp != ResE);
1015 SymbolResolution Res = *ResITmp++;
1016
1017 if (!Sym.getIRName().empty()) {
1019 Sym.getIRName(), GlobalValue::ExternalLinkage, ""));
1020 if (Res.Prevailing)
1021 ThinLTO.PrevailingModuleForGUID[GUID] = BM.getModuleIdentifier();
1022 }
1023 }
1024
1025 if (Error Err =
1026 BM.readSummary(ThinLTO.CombinedIndex, BM.getModuleIdentifier(),
1027 [&](GlobalValue::GUID GUID) {
1028 return ThinLTO.PrevailingModuleForGUID[GUID] ==
1029 BM.getModuleIdentifier();
1030 }))
1031 return Err;
1032 LLVM_DEBUG(dbgs() << "Module " << BM.getModuleIdentifier() << "\n");
1033
1034 for (const InputFile::Symbol &Sym : Syms) {
1035 assert(ResI != ResE);
1036 SymbolResolution Res = *ResI++;
1037
1038 if (!Sym.getIRName().empty()) {
1040 Sym.getIRName(), GlobalValue::ExternalLinkage, ""));
1041 if (Res.Prevailing) {
1042 assert(ThinLTO.PrevailingModuleForGUID[GUID] ==
1043 BM.getModuleIdentifier());
1044
1045 // For linker redefined symbols (via --wrap or --defsym) we want to
1046 // switch the linkage to `weak` to prevent IPOs from happening.
1047 // Find the summary in the module for this very GV and record the new
1048 // linkage so that we can switch it when we import the GV.
1049 if (Res.LinkerRedefined)
1050 if (auto S = ThinLTO.CombinedIndex.findSummaryInModule(
1051 GUID, BM.getModuleIdentifier()))
1052 S->setLinkage(GlobalValue::WeakAnyLinkage);
1053 }
1054
1055 // If the linker resolved the symbol to a local definition then mark it
1056 // as local in the summary for the module we are adding.
1058 if (auto S = ThinLTO.CombinedIndex.findSummaryInModule(
1059 GUID, BM.getModuleIdentifier())) {
1060 S->setDSOLocal(true);
1061 }
1062 }
1063 }
1064 }
1065
1066 if (!ThinLTO.ModuleMap.insert({BM.getModuleIdentifier(), BM}).second)
1067 return make_error<StringError>(
1068 "Expected at most one ThinLTO module per bitcode file",
1070
1071 if (!Conf.ThinLTOModulesToCompile.empty()) {
1072 if (!ThinLTO.ModulesToCompile)
1073 ThinLTO.ModulesToCompile = ModuleMapType();
1074 // This is a fuzzy name matching where only modules with name containing the
1075 // specified switch values are going to be compiled.
1076 for (const std::string &Name : Conf.ThinLTOModulesToCompile) {
1077 if (BM.getModuleIdentifier().contains(Name)) {
1078 ThinLTO.ModulesToCompile->insert({BM.getModuleIdentifier(), BM});
1079 llvm::errs() << "[ThinLTO] Selecting " << BM.getModuleIdentifier()
1080 << " to compile\n";
1081 }
1082 }
1083 }
1084
1085 return Error::success();
1086}
1087
1088unsigned LTO::getMaxTasks() const {
1089 CalledGetMaxTasks = true;
1090 auto ModuleCount = ThinLTO.ModulesToCompile ? ThinLTO.ModulesToCompile->size()
1091 : ThinLTO.ModuleMap.size();
1092 return RegularLTO.ParallelCodeGenParallelismLevel + ModuleCount;
1093}
1094
1095// If only some of the modules were split, we cannot correctly handle
1096// code that contains type tests or type checked loads.
1097Error LTO::checkPartiallySplit() {
1098 if (!ThinLTO.CombinedIndex.partiallySplitLTOUnits())
1099 return Error::success();
1100
1101 Function *TypeTestFunc = RegularLTO.CombinedModule->getFunction(
1102 Intrinsic::getName(Intrinsic::type_test));
1103 Function *TypeCheckedLoadFunc = RegularLTO.CombinedModule->getFunction(
1104 Intrinsic::getName(Intrinsic::type_checked_load));
1105 Function *TypeCheckedLoadRelativeFunc =
1106 RegularLTO.CombinedModule->getFunction(
1107 Intrinsic::getName(Intrinsic::type_checked_load_relative));
1108
1109 // First check if there are type tests / type checked loads in the
1110 // merged regular LTO module IR.
1111 if ((TypeTestFunc && !TypeTestFunc->use_empty()) ||
1112 (TypeCheckedLoadFunc && !TypeCheckedLoadFunc->use_empty()) ||
1113 (TypeCheckedLoadRelativeFunc &&
1114 !TypeCheckedLoadRelativeFunc->use_empty()))
1115 return make_error<StringError>(
1116 "inconsistent LTO Unit splitting (recompile with -fsplit-lto-unit)",
1118
1119 // Otherwise check if there are any recorded in the combined summary from the
1120 // ThinLTO modules.
1121 for (auto &P : ThinLTO.CombinedIndex) {
1122 for (auto &S : P.second.SummaryList) {
1123 auto *FS = dyn_cast<FunctionSummary>(S.get());
1124 if (!FS)
1125 continue;
1126 if (!FS->type_test_assume_vcalls().empty() ||
1127 !FS->type_checked_load_vcalls().empty() ||
1128 !FS->type_test_assume_const_vcalls().empty() ||
1129 !FS->type_checked_load_const_vcalls().empty() ||
1130 !FS->type_tests().empty())
1131 return make_error<StringError>(
1132 "inconsistent LTO Unit splitting (recompile with -fsplit-lto-unit)",
1134 }
1135 }
1136 return Error::success();
1137}
1138
1140 // Compute "dead" symbols, we don't want to import/export these!
1141 DenseSet<GlobalValue::GUID> GUIDPreservedSymbols;
1142 DenseMap<GlobalValue::GUID, PrevailingType> GUIDPrevailingResolutions;
1143 for (auto &Res : *GlobalResolutions) {
1144 // Normally resolution have IR name of symbol. We can do nothing here
1145 // otherwise. See comments in GlobalResolution struct for more details.
1146 if (Res.second.IRName.empty())
1147 continue;
1148
1150 GlobalValue::dropLLVMManglingEscape(Res.second.IRName));
1151
1152 if (Res.second.VisibleOutsideSummary && Res.second.Prevailing)
1153 GUIDPreservedSymbols.insert(GUID);
1154
1155 if (Res.second.ExportDynamic)
1156 DynamicExportSymbols.insert(GUID);
1157
1158 GUIDPrevailingResolutions[GUID] =
1160 }
1161
1162 auto isPrevailing = [&](GlobalValue::GUID G) {
1163 auto It = GUIDPrevailingResolutions.find(G);
1164 if (It == GUIDPrevailingResolutions.end())
1166 return It->second;
1167 };
1168 computeDeadSymbolsWithConstProp(ThinLTO.CombinedIndex, GUIDPreservedSymbols,
1169 isPrevailing, Conf.OptLevel > 0);
1170
1171 // Setup output file to emit statistics.
1172 auto StatsFileOrErr = setupStatsFile(Conf.StatsFile);
1173 if (!StatsFileOrErr)
1174 return StatsFileOrErr.takeError();
1175 std::unique_ptr<ToolOutputFile> StatsFile = std::move(StatsFileOrErr.get());
1176
1177 // TODO: Ideally this would be controlled automatically by detecting that we
1178 // are linking with an allocator that supports these interfaces, rather than
1179 // an internal option (which would still be needed for tests, however). For
1180 // example, if the library exported a symbol like __malloc_hot_cold the linker
1181 // could recognize that and set a flag in the lto::Config.
1183 ThinLTO.CombinedIndex.setWithSupportsHotColdNew();
1184
1185 Error Result = runRegularLTO(AddStream);
1186 if (!Result)
1187 // This will reset the GlobalResolutions optional once done with it to
1188 // reduce peak memory before importing.
1189 Result = runThinLTO(AddStream, Cache, GUIDPreservedSymbols);
1190
1191 if (StatsFile)
1192 PrintStatisticsJSON(StatsFile->os());
1193
1194 return Result;
1195}
1196
1198 const ModuleSummaryIndex &Index) {
1199 if (Index.withSupportsHotColdNew())
1200 return;
1201
1202 // The profile matcher applies hotness attributes directly for allocations,
1203 // and those will cause us to generate calls to the hot/cold interfaces
1204 // unconditionally. If supports-hot-cold-new was not enabled in the LTO
1205 // link then assume we don't want these calls (e.g. not linking with
1206 // the appropriate library, or otherwise trying to disable this behavior).
1207 for (auto &F : Mod) {
1208 for (auto &BB : F) {
1209 for (auto &I : BB) {
1210 auto *CI = dyn_cast<CallBase>(&I);
1211 if (!CI)
1212 continue;
1213 if (CI->hasFnAttr("memprof"))
1214 CI->removeFnAttr("memprof");
1215 // Strip off all memprof metadata as it is no longer needed.
1216 // Importantly, this avoids the addition of new memprof attributes
1217 // after inlining propagation.
1218 // TODO: If we support additional types of MemProf metadata beyond hot
1219 // and cold, we will need to update the metadata based on the allocator
1220 // APIs supported instead of completely stripping all.
1221 CI->setMetadata(LLVMContext::MD_memprof, nullptr);
1222 CI->setMetadata(LLVMContext::MD_callsite, nullptr);
1223 }
1224 }
1225 }
1226}
1227
1228Error LTO::runRegularLTO(AddStreamFn AddStream) {
1229 // Setup optimization remarks.
1230 auto DiagFileOrErr = lto::setupLLVMOptimizationRemarks(
1231 RegularLTO.CombinedModule->getContext(), Conf.RemarksFilename,
1234 LLVM_DEBUG(dbgs() << "Running regular LTO\n");
1235 if (!DiagFileOrErr)
1236 return DiagFileOrErr.takeError();
1237 DiagnosticOutputFile = std::move(*DiagFileOrErr);
1238
1239 // Finalize linking of regular LTO modules containing summaries now that
1240 // we have computed liveness information.
1241 for (auto &M : RegularLTO.ModsWithSummaries)
1242 if (Error Err = linkRegularLTO(std::move(M),
1243 /*LivenessFromIndex=*/true))
1244 return Err;
1245
1246 // Ensure we don't have inconsistently split LTO units with type tests.
1247 // FIXME: this checks both LTO and ThinLTO. It happens to work as we take
1248 // this path both cases but eventually this should be split into two and
1249 // do the ThinLTO checks in `runThinLTO`.
1250 if (Error Err = checkPartiallySplit())
1251 return Err;
1252
1253 // Make sure commons have the right size/alignment: we kept the largest from
1254 // all the prevailing when adding the inputs, and we apply it here.
1255 const DataLayout &DL = RegularLTO.CombinedModule->getDataLayout();
1256 for (auto &I : RegularLTO.Commons) {
1257 if (!I.second.Prevailing)
1258 // Don't do anything if no instance of this common was prevailing.
1259 continue;
1260 GlobalVariable *OldGV = RegularLTO.CombinedModule->getNamedGlobal(I.first);
1261 if (OldGV && DL.getTypeAllocSize(OldGV->getValueType()) == I.second.Size) {
1262 // Don't create a new global if the type is already correct, just make
1263 // sure the alignment is correct.
1264 OldGV->setAlignment(I.second.Alignment);
1265 continue;
1266 }
1267 ArrayType *Ty =
1268 ArrayType::get(Type::getInt8Ty(RegularLTO.Ctx), I.second.Size);
1269 auto *GV = new GlobalVariable(*RegularLTO.CombinedModule, Ty, false,
1272 GV->setAlignment(I.second.Alignment);
1273 if (OldGV) {
1274 OldGV->replaceAllUsesWith(GV);
1275 GV->takeName(OldGV);
1276 OldGV->eraseFromParent();
1277 } else {
1278 GV->setName(I.first);
1279 }
1280 }
1281
1282 updateMemProfAttributes(*RegularLTO.CombinedModule, ThinLTO.CombinedIndex);
1283
1284 bool WholeProgramVisibilityEnabledInLTO =
1286 // If validation is enabled, upgrade visibility only when all vtables
1287 // have typeinfos.
1289
1290 // This returns true when the name is local or not defined. Locals are
1291 // expected to be handled separately.
1292 auto IsVisibleToRegularObj = [&](StringRef name) {
1293 auto It = GlobalResolutions->find(name);
1294 return (It == GlobalResolutions->end() || It->second.VisibleOutsideSummary);
1295 };
1296
1297 // If allowed, upgrade public vcall visibility metadata to linkage unit
1298 // visibility before whole program devirtualization in the optimizer.
1300 *RegularLTO.CombinedModule, WholeProgramVisibilityEnabledInLTO,
1301 DynamicExportSymbols, Conf.ValidateAllVtablesHaveTypeInfos,
1302 IsVisibleToRegularObj);
1303 updatePublicTypeTestCalls(*RegularLTO.CombinedModule,
1304 WholeProgramVisibilityEnabledInLTO);
1305
1306 if (Conf.PreOptModuleHook &&
1307 !Conf.PreOptModuleHook(0, *RegularLTO.CombinedModule))
1308 return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
1309
1310 if (!Conf.CodeGenOnly) {
1311 for (const auto &R : *GlobalResolutions) {
1312 GlobalValue *GV =
1313 RegularLTO.CombinedModule->getNamedValue(R.second.IRName);
1314 if (!R.second.isPrevailingIRSymbol())
1315 continue;
1316 if (R.second.Partition != 0 &&
1317 R.second.Partition != GlobalResolution::External)
1318 continue;
1319
1320 // Ignore symbols defined in other partitions.
1321 // Also skip declarations, which are not allowed to have internal linkage.
1322 if (!GV || GV->hasLocalLinkage() || GV->isDeclaration())
1323 continue;
1324
1325 // Symbols that are marked DLLImport or DLLExport should not be
1326 // internalized, as they are either externally visible or referencing
1327 // external symbols. Symbols that have AvailableExternally or Appending
1328 // linkage might be used by future passes and should be kept as is.
1329 // These linkages are seen in Unified regular LTO, because the process
1330 // of creating split LTO units introduces symbols with that linkage into
1331 // one of the created modules. Normally, only the ThinLTO backend would
1332 // compile this module, but Unified Regular LTO processes both
1333 // modules created by the splitting process as regular LTO modules.
1334 if ((LTOMode == LTOKind::LTOK_UnifiedRegular) &&
1337 continue;
1338
1339 GV->setUnnamedAddr(R.second.UnnamedAddr ? GlobalValue::UnnamedAddr::Global
1341 if (EnableLTOInternalization && R.second.Partition == 0)
1343 }
1344
1345 if (Conf.PostInternalizeModuleHook &&
1346 !Conf.PostInternalizeModuleHook(0, *RegularLTO.CombinedModule))
1347 return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
1348 }
1349
1350 if (!RegularLTO.EmptyCombinedModule || Conf.AlwaysEmitRegularLTOObj) {
1351 if (Error Err =
1352 backend(Conf, AddStream, RegularLTO.ParallelCodeGenParallelismLevel,
1353 *RegularLTO.CombinedModule, ThinLTO.CombinedIndex))
1354 return Err;
1355 }
1356
1357 return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
1358}
1359
1360static const char *libcallRoutineNames[] = {
1361#define HANDLE_LIBCALL(code, name) name,
1362#include "llvm/IR/RuntimeLibcalls.def"
1363#undef HANDLE_LIBCALL
1364};
1365
1368}
1369
1370/// This class defines the interface to the ThinLTO backend.
1372protected:
1373 const Config &Conf;
1378
1379public:
1381 const Config &Conf, ModuleSummaryIndex &CombinedIndex,
1382 const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1383 lto::IndexWriteCallback OnWrite, bool ShouldEmitImportsFiles)
1384 : Conf(Conf), CombinedIndex(CombinedIndex),
1385 ModuleToDefinedGVSummaries(ModuleToDefinedGVSummaries),
1386 OnWrite(OnWrite), ShouldEmitImportsFiles(ShouldEmitImportsFiles) {}
1387
1388 virtual ~ThinBackendProc() = default;
1389 virtual Error start(
1390 unsigned Task, BitcodeModule BM,
1391 const FunctionImporter::ImportMapTy &ImportList,
1392 const FunctionImporter::ExportSetTy &ExportList,
1393 const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
1395 virtual Error wait() = 0;
1396 virtual unsigned getThreadCount() = 0;
1397
1398 // Write sharded indices and (optionally) imports to disk
1400 llvm::StringRef ModulePath,
1401 const std::string &NewModulePath) {
1402 std::map<std::string, GVSummaryMapTy> ModuleToSummariesForIndex;
1403 GVSummaryPtrSet DeclarationSummaries;
1404
1405 std::error_code EC;
1406 gatherImportedSummariesForModule(ModulePath, ModuleToDefinedGVSummaries,
1407 ImportList, ModuleToSummariesForIndex,
1408 DeclarationSummaries);
1409
1410 raw_fd_ostream OS(NewModulePath + ".thinlto.bc", EC,
1411 sys::fs::OpenFlags::OF_None);
1412 if (EC)
1413 return errorCodeToError(EC);
1414
1415 writeIndexToFile(CombinedIndex, OS, &ModuleToSummariesForIndex,
1416 &DeclarationSummaries);
1417
1418 if (ShouldEmitImportsFiles) {
1419 EC = EmitImportsFiles(ModulePath, NewModulePath + ".imports",
1420 ModuleToSummariesForIndex);
1421 if (EC)
1422 return errorCodeToError(EC);
1423 }
1424 return Error::success();
1425 }
1426};
1427
1428namespace {
1429class InProcessThinBackend : public ThinBackendProc {
1430 DefaultThreadPool BackendThreadPool;
1431 AddStreamFn AddStream;
1432 FileCache Cache;
1433 std::set<GlobalValue::GUID> CfiFunctionDefs;
1434 std::set<GlobalValue::GUID> CfiFunctionDecls;
1435
1436 std::optional<Error> Err;
1437 std::mutex ErrMu;
1438
1439 bool ShouldEmitIndexFiles;
1440
1441public:
1442 InProcessThinBackend(
1443 const Config &Conf, ModuleSummaryIndex &CombinedIndex,
1444 ThreadPoolStrategy ThinLTOParallelism,
1445 const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1446 AddStreamFn AddStream, FileCache Cache, lto::IndexWriteCallback OnWrite,
1447 bool ShouldEmitIndexFiles, bool ShouldEmitImportsFiles)
1448 : ThinBackendProc(Conf, CombinedIndex, ModuleToDefinedGVSummaries,
1449 OnWrite, ShouldEmitImportsFiles),
1450 BackendThreadPool(ThinLTOParallelism), AddStream(std::move(AddStream)),
1451 Cache(std::move(Cache)), ShouldEmitIndexFiles(ShouldEmitIndexFiles) {
1452 for (auto &Name : CombinedIndex.cfiFunctionDefs())
1453 CfiFunctionDefs.insert(
1455 for (auto &Name : CombinedIndex.cfiFunctionDecls())
1456 CfiFunctionDecls.insert(
1458 }
1459
1460 Error runThinLTOBackendThread(
1461 AddStreamFn AddStream, FileCache Cache, unsigned Task, BitcodeModule BM,
1462 ModuleSummaryIndex &CombinedIndex,
1463 const FunctionImporter::ImportMapTy &ImportList,
1464 const FunctionImporter::ExportSetTy &ExportList,
1465 const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
1466 const GVSummaryMapTy &DefinedGlobals,
1468 auto RunThinBackend = [&](AddStreamFn AddStream) {
1469 LTOLLVMContext BackendContext(Conf);
1470 Expected<std::unique_ptr<Module>> MOrErr = BM.parseModule(BackendContext);
1471 if (!MOrErr)
1472 return MOrErr.takeError();
1473
1474 return thinBackend(Conf, Task, AddStream, **MOrErr, CombinedIndex,
1475 ImportList, DefinedGlobals, &ModuleMap);
1476 };
1477
1478 auto ModuleID = BM.getModuleIdentifier();
1479
1480 if (ShouldEmitIndexFiles) {
1481 if (auto E = emitFiles(ImportList, ModuleID, ModuleID.str()))
1482 return E;
1483 }
1484
1485 if (!Cache || !CombinedIndex.modulePaths().count(ModuleID) ||
1486 all_of(CombinedIndex.getModuleHash(ModuleID),
1487 [](uint32_t V) { return V == 0; }))
1488 // Cache disabled or no entry for this module in the combined index or
1489 // no module hash.
1490 return RunThinBackend(AddStream);
1491
1493 // The module may be cached, this helps handling it.
1494 computeLTOCacheKey(Key, Conf, CombinedIndex, ModuleID, ImportList,
1495 ExportList, ResolvedODR, DefinedGlobals, CfiFunctionDefs,
1496 CfiFunctionDecls);
1497 Expected<AddStreamFn> CacheAddStreamOrErr = Cache(Task, Key, ModuleID);
1498 if (Error Err = CacheAddStreamOrErr.takeError())
1499 return Err;
1500 AddStreamFn &CacheAddStream = *CacheAddStreamOrErr;
1501 if (CacheAddStream)
1502 return RunThinBackend(CacheAddStream);
1503
1504 return Error::success();
1505 }
1506
1507 Error start(
1508 unsigned Task, BitcodeModule BM,
1509 const FunctionImporter::ImportMapTy &ImportList,
1510 const FunctionImporter::ExportSetTy &ExportList,
1511 const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
1512 MapVector<StringRef, BitcodeModule> &ModuleMap) override {
1513 StringRef ModulePath = BM.getModuleIdentifier();
1514 assert(ModuleToDefinedGVSummaries.count(ModulePath));
1515 const GVSummaryMapTy &DefinedGlobals =
1516 ModuleToDefinedGVSummaries.find(ModulePath)->second;
1517 BackendThreadPool.async(
1518 [=](BitcodeModule BM, ModuleSummaryIndex &CombinedIndex,
1519 const FunctionImporter::ImportMapTy &ImportList,
1520 const FunctionImporter::ExportSetTy &ExportList,
1521 const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>
1522 &ResolvedODR,
1523 const GVSummaryMapTy &DefinedGlobals,
1525 if (LLVM_ENABLE_THREADS && Conf.TimeTraceEnabled)
1527 "thin backend");
1528 Error E = runThinLTOBackendThread(
1529 AddStream, Cache, Task, BM, CombinedIndex, ImportList, ExportList,
1530 ResolvedODR, DefinedGlobals, ModuleMap);
1531 if (E) {
1532 std::unique_lock<std::mutex> L(ErrMu);
1533 if (Err)
1534 Err = joinErrors(std::move(*Err), std::move(E));
1535 else
1536 Err = std::move(E);
1537 }
1538 if (LLVM_ENABLE_THREADS && Conf.TimeTraceEnabled)
1540 },
1541 BM, std::ref(CombinedIndex), std::ref(ImportList), std::ref(ExportList),
1542 std::ref(ResolvedODR), std::ref(DefinedGlobals), std::ref(ModuleMap));
1543
1544 if (OnWrite)
1545 OnWrite(std::string(ModulePath));
1546 return Error::success();
1547 }
1548
1549 Error wait() override {
1550 BackendThreadPool.wait();
1551 if (Err)
1552 return std::move(*Err);
1553 else
1554 return Error::success();
1555 }
1556
1557 unsigned getThreadCount() override {
1558 return BackendThreadPool.getMaxConcurrency();
1559 }
1560};
1561} // end anonymous namespace
1562
1565 bool ShouldEmitIndexFiles,
1566 bool ShouldEmitImportsFiles) {
1567 return
1568 [=](const Config &Conf, ModuleSummaryIndex &CombinedIndex,
1569 const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1570 AddStreamFn AddStream, FileCache Cache) {
1571 return std::make_unique<InProcessThinBackend>(
1572 Conf, CombinedIndex, Parallelism, ModuleToDefinedGVSummaries,
1573 AddStream, Cache, OnWrite, ShouldEmitIndexFiles,
1574 ShouldEmitImportsFiles);
1575 };
1576}
1577
1579 if (!TheTriple.isOSDarwin())
1580 return "";
1581 if (TheTriple.getArch() == Triple::x86_64)
1582 return "core2";
1583 if (TheTriple.getArch() == Triple::x86)
1584 return "yonah";
1585 if (TheTriple.isArm64e())
1586 return "apple-a12";
1587 if (TheTriple.getArch() == Triple::aarch64 ||
1588 TheTriple.getArch() == Triple::aarch64_32)
1589 return "cyclone";
1590 return "";
1591}
1592
1593// Given the original \p Path to an output file, replace any path
1594// prefix matching \p OldPrefix with \p NewPrefix. Also, create the
1595// resulting directory if it does not yet exist.
1597 StringRef NewPrefix) {
1598 if (OldPrefix.empty() && NewPrefix.empty())
1599 return std::string(Path);
1600 SmallString<128> NewPath(Path);
1601 llvm::sys::path::replace_path_prefix(NewPath, OldPrefix, NewPrefix);
1602 StringRef ParentPath = llvm::sys::path::parent_path(NewPath.str());
1603 if (!ParentPath.empty()) {
1604 // Make sure the new directory exists, creating it if necessary.
1605 if (std::error_code EC = llvm::sys::fs::create_directories(ParentPath))
1606 llvm::errs() << "warning: could not create directory '" << ParentPath
1607 << "': " << EC.message() << '\n';
1608 }
1609 return std::string(NewPath);
1610}
1611
1612namespace {
1613class WriteIndexesThinBackend : public ThinBackendProc {
1614 std::string OldPrefix, NewPrefix, NativeObjectPrefix;
1615 raw_fd_ostream *LinkedObjectsFile;
1616
1617public:
1618 WriteIndexesThinBackend(
1619 const Config &Conf, ModuleSummaryIndex &CombinedIndex,
1620 const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1621 std::string OldPrefix, std::string NewPrefix,
1622 std::string NativeObjectPrefix, bool ShouldEmitImportsFiles,
1623 raw_fd_ostream *LinkedObjectsFile, lto::IndexWriteCallback OnWrite)
1624 : ThinBackendProc(Conf, CombinedIndex, ModuleToDefinedGVSummaries,
1625 OnWrite, ShouldEmitImportsFiles),
1626 OldPrefix(OldPrefix), NewPrefix(NewPrefix),
1627 NativeObjectPrefix(NativeObjectPrefix),
1628 LinkedObjectsFile(LinkedObjectsFile) {}
1629
1630 Error start(
1631 unsigned Task, BitcodeModule BM,
1632 const FunctionImporter::ImportMapTy &ImportList,
1633 const FunctionImporter::ExportSetTy &ExportList,
1634 const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
1635 MapVector<StringRef, BitcodeModule> &ModuleMap) override {
1636 StringRef ModulePath = BM.getModuleIdentifier();
1637 std::string NewModulePath =
1638 getThinLTOOutputFile(ModulePath, OldPrefix, NewPrefix);
1639
1640 if (LinkedObjectsFile) {
1641 std::string ObjectPrefix =
1642 NativeObjectPrefix.empty() ? NewPrefix : NativeObjectPrefix;
1643 std::string LinkedObjectsFilePath =
1644 getThinLTOOutputFile(ModulePath, OldPrefix, ObjectPrefix);
1645 *LinkedObjectsFile << LinkedObjectsFilePath << '\n';
1646 }
1647
1648 if (auto E = emitFiles(ImportList, ModulePath, NewModulePath))
1649 return E;
1650
1651 if (OnWrite)
1652 OnWrite(std::string(ModulePath));
1653 return Error::success();
1654 }
1655
1656 Error wait() override { return Error::success(); }
1657
1658 // WriteIndexesThinBackend should always return 1 to prevent module
1659 // re-ordering and avoid non-determinism in the final link.
1660 unsigned getThreadCount() override { return 1; }
1661};
1662} // end anonymous namespace
1663
1665 std::string OldPrefix, std::string NewPrefix,
1666 std::string NativeObjectPrefix, bool ShouldEmitImportsFiles,
1667 raw_fd_ostream *LinkedObjectsFile, IndexWriteCallback OnWrite) {
1668 return
1669 [=](const Config &Conf, ModuleSummaryIndex &CombinedIndex,
1670 const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1671 AddStreamFn AddStream, FileCache Cache) {
1672 return std::make_unique<WriteIndexesThinBackend>(
1673 Conf, CombinedIndex, ModuleToDefinedGVSummaries, OldPrefix,
1674 NewPrefix, NativeObjectPrefix, ShouldEmitImportsFiles,
1675 LinkedObjectsFile, OnWrite);
1676 };
1677}
1678
1679Error LTO::runThinLTO(AddStreamFn AddStream, FileCache Cache,
1680 const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols) {
1681 LLVM_DEBUG(dbgs() << "Running ThinLTO\n");
1682 ThinLTO.CombinedIndex.releaseTemporaryMemory();
1683 timeTraceProfilerBegin("ThinLink", StringRef(""));
1684 auto TimeTraceScopeExit = llvm::make_scope_exit([]() {
1687 });
1688 if (ThinLTO.ModuleMap.empty())
1689 return Error::success();
1690
1691 if (ThinLTO.ModulesToCompile && ThinLTO.ModulesToCompile->empty()) {
1692 llvm::errs() << "warning: [ThinLTO] No module compiled\n";
1693 return Error::success();
1694 }
1695
1696 if (Conf.CombinedIndexHook &&
1697 !Conf.CombinedIndexHook(ThinLTO.CombinedIndex, GUIDPreservedSymbols))
1698 return Error::success();
1699
1700 // Collect for each module the list of function it defines (GUID ->
1701 // Summary).
1702 DenseMap<StringRef, GVSummaryMapTy> ModuleToDefinedGVSummaries(
1703 ThinLTO.ModuleMap.size());
1704 ThinLTO.CombinedIndex.collectDefinedGVSummariesPerModule(
1705 ModuleToDefinedGVSummaries);
1706 // Create entries for any modules that didn't have any GV summaries
1707 // (either they didn't have any GVs to start with, or we suppressed
1708 // generation of the summaries because they e.g. had inline assembly
1709 // uses that couldn't be promoted/renamed on export). This is so
1710 // InProcessThinBackend::start can still launch a backend thread, which
1711 // is passed the map of summaries for the module, without any special
1712 // handling for this case.
1713 for (auto &Mod : ThinLTO.ModuleMap)
1714 if (!ModuleToDefinedGVSummaries.count(Mod.first))
1715 ModuleToDefinedGVSummaries.try_emplace(Mod.first);
1716
1717 // Synthesize entry counts for functions in the CombinedIndex.
1718 computeSyntheticCounts(ThinLTO.CombinedIndex);
1719
1721 ThinLTO.ModuleMap.size());
1723 ThinLTO.ModuleMap.size());
1725
1726 if (DumpThinCGSCCs)
1727 ThinLTO.CombinedIndex.dumpSCCs(outs());
1728
1729 std::set<GlobalValue::GUID> ExportedGUIDs;
1730
1731 bool WholeProgramVisibilityEnabledInLTO =
1733 // If validation is enabled, upgrade visibility only when all vtables
1734 // have typeinfos.
1736 if (hasWholeProgramVisibility(WholeProgramVisibilityEnabledInLTO))
1737 ThinLTO.CombinedIndex.setWithWholeProgramVisibility();
1738
1739 // If we're validating, get the vtable symbols that should not be
1740 // upgraded because they correspond to typeIDs outside of index-based
1741 // WPD info.
1742 DenseSet<GlobalValue::GUID> VisibleToRegularObjSymbols;
1743 if (WholeProgramVisibilityEnabledInLTO &&
1745 // This returns true when the name is local or not defined. Locals are
1746 // expected to be handled separately.
1747 auto IsVisibleToRegularObj = [&](StringRef name) {
1748 auto It = GlobalResolutions->find(name);
1749 return (It == GlobalResolutions->end() ||
1750 It->second.VisibleOutsideSummary);
1751 };
1752
1753 getVisibleToRegularObjVtableGUIDs(ThinLTO.CombinedIndex,
1754 VisibleToRegularObjSymbols,
1755 IsVisibleToRegularObj);
1756 }
1757
1758 // If allowed, upgrade public vcall visibility to linkage unit visibility in
1759 // the summaries before whole program devirtualization below.
1761 ThinLTO.CombinedIndex, WholeProgramVisibilityEnabledInLTO,
1762 DynamicExportSymbols, VisibleToRegularObjSymbols);
1763
1764 // Perform index-based WPD. This will return immediately if there are
1765 // no index entries in the typeIdMetadata map (e.g. if we are instead
1766 // performing IR-based WPD in hybrid regular/thin LTO mode).
1767 std::map<ValueInfo, std::vector<VTableSlotSummary>> LocalWPDTargetsMap;
1768 runWholeProgramDevirtOnIndex(ThinLTO.CombinedIndex, ExportedGUIDs,
1769 LocalWPDTargetsMap);
1770
1771 auto isPrevailing = [&](GlobalValue::GUID GUID, const GlobalValueSummary *S) {
1772 return ThinLTO.PrevailingModuleForGUID[GUID] == S->modulePath();
1773 };
1775 MemProfContextDisambiguation ContextDisambiguation;
1776 ContextDisambiguation.run(ThinLTO.CombinedIndex, isPrevailing);
1777 }
1778
1779 // Figure out which symbols need to be internalized. This also needs to happen
1780 // at -O0 because summary-based DCE is implemented using internalization, and
1781 // we must apply DCE consistently with the full LTO module in order to avoid
1782 // undefined references during the final link.
1783 for (auto &Res : *GlobalResolutions) {
1784 // If the symbol does not have external references or it is not prevailing,
1785 // then not need to mark it as exported from a ThinLTO partition.
1786 if (Res.second.Partition != GlobalResolution::External ||
1787 !Res.second.isPrevailingIRSymbol())
1788 continue;
1790 GlobalValue::dropLLVMManglingEscape(Res.second.IRName));
1791 // Mark exported unless index-based analysis determined it to be dead.
1792 if (ThinLTO.CombinedIndex.isGUIDLive(GUID))
1793 ExportedGUIDs.insert(GUID);
1794 }
1795
1796 // Reset the GlobalResolutions to deallocate the associated memory, as there
1797 // are no further accesses. We specifically want to do this before computing
1798 // cross module importing, which adds to peak memory via the computed import
1799 // and export lists.
1800 GlobalResolutions.reset();
1801
1802 if (Conf.OptLevel > 0)
1803 ComputeCrossModuleImport(ThinLTO.CombinedIndex, ModuleToDefinedGVSummaries,
1804 isPrevailing, ImportLists, ExportLists);
1805
1806 // Any functions referenced by the jump table in the regular LTO object must
1807 // be exported.
1808 for (auto &Def : ThinLTO.CombinedIndex.cfiFunctionDefs())
1809 ExportedGUIDs.insert(
1811 for (auto &Decl : ThinLTO.CombinedIndex.cfiFunctionDecls())
1812 ExportedGUIDs.insert(
1814
1815 auto isExported = [&](StringRef ModuleIdentifier, ValueInfo VI) {
1816 const auto &ExportList = ExportLists.find(ModuleIdentifier);
1817 return (ExportList != ExportLists.end() && ExportList->second.count(VI)) ||
1818 ExportedGUIDs.count(VI.getGUID());
1819 };
1820
1821 // Update local devirtualized targets that were exported by cross-module
1822 // importing or by other devirtualizations marked in the ExportedGUIDs set.
1823 updateIndexWPDForExports(ThinLTO.CombinedIndex, isExported,
1824 LocalWPDTargetsMap);
1825
1826 thinLTOInternalizeAndPromoteInIndex(ThinLTO.CombinedIndex, isExported,
1827 isPrevailing);
1828
1829 auto recordNewLinkage = [&](StringRef ModuleIdentifier,
1831 GlobalValue::LinkageTypes NewLinkage) {
1832 ResolvedODR[ModuleIdentifier][GUID] = NewLinkage;
1833 };
1834 thinLTOResolvePrevailingInIndex(Conf, ThinLTO.CombinedIndex, isPrevailing,
1835 recordNewLinkage, GUIDPreservedSymbols);
1836
1837 thinLTOPropagateFunctionAttrs(ThinLTO.CombinedIndex, isPrevailing);
1838
1839 generateParamAccessSummary(ThinLTO.CombinedIndex);
1840
1843
1844 TimeTraceScopeExit.release();
1845
1846 std::unique_ptr<ThinBackendProc> BackendProc =
1847 ThinLTO.Backend(Conf, ThinLTO.CombinedIndex, ModuleToDefinedGVSummaries,
1848 AddStream, Cache);
1849
1850 auto &ModuleMap =
1851 ThinLTO.ModulesToCompile ? *ThinLTO.ModulesToCompile : ThinLTO.ModuleMap;
1852
1853 auto ProcessOneModule = [&](int I) -> Error {
1854 auto &Mod = *(ModuleMap.begin() + I);
1855 // Tasks 0 through ParallelCodeGenParallelismLevel-1 are reserved for
1856 // combined module and parallel code generation partitions.
1857 return BackendProc->start(RegularLTO.ParallelCodeGenParallelismLevel + I,
1858 Mod.second, ImportLists[Mod.first],
1859 ExportLists[Mod.first], ResolvedODR[Mod.first],
1860 ThinLTO.ModuleMap);
1861 };
1862
1863 if (BackendProc->getThreadCount() == 1) {
1864 // Process the modules in the order they were provided on the command-line.
1865 // It is important for this codepath to be used for WriteIndexesThinBackend,
1866 // to ensure the emitted LinkedObjectsFile lists ThinLTO objects in the same
1867 // order as the inputs, which otherwise would affect the final link order.
1868 for (int I = 0, E = ModuleMap.size(); I != E; ++I)
1869 if (Error E = ProcessOneModule(I))
1870 return E;
1871 } else {
1872 // When executing in parallel, process largest bitsize modules first to
1873 // improve parallelism, and avoid starving the thread pool near the end.
1874 // This saves about 15 sec on a 36-core machine while link `clang.exe` (out
1875 // of 100 sec).
1876 std::vector<BitcodeModule *> ModulesVec;
1877 ModulesVec.reserve(ModuleMap.size());
1878 for (auto &Mod : ModuleMap)
1879 ModulesVec.push_back(&Mod.second);
1880 for (int I : generateModulesOrdering(ModulesVec))
1881 if (Error E = ProcessOneModule(I))
1882 return E;
1883 }
1884 return BackendProc->wait();
1885}
1886
1890 std::optional<uint64_t> RemarksHotnessThreshold, int Count) {
1891 std::string Filename = std::string(RemarksFilename);
1892 // For ThinLTO, file.opt.<format> becomes
1893 // file.opt.<format>.thin.<num>.<format>.
1894 if (!Filename.empty() && Count != -1)
1895 Filename =
1896 (Twine(Filename) + ".thin." + llvm::utostr(Count) + "." + RemarksFormat)
1897 .str();
1898
1899 auto ResultOrErr = llvm::setupLLVMOptimizationRemarks(
1900 Context, Filename, RemarksPasses, RemarksFormat, RemarksWithHotness,
1902 if (Error E = ResultOrErr.takeError())
1903 return std::move(E);
1904
1905 if (*ResultOrErr)
1906 (*ResultOrErr)->keep();
1907
1908 return ResultOrErr;
1909}
1910
1913 // Setup output file to emit statistics.
1914 if (StatsFilename.empty())
1915 return nullptr;
1916
1918 std::error_code EC;
1919 auto StatsFile =
1920 std::make_unique<ToolOutputFile>(StatsFilename, EC, sys::fs::OF_None);
1921 if (EC)
1922 return errorCodeToError(EC);
1923
1924 StatsFile->keep();
1925 return std::move(StatsFile);
1926}
1927
1928// Compute the ordering we will process the inputs: the rough heuristic here
1929// is to sort them per size so that the largest module get schedule as soon as
1930// possible. This is purely a compile-time optimization.
1932 auto Seq = llvm::seq<int>(0, R.size());
1933 std::vector<int> ModulesOrdering(Seq.begin(), Seq.end());
1934 llvm::sort(ModulesOrdering, [&](int LeftIndex, int RightIndex) {
1935 auto LSize = R[LeftIndex]->getBuffer().size();
1936 auto RSize = R[RightIndex]->getBuffer().size();
1937 return LSize > RSize;
1938 });
1939 return ModulesOrdering;
1940}
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define LLVM_DEBUG(X)
Definition: Debug.h:101
std::string Name
Symbol * Sym
Definition: ELF_riscv.cpp:479
#define DEBUG_TYPE
static void writeToResolutionFile(raw_ostream &OS, InputFile *Input, ArrayRef< SymbolResolution > Res)
Definition: LTO.cpp:682
static void thinLTOResolvePrevailingGUID(const Config &C, ValueInfo VI, DenseSet< GlobalValueSummary * > &GlobalInvolvedWithAlias, function_ref< bool(GlobalValue::GUID, const GlobalValueSummary *)> isPrevailing, function_ref< void(StringRef, GlobalValue::GUID, GlobalValue::LinkageTypes)> recordNewLinkage, const DenseSet< GlobalValue::GUID > &GUIDPreservedSymbols)
Definition: LTO.cpp:360
static void handleNonPrevailingComdat(GlobalValue &GV, std::set< const Comdat * > &NonPrevailingComdats)
Definition: LTO.cpp:794
static cl::opt< bool > DumpThinCGSCCs("dump-thin-cg-sccs", cl::init(false), cl::Hidden, cl::desc("Dump the SCCs in the ThinLTO index's callgraph"))
static void thinLTOInternalizeAndPromoteGUID(ValueInfo VI, function_ref< bool(StringRef, ValueInfo)> isExported, function_ref< bool(GlobalValue::GUID, const GlobalValueSummary *)> isPrevailing)
Definition: LTO.cpp:459
cl::opt< bool > UseNewDbgInfoFormat
static const char * libcallRoutineNames[]
Definition: LTO.cpp:1360
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
#define G(x, y, z)
Definition: MD5.cpp:56
This file contains the declarations for metadata subclasses.
#define P(N)
llvm::cl::opt< bool > UseNewDbgInfoFormat
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static const char * name
Definition: SMEABIPass.cpp:50
raw_pwrite_stream & OS
This file defines the make_scope_exit function, which executes user-defined cleanup logic at scope ex...
This file defines the SmallSet class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
This file contains some functions that are useful when dealing with strings.
This pass exposes codegen information to IR-level passes.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
iterator end() const
Definition: ArrayRef.h:154
iterator begin() const
Definition: ArrayRef.h:153
static ArrayType * get(Type *ElementType, uint64_t NumElements)
This static method is the primary way to construct an ArrayType.
Definition: Type.cpp:647
Represents a module in a bitcode file.
StringRef getModuleIdentifier() const
Error readSummary(ModuleSummaryIndex &CombinedIndex, StringRef ModulePath, std::function< bool(GlobalValue::GUID)> IsPrevailing=nullptr)
Parse the specified bitcode buffer and merge its module summary index into CombinedIndex.
Expected< std::unique_ptr< Module > > parseModule(LLVMContext &Context, ParserCallbacks Callbacks={})
Read the entire bitcode module and return it.
Expected< std::unique_ptr< Module > > getLazyModule(LLVMContext &Context, bool ShouldLazyLoadMetadata, bool IsImporting, ParserCallbacks Callbacks={})
Read the bitcode module and prepare for lazy deserialization of function bodies.
static ConstantAggregateZero * get(Type *Ty)
Definition: Constants.cpp:1650
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:110
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:155
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&... Args)
Definition: DenseMap.h:235
unsigned size() const
Definition: DenseMap.h:99
iterator begin()
Definition: DenseMap.h:75
size_type count(const_arg_type_t< KeyT > Val) const
Return 1 if the specified key is in the map, 0 otherwise.
Definition: DenseMap.h:151
iterator end()
Definition: DenseMap.h:84
Implements a dense probed hash-table based set.
Definition: DenseSet.h:271
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
static ErrorSuccess success()
Create a success value.
Definition: Error.h:337
Tagged union holding either a T or a Error.
Definition: Error.h:481
Error takeError()
Take ownership of the stored error.
Definition: Error.h:608
std::unordered_map< GlobalValue::GUID, GlobalValueSummary::ImportKind > FunctionsToImportTy
The functions to import from a source module and their import type.
void setAlignment(Align Align)
Sets the alignment attribute of the GlobalObject.
Definition: Globals.cpp:137
Function and variable summary information to aid decisions and implementation of importing.
static bool isAppendingLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:400
static bool isExternalWeakLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:412
static bool isLocalLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:409
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition: Globals.cpp:290
void setUnnamedAddr(UnnamedAddr Val)
Definition: GlobalValue.h:231
bool hasLocalLinkage() const
Definition: GlobalValue.h:528
static StringRef dropLLVMManglingEscape(StringRef Name)
If the given string begins with the GlobalValue name mangling escape character '\1',...
Definition: GlobalValue.h:567
const Comdat * getComdat() const
Definition: Globals.cpp:193
static bool isLinkOnceLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:388
static GUID getGUID(StringRef GlobalName)
Return a 64-bit global unique ID constructed from global value name (i.e.
Definition: Globals.cpp:75
void setLinkage(LinkageTypes LT)
Definition: GlobalValue.h:537
DLLStorageClassTypes
Storage classes of global values for PE targets.
Definition: GlobalValue.h:73
GUID getGUID() const
Return a 64-bit global unique ID constructed from global value name (i.e.
Definition: GlobalValue.h:595
const GlobalObject * getAliaseeObject() const
Definition: Globals.cpp:394
static bool isExternalLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:376
VisibilityTypes
An enumeration for the kinds of visibility of global values.
Definition: GlobalValue.h:66
@ DefaultVisibility
The GV is visible.
Definition: GlobalValue.h:67
static LinkageTypes getWeakLinkage(bool ODR)
Definition: GlobalValue.h:372
bool isWeakForLinker() const
Definition: GlobalValue.h:552
bool hasAppendingLinkage() const
Definition: GlobalValue.h:525
bool hasAvailableExternallyLinkage() const
Definition: GlobalValue.h:512
std::string getGlobalIdentifier() const
Return the modified name for this global value suitable to be used as the key for a global lookup (e....
Definition: Globals.cpp:178
LinkageTypes
An enumeration for the kinds of linkage for global values.
Definition: GlobalValue.h:51
@ CommonLinkage
Tentative definitions.
Definition: GlobalValue.h:62
@ InternalLinkage
Rename collisions when linking (static functions).
Definition: GlobalValue.h:59
@ ExternalLinkage
Externally visible function.
Definition: GlobalValue.h:52
@ WeakAnyLinkage
Keep one copy of named function when linking (weak)
Definition: GlobalValue.h:56
@ AvailableExternallyLinkage
Available for inspection, not emission.
Definition: GlobalValue.h:53
DLLStorageClassTypes getDLLStorageClass() const
Definition: GlobalValue.h:275
Type * getValueType() const
Definition: GlobalValue.h:296
static bool isLinkOnceODRLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:385
void eraseFromParent()
eraseFromParent - This method unlinks 'this' from the containing module and deletes it.
Definition: Globals.cpp:481
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
This class implements a map that also provides access to all stored values in a deterministic order.
Definition: MapVector.h:36
iterator begin()
Definition: MapVector.h:69
size_type size() const
Definition: MapVector.h:60
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFile(const Twine &Filename, bool IsText=false, bool RequiresNullTerminator=true, bool IsVolatile=false, std::optional< Align > Alignment=std::nullopt)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful,...
Class to hold module path string table and global value map, and encapsulate methods for operating on...
std::set< std::string > & cfiFunctionDecls()
const ModuleHash & getModuleHash(const StringRef ModPath) const
Get the module SHA1 hash recorded for the given module path.
const StringMap< ModuleHash > & modulePaths() const
Table of modules, containing module hash and id.
std::set< std::string > & cfiFunctionDefs()
static void CollectAsmSymvers(const Module &M, function_ref< void(StringRef, StringRef)> AsmSymver)
Parse inline ASM and collect the symvers directives that are defined in the current module.
uint32_t getSymbolFlags(Symbol S) const
ArrayRef< Symbol > symbols() const
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
A tuple of MDNodes.
Definition: Metadata.h:1729
The optimization diagnostic interface.
Diagnostic information for applied optimization remarks.
A discriminated union of two or more pointer types, with the discriminator in the low bit of the poin...
Definition: PointerUnion.h:118
A class that wrap the SHA1 algorithm.
Definition: SHA1.h:26
void update(ArrayRef< uint8_t > Data)
Digest more data.
Definition: SHA1.cpp:208
std::array< uint8_t, 20 > result()
Return the current raw 160-bits SHA1 for the digested data since the last call to init().
Definition: SHA1.cpp:288
A non-threaded implementation.
Definition: ThreadPool.h:218
void wait() override
Blocking wait for all the tasks to execute first.
Definition: ThreadPool.cpp:201
unsigned getMaxConcurrency() const override
Returns always 1: there is no concurrency.
Definition: ThreadPool.h:233
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
Definition: SmallSet.h:135
size_type count(const T &V) const
count - Return 1 if the element is in the set, 0 otherwise.
Definition: SmallSet.h:166
bool empty() const
Definition: SmallSet.h:159
bool erase(const T &V)
Definition: SmallSet.h:207
std::pair< const_iterator, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
Definition: SmallSet.h:179
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
StringRef str() const
Explicit conversion to StringRef.
Definition: SmallString.h:254
A wrapper around a string literal that serves as a proxy for constructing global tables of StringRefs...
Definition: StringRef.h:838
StringMapEntry - This is used to represent one value that is inserted into a StringMap.
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition: StringMap.h:128
size_type count(StringRef Key) const
count - Return 1 if the element is in the map, 0 otherwise.
Definition: StringMap.h:276
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
constexpr bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:134
bool contains(StringRef Other) const
Return true if the given string is a substring of *this, and false otherwise.
Definition: StringRef.h:409
MCTargetOptions MCOptions
Machine level options.
DebuggerKind DebuggerTuning
Which debugger to tune for.
unsigned FunctionSections
Emit functions into separate sections.
unsigned DataSections
Emit data into separate sections.
auto async(Function &&F, Args &&...ArgList)
Asynchronous submission of a task to the pool.
Definition: ThreadPool.h:78
This tells how a thread pool will be used.
Definition: Threading.h:116
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
bool isArm64e() const
Tests whether the target is the Apple "arm64e" AArch64 subarch.
Definition: Triple.h:1031
@ aarch64_32
Definition: Triple.h:53
ArchType getArch() const
Get the parsed architecture type of this triple.
Definition: Triple.h:373
bool isOSDarwin() const
Is this a "Darwin" OS (macOS, iOS, tvOS, watchOS, XROS, or DriverKit).
Definition: Triple.h:558
bool isOSBinFormatELF() const
Tests whether the OS uses the ELF binary format.
Definition: Triple.h:719
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
static IntegerType * getInt8Ty(LLVMContext &C)
void setName(const Twine &Name)
Change the name of the value.
Definition: Value.cpp:377
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:534
bool use_empty() const
Definition: Value.h:344
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:309
void takeName(Value *V)
Transfer the name from V to this value.
Definition: Value.cpp:383
std::pair< iterator, bool > insert(const ValueT &V)
Definition: DenseSet.h:206
iterator find(const_arg_type_t< ValueT > V)
Definition: DenseSet.h:179
size_type size() const
Definition: DenseSet.h:81
size_type count(const_arg_type_t< ValueT > V) const
Return 1 if the specified key is in the set, 0 otherwise.
Definition: DenseSet.h:97
An efficient, type-erasing, non-owning reference to a callable.
Ephemeral symbols produced by Reader::symbols() and Reader::module_symbols().
Definition: IRSymtab.h:313
The purpose of this class is to only expose the symbol information that an LTO client should need in ...
Definition: LTO.h:139
An input file.
Definition: LTO.h:111
static Expected< std::unique_ptr< InputFile > > create(MemoryBufferRef Object)
Create an InputFile.
Definition: LTO.cpp:551
ArrayRef< Symbol > symbols() const
A range over the symbols in this InputFile.
Definition: LTO.h:164
StringRef getName() const
Returns the path to the InputFile.
Definition: LTO.cpp:580
BitcodeModule & getSingleBitcodeModule()
Definition: LTO.cpp:584
Error add(std::unique_ptr< InputFile > Obj, ArrayRef< SymbolResolution > Res)
Add an input file to the LTO link, using the provided symbol resolutions.
Definition: LTO.cpp:706
LTO(Config Conf, ThinBackend Backend=nullptr, unsigned ParallelCodeGenParallelismLevel=1, LTOKind LTOMode=LTOK_Default)
Create an LTO object.
Definition: LTO.cpp:604
LTOKind
Unified LTO modes.
Definition: LTO.h:261
@ LTOK_UnifiedRegular
Regular LTO, with Unified LTO enabled.
Definition: LTO.h:266
@ LTOK_Default
Any LTO mode without Unified LTO. The default mode.
Definition: LTO.h:263
@ LTOK_UnifiedThin
ThinLTO, with Unified LTO enabled.
Definition: LTO.h:269
static ArrayRef< const char * > getRuntimeLibcallSymbols()
Static method that returns a list of libcall symbols that can be generated by LTO but might not be vi...
Definition: LTO.cpp:1366
Error run(AddStreamFn AddStream, FileCache Cache=nullptr)
Runs the LTO pipeline.
Definition: LTO.cpp:1139
unsigned getMaxTasks() const
Returns an upper bound on the number of tasks that the client may expect.
Definition: LTO.cpp:1088
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:460
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
This class defines the interface to the ThinLTO backend.
Definition: LTO.cpp:1371
virtual unsigned getThreadCount()=0
const Config & Conf
Definition: LTO.cpp:1373
lto::IndexWriteCallback OnWrite
Definition: LTO.cpp:1376
Error emitFiles(const FunctionImporter::ImportMapTy &ImportList, llvm::StringRef ModulePath, const std::string &NewModulePath)
Definition: LTO.cpp:1399
virtual Error wait()=0
ThinBackendProc(const Config &Conf, ModuleSummaryIndex &CombinedIndex, const DenseMap< StringRef, GVSummaryMapTy > &ModuleToDefinedGVSummaries, lto::IndexWriteCallback OnWrite, bool ShouldEmitImportsFiles)
Definition: LTO.cpp:1380
const DenseMap< StringRef, GVSummaryMapTy > & ModuleToDefinedGVSummaries
Definition: LTO.cpp:1375
ModuleSummaryIndex & CombinedIndex
Definition: LTO.cpp:1374
virtual ~ThinBackendProc()=default
virtual Error start(unsigned Task, BitcodeModule BM, const FunctionImporter::ImportMapTy &ImportList, const FunctionImporter::ExportSetTy &ExportList, const std::map< GlobalValue::GUID, GlobalValue::LinkageTypes > &ResolvedODR, MapVector< StringRef, BitcodeModule > &ModuleMap)=0
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Key
PAL metadata keys.
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
StringRef getName(ID id)
Return the LLVM name for an intrinsic, such as "llvm.ppc.altivec.lvx".
Definition: Function.cpp:1071
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:443
ThinBackend createInProcessThinBackend(ThreadPoolStrategy Parallelism, IndexWriteCallback OnWrite=nullptr, bool ShouldEmitIndexFiles=false, bool ShouldEmitImportsFiles=false)
Definition: LTO.cpp:1563
Error thinBackend(const Config &C, unsigned Task, AddStreamFn AddStream, Module &M, const ModuleSummaryIndex &CombinedIndex, const FunctionImporter::ImportMapTy &ImportList, const GVSummaryMapTy &DefinedGlobals, MapVector< StringRef, BitcodeModule > *ModuleMap, const std::vector< uint8_t > &CmdArgs=std::vector< uint8_t >())
Runs a ThinLTO backend.
Definition: LTOBackend.cpp:553
std::string getThinLTOOutputFile(StringRef Path, StringRef OldPrefix, StringRef NewPrefix)
Given the original Path to an output file, replace any path prefix matching OldPrefix with NewPrefix.
Definition: LTO.cpp:1596
std::function< std::unique_ptr< ThinBackendProc >(const Config &C, ModuleSummaryIndex &CombinedIndex, DenseMap< StringRef, GVSummaryMapTy > &ModuleToDefinedGVSummaries, AddStreamFn AddStream, FileCache Cache)> ThinBackend
A ThinBackend defines what happens after the thin-link phase during ThinLTO.
Definition: LTO.h:202
std::function< void(const std::string &)> IndexWriteCallback
This ThinBackend runs the individual backend jobs in-process.
Definition: LTO.h:212
StringLiteral getThinLTODefaultCPU(const Triple &TheTriple)
Definition: LTO.cpp:1578
Expected< std::unique_ptr< ToolOutputFile > > setupStatsFile(StringRef StatsFilename)
Setups the output file for saving statistics.
Definition: LTO.cpp:1912
Error backend(const Config &C, AddStreamFn AddStream, unsigned ParallelCodeGenParallelismLevel, Module &M, ModuleSummaryIndex &CombinedIndex)
Runs a regular LTO backend.
Definition: LTOBackend.cpp:506
Error finalizeOptimizationRemarks(std::unique_ptr< ToolOutputFile > DiagOutputFile)
Definition: LTOBackend.cpp:495
ThinBackend createWriteIndexesThinBackend(std::string OldPrefix, std::string NewPrefix, std::string NativeObjectPrefix, bool ShouldEmitImportsFiles, raw_fd_ostream *LinkedObjectsFile, IndexWriteCallback OnWrite)
This ThinBackend writes individual module indexes to files, instead of running the individual backend...
Definition: LTO.cpp:1664
std::vector< int > generateModulesOrdering(ArrayRef< BitcodeModule * > R)
Produces a container ordering for optimal multi-threaded processing.
Definition: LTO.cpp:1931
Expected< std::unique_ptr< ToolOutputFile > > setupLLVMOptimizationRemarks(LLVMContext &Context, StringRef RemarksFilename, StringRef RemarksPasses, StringRef RemarksFormat, bool RemarksWithHotness, std::optional< uint64_t > RemarksHotnessThreshold=0, int Count=-1)
Setup optimization remarks.
Definition: LTO.cpp:1887
void updateMemProfAttributes(Module &Mod, const ModuleSummaryIndex &Index)
Updates MemProf attributes (and metadata) based on whether the index has recorded that we are linking...
Definition: LTO.cpp:1197
Expected< IRSymtabFile > readIRSymtab(MemoryBufferRef MBRef)
Reads a bitcode file, creating its irsymtab if necessary.
size_t getThreadCount()
Definition: Parallel.h:55
void write64le(void *P, uint64_t V)
Definition: Endian.h:471
void write32le(void *P, uint32_t V)
Definition: Endian.h:468
std::error_code create_directories(const Twine &path, bool IgnoreExisting=true, perms Perms=owner_all|group_all)
Create all the non-existent directories in path.
Definition: Path.cpp:968
StringRef parent_path(StringRef path, Style style=Style::native)
Get parent path.
Definition: Path.cpp:468
bool replace_path_prefix(SmallVectorImpl< char > &Path, StringRef OldPrefix, StringRef NewPrefix, Style style=Style::native)
Replace matching path prefix with another path.
Definition: Path.cpp:519
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
ThreadPoolStrategy heavyweight_hardware_concurrency(unsigned ThreadCount=0)
Returns a thread strategy for tasks requiring significant memory or other resources.
Definition: Threading.h:162
cl::opt< std::string > RemarksFormat("lto-pass-remarks-format", cl::desc("The format used for serializing remarks (default: YAML)"), cl::value_desc("format"), cl::init("yaml"))
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1722
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition: STLExtras.h:1680
void gatherImportedSummariesForModule(StringRef ModulePath, const DenseMap< StringRef, GVSummaryMapTy > &ModuleToDefinedGVSummaries, const FunctionImporter::ImportMapTy &ImportList, std::map< std::string, GVSummaryMapTy > &ModuleToSummariesForIndex, GVSummaryPtrSet &DecSummaries)
Compute the set of summaries needed for a ThinLTO backend compilation of ModulePath.
void ComputeCrossModuleImport(const ModuleSummaryIndex &Index, const DenseMap< StringRef, GVSummaryMapTy > &ModuleToDefinedGVSummaries, function_ref< bool(GlobalValue::GUID, const GlobalValueSummary *)> isPrevailing, DenseMap< StringRef, FunctionImporter::ImportMapTy > &ImportLists, DenseMap< StringRef, FunctionImporter::ExportSetTy > &ExportLists)
Compute all the imports and exports for every module in the Index.
detail::scope_exit< std::decay_t< Callable > > make_scope_exit(Callable &&F)
Definition: ScopeExit.h:59
void timeTraceProfilerInitialize(unsigned TimeTraceGranularity, StringRef ProcName)
Initialize the time trace profiler.
void generateParamAccessSummary(ModuleSummaryIndex &Index)
cl::opt< std::string > RemarksPasses("lto-pass-remarks-filter", cl::desc("Only record optimization remarks from passes whose " "names match the given regular expression"), cl::value_desc("regex"))
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition: Error.cpp:98
raw_fd_ostream & outs()
This returns a reference to a raw_fd_ostream for standard output.
std::function< Expected< std::unique_ptr< CachedFileStream > >(unsigned Task, const Twine &ModuleName)> AddStreamFn
This type defines the callback to add a file that is generated on the fly.
Definition: Caching.h:42
std::error_code EmitImportsFiles(StringRef ModulePath, StringRef OutputFilename, const std::map< std::string, GVSummaryMapTy > &ModuleToSummariesForIndex)
Emit into OutputFilename the files module ModulePath will import from.
bool thinLTOPropagateFunctionAttrs(ModuleSummaryIndex &Index, function_ref< bool(GlobalValue::GUID, const GlobalValueSummary *)> isPrevailing)
Propagate function attributes for function summaries along the index's callgraph during thinlink.
bool hasWholeProgramVisibility(bool WholeProgramVisibilityEnabledInLTO)
void EnableStatistics(bool DoPrintOnExit=true)
Enable the collection and printing of statistics.
Definition: Statistic.cpp:134
void thinLTOInternalizeAndPromoteInIndex(ModuleSummaryIndex &Index, function_ref< bool(StringRef, ValueInfo)> isExported, function_ref< bool(GlobalValue::GUID, const GlobalValueSummary *)> isPrevailing)
Update the linkages in the given Index to mark exported values as external and non-exported values as...
Definition: LTO.cpp:538
void computeLTOCacheKey(SmallString< 40 > &Key, const lto::Config &Conf, const ModuleSummaryIndex &Index, StringRef ModuleID, const FunctionImporter::ImportMapTy &ImportList, const FunctionImporter::ExportSetTy &ExportList, const std::map< GlobalValue::GUID, GlobalValue::LinkageTypes > &ResolvedODR, const GVSummaryMapTy &DefinedGlobals, const std::set< GlobalValue::GUID > &CfiFunctionDefs={}, const std::set< GlobalValue::GUID > &CfiFunctionDecls={})
Computes a unique hash for the Module considering the current list of export/import and other global ...
Definition: LTO.cpp:91
void timeTraceProfilerFinishThread()
Finish a time trace profiler running on a worker thread.
Error joinErrors(Error E1, Error E2)
Concatenate errors.
Definition: Error.h:438
void updatePublicTypeTestCalls(Module &M, bool WholeProgramVisibilityEnabledInLTO)
void getVisibleToRegularObjVtableGUIDs(ModuleSummaryIndex &Index, DenseSet< GlobalValue::GUID > &VisibleToRegularObjSymbols, function_ref< bool(StringRef)> IsVisibleToRegularObj)
Based on typeID string, get all associated vtable GUIDS that are visible to regular objects.
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1647
void writeIndexToFile(const ModuleSummaryIndex &Index, raw_ostream &Out, const std::map< std::string, GVSummaryMapTy > *ModuleToSummariesForIndex=nullptr, const GVSummaryPtrSet *DecSummaries=nullptr)
Write the specified module summary index to the given raw output stream, where it will be written in ...
bool timeTraceProfilerEnabled()
Is the time trace profiler enabled, i.e. initialized?
Definition: TimeProfiler.h:104
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
Expected< std::unique_ptr< ToolOutputFile > > setupLLVMOptimizationRemarks(LLVMContext &Context, StringRef RemarksFilename, StringRef RemarksPasses, StringRef RemarksFormat, bool RemarksWithHotness, std::optional< uint64_t > RemarksHotnessThreshold=0)
Setup optimization remarks that output to a file.
cl::opt< bool > EnableLTOInternalization
Enable global value internalization in LTO.
cl::opt< bool > RemarksWithHotness("lto-pass-remarks-with-hotness", cl::desc("With PGO, include profile count in optimization remarks"), cl::Hidden)
void computeSyntheticCounts(ModuleSummaryIndex &Index)
Compute synthetic function entry counts.
void timeTraceProfilerEnd()
Manually end the last time section.
cl::opt< std::string > RemarksFilename("lto-pass-remarks-output", cl::desc("Output filename for pass remarks"), cl::value_desc("filename"))
cl::opt< bool > SupportsHotColdNew
Indicate we are linking with an allocator that supports hot/cold operator new interfaces.
void updateIndexWPDForExports(ModuleSummaryIndex &Summary, function_ref< bool(StringRef, ValueInfo)> isExported, std::map< ValueInfo, std::vector< VTableSlotSummary > > &LocalWPDTargetsMap)
Call after cross-module importing to update the recorded single impl devirt target names for any loca...
void thinLTOResolvePrevailingInIndex(const lto::Config &C, ModuleSummaryIndex &Index, function_ref< bool(GlobalValue::GUID, const GlobalValueSummary *)> isPrevailing, function_ref< void(StringRef, GlobalValue::GUID, GlobalValue::LinkageTypes)> recordNewLinkage, const DenseSet< GlobalValue::GUID > &GUIDPreservedSymbols)
Resolve linkage for prevailing symbols in the Index.
Definition: LTO.cpp:437
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
@ Mod
The access may modify the value stored in memory.
cl::opt< bool > EnableMemProfContextDisambiguation
Enable MemProf context disambiguation for thin link.
void runWholeProgramDevirtOnIndex(ModuleSummaryIndex &Summary, std::set< GlobalValue::GUID > &ExportedGUIDs, std::map< ValueInfo, std::vector< VTableSlotSummary > > &LocalWPDTargetsMap)
Perform index-based whole program devirtualization on the Summary index.
std::unordered_set< GlobalValueSummary * > GVSummaryPtrSet
A set of global value summary pointers.
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1849
auto count_if(R &&Range, UnaryPredicate P)
Wrapper function around std::count_if to count the number of times an element satisfying a given pred...
Definition: STLExtras.h:1921
cl::opt< std::optional< uint64_t >, false, remarks::HotnessThresholdParser > RemarksHotnessThreshold("lto-pass-remarks-hotness-threshold", cl::desc("Minimum profile count required for an " "optimization remark to be output." " Use 'auto' to apply the threshold from profile summary."), cl::value_desc("uint or 'auto'"), cl::init(0), cl::Hidden)
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
Definition: Error.cpp:111
bool UpgradeDebugInfo(Module &M)
Check the debug info version number, if it is out-dated, drop the debug info.
std::array< uint32_t, 5 > ModuleHash
160 bits SHA1
std::function< Expected< AddStreamFn >(unsigned Task, StringRef Key, const Twine &ModuleName)> FileCache
This is the type of a file cache.
Definition: Caching.h:58
void PrintStatisticsJSON(raw_ostream &OS)
Print statistics in JSON format.
Definition: Statistic.cpp:203
void computeDeadSymbolsWithConstProp(ModuleSummaryIndex &Index, const DenseSet< GlobalValue::GUID > &GUIDPreservedSymbols, function_ref< PrevailingType(GlobalValue::GUID)> isPrevailing, bool ImportEnabled)
Compute dead symbols and run constant propagation in combined index after that.
@ Keep
No function return thunk.
void updateVCallVisibilityInModule(Module &M, bool WholeProgramVisibilityEnabledInLTO, const DenseSet< GlobalValue::GUID > &DynamicExportSymbols, bool ValidateAllVtablesHaveTypeInfos, function_ref< bool(StringRef)> IsVisibleToRegularObj)
If whole program visibility asserted, then upgrade all public vcall visibility metadata on vtable def...
TimeTraceProfilerEntry * timeTraceProfilerBegin(StringRef Name, StringRef Detail)
Manually begin a time section, with the given Name and Detail.
void updateVCallVisibilityInIndex(ModuleSummaryIndex &Index, bool WholeProgramVisibilityEnabledInLTO, const DenseSet< GlobalValue::GUID > &DynamicExportSymbols, const DenseSet< GlobalValue::GUID > &VisibleToRegularObjSymbols)
If whole program visibility asserted, then upgrade all public vcall visibility metadata on vtable def...
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
Used in the streaming interface as the general argument type.
Struct that holds a reference to a particular GUID in a global value summary.
LTO configuration.
Definition: Config.h:41
bool HasWholeProgramVisibility
Asserts whether we can assume whole program visibility during the LTO link.
Definition: Config.h:78
bool ValidateAllVtablesHaveTypeInfos
We're validating that all native vtables have corresponding type infos.
Definition: Config.h:81
std::optional< uint64_t > RemarksHotnessThreshold
The minimum hotness value a diagnostic needs in order to be included in optimization diagnostics.
Definition: Config.h:160
std::string StatsFile
Statistics output file path.
Definition: Config.h:169
ModuleHookFn PreOptModuleHook
This module hook is called after linking (regular LTO) or loading (ThinLTO) the module,...
Definition: Config.h:223
CombinedIndexHookFn CombinedIndexHook
Definition: Config.h:255
std::optional< CodeModel::Model > CodeModel
Definition: Config.h:56
std::string AAPipeline
Definition: Config.h:105
bool CodeGenOnly
Disable entirely the optimizer, including importing for ThinLTO.
Definition: Config.h:68
std::vector< std::string > MAttrs
Definition: Config.h:50
std::vector< std::string > MllvmArgs
Definition: Config.h:51
std::vector< std::string > ThinLTOModulesToCompile
Specific thinLTO modules to compile.
Definition: Config.h:172
CodeGenOptLevel CGOptLevel
Definition: Config.h:57
std::unique_ptr< raw_ostream > ResolutionFile
If this field is set, LTO will write input file paths and symbol resolutions here in llvm-lto2 comman...
Definition: Config.h:190
std::string DefaultTriple
Setting this field will replace unspecified target triples in input files with this triple.
Definition: Config.h:113
bool AlwaysEmitRegularLTOObj
Always emit a Regular LTO object even when it is empty because no Regular LTO modules were linked.
Definition: Config.h:89
std::string CPU
Definition: Config.h:48
std::string DwoDir
The directory to store .dwo files.
Definition: Config.h:125
std::string RemarksFilename
Optimization remarks file path.
Definition: Config.h:139
VisScheme VisibilityScheme
Allows non-imported definitions to get the potentially more constraining visibility from the prevaili...
Definition: Config.h:95
std::string OverrideTriple
Setting this field will replace target triples in input files with this triple.
Definition: Config.h:109
std::string ProfileRemapping
Name remapping file for profile data.
Definition: Config.h:122
bool AllVtablesHaveTypeInfos
If all native vtables have corresponding type infos, allow usage of RTTI to block devirtualization on...
Definition: Config.h:84
TargetOptions Options
Definition: Config.h:49
bool TimeTraceEnabled
Time trace enabled.
Definition: Config.h:175
std::string RemarksPasses
Optimization remarks pass filter.
Definition: Config.h:142
std::string OptPipeline
If this field is set, the set of passes run in the middle-end optimizer will be the one specified by ...
Definition: Config.h:100
ModuleHookFn PostInternalizeModuleHook
This hook is called after internalizing the module.
Definition: Config.h:230
unsigned TimeTraceGranularity
Time trace granularity.
Definition: Config.h:178
unsigned OptLevel
Definition: Config.h:59
bool RemarksWithHotness
Whether to emit optimization remarks with hotness informations.
Definition: Config.h:145
std::optional< Reloc::Model > RelocModel
Definition: Config.h:55
CodeGenFileType CGFileType
Definition: Config.h:58
bool Freestanding
Flag to indicate that the optimizer should not assume builtins are present on the target.
Definition: Config.h:65
std::string SampleProfile
Sample PGO profile path.
Definition: Config.h:119
std::string RemarksFormat
The format used for serializing remarks (default: YAML).
Definition: Config.h:163
A derived class of LLVMContext that initializes itself according to a given Config object.
Definition: Config.h:294
The resolution for a symbol.
Definition: LTO.h:457
unsigned FinalDefinitionInLinkageUnit
The definition of this symbol is unpreemptable at runtime and is known to be in this linkage unit.
Definition: LTO.h:467
unsigned ExportDynamic
The symbol was exported dynamically, and therefore could be referenced by a shared library not visibl...
Definition: LTO.h:474
unsigned Prevailing
The linker has chosen this definition of the symbol.
Definition: LTO.h:463
unsigned LinkerRedefined
Linker redefined version of the symbol which appeared in -wrap or -defsym linker option.
Definition: LTO.h:478
unsigned VisibleToRegularObj
The definition of this symbol is visible outside of the LTO unit.
Definition: LTO.h:470