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