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/// Pass a workload description file - an example of workload would be the
144/// functions executed to satisfy a RPC request. A workload is defined by a root
145/// function and the list of functions that are (frequently) needed to satisfy
146/// it. The module that defines the root will have all those functions imported.
147/// The file contains a JSON dictionary. The keys are root functions, the values
148/// are lists of functions to import in the module defining the root. It is
149/// assumed -funique-internal-linkage-names was used, thus ensuring function
150/// names are unique even for local linkage ones.
152 "thinlto-workload-def",
153 cl::desc("Pass a workload definition. This is a file containing a JSON "
154 "dictionary. The keys are root functions, the values are lists of "
155 "functions to import in the module defining the root. It is "
156 "assumed -funique-internal-linkage-names was used, to ensure "
157 "local linkage functions have unique names. For example: \n"
158 "{\n"
159 " \"rootFunction_1\": [\"function_to_import_1\", "
160 "\"function_to_import_2\"], \n"
161 " \"rootFunction_2\": [\"function_to_import_3\", "
162 "\"function_to_import_4\"] \n"
163 "}"),
164 cl::Hidden);
165
166namespace llvm {
168}
169
170// Load lazily a module from \p FileName in \p Context.
171static std::unique_ptr<Module> loadFile(const std::string &FileName,
172 LLVMContext &Context) {
173 SMDiagnostic Err;
174 LLVM_DEBUG(dbgs() << "Loading '" << FileName << "'\n");
175 // Metadata isn't loaded until functions are imported, to minimize
176 // the memory overhead.
177 std::unique_ptr<Module> Result =
178 getLazyIRFileModule(FileName, Err, Context,
179 /* ShouldLazyLoadMetadata = */ true);
180 if (!Result) {
181 Err.print("function-import", errs());
182 report_fatal_error("Abort");
183 }
184
185 return Result;
186}
187
188/// Given a list of possible callee implementation for a call site, qualify the
189/// legality of importing each. The return is a range of pairs. Each pair
190/// corresponds to a candidate. The first value is the ImportFailureReason for
191/// that candidate, the second is the candidate.
194 ArrayRef<std::unique_ptr<GlobalValueSummary>> CalleeSummaryList,
195 StringRef CallerModulePath) {
196 return llvm::map_range(
197 CalleeSummaryList,
198 [&Index, CalleeSummaryList,
199 CallerModulePath](const std::unique_ptr<GlobalValueSummary> &SummaryPtr)
201 const GlobalValueSummary *> {
202 auto *GVSummary = SummaryPtr.get();
203 if (!Index.isGlobalValueLive(GVSummary))
204 return {FunctionImporter::ImportFailureReason::NotLive, GVSummary};
205
206 if (GlobalValue::isInterposableLinkage(GVSummary->linkage()))
207 return {FunctionImporter::ImportFailureReason::InterposableLinkage,
208 GVSummary};
209
210 auto *Summary = dyn_cast<FunctionSummary>(GVSummary->getBaseObject());
211
212 // Ignore any callees that aren't actually functions. This could happen
213 // in the case of GUID hash collisions. It could also happen in theory
214 // for SamplePGO profiles collected on old versions of the code after
215 // renaming, since we synthesize edges to any inlined callees appearing
216 // in the profile.
217 if (!Summary)
218 return {FunctionImporter::ImportFailureReason::GlobalVar, GVSummary};
219
220 // If this is a local function, make sure we import the copy
221 // in the caller's module. The only time a local function can
222 // share an entry in the index is if there is a local with the same name
223 // in another module that had the same source file name (in a different
224 // directory), where each was compiled in their own directory so there
225 // was not distinguishing path.
226 // However, do the import from another module if there is only one
227 // entry in the list - in that case this must be a reference due
228 // to indirect call profile data, since a function pointer can point to
229 // a local in another module.
230 if (GlobalValue::isLocalLinkage(Summary->linkage()) &&
231 CalleeSummaryList.size() > 1 &&
232 Summary->modulePath() != CallerModulePath)
233 return {
234 FunctionImporter::ImportFailureReason::LocalLinkageNotInModule,
235 GVSummary};
236
237 // Skip if it isn't legal to import (e.g. may reference unpromotable
238 // locals).
239 if (Summary->notEligibleToImport())
240 return {FunctionImporter::ImportFailureReason::NotEligible,
241 GVSummary};
242
243 return {FunctionImporter::ImportFailureReason::None, GVSummary};
244 });
245}
246
247/// Given a list of possible callee implementation for a call site, select one
248/// that fits the \p Threshold. If none are found, the Reason will give the last
249/// reason for the failure (last, in the order of CalleeSummaryList entries).
250///
251/// FIXME: select "best" instead of first that fits. But what is "best"?
252/// - The smallest: more likely to be inlined.
253/// - The one with the least outgoing edges (already well optimized).
254/// - One from a module already being imported from in order to reduce the
255/// number of source modules parsed/linked.
256/// - One that has PGO data attached.
257/// - [insert you fancy metric here]
258static const GlobalValueSummary *
260 ArrayRef<std::unique_ptr<GlobalValueSummary>> CalleeSummaryList,
261 unsigned Threshold, StringRef CallerModulePath,
263 auto QualifiedCandidates =
264 qualifyCalleeCandidates(Index, CalleeSummaryList, CallerModulePath);
265 for (auto QualifiedValue : QualifiedCandidates) {
266 Reason = QualifiedValue.first;
267 if (Reason != FunctionImporter::ImportFailureReason::None)
268 continue;
269 auto *Summary =
270 cast<FunctionSummary>(QualifiedValue.second->getBaseObject());
271
272 if ((Summary->instCount() > Threshold) && !Summary->fflags().AlwaysInline &&
274 Reason = FunctionImporter::ImportFailureReason::TooLarge;
275 continue;
276 }
277
278 // Don't bother importing if we can't inline it anyway.
279 if (Summary->fflags().NoInline && !ForceImportAll) {
280 Reason = FunctionImporter::ImportFailureReason::NoInline;
281 continue;
282 }
283
284 return Summary;
285 }
286 return nullptr;
287}
288
289namespace {
290
291using EdgeInfo = std::tuple<const FunctionSummary *, unsigned /* Threshold */>;
292
293} // anonymous namespace
294
295/// Import globals referenced by a function or other globals that are being
296/// imported, if importing such global is possible.
297class GlobalsImporter final {
299 const GVSummaryMapTy &DefinedGVSummaries;
301 IsPrevailing;
304
305 bool shouldImportGlobal(const ValueInfo &VI) {
306 const auto &GVS = DefinedGVSummaries.find(VI.getGUID());
307 if (GVS == DefinedGVSummaries.end())
308 return true;
309 // We should not skip import if the module contains a non-prevailing
310 // definition with interposable linkage type. This is required for
311 // correctness in the situation where there is a prevailing def available
312 // for import and marked read-only. In this case, the non-prevailing def
313 // will be converted to a declaration, while the prevailing one becomes
314 // internal, thus no definitions will be available for linking. In order to
315 // prevent undefined symbol link error, the prevailing definition must be
316 // imported.
317 // FIXME: Consider adding a check that the suitable prevailing definition
318 // exists and marked read-only.
319 if (VI.getSummaryList().size() > 1 &&
320 GlobalValue::isInterposableLinkage(GVS->second->linkage()) &&
321 !IsPrevailing(VI.getGUID(), GVS->second))
322 return true;
323
324 return false;
325 }
326
327 void
328 onImportingSummaryImpl(const GlobalValueSummary &Summary,
330 for (const auto &VI : Summary.refs()) {
331 if (!shouldImportGlobal(VI)) {
333 dbgs() << "Ref ignored! Target already in destination module.\n");
334 continue;
335 }
336
337 LLVM_DEBUG(dbgs() << " ref -> " << VI << "\n");
338
339 // If this is a local variable, make sure we import the copy
340 // in the caller's module. The only time a local variable can
341 // share an entry in the index is if there is a local with the same name
342 // in another module that had the same source file name (in a different
343 // directory), where each was compiled in their own directory so there
344 // was not distinguishing path.
345 auto LocalNotInModule =
346 [&](const GlobalValueSummary *RefSummary) -> bool {
347 return GlobalValue::isLocalLinkage(RefSummary->linkage()) &&
348 RefSummary->modulePath() != Summary.modulePath();
349 };
350
351 for (const auto &RefSummary : VI.getSummaryList()) {
352 const auto *GVS = dyn_cast<GlobalVarSummary>(RefSummary.get());
353 // Functions could be referenced by global vars - e.g. a vtable; but we
354 // don't currently imagine a reason those would be imported here, rather
355 // than as part of the logic deciding which functions to import (i.e.
356 // based on profile information). Should we decide to handle them here,
357 // we can refactor accordingly at that time.
358 if (!GVS || !Index.canImportGlobalVar(GVS, /* AnalyzeRefs */ true) ||
359 LocalNotInModule(GVS))
360 continue;
361 auto ILI = ImportList[RefSummary->modulePath()].insert(VI.getGUID());
362 // Only update stat and exports if we haven't already imported this
363 // variable.
364 if (!ILI.second)
365 break;
366 NumImportedGlobalVarsThinLink++;
367 // Any references made by this variable will be marked exported
368 // later, in ComputeCrossModuleImport, after import decisions are
369 // complete, which is more efficient than adding them here.
370 if (ExportLists)
371 (*ExportLists)[RefSummary->modulePath()].insert(VI);
372
373 // If variable is not writeonly we attempt to recursively analyze
374 // its references in order to import referenced constants.
375 if (!Index.isWriteOnly(GVS))
376 Worklist.emplace_back(GVS);
377 break;
378 }
379 }
380 }
381
382public:
384 const ModuleSummaryIndex &Index, const GVSummaryMapTy &DefinedGVSummaries,
386 IsPrevailing,
389 : Index(Index), DefinedGVSummaries(DefinedGVSummaries),
390 IsPrevailing(IsPrevailing), ImportList(ImportList),
391 ExportLists(ExportLists) {}
392
395 onImportingSummaryImpl(Summary, Worklist);
396 while (!Worklist.empty())
397 onImportingSummaryImpl(*Worklist.pop_back_val(), Worklist);
398 }
399};
400
402
403/// Determine the list of imports and exports for each module.
405protected:
410
417
418public:
419 virtual ~ModuleImportsManager() = default;
420
421 /// Given the list of globals defined in a module, compute the list of imports
422 /// as well as the list of "exports", i.e. the list of symbols referenced from
423 /// another module (that may require promotion).
424 virtual void
425 computeImportForModule(const GVSummaryMapTy &DefinedGVSummaries,
426 StringRef ModName,
428
429 static std::unique_ptr<ModuleImportsManager>
434 nullptr);
435};
436
437/// A ModuleImportsManager that operates based on a workload definition (see
438/// -thinlto-workload-def). For modules that do not define workload roots, it
439/// applies the base ModuleImportsManager import policy.
441 // Keep a module name -> value infos to import association. We use it to
442 // determine if a module's import list should be done by the base
443 // ModuleImportsManager or by us.
445
446 void
447 computeImportForModule(const GVSummaryMapTy &DefinedGVSummaries,
448 StringRef ModName,
449 FunctionImporter::ImportMapTy &ImportList) override {
450 auto SetIter = Workloads.find(ModName);
451 if (SetIter == Workloads.end()) {
452 LLVM_DEBUG(dbgs() << "[Workload] " << ModName
453 << " does not contain the root of any context.\n");
454 return ModuleImportsManager::computeImportForModule(DefinedGVSummaries,
455 ModName, ImportList);
456 }
457 LLVM_DEBUG(dbgs() << "[Workload] " << ModName
458 << " contains the root(s) of context(s).\n");
459
460 GlobalsImporter GVI(Index, DefinedGVSummaries, IsPrevailing, ImportList,
462 auto &ValueInfos = SetIter->second;
463 SmallVector<EdgeInfo, 128> GlobWorklist;
464 for (auto &VI : llvm::make_early_inc_range(ValueInfos)) {
465 auto It = DefinedGVSummaries.find(VI.getGUID());
466 if (It != DefinedGVSummaries.end() &&
467 IsPrevailing(VI.getGUID(), It->second)) {
469 dbgs() << "[Workload] " << VI.name()
470 << " has the prevailing variant already in the module "
471 << ModName << ". No need to import\n");
472 continue;
473 }
474 auto Candidates =
475 qualifyCalleeCandidates(Index, VI.getSummaryList(), ModName);
476
477 const GlobalValueSummary *GVS = nullptr;
478 auto PotentialCandidates = llvm::map_range(
480 Candidates,
481 [&](const auto &Candidate) {
482 LLVM_DEBUG(dbgs() << "[Workflow] Candidate for " << VI.name()
483 << " from " << Candidate.second->modulePath()
484 << " ImportFailureReason: "
485 << getFailureName(Candidate.first) << "\n");
486 return Candidate.first ==
487 FunctionImporter::ImportFailureReason::None;
488 }),
489 [](const auto &Candidate) { return Candidate.second; });
490 if (PotentialCandidates.empty()) {
491 LLVM_DEBUG(dbgs() << "[Workload] Not importing " << VI.name()
492 << " because can't find eligible Callee. Guid is: "
493 << Function::getGUID(VI.name()) << "\n");
494 continue;
495 }
496 /// We will prefer importing the prevailing candidate, if not, we'll
497 /// still pick the first available candidate. The reason we want to make
498 /// sure we do import the prevailing candidate is because the goal of
499 /// workload-awareness is to enable optimizations specializing the call
500 /// graph of that workload. Suppose a function is already defined in the
501 /// module, but it's not the prevailing variant. Suppose also we do not
502 /// inline it (in fact, if it were interposable, we can't inline it),
503 /// but we could specialize it to the workload in other ways. However,
504 /// the linker would drop it in the favor of the prevailing copy.
505 /// Instead, by importing the prevailing variant (assuming also the use
506 /// of `-avail-extern-to-local`), we keep the specialization. We could
507 /// alteranatively make the non-prevailing variant local, but the
508 /// prevailing one is also the one for which we would have previously
509 /// collected profiles, making it preferrable.
510 auto PrevailingCandidates = llvm::make_filter_range(
511 PotentialCandidates, [&](const auto *Candidate) {
512 return IsPrevailing(VI.getGUID(), Candidate);
513 });
514 if (PrevailingCandidates.empty()) {
515 GVS = *PotentialCandidates.begin();
516 if (!llvm::hasSingleElement(PotentialCandidates) &&
519 dbgs()
520 << "[Workload] Found multiple non-prevailing candidates for "
521 << VI.name()
522 << ". This is unexpected. Are module paths passed to the "
523 "compiler unique for the modules passed to the linker?");
524 // We could in theory have multiple (interposable) copies of a symbol
525 // when there is no prevailing candidate, if say the prevailing copy was
526 // in a native object being linked in. However, we should in theory be
527 // marking all of these non-prevailing IR copies dead in that case, in
528 // which case they won't be candidates.
529 assert(GVS->isLive());
530 } else {
531 assert(llvm::hasSingleElement(PrevailingCandidates));
532 GVS = *PrevailingCandidates.begin();
533 }
534
535 auto ExportingModule = GVS->modulePath();
536 // We checked that for the prevailing case, but if we happen to have for
537 // example an internal that's defined in this module, it'd have no
538 // PrevailingCandidates.
539 if (ExportingModule == ModName) {
540 LLVM_DEBUG(dbgs() << "[Workload] Not importing " << VI.name()
541 << " because its defining module is the same as the "
542 "current module\n");
543 continue;
544 }
545 LLVM_DEBUG(dbgs() << "[Workload][Including]" << VI.name() << " from "
546 << ExportingModule << " : "
547 << Function::getGUID(VI.name()) << "\n");
548 ImportList[ExportingModule].insert(VI.getGUID());
549 GVI.onImportingSummary(*GVS);
550 if (ExportLists)
551 (*ExportLists)[ExportingModule].insert(VI);
552 }
553 LLVM_DEBUG(dbgs() << "[Workload] Done\n");
554 }
555
556public:
563 // Since the workload def uses names, we need a quick lookup
564 // name->ValueInfo.
565 StringMap<ValueInfo> NameToValueInfo;
566 StringSet<> AmbiguousNames;
567 for (auto &I : Index) {
568 ValueInfo VI = Index.getValueInfo(I);
569 if (!NameToValueInfo.insert(std::make_pair(VI.name(), VI)).second)
570 LLVM_DEBUG(AmbiguousNames.insert(VI.name()));
571 }
572 auto DbgReportIfAmbiguous = [&](StringRef Name) {
573 LLVM_DEBUG(if (AmbiguousNames.count(Name) > 0) {
574 dbgs() << "[Workload] Function name " << Name
575 << " present in the workload definition is ambiguous. Consider "
576 "compiling with -funique-internal-linkage-names.";
577 });
578 };
579 std::error_code EC;
581 if (std::error_code EC = BufferOrErr.getError()) {
582 report_fatal_error("Failed to open context file");
583 return;
584 }
585 auto Buffer = std::move(BufferOrErr.get());
586 std::map<std::string, std::vector<std::string>> WorkloadDefs;
587 json::Path::Root NullRoot;
588 // The JSON is supposed to contain a dictionary matching the type of
589 // WorkloadDefs. For example:
590 // {
591 // "rootFunction_1": ["function_to_import_1", "function_to_import_2"],
592 // "rootFunction_2": ["function_to_import_3", "function_to_import_4"]
593 // }
594 auto Parsed = json::parse(Buffer->getBuffer());
595 if (!Parsed)
596 report_fatal_error(Parsed.takeError());
597 if (!json::fromJSON(*Parsed, WorkloadDefs, NullRoot))
598 report_fatal_error("Invalid thinlto contextual profile format.");
599 for (const auto &Workload : WorkloadDefs) {
600 const auto &Root = Workload.first;
601 DbgReportIfAmbiguous(Root);
602 LLVM_DEBUG(dbgs() << "[Workload] Root: " << Root << "\n");
603 const auto &AllCallees = Workload.second;
604 auto RootIt = NameToValueInfo.find(Root);
605 if (RootIt == NameToValueInfo.end()) {
606 LLVM_DEBUG(dbgs() << "[Workload] Root " << Root
607 << " not found in this linkage unit.\n");
608 continue;
609 }
610 auto RootVI = RootIt->second;
611 if (RootVI.getSummaryList().size() != 1) {
612 LLVM_DEBUG(dbgs() << "[Workload] Root " << Root
613 << " should have exactly one summary, but has "
614 << RootVI.getSummaryList().size() << ". Skipping.\n");
615 continue;
616 }
617 StringRef RootDefiningModule =
618 RootVI.getSummaryList().front()->modulePath();
619 LLVM_DEBUG(dbgs() << "[Workload] Root defining module for " << Root
620 << " is : " << RootDefiningModule << "\n");
621 auto &Set = Workloads[RootDefiningModule];
622 for (const auto &Callee : AllCallees) {
623 LLVM_DEBUG(dbgs() << "[Workload] " << Callee << "\n");
624 DbgReportIfAmbiguous(Callee);
625 auto ElemIt = NameToValueInfo.find(Callee);
626 if (ElemIt == NameToValueInfo.end()) {
627 LLVM_DEBUG(dbgs() << "[Workload] " << Callee << " not found\n");
628 continue;
629 }
630 Set.insert(ElemIt->second);
631 }
632 LLVM_DEBUG({
633 dbgs() << "[Workload] Root: " << Root << " we have " << Set.size()
634 << " distinct callees.\n";
635 for (const auto &VI : Set) {
636 dbgs() << "[Workload] Root: " << Root
637 << " Would include: " << VI.getGUID() << "\n";
638 }
639 });
640 }
641 }
642};
643
644std::unique_ptr<ModuleImportsManager> ModuleImportsManager::create(
646 IsPrevailing,
649 if (WorkloadDefinitions.empty()) {
650 LLVM_DEBUG(dbgs() << "[Workload] Using the regular imports manager.\n");
651 return std::unique_ptr<ModuleImportsManager>(
653 }
654 LLVM_DEBUG(dbgs() << "[Workload] Using the contextual imports manager.\n");
655 return std::make_unique<WorkloadImportsManager>(IsPrevailing, Index,
657}
658
659static const char *
661 switch (Reason) {
662 case FunctionImporter::ImportFailureReason::None:
663 return "None";
664 case FunctionImporter::ImportFailureReason::GlobalVar:
665 return "GlobalVar";
666 case FunctionImporter::ImportFailureReason::NotLive:
667 return "NotLive";
668 case FunctionImporter::ImportFailureReason::TooLarge:
669 return "TooLarge";
670 case FunctionImporter::ImportFailureReason::InterposableLinkage:
671 return "InterposableLinkage";
672 case FunctionImporter::ImportFailureReason::LocalLinkageNotInModule:
673 return "LocalLinkageNotInModule";
674 case FunctionImporter::ImportFailureReason::NotEligible:
675 return "NotEligible";
676 case FunctionImporter::ImportFailureReason::NoInline:
677 return "NoInline";
678 }
679 llvm_unreachable("invalid reason");
680}
681
682/// Compute the list of functions to import for a given caller. Mark these
683/// imported functions and the symbols they reference in their source module as
684/// exported from their source module.
686 const FunctionSummary &Summary, const ModuleSummaryIndex &Index,
687 const unsigned Threshold, const GVSummaryMapTy &DefinedGVSummaries,
689 isPrevailing,
690 SmallVectorImpl<EdgeInfo> &Worklist, GlobalsImporter &GVImporter,
693 FunctionImporter::ImportThresholdsTy &ImportThresholds) {
694 GVImporter.onImportingSummary(Summary);
695 static int ImportCount = 0;
696 for (const auto &Edge : Summary.calls()) {
697 ValueInfo VI = Edge.first;
698 LLVM_DEBUG(dbgs() << " edge -> " << VI << " Threshold:" << Threshold
699 << "\n");
700
701 if (ImportCutoff >= 0 && ImportCount >= ImportCutoff) {
702 LLVM_DEBUG(dbgs() << "ignored! import-cutoff value of " << ImportCutoff
703 << " reached.\n");
704 continue;
705 }
706
707 if (DefinedGVSummaries.count(VI.getGUID())) {
708 // FIXME: Consider not skipping import if the module contains
709 // a non-prevailing def with interposable linkage. The prevailing copy
710 // can safely be imported (see shouldImportGlobal()).
711 LLVM_DEBUG(dbgs() << "ignored! Target already in destination module.\n");
712 continue;
713 }
714
715 auto GetBonusMultiplier = [](CalleeInfo::HotnessType Hotness) -> float {
716 if (Hotness == CalleeInfo::HotnessType::Hot)
717 return ImportHotMultiplier;
718 if (Hotness == CalleeInfo::HotnessType::Cold)
720 if (Hotness == CalleeInfo::HotnessType::Critical)
722 return 1.0;
723 };
724
725 const auto NewThreshold =
726 Threshold * GetBonusMultiplier(Edge.second.getHotness());
727
728 auto IT = ImportThresholds.insert(std::make_pair(
729 VI.getGUID(), std::make_tuple(NewThreshold, nullptr, nullptr)));
730 bool PreviouslyVisited = !IT.second;
731 auto &ProcessedThreshold = std::get<0>(IT.first->second);
732 auto &CalleeSummary = std::get<1>(IT.first->second);
733 auto &FailureInfo = std::get<2>(IT.first->second);
734
735 bool IsHotCallsite =
736 Edge.second.getHotness() == CalleeInfo::HotnessType::Hot;
737 bool IsCriticalCallsite =
738 Edge.second.getHotness() == CalleeInfo::HotnessType::Critical;
739
740 const FunctionSummary *ResolvedCalleeSummary = nullptr;
741 if (CalleeSummary) {
742 assert(PreviouslyVisited);
743 // Since the traversal of the call graph is DFS, we can revisit a function
744 // a second time with a higher threshold. In this case, it is added back
745 // to the worklist with the new threshold (so that its own callee chains
746 // can be considered with the higher threshold).
747 if (NewThreshold <= ProcessedThreshold) {
749 dbgs() << "ignored! Target was already imported with Threshold "
750 << ProcessedThreshold << "\n");
751 continue;
752 }
753 // Update with new larger threshold.
754 ProcessedThreshold = NewThreshold;
755 ResolvedCalleeSummary = cast<FunctionSummary>(CalleeSummary);
756 } else {
757 // If we already rejected importing a callee at the same or higher
758 // threshold, don't waste time calling selectCallee.
759 if (PreviouslyVisited && NewThreshold <= ProcessedThreshold) {
761 dbgs() << "ignored! Target was already rejected with Threshold "
762 << ProcessedThreshold << "\n");
764 assert(FailureInfo &&
765 "Expected FailureInfo for previously rejected candidate");
766 FailureInfo->Attempts++;
767 }
768 continue;
769 }
770
772 CalleeSummary = selectCallee(Index, VI.getSummaryList(), NewThreshold,
773 Summary.modulePath(), Reason);
774 if (!CalleeSummary) {
775 // Update with new larger threshold if this was a retry (otherwise
776 // we would have already inserted with NewThreshold above). Also
777 // update failure info if requested.
778 if (PreviouslyVisited) {
779 ProcessedThreshold = NewThreshold;
781 assert(FailureInfo &&
782 "Expected FailureInfo for previously rejected candidate");
783 FailureInfo->Reason = Reason;
784 FailureInfo->Attempts++;
785 FailureInfo->MaxHotness =
786 std::max(FailureInfo->MaxHotness, Edge.second.getHotness());
787 }
788 } else if (PrintImportFailures) {
789 assert(!FailureInfo &&
790 "Expected no FailureInfo for newly rejected candidate");
791 FailureInfo = std::make_unique<FunctionImporter::ImportFailureInfo>(
792 VI, Edge.second.getHotness(), Reason, 1);
793 }
794 if (ForceImportAll) {
795 std::string Msg = std::string("Failed to import function ") +
796 VI.name().str() + " due to " +
797 getFailureName(Reason);
798 auto Error = make_error<StringError>(
799 Msg, make_error_code(errc::not_supported));
800 logAllUnhandledErrors(std::move(Error), errs(),
801 "Error importing module: ");
802 break;
803 } else {
805 << "ignored! No qualifying callee with summary found.\n");
806 continue;
807 }
808 }
809
810 // "Resolve" the summary
811 CalleeSummary = CalleeSummary->getBaseObject();
812 ResolvedCalleeSummary = cast<FunctionSummary>(CalleeSummary);
813
814 assert((ResolvedCalleeSummary->fflags().AlwaysInline || ForceImportAll ||
815 (ResolvedCalleeSummary->instCount() <= NewThreshold)) &&
816 "selectCallee() didn't honor the threshold");
817
818 auto ExportModulePath = ResolvedCalleeSummary->modulePath();
819 auto ILI = ImportList[ExportModulePath].insert(VI.getGUID());
820 // We previously decided to import this GUID definition if it was already
821 // inserted in the set of imports from the exporting module.
822 bool PreviouslyImported = !ILI.second;
823 if (!PreviouslyImported) {
824 NumImportedFunctionsThinLink++;
825 if (IsHotCallsite)
826 NumImportedHotFunctionsThinLink++;
827 if (IsCriticalCallsite)
828 NumImportedCriticalFunctionsThinLink++;
829 }
830
831 // Any calls/references made by this function will be marked exported
832 // later, in ComputeCrossModuleImport, after import decisions are
833 // complete, which is more efficient than adding them here.
834 if (ExportLists)
835 (*ExportLists)[ExportModulePath].insert(VI);
836 }
837
838 auto GetAdjustedThreshold = [](unsigned Threshold, bool IsHotCallsite) {
839 // Adjust the threshold for next level of imported functions.
840 // The threshold is different for hot callsites because we can then
841 // inline chains of hot calls.
842 if (IsHotCallsite)
843 return Threshold * ImportHotInstrFactor;
844 return Threshold * ImportInstrFactor;
845 };
846
847 const auto AdjThreshold = GetAdjustedThreshold(Threshold, IsHotCallsite);
848
849 ImportCount++;
850
851 // Insert the newly imported function to the worklist.
852 Worklist.emplace_back(ResolvedCalleeSummary, AdjThreshold);
853 }
854}
855
857 const GVSummaryMapTy &DefinedGVSummaries, StringRef ModName,
858 FunctionImporter::ImportMapTy &ImportList) {
859 // Worklist contains the list of function imported in this module, for which
860 // we will analyse the callees and may import further down the callgraph.
862 GlobalsImporter GVI(Index, DefinedGVSummaries, IsPrevailing, ImportList,
865
866 // Populate the worklist with the import for the functions in the current
867 // module
868 for (const auto &GVSummary : DefinedGVSummaries) {
869#ifndef NDEBUG
870 // FIXME: Change the GVSummaryMapTy to hold ValueInfo instead of GUID
871 // so this map look up (and possibly others) can be avoided.
872 auto VI = Index.getValueInfo(GVSummary.first);
873#endif
874 if (!Index.isGlobalValueLive(GVSummary.second)) {
875 LLVM_DEBUG(dbgs() << "Ignores Dead GUID: " << VI << "\n");
876 continue;
877 }
878 auto *FuncSummary =
879 dyn_cast<FunctionSummary>(GVSummary.second->getBaseObject());
880 if (!FuncSummary)
881 // Skip import for global variables
882 continue;
883 LLVM_DEBUG(dbgs() << "Initialize import for " << VI << "\n");
885 DefinedGVSummaries, IsPrevailing, Worklist, GVI,
886 ImportList, ExportLists, ImportThresholds);
887 }
888
889 // Process the newly imported functions and add callees to the worklist.
890 while (!Worklist.empty()) {
891 auto GVInfo = Worklist.pop_back_val();
892 auto *Summary = std::get<0>(GVInfo);
893 auto Threshold = std::get<1>(GVInfo);
894
895 if (auto *FS = dyn_cast<FunctionSummary>(Summary))
896 computeImportForFunction(*FS, Index, Threshold, DefinedGVSummaries,
897 IsPrevailing, Worklist, GVI, ImportList,
898 ExportLists, ImportThresholds);
899 }
900
901 // Print stats about functions considered but rejected for importing
902 // when requested.
904 dbgs() << "Missed imports into module " << ModName << "\n";
905 for (auto &I : ImportThresholds) {
906 auto &ProcessedThreshold = std::get<0>(I.second);
907 auto &CalleeSummary = std::get<1>(I.second);
908 auto &FailureInfo = std::get<2>(I.second);
909 if (CalleeSummary)
910 continue; // We are going to import.
911 assert(FailureInfo);
912 FunctionSummary *FS = nullptr;
913 if (!FailureInfo->VI.getSummaryList().empty())
914 FS = dyn_cast<FunctionSummary>(
915 FailureInfo->VI.getSummaryList()[0]->getBaseObject());
916 dbgs() << FailureInfo->VI
917 << ": Reason = " << getFailureName(FailureInfo->Reason)
918 << ", Threshold = " << ProcessedThreshold
919 << ", Size = " << (FS ? (int)FS->instCount() : -1)
920 << ", MaxHotness = " << getHotnessName(FailureInfo->MaxHotness)
921 << ", Attempts = " << FailureInfo->Attempts << "\n";
922 }
923 }
924}
925
926#ifndef NDEBUG
928 auto SL = VI.getSummaryList();
929 return SL.empty()
930 ? false
931 : SL[0]->getSummaryKind() == GlobalValueSummary::GlobalVarKind;
932}
933
936 if (const auto &VI = Index.getValueInfo(G))
937 return isGlobalVarSummary(Index, VI);
938 return false;
939}
940
941template <class T>
943 T &Cont) {
944 unsigned NumGVS = 0;
945 for (auto &V : Cont)
947 ++NumGVS;
948 return NumGVS;
949}
950#endif
951
952#ifndef NDEBUG
957
958 DenseSet<GlobalValue::GUID> FlattenedImports;
959
960 for (auto &ImportPerModule : ImportLists)
961 for (auto &ExportPerModule : ImportPerModule.second)
962 FlattenedImports.insert(ExportPerModule.second.begin(),
963 ExportPerModule.second.end());
964
965 // Checks that all GUIDs of read/writeonly vars we see in export lists
966 // are also in the import lists. Otherwise we my face linker undefs,
967 // because readonly and writeonly vars are internalized in their
968 // source modules. The exception would be if it has a linkage type indicating
969 // that there may have been a copy existing in the importing module (e.g.
970 // linkonce_odr). In that case we cannot accurately do this checking.
971 auto IsReadOrWriteOnlyVarNeedingImporting = [&](StringRef ModulePath,
972 const ValueInfo &VI) {
973 auto *GVS = dyn_cast_or_null<GlobalVarSummary>(
974 Index.findSummaryInModule(VI, ModulePath));
975 return GVS && (Index.isReadOnly(GVS) || Index.isWriteOnly(GVS)) &&
976 !(GVS->linkage() == GlobalValue::AvailableExternallyLinkage ||
977 GVS->linkage() == GlobalValue::WeakODRLinkage ||
978 GVS->linkage() == GlobalValue::LinkOnceODRLinkage);
979 };
980
981 for (auto &ExportPerModule : ExportLists)
982 for (auto &VI : ExportPerModule.second)
983 if (!FlattenedImports.count(VI.getGUID()) &&
984 IsReadOrWriteOnlyVarNeedingImporting(ExportPerModule.first, VI))
985 return false;
986
987 return true;
988}
989#endif
990
991/// Compute all the import and export for every module using the Index.
994 const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
996 isPrevailing,
999 auto MIS = ModuleImportsManager::create(isPrevailing, Index, &ExportLists);
1000 // For each module that has function defined, compute the import/export lists.
1001 for (const auto &DefinedGVSummaries : ModuleToDefinedGVSummaries) {
1002 auto &ImportList = ImportLists[DefinedGVSummaries.first];
1003 LLVM_DEBUG(dbgs() << "Computing import for Module '"
1004 << DefinedGVSummaries.first << "'\n");
1005 MIS->computeImportForModule(DefinedGVSummaries.second,
1006 DefinedGVSummaries.first, ImportList);
1007 }
1008
1009 // When computing imports we only added the variables and functions being
1010 // imported to the export list. We also need to mark any references and calls
1011 // they make as exported as well. We do this here, as it is more efficient
1012 // since we may import the same values multiple times into different modules
1013 // during the import computation.
1014 for (auto &ELI : ExportLists) {
1016 const auto &DefinedGVSummaries =
1017 ModuleToDefinedGVSummaries.lookup(ELI.first);
1018 for (auto &EI : ELI.second) {
1019 // Find the copy defined in the exporting module so that we can mark the
1020 // values it references in that specific definition as exported.
1021 // Below we will add all references and called values, without regard to
1022 // whether they are also defined in this module. We subsequently prune the
1023 // list to only include those defined in the exporting module, see comment
1024 // there as to why.
1025 auto DS = DefinedGVSummaries.find(EI.getGUID());
1026 // Anything marked exported during the import computation must have been
1027 // defined in the exporting module.
1028 assert(DS != DefinedGVSummaries.end());
1029 auto *S = DS->getSecond();
1030 S = S->getBaseObject();
1031 if (auto *GVS = dyn_cast<GlobalVarSummary>(S)) {
1032 // Export referenced functions and variables. We don't export/promote
1033 // objects referenced by writeonly variable initializer, because
1034 // we convert such variables initializers to "zeroinitializer".
1035 // See processGlobalForThinLTO.
1036 if (!Index.isWriteOnly(GVS))
1037 for (const auto &VI : GVS->refs())
1038 NewExports.insert(VI);
1039 } else {
1040 auto *FS = cast<FunctionSummary>(S);
1041 for (const auto &Edge : FS->calls())
1042 NewExports.insert(Edge.first);
1043 for (const auto &Ref : FS->refs())
1044 NewExports.insert(Ref);
1045 }
1046 }
1047 // Prune list computed above to only include values defined in the exporting
1048 // module. We do this after the above insertion since we may hit the same
1049 // ref/call target multiple times in above loop, and it is more efficient to
1050 // avoid a set lookup each time.
1051 for (auto EI = NewExports.begin(); EI != NewExports.end();) {
1052 if (!DefinedGVSummaries.count(EI->getGUID()))
1053 NewExports.erase(EI++);
1054 else
1055 ++EI;
1056 }
1057 ELI.second.insert(NewExports.begin(), NewExports.end());
1058 }
1059
1060 assert(checkVariableImport(Index, ImportLists, ExportLists));
1061#ifndef NDEBUG
1062 LLVM_DEBUG(dbgs() << "Import/Export lists for " << ImportLists.size()
1063 << " modules:\n");
1064 for (auto &ModuleImports : ImportLists) {
1065 auto ModName = ModuleImports.first;
1066 auto &Exports = ExportLists[ModName];
1067 unsigned NumGVS = numGlobalVarSummaries(Index, Exports);
1068 LLVM_DEBUG(dbgs() << "* Module " << ModName << " exports "
1069 << Exports.size() - NumGVS << " functions and " << NumGVS
1070 << " vars. Imports from " << ModuleImports.second.size()
1071 << " modules.\n");
1072 for (auto &Src : ModuleImports.second) {
1073 auto SrcModName = Src.first;
1074 unsigned NumGVSPerMod = numGlobalVarSummaries(Index, Src.second);
1075 LLVM_DEBUG(dbgs() << " - " << Src.second.size() - NumGVSPerMod
1076 << " functions imported from " << SrcModName << "\n");
1077 LLVM_DEBUG(dbgs() << " - " << NumGVSPerMod
1078 << " global vars imported from " << SrcModName << "\n");
1079 }
1080 }
1081#endif
1082}
1083
1084#ifndef NDEBUG
1086 StringRef ModulePath,
1087 FunctionImporter::ImportMapTy &ImportList) {
1088 LLVM_DEBUG(dbgs() << "* Module " << ModulePath << " imports from "
1089 << ImportList.size() << " modules.\n");
1090 for (auto &Src : ImportList) {
1091 auto SrcModName = Src.first;
1092 unsigned NumGVSPerMod = numGlobalVarSummaries(Index, Src.second);
1093 LLVM_DEBUG(dbgs() << " - " << Src.second.size() - NumGVSPerMod
1094 << " functions imported from " << SrcModName << "\n");
1095 LLVM_DEBUG(dbgs() << " - " << NumGVSPerMod << " vars imported from "
1096 << SrcModName << "\n");
1097 }
1098}
1099#endif
1100
1101/// Compute all the imports for the given module using the Index.
1102///
1103/// \p isPrevailing is a callback that will be called with a global value's GUID
1104/// and summary and should return whether the module corresponding to the
1105/// summary contains the linker-prevailing copy of that value.
1106///
1107/// \p ImportList will be populated with a map that can be passed to
1108/// FunctionImporter::importFunctions() above (see description there).
1110 StringRef ModulePath,
1112 isPrevailing,
1114 FunctionImporter::ImportMapTy &ImportList) {
1115 // Collect the list of functions this module defines.
1116 // GUID -> Summary
1117 GVSummaryMapTy FunctionSummaryMap;
1118 Index.collectDefinedFunctionsForModule(ModulePath, FunctionSummaryMap);
1119
1120 // Compute the import list for this module.
1121 LLVM_DEBUG(dbgs() << "Computing import for Module '" << ModulePath << "'\n");
1122 auto MIS = ModuleImportsManager::create(isPrevailing, Index);
1123 MIS->computeImportForModule(FunctionSummaryMap, ModulePath, ImportList);
1124
1125#ifndef NDEBUG
1126 dumpImportListForModule(Index, ModulePath, ImportList);
1127#endif
1128}
1129
1130/// Mark all external summaries in \p Index for import into the given module.
1131/// Used for testing the case of distributed builds using a distributed index.
1132///
1133/// \p ImportList will be populated with a map that can be passed to
1134/// FunctionImporter::importFunctions() above (see description there).
1136 StringRef ModulePath, const ModuleSummaryIndex &Index,
1137 FunctionImporter::ImportMapTy &ImportList) {
1138 for (const auto &GlobalList : Index) {
1139 // Ignore entries for undefined references.
1140 if (GlobalList.second.SummaryList.empty())
1141 continue;
1142
1143 auto GUID = GlobalList.first;
1144 assert(GlobalList.second.SummaryList.size() == 1 &&
1145 "Expected individual combined index to have one summary per GUID");
1146 auto &Summary = GlobalList.second.SummaryList[0];
1147 // Skip the summaries for the importing module. These are included to
1148 // e.g. record required linkage changes.
1149 if (Summary->modulePath() == ModulePath)
1150 continue;
1151 // Add an entry to provoke importing by thinBackend.
1152 ImportList[Summary->modulePath()].insert(GUID);
1153 }
1154#ifndef NDEBUG
1155 dumpImportListForModule(Index, ModulePath, ImportList);
1156#endif
1157}
1158
1159// For SamplePGO, the indirect call targets for local functions will
1160// have its original name annotated in profile. We try to find the
1161// corresponding PGOFuncName as the GUID, and fix up the edges
1162// accordingly.
1164 FunctionSummary *FS) {
1165 for (auto &EI : FS->mutableCalls()) {
1166 if (!EI.first.getSummaryList().empty())
1167 continue;
1168 auto GUID = Index.getGUIDFromOriginalID(EI.first.getGUID());
1169 if (GUID == 0)
1170 continue;
1171 // Update the edge to point directly to the correct GUID.
1172 auto VI = Index.getValueInfo(GUID);
1173 if (llvm::any_of(
1174 VI.getSummaryList(),
1175 [&](const std::unique_ptr<GlobalValueSummary> &SummaryPtr) {
1176 // The mapping from OriginalId to GUID may return a GUID
1177 // that corresponds to a static variable. Filter it out here.
1178 // This can happen when
1179 // 1) There is a call to a library function which is not defined
1180 // in the index.
1181 // 2) There is a static variable with the OriginalGUID identical
1182 // to the GUID of the library function in 1);
1183 // When this happens the static variable in 2) will be found,
1184 // which needs to be filtered out.
1185 return SummaryPtr->getSummaryKind() ==
1186 GlobalValueSummary::GlobalVarKind;
1187 }))
1188 continue;
1189 EI.first = VI;
1190 }
1191}
1192
1194 for (const auto &Entry : Index) {
1195 for (const auto &S : Entry.second.SummaryList) {
1196 if (auto *FS = dyn_cast<FunctionSummary>(S.get()))
1198 }
1199 }
1200}
1201
1204 const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
1206 assert(!Index.withGlobalValueDeadStripping());
1207 if (!ComputeDead ||
1208 // Don't do anything when nothing is live, this is friendly with tests.
1209 GUIDPreservedSymbols.empty()) {
1210 // Still need to update indirect calls.
1212 return;
1213 }
1214 unsigned LiveSymbols = 0;
1216 Worklist.reserve(GUIDPreservedSymbols.size() * 2);
1217 for (auto GUID : GUIDPreservedSymbols) {
1218 ValueInfo VI = Index.getValueInfo(GUID);
1219 if (!VI)
1220 continue;
1221 for (const auto &S : VI.getSummaryList())
1222 S->setLive(true);
1223 }
1224
1225 // Add values flagged in the index as live roots to the worklist.
1226 for (const auto &Entry : Index) {
1227 auto VI = Index.getValueInfo(Entry);
1228 for (const auto &S : Entry.second.SummaryList) {
1229 if (auto *FS = dyn_cast<FunctionSummary>(S.get()))
1231 if (S->isLive()) {
1232 LLVM_DEBUG(dbgs() << "Live root: " << VI << "\n");
1233 Worklist.push_back(VI);
1234 ++LiveSymbols;
1235 break;
1236 }
1237 }
1238 }
1239
1240 // Make value live and add it to the worklist if it was not live before.
1241 auto visit = [&](ValueInfo VI, bool IsAliasee) {
1242 // FIXME: If we knew which edges were created for indirect call profiles,
1243 // we could skip them here. Any that are live should be reached via
1244 // other edges, e.g. reference edges. Otherwise, using a profile collected
1245 // on a slightly different binary might provoke preserving, importing
1246 // and ultimately promoting calls to functions not linked into this
1247 // binary, which increases the binary size unnecessarily. Note that
1248 // if this code changes, the importer needs to change so that edges
1249 // to functions marked dead are skipped.
1250
1251 if (llvm::any_of(VI.getSummaryList(),
1252 [](const std::unique_ptr<llvm::GlobalValueSummary> &S) {
1253 return S->isLive();
1254 }))
1255 return;
1256
1257 // We only keep live symbols that are known to be non-prevailing if any are
1258 // available_externally, linkonceodr, weakodr. Those symbols are discarded
1259 // later in the EliminateAvailableExternally pass and setting them to
1260 // not-live could break downstreams users of liveness information (PR36483)
1261 // or limit optimization opportunities.
1262 if (isPrevailing(VI.getGUID()) == PrevailingType::No) {
1263 bool KeepAliveLinkage = false;
1264 bool Interposable = false;
1265 for (const auto &S : VI.getSummaryList()) {
1266 if (S->linkage() == GlobalValue::AvailableExternallyLinkage ||
1267 S->linkage() == GlobalValue::WeakODRLinkage ||
1268 S->linkage() == GlobalValue::LinkOnceODRLinkage)
1269 KeepAliveLinkage = true;
1270 else if (GlobalValue::isInterposableLinkage(S->linkage()))
1271 Interposable = true;
1272 }
1273
1274 if (!IsAliasee) {
1275 if (!KeepAliveLinkage)
1276 return;
1277
1278 if (Interposable)
1280 "Interposable and available_externally/linkonce_odr/weak_odr "
1281 "symbol");
1282 }
1283 }
1284
1285 for (const auto &S : VI.getSummaryList())
1286 S->setLive(true);
1287 ++LiveSymbols;
1288 Worklist.push_back(VI);
1289 };
1290
1291 while (!Worklist.empty()) {
1292 auto VI = Worklist.pop_back_val();
1293 for (const auto &Summary : VI.getSummaryList()) {
1294 if (auto *AS = dyn_cast<AliasSummary>(Summary.get())) {
1295 // If this is an alias, visit the aliasee VI to ensure that all copies
1296 // are marked live and it is added to the worklist for further
1297 // processing of its references.
1298 visit(AS->getAliaseeVI(), true);
1299 continue;
1300 }
1301 for (auto Ref : Summary->refs())
1302 visit(Ref, false);
1303 if (auto *FS = dyn_cast<FunctionSummary>(Summary.get()))
1304 for (auto Call : FS->calls())
1305 visit(Call.first, false);
1306 }
1307 }
1308 Index.setWithGlobalValueDeadStripping();
1309
1310 unsigned DeadSymbols = Index.size() - LiveSymbols;
1311 LLVM_DEBUG(dbgs() << LiveSymbols << " symbols Live, and " << DeadSymbols
1312 << " symbols Dead \n");
1313 NumDeadSymbols += DeadSymbols;
1314 NumLiveSymbols += LiveSymbols;
1315}
1316
1317// Compute dead symbols and propagate constants in combined index.
1320 const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
1322 bool ImportEnabled) {
1323 computeDeadSymbolsAndUpdateIndirectCalls(Index, GUIDPreservedSymbols,
1324 isPrevailing);
1325 if (ImportEnabled)
1326 Index.propagateAttributes(GUIDPreservedSymbols);
1327}
1328
1329/// Compute the set of summaries needed for a ThinLTO backend compilation of
1330/// \p ModulePath.
1332 StringRef ModulePath,
1333 const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1334 const FunctionImporter::ImportMapTy &ImportList,
1335 std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex) {
1336 // Include all summaries from the importing module.
1337 ModuleToSummariesForIndex[std::string(ModulePath)] =
1338 ModuleToDefinedGVSummaries.lookup(ModulePath);
1339 // Include summaries for imports.
1340 for (const auto &ILI : ImportList) {
1341 auto &SummariesForIndex = ModuleToSummariesForIndex[std::string(ILI.first)];
1342 const auto &DefinedGVSummaries =
1343 ModuleToDefinedGVSummaries.lookup(ILI.first);
1344 for (const auto &GI : ILI.second) {
1345 const auto &DS = DefinedGVSummaries.find(GI);
1346 assert(DS != DefinedGVSummaries.end() &&
1347 "Expected a defined summary for imported global value");
1348 SummariesForIndex[GI] = DS->second;
1349 }
1350 }
1351}
1352
1353/// Emit the files \p ModulePath will import from into \p OutputFilename.
1356 const std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex) {
1357 std::error_code EC;
1358 raw_fd_ostream ImportsOS(OutputFilename, EC, sys::fs::OpenFlags::OF_None);
1359 if (EC)
1360 return EC;
1361 for (const auto &ILI : ModuleToSummariesForIndex)
1362 // The ModuleToSummariesForIndex map includes an entry for the current
1363 // Module (needed for writing out the index files). We don't want to
1364 // include it in the imports file, however, so filter it out.
1365 if (ILI.first != ModulePath)
1366 ImportsOS << ILI.first << "\n";
1367 return std::error_code();
1368}
1369
1371 LLVM_DEBUG(dbgs() << "Converting to a declaration: `" << GV.getName()
1372 << "\n");
1373 if (Function *F = dyn_cast<Function>(&GV)) {
1374 F->deleteBody();
1375 F->clearMetadata();
1376 F->setComdat(nullptr);
1377 } else if (GlobalVariable *V = dyn_cast<GlobalVariable>(&GV)) {
1378 V->setInitializer(nullptr);
1379 V->setLinkage(GlobalValue::ExternalLinkage);
1380 V->clearMetadata();
1381 V->setComdat(nullptr);
1382 } else {
1383 GlobalValue *NewGV;
1384 if (GV.getValueType()->isFunctionTy())
1385 NewGV =
1386 Function::Create(cast<FunctionType>(GV.getValueType()),
1388 "", GV.getParent());
1389 else
1390 NewGV =
1391 new GlobalVariable(*GV.getParent(), GV.getValueType(),
1392 /*isConstant*/ false, GlobalValue::ExternalLinkage,
1393 /*init*/ nullptr, "",
1394 /*insertbefore*/ nullptr, GV.getThreadLocalMode(),
1395 GV.getType()->getAddressSpace());
1396 NewGV->takeName(&GV);
1397 GV.replaceAllUsesWith(NewGV);
1398 return false;
1399 }
1400 if (!GV.isImplicitDSOLocal())
1401 GV.setDSOLocal(false);
1402 return true;
1403}
1404
1406 const GVSummaryMapTy &DefinedGlobals,
1407 bool PropagateAttrs) {
1408 DenseSet<Comdat *> NonPrevailingComdats;
1409 auto FinalizeInModule = [&](GlobalValue &GV, bool Propagate = false) {
1410 // See if the global summary analysis computed a new resolved linkage.
1411 const auto &GS = DefinedGlobals.find(GV.getGUID());
1412 if (GS == DefinedGlobals.end())
1413 return;
1414
1415 if (Propagate)
1416 if (FunctionSummary *FS = dyn_cast<FunctionSummary>(GS->second)) {
1417 if (Function *F = dyn_cast<Function>(&GV)) {
1418 // TODO: propagate ReadNone and ReadOnly.
1419 if (FS->fflags().ReadNone && !F->doesNotAccessMemory())
1420 F->setDoesNotAccessMemory();
1421
1422 if (FS->fflags().ReadOnly && !F->onlyReadsMemory())
1423 F->setOnlyReadsMemory();
1424
1425 if (FS->fflags().NoRecurse && !F->doesNotRecurse())
1426 F->setDoesNotRecurse();
1427
1428 if (FS->fflags().NoUnwind && !F->doesNotThrow())
1429 F->setDoesNotThrow();
1430 }
1431 }
1432
1433 auto NewLinkage = GS->second->linkage();
1435 // Don't internalize anything here, because the code below
1436 // lacks necessary correctness checks. Leave this job to
1437 // LLVM 'internalize' pass.
1438 GlobalValue::isLocalLinkage(NewLinkage) ||
1439 // In case it was dead and already converted to declaration.
1440 GV.isDeclaration())
1441 return;
1442
1443 // Set the potentially more constraining visibility computed from summaries.
1444 // The DefaultVisibility condition is because older GlobalValueSummary does
1445 // not record DefaultVisibility and we don't want to change protected/hidden
1446 // to default.
1447 if (GS->second->getVisibility() != GlobalValue::DefaultVisibility)
1448 GV.setVisibility(GS->second->getVisibility());
1449
1450 if (NewLinkage == GV.getLinkage())
1451 return;
1452
1453 // Check for a non-prevailing def that has interposable linkage
1454 // (e.g. non-odr weak or linkonce). In that case we can't simply
1455 // convert to available_externally, since it would lose the
1456 // interposable property and possibly get inlined. Simply drop
1457 // the definition in that case.
1460 if (!convertToDeclaration(GV))
1461 // FIXME: Change this to collect replaced GVs and later erase
1462 // them from the parent module once thinLTOResolvePrevailingGUID is
1463 // changed to enable this for aliases.
1464 llvm_unreachable("Expected GV to be converted");
1465 } else {
1466 // If all copies of the original symbol had global unnamed addr and
1467 // linkonce_odr linkage, or if all of them had local unnamed addr linkage
1468 // and are constants, then it should be an auto hide symbol. In that case
1469 // the thin link would have marked it as CanAutoHide. Add hidden
1470 // visibility to the symbol to preserve the property.
1471 if (NewLinkage == GlobalValue::WeakODRLinkage &&
1472 GS->second->canAutoHide()) {
1475 }
1476
1477 LLVM_DEBUG(dbgs() << "ODR fixing up linkage for `" << GV.getName()
1478 << "` from " << GV.getLinkage() << " to " << NewLinkage
1479 << "\n");
1480 GV.setLinkage(NewLinkage);
1481 }
1482 // Remove declarations from comdats, including available_externally
1483 // as this is a declaration for the linker, and will be dropped eventually.
1484 // It is illegal for comdats to contain declarations.
1485 auto *GO = dyn_cast_or_null<GlobalObject>(&GV);
1486 if (GO && GO->isDeclarationForLinker() && GO->hasComdat()) {
1487 if (GO->getComdat()->getName() == GO->getName())
1488 NonPrevailingComdats.insert(GO->getComdat());
1489 GO->setComdat(nullptr);
1490 }
1491 };
1492
1493 // Process functions and global now
1494 for (auto &GV : TheModule)
1495 FinalizeInModule(GV, PropagateAttrs);
1496 for (auto &GV : TheModule.globals())
1497 FinalizeInModule(GV);
1498 for (auto &GV : TheModule.aliases())
1499 FinalizeInModule(GV);
1500
1501 // For a non-prevailing comdat, all its members must be available_externally.
1502 // FinalizeInModule has handled non-local-linkage GlobalValues. Here we handle
1503 // local linkage GlobalValues.
1504 if (NonPrevailingComdats.empty())
1505 return;
1506 for (auto &GO : TheModule.global_objects()) {
1507 if (auto *C = GO.getComdat(); C && NonPrevailingComdats.count(C)) {
1508 GO.setComdat(nullptr);
1510 }
1511 }
1512 bool Changed;
1513 do {
1514 Changed = false;
1515 // If an alias references a GlobalValue in a non-prevailing comdat, change
1516 // it to available_externally. For simplicity we only handle GlobalValue and
1517 // ConstantExpr with a base object. ConstantExpr without a base object is
1518 // unlikely used in a COMDAT.
1519 for (auto &GA : TheModule.aliases()) {
1520 if (GA.hasAvailableExternallyLinkage())
1521 continue;
1522 GlobalObject *Obj = GA.getAliaseeObject();
1523 assert(Obj && "aliasee without an base object is unimplemented");
1524 if (Obj->hasAvailableExternallyLinkage()) {
1526 Changed = true;
1527 }
1528 }
1529 } while (Changed);
1530}
1531
1532/// Run internalization on \p TheModule based on symmary analysis.
1534 const GVSummaryMapTy &DefinedGlobals) {
1535 // Declare a callback for the internalize pass that will ask for every
1536 // candidate GlobalValue if it can be internalized or not.
1537 auto MustPreserveGV = [&](const GlobalValue &GV) -> bool {
1538 // It may be the case that GV is on a chain of an ifunc, its alias and
1539 // subsequent aliases. In this case, the summary for the value is not
1540 // available.
1541 if (isa<GlobalIFunc>(&GV) ||
1542 (isa<GlobalAlias>(&GV) &&
1543 isa<GlobalIFunc>(cast<GlobalAlias>(&GV)->getAliaseeObject())))
1544 return true;
1545
1546 // Lookup the linkage recorded in the summaries during global analysis.
1547 auto GS = DefinedGlobals.find(GV.getGUID());
1548 if (GS == DefinedGlobals.end()) {
1549 // Must have been promoted (possibly conservatively). Find original
1550 // name so that we can access the correct summary and see if it can
1551 // be internalized again.
1552 // FIXME: Eventually we should control promotion instead of promoting
1553 // and internalizing again.
1554 StringRef OrigName =
1556 std::string OrigId = GlobalValue::getGlobalIdentifier(
1558 TheModule.getSourceFileName());
1559 GS = DefinedGlobals.find(GlobalValue::getGUID(OrigId));
1560 if (GS == DefinedGlobals.end()) {
1561 // Also check the original non-promoted non-globalized name. In some
1562 // cases a preempted weak value is linked in as a local copy because
1563 // it is referenced by an alias (IRLinker::linkGlobalValueProto).
1564 // In that case, since it was originally not a local value, it was
1565 // recorded in the index using the original name.
1566 // FIXME: This may not be needed once PR27866 is fixed.
1567 GS = DefinedGlobals.find(GlobalValue::getGUID(OrigName));
1568 assert(GS != DefinedGlobals.end());
1569 }
1570 }
1571 return !GlobalValue::isLocalLinkage(GS->second->linkage());
1572 };
1573
1574 // FIXME: See if we can just internalize directly here via linkage changes
1575 // based on the index, rather than invoking internalizeModule.
1576 internalizeModule(TheModule, MustPreserveGV);
1577}
1578
1579/// Make alias a clone of its aliasee.
1581 Function *Fn = cast<Function>(GA->getAliaseeObject());
1582
1583 ValueToValueMapTy VMap;
1584 Function *NewFn = CloneFunction(Fn, VMap);
1585 // Clone should use the original alias's linkage, visibility and name, and we
1586 // ensure all uses of alias instead use the new clone (casted if necessary).
1587 NewFn->setLinkage(GA->getLinkage());
1588 NewFn->setVisibility(GA->getVisibility());
1589 GA->replaceAllUsesWith(NewFn);
1590 NewFn->takeName(GA);
1591 return NewFn;
1592}
1593
1594// Internalize values that we marked with specific attribute
1595// in processGlobalForThinLTO.
1597 for (auto &GV : M.globals())
1598 // Skip GVs which have been converted to declarations
1599 // by dropDeadSymbols.
1600 if (!GV.isDeclaration() && GV.hasAttribute("thinlto-internalize")) {
1601 GV.setLinkage(GlobalValue::InternalLinkage);
1602 GV.setVisibility(GlobalValue::DefaultVisibility);
1603 }
1604}
1605
1606// Automatically import functions in Module \p DestModule based on the summaries
1607// index.
1609 Module &DestModule, const FunctionImporter::ImportMapTy &ImportList) {
1610 LLVM_DEBUG(dbgs() << "Starting import for Module "
1611 << DestModule.getModuleIdentifier() << "\n");
1612 unsigned ImportedCount = 0, ImportedGVCount = 0;
1613
1614 IRMover Mover(DestModule);
1615 // Do the actual import of functions now, one Module at a time
1616 std::set<StringRef> ModuleNameOrderedList;
1617 for (const auto &FunctionsToImportPerModule : ImportList) {
1618 ModuleNameOrderedList.insert(FunctionsToImportPerModule.first);
1619 }
1620 for (const auto &Name : ModuleNameOrderedList) {
1621 // Get the module for the import
1622 const auto &FunctionsToImportPerModule = ImportList.find(Name);
1623 assert(FunctionsToImportPerModule != ImportList.end());
1624 Expected<std::unique_ptr<Module>> SrcModuleOrErr = ModuleLoader(Name);
1625 if (!SrcModuleOrErr)
1626 return SrcModuleOrErr.takeError();
1627 std::unique_ptr<Module> SrcModule = std::move(*SrcModuleOrErr);
1628 assert(&DestModule.getContext() == &SrcModule->getContext() &&
1629 "Context mismatch");
1630
1631 // If modules were created with lazy metadata loading, materialize it
1632 // now, before linking it (otherwise this will be a noop).
1633 if (Error Err = SrcModule->materializeMetadata())
1634 return std::move(Err);
1635
1636 auto &ImportGUIDs = FunctionsToImportPerModule->second;
1637 // Find the globals to import
1638 SetVector<GlobalValue *> GlobalsToImport;
1639 for (Function &F : *SrcModule) {
1640 if (!F.hasName())
1641 continue;
1642 auto GUID = F.getGUID();
1643 auto Import = ImportGUIDs.count(GUID);
1644 LLVM_DEBUG(dbgs() << (Import ? "Is" : "Not") << " importing function "
1645 << GUID << " " << F.getName() << " from "
1646 << SrcModule->getSourceFileName() << "\n");
1647 if (Import) {
1648 if (Error Err = F.materialize())
1649 return std::move(Err);
1650 // MemProf should match function's definition and summary,
1651 // 'thinlto_src_module' is needed.
1653 // Add 'thinlto_src_module' and 'thinlto_src_file' metadata for
1654 // statistics and debugging.
1655 F.setMetadata(
1656 "thinlto_src_module",
1657 MDNode::get(DestModule.getContext(),
1658 {MDString::get(DestModule.getContext(),
1659 SrcModule->getModuleIdentifier())}));
1660 F.setMetadata(
1661 "thinlto_src_file",
1662 MDNode::get(DestModule.getContext(),
1663 {MDString::get(DestModule.getContext(),
1664 SrcModule->getSourceFileName())}));
1665 }
1666 GlobalsToImport.insert(&F);
1667 }
1668 }
1669 for (GlobalVariable &GV : SrcModule->globals()) {
1670 if (!GV.hasName())
1671 continue;
1672 auto GUID = GV.getGUID();
1673 auto Import = ImportGUIDs.count(GUID);
1674 LLVM_DEBUG(dbgs() << (Import ? "Is" : "Not") << " importing global "
1675 << GUID << " " << GV.getName() << " from "
1676 << SrcModule->getSourceFileName() << "\n");
1677 if (Import) {
1678 if (Error Err = GV.materialize())
1679 return std::move(Err);
1680 ImportedGVCount += GlobalsToImport.insert(&GV);
1681 }
1682 }
1683 for (GlobalAlias &GA : SrcModule->aliases()) {
1684 if (!GA.hasName() || isa<GlobalIFunc>(GA.getAliaseeObject()))
1685 continue;
1686 auto GUID = GA.getGUID();
1687 auto Import = ImportGUIDs.count(GUID);
1688 LLVM_DEBUG(dbgs() << (Import ? "Is" : "Not") << " importing alias "
1689 << GUID << " " << GA.getName() << " from "
1690 << SrcModule->getSourceFileName() << "\n");
1691 if (Import) {
1692 if (Error Err = GA.materialize())
1693 return std::move(Err);
1694 // Import alias as a copy of its aliasee.
1695 GlobalObject *GO = GA.getAliaseeObject();
1696 if (Error Err = GO->materialize())
1697 return std::move(Err);
1698 auto *Fn = replaceAliasWithAliasee(SrcModule.get(), &GA);
1699 LLVM_DEBUG(dbgs() << "Is importing aliasee fn " << GO->getGUID() << " "
1700 << GO->getName() << " from "
1701 << SrcModule->getSourceFileName() << "\n");
1703 // Add 'thinlto_src_module' and 'thinlto_src_file' metadata for
1704 // statistics and debugging.
1705 Fn->setMetadata(
1706 "thinlto_src_module",
1707 MDNode::get(DestModule.getContext(),
1708 {MDString::get(DestModule.getContext(),
1709 SrcModule->getModuleIdentifier())}));
1710 Fn->setMetadata(
1711 "thinlto_src_file",
1712 MDNode::get(DestModule.getContext(),
1713 {MDString::get(DestModule.getContext(),
1714 SrcModule->getSourceFileName())}));
1715 }
1716 GlobalsToImport.insert(Fn);
1717 }
1718 }
1719
1720 // Upgrade debug info after we're done materializing all the globals and we
1721 // have loaded all the required metadata!
1722 UpgradeDebugInfo(*SrcModule);
1723
1724 // Set the partial sample profile ratio in the profile summary module flag
1725 // of the imported source module, if applicable, so that the profile summary
1726 // module flag will match with that of the destination module when it's
1727 // imported.
1728 SrcModule->setPartialSampleProfileRatio(Index);
1729
1730 // Link in the specified functions.
1731 if (renameModuleForThinLTO(*SrcModule, Index, ClearDSOLocalOnDeclarations,
1732 &GlobalsToImport))
1733 return true;
1734
1735 if (PrintImports) {
1736 for (const auto *GV : GlobalsToImport)
1737 dbgs() << DestModule.getSourceFileName() << ": Import " << GV->getName()
1738 << " from " << SrcModule->getSourceFileName() << "\n";
1739 }
1740
1741 if (Error Err = Mover.move(std::move(SrcModule),
1742 GlobalsToImport.getArrayRef(), nullptr,
1743 /*IsPerformingImport=*/true))
1745 Twine("Function Import: link error: ") +
1746 toString(std::move(Err)));
1747
1748 ImportedCount += GlobalsToImport.size();
1749 NumImportedModules++;
1750 }
1751
1752 internalizeGVsAfterImport(DestModule);
1753
1754 NumImportedFunctions += (ImportedCount - ImportedGVCount);
1755 NumImportedGlobalVars += ImportedGVCount;
1756
1757 LLVM_DEBUG(dbgs() << "Imported " << ImportedCount - ImportedGVCount
1758 << " functions for Module "
1759 << DestModule.getModuleIdentifier() << "\n");
1760 LLVM_DEBUG(dbgs() << "Imported " << ImportedGVCount
1761 << " global variables for Module "
1762 << DestModule.getModuleIdentifier() << "\n");
1763 return ImportedCount;
1764}
1765
1768 isPrevailing) {
1769 if (SummaryFile.empty())
1770 report_fatal_error("error: -function-import requires -summary-file\n");
1773 if (!IndexPtrOrErr) {
1774 logAllUnhandledErrors(IndexPtrOrErr.takeError(), errs(),
1775 "Error loading file '" + SummaryFile + "': ");
1776 return false;
1777 }
1778 std::unique_ptr<ModuleSummaryIndex> Index = std::move(*IndexPtrOrErr);
1779
1780 // First step is collecting the import list.
1782 // If requested, simply import all functions in the index. This is used
1783 // when testing distributed backend handling via the opt tool, when
1784 // we have distributed indexes containing exactly the summaries to import.
1785 if (ImportAllIndex)
1787 *Index, ImportList);
1788 else
1789 ComputeCrossModuleImportForModuleForTest(M.getModuleIdentifier(),
1790 isPrevailing, *Index, ImportList);
1791
1792 // Conservatively mark all internal values as promoted. This interface is
1793 // only used when doing importing via the function importing pass. The pass
1794 // is only enabled when testing importing via the 'opt' tool, which does
1795 // not do the ThinLink that would normally determine what values to promote.
1796 for (auto &I : *Index) {
1797 for (auto &S : I.second.SummaryList) {
1798 if (GlobalValue::isLocalLinkage(S->linkage()))
1799 S->setLinkage(GlobalValue::ExternalLinkage);
1800 }
1801 }
1802
1803 // Next we need to promote to global scope and rename any local values that
1804 // are potentially exported to other modules.
1805 if (renameModuleForThinLTO(M, *Index, /*ClearDSOLocalOnDeclarations=*/false,
1806 /*GlobalsToImport=*/nullptr)) {
1807 errs() << "Error renaming module\n";
1808 return true;
1809 }
1810
1811 // Perform the import now.
1812 auto ModuleLoader = [&M](StringRef Identifier) {
1813 return loadFile(std::string(Identifier), M.getContext());
1814 };
1815 FunctionImporter Importer(*Index, ModuleLoader,
1816 /*ClearDSOLocalOnDeclarations=*/false);
1817 Expected<bool> Result = Importer.importFunctions(M, ImportList);
1818
1819 // FIXME: Probably need to propagate Errors through the pass manager.
1820 if (!Result) {
1821 logAllUnhandledErrors(Result.takeError(), errs(),
1822 "Error importing module: ");
1823 return true;
1824 }
1825
1826 return true;
1827}
1828
1831 // This is only used for testing the function import pass via opt, where we
1832 // don't have prevailing information from the LTO context available, so just
1833 // conservatively assume everything is prevailing (which is fine for the very
1834 // limited use of prevailing checking in this pass).
1835 auto isPrevailing = [](GlobalValue::GUID, const GlobalValueSummary *) {
1836 return true;
1837 };
1838 if (!doImportingForModuleForTest(M, isPrevailing))
1839 return PreservedAnalyses::all();
1840
1841 return PreservedAnalyses::none();
1842}
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 const GlobalValueSummary * selectCallee(const ModuleSummaryIndex &Index, ArrayRef< std::unique_ptr< GlobalValueSummary > > CalleeSummaryList, unsigned Threshold, StringRef CallerModulePath, FunctionImporter::ImportFailureReason &Reason)
Given a list of possible callee implementation for a call site, select one that fits the Threshold.
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< 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 unsigned numGlobalVarSummaries(const ModuleSummaryIndex &Index, T &Cont)
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.
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:348
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
unsigned size() const
Definition: DenseMap.h:99
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:162
const GlobalObject * getAliaseeObject() const
Definition: Globals.cpp:556
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:248
bool isImplicitDSOLocal() const
Definition: GlobalValue.h:298
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:274
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:271
static bool isAvailableExternallyLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:378
void setLinkage(LinkageTypes LT)
Definition: GlobalValue.h:536
unsigned getAddressSpace() const
Definition: GlobalValue.h:205
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:368
void setDSOLocal(bool Local)
Definition: GlobalValue.h:303
PointerType * getType() const
Global values are always pointers.
Definition: GlobalValue.h:294
@ DefaultVisibility
The GV is visible.
Definition: GlobalValue.h:67
@ HiddenVisibility
The GV is hidden.
Definition: GlobalValue.h:68
static GUID getGUID(StringRef GlobalName)
Return a 64-bit global unique ID constructed from global value name (i.e.
Definition: GlobalValue.h:590
void setVisibility(VisibilityTypes V)
Definition: GlobalValue.h:254
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:47
bool canBeOmittedFromSymbolTable() const
True if GV can be left out of the object symbol table.
Definition: Globals.cpp:393
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:169
@ InternalLinkage
Rename collisions when linking (static functions).
Definition: GlobalValue.h:59
@ WeakODRLinkage
Same, but only replaced by something equivalent.
Definition: GlobalValue.h:57
@ ExternalLinkage
Externally visible function.
Definition: GlobalValue.h:52
@ AvailableExternallyLinkage
Available for inspection, not emission.
Definition: GlobalValue.h:53
@ LinkOnceODRLinkage
Same, but only replaced by something equivalent.
Definition: GlobalValue.h:55
Type * getValueType() const
Definition: GlobalValue.h:296
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:1792
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:295
const std::string & getSourceFileName() const
Get the module's original source file name.
Definition: Module.h:272
iterator_range< alias_iterator > aliases()
Definition: Module.h:733
iterator_range< global_iterator > globals()
Definition: Module.h:693
const std::string & getModuleIdentifier() const
Get the module identifier which is, essentially, the name of the module.
Definition: Module.h:261
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
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:221
iterator find(StringRef Key)
Definition: StringMap.h:234
size_type count(StringRef Key) const
count - Return 1 if the element is in the map, 0 otherwise.
Definition: StringMap.h:277
bool insert(MapEntryTy *KeyValue)
insert - Insert the specified key/value pair into the map.
Definition: StringMap.h:307
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
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
bool erase(const ValueT &V)
Definition: DenseSet.h:101
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 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.
@ Import
Import information from summary.
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:665
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:1738
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:581
void gatherImportedSummariesForModule(StringRef ModulePath, const DenseMap< StringRef, GVSummaryMapTy > &ModuleToDefinedGVSummaries, const FunctionImporter::ImportMapTy &ImportList, std::map< std::string, GVSummaryMapTy > &ModuleToSummariesForIndex)
Compute the set of summaries needed for a ThinLTO backend compilation of ModulePath.
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.