LLVM 18.0.0git
FunctionImport.cpp
Go to the documentation of this file.
1//===- FunctionImport.cpp - ThinLTO Summary-based Function Import ---------===//
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 Function import based on summaries.
10//
11//===----------------------------------------------------------------------===//
12
14#include "llvm/ADT/ArrayRef.h"
15#include "llvm/ADT/STLExtras.h"
16#include "llvm/ADT/SetVector.h"
18#include "llvm/ADT/Statistic.h"
19#include "llvm/ADT/StringRef.h"
21#include "llvm/IR/AutoUpgrade.h"
22#include "llvm/IR/Constants.h"
23#include "llvm/IR/Function.h"
24#include "llvm/IR/GlobalAlias.h"
26#include "llvm/IR/GlobalValue.h"
28#include "llvm/IR/Metadata.h"
29#include "llvm/IR/Module.h"
32#include "llvm/Linker/IRMover.h"
35#include "llvm/Support/Debug.h"
36#include "llvm/Support/Errc.h"
37#include "llvm/Support/Error.h"
46#include <cassert>
47#include <memory>
48#include <set>
49#include <string>
50#include <system_error>
51#include <tuple>
52#include <utility>
53
54using namespace llvm;
55
56#define DEBUG_TYPE "function-import"
57
58STATISTIC(NumImportedFunctionsThinLink,
59 "Number of functions thin link decided to import");
60STATISTIC(NumImportedHotFunctionsThinLink,
61 "Number of hot functions thin link decided to import");
62STATISTIC(NumImportedCriticalFunctionsThinLink,
63 "Number of critical functions thin link decided to import");
64STATISTIC(NumImportedGlobalVarsThinLink,
65 "Number of global variables thin link decided to import");
66STATISTIC(NumImportedFunctions, "Number of functions imported in backend");
67STATISTIC(NumImportedGlobalVars,
68 "Number of global variables imported in backend");
69STATISTIC(NumImportedModules, "Number of modules imported from");
70STATISTIC(NumDeadSymbols, "Number of dead stripped symbols in index");
71STATISTIC(NumLiveSymbols, "Number of live symbols in index");
72
73/// Limit on instruction count of imported functions.
75 "import-instr-limit", cl::init(100), cl::Hidden, cl::value_desc("N"),
76 cl::desc("Only import functions with less than N instructions"));
77
79 "import-cutoff", cl::init(-1), cl::Hidden, cl::value_desc("N"),
80 cl::desc("Only import first N functions if N>=0 (default -1)"));
81
82static cl::opt<bool>
83 ForceImportAll("force-import-all", cl::init(false), cl::Hidden,
84 cl::desc("Import functions with noinline attribute"));
85
86static cl::opt<float>
87 ImportInstrFactor("import-instr-evolution-factor", cl::init(0.7),
89 cl::desc("As we import functions, multiply the "
90 "`import-instr-limit` threshold by this factor "
91 "before processing newly imported functions"));
92
94 "import-hot-evolution-factor", cl::init(1.0), cl::Hidden,
95 cl::value_desc("x"),
96 cl::desc("As we import functions called from hot callsite, multiply the "
97 "`import-instr-limit` threshold by this factor "
98 "before processing newly imported functions"));
99
101 "import-hot-multiplier", cl::init(10.0), cl::Hidden, cl::value_desc("x"),
102 cl::desc("Multiply the `import-instr-limit` threshold for hot callsites"));
103
105 "import-critical-multiplier", cl::init(100.0), cl::Hidden,
106 cl::value_desc("x"),
107 cl::desc(
108 "Multiply the `import-instr-limit` threshold for critical callsites"));
109
110// FIXME: This multiplier was not really tuned up.
112 "import-cold-multiplier", cl::init(0), cl::Hidden, cl::value_desc("N"),
113 cl::desc("Multiply the `import-instr-limit` threshold for cold callsites"));
114
115static cl::opt<bool> PrintImports("print-imports", cl::init(false), cl::Hidden,
116 cl::desc("Print imported functions"));
117
119 "print-import-failures", cl::init(false), cl::Hidden,
120 cl::desc("Print information for functions rejected for importing"));
121
122static cl::opt<bool> ComputeDead("compute-dead", cl::init(true), cl::Hidden,
123 cl::desc("Compute dead symbols"));
124
126 "enable-import-metadata", cl::init(false), cl::Hidden,
127 cl::desc("Enable import metadata like 'thinlto_src_module'"));
128
129/// Summary file to use for function importing when using -function-import from
130/// the command line.
132 SummaryFile("summary-file",
133 cl::desc("The summary file to use for function importing."));
134
135/// Used when testing importing from distributed indexes via opt
136// -function-import.
137static cl::opt<bool>
138 ImportAllIndex("import-all-index",
139 cl::desc("Import all external functions in index."));
140
141// Load lazily a module from \p FileName in \p Context.
142static std::unique_ptr<Module> loadFile(const std::string &FileName,
143 LLVMContext &Context) {
144 SMDiagnostic Err;
145 LLVM_DEBUG(dbgs() << "Loading '" << FileName << "'\n");
146 // Metadata isn't loaded until functions are imported, to minimize
147 // the memory overhead.
148 std::unique_ptr<Module> Result =
149 getLazyIRFileModule(FileName, Err, Context,
150 /* ShouldLazyLoadMetadata = */ true);
151 if (!Result) {
152 Err.print("function-import", errs());
153 report_fatal_error("Abort");
154 }
155
156 return Result;
157}
158
159/// Given a list of possible callee implementation for a call site, qualify the
160/// legality of importing each. The return is a range of pairs. Each pair
161/// corresponds to a candidate. The first value is the ImportFailureReason for
162/// that candidate, the second is the candidate.
165 ArrayRef<std::unique_ptr<GlobalValueSummary>> CalleeSummaryList,
166 StringRef CallerModulePath) {
167 return llvm::map_range(
168 CalleeSummaryList,
169 [&Index, CalleeSummaryList,
170 CallerModulePath](const std::unique_ptr<GlobalValueSummary> &SummaryPtr)
172 const GlobalValueSummary *> {
173 auto *GVSummary = SummaryPtr.get();
174 if (!Index.isGlobalValueLive(GVSummary))
175 return {FunctionImporter::ImportFailureReason::NotLive, GVSummary};
176
177 if (GlobalValue::isInterposableLinkage(GVSummary->linkage()))
178 return {FunctionImporter::ImportFailureReason::InterposableLinkage,
179 GVSummary};
180
181 auto *Summary = dyn_cast<FunctionSummary>(GVSummary->getBaseObject());
182
183 // Ignore any callees that aren't actually functions. This could happen
184 // in the case of GUID hash collisions. It could also happen in theory
185 // for SamplePGO profiles collected on old versions of the code after
186 // renaming, since we synthesize edges to any inlined callees appearing
187 // in the profile.
188 if (!Summary)
189 return {FunctionImporter::ImportFailureReason::GlobalVar, GVSummary};
190
191 // If this is a local function, make sure we import the copy
192 // in the caller's module. The only time a local function can
193 // share an entry in the index is if there is a local with the same name
194 // in another module that had the same source file name (in a different
195 // directory), where each was compiled in their own directory so there
196 // was not distinguishing path.
197 // However, do the import from another module if there is only one
198 // entry in the list - in that case this must be a reference due
199 // to indirect call profile data, since a function pointer can point to
200 // a local in another module.
201 if (GlobalValue::isLocalLinkage(Summary->linkage()) &&
202 CalleeSummaryList.size() > 1 &&
203 Summary->modulePath() != CallerModulePath)
204 return {
205 FunctionImporter::ImportFailureReason::LocalLinkageNotInModule,
206 GVSummary};
207
208 // Skip if it isn't legal to import (e.g. may reference unpromotable
209 // locals).
210 if (Summary->notEligibleToImport())
211 return {FunctionImporter::ImportFailureReason::NotEligible,
212 GVSummary};
213
214 return {FunctionImporter::ImportFailureReason::None, GVSummary};
215 });
216}
217
218/// Given a list of possible callee implementation for a call site, select one
219/// that fits the \p Threshold. If none are found, the Reason will give the last
220/// reason for the failure (last, in the order of CalleeSummaryList entries).
221///
222/// FIXME: select "best" instead of first that fits. But what is "best"?
223/// - The smallest: more likely to be inlined.
224/// - The one with the least outgoing edges (already well optimized).
225/// - One from a module already being imported from in order to reduce the
226/// number of source modules parsed/linked.
227/// - One that has PGO data attached.
228/// - [insert you fancy metric here]
229static const GlobalValueSummary *
231 ArrayRef<std::unique_ptr<GlobalValueSummary>> CalleeSummaryList,
232 unsigned Threshold, StringRef CallerModulePath,
234 auto QualifiedCandidates =
235 qualifyCalleeCandidates(Index, CalleeSummaryList, CallerModulePath);
236 for (auto QualifiedValue : QualifiedCandidates) {
237 Reason = QualifiedValue.first;
238 if (Reason != FunctionImporter::ImportFailureReason::None)
239 continue;
240 auto *Summary =
241 cast<FunctionSummary>(QualifiedValue.second->getBaseObject());
242
243 if ((Summary->instCount() > Threshold) && !Summary->fflags().AlwaysInline &&
245 Reason = FunctionImporter::ImportFailureReason::TooLarge;
246 continue;
247 }
248
249 // Don't bother importing if we can't inline it anyway.
250 if (Summary->fflags().NoInline && !ForceImportAll) {
251 Reason = FunctionImporter::ImportFailureReason::NoInline;
252 continue;
253 }
254
255 return Summary;
256 }
257 return nullptr;
258}
259
260namespace {
261
262using EdgeInfo = std::tuple<const FunctionSummary *, unsigned /* Threshold */>;
263
264} // anonymous namespace
265
266/// Import globals referenced by a function or other globals that are being
267/// imported, if importing such global is possible.
268class GlobalsImporter final {
270 const GVSummaryMapTy &DefinedGVSummaries;
272 IsPrevailing;
275
276 bool shouldImportGlobal(const ValueInfo &VI) {
277 const auto &GVS = DefinedGVSummaries.find(VI.getGUID());
278 if (GVS == DefinedGVSummaries.end())
279 return true;
280 // We should not skip import if the module contains a non-prevailing
281 // definition with interposable linkage type. This is required for
282 // correctness in the situation where there is a prevailing def available
283 // for import and marked read-only. In this case, the non-prevailing def
284 // will be converted to a declaration, while the prevailing one becomes
285 // internal, thus no definitions will be available for linking. In order to
286 // prevent undefined symbol link error, the prevailing definition must be
287 // imported.
288 // FIXME: Consider adding a check that the suitable prevailing definition
289 // exists and marked read-only.
290 if (VI.getSummaryList().size() > 1 &&
291 GlobalValue::isInterposableLinkage(GVS->second->linkage()) &&
292 !IsPrevailing(VI.getGUID(), GVS->second))
293 return true;
294
295 return false;
296 }
297
298 void
299 onImportingSummaryImpl(const GlobalValueSummary &Summary,
301 for (const auto &VI : Summary.refs()) {
302 if (!shouldImportGlobal(VI)) {
304 dbgs() << "Ref ignored! Target already in destination module.\n");
305 continue;
306 }
307
308 LLVM_DEBUG(dbgs() << " ref -> " << VI << "\n");
309
310 // If this is a local variable, make sure we import the copy
311 // in the caller's module. The only time a local variable can
312 // share an entry in the index is if there is a local with the same name
313 // in another module that had the same source file name (in a different
314 // directory), where each was compiled in their own directory so there
315 // was not distinguishing path.
316 auto LocalNotInModule =
317 [&](const GlobalValueSummary *RefSummary) -> bool {
318 return GlobalValue::isLocalLinkage(RefSummary->linkage()) &&
319 RefSummary->modulePath() != Summary.modulePath();
320 };
321
322 for (const auto &RefSummary : VI.getSummaryList()) {
323 const auto *GVS = dyn_cast<GlobalVarSummary>(RefSummary.get());
324 // Functions could be referenced by global vars - e.g. a vtable; but we
325 // don't currently imagine a reason those would be imported here, rather
326 // than as part of the logic deciding which functions to import (i.e.
327 // based on profile information). Should we decide to handle them here,
328 // we can refactor accordingly at that time.
329 if (!GVS || !Index.canImportGlobalVar(GVS, /* AnalyzeRefs */ true) ||
330 LocalNotInModule(GVS))
331 continue;
332 auto ILI = ImportList[RefSummary->modulePath()].insert(VI.getGUID());
333 // Only update stat and exports if we haven't already imported this
334 // variable.
335 if (!ILI.second)
336 break;
337 NumImportedGlobalVarsThinLink++;
338 // Any references made by this variable will be marked exported
339 // later, in ComputeCrossModuleImport, after import decisions are
340 // complete, which is more efficient than adding them here.
341 if (ExportLists)
342 (*ExportLists)[RefSummary->modulePath()].insert(VI);
343
344 // If variable is not writeonly we attempt to recursively analyze
345 // its references in order to import referenced constants.
346 if (!Index.isWriteOnly(GVS))
347 Worklist.emplace_back(GVS);
348 break;
349 }
350 }
351 }
352
353public:
355 const ModuleSummaryIndex &Index, const GVSummaryMapTy &DefinedGVSummaries,
357 IsPrevailing,
360 : Index(Index), DefinedGVSummaries(DefinedGVSummaries),
361 IsPrevailing(IsPrevailing), ImportList(ImportList),
362 ExportLists(ExportLists) {}
363
366 onImportingSummaryImpl(Summary, Worklist);
367 while (!Worklist.empty())
368 onImportingSummaryImpl(*Worklist.pop_back_val(), Worklist);
369 }
370};
371
372/// Determine the list of imports and exports for each module.
375 IsPrevailing;
378
379public:
382 IsPrevailing,
385 : IsPrevailing(IsPrevailing), Index(Index), ExportLists(ExportLists) {}
386
387 /// Given the list of globals defined in a module, compute the list of imports
388 /// as well as the list of "exports", i.e. the list of symbols referenced from
389 /// another module (that may require promotion).
390 void computeImportForModule(const GVSummaryMapTy &DefinedGVSummaries,
391 StringRef ModName,
393};
394
395static const char *
397 switch (Reason) {
398 case FunctionImporter::ImportFailureReason::None:
399 return "None";
400 case FunctionImporter::ImportFailureReason::GlobalVar:
401 return "GlobalVar";
402 case FunctionImporter::ImportFailureReason::NotLive:
403 return "NotLive";
404 case FunctionImporter::ImportFailureReason::TooLarge:
405 return "TooLarge";
406 case FunctionImporter::ImportFailureReason::InterposableLinkage:
407 return "InterposableLinkage";
408 case FunctionImporter::ImportFailureReason::LocalLinkageNotInModule:
409 return "LocalLinkageNotInModule";
410 case FunctionImporter::ImportFailureReason::NotEligible:
411 return "NotEligible";
412 case FunctionImporter::ImportFailureReason::NoInline:
413 return "NoInline";
414 }
415 llvm_unreachable("invalid reason");
416}
417
418/// Compute the list of functions to import for a given caller. Mark these
419/// imported functions and the symbols they reference in their source module as
420/// exported from their source module.
422 const FunctionSummary &Summary, const ModuleSummaryIndex &Index,
423 const unsigned Threshold, const GVSummaryMapTy &DefinedGVSummaries,
425 isPrevailing,
426 SmallVectorImpl<EdgeInfo> &Worklist, GlobalsImporter &GVImporter,
429 FunctionImporter::ImportThresholdsTy &ImportThresholds) {
430 GVImporter.onImportingSummary(Summary);
431 static int ImportCount = 0;
432 for (const auto &Edge : Summary.calls()) {
433 ValueInfo VI = Edge.first;
434 LLVM_DEBUG(dbgs() << " edge -> " << VI << " Threshold:" << Threshold
435 << "\n");
436
437 if (ImportCutoff >= 0 && ImportCount >= ImportCutoff) {
438 LLVM_DEBUG(dbgs() << "ignored! import-cutoff value of " << ImportCutoff
439 << " reached.\n");
440 continue;
441 }
442
443 if (DefinedGVSummaries.count(VI.getGUID())) {
444 // FIXME: Consider not skipping import if the module contains
445 // a non-prevailing def with interposable linkage. The prevailing copy
446 // can safely be imported (see shouldImportGlobal()).
447 LLVM_DEBUG(dbgs() << "ignored! Target already in destination module.\n");
448 continue;
449 }
450
451 auto GetBonusMultiplier = [](CalleeInfo::HotnessType Hotness) -> float {
452 if (Hotness == CalleeInfo::HotnessType::Hot)
453 return ImportHotMultiplier;
454 if (Hotness == CalleeInfo::HotnessType::Cold)
456 if (Hotness == CalleeInfo::HotnessType::Critical)
458 return 1.0;
459 };
460
461 const auto NewThreshold =
462 Threshold * GetBonusMultiplier(Edge.second.getHotness());
463
464 auto IT = ImportThresholds.insert(std::make_pair(
465 VI.getGUID(), std::make_tuple(NewThreshold, nullptr, nullptr)));
466 bool PreviouslyVisited = !IT.second;
467 auto &ProcessedThreshold = std::get<0>(IT.first->second);
468 auto &CalleeSummary = std::get<1>(IT.first->second);
469 auto &FailureInfo = std::get<2>(IT.first->second);
470
471 bool IsHotCallsite =
472 Edge.second.getHotness() == CalleeInfo::HotnessType::Hot;
473 bool IsCriticalCallsite =
474 Edge.second.getHotness() == CalleeInfo::HotnessType::Critical;
475
476 const FunctionSummary *ResolvedCalleeSummary = nullptr;
477 if (CalleeSummary) {
478 assert(PreviouslyVisited);
479 // Since the traversal of the call graph is DFS, we can revisit a function
480 // a second time with a higher threshold. In this case, it is added back
481 // to the worklist with the new threshold (so that its own callee chains
482 // can be considered with the higher threshold).
483 if (NewThreshold <= ProcessedThreshold) {
485 dbgs() << "ignored! Target was already imported with Threshold "
486 << ProcessedThreshold << "\n");
487 continue;
488 }
489 // Update with new larger threshold.
490 ProcessedThreshold = NewThreshold;
491 ResolvedCalleeSummary = cast<FunctionSummary>(CalleeSummary);
492 } else {
493 // If we already rejected importing a callee at the same or higher
494 // threshold, don't waste time calling selectCallee.
495 if (PreviouslyVisited && NewThreshold <= ProcessedThreshold) {
497 dbgs() << "ignored! Target was already rejected with Threshold "
498 << ProcessedThreshold << "\n");
500 assert(FailureInfo &&
501 "Expected FailureInfo for previously rejected candidate");
502 FailureInfo->Attempts++;
503 }
504 continue;
505 }
506
508 CalleeSummary = selectCallee(Index, VI.getSummaryList(), NewThreshold,
509 Summary.modulePath(), Reason);
510 if (!CalleeSummary) {
511 // Update with new larger threshold if this was a retry (otherwise
512 // we would have already inserted with NewThreshold above). Also
513 // update failure info if requested.
514 if (PreviouslyVisited) {
515 ProcessedThreshold = NewThreshold;
517 assert(FailureInfo &&
518 "Expected FailureInfo for previously rejected candidate");
519 FailureInfo->Reason = Reason;
520 FailureInfo->Attempts++;
521 FailureInfo->MaxHotness =
522 std::max(FailureInfo->MaxHotness, Edge.second.getHotness());
523 }
524 } else if (PrintImportFailures) {
525 assert(!FailureInfo &&
526 "Expected no FailureInfo for newly rejected candidate");
527 FailureInfo = std::make_unique<FunctionImporter::ImportFailureInfo>(
528 VI, Edge.second.getHotness(), Reason, 1);
529 }
530 if (ForceImportAll) {
531 std::string Msg = std::string("Failed to import function ") +
532 VI.name().str() + " due to " +
533 getFailureName(Reason);
534 auto Error = make_error<StringError>(
535 Msg, make_error_code(errc::not_supported));
536 logAllUnhandledErrors(std::move(Error), errs(),
537 "Error importing module: ");
538 break;
539 } else {
541 << "ignored! No qualifying callee with summary found.\n");
542 continue;
543 }
544 }
545
546 // "Resolve" the summary
547 CalleeSummary = CalleeSummary->getBaseObject();
548 ResolvedCalleeSummary = cast<FunctionSummary>(CalleeSummary);
549
550 assert((ResolvedCalleeSummary->fflags().AlwaysInline || ForceImportAll ||
551 (ResolvedCalleeSummary->instCount() <= NewThreshold)) &&
552 "selectCallee() didn't honor the threshold");
553
554 auto ExportModulePath = ResolvedCalleeSummary->modulePath();
555 auto ILI = ImportList[ExportModulePath].insert(VI.getGUID());
556 // We previously decided to import this GUID definition if it was already
557 // inserted in the set of imports from the exporting module.
558 bool PreviouslyImported = !ILI.second;
559 if (!PreviouslyImported) {
560 NumImportedFunctionsThinLink++;
561 if (IsHotCallsite)
562 NumImportedHotFunctionsThinLink++;
563 if (IsCriticalCallsite)
564 NumImportedCriticalFunctionsThinLink++;
565 }
566
567 // Any calls/references made by this function will be marked exported
568 // later, in ComputeCrossModuleImport, after import decisions are
569 // complete, which is more efficient than adding them here.
570 if (ExportLists)
571 (*ExportLists)[ExportModulePath].insert(VI);
572 }
573
574 auto GetAdjustedThreshold = [](unsigned Threshold, bool IsHotCallsite) {
575 // Adjust the threshold for next level of imported functions.
576 // The threshold is different for hot callsites because we can then
577 // inline chains of hot calls.
578 if (IsHotCallsite)
579 return Threshold * ImportHotInstrFactor;
580 return Threshold * ImportInstrFactor;
581 };
582
583 const auto AdjThreshold = GetAdjustedThreshold(Threshold, IsHotCallsite);
584
585 ImportCount++;
586
587 // Insert the newly imported function to the worklist.
588 Worklist.emplace_back(ResolvedCalleeSummary, AdjThreshold);
589 }
590}
591
593 const GVSummaryMapTy &DefinedGVSummaries, StringRef ModName,
594 FunctionImporter::ImportMapTy &ImportList) {
595 // Worklist contains the list of function imported in this module, for which
596 // we will analyse the callees and may import further down the callgraph.
598 GlobalsImporter GVI(Index, DefinedGVSummaries, IsPrevailing, ImportList,
599 ExportLists);
601
602 // Populate the worklist with the import for the functions in the current
603 // module
604 for (const auto &GVSummary : DefinedGVSummaries) {
605#ifndef NDEBUG
606 // FIXME: Change the GVSummaryMapTy to hold ValueInfo instead of GUID
607 // so this map look up (and possibly others) can be avoided.
608 auto VI = Index.getValueInfo(GVSummary.first);
609#endif
610 if (!Index.isGlobalValueLive(GVSummary.second)) {
611 LLVM_DEBUG(dbgs() << "Ignores Dead GUID: " << VI << "\n");
612 continue;
613 }
614 auto *FuncSummary =
615 dyn_cast<FunctionSummary>(GVSummary.second->getBaseObject());
616 if (!FuncSummary)
617 // Skip import for global variables
618 continue;
619 LLVM_DEBUG(dbgs() << "Initialize import for " << VI << "\n");
621 DefinedGVSummaries, IsPrevailing, Worklist, GVI,
622 ImportList, ExportLists, ImportThresholds);
623 }
624
625 // Process the newly imported functions and add callees to the worklist.
626 while (!Worklist.empty()) {
627 auto GVInfo = Worklist.pop_back_val();
628 auto *Summary = std::get<0>(GVInfo);
629 auto Threshold = std::get<1>(GVInfo);
630
631 if (auto *FS = dyn_cast<FunctionSummary>(Summary))
632 computeImportForFunction(*FS, Index, Threshold, DefinedGVSummaries,
633 IsPrevailing, Worklist, GVI, ImportList,
634 ExportLists, ImportThresholds);
635 }
636
637 // Print stats about functions considered but rejected for importing
638 // when requested.
640 dbgs() << "Missed imports into module " << ModName << "\n";
641 for (auto &I : ImportThresholds) {
642 auto &ProcessedThreshold = std::get<0>(I.second);
643 auto &CalleeSummary = std::get<1>(I.second);
644 auto &FailureInfo = std::get<2>(I.second);
645 if (CalleeSummary)
646 continue; // We are going to import.
647 assert(FailureInfo);
648 FunctionSummary *FS = nullptr;
649 if (!FailureInfo->VI.getSummaryList().empty())
650 FS = dyn_cast<FunctionSummary>(
651 FailureInfo->VI.getSummaryList()[0]->getBaseObject());
652 dbgs() << FailureInfo->VI
653 << ": Reason = " << getFailureName(FailureInfo->Reason)
654 << ", Threshold = " << ProcessedThreshold
655 << ", Size = " << (FS ? (int)FS->instCount() : -1)
656 << ", MaxHotness = " << getHotnessName(FailureInfo->MaxHotness)
657 << ", Attempts = " << FailureInfo->Attempts << "\n";
658 }
659 }
660}
661
662#ifndef NDEBUG
664 auto SL = VI.getSummaryList();
665 return SL.empty()
666 ? false
667 : SL[0]->getSummaryKind() == GlobalValueSummary::GlobalVarKind;
668}
669
672 if (const auto &VI = Index.getValueInfo(G))
673 return isGlobalVarSummary(Index, VI);
674 return false;
675}
676
677template <class T>
679 T &Cont) {
680 unsigned NumGVS = 0;
681 for (auto &V : Cont)
683 ++NumGVS;
684 return NumGVS;
685}
686#endif
687
688#ifndef NDEBUG
693
694 DenseSet<GlobalValue::GUID> FlattenedImports;
695
696 for (auto &ImportPerModule : ImportLists)
697 for (auto &ExportPerModule : ImportPerModule.second)
698 FlattenedImports.insert(ExportPerModule.second.begin(),
699 ExportPerModule.second.end());
700
701 // Checks that all GUIDs of read/writeonly vars we see in export lists
702 // are also in the import lists. Otherwise we my face linker undefs,
703 // because readonly and writeonly vars are internalized in their
704 // source modules. The exception would be if it has a linkage type indicating
705 // that there may have been a copy existing in the importing module (e.g.
706 // linkonce_odr). In that case we cannot accurately do this checking.
707 auto IsReadOrWriteOnlyVarNeedingImporting = [&](StringRef ModulePath,
708 const ValueInfo &VI) {
709 auto *GVS = dyn_cast_or_null<GlobalVarSummary>(
710 Index.findSummaryInModule(VI, ModulePath));
711 return GVS && (Index.isReadOnly(GVS) || Index.isWriteOnly(GVS)) &&
712 !(GVS->linkage() == GlobalValue::AvailableExternallyLinkage ||
713 GVS->linkage() == GlobalValue::WeakODRLinkage ||
714 GVS->linkage() == GlobalValue::LinkOnceODRLinkage);
715 };
716
717 for (auto &ExportPerModule : ExportLists)
718 for (auto &VI : ExportPerModule.second)
719 if (!FlattenedImports.count(VI.getGUID()) &&
720 IsReadOrWriteOnlyVarNeedingImporting(ExportPerModule.first, VI))
721 return false;
722
723 return true;
724}
725#endif
726
727/// Compute all the import and export for every module using the Index.
730 const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
732 isPrevailing,
735 ModuleImportsManager MIS(isPrevailing, Index, &ExportLists);
736 // For each module that has function defined, compute the import/export lists.
737 for (const auto &DefinedGVSummaries : ModuleToDefinedGVSummaries) {
738 auto &ImportList = ImportLists[DefinedGVSummaries.first];
739 LLVM_DEBUG(dbgs() << "Computing import for Module '"
740 << DefinedGVSummaries.first << "'\n");
741 MIS.computeImportForModule(DefinedGVSummaries.second,
742 DefinedGVSummaries.first, ImportList);
743 }
744
745 // When computing imports we only added the variables and functions being
746 // imported to the export list. We also need to mark any references and calls
747 // they make as exported as well. We do this here, as it is more efficient
748 // since we may import the same values multiple times into different modules
749 // during the import computation.
750 for (auto &ELI : ExportLists) {
752 const auto &DefinedGVSummaries =
753 ModuleToDefinedGVSummaries.lookup(ELI.first);
754 for (auto &EI : ELI.second) {
755 // Find the copy defined in the exporting module so that we can mark the
756 // values it references in that specific definition as exported.
757 // Below we will add all references and called values, without regard to
758 // whether they are also defined in this module. We subsequently prune the
759 // list to only include those defined in the exporting module, see comment
760 // there as to why.
761 auto DS = DefinedGVSummaries.find(EI.getGUID());
762 // Anything marked exported during the import computation must have been
763 // defined in the exporting module.
764 assert(DS != DefinedGVSummaries.end());
765 auto *S = DS->getSecond();
766 S = S->getBaseObject();
767 if (auto *GVS = dyn_cast<GlobalVarSummary>(S)) {
768 // Export referenced functions and variables. We don't export/promote
769 // objects referenced by writeonly variable initializer, because
770 // we convert such variables initializers to "zeroinitializer".
771 // See processGlobalForThinLTO.
772 if (!Index.isWriteOnly(GVS))
773 for (const auto &VI : GVS->refs())
774 NewExports.insert(VI);
775 } else {
776 auto *FS = cast<FunctionSummary>(S);
777 for (const auto &Edge : FS->calls())
778 NewExports.insert(Edge.first);
779 for (const auto &Ref : FS->refs())
780 NewExports.insert(Ref);
781 }
782 }
783 // Prune list computed above to only include values defined in the exporting
784 // module. We do this after the above insertion since we may hit the same
785 // ref/call target multiple times in above loop, and it is more efficient to
786 // avoid a set lookup each time.
787 for (auto EI = NewExports.begin(); EI != NewExports.end();) {
788 if (!DefinedGVSummaries.count(EI->getGUID()))
789 NewExports.erase(EI++);
790 else
791 ++EI;
792 }
793 ELI.second.insert(NewExports.begin(), NewExports.end());
794 }
795
796 assert(checkVariableImport(Index, ImportLists, ExportLists));
797#ifndef NDEBUG
798 LLVM_DEBUG(dbgs() << "Import/Export lists for " << ImportLists.size()
799 << " modules:\n");
800 for (auto &ModuleImports : ImportLists) {
801 auto ModName = ModuleImports.first;
802 auto &Exports = ExportLists[ModName];
803 unsigned NumGVS = numGlobalVarSummaries(Index, Exports);
804 LLVM_DEBUG(dbgs() << "* Module " << ModName << " exports "
805 << Exports.size() - NumGVS << " functions and " << NumGVS
806 << " vars. Imports from " << ModuleImports.second.size()
807 << " modules.\n");
808 for (auto &Src : ModuleImports.second) {
809 auto SrcModName = Src.first;
810 unsigned NumGVSPerMod = numGlobalVarSummaries(Index, Src.second);
811 LLVM_DEBUG(dbgs() << " - " << Src.second.size() - NumGVSPerMod
812 << " functions imported from " << SrcModName << "\n");
813 LLVM_DEBUG(dbgs() << " - " << NumGVSPerMod
814 << " global vars imported from " << SrcModName << "\n");
815 }
816 }
817#endif
818}
819
820#ifndef NDEBUG
822 StringRef ModulePath,
823 FunctionImporter::ImportMapTy &ImportList) {
824 LLVM_DEBUG(dbgs() << "* Module " << ModulePath << " imports from "
825 << ImportList.size() << " modules.\n");
826 for (auto &Src : ImportList) {
827 auto SrcModName = Src.first;
828 unsigned NumGVSPerMod = numGlobalVarSummaries(Index, Src.second);
829 LLVM_DEBUG(dbgs() << " - " << Src.second.size() - NumGVSPerMod
830 << " functions imported from " << SrcModName << "\n");
831 LLVM_DEBUG(dbgs() << " - " << NumGVSPerMod << " vars imported from "
832 << SrcModName << "\n");
833 }
834}
835#endif
836
837/// Compute all the imports for the given module using the Index.
838///
839/// \p isPrevailing is a callback that will be called with a global value's GUID
840/// and summary and should return whether the module corresponding to the
841/// summary contains the linker-prevailing copy of that value.
842///
843/// \p ImportList will be populated with a map that can be passed to
844/// FunctionImporter::importFunctions() above (see description there).
846 StringRef ModulePath,
848 isPrevailing,
850 FunctionImporter::ImportMapTy &ImportList) {
851 // Collect the list of functions this module defines.
852 // GUID -> Summary
853 GVSummaryMapTy FunctionSummaryMap;
854 Index.collectDefinedFunctionsForModule(ModulePath, FunctionSummaryMap);
855
856 // Compute the import list for this module.
857 LLVM_DEBUG(dbgs() << "Computing import for Module '" << ModulePath << "'\n");
858 ModuleImportsManager MIS(isPrevailing, Index);
859 MIS.computeImportForModule(FunctionSummaryMap, ModulePath, ImportList);
860
861#ifndef NDEBUG
862 dumpImportListForModule(Index, ModulePath, ImportList);
863#endif
864}
865
866/// Mark all external summaries in \p Index for import into the given module.
867/// Used for testing the case of distributed builds using a distributed index.
868///
869/// \p ImportList will be populated with a map that can be passed to
870/// FunctionImporter::importFunctions() above (see description there).
872 StringRef ModulePath, const ModuleSummaryIndex &Index,
873 FunctionImporter::ImportMapTy &ImportList) {
874 for (const auto &GlobalList : Index) {
875 // Ignore entries for undefined references.
876 if (GlobalList.second.SummaryList.empty())
877 continue;
878
879 auto GUID = GlobalList.first;
880 assert(GlobalList.second.SummaryList.size() == 1 &&
881 "Expected individual combined index to have one summary per GUID");
882 auto &Summary = GlobalList.second.SummaryList[0];
883 // Skip the summaries for the importing module. These are included to
884 // e.g. record required linkage changes.
885 if (Summary->modulePath() == ModulePath)
886 continue;
887 // Add an entry to provoke importing by thinBackend.
888 ImportList[Summary->modulePath()].insert(GUID);
889 }
890#ifndef NDEBUG
891 dumpImportListForModule(Index, ModulePath, ImportList);
892#endif
893}
894
895// For SamplePGO, the indirect call targets for local functions will
896// have its original name annotated in profile. We try to find the
897// corresponding PGOFuncName as the GUID, and fix up the edges
898// accordingly.
900 FunctionSummary *FS) {
901 for (auto &EI : FS->mutableCalls()) {
902 if (!EI.first.getSummaryList().empty())
903 continue;
904 auto GUID = Index.getGUIDFromOriginalID(EI.first.getGUID());
905 if (GUID == 0)
906 continue;
907 // Update the edge to point directly to the correct GUID.
908 auto VI = Index.getValueInfo(GUID);
909 if (llvm::any_of(
910 VI.getSummaryList(),
911 [&](const std::unique_ptr<GlobalValueSummary> &SummaryPtr) {
912 // The mapping from OriginalId to GUID may return a GUID
913 // that corresponds to a static variable. Filter it out here.
914 // This can happen when
915 // 1) There is a call to a library function which is not defined
916 // in the index.
917 // 2) There is a static variable with the OriginalGUID identical
918 // to the GUID of the library function in 1);
919 // When this happens the static variable in 2) will be found,
920 // which needs to be filtered out.
921 return SummaryPtr->getSummaryKind() ==
922 GlobalValueSummary::GlobalVarKind;
923 }))
924 continue;
925 EI.first = VI;
926 }
927}
928
930 for (const auto &Entry : Index) {
931 for (const auto &S : Entry.second.SummaryList) {
932 if (auto *FS = dyn_cast<FunctionSummary>(S.get()))
934 }
935 }
936}
937
940 const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
942 assert(!Index.withGlobalValueDeadStripping());
943 if (!ComputeDead ||
944 // Don't do anything when nothing is live, this is friendly with tests.
945 GUIDPreservedSymbols.empty()) {
946 // Still need to update indirect calls.
948 return;
949 }
950 unsigned LiveSymbols = 0;
952 Worklist.reserve(GUIDPreservedSymbols.size() * 2);
953 for (auto GUID : GUIDPreservedSymbols) {
954 ValueInfo VI = Index.getValueInfo(GUID);
955 if (!VI)
956 continue;
957 for (const auto &S : VI.getSummaryList())
958 S->setLive(true);
959 }
960
961 // Add values flagged in the index as live roots to the worklist.
962 for (const auto &Entry : Index) {
963 auto VI = Index.getValueInfo(Entry);
964 for (const auto &S : Entry.second.SummaryList) {
965 if (auto *FS = dyn_cast<FunctionSummary>(S.get()))
967 if (S->isLive()) {
968 LLVM_DEBUG(dbgs() << "Live root: " << VI << "\n");
969 Worklist.push_back(VI);
970 ++LiveSymbols;
971 break;
972 }
973 }
974 }
975
976 // Make value live and add it to the worklist if it was not live before.
977 auto visit = [&](ValueInfo VI, bool IsAliasee) {
978 // FIXME: If we knew which edges were created for indirect call profiles,
979 // we could skip them here. Any that are live should be reached via
980 // other edges, e.g. reference edges. Otherwise, using a profile collected
981 // on a slightly different binary might provoke preserving, importing
982 // and ultimately promoting calls to functions not linked into this
983 // binary, which increases the binary size unnecessarily. Note that
984 // if this code changes, the importer needs to change so that edges
985 // to functions marked dead are skipped.
986
987 if (llvm::any_of(VI.getSummaryList(),
988 [](const std::unique_ptr<llvm::GlobalValueSummary> &S) {
989 return S->isLive();
990 }))
991 return;
992
993 // We only keep live symbols that are known to be non-prevailing if any are
994 // available_externally, linkonceodr, weakodr. Those symbols are discarded
995 // later in the EliminateAvailableExternally pass and setting them to
996 // not-live could break downstreams users of liveness information (PR36483)
997 // or limit optimization opportunities.
998 if (isPrevailing(VI.getGUID()) == PrevailingType::No) {
999 bool KeepAliveLinkage = false;
1000 bool Interposable = false;
1001 for (const auto &S : VI.getSummaryList()) {
1002 if (S->linkage() == GlobalValue::AvailableExternallyLinkage ||
1003 S->linkage() == GlobalValue::WeakODRLinkage ||
1004 S->linkage() == GlobalValue::LinkOnceODRLinkage)
1005 KeepAliveLinkage = true;
1006 else if (GlobalValue::isInterposableLinkage(S->linkage()))
1007 Interposable = true;
1008 }
1009
1010 if (!IsAliasee) {
1011 if (!KeepAliveLinkage)
1012 return;
1013
1014 if (Interposable)
1016 "Interposable and available_externally/linkonce_odr/weak_odr "
1017 "symbol");
1018 }
1019 }
1020
1021 for (const auto &S : VI.getSummaryList())
1022 S->setLive(true);
1023 ++LiveSymbols;
1024 Worklist.push_back(VI);
1025 };
1026
1027 while (!Worklist.empty()) {
1028 auto VI = Worklist.pop_back_val();
1029 for (const auto &Summary : VI.getSummaryList()) {
1030 if (auto *AS = dyn_cast<AliasSummary>(Summary.get())) {
1031 // If this is an alias, visit the aliasee VI to ensure that all copies
1032 // are marked live and it is added to the worklist for further
1033 // processing of its references.
1034 visit(AS->getAliaseeVI(), true);
1035 continue;
1036 }
1037 for (auto Ref : Summary->refs())
1038 visit(Ref, false);
1039 if (auto *FS = dyn_cast<FunctionSummary>(Summary.get()))
1040 for (auto Call : FS->calls())
1041 visit(Call.first, false);
1042 }
1043 }
1044 Index.setWithGlobalValueDeadStripping();
1045
1046 unsigned DeadSymbols = Index.size() - LiveSymbols;
1047 LLVM_DEBUG(dbgs() << LiveSymbols << " symbols Live, and " << DeadSymbols
1048 << " symbols Dead \n");
1049 NumDeadSymbols += DeadSymbols;
1050 NumLiveSymbols += LiveSymbols;
1051}
1052
1053// Compute dead symbols and propagate constants in combined index.
1056 const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
1058 bool ImportEnabled) {
1059 computeDeadSymbolsAndUpdateIndirectCalls(Index, GUIDPreservedSymbols,
1060 isPrevailing);
1061 if (ImportEnabled)
1062 Index.propagateAttributes(GUIDPreservedSymbols);
1063}
1064
1065/// Compute the set of summaries needed for a ThinLTO backend compilation of
1066/// \p ModulePath.
1068 StringRef ModulePath,
1069 const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1070 const FunctionImporter::ImportMapTy &ImportList,
1071 std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex) {
1072 // Include all summaries from the importing module.
1073 ModuleToSummariesForIndex[std::string(ModulePath)] =
1074 ModuleToDefinedGVSummaries.lookup(ModulePath);
1075 // Include summaries for imports.
1076 for (const auto &ILI : ImportList) {
1077 auto &SummariesForIndex = ModuleToSummariesForIndex[std::string(ILI.first)];
1078 const auto &DefinedGVSummaries =
1079 ModuleToDefinedGVSummaries.lookup(ILI.first);
1080 for (const auto &GI : ILI.second) {
1081 const auto &DS = DefinedGVSummaries.find(GI);
1082 assert(DS != DefinedGVSummaries.end() &&
1083 "Expected a defined summary for imported global value");
1084 SummariesForIndex[GI] = DS->second;
1085 }
1086 }
1087}
1088
1089/// Emit the files \p ModulePath will import from into \p OutputFilename.
1092 const std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex) {
1093 std::error_code EC;
1094 raw_fd_ostream ImportsOS(OutputFilename, EC, sys::fs::OpenFlags::OF_None);
1095 if (EC)
1096 return EC;
1097 for (const auto &ILI : ModuleToSummariesForIndex)
1098 // The ModuleToSummariesForIndex map includes an entry for the current
1099 // Module (needed for writing out the index files). We don't want to
1100 // include it in the imports file, however, so filter it out.
1101 if (ILI.first != ModulePath)
1102 ImportsOS << ILI.first << "\n";
1103 return std::error_code();
1104}
1105
1107 LLVM_DEBUG(dbgs() << "Converting to a declaration: `" << GV.getName()
1108 << "\n");
1109 if (Function *F = dyn_cast<Function>(&GV)) {
1110 F->deleteBody();
1111 F->clearMetadata();
1112 F->setComdat(nullptr);
1113 } else if (GlobalVariable *V = dyn_cast<GlobalVariable>(&GV)) {
1114 V->setInitializer(nullptr);
1115 V->setLinkage(GlobalValue::ExternalLinkage);
1116 V->clearMetadata();
1117 V->setComdat(nullptr);
1118 } else {
1119 GlobalValue *NewGV;
1120 if (GV.getValueType()->isFunctionTy())
1121 NewGV =
1122 Function::Create(cast<FunctionType>(GV.getValueType()),
1124 "", GV.getParent());
1125 else
1126 NewGV =
1127 new GlobalVariable(*GV.getParent(), GV.getValueType(),
1128 /*isConstant*/ false, GlobalValue::ExternalLinkage,
1129 /*init*/ nullptr, "",
1130 /*insertbefore*/ nullptr, GV.getThreadLocalMode(),
1131 GV.getType()->getAddressSpace());
1132 NewGV->takeName(&GV);
1133 GV.replaceAllUsesWith(NewGV);
1134 return false;
1135 }
1136 if (!GV.isImplicitDSOLocal())
1137 GV.setDSOLocal(false);
1138 return true;
1139}
1140
1142 const GVSummaryMapTy &DefinedGlobals,
1143 bool PropagateAttrs) {
1144 DenseSet<Comdat *> NonPrevailingComdats;
1145 auto FinalizeInModule = [&](GlobalValue &GV, bool Propagate = false) {
1146 // See if the global summary analysis computed a new resolved linkage.
1147 const auto &GS = DefinedGlobals.find(GV.getGUID());
1148 if (GS == DefinedGlobals.end())
1149 return;
1150
1151 if (Propagate)
1152 if (FunctionSummary *FS = dyn_cast<FunctionSummary>(GS->second)) {
1153 if (Function *F = dyn_cast<Function>(&GV)) {
1154 // TODO: propagate ReadNone and ReadOnly.
1155 if (FS->fflags().ReadNone && !F->doesNotAccessMemory())
1156 F->setDoesNotAccessMemory();
1157
1158 if (FS->fflags().ReadOnly && !F->onlyReadsMemory())
1159 F->setOnlyReadsMemory();
1160
1161 if (FS->fflags().NoRecurse && !F->doesNotRecurse())
1162 F->setDoesNotRecurse();
1163
1164 if (FS->fflags().NoUnwind && !F->doesNotThrow())
1165 F->setDoesNotThrow();
1166 }
1167 }
1168
1169 auto NewLinkage = GS->second->linkage();
1171 // Don't internalize anything here, because the code below
1172 // lacks necessary correctness checks. Leave this job to
1173 // LLVM 'internalize' pass.
1174 GlobalValue::isLocalLinkage(NewLinkage) ||
1175 // In case it was dead and already converted to declaration.
1176 GV.isDeclaration())
1177 return;
1178
1179 // Set the potentially more constraining visibility computed from summaries.
1180 // The DefaultVisibility condition is because older GlobalValueSummary does
1181 // not record DefaultVisibility and we don't want to change protected/hidden
1182 // to default.
1183 if (GS->second->getVisibility() != GlobalValue::DefaultVisibility)
1184 GV.setVisibility(GS->second->getVisibility());
1185
1186 if (NewLinkage == GV.getLinkage())
1187 return;
1188
1189 // Check for a non-prevailing def that has interposable linkage
1190 // (e.g. non-odr weak or linkonce). In that case we can't simply
1191 // convert to available_externally, since it would lose the
1192 // interposable property and possibly get inlined. Simply drop
1193 // the definition in that case.
1196 if (!convertToDeclaration(GV))
1197 // FIXME: Change this to collect replaced GVs and later erase
1198 // them from the parent module once thinLTOResolvePrevailingGUID is
1199 // changed to enable this for aliases.
1200 llvm_unreachable("Expected GV to be converted");
1201 } else {
1202 // If all copies of the original symbol had global unnamed addr and
1203 // linkonce_odr linkage, or if all of them had local unnamed addr linkage
1204 // and are constants, then it should be an auto hide symbol. In that case
1205 // the thin link would have marked it as CanAutoHide. Add hidden
1206 // visibility to the symbol to preserve the property.
1207 if (NewLinkage == GlobalValue::WeakODRLinkage &&
1208 GS->second->canAutoHide()) {
1211 }
1212
1213 LLVM_DEBUG(dbgs() << "ODR fixing up linkage for `" << GV.getName()
1214 << "` from " << GV.getLinkage() << " to " << NewLinkage
1215 << "\n");
1216 GV.setLinkage(NewLinkage);
1217 }
1218 // Remove declarations from comdats, including available_externally
1219 // as this is a declaration for the linker, and will be dropped eventually.
1220 // It is illegal for comdats to contain declarations.
1221 auto *GO = dyn_cast_or_null<GlobalObject>(&GV);
1222 if (GO && GO->isDeclarationForLinker() && GO->hasComdat()) {
1223 if (GO->getComdat()->getName() == GO->getName())
1224 NonPrevailingComdats.insert(GO->getComdat());
1225 GO->setComdat(nullptr);
1226 }
1227 };
1228
1229 // Process functions and global now
1230 for (auto &GV : TheModule)
1231 FinalizeInModule(GV, PropagateAttrs);
1232 for (auto &GV : TheModule.globals())
1233 FinalizeInModule(GV);
1234 for (auto &GV : TheModule.aliases())
1235 FinalizeInModule(GV);
1236
1237 // For a non-prevailing comdat, all its members must be available_externally.
1238 // FinalizeInModule has handled non-local-linkage GlobalValues. Here we handle
1239 // local linkage GlobalValues.
1240 if (NonPrevailingComdats.empty())
1241 return;
1242 for (auto &GO : TheModule.global_objects()) {
1243 if (auto *C = GO.getComdat(); C && NonPrevailingComdats.count(C)) {
1244 GO.setComdat(nullptr);
1246 }
1247 }
1248 bool Changed;
1249 do {
1250 Changed = false;
1251 // If an alias references a GlobalValue in a non-prevailing comdat, change
1252 // it to available_externally. For simplicity we only handle GlobalValue and
1253 // ConstantExpr with a base object. ConstantExpr without a base object is
1254 // unlikely used in a COMDAT.
1255 for (auto &GA : TheModule.aliases()) {
1256 if (GA.hasAvailableExternallyLinkage())
1257 continue;
1258 GlobalObject *Obj = GA.getAliaseeObject();
1259 assert(Obj && "aliasee without an base object is unimplemented");
1260 if (Obj->hasAvailableExternallyLinkage()) {
1262 Changed = true;
1263 }
1264 }
1265 } while (Changed);
1266}
1267
1268/// Run internalization on \p TheModule based on symmary analysis.
1270 const GVSummaryMapTy &DefinedGlobals) {
1271 // Declare a callback for the internalize pass that will ask for every
1272 // candidate GlobalValue if it can be internalized or not.
1273 auto MustPreserveGV = [&](const GlobalValue &GV) -> bool {
1274 // It may be the case that GV is on a chain of an ifunc, its alias and
1275 // subsequent aliases. In this case, the summary for the value is not
1276 // available.
1277 if (isa<GlobalIFunc>(&GV) ||
1278 (isa<GlobalAlias>(&GV) &&
1279 isa<GlobalIFunc>(cast<GlobalAlias>(&GV)->getAliaseeObject())))
1280 return true;
1281
1282 // Lookup the linkage recorded in the summaries during global analysis.
1283 auto GS = DefinedGlobals.find(GV.getGUID());
1284 if (GS == DefinedGlobals.end()) {
1285 // Must have been promoted (possibly conservatively). Find original
1286 // name so that we can access the correct summary and see if it can
1287 // be internalized again.
1288 // FIXME: Eventually we should control promotion instead of promoting
1289 // and internalizing again.
1290 StringRef OrigName =
1292 std::string OrigId = GlobalValue::getGlobalIdentifier(
1294 TheModule.getSourceFileName());
1295 GS = DefinedGlobals.find(GlobalValue::getGUID(OrigId));
1296 if (GS == DefinedGlobals.end()) {
1297 // Also check the original non-promoted non-globalized name. In some
1298 // cases a preempted weak value is linked in as a local copy because
1299 // it is referenced by an alias (IRLinker::linkGlobalValueProto).
1300 // In that case, since it was originally not a local value, it was
1301 // recorded in the index using the original name.
1302 // FIXME: This may not be needed once PR27866 is fixed.
1303 GS = DefinedGlobals.find(GlobalValue::getGUID(OrigName));
1304 assert(GS != DefinedGlobals.end());
1305 }
1306 }
1307 return !GlobalValue::isLocalLinkage(GS->second->linkage());
1308 };
1309
1310 // FIXME: See if we can just internalize directly here via linkage changes
1311 // based on the index, rather than invoking internalizeModule.
1312 internalizeModule(TheModule, MustPreserveGV);
1313}
1314
1315/// Make alias a clone of its aliasee.
1317 Function *Fn = cast<Function>(GA->getAliaseeObject());
1318
1319 ValueToValueMapTy VMap;
1320 Function *NewFn = CloneFunction(Fn, VMap);
1321 // Clone should use the original alias's linkage, visibility and name, and we
1322 // ensure all uses of alias instead use the new clone (casted if necessary).
1323 NewFn->setLinkage(GA->getLinkage());
1324 NewFn->setVisibility(GA->getVisibility());
1325 GA->replaceAllUsesWith(NewFn);
1326 NewFn->takeName(GA);
1327 return NewFn;
1328}
1329
1330// Internalize values that we marked with specific attribute
1331// in processGlobalForThinLTO.
1333 for (auto &GV : M.globals())
1334 // Skip GVs which have been converted to declarations
1335 // by dropDeadSymbols.
1336 if (!GV.isDeclaration() && GV.hasAttribute("thinlto-internalize")) {
1337 GV.setLinkage(GlobalValue::InternalLinkage);
1338 GV.setVisibility(GlobalValue::DefaultVisibility);
1339 }
1340}
1341
1342// Automatically import functions in Module \p DestModule based on the summaries
1343// index.
1345 Module &DestModule, const FunctionImporter::ImportMapTy &ImportList) {
1346 LLVM_DEBUG(dbgs() << "Starting import for Module "
1347 << DestModule.getModuleIdentifier() << "\n");
1348 unsigned ImportedCount = 0, ImportedGVCount = 0;
1349
1350 IRMover Mover(DestModule);
1351 // Do the actual import of functions now, one Module at a time
1352 std::set<StringRef> ModuleNameOrderedList;
1353 for (const auto &FunctionsToImportPerModule : ImportList) {
1354 ModuleNameOrderedList.insert(FunctionsToImportPerModule.first);
1355 }
1356 for (const auto &Name : ModuleNameOrderedList) {
1357 // Get the module for the import
1358 const auto &FunctionsToImportPerModule = ImportList.find(Name);
1359 assert(FunctionsToImportPerModule != ImportList.end());
1360 Expected<std::unique_ptr<Module>> SrcModuleOrErr = ModuleLoader(Name);
1361 if (!SrcModuleOrErr)
1362 return SrcModuleOrErr.takeError();
1363 std::unique_ptr<Module> SrcModule = std::move(*SrcModuleOrErr);
1364 assert(&DestModule.getContext() == &SrcModule->getContext() &&
1365 "Context mismatch");
1366
1367 // If modules were created with lazy metadata loading, materialize it
1368 // now, before linking it (otherwise this will be a noop).
1369 if (Error Err = SrcModule->materializeMetadata())
1370 return std::move(Err);
1371
1372 auto &ImportGUIDs = FunctionsToImportPerModule->second;
1373 // Find the globals to import
1374 SetVector<GlobalValue *> GlobalsToImport;
1375 for (Function &F : *SrcModule) {
1376 if (!F.hasName())
1377 continue;
1378 auto GUID = F.getGUID();
1379 auto Import = ImportGUIDs.count(GUID);
1380 LLVM_DEBUG(dbgs() << (Import ? "Is" : "Not") << " importing function "
1381 << GUID << " " << F.getName() << " from "
1382 << SrcModule->getSourceFileName() << "\n");
1383 if (Import) {
1384 if (Error Err = F.materialize())
1385 return std::move(Err);
1387 // Add 'thinlto_src_module' metadata for statistics and debugging.
1388 F.setMetadata(
1389 "thinlto_src_module",
1390 MDNode::get(DestModule.getContext(),
1391 {MDString::get(DestModule.getContext(),
1392 SrcModule->getSourceFileName())}));
1393 }
1394 GlobalsToImport.insert(&F);
1395 }
1396 }
1397 for (GlobalVariable &GV : SrcModule->globals()) {
1398 if (!GV.hasName())
1399 continue;
1400 auto GUID = GV.getGUID();
1401 auto Import = ImportGUIDs.count(GUID);
1402 LLVM_DEBUG(dbgs() << (Import ? "Is" : "Not") << " importing global "
1403 << GUID << " " << GV.getName() << " from "
1404 << SrcModule->getSourceFileName() << "\n");
1405 if (Import) {
1406 if (Error Err = GV.materialize())
1407 return std::move(Err);
1408 ImportedGVCount += GlobalsToImport.insert(&GV);
1409 }
1410 }
1411 for (GlobalAlias &GA : SrcModule->aliases()) {
1412 if (!GA.hasName() || isa<GlobalIFunc>(GA.getAliaseeObject()))
1413 continue;
1414 auto GUID = GA.getGUID();
1415 auto Import = ImportGUIDs.count(GUID);
1416 LLVM_DEBUG(dbgs() << (Import ? "Is" : "Not") << " importing alias "
1417 << GUID << " " << GA.getName() << " from "
1418 << SrcModule->getSourceFileName() << "\n");
1419 if (Import) {
1420 if (Error Err = GA.materialize())
1421 return std::move(Err);
1422 // Import alias as a copy of its aliasee.
1423 GlobalObject *GO = GA.getAliaseeObject();
1424 if (Error Err = GO->materialize())
1425 return std::move(Err);
1426 auto *Fn = replaceAliasWithAliasee(SrcModule.get(), &GA);
1427 LLVM_DEBUG(dbgs() << "Is importing aliasee fn " << GO->getGUID() << " "
1428 << GO->getName() << " from "
1429 << SrcModule->getSourceFileName() << "\n");
1431 // Add 'thinlto_src_module' metadata for statistics and debugging.
1432 Fn->setMetadata(
1433 "thinlto_src_module",
1434 MDNode::get(DestModule.getContext(),
1435 {MDString::get(DestModule.getContext(),
1436 SrcModule->getSourceFileName())}));
1437 }
1438 GlobalsToImport.insert(Fn);
1439 }
1440 }
1441
1442 // Upgrade debug info after we're done materializing all the globals and we
1443 // have loaded all the required metadata!
1444 UpgradeDebugInfo(*SrcModule);
1445
1446 // Set the partial sample profile ratio in the profile summary module flag
1447 // of the imported source module, if applicable, so that the profile summary
1448 // module flag will match with that of the destination module when it's
1449 // imported.
1450 SrcModule->setPartialSampleProfileRatio(Index);
1451
1452 // Link in the specified functions.
1453 if (renameModuleForThinLTO(*SrcModule, Index, ClearDSOLocalOnDeclarations,
1454 &GlobalsToImport))
1455 return true;
1456
1457 if (PrintImports) {
1458 for (const auto *GV : GlobalsToImport)
1459 dbgs() << DestModule.getSourceFileName() << ": Import " << GV->getName()
1460 << " from " << SrcModule->getSourceFileName() << "\n";
1461 }
1462
1463 if (Error Err = Mover.move(std::move(SrcModule),
1464 GlobalsToImport.getArrayRef(), nullptr,
1465 /*IsPerformingImport=*/true))
1467 Twine("Function Import: link error: ") +
1468 toString(std::move(Err)));
1469
1470 ImportedCount += GlobalsToImport.size();
1471 NumImportedModules++;
1472 }
1473
1474 internalizeGVsAfterImport(DestModule);
1475
1476 NumImportedFunctions += (ImportedCount - ImportedGVCount);
1477 NumImportedGlobalVars += ImportedGVCount;
1478
1479 LLVM_DEBUG(dbgs() << "Imported " << ImportedCount - ImportedGVCount
1480 << " functions for Module "
1481 << DestModule.getModuleIdentifier() << "\n");
1482 LLVM_DEBUG(dbgs() << "Imported " << ImportedGVCount
1483 << " global variables for Module "
1484 << DestModule.getModuleIdentifier() << "\n");
1485 return ImportedCount;
1486}
1487
1490 isPrevailing) {
1491 if (SummaryFile.empty())
1492 report_fatal_error("error: -function-import requires -summary-file\n");
1495 if (!IndexPtrOrErr) {
1496 logAllUnhandledErrors(IndexPtrOrErr.takeError(), errs(),
1497 "Error loading file '" + SummaryFile + "': ");
1498 return false;
1499 }
1500 std::unique_ptr<ModuleSummaryIndex> Index = std::move(*IndexPtrOrErr);
1501
1502 // First step is collecting the import list.
1504 // If requested, simply import all functions in the index. This is used
1505 // when testing distributed backend handling via the opt tool, when
1506 // we have distributed indexes containing exactly the summaries to import.
1507 if (ImportAllIndex)
1509 *Index, ImportList);
1510 else
1511 ComputeCrossModuleImportForModuleForTest(M.getModuleIdentifier(),
1512 isPrevailing, *Index, ImportList);
1513
1514 // Conservatively mark all internal values as promoted. This interface is
1515 // only used when doing importing via the function importing pass. The pass
1516 // is only enabled when testing importing via the 'opt' tool, which does
1517 // not do the ThinLink that would normally determine what values to promote.
1518 for (auto &I : *Index) {
1519 for (auto &S : I.second.SummaryList) {
1520 if (GlobalValue::isLocalLinkage(S->linkage()))
1521 S->setLinkage(GlobalValue::ExternalLinkage);
1522 }
1523 }
1524
1525 // Next we need to promote to global scope and rename any local values that
1526 // are potentially exported to other modules.
1527 if (renameModuleForThinLTO(M, *Index, /*ClearDSOLocalOnDeclarations=*/false,
1528 /*GlobalsToImport=*/nullptr)) {
1529 errs() << "Error renaming module\n";
1530 return true;
1531 }
1532
1533 // Perform the import now.
1534 auto ModuleLoader = [&M](StringRef Identifier) {
1535 return loadFile(std::string(Identifier), M.getContext());
1536 };
1537 FunctionImporter Importer(*Index, ModuleLoader,
1538 /*ClearDSOLocalOnDeclarations=*/false);
1539 Expected<bool> Result = Importer.importFunctions(M, ImportList);
1540
1541 // FIXME: Probably need to propagate Errors through the pass manager.
1542 if (!Result) {
1543 logAllUnhandledErrors(Result.takeError(), errs(),
1544 "Error importing module: ");
1545 return true;
1546 }
1547
1548 return true;
1549}
1550
1553 // This is only used for testing the function import pass via opt, where we
1554 // don't have prevailing information from the LTO context available, so just
1555 // conservatively assume everything is prevailing (which is fine for the very
1556 // limited use of prevailing checking in this pass).
1557 auto isPrevailing = [](GlobalValue::GUID, const GlobalValueSummary *) {
1558 return true;
1559 };
1560 if (!doImportingForModuleForTest(M, isPrevailing))
1561 return PreservedAnalyses::all();
1562
1563 return PreservedAnalyses::none();
1564}
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")))
This file contains the declarations for the subclasses of Constant, which represent the different fla...
#define LLVM_DEBUG(X)
Definition: Debug.h:101
std::string Name
static auto qualifyCalleeCandidates(const ModuleSummaryIndex &Index, ArrayRef< std::unique_ptr< GlobalValueSummary > > CalleeSummaryList, StringRef CallerModulePath)
Given a list of possible callee implementation for a call site, qualify the legality of importing eac...
static cl::opt< float > ImportColdMultiplier("import-cold-multiplier", cl::init(0), cl::Hidden, cl::value_desc("N"), cl::desc("Multiply the `import-instr-limit` threshold for cold callsites"))
static cl::opt< float > ImportHotMultiplier("import-hot-multiplier", cl::init(10.0), cl::Hidden, cl::value_desc("x"), cl::desc("Multiply the `import-instr-limit` threshold for hot callsites"))
static const GlobalValueSummary * selectCallee(const ModuleSummaryIndex &Index, ArrayRef< std::unique_ptr< GlobalValueSummary > > CalleeSummaryList, unsigned Threshold, StringRef CallerModulePath, FunctionImporter::ImportFailureReason &Reason)
Given a list of possible callee implementation for a call site, select one that fits the Threshold.
static bool checkVariableImport(const ModuleSummaryIndex &Index, DenseMap< StringRef, FunctionImporter::ImportMapTy > &ImportLists, DenseMap< StringRef, FunctionImporter::ExportSetTy > &ExportLists)
static bool doImportingForModuleForTest(Module &M, function_ref< bool(GlobalValue::GUID, const GlobalValueSummary *)> isPrevailing)
static cl::opt< bool > EnableImportMetadata("enable-import-metadata", cl::init(false), cl::Hidden, cl::desc("Enable import metadata like 'thinlto_src_module'"))
static cl::opt< float > ImportHotInstrFactor("import-hot-evolution-factor", cl::init(1.0), cl::Hidden, cl::value_desc("x"), cl::desc("As we import functions called from hot callsite, multiply the " "`import-instr-limit` threshold by this factor " "before processing newly imported functions"))
static void computeImportForFunction(const FunctionSummary &Summary, const ModuleSummaryIndex &Index, const unsigned Threshold, const GVSummaryMapTy &DefinedGVSummaries, function_ref< bool(GlobalValue::GUID, const GlobalValueSummary *)> isPrevailing, SmallVectorImpl< EdgeInfo > &Worklist, GlobalsImporter &GVImporter, FunctionImporter::ImportMapTy &ImportList, DenseMap< StringRef, FunctionImporter::ExportSetTy > *ExportLists, FunctionImporter::ImportThresholdsTy &ImportThresholds)
Compute the list of functions to import for a given caller.
static cl::opt< float > ImportCriticalMultiplier("import-critical-multiplier", cl::init(100.0), cl::Hidden, cl::value_desc("x"), cl::desc("Multiply the `import-instr-limit` threshold for critical callsites"))
static cl::opt< int > ImportCutoff("import-cutoff", cl::init(-1), cl::Hidden, cl::value_desc("N"), cl::desc("Only import first N functions if N>=0 (default -1)"))
static const char * getFailureName(FunctionImporter::ImportFailureReason Reason)
static cl::opt< float > ImportInstrFactor("import-instr-evolution-factor", cl::init(0.7), cl::Hidden, cl::value_desc("x"), cl::desc("As we import functions, multiply the " "`import-instr-limit` threshold by this factor " "before processing newly imported functions"))
static void internalizeGVsAfterImport(Module &M)
static cl::opt< bool > PrintImports("print-imports", cl::init(false), cl::Hidden, cl::desc("Print imported functions"))
void updateValueInfoForIndirectCalls(ModuleSummaryIndex &Index, FunctionSummary *FS)
static std::unique_ptr< Module > loadFile(const std::string &FileName, LLVMContext &Context)
static cl::opt< bool > ForceImportAll("force-import-all", cl::init(false), cl::Hidden, cl::desc("Import functions with noinline attribute"))
static bool isGlobalVarSummary(const ModuleSummaryIndex &Index, ValueInfo VI)
static cl::opt< unsigned > ImportInstrLimit("import-instr-limit", cl::init(100), cl::Hidden, cl::value_desc("N"), cl::desc("Only import functions with less than N instructions"))
Limit on instruction count of imported functions.
static cl::opt< bool > ComputeDead("compute-dead", cl::init(true), cl::Hidden, cl::desc("Compute dead symbols"))
static cl::opt< std::string > SummaryFile("summary-file", cl::desc("The summary file to use for function importing."))
Summary file to use for function importing when using -function-import from the command line.
static cl::opt< bool > ImportAllIndex("import-all-index", cl::desc("Import all external functions in index."))
Used when testing importing from distributed indexes via opt.
static cl::opt< bool > PrintImportFailures("print-import-failures", cl::init(false), cl::Hidden, cl::desc("Print information for functions rejected for importing"))
static Function * replaceAliasWithAliasee(Module *SrcModule, GlobalAlias *GA)
Make alias a clone of its aliasee.
static void dumpImportListForModule(const ModuleSummaryIndex &Index, StringRef ModulePath, FunctionImporter::ImportMapTy &ImportList)
static void ComputeCrossModuleImportForModuleFromIndexForTest(StringRef ModulePath, const ModuleSummaryIndex &Index, FunctionImporter::ImportMapTy &ImportList)
Mark all external summaries in Index for import into the given module.
static unsigned numGlobalVarSummaries(const ModuleSummaryIndex &Index, T &Cont)
static void ComputeCrossModuleImportForModuleForTest(StringRef ModulePath, function_ref< bool(GlobalValue::GUID, const GlobalValueSummary *)> isPrevailing, const ModuleSummaryIndex &Index, FunctionImporter::ImportMapTy &ImportList)
Compute all the imports for the given module using the Index.
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
#define G(x, y, z)
Definition: MD5.cpp:56
static cl::opt< std::string > OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"), cl::init("-"))
This file contains the declarations for metadata subclasses.
static cl::opt< bool > PropagateAttrs("propagate-attrs", cl::init(true), cl::Hidden, cl::desc("Propagate attributes in index"))
ModuleSummaryIndex.h This file contains the declarations the classes that hold the module index and s...
Module.h This file contains the declarations for the Module class.
LLVMContext & Context
if(VerifyEach)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file contains some templates that are useful if you are working with the STL at all.
This file implements a set that has insertion order iteration characteristics.
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition: Statistic.h:167
Import globals referenced by a function or other globals that are being imported, if importing such g...
void onImportingSummary(const GlobalValueSummary &Summary)
GlobalsImporter(const ModuleSummaryIndex &Index, const GVSummaryMapTy &DefinedGVSummaries, function_ref< bool(GlobalValue::GUID, const GlobalValueSummary *)> IsPrevailing, FunctionImporter::ImportMapTy &ImportList, DenseMap< StringRef, FunctionImporter::ExportSetTy > *ExportLists)
Determine the list of imports and exports for each module.
ModuleImportsManager(function_ref< bool(GlobalValue::GUID, const GlobalValueSummary *)> IsPrevailing, const ModuleSummaryIndex &Index, DenseMap< StringRef, FunctionImporter::ExportSetTy > *ExportLists=nullptr)
void computeImportForModule(const GVSummaryMapTy &DefinedGVSummaries, StringRef ModName, FunctionImporter::ImportMapTy &ImportList)
Given the list of globals defined in a module, compute the list of imports as well as the list of "ex...
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:649
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
ValueT lookup(const_arg_type_t< KeyT > Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
Definition: DenseMap.h:202
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:155
unsigned size() const
Definition: DenseMap.h:99
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:151
iterator end()
Definition: DenseMap.h:84
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: DenseMap.h:220
Implements a dense probed hash-table based set.
Definition: DenseSet.h:271
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
Tagged union holding either a T or a Error.
Definition: Error.h:474
Error takeError()
Take ownership of the stored error.
Definition: Error.h:601
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
The function importer is automatically importing function from other modules based on the provided su...
Expected< bool > importFunctions(Module &M, const ImportMapTy &ImportList)
Import functions in Module M based on the supplied import list.
ImportFailureReason
The different reasons selectCallee will chose not to import a candidate.
Function summary information to aid decisions and implementation of importing.
unsigned instCount() const
Get the instruction count recorded for this function.
FFlags fflags() const
Get function summary flags.
static Function * Create(FunctionType *Ty, LinkageTypes Linkage, unsigned AddrSpace, const Twine &N="", Module *M=nullptr)
Definition: Function.h:162
const GlobalObject * getAliaseeObject() const
Definition: Globals.cpp:544
Function and variable summary information to aid decisions and implementation of importing.
StringRef modulePath() const
Get the path to the module containing this function.
VisibilityTypes getVisibility() const
Definition: GlobalValue.h:244
bool isImplicitDSOLocal() const
Definition: GlobalValue.h:294
static bool isLocalLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:404
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition: Globals.cpp:273
LinkageTypes getLinkage() const
Definition: GlobalValue.h:541
uint64_t GUID
Declare a type to represent a global unique identifier for a global value.
Definition: GlobalValue.h:583
ThreadLocalMode getThreadLocalMode() const
Definition: GlobalValue.h:267
static bool isAvailableExternallyLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:374
void setLinkage(LinkageTypes LT)
Definition: GlobalValue.h:532
unsigned getAddressSpace() const
Definition: GlobalValue.h:201
GUID getGUID() const
Return a 64-bit global unique ID constructed from global value name (i.e.
Definition: GlobalValue.h:591
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:652
const GlobalObject * getAliaseeObject() const
Definition: Globals.cpp:367
void setDSOLocal(bool Local)
Definition: GlobalValue.h:299
PointerType * getType() const
Global values are always pointers.
Definition: GlobalValue.h:290
@ DefaultVisibility
The GV is visible.
Definition: GlobalValue.h:63
@ HiddenVisibility
The GV is hidden.
Definition: GlobalValue.h:64
static GUID getGUID(StringRef GlobalName)
Return a 64-bit global unique ID constructed from global value name (i.e.
Definition: GlobalValue.h:587
void setVisibility(VisibilityTypes V)
Definition: GlobalValue.h:250
static bool isInterposableLinkage(LinkageTypes Linkage)
Whether the definition of this global may be replaced by something non-equivalent at link time.
Definition: GlobalValue.h:420
Error materialize()
Make sure this GlobalValue is fully read.
Definition: Globals.cpp:47
bool canBeOmittedFromSymbolTable() const
True if GV can be left out of the object symbol table.
Definition: Globals.cpp:392
bool hasAvailableExternallyLinkage() const
Definition: GlobalValue.h:507
std::string getGlobalIdentifier() const
Return the modified name for this global value suitable to be used as the key for a global lookup (e....
Definition: Globals.cpp:168
@ InternalLinkage
Rename collisions when linking (static functions).
Definition: GlobalValue.h:55
@ WeakODRLinkage
Same, but only replaced by something equivalent.
Definition: GlobalValue.h:53
@ ExternalLinkage
Externally visible function.
Definition: GlobalValue.h:48
@ AvailableExternallyLinkage
Available for inspection, not emission.
Definition: GlobalValue.h:49
@ LinkOnceODRLinkage
Same, but only replaced by something equivalent.
Definition: GlobalValue.h:51
Type * getValueType() const
Definition: GlobalValue.h:292
Error move(std::unique_ptr< Module > Src, ArrayRef< GlobalValue * > ValuesToLink, LazyCallback AddLazyFor, bool IsPerformingImport)
Move in the provide values in ValuesToLink from Src.
Definition: IRMover.cpp:1774
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1504
Class to hold module path string table and global value map, and encapsulate methods for operating on...
static StringRef getOriginalNameBeforePromote(StringRef Name)
Helper to obtain the unpromoted name for a global value (or the original name if not promoted).
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
LLVMContext & getContext() const
Get the global data context.
Definition: Module.h:283
const std::string & getSourceFileName() const
Get the module's original source file name.
Definition: Module.h:260
iterator_range< alias_iterator > aliases()
Definition: Module.h:728
iterator_range< global_iterator > globals()
Definition: Module.h:688
const std::string & getModuleIdentifier() const
Get the module identifier which is, essentially, the name of the module.
Definition: Module.h:249
iterator_range< global_object_iterator > global_objects()
Definition: Module.cpp:399
unsigned getAddressSpace() const
Return the address space of the Pointer type.
Definition: DerivedTypes.h:693
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:172
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition: PassManager.h:175
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:178
Instances of this class encapsulate one diagnostic report, allowing printing to a raw_ostream as a ca...
Definition: SourceMgr.h:281
A vector that has set insertion semantics.
Definition: SetVector.h:57
ArrayRef< value_type > getArrayRef() const
Definition: SetVector.h:84
size_type size() const
Determine the number of elements in the SetVector.
Definition: SetVector.h:98
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition: SetVector.h:162
bool empty() const
Definition: SmallVector.h:94
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:577
reference emplace_back(ArgTypes &&... Args)
Definition: SmallVector.h:941
void reserve(size_type N)
Definition: SmallVector.h:667
void push_back(const T &Elt)
Definition: SmallVector.h:416
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1200
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
bool isFunctionTy() const
True if this is an instance of FunctionType.
Definition: Type.h:246
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:534
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:309
void takeName(Value *V)
Transfer the name from V to this value.
Definition: Value.cpp:383
std::pair< iterator, bool > insert(const ValueT &V)
Definition: DenseSet.h:206
size_type size() const
Definition: DenseSet.h:81
bool erase(const ValueT &V)
Definition: DenseSet.h:101
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:97
An efficient, type-erasing, non-owning reference to a callable.
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:454
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:445
std::optional< const char * > toString(const std::optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract a string value from it.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner={})
Log all errors (if any) in E to OS.
Definition: Error.cpp:65
void ComputeCrossModuleImport(const ModuleSummaryIndex &Index, const DenseMap< StringRef, GVSummaryMapTy > &ModuleToDefinedGVSummaries, function_ref< bool(GlobalValue::GUID, const GlobalValueSummary *)> isPrevailing, DenseMap< StringRef, FunctionImporter::ImportMapTy > &ImportLists, DenseMap< StringRef, FunctionImporter::ExportSetTy > &ExportLists)
Compute all the imports and exports for every module in the Index.
bool internalizeModule(Module &TheModule, std::function< bool(const GlobalValue &)> MustPreserveGV)
Helper function to internalize functions and variables in a Module.
Definition: Internalize.h:75
std::error_code make_error_code(BitcodeError E)
const char * getHotnessName(CalleeInfo::HotnessType HT)
std::error_code EmitImportsFiles(StringRef ModulePath, StringRef OutputFilename, const std::map< std::string, GVSummaryMapTy > &ModuleToSummariesForIndex)
Emit into OutputFilename the files module ModulePath will import from.
@ Import
Import information from summary.
bool convertToDeclaration(GlobalValue &GV)
Converts value GV to declaration, or replaces with a declaration if it is an alias.
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition: Error.h:1244
auto map_range(ContainerTy &&C, FuncTy F)
Definition: STLExtras.h:377
bool renameModuleForThinLTO(Module &M, const ModuleSummaryIndex &Index, bool ClearDSOLocalOnDeclarations, SetVector< GlobalValue * > *GlobalsToImport=nullptr)
Perform in-place global value handling on the given Module for exported local functions renamed and p...
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1733
void computeDeadSymbolsAndUpdateIndirectCalls(ModuleSummaryIndex &Index, const DenseSet< GlobalValue::GUID > &GUIDPreservedSymbols, function_ref< PrevailingType(GlobalValue::GUID)> isPrevailing)
Compute all the symbols that are "dead": i.e these that can't be reached in the graph from any of the...
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:156
void gatherImportedSummariesForModule(StringRef ModulePath, const DenseMap< StringRef, GVSummaryMapTy > &ModuleToDefinedGVSummaries, const FunctionImporter::ImportMapTy &ImportList, std::map< std::string, GVSummaryMapTy > &ModuleToSummariesForIndex)
Compute the set of summaries needed for a ThinLTO backend compilation of ModulePath.
void updateIndirectCalls(ModuleSummaryIndex &Index)
Update call edges for indirect calls to local functions added from SamplePGO when needed.
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
@ Ref
The access may reference the value stored in memory.
std::unique_ptr< Module > getLazyIRFileModule(StringRef Filename, SMDiagnostic &Err, LLVMContext &Context, bool ShouldLazyLoadMetadata=false)
If the given file holds a bitcode image, return a Module for it which does lazy deserialization of fu...
Definition: IRReader.cpp:53
void thinLTOInternalizeModule(Module &TheModule, const GVSummaryMapTy &DefinedGlobals)
Internalize TheModule based on the information recorded in the summaries during global summary-based ...
PrevailingType
PrevailingType enum used as a return type of callback passed to computeDeadSymbolsAndUpdateIndirectCa...
bool UpgradeDebugInfo(Module &M)
Check the debug info version number, if it is out-dated, drop the debug info.
Function * CloneFunction(Function *F, ValueToValueMapTy &VMap, ClonedCodeInfo *CodeInfo=nullptr)
Return a copy of the specified function and add it to that function's module.
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.
Expected< std::unique_ptr< ModuleSummaryIndex > > getModuleSummaryIndexForFile(StringRef Path, bool IgnoreEmptyThinLTOIndexFile=false)
Parse the module summary index out of an IR file and return the module summary index object if found,...
void thinLTOFinalizeInModule(Module &TheModule, const GVSummaryMapTy &DefinedGlobals, bool PropagateAttrs)
Based on the information recorded in the summaries during global summary-based analysis:
Struct that holds a reference to a particular GUID in a global value summary.