LLVM 19.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/Constants.h"
23#include "llvm/IR/Function.h"
24#include "llvm/IR/GlobalAlias.h"
26#include "llvm/IR/GlobalValue.h"
28#include "llvm/IR/Metadata.h"
29#include "llvm/IR/Module.h"
32#include "llvm/Linker/IRMover.h"
35#include "llvm/Support/Debug.h"
36#include "llvm/Support/Errc.h"
37#include "llvm/Support/Error.h"
40#include "llvm/Support/JSON.h"
47#include <cassert>
48#include <memory>
49#include <set>
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
74/// Limit on instruction count of imported functions.
76 "import-instr-limit", cl::init(100), cl::Hidden, cl::value_desc("N"),
77 cl::desc("Only import functions with less than N instructions"));
78
80 "import-cutoff", cl::init(-1), cl::Hidden, cl::value_desc("N"),
81 cl::desc("Only import first N functions if N>=0 (default -1)"));
82
83static cl::opt<bool>
84 ForceImportAll("force-import-all", cl::init(false), cl::Hidden,
85 cl::desc("Import functions with noinline attribute"));
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
177namespace llvm {
179}
180
181// Load lazily a module from \p FileName in \p Context.
182static std::unique_ptr<Module> loadFile(const std::string &FileName,
183 LLVMContext &Context) {
184 SMDiagnostic Err;
185 LLVM_DEBUG(dbgs() << "Loading '" << FileName << "'\n");
186 // Metadata isn't loaded until functions are imported, to minimize
187 // the memory overhead.
188 std::unique_ptr<Module> Result =
189 getLazyIRFileModule(FileName, Err, Context,
190 /* ShouldLazyLoadMetadata = */ true);
191 if (!Result) {
192 Err.print("function-import", errs());
193 report_fatal_error("Abort");
194 }
195
196 return Result;
197}
198
199/// Given a list of possible callee implementation for a call site, qualify the
200/// legality of importing each. The return is a range of pairs. Each pair
201/// corresponds to a candidate. The first value is the ImportFailureReason for
202/// that candidate, the second is the candidate.
205 ArrayRef<std::unique_ptr<GlobalValueSummary>> CalleeSummaryList,
206 StringRef CallerModulePath) {
207 return llvm::map_range(
208 CalleeSummaryList,
209 [&Index, CalleeSummaryList,
210 CallerModulePath](const std::unique_ptr<GlobalValueSummary> &SummaryPtr)
212 const GlobalValueSummary *> {
213 auto *GVSummary = SummaryPtr.get();
214 if (!Index.isGlobalValueLive(GVSummary))
215 return {FunctionImporter::ImportFailureReason::NotLive, GVSummary};
216
217 if (GlobalValue::isInterposableLinkage(GVSummary->linkage()))
218 return {FunctionImporter::ImportFailureReason::InterposableLinkage,
219 GVSummary};
220
221 auto *Summary = dyn_cast<FunctionSummary>(GVSummary->getBaseObject());
222
223 // Ignore any callees that aren't actually functions. This could happen
224 // in the case of GUID hash collisions. It could also happen in theory
225 // for SamplePGO profiles collected on old versions of the code after
226 // renaming, since we synthesize edges to any inlined callees appearing
227 // in the profile.
228 if (!Summary)
229 return {FunctionImporter::ImportFailureReason::GlobalVar, GVSummary};
230
231 // If this is a local function, make sure we import the copy
232 // in the caller's module. The only time a local function can
233 // share an entry in the index is if there is a local with the same name
234 // in another module that had the same source file name (in a different
235 // directory), where each was compiled in their own directory so there
236 // was not distinguishing path.
237 // However, do the import from another module if there is only one
238 // entry in the list - in that case this must be a reference due
239 // to indirect call profile data, since a function pointer can point to
240 // a local in another module.
241 if (GlobalValue::isLocalLinkage(Summary->linkage()) &&
242 CalleeSummaryList.size() > 1 &&
243 Summary->modulePath() != CallerModulePath)
244 return {
245 FunctionImporter::ImportFailureReason::LocalLinkageNotInModule,
246 GVSummary};
247
248 // Skip if it isn't legal to import (e.g. may reference unpromotable
249 // locals).
250 if (Summary->notEligibleToImport())
251 return {FunctionImporter::ImportFailureReason::NotEligible,
252 GVSummary};
253
254 return {FunctionImporter::ImportFailureReason::None, GVSummary};
255 });
256}
257
258/// Given a list of possible callee implementation for a call site, select one
259/// that fits the \p Threshold for function definition import. If none are
260/// found, the Reason will give the last reason for the failure (last, in the
261/// order of CalleeSummaryList entries). While looking for a callee definition,
262/// sets \p TooLargeOrNoInlineSummary to the last seen too-large or noinline
263/// candidate; other modules may want to know the function summary or
264/// declaration even if a definition is not needed.
265///
266/// FIXME: select "best" instead of first that fits. But what is "best"?
267/// - The smallest: more likely to be inlined.
268/// - The one with the least outgoing edges (already well optimized).
269/// - One from a module already being imported from in order to reduce the
270/// number of source modules parsed/linked.
271/// - One that has PGO data attached.
272/// - [insert you fancy metric here]
273static const GlobalValueSummary *
275 ArrayRef<std::unique_ptr<GlobalValueSummary>> CalleeSummaryList,
276 unsigned Threshold, StringRef CallerModulePath,
277 const GlobalValueSummary *&TooLargeOrNoInlineSummary,
279 // Records the last summary with reason noinline or too-large.
280 TooLargeOrNoInlineSummary = nullptr;
281 auto QualifiedCandidates =
282 qualifyCalleeCandidates(Index, CalleeSummaryList, CallerModulePath);
283 for (auto QualifiedValue : QualifiedCandidates) {
284 Reason = QualifiedValue.first;
285 // Skip a summary if its import is not (proved to be) legal.
286 if (Reason != FunctionImporter::ImportFailureReason::None)
287 continue;
288 auto *Summary =
289 cast<FunctionSummary>(QualifiedValue.second->getBaseObject());
290
291 // Don't bother importing the definition if the chance of inlining it is
292 // not high enough (except under `--force-import-all`).
293 if ((Summary->instCount() > Threshold) && !Summary->fflags().AlwaysInline &&
295 TooLargeOrNoInlineSummary = Summary;
296 Reason = FunctionImporter::ImportFailureReason::TooLarge;
297 continue;
298 }
299
300 // Don't bother importing the definition if we can't inline it anyway.
301 if (Summary->fflags().NoInline && !ForceImportAll) {
302 TooLargeOrNoInlineSummary = Summary;
303 Reason = FunctionImporter::ImportFailureReason::NoInline;
304 continue;
305 }
306
307 return Summary;
308 }
309 return nullptr;
310}
311
312namespace {
313
314using EdgeInfo = std::tuple<const FunctionSummary *, unsigned /* Threshold */>;
315
316} // anonymous namespace
317
318/// Import globals referenced by a function or other globals that are being
319/// imported, if importing such global is possible.
320class GlobalsImporter final {
322 const GVSummaryMapTy &DefinedGVSummaries;
324 IsPrevailing;
327
328 bool shouldImportGlobal(const ValueInfo &VI) {
329 const auto &GVS = DefinedGVSummaries.find(VI.getGUID());
330 if (GVS == DefinedGVSummaries.end())
331 return true;
332 // We should not skip import if the module contains a non-prevailing
333 // definition with interposable linkage type. This is required for
334 // correctness in the situation where there is a prevailing def available
335 // for import and marked read-only. In this case, the non-prevailing def
336 // will be converted to a declaration, while the prevailing one becomes
337 // internal, thus no definitions will be available for linking. In order to
338 // prevent undefined symbol link error, the prevailing definition must be
339 // imported.
340 // FIXME: Consider adding a check that the suitable prevailing definition
341 // exists and marked read-only.
342 if (VI.getSummaryList().size() > 1 &&
343 GlobalValue::isInterposableLinkage(GVS->second->linkage()) &&
344 !IsPrevailing(VI.getGUID(), GVS->second))
345 return true;
346
347 return false;
348 }
349
350 void
351 onImportingSummaryImpl(const GlobalValueSummary &Summary,
353 for (const auto &VI : Summary.refs()) {
354 if (!shouldImportGlobal(VI)) {
356 dbgs() << "Ref ignored! Target already in destination module.\n");
357 continue;
358 }
359
360 LLVM_DEBUG(dbgs() << " ref -> " << VI << "\n");
361
362 // If this is a local variable, make sure we import the copy
363 // in the caller's module. The only time a local variable can
364 // share an entry in the index is if there is a local with the same name
365 // in another module that had the same source file name (in a different
366 // directory), where each was compiled in their own directory so there
367 // was not distinguishing path.
368 auto LocalNotInModule =
369 [&](const GlobalValueSummary *RefSummary) -> bool {
370 return GlobalValue::isLocalLinkage(RefSummary->linkage()) &&
371 RefSummary->modulePath() != Summary.modulePath();
372 };
373
374 for (const auto &RefSummary : VI.getSummaryList()) {
375 const auto *GVS = dyn_cast<GlobalVarSummary>(RefSummary.get());
376 // Functions could be referenced by global vars - e.g. a vtable; but we
377 // don't currently imagine a reason those would be imported here, rather
378 // than as part of the logic deciding which functions to import (i.e.
379 // based on profile information). Should we decide to handle them here,
380 // we can refactor accordingly at that time.
381 if (!GVS || !Index.canImportGlobalVar(GVS, /* AnalyzeRefs */ true) ||
382 LocalNotInModule(GVS))
383 continue;
384
385 // If there isn't an entry for GUID, insert <GUID, Definition> pair.
386 // Otherwise, definition should take precedence over declaration.
387 auto [Iter, Inserted] =
388 ImportList[RefSummary->modulePath()].try_emplace(
389 VI.getGUID(), GlobalValueSummary::Definition);
390 // Only update stat and exports if we haven't already imported this
391 // variable.
392 if (!Inserted) {
393 // Set the value to 'std::min(existing-value, new-value)' to make
394 // sure a definition takes precedence over a declaration.
395 Iter->second = std::min(GlobalValueSummary::Definition, Iter->second);
396 break;
397 }
398 NumImportedGlobalVarsThinLink++;
399 // Any references made by this variable will be marked exported
400 // later, in ComputeCrossModuleImport, after import decisions are
401 // complete, which is more efficient than adding them here.
402 if (ExportLists)
403 (*ExportLists)[RefSummary->modulePath()][VI] =
405
406 // If variable is not writeonly we attempt to recursively analyze
407 // its references in order to import referenced constants.
408 if (!Index.isWriteOnly(GVS))
409 Worklist.emplace_back(GVS);
410 break;
411 }
412 }
413 }
414
415public:
417 const ModuleSummaryIndex &Index, const GVSummaryMapTy &DefinedGVSummaries,
419 IsPrevailing,
422 : Index(Index), DefinedGVSummaries(DefinedGVSummaries),
423 IsPrevailing(IsPrevailing), ImportList(ImportList),
424 ExportLists(ExportLists) {}
425
428 onImportingSummaryImpl(Summary, Worklist);
429 while (!Worklist.empty())
430 onImportingSummaryImpl(*Worklist.pop_back_val(), Worklist);
431 }
432};
433
435
436/// Determine the list of imports and exports for each module.
438protected:
443
450
451public:
452 virtual ~ModuleImportsManager() = default;
453
454 /// Given the list of globals defined in a module, compute the list of imports
455 /// as well as the list of "exports", i.e. the list of symbols referenced from
456 /// another module (that may require promotion).
457 virtual void
458 computeImportForModule(const GVSummaryMapTy &DefinedGVSummaries,
459 StringRef ModName,
461
462 static std::unique_ptr<ModuleImportsManager>
467 nullptr);
468};
469
470/// A ModuleImportsManager that operates based on a workload definition (see
471/// -thinlto-workload-def). For modules that do not define workload roots, it
472/// applies the base ModuleImportsManager import policy.
474 // Keep a module name -> value infos to import association. We use it to
475 // determine if a module's import list should be done by the base
476 // ModuleImportsManager or by us.
478
479 void
480 computeImportForModule(const GVSummaryMapTy &DefinedGVSummaries,
481 StringRef ModName,
482 FunctionImporter::ImportMapTy &ImportList) override {
483 auto SetIter = Workloads.find(ModName);
484 if (SetIter == Workloads.end()) {
485 LLVM_DEBUG(dbgs() << "[Workload] " << ModName
486 << " does not contain the root of any context.\n");
487 return ModuleImportsManager::computeImportForModule(DefinedGVSummaries,
488 ModName, ImportList);
489 }
490 LLVM_DEBUG(dbgs() << "[Workload] " << ModName
491 << " contains the root(s) of context(s).\n");
492
493 GlobalsImporter GVI(Index, DefinedGVSummaries, IsPrevailing, ImportList,
495 auto &ValueInfos = SetIter->second;
496 SmallVector<EdgeInfo, 128> GlobWorklist;
497 for (auto &VI : llvm::make_early_inc_range(ValueInfos)) {
498 auto It = DefinedGVSummaries.find(VI.getGUID());
499 if (It != DefinedGVSummaries.end() &&
500 IsPrevailing(VI.getGUID(), It->second)) {
502 dbgs() << "[Workload] " << VI.name()
503 << " has the prevailing variant already in the module "
504 << ModName << ". No need to import\n");
505 continue;
506 }
507 auto Candidates =
508 qualifyCalleeCandidates(Index, VI.getSummaryList(), ModName);
509
510 const GlobalValueSummary *GVS = nullptr;
511 auto PotentialCandidates = llvm::map_range(
513 Candidates,
514 [&](const auto &Candidate) {
515 LLVM_DEBUG(dbgs() << "[Workflow] Candidate for " << VI.name()
516 << " from " << Candidate.second->modulePath()
517 << " ImportFailureReason: "
518 << getFailureName(Candidate.first) << "\n");
519 return Candidate.first ==
520 FunctionImporter::ImportFailureReason::None;
521 }),
522 [](const auto &Candidate) { return Candidate.second; });
523 if (PotentialCandidates.empty()) {
524 LLVM_DEBUG(dbgs() << "[Workload] Not importing " << VI.name()
525 << " because can't find eligible Callee. Guid is: "
526 << Function::getGUID(VI.name()) << "\n");
527 continue;
528 }
529 /// We will prefer importing the prevailing candidate, if not, we'll
530 /// still pick the first available candidate. The reason we want to make
531 /// sure we do import the prevailing candidate is because the goal of
532 /// workload-awareness is to enable optimizations specializing the call
533 /// graph of that workload. Suppose a function is already defined in the
534 /// module, but it's not the prevailing variant. Suppose also we do not
535 /// inline it (in fact, if it were interposable, we can't inline it),
536 /// but we could specialize it to the workload in other ways. However,
537 /// the linker would drop it in the favor of the prevailing copy.
538 /// Instead, by importing the prevailing variant (assuming also the use
539 /// of `-avail-extern-to-local`), we keep the specialization. We could
540 /// alteranatively make the non-prevailing variant local, but the
541 /// prevailing one is also the one for which we would have previously
542 /// collected profiles, making it preferrable.
543 auto PrevailingCandidates = llvm::make_filter_range(
544 PotentialCandidates, [&](const auto *Candidate) {
545 return IsPrevailing(VI.getGUID(), Candidate);
546 });
547 if (PrevailingCandidates.empty()) {
548 GVS = *PotentialCandidates.begin();
549 if (!llvm::hasSingleElement(PotentialCandidates) &&
552 dbgs()
553 << "[Workload] Found multiple non-prevailing candidates for "
554 << VI.name()
555 << ". This is unexpected. Are module paths passed to the "
556 "compiler unique for the modules passed to the linker?");
557 // We could in theory have multiple (interposable) copies of a symbol
558 // when there is no prevailing candidate, if say the prevailing copy was
559 // in a native object being linked in. However, we should in theory be
560 // marking all of these non-prevailing IR copies dead in that case, in
561 // which case they won't be candidates.
562 assert(GVS->isLive());
563 } else {
564 assert(llvm::hasSingleElement(PrevailingCandidates));
565 GVS = *PrevailingCandidates.begin();
566 }
567
568 auto ExportingModule = GVS->modulePath();
569 // We checked that for the prevailing case, but if we happen to have for
570 // example an internal that's defined in this module, it'd have no
571 // PrevailingCandidates.
572 if (ExportingModule == ModName) {
573 LLVM_DEBUG(dbgs() << "[Workload] Not importing " << VI.name()
574 << " because its defining module is the same as the "
575 "current module\n");
576 continue;
577 }
578 LLVM_DEBUG(dbgs() << "[Workload][Including]" << VI.name() << " from "
579 << ExportingModule << " : "
580 << Function::getGUID(VI.name()) << "\n");
581 ImportList[ExportingModule][VI.getGUID()] =
583 GVI.onImportingSummary(*GVS);
584 if (ExportLists)
585 (*ExportLists)[ExportingModule][VI] = GlobalValueSummary::Definition;
586 }
587 LLVM_DEBUG(dbgs() << "[Workload] Done\n");
588 }
589
590public:
597 // Since the workload def uses names, we need a quick lookup
598 // name->ValueInfo.
599 StringMap<ValueInfo> NameToValueInfo;
600 StringSet<> AmbiguousNames;
601 for (auto &I : Index) {
602 ValueInfo VI = Index.getValueInfo(I);
603 if (!NameToValueInfo.insert(std::make_pair(VI.name(), VI)).second)
604 LLVM_DEBUG(AmbiguousNames.insert(VI.name()));
605 }
606 auto DbgReportIfAmbiguous = [&](StringRef Name) {
607 LLVM_DEBUG(if (AmbiguousNames.count(Name) > 0) {
608 dbgs() << "[Workload] Function name " << Name
609 << " present in the workload definition is ambiguous. Consider "
610 "compiling with -funique-internal-linkage-names.";
611 });
612 };
613 std::error_code EC;
615 if (std::error_code EC = BufferOrErr.getError()) {
616 report_fatal_error("Failed to open context file");
617 return;
618 }
619 auto Buffer = std::move(BufferOrErr.get());
620 std::map<std::string, std::vector<std::string>> WorkloadDefs;
621 json::Path::Root NullRoot;
622 // The JSON is supposed to contain a dictionary matching the type of
623 // WorkloadDefs. For example:
624 // {
625 // "rootFunction_1": ["function_to_import_1", "function_to_import_2"],
626 // "rootFunction_2": ["function_to_import_3", "function_to_import_4"]
627 // }
628 auto Parsed = json::parse(Buffer->getBuffer());
629 if (!Parsed)
630 report_fatal_error(Parsed.takeError());
631 if (!json::fromJSON(*Parsed, WorkloadDefs, NullRoot))
632 report_fatal_error("Invalid thinlto contextual profile format.");
633 for (const auto &Workload : WorkloadDefs) {
634 const auto &Root = Workload.first;
635 DbgReportIfAmbiguous(Root);
636 LLVM_DEBUG(dbgs() << "[Workload] Root: " << Root << "\n");
637 const auto &AllCallees = Workload.second;
638 auto RootIt = NameToValueInfo.find(Root);
639 if (RootIt == NameToValueInfo.end()) {
640 LLVM_DEBUG(dbgs() << "[Workload] Root " << Root
641 << " not found in this linkage unit.\n");
642 continue;
643 }
644 auto RootVI = RootIt->second;
645 if (RootVI.getSummaryList().size() != 1) {
646 LLVM_DEBUG(dbgs() << "[Workload] Root " << Root
647 << " should have exactly one summary, but has "
648 << RootVI.getSummaryList().size() << ". Skipping.\n");
649 continue;
650 }
651 StringRef RootDefiningModule =
652 RootVI.getSummaryList().front()->modulePath();
653 LLVM_DEBUG(dbgs() << "[Workload] Root defining module for " << Root
654 << " is : " << RootDefiningModule << "\n");
655 auto &Set = Workloads[RootDefiningModule];
656 for (const auto &Callee : AllCallees) {
657 LLVM_DEBUG(dbgs() << "[Workload] " << Callee << "\n");
658 DbgReportIfAmbiguous(Callee);
659 auto ElemIt = NameToValueInfo.find(Callee);
660 if (ElemIt == NameToValueInfo.end()) {
661 LLVM_DEBUG(dbgs() << "[Workload] " << Callee << " not found\n");
662 continue;
663 }
664 Set.insert(ElemIt->second);
665 }
666 LLVM_DEBUG({
667 dbgs() << "[Workload] Root: " << Root << " we have " << Set.size()
668 << " distinct callees.\n";
669 for (const auto &VI : Set) {
670 dbgs() << "[Workload] Root: " << Root
671 << " Would include: " << VI.getGUID() << "\n";
672 }
673 });
674 }
675 }
676};
677
678std::unique_ptr<ModuleImportsManager> ModuleImportsManager::create(
680 IsPrevailing,
683 if (WorkloadDefinitions.empty()) {
684 LLVM_DEBUG(dbgs() << "[Workload] Using the regular imports manager.\n");
685 return std::unique_ptr<ModuleImportsManager>(
687 }
688 LLVM_DEBUG(dbgs() << "[Workload] Using the contextual imports manager.\n");
689 return std::make_unique<WorkloadImportsManager>(IsPrevailing, Index,
691}
692
693static const char *
695 switch (Reason) {
696 case FunctionImporter::ImportFailureReason::None:
697 return "None";
698 case FunctionImporter::ImportFailureReason::GlobalVar:
699 return "GlobalVar";
700 case FunctionImporter::ImportFailureReason::NotLive:
701 return "NotLive";
702 case FunctionImporter::ImportFailureReason::TooLarge:
703 return "TooLarge";
704 case FunctionImporter::ImportFailureReason::InterposableLinkage:
705 return "InterposableLinkage";
706 case FunctionImporter::ImportFailureReason::LocalLinkageNotInModule:
707 return "LocalLinkageNotInModule";
708 case FunctionImporter::ImportFailureReason::NotEligible:
709 return "NotEligible";
710 case FunctionImporter::ImportFailureReason::NoInline:
711 return "NoInline";
712 }
713 llvm_unreachable("invalid reason");
714}
715
716/// Compute the list of functions to import for a given caller. Mark these
717/// imported functions and the symbols they reference in their source module as
718/// exported from their source module.
720 const FunctionSummary &Summary, const ModuleSummaryIndex &Index,
721 const unsigned Threshold, const GVSummaryMapTy &DefinedGVSummaries,
723 isPrevailing,
724 SmallVectorImpl<EdgeInfo> &Worklist, GlobalsImporter &GVImporter,
727 FunctionImporter::ImportThresholdsTy &ImportThresholds) {
728 GVImporter.onImportingSummary(Summary);
729 static int ImportCount = 0;
730 for (const auto &Edge : Summary.calls()) {
731 ValueInfo VI = Edge.first;
732 LLVM_DEBUG(dbgs() << " edge -> " << VI << " Threshold:" << Threshold
733 << "\n");
734
735 if (ImportCutoff >= 0 && ImportCount >= ImportCutoff) {
736 LLVM_DEBUG(dbgs() << "ignored! import-cutoff value of " << ImportCutoff
737 << " reached.\n");
738 continue;
739 }
740
741 if (DefinedGVSummaries.count(VI.getGUID())) {
742 // FIXME: Consider not skipping import if the module contains
743 // a non-prevailing def with interposable linkage. The prevailing copy
744 // can safely be imported (see shouldImportGlobal()).
745 LLVM_DEBUG(dbgs() << "ignored! Target already in destination module.\n");
746 continue;
747 }
748
749 auto GetBonusMultiplier = [](CalleeInfo::HotnessType Hotness) -> float {
750 if (Hotness == CalleeInfo::HotnessType::Hot)
751 return ImportHotMultiplier;
752 if (Hotness == CalleeInfo::HotnessType::Cold)
754 if (Hotness == CalleeInfo::HotnessType::Critical)
756 return 1.0;
757 };
758
759 const auto NewThreshold =
760 Threshold * GetBonusMultiplier(Edge.second.getHotness());
761
762 auto IT = ImportThresholds.insert(std::make_pair(
763 VI.getGUID(), std::make_tuple(NewThreshold, nullptr, nullptr)));
764 bool PreviouslyVisited = !IT.second;
765 auto &ProcessedThreshold = std::get<0>(IT.first->second);
766 auto &CalleeSummary = std::get<1>(IT.first->second);
767 auto &FailureInfo = std::get<2>(IT.first->second);
768
769 bool IsHotCallsite =
770 Edge.second.getHotness() == CalleeInfo::HotnessType::Hot;
771 bool IsCriticalCallsite =
772 Edge.second.getHotness() == CalleeInfo::HotnessType::Critical;
773
774 const FunctionSummary *ResolvedCalleeSummary = nullptr;
775 if (CalleeSummary) {
776 assert(PreviouslyVisited);
777 // Since the traversal of the call graph is DFS, we can revisit a function
778 // a second time with a higher threshold. In this case, it is added back
779 // to the worklist with the new threshold (so that its own callee chains
780 // can be considered with the higher threshold).
781 if (NewThreshold <= ProcessedThreshold) {
783 dbgs() << "ignored! Target was already imported with Threshold "
784 << ProcessedThreshold << "\n");
785 continue;
786 }
787 // Update with new larger threshold.
788 ProcessedThreshold = NewThreshold;
789 ResolvedCalleeSummary = cast<FunctionSummary>(CalleeSummary);
790 } else {
791 // If we already rejected importing a callee at the same or higher
792 // threshold, don't waste time calling selectCallee.
793 if (PreviouslyVisited && NewThreshold <= ProcessedThreshold) {
795 dbgs() << "ignored! Target was already rejected with Threshold "
796 << ProcessedThreshold << "\n");
798 assert(FailureInfo &&
799 "Expected FailureInfo for previously rejected candidate");
800 FailureInfo->Attempts++;
801 }
802 continue;
803 }
804
806
807 // `SummaryForDeclImport` is an summary eligible for declaration import.
808 const GlobalValueSummary *SummaryForDeclImport = nullptr;
809 CalleeSummary =
810 selectCallee(Index, VI.getSummaryList(), NewThreshold,
811 Summary.modulePath(), SummaryForDeclImport, Reason);
812 if (!CalleeSummary) {
813 // There isn't a callee for definition import but one for declaration
814 // import.
815 if (ImportDeclaration && SummaryForDeclImport) {
816 StringRef DeclSourceModule = SummaryForDeclImport->modulePath();
817
818 // Since definition takes precedence over declaration for the same VI,
819 // try emplace <VI, declaration> pair without checking insert result.
820 // If insert doesn't happen, there must be an existing entry keyed by
821 // VI.
822 if (ExportLists)
823 (*ExportLists)[DeclSourceModule].try_emplace(
825 ImportList[DeclSourceModule].try_emplace(
826 VI.getGUID(), GlobalValueSummary::Declaration);
827 }
828 // Update with new larger threshold if this was a retry (otherwise
829 // we would have already inserted with NewThreshold above). Also
830 // update failure info if requested.
831 if (PreviouslyVisited) {
832 ProcessedThreshold = NewThreshold;
834 assert(FailureInfo &&
835 "Expected FailureInfo for previously rejected candidate");
836 FailureInfo->Reason = Reason;
837 FailureInfo->Attempts++;
838 FailureInfo->MaxHotness =
839 std::max(FailureInfo->MaxHotness, Edge.second.getHotness());
840 }
841 } else if (PrintImportFailures) {
842 assert(!FailureInfo &&
843 "Expected no FailureInfo for newly rejected candidate");
844 FailureInfo = std::make_unique<FunctionImporter::ImportFailureInfo>(
845 VI, Edge.second.getHotness(), Reason, 1);
846 }
847 if (ForceImportAll) {
848 std::string Msg = std::string("Failed to import function ") +
849 VI.name().str() + " due to " +
850 getFailureName(Reason);
851 auto Error = make_error<StringError>(
852 Msg, make_error_code(errc::not_supported));
853 logAllUnhandledErrors(std::move(Error), errs(),
854 "Error importing module: ");
855 break;
856 } else {
858 << "ignored! No qualifying callee with summary found.\n");
859 continue;
860 }
861 }
862
863 // "Resolve" the summary
864 CalleeSummary = CalleeSummary->getBaseObject();
865 ResolvedCalleeSummary = cast<FunctionSummary>(CalleeSummary);
866
867 assert((ResolvedCalleeSummary->fflags().AlwaysInline || ForceImportAll ||
868 (ResolvedCalleeSummary->instCount() <= NewThreshold)) &&
869 "selectCallee() didn't honor the threshold");
870
871 auto ExportModulePath = ResolvedCalleeSummary->modulePath();
872
873 // Try emplace the definition entry, and update stats based on insertion
874 // status.
875 auto [Iter, Inserted] = ImportList[ExportModulePath].try_emplace(
876 VI.getGUID(), GlobalValueSummary::Definition);
877
878 // We previously decided to import this GUID definition if it was already
879 // inserted in the set of imports from the exporting module.
880 if (Inserted || Iter->second == GlobalValueSummary::Declaration) {
881 NumImportedFunctionsThinLink++;
882 if (IsHotCallsite)
883 NumImportedHotFunctionsThinLink++;
884 if (IsCriticalCallsite)
885 NumImportedCriticalFunctionsThinLink++;
886 }
887
888 if (Iter->second == GlobalValueSummary::Declaration)
889 Iter->second = GlobalValueSummary::Definition;
890
891 // Any calls/references made by this function will be marked exported
892 // later, in ComputeCrossModuleImport, after import decisions are
893 // complete, which is more efficient than adding them here.
894 if (ExportLists)
895 (*ExportLists)[ExportModulePath][VI] = GlobalValueSummary::Definition;
896 }
897
898 auto GetAdjustedThreshold = [](unsigned Threshold, bool IsHotCallsite) {
899 // Adjust the threshold for next level of imported functions.
900 // The threshold is different for hot callsites because we can then
901 // inline chains of hot calls.
902 if (IsHotCallsite)
903 return Threshold * ImportHotInstrFactor;
904 return Threshold * ImportInstrFactor;
905 };
906
907 const auto AdjThreshold = GetAdjustedThreshold(Threshold, IsHotCallsite);
908
909 ImportCount++;
910
911 // Insert the newly imported function to the worklist.
912 Worklist.emplace_back(ResolvedCalleeSummary, AdjThreshold);
913 }
914}
915
917 const GVSummaryMapTy &DefinedGVSummaries, StringRef ModName,
918 FunctionImporter::ImportMapTy &ImportList) {
919 // Worklist contains the list of function imported in this module, for which
920 // we will analyse the callees and may import further down the callgraph.
922 GlobalsImporter GVI(Index, DefinedGVSummaries, IsPrevailing, ImportList,
925
926 // Populate the worklist with the import for the functions in the current
927 // module
928 for (const auto &GVSummary : DefinedGVSummaries) {
929#ifndef NDEBUG
930 // FIXME: Change the GVSummaryMapTy to hold ValueInfo instead of GUID
931 // so this map look up (and possibly others) can be avoided.
932 auto VI = Index.getValueInfo(GVSummary.first);
933#endif
934 if (!Index.isGlobalValueLive(GVSummary.second)) {
935 LLVM_DEBUG(dbgs() << "Ignores Dead GUID: " << VI << "\n");
936 continue;
937 }
938 auto *FuncSummary =
939 dyn_cast<FunctionSummary>(GVSummary.second->getBaseObject());
940 if (!FuncSummary)
941 // Skip import for global variables
942 continue;
943 LLVM_DEBUG(dbgs() << "Initialize import for " << VI << "\n");
945 DefinedGVSummaries, IsPrevailing, Worklist, GVI,
946 ImportList, ExportLists, ImportThresholds);
947 }
948
949 // Process the newly imported functions and add callees to the worklist.
950 while (!Worklist.empty()) {
951 auto GVInfo = Worklist.pop_back_val();
952 auto *Summary = std::get<0>(GVInfo);
953 auto Threshold = std::get<1>(GVInfo);
954
955 if (auto *FS = dyn_cast<FunctionSummary>(Summary))
956 computeImportForFunction(*FS, Index, Threshold, DefinedGVSummaries,
957 IsPrevailing, Worklist, GVI, ImportList,
958 ExportLists, ImportThresholds);
959 }
960
961 // Print stats about functions considered but rejected for importing
962 // when requested.
964 dbgs() << "Missed imports into module " << ModName << "\n";
965 for (auto &I : ImportThresholds) {
966 auto &ProcessedThreshold = std::get<0>(I.second);
967 auto &CalleeSummary = std::get<1>(I.second);
968 auto &FailureInfo = std::get<2>(I.second);
969 if (CalleeSummary)
970 continue; // We are going to import.
971 assert(FailureInfo);
972 FunctionSummary *FS = nullptr;
973 if (!FailureInfo->VI.getSummaryList().empty())
974 FS = dyn_cast<FunctionSummary>(
975 FailureInfo->VI.getSummaryList()[0]->getBaseObject());
976 dbgs() << FailureInfo->VI
977 << ": Reason = " << getFailureName(FailureInfo->Reason)
978 << ", Threshold = " << ProcessedThreshold
979 << ", Size = " << (FS ? (int)FS->instCount() : -1)
980 << ", MaxHotness = " << getHotnessName(FailureInfo->MaxHotness)
981 << ", Attempts = " << FailureInfo->Attempts << "\n";
982 }
983 }
984}
985
986#ifndef NDEBUG
988 auto SL = VI.getSummaryList();
989 return SL.empty()
990 ? false
991 : SL[0]->getSummaryKind() == GlobalValueSummary::GlobalVarKind;
992}
993
996 if (const auto &VI = Index.getValueInfo(G))
997 return isGlobalVarSummary(Index, VI);
998 return false;
999}
1000
1001template <class T>
1002static unsigned numGlobalVarSummaries(const ModuleSummaryIndex &Index, T &Cont,
1003 unsigned &DefinedGVS,
1004 unsigned &DefinedFS) {
1005 unsigned NumGVS = 0;
1006 DefinedGVS = 0;
1007 DefinedFS = 0;
1008 for (auto &[GUID, Type] : Cont) {
1009 if (isGlobalVarSummary(Index, GUID)) {
1011 ++DefinedGVS;
1012 ++NumGVS;
1014 ++DefinedFS;
1015 }
1016 return NumGVS;
1017}
1018#endif
1019
1020#ifndef NDEBUG
1025 DenseSet<GlobalValue::GUID> FlattenedImports;
1026
1027 for (auto &ImportPerModule : ImportLists)
1028 for (auto &ExportPerModule : ImportPerModule.second)
1029 for (auto &[GUID, Type] : ExportPerModule.second)
1030 FlattenedImports.insert(GUID);
1031
1032 // Checks that all GUIDs of read/writeonly vars we see in export lists
1033 // are also in the import lists. Otherwise we my face linker undefs,
1034 // because readonly and writeonly vars are internalized in their
1035 // source modules. The exception would be if it has a linkage type indicating
1036 // that there may have been a copy existing in the importing module (e.g.
1037 // linkonce_odr). In that case we cannot accurately do this checking.
1038 auto IsReadOrWriteOnlyVarNeedingImporting = [&](StringRef ModulePath,
1039 const ValueInfo &VI) {
1040 auto *GVS = dyn_cast_or_null<GlobalVarSummary>(
1041 Index.findSummaryInModule(VI, ModulePath));
1042 return GVS && (Index.isReadOnly(GVS) || Index.isWriteOnly(GVS)) &&
1043 !(GVS->linkage() == GlobalValue::AvailableExternallyLinkage ||
1044 GVS->linkage() == GlobalValue::WeakODRLinkage ||
1045 GVS->linkage() == GlobalValue::LinkOnceODRLinkage);
1046 };
1047
1048 for (auto &ExportPerModule : ExportLists)
1049 for (auto &[VI, Unused] : ExportPerModule.second)
1050 if (!FlattenedImports.count(VI.getGUID()) &&
1051 IsReadOrWriteOnlyVarNeedingImporting(ExportPerModule.first, VI))
1052 return false;
1053
1054 return true;
1055}
1056#endif
1057
1058/// Compute all the import and export for every module using the Index.
1061 const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1063 isPrevailing,
1066 auto MIS = ModuleImportsManager::create(isPrevailing, Index, &ExportLists);
1067 // For each module that has function defined, compute the import/export lists.
1068 for (const auto &DefinedGVSummaries : ModuleToDefinedGVSummaries) {
1069 auto &ImportList = ImportLists[DefinedGVSummaries.first];
1070 LLVM_DEBUG(dbgs() << "Computing import for Module '"
1071 << DefinedGVSummaries.first << "'\n");
1072 MIS->computeImportForModule(DefinedGVSummaries.second,
1073 DefinedGVSummaries.first, ImportList);
1074 }
1075
1076 // When computing imports we only added the variables and functions being
1077 // imported to the export list. We also need to mark any references and calls
1078 // they make as exported as well. We do this here, as it is more efficient
1079 // since we may import the same values multiple times into different modules
1080 // during the import computation.
1081 for (auto &ELI : ExportLists) {
1083 const auto &DefinedGVSummaries =
1084 ModuleToDefinedGVSummaries.lookup(ELI.first);
1085 for (auto &[EI, Type] : ELI.second) {
1086 // If a variable is exported as a declaration, its 'refs' and 'calls' are
1087 // not further exported.
1089 continue;
1090 // Find the copy defined in the exporting module so that we can mark the
1091 // values it references in that specific definition as exported.
1092 // Below we will add all references and called values, without regard to
1093 // whether they are also defined in this module. We subsequently prune the
1094 // list to only include those defined in the exporting module, see comment
1095 // there as to why.
1096 auto DS = DefinedGVSummaries.find(EI.getGUID());
1097 // Anything marked exported during the import computation must have been
1098 // defined in the exporting module.
1099 assert(DS != DefinedGVSummaries.end());
1100 auto *S = DS->getSecond();
1101 S = S->getBaseObject();
1102 if (auto *GVS = dyn_cast<GlobalVarSummary>(S)) {
1103 // Export referenced functions and variables. We don't export/promote
1104 // objects referenced by writeonly variable initializer, because
1105 // we convert such variables initializers to "zeroinitializer".
1106 // See processGlobalForThinLTO.
1107 if (!Index.isWriteOnly(GVS))
1108 for (const auto &VI : GVS->refs()) {
1109 // Try to emplace the declaration entry. If a definition entry
1110 // already exists for key `VI`, this is a no-op.
1112 }
1113 } else {
1114 auto *FS = cast<FunctionSummary>(S);
1115 for (const auto &Edge : FS->calls()) {
1116 // Try to emplace the declaration entry. If a definition entry
1117 // already exists for key `VI`, this is a no-op.
1118 NewExports.try_emplace(Edge.first, GlobalValueSummary::Declaration);
1119 }
1120 for (const auto &Ref : FS->refs()) {
1121 // Try to emplace the declaration entry. If a definition entry
1122 // already exists for key `VI`, this is a no-op.
1124 }
1125 }
1126 }
1127 // Prune list computed above to only include values defined in the
1128 // exporting module. We do this after the above insertion since we may hit
1129 // the same ref/call target multiple times in above loop, and it is more
1130 // efficient to avoid a set lookup each time.
1131 for (auto EI = NewExports.begin(); EI != NewExports.end();) {
1132 if (!DefinedGVSummaries.count(EI->first.getGUID()))
1133 NewExports.erase(EI++);
1134 else
1135 ++EI;
1136 }
1137 ELI.second.insert(NewExports.begin(), NewExports.end());
1138 }
1139
1140 assert(checkVariableImport(Index, ImportLists, ExportLists));
1141#ifndef NDEBUG
1142 LLVM_DEBUG(dbgs() << "Import/Export lists for " << ImportLists.size()
1143 << " modules:\n");
1144 for (auto &ModuleImports : ImportLists) {
1145 auto ModName = ModuleImports.first;
1146 auto &Exports = ExportLists[ModName];
1147 unsigned DefinedGVS = 0, DefinedFS = 0;
1148 unsigned NumGVS =
1149 numGlobalVarSummaries(Index, Exports, DefinedGVS, DefinedFS);
1150 LLVM_DEBUG(dbgs() << "* Module " << ModName << " exports " << DefinedFS
1151 << " function as definitions, "
1152 << Exports.size() - NumGVS - DefinedFS
1153 << " functions as declarations, " << DefinedGVS
1154 << " var definitions and " << NumGVS - DefinedGVS
1155 << " var declarations. Imports from "
1156 << ModuleImports.second.size() << " modules.\n");
1157 for (auto &Src : ModuleImports.second) {
1158 auto SrcModName = Src.first;
1159 unsigned DefinedGVS = 0, DefinedFS = 0;
1160 unsigned NumGVSPerMod =
1161 numGlobalVarSummaries(Index, Src.second, DefinedGVS, DefinedFS);
1162 LLVM_DEBUG(dbgs() << " - " << DefinedFS << " function definitions and "
1163 << Src.second.size() - NumGVSPerMod - DefinedFS
1164 << " function declarations imported from " << SrcModName
1165 << "\n");
1166 LLVM_DEBUG(dbgs() << " - " << DefinedGVS << " global vars definition and "
1167 << NumGVSPerMod - DefinedGVS
1168 << " global vars declaration imported from "
1169 << SrcModName << "\n");
1170 }
1171 }
1172#endif
1173}
1174
1175#ifndef NDEBUG
1177 StringRef ModulePath,
1178 FunctionImporter::ImportMapTy &ImportList) {
1179 LLVM_DEBUG(dbgs() << "* Module " << ModulePath << " imports from "
1180 << ImportList.size() << " modules.\n");
1181 for (auto &Src : ImportList) {
1182 auto SrcModName = Src.first;
1183 unsigned DefinedGVS = 0, DefinedFS = 0;
1184 unsigned NumGVSPerMod =
1185 numGlobalVarSummaries(Index, Src.second, DefinedGVS, DefinedFS);
1186 LLVM_DEBUG(dbgs() << " - " << DefinedFS << " function definitions and "
1187 << Src.second.size() - DefinedFS - NumGVSPerMod
1188 << " function declarations imported from " << SrcModName
1189 << "\n");
1190 LLVM_DEBUG(dbgs() << " - " << DefinedGVS << " var definitions and "
1191 << NumGVSPerMod - DefinedGVS
1192 << " var declarations imported from " << SrcModName
1193 << "\n");
1194 }
1195}
1196#endif
1197
1198/// Compute all the imports for the given module using the Index.
1199///
1200/// \p isPrevailing is a callback that will be called with a global value's GUID
1201/// and summary and should return whether the module corresponding to the
1202/// summary contains the linker-prevailing copy of that value.
1203///
1204/// \p ImportList will be populated with a map that can be passed to
1205/// FunctionImporter::importFunctions() above (see description there).
1207 StringRef ModulePath,
1209 isPrevailing,
1211 FunctionImporter::ImportMapTy &ImportList) {
1212 // Collect the list of functions this module defines.
1213 // GUID -> Summary
1214 GVSummaryMapTy FunctionSummaryMap;
1215 Index.collectDefinedFunctionsForModule(ModulePath, FunctionSummaryMap);
1216
1217 // Compute the import list for this module.
1218 LLVM_DEBUG(dbgs() << "Computing import for Module '" << ModulePath << "'\n");
1219 auto MIS = ModuleImportsManager::create(isPrevailing, Index);
1220 MIS->computeImportForModule(FunctionSummaryMap, ModulePath, ImportList);
1221
1222#ifndef NDEBUG
1223 dumpImportListForModule(Index, ModulePath, ImportList);
1224#endif
1225}
1226
1227/// Mark all external summaries in \p Index for import into the given module.
1228/// Used for testing the case of distributed builds using a distributed index.
1229///
1230/// \p ImportList will be populated with a map that can be passed to
1231/// FunctionImporter::importFunctions() above (see description there).
1233 StringRef ModulePath, const ModuleSummaryIndex &Index,
1234 FunctionImporter::ImportMapTy &ImportList) {
1235 for (const auto &GlobalList : Index) {
1236 // Ignore entries for undefined references.
1237 if (GlobalList.second.SummaryList.empty())
1238 continue;
1239
1240 auto GUID = GlobalList.first;
1241 assert(GlobalList.second.SummaryList.size() == 1 &&
1242 "Expected individual combined index to have one summary per GUID");
1243 auto &Summary = GlobalList.second.SummaryList[0];
1244 // Skip the summaries for the importing module. These are included to
1245 // e.g. record required linkage changes.
1246 if (Summary->modulePath() == ModulePath)
1247 continue;
1248 // Add an entry to provoke importing by thinBackend.
1249 auto [Iter, Inserted] = ImportList[Summary->modulePath()].try_emplace(
1250 GUID, Summary->importType());
1251 if (!Inserted) {
1252 // Use 'std::min' to make sure definition (with enum value 0) takes
1253 // precedence over declaration (with enum value 1).
1254 Iter->second = std::min(Iter->second, Summary->importType());
1255 }
1256 }
1257#ifndef NDEBUG
1258 dumpImportListForModule(Index, ModulePath, ImportList);
1259#endif
1260}
1261
1262// For SamplePGO, the indirect call targets for local functions will
1263// have its original name annotated in profile. We try to find the
1264// corresponding PGOFuncName as the GUID, and fix up the edges
1265// accordingly.
1267 FunctionSummary *FS) {
1268 for (auto &EI : FS->mutableCalls()) {
1269 if (!EI.first.getSummaryList().empty())
1270 continue;
1271 auto GUID = Index.getGUIDFromOriginalID(EI.first.getGUID());
1272 if (GUID == 0)
1273 continue;
1274 // Update the edge to point directly to the correct GUID.
1275 auto VI = Index.getValueInfo(GUID);
1276 if (llvm::any_of(
1277 VI.getSummaryList(),
1278 [&](const std::unique_ptr<GlobalValueSummary> &SummaryPtr) {
1279 // The mapping from OriginalId to GUID may return a GUID
1280 // that corresponds to a static variable. Filter it out here.
1281 // This can happen when
1282 // 1) There is a call to a library function which is not defined
1283 // in the index.
1284 // 2) There is a static variable with the OriginalGUID identical
1285 // to the GUID of the library function in 1);
1286 // When this happens the static variable in 2) will be found,
1287 // which needs to be filtered out.
1288 return SummaryPtr->getSummaryKind() ==
1289 GlobalValueSummary::GlobalVarKind;
1290 }))
1291 continue;
1292 EI.first = VI;
1293 }
1294}
1295
1297 for (const auto &Entry : Index) {
1298 for (const auto &S : Entry.second.SummaryList) {
1299 if (auto *FS = dyn_cast<FunctionSummary>(S.get()))
1301 }
1302 }
1303}
1304
1307 const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
1309 assert(!Index.withGlobalValueDeadStripping());
1310 if (!ComputeDead ||
1311 // Don't do anything when nothing is live, this is friendly with tests.
1312 GUIDPreservedSymbols.empty()) {
1313 // Still need to update indirect calls.
1315 return;
1316 }
1317 unsigned LiveSymbols = 0;
1319 Worklist.reserve(GUIDPreservedSymbols.size() * 2);
1320 for (auto GUID : GUIDPreservedSymbols) {
1321 ValueInfo VI = Index.getValueInfo(GUID);
1322 if (!VI)
1323 continue;
1324 for (const auto &S : VI.getSummaryList())
1325 S->setLive(true);
1326 }
1327
1328 // Add values flagged in the index as live roots to the worklist.
1329 for (const auto &Entry : Index) {
1330 auto VI = Index.getValueInfo(Entry);
1331 for (const auto &S : Entry.second.SummaryList) {
1332 if (auto *FS = dyn_cast<FunctionSummary>(S.get()))
1334 if (S->isLive()) {
1335 LLVM_DEBUG(dbgs() << "Live root: " << VI << "\n");
1336 Worklist.push_back(VI);
1337 ++LiveSymbols;
1338 break;
1339 }
1340 }
1341 }
1342
1343 // Make value live and add it to the worklist if it was not live before.
1344 auto visit = [&](ValueInfo VI, bool IsAliasee) {
1345 // FIXME: If we knew which edges were created for indirect call profiles,
1346 // we could skip them here. Any that are live should be reached via
1347 // other edges, e.g. reference edges. Otherwise, using a profile collected
1348 // on a slightly different binary might provoke preserving, importing
1349 // and ultimately promoting calls to functions not linked into this
1350 // binary, which increases the binary size unnecessarily. Note that
1351 // if this code changes, the importer needs to change so that edges
1352 // to functions marked dead are skipped.
1353
1354 if (llvm::any_of(VI.getSummaryList(),
1355 [](const std::unique_ptr<llvm::GlobalValueSummary> &S) {
1356 return S->isLive();
1357 }))
1358 return;
1359
1360 // We only keep live symbols that are known to be non-prevailing if any are
1361 // available_externally, linkonceodr, weakodr. Those symbols are discarded
1362 // later in the EliminateAvailableExternally pass and setting them to
1363 // not-live could break downstreams users of liveness information (PR36483)
1364 // or limit optimization opportunities.
1365 if (isPrevailing(VI.getGUID()) == PrevailingType::No) {
1366 bool KeepAliveLinkage = false;
1367 bool Interposable = false;
1368 for (const auto &S : VI.getSummaryList()) {
1369 if (S->linkage() == GlobalValue::AvailableExternallyLinkage ||
1370 S->linkage() == GlobalValue::WeakODRLinkage ||
1371 S->linkage() == GlobalValue::LinkOnceODRLinkage)
1372 KeepAliveLinkage = true;
1373 else if (GlobalValue::isInterposableLinkage(S->linkage()))
1374 Interposable = true;
1375 }
1376
1377 if (!IsAliasee) {
1378 if (!KeepAliveLinkage)
1379 return;
1380
1381 if (Interposable)
1383 "Interposable and available_externally/linkonce_odr/weak_odr "
1384 "symbol");
1385 }
1386 }
1387
1388 for (const auto &S : VI.getSummaryList())
1389 S->setLive(true);
1390 ++LiveSymbols;
1391 Worklist.push_back(VI);
1392 };
1393
1394 while (!Worklist.empty()) {
1395 auto VI = Worklist.pop_back_val();
1396 for (const auto &Summary : VI.getSummaryList()) {
1397 if (auto *AS = dyn_cast<AliasSummary>(Summary.get())) {
1398 // If this is an alias, visit the aliasee VI to ensure that all copies
1399 // are marked live and it is added to the worklist for further
1400 // processing of its references.
1401 visit(AS->getAliaseeVI(), true);
1402 continue;
1403 }
1404 for (auto Ref : Summary->refs())
1405 visit(Ref, false);
1406 if (auto *FS = dyn_cast<FunctionSummary>(Summary.get()))
1407 for (auto Call : FS->calls())
1408 visit(Call.first, false);
1409 }
1410 }
1411 Index.setWithGlobalValueDeadStripping();
1412
1413 unsigned DeadSymbols = Index.size() - LiveSymbols;
1414 LLVM_DEBUG(dbgs() << LiveSymbols << " symbols Live, and " << DeadSymbols
1415 << " symbols Dead \n");
1416 NumDeadSymbols += DeadSymbols;
1417 NumLiveSymbols += LiveSymbols;
1418}
1419
1420// Compute dead symbols and propagate constants in combined index.
1423 const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
1425 bool ImportEnabled) {
1426 computeDeadSymbolsAndUpdateIndirectCalls(Index, GUIDPreservedSymbols,
1427 isPrevailing);
1428 if (ImportEnabled)
1429 Index.propagateAttributes(GUIDPreservedSymbols);
1430}
1431
1432/// Compute the set of summaries needed for a ThinLTO backend compilation of
1433/// \p ModulePath.
1435 StringRef ModulePath,
1436 const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1437 const FunctionImporter::ImportMapTy &ImportList,
1438 std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex,
1439 GVSummaryPtrSet &DecSummaries) {
1440 // Include all summaries from the importing module.
1441 ModuleToSummariesForIndex[std::string(ModulePath)] =
1442 ModuleToDefinedGVSummaries.lookup(ModulePath);
1443 // Include summaries for imports.
1444 for (const auto &ILI : ImportList) {
1445 auto &SummariesForIndex = ModuleToSummariesForIndex[std::string(ILI.first)];
1446
1447 const auto &DefinedGVSummaries =
1448 ModuleToDefinedGVSummaries.lookup(ILI.first);
1449 for (const auto &[GUID, Type] : ILI.second) {
1450 const auto &DS = DefinedGVSummaries.find(GUID);
1451 assert(DS != DefinedGVSummaries.end() &&
1452 "Expected a defined summary for imported global value");
1454 DecSummaries.insert(DS->second);
1455
1456 SummariesForIndex[GUID] = DS->second;
1457 }
1458 }
1459}
1460
1461/// Emit the files \p ModulePath will import from into \p OutputFilename.
1464 const std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex) {
1465 std::error_code EC;
1466 raw_fd_ostream ImportsOS(OutputFilename, EC, sys::fs::OpenFlags::OF_None);
1467 if (EC)
1468 return EC;
1469 for (const auto &ILI : ModuleToSummariesForIndex)
1470 // The ModuleToSummariesForIndex map includes an entry for the current
1471 // Module (needed for writing out the index files). We don't want to
1472 // include it in the imports file, however, so filter it out.
1473 if (ILI.first != ModulePath)
1474 ImportsOS << ILI.first << "\n";
1475 return std::error_code();
1476}
1477
1479 LLVM_DEBUG(dbgs() << "Converting to a declaration: `" << GV.getName()
1480 << "\n");
1481 if (Function *F = dyn_cast<Function>(&GV)) {
1482 F->deleteBody();
1483 F->clearMetadata();
1484 F->setComdat(nullptr);
1485 } else if (GlobalVariable *V = dyn_cast<GlobalVariable>(&GV)) {
1486 V->setInitializer(nullptr);
1487 V->setLinkage(GlobalValue::ExternalLinkage);
1488 V->clearMetadata();
1489 V->setComdat(nullptr);
1490 } else {
1491 GlobalValue *NewGV;
1492 if (GV.getValueType()->isFunctionTy())
1493 NewGV =
1494 Function::Create(cast<FunctionType>(GV.getValueType()),
1496 "", GV.getParent());
1497 else
1498 NewGV =
1499 new GlobalVariable(*GV.getParent(), GV.getValueType(),
1500 /*isConstant*/ false, GlobalValue::ExternalLinkage,
1501 /*init*/ nullptr, "",
1502 /*insertbefore*/ nullptr, GV.getThreadLocalMode(),
1503 GV.getType()->getAddressSpace());
1504 NewGV->takeName(&GV);
1505 GV.replaceAllUsesWith(NewGV);
1506 return false;
1507 }
1508 if (!GV.isImplicitDSOLocal())
1509 GV.setDSOLocal(false);
1510 return true;
1511}
1512
1514 const GVSummaryMapTy &DefinedGlobals,
1515 bool PropagateAttrs) {
1516 DenseSet<Comdat *> NonPrevailingComdats;
1517 auto FinalizeInModule = [&](GlobalValue &GV, bool Propagate = false) {
1518 // See if the global summary analysis computed a new resolved linkage.
1519 const auto &GS = DefinedGlobals.find(GV.getGUID());
1520 if (GS == DefinedGlobals.end())
1521 return;
1522
1523 if (Propagate)
1524 if (FunctionSummary *FS = dyn_cast<FunctionSummary>(GS->second)) {
1525 if (Function *F = dyn_cast<Function>(&GV)) {
1526 // TODO: propagate ReadNone and ReadOnly.
1527 if (FS->fflags().ReadNone && !F->doesNotAccessMemory())
1528 F->setDoesNotAccessMemory();
1529
1530 if (FS->fflags().ReadOnly && !F->onlyReadsMemory())
1531 F->setOnlyReadsMemory();
1532
1533 if (FS->fflags().NoRecurse && !F->doesNotRecurse())
1534 F->setDoesNotRecurse();
1535
1536 if (FS->fflags().NoUnwind && !F->doesNotThrow())
1537 F->setDoesNotThrow();
1538 }
1539 }
1540
1541 auto NewLinkage = GS->second->linkage();
1543 // Don't internalize anything here, because the code below
1544 // lacks necessary correctness checks. Leave this job to
1545 // LLVM 'internalize' pass.
1546 GlobalValue::isLocalLinkage(NewLinkage) ||
1547 // In case it was dead and already converted to declaration.
1548 GV.isDeclaration())
1549 return;
1550
1551 // Set the potentially more constraining visibility computed from summaries.
1552 // The DefaultVisibility condition is because older GlobalValueSummary does
1553 // not record DefaultVisibility and we don't want to change protected/hidden
1554 // to default.
1555 if (GS->second->getVisibility() != GlobalValue::DefaultVisibility)
1556 GV.setVisibility(GS->second->getVisibility());
1557
1558 if (NewLinkage == GV.getLinkage())
1559 return;
1560
1561 // Check for a non-prevailing def that has interposable linkage
1562 // (e.g. non-odr weak or linkonce). In that case we can't simply
1563 // convert to available_externally, since it would lose the
1564 // interposable property and possibly get inlined. Simply drop
1565 // the definition in that case.
1568 if (!convertToDeclaration(GV))
1569 // FIXME: Change this to collect replaced GVs and later erase
1570 // them from the parent module once thinLTOResolvePrevailingGUID is
1571 // changed to enable this for aliases.
1572 llvm_unreachable("Expected GV to be converted");
1573 } else {
1574 // If all copies of the original symbol had global unnamed addr and
1575 // linkonce_odr linkage, or if all of them had local unnamed addr linkage
1576 // and are constants, then it should be an auto hide symbol. In that case
1577 // the thin link would have marked it as CanAutoHide. Add hidden
1578 // visibility to the symbol to preserve the property.
1579 if (NewLinkage == GlobalValue::WeakODRLinkage &&
1580 GS->second->canAutoHide()) {
1583 }
1584
1585 LLVM_DEBUG(dbgs() << "ODR fixing up linkage for `" << GV.getName()
1586 << "` from " << GV.getLinkage() << " to " << NewLinkage
1587 << "\n");
1588 GV.setLinkage(NewLinkage);
1589 }
1590 // Remove declarations from comdats, including available_externally
1591 // as this is a declaration for the linker, and will be dropped eventually.
1592 // It is illegal for comdats to contain declarations.
1593 auto *GO = dyn_cast_or_null<GlobalObject>(&GV);
1594 if (GO && GO->isDeclarationForLinker() && GO->hasComdat()) {
1595 if (GO->getComdat()->getName() == GO->getName())
1596 NonPrevailingComdats.insert(GO->getComdat());
1597 GO->setComdat(nullptr);
1598 }
1599 };
1600
1601 // Process functions and global now
1602 for (auto &GV : TheModule)
1603 FinalizeInModule(GV, PropagateAttrs);
1604 for (auto &GV : TheModule.globals())
1605 FinalizeInModule(GV);
1606 for (auto &GV : TheModule.aliases())
1607 FinalizeInModule(GV);
1608
1609 // For a non-prevailing comdat, all its members must be available_externally.
1610 // FinalizeInModule has handled non-local-linkage GlobalValues. Here we handle
1611 // local linkage GlobalValues.
1612 if (NonPrevailingComdats.empty())
1613 return;
1614 for (auto &GO : TheModule.global_objects()) {
1615 if (auto *C = GO.getComdat(); C && NonPrevailingComdats.count(C)) {
1616 GO.setComdat(nullptr);
1618 }
1619 }
1620 bool Changed;
1621 do {
1622 Changed = false;
1623 // If an alias references a GlobalValue in a non-prevailing comdat, change
1624 // it to available_externally. For simplicity we only handle GlobalValue and
1625 // ConstantExpr with a base object. ConstantExpr without a base object is
1626 // unlikely used in a COMDAT.
1627 for (auto &GA : TheModule.aliases()) {
1628 if (GA.hasAvailableExternallyLinkage())
1629 continue;
1630 GlobalObject *Obj = GA.getAliaseeObject();
1631 assert(Obj && "aliasee without an base object is unimplemented");
1632 if (Obj->hasAvailableExternallyLinkage()) {
1634 Changed = true;
1635 }
1636 }
1637 } while (Changed);
1638}
1639
1640/// Run internalization on \p TheModule based on symmary analysis.
1642 const GVSummaryMapTy &DefinedGlobals) {
1643 // Declare a callback for the internalize pass that will ask for every
1644 // candidate GlobalValue if it can be internalized or not.
1645 auto MustPreserveGV = [&](const GlobalValue &GV) -> bool {
1646 // It may be the case that GV is on a chain of an ifunc, its alias and
1647 // subsequent aliases. In this case, the summary for the value is not
1648 // available.
1649 if (isa<GlobalIFunc>(&GV) ||
1650 (isa<GlobalAlias>(&GV) &&
1651 isa<GlobalIFunc>(cast<GlobalAlias>(&GV)->getAliaseeObject())))
1652 return true;
1653
1654 // Lookup the linkage recorded in the summaries during global analysis.
1655 auto GS = DefinedGlobals.find(GV.getGUID());
1656 if (GS == DefinedGlobals.end()) {
1657 // Must have been promoted (possibly conservatively). Find original
1658 // name so that we can access the correct summary and see if it can
1659 // be internalized again.
1660 // FIXME: Eventually we should control promotion instead of promoting
1661 // and internalizing again.
1662 StringRef OrigName =
1664 std::string OrigId = GlobalValue::getGlobalIdentifier(
1666 TheModule.getSourceFileName());
1667 GS = DefinedGlobals.find(GlobalValue::getGUID(OrigId));
1668 if (GS == DefinedGlobals.end()) {
1669 // Also check the original non-promoted non-globalized name. In some
1670 // cases a preempted weak value is linked in as a local copy because
1671 // it is referenced by an alias (IRLinker::linkGlobalValueProto).
1672 // In that case, since it was originally not a local value, it was
1673 // recorded in the index using the original name.
1674 // FIXME: This may not be needed once PR27866 is fixed.
1675 GS = DefinedGlobals.find(GlobalValue::getGUID(OrigName));
1676 assert(GS != DefinedGlobals.end());
1677 }
1678 }
1679 return !GlobalValue::isLocalLinkage(GS->second->linkage());
1680 };
1681
1682 // FIXME: See if we can just internalize directly here via linkage changes
1683 // based on the index, rather than invoking internalizeModule.
1684 internalizeModule(TheModule, MustPreserveGV);
1685}
1686
1687/// Make alias a clone of its aliasee.
1689 Function *Fn = cast<Function>(GA->getAliaseeObject());
1690
1691 ValueToValueMapTy VMap;
1692 Function *NewFn = CloneFunction(Fn, VMap);
1693 // Clone should use the original alias's linkage, visibility and name, and we
1694 // ensure all uses of alias instead use the new clone (casted if necessary).
1695 NewFn->setLinkage(GA->getLinkage());
1696 NewFn->setVisibility(GA->getVisibility());
1697 GA->replaceAllUsesWith(NewFn);
1698 NewFn->takeName(GA);
1699 return NewFn;
1700}
1701
1702// Internalize values that we marked with specific attribute
1703// in processGlobalForThinLTO.
1705 for (auto &GV : M.globals())
1706 // Skip GVs which have been converted to declarations
1707 // by dropDeadSymbols.
1708 if (!GV.isDeclaration() && GV.hasAttribute("thinlto-internalize")) {
1709 GV.setLinkage(GlobalValue::InternalLinkage);
1710 GV.setVisibility(GlobalValue::DefaultVisibility);
1711 }
1712}
1713
1714// Automatically import functions in Module \p DestModule based on the summaries
1715// index.
1717 Module &DestModule, const FunctionImporter::ImportMapTy &ImportList) {
1718 LLVM_DEBUG(dbgs() << "Starting import for Module "
1719 << DestModule.getModuleIdentifier() << "\n");
1720 unsigned ImportedCount = 0, ImportedGVCount = 0;
1721
1722 IRMover Mover(DestModule);
1723 // Do the actual import of functions now, one Module at a time
1724 std::set<StringRef> ModuleNameOrderedList;
1725 for (const auto &FunctionsToImportPerModule : ImportList) {
1726 ModuleNameOrderedList.insert(FunctionsToImportPerModule.first);
1727 }
1728
1729 auto getImportType = [&](const FunctionsToImportTy &GUIDToImportType,
1730 GlobalValue::GUID GUID)
1731 -> std::optional<GlobalValueSummary::ImportKind> {
1732 auto Iter = GUIDToImportType.find(GUID);
1733 if (Iter == GUIDToImportType.end())
1734 return std::nullopt;
1735 return Iter->second;
1736 };
1737
1738 for (const auto &Name : ModuleNameOrderedList) {
1739 // Get the module for the import
1740 const auto &FunctionsToImportPerModule = ImportList.find(Name);
1741 assert(FunctionsToImportPerModule != ImportList.end());
1742 Expected<std::unique_ptr<Module>> SrcModuleOrErr = ModuleLoader(Name);
1743 if (!SrcModuleOrErr)
1744 return SrcModuleOrErr.takeError();
1745 std::unique_ptr<Module> SrcModule = std::move(*SrcModuleOrErr);
1746 assert(&DestModule.getContext() == &SrcModule->getContext() &&
1747 "Context mismatch");
1748
1749 // If modules were created with lazy metadata loading, materialize it
1750 // now, before linking it (otherwise this will be a noop).
1751 if (Error Err = SrcModule->materializeMetadata())
1752 return std::move(Err);
1753
1754 auto &ImportGUIDs = FunctionsToImportPerModule->second;
1755
1756 // Find the globals to import
1757 SetVector<GlobalValue *> GlobalsToImport;
1758 for (Function &F : *SrcModule) {
1759 if (!F.hasName())
1760 continue;
1761 auto GUID = F.getGUID();
1762 auto MaybeImportType = getImportType(ImportGUIDs, GUID);
1763
1764 bool ImportDefinition =
1765 (MaybeImportType &&
1766 (*MaybeImportType == GlobalValueSummary::Definition));
1767
1768 LLVM_DEBUG(dbgs() << (MaybeImportType ? "Is" : "Not")
1769 << " importing function"
1770 << (ImportDefinition
1771 ? " definition "
1772 : (MaybeImportType ? " declaration " : " "))
1773 << GUID << " " << F.getName() << " from "
1774 << SrcModule->getSourceFileName() << "\n");
1775 if (ImportDefinition) {
1776 if (Error Err = F.materialize())
1777 return std::move(Err);
1778 // MemProf should match function's definition and summary,
1779 // 'thinlto_src_module' is needed.
1781 // Add 'thinlto_src_module' and 'thinlto_src_file' metadata for
1782 // statistics and debugging.
1783 F.setMetadata(
1784 "thinlto_src_module",
1785 MDNode::get(DestModule.getContext(),
1786 {MDString::get(DestModule.getContext(),
1787 SrcModule->getModuleIdentifier())}));
1788 F.setMetadata(
1789 "thinlto_src_file",
1790 MDNode::get(DestModule.getContext(),
1791 {MDString::get(DestModule.getContext(),
1792 SrcModule->getSourceFileName())}));
1793 }
1794 GlobalsToImport.insert(&F);
1795 }
1796 }
1797 for (GlobalVariable &GV : SrcModule->globals()) {
1798 if (!GV.hasName())
1799 continue;
1800 auto GUID = GV.getGUID();
1801 auto MaybeImportType = getImportType(ImportGUIDs, GUID);
1802
1803 bool ImportDefinition =
1804 (MaybeImportType &&
1805 (*MaybeImportType == GlobalValueSummary::Definition));
1806
1807 LLVM_DEBUG(dbgs() << (MaybeImportType ? "Is" : "Not")
1808 << " importing global"
1809 << (ImportDefinition
1810 ? " definition "
1811 : (MaybeImportType ? " declaration " : " "))
1812 << GUID << " " << GV.getName() << " from "
1813 << SrcModule->getSourceFileName() << "\n");
1814 if (ImportDefinition) {
1815 if (Error Err = GV.materialize())
1816 return std::move(Err);
1817 ImportedGVCount += GlobalsToImport.insert(&GV);
1818 }
1819 }
1820 for (GlobalAlias &GA : SrcModule->aliases()) {
1821 if (!GA.hasName() || isa<GlobalIFunc>(GA.getAliaseeObject()))
1822 continue;
1823 auto GUID = GA.getGUID();
1824 auto MaybeImportType = getImportType(ImportGUIDs, GUID);
1825
1826 bool ImportDefinition =
1827 (MaybeImportType &&
1828 (*MaybeImportType == GlobalValueSummary::Definition));
1829
1830 LLVM_DEBUG(dbgs() << (MaybeImportType ? "Is" : "Not")
1831 << " importing alias"
1832 << (ImportDefinition
1833 ? " definition "
1834 : (MaybeImportType ? " declaration " : " "))
1835 << GUID << " " << GA.getName() << " from "
1836 << SrcModule->getSourceFileName() << "\n");
1837 if (ImportDefinition) {
1838 if (Error Err = GA.materialize())
1839 return std::move(Err);
1840 // Import alias as a copy of its aliasee.
1841 GlobalObject *GO = GA.getAliaseeObject();
1842 if (Error Err = GO->materialize())
1843 return std::move(Err);
1844 auto *Fn = replaceAliasWithAliasee(SrcModule.get(), &GA);
1845 LLVM_DEBUG(dbgs() << "Is importing aliasee fn " << GO->getGUID() << " "
1846 << GO->getName() << " from "
1847 << SrcModule->getSourceFileName() << "\n");
1849 // Add 'thinlto_src_module' and 'thinlto_src_file' metadata for
1850 // statistics and debugging.
1851 Fn->setMetadata(
1852 "thinlto_src_module",
1853 MDNode::get(DestModule.getContext(),
1854 {MDString::get(DestModule.getContext(),
1855 SrcModule->getModuleIdentifier())}));
1856 Fn->setMetadata(
1857 "thinlto_src_file",
1858 MDNode::get(DestModule.getContext(),
1859 {MDString::get(DestModule.getContext(),
1860 SrcModule->getSourceFileName())}));
1861 }
1862 GlobalsToImport.insert(Fn);
1863 }
1864 }
1865
1866 // Upgrade debug info after we're done materializing all the globals and we
1867 // have loaded all the required metadata!
1868 UpgradeDebugInfo(*SrcModule);
1869
1870 // Set the partial sample profile ratio in the profile summary module flag
1871 // of the imported source module, if applicable, so that the profile summary
1872 // module flag will match with that of the destination module when it's
1873 // imported.
1874 SrcModule->setPartialSampleProfileRatio(Index);
1875
1876 // Link in the specified functions.
1877 if (renameModuleForThinLTO(*SrcModule, Index, ClearDSOLocalOnDeclarations,
1878 &GlobalsToImport))
1879 return true;
1880
1881 if (PrintImports) {
1882 for (const auto *GV : GlobalsToImport)
1883 dbgs() << DestModule.getSourceFileName() << ": Import " << GV->getName()
1884 << " from " << SrcModule->getSourceFileName() << "\n";
1885 }
1886
1887 if (Error Err = Mover.move(std::move(SrcModule),
1888 GlobalsToImport.getArrayRef(), nullptr,
1889 /*IsPerformingImport=*/true))
1891 Twine("Function Import: link error: ") +
1892 toString(std::move(Err)));
1893
1894 ImportedCount += GlobalsToImport.size();
1895 NumImportedModules++;
1896 }
1897
1898 internalizeGVsAfterImport(DestModule);
1899
1900 NumImportedFunctions += (ImportedCount - ImportedGVCount);
1901 NumImportedGlobalVars += ImportedGVCount;
1902
1903 // TODO: Print counters for definitions and declarations in the debugging log.
1904 LLVM_DEBUG(dbgs() << "Imported " << ImportedCount - ImportedGVCount
1905 << " functions for Module "
1906 << DestModule.getModuleIdentifier() << "\n");
1907 LLVM_DEBUG(dbgs() << "Imported " << ImportedGVCount
1908 << " global variables for Module "
1909 << DestModule.getModuleIdentifier() << "\n");
1910 return ImportedCount;
1911}
1912
1915 isPrevailing) {
1916 if (SummaryFile.empty())
1917 report_fatal_error("error: -function-import requires -summary-file\n");
1920 if (!IndexPtrOrErr) {
1921 logAllUnhandledErrors(IndexPtrOrErr.takeError(), errs(),
1922 "Error loading file '" + SummaryFile + "': ");
1923 return false;
1924 }
1925 std::unique_ptr<ModuleSummaryIndex> Index = std::move(*IndexPtrOrErr);
1926
1927 // First step is collecting the import list.
1929 // If requested, simply import all functions in the index. This is used
1930 // when testing distributed backend handling via the opt tool, when
1931 // we have distributed indexes containing exactly the summaries to import.
1932 if (ImportAllIndex)
1934 *Index, ImportList);
1935 else
1936 ComputeCrossModuleImportForModuleForTest(M.getModuleIdentifier(),
1937 isPrevailing, *Index, ImportList);
1938
1939 // Conservatively mark all internal values as promoted. This interface is
1940 // only used when doing importing via the function importing pass. The pass
1941 // is only enabled when testing importing via the 'opt' tool, which does
1942 // not do the ThinLink that would normally determine what values to promote.
1943 for (auto &I : *Index) {
1944 for (auto &S : I.second.SummaryList) {
1945 if (GlobalValue::isLocalLinkage(S->linkage()))
1946 S->setLinkage(GlobalValue::ExternalLinkage);
1947 }
1948 }
1949
1950 // Next we need to promote to global scope and rename any local values that
1951 // are potentially exported to other modules.
1952 if (renameModuleForThinLTO(M, *Index, /*ClearDSOLocalOnDeclarations=*/false,
1953 /*GlobalsToImport=*/nullptr)) {
1954 errs() << "Error renaming module\n";
1955 return true;
1956 }
1957
1958 // Perform the import now.
1959 auto ModuleLoader = [&M](StringRef Identifier) {
1960 return loadFile(std::string(Identifier), M.getContext());
1961 };
1962 FunctionImporter Importer(*Index, ModuleLoader,
1963 /*ClearDSOLocalOnDeclarations=*/false);
1964 Expected<bool> Result = Importer.importFunctions(M, ImportList);
1965
1966 // FIXME: Probably need to propagate Errors through the pass manager.
1967 if (!Result) {
1968 logAllUnhandledErrors(Result.takeError(), errs(),
1969 "Error importing module: ");
1970 return true;
1971 }
1972
1973 return true;
1974}
1975
1978 // This is only used for testing the function import pass via opt, where we
1979 // don't have prevailing information from the LTO context available, so just
1980 // conservatively assume everything is prevailing (which is fine for the very
1981 // limited use of prevailing checking in this pass).
1982 auto isPrevailing = [](GlobalValue::GUID, const GlobalValueSummary *) {
1983 return true;
1984 };
1985 if (!doImportingForModuleForTest(M, isPrevailing))
1986 return PreservedAnalyses::all();
1987
1988 return PreservedAnalyses::none();
1989}
static cl::opt< ITMode > IT(cl::desc("IT block support"), cl::Hidden, cl::init(DefaultIT), cl::values(clEnumValN(DefaultIT, "arm-default-it", "Generate any type of IT block"), clEnumValN(RestrictedIT, "arm-restrict-it", "Disallow complex IT blocks")))
This file contains the declarations for the subclasses of Constant, which represent the different fla...
#define LLVM_DEBUG(X)
Definition: Debug.h:101
std::string Name
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 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 bool checkVariableImport(const ModuleSummaryIndex &Index, DenseMap< StringRef, FunctionImporter::ImportMapTy > &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 void computeImportForFunction(const FunctionSummary &Summary, const ModuleSummaryIndex &Index, const unsigned Threshold, const GVSummaryMapTy &DefinedGVSummaries, function_ref< bool(GlobalValue::GUID, const GlobalValueSummary *)> isPrevailing, SmallVectorImpl< EdgeInfo > &Worklist, GlobalsImporter &GVImporter, FunctionImporter::ImportMapTy &ImportList, DenseMap< StringRef, FunctionImporter::ExportSetTy > *ExportLists, FunctionImporter::ImportThresholdsTy &ImportThresholds)
Compute the list of functions to import for a given caller.
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)"))
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"))
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)
static cl::opt< bool > ForceImportAll("force-import-all", cl::init(false), cl::Hidden, cl::desc("Import functions with noinline attribute"))
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.
static unsigned numGlobalVarSummaries(const ModuleSummaryIndex &Index, T &Cont, unsigned &DefinedGVS, unsigned &DefinedFS)
This file supports working with JSON data.
#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
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...
Module.h This file contains the declarations for the Module class.
LLVMContext & Context
if(VerifyEach)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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
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.
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:321
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:202
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:155
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&... Args)
Definition: DenseMap.h:235
bool erase(const KeyT &Val)
Definition: DenseMap.h:329
unsigned size() const
Definition: DenseMap.h:99
iterator begin()
Definition: DenseMap.h:75
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:151
iterator end()
Definition: DenseMap.h:84
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: DenseMap.h:220
Implements a dense probed hash-table based set.
Definition: DenseSet.h:271
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
Tagged union holding either a T or a Error.
Definition: Error.h:474
Error takeError()
Take ownership of the stored error.
Definition: Error.h:601
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
The function importer is automatically importing function from other modules based on the provided su...
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:164
const GlobalObject * getAliaseeObject() const
Definition: Globals.cpp:568
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.
VisibilityTypes getVisibility() const
Definition: GlobalValue.h:247
bool isImplicitDSOLocal() const
Definition: GlobalValue.h:297
static bool isLocalLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:408
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition: Globals.cpp:286
LinkageTypes getLinkage() const
Definition: GlobalValue.h:545
uint64_t GUID
Declare a type to represent a global unique identifier for a global value.
Definition: GlobalValue.h:586
ThreadLocalMode getThreadLocalMode() const
Definition: GlobalValue.h:270
static bool isAvailableExternallyLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:378
static GUID getGUID(StringRef GlobalName)
Return a 64-bit global unique ID constructed from global value name (i.e.
Definition: Globals.cpp:75
void setLinkage(LinkageTypes LT)
Definition: GlobalValue.h:536
unsigned getAddressSpace() const
Definition: GlobalValue.h:204
GUID getGUID() const
Return a 64-bit global unique ID constructed from global value name (i.e.
Definition: GlobalValue.h:594
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:655
const GlobalObject * getAliaseeObject() const
Definition: Globals.cpp:380
void setDSOLocal(bool Local)
Definition: GlobalValue.h:302
PointerType * getType() const
Global values are always pointers.
Definition: GlobalValue.h:293
@ DefaultVisibility
The GV is visible.
Definition: GlobalValue.h:66
@ HiddenVisibility
The GV is hidden.
Definition: GlobalValue.h:67
void setVisibility(VisibilityTypes V)
Definition: GlobalValue.h:253
static bool isInterposableLinkage(LinkageTypes Linkage)
Whether the definition of this global may be replaced by something non-equivalent at link time.
Definition: GlobalValue.h:424
Error materialize()
Make sure this GlobalValue is fully read.
Definition: Globals.cpp:48
bool canBeOmittedFromSymbolTable() const
True if GV can be left out of the object symbol table.
Definition: Globals.cpp:405
bool hasAvailableExternallyLinkage() const
Definition: GlobalValue.h:511
std::string getGlobalIdentifier() const
Return the modified name for this global value suitable to be used as the key for a global lookup (e....
Definition: Globals.cpp:174
@ InternalLinkage
Rename collisions when linking (static functions).
Definition: GlobalValue.h:58
@ WeakODRLinkage
Same, but only replaced by something equivalent.
Definition: GlobalValue.h:56
@ ExternalLinkage
Externally visible function.
Definition: GlobalValue.h:51
@ AvailableExternallyLinkage
Available for inspection, not emission.
Definition: GlobalValue.h:52
@ LinkOnceODRLinkage
Same, but only replaced by something equivalent.
Definition: GlobalValue.h:54
Type * getValueType() const
Definition: GlobalValue.h:295
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:1777
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1541
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:65
LLVMContext & getContext() const
Get the global data context.
Definition: Module.h:301
const std::string & getSourceFileName() const
Get the module's original source file name.
Definition: Module.h:278
iterator_range< alias_iterator > aliases()
Definition: Module.h:739
iterator_range< global_iterator > globals()
Definition: Module.h:699
const std::string & getModuleIdentifier() const
Get the module identifier which is, essentially, the name of the module.
Definition: Module.h:267
iterator_range< global_object_iterator > global_objects()
Definition: Module.cpp:420
unsigned getAddressSpace() const
Return the address space of the Pointer type.
Definition: DerivedTypes.h:679
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:109
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition: Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: Analysis.h:115
Instances of this class encapsulate one diagnostic report, allowing printing to a raw_ostream as a ca...
Definition: SourceMgr.h:281
A vector that has set insertion semantics.
Definition: SetVector.h:57
ArrayRef< value_type > getArrayRef() const
Definition: SetVector.h:84
size_type size() const
Determine the number of elements in the SetVector.
Definition: SetVector.h:98
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition: SetVector.h:162
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:342
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:427
bool empty() const
Definition: SmallVector.h:94
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:586
reference emplace_back(ArgTypes &&... Args)
Definition: SmallVector.h:950
void reserve(size_type N)
Definition: SmallVector.h:676
void push_back(const T &Elt)
Definition: SmallVector.h:426
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition: StringMap.h:128
iterator end()
Definition: StringMap.h:220
iterator find(StringRef Key)
Definition: StringMap.h:233
size_type count(StringRef Key) const
count - Return 1 if the element is in the map, 0 otherwise.
Definition: StringMap.h:276
bool insert(MapEntryTy *KeyValue)
insert - Insert the specified key/value pair into the map.
Definition: StringMap.h:308
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
constexpr size_t size() const
size - Get the string size.
Definition: StringRef.h:137
char front() const
front - Get the first character in the string.
Definition: StringRef.h:140
StringSet - A wrapper for StringMap that provides set-like functionality.
Definition: StringSet.h:23
std::pair< typename Base::iterator, bool > insert(StringRef key)
Definition: StringSet.h:38
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
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:246
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:534
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:309
void takeName(Value *V)
Transfer the name from V to this value.
Definition: Value.cpp:383
std::pair< iterator, bool > insert(const ValueT &V)
Definition: DenseSet.h:206
size_type size() const
Definition: DenseSet.h:81
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:97
An efficient, type-erasing, non-owning reference to a callable.
The root is the trivial Path to the root value.
Definition: JSON.h:700
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:470
#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:450
std::optional< const char * > toString(const std::optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract a string value from it.
llvm::Expected< Value > parse(llvm::StringRef JSON)
Parses the provided JSON source, or returns a ParseError.
Definition: JSON.cpp:686
bool fromJSON(const Value &E, std::string &Out, Path P)
Definition: JSON.h:729
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner={})
Log all errors (if any) in E to OS.
Definition: Error.cpp:65
void gatherImportedSummariesForModule(StringRef ModulePath, const DenseMap< StringRef, GVSummaryMapTy > &ModuleToDefinedGVSummaries, const FunctionImporter::ImportMapTy &ImportList, std::map< std::string, GVSummaryMapTy > &ModuleToSummariesForIndex, GVSummaryPtrSet &DecSummaries)
Compute the set of summaries needed for a ThinLTO backend compilation of ModulePath.
void ComputeCrossModuleImport(const ModuleSummaryIndex &Index, const DenseMap< StringRef, GVSummaryMapTy > &ModuleToDefinedGVSummaries, function_ref< bool(GlobalValue::GUID, const GlobalValueSummary *)> isPrevailing, DenseMap< StringRef, FunctionImporter::ImportMapTy > &ImportLists, DenseMap< StringRef, FunctionImporter::ExportSetTy > &ExportLists)
Compute all the imports and exports for every module in the Index.
bool internalizeModule(Module &TheModule, std::function< bool(const GlobalValue &)> MustPreserveGV)
Helper function to internalize functions and variables in a Module.
Definition: Internalize.h:75
std::error_code make_error_code(BitcodeError E)
const char * getHotnessName(CalleeInfo::HotnessType HT)
std::error_code EmitImportsFiles(StringRef ModulePath, StringRef OutputFilename, const std::map< std::string, GVSummaryMapTy > &ModuleToSummariesForIndex)
Emit into OutputFilename the files module ModulePath will import from.
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:656
bool convertToDeclaration(GlobalValue &GV)
Converts value GV to declaration, or replaces with a declaration if it is an alias.
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition: Error.h:1258
auto map_range(ContainerTy &&C, FuncTy F)
Definition: STLExtras.h:377
bool 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...
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:1729
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...
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:156
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:572
void updateIndirectCalls(ModuleSummaryIndex &Index)
Update call edges for indirect calls to local functions added from SamplePGO when needed.
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.
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
void thinLTOInternalizeModule(Module &TheModule, const GVSummaryMapTy &DefinedGlobals)
Internalize TheModule based on the information recorded in the summaries during global summary-based ...
PrevailingType
PrevailingType enum used as a return type of callback passed to computeDeadSymbolsAndUpdateIndirectCa...
bool UpgradeDebugInfo(Module &M)
Check the debug info version number, if it is out-dated, drop the debug info.
Function * CloneFunction(Function *F, ValueToValueMapTy &VMap, ClonedCodeInfo *CodeInfo=nullptr)
Return a copy of the specified function and add it to that function's module.
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.
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,...
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.