LLVM 24.0.0git
LTO.cpp
Go to the documentation of this file.
1//===-LTO.cpp - LLVM Link Time Optimizer ----------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements functions and classes used to support LTO.
10//
11//===----------------------------------------------------------------------===//
12
13#include "llvm/LTO/LTO.h"
14#include "llvm/ADT/ArrayRef.h"
15#include "llvm/ADT/ScopeExit.h"
16#include "llvm/ADT/SmallSet.h"
18#include "llvm/ADT/Statistic.h"
27#include "llvm/Config/llvm-config.h"
28#include "llvm/IR/AutoUpgrade.h"
30#include "llvm/IR/GlobalValue.h"
31#include "llvm/IR/Intrinsics.h"
34#include "llvm/IR/Mangler.h"
35#include "llvm/IR/Metadata.h"
37#include "llvm/LTO/LTOBackend.h"
38#include "llvm/Linker/IRMover.h"
44#include "llvm/Support/Error.h"
46#include "llvm/Support/JSON.h"
48#include "llvm/Support/Path.h"
50#include "llvm/Support/SHA1.h"
57#include "llvm/Support/VCSRevision.h"
60#include "llvm/Transforms/IPO.h"
65
66#include <optional>
67#include <set>
68
69using namespace llvm;
70using namespace lto;
71using namespace object;
72
73#define DEBUG_TYPE "lto"
74
75Error LTO::setupOptimizationRemarks() {
76 // Setup the remark streamer according to the provided configuration.
77 auto DiagFileOrErr = lto::setupLLVMOptimizationRemarks(
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(
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 // Sort the defined globals by GUID to be independent of the insertion order,
308 // which may depend on the order that modules are added.
310 SortedDefinedGlobals(DefinedGlobals.begin(), DefinedGlobals.end());
311 llvm::sort(SortedDefinedGlobals, llvm::less_first());
312 for (auto &GS : SortedDefinedGlobals) {
313 // Include the hash for the linkage type to reflect internalization and weak
314 // resolution, and collect any used type identifier resolutions.
315 GlobalValue::LinkageTypes Linkage = GS.second->linkage();
316 Hasher.update(
317 ArrayRef<uint8_t>((const uint8_t *)&Linkage, sizeof(Linkage)));
318 AddUsedCfiGlobal(GS.first);
319 AddUsedThings(GS.second);
320 }
321
322 // Imported functions may introduce new uses of type identifier resolutions,
323 // so we need to collect their used resolutions as well.
324 for (const auto &[FromModule, GUID, Type] : SortedImportList) {
325 GlobalValueSummary *S = Index.findSummaryInModule(GUID, FromModule);
326 AddUsedThings(S);
327 // If this is an alias, we also care about any types/etc. that the aliasee
328 // may reference.
329 if (auto *AS = dyn_cast_or_null<AliasSummary>(S))
330 AddUsedThings(AS->getBaseObject());
331 }
332
333 auto AddTypeIdSummary = [&](StringRef TId, const TypeIdSummary &S) {
334 AddString(TId);
335
336 AddUnsigned(S.TTRes.TheKind);
337 AddUnsigned(S.TTRes.SizeM1BitWidth);
338
339 AddUint64(S.TTRes.AlignLog2);
340 AddUint64(S.TTRes.SizeM1);
341 AddUint64(S.TTRes.BitMask);
342 AddUint64(S.TTRes.InlineBits);
343
344 AddUint64(S.WPDRes.size());
345 for (auto &WPD : S.WPDRes) {
346 AddUnsigned(WPD.first);
347 AddUnsigned(WPD.second.TheKind);
348 AddString(WPD.second.SingleImplName);
349
350 AddUint64(WPD.second.ResByArg.size());
351 for (auto &ByArg : WPD.second.ResByArg) {
352 AddUint64(ByArg.first.size());
353 for (uint64_t Arg : ByArg.first)
354 AddUint64(Arg);
355 AddUnsigned(ByArg.second.TheKind);
356 AddUint64(ByArg.second.Info);
357 AddUnsigned(ByArg.second.Byte);
358 AddUnsigned(ByArg.second.Bit);
359 }
360 }
361 };
362
363 // Include the hash for all type identifiers used by this module.
364 for (GlobalValue::GUID TId : UsedTypeIds) {
365 auto TidIter = Index.typeIds().equal_range(TId);
366 for (const auto &I : make_range(TidIter))
367 AddTypeIdSummary(I.second.first, I.second.second);
368 }
369
370 AddUnsigned(UsedCfiDefs.size());
371 for (auto &V : UsedCfiDefs)
372 AddUint64(V);
373
374 AddUnsigned(UsedCfiDecls.size());
375 for (auto &V : UsedCfiDecls)
376 AddUint64(V);
377
378 if (!Conf.SampleProfile.empty()) {
379 auto FileOrErr = MemoryBuffer::getFile(Conf.SampleProfile);
380 if (FileOrErr) {
381 Hasher.update(FileOrErr.get()->getBuffer());
382
383 if (!Conf.ProfileRemapping.empty()) {
384 FileOrErr = MemoryBuffer::getFile(Conf.ProfileRemapping);
385 if (FileOrErr)
386 Hasher.update(FileOrErr.get()->getBuffer());
387 }
388 }
389 }
390
391 return toHex(Hasher.result());
392}
393
394std::string llvm::recomputeLTOCacheKey(const std::string &Key,
395 StringRef ExtraID) {
396 SHA1 Hasher;
397
398 auto AddString = [&](StringRef Str) {
399 Hasher.update(Str);
400 Hasher.update(ArrayRef<uint8_t>{0});
401 };
402 AddString(Key);
403 AddString(ExtraID);
404
405 return toHex(Hasher.result());
406}
407
409 const Config &C, ValueInfo VI,
410 DenseSet<GlobalValueSummary *> &GlobalInvolvedWithAlias,
412 isPrevailing,
414 recordNewLinkage,
415 const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols) {
417 C.VisibilityScheme == Config::ELF ? VI.getELFVisibility()
419 for (auto &S : VI.getSummaryList()) {
420 GlobalValue::LinkageTypes OriginalLinkage = S->linkage();
421 // Ignore local and appending linkage values since the linker
422 // doesn't resolve them.
423 if (GlobalValue::isLocalLinkage(OriginalLinkage) ||
425 continue;
426 // We need to emit only one of these. The prevailing module will keep it,
427 // but turned into a weak, while the others will drop it when possible.
428 // This is both a compile-time optimization and a correctness
429 // transformation. This is necessary for correctness when we have exported
430 // a reference - we need to convert the linkonce to weak to
431 // ensure a copy is kept to satisfy the exported reference.
432 // FIXME: We may want to split the compile time and correctness
433 // aspects into separate routines.
434 if (isPrevailing(VI.getGUID(), S.get())) {
435 assert(!S->wasPromoted() &&
436 "promoted symbols used to be internal linkage and shouldn't have "
437 "a prevailing variant");
438 if (GlobalValue::isLinkOnceLinkage(OriginalLinkage)) {
439 S->setLinkage(GlobalValue::getWeakLinkage(
440 GlobalValue::isLinkOnceODRLinkage(OriginalLinkage)));
441 // The kept copy is eligible for auto-hiding (hidden visibility) if all
442 // copies were (i.e. they were all linkonce_odr global unnamed addr).
443 // If any copy is not (e.g. it was originally weak_odr), then the symbol
444 // must remain externally available (e.g. a weak_odr from an explicitly
445 // instantiated template). Additionally, if it is in the
446 // GUIDPreservedSymbols set, that means that it is visibile outside
447 // the summary (e.g. in a native object or a bitcode file without
448 // summary), and in that case we cannot hide it as it isn't possible to
449 // check all copies.
450 S->setCanAutoHide(VI.canAutoHide() &&
451 !GUIDPreservedSymbols.count(VI.getGUID()));
452 }
453 if (C.VisibilityScheme == Config::FromPrevailing)
454 Visibility = S->getVisibility();
455 }
456 // Alias and aliasee can't be turned into available_externally.
457 // When force-import-all is used, it indicates that object linking is not
458 // supported by the target. In this case, we can't change the linkage as
459 // well in case the global is converted to declaration.
460 // Also, if the symbol was promoted, it wouldn't have a prevailing variant,
461 // but also its linkage is set correctly (to External) already.
462 else if (!isa<AliasSummary>(S.get()) &&
463 !GlobalInvolvedWithAlias.count(S.get()) && !ForceImportAll &&
464 !S->wasPromoted())
466
467 // For ELF, set visibility to the computed visibility from summaries. We
468 // don't track visibility from declarations so this may be more relaxed than
469 // the most constraining one.
470 if (C.VisibilityScheme == Config::ELF)
471 S->setVisibility(Visibility);
472
473 if (S->linkage() != OriginalLinkage)
474 recordNewLinkage(S->modulePath(), VI.getGUID(), S->linkage());
475 }
476
477 if (C.VisibilityScheme == Config::FromPrevailing) {
478 for (auto &S : VI.getSummaryList()) {
479 GlobalValue::LinkageTypes OriginalLinkage = S->linkage();
480 if (GlobalValue::isLocalLinkage(OriginalLinkage) ||
482 continue;
483 S->setVisibility(Visibility);
484 }
485 }
486}
487
488/// Resolve linkage for prevailing symbols in the \p Index.
489//
490// We'd like to drop these functions if they are no longer referenced in the
491// current module. However there is a chance that another module is still
492// referencing them because of the import. We make sure we always emit at least
493// one copy.
495 const Config &C, ModuleSummaryIndex &Index,
497 isPrevailing,
499 recordNewLinkage,
500 const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols) {
501 // We won't optimize the globals that are referenced by an alias for now
502 // Ideally we should turn the alias into a global and duplicate the definition
503 // when needed.
504 DenseSet<GlobalValueSummary *> GlobalInvolvedWithAlias;
505 for (auto &I : Index)
506 for (auto &S : I.second.getSummaryList())
507 if (auto AS = dyn_cast<AliasSummary>(S.get()))
508 GlobalInvolvedWithAlias.insert(&AS->getAliasee());
509
510 for (auto &I : Index)
511 thinLTOResolvePrevailingGUID(C, Index.getValueInfo(I),
512 GlobalInvolvedWithAlias, isPrevailing,
513 recordNewLinkage, GUIDPreservedSymbols);
514}
515
517 ValueInfo VI, function_ref<bool(StringRef, ValueInfo)> isExported,
519 isPrevailing,
520 DenseSet<StringRef> *ExternallyVisibleSymbolNamesPtr) {
521 // Before performing index-based internalization and promotion for this GUID,
522 // the local flag should be consistent with the summary list linkage types.
523 VI.verifyLocal();
524
525 const bool SingleExternallyVisibleCopy =
526 VI.getSummaryList().size() == 1 &&
527 !GlobalValue::isLocalLinkage(VI.getSummaryList().front()->linkage());
528
529 bool NameRecorded = false;
530 for (auto &S : VI.getSummaryList()) {
531 // First see if we need to promote an internal value because it is not
532 // exported.
533 if (isExported(S->modulePath(), VI)) {
534 if (GlobalValue::isLocalLinkage(S->linkage())) {
535 // Only the first local GlobalValue in a list of summaries does not
536 // need renaming. In rare cases if there exist more than one summaries
537 // in the list, the rest of them must have renaming (through promotion)
538 // to avoid conflict.
539 if (ExternallyVisibleSymbolNamesPtr && !NameRecorded) {
540 NameRecorded = true;
541 if (ExternallyVisibleSymbolNamesPtr->insert(VI.name()).second)
542 S->setNoRenameOnPromotion(true);
543 }
544
545 S->promote();
546 }
547 continue;
548 }
549
550 // Otherwise, see if we can internalize.
552 continue;
553
554 // Non-exported values with external linkage can be internalized.
555 if (GlobalValue::isExternalLinkage(S->linkage())) {
556 S->setLinkage(GlobalValue::InternalLinkage);
557 continue;
558 }
559
560 // Non-exported function and variable definitions with a weak-for-linker
561 // linkage can be internalized in certain cases. The minimum legality
562 // requirements would be that they are not address taken to ensure that we
563 // don't break pointer equality checks, and that variables are either read-
564 // or write-only. For functions, this is the case if either all copies are
565 // [local_]unnamed_addr, or we can propagate reference edge attributes
566 // (which is how this is guaranteed for variables, when analyzing whether
567 // they are read or write-only).
568 //
569 // However, we only get to this code for weak-for-linkage values in one of
570 // two cases:
571 // 1) The prevailing copy is not in IR (it is in native code).
572 // 2) The prevailing copy in IR is not exported from its module.
573 // Additionally, at least for the new LTO API, case 2 will only happen if
574 // there is exactly one definition of the value (i.e. in exactly one
575 // module), as duplicate defs are result in the value being marked exported.
576 // Likely, users of the legacy LTO API are similar, however, currently there
577 // are llvm-lto based tests of the legacy LTO API that do not mark
578 // duplicate linkonce_odr copies as exported via the tool, so we need
579 // to handle that case below by checking the number of copies.
580 //
581 // Generally, we only want to internalize a weak-for-linker value in case
582 // 2, because in case 1 we cannot see how the value is used to know if it
583 // is read or write-only. We also don't want to bloat the binary with
584 // multiple internalized copies of non-prevailing linkonce/weak functions.
585 // Note if we don't internalize, we will convert non-prevailing copies to
586 // available_externally anyway, so that we drop them after inlining. The
587 // only reason to internalize such a function is if we indeed have a single
588 // copy, because internalizing it won't increase binary size, and enables
589 // use of inliner heuristics that are more aggressive in the face of a
590 // single call to a static (local). For variables, internalizing a read or
591 // write only variable can enable more aggressive optimization. However, we
592 // already perform this elsewhere in the ThinLTO backend handling for
593 // read or write-only variables (processGlobalForThinLTO).
594 //
595 // Therefore, only internalize linkonce/weak if there is a single copy, that
596 // is prevailing in this IR module. We can do so aggressively, without
597 // requiring the address to be insignificant, or that a variable be read or
598 // write-only.
599 if (!GlobalValue::isWeakForLinker(S->linkage()) ||
601 continue;
602
603 // We may have a single summary copy that is externally visible but not
604 // prevailing if the prevailing copy is in a native object.
605 if (SingleExternallyVisibleCopy && isPrevailing(VI.getGUID(), S.get()))
606 S->setLinkage(GlobalValue::InternalLinkage);
607 }
608}
609
610// Update the linkages in the given \p Index to mark exported values
611// as external and non-exported values as internal.
613 ModuleSummaryIndex &Index,
614 function_ref<bool(StringRef, ValueInfo)> isExported,
616 isPrevailing,
617 DenseSet<StringRef> *ExternallyVisibleSymbolNamesPtr) {
618 assert(!Index.withInternalizeAndPromote());
619
620 for (auto &I : Index)
621 thinLTOInternalizeAndPromoteGUID(Index.getValueInfo(I), isExported,
622 isPrevailing,
623 ExternallyVisibleSymbolNamesPtr);
624 Index.setWithInternalizeAndPromote();
625}
626
627// Requires a destructor for std::vector<InputModule>.
628InputFile::~InputFile() = default;
629
631 std::unique_ptr<InputFile> File(new InputFile);
632
633 Expected<IRSymtabFile> FOrErr = readIRSymtab(Object);
634 if (!FOrErr)
635 return FOrErr.takeError();
636
637 File->TargetTriple = FOrErr->TheReader.getTargetTriple();
638 File->SourceFileName = FOrErr->TheReader.getSourceFileName();
639 File->COFFLinkerOpts = FOrErr->TheReader.getCOFFLinkerOpts();
640 File->DependentLibraries = FOrErr->TheReader.getDependentLibraries();
641 File->ComdatTable = FOrErr->TheReader.getComdatTable();
642 File->MbRef =
643 Object; // Save a memory buffer reference to an input file object.
644
645 for (unsigned I = 0; I != FOrErr->Mods.size(); ++I) {
646 size_t Begin = File->Symbols.size();
647 for (const irsymtab::Reader::SymbolRef &Sym :
648 FOrErr->TheReader.module_symbols(I))
649 // Skip symbols that are irrelevant to LTO. Note that this condition needs
650 // to match the one in Skip() in LTO::addRegularLTO().
651 if (Sym.isGlobal() && !Sym.isFormatSpecific())
652 File->Symbols.push_back(Sym);
653 File->ModuleSymIndices.push_back({Begin, File->Symbols.size()});
654 }
655
656 File->Mods = FOrErr->Mods;
657 File->Strtab = std::move(FOrErr->Strtab);
658 return std::move(File);
659}
660
662 const TargetLibraryInfo &TLI,
663 const RTLIB::RuntimeLibcallsInfo &Libcalls) const {
664 LibFunc F;
665 if (TLI.getLibFunc(IRName, F) && TLI.has(F))
666 return true;
667 return Libcalls.getSupportedLibcallImpl(IRName) != RTLIB::Unsupported;
668}
669
671 return Mods[0].getModuleIdentifier();
672}
673
675 assert(Mods.size() == 1 && "Expect only one bitcode module");
676 return Mods[0];
677}
678
680
686
693
695 unsigned ParallelCodeGenParallelismLevel, LTOKind LTOMode)
696 : Conf(std::move(Conf)),
697 RegularLTO(ParallelCodeGenParallelismLevel, this->Conf),
698 ThinLTO(std::move(Backend)),
699 GlobalResolutions(
700 std::make_unique<DenseMap<StringRef, GlobalResolution>>()),
702 if (Conf.KeepSymbolNameCopies || LTOKeepSymbolCopies) {
703 Alloc = std::make_unique<BumpPtrAllocator>();
704 GlobalResolutionSymbolSaver = std::make_unique<llvm::StringSaver>(*Alloc);
705 }
706}
707
708// Requires a destructor for MapVector<BitcodeModule>.
709LTO::~LTO() = default;
710
712 DummyModule.reset();
713 LinkerRemarkFunction = nullptr;
714 consumeError(finalizeOptimizationRemarks(std::move(DiagnosticOutputFile)));
715}
716
717// Add the symbols in the given module to the GlobalResolutions map, and resolve
718// their partitions.
719void LTO::addModuleToGlobalRes(ArrayRef<InputFile::Symbol> Syms,
721 unsigned Partition, bool InSummary,
722 const Triple &TT) {
723 llvm::TimeTraceScope timeScope("LTO add module to global resolution");
724 auto *ResI = Res.begin();
725 auto *ResE = Res.end();
726 (void)ResE;
727 RTLIB::RuntimeLibcallsInfo Libcalls(TT);
728 TargetLibraryInfoImpl TLII(TT);
729 TargetLibraryInfo TLI(TLII);
730 for (const InputFile::Symbol &Sym : Syms) {
731 assert(ResI != ResE);
732 SymbolResolution Res = *ResI++;
733
734 StringRef SymbolName = Sym.getName();
735 // Keep copies of symbols if the client of LTO says so.
736 if (GlobalResolutionSymbolSaver && !GlobalResolutions->contains(SymbolName))
737 SymbolName = GlobalResolutionSymbolSaver->save(SymbolName);
738
739 auto &GlobalRes = (*GlobalResolutions)[SymbolName];
740 GlobalRes.UnnamedAddr &= Sym.isUnnamedAddr();
741 if (Res.Prevailing) {
742 assert(!GlobalRes.Prevailing &&
743 "Multiple prevailing defs are not allowed");
744 GlobalRes.Prevailing = true;
745 GlobalRes.IRName = std::string(Sym.getIRName());
746 } else if (!GlobalRes.Prevailing && GlobalRes.IRName.empty()) {
747 // Sometimes it can be two copies of symbol in a module and prevailing
748 // symbol can have no IR name. That might happen if symbol is defined in
749 // module level inline asm block. In case we have multiple modules with
750 // the same symbol we want to use IR name of the prevailing symbol.
751 // Otherwise, if we haven't seen a prevailing symbol, set the name so that
752 // we can later use it to check if there is any prevailing copy in IR.
753 GlobalRes.IRName = std::string(Sym.getIRName());
754 }
755
756 // In rare occasion, the symbol used to initialize GlobalRes has a different
757 // IRName from the inspected Symbol. This can happen on macOS + iOS, when a
758 // symbol is referenced through its mangled name, say @"\01_symbol" while
759 // the IRName is @symbol (the prefix underscore comes from MachO mangling).
760 // In that case, we have the same actual Symbol that can get two different
761 // GUID, leading to some invalid internalization. Workaround this by marking
762 // the GlobalRes external.
763
764 // FIXME: instead of this check, it would be desirable to compute GUIDs
765 // based on mangled name, but this requires an access to the Target Triple
766 // and would be relatively invasive on the codebase.
767 // FIXME: use the GUID member of GlobalRes.
768 if (GlobalRes.IRName != Sym.getIRName()) {
769 GlobalRes.Partition = GlobalResolution::External;
770 GlobalRes.VisibleOutsideSummary = true;
771 }
772
773 bool IsLibcall = Sym.isLibcall(TLI, Libcalls);
774
775 // Set the partition to external if we know it is re-defined by the linker
776 // with -defsym or -wrap options, used elsewhere, e.g. it is visible to a
777 // regular object, is referenced from llvm.compiler.used/llvm.used, or was
778 // already recorded as being referenced from a different partition.
779 if (Res.LinkerRedefined || Res.VisibleToRegularObj || Sym.isUsed() ||
780 IsLibcall ||
781 (GlobalRes.Partition != GlobalResolution::Unknown &&
782 GlobalRes.Partition != Partition)) {
783 GlobalRes.Partition = GlobalResolution::External;
784 } else
785 // First recorded reference, save the current partition.
786 GlobalRes.Partition = Partition;
787
788 // Flag as visible outside of summary if visible from a regular object or
789 // from a module that does not have a summary.
790 GlobalRes.VisibleOutsideSummary |=
791 (Res.VisibleToRegularObj || Sym.isUsed() || IsLibcall || !InSummary);
792
793 GlobalRes.ExportDynamic |= Res.ExportDynamic;
794 }
795}
796
797void LTO::releaseGlobalResolutionsMemory() {
798 // Release GlobalResolutions dense-map itself.
799 GlobalResolutions.reset();
800 // Release the string saver memory.
801 GlobalResolutionSymbolSaver.reset();
802 Alloc.reset();
803}
804
807 StringRef Path = Input->getName();
808 OS << Path << '\n';
809 auto ResI = Res.begin();
810 for (const InputFile::Symbol &Sym : Input->symbols()) {
811 assert(ResI != Res.end());
812 SymbolResolution Res = *ResI++;
813
814 OS << "-r=" << Path << ',' << Sym.getName() << ',';
815 if (Res.Prevailing)
816 OS << 'p';
818 OS << 'l';
819 if (Res.VisibleToRegularObj)
820 OS << 'x';
821 if (Res.LinkerRedefined)
822 OS << 'r';
823 OS << '\n';
824 }
825 OS.flush();
826 assert(ResI == Res.end());
827}
828
829Error LTO::add(std::unique_ptr<InputFile> InputPtr,
831 llvm::TimeTraceScope timeScope("LTO add input", InputPtr->getName());
832 assert(!CalledGetMaxTasks);
833
835 addInput(std::move(InputPtr));
836 if (!InputOrErr)
837 return InputOrErr.takeError();
838 InputFile *Input = (*InputOrErr).get();
839
840 if (Conf.ResolutionFile)
841 writeToResolutionFile(*Conf.ResolutionFile, Input, Res);
842
843 if (RegularLTO.CombinedModule->getTargetTriple().empty()) {
844 Triple InputTriple(Input->getTargetTriple());
845 RegularLTO.CombinedModule->setTargetTriple(InputTriple);
846 if (InputTriple.isOSBinFormatELF())
847 Conf.VisibilityScheme = Config::ELF;
848 }
849
850 ArrayRef<SymbolResolution> InputRes = Res;
851 for (unsigned I = 0; I != Input->Mods.size(); ++I) {
852 if (auto Err = addModule(*Input, InputRes, I, Res).moveInto(Res))
853 return Err;
854 }
855
856 assert(Res.empty());
857 return Error::success();
858}
859
861 assert(this->BitcodeLibFuncs.empty() &&
862 "bitcode libfuncs were set twice; maybe accidentally clobbered?");
863 this->BitcodeLibFuncs.append(BitcodeLibFuncs.begin(), BitcodeLibFuncs.end());
864}
865
867LTO::addModule(InputFile &Input, ArrayRef<SymbolResolution> InputRes,
868 unsigned ModI, ArrayRef<SymbolResolution> Res) {
869 llvm::TimeTraceScope timeScope("LTO add module", Input.getName());
870 Expected<BitcodeLTOInfo> LTOInfo = Input.Mods[ModI].getLTOInfo();
871 if (!LTOInfo)
872 return LTOInfo.takeError();
873
874 if (EnableSplitLTOUnit) {
875 // If only some modules were split, flag this in the index so that
876 // we can skip or error on optimizations that need consistently split
877 // modules (whole program devirt and lower type tests).
878 if (*EnableSplitLTOUnit != LTOInfo->EnableSplitLTOUnit)
880 } else
881 EnableSplitLTOUnit = LTOInfo->EnableSplitLTOUnit;
882
883 BitcodeModule BM = Input.Mods[ModI];
884
886 !LTOInfo->UnifiedLTO)
888 "unified LTO compilation must use "
889 "compatible bitcode modules (use -funified-lto)",
891
892 if (LTOInfo->UnifiedLTO && LTOMode == LTOK_Default)
894
895 bool IsThinLTO = LTOInfo->IsThinLTO && (LTOMode != LTOK_UnifiedRegular);
896 // If any of the modules inside of a input bitcode file was compiled with
897 // ThinLTO, we assume that the whole input file also was compiled with
898 // ThinLTO.
899 Input.IsThinLTO |= IsThinLTO;
900
901 auto ModSyms = Input.module_symbols(ModI);
902 addModuleToGlobalRes(ModSyms, Res,
903 IsThinLTO ? ThinLTO.ModuleMap.size() + 1 : 0,
904 LTOInfo->HasSummary, Triple(Input.getTargetTriple()));
905
906 if (IsThinLTO)
907 return addThinLTO(BM, ModSyms, Res);
908
910 auto ModOrErr = addRegularLTO(Input, InputRes, BM, ModSyms, Res);
911 if (!ModOrErr)
912 return ModOrErr.takeError();
913 Res = ModOrErr->second;
914
915 if (!LTOInfo->HasSummary) {
916 if (Error Err = linkRegularLTO(std::move(ModOrErr->first),
917 /*LivenessFromIndex=*/false))
918 return Err;
919 return Res;
920 }
921
922 // Regular LTO module summaries are added to a dummy module that represents
923 // the combined regular LTO module.
924 if (Error Err = BM.readSummary(ThinLTO.CombinedIndex, ""))
925 return Err;
926 RegularLTO.ModsWithSummaries.push_back(std::move(ModOrErr->first));
927 return Res;
928}
929
930// Checks whether the given global value is in a non-prevailing comdat
931// (comdat containing values the linker indicated were not prevailing,
932// which we then dropped to available_externally), and if so, removes
933// it from the comdat. This is called for all global values to ensure the
934// comdat is empty rather than leaving an incomplete comdat. It is needed for
935// regular LTO modules, in case we are in a mixed-LTO mode (both regular
936// and thin LTO modules) compilation. Since the regular LTO module will be
937// linked first in the final native link, we want to make sure the linker
938// doesn't select any of these incomplete comdats that would be left
939// in the regular LTO module without this cleanup.
940static void
942 std::set<const Comdat *> &NonPrevailingComdats) {
943 Comdat *C = GV.getComdat();
944 if (!C)
945 return;
946
947 if (!NonPrevailingComdats.count(C))
948 return;
949
950 // Additionally need to drop all global values from the comdat to
951 // available_externally, to satisfy the COMDAT requirement that all members
952 // are discarded as a unit. The non-local linkage global values avoid
953 // duplicate definition linker errors.
955
956 if (auto GO = dyn_cast<GlobalObject>(&GV))
957 GO->setComdat(nullptr);
958}
959
960// Add a regular LTO object to the link.
961// The resulting module needs to be linked into the combined LTO module with
962// linkRegularLTO.
963Expected<
964 std::pair<LTO::RegularLTOState::AddedModule, ArrayRef<SymbolResolution>>>
965LTO::addRegularLTO(InputFile &Input, ArrayRef<SymbolResolution> InputRes,
966 BitcodeModule BM, ArrayRef<InputFile::Symbol> Syms,
968 llvm::TimeTraceScope timeScope("LTO add regular LTO");
970 Expected<std::unique_ptr<Module>> MOrErr =
971 BM.getLazyModule(RegularLTO.Ctx, /*ShouldLazyLoadMetadata*/ true,
972 /*IsImporting*/ false);
973 if (!MOrErr)
974 return MOrErr.takeError();
975 Module &M = **MOrErr;
976 Mod.M = std::move(*MOrErr);
977
978 if (Error Err = M.materializeMetadata())
979 return std::move(Err);
980
982 // cfi.functions metadata is intended to be used with ThinLTO and may
983 // trigger invalid IR transformations if they are present when doing regular
984 // LTO, so delete it.
985 if (NamedMDNode *CfiFunctionsMD = M.getNamedMetadata("cfi.functions"))
986 M.eraseNamedMetadata(CfiFunctionsMD);
987 } else if (NamedMDNode *AliasesMD = M.getNamedMetadata("aliases")) {
988 // Delete aliases entries for non-prevailing symbols on the ThinLTO side of
989 // this input file.
990 DenseSet<StringRef> Prevailing;
991 for (auto [I, R] : zip(Input.symbols(), InputRes))
992 if (R.Prevailing && !I.getIRName().empty())
993 Prevailing.insert(I.getIRName());
994 std::vector<MDNode *> AliasGroups;
995 for (MDNode *AliasGroup : AliasesMD->operands()) {
996 std::vector<Metadata *> Aliases;
997 for (Metadata *Alias : AliasGroup->operands()) {
998 if (isa<MDString>(Alias) &&
999 Prevailing.count(cast<MDString>(Alias)->getString()))
1000 Aliases.push_back(Alias);
1001 }
1002 if (Aliases.size() > 1)
1003 AliasGroups.push_back(MDTuple::get(RegularLTO.Ctx, Aliases));
1004 }
1005 AliasesMD->clearOperands();
1006 for (MDNode *G : AliasGroups)
1007 AliasesMD->addOperand(G);
1008 }
1009
1011
1012 ModuleSymbolTable SymTab;
1013 SymTab.addModule(&M);
1014
1015 for (GlobalVariable &GV : M.globals())
1016 if (GV.hasAppendingLinkage())
1017 Mod.Keep.push_back(&GV);
1018
1019 DenseSet<GlobalObject *> AliasedGlobals;
1020 for (auto &GA : M.aliases())
1021 if (GlobalObject *GO = GA.getAliaseeObject())
1022 AliasedGlobals.insert(GO);
1023
1024 // In this function we need IR GlobalValues matching the symbols in Syms
1025 // (which is not backed by a module), so we need to enumerate them in the same
1026 // order. The symbol enumeration order of a ModuleSymbolTable intentionally
1027 // matches the order of an irsymtab, but when we read the irsymtab in
1028 // InputFile::create we omit some symbols that are irrelevant to LTO. The
1029 // Skip() function skips the same symbols from the module as InputFile does
1030 // from the symbol table.
1031 auto MsymI = SymTab.symbols().begin(), MsymE = SymTab.symbols().end();
1032 auto Skip = [&]() {
1033 while (MsymI != MsymE) {
1034 auto Flags = SymTab.getSymbolFlags(*MsymI);
1035 if ((Flags & object::BasicSymbolRef::SF_Global) &&
1037 return;
1038 ++MsymI;
1039 }
1040 };
1041 Skip();
1042
1043 std::set<const Comdat *> NonPrevailingComdats;
1044 SmallSet<StringRef, 2> NonPrevailingAsmSymbols;
1045 for (const InputFile::Symbol &Sym : Syms) {
1046 assert(!Res.empty());
1047 const SymbolResolution &R = Res.consume_front();
1048
1049 assert(MsymI != MsymE);
1050 ModuleSymbolTable::Symbol Msym = *MsymI++;
1051 Skip();
1052
1053 if (GlobalValue *GV = dyn_cast_if_present<GlobalValue *>(Msym)) {
1054 if (R.Prevailing) {
1055 if (Sym.isUndefined())
1056 continue;
1057 Mod.Keep.push_back(GV);
1058 // For symbols re-defined with linker -wrap and -defsym options,
1059 // set the linkage to weak to inhibit IPO. The linkage will be
1060 // restored by the linker.
1061 if (R.LinkerRedefined)
1062 GV->setLinkage(GlobalValue::WeakAnyLinkage);
1063
1064 GlobalValue::LinkageTypes OriginalLinkage = GV->getLinkage();
1065 if (GlobalValue::isLinkOnceLinkage(OriginalLinkage))
1066 GV->setLinkage(GlobalValue::getWeakLinkage(
1067 GlobalValue::isLinkOnceODRLinkage(OriginalLinkage)));
1068 } else if (isa<GlobalObject>(GV) &&
1069 (GV->hasLinkOnceODRLinkage() || GV->hasWeakODRLinkage() ||
1070 GV->hasAvailableExternallyLinkage()) &&
1071 !AliasedGlobals.count(cast<GlobalObject>(GV))) {
1072 // Any of the above three types of linkage indicates that the
1073 // chosen prevailing symbol will have the same semantics as this copy of
1074 // the symbol, so we may be able to link it with available_externally
1075 // linkage. We will decide later whether to do that when we link this
1076 // module (in linkRegularLTO), based on whether it is undefined.
1077 Mod.Keep.push_back(GV);
1079 if (GV->hasComdat())
1080 NonPrevailingComdats.insert(GV->getComdat());
1081 cast<GlobalObject>(GV)->setComdat(nullptr);
1082 }
1083
1084 // Set the 'local' flag based on the linker resolution for this symbol.
1085 if (R.FinalDefinitionInLinkageUnit) {
1086 GV->setDSOLocal(true);
1087 if (GV->hasDLLImportStorageClass())
1088 GV->setDLLStorageClass(GlobalValue::DLLStorageClassTypes::
1089 DefaultStorageClass);
1090 }
1091 } else if (auto *AS =
1093 // Collect non-prevailing symbols.
1094 if (!R.Prevailing)
1095 NonPrevailingAsmSymbols.insert(AS->first);
1096 } else {
1097 llvm_unreachable("unknown symbol type");
1098 }
1099
1100 // Common resolution: collect the maximum size/alignment over all commons.
1101 // We also record if we see an instance of a common as prevailing, so that
1102 // if none is prevailing we can ignore it later.
1103 if (Sym.isCommon()) {
1104 // FIXME: We should figure out what to do about commons defined by asm.
1105 // For now they aren't reported correctly by ModuleSymbolTable.
1106 auto &CommonRes = RegularLTO.Commons[std::string(Sym.getIRName())];
1107 CommonRes.Size = std::max(CommonRes.Size, Sym.getCommonSize());
1108 if (uint32_t SymAlignValue = Sym.getCommonAlignment()) {
1109 CommonRes.Alignment =
1110 std::max(Align(SymAlignValue), CommonRes.Alignment);
1111 }
1112 CommonRes.Prevailing |= R.Prevailing;
1113 }
1114 }
1115
1116 if (!M.getComdatSymbolTable().empty())
1117 for (GlobalValue &GV : M.global_values())
1118 handleNonPrevailingComdat(GV, NonPrevailingComdats);
1119
1120 // Prepend ".lto_discard <sym>, <sym>*" directive to each module inline asm
1121 // block.
1122 if (M.hasModuleInlineAsm()) {
1123 std::string NewIA = ".lto_discard";
1124 if (!NonPrevailingAsmSymbols.empty()) {
1125 // Don't dicard a symbol if there is a live .symver for it.
1127 M, [&](StringRef Name, StringRef Alias) {
1128 if (!NonPrevailingAsmSymbols.count(Alias))
1129 NonPrevailingAsmSymbols.erase(Name);
1130 });
1131 NewIA += " " + llvm::join(NonPrevailingAsmSymbols, ", ");
1132 }
1133 NewIA += "\n";
1134 M.prependModuleInlineAsm(NewIA);
1135 }
1136
1137 assert(MsymI == MsymE);
1138 return std::make_pair(std::move(Mod), Res);
1139}
1140
1141Error LTO::linkRegularLTO(RegularLTOState::AddedModule Mod,
1142 bool LivenessFromIndex) {
1143 llvm::TimeTraceScope timeScope("LTO link regular LTO");
1144 std::vector<GlobalValue *> Keep;
1145 for (GlobalValue *GV : Mod.Keep) {
1146 if (LivenessFromIndex) {
1147 const auto GUID = GV->getGUIDOrFallback();
1148 if (!ThinLTO.CombinedIndex.isGUIDLive(GUID)) {
1149 if (Function *F = dyn_cast<Function>(GV)) {
1150 if (DiagnosticOutputFile) {
1151 if (Error Err = F->materialize())
1152 return Err;
1153 auto R = OptimizationRemark(DEBUG_TYPE, "deadfunction", F);
1154 R << ore::NV("Function", F) << " not added to the combined module ";
1155 emitRemark(R);
1156 }
1157 }
1158 continue;
1159 }
1160 }
1161
1162 if (!GV->hasAvailableExternallyLinkage()) {
1163 Keep.push_back(GV);
1164 continue;
1165 }
1166
1167 // Only link available_externally definitions if we don't already have a
1168 // definition.
1169 GlobalValue *CombinedGV =
1170 RegularLTO.CombinedModule->getNamedValue(GV->getName());
1171 if (CombinedGV && !CombinedGV->isDeclaration())
1172 continue;
1173
1174 Keep.push_back(GV);
1175 }
1176
1177 return RegularLTO.Mover->move(std::move(Mod.M), Keep, nullptr,
1178 /* IsPerformingImport */ false);
1179}
1180
1181// Add a ThinLTO module to the link.
1182Expected<ArrayRef<SymbolResolution>>
1183LTO::addThinLTO(BitcodeModule BM, ArrayRef<InputFile::Symbol> Syms,
1185 llvm::TimeTraceScope timeScope("LTO add thin LTO");
1186 const auto BMID = BM.getModuleIdentifier();
1187 ArrayRef<SymbolResolution> ResTmp = Res;
1188 DenseSet<StringRef> Prevailing;
1189 for (const InputFile::Symbol &Sym : Syms) {
1190 assert(!ResTmp.empty());
1191 const SymbolResolution &R = ResTmp.consume_front();
1192 if (!Sym.getIRName().empty() && R.Prevailing)
1193 Prevailing.insert(Sym.getIRName());
1194 }
1195
1196 // Track the GUIDs stored in the bitcode GUID table.
1197 StringMap<GlobalValue::GUID> IRSpecifiedGUIDs;
1198 if (Error Err = BM.readSummary(
1199 ThinLTO.CombinedIndex, BMID,
1200 [&](StringRef Name) { return (Prevailing.count(Name) > 0); },
1201 [&](ValueInfo VI) {
1202 auto IT = IRSpecifiedGUIDs.insert({VI.name(), VI.getGUID()});
1203 (void)IT;
1204 assert(IT.second);
1205 if (auto GRIt = GlobalResolutions->find(VI.name());
1206 GRIt != GlobalResolutions->end() &&
1207 Prevailing.count(VI.name())) {
1208 GRIt->second.setGUID(VI.getGUID());
1209 }
1210 }))
1211 return Err;
1212 LLVM_DEBUG(dbgs() << "Module " << BMID << "\n");
1213
1214 for (const InputFile::Symbol &Sym : Syms) {
1215 assert(!Res.empty());
1216 const SymbolResolution &R = Res.consume_front();
1217 auto GUIDIter = IRSpecifiedGUIDs.find(Sym.getIRName());
1218 // The bitcode GUID table might not be present if this is an old bitcode
1219 // file. For backwards-compatibility, just compute the GUID now in that
1220 // case.
1221 auto GUID =
1222 GUIDIter == IRSpecifiedGUIDs.end()
1225 Sym.getIRName(), GlobalValue::ExternalLinkage, ""))
1226 : GUIDIter->second;
1227 if (!Sym.getIRName().empty() &&
1228 (R.Prevailing || R.FinalDefinitionInLinkageUnit)) {
1229 if (R.Prevailing) {
1230 ThinLTO.setPrevailingModuleForGUID(GUID, BMID);
1231 // For linker redefined symbols (via --wrap or --defsym) we want to
1232 // switch the linkage to `weak` to prevent IPOs from happening.
1233 // Find the summary in the module for this very GV and record the new
1234 // linkage so that we can switch it when we import the GV.
1235 if (R.LinkerRedefined)
1236 if (auto *S = ThinLTO.CombinedIndex.findSummaryInModule(GUID, BMID))
1237 S->setLinkage(GlobalValue::WeakAnyLinkage);
1238 }
1239
1240 // If the linker resolved the symbol to a local definition then mark it
1241 // as local in the summary for the module we are adding.
1242 if (R.FinalDefinitionInLinkageUnit) {
1243 if (auto *S = ThinLTO.CombinedIndex.findSummaryInModule(GUID, BMID)) {
1244 S->setDSOLocal(true);
1245 }
1246 }
1247 }
1248 }
1249
1250 if (!ThinLTO.ModuleMap.insert({BMID, BM}).second)
1252 "Expected at most one ThinLTO module per bitcode file",
1254
1255 if (!Conf.ThinLTOModulesToCompile.empty()) {
1256 if (!ThinLTO.ModulesToCompile)
1257 ThinLTO.ModulesToCompile = ModuleMapType();
1258 // This is a fuzzy name matching where only modules with name containing the
1259 // specified switch values are going to be compiled.
1260 for (const std::string &Name : Conf.ThinLTOModulesToCompile) {
1261 if (BMID.contains(Name)) {
1262 ThinLTO.ModulesToCompile->insert({BMID, BM});
1263 LLVM_DEBUG(dbgs() << "[ThinLTO] Selecting " << BMID << " to compile\n");
1264 break;
1265 }
1266 }
1267 }
1268
1269 return Res;
1270}
1271
1272unsigned LTO::getMaxTasks() const {
1273 CalledGetMaxTasks = true;
1274 auto ModuleCount = ThinLTO.ModulesToCompile ? ThinLTO.ModulesToCompile->size()
1275 : ThinLTO.ModuleMap.size();
1276 return RegularLTO.ParallelCodeGenParallelismLevel + ModuleCount;
1277}
1278
1279// If only some of the modules were split, we cannot correctly handle
1280// code that contains type tests or type checked loads.
1281Error LTO::checkPartiallySplit() {
1283 return Error::success();
1284
1285 const Module *Combined = RegularLTO.CombinedModule.get();
1286 Function *TypeTestFunc =
1287 Intrinsic::getDeclarationIfExists(Combined, Intrinsic::type_test);
1288 Function *TypeCheckedLoadFunc =
1289 Intrinsic::getDeclarationIfExists(Combined, Intrinsic::type_checked_load);
1290 Function *TypeCheckedLoadRelativeFunc = Intrinsic::getDeclarationIfExists(
1291 Combined, Intrinsic::type_checked_load_relative);
1292
1293 // First check if there are type tests / type checked loads in the
1294 // merged regular LTO module IR.
1295 if ((TypeTestFunc && !TypeTestFunc->use_empty()) ||
1296 (TypeCheckedLoadFunc && !TypeCheckedLoadFunc->use_empty()) ||
1297 (TypeCheckedLoadRelativeFunc &&
1298 !TypeCheckedLoadRelativeFunc->use_empty()))
1300 "inconsistent LTO Unit splitting (recompile with -fsplit-lto-unit)",
1302
1303 // Otherwise check if there are any recorded in the combined summary from the
1304 // ThinLTO modules.
1305 for (auto &P : ThinLTO.CombinedIndex) {
1306 for (auto &S : P.second.getSummaryList()) {
1307 auto *FS = dyn_cast<FunctionSummary>(S.get());
1308 if (!FS)
1309 continue;
1310 if (!FS->type_test_assume_vcalls().empty() ||
1311 !FS->type_checked_load_vcalls().empty() ||
1312 !FS->type_test_assume_const_vcalls().empty() ||
1313 !FS->type_checked_load_const_vcalls().empty() ||
1314 !FS->type_tests().empty())
1316 "inconsistent LTO Unit splitting (recompile with -fsplit-lto-unit)",
1318 }
1319 }
1320 return Error::success();
1321}
1322
1324 // Call the base class cleanup() explicitly since run() may be invoked on a
1325 // derived LTO object.
1326 llvm::scope_exit CleanUp([this]() { LTO::cleanup(); });
1327
1328 // Compute "dead" symbols, we don't want to import/export these!
1329 DenseSet<GlobalValue::GUID> GUIDPreservedSymbols;
1330 DenseMap<GlobalValue::GUID, PrevailingType> GUIDPrevailingResolutions;
1331 for (auto &Res : *GlobalResolutions) {
1332 // Normally resolution have IR name of symbol. We can do nothing here
1333 // otherwise. See comments in GlobalResolution struct for more details.
1334 if (Res.second.IRName.empty())
1335 continue;
1336
1337 GlobalValue::GUID GUID = Res.second.getGUID();
1338
1339 if (Res.second.VisibleOutsideSummary && Res.second.Prevailing)
1340 GUIDPreservedSymbols.insert(GUID);
1341
1342 if (Res.second.ExportDynamic)
1343 DynamicExportSymbols.insert(GUID);
1344
1345 GUIDPrevailingResolutions[GUID] =
1346 Res.second.Prevailing ? PrevailingType::Yes : PrevailingType::No;
1347 }
1348
1349 auto isPrevailing = [&](GlobalValue::GUID G) {
1350 auto It = GUIDPrevailingResolutions.find(G);
1351 if (It == GUIDPrevailingResolutions.end())
1353 return It->second;
1354 };
1355 computeDeadSymbolsWithConstProp(ThinLTO.CombinedIndex, GUIDPreservedSymbols,
1356 isPrevailing, Conf.OptLevel > 0);
1357
1358 // Setup output file to emit statistics.
1359 auto StatsFileOrErr = setupStatsFile(Conf.StatsFile);
1360 if (!StatsFileOrErr)
1361 return StatsFileOrErr.takeError();
1362 std::unique_ptr<ToolOutputFile> StatsFile = std::move(StatsFileOrErr.get());
1363
1364 if (Error Err = setupOptimizationRemarks())
1365 return Err;
1366
1367 // TODO: Ideally this would be controlled automatically by detecting that we
1368 // are linking with an allocator that supports these interfaces, rather than
1369 // an internal option (which would still be needed for tests, however). For
1370 // example, if the library exported a symbol like __malloc_hot_cold the linker
1371 // could recognize that and set a flag in the lto::Config.
1373 ThinLTO.CombinedIndex.setWithSupportsHotColdNew();
1374
1375 Error Result = runRegularLTO(AddStream);
1376 if (!Result)
1377 // This will reset the GlobalResolutions optional once done with it to
1378 // reduce peak memory before importing.
1379 Result = runThinLTO(AddStream, Cache, GUIDPreservedSymbols);
1380
1381 if (StatsFile)
1382 PrintStatisticsJSON(StatsFile->os());
1383
1384 return Result;
1385}
1386
1387Error LTO::runRegularLTO(AddStreamFn AddStream) {
1388 llvm::TimeTraceScope timeScope("Run regular LTO");
1389 LLVM_DEBUG(dbgs() << "Running regular LTO\n");
1390
1391 // Finalize linking of regular LTO modules containing summaries now that
1392 // we have computed liveness information.
1393 {
1394 llvm::TimeTraceScope timeScope("Link regular LTO");
1395 for (auto &M : RegularLTO.ModsWithSummaries)
1396 if (Error Err = linkRegularLTO(std::move(M), /*LivenessFromIndex=*/true))
1397 return Err;
1398 }
1399
1400 // Ensure we don't have inconsistently split LTO units with type tests.
1401 // FIXME: this checks both LTO and ThinLTO. It happens to work as we take
1402 // this path both cases but eventually this should be split into two and
1403 // do the ThinLTO checks in `runThinLTO`.
1404 if (Error Err = checkPartiallySplit())
1405 return Err;
1406
1407 // Make sure commons have the right size/alignment: we kept the largest from
1408 // all the prevailing when adding the inputs, and we apply it here.
1409 const DataLayout &DL = RegularLTO.CombinedModule->getDataLayout();
1410 for (auto &I : RegularLTO.Commons) {
1411 if (!I.second.Prevailing)
1412 // Don't do anything if no instance of this common was prevailing.
1413 continue;
1414 GlobalVariable *OldGV = RegularLTO.CombinedModule->getNamedGlobal(I.first);
1415 if (OldGV && OldGV->getGlobalSize(DL) == I.second.Size) {
1416 // Don't create a new global if the type is already correct, just make
1417 // sure the alignment is correct.
1418 OldGV->setAlignment(I.second.Alignment);
1419 continue;
1420 }
1421 ArrayType *Ty =
1423 auto *GV = new GlobalVariable(*RegularLTO.CombinedModule, Ty, false,
1426 GV->setAlignment(I.second.Alignment);
1427 if (OldGV) {
1428 OldGV->replaceAllUsesWith(GV);
1429 GV->takeName(OldGV);
1430 OldGV->eraseFromParent();
1431 } else {
1432 GV->setName(I.first);
1433 }
1434 }
1435
1436 bool WholeProgramVisibilityEnabledInLTO =
1437 Conf.HasWholeProgramVisibility &&
1438 // If validation is enabled, upgrade visibility only when all vtables
1439 // have typeinfos.
1440 (!Conf.ValidateAllVtablesHaveTypeInfos || Conf.AllVtablesHaveTypeInfos);
1441
1442 // This returns true when the name is local or not defined. Locals are
1443 // expected to be handled separately.
1444 auto IsVisibleToRegularObj = [&](StringRef name) {
1445 auto It = GlobalResolutions->find(name);
1446 return (It == GlobalResolutions->end() ||
1447 It->second.VisibleOutsideSummary || !It->second.Prevailing);
1448 };
1449
1450 // If allowed, upgrade public vcall visibility metadata to linkage unit
1451 // visibility before whole program devirtualization in the optimizer.
1453 *RegularLTO.CombinedModule, WholeProgramVisibilityEnabledInLTO,
1454 DynamicExportSymbols, Conf.ValidateAllVtablesHaveTypeInfos,
1455 IsVisibleToRegularObj);
1456 updatePublicTypeTestCalls(*RegularLTO.CombinedModule,
1457 WholeProgramVisibilityEnabledInLTO);
1458
1459 if (Conf.PreOptModuleHook &&
1460 !Conf.PreOptModuleHook(0, *RegularLTO.CombinedModule))
1461 return Error::success();
1462
1463 if (!Conf.CodeGenOnly) {
1464 for (const auto &R : *GlobalResolutions) {
1465 GlobalValue *GV =
1466 RegularLTO.CombinedModule->getNamedValue(R.second.IRName);
1467 if (!R.second.isPrevailingIRSymbol())
1468 continue;
1469 if (R.second.Partition != 0 &&
1470 R.second.Partition != GlobalResolution::External)
1471 continue;
1472
1473 // Ignore symbols defined in other partitions.
1474 // Also skip declarations, which are not allowed to have internal linkage.
1475 if (!GV || GV->hasLocalLinkage() || GV->isDeclaration())
1476 continue;
1477
1478 // Symbols that are marked DLLImport or DLLExport should not be
1479 // internalized, as they are either externally visible or referencing
1480 // external symbols. Symbols that have AvailableExternally or Appending
1481 // linkage might be used by future passes and should be kept as is.
1482 // These linkages are seen in Unified regular LTO, because the process
1483 // of creating split LTO units introduces symbols with that linkage into
1484 // one of the created modules. Normally, only the ThinLTO backend would
1485 // compile this module, but Unified Regular LTO processes both
1486 // modules created by the splitting process as regular LTO modules.
1490 continue;
1491
1492 GV->setUnnamedAddr(R.second.UnnamedAddr ? GlobalValue::UnnamedAddr::Global
1494 if (EnableLTOInternalization && R.second.Partition == 0)
1496 }
1497
1498 if (Conf.PostInternalizeModuleHook &&
1499 !Conf.PostInternalizeModuleHook(0, *RegularLTO.CombinedModule))
1500 return Error::success();
1501 }
1502
1503 if (!RegularLTO.EmptyCombinedModule || Conf.AlwaysEmitRegularLTOObj) {
1504 if (Error Err = backend(
1505 Conf, AddStream, RegularLTO.ParallelCodeGenParallelismLevel,
1506 *RegularLTO.CombinedModule, ThinLTO.CombinedIndex, BitcodeLibFuncs))
1507 return Err;
1508 }
1509
1510 return Error::success();
1511}
1512
1514 RTLIB::RuntimeLibcallsInfo Libcalls(TT);
1515 SmallVector<const char *> LibcallSymbols;
1516 LibcallSymbols.reserve(Libcalls.getNumAvailableLibcallImpls());
1517
1518 for (RTLIB::LibcallImpl Impl : RTLIB::libcall_impls()) {
1519 if (Libcalls.isAvailable(Impl))
1520 LibcallSymbols.push_back(Libcalls.getLibcallImplName(Impl).data());
1521 }
1522
1523 return LibcallSymbols;
1524}
1525
1527 StringSaver &Saver) {
1528 auto TLII = std::make_unique<TargetLibraryInfoImpl>(TT);
1529 TargetLibraryInfo TLI(*TLII);
1530 SmallVector<StringRef> LibFuncSymbols;
1531 LibFuncSymbols.reserve(LibFunc::NumLibFuncs);
1532 for (unsigned I = LibFunc::Begin_LibFunc; I != LibFunc::End_LibFunc; ++I) {
1533 LibFunc F = static_cast<LibFunc>(I);
1534 if (TLI.has(F))
1535 LibFuncSymbols.push_back(Saver.save(TLI.getName(F)).data());
1536 }
1537 return LibFuncSymbols;
1538}
1539
1541 const FunctionImporter::ImportMapTy &ImportList, unsigned Task,
1542 llvm::StringRef ModulePath, const std::string &NewModulePath) const {
1543 return emitFiles(ImportList, Task, ModulePath, NewModulePath,
1544 NewModulePath + ".thinlto.bc");
1545}
1546
1548 const FunctionImporter::ImportMapTy &ImportList, unsigned Task,
1549 llvm::StringRef ModulePath, const std::string &NewModulePath,
1550 StringRef SummaryPath) const {
1551 ModuleToSummariesForIndexTy ModuleToSummariesForIndex;
1552 GVSummaryPtrSet DeclarationSummaries;
1553
1554 std::error_code EC;
1556 ImportList, ModuleToSummariesForIndex,
1557 DeclarationSummaries);
1558 // Resolve the output stream (either file-backed or callback-provided) for the
1559 // index file.
1560 std::unique_ptr<raw_pwrite_stream> OS;
1561 if (Conf.GetSummaryIndexOutputStream) {
1562 OS = Conf.GetSummaryIndexOutputStream(Task);
1563 assert(OS && "GetSummaryIndexOutputStream returned null");
1564 } else {
1565 auto FileOS = std::make_unique<raw_fd_ostream>(SummaryPath, EC,
1567 if (EC)
1568 return createFileError("cannot open " + Twine(SummaryPath), EC);
1569 OS = std::move(FileOS);
1570 }
1571
1572 writeIndexToFile(CombinedIndex, *OS, &ModuleToSummariesForIndex,
1573 &DeclarationSummaries);
1574
1575 // Emit imports files if requested, using callback if provided.
1576 if (Conf.GetImportsListOutputArray) {
1577 std::vector<std::string> &ImportsListRef =
1578 Conf.GetImportsListOutputArray(Task);
1580 ModulePath, ModuleToSummariesForIndex,
1581 [&](StringRef M) { ImportsListRef.push_back(M.str()); });
1582 } else if (ShouldEmitImportsFiles) {
1583 if (Error E = EmitImportsFiles(ModulePath, NewModulePath + ".imports",
1584 ModuleToSummariesForIndex))
1585 return E;
1586 }
1587 return Error::success();
1588}
1589
1590namespace {
1591/// Base class for ThinLTO backends that perform code generation and insert the
1592/// generated files back into the link.
1593class CGThinBackend : public ThinBackendProc {
1594protected:
1595 DenseSet<GlobalValue::GUID> CfiFunctionDefs;
1596 DenseSet<GlobalValue::GUID> CfiFunctionDecls;
1597 bool ShouldEmitIndexFiles;
1598
1599public:
1600 CGThinBackend(
1601 const Config &Conf, ModuleSummaryIndex &CombinedIndex,
1602 const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1603 lto::IndexWriteCallback OnWrite, bool ShouldEmitIndexFiles,
1604 bool ShouldEmitImportsFiles, ThreadPoolStrategy ThinLTOParallelism)
1605 : ThinBackendProc(Conf, CombinedIndex, ModuleToDefinedGVSummaries,
1606 OnWrite, ShouldEmitImportsFiles, ThinLTOParallelism),
1607 ShouldEmitIndexFiles(ShouldEmitIndexFiles) {
1608 auto &Defs = CombinedIndex.cfiFunctionDefs();
1609 CfiFunctionDefs.insert_range(Defs.getExportedThinLTOGUIDs());
1610 auto &Decls = CombinedIndex.cfiFunctionDecls();
1611 CfiFunctionDecls.insert_range(Decls.getExportedThinLTOGUIDs());
1612 }
1613};
1614
1615/// This backend performs code generation by scheduling a job to run on
1616/// an in-process thread when invoked for each task.
1617class InProcessThinBackend : public CGThinBackend {
1618protected:
1619 // Callback used to add generated native object files to the link by code
1620 // generating directly into the returned output stream.
1621 AddStreamFn AddStream;
1622 FileCache Cache;
1623 ArrayRef<StringRef> BitcodeLibFuncs;
1624
1625public:
1626 InProcessThinBackend(
1627 const Config &Conf, ModuleSummaryIndex &CombinedIndex,
1628 ThreadPoolStrategy ThinLTOParallelism,
1629 const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1630 AddStreamFn AddStream, FileCache Cache, lto::IndexWriteCallback OnWrite,
1631 bool ShouldEmitIndexFiles, bool ShouldEmitImportsFiles,
1632 ArrayRef<StringRef> BitcodeLibFuncs)
1633 : CGThinBackend(Conf, CombinedIndex, ModuleToDefinedGVSummaries, OnWrite,
1634 ShouldEmitIndexFiles, ShouldEmitImportsFiles,
1635 ThinLTOParallelism),
1636 AddStream(std::move(AddStream)), Cache(std::move(Cache)),
1637 BitcodeLibFuncs(BitcodeLibFuncs) {}
1638
1639 virtual Error runThinLTOBackendThread(
1640 AddStreamFn AddStream, FileCache Cache, unsigned Task, BitcodeModule BM,
1641 ModuleSummaryIndex &CombinedIndex,
1642 const FunctionImporter::ImportMapTy &ImportList,
1643 const FunctionImporter::ExportSetTy &ExportList,
1644 const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
1645 const GVSummaryMapTy &DefinedGlobals,
1646 MapVector<StringRef, BitcodeModule> &ModuleMap) {
1647 auto ModuleID = BM.getModuleIdentifier();
1648 llvm::TimeTraceScope timeScope("Run ThinLTO backend thread (in-process)",
1649 ModuleID);
1650 auto RunThinBackend = [&](AddStreamFn AddStream) {
1651 LTOLLVMContext BackendContext(Conf);
1652 Expected<std::unique_ptr<Module>> MOrErr = BM.parseModule(BackendContext);
1653 if (!MOrErr)
1654 return MOrErr.takeError();
1655
1656 return thinBackend(Conf, Task, AddStream, **MOrErr, CombinedIndex,
1657 ImportList, DefinedGlobals, &ModuleMap,
1658 Conf.CodeGenOnly, BitcodeLibFuncs);
1659 };
1660 if (ShouldEmitIndexFiles) {
1661 if (auto E = emitFiles(ImportList, Task, ModuleID, ModuleID.str()))
1662 return E;
1663 }
1664
1665 if (!Cache.isValid() || !CombinedIndex.modulePaths().count(ModuleID) ||
1666 all_of(CombinedIndex.getModuleHash(ModuleID),
1667 [](uint32_t V) { return V == 0; }))
1668 // Cache disabled or no entry for this module in the combined index or
1669 // no module hash.
1670 return RunThinBackend(AddStream);
1671
1672 // The module may be cached, this helps handling it.
1673 std::string Key = computeLTOCacheKey(
1674 Conf, CombinedIndex, ModuleID, ImportList, ExportList, ResolvedODR,
1675 DefinedGlobals, CfiFunctionDefs, CfiFunctionDecls);
1676 Expected<AddStreamFn> CacheAddStreamOrErr = Cache(Task, Key, ModuleID);
1677 if (Error Err = CacheAddStreamOrErr.takeError())
1678 return Err;
1679 AddStreamFn &CacheAddStream = *CacheAddStreamOrErr;
1680 if (CacheAddStream)
1681 return RunThinBackend(CacheAddStream);
1682
1683 return Error::success();
1684 }
1685
1686 Error start(
1687 unsigned Task, BitcodeModule BM,
1688 const FunctionImporter::ImportMapTy &ImportList,
1689 const FunctionImporter::ExportSetTy &ExportList,
1690 const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
1691 MapVector<StringRef, BitcodeModule> &ModuleMap) override {
1692 StringRef ModulePath = BM.getModuleIdentifier();
1693 assert(ModuleToDefinedGVSummaries.count(ModulePath));
1694 const GVSummaryMapTy &DefinedGlobals =
1695 ModuleToDefinedGVSummaries.find(ModulePath)->second;
1696 BackendThreadPool.async(
1697 [=](BitcodeModule BM, ModuleSummaryIndex &CombinedIndex,
1698 const FunctionImporter::ImportMapTy &ImportList,
1699 const FunctionImporter::ExportSetTy &ExportList,
1700 const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>
1701 &ResolvedODR,
1702 const GVSummaryMapTy &DefinedGlobals,
1703 MapVector<StringRef, BitcodeModule> &ModuleMap) {
1704 if (LLVM_ENABLE_THREADS && Conf.TimeTraceEnabled)
1706 "thin backend");
1707 Error E = runThinLTOBackendThread(
1708 AddStream, Cache, Task, BM, CombinedIndex, ImportList, ExportList,
1709 ResolvedODR, DefinedGlobals, ModuleMap);
1710 if (E) {
1711 std::unique_lock<std::mutex> L(ErrMu);
1712 if (Err)
1713 Err = joinErrors(std::move(*Err), std::move(E));
1714 else
1715 Err = std::move(E);
1716 }
1717 if (LLVM_ENABLE_THREADS && Conf.TimeTraceEnabled)
1719 },
1720 BM, std::ref(CombinedIndex), std::ref(ImportList), std::ref(ExportList),
1721 std::ref(ResolvedODR), std::ref(DefinedGlobals), std::ref(ModuleMap));
1722
1723 if (OnWrite)
1724 OnWrite(std::string(ModulePath));
1725 return Error::success();
1726 }
1727};
1728
1729/// This backend is utilized in the first round of a two-codegen round process.
1730/// It first saves optimized bitcode files to disk before the codegen process
1731/// begins. After codegen, it stores the resulting object files in a scratch
1732/// buffer. Note the codegen data stored in the scratch buffer will be extracted
1733/// and merged in the subsequent step.
1734class FirstRoundThinBackend : public InProcessThinBackend {
1735 AddStreamFn IRAddStream;
1736 FileCache IRCache;
1737
1738public:
1739 FirstRoundThinBackend(
1740 const Config &Conf, ModuleSummaryIndex &CombinedIndex,
1741 ThreadPoolStrategy ThinLTOParallelism,
1742 const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1743 AddStreamFn CGAddStream, FileCache CGCache,
1744 ArrayRef<StringRef> BitcodeLibFuncs, AddStreamFn IRAddStream,
1745 FileCache IRCache)
1746 : InProcessThinBackend(Conf, CombinedIndex, ThinLTOParallelism,
1747 ModuleToDefinedGVSummaries, std::move(CGAddStream),
1748 std::move(CGCache), /*OnWrite=*/nullptr,
1749 /*ShouldEmitIndexFiles=*/false,
1750 /*ShouldEmitImportsFiles=*/false, BitcodeLibFuncs),
1751 IRAddStream(std::move(IRAddStream)), IRCache(std::move(IRCache)) {}
1752
1753 Error runThinLTOBackendThread(
1754 AddStreamFn CGAddStream, FileCache CGCache, unsigned Task,
1755 BitcodeModule BM, ModuleSummaryIndex &CombinedIndex,
1756 const FunctionImporter::ImportMapTy &ImportList,
1757 const FunctionImporter::ExportSetTy &ExportList,
1758 const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
1759 const GVSummaryMapTy &DefinedGlobals,
1760 MapVector<StringRef, BitcodeModule> &ModuleMap) override {
1761 auto ModuleID = BM.getModuleIdentifier();
1762 llvm::TimeTraceScope timeScope("Run ThinLTO backend thread (first round)",
1763 ModuleID);
1764 auto RunThinBackend = [&](AddStreamFn CGAddStream,
1765 AddStreamFn IRAddStream) {
1766 LTOLLVMContext BackendContext(Conf);
1767 Expected<std::unique_ptr<Module>> MOrErr = BM.parseModule(BackendContext);
1768 if (!MOrErr)
1769 return MOrErr.takeError();
1770
1771 return thinBackend(Conf, Task, CGAddStream, **MOrErr, CombinedIndex,
1772 ImportList, DefinedGlobals, &ModuleMap,
1773 Conf.CodeGenOnly, BitcodeLibFuncs, IRAddStream);
1774 };
1775 // Like InProcessThinBackend, we produce index files as needed for
1776 // FirstRoundThinBackend. However, these files are not generated for
1777 // SecondRoundThinBackend.
1778 if (ShouldEmitIndexFiles) {
1779 if (auto E = emitFiles(ImportList, Task, ModuleID, ModuleID.str()))
1780 return E;
1781 }
1782
1783 assert((CGCache.isValid() == IRCache.isValid()) &&
1784 "Both caches for CG and IR should have matching availability");
1785 if (!CGCache.isValid() || !CombinedIndex.modulePaths().count(ModuleID) ||
1786 all_of(CombinedIndex.getModuleHash(ModuleID),
1787 [](uint32_t V) { return V == 0; }))
1788 // Cache disabled or no entry for this module in the combined index or
1789 // no module hash.
1790 return RunThinBackend(CGAddStream, IRAddStream);
1791
1792 // Get CGKey for caching object in CGCache.
1793 std::string CGKey = computeLTOCacheKey(
1794 Conf, CombinedIndex, ModuleID, ImportList, ExportList, ResolvedODR,
1795 DefinedGlobals, CfiFunctionDefs, CfiFunctionDecls);
1796 Expected<AddStreamFn> CacheCGAddStreamOrErr =
1797 CGCache(Task, CGKey, ModuleID);
1798 if (Error Err = CacheCGAddStreamOrErr.takeError())
1799 return Err;
1800 AddStreamFn &CacheCGAddStream = *CacheCGAddStreamOrErr;
1801
1802 // Get IRKey for caching (optimized) IR in IRCache with an extra ID.
1803 std::string IRKey = recomputeLTOCacheKey(CGKey, /*ExtraID=*/"IR");
1804 Expected<AddStreamFn> CacheIRAddStreamOrErr =
1805 IRCache(Task, IRKey, ModuleID);
1806 if (Error Err = CacheIRAddStreamOrErr.takeError())
1807 return Err;
1808 AddStreamFn &CacheIRAddStream = *CacheIRAddStreamOrErr;
1809
1810 // Ideally, both CG and IR caching should be synchronized. However, in
1811 // practice, their availability may differ due to different expiration
1812 // times. Therefore, if either cache is missing, the backend process is
1813 // triggered.
1814 if (CacheCGAddStream || CacheIRAddStream) {
1815 LLVM_DEBUG(dbgs() << "[FirstRound] Cache Miss for "
1816 << BM.getModuleIdentifier() << "\n");
1817 return RunThinBackend(CacheCGAddStream ? CacheCGAddStream : CGAddStream,
1818 CacheIRAddStream ? CacheIRAddStream : IRAddStream);
1819 }
1820
1821 return Error::success();
1822 }
1823};
1824
1825/// This backend operates in the second round of a two-codegen round process.
1826/// It starts by reading the optimized bitcode files that were saved during the
1827/// first round. The backend then executes the codegen only to further optimize
1828/// the code, utilizing the codegen data merged from the first round. Finally,
1829/// it writes the resulting object files as usual.
1830class SecondRoundThinBackend : public InProcessThinBackend {
1831 std::unique_ptr<SmallVector<StringRef>> IRFiles;
1832 stable_hash CombinedCGDataHash;
1833
1834public:
1835 SecondRoundThinBackend(
1836 const Config &Conf, ModuleSummaryIndex &CombinedIndex,
1837 ThreadPoolStrategy ThinLTOParallelism,
1838 const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1839 AddStreamFn AddStream, FileCache Cache,
1840 ArrayRef<StringRef> BitcodeLibFuncs,
1841 std::unique_ptr<SmallVector<StringRef>> IRFiles,
1842 stable_hash CombinedCGDataHash)
1843 : InProcessThinBackend(Conf, CombinedIndex, ThinLTOParallelism,
1844 ModuleToDefinedGVSummaries, std::move(AddStream),
1845 std::move(Cache),
1846 /*OnWrite=*/nullptr,
1847 /*ShouldEmitIndexFiles=*/false,
1848 /*ShouldEmitImportsFiles=*/false, BitcodeLibFuncs),
1849 IRFiles(std::move(IRFiles)), CombinedCGDataHash(CombinedCGDataHash) {}
1850
1851 Error runThinLTOBackendThread(
1852 AddStreamFn AddStream, FileCache Cache, unsigned Task, BitcodeModule BM,
1853 ModuleSummaryIndex &CombinedIndex,
1854 const FunctionImporter::ImportMapTy &ImportList,
1855 const FunctionImporter::ExportSetTy &ExportList,
1856 const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
1857 const GVSummaryMapTy &DefinedGlobals,
1858 MapVector<StringRef, BitcodeModule> &ModuleMap) override {
1859 auto ModuleID = BM.getModuleIdentifier();
1860 llvm::TimeTraceScope timeScope("Run ThinLTO backend thread (second round)",
1861 ModuleID);
1862 auto RunThinBackend = [&](AddStreamFn AddStream) {
1863 LTOLLVMContext BackendContext(Conf);
1864 std::unique_ptr<Module> LoadedModule =
1865 cgdata::loadModuleForTwoRounds(BM, Task, BackendContext, *IRFiles);
1866
1867 return thinBackend(Conf, Task, AddStream, *LoadedModule, CombinedIndex,
1868 ImportList, DefinedGlobals, &ModuleMap,
1869 /*CodeGenOnly=*/true, BitcodeLibFuncs);
1870 };
1871 if (!Cache.isValid() || !CombinedIndex.modulePaths().count(ModuleID) ||
1872 all_of(CombinedIndex.getModuleHash(ModuleID),
1873 [](uint32_t V) { return V == 0; }))
1874 // Cache disabled or no entry for this module in the combined index or
1875 // no module hash.
1876 return RunThinBackend(AddStream);
1877
1878 // Get Key for caching the final object file in Cache with the combined
1879 // CGData hash.
1880 std::string Key = computeLTOCacheKey(
1881 Conf, CombinedIndex, ModuleID, ImportList, ExportList, ResolvedODR,
1882 DefinedGlobals, CfiFunctionDefs, CfiFunctionDecls);
1884 /*ExtraID=*/std::to_string(CombinedCGDataHash));
1885 Expected<AddStreamFn> CacheAddStreamOrErr = Cache(Task, Key, ModuleID);
1886 if (Error Err = CacheAddStreamOrErr.takeError())
1887 return Err;
1888 AddStreamFn &CacheAddStream = *CacheAddStreamOrErr;
1889
1890 if (CacheAddStream) {
1891 LLVM_DEBUG(dbgs() << "[SecondRound] Cache Miss for "
1892 << BM.getModuleIdentifier() << "\n");
1893 return RunThinBackend(CacheAddStream);
1894 }
1895
1896 return Error::success();
1897 }
1898};
1899} // end anonymous namespace
1900
1903 bool ShouldEmitIndexFiles,
1904 bool ShouldEmitImportsFiles) {
1905 auto Func =
1906 [=](const Config &Conf, ModuleSummaryIndex &CombinedIndex,
1907 const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1908 AddStreamFn AddStream, FileCache Cache,
1909 ArrayRef<StringRef> BitcodeLibFuncs) {
1910 return std::make_unique<InProcessThinBackend>(
1911 Conf, CombinedIndex, Parallelism, ModuleToDefinedGVSummaries,
1912 AddStream, Cache, OnWrite, ShouldEmitIndexFiles,
1913 ShouldEmitImportsFiles, BitcodeLibFuncs);
1914 };
1915 return ThinBackend(Func, Parallelism);
1916}
1917
1919 if (!TheTriple.isOSDarwin())
1920 return "";
1921 if (TheTriple.getArch() == Triple::x86_64)
1922 return "core2";
1923 if (TheTriple.getArch() == Triple::x86)
1924 return "yonah";
1925 if (TheTriple.isArm64e())
1926 return "apple-a12";
1927 if (TheTriple.getArch() == Triple::aarch64 ||
1928 TheTriple.getArch() == Triple::aarch64_32)
1929 return "cyclone";
1930 return "";
1931}
1932
1933// Given the original \p Path to an output file, replace any path
1934// prefix matching \p OldPrefix with \p NewPrefix. Also, create the
1935// resulting directory if it does not yet exist.
1937 StringRef NewPrefix) {
1938 if (OldPrefix.empty() && NewPrefix.empty())
1939 return std::string(Path);
1940 SmallString<128> NewPath(Path);
1941 llvm::sys::path::replace_path_prefix(NewPath, OldPrefix, NewPrefix);
1942 StringRef ParentPath = llvm::sys::path::parent_path(NewPath.str());
1943 if (!ParentPath.empty()) {
1944 // Make sure the new directory exists, creating it if necessary.
1945 if (std::error_code EC = llvm::sys::fs::create_directories(ParentPath))
1946 llvm::errs() << "warning: could not create directory '" << ParentPath
1947 << "': " << EC.message() << '\n';
1948 }
1949 return std::string(NewPath);
1950}
1951
1952namespace {
1953class WriteIndexesThinBackend : public ThinBackendProc {
1954 std::string OldPrefix, NewPrefix, NativeObjectPrefix;
1955 raw_fd_ostream *LinkedObjectsFile;
1956 DenseSet<GlobalValue::GUID> CfiFunctionDefs;
1957 DenseSet<GlobalValue::GUID> CfiFunctionDecls;
1958
1959public:
1960 WriteIndexesThinBackend(
1961 const Config &Conf, ModuleSummaryIndex &CombinedIndex,
1962 ThreadPoolStrategy ThinLTOParallelism,
1963 const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1964 std::string OldPrefix, std::string NewPrefix,
1965 std::string NativeObjectPrefix, bool ShouldEmitImportsFiles,
1966 raw_fd_ostream *LinkedObjectsFile, lto::IndexWriteCallback OnWrite)
1967 : ThinBackendProc(Conf, CombinedIndex, ModuleToDefinedGVSummaries,
1968 OnWrite, ShouldEmitImportsFiles, ThinLTOParallelism),
1969 OldPrefix(OldPrefix), NewPrefix(NewPrefix),
1970 NativeObjectPrefix(NativeObjectPrefix),
1971 LinkedObjectsFile(LinkedObjectsFile) {
1972 auto Defs = CombinedIndex.cfiFunctionDefs().getExportedThinLTOGUIDs();
1973 CfiFunctionDefs.insert(Defs.begin(), Defs.end());
1974 auto Decls = CombinedIndex.cfiFunctionDecls().getExportedThinLTOGUIDs();
1975 CfiFunctionDecls.insert(Decls.begin(), Decls.end());
1976 }
1977
1978 Error start(
1979 unsigned Task, BitcodeModule BM,
1980 const FunctionImporter::ImportMapTy &ImportList,
1981 const FunctionImporter::ExportSetTy &ExportList,
1982 const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
1983 MapVector<StringRef, BitcodeModule> &ModuleMap) override {
1984 StringRef ModulePath = BM.getModuleIdentifier();
1985
1986 // The contents of this file may be used as input to a native link, and must
1987 // therefore contain the processed modules in a determinstic order that
1988 // match the order they are provided on the command line. For that reason,
1989 // we cannot include this in the asynchronously executed lambda below.
1990 if (LinkedObjectsFile) {
1991 std::string ObjectPrefix =
1992 NativeObjectPrefix.empty() ? NewPrefix : NativeObjectPrefix;
1993 std::string LinkedObjectsFilePath =
1994 getThinLTOOutputFile(ModulePath, OldPrefix, ObjectPrefix);
1995 *LinkedObjectsFile << LinkedObjectsFilePath << '\n';
1996 }
1997
1998 BackendThreadPool.async(
1999 [this](unsigned Task, const StringRef ModulePath,
2000 const FunctionImporter::ImportMapTy &ImportList,
2001 const FunctionImporter::ExportSetTy &ExportList,
2002 const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>
2003 &ResolvedODR,
2004 const std::string &OldPrefix, const std::string &NewPrefix) {
2005 std::string NewModulePath =
2006 getThinLTOOutputFile(ModulePath, OldPrefix, NewPrefix);
2007 auto E = emitFiles(ImportList, Task, ModulePath, NewModulePath);
2008 if (E) {
2009 std::unique_lock<std::mutex> L(ErrMu);
2010 if (Err)
2011 Err = joinErrors(std::move(*Err), std::move(E));
2012 else
2013 Err = std::move(E);
2014 }
2015 assert(ModuleToDefinedGVSummaries.count(ModulePath));
2016 const GVSummaryMapTy &DefinedGlobals =
2017 ModuleToDefinedGVSummaries.find(ModulePath)->second;
2018
2019 // DTLTO needs the per-module LTO cache key to probe the cache.
2020 if (Conf.GetCacheKeyOutputString) {
2021 std::string &CacheKey = Conf.GetCacheKeyOutputString(Task);
2022 CacheKey = computeLTOCacheKey(
2023 Conf, CombinedIndex, ModulePath, ImportList, ExportList,
2024 ResolvedODR, DefinedGlobals, CfiFunctionDefs, CfiFunctionDecls);
2025 }
2026 },
2027 Task, ModulePath, ImportList, ExportList, ResolvedODR, OldPrefix,
2028 NewPrefix);
2029
2030 if (OnWrite)
2031 OnWrite(std::string(ModulePath));
2032 return Error::success();
2033 }
2034
2035 bool isSensitiveToInputOrder() override {
2036 // The order which modules are written to LinkedObjectsFile should be
2037 // deterministic and match the order they are passed on the command line.
2038 return true;
2039 }
2040};
2041} // end anonymous namespace
2042
2044 ThreadPoolStrategy Parallelism, std::string OldPrefix,
2045 std::string NewPrefix, std::string NativeObjectPrefix,
2046 bool ShouldEmitImportsFiles, raw_fd_ostream *LinkedObjectsFile,
2047 IndexWriteCallback OnWrite) {
2048 auto Func =
2049 [=](const Config &Conf, ModuleSummaryIndex &CombinedIndex,
2050 const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
2051 AddStreamFn AddStream, FileCache Cache,
2052 ArrayRef<StringRef> BitcodeLibFuncs) {
2053 return std::make_unique<WriteIndexesThinBackend>(
2054 Conf, CombinedIndex, Parallelism, ModuleToDefinedGVSummaries,
2055 OldPrefix, NewPrefix, NativeObjectPrefix, ShouldEmitImportsFiles,
2056 LinkedObjectsFile, OnWrite);
2057 };
2058 return ThinBackend(Func, Parallelism);
2059}
2060
2061Error LTO::runThinLTO(AddStreamFn AddStream, FileCache Cache,
2062 const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols) {
2063 llvm::TimeTraceScope timeScope("Run ThinLTO");
2064 LLVM_DEBUG(dbgs() << "Running ThinLTO\n");
2066 timeTraceProfilerBegin("ThinLink", StringRef(""));
2067 llvm::scope_exit TimeTraceScopeExit([]() {
2070 });
2071 if (ThinLTO.ModuleMap.empty())
2072 return Error::success();
2073
2075 llvm::errs() << "warning: [ThinLTO] No module compiled\n";
2076 return Error::success();
2077 }
2078
2079 if (Conf.CombinedIndexHook &&
2080 !Conf.CombinedIndexHook(ThinLTO.CombinedIndex, GUIDPreservedSymbols))
2081 return Error::success();
2082
2083 // Collect for each module the list of function it defines (GUID ->
2084 // Summary).
2085 DenseMap<StringRef, GVSummaryMapTy> ModuleToDefinedGVSummaries(
2086 ThinLTO.ModuleMap.size());
2087 ThinLTO.CombinedIndex.collectDefinedGVSummariesPerModule(
2088 ModuleToDefinedGVSummaries);
2089 // Create entries for any modules that didn't have any GV summaries
2090 // (either they didn't have any GVs to start with, or we suppressed
2091 // generation of the summaries because they e.g. had inline assembly
2092 // uses that couldn't be promoted/renamed on export). This is so
2093 // InProcessThinBackend::start can still launch a backend thread, which
2094 // is passed the map of summaries for the module, without any special
2095 // handling for this case.
2096 for (auto &Mod : ThinLTO.ModuleMap)
2097 if (!ModuleToDefinedGVSummaries.count(Mod.first))
2098 ModuleToDefinedGVSummaries.try_emplace(Mod.first);
2099
2100 FunctionImporter::ImportListsTy ImportLists(ThinLTO.ModuleMap.size());
2101 DenseMap<StringRef, FunctionImporter::ExportSetTy> ExportLists(
2102 ThinLTO.ModuleMap.size());
2103 StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>> ResolvedODR;
2104
2105 if (DumpThinCGSCCs)
2106 ThinLTO.CombinedIndex.dumpSCCs(outs());
2107
2108 std::set<GlobalValue::GUID> ExportedGUIDs;
2109
2110 bool WholeProgramVisibilityEnabledInLTO =
2111 Conf.HasWholeProgramVisibility &&
2112 // If validation is enabled, upgrade visibility only when all vtables
2113 // have typeinfos.
2114 (!Conf.ValidateAllVtablesHaveTypeInfos || Conf.AllVtablesHaveTypeInfos);
2115 if (hasWholeProgramVisibility(WholeProgramVisibilityEnabledInLTO))
2116 ThinLTO.CombinedIndex.setWithWholeProgramVisibility();
2117
2118 // If we're validating, get the vtable symbols that should not be
2119 // upgraded because they correspond to typeIDs outside of index-based
2120 // WPD info.
2121 DenseSet<GlobalValue::GUID> VisibleToRegularObjSymbols;
2122 if (WholeProgramVisibilityEnabledInLTO &&
2123 Conf.ValidateAllVtablesHaveTypeInfos) {
2124 // This returns true when the name is local or not defined. Locals are
2125 // expected to be handled separately.
2126 auto IsVisibleToRegularObj = [&](StringRef name) {
2127 auto It = GlobalResolutions->find(name);
2128 return (It == GlobalResolutions->end() ||
2129 It->second.VisibleOutsideSummary || !It->second.Prevailing);
2130 };
2131
2133 VisibleToRegularObjSymbols,
2134 IsVisibleToRegularObj);
2135 }
2136
2137 // If allowed, upgrade public vcall visibility to linkage unit visibility in
2138 // the summaries before whole program devirtualization below.
2140 ThinLTO.CombinedIndex, WholeProgramVisibilityEnabledInLTO,
2141 DynamicExportSymbols, VisibleToRegularObjSymbols);
2142
2143 // Perform index-based WPD. This will return immediately if there are
2144 // no index entries in the typeIdMetadata map (e.g. if we are instead
2145 // performing IR-based WPD in hybrid regular/thin LTO mode).
2146 std::map<ValueInfo, std::vector<VTableSlotSummary>> LocalWPDTargetsMap;
2147 DenseSet<StringRef> ExternallyVisibleSymbolNames;
2148
2149 // Used by the promotion-time renaming logic. When non-null, this set
2150 // identifies symbols that should not be renamed during promotion.
2151 // It is non-null only when whole-program visibility is enabled and
2152 // renaming is not forced. Otherwise, the default renaming behavior applies.
2153 DenseSet<StringRef> *ExternallyVisibleSymbolNamesPtr =
2154 (WholeProgramVisibilityEnabledInLTO && !AlwaysRenamePromotedLocals)
2155 ? &ExternallyVisibleSymbolNames
2156 : nullptr;
2157 runWholeProgramDevirtOnIndex(ThinLTO.CombinedIndex, ExportedGUIDs,
2158 LocalWPDTargetsMap,
2159 ExternallyVisibleSymbolNamesPtr);
2160
2161 auto isPrevailing = [&](GlobalValue::GUID GUID, const GlobalValueSummary *S) {
2162 return ThinLTO.isPrevailingModuleForGUID(GUID, S->modulePath());
2163 };
2165 MemProfContextDisambiguation ContextDisambiguation;
2166 ContextDisambiguation.run(
2167 ThinLTO.CombinedIndex, isPrevailing, RegularLTO.Ctx,
2168 [&](StringRef PassName, StringRef RemarkName, const Twine &Msg) {
2169 auto R = OptimizationRemark(PassName.data(), RemarkName,
2170 LinkerRemarkFunction);
2171 R << Msg.str();
2172 emitRemark(R);
2173 });
2174 }
2175
2176 // Figure out which symbols need to be internalized. This also needs to happen
2177 // at -O0 because summary-based DCE is implemented using internalization, and
2178 // we must apply DCE consistently with the full LTO module in order to avoid
2179 // undefined references during the final link.
2180 for (auto &Res : *GlobalResolutions) {
2181 // If the symbol does not have external references or it is not prevailing,
2182 // then not need to mark it as exported from a ThinLTO partition.
2183 if (Res.second.Partition != GlobalResolution::External ||
2184 !Res.second.isPrevailingIRSymbol())
2185 continue;
2186 auto GUID = Res.second.getGUID();
2187 // Mark exported unless index-based analysis determined it to be dead.
2188 if (ThinLTO.CombinedIndex.isGUIDLive(GUID))
2189 ExportedGUIDs.insert(GUID);
2190 }
2191
2192 // Reset the GlobalResolutions to deallocate the associated memory, as there
2193 // are no further accesses. We specifically want to do this before computing
2194 // cross module importing, which adds to peak memory via the computed import
2195 // and export lists.
2196 releaseGlobalResolutionsMemory();
2197
2198 if (Conf.OptLevel > 0)
2199 ComputeCrossModuleImport(ThinLTO.CombinedIndex, ModuleToDefinedGVSummaries,
2200 isPrevailing, ImportLists, ExportLists);
2201
2202 // Any functions referenced by the jump table in the regular LTO object must
2203 // be exported.
2204 auto Defs = ThinLTO.CombinedIndex.cfiFunctionDefs().getExportedThinLTOGUIDs();
2205 ExportedGUIDs.insert(Defs.begin(), Defs.end());
2206 auto Decls =
2207 ThinLTO.CombinedIndex.cfiFunctionDecls().getExportedThinLTOGUIDs();
2208 ExportedGUIDs.insert(Decls.begin(), Decls.end());
2209
2210 auto isExported = [&](StringRef ModuleIdentifier, ValueInfo VI) {
2211 const auto &ExportList = ExportLists.find(ModuleIdentifier);
2212 return (ExportList != ExportLists.end() && ExportList->second.count(VI)) ||
2213 ExportedGUIDs.count(VI.getGUID());
2214 };
2215
2216 // Update local devirtualized targets that were exported by cross-module
2217 // importing or by other devirtualizations marked in the ExportedGUIDs set.
2218 updateIndexWPDForExports(ThinLTO.CombinedIndex, isExported,
2219 LocalWPDTargetsMap, ExternallyVisibleSymbolNamesPtr);
2220
2221 if (ExternallyVisibleSymbolNamesPtr) {
2222 // Add to ExternallyVisibleSymbolNames the set of unique names used by all
2223 // externally visible symbols in the index.
2224 for (auto &I : ThinLTO.CombinedIndex) {
2225 ValueInfo VI = ThinLTO.CombinedIndex.getValueInfo(I);
2226 for (const auto &Summary : VI.getSummaryList()) {
2227 const GlobalValueSummary *Base = Summary->getBaseObject();
2228 if (GlobalValue::isLocalLinkage(Base->linkage()))
2229 continue;
2230
2231 ExternallyVisibleSymbolNamesPtr->insert(VI.name());
2232 break;
2233 }
2234 }
2235 }
2236
2237 thinLTOInternalizeAndPromoteInIndex(ThinLTO.CombinedIndex, isExported,
2238 isPrevailing,
2239 ExternallyVisibleSymbolNamesPtr);
2240
2241 auto recordNewLinkage = [&](StringRef ModuleIdentifier,
2243 GlobalValue::LinkageTypes NewLinkage) {
2244 ResolvedODR[ModuleIdentifier][GUID] = NewLinkage;
2245 };
2246 thinLTOResolvePrevailingInIndex(Conf, ThinLTO.CombinedIndex, isPrevailing,
2247 recordNewLinkage, GUIDPreservedSymbols);
2248
2249 thinLTOPropagateFunctionAttrs(ThinLTO.CombinedIndex, isPrevailing);
2250
2251 generateParamAccessSummary(ThinLTO.CombinedIndex);
2252
2255
2256 TimeTraceScopeExit.release();
2257
2258 auto &ModuleMap =
2259 ThinLTO.ModulesToCompile ? *ThinLTO.ModulesToCompile : ThinLTO.ModuleMap;
2260
2261 auto RunBackends = [&](ThinBackendProc *BackendProcess) -> Error {
2262 auto ProcessOneModule = [&](int I) -> Error {
2263 auto &Mod = *(ModuleMap.begin() + I);
2264 // Tasks 0 through ParallelCodeGenParallelismLevel-1 are reserved for
2265 // combined module and parallel code generation partitions.
2266 return BackendProcess->start(
2267 RegularLTO.ParallelCodeGenParallelismLevel + I, Mod.second,
2268 ImportLists[Mod.first], ExportLists[Mod.first],
2269 ResolvedODR[Mod.first], ThinLTO.ModuleMap);
2270 };
2271
2272 BackendProcess->setup(ModuleMap.size(),
2273 RegularLTO.ParallelCodeGenParallelismLevel,
2274 RegularLTO.CombinedModule->getTargetTriple());
2275
2276 if (BackendProcess->getThreadCount() == 1 ||
2277 BackendProcess->isSensitiveToInputOrder()) {
2278 // Process the modules in the order they were provided on the
2279 // command-line. It is important for this codepath to be used for
2280 // WriteIndexesThinBackend, to ensure the emitted LinkedObjectsFile lists
2281 // ThinLTO objects in the same order as the inputs, which otherwise would
2282 // affect the final link order.
2283 for (int I = 0, E = ModuleMap.size(); I != E; ++I)
2284 if (Error E = ProcessOneModule(I))
2285 return E;
2286 } else {
2287 // When executing in parallel, process largest bitsize modules first to
2288 // improve parallelism, and avoid starving the thread pool near the end.
2289 // This saves about 15 sec on a 36-core machine while link `clang.exe`
2290 // (out of 100 sec).
2291 std::vector<BitcodeModule *> ModulesVec;
2292 ModulesVec.reserve(ModuleMap.size());
2293 for (auto &Mod : ModuleMap)
2294 ModulesVec.push_back(&Mod.second);
2295 for (int I : generateModulesOrdering(ModulesVec))
2296 if (Error E = ProcessOneModule(I))
2297 return E;
2298 }
2299 return BackendProcess->wait();
2300 };
2301
2303 std::unique_ptr<ThinBackendProc> BackendProc =
2304 ThinLTO.Backend(Conf, ThinLTO.CombinedIndex, ModuleToDefinedGVSummaries,
2305 AddStream, Cache, BitcodeLibFuncs);
2306 return RunBackends(BackendProc.get());
2307 }
2308
2309 // Perform two rounds of code generation for ThinLTO:
2310 // 1. First round: Perform optimization and code generation, outputting to
2311 // temporary scratch objects.
2312 // 2. Merge code generation data extracted from the temporary scratch objects.
2313 // 3. Second round: Execute code generation again using the merged data.
2314 LLVM_DEBUG(dbgs() << "[TwoRounds] Initializing ThinLTO two-codegen rounds\n");
2315
2316 unsigned MaxTasks = getMaxTasks();
2317 auto Parallelism = ThinLTO.Backend.getParallelism();
2318 // Set up two additional streams and caches for storing temporary scratch
2319 // objects and optimized IRs, using the same cache directory as the original.
2320 cgdata::StreamCacheData CG(MaxTasks, Cache, "CG"), IR(MaxTasks, Cache, "IR");
2321
2322 // First round: Execute optimization and code generation, outputting to
2323 // temporary scratch objects. Serialize the optimized IRs before initiating
2324 // code generation.
2325 LLVM_DEBUG(dbgs() << "[TwoRounds] Running the first round of codegen\n");
2326 auto FirstRoundLTO = std::make_unique<FirstRoundThinBackend>(
2327 Conf, ThinLTO.CombinedIndex, Parallelism, ModuleToDefinedGVSummaries,
2328 CG.AddStream, CG.Cache, BitcodeLibFuncs, IR.AddStream, IR.Cache);
2329 if (Error E = RunBackends(FirstRoundLTO.get()))
2330 return E;
2331
2332 LLVM_DEBUG(dbgs() << "[TwoRounds] Merging codegen data\n");
2333 auto CombinedHashOrErr = cgdata::mergeCodeGenData(*CG.getResult());
2334 if (Error E = CombinedHashOrErr.takeError())
2335 return E;
2336 auto CombinedHash = *CombinedHashOrErr;
2337 LLVM_DEBUG(dbgs() << "[TwoRounds] CGData hash: " << CombinedHash << "\n");
2338
2339 // Second round: Read the optimized IRs and execute code generation using the
2340 // merged data.
2341 LLVM_DEBUG(dbgs() << "[TwoRounds] Running the second round of codegen\n");
2342 auto SecondRoundLTO = std::make_unique<SecondRoundThinBackend>(
2343 Conf, ThinLTO.CombinedIndex, Parallelism, ModuleToDefinedGVSummaries,
2344 AddStream, Cache, BitcodeLibFuncs, IR.getResult(), CombinedHash);
2345 return RunBackends(SecondRoundLTO.get());
2346}
2347
2351 std::optional<uint64_t> RemarksHotnessThreshold, int Count) {
2352 std::string Filename = std::string(RemarksFilename);
2353 // For ThinLTO, file.opt.<format> becomes
2354 // file.opt.<format>.thin.<num>.<format>.
2355 if (!Filename.empty() && Count != -1)
2356 Filename =
2357 (Twine(Filename) + ".thin." + llvm::utostr(Count) + "." + RemarksFormat)
2358 .str();
2359
2360 auto ResultOrErr = llvm::setupLLVMOptimizationRemarks(
2363 if (Error E = ResultOrErr.takeError())
2364 return std::move(E);
2365
2366 if (*ResultOrErr)
2367 (*ResultOrErr)->keep();
2368
2369 return ResultOrErr;
2370}
2371
2374 // Setup output file to emit statistics.
2375 if (StatsFilename.empty())
2376 return nullptr;
2377
2379 std::error_code EC;
2380 auto StatsFile =
2381 std::make_unique<ToolOutputFile>(StatsFilename, EC, sys::fs::OF_None);
2382 if (EC)
2383 return errorCodeToError(EC);
2384
2385 StatsFile->keep();
2386 return std::move(StatsFile);
2387}
2388
2389// Compute the ordering we will process the inputs: the rough heuristic here
2390// is to sort them per size so that the largest module get schedule as soon as
2391// possible. This is purely a compile-time optimization.
2393 auto Seq = llvm::seq<int>(0, R.size());
2394 std::vector<int> ModulesOrdering(Seq.begin(), Seq.end());
2395 llvm::sort(ModulesOrdering, [&](int LeftIndex, int RightIndex) {
2396 auto LSize = R[LeftIndex]->getBuffer().size();
2397 auto RSize = R[RightIndex]->getBuffer().size();
2398 return LSize > RSize;
2399 });
2400 return ModulesOrdering;
2401}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static cl::opt< ITMode > IT(cl::desc("IT block support"), cl::Hidden, cl::init(DefaultIT), cl::values(clEnumValN(DefaultIT, "arm-default-it", "Generate any type of IT block"), clEnumValN(RestrictedIT, "arm-restrict-it", "Disallow complex IT blocks")))
Function Alias Analysis false
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
dxil translate DXIL Translate Metadata
#define DEBUG_TYPE
This file supports working with JSON data.
static void writeToResolutionFile(raw_ostream &OS, InputFile *Input, ArrayRef< SymbolResolution > Res)
Definition LTO.cpp:805
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:408
static void handleNonPrevailingComdat(GlobalValue &GV, std::set< const Comdat * > &NonPrevailingComdats)
Definition LTO.cpp:941
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:516
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 ...
const char * Msg
static const char * name
This file defines the make_scope_exit function, which executes user-defined cleanup logic at scope ex...
This file defines the SmallSet class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
This file contains some functions that are useful when dealing with strings.
#define LLVM_DEBUG(...)
Definition Debug.h:119
This pass exposes codegen information to IR-level passes.
static const char PassName[]
The Input class is used to parse a yaml document into in-memory structs and vectors.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
iterator end() const
Definition ArrayRef.h:130
iterator begin() const
Definition ArrayRef.h:129
bool empty() const
Check if the array is empty.
Definition ArrayRef.h:136
const T & consume_front()
consume_front() - Returns the first element and drops it from ArrayRef.
Definition ArrayRef.h:156
static LLVM_ABI ArrayType * get(Type *ElementType, uint64_t NumElements)
This static method is the primary way to construct an ArrayType.
Represents a module in a bitcode file.
StringRef getModuleIdentifier() const
LLVM_ABI Expected< std::unique_ptr< Module > > parseModule(LLVMContext &Context, ParserCallbacks Callbacks={})
Read the entire bitcode module and return it.
LLVM_ABI Error readSummary(ModuleSummaryIndex &CombinedIndex, StringRef ModulePath, std::function< bool(StringRef)> IsPrevailing=nullptr, std::function< void(ValueInfo)> OnValueInfo=nullptr)
Parse the specified bitcode buffer and merge its module summary index into CombinedIndex.
LLVM_ABI Expected< std::unique_ptr< Module > > getLazyModule(LLVMContext &Context, bool ShouldLazyLoadMetadata, bool IsImporting, ParserCallbacks Callbacks={})
Read the bitcode module and prepare for lazy deserialization of function bodies.
auto getExportedThinLTOGUIDs() const
get the set of GUIDs that should also be exported because they are the GUIDs of the cfi functions enc...
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:223
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&...Args)
Definition DenseMap.h:299
iterator begin()
Definition DenseMap.h:137
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:219
iterator end()
Definition DenseMap.h:141
Implements a dense probed hash-table based set.
Definition DenseSet.h:281
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
Tagged union holding either a T or a Error.
Definition Error.h:485
Error takeError()
Take ownership of the stored error.
Definition Error.h:612
The map maintains the list of imports.
DenseSet< ValueInfo > ExportSetTy
The set contains an entry for every global value that the module exports.
static LLVM_ABI FunctionType * get(Type *Result, ArrayRef< Type * > Params, bool isVarArg)
This static method is the primary way of constructing a FunctionType.
static Function * Create(FunctionType *Ty, LinkageTypes Linkage, unsigned AddrSpace, const Twine &N="", Module *M=nullptr)
Definition Function.h:168
Function and variable summary information to aid decisions and implementation of importing.
static bool isAppendingLinkage(LinkageTypes Linkage)
static LLVM_ABI GUID getGUIDAssumingExternalLinkage(StringRef GlobalName)
Return a 64-bit global unique ID constructed from the name of a global symbol.
Definition Globals.cpp:80
static bool isExternalWeakLinkage(LinkageTypes Linkage)
static bool isLocalLinkage(LinkageTypes Linkage)
LLVM_ABI bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition Globals.cpp:408
void setUnnamedAddr(UnnamedAddr Val)
uint64_t GUID
Declare a type to represent a global unique identifier for a global value.
bool hasLocalLinkage() const
LLVM_ABI GUID getGUIDOrFallback() const
Return the GUID for this value if it has been assigned, otherwise fall back to computing it based on ...
Definition Globals.cpp:110
LLVM_ABI const Comdat * getComdat() const
Definition Globals.cpp:274
static bool isLinkOnceLinkage(LinkageTypes Linkage)
void setLinkage(LinkageTypes LT)
DLLStorageClassTypes
Storage classes of global values for PE targets.
Definition GlobalValue.h:74
static bool isExternalLinkage(LinkageTypes Linkage)
VisibilityTypes
An enumeration for the kinds of visibility of global values.
Definition GlobalValue.h:67
@ DefaultVisibility
The GV is visible.
Definition GlobalValue.h:68
static LLVM_ABI std::string getGlobalIdentifier(StringRef Name, GlobalValue::LinkageTypes Linkage, StringRef FileName)
Return the modified name for a global value suitable to be used as the key for a global lookup (e....
Definition Globals.cpp:234
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:640
LLVM_ABI void eraseFromParent()
eraseFromParent - This method unlinks 'this' from the containing module and deletes it.
Definition Globals.cpp:609
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:1511
This class implements a map that also provides access to all stored values in a deterministic order.
Definition MapVector.h:38
iterator begin()
Definition MapVector.h:67
bool empty() const
Definition MapVector.h:79
size_type size() const
Definition MapVector.h:58
LLVM_ABI PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
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 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:888
iterator end()
Definition StringMap.h:213
iterator find(StringRef Key)
Definition StringMap.h:226
size_type count(StringRef Key) const
count - Return 1 if the element is in the map, 0 otherwise.
Definition StringMap.h:274
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
constexpr bool empty() const
Check if the string is empty.
Definition StringRef.h:141
constexpr const char * data() const
Get a pointer to the start of the string (which may not be null terminated).
Definition StringRef.h:138
Saves strings in the provided stable storage and returns a StringRef with a stable character pointer.
Definition StringSaver.h:22
StringRef save(const char *S)
Definition StringSaver.h:31
Implementation of the target library information.
Provides information about what library functions are available for the current target.
bool has(LibFunc F) const
Tests whether a library function is available.
bool getLibFunc(StringRef funcName, LibFunc &F) const
Searches for a particular function name.
StringRef getName(LibFunc F) const
MCTargetOptions MCOptions
Machine level options.
DebuggerKind DebuggerTuning
Which debugger to tune for.
unsigned FunctionSections
Emit functions into separate sections.
unsigned DataSections
Emit data into separate sections.
This tells how a thread pool will be used.
Definition Threading.h:115
The TimeTraceScope is a helper class to call the begin and end functions of the time trace profiler.
Triple - Helper class for working with autoconf configuration names.
Definition Triple.h:48
bool isArm64e() const
Tests whether the target is the Apple "arm64e" AArch64 subarch.
Definition Triple.h:1217
ArchType getArch() const
Get the parsed architecture type of this triple.
Definition Triple.h:512
bool isOSDarwin() const
Is this a "Darwin" OS (macOS, iOS, tvOS, watchOS, DriverKit, XROS, or bridgeOS).
Definition Triple.h:721
bool isOSBinFormatELF() const
Tests whether the OS uses the ELF binary format.
Definition Triple.h:864
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
static LLVM_ABI Type * getVoidTy(LLVMContext &C)
Definition Type.cpp:282
static LLVM_ABI IntegerType * getInt8Ty(LLVMContext &C)
Definition Type.cpp:307
LLVM_ABI void setName(const Twine &Name)
Change the name of the value.
Definition Value.cpp:394
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:346
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:319
LLVM_ABI void takeName(Value *V)
Transfer the name from V to this value.
Definition Value.cpp:400
std::pair< iterator, bool > insert(const ValueT &V)
Definition DenseSet.h:209
bool contains(const_arg_type_t< ValueT > V) const
Check if the set contains the given element.
Definition DenseSet.h:182
void insert_range(Range &&R)
Definition DenseSet.h:235
size_type size() const
Definition DenseSet.h:84
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:187
iterator find(const_arg_type_t< ValueT > V)
Definition DenseSet.h:174
An efficient, type-erasing, non-owning reference to a callable.
Ephemeral symbols produced by Reader::symbols() and Reader::module_symbols().
Definition IRSymtab.h:318
An input file.
Definition LTO.h:115
LLVM_ABI BitcodeModule & getPrimaryBitcodeModule()
Definition LTO.cpp:679
static LLVM_ABI Expected< std::unique_ptr< InputFile > > create(MemoryBufferRef Object)
Create an InputFile.
Definition LTO.cpp:630
ArrayRef< Symbol > symbols() const
A range over the symbols in this InputFile.
Definition LTO.h:188
LLVM_ABI StringRef getName() const
Returns the path to the InputFile.
Definition LTO.cpp:670
LLVM_ABI BitcodeModule & getSingleBitcodeModule()
Definition LTO.cpp:674
LTO(Config Conf, ThinBackend Backend={}, unsigned ParallelCodeGenParallelismLevel=1, LTOKind LTOMode=LTOK_Default)
Create an LTO object.
Definition LTO.cpp:694
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:829
struct llvm::lto::LTO::RegularLTOState RegularLTO
virtual void cleanup()
Definition LTO.cpp:711
static 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:1513
virtual Expected< std::shared_ptr< lto::InputFile > > addInput(std::unique_ptr< lto::InputFile > InputPtr)
Definition LTO.h:683
Config Conf
Definition LTO.h:466
void setBitcodeLibFuncs(ArrayRef< StringRef > BitcodeLibFuncs)
Set the list of functions implemented in bitcode that were not extracted from an archive.
Definition LTO.cpp:860
LTOKind
Unified LTO modes.
Definition LTO.h:396
@ LTOK_UnifiedRegular
Regular LTO, with Unified LTO enabled.
Definition LTO.h:401
@ LTOK_Default
Any LTO mode without Unified LTO. The default mode.
Definition LTO.h:398
@ LTOK_UnifiedThin
ThinLTO, with Unified LTO enabled.
Definition LTO.h:404
virtual ~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
struct llvm::lto::LTO::ThinLTOState ThinLTO
LTOKind LTOMode
Definition LTO.h:647
unsigned getMaxTasks() const
Returns an upper bound on the number of tasks that the client may expect.
Definition LTO.cpp:1272
virtual Error run(AddStreamFn AddStream, FileCache Cache={})
Runs the LTO pipeline.
Definition LTO.cpp:1323
static SmallVector< StringRef > getLibFuncSymbols(const Triple &TT, llvm::StringSaver &Saver)
Static method that returns a list of library function symbols that can be generated by LTO but might ...
Definition LTO.cpp:1526
This class defines the interface to the ThinLTO backend.
Definition LTO.h:250
const Config & Conf
Definition LTO.h:252
const DenseMap< StringRef, GVSummaryMapTy > & ModuleToDefinedGVSummaries
Definition LTO.h:254
ModuleSummaryIndex & CombinedIndex
Definition LTO.h:253
LLVM_ABI Error emitFiles(const FunctionImporter::ImportMapTy &ImportList, unsigned Task, StringRef ModulePath, const std::string &NewModulePath) const
Definition LTO.cpp:1540
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
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
@ 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:1901
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:1936
LLVM_ABI Error thinBackend(const Config &C, unsigned Task, AddStreamFn AddStream, Module &M, const ModuleSummaryIndex &CombinedIndex, const FunctionImporter::ImportMapTy &ImportList, const GVSummaryMapTy &DefinedGlobals, MapVector< StringRef, BitcodeModule > *ModuleMap, bool CodeGenOnly, ArrayRef< StringRef > BitcodeLibFuncs, AddStreamFn IRAddStream=nullptr, const std::vector< uint8_t > &CmdArgs=std::vector< uint8_t >())
Runs a ThinLTO backend.
LLVM_ABI StringLiteral getThinLTODefaultCPU(const Triple &TheTriple)
Definition LTO.cpp:1918
LLVM_ABI Expected< std::unique_ptr< ToolOutputFile > > setupStatsFile(StringRef StatsFilename)
Setups the output file for saving statistics.
Definition LTO.cpp:2373
LLVM_ABI Error backend(const Config &C, AddStreamFn AddStream, unsigned ParallelCodeGenParallelismLevel, Module &M, ModuleSummaryIndex &CombinedIndex, ArrayRef< StringRef > BitcodeLibFuncs)
Runs a regular LTO backend.
std::function< void(const std::string &)> IndexWriteCallback
Definition LTO.h:245
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:2043
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:2348
LLVM_ABI std::vector< int > generateModulesOrdering(ArrayRef< BitcodeModule * > R)
Produces a container ordering for optimal multi-threaded processing.
Definition LTO.cpp:2392
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 create_directories(const Twine &path, bool IgnoreExisting=true, perms Perms=owner_all|group_all)
Create all the non-existent directories in path.
Definition Path.cpp:993
LLVM_ABI StringRef parent_path(StringRef path LLVM_LIFETIME_BOUND, Style style=Style::native)
Get parent path.
Definition Path.cpp:478
LLVM_ABI bool replace_path_prefix(SmallVectorImpl< char > &Path, StringRef OldPrefix, StringRef NewPrefix, Style style=Style::native)
Replace matching path prefix with another path.
Definition Path.cpp:529
This is an optimization pass for GlobalISel generic memory operations.
ThreadPoolStrategy heavyweight_hardware_concurrency(unsigned ThreadCount=0)
Returns a thread strategy for tasks requiring significant memory or other resources.
Definition Threading.h:167
detail::zippy< detail::zip_shortest, T, U, Args... > zip(T &&t, U &&u, Args &&...args)
zip iterator for two or more iteratable types.
Definition STLExtras.h:830
cl::opt< std::string > RemarksFormat("lto-pass-remarks-format", cl::desc("The format used for serializing remarks (default: YAML)"), cl::value_desc("format"), cl::init("yaml"))
LLVM_ABI void runWholeProgramDevirtOnIndex(ModuleSummaryIndex &Summary, std::set< GlobalValue::GUID > &ExportedGUIDs, std::map< ValueInfo, std::vector< VTableSlotSummary > > &LocalWPDTargetsMap, DenseSet< StringRef > *ExternallyVisibleSymbolNamesPtr=nullptr)
Perform index-based whole program devirtualization on the Summary index.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1739
Error createFileError(const Twine &F, Error E)
Concatenate a source file path and/or name with an Error.
Definition Error.h:1415
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
LLVM_ABI void generateParamAccessSummary(ModuleSummaryIndex &Index)
cl::opt< bool > CodeGenDataThinLTOTwoRounds("codegen-data-thinlto-two-rounds", cl::init(false), cl::Hidden, cl::desc("Enable two-round ThinLTO code generation. The first round " "emits codegen data, while the second round uses the emitted " "codegen data for further optimizations."))
Definition LTO.cpp:112
LLVM_ABI Expected< LLVMRemarkFileHandle > setupLLVMOptimizationRemarks(LLVMContext &Context, StringRef RemarksFilename, StringRef RemarksPasses, StringRef RemarksFormat, bool RemarksWithHotness, std::optional< uint64_t > RemarksHotnessThreshold=0)
Set up optimization remarks that output to a file.
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.
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.
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:394
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
cl::opt< bool > AlwaysRenamePromotedLocals("always-rename-promoted-locals", cl::init(true), cl::Hidden, cl::desc("Always rename promoted locals."))
Definition LTO.cpp:114
bool timeTraceProfilerEnabled()
Is the time trace profiler enabled, i.e. initialized?
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
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:494
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.
cl::opt< bool > EnableMemProfContextDisambiguation
Enable MemProf context disambiguation for thin link.
cl::opt< bool > ForceImportAll
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Count
Definition InstrProf.h:145
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
constexpr auto seq(T Begin, T End)
Iterate over an integral type from Begin up to - but not including - End.
Definition Sequence.h:341
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.
SmallPtrSet< GlobalValueSummary *, 0 > GVSummaryPtrSet
A set of global value summary pointers.
std::function< Expected< std::unique_ptr< CachedFileStream > >( unsigned Task, const Twine &ModuleName)> AddStreamFn
This type defines the callback to add a file that is generated on the fly.
Definition Caching.h:58
LLVM_ABI void PrintStatisticsJSON(raw_ostream &OS)
Print statistics in JSON format.
void consumeError(Error Err)
Consume a Error without doing anything.
Definition Error.h:1106
LLVM_ABI void computeDeadSymbolsWithConstProp(ModuleSummaryIndex &Index, const DenseSet< GlobalValue::GUID > &GUIDPreservedSymbols, function_ref< PrevailingType(GlobalValue::GUID)> isPrevailing, bool ImportEnabled)
Compute dead symbols and run constant propagation in combined index after that.
LLVM_ABI Error EmitImportsFiles(StringRef ModulePath, StringRef OutputFilename, const ModuleToSummariesForIndexTy &ModuleToSummariesForIndex)
Emit into OutputFilename the files module ModulePath will import from.
@ Keep
No function return thunk.
Definition CodeGen.h:162
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:612
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:878
This type represents a file cache system that manages caching of files.
Definition Caching.h:84
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
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.
Function object to check whether the first component of a container supported by std::get (like std::...
Definition STLExtras.h:1439
LTO configuration.
Definition Config.h:43
std::function< std::string &(size_t Task)> GetCacheKeyOutputString
Called by WriteIndexesThinBackend when it needs to store a bitcode module's cache key.
Definition Config.h:312
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:156
LLVM_ABI bool isLibcall(const TargetLibraryInfo &TLI, const RTLIB::RuntimeLibcallsInfo &Libcalls) const
Definition LTO.cpp:661
std::vector< AddedModule > ModsWithSummaries
Definition LTO.h:492
std::unique_ptr< IRMover > Mover
Definition LTO.h:482
unsigned ParallelCodeGenParallelismLevel
Definition LTO.h:479
std::map< std::string, CommonResolution > Commons
Definition LTO.h:477
std::unique_ptr< Module > CombinedModule
Definition LTO.h:481
LLVM_ABI RegularLTOState(unsigned ParallelCodeGenParallelismLevel, const Config &Conf)
Definition LTO.cpp:681
ModuleMapType ModuleMap
Definition LTO.h:504
LLVM_ABI ThinLTOState(ThinBackend Backend)
Definition LTO.cpp:687
std::optional< ModuleMapType > ModulesToCompile
Definition LTO.h:506
ModuleSummaryIndex CombinedIndex
Definition LTO.h:502
The resolution for a symbol.
Definition LTO.h:690
unsigned FinalDefinitionInLinkageUnit
The definition of this symbol is unpreemptable at runtime and is known to be in this linkage unit.
Definition LTO.h:700
unsigned ExportDynamic
The symbol was exported dynamically, and therefore could be referenced by a shared library not visibl...
Definition LTO.h:707
unsigned Prevailing
The linker has chosen this definition of the symbol.
Definition LTO.h:696
unsigned LinkerRedefined
Linker redefined version of the symbol which appeared in -wrap or -defsym linker option.
Definition LTO.h:711
unsigned VisibleToRegularObj
The definition of this symbol is visible outside of the LTO unit.
Definition LTO.h:703
This type defines the behavior following the thin-link phase during ThinLTO.
Definition LTO.h:320