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