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