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