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/LLVMContext.h"
28#include "llvm/IR/Metadata.h"
29#include "llvm/IR/Module.h"
32#include "llvm/Linker/IRMover.h"
36#include "llvm/Support/Debug.h"
37#include "llvm/Support/Errc.h"
38#include "llvm/Support/Error.h"
41#include "llvm/Support/JSON.h"
42#include "llvm/Support/Path.h"
51#include <cassert>
52#include <memory>
53#include <string>
54#include <system_error>
55#include <tuple>
56#include <utility>
57
58using namespace llvm;
59
60#define DEBUG_TYPE "function-import"
61
62STATISTIC(NumImportedFunctionsThinLink,
63 "Number of functions thin link decided to import");
64STATISTIC(NumImportedHotFunctionsThinLink,
65 "Number of hot functions thin link decided to import");
66STATISTIC(NumImportedCriticalFunctionsThinLink,
67 "Number of critical functions thin link decided to import");
68STATISTIC(NumImportedGlobalVarsThinLink,
69 "Number of global variables thin link decided to import");
70STATISTIC(NumImportedFunctions, "Number of functions imported in backend");
71STATISTIC(NumImportedGlobalVars,
72 "Number of global variables imported in backend");
73STATISTIC(NumImportedModules, "Number of modules imported from");
74STATISTIC(NumDeadSymbols, "Number of dead stripped symbols in index");
75STATISTIC(NumLiveSymbols, "Number of live symbols in index");
76
77namespace llvm {
79
81 ForceImportAll("force-import-all", cl::init(false), cl::Hidden,
82 cl::desc("Import functions with noinline attribute"));
83
84/// Limit on instruction count of imported functions.
86 "import-instr-limit", cl::init(100), cl::Hidden, cl::value_desc("N"),
87 cl::desc("Only import functions with less than N instructions"));
88
90 "import-cutoff", cl::init(-1), cl::Hidden, cl::value_desc("N"),
91 cl::desc("Only import first N functions if N>=0 (default -1)"));
92
93static cl::opt<float>
94 ImportInstrFactor("import-instr-evolution-factor", cl::init(0.7),
96 cl::desc("As we import functions, multiply the "
97 "`import-instr-limit` threshold by this factor "
98 "before processing newly imported functions"));
99
101 "import-hot-evolution-factor", cl::init(1.0), cl::Hidden,
102 cl::value_desc("x"),
103 cl::desc("As we import functions called from hot callsite, multiply the "
104 "`import-instr-limit` threshold by this factor "
105 "before processing newly imported functions"));
106
108 "import-hot-multiplier", cl::init(10.0), cl::Hidden, cl::value_desc("x"),
109 cl::desc("Multiply the `import-instr-limit` threshold for hot callsites"));
110
112 "import-critical-multiplier", cl::init(100.0), cl::Hidden,
113 cl::value_desc("x"),
114 cl::desc(
115 "Multiply the `import-instr-limit` threshold for critical callsites"));
116
117// FIXME: This multiplier was not really tuned up.
119 "import-cold-multiplier", cl::init(0), cl::Hidden, cl::value_desc("N"),
120 cl::desc("Multiply the `import-instr-limit` threshold for cold callsites"));
121
122static cl::opt<bool> PrintImports("print-imports", cl::init(false), cl::Hidden,
123 cl::desc("Print imported functions"));
124
126 "print-import-failures", cl::init(false), cl::Hidden,
127 cl::desc("Print information for functions rejected for importing"));
128
129static cl::opt<bool> ComputeDead("compute-dead", cl::init(true), cl::Hidden,
130 cl::desc("Compute dead symbols"));
131
133 "enable-import-metadata", cl::init(false), cl::Hidden,
134 cl::desc("Enable import metadata like 'thinlto_src_module' and "
135 "'thinlto_src_file'"));
136
137/// Summary file to use for function importing when using -function-import from
138/// the command line.
140 SummaryFile("summary-file",
141 cl::desc("The summary file to use for function importing."));
142
143/// Used when testing importing from distributed indexes via opt
144// -function-import.
145static cl::opt<bool>
146 ImportAllIndex("import-all-index",
147 cl::desc("Import all external functions in index."));
148
149/// This is a test-only option.
150/// If this option is enabled, the ThinLTO indexing step will import each
151/// function declaration as a fallback. In a real build this may increase ram
152/// usage of the indexing step unnecessarily.
153/// TODO: Implement selective import (based on combined summary analysis) to
154/// ensure the imported function has a use case in the postlink pipeline.
156 "import-declaration", cl::init(false), cl::Hidden,
157 cl::desc("If true, import function declaration as fallback if the function "
158 "definition is not imported."));
159
160/// Pass a workload description file - an example of workload would be the
161/// functions executed to satisfy a RPC request. A workload is defined by a root
162/// function and the list of functions that are (frequently) needed to satisfy
163/// it. The module that defines the root will have all those functions imported.
164/// The file contains a JSON dictionary. The keys are root functions, the values
165/// are lists of functions to import in the module defining the root. It is
166/// assumed -funique-internal-linkage-names was used, thus ensuring function
167/// names are unique even for local linkage ones.
169 "thinlto-workload-def",
170 cl::desc("Pass a workload definition. This is a file containing a JSON "
171 "dictionary. The keys are root functions, the values are lists of "
172 "functions to import in the module defining the root. It is "
173 "assumed -funique-internal-linkage-names was used, to ensure "
174 "local linkage functions have unique names. For example: \n"
175 "{\n"
176 " \"rootFunction_1\": [\"function_to_import_1\", "
177 "\"function_to_import_2\"], \n"
178 " \"rootFunction_2\": [\"function_to_import_3\", "
179 "\"function_to_import_4\"] \n"
180 "}"),
181 cl::Hidden);
182
184
186 "thinlto-move-ctxprof-trees",
187 cl::desc("Move contextual profiling roots and the graphs under them in "
188 "their own module."),
189 cl::Hidden, cl::init(false));
190
192
194} // end namespace llvm
195
196// Load lazily a module from \p FileName in \p Context.
197static std::unique_ptr<Module> loadFile(const std::string &FileName,
198 LLVMContext &Context) {
199 SMDiagnostic Err;
200 LLVM_DEBUG(dbgs() << "Loading '" << FileName << "'\n");
201 // Metadata isn't loaded until functions are imported, to minimize
202 // the memory overhead.
203 std::unique_ptr<Module> Result =
204 getLazyIRFileModule(FileName, Err, Context,
205 /* ShouldLazyLoadMetadata = */ true);
206 if (!Result) {
207 Err.print("function-import", errs());
208 report_fatal_error("Abort");
209 }
210
211 return Result;
212}
213
215 size_t NumDefs,
216 StringRef ImporterModule) {
217 // We can import a local when there is one definition.
218 if (NumDefs == 1)
219 return false;
220 // In other cases, make sure we import the copy in the caller's module if the
221 // referenced value has local linkage. The only time a local variable can
222 // share an entry in the index is if there is a local with the same name in
223 // another module that had the same source file name (in a different
224 // directory), where each was compiled in their own directory so there was not
225 // distinguishing path.
226 return GlobalValue::isLocalLinkage(RefSummary->linkage()) &&
227 RefSummary->modulePath() != ImporterModule;
228}
229
230/// Given a list of possible callee implementation for a call site, qualify the
231/// legality of importing each. The return is a range of pairs. Each pair
232/// corresponds to a candidate. The first value is the ImportFailureReason for
233/// that candidate, the second is the candidate.
235 const ModuleSummaryIndex &Index,
236 ArrayRef<std::unique_ptr<GlobalValueSummary>> CalleeSummaryList,
237 StringRef CallerModulePath) {
238 return llvm::map_range(
239 CalleeSummaryList,
240 [&Index, CalleeSummaryList,
241 CallerModulePath](const std::unique_ptr<GlobalValueSummary> &SummaryPtr)
243 const GlobalValueSummary *> {
244 auto *GVSummary = SummaryPtr.get();
245 if (!Index.isGlobalValueLive(GVSummary))
247
248 if (GlobalValue::isInterposableLinkage(GVSummary->linkage()))
250 GVSummary};
251
252 auto *Summary = dyn_cast<FunctionSummary>(GVSummary->getBaseObject());
253
254 // Ignore any callees that aren't actually functions. This could happen
255 // in the case of GUID hash collisions. It could also happen in theory
256 // for SamplePGO profiles collected on old versions of the code after
257 // renaming, since we synthesize edges to any inlined callees appearing
258 // in the profile.
259 if (!Summary)
261
262 // If this is a local function, make sure we import the copy in the
263 // caller's module. The only time a local function can share an entry in
264 // the index is if there is a local with the same name in another module
265 // that had the same source file name (in a different directory), where
266 // each was compiled in their own directory so there was not
267 // distinguishing path.
268 // If the local function is from another module, it must be a reference
269 // due to indirect call profile data since a function pointer can point
270 // to a local in another module. Do the import from another module if
271 // there is only one entry in the list or when all files in the program
272 // are compiled with full path - in both cases the local function has
273 // unique PGO name and GUID.
274 if (shouldSkipLocalInAnotherModule(Summary, CalleeSummaryList.size(),
275 CallerModulePath))
276 return {
278 GVSummary};
279
280 // Skip if it isn't legal to import (e.g. may reference unpromotable
281 // locals).
282 if (Summary->notEligibleToImport())
284 GVSummary};
285
287 });
288}
289
290/// Given a list of possible callee implementation for a call site, select one
291/// that fits the \p Threshold for function definition import. If none are
292/// found, the Reason will give the last reason for the failure (last, in the
293/// order of CalleeSummaryList entries). While looking for a callee definition,
294/// sets \p TooLargeOrNoInlineSummary to the last seen too-large or noinline
295/// candidate; other modules may want to know the function summary or
296/// declaration even if a definition is not needed.
297///
298/// FIXME: select "best" instead of first that fits. But what is "best"?
299/// - The smallest: more likely to be inlined.
300/// - The one with the least outgoing edges (already well optimized).
301/// - One from a module already being imported from in order to reduce the
302/// number of source modules parsed/linked.
303/// - One that has PGO data attached.
304/// - [insert you fancy metric here]
305static const GlobalValueSummary *
307 ArrayRef<std::unique_ptr<GlobalValueSummary>> CalleeSummaryList,
308 unsigned Threshold, StringRef CallerModulePath,
309 const GlobalValueSummary *&TooLargeOrNoInlineSummary,
311 // Records the last summary with reason noinline or too-large.
312 TooLargeOrNoInlineSummary = nullptr;
313 auto QualifiedCandidates =
314 qualifyCalleeCandidates(Index, CalleeSummaryList, CallerModulePath);
315 for (auto QualifiedValue : QualifiedCandidates) {
316 Reason = QualifiedValue.first;
317 // Skip a summary if its import is not (proved to be) legal.
319 continue;
320 auto *Summary =
321 cast<FunctionSummary>(QualifiedValue.second->getBaseObject());
322
323 // Don't bother importing the definition if the chance of inlining it is
324 // not high enough (except under `--force-import-all`).
325 if ((Summary->instCount() > Threshold) && !Summary->fflags().AlwaysInline &&
327 TooLargeOrNoInlineSummary = Summary;
329 continue;
330 }
331
332 // Don't bother importing the definition if we can't inline it anyway.
333 if (Summary->fflags().NoInline && !ForceImportAll) {
334 TooLargeOrNoInlineSummary = Summary;
336 continue;
337 }
338
339 return Summary;
340 }
341 return nullptr;
342}
343
344namespace {
345
346using EdgeInfo = std::tuple<const FunctionSummary *, unsigned /* Threshold */>;
347
348} // anonymous namespace
349
352 GlobalValue::GUID GUID) {
353 auto [Def, Decl] = IDs.createImportIDs(FromModule, GUID);
354 if (!Imports.insert(Def).second)
355 // Already there.
357
358 // Remove Decl in case it's there. Note that a definition takes precedence
359 // over a declaration for a given GUID.
360 return Imports.erase(Decl) ? AddDefinitionStatus::ChangedToDefinition
362}
363
365 StringRef FromModule, GlobalValue::GUID GUID) {
366 auto [Def, Decl] = IDs.createImportIDs(FromModule, GUID);
367 // Insert Decl only if Def is not present. Note that a definition takes
368 // precedence over a declaration for a given GUID.
369 if (!Imports.contains(Def))
370 Imports.insert(Decl);
371}
372
375 SetVector<StringRef> ModuleSet;
376 for (const auto &[SrcMod, GUID, ImportType] : *this)
377 ModuleSet.insert(SrcMod);
378 SmallVector<StringRef, 0> Modules = ModuleSet.takeVector();
379 llvm::sort(Modules);
380 return Modules;
381}
382
383std::optional<GlobalValueSummary::ImportKind>
385 GlobalValue::GUID GUID) const {
386 if (auto IDPair = IDs.getImportIDs(FromModule, GUID)) {
387 auto [Def, Decl] = *IDPair;
388 if (Imports.contains(Def))
390 if (Imports.contains(Decl))
392 }
393 return std::nullopt;
394}
395
396/// Import globals referenced by a function or other globals that are being
397/// imported, if importing such global is possible.
398class GlobalsImporter final {
399 const ModuleSummaryIndex &Index;
400 const GVSummaryMapTy &DefinedGVSummaries;
402 IsPrevailing;
405
406 bool shouldImportGlobal(const ValueInfo &VI) {
407 const auto &GVS = DefinedGVSummaries.find(VI.getGUID());
408 if (GVS == DefinedGVSummaries.end())
409 return true;
410 // We should not skip import if the module contains a non-prevailing
411 // definition with interposable linkage type. This is required for
412 // correctness in the situation where there is a prevailing def available
413 // for import and marked read-only. In this case, the non-prevailing def
414 // will be converted to a declaration, while the prevailing one becomes
415 // internal, thus no definitions will be available for linking. In order to
416 // prevent undefined symbol link error, the prevailing definition must be
417 // imported.
418 // FIXME: Consider adding a check that the suitable prevailing definition
419 // exists and marked read-only.
420 if (VI.getSummaryList().size() > 1 &&
421 GlobalValue::isInterposableLinkage(GVS->second->linkage()) &&
422 !IsPrevailing(VI.getGUID(), GVS->second))
423 return true;
424
425 return false;
426 }
427
428 void
429 onImportingSummaryImpl(const GlobalValueSummary &Summary,
431 for (const auto &VI : Summary.refs()) {
432 if (!shouldImportGlobal(VI)) {
434 dbgs() << "Ref ignored! Target already in destination module.\n");
435 continue;
436 }
437
438 LLVM_DEBUG(dbgs() << " ref -> " << VI << "\n");
439
440 for (const auto &RefSummary : VI.getSummaryList()) {
441 const auto *GVS = dyn_cast<GlobalVarSummary>(RefSummary.get());
442 // Stop looking if this is not a global variable, e.g. a function.
443 // Functions could be referenced by global vars - e.g. a vtable; but we
444 // don't currently imagine a reason those would be imported here, rather
445 // than as part of the logic deciding which functions to import (i.e.
446 // based on profile information). Should we decide to handle them here,
447 // we can refactor accordingly at that time.
448 // Note that it is safe to stop looking because the one case where we
449 // might have to import (a read/write-only global variable) cannot occur
450 // if this GUID has a non-variable summary. The only case where we even
451 // might find another summary in the list that is a variable is in the
452 // case of same-named locals in different modules not compiled with
453 // enough path, and during attribute propagation we will mark all
454 // summaries for a GUID (ValueInfo) as non read/write-only if any is not
455 // a global variable.
456 if (!GVS)
457 break;
458 bool CanImportDecl = false;
459 if (shouldSkipLocalInAnotherModule(GVS, VI.getSummaryList().size(),
460 Summary.modulePath()) ||
461 !Index.canImportGlobalVar(GVS, /* AnalyzeRefs */ true,
462 CanImportDecl)) {
463 if (ImportDeclaration && CanImportDecl)
464 ImportList.maybeAddDeclaration(RefSummary->modulePath(),
465 VI.getGUID());
466
467 continue;
468 }
469
470 // If there isn't an entry for GUID, insert <GUID, Definition> pair.
471 // Otherwise, definition should take precedence over declaration.
472 if (ImportList.addDefinition(RefSummary->modulePath(), VI.getGUID()) !=
474 break;
475
476 // Only update stat and exports if we haven't already imported this
477 // variable.
478 NumImportedGlobalVarsThinLink++;
479 // Any references made by this variable will be marked exported
480 // later, in ComputeCrossModuleImport, after import decisions are
481 // complete, which is more efficient than adding them here.
482 if (ExportLists)
483 (*ExportLists)[RefSummary->modulePath()].insert(VI);
484
485 // If variable is not writeonly we attempt to recursively analyze
486 // its references in order to import referenced constants.
487 if (!Index.isWriteOnly(GVS))
488 Worklist.emplace_back(GVS);
489 break;
490 }
491 }
492 }
493
494public:
496 const ModuleSummaryIndex &Index, const GVSummaryMapTy &DefinedGVSummaries,
498 IsPrevailing,
501 : Index(Index), DefinedGVSummaries(DefinedGVSummaries),
502 IsPrevailing(IsPrevailing), ImportList(ImportList),
503 ExportLists(ExportLists) {}
504
507 onImportingSummaryImpl(Summary, Worklist);
508 while (!Worklist.empty())
509 onImportingSummaryImpl(*Worklist.pop_back_val(), Worklist);
510 }
511};
512
514
515/// Determine the list of imports and exports for each module.
517 void computeImportForFunction(
518 const FunctionSummary &Summary, unsigned Threshold,
519 const GVSummaryMapTy &DefinedGVSummaries,
520 SmallVectorImpl<EdgeInfo> &Worklist, GlobalsImporter &GVImporter,
522 FunctionImporter::ImportThresholdsTy &ImportThresholds);
523
524protected:
529
536 virtual bool canImport(ValueInfo VI) { return true; }
537
538public:
539 virtual ~ModuleImportsManager() = default;
540
541 /// Given the list of globals defined in a module, compute the list of imports
542 /// as well as the list of "exports", i.e. the list of symbols referenced from
543 /// another module (that may require promotion).
544 virtual void
545 computeImportForModule(const GVSummaryMapTy &DefinedGVSummaries,
546 StringRef ModName,
548
549 static std::unique_ptr<ModuleImportsManager>
554 nullptr);
555};
556
557/// A ModuleImportsManager that operates based on a workload definition (see
558/// -thinlto-workload-def). For modules that do not define workload roots, it
559/// applies the base ModuleImportsManager import policy.
561 // Keep a module name -> value infos to import association. We use it to
562 // determine if a module's import list should be done by the base
563 // ModuleImportsManager or by us.
565 // Track the roots to avoid importing them due to other callers. We want there
566 // to be only one variant), for which we optimize according to the contextual
567 // profile. "Variants" refers to copies due to importing - we want there to be
568 // just one instance of this function.
570
571 void
572 computeImportForModule(const GVSummaryMapTy &DefinedGVSummaries,
573 StringRef ModName,
574 FunctionImporter::ImportMapTy &ImportList) override {
575 StringRef Filename = ModName;
577 Filename = sys::path::filename(ModName);
578 // Drop the file extension.
579 Filename = Filename.substr(0, Filename.find_last_of('.'));
580 }
581 auto SetIter = Workloads.find(Filename);
582
583 if (SetIter == Workloads.end()) {
584 LLVM_DEBUG(dbgs() << "[Workload] " << ModName
585 << " does not contain the root of any context.\n");
586 return ModuleImportsManager::computeImportForModule(DefinedGVSummaries,
587 ModName, ImportList);
588 }
589 LLVM_DEBUG(dbgs() << "[Workload] " << ModName
590 << " contains the root(s) of context(s).\n");
591
592 GlobalsImporter GVI(Index, DefinedGVSummaries, IsPrevailing, ImportList,
594 auto &ValueInfos = SetIter->second;
595 for (auto &VI : llvm::make_early_inc_range(ValueInfos)) {
596 auto It = DefinedGVSummaries.find(VI.getGUID());
597 if (It != DefinedGVSummaries.end() &&
598 IsPrevailing(VI.getGUID(), It->second)) {
600 dbgs() << "[Workload] " << VI.name()
601 << " has the prevailing variant already in the module "
602 << ModName << ". No need to import\n");
603 continue;
604 }
605 auto Candidates =
606 qualifyCalleeCandidates(Index, VI.getSummaryList(), ModName);
607
608 const GlobalValueSummary *GVS = nullptr;
609 auto PotentialCandidates = llvm::map_range(
611 Candidates,
612 [&](const auto &Candidate) {
613 LLVM_DEBUG(dbgs() << "[Workflow] Candidate for " << VI.name()
614 << " from " << Candidate.second->modulePath()
615 << " ImportFailureReason: "
616 << getFailureName(Candidate.first) << "\n");
617 return Candidate.first ==
619 }),
620 [](const auto &Candidate) { return Candidate.second; });
621 if (PotentialCandidates.empty()) {
622 LLVM_DEBUG(dbgs() << "[Workload] Not importing " << VI.name()
623 << " because can't find eligible Callee. Guid is: "
624 << VI.getGUID() << "\n");
625 continue;
626 }
627 /// We will prefer importing the prevailing candidate, if not, we'll
628 /// still pick the first available candidate. The reason we want to make
629 /// sure we do import the prevailing candidate is because the goal of
630 /// workload-awareness is to enable optimizations specializing the call
631 /// graph of that workload. Suppose a function is already defined in the
632 /// module, but it's not the prevailing variant. Suppose also we do not
633 /// inline it (in fact, if it were interposable, we can't inline it),
634 /// but we could specialize it to the workload in other ways. However,
635 /// the linker would drop it in the favor of the prevailing copy.
636 /// Instead, by importing the prevailing variant (assuming also the use
637 /// of `-avail-extern-to-local`), we keep the specialization. We could
638 /// alteranatively make the non-prevailing variant local, but the
639 /// prevailing one is also the one for which we would have previously
640 /// collected profiles, making it preferrable.
641 auto PrevailingCandidates = llvm::make_filter_range(
642 PotentialCandidates, [&](const auto *Candidate) {
643 return IsPrevailing(VI.getGUID(), Candidate);
644 });
645 if (PrevailingCandidates.empty()) {
646 GVS = *PotentialCandidates.begin();
647 if (!llvm::hasSingleElement(PotentialCandidates) &&
650 dbgs()
651 << "[Workload] Found multiple non-prevailing candidates for "
652 << VI.name()
653 << ". This is unexpected. Are module paths passed to the "
654 "compiler unique for the modules passed to the linker?");
655 // We could in theory have multiple (interposable) copies of a symbol
656 // when there is no prevailing candidate, if say the prevailing copy was
657 // in a native object being linked in. However, we should in theory be
658 // marking all of these non-prevailing IR copies dead in that case, in
659 // which case they won't be candidates.
660 assert(GVS->isLive());
661 } else {
662 assert(llvm::hasSingleElement(PrevailingCandidates));
663 GVS = *PrevailingCandidates.begin();
664 }
665
666 auto ExportingModule = GVS->modulePath();
667 // We checked that for the prevailing case, but if we happen to have for
668 // example an internal that's defined in this module, it'd have no
669 // PrevailingCandidates.
670 if (ExportingModule == ModName) {
671 LLVM_DEBUG(dbgs() << "[Workload] Not importing " << VI.name()
672 << " because its defining module is the same as the "
673 "current module\n");
674 continue;
675 }
676 LLVM_DEBUG(dbgs() << "[Workload][Including]" << VI.name() << " from "
677 << ExportingModule << " : " << VI.getGUID() << "\n");
678 ImportList.addDefinition(ExportingModule, VI.getGUID());
679 GVI.onImportingSummary(*GVS);
680 if (ExportLists)
681 (*ExportLists)[ExportingModule].insert(VI);
682 }
683 LLVM_DEBUG(dbgs() << "[Workload] Done\n");
684 }
685
686 void loadFromJson() {
687 // Since the workload def uses names, we need a quick lookup
688 // name->ValueInfo.
689 StringMap<ValueInfo> NameToValueInfo;
690 StringSet<> AmbiguousNames;
691 for (auto &I : Index) {
692 ValueInfo VI = Index.getValueInfo(I);
693 if (!NameToValueInfo.insert(std::make_pair(VI.name(), VI)).second)
694 LLVM_DEBUG(AmbiguousNames.insert(VI.name()));
695 }
696 auto DbgReportIfAmbiguous = [&](StringRef Name) {
697 LLVM_DEBUG(if (AmbiguousNames.count(Name) > 0) {
698 dbgs() << "[Workload] Function name " << Name
699 << " present in the workload definition is ambiguous. Consider "
700 "compiling with -funique-internal-linkage-names.";
701 });
702 };
703 std::error_code EC;
705 if (std::error_code EC = BufferOrErr.getError()) {
706 report_fatal_error("Failed to open context file");
707 return;
708 }
709 auto Buffer = std::move(BufferOrErr.get());
710 std::map<std::string, std::vector<std::string>> WorkloadDefs;
711 json::Path::Root NullRoot;
712 // The JSON is supposed to contain a dictionary matching the type of
713 // WorkloadDefs. For example:
714 // {
715 // "rootFunction_1": ["function_to_import_1", "function_to_import_2"],
716 // "rootFunction_2": ["function_to_import_3", "function_to_import_4"]
717 // }
718 auto Parsed = json::parse(Buffer->getBuffer());
719 if (!Parsed)
720 report_fatal_error(Parsed.takeError());
721 if (!json::fromJSON(*Parsed, WorkloadDefs, NullRoot))
722 report_fatal_error("Invalid thinlto contextual profile format.");
723 for (const auto &Workload : WorkloadDefs) {
724 const auto &Root = Workload.first;
725 DbgReportIfAmbiguous(Root);
726 LLVM_DEBUG(dbgs() << "[Workload] Root: " << Root << "\n");
727 const auto &AllCallees = Workload.second;
728 auto RootIt = NameToValueInfo.find(Root);
729 if (RootIt == NameToValueInfo.end()) {
730 LLVM_DEBUG(dbgs() << "[Workload] Root " << Root
731 << " not found in this linkage unit.\n");
732 continue;
733 }
734 auto RootVI = RootIt->second;
735 if (RootVI.getSummaryList().size() != 1) {
736 LLVM_DEBUG(dbgs() << "[Workload] Root " << Root
737 << " should have exactly one summary, but has "
738 << RootVI.getSummaryList().size() << ". Skipping.\n");
739 continue;
740 }
741 StringRef RootDefiningModule =
742 RootVI.getSummaryList().front()->modulePath();
743 LLVM_DEBUG(dbgs() << "[Workload] Root defining module for " << Root
744 << " is : " << RootDefiningModule << "\n");
745 auto &Set = Workloads[RootDefiningModule];
746 for (const auto &Callee : AllCallees) {
747 LLVM_DEBUG(dbgs() << "[Workload] " << Callee << "\n");
748 DbgReportIfAmbiguous(Callee);
749 auto ElemIt = NameToValueInfo.find(Callee);
750 if (ElemIt == NameToValueInfo.end()) {
751 LLVM_DEBUG(dbgs() << "[Workload] " << Callee << " not found\n");
752 continue;
753 }
754 Set.insert(ElemIt->second);
755 }
756 }
757 }
758
759 void loadFromCtxProf() {
760 std::error_code EC;
762 if (std::error_code EC = BufferOrErr.getError()) {
763 report_fatal_error("Failed to open contextual profile file");
764 return;
765 }
766 auto Buffer = std::move(BufferOrErr.get());
767
768 PGOCtxProfileReader Reader(Buffer->getBuffer());
769 auto Ctx = Reader.loadProfiles();
770 if (!Ctx) {
771 report_fatal_error("Failed to parse contextual profiles");
772 return;
773 }
774 const auto &CtxMap = Ctx->Contexts;
775 SetVector<GlobalValue::GUID> ContainedGUIDs;
776 for (const auto &[RootGuid, Root] : CtxMap) {
777 // Avoid ContainedGUIDs to get in/out of scope. Reuse its memory for
778 // subsequent roots, but clear its contents.
779 ContainedGUIDs.clear();
780
781 auto RootVI = Index.getValueInfo(RootGuid);
782 if (!RootVI) {
783 LLVM_DEBUG(dbgs() << "[Workload] Root " << RootGuid
784 << " not found in this linkage unit.\n");
785 continue;
786 }
787 if (RootVI.getSummaryList().size() != 1) {
788 LLVM_DEBUG(dbgs() << "[Workload] Root " << RootGuid
789 << " should have exactly one summary, but has "
790 << RootVI.getSummaryList().size() << ". Skipping.\n");
791 continue;
792 }
793 std::string RootDefiningModule =
794 RootVI.getSummaryList().front()->modulePath().str();
796 RootDefiningModule = std::to_string(RootGuid);
798 dbgs() << "[Workload] Moving " << RootGuid
799 << " to a module with the filename without extension : "
800 << RootDefiningModule << "\n");
801 } else {
802 LLVM_DEBUG(dbgs() << "[Workload] Root defining module for " << RootGuid
803 << " is : " << RootDefiningModule << "\n");
804 }
805 auto &Set = Workloads[RootDefiningModule];
806 Root.getContainedGuids(ContainedGUIDs);
807 Roots.insert(RootVI);
808 for (auto Guid : ContainedGUIDs)
809 if (auto VI = Index.getValueInfo(Guid))
810 Set.insert(VI);
811 }
812 }
813
814 bool canImport(ValueInfo VI) override { return !Roots.contains(VI); }
815
816public:
823 if (UseCtxProfile.empty() == WorkloadDefinitions.empty()) {
824 report_fatal_error(
825 "Pass only one of: -thinlto-pgo-ctx-prof or -thinlto-workload-def");
826 return;
827 }
828 if (!UseCtxProfile.empty())
829 loadFromCtxProf();
830 else
831 loadFromJson();
832 LLVM_DEBUG({
833 for (const auto &[Root, Set] : Workloads) {
834 dbgs() << "[Workload] Root: " << Root << " we have " << Set.size()
835 << " distinct callees.\n";
836 for (const auto &VI : Set) {
837 dbgs() << "[Workload] Root: " << Root
838 << " Would include: " << VI.getGUID() << "\n";
839 }
840 }
841 });
842 }
843};
844
845std::unique_ptr<ModuleImportsManager> ModuleImportsManager::create(
850 if (WorkloadDefinitions.empty() && UseCtxProfile.empty()) {
851 LLVM_DEBUG(dbgs() << "[Workload] Using the regular imports manager.\n");
852 return std::unique_ptr<ModuleImportsManager>(
854 }
855 LLVM_DEBUG(dbgs() << "[Workload] Using the contextual imports manager.\n");
856 return std::make_unique<WorkloadImportsManager>(IsPrevailing, Index,
858}
859
860static const char *
862 switch (Reason) {
864 return "None";
866 return "GlobalVar";
868 return "NotLive";
870 return "TooLarge";
872 return "InterposableLinkage";
874 return "LocalLinkageNotInModule";
876 return "NotEligible";
878 return "NoInline";
879 }
880 llvm_unreachable("invalid reason");
881}
882
883/// Compute the list of functions to import for a given caller. Mark these
884/// imported functions and the symbols they reference in their source module as
885/// exported from their source module.
886void ModuleImportsManager::computeImportForFunction(
887 const FunctionSummary &Summary, const unsigned Threshold,
888 const GVSummaryMapTy &DefinedGVSummaries,
889 SmallVectorImpl<EdgeInfo> &Worklist, GlobalsImporter &GVImporter,
890 FunctionImporter::ImportMapTy &ImportList,
891 FunctionImporter::ImportThresholdsTy &ImportThresholds) {
892 GVImporter.onImportingSummary(Summary);
893 static int ImportCount = 0;
894 for (const auto &Edge : Summary.calls()) {
895 ValueInfo VI = Edge.first;
896 LLVM_DEBUG(dbgs() << " edge -> " << VI << " Threshold:" << Threshold
897 << "\n");
898
899 if (ImportCutoff >= 0 && ImportCount >= ImportCutoff) {
900 LLVM_DEBUG(dbgs() << "ignored! import-cutoff value of " << ImportCutoff
901 << " reached.\n");
902 continue;
903 }
904
905 if (DefinedGVSummaries.count(VI.getGUID())) {
906 // FIXME: Consider not skipping import if the module contains
907 // a non-prevailing def with interposable linkage. The prevailing copy
908 // can safely be imported (see shouldImportGlobal()).
909 LLVM_DEBUG(dbgs() << "ignored! Target already in destination module.\n");
910 continue;
911 }
912
913 if (!canImport(VI)) {
915 dbgs() << "Skipping over " << VI.getGUID()
916 << " because its import is handled in a different module.");
917 assert(VI.getSummaryList().size() == 1 &&
918 "The root was expected to be an external symbol");
919 continue;
920 }
921
922 auto GetBonusMultiplier = [](CalleeInfo::HotnessType Hotness) -> float {
923 if (Hotness == CalleeInfo::HotnessType::Hot)
924 return ImportHotMultiplier;
925 if (Hotness == CalleeInfo::HotnessType::Cold)
929 return 1.0;
930 };
931
932 const auto NewThreshold =
933 Threshold * GetBonusMultiplier(Edge.second.getHotness());
934
935 auto IT = ImportThresholds.insert(std::make_pair(
936 VI.getGUID(), std::make_tuple(NewThreshold, nullptr, nullptr)));
937 bool PreviouslyVisited = !IT.second;
938 auto &ProcessedThreshold = std::get<0>(IT.first->second);
939 auto &CalleeSummary = std::get<1>(IT.first->second);
940 auto &FailureInfo = std::get<2>(IT.first->second);
941
942 bool IsHotCallsite =
943 Edge.second.getHotness() == CalleeInfo::HotnessType::Hot;
944 bool IsCriticalCallsite =
945 Edge.second.getHotness() == CalleeInfo::HotnessType::Critical;
946
947 const FunctionSummary *ResolvedCalleeSummary = nullptr;
948 if (CalleeSummary) {
949 assert(PreviouslyVisited);
950 // Since the traversal of the call graph is DFS, we can revisit a function
951 // a second time with a higher threshold. In this case, it is added back
952 // to the worklist with the new threshold (so that its own callee chains
953 // can be considered with the higher threshold).
954 if (NewThreshold <= ProcessedThreshold) {
956 dbgs() << "ignored! Target was already imported with Threshold "
957 << ProcessedThreshold << "\n");
958 continue;
959 }
960 // Update with new larger threshold.
961 ProcessedThreshold = NewThreshold;
962 ResolvedCalleeSummary = cast<FunctionSummary>(CalleeSummary);
963 } else {
964 // If we already rejected importing a callee at the same or higher
965 // threshold, don't waste time calling selectCallee.
966 if (PreviouslyVisited && NewThreshold <= ProcessedThreshold) {
968 dbgs() << "ignored! Target was already rejected with Threshold "
969 << ProcessedThreshold << "\n");
971 assert(FailureInfo &&
972 "Expected FailureInfo for previously rejected candidate");
973 FailureInfo->Attempts++;
974 }
975 continue;
976 }
977
979
980 // `SummaryForDeclImport` is an summary eligible for declaration import.
981 const GlobalValueSummary *SummaryForDeclImport = nullptr;
982 CalleeSummary =
983 selectCallee(Index, VI.getSummaryList(), NewThreshold,
984 Summary.modulePath(), SummaryForDeclImport, Reason);
985 if (!CalleeSummary) {
986 // There isn't a callee for definition import but one for declaration
987 // import.
988 if (ImportDeclaration && SummaryForDeclImport) {
989 StringRef DeclSourceModule = SummaryForDeclImport->modulePath();
990
991 // Note `ExportLists` only keeps track of exports due to imported
992 // definitions.
993 ImportList.maybeAddDeclaration(DeclSourceModule, VI.getGUID());
994 }
995 // Update with new larger threshold if this was a retry (otherwise
996 // we would have already inserted with NewThreshold above). Also
997 // update failure info if requested.
998 if (PreviouslyVisited) {
999 ProcessedThreshold = NewThreshold;
1000 if (PrintImportFailures) {
1001 assert(FailureInfo &&
1002 "Expected FailureInfo for previously rejected candidate");
1003 FailureInfo->Reason = Reason;
1004 FailureInfo->Attempts++;
1005 FailureInfo->MaxHotness =
1006 std::max(FailureInfo->MaxHotness, Edge.second.getHotness());
1007 }
1008 } else if (PrintImportFailures) {
1009 assert(!FailureInfo &&
1010 "Expected no FailureInfo for newly rejected candidate");
1011 FailureInfo = std::make_unique<FunctionImporter::ImportFailureInfo>(
1012 VI, Edge.second.getHotness(), Reason, 1);
1013 }
1014 if (ForceImportAll) {
1015 std::string Msg = std::string("Failed to import function ") +
1016 VI.name().str() + " due to " +
1017 getFailureName(Reason);
1020 logAllUnhandledErrors(std::move(Error), errs(),
1021 "Error importing module: ");
1022 break;
1023 } else {
1025 << "ignored! No qualifying callee with summary found.\n");
1026 continue;
1027 }
1028 }
1029
1030 // "Resolve" the summary
1031 CalleeSummary = CalleeSummary->getBaseObject();
1032 ResolvedCalleeSummary = cast<FunctionSummary>(CalleeSummary);
1033
1034 assert((ResolvedCalleeSummary->fflags().AlwaysInline || ForceImportAll ||
1035 (ResolvedCalleeSummary->instCount() <= NewThreshold)) &&
1036 "selectCallee() didn't honor the threshold");
1037
1038 auto ExportModulePath = ResolvedCalleeSummary->modulePath();
1039
1040 // Try emplace the definition entry, and update stats based on insertion
1041 // status.
1042 if (ImportList.addDefinition(ExportModulePath, VI.getGUID()) !=
1044 NumImportedFunctionsThinLink++;
1045 if (IsHotCallsite)
1046 NumImportedHotFunctionsThinLink++;
1047 if (IsCriticalCallsite)
1048 NumImportedCriticalFunctionsThinLink++;
1049 }
1050
1051 // Any calls/references made by this function will be marked exported
1052 // later, in ComputeCrossModuleImport, after import decisions are
1053 // complete, which is more efficient than adding them here.
1054 if (ExportLists)
1055 (*ExportLists)[ExportModulePath].insert(VI);
1056 }
1057
1058 auto GetAdjustedThreshold = [](unsigned Threshold, bool IsHotCallsite) {
1059 // Adjust the threshold for next level of imported functions.
1060 // The threshold is different for hot callsites because we can then
1061 // inline chains of hot calls.
1062 if (IsHotCallsite)
1063 return Threshold * ImportHotInstrFactor;
1064 return Threshold * ImportInstrFactor;
1065 };
1066
1067 const auto AdjThreshold = GetAdjustedThreshold(Threshold, IsHotCallsite);
1068
1069 ImportCount++;
1070
1071 // Insert the newly imported function to the worklist.
1072 Worklist.emplace_back(ResolvedCalleeSummary, AdjThreshold);
1073 }
1074}
1075
1077 const GVSummaryMapTy &DefinedGVSummaries, StringRef ModName,
1078 FunctionImporter::ImportMapTy &ImportList) {
1079 // Worklist contains the list of function imported in this module, for which
1080 // we will analyse the callees and may import further down the callgraph.
1082 GlobalsImporter GVI(Index, DefinedGVSummaries, IsPrevailing, ImportList,
1083 ExportLists);
1084 FunctionImporter::ImportThresholdsTy ImportThresholds;
1085
1086 // Populate the worklist with the import for the functions in the current
1087 // module
1088 for (const auto &GVSummary : DefinedGVSummaries) {
1089#ifndef NDEBUG
1090 // FIXME: Change the GVSummaryMapTy to hold ValueInfo instead of GUID
1091 // so this map look up (and possibly others) can be avoided.
1092 auto VI = Index.getValueInfo(GVSummary.first);
1093#endif
1094 if (!Index.isGlobalValueLive(GVSummary.second)) {
1095 LLVM_DEBUG(dbgs() << "Ignores Dead GUID: " << VI << "\n");
1096 continue;
1097 }
1098 auto *FuncSummary =
1099 dyn_cast<FunctionSummary>(GVSummary.second->getBaseObject());
1100 if (!FuncSummary)
1101 // Skip import for global variables
1102 continue;
1103 LLVM_DEBUG(dbgs() << "Initialize import for " << VI << "\n");
1104 computeImportForFunction(*FuncSummary, ImportInstrLimit, DefinedGVSummaries,
1105 Worklist, GVI, ImportList, ImportThresholds);
1106 }
1107
1108 // Process the newly imported functions and add callees to the worklist.
1109 while (!Worklist.empty()) {
1110 auto GVInfo = Worklist.pop_back_val();
1111 auto *Summary = std::get<0>(GVInfo);
1112 auto Threshold = std::get<1>(GVInfo);
1113
1114 computeImportForFunction(*Summary, Threshold, DefinedGVSummaries, Worklist,
1115 GVI, ImportList, ImportThresholds);
1116 }
1117
1118 // Print stats about functions considered but rejected for importing
1119 // when requested.
1120 if (PrintImportFailures) {
1121 dbgs() << "Missed imports into module " << ModName << "\n";
1122 for (auto &I : ImportThresholds) {
1123 auto &ProcessedThreshold = std::get<0>(I.second);
1124 auto &CalleeSummary = std::get<1>(I.second);
1125 auto &FailureInfo = std::get<2>(I.second);
1126 if (CalleeSummary)
1127 continue; // We are going to import.
1128 assert(FailureInfo);
1129 FunctionSummary *FS = nullptr;
1130 if (!FailureInfo->VI.getSummaryList().empty())
1132 FailureInfo->VI.getSummaryList()[0]->getBaseObject());
1133 dbgs() << FailureInfo->VI
1134 << ": Reason = " << getFailureName(FailureInfo->Reason)
1135 << ", Threshold = " << ProcessedThreshold
1136 << ", Size = " << (FS ? (int)FS->instCount() : -1)
1137 << ", MaxHotness = " << getHotnessName(FailureInfo->MaxHotness)
1138 << ", Attempts = " << FailureInfo->Attempts << "\n";
1139 }
1140 }
1141}
1142
1143#ifndef NDEBUG
1144static bool isGlobalVarSummary(const ModuleSummaryIndex &Index, ValueInfo VI) {
1145 auto SL = VI.getSummaryList();
1146 return SL.empty()
1147 ? false
1148 : SL[0]->getSummaryKind() == GlobalValueSummary::GlobalVarKind;
1149}
1150
1153 if (const auto &VI = Index.getValueInfo(G))
1154 return isGlobalVarSummary(Index, VI);
1155 return false;
1156}
1157
1158// Return the number of global variable summaries in ExportSet.
1159static unsigned
1161 FunctionImporter::ExportSetTy &ExportSet) {
1162 unsigned NumGVS = 0;
1163 for (auto &VI : ExportSet)
1164 if (isGlobalVarSummary(Index, VI.getGUID()))
1165 ++NumGVS;
1166 return NumGVS;
1167}
1168
1170 unsigned NumGVS = 0;
1171 unsigned DefinedFS = 0;
1172 unsigned Count = 0;
1173};
1174
1175// Compute import statistics for each source module in ImportList.
1178 const FunctionImporter::ImportMapTy &ImportList) {
1180
1181 for (const auto &[FromModule, GUID, Type] : ImportList) {
1182 ImportStatistics &Entry = Histogram[FromModule];
1183 ++Entry.Count;
1184 if (isGlobalVarSummary(Index, GUID))
1185 ++Entry.NumGVS;
1187 ++Entry.DefinedFS;
1188 }
1189 return Histogram;
1190}
1191#endif
1192
1193#ifndef NDEBUG
1195 const ModuleSummaryIndex &Index,
1198 DenseSet<GlobalValue::GUID> FlattenedImports;
1199
1200 for (const auto &ImportPerModule : ImportLists)
1201 for (const auto &[FromModule, GUID, ImportType] : ImportPerModule.second)
1202 FlattenedImports.insert(GUID);
1203
1204 // Checks that all GUIDs of read/writeonly vars we see in export lists
1205 // are also in the import lists. Otherwise we my face linker undefs,
1206 // because readonly and writeonly vars are internalized in their
1207 // source modules. The exception would be if it has a linkage type indicating
1208 // that there may have been a copy existing in the importing module (e.g.
1209 // linkonce_odr). In that case we cannot accurately do this checking.
1210 auto IsReadOrWriteOnlyVarNeedingImporting = [&](StringRef ModulePath,
1211 const ValueInfo &VI) {
1213 Index.findSummaryInModule(VI, ModulePath));
1214 return GVS && (Index.isReadOnly(GVS) || Index.isWriteOnly(GVS)) &&
1215 !(GVS->linkage() == GlobalValue::AvailableExternallyLinkage ||
1216 GVS->linkage() == GlobalValue::WeakODRLinkage ||
1217 GVS->linkage() == GlobalValue::LinkOnceODRLinkage);
1218 };
1219
1220 for (auto &ExportPerModule : ExportLists)
1221 for (auto &VI : ExportPerModule.second)
1222 if (!FlattenedImports.count(VI.getGUID()) &&
1223 IsReadOrWriteOnlyVarNeedingImporting(ExportPerModule.first, VI))
1224 return false;
1225
1226 return true;
1227}
1228#endif
1229
1230/// Compute all the import and export for every module using the Index.
1232 const ModuleSummaryIndex &Index,
1233 const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1235 isPrevailing,
1238 auto MIS = ModuleImportsManager::create(isPrevailing, Index, &ExportLists);
1239 // For each module that has function defined, compute the import/export lists.
1240 for (const auto &DefinedGVSummaries : ModuleToDefinedGVSummaries) {
1241 auto &ImportList = ImportLists[DefinedGVSummaries.first];
1242 LLVM_DEBUG(dbgs() << "Computing import for Module '"
1243 << DefinedGVSummaries.first << "'\n");
1244 MIS->computeImportForModule(DefinedGVSummaries.second,
1245 DefinedGVSummaries.first, ImportList);
1246 }
1247
1248 // When computing imports we only added the variables and functions being
1249 // imported to the export list. We also need to mark any references and calls
1250 // they make as exported as well. We do this here, as it is more efficient
1251 // since we may import the same values multiple times into different modules
1252 // during the import computation.
1253 for (auto &ELI : ExportLists) {
1254 // `NewExports` tracks the VI that gets exported because the full definition
1255 // of its user/referencer gets exported.
1257 const auto &DefinedGVSummaries =
1258 ModuleToDefinedGVSummaries.lookup(ELI.first);
1259 for (auto &EI : ELI.second) {
1260 // Find the copy defined in the exporting module so that we can mark the
1261 // values it references in that specific definition as exported.
1262 // Below we will add all references and called values, without regard to
1263 // whether they are also defined in this module. We subsequently prune the
1264 // list to only include those defined in the exporting module, see comment
1265 // there as to why.
1266 auto DS = DefinedGVSummaries.find(EI.getGUID());
1267 // Anything marked exported during the import computation must have been
1268 // defined in the exporting module.
1269 assert(DS != DefinedGVSummaries.end());
1270 auto *S = DS->getSecond();
1271 S = S->getBaseObject();
1272 if (auto *GVS = dyn_cast<GlobalVarSummary>(S)) {
1273 // Export referenced functions and variables. We don't export/promote
1274 // objects referenced by writeonly variable initializer, because
1275 // we convert such variables initializers to "zeroinitializer".
1276 // See processGlobalForThinLTO.
1277 if (!Index.isWriteOnly(GVS))
1278 NewExports.insert_range(GVS->refs());
1279 } else {
1280 auto *FS = cast<FunctionSummary>(S);
1281 NewExports.insert_range(llvm::make_first_range(FS->calls()));
1282 NewExports.insert_range(FS->refs());
1283 }
1284 }
1285 // Prune list computed above to only include values defined in the
1286 // exporting module. We do this after the above insertion since we may hit
1287 // the same ref/call target multiple times in above loop, and it is more
1288 // efficient to avoid a set lookup each time.
1289 NewExports.remove_if(
1290 [&](ValueInfo VI) { return !DefinedGVSummaries.count(VI.getGUID()); });
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 // When AlwaysRenamePromotedLocals is false, for each source module we import
1612 // from, also include summaries for local functions that have
1613 // NoRenameOnPromotion set. This is needed for distributed ThinLTO. Otherwise,
1614 // the local function of the source module will keep its origin name, e.g.,
1615 // foo() while the function in destination module will have name
1616 // foo.llvm.<...>() and this will cause a link failure.
1617 //
1618 // Note: this imports a superset of the necessary declarations — all locals
1619 // with NoRenameOnPromotion in each source module, not just those referenced
1620 // by the importing module. Computing the precise set would require walking
1621 // the summary reference graph from each imported function, which is more
1622 // expensive than the simple scan here.
1624 for (auto &[ModPath, SummariesForIndex] : ModuleToSummariesForIndex) {
1625 if (ModPath == ModulePath)
1626 continue;
1627 auto It = ModuleToDefinedGVSummaries.find(ModPath);
1628 if (It == ModuleToDefinedGVSummaries.end())
1629 continue;
1630 for (const auto &[GUID, Summary] : It->second) {
1631 if (Summary->noRenameOnPromotion()) {
1632 DecSummaries.insert(Summary);
1633 SummariesForIndex.try_emplace(GUID, Summary);
1634 }
1635 }
1636 }
1637 }
1638}
1639
1640/// Emit the files \p ModulePath will import from into \p OutputFilename.
1643 const ModuleToSummariesForIndexTy &ModuleToSummariesForIndex) {
1644 std::error_code EC;
1646 if (EC)
1647 return createFileError("cannot open " + OutputFilename,
1648 errorCodeToError(EC));
1649 processImportsFiles(ModulePath, ModuleToSummariesForIndex,
1650 [&](StringRef M) { ImportsOS << M << "\n"; });
1651 return Error::success();
1652}
1653
1654/// Invoke callback \p F on the file paths from which \p ModulePath
1655/// will import.
1657 StringRef ModulePath,
1658 const ModuleToSummariesForIndexTy &ModuleToSummariesForIndex,
1659 function_ref<void(const std::string &)> F) {
1660 for (const auto &ILI : ModuleToSummariesForIndex)
1661 // The ModuleToSummariesForIndex map includes an entry for the current
1662 // Module (needed for writing out the index files). We don't want to
1663 // include it in the imports file, however, so filter it out.
1664 if (ILI.first != ModulePath)
1665 F(ILI.first);
1666}
1667
1669 LLVM_DEBUG(dbgs() << "Converting to a declaration: `" << GV.getName()
1670 << "\n");
1671 if (Function *F = dyn_cast<Function>(&GV)) {
1672 F->deleteBody();
1673 F->clearMetadata();
1674 F->setComdat(nullptr);
1675 } else if (GlobalVariable *V = dyn_cast<GlobalVariable>(&GV)) {
1676 V->setInitializer(nullptr);
1677 V->setLinkage(GlobalValue::ExternalLinkage);
1678 V->clearMetadata();
1679 V->setComdat(nullptr);
1680 } else {
1681 GlobalValue *NewGV;
1682 if (GV.getValueType()->isFunctionTy())
1683 NewGV =
1686 "", GV.getParent());
1687 else
1688 NewGV =
1689 new GlobalVariable(*GV.getParent(), GV.getValueType(),
1690 /*isConstant*/ false, GlobalValue::ExternalLinkage,
1691 /*init*/ nullptr, "",
1692 /*insertbefore*/ nullptr, GV.getThreadLocalMode(),
1693 GV.getType()->getAddressSpace());
1694 NewGV->takeName(&GV);
1695 GV.replaceAllUsesWith(NewGV);
1696 return false;
1697 }
1698 if (!GV.isImplicitDSOLocal())
1699 GV.setDSOLocal(false);
1700 return true;
1701}
1702
1704 const GVSummaryMapTy &DefinedGlobals,
1705 bool PropagateAttrs) {
1706 llvm::TimeTraceScope timeScope("ThinLTO finalize in module");
1707 DenseSet<Comdat *> NonPrevailingComdats;
1708 auto FinalizeInModule = [&](GlobalValue &GV, bool Propagate = false) {
1709 // See if the global summary analysis computed a new resolved linkage.
1710 const auto &GS = DefinedGlobals.find(GV.getGUID());
1711 if (GS == DefinedGlobals.end())
1712 return;
1713
1714 if (Propagate)
1715 if (FunctionSummary *FS = dyn_cast<FunctionSummary>(GS->second)) {
1716 if (Function *F = dyn_cast<Function>(&GV)) {
1717 // TODO: propagate ReadNone and ReadOnly.
1718 if (FS->fflags().ReadNone && !F->doesNotAccessMemory())
1719 F->setDoesNotAccessMemory();
1720
1721 if (FS->fflags().ReadOnly && !F->onlyReadsMemory())
1722 F->setOnlyReadsMemory();
1723
1724 if (FS->fflags().NoRecurse && !F->doesNotRecurse())
1725 F->setDoesNotRecurse();
1726
1727 if (FS->fflags().NoUnwind && !F->doesNotThrow())
1728 F->setDoesNotThrow();
1729 }
1730 }
1731
1732 auto NewLinkage = GS->second->linkage();
1734 // Don't internalize anything here, because the code below
1735 // lacks necessary correctness checks. Leave this job to
1736 // LLVM 'internalize' pass.
1737 GlobalValue::isLocalLinkage(NewLinkage) ||
1738 // In case it was dead and already converted to declaration.
1739 GV.isDeclaration())
1740 return;
1741
1742 // Set the potentially more constraining visibility computed from summaries.
1743 // The DefaultVisibility condition is because older GlobalValueSummary does
1744 // not record DefaultVisibility and we don't want to change protected/hidden
1745 // to default.
1746 if (GS->second->getVisibility() != GlobalValue::DefaultVisibility)
1747 GV.setVisibility(GS->second->getVisibility());
1748
1749 if (NewLinkage == GV.getLinkage())
1750 return;
1751
1752 // Check for a non-prevailing def that has interposable linkage
1753 // (e.g. non-odr weak or linkonce). In that case we can't simply
1754 // convert to available_externally, since it would lose the
1755 // interposable property and possibly get inlined. Simply drop
1756 // the definition in that case.
1759 if (!convertToDeclaration(GV))
1760 // FIXME: Change this to collect replaced GVs and later erase
1761 // them from the parent module once thinLTOResolvePrevailingGUID is
1762 // changed to enable this for aliases.
1763 llvm_unreachable("Expected GV to be converted");
1764 } else {
1765 // If all copies of the original symbol had global unnamed addr and
1766 // linkonce_odr linkage, or if all of them had local unnamed addr linkage
1767 // and are constants, then it should be an auto hide symbol. In that case
1768 // the thin link would have marked it as CanAutoHide. Add hidden
1769 // visibility to the symbol to preserve the property.
1770 if (NewLinkage == GlobalValue::WeakODRLinkage &&
1771 GS->second->canAutoHide()) {
1774 }
1775
1776 LLVM_DEBUG(dbgs() << "ODR fixing up linkage for `" << GV.getName()
1777 << "` from " << GV.getLinkage() << " to " << NewLinkage
1778 << "\n");
1779 GV.setLinkage(NewLinkage);
1780 }
1781 // Remove declarations from comdats, including available_externally
1782 // as this is a declaration for the linker, and will be dropped eventually.
1783 // It is illegal for comdats to contain declarations.
1784 auto *GO = dyn_cast_or_null<GlobalObject>(&GV);
1785 if (GO && GO->isDeclarationForLinker() && GO->hasComdat()) {
1786 if (GO->getComdat()->getName() == GO->getName())
1787 NonPrevailingComdats.insert(GO->getComdat());
1788 GO->setComdat(nullptr);
1789 }
1790 };
1791
1792 // Process functions and global now
1793 for (auto &GV : TheModule)
1794 FinalizeInModule(GV, PropagateAttrs);
1795 for (auto &GV : TheModule.globals())
1796 FinalizeInModule(GV);
1797 for (auto &GV : TheModule.aliases())
1798 FinalizeInModule(GV);
1799
1800 // For a non-prevailing comdat, all its members must be available_externally.
1801 // FinalizeInModule has handled non-local-linkage GlobalValues. Here we handle
1802 // local linkage GlobalValues.
1803 if (NonPrevailingComdats.empty())
1804 return;
1805 for (auto &GO : TheModule.global_objects()) {
1806 if (auto *C = GO.getComdat(); C && NonPrevailingComdats.count(C)) {
1807 GO.setComdat(nullptr);
1809 }
1810 }
1811 bool Changed;
1812 do {
1813 Changed = false;
1814 // If an alias references a GlobalValue in a non-prevailing comdat, change
1815 // it to available_externally. For simplicity we only handle GlobalValue and
1816 // ConstantExpr with a base object. ConstantExpr without a base object is
1817 // unlikely used in a COMDAT.
1818 for (auto &GA : TheModule.aliases()) {
1819 if (GA.hasAvailableExternallyLinkage())
1820 continue;
1821 GlobalObject *Obj = GA.getAliaseeObject();
1822 assert(Obj && "aliasee without an base object is unimplemented");
1823 if (Obj->hasAvailableExternallyLinkage()) {
1825 Changed = true;
1826 }
1827 }
1828 } while (Changed);
1829}
1830
1831/// Run internalization on \p TheModule based on symmary analysis.
1833 const GVSummaryMapTy &DefinedGlobals) {
1834 llvm::TimeTraceScope timeScope("ThinLTO internalize module");
1835 // Declare a callback for the internalize pass that will ask for every
1836 // candidate GlobalValue if it can be internalized or not.
1837 auto MustPreserveGV = [&](const GlobalValue &GV) -> bool {
1838 // It may be the case that GV is on a chain of an ifunc, its alias and
1839 // subsequent aliases. In this case, the summary for the value is not
1840 // available.
1841 if (isa<GlobalIFunc>(&GV) ||
1842 (isa<GlobalAlias>(&GV) &&
1843 isa<GlobalIFunc>(cast<GlobalAlias>(&GV)->getAliaseeObject())))
1844 return true;
1845
1846 // Lookup the linkage recorded in the summaries during global analysis.
1847 auto GS = DefinedGlobals.find(GV.getGUID());
1848 if (GS == DefinedGlobals.end()) {
1849 // Must have been promoted (possibly conservatively). Find original
1850 // name so that we can access the correct summary and see if it can
1851 // be internalized again.
1852 // FIXME: Eventually we should control promotion instead of promoting
1853 // and internalizing again.
1854 StringRef OrigName =
1856 std::string OrigId = GlobalValue::getGlobalIdentifier(
1858 TheModule.getSourceFileName());
1859 GS = DefinedGlobals.find(
1861 if (GS == DefinedGlobals.end()) {
1862 // Also check the original non-promoted non-globalized name. In some
1863 // cases a preempted weak value is linked in as a local copy because
1864 // it is referenced by an alias (IRLinker::linkGlobalValueProto).
1865 // In that case, since it was originally not a local value, it was
1866 // recorded in the index using the original name.
1867 // FIXME: This may not be needed once PR27866 is fixed.
1868 GS = DefinedGlobals.find(
1870 assert(GS != DefinedGlobals.end());
1871 }
1872 }
1873 return !GlobalValue::isLocalLinkage(GS->second->linkage());
1874 };
1875
1876 // FIXME: See if we can just internalize directly here via linkage changes
1877 // based on the index, rather than invoking internalizeModule.
1878 internalizeModule(TheModule, MustPreserveGV);
1879}
1880
1881/// Make alias a clone of its aliasee.
1884
1885 ValueToValueMapTy VMap;
1886 Function *NewFn = CloneFunction(Fn, VMap);
1887 // Clone should use the original alias's linkage, visibility and name, and we
1888 // ensure all uses of alias instead use the new clone (casted if necessary).
1889 NewFn->setLinkage(GA->getLinkage());
1890 NewFn->setVisibility(GA->getVisibility());
1891 GA->replaceAllUsesWith(NewFn);
1892 NewFn->takeName(GA);
1893 return NewFn;
1894}
1895
1896// Internalize values that we marked with specific attribute
1897// in processGlobalForThinLTO.
1899 for (auto &GV : M.globals())
1900 // Skip GVs which have been converted to declarations
1901 // by dropDeadSymbols.
1902 if (!GV.isDeclaration() && GV.hasAttribute("thinlto-internalize")) {
1903 GV.setLinkage(GlobalValue::InternalLinkage);
1904 GV.setVisibility(GlobalValue::DefaultVisibility);
1905 }
1906}
1907
1908/// When a function carrying !implicit.ref is imported via ThinLTO, the
1909/// referenced global arrives as available_externally. This string can be dead
1910/// code eliminated since it has no IR uses -- only metadata references. Adding
1911/// it to llvm.compiler.used prevents elimination.
1915 for (Function &F : M) {
1916 if (!F.hasAvailableExternallyLinkage() ||
1917 !F.hasMetadata(LLVMContext::MD_implicit_ref))
1918 continue;
1920 F.getMetadata(LLVMContext::MD_implicit_ref, MDs);
1921 for (MDNode *MD : MDs) {
1922 auto *Op = MD->getOperand(0).get();
1923 if (!Op)
1924 continue;
1925 if (auto *VAM = dyn_cast<ValueAsMetadata>(Op))
1926 if (auto *GV = dyn_cast<GlobalVariable>(VAM->getValue()))
1927 if (GV->hasAvailableExternallyLinkage() && Seen.insert(GV).second)
1928 ToProtect.push_back(GV);
1929 }
1930 }
1931 if (!ToProtect.empty())
1932 appendToCompilerUsed(M, ToProtect);
1933}
1934
1935// Automatically import functions in Module \p DestModule based on the summaries
1936// index.
1938 Module &DestModule, const FunctionImporter::ImportMapTy &ImportList) {
1939 LLVM_DEBUG(dbgs() << "Starting import for Module "
1940 << DestModule.getModuleIdentifier() << "\n");
1941 unsigned ImportedCount = 0, ImportedGVCount = 0;
1942 // Before carrying out any imports, see if this module defines functions in
1943 // MoveSymbolGUID. If it does, delete them here (but leave the declaration).
1944 // The function will be imported elsewhere, as extenal linkage, and the
1945 // destination doesn't yet have its definition.
1946 DenseSet<GlobalValue::GUID> MoveSymbolGUIDSet;
1947 MoveSymbolGUIDSet.insert_range(MoveSymbolGUID);
1948 for (auto &F : DestModule)
1949 if (!F.isDeclaration() && MoveSymbolGUIDSet.contains(F.getGUID()))
1950 F.deleteBody();
1951
1952 IRMover Mover(DestModule);
1953
1954 // Do the actual import of functions now, one Module at a time
1955 for (const auto &ModName : ImportList.getSourceModules()) {
1956 llvm::TimeTraceScope timeScope("Import", ModName);
1957 // Get the module for the import
1958 Expected<std::unique_ptr<Module>> SrcModuleOrErr = ModuleLoader(ModName);
1959 if (!SrcModuleOrErr)
1960 return SrcModuleOrErr.takeError();
1961 std::unique_ptr<Module> SrcModule = std::move(*SrcModuleOrErr);
1962 assert(&DestModule.getContext() == &SrcModule->getContext() &&
1963 "Context mismatch");
1964
1965 // If modules were created with lazy metadata loading, materialize it
1966 // now, before linking it (otherwise this will be a noop).
1967 if (Error Err = SrcModule->materializeMetadata())
1968 return std::move(Err);
1969
1970 // Find the globals to import
1971 SetVector<GlobalValue *> GlobalsToImport;
1972 {
1973 llvm::TimeTraceScope functionsScope("Functions");
1974 for (Function &F : *SrcModule) {
1975 if (!F.hasName())
1976 continue;
1977 auto GUID = F.getGUID();
1978 auto MaybeImportType = ImportList.getImportType(ModName, GUID);
1979 bool ImportDefinition =
1980 MaybeImportType == GlobalValueSummary::Definition;
1981
1982 LLVM_DEBUG(dbgs() << (MaybeImportType ? "Is" : "Not")
1983 << " importing function"
1984 << (ImportDefinition
1985 ? " definition "
1986 : (MaybeImportType ? " declaration " : " "))
1987 << GUID << " " << F.getName() << " from "
1988 << SrcModule->getSourceFileName() << "\n");
1989 if (ImportDefinition) {
1990 if (Error Err = F.materialize())
1991 return std::move(Err);
1992 // MemProf should match function's definition and summary,
1993 // 'thinlto_src_module' is needed.
1995 // Add 'thinlto_src_module' and 'thinlto_src_file' metadata for
1996 // statistics and debugging.
1997 F.setMetadata(
1998 "thinlto_src_module",
1999 MDNode::get(DestModule.getContext(),
2000 {MDString::get(DestModule.getContext(),
2001 SrcModule->getModuleIdentifier())}));
2002 F.setMetadata(
2003 "thinlto_src_file",
2004 MDNode::get(DestModule.getContext(),
2005 {MDString::get(DestModule.getContext(),
2006 SrcModule->getSourceFileName())}));
2007 }
2008 GlobalsToImport.insert(&F);
2009 }
2010 }
2011 }
2012 {
2013 llvm::TimeTraceScope globalsScope("Globals");
2014 for (GlobalVariable &GV : SrcModule->globals()) {
2015 if (!GV.hasName())
2016 continue;
2017 auto GUID = GV.getGUID();
2018 auto MaybeImportType = ImportList.getImportType(ModName, GUID);
2019 bool ImportDefinition =
2020 MaybeImportType == GlobalValueSummary::Definition;
2021
2022 LLVM_DEBUG(dbgs() << (MaybeImportType ? "Is" : "Not")
2023 << " importing global"
2024 << (ImportDefinition
2025 ? " definition "
2026 : (MaybeImportType ? " declaration " : " "))
2027 << GUID << " " << GV.getName() << " from "
2028 << SrcModule->getSourceFileName() << "\n");
2029 if (ImportDefinition) {
2030 if (Error Err = GV.materialize())
2031 return std::move(Err);
2032 ImportedGVCount += GlobalsToImport.insert(&GV);
2033 }
2034 }
2035 }
2036 {
2037 llvm::TimeTraceScope aliasesScope("Aliases");
2038 for (GlobalAlias &GA : SrcModule->aliases()) {
2039 if (!GA.hasName() || isa<GlobalIFunc>(GA.getAliaseeObject()))
2040 continue;
2041 auto GUID = GA.getGUID();
2042 auto MaybeImportType = ImportList.getImportType(ModName, GUID);
2043 bool ImportDefinition =
2044 MaybeImportType == GlobalValueSummary::Definition;
2045
2046 LLVM_DEBUG(dbgs() << (MaybeImportType ? "Is" : "Not")
2047 << " importing alias"
2048 << (ImportDefinition
2049 ? " definition "
2050 : (MaybeImportType ? " declaration " : " "))
2051 << GUID << " " << GA.getName() << " from "
2052 << SrcModule->getSourceFileName() << "\n");
2053 if (ImportDefinition) {
2054 if (Error Err = GA.materialize())
2055 return std::move(Err);
2056 // Import alias as a copy of its aliasee.
2057 GlobalObject *GO = GA.getAliaseeObject();
2058 if (Error Err = GO->materialize())
2059 return std::move(Err);
2060 auto *Fn = replaceAliasWithAliasee(SrcModule.get(), &GA);
2061 LLVM_DEBUG(dbgs() << "Is importing aliasee fn " << GO->getGUID()
2062 << " " << GO->getName() << " from "
2063 << SrcModule->getSourceFileName() << "\n");
2065 // Add 'thinlto_src_module' and 'thinlto_src_file' metadata for
2066 // statistics and debugging.
2067 Fn->setMetadata(
2068 "thinlto_src_module",
2069 MDNode::get(DestModule.getContext(),
2070 {MDString::get(DestModule.getContext(),
2071 SrcModule->getModuleIdentifier())}));
2072 Fn->setMetadata(
2073 "thinlto_src_file",
2074 MDNode::get(DestModule.getContext(),
2075 {MDString::get(DestModule.getContext(),
2076 SrcModule->getSourceFileName())}));
2077 }
2078 GlobalsToImport.insert(Fn);
2079 }
2080 }
2081 }
2082
2083 // Upgrade debug info after we're done materializing all the globals and we
2084 // have loaded all the required metadata!
2085 UpgradeDebugInfo(*SrcModule);
2086
2087 // Set the partial sample profile ratio in the profile summary module flag
2088 // of the imported source module, if applicable, so that the profile summary
2089 // module flag will match with that of the destination module when it's
2090 // imported.
2091 SrcModule->setPartialSampleProfileRatio(Index);
2092
2093 // Link in the specified functions.
2094 renameModuleForThinLTO(*SrcModule, Index, ClearDSOLocalOnDeclarations,
2095 &GlobalsToImport);
2096
2097 if (PrintImports) {
2098 for (const auto *GV : GlobalsToImport)
2099 dbgs() << DestModule.getSourceFileName() << ": Import " << GV->getName()
2100 << " from " << SrcModule->getSourceFileName() << "\n";
2101 }
2102
2103 if (Error Err = Mover.move(std::move(SrcModule),
2104 GlobalsToImport.getArrayRef(), nullptr,
2105 /*IsPerformingImport=*/true))
2107 Twine("Function Import: link error: ") +
2108 toString(std::move(Err)));
2109
2110 ImportedCount += GlobalsToImport.size();
2111 NumImportedModules++;
2112 }
2113
2114 internalizeGVsAfterImport(DestModule);
2115
2116 // Protect !implicit.ref-referenced globals imported as available_externally
2117 // from DCE'd. Only needed when globals were actually imported.
2118 if (ImportedGVCount > 0)
2119 protectImplicitRefGlobals(DestModule);
2120
2121 NumImportedFunctions += (ImportedCount - ImportedGVCount);
2122 NumImportedGlobalVars += ImportedGVCount;
2123
2124 // TODO: Print counters for definitions and declarations in the debugging log.
2125 LLVM_DEBUG(dbgs() << "Imported " << ImportedCount - ImportedGVCount
2126 << " functions for Module "
2127 << DestModule.getModuleIdentifier() << "\n");
2128 LLVM_DEBUG(dbgs() << "Imported " << ImportedGVCount
2129 << " global variables for Module "
2130 << DestModule.getModuleIdentifier() << "\n");
2131 return ImportedCount;
2132}
2133
2136 isPrevailing) {
2137 if (SummaryFile.empty())
2138 report_fatal_error("error: -function-import requires -summary-file\n");
2141 if (!IndexPtrOrErr) {
2142 logAllUnhandledErrors(IndexPtrOrErr.takeError(), errs(),
2143 "Error loading file '" + SummaryFile + "': ");
2144 return false;
2145 }
2146 std::unique_ptr<ModuleSummaryIndex> Index = std::move(*IndexPtrOrErr);
2147
2148 // First step is collecting the import list.
2150 FunctionImporter::ImportMapTy ImportList(ImportIDs);
2151 // If requested, simply import all functions in the index. This is used
2152 // when testing distributed backend handling via the opt tool, when
2153 // we have distributed indexes containing exactly the summaries to import.
2154 if (ImportAllIndex)
2156 *Index, ImportList);
2157 else
2158 ComputeCrossModuleImportForModuleForTest(M.getModuleIdentifier(),
2159 isPrevailing, *Index, ImportList);
2160
2161 // Conservatively mark all internal values as promoted. This interface is
2162 // only used when doing importing via the function importing pass. The pass
2163 // is only enabled when testing importing via the 'opt' tool, which does
2164 // not do the ThinLink that would normally determine what values to promote.
2165 for (auto &I : *Index) {
2166 for (auto &S : I.second.getSummaryList()) {
2167 if (GlobalValue::isLocalLinkage(S->linkage()))
2168 S->setExternalLinkageForTest();
2169 }
2170 }
2171
2172 // Next we need to promote to global scope and rename any local values that
2173 // are potentially exported to other modules.
2174 renameModuleForThinLTO(M, *Index, /*ClearDSOLocalOnDeclarations=*/false,
2175 /*GlobalsToImport=*/nullptr);
2176
2177 // Perform the import now.
2178 auto ModuleLoader = [&M](StringRef Identifier) {
2179 return loadFile(std::string(Identifier), M.getContext());
2180 };
2181 FunctionImporter Importer(*Index, ModuleLoader,
2182 /*ClearDSOLocalOnDeclarations=*/false);
2183 Expected<bool> Result = Importer.importFunctions(M, ImportList);
2184
2185 // FIXME: Probably need to propagate Errors through the pass manager.
2186 if (!Result) {
2187 logAllUnhandledErrors(Result.takeError(), errs(),
2188 "Error importing module: ");
2189 return true;
2190 }
2191
2192 return true;
2193}
2194
2197 // This is only used for testing the function import pass via opt, where we
2198 // don't have prevailing information from the LTO context available, so just
2199 // conservatively assume everything is prevailing (which is fine for the very
2200 // limited use of prevailing checking in this pass).
2201 auto isPrevailing = [](GlobalValue::GUID, const GlobalValueSummary *) {
2202 return true;
2203 };
2204 if (!doImportingForModuleForTest(M, isPrevailing))
2205 return PreservedAnalyses::all();
2206
2207 return PreservedAnalyses::none();
2208}
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 protectImplicitRefGlobals(Module &M)
When a function carrying !implicit.ref is imported via ThinLTO, the referenced global arrives as avai...
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:119
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)
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)
Return the entry for the specified key, or abort if no such entry exists.
Definition DenseMap.h:270
ValueT lookup(const_arg_type_t< KeyT > Val) const
Return the entry for the specified key, or a default constructed value if no such entry exists.
Definition DenseMap.h:252
iterator find(const_arg_type_t< KeyT > Val)
Definition DenseMap.h:225
unsigned size() const
Definition DenseMap.h:174
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:221
iterator end()
Definition DenseMap.h:143
Implements a dense probed hash-table based set.
Definition DenseSet.h:289
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
Tagged union holding either a T or a Error.
Definition Error.h:485
Error takeError()
Take ownership of the stored error.
Definition Error.h:612
LLVM_ABI PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
The map maintains the list of imports.
LLVM_ABI AddDefinitionStatus addDefinition(StringRef FromModule, GlobalValue::GUID GUID)
void addGUID(StringRef FromModule, GlobalValue::GUID GUID, GlobalValueSummary::ImportKind ImportKind)
LLVM_ABI SmallVector< StringRef, 0 > getSourceModules() const
LLVM_ABI std::optional< GlobalValueSummary::ImportKind > getImportType(StringRef FromModule, GlobalValue::GUID GUID) const
LLVM_ABI void maybeAddDeclaration(StringRef FromModule, GlobalValue::GUID GUID)
The function importer is automatically importing function from other modules based on the provided su...
LLVM_ABI Expected< bool > importFunctions(Module &M, const ImportMapTy &ImportList)
Import functions in Module M based on the supplied import list.
DenseMap< GlobalValue::GUID, std::tuple< unsigned, const GlobalValueSummary *, std::unique_ptr< ImportFailureInfo > > > ImportThresholdsTy
Map of callee GUID considered for import into a given module to a pair consisting of the largest thre...
ImportFailureReason
The different reasons selectCallee will chose not to import a candidate.
DenseSet< ValueInfo > ExportSetTy
The set contains an entry for every global value that the module exports.
Function summary information to aid decisions and implementation of importing.
unsigned instCount() const
Get the instruction count recorded for this function.
FFlags fflags() const
Get function summary flags.
static Function * Create(FunctionType *Ty, LinkageTypes Linkage, unsigned AddrSpace, const Twine &N="", Module *M=nullptr)
Definition Function.h:168
LLVM_ABI const GlobalObject * getAliaseeObject() const
Definition Globals.cpp:659
Function and variable summary information to aid decisions and implementation of importing.
StringRef modulePath() const
Get the path to the module containing this function.
GlobalValue::LinkageTypes linkage() const
Return linkage type recorded for this global value.
static LLVM_ABI GUID getGUIDAssumingExternalLinkage(StringRef GlobalName)
Return a 64-bit global unique ID constructed from the name of a global symbol.
Definition Globals.cpp:80
VisibilityTypes getVisibility() const
bool isImplicitDSOLocal() const
static bool isLocalLinkage(LinkageTypes Linkage)
LLVM_ABI bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition Globals.cpp:337
LinkageTypes getLinkage() const
uint64_t GUID
Declare a type to represent a global unique identifier for a global value.
ThreadLocalMode getThreadLocalMode() const
static bool isAvailableExternallyLinkage(LinkageTypes Linkage)
void setLinkage(LinkageTypes LT)
unsigned getAddressSpace() const
GUID getGUID() const
Return a 64-bit global unique ID constructed from global value name (i.e.
Module * getParent()
Get the module that this global value is contained inside of...
LLVM_ABI const GlobalObject * getAliaseeObject() const
Definition Globals.cpp:450
void setDSOLocal(bool Local)
PointerType * getType() const
Global values are always pointers.
@ DefaultVisibility
The GV is visible.
Definition GlobalValue.h:68
@ HiddenVisibility
The GV is hidden.
Definition GlobalValue.h:69
static LLVM_ABI std::string getGlobalIdentifier(StringRef Name, GlobalValue::LinkageTypes Linkage, StringRef FileName)
Return the modified name for a global value suitable to be used as the key for a global lookup (e....
Definition Globals.cpp:170
void setVisibility(VisibilityTypes V)
static bool isInterposableLinkage(LinkageTypes Linkage)
Whether the definition of this global may be replaced by something non-equivalent at link time.
LLVM_ABI Error materialize()
Make sure this GlobalValue is fully read.
Definition Globals.cpp:52
LLVM_ABI bool canBeOmittedFromSymbolTable() const
True if GV can be left out of the object symbol table.
Definition Globals.cpp:475
@ InternalLinkage
Rename collisions when linking (static functions).
Definition GlobalValue.h:60
@ WeakODRLinkage
Same, but only replaced by something equivalent.
Definition GlobalValue.h:58
@ ExternalLinkage
Externally visible function.
Definition GlobalValue.h:53
@ AvailableExternallyLinkage
Available for inspection, not emission.
Definition GlobalValue.h:54
@ LinkOnceODRLinkage
Same, but only replaced by something equivalent.
Definition GlobalValue.h:56
Type * getValueType() const
LLVM_ABI Error move(std::unique_ptr< Module > Src, ArrayRef< GlobalValue * > ValuesToLink, LazyCallback AddLazyFor, bool IsPerformingImport)
Move in the provide values in ValuesToLink from Src.
Definition IRMover.cpp:1689
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
Metadata node.
Definition Metadata.h:1069
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition Metadata.h:1561
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:287
const std::string & getSourceFileName() const
Get the module's original source file name.
Definition Module.h:265
iterator_range< alias_iterator > aliases()
Definition Module.h:737
iterator_range< global_iterator > globals()
Definition Module.h:686
const std::string & getModuleIdentifier() const
Get the module identifier which is, essentially, the name of the module.
Definition Module.h:254
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:303
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
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
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:128
iterator end()
Definition StringMap.h:213
iterator find(StringRef Key)
Definition StringMap.h:226
size_type count(StringRef Key) const
count - Return 1 if the element is in the map, 0 otherwise.
Definition StringMap.h:274
bool insert(MapEntryTy *KeyValue)
insert - Insert the specified key/value pair into the map.
Definition StringMap.h:310
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
constexpr size_t size() const
Get the string size.
Definition StringRef.h:144
char front() const
Get the first character in the string.
Definition StringRef.h:147
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:273
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:319
LLVM_ABI void takeName(Value *V)
Transfer the name from V to this value.
Definition Value.cpp:400
std::pair< iterator, bool > insert(const ValueT &V)
Definition DenseSet.h:212
void insert_range(Range &&R)
Definition DenseSet.h:238
bool remove_if(Predicate Pred)
Remove all elements for which Pred returns true.
Definition DenseSet.h:106
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:185
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:190
An efficient, type-erasing, non-owning reference to a callable.
The root is the trivial Path to the root value.
Definition JSON.h:699
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:728
@ OF_Text
The file should be opened in text mode on platforms like z/OS that make this distinction.
Definition FileSystem.h:795
LLVM_ABI StringRef filename(StringRef path LLVM_LIFETIME_BOUND, Style style=Style::native)
Get filename.
Definition Path.cpp:594
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner={})
Log all errors (if any) in E to OS.
Definition Error.cpp:61
static cl::opt< float > ImportHotMultiplier("import-hot-multiplier", cl::init(10.0), cl::Hidden, cl::value_desc("x"), cl::desc("Multiply the `import-instr-limit` threshold for hot callsites"))
static cl::opt< bool > CtxprofMoveRootsToOwnModule("thinlto-move-ctxprof-trees", cl::desc("Move contextual profiling roots and the graphs under them in " "their own module."), cl::Hidden, cl::init(false))
Error createFileError(const Twine &F, Error E)
Concatenate a source file path and/or name with an Error.
Definition Error.h:1415
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:633
LLVM_ABI bool convertToDeclaration(GlobalValue &GV)
Converts value GV to declaration, or replaces with a declaration if it is an alias.
static cl::opt< unsigned > ImportInstrLimit("import-instr-limit", cl::init(100), cl::Hidden, cl::value_desc("N"), cl::desc("Only import functions with less than N instructions"))
Limit on instruction count of imported functions.
LLVM_ABI void renameModuleForThinLTO(Module &M, const ModuleSummaryIndex &Index, bool ClearDSOLocalOnDeclarations, SetVector< GlobalValue * > *GlobalsToImport=nullptr)
Perform in-place global value handling on the given Module for exported local functions renamed and p...
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition Error.h:1321
static cl::opt< float > ImportHotInstrFactor("import-hot-evolution-factor", cl::init(1.0), cl::Hidden, cl::value_desc("x"), cl::desc("As we import functions called from hot callsite, multiply the " "`import-instr-limit` threshold by this factor " "before processing newly imported functions"))
static cl::opt< bool > PrintImports("print-imports", cl::init(false), cl::Hidden, cl::desc("Print imported functions"))
auto map_range(ContainerTy &&C, FuncTy F)
Return a range that applies F to the elements of C.
Definition STLExtras.h:365
@ 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:1745
void sort(IteratorTy Start, IteratorTy End)
Definition STLExtras.h:1635
cl::opt< bool > AlwaysRenamePromotedLocals("always-rename-promoted-locals", cl::init(true), cl::Hidden, cl::desc("Always rename promoted locals."))
Definition LTO.cpp:113
LLVM_ABI void computeDeadSymbolsAndUpdateIndirectCalls(ModuleSummaryIndex &Index, const DenseSet< GlobalValue::GUID > &GUIDPreservedSymbols, function_ref< PrevailingType(GlobalValue::GUID)> isPrevailing)
Compute all the symbols that are "dead": i.e these that can't be reached in the graph from any of the...
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
auto make_first_range(ContainerTy &&c)
Given a container of pairs, return a range over the first elements.
Definition STLExtras.h:1398
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:299
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:551
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 void appendToCompilerUsed(Module &M, ArrayRef< GlobalValue * > Values)
Adds global values to the llvm.compiler.used list.
LLVM_ABI std::unique_ptr< Module > getLazyIRFileModule(StringRef Filename, SMDiagnostic &Err, LLVMContext &Context, bool ShouldLazyLoadMetadata=false)
If the given file holds a bitcode image, return a Module for it which does lazy deserialization of fu...
Definition IRReader.cpp:52
LLVM_ABI void thinLTOInternalizeModule(Module &TheModule, const GVSummaryMapTy &DefinedGlobals)
Internalize TheModule based on the information recorded in the summaries during global summary-based ...
cl::opt< bool > ForceImportAll
DWARFExpression::Operation Op
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'"))
SmallPtrSet< GlobalValueSummary *, 0 > GVSummaryPtrSet
A set of global value summary pointers.
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.