Movatterモバイル変換


[0]ホーム

URL:


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

Generated on Sun Jul 20 2025 13:39:49 for LLVM by doxygen 1.9.6
[8]ページ先頭

©2009-2025 Movatter.jp