LLVM 19.0.0git
FunctionImport.h
Go to the documentation of this file.
1//===- llvm/Transforms/IPO/FunctionImport.h - ThinLTO importing -*- C++ -*-===//
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#ifndef LLVM_TRANSFORMS_IPO_FUNCTIONIMPORT_H
10#define LLVM_TRANSFORMS_IPO_FUNCTIONIMPORT_H
11
12#include "llvm/ADT/DenseSet.h"
13#include "llvm/ADT/StringRef.h"
14#include "llvm/IR/GlobalValue.h"
16#include "llvm/IR/PassManager.h"
17#include "llvm/Support/Error.h"
18#include <functional>
19#include <map>
20#include <memory>
21#include <string>
22#include <system_error>
23#include <unordered_map>
24#include <unordered_set>
25#include <utility>
26
27namespace llvm {
28
29class Module;
30
31/// The function importer is automatically importing function from other modules
32/// based on the provided summary informations.
34public:
35 /// The functions to import from a source module and their import type.
36 /// Note we choose unordered_map over (Small)DenseMap. The number of imports
37 /// from a source module could be small but DenseMap size grows to 64 quickly
38 /// and not memory efficient (see
39 /// https://llvm.org/docs/ProgrammersManual.html#llvm-adt-densemap-h)
41 std::unordered_map<GlobalValue::GUID, GlobalValueSummary::ImportKind>;
42
43 /// The different reasons selectCallee will chose not to import a
44 /// candidate.
47 // We can encounter a global variable instead of a function in rare
48 // situations with SamplePGO. See comments where this failure type is
49 // set for more details.
51 // Found to be globally dead, so we don't bother importing.
53 // Instruction count over the current threshold.
55 // Don't import something with interposable linkage as we can't inline it
56 // anyway.
58 // Generally we won't end up failing due to this reason, as we expect
59 // to find at least one summary for the GUID that is global or a local
60 // in the referenced module for direct calls.
62 // This corresponds to the NotEligibleToImport being set on the summary,
63 // which can happen in a few different cases (e.g. local that can't be
64 // renamed or promoted because it is referenced on a llvm*.used variable).
66 // This corresponds to NoInline being set on the function summary,
67 // which will happen if it is known that the inliner will not be able
68 // to inline the function (e.g. it is marked with a NoInline attribute).
70 };
71
72 /// Information optionally tracked for candidates the importer decided
73 /// not to import. Used for optional stat printing.
75 // The ValueInfo corresponding to the candidate. We save an index hash
76 // table lookup for each GUID by stashing this here.
78 // The maximum call edge hotness for all failed imports of this candidate.
80 // most recent reason for failing to import (doesn't necessarily correspond
81 // to the attempt with the maximum hotness).
83 // The number of times we tried to import candidate but failed.
84 unsigned Attempts;
88 };
89
90 /// Map of callee GUID considered for import into a given module to a pair
91 /// consisting of the largest threshold applied when deciding whether to
92 /// import it and, if we decided to import, a pointer to the summary instance
93 /// imported. If we decided not to import, the summary will be nullptr.
96 std::tuple<unsigned, const GlobalValueSummary *,
97 std::unique_ptr<ImportFailureInfo>>>;
98
99 /// The map contains an entry for every module to import from, the key being
100 /// the module identifier to pass to the ModuleLoader. The value is the set of
101 /// functions to import. The module identifier strings must be owned
102 /// elsewhere, typically by the in-memory ModuleSummaryIndex the importing
103 /// decisions are made from (the module path for each summary is owned by the
104 /// index's module path string table).
106
107 /// The set contains an entry for every global value that the module exports.
108 /// Depending on the user context, this container is allowed to contain
109 /// definitions, declarations or a mix of both.
111
112 /// A function of this type is used to load modules referenced by the index.
114 std::function<Expected<std::unique_ptr<Module>>(StringRef Identifier)>;
115
116 /// Create a Function Importer.
118 bool ClearDSOLocalOnDeclarations)
119 : Index(Index), ModuleLoader(std::move(ModuleLoader)),
120 ClearDSOLocalOnDeclarations(ClearDSOLocalOnDeclarations) {}
121
122 /// Import functions in Module \p M based on the supplied import list.
123 Expected<bool> importFunctions(Module &M, const ImportMapTy &ImportList);
124
125private:
126 /// The summaries index used to trigger importing.
128
129 /// Factory function to load a Module for a given identifier
130 ModuleLoaderTy ModuleLoader;
131
132 /// See the comment of ClearDSOLocalOnDeclarations in
133 /// Utils/FunctionImportUtils.h.
134 bool ClearDSOLocalOnDeclarations;
135};
136
137/// The function importing pass
138class FunctionImportPass : public PassInfoMixin<FunctionImportPass> {
139public:
141};
142
143/// Compute all the imports and exports for every module in the Index.
144///
145/// \p ModuleToDefinedGVSummaries contains for each Module a map
146/// (GUID -> Summary) for every global defined in the module.
147///
148/// \p isPrevailing is a callback that will be called with a global value's GUID
149/// and summary and should return whether the module corresponding to the
150/// summary contains the linker-prevailing copy of that value.
151///
152/// \p ImportLists will be populated with an entry for every Module we are
153/// importing into. This entry is itself a map that can be passed to
154/// FunctionImporter::importFunctions() above (see description there).
155///
156/// \p ExportLists contains for each Module the set of globals (GUID) that will
157/// be imported by another module, or referenced by such a function. I.e. this
158/// is the set of globals that need to be promoted/renamed appropriately.
159///
160/// The module identifier strings that are the keys of the above two maps
161/// are owned by the in-memory ModuleSummaryIndex the importing decisions
162/// are made from (the module path for each summary is owned by the index's
163/// module path string table).
166 const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
168 isPrevailing,
171
172/// PrevailingType enum used as a return type of callback passed
173/// to computeDeadSymbolsAndUpdateIndirectCalls. Yes and No values used when
174/// status explicitly set by symbols resolution, otherwise status is Unknown.
175enum class PrevailingType { Yes, No, Unknown };
176
177/// Update call edges for indirect calls to local functions added from
178/// SamplePGO when needed. Normally this is done during
179/// computeDeadSymbolsAndUpdateIndirectCalls, but can be called standalone
180/// when that is not called (e.g. during testing).
181void updateIndirectCalls(ModuleSummaryIndex &Index);
182
183/// Compute all the symbols that are "dead": i.e these that can't be reached
184/// in the graph from any of the given symbols listed in
185/// \p GUIDPreservedSymbols. Non-prevailing symbols are symbols without a
186/// prevailing copy anywhere in IR and are normally dead, \p isPrevailing
187/// predicate returns status of symbol.
188/// Also update call edges for indirect calls to local functions added from
189/// SamplePGO when needed.
191 ModuleSummaryIndex &Index,
192 const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
193 function_ref<PrevailingType(GlobalValue::GUID)> isPrevailing);
194
195/// Compute dead symbols and run constant propagation in combined index
196/// after that.
198 ModuleSummaryIndex &Index,
199 const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
200 function_ref<PrevailingType(GlobalValue::GUID)> isPrevailing,
201 bool ImportEnabled);
202
203/// Converts value \p GV to declaration, or replaces with a declaration if
204/// it is an alias. Returns true if converted, false if replaced.
205bool convertToDeclaration(GlobalValue &GV);
206
207/// Compute the set of summaries needed for a ThinLTO backend compilation of
208/// \p ModulePath.
209//
210/// This includes summaries from that module (in case any global summary based
211/// optimizations were recorded) and from any definitions in other modules that
212/// should be imported.
213//
214/// \p ModuleToSummariesForIndex will be populated with the needed summaries
215/// from each required module path. Use a std::map instead of StringMap to get
216/// stable order for bitcode emission.
217///
218/// \p DecSummaries will be popluated with the subset of of summary pointers
219/// that have 'declaration' import type among all summaries the module need.
221 StringRef ModulePath,
222 const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
223 const FunctionImporter::ImportMapTy &ImportList,
224 std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex,
225 GVSummaryPtrSet &DecSummaries);
226
227/// Emit into \p OutputFilename the files module \p ModulePath will import from.
228std::error_code EmitImportsFiles(
229 StringRef ModulePath, StringRef OutputFilename,
230 const std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex);
231
232/// Based on the information recorded in the summaries during global
233/// summary-based analysis:
234/// 1. Resolve prevailing symbol linkages and constrain visibility (CanAutoHide
235/// and consider visibility from other definitions for ELF) in \p TheModule
236/// 2. (optional) Apply propagated function attributes to \p TheModule if
237/// PropagateAttrs is true
238void thinLTOFinalizeInModule(Module &TheModule,
239 const GVSummaryMapTy &DefinedGlobals,
240 bool PropagateAttrs);
241
242/// Internalize \p TheModule based on the information recorded in the summaries
243/// during global summary-based analysis.
244void thinLTOInternalizeModule(Module &TheModule,
245 const GVSummaryMapTy &DefinedGlobals);
246
247} // end namespace llvm
248
249#endif // LLVM_TRANSFORMS_IPO_FUNCTIONIMPORT_H
This file defines the DenseSet and SmallDenseSet classes.
Machine Check Debug Module
static cl::opt< std::string > OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"), cl::init("-"))
static cl::opt< bool > PropagateAttrs("propagate-attrs", cl::init(true), cl::Hidden, cl::desc("Propagate attributes in index"))
ModuleSummaryIndex.h This file contains the declarations the classes that hold the module index and s...
This header defines various interfaces for pass management in LLVM.
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:253
Implements a dense probed hash-table based set.
Definition: DenseSet.h:271
Tagged union holding either a T or a Error.
Definition: Error.h:481
The function importing pass.
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
The function importer is automatically importing function from other modules based on the provided su...
Expected< bool > importFunctions(Module &M, const ImportMapTy &ImportList)
Import functions in Module M based on the supplied import list.
std::unordered_map< GlobalValue::GUID, GlobalValueSummary::ImportKind > FunctionsToImportTy
The functions to import from a source module and their import type.
DenseMap< StringRef, FunctionsToImportTy > ImportMapTy
The map contains an entry for every module to import from, the key being the module identifier to pas...
FunctionImporter(const ModuleSummaryIndex &Index, ModuleLoaderTy ModuleLoader, bool ClearDSOLocalOnDeclarations)
Create a Function Importer.
ImportFailureReason
The different reasons selectCallee will chose not to import a candidate.
std::function< Expected< std::unique_ptr< Module > >(StringRef Identifier)> ModuleLoaderTy
A function of this type is used to load modules referenced by the index.
Function and variable summary information to aid decisions and implementation of importing.
uint64_t GUID
Declare a type to represent a global unique identifier for a global value.
Definition: GlobalValue.h:587
Class to hold module path string table and global value map, and encapsulate methods for operating on...
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:111
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
An efficient, type-erasing, non-owning reference to a callable.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void gatherImportedSummariesForModule(StringRef ModulePath, const DenseMap< StringRef, GVSummaryMapTy > &ModuleToDefinedGVSummaries, const FunctionImporter::ImportMapTy &ImportList, std::map< std::string, GVSummaryMapTy > &ModuleToSummariesForIndex, GVSummaryPtrSet &DecSummaries)
Compute the set of summaries needed for a ThinLTO backend compilation of ModulePath.
void ComputeCrossModuleImport(const ModuleSummaryIndex &Index, const DenseMap< StringRef, GVSummaryMapTy > &ModuleToDefinedGVSummaries, function_ref< bool(GlobalValue::GUID, const GlobalValueSummary *)> isPrevailing, DenseMap< StringRef, FunctionImporter::ImportMapTy > &ImportLists, DenseMap< StringRef, FunctionImporter::ExportSetTy > &ExportLists)
Compute all the imports and exports for every module in the Index.
std::error_code EmitImportsFiles(StringRef ModulePath, StringRef OutputFilename, const std::map< std::string, GVSummaryMapTy > &ModuleToSummariesForIndex)
Emit into OutputFilename the files module ModulePath will import from.
bool convertToDeclaration(GlobalValue &GV)
Converts value GV to declaration, or replaces with a declaration if it is an alias.
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...
void updateIndirectCalls(ModuleSummaryIndex &Index)
Update call edges for indirect calls to local functions added from SamplePGO when needed.
void thinLTOInternalizeModule(Module &TheModule, const GVSummaryMapTy &DefinedGlobals)
Internalize TheModule based on the information recorded in the summaries during global summary-based ...
std::unordered_set< GlobalValueSummary * > GVSummaryPtrSet
A set of global value summary pointers.
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1849
PrevailingType
PrevailingType enum used as a return type of callback passed to computeDeadSymbolsAndUpdateIndirectCa...
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.
DenseMap< GlobalValue::GUID, GlobalValueSummary * > GVSummaryMapTy
Map of global value GUID to its summary, used to identify values defined in a particular module,...
void thinLTOFinalizeInModule(Module &TheModule, const GVSummaryMapTy &DefinedGlobals, bool PropagateAttrs)
Based on the information recorded in the summaries during global summary-based analysis:
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858
Information optionally tracked for candidates the importer decided not to import.
ImportFailureInfo(ValueInfo VI, CalleeInfo::HotnessType MaxHotness, ImportFailureReason Reason, unsigned Attempts)
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:69
Struct that holds a reference to a particular GUID in a global value summary.