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 TargetLibraryInfo &TLI,
657 const RTLIB::RuntimeLibcallsInfo &Libcalls) const {
658 LibFunc F;
659 if (TLI.getLibFunc(IRName, F) && TLI.has(F))
660 return true;
661 return Libcalls.getSupportedLibcallImpl(IRName) != RTLIB::Unsupported;
662}
663
665 return Mods[0].getModuleIdentifier();
666}
667
669 assert(Mods.size() == 1 && "Expect only one bitcode module");
670 return Mods[0];
671}
672
674
675LTO::RegularLTOState::RegularLTOState(unsigned ParallelCodeGenParallelismLevel,
676 const Config &Conf)
677 : ParallelCodeGenParallelismLevel(ParallelCodeGenParallelismLevel),
678 Ctx(Conf), CombinedModule(std::make_unique<Module>("ld-temp.o", Ctx)),
679 Mover(std::make_unique<IRMover>(*CombinedModule)) {}
680
681LTO::ThinLTOState::ThinLTOState(ThinBackend BackendParam)
682 : Backend(std::move(BackendParam)), CombinedIndex(/*HaveGVs*/ false) {
683 if (!Backend.isValid())
684 Backend =
686}
687
689 unsigned ParallelCodeGenParallelismLevel, LTOKind LTOMode)
690 : Conf(std::move(Conf)),
691 RegularLTO(ParallelCodeGenParallelismLevel, this->Conf),
692 ThinLTO(std::move(Backend)),
693 GlobalResolutions(
694 std::make_unique<DenseMap<StringRef, GlobalResolution>>()),
695 LTOMode(LTOMode) {
696 if (Conf.KeepSymbolNameCopies || LTOKeepSymbolCopies) {
697 Alloc = std::make_unique<BumpPtrAllocator>();
698 GlobalResolutionSymbolSaver = std::make_unique<llvm::StringSaver>(*Alloc);
699 }
700}
701
702// Requires a destructor for MapVector<BitcodeModule>.
703LTO::~LTO() = default;
704
706 DummyModule.reset();
707 LinkerRemarkFunction = nullptr;
708 consumeError(finalizeOptimizationRemarks(std::move(DiagnosticOutputFile)));
709}
710
711// Add the symbols in the given module to the GlobalResolutions map, and resolve
712// their partitions.
713void LTO::addModuleToGlobalRes(ArrayRef<InputFile::Symbol> Syms,
715 unsigned Partition, bool InSummary,
716 const Triple &TT) {
717 llvm::TimeTraceScope timeScope("LTO add module to global resolution");
718 auto *ResI = Res.begin();
719 auto *ResE = Res.end();
720 (void)ResE;
721 RTLIB::RuntimeLibcallsInfo Libcalls(TT);
722 TargetLibraryInfoImpl TLII(TT);
723 TargetLibraryInfo TLI(TLII);
724 for (const InputFile::Symbol &Sym : Syms) {
725 assert(ResI != ResE);
726 SymbolResolution Res = *ResI++;
727
728 StringRef SymbolName = Sym.getName();
729 // Keep copies of symbols if the client of LTO says so.
730 if (GlobalResolutionSymbolSaver && !GlobalResolutions->contains(SymbolName))
731 SymbolName = GlobalResolutionSymbolSaver->save(SymbolName);
732
733 auto &GlobalRes = (*GlobalResolutions)[SymbolName];
734 GlobalRes.UnnamedAddr &= Sym.isUnnamedAddr();
735 if (Res.Prevailing) {
736 assert(!GlobalRes.Prevailing &&
737 "Multiple prevailing defs are not allowed");
738 GlobalRes.Prevailing = true;
739 GlobalRes.IRName = std::string(Sym.getIRName());
740 } else if (!GlobalRes.Prevailing && GlobalRes.IRName.empty()) {
741 // Sometimes it can be two copies of symbol in a module and prevailing
742 // symbol can have no IR name. That might happen if symbol is defined in
743 // module level inline asm block. In case we have multiple modules with
744 // the same symbol we want to use IR name of the prevailing symbol.
745 // Otherwise, if we haven't seen a prevailing symbol, set the name so that
746 // we can later use it to check if there is any prevailing copy in IR.
747 GlobalRes.IRName = std::string(Sym.getIRName());
748 }
749
750 // In rare occasion, the symbol used to initialize GlobalRes has a different
751 // IRName from the inspected Symbol. This can happen on macOS + iOS, when a
752 // symbol is referenced through its mangled name, say @"\01_symbol" while
753 // the IRName is @symbol (the prefix underscore comes from MachO mangling).
754 // In that case, we have the same actual Symbol that can get two different
755 // GUID, leading to some invalid internalization. Workaround this by marking
756 // the GlobalRes external.
757
758 // FIXME: instead of this check, it would be desirable to compute GUIDs
759 // based on mangled name, but this requires an access to the Target Triple
760 // and would be relatively invasive on the codebase.
761 if (GlobalRes.IRName != Sym.getIRName()) {
762 GlobalRes.Partition = GlobalResolution::External;
763 GlobalRes.VisibleOutsideSummary = true;
764 }
765
766 bool IsLibcall = Sym.isLibcall(TLI, Libcalls);
767
768 // Set the partition to external if we know it is re-defined by the linker
769 // with -defsym or -wrap options, used elsewhere, e.g. it is visible to a
770 // regular object, is referenced from llvm.compiler.used/llvm.used, or was
771 // already recorded as being referenced from a different partition.
772 if (Res.LinkerRedefined || Res.VisibleToRegularObj || Sym.isUsed() ||
773 IsLibcall ||
774 (GlobalRes.Partition != GlobalResolution::Unknown &&
775 GlobalRes.Partition != Partition)) {
776 GlobalRes.Partition = GlobalResolution::External;
777 } else
778 // First recorded reference, save the current partition.
779 GlobalRes.Partition = Partition;
780
781 // Flag as visible outside of summary if visible from a regular object or
782 // from a module that does not have a summary.
783 GlobalRes.VisibleOutsideSummary |=
784 (Res.VisibleToRegularObj || Sym.isUsed() || IsLibcall || !InSummary);
785
786 GlobalRes.ExportDynamic |= Res.ExportDynamic;
787 }
788}
789
790void LTO::releaseGlobalResolutionsMemory() {
791 // Release GlobalResolutions dense-map itself.
792 GlobalResolutions.reset();
793 // Release the string saver memory.
794 GlobalResolutionSymbolSaver.reset();
795 Alloc.reset();
796}
797
800 StringRef Path = Input->getName();
801 OS << Path << '\n';
802 auto ResI = Res.begin();
803 for (const InputFile::Symbol &Sym : Input->symbols()) {
804 assert(ResI != Res.end());
805 SymbolResolution Res = *ResI++;
806
807 OS << "-r=" << Path << ',' << Sym.getName() << ',';
808 if (Res.Prevailing)
809 OS << 'p';
811 OS << 'l';
812 if (Res.VisibleToRegularObj)
813 OS << 'x';
814 if (Res.LinkerRedefined)
815 OS << 'r';
816 OS << '\n';
817 }
818 OS.flush();
819 assert(ResI == Res.end());
820}
821
822Error LTO::add(std::unique_ptr<InputFile> InputPtr,
824 llvm::TimeTraceScope timeScope("LTO add input", InputPtr->getName());
825 assert(!CalledGetMaxTasks);
826
828 addInput(std::move(InputPtr));
829 if (!InputOrErr)
830 return InputOrErr.takeError();
831 InputFile *Input = (*InputOrErr).get();
832
833 if (Conf.ResolutionFile)
834 writeToResolutionFile(*Conf.ResolutionFile, Input, Res);
835
836 if (RegularLTO.CombinedModule->getTargetTriple().empty()) {
837 Triple InputTriple(Input->getTargetTriple());
838 RegularLTO.CombinedModule->setTargetTriple(InputTriple);
839 if (InputTriple.isOSBinFormatELF())
840 Conf.VisibilityScheme = Config::ELF;
841 }
842
843 ArrayRef<SymbolResolution> InputRes = Res;
844 for (unsigned I = 0; I != Input->Mods.size(); ++I) {
845 if (auto Err = addModule(*Input, InputRes, I, Res).moveInto(Res))
846 return Err;
847 }
848
849 assert(Res.empty());
850 return Error::success();
851}
852
854 assert(this->BitcodeLibFuncs.empty() &&
855 "bitcode libfuncs were set twice; maybe accidentally clobbered?");
856 this->BitcodeLibFuncs.append(BitcodeLibFuncs.begin(), BitcodeLibFuncs.end());
857}
858
860LTO::addModule(InputFile &Input, ArrayRef<SymbolResolution> InputRes,
861 unsigned ModI, ArrayRef<SymbolResolution> Res) {
862 llvm::TimeTraceScope timeScope("LTO add module", Input.getName());
863 Expected<BitcodeLTOInfo> LTOInfo = Input.Mods[ModI].getLTOInfo();
864 if (!LTOInfo)
865 return LTOInfo.takeError();
866
867 if (EnableSplitLTOUnit) {
868 // If only some modules were split, flag this in the index so that
869 // we can skip or error on optimizations that need consistently split
870 // modules (whole program devirt and lower type tests).
871 if (*EnableSplitLTOUnit != LTOInfo->EnableSplitLTOUnit)
872 ThinLTO.CombinedIndex.setPartiallySplitLTOUnits();
873 } else
874 EnableSplitLTOUnit = LTOInfo->EnableSplitLTOUnit;
875
876 BitcodeModule BM = Input.Mods[ModI];
877
878 if ((LTOMode == LTOK_UnifiedRegular || LTOMode == LTOK_UnifiedThin) &&
879 !LTOInfo->UnifiedLTO)
881 "unified LTO compilation must use "
882 "compatible bitcode modules (use -funified-lto)",
884
885 if (LTOInfo->UnifiedLTO && LTOMode == LTOK_Default)
886 LTOMode = LTOK_UnifiedThin;
887
888 bool IsThinLTO = LTOInfo->IsThinLTO && (LTOMode != LTOK_UnifiedRegular);
889 // If any of the modules inside of a input bitcode file was compiled with
890 // ThinLTO, we assume that the whole input file also was compiled with
891 // ThinLTO.
892 Input.IsThinLTO |= IsThinLTO;
893
894 auto ModSyms = Input.module_symbols(ModI);
895 addModuleToGlobalRes(ModSyms, Res,
896 IsThinLTO ? ThinLTO.ModuleMap.size() + 1 : 0,
897 LTOInfo->HasSummary, Triple(Input.getTargetTriple()));
898
899 if (IsThinLTO)
900 return addThinLTO(BM, ModSyms, Res);
901
902 RegularLTO.EmptyCombinedModule = false;
903 auto ModOrErr = addRegularLTO(Input, InputRes, BM, ModSyms, Res);
904 if (!ModOrErr)
905 return ModOrErr.takeError();
906 Res = ModOrErr->second;
907
908 if (!LTOInfo->HasSummary) {
909 if (Error Err = linkRegularLTO(std::move(ModOrErr->first),
910 /*LivenessFromIndex=*/false))
911 return Err;
912 return Res;
913 }
914
915 // Regular LTO module summaries are added to a dummy module that represents
916 // the combined regular LTO module.
917 if (Error Err = BM.readSummary(ThinLTO.CombinedIndex, ""))
918 return Err;
919 RegularLTO.ModsWithSummaries.push_back(std::move(ModOrErr->first));
920 return Res;
921}
922
923// Checks whether the given global value is in a non-prevailing comdat
924// (comdat containing values the linker indicated were not prevailing,
925// which we then dropped to available_externally), and if so, removes
926// it from the comdat. This is called for all global values to ensure the
927// comdat is empty rather than leaving an incomplete comdat. It is needed for
928// regular LTO modules, in case we are in a mixed-LTO mode (both regular
929// and thin LTO modules) compilation. Since the regular LTO module will be
930// linked first in the final native link, we want to make sure the linker
931// doesn't select any of these incomplete comdats that would be left
932// in the regular LTO module without this cleanup.
933static void
935 std::set<const Comdat *> &NonPrevailingComdats) {
936 Comdat *C = GV.getComdat();
937 if (!C)
938 return;
939
940 if (!NonPrevailingComdats.count(C))
941 return;
942
943 // Additionally need to drop all global values from the comdat to
944 // available_externally, to satisfy the COMDAT requirement that all members
945 // are discarded as a unit. The non-local linkage global values avoid
946 // duplicate definition linker errors.
948
949 if (auto GO = dyn_cast<GlobalObject>(&GV))
950 GO->setComdat(nullptr);
951}
952
953// Add a regular LTO object to the link.
954// The resulting module needs to be linked into the combined LTO module with
955// linkRegularLTO.
956Expected<
957 std::pair<LTO::RegularLTOState::AddedModule, ArrayRef<SymbolResolution>>>
958LTO::addRegularLTO(InputFile &Input, ArrayRef<SymbolResolution> InputRes,
959 BitcodeModule BM, ArrayRef<InputFile::Symbol> Syms,
961 llvm::TimeTraceScope timeScope("LTO add regular LTO");
963 Expected<std::unique_ptr<Module>> MOrErr =
964 BM.getLazyModule(RegularLTO.Ctx, /*ShouldLazyLoadMetadata*/ true,
965 /*IsImporting*/ false);
966 if (!MOrErr)
967 return MOrErr.takeError();
968 Module &M = **MOrErr;
969 Mod.M = std::move(*MOrErr);
970
971 if (Error Err = M.materializeMetadata())
972 return std::move(Err);
973
974 if (LTOMode == LTOK_UnifiedRegular) {
975 // cfi.functions metadata is intended to be used with ThinLTO and may
976 // trigger invalid IR transformations if they are present when doing regular
977 // LTO, so delete it.
978 if (NamedMDNode *CfiFunctionsMD = M.getNamedMetadata("cfi.functions"))
979 M.eraseNamedMetadata(CfiFunctionsMD);
980 } else if (NamedMDNode *AliasesMD = M.getNamedMetadata("aliases")) {
981 // Delete aliases entries for non-prevailing symbols on the ThinLTO side of
982 // this input file.
983 DenseSet<StringRef> Prevailing;
984 for (auto [I, R] : zip(Input.symbols(), InputRes))
985 if (R.Prevailing && !I.getIRName().empty())
986 Prevailing.insert(I.getIRName());
987 std::vector<MDNode *> AliasGroups;
988 for (MDNode *AliasGroup : AliasesMD->operands()) {
989 std::vector<Metadata *> Aliases;
990 for (Metadata *Alias : AliasGroup->operands()) {
991 if (isa<MDString>(Alias) &&
992 Prevailing.count(cast<MDString>(Alias)->getString()))
993 Aliases.push_back(Alias);
994 }
995 if (Aliases.size() > 1)
996 AliasGroups.push_back(MDTuple::get(RegularLTO.Ctx, Aliases));
997 }
998 AliasesMD->clearOperands();
999 for (MDNode *G : AliasGroups)
1000 AliasesMD->addOperand(G);
1001 }
1002
1004
1005 ModuleSymbolTable SymTab;
1006 SymTab.addModule(&M);
1007
1008 for (GlobalVariable &GV : M.globals())
1009 if (GV.hasAppendingLinkage())
1010 Mod.Keep.push_back(&GV);
1011
1012 DenseSet<GlobalObject *> AliasedGlobals;
1013 for (auto &GA : M.aliases())
1014 if (GlobalObject *GO = GA.getAliaseeObject())
1015 AliasedGlobals.insert(GO);
1016
1017 // In this function we need IR GlobalValues matching the symbols in Syms
1018 // (which is not backed by a module), so we need to enumerate them in the same
1019 // order. The symbol enumeration order of a ModuleSymbolTable intentionally
1020 // matches the order of an irsymtab, but when we read the irsymtab in
1021 // InputFile::create we omit some symbols that are irrelevant to LTO. The
1022 // Skip() function skips the same symbols from the module as InputFile does
1023 // from the symbol table.
1024 auto MsymI = SymTab.symbols().begin(), MsymE = SymTab.symbols().end();
1025 auto Skip = [&]() {
1026 while (MsymI != MsymE) {
1027 auto Flags = SymTab.getSymbolFlags(*MsymI);
1028 if ((Flags & object::BasicSymbolRef::SF_Global) &&
1030 return;
1031 ++MsymI;
1032 }
1033 };
1034 Skip();
1035
1036 std::set<const Comdat *> NonPrevailingComdats;
1037 SmallSet<StringRef, 2> NonPrevailingAsmSymbols;
1038 for (const InputFile::Symbol &Sym : Syms) {
1039 assert(!Res.empty());
1040 const SymbolResolution &R = Res.consume_front();
1041
1042 assert(MsymI != MsymE);
1043 ModuleSymbolTable::Symbol Msym = *MsymI++;
1044 Skip();
1045
1046 if (GlobalValue *GV = dyn_cast_if_present<GlobalValue *>(Msym)) {
1047 if (R.Prevailing) {
1048 if (Sym.isUndefined())
1049 continue;
1050 Mod.Keep.push_back(GV);
1051 // For symbols re-defined with linker -wrap and -defsym options,
1052 // set the linkage to weak to inhibit IPO. The linkage will be
1053 // restored by the linker.
1054 if (R.LinkerRedefined)
1055 GV->setLinkage(GlobalValue::WeakAnyLinkage);
1056
1057 GlobalValue::LinkageTypes OriginalLinkage = GV->getLinkage();
1058 if (GlobalValue::isLinkOnceLinkage(OriginalLinkage))
1059 GV->setLinkage(GlobalValue::getWeakLinkage(
1060 GlobalValue::isLinkOnceODRLinkage(OriginalLinkage)));
1061 } else if (isa<GlobalObject>(GV) &&
1062 (GV->hasLinkOnceODRLinkage() || GV->hasWeakODRLinkage() ||
1063 GV->hasAvailableExternallyLinkage()) &&
1064 !AliasedGlobals.count(cast<GlobalObject>(GV))) {
1065 // Any of the above three types of linkage indicates that the
1066 // chosen prevailing symbol will have the same semantics as this copy of
1067 // the symbol, so we may be able to link it with available_externally
1068 // linkage. We will decide later whether to do that when we link this
1069 // module (in linkRegularLTO), based on whether it is undefined.
1070 Mod.Keep.push_back(GV);
1072 if (GV->hasComdat())
1073 NonPrevailingComdats.insert(GV->getComdat());
1074 cast<GlobalObject>(GV)->setComdat(nullptr);
1075 }
1076
1077 // Set the 'local' flag based on the linker resolution for this symbol.
1078 if (R.FinalDefinitionInLinkageUnit) {
1079 GV->setDSOLocal(true);
1080 if (GV->hasDLLImportStorageClass())
1081 GV->setDLLStorageClass(GlobalValue::DLLStorageClassTypes::
1082 DefaultStorageClass);
1083 }
1084 } else if (auto *AS =
1086 // Collect non-prevailing symbols.
1087 if (!R.Prevailing)
1088 NonPrevailingAsmSymbols.insert(AS->first);
1089 } else {
1090 llvm_unreachable("unknown symbol type");
1091 }
1092
1093 // Common resolution: collect the maximum size/alignment over all commons.
1094 // We also record if we see an instance of a common as prevailing, so that
1095 // if none is prevailing we can ignore it later.
1096 if (Sym.isCommon()) {
1097 // FIXME: We should figure out what to do about commons defined by asm.
1098 // For now they aren't reported correctly by ModuleSymbolTable.
1099 auto &CommonRes = RegularLTO.Commons[std::string(Sym.getIRName())];
1100 CommonRes.Size = std::max(CommonRes.Size, Sym.getCommonSize());
1101 if (uint32_t SymAlignValue = Sym.getCommonAlignment()) {
1102 CommonRes.Alignment =
1103 std::max(Align(SymAlignValue), CommonRes.Alignment);
1104 }
1105 CommonRes.Prevailing |= R.Prevailing;
1106 }
1107 }
1108
1109 if (!M.getComdatSymbolTable().empty())
1110 for (GlobalValue &GV : M.global_values())
1111 handleNonPrevailingComdat(GV, NonPrevailingComdats);
1112
1113 // Prepend ".lto_discard <sym>, <sym>*" directive to each module inline asm
1114 // block.
1115 if (!M.getModuleInlineAsm().empty()) {
1116 std::string NewIA = ".lto_discard";
1117 if (!NonPrevailingAsmSymbols.empty()) {
1118 // Don't dicard a symbol if there is a live .symver for it.
1120 M, [&](StringRef Name, StringRef Alias) {
1121 if (!NonPrevailingAsmSymbols.count(Alias))
1122 NonPrevailingAsmSymbols.erase(Name);
1123 });
1124 NewIA += " " + llvm::join(NonPrevailingAsmSymbols, ", ");
1125 }
1126 NewIA += "\n";
1127 M.setModuleInlineAsm(NewIA + M.getModuleInlineAsm());
1128 }
1129
1130 assert(MsymI == MsymE);
1131 return std::make_pair(std::move(Mod), Res);
1132}
1133
1134Error LTO::linkRegularLTO(RegularLTOState::AddedModule Mod,
1135 bool LivenessFromIndex) {
1136 llvm::TimeTraceScope timeScope("LTO link regular LTO");
1137 std::vector<GlobalValue *> Keep;
1138 for (GlobalValue *GV : Mod.Keep) {
1139 if (LivenessFromIndex && !ThinLTO.CombinedIndex.isGUIDLive(GV->getGUID())) {
1140 if (Function *F = dyn_cast<Function>(GV)) {
1141 if (DiagnosticOutputFile) {
1142 if (Error Err = F->materialize())
1143 return Err;
1144 auto R = OptimizationRemark(DEBUG_TYPE, "deadfunction", F);
1145 R << ore::NV("Function", F) << " not added to the combined module ";
1146 emitRemark(R);
1147 }
1148 }
1149 continue;
1150 }
1151
1152 if (!GV->hasAvailableExternallyLinkage()) {
1153 Keep.push_back(GV);
1154 continue;
1155 }
1156
1157 // Only link available_externally definitions if we don't already have a
1158 // definition.
1159 GlobalValue *CombinedGV =
1160 RegularLTO.CombinedModule->getNamedValue(GV->getName());
1161 if (CombinedGV && !CombinedGV->isDeclaration())
1162 continue;
1163
1164 Keep.push_back(GV);
1165 }
1166
1167 return RegularLTO.Mover->move(std::move(Mod.M), Keep, nullptr,
1168 /* IsPerformingImport */ false);
1169}
1170
1171// Add a ThinLTO module to the link.
1172Expected<ArrayRef<SymbolResolution>>
1173LTO::addThinLTO(BitcodeModule BM, ArrayRef<InputFile::Symbol> Syms,
1175 llvm::TimeTraceScope timeScope("LTO add thin LTO");
1176 const auto BMID = BM.getModuleIdentifier();
1177 ArrayRef<SymbolResolution> ResTmp = Res;
1178 for (const InputFile::Symbol &Sym : Syms) {
1179 assert(!ResTmp.empty());
1180 const SymbolResolution &R = ResTmp.consume_front();
1181
1182 if (!Sym.getIRName().empty() && R.Prevailing) {
1184 GlobalValue::getGlobalIdentifier(Sym.getIRName(),
1186 ThinLTO.setPrevailingModuleForGUID(GUID, BMID);
1187 }
1188 }
1189
1190 if (Error Err = BM.readSummary(
1191 ThinLTO.CombinedIndex, BMID, [&](GlobalValue::GUID GUID) {
1192 return ThinLTO.isPrevailingModuleForGUID(GUID, BMID);
1193 }))
1194 return Err;
1195 LLVM_DEBUG(dbgs() << "Module " << BMID << "\n");
1196
1197 for (const InputFile::Symbol &Sym : Syms) {
1198 assert(!Res.empty());
1199 const SymbolResolution &R = Res.consume_front();
1200
1201 if (!Sym.getIRName().empty() &&
1202 (R.Prevailing || R.FinalDefinitionInLinkageUnit)) {
1204 GlobalValue::getGlobalIdentifier(Sym.getIRName(),
1206 if (R.Prevailing) {
1207 assert(ThinLTO.isPrevailingModuleForGUID(GUID, BMID));
1208
1209 // For linker redefined symbols (via --wrap or --defsym) we want to
1210 // switch the linkage to `weak` to prevent IPOs from happening.
1211 // Find the summary in the module for this very GV and record the new
1212 // linkage so that we can switch it when we import the GV.
1213 if (R.LinkerRedefined)
1214 if (auto S = ThinLTO.CombinedIndex.findSummaryInModule(GUID, BMID))
1215 S->setLinkage(GlobalValue::WeakAnyLinkage);
1216 }
1217
1218 // If the linker resolved the symbol to a local definition then mark it
1219 // as local in the summary for the module we are adding.
1220 if (R.FinalDefinitionInLinkageUnit) {
1221 if (auto S = ThinLTO.CombinedIndex.findSummaryInModule(GUID, BMID)) {
1222 S->setDSOLocal(true);
1223 }
1224 }
1225 }
1226 }
1227
1228 if (!ThinLTO.ModuleMap.insert({BMID, BM}).second)
1230 "Expected at most one ThinLTO module per bitcode file",
1232
1233 if (!Conf.ThinLTOModulesToCompile.empty()) {
1234 if (!ThinLTO.ModulesToCompile)
1235 ThinLTO.ModulesToCompile = ModuleMapType();
1236 // This is a fuzzy name matching where only modules with name containing the
1237 // specified switch values are going to be compiled.
1238 for (const std::string &Name : Conf.ThinLTOModulesToCompile) {
1239 if (BMID.contains(Name)) {
1240 ThinLTO.ModulesToCompile->insert({BMID, BM});
1241 LLVM_DEBUG(dbgs() << "[ThinLTO] Selecting " << BMID << " to compile\n");
1242 break;
1243 }
1244 }
1245 }
1246
1247 return Res;
1248}
1249
1250unsigned LTO::getMaxTasks() const {
1251 CalledGetMaxTasks = true;
1252 auto ModuleCount = ThinLTO.ModulesToCompile ? ThinLTO.ModulesToCompile->size()
1253 : ThinLTO.ModuleMap.size();
1254 return RegularLTO.ParallelCodeGenParallelismLevel + ModuleCount;
1255}
1256
1257// If only some of the modules were split, we cannot correctly handle
1258// code that contains type tests or type checked loads.
1259Error LTO::checkPartiallySplit() {
1260 if (!ThinLTO.CombinedIndex.partiallySplitLTOUnits())
1261 return Error::success();
1262
1263 const Module *Combined = RegularLTO.CombinedModule.get();
1264 Function *TypeTestFunc =
1265 Intrinsic::getDeclarationIfExists(Combined, Intrinsic::type_test);
1266 Function *TypeCheckedLoadFunc =
1267 Intrinsic::getDeclarationIfExists(Combined, Intrinsic::type_checked_load);
1268 Function *TypeCheckedLoadRelativeFunc = Intrinsic::getDeclarationIfExists(
1269 Combined, Intrinsic::type_checked_load_relative);
1270
1271 // First check if there are type tests / type checked loads in the
1272 // merged regular LTO module IR.
1273 if ((TypeTestFunc && !TypeTestFunc->use_empty()) ||
1274 (TypeCheckedLoadFunc && !TypeCheckedLoadFunc->use_empty()) ||
1275 (TypeCheckedLoadRelativeFunc &&
1276 !TypeCheckedLoadRelativeFunc->use_empty()))
1278 "inconsistent LTO Unit splitting (recompile with -fsplit-lto-unit)",
1280
1281 // Otherwise check if there are any recorded in the combined summary from the
1282 // ThinLTO modules.
1283 for (auto &P : ThinLTO.CombinedIndex) {
1284 for (auto &S : P.second.getSummaryList()) {
1285 auto *FS = dyn_cast<FunctionSummary>(S.get());
1286 if (!FS)
1287 continue;
1288 if (!FS->type_test_assume_vcalls().empty() ||
1289 !FS->type_checked_load_vcalls().empty() ||
1290 !FS->type_test_assume_const_vcalls().empty() ||
1291 !FS->type_checked_load_const_vcalls().empty() ||
1292 !FS->type_tests().empty())
1294 "inconsistent LTO Unit splitting (recompile with -fsplit-lto-unit)",
1296 }
1297 }
1298 return Error::success();
1299}
1300
1302 llvm::scope_exit CleanUp([this]() { cleanup(); });
1303
1305 return EC;
1306
1307 // Compute "dead" symbols, we don't want to import/export these!
1308 DenseSet<GlobalValue::GUID> GUIDPreservedSymbols;
1309 DenseMap<GlobalValue::GUID, PrevailingType> GUIDPrevailingResolutions;
1310 for (auto &Res : *GlobalResolutions) {
1311 // Normally resolution have IR name of symbol. We can do nothing here
1312 // otherwise. See comments in GlobalResolution struct for more details.
1313 if (Res.second.IRName.empty())
1314 continue;
1315
1317 GlobalValue::dropLLVMManglingEscape(Res.second.IRName));
1318
1319 if (Res.second.VisibleOutsideSummary && Res.second.Prevailing)
1320 GUIDPreservedSymbols.insert(GUID);
1321
1322 if (Res.second.ExportDynamic)
1323 DynamicExportSymbols.insert(GUID);
1324
1325 GUIDPrevailingResolutions[GUID] =
1326 Res.second.Prevailing ? PrevailingType::Yes : PrevailingType::No;
1327 }
1328
1329 auto isPrevailing = [&](GlobalValue::GUID G) {
1330 auto It = GUIDPrevailingResolutions.find(G);
1331 if (It == GUIDPrevailingResolutions.end())
1333 return It->second;
1334 };
1335 computeDeadSymbolsWithConstProp(ThinLTO.CombinedIndex, GUIDPreservedSymbols,
1336 isPrevailing, Conf.OptLevel > 0);
1337
1338 // Setup output file to emit statistics.
1339 auto StatsFileOrErr = setupStatsFile(Conf.StatsFile);
1340 if (!StatsFileOrErr)
1341 return StatsFileOrErr.takeError();
1342 std::unique_ptr<ToolOutputFile> StatsFile = std::move(StatsFileOrErr.get());
1343
1344 if (Error Err = setupOptimizationRemarks())
1345 return Err;
1346
1347 // TODO: Ideally this would be controlled automatically by detecting that we
1348 // are linking with an allocator that supports these interfaces, rather than
1349 // an internal option (which would still be needed for tests, however). For
1350 // example, if the library exported a symbol like __malloc_hot_cold the linker
1351 // could recognize that and set a flag in the lto::Config.
1353 ThinLTO.CombinedIndex.setWithSupportsHotColdNew();
1354
1355 Error Result = runRegularLTO(AddStream);
1356 if (!Result)
1357 // This will reset the GlobalResolutions optional once done with it to
1358 // reduce peak memory before importing.
1359 Result = runThinLTO(AddStream, Cache, GUIDPreservedSymbols);
1360
1361 if (StatsFile)
1362 PrintStatisticsJSON(StatsFile->os());
1363
1364 return Result;
1365}
1366
1367Error LTO::runRegularLTO(AddStreamFn AddStream) {
1368 llvm::TimeTraceScope timeScope("Run regular LTO");
1369 LLVM_DEBUG(dbgs() << "Running regular LTO\n");
1370
1371 // Finalize linking of regular LTO modules containing summaries now that
1372 // we have computed liveness information.
1373 {
1374 llvm::TimeTraceScope timeScope("Link regular LTO");
1375 for (auto &M : RegularLTO.ModsWithSummaries)
1376 if (Error Err = linkRegularLTO(std::move(M), /*LivenessFromIndex=*/true))
1377 return Err;
1378 }
1379
1380 // Ensure we don't have inconsistently split LTO units with type tests.
1381 // FIXME: this checks both LTO and ThinLTO. It happens to work as we take
1382 // this path both cases but eventually this should be split into two and
1383 // do the ThinLTO checks in `runThinLTO`.
1384 if (Error Err = checkPartiallySplit())
1385 return Err;
1386
1387 // Make sure commons have the right size/alignment: we kept the largest from
1388 // all the prevailing when adding the inputs, and we apply it here.
1389 const DataLayout &DL = RegularLTO.CombinedModule->getDataLayout();
1390 for (auto &I : RegularLTO.Commons) {
1391 if (!I.second.Prevailing)
1392 // Don't do anything if no instance of this common was prevailing.
1393 continue;
1394 GlobalVariable *OldGV = RegularLTO.CombinedModule->getNamedGlobal(I.first);
1395 if (OldGV && OldGV->getGlobalSize(DL) == I.second.Size) {
1396 // Don't create a new global if the type is already correct, just make
1397 // sure the alignment is correct.
1398 OldGV->setAlignment(I.second.Alignment);
1399 continue;
1400 }
1401 ArrayType *Ty =
1402 ArrayType::get(Type::getInt8Ty(RegularLTO.Ctx), I.second.Size);
1403 auto *GV = new GlobalVariable(*RegularLTO.CombinedModule, Ty, false,
1406 GV->setAlignment(I.second.Alignment);
1407 if (OldGV) {
1408 OldGV->replaceAllUsesWith(GV);
1409 GV->takeName(OldGV);
1410 OldGV->eraseFromParent();
1411 } else {
1412 GV->setName(I.first);
1413 }
1414 }
1415
1416 bool WholeProgramVisibilityEnabledInLTO =
1417 Conf.HasWholeProgramVisibility &&
1418 // If validation is enabled, upgrade visibility only when all vtables
1419 // have typeinfos.
1420 (!Conf.ValidateAllVtablesHaveTypeInfos || Conf.AllVtablesHaveTypeInfos);
1421
1422 // This returns true when the name is local or not defined. Locals are
1423 // expected to be handled separately.
1424 auto IsVisibleToRegularObj = [&](StringRef name) {
1425 auto It = GlobalResolutions->find(name);
1426 return (It == GlobalResolutions->end() ||
1427 It->second.VisibleOutsideSummary || !It->second.Prevailing);
1428 };
1429
1430 // If allowed, upgrade public vcall visibility metadata to linkage unit
1431 // visibility before whole program devirtualization in the optimizer.
1433 *RegularLTO.CombinedModule, WholeProgramVisibilityEnabledInLTO,
1434 DynamicExportSymbols, Conf.ValidateAllVtablesHaveTypeInfos,
1435 IsVisibleToRegularObj);
1436 updatePublicTypeTestCalls(*RegularLTO.CombinedModule,
1437 WholeProgramVisibilityEnabledInLTO);
1438
1439 if (Conf.PreOptModuleHook &&
1440 !Conf.PreOptModuleHook(0, *RegularLTO.CombinedModule))
1441 return Error::success();
1442
1443 if (!Conf.CodeGenOnly) {
1444 for (const auto &R : *GlobalResolutions) {
1445 GlobalValue *GV =
1446 RegularLTO.CombinedModule->getNamedValue(R.second.IRName);
1447 if (!R.second.isPrevailingIRSymbol())
1448 continue;
1449 if (R.second.Partition != 0 &&
1450 R.second.Partition != GlobalResolution::External)
1451 continue;
1452
1453 // Ignore symbols defined in other partitions.
1454 // Also skip declarations, which are not allowed to have internal linkage.
1455 if (!GV || GV->hasLocalLinkage() || GV->isDeclaration())
1456 continue;
1457
1458 // Symbols that are marked DLLImport or DLLExport should not be
1459 // internalized, as they are either externally visible or referencing
1460 // external symbols. Symbols that have AvailableExternally or Appending
1461 // linkage might be used by future passes and should be kept as is.
1462 // These linkages are seen in Unified regular LTO, because the process
1463 // of creating split LTO units introduces symbols with that linkage into
1464 // one of the created modules. Normally, only the ThinLTO backend would
1465 // compile this module, but Unified Regular LTO processes both
1466 // modules created by the splitting process as regular LTO modules.
1467 if ((LTOMode == LTOKind::LTOK_UnifiedRegular) &&
1470 continue;
1471
1472 GV->setUnnamedAddr(R.second.UnnamedAddr ? GlobalValue::UnnamedAddr::Global
1474 if (EnableLTOInternalization && R.second.Partition == 0)
1476 }
1477
1478 if (Conf.PostInternalizeModuleHook &&
1479 !Conf.PostInternalizeModuleHook(0, *RegularLTO.CombinedModule))
1480 return Error::success();
1481 }
1482
1483 if (!RegularLTO.EmptyCombinedModule || Conf.AlwaysEmitRegularLTOObj) {
1484 if (Error Err = backend(
1485 Conf, AddStream, RegularLTO.ParallelCodeGenParallelismLevel,
1486 *RegularLTO.CombinedModule, ThinLTO.CombinedIndex, BitcodeLibFuncs))
1487 return Err;
1488 }
1489
1490 return Error::success();
1491}
1492
1494 RTLIB::RuntimeLibcallsInfo Libcalls(TT);
1495 SmallVector<const char *> LibcallSymbols;
1496 LibcallSymbols.reserve(Libcalls.getNumAvailableLibcallImpls());
1497
1498 for (RTLIB::LibcallImpl Impl : RTLIB::libcall_impls()) {
1499 if (Libcalls.isAvailable(Impl))
1500 LibcallSymbols.push_back(Libcalls.getLibcallImplName(Impl).data());
1501 }
1502
1503 return LibcallSymbols;
1504}
1505
1507 StringSaver &Saver) {
1508 auto TLII = std::make_unique<TargetLibraryInfoImpl>(TT);
1509 TargetLibraryInfo TLI(*TLII);
1510 SmallVector<StringRef> LibFuncSymbols;
1511 LibFuncSymbols.reserve(LibFunc::NumLibFuncs);
1512 for (unsigned I = LibFunc::Begin_LibFunc; I != LibFunc::End_LibFunc; ++I) {
1513 LibFunc F = static_cast<LibFunc>(I);
1514 if (TLI.has(F))
1515 LibFuncSymbols.push_back(Saver.save(TLI.getName(F)).data());
1516 }
1517 return LibFuncSymbols;
1518}
1519
1521 const FunctionImporter::ImportMapTy &ImportList, llvm::StringRef ModulePath,
1522 const std::string &NewModulePath) const {
1523 return emitFiles(ImportList, ModulePath, NewModulePath,
1524 NewModulePath + ".thinlto.bc",
1525 /*ImportsFiles=*/std::nullopt);
1526}
1527
1529 const FunctionImporter::ImportMapTy &ImportList, llvm::StringRef ModulePath,
1530 const std::string &NewModulePath, StringRef SummaryPath,
1531 std::optional<std::reference_wrapper<ImportsFilesContainer>> ImportsFiles)
1532 const {
1533 ModuleToSummariesForIndexTy ModuleToSummariesForIndex;
1534 GVSummaryPtrSet DeclarationSummaries;
1535
1536 std::error_code EC;
1538 ImportList, ModuleToSummariesForIndex,
1539 DeclarationSummaries);
1540
1541 raw_fd_ostream OS(SummaryPath, EC, sys::fs::OpenFlags::OF_None);
1542 if (EC)
1543 return createFileError("cannot open " + Twine(SummaryPath), EC);
1544
1545 writeIndexToFile(CombinedIndex, OS, &ModuleToSummariesForIndex,
1546 &DeclarationSummaries);
1547
1549 Error ImportsFilesError = EmitImportsFiles(
1550 ModulePath, NewModulePath + ".imports", ModuleToSummariesForIndex);
1551 if (ImportsFilesError)
1552 return ImportsFilesError;
1553 }
1554
1555 // Optionally, store the imports files.
1556 if (ImportsFiles)
1558 ModulePath, ModuleToSummariesForIndex,
1559 [&](StringRef M) { ImportsFiles->get().push_back(M.str()); });
1560
1561 return Error::success();
1562}
1563
1564namespace {
1565/// Base class for ThinLTO backends that perform code generation and insert the
1566/// generated files back into the link.
1567class CGThinBackend : public ThinBackendProc {
1568protected:
1569 DenseSet<GlobalValue::GUID> CfiFunctionDefs;
1570 DenseSet<GlobalValue::GUID> CfiFunctionDecls;
1571 bool ShouldEmitIndexFiles;
1572
1573public:
1574 CGThinBackend(
1575 const Config &Conf, ModuleSummaryIndex &CombinedIndex,
1576 const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1577 lto::IndexWriteCallback OnWrite, bool ShouldEmitIndexFiles,
1578 bool ShouldEmitImportsFiles, ThreadPoolStrategy ThinLTOParallelism)
1579 : ThinBackendProc(Conf, CombinedIndex, ModuleToDefinedGVSummaries,
1580 OnWrite, ShouldEmitImportsFiles, ThinLTOParallelism),
1581 ShouldEmitIndexFiles(ShouldEmitIndexFiles) {
1582 auto &Defs = CombinedIndex.cfiFunctionDefs();
1583 CfiFunctionDefs.insert_range(Defs.guids());
1584 auto &Decls = CombinedIndex.cfiFunctionDecls();
1585 CfiFunctionDecls.insert_range(Decls.guids());
1586 }
1587};
1588
1589/// This backend performs code generation by scheduling a job to run on
1590/// an in-process thread when invoked for each task.
1591class InProcessThinBackend : public CGThinBackend {
1592protected:
1593 // Callback used to add generated native object files to the link by code
1594 // generating directly into the returned output stream.
1595 AddStreamFn AddStream;
1596 FileCache Cache;
1597 ArrayRef<StringRef> BitcodeLibFuncs;
1598
1599public:
1600 InProcessThinBackend(
1601 const Config &Conf, ModuleSummaryIndex &CombinedIndex,
1602 ThreadPoolStrategy ThinLTOParallelism,
1603 const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1604 AddStreamFn AddStream, FileCache Cache, lto::IndexWriteCallback OnWrite,
1605 bool ShouldEmitIndexFiles, bool ShouldEmitImportsFiles,
1606 ArrayRef<StringRef> BitcodeLibFuncs)
1607 : CGThinBackend(Conf, CombinedIndex, ModuleToDefinedGVSummaries, OnWrite,
1608 ShouldEmitIndexFiles, ShouldEmitImportsFiles,
1609 ThinLTOParallelism),
1610 AddStream(std::move(AddStream)), Cache(std::move(Cache)),
1611 BitcodeLibFuncs(BitcodeLibFuncs) {}
1612
1613 virtual Error runThinLTOBackendThread(
1614 AddStreamFn AddStream, FileCache Cache, unsigned Task, BitcodeModule BM,
1615 ModuleSummaryIndex &CombinedIndex,
1616 const FunctionImporter::ImportMapTy &ImportList,
1617 const FunctionImporter::ExportSetTy &ExportList,
1618 const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
1619 const GVSummaryMapTy &DefinedGlobals,
1620 MapVector<StringRef, BitcodeModule> &ModuleMap) {
1621 auto ModuleID = BM.getModuleIdentifier();
1622 llvm::TimeTraceScope timeScope("Run ThinLTO backend thread (in-process)",
1623 ModuleID);
1624 auto RunThinBackend = [&](AddStreamFn AddStream) {
1625 LTOLLVMContext BackendContext(Conf);
1626 Expected<std::unique_ptr<Module>> MOrErr = BM.parseModule(BackendContext);
1627 if (!MOrErr)
1628 return MOrErr.takeError();
1629
1630 return thinBackend(Conf, Task, AddStream, **MOrErr, CombinedIndex,
1631 ImportList, DefinedGlobals, &ModuleMap,
1632 Conf.CodeGenOnly, BitcodeLibFuncs);
1633 };
1634 if (ShouldEmitIndexFiles) {
1635 if (auto E = emitFiles(ImportList, ModuleID, ModuleID.str()))
1636 return E;
1637 }
1638
1639 if (!Cache.isValid() || !CombinedIndex.modulePaths().count(ModuleID) ||
1640 all_of(CombinedIndex.getModuleHash(ModuleID),
1641 [](uint32_t V) { return V == 0; }))
1642 // Cache disabled or no entry for this module in the combined index or
1643 // no module hash.
1644 return RunThinBackend(AddStream);
1645
1646 // The module may be cached, this helps handling it.
1647 std::string Key = computeLTOCacheKey(
1648 Conf, CombinedIndex, ModuleID, ImportList, ExportList, ResolvedODR,
1649 DefinedGlobals, CfiFunctionDefs, CfiFunctionDecls);
1650 Expected<AddStreamFn> CacheAddStreamOrErr = Cache(Task, Key, ModuleID);
1651 if (Error Err = CacheAddStreamOrErr.takeError())
1652 return Err;
1653 AddStreamFn &CacheAddStream = *CacheAddStreamOrErr;
1654 if (CacheAddStream)
1655 return RunThinBackend(CacheAddStream);
1656
1657 return Error::success();
1658 }
1659
1660 Error start(
1661 unsigned Task, BitcodeModule BM,
1662 const FunctionImporter::ImportMapTy &ImportList,
1663 const FunctionImporter::ExportSetTy &ExportList,
1664 const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
1665 MapVector<StringRef, BitcodeModule> &ModuleMap) override {
1666 StringRef ModulePath = BM.getModuleIdentifier();
1667 assert(ModuleToDefinedGVSummaries.count(ModulePath));
1668 const GVSummaryMapTy &DefinedGlobals =
1669 ModuleToDefinedGVSummaries.find(ModulePath)->second;
1670 BackendThreadPool.async(
1671 [=](BitcodeModule BM, ModuleSummaryIndex &CombinedIndex,
1672 const FunctionImporter::ImportMapTy &ImportList,
1673 const FunctionImporter::ExportSetTy &ExportList,
1674 const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>
1675 &ResolvedODR,
1676 const GVSummaryMapTy &DefinedGlobals,
1677 MapVector<StringRef, BitcodeModule> &ModuleMap) {
1678 if (LLVM_ENABLE_THREADS && Conf.TimeTraceEnabled)
1680 "thin backend");
1681 Error E = runThinLTOBackendThread(
1682 AddStream, Cache, Task, BM, CombinedIndex, ImportList, ExportList,
1683 ResolvedODR, DefinedGlobals, ModuleMap);
1684 if (E) {
1685 std::unique_lock<std::mutex> L(ErrMu);
1686 if (Err)
1687 Err = joinErrors(std::move(*Err), std::move(E));
1688 else
1689 Err = std::move(E);
1690 }
1691 if (LLVM_ENABLE_THREADS && Conf.TimeTraceEnabled)
1693 },
1694 BM, std::ref(CombinedIndex), std::ref(ImportList), std::ref(ExportList),
1695 std::ref(ResolvedODR), std::ref(DefinedGlobals), std::ref(ModuleMap));
1696
1697 if (OnWrite)
1698 OnWrite(std::string(ModulePath));
1699 return Error::success();
1700 }
1701};
1702
1703/// This backend is utilized in the first round of a two-codegen round process.
1704/// It first saves optimized bitcode files to disk before the codegen process
1705/// begins. After codegen, it stores the resulting object files in a scratch
1706/// buffer. Note the codegen data stored in the scratch buffer will be extracted
1707/// and merged in the subsequent step.
1708class FirstRoundThinBackend : public InProcessThinBackend {
1709 AddStreamFn IRAddStream;
1710 FileCache IRCache;
1711
1712public:
1713 FirstRoundThinBackend(
1714 const Config &Conf, ModuleSummaryIndex &CombinedIndex,
1715 ThreadPoolStrategy ThinLTOParallelism,
1716 const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1717 AddStreamFn CGAddStream, FileCache CGCache,
1718 ArrayRef<StringRef> BitcodeLibFuncs, AddStreamFn IRAddStream,
1719 FileCache IRCache)
1720 : InProcessThinBackend(Conf, CombinedIndex, ThinLTOParallelism,
1721 ModuleToDefinedGVSummaries, std::move(CGAddStream),
1722 std::move(CGCache), /*OnWrite=*/nullptr,
1723 /*ShouldEmitIndexFiles=*/false,
1724 /*ShouldEmitImportsFiles=*/false, BitcodeLibFuncs),
1725 IRAddStream(std::move(IRAddStream)), IRCache(std::move(IRCache)) {}
1726
1727 Error runThinLTOBackendThread(
1728 AddStreamFn CGAddStream, FileCache CGCache, unsigned Task,
1729 BitcodeModule BM, ModuleSummaryIndex &CombinedIndex,
1730 const FunctionImporter::ImportMapTy &ImportList,
1731 const FunctionImporter::ExportSetTy &ExportList,
1732 const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
1733 const GVSummaryMapTy &DefinedGlobals,
1734 MapVector<StringRef, BitcodeModule> &ModuleMap) override {
1735 auto ModuleID = BM.getModuleIdentifier();
1736 llvm::TimeTraceScope timeScope("Run ThinLTO backend thread (first round)",
1737 ModuleID);
1738 auto RunThinBackend = [&](AddStreamFn CGAddStream,
1739 AddStreamFn IRAddStream) {
1740 LTOLLVMContext BackendContext(Conf);
1741 Expected<std::unique_ptr<Module>> MOrErr = BM.parseModule(BackendContext);
1742 if (!MOrErr)
1743 return MOrErr.takeError();
1744
1745 return thinBackend(Conf, Task, CGAddStream, **MOrErr, CombinedIndex,
1746 ImportList, DefinedGlobals, &ModuleMap,
1747 Conf.CodeGenOnly, BitcodeLibFuncs, IRAddStream);
1748 };
1749 // Like InProcessThinBackend, we produce index files as needed for
1750 // FirstRoundThinBackend. However, these files are not generated for
1751 // SecondRoundThinBackend.
1752 if (ShouldEmitIndexFiles) {
1753 if (auto E = emitFiles(ImportList, ModuleID, ModuleID.str()))
1754 return E;
1755 }
1756
1757 assert((CGCache.isValid() == IRCache.isValid()) &&
1758 "Both caches for CG and IR should have matching availability");
1759 if (!CGCache.isValid() || !CombinedIndex.modulePaths().count(ModuleID) ||
1760 all_of(CombinedIndex.getModuleHash(ModuleID),
1761 [](uint32_t V) { return V == 0; }))
1762 // Cache disabled or no entry for this module in the combined index or
1763 // no module hash.
1764 return RunThinBackend(CGAddStream, IRAddStream);
1765
1766 // Get CGKey for caching object in CGCache.
1767 std::string CGKey = computeLTOCacheKey(
1768 Conf, CombinedIndex, ModuleID, ImportList, ExportList, ResolvedODR,
1769 DefinedGlobals, CfiFunctionDefs, CfiFunctionDecls);
1770 Expected<AddStreamFn> CacheCGAddStreamOrErr =
1771 CGCache(Task, CGKey, ModuleID);
1772 if (Error Err = CacheCGAddStreamOrErr.takeError())
1773 return Err;
1774 AddStreamFn &CacheCGAddStream = *CacheCGAddStreamOrErr;
1775
1776 // Get IRKey for caching (optimized) IR in IRCache with an extra ID.
1777 std::string IRKey = recomputeLTOCacheKey(CGKey, /*ExtraID=*/"IR");
1778 Expected<AddStreamFn> CacheIRAddStreamOrErr =
1779 IRCache(Task, IRKey, ModuleID);
1780 if (Error Err = CacheIRAddStreamOrErr.takeError())
1781 return Err;
1782 AddStreamFn &CacheIRAddStream = *CacheIRAddStreamOrErr;
1783
1784 // Ideally, both CG and IR caching should be synchronized. However, in
1785 // practice, their availability may differ due to different expiration
1786 // times. Therefore, if either cache is missing, the backend process is
1787 // triggered.
1788 if (CacheCGAddStream || CacheIRAddStream) {
1789 LLVM_DEBUG(dbgs() << "[FirstRound] Cache Miss for "
1790 << BM.getModuleIdentifier() << "\n");
1791 return RunThinBackend(CacheCGAddStream ? CacheCGAddStream : CGAddStream,
1792 CacheIRAddStream ? CacheIRAddStream : IRAddStream);
1793 }
1794
1795 return Error::success();
1796 }
1797};
1798
1799/// This backend operates in the second round of a two-codegen round process.
1800/// It starts by reading the optimized bitcode files that were saved during the
1801/// first round. The backend then executes the codegen only to further optimize
1802/// the code, utilizing the codegen data merged from the first round. Finally,
1803/// it writes the resulting object files as usual.
1804class SecondRoundThinBackend : public InProcessThinBackend {
1805 std::unique_ptr<SmallVector<StringRef>> IRFiles;
1806 stable_hash CombinedCGDataHash;
1807
1808public:
1809 SecondRoundThinBackend(
1810 const Config &Conf, ModuleSummaryIndex &CombinedIndex,
1811 ThreadPoolStrategy ThinLTOParallelism,
1812 const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1813 AddStreamFn AddStream, FileCache Cache,
1814 ArrayRef<StringRef> BitcodeLibFuncs,
1815 std::unique_ptr<SmallVector<StringRef>> IRFiles,
1816 stable_hash CombinedCGDataHash)
1817 : InProcessThinBackend(Conf, CombinedIndex, ThinLTOParallelism,
1818 ModuleToDefinedGVSummaries, std::move(AddStream),
1819 std::move(Cache),
1820 /*OnWrite=*/nullptr,
1821 /*ShouldEmitIndexFiles=*/false,
1822 /*ShouldEmitImportsFiles=*/false, BitcodeLibFuncs),
1823 IRFiles(std::move(IRFiles)), CombinedCGDataHash(CombinedCGDataHash) {}
1824
1825 Error runThinLTOBackendThread(
1826 AddStreamFn AddStream, FileCache Cache, unsigned Task, BitcodeModule BM,
1827 ModuleSummaryIndex &CombinedIndex,
1828 const FunctionImporter::ImportMapTy &ImportList,
1829 const FunctionImporter::ExportSetTy &ExportList,
1830 const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
1831 const GVSummaryMapTy &DefinedGlobals,
1832 MapVector<StringRef, BitcodeModule> &ModuleMap) override {
1833 auto ModuleID = BM.getModuleIdentifier();
1834 llvm::TimeTraceScope timeScope("Run ThinLTO backend thread (second round)",
1835 ModuleID);
1836 auto RunThinBackend = [&](AddStreamFn AddStream) {
1837 LTOLLVMContext BackendContext(Conf);
1838 std::unique_ptr<Module> LoadedModule =
1839 cgdata::loadModuleForTwoRounds(BM, Task, BackendContext, *IRFiles);
1840
1841 return thinBackend(Conf, Task, AddStream, *LoadedModule, CombinedIndex,
1842 ImportList, DefinedGlobals, &ModuleMap,
1843 /*CodeGenOnly=*/true, BitcodeLibFuncs);
1844 };
1845 if (!Cache.isValid() || !CombinedIndex.modulePaths().count(ModuleID) ||
1846 all_of(CombinedIndex.getModuleHash(ModuleID),
1847 [](uint32_t V) { return V == 0; }))
1848 // Cache disabled or no entry for this module in the combined index or
1849 // no module hash.
1850 return RunThinBackend(AddStream);
1851
1852 // Get Key for caching the final object file in Cache with the combined
1853 // CGData hash.
1854 std::string Key = computeLTOCacheKey(
1855 Conf, CombinedIndex, ModuleID, ImportList, ExportList, ResolvedODR,
1856 DefinedGlobals, CfiFunctionDefs, CfiFunctionDecls);
1858 /*ExtraID=*/std::to_string(CombinedCGDataHash));
1859 Expected<AddStreamFn> CacheAddStreamOrErr = Cache(Task, Key, ModuleID);
1860 if (Error Err = CacheAddStreamOrErr.takeError())
1861 return Err;
1862 AddStreamFn &CacheAddStream = *CacheAddStreamOrErr;
1863
1864 if (CacheAddStream) {
1865 LLVM_DEBUG(dbgs() << "[SecondRound] Cache Miss for "
1866 << BM.getModuleIdentifier() << "\n");
1867 return RunThinBackend(CacheAddStream);
1868 }
1869
1870 return Error::success();
1871 }
1872};
1873} // end anonymous namespace
1874
1877 bool ShouldEmitIndexFiles,
1878 bool ShouldEmitImportsFiles) {
1879 auto Func =
1880 [=](const Config &Conf, ModuleSummaryIndex &CombinedIndex,
1881 const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1882 AddStreamFn AddStream, FileCache Cache,
1883 ArrayRef<StringRef> BitcodeLibFuncs) {
1884 return std::make_unique<InProcessThinBackend>(
1885 Conf, CombinedIndex, Parallelism, ModuleToDefinedGVSummaries,
1886 AddStream, Cache, OnWrite, ShouldEmitIndexFiles,
1887 ShouldEmitImportsFiles, BitcodeLibFuncs);
1888 };
1889 return ThinBackend(Func, Parallelism);
1890}
1891
1893 if (!TheTriple.isOSDarwin())
1894 return "";
1895 if (TheTriple.getArch() == Triple::x86_64)
1896 return "core2";
1897 if (TheTriple.getArch() == Triple::x86)
1898 return "yonah";
1899 if (TheTriple.isArm64e())
1900 return "apple-a12";
1901 if (TheTriple.getArch() == Triple::aarch64 ||
1902 TheTriple.getArch() == Triple::aarch64_32)
1903 return "cyclone";
1904 return "";
1905}
1906
1907// Given the original \p Path to an output file, replace any path
1908// prefix matching \p OldPrefix with \p NewPrefix. Also, create the
1909// resulting directory if it does not yet exist.
1911 StringRef NewPrefix) {
1912 if (OldPrefix.empty() && NewPrefix.empty())
1913 return std::string(Path);
1914 SmallString<128> NewPath(Path);
1915 llvm::sys::path::replace_path_prefix(NewPath, OldPrefix, NewPrefix);
1916 StringRef ParentPath = llvm::sys::path::parent_path(NewPath.str());
1917 if (!ParentPath.empty()) {
1918 // Make sure the new directory exists, creating it if necessary.
1919 if (std::error_code EC = llvm::sys::fs::create_directories(ParentPath))
1920 llvm::errs() << "warning: could not create directory '" << ParentPath
1921 << "': " << EC.message() << '\n';
1922 }
1923 return std::string(NewPath);
1924}
1925
1926namespace {
1927class WriteIndexesThinBackend : public ThinBackendProc {
1928 std::string OldPrefix, NewPrefix, NativeObjectPrefix;
1929 raw_fd_ostream *LinkedObjectsFile;
1930
1931public:
1932 WriteIndexesThinBackend(
1933 const Config &Conf, ModuleSummaryIndex &CombinedIndex,
1934 ThreadPoolStrategy ThinLTOParallelism,
1935 const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1936 std::string OldPrefix, std::string NewPrefix,
1937 std::string NativeObjectPrefix, bool ShouldEmitImportsFiles,
1938 raw_fd_ostream *LinkedObjectsFile, lto::IndexWriteCallback OnWrite)
1939 : ThinBackendProc(Conf, CombinedIndex, ModuleToDefinedGVSummaries,
1940 OnWrite, ShouldEmitImportsFiles, ThinLTOParallelism),
1941 OldPrefix(OldPrefix), NewPrefix(NewPrefix),
1942 NativeObjectPrefix(NativeObjectPrefix),
1943 LinkedObjectsFile(LinkedObjectsFile) {}
1944
1945 Error start(
1946 unsigned Task, BitcodeModule BM,
1947 const FunctionImporter::ImportMapTy &ImportList,
1948 const FunctionImporter::ExportSetTy &ExportList,
1949 const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
1950 MapVector<StringRef, BitcodeModule> &ModuleMap) override {
1951 StringRef ModulePath = BM.getModuleIdentifier();
1952
1953 // The contents of this file may be used as input to a native link, and must
1954 // therefore contain the processed modules in a determinstic order that
1955 // match the order they are provided on the command line. For that reason,
1956 // we cannot include this in the asynchronously executed lambda below.
1957 if (LinkedObjectsFile) {
1958 std::string ObjectPrefix =
1959 NativeObjectPrefix.empty() ? NewPrefix : NativeObjectPrefix;
1960 std::string LinkedObjectsFilePath =
1961 getThinLTOOutputFile(ModulePath, OldPrefix, ObjectPrefix);
1962 *LinkedObjectsFile << LinkedObjectsFilePath << '\n';
1963 }
1964
1965 BackendThreadPool.async(
1966 [this](const StringRef ModulePath,
1967 const FunctionImporter::ImportMapTy &ImportList,
1968 const std::string &OldPrefix, const std::string &NewPrefix) {
1969 std::string NewModulePath =
1970 getThinLTOOutputFile(ModulePath, OldPrefix, NewPrefix);
1971 auto E = emitFiles(ImportList, ModulePath, NewModulePath);
1972 if (E) {
1973 std::unique_lock<std::mutex> L(ErrMu);
1974 if (Err)
1975 Err = joinErrors(std::move(*Err), std::move(E));
1976 else
1977 Err = std::move(E);
1978 return;
1979 }
1980 },
1981 ModulePath, ImportList, OldPrefix, NewPrefix);
1982
1983 if (OnWrite)
1984 OnWrite(std::string(ModulePath));
1985 return Error::success();
1986 }
1987
1988 bool isSensitiveToInputOrder() override {
1989 // The order which modules are written to LinkedObjectsFile should be
1990 // deterministic and match the order they are passed on the command line.
1991 return true;
1992 }
1993};
1994} // end anonymous namespace
1995
1997 ThreadPoolStrategy Parallelism, std::string OldPrefix,
1998 std::string NewPrefix, std::string NativeObjectPrefix,
1999 bool ShouldEmitImportsFiles, raw_fd_ostream *LinkedObjectsFile,
2000 IndexWriteCallback OnWrite) {
2001 auto Func =
2002 [=](const Config &Conf, ModuleSummaryIndex &CombinedIndex,
2003 const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
2004 AddStreamFn AddStream, FileCache Cache,
2005 ArrayRef<StringRef> BitcodeLibFuncs) {
2006 return std::make_unique<WriteIndexesThinBackend>(
2007 Conf, CombinedIndex, Parallelism, ModuleToDefinedGVSummaries,
2008 OldPrefix, NewPrefix, NativeObjectPrefix, ShouldEmitImportsFiles,
2009 LinkedObjectsFile, OnWrite);
2010 };
2011 return ThinBackend(Func, Parallelism);
2012}
2013
2014Error LTO::runThinLTO(AddStreamFn AddStream, FileCache Cache,
2015 const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols) {
2016 llvm::TimeTraceScope timeScope("Run ThinLTO");
2017 LLVM_DEBUG(dbgs() << "Running ThinLTO\n");
2018 ThinLTO.CombinedIndex.releaseTemporaryMemory();
2019 timeTraceProfilerBegin("ThinLink", StringRef(""));
2020 llvm::scope_exit TimeTraceScopeExit([]() {
2023 });
2024 if (ThinLTO.ModuleMap.empty())
2025 return Error::success();
2026
2027 if (ThinLTO.ModulesToCompile && ThinLTO.ModulesToCompile->empty()) {
2028 llvm::errs() << "warning: [ThinLTO] No module compiled\n";
2029 return Error::success();
2030 }
2031
2032 if (Conf.CombinedIndexHook &&
2033 !Conf.CombinedIndexHook(ThinLTO.CombinedIndex, GUIDPreservedSymbols))
2034 return Error::success();
2035
2036 // Collect for each module the list of function it defines (GUID ->
2037 // Summary).
2038 DenseMap<StringRef, GVSummaryMapTy> ModuleToDefinedGVSummaries(
2039 ThinLTO.ModuleMap.size());
2040 ThinLTO.CombinedIndex.collectDefinedGVSummariesPerModule(
2041 ModuleToDefinedGVSummaries);
2042 // Create entries for any modules that didn't have any GV summaries
2043 // (either they didn't have any GVs to start with, or we suppressed
2044 // generation of the summaries because they e.g. had inline assembly
2045 // uses that couldn't be promoted/renamed on export). This is so
2046 // InProcessThinBackend::start can still launch a backend thread, which
2047 // is passed the map of summaries for the module, without any special
2048 // handling for this case.
2049 for (auto &Mod : ThinLTO.ModuleMap)
2050 if (!ModuleToDefinedGVSummaries.count(Mod.first))
2051 ModuleToDefinedGVSummaries.try_emplace(Mod.first);
2052
2053 FunctionImporter::ImportListsTy ImportLists(ThinLTO.ModuleMap.size());
2054 DenseMap<StringRef, FunctionImporter::ExportSetTy> ExportLists(
2055 ThinLTO.ModuleMap.size());
2056 StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>> ResolvedODR;
2057
2058 if (DumpThinCGSCCs)
2059 ThinLTO.CombinedIndex.dumpSCCs(outs());
2060
2061 std::set<GlobalValue::GUID> ExportedGUIDs;
2062
2063 bool WholeProgramVisibilityEnabledInLTO =
2064 Conf.HasWholeProgramVisibility &&
2065 // If validation is enabled, upgrade visibility only when all vtables
2066 // have typeinfos.
2067 (!Conf.ValidateAllVtablesHaveTypeInfos || Conf.AllVtablesHaveTypeInfos);
2068 if (hasWholeProgramVisibility(WholeProgramVisibilityEnabledInLTO))
2069 ThinLTO.CombinedIndex.setWithWholeProgramVisibility();
2070
2071 // If we're validating, get the vtable symbols that should not be
2072 // upgraded because they correspond to typeIDs outside of index-based
2073 // WPD info.
2074 DenseSet<GlobalValue::GUID> VisibleToRegularObjSymbols;
2075 if (WholeProgramVisibilityEnabledInLTO &&
2076 Conf.ValidateAllVtablesHaveTypeInfos) {
2077 // This returns true when the name is local or not defined. Locals are
2078 // expected to be handled separately.
2079 auto IsVisibleToRegularObj = [&](StringRef name) {
2080 auto It = GlobalResolutions->find(name);
2081 return (It == GlobalResolutions->end() ||
2082 It->second.VisibleOutsideSummary || !It->second.Prevailing);
2083 };
2084
2085 getVisibleToRegularObjVtableGUIDs(ThinLTO.CombinedIndex,
2086 VisibleToRegularObjSymbols,
2087 IsVisibleToRegularObj);
2088 }
2089
2090 // If allowed, upgrade public vcall visibility to linkage unit visibility in
2091 // the summaries before whole program devirtualization below.
2093 ThinLTO.CombinedIndex, WholeProgramVisibilityEnabledInLTO,
2094 DynamicExportSymbols, VisibleToRegularObjSymbols);
2095
2096 // Perform index-based WPD. This will return immediately if there are
2097 // no index entries in the typeIdMetadata map (e.g. if we are instead
2098 // performing IR-based WPD in hybrid regular/thin LTO mode).
2099 std::map<ValueInfo, std::vector<VTableSlotSummary>> LocalWPDTargetsMap;
2100 DenseSet<StringRef> ExternallyVisibleSymbolNames;
2101
2102 // Used by the promotion-time renaming logic. When non-null, this set
2103 // identifies symbols that should not be renamed during promotion.
2104 // It is non-null only when whole-program visibility is enabled and
2105 // renaming is not forced. Otherwise, the default renaming behavior applies.
2106 DenseSet<StringRef> *ExternallyVisibleSymbolNamesPtr =
2107 (WholeProgramVisibilityEnabledInLTO && !AlwaysRenamePromotedLocals)
2108 ? &ExternallyVisibleSymbolNames
2109 : nullptr;
2110 runWholeProgramDevirtOnIndex(ThinLTO.CombinedIndex, ExportedGUIDs,
2111 LocalWPDTargetsMap,
2112 ExternallyVisibleSymbolNamesPtr);
2113
2114 auto isPrevailing = [&](GlobalValue::GUID GUID, const GlobalValueSummary *S) {
2115 return ThinLTO.isPrevailingModuleForGUID(GUID, S->modulePath());
2116 };
2118 MemProfContextDisambiguation ContextDisambiguation;
2119 ContextDisambiguation.run(
2120 ThinLTO.CombinedIndex, isPrevailing, RegularLTO.Ctx,
2121 [&](StringRef PassName, StringRef RemarkName, const Twine &Msg) {
2122 auto R = OptimizationRemark(PassName.data(), RemarkName,
2123 LinkerRemarkFunction);
2124 R << Msg.str();
2125 emitRemark(R);
2126 });
2127 }
2128
2129 // Figure out which symbols need to be internalized. This also needs to happen
2130 // at -O0 because summary-based DCE is implemented using internalization, and
2131 // we must apply DCE consistently with the full LTO module in order to avoid
2132 // undefined references during the final link.
2133 for (auto &Res : *GlobalResolutions) {
2134 // If the symbol does not have external references or it is not prevailing,
2135 // then not need to mark it as exported from a ThinLTO partition.
2136 if (Res.second.Partition != GlobalResolution::External ||
2137 !Res.second.isPrevailingIRSymbol())
2138 continue;
2140 GlobalValue::dropLLVMManglingEscape(Res.second.IRName));
2141 // Mark exported unless index-based analysis determined it to be dead.
2142 if (ThinLTO.CombinedIndex.isGUIDLive(GUID))
2143 ExportedGUIDs.insert(GUID);
2144 }
2145
2146 // Reset the GlobalResolutions to deallocate the associated memory, as there
2147 // are no further accesses. We specifically want to do this before computing
2148 // cross module importing, which adds to peak memory via the computed import
2149 // and export lists.
2150 releaseGlobalResolutionsMemory();
2151
2152 if (Conf.OptLevel > 0)
2153 ComputeCrossModuleImport(ThinLTO.CombinedIndex, ModuleToDefinedGVSummaries,
2154 isPrevailing, ImportLists, ExportLists);
2155
2156 // Any functions referenced by the jump table in the regular LTO object must
2157 // be exported.
2158 auto &Defs = ThinLTO.CombinedIndex.cfiFunctionDefs();
2159 ExportedGUIDs.insert(Defs.guid_begin(), Defs.guid_end());
2160 auto &Decls = ThinLTO.CombinedIndex.cfiFunctionDecls();
2161 ExportedGUIDs.insert(Decls.guid_begin(), Decls.guid_end());
2162
2163 auto isExported = [&](StringRef ModuleIdentifier, ValueInfo VI) {
2164 const auto &ExportList = ExportLists.find(ModuleIdentifier);
2165 return (ExportList != ExportLists.end() && ExportList->second.count(VI)) ||
2166 ExportedGUIDs.count(VI.getGUID());
2167 };
2168
2169 // Update local devirtualized targets that were exported by cross-module
2170 // importing or by other devirtualizations marked in the ExportedGUIDs set.
2171 updateIndexWPDForExports(ThinLTO.CombinedIndex, isExported,
2172 LocalWPDTargetsMap, ExternallyVisibleSymbolNamesPtr);
2173
2174 if (ExternallyVisibleSymbolNamesPtr) {
2175 // Add to ExternallyVisibleSymbolNames the set of unique names used by all
2176 // externally visible symbols in the index.
2177 for (auto &I : ThinLTO.CombinedIndex) {
2178 ValueInfo VI = ThinLTO.CombinedIndex.getValueInfo(I);
2179 for (const auto &Summary : VI.getSummaryList()) {
2180 const GlobalValueSummary *Base = Summary->getBaseObject();
2181 if (GlobalValue::isLocalLinkage(Base->linkage()))
2182 continue;
2183
2184 ExternallyVisibleSymbolNamesPtr->insert(VI.name());
2185 break;
2186 }
2187 }
2188 }
2189
2190 thinLTOInternalizeAndPromoteInIndex(ThinLTO.CombinedIndex, isExported,
2191 isPrevailing,
2192 ExternallyVisibleSymbolNamesPtr);
2193
2194 auto recordNewLinkage = [&](StringRef ModuleIdentifier,
2196 GlobalValue::LinkageTypes NewLinkage) {
2197 ResolvedODR[ModuleIdentifier][GUID] = NewLinkage;
2198 };
2199 thinLTOResolvePrevailingInIndex(Conf, ThinLTO.CombinedIndex, isPrevailing,
2200 recordNewLinkage, GUIDPreservedSymbols);
2201
2202 thinLTOPropagateFunctionAttrs(ThinLTO.CombinedIndex, isPrevailing);
2203
2204 generateParamAccessSummary(ThinLTO.CombinedIndex);
2205
2208
2209 TimeTraceScopeExit.release();
2210
2211 auto &ModuleMap =
2212 ThinLTO.ModulesToCompile ? *ThinLTO.ModulesToCompile : ThinLTO.ModuleMap;
2213
2214 auto RunBackends = [&](ThinBackendProc *BackendProcess) -> Error {
2215 auto ProcessOneModule = [&](int I) -> Error {
2216 auto &Mod = *(ModuleMap.begin() + I);
2217 // Tasks 0 through ParallelCodeGenParallelismLevel-1 are reserved for
2218 // combined module and parallel code generation partitions.
2219 return BackendProcess->start(
2220 RegularLTO.ParallelCodeGenParallelismLevel + I, Mod.second,
2221 ImportLists[Mod.first], ExportLists[Mod.first],
2222 ResolvedODR[Mod.first], ThinLTO.ModuleMap);
2223 };
2224
2225 BackendProcess->setup(ModuleMap.size(),
2226 RegularLTO.ParallelCodeGenParallelismLevel,
2227 RegularLTO.CombinedModule->getTargetTriple());
2228
2229 if (BackendProcess->getThreadCount() == 1 ||
2230 BackendProcess->isSensitiveToInputOrder()) {
2231 // Process the modules in the order they were provided on the
2232 // command-line. It is important for this codepath to be used for
2233 // WriteIndexesThinBackend, to ensure the emitted LinkedObjectsFile lists
2234 // ThinLTO objects in the same order as the inputs, which otherwise would
2235 // affect the final link order.
2236 for (int I = 0, E = ModuleMap.size(); I != E; ++I)
2237 if (Error E = ProcessOneModule(I))
2238 return E;
2239 } else {
2240 // When executing in parallel, process largest bitsize modules first to
2241 // improve parallelism, and avoid starving the thread pool near the end.
2242 // This saves about 15 sec on a 36-core machine while link `clang.exe`
2243 // (out of 100 sec).
2244 std::vector<BitcodeModule *> ModulesVec;
2245 ModulesVec.reserve(ModuleMap.size());
2246 for (auto &Mod : ModuleMap)
2247 ModulesVec.push_back(&Mod.second);
2248 for (int I : generateModulesOrdering(ModulesVec))
2249 if (Error E = ProcessOneModule(I))
2250 return E;
2251 }
2252 return BackendProcess->wait();
2253 };
2254
2256 std::unique_ptr<ThinBackendProc> BackendProc =
2257 ThinLTO.Backend(Conf, ThinLTO.CombinedIndex, ModuleToDefinedGVSummaries,
2258 AddStream, Cache, BitcodeLibFuncs);
2259 return RunBackends(BackendProc.get());
2260 }
2261
2262 // Perform two rounds of code generation for ThinLTO:
2263 // 1. First round: Perform optimization and code generation, outputting to
2264 // temporary scratch objects.
2265 // 2. Merge code generation data extracted from the temporary scratch objects.
2266 // 3. Second round: Execute code generation again using the merged data.
2267 LLVM_DEBUG(dbgs() << "[TwoRounds] Initializing ThinLTO two-codegen rounds\n");
2268
2269 unsigned MaxTasks = getMaxTasks();
2270 auto Parallelism = ThinLTO.Backend.getParallelism();
2271 // Set up two additional streams and caches for storing temporary scratch
2272 // objects and optimized IRs, using the same cache directory as the original.
2273 cgdata::StreamCacheData CG(MaxTasks, Cache, "CG"), IR(MaxTasks, Cache, "IR");
2274
2275 // First round: Execute optimization and code generation, outputting to
2276 // temporary scratch objects. Serialize the optimized IRs before initiating
2277 // code generation.
2278 LLVM_DEBUG(dbgs() << "[TwoRounds] Running the first round of codegen\n");
2279 auto FirstRoundLTO = std::make_unique<FirstRoundThinBackend>(
2280 Conf, ThinLTO.CombinedIndex, Parallelism, ModuleToDefinedGVSummaries,
2281 CG.AddStream, CG.Cache, BitcodeLibFuncs, IR.AddStream, IR.Cache);
2282 if (Error E = RunBackends(FirstRoundLTO.get()))
2283 return E;
2284
2285 LLVM_DEBUG(dbgs() << "[TwoRounds] Merging codegen data\n");
2286 auto CombinedHashOrErr = cgdata::mergeCodeGenData(*CG.getResult());
2287 if (Error E = CombinedHashOrErr.takeError())
2288 return E;
2289 auto CombinedHash = *CombinedHashOrErr;
2290 LLVM_DEBUG(dbgs() << "[TwoRounds] CGData hash: " << CombinedHash << "\n");
2291
2292 // Second round: Read the optimized IRs and execute code generation using the
2293 // merged data.
2294 LLVM_DEBUG(dbgs() << "[TwoRounds] Running the second round of codegen\n");
2295 auto SecondRoundLTO = std::make_unique<SecondRoundThinBackend>(
2296 Conf, ThinLTO.CombinedIndex, Parallelism, ModuleToDefinedGVSummaries,
2297 AddStream, Cache, BitcodeLibFuncs, IR.getResult(), CombinedHash);
2298 return RunBackends(SecondRoundLTO.get());
2299}
2300
2304 std::optional<uint64_t> RemarksHotnessThreshold, int Count) {
2305 std::string Filename = std::string(RemarksFilename);
2306 // For ThinLTO, file.opt.<format> becomes
2307 // file.opt.<format>.thin.<num>.<format>.
2308 if (!Filename.empty() && Count != -1)
2309 Filename =
2310 (Twine(Filename) + ".thin." + llvm::utostr(Count) + "." + RemarksFormat)
2311 .str();
2312
2313 auto ResultOrErr = llvm::setupLLVMOptimizationRemarks(
2316 if (Error E = ResultOrErr.takeError())
2317 return std::move(E);
2318
2319 if (*ResultOrErr)
2320 (*ResultOrErr)->keep();
2321
2322 return ResultOrErr;
2323}
2324
2327 // Setup output file to emit statistics.
2328 if (StatsFilename.empty())
2329 return nullptr;
2330
2332 std::error_code EC;
2333 auto StatsFile =
2334 std::make_unique<ToolOutputFile>(StatsFilename, EC, sys::fs::OF_None);
2335 if (EC)
2336 return errorCodeToError(EC);
2337
2338 StatsFile->keep();
2339 return std::move(StatsFile);
2340}
2341
2342// Compute the ordering we will process the inputs: the rough heuristic here
2343// is to sort them per size so that the largest module get schedule as soon as
2344// possible. This is purely a compile-time optimization.
2346 auto Seq = llvm::seq<int>(0, R.size());
2347 std::vector<int> ModulesOrdering(Seq.begin(), Seq.end());
2348 llvm::sort(ModulesOrdering, [&](int LeftIndex, int RightIndex) {
2349 auto LSize = R[LeftIndex]->getBuffer().size();
2350 auto RSize = R[RightIndex]->getBuffer().size();
2351 return LSize > RSize;
2352 });
2353 return ModulesOrdering;
2354}
2355
2356namespace {
2357/// This out-of-process backend does not perform code generation when invoked
2358/// for each task. Instead, it generates the necessary information (e.g., the
2359/// summary index shard, import list, etc.) to enable code generation to be
2360/// performed externally, similar to WriteIndexesThinBackend. The backend's
2361/// `wait` function then invokes an external distributor process to carry out
2362/// the backend compilations.
2363class OutOfProcessThinBackend : public CGThinBackend {
2364 using SString = SmallString<128>;
2365
2367 StringSaver Saver{Alloc};
2368
2369 SString LinkerOutputFile;
2370
2371 SString DistributorPath;
2372 ArrayRef<StringRef> DistributorArgs;
2373
2374 SString RemoteCompiler;
2375 ArrayRef<StringRef> RemoteCompilerPrependArgs;
2376 ArrayRef<StringRef> RemoteCompilerArgs;
2377
2378 bool SaveTemps;
2379
2380 SmallVector<StringRef, 0> CodegenOptions;
2381 DenseSet<StringRef> CommonInputs;
2382 // Number of the object files that have been already cached.
2383 std::atomic<size_t> CachedJobs{0};
2384 // Information specific to individual backend compilation job.
2385 struct Job {
2386 unsigned Task;
2387 StringRef ModuleID;
2388 StringRef NativeObjectPath;
2389 StringRef SummaryIndexPath;
2390 ImportsFilesContainer ImportsFiles;
2391 std::string CacheKey;
2392 AddStreamFn CacheAddStream;
2393 bool Cached = false;
2394 };
2395 // The set of backend compilations jobs.
2396 SmallVector<Job> Jobs;
2397
2398 // A unique string to identify the current link.
2399 SmallString<8> UID;
2400
2401 // The offset to the first ThinLTO task.
2402 unsigned ThinLTOTaskOffset;
2403
2404 // The target triple to supply for backend compilations.
2405 llvm::Triple Triple;
2406
2407 // Cache
2408 FileCache Cache;
2409
2410 // Callback to add a pre-existing native object buffer to the link.
2411 AddBufferFn AddBuffer;
2412
2413public:
2414 OutOfProcessThinBackend(
2415 const Config &Conf, ModuleSummaryIndex &CombinedIndex,
2416 ThreadPoolStrategy ThinLTOParallelism,
2417 const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
2418 FileCache CacheFn, lto::IndexWriteCallback OnWrite,
2419 bool ShouldEmitIndexFiles, bool ShouldEmitImportsFiles,
2420 StringRef LinkerOutputFile, StringRef Distributor,
2421 ArrayRef<StringRef> DistributorArgs, StringRef RemoteCompiler,
2422 ArrayRef<StringRef> RemoteCompilerPrependArgs,
2423 ArrayRef<StringRef> RemoteCompilerArgs, bool SaveTemps,
2424 AddBufferFn AddBuffer)
2425 : CGThinBackend(Conf, CombinedIndex, ModuleToDefinedGVSummaries, OnWrite,
2426 ShouldEmitIndexFiles, ShouldEmitImportsFiles,
2427 ThinLTOParallelism),
2428 LinkerOutputFile(LinkerOutputFile), DistributorPath(Distributor),
2429 DistributorArgs(DistributorArgs), RemoteCompiler(RemoteCompiler),
2430 RemoteCompilerPrependArgs(RemoteCompilerPrependArgs),
2431 RemoteCompilerArgs(RemoteCompilerArgs), SaveTemps(SaveTemps),
2432 Cache(std::move(CacheFn)), AddBuffer(std::move(AddBuffer)) {}
2433
2434 void setup(unsigned ThinLTONumTasks, unsigned ThinLTOTaskOffset,
2435 llvm::Triple Triple) override {
2437 Jobs.resize((size_t)ThinLTONumTasks);
2438 this->ThinLTOTaskOffset = ThinLTOTaskOffset;
2439 this->Triple = std::move(Triple);
2440 this->Conf.Dtlto = 1;
2441 }
2442
2443 virtual Error runThinLTOBackendThread(
2444 Job &J, const FunctionImporter::ImportMapTy &ImportList,
2445 const FunctionImporter::ExportSetTy &ExportList,
2446 const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>
2447 &ResolvedODR) {
2448 {
2449 TimeTraceScope TimeScope("Emit individual index for DTLTO",
2450 J.SummaryIndexPath);
2451 if (auto E = emitFiles(ImportList, J.ModuleID, J.ModuleID.str(),
2452 J.SummaryIndexPath, J.ImportsFiles))
2453 return E;
2454 }
2455
2456 if (!Cache.isValid() || !CombinedIndex.modulePaths().count(J.ModuleID) ||
2457 all_of(CombinedIndex.getModuleHash(J.ModuleID),
2458 [](uint32_t V) { return V == 0; }))
2459 // Cache disabled or no entry for this module in the combined index or
2460 // no module hash.
2461 return Error::success();
2462
2463 TimeTraceScope TimeScope("Check cache for DTLTO", J.SummaryIndexPath);
2464 const GVSummaryMapTy &DefinedGlobals =
2465 ModuleToDefinedGVSummaries.find(J.ModuleID)->second;
2466
2467 // The module may be cached, this helps handling it.
2468 J.CacheKey = computeLTOCacheKey(Conf, CombinedIndex, J.ModuleID, ImportList,
2469 ExportList, ResolvedODR, DefinedGlobals,
2470 CfiFunctionDefs, CfiFunctionDecls);
2471
2472 // The module may be cached, this helps handling it.
2473 auto CacheAddStreamExp = Cache(J.Task, J.CacheKey, J.ModuleID);
2474 if (Error Err = CacheAddStreamExp.takeError())
2475 return Err;
2476 AddStreamFn &CacheAddStream = *CacheAddStreamExp;
2477 // If CacheAddStream is null, we have a cache hit and at this point
2478 // object file is already passed back to the linker.
2479 if (!CacheAddStream) {
2480 J.Cached = true; // Cache hit, mark the job as cached.
2481 CachedJobs.fetch_add(1);
2482 } else {
2483 // If CacheAddStream is not null, we have a cache miss and we need to
2484 // run the backend for codegen. Save cache 'add stream'
2485 // function for a later use.
2486 J.CacheAddStream = std::move(CacheAddStream);
2487 }
2488 return Error::success();
2489 }
2490
2491 Error start(
2492 unsigned Task, BitcodeModule BM,
2493 const FunctionImporter::ImportMapTy &ImportList,
2494 const FunctionImporter::ExportSetTy &ExportList,
2495 const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
2496 MapVector<StringRef, BitcodeModule> &ModuleMap) override {
2497
2498 StringRef ModulePath = BM.getModuleIdentifier();
2499
2500 SString ObjFilePath = sys::path::parent_path(LinkerOutputFile);
2501 sys::path::append(ObjFilePath, sys::path::stem(ModulePath) + "." +
2502 itostr(Task) + "." + UID + ".native.o");
2503
2504 Job &J = Jobs[Task - ThinLTOTaskOffset];
2505 J = {Task,
2506 ModulePath,
2507 Saver.save(ObjFilePath.str()),
2508 Saver.save(ObjFilePath.str() + ".thinlto.bc"),
2509 {}, // Filled in by emitFiles below.
2510 "", /*CacheKey=*/
2511 nullptr,
2512 false};
2513
2514 // Cleanup per-job temporary files on abnormal process exit.
2515 if (!SaveTemps) {
2516 llvm::sys::RemoveFileOnSignal(J.NativeObjectPath);
2517 if (!ShouldEmitIndexFiles)
2518 llvm::sys::RemoveFileOnSignal(J.SummaryIndexPath);
2519 }
2520
2521 assert(ModuleToDefinedGVSummaries.count(ModulePath));
2522
2523 // The BackendThreadPool is only used here to write the sharded index files
2524 // (similar to WriteIndexesThinBackend).
2525 BackendThreadPool.async(
2526 [=](Job &J, const FunctionImporter::ImportMapTy &ImportList,
2527 const FunctionImporter::ExportSetTy &ExportList,
2528 const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>
2529 &ResolvedODR) {
2530 if (LLVM_ENABLE_THREADS && Conf.TimeTraceEnabled)
2533 "Emit individual index and check cache for DTLTO");
2534 Error E =
2535 runThinLTOBackendThread(J, ImportList, ExportList, ResolvedODR);
2536 if (E) {
2537 std::unique_lock<std::mutex> L(ErrMu);
2538 if (Err)
2539 Err = joinErrors(std::move(*Err), std::move(E));
2540 else
2541 Err = std::move(E);
2542 }
2543 if (LLVM_ENABLE_THREADS && Conf.TimeTraceEnabled)
2545 },
2546 std::ref(J), std::ref(ImportList), std::ref(ExportList),
2547 std::ref(ResolvedODR));
2548
2549 return Error::success();
2550 }
2551
2552 // Derive a set of Clang options that will be shared/common for all DTLTO
2553 // backend compilations. We are intentionally minimal here as these options
2554 // must remain synchronized with the behavior of Clang. DTLTO does not support
2555 // all the features available with in-process LTO. More features are expected
2556 // to be added over time. Users can specify Clang options directly if a
2557 // feature is not supported. Note that explicitly specified options that imply
2558 // additional input or output file dependencies must be communicated to the
2559 // distribution system, potentially by setting extra options on the
2560 // distributor program.
2561 void buildCommonRemoteCompilerOptions() {
2562 const lto::Config &C = Conf;
2563 auto &Ops = CodegenOptions;
2564
2565 Ops.push_back(Saver.save("-O" + Twine(C.OptLevel)));
2566
2567 if (C.Options.EmitAddrsig)
2568 Ops.push_back("-faddrsig");
2569 if (C.Options.FunctionSections)
2570 Ops.push_back("-ffunction-sections");
2571 if (C.Options.DataSections)
2572 Ops.push_back("-fdata-sections");
2573
2574 if (C.RelocModel == Reloc::PIC_)
2575 // Clang doesn't have -fpic for all triples.
2576 if (!Triple.isOSBinFormatCOFF())
2577 Ops.push_back("-fpic");
2578
2579 // Turn on/off warnings about profile cfg mismatch (default on)
2580 // --lto-pgo-warn-mismatch.
2581 if (!C.PGOWarnMismatch) {
2582 Ops.push_back("-mllvm");
2583 Ops.push_back("-no-pgo-warn-mismatch");
2584 }
2585
2586 // Enable sample-based profile guided optimizations.
2587 // Sample profile file path --lto-sample-profile=<value>.
2588 if (!C.SampleProfile.empty()) {
2589 Ops.push_back(
2590 Saver.save("-fprofile-sample-use=" + Twine(C.SampleProfile)));
2591 CommonInputs.insert(C.SampleProfile);
2592 }
2593
2594 // We don't know which of options will be used by Clang.
2595 Ops.push_back("-Wno-unused-command-line-argument");
2596
2597 // Forward any supplied options.
2598 if (!RemoteCompilerArgs.empty())
2599 for (auto &a : RemoteCompilerArgs)
2600 Ops.push_back(a);
2601 }
2602
2603 // Generates a JSON file describing the backend compilations, for the
2604 // distributor.
2605 bool emitDistributorJson(StringRef DistributorJson) {
2606 using json::Array;
2607 std::error_code EC;
2608 raw_fd_ostream OS(DistributorJson, EC);
2609 if (EC)
2610 return false;
2611
2612 json::OStream JOS(OS);
2613 JOS.object([&]() {
2614 // Information common to all jobs.
2615 JOS.attributeObject("common", [&]() {
2616 JOS.attribute("linker_output", LinkerOutputFile);
2617
2618 JOS.attributeArray("args", [&]() {
2619 JOS.value(RemoteCompiler);
2620
2621 // Forward any supplied prepend options.
2622 if (!RemoteCompilerPrependArgs.empty())
2623 for (auto &A : RemoteCompilerPrependArgs)
2624 JOS.value(A);
2625
2626 JOS.value("-c");
2627
2628 JOS.value(Saver.save("--target=" + Triple.str()));
2629
2630 for (const auto &A : CodegenOptions)
2631 JOS.value(A);
2632 });
2633
2634 JOS.attribute("inputs", Array(CommonInputs));
2635 });
2636
2637 // Per-compilation-job information.
2638 JOS.attributeArray("jobs", [&]() {
2639 for (const auto &J : Jobs) {
2640 assert(J.Task != 0);
2641 if (J.Cached) {
2642 assert(!Cache.getCacheDirectoryPath().empty());
2643 continue;
2644 }
2645
2647 SmallVector<StringRef, 1> Outputs;
2648
2649 JOS.object([&]() {
2650 JOS.attributeArray("args", [&]() {
2651 JOS.value(J.ModuleID);
2652 Inputs.push_back(J.ModuleID);
2653
2654 JOS.value(
2655 Saver.save("-fthinlto-index=" + Twine(J.SummaryIndexPath)));
2656 Inputs.push_back(J.SummaryIndexPath);
2657
2658 JOS.value("-o");
2659 JOS.value(J.NativeObjectPath);
2660 Outputs.push_back(J.NativeObjectPath);
2661 });
2662
2663 // Add the bitcode files from which imports will be made. These do
2664 // not explicitly appear on the backend compilation command lines
2665 // but are recorded in the summary index shards.
2666 llvm::append_range(Inputs, J.ImportsFiles);
2667 JOS.attribute("inputs", Array(Inputs));
2668
2669 JOS.attribute("outputs", Array(Outputs));
2670 });
2671 }
2672 });
2673 });
2674
2675 return true;
2676 }
2677
2678 void removeFile(StringRef FileName) {
2679 std::error_code EC = sys::fs::remove(FileName, true);
2680 if (EC && EC != std::make_error_code(std::errc::no_such_file_or_directory))
2681 errs() << "warning: could not remove the file '" << FileName
2682 << "': " << EC.message() << "\n";
2683 }
2684
2685 Error wait() override {
2686 // Wait for the information on the required backend compilations to be
2687 // gathered.
2688 BackendThreadPool.wait();
2689 if (Err)
2690 return std::move(*Err);
2691
2692 llvm::scope_exit CleanPerJobFiles([&] {
2693 llvm::TimeTraceScope TimeScope("Remove DTLTO temporary files");
2694 if (!SaveTemps)
2695 for (auto &Job : Jobs) {
2696 removeFile(Job.NativeObjectPath);
2697 if (!ShouldEmitIndexFiles)
2698 removeFile(Job.SummaryIndexPath);
2699 }
2700 });
2701
2702 const StringRef BCError = "DTLTO backend compilation: ";
2703
2704 buildCommonRemoteCompilerOptions();
2705
2706 SString JsonFile = sys::path::parent_path(LinkerOutputFile);
2707 {
2708 llvm::TimeTraceScope TimeScope("Emit DTLTO JSON");
2709 sys::path::append(JsonFile, sys::path::stem(LinkerOutputFile) + "." +
2710 UID + ".dist-file.json");
2711 // Cleanup DTLTO JSON file on abnormal process exit.
2712 if (!SaveTemps)
2714 if (!emitDistributorJson(JsonFile))
2716 BCError + "failed to generate distributor JSON script: " + JsonFile,
2718 }
2719 llvm::scope_exit CleanJson([&] {
2720 if (!SaveTemps)
2721 removeFile(JsonFile);
2722 });
2723
2724 {
2725 llvm::TimeTraceScope TimeScope("Execute DTLTO distributor",
2726 DistributorPath);
2727 // Checks if we have any jobs that don't have corresponding cache entries.
2728 if (CachedJobs.load() < Jobs.size()) {
2729 SmallVector<StringRef, 3> Args = {DistributorPath};
2730 llvm::append_range(Args, DistributorArgs);
2731 Args.push_back(JsonFile);
2732 std::string ErrMsg;
2733 if (sys::ExecuteAndWait(Args[0], Args,
2734 /*Env=*/std::nullopt, /*Redirects=*/{},
2735 /*SecondsToWait=*/0, /*MemoryLimit=*/0,
2736 &ErrMsg)) {
2738 BCError + "distributor execution failed" +
2739 (!ErrMsg.empty() ? ": " + ErrMsg + Twine(".") : Twine(".")),
2741 }
2742 }
2743 }
2744
2745 {
2746 llvm::TimeTraceScope FilesScope("Add DTLTO files to the link");
2747 for (auto &Job : Jobs) {
2748 if (!Job.CacheKey.empty() && Job.Cached) {
2749 assert(Cache.isValid());
2750 continue;
2751 }
2752 // Load the native object from a file into a memory buffer
2753 // and store its contents in the output buffer.
2754 auto ObjFileMbOrErr =
2755 MemoryBuffer::getFile(Job.NativeObjectPath, /*IsText=*/false,
2756 /*RequiresNullTerminator=*/false);
2757 if (std::error_code EC = ObjFileMbOrErr.getError())
2759 BCError + "cannot open native object file: " +
2760 Job.NativeObjectPath + ": " + EC.message(),
2762
2763 if (Cache.isValid()) {
2764 // Cache hits are taken care of earlier. At this point, we could only
2765 // have cache misses.
2766 assert(Job.CacheAddStream);
2767 MemoryBufferRef ObjFileMbRef =
2768 ObjFileMbOrErr->get()->getMemBufferRef();
2769 // Obtain a file stream for a storing a cache entry.
2770 auto CachedFileStreamOrErr =
2771 Job.CacheAddStream(Job.Task, Job.ModuleID);
2772 if (!CachedFileStreamOrErr)
2773 return joinErrors(
2774 CachedFileStreamOrErr.takeError(),
2776 "Cannot get a cache file stream: %s",
2777 Job.NativeObjectPath.data()));
2778 // Store a file buffer into the cache stream.
2779 auto &CacheStream = *(CachedFileStreamOrErr->get());
2780 *(CacheStream.OS) << ObjFileMbRef.getBuffer();
2781 if (Error Err = CacheStream.commit())
2782 return Err;
2783 } else {
2784 AddBuffer(Job.Task, Job.ModuleID, std::move(*ObjFileMbOrErr));
2785 }
2786 }
2787 }
2788 return Error::success();
2789 }
2790};
2791} // end anonymous namespace
2792
2794 ThreadPoolStrategy Parallelism, lto::IndexWriteCallback OnWrite,
2795 bool ShouldEmitIndexFiles, bool ShouldEmitImportsFiles,
2796 StringRef LinkerOutputFile, StringRef Distributor,
2797 ArrayRef<StringRef> DistributorArgs, StringRef RemoteCompiler,
2798 ArrayRef<StringRef> RemoteCompilerPrependArgs,
2799 ArrayRef<StringRef> RemoteCompilerArgs, bool SaveTemps,
2800 AddBufferFn AddBuffer) {
2801 auto Func =
2802 [=](const Config &Conf, ModuleSummaryIndex &CombinedIndex,
2803 const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
2804 AddStreamFn, FileCache Cache, ArrayRef<StringRef> BitcodeLibFuncs) {
2805 return std::make_unique<OutOfProcessThinBackend>(
2806 Conf, CombinedIndex, Parallelism, ModuleToDefinedGVSummaries, Cache,
2807 OnWrite, ShouldEmitIndexFiles, ShouldEmitImportsFiles,
2808 LinkerOutputFile, Distributor, DistributorArgs, RemoteCompiler,
2809 RemoteCompilerPrependArgs, RemoteCompilerArgs, SaveTemps,
2810 AddBuffer);
2811 };
2812 return ThinBackend(Func, Parallelism);
2813}
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:798
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:934
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:80
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:337
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:210
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:170
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:569
LLVM_ABI void eraseFromParent()
eraseFromParent - This method unlinks 'this' from the containing module and deletes it.
Definition Globals.cpp:538
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
Implementation of the target library information.
Provides information about what library functions are available for the current target.
bool has(LibFunc F) const
Tests whether a library function is available.
bool getLibFunc(StringRef funcName, LibFunc &F) const
Searches for a particular function name.
StringRef getName(LibFunc F) const
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:1187
ArchType getArch() const
Get the parsed architecture type of this triple.
Definition Triple.h:427
bool isOSBinFormatCOFF() const
Tests whether the OS uses the COFF binary format.
Definition Triple.h:815
const std::string & str() const
Definition Triple.h:494
bool isOSDarwin() const
Is this a "Darwin" OS (macOS, iOS, tvOS, watchOS, DriverKit, XROS, or bridgeOS).
Definition Triple.h:646
bool isOSBinFormatELF() const
Tests whether the OS uses the ELF binary format.
Definition Triple.h:810
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:46
static LLVM_ABI Type * getVoidTy(LLVMContext &C)
Definition Type.cpp:286
static LLVM_ABI IntegerType * getInt8Ty(LLVMContext &C)
Definition Type.cpp:311
LLVM_ABI void setName(const Twine &Name)
Change the name of the value.
Definition Value.cpp:393
LLVM_ABI void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition Value.cpp:549
bool use_empty() const
Definition Value.h:346
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:318
LLVM_ABI void takeName(Value *V)
Transfer the name from V to this value.
Definition Value.cpp:399
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:673
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:187
LLVM_ABI StringRef getName() const
Returns the path to the InputFile.
Definition LTO.cpp:664
LLVM_ABI BitcodeModule & getSingleBitcodeModule()
Definition LTO.cpp:668
LLVM_ABI LTO(Config Conf, ThinBackend Backend={}, unsigned ParallelCodeGenParallelismLevel=1, LTOKind LTOMode=LTOK_Default)
Create an LTO object.
Definition LTO.cpp:688
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:822
virtual void cleanup()
Definition LTO.cpp:705
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:1493
virtual Expected< std::shared_ptr< lto::InputFile > > addInput(std::unique_ptr< lto::InputFile > InputPtr)
Definition LTO.h:687
LLVM_ABI void setBitcodeLibFuncs(ArrayRef< StringRef > BitcodeLibFuncs)
Set the list of functions implemented in bitcode that were not extracted from an archive.
Definition LTO.cpp:853
virtual Error serializeInputsForDistribution()
Definition LTO.h:487
LTOKind
Unified LTO modes.
Definition LTO.h:425
@ LTOK_UnifiedRegular
Regular LTO, with Unified LTO enabled.
Definition LTO.h:430
@ LTOK_Default
Any LTO mode without Unified LTO. The default mode.
Definition LTO.h:427
@ LTOK_UnifiedThin
ThinLTO, with Unified LTO enabled.
Definition LTO.h:433
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:1250
LLVM_ABI Error run(AddStreamFn AddStream, FileCache Cache={})
Runs the LTO pipeline.
Definition LTO.cpp:1301
static LLVM_ABI SmallVector< StringRef > getLibFuncSymbols(const Triple &TT, llvm::StringSaver &Saver)
Static method that returns a list of library function symbols that can be generated by LTO but might ...
Definition LTO.cpp:1506
This class defines the interface to the ThinLTO backend.
Definition LTO.h:249
const DenseMap< StringRef, GVSummaryMapTy > & ModuleToDefinedGVSummaries
Definition LTO.h:253
LLVM_ABI Error emitFiles(const FunctionImporter::ImportMapTy &ImportList, StringRef ModulePath, const std::string &NewModulePath) const
Definition LTO.cpp:1520
ModuleSummaryIndex & CombinedIndex
Definition LTO.h:252
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:1875
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:1910
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, AddBufferFn AddBuffer)
This ThinBackend generates the index shards and then runs the individual backend jobs via an external...
Definition LTO.cpp:2793
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, ArrayRef< StringRef > BitcodeLibFuncs, AddStreamFn IRAddStream=nullptr, const std::vector< uint8_t > &CmdArgs=std::vector< uint8_t >())
Runs a ThinLTO backend.
LLVM_ABI StringLiteral getThinLTODefaultCPU(const Triple &TheTriple)
Definition LTO.cpp:1892
LLVM_ABI Expected< std::unique_ptr< ToolOutputFile > > setupStatsFile(StringRef StatsFilename)
Setups the output file for saving statistics.
Definition LTO.cpp:2326
LLVM_ABI Error backend(const Config &C, AddStreamFn AddStream, unsigned ParallelCodeGenParallelismLevel, Module &M, ModuleSummaryIndex &CombinedIndex, ArrayRef< StringRef > BitcodeLibFuncs)
Runs a regular LTO backend.
std::function< void(const std::string &)> IndexWriteCallback
Definition LTO.h:244
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:1996
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:2301
LLVM_ABI std::vector< int > generateModulesOrdering(ArrayRef< BitcodeModule * > R)
Produces a container ordering for optimal multi-threaded processing.
Definition LTO.cpp:2345
llvm::SmallVector< std::string > ImportsFilesContainer
Definition LTO.h:246
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:977
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.
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:1422
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:1328
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...
std::function< void(unsigned Task, const Twine &ModuleName, std::unique_ptr< MemoryBuffer > MB)> AddBufferFn
This type defines the callback to add a pre-existing file (e.g.
Definition Caching.h:107
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
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:221
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:1106
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:43
std::optional< uint64_t > RemarksHotnessThreshold
The minimum hotness value a diagnostic needs in order to be included in optimization diagnostics.
Definition Config.h:177
std::optional< CodeModel::Model > CodeModel
Definition Config.h:63
std::string AAPipeline
Definition Config.h:122
bool CodeGenOnly
Disable entirely the optimizer, including importing for ThinLTO.
Definition Config.h:75
std::vector< std::string > MAttrs
Definition Config.h:52
std::vector< std::string > MllvmArgs
Definition Config.h:53
CodeGenOptLevel CGOptLevel
Definition Config.h:64
bool Dtlto
This flag is used as one of parameters to calculate cache entries and to ensure that in-process cache...
Definition Config.h:106
std::string DefaultTriple
Setting this field will replace unspecified target triples in input files with this triple.
Definition Config.h:130
std::string CPU
Definition Config.h:50
std::string DwoDir
The directory to store .dwo files.
Definition Config.h:142
std::string RemarksFilename
Optimization remarks file path.
Definition Config.h:156
std::string OverrideTriple
Setting this field will replace target triples in input files with this triple.
Definition Config.h:126
std::string ProfileRemapping
Name remapping file for profile data.
Definition Config.h:139
TargetOptions Options
Definition Config.h:51
bool TimeTraceEnabled
Time trace enabled.
Definition Config.h:192
std::string RemarksPasses
Optimization remarks pass filter.
Definition Config.h:159
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:117
unsigned TimeTraceGranularity
Time trace granularity.
Definition Config.h:195
unsigned OptLevel
Definition Config.h:66
bool RemarksWithHotness
Whether to emit optimization remarks with hotness informations.
Definition Config.h:162
std::optional< Reloc::Model > RelocModel
Definition Config.h:62
CodeGenFileType CGFileType
Definition Config.h:65
bool Freestanding
Flag to indicate that the optimizer should not assume builtins are present on the target.
Definition Config.h:72
std::string SampleProfile
Sample PGO profile path.
Definition Config.h:136
std::string RemarksFormat
The format used for serializing remarks (default: YAML).
Definition Config.h:180
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 TargetLibraryInfo &TLI, const RTLIB::RuntimeLibcallsInfo &Libcalls) const
Definition LTO.cpp:655
The resolution for a symbol.
Definition LTO.h:694
unsigned FinalDefinitionInLinkageUnit
The definition of this symbol is unpreemptable at runtime and is known to be in this linkage unit.
Definition LTO.h:704
unsigned ExportDynamic
The symbol was exported dynamically, and therefore could be referenced by a shared library not visibl...
Definition LTO.h:711
unsigned Prevailing
The linker has chosen this definition of the symbol.
Definition LTO.h:700
unsigned LinkerRedefined
Linker redefined version of the symbol which appeared in -wrap or -defsym linker option.
Definition LTO.h:715
unsigned VisibleToRegularObj
The definition of this symbol is visible outside of the LTO unit.
Definition LTO.h:707
This type defines the behavior following the thin-link phase during ThinLTO.
Definition LTO.h:320