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