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) {
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 auto *ResI = Res.begin();
635 auto *ResE = Res.end();
636 (void)ResE;
637 for (const InputFile::Symbol &Sym : Syms) {
638 assert(ResI != ResE);
639 SymbolResolution Res = *ResI++;
640
641 StringRef SymbolName = Sym.getName();
642 // Keep copies of symbols if the client of LTO says so.
643 if (GlobalResolutionSymbolSaver && !GlobalResolutions->contains(SymbolName))
644 SymbolName = GlobalResolutionSymbolSaver->save(SymbolName);
645
646 auto &GlobalRes = (*GlobalResolutions)[SymbolName];
647 GlobalRes.UnnamedAddr &= Sym.isUnnamedAddr();
648 if (Res.Prevailing) {
649 assert(!GlobalRes.Prevailing &&
650 "Multiple prevailing defs are not allowed");
651 GlobalRes.Prevailing = true;
652 GlobalRes.IRName = std::string(Sym.getIRName());
653 } else if (!GlobalRes.Prevailing && GlobalRes.IRName.empty()) {
654 // Sometimes it can be two copies of symbol in a module and prevailing
655 // symbol can have no IR name. That might happen if symbol is defined in
656 // module level inline asm block. In case we have multiple modules with
657 // the same symbol we want to use IR name of the prevailing symbol.
658 // Otherwise, if we haven't seen a prevailing symbol, set the name so that
659 // we can later use it to check if there is any prevailing copy in IR.
660 GlobalRes.IRName = std::string(Sym.getIRName());
661 }
662
663 // In rare occasion, the symbol used to initialize GlobalRes has a different
664 // IRName from the inspected Symbol. This can happen on macOS + iOS, when a
665 // symbol is referenced through its mangled name, say @"\01_symbol" while
666 // the IRName is @symbol (the prefix underscore comes from MachO mangling).
667 // In that case, we have the same actual Symbol that can get two different
668 // GUID, leading to some invalid internalization. Workaround this by marking
669 // the GlobalRes external.
670
671 // FIXME: instead of this check, it would be desirable to compute GUIDs
672 // based on mangled name, but this requires an access to the Target Triple
673 // and would be relatively invasive on the codebase.
674 if (GlobalRes.IRName != Sym.getIRName()) {
675 GlobalRes.Partition = GlobalResolution::External;
676 GlobalRes.VisibleOutsideSummary = true;
677 }
678
679 // Set the partition to external if we know it is re-defined by the linker
680 // with -defsym or -wrap options, used elsewhere, e.g. it is visible to a
681 // regular object, is referenced from llvm.compiler.used/llvm.used, or was
682 // already recorded as being referenced from a different partition.
683 if (Res.LinkerRedefined || Res.VisibleToRegularObj || Sym.isUsed() ||
684 (GlobalRes.Partition != GlobalResolution::Unknown &&
685 GlobalRes.Partition != Partition)) {
686 GlobalRes.Partition = GlobalResolution::External;
687 } else
688 // First recorded reference, save the current partition.
689 GlobalRes.Partition = Partition;
690
691 // Flag as visible outside of summary if visible from a regular object or
692 // from a module that does not have a summary.
693 GlobalRes.VisibleOutsideSummary |=
694 (Res.VisibleToRegularObj || Sym.isUsed() || !InSummary);
695
696 GlobalRes.ExportDynamic |= Res.ExportDynamic;
697 }
698}
699
700void LTO::releaseGlobalResolutionsMemory() {
701 // Release GlobalResolutions dense-map itself.
702 GlobalResolutions.reset();
703 // Release the string saver memory.
704 GlobalResolutionSymbolSaver.reset();
705 Alloc.reset();
706}
707
710 StringRef Path = Input->getName();
711 OS << Path << '\n';
712 auto ResI = Res.begin();
713 for (const InputFile::Symbol &Sym : Input->symbols()) {
714 assert(ResI != Res.end());
715 SymbolResolution Res = *ResI++;
716
717 OS << "-r=" << Path << ',' << Sym.getName() << ',';
718 if (Res.Prevailing)
719 OS << 'p';
721 OS << 'l';
722 if (Res.VisibleToRegularObj)
723 OS << 'x';
724 if (Res.LinkerRedefined)
725 OS << 'r';
726 OS << '\n';
727 }
728 OS.flush();
729 assert(ResI == Res.end());
730}
731
732Error LTO::add(std::unique_ptr<InputFile> Input,
734 assert(!CalledGetMaxTasks);
735
736 if (Conf.ResolutionFile)
737 writeToResolutionFile(*Conf.ResolutionFile, Input.get(), Res);
738
739 if (RegularLTO.CombinedModule->getTargetTriple().empty()) {
740 Triple InputTriple(Input->getTargetTriple());
741 RegularLTO.CombinedModule->setTargetTriple(InputTriple);
742 if (InputTriple.isOSBinFormatELF())
744 }
745
746 ArrayRef<SymbolResolution> InputRes = Res;
747 for (unsigned I = 0; I != Input->Mods.size(); ++I) {
748 if (auto Err = addModule(*Input, InputRes, I, Res).moveInto(Res))
749 return Err;
750 }
751
752 assert(Res.empty());
753 return Error::success();
754}
755
757LTO::addModule(InputFile &Input, ArrayRef<SymbolResolution> InputRes,
758 unsigned ModI, ArrayRef<SymbolResolution> Res) {
759 Expected<BitcodeLTOInfo> LTOInfo = Input.Mods[ModI].getLTOInfo();
760 if (!LTOInfo)
761 return LTOInfo.takeError();
762
763 if (EnableSplitLTOUnit) {
764 // If only some modules were split, flag this in the index so that
765 // we can skip or error on optimizations that need consistently split
766 // modules (whole program devirt and lower type tests).
767 if (*EnableSplitLTOUnit != LTOInfo->EnableSplitLTOUnit)
768 ThinLTO.CombinedIndex.setPartiallySplitLTOUnits();
769 } else
770 EnableSplitLTOUnit = LTOInfo->EnableSplitLTOUnit;
771
772 BitcodeModule BM = Input.Mods[ModI];
773
774 if ((LTOMode == LTOK_UnifiedRegular || LTOMode == LTOK_UnifiedThin) &&
775 !LTOInfo->UnifiedLTO)
776 return make_error<StringError>(
777 "unified LTO compilation must use "
778 "compatible bitcode modules (use -funified-lto)",
780
781 if (LTOInfo->UnifiedLTO && LTOMode == LTOK_Default)
782 LTOMode = LTOK_UnifiedThin;
783
784 bool IsThinLTO = LTOInfo->IsThinLTO && (LTOMode != LTOK_UnifiedRegular);
785
786 auto ModSyms = Input.module_symbols(ModI);
787 addModuleToGlobalRes(ModSyms, Res,
788 IsThinLTO ? ThinLTO.ModuleMap.size() + 1 : 0,
789 LTOInfo->HasSummary);
790
791 if (IsThinLTO)
792 return addThinLTO(BM, ModSyms, Res);
793
794 RegularLTO.EmptyCombinedModule = false;
795 auto ModOrErr = addRegularLTO(Input, InputRes, BM, ModSyms, Res);
796 if (!ModOrErr)
797 return ModOrErr.takeError();
798 Res = ModOrErr->second;
799
800 if (!LTOInfo->HasSummary) {
801 if (Error Err = linkRegularLTO(std::move(ModOrErr->first),
802 /*LivenessFromIndex=*/false))
803 return Err;
804 return Res;
805 }
806
807 // Regular LTO module summaries are added to a dummy module that represents
808 // the combined regular LTO module.
809 if (Error Err = BM.readSummary(ThinLTO.CombinedIndex, ""))
810 return Err;
811 RegularLTO.ModsWithSummaries.push_back(std::move(ModOrErr->first));
812 return Res;
813}
814
815// Checks whether the given global value is in a non-prevailing comdat
816// (comdat containing values the linker indicated were not prevailing,
817// which we then dropped to available_externally), and if so, removes
818// it from the comdat. This is called for all global values to ensure the
819// comdat is empty rather than leaving an incomplete comdat. It is needed for
820// regular LTO modules, in case we are in a mixed-LTO mode (both regular
821// and thin LTO modules) compilation. Since the regular LTO module will be
822// linked first in the final native link, we want to make sure the linker
823// doesn't select any of these incomplete comdats that would be left
824// in the regular LTO module without this cleanup.
825static void
827 std::set<const Comdat *> &NonPrevailingComdats) {
828 Comdat *C = GV.getComdat();
829 if (!C)
830 return;
831
832 if (!NonPrevailingComdats.count(C))
833 return;
834
835 // Additionally need to drop all global values from the comdat to
836 // available_externally, to satisfy the COMDAT requirement that all members
837 // are discarded as a unit. The non-local linkage global values avoid
838 // duplicate definition linker errors.
840
841 if (auto GO = dyn_cast<GlobalObject>(&GV))
842 GO->setComdat(nullptr);
843}
844
845// Add a regular LTO object to the link.
846// The resulting module needs to be linked into the combined LTO module with
847// linkRegularLTO.
849 std::pair<LTO::RegularLTOState::AddedModule, ArrayRef<SymbolResolution>>>
850LTO::addRegularLTO(InputFile &Input, ArrayRef<SymbolResolution> InputRes,
853 RegularLTOState::AddedModule Mod;
855 BM.getLazyModule(RegularLTO.Ctx, /*ShouldLazyLoadMetadata*/ true,
856 /*IsImporting*/ false);
857 if (!MOrErr)
858 return MOrErr.takeError();
859 Module &M = **MOrErr;
860 Mod.M = std::move(*MOrErr);
861
862 if (Error Err = M.materializeMetadata())
863 return std::move(Err);
864
865 if (LTOMode == LTOK_UnifiedRegular) {
866 // cfi.functions metadata is intended to be used with ThinLTO and may
867 // trigger invalid IR transformations if they are present when doing regular
868 // LTO, so delete it.
869 if (NamedMDNode *CfiFunctionsMD = M.getNamedMetadata("cfi.functions"))
870 M.eraseNamedMetadata(CfiFunctionsMD);
871 } else if (NamedMDNode *AliasesMD = M.getNamedMetadata("aliases")) {
872 // Delete aliases entries for non-prevailing symbols on the ThinLTO side of
873 // this input file.
874 DenseSet<StringRef> Prevailing;
875 for (auto [I, R] : zip(Input.symbols(), InputRes))
876 if (R.Prevailing && !I.getIRName().empty())
877 Prevailing.insert(I.getIRName());
878 std::vector<MDNode *> AliasGroups;
879 for (MDNode *AliasGroup : AliasesMD->operands()) {
880 std::vector<Metadata *> Aliases;
881 for (Metadata *Alias : AliasGroup->operands()) {
882 if (isa<MDString>(Alias) &&
883 Prevailing.count(cast<MDString>(Alias)->getString()))
884 Aliases.push_back(Alias);
885 }
886 if (Aliases.size() > 1)
887 AliasGroups.push_back(MDTuple::get(RegularLTO.Ctx, Aliases));
888 }
889 AliasesMD->clearOperands();
890 for (MDNode *G : AliasGroups)
891 AliasesMD->addOperand(G);
892 }
893
895
896 ModuleSymbolTable SymTab;
897 SymTab.addModule(&M);
898
899 for (GlobalVariable &GV : M.globals())
900 if (GV.hasAppendingLinkage())
901 Mod.Keep.push_back(&GV);
902
903 DenseSet<GlobalObject *> AliasedGlobals;
904 for (auto &GA : M.aliases())
905 if (GlobalObject *GO = GA.getAliaseeObject())
906 AliasedGlobals.insert(GO);
907
908 // In this function we need IR GlobalValues matching the symbols in Syms
909 // (which is not backed by a module), so we need to enumerate them in the same
910 // order. The symbol enumeration order of a ModuleSymbolTable intentionally
911 // matches the order of an irsymtab, but when we read the irsymtab in
912 // InputFile::create we omit some symbols that are irrelevant to LTO. The
913 // Skip() function skips the same symbols from the module as InputFile does
914 // from the symbol table.
915 auto MsymI = SymTab.symbols().begin(), MsymE = SymTab.symbols().end();
916 auto Skip = [&]() {
917 while (MsymI != MsymE) {
918 auto Flags = SymTab.getSymbolFlags(*MsymI);
919 if ((Flags & object::BasicSymbolRef::SF_Global) &&
921 return;
922 ++MsymI;
923 }
924 };
925 Skip();
926
927 std::set<const Comdat *> NonPrevailingComdats;
928 SmallSet<StringRef, 2> NonPrevailingAsmSymbols;
929 for (const InputFile::Symbol &Sym : Syms) {
930 assert(!Res.empty());
931 const SymbolResolution &R = Res.consume_front();
932
933 assert(MsymI != MsymE);
934 ModuleSymbolTable::Symbol Msym = *MsymI++;
935 Skip();
936
937 if (GlobalValue *GV = dyn_cast_if_present<GlobalValue *>(Msym)) {
938 if (R.Prevailing) {
939 if (Sym.isUndefined())
940 continue;
941 Mod.Keep.push_back(GV);
942 // For symbols re-defined with linker -wrap and -defsym options,
943 // set the linkage to weak to inhibit IPO. The linkage will be
944 // restored by the linker.
945 if (R.LinkerRedefined)
946 GV->setLinkage(GlobalValue::WeakAnyLinkage);
947
948 GlobalValue::LinkageTypes OriginalLinkage = GV->getLinkage();
949 if (GlobalValue::isLinkOnceLinkage(OriginalLinkage))
950 GV->setLinkage(GlobalValue::getWeakLinkage(
951 GlobalValue::isLinkOnceODRLinkage(OriginalLinkage)));
952 } else if (isa<GlobalObject>(GV) &&
953 (GV->hasLinkOnceODRLinkage() || GV->hasWeakODRLinkage() ||
954 GV->hasAvailableExternallyLinkage()) &&
955 !AliasedGlobals.count(cast<GlobalObject>(GV))) {
956 // Any of the above three types of linkage indicates that the
957 // chosen prevailing symbol will have the same semantics as this copy of
958 // the symbol, so we may be able to link it with available_externally
959 // linkage. We will decide later whether to do that when we link this
960 // module (in linkRegularLTO), based on whether it is undefined.
961 Mod.Keep.push_back(GV);
963 if (GV->hasComdat())
964 NonPrevailingComdats.insert(GV->getComdat());
965 cast<GlobalObject>(GV)->setComdat(nullptr);
966 }
967
968 // Set the 'local' flag based on the linker resolution for this symbol.
969 if (R.FinalDefinitionInLinkageUnit) {
970 GV->setDSOLocal(true);
971 if (GV->hasDLLImportStorageClass())
972 GV->setDLLStorageClass(GlobalValue::DLLStorageClassTypes::
973 DefaultStorageClass);
974 }
975 } else if (auto *AS =
976 dyn_cast_if_present<ModuleSymbolTable::AsmSymbol *>(Msym)) {
977 // Collect non-prevailing symbols.
978 if (!R.Prevailing)
979 NonPrevailingAsmSymbols.insert(AS->first);
980 } else {
981 llvm_unreachable("unknown symbol type");
982 }
983
984 // Common resolution: collect the maximum size/alignment over all commons.
985 // We also record if we see an instance of a common as prevailing, so that
986 // if none is prevailing we can ignore it later.
987 if (Sym.isCommon()) {
988 // FIXME: We should figure out what to do about commons defined by asm.
989 // For now they aren't reported correctly by ModuleSymbolTable.
990 auto &CommonRes = RegularLTO.Commons[std::string(Sym.getIRName())];
991 CommonRes.Size = std::max(CommonRes.Size, Sym.getCommonSize());
992 if (uint32_t SymAlignValue = Sym.getCommonAlignment()) {
993 CommonRes.Alignment =
994 std::max(Align(SymAlignValue), CommonRes.Alignment);
995 }
996 CommonRes.Prevailing |= R.Prevailing;
997 }
998 }
999
1000 if (!M.getComdatSymbolTable().empty())
1001 for (GlobalValue &GV : M.global_values())
1002 handleNonPrevailingComdat(GV, NonPrevailingComdats);
1003
1004 // Prepend ".lto_discard <sym>, <sym>*" directive to each module inline asm
1005 // block.
1006 if (!M.getModuleInlineAsm().empty()) {
1007 std::string NewIA = ".lto_discard";
1008 if (!NonPrevailingAsmSymbols.empty()) {
1009 // Don't dicard a symbol if there is a live .symver for it.
1011 M, [&](StringRef Name, StringRef Alias) {
1012 if (!NonPrevailingAsmSymbols.count(Alias))
1013 NonPrevailingAsmSymbols.erase(Name);
1014 });
1015 NewIA += " " + llvm::join(NonPrevailingAsmSymbols, ", ");
1016 }
1017 NewIA += "\n";
1018 M.setModuleInlineAsm(NewIA + M.getModuleInlineAsm());
1019 }
1020
1021 assert(MsymI == MsymE);
1022 return std::make_pair(std::move(Mod), Res);
1023}
1024
1025Error LTO::linkRegularLTO(RegularLTOState::AddedModule Mod,
1026 bool LivenessFromIndex) {
1027 std::vector<GlobalValue *> Keep;
1028 for (GlobalValue *GV : Mod.Keep) {
1029 if (LivenessFromIndex && !ThinLTO.CombinedIndex.isGUIDLive(GV->getGUID())) {
1030 if (Function *F = dyn_cast<Function>(GV)) {
1031 if (DiagnosticOutputFile) {
1032 if (Error Err = F->materialize())
1033 return Err;
1034 OptimizationRemarkEmitter ORE(F, nullptr);
1035 ORE.emit(OptimizationRemark(DEBUG_TYPE, "deadfunction", F)
1036 << ore::NV("Function", F)
1037 << " not added to the combined module ");
1038 }
1039 }
1040 continue;
1041 }
1042
1043 if (!GV->hasAvailableExternallyLinkage()) {
1044 Keep.push_back(GV);
1045 continue;
1046 }
1047
1048 // Only link available_externally definitions if we don't already have a
1049 // definition.
1050 GlobalValue *CombinedGV =
1051 RegularLTO.CombinedModule->getNamedValue(GV->getName());
1052 if (CombinedGV && !CombinedGV->isDeclaration())
1053 continue;
1054
1055 Keep.push_back(GV);
1056 }
1057
1058 return RegularLTO.Mover->move(std::move(Mod.M), Keep, nullptr,
1059 /* IsPerformingImport */ false);
1060}
1061
1062// Add a ThinLTO module to the link.
1064LTO::addThinLTO(BitcodeModule BM, ArrayRef<InputFile::Symbol> Syms,
1066 ArrayRef<SymbolResolution> ResTmp = Res;
1067 for (const InputFile::Symbol &Sym : Syms) {
1068 assert(!ResTmp.empty());
1069 const SymbolResolution &R = ResTmp.consume_front();
1070
1071 if (!Sym.getIRName().empty()) {
1073 GlobalValue::getGlobalIdentifier(Sym.getIRName(),
1075 if (R.Prevailing)
1076 ThinLTO.PrevailingModuleForGUID[GUID] = BM.getModuleIdentifier();
1077 }
1078 }
1079
1080 if (Error Err =
1081 BM.readSummary(ThinLTO.CombinedIndex, BM.getModuleIdentifier(),
1082 [&](GlobalValue::GUID GUID) {
1083 return ThinLTO.PrevailingModuleForGUID[GUID] ==
1084 BM.getModuleIdentifier();
1085 }))
1086 return Err;
1087 LLVM_DEBUG(dbgs() << "Module " << BM.getModuleIdentifier() << "\n");
1088
1089 for (const InputFile::Symbol &Sym : Syms) {
1090 assert(!Res.empty());
1091 const SymbolResolution &R = Res.consume_front();
1092
1093 if (!Sym.getIRName().empty()) {
1095 GlobalValue::getGlobalIdentifier(Sym.getIRName(),
1097 if (R.Prevailing) {
1098 assert(ThinLTO.PrevailingModuleForGUID[GUID] ==
1099 BM.getModuleIdentifier());
1100
1101 // For linker redefined symbols (via --wrap or --defsym) we want to
1102 // switch the linkage to `weak` to prevent IPOs from happening.
1103 // Find the summary in the module for this very GV and record the new
1104 // linkage so that we can switch it when we import the GV.
1105 if (R.LinkerRedefined)
1106 if (auto S = ThinLTO.CombinedIndex.findSummaryInModule(
1107 GUID, BM.getModuleIdentifier()))
1108 S->setLinkage(GlobalValue::WeakAnyLinkage);
1109 }
1110
1111 // If the linker resolved the symbol to a local definition then mark it
1112 // as local in the summary for the module we are adding.
1113 if (R.FinalDefinitionInLinkageUnit) {
1114 if (auto S = ThinLTO.CombinedIndex.findSummaryInModule(
1115 GUID, BM.getModuleIdentifier())) {
1116 S->setDSOLocal(true);
1117 }
1118 }
1119 }
1120 }
1121
1122 if (!ThinLTO.ModuleMap.insert({BM.getModuleIdentifier(), BM}).second)
1123 return make_error<StringError>(
1124 "Expected at most one ThinLTO module per bitcode file",
1126
1127 if (!Conf.ThinLTOModulesToCompile.empty()) {
1128 if (!ThinLTO.ModulesToCompile)
1129 ThinLTO.ModulesToCompile = ModuleMapType();
1130 // This is a fuzzy name matching where only modules with name containing the
1131 // specified switch values are going to be compiled.
1132 for (const std::string &Name : Conf.ThinLTOModulesToCompile) {
1133 if (BM.getModuleIdentifier().contains(Name)) {
1134 ThinLTO.ModulesToCompile->insert({BM.getModuleIdentifier(), BM});
1135 LLVM_DEBUG(dbgs() << "[ThinLTO] Selecting " << BM.getModuleIdentifier()
1136 << " to compile\n");
1137 }
1138 }
1139 }
1140
1141 return Res;
1142}
1143
1144unsigned LTO::getMaxTasks() const {
1145 CalledGetMaxTasks = true;
1146 auto ModuleCount = ThinLTO.ModulesToCompile ? ThinLTO.ModulesToCompile->size()
1147 : ThinLTO.ModuleMap.size();
1148 return RegularLTO.ParallelCodeGenParallelismLevel + ModuleCount;
1149}
1150
1151// If only some of the modules were split, we cannot correctly handle
1152// code that contains type tests or type checked loads.
1153Error LTO::checkPartiallySplit() {
1154 if (!ThinLTO.CombinedIndex.partiallySplitLTOUnits())
1155 return Error::success();
1156
1157 const Module *Combined = RegularLTO.CombinedModule.get();
1158 Function *TypeTestFunc =
1159 Intrinsic::getDeclarationIfExists(Combined, Intrinsic::type_test);
1160 Function *TypeCheckedLoadFunc =
1161 Intrinsic::getDeclarationIfExists(Combined, Intrinsic::type_checked_load);
1162 Function *TypeCheckedLoadRelativeFunc = Intrinsic::getDeclarationIfExists(
1163 Combined, Intrinsic::type_checked_load_relative);
1164
1165 // First check if there are type tests / type checked loads in the
1166 // merged regular LTO module IR.
1167 if ((TypeTestFunc && !TypeTestFunc->use_empty()) ||
1168 (TypeCheckedLoadFunc && !TypeCheckedLoadFunc->use_empty()) ||
1169 (TypeCheckedLoadRelativeFunc &&
1170 !TypeCheckedLoadRelativeFunc->use_empty()))
1171 return make_error<StringError>(
1172 "inconsistent LTO Unit splitting (recompile with -fsplit-lto-unit)",
1174
1175 // Otherwise check if there are any recorded in the combined summary from the
1176 // ThinLTO modules.
1177 for (auto &P : ThinLTO.CombinedIndex) {
1178 for (auto &S : P.second.SummaryList) {
1179 auto *FS = dyn_cast<FunctionSummary>(S.get());
1180 if (!FS)
1181 continue;
1182 if (!FS->type_test_assume_vcalls().empty() ||
1183 !FS->type_checked_load_vcalls().empty() ||
1184 !FS->type_test_assume_const_vcalls().empty() ||
1185 !FS->type_checked_load_const_vcalls().empty() ||
1186 !FS->type_tests().empty())
1187 return make_error<StringError>(
1188 "inconsistent LTO Unit splitting (recompile with -fsplit-lto-unit)",
1190 }
1191 }
1192 return Error::success();
1193}
1194
1196 // Compute "dead" symbols, we don't want to import/export these!
1197 DenseSet<GlobalValue::GUID> GUIDPreservedSymbols;
1198 DenseMap<GlobalValue::GUID, PrevailingType> GUIDPrevailingResolutions;
1199 for (auto &Res : *GlobalResolutions) {
1200 // Normally resolution have IR name of symbol. We can do nothing here
1201 // otherwise. See comments in GlobalResolution struct for more details.
1202 if (Res.second.IRName.empty())
1203 continue;
1204
1206 GlobalValue::dropLLVMManglingEscape(Res.second.IRName));
1207
1208 if (Res.second.VisibleOutsideSummary && Res.second.Prevailing)
1209 GUIDPreservedSymbols.insert(GUID);
1210
1211 if (Res.second.ExportDynamic)
1212 DynamicExportSymbols.insert(GUID);
1213
1214 GUIDPrevailingResolutions[GUID] =
1215 Res.second.Prevailing ? PrevailingType::Yes : PrevailingType::No;
1216 }
1217
1218 auto isPrevailing = [&](GlobalValue::GUID G) {
1219 auto It = GUIDPrevailingResolutions.find(G);
1220 if (It == GUIDPrevailingResolutions.end())
1222 return It->second;
1223 };
1224 computeDeadSymbolsWithConstProp(ThinLTO.CombinedIndex, GUIDPreservedSymbols,
1225 isPrevailing, Conf.OptLevel > 0);
1226
1227 // Setup output file to emit statistics.
1228 auto StatsFileOrErr = setupStatsFile(Conf.StatsFile);
1229 if (!StatsFileOrErr)
1230 return StatsFileOrErr.takeError();
1231 std::unique_ptr<ToolOutputFile> StatsFile = std::move(StatsFileOrErr.get());
1232
1233 // TODO: Ideally this would be controlled automatically by detecting that we
1234 // are linking with an allocator that supports these interfaces, rather than
1235 // an internal option (which would still be needed for tests, however). For
1236 // example, if the library exported a symbol like __malloc_hot_cold the linker
1237 // could recognize that and set a flag in the lto::Config.
1239 ThinLTO.CombinedIndex.setWithSupportsHotColdNew();
1240
1241 Error Result = runRegularLTO(AddStream);
1242 if (!Result)
1243 // This will reset the GlobalResolutions optional once done with it to
1244 // reduce peak memory before importing.
1245 Result = runThinLTO(AddStream, Cache, GUIDPreservedSymbols);
1246
1247 if (StatsFile)
1248 PrintStatisticsJSON(StatsFile->os());
1249
1250 return Result;
1251}
1252
1254 const ModuleSummaryIndex &Index) {
1255 if (Index.withSupportsHotColdNew())
1256 return;
1257
1258 // The profile matcher applies hotness attributes directly for allocations,
1259 // and those will cause us to generate calls to the hot/cold interfaces
1260 // unconditionally. If supports-hot-cold-new was not enabled in the LTO
1261 // link then assume we don't want these calls (e.g. not linking with
1262 // the appropriate library, or otherwise trying to disable this behavior).
1263 for (auto &F : Mod) {
1264 for (auto &BB : F) {
1265 for (auto &I : BB) {
1266 auto *CI = dyn_cast<CallBase>(&I);
1267 if (!CI)
1268 continue;
1269 if (CI->hasFnAttr("memprof"))
1270 CI->removeFnAttr("memprof");
1271 // Strip off all memprof metadata as it is no longer needed.
1272 // Importantly, this avoids the addition of new memprof attributes
1273 // after inlining propagation.
1274 // TODO: If we support additional types of MemProf metadata beyond hot
1275 // and cold, we will need to update the metadata based on the allocator
1276 // APIs supported instead of completely stripping all.
1277 CI->setMetadata(LLVMContext::MD_memprof, nullptr);
1278 CI->setMetadata(LLVMContext::MD_callsite, nullptr);
1279 }
1280 }
1281 }
1282}
1283
1284Error LTO::runRegularLTO(AddStreamFn AddStream) {
1285 // Setup optimization remarks.
1286 auto DiagFileOrErr = lto::setupLLVMOptimizationRemarks(
1287 RegularLTO.CombinedModule->getContext(), Conf.RemarksFilename,
1290 LLVM_DEBUG(dbgs() << "Running regular LTO\n");
1291 if (!DiagFileOrErr)
1292 return DiagFileOrErr.takeError();
1293 DiagnosticOutputFile = std::move(*DiagFileOrErr);
1294
1295 // Finalize linking of regular LTO modules containing summaries now that
1296 // we have computed liveness information.
1297 for (auto &M : RegularLTO.ModsWithSummaries)
1298 if (Error Err = linkRegularLTO(std::move(M),
1299 /*LivenessFromIndex=*/true))
1300 return Err;
1301
1302 // Ensure we don't have inconsistently split LTO units with type tests.
1303 // FIXME: this checks both LTO and ThinLTO. It happens to work as we take
1304 // this path both cases but eventually this should be split into two and
1305 // do the ThinLTO checks in `runThinLTO`.
1306 if (Error Err = checkPartiallySplit())
1307 return Err;
1308
1309 // Make sure commons have the right size/alignment: we kept the largest from
1310 // all the prevailing when adding the inputs, and we apply it here.
1311 const DataLayout &DL = RegularLTO.CombinedModule->getDataLayout();
1312 for (auto &I : RegularLTO.Commons) {
1313 if (!I.second.Prevailing)
1314 // Don't do anything if no instance of this common was prevailing.
1315 continue;
1316 GlobalVariable *OldGV = RegularLTO.CombinedModule->getNamedGlobal(I.first);
1317 if (OldGV && DL.getTypeAllocSize(OldGV->getValueType()) == I.second.Size) {
1318 // Don't create a new global if the type is already correct, just make
1319 // sure the alignment is correct.
1320 OldGV->setAlignment(I.second.Alignment);
1321 continue;
1322 }
1323 ArrayType *Ty =
1324 ArrayType::get(Type::getInt8Ty(RegularLTO.Ctx), I.second.Size);
1325 auto *GV = new GlobalVariable(*RegularLTO.CombinedModule, Ty, false,
1328 GV->setAlignment(I.second.Alignment);
1329 if (OldGV) {
1330 OldGV->replaceAllUsesWith(GV);
1331 GV->takeName(OldGV);
1332 OldGV->eraseFromParent();
1333 } else {
1334 GV->setName(I.first);
1335 }
1336 }
1337
1338 updateMemProfAttributes(*RegularLTO.CombinedModule, ThinLTO.CombinedIndex);
1339
1340 bool WholeProgramVisibilityEnabledInLTO =
1342 // If validation is enabled, upgrade visibility only when all vtables
1343 // have typeinfos.
1345
1346 // This returns true when the name is local or not defined. Locals are
1347 // expected to be handled separately.
1348 auto IsVisibleToRegularObj = [&](StringRef name) {
1349 auto It = GlobalResolutions->find(name);
1350 return (It == GlobalResolutions->end() ||
1351 It->second.VisibleOutsideSummary || !It->second.Prevailing);
1352 };
1353
1354 // If allowed, upgrade public vcall visibility metadata to linkage unit
1355 // visibility before whole program devirtualization in the optimizer.
1357 *RegularLTO.CombinedModule, WholeProgramVisibilityEnabledInLTO,
1358 DynamicExportSymbols, Conf.ValidateAllVtablesHaveTypeInfos,
1359 IsVisibleToRegularObj);
1360 updatePublicTypeTestCalls(*RegularLTO.CombinedModule,
1361 WholeProgramVisibilityEnabledInLTO);
1362
1363 if (Conf.PreOptModuleHook &&
1364 !Conf.PreOptModuleHook(0, *RegularLTO.CombinedModule))
1365 return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
1366
1367 if (!Conf.CodeGenOnly) {
1368 for (const auto &R : *GlobalResolutions) {
1369 GlobalValue *GV =
1370 RegularLTO.CombinedModule->getNamedValue(R.second.IRName);
1371 if (!R.second.isPrevailingIRSymbol())
1372 continue;
1373 if (R.second.Partition != 0 &&
1374 R.second.Partition != GlobalResolution::External)
1375 continue;
1376
1377 // Ignore symbols defined in other partitions.
1378 // Also skip declarations, which are not allowed to have internal linkage.
1379 if (!GV || GV->hasLocalLinkage() || GV->isDeclaration())
1380 continue;
1381
1382 // Symbols that are marked DLLImport or DLLExport should not be
1383 // internalized, as they are either externally visible or referencing
1384 // external symbols. Symbols that have AvailableExternally or Appending
1385 // linkage might be used by future passes and should be kept as is.
1386 // These linkages are seen in Unified regular LTO, because the process
1387 // of creating split LTO units introduces symbols with that linkage into
1388 // one of the created modules. Normally, only the ThinLTO backend would
1389 // compile this module, but Unified Regular LTO processes both
1390 // modules created by the splitting process as regular LTO modules.
1391 if ((LTOMode == LTOKind::LTOK_UnifiedRegular) &&
1394 continue;
1395
1396 GV->setUnnamedAddr(R.second.UnnamedAddr ? GlobalValue::UnnamedAddr::Global
1398 if (EnableLTOInternalization && R.second.Partition == 0)
1400 }
1401
1402 if (Conf.PostInternalizeModuleHook &&
1403 !Conf.PostInternalizeModuleHook(0, *RegularLTO.CombinedModule))
1404 return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
1405 }
1406
1407 if (!RegularLTO.EmptyCombinedModule || Conf.AlwaysEmitRegularLTOObj) {
1408 if (Error Err =
1409 backend(Conf, AddStream, RegularLTO.ParallelCodeGenParallelismLevel,
1410 *RegularLTO.CombinedModule, ThinLTO.CombinedIndex))
1411 return Err;
1412 }
1413
1414 return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
1415}
1416
1418 RTLIB::RuntimeLibcallsInfo Libcalls(TT);
1419 SmallVector<const char *> LibcallSymbols;
1420 ArrayRef<RTLIB::LibcallImpl> LibcallImpls = Libcalls.getLibcallImpls();
1421 LibcallSymbols.reserve(LibcallImpls.size());
1422
1423 for (RTLIB::LibcallImpl Impl : LibcallImpls) {
1424 if (Impl != RTLIB::Unsupported)
1425 LibcallSymbols.push_back(Libcalls.getLibcallImplName(Impl).data());
1426 }
1427
1428 return LibcallSymbols;
1429}
1430
1432 const FunctionImporter::ImportMapTy &ImportList, llvm::StringRef ModulePath,
1433 const std::string &NewModulePath) const {
1434 return emitFiles(ImportList, ModulePath, NewModulePath,
1435 NewModulePath + ".thinlto.bc",
1436 /*ImportsFiles=*/std::nullopt);
1437}
1438
1440 const FunctionImporter::ImportMapTy &ImportList, llvm::StringRef ModulePath,
1441 const std::string &NewModulePath, StringRef SummaryPath,
1442 std::optional<std::reference_wrapper<ImportsFilesContainer>> ImportsFiles)
1443 const {
1444 ModuleToSummariesForIndexTy ModuleToSummariesForIndex;
1445 GVSummaryPtrSet DeclarationSummaries;
1446
1447 std::error_code EC;
1449 ImportList, ModuleToSummariesForIndex,
1450 DeclarationSummaries);
1451
1453 if (EC)
1454 return createFileError("cannot open " + Twine(SummaryPath), EC);
1455
1456 writeIndexToFile(CombinedIndex, OS, &ModuleToSummariesForIndex,
1457 &DeclarationSummaries);
1458
1460 Error ImportsFilesError = EmitImportsFiles(
1461 ModulePath, NewModulePath + ".imports", ModuleToSummariesForIndex);
1462 if (ImportsFilesError)
1463 return ImportsFilesError;
1464 }
1465
1466 // Optionally, store the imports files.
1467 if (ImportsFiles)
1469 ModulePath, ModuleToSummariesForIndex,
1470 [&](StringRef M) { ImportsFiles->get().push_back(M.str()); });
1471
1472 return Error::success();
1473}
1474
1475namespace {
1476/// Base class for ThinLTO backends that perform code generation and insert the
1477/// generated files back into the link.
1478class CGThinBackend : public ThinBackendProc {
1479protected:
1480 AddStreamFn AddStream;
1481 DenseSet<GlobalValue::GUID> CfiFunctionDefs;
1482 DenseSet<GlobalValue::GUID> CfiFunctionDecls;
1483 bool ShouldEmitIndexFiles;
1484
1485public:
1486 CGThinBackend(
1487 const Config &Conf, ModuleSummaryIndex &CombinedIndex,
1488 const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1489 AddStreamFn AddStream, lto::IndexWriteCallback OnWrite,
1490 bool ShouldEmitIndexFiles, bool ShouldEmitImportsFiles,
1491 ThreadPoolStrategy ThinLTOParallelism)
1492 : ThinBackendProc(Conf, CombinedIndex, ModuleToDefinedGVSummaries,
1493 OnWrite, ShouldEmitImportsFiles, ThinLTOParallelism),
1494 AddStream(std::move(AddStream)),
1495 ShouldEmitIndexFiles(ShouldEmitIndexFiles) {
1496 auto &Defs = CombinedIndex.cfiFunctionDefs();
1497 CfiFunctionDefs.insert_range(Defs.guids());
1498 auto &Decls = CombinedIndex.cfiFunctionDecls();
1499 CfiFunctionDecls.insert_range(Decls.guids());
1500 }
1501};
1502
1503/// This backend performs code generation by scheduling a job to run on
1504/// an in-process thread when invoked for each task.
1505class InProcessThinBackend : public CGThinBackend {
1506protected:
1507 FileCache Cache;
1508
1509public:
1510 InProcessThinBackend(
1511 const Config &Conf, ModuleSummaryIndex &CombinedIndex,
1512 ThreadPoolStrategy ThinLTOParallelism,
1513 const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1514 AddStreamFn AddStream, FileCache Cache, lto::IndexWriteCallback OnWrite,
1515 bool ShouldEmitIndexFiles, bool ShouldEmitImportsFiles)
1516 : CGThinBackend(Conf, CombinedIndex, ModuleToDefinedGVSummaries,
1517 AddStream, OnWrite, ShouldEmitIndexFiles,
1518 ShouldEmitImportsFiles, ThinLTOParallelism),
1519 Cache(std::move(Cache)) {}
1520
1521 virtual Error runThinLTOBackendThread(
1522 AddStreamFn AddStream, FileCache Cache, unsigned Task, BitcodeModule BM,
1523 ModuleSummaryIndex &CombinedIndex,
1524 const FunctionImporter::ImportMapTy &ImportList,
1525 const FunctionImporter::ExportSetTy &ExportList,
1526 const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
1527 const GVSummaryMapTy &DefinedGlobals,
1529 auto RunThinBackend = [&](AddStreamFn AddStream) {
1530 LTOLLVMContext BackendContext(Conf);
1531 Expected<std::unique_ptr<Module>> MOrErr = BM.parseModule(BackendContext);
1532 if (!MOrErr)
1533 return MOrErr.takeError();
1534
1535 return thinBackend(Conf, Task, AddStream, **MOrErr, CombinedIndex,
1536 ImportList, DefinedGlobals, &ModuleMap,
1537 Conf.CodeGenOnly);
1538 };
1539
1540 auto ModuleID = BM.getModuleIdentifier();
1541
1542 if (ShouldEmitIndexFiles) {
1543 if (auto E = emitFiles(ImportList, ModuleID, ModuleID.str()))
1544 return E;
1545 }
1546
1547 if (!Cache.isValid() || !CombinedIndex.modulePaths().count(ModuleID) ||
1548 all_of(CombinedIndex.getModuleHash(ModuleID),
1549 [](uint32_t V) { return V == 0; }))
1550 // Cache disabled or no entry for this module in the combined index or
1551 // no module hash.
1552 return RunThinBackend(AddStream);
1553
1554 // The module may be cached, this helps handling it.
1555 std::string Key = computeLTOCacheKey(
1556 Conf, CombinedIndex, ModuleID, ImportList, ExportList, ResolvedODR,
1557 DefinedGlobals, CfiFunctionDefs, CfiFunctionDecls);
1558 Expected<AddStreamFn> CacheAddStreamOrErr = Cache(Task, Key, ModuleID);
1559 if (Error Err = CacheAddStreamOrErr.takeError())
1560 return Err;
1561 AddStreamFn &CacheAddStream = *CacheAddStreamOrErr;
1562 if (CacheAddStream)
1563 return RunThinBackend(CacheAddStream);
1564
1565 return Error::success();
1566 }
1567
1568 Error start(
1569 unsigned Task, BitcodeModule BM,
1570 const FunctionImporter::ImportMapTy &ImportList,
1571 const FunctionImporter::ExportSetTy &ExportList,
1572 const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
1573 MapVector<StringRef, BitcodeModule> &ModuleMap) override {
1574 StringRef ModulePath = BM.getModuleIdentifier();
1575 assert(ModuleToDefinedGVSummaries.count(ModulePath));
1576 const GVSummaryMapTy &DefinedGlobals =
1577 ModuleToDefinedGVSummaries.find(ModulePath)->second;
1578 BackendThreadPool.async(
1579 [=](BitcodeModule BM, ModuleSummaryIndex &CombinedIndex,
1580 const FunctionImporter::ImportMapTy &ImportList,
1581 const FunctionImporter::ExportSetTy &ExportList,
1582 const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>
1583 &ResolvedODR,
1584 const GVSummaryMapTy &DefinedGlobals,
1586 if (LLVM_ENABLE_THREADS && Conf.TimeTraceEnabled)
1588 "thin backend");
1589 Error E = runThinLTOBackendThread(
1590 AddStream, Cache, Task, BM, CombinedIndex, ImportList, ExportList,
1591 ResolvedODR, DefinedGlobals, ModuleMap);
1592 if (E) {
1593 std::unique_lock<std::mutex> L(ErrMu);
1594 if (Err)
1595 Err = joinErrors(std::move(*Err), std::move(E));
1596 else
1597 Err = std::move(E);
1598 }
1599 if (LLVM_ENABLE_THREADS && Conf.TimeTraceEnabled)
1601 },
1602 BM, std::ref(CombinedIndex), std::ref(ImportList), std::ref(ExportList),
1603 std::ref(ResolvedODR), std::ref(DefinedGlobals), std::ref(ModuleMap));
1604
1605 if (OnWrite)
1606 OnWrite(std::string(ModulePath));
1607 return Error::success();
1608 }
1609};
1610
1611/// This backend is utilized in the first round of a two-codegen round process.
1612/// It first saves optimized bitcode files to disk before the codegen process
1613/// begins. After codegen, it stores the resulting object files in a scratch
1614/// buffer. Note the codegen data stored in the scratch buffer will be extracted
1615/// and merged in the subsequent step.
1616class FirstRoundThinBackend : public InProcessThinBackend {
1617 AddStreamFn IRAddStream;
1618 FileCache IRCache;
1619
1620public:
1621 FirstRoundThinBackend(
1622 const Config &Conf, ModuleSummaryIndex &CombinedIndex,
1623 ThreadPoolStrategy ThinLTOParallelism,
1624 const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1625 AddStreamFn CGAddStream, FileCache CGCache, AddStreamFn IRAddStream,
1626 FileCache IRCache)
1627 : InProcessThinBackend(Conf, CombinedIndex, ThinLTOParallelism,
1628 ModuleToDefinedGVSummaries, std::move(CGAddStream),
1629 std::move(CGCache), /*OnWrite=*/nullptr,
1630 /*ShouldEmitIndexFiles=*/false,
1631 /*ShouldEmitImportsFiles=*/false),
1632 IRAddStream(std::move(IRAddStream)), IRCache(std::move(IRCache)) {}
1633
1634 Error runThinLTOBackendThread(
1635 AddStreamFn CGAddStream, FileCache CGCache, unsigned Task,
1636 BitcodeModule BM, ModuleSummaryIndex &CombinedIndex,
1637 const FunctionImporter::ImportMapTy &ImportList,
1638 const FunctionImporter::ExportSetTy &ExportList,
1639 const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
1640 const GVSummaryMapTy &DefinedGlobals,
1641 MapVector<StringRef, BitcodeModule> &ModuleMap) override {
1642 auto RunThinBackend = [&](AddStreamFn CGAddStream,
1643 AddStreamFn IRAddStream) {
1644 LTOLLVMContext BackendContext(Conf);
1645 Expected<std::unique_ptr<Module>> MOrErr = BM.parseModule(BackendContext);
1646 if (!MOrErr)
1647 return MOrErr.takeError();
1648
1649 return thinBackend(Conf, Task, CGAddStream, **MOrErr, CombinedIndex,
1650 ImportList, DefinedGlobals, &ModuleMap,
1651 Conf.CodeGenOnly, IRAddStream);
1652 };
1653
1654 auto ModuleID = BM.getModuleIdentifier();
1655 // Like InProcessThinBackend, we produce index files as needed for
1656 // FirstRoundThinBackend. However, these files are not generated for
1657 // SecondRoundThinBackend.
1658 if (ShouldEmitIndexFiles) {
1659 if (auto E = emitFiles(ImportList, ModuleID, ModuleID.str()))
1660 return E;
1661 }
1662
1663 assert((CGCache.isValid() == IRCache.isValid()) &&
1664 "Both caches for CG and IR should have matching availability");
1665 if (!CGCache.isValid() || !CombinedIndex.modulePaths().count(ModuleID) ||
1666 all_of(CombinedIndex.getModuleHash(ModuleID),
1667 [](uint32_t V) { return V == 0; }))
1668 // Cache disabled or no entry for this module in the combined index or
1669 // no module hash.
1670 return RunThinBackend(CGAddStream, IRAddStream);
1671
1672 // Get CGKey for caching object in CGCache.
1673 std::string CGKey = computeLTOCacheKey(
1674 Conf, CombinedIndex, ModuleID, ImportList, ExportList, ResolvedODR,
1675 DefinedGlobals, CfiFunctionDefs, CfiFunctionDecls);
1676 Expected<AddStreamFn> CacheCGAddStreamOrErr =
1677 CGCache(Task, CGKey, ModuleID);
1678 if (Error Err = CacheCGAddStreamOrErr.takeError())
1679 return Err;
1680 AddStreamFn &CacheCGAddStream = *CacheCGAddStreamOrErr;
1681
1682 // Get IRKey for caching (optimized) IR in IRCache with an extra ID.
1683 std::string IRKey = recomputeLTOCacheKey(CGKey, /*ExtraID=*/"IR");
1684 Expected<AddStreamFn> CacheIRAddStreamOrErr =
1685 IRCache(Task, IRKey, ModuleID);
1686 if (Error Err = CacheIRAddStreamOrErr.takeError())
1687 return Err;
1688 AddStreamFn &CacheIRAddStream = *CacheIRAddStreamOrErr;
1689
1690 // Ideally, both CG and IR caching should be synchronized. However, in
1691 // practice, their availability may differ due to different expiration
1692 // times. Therefore, if either cache is missing, the backend process is
1693 // triggered.
1694 if (CacheCGAddStream || CacheIRAddStream) {
1695 LLVM_DEBUG(dbgs() << "[FirstRound] Cache Miss for "
1696 << BM.getModuleIdentifier() << "\n");
1697 return RunThinBackend(CacheCGAddStream ? CacheCGAddStream : CGAddStream,
1698 CacheIRAddStream ? CacheIRAddStream : IRAddStream);
1699 }
1700
1701 return Error::success();
1702 }
1703};
1704
1705/// This backend operates in the second round of a two-codegen round process.
1706/// It starts by reading the optimized bitcode files that were saved during the
1707/// first round. The backend then executes the codegen only to further optimize
1708/// the code, utilizing the codegen data merged from the first round. Finally,
1709/// it writes the resulting object files as usual.
1710class SecondRoundThinBackend : public InProcessThinBackend {
1711 std::unique_ptr<SmallVector<StringRef>> IRFiles;
1712 stable_hash CombinedCGDataHash;
1713
1714public:
1715 SecondRoundThinBackend(
1716 const Config &Conf, ModuleSummaryIndex &CombinedIndex,
1717 ThreadPoolStrategy ThinLTOParallelism,
1718 const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1719 AddStreamFn AddStream, FileCache Cache,
1720 std::unique_ptr<SmallVector<StringRef>> IRFiles,
1721 stable_hash CombinedCGDataHash)
1722 : InProcessThinBackend(Conf, CombinedIndex, ThinLTOParallelism,
1723 ModuleToDefinedGVSummaries, std::move(AddStream),
1724 std::move(Cache),
1725 /*OnWrite=*/nullptr,
1726 /*ShouldEmitIndexFiles=*/false,
1727 /*ShouldEmitImportsFiles=*/false),
1728 IRFiles(std::move(IRFiles)), CombinedCGDataHash(CombinedCGDataHash) {}
1729
1730 virtual Error runThinLTOBackendThread(
1731 AddStreamFn AddStream, FileCache Cache, unsigned Task, BitcodeModule BM,
1732 ModuleSummaryIndex &CombinedIndex,
1733 const FunctionImporter::ImportMapTy &ImportList,
1734 const FunctionImporter::ExportSetTy &ExportList,
1735 const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
1736 const GVSummaryMapTy &DefinedGlobals,
1737 MapVector<StringRef, BitcodeModule> &ModuleMap) override {
1738 auto RunThinBackend = [&](AddStreamFn AddStream) {
1739 LTOLLVMContext BackendContext(Conf);
1740 std::unique_ptr<Module> LoadedModule =
1741 cgdata::loadModuleForTwoRounds(BM, Task, BackendContext, *IRFiles);
1742
1743 return thinBackend(Conf, Task, AddStream, *LoadedModule, CombinedIndex,
1744 ImportList, DefinedGlobals, &ModuleMap,
1745 /*CodeGenOnly=*/true);
1746 };
1747
1748 auto ModuleID = BM.getModuleIdentifier();
1749 if (!Cache.isValid() || !CombinedIndex.modulePaths().count(ModuleID) ||
1750 all_of(CombinedIndex.getModuleHash(ModuleID),
1751 [](uint32_t V) { return V == 0; }))
1752 // Cache disabled or no entry for this module in the combined index or
1753 // no module hash.
1754 return RunThinBackend(AddStream);
1755
1756 // Get Key for caching the final object file in Cache with the combined
1757 // CGData hash.
1758 std::string Key = computeLTOCacheKey(
1759 Conf, CombinedIndex, ModuleID, ImportList, ExportList, ResolvedODR,
1760 DefinedGlobals, CfiFunctionDefs, CfiFunctionDecls);
1762 /*ExtraID=*/std::to_string(CombinedCGDataHash));
1763 Expected<AddStreamFn> CacheAddStreamOrErr = Cache(Task, Key, ModuleID);
1764 if (Error Err = CacheAddStreamOrErr.takeError())
1765 return Err;
1766 AddStreamFn &CacheAddStream = *CacheAddStreamOrErr;
1767
1768 if (CacheAddStream) {
1769 LLVM_DEBUG(dbgs() << "[SecondRound] Cache Miss for "
1770 << BM.getModuleIdentifier() << "\n");
1771 return RunThinBackend(CacheAddStream);
1772 }
1773
1774 return Error::success();
1775 }
1776};
1777} // end anonymous namespace
1778
1781 bool ShouldEmitIndexFiles,
1782 bool ShouldEmitImportsFiles) {
1783 auto Func =
1784 [=](const Config &Conf, ModuleSummaryIndex &CombinedIndex,
1785 const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1786 AddStreamFn AddStream, FileCache Cache) {
1787 return std::make_unique<InProcessThinBackend>(
1788 Conf, CombinedIndex, Parallelism, ModuleToDefinedGVSummaries,
1789 AddStream, Cache, OnWrite, ShouldEmitIndexFiles,
1790 ShouldEmitImportsFiles);
1791 };
1792 return ThinBackend(Func, Parallelism);
1793}
1794
1796 if (!TheTriple.isOSDarwin())
1797 return "";
1798 if (TheTriple.getArch() == Triple::x86_64)
1799 return "core2";
1800 if (TheTriple.getArch() == Triple::x86)
1801 return "yonah";
1802 if (TheTriple.isArm64e())
1803 return "apple-a12";
1804 if (TheTriple.getArch() == Triple::aarch64 ||
1805 TheTriple.getArch() == Triple::aarch64_32)
1806 return "cyclone";
1807 return "";
1808}
1809
1810// Given the original \p Path to an output file, replace any path
1811// prefix matching \p OldPrefix with \p NewPrefix. Also, create the
1812// resulting directory if it does not yet exist.
1814 StringRef NewPrefix) {
1815 if (OldPrefix.empty() && NewPrefix.empty())
1816 return std::string(Path);
1817 SmallString<128> NewPath(Path);
1818 llvm::sys::path::replace_path_prefix(NewPath, OldPrefix, NewPrefix);
1819 StringRef ParentPath = llvm::sys::path::parent_path(NewPath.str());
1820 if (!ParentPath.empty()) {
1821 // Make sure the new directory exists, creating it if necessary.
1822 if (std::error_code EC = llvm::sys::fs::create_directories(ParentPath))
1823 llvm::errs() << "warning: could not create directory '" << ParentPath
1824 << "': " << EC.message() << '\n';
1825 }
1826 return std::string(NewPath);
1827}
1828
1829namespace {
1830class WriteIndexesThinBackend : public ThinBackendProc {
1831 std::string OldPrefix, NewPrefix, NativeObjectPrefix;
1832 raw_fd_ostream *LinkedObjectsFile;
1833
1834public:
1835 WriteIndexesThinBackend(
1836 const Config &Conf, ModuleSummaryIndex &CombinedIndex,
1837 ThreadPoolStrategy ThinLTOParallelism,
1838 const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1839 std::string OldPrefix, std::string NewPrefix,
1840 std::string NativeObjectPrefix, bool ShouldEmitImportsFiles,
1841 raw_fd_ostream *LinkedObjectsFile, lto::IndexWriteCallback OnWrite)
1842 : ThinBackendProc(Conf, CombinedIndex, ModuleToDefinedGVSummaries,
1843 OnWrite, ShouldEmitImportsFiles, ThinLTOParallelism),
1844 OldPrefix(OldPrefix), NewPrefix(NewPrefix),
1845 NativeObjectPrefix(NativeObjectPrefix),
1846 LinkedObjectsFile(LinkedObjectsFile) {}
1847
1848 Error start(
1849 unsigned Task, BitcodeModule BM,
1850 const FunctionImporter::ImportMapTy &ImportList,
1851 const FunctionImporter::ExportSetTy &ExportList,
1852 const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
1853 MapVector<StringRef, BitcodeModule> &ModuleMap) override {
1854 StringRef ModulePath = BM.getModuleIdentifier();
1855
1856 // The contents of this file may be used as input to a native link, and must
1857 // therefore contain the processed modules in a determinstic order that
1858 // match the order they are provided on the command line. For that reason,
1859 // we cannot include this in the asynchronously executed lambda below.
1860 if (LinkedObjectsFile) {
1861 std::string ObjectPrefix =
1862 NativeObjectPrefix.empty() ? NewPrefix : NativeObjectPrefix;
1863 std::string LinkedObjectsFilePath =
1864 getThinLTOOutputFile(ModulePath, OldPrefix, ObjectPrefix);
1865 *LinkedObjectsFile << LinkedObjectsFilePath << '\n';
1866 }
1867
1868 BackendThreadPool.async(
1869 [this](const StringRef ModulePath,
1870 const FunctionImporter::ImportMapTy &ImportList,
1871 const std::string &OldPrefix, const std::string &NewPrefix) {
1872 std::string NewModulePath =
1873 getThinLTOOutputFile(ModulePath, OldPrefix, NewPrefix);
1874 auto E = emitFiles(ImportList, ModulePath, NewModulePath);
1875 if (E) {
1876 std::unique_lock<std::mutex> L(ErrMu);
1877 if (Err)
1878 Err = joinErrors(std::move(*Err), std::move(E));
1879 else
1880 Err = std::move(E);
1881 return;
1882 }
1883 },
1884 ModulePath, ImportList, OldPrefix, NewPrefix);
1885
1886 if (OnWrite)
1887 OnWrite(std::string(ModulePath));
1888 return Error::success();
1889 }
1890
1891 bool isSensitiveToInputOrder() override {
1892 // The order which modules are written to LinkedObjectsFile should be
1893 // deterministic and match the order they are passed on the command line.
1894 return true;
1895 }
1896};
1897} // end anonymous namespace
1898
1900 ThreadPoolStrategy Parallelism, std::string OldPrefix,
1901 std::string NewPrefix, std::string NativeObjectPrefix,
1902 bool ShouldEmitImportsFiles, raw_fd_ostream *LinkedObjectsFile,
1903 IndexWriteCallback OnWrite) {
1904 auto Func =
1905 [=](const Config &Conf, ModuleSummaryIndex &CombinedIndex,
1906 const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1907 AddStreamFn AddStream, FileCache Cache) {
1908 return std::make_unique<WriteIndexesThinBackend>(
1909 Conf, CombinedIndex, Parallelism, ModuleToDefinedGVSummaries,
1910 OldPrefix, NewPrefix, NativeObjectPrefix, ShouldEmitImportsFiles,
1911 LinkedObjectsFile, OnWrite);
1912 };
1913 return ThinBackend(Func, Parallelism);
1914}
1915
1916Error LTO::runThinLTO(AddStreamFn AddStream, FileCache Cache,
1917 const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols) {
1918 LLVM_DEBUG(dbgs() << "Running ThinLTO\n");
1919 ThinLTO.CombinedIndex.releaseTemporaryMemory();
1920 timeTraceProfilerBegin("ThinLink", StringRef(""));
1921 auto TimeTraceScopeExit = llvm::make_scope_exit([]() {
1924 });
1925 if (ThinLTO.ModuleMap.empty())
1926 return Error::success();
1927
1928 if (ThinLTO.ModulesToCompile && ThinLTO.ModulesToCompile->empty()) {
1929 llvm::errs() << "warning: [ThinLTO] No module compiled\n";
1930 return Error::success();
1931 }
1932
1933 if (Conf.CombinedIndexHook &&
1934 !Conf.CombinedIndexHook(ThinLTO.CombinedIndex, GUIDPreservedSymbols))
1935 return Error::success();
1936
1937 // Collect for each module the list of function it defines (GUID ->
1938 // Summary).
1939 DenseMap<StringRef, GVSummaryMapTy> ModuleToDefinedGVSummaries(
1940 ThinLTO.ModuleMap.size());
1941 ThinLTO.CombinedIndex.collectDefinedGVSummariesPerModule(
1942 ModuleToDefinedGVSummaries);
1943 // Create entries for any modules that didn't have any GV summaries
1944 // (either they didn't have any GVs to start with, or we suppressed
1945 // generation of the summaries because they e.g. had inline assembly
1946 // uses that couldn't be promoted/renamed on export). This is so
1947 // InProcessThinBackend::start can still launch a backend thread, which
1948 // is passed the map of summaries for the module, without any special
1949 // handling for this case.
1950 for (auto &Mod : ThinLTO.ModuleMap)
1951 if (!ModuleToDefinedGVSummaries.count(Mod.first))
1952 ModuleToDefinedGVSummaries.try_emplace(Mod.first);
1953
1954 FunctionImporter::ImportListsTy ImportLists(ThinLTO.ModuleMap.size());
1956 ThinLTO.ModuleMap.size());
1958
1959 if (DumpThinCGSCCs)
1960 ThinLTO.CombinedIndex.dumpSCCs(outs());
1961
1962 std::set<GlobalValue::GUID> ExportedGUIDs;
1963
1964 bool WholeProgramVisibilityEnabledInLTO =
1966 // If validation is enabled, upgrade visibility only when all vtables
1967 // have typeinfos.
1969 if (hasWholeProgramVisibility(WholeProgramVisibilityEnabledInLTO))
1970 ThinLTO.CombinedIndex.setWithWholeProgramVisibility();
1971
1972 // If we're validating, get the vtable symbols that should not be
1973 // upgraded because they correspond to typeIDs outside of index-based
1974 // WPD info.
1975 DenseSet<GlobalValue::GUID> VisibleToRegularObjSymbols;
1976 if (WholeProgramVisibilityEnabledInLTO &&
1978 // This returns true when the name is local or not defined. Locals are
1979 // expected to be handled separately.
1980 auto IsVisibleToRegularObj = [&](StringRef name) {
1981 auto It = GlobalResolutions->find(name);
1982 return (It == GlobalResolutions->end() ||
1983 It->second.VisibleOutsideSummary || !It->second.Prevailing);
1984 };
1985
1986 getVisibleToRegularObjVtableGUIDs(ThinLTO.CombinedIndex,
1987 VisibleToRegularObjSymbols,
1988 IsVisibleToRegularObj);
1989 }
1990
1991 // If allowed, upgrade public vcall visibility to linkage unit visibility in
1992 // the summaries before whole program devirtualization below.
1994 ThinLTO.CombinedIndex, WholeProgramVisibilityEnabledInLTO,
1995 DynamicExportSymbols, VisibleToRegularObjSymbols);
1996
1997 // Perform index-based WPD. This will return immediately if there are
1998 // no index entries in the typeIdMetadata map (e.g. if we are instead
1999 // performing IR-based WPD in hybrid regular/thin LTO mode).
2000 std::map<ValueInfo, std::vector<VTableSlotSummary>> LocalWPDTargetsMap;
2001 runWholeProgramDevirtOnIndex(ThinLTO.CombinedIndex, ExportedGUIDs,
2002 LocalWPDTargetsMap);
2003
2004 auto isPrevailing = [&](GlobalValue::GUID GUID, const GlobalValueSummary *S) {
2005 return ThinLTO.PrevailingModuleForGUID[GUID] == S->modulePath();
2006 };
2008 MemProfContextDisambiguation ContextDisambiguation;
2009 ContextDisambiguation.run(ThinLTO.CombinedIndex, isPrevailing);
2010 }
2011
2012 // Figure out which symbols need to be internalized. This also needs to happen
2013 // at -O0 because summary-based DCE is implemented using internalization, and
2014 // we must apply DCE consistently with the full LTO module in order to avoid
2015 // undefined references during the final link.
2016 for (auto &Res : *GlobalResolutions) {
2017 // If the symbol does not have external references or it is not prevailing,
2018 // then not need to mark it as exported from a ThinLTO partition.
2019 if (Res.second.Partition != GlobalResolution::External ||
2020 !Res.second.isPrevailingIRSymbol())
2021 continue;
2023 GlobalValue::dropLLVMManglingEscape(Res.second.IRName));
2024 // Mark exported unless index-based analysis determined it to be dead.
2025 if (ThinLTO.CombinedIndex.isGUIDLive(GUID))
2026 ExportedGUIDs.insert(GUID);
2027 }
2028
2029 // Reset the GlobalResolutions to deallocate the associated memory, as there
2030 // are no further accesses. We specifically want to do this before computing
2031 // cross module importing, which adds to peak memory via the computed import
2032 // and export lists.
2033 releaseGlobalResolutionsMemory();
2034
2035 if (Conf.OptLevel > 0)
2036 ComputeCrossModuleImport(ThinLTO.CombinedIndex, ModuleToDefinedGVSummaries,
2037 isPrevailing, ImportLists, ExportLists);
2038
2039 // Any functions referenced by the jump table in the regular LTO object must
2040 // be exported.
2041 auto &Defs = ThinLTO.CombinedIndex.cfiFunctionDefs();
2042 ExportedGUIDs.insert(Defs.guid_begin(), Defs.guid_end());
2043 auto &Decls = ThinLTO.CombinedIndex.cfiFunctionDecls();
2044 ExportedGUIDs.insert(Decls.guid_begin(), Decls.guid_end());
2045
2046 auto isExported = [&](StringRef ModuleIdentifier, ValueInfo VI) {
2047 const auto &ExportList = ExportLists.find(ModuleIdentifier);
2048 return (ExportList != ExportLists.end() && ExportList->second.count(VI)) ||
2049 ExportedGUIDs.count(VI.getGUID());
2050 };
2051
2052 // Update local devirtualized targets that were exported by cross-module
2053 // importing or by other devirtualizations marked in the ExportedGUIDs set.
2054 updateIndexWPDForExports(ThinLTO.CombinedIndex, isExported,
2055 LocalWPDTargetsMap);
2056
2057 thinLTOInternalizeAndPromoteInIndex(ThinLTO.CombinedIndex, isExported,
2058 isPrevailing);
2059
2060 auto recordNewLinkage = [&](StringRef ModuleIdentifier,
2062 GlobalValue::LinkageTypes NewLinkage) {
2063 ResolvedODR[ModuleIdentifier][GUID] = NewLinkage;
2064 };
2065 thinLTOResolvePrevailingInIndex(Conf, ThinLTO.CombinedIndex, isPrevailing,
2066 recordNewLinkage, GUIDPreservedSymbols);
2067
2068 thinLTOPropagateFunctionAttrs(ThinLTO.CombinedIndex, isPrevailing);
2069
2070 generateParamAccessSummary(ThinLTO.CombinedIndex);
2071
2074
2075 TimeTraceScopeExit.release();
2076
2077 auto &ModuleMap =
2078 ThinLTO.ModulesToCompile ? *ThinLTO.ModulesToCompile : ThinLTO.ModuleMap;
2079
2080 auto RunBackends = [&](ThinBackendProc *BackendProcess) -> Error {
2081 auto ProcessOneModule = [&](int I) -> Error {
2082 auto &Mod = *(ModuleMap.begin() + I);
2083 // Tasks 0 through ParallelCodeGenParallelismLevel-1 are reserved for
2084 // combined module and parallel code generation partitions.
2085 return BackendProcess->start(
2086 RegularLTO.ParallelCodeGenParallelismLevel + I, Mod.second,
2087 ImportLists[Mod.first], ExportLists[Mod.first],
2088 ResolvedODR[Mod.first], ThinLTO.ModuleMap);
2089 };
2090
2091 BackendProcess->setup(ModuleMap.size(),
2092 RegularLTO.ParallelCodeGenParallelismLevel,
2093 RegularLTO.CombinedModule->getTargetTriple());
2094
2095 if (BackendProcess->getThreadCount() == 1 ||
2096 BackendProcess->isSensitiveToInputOrder()) {
2097 // Process the modules in the order they were provided on the
2098 // command-line. It is important for this codepath to be used for
2099 // WriteIndexesThinBackend, to ensure the emitted LinkedObjectsFile lists
2100 // ThinLTO objects in the same order as the inputs, which otherwise would
2101 // affect the final link order.
2102 for (int I = 0, E = ModuleMap.size(); I != E; ++I)
2103 if (Error E = ProcessOneModule(I))
2104 return E;
2105 } else {
2106 // When executing in parallel, process largest bitsize modules first to
2107 // improve parallelism, and avoid starving the thread pool near the end.
2108 // This saves about 15 sec on a 36-core machine while link `clang.exe`
2109 // (out of 100 sec).
2110 std::vector<BitcodeModule *> ModulesVec;
2111 ModulesVec.reserve(ModuleMap.size());
2112 for (auto &Mod : ModuleMap)
2113 ModulesVec.push_back(&Mod.second);
2114 for (int I : generateModulesOrdering(ModulesVec))
2115 if (Error E = ProcessOneModule(I))
2116 return E;
2117 }
2118 return BackendProcess->wait();
2119 };
2120
2122 std::unique_ptr<ThinBackendProc> BackendProc =
2123 ThinLTO.Backend(Conf, ThinLTO.CombinedIndex, ModuleToDefinedGVSummaries,
2124 AddStream, Cache);
2125 return RunBackends(BackendProc.get());
2126 }
2127
2128 // Perform two rounds of code generation for ThinLTO:
2129 // 1. First round: Perform optimization and code generation, outputting to
2130 // temporary scratch objects.
2131 // 2. Merge code generation data extracted from the temporary scratch objects.
2132 // 3. Second round: Execute code generation again using the merged data.
2133 LLVM_DEBUG(dbgs() << "[TwoRounds] Initializing ThinLTO two-codegen rounds\n");
2134
2135 unsigned MaxTasks = getMaxTasks();
2136 auto Parallelism = ThinLTO.Backend.getParallelism();
2137 // Set up two additional streams and caches for storing temporary scratch
2138 // objects and optimized IRs, using the same cache directory as the original.
2139 cgdata::StreamCacheData CG(MaxTasks, Cache, "CG"), IR(MaxTasks, Cache, "IR");
2140
2141 // First round: Execute optimization and code generation, outputting to
2142 // temporary scratch objects. Serialize the optimized IRs before initiating
2143 // code generation.
2144 LLVM_DEBUG(dbgs() << "[TwoRounds] Running the first round of codegen\n");
2145 auto FirstRoundLTO = std::make_unique<FirstRoundThinBackend>(
2146 Conf, ThinLTO.CombinedIndex, Parallelism, ModuleToDefinedGVSummaries,
2147 CG.AddStream, CG.Cache, IR.AddStream, IR.Cache);
2148 if (Error E = RunBackends(FirstRoundLTO.get()))
2149 return E;
2150
2151 LLVM_DEBUG(dbgs() << "[TwoRounds] Merging codegen data\n");
2152 auto CombinedHashOrErr = cgdata::mergeCodeGenData(*CG.getResult());
2153 if (Error E = CombinedHashOrErr.takeError())
2154 return E;
2155 auto CombinedHash = *CombinedHashOrErr;
2156 LLVM_DEBUG(dbgs() << "[TwoRounds] CGData hash: " << CombinedHash << "\n");
2157
2158 // Second round: Read the optimized IRs and execute code generation using the
2159 // merged data.
2160 LLVM_DEBUG(dbgs() << "[TwoRounds] Running the second round of codegen\n");
2161 auto SecondRoundLTO = std::make_unique<SecondRoundThinBackend>(
2162 Conf, ThinLTO.CombinedIndex, Parallelism, ModuleToDefinedGVSummaries,
2163 AddStream, Cache, IR.getResult(), CombinedHash);
2164 return RunBackends(SecondRoundLTO.get());
2165}
2166
2170 std::optional<uint64_t> RemarksHotnessThreshold, int Count) {
2171 std::string Filename = std::string(RemarksFilename);
2172 // For ThinLTO, file.opt.<format> becomes
2173 // file.opt.<format>.thin.<num>.<format>.
2174 if (!Filename.empty() && Count != -1)
2175 Filename =
2176 (Twine(Filename) + ".thin." + llvm::utostr(Count) + "." + RemarksFormat)
2177 .str();
2178
2179 auto ResultOrErr = llvm::setupLLVMOptimizationRemarks(
2182 if (Error E = ResultOrErr.takeError())
2183 return std::move(E);
2184
2185 if (*ResultOrErr)
2186 (*ResultOrErr)->keep();
2187
2188 return ResultOrErr;
2189}
2190
2193 // Setup output file to emit statistics.
2194 if (StatsFilename.empty())
2195 return nullptr;
2196
2198 std::error_code EC;
2199 auto StatsFile =
2200 std::make_unique<ToolOutputFile>(StatsFilename, EC, sys::fs::OF_None);
2201 if (EC)
2202 return errorCodeToError(EC);
2203
2204 StatsFile->keep();
2205 return std::move(StatsFile);
2206}
2207
2208// Compute the ordering we will process the inputs: the rough heuristic here
2209// is to sort them per size so that the largest module get schedule as soon as
2210// possible. This is purely a compile-time optimization.
2212 auto Seq = llvm::seq<int>(0, R.size());
2213 std::vector<int> ModulesOrdering(Seq.begin(), Seq.end());
2214 llvm::sort(ModulesOrdering, [&](int LeftIndex, int RightIndex) {
2215 auto LSize = R[LeftIndex]->getBuffer().size();
2216 auto RSize = R[RightIndex]->getBuffer().size();
2217 return LSize > RSize;
2218 });
2219 return ModulesOrdering;
2220}
2221
2222namespace {
2223/// This out-of-process backend does not perform code generation when invoked
2224/// for each task. Instead, it generates the necessary information (e.g., the
2225/// summary index shard, import list, etc.) to enable code generation to be
2226/// performed externally, similar to WriteIndexesThinBackend. The backend's
2227/// `wait` function then invokes an external distributor process to carry out
2228/// the backend compilations.
2229class OutOfProcessThinBackend : public CGThinBackend {
2230 using SString = SmallString<128>;
2231
2232 BumpPtrAllocator Alloc;
2233 StringSaver Saver{Alloc};
2234
2235 SString LinkerOutputFile;
2236
2237 SString DistributorPath;
2238 ArrayRef<StringRef> DistributorArgs;
2239
2240 SString RemoteCompiler;
2241 ArrayRef<StringRef> RemoteCompilerArgs;
2242
2243 bool SaveTemps;
2244
2245 SmallVector<StringRef, 0> CodegenOptions;
2246 DenseSet<StringRef> CommonInputs;
2247
2248 // Information specific to individual backend compilation job.
2249 struct Job {
2250 unsigned Task;
2251 StringRef ModuleID;
2252 StringRef NativeObjectPath;
2253 StringRef SummaryIndexPath;
2254 ImportsFilesContainer ImportsFiles;
2255 };
2256 // The set of backend compilations jobs.
2257 SmallVector<Job> Jobs;
2258
2259 // A unique string to identify the current link.
2260 SmallString<8> UID;
2261
2262 // The offset to the first ThinLTO task.
2263 unsigned ThinLTOTaskOffset;
2264
2265 // The target triple to supply for backend compilations.
2267
2268public:
2269 OutOfProcessThinBackend(
2270 const Config &Conf, ModuleSummaryIndex &CombinedIndex,
2271 ThreadPoolStrategy ThinLTOParallelism,
2272 const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
2273 AddStreamFn AddStream, lto::IndexWriteCallback OnWrite,
2274 bool ShouldEmitIndexFiles, bool ShouldEmitImportsFiles,
2275 StringRef LinkerOutputFile, StringRef Distributor,
2276 ArrayRef<StringRef> DistributorArgs, StringRef RemoteCompiler,
2277 ArrayRef<StringRef> RemoteCompilerArgs, bool SaveTemps)
2278 : CGThinBackend(Conf, CombinedIndex, ModuleToDefinedGVSummaries,
2279 AddStream, OnWrite, ShouldEmitIndexFiles,
2280 ShouldEmitImportsFiles, ThinLTOParallelism),
2281 LinkerOutputFile(LinkerOutputFile), DistributorPath(Distributor),
2282 DistributorArgs(DistributorArgs), RemoteCompiler(RemoteCompiler),
2283 RemoteCompilerArgs(RemoteCompilerArgs), SaveTemps(SaveTemps) {}
2284
2285 virtual void setup(unsigned ThinLTONumTasks, unsigned ThinLTOTaskOffset,
2286 llvm::Triple Triple) override {
2287 UID = itostr(sys::Process::getProcessId());
2288 Jobs.resize((size_t)ThinLTONumTasks);
2289 this->ThinLTOTaskOffset = ThinLTOTaskOffset;
2290 this->Triple = Triple;
2291 }
2292
2293 Error start(
2294 unsigned Task, BitcodeModule BM,
2295 const FunctionImporter::ImportMapTy &ImportList,
2296 const FunctionImporter::ExportSetTy &ExportList,
2297 const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
2298 MapVector<StringRef, BitcodeModule> &ModuleMap) override {
2299
2300 StringRef ModulePath = BM.getModuleIdentifier();
2301
2302 SString ObjFilePath = sys::path::parent_path(LinkerOutputFile);
2303 sys::path::append(ObjFilePath, sys::path::stem(ModulePath) + "." +
2304 itostr(Task) + "." + UID + ".native.o");
2305
2306 Job &J = Jobs[Task - ThinLTOTaskOffset];
2307 J = {
2308 Task,
2309 ModulePath,
2310 Saver.save(ObjFilePath.str()),
2311 Saver.save(ObjFilePath.str() + ".thinlto.bc"),
2312 {} // Filled in by emitFiles below.
2313 };
2314
2315 assert(ModuleToDefinedGVSummaries.count(ModulePath));
2316
2317 // The BackendThreadPool is only used here to write the sharded index files
2318 // (similar to WriteIndexesThinBackend).
2319 BackendThreadPool.async(
2320 [=](Job &J, const FunctionImporter::ImportMapTy &ImportList) {
2321 if (auto E = emitFiles(ImportList, J.ModuleID, J.ModuleID.str(),
2322 J.SummaryIndexPath, J.ImportsFiles)) {
2323 std::unique_lock<std::mutex> L(ErrMu);
2324 if (Err)
2325 Err = joinErrors(std::move(*Err), std::move(E));
2326 else
2327 Err = std::move(E);
2328 }
2329 },
2330 std::ref(J), std::ref(ImportList));
2331
2332 return Error::success();
2333 }
2334
2335 // Derive a set of Clang options that will be shared/common for all DTLTO
2336 // backend compilations. We are intentionally minimal here as these options
2337 // must remain synchronized with the behavior of Clang. DTLTO does not support
2338 // all the features available with in-process LTO. More features are expected
2339 // to be added over time. Users can specify Clang options directly if a
2340 // feature is not supported. Note that explicitly specified options that imply
2341 // additional input or output file dependencies must be communicated to the
2342 // distribution system, potentially by setting extra options on the
2343 // distributor program.
2344 void buildCommonRemoteCompilerOptions() {
2345 const lto::Config &C = Conf;
2346 auto &Ops = CodegenOptions;
2347
2348 Ops.push_back(Saver.save("-O" + Twine(C.OptLevel)));
2349
2350 if (C.Options.EmitAddrsig)
2351 Ops.push_back("-faddrsig");
2352 if (C.Options.FunctionSections)
2353 Ops.push_back("-ffunction-sections");
2354 if (C.Options.DataSections)
2355 Ops.push_back("-fdata-sections");
2356
2357 if (C.RelocModel == Reloc::PIC_)
2358 // Clang doesn't have -fpic for all triples.
2360 Ops.push_back("-fpic");
2361
2362 // Turn on/off warnings about profile cfg mismatch (default on)
2363 // --lto-pgo-warn-mismatch.
2364 if (!C.PGOWarnMismatch) {
2365 Ops.push_back("-mllvm");
2366 Ops.push_back("-no-pgo-warn-mismatch");
2367 }
2368
2369 // Enable sample-based profile guided optimizations.
2370 // Sample profile file path --lto-sample-profile=<value>.
2371 if (!C.SampleProfile.empty()) {
2372 Ops.push_back(
2373 Saver.save("-fprofile-sample-use=" + Twine(C.SampleProfile)));
2374 CommonInputs.insert(C.SampleProfile);
2375 }
2376
2377 // We don't know which of options will be used by Clang.
2378 Ops.push_back("-Wno-unused-command-line-argument");
2379
2380 // Forward any supplied options.
2381 if (!RemoteCompilerArgs.empty())
2382 for (auto &a : RemoteCompilerArgs)
2383 Ops.push_back(a);
2384 }
2385
2386 // Generates a JSON file describing the backend compilations, for the
2387 // distributor.
2388 bool emitDistributorJson(StringRef DistributorJson) {
2389 using json::Array;
2390 std::error_code EC;
2391 raw_fd_ostream OS(DistributorJson, EC);
2392 if (EC)
2393 return false;
2394
2395 json::OStream JOS(OS);
2396 JOS.object([&]() {
2397 // Information common to all jobs.
2398 JOS.attributeObject("common", [&]() {
2399 JOS.attribute("linker_output", LinkerOutputFile);
2400
2401 JOS.attributeArray("args", [&]() {
2402 JOS.value(RemoteCompiler);
2403
2404 JOS.value("-c");
2405
2406 JOS.value(Saver.save("--target=" + Triple.str()));
2407
2408 for (const auto &A : CodegenOptions)
2409 JOS.value(A);
2410 });
2411
2412 JOS.attribute("inputs", Array(CommonInputs));
2413 });
2414
2415 // Per-compilation-job information.
2416 JOS.attributeArray("jobs", [&]() {
2417 for (const auto &J : Jobs) {
2418 assert(J.Task != 0);
2419
2422
2423 JOS.object([&]() {
2424 JOS.attributeArray("args", [&]() {
2425 JOS.value(J.ModuleID);
2426 Inputs.push_back(J.ModuleID);
2427
2428 JOS.value(
2429 Saver.save("-fthinlto-index=" + Twine(J.SummaryIndexPath)));
2430 Inputs.push_back(J.SummaryIndexPath);
2431
2432 JOS.value("-o");
2433 JOS.value(J.NativeObjectPath);
2434 Outputs.push_back(J.NativeObjectPath);
2435 });
2436
2437 // Add the bitcode files from which imports will be made. These do
2438 // not explicitly appear on the backend compilation command lines
2439 // but are recorded in the summary index shards.
2440 llvm::append_range(Inputs, J.ImportsFiles);
2441 JOS.attribute("inputs", Array(Inputs));
2442
2443 JOS.attribute("outputs", Array(Outputs));
2444 });
2445 }
2446 });
2447 });
2448
2449 return true;
2450 }
2451
2452 void removeFile(StringRef FileName) {
2453 std::error_code EC = sys::fs::remove(FileName, true);
2454 if (EC && EC != std::make_error_code(std::errc::no_such_file_or_directory))
2455 errs() << "warning: could not remove the file '" << FileName
2456 << "': " << EC.message() << "\n";
2457 }
2458
2459 Error wait() override {
2460 // Wait for the information on the required backend compilations to be
2461 // gathered.
2462 BackendThreadPool.wait();
2463 if (Err)
2464 return std::move(*Err);
2465
2466 auto CleanPerJobFiles = llvm::make_scope_exit([&] {
2467 if (!SaveTemps)
2468 for (auto &Job : Jobs) {
2469 removeFile(Job.NativeObjectPath);
2470 if (!ShouldEmitIndexFiles)
2471 removeFile(Job.SummaryIndexPath);
2472 }
2473 });
2474
2475 const StringRef BCError = "DTLTO backend compilation: ";
2476
2477 buildCommonRemoteCompilerOptions();
2478
2479 SString JsonFile = sys::path::parent_path(LinkerOutputFile);
2480 sys::path::append(JsonFile, sys::path::stem(LinkerOutputFile) + "." + UID +
2481 ".dist-file.json");
2482 if (!emitDistributorJson(JsonFile))
2483 return make_error<StringError>(
2484 BCError + "failed to generate distributor JSON script: " + JsonFile,
2486 auto CleanJson = llvm::make_scope_exit([&] {
2487 if (!SaveTemps)
2488 removeFile(JsonFile);
2489 });
2490
2491 SmallVector<StringRef, 3> Args = {DistributorPath};
2492 llvm::append_range(Args, DistributorArgs);
2493 Args.push_back(JsonFile);
2494 std::string ErrMsg;
2495 if (sys::ExecuteAndWait(Args[0], Args,
2496 /*Env=*/std::nullopt, /*Redirects=*/{},
2497 /*SecondsToWait=*/0, /*MemoryLimit=*/0, &ErrMsg)) {
2498 return make_error<StringError>(
2499 BCError + "distributor execution failed" +
2500 (!ErrMsg.empty() ? ": " + ErrMsg + Twine(".") : Twine(".")),
2502 }
2503
2504 for (auto &Job : Jobs) {
2505 // Load the native object from a file into a memory buffer
2506 // and store its contents in the output buffer.
2507 auto ObjFileMbOrErr =
2508 MemoryBuffer::getFile(Job.NativeObjectPath, /*IsText=*/false,
2509 /*RequiresNullTerminator=*/false);
2510 if (std::error_code EC = ObjFileMbOrErr.getError())
2511 return make_error<StringError>(
2512 BCError + "cannot open native object file: " +
2513 Job.NativeObjectPath + ": " + EC.message(),
2515 auto StreamOrErr = AddStream(Job.Task, Job.ModuleID);
2516 if (Error Err = StreamOrErr.takeError())
2517 report_fatal_error(std::move(Err));
2518 auto &Stream = *StreamOrErr->get();
2519 *Stream.OS << ObjFileMbOrErr->get()->getMemBufferRef().getBuffer();
2520 if (Error Err = Stream.commit())
2521 report_fatal_error(std::move(Err));
2522 }
2523
2524 return Error::success();
2525 }
2526};
2527} // end anonymous namespace
2528
2530 ThreadPoolStrategy Parallelism, lto::IndexWriteCallback OnWrite,
2531 bool ShouldEmitIndexFiles, bool ShouldEmitImportsFiles,
2532 StringRef LinkerOutputFile, StringRef Distributor,
2533 ArrayRef<StringRef> DistributorArgs, StringRef RemoteCompiler,
2534 ArrayRef<StringRef> RemoteCompilerArgs, bool SaveTemps) {
2535 auto Func =
2536 [=](const Config &Conf, ModuleSummaryIndex &CombinedIndex,
2537 const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
2538 AddStreamFn AddStream, FileCache /*Cache*/) {
2539 return std::make_unique<OutOfProcessThinBackend>(
2540 Conf, CombinedIndex, Parallelism, ModuleToDefinedGVSummaries,
2541 AddStream, OnWrite, ShouldEmitIndexFiles, ShouldEmitImportsFiles,
2542 LinkerOutputFile, Distributor, DistributorArgs, RemoteCompiler,
2543 RemoteCompilerArgs, SaveTemps);
2544 };
2545 return ThinBackend(Func, Parallelism);
2546}
for(const MachineOperand &MO :llvm::drop_begin(OldMI.operands(), Desc.getNumOperands()))
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
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."))
std::string Name
Symbol * Sym
Definition: ELF_riscv.cpp:479
#define DEBUG_TYPE
This file supports working with JSON data.
static void writeToResolutionFile(raw_ostream &OS, InputFile *Input, ArrayRef< SymbolResolution > Res)
Definition: LTO.cpp:708
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:826
cl::opt< bool > CodeGenDataThinLTOTwoRounds
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
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
Definition: SMEABIPass.cpp:52
raw_pwrite_stream & OS
This file defines the make_scope_exit function, which executes user-defined cleanup logic at scope ex...
This file defines the SmallSet class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
This file contains some functions that are useful when dealing with strings.
#define LLVM_DEBUG(...)
Definition: Debug.h:119
This pass exposes codegen information to IR-level passes.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
iterator end() const
Definition: ArrayRef.h: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.
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:67
static LLVM_ABI ConstantAggregateZero * get(Type *Ty)
Definition: Constants.cpp:1677
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:177
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&...Args)
Definition: DenseMap.h:245
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:173
iterator end()
Definition: DenseMap.h:87
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.
Function and variable summary information to aid decisions and implementation of importing.
static bool isAppendingLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:402
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)
Definition: GlobalValue.h:414
static bool isLocalLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:411
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)
Definition: GlobalValue.h:233
bool hasLocalLinkage() const
Definition: GlobalValue.h:530
static StringRef dropLLVMManglingEscape(StringRef Name)
If the given string begins with the GlobalValue name mangling escape character '\1',...
Definition: GlobalValue.h:569
LLVM_ABI const Comdat * getComdat() const
Definition: Globals.cpp:201
static bool isLinkOnceLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:390
void setLinkage(LinkageTypes LT)
Definition: GlobalValue.h:539
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.
Definition: GlobalValue.h:600
LLVM_ABI const GlobalObject * getAliaseeObject() const
Definition: Globals.cpp:419
static bool isExternalLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:378
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 LinkageTypes getWeakLinkage(bool ODR)
Definition: GlobalValue.h:374
bool isWeakForLinker() const
Definition: GlobalValue.h:554
bool hasAppendingLinkage() const
Definition: GlobalValue.h:527
bool hasAvailableExternallyLinkage() const
Definition: GlobalValue.h:514
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
Definition: GlobalValue.h:277
Type * getValueType() const
Definition: GlobalValue.h:298
static bool isLinkOnceODRLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:387
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
Metadata node.
Definition: Metadata.h:1077
ArrayRef< MDOperand > operands() const
Definition: Metadata.h:1443
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
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,...
Root of the metadata hierarchy.
Definition: Metadata.h:63
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.
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 tuple of MDNodes.
Definition: Metadata.h:1753
The optimization diagnostic interface.
Diagnostic information for applied optimization remarks.
A discriminated union of two or more pointer types, with the discriminator in the low bit of the poin...
Definition: PointerUnion.h:118
A class that wrap the SHA1 algorithm.
Definition: SHA1.h: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
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
Definition: SmallSet.h:134
size_type count(const T &V) const
count - Return 1 if the element is in the set, 0 otherwise.
Definition: SmallSet.h:176
bool empty() const
Definition: SmallSet.h:169
bool erase(const T &V)
Definition: SmallSet.h:198
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:182
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
StringRef str() const
Explicit conversion to StringRef.
Definition: SmallString.h:254
void reserve(size_type N)
Definition: SmallVector.h:664
void resize(size_type N)
Definition: SmallVector.h:639
void push_back(const T &Elt)
Definition: SmallVector.h:414
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1197
A wrapper around a string literal that serves as a proxy for constructing global tables of StringRefs...
Definition: StringRef.h:862
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition: StringMap.h:133
size_type count(StringRef Key) const
count - Return 1 if the element is in the map, 0 otherwise.
Definition: StringMap.h:280
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
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
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:1119
@ aarch64_32
Definition: Triple.h:56
ArchType getArch() const
Get the parsed architecture type of this triple.
Definition: Triple.h:408
bool isOSBinFormatCOFF() const
Tests whether the OS uses the COFF binary format.
Definition: Triple.h:771
const std::string & str() const
Definition: Triple.h:475
bool isOSDarwin() const
Is this a "Darwin" OS (macOS, iOS, tvOS, watchOS, DriverKit, XROS, or bridgeOS).
Definition: Triple.h:608
bool isOSBinFormatELF() const
Tests whether the OS uses the ELF binary format.
Definition: Triple.h:766
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)
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:222
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 Array is a JSON array, which contains heterogeneous JSON values.
Definition: JSON.h:164
json::OStream allows writing well-formed JSON without materializing all structures as json::Value ahe...
Definition: JSON.h:996
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:732
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:1417
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:1144
LLVM_ABI Error run(AddStreamFn AddStream, FileCache Cache={})
Runs the LTO pipeline.
Definition: LTO.cpp:1195
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:1431
ModuleSummaryIndex & CombinedIndex
Definition: LTO.h:212
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:461
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 Args[]
Key for Kernel::Metadata::mArgs.
Key
PAL metadata keys.
@ 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.
Definition: Intrinsics.cpp:762
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)
Definition: CommandLine.h:444
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:1779
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:1813
std::function< void(const std::string &)> IndexWriteCallback
Definition: LTO.h:204
LLVM_ABI StringLiteral getThinLTODefaultCPU(const Triple &TheTriple)
Definition: LTO.cpp:1795
LLVM_ABI Expected< std::unique_ptr< ToolOutputFile > > setupStatsFile(StringRef StatsFilename)
Setups the output file for saving statistics.
Definition: LTO.cpp:2192
LLVM_ABI Error backend(const Config &C, AddStreamFn AddStream, unsigned ParallelCodeGenParallelismLevel, Module &M, ModuleSummaryIndex &CombinedIndex)
Runs a regular LTO backend.
Definition: LTOBackend.cpp:552
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:1899
LLVM_ABI Error finalizeOptimizationRemarks(std::unique_ptr< ToolOutputFile > DiagOutputFile)
Definition: LTOBackend.cpp:541
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:2529
LLVM_ABI std::vector< int > generateModulesOrdering(ArrayRef< BitcodeModule * > R)
Produces a container ordering for optimal multi-threaded processing.
Definition: LTO.cpp:2211
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:2167
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.
Definition: LTOBackend.cpp:599
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:1253
LLVM_ABI Expected< IRSymtabFile > readIRSymtab(MemoryBufferRef MBRef)
Reads a bitcode file, creating its irsymtab if necessary.
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.
Definition: AddressRanges.h:18
ThreadPoolStrategy heavyweight_hardware_concurrency(unsigned ThreadCount=0)
Returns a thread strategy for tasks requiring significant memory or other resources.
Definition: Threading.h:162
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:860
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:1744
Error createFileError(const Twine &F, Error E)
Concatenate a source file path and/or name with an Error.
Definition: Error.h:1399
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition: STLExtras.h:1702
detail::scope_exit< std::decay_t< Callable > > make_scope_exit(Callable &&F)
Definition: ScopeExit.h:59
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.
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:60
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:2155
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.
LLVM_ABI void EnableStatistics(bool DoPrintOnExit=true)
Enable the collection and printing of statistics.
Definition: Statistic.cpp:134
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:1669
bool timeTraceProfilerEnabled()
Is the time trace profiler enabled, i.e. initialized?
Definition: TimeProfiler.h:130
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
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"))
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...
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
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.
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.
std::unordered_set< GlobalValueSummary * > GVSummaryPtrSet
A set of global value summary pointers.
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.
std::map< std::string, GVSummaryMapTy, std::less<> > ModuleToSummariesForIndexTy
Map of a module name to the GUIDs and summaries we will import from that module.
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:1886
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:1980
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)
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.
LLVM_ABI void PrintStatisticsJSON(raw_ostream &OS)
Print statistics in JSON format.
Definition: Statistic.cpp:203
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.
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:856
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
Used in the streaming interface as the general argument type.
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.
bool isGlobal() const
Definition: IRSymtab.h:212
bool isFormatSpecific() const
Definition: IRSymtab.h:213
LTO configuration.
Definition: Config.h:42
bool HasWholeProgramVisibility
Asserts whether we can assume whole program visibility during the LTO link.
Definition: Config.h:79
bool ValidateAllVtablesHaveTypeInfos
We're validating that all native vtables have corresponding type infos.
Definition: Config.h:82
bool KeepSymbolNameCopies
If true, the LTO instance creates copies of the symbol names for LTO::run.
Definition: Config.h:95
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::string StatsFile
Statistics output file path.
Definition: Config.h:175
ModuleHookFn PreOptModuleHook
This module hook is called after linking (regular LTO) or loading (ThinLTO) the module,...
Definition: Config.h:229
CombinedIndexHookFn CombinedIndexHook
Definition: Config.h:261
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
std::vector< std::string > ThinLTOModulesToCompile
Specific thinLTO modules to compile.
Definition: Config.h:178
CodeGenOptLevel CGOptLevel
Definition: Config.h:58
std::unique_ptr< raw_ostream > ResolutionFile
If this field is set, LTO will write input file paths and symbol resolutions here in llvm-lto2 comman...
Definition: Config.h:196
std::string DefaultTriple
Setting this field will replace unspecified target triples in input files with this triple.
Definition: Config.h:119
bool AlwaysEmitRegularLTOObj
Always emit a Regular LTO object even when it is empty because no Regular LTO modules were linked.
Definition: Config.h:90
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
VisScheme VisibilityScheme
Allows non-imported definitions to get the potentially more constraining visibility from the prevaili...
Definition: Config.h:101
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
bool AllVtablesHaveTypeInfos
If all native vtables have corresponding type infos, allow usage of RTTI to block devirtualization on...
Definition: Config.h:85
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
ModuleHookFn PostInternalizeModuleHook
This hook is called after internalizing the module.
Definition: Config.h:236
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
A derived class of LLVMContext that initializes itself according to a given Config object.
Definition: Config.h:300
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