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