LLVM 22.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/ArrayRef.h"
15#include "llvm/ADT/ScopeExit.h"
16#include "llvm/ADT/SmallSet.h"
18#include "llvm/ADT/Statistic.h"
28#include "llvm/Config/llvm-config.h"
29#include "llvm/IR/AutoUpgrade.h"
31#include "llvm/IR/Intrinsics.h"
34#include "llvm/IR/Mangler.h"
35#include "llvm/IR/Metadata.h"
37#include "llvm/LTO/LTOBackend.h"
38#include "llvm/Linker/IRMover.h"
44#include "llvm/Support/Error.h"
46#include "llvm/Support/JSON.h"
48#include "llvm/Support/Path.h"
50#include "llvm/Support/SHA1.h"
56#include "llvm/Support/VCSRevision.h"
59#include "llvm/Transforms/IPO.h"
64
65#include <optional>
66#include <set>
67
68using namespace llvm;
69using namespace lto;
70using namespace object;
71
72#define DEBUG_TYPE "lto"
73
74static cl::opt<bool>
75 DumpThinCGSCCs("dump-thin-cg-sccs", cl::init(false), cl::Hidden,
76 cl::desc("Dump the SCCs in the ThinLTO index's callgraph"));
77
78namespace llvm {
81} // end namespace llvm
82
83namespace llvm {
84/// Enable global value internalization in LTO.
86 "enable-lto-internalization", cl::init(true), cl::Hidden,
87 cl::desc("Enable global value internalization in LTO"));
88
89static cl::opt<bool>
90 LTOKeepSymbolCopies("lto-keep-symbol-copies", cl::init(false), cl::Hidden,
91 cl::desc("Keep copies of symbols in LTO indexing"));
92
93/// Indicate we are linking with an allocator that supports hot/cold operator
94/// new interfaces.
96
97/// Enable MemProf context disambiguation for thin link.
99} // namespace llvm
100
101// Computes a unique hash for the Module considering the current list of
102// export/import and other global analysis results.
103// Returns the hash in its hexadecimal representation.
105 const Config &Conf, const ModuleSummaryIndex &Index, StringRef ModuleID,
106 const FunctionImporter::ImportMapTy &ImportList,
107 const FunctionImporter::ExportSetTy &ExportList,
108 const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
109 const GVSummaryMapTy &DefinedGlobals,
110 const DenseSet<GlobalValue::GUID> &CfiFunctionDefs,
111 const DenseSet<GlobalValue::GUID> &CfiFunctionDecls) {
112 // Compute the unique hash for this entry.
113 // This is based on the current compiler version, the module itself, the
114 // export list, the hash for every single module in the import list, the
115 // list of ResolvedODR for the module, and the list of preserved symbols.
116 SHA1 Hasher;
117
118 // Start with the compiler revision
119 Hasher.update(LLVM_VERSION_STRING);
120#ifdef LLVM_REVISION
121 Hasher.update(LLVM_REVISION);
122#endif
123
124 // Include the parts of the LTO configuration that affect code generation.
125 auto AddString = [&](StringRef Str) {
126 Hasher.update(Str);
127 Hasher.update(ArrayRef<uint8_t>{0});
128 };
129 auto AddUnsigned = [&](unsigned I) {
130 uint8_t Data[4];
132 Hasher.update(Data);
133 };
134 auto AddUint64 = [&](uint64_t I) {
135 uint8_t Data[8];
137 Hasher.update(Data);
138 };
139 auto AddUint8 = [&](const uint8_t I) {
140 Hasher.update(ArrayRef<uint8_t>(&I, 1));
141 };
142 AddString(Conf.CPU);
143 // FIXME: Hash more of Options. For now all clients initialize Options from
144 // command-line flags (which is unsupported in production), but may set
145 // X86RelaxRelocations. The clang driver can also pass FunctionSections,
146 // DataSections and DebuggerTuning via command line flags.
147 AddUnsigned(Conf.Options.MCOptions.X86RelaxRelocations);
148 AddUnsigned(Conf.Options.FunctionSections);
149 AddUnsigned(Conf.Options.DataSections);
150 AddUnsigned((unsigned)Conf.Options.DebuggerTuning);
151 for (auto &A : Conf.MAttrs)
152 AddString(A);
153 if (Conf.RelocModel)
154 AddUnsigned(*Conf.RelocModel);
155 else
156 AddUnsigned(-1);
157 if (Conf.CodeModel)
158 AddUnsigned(*Conf.CodeModel);
159 else
160 AddUnsigned(-1);
161 for (const auto &S : Conf.MllvmArgs)
162 AddString(S);
163 AddUnsigned(static_cast<int>(Conf.CGOptLevel));
164 AddUnsigned(static_cast<int>(Conf.CGFileType));
165 AddUnsigned(Conf.OptLevel);
166 AddUnsigned(Conf.Freestanding);
167 AddString(Conf.OptPipeline);
168 AddString(Conf.AAPipeline);
169 AddString(Conf.OverrideTriple);
170 AddString(Conf.DefaultTriple);
171 AddString(Conf.DwoDir);
172
173 // Include the hash for the current module
174 auto ModHash = Index.getModuleHash(ModuleID);
175 Hasher.update(ArrayRef<uint8_t>((uint8_t *)&ModHash[0], sizeof(ModHash)));
176
177 // TODO: `ExportList` is determined by `ImportList`. Since `ImportList` is
178 // used to compute cache key, we could omit hashing `ExportList` here.
179 std::vector<uint64_t> ExportsGUID;
180 ExportsGUID.reserve(ExportList.size());
181 for (const auto &VI : ExportList)
182 ExportsGUID.push_back(VI.getGUID());
183
184 // Sort the export list elements GUIDs.
185 llvm::sort(ExportsGUID);
186 for (auto GUID : ExportsGUID)
187 Hasher.update(ArrayRef<uint8_t>((uint8_t *)&GUID, sizeof(GUID)));
188
189 // Order using module hash, to be both independent of module name and
190 // module order.
191 auto Comp = [&](const std::pair<StringRef, GlobalValue::GUID> &L,
192 const std::pair<StringRef, GlobalValue::GUID> &R) {
193 return std::make_pair(Index.getModule(L.first)->second, L.second) <
194 std::make_pair(Index.getModule(R.first)->second, R.second);
195 };
196 FunctionImporter::SortedImportList SortedImportList(ImportList, Comp);
197
198 // Count the number of imports for each source module.
199 DenseMap<StringRef, unsigned> ModuleToNumImports;
200 for (const auto &[FromModule, GUID, Type] : SortedImportList)
201 ++ModuleToNumImports[FromModule];
202
203 std::optional<StringRef> LastModule;
204 for (const auto &[FromModule, GUID, Type] : SortedImportList) {
205 if (LastModule != FromModule) {
206 // Include the hash for every module we import functions from. The set of
207 // imported symbols for each module may affect code generation and is
208 // sensitive to link order, so include that as well.
209 LastModule = FromModule;
210 auto ModHash = Index.getModule(FromModule)->second;
211 Hasher.update(ArrayRef<uint8_t>((uint8_t *)&ModHash[0], sizeof(ModHash)));
212 AddUint64(ModuleToNumImports[FromModule]);
213 }
214 AddUint64(GUID);
215 AddUint8(Type);
216 }
217
218 // Include the hash for the resolved ODR.
219 for (auto &Entry : ResolvedODR) {
220 Hasher.update(ArrayRef<uint8_t>((const uint8_t *)&Entry.first,
221 sizeof(GlobalValue::GUID)));
222 Hasher.update(ArrayRef<uint8_t>((const uint8_t *)&Entry.second,
224 }
225
226 // Members of CfiFunctionDefs and CfiFunctionDecls that are referenced or
227 // defined in this module.
228 std::set<GlobalValue::GUID> UsedCfiDefs;
229 std::set<GlobalValue::GUID> UsedCfiDecls;
230
231 // Typeids used in this module.
232 std::set<GlobalValue::GUID> UsedTypeIds;
233
234 auto AddUsedCfiGlobal = [&](GlobalValue::GUID ValueGUID) {
235 if (CfiFunctionDefs.contains(ValueGUID))
236 UsedCfiDefs.insert(ValueGUID);
237 if (CfiFunctionDecls.contains(ValueGUID))
238 UsedCfiDecls.insert(ValueGUID);
239 };
240
241 auto AddUsedThings = [&](GlobalValueSummary *GS) {
242 if (!GS) return;
243 AddUnsigned(GS->getVisibility());
244 AddUnsigned(GS->isLive());
245 AddUnsigned(GS->canAutoHide());
246 for (const ValueInfo &VI : GS->refs()) {
247 AddUnsigned(VI.isDSOLocal(Index.withDSOLocalPropagation()));
248 AddUsedCfiGlobal(VI.getGUID());
249 }
250 if (auto *GVS = dyn_cast<GlobalVarSummary>(GS)) {
251 AddUnsigned(GVS->maybeReadOnly());
252 AddUnsigned(GVS->maybeWriteOnly());
253 }
254 if (auto *FS = dyn_cast<FunctionSummary>(GS)) {
255 for (auto &TT : FS->type_tests())
256 UsedTypeIds.insert(TT);
257 for (auto &TT : FS->type_test_assume_vcalls())
258 UsedTypeIds.insert(TT.GUID);
259 for (auto &TT : FS->type_checked_load_vcalls())
260 UsedTypeIds.insert(TT.GUID);
261 for (auto &TT : FS->type_test_assume_const_vcalls())
262 UsedTypeIds.insert(TT.VFunc.GUID);
263 for (auto &TT : FS->type_checked_load_const_vcalls())
264 UsedTypeIds.insert(TT.VFunc.GUID);
265 for (auto &ET : FS->calls()) {
266 AddUnsigned(ET.first.isDSOLocal(Index.withDSOLocalPropagation()));
267 AddUsedCfiGlobal(ET.first.getGUID());
268 }
269 }
270 };
271
272 // Include the hash for the linkage type to reflect internalization and weak
273 // resolution, and collect any used type identifier resolutions.
274 for (auto &GS : DefinedGlobals) {
275 GlobalValue::LinkageTypes Linkage = GS.second->linkage();
276 Hasher.update(
277 ArrayRef<uint8_t>((const uint8_t *)&Linkage, sizeof(Linkage)));
278 AddUsedCfiGlobal(GS.first);
279 AddUsedThings(GS.second);
280 }
281
282 // Imported functions may introduce new uses of type identifier resolutions,
283 // so we need to collect their used resolutions as well.
284 for (const auto &[FromModule, GUID, Type] : SortedImportList) {
285 GlobalValueSummary *S = Index.findSummaryInModule(GUID, FromModule);
286 AddUsedThings(S);
287 // If this is an alias, we also care about any types/etc. that the aliasee
288 // may reference.
289 if (auto *AS = dyn_cast_or_null<AliasSummary>(S))
290 AddUsedThings(AS->getBaseObject());
291 }
292
293 auto AddTypeIdSummary = [&](StringRef TId, const TypeIdSummary &S) {
294 AddString(TId);
295
296 AddUnsigned(S.TTRes.TheKind);
297 AddUnsigned(S.TTRes.SizeM1BitWidth);
298
299 AddUint64(S.TTRes.AlignLog2);
300 AddUint64(S.TTRes.SizeM1);
301 AddUint64(S.TTRes.BitMask);
302 AddUint64(S.TTRes.InlineBits);
303
304 AddUint64(S.WPDRes.size());
305 for (auto &WPD : S.WPDRes) {
306 AddUnsigned(WPD.first);
307 AddUnsigned(WPD.second.TheKind);
308 AddString(WPD.second.SingleImplName);
309
310 AddUint64(WPD.second.ResByArg.size());
311 for (auto &ByArg : WPD.second.ResByArg) {
312 AddUint64(ByArg.first.size());
313 for (uint64_t Arg : ByArg.first)
314 AddUint64(Arg);
315 AddUnsigned(ByArg.second.TheKind);
316 AddUint64(ByArg.second.Info);
317 AddUnsigned(ByArg.second.Byte);
318 AddUnsigned(ByArg.second.Bit);
319 }
320 }
321 };
322
323 // Include the hash for all type identifiers used by this module.
324 for (GlobalValue::GUID TId : UsedTypeIds) {
325 auto TidIter = Index.typeIds().equal_range(TId);
326 for (const auto &I : make_range(TidIter))
327 AddTypeIdSummary(I.second.first, I.second.second);
328 }
329
330 AddUnsigned(UsedCfiDefs.size());
331 for (auto &V : UsedCfiDefs)
332 AddUint64(V);
333
334 AddUnsigned(UsedCfiDecls.size());
335 for (auto &V : UsedCfiDecls)
336 AddUint64(V);
337
338 if (!Conf.SampleProfile.empty()) {
339 auto FileOrErr = MemoryBuffer::getFile(Conf.SampleProfile);
340 if (FileOrErr) {
341 Hasher.update(FileOrErr.get()->getBuffer());
342
343 if (!Conf.ProfileRemapping.empty()) {
344 FileOrErr = MemoryBuffer::getFile(Conf.ProfileRemapping);
345 if (FileOrErr)
346 Hasher.update(FileOrErr.get()->getBuffer());
347 }
348 }
349 }
350
351 return toHex(Hasher.result());
352}
353
354std::string llvm::recomputeLTOCacheKey(const std::string &Key,
355 StringRef ExtraID) {
356 SHA1 Hasher;
357
358 auto AddString = [&](StringRef Str) {
359 Hasher.update(Str);
360 Hasher.update(ArrayRef<uint8_t>{0});
361 };
362 AddString(Key);
363 AddString(ExtraID);
364
365 return toHex(Hasher.result());
366}
367
369 const Config &C, ValueInfo VI,
370 DenseSet<GlobalValueSummary *> &GlobalInvolvedWithAlias,
372 isPrevailing,
374 recordNewLinkage,
375 const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols) {
377 C.VisibilityScheme == Config::ELF ? VI.getELFVisibility()
379 for (auto &S : VI.getSummaryList()) {
380 GlobalValue::LinkageTypes OriginalLinkage = S->linkage();
381 // Ignore local and appending linkage values since the linker
382 // doesn't resolve them.
383 if (GlobalValue::isLocalLinkage(OriginalLinkage) ||
385 continue;
386 // We need to emit only one of these. The prevailing module will keep it,
387 // but turned into a weak, while the others will drop it when possible.
388 // This is both a compile-time optimization and a correctness
389 // transformation. This is necessary for correctness when we have exported
390 // a reference - we need to convert the linkonce to weak to
391 // ensure a copy is kept to satisfy the exported reference.
392 // FIXME: We may want to split the compile time and correctness
393 // aspects into separate routines.
394 if (isPrevailing(VI.getGUID(), S.get())) {
395 if (GlobalValue::isLinkOnceLinkage(OriginalLinkage)) {
396 S->setLinkage(GlobalValue::getWeakLinkage(
397 GlobalValue::isLinkOnceODRLinkage(OriginalLinkage)));
398 // The kept copy is eligible for auto-hiding (hidden visibility) if all
399 // copies were (i.e. they were all linkonce_odr global unnamed addr).
400 // If any copy is not (e.g. it was originally weak_odr), then the symbol
401 // must remain externally available (e.g. a weak_odr from an explicitly
402 // instantiated template). Additionally, if it is in the
403 // GUIDPreservedSymbols set, that means that it is visibile outside
404 // the summary (e.g. in a native object or a bitcode file without
405 // summary), and in that case we cannot hide it as it isn't possible to
406 // check all copies.
407 S->setCanAutoHide(VI.canAutoHide() &&
408 !GUIDPreservedSymbols.count(VI.getGUID()));
409 }
410 if (C.VisibilityScheme == Config::FromPrevailing)
411 Visibility = S->getVisibility();
412 }
413 // Alias and aliasee can't be turned into available_externally.
414 // When force-import-all is used, it indicates that object linking is not
415 // supported by the target. In this case, we can't change the linkage as
416 // well in case the global is converted to declaration.
417 else if (!isa<AliasSummary>(S.get()) &&
418 !GlobalInvolvedWithAlias.count(S.get()) && !ForceImportAll)
420
421 // For ELF, set visibility to the computed visibility from summaries. We
422 // don't track visibility from declarations so this may be more relaxed than
423 // the most constraining one.
424 if (C.VisibilityScheme == Config::ELF)
425 S->setVisibility(Visibility);
426
427 if (S->linkage() != OriginalLinkage)
428 recordNewLinkage(S->modulePath(), VI.getGUID(), S->linkage());
429 }
430
431 if (C.VisibilityScheme == Config::FromPrevailing) {
432 for (auto &S : VI.getSummaryList()) {
433 GlobalValue::LinkageTypes OriginalLinkage = S->linkage();
434 if (GlobalValue::isLocalLinkage(OriginalLinkage) ||
436 continue;
437 S->setVisibility(Visibility);
438 }
439 }
440}
441
442/// Resolve linkage for prevailing symbols in the \p Index.
443//
444// We'd like to drop these functions if they are no longer referenced in the
445// current module. However there is a chance that another module is still
446// referencing them because of the import. We make sure we always emit at least
447// one copy.
449 const Config &C, ModuleSummaryIndex &Index,
451 isPrevailing,
453 recordNewLinkage,
454 const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols) {
455 // We won't optimize the globals that are referenced by an alias for now
456 // Ideally we should turn the alias into a global and duplicate the definition
457 // when needed.
458 DenseSet<GlobalValueSummary *> GlobalInvolvedWithAlias;
459 for (auto &I : Index)
460 for (auto &S : I.second.SummaryList)
461 if (auto AS = dyn_cast<AliasSummary>(S.get()))
462 GlobalInvolvedWithAlias.insert(&AS->getAliasee());
463
464 for (auto &I : Index)
465 thinLTOResolvePrevailingGUID(C, Index.getValueInfo(I),
466 GlobalInvolvedWithAlias, isPrevailing,
467 recordNewLinkage, GUIDPreservedSymbols);
468}
469
471 ValueInfo VI, function_ref<bool(StringRef, ValueInfo)> isExported,
473 isPrevailing) {
474 auto ExternallyVisibleCopies =
475 llvm::count_if(VI.getSummaryList(),
476 [](const std::unique_ptr<GlobalValueSummary> &Summary) {
477 return !GlobalValue::isLocalLinkage(Summary->linkage());
478 });
479
480 for (auto &S : VI.getSummaryList()) {
481 // First see if we need to promote an internal value because it is not
482 // exported.
483 if (isExported(S->modulePath(), VI)) {
484 if (GlobalValue::isLocalLinkage(S->linkage()))
485 S->setLinkage(GlobalValue::ExternalLinkage);
486 continue;
487 }
488
489 // Otherwise, see if we can internalize.
491 continue;
492
493 // Non-exported values with external linkage can be internalized.
494 if (GlobalValue::isExternalLinkage(S->linkage())) {
495 S->setLinkage(GlobalValue::InternalLinkage);
496 continue;
497 }
498
499 // Non-exported function and variable definitions with a weak-for-linker
500 // linkage can be internalized in certain cases. The minimum legality
501 // requirements would be that they are not address taken to ensure that we
502 // don't break pointer equality checks, and that variables are either read-
503 // or write-only. For functions, this is the case if either all copies are
504 // [local_]unnamed_addr, or we can propagate reference edge attributes
505 // (which is how this is guaranteed for variables, when analyzing whether
506 // they are read or write-only).
507 //
508 // However, we only get to this code for weak-for-linkage values in one of
509 // two cases:
510 // 1) The prevailing copy is not in IR (it is in native code).
511 // 2) The prevailing copy in IR is not exported from its module.
512 // Additionally, at least for the new LTO API, case 2 will only happen if
513 // there is exactly one definition of the value (i.e. in exactly one
514 // module), as duplicate defs are result in the value being marked exported.
515 // Likely, users of the legacy LTO API are similar, however, currently there
516 // are llvm-lto based tests of the legacy LTO API that do not mark
517 // duplicate linkonce_odr copies as exported via the tool, so we need
518 // to handle that case below by checking the number of copies.
519 //
520 // Generally, we only want to internalize a weak-for-linker value in case
521 // 2, because in case 1 we cannot see how the value is used to know if it
522 // is read or write-only. We also don't want to bloat the binary with
523 // multiple internalized copies of non-prevailing linkonce/weak functions.
524 // Note if we don't internalize, we will convert non-prevailing copies to
525 // available_externally anyway, so that we drop them after inlining. The
526 // only reason to internalize such a function is if we indeed have a single
527 // copy, because internalizing it won't increase binary size, and enables
528 // use of inliner heuristics that are more aggressive in the face of a
529 // single call to a static (local). For variables, internalizing a read or
530 // write only variable can enable more aggressive optimization. However, we
531 // already perform this elsewhere in the ThinLTO backend handling for
532 // read or write-only variables (processGlobalForThinLTO).
533 //
534 // Therefore, only internalize linkonce/weak if there is a single copy, that
535 // is prevailing in this IR module. We can do so aggressively, without
536 // requiring the address to be insignificant, or that a variable be read or
537 // write-only.
538 if (!GlobalValue::isWeakForLinker(S->linkage()) ||
540 continue;
541
542 if (isPrevailing(VI.getGUID(), S.get()) && ExternallyVisibleCopies == 1)
543 S->setLinkage(GlobalValue::InternalLinkage);
544 }
545}
546
547// Update the linkages in the given \p Index to mark exported values
548// as external and non-exported values as internal.
550 ModuleSummaryIndex &Index,
551 function_ref<bool(StringRef, ValueInfo)> isExported,
553 isPrevailing) {
554 for (auto &I : Index)
555 thinLTOInternalizeAndPromoteGUID(Index.getValueInfo(I), isExported,
556 isPrevailing);
557}
558
559// Requires a destructor for std::vector<InputModule>.
560InputFile::~InputFile() = default;
561
563 std::unique_ptr<InputFile> File(new InputFile);
564
565 Expected<IRSymtabFile> FOrErr = readIRSymtab(Object);
566 if (!FOrErr)
567 return FOrErr.takeError();
568
569 File->TargetTriple = FOrErr->TheReader.getTargetTriple();
570 File->SourceFileName = FOrErr->TheReader.getSourceFileName();
571 File->COFFLinkerOpts = FOrErr->TheReader.getCOFFLinkerOpts();
572 File->DependentLibraries = FOrErr->TheReader.getDependentLibraries();
573 File->ComdatTable = FOrErr->TheReader.getComdatTable();
574
575 for (unsigned I = 0; I != FOrErr->Mods.size(); ++I) {
576 size_t Begin = File->Symbols.size();
577 for (const irsymtab::Reader::SymbolRef &Sym :
578 FOrErr->TheReader.module_symbols(I))
579 // Skip symbols that are irrelevant to LTO. Note that this condition needs
580 // to match the one in Skip() in LTO::addRegularLTO().
581 if (Sym.isGlobal() && !Sym.isFormatSpecific())
582 File->Symbols.push_back(Sym);
583 File->ModuleSymIndices.push_back({Begin, File->Symbols.size()});
584 }
585
586 File->Mods = FOrErr->Mods;
587 File->Strtab = std::move(FOrErr->Strtab);
588 return std::move(File);
589}
590
592 return Mods[0].getModuleIdentifier();
593}
594
596 assert(Mods.size() == 1 && "Expect only one bitcode module");
597 return Mods[0];
598}
599
600LTO::RegularLTOState::RegularLTOState(unsigned ParallelCodeGenParallelismLevel,
601 const Config &Conf)
602 : ParallelCodeGenParallelismLevel(ParallelCodeGenParallelismLevel),
603 Ctx(Conf), CombinedModule(std::make_unique<Module>("ld-temp.o", Ctx)),
604 Mover(std::make_unique<IRMover>(*CombinedModule)) {}
605
606LTO::ThinLTOState::ThinLTOState(ThinBackend BackendParam)
607 : Backend(std::move(BackendParam)), CombinedIndex(/*HaveGVs*/ false) {
608 if (!Backend.isValid())
609 Backend =
611}
612
614 unsigned ParallelCodeGenParallelismLevel, LTOKind LTOMode)
615 : Conf(std::move(Conf)),
616 RegularLTO(ParallelCodeGenParallelismLevel, this->Conf),
617 ThinLTO(std::move(Backend)),
618 GlobalResolutions(
619 std::make_unique<DenseMap<StringRef, GlobalResolution>>()),
620 LTOMode(LTOMode) {
621 if (Conf.KeepSymbolNameCopies || LTOKeepSymbolCopies) {
622 Alloc = std::make_unique<BumpPtrAllocator>();
623 GlobalResolutionSymbolSaver = std::make_unique<llvm::StringSaver>(*Alloc);
624 }
625}
626
627// Requires a destructor for MapVector<BitcodeModule>.
628LTO::~LTO() = default;
629
630// Add the symbols in the given module to the GlobalResolutions map, and resolve
631// their partitions.
632void LTO::addModuleToGlobalRes(ArrayRef<InputFile::Symbol> Syms,
634 unsigned Partition, bool InSummary) {
635 llvm::TimeTraceScope timeScope("LTO add module to global resolution");
636 auto *ResI = Res.begin();
637 auto *ResE = Res.end();
638 (void)ResE;
639 for (const InputFile::Symbol &Sym : Syms) {
640 assert(ResI != ResE);
641 SymbolResolution Res = *ResI++;
642
643 StringRef SymbolName = Sym.getName();
644 // Keep copies of symbols if the client of LTO says so.
645 if (GlobalResolutionSymbolSaver && !GlobalResolutions->contains(SymbolName))
646 SymbolName = GlobalResolutionSymbolSaver->save(SymbolName);
647
648 auto &GlobalRes = (*GlobalResolutions)[SymbolName];
649 GlobalRes.UnnamedAddr &= Sym.isUnnamedAddr();
650 if (Res.Prevailing) {
651 assert(!GlobalRes.Prevailing &&
652 "Multiple prevailing defs are not allowed");
653 GlobalRes.Prevailing = true;
654 GlobalRes.IRName = std::string(Sym.getIRName());
655 } else if (!GlobalRes.Prevailing && GlobalRes.IRName.empty()) {
656 // Sometimes it can be two copies of symbol in a module and prevailing
657 // symbol can have no IR name. That might happen if symbol is defined in
658 // module level inline asm block. In case we have multiple modules with
659 // the same symbol we want to use IR name of the prevailing symbol.
660 // Otherwise, if we haven't seen a prevailing symbol, set the name so that
661 // we can later use it to check if there is any prevailing copy in IR.
662 GlobalRes.IRName = std::string(Sym.getIRName());
663 }
664
665 // In rare occasion, the symbol used to initialize GlobalRes has a different
666 // IRName from the inspected Symbol. This can happen on macOS + iOS, when a
667 // symbol is referenced through its mangled name, say @"\01_symbol" while
668 // the IRName is @symbol (the prefix underscore comes from MachO mangling).
669 // In that case, we have the same actual Symbol that can get two different
670 // GUID, leading to some invalid internalization. Workaround this by marking
671 // the GlobalRes external.
672
673 // FIXME: instead of this check, it would be desirable to compute GUIDs
674 // based on mangled name, but this requires an access to the Target Triple
675 // and would be relatively invasive on the codebase.
676 if (GlobalRes.IRName != Sym.getIRName()) {
677 GlobalRes.Partition = GlobalResolution::External;
678 GlobalRes.VisibleOutsideSummary = true;
679 }
680
681 // Set the partition to external if we know it is re-defined by the linker
682 // with -defsym or -wrap options, used elsewhere, e.g. it is visible to a
683 // regular object, is referenced from llvm.compiler.used/llvm.used, or was
684 // already recorded as being referenced from a different partition.
685 if (Res.LinkerRedefined || Res.VisibleToRegularObj || Sym.isUsed() ||
686 (GlobalRes.Partition != GlobalResolution::Unknown &&
687 GlobalRes.Partition != Partition)) {
688 GlobalRes.Partition = GlobalResolution::External;
689 } else
690 // First recorded reference, save the current partition.
691 GlobalRes.Partition = Partition;
692
693 // Flag as visible outside of summary if visible from a regular object or
694 // from a module that does not have a summary.
695 GlobalRes.VisibleOutsideSummary |=
696 (Res.VisibleToRegularObj || Sym.isUsed() || !InSummary);
697
698 GlobalRes.ExportDynamic |= Res.ExportDynamic;
699 }
700}
701
702void LTO::releaseGlobalResolutionsMemory() {
703 // Release GlobalResolutions dense-map itself.
704 GlobalResolutions.reset();
705 // Release the string saver memory.
706 GlobalResolutionSymbolSaver.reset();
707 Alloc.reset();
708}
709
712 StringRef Path = Input->getName();
713 OS << Path << '\n';
714 auto ResI = Res.begin();
715 for (const InputFile::Symbol &Sym : Input->symbols()) {
716 assert(ResI != Res.end());
717 SymbolResolution Res = *ResI++;
718
719 OS << "-r=" << Path << ',' << Sym.getName() << ',';
720 if (Res.Prevailing)
721 OS << 'p';
723 OS << 'l';
724 if (Res.VisibleToRegularObj)
725 OS << 'x';
726 if (Res.LinkerRedefined)
727 OS << 'r';
728 OS << '\n';
729 }
730 OS.flush();
731 assert(ResI == Res.end());
732}
733
734Error LTO::add(std::unique_ptr<InputFile> Input,
736 llvm::TimeTraceScope timeScope("LTO add input", Input->getName());
737 assert(!CalledGetMaxTasks);
738
739 if (Conf.ResolutionFile)
740 writeToResolutionFile(*Conf.ResolutionFile, Input.get(), Res);
741
742 if (RegularLTO.CombinedModule->getTargetTriple().empty()) {
743 Triple InputTriple(Input->getTargetTriple());
744 RegularLTO.CombinedModule->setTargetTriple(InputTriple);
745 if (InputTriple.isOSBinFormatELF())
746 Conf.VisibilityScheme = Config::ELF;
747 }
748
749 ArrayRef<SymbolResolution> InputRes = Res;
750 for (unsigned I = 0; I != Input->Mods.size(); ++I) {
751 if (auto Err = addModule(*Input, InputRes, I, Res).moveInto(Res))
752 return Err;
753 }
754
755 assert(Res.empty());
756 return Error::success();
757}
758
760LTO::addModule(InputFile &Input, ArrayRef<SymbolResolution> InputRes,
761 unsigned ModI, ArrayRef<SymbolResolution> Res) {
762 llvm::TimeTraceScope timeScope("LTO add module", Input.getName());
763 Expected<BitcodeLTOInfo> LTOInfo = Input.Mods[ModI].getLTOInfo();
764 if (!LTOInfo)
765 return LTOInfo.takeError();
766
767 if (EnableSplitLTOUnit) {
768 // If only some modules were split, flag this in the index so that
769 // we can skip or error on optimizations that need consistently split
770 // modules (whole program devirt and lower type tests).
771 if (*EnableSplitLTOUnit != LTOInfo->EnableSplitLTOUnit)
772 ThinLTO.CombinedIndex.setPartiallySplitLTOUnits();
773 } else
774 EnableSplitLTOUnit = LTOInfo->EnableSplitLTOUnit;
775
776 BitcodeModule BM = Input.Mods[ModI];
777
778 if ((LTOMode == LTOK_UnifiedRegular || LTOMode == LTOK_UnifiedThin) &&
779 !LTOInfo->UnifiedLTO)
781 "unified LTO compilation must use "
782 "compatible bitcode modules (use -funified-lto)",
784
785 if (LTOInfo->UnifiedLTO && LTOMode == LTOK_Default)
786 LTOMode = LTOK_UnifiedThin;
787
788 bool IsThinLTO = LTOInfo->IsThinLTO && (LTOMode != LTOK_UnifiedRegular);
789
790 auto ModSyms = Input.module_symbols(ModI);
791 addModuleToGlobalRes(ModSyms, Res,
792 IsThinLTO ? ThinLTO.ModuleMap.size() + 1 : 0,
793 LTOInfo->HasSummary);
794
795 if (IsThinLTO)
796 return addThinLTO(BM, ModSyms, Res);
797
798 RegularLTO.EmptyCombinedModule = false;
799 auto ModOrErr = addRegularLTO(Input, InputRes, BM, ModSyms, Res);
800 if (!ModOrErr)
801 return ModOrErr.takeError();
802 Res = ModOrErr->second;
803
804 if (!LTOInfo->HasSummary) {
805 if (Error Err = linkRegularLTO(std::move(ModOrErr->first),
806 /*LivenessFromIndex=*/false))
807 return Err;
808 return Res;
809 }
810
811 // Regular LTO module summaries are added to a dummy module that represents
812 // the combined regular LTO module.
813 if (Error Err = BM.readSummary(ThinLTO.CombinedIndex, ""))
814 return Err;
815 RegularLTO.ModsWithSummaries.push_back(std::move(ModOrErr->first));
816 return Res;
817}
818
819// Checks whether the given global value is in a non-prevailing comdat
820// (comdat containing values the linker indicated were not prevailing,
821// which we then dropped to available_externally), and if so, removes
822// it from the comdat. This is called for all global values to ensure the
823// comdat is empty rather than leaving an incomplete comdat. It is needed for
824// regular LTO modules, in case we are in a mixed-LTO mode (both regular
825// and thin LTO modules) compilation. Since the regular LTO module will be
826// linked first in the final native link, we want to make sure the linker
827// doesn't select any of these incomplete comdats that would be left
828// in the regular LTO module without this cleanup.
829static void
831 std::set<const Comdat *> &NonPrevailingComdats) {
832 Comdat *C = GV.getComdat();
833 if (!C)
834 return;
835
836 if (!NonPrevailingComdats.count(C))
837 return;
838
839 // Additionally need to drop all global values from the comdat to
840 // available_externally, to satisfy the COMDAT requirement that all members
841 // are discarded as a unit. The non-local linkage global values avoid
842 // duplicate definition linker errors.
844
845 if (auto GO = dyn_cast<GlobalObject>(&GV))
846 GO->setComdat(nullptr);
847}
848
849// Add a regular LTO object to the link.
850// The resulting module needs to be linked into the combined LTO module with
851// linkRegularLTO.
852Expected<
853 std::pair<LTO::RegularLTOState::AddedModule, ArrayRef<SymbolResolution>>>
854LTO::addRegularLTO(InputFile &Input, ArrayRef<SymbolResolution> InputRes,
855 BitcodeModule BM, ArrayRef<InputFile::Symbol> Syms,
857 llvm::TimeTraceScope timeScope("LTO add regular LTO");
859 Expected<std::unique_ptr<Module>> MOrErr =
860 BM.getLazyModule(RegularLTO.Ctx, /*ShouldLazyLoadMetadata*/ true,
861 /*IsImporting*/ false);
862 if (!MOrErr)
863 return MOrErr.takeError();
864 Module &M = **MOrErr;
865 Mod.M = std::move(*MOrErr);
866
867 if (Error Err = M.materializeMetadata())
868 return std::move(Err);
869
870 if (LTOMode == LTOK_UnifiedRegular) {
871 // cfi.functions metadata is intended to be used with ThinLTO and may
872 // trigger invalid IR transformations if they are present when doing regular
873 // LTO, so delete it.
874 if (NamedMDNode *CfiFunctionsMD = M.getNamedMetadata("cfi.functions"))
875 M.eraseNamedMetadata(CfiFunctionsMD);
876 } else if (NamedMDNode *AliasesMD = M.getNamedMetadata("aliases")) {
877 // Delete aliases entries for non-prevailing symbols on the ThinLTO side of
878 // this input file.
879 DenseSet<StringRef> Prevailing;
880 for (auto [I, R] : zip(Input.symbols(), InputRes))
881 if (R.Prevailing && !I.getIRName().empty())
882 Prevailing.insert(I.getIRName());
883 std::vector<MDNode *> AliasGroups;
884 for (MDNode *AliasGroup : AliasesMD->operands()) {
885 std::vector<Metadata *> Aliases;
886 for (Metadata *Alias : AliasGroup->operands()) {
887 if (isa<MDString>(Alias) &&
888 Prevailing.count(cast<MDString>(Alias)->getString()))
889 Aliases.push_back(Alias);
890 }
891 if (Aliases.size() > 1)
892 AliasGroups.push_back(MDTuple::get(RegularLTO.Ctx, Aliases));
893 }
894 AliasesMD->clearOperands();
895 for (MDNode *G : AliasGroups)
896 AliasesMD->addOperand(G);
897 }
898
900
901 ModuleSymbolTable SymTab;
902 SymTab.addModule(&M);
903
904 for (GlobalVariable &GV : M.globals())
905 if (GV.hasAppendingLinkage())
906 Mod.Keep.push_back(&GV);
907
908 DenseSet<GlobalObject *> AliasedGlobals;
909 for (auto &GA : M.aliases())
910 if (GlobalObject *GO = GA.getAliaseeObject())
911 AliasedGlobals.insert(GO);
912
913 // In this function we need IR GlobalValues matching the symbols in Syms
914 // (which is not backed by a module), so we need to enumerate them in the same
915 // order. The symbol enumeration order of a ModuleSymbolTable intentionally
916 // matches the order of an irsymtab, but when we read the irsymtab in
917 // InputFile::create we omit some symbols that are irrelevant to LTO. The
918 // Skip() function skips the same symbols from the module as InputFile does
919 // from the symbol table.
920 auto MsymI = SymTab.symbols().begin(), MsymE = SymTab.symbols().end();
921 auto Skip = [&]() {
922 while (MsymI != MsymE) {
923 auto Flags = SymTab.getSymbolFlags(*MsymI);
924 if ((Flags & object::BasicSymbolRef::SF_Global) &&
926 return;
927 ++MsymI;
928 }
929 };
930 Skip();
931
932 std::set<const Comdat *> NonPrevailingComdats;
933 SmallSet<StringRef, 2> NonPrevailingAsmSymbols;
934 for (const InputFile::Symbol &Sym : Syms) {
935 assert(!Res.empty());
936 const SymbolResolution &R = Res.consume_front();
937
938 assert(MsymI != MsymE);
939 ModuleSymbolTable::Symbol Msym = *MsymI++;
940 Skip();
941
942 if (GlobalValue *GV = dyn_cast_if_present<GlobalValue *>(Msym)) {
943 if (R.Prevailing) {
944 if (Sym.isUndefined())
945 continue;
946 Mod.Keep.push_back(GV);
947 // For symbols re-defined with linker -wrap and -defsym options,
948 // set the linkage to weak to inhibit IPO. The linkage will be
949 // restored by the linker.
950 if (R.LinkerRedefined)
951 GV->setLinkage(GlobalValue::WeakAnyLinkage);
952
953 GlobalValue::LinkageTypes OriginalLinkage = GV->getLinkage();
954 if (GlobalValue::isLinkOnceLinkage(OriginalLinkage))
955 GV->setLinkage(GlobalValue::getWeakLinkage(
956 GlobalValue::isLinkOnceODRLinkage(OriginalLinkage)));
957 } else if (isa<GlobalObject>(GV) &&
958 (GV->hasLinkOnceODRLinkage() || GV->hasWeakODRLinkage() ||
959 GV->hasAvailableExternallyLinkage()) &&
960 !AliasedGlobals.count(cast<GlobalObject>(GV))) {
961 // Any of the above three types of linkage indicates that the
962 // chosen prevailing symbol will have the same semantics as this copy of
963 // the symbol, so we may be able to link it with available_externally
964 // linkage. We will decide later whether to do that when we link this
965 // module (in linkRegularLTO), based on whether it is undefined.
966 Mod.Keep.push_back(GV);
968 if (GV->hasComdat())
969 NonPrevailingComdats.insert(GV->getComdat());
970 cast<GlobalObject>(GV)->setComdat(nullptr);
971 }
972
973 // Set the 'local' flag based on the linker resolution for this symbol.
974 if (R.FinalDefinitionInLinkageUnit) {
975 GV->setDSOLocal(true);
976 if (GV->hasDLLImportStorageClass())
977 GV->setDLLStorageClass(GlobalValue::DLLStorageClassTypes::
978 DefaultStorageClass);
979 }
980 } else if (auto *AS =
982 // Collect non-prevailing symbols.
983 if (!R.Prevailing)
984 NonPrevailingAsmSymbols.insert(AS->first);
985 } else {
986 llvm_unreachable("unknown symbol type");
987 }
988
989 // Common resolution: collect the maximum size/alignment over all commons.
990 // We also record if we see an instance of a common as prevailing, so that
991 // if none is prevailing we can ignore it later.
992 if (Sym.isCommon()) {
993 // FIXME: We should figure out what to do about commons defined by asm.
994 // For now they aren't reported correctly by ModuleSymbolTable.
995 auto &CommonRes = RegularLTO.Commons[std::string(Sym.getIRName())];
996 CommonRes.Size = std::max(CommonRes.Size, Sym.getCommonSize());
997 if (uint32_t SymAlignValue = Sym.getCommonAlignment()) {
998 CommonRes.Alignment =
999 std::max(Align(SymAlignValue), CommonRes.Alignment);
1000 }
1001 CommonRes.Prevailing |= R.Prevailing;
1002 }
1003 }
1004
1005 if (!M.getComdatSymbolTable().empty())
1006 for (GlobalValue &GV : M.global_values())
1007 handleNonPrevailingComdat(GV, NonPrevailingComdats);
1008
1009 // Prepend ".lto_discard <sym>, <sym>*" directive to each module inline asm
1010 // block.
1011 if (!M.getModuleInlineAsm().empty()) {
1012 std::string NewIA = ".lto_discard";
1013 if (!NonPrevailingAsmSymbols.empty()) {
1014 // Don't dicard a symbol if there is a live .symver for it.
1016 M, [&](StringRef Name, StringRef Alias) {
1017 if (!NonPrevailingAsmSymbols.count(Alias))
1018 NonPrevailingAsmSymbols.erase(Name);
1019 });
1020 NewIA += " " + llvm::join(NonPrevailingAsmSymbols, ", ");
1021 }
1022 NewIA += "\n";
1023 M.setModuleInlineAsm(NewIA + M.getModuleInlineAsm());
1024 }
1025
1026 assert(MsymI == MsymE);
1027 return std::make_pair(std::move(Mod), Res);
1028}
1029
1030Error LTO::linkRegularLTO(RegularLTOState::AddedModule Mod,
1031 bool LivenessFromIndex) {
1032 llvm::TimeTraceScope timeScope("LTO link regular LTO");
1033 std::vector<GlobalValue *> Keep;
1034 for (GlobalValue *GV : Mod.Keep) {
1035 if (LivenessFromIndex && !ThinLTO.CombinedIndex.isGUIDLive(GV->getGUID())) {
1036 if (Function *F = dyn_cast<Function>(GV)) {
1037 if (DiagnosticOutputFile) {
1038 if (Error Err = F->materialize())
1039 return Err;
1040 OptimizationRemarkEmitter ORE(F, nullptr);
1041 ORE.emit(OptimizationRemark(DEBUG_TYPE, "deadfunction", F)
1042 << ore::NV("Function", F)
1043 << " not added to the combined module ");
1044 }
1045 }
1046 continue;
1047 }
1048
1049 if (!GV->hasAvailableExternallyLinkage()) {
1050 Keep.push_back(GV);
1051 continue;
1052 }
1053
1054 // Only link available_externally definitions if we don't already have a
1055 // definition.
1056 GlobalValue *CombinedGV =
1057 RegularLTO.CombinedModule->getNamedValue(GV->getName());
1058 if (CombinedGV && !CombinedGV->isDeclaration())
1059 continue;
1060
1061 Keep.push_back(GV);
1062 }
1063
1064 return RegularLTO.Mover->move(std::move(Mod.M), Keep, nullptr,
1065 /* IsPerformingImport */ false);
1066}
1067
1068// Add a ThinLTO module to the link.
1069Expected<ArrayRef<SymbolResolution>>
1070LTO::addThinLTO(BitcodeModule BM, ArrayRef<InputFile::Symbol> Syms,
1072 llvm::TimeTraceScope timeScope("LTO add thin LTO");
1073 ArrayRef<SymbolResolution> ResTmp = Res;
1074 for (const InputFile::Symbol &Sym : Syms) {
1075 assert(!ResTmp.empty());
1076 const SymbolResolution &R = ResTmp.consume_front();
1077
1078 if (!Sym.getIRName().empty()) {
1080 GlobalValue::getGlobalIdentifier(Sym.getIRName(),
1082 if (R.Prevailing)
1083 ThinLTO.PrevailingModuleForGUID[GUID] = BM.getModuleIdentifier();
1084 }
1085 }
1086
1087 if (Error Err =
1088 BM.readSummary(ThinLTO.CombinedIndex, BM.getModuleIdentifier(),
1089 [&](GlobalValue::GUID GUID) {
1090 return ThinLTO.PrevailingModuleForGUID[GUID] ==
1091 BM.getModuleIdentifier();
1092 }))
1093 return Err;
1094 LLVM_DEBUG(dbgs() << "Module " << BM.getModuleIdentifier() << "\n");
1095
1096 for (const InputFile::Symbol &Sym : Syms) {
1097 assert(!Res.empty());
1098 const SymbolResolution &R = Res.consume_front();
1099
1100 if (!Sym.getIRName().empty()) {
1102 GlobalValue::getGlobalIdentifier(Sym.getIRName(),
1104 if (R.Prevailing) {
1105 assert(ThinLTO.PrevailingModuleForGUID[GUID] ==
1106 BM.getModuleIdentifier());
1107
1108 // For linker redefined symbols (via --wrap or --defsym) we want to
1109 // switch the linkage to `weak` to prevent IPOs from happening.
1110 // Find the summary in the module for this very GV and record the new
1111 // linkage so that we can switch it when we import the GV.
1112 if (R.LinkerRedefined)
1113 if (auto S = ThinLTO.CombinedIndex.findSummaryInModule(
1114 GUID, BM.getModuleIdentifier()))
1115 S->setLinkage(GlobalValue::WeakAnyLinkage);
1116 }
1117
1118 // If the linker resolved the symbol to a local definition then mark it
1119 // as local in the summary for the module we are adding.
1120 if (R.FinalDefinitionInLinkageUnit) {
1121 if (auto S = ThinLTO.CombinedIndex.findSummaryInModule(
1122 GUID, BM.getModuleIdentifier())) {
1123 S->setDSOLocal(true);
1124 }
1125 }
1126 }
1127 }
1128
1129 if (!ThinLTO.ModuleMap.insert({BM.getModuleIdentifier(), BM}).second)
1131 "Expected at most one ThinLTO module per bitcode file",
1133
1134 if (!Conf.ThinLTOModulesToCompile.empty()) {
1135 if (!ThinLTO.ModulesToCompile)
1136 ThinLTO.ModulesToCompile = ModuleMapType();
1137 // This is a fuzzy name matching where only modules with name containing the
1138 // specified switch values are going to be compiled.
1139 for (const std::string &Name : Conf.ThinLTOModulesToCompile) {
1140 if (BM.getModuleIdentifier().contains(Name)) {
1141 ThinLTO.ModulesToCompile->insert({BM.getModuleIdentifier(), BM});
1142 LLVM_DEBUG(dbgs() << "[ThinLTO] Selecting " << BM.getModuleIdentifier()
1143 << " to compile\n");
1144 }
1145 }
1146 }
1147
1148 return Res;
1149}
1150
1151unsigned LTO::getMaxTasks() const {
1152 CalledGetMaxTasks = true;
1153 auto ModuleCount = ThinLTO.ModulesToCompile ? ThinLTO.ModulesToCompile->size()
1154 : ThinLTO.ModuleMap.size();
1155 return RegularLTO.ParallelCodeGenParallelismLevel + ModuleCount;
1156}
1157
1158// If only some of the modules were split, we cannot correctly handle
1159// code that contains type tests or type checked loads.
1160Error LTO::checkPartiallySplit() {
1161 if (!ThinLTO.CombinedIndex.partiallySplitLTOUnits())
1162 return Error::success();
1163
1164 const Module *Combined = RegularLTO.CombinedModule.get();
1165 Function *TypeTestFunc =
1166 Intrinsic::getDeclarationIfExists(Combined, Intrinsic::type_test);
1167 Function *TypeCheckedLoadFunc =
1168 Intrinsic::getDeclarationIfExists(Combined, Intrinsic::type_checked_load);
1169 Function *TypeCheckedLoadRelativeFunc = Intrinsic::getDeclarationIfExists(
1170 Combined, Intrinsic::type_checked_load_relative);
1171
1172 // First check if there are type tests / type checked loads in the
1173 // merged regular LTO module IR.
1174 if ((TypeTestFunc && !TypeTestFunc->use_empty()) ||
1175 (TypeCheckedLoadFunc && !TypeCheckedLoadFunc->use_empty()) ||
1176 (TypeCheckedLoadRelativeFunc &&
1177 !TypeCheckedLoadRelativeFunc->use_empty()))
1179 "inconsistent LTO Unit splitting (recompile with -fsplit-lto-unit)",
1181
1182 // Otherwise check if there are any recorded in the combined summary from the
1183 // ThinLTO modules.
1184 for (auto &P : ThinLTO.CombinedIndex) {
1185 for (auto &S : P.second.SummaryList) {
1186 auto *FS = dyn_cast<FunctionSummary>(S.get());
1187 if (!FS)
1188 continue;
1189 if (!FS->type_test_assume_vcalls().empty() ||
1190 !FS->type_checked_load_vcalls().empty() ||
1191 !FS->type_test_assume_const_vcalls().empty() ||
1192 !FS->type_checked_load_const_vcalls().empty() ||
1193 !FS->type_tests().empty())
1195 "inconsistent LTO Unit splitting (recompile with -fsplit-lto-unit)",
1197 }
1198 }
1199 return Error::success();
1200}
1201
1203 // Compute "dead" symbols, we don't want to import/export these!
1204 DenseSet<GlobalValue::GUID> GUIDPreservedSymbols;
1205 DenseMap<GlobalValue::GUID, PrevailingType> GUIDPrevailingResolutions;
1206 for (auto &Res : *GlobalResolutions) {
1207 // Normally resolution have IR name of symbol. We can do nothing here
1208 // otherwise. See comments in GlobalResolution struct for more details.
1209 if (Res.second.IRName.empty())
1210 continue;
1211
1213 GlobalValue::dropLLVMManglingEscape(Res.second.IRName));
1214
1215 if (Res.second.VisibleOutsideSummary && Res.second.Prevailing)
1216 GUIDPreservedSymbols.insert(GUID);
1217
1218 if (Res.second.ExportDynamic)
1219 DynamicExportSymbols.insert(GUID);
1220
1221 GUIDPrevailingResolutions[GUID] =
1222 Res.second.Prevailing ? PrevailingType::Yes : PrevailingType::No;
1223 }
1224
1225 auto isPrevailing = [&](GlobalValue::GUID G) {
1226 auto It = GUIDPrevailingResolutions.find(G);
1227 if (It == GUIDPrevailingResolutions.end())
1229 return It->second;
1230 };
1231 computeDeadSymbolsWithConstProp(ThinLTO.CombinedIndex, GUIDPreservedSymbols,
1232 isPrevailing, Conf.OptLevel > 0);
1233
1234 // Setup output file to emit statistics.
1235 auto StatsFileOrErr = setupStatsFile(Conf.StatsFile);
1236 if (!StatsFileOrErr)
1237 return StatsFileOrErr.takeError();
1238 std::unique_ptr<ToolOutputFile> StatsFile = std::move(StatsFileOrErr.get());
1239
1240 // TODO: Ideally this would be controlled automatically by detecting that we
1241 // are linking with an allocator that supports these interfaces, rather than
1242 // an internal option (which would still be needed for tests, however). For
1243 // example, if the library exported a symbol like __malloc_hot_cold the linker
1244 // could recognize that and set a flag in the lto::Config.
1246 ThinLTO.CombinedIndex.setWithSupportsHotColdNew();
1247
1248 Error Result = runRegularLTO(AddStream);
1249 if (!Result)
1250 // This will reset the GlobalResolutions optional once done with it to
1251 // reduce peak memory before importing.
1252 Result = runThinLTO(AddStream, Cache, GUIDPreservedSymbols);
1253
1254 if (StatsFile)
1255 PrintStatisticsJSON(StatsFile->os());
1256
1257 return Result;
1258}
1259
1261 const ModuleSummaryIndex &Index) {
1262 llvm::TimeTraceScope timeScope("LTO update memprof attributes");
1263 if (Index.withSupportsHotColdNew())
1264 return;
1265
1266 // The profile matcher applies hotness attributes directly for allocations,
1267 // and those will cause us to generate calls to the hot/cold interfaces
1268 // unconditionally. If supports-hot-cold-new was not enabled in the LTO
1269 // link then assume we don't want these calls (e.g. not linking with
1270 // the appropriate library, or otherwise trying to disable this behavior).
1271 for (auto &F : Mod) {
1272 for (auto &BB : F) {
1273 for (auto &I : BB) {
1274 auto *CI = dyn_cast<CallBase>(&I);
1275 if (!CI)
1276 continue;
1277 if (CI->hasFnAttr("memprof"))
1278 CI->removeFnAttr("memprof");
1279 // Strip off all memprof metadata as it is no longer needed.
1280 // Importantly, this avoids the addition of new memprof attributes
1281 // after inlining propagation.
1282 // TODO: If we support additional types of MemProf metadata beyond hot
1283 // and cold, we will need to update the metadata based on the allocator
1284 // APIs supported instead of completely stripping all.
1285 CI->setMetadata(LLVMContext::MD_memprof, nullptr);
1286 CI->setMetadata(LLVMContext::MD_callsite, nullptr);
1287 }
1288 }
1289 }
1290}
1291
1292Error LTO::runRegularLTO(AddStreamFn AddStream) {
1293 llvm::TimeTraceScope timeScope("Run regular LTO");
1294 LLVMContext &CombinedCtx = RegularLTO.CombinedModule->getContext();
1295 // Setup optimization remarks.
1296 auto DiagFileOrErr = lto::setupLLVMOptimizationRemarks(
1297 CombinedCtx, Conf.RemarksFilename, Conf.RemarksPasses, Conf.RemarksFormat,
1299 LLVM_DEBUG(dbgs() << "Running regular LTO\n");
1300 if (!DiagFileOrErr)
1301 return DiagFileOrErr.takeError();
1302 DiagnosticOutputFile = std::move(*DiagFileOrErr);
1303
1304 // Finalize linking of regular LTO modules containing summaries now that
1305 // we have computed liveness information.
1306 {
1307 llvm::TimeTraceScope timeScope("Link regular LTO");
1308 for (auto &M : RegularLTO.ModsWithSummaries)
1309 if (Error Err = linkRegularLTO(std::move(M), /*LivenessFromIndex=*/true))
1310 return Err;
1311 }
1312
1313 // Ensure we don't have inconsistently split LTO units with type tests.
1314 // FIXME: this checks both LTO and ThinLTO. It happens to work as we take
1315 // this path both cases but eventually this should be split into two and
1316 // do the ThinLTO checks in `runThinLTO`.
1317 if (Error Err = checkPartiallySplit())
1318 return Err;
1319
1320 // Make sure commons have the right size/alignment: we kept the largest from
1321 // all the prevailing when adding the inputs, and we apply it here.
1322 const DataLayout &DL = RegularLTO.CombinedModule->getDataLayout();
1323 for (auto &I : RegularLTO.Commons) {
1324 if (!I.second.Prevailing)
1325 // Don't do anything if no instance of this common was prevailing.
1326 continue;
1327 GlobalVariable *OldGV = RegularLTO.CombinedModule->getNamedGlobal(I.first);
1328 if (OldGV && DL.getTypeAllocSize(OldGV->getValueType()) == I.second.Size) {
1329 // Don't create a new global if the type is already correct, just make
1330 // sure the alignment is correct.
1331 OldGV->setAlignment(I.second.Alignment);
1332 continue;
1333 }
1334 ArrayType *Ty =
1335 ArrayType::get(Type::getInt8Ty(RegularLTO.Ctx), I.second.Size);
1336 auto *GV = new GlobalVariable(*RegularLTO.CombinedModule, Ty, false,
1339 GV->setAlignment(I.second.Alignment);
1340 if (OldGV) {
1341 OldGV->replaceAllUsesWith(GV);
1342 GV->takeName(OldGV);
1343 OldGV->eraseFromParent();
1344 } else {
1345 GV->setName(I.first);
1346 }
1347 }
1348
1349 updateMemProfAttributes(*RegularLTO.CombinedModule, ThinLTO.CombinedIndex);
1350
1351 bool WholeProgramVisibilityEnabledInLTO =
1352 Conf.HasWholeProgramVisibility &&
1353 // If validation is enabled, upgrade visibility only when all vtables
1354 // have typeinfos.
1355 (!Conf.ValidateAllVtablesHaveTypeInfos || Conf.AllVtablesHaveTypeInfos);
1356
1357 // This returns true when the name is local or not defined. Locals are
1358 // expected to be handled separately.
1359 auto IsVisibleToRegularObj = [&](StringRef name) {
1360 auto It = GlobalResolutions->find(name);
1361 return (It == GlobalResolutions->end() ||
1362 It->second.VisibleOutsideSummary || !It->second.Prevailing);
1363 };
1364
1365 // If allowed, upgrade public vcall visibility metadata to linkage unit
1366 // visibility before whole program devirtualization in the optimizer.
1368 *RegularLTO.CombinedModule, WholeProgramVisibilityEnabledInLTO,
1369 DynamicExportSymbols, Conf.ValidateAllVtablesHaveTypeInfos,
1370 IsVisibleToRegularObj);
1371 updatePublicTypeTestCalls(*RegularLTO.CombinedModule,
1372 WholeProgramVisibilityEnabledInLTO);
1373
1374 if (Conf.PreOptModuleHook &&
1375 !Conf.PreOptModuleHook(0, *RegularLTO.CombinedModule))
1376 return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
1377
1378 if (!Conf.CodeGenOnly) {
1379 for (const auto &R : *GlobalResolutions) {
1380 GlobalValue *GV =
1381 RegularLTO.CombinedModule->getNamedValue(R.second.IRName);
1382 if (!R.second.isPrevailingIRSymbol())
1383 continue;
1384 if (R.second.Partition != 0 &&
1385 R.second.Partition != GlobalResolution::External)
1386 continue;
1387
1388 // Ignore symbols defined in other partitions.
1389 // Also skip declarations, which are not allowed to have internal linkage.
1390 if (!GV || GV->hasLocalLinkage() || GV->isDeclaration())
1391 continue;
1392
1393 // Symbols that are marked DLLImport or DLLExport should not be
1394 // internalized, as they are either externally visible or referencing
1395 // external symbols. Symbols that have AvailableExternally or Appending
1396 // linkage might be used by future passes and should be kept as is.
1397 // These linkages are seen in Unified regular LTO, because the process
1398 // of creating split LTO units introduces symbols with that linkage into
1399 // one of the created modules. Normally, only the ThinLTO backend would
1400 // compile this module, but Unified Regular LTO processes both
1401 // modules created by the splitting process as regular LTO modules.
1402 if ((LTOMode == LTOKind::LTOK_UnifiedRegular) &&
1405 continue;
1406
1407 GV->setUnnamedAddr(R.second.UnnamedAddr ? GlobalValue::UnnamedAddr::Global
1409 if (EnableLTOInternalization && R.second.Partition == 0)
1411 }
1412
1413 if (Conf.PostInternalizeModuleHook &&
1414 !Conf.PostInternalizeModuleHook(0, *RegularLTO.CombinedModule))
1415 return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
1416 }
1417
1418 if (!RegularLTO.EmptyCombinedModule || Conf.AlwaysEmitRegularLTOObj) {
1419 if (Error Err =
1420 backend(Conf, AddStream, RegularLTO.ParallelCodeGenParallelismLevel,
1421 *RegularLTO.CombinedModule, ThinLTO.CombinedIndex))
1422 return Err;
1423 }
1424
1425 return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
1426}
1427
1429 RTLIB::RuntimeLibcallsInfo Libcalls(TT);
1430 SmallVector<const char *> LibcallSymbols;
1431 ArrayRef<RTLIB::LibcallImpl> LibcallImpls = Libcalls.getLibcallImpls();
1432 LibcallSymbols.reserve(LibcallImpls.size());
1433
1434 for (RTLIB::LibcallImpl Impl : LibcallImpls) {
1435 if (Impl != RTLIB::Unsupported)
1436 LibcallSymbols.push_back(Libcalls.getLibcallImplName(Impl).data());
1437 }
1438
1439 return LibcallSymbols;
1440}
1441
1443 const FunctionImporter::ImportMapTy &ImportList, llvm::StringRef ModulePath,
1444 const std::string &NewModulePath) const {
1445 return emitFiles(ImportList, ModulePath, NewModulePath,
1446 NewModulePath + ".thinlto.bc",
1447 /*ImportsFiles=*/std::nullopt);
1448}
1449
1451 const FunctionImporter::ImportMapTy &ImportList, llvm::StringRef ModulePath,
1452 const std::string &NewModulePath, StringRef SummaryPath,
1453 std::optional<std::reference_wrapper<ImportsFilesContainer>> ImportsFiles)
1454 const {
1455 ModuleToSummariesForIndexTy ModuleToSummariesForIndex;
1456 GVSummaryPtrSet DeclarationSummaries;
1457
1458 std::error_code EC;
1460 ImportList, ModuleToSummariesForIndex,
1461 DeclarationSummaries);
1462
1463 raw_fd_ostream OS(SummaryPath, EC, sys::fs::OpenFlags::OF_None);
1464 if (EC)
1465 return createFileError("cannot open " + Twine(SummaryPath), EC);
1466
1467 writeIndexToFile(CombinedIndex, OS, &ModuleToSummariesForIndex,
1468 &DeclarationSummaries);
1469
1471 Error ImportsFilesError = EmitImportsFiles(
1472 ModulePath, NewModulePath + ".imports", ModuleToSummariesForIndex);
1473 if (ImportsFilesError)
1474 return ImportsFilesError;
1475 }
1476
1477 // Optionally, store the imports files.
1478 if (ImportsFiles)
1480 ModulePath, ModuleToSummariesForIndex,
1481 [&](StringRef M) { ImportsFiles->get().push_back(M.str()); });
1482
1483 return Error::success();
1484}
1485
1486namespace {
1487/// Base class for ThinLTO backends that perform code generation and insert the
1488/// generated files back into the link.
1489class CGThinBackend : public ThinBackendProc {
1490protected:
1491 AddStreamFn AddStream;
1492 DenseSet<GlobalValue::GUID> CfiFunctionDefs;
1493 DenseSet<GlobalValue::GUID> CfiFunctionDecls;
1494 bool ShouldEmitIndexFiles;
1495
1496public:
1497 CGThinBackend(
1498 const Config &Conf, ModuleSummaryIndex &CombinedIndex,
1499 const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1500 AddStreamFn AddStream, lto::IndexWriteCallback OnWrite,
1501 bool ShouldEmitIndexFiles, bool ShouldEmitImportsFiles,
1502 ThreadPoolStrategy ThinLTOParallelism)
1503 : ThinBackendProc(Conf, CombinedIndex, ModuleToDefinedGVSummaries,
1504 OnWrite, ShouldEmitImportsFiles, ThinLTOParallelism),
1505 AddStream(std::move(AddStream)),
1506 ShouldEmitIndexFiles(ShouldEmitIndexFiles) {
1507 auto &Defs = CombinedIndex.cfiFunctionDefs();
1508 CfiFunctionDefs.insert_range(Defs.guids());
1509 auto &Decls = CombinedIndex.cfiFunctionDecls();
1510 CfiFunctionDecls.insert_range(Decls.guids());
1511 }
1512};
1513
1514/// This backend performs code generation by scheduling a job to run on
1515/// an in-process thread when invoked for each task.
1516class InProcessThinBackend : public CGThinBackend {
1517protected:
1518 FileCache Cache;
1519
1520public:
1521 InProcessThinBackend(
1522 const Config &Conf, ModuleSummaryIndex &CombinedIndex,
1523 ThreadPoolStrategy ThinLTOParallelism,
1524 const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1525 AddStreamFn AddStream, FileCache Cache, lto::IndexWriteCallback OnWrite,
1526 bool ShouldEmitIndexFiles, bool ShouldEmitImportsFiles)
1527 : CGThinBackend(Conf, CombinedIndex, ModuleToDefinedGVSummaries,
1528 AddStream, OnWrite, ShouldEmitIndexFiles,
1529 ShouldEmitImportsFiles, ThinLTOParallelism),
1530 Cache(std::move(Cache)) {}
1531
1532 virtual Error runThinLTOBackendThread(
1533 AddStreamFn AddStream, FileCache Cache, unsigned Task, BitcodeModule BM,
1534 ModuleSummaryIndex &CombinedIndex,
1535 const FunctionImporter::ImportMapTy &ImportList,
1536 const FunctionImporter::ExportSetTy &ExportList,
1537 const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
1538 const GVSummaryMapTy &DefinedGlobals,
1539 MapVector<StringRef, BitcodeModule> &ModuleMap) {
1540 auto ModuleID = BM.getModuleIdentifier();
1541 llvm::TimeTraceScope timeScope("Run ThinLTO backend thread (in-process)",
1542 ModuleID);
1543 auto RunThinBackend = [&](AddStreamFn AddStream) {
1544 LTOLLVMContext BackendContext(Conf);
1545 Expected<std::unique_ptr<Module>> MOrErr = BM.parseModule(BackendContext);
1546 if (!MOrErr)
1547 return MOrErr.takeError();
1548
1549 return thinBackend(Conf, Task, AddStream, **MOrErr, CombinedIndex,
1550 ImportList, DefinedGlobals, &ModuleMap,
1551 Conf.CodeGenOnly);
1552 };
1553 if (ShouldEmitIndexFiles) {
1554 if (auto E = emitFiles(ImportList, ModuleID, ModuleID.str()))
1555 return E;
1556 }
1557
1558 if (!Cache.isValid() || !CombinedIndex.modulePaths().count(ModuleID) ||
1559 all_of(CombinedIndex.getModuleHash(ModuleID),
1560 [](uint32_t V) { return V == 0; }))
1561 // Cache disabled or no entry for this module in the combined index or
1562 // no module hash.
1563 return RunThinBackend(AddStream);
1564
1565 // The module may be cached, this helps handling it.
1566 std::string Key = computeLTOCacheKey(
1567 Conf, CombinedIndex, ModuleID, ImportList, ExportList, ResolvedODR,
1568 DefinedGlobals, CfiFunctionDefs, CfiFunctionDecls);
1569 Expected<AddStreamFn> CacheAddStreamOrErr = Cache(Task, Key, ModuleID);
1570 if (Error Err = CacheAddStreamOrErr.takeError())
1571 return Err;
1572 AddStreamFn &CacheAddStream = *CacheAddStreamOrErr;
1573 if (CacheAddStream)
1574 return RunThinBackend(CacheAddStream);
1575
1576 return Error::success();
1577 }
1578
1579 Error start(
1580 unsigned Task, BitcodeModule BM,
1581 const FunctionImporter::ImportMapTy &ImportList,
1582 const FunctionImporter::ExportSetTy &ExportList,
1583 const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
1584 MapVector<StringRef, BitcodeModule> &ModuleMap) override {
1585 StringRef ModulePath = BM.getModuleIdentifier();
1586 assert(ModuleToDefinedGVSummaries.count(ModulePath));
1587 const GVSummaryMapTy &DefinedGlobals =
1588 ModuleToDefinedGVSummaries.find(ModulePath)->second;
1589 BackendThreadPool.async(
1590 [=](BitcodeModule BM, ModuleSummaryIndex &CombinedIndex,
1591 const FunctionImporter::ImportMapTy &ImportList,
1592 const FunctionImporter::ExportSetTy &ExportList,
1593 const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>
1594 &ResolvedODR,
1595 const GVSummaryMapTy &DefinedGlobals,
1596 MapVector<StringRef, BitcodeModule> &ModuleMap) {
1597 if (LLVM_ENABLE_THREADS && Conf.TimeTraceEnabled)
1599 "thin backend");
1600 Error E = runThinLTOBackendThread(
1601 AddStream, Cache, Task, BM, CombinedIndex, ImportList, ExportList,
1602 ResolvedODR, DefinedGlobals, ModuleMap);
1603 if (E) {
1604 std::unique_lock<std::mutex> L(ErrMu);
1605 if (Err)
1606 Err = joinErrors(std::move(*Err), std::move(E));
1607 else
1608 Err = std::move(E);
1609 }
1610 if (LLVM_ENABLE_THREADS && Conf.TimeTraceEnabled)
1612 },
1613 BM, std::ref(CombinedIndex), std::ref(ImportList), std::ref(ExportList),
1614 std::ref(ResolvedODR), std::ref(DefinedGlobals), std::ref(ModuleMap));
1615
1616 if (OnWrite)
1617 OnWrite(std::string(ModulePath));
1618 return Error::success();
1619 }
1620};
1621
1622/// This backend is utilized in the first round of a two-codegen round process.
1623/// It first saves optimized bitcode files to disk before the codegen process
1624/// begins. After codegen, it stores the resulting object files in a scratch
1625/// buffer. Note the codegen data stored in the scratch buffer will be extracted
1626/// and merged in the subsequent step.
1627class FirstRoundThinBackend : public InProcessThinBackend {
1628 AddStreamFn IRAddStream;
1629 FileCache IRCache;
1630
1631public:
1632 FirstRoundThinBackend(
1633 const Config &Conf, ModuleSummaryIndex &CombinedIndex,
1634 ThreadPoolStrategy ThinLTOParallelism,
1635 const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1636 AddStreamFn CGAddStream, FileCache CGCache, AddStreamFn IRAddStream,
1637 FileCache IRCache)
1638 : InProcessThinBackend(Conf, CombinedIndex, ThinLTOParallelism,
1639 ModuleToDefinedGVSummaries, std::move(CGAddStream),
1640 std::move(CGCache), /*OnWrite=*/nullptr,
1641 /*ShouldEmitIndexFiles=*/false,
1642 /*ShouldEmitImportsFiles=*/false),
1643 IRAddStream(std::move(IRAddStream)), IRCache(std::move(IRCache)) {}
1644
1645 Error runThinLTOBackendThread(
1646 AddStreamFn CGAddStream, FileCache CGCache, unsigned Task,
1647 BitcodeModule BM, ModuleSummaryIndex &CombinedIndex,
1648 const FunctionImporter::ImportMapTy &ImportList,
1649 const FunctionImporter::ExportSetTy &ExportList,
1650 const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
1651 const GVSummaryMapTy &DefinedGlobals,
1652 MapVector<StringRef, BitcodeModule> &ModuleMap) override {
1653 auto ModuleID = BM.getModuleIdentifier();
1654 llvm::TimeTraceScope timeScope("Run ThinLTO backend thread (first round)",
1655 ModuleID);
1656 auto RunThinBackend = [&](AddStreamFn CGAddStream,
1657 AddStreamFn IRAddStream) {
1658 LTOLLVMContext BackendContext(Conf);
1659 Expected<std::unique_ptr<Module>> MOrErr = BM.parseModule(BackendContext);
1660 if (!MOrErr)
1661 return MOrErr.takeError();
1662
1663 return thinBackend(Conf, Task, CGAddStream, **MOrErr, CombinedIndex,
1664 ImportList, DefinedGlobals, &ModuleMap,
1665 Conf.CodeGenOnly, IRAddStream);
1666 };
1667 // Like InProcessThinBackend, we produce index files as needed for
1668 // FirstRoundThinBackend. However, these files are not generated for
1669 // SecondRoundThinBackend.
1670 if (ShouldEmitIndexFiles) {
1671 if (auto E = emitFiles(ImportList, ModuleID, ModuleID.str()))
1672 return E;
1673 }
1674
1675 assert((CGCache.isValid() == IRCache.isValid()) &&
1676 "Both caches for CG and IR should have matching availability");
1677 if (!CGCache.isValid() || !CombinedIndex.modulePaths().count(ModuleID) ||
1678 all_of(CombinedIndex.getModuleHash(ModuleID),
1679 [](uint32_t V) { return V == 0; }))
1680 // Cache disabled or no entry for this module in the combined index or
1681 // no module hash.
1682 return RunThinBackend(CGAddStream, IRAddStream);
1683
1684 // Get CGKey for caching object in CGCache.
1685 std::string CGKey = computeLTOCacheKey(
1686 Conf, CombinedIndex, ModuleID, ImportList, ExportList, ResolvedODR,
1687 DefinedGlobals, CfiFunctionDefs, CfiFunctionDecls);
1688 Expected<AddStreamFn> CacheCGAddStreamOrErr =
1689 CGCache(Task, CGKey, ModuleID);
1690 if (Error Err = CacheCGAddStreamOrErr.takeError())
1691 return Err;
1692 AddStreamFn &CacheCGAddStream = *CacheCGAddStreamOrErr;
1693
1694 // Get IRKey for caching (optimized) IR in IRCache with an extra ID.
1695 std::string IRKey = recomputeLTOCacheKey(CGKey, /*ExtraID=*/"IR");
1696 Expected<AddStreamFn> CacheIRAddStreamOrErr =
1697 IRCache(Task, IRKey, ModuleID);
1698 if (Error Err = CacheIRAddStreamOrErr.takeError())
1699 return Err;
1700 AddStreamFn &CacheIRAddStream = *CacheIRAddStreamOrErr;
1701
1702 // Ideally, both CG and IR caching should be synchronized. However, in
1703 // practice, their availability may differ due to different expiration
1704 // times. Therefore, if either cache is missing, the backend process is
1705 // triggered.
1706 if (CacheCGAddStream || CacheIRAddStream) {
1707 LLVM_DEBUG(dbgs() << "[FirstRound] Cache Miss for "
1708 << BM.getModuleIdentifier() << "\n");
1709 return RunThinBackend(CacheCGAddStream ? CacheCGAddStream : CGAddStream,
1710 CacheIRAddStream ? CacheIRAddStream : IRAddStream);
1711 }
1712
1713 return Error::success();
1714 }
1715};
1716
1717/// This backend operates in the second round of a two-codegen round process.
1718/// It starts by reading the optimized bitcode files that were saved during the
1719/// first round. The backend then executes the codegen only to further optimize
1720/// the code, utilizing the codegen data merged from the first round. Finally,
1721/// it writes the resulting object files as usual.
1722class SecondRoundThinBackend : public InProcessThinBackend {
1723 std::unique_ptr<SmallVector<StringRef>> IRFiles;
1724 stable_hash CombinedCGDataHash;
1725
1726public:
1727 SecondRoundThinBackend(
1728 const Config &Conf, ModuleSummaryIndex &CombinedIndex,
1729 ThreadPoolStrategy ThinLTOParallelism,
1730 const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1731 AddStreamFn AddStream, FileCache Cache,
1732 std::unique_ptr<SmallVector<StringRef>> IRFiles,
1733 stable_hash CombinedCGDataHash)
1734 : InProcessThinBackend(Conf, CombinedIndex, ThinLTOParallelism,
1735 ModuleToDefinedGVSummaries, std::move(AddStream),
1736 std::move(Cache),
1737 /*OnWrite=*/nullptr,
1738 /*ShouldEmitIndexFiles=*/false,
1739 /*ShouldEmitImportsFiles=*/false),
1740 IRFiles(std::move(IRFiles)), CombinedCGDataHash(CombinedCGDataHash) {}
1741
1742 virtual Error runThinLTOBackendThread(
1743 AddStreamFn AddStream, FileCache Cache, unsigned Task, BitcodeModule BM,
1744 ModuleSummaryIndex &CombinedIndex,
1745 const FunctionImporter::ImportMapTy &ImportList,
1746 const FunctionImporter::ExportSetTy &ExportList,
1747 const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
1748 const GVSummaryMapTy &DefinedGlobals,
1749 MapVector<StringRef, BitcodeModule> &ModuleMap) override {
1750 auto ModuleID = BM.getModuleIdentifier();
1751 llvm::TimeTraceScope timeScope("Run ThinLTO backend thread (second round)",
1752 ModuleID);
1753 auto RunThinBackend = [&](AddStreamFn AddStream) {
1754 LTOLLVMContext BackendContext(Conf);
1755 std::unique_ptr<Module> LoadedModule =
1756 cgdata::loadModuleForTwoRounds(BM, Task, BackendContext, *IRFiles);
1757
1758 return thinBackend(Conf, Task, AddStream, *LoadedModule, CombinedIndex,
1759 ImportList, DefinedGlobals, &ModuleMap,
1760 /*CodeGenOnly=*/true);
1761 };
1762 if (!Cache.isValid() || !CombinedIndex.modulePaths().count(ModuleID) ||
1763 all_of(CombinedIndex.getModuleHash(ModuleID),
1764 [](uint32_t V) { return V == 0; }))
1765 // Cache disabled or no entry for this module in the combined index or
1766 // no module hash.
1767 return RunThinBackend(AddStream);
1768
1769 // Get Key for caching the final object file in Cache with the combined
1770 // CGData hash.
1771 std::string Key = computeLTOCacheKey(
1772 Conf, CombinedIndex, ModuleID, ImportList, ExportList, ResolvedODR,
1773 DefinedGlobals, CfiFunctionDefs, CfiFunctionDecls);
1775 /*ExtraID=*/std::to_string(CombinedCGDataHash));
1776 Expected<AddStreamFn> CacheAddStreamOrErr = Cache(Task, Key, ModuleID);
1777 if (Error Err = CacheAddStreamOrErr.takeError())
1778 return Err;
1779 AddStreamFn &CacheAddStream = *CacheAddStreamOrErr;
1780
1781 if (CacheAddStream) {
1782 LLVM_DEBUG(dbgs() << "[SecondRound] Cache Miss for "
1783 << BM.getModuleIdentifier() << "\n");
1784 return RunThinBackend(CacheAddStream);
1785 }
1786
1787 return Error::success();
1788 }
1789};
1790} // end anonymous namespace
1791
1794 bool ShouldEmitIndexFiles,
1795 bool ShouldEmitImportsFiles) {
1796 auto Func =
1797 [=](const Config &Conf, ModuleSummaryIndex &CombinedIndex,
1798 const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1799 AddStreamFn AddStream, FileCache Cache) {
1800 return std::make_unique<InProcessThinBackend>(
1801 Conf, CombinedIndex, Parallelism, ModuleToDefinedGVSummaries,
1802 AddStream, Cache, OnWrite, ShouldEmitIndexFiles,
1803 ShouldEmitImportsFiles);
1804 };
1805 return ThinBackend(Func, Parallelism);
1806}
1807
1809 if (!TheTriple.isOSDarwin())
1810 return "";
1811 if (TheTriple.getArch() == Triple::x86_64)
1812 return "core2";
1813 if (TheTriple.getArch() == Triple::x86)
1814 return "yonah";
1815 if (TheTriple.isArm64e())
1816 return "apple-a12";
1817 if (TheTriple.getArch() == Triple::aarch64 ||
1818 TheTriple.getArch() == Triple::aarch64_32)
1819 return "cyclone";
1820 return "";
1821}
1822
1823// Given the original \p Path to an output file, replace any path
1824// prefix matching \p OldPrefix with \p NewPrefix. Also, create the
1825// resulting directory if it does not yet exist.
1827 StringRef NewPrefix) {
1828 if (OldPrefix.empty() && NewPrefix.empty())
1829 return std::string(Path);
1830 SmallString<128> NewPath(Path);
1831 llvm::sys::path::replace_path_prefix(NewPath, OldPrefix, NewPrefix);
1832 StringRef ParentPath = llvm::sys::path::parent_path(NewPath.str());
1833 if (!ParentPath.empty()) {
1834 // Make sure the new directory exists, creating it if necessary.
1835 if (std::error_code EC = llvm::sys::fs::create_directories(ParentPath))
1836 llvm::errs() << "warning: could not create directory '" << ParentPath
1837 << "': " << EC.message() << '\n';
1838 }
1839 return std::string(NewPath);
1840}
1841
1842namespace {
1843class WriteIndexesThinBackend : public ThinBackendProc {
1844 std::string OldPrefix, NewPrefix, NativeObjectPrefix;
1845 raw_fd_ostream *LinkedObjectsFile;
1846
1847public:
1848 WriteIndexesThinBackend(
1849 const Config &Conf, ModuleSummaryIndex &CombinedIndex,
1850 ThreadPoolStrategy ThinLTOParallelism,
1851 const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1852 std::string OldPrefix, std::string NewPrefix,
1853 std::string NativeObjectPrefix, bool ShouldEmitImportsFiles,
1854 raw_fd_ostream *LinkedObjectsFile, lto::IndexWriteCallback OnWrite)
1855 : ThinBackendProc(Conf, CombinedIndex, ModuleToDefinedGVSummaries,
1856 OnWrite, ShouldEmitImportsFiles, ThinLTOParallelism),
1857 OldPrefix(OldPrefix), NewPrefix(NewPrefix),
1858 NativeObjectPrefix(NativeObjectPrefix),
1859 LinkedObjectsFile(LinkedObjectsFile) {}
1860
1861 Error start(
1862 unsigned Task, BitcodeModule BM,
1863 const FunctionImporter::ImportMapTy &ImportList,
1864 const FunctionImporter::ExportSetTy &ExportList,
1865 const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
1866 MapVector<StringRef, BitcodeModule> &ModuleMap) override {
1867 StringRef ModulePath = BM.getModuleIdentifier();
1868
1869 // The contents of this file may be used as input to a native link, and must
1870 // therefore contain the processed modules in a determinstic order that
1871 // match the order they are provided on the command line. For that reason,
1872 // we cannot include this in the asynchronously executed lambda below.
1873 if (LinkedObjectsFile) {
1874 std::string ObjectPrefix =
1875 NativeObjectPrefix.empty() ? NewPrefix : NativeObjectPrefix;
1876 std::string LinkedObjectsFilePath =
1877 getThinLTOOutputFile(ModulePath, OldPrefix, ObjectPrefix);
1878 *LinkedObjectsFile << LinkedObjectsFilePath << '\n';
1879 }
1880
1881 BackendThreadPool.async(
1882 [this](const StringRef ModulePath,
1883 const FunctionImporter::ImportMapTy &ImportList,
1884 const std::string &OldPrefix, const std::string &NewPrefix) {
1885 std::string NewModulePath =
1886 getThinLTOOutputFile(ModulePath, OldPrefix, NewPrefix);
1887 auto E = emitFiles(ImportList, ModulePath, NewModulePath);
1888 if (E) {
1889 std::unique_lock<std::mutex> L(ErrMu);
1890 if (Err)
1891 Err = joinErrors(std::move(*Err), std::move(E));
1892 else
1893 Err = std::move(E);
1894 return;
1895 }
1896 },
1897 ModulePath, ImportList, OldPrefix, NewPrefix);
1898
1899 if (OnWrite)
1900 OnWrite(std::string(ModulePath));
1901 return Error::success();
1902 }
1903
1904 bool isSensitiveToInputOrder() override {
1905 // The order which modules are written to LinkedObjectsFile should be
1906 // deterministic and match the order they are passed on the command line.
1907 return true;
1908 }
1909};
1910} // end anonymous namespace
1911
1913 ThreadPoolStrategy Parallelism, std::string OldPrefix,
1914 std::string NewPrefix, std::string NativeObjectPrefix,
1915 bool ShouldEmitImportsFiles, raw_fd_ostream *LinkedObjectsFile,
1916 IndexWriteCallback OnWrite) {
1917 auto Func =
1918 [=](const Config &Conf, ModuleSummaryIndex &CombinedIndex,
1919 const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1920 AddStreamFn AddStream, FileCache Cache) {
1921 return std::make_unique<WriteIndexesThinBackend>(
1922 Conf, CombinedIndex, Parallelism, ModuleToDefinedGVSummaries,
1923 OldPrefix, NewPrefix, NativeObjectPrefix, ShouldEmitImportsFiles,
1924 LinkedObjectsFile, OnWrite);
1925 };
1926 return ThinBackend(Func, Parallelism);
1927}
1928
1929Error LTO::runThinLTO(AddStreamFn AddStream, FileCache Cache,
1930 const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols) {
1931 llvm::TimeTraceScope timeScope("Run ThinLTO");
1932 LLVM_DEBUG(dbgs() << "Running ThinLTO\n");
1933 ThinLTO.CombinedIndex.releaseTemporaryMemory();
1934 timeTraceProfilerBegin("ThinLink", StringRef(""));
1935 auto TimeTraceScopeExit = llvm::make_scope_exit([]() {
1938 });
1939 if (ThinLTO.ModuleMap.empty())
1940 return Error::success();
1941
1942 if (ThinLTO.ModulesToCompile && ThinLTO.ModulesToCompile->empty()) {
1943 llvm::errs() << "warning: [ThinLTO] No module compiled\n";
1944 return Error::success();
1945 }
1946
1947 if (Conf.CombinedIndexHook &&
1948 !Conf.CombinedIndexHook(ThinLTO.CombinedIndex, GUIDPreservedSymbols))
1949 return Error::success();
1950
1951 // Collect for each module the list of function it defines (GUID ->
1952 // Summary).
1953 DenseMap<StringRef, GVSummaryMapTy> ModuleToDefinedGVSummaries(
1954 ThinLTO.ModuleMap.size());
1955 ThinLTO.CombinedIndex.collectDefinedGVSummariesPerModule(
1956 ModuleToDefinedGVSummaries);
1957 // Create entries for any modules that didn't have any GV summaries
1958 // (either they didn't have any GVs to start with, or we suppressed
1959 // generation of the summaries because they e.g. had inline assembly
1960 // uses that couldn't be promoted/renamed on export). This is so
1961 // InProcessThinBackend::start can still launch a backend thread, which
1962 // is passed the map of summaries for the module, without any special
1963 // handling for this case.
1964 for (auto &Mod : ThinLTO.ModuleMap)
1965 if (!ModuleToDefinedGVSummaries.count(Mod.first))
1966 ModuleToDefinedGVSummaries.try_emplace(Mod.first);
1967
1968 FunctionImporter::ImportListsTy ImportLists(ThinLTO.ModuleMap.size());
1969 DenseMap<StringRef, FunctionImporter::ExportSetTy> ExportLists(
1970 ThinLTO.ModuleMap.size());
1971 StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>> ResolvedODR;
1972
1973 if (DumpThinCGSCCs)
1974 ThinLTO.CombinedIndex.dumpSCCs(outs());
1975
1976 std::set<GlobalValue::GUID> ExportedGUIDs;
1977
1978 bool WholeProgramVisibilityEnabledInLTO =
1979 Conf.HasWholeProgramVisibility &&
1980 // If validation is enabled, upgrade visibility only when all vtables
1981 // have typeinfos.
1982 (!Conf.ValidateAllVtablesHaveTypeInfos || Conf.AllVtablesHaveTypeInfos);
1983 if (hasWholeProgramVisibility(WholeProgramVisibilityEnabledInLTO))
1984 ThinLTO.CombinedIndex.setWithWholeProgramVisibility();
1985
1986 // If we're validating, get the vtable symbols that should not be
1987 // upgraded because they correspond to typeIDs outside of index-based
1988 // WPD info.
1989 DenseSet<GlobalValue::GUID> VisibleToRegularObjSymbols;
1990 if (WholeProgramVisibilityEnabledInLTO &&
1991 Conf.ValidateAllVtablesHaveTypeInfos) {
1992 // This returns true when the name is local or not defined. Locals are
1993 // expected to be handled separately.
1994 auto IsVisibleToRegularObj = [&](StringRef name) {
1995 auto It = GlobalResolutions->find(name);
1996 return (It == GlobalResolutions->end() ||
1997 It->second.VisibleOutsideSummary || !It->second.Prevailing);
1998 };
1999
2000 getVisibleToRegularObjVtableGUIDs(ThinLTO.CombinedIndex,
2001 VisibleToRegularObjSymbols,
2002 IsVisibleToRegularObj);
2003 }
2004
2005 // If allowed, upgrade public vcall visibility to linkage unit visibility in
2006 // the summaries before whole program devirtualization below.
2008 ThinLTO.CombinedIndex, WholeProgramVisibilityEnabledInLTO,
2009 DynamicExportSymbols, VisibleToRegularObjSymbols);
2010
2011 // Perform index-based WPD. This will return immediately if there are
2012 // no index entries in the typeIdMetadata map (e.g. if we are instead
2013 // performing IR-based WPD in hybrid regular/thin LTO mode).
2014 std::map<ValueInfo, std::vector<VTableSlotSummary>> LocalWPDTargetsMap;
2015 runWholeProgramDevirtOnIndex(ThinLTO.CombinedIndex, ExportedGUIDs,
2016 LocalWPDTargetsMap);
2017
2018 auto isPrevailing = [&](GlobalValue::GUID GUID, const GlobalValueSummary *S) {
2019 return ThinLTO.PrevailingModuleForGUID[GUID] == S->modulePath();
2020 };
2022 MemProfContextDisambiguation ContextDisambiguation;
2023 ContextDisambiguation.run(ThinLTO.CombinedIndex, isPrevailing);
2024 }
2025
2026 // Figure out which symbols need to be internalized. This also needs to happen
2027 // at -O0 because summary-based DCE is implemented using internalization, and
2028 // we must apply DCE consistently with the full LTO module in order to avoid
2029 // undefined references during the final link.
2030 for (auto &Res : *GlobalResolutions) {
2031 // If the symbol does not have external references or it is not prevailing,
2032 // then not need to mark it as exported from a ThinLTO partition.
2033 if (Res.second.Partition != GlobalResolution::External ||
2034 !Res.second.isPrevailingIRSymbol())
2035 continue;
2037 GlobalValue::dropLLVMManglingEscape(Res.second.IRName));
2038 // Mark exported unless index-based analysis determined it to be dead.
2039 if (ThinLTO.CombinedIndex.isGUIDLive(GUID))
2040 ExportedGUIDs.insert(GUID);
2041 }
2042
2043 // Reset the GlobalResolutions to deallocate the associated memory, as there
2044 // are no further accesses. We specifically want to do this before computing
2045 // cross module importing, which adds to peak memory via the computed import
2046 // and export lists.
2047 releaseGlobalResolutionsMemory();
2048
2049 if (Conf.OptLevel > 0)
2050 ComputeCrossModuleImport(ThinLTO.CombinedIndex, ModuleToDefinedGVSummaries,
2051 isPrevailing, ImportLists, ExportLists);
2052
2053 // Any functions referenced by the jump table in the regular LTO object must
2054 // be exported.
2055 auto &Defs = ThinLTO.CombinedIndex.cfiFunctionDefs();
2056 ExportedGUIDs.insert(Defs.guid_begin(), Defs.guid_end());
2057 auto &Decls = ThinLTO.CombinedIndex.cfiFunctionDecls();
2058 ExportedGUIDs.insert(Decls.guid_begin(), Decls.guid_end());
2059
2060 auto isExported = [&](StringRef ModuleIdentifier, ValueInfo VI) {
2061 const auto &ExportList = ExportLists.find(ModuleIdentifier);
2062 return (ExportList != ExportLists.end() && ExportList->second.count(VI)) ||
2063 ExportedGUIDs.count(VI.getGUID());
2064 };
2065
2066 // Update local devirtualized targets that were exported by cross-module
2067 // importing or by other devirtualizations marked in the ExportedGUIDs set.
2068 updateIndexWPDForExports(ThinLTO.CombinedIndex, isExported,
2069 LocalWPDTargetsMap);
2070
2071 thinLTOInternalizeAndPromoteInIndex(ThinLTO.CombinedIndex, isExported,
2072 isPrevailing);
2073
2074 auto recordNewLinkage = [&](StringRef ModuleIdentifier,
2076 GlobalValue::LinkageTypes NewLinkage) {
2077 ResolvedODR[ModuleIdentifier][GUID] = NewLinkage;
2078 };
2079 thinLTOResolvePrevailingInIndex(Conf, ThinLTO.CombinedIndex, isPrevailing,
2080 recordNewLinkage, GUIDPreservedSymbols);
2081
2082 thinLTOPropagateFunctionAttrs(ThinLTO.CombinedIndex, isPrevailing);
2083
2084 generateParamAccessSummary(ThinLTO.CombinedIndex);
2085
2088
2089 TimeTraceScopeExit.release();
2090
2091 auto &ModuleMap =
2092 ThinLTO.ModulesToCompile ? *ThinLTO.ModulesToCompile : ThinLTO.ModuleMap;
2093
2094 auto RunBackends = [&](ThinBackendProc *BackendProcess) -> Error {
2095 auto ProcessOneModule = [&](int I) -> Error {
2096 auto &Mod = *(ModuleMap.begin() + I);
2097 // Tasks 0 through ParallelCodeGenParallelismLevel-1 are reserved for
2098 // combined module and parallel code generation partitions.
2099 return BackendProcess->start(
2100 RegularLTO.ParallelCodeGenParallelismLevel + I, Mod.second,
2101 ImportLists[Mod.first], ExportLists[Mod.first],
2102 ResolvedODR[Mod.first], ThinLTO.ModuleMap);
2103 };
2104
2105 BackendProcess->setup(ModuleMap.size(),
2106 RegularLTO.ParallelCodeGenParallelismLevel,
2107 RegularLTO.CombinedModule->getTargetTriple());
2108
2109 if (BackendProcess->getThreadCount() == 1 ||
2110 BackendProcess->isSensitiveToInputOrder()) {
2111 // Process the modules in the order they were provided on the
2112 // command-line. It is important for this codepath to be used for
2113 // WriteIndexesThinBackend, to ensure the emitted LinkedObjectsFile lists
2114 // ThinLTO objects in the same order as the inputs, which otherwise would
2115 // affect the final link order.
2116 for (int I = 0, E = ModuleMap.size(); I != E; ++I)
2117 if (Error E = ProcessOneModule(I))
2118 return E;
2119 } else {
2120 // When executing in parallel, process largest bitsize modules first to
2121 // improve parallelism, and avoid starving the thread pool near the end.
2122 // This saves about 15 sec on a 36-core machine while link `clang.exe`
2123 // (out of 100 sec).
2124 std::vector<BitcodeModule *> ModulesVec;
2125 ModulesVec.reserve(ModuleMap.size());
2126 for (auto &Mod : ModuleMap)
2127 ModulesVec.push_back(&Mod.second);
2128 for (int I : generateModulesOrdering(ModulesVec))
2129 if (Error E = ProcessOneModule(I))
2130 return E;
2131 }
2132 return BackendProcess->wait();
2133 };
2134
2136 std::unique_ptr<ThinBackendProc> BackendProc =
2137 ThinLTO.Backend(Conf, ThinLTO.CombinedIndex, ModuleToDefinedGVSummaries,
2138 AddStream, Cache);
2139 return RunBackends(BackendProc.get());
2140 }
2141
2142 // Perform two rounds of code generation for ThinLTO:
2143 // 1. First round: Perform optimization and code generation, outputting to
2144 // temporary scratch objects.
2145 // 2. Merge code generation data extracted from the temporary scratch objects.
2146 // 3. Second round: Execute code generation again using the merged data.
2147 LLVM_DEBUG(dbgs() << "[TwoRounds] Initializing ThinLTO two-codegen rounds\n");
2148
2149 unsigned MaxTasks = getMaxTasks();
2150 auto Parallelism = ThinLTO.Backend.getParallelism();
2151 // Set up two additional streams and caches for storing temporary scratch
2152 // objects and optimized IRs, using the same cache directory as the original.
2153 cgdata::StreamCacheData CG(MaxTasks, Cache, "CG"), IR(MaxTasks, Cache, "IR");
2154
2155 // First round: Execute optimization and code generation, outputting to
2156 // temporary scratch objects. Serialize the optimized IRs before initiating
2157 // code generation.
2158 LLVM_DEBUG(dbgs() << "[TwoRounds] Running the first round of codegen\n");
2159 auto FirstRoundLTO = std::make_unique<FirstRoundThinBackend>(
2160 Conf, ThinLTO.CombinedIndex, Parallelism, ModuleToDefinedGVSummaries,
2161 CG.AddStream, CG.Cache, IR.AddStream, IR.Cache);
2162 if (Error E = RunBackends(FirstRoundLTO.get()))
2163 return E;
2164
2165 LLVM_DEBUG(dbgs() << "[TwoRounds] Merging codegen data\n");
2166 auto CombinedHashOrErr = cgdata::mergeCodeGenData(*CG.getResult());
2167 if (Error E = CombinedHashOrErr.takeError())
2168 return E;
2169 auto CombinedHash = *CombinedHashOrErr;
2170 LLVM_DEBUG(dbgs() << "[TwoRounds] CGData hash: " << CombinedHash << "\n");
2171
2172 // Second round: Read the optimized IRs and execute code generation using the
2173 // merged data.
2174 LLVM_DEBUG(dbgs() << "[TwoRounds] Running the second round of codegen\n");
2175 auto SecondRoundLTO = std::make_unique<SecondRoundThinBackend>(
2176 Conf, ThinLTO.CombinedIndex, Parallelism, ModuleToDefinedGVSummaries,
2177 AddStream, Cache, IR.getResult(), CombinedHash);
2178 return RunBackends(SecondRoundLTO.get());
2179}
2180
2184 std::optional<uint64_t> RemarksHotnessThreshold, int Count) {
2185 std::string Filename = std::string(RemarksFilename);
2186 // For ThinLTO, file.opt.<format> becomes
2187 // file.opt.<format>.thin.<num>.<format>.
2188 if (!Filename.empty() && Count != -1)
2189 Filename =
2190 (Twine(Filename) + ".thin." + llvm::utostr(Count) + "." + RemarksFormat)
2191 .str();
2192
2193 auto ResultOrErr = llvm::setupLLVMOptimizationRemarks(
2194 Context, Filename, RemarksPasses, RemarksFormat, RemarksWithHotness,
2196 if (Error E = ResultOrErr.takeError())
2197 return std::move(E);
2198
2199 if (*ResultOrErr)
2200 (*ResultOrErr)->keep();
2201
2202 return ResultOrErr;
2203}
2204
2207 // Setup output file to emit statistics.
2208 if (StatsFilename.empty())
2209 return nullptr;
2210
2212 std::error_code EC;
2213 auto StatsFile =
2214 std::make_unique<ToolOutputFile>(StatsFilename, EC, sys::fs::OF_None);
2215 if (EC)
2216 return errorCodeToError(EC);
2217
2218 StatsFile->keep();
2219 return std::move(StatsFile);
2220}
2221
2222// Compute the ordering we will process the inputs: the rough heuristic here
2223// is to sort them per size so that the largest module get schedule as soon as
2224// possible. This is purely a compile-time optimization.
2226 auto Seq = llvm::seq<int>(0, R.size());
2227 std::vector<int> ModulesOrdering(Seq.begin(), Seq.end());
2228 llvm::sort(ModulesOrdering, [&](int LeftIndex, int RightIndex) {
2229 auto LSize = R[LeftIndex]->getBuffer().size();
2230 auto RSize = R[RightIndex]->getBuffer().size();
2231 return LSize > RSize;
2232 });
2233 return ModulesOrdering;
2234}
2235
2236namespace {
2237/// This out-of-process backend does not perform code generation when invoked
2238/// for each task. Instead, it generates the necessary information (e.g., the
2239/// summary index shard, import list, etc.) to enable code generation to be
2240/// performed externally, similar to WriteIndexesThinBackend. The backend's
2241/// `wait` function then invokes an external distributor process to carry out
2242/// the backend compilations.
2243class OutOfProcessThinBackend : public CGThinBackend {
2244 using SString = SmallString<128>;
2245
2247 StringSaver Saver{Alloc};
2248
2249 SString LinkerOutputFile;
2250
2251 SString DistributorPath;
2252 ArrayRef<StringRef> DistributorArgs;
2253
2254 SString RemoteCompiler;
2255 ArrayRef<StringRef> RemoteCompilerArgs;
2256
2257 bool SaveTemps;
2258
2259 SmallVector<StringRef, 0> CodegenOptions;
2260 DenseSet<StringRef> CommonInputs;
2261
2262 // Information specific to individual backend compilation job.
2263 struct Job {
2264 unsigned Task;
2265 StringRef ModuleID;
2266 StringRef NativeObjectPath;
2267 StringRef SummaryIndexPath;
2268 ImportsFilesContainer ImportsFiles;
2269 };
2270 // The set of backend compilations jobs.
2271 SmallVector<Job> Jobs;
2272
2273 // A unique string to identify the current link.
2274 SmallString<8> UID;
2275
2276 // The offset to the first ThinLTO task.
2277 unsigned ThinLTOTaskOffset;
2278
2279 // The target triple to supply for backend compilations.
2280 llvm::Triple Triple;
2281
2282public:
2283 OutOfProcessThinBackend(
2284 const Config &Conf, ModuleSummaryIndex &CombinedIndex,
2285 ThreadPoolStrategy ThinLTOParallelism,
2286 const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
2287 AddStreamFn AddStream, lto::IndexWriteCallback OnWrite,
2288 bool ShouldEmitIndexFiles, bool ShouldEmitImportsFiles,
2289 StringRef LinkerOutputFile, StringRef Distributor,
2290 ArrayRef<StringRef> DistributorArgs, StringRef RemoteCompiler,
2291 ArrayRef<StringRef> RemoteCompilerArgs, bool SaveTemps)
2292 : CGThinBackend(Conf, CombinedIndex, ModuleToDefinedGVSummaries,
2293 AddStream, OnWrite, ShouldEmitIndexFiles,
2294 ShouldEmitImportsFiles, ThinLTOParallelism),
2295 LinkerOutputFile(LinkerOutputFile), DistributorPath(Distributor),
2296 DistributorArgs(DistributorArgs), RemoteCompiler(RemoteCompiler),
2297 RemoteCompilerArgs(RemoteCompilerArgs), SaveTemps(SaveTemps) {}
2298
2299 virtual void setup(unsigned ThinLTONumTasks, unsigned ThinLTOTaskOffset,
2300 llvm::Triple Triple) override {
2302 Jobs.resize((size_t)ThinLTONumTasks);
2303 this->ThinLTOTaskOffset = ThinLTOTaskOffset;
2304 this->Triple = Triple;
2305 }
2306
2307 Error start(
2308 unsigned Task, BitcodeModule BM,
2309 const FunctionImporter::ImportMapTy &ImportList,
2310 const FunctionImporter::ExportSetTy &ExportList,
2311 const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
2312 MapVector<StringRef, BitcodeModule> &ModuleMap) override {
2313
2314 StringRef ModulePath = BM.getModuleIdentifier();
2315
2316 SString ObjFilePath = sys::path::parent_path(LinkerOutputFile);
2317 sys::path::append(ObjFilePath, sys::path::stem(ModulePath) + "." +
2318 itostr(Task) + "." + UID + ".native.o");
2319
2320 Job &J = Jobs[Task - ThinLTOTaskOffset];
2321 J = {
2322 Task,
2323 ModulePath,
2324 Saver.save(ObjFilePath.str()),
2325 Saver.save(ObjFilePath.str() + ".thinlto.bc"),
2326 {} // Filled in by emitFiles below.
2327 };
2328
2329 assert(ModuleToDefinedGVSummaries.count(ModulePath));
2330
2331 // The BackendThreadPool is only used here to write the sharded index files
2332 // (similar to WriteIndexesThinBackend).
2333 BackendThreadPool.async(
2334 [=](Job &J, const FunctionImporter::ImportMapTy &ImportList) {
2335 if (auto E = emitFiles(ImportList, J.ModuleID, J.ModuleID.str(),
2336 J.SummaryIndexPath, J.ImportsFiles)) {
2337 std::unique_lock<std::mutex> L(ErrMu);
2338 if (Err)
2339 Err = joinErrors(std::move(*Err), std::move(E));
2340 else
2341 Err = std::move(E);
2342 }
2343 },
2344 std::ref(J), std::ref(ImportList));
2345
2346 return Error::success();
2347 }
2348
2349 // Derive a set of Clang options that will be shared/common for all DTLTO
2350 // backend compilations. We are intentionally minimal here as these options
2351 // must remain synchronized with the behavior of Clang. DTLTO does not support
2352 // all the features available with in-process LTO. More features are expected
2353 // to be added over time. Users can specify Clang options directly if a
2354 // feature is not supported. Note that explicitly specified options that imply
2355 // additional input or output file dependencies must be communicated to the
2356 // distribution system, potentially by setting extra options on the
2357 // distributor program.
2358 void buildCommonRemoteCompilerOptions() {
2359 const lto::Config &C = Conf;
2360 auto &Ops = CodegenOptions;
2361
2362 Ops.push_back(Saver.save("-O" + Twine(C.OptLevel)));
2363
2364 if (C.Options.EmitAddrsig)
2365 Ops.push_back("-faddrsig");
2366 if (C.Options.FunctionSections)
2367 Ops.push_back("-ffunction-sections");
2368 if (C.Options.DataSections)
2369 Ops.push_back("-fdata-sections");
2370
2371 if (C.RelocModel == Reloc::PIC_)
2372 // Clang doesn't have -fpic for all triples.
2373 if (!Triple.isOSBinFormatCOFF())
2374 Ops.push_back("-fpic");
2375
2376 // Turn on/off warnings about profile cfg mismatch (default on)
2377 // --lto-pgo-warn-mismatch.
2378 if (!C.PGOWarnMismatch) {
2379 Ops.push_back("-mllvm");
2380 Ops.push_back("-no-pgo-warn-mismatch");
2381 }
2382
2383 // Enable sample-based profile guided optimizations.
2384 // Sample profile file path --lto-sample-profile=<value>.
2385 if (!C.SampleProfile.empty()) {
2386 Ops.push_back(
2387 Saver.save("-fprofile-sample-use=" + Twine(C.SampleProfile)));
2388 CommonInputs.insert(C.SampleProfile);
2389 }
2390
2391 // We don't know which of options will be used by Clang.
2392 Ops.push_back("-Wno-unused-command-line-argument");
2393
2394 // Forward any supplied options.
2395 if (!RemoteCompilerArgs.empty())
2396 for (auto &a : RemoteCompilerArgs)
2397 Ops.push_back(a);
2398 }
2399
2400 // Generates a JSON file describing the backend compilations, for the
2401 // distributor.
2402 bool emitDistributorJson(StringRef DistributorJson) {
2403 using json::Array;
2404 std::error_code EC;
2405 raw_fd_ostream OS(DistributorJson, EC);
2406 if (EC)
2407 return false;
2408
2409 json::OStream JOS(OS);
2410 JOS.object([&]() {
2411 // Information common to all jobs.
2412 JOS.attributeObject("common", [&]() {
2413 JOS.attribute("linker_output", LinkerOutputFile);
2414
2415 JOS.attributeArray("args", [&]() {
2416 JOS.value(RemoteCompiler);
2417
2418 JOS.value("-c");
2419
2420 JOS.value(Saver.save("--target=" + Triple.str()));
2421
2422 for (const auto &A : CodegenOptions)
2423 JOS.value(A);
2424 });
2425
2426 JOS.attribute("inputs", Array(CommonInputs));
2427 });
2428
2429 // Per-compilation-job information.
2430 JOS.attributeArray("jobs", [&]() {
2431 for (const auto &J : Jobs) {
2432 assert(J.Task != 0);
2433
2435 SmallVector<StringRef, 1> Outputs;
2436
2437 JOS.object([&]() {
2438 JOS.attributeArray("args", [&]() {
2439 JOS.value(J.ModuleID);
2440 Inputs.push_back(J.ModuleID);
2441
2442 JOS.value(
2443 Saver.save("-fthinlto-index=" + Twine(J.SummaryIndexPath)));
2444 Inputs.push_back(J.SummaryIndexPath);
2445
2446 JOS.value("-o");
2447 JOS.value(J.NativeObjectPath);
2448 Outputs.push_back(J.NativeObjectPath);
2449 });
2450
2451 // Add the bitcode files from which imports will be made. These do
2452 // not explicitly appear on the backend compilation command lines
2453 // but are recorded in the summary index shards.
2454 llvm::append_range(Inputs, J.ImportsFiles);
2455 JOS.attribute("inputs", Array(Inputs));
2456
2457 JOS.attribute("outputs", Array(Outputs));
2458 });
2459 }
2460 });
2461 });
2462
2463 return true;
2464 }
2465
2466 void removeFile(StringRef FileName) {
2467 std::error_code EC = sys::fs::remove(FileName, true);
2468 if (EC && EC != std::make_error_code(std::errc::no_such_file_or_directory))
2469 errs() << "warning: could not remove the file '" << FileName
2470 << "': " << EC.message() << "\n";
2471 }
2472
2473 Error wait() override {
2474 // Wait for the information on the required backend compilations to be
2475 // gathered.
2476 BackendThreadPool.wait();
2477 if (Err)
2478 return std::move(*Err);
2479
2480 auto CleanPerJobFiles = llvm::make_scope_exit([&] {
2481 if (!SaveTemps)
2482 for (auto &Job : Jobs) {
2483 removeFile(Job.NativeObjectPath);
2484 if (!ShouldEmitIndexFiles)
2485 removeFile(Job.SummaryIndexPath);
2486 }
2487 });
2488
2489 const StringRef BCError = "DTLTO backend compilation: ";
2490
2491 buildCommonRemoteCompilerOptions();
2492
2493 SString JsonFile = sys::path::parent_path(LinkerOutputFile);
2494 sys::path::append(JsonFile, sys::path::stem(LinkerOutputFile) + "." + UID +
2495 ".dist-file.json");
2496 if (!emitDistributorJson(JsonFile))
2498 BCError + "failed to generate distributor JSON script: " + JsonFile,
2500 auto CleanJson = llvm::make_scope_exit([&] {
2501 if (!SaveTemps)
2502 removeFile(JsonFile);
2503 });
2504
2505 SmallVector<StringRef, 3> Args = {DistributorPath};
2506 llvm::append_range(Args, DistributorArgs);
2507 Args.push_back(JsonFile);
2508 std::string ErrMsg;
2509 if (sys::ExecuteAndWait(Args[0], Args,
2510 /*Env=*/std::nullopt, /*Redirects=*/{},
2511 /*SecondsToWait=*/0, /*MemoryLimit=*/0, &ErrMsg)) {
2513 BCError + "distributor execution failed" +
2514 (!ErrMsg.empty() ? ": " + ErrMsg + Twine(".") : Twine(".")),
2516 }
2517
2518 for (auto &Job : Jobs) {
2519 // Load the native object from a file into a memory buffer
2520 // and store its contents in the output buffer.
2521 auto ObjFileMbOrErr =
2522 MemoryBuffer::getFile(Job.NativeObjectPath, /*IsText=*/false,
2523 /*RequiresNullTerminator=*/false);
2524 if (std::error_code EC = ObjFileMbOrErr.getError())
2526 BCError + "cannot open native object file: " +
2527 Job.NativeObjectPath + ": " + EC.message(),
2529 auto StreamOrErr = AddStream(Job.Task, Job.ModuleID);
2530 if (Error Err = StreamOrErr.takeError())
2531 report_fatal_error(std::move(Err));
2532 auto &Stream = *StreamOrErr->get();
2533 *Stream.OS << ObjFileMbOrErr->get()->getMemBufferRef().getBuffer();
2534 if (Error Err = Stream.commit())
2535 report_fatal_error(std::move(Err));
2536 }
2537
2538 return Error::success();
2539 }
2540};
2541} // end anonymous namespace
2542
2544 ThreadPoolStrategy Parallelism, lto::IndexWriteCallback OnWrite,
2545 bool ShouldEmitIndexFiles, bool ShouldEmitImportsFiles,
2546 StringRef LinkerOutputFile, StringRef Distributor,
2547 ArrayRef<StringRef> DistributorArgs, StringRef RemoteCompiler,
2548 ArrayRef<StringRef> RemoteCompilerArgs, bool SaveTemps) {
2549 auto Func =
2550 [=](const Config &Conf, ModuleSummaryIndex &CombinedIndex,
2551 const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
2552 AddStreamFn AddStream, FileCache /*Cache*/) {
2553 return std::make_unique<OutOfProcessThinBackend>(
2554 Conf, CombinedIndex, Parallelism, ModuleToDefinedGVSummaries,
2555 AddStream, OnWrite, ShouldEmitIndexFiles, ShouldEmitImportsFiles,
2556 LinkerOutputFile, Distributor, DistributorArgs, RemoteCompiler,
2557 RemoteCompilerArgs, SaveTemps);
2558 };
2559 return ThinBackend(Func, Parallelism);
2560}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Function Alias Analysis false
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
dxil translate DXIL Translate Metadata
#define DEBUG_TYPE
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
This file supports working with JSON data.
static void writeToResolutionFile(raw_ostream &OS, InputFile *Input, ArrayRef< SymbolResolution > Res)
Definition LTO.cpp:710
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:368
static void handleNonPrevailingComdat(GlobalValue &GV, std::set< const Comdat * > &NonPrevailingComdats)
Definition LTO.cpp:830
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:470
Legalize the Machine IR a function s Machine IR
Definition Legalizer.cpp:80
#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
Machine Check Debug Module
This file contains the declarations for metadata subclasses.
#define P(N)
Provides a library for accessing information about this process and other processes on the operating ...
static const char * name
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.
#define LLVM_DEBUG(...)
Definition Debug.h:114
This pass exposes codegen information to IR-level passes.
The Input class is used to parse a yaml document into in-memory structs and vectors.
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:136
size_t size() const
size - Get the array size.
Definition ArrayRef.h:147
iterator begin() const
Definition ArrayRef.h:135
bool empty() const
empty - Check if the array is empty.
Definition ArrayRef.h:142
const T & consume_front()
consume_front() - Returns the first element and drops it from ArrayRef.
Definition ArrayRef.h:162
static LLVM_ABI ArrayType * get(Type *ElementType, uint64_t NumElements)
This static method is the primary way to construct an ArrayType.
Represents a module in a bitcode file.
StringRef getModuleIdentifier() const
LLVM_ABI Error readSummary(ModuleSummaryIndex &CombinedIndex, StringRef ModulePath, std::function< bool(GlobalValue::GUID)> IsPrevailing=nullptr)
Parse the specified bitcode buffer and merge its module summary index into CombinedIndex.
LLVM_ABI Expected< std::unique_ptr< Module > > parseModule(LLVMContext &Context, ParserCallbacks Callbacks={})
Read the entire bitcode module and return it.
LLVM_ABI 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 LLVM_ABI ConstantAggregateZero * get(Type *Ty)
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:63
iterator find(const_arg_type_t< KeyT > Val)
Definition DenseMap.h:167
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&...Args)
Definition DenseMap.h:237
size_type count(const_arg_type_t< KeyT > Val) const
Return 1 if the specified key is in the map, 0 otherwise.
Definition DenseMap.h:163
iterator end()
Definition DenseMap.h:81
Implements a dense probed hash-table based set.
Definition DenseSet.h:279
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
Tagged union holding either a T or a Error.
Definition Error.h:485
Error takeError()
Take ownership of the stored error.
Definition Error.h:612
The map maintains the list of imports.
DenseSet< ValueInfo > ExportSetTy
The set contains an entry for every global value that the module exports.
Function and variable summary information to aid decisions and implementation of importing.
static bool isAppendingLinkage(LinkageTypes Linkage)
static LLVM_ABI GUID getGUIDAssumingExternalLinkage(StringRef GlobalName)
Return a 64-bit global unique ID constructed from the name of a global symbol.
Definition Globals.cpp:77
static bool isExternalWeakLinkage(LinkageTypes Linkage)
static bool isLocalLinkage(LinkageTypes Linkage)
LLVM_ABI bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition Globals.cpp:328
void setUnnamedAddr(UnnamedAddr Val)
uint64_t GUID
Declare a type to represent a global unique identifier for a global value.
bool hasLocalLinkage() const
static StringRef dropLLVMManglingEscape(StringRef Name)
If the given string begins with the GlobalValue name mangling escape character '\1',...
LLVM_ABI const Comdat * getComdat() const
Definition Globals.cpp:201
static bool isLinkOnceLinkage(LinkageTypes Linkage)
void setLinkage(LinkageTypes LT)
DLLStorageClassTypes
Storage classes of global values for PE targets.
Definition GlobalValue.h:74
GUID getGUID() const
Return a 64-bit global unique ID constructed from global value name (i.e.
static bool isExternalLinkage(LinkageTypes Linkage)
VisibilityTypes
An enumeration for the kinds of visibility of global values.
Definition GlobalValue.h:67
@ DefaultVisibility
The GV is visible.
Definition GlobalValue.h:68
static LLVM_ABI std::string getGlobalIdentifier(StringRef Name, GlobalValue::LinkageTypes Linkage, StringRef FileName)
Return the modified name for a global value suitable to be used as the key for a global lookup (e....
Definition Globals.cpp:161
static LinkageTypes getWeakLinkage(bool ODR)
static bool isWeakForLinker(LinkageTypes Linkage)
Whether the definition of this global may be replaced at link time.
bool hasAppendingLinkage() const
bool hasAvailableExternallyLinkage() const
LinkageTypes
An enumeration for the kinds of linkage for global values.
Definition GlobalValue.h:52
@ CommonLinkage
Tentative definitions.
Definition GlobalValue.h:63
@ InternalLinkage
Rename collisions when linking (static functions).
Definition GlobalValue.h:60
@ ExternalLinkage
Externally visible function.
Definition GlobalValue.h:53
@ WeakAnyLinkage
Keep one copy of named function when linking (weak)
Definition GlobalValue.h:57
@ AvailableExternallyLinkage
Available for inspection, not emission.
Definition GlobalValue.h:54
DLLStorageClassTypes getDLLStorageClass() const
Type * getValueType() const
static bool isLinkOnceODRLinkage(LinkageTypes Linkage)
LLVM_ABI void eraseFromParent()
eraseFromParent - This method unlinks 'this' from the containing module and deletes it.
Definition Globals.cpp:519
void setAlignment(Align Align)
Sets the alignment attribute of the GlobalVariable.
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition Metadata.h:1526
This class implements a map that also provides access to all stored values in a deterministic order.
Definition MapVector.h:36
bool empty() const
Definition MapVector.h:77
iterator begin()
Definition MapVector.h:65
size_type size() const
Definition MapVector.h:56
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFile(const Twine &Filename, bool IsText=false, bool RequiresNullTerminator=true, bool IsVolatile=false, std::optional< Align > Alignment=std::nullopt)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful,...
Class to hold module path string table and global value map, and encapsulate methods for operating on...
CfiFunctionIndex & cfiFunctionDecls()
const ModuleHash & getModuleHash(const StringRef ModPath) const
Get the module SHA1 hash recorded for the given module path.
const StringMap< ModuleHash > & modulePaths() const
Table of modules, containing module hash and id.
CfiFunctionIndex & cfiFunctionDefs()
LLVM_ABI void addModule(Module *M)
static LLVM_ABI 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.
PointerUnion< GlobalValue *, AsmSymbol * > Symbol
LLVM_ABI 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:67
A class that wrap the SHA1 algorithm.
Definition SHA1.h:27
LLVM_ABI void update(ArrayRef< uint8_t > Data)
Digest more data.
Definition SHA1.cpp:208
LLVM_ABI 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
size_type count(const T &V) const
count - Return 1 if the element is in the set, 0 otherwise.
Definition SmallSet.h:175
bool empty() const
Definition SmallSet.h:168
bool erase(const T &V)
Definition SmallSet.h:199
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:183
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.
void reserve(size_type N)
void resize(size_type N)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
A wrapper around a string literal that serves as a proxy for constructing global tables of StringRefs...
Definition StringRef.h:854
size_type count(StringRef Key) const
count - Return 1 if the element is in the map, 0 otherwise.
Definition StringMap.h:285
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
constexpr bool empty() const
empty - Check if the string is empty.
Definition StringRef.h:143
constexpr const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition StringRef.h:140
bool contains(StringRef Other) const
Return true if the given string is a substring of *this, and false otherwise.
Definition StringRef.h:426
Saves strings in the provided stable storage and returns a StringRef with a stable character pointer.
Definition StringSaver.h:22
StringRef save(const char *S)
Definition StringSaver.h:31
MCTargetOptions MCOptions
Machine level options.
DebuggerKind DebuggerTuning
Which debugger to tune for.
unsigned FunctionSections
Emit functions into separate sections.
unsigned DataSections
Emit data into separate sections.
This tells how a thread pool will be used.
Definition Threading.h:115
The TimeTraceScope is a helper class to call the begin and end functions of the time trace profiler.
Triple - Helper class for working with autoconf configuration names.
Definition Triple.h:47
bool isArm64e() const
Tests whether the target is the Apple "arm64e" AArch64 subarch.
Definition Triple.h:1128
ArchType getArch() const
Get the parsed architecture type of this triple.
Definition Triple.h:411
bool isOSBinFormatCOFF() const
Tests whether the OS uses the COFF binary format.
Definition Triple.h:774
const std::string & str() const
Definition Triple.h:478
bool isOSDarwin() const
Is this a "Darwin" OS (macOS, iOS, tvOS, watchOS, DriverKit, XROS, or bridgeOS).
Definition Triple.h:611
bool isOSBinFormatELF() const
Tests whether the OS uses the ELF binary format.
Definition Triple.h:769
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:45
static LLVM_ABI IntegerType * getInt8Ty(LLVMContext &C)
Definition Type.cpp:295
LLVM_ABI void setName(const Twine &Name)
Change the name of the value.
Definition Value.cpp:390
LLVM_ABI void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition Value.cpp:546
bool use_empty() const
Definition Value.h:346
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:322
LLVM_ABI void takeName(Value *V)
Transfer the name from V to this value.
Definition Value.cpp:396
std::pair< iterator, bool > insert(const ValueT &V)
Definition DenseSet.h:202
iterator find(const_arg_type_t< ValueT > V)
Definition DenseSet.h:167
void insert_range(Range &&R)
Definition DenseSet.h:228
size_type size() const
Definition DenseSet.h:87
bool contains(const_arg_type_t< ValueT > V) const
Check if the set contains the given element.
Definition DenseSet.h:175
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:180
An efficient, type-erasing, non-owning reference to a callable.
Ephemeral symbols produced by Reader::symbols() and Reader::module_symbols().
Definition IRSymtab.h:316
An input file.
Definition LTO.h:119
static LLVM_ABI Expected< std::unique_ptr< InputFile > > create(MemoryBufferRef Object)
Create an InputFile.
Definition LTO.cpp:562
ArrayRef< Symbol > symbols() const
A range over the symbols in this InputFile.
Definition LTO.h:173
LLVM_ABI StringRef getName() const
Returns the path to the InputFile.
Definition LTO.cpp:591
LLVM_ABI BitcodeModule & getSingleBitcodeModule()
Definition LTO.cpp:595
LLVM_ABI LTO(Config Conf, ThinBackend Backend={}, unsigned ParallelCodeGenParallelismLevel=1, LTOKind LTOMode=LTOK_Default)
Create an LTO object.
Definition LTO.cpp:613
LLVM_ABI 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:734
static LLVM_ABI SmallVector< const char * > getRuntimeLibcallSymbols(const Triple &TT)
Static method that returns a list of libcall symbols that can be generated by LTO but might not be vi...
Definition LTO.cpp:1428
LTOKind
Unified LTO modes.
Definition LTO.h:379
@ LTOK_UnifiedRegular
Regular LTO, with Unified LTO enabled.
Definition LTO.h:384
@ LTOK_Default
Any LTO mode without Unified LTO. The default mode.
Definition LTO.h:381
@ LTOK_UnifiedThin
ThinLTO, with Unified LTO enabled.
Definition LTO.h:387
LLVM_ABI ~LTO()
LLVM_ABI unsigned getMaxTasks() const
Returns an upper bound on the number of tasks that the client may expect.
Definition LTO.cpp:1151
LLVM_ABI Error run(AddStreamFn AddStream, FileCache Cache={})
Runs the LTO pipeline.
Definition LTO.cpp:1202
This class defines the interface to the ThinLTO backend.
Definition LTO.h:210
const DenseMap< StringRef, GVSummaryMapTy > & ModuleToDefinedGVSummaries
Definition LTO.h:214
LLVM_ABI Error emitFiles(const FunctionImporter::ImportMapTy &ImportList, StringRef ModulePath, const std::string &NewModulePath) const
Definition LTO.cpp:1442
ModuleSummaryIndex & CombinedIndex
Definition LTO.h:213
A raw_ostream that writes to a file descriptor.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
static LLVM_ABI Pid getProcessId()
Get the process's identifier.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
LLVM_ABI Function * getDeclarationIfExists(const Module *M, ID id)
Look up the Function declaration of the intrinsic id in the Module M and return it if it exists.
LLVM_ABI Expected< stable_hash > mergeCodeGenData(ArrayRef< StringRef > ObjectFiles)
Merge the codegen data from the scratch objects ObjectFiles from the first codegen round.
LLVM_ABI std::unique_ptr< Module > loadModuleForTwoRounds(BitcodeModule &OrigModule, unsigned Task, LLVMContext &Context, ArrayRef< StringRef > IRFiles)
Load the optimized bitcode module for the second codegen round.
initializer< Ty > init(const Ty &Val)
LLVM_ABI ThinBackend createInProcessThinBackend(ThreadPoolStrategy Parallelism, IndexWriteCallback OnWrite=nullptr, bool ShouldEmitIndexFiles=false, bool ShouldEmitImportsFiles=false)
This ThinBackend runs the individual backend jobs in-process.
Definition LTO.cpp:1792
LLVM_ABI std::string getThinLTOOutputFile(StringRef Path, StringRef OldPrefix, StringRef NewPrefix)
Given the original Path to an output file, replace any path prefix matching OldPrefix with NewPrefix.
Definition LTO.cpp:1826
LLVM_ABI StringLiteral getThinLTODefaultCPU(const Triple &TheTriple)
Definition LTO.cpp:1808
LLVM_ABI Expected< std::unique_ptr< ToolOutputFile > > setupStatsFile(StringRef StatsFilename)
Setups the output file for saving statistics.
Definition LTO.cpp:2206
LLVM_ABI Error backend(const Config &C, AddStreamFn AddStream, unsigned ParallelCodeGenParallelismLevel, Module &M, ModuleSummaryIndex &CombinedIndex)
Runs a regular LTO backend.
std::function< void(const std::string &)> IndexWriteCallback
Definition LTO.h:205
LLVM_ABI Error finalizeOptimizationRemarks(LLVMRemarkFileHandle DiagOutputFile)
LLVM_ABI ThinBackend createWriteIndexesThinBackend(ThreadPoolStrategy Parallelism, std::string OldPrefix, std::string NewPrefix, std::string NativeObjectPrefix, bool ShouldEmitImportsFiles, raw_fd_ostream *LinkedObjectsFile, IndexWriteCallback OnWrite)
This ThinBackend writes individual module indexes to files, instead of running the individual backend...
Definition LTO.cpp:1912
LLVM_ABI Expected< LLVMRemarkFileHandle > 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:2181
LLVM_ABI ThinBackend createOutOfProcessThinBackend(ThreadPoolStrategy Parallelism, IndexWriteCallback OnWrite, bool ShouldEmitIndexFiles, bool ShouldEmitImportsFiles, StringRef LinkerOutputFile, StringRef Distributor, ArrayRef< StringRef > DistributorArgs, StringRef RemoteCompiler, ArrayRef< StringRef > RemoteCompilerArgs, bool SaveTemps)
This ThinBackend generates the index shards and then runs the individual backend jobs via an external...
Definition LTO.cpp:2543
LLVM_ABI std::vector< int > generateModulesOrdering(ArrayRef< BitcodeModule * > R)
Produces a container ordering for optimal multi-threaded processing.
Definition LTO.cpp:2225
LLVM_ABI 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, bool CodeGenOnly, AddStreamFn IRAddStream=nullptr, const std::vector< uint8_t > &CmdArgs=std::vector< uint8_t >())
Runs a ThinLTO backend.
llvm::SmallVector< std::string > ImportsFilesContainer
Definition LTO.h:207
LLVM_ABI void updateMemProfAttributes(Module &Mod, const ModuleSummaryIndex &Index)
Updates MemProf attributes (and metadata) based on whether the index has recorded that we are linking...
Definition LTO.cpp:1260
LLVM_ABI Expected< IRSymtabFile > readIRSymtab(MemoryBufferRef MBRef)
Reads a bitcode file, creating its irsymtab if necessary.
DiagnosticInfoOptimizationBase::Argument NV
void write64le(void *P, uint64_t V)
Definition Endian.h:478
void write32le(void *P, uint32_t V)
Definition Endian.h:475
LLVM_ABI std::error_code remove(const Twine &path, bool IgnoreNonExisting=true)
Remove path.
LLVM_ABI 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
LLVM_ABI StringRef stem(StringRef path LLVM_LIFETIME_BOUND, Style style=Style::native)
Get stem.
Definition Path.cpp:579
LLVM_ABI StringRef parent_path(StringRef path LLVM_LIFETIME_BOUND, Style style=Style::native)
Get parent path.
Definition Path.cpp:467
LLVM_ABI 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
LLVM_ABI void append(SmallVectorImpl< char > &path, const Twine &a, const Twine &b="", const Twine &c="", const Twine &d="")
Append to path.
Definition Path.cpp:456
LLVM_ABI int ExecuteAndWait(StringRef Program, ArrayRef< StringRef > Args, std::optional< ArrayRef< StringRef > > Env=std::nullopt, ArrayRef< std::optional< StringRef > > Redirects={}, unsigned SecondsToWait=0, unsigned MemoryLimit=0, std::string *ErrMsg=nullptr, bool *ExecutionFailed=nullptr, std::optional< ProcessStatistics > *ProcStat=nullptr, BitVector *AffinityMask=nullptr)
This function executes the program using the arguments provided.
Definition Program.cpp:32
This is an optimization pass for GlobalISel generic memory operations.
ThreadPoolStrategy heavyweight_hardware_concurrency(unsigned ThreadCount=0)
Returns a thread strategy for tasks requiring significant memory or other resources.
Definition Threading.h:162
detail::zippy< detail::zip_shortest, T, U, Args... > zip(T &&t, U &&u, Args &&...args)
zip iterator for two or more iteratable types.
Definition STLExtras.h:831
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:1705
Error createFileError(const Twine &F, Error E)
Concatenate a source file path and/or name with an Error.
Definition Error.h:1399
std::unordered_set< GlobalValueSummary * > GVSummaryPtrSet
A set of global value summary pointers.
detail::scope_exit< std::decay_t< Callable > > make_scope_exit(Callable &&F)
Definition ScopeExit.h:59
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:644
void generateParamAccessSummary(ModuleSummaryIndex &Index)
cl::opt< bool > CodeGenDataThinLTOTwoRounds("codegen-data-thinlto-two-rounds", cl::init(false), cl::Hidden, cl::desc("Enable two-round ThinLTO code generation. The first round " "emits codegen data, while the second round uses the emitted " "codegen data for further optimizations."))
Definition LTO.cpp:79
LLVM_ABI Expected< LLVMRemarkFileHandle > setupLLVMOptimizationRemarks(LLVMContext &Context, StringRef RemarksFilename, StringRef RemarksPasses, StringRef RemarksFormat, bool RemarksWithHotness, std::optional< uint64_t > RemarksHotnessThreshold=0)
Set up optimization remarks that output to a file.
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"))
LLVM_ABI std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition Error.cpp:98
LLVM_ABI raw_fd_ostream & outs()
This returns a reference to a raw_fd_ostream for standard output.
DenseMap< GlobalValue::GUID, GlobalValueSummary * > GVSummaryMapTy
Map of global value GUID to its summary, used to identify values defined in a particular module,...
auto dyn_cast_if_present(const Y &Val)
dyn_cast_if_present<X> - Functionally identical to dyn_cast, except that a null (or none in the case ...
Definition Casting.h:733
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2116
uint64_t stable_hash
An opaque object representing a stable hash code.
std::string utostr(uint64_t X, bool isNeg=false)
LLVM_ABI 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.
LLVM_ABI bool hasWholeProgramVisibility(bool WholeProgramVisibilityEnabledInLTO)
LLVM_ABI void writeIndexToFile(const ModuleSummaryIndex &Index, raw_ostream &Out, const ModuleToSummariesForIndexTy *ModuleToSummariesForIndex=nullptr, const GVSummaryPtrSet *DecSummaries=nullptr)
Write the specified module summary index to the given raw output stream, where it will be written in ...
LLVM_ABI void ComputeCrossModuleImport(const ModuleSummaryIndex &Index, const DenseMap< StringRef, GVSummaryMapTy > &ModuleToDefinedGVSummaries, function_ref< bool(GlobalValue::GUID, const GlobalValueSummary *)> isPrevailing, FunctionImporter::ImportListsTy &ImportLists, DenseMap< StringRef, FunctionImporter::ExportSetTy > &ExportLists)
Compute all the imports and exports for every module in the Index.
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:754
LLVM_ABI void EnableStatistics(bool DoPrintOnExit=true)
Enable the collection and printing of statistics.
LLVM_ABI 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:549
LLVM_ABI void timeTraceProfilerInitialize(unsigned TimeTraceGranularity, StringRef ProcName, bool TimeTraceVerbose=false)
Initialize the time trace profiler.
LLVM_ABI void timeTraceProfilerFinishThread()
Finish a time trace profiler running on a worker thread.
LLVM_ABI std::string recomputeLTOCacheKey(const std::string &Key, StringRef ExtraID)
Recomputes the LTO cache key for a given key with an extra identifier.
Definition LTO.cpp:354
Error joinErrors(Error E1, Error E2)
Concatenate errors.
Definition Error.h:442
LLVM_ABI void updatePublicTypeTestCalls(Module &M, bool WholeProgramVisibilityEnabledInLTO)
LLVM_ABI void getVisibleToRegularObjVtableGUIDs(ModuleSummaryIndex &Index, DenseSet< GlobalValue::GUID > &VisibleToRegularObjSymbols, function_ref< bool(StringRef)> IsVisibleToRegularObj)
Based on typeID string, get all associated vtable GUIDS that are visible to regular objects.
void sort(IteratorTy Start, IteratorTy End)
Definition STLExtras.h:1624
bool timeTraceProfilerEnabled()
Is the time trace profiler enabled, i.e. initialized?
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:167
FunctionAddr VTableAddr Count
Definition InstrProf.h:139
std::map< std::string, GVSummaryMapTy, std::less<> > ModuleToSummariesForIndexTy
Map of a module name to the GUIDs and summaries we will import from that module.
LLVM_ABI 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)
LLVM_ABI 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"))
BumpPtrAllocatorImpl BumpPtrAllocator
The standard BumpPtrAllocator which just uses the default template parameters.
Definition Allocator.h:383
cl::opt< bool > SupportsHotColdNew
Indicate we are linking with an allocator that supports hot/cold operator new interfaces.
LLVM_ABI 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...
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
LLVM_ABI 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:448
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:548
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
Error make_error(ArgTs &&... Args)
Make a Error instance representing failure using the given error info type.
Definition Error.h:340
LLVM_ABI 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.
Definition ModRef.h:34
std::string join(IteratorT Begin, IteratorT End, StringRef Separator)
Joins the strings in the range [Begin, End), adding Separator between the elements.
FunctionAddr VTableAddr uintptr_t uintptr_t Data
Definition InstrProf.h:189
cl::opt< bool > EnableMemProfContextDisambiguation
Enable MemProf context disambiguation for thin link.
LLVM_ABI 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.
cl::opt< bool > ForceImportAll
LLVM_ABI void gatherImportedSummariesForModule(StringRef ModulePath, const DenseMap< StringRef, GVSummaryMapTy > &ModuleToDefinedGVSummaries, const FunctionImporter::ImportMapTy &ImportList, ModuleToSummariesForIndexTy &ModuleToSummariesForIndex, GVSummaryPtrSet &DecSummaries)
Compute the set of summaries needed for a ThinLTO backend compilation of ModulePath.
ArrayRef(const T &OneElt) -> ArrayRef< T >
void toHex(ArrayRef< uint8_t > Input, bool LowerCase, SmallVectorImpl< char > &Output)
Convert buffer Input to its hexadecimal representation. The returned string is double the size of Inp...
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:1847
LLVM_ABI void processImportsFiles(StringRef ModulePath, const ModuleToSummariesForIndexTy &ModuleToSummariesForIndex, function_ref< void(const std::string &)> F)
Call F passing each of the files module ModulePath will import from.
auto count_if(R &&Range, UnaryPredicate P)
Wrapper function around std::count_if to count the number of times an element satisfying a given pred...
Definition STLExtras.h:1941
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)
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:560
LLVM_ABI std::string computeLTOCacheKey(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 DenseSet< GlobalValue::GUID > &CfiFunctionDefs={}, const DenseSet< GlobalValue::GUID > &CfiFunctionDecls={})
Computes a unique hash for the Module considering the current list of export/import and other global ...
Definition LTO.cpp:104
LLVM_ABI Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
Definition Error.cpp:111
static cl::opt< bool > LTOKeepSymbolCopies("lto-keep-symbol-copies", cl::init(false), cl::Hidden, cl::desc("Keep copies of symbols in LTO indexing"))
LLVM_ABI bool UpgradeDebugInfo(Module &M)
Check the debug info version number, if it is out-dated, drop the debug info.
auto seq(T Begin, T End)
Iterate over an integral type from Begin up to - but not including - End.
Definition Sequence.h:305
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:59
LLVM_ABI void PrintStatisticsJSON(raw_ostream &OS)
Print statistics in JSON format.
LLVM_ABI 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.
LLVM_ABI Error EmitImportsFiles(StringRef ModulePath, StringRef OutputFilename, const ModuleToSummariesForIndexTy &ModuleToSummariesForIndex)
Emit into OutputFilename the files module ModulePath will import from.
@ Keep
No function return thunk.
Definition CodeGen.h:156
std::string itostr(int64_t X)
LLVM_ABI void updateVCallVisibilityInModule(Module &M, bool WholeProgramVisibilityEnabledInLTO, const DenseSet< GlobalValue::GUID > &DynamicExportSymbols, bool ValidateAllVtablesHaveTypeInfos, function_ref< bool(StringRef)> IsVisibleToRegularObj)
If whole program visibility asserted, then upgrade all public vcall visibility metadata on vtable def...
LLVM_ABI TimeTraceProfilerEntry * timeTraceProfilerBegin(StringRef Name, StringRef Detail)
Manually begin a time section, with the given Name and Detail.
LLVM_ABI void updateVCallVisibilityInIndex(ModuleSummaryIndex &Index, bool WholeProgramVisibilityEnabledInLTO, const DenseSet< GlobalValue::GUID > &DynamicExportSymbols, const DenseSet< GlobalValue::GUID > &VisibleToRegularObjSymbols)
If whole program visibility asserted, then upgrade all public vcall visibility metadata on vtable def...
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:867
This type represents a file cache system that manages caching of files.
Definition Caching.h:85
bool isValid() const
Definition Caching.h:98
A simple container for information about the supported runtime calls.
static StringRef getLibcallImplName(RTLIB::LibcallImpl CallImpl)
Get the libcall routine name for the specified libcall implementation.
ArrayRef< RTLIB::LibcallImpl > getLibcallImpls() const
Struct that holds a reference to a particular GUID in a global value summary.
LTO configuration.
Definition Config.h:42
std::optional< uint64_t > RemarksHotnessThreshold
The minimum hotness value a diagnostic needs in order to be included in optimization diagnostics.
Definition Config.h:166
std::optional< CodeModel::Model > CodeModel
Definition Config.h:57
std::string AAPipeline
Definition Config.h:111
bool CodeGenOnly
Disable entirely the optimizer, including importing for ThinLTO.
Definition Config.h:69
std::vector< std::string > MAttrs
Definition Config.h:51
std::vector< std::string > MllvmArgs
Definition Config.h:52
CodeGenOptLevel CGOptLevel
Definition Config.h:58
std::string DefaultTriple
Setting this field will replace unspecified target triples in input files with this triple.
Definition Config.h:119
std::string CPU
Definition Config.h:49
std::string DwoDir
The directory to store .dwo files.
Definition Config.h:131
std::string RemarksFilename
Optimization remarks file path.
Definition Config.h:145
std::string OverrideTriple
Setting this field will replace target triples in input files with this triple.
Definition Config.h:115
std::string ProfileRemapping
Name remapping file for profile data.
Definition Config.h:128
TargetOptions Options
Definition Config.h:50
bool TimeTraceEnabled
Time trace enabled.
Definition Config.h:181
std::string RemarksPasses
Optimization remarks pass filter.
Definition Config.h:148
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:106
unsigned TimeTraceGranularity
Time trace granularity.
Definition Config.h:184
unsigned OptLevel
Definition Config.h:60
bool RemarksWithHotness
Whether to emit optimization remarks with hotness informations.
Definition Config.h:151
std::optional< Reloc::Model > RelocModel
Definition Config.h:56
CodeGenFileType CGFileType
Definition Config.h:59
bool Freestanding
Flag to indicate that the optimizer should not assume builtins are present on the target.
Definition Config.h:66
std::string SampleProfile
Sample PGO profile path.
Definition Config.h:125
std::string RemarksFormat
The format used for serializing remarks (default: YAML).
Definition Config.h:169
The purpose of this struct is to only expose the symbol information that an LTO client should need in...
Definition LTO.h:148
The resolution for a symbol.
Definition LTO.h:588
unsigned FinalDefinitionInLinkageUnit
The definition of this symbol is unpreemptable at runtime and is known to be in this linkage unit.
Definition LTO.h:598
unsigned ExportDynamic
The symbol was exported dynamically, and therefore could be referenced by a shared library not visibl...
Definition LTO.h:605
unsigned Prevailing
The linker has chosen this definition of the symbol.
Definition LTO.h:594
unsigned LinkerRedefined
Linker redefined version of the symbol which appeared in -wrap or -defsym linker option.
Definition LTO.h:609
unsigned VisibleToRegularObj
The definition of this symbol is visible outside of the LTO unit.
Definition LTO.h:601
This type defines the behavior following the thin-link phase during ThinLTO.
Definition LTO.h:280