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 std::vector<std::pair<uint64_t, uint8_t>> ExportsGUID;
165 ExportsGUID.reserve(ExportList.size());
166 for (const auto &[VI, ExportType] : ExportList)
167 ExportsGUID.push_back(
168 std::make_pair(VI.getGUID(), static_cast<uint8_t>(ExportType)));
169
170 // Sort the export list elements GUIDs.
171 llvm::sort(ExportsGUID);
172 for (auto [GUID, ExportType] : ExportsGUID) {
173 // The export list can impact the internalization, be conservative here
174 Hasher.update(ArrayRef<uint8_t>((uint8_t *)&GUID, sizeof(GUID)));
175 AddUint8(ExportType);
176 }
177
178 // Include the hash for every module we import functions from. The set of
179 // imported symbols for each module may affect code generation and is
180 // sensitive to link order, so include that as well.
181 using ImportMapIteratorTy = FunctionImporter::ImportMapTy::const_iterator;
182 struct ImportModule {
183 ImportMapIteratorTy ModIt;
184 const ModuleSummaryIndex::ModuleInfo *ModInfo;
185
186 StringRef getIdentifier() const { return ModIt->getFirst(); }
187 const FunctionImporter::FunctionsToImportTy &getFunctions() const {
188 return ModIt->second;
189 }
190
191 const ModuleHash &getHash() const { return ModInfo->second; }
192 };
193
194 std::vector<ImportModule> ImportModulesVector;
195 ImportModulesVector.reserve(ImportList.size());
196
197 for (ImportMapIteratorTy It = ImportList.begin(); It != ImportList.end();
198 ++It) {
199 ImportModulesVector.push_back({It, Index.getModule(It->getFirst())});
200 }
201 // Order using module hash, to be both independent of module name and
202 // module order.
203 llvm::sort(ImportModulesVector,
204 [](const ImportModule &Lhs, const ImportModule &Rhs) -> bool {
205 return Lhs.getHash() < Rhs.getHash();
206 });
207 std::vector<std::pair<uint64_t, uint8_t>> ImportedGUIDs;
208 for (const ImportModule &Entry : ImportModulesVector) {
209 auto ModHash = Entry.getHash();
210 Hasher.update(ArrayRef<uint8_t>((uint8_t *)&ModHash[0], sizeof(ModHash)));
211
212 AddUint64(Entry.getFunctions().size());
213
214 ImportedGUIDs.clear();
215 for (auto &[Fn, ImportType] : Entry.getFunctions())
216 ImportedGUIDs.push_back(std::make_pair(Fn, ImportType));
217 llvm::sort(ImportedGUIDs);
218 for (auto &[GUID, Type] : ImportedGUIDs) {
219 AddUint64(GUID);
220 AddUint8(Type);
221 }
222 }
223
224 // Include the hash for the resolved ODR.
225 for (auto &Entry : ResolvedODR) {
226 Hasher.update(ArrayRef<uint8_t>((const uint8_t *)&Entry.first,
227 sizeof(GlobalValue::GUID)));
228 Hasher.update(ArrayRef<uint8_t>((const uint8_t *)&Entry.second,
230 }
231
232 // Members of CfiFunctionDefs and CfiFunctionDecls that are referenced or
233 // defined in this module.
234 std::set<GlobalValue::GUID> UsedCfiDefs;
235 std::set<GlobalValue::GUID> UsedCfiDecls;
236
237 // Typeids used in this module.
238 std::set<GlobalValue::GUID> UsedTypeIds;
239
240 auto AddUsedCfiGlobal = [&](GlobalValue::GUID ValueGUID) {
241 if (CfiFunctionDefs.count(ValueGUID))
242 UsedCfiDefs.insert(ValueGUID);
243 if (CfiFunctionDecls.count(ValueGUID))
244 UsedCfiDecls.insert(ValueGUID);
245 };
246
247 auto AddUsedThings = [&](GlobalValueSummary *GS) {
248 if (!GS) return;
249 AddUnsigned(GS->getVisibility());
250 AddUnsigned(GS->isLive());
251 AddUnsigned(GS->canAutoHide());
252 for (const ValueInfo &VI : GS->refs()) {
253 AddUnsigned(VI.isDSOLocal(Index.withDSOLocalPropagation()));
254 AddUsedCfiGlobal(VI.getGUID());
255 }
256 if (auto *GVS = dyn_cast<GlobalVarSummary>(GS)) {
257 AddUnsigned(GVS->maybeReadOnly());
258 AddUnsigned(GVS->maybeWriteOnly());
259 }
260 if (auto *FS = dyn_cast<FunctionSummary>(GS)) {
261 for (auto &TT : FS->type_tests())
262 UsedTypeIds.insert(TT);
263 for (auto &TT : FS->type_test_assume_vcalls())
264 UsedTypeIds.insert(TT.GUID);
265 for (auto &TT : FS->type_checked_load_vcalls())
266 UsedTypeIds.insert(TT.GUID);
267 for (auto &TT : FS->type_test_assume_const_vcalls())
268 UsedTypeIds.insert(TT.VFunc.GUID);
269 for (auto &TT : FS->type_checked_load_const_vcalls())
270 UsedTypeIds.insert(TT.VFunc.GUID);
271 for (auto &ET : FS->calls()) {
272 AddUnsigned(ET.first.isDSOLocal(Index.withDSOLocalPropagation()));
273 AddUsedCfiGlobal(ET.first.getGUID());
274 }
275 }
276 };
277
278 // Include the hash for the linkage type to reflect internalization and weak
279 // resolution, and collect any used type identifier resolutions.
280 for (auto &GS : DefinedGlobals) {
281 GlobalValue::LinkageTypes Linkage = GS.second->linkage();
282 Hasher.update(
283 ArrayRef<uint8_t>((const uint8_t *)&Linkage, sizeof(Linkage)));
284 AddUsedCfiGlobal(GS.first);
285 AddUsedThings(GS.second);
286 }
287
288 // Imported functions may introduce new uses of type identifier resolutions,
289 // so we need to collect their used resolutions as well.
290 for (const ImportModule &ImpM : ImportModulesVector)
291 for (auto &[GUID, UnusedImportType] : ImpM.getFunctions()) {
293 Index.findSummaryInModule(GUID, ImpM.getIdentifier());
294 AddUsedThings(S);
295 // If this is an alias, we also care about any types/etc. that the aliasee
296 // may reference.
297 if (auto *AS = dyn_cast_or_null<AliasSummary>(S))
298 AddUsedThings(AS->getBaseObject());
299 }
300
301 auto AddTypeIdSummary = [&](StringRef TId, const TypeIdSummary &S) {
302 AddString(TId);
303
304 AddUnsigned(S.TTRes.TheKind);
305 AddUnsigned(S.TTRes.SizeM1BitWidth);
306
307 AddUint64(S.TTRes.AlignLog2);
308 AddUint64(S.TTRes.SizeM1);
309 AddUint64(S.TTRes.BitMask);
310 AddUint64(S.TTRes.InlineBits);
311
312 AddUint64(S.WPDRes.size());
313 for (auto &WPD : S.WPDRes) {
314 AddUnsigned(WPD.first);
315 AddUnsigned(WPD.second.TheKind);
316 AddString(WPD.second.SingleImplName);
317
318 AddUint64(WPD.second.ResByArg.size());
319 for (auto &ByArg : WPD.second.ResByArg) {
320 AddUint64(ByArg.first.size());
321 for (uint64_t Arg : ByArg.first)
322 AddUint64(Arg);
323 AddUnsigned(ByArg.second.TheKind);
324 AddUint64(ByArg.second.Info);
325 AddUnsigned(ByArg.second.Byte);
326 AddUnsigned(ByArg.second.Bit);
327 }
328 }
329 };
330
331 // Include the hash for all type identifiers used by this module.
332 for (GlobalValue::GUID TId : UsedTypeIds) {
333 auto TidIter = Index.typeIds().equal_range(TId);
334 for (auto It = TidIter.first; It != TidIter.second; ++It)
335 AddTypeIdSummary(It->second.first, It->second.second);
336 }
337
338 AddUnsigned(UsedCfiDefs.size());
339 for (auto &V : UsedCfiDefs)
340 AddUint64(V);
341
342 AddUnsigned(UsedCfiDecls.size());
343 for (auto &V : UsedCfiDecls)
344 AddUint64(V);
345
346 if (!Conf.SampleProfile.empty()) {
347 auto FileOrErr = MemoryBuffer::getFile(Conf.SampleProfile);
348 if (FileOrErr) {
349 Hasher.update(FileOrErr.get()->getBuffer());
350
351 if (!Conf.ProfileRemapping.empty()) {
352 FileOrErr = MemoryBuffer::getFile(Conf.ProfileRemapping);
353 if (FileOrErr)
354 Hasher.update(FileOrErr.get()->getBuffer());
355 }
356 }
357 }
358
359 Key = toHex(Hasher.result());
360}
361
363 const Config &C, ValueInfo VI,
364 DenseSet<GlobalValueSummary *> &GlobalInvolvedWithAlias,
366 isPrevailing,
368 recordNewLinkage,
369 const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols) {
371 C.VisibilityScheme == Config::ELF ? VI.getELFVisibility()
373 for (auto &S : VI.getSummaryList()) {
374 GlobalValue::LinkageTypes OriginalLinkage = S->linkage();
375 // Ignore local and appending linkage values since the linker
376 // doesn't resolve them.
377 if (GlobalValue::isLocalLinkage(OriginalLinkage) ||
379 continue;
380 // We need to emit only one of these. The prevailing module will keep it,
381 // but turned into a weak, while the others will drop it when possible.
382 // This is both a compile-time optimization and a correctness
383 // transformation. This is necessary for correctness when we have exported
384 // a reference - we need to convert the linkonce to weak to
385 // ensure a copy is kept to satisfy the exported reference.
386 // FIXME: We may want to split the compile time and correctness
387 // aspects into separate routines.
388 if (isPrevailing(VI.getGUID(), S.get())) {
389 if (GlobalValue::isLinkOnceLinkage(OriginalLinkage)) {
390 S->setLinkage(GlobalValue::getWeakLinkage(
391 GlobalValue::isLinkOnceODRLinkage(OriginalLinkage)));
392 // The kept copy is eligible for auto-hiding (hidden visibility) if all
393 // copies were (i.e. they were all linkonce_odr global unnamed addr).
394 // If any copy is not (e.g. it was originally weak_odr), then the symbol
395 // must remain externally available (e.g. a weak_odr from an explicitly
396 // instantiated template). Additionally, if it is in the
397 // GUIDPreservedSymbols set, that means that it is visibile outside
398 // the summary (e.g. in a native object or a bitcode file without
399 // summary), and in that case we cannot hide it as it isn't possible to
400 // check all copies.
401 S->setCanAutoHide(VI.canAutoHide() &&
402 !GUIDPreservedSymbols.count(VI.getGUID()));
403 }
404 if (C.VisibilityScheme == Config::FromPrevailing)
405 Visibility = S->getVisibility();
406 }
407 // Alias and aliasee can't be turned into available_externally.
408 else if (!isa<AliasSummary>(S.get()) &&
409 !GlobalInvolvedWithAlias.count(S.get()))
411
412 // For ELF, set visibility to the computed visibility from summaries. We
413 // don't track visibility from declarations so this may be more relaxed than
414 // the most constraining one.
415 if (C.VisibilityScheme == Config::ELF)
416 S->setVisibility(Visibility);
417
418 if (S->linkage() != OriginalLinkage)
419 recordNewLinkage(S->modulePath(), VI.getGUID(), S->linkage());
420 }
421
422 if (C.VisibilityScheme == Config::FromPrevailing) {
423 for (auto &S : VI.getSummaryList()) {
424 GlobalValue::LinkageTypes OriginalLinkage = S->linkage();
425 if (GlobalValue::isLocalLinkage(OriginalLinkage) ||
427 continue;
428 S->setVisibility(Visibility);
429 }
430 }
431}
432
433/// Resolve linkage for prevailing symbols in the \p Index.
434//
435// We'd like to drop these functions if they are no longer referenced in the
436// current module. However there is a chance that another module is still
437// referencing them because of the import. We make sure we always emit at least
438// one copy.
442 isPrevailing,
444 recordNewLinkage,
445 const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols) {
446 // We won't optimize the globals that are referenced by an alias for now
447 // Ideally we should turn the alias into a global and duplicate the definition
448 // when needed.
449 DenseSet<GlobalValueSummary *> GlobalInvolvedWithAlias;
450 for (auto &I : Index)
451 for (auto &S : I.second.SummaryList)
452 if (auto AS = dyn_cast<AliasSummary>(S.get()))
453 GlobalInvolvedWithAlias.insert(&AS->getAliasee());
454
455 for (auto &I : Index)
456 thinLTOResolvePrevailingGUID(C, Index.getValueInfo(I),
457 GlobalInvolvedWithAlias, isPrevailing,
458 recordNewLinkage, GUIDPreservedSymbols);
459}
460
462 ValueInfo VI, function_ref<bool(StringRef, ValueInfo)> isExported,
464 isPrevailing) {
465 auto ExternallyVisibleCopies =
466 llvm::count_if(VI.getSummaryList(),
467 [](const std::unique_ptr<GlobalValueSummary> &Summary) {
468 return !GlobalValue::isLocalLinkage(Summary->linkage());
469 });
470
471 for (auto &S : VI.getSummaryList()) {
472 // First see if we need to promote an internal value because it is not
473 // exported.
474 if (isExported(S->modulePath(), VI)) {
475 if (GlobalValue::isLocalLinkage(S->linkage()))
476 S->setLinkage(GlobalValue::ExternalLinkage);
477 continue;
478 }
479
480 // Otherwise, see if we can internalize.
482 continue;
483
484 // Non-exported values with external linkage can be internalized.
485 if (GlobalValue::isExternalLinkage(S->linkage())) {
486 S->setLinkage(GlobalValue::InternalLinkage);
487 continue;
488 }
489
490 // Non-exported function and variable definitions with a weak-for-linker
491 // linkage can be internalized in certain cases. The minimum legality
492 // requirements would be that they are not address taken to ensure that we
493 // don't break pointer equality checks, and that variables are either read-
494 // or write-only. For functions, this is the case if either all copies are
495 // [local_]unnamed_addr, or we can propagate reference edge attributes
496 // (which is how this is guaranteed for variables, when analyzing whether
497 // they are read or write-only).
498 //
499 // However, we only get to this code for weak-for-linkage values in one of
500 // two cases:
501 // 1) The prevailing copy is not in IR (it is in native code).
502 // 2) The prevailing copy in IR is not exported from its module.
503 // Additionally, at least for the new LTO API, case 2 will only happen if
504 // there is exactly one definition of the value (i.e. in exactly one
505 // module), as duplicate defs are result in the value being marked exported.
506 // Likely, users of the legacy LTO API are similar, however, currently there
507 // are llvm-lto based tests of the legacy LTO API that do not mark
508 // duplicate linkonce_odr copies as exported via the tool, so we need
509 // to handle that case below by checking the number of copies.
510 //
511 // Generally, we only want to internalize a weak-for-linker value in case
512 // 2, because in case 1 we cannot see how the value is used to know if it
513 // is read or write-only. We also don't want to bloat the binary with
514 // multiple internalized copies of non-prevailing linkonce/weak functions.
515 // Note if we don't internalize, we will convert non-prevailing copies to
516 // available_externally anyway, so that we drop them after inlining. The
517 // only reason to internalize such a function is if we indeed have a single
518 // copy, because internalizing it won't increase binary size, and enables
519 // use of inliner heuristics that are more aggressive in the face of a
520 // single call to a static (local). For variables, internalizing a read or
521 // write only variable can enable more aggressive optimization. However, we
522 // already perform this elsewhere in the ThinLTO backend handling for
523 // read or write-only variables (processGlobalForThinLTO).
524 //
525 // Therefore, only internalize linkonce/weak if there is a single copy, that
526 // is prevailing in this IR module. We can do so aggressively, without
527 // requiring the address to be insignificant, or that a variable be read or
528 // write-only.
529 if (!GlobalValue::isWeakForLinker(S->linkage()) ||
531 continue;
532
533 if (isPrevailing(VI.getGUID(), S.get()) && ExternallyVisibleCopies == 1)
534 S->setLinkage(GlobalValue::InternalLinkage);
535 }
536}
537
538// Update the linkages in the given \p Index to mark exported values
539// as external and non-exported values as internal.
542 function_ref<bool(StringRef, ValueInfo)> isExported,
544 isPrevailing) {
545 for (auto &I : Index)
546 thinLTOInternalizeAndPromoteGUID(Index.getValueInfo(I), isExported,
547 isPrevailing);
548}
549
550// Requires a destructor for std::vector<InputModule>.
551InputFile::~InputFile() = default;
552
554 std::unique_ptr<InputFile> File(new InputFile);
555
556 Expected<IRSymtabFile> FOrErr = readIRSymtab(Object);
557 if (!FOrErr)
558 return FOrErr.takeError();
559
560 File->TargetTriple = FOrErr->TheReader.getTargetTriple();
561 File->SourceFileName = FOrErr->TheReader.getSourceFileName();
562 File->COFFLinkerOpts = FOrErr->TheReader.getCOFFLinkerOpts();
563 File->DependentLibraries = FOrErr->TheReader.getDependentLibraries();
564 File->ComdatTable = FOrErr->TheReader.getComdatTable();
565
566 for (unsigned I = 0; I != FOrErr->Mods.size(); ++I) {
567 size_t Begin = File->Symbols.size();
568 for (const irsymtab::Reader::SymbolRef &Sym :
569 FOrErr->TheReader.module_symbols(I))
570 // Skip symbols that are irrelevant to LTO. Note that this condition needs
571 // to match the one in Skip() in LTO::addRegularLTO().
572 if (Sym.isGlobal() && !Sym.isFormatSpecific())
573 File->Symbols.push_back(Sym);
574 File->ModuleSymIndices.push_back({Begin, File->Symbols.size()});
575 }
576
577 File->Mods = FOrErr->Mods;
578 File->Strtab = std::move(FOrErr->Strtab);
579 return std::move(File);
580}
581
583 return Mods[0].getModuleIdentifier();
584}
585
587 assert(Mods.size() == 1 && "Expect only one bitcode module");
588 return Mods[0];
589}
590
591LTO::RegularLTOState::RegularLTOState(unsigned ParallelCodeGenParallelismLevel,
592 const Config &Conf)
593 : ParallelCodeGenParallelismLevel(ParallelCodeGenParallelismLevel),
594 Ctx(Conf), CombinedModule(std::make_unique<Module>("ld-temp.o", Ctx)),
595 Mover(std::make_unique<IRMover>(*CombinedModule)) {
596 CombinedModule->IsNewDbgInfoFormat = UseNewDbgInfoFormat;
597}
598
599LTO::ThinLTOState::ThinLTOState(ThinBackend Backend)
600 : Backend(Backend), CombinedIndex(/*HaveGVs*/ false) {
601 if (!Backend)
602 this->Backend =
604}
605
607 unsigned ParallelCodeGenParallelismLevel, LTOKind LTOMode)
608 : Conf(std::move(Conf)),
609 RegularLTO(ParallelCodeGenParallelismLevel, this->Conf),
610 ThinLTO(std::move(Backend)),
611 GlobalResolutions(std::make_optional<StringMap<GlobalResolution>>()),
612 LTOMode(LTOMode) {}
613
614// Requires a destructor for MapVector<BitcodeModule>.
615LTO::~LTO() = default;
616
617// Add the symbols in the given module to the GlobalResolutions map, and resolve
618// their partitions.
619void LTO::addModuleToGlobalRes(ArrayRef<InputFile::Symbol> Syms,
621 unsigned Partition, bool InSummary) {
622 auto *ResI = Res.begin();
623 auto *ResE = Res.end();
624 (void)ResE;
625 const Triple TT(RegularLTO.CombinedModule->getTargetTriple());
626 for (const InputFile::Symbol &Sym : Syms) {
627 assert(ResI != ResE);
628 SymbolResolution Res = *ResI++;
629
630 auto &GlobalRes = (*GlobalResolutions)[Sym.getName()];
631 GlobalRes.UnnamedAddr &= Sym.isUnnamedAddr();
632 if (Res.Prevailing) {
633 assert(!GlobalRes.Prevailing &&
634 "Multiple prevailing defs are not allowed");
635 GlobalRes.Prevailing = true;
636 GlobalRes.IRName = std::string(Sym.getIRName());
637 } else if (!GlobalRes.Prevailing && GlobalRes.IRName.empty()) {
638 // Sometimes it can be two copies of symbol in a module and prevailing
639 // symbol can have no IR name. That might happen if symbol is defined in
640 // module level inline asm block. In case we have multiple modules with
641 // the same symbol we want to use IR name of the prevailing symbol.
642 // Otherwise, if we haven't seen a prevailing symbol, set the name so that
643 // we can later use it to check if there is any prevailing copy in IR.
644 GlobalRes.IRName = std::string(Sym.getIRName());
645 }
646
647 // In rare occasion, the symbol used to initialize GlobalRes has a different
648 // IRName from the inspected Symbol. This can happen on macOS + iOS, when a
649 // symbol is referenced through its mangled name, say @"\01_symbol" while
650 // the IRName is @symbol (the prefix underscore comes from MachO mangling).
651 // In that case, we have the same actual Symbol that can get two different
652 // GUID, leading to some invalid internalization. Workaround this by marking
653 // the GlobalRes external.
654
655 // FIXME: instead of this check, it would be desirable to compute GUIDs
656 // based on mangled name, but this requires an access to the Target Triple
657 // and would be relatively invasive on the codebase.
658 if (GlobalRes.IRName != Sym.getIRName()) {
659 GlobalRes.Partition = GlobalResolution::External;
660 GlobalRes.VisibleOutsideSummary = true;
661 }
662
663 // Set the partition to external if we know it is re-defined by the linker
664 // with -defsym or -wrap options, used elsewhere, e.g. it is visible to a
665 // regular object, is referenced from llvm.compiler.used/llvm.used, or was
666 // already recorded as being referenced from a different partition.
667 if (Res.LinkerRedefined || Res.VisibleToRegularObj || Sym.isUsed() ||
668 (GlobalRes.Partition != GlobalResolution::Unknown &&
669 GlobalRes.Partition != Partition)) {
670 GlobalRes.Partition = GlobalResolution::External;
671 } else
672 // First recorded reference, save the current partition.
673 GlobalRes.Partition = Partition;
674
675 // Flag as visible outside of summary if visible from a regular object or
676 // from a module that does not have a summary.
677 GlobalRes.VisibleOutsideSummary |=
678 (Res.VisibleToRegularObj || Sym.isUsed() || !InSummary);
679
680 GlobalRes.ExportDynamic |= Res.ExportDynamic;
681 }
682}
683
686 StringRef Path = Input->getName();
687 OS << Path << '\n';
688 auto ResI = Res.begin();
689 for (const InputFile::Symbol &Sym : Input->symbols()) {
690 assert(ResI != Res.end());
691 SymbolResolution Res = *ResI++;
692
693 OS << "-r=" << Path << ',' << Sym.getName() << ',';
694 if (Res.Prevailing)
695 OS << 'p';
697 OS << 'l';
698 if (Res.VisibleToRegularObj)
699 OS << 'x';
700 if (Res.LinkerRedefined)
701 OS << 'r';
702 OS << '\n';
703 }
704 OS.flush();
705 assert(ResI == Res.end());
706}
707
708Error LTO::add(std::unique_ptr<InputFile> Input,
710 assert(!CalledGetMaxTasks);
711
712 if (Conf.ResolutionFile)
713 writeToResolutionFile(*Conf.ResolutionFile, Input.get(), Res);
714
715 if (RegularLTO.CombinedModule->getTargetTriple().empty()) {
716 RegularLTO.CombinedModule->setTargetTriple(Input->getTargetTriple());
717 if (Triple(Input->getTargetTriple()).isOSBinFormatELF())
719 }
720
721 const SymbolResolution *ResI = Res.begin();
722 for (unsigned I = 0; I != Input->Mods.size(); ++I)
723 if (Error Err = addModule(*Input, I, ResI, Res.end()))
724 return Err;
725
726 assert(ResI == Res.end());
727 return Error::success();
728}
729
730Error LTO::addModule(InputFile &Input, unsigned ModI,
731 const SymbolResolution *&ResI,
732 const SymbolResolution *ResE) {
733 Expected<BitcodeLTOInfo> LTOInfo = Input.Mods[ModI].getLTOInfo();
734 if (!LTOInfo)
735 return LTOInfo.takeError();
736
737 if (EnableSplitLTOUnit) {
738 // If only some modules were split, flag this in the index so that
739 // we can skip or error on optimizations that need consistently split
740 // modules (whole program devirt and lower type tests).
741 if (*EnableSplitLTOUnit != LTOInfo->EnableSplitLTOUnit)
742 ThinLTO.CombinedIndex.setPartiallySplitLTOUnits();
743 } else
744 EnableSplitLTOUnit = LTOInfo->EnableSplitLTOUnit;
745
746 BitcodeModule BM = Input.Mods[ModI];
747
748 if ((LTOMode == LTOK_UnifiedRegular || LTOMode == LTOK_UnifiedThin) &&
749 !LTOInfo->UnifiedLTO)
750 return make_error<StringError>(
751 "unified LTO compilation must use "
752 "compatible bitcode modules (use -funified-lto)",
754
755 if (LTOInfo->UnifiedLTO && LTOMode == LTOK_Default)
756 LTOMode = LTOK_UnifiedThin;
757
758 bool IsThinLTO = LTOInfo->IsThinLTO && (LTOMode != LTOK_UnifiedRegular);
759
760 auto ModSyms = Input.module_symbols(ModI);
761 addModuleToGlobalRes(ModSyms, {ResI, ResE},
762 IsThinLTO ? ThinLTO.ModuleMap.size() + 1 : 0,
763 LTOInfo->HasSummary);
764
765 if (IsThinLTO)
766 return addThinLTO(BM, ModSyms, ResI, ResE);
767
768 RegularLTO.EmptyCombinedModule = false;
770 addRegularLTO(BM, ModSyms, ResI, ResE);
771 if (!ModOrErr)
772 return ModOrErr.takeError();
773
774 if (!LTOInfo->HasSummary)
775 return linkRegularLTO(std::move(*ModOrErr), /*LivenessFromIndex=*/false);
776
777 // Regular LTO module summaries are added to a dummy module that represents
778 // the combined regular LTO module.
779 if (Error Err = BM.readSummary(ThinLTO.CombinedIndex, ""))
780 return Err;
781 RegularLTO.ModsWithSummaries.push_back(std::move(*ModOrErr));
782 return Error::success();
783}
784
785// Checks whether the given global value is in a non-prevailing comdat
786// (comdat containing values the linker indicated were not prevailing,
787// which we then dropped to available_externally), and if so, removes
788// it from the comdat. This is called for all global values to ensure the
789// comdat is empty rather than leaving an incomplete comdat. It is needed for
790// regular LTO modules, in case we are in a mixed-LTO mode (both regular
791// and thin LTO modules) compilation. Since the regular LTO module will be
792// linked first in the final native link, we want to make sure the linker
793// doesn't select any of these incomplete comdats that would be left
794// in the regular LTO module without this cleanup.
795static void
797 std::set<const Comdat *> &NonPrevailingComdats) {
798 Comdat *C = GV.getComdat();
799 if (!C)
800 return;
801
802 if (!NonPrevailingComdats.count(C))
803 return;
804
805 // Additionally need to drop all global values from the comdat to
806 // available_externally, to satisfy the COMDAT requirement that all members
807 // are discarded as a unit. The non-local linkage global values avoid
808 // duplicate definition linker errors.
810
811 if (auto GO = dyn_cast<GlobalObject>(&GV))
812 GO->setComdat(nullptr);
813}
814
815// Add a regular LTO object to the link.
816// The resulting module needs to be linked into the combined LTO module with
817// linkRegularLTO.
819LTO::addRegularLTO(BitcodeModule BM, ArrayRef<InputFile::Symbol> Syms,
820 const SymbolResolution *&ResI,
821 const SymbolResolution *ResE) {
822 RegularLTOState::AddedModule Mod;
824 BM.getLazyModule(RegularLTO.Ctx, /*ShouldLazyLoadMetadata*/ true,
825 /*IsImporting*/ false);
826 if (!MOrErr)
827 return MOrErr.takeError();
828 Module &M = **MOrErr;
829 Mod.M = std::move(*MOrErr);
830
831 if (Error Err = M.materializeMetadata())
832 return std::move(Err);
833
834 // If cfi.functions is present and we are in regular LTO mode, LowerTypeTests
835 // will rename local functions in the merged module as "<function name>.1".
836 // This causes linking errors, since other parts of the module expect the
837 // original function name.
838 if (LTOMode == LTOK_UnifiedRegular)
839 if (NamedMDNode *CfiFunctionsMD = M.getNamedMetadata("cfi.functions"))
840 M.eraseNamedMetadata(CfiFunctionsMD);
841
843
844 ModuleSymbolTable SymTab;
845 SymTab.addModule(&M);
846
847 for (GlobalVariable &GV : M.globals())
848 if (GV.hasAppendingLinkage())
849 Mod.Keep.push_back(&GV);
850
851 DenseSet<GlobalObject *> AliasedGlobals;
852 for (auto &GA : M.aliases())
853 if (GlobalObject *GO = GA.getAliaseeObject())
854 AliasedGlobals.insert(GO);
855
856 // In this function we need IR GlobalValues matching the symbols in Syms
857 // (which is not backed by a module), so we need to enumerate them in the same
858 // order. The symbol enumeration order of a ModuleSymbolTable intentionally
859 // matches the order of an irsymtab, but when we read the irsymtab in
860 // InputFile::create we omit some symbols that are irrelevant to LTO. The
861 // Skip() function skips the same symbols from the module as InputFile does
862 // from the symbol table.
863 auto MsymI = SymTab.symbols().begin(), MsymE = SymTab.symbols().end();
864 auto Skip = [&]() {
865 while (MsymI != MsymE) {
866 auto Flags = SymTab.getSymbolFlags(*MsymI);
867 if ((Flags & object::BasicSymbolRef::SF_Global) &&
869 return;
870 ++MsymI;
871 }
872 };
873 Skip();
874
875 std::set<const Comdat *> NonPrevailingComdats;
876 SmallSet<StringRef, 2> NonPrevailingAsmSymbols;
877 for (const InputFile::Symbol &Sym : Syms) {
878 assert(ResI != ResE);
879 SymbolResolution Res = *ResI++;
880
881 assert(MsymI != MsymE);
882 ModuleSymbolTable::Symbol Msym = *MsymI++;
883 Skip();
884
885 if (GlobalValue *GV = dyn_cast_if_present<GlobalValue *>(Msym)) {
886 if (Res.Prevailing) {
887 if (Sym.isUndefined())
888 continue;
889 Mod.Keep.push_back(GV);
890 // For symbols re-defined with linker -wrap and -defsym options,
891 // set the linkage to weak to inhibit IPO. The linkage will be
892 // restored by the linker.
893 if (Res.LinkerRedefined)
894 GV->setLinkage(GlobalValue::WeakAnyLinkage);
895
896 GlobalValue::LinkageTypes OriginalLinkage = GV->getLinkage();
897 if (GlobalValue::isLinkOnceLinkage(OriginalLinkage))
898 GV->setLinkage(GlobalValue::getWeakLinkage(
899 GlobalValue::isLinkOnceODRLinkage(OriginalLinkage)));
900 } else if (isa<GlobalObject>(GV) &&
901 (GV->hasLinkOnceODRLinkage() || GV->hasWeakODRLinkage() ||
902 GV->hasAvailableExternallyLinkage()) &&
903 !AliasedGlobals.count(cast<GlobalObject>(GV))) {
904 // Any of the above three types of linkage indicates that the
905 // chosen prevailing symbol will have the same semantics as this copy of
906 // the symbol, so we may be able to link it with available_externally
907 // linkage. We will decide later whether to do that when we link this
908 // module (in linkRegularLTO), based on whether it is undefined.
909 Mod.Keep.push_back(GV);
911 if (GV->hasComdat())
912 NonPrevailingComdats.insert(GV->getComdat());
913 cast<GlobalObject>(GV)->setComdat(nullptr);
914 }
915
916 // Set the 'local' flag based on the linker resolution for this symbol.
918 GV->setDSOLocal(true);
919 if (GV->hasDLLImportStorageClass())
920 GV->setDLLStorageClass(GlobalValue::DLLStorageClassTypes::
921 DefaultStorageClass);
922 }
923 } else if (auto *AS =
924 dyn_cast_if_present<ModuleSymbolTable::AsmSymbol *>(Msym)) {
925 // Collect non-prevailing symbols.
926 if (!Res.Prevailing)
927 NonPrevailingAsmSymbols.insert(AS->first);
928 } else {
929 llvm_unreachable("unknown symbol type");
930 }
931
932 // Common resolution: collect the maximum size/alignment over all commons.
933 // We also record if we see an instance of a common as prevailing, so that
934 // if none is prevailing we can ignore it later.
935 if (Sym.isCommon()) {
936 // FIXME: We should figure out what to do about commons defined by asm.
937 // For now they aren't reported correctly by ModuleSymbolTable.
938 auto &CommonRes = RegularLTO.Commons[std::string(Sym.getIRName())];
939 CommonRes.Size = std::max(CommonRes.Size, Sym.getCommonSize());
940 if (uint32_t SymAlignValue = Sym.getCommonAlignment()) {
941 CommonRes.Alignment =
942 std::max(Align(SymAlignValue), CommonRes.Alignment);
943 }
944 CommonRes.Prevailing |= Res.Prevailing;
945 }
946 }
947
948 if (!M.getComdatSymbolTable().empty())
949 for (GlobalValue &GV : M.global_values())
950 handleNonPrevailingComdat(GV, NonPrevailingComdats);
951
952 // Prepend ".lto_discard <sym>, <sym>*" directive to each module inline asm
953 // block.
954 if (!M.getModuleInlineAsm().empty()) {
955 std::string NewIA = ".lto_discard";
956 if (!NonPrevailingAsmSymbols.empty()) {
957 // Don't dicard a symbol if there is a live .symver for it.
959 M, [&](StringRef Name, StringRef Alias) {
960 if (!NonPrevailingAsmSymbols.count(Alias))
961 NonPrevailingAsmSymbols.erase(Name);
962 });
963 NewIA += " " + llvm::join(NonPrevailingAsmSymbols, ", ");
964 }
965 NewIA += "\n";
966 M.setModuleInlineAsm(NewIA + M.getModuleInlineAsm());
967 }
968
969 assert(MsymI == MsymE);
970 return std::move(Mod);
971}
972
973Error LTO::linkRegularLTO(RegularLTOState::AddedModule Mod,
974 bool LivenessFromIndex) {
975 std::vector<GlobalValue *> Keep;
976 for (GlobalValue *GV : Mod.Keep) {
977 if (LivenessFromIndex && !ThinLTO.CombinedIndex.isGUIDLive(GV->getGUID())) {
978 if (Function *F = dyn_cast<Function>(GV)) {
979 if (DiagnosticOutputFile) {
980 if (Error Err = F->materialize())
981 return Err;
982 OptimizationRemarkEmitter ORE(F, nullptr);
983 ORE.emit(OptimizationRemark(DEBUG_TYPE, "deadfunction", F)
984 << ore::NV("Function", F)
985 << " not added to the combined module ");
986 }
987 }
988 continue;
989 }
990
992 Keep.push_back(GV);
993 continue;
994 }
995
996 // Only link available_externally definitions if we don't already have a
997 // definition.
998 GlobalValue *CombinedGV =
999 RegularLTO.CombinedModule->getNamedValue(GV->getName());
1000 if (CombinedGV && !CombinedGV->isDeclaration())
1001 continue;
1002
1003 Keep.push_back(GV);
1004 }
1005
1006 return RegularLTO.Mover->move(std::move(Mod.M), Keep, nullptr,
1007 /* IsPerformingImport */ false);
1008}
1009
1010// Add a ThinLTO module to the link.
1011Error LTO::addThinLTO(BitcodeModule BM, ArrayRef<InputFile::Symbol> Syms,
1012 const SymbolResolution *&ResI,
1013 const SymbolResolution *ResE) {
1014 const SymbolResolution *ResITmp = ResI;
1015 for (const InputFile::Symbol &Sym : Syms) {
1016 assert(ResITmp != ResE);
1017 SymbolResolution Res = *ResITmp++;
1018
1019 if (!Sym.getIRName().empty()) {
1021 Sym.getIRName(), GlobalValue::ExternalLinkage, ""));
1022 if (Res.Prevailing)
1023 ThinLTO.PrevailingModuleForGUID[GUID] = BM.getModuleIdentifier();
1024 }
1025 }
1026
1027 if (Error Err =
1028 BM.readSummary(ThinLTO.CombinedIndex, BM.getModuleIdentifier(),
1029 [&](GlobalValue::GUID GUID) {
1030 return ThinLTO.PrevailingModuleForGUID[GUID] ==
1031 BM.getModuleIdentifier();
1032 }))
1033 return Err;
1034 LLVM_DEBUG(dbgs() << "Module " << BM.getModuleIdentifier() << "\n");
1035
1036 for (const InputFile::Symbol &Sym : Syms) {
1037 assert(ResI != ResE);
1038 SymbolResolution Res = *ResI++;
1039
1040 if (!Sym.getIRName().empty()) {
1042 Sym.getIRName(), GlobalValue::ExternalLinkage, ""));
1043 if (Res.Prevailing) {
1044 assert(ThinLTO.PrevailingModuleForGUID[GUID] ==
1045 BM.getModuleIdentifier());
1046
1047 // For linker redefined symbols (via --wrap or --defsym) we want to
1048 // switch the linkage to `weak` to prevent IPOs from happening.
1049 // Find the summary in the module for this very GV and record the new
1050 // linkage so that we can switch it when we import the GV.
1051 if (Res.LinkerRedefined)
1052 if (auto S = ThinLTO.CombinedIndex.findSummaryInModule(
1053 GUID, BM.getModuleIdentifier()))
1054 S->setLinkage(GlobalValue::WeakAnyLinkage);
1055 }
1056
1057 // If the linker resolved the symbol to a local definition then mark it
1058 // as local in the summary for the module we are adding.
1060 if (auto S = ThinLTO.CombinedIndex.findSummaryInModule(
1061 GUID, BM.getModuleIdentifier())) {
1062 S->setDSOLocal(true);
1063 }
1064 }
1065 }
1066 }
1067
1068 if (!ThinLTO.ModuleMap.insert({BM.getModuleIdentifier(), BM}).second)
1069 return make_error<StringError>(
1070 "Expected at most one ThinLTO module per bitcode file",
1072
1073 if (!Conf.ThinLTOModulesToCompile.empty()) {
1074 if (!ThinLTO.ModulesToCompile)
1075 ThinLTO.ModulesToCompile = ModuleMapType();
1076 // This is a fuzzy name matching where only modules with name containing the
1077 // specified switch values are going to be compiled.
1078 for (const std::string &Name : Conf.ThinLTOModulesToCompile) {
1079 if (BM.getModuleIdentifier().contains(Name)) {
1080 ThinLTO.ModulesToCompile->insert({BM.getModuleIdentifier(), BM});
1081 llvm::errs() << "[ThinLTO] Selecting " << BM.getModuleIdentifier()
1082 << " to compile\n";
1083 }
1084 }
1085 }
1086
1087 return Error::success();
1088}
1089
1090unsigned LTO::getMaxTasks() const {
1091 CalledGetMaxTasks = true;
1092 auto ModuleCount = ThinLTO.ModulesToCompile ? ThinLTO.ModulesToCompile->size()
1093 : ThinLTO.ModuleMap.size();
1094 return RegularLTO.ParallelCodeGenParallelismLevel + ModuleCount;
1095}
1096
1097// If only some of the modules were split, we cannot correctly handle
1098// code that contains type tests or type checked loads.
1099Error LTO::checkPartiallySplit() {
1100 if (!ThinLTO.CombinedIndex.partiallySplitLTOUnits())
1101 return Error::success();
1102
1103 Function *TypeTestFunc = RegularLTO.CombinedModule->getFunction(
1104 Intrinsic::getName(Intrinsic::type_test));
1105 Function *TypeCheckedLoadFunc = RegularLTO.CombinedModule->getFunction(
1106 Intrinsic::getName(Intrinsic::type_checked_load));
1107 Function *TypeCheckedLoadRelativeFunc =
1108 RegularLTO.CombinedModule->getFunction(
1109 Intrinsic::getName(Intrinsic::type_checked_load_relative));
1110
1111 // First check if there are type tests / type checked loads in the
1112 // merged regular LTO module IR.
1113 if ((TypeTestFunc && !TypeTestFunc->use_empty()) ||
1114 (TypeCheckedLoadFunc && !TypeCheckedLoadFunc->use_empty()) ||
1115 (TypeCheckedLoadRelativeFunc &&
1116 !TypeCheckedLoadRelativeFunc->use_empty()))
1117 return make_error<StringError>(
1118 "inconsistent LTO Unit splitting (recompile with -fsplit-lto-unit)",
1120
1121 // Otherwise check if there are any recorded in the combined summary from the
1122 // ThinLTO modules.
1123 for (auto &P : ThinLTO.CombinedIndex) {
1124 for (auto &S : P.second.SummaryList) {
1125 auto *FS = dyn_cast<FunctionSummary>(S.get());
1126 if (!FS)
1127 continue;
1128 if (!FS->type_test_assume_vcalls().empty() ||
1129 !FS->type_checked_load_vcalls().empty() ||
1130 !FS->type_test_assume_const_vcalls().empty() ||
1131 !FS->type_checked_load_const_vcalls().empty() ||
1132 !FS->type_tests().empty())
1133 return make_error<StringError>(
1134 "inconsistent LTO Unit splitting (recompile with -fsplit-lto-unit)",
1136 }
1137 }
1138 return Error::success();
1139}
1140
1142 // Compute "dead" symbols, we don't want to import/export these!
1143 DenseSet<GlobalValue::GUID> GUIDPreservedSymbols;
1144 DenseMap<GlobalValue::GUID, PrevailingType> GUIDPrevailingResolutions;
1145 for (auto &Res : *GlobalResolutions) {
1146 // Normally resolution have IR name of symbol. We can do nothing here
1147 // otherwise. See comments in GlobalResolution struct for more details.
1148 if (Res.second.IRName.empty())
1149 continue;
1150
1152 GlobalValue::dropLLVMManglingEscape(Res.second.IRName));
1153
1154 if (Res.second.VisibleOutsideSummary && Res.second.Prevailing)
1155 GUIDPreservedSymbols.insert(GUID);
1156
1157 if (Res.second.ExportDynamic)
1158 DynamicExportSymbols.insert(GUID);
1159
1160 GUIDPrevailingResolutions[GUID] =
1162 }
1163
1164 auto isPrevailing = [&](GlobalValue::GUID G) {
1165 auto It = GUIDPrevailingResolutions.find(G);
1166 if (It == GUIDPrevailingResolutions.end())
1168 return It->second;
1169 };
1170 computeDeadSymbolsWithConstProp(ThinLTO.CombinedIndex, GUIDPreservedSymbols,
1171 isPrevailing, Conf.OptLevel > 0);
1172
1173 // Setup output file to emit statistics.
1174 auto StatsFileOrErr = setupStatsFile(Conf.StatsFile);
1175 if (!StatsFileOrErr)
1176 return StatsFileOrErr.takeError();
1177 std::unique_ptr<ToolOutputFile> StatsFile = std::move(StatsFileOrErr.get());
1178
1179 // TODO: Ideally this would be controlled automatically by detecting that we
1180 // are linking with an allocator that supports these interfaces, rather than
1181 // an internal option (which would still be needed for tests, however). For
1182 // example, if the library exported a symbol like __malloc_hot_cold the linker
1183 // could recognize that and set a flag in the lto::Config.
1185 ThinLTO.CombinedIndex.setWithSupportsHotColdNew();
1186
1187 Error Result = runRegularLTO(AddStream);
1188 if (!Result)
1189 // This will reset the GlobalResolutions optional once done with it to
1190 // reduce peak memory before importing.
1191 Result = runThinLTO(AddStream, Cache, GUIDPreservedSymbols);
1192
1193 if (StatsFile)
1194 PrintStatisticsJSON(StatsFile->os());
1195
1196 return Result;
1197}
1198
1200 const ModuleSummaryIndex &Index) {
1201 if (Index.withSupportsHotColdNew())
1202 return;
1203
1204 // The profile matcher applies hotness attributes directly for allocations,
1205 // and those will cause us to generate calls to the hot/cold interfaces
1206 // unconditionally. If supports-hot-cold-new was not enabled in the LTO
1207 // link then assume we don't want these calls (e.g. not linking with
1208 // the appropriate library, or otherwise trying to disable this behavior).
1209 for (auto &F : Mod) {
1210 for (auto &BB : F) {
1211 for (auto &I : BB) {
1212 auto *CI = dyn_cast<CallBase>(&I);
1213 if (!CI)
1214 continue;
1215 if (CI->hasFnAttr("memprof"))
1216 CI->removeFnAttr("memprof");
1217 // Strip off all memprof metadata as it is no longer needed.
1218 // Importantly, this avoids the addition of new memprof attributes
1219 // after inlining propagation.
1220 // TODO: If we support additional types of MemProf metadata beyond hot
1221 // and cold, we will need to update the metadata based on the allocator
1222 // APIs supported instead of completely stripping all.
1223 CI->setMetadata(LLVMContext::MD_memprof, nullptr);
1224 CI->setMetadata(LLVMContext::MD_callsite, nullptr);
1225 }
1226 }
1227 }
1228}
1229
1230Error LTO::runRegularLTO(AddStreamFn AddStream) {
1231 // Setup optimization remarks.
1232 auto DiagFileOrErr = lto::setupLLVMOptimizationRemarks(
1233 RegularLTO.CombinedModule->getContext(), Conf.RemarksFilename,
1236 LLVM_DEBUG(dbgs() << "Running regular LTO\n");
1237 if (!DiagFileOrErr)
1238 return DiagFileOrErr.takeError();
1239 DiagnosticOutputFile = std::move(*DiagFileOrErr);
1240
1241 // Finalize linking of regular LTO modules containing summaries now that
1242 // we have computed liveness information.
1243 for (auto &M : RegularLTO.ModsWithSummaries)
1244 if (Error Err = linkRegularLTO(std::move(M),
1245 /*LivenessFromIndex=*/true))
1246 return Err;
1247
1248 // Ensure we don't have inconsistently split LTO units with type tests.
1249 // FIXME: this checks both LTO and ThinLTO. It happens to work as we take
1250 // this path both cases but eventually this should be split into two and
1251 // do the ThinLTO checks in `runThinLTO`.
1252 if (Error Err = checkPartiallySplit())
1253 return Err;
1254
1255 // Make sure commons have the right size/alignment: we kept the largest from
1256 // all the prevailing when adding the inputs, and we apply it here.
1257 const DataLayout &DL = RegularLTO.CombinedModule->getDataLayout();
1258 for (auto &I : RegularLTO.Commons) {
1259 if (!I.second.Prevailing)
1260 // Don't do anything if no instance of this common was prevailing.
1261 continue;
1262 GlobalVariable *OldGV = RegularLTO.CombinedModule->getNamedGlobal(I.first);
1263 if (OldGV && DL.getTypeAllocSize(OldGV->getValueType()) == I.second.Size) {
1264 // Don't create a new global if the type is already correct, just make
1265 // sure the alignment is correct.
1266 OldGV->setAlignment(I.second.Alignment);
1267 continue;
1268 }
1269 ArrayType *Ty =
1270 ArrayType::get(Type::getInt8Ty(RegularLTO.Ctx), I.second.Size);
1271 auto *GV = new GlobalVariable(*RegularLTO.CombinedModule, Ty, false,
1274 GV->setAlignment(I.second.Alignment);
1275 if (OldGV) {
1276 OldGV->replaceAllUsesWith(GV);
1277 GV->takeName(OldGV);
1278 OldGV->eraseFromParent();
1279 } else {
1280 GV->setName(I.first);
1281 }
1282 }
1283
1284 updateMemProfAttributes(*RegularLTO.CombinedModule, ThinLTO.CombinedIndex);
1285
1286 bool WholeProgramVisibilityEnabledInLTO =
1288 // If validation is enabled, upgrade visibility only when all vtables
1289 // have typeinfos.
1291
1292 // This returns true when the name is local or not defined. Locals are
1293 // expected to be handled separately.
1294 auto IsVisibleToRegularObj = [&](StringRef name) {
1295 auto It = GlobalResolutions->find(name);
1296 return (It == GlobalResolutions->end() || It->second.VisibleOutsideSummary);
1297 };
1298
1299 // If allowed, upgrade public vcall visibility metadata to linkage unit
1300 // visibility before whole program devirtualization in the optimizer.
1302 *RegularLTO.CombinedModule, WholeProgramVisibilityEnabledInLTO,
1303 DynamicExportSymbols, Conf.ValidateAllVtablesHaveTypeInfos,
1304 IsVisibleToRegularObj);
1305 updatePublicTypeTestCalls(*RegularLTO.CombinedModule,
1306 WholeProgramVisibilityEnabledInLTO);
1307
1308 if (Conf.PreOptModuleHook &&
1309 !Conf.PreOptModuleHook(0, *RegularLTO.CombinedModule))
1310 return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
1311
1312 if (!Conf.CodeGenOnly) {
1313 for (const auto &R : *GlobalResolutions) {
1314 GlobalValue *GV =
1315 RegularLTO.CombinedModule->getNamedValue(R.second.IRName);
1316 if (!R.second.isPrevailingIRSymbol())
1317 continue;
1318 if (R.second.Partition != 0 &&
1319 R.second.Partition != GlobalResolution::External)
1320 continue;
1321
1322 // Ignore symbols defined in other partitions.
1323 // Also skip declarations, which are not allowed to have internal linkage.
1324 if (!GV || GV->hasLocalLinkage() || GV->isDeclaration())
1325 continue;
1326
1327 // Symbols that are marked DLLImport or DLLExport should not be
1328 // internalized, as they are either externally visible or referencing
1329 // external symbols. Symbols that have AvailableExternally or Appending
1330 // linkage might be used by future passes and should be kept as is.
1331 // These linkages are seen in Unified regular LTO, because the process
1332 // of creating split LTO units introduces symbols with that linkage into
1333 // one of the created modules. Normally, only the ThinLTO backend would
1334 // compile this module, but Unified Regular LTO processes both
1335 // modules created by the splitting process as regular LTO modules.
1336 if ((LTOMode == LTOKind::LTOK_UnifiedRegular) &&
1339 continue;
1340
1341 GV->setUnnamedAddr(R.second.UnnamedAddr ? GlobalValue::UnnamedAddr::Global
1343 if (EnableLTOInternalization && R.second.Partition == 0)
1345 }
1346
1347 if (Conf.PostInternalizeModuleHook &&
1348 !Conf.PostInternalizeModuleHook(0, *RegularLTO.CombinedModule))
1349 return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
1350 }
1351
1352 if (!RegularLTO.EmptyCombinedModule || Conf.AlwaysEmitRegularLTOObj) {
1353 if (Error Err =
1354 backend(Conf, AddStream, RegularLTO.ParallelCodeGenParallelismLevel,
1355 *RegularLTO.CombinedModule, ThinLTO.CombinedIndex))
1356 return Err;
1357 }
1358
1359 return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
1360}
1361
1362static const char *libcallRoutineNames[] = {
1363#define HANDLE_LIBCALL(code, name) name,
1364#include "llvm/IR/RuntimeLibcalls.def"
1365#undef HANDLE_LIBCALL
1366};
1367
1370}
1371
1372/// This class defines the interface to the ThinLTO backend.
1374protected:
1375 const Config &Conf;
1380
1381public:
1383 const Config &Conf, ModuleSummaryIndex &CombinedIndex,
1384 const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1385 lto::IndexWriteCallback OnWrite, bool ShouldEmitImportsFiles)
1386 : Conf(Conf), CombinedIndex(CombinedIndex),
1387 ModuleToDefinedGVSummaries(ModuleToDefinedGVSummaries),
1388 OnWrite(OnWrite), ShouldEmitImportsFiles(ShouldEmitImportsFiles) {}
1389
1390 virtual ~ThinBackendProc() = default;
1391 virtual Error start(
1392 unsigned Task, BitcodeModule BM,
1393 const FunctionImporter::ImportMapTy &ImportList,
1394 const FunctionImporter::ExportSetTy &ExportList,
1395 const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
1397 virtual Error wait() = 0;
1398 virtual unsigned getThreadCount() = 0;
1399
1400 // Write sharded indices and (optionally) imports to disk
1402 llvm::StringRef ModulePath,
1403 const std::string &NewModulePath) {
1404 std::map<std::string, GVSummaryMapTy> ModuleToSummariesForIndex;
1405
1406 std::error_code EC;
1407 gatherImportedSummariesForModule(ModulePath, ModuleToDefinedGVSummaries,
1408 ImportList, ModuleToSummariesForIndex);
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 // TODO: Serialize declaration bits to bitcode.
1416 writeIndexToFile(CombinedIndex, OS, &ModuleToSummariesForIndex);
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:684
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:362
static void handleNonPrevailingComdat(GlobalValue &GV, std::set< const Comdat * > &NonPrevailingComdats)
Definition: LTO.cpp:796
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:461
cl::opt< bool > UseNewDbgInfoFormat
static const char * libcallRoutineNames[]
Definition: LTO.cpp:1362
#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:49
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:846
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:417
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 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:553
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:582
BitcodeModule & getSingleBitcodeModule()
Definition: LTO.cpp:586
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:708
LTO(Config Conf, ThinBackend Backend=nullptr, unsigned ParallelCodeGenParallelismLevel=1, LTOKind LTOMode=LTOK_Default)
Create an LTO object.
Definition: LTO.cpp:606
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:1368
Error run(AddStreamFn AddStream, FileCache Cache=nullptr)
Runs the LTO pipeline.
Definition: LTO.cpp:1141
unsigned getMaxTasks() const
Returns an upper bound on the number of tasks that the client may expect.
Definition: LTO.cpp:1090
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:471
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:1373
virtual unsigned getThreadCount()=0
const Config & Conf
Definition: LTO.cpp:1375
lto::IndexWriteCallback OnWrite
Definition: LTO.cpp:1378
Error emitFiles(const FunctionImporter::ImportMapTy &ImportList, llvm::StringRef ModulePath, const std::string &NewModulePath)
Definition: LTO.cpp:1401
virtual Error wait()=0
ThinBackendProc(const Config &Conf, ModuleSummaryIndex &CombinedIndex, const DenseMap< StringRef, GVSummaryMapTy > &ModuleToDefinedGVSummaries, lto::IndexWriteCallback OnWrite, bool ShouldEmitImportsFiles)
Definition: LTO.cpp:1382
const DenseMap< StringRef, GVSummaryMapTy > & ModuleToDefinedGVSummaries
Definition: LTO.cpp:1377
ModuleSummaryIndex & CombinedIndex
Definition: LTO.cpp:1376
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:1042
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:1199
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 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
void writeIndexToFile(const ModuleSummaryIndex &Index, raw_ostream &Out, const std::map< std::string, GVSummaryMapTy > *ModuleToSummariesForIndex=nullptr)
Write the specified module summary index to the given raw output stream, where it will be written in ...
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:540
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
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:439
void gatherImportedSummariesForModule(StringRef ModulePath, const DenseMap< StringRef, GVSummaryMapTy > &ModuleToDefinedGVSummaries, const FunctionImporter::ImportMapTy &ImportList, std::map< std::string, GVSummaryMapTy > &ModuleToSummariesForIndex)
Compute the set of summaries needed for a ThinLTO backend compilation of ModulePath.
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.
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