LLVM 17.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"
57
58#include <optional>
59#include <set>
60
61using namespace llvm;
62using namespace lto;
63using namespace object;
64
65#define DEBUG_TYPE "lto"
66
67static cl::opt<bool>
68 DumpThinCGSCCs("dump-thin-cg-sccs", cl::init(false), cl::Hidden,
69 cl::desc("Dump the SCCs in the ThinLTO index's callgraph"));
70
71namespace llvm {
72/// Enable global value internalization in LTO.
74 "enable-lto-internalization", cl::init(true), cl::Hidden,
75 cl::desc("Enable global value internalization in LTO"));
76}
77
78// Computes a unique hash for the Module considering the current list of
79// export/import and other global analysis results.
80// The hash is produced in \p Key.
82 SmallString<40> &Key, const Config &Conf, const ModuleSummaryIndex &Index,
83 StringRef ModuleID, const FunctionImporter::ImportMapTy &ImportList,
84 const FunctionImporter::ExportSetTy &ExportList,
85 const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
86 const GVSummaryMapTy &DefinedGlobals,
87 const std::set<GlobalValue::GUID> &CfiFunctionDefs,
88 const std::set<GlobalValue::GUID> &CfiFunctionDecls) {
89 // Compute the unique hash for this entry.
90 // This is based on the current compiler version, the module itself, the
91 // export list, the hash for every single module in the import list, the
92 // list of ResolvedODR for the module, and the list of preserved symbols.
93 SHA1 Hasher;
94
95 // Start with the compiler revision
96 Hasher.update(LLVM_VERSION_STRING);
97#ifdef LLVM_REVISION
98 Hasher.update(LLVM_REVISION);
99#endif
100
101 // Include the parts of the LTO configuration that affect code generation.
102 auto AddString = [&](StringRef Str) {
103 Hasher.update(Str);
104 Hasher.update(ArrayRef<uint8_t>{0});
105 };
106 auto AddUnsigned = [&](unsigned I) {
107 uint8_t Data[4];
109 Hasher.update(ArrayRef<uint8_t>{Data, 4});
110 };
111 auto AddUint64 = [&](uint64_t I) {
112 uint8_t Data[8];
114 Hasher.update(ArrayRef<uint8_t>{Data, 8});
115 };
116 AddString(Conf.CPU);
117 // FIXME: Hash more of Options. For now all clients initialize Options from
118 // command-line flags (which is unsupported in production), but may set
119 // RelaxELFRelocations. The clang driver can also pass FunctionSections,
120 // DataSections and DebuggerTuning via command line flags.
121 AddUnsigned(Conf.Options.RelaxELFRelocations);
122 AddUnsigned(Conf.Options.FunctionSections);
123 AddUnsigned(Conf.Options.DataSections);
124 AddUnsigned((unsigned)Conf.Options.DebuggerTuning);
125 for (auto &A : Conf.MAttrs)
126 AddString(A);
127 if (Conf.RelocModel)
128 AddUnsigned(*Conf.RelocModel);
129 else
130 AddUnsigned(-1);
131 if (Conf.CodeModel)
132 AddUnsigned(*Conf.CodeModel);
133 else
134 AddUnsigned(-1);
135 for (const auto &S : Conf.MllvmArgs)
136 AddString(S);
137 AddUnsigned(Conf.CGOptLevel);
138 AddUnsigned(Conf.CGFileType);
139 AddUnsigned(Conf.OptLevel);
140 AddUnsigned(Conf.Freestanding);
141 AddString(Conf.OptPipeline);
142 AddString(Conf.AAPipeline);
143 AddString(Conf.OverrideTriple);
144 AddString(Conf.DefaultTriple);
145 AddString(Conf.DwoDir);
146
147 // Include the hash for the current module
148 auto ModHash = Index.getModuleHash(ModuleID);
149 Hasher.update(ArrayRef<uint8_t>((uint8_t *)&ModHash[0], sizeof(ModHash)));
150
151 std::vector<uint64_t> ExportsGUID;
152 ExportsGUID.reserve(ExportList.size());
153 for (const auto &VI : ExportList) {
154 auto GUID = VI.getGUID();
155 ExportsGUID.push_back(GUID);
156 }
157
158 // Sort the export list elements GUIDs.
159 llvm::sort(ExportsGUID);
160 for (uint64_t GUID : ExportsGUID) {
161 // The export list can impact the internalization, be conservative here
162 Hasher.update(ArrayRef<uint8_t>((uint8_t *)&GUID, sizeof(GUID)));
163 }
164
165 // Include the hash for every module we import functions from. The set of
166 // imported symbols for each module may affect code generation and is
167 // sensitive to link order, so include that as well.
168 using ImportMapIteratorTy = FunctionImporter::ImportMapTy::const_iterator;
169 std::vector<ImportMapIteratorTy> ImportModulesVector;
170 ImportModulesVector.reserve(ImportList.size());
171
172 for (ImportMapIteratorTy It = ImportList.begin(); It != ImportList.end();
173 ++It) {
174 ImportModulesVector.push_back(It);
175 }
176 llvm::sort(ImportModulesVector,
177 [](const ImportMapIteratorTy &Lhs, const ImportMapIteratorTy &Rhs)
178 -> bool { return Lhs->getKey() < Rhs->getKey(); });
179 for (const ImportMapIteratorTy &EntryIt : ImportModulesVector) {
180 auto ModHash = Index.getModuleHash(EntryIt->first());
181 Hasher.update(ArrayRef<uint8_t>((uint8_t *)&ModHash[0], sizeof(ModHash)));
182
183 AddUint64(EntryIt->second.size());
184 for (auto &Fn : EntryIt->second)
185 AddUint64(Fn);
186 }
187
188 // Include the hash for the resolved ODR.
189 for (auto &Entry : ResolvedODR) {
190 Hasher.update(ArrayRef<uint8_t>((const uint8_t *)&Entry.first,
191 sizeof(GlobalValue::GUID)));
192 Hasher.update(ArrayRef<uint8_t>((const uint8_t *)&Entry.second,
194 }
195
196 // Members of CfiFunctionDefs and CfiFunctionDecls that are referenced or
197 // defined in this module.
198 std::set<GlobalValue::GUID> UsedCfiDefs;
199 std::set<GlobalValue::GUID> UsedCfiDecls;
200
201 // Typeids used in this module.
202 std::set<GlobalValue::GUID> UsedTypeIds;
203
204 auto AddUsedCfiGlobal = [&](GlobalValue::GUID ValueGUID) {
205 if (CfiFunctionDefs.count(ValueGUID))
206 UsedCfiDefs.insert(ValueGUID);
207 if (CfiFunctionDecls.count(ValueGUID))
208 UsedCfiDecls.insert(ValueGUID);
209 };
210
211 auto AddUsedThings = [&](GlobalValueSummary *GS) {
212 if (!GS) return;
213 AddUnsigned(GS->getVisibility());
214 AddUnsigned(GS->isLive());
215 AddUnsigned(GS->canAutoHide());
216 for (const ValueInfo &VI : GS->refs()) {
217 AddUnsigned(VI.isDSOLocal(Index.withDSOLocalPropagation()));
218 AddUsedCfiGlobal(VI.getGUID());
219 }
220 if (auto *GVS = dyn_cast<GlobalVarSummary>(GS)) {
221 AddUnsigned(GVS->maybeReadOnly());
222 AddUnsigned(GVS->maybeWriteOnly());
223 }
224 if (auto *FS = dyn_cast<FunctionSummary>(GS)) {
225 for (auto &TT : FS->type_tests())
226 UsedTypeIds.insert(TT);
227 for (auto &TT : FS->type_test_assume_vcalls())
228 UsedTypeIds.insert(TT.GUID);
229 for (auto &TT : FS->type_checked_load_vcalls())
230 UsedTypeIds.insert(TT.GUID);
231 for (auto &TT : FS->type_test_assume_const_vcalls())
232 UsedTypeIds.insert(TT.VFunc.GUID);
233 for (auto &TT : FS->type_checked_load_const_vcalls())
234 UsedTypeIds.insert(TT.VFunc.GUID);
235 for (auto &ET : FS->calls()) {
236 AddUnsigned(ET.first.isDSOLocal(Index.withDSOLocalPropagation()));
237 AddUsedCfiGlobal(ET.first.getGUID());
238 }
239 }
240 };
241
242 // Include the hash for the linkage type to reflect internalization and weak
243 // resolution, and collect any used type identifier resolutions.
244 for (auto &GS : DefinedGlobals) {
245 GlobalValue::LinkageTypes Linkage = GS.second->linkage();
246 Hasher.update(
247 ArrayRef<uint8_t>((const uint8_t *)&Linkage, sizeof(Linkage)));
248 AddUsedCfiGlobal(GS.first);
249 AddUsedThings(GS.second);
250 }
251
252 // Imported functions may introduce new uses of type identifier resolutions,
253 // so we need to collect their used resolutions as well.
254 for (auto &ImpM : ImportList)
255 for (auto &ImpF : ImpM.second) {
256 GlobalValueSummary *S = Index.findSummaryInModule(ImpF, ImpM.first());
257 AddUsedThings(S);
258 // If this is an alias, we also care about any types/etc. that the aliasee
259 // may reference.
260 if (auto *AS = dyn_cast_or_null<AliasSummary>(S))
261 AddUsedThings(AS->getBaseObject());
262 }
263
264 auto AddTypeIdSummary = [&](StringRef TId, const TypeIdSummary &S) {
265 AddString(TId);
266
267 AddUnsigned(S.TTRes.TheKind);
268 AddUnsigned(S.TTRes.SizeM1BitWidth);
269
270 AddUint64(S.TTRes.AlignLog2);
271 AddUint64(S.TTRes.SizeM1);
272 AddUint64(S.TTRes.BitMask);
273 AddUint64(S.TTRes.InlineBits);
274
275 AddUint64(S.WPDRes.size());
276 for (auto &WPD : S.WPDRes) {
277 AddUnsigned(WPD.first);
278 AddUnsigned(WPD.second.TheKind);
279 AddString(WPD.second.SingleImplName);
280
281 AddUint64(WPD.second.ResByArg.size());
282 for (auto &ByArg : WPD.second.ResByArg) {
283 AddUint64(ByArg.first.size());
284 for (uint64_t Arg : ByArg.first)
285 AddUint64(Arg);
286 AddUnsigned(ByArg.second.TheKind);
287 AddUint64(ByArg.second.Info);
288 AddUnsigned(ByArg.second.Byte);
289 AddUnsigned(ByArg.second.Bit);
290 }
291 }
292 };
293
294 // Include the hash for all type identifiers used by this module.
295 for (GlobalValue::GUID TId : UsedTypeIds) {
296 auto TidIter = Index.typeIds().equal_range(TId);
297 for (auto It = TidIter.first; It != TidIter.second; ++It)
298 AddTypeIdSummary(It->second.first, It->second.second);
299 }
300
301 AddUnsigned(UsedCfiDefs.size());
302 for (auto &V : UsedCfiDefs)
303 AddUint64(V);
304
305 AddUnsigned(UsedCfiDecls.size());
306 for (auto &V : UsedCfiDecls)
307 AddUint64(V);
308
309 if (!Conf.SampleProfile.empty()) {
310 auto FileOrErr = MemoryBuffer::getFile(Conf.SampleProfile);
311 if (FileOrErr) {
312 Hasher.update(FileOrErr.get()->getBuffer());
313
314 if (!Conf.ProfileRemapping.empty()) {
315 FileOrErr = MemoryBuffer::getFile(Conf.ProfileRemapping);
316 if (FileOrErr)
317 Hasher.update(FileOrErr.get()->getBuffer());
318 }
319 }
320 }
321
322 Key = toHex(Hasher.result());
323}
324
326 const Config &C, ValueInfo VI,
327 DenseSet<GlobalValueSummary *> &GlobalInvolvedWithAlias,
329 isPrevailing,
331 recordNewLinkage,
332 const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols) {
334 C.VisibilityScheme == Config::ELF ? VI.getELFVisibility()
336 for (auto &S : VI.getSummaryList()) {
337 GlobalValue::LinkageTypes OriginalLinkage = S->linkage();
338 // Ignore local and appending linkage values since the linker
339 // doesn't resolve them.
340 if (GlobalValue::isLocalLinkage(OriginalLinkage) ||
342 continue;
343 // We need to emit only one of these. The prevailing module will keep it,
344 // but turned into a weak, while the others will drop it when possible.
345 // This is both a compile-time optimization and a correctness
346 // transformation. This is necessary for correctness when we have exported
347 // a reference - we need to convert the linkonce to weak to
348 // ensure a copy is kept to satisfy the exported reference.
349 // FIXME: We may want to split the compile time and correctness
350 // aspects into separate routines.
351 if (isPrevailing(VI.getGUID(), S.get())) {
352 if (GlobalValue::isLinkOnceLinkage(OriginalLinkage)) {
353 S->setLinkage(GlobalValue::getWeakLinkage(
354 GlobalValue::isLinkOnceODRLinkage(OriginalLinkage)));
355 // The kept copy is eligible for auto-hiding (hidden visibility) if all
356 // copies were (i.e. they were all linkonce_odr global unnamed addr).
357 // If any copy is not (e.g. it was originally weak_odr), then the symbol
358 // must remain externally available (e.g. a weak_odr from an explicitly
359 // instantiated template). Additionally, if it is in the
360 // GUIDPreservedSymbols set, that means that it is visibile outside
361 // the summary (e.g. in a native object or a bitcode file without
362 // summary), and in that case we cannot hide it as it isn't possible to
363 // check all copies.
364 S->setCanAutoHide(VI.canAutoHide() &&
365 !GUIDPreservedSymbols.count(VI.getGUID()));
366 }
367 if (C.VisibilityScheme == Config::FromPrevailing)
368 Visibility = S->getVisibility();
369 }
370 // Alias and aliasee can't be turned into available_externally.
371 else if (!isa<AliasSummary>(S.get()) &&
372 !GlobalInvolvedWithAlias.count(S.get()))
374
375 // For ELF, set visibility to the computed visibility from summaries. We
376 // don't track visibility from declarations so this may be more relaxed than
377 // the most constraining one.
378 if (C.VisibilityScheme == Config::ELF)
379 S->setVisibility(Visibility);
380
381 if (S->linkage() != OriginalLinkage)
382 recordNewLinkage(S->modulePath(), VI.getGUID(), S->linkage());
383 }
384
385 if (C.VisibilityScheme == Config::FromPrevailing) {
386 for (auto &S : VI.getSummaryList()) {
387 GlobalValue::LinkageTypes OriginalLinkage = S->linkage();
388 if (GlobalValue::isLocalLinkage(OriginalLinkage) ||
390 continue;
391 S->setVisibility(Visibility);
392 }
393 }
394}
395
396/// Resolve linkage for prevailing symbols in the \p Index.
397//
398// We'd like to drop these functions if they are no longer referenced in the
399// current module. However there is a chance that another module is still
400// referencing them because of the import. We make sure we always emit at least
401// one copy.
405 isPrevailing,
407 recordNewLinkage,
408 const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols) {
409 // We won't optimize the globals that are referenced by an alias for now
410 // Ideally we should turn the alias into a global and duplicate the definition
411 // when needed.
412 DenseSet<GlobalValueSummary *> GlobalInvolvedWithAlias;
413 for (auto &I : Index)
414 for (auto &S : I.second.SummaryList)
415 if (auto AS = dyn_cast<AliasSummary>(S.get()))
416 GlobalInvolvedWithAlias.insert(&AS->getAliasee());
417
418 for (auto &I : Index)
419 thinLTOResolvePrevailingGUID(C, Index.getValueInfo(I),
420 GlobalInvolvedWithAlias, isPrevailing,
421 recordNewLinkage, GUIDPreservedSymbols);
422}
423
425 if (auto *VarSummary = dyn_cast<GlobalVarSummary>(GVS->getBaseObject()))
426 return !VarSummary->maybeReadOnly() && !VarSummary->maybeWriteOnly() &&
427 (VarSummary->linkage() == GlobalValue::WeakODRLinkage ||
428 VarSummary->linkage() == GlobalValue::LinkOnceODRLinkage);
429 return false;
430}
431
433 ValueInfo VI, function_ref<bool(StringRef, ValueInfo)> isExported,
435 isPrevailing) {
436 for (auto &S : VI.getSummaryList()) {
437 if (isExported(S->modulePath(), VI)) {
438 if (GlobalValue::isLocalLinkage(S->linkage()))
439 S->setLinkage(GlobalValue::ExternalLinkage);
440 } else if (EnableLTOInternalization &&
441 // Ignore local and appending linkage values since the linker
442 // doesn't resolve them.
443 !GlobalValue::isLocalLinkage(S->linkage()) &&
444 (!GlobalValue::isInterposableLinkage(S->linkage()) ||
445 isPrevailing(VI.getGUID(), S.get())) &&
446 S->linkage() != GlobalValue::AppendingLinkage &&
447 // We can't internalize available_externally globals because this
448 // can break function pointer equality.
450 // Functions and read-only variables with linkonce_odr and
451 // weak_odr linkage can be internalized. We can't internalize
452 // linkonce_odr and weak_odr variables which are both modified
453 // and read somewhere in the program because reads and writes
454 // will become inconsistent.
455 !isWeakObjectWithRWAccess(S.get()))
456 S->setLinkage(GlobalValue::InternalLinkage);
457 }
458}
459
460// Update the linkages in the given \p Index to mark exported values
461// as external and non-exported values as internal.
464 function_ref<bool(StringRef, ValueInfo)> isExported,
466 isPrevailing) {
467 for (auto &I : Index)
468 thinLTOInternalizeAndPromoteGUID(Index.getValueInfo(I), isExported,
469 isPrevailing);
470}
471
472// Requires a destructor for std::vector<InputModule>.
473InputFile::~InputFile() = default;
474
476 std::unique_ptr<InputFile> File(new InputFile);
477
479 if (!FOrErr)
480 return FOrErr.takeError();
481
482 File->TargetTriple = FOrErr->TheReader.getTargetTriple();
483 File->SourceFileName = FOrErr->TheReader.getSourceFileName();
484 File->COFFLinkerOpts = FOrErr->TheReader.getCOFFLinkerOpts();
485 File->DependentLibraries = FOrErr->TheReader.getDependentLibraries();
486 File->ComdatTable = FOrErr->TheReader.getComdatTable();
487
488 for (unsigned I = 0; I != FOrErr->Mods.size(); ++I) {
489 size_t Begin = File->Symbols.size();
490 for (const irsymtab::Reader::SymbolRef &Sym :
491 FOrErr->TheReader.module_symbols(I))
492 // Skip symbols that are irrelevant to LTO. Note that this condition needs
493 // to match the one in Skip() in LTO::addRegularLTO().
494 if (Sym.isGlobal() && !Sym.isFormatSpecific())
495 File->Symbols.push_back(Sym);
496 File->ModuleSymIndices.push_back({Begin, File->Symbols.size()});
497 }
498
499 File->Mods = FOrErr->Mods;
500 File->Strtab = std::move(FOrErr->Strtab);
501 return std::move(File);
502}
503
505 return Mods[0].getModuleIdentifier();
506}
507
509 assert(Mods.size() == 1 && "Expect only one bitcode module");
510 return Mods[0];
511}
512
513LTO::RegularLTOState::RegularLTOState(unsigned ParallelCodeGenParallelismLevel,
514 const Config &Conf)
515 : ParallelCodeGenParallelismLevel(ParallelCodeGenParallelismLevel),
516 Ctx(Conf), CombinedModule(std::make_unique<Module>("ld-temp.o", Ctx)),
517 Mover(std::make_unique<IRMover>(*CombinedModule)) {}
518
519LTO::ThinLTOState::ThinLTOState(ThinBackend Backend)
520 : Backend(Backend), CombinedIndex(/*HaveGVs*/ false) {
521 if (!Backend)
522 this->Backend =
524}
525
527 unsigned ParallelCodeGenParallelismLevel)
528 : Conf(std::move(Conf)),
529 RegularLTO(ParallelCodeGenParallelismLevel, this->Conf),
530 ThinLTO(std::move(Backend)) {}
531
532// Requires a destructor for MapVector<BitcodeModule>.
533LTO::~LTO() = default;
534
535// Add the symbols in the given module to the GlobalResolutions map, and resolve
536// their partitions.
537void LTO::addModuleToGlobalRes(ArrayRef<InputFile::Symbol> Syms,
539 unsigned Partition, bool InSummary) {
540 auto *ResI = Res.begin();
541 auto *ResE = Res.end();
542 (void)ResE;
543 const Triple TT(RegularLTO.CombinedModule->getTargetTriple());
544 for (const InputFile::Symbol &Sym : Syms) {
545 assert(ResI != ResE);
546 SymbolResolution Res = *ResI++;
547
548 StringRef Name = Sym.getName();
549 // Strip the __imp_ prefix from COFF dllimport symbols (similar to the
550 // way they are handled by lld), otherwise we can end up with two
551 // global resolutions (one with and one for a copy of the symbol without).
552 if (TT.isOSBinFormatCOFF() && Name.startswith("__imp_"))
553 Name = Name.substr(strlen("__imp_"));
554 auto &GlobalRes = GlobalResolutions[Name];
555 GlobalRes.UnnamedAddr &= Sym.isUnnamedAddr();
556 if (Res.Prevailing) {
557 assert(!GlobalRes.Prevailing &&
558 "Multiple prevailing defs are not allowed");
559 GlobalRes.Prevailing = true;
560 GlobalRes.IRName = std::string(Sym.getIRName());
561 } else if (!GlobalRes.Prevailing && GlobalRes.IRName.empty()) {
562 // Sometimes it can be two copies of symbol in a module and prevailing
563 // symbol can have no IR name. That might happen if symbol is defined in
564 // module level inline asm block. In case we have multiple modules with
565 // the same symbol we want to use IR name of the prevailing symbol.
566 // Otherwise, if we haven't seen a prevailing symbol, set the name so that
567 // we can later use it to check if there is any prevailing copy in IR.
568 GlobalRes.IRName = std::string(Sym.getIRName());
569 }
570
571 // In rare occasion, the symbol used to initialize GlobalRes has a different
572 // IRName from the inspected Symbol. This can happen on macOS + iOS, when a
573 // symbol is referenced through its mangled name, say @"\01_symbol" while
574 // the IRName is @symbol (the prefix underscore comes from MachO mangling).
575 // In that case, we have the same actual Symbol that can get two different
576 // GUID, leading to some invalid internalization. Workaround this by marking
577 // the GlobalRes external.
578
579 // FIXME: instead of this check, it would be desirable to compute GUIDs
580 // based on mangled name, but this requires an access to the Target Triple
581 // and would be relatively invasive on the codebase.
582 if (GlobalRes.IRName != Sym.getIRName()) {
583 GlobalRes.Partition = GlobalResolution::External;
584 GlobalRes.VisibleOutsideSummary = true;
585 }
586
587 // Set the partition to external if we know it is re-defined by the linker
588 // with -defsym or -wrap options, used elsewhere, e.g. it is visible to a
589 // regular object, is referenced from llvm.compiler.used/llvm.used, or was
590 // already recorded as being referenced from a different partition.
591 if (Res.LinkerRedefined || Res.VisibleToRegularObj || Sym.isUsed() ||
592 (GlobalRes.Partition != GlobalResolution::Unknown &&
593 GlobalRes.Partition != Partition)) {
594 GlobalRes.Partition = GlobalResolution::External;
595 } else
596 // First recorded reference, save the current partition.
597 GlobalRes.Partition = Partition;
598
599 // Flag as visible outside of summary if visible from a regular object or
600 // from a module that does not have a summary.
601 GlobalRes.VisibleOutsideSummary |=
602 (Res.VisibleToRegularObj || Sym.isUsed() || !InSummary);
603
604 GlobalRes.ExportDynamic |= Res.ExportDynamic;
605 }
606}
607
610 StringRef Path = Input->getName();
611 OS << Path << '\n';
612 auto ResI = Res.begin();
613 for (const InputFile::Symbol &Sym : Input->symbols()) {
614 assert(ResI != Res.end());
615 SymbolResolution Res = *ResI++;
616
617 OS << "-r=" << Path << ',' << Sym.getName() << ',';
618 if (Res.Prevailing)
619 OS << 'p';
621 OS << 'l';
622 if (Res.VisibleToRegularObj)
623 OS << 'x';
624 if (Res.LinkerRedefined)
625 OS << 'r';
626 OS << '\n';
627 }
628 OS.flush();
629 assert(ResI == Res.end());
630}
631
632Error LTO::add(std::unique_ptr<InputFile> Input,
634 assert(!CalledGetMaxTasks);
635
636 if (Conf.ResolutionFile)
637 writeToResolutionFile(*Conf.ResolutionFile, Input.get(), Res);
638
639 if (RegularLTO.CombinedModule->getTargetTriple().empty()) {
640 RegularLTO.CombinedModule->setTargetTriple(Input->getTargetTriple());
641 if (Triple(Input->getTargetTriple()).isOSBinFormatELF())
643 }
644
645 const SymbolResolution *ResI = Res.begin();
646 for (unsigned I = 0; I != Input->Mods.size(); ++I)
647 if (Error Err = addModule(*Input, I, ResI, Res.end()))
648 return Err;
649
650 assert(ResI == Res.end());
651 return Error::success();
652}
653
654Error LTO::addModule(InputFile &Input, unsigned ModI,
655 const SymbolResolution *&ResI,
656 const SymbolResolution *ResE) {
657 Expected<BitcodeLTOInfo> LTOInfo = Input.Mods[ModI].getLTOInfo();
658 if (!LTOInfo)
659 return LTOInfo.takeError();
660
661 if (EnableSplitLTOUnit) {
662 // If only some modules were split, flag this in the index so that
663 // we can skip or error on optimizations that need consistently split
664 // modules (whole program devirt and lower type tests).
665 if (*EnableSplitLTOUnit != LTOInfo->EnableSplitLTOUnit)
666 ThinLTO.CombinedIndex.setPartiallySplitLTOUnits();
667 } else
668 EnableSplitLTOUnit = LTOInfo->EnableSplitLTOUnit;
669
670 BitcodeModule BM = Input.Mods[ModI];
671 auto ModSyms = Input.module_symbols(ModI);
672 addModuleToGlobalRes(ModSyms, {ResI, ResE},
673 LTOInfo->IsThinLTO ? ThinLTO.ModuleMap.size() + 1 : 0,
674 LTOInfo->HasSummary);
675
676 if (LTOInfo->IsThinLTO)
677 return addThinLTO(BM, ModSyms, ResI, ResE);
678
679 RegularLTO.EmptyCombinedModule = false;
681 addRegularLTO(BM, ModSyms, ResI, ResE);
682 if (!ModOrErr)
683 return ModOrErr.takeError();
684
685 if (!LTOInfo->HasSummary)
686 return linkRegularLTO(std::move(*ModOrErr), /*LivenessFromIndex=*/false);
687
688 // Regular LTO module summaries are added to a dummy module that represents
689 // the combined regular LTO module.
690 if (Error Err = BM.readSummary(ThinLTO.CombinedIndex, "", -1ull))
691 return Err;
692 RegularLTO.ModsWithSummaries.push_back(std::move(*ModOrErr));
693 return Error::success();
694}
695
696// Checks whether the given global value is in a non-prevailing comdat
697// (comdat containing values the linker indicated were not prevailing,
698// which we then dropped to available_externally), and if so, removes
699// it from the comdat. This is called for all global values to ensure the
700// comdat is empty rather than leaving an incomplete comdat. It is needed for
701// regular LTO modules, in case we are in a mixed-LTO mode (both regular
702// and thin LTO modules) compilation. Since the regular LTO module will be
703// linked first in the final native link, we want to make sure the linker
704// doesn't select any of these incomplete comdats that would be left
705// in the regular LTO module without this cleanup.
706static void
708 std::set<const Comdat *> &NonPrevailingComdats) {
709 Comdat *C = GV.getComdat();
710 if (!C)
711 return;
712
713 if (!NonPrevailingComdats.count(C))
714 return;
715
716 // Additionally need to drop all global values from the comdat to
717 // available_externally, to satisfy the COMDAT requirement that all members
718 // are discarded as a unit. The non-local linkage global values avoid
719 // duplicate definition linker errors.
721
722 if (auto GO = dyn_cast<GlobalObject>(&GV))
723 GO->setComdat(nullptr);
724}
725
726// Add a regular LTO object to the link.
727// The resulting module needs to be linked into the combined LTO module with
728// linkRegularLTO.
730LTO::addRegularLTO(BitcodeModule BM, ArrayRef<InputFile::Symbol> Syms,
731 const SymbolResolution *&ResI,
732 const SymbolResolution *ResE) {
733 RegularLTOState::AddedModule Mod;
735 BM.getLazyModule(RegularLTO.Ctx, /*ShouldLazyLoadMetadata*/ true,
736 /*IsImporting*/ false);
737 if (!MOrErr)
738 return MOrErr.takeError();
739 Module &M = **MOrErr;
740 Mod.M = std::move(*MOrErr);
741
742 if (Error Err = M.materializeMetadata())
743 return std::move(Err);
745
746 ModuleSymbolTable SymTab;
747 SymTab.addModule(&M);
748
749 for (GlobalVariable &GV : M.globals())
750 if (GV.hasAppendingLinkage())
751 Mod.Keep.push_back(&GV);
752
753 DenseSet<GlobalObject *> AliasedGlobals;
754 for (auto &GA : M.aliases())
755 if (GlobalObject *GO = GA.getAliaseeObject())
756 AliasedGlobals.insert(GO);
757
758 // In this function we need IR GlobalValues matching the symbols in Syms
759 // (which is not backed by a module), so we need to enumerate them in the same
760 // order. The symbol enumeration order of a ModuleSymbolTable intentionally
761 // matches the order of an irsymtab, but when we read the irsymtab in
762 // InputFile::create we omit some symbols that are irrelevant to LTO. The
763 // Skip() function skips the same symbols from the module as InputFile does
764 // from the symbol table.
765 auto MsymI = SymTab.symbols().begin(), MsymE = SymTab.symbols().end();
766 auto Skip = [&]() {
767 while (MsymI != MsymE) {
768 auto Flags = SymTab.getSymbolFlags(*MsymI);
769 if ((Flags & object::BasicSymbolRef::SF_Global) &&
771 return;
772 ++MsymI;
773 }
774 };
775 Skip();
776
777 std::set<const Comdat *> NonPrevailingComdats;
778 SmallSet<StringRef, 2> NonPrevailingAsmSymbols;
779 for (const InputFile::Symbol &Sym : Syms) {
780 assert(ResI != ResE);
781 SymbolResolution Res = *ResI++;
782
783 assert(MsymI != MsymE);
784 ModuleSymbolTable::Symbol Msym = *MsymI++;
785 Skip();
786
787 if (GlobalValue *GV = Msym.dyn_cast<GlobalValue *>()) {
788 if (Res.Prevailing) {
789 if (Sym.isUndefined())
790 continue;
791 Mod.Keep.push_back(GV);
792 // For symbols re-defined with linker -wrap and -defsym options,
793 // set the linkage to weak to inhibit IPO. The linkage will be
794 // restored by the linker.
795 if (Res.LinkerRedefined)
796 GV->setLinkage(GlobalValue::WeakAnyLinkage);
797
798 GlobalValue::LinkageTypes OriginalLinkage = GV->getLinkage();
799 if (GlobalValue::isLinkOnceLinkage(OriginalLinkage))
800 GV->setLinkage(GlobalValue::getWeakLinkage(
801 GlobalValue::isLinkOnceODRLinkage(OriginalLinkage)));
802 } else if (isa<GlobalObject>(GV) &&
803 (GV->hasLinkOnceODRLinkage() || GV->hasWeakODRLinkage() ||
804 GV->hasAvailableExternallyLinkage()) &&
805 !AliasedGlobals.count(cast<GlobalObject>(GV))) {
806 // Any of the above three types of linkage indicates that the
807 // chosen prevailing symbol will have the same semantics as this copy of
808 // the symbol, so we may be able to link it with available_externally
809 // linkage. We will decide later whether to do that when we link this
810 // module (in linkRegularLTO), based on whether it is undefined.
811 Mod.Keep.push_back(GV);
813 if (GV->hasComdat())
814 NonPrevailingComdats.insert(GV->getComdat());
815 cast<GlobalObject>(GV)->setComdat(nullptr);
816 }
817
818 // Set the 'local' flag based on the linker resolution for this symbol.
820 GV->setDSOLocal(true);
821 if (GV->hasDLLImportStorageClass())
822 GV->setDLLStorageClass(GlobalValue::DLLStorageClassTypes::
823 DefaultStorageClass);
824 }
825 } else if (auto *AS = Msym.dyn_cast<ModuleSymbolTable::AsmSymbol *>()) {
826 // Collect non-prevailing symbols.
827 if (!Res.Prevailing)
828 NonPrevailingAsmSymbols.insert(AS->first);
829 } else {
830 llvm_unreachable("unknown symbol type");
831 }
832
833 // Common resolution: collect the maximum size/alignment over all commons.
834 // We also record if we see an instance of a common as prevailing, so that
835 // if none is prevailing we can ignore it later.
836 if (Sym.isCommon()) {
837 // FIXME: We should figure out what to do about commons defined by asm.
838 // For now they aren't reported correctly by ModuleSymbolTable.
839 auto &CommonRes = RegularLTO.Commons[std::string(Sym.getIRName())];
840 CommonRes.Size = std::max(CommonRes.Size, Sym.getCommonSize());
841 if (uint32_t SymAlignValue = Sym.getCommonAlignment()) {
842 CommonRes.Alignment =
843 std::max(Align(SymAlignValue), CommonRes.Alignment);
844 }
845 CommonRes.Prevailing |= Res.Prevailing;
846 }
847 }
848
849 if (!M.getComdatSymbolTable().empty())
850 for (GlobalValue &GV : M.global_values())
851 handleNonPrevailingComdat(GV, NonPrevailingComdats);
852
853 // Prepend ".lto_discard <sym>, <sym>*" directive to each module inline asm
854 // block.
855 if (!M.getModuleInlineAsm().empty()) {
856 std::string NewIA = ".lto_discard";
857 if (!NonPrevailingAsmSymbols.empty()) {
858 // Don't dicard a symbol if there is a live .symver for it.
860 M, [&](StringRef Name, StringRef Alias) {
861 if (!NonPrevailingAsmSymbols.count(Alias))
862 NonPrevailingAsmSymbols.erase(Name);
863 });
864 NewIA += " " + llvm::join(NonPrevailingAsmSymbols, ", ");
865 }
866 NewIA += "\n";
867 M.setModuleInlineAsm(NewIA + M.getModuleInlineAsm());
868 }
869
870 assert(MsymI == MsymE);
871 return std::move(Mod);
872}
873
874Error LTO::linkRegularLTO(RegularLTOState::AddedModule Mod,
875 bool LivenessFromIndex) {
876 std::vector<GlobalValue *> Keep;
877 for (GlobalValue *GV : Mod.Keep) {
878 if (LivenessFromIndex && !ThinLTO.CombinedIndex.isGUIDLive(GV->getGUID())) {
879 if (Function *F = dyn_cast<Function>(GV)) {
880 if (DiagnosticOutputFile) {
881 if (Error Err = F->materialize())
882 return Err;
883 OptimizationRemarkEmitter ORE(F, nullptr);
884 ORE.emit(OptimizationRemark(DEBUG_TYPE, "deadfunction", F)
885 << ore::NV("Function", F)
886 << " not added to the combined module ");
887 }
888 }
889 continue;
890 }
891
893 Keep.push_back(GV);
894 continue;
895 }
896
897 // Only link available_externally definitions if we don't already have a
898 // definition.
899 GlobalValue *CombinedGV =
900 RegularLTO.CombinedModule->getNamedValue(GV->getName());
901 if (CombinedGV && !CombinedGV->isDeclaration())
902 continue;
903
904 Keep.push_back(GV);
905 }
906
907 return RegularLTO.Mover->move(std::move(Mod.M), Keep, nullptr,
908 /* IsPerformingImport */ false);
909}
910
911// Add a ThinLTO module to the link.
912Error LTO::addThinLTO(BitcodeModule BM, ArrayRef<InputFile::Symbol> Syms,
913 const SymbolResolution *&ResI,
914 const SymbolResolution *ResE) {
915 const SymbolResolution *ResITmp = ResI;
916 for (const InputFile::Symbol &Sym : Syms) {
917 assert(ResITmp != ResE);
918 SymbolResolution Res = *ResITmp++;
919
920 if (!Sym.getIRName().empty()) {
922 Sym.getIRName(), GlobalValue::ExternalLinkage, ""));
923 if (Res.Prevailing)
924 ThinLTO.PrevailingModuleForGUID[GUID] = BM.getModuleIdentifier();
925 }
926 }
927
928 if (Error Err =
929 BM.readSummary(ThinLTO.CombinedIndex, BM.getModuleIdentifier(),
930 ThinLTO.ModuleMap.size(), [&](GlobalValue::GUID GUID) {
931 return ThinLTO.PrevailingModuleForGUID[GUID] ==
932 BM.getModuleIdentifier();
933 }))
934 return Err;
935
936 for (const InputFile::Symbol &Sym : Syms) {
937 assert(ResI != ResE);
938 SymbolResolution Res = *ResI++;
939
940 if (!Sym.getIRName().empty()) {
942 Sym.getIRName(), GlobalValue::ExternalLinkage, ""));
943 if (Res.Prevailing) {
944 assert(ThinLTO.PrevailingModuleForGUID[GUID] ==
946
947 // For linker redefined symbols (via --wrap or --defsym) we want to
948 // switch the linkage to `weak` to prevent IPOs from happening.
949 // Find the summary in the module for this very GV and record the new
950 // linkage so that we can switch it when we import the GV.
951 if (Res.LinkerRedefined)
952 if (auto S = ThinLTO.CombinedIndex.findSummaryInModule(
953 GUID, BM.getModuleIdentifier()))
954 S->setLinkage(GlobalValue::WeakAnyLinkage);
955 }
956
957 // If the linker resolved the symbol to a local definition then mark it
958 // as local in the summary for the module we are adding.
960 if (auto S = ThinLTO.CombinedIndex.findSummaryInModule(
961 GUID, BM.getModuleIdentifier())) {
962 S->setDSOLocal(true);
963 }
964 }
965 }
966 }
967
968 if (!ThinLTO.ModuleMap.insert({BM.getModuleIdentifier(), BM}).second)
969 return make_error<StringError>(
970 "Expected at most one ThinLTO module per bitcode file",
972
973 if (!Conf.ThinLTOModulesToCompile.empty()) {
974 if (!ThinLTO.ModulesToCompile)
975 ThinLTO.ModulesToCompile = ModuleMapType();
976 // This is a fuzzy name matching where only modules with name containing the
977 // specified switch values are going to be compiled.
978 for (const std::string &Name : Conf.ThinLTOModulesToCompile) {
980 ThinLTO.ModulesToCompile->insert({BM.getModuleIdentifier(), BM});
981 llvm::errs() << "[ThinLTO] Selecting " << BM.getModuleIdentifier()
982 << " to compile\n";
983 }
984 }
985 }
986
987 return Error::success();
988}
989
990unsigned LTO::getMaxTasks() const {
991 CalledGetMaxTasks = true;
992 auto ModuleCount = ThinLTO.ModulesToCompile ? ThinLTO.ModulesToCompile->size()
993 : ThinLTO.ModuleMap.size();
994 return RegularLTO.ParallelCodeGenParallelismLevel + ModuleCount;
995}
996
997// If only some of the modules were split, we cannot correctly handle
998// code that contains type tests or type checked loads.
999Error LTO::checkPartiallySplit() {
1000 if (!ThinLTO.CombinedIndex.partiallySplitLTOUnits())
1001 return Error::success();
1002
1003 Function *TypeTestFunc = RegularLTO.CombinedModule->getFunction(
1004 Intrinsic::getName(Intrinsic::type_test));
1005 Function *TypeCheckedLoadFunc = RegularLTO.CombinedModule->getFunction(
1006 Intrinsic::getName(Intrinsic::type_checked_load));
1007
1008 // First check if there are type tests / type checked loads in the
1009 // merged regular LTO module IR.
1010 if ((TypeTestFunc && !TypeTestFunc->use_empty()) ||
1011 (TypeCheckedLoadFunc && !TypeCheckedLoadFunc->use_empty()))
1012 return make_error<StringError>(
1013 "inconsistent LTO Unit splitting (recompile with -fsplit-lto-unit)",
1015
1016 // Otherwise check if there are any recorded in the combined summary from the
1017 // ThinLTO modules.
1018 for (auto &P : ThinLTO.CombinedIndex) {
1019 for (auto &S : P.second.SummaryList) {
1020 auto *FS = dyn_cast<FunctionSummary>(S.get());
1021 if (!FS)
1022 continue;
1023 if (!FS->type_test_assume_vcalls().empty() ||
1024 !FS->type_checked_load_vcalls().empty() ||
1025 !FS->type_test_assume_const_vcalls().empty() ||
1026 !FS->type_checked_load_const_vcalls().empty() ||
1027 !FS->type_tests().empty())
1028 return make_error<StringError>(
1029 "inconsistent LTO Unit splitting (recompile with -fsplit-lto-unit)",
1031 }
1032 }
1033 return Error::success();
1034}
1035
1037 // Compute "dead" symbols, we don't want to import/export these!
1038 DenseSet<GlobalValue::GUID> GUIDPreservedSymbols;
1039 DenseMap<GlobalValue::GUID, PrevailingType> GUIDPrevailingResolutions;
1040 for (auto &Res : GlobalResolutions) {
1041 // Normally resolution have IR name of symbol. We can do nothing here
1042 // otherwise. See comments in GlobalResolution struct for more details.
1043 if (Res.second.IRName.empty())
1044 continue;
1045
1047 GlobalValue::dropLLVMManglingEscape(Res.second.IRName));
1048
1049 if (Res.second.VisibleOutsideSummary && Res.second.Prevailing)
1050 GUIDPreservedSymbols.insert(GUID);
1051
1052 if (Res.second.ExportDynamic)
1053 DynamicExportSymbols.insert(GUID);
1054
1055 GUIDPrevailingResolutions[GUID] =
1057 }
1058
1059 auto isPrevailing = [&](GlobalValue::GUID G) {
1060 auto It = GUIDPrevailingResolutions.find(G);
1061 if (It == GUIDPrevailingResolutions.end())
1063 return It->second;
1064 };
1065 computeDeadSymbolsWithConstProp(ThinLTO.CombinedIndex, GUIDPreservedSymbols,
1066 isPrevailing, Conf.OptLevel > 0);
1067
1068 // Setup output file to emit statistics.
1069 auto StatsFileOrErr = setupStatsFile(Conf.StatsFile);
1070 if (!StatsFileOrErr)
1071 return StatsFileOrErr.takeError();
1072 std::unique_ptr<ToolOutputFile> StatsFile = std::move(StatsFileOrErr.get());
1073
1074 Error Result = runRegularLTO(AddStream);
1075 if (!Result)
1076 Result = runThinLTO(AddStream, Cache, GUIDPreservedSymbols);
1077
1078 if (StatsFile)
1079 PrintStatisticsJSON(StatsFile->os());
1080
1081 return Result;
1082}
1083
1084Error LTO::runRegularLTO(AddStreamFn AddStream) {
1085 // Setup optimization remarks.
1086 auto DiagFileOrErr = lto::setupLLVMOptimizationRemarks(
1087 RegularLTO.CombinedModule->getContext(), Conf.RemarksFilename,
1090 if (!DiagFileOrErr)
1091 return DiagFileOrErr.takeError();
1092 DiagnosticOutputFile = std::move(*DiagFileOrErr);
1093
1094 // Finalize linking of regular LTO modules containing summaries now that
1095 // we have computed liveness information.
1096 for (auto &M : RegularLTO.ModsWithSummaries)
1097 if (Error Err = linkRegularLTO(std::move(M),
1098 /*LivenessFromIndex=*/true))
1099 return Err;
1100
1101 // Ensure we don't have inconsistently split LTO units with type tests.
1102 // FIXME: this checks both LTO and ThinLTO. It happens to work as we take
1103 // this path both cases but eventually this should be split into two and
1104 // do the ThinLTO checks in `runThinLTO`.
1105 if (Error Err = checkPartiallySplit())
1106 return Err;
1107
1108 // Make sure commons have the right size/alignment: we kept the largest from
1109 // all the prevailing when adding the inputs, and we apply it here.
1110 const DataLayout &DL = RegularLTO.CombinedModule->getDataLayout();
1111 for (auto &I : RegularLTO.Commons) {
1112 if (!I.second.Prevailing)
1113 // Don't do anything if no instance of this common was prevailing.
1114 continue;
1115 GlobalVariable *OldGV = RegularLTO.CombinedModule->getNamedGlobal(I.first);
1116 if (OldGV && DL.getTypeAllocSize(OldGV->getValueType()) == I.second.Size) {
1117 // Don't create a new global if the type is already correct, just make
1118 // sure the alignment is correct.
1119 OldGV->setAlignment(I.second.Alignment);
1120 continue;
1121 }
1122 ArrayType *Ty =
1123 ArrayType::get(Type::getInt8Ty(RegularLTO.Ctx), I.second.Size);
1124 auto *GV = new GlobalVariable(*RegularLTO.CombinedModule, Ty, false,
1127 GV->setAlignment(I.second.Alignment);
1128 if (OldGV) {
1130 GV->takeName(OldGV);
1131 OldGV->eraseFromParent();
1132 } else {
1133 GV->setName(I.first);
1134 }
1135 }
1136
1137 // If allowed, upgrade public vcall visibility metadata to linkage unit
1138 // visibility before whole program devirtualization in the optimizer.
1139 updateVCallVisibilityInModule(*RegularLTO.CombinedModule,
1141 DynamicExportSymbols);
1142 updatePublicTypeTestCalls(*RegularLTO.CombinedModule,
1144
1145 if (Conf.PreOptModuleHook &&
1146 !Conf.PreOptModuleHook(0, *RegularLTO.CombinedModule))
1147 return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
1148
1149 if (!Conf.CodeGenOnly) {
1150 for (const auto &R : GlobalResolutions) {
1151 if (!R.second.isPrevailingIRSymbol())
1152 continue;
1153 if (R.second.Partition != 0 &&
1154 R.second.Partition != GlobalResolution::External)
1155 continue;
1156
1157 GlobalValue *GV =
1158 RegularLTO.CombinedModule->getNamedValue(R.second.IRName);
1159 // Ignore symbols defined in other partitions.
1160 // Also skip declarations, which are not allowed to have internal linkage.
1161 if (!GV || GV->hasLocalLinkage() || GV->isDeclaration())
1162 continue;
1163 GV->setUnnamedAddr(R.second.UnnamedAddr ? GlobalValue::UnnamedAddr::Global
1165 if (EnableLTOInternalization && R.second.Partition == 0)
1167 }
1168
1169 RegularLTO.CombinedModule->addModuleFlag(Module::Error, "LTOPostLink", 1);
1170
1171 if (Conf.PostInternalizeModuleHook &&
1172 !Conf.PostInternalizeModuleHook(0, *RegularLTO.CombinedModule))
1173 return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
1174 }
1175
1176 if (!RegularLTO.EmptyCombinedModule || Conf.AlwaysEmitRegularLTOObj) {
1177 if (Error Err =
1178 backend(Conf, AddStream, RegularLTO.ParallelCodeGenParallelismLevel,
1179 *RegularLTO.CombinedModule, ThinLTO.CombinedIndex))
1180 return Err;
1181 }
1182
1183 return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
1184}
1185
1186static const char *libcallRoutineNames[] = {
1187#define HANDLE_LIBCALL(code, name) name,
1188#include "llvm/IR/RuntimeLibcalls.def"
1189#undef HANDLE_LIBCALL
1190};
1191
1194}
1195
1196/// This class defines the interface to the ThinLTO backend.
1198protected:
1199 const Config &Conf;
1204
1205public:
1206 ThinBackendProc(const Config &Conf, ModuleSummaryIndex &CombinedIndex,
1207 const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1208 lto::IndexWriteCallback OnWrite, bool ShouldEmitImportsFiles)
1209 : Conf(Conf), CombinedIndex(CombinedIndex),
1210 ModuleToDefinedGVSummaries(ModuleToDefinedGVSummaries),
1211 OnWrite(OnWrite), ShouldEmitImportsFiles(ShouldEmitImportsFiles) {}
1212
1213 virtual ~ThinBackendProc() = default;
1214 virtual Error start(
1215 unsigned Task, BitcodeModule BM,
1216 const FunctionImporter::ImportMapTy &ImportList,
1217 const FunctionImporter::ExportSetTy &ExportList,
1218 const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
1220 virtual Error wait() = 0;
1221 virtual unsigned getThreadCount() = 0;
1222
1223 // Write sharded indices and (optionally) imports to disk
1225 llvm::StringRef ModulePath,
1226 const std::string &NewModulePath) {
1227 std::map<std::string, GVSummaryMapTy> ModuleToSummariesForIndex;
1228 std::error_code EC;
1229 gatherImportedSummariesForModule(ModulePath, ModuleToDefinedGVSummaries,
1230 ImportList, ModuleToSummariesForIndex);
1231
1232 raw_fd_ostream OS(NewModulePath + ".thinlto.bc", EC,
1233 sys::fs::OpenFlags::OF_None);
1234 if (EC)
1235 return errorCodeToError(EC);
1236 writeIndexToFile(CombinedIndex, OS, &ModuleToSummariesForIndex);
1237
1238 if (ShouldEmitImportsFiles) {
1239 EC = EmitImportsFiles(ModulePath, NewModulePath + ".imports",
1240 ModuleToSummariesForIndex);
1241 if (EC)
1242 return errorCodeToError(EC);
1243 }
1244 return Error::success();
1245 }
1246};
1247
1248namespace {
1249class InProcessThinBackend : public ThinBackendProc {
1250 ThreadPool BackendThreadPool;
1251 AddStreamFn AddStream;
1252 FileCache Cache;
1253 std::set<GlobalValue::GUID> CfiFunctionDefs;
1254 std::set<GlobalValue::GUID> CfiFunctionDecls;
1255
1256 std::optional<Error> Err;
1257 std::mutex ErrMu;
1258
1259 bool ShouldEmitIndexFiles;
1260
1261public:
1262 InProcessThinBackend(
1263 const Config &Conf, ModuleSummaryIndex &CombinedIndex,
1264 ThreadPoolStrategy ThinLTOParallelism,
1265 const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1266 AddStreamFn AddStream, FileCache Cache, lto::IndexWriteCallback OnWrite,
1267 bool ShouldEmitIndexFiles, bool ShouldEmitImportsFiles)
1268 : ThinBackendProc(Conf, CombinedIndex, ModuleToDefinedGVSummaries,
1269 OnWrite, ShouldEmitImportsFiles),
1270 BackendThreadPool(ThinLTOParallelism), AddStream(std::move(AddStream)),
1271 Cache(std::move(Cache)), ShouldEmitIndexFiles(ShouldEmitIndexFiles) {
1272 for (auto &Name : CombinedIndex.cfiFunctionDefs())
1273 CfiFunctionDefs.insert(
1275 for (auto &Name : CombinedIndex.cfiFunctionDecls())
1276 CfiFunctionDecls.insert(
1278 }
1279
1280 Error runThinLTOBackendThread(
1281 AddStreamFn AddStream, FileCache Cache, unsigned Task, BitcodeModule BM,
1282 ModuleSummaryIndex &CombinedIndex,
1283 const FunctionImporter::ImportMapTy &ImportList,
1284 const FunctionImporter::ExportSetTy &ExportList,
1285 const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
1286 const GVSummaryMapTy &DefinedGlobals,
1288 auto RunThinBackend = [&](AddStreamFn AddStream) {
1289 LTOLLVMContext BackendContext(Conf);
1290 Expected<std::unique_ptr<Module>> MOrErr = BM.parseModule(BackendContext);
1291 if (!MOrErr)
1292 return MOrErr.takeError();
1293
1294 return thinBackend(Conf, Task, AddStream, **MOrErr, CombinedIndex,
1295 ImportList, DefinedGlobals, &ModuleMap);
1296 };
1297
1298 auto ModuleID = BM.getModuleIdentifier();
1299
1300 if (ShouldEmitIndexFiles) {
1301 if (auto E = emitFiles(ImportList, ModuleID, ModuleID.str()))
1302 return E;
1303 }
1304
1305 if (!Cache || !CombinedIndex.modulePaths().count(ModuleID) ||
1306 all_of(CombinedIndex.getModuleHash(ModuleID),
1307 [](uint32_t V) { return V == 0; }))
1308 // Cache disabled or no entry for this module in the combined index or
1309 // no module hash.
1310 return RunThinBackend(AddStream);
1311
1313 // The module may be cached, this helps handling it.
1314 computeLTOCacheKey(Key, Conf, CombinedIndex, ModuleID, ImportList,
1315 ExportList, ResolvedODR, DefinedGlobals, CfiFunctionDefs,
1316 CfiFunctionDecls);
1317 Expected<AddStreamFn> CacheAddStreamOrErr = Cache(Task, Key, ModuleID);
1318 if (Error Err = CacheAddStreamOrErr.takeError())
1319 return Err;
1320 AddStreamFn &CacheAddStream = *CacheAddStreamOrErr;
1321 if (CacheAddStream)
1322 return RunThinBackend(CacheAddStream);
1323
1324 return Error::success();
1325 }
1326
1327 Error start(
1328 unsigned Task, BitcodeModule BM,
1329 const FunctionImporter::ImportMapTy &ImportList,
1330 const FunctionImporter::ExportSetTy &ExportList,
1331 const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
1332 MapVector<StringRef, BitcodeModule> &ModuleMap) override {
1333 StringRef ModulePath = BM.getModuleIdentifier();
1334 assert(ModuleToDefinedGVSummaries.count(ModulePath));
1335 const GVSummaryMapTy &DefinedGlobals =
1336 ModuleToDefinedGVSummaries.find(ModulePath)->second;
1337 BackendThreadPool.async(
1338 [=](BitcodeModule BM, ModuleSummaryIndex &CombinedIndex,
1339 const FunctionImporter::ImportMapTy &ImportList,
1340 const FunctionImporter::ExportSetTy &ExportList,
1341 const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>
1342 &ResolvedODR,
1343 const GVSummaryMapTy &DefinedGlobals,
1345 if (LLVM_ENABLE_THREADS && Conf.TimeTraceEnabled)
1347 "thin backend");
1348 Error E = runThinLTOBackendThread(
1349 AddStream, Cache, Task, BM, CombinedIndex, ImportList, ExportList,
1350 ResolvedODR, DefinedGlobals, ModuleMap);
1351 if (E) {
1352 std::unique_lock<std::mutex> L(ErrMu);
1353 if (Err)
1354 Err = joinErrors(std::move(*Err), std::move(E));
1355 else
1356 Err = std::move(E);
1357 }
1358 if (LLVM_ENABLE_THREADS && Conf.TimeTraceEnabled)
1360 },
1361 BM, std::ref(CombinedIndex), std::ref(ImportList), std::ref(ExportList),
1362 std::ref(ResolvedODR), std::ref(DefinedGlobals), std::ref(ModuleMap));
1363
1364 if (OnWrite)
1365 OnWrite(std::string(ModulePath));
1366 return Error::success();
1367 }
1368
1369 Error wait() override {
1370 BackendThreadPool.wait();
1371 if (Err)
1372 return std::move(*Err);
1373 else
1374 return Error::success();
1375 }
1376
1377 unsigned getThreadCount() override {
1378 return BackendThreadPool.getThreadCount();
1379 }
1380};
1381} // end anonymous namespace
1382
1385 bool ShouldEmitIndexFiles,
1386 bool ShouldEmitImportsFiles) {
1387 return [=](const Config &Conf, ModuleSummaryIndex &CombinedIndex,
1388 const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1389 AddStreamFn AddStream, FileCache Cache) {
1390 return std::make_unique<InProcessThinBackend>(
1391 Conf, CombinedIndex, Parallelism, ModuleToDefinedGVSummaries, AddStream,
1392 Cache, OnWrite, ShouldEmitIndexFiles, ShouldEmitImportsFiles);
1393 };
1394}
1395
1396// Given the original \p Path to an output file, replace any path
1397// prefix matching \p OldPrefix with \p NewPrefix. Also, create the
1398// resulting directory if it does not yet exist.
1399std::string lto::getThinLTOOutputFile(const std::string &Path,
1400 const std::string &OldPrefix,
1401 const std::string &NewPrefix) {
1402 if (OldPrefix.empty() && NewPrefix.empty())
1403 return Path;
1404 SmallString<128> NewPath(Path);
1405 llvm::sys::path::replace_path_prefix(NewPath, OldPrefix, NewPrefix);
1406 StringRef ParentPath = llvm::sys::path::parent_path(NewPath.str());
1407 if (!ParentPath.empty()) {
1408 // Make sure the new directory exists, creating it if necessary.
1409 if (std::error_code EC = llvm::sys::fs::create_directories(ParentPath))
1410 llvm::errs() << "warning: could not create directory '" << ParentPath
1411 << "': " << EC.message() << '\n';
1412 }
1413 return std::string(NewPath.str());
1414}
1415
1416namespace {
1417class WriteIndexesThinBackend : public ThinBackendProc {
1418 std::string OldPrefix, NewPrefix;
1419 raw_fd_ostream *LinkedObjectsFile;
1420
1421public:
1422 WriteIndexesThinBackend(
1423 const Config &Conf, ModuleSummaryIndex &CombinedIndex,
1424 const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1425 std::string OldPrefix, std::string NewPrefix, bool ShouldEmitImportsFiles,
1426 raw_fd_ostream *LinkedObjectsFile, lto::IndexWriteCallback OnWrite)
1427 : ThinBackendProc(Conf, CombinedIndex, ModuleToDefinedGVSummaries,
1428 OnWrite, ShouldEmitImportsFiles),
1429 OldPrefix(OldPrefix), NewPrefix(NewPrefix),
1430 LinkedObjectsFile(LinkedObjectsFile) {}
1431
1432 Error start(
1433 unsigned Task, BitcodeModule BM,
1434 const FunctionImporter::ImportMapTy &ImportList,
1435 const FunctionImporter::ExportSetTy &ExportList,
1436 const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
1437 MapVector<StringRef, BitcodeModule> &ModuleMap) override {
1438 StringRef ModulePath = BM.getModuleIdentifier();
1439 std::string NewModulePath =
1440 getThinLTOOutputFile(std::string(ModulePath), OldPrefix, NewPrefix);
1441
1442 if (LinkedObjectsFile)
1443 *LinkedObjectsFile << NewModulePath << '\n';
1444
1445 if (auto E = emitFiles(ImportList, ModulePath, NewModulePath))
1446 return E;
1447
1448 if (OnWrite)
1449 OnWrite(std::string(ModulePath));
1450 return Error::success();
1451 }
1452
1453 Error wait() override { return Error::success(); }
1454
1455 // WriteIndexesThinBackend should always return 1 to prevent module
1456 // re-ordering and avoid non-determinism in the final link.
1457 unsigned getThreadCount() override { return 1; }
1458};
1459} // end anonymous namespace
1460
1462 std::string OldPrefix, std::string NewPrefix, bool ShouldEmitImportsFiles,
1463 raw_fd_ostream *LinkedObjectsFile, IndexWriteCallback OnWrite) {
1464 return [=](const Config &Conf, ModuleSummaryIndex &CombinedIndex,
1465 const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1466 AddStreamFn AddStream, FileCache Cache) {
1467 return std::make_unique<WriteIndexesThinBackend>(
1468 Conf, CombinedIndex, ModuleToDefinedGVSummaries, OldPrefix, NewPrefix,
1469 ShouldEmitImportsFiles, LinkedObjectsFile, OnWrite);
1470 };
1471}
1472
1473Error LTO::runThinLTO(AddStreamFn AddStream, FileCache Cache,
1474 const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols) {
1475 ThinLTO.CombinedIndex.releaseTemporaryMemory();
1476 timeTraceProfilerBegin("ThinLink", StringRef(""));
1477 auto TimeTraceScopeExit = llvm::make_scope_exit([]() {
1480 });
1481 if (ThinLTO.ModuleMap.empty())
1482 return Error::success();
1483
1484 if (ThinLTO.ModulesToCompile && ThinLTO.ModulesToCompile->empty()) {
1485 llvm::errs() << "warning: [ThinLTO] No module compiled\n";
1486 return Error::success();
1487 }
1488
1489 if (Conf.CombinedIndexHook &&
1490 !Conf.CombinedIndexHook(ThinLTO.CombinedIndex, GUIDPreservedSymbols))
1491 return Error::success();
1492
1493 // Collect for each module the list of function it defines (GUID ->
1494 // Summary).
1496 ModuleToDefinedGVSummaries(ThinLTO.ModuleMap.size());
1497 ThinLTO.CombinedIndex.collectDefinedGVSummariesPerModule(
1498 ModuleToDefinedGVSummaries);
1499 // Create entries for any modules that didn't have any GV summaries
1500 // (either they didn't have any GVs to start with, or we suppressed
1501 // generation of the summaries because they e.g. had inline assembly
1502 // uses that couldn't be promoted/renamed on export). This is so
1503 // InProcessThinBackend::start can still launch a backend thread, which
1504 // is passed the map of summaries for the module, without any special
1505 // handling for this case.
1506 for (auto &Mod : ThinLTO.ModuleMap)
1507 if (!ModuleToDefinedGVSummaries.count(Mod.first))
1508 ModuleToDefinedGVSummaries.try_emplace(Mod.first);
1509
1510 // Synthesize entry counts for functions in the CombinedIndex.
1511 computeSyntheticCounts(ThinLTO.CombinedIndex);
1512
1514 ThinLTO.ModuleMap.size());
1516 ThinLTO.ModuleMap.size());
1518
1519 if (DumpThinCGSCCs)
1520 ThinLTO.CombinedIndex.dumpSCCs(outs());
1521
1522 std::set<GlobalValue::GUID> ExportedGUIDs;
1523
1525 ThinLTO.CombinedIndex.setWithWholeProgramVisibility();
1526 // If allowed, upgrade public vcall visibility to linkage unit visibility in
1527 // the summaries before whole program devirtualization below.
1528 updateVCallVisibilityInIndex(ThinLTO.CombinedIndex,
1530 DynamicExportSymbols);
1531
1532 // Perform index-based WPD. This will return immediately if there are
1533 // no index entries in the typeIdMetadata map (e.g. if we are instead
1534 // performing IR-based WPD in hybrid regular/thin LTO mode).
1535 std::map<ValueInfo, std::vector<VTableSlotSummary>> LocalWPDTargetsMap;
1536 runWholeProgramDevirtOnIndex(ThinLTO.CombinedIndex, ExportedGUIDs,
1537 LocalWPDTargetsMap);
1538
1539 if (Conf.OptLevel > 0)
1540 ComputeCrossModuleImport(ThinLTO.CombinedIndex, ModuleToDefinedGVSummaries,
1541 ImportLists, ExportLists);
1542
1543 // Figure out which symbols need to be internalized. This also needs to happen
1544 // at -O0 because summary-based DCE is implemented using internalization, and
1545 // we must apply DCE consistently with the full LTO module in order to avoid
1546 // undefined references during the final link.
1547 for (auto &Res : GlobalResolutions) {
1548 // If the symbol does not have external references or it is not prevailing,
1549 // then not need to mark it as exported from a ThinLTO partition.
1550 if (Res.second.Partition != GlobalResolution::External ||
1551 !Res.second.isPrevailingIRSymbol())
1552 continue;
1553 auto GUID = GlobalValue::getGUID(
1554 GlobalValue::dropLLVMManglingEscape(Res.second.IRName));
1555 // Mark exported unless index-based analysis determined it to be dead.
1556 if (ThinLTO.CombinedIndex.isGUIDLive(GUID))
1557 ExportedGUIDs.insert(GUID);
1558 }
1559
1560 // Any functions referenced by the jump table in the regular LTO object must
1561 // be exported.
1562 for (auto &Def : ThinLTO.CombinedIndex.cfiFunctionDefs())
1563 ExportedGUIDs.insert(
1565 for (auto &Decl : ThinLTO.CombinedIndex.cfiFunctionDecls())
1566 ExportedGUIDs.insert(
1568
1569 auto isExported = [&](StringRef ModuleIdentifier, ValueInfo VI) {
1570 const auto &ExportList = ExportLists.find(ModuleIdentifier);
1571 return (ExportList != ExportLists.end() && ExportList->second.count(VI)) ||
1572 ExportedGUIDs.count(VI.getGUID());
1573 };
1574
1575 // Update local devirtualized targets that were exported by cross-module
1576 // importing or by other devirtualizations marked in the ExportedGUIDs set.
1577 updateIndexWPDForExports(ThinLTO.CombinedIndex, isExported,
1578 LocalWPDTargetsMap);
1579
1580 auto isPrevailing = [&](GlobalValue::GUID GUID,
1581 const GlobalValueSummary *S) {
1582 return ThinLTO.PrevailingModuleForGUID[GUID] == S->modulePath();
1583 };
1584 thinLTOInternalizeAndPromoteInIndex(ThinLTO.CombinedIndex, isExported,
1585 isPrevailing);
1586
1587 auto recordNewLinkage = [&](StringRef ModuleIdentifier,
1588 GlobalValue::GUID GUID,
1589 GlobalValue::LinkageTypes NewLinkage) {
1590 ResolvedODR[ModuleIdentifier][GUID] = NewLinkage;
1591 };
1592 thinLTOResolvePrevailingInIndex(Conf, ThinLTO.CombinedIndex, isPrevailing,
1593 recordNewLinkage, GUIDPreservedSymbols);
1594
1595 thinLTOPropagateFunctionAttrs(ThinLTO.CombinedIndex, isPrevailing);
1596
1597 generateParamAccessSummary(ThinLTO.CombinedIndex);
1598
1601
1602 TimeTraceScopeExit.release();
1603
1604 std::unique_ptr<ThinBackendProc> BackendProc =
1605 ThinLTO.Backend(Conf, ThinLTO.CombinedIndex, ModuleToDefinedGVSummaries,
1606 AddStream, Cache);
1607
1608 auto &ModuleMap =
1609 ThinLTO.ModulesToCompile ? *ThinLTO.ModulesToCompile : ThinLTO.ModuleMap;
1610
1611 auto ProcessOneModule = [&](int I) -> Error {
1612 auto &Mod = *(ModuleMap.begin() + I);
1613 // Tasks 0 through ParallelCodeGenParallelismLevel-1 are reserved for
1614 // combined module and parallel code generation partitions.
1615 return BackendProc->start(RegularLTO.ParallelCodeGenParallelismLevel + I,
1616 Mod.second, ImportLists[Mod.first],
1617 ExportLists[Mod.first], ResolvedODR[Mod.first],
1618 ThinLTO.ModuleMap);
1619 };
1620
1621 if (BackendProc->getThreadCount() == 1) {
1622 // Process the modules in the order they were provided on the command-line.
1623 // It is important for this codepath to be used for WriteIndexesThinBackend,
1624 // to ensure the emitted LinkedObjectsFile lists ThinLTO objects in the same
1625 // order as the inputs, which otherwise would affect the final link order.
1626 for (int I = 0, E = ModuleMap.size(); I != E; ++I)
1627 if (Error E = ProcessOneModule(I))
1628 return E;
1629 } else {
1630 // When executing in parallel, process largest bitsize modules first to
1631 // improve parallelism, and avoid starving the thread pool near the end.
1632 // This saves about 15 sec on a 36-core machine while link `clang.exe` (out
1633 // of 100 sec).
1634 std::vector<BitcodeModule *> ModulesVec;
1635 ModulesVec.reserve(ModuleMap.size());
1636 for (auto &Mod : ModuleMap)
1637 ModulesVec.push_back(&Mod.second);
1638 for (int I : generateModulesOrdering(ModulesVec))
1639 if (Error E = ProcessOneModule(I))
1640 return E;
1641 }
1642 return BackendProc->wait();
1643}
1644
1648 std::optional<uint64_t> RemarksHotnessThreshold, int Count) {
1649 std::string Filename = std::string(RemarksFilename);
1650 // For ThinLTO, file.opt.<format> becomes
1651 // file.opt.<format>.thin.<num>.<format>.
1652 if (!Filename.empty() && Count != -1)
1653 Filename =
1654 (Twine(Filename) + ".thin." + llvm::utostr(Count) + "." + RemarksFormat)
1655 .str();
1656
1657 auto ResultOrErr = llvm::setupLLVMOptimizationRemarks(
1660 if (Error E = ResultOrErr.takeError())
1661 return std::move(E);
1662
1663 if (*ResultOrErr)
1664 (*ResultOrErr)->keep();
1665
1666 return ResultOrErr;
1667}
1668
1671 // Setup output file to emit statistics.
1672 if (StatsFilename.empty())
1673 return nullptr;
1674
1676 std::error_code EC;
1677 auto StatsFile =
1678 std::make_unique<ToolOutputFile>(StatsFilename, EC, sys::fs::OF_None);
1679 if (EC)
1680 return errorCodeToError(EC);
1681
1682 StatsFile->keep();
1683 return std::move(StatsFile);
1684}
1685
1686// Compute the ordering we will process the inputs: the rough heuristic here
1687// is to sort them per size so that the largest module get schedule as soon as
1688// possible. This is purely a compile-time optimization.
1690 auto Seq = llvm::seq<int>(0, R.size());
1691 std::vector<int> ModulesOrdering(Seq.begin(), Seq.end());
1692 llvm::sort(ModulesOrdering, [&](int LeftIndex, int RightIndex) {
1693 auto LSize = R[LeftIndex]->getBuffer().size();
1694 auto RSize = R[RightIndex]->getBuffer().size();
1695 return LSize > RSize;
1696 });
1697 return ModulesOrdering;
1698}
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
std::string Name
#define DEBUG_TYPE
static void writeToResolutionFile(raw_ostream &OS, InputFile *Input, ArrayRef< SymbolResolution > Res)
Definition: LTO.cpp:608
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:325
static void handleNonPrevailingComdat(GlobalValue &GV, std::set< const Comdat * > &NonPrevailingComdats)
Definition: LTO.cpp:707
static bool isWeakObjectWithRWAccess(GlobalValueSummary *GVS)
Definition: LTO.cpp:424
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:432
static const char * libcallRoutineNames[]
Definition: LTO.cpp:1186
#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.
LLVMContext & Context
return ToRemove size() > 0
#define P(N)
@ VI
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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:152
iterator begin() const
Definition: ArrayRef.h:151
Class to represent array types.
Definition: DerivedTypes.h:357
static ArrayType * get(Type *ElementType, uint64_t NumElements)
This static method is the primary way to construct an ArrayType.
Definition: Type.cpp:652
Represents a module in a bitcode file.
StringRef getModuleIdentifier() const
Expected< std::unique_ptr< Module > > parseModule(LLVMContext &Context, ParserCallbacks Callbacks={})
Read the entire bitcode module and return it.
Error readSummary(ModuleSummaryIndex &CombinedIndex, StringRef ModulePath, uint64_t ModuleId, 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 > > 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:1595
static Constant * getBitCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:2229
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:114
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:150
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:156
static ErrorSuccess success()
Create a success value.
Definition: Error.h:330
Tagged union holding either a T or a Error.
Definition: Error.h:470
Error takeError()
Take ownership of the stored error.
Definition: Error.h:597
void setAlignment(Align Align)
Sets the alignment attribute of the GlobalObject.
Definition: Globals.cpp:130
Function and variable summary information to aid decisions and implementation of importing.
GlobalValueSummary * getBaseObject()
If this is an alias summary, returns the summary of the aliased object (a global variable or function...
static bool isAppendingLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:395
static bool isLocalLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:404
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition: Globals.cpp:275
void setUnnamedAddr(UnnamedAddr Val)
Definition: GlobalValue.h:227
bool hasLocalLinkage() const
Definition: GlobalValue.h:523
static StringRef dropLLVMManglingEscape(StringRef Name)
If the given string begins with the GlobalValue name mangling escape character '\1',...
Definition: GlobalValue.h:562
const Comdat * getComdat() const
Definition: Globals.cpp:185
static bool isLinkOnceLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:383
void setLinkage(LinkageTypes LT)
Definition: GlobalValue.h:532
DLLStorageClassTypes
Storage classes of global values for PE targets.
Definition: GlobalValue.h:69
GUID getGUID() const
Return a 64-bit global unique ID constructed from global value name (i.e.
Definition: GlobalValue.h:591
const GlobalObject * getAliaseeObject() const
Definition: Globals.cpp:369
PointerType * getType() const
Global values are always pointers.
Definition: GlobalValue.h:290
VisibilityTypes
An enumeration for the kinds of visibility of global values.
Definition: GlobalValue.h:62
@ DefaultVisibility
The GV is visible.
Definition: GlobalValue.h:63
static GUID getGUID(StringRef GlobalName)
Return a 64-bit global unique ID constructed from global value name (i.e.
Definition: GlobalValue.h:587
static bool isInterposableLinkage(LinkageTypes Linkage)
Whether the definition of this global may be replaced by something non-equivalent at link time.
Definition: GlobalValue.h:420
static LinkageTypes getWeakLinkage(bool ODR)
Definition: GlobalValue.h:367
bool hasAvailableExternallyLinkage() const
Definition: GlobalValue.h:507
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:170
LinkageTypes
An enumeration for the kinds of linkage for global values.
Definition: GlobalValue.h:47
@ CommonLinkage
Tentative definitions.
Definition: GlobalValue.h:58
@ InternalLinkage
Rename collisions when linking (static functions).
Definition: GlobalValue.h:55
@ WeakODRLinkage
Same, but only replaced by something equivalent.
Definition: GlobalValue.h:53
@ ExternalLinkage
Externally visible function.
Definition: GlobalValue.h:48
@ WeakAnyLinkage
Keep one copy of named function when linking (weak)
Definition: GlobalValue.h:52
@ AppendingLinkage
Special purpose, only applies to global arrays.
Definition: GlobalValue.h:54
@ AvailableExternallyLinkage
Available for inspection, not emission.
Definition: GlobalValue.h:49
@ LinkOnceODRLinkage
Same, but only replaced by something equivalent.
Definition: GlobalValue.h:51
Type * getValueType() const
Definition: GlobalValue.h:292
static bool isLinkOnceODRLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:380
void eraseFromParent()
eraseFromParent - This method unlinks 'this' from the containing module and deletes it.
Definition: Globals.cpp:468
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:37
iterator begin()
Definition: MapVector.h:70
size_type size() const
Definition: MapVector.h:61
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< std::pair< uint64_t, 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.
std::pair< std::string, uint32_t > AsmSymbol
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
@ Error
Emits an error if two values disagree, otherwise the resulting value is that of the operands.
Definition: Module.h:118
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
T dyn_cast() const
Returns the current pointer if it is of the specified pointer type, otherwise returns null.
Definition: PointerUnion.h:162
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
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:164
bool empty() const
Definition: SmallSet.h:157
bool erase(const T &V)
Definition: SmallSet.h:205
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:177
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:261
unsigned size() const
Definition: StringMap.h:95
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition: StringMap.h:111
StringMapConstIterator< ValueTy > const_iterator
Definition: StringMap.h:200
iterator end()
Definition: StringMap.h:204
iterator begin()
Definition: StringMap.h:203
iterator find(StringRef Key)
Definition: StringMap.h:217
size_type count(StringRef Key) const
count - Return 1 if the element is in the map, 0 otherwise.
Definition: StringMap.h:245
std::pair< iterator, bool > try_emplace(StringRef Key, ArgsTy &&...Args)
Emplace a new element for the specified key into the map if the key isn't already in the map.
Definition: StringMap.h:329
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:423
unsigned RelaxELFRelocations
DebuggerKind DebuggerTuning
Which debugger to tune for.
unsigned FunctionSections
Emit functions into separate sections.
unsigned DataSections
Emit data into separate sections.
This tells how a thread pool will be used.
Definition: Threading.h:116
A ThreadPool for asynchronous parallel execution on a defined number of threads.
Definition: ThreadPool.h:52
void wait()
Blocking wait for all the threads to complete and the queue to be empty.
Definition: ThreadPool.cpp:200
unsigned getThreadCount() const
Definition: ThreadPool.h:110
auto async(Function &&F, Args &&...ArgList)
Asynchronous submission of a task to the pool.
Definition: ThreadPool.h:66
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
bool isOSBinFormatELF() const
Tests whether the OS uses the ELF binary format.
Definition: Triple.h:674
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
static IntegerType * getInt8Ty(LLVMContext &C)
void setName(const Twine &Name)
Change the name of the value.
Definition: Value.cpp:375
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:532
bool use_empty() const
Definition: Value.h:344
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:308
void takeName(Value *V)
Transfer the name from V to this value.
Definition: Value.cpp:381
std::pair< iterator, bool > insert(const ValueT &V)
Definition: DenseSet.h:206
iterator find(const_arg_type_t< ValueT > V)
Definition: DenseSet.h:179
size_type size() const
Definition: DenseSet.h:81
size_type count(const_arg_type_t< ValueT > V) const
Return 1 if the specified key is in the set, 0 otherwise.
Definition: DenseSet.h:97
An efficient, type-erasing, non-owning reference to a callable.
Ephemeral symbols produced by Reader::symbols() and Reader::module_symbols().
Definition: IRSymtab.h:313
The purpose of this class is to only expose the symbol information that an LTO client should need in ...
Definition: LTO.h:133
An input file.
Definition: LTO.h:105
static Expected< std::unique_ptr< InputFile > > create(MemoryBufferRef Object)
Create an InputFile.
Definition: LTO.cpp:475
ArrayRef< Symbol > symbols() const
A range over the symbols in this InputFile.
Definition: LTO.h:158
StringRef getName() const
Returns the path to the InputFile.
Definition: LTO.cpp:504
BitcodeModule & getSingleBitcodeModule()
Definition: LTO.cpp:508
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:632
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:1192
Error run(AddStreamFn AddStream, FileCache Cache=nullptr)
Runs the LTO pipeline.
Definition: LTO.cpp:1036
unsigned getMaxTasks() const
Returns an upper bound on the number of tasks that the client may expect.
Definition: LTO.cpp:990
LTO(Config Conf, ThinBackend Backend=nullptr, unsigned ParallelCodeGenParallelismLevel=1)
Create an LTO object.
Definition: LTO.cpp:526
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:454
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:1197
virtual unsigned getThreadCount()=0
const Config & Conf
Definition: LTO.cpp:1199
lto::IndexWriteCallback OnWrite
Definition: LTO.cpp:1202
ThinBackendProc(const Config &Conf, ModuleSummaryIndex &CombinedIndex, const StringMap< GVSummaryMapTy > &ModuleToDefinedGVSummaries, lto::IndexWriteCallback OnWrite, bool ShouldEmitImportsFiles)
Definition: LTO.cpp:1206
Error emitFiles(const FunctionImporter::ImportMapTy &ImportList, llvm::StringRef ModulePath, const std::string &NewModulePath)
Definition: LTO.cpp:1224
virtual Error wait()=0
ModuleSummaryIndex & CombinedIndex
Definition: LTO.cpp:1200
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
const StringMap< GVSummaryMapTy > & ModuleToDefinedGVSummaries
Definition: LTO.cpp:1201
#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:975
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:445
ThinBackend createInProcessThinBackend(ThreadPoolStrategy Parallelism, IndexWriteCallback OnWrite=nullptr, bool ShouldEmitIndexFiles=false, bool ShouldEmitImportsFiles=false)
Definition: LTO.cpp:1383
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:543
ThinBackend createWriteIndexesThinBackend(std::string OldPrefix, std::string NewPrefix, 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:1461
std::function< std::unique_ptr< ThinBackendProc >(const Config &C, ModuleSummaryIndex &CombinedIndex, StringMap< GVSummaryMapTy > &ModuleToDefinedGVSummaries, AddStreamFn AddStream, FileCache Cache)> ThinBackend
A ThinBackend defines what happens after the thin-link phase during ThinLTO.
Definition: LTO.h:196
std::function< void(const std::string &)> IndexWriteCallback
This ThinBackend runs the individual backend jobs in-process.
Definition: LTO.h:206
Expected< std::unique_ptr< ToolOutputFile > > setupStatsFile(StringRef StatsFilename)
Setups the output file for saving statistics.
Definition: LTO.cpp:1670
Error backend(const Config &C, AddStreamFn AddStream, unsigned ParallelCodeGenParallelismLevel, Module &M, ModuleSummaryIndex &CombinedIndex)
Runs a regular LTO backend.
Definition: LTOBackend.cpp:497
std::string getThinLTOOutputFile(const std::string &Path, const std::string &OldPrefix, const std::string &NewPrefix)
Given the original Path to an output file, replace any path prefix matching OldPrefix with NewPrefix.
Definition: LTO.cpp:1399
Error finalizeOptimizationRemarks(std::unique_ptr< ToolOutputFile > DiagOutputFile)
Definition: LTOBackend.cpp:486
std::vector< int > generateModulesOrdering(ArrayRef< BitcodeModule * > R)
Produces a container ordering for optimal multi-threaded processing.
Definition: LTO.cpp:1689
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:1645
Expected< IRSymtabFile > readIRSymtab(MemoryBufferRef MBRef)
Reads a bitcode file, creating its irsymtab if necessary.
void write64le(void *P, uint64_t V)
Definition: Endian.h:417
void write32le(void *P, uint32_t V)
Definition: Endian.h:416
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:967
StringRef parent_path(StringRef path, Style style=Style::native)
Get parent path.
Definition: Path.cpp:467
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:518
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:1735
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)
void updateVCallVisibilityInModule(Module &M, bool WholeProgramVisibilityEnabledInLTO, const DenseSet< GlobalValue::GUID > &DynamicExportSymbols)
If whole program visibility asserted, then upgrade all public vcall visibility metadata on vtable def...
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:79
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.
void gatherImportedSummariesForModule(StringRef ModulePath, const StringMap< 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.
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:462
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:81
void timeTraceProfilerFinishThread()
Finish a time trace profiler running on a worker thread.
void timeTraceProfilerBegin(StringRef Name, StringRef Detail)
Manually begin a time section, with the given Name and Detail.
Error joinErrors(Error E1, Error E2)
Concatenate errors.
Definition: Error.h:427
void updatePublicTypeTestCalls(Module &M, bool WholeProgramVisibilityEnabledInLTO)
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1683
bool timeTraceProfilerEnabled()
Is the time trace profiler enabled, i.e. initialized?
Definition: TimeProfiler.h:102
void ComputeCrossModuleImport(const ModuleSummaryIndex &Index, const StringMap< GVSummaryMapTy > &ModuleToDefinedGVSummaries, StringMap< FunctionImporter::ImportMapTy > &ImportLists, StringMap< FunctionImporter::ExportSetTy > &ExportLists)
Compute all the imports and exports for every module in the Index.
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"))
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:402
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.
void updateVCallVisibilityInIndex(ModuleSummaryIndex &Index, bool WholeProgramVisibilityEnabledInLTO, const DenseSet< GlobalValue::GUID > &DynamicExportSymbols)
If whole program visibility asserted, then upgrade all public vcall visibility metadata on vtable def...
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:1862
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:92
bool UpgradeDebugInfo(Module &M)
Check the debug info version number, if it is out-dated, drop the debug info.
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.
Definition: BitVector.h:851
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:80
std::optional< uint64_t > RemarksHotnessThreshold
The minimum hotness value a diagnostic needs in order to be included in optimization diagnostics.
Definition: Config.h:156
std::string StatsFile
Statistics output file path.
Definition: Config.h:165
ModuleHookFn PreOptModuleHook
This module hook is called after linking (regular LTO) or loading (ThinLTO) the module,...
Definition: Config.h:219
CombinedIndexHookFn CombinedIndexHook
Definition: Config.h:251
std::optional< CodeModel::Model > CodeModel
Definition: Config.h:56
std::string AAPipeline
Definition: Config.h:101
bool CodeGenOnly
Disable entirely the optimizer, including importing for ThinLTO.
Definition: Config.h:70
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:168
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:186
std::string DefaultTriple
Setting this field will replace unspecified target triples in input files with this triple.
Definition: Config.h:109
bool AlwaysEmitRegularLTOObj
Always emit a Regular LTO object even when it is empty because no Regular LTO modules were linked.
Definition: Config.h:85
std::string CPU
Definition: Config.h:48
std::string DwoDir
The directory to store .dwo files.
Definition: Config.h:121
std::string RemarksFilename
Optimization remarks file path.
Definition: Config.h:135
VisScheme VisibilityScheme
Allows non-imported definitions to get the potentially more constraining visibility from the prevaili...
Definition: Config.h:91
std::string OverrideTriple
Setting this field will replace target triples in input files with this triple.
Definition: Config.h:105
std::string ProfileRemapping
Name remapping file for profile data.
Definition: Config.h:118
TargetOptions Options
Definition: Config.h:49
bool TimeTraceEnabled
Time trace enabled.
Definition: Config.h:171
CodeGenOpt::Level CGOptLevel
Definition: Config.h:57
std::string RemarksPasses
Optimization remarks pass filter.
Definition: Config.h:138
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:96
ModuleHookFn PostInternalizeModuleHook
This hook is called after internalizing the module.
Definition: Config.h:226
unsigned TimeTraceGranularity
Time trace granularity.
Definition: Config.h:174
unsigned OptLevel
Definition: Config.h:59
bool RemarksWithHotness
Whether to emit optimization remarks with hotness informations.
Definition: Config.h:141
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:67
std::string SampleProfile
Sample PGO profile path.
Definition: Config.h:115
std::string RemarksFormat
The format used for serializing remarks (default: YAML).
Definition: Config.h:159
A derived class of LLVMContext that initializes itself according to a given Config object.
Definition: Config.h:290
The resolution for a symbol.
Definition: LTO.h:430
unsigned FinalDefinitionInLinkageUnit
The definition of this symbol is unpreemptable at runtime and is known to be in this linkage unit.
Definition: LTO.h:440
unsigned ExportDynamic
The symbol was exported dynamically, and therefore could be referenced by a shared library not visibl...
Definition: LTO.h:447
unsigned Prevailing
The linker has chosen this definition of the symbol.
Definition: LTO.h:436
unsigned LinkerRedefined
Linker redefined version of the symbol which appeared in -wrap or -defsym linker option.
Definition: LTO.h:451
unsigned VisibleToRegularObj
The definition of this symbol is visible outside of the LTO unit.
Definition: LTO.h:443