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