llvm.org GIT mirror llvm / c37b055
Revert "[ThinLTO] Renaming of function index to module summary index (NFC)" This reverts commit r263490. Missed a file. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@263493 91177308-0d34-0410-b5e6-96231b3b80d8 Teresa Johnson 3 years ago
49 changed file(s) with 986 addition(s) and 559 deletion(s). Raw diff Collapse all Expand all
2929 /// If \c ShouldPreserveUseListOrder, encode use-list order so it can be
3030 /// reproduced when deserialized.
3131 ///
32 /// If \c EmitSummaryIndex, emit the summary index (currently
32 /// If \c EmitFunctionSummary, emit the function summary index (currently
3333 /// for use in ThinLTO optimization).
3434 ModulePass *createBitcodeWriterPass(raw_ostream &Str,
3535 bool ShouldPreserveUseListOrder = false,
36 bool EmitSummaryIndex = false);
36 bool EmitFunctionSummary = false);
3737
3838 /// \brief Pass for writing a module of IR out to a bitcode file.
3939 ///
4242 class BitcodeWriterPass {
4343 raw_ostream &OS;
4444 bool ShouldPreserveUseListOrder;
45 bool EmitSummaryIndex;
45 bool EmitFunctionSummary;
4646
4747 public:
4848 /// \brief Construct a bitcode writer pass around a particular output stream.
5050 /// If \c ShouldPreserveUseListOrder, encode use-list order so it can be
5151 /// reproduced when deserialized.
5252 ///
53 /// If \c EmitSummaryIndex, emit the summary index (currently
53 /// If \c EmitFunctionSummary, emit the function summary index (currently
5454 /// for use in ThinLTO optimization).
5555 explicit BitcodeWriterPass(raw_ostream &OS,
5656 bool ShouldPreserveUseListOrder = false,
57 bool EmitSummaryIndex = false)
57 bool EmitFunctionSummary = false)
5858 : OS(OS), ShouldPreserveUseListOrder(ShouldPreserveUseListOrder),
59 EmitSummaryIndex(EmitSummaryIndex) {}
59 EmitFunctionSummary(EmitFunctionSummary) {}
6060
6161 /// \brief Run the bitcode writer pass, and output the module to the selected
6262 /// output stream.
185185 MST_CODE_ENTRY = 1, // MST_ENTRY: [modid, namechar x N]
186186 };
187187
188 // The summary section uses different codes in the per-module
188 // The function summary section uses different codes in the per-module
189189 // and combined index cases.
190 enum GlobalValueSummarySymtabCodes {
190 enum FunctionSummarySymtabCodes {
191191 // PERMODULE: [valueid, linkage, instcount, numrefs, numrefs x valueid,
192192 // n x (valueid, callsitecount)]
193193 FS_PERMODULE = 1,
1414 #define LLVM_BITCODE_READERWRITER_H
1515
1616 #include "llvm/IR/DiagnosticInfo.h"
17 #include "llvm/IR/ModuleSummaryIndex.h"
17 #include "llvm/IR/FunctionInfo.h"
1818 #include "llvm/Support/Endian.h"
1919 #include "llvm/Support/ErrorOr.h"
2020 #include "llvm/Support/MemoryBuffer.h"
7373 bool hasGlobalValueSummary(MemoryBufferRef Buffer,
7474 DiagnosticHandlerFunction DiagnosticHandler);
7575
76 /// Parse the specified bitcode buffer, returning the module summary index.
77 /// If IsLazy is true, parse the entire module summary into
78 /// the index. Otherwise skip the module summary section, and only create
79 /// an index object with a map from value name to the value's summary offset.
80 /// The index is used to perform lazy summary reading later.
81 ErrorOr>
82 getModuleSummaryIndex(MemoryBufferRef Buffer,
83 DiagnosticHandlerFunction DiagnosticHandler,
84 bool IsLazy = false);
85
86 /// This method supports lazy reading of summary data from the
76 /// Parse the specified bitcode buffer, returning the function info index.
77 /// If IsLazy is true, parse the entire function summary into
78 /// the index. Otherwise skip the function summary section, and only create
79 /// an index object with a map from function name to function summary offset.
80 /// The index is used to perform lazy function summary reading later.
81 ErrorOr>
82 getFunctionInfoIndex(MemoryBufferRef Buffer,
83 DiagnosticHandlerFunction DiagnosticHandler,
84 bool IsLazy = false);
85
86 /// This method supports lazy reading of function summary data from the
8787 /// combined index during function importing. When reading the combined index
88 /// file, getModuleSummaryIndex is first invoked with IsLazy=true.
89 /// Then this method is called for each value considered for importing,
90 /// to parse the summary information for the given value name into
88 /// file, getFunctionInfoIndex is first invoked with IsLazy=true.
89 /// Then this method is called for each function considered for importing,
90 /// to parse the summary information for the given function name into
9191 /// the index.
92 std::error_code readGlobalValueSummary(
92 std::error_code readFunctionSummary(
9393 MemoryBufferRef Buffer, DiagnosticHandlerFunction DiagnosticHandler,
94 StringRef ValueName, std::unique_ptrIndex> Index);
94 StringRef FunctionName, std::unique_ptrIndex> Index);
9595
9696 /// \brief Write the specified module to the specified raw output stream.
9797 ///
111111 /// Write the specified module summary index to the given raw output stream,
112112 /// where it will be written in a new bitcode block. This is used when
113113 /// writing the combined index file for ThinLTO.
114 void WriteIndexToFile(const ModuleSummaryIndex &Index, raw_ostream &Out);
114 void WriteIndexToFile(const FunctionInfoIndex &Index, raw_ostream &Out);
115115
116116 /// isBitcodeWrapper - Return true if the given bytes are the magic bytes
117117 /// for an LLVM IR bitcode wrapper.
0 //===-- llvm/FunctionInfo.h - Function Info Index ---------------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 /// @file
10 /// FunctionInfo.h This file contains the declarations the classes that hold
11 /// the module index and summary for function importing.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_IR_FUNCTIONINFO_H
16 #define LLVM_IR_FUNCTIONINFO_H
17
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/DenseSet.h"
20 #include "llvm/ADT/SmallString.h"
21 #include "llvm/ADT/StringMap.h"
22 #include "llvm/IR/Function.h"
23 #include "llvm/IR/Module.h"
24 #include "llvm/Support/MemoryBuffer.h"
25 #include "llvm/Support/raw_ostream.h"
26
27 namespace llvm {
28
29 /// \brief Class to accumulate and hold information about a callee.
30 struct CalleeInfo {
31 /// The static number of callsites calling corresponding function.
32 unsigned CallsiteCount;
33 /// The cumulative profile count of calls to corresponding function
34 /// (if using PGO, otherwise 0).
35 uint64_t ProfileCount;
36 CalleeInfo() : CallsiteCount(0), ProfileCount(0) {}
37 CalleeInfo(unsigned CallsiteCount, uint64_t ProfileCount)
38 : CallsiteCount(CallsiteCount), ProfileCount(ProfileCount) {}
39 CalleeInfo &operator+=(uint64_t RHSProfileCount) {
40 CallsiteCount++;
41 ProfileCount += RHSProfileCount;
42 return *this;
43 }
44 };
45
46 /// \brief Function and variable summary information to aid decisions and
47 /// implementation of importing.
48 ///
49 /// This is a separate class from GlobalValueInfo to enable lazy reading of this
50 /// summary information from the combined index file during imporing.
51 class GlobalValueSummary {
52 public:
53 /// \brief Sububclass discriminator (for dyn_cast<> et al.)
54 enum SummaryKind { FunctionKind, GlobalVarKind };
55
56 private:
57 /// Kind of summary for use in dyn_cast<> et al.
58 SummaryKind Kind;
59
60 /// \brief Path of module IR containing value's definition, used to locate
61 /// module during importing.
62 ///
63 /// This is only used during parsing of the combined index, or when
64 /// parsing the per-module index for creation of the combined function index,
65 /// not during writing of the per-module index which doesn't contain a
66 /// module path string table.
67 StringRef ModulePath;
68
69 /// \brief The linkage type of the associated global value.
70 ///
71 /// One use is to flag values that have local linkage types and need to
72 /// have module identifier appended before placing into the combined
73 /// index, to disambiguate from other values with the same name.
74 /// In the future this will be used to update and optimize linkage
75 /// types based on global summary-based analysis.
76 GlobalValue::LinkageTypes Linkage;
77
78 /// List of GUIDs of values referenced by this global value's definition
79 /// (either by the initializer of a global variable, or referenced
80 /// from within a function). This does not include functions called, which
81 /// are listed in the derived FunctionSummary object.
82 std::vector RefEdgeList;
83
84 protected:
85 /// GlobalValueSummary constructor.
86 GlobalValueSummary(SummaryKind K, GlobalValue::LinkageTypes Linkage)
87 : Kind(K), Linkage(Linkage) {}
88
89 public:
90 virtual ~GlobalValueSummary() = default;
91
92 /// Which kind of summary subclass this is.
93 SummaryKind getSummaryKind() const { return Kind; }
94
95 /// Set the path to the module containing this function, for use in
96 /// the combined index.
97 void setModulePath(StringRef ModPath) { ModulePath = ModPath; }
98
99 /// Get the path to the module containing this function.
100 StringRef modulePath() const { return ModulePath; }
101
102 /// Return linkage type recorded for this global value.
103 GlobalValue::LinkageTypes linkage() const { return Linkage; }
104
105 /// Record a reference from this global value to the global value identified
106 /// by \p RefGUID.
107 void addRefEdge(uint64_t RefGUID) { RefEdgeList.push_back(RefGUID); }
108
109 /// Record a reference from this global value to each global value identified
110 /// in \p RefEdges.
111 void addRefEdges(DenseSet &RefEdges) {
112 for (auto &RI : RefEdges)
113 addRefEdge(RI);
114 }
115
116 /// Return the list of GUIDs referenced by this global value definition.
117 std::vector &refs() { return RefEdgeList; }
118 const std::vector &refs() const { return RefEdgeList; }
119 };
120
121 /// \brief Function summary information to aid decisions and implementation of
122 /// importing.
123 class FunctionSummary : public GlobalValueSummary {
124 public:
125 /// call edge pair.
126 typedef std::pair EdgeTy;
127
128 private:
129 /// Number of instructions (ignoring debug instructions, e.g.) computed
130 /// during the initial compile step when the function index is first built.
131 unsigned InstCount;
132
133 /// List of call edge pairs from this function.
134 std::vector CallGraphEdgeList;
135
136 public:
137 /// Summary constructors.
138 FunctionSummary(GlobalValue::LinkageTypes Linkage, unsigned NumInsts)
139 : GlobalValueSummary(FunctionKind, Linkage), InstCount(NumInsts) {}
140
141 /// Check if this is a function summary.
142 static bool classof(const GlobalValueSummary *GVS) {
143 return GVS->getSummaryKind() == FunctionKind;
144 }
145
146 /// Get the instruction count recorded for this function.
147 unsigned instCount() const { return InstCount; }
148
149 /// Record a call graph edge from this function to the function identified
150 /// by \p CalleeGUID, with \p CalleeInfo including the cumulative profile
151 /// count (across all calls from this function) or 0 if no PGO.
152 void addCallGraphEdge(uint64_t CalleeGUID, CalleeInfo Info) {
153 CallGraphEdgeList.push_back(std::make_pair(CalleeGUID, Info));
154 }
155
156 /// Record a call graph edge from this function to each function recorded
157 /// in \p CallGraphEdges.
158 void addCallGraphEdges(DenseMap &CallGraphEdges) {
159 for (auto &EI : CallGraphEdges)
160 addCallGraphEdge(EI.first, EI.second);
161 }
162
163 /// Return the list of pairs.
164 std::vector &edges() { return CallGraphEdgeList; }
165 const std::vector &edges() const { return CallGraphEdgeList; }
166 };
167
168 /// \brief Global variable summary information to aid decisions and
169 /// implementation of importing.
170 ///
171 /// Currently this doesn't add anything to the base \p GlobalValueSummary,
172 /// but is a placeholder as additional info may be added to the summary
173 /// for variables.
174 class GlobalVarSummary : public GlobalValueSummary {
175
176 public:
177 /// Summary constructors.
178 GlobalVarSummary(GlobalValue::LinkageTypes Linkage)
179 : GlobalValueSummary(GlobalVarKind, Linkage) {}
180
181 /// Check if this is a global variable summary.
182 static bool classof(const GlobalValueSummary *GVS) {
183 return GVS->getSummaryKind() == GlobalVarKind;
184 }
185 };
186
187 /// \brief Class to hold pointer to summary object and information required
188 /// for parsing or writing it.
189 class GlobalValueInfo {
190 private:
191 /// Summary information used to help make ThinLTO importing decisions.
192 std::unique_ptr Summary;
193
194 /// \brief The bitcode offset corresponding to either an associated
195 /// function's function body record, or to an associated summary record,
196 /// depending on whether this is a per-module or combined index.
197 ///
198 /// This bitcode offset is written to or read from the associated
199 /// \a ValueSymbolTable entry for a function.
200 /// For the per-module index this holds the bitcode offset of a
201 /// function's body record within bitcode module block in its module,
202 /// although this field is currently only used when writing the VST
203 /// (it is set to 0 and also unused when this is a global variable).
204 /// For the combined index this holds the offset of the corresponding
205 /// summary record, to enable associating the combined index
206 /// VST records with the summary records.
207 uint64_t BitcodeIndex;
208
209 public:
210 GlobalValueInfo(uint64_t Offset = 0,
211 std::unique_ptr Summary = nullptr)
212 : Summary(std::move(Summary)), BitcodeIndex(Offset) {}
213
214 /// Record the summary information parsed out of the summary block during
215 /// parsing or combined index creation.
216 void setSummary(std::unique_ptr GVSummary) {
217 Summary = std::move(GVSummary);
218 }
219
220 /// Get the summary recorded for this global value.
221 GlobalValueSummary *summary() const { return Summary.get(); }
222
223 /// Get the bitcode index recorded for this value symbol table entry.
224 uint64_t bitcodeIndex() const { return BitcodeIndex; }
225
226 /// Set the bitcode index recorded for this value symbol table entry.
227 void setBitcodeIndex(uint64_t Offset) { BitcodeIndex = Offset; }
228 };
229
230 /// List of global value info structures for a particular value held
231 /// in the GlobalValueMap. Requires a vector in the case of multiple
232 /// COMDAT values of the same name.
233 typedef std::vector> GlobalValueInfoList;
234
235 /// Map from global value GUID to corresponding info structures.
236 /// Use a std::map rather than a DenseMap since it will likely incur
237 /// less overhead, as the value type is not very small and the size
238 /// of the map is unknown, resulting in inefficiencies due to repeated
239 /// insertions and resizing.
240 typedef std::map GlobalValueInfoMapTy;
241
242 /// Type used for iterating through the global value info map.
243 typedef GlobalValueInfoMapTy::const_iterator const_globalvalueinfo_iterator;
244 typedef GlobalValueInfoMapTy::iterator globalvalueinfo_iterator;
245
246 /// String table to hold/own module path strings, which additionally holds the
247 /// module ID assigned to each module during the plugin step. The StringMap
248 /// makes a copy of and owns inserted strings.
249 typedef StringMap ModulePathStringTableTy;
250
251 /// Class to hold module path string table and global value map,
252 /// and encapsulate methods for operating on them.
253 /// FIXME: Rename this and other uses of Function.*Index to something
254 /// that reflects the now-expanded scope of the summary beyond just functions.
255 class FunctionInfoIndex {
256 private:
257 /// Map from value name to list of information instances for values of that
258 /// name (may be duplicates in the COMDAT case, e.g.).
259 GlobalValueInfoMapTy GlobalValueMap;
260
261 /// Holds strings for combined index, mapping to the corresponding module ID.
262 ModulePathStringTableTy ModulePathStringTable;
263
264 public:
265 FunctionInfoIndex() = default;
266
267 // Disable the copy constructor and assignment operators, so
268 // no unexpected copying/moving occurs.
269 FunctionInfoIndex(const FunctionInfoIndex &) = delete;
270 void operator=(const FunctionInfoIndex &) = delete;
271
272 globalvalueinfo_iterator begin() { return GlobalValueMap.begin(); }
273 const_globalvalueinfo_iterator begin() const {
274 return GlobalValueMap.begin();
275 }
276 globalvalueinfo_iterator end() { return GlobalValueMap.end(); }
277 const_globalvalueinfo_iterator end() const { return GlobalValueMap.end(); }
278
279 /// Get the list of global value info objects for a given value name.
280 const GlobalValueInfoList &getGlobalValueInfoList(StringRef FuncName) {
281 return GlobalValueMap[Function::getGUID(FuncName)];
282 }
283
284 /// Get the list of global value info objects for a given value name.
285 const const_globalvalueinfo_iterator
286 findGlobalValueInfoList(StringRef ValueName) const {
287 return GlobalValueMap.find(Function::getGUID(ValueName));
288 }
289
290 /// Get the list of global value info objects for a given value GUID.
291 const const_globalvalueinfo_iterator
292 findGlobalValueInfoList(uint64_t ValueGUID) const {
293 return GlobalValueMap.find(ValueGUID);
294 }
295
296 /// Add a global value info for a value of the given name.
297 void addGlobalValueInfo(StringRef ValueName,
298 std::unique_ptr Info) {
299 GlobalValueMap[Function::getGUID(ValueName)].push_back(std::move(Info));
300 }
301
302 /// Add a global value info for a value of the given GUID.
303 void addGlobalValueInfo(uint64_t ValueGUID,
304 std::unique_ptr Info) {
305 GlobalValueMap[ValueGUID].push_back(std::move(Info));
306 }
307
308 /// Iterator to allow writer to walk through table during emission.
309 iterator_range::const_iterator>
310 modPathStringEntries() const {
311 return llvm::make_range(ModulePathStringTable.begin(),
312 ModulePathStringTable.end());
313 }
314
315 /// Get the module ID recorded for the given module path.
316 uint64_t getModuleId(const StringRef ModPath) const {
317 return ModulePathStringTable.lookup(ModPath);
318 }
319
320 /// Add the given per-module index into this module index/summary,
321 /// assigning it the given module ID. Each module merged in should have
322 /// a unique ID, necessary for consistent renaming of promoted
323 /// static (local) variables.
324 void mergeFrom(std::unique_ptr Other,
325 uint64_t NextModuleId);
326
327 /// Convenience method for creating a promoted global name
328 /// for the given value name of a local, and its original module's ID.
329 static std::string getGlobalNameForLocal(StringRef Name, uint64_t ModId) {
330 SmallString<256> NewName(Name);
331 NewName += ".llvm.";
332 raw_svector_ostream(NewName) << ModId;
333 return NewName.str();
334 }
335
336 /// Add a new module path, mapped to the given module Id, and return StringRef
337 /// owned by string table map.
338 StringRef addModulePath(StringRef ModPath, uint64_t ModId) {
339 return ModulePathStringTable.insert(std::make_pair(ModPath, ModId))
340 .first->first();
341 }
342
343 /// Check if the given Module has any functions available for exporting
344 /// in the index. We consider any module present in the ModulePathStringTable
345 /// to have exported functions.
346 bool hasExportedFunctions(const Module &M) const {
347 return ModulePathStringTable.count(M.getModuleIdentifier());
348 }
349
350 /// Remove entries in the GlobalValueMap that have empty summaries due to the
351 /// eager nature of map entry creation during VST parsing. These would
352 /// also be suppressed during combined index generation in mergeFrom(),
353 /// but if there was only one module or this was the first module we might
354 /// not invoke mergeFrom.
355 void removeEmptySummaryEntries();
356 };
357
358 } // End llvm namespace
359
360 #endif
2626 #include
2727
2828 namespace llvm {
29 class ModuleSummaryIndex;
29 class FunctionInfoIndex;
3030 class LLVMContext;
3131 class TargetMachine;
3232
176176 */
177177
178178 /**
179 * Produce the combined summary index from all the bitcode files:
179 * Produce the combined function index from all the bitcode files:
180180 * "thin-link".
181181 */
182 std::unique_ptr<ModuleSummaryIndex> linkCombinedIndex();
182 std::unique_ptr<FunctionInfoIndex> linkCombinedIndex();
183183
184184 /**
185185 * Perform promotion and renaming of exported internal functions.
186186 */
187 void promote(Module &Module, ModuleSummaryIndex &Index);
187 void promote(Module &Module, FunctionInfoIndex &Index);
188188
189189 /**
190190 * Perform cross-module importing for the module identified by
191191 * ModuleIdentifier.
192192 */
193 void crossModuleImport(Module &Module, ModuleSummaryIndex &Index);
193 void crossModuleImport(Module &Module, FunctionInfoIndex &Index);
194194
195195 /**
196196 * Perform post-importing ThinLTO optimizations.
99 #ifndef LLVM_LINKER_LINKER_H
1010 #define LLVM_LINKER_LINKER_H
1111
12 #include "llvm/IR/ModuleSummaryIndex.h"
12 #include "llvm/IR/FunctionInfo.h"
1313 #include "llvm/Linker/IRMover.h"
1414
1515 namespace llvm {
3838 ///
3939 /// Passing OverrideSymbols as true will have symbols from Src
4040 /// shadow those in the Dest.
41 /// For ThinLTO function importing/exporting the \p ModuleSummaryIndex
41 /// For ThinLTO function importing/exporting the \p FunctionInfoIndex
4242 /// is passed. If \p FunctionsToImport is provided, only the functions that
4343 /// are part of the set will be imported from the source module.
4444 /// The \p ValIDToTempMDMap is populated by the linker when function
4646 ///
4747 /// Returns true on error.
4848 bool linkInModule(std::unique_ptr Src, unsigned Flags = Flags::None,
49 const ModuleSummaryIndex *Index = nullptr,
49 const FunctionInfoIndex *Index = nullptr,
5050 DenseSet *FunctionsToImport = nullptr,
5151 DenseMap *ValIDToTempMDMap = nullptr);
5252
4141 ID_Archive,
4242 ID_MachOUniversalBinary,
4343 ID_COFFImportFile,
44 ID_IR, // LLVM IR
45 ID_ModuleSummaryIndex, // Module summary index
44 ID_IR, // LLVM IR
45 ID_FunctionIndex, // Function summary index
4646
4747 // Object and children.
4848 ID_StartObjects,
122122 return TypeID == ID_IR;
123123 }
124124
125 bool isModuleSummaryIndex() const { return TypeID == ID_ModuleSummaryIndex; }
125 bool isFunctionIndex() const { return TypeID == ID_FunctionIndex; }
126126
127127 bool isLittleEndian() const {
128128 return !(TypeID == ID_ELF32B || TypeID == ID_ELF64B ||
0 //===- FunctionIndexObjectFile.h - Function index file implementation -----===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file declares the FunctionIndexObjectFile template class.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #ifndef LLVM_OBJECT_FUNCTIONINDEXOBJECTFILE_H
14 #define LLVM_OBJECT_FUNCTIONINDEXOBJECTFILE_H
15
16 #include "llvm/IR/DiagnosticInfo.h"
17 #include "llvm/Object/SymbolicFile.h"
18
19 namespace llvm {
20 class FunctionInfoIndex;
21 class Module;
22
23 namespace object {
24 class ObjectFile;
25
26 /// This class is used to read just the function summary index related
27 /// sections out of the given object (which may contain a single module's
28 /// bitcode or be a combined index bitcode file). It builds a FunctionInfoIndex
29 /// object.
30 class FunctionIndexObjectFile : public SymbolicFile {
31 std::unique_ptr Index;
32
33 public:
34 FunctionIndexObjectFile(MemoryBufferRef Object,
35 std::unique_ptr I);
36 ~FunctionIndexObjectFile() override;
37
38 // TODO: Walk through FunctionMap entries for function symbols.
39 // However, currently these interfaces are not used by any consumers.
40 void moveSymbolNext(DataRefImpl &Symb) const override {
41 llvm_unreachable("not implemented");
42 }
43 std::error_code printSymbolName(raw_ostream &OS,
44 DataRefImpl Symb) const override {
45 llvm_unreachable("not implemented");
46 return std::error_code();
47 }
48 uint32_t getSymbolFlags(DataRefImpl Symb) const override {
49 llvm_unreachable("not implemented");
50 return 0;
51 }
52 basic_symbol_iterator symbol_begin_impl() const override {
53 llvm_unreachable("not implemented");
54 return basic_symbol_iterator(BasicSymbolRef());
55 }
56 basic_symbol_iterator symbol_end_impl() const override {
57 llvm_unreachable("not implemented");
58 return basic_symbol_iterator(BasicSymbolRef());
59 }
60
61 const FunctionInfoIndex &getIndex() const {
62 return const_cast(this)->getIndex();
63 }
64 FunctionInfoIndex &getIndex() { return *Index; }
65 std::unique_ptr takeIndex();
66
67 static inline bool classof(const Binary *v) { return v->isFunctionIndex(); }
68
69 /// \brief Finds and returns bitcode embedded in the given object file, or an
70 /// error code if not found.
71 static ErrorOr findBitcodeInObject(const ObjectFile &Obj);
72
73 /// \brief Finds and returns bitcode in the given memory buffer (which may
74 /// be either a bitcode file or a native object file with embedded bitcode),
75 /// or an error code if not found.
76 static ErrorOr
77 findBitcodeInMemBuffer(MemoryBufferRef Object);
78
79 /// \brief Looks for summary sections in the given memory buffer,
80 /// returns true if found, else false.
81 static bool
82 hasGlobalValueSummaryInMemBuffer(MemoryBufferRef Object,
83 DiagnosticHandlerFunction DiagnosticHandler);
84
85 /// \brief Parse function index in the given memory buffer.
86 /// Return new FunctionIndexObjectFile instance containing parsed function
87 /// summary/index.
88 static ErrorOr>
89 create(MemoryBufferRef Object, DiagnosticHandlerFunction DiagnosticHandler,
90 bool IsLazy = false);
91
92 /// \brief Parse the function summary information for function with the
93 /// given name out of the given buffer. Parsed information is
94 /// stored on the index object saved in this object.
95 std::error_code
96 findFunctionSummaryInMemBuffer(MemoryBufferRef Object,
97 DiagnosticHandlerFunction DiagnosticHandler,
98 StringRef FunctionName);
99 };
100 }
101
102 /// Parse the function index out of an IR file and return the function
103 /// index object if found, or nullptr if not.
104 ErrorOr>
105 getFunctionIndexForFile(StringRef Path,
106 DiagnosticHandlerFunction DiagnosticHandler);
107 }
108
109 #endif
+0
-112
include/llvm/Object/ModuleSummaryIndexObjectFile.h less more
None //===- ModuleSummaryIndexObjectFile.h - Summary index file implementation -=//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file declares the ModuleSummaryIndexObjectFile template class.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #ifndef LLVM_OBJECT_MODULESUMMARYINDEXOBJECTFILE_H
14 #define LLVM_OBJECT_MODULESUMMARYINDEXOBJECTFILE_H
15
16 #include "llvm/IR/DiagnosticInfo.h"
17 #include "llvm/Object/SymbolicFile.h"
18
19 namespace llvm {
20 class ModuleSummaryIndex;
21 class Module;
22
23 namespace object {
24 class ObjectFile;
25
26 /// This class is used to read just the module summary index related
27 /// sections out of the given object (which may contain a single module's
28 /// bitcode or be a combined index bitcode file). It builds a ModuleSummaryIndex
29 /// object.
30 class ModuleSummaryIndexObjectFile : public SymbolicFile {
31 std::unique_ptr Index;
32
33 public:
34 ModuleSummaryIndexObjectFile(MemoryBufferRef Object,
35 std::unique_ptr I);
36 ~ModuleSummaryIndexObjectFile() override;
37
38 // TODO: Walk through GlobalValueMap entries for symbols.
39 // However, currently these interfaces are not used by any consumers.
40 void moveSymbolNext(DataRefImpl &Symb) const override {
41 llvm_unreachable("not implemented");
42 }
43 std::error_code printSymbolName(raw_ostream &OS,
44 DataRefImpl Symb) const override {
45 llvm_unreachable("not implemented");
46 return std::error_code();
47 }
48 uint32_t getSymbolFlags(DataRefImpl Symb) const override {
49 llvm_unreachable("not implemented");
50 return 0;
51 }
52 basic_symbol_iterator symbol_begin_impl() const override {
53 llvm_unreachable("not implemented");
54 return basic_symbol_iterator(BasicSymbolRef());
55 }
56 basic_symbol_iterator symbol_end_impl() const override {
57 llvm_unreachable("not implemented");
58 return basic_symbol_iterator(BasicSymbolRef());
59 }
60
61 const ModuleSummaryIndex &getIndex() const {
62 return const_cast(this)->getIndex();
63 }
64 ModuleSummaryIndex &getIndex() { return *Index; }
65 std::unique_ptr takeIndex();
66
67 static inline bool classof(const Binary *v) {
68 return v->isModuleSummaryIndex();
69 }
70
71 /// \brief Finds and returns bitcode embedded in the given object file, or an
72 /// error code if not found.
73 static ErrorOr findBitcodeInObject(const ObjectFile &Obj);
74
75 /// \brief Finds and returns bitcode in the given memory buffer (which may
76 /// be either a bitcode file or a native object file with embedded bitcode),
77 /// or an error code if not found.
78 static ErrorOr
79 findBitcodeInMemBuffer(MemoryBufferRef Object);
80
81 /// \brief Looks for summary sections in the given memory buffer,
82 /// returns true if found, else false.
83 static bool
84 hasGlobalValueSummaryInMemBuffer(MemoryBufferRef Object,
85 DiagnosticHandlerFunction DiagnosticHandler);
86
87 /// \brief Parse module summary index in the given memory buffer.
88 /// Return new ModuleSummaryIndexObjectFile instance containing parsed module
89 /// summary/index.
90 static ErrorOr>
91 create(MemoryBufferRef Object, DiagnosticHandlerFunction DiagnosticHandler,
92 bool IsLazy = false);
93
94 /// \brief Parse the summary information for global value with the
95 /// given name out of the given buffer. Parsed information is
96 /// stored on the index object saved in this object.
97 std::error_code
98 findGlobalValueSummaryInMemBuffer(MemoryBufferRef Object,
99 DiagnosticHandlerFunction DiagnosticHandler,
100 StringRef ValueName);
101 };
102 }
103
104 /// Parse the module summary index out of an IR file and return the module
105 /// summary index object if found, or nullptr if not.
106 ErrorOr>
107 getModuleSummaryIndexForFile(StringRef Path,
108 DiagnosticHandlerFunction DiagnosticHandler);
109 }
110
111 #endif
1515 namespace llvm {
1616 class LLVMContext;
1717 class Module;
18 class ModuleSummaryIndex;
18 class FunctionInfoIndex;
1919
2020 /// The function importer is automatically importing function from other modules
2121 /// based on the provided summary informations.
2222 class FunctionImporter {
2323
2424 /// The summaries index used to trigger importing.
25 const ModuleSummaryIndex &Index;
25 const FunctionInfoIndex &Index;
2626
2727 /// Factory function to load a Module for a given identifier
2828 std::function(StringRef Identifier)> ModuleLoader;
3030 public:
3131 /// Create a Function Importer.
3232 FunctionImporter(
33 const ModuleSummaryIndex &Index,
33 const FunctionInfoIndex &Index,
3434 std::function(StringRef Identifier)> ModuleLoader)
3535 : Index(Index), ModuleLoader(ModuleLoader) {}
3636
1919 #include
2020
2121 namespace llvm {
22 class ModuleSummaryIndex;
22 class FunctionInfoIndex;
2323 class Pass;
2424 class TargetLibraryInfoImpl;
2525 class TargetMachine;
116116 /// added to the per-module passes.
117117 Pass *Inliner;
118118
119 /// The module summary index to use for function importing.
120 const ModuleSummaryIndex *ModuleSummary;
119 /// The function summary index to use for function importing.
120 const FunctionInfoIndex *FunctionIndex;
121121
122122 bool DisableTailCalls;
123123 bool DisableUnitAtATime;
2020
2121 namespace llvm {
2222
23 class ModuleSummaryIndex;
23 class FunctionInfoIndex;
2424 class ModulePass;
2525 class Pass;
2626 class Function;
8888
8989 //===----------------------------------------------------------------------===//
9090 /// This pass performs iterative function importing from other modules.
91 Pass *createFunctionImportPass(const ModuleSummaryIndex *Index = nullptr);
91 Pass *createFunctionImportPass(const FunctionInfoIndex *Index = nullptr);
9292
9393 //===----------------------------------------------------------------------===//
9494 /// createFunctionInliningPass - Return a new pass object that uses a heuristic
1515 #define LLVM_TRANSFORMS_UTILS_FUNCTIONIMPORTUTILS_H
1616
1717 #include "llvm/ADT/SetVector.h"
18 #include "llvm/IR/ModuleSummaryIndex.h"
18 #include "llvm/IR/FunctionInfo.h"
1919
2020 namespace llvm {
2121 class Module;
2626 /// The Module which we are exporting or importing functions from.
2727 Module &M;
2828
29 /// Module summary index passed in for function importing/exporting handling.
30 const ModuleSummaryIndex &ImportIndex;
29 /// Function index passed in for function importing/exporting handling.
30 const FunctionInfoIndex &ImportIndex;
3131
3232 /// Functions to import from this module, all other functions will be
3333 /// imported as declarations instead of definitions.
3434 DenseSet *FunctionsToImport;
3535
36 /// Set to true if the given ModuleSummaryIndex contains any functions
36 /// Set to true if the given FunctionInfoIndex contains any functions
3737 /// from this source module, in which case we must conservatively assume
3838 /// that any of its functions may be imported into another module
3939 /// as part of a different backend compilation process.
7575
7676 public:
7777 FunctionImportGlobalProcessing(
78 Module &M, const ModuleSummaryIndex &Index,
78 Module &M, const FunctionInfoIndex &Index,
7979 DenseSet *FunctionsToImport = nullptr)
8080 : M(M), ImportIndex(Index), FunctionsToImport(FunctionsToImport) {
81 // If we have a ModuleSummaryIndex but no function to import,
81 // If we have a FunctionInfoIndex but no function to import,
8282 // then this is the primary module being compiled in a ThinLTO
8383 // backend compilation, and we need to see if it has functions that
8484 // may be exported to another backend compilation.
9898
9999 /// Perform in-place global value handling on the given Module for
100100 /// exported local functions renamed and promoted for ThinLTO.
101 bool renameModuleForThinLTO(Module &M, const ModuleSummaryIndex &Index);
101 bool renameModuleForThinLTO(Module &M, const FunctionInfoIndex &Index);
102102
103103 } // End llvm namespace
104104
66 //
77 //===----------------------------------------------------------------------===//
88
9 #include "llvm/Bitcode/ReaderWriter.h"
910 #include "llvm/ADT/STLExtras.h"
1011 #include "llvm/ADT/SmallString.h"
1112 #include "llvm/ADT/SmallVector.h"
1213 #include "llvm/ADT/Triple.h"
1314 #include "llvm/Bitcode/BitstreamReader.h"
1415 #include "llvm/Bitcode/LLVMBitCodes.h"
15 #include "llvm/Bitcode/ReaderWriter.h"
1616 #include "llvm/IR/AutoUpgrade.h"
1717 #include "llvm/IR/Constants.h"
1818 #include "llvm/IR/DebugInfo.h"
2424 #include "llvm/IR/IntrinsicInst.h"
2525 #include "llvm/IR/LLVMContext.h"
2626 #include "llvm/IR/Module.h"
27 #include "llvm/IR/ModuleSummaryIndex.h"
2827 #include "llvm/IR/OperandTraits.h"
2928 #include "llvm/IR/Operator.h"
29 #include "llvm/IR/FunctionInfo.h"
3030 #include "llvm/IR/ValueHandle.h"
3131 #include "llvm/Support/DataStream.h"
3232 #include "llvm/Support/ManagedStatic.h"
412412
413413 /// Class to manage reading and parsing function summary index bitcode
414414 /// files/sections.
415 class ModuleSummaryIndexBitcodeReader {
415 class FunctionIndexBitcodeReader {
416416 DiagnosticHandlerFunction DiagnosticHandler;
417417
418418 /// Eventually points to the module index built during parsing.
419 ModuleSummaryIndex *TheIndex = nullptr;
419 FunctionInfoIndex *TheIndex = nullptr;
420420
421421 std::unique_ptr Buffer;
422422 std::unique_ptr StreamFile;
426426 ///
427427 /// If false, the summary section is fully parsed into the index during
428428 /// the initial parse. Otherwise, if true, the caller is expected to
429 /// invoke \a readGlobalValueSummary for each summary needed, and the summary
429 /// invoke \a readFunctionSummary for each summary needed, and the summary
430430 /// section is thus parsed lazily.
431431 bool IsLazy = false;
432432
451451 // ValueSymbolTable. It is used after the VST is parsed to convert
452452 // call graph edges read from the function summary from referencing
453453 // callees by their ValueId to using the GUID instead, which is how
454 // they are recorded in the summary index being built.
454 // they are recorded in the function index being built.
455455 DenseMap ValueIdToCallGraphGUIDMap;
456456
457457 /// Map to save the association between summary offset in the VST to the
473473 std::error_code error(BitcodeError E);
474474 std::error_code error(const Twine &Message);
475475
476 ModuleSummaryIndexBitcodeReader(
477 MemoryBuffer *Buffer, DiagnosticHandlerFunction DiagnosticHandler,
478 bool IsLazy = false, bool CheckGlobalValSummaryPresenceOnly = false);
479 ModuleSummaryIndexBitcodeReader(
480 DiagnosticHandlerFunction DiagnosticHandler, bool IsLazy = false,
481 bool CheckGlobalValSummaryPresenceOnly = false);
482 ~ModuleSummaryIndexBitcodeReader() { freeState(); }
476 FunctionIndexBitcodeReader(MemoryBuffer *Buffer,
477 DiagnosticHandlerFunction DiagnosticHandler,
478 bool IsLazy = false,
479 bool CheckGlobalValSummaryPresenceOnly = false);
480 FunctionIndexBitcodeReader(DiagnosticHandlerFunction DiagnosticHandler,
481 bool IsLazy = false,
482 bool CheckGlobalValSummaryPresenceOnly = false);
483 ~FunctionIndexBitcodeReader() { freeState(); }
483484
484485 void freeState();
485486
491492 /// \brief Main interface to parsing a bitcode buffer.
492493 /// \returns true if an error occurred.
493494 std::error_code parseSummaryIndexInto(std::unique_ptr Streamer,
494 ModuleSummaryIndex *I);
495 FunctionInfoIndex *I);
495496
496497 /// \brief Interface for parsing a summary lazily.
497 std::error_code
498 parseGlobalValueSummary(std::unique_ptr Streamer,
499 ModuleSummaryIndex *I, size_t SummaryOffset);
498 std::error_code parseFunctionSummary(std::unique_ptr Streamer,
499 FunctionInfoIndex *I,
500 size_t FunctionSummaryOffset);
500501
501502 private:
502503 std::error_code parseModule();
54125413 return std::error_code();
54135414 }
54145415
5415 std::error_code ModuleSummaryIndexBitcodeReader::error(BitcodeError E,
5416 const Twine &Message) {
5416 std::error_code FunctionIndexBitcodeReader::error(BitcodeError E,
5417 const Twine &Message) {
54175418 return ::error(DiagnosticHandler, make_error_code(E), Message);
54185419 }
54195420
5420 std::error_code ModuleSummaryIndexBitcodeReader::error(const Twine &Message) {
5421 std::error_code FunctionIndexBitcodeReader::error(const Twine &Message) {
54215422 return ::error(DiagnosticHandler,
54225423 make_error_code(BitcodeError::CorruptedBitcode), Message);
54235424 }
54245425
5425 std::error_code ModuleSummaryIndexBitcodeReader::error(BitcodeError E) {
5426 std::error_code FunctionIndexBitcodeReader::error(BitcodeError E) {
54265427 return ::error(DiagnosticHandler, make_error_code(E));
54275428 }
54285429
5429 ModuleSummaryIndexBitcodeReader::ModuleSummaryIndexBitcodeReader(
5430 FunctionIndexBitcodeReader::FunctionIndexBitcodeReader(
54305431 MemoryBuffer *Buffer, DiagnosticHandlerFunction DiagnosticHandler,
54315432 bool IsLazy, bool CheckGlobalValSummaryPresenceOnly)
54325433 : DiagnosticHandler(DiagnosticHandler), Buffer(Buffer), IsLazy(IsLazy),
54335434 CheckGlobalValSummaryPresenceOnly(CheckGlobalValSummaryPresenceOnly) {}
54345435
5435 ModuleSummaryIndexBitcodeReader::ModuleSummaryIndexBitcodeReader(
5436 FunctionIndexBitcodeReader::FunctionIndexBitcodeReader(
54365437 DiagnosticHandlerFunction DiagnosticHandler, bool IsLazy,
54375438 bool CheckGlobalValSummaryPresenceOnly)
54385439 : DiagnosticHandler(DiagnosticHandler), Buffer(nullptr), IsLazy(IsLazy),
54395440 CheckGlobalValSummaryPresenceOnly(CheckGlobalValSummaryPresenceOnly) {}
54405441
5441 void ModuleSummaryIndexBitcodeReader::freeState() { Buffer = nullptr; }
5442
5443 void ModuleSummaryIndexBitcodeReader::releaseBuffer() { Buffer.release(); }
5444
5445 uint64_t ModuleSummaryIndexBitcodeReader::getGUIDFromValueId(unsigned ValueId) {
5442 void FunctionIndexBitcodeReader::freeState() { Buffer = nullptr; }
5443
5444 void FunctionIndexBitcodeReader::releaseBuffer() { Buffer.release(); }
5445
5446 uint64_t FunctionIndexBitcodeReader::getGUIDFromValueId(unsigned ValueId) {
54465447 auto VGI = ValueIdToCallGraphGUIDMap.find(ValueId);
54475448 assert(VGI != ValueIdToCallGraphGUIDMap.end());
54485449 return VGI->second;
54495450 }
54505451
54515452 GlobalValueInfo *
5452 ModuleSummaryIndexBitcodeReader::getInfoFromSummaryOffset(uint64_t Offset) {
5453 FunctionIndexBitcodeReader::getInfoFromSummaryOffset(uint64_t Offset) {
54535454 auto I = SummaryOffsetToInfoMap.find(Offset);
54545455 assert(I != SummaryOffsetToInfoMap.end());
54555456 return I->second;
54615462 // from global value name to GlobalValueInfo. The global value info contains
54625463 // the function block's bitcode offset (if applicable), or the offset into the
54635464 // summary section for the combined index.
5464 std::error_code ModuleSummaryIndexBitcodeReader::parseValueSymbolTable(
5465 std::error_code FunctionIndexBitcodeReader::parseValueSymbolTable(
54655466 uint64_t Offset,
54665467 DenseMap &ValueIdToLinkageMap) {
54675468 assert(Offset > 0 && "Expected non-zero VST offset");
55625563 // either the parsed summary information (when parsing summaries
55635564 // eagerly), or just to the summary record's offset
55645565 // if parsing lazily (IsLazy).
5565 std::error_code ModuleSummaryIndexBitcodeReader::parseModule() {
5566 std::error_code FunctionIndexBitcodeReader::parseModule() {
55665567 if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
55675568 return error("Invalid record");
55685569
56995700
57005701 // Eagerly parse the entire summary block. This populates the GlobalValueSummary
57015702 // objects in the index.
5702 std::error_code ModuleSummaryIndexBitcodeReader::parseEntireSummary() {
5703 std::error_code FunctionIndexBitcodeReader::parseEntireSummary() {
57035704 if (Stream.EnterSubBlock(bitc::GLOBALVAL_SUMMARY_BLOCK_ID))
57045705 return error("Invalid record");
57055706
58815882
58825883 // Parse the module string table block into the Index.
58835884 // This populates the ModulePathStringTable map in the index.
5884 std::error_code ModuleSummaryIndexBitcodeReader::parseModuleStringTable() {
5885 std::error_code FunctionIndexBitcodeReader::parseModuleStringTable() {
58855886 if (Stream.EnterSubBlock(bitc::MODULE_STRTAB_BLOCK_ID))
58865887 return error("Invalid record");
58875888
59225923 }
59235924
59245925 // Parse the function info index from the bitcode streamer into the given index.
5925 std::error_code ModuleSummaryIndexBitcodeReader::parseSummaryIndexInto(
5926 std::unique_ptr Streamer, ModuleSummaryIndex *I) {
5926 std::error_code FunctionIndexBitcodeReader::parseSummaryIndexInto(
5927 std::unique_ptr Streamer, FunctionInfoIndex *I) {
59275928 TheIndex = I;
59285929
59295930 if (std::error_code EC = initStream(std::move(Streamer)))
59575958 }
59585959 }
59595960
5960 // Parse the summary information at the given offset in the buffer into
5961 // the index. Used to support lazy parsing of summaries from the
5961 // Parse the function information at the given offset in the buffer into
5962 // the index. Used to support lazy parsing of function summaries from the
59625963 // combined index during importing.
59635964 // TODO: This function is not yet complete as it won't have a consumer
59645965 // until ThinLTO function importing is added.
5965 std::error_code ModuleSummaryIndexBitcodeReader::parseGlobalValueSummary(
5966 std::unique_ptr Streamer, ModuleSummaryIndex *I,
5967 size_t SummaryOffset) {
5966 std::error_code FunctionIndexBitcodeReader::parseFunctionSummary(
5967 std::unique_ptr Streamer, FunctionInfoIndex *I,
5968 size_t FunctionSummaryOffset) {
59685969 TheIndex = I;
59695970
59705971 if (std::error_code EC = initStream(std::move(Streamer)))
59745975 if (!hasValidBitcodeHeader(Stream))
59755976 return error("Invalid bitcode signature");
59765977
5977 Stream.JumpToBit(SummaryOffset);
5978 Stream.JumpToBit(FunctionSummaryOffset);
59785979
59795980 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
59805981
60006001 return std::error_code();
60016002 }
60026003
6003 std::error_code ModuleSummaryIndexBitcodeReader::initStream(
6004 std::unique_ptr Streamer) {
6004 std::error_code
6005 FunctionIndexBitcodeReader::initStream(std::unique_ptr Streamer) {
60056006 if (Streamer)
60066007 return initLazyStream(std::move(Streamer));
60076008 return initStreamFromBuffer();
60086009 }
60096010
6010 std::error_code ModuleSummaryIndexBitcodeReader::initStreamFromBuffer() {
6011 std::error_code FunctionIndexBitcodeReader::initStreamFromBuffer() {
60116012 const unsigned char *BufPtr = (const unsigned char *)Buffer->getBufferStart();
60126013 const unsigned char *BufEnd = BufPtr + Buffer->getBufferSize();
60136014
60266027 return std::error_code();
60276028 }
60286029
6029 std::error_code ModuleSummaryIndexBitcodeReader::initLazyStream(
6030 std::error_code FunctionIndexBitcodeReader::initLazyStream(
60306031 std::unique_ptr Streamer) {
60316032 // Check and strip off the bitcode wrapper; BitstreamReader expects never to
60326033 // see it.
61856186 // the index. Otherwise skip the function summary section, and only create
61866187 // an index object with a map from function name to function summary offset.
61876188 // The index is used to perform lazy function summary reading later.
6188 ErrorOr>
6189 llvm::getModuleSummaryIndex(MemoryBufferRef Buffer,
6190 DiagnosticHandlerFunction DiagnosticHandler,
6191 bool IsLazy) {
6189 ErrorOr>
6190 llvm::getFunctionInfoIndex(MemoryBufferRef Buffer,
6191 DiagnosticHandlerFunction DiagnosticHandler,
6192 bool IsLazy) {
61926193 std::unique_ptr Buf = MemoryBuffer::getMemBuffer(Buffer, false);
6193 ModuleSummaryIndexBitcodeReader R(Buf.get(), DiagnosticHandler, IsLazy);
6194
6195 auto Index = llvm::make_unique();
6194 FunctionIndexBitcodeReader R(Buf.get(), DiagnosticHandler, IsLazy);
6195
6196 auto Index = llvm::make_unique();
61966197
61976198 auto cleanupOnError = [&](std::error_code EC) {
61986199 R.releaseBuffer(); // Never take ownership on error.
62026203 if (std::error_code EC = R.parseSummaryIndexInto(nullptr, Index.get()))
62036204 return cleanupOnError(EC);
62046205
6205 Buf.release(); // The ModuleSummaryIndexBitcodeReader owns it now.
6206 Buf.release(); // The FunctionIndexBitcodeReader owns it now.
62066207 return std::move(Index);
62076208 }
62086209
62106211 bool llvm::hasGlobalValueSummary(MemoryBufferRef Buffer,
62116212 DiagnosticHandlerFunction DiagnosticHandler) {
62126213 std::unique_ptr Buf = MemoryBuffer::getMemBuffer(Buffer, false);
6213 ModuleSummaryIndexBitcodeReader R(Buf.get(), DiagnosticHandler, false, true);
6214 FunctionIndexBitcodeReader R(Buf.get(), DiagnosticHandler, false, true);
62146215
62156216 auto cleanupOnError = [&](std::error_code EC) {
62166217 R.releaseBuffer(); // Never take ownership on error.
62206221 if (std::error_code EC = R.parseSummaryIndexInto(nullptr, nullptr))
62216222 return cleanupOnError(EC);
62226223
6223 Buf.release(); // The ModuleSummaryIndexBitcodeReader owns it now.
6224 Buf.release(); // The FunctionIndexBitcodeReader owns it now.
62246225 return R.foundGlobalValSummary();
62256226 }
62266227
6227 // This method supports lazy reading of summary data from the combined
6228 // This method supports lazy reading of function summary data from the combined
62286229 // index during ThinLTO function importing. When reading the combined index
6229 // file, getModuleSummaryIndex is first invoked with IsLazy=true.
6230 // Then this method is called for each value considered for importing,
6231 // to parse the summary information for the given value name into
6230 // file, getFunctionInfoIndex is first invoked with IsLazy=true.
6231 // Then this method is called for each function considered for importing,
6232 // to parse the summary information for the given function name into
62326233 // the index.
6233 std::error_code llvm::readGlobalValueSummary(
6234 std::error_code llvm::readFunctionSummary(
62346235 MemoryBufferRef Buffer, DiagnosticHandlerFunction DiagnosticHandler,
6235 StringRef ValueName, std::unique_ptrIndex> Index) {
6236 StringRef FunctionName, std::unique_ptrIndex> Index) {
62366237 std::unique_ptr Buf = MemoryBuffer::getMemBuffer(Buffer, false);
6237 ModuleSummaryIndexBitcodeReader R(Buf.get(), DiagnosticHandler);
6238 FunctionIndexBitcodeReader R(Buf.get(), DiagnosticHandler);
62386239
62396240 auto cleanupOnError = [&](std::error_code EC) {
62406241 R.releaseBuffer(); // Never take ownership on error.
62416242 return EC;
62426243 };
62436244
6244 // Lookup the given value name in the GlobalValueMap, which may
6245 // contain a list of global value infos in the case of a COMDAT. Walk through
6246 // and parse each summary info at the summary offset
6245 // Lookup the given function name in the FunctionMap, which may
6246 // contain a list of function infos in the case of a COMDAT. Walk through
6247 // and parse each function summary info at the function summary offset
62476248 // recorded when parsing the value symbol table.
6248 for (const auto &FI : Index->getGlobalValueInfoList(ValueName)) {
6249 size_t SummaryOffset = FI->bitcodeIndex();
6249 for (const auto &FI : Index->getGlobalValueInfoList(FunctionName)) {
6250 size_t FunctionSummaryOffset = FI->bitcodeIndex();
62506251 if (std::error_code EC =
6251 R.parseGlobalValueSummary(nullptr, Index.get(), SummaryOffset))
6252 R.parseFunctionSummary(nullptr, Index.get(), FunctionSummaryOffset))
62526253 return cleanupOnError(EC);
62536254 }
62546255
6255 Buf.release(); // The ModuleSummaryIndexBitcodeReader owns it now.
6256 Buf.release(); // The FunctionIndexBitcodeReader owns it now.
62566257 return std::error_code();
62576258 }
22442244 }
22452245
22462246 /// Emit names for globals/functions etc. The VSTOffsetPlaceholder,
2247 /// BitcodeStartBit and ModuleSummaryIndex are only passed for the module-level
2247 /// BitcodeStartBit and FunctionIndex are only passed for the module-level
22482248 /// VST, where we are including a function bitcode index and need to
22492249 /// backpatch the VST forward declaration record.
22502250 static void WriteValueSymbolTable(
23462346
23472347 // Save the word offset of the function (from the start of the
23482348 // actual bitcode written to the stream).
2349 assert(FunctionIndex->count(F) == 1);
23492350 uint64_t BitcodeIndex =
23502351 (*FunctionIndex)[F]->bitcodeIndex() - BitcodeStartBit;
23512352 assert((BitcodeIndex & 31) == 0 && "function block not 32-bit aligned");
23782379 /// Emit function names and summary offsets for the combined index
23792380 /// used by ThinLTO.
23802381 static void
2381 WriteCombinedValueSymbolTable(const ModuleSummaryIndex &Index,
2382 WriteCombinedValueSymbolTable(const FunctionInfoIndex &Index,
23822383 BitstreamWriter &Stream,
23832384 std::map &GUIDToValueIdMap,
23842385 uint64_t VSTOffsetPlaceholder) {
28082809
28092810 /// Write the module path strings, currently only used when generating
28102811 /// a combined index file.
2811 static void WriteModStrings(const ModuleSummaryIndex &I,
2812 static void WriteModStrings(const FunctionInfoIndex &I,
28122813 BitstreamWriter &Stream) {
28132814 Stream.EnterSubblock(bitc::MODULE_STRTAB_BLOCK_ID, 3);
28142815
30093010 Stream.ExitBlock();
30103011 }
30113012
3012 /// Emit the combined summary section into the combined index file.
3013 /// Emit the combined function summary section into the combined index
3014 /// file.
30133015 static void WriteCombinedGlobalValueSummary(
3014 const ModuleSummaryIndex &I, BitstreamWriter &Stream,
3016 const FunctionInfoIndex &I, BitstreamWriter &Stream,
30153017 std::map &GUIDToValueIdMap, unsigned GlobalValueId) {
30163018 Stream.EnterSubblock(bitc::GLOBALVAL_SUMMARY_BLOCK_ID, 3);
30173019
33513353 // Write the specified module summary index to the given raw output stream,
33523354 // where it will be written in a new bitcode block. This is used when
33533355 // writing the combined index file for ThinLTO.
3354 void llvm::WriteIndexToFile(const ModuleSummaryIndex &Index, raw_ostream &Out) {
3356 void llvm::WriteIndexToFile(const FunctionInfoIndex &Index, raw_ostream &Out) {
33553357 SmallVector Buffer;
33563358 Buffer.reserve(256 * 1024);
33573359
1818 using namespace llvm;
1919
2020 PreservedAnalyses BitcodeWriterPass::run(Module &M) {
21 WriteBitcodeToFile(&M, OS, ShouldPreserveUseListOrder, EmitSummaryIndex);
21 WriteBitcodeToFile(&M, OS, ShouldPreserveUseListOrder, EmitFunctionSummary);
2222 return PreservedAnalyses::all();
2323 }
2424
2626 class WriteBitcodePass : public ModulePass {
2727 raw_ostream &OS; // raw_ostream to print on
2828 bool ShouldPreserveUseListOrder;
29 bool EmitSummaryIndex;
29 bool EmitFunctionSummary;
3030
3131 public:
3232 static char ID; // Pass identification, replacement for typeid
3333 explicit WriteBitcodePass(raw_ostream &o, bool ShouldPreserveUseListOrder,
34 bool EmitSummaryIndex)
34 bool EmitFunctionSummary)
3535 : ModulePass(ID), OS(o),
3636 ShouldPreserveUseListOrder(ShouldPreserveUseListOrder),
37 EmitSummaryIndex(EmitSummaryIndex) {}
37 EmitFunctionSummary(EmitFunctionSummary) {}
3838
3939 const char *getPassName() const override { return "Bitcode Writer"; }
4040
4141 bool runOnModule(Module &M) override {
42 WriteBitcodeToFile(&M, OS, ShouldPreserveUseListOrder, EmitSummaryIndex);
42 WriteBitcodeToFile(&M, OS, ShouldPreserveUseListOrder,
43 EmitFunctionSummary);
4344 return false;
4445 }
4546 };
4950
5051 ModulePass *llvm::createBitcodeWriterPass(raw_ostream &Str,
5152 bool ShouldPreserveUseListOrder,
52 bool EmitSummaryIndex) {
53 bool EmitFunctionSummary) {
5354 return new WriteBitcodePass(Str, ShouldPreserveUseListOrder,
54 EmitSummaryIndex);
55 EmitFunctionSummary);
5556 }
3636 Mangler.cpp
3737 Metadata.cpp
3838 Module.cpp
39 ModuleSummaryIndex.cpp
4039 Operator.cpp
4140 Pass.cpp
4241 PassManager.cpp
4342 PassRegistry.cpp
4443 Statepoint.cpp
44 FunctionInfo.cpp
4545 Type.cpp
4646 TypeFinder.cpp
4747 Use.cpp
0 //===-- FunctionInfo.cpp - Function Info Index ----------------------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the module index and summary classes for the
10 // IR library.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/IR/FunctionInfo.h"
15 #include "llvm/ADT/StringMap.h"
16 using namespace llvm;
17
18 // Create the combined module index/summary from multiple
19 // per-module instances.
20 void FunctionInfoIndex::mergeFrom(std::unique_ptr Other,
21 uint64_t NextModuleId) {
22
23 StringRef ModPath;
24 for (auto &OtherGlobalValInfoLists : *Other) {
25 uint64_t ValueGUID = OtherGlobalValInfoLists.first;
26 GlobalValueInfoList &List = OtherGlobalValInfoLists.second;
27
28 // Assert that the value info list only has one entry, since we shouldn't
29 // have duplicate names within a single per-module index.
30 assert(List.size() == 1);
31 std::unique_ptr Info = std::move(List.front());
32
33 // Skip if there was no summary section.
34 if (!Info->summary())
35 continue;
36
37 // Add the module path string ref for this module if we haven't already
38 // saved a reference to it.
39 if (ModPath.empty())
40 ModPath = addModulePath(Info->summary()->modulePath(), NextModuleId);
41 else
42 assert(ModPath == Info->summary()->modulePath() &&
43 "Each module in the combined map should have a unique ID");
44
45 // Note the module path string ref was copied above and is still owned by
46 // the original per-module index. Reset it to the new module path
47 // string reference owned by the combined index.
48 Info->summary()->setModulePath(ModPath);
49
50 // Add new value info to existing list. There may be duplicates when
51 // combining GlobalValueMap entries, due to COMDAT values. Any local
52 // values were given unique global IDs.
53 addGlobalValueInfo(ValueGUID, std::move(Info));
54 }
55 }
56
57 void FunctionInfoIndex::removeEmptySummaryEntries() {
58 for (auto MI = begin(), MIE = end(); MI != MIE;) {
59 // Only expect this to be called on a per-module index, which has a single
60 // entry per value entry list.
61 assert(MI->second.size() == 1);
62 if (!MI->second[0]->summary())
63 MI = GlobalValueMap.erase(MI);
64 else
65 ++MI;
66 }
67 }
1313
1414 #include "llvm/LTO/ThinLTOCodeGenerator.h"
1515
16 #include "llvm/ADT/StringExtras.h"
1617 #include "llvm/ADT/Statistic.h"
17 #include "llvm/ADT/StringExtras.h"
1818 #include "llvm/Analysis/TargetLibraryInfo.h"
1919 #include "llvm/Analysis/TargetTransformInfo.h"
20 #include "llvm/Bitcode/ReaderWriter.h"
2021 #include "llvm/Bitcode/BitcodeWriterPass.h"
21 #include "llvm/Bitcode/ReaderWriter.h"
2222 #include "llvm/ExecutionEngine/ObjectMemoryBuffer.h"
23 #include "llvm/IR/LLVMContext.h"
2324 #include "llvm/IR/DiagnosticPrinter.h"
24 #include "llvm/IR/LLVMContext.h"
2525 #include "llvm/IR/LegacyPassManager.h"
2626 #include "llvm/IR/Mangler.h"
2727 #include "llvm/IRReader/IRReader.h"
2828 #include "llvm/Linker/Linker.h"
2929 #include "llvm/MC/SubtargetFeature.h"
30 #include "llvm/Object/ModuleSummaryIndexObjectFile.h"
30 #include "llvm/Object/FunctionIndexObjectFile.h"
3131 #include "llvm/Support/SourceMgr.h"
3232 #include "llvm/Support/TargetRegistry.h"
3333 #include "llvm/Support/ThreadPool.h"
125125 }
126126 };
127127
128 static void promoteModule(Module &TheModule, const ModuleSummaryIndex &Index) {
128 static void promoteModule(Module &TheModule, const FunctionInfoIndex &Index) {
129129 if (renameModuleForThinLTO(TheModule, Index))
130130 report_fatal_error("renameModuleForThinLTO failed");
131131 }
132132
133133 static void crossImportIntoModule(Module &TheModule,
134 const ModuleSummaryIndex &Index,
134 const FunctionInfoIndex &Index,
135135 StringMap &ModuleMap) {
136136 ModuleLoader Loader(TheModule.getContext(), ModuleMap);
137137 FunctionImporter Importer(Index, Loader);
182182 }
183183
184184 static std::unique_ptr
185 ProcessThinLTOModule(Module &TheModule, const ModuleSummaryIndex &Index,
185 ProcessThinLTOModule(Module &TheModule, const FunctionInfoIndex &Index,
186186 StringMap &ModuleMap, TargetMachine &TM,
187187 ThinLTOCodeGenerator::CachingOptions CacheOptions,
188188 StringRef SaveTempsDir, unsigned count) {
276276 }
277277
278278 /**
279 * Produce the combined summary index from all the bitcode files:
279 * Produce the combined function index from all the bitcode files:
280280 * "thin-link".
281281 */
282 std::unique_ptr ThinLTOCodeGenerator::linkCombinedIndex() {
283 std::unique_ptr CombinedIndex;
282 std::unique_ptr ThinLTOCodeGenerator::linkCombinedIndex() {
283 std::unique_ptr CombinedIndex;
284284 uint64_t NextModuleId = 0;
285285 for (auto &ModuleBuffer : Modules) {
286 ErrorOr> ObjOrErr =
287 object::ModuleSummaryIndexObjectFile::create(ModuleBuffer,
288 diagnosticHandler, false);
286 ErrorOr> ObjOrErr =
287 object::FunctionIndexObjectFile::create(ModuleBuffer, diagnosticHandler,
288 false);
289289 if (std::error_code EC = ObjOrErr.getError()) {
290290 // FIXME diagnose
291 errs() << "error: can't create ModuleSummaryIndexObjectFile for buffer: "
291 errs() << "error: can't create FunctionIndexObjectFile for buffer: "
292292 << EC.message() << "\n";
293293 return nullptr;
294294 }
306306 * Perform promotion and renaming of exported internal functions.
307307 */
308308 void ThinLTOCodeGenerator::promote(Module &TheModule,
309 ModuleSummaryIndex &Index) {
309 FunctionInfoIndex &Index) {
310310 promoteModule(TheModule, Index);
311311 }
312312
314314 * Perform cross-module importing for the module identified by ModuleIdentifier.
315315 */
316316 void ThinLTOCodeGenerator::crossModuleImport(Module &TheModule,
317 ModuleSummaryIndex &Index) {
317 FunctionInfoIndex &Index) {
318318 auto ModuleMap = generateModuleMap(Modules);
319319 crossImportIntoModule(TheModule, Index, ModuleMap);
320320 }
3434 /// For symbol clashes, prefer those from Src.
3535 unsigned Flags;
3636
37 /// Module summary index passed into ModuleLinker for using in function
37 /// Function index passed into ModuleLinker for using in function
3838 /// importing/exporting handling.
39 const ModuleSummaryIndex *ImportIndex;
39 const FunctionInfoIndex *ImportIndex;
4040
4141 /// Functions to import from source module, all other functions are
4242 /// imported as declarations instead of definitions.
4343 DenseSet *FunctionsToImport;
4444
45 /// Set to true if the given ModuleSummaryIndex contains any functions
45 /// Set to true if the given FunctionInfoIndex contains any functions
4646 /// from this source module, in which case we must conservatively assume
4747 /// that any of its functions may be imported into another module
4848 /// as part of a different backend compilation process.
123123
124124 public:
125125 ModuleLinker(IRMover &Mover, std::unique_ptr SrcM, unsigned Flags,
126 const ModuleSummaryIndex *Index = nullptr,
126 const FunctionInfoIndex *Index = nullptr,
127127 DenseSet *FunctionsToImport = nullptr,
128128 DenseMap *ValIDToTempMDMap = nullptr)
129129 : Mover(Mover), SrcM(std::move(SrcM)), Flags(Flags), ImportIndex(Index),
130130 FunctionsToImport(FunctionsToImport),
131131 ValIDToTempMDMap(ValIDToTempMDMap) {
132132 assert((ImportIndex || !FunctionsToImport) &&
133 "Expect a ModuleSummaryIndex when importing");
134 // If we have a ModuleSummaryIndex but no function to import,
133 "Expect a FunctionInfoIndex when importing");
134 // If we have a FunctionInfoIndex but no function to import,
135135 // then this is the primary module being compiled in a ThinLTO
136136 // backend compilation, and we need to see if it has functions that
137137 // may be exported to another backend compilation.
548548 Linker::Linker(Module &M) : Mover(M) {}
549549
550550 bool Linker::linkInModule(std::unique_ptr Src, unsigned Flags,
551 const ModuleSummaryIndex *Index,
551 const FunctionInfoIndex *Index,
552552 DenseSet *FunctionsToImport,
553553 DenseMap *ValIDToTempMDMap) {
554554 ModuleLinker ModLinker(Mover, std::move(Src), Flags, Index, FunctionsToImport,
88 IRObjectFile.cpp
99 MachOObjectFile.cpp
1010 MachOUniversal.cpp
11 ModuleSummaryIndexObjectFile.cpp
1211 Object.cpp
1312 ObjectFile.cpp
1413 RecordStreamer.cpp
1514 SymbolicFile.cpp
1615 SymbolSize.cpp
16 FunctionIndexObjectFile.cpp
1717
1818 ADDITIONAL_HEADER_DIRS
1919 ${LLVM_MAIN_INCLUDE_DIR}/llvm/Object
0 //===- FunctionIndexObjectFile.cpp - Function index file implementation ---===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Part of the FunctionIndexObjectFile class implementation.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include "llvm/Object/FunctionIndexObjectFile.h"
14 #include "llvm/ADT/STLExtras.h"
15 #include "llvm/Bitcode/ReaderWriter.h"
16 #include "llvm/IR/FunctionInfo.h"
17 #include "llvm/MC/MCStreamer.h"
18 #include "llvm/Object/ObjectFile.h"
19 #include "llvm/Support/MemoryBuffer.h"
20 #include "llvm/Support/raw_ostream.h"
21 using namespace llvm;
22 using namespace object;
23
24 FunctionIndexObjectFile::FunctionIndexObjectFile(
25 MemoryBufferRef Object, std::unique_ptr I)
26 : SymbolicFile(Binary::ID_FunctionIndex, Object), Index(std::move(I)) {}
27
28 FunctionIndexObjectFile::~FunctionIndexObjectFile() {}
29
30 std::unique_ptr FunctionIndexObjectFile::takeIndex() {
31 return std::move(Index);
32 }
33
34 ErrorOr
35 FunctionIndexObjectFile::findBitcodeInObject(const ObjectFile &Obj) {
36 for (const SectionRef &Sec : Obj.sections()) {
37 if (Sec.isBitcode()) {
38 StringRef SecContents;
39 if (std::error_code EC = Sec.getContents(SecContents))
40 return EC;
41 return MemoryBufferRef(SecContents, Obj.getFileName());
42 }
43 }
44
45 return object_error::bitcode_section_not_found;
46 }
47
48 ErrorOr
49 FunctionIndexObjectFile::findBitcodeInMemBuffer(MemoryBufferRef Object) {
50 sys::fs::file_magic Type = sys::fs::identify_magic(Object.getBuffer());
51 switch (Type) {
52 case sys::fs::file_magic::bitcode:
53 return Object;
54 case sys::fs::file_magic::elf_relocatable:
55 case sys::fs::file_magic::macho_object:
56 case sys::fs::file_magic::coff_object: {
57 ErrorOr> ObjFile =
58 ObjectFile::createObjectFile(Object, Type);
59 if (!ObjFile)
60 return ObjFile.getError();
61 return findBitcodeInObject(*ObjFile->get());
62 }
63 default:
64 return object_error::invalid_file_type;
65 }
66 }
67
68 // Looks for module summary index in the given memory buffer.
69 // returns true if found, else false.
70 bool FunctionIndexObjectFile::hasGlobalValueSummaryInMemBuffer(
71 MemoryBufferRef Object, DiagnosticHandlerFunction DiagnosticHandler) {
72 ErrorOr BCOrErr = findBitcodeInMemBuffer(Object);
73 if (!BCOrErr)
74 return false;
75
76 return hasGlobalValueSummary(BCOrErr.get(), DiagnosticHandler);
77 }
78
79 // Parse function index in the given memory buffer.
80 // Return new FunctionIndexObjectFile instance containing parsed
81 // function summary/index.
82 ErrorOr>
83 FunctionIndexObjectFile::create(MemoryBufferRef Object,
84 DiagnosticHandlerFunction DiagnosticHandler,
85 bool IsLazy) {
86 std::unique_ptr Index;
87
88 ErrorOr BCOrErr = findBitcodeInMemBuffer(Object);
89 if (!BCOrErr)
90 return BCOrErr.getError();
91
92 ErrorOr> IOrErr = getFunctionInfoIndex(
93 BCOrErr.get(), DiagnosticHandler, IsLazy);
94
95 if (std::error_code EC = IOrErr.getError())
96 return EC;
97
98 Index = std::move(IOrErr.get());
99
100 return llvm::make_unique(Object, std::move(Index));
101 }
102
103 // Parse the function summary information for function with the
104 // given name out of the given buffer. Parsed information is
105 // stored on the index object saved in this object.
106 std::error_code FunctionIndexObjectFile::findFunctionSummaryInMemBuffer(
107 MemoryBufferRef Object, DiagnosticHandlerFunction DiagnosticHandler,
108 StringRef FunctionName) {
109 sys::fs::file_magic Type = sys::fs::identify_magic(Object.getBuffer());
110 switch (Type) {
111 case sys::fs::file_magic::bitcode: {
112 return readFunctionSummary(Object, DiagnosticHandler, FunctionName,
113 std::move(Index));
114 }
115 default:
116 return object_error::invalid_file_type;
117 }
118 }
119
120 // Parse the function index out of an IR file and return the function
121 // index object if found, or nullptr if not.
122 ErrorOr>
123 llvm::getFunctionIndexForFile(StringRef Path,
124 DiagnosticHandlerFunction DiagnosticHandler) {
125 ErrorOr> FileOrErr =
126 MemoryBuffer::getFileOrSTDIN(Path);
127 std::error_code EC = FileOrErr.getError();
128 if (EC)
129 return EC;
130 MemoryBufferRef BufferRef = (FileOrErr.get())->getMemBufferRef();
131 ErrorOr> ObjOrErr =
132 object::FunctionIndexObjectFile::create(BufferRef, DiagnosticHandler);
133 EC = ObjOrErr.getError();
134 if (EC)
135 return EC;
136
137 object::FunctionIndexObjectFile &Obj = **ObjOrErr;
138 return Obj.takeIndex();
139 }
+0
-142
lib/Object/ModuleSummaryIndexObjectFile.cpp less more
None //===- ModuleSummaryIndexObjectFile.cpp - Summary index file implementation ==//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Part of the ModuleSummaryIndexObjectFile class implementation.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include "llvm/Object/ModuleSummaryIndexObjectFile.h"
14 #include "llvm/ADT/STLExtras.h"
15 #include "llvm/Bitcode/ReaderWriter.h"
16 #include "llvm/IR/ModuleSummaryIndex.h"
17 #include "llvm/MC/MCStreamer.h"
18 #include "llvm/Object/ObjectFile.h"
19 #include "llvm/Support/MemoryBuffer.h"
20 #include "llvm/Support/raw_ostream.h"
21 using namespace llvm;
22 using namespace object;
23
24 ModuleSummaryIndexObjectFile::ModuleSummaryIndexObjectFile(
25 MemoryBufferRef Object, std::unique_ptr I)
26 : SymbolicFile(Binary::ID_ModuleSummaryIndex, Object), Index(std::move(I)) {
27 }
28
29 ModuleSummaryIndexObjectFile::~ModuleSummaryIndexObjectFile() {}
30
31 std::unique_ptr ModuleSummaryIndexObjectFile::takeIndex() {
32 return std::move(Index);
33 }
34
35 ErrorOr
36 ModuleSummaryIndexObjectFile::findBitcodeInObject(const ObjectFile &Obj) {
37 for (const SectionRef &Sec : Obj.sections()) {
38 if (Sec.isBitcode()) {
39 StringRef SecContents;
40 if (std::error_code EC = Sec.getContents(SecContents))
41 return EC;
42 return MemoryBufferRef(SecContents, Obj.getFileName());
43 }
44 }
45
46 return object_error::bitcode_section_not_found;
47 }
48
49 ErrorOr
50 ModuleSummaryIndexObjectFile::findBitcodeInMemBuffer(MemoryBufferRef Object) {
51 sys::fs::file_magic Type = sys::fs::identify_magic(Object.getBuffer());
52 switch (Type) {
53 case sys::fs::file_magic::bitcode:
54 return Object;
55 case sys::fs::file_magic::elf_relocatable:
56 case sys::fs::file_magic::macho_object:
57 case sys::fs::file_magic::coff_object: {
58 ErrorOr> ObjFile =
59 ObjectFile::createObjectFile(Object, Type);
60 if (!ObjFile)
61 return ObjFile.getError();
62 return findBitcodeInObject(*ObjFile->get());
63 }
64 default:
65 return object_error::invalid_file_type;
66 }
67 }
68
69 // Looks for module summary index in the given memory buffer.
70 // returns true if found, else false.
71 bool ModuleSummaryIndexObjectFile::hasGlobalValueSummaryInMemBuffer(
72 MemoryBufferRef Object, DiagnosticHandlerFunction DiagnosticHandler) {
73 ErrorOr BCOrErr = findBitcodeInMemBuffer(Object);
74 if (!BCOrErr)
75 return false;
76
77 return hasGlobalValueSummary(BCOrErr.get(), DiagnosticHandler);
78 }
79
80 // Parse module summary index in the given memory buffer.
81 // Return new ModuleSummaryIndexObjectFile instance containing parsed
82 // module summary/index.
83 ErrorOr>
84 ModuleSummaryIndexObjectFile::create(
85 MemoryBufferRef Object, DiagnosticHandlerFunction DiagnosticHandler,
86 bool IsLazy) {
87 std::unique_ptr Index;
88
89 ErrorOr BCOrErr = findBitcodeInMemBuffer(Object);
90 if (!BCOrErr)
91 return BCOrErr.getError();
92
93 ErrorOr> IOrErr =
94 getModuleSummaryIndex(BCOrErr.get(), DiagnosticHandler, IsLazy);
95
96 if (std::error_code EC = IOrErr.getError())
97 return EC;
98
99 Index = std::move(IOrErr.get());
100
101 return llvm::make_unique(Object,
102 std::move(Index));
103 }
104
105 // Parse the summary information for value with the
106 // given name out of the given buffer. Parsed information is
107 // stored on the index object saved in this object.
108 std::error_code ModuleSummaryIndexObjectFile::findGlobalValueSummaryInMemBuffer(
109 MemoryBufferRef Object, DiagnosticHandlerFunction DiagnosticHandler,
110 StringRef ValueName) {
111 sys::fs::file_magic Type = sys::fs::identify_magic(Object.getBuffer());
112 switch (Type) {
113 case sys::fs::file_magic::bitcode: {
114 return readGlobalValueSummary(Object, DiagnosticHandler, ValueName,
115 std::move(Index));
116 }
117 default:
118 return object_error::invalid_file_type;
119 }
120 }
121
122 // Parse the module summary index out of an IR file and return the summary
123 // index object if found, or nullptr if not.
124 ErrorOr> llvm::getModuleSummaryIndexForFile(
125 StringRef Path, DiagnosticHandlerFunction DiagnosticHandler) {
126 ErrorOr> FileOrErr =
127 MemoryBuffer::getFileOrSTDIN(Path);
128 std::error_code EC = FileOrErr.getError();
129 if (EC)
130 return EC;
131 MemoryBufferRef BufferRef = (FileOrErr.get())->getMemBufferRef();
132 ErrorOr> ObjOrErr =
133 object::ModuleSummaryIndexObjectFile::create(BufferRef,
134 DiagnosticHandler);
135 EC = ObjOrErr.getError();
136 if (EC)
137 return EC;
138
139 object::ModuleSummaryIndexObjectFile &Obj = **ObjOrErr;
140 return Obj.takeIndex();
141 }
1919 #include "llvm/IR/Module.h"
2020 #include "llvm/IRReader/IRReader.h"
2121 #include "llvm/Linker/Linker.h"
22 #include "llvm/Object/ModuleSummaryIndexObjectFile.h"
22 #include "llvm/Object/FunctionIndexObjectFile.h"
2323 #include "llvm/Support/CommandLine.h"
2424 #include "llvm/Support/Debug.h"
2525 #include "llvm/Support/SourceMgr.h"
110110 /// calls not already in the \p VisitedFunctions map. If any are
111111 /// found they are added to the \p Worklist for importing.
112112 static void findExternalCalls(
113 const Module &DestModule, Function &F, const ModuleSummaryIndex &Index,
113 const Module &DestModule, Function &F, const FunctionInfoIndex &Index,
114114 VisitedFunctionTrackerTy &VisitedFunctions, unsigned Threshold,
115115 SmallVectorImpl> &Worklist) {
116116 // We need to suffix internal function calls imported from other modules,
140140 if (CalledFunction->hasInternalLinkage()) {
141141 ImportedName = Renamed;
142142 }
143 // Compute the global identifier used in the summary index.
143 // Compute the global identifier used in the function index.
144144 auto CalledFunctionGlobalID = Function::getGlobalIdentifier(
145145 CalledFunction->getName(), CalledFunction->getLinkage(),
146146 CalledFunction->getParent()->getSourceFileName());
191191 GetImportList(Module &DestModule,
192192 SmallVectorImpl> &Worklist,
193193 VisitedFunctionTrackerTy &VisitedFunctions,
194 std::map>
195 &ModuleToFunctionsToImportMap,
196 const ModuleSummaryIndex &Index,
194 std::map> &
195 ModuleToFunctionsToImportMap,
196 const FunctionInfoIndex &Index,
197197 ModuleLazyLoaderCache &ModuleLoaderCache) {
198198 while (!Worklist.empty()) {
199199 StringRef CalledFunctionName;
373373 OS << '\n';
374374 }
375375
376 /// Parse the summary index out of an IR file and return the summary
376 /// Parse the function index out of an IR file and return the function
377377 /// index object if found, or nullptr if not.
378 static std::unique_ptr
379 getModuleSummaryIndexForFile(StringRef Path, std::string &Error,
380 DiagnosticHandlerFunction DiagnosticHandler) {
378 static std::unique_ptr
379 getFunctionIndexForFile(StringRef Path, std::string &Error,
380 DiagnosticHandlerFunction DiagnosticHandler) {
381381 std::unique_ptr Buffer;
382382 ErrorOr> BufferOrErr =
383383 MemoryBuffer::getFile(Path);
386386 return nullptr;
387387 }
388388 Buffer = std::move(BufferOrErr.get());
389 ErrorOr> ObjOrErr =
390 object::ModuleSummaryIndexObjectFile::create(Buffer->getMemBufferRef(),
391 DiagnosticHandler);
389 ErrorOr> ObjOrErr =
390 object::FunctionIndexObjectFile::create(Buffer->getMemBufferRef(),
391 DiagnosticHandler);
392392 if (std::error_code EC = ObjOrErr.getError()) {
393393 Error = EC.message();
394394 return nullptr;
399399 namespace {
400400 /// Pass that performs cross-module function import provided a summary file.
401401 class FunctionImportPass : public ModulePass {
402 /// Optional module summary index to use for importing, otherwise
402 /// Optional function summary index to use for importing, otherwise
403403 /// the summary-file option must be specified.
404 const ModuleSummaryIndex *Index;
404 const FunctionInfoIndex *Index;
405405
406406 public:
407407 /// Pass identification, replacement for typeid
412412 return "Function Importing";
413413 }
414414
415 explicit FunctionImportPass(const ModuleSummaryIndex *Index = nullptr)
415 explicit FunctionImportPass(const FunctionInfoIndex *Index = nullptr)
416416 : ModulePass(ID), Index(Index) {}
417417
418418 bool runOnModule(Module &M) override {
419419 if (SummaryFile.empty() && !Index)
420420 report_fatal_error("error: -function-import requires -summary-file or "
421421 "file from frontend\n");
422 std::unique_ptr<ModuleSummaryIndex> IndexPtr;
422 std::unique_ptr<FunctionInfoIndex> IndexPtr;
423423 if (!SummaryFile.empty()) {
424424 if (Index)
425425 report_fatal_error("error: -summary-file and index from frontend\n");
426426 std::string Error;
427 IndexPtr =
428 getModuleSummaryIndexForFile(SummaryFile, Error, diagnosticHandler);
427 IndexPtr = getFunctionIndexForFile(SummaryFile, Error, diagnosticHandler);
429428 if (!IndexPtr) {
430429 errs() << "Error loading file '" << SummaryFile << "': " << Error
431430 << "\n";
458457 "Summary Based Function Import", false, false)
459458
460459 namespace llvm {
461 Pass *createFunctionImportPass(const ModuleSummaryIndex *Index = nullptr) {
460 Pass *createFunctionImportPass(const FunctionInfoIndex *Index = nullptr) {
462461 return new FunctionImportPass(Index);
463462 }
464463 }
2222 #include "llvm/Analysis/TargetLibraryInfo.h"
2323 #include "llvm/Analysis/TypeBasedAliasAnalysis.h"
2424 #include "llvm/IR/DataLayout.h"
25 #include "llvm/IR/FunctionInfo.h"
2526 #include "llvm/IR/LegacyPassManager.h"
26 #include "llvm/IR/ModuleSummaryIndex.h"
2727 #include "llvm/IR/Verifier.h"
2828 #include "llvm/Support/CommandLine.h"
2929 #include "llvm/Support/ManagedStatic.h"
3232 #include "llvm/Transforms/IPO/ForceFunctionAttrs.h"
3333 #include "llvm/Transforms/IPO/FunctionAttrs.h"
3434 #include "llvm/Transforms/IPO/InferFunctionAttrs.h"
35 #include "llvm/Transforms/Instrumentation.h"
3635 #include "llvm/Transforms/Scalar.h"
3736 #include "llvm/Transforms/Scalar/GVN.h"
3837 #include "llvm/Transforms/Vectorize.h"
38 #include "llvm/Transforms/Instrumentation.h"
3939
4040 using namespace llvm;
4141
126126 SizeLevel = 0;
127127 LibraryInfo = nullptr;
128128 Inliner = nullptr;
129 ModuleSummary = nullptr;
129 FunctionIndex = nullptr;
130130 DisableUnitAtATime = false;
131131 DisableUnrollLoops = false;
132132 BBVectorize = RunBBVectorization;
571571 // Provide AliasAnalysis services for optimizations.
572572 addInitialAliasAnalysisPasses(PM);
573573
574 if (ModuleSummary)
575 PM.add(createFunctionImportPass(ModuleSummary));
574 if (FunctionIndex)
575 PM.add(createFunctionImportPass(FunctionIndex));
576576
577577 // Allow forcing function attributes as a debugging and tuning aid.
578578 PM.add(createForceFunctionAttrsLegacyPass());
723723 if (VerifyInput)
724724 PM.add(createVerifierPass());
725725
726 if (ModuleSummary)
727 PM.add(createFunctionImportPass(ModuleSummary));
726 if (FunctionIndex)
727 PM.add(createFunctionImportPass(FunctionIndex));
728728
729729 populateModulePassManager(PM);
730730
6767 // For now we are conservative in determining which variables are not
6868 // address taken by checking the unnamed addr flag. To be more aggressive,
6969 // the address taken information must be checked earlier during parsing
70 // of the module and recorded in the summary index for use when importing
70 // of the module and recorded in the function index for use when importing
7171 // from that module.
7272 auto *GVar = dyn_cast(SGV);
7373 if (GVar && GVar->isConstant() && GVar->hasUnnamedAddr())
7575
7676 // Eventually we only need to promote functions in the exporting module that
7777 // are referenced by a potentially exported function (i.e. one that is in the
78 // summary index).
78 // function index).
7979 return true;
8080 }
8181
8787 // avoid naming conflicts between locals imported from different modules.
8888 if (SGV->hasLocalLinkage() &&
8989 (doPromoteLocalToGlobal(SGV) || isPerformingImport()))
90 return ModuleSummaryIndex::getGlobalNameForLocal(
90 return FunctionInfoIndex::getGlobalNameForLocal(
9191 SGV->getName(),
9292 ImportIndex.getModuleId(SGV->getParent()->getModuleIdentifier()));
9393 return SGV->getName();
230230 return false;
231231 }
232232
233 bool llvm::renameModuleForThinLTO(Module &M, const ModuleSummaryIndex &Index) {
233 bool llvm::renameModuleForThinLTO(Module &M, const FunctionInfoIndex &Index) {
234234 FunctionImportGlobalProcessing ThinLTOProcessing(M, Index);
235235 return ThinLTOProcessing.run();
236236 }
0 ; Test to check the callgraph in summary when there is PGO
1 ; RUN: llvm-as -module-summary %s -o %t.o
1 ; RUN: llvm-as -function-summary %s -o %t.o
22 ; RUN: llvm-bcanalyzer -dump %t.o | FileCheck %s
3 ; RUN: llvm-as -module-summary %p/Inputs/thinlto-function-summary-callgraph.ll -o %t2.o
3 ; RUN: llvm-as -function-summary %p/Inputs/thinlto-function-summary-callgraph.ll -o %t2.o
44 ; RUN: llvm-lto -thinlto -o %t3 %t.o %t2.o
55 ; RUN: llvm-bcanalyzer -dump %t3.thinlto.bc | FileCheck %s --check-prefix=COMBINED
66
0 ; Test to check the callgraph in summary
1 ; RUN: llvm-as -module-summary %s -o %t.o
1 ; RUN: llvm-as -function-summary %s -o %t.o
22 ; RUN: llvm-bcanalyzer -dump %t.o | FileCheck %s
3 ; RUN: llvm-as -module-summary %p/Inputs/thinlto-function-summary-callgraph.ll -o %t2.o
3 ; RUN: llvm-as -function-summary %p/Inputs/thinlto-function-summary-callgraph.ll -o %t2.o
44 ; RUN: llvm-lto -thinlto -o %t3 %t.o %t2.o
55 ; RUN: llvm-bcanalyzer -dump %t3.thinlto.bc | FileCheck %s --check-prefix=COMBINED
66
0 ; Test to check both the callgraph and refgraph in summary
1 ; RUN: llvm-as -module-summary %s -o %t.o
1 ; RUN: llvm-as -function-summary %s -o %t.o
22 ; RUN: llvm-bcanalyzer -dump %t.o | FileCheck %s
33
44 ; See if the calls and other references are recorded properly using the
None ; RUN: llvm-as -module-summary < %s | llvm-bcanalyzer -dump | FileCheck %s -check-prefix=BC
1 ; Check for summary block/records.
0 ; RUN: llvm-as -function-summary < %s | llvm-bcanalyzer -dump | FileCheck %s -check-prefix=BC
1 ; Check for function summary block/records.
22
3 ; Check the value ids in the summary entries against the
3 ; Check the value ids in the function summary entries against the
44 ; same in the ValueSumbolTable, to ensure the ordering is stable.
55 ; Also check the linkage field on the summary entries.
66 ; BC:
1313 ; BC-NEXT: record string = 'bar'
1414 ; BC-NEXT: record string = 'f'
1515
16 ; RUN: llvm-as -module-summary < %s | llvm-dis | FileCheck %s
16 ; RUN: llvm-as -function-summary < %s | llvm-dis | FileCheck %s
1717 ; Check that this round-trips correctly.
1818
1919 ; ModuleID = ''
0 ; Check the linkage types in both the per-module and combined summaries.
1 ; RUN: llvm-as -module-summary %s -o %t.o
1 ; RUN: llvm-as -function-summary %s -o %t.o
22 ; RUN: llvm-bcanalyzer -dump %t.o | FileCheck %s
33 ; RUN: llvm-lto -thinlto -o %t2 %t.o
44 ; RUN: llvm-bcanalyzer -dump %t2.thinlto.bc | FileCheck %s --check-prefix=COMBINED
0 ; First ensure that the ThinLTO handling in llvm-link and llvm-lto handles
1 ; bitcode without summary sections gracefully.
1 ; bitcode without function summary sections gracefully.
22 ; RUN: llvm-as %s -o %t.bc
33 ; RUN: llvm-as %p/Inputs/funcimport.ll -o %t2.bc
4 ; RUN: llvm-link %t.bc -summary-index=%t.bc -S
4 ; RUN: llvm-link %t.bc -functionindex=%t.bc -S
55 ; RUN: llvm-lto -thinlto -o %t3 %t.bc %t2.bc
66
77 ; Do setup work for all below tests: generate bitcode and combined index
8 ; RUN: llvm-as -module-summary %s -o %t.bc
9 ; RUN: llvm-as -module-summary %p/Inputs/funcimport.ll -o %t2.bc
8 ; RUN: llvm-as -function-summary %s -o %t.bc
9 ; RUN: llvm-as -function-summary %p/Inputs/funcimport.ll -o %t2.bc
1010 ; RUN: llvm-lto -thinlto -o %t3 %t.bc %t2.bc
1111
1212 ; Ensure statics are promoted/renamed correctly from this file (all but
1313 ; constant variable need promotion).
14 ; RUN: llvm-link %t.bc -summary-index=%t3.thinlto.bc -S | FileCheck %s --check-prefix=EXPORTSTATIC
14 ; RUN: llvm-link %t.bc -functionindex=%t3.thinlto.bc -S | FileCheck %s --check-prefix=EXPORTSTATIC
1515 ; EXPORTSTATIC-DAG: @staticvar.llvm.1 = hidden global
1616 ; EXPORTSTATIC-DAG: @staticconstvar = internal unnamed_addr constant
1717 ; EXPORTSTATIC-DAG: @P.llvm.1 = hidden global void ()* null
2323 ; Also ensures that alias to a linkonce function is turned into a declaration
2424 ; and that the associated linkonce function is not in the output, as it is
2525 ; lazily linked and never referenced/materialized.
26 ; RUN: llvm-link %t2.bc -summary-index=%t3.thinlto.bc -import=globalfunc1:%t.bc -S | FileCheck %s --check-prefix=IMPORTGLOB1
26 ; RUN: llvm-link %t2.bc -functionindex=%t3.thinlto.bc -import=globalfunc1:%t.bc -S | FileCheck %s --check-prefix=IMPORTGLOB1
2727 ; IMPORTGLOB1-DAG: define available_externally void @globalfunc1
2828 ; IMPORTGLOB1-DAG: declare void @weakalias
2929 ; IMPORTGLOB1-DAG: declare void @analias
3434 ; Ensure that weak alias to a non-imported function is correctly
3535 ; turned into a declaration, but that strong alias to an imported function
3636 ; is imported as alias.
37 ; RUN: llvm-link %t2.bc -summary-index=%t3.thinlto.bc -import=globalfunc2:%t.bc -S | FileCheck %s --check-prefix=IMPORTGLOB2
37 ; RUN: llvm-link %t2.bc -functionindex=%t3.thinlto.bc -import=globalfunc2:%t.bc -S | FileCheck %s --check-prefix=IMPORTGLOB2
3838 ; IMPORTGLOB2-DAG: declare void @analias
3939 ; IMPORTGLOB2-DAG: define available_externally void @globalfunc2
4040 ; IMPORTGLOB2-DAG: declare void @weakalias
4242
4343 ; Ensure that strong alias imported in second pass of importing ends up
4444 ; as an alias.
45 ; RUN: llvm-link %t2.bc -summary-index=%t3.thinlto.bc -import=globalfunc1:%t.bc -import=globalfunc2:%t.bc -S | FileCheck %s --check-prefix=IMPORTGLOB3
45 ; RUN: llvm-link %t2.bc -functionindex=%t3.thinlto.bc -import=globalfunc1:%t.bc -import=globalfunc2:%t.bc -S | FileCheck %s --check-prefix=IMPORTGLOB3
4646 ; IMPORTGLOB3-DAG: declare void @analias
4747 ; IMPORTGLOB3-DAG: define available_externally void @globalfunc1
4848 ; IMPORTGLOB3-DAG: define available_externally void @globalfunc2
5151 ; Ensure that strong alias imported in first pass of importing ends up
5252 ; as an alias, and that seeing the alias definition during a second inlining
5353 ; pass is handled correctly.
54 ; RUN: llvm-link %t2.bc -summary-index=%t3.thinlto.bc -import=globalfunc2:%t.bc -import=globalfunc1:%t.bc -S | FileCheck %s --check-prefix=IMPORTGLOB4
54 ; RUN: llvm-link %t2.bc -functionindex=%t3.thinlto.bc -import=globalfunc2:%t.bc -import=globalfunc1:%t.bc -S | FileCheck %s --check-prefix=IMPORTGLOB4
5555 ; IMPORTGLOB4-DAG: declare void @analias
5656 ; IMPORTGLOB4-DAG: define available_externally void @globalfunc2
5757 ; IMPORTGLOB4-DAG: define available_externally void @globalfunc1
5959
6060 ; An alias to an imported function is imported as alias if the function is not
6161 ; available_externally.
62 ; RUN: llvm-link %t2.bc -summary-index=%t3.thinlto.bc -import=linkoncefunc:%t.bc -S | FileCheck %s --check-prefix=IMPORTGLOB5
62 ; RUN: llvm-link %t2.bc -functionindex=%t3.thinlto.bc -import=linkoncefunc:%t.bc -S | FileCheck %s --check-prefix=IMPORTGLOB5
6363 ; IMPORTGLOB5-DAG: linkoncealias = alias void (...), bitcast (void ()* @linkoncefunc to void (...)*)
6464 ; IMPORTGLOB5-DAG: define linkonce_odr void @linkoncefunc()
6565
6666 ; Ensure that imported static variable and function references are correctly
6767 ; promoted and renamed (including static constant variable).
68 ; RUN: llvm-link %t2.bc -summary-index=%t3.thinlto.bc -import=referencestatics:%t.bc -S | FileCheck %s --check-prefix=IMPORTSTATIC
68 ; RUN: llvm-link %t2.bc -functionindex=%t3.thinlto.bc -import=referencestatics:%t.bc -S | FileCheck %s --check-prefix=IMPORTSTATIC
6969 ; IMPORTSTATIC-DAG: @staticvar.llvm.1 = available_externally hidden global
7070 ; IMPORTSTATIC-DAG: @staticconstvar.llvm.1 = internal unnamed_addr constant
7171 ; IMPORTSTATIC-DAG: define available_externally i32 @referencestatics
7676 ; Ensure that imported global (external) function and variable references
7777 ; are handled correctly (including referenced variable imported as
7878 ; available_externally definition)
79 ; RUN: llvm-link %t2.bc -summary-index=%t3.thinlto.bc -import=referenceglobals:%t.bc -S | FileCheck %s --check-prefix=IMPORTGLOBALS
79 ; RUN: llvm-link %t2.bc -functionindex=%t3.thinlto.bc -import=referenceglobals:%t.bc -S | FileCheck %s --check-prefix=IMPORTGLOBALS
8080 ; IMPORTGLOBALS-DAG: @globalvar = available_externally global
8181 ; IMPORTGLOBALS-DAG: declare void @globalfunc1()
8282 ; IMPORTGLOBALS-DAG: define available_externally i32 @referenceglobals
8383
8484 ; Ensure that common variable correctly imported as common defition.
85 ; RUN: llvm-link %t2.bc -summary-index=%t3.thinlto.bc -import=referencecommon:%t.bc -S | FileCheck %s --check-prefix=IMPORTCOMMON
85 ; RUN: llvm-link %t2.bc -functionindex=%t3.thinlto.bc -import=referencecommon:%t.bc -S | FileCheck %s --check-prefix=IMPORTCOMMON
8686 ; IMPORTCOMMON-DAG: @commonvar = common global
8787 ; IMPORTCOMMON-DAG: define available_externally i32 @referencecommon
8888
8989 ; Ensure that imported static function pointer correctly promoted and renamed.
90 ; RUN: llvm-link %t2.bc -summary-index=%t3.thinlto.bc -import=callfuncptr:%t.bc -S | FileCheck %s --check-prefix=IMPORTFUNCPTR
90 ; RUN: llvm-link %t2.bc -functionindex=%t3.thinlto.bc -import=callfuncptr:%t.bc -S | FileCheck %s --check-prefix=IMPORTFUNCPTR
9191 ; IMPORTFUNCPTR-DAG: @P.llvm.1 = available_externally hidden global void ()* null
9292 ; IMPORTFUNCPTR-DAG: define available_externally void @callfuncptr
9393 ; IMPORTFUNCPTR-DAG: %0 = load void ()*, void ()** @P.llvm.1
9595 ; Ensure that imported weak function reference/definition handled properly.
9696 ; Imported weak_any definition should be skipped with warning, and imported
9797 ; reference should turned into an external_weak declaration.
98 ; RUN: llvm-link %t2.bc -summary-index=%t3.thinlto.bc -import=callweakfunc:%t.bc -import=weakfunc:%t.bc -S 2>&1 | FileCheck %s --check-prefix=IMPORTWEAKFUNC
98 ; RUN: llvm-link %t2.bc -functionindex=%t3.thinlto.bc -import=callweakfunc:%t.bc -import=weakfunc:%t.bc -S 2>&1 | FileCheck %s --check-prefix=IMPORTWEAKFUNC
9999 ; IMPORTWEAKFUNC-DAG: Ignoring import request for weak-any function weakfunc
100100 ; IMPORTWEAKFUNC-DAG: declare extern_weak void @weakfunc
101101 ; IMPORTWEAKFUNC-DAG: define available_externally void @callweakfunc
None ; RUN: llvm-as -module-summary %s -o %t1.bc
1 ; RUN: llvm-as -module-summary %p/Inputs/funcimport2.ll -o %t2.bc
0 ; RUN: llvm-as -function-summary %s -o %t1.bc
1 ; RUN: llvm-as -function-summary %p/Inputs/funcimport2.ll -o %t2.bc
22 ; RUN: llvm-lto -thinlto -o %t3 %t1.bc %t2.bc
3 ; RUN: llvm-link -import=bar:%t2.bc %t1.bc -summary-index=%t3.thinlto.bc -S | FileCheck %s
3 ; RUN: llvm-link -import=bar:%t2.bc %t1.bc -functionindex=%t3.thinlto.bc -S | FileCheck %s
44
55 ; CHECK: define linkonce_odr hidden void @foo() {
66 define available_externally hidden void @foo() {
None ; RUN: llvm-as -module-summary %s -o %t.bc
1 ; RUN: llvm-as -module-summary %p/Inputs/funcimport_appending_global.ll -o %t2.bc
0 ; RUN: llvm-as -function-summary %s -o %t.bc
1 ; RUN: llvm-as -function-summary %p/Inputs/funcimport_appending_global.ll -o %t2.bc
22 ; RUN: llvm-lto -thinlto -o %t3 %t.bc %t2.bc
33
44 ; Do the import now
5 ; RUN: llvm-link %t.bc -summary-index=%t3.thinlto.bc -import=foo:%t2.bc -S | FileCheck %s
5 ; RUN: llvm-link %t.bc -functionindex=%t3.thinlto.bc -import=foo:%t2.bc -S | FileCheck %s
66
77 ; Ensure that global constructor (appending linkage) is not imported
88 ; CHECK-NOT: @llvm.global_ctors = {{.*}}@foo
0 ; Do setup work for all below tests: generate bitcode and combined index
1 ; RUN: llvm-as -module-summary %s -o %t.bc
2 ; RUN: llvm-as -module-summary %p/Inputs/funcimport_comdat.ll -o %t2.bc
1 ; RUN: llvm-as -function-summary %s -o %t.bc
2 ; RUN: llvm-as -function-summary %p/Inputs/funcimport_comdat.ll -o %t2.bc
33 ; RUN: llvm-lto -thinlto -o %t3 %t.bc %t2.bc
44
55 ; Ensure linking of comdat containing external linkage global and function
66 ; removes the imported available_externally defs from comdat.
7 ; RUN: llvm-link %t2.bc -summary-index=%t3.thinlto.bc -import=comdat1_func1:%t.bc -S | FileCheck %s --check-prefix=IMPORTCOMDAT
7 ; RUN: llvm-link %t2.bc -functionindex=%t3.thinlto.bc -import=comdat1_func1:%t.bc -S | FileCheck %s --check-prefix=IMPORTCOMDAT
88 ; IMPORTCOMDAT-NOT: $comdat1 = comdat any
99 ; IMPORTCOMDAT-NOT: comdat($comdat1)
1010
1111 ; Ensure linking of comdat containing internal linkage function with alias
1212 ; removes the imported and promoted available_externally defs from comdat.
13 ; RUN: llvm-link %t2.bc -summary-index=%t3.thinlto.bc -import=comdat2_func1:%t.bc -S | FileCheck %s --check-prefix=IMPORTCOMDAT2
13 ; RUN: llvm-link %t2.bc -functionindex=%t3.thinlto.bc -import=comdat2_func1:%t.bc -S | FileCheck %s --check-prefix=IMPORTCOMDAT2
1414 ; IMPORTCOMDAT2-NOT: $comdat2 = comdat any
1515 ; IMPORTCOMDAT2-NOT: comdat($comdat2)
1616
0 ; Do setup work for all below tests: generate bitcode and combined index
1 ; RUN: llvm-as -module-summary %s -o %t.bc
2 ; RUN: llvm-as -module-summary %p/Inputs/thinlto_funcimport_debug.ll -o %t2.bc
1 ; RUN: llvm-as -function-summary %s -o %t.bc
2 ; RUN: llvm-as -function-summary %p/Inputs/thinlto_funcimport_debug.ll -o %t2.bc
33 ; RUN: llvm-lto -thinlto -o %t3 %t.bc %t2.bc
44
55 ; If we import func1 and not func2 we should only link DISubprogram for func1
6 ; RUN: llvm-link %t2.bc -summary-index=%t3.thinlto.bc -import=func1:%t.bc -S | FileCheck %s
6 ; RUN: llvm-link %t2.bc -functionindex=%t3.thinlto.bc -import=func1:%t.bc -S | FileCheck %s
77
88 ; CHECK: declare i32 @func2
99 ; CHECK: define available_externally i32 @func1
0 ; Do setup work for all below tests: generate bitcode and combined index
1 ; RUN: llvm-as -module-summary %s -o %t.bc
2 ; RUN: llvm-as -module-summary %p/Inputs/funcimport.ll -o %t2.bc
1 ; RUN: llvm-as -function-summary %s -o %t.bc
2 ; RUN: llvm-as -function-summary %p/Inputs/funcimport.ll -o %t2.bc
33 ; RUN: llvm-lto -thinlto-action=thinlink -o %t3.bc %t.bc %t2.bc
44
55 ; Ensure statics are promoted/renamed correctly from this file (all but
0 ; Do setup work for all below tests: generate bitcode and combined index
1 ; RUN: llvm-as -module-summary %s -o %t.bc
2 ; RUN: llvm-as -module-summary %p/Inputs/adjustable_threshold.ll -o %t2.bc
1 ; RUN: llvm-as -function-summary %s -o %t.bc
2 ; RUN: llvm-as -function-summary %p/Inputs/adjustable_threshold.ll -o %t2.bc
33 ; RUN: llvm-lto -thinlto -o %t3 %t.bc %t2.bc
44
55 ; Test import with default progressive instruction factor
0 ; Do setup work for all below tests: generate bitcode and combined index
1 ; RUN: llvm-as -module-summary %s -o %t.bc
2 ; RUN: llvm-as -module-summary %p/Inputs/funcimport.ll -o %t2.bc
1 ; RUN: llvm-as -function-summary %s -o %t.bc
2 ; RUN: llvm-as -function-summary %p/Inputs/funcimport.ll -o %t2.bc
33 ; RUN: llvm-lto -thinlto -o %t3 %t.bc %t2.bc
44
55 ; Do the import now
0 ; Do setup work for all below tests: generate bitcode and combined index
1 ; RUN: llvm-as -module-summary %s -o %t.bc
2 ; RUN: llvm-as -module-summary %p/Inputs/funcimport_alias.ll -o %t2.bc
1 ; RUN: llvm-as -function-summary %s -o %t.bc
2 ; RUN: llvm-as -function-summary %p/Inputs/funcimport_alias.ll -o %t2.bc
33 ; RUN: llvm-lto -thinlto -o %t3 %t.bc %t2.bc
44
55 ; Do the import now. Ensures that the importer handles an external call
0 ; Do setup work for all below tests: generate bitcode and combined index
1 ; RUN: llvm-as -module-summary %s -o %t.bc
2 ; RUN: llvm-as -module-summary %p/Inputs/funcimport_debug.ll -o %t2.bc
1 ; RUN: llvm-as -function-summary %s -o %t.bc
2 ; RUN: llvm-as -function-summary %p/Inputs/funcimport_debug.ll -o %t2.bc
33 ; RUN: llvm-lto -thinlto -o %t3 %t.bc %t2.bc
44
55 ; Do the import now and confirm that metadata is linked for imported function.
0 ; RUN: llc %s -o %t.o -filetype=obj -relocation-model=pic
1 ; RUN: llvm-as -module-summary %p/Inputs/pr19901-1.ll -o %t2.o
1 ; RUN: llvm-as -function-summary %p/Inputs/pr19901-1.ll -o %t2.o
22 ; RUN: %gold -plugin %llvmshlibdir/LLVMgold.so \
33 ; RUN: --plugin-opt=thinlto \
44 ; RUN: -shared -m elf_x86_64 -o %t.so %t2.o %t.o
0 ; First ensure that the ThinLTO handling in the gold plugin handles
1 ; bitcode without summary sections gracefully.
1 ; bitcode without function summary sections gracefully.
22 ; RUN: llvm-as %s -o %t.o
33 ; RUN: llvm-as %p/Inputs/thinlto.ll -o %t2.o
44 ; RUN: %gold -plugin %llvmshlibdir/LLVMgold.so \
1111 ; RUN: -shared %t.o %t2.o -o %t4
1212 ; RUN: llvm-nm %t4 | FileCheck %s --check-prefix=NM
1313
14 ; Next generate summary sections and test gold handling.
15 ; RUN: llvm-as -module-summary %s -o %t.o
16 ; RUN: llvm-as -module-summary %p/Inputs/thinlto.ll -o %t2.o
14 ; Next generate function summary sections and test gold handling.
15 ; RUN: llvm-as -function-summary %s -o %t.o
16 ; RUN: llvm-as -function-summary %p/Inputs/thinlto.ll -o %t2.o
1717
1818 ; Ensure gold generates an index and not a binary if requested.
1919 ; RUN: %gold -plugin %llvmshlibdir/LLVMgold.so \
None ; RUN: llvm-as -module-summary %s -o %t.o
1 ; RUN: llvm-as -module-summary %p/Inputs/thinlto_linkonceresolution.ll -o %t2.o
0 ; RUN: llvm-as -function-summary %s -o %t.o
1 ; RUN: llvm-as -function-summary %p/Inputs/thinlto_linkonceresolution.ll -o %t2.o
22
33 ; Ensure the plugin ensures that for ThinLTO the prevailing copy of a
44 ; linkonce symbol is changed to weak to ensure it is not eliminated.
0 ; Test combined function index generation for ThinLTO via llvm-lto.
1 ; RUN: llvm-as -module-summary %s -o %t.o
2 ; RUN: llvm-as -module-summary %p/Inputs/thinlto.ll -o %t2.o
1 ; RUN: llvm-as -function-summary %s -o %t.o
2 ; RUN: llvm-as -function-summary %p/Inputs/thinlto.ll -o %t2.o
33 ; RUN: llvm-lto -thinlto -o %t3 %t.o %t2.o
44 ; RUN: llvm-bcanalyzer -dump %t3.thinlto.bc | FileCheck %s --check-prefix=COMBINED
55 ; RUN: not test -e %t3
3030 #include "llvm/IR/Verifier.h"
3131 #include "llvm/Linker/IRMover.h"
3232 #include "llvm/MC/SubtargetFeature.h"
33 #include "llvm/Object/ModuleSummaryIndexObjectFile.h"
33 #include "llvm/Object/FunctionIndexObjectFile.h"
3434 #include "llvm/Object/IRObjectFile.h"
3535 #include "llvm/Support/Host.h"
3636 #include "llvm/Support/ManagedStatic.h"
623623 return View;
624624 }
625625
626 static std::unique_ptr
627 getModuleSummaryIndexForFile(claimed_file &F, ld_plugin_input_file &Info) {
626 static std::unique_ptr
627 getFunctionIndexForFile(claimed_file &F, ld_plugin_input_file &Info) {
628628 const void *View = getSymbolsAndView(F);
629629 if (!View)
630630 return nullptr;
634634
635635 // Don't bother trying to build an index if there is no summary information
636636 // in this bitcode file.
637 if (!object::ModuleSummaryIndexObjectFile::hasGlobalValueSummaryInMemBuffer(
637 if (!object::FunctionIndexObjectFile::hasGlobalValueSummaryInMemBuffer(
638638 BufferRef, diagnosticHandler))
639 return std::unique_ptr(nullptr);
640
641 ErrorOr> ObjOrErr =
642 object::ModuleSummaryIndexObjectFile::create(BufferRef,
643 diagnosticHandler);
639 return std::unique_ptr(nullptr);
640
641 ErrorOr> ObjOrErr =
642 object::FunctionIndexObjectFile::create(BufferRef, diagnosticHandler);
644643
645644 if (std::error_code EC = ObjOrErr.getError())
646 message(LDPL_FATAL,
647 "Could not read module summary index bitcode from file : %s",
645 message(LDPL_FATAL, "Could not read function index bitcode from file : %s",
648646 EC.message().c_str());
649647
650 object::ModuleSummaryIndexObjectFile &Obj = **ObjOrErr;
648 object::FunctionIndexObjectFile &Obj = **ObjOrErr;
651649
652650 return Obj.takeIndex();
653651 }
845843 /// The task ID when this was invoked in a thread (ThinLTO).
846844 int TaskID;
847845
848 /// The module summary index for ThinLTO tasks.
849 const ModuleSummaryIndex *CombinedIndex;
846 /// The function index for ThinLTO tasks.
847 const FunctionInfoIndex *CombinedIndex;
850848
851849 /// The target machine for generating code for this module.
852850 std::unique_ptr TM;
863861 }
864862 /// Constructor used by ThinLTO.
865863 CodeGen(std::unique_ptr M, raw_fd_ostream *OS, int TaskID,
866 const ModuleSummaryIndex *CombinedIndex, std::string Filename)
864 const FunctionInfoIndex *CombinedIndex, std::string Filename)
867865 : M(std::move(M)), OS(OS), TaskID(TaskID), CombinedIndex(CombinedIndex),
868866 SaveTempsFilename(Filename) {
869867 assert(options::thinlto == !!CombinedIndex &&
870 "Expected module summary index iff performing ThinLTO");
868 "Expected function index iff performing ThinLTO");
871869 initTargetMachine();
872870 }
873871
952950 PMB.LoopVectorize = true;
953951 PMB.SLPVectorize = true;
954952 PMB.OptLevel = options::OptLevel;
955 PMB.ModuleSummary = CombinedIndex;
953 PMB.FunctionIndex = CombinedIndex;
956954 PMB.populateLTOPassManager(passes);
957955 passes.run(*M);
958956 }
10951093 static void thinLTOBackendTask(claimed_file &F, const void *View,
10961094 ld_plugin_input_file &File,
10971095 raw_fd_ostream *ApiFile,
1098 const ModuleSummaryIndex &CombinedIndex,
1096 const FunctionInfoIndex &CombinedIndex,
10991097 raw_fd_ostream *OS, unsigned TaskID) {
11001098 // Need to use a separate context for each task
11011099 LLVMContext Context;
11161114
11171115 /// Launch each module's backend pipeline in a separate task in a thread pool.
11181116 static void thinLTOBackends(raw_fd_ostream *ApiFile,
1119 const ModuleSummaryIndex &CombinedIndex) {
1117 const FunctionInfoIndex &CombinedIndex) {
11201118 unsigned TaskCount = 0;
11211119 std::vector Tasks;
11221120 Tasks.reserve(Modules.size());
11851183 cl::ParseCommandLineOptions(NumOpts, &options::extra[0]);
11861184
11871185 // If we are doing ThinLTO compilation, simply build the combined
1188 // module index/summary and emit it. We don't need to parse the modules
1186 // function index/summary and emit it. We don't need to parse the modules
11891187 // and link them in this case.
11901188 if (options::thinlto) {
1191 ModuleSummaryIndex CombinedIndex;
1189 FunctionInfoIndex CombinedIndex;
11921190 uint64_t NextModuleId = 0;
11931191 for (claimed_file &F : Modules) {
11941192 PluginInputFile InputFile(F.handle);
11951193
1196 std::unique_ptr Index =
1197 getModuleSummaryIndexForFile(F, InputFile.file());
1198
1199 // Skip files without a module summary.
1194 std::unique_ptr Index =
1195 getFunctionIndexForFile(F, InputFile.file());
1196
1197 // Skip files without a function summary.
12001198 if (Index)
12011199 CombinedIndex.mergeFrom(std::move(Index), ++NextModuleId);
12021200 }
4343 static cl::opt
4444 DisableOutput("disable-output", cl::desc("Disable output"), cl::init(false));
4545
46 static cl::opt EmitSummaryIndex("module-summary",
47 cl::desc("Emit module summary index"),
48 cl::init(false));
46 static cl::opt
47 EmitFunctionSummary("function-summary", cl::desc("Emit function summary index"),
48 cl::init(false));
4949
5050 static cl::opt
5151 DumpAsm("d", cl::desc("Print assembly as parsed"), cl::Hidden);
8181
8282 if (Force || !CheckBitcodeOutputToConsole(Out->os(), true))
8383 WriteBitcodeToFile(M, Out->os(), PreserveBitcodeUseListOrder,
84 EmitSummaryIndex);
84 EmitFunctionSummary);
8585
8686 // Declare success.
8787 Out->keep();
1111 //
1212 //===----------------------------------------------------------------------===//
1313
14 #include "llvm/Linker/Linker.h"
1415 #include "llvm/ADT/STLExtras.h"
1516 #include "llvm/Bitcode/ReaderWriter.h"
1617 #include "llvm/IR/AutoUpgrade.h"
1718 #include "llvm/IR/DiagnosticInfo.h"
1819 #include "llvm/IR/DiagnosticPrinter.h"
20 #include "llvm/IR/FunctionInfo.h"
1921 #include "llvm/IR/LLVMContext.h"
2022 #include "llvm/IR/Module.h"
21 #include "llvm/IR/ModuleSummaryIndex.h"
2223 #include "llvm/IR/Verifier.h"
2324 #include "llvm/IRReader/IRReader.h"
24 #include "llvm/Linker/Linker.h"
25 #include "llvm/Object/ModuleSummaryIndexObjectFile.h"
25 #include "llvm/Object/FunctionIndexObjectFile.h"
2626 #include "llvm/Support/CommandLine.h"
2727 #include "llvm/Support/FileSystem.h"
2828 #include "llvm/Support/ManagedStatic.h"
5151 cl::desc("Pair of function name and filename, where function should be "
5252 "imported from bitcode in filename"));
5353
54 // Option to support testing of function importing. The module summary
54 // Option to support testing of function importing. The function index
5555 // must be specified in the case were we request imports via the -import
5656 // option, as well as when compiling any module with functions that may be
5757 // exported (imported by a different llvm-link -import invocation), to ensure
5858 // consistent promotion and renaming of locals.
59 static cl::opt
60 SummaryIndex("summary-index", cl::desc("Module summary index filename"),
61 cl::init(""), cl::value_desc("filename"));
59 static cl::opt FunctionIndex("functionindex",
60 cl::desc("Function index filename"),
61 cl::init(""),
62 cl::value_desc("filename"));
6263
6364 static cl::opt
6465 OutputFilename("o", cl::desc("Override output filename"), cl::init("-"),
190191 if (Verbose)
191192 errs() << "Importing " << FunctionName << " from " << FileName << "\n";
192193
193 std::unique_ptr Index;
194 if (!SummaryIndex.empty()) {
195 ErrorOr> IndexOrErr =
196 llvm::getModuleSummaryIndexForFile(SummaryIndex, diagnosticHandler);
194 std::unique_ptr Index;
195 if (!FunctionIndex.empty()) {
196 ErrorOr> IndexOrErr =
197 llvm::getFunctionIndexForFile(FunctionIndex, diagnosticHandler);
197198 std::error_code EC = IndexOrErr.getError();
198199 if (EC) {
199200 errs() << EC.message() << '\n';
257258 return false;
258259 }
259260
260 // If a module summary index is supplied, load it so linkInModule can treat
261 // If a function index is supplied, load it so linkInModule can treat
261262 // local functions/variables as exported and promote if necessary.
262 std::unique_ptr Index;
263 if (!SummaryIndex.empty()) {
264 ErrorOr> IndexOrErr =
265 llvm::getModuleSummaryIndexForFile(SummaryIndex, diagnosticHandler);
263 std::unique_ptr Index;
264 if (!FunctionIndex.empty()) {
265 ErrorOr> IndexOrErr =
266 llvm::getFunctionIndexForFile(FunctionIndex, diagnosticHandler);
266267 std::error_code EC = IndexOrErr.getError();
267268 if (EC) {
268269 errs() << EC.message() << '\n';
1818 #include "llvm/IR/LLVMContext.h"
1919 #include "llvm/IRReader/IRReader.h"
2020 #include "llvm/LTO/LTOCodeGenerator.h"
21 #include "llvm/LTO/ThinLTOCodeGenerator.h"
2122 #include "llvm/LTO/LTOModule.h"
22 #include "llvm/LTO/ThinLTOCodeGenerator.h"
23 #include "llvm/Object/ModuleSummaryIndexObjectFile.h"
23 #include "llvm/Object/FunctionIndexObjectFile.h"
2424 #include "llvm/Support/CommandLine.h"
2525 #include "llvm/Support/FileSystem.h"
2626 #include "llvm/Support/ManagedStatic.h"
250250 ///
251251 /// This is meant to enable testing of ThinLTO combined index generation,
252252 /// currently available via the gold plugin via -thinlto.
253 static void createCombinedModuleSummaryIndex() {
254 ModuleSummaryIndex CombinedIndex;
253 static void createCombinedFunctionIndex() {
254 FunctionInfoIndex CombinedIndex;
255255 uint64_t NextModuleId = 0;
256256 for (auto &Filename : InputFilenames) {
257257 CurrentActivity = "loading file '" + Filename + "'";
258 ErrorOr> IndexOrErr =
259 llvm::getModuleSummaryIndexForFile(Filename, diagnosticHandler);
260 std::unique_ptr Index = std::move(IndexOrErr.get());
258 ErrorOr> IndexOrErr =
259 llvm::getFunctionIndexForFile(Filename, diagnosticHandler);
260 std::unique_ptr Index = std::move(IndexOrErr.get());
261261 CurrentActivity = "";
262 // Skip files without a module summary.
262 // Skip files without a function summary.
263263 if (!Index)
264264 continue;
265265 CombinedIndex.mergeFrom(std::move(Index), ++NextModuleId);
276276 namespace thinlto {
277277
278278 std::vector>
279 loadAllFilesForIndex(const ModuleSummaryIndex &Index) {
279 loadAllFilesForIndex(const FunctionInfoIndex &Index) {
280280 std::vector> InputBuffers;
281281
282282 for (auto &ModPath : Index.modPathStringEntries()) {
289289 return InputBuffers;
290290 }
291291
292 std::unique_ptr<ModuleSummaryIndex> loadCombinedIndex() {
292 std::unique_ptr<FunctionInfoIndex> loadCombinedIndex() {
293293 if (ThinLTOIndex.empty())
294294 report_fatal_error("Missing -thinlto-index for ThinLTO promotion stage");
295295 auto CurrentActivity = "loading file '" + ThinLTOIndex + "'";
296 ErrorOr> IndexOrErr =
297 llvm::getModuleSummaryIndexForFile(ThinLTOIndex, diagnosticHandler);
296 ErrorOr> IndexOrErr =
297 llvm::getFunctionIndexForFile(ThinLTOIndex, diagnosticHandler);
298298 error(IndexOrErr, "error " + CurrentActivity);
299299 return std::move(IndexOrErr.get());
300300 }
556556 }
557557
558558 if (ThinLTO) {
559 createCombinedModuleSummaryIndex();
559 createCombinedFunctionIndex();
560560 return 0;
561561 }
562562