LLVM 23.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/Function.h"
23#include "llvm/IR/GlobalAlias.h"
25#include "llvm/IR/GlobalValue.h"
27#include "llvm/IR/Metadata.h"
28#include "llvm/IR/Module.h"
31#include "llvm/Linker/IRMover.h"
35#include "llvm/Support/Debug.h"
36#include "llvm/Support/Errc.h"
37#include "llvm/Support/Error.h"
40#include "llvm/Support/JSON.h"
41#include "llvm/Support/Path.h"
49#include <cassert>
50#include <memory>
51#include <string>
52#include <system_error>
53#include <tuple>
54#include <utility>
55
56using namespace llvm;
57
58#define DEBUG_TYPE "function-import"
59
60STATISTIC(NumImportedFunctionsThinLink,
61 "Number of functions thin link decided to import");
62STATISTIC(NumImportedHotFunctionsThinLink,
63 "Number of hot functions thin link decided to import");
64STATISTIC(NumImportedCriticalFunctionsThinLink,
65 "Number of critical functions thin link decided to import");
66STATISTIC(NumImportedGlobalVarsThinLink,
67 "Number of global variables thin link decided to import");
68STATISTIC(NumImportedFunctions, "Number of functions imported in backend");
69STATISTIC(NumImportedGlobalVars,
70 "Number of global variables imported in backend");
71STATISTIC(NumImportedModules, "Number of modules imported from");
72STATISTIC(NumDeadSymbols, "Number of dead stripped symbols in index");
73STATISTIC(NumLiveSymbols, "Number of live symbols in index");
74
75namespace llvm {
77
79 ForceImportAll("force-import-all", cl::init(false), cl::Hidden,
80 cl::desc("Import functions with noinline attribute"));
81
82/// Limit on instruction count of imported functions.
84 "import-instr-limit", cl::init(100), cl::Hidden, cl::value_desc("N"),
85 cl::desc("Only import functions with less than N instructions"));
86
88 "import-cutoff", cl::init(-1), cl::Hidden, cl::value_desc("N"),
89 cl::desc("Only import first N functions if N>=0 (default -1)"));
90
91static cl::opt<float>
92 ImportInstrFactor("import-instr-evolution-factor", cl::init(0.7),
94 cl::desc("As we import functions, multiply the "
95 "`import-instr-limit` threshold by this factor "
96 "before processing newly imported functions"));
97
99 "import-hot-evolution-factor", cl::init(1.0), cl::Hidden,
100 cl::value_desc("x"),
101 cl::desc("As we import functions called from hot callsite, multiply the "
102 "`import-instr-limit` threshold by this factor "
103 "before processing newly imported functions"));
104
106 "import-hot-multiplier", cl::init(10.0), cl::Hidden, cl::value_desc("x"),
107 cl::desc("Multiply the `import-instr-limit` threshold for hot callsites"));
108
110 "import-critical-multiplier", cl::init(100.0), cl::Hidden,
111 cl::value_desc("x"),
112 cl::desc(
113 "Multiply the `import-instr-limit` threshold for critical callsites"));
114
115// FIXME: This multiplier was not really tuned up.
117 "import-cold-multiplier", cl::init(0), cl::Hidden, cl::value_desc("N"),
118 cl::desc("Multiply the `import-instr-limit` threshold for cold callsites"));
119
120static cl::opt<bool> PrintImports("print-imports", cl::init(false), cl::Hidden,
121 cl::desc("Print imported functions"));
122
124 "print-import-failures", cl::init(false), cl::Hidden,
125 cl::desc("Print information for functions rejected for importing"));
126
127static cl::opt<bool> ComputeDead("compute-dead", cl::init(true), cl::Hidden,
128 cl::desc("Compute dead symbols"));
129
131 "enable-import-metadata", cl::init(false), cl::Hidden,
132 cl::desc("Enable import metadata like 'thinlto_src_module' and "
133 "'thinlto_src_file'"));
134
135/// Summary file to use for function importing when using -function-import from
136/// the command line.
138 SummaryFile("summary-file",
139 cl::desc("The summary file to use for function importing."));
140
141/// Used when testing importing from distributed indexes via opt
142// -function-import.
143static cl::opt<bool>
144 ImportAllIndex("import-all-index",
145 cl::desc("Import all external functions in index."));
146
147/// This is a test-only option.
148/// If this option is enabled, the ThinLTO indexing step will import each
149/// function declaration as a fallback. In a real build this may increase ram
150/// usage of the indexing step unnecessarily.
151/// TODO: Implement selective import (based on combined summary analysis) to
152/// ensure the imported function has a use case in the postlink pipeline.
154 "import-declaration", cl::init(false), cl::Hidden,
155 cl::desc("If true, import function declaration as fallback if the function "
156 "definition is not imported."));
157
158/// Pass a workload description file - an example of workload would be the
159/// functions executed to satisfy a RPC request. A workload is defined by a root
160/// function and the list of functions that are (frequently) needed to satisfy
161/// it. The module that defines the root will have all those functions imported.
162/// The file contains a JSON dictionary. The keys are root functions, the values
163/// are lists of functions to import in the module defining the root. It is
164/// assumed -funique-internal-linkage-names was used, thus ensuring function
165/// names are unique even for local linkage ones.
167 "thinlto-workload-def",
168 cl::desc("Pass a workload definition. This is a file containing a JSON "
169 "dictionary. The keys are root functions, the values are lists of "
170 "functions to import in the module defining the root. It is "
171 "assumed -funique-internal-linkage-names was used, to ensure "
172 "local linkage functions have unique names. For example: \n"
173 "{\n"
174 " \"rootFunction_1\": [\"function_to_import_1\", "
175 "\"function_to_import_2\"], \n"
176 " \"rootFunction_2\": [\"function_to_import_3\", "
177 "\"function_to_import_4\"] \n"
178 "}"),
179 cl::Hidden);
180
182
184 "thinlto-move-ctxprof-trees",
185 cl::desc("Move contextual profiling roots and the graphs under them in "
186 "their own module."),
187 cl::Hidden, cl::init(false));
188
190
192} // end namespace llvm
193
194// Load lazily a module from \p FileName in \p Context.
195static std::unique_ptr<Module> loadFile(const std::string &FileName,
196 LLVMContext &Context) {
197 SMDiagnostic Err;
198 LLVM_DEBUG(dbgs() << "Loading '" << FileName << "'\n");
199 // Metadata isn't loaded until functions are imported, to minimize
200 // the memory overhead.
201 std::unique_ptr<Module> Result =
202 getLazyIRFileModule(FileName, Err, Context,
203 /* ShouldLazyLoadMetadata = */ true);
204 if (!Result) {
205 Err.print("function-import", errs());
206 report_fatal_error("Abort");
207 }
208
209 return Result;
210}
211
213 size_t NumDefs,
214 StringRef ImporterModule) {
215 // We can import a local when there is one definition.
216 if (NumDefs == 1)
217 return false;
218 // In other cases, make sure we import the copy in the caller's module if the
219 // referenced value has local linkage. The only time a local variable can
220 // share an entry in the index is if there is a local with the same name in
221 // another module that had the same source file name (in a different
222 // directory), where each was compiled in their own directory so there was not
223 // distinguishing path.
224 return GlobalValue::isLocalLinkage(RefSummary->linkage()) &&
225 RefSummary->modulePath() != ImporterModule;
226}
227
228/// Given a list of possible callee implementation for a call site, qualify the
229/// legality of importing each. The return is a range of pairs. Each pair
230/// corresponds to a candidate. The first value is the ImportFailureReason for
231/// that candidate, the second is the candidate.
233 const ModuleSummaryIndex &Index,
234 ArrayRef<std::unique_ptr<GlobalValueSummary>> CalleeSummaryList,
235 StringRef CallerModulePath) {
236 return llvm::map_range(
237 CalleeSummaryList,
238 [&Index, CalleeSummaryList,
239 CallerModulePath](const std::unique_ptr<GlobalValueSummary> &SummaryPtr)
241 const GlobalValueSummary *> {
242 auto *GVSummary = SummaryPtr.get();
243 if (!Index.isGlobalValueLive(GVSummary))
245
246 if (GlobalValue::isInterposableLinkage(GVSummary->linkage()))
248 GVSummary};
249
250 auto *Summary = dyn_cast<FunctionSummary>(GVSummary->getBaseObject());
251
252 // Ignore any callees that aren't actually functions. This could happen
253 // in the case of GUID hash collisions. It could also happen in theory
254 // for SamplePGO profiles collected on old versions of the code after
255 // renaming, since we synthesize edges to any inlined callees appearing
256 // in the profile.
257 if (!Summary)
259
260 // If this is a local function, make sure we import the copy in the
261 // caller's module. The only time a local function can share an entry in
262 // the index is if there is a local with the same name in another module
263 // that had the same source file name (in a different directory), where
264 // each was compiled in their own directory so there was not
265 // distinguishing path.
266 // If the local function is from another module, it must be a reference
267 // due to indirect call profile data since a function pointer can point
268 // to a local in another module. Do the import from another module if
269 // there is only one entry in the list or when all files in the program
270 // are compiled with full path - in both cases the local function has
271 // unique PGO name and GUID.
272 if (shouldSkipLocalInAnotherModule(Summary, CalleeSummaryList.size(),
273 CallerModulePath))
274 return {
276 GVSummary};
277
278 // Skip if it isn't legal to import (e.g. may reference unpromotable
279 // locals).
280 if (Summary->notEligibleToImport())
282 GVSummary};
283
285 });
286}
287
288/// Given a list of possible callee implementation for a call site, select one
289/// that fits the \p Threshold for function definition import. If none are
290/// found, the Reason will give the last reason for the failure (last, in the
291/// order of CalleeSummaryList entries). While looking for a callee definition,
292/// sets \p TooLargeOrNoInlineSummary to the last seen too-large or noinline
293/// candidate; other modules may want to know the function summary or
294/// declaration even if a definition is not needed.
295///
296/// FIXME: select "best" instead of first that fits. But what is "best"?
297/// - The smallest: more likely to be inlined.
298/// - The one with the least outgoing edges (already well optimized).
299/// - One from a module already being imported from in order to reduce the
300/// number of source modules parsed/linked.
301/// - One that has PGO data attached.
302/// - [insert you fancy metric here]
303static const GlobalValueSummary *
305 ArrayRef<std::unique_ptr<GlobalValueSummary>> CalleeSummaryList,
306 unsigned Threshold, StringRef CallerModulePath,
307 const GlobalValueSummary *&TooLargeOrNoInlineSummary,
309 // Records the last summary with reason noinline or too-large.
310 TooLargeOrNoInlineSummary = nullptr;
311 auto QualifiedCandidates =
312 qualifyCalleeCandidates(Index, CalleeSummaryList, CallerModulePath);
313 for (auto QualifiedValue : QualifiedCandidates) {
314 Reason = QualifiedValue.first;
315 // Skip a summary if its import is not (proved to be) legal.
317 continue;
318 auto *Summary =
319 cast<FunctionSummary>(QualifiedValue.second->getBaseObject());
320
321 // Don't bother importing the definition if the chance of inlining it is
322 // not high enough (except under `--force-import-all`).
323 if ((Summary->instCount() > Threshold) && !Summary->fflags().AlwaysInline &&
325 TooLargeOrNoInlineSummary = Summary;
327 continue;
328 }
329
330 // Don't bother importing the definition if we can't inline it anyway.
331 if (Summary->fflags().NoInline && !ForceImportAll) {
332 TooLargeOrNoInlineSummary = Summary;
334 continue;
335 }
336
337 return Summary;
338 }
339 return nullptr;
340}
341
342namespace {
343
344using EdgeInfo = std::tuple<const FunctionSummary *, unsigned /* Threshold */>;
345
346} // anonymous namespace
347
350 GlobalValue::GUID GUID) {
351 auto [Def, Decl] = IDs.createImportIDs(FromModule, GUID);
352 if (!Imports.insert(Def).second)
353 // Already there.
355
356 // Remove Decl in case it's there. Note that a definition takes precedence
357 // over a declaration for a given GUID.
358 return Imports.erase(Decl) ? AddDefinitionStatus::ChangedToDefinition
360}
361
363 StringRef FromModule, GlobalValue::GUID GUID) {
364 auto [Def, Decl] = IDs.createImportIDs(FromModule, GUID);
365 // Insert Decl only if Def is not present. Note that a definition takes
366 // precedence over a declaration for a given GUID.
367 if (!Imports.contains(Def))
368 Imports.insert(Decl);
369}
370
373 SetVector<StringRef> ModuleSet;
374 for (const auto &[SrcMod, GUID, ImportType] : *this)
375 ModuleSet.insert(SrcMod);
376 SmallVector<StringRef, 0> Modules = ModuleSet.takeVector();
377 llvm::sort(Modules);
378 return Modules;
379}
380
381std::optional<GlobalValueSummary::ImportKind>
383 GlobalValue::GUID GUID) const {
384 if (auto IDPair = IDs.getImportIDs(FromModule, GUID)) {
385 auto [Def, Decl] = *IDPair;
386 if (Imports.contains(Def))
388 if (Imports.contains(Decl))
390 }
391 return std::nullopt;
392}
393
394/// Import globals referenced by a function or other globals that are being
395/// imported, if importing such global is possible.
396class GlobalsImporter final {
397 const ModuleSummaryIndex &Index;
398 const GVSummaryMapTy &DefinedGVSummaries;
400 IsPrevailing;
403
404 bool shouldImportGlobal(const ValueInfo &VI) {
405 const auto &GVS = DefinedGVSummaries.find(VI.getGUID());
406 if (GVS == DefinedGVSummaries.end())
407 return true;
408 // We should not skip import if the module contains a non-prevailing
409 // definition with interposable linkage type. This is required for
410 // correctness in the situation where there is a prevailing def available
411 // for import and marked read-only. In this case, the non-prevailing def
412 // will be converted to a declaration, while the prevailing one becomes
413 // internal, thus no definitions will be available for linking. In order to
414 // prevent undefined symbol link error, the prevailing definition must be
415 // imported.
416 // FIXME: Consider adding a check that the suitable prevailing definition
417 // exists and marked read-only.
418 if (VI.getSummaryList().size() > 1 &&
419 GlobalValue::isInterposableLinkage(GVS->second->linkage()) &&
420 !IsPrevailing(VI.getGUID(), GVS->second))
421 return true;
422
423 return false;
424 }
425
426 void
427 onImportingSummaryImpl(const GlobalValueSummary &Summary,
429 for (const auto &VI : Summary.refs()) {
430 if (!shouldImportGlobal(VI)) {
432 dbgs() << "Ref ignored! Target already in destination module.\n");
433 continue;
434 }
435
436 LLVM_DEBUG(dbgs() << " ref -> " << VI << "\n");
437
438 for (const auto &RefSummary : VI.getSummaryList()) {
439 const auto *GVS = dyn_cast<GlobalVarSummary>(RefSummary.get());
440 // Stop looking if this is not a global variable, e.g. a function.
441 // Functions could be referenced by global vars - e.g. a vtable; but we
442 // don't currently imagine a reason those would be imported here, rather
443 // than as part of the logic deciding which functions to import (i.e.
444 // based on profile information). Should we decide to handle them here,
445 // we can refactor accordingly at that time.
446 // Note that it is safe to stop looking because the one case where we
447 // might have to import (a read/write-only global variable) cannot occur
448 // if this GUID has a non-variable summary. The only case where we even
449 // might find another summary in the list that is a variable is in the
450 // case of same-named locals in different modules not compiled with
451 // enough path, and during attribute propagation we will mark all
452 // summaries for a GUID (ValueInfo) as non read/write-only if any is not
453 // a global variable.
454 if (!GVS)
455 break;
456 bool CanImportDecl = false;
457 if (shouldSkipLocalInAnotherModule(GVS, VI.getSummaryList().size(),
458 Summary.modulePath()) ||
459 !Index.canImportGlobalVar(GVS, /* AnalyzeRefs */ true,
460 CanImportDecl)) {
461 if (ImportDeclaration && CanImportDecl)
462 ImportList.maybeAddDeclaration(RefSummary->modulePath(),
463 VI.getGUID());
464
465 continue;
466 }
467
468 // If there isn't an entry for GUID, insert <GUID, Definition> pair.
469 // Otherwise, definition should take precedence over declaration.
470 if (ImportList.addDefinition(RefSummary->modulePath(), VI.getGUID()) !=
472 break;
473
474 // Only update stat and exports if we haven't already imported this
475 // variable.
476 NumImportedGlobalVarsThinLink++;
477 // Any references made by this variable will be marked exported
478 // later, in ComputeCrossModuleImport, after import decisions are
479 // complete, which is more efficient than adding them here.
480 if (ExportLists)
481 (*ExportLists)[RefSummary->modulePath()].insert(VI);
482
483 // If variable is not writeonly we attempt to recursively analyze
484 // its references in order to import referenced constants.
485 if (!Index.isWriteOnly(GVS))
486 Worklist.emplace_back(GVS);
487 break;
488 }
489 }
490 }
491
492public:
494 const ModuleSummaryIndex &Index, const GVSummaryMapTy &DefinedGVSummaries,
496 IsPrevailing,
499 : Index(Index), DefinedGVSummaries(DefinedGVSummaries),
500 IsPrevailing(IsPrevailing), ImportList(ImportList),
501 ExportLists(ExportLists) {}
502
505 onImportingSummaryImpl(Summary, Worklist);
506 while (!Worklist.empty())
507 onImportingSummaryImpl(*Worklist.pop_back_val(), Worklist);
508 }
509};
510
512
513/// Determine the list of imports and exports for each module.
515 void computeImportForFunction(
516 const FunctionSummary &Summary, unsigned Threshold,
517 const GVSummaryMapTy &DefinedGVSummaries,
518 SmallVectorImpl<EdgeInfo> &Worklist, GlobalsImporter &GVImporter,
520 FunctionImporter::ImportThresholdsTy &ImportThresholds);
521
522protected:
527
534 virtual bool canImport(ValueInfo VI) { return true; }
535
536public:
537 virtual ~ModuleImportsManager() = default;
538
539 /// Given the list of globals defined in a module, compute the list of imports
540 /// as well as the list of "exports", i.e. the list of symbols referenced from
541 /// another module (that may require promotion).
542 virtual void
543 computeImportForModule(const GVSummaryMapTy &DefinedGVSummaries,
544 StringRef ModName,
546
547 static std::unique_ptr<ModuleImportsManager>
552 nullptr);
553};
554
555/// A ModuleImportsManager that operates based on a workload definition (see
556/// -thinlto-workload-def). For modules that do not define workload roots, it
557/// applies the base ModuleImportsManager import policy.
559 // Keep a module name -> value infos to import association. We use it to
560 // determine if a module's import list should be done by the base
561 // ModuleImportsManager or by us.
563 // Track the roots to avoid importing them due to other callers. We want there
564 // to be only one variant), for which we optimize according to the contextual
565 // profile. "Variants" refers to copies due to importing - we want there to be
566 // just one instance of this function.
568
569 void
570 computeImportForModule(const GVSummaryMapTy &DefinedGVSummaries,
571 StringRef ModName,
572 FunctionImporter::ImportMapTy &ImportList) override {
573 StringRef Filename = ModName;
575 Filename = sys::path::filename(ModName);
576 // Drop the file extension.
577 Filename = Filename.substr(0, Filename.find_last_of('.'));
578 }
579 auto SetIter = Workloads.find(Filename);
580
581 if (SetIter == Workloads.end()) {
582 LLVM_DEBUG(dbgs() << "[Workload] " << ModName
583 << " does not contain the root of any context.\n");
584 return ModuleImportsManager::computeImportForModule(DefinedGVSummaries,
585 ModName, ImportList);
586 }
587 LLVM_DEBUG(dbgs() << "[Workload] " << ModName
588 << " contains the root(s) of context(s).\n");
589
590 GlobalsImporter GVI(Index, DefinedGVSummaries, IsPrevailing, ImportList,
592 auto &ValueInfos = SetIter->second;
593 for (auto &VI : llvm::make_early_inc_range(ValueInfos)) {
594 auto It = DefinedGVSummaries.find(VI.getGUID());
595 if (It != DefinedGVSummaries.end() &&
596 IsPrevailing(VI.getGUID(), It->second)) {
598 dbgs() << "[Workload] " << VI.name()
599 << " has the prevailing variant already in the module "
600 << ModName << ". No need to import\n");
601 continue;
602 }
603 auto Candidates =
604 qualifyCalleeCandidates(Index, VI.getSummaryList(), ModName);
605
606 const GlobalValueSummary *GVS = nullptr;
607 auto PotentialCandidates = llvm::map_range(
609 Candidates,
610 [&](const auto &Candidate) {
611 LLVM_DEBUG(dbgs() << "[Workflow] Candidate for " << VI.name()
612 << " from " << Candidate.second->modulePath()
613 << " ImportFailureReason: "
614 << getFailureName(Candidate.first) << "\n");
615 return Candidate.first ==
617 }),
618 [](const auto &Candidate) { return Candidate.second; });
619 if (PotentialCandidates.empty()) {
620 LLVM_DEBUG(dbgs() << "[Workload] Not importing " << VI.name()
621 << " because can't find eligible Callee. Guid is: "
622 << VI.getGUID() << "\n");
623 continue;
624 }
625 /// We will prefer importing the prevailing candidate, if not, we'll
626 /// still pick the first available candidate. The reason we want to make
627 /// sure we do import the prevailing candidate is because the goal of
628 /// workload-awareness is to enable optimizations specializing the call
629 /// graph of that workload. Suppose a function is already defined in the
630 /// module, but it's not the prevailing variant. Suppose also we do not
631 /// inline it (in fact, if it were interposable, we can't inline it),
632 /// but we could specialize it to the workload in other ways. However,
633 /// the linker would drop it in the favor of the prevailing copy.
634 /// Instead, by importing the prevailing variant (assuming also the use
635 /// of `-avail-extern-to-local`), we keep the specialization. We could
636 /// alteranatively make the non-prevailing variant local, but the
637 /// prevailing one is also the one for which we would have previously
638 /// collected profiles, making it preferrable.
639 auto PrevailingCandidates = llvm::make_filter_range(
640 PotentialCandidates, [&](const auto *Candidate) {
641 return IsPrevailing(VI.getGUID(), Candidate);
642 });
643 if (PrevailingCandidates.empty()) {
644 GVS = *PotentialCandidates.begin();
645 if (!llvm::hasSingleElement(PotentialCandidates) &&
648 dbgs()
649 << "[Workload] Found multiple non-prevailing candidates for "
650 << VI.name()
651 << ". This is unexpected. Are module paths passed to the "
652 "compiler unique for the modules passed to the linker?");
653 // We could in theory have multiple (interposable) copies of a symbol
654 // when there is no prevailing candidate, if say the prevailing copy was
655 // in a native object being linked in. However, we should in theory be
656 // marking all of these non-prevailing IR copies dead in that case, in
657 // which case they won't be candidates.
658 assert(GVS->isLive());
659 } else {
660 assert(llvm::hasSingleElement(PrevailingCandidates));
661 GVS = *PrevailingCandidates.begin();
662 }
663
664 auto ExportingModule = GVS->modulePath();
665 // We checked that for the prevailing case, but if we happen to have for
666 // example an internal that's defined in this module, it'd have no
667 // PrevailingCandidates.
668 if (ExportingModule == ModName) {
669 LLVM_DEBUG(dbgs() << "[Workload] Not importing " << VI.name()
670 << " because its defining module is the same as the "
671 "current module\n");
672 continue;
673 }
674 LLVM_DEBUG(dbgs() << "[Workload][Including]" << VI.name() << " from "
675 << ExportingModule << " : " << VI.getGUID() << "\n");
676 ImportList.addDefinition(ExportingModule, VI.getGUID());
677 GVI.onImportingSummary(*GVS);
678 if (ExportLists)
679 (*ExportLists)[ExportingModule].insert(VI);
680 }
681 LLVM_DEBUG(dbgs() << "[Workload] Done\n");
682 }
683
684 void loadFromJson() {
685 // Since the workload def uses names, we need a quick lookup
686 // name->ValueInfo.
687 StringMap<ValueInfo> NameToValueInfo;
688 StringSet<> AmbiguousNames;
689 for (auto &I : Index) {
690 ValueInfo VI = Index.getValueInfo(I);
691 if (!NameToValueInfo.insert(std::make_pair(VI.name(), VI)).second)
692 LLVM_DEBUG(AmbiguousNames.insert(VI.name()));
693 }
694 auto DbgReportIfAmbiguous = [&](StringRef Name) {
695 LLVM_DEBUG(if (AmbiguousNames.count(Name) > 0) {
696 dbgs() << "[Workload] Function name " << Name
697 << " present in the workload definition is ambiguous. Consider "
698 "compiling with -funique-internal-linkage-names.";
699 });
700 };
701 std::error_code EC;
703 if (std::error_code EC = BufferOrErr.getError()) {
704 report_fatal_error("Failed to open context file");
705 return;
706 }
707 auto Buffer = std::move(BufferOrErr.get());
708 std::map<std::string, std::vector<std::string>> WorkloadDefs;
709 json::Path::Root NullRoot;
710 // The JSON is supposed to contain a dictionary matching the type of
711 // WorkloadDefs. For example:
712 // {
713 // "rootFunction_1": ["function_to_import_1", "function_to_import_2"],
714 // "rootFunction_2": ["function_to_import_3", "function_to_import_4"]
715 // }
716 auto Parsed = json::parse(Buffer->getBuffer());
717 if (!Parsed)
718 report_fatal_error(Parsed.takeError());
719 if (!json::fromJSON(*Parsed, WorkloadDefs, NullRoot))
720 report_fatal_error("Invalid thinlto contextual profile format.");
721 for (const auto &Workload : WorkloadDefs) {
722 const auto &Root = Workload.first;
723 DbgReportIfAmbiguous(Root);
724 LLVM_DEBUG(dbgs() << "[Workload] Root: " << Root << "\n");
725 const auto &AllCallees = Workload.second;
726 auto RootIt = NameToValueInfo.find(Root);
727 if (RootIt == NameToValueInfo.end()) {
728 LLVM_DEBUG(dbgs() << "[Workload] Root " << Root
729 << " not found in this linkage unit.\n");
730 continue;
731 }
732 auto RootVI = RootIt->second;
733 if (RootVI.getSummaryList().size() != 1) {
734 LLVM_DEBUG(dbgs() << "[Workload] Root " << Root
735 << " should have exactly one summary, but has "
736 << RootVI.getSummaryList().size() << ". Skipping.\n");
737 continue;
738 }
739 StringRef RootDefiningModule =
740 RootVI.getSummaryList().front()->modulePath();
741 LLVM_DEBUG(dbgs() << "[Workload] Root defining module for " << Root
742 << " is : " << RootDefiningModule << "\n");
743 auto &Set = Workloads[RootDefiningModule];
744 for (const auto &Callee : AllCallees) {
745 LLVM_DEBUG(dbgs() << "[Workload] " << Callee << "\n");
746 DbgReportIfAmbiguous(Callee);
747 auto ElemIt = NameToValueInfo.find(Callee);
748 if (ElemIt == NameToValueInfo.end()) {
749 LLVM_DEBUG(dbgs() << "[Workload] " << Callee << " not found\n");
750 continue;
751 }
752 Set.insert(ElemIt->second);
753 }
754 }
755 }
756
757 void loadFromCtxProf() {
758 std::error_code EC;
760 if (std::error_code EC = BufferOrErr.getError()) {
761 report_fatal_error("Failed to open contextual profile file");
762 return;
763 }
764 auto Buffer = std::move(BufferOrErr.get());
765
766 PGOCtxProfileReader Reader(Buffer->getBuffer());
767 auto Ctx = Reader.loadProfiles();
768 if (!Ctx) {
769 report_fatal_error("Failed to parse contextual profiles");
770 return;
771 }
772 const auto &CtxMap = Ctx->Contexts;
773 SetVector<GlobalValue::GUID> ContainedGUIDs;
774 for (const auto &[RootGuid, Root] : CtxMap) {
775 // Avoid ContainedGUIDs to get in/out of scope. Reuse its memory for
776 // subsequent roots, but clear its contents.
777 ContainedGUIDs.clear();
778
779 auto RootVI = Index.getValueInfo(RootGuid);
780 if (!RootVI) {
781 LLVM_DEBUG(dbgs() << "[Workload] Root " << RootGuid
782 << " not found in this linkage unit.\n");
783 continue;
784 }
785 if (RootVI.getSummaryList().size() != 1) {
786 LLVM_DEBUG(dbgs() << "[Workload] Root " << RootGuid
787 << " should have exactly one summary, but has "
788 << RootVI.getSummaryList().size() << ". Skipping.\n");
789 continue;
790 }
791 std::string RootDefiningModule =
792 RootVI.getSummaryList().front()->modulePath().str();
794 RootDefiningModule = std::to_string(RootGuid);
796 dbgs() << "[Workload] Moving " << RootGuid
797 << " to a module with the filename without extension : "
798 << RootDefiningModule << "\n");
799 } else {
800 LLVM_DEBUG(dbgs() << "[Workload] Root defining module for " << RootGuid
801 << " is : " << RootDefiningModule << "\n");
802 }
803 auto &Set = Workloads[RootDefiningModule];
804 Root.getContainedGuids(ContainedGUIDs);
805 Roots.insert(RootVI);
806 for (auto Guid : ContainedGUIDs)
807 if (auto VI = Index.getValueInfo(Guid))
808 Set.insert(VI);
809 }
810 }
811
812 bool canImport(ValueInfo VI) override { return !Roots.contains(VI); }
813
814public:
821 if (UseCtxProfile.empty() == WorkloadDefinitions.empty()) {
822 report_fatal_error(
823 "Pass only one of: -thinlto-pgo-ctx-prof or -thinlto-workload-def");
824 return;
825 }
826 if (!UseCtxProfile.empty())
827 loadFromCtxProf();
828 else
829 loadFromJson();
830 LLVM_DEBUG({
831 for (const auto &[Root, Set] : Workloads) {
832 dbgs() << "[Workload] Root: " << Root << " we have " << Set.size()
833 << " distinct callees.\n";
834 for (const auto &VI : Set) {
835 dbgs() << "[Workload] Root: " << Root
836 << " Would include: " << VI.getGUID() << "\n";
837 }
838 }
839 });
840 }
841};
842
843std::unique_ptr<ModuleImportsManager> ModuleImportsManager::create(
848 if (WorkloadDefinitions.empty() && UseCtxProfile.empty()) {
849 LLVM_DEBUG(dbgs() << "[Workload] Using the regular imports manager.\n");
850 return std::unique_ptr<ModuleImportsManager>(
852 }
853 LLVM_DEBUG(dbgs() << "[Workload] Using the contextual imports manager.\n");
854 return std::make_unique<WorkloadImportsManager>(IsPrevailing, Index,
856}
857
858static const char *
860 switch (Reason) {
862 return "None";
864 return "GlobalVar";
866 return "NotLive";
868 return "TooLarge";
870 return "InterposableLinkage";
872 return "LocalLinkageNotInModule";
874 return "NotEligible";
876 return "NoInline";
877 }
878 llvm_unreachable("invalid reason");
879}
880
881/// Compute the list of functions to import for a given caller. Mark these
882/// imported functions and the symbols they reference in their source module as
883/// exported from their source module.
884void ModuleImportsManager::computeImportForFunction(
885 const FunctionSummary &Summary, const unsigned Threshold,
886 const GVSummaryMapTy &DefinedGVSummaries,
887 SmallVectorImpl<EdgeInfo> &Worklist, GlobalsImporter &GVImporter,
888 FunctionImporter::ImportMapTy &ImportList,
889 FunctionImporter::ImportThresholdsTy &ImportThresholds) {
890 GVImporter.onImportingSummary(Summary);
891 static int ImportCount = 0;
892 for (const auto &Edge : Summary.calls()) {
893 ValueInfo VI = Edge.first;
894 LLVM_DEBUG(dbgs() << " edge -> " << VI << " Threshold:" << Threshold
895 << "\n");
896
897 if (ImportCutoff >= 0 && ImportCount >= ImportCutoff) {
898 LLVM_DEBUG(dbgs() << "ignored! import-cutoff value of " << ImportCutoff
899 << " reached.\n");
900 continue;
901 }
902
903 if (DefinedGVSummaries.count(VI.getGUID())) {
904 // FIXME: Consider not skipping import if the module contains
905 // a non-prevailing def with interposable linkage. The prevailing copy
906 // can safely be imported (see shouldImportGlobal()).
907 LLVM_DEBUG(dbgs() << "ignored! Target already in destination module.\n");
908 continue;
909 }
910
911 if (!canImport(VI)) {
913 dbgs() << "Skipping over " << VI.getGUID()
914 << " because its import is handled in a different module.");
915 assert(VI.getSummaryList().size() == 1 &&
916 "The root was expected to be an external symbol");
917 continue;
918 }
919
920 auto GetBonusMultiplier = [](CalleeInfo::HotnessType Hotness) -> float {
921 if (Hotness == CalleeInfo::HotnessType::Hot)
922 return ImportHotMultiplier;
923 if (Hotness == CalleeInfo::HotnessType::Cold)
927 return 1.0;
928 };
929
930 const auto NewThreshold =
931 Threshold * GetBonusMultiplier(Edge.second.getHotness());
932
933 auto IT = ImportThresholds.insert(std::make_pair(
934 VI.getGUID(), std::make_tuple(NewThreshold, nullptr, nullptr)));
935 bool PreviouslyVisited = !IT.second;
936 auto &ProcessedThreshold = std::get<0>(IT.first->second);
937 auto &CalleeSummary = std::get<1>(IT.first->second);
938 auto &FailureInfo = std::get<2>(IT.first->second);
939
940 bool IsHotCallsite =
941 Edge.second.getHotness() == CalleeInfo::HotnessType::Hot;
942 bool IsCriticalCallsite =
943 Edge.second.getHotness() == CalleeInfo::HotnessType::Critical;
944
945 const FunctionSummary *ResolvedCalleeSummary = nullptr;
946 if (CalleeSummary) {
947 assert(PreviouslyVisited);
948 // Since the traversal of the call graph is DFS, we can revisit a function
949 // a second time with a higher threshold. In this case, it is added back
950 // to the worklist with the new threshold (so that its own callee chains
951 // can be considered with the higher threshold).
952 if (NewThreshold <= ProcessedThreshold) {
954 dbgs() << "ignored! Target was already imported with Threshold "
955 << ProcessedThreshold << "\n");
956 continue;
957 }
958 // Update with new larger threshold.
959 ProcessedThreshold = NewThreshold;
960 ResolvedCalleeSummary = cast<FunctionSummary>(CalleeSummary);
961 } else {
962 // If we already rejected importing a callee at the same or higher
963 // threshold, don't waste time calling selectCallee.
964 if (PreviouslyVisited && NewThreshold <= ProcessedThreshold) {
966 dbgs() << "ignored! Target was already rejected with Threshold "
967 << ProcessedThreshold << "\n");
969 assert(FailureInfo &&
970 "Expected FailureInfo for previously rejected candidate");
971 FailureInfo->Attempts++;
972 }
973 continue;
974 }
975
977
978 // `SummaryForDeclImport` is an summary eligible for declaration import.
979 const GlobalValueSummary *SummaryForDeclImport = nullptr;
980 CalleeSummary =
981 selectCallee(Index, VI.getSummaryList(), NewThreshold,
982 Summary.modulePath(), SummaryForDeclImport, Reason);
983 if (!CalleeSummary) {
984 // There isn't a callee for definition import but one for declaration
985 // import.
986 if (ImportDeclaration && SummaryForDeclImport) {
987 StringRef DeclSourceModule = SummaryForDeclImport->modulePath();
988
989 // Note `ExportLists` only keeps track of exports due to imported
990 // definitions.
991 ImportList.maybeAddDeclaration(DeclSourceModule, VI.getGUID());
992 }
993 // Update with new larger threshold if this was a retry (otherwise
994 // we would have already inserted with NewThreshold above). Also
995 // update failure info if requested.
996 if (PreviouslyVisited) {
997 ProcessedThreshold = NewThreshold;
999 assert(FailureInfo &&
1000 "Expected FailureInfo for previously rejected candidate");
1001 FailureInfo->Reason = Reason;
1002 FailureInfo->Attempts++;
1003 FailureInfo->MaxHotness =
1004 std::max(FailureInfo->MaxHotness, Edge.second.getHotness());
1005 }
1006 } else if (PrintImportFailures) {
1007 assert(!FailureInfo &&
1008 "Expected no FailureInfo for newly rejected candidate");
1009 FailureInfo = std::make_unique<FunctionImporter::ImportFailureInfo>(
1010 VI, Edge.second.getHotness(), Reason, 1);
1011 }
1012 if (ForceImportAll) {
1013 std::string Msg = std::string("Failed to import function ") +
1014 VI.name().str() + " due to " +
1015 getFailureName(Reason);
1018 logAllUnhandledErrors(std::move(Error), errs(),
1019 "Error importing module: ");
1020 break;
1021 } else {
1023 << "ignored! No qualifying callee with summary found.\n");
1024 continue;
1025 }
1026 }
1027
1028 // "Resolve" the summary
1029 CalleeSummary = CalleeSummary->getBaseObject();
1030 ResolvedCalleeSummary = cast<FunctionSummary>(CalleeSummary);
1031
1032 assert((ResolvedCalleeSummary->fflags().AlwaysInline || ForceImportAll ||
1033 (ResolvedCalleeSummary->instCount() <= NewThreshold)) &&
1034 "selectCallee() didn't honor the threshold");
1035
1036 auto ExportModulePath = ResolvedCalleeSummary->modulePath();
1037
1038 // Try emplace the definition entry, and update stats based on insertion
1039 // status.
1040 if (ImportList.addDefinition(ExportModulePath, VI.getGUID()) !=
1042 NumImportedFunctionsThinLink++;
1043 if (IsHotCallsite)
1044 NumImportedHotFunctionsThinLink++;
1045 if (IsCriticalCallsite)
1046 NumImportedCriticalFunctionsThinLink++;
1047 }
1048
1049 // Any calls/references made by this function will be marked exported
1050 // later, in ComputeCrossModuleImport, after import decisions are
1051 // complete, which is more efficient than adding them here.
1052 if (ExportLists)
1053 (*ExportLists)[ExportModulePath].insert(VI);
1054 }
1055
1056 auto GetAdjustedThreshold = [](unsigned Threshold, bool IsHotCallsite) {
1057 // Adjust the threshold for next level of imported functions.
1058 // The threshold is different for hot callsites because we can then
1059 // inline chains of hot calls.
1060 if (IsHotCallsite)
1061 return Threshold * ImportHotInstrFactor;
1062 return Threshold * ImportInstrFactor;
1063 };
1064
1065 const auto AdjThreshold = GetAdjustedThreshold(Threshold, IsHotCallsite);
1066
1067 ImportCount++;
1068
1069 // Insert the newly imported function to the worklist.
1070 Worklist.emplace_back(ResolvedCalleeSummary, AdjThreshold);
1071 }
1072}
1073
1075 const GVSummaryMapTy &DefinedGVSummaries, StringRef ModName,
1076 FunctionImporter::ImportMapTy &ImportList) {
1077 // Worklist contains the list of function imported in this module, for which
1078 // we will analyse the callees and may import further down the callgraph.
1080 GlobalsImporter GVI(Index, DefinedGVSummaries, IsPrevailing, ImportList,
1081 ExportLists);
1082 FunctionImporter::ImportThresholdsTy ImportThresholds;
1083
1084 // Populate the worklist with the import for the functions in the current
1085 // module
1086 for (const auto &GVSummary : DefinedGVSummaries) {
1087#ifndef NDEBUG
1088 // FIXME: Change the GVSummaryMapTy to hold ValueInfo instead of GUID
1089 // so this map look up (and possibly others) can be avoided.
1090 auto VI = Index.getValueInfo(GVSummary.first);
1091#endif
1092 if (!Index.isGlobalValueLive(GVSummary.second)) {
1093 LLVM_DEBUG(dbgs() << "Ignores Dead GUID: " << VI << "\n");
1094 continue;
1095 }
1096 auto *FuncSummary =
1097 dyn_cast<FunctionSummary>(GVSummary.second->getBaseObject());
1098 if (!FuncSummary)
1099 // Skip import for global variables
1100 continue;
1101 LLVM_DEBUG(dbgs() << "Initialize import for " << VI << "\n");
1102 computeImportForFunction(*FuncSummary, ImportInstrLimit, DefinedGVSummaries,
1103 Worklist, GVI, ImportList, ImportThresholds);
1104 }
1105
1106 // Process the newly imported functions and add callees to the worklist.
1107 while (!Worklist.empty()) {
1108 auto GVInfo = Worklist.pop_back_val();
1109 auto *Summary = std::get<0>(GVInfo);
1110 auto Threshold = std::get<1>(GVInfo);
1111
1112 computeImportForFunction(*Summary, Threshold, DefinedGVSummaries, Worklist,
1113 GVI, ImportList, ImportThresholds);
1114 }
1115
1116 // Print stats about functions considered but rejected for importing
1117 // when requested.
1118 if (PrintImportFailures) {
1119 dbgs() << "Missed imports into module " << ModName << "\n";
1120 for (auto &I : ImportThresholds) {
1121 auto &ProcessedThreshold = std::get<0>(I.second);
1122 auto &CalleeSummary = std::get<1>(I.second);
1123 auto &FailureInfo = std::get<2>(I.second);
1124 if (CalleeSummary)
1125 continue; // We are going to import.
1126 assert(FailureInfo);
1127 FunctionSummary *FS = nullptr;
1128 if (!FailureInfo->VI.getSummaryList().empty())
1130 FailureInfo->VI.getSummaryList()[0]->getBaseObject());
1131 dbgs() << FailureInfo->VI
1132 << ": Reason = " << getFailureName(FailureInfo->Reason)
1133 << ", Threshold = " << ProcessedThreshold
1134 << ", Size = " << (FS ? (int)FS->instCount() : -1)
1135 << ", MaxHotness = " << getHotnessName(FailureInfo->MaxHotness)
1136 << ", Attempts = " << FailureInfo->Attempts << "\n";
1137 }
1138 }
1139}
1140
1141#ifndef NDEBUG
1142static bool isGlobalVarSummary(const ModuleSummaryIndex &Index, ValueInfo VI) {
1143 auto SL = VI.getSummaryList();
1144 return SL.empty()
1145 ? false
1146 : SL[0]->getSummaryKind() == GlobalValueSummary::GlobalVarKind;
1147}
1148
1151 if (const auto &VI = Index.getValueInfo(G))
1152 return isGlobalVarSummary(Index, VI);
1153 return false;
1154}
1155
1156// Return the number of global variable summaries in ExportSet.
1157static unsigned
1159 FunctionImporter::ExportSetTy &ExportSet) {
1160 unsigned NumGVS = 0;
1161 for (auto &VI : ExportSet)
1162 if (isGlobalVarSummary(Index, VI.getGUID()))
1163 ++NumGVS;
1164 return NumGVS;
1165}
1166
1168 unsigned NumGVS = 0;
1169 unsigned DefinedFS = 0;
1170 unsigned Count = 0;
1171};
1172
1173// Compute import statistics for each source module in ImportList.
1176 const FunctionImporter::ImportMapTy &ImportList) {
1178
1179 for (const auto &[FromModule, GUID, Type] : ImportList) {
1180 ImportStatistics &Entry = Histogram[FromModule];
1181 ++Entry.Count;
1182 if (isGlobalVarSummary(Index, GUID))
1183 ++Entry.NumGVS;
1185 ++Entry.DefinedFS;
1186 }
1187 return Histogram;
1188}
1189#endif
1190
1191#ifndef NDEBUG
1193 const ModuleSummaryIndex &Index,
1196 DenseSet<GlobalValue::GUID> FlattenedImports;
1197
1198 for (const auto &ImportPerModule : ImportLists)
1199 for (const auto &[FromModule, GUID, ImportType] : ImportPerModule.second)
1200 FlattenedImports.insert(GUID);
1201
1202 // Checks that all GUIDs of read/writeonly vars we see in export lists
1203 // are also in the import lists. Otherwise we my face linker undefs,
1204 // because readonly and writeonly vars are internalized in their
1205 // source modules. The exception would be if it has a linkage type indicating
1206 // that there may have been a copy existing in the importing module (e.g.
1207 // linkonce_odr). In that case we cannot accurately do this checking.
1208 auto IsReadOrWriteOnlyVarNeedingImporting = [&](StringRef ModulePath,
1209 const ValueInfo &VI) {
1211 Index.findSummaryInModule(VI, ModulePath));
1212 return GVS && (Index.isReadOnly(GVS) || Index.isWriteOnly(GVS)) &&
1213 !(GVS->linkage() == GlobalValue::AvailableExternallyLinkage ||
1214 GVS->linkage() == GlobalValue::WeakODRLinkage ||
1215 GVS->linkage() == GlobalValue::LinkOnceODRLinkage);
1216 };
1217
1218 for (auto &ExportPerModule : ExportLists)
1219 for (auto &VI : ExportPerModule.second)
1220 if (!FlattenedImports.count(VI.getGUID()) &&
1221 IsReadOrWriteOnlyVarNeedingImporting(ExportPerModule.first, VI))
1222 return false;
1223
1224 return true;
1225}
1226#endif
1227
1228/// Compute all the import and export for every module using the Index.
1230 const ModuleSummaryIndex &Index,
1231 const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1233 isPrevailing,
1236 auto MIS = ModuleImportsManager::create(isPrevailing, Index, &ExportLists);
1237 // For each module that has function defined, compute the import/export lists.
1238 for (const auto &DefinedGVSummaries : ModuleToDefinedGVSummaries) {
1239 auto &ImportList = ImportLists[DefinedGVSummaries.first];
1240 LLVM_DEBUG(dbgs() << "Computing import for Module '"
1241 << DefinedGVSummaries.first << "'\n");
1242 MIS->computeImportForModule(DefinedGVSummaries.second,
1243 DefinedGVSummaries.first, ImportList);
1244 }
1245
1246 // When computing imports we only added the variables and functions being
1247 // imported to the export list. We also need to mark any references and calls
1248 // they make as exported as well. We do this here, as it is more efficient
1249 // since we may import the same values multiple times into different modules
1250 // during the import computation.
1251 for (auto &ELI : ExportLists) {
1252 // `NewExports` tracks the VI that gets exported because the full definition
1253 // of its user/referencer gets exported.
1255 const auto &DefinedGVSummaries =
1256 ModuleToDefinedGVSummaries.lookup(ELI.first);
1257 for (auto &EI : ELI.second) {
1258 // Find the copy defined in the exporting module so that we can mark the
1259 // values it references in that specific definition as exported.
1260 // Below we will add all references and called values, without regard to
1261 // whether they are also defined in this module. We subsequently prune the
1262 // list to only include those defined in the exporting module, see comment
1263 // there as to why.
1264 auto DS = DefinedGVSummaries.find(EI.getGUID());
1265 // Anything marked exported during the import computation must have been
1266 // defined in the exporting module.
1267 assert(DS != DefinedGVSummaries.end());
1268 auto *S = DS->getSecond();
1269 S = S->getBaseObject();
1270 if (auto *GVS = dyn_cast<GlobalVarSummary>(S)) {
1271 // Export referenced functions and variables. We don't export/promote
1272 // objects referenced by writeonly variable initializer, because
1273 // we convert such variables initializers to "zeroinitializer".
1274 // See processGlobalForThinLTO.
1275 if (!Index.isWriteOnly(GVS))
1276 NewExports.insert_range(GVS->refs());
1277 } else {
1278 auto *FS = cast<FunctionSummary>(S);
1279 NewExports.insert_range(llvm::make_first_range(FS->calls()));
1280 NewExports.insert_range(FS->refs());
1281 }
1282 }
1283 // Prune list computed above to only include values defined in the
1284 // exporting module. We do this after the above insertion since we may hit
1285 // the same ref/call target multiple times in above loop, and it is more
1286 // efficient to avoid a set lookup each time.
1287 for (auto EI = NewExports.begin(); EI != NewExports.end();) {
1288 if (!DefinedGVSummaries.count(EI->getGUID()))
1289 NewExports.erase(EI++);
1290 else
1291 ++EI;
1292 }
1293 ELI.second.insert_range(NewExports);
1294 }
1295
1296 assert(checkVariableImport(Index, ImportLists, ExportLists));
1297#ifndef NDEBUG
1298 LLVM_DEBUG(dbgs() << "Import/Export lists for " << ImportLists.size()
1299 << " modules:\n");
1300 for (const auto &ModuleImports : ImportLists) {
1301 auto ModName = ModuleImports.first;
1302 auto &Exports = ExportLists[ModName];
1303 unsigned NumGVS = numGlobalVarSummaries(Index, Exports);
1305 collectImportStatistics(Index, ModuleImports.second);
1306 LLVM_DEBUG(dbgs() << "* Module " << ModName << " exports "
1307 << Exports.size() - NumGVS << " functions and " << NumGVS
1308 << " vars. Imports from " << Histogram.size()
1309 << " modules.\n");
1310 for (const auto &[SrcModName, Stats] : Histogram) {
1311 LLVM_DEBUG(dbgs() << " - " << Stats.DefinedFS
1312 << " function definitions and "
1313 << Stats.Count - Stats.NumGVS - Stats.DefinedFS
1314 << " function declarations imported from " << SrcModName
1315 << "\n");
1316 LLVM_DEBUG(dbgs() << " - " << Stats.NumGVS
1317 << " global vars imported from " << SrcModName << "\n");
1318 }
1319 }
1320#endif
1321}
1322
1323#ifndef NDEBUG
1325 StringRef ModulePath,
1326 FunctionImporter::ImportMapTy &ImportList) {
1328 collectImportStatistics(Index, ImportList);
1329 LLVM_DEBUG(dbgs() << "* Module " << ModulePath << " imports from "
1330 << Histogram.size() << " modules.\n");
1331 for (const auto &[SrcModName, Stats] : Histogram) {
1332 LLVM_DEBUG(dbgs() << " - " << Stats.DefinedFS
1333 << " function definitions and "
1334 << Stats.Count - Stats.DefinedFS - Stats.NumGVS
1335 << " function declarations imported from " << SrcModName
1336 << "\n");
1337 LLVM_DEBUG(dbgs() << " - " << Stats.NumGVS << " vars imported from "
1338 << SrcModName << "\n");
1339 }
1340}
1341#endif
1342
1343/// Compute all the imports for the given module using the Index.
1344///
1345/// \p isPrevailing is a callback that will be called with a global value's GUID
1346/// and summary and should return whether the module corresponding to the
1347/// summary contains the linker-prevailing copy of that value.
1348///
1349/// \p ImportList will be populated with a map that can be passed to
1350/// FunctionImporter::importFunctions() above (see description there).
1352 StringRef ModulePath,
1354 isPrevailing,
1355 const ModuleSummaryIndex &Index,
1356 FunctionImporter::ImportMapTy &ImportList) {
1357 // Collect the list of functions this module defines.
1358 // GUID -> Summary
1359 GVSummaryMapTy FunctionSummaryMap;
1360 Index.collectDefinedFunctionsForModule(ModulePath, FunctionSummaryMap);
1361
1362 // Compute the import list for this module.
1363 LLVM_DEBUG(dbgs() << "Computing import for Module '" << ModulePath << "'\n");
1364 auto MIS = ModuleImportsManager::create(isPrevailing, Index);
1365 MIS->computeImportForModule(FunctionSummaryMap, ModulePath, ImportList);
1366
1367#ifndef NDEBUG
1368 dumpImportListForModule(Index, ModulePath, ImportList);
1369#endif
1370}
1371
1372/// Mark all external summaries in \p Index for import into the given module.
1373/// Used for testing the case of distributed builds using a distributed index.
1374///
1375/// \p ImportList will be populated with a map that can be passed to
1376/// FunctionImporter::importFunctions() above (see description there).
1378 StringRef ModulePath, const ModuleSummaryIndex &Index,
1379 FunctionImporter::ImportMapTy &ImportList) {
1380 for (const auto &GlobalList : Index) {
1381 // Ignore entries for undefined references.
1382 if (GlobalList.second.getSummaryList().empty())
1383 continue;
1384
1385 auto GUID = GlobalList.first;
1386 assert(GlobalList.second.getSummaryList().size() == 1 &&
1387 "Expected individual combined index to have one summary per GUID");
1388 auto &Summary = GlobalList.second.getSummaryList()[0];
1389 // Skip the summaries for the importing module. These are included to
1390 // e.g. record required linkage changes.
1391 if (Summary->modulePath() == ModulePath)
1392 continue;
1393 // Add an entry to provoke importing by thinBackend.
1394 ImportList.addGUID(Summary->modulePath(), GUID, Summary->importType());
1395 }
1396#ifndef NDEBUG
1397 dumpImportListForModule(Index, ModulePath, ImportList);
1398#endif
1399}
1400
1401// For SamplePGO, the indirect call targets for local functions will
1402// have its original name annotated in profile. We try to find the
1403// corresponding PGOFuncName as the GUID, and fix up the edges
1404// accordingly.
1406 FunctionSummary *FS) {
1407 for (auto &EI : FS->mutableCalls()) {
1408 if (!EI.first.getSummaryList().empty())
1409 continue;
1410 auto GUID = Index.getGUIDFromOriginalID(EI.first.getGUID());
1411 if (GUID == 0)
1412 continue;
1413 // Update the edge to point directly to the correct GUID.
1414 auto VI = Index.getValueInfo(GUID);
1415 if (llvm::any_of(
1416 VI.getSummaryList(),
1417 [&](const std::unique_ptr<GlobalValueSummary> &SummaryPtr) {
1418 // The mapping from OriginalId to GUID may return a GUID
1419 // that corresponds to a static variable. Filter it out here.
1420 // This can happen when
1421 // 1) There is a call to a library function which is not defined
1422 // in the index.
1423 // 2) There is a static variable with the OriginalGUID identical
1424 // to the GUID of the library function in 1);
1425 // When this happens the static variable in 2) will be found,
1426 // which needs to be filtered out.
1427 return SummaryPtr->getSummaryKind() ==
1428 GlobalValueSummary::GlobalVarKind;
1429 }))
1430 continue;
1431 EI.first = VI;
1432 }
1433}
1434
1436 for (const auto &Entry : Index) {
1437 for (const auto &S : Entry.second.getSummaryList()) {
1438 if (auto *FS = dyn_cast<FunctionSummary>(S.get()))
1440 }
1441 }
1442}
1443
1445 ModuleSummaryIndex &Index,
1446 const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
1448 assert(!Index.withGlobalValueDeadStripping());
1449 if (!ComputeDead ||
1450 // Don't do anything when nothing is live, this is friendly with tests.
1451 GUIDPreservedSymbols.empty()) {
1452 // Still need to update indirect calls.
1453 updateIndirectCalls(Index);
1454 return;
1455 }
1456 unsigned LiveSymbols = 0;
1458 Worklist.reserve(GUIDPreservedSymbols.size() * 2);
1459 for (auto GUID : GUIDPreservedSymbols) {
1460 ValueInfo VI = Index.getValueInfo(GUID);
1461 if (!VI)
1462 continue;
1463 for (const auto &S : VI.getSummaryList())
1464 S->setLive(true);
1465 }
1466
1467 // Add values flagged in the index as live roots to the worklist.
1468 for (const auto &Entry : Index) {
1469 auto VI = Index.getValueInfo(Entry);
1470 for (const auto &S : Entry.second.getSummaryList()) {
1471 if (auto *FS = dyn_cast<FunctionSummary>(S.get()))
1473 if (S->isLive()) {
1474 LLVM_DEBUG(dbgs() << "Live root: " << VI << "\n");
1475 Worklist.push_back(VI);
1476 ++LiveSymbols;
1477 break;
1478 }
1479 }
1480 }
1481
1482 // Make value live and add it to the worklist if it was not live before.
1483 auto visit = [&](ValueInfo VI, bool IsAliasee) {
1484 // FIXME: If we knew which edges were created for indirect call profiles,
1485 // we could skip them here. Any that are live should be reached via
1486 // other edges, e.g. reference edges. Otherwise, using a profile collected
1487 // on a slightly different binary might provoke preserving, importing
1488 // and ultimately promoting calls to functions not linked into this
1489 // binary, which increases the binary size unnecessarily. Note that
1490 // if this code changes, the importer needs to change so that edges
1491 // to functions marked dead are skipped.
1492
1493 if (llvm::any_of(VI.getSummaryList(),
1494 [](const std::unique_ptr<llvm::GlobalValueSummary> &S) {
1495 return S->isLive();
1496 }))
1497 return;
1498
1499 // We only keep live symbols that are known to be non-prevailing if any are
1500 // available_externally, linkonceodr, weakodr. Those symbols are discarded
1501 // later in the EliminateAvailableExternally pass and setting them to
1502 // not-live could break downstreams users of liveness information (PR36483)
1503 // or limit optimization opportunities.
1504 if (isPrevailing(VI.getGUID()) == PrevailingType::No) {
1505 bool KeepAliveLinkage = false;
1506 bool Interposable = false;
1507 for (const auto &S : VI.getSummaryList()) {
1508 if (S->linkage() == GlobalValue::AvailableExternallyLinkage ||
1509 S->linkage() == GlobalValue::WeakODRLinkage ||
1510 S->linkage() == GlobalValue::LinkOnceODRLinkage)
1511 KeepAliveLinkage = true;
1512 else if (GlobalValue::isInterposableLinkage(S->linkage()))
1513 Interposable = true;
1514 }
1515
1516 if (!IsAliasee) {
1517 if (!KeepAliveLinkage)
1518 return;
1519
1520 if (Interposable)
1522 "Interposable and available_externally/linkonce_odr/weak_odr "
1523 "symbol");
1524 }
1525 }
1526
1527 for (const auto &S : VI.getSummaryList())
1528 S->setLive(true);
1529 ++LiveSymbols;
1530 Worklist.push_back(VI);
1531 };
1532
1533 while (!Worklist.empty()) {
1534 auto VI = Worklist.pop_back_val();
1535 for (const auto &Summary : VI.getSummaryList()) {
1536 if (auto *AS = dyn_cast<AliasSummary>(Summary.get())) {
1537 // If this is an alias, visit the aliasee VI to ensure that all copies
1538 // are marked live and it is added to the worklist for further
1539 // processing of its references.
1540 visit(AS->getAliaseeVI(), true);
1541 continue;
1542 }
1543 for (auto Ref : Summary->refs())
1544 visit(Ref, false);
1545 if (auto *FS = dyn_cast<FunctionSummary>(Summary.get()))
1546 for (auto Call : FS->calls())
1547 visit(Call.first, false);
1548 }
1549 }
1550 Index.setWithGlobalValueDeadStripping();
1551
1552 unsigned DeadSymbols = Index.size() - LiveSymbols;
1553 LLVM_DEBUG(dbgs() << LiveSymbols << " symbols Live, and " << DeadSymbols
1554 << " symbols Dead \n");
1555 NumDeadSymbols += DeadSymbols;
1556 NumLiveSymbols += LiveSymbols;
1557}
1558
1559// Compute dead symbols and propagate constants in combined index.
1561 ModuleSummaryIndex &Index,
1562 const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
1564 bool ImportEnabled) {
1565 llvm::TimeTraceScope timeScope("Drop dead symbols and propagate attributes");
1566 computeDeadSymbolsAndUpdateIndirectCalls(Index, GUIDPreservedSymbols,
1567 isPrevailing);
1568 if (ImportEnabled)
1569 Index.propagateAttributes(GUIDPreservedSymbols);
1570}
1571
1572/// Compute the set of summaries needed for a ThinLTO backend compilation of
1573/// \p ModulePath.
1575 StringRef ModulePath,
1576 const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1577 const FunctionImporter::ImportMapTy &ImportList,
1578 ModuleToSummariesForIndexTy &ModuleToSummariesForIndex,
1579 GVSummaryPtrSet &DecSummaries) {
1580 // Include all summaries from the importing module.
1581 ModuleToSummariesForIndex[std::string(ModulePath)] =
1582 ModuleToDefinedGVSummaries.lookup(ModulePath);
1583
1584 // Forward port the heterogeneous std::map::operator[]() from C++26, which
1585 // lets us look up the map without allocating an instance of std::string when
1586 // the key-value pair exists in the map.
1587 // TODO: Remove this in favor of the heterogenous std::map::operator[]() from
1588 // C++26 when it becomes available for our codebase.
1589 auto LookupOrCreate = [](ModuleToSummariesForIndexTy &Map,
1591 auto It = Map.find(Key);
1592 if (It == Map.end())
1593 std::tie(It, std::ignore) =
1594 Map.try_emplace(std::string(Key), GVSummaryMapTy());
1595 return It->second;
1596 };
1597
1598 // Include summaries for imports.
1599 for (const auto &[FromModule, GUID, ImportType] : ImportList) {
1600 auto &SummariesForIndex =
1601 LookupOrCreate(ModuleToSummariesForIndex, FromModule);
1602
1603 const auto &DefinedGVSummaries = ModuleToDefinedGVSummaries.at(FromModule);
1604 const auto &DS = DefinedGVSummaries.find(GUID);
1605 assert(DS != DefinedGVSummaries.end() &&
1606 "Expected a defined summary for imported global value");
1607 if (ImportType == GlobalValueSummary::Declaration)
1608 DecSummaries.insert(DS->second);
1609
1610 SummariesForIndex[GUID] = DS->second;
1611 }
1612
1613 // When AlwaysRenamePromotedLocals is false, for each source module we import
1614 // from, also include summaries for local functions that have
1615 // NoRenameOnPromotion set. This is needed for distributed ThinLTO. Otherwise,
1616 // the local function of the source module will keep its origin name, e.g.,
1617 // foo() while the function in destination module will have name
1618 // foo.llvm.<...>() and this will cause a link failure.
1619 //
1620 // Note: this imports a superset of the necessary declarations — all locals
1621 // with NoRenameOnPromotion in each source module, not just those referenced
1622 // by the importing module. Computing the precise set would require walking
1623 // the summary reference graph from each imported function, which is more
1624 // expensive than the simple scan here.
1626 for (auto &[ModPath, SummariesForIndex] : ModuleToSummariesForIndex) {
1627 if (ModPath == ModulePath)
1628 continue;
1629 auto It = ModuleToDefinedGVSummaries.find(ModPath);
1630 if (It == ModuleToDefinedGVSummaries.end())
1631 continue;
1632 for (const auto &[GUID, Summary] : It->second) {
1633 if (Summary->noRenameOnPromotion()) {
1634 DecSummaries.insert(Summary);
1635 SummariesForIndex.try_emplace(GUID, Summary);
1636 }
1637 }
1638 }
1639 }
1640}
1641
1642/// Emit the files \p ModulePath will import from into \p OutputFilename.
1645 const ModuleToSummariesForIndexTy &ModuleToSummariesForIndex) {
1646 std::error_code EC;
1648 if (EC)
1649 return createFileError("cannot open " + OutputFilename,
1650 errorCodeToError(EC));
1651 processImportsFiles(ModulePath, ModuleToSummariesForIndex,
1652 [&](StringRef M) { ImportsOS << M << "\n"; });
1653 return Error::success();
1654}
1655
1656/// Invoke callback \p F on the file paths from which \p ModulePath
1657/// will import.
1659 StringRef ModulePath,
1660 const ModuleToSummariesForIndexTy &ModuleToSummariesForIndex,
1661 function_ref<void(const std::string &)> F) {
1662 for (const auto &ILI : ModuleToSummariesForIndex)
1663 // The ModuleToSummariesForIndex map includes an entry for the current
1664 // Module (needed for writing out the index files). We don't want to
1665 // include it in the imports file, however, so filter it out.
1666 if (ILI.first != ModulePath)
1667 F(ILI.first);
1668}
1669
1671 LLVM_DEBUG(dbgs() << "Converting to a declaration: `" << GV.getName()
1672 << "\n");
1673 if (Function *F = dyn_cast<Function>(&GV)) {
1674 F->deleteBody();
1675 F->clearMetadata();
1676 F->setComdat(nullptr);
1677 } else if (GlobalVariable *V = dyn_cast<GlobalVariable>(&GV)) {
1678 V->setInitializer(nullptr);
1679 V->setLinkage(GlobalValue::ExternalLinkage);
1680 V->clearMetadata();
1681 V->setComdat(nullptr);
1682 } else {
1683 GlobalValue *NewGV;
1684 if (GV.getValueType()->isFunctionTy())
1685 NewGV =
1688 "", GV.getParent());
1689 else
1690 NewGV =
1691 new GlobalVariable(*GV.getParent(), GV.getValueType(),
1692 /*isConstant*/ false, GlobalValue::ExternalLinkage,
1693 /*init*/ nullptr, "",
1694 /*insertbefore*/ nullptr, GV.getThreadLocalMode(),
1695 GV.getType()->getAddressSpace());
1696 NewGV->takeName(&GV);
1697 GV.replaceAllUsesWith(NewGV);
1698 return false;
1699 }
1700 if (!GV.isImplicitDSOLocal())
1701 GV.setDSOLocal(false);
1702 return true;
1703}
1704
1706 const GVSummaryMapTy &DefinedGlobals,
1707 bool PropagateAttrs) {
1708 llvm::TimeTraceScope timeScope("ThinLTO finalize in module");
1709 DenseSet<Comdat *> NonPrevailingComdats;
1710 auto FinalizeInModule = [&](GlobalValue &GV, bool Propagate = false) {
1711 // See if the global summary analysis computed a new resolved linkage.
1712 const auto &GS = DefinedGlobals.find(GV.getGUID());
1713 if (GS == DefinedGlobals.end())
1714 return;
1715
1716 if (Propagate)
1717 if (FunctionSummary *FS = dyn_cast<FunctionSummary>(GS->second)) {
1718 if (Function *F = dyn_cast<Function>(&GV)) {
1719 // TODO: propagate ReadNone and ReadOnly.
1720 if (FS->fflags().ReadNone && !F->doesNotAccessMemory())
1721 F->setDoesNotAccessMemory();
1722
1723 if (FS->fflags().ReadOnly && !F->onlyReadsMemory())
1724 F->setOnlyReadsMemory();
1725
1726 if (FS->fflags().NoRecurse && !F->doesNotRecurse())
1727 F->setDoesNotRecurse();
1728
1729 if (FS->fflags().NoUnwind && !F->doesNotThrow())
1730 F->setDoesNotThrow();
1731 }
1732 }
1733
1734 auto NewLinkage = GS->second->linkage();
1736 // Don't internalize anything here, because the code below
1737 // lacks necessary correctness checks. Leave this job to
1738 // LLVM 'internalize' pass.
1739 GlobalValue::isLocalLinkage(NewLinkage) ||
1740 // In case it was dead and already converted to declaration.
1741 GV.isDeclaration())
1742 return;
1743
1744 // Set the potentially more constraining visibility computed from summaries.
1745 // The DefaultVisibility condition is because older GlobalValueSummary does
1746 // not record DefaultVisibility and we don't want to change protected/hidden
1747 // to default.
1748 if (GS->second->getVisibility() != GlobalValue::DefaultVisibility)
1749 GV.setVisibility(GS->second->getVisibility());
1750
1751 if (NewLinkage == GV.getLinkage())
1752 return;
1753
1754 // Check for a non-prevailing def that has interposable linkage
1755 // (e.g. non-odr weak or linkonce). In that case we can't simply
1756 // convert to available_externally, since it would lose the
1757 // interposable property and possibly get inlined. Simply drop
1758 // the definition in that case.
1761 if (!convertToDeclaration(GV))
1762 // FIXME: Change this to collect replaced GVs and later erase
1763 // them from the parent module once thinLTOResolvePrevailingGUID is
1764 // changed to enable this for aliases.
1765 llvm_unreachable("Expected GV to be converted");
1766 } else {
1767 // If all copies of the original symbol had global unnamed addr and
1768 // linkonce_odr linkage, or if all of them had local unnamed addr linkage
1769 // and are constants, then it should be an auto hide symbol. In that case
1770 // the thin link would have marked it as CanAutoHide. Add hidden
1771 // visibility to the symbol to preserve the property.
1772 if (NewLinkage == GlobalValue::WeakODRLinkage &&
1773 GS->second->canAutoHide()) {
1776 }
1777
1778 LLVM_DEBUG(dbgs() << "ODR fixing up linkage for `" << GV.getName()
1779 << "` from " << GV.getLinkage() << " to " << NewLinkage
1780 << "\n");
1781 GV.setLinkage(NewLinkage);
1782 }
1783 // Remove declarations from comdats, including available_externally
1784 // as this is a declaration for the linker, and will be dropped eventually.
1785 // It is illegal for comdats to contain declarations.
1786 auto *GO = dyn_cast_or_null<GlobalObject>(&GV);
1787 if (GO && GO->isDeclarationForLinker() && GO->hasComdat()) {
1788 if (GO->getComdat()->getName() == GO->getName())
1789 NonPrevailingComdats.insert(GO->getComdat());
1790 GO->setComdat(nullptr);
1791 }
1792 };
1793
1794 // Process functions and global now
1795 for (auto &GV : TheModule)
1796 FinalizeInModule(GV, PropagateAttrs);
1797 for (auto &GV : TheModule.globals())
1798 FinalizeInModule(GV);
1799 for (auto &GV : TheModule.aliases())
1800 FinalizeInModule(GV);
1801
1802 // For a non-prevailing comdat, all its members must be available_externally.
1803 // FinalizeInModule has handled non-local-linkage GlobalValues. Here we handle
1804 // local linkage GlobalValues.
1805 if (NonPrevailingComdats.empty())
1806 return;
1807 for (auto &GO : TheModule.global_objects()) {
1808 if (auto *C = GO.getComdat(); C && NonPrevailingComdats.count(C)) {
1809 GO.setComdat(nullptr);
1811 }
1812 }
1813 bool Changed;
1814 do {
1815 Changed = false;
1816 // If an alias references a GlobalValue in a non-prevailing comdat, change
1817 // it to available_externally. For simplicity we only handle GlobalValue and
1818 // ConstantExpr with a base object. ConstantExpr without a base object is
1819 // unlikely used in a COMDAT.
1820 for (auto &GA : TheModule.aliases()) {
1821 if (GA.hasAvailableExternallyLinkage())
1822 continue;
1823 GlobalObject *Obj = GA.getAliaseeObject();
1824 assert(Obj && "aliasee without an base object is unimplemented");
1825 if (Obj->hasAvailableExternallyLinkage()) {
1827 Changed = true;
1828 }
1829 }
1830 } while (Changed);
1831}
1832
1833/// Run internalization on \p TheModule based on symmary analysis.
1835 const GVSummaryMapTy &DefinedGlobals) {
1836 llvm::TimeTraceScope timeScope("ThinLTO internalize module");
1837 // Declare a callback for the internalize pass that will ask for every
1838 // candidate GlobalValue if it can be internalized or not.
1839 auto MustPreserveGV = [&](const GlobalValue &GV) -> bool {
1840 // It may be the case that GV is on a chain of an ifunc, its alias and
1841 // subsequent aliases. In this case, the summary for the value is not
1842 // available.
1843 if (isa<GlobalIFunc>(&GV) ||
1844 (isa<GlobalAlias>(&GV) &&
1845 isa<GlobalIFunc>(cast<GlobalAlias>(&GV)->getAliaseeObject())))
1846 return true;
1847
1848 // Lookup the linkage recorded in the summaries during global analysis.
1849 auto GS = DefinedGlobals.find(GV.getGUID());
1850 if (GS == DefinedGlobals.end()) {
1851 // Must have been promoted (possibly conservatively). Find original
1852 // name so that we can access the correct summary and see if it can
1853 // be internalized again.
1854 // FIXME: Eventually we should control promotion instead of promoting
1855 // and internalizing again.
1856 StringRef OrigName =
1858 std::string OrigId = GlobalValue::getGlobalIdentifier(
1860 TheModule.getSourceFileName());
1861 GS = DefinedGlobals.find(
1863 if (GS == DefinedGlobals.end()) {
1864 // Also check the original non-promoted non-globalized name. In some
1865 // cases a preempted weak value is linked in as a local copy because
1866 // it is referenced by an alias (IRLinker::linkGlobalValueProto).
1867 // In that case, since it was originally not a local value, it was
1868 // recorded in the index using the original name.
1869 // FIXME: This may not be needed once PR27866 is fixed.
1870 GS = DefinedGlobals.find(
1872 assert(GS != DefinedGlobals.end());
1873 }
1874 }
1875 return !GlobalValue::isLocalLinkage(GS->second->linkage());
1876 };
1877
1878 // FIXME: See if we can just internalize directly here via linkage changes
1879 // based on the index, rather than invoking internalizeModule.
1880 internalizeModule(TheModule, MustPreserveGV);
1881}
1882
1883/// Make alias a clone of its aliasee.
1886
1887 ValueToValueMapTy VMap;
1888 Function *NewFn = CloneFunction(Fn, VMap);
1889 // Clone should use the original alias's linkage, visibility and name, and we
1890 // ensure all uses of alias instead use the new clone (casted if necessary).
1891 NewFn->setLinkage(GA->getLinkage());
1892 NewFn->setVisibility(GA->getVisibility());
1893 GA->replaceAllUsesWith(NewFn);
1894 NewFn->takeName(GA);
1895 return NewFn;
1896}
1897
1898// Internalize values that we marked with specific attribute
1899// in processGlobalForThinLTO.
1901 for (auto &GV : M.globals())
1902 // Skip GVs which have been converted to declarations
1903 // by dropDeadSymbols.
1904 if (!GV.isDeclaration() && GV.hasAttribute("thinlto-internalize")) {
1905 GV.setLinkage(GlobalValue::InternalLinkage);
1906 GV.setVisibility(GlobalValue::DefaultVisibility);
1907 }
1908}
1909
1910// Automatically import functions in Module \p DestModule based on the summaries
1911// index.
1913 Module &DestModule, const FunctionImporter::ImportMapTy &ImportList) {
1914 LLVM_DEBUG(dbgs() << "Starting import for Module "
1915 << DestModule.getModuleIdentifier() << "\n");
1916 unsigned ImportedCount = 0, ImportedGVCount = 0;
1917 // Before carrying out any imports, see if this module defines functions in
1918 // MoveSymbolGUID. If it does, delete them here (but leave the declaration).
1919 // The function will be imported elsewhere, as extenal linkage, and the
1920 // destination doesn't yet have its definition.
1921 DenseSet<GlobalValue::GUID> MoveSymbolGUIDSet;
1922 MoveSymbolGUIDSet.insert_range(MoveSymbolGUID);
1923 for (auto &F : DestModule)
1924 if (!F.isDeclaration() && MoveSymbolGUIDSet.contains(F.getGUID()))
1925 F.deleteBody();
1926
1927 IRMover Mover(DestModule);
1928
1929 // Do the actual import of functions now, one Module at a time
1930 for (const auto &ModName : ImportList.getSourceModules()) {
1931 llvm::TimeTraceScope timeScope("Import", ModName);
1932 // Get the module for the import
1933 Expected<std::unique_ptr<Module>> SrcModuleOrErr = ModuleLoader(ModName);
1934 if (!SrcModuleOrErr)
1935 return SrcModuleOrErr.takeError();
1936 std::unique_ptr<Module> SrcModule = std::move(*SrcModuleOrErr);
1937 assert(&DestModule.getContext() == &SrcModule->getContext() &&
1938 "Context mismatch");
1939
1940 // If modules were created with lazy metadata loading, materialize it
1941 // now, before linking it (otherwise this will be a noop).
1942 if (Error Err = SrcModule->materializeMetadata())
1943 return std::move(Err);
1944
1945 // Find the globals to import
1946 SetVector<GlobalValue *> GlobalsToImport;
1947 {
1948 llvm::TimeTraceScope functionsScope("Functions");
1949 for (Function &F : *SrcModule) {
1950 if (!F.hasName())
1951 continue;
1952 auto GUID = F.getGUID();
1953 auto MaybeImportType = ImportList.getImportType(ModName, GUID);
1954 bool ImportDefinition =
1955 MaybeImportType == GlobalValueSummary::Definition;
1956
1957 LLVM_DEBUG(dbgs() << (MaybeImportType ? "Is" : "Not")
1958 << " importing function"
1959 << (ImportDefinition
1960 ? " definition "
1961 : (MaybeImportType ? " declaration " : " "))
1962 << GUID << " " << F.getName() << " from "
1963 << SrcModule->getSourceFileName() << "\n");
1964 if (ImportDefinition) {
1965 if (Error Err = F.materialize())
1966 return std::move(Err);
1967 // MemProf should match function's definition and summary,
1968 // 'thinlto_src_module' is needed.
1970 // Add 'thinlto_src_module' and 'thinlto_src_file' metadata for
1971 // statistics and debugging.
1972 F.setMetadata(
1973 "thinlto_src_module",
1974 MDNode::get(DestModule.getContext(),
1975 {MDString::get(DestModule.getContext(),
1976 SrcModule->getModuleIdentifier())}));
1977 F.setMetadata(
1978 "thinlto_src_file",
1979 MDNode::get(DestModule.getContext(),
1980 {MDString::get(DestModule.getContext(),
1981 SrcModule->getSourceFileName())}));
1982 }
1983 GlobalsToImport.insert(&F);
1984 }
1985 }
1986 }
1987 {
1988 llvm::TimeTraceScope globalsScope("Globals");
1989 for (GlobalVariable &GV : SrcModule->globals()) {
1990 if (!GV.hasName())
1991 continue;
1992 auto GUID = GV.getGUID();
1993 auto MaybeImportType = ImportList.getImportType(ModName, GUID);
1994 bool ImportDefinition =
1995 MaybeImportType == GlobalValueSummary::Definition;
1996
1997 LLVM_DEBUG(dbgs() << (MaybeImportType ? "Is" : "Not")
1998 << " importing global"
1999 << (ImportDefinition
2000 ? " definition "
2001 : (MaybeImportType ? " declaration " : " "))
2002 << GUID << " " << GV.getName() << " from "
2003 << SrcModule->getSourceFileName() << "\n");
2004 if (ImportDefinition) {
2005 if (Error Err = GV.materialize())
2006 return std::move(Err);
2007 ImportedGVCount += GlobalsToImport.insert(&GV);
2008 }
2009 }
2010 }
2011 {
2012 llvm::TimeTraceScope aliasesScope("Aliases");
2013 for (GlobalAlias &GA : SrcModule->aliases()) {
2014 if (!GA.hasName() || isa<GlobalIFunc>(GA.getAliaseeObject()))
2015 continue;
2016 auto GUID = GA.getGUID();
2017 auto MaybeImportType = ImportList.getImportType(ModName, GUID);
2018 bool ImportDefinition =
2019 MaybeImportType == GlobalValueSummary::Definition;
2020
2021 LLVM_DEBUG(dbgs() << (MaybeImportType ? "Is" : "Not")
2022 << " importing alias"
2023 << (ImportDefinition
2024 ? " definition "
2025 : (MaybeImportType ? " declaration " : " "))
2026 << GUID << " " << GA.getName() << " from "
2027 << SrcModule->getSourceFileName() << "\n");
2028 if (ImportDefinition) {
2029 if (Error Err = GA.materialize())
2030 return std::move(Err);
2031 // Import alias as a copy of its aliasee.
2032 GlobalObject *GO = GA.getAliaseeObject();
2033 if (Error Err = GO->materialize())
2034 return std::move(Err);
2035 auto *Fn = replaceAliasWithAliasee(SrcModule.get(), &GA);
2036 LLVM_DEBUG(dbgs() << "Is importing aliasee fn " << GO->getGUID()
2037 << " " << GO->getName() << " from "
2038 << SrcModule->getSourceFileName() << "\n");
2040 // Add 'thinlto_src_module' and 'thinlto_src_file' metadata for
2041 // statistics and debugging.
2042 Fn->setMetadata(
2043 "thinlto_src_module",
2044 MDNode::get(DestModule.getContext(),
2045 {MDString::get(DestModule.getContext(),
2046 SrcModule->getModuleIdentifier())}));
2047 Fn->setMetadata(
2048 "thinlto_src_file",
2049 MDNode::get(DestModule.getContext(),
2050 {MDString::get(DestModule.getContext(),
2051 SrcModule->getSourceFileName())}));
2052 }
2053 GlobalsToImport.insert(Fn);
2054 }
2055 }
2056 }
2057
2058 // Upgrade debug info after we're done materializing all the globals and we
2059 // have loaded all the required metadata!
2060 UpgradeDebugInfo(*SrcModule);
2061
2062 // Set the partial sample profile ratio in the profile summary module flag
2063 // of the imported source module, if applicable, so that the profile summary
2064 // module flag will match with that of the destination module when it's
2065 // imported.
2066 SrcModule->setPartialSampleProfileRatio(Index);
2067
2068 // Link in the specified functions.
2069 renameModuleForThinLTO(*SrcModule, Index, ClearDSOLocalOnDeclarations,
2070 &GlobalsToImport);
2071
2072 if (PrintImports) {
2073 for (const auto *GV : GlobalsToImport)
2074 dbgs() << DestModule.getSourceFileName() << ": Import " << GV->getName()
2075 << " from " << SrcModule->getSourceFileName() << "\n";
2076 }
2077
2078 if (Error Err = Mover.move(std::move(SrcModule),
2079 GlobalsToImport.getArrayRef(), nullptr,
2080 /*IsPerformingImport=*/true))
2082 Twine("Function Import: link error: ") +
2083 toString(std::move(Err)));
2084
2085 ImportedCount += GlobalsToImport.size();
2086 NumImportedModules++;
2087 }
2088
2089 internalizeGVsAfterImport(DestModule);
2090
2091 NumImportedFunctions += (ImportedCount - ImportedGVCount);
2092 NumImportedGlobalVars += ImportedGVCount;
2093
2094 // TODO: Print counters for definitions and declarations in the debugging log.
2095 LLVM_DEBUG(dbgs() << "Imported " << ImportedCount - ImportedGVCount
2096 << " functions for Module "
2097 << DestModule.getModuleIdentifier() << "\n");
2098 LLVM_DEBUG(dbgs() << "Imported " << ImportedGVCount
2099 << " global variables for Module "
2100 << DestModule.getModuleIdentifier() << "\n");
2101 return ImportedCount;
2102}
2103
2106 isPrevailing) {
2107 if (SummaryFile.empty())
2108 report_fatal_error("error: -function-import requires -summary-file\n");
2111 if (!IndexPtrOrErr) {
2112 logAllUnhandledErrors(IndexPtrOrErr.takeError(), errs(),
2113 "Error loading file '" + SummaryFile + "': ");
2114 return false;
2115 }
2116 std::unique_ptr<ModuleSummaryIndex> Index = std::move(*IndexPtrOrErr);
2117
2118 // First step is collecting the import list.
2120 FunctionImporter::ImportMapTy ImportList(ImportIDs);
2121 // If requested, simply import all functions in the index. This is used
2122 // when testing distributed backend handling via the opt tool, when
2123 // we have distributed indexes containing exactly the summaries to import.
2124 if (ImportAllIndex)
2126 *Index, ImportList);
2127 else
2128 ComputeCrossModuleImportForModuleForTest(M.getModuleIdentifier(),
2129 isPrevailing, *Index, ImportList);
2130
2131 // Conservatively mark all internal values as promoted. This interface is
2132 // only used when doing importing via the function importing pass. The pass
2133 // is only enabled when testing importing via the 'opt' tool, which does
2134 // not do the ThinLink that would normally determine what values to promote.
2135 for (auto &I : *Index) {
2136 for (auto &S : I.second.getSummaryList()) {
2137 if (GlobalValue::isLocalLinkage(S->linkage()))
2138 S->setExternalLinkageForTest();
2139 }
2140 }
2141
2142 // Next we need to promote to global scope and rename any local values that
2143 // are potentially exported to other modules.
2144 renameModuleForThinLTO(M, *Index, /*ClearDSOLocalOnDeclarations=*/false,
2145 /*GlobalsToImport=*/nullptr);
2146
2147 // Perform the import now.
2148 auto ModuleLoader = [&M](StringRef Identifier) {
2149 return loadFile(std::string(Identifier), M.getContext());
2150 };
2151 FunctionImporter Importer(*Index, ModuleLoader,
2152 /*ClearDSOLocalOnDeclarations=*/false);
2153 Expected<bool> Result = Importer.importFunctions(M, ImportList);
2154
2155 // FIXME: Probably need to propagate Errors through the pass manager.
2156 if (!Result) {
2157 logAllUnhandledErrors(Result.takeError(), errs(),
2158 "Error importing module: ");
2159 return true;
2160 }
2161
2162 return true;
2163}
2164
2167 // This is only used for testing the function import pass via opt, where we
2168 // don't have prevailing information from the LTO context available, so just
2169 // conservatively assume everything is prevailing (which is fine for the very
2170 // limited use of prevailing checking in this pass).
2171 auto isPrevailing = [](GlobalValue::GUID, const GlobalValueSummary *) {
2172 return true;
2173 };
2174 if (!doImportingForModuleForTest(M, isPrevailing))
2175 return PreservedAnalyses::all();
2176
2177 return PreservedAnalyses::none();
2178}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
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")))
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 DenseMap< StringRef, ImportStatistics > collectImportStatistics(const ModuleSummaryIndex &Index, const FunctionImporter::ImportMapTy &ImportList)
static unsigned numGlobalVarSummaries(const ModuleSummaryIndex &Index, FunctionImporter::ExportSetTy &ExportSet)
static bool checkVariableImport(const ModuleSummaryIndex &Index, FunctionImporter::ImportListsTy &ImportLists, DenseMap< StringRef, FunctionImporter::ExportSetTy > &ExportLists)
static bool doImportingForModuleForTest(Module &M, function_ref< bool(GlobalValue::GUID, const GlobalValueSummary *)> isPrevailing)
static const char * getFailureName(FunctionImporter::ImportFailureReason Reason)
static void internalizeGVsAfterImport(Module &M)
void updateValueInfoForIndirectCalls(ModuleSummaryIndex &Index, FunctionSummary *FS)
static std::unique_ptr< Module > loadFile(const std::string &FileName, LLVMContext &Context)
static bool shouldSkipLocalInAnotherModule(const GlobalValueSummary *RefSummary, size_t NumDefs, StringRef ImporterModule)
static bool isGlobalVarSummary(const ModuleSummaryIndex &Index, ValueInfo VI)
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 const GlobalValueSummary * selectCallee(const ModuleSummaryIndex &Index, ArrayRef< std::unique_ptr< GlobalValueSummary > > CalleeSummaryList, unsigned Threshold, StringRef CallerModulePath, const GlobalValueSummary *&TooLargeOrNoInlineSummary, FunctionImporter::ImportFailureReason &Reason)
Given a list of possible callee implementation for a call site, select one that fits the Threshold fo...
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.
Module.h This file contains the declarations for the Module class.
This file supports working with JSON data.
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
#define G(x, y, z)
Definition MD5.cpp:55
block placement Basic Block Placement Stats
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...
static constexpr StringLiteral Filename
Reader for contextual iFDO profile, which comes in bitstream format.
if(PassOpts->AAPipeline)
static void visit(BasicBlock &Start, std::function< bool(BasicBlock *)> op)
std::pair< BasicBlock *, BasicBlock * > Edge
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:171
#define LLVM_DEBUG(...)
Definition Debug.h:114
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)
virtual bool canImport(ValueInfo VI)
DenseMap< StringRef, FunctionImporter::ExportSetTy > *const ExportLists
virtual ~ModuleImportsManager()=default
static std::unique_ptr< ModuleImportsManager > create(function_ref< bool(GlobalValue::GUID, const GlobalValueSummary *)> IsPrevailing, const ModuleSummaryIndex &Index, DenseMap< StringRef, FunctionImporter::ExportSetTy > *ExportLists=nullptr)
function_ref< bool(GlobalValue::GUID, const GlobalValueSummary *)> IsPrevailing
ModuleImportsManager(function_ref< bool(GlobalValue::GUID, const GlobalValueSummary *)> IsPrevailing, const ModuleSummaryIndex &Index, DenseMap< StringRef, FunctionImporter::ExportSetTy > *ExportLists=nullptr)
const ModuleSummaryIndex & Index
virtual 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...
WorkloadImportsManager(function_ref< bool(GlobalValue::GUID, const GlobalValueSummary *)> IsPrevailing, const ModuleSummaryIndex &Index, DenseMap< StringRef, FunctionImporter::ExportSetTy > *ExportLists)
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
ValueT & at(const_arg_type_t< KeyT > Val)
at - Return the entry for the specified key, or abort if no such entry exists.
Definition DenseMap.h:224
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:205
iterator find(const_arg_type_t< KeyT > Val)
Definition DenseMap.h:178
unsigned size() const
Definition DenseMap.h:110
size_type count(const_arg_type_t< KeyT > Val) const
Return 1 if the specified key is in the map, 0 otherwise.
Definition DenseMap.h:174
iterator end()
Definition DenseMap.h:81
Implements a dense probed hash-table based set.
Definition DenseSet.h:279
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
Tagged union holding either a T or a Error.
Definition Error.h:485
Error takeError()
Take ownership of the stored error.
Definition Error.h:612
LLVM_ABI PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
The map maintains the list of imports.
LLVM_ABI AddDefinitionStatus addDefinition(StringRef FromModule, GlobalValue::GUID GUID)
void addGUID(StringRef FromModule, GlobalValue::GUID GUID, GlobalValueSummary::ImportKind ImportKind)
LLVM_ABI SmallVector< StringRef, 0 > getSourceModules() const
LLVM_ABI std::optional< GlobalValueSummary::ImportKind > getImportType(StringRef FromModule, GlobalValue::GUID GUID) const
LLVM_ABI void maybeAddDeclaration(StringRef FromModule, GlobalValue::GUID GUID)
The function importer is automatically importing function from other modules based on the provided su...
LLVM_ABI Expected< bool > importFunctions(Module &M, const ImportMapTy &ImportList)
Import functions in Module M based on the supplied import list.
DenseMap< GlobalValue::GUID, std::tuple< unsigned, const GlobalValueSummary *, std::unique_ptr< ImportFailureInfo > > > ImportThresholdsTy
Map of callee GUID considered for import into a given module to a pair consisting of the largest thre...
ImportFailureReason
The different reasons selectCallee will chose not to import a candidate.
DenseSet< ValueInfo > ExportSetTy
The set contains an entry for every global value that the module exports.
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:168
LLVM_ABI const GlobalObject * getAliaseeObject() const
Definition Globals.cpp:659
Function and variable summary information to aid decisions and implementation of importing.
StringRef modulePath() const
Get the path to the module containing this function.
GlobalValue::LinkageTypes linkage() const
Return linkage type recorded for this global value.
static LLVM_ABI GUID getGUIDAssumingExternalLinkage(StringRef GlobalName)
Return a 64-bit global unique ID constructed from the name of a global symbol.
Definition Globals.cpp:80
VisibilityTypes getVisibility() const
bool isImplicitDSOLocal() const
static bool isLocalLinkage(LinkageTypes Linkage)
LLVM_ABI bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition Globals.cpp:337
LinkageTypes getLinkage() const
uint64_t GUID
Declare a type to represent a global unique identifier for a global value.
ThreadLocalMode getThreadLocalMode() const
static bool isAvailableExternallyLinkage(LinkageTypes Linkage)
void setLinkage(LinkageTypes LT)
unsigned getAddressSpace() const
GUID getGUID() const
Return a 64-bit global unique ID constructed from global value name (i.e.
Module * getParent()
Get the module that this global value is contained inside of...
LLVM_ABI const GlobalObject * getAliaseeObject() const
Definition Globals.cpp:450
void setDSOLocal(bool Local)
PointerType * getType() const
Global values are always pointers.
@ DefaultVisibility
The GV is visible.
Definition GlobalValue.h:68
@ HiddenVisibility
The GV is hidden.
Definition GlobalValue.h:69
static LLVM_ABI std::string getGlobalIdentifier(StringRef Name, GlobalValue::LinkageTypes Linkage, StringRef FileName)
Return the modified name for a global value suitable to be used as the key for a global lookup (e....
Definition Globals.cpp:170
void setVisibility(VisibilityTypes V)
static bool isInterposableLinkage(LinkageTypes Linkage)
Whether the definition of this global may be replaced by something non-equivalent at link time.
LLVM_ABI Error materialize()
Make sure this GlobalValue is fully read.
Definition Globals.cpp:52
LLVM_ABI bool canBeOmittedFromSymbolTable() const
True if GV can be left out of the object symbol table.
Definition Globals.cpp:475
@ InternalLinkage
Rename collisions when linking (static functions).
Definition GlobalValue.h:60
@ WeakODRLinkage
Same, but only replaced by something equivalent.
Definition GlobalValue.h:58
@ ExternalLinkage
Externally visible function.
Definition GlobalValue.h:53
@ AvailableExternallyLinkage
Available for inspection, not emission.
Definition GlobalValue.h:54
@ LinkOnceODRLinkage
Same, but only replaced by something equivalent.
Definition GlobalValue.h:56
Type * getValueType() const
LLVM_ABI 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:1701
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition Metadata.h:1572
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFileOrSTDIN(const Twine &Filename, bool IsText=false, bool RequiresNullTerminator=true, std::optional< Align > Alignment=std::nullopt)
Open the specified file as a MemoryBuffer, or open stdin if the Filename is "-".
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:67
LLVMContext & getContext() const
Get the global data context.
Definition Module.h:285
const std::string & getSourceFileName() const
Get the module's original source file name.
Definition Module.h:263
iterator_range< alias_iterator > aliases()
Definition Module.h:735
iterator_range< global_iterator > globals()
Definition Module.h:684
const std::string & getModuleIdentifier() const
Get the module identifier which is, essentially, the name of the module.
Definition Module.h:252
iterator_range< global_object_iterator > global_objects()
Definition Module.cpp:451
LLVM_ABI Expected< PGOCtxProfile > loadProfiles()
unsigned getAddressSpace() const
Return the address space of the Pointer type.
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition Analysis.h:115
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
Instances of this class encapsulate one diagnostic report, allowing printing to a raw_ostream as a ca...
Definition SourceMgr.h:297
A vector that has set insertion semantics.
Definition SetVector.h:57
ArrayRef< value_type > getArrayRef() const
Definition SetVector.h:91
size_type size() const
Determine the number of elements in the SetVector.
Definition SetVector.h:103
Vector takeVector()
Clear the SetVector and return the underlying vector.
Definition SetVector.h:94
bool contains(const_arg_type key) const
Check if the SetVector contains the given key.
Definition SetVector.h:252
void clear()
Completely clear the SetVector.
Definition SetVector.h:267
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition SetVector.h:151
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
reference emplace_back(ArgTypes &&... Args)
void reserve(size_type N)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition StringMap.h:133
iterator end()
Definition StringMap.h:224
iterator find(StringRef Key)
Definition StringMap.h:237
size_type count(StringRef Key) const
count - Return 1 if the element is in the map, 0 otherwise.
Definition StringMap.h:285
bool insert(MapEntryTy *KeyValue)
insert - Insert the specified key/value pair into the map.
Definition StringMap.h:321
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
constexpr size_t size() const
size - Get the string size.
Definition StringRef.h:143
char front() const
front - Get the first character in the string.
Definition StringRef.h:146
size_t find(char C, size_t From=0) const
Search for the first character C in the string.
Definition StringRef.h:290
StringSet - A wrapper for StringMap that provides set-like functionality.
Definition StringSet.h:25
std::pair< typename Base::iterator, bool > insert(StringRef key)
Definition StringSet.h:39
The TimeTraceScope is a helper class to call the begin and end functions of the time trace profiler.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
bool isFunctionTy() const
True if this is an instance of FunctionType.
Definition Type.h:275
LLVM_ABI void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition Value.cpp:549
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:318
LLVM_ABI void takeName(Value *V)
Transfer the name from V to this value.
Definition Value.cpp:399
std::pair< iterator, bool > insert(const ValueT &V)
Definition DenseSet.h:202
void insert_range(Range &&R)
Definition DenseSet.h:228
size_type size() const
Definition DenseSet.h:87
bool contains(const_arg_type_t< ValueT > V) const
Check if the set contains the given element.
Definition DenseSet.h:175
bool erase(const ValueT &V)
Definition DenseSet.h:100
size_type count(const_arg_type_t< ValueT > V) const
Return 1 if the specified key is in the set, 0 otherwise.
Definition DenseSet.h:180
An efficient, type-erasing, non-owning reference to a callable.
The root is the trivial Path to the root value.
Definition JSON.h:712
A raw_ostream that writes to a file descriptor.
CallInst * Call
Changed
#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)
LLVM_ABI llvm::Expected< Value > parse(llvm::StringRef JSON)
Parses the provided JSON source, or returns a ParseError.
Definition JSON.cpp:681
bool fromJSON(const Value &E, std::string &Out, Path P)
Definition JSON.h:741
@ OF_Text
The file should be opened in text mode on platforms like z/OS that make this distinction.
Definition FileSystem.h:777
LLVM_ABI StringRef filename(StringRef path LLVM_LIFETIME_BOUND, Style style=Style::native)
Get filename.
Definition Path.cpp:584
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner={})
Log all errors (if any) in E to OS.
Definition Error.cpp:61
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 cl::opt< bool > CtxprofMoveRootsToOwnModule("thinlto-move-ctxprof-trees", cl::desc("Move contextual profiling roots and the graphs under them in " "their own module."), cl::Hidden, cl::init(false))
Error createFileError(const Twine &F, Error E)
Concatenate a source file path and/or name with an Error.
Definition Error.h:1415
std::unordered_set< GlobalValueSummary * > GVSummaryPtrSet
A set of global value summary pointers.
bool internalizeModule(Module &TheModule, std::function< bool(const GlobalValue &)> MustPreserveGV)
Helper function to internalize functions and variables in a Module.
Definition Internalize.h:78
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"))
std::error_code make_error_code(BitcodeError E)
cl::list< GlobalValue::GUID > MoveSymbolGUID
const char * getHotnessName(CalleeInfo::HotnessType HT)
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
static cl::opt< std::string > WorkloadDefinitions("thinlto-workload-def", cl::desc("Pass a workload definition. This is a file containing a JSON " "dictionary. The keys are root functions, the values are lists of " "functions to import in the module defining the root. It is " "assumed -funique-internal-linkage-names was used, to ensure " "local linkage functions have unique names. For example: \n" "{\n" " \"rootFunction_1\": [\"function_to_import_1\", " "\"function_to_import_2\"], \n" " \"rootFunction_2\": [\"function_to_import_3\", " "\"function_to_import_4\"] \n" "}"), cl::Hidden)
Pass a workload description file - an example of workload would be the functions executed to satisfy ...
DenseMap< GlobalValue::GUID, GlobalValueSummary * > GVSummaryMapTy
Map of global value GUID to its summary, used to identify values defined in a particular module,...
cl::opt< std::string > UseCtxProfile("use-ctx-profile", cl::init(""), cl::Hidden, cl::desc("Use the specified contextual profile file"))
static cl::opt< bool > PrintImportFailures("print-import-failures", cl::init(false), cl::Hidden, cl::desc("Print information for functions rejected for importing"))
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition STLExtras.h:634
LLVM_ABI bool convertToDeclaration(GlobalValue &GV)
Converts value GV to declaration, or replaces with a declaration if it is an alias.
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.
LLVM_ABI void 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...
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition Error.h:1321
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 cl::opt< bool > PrintImports("print-imports", cl::init(false), cl::Hidden, cl::desc("Print imported functions"))
auto map_range(ContainerTy &&C, FuncTy F)
Return a range that applies F to the elements of C.
Definition STLExtras.h:366
@ not_supported
Definition Errc.h:69
@ invalid_argument
Definition Errc.h:56
LLVM_ABI void ComputeCrossModuleImport(const ModuleSummaryIndex &Index, const DenseMap< StringRef, GVSummaryMapTy > &ModuleToDefinedGVSummaries, function_ref< bool(GlobalValue::GUID, const GlobalValueSummary *)> isPrevailing, FunctionImporter::ImportListsTy &ImportLists, DenseMap< StringRef, FunctionImporter::ExportSetTy > &ExportLists)
Compute all the imports and exports for every module in the Index.
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
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"))
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:1746
void sort(IteratorTy Start, IteratorTy End)
Definition STLExtras.h:1636
cl::opt< bool > AlwaysRenamePromotedLocals("always-rename-promoted-locals", cl::init(true), cl::Hidden, cl::desc("Always rename promoted locals."))
Definition LTO.cpp:113
LLVM_ABI 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...
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
auto make_first_range(ContainerTy &&c)
Given a container of pairs, return a range over the first elements.
Definition STLExtras.h:1399
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:163
std::map< std::string, GVSummaryMapTy, std::less<> > ModuleToSummariesForIndexTy
Map of a module name to the GUIDs and summaries we will import from that module.
bool hasSingleElement(ContainerTy &&C)
Returns true if the given container only contains a single element.
Definition STLExtras.h:300
iterator_range< filter_iterator< detail::IterOfRange< RangeT >, PredicateT > > make_filter_range(RangeT &&Range, PredicateT Pred)
Convenience function that takes a range of elements and a predicate, and return a new filter_iterator...
Definition STLExtras.h:552
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< 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 cl::opt< bool > ImportDeclaration("import-declaration", cl::init(false), cl::Hidden, cl::desc("If true, import function declaration as fallback if the function " "definition is not imported."))
This is a test-only option.
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
Error make_error(ArgTs &&... Args)
Make a Error instance representing failure using the given error info type.
Definition Error.h:340
LLVM_ABI void updateIndirectCalls(ModuleSummaryIndex &Index)
Update call edges for indirect calls to local functions added from SamplePGO when needed.
LLVM_ABI 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.
Definition ModRef.h:32
cl::opt< bool > EnableMemProfContextDisambiguation
Enable MemProf context disambiguation for thin link.
LLVM_ABI 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:52
LLVM_ABI void thinLTOInternalizeModule(Module &TheModule, const GVSummaryMapTy &DefinedGlobals)
Internalize TheModule based on the information recorded in the summaries during global summary-based ...
cl::opt< bool > ForceImportAll
LLVM_ABI void gatherImportedSummariesForModule(StringRef ModulePath, const DenseMap< StringRef, GVSummaryMapTy > &ModuleToDefinedGVSummaries, const FunctionImporter::ImportMapTy &ImportList, ModuleToSummariesForIndexTy &ModuleToSummariesForIndex, GVSummaryPtrSet &DecSummaries)
Compute the set of summaries needed for a ThinLTO backend compilation of ModulePath.
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.
std::string toString(const APInt &I, unsigned Radix, bool Signed, bool formatAsCLiteral=false, bool UpperCase=true, bool InsertSeparators=false)
ValueMap< const Value *, WeakTrackingVH > ValueToValueMapTy
LLVM_ABI void processImportsFiles(StringRef ModulePath, const ModuleToSummariesForIndexTy &ModuleToSummariesForIndex, function_ref< void(const std::string &)> F)
Call F passing each of the files module ModulePath will import from.
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"))
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
PrevailingType
PrevailingType enum used as a return type of callback passed to computeDeadSymbolsAndUpdateIndirectCa...
LLVM_ABI Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
Definition Error.cpp:107
LLVM_ABI bool UpgradeDebugInfo(Module &M)
Check the debug info version number, if it is out-dated, drop the debug info.
static cl::opt< bool > EnableImportMetadata("enable-import-metadata", cl::init(false), cl::Hidden, cl::desc("Enable import metadata like 'thinlto_src_module' and " "'thinlto_src_file'"))
LLVM_ABI Function * CloneFunction(Function *F, ValueToValueMapTy &VMap, ClonedCodeInfo *CodeInfo=nullptr)
Return a copy of the specified function and add it to that function's module.
LLVM_ABI void computeDeadSymbolsWithConstProp(ModuleSummaryIndex &Index, const DenseSet< GlobalValue::GUID > &GUIDPreservedSymbols, function_ref< PrevailingType(GlobalValue::GUID)> isPrevailing, bool ImportEnabled)
Compute dead symbols and run constant propagation in combined index after that.
LLVM_ABI Error EmitImportsFiles(StringRef ModulePath, StringRef OutputFilename, const ModuleToSummariesForIndexTy &ModuleToSummariesForIndex)
Emit into OutputFilename the files module ModulePath will import from.
static cl::opt< bool > ComputeDead("compute-dead", cl::init(true), cl::Hidden, cl::desc("Compute dead symbols"))
LLVM_ABI 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,...
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
Definition MIRParser.h:39
LLVM_ABI 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.