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