Line data Source code
1 : //===- llvm/Transforms/IPO/FunctionImport.h - ThinLTO importing -*- C++ -*-===//
2 : //
3 : // The LLVM Compiler Infrastructure
4 : //
5 : // This file is distributed under the University of Illinois Open Source
6 : // License. See LICENSE.TXT for details.
7 : //
8 : //===----------------------------------------------------------------------===//
9 :
10 : #ifndef LLVM_TRANSFORMS_IPO_FUNCTIONIMPORT_H
11 : #define LLVM_TRANSFORMS_IPO_FUNCTIONIMPORT_H
12 :
13 : #include "llvm/ADT/DenseSet.h"
14 : #include "llvm/ADT/StringMap.h"
15 : #include "llvm/ADT/StringRef.h"
16 : #include "llvm/IR/GlobalValue.h"
17 : #include "llvm/IR/ModuleSummaryIndex.h"
18 : #include "llvm/IR/PassManager.h"
19 : #include "llvm/Support/Error.h"
20 : #include <functional>
21 : #include <map>
22 : #include <memory>
23 : #include <string>
24 : #include <system_error>
25 : #include <unordered_set>
26 : #include <utility>
27 :
28 : namespace llvm {
29 :
30 : class Module;
31 :
32 : /// The function importer is automatically importing function from other modules
33 : /// based on the provided summary informations.
34 290 : class FunctionImporter {
35 : public:
36 : /// Set of functions to import from a source module. Each entry is a set
37 : /// containing all the GUIDs of all functions to import for a source module.
38 : using FunctionsToImportTy = std::unordered_set<GlobalValue::GUID>;
39 :
40 : /// The different reasons selectCallee will chose not to import a
41 : /// candidate.
42 : enum ImportFailureReason {
43 : None,
44 : // We can encounter a global variable instead of a function in rare
45 : // situations with SamplePGO. See comments where this failure type is
46 : // set for more details.
47 : GlobalVar,
48 : // Found to be globally dead, so we don't bother importing.
49 : NotLive,
50 : // Instruction count over the current threshold.
51 : TooLarge,
52 : // Don't import something with interposable linkage as we can't inline it
53 : // anyway.
54 : InterposableLinkage,
55 : // Generally we won't end up failing due to this reason, as we expect
56 : // to find at least one summary for the GUID that is global or a local
57 : // in the referenced module for direct calls.
58 : LocalLinkageNotInModule,
59 : // This corresponse to the NotEligibleToImport being set on the summary,
60 : // which can happen in a few different cases (e.g. local that can't be
61 : // renamed or promoted because it is referenced on a llvm*.used variable).
62 : NotEligible
63 : };
64 :
65 : /// Information optionally tracked for candidates the importer decided
66 : /// not to import. Used for optional stat printing.
67 : struct ImportFailureInfo {
68 : // The ValueInfo corresponding to the candidate. We save an index hash
69 : // table lookup for each GUID by stashing this here.
70 : ValueInfo VI;
71 : // The maximum call edge hotness for all failed imports of this candidate.
72 : CalleeInfo::HotnessType MaxHotness;
73 : // most recent reason for failing to import (doesn't necessarily correspond
74 : // to the attempt with the maximum hotness).
75 : ImportFailureReason Reason;
76 : // The number of times we tried to import candidate but failed.
77 : unsigned Attempts;
78 : ImportFailureInfo(ValueInfo VI, CalleeInfo::HotnessType MaxHotness,
79 : ImportFailureReason Reason, unsigned Attempts)
80 1 : : VI(VI), MaxHotness(MaxHotness), Reason(Reason), Attempts(Attempts) {}
81 : };
82 :
83 : /// Map of callee GUID considered for import into a given module to a pair
84 : /// consisting of the largest threshold applied when deciding whether to
85 : /// import it and, if we decided to import, a pointer to the summary instance
86 : /// imported. If we decided not to import, the summary will be nullptr.
87 : using ImportThresholdsTy =
88 : DenseMap<GlobalValue::GUID,
89 : std::tuple<unsigned, const GlobalValueSummary *,
90 : std::unique_ptr<ImportFailureInfo>>>;
91 :
92 : /// The map contains an entry for every module to import from, the key being
93 : /// the module identifier to pass to the ModuleLoader. The value is the set of
94 : /// functions to import.
95 : using ImportMapTy = StringMap<FunctionsToImportTy>;
96 :
97 : /// The set contains an entry for every global value the module exports.
98 : using ExportSetTy = std::unordered_set<GlobalValue::GUID>;
99 :
100 : /// A function of this type is used to load modules referenced by the index.
101 : using ModuleLoaderTy =
102 : std::function<Expected<std::unique_ptr<Module>>(StringRef Identifier)>;
103 :
104 : /// Create a Function Importer.
105 : FunctionImporter(const ModuleSummaryIndex &Index, ModuleLoaderTy ModuleLoader)
106 357 : : Index(Index), ModuleLoader(std::move(ModuleLoader)) {}
107 :
108 : /// Import functions in Module \p M based on the supplied import list.
109 : Expected<bool> importFunctions(Module &M, const ImportMapTy &ImportList);
110 :
111 : private:
112 : /// The summaries index used to trigger importing.
113 : const ModuleSummaryIndex &Index;
114 :
115 : /// Factory function to load a Module for a given identifier
116 : ModuleLoaderTy ModuleLoader;
117 : };
118 :
119 : /// The function importing pass
120 : class FunctionImportPass : public PassInfoMixin<FunctionImportPass> {
121 : public:
122 : PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
123 : };
124 :
125 : /// Compute all the imports and exports for every module in the Index.
126 : ///
127 : /// \p ModuleToDefinedGVSummaries contains for each Module a map
128 : /// (GUID -> Summary) for every global defined in the module.
129 : ///
130 : /// \p ImportLists will be populated with an entry for every Module we are
131 : /// importing into. This entry is itself a map that can be passed to
132 : /// FunctionImporter::importFunctions() above (see description there).
133 : ///
134 : /// \p ExportLists contains for each Module the set of globals (GUID) that will
135 : /// be imported by another module, or referenced by such a function. I.e. this
136 : /// is the set of globals that need to be promoted/renamed appropriately.
137 : void ComputeCrossModuleImport(
138 : const ModuleSummaryIndex &Index,
139 : const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
140 : StringMap<FunctionImporter::ImportMapTy> &ImportLists,
141 : StringMap<FunctionImporter::ExportSetTy> &ExportLists);
142 :
143 : /// Compute all the imports for the given module using the Index.
144 : ///
145 : /// \p ImportList will be populated with a map that can be passed to
146 : /// FunctionImporter::importFunctions() above (see description there).
147 : void ComputeCrossModuleImportForModule(
148 : StringRef ModulePath, const ModuleSummaryIndex &Index,
149 : FunctionImporter::ImportMapTy &ImportList);
150 :
151 : /// Mark all external summaries in \p Index for import into the given module.
152 : /// Used for distributed builds using a distributed index.
153 : ///
154 : /// \p ImportList will be populated with a map that can be passed to
155 : /// FunctionImporter::importFunctions() above (see description there).
156 : void ComputeCrossModuleImportForModuleFromIndex(
157 : StringRef ModulePath, const ModuleSummaryIndex &Index,
158 : FunctionImporter::ImportMapTy &ImportList);
159 :
160 : /// PrevailingType enum used as a return type of callback passed
161 : /// to computeDeadSymbols. Yes and No values used when status explicitly
162 : /// set by symbols resolution, otherwise status is Unknown.
163 : enum class PrevailingType { Yes, No, Unknown };
164 :
165 : /// Compute all the symbols that are "dead": i.e these that can't be reached
166 : /// in the graph from any of the given symbols listed in
167 : /// \p GUIDPreservedSymbols. Non-prevailing symbols are symbols without a
168 : /// prevailing copy anywhere in IR and are normally dead, \p isPrevailing
169 : /// predicate returns status of symbol.
170 : void computeDeadSymbols(
171 : ModuleSummaryIndex &Index,
172 : const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
173 : function_ref<PrevailingType(GlobalValue::GUID)> isPrevailing);
174 :
175 : /// Converts value \p GV to declaration, or replaces with a declaration if
176 : /// it is an alias. Returns true if converted, false if replaced.
177 : bool convertToDeclaration(GlobalValue &GV);
178 :
179 : /// Compute the set of summaries needed for a ThinLTO backend compilation of
180 : /// \p ModulePath.
181 : //
182 : /// This includes summaries from that module (in case any global summary based
183 : /// optimizations were recorded) and from any definitions in other modules that
184 : /// should be imported.
185 : //
186 : /// \p ModuleToSummariesForIndex will be populated with the needed summaries
187 : /// from each required module path. Use a std::map instead of StringMap to get
188 : /// stable order for bitcode emission.
189 : void gatherImportedSummariesForModule(
190 : StringRef ModulePath,
191 : const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
192 : const FunctionImporter::ImportMapTy &ImportList,
193 : std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex);
194 :
195 : /// Emit into \p OutputFilename the files module \p ModulePath will import from.
196 : std::error_code EmitImportsFiles(
197 : StringRef ModulePath, StringRef OutputFilename,
198 : const std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex);
199 :
200 : /// Resolve WeakForLinker values in \p TheModule based on the information
201 : /// recorded in the summaries during global summary-based analysis.
202 : void thinLTOResolveWeakForLinkerModule(Module &TheModule,
203 : const GVSummaryMapTy &DefinedGlobals);
204 :
205 : /// Internalize \p TheModule based on the information recorded in the summaries
206 : /// during global summary-based analysis.
207 : void thinLTOInternalizeModule(Module &TheModule,
208 : const GVSummaryMapTy &DefinedGlobals);
209 :
210 : } // end namespace llvm
211 :
212 : #endif // LLVM_TRANSFORMS_IPO_FUNCTIONIMPORT_H
|