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