llvm.org GIT mirror llvm / b97baa5
Support for function summary index bitcode sections and files. Summary: The bitcode format is described in this document: https://drive.google.com/file/d/0B036uwnWM6RWdnBLakxmeDdOeXc/view For more info on ThinLTO see: https://sites.google.com/site/llvmthinlto The first customer is ThinLTO, however the data structures are designed and named more generally based on prior feedback. There are a few comments regarding how certain interfaces are used by ThinLTO, and the options added here to gold currently have ThinLTO-specific names as the behavior they provoke is currently ThinLTO-specific. This patch includes support for generating per-module function indexes, the combined index file via the gold plugin, and several tests (more are included with the associated clang patch D11908). Reviewers: dexonsmith, davidxl, joker.eph Subscribers: llvm-commits Differential Revision: http://reviews.llvm.org/D13107 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@249270 91177308-0d34-0410-b5e6-96231b3b80d8 Teresa Johnson 4 years ago
20 changed file(s) with 1635 addition(s) and 48 deletion(s). Raw diff Collapse all Expand all
2828 ///
2929 /// If \c ShouldPreserveUseListOrder, encode use-list order so it can be
3030 /// reproduced when deserialized.
31 ///
32 /// If \c EmitFunctionSummary, emit the function summary index (currently
33 /// for use in ThinLTO optimization).
3134 ModulePass *createBitcodeWriterPass(raw_ostream &Str,
32 bool ShouldPreserveUseListOrder = false);
35 bool ShouldPreserveUseListOrder = false,
36 bool EmitFunctionSummary = false);
3337
3438 /// \brief Pass for writing a module of IR out to a bitcode file.
3539 ///
3842 class BitcodeWriterPass {
3943 raw_ostream &OS;
4044 bool ShouldPreserveUseListOrder;
45 bool EmitFunctionSummary;
4146
4247 public:
4348 /// \brief Construct a bitcode writer pass around a particular output stream.
4449 ///
4550 /// If \c ShouldPreserveUseListOrder, encode use-list order so it can be
4651 /// reproduced when deserialized.
52 ///
53 /// If \c EmitFunctionSummary, emit the function summary index (currently
54 /// for use in ThinLTO optimization).
4755 explicit BitcodeWriterPass(raw_ostream &OS,
48 bool ShouldPreserveUseListOrder = false)
49 : OS(OS), ShouldPreserveUseListOrder(ShouldPreserveUseListOrder) {}
56 bool ShouldPreserveUseListOrder = false,
57 bool EmitFunctionSummary = false)
58 : OS(OS), ShouldPreserveUseListOrder(ShouldPreserveUseListOrder),
59 EmitFunctionSummary(EmitFunctionSummary) {}
5060
5161 /// \brief Run the bitcode writer pass, and output the module to the selected
5262 /// output stream.
9393
9494 /// \brief Retrieve the current position in the stream, in bits.
9595 uint64_t GetCurrentBitNo() const { return GetBufferOffset() * 8 + CurBit; }
96
97 /// \brief Retrieve the number of bits currently used to encode an abbrev ID.
98 unsigned GetAbbrevIDWidth() const { return CurCodeSize; }
9699
97100 //===--------------------------------------------------------------------===//
98101 // Basic Primitives for emitting bits to the stream.
149149 VST_CODE_ENTRY = 1, // VST_ENTRY: [valueid, namechar x N]
150150 VST_CODE_BBENTRY = 2, // VST_BBENTRY: [bbid, namechar x N]
151151 VST_CODE_FNENTRY = 3, // VST_FNENTRY: [valueid, offset, namechar x N]
152 // VST_COMBINED_FNENTRY: [offset, namechar x N]
153 VST_CODE_COMBINED_FNENTRY = 4
154 };
155
156 // The module path symbol table only has one code (MST_CODE_ENTRY).
157 enum ModulePathSymtabCodes {
158 MST_CODE_ENTRY = 1, // MST_ENTRY: [modid, namechar x N]
159 };
160
161 // The function summary section uses different codes in the per-module
162 // and combined index cases.
163 enum FunctionSummarySymtabCodes {
164 FS_CODE_PERMODULE_ENTRY = 1, // FS_ENTRY: [valueid, islocal, instcount]
165 FS_CODE_COMBINED_ENTRY = 2, // FS_ENTRY: [modid, instcount]
152166 };
153167
154168 enum MetadataCodes {
1414 #define LLVM_BITCODE_READERWRITER_H
1515
1616 #include "llvm/IR/DiagnosticInfo.h"
17 #include "llvm/IR/FunctionInfo.h"
1718 #include "llvm/Support/Endian.h"
1819 #include "llvm/Support/ErrorOr.h"
1920 #include "llvm/Support/MemoryBuffer.h"
5758 parseBitcodeFile(MemoryBufferRef Buffer, LLVMContext &Context,
5859 DiagnosticHandlerFunction DiagnosticHandler = nullptr);
5960
61 /// Check if the given bitcode buffer contains a function summary block.
62 bool hasFunctionSummary(MemoryBufferRef Buffer, LLVMContext &Context,
63 DiagnosticHandlerFunction DiagnosticHandler);
64
65 /// Parse the specified bitcode buffer, returning the function info index.
66 /// If IsLazy is true, parse the entire function summary into
67 /// the index. Otherwise skip the function summary section, and only create
68 /// an index object with a map from function name to function summary offset.
69 /// The index is used to perform lazy function summary reading later.
70 ErrorOr> getFunctionInfoIndex(
71 MemoryBufferRef Buffer, LLVMContext &Context,
72 DiagnosticHandlerFunction DiagnosticHandler, bool IsLazy = false);
73
74 /// This method supports lazy reading of function summary data from the
75 /// combined index during function importing. When reading the combined index
76 /// file, getFunctionInfoIndex is first invoked with IsLazy=true.
77 /// Then this method is called for each function considered for importing,
78 /// to parse the summary information for the given function name into
79 /// the index.
80 std::error_code readFunctionSummary(
81 MemoryBufferRef Buffer, LLVMContext &Context,
82 DiagnosticHandlerFunction DiagnosticHandler, StringRef FunctionName,
83 std::unique_ptr Index);
84
6085 /// \brief Write the specified module to the specified raw output stream.
6186 ///
6287 /// For streams where it matters, the given stream should be in "binary"
6590 /// If \c ShouldPreserveUseListOrder, encode the use-list order for each \a
6691 /// Value in \c M. These will be reconstructed exactly when \a M is
6792 /// deserialized.
93 ///
94 /// If \c EmitFunctionSummary, emit the function summary index (currently
95 /// for use in ThinLTO optimization).
6896 void WriteBitcodeToFile(const Module *M, raw_ostream &Out,
69 bool ShouldPreserveUseListOrder = false);
97 bool ShouldPreserveUseListOrder = false,
98 bool EmitFunctionSummary = false);
99
100 /// Write the specified function summary index to the given raw output stream,
101 /// where it will be written in a new bitcode block. This is used when
102 /// writing the combined index file for ThinLTO.
103 void WriteFunctionSummaryToFile(const FunctionInfoIndex *Index,
104 raw_ostream &Out);
70105
71106 /// isBitcodeWrapper - Return true if the given bytes are the magic bytes
72107 /// 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 function info index and summary.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_IR_FUNCTIONINFO_H
16 #define LLVM_IR_FUNCTIONINFO_H
17
18 #include "llvm/ADT/SmallString.h"
19 #include "llvm/ADT/StringMap.h"
20 #include "llvm/IR/Module.h"
21 #include "llvm/Support/MemoryBuffer.h"
22 #include "llvm/Support/raw_ostream.h"
23
24 namespace llvm {
25
26 /// \brief Function summary information to aid decisions and implementation of
27 /// importing.
28 ///
29 /// This is a separate class from FunctionInfo to enable lazy reading of this
30 /// function summary information from the combined index file during imporing.
31 class FunctionSummary {
32 private:
33 /// \brief Path of module containing function IR, used to locate module when
34 /// importing this function.
35 ///
36 /// This is only used during parsing of the combined function index, or when
37 /// parsing the per-module index for creation of the combined function index,
38 /// not during writing of the per-module index which doesn't contain a
39 /// module path string table.
40 StringRef ModulePath;
41
42 /// \brief Used to flag functions that have local linkage types and need to
43 /// have module identifier appended before placing into the combined
44 /// index, to disambiguate from other functions with the same name.
45 ///
46 /// This is only used in the per-module function index, as it is consumed
47 /// while creating the combined index.
48 bool IsLocalFunction;
49
50 // The rest of the information is used to help decide whether importing
51 // is likely to be profitable.
52 // Other information will be added as the importing is tuned, such
53 // as hotness (when profile available), and other function characteristics.
54
55 /// Number of instructions (ignoring debug instructions, e.g.) computed
56 /// during the initial compile step when the function index is first built.
57 unsigned InstCount;
58
59 public:
60 /// Construct a summary object from summary data expected for all
61 /// summary records.
62 FunctionSummary(unsigned NumInsts) : InstCount(NumInsts) {}
63
64 /// Set the path to the module containing this function, for use in
65 /// the combined index.
66 void setModulePath(StringRef ModPath) { ModulePath = ModPath; }
67
68 /// Get the path to the module containing this function.
69 StringRef modulePath() const { return ModulePath; }
70
71 /// Record whether this is a local function in the per-module index.
72 void setLocalFunction(bool IsLocal) { IsLocalFunction = IsLocal; }
73
74 /// Check whether this was a local function, for use in creating
75 /// the combined index.
76 bool isLocalFunction() const { return IsLocalFunction; }
77
78 /// Get the instruction count recorded for this function.
79 unsigned instCount() const { return InstCount; }
80 };
81
82 /// \brief Class to hold pointer to function summary and information required
83 /// for parsing it.
84 ///
85 /// For the per-module index, this holds the bitcode offset
86 /// of the corresponding function block. For the combined index,
87 /// after parsing of the \a ValueSymbolTable, this initially
88 /// holds the offset of the corresponding function summary bitcode
89 /// record. After parsing the associated summary information from the summary
90 /// block the \a FunctionSummary is populated and stored here.
91 class FunctionInfo {
92 private:
93 /// Function summary information used to help make ThinLTO importing
94 /// decisions.
95 std::unique_ptr Summary;
96
97 /// \brief The bitcode offset corresponding to either the associated
98 /// function's function body record, or its function summary record,
99 /// depending on whether this is a per-module or combined index.
100 ///
101 /// This bitcode offset is written to or read from the associated
102 /// \a ValueSymbolTable entry for the function.
103 /// For the per-module index this holds the bitcode offset of the
104 /// function's body record within bitcode module block in its module,
105 /// which is used during lazy function parsing or ThinLTO importing.
106 /// For the combined index this holds the offset of the corresponding
107 /// function summary record, to enable associating the combined index
108 /// VST records with the summary records.
109 uint64_t BitcodeIndex;
110
111 public:
112 /// Constructor used during parsing of VST entries.
113 FunctionInfo(uint64_t FuncOffset)
114 : Summary(nullptr), BitcodeIndex(FuncOffset) {}
115
116 /// Constructor used for per-module index bitcode writing.
117 FunctionInfo(uint64_t FuncOffset,
118 std::unique_ptr FuncSummary)
119 : Summary(std::move(FuncSummary)), BitcodeIndex(FuncOffset) {}
120
121 /// Record the function summary information parsed out of the function
122 /// summary block during parsing or combined index creation.
123 void setFunctionSummary(std::unique_ptr FuncSummary) {
124 Summary = std::move(FuncSummary);
125 }
126
127 /// Get the function summary recorded for this function.
128 FunctionSummary *functionSummary() const { return Summary.get(); }
129
130 /// Get the bitcode index recorded for this function, depending on
131 /// the index type.
132 uint64_t bitcodeIndex() const { return BitcodeIndex; }
133
134 /// Record the bitcode index for this function, depending on
135 /// the index type.
136 void setBitcodeIndex(uint64_t FuncOffset) { BitcodeIndex = FuncOffset; }
137 };
138
139 /// List of function info structures for a particular function name held
140 /// in the FunctionMap. Requires a vector in the case of multiple
141 /// COMDAT functions of the same name.
142 typedef std::vector> FunctionInfoList;
143
144 /// Map from function name to corresponding function info structures.
145 typedef StringMap FunctionInfoMapTy;
146
147 /// Type used for iterating through the function info map.
148 typedef FunctionInfoMapTy::const_iterator const_funcinfo_iterator;
149 typedef FunctionInfoMapTy::iterator funcinfo_iterator;
150
151 /// String table to hold/own module path strings, which additionally holds the
152 /// module ID assigned to each module during the plugin step. The StringMap
153 /// makes a copy of and owns inserted strings.
154 typedef StringMap ModulePathStringTableTy;
155
156 /// Class to hold module path string table and function map,
157 /// and encapsulate methods for operating on them.
158 class FunctionInfoIndex {
159 private:
160 /// Map from function name to list of function information instances
161 /// for functions of that name (may be duplicates in the COMDAT case, e.g.).
162 FunctionInfoMapTy FunctionMap;
163
164 /// Holds strings for combined index, mapping to the corresponding module ID.
165 ModulePathStringTableTy ModulePathStringTable;
166
167 public:
168 FunctionInfoIndex() = default;
169 ~FunctionInfoIndex() = default;
170
171 // Disable the copy constructor and assignment operators, so
172 // no unexpected copying/moving occurs.
173 FunctionInfoIndex(const FunctionInfoIndex &) = delete;
174 void operator=(const FunctionInfoIndex &) = delete;
175
176 funcinfo_iterator begin() { return FunctionMap.begin(); }
177 const_funcinfo_iterator begin() const { return FunctionMap.begin(); }
178 funcinfo_iterator end() { return FunctionMap.end(); }
179 const_funcinfo_iterator end() const { return FunctionMap.end(); }
180
181 /// Get the list of function info objects for a given function.
182 const FunctionInfoList &getFunctionInfoList(StringRef FuncName) {
183 return FunctionMap[FuncName];
184 }
185
186 /// Add a function info for a function of the given name.
187 void addFunctionInfo(StringRef FuncName, std::unique_ptr Info) {
188 FunctionMap[FuncName].push_back(std::move(Info));
189 }
190
191 /// Iterator to allow writer to walk through table during emission.
192 iterator_range::const_iterator> modPathStringEntries()
193 const {
194 return llvm::make_range(ModulePathStringTable.begin(),
195 ModulePathStringTable.end());
196 }
197
198 /// Get the module ID recorded for the given module path.
199 uint64_t getModuleId(const StringRef ModPath) const {
200 return ModulePathStringTable.lookup(ModPath);
201 }
202
203 /// Add the given per-module index into this function index/summary,
204 /// assigning it the given module ID. Each module merged in should have
205 /// a unique ID, necessary for consistent renaming of promoted
206 /// static (local) variables.
207 void mergeFrom(std::unique_ptr Other,
208 uint64_t NextModuleId);
209
210 /// Convenience method for creating a promoted global name
211 /// for the given value name of a local, and its original module's ID.
212 static std::string getGlobalNameForLocal(StringRef Name, uint64_t ModId) {
213 SmallString<256> NewName(Name);
214 NewName += ".llvm.";
215 raw_svector_ostream(NewName) << ModId;
216 return NewName.str();
217 }
218
219 /// Add a new module path, mapped to the given module Id, and return StringRef
220 /// owned by string table map.
221 StringRef addModulePath(StringRef ModPath, uint64_t ModId) {
222 return ModulePathStringTable.insert(std::make_pair(ModPath, ModId))
223 .first->first();
224 }
225 };
226
227 } // End llvm namespace
228
229 #endif
4242 ID_MachOUniversalBinary,
4343 ID_COFFImportFile,
4444 ID_IR, // LLVM IR
45 ID_FunctionIndex, // Function summary index
4546
4647 // Object and children.
4748 ID_StartObjects,
121122 return TypeID == ID_IR;
122123 }
123124
125 bool isFunctionIndex() const {
126 return TypeID == ID_FunctionIndex;
127 }
128
124129 bool isLittleEndian() const {
125130 return !(TypeID == ID_ELF32B || TypeID == ID_ELF64B ||
126131 TypeID == ID_MachO32B || TypeID == ID_MachO64B);
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/Object/SymbolicFile.h"
17
18 namespace llvm {
19 class FunctionInfoIndex;
20
21 namespace object {
22 class ObjectFile;
23
24 /// This class is used to read just the function summary index related
25 /// sections out of the given object (which may contain a single module's
26 /// bitcode or be a combined index bitcode file). It builds a FunctionInfoIndex
27 /// object.
28 class FunctionIndexObjectFile : public SymbolicFile {
29 std::unique_ptr Index;
30
31 public:
32 FunctionIndexObjectFile(MemoryBufferRef Object,
33 std::unique_ptr I);
34 ~FunctionIndexObjectFile() override;
35
36 // TODO: Walk through FunctionMap entries for function symbols.
37 // However, currently these interfaces are not used by any consumers.
38 void moveSymbolNext(DataRefImpl &Symb) const override {
39 llvm_unreachable("not implemented");
40 }
41 std::error_code printSymbolName(raw_ostream &OS,
42 DataRefImpl Symb) const override {
43 llvm_unreachable("not implemented");
44 return std::error_code();
45 }
46 uint32_t getSymbolFlags(DataRefImpl Symb) const override {
47 llvm_unreachable("not implemented");
48 return 0;
49 }
50 basic_symbol_iterator symbol_begin_impl() const override {
51 llvm_unreachable("not implemented");
52 return basic_symbol_iterator(BasicSymbolRef());
53 }
54 basic_symbol_iterator symbol_end_impl() const override {
55 llvm_unreachable("not implemented");
56 return basic_symbol_iterator(BasicSymbolRef());
57 }
58
59 const FunctionInfoIndex &getIndex() const {
60 return const_cast(this)->getIndex();
61 }
62 FunctionInfoIndex &getIndex() { return *Index; }
63 std::unique_ptr takeIndex();
64
65 static inline bool classof(const Binary *v) { return v->isFunctionIndex(); }
66
67 /// \brief Finds and returns bitcode embedded in the given object file, or an
68 /// error code if not found.
69 static ErrorOr findBitcodeInObject(const ObjectFile &Obj);
70
71 /// \brief Finds and returns bitcode in the given memory buffer (which may
72 /// be either a bitcode file or a native object file with embedded bitcode),
73 /// or an error code if not found.
74 static ErrorOr findBitcodeInMemBuffer(
75 MemoryBufferRef Object);
76
77 /// \brief Looks for function summary in the given memory buffer,
78 /// returns true if found, else false.
79 static bool hasFunctionSummaryInMemBuffer(MemoryBufferRef Object,
80 LLVMContext &Context);
81
82 /// \brief Parse function index in the given memory buffer.
83 /// Return new FunctionIndexObjectFile instance containing parsed function
84 /// summary/index.
85 static ErrorOr> create(
86 MemoryBufferRef Object, LLVMContext &Context, bool IsLazy = false);
87
88 /// \brief Parse the function summary information for function with the
89 /// given name out of the given buffer. Parsed information is
90 /// stored on the index object saved in this object.
91 std::error_code findFunctionSummaryInMemBuffer(MemoryBufferRef Object,
92 LLVMContext &Context,
93 StringRef FunctionName);
94 };
95 }
96 }
97
98 #endif
2626 #include "llvm/IR/Module.h"
2727 #include "llvm/IR/OperandTraits.h"
2828 #include "llvm/IR/Operator.h"
29 #include "llvm/IR/FunctionInfo.h"
2930 #include "llvm/IR/ValueHandle.h"
3031 #include "llvm/Support/DataStream.h"
3132 #include "llvm/Support/ManagedStatic.h"
393394 std::error_code findFunctionInStream(
394395 Function *F,
395396 DenseMap::iterator DeferredFunctionInfoIterator);
397 };
398
399 /// Class to manage reading and parsing function summary index bitcode
400 /// files/sections.
401 class FunctionIndexBitcodeReader {
402 LLVMContext &Context;
403 DiagnosticHandlerFunction DiagnosticHandler;
404
405 /// Eventually points to the function index built during parsing.
406 FunctionInfoIndex *TheIndex = nullptr;
407
408 std::unique_ptr Buffer;
409 std::unique_ptr StreamFile;
410 BitstreamCursor Stream;
411
412 /// \brief Used to indicate whether we are doing lazy parsing of summary data.
413 ///
414 /// If false, the summary section is fully parsed into the index during
415 /// the initial parse. Otherwise, if true, the caller is expected to
416 /// invoke \a readFunctionSummary for each summary needed, and the summary
417 /// section is thus parsed lazily.
418 bool IsLazy = false;
419
420 /// Used to indicate whether caller only wants to check for the presence
421 /// of the function summary bitcode section. All blocks are skipped,
422 /// but the SeenFuncSummary boolean is set.
423 bool CheckFuncSummaryPresenceOnly = false;
424
425 /// Indicates whether we have encountered a function summary section
426 /// yet during parsing, used when checking if file contains function
427 /// summary section.
428 bool SeenFuncSummary = false;
429
430 /// \brief Map populated during function summary section parsing, and
431 /// consumed during ValueSymbolTable parsing.
432 ///
433 /// Used to correlate summary records with VST entries. For the per-module
434 /// index this maps the ValueID to the parsed function summary, and
435 /// for the combined index this maps the summary record's bitcode
436 /// offset to the function summary (since in the combined index the
437 /// VST records do not hold value IDs but rather hold the function
438 /// summary record offset).
439 DenseMap> SummaryMap;
440
441 /// Map populated during module path string table parsing, from the
442 /// module ID to a string reference owned by the index's module
443 /// path string table, used to correlate with combined index function
444 /// summary records.
445 DenseMap ModuleIdMap;
446
447 public:
448 std::error_code error(BitcodeError E, const Twine &Message);
449 std::error_code error(BitcodeError E);
450 std::error_code error(const Twine &Message);
451
452 FunctionIndexBitcodeReader(MemoryBuffer *Buffer, LLVMContext &Context,
453 DiagnosticHandlerFunction DiagnosticHandler,
454 bool IsLazy = false,
455 bool CheckFuncSummaryPresenceOnly = false);
456 FunctionIndexBitcodeReader(LLVMContext &Context,
457 DiagnosticHandlerFunction DiagnosticHandler,
458 bool IsLazy = false,
459 bool CheckFuncSummaryPresenceOnly = false);
460 ~FunctionIndexBitcodeReader() { freeState(); }
461
462 void freeState();
463
464 void releaseBuffer();
465
466 /// Check if the parser has encountered a function summary section.
467 bool foundFuncSummary() { return SeenFuncSummary; }
468
469 /// \brief Main interface to parsing a bitcode buffer.
470 /// \returns true if an error occurred.
471 std::error_code parseSummaryIndexInto(std::unique_ptr Streamer,
472 FunctionInfoIndex *I);
473
474 /// \brief Interface for parsing a function summary lazily.
475 std::error_code parseFunctionSummary(std::unique_ptr Streamer,
476 FunctionInfoIndex *I,
477 size_t FunctionSummaryOffset);
478
479 private:
480 std::error_code parseModule();
481 std::error_code parseValueSymbolTable();
482 std::error_code parseEntireSummary();
483 std::error_code parseModuleStringTable();
484 std::error_code initStream(std::unique_ptr Streamer);
485 std::error_code initStreamFromBuffer();
486 std::error_code initLazyStream(std::unique_ptr Streamer);
396487 };
397488 } // namespace
398489
33763467 }
33773468 }
33783469
3379 std::error_code
3380 BitcodeReader::parseBitcodeInto(std::unique_ptr Streamer,
3381 Module *M, bool ShouldLazyLoadMetadata) {
3382 TheModule = M;
3383
3384 if (std::error_code EC = initStream(std::move(Streamer)))
3385 return EC;
3386
3470 /// Helper to read the header common to all bitcode files.
3471 static bool hasValidBitcodeHeader(BitstreamCursor &Stream) {
33873472 // Sniff for the signature.
33883473 if (Stream.Read(8) != 'B' ||
33893474 Stream.Read(8) != 'C' ||
33913476 Stream.Read(4) != 0xC ||
33923477 Stream.Read(4) != 0xE ||
33933478 Stream.Read(4) != 0xD)
3394 return error("Invalid bitcode signature");
3479 return false;
3480 return true;
3481 }
3482
3483 std::error_code
3484 BitcodeReader::parseBitcodeInto(std::unique_ptr Streamer,
3485 Module *M, bool ShouldLazyLoadMetadata) {
3486 TheModule = M;
3487
3488 if (std::error_code EC = initStream(std::move(Streamer)))
3489 return EC;
3490
3491 // Sniff for the signature.
3492 if (!hasValidBitcodeHeader(Stream)) return error("Invalid bitcode signature");
33953493
33963494 // We expect a number of well-defined blocks, though we don't necessarily
33973495 // need to understand them all.
34583556 return EC;
34593557
34603558 // Sniff for the signature.
3461 if (Stream.Read(8) != 'B' ||
3462 Stream.Read(8) != 'C' ||
3463 Stream.Read(4) != 0x0 ||
3464 Stream.Read(4) != 0xC ||
3465 Stream.Read(4) != 0xE ||
3466 Stream.Read(4) != 0xD)
3467 return error("Invalid bitcode signature");
3559 if (!hasValidBitcodeHeader(Stream)) return error("Invalid bitcode signature");
34683560
34693561 // We expect a number of well-defined blocks, though we don't necessarily
34703562 // need to understand them all.
50595151 return std::error_code();
50605152 }
50615153
5154 std::error_code FunctionIndexBitcodeReader::error(BitcodeError E,
5155 const Twine &Message) {
5156 return ::error(DiagnosticHandler, make_error_code(E), Message);
5157 }
5158
5159 std::error_code FunctionIndexBitcodeReader::error(const Twine &Message) {
5160 return ::error(DiagnosticHandler,
5161 make_error_code(BitcodeError::CorruptedBitcode), Message);
5162 }
5163
5164 std::error_code FunctionIndexBitcodeReader::error(BitcodeError E) {
5165 return ::error(DiagnosticHandler, make_error_code(E));
5166 }
5167
5168 FunctionIndexBitcodeReader::FunctionIndexBitcodeReader(
5169 MemoryBuffer *Buffer, LLVMContext &Context,
5170 DiagnosticHandlerFunction DiagnosticHandler, bool IsLazy,
5171 bool CheckFuncSummaryPresenceOnly)
5172 : Context(Context),
5173 DiagnosticHandler(getDiagHandler(DiagnosticHandler, Context)),
5174 Buffer(Buffer),
5175 IsLazy(IsLazy),
5176 CheckFuncSummaryPresenceOnly(CheckFuncSummaryPresenceOnly) {}
5177
5178 FunctionIndexBitcodeReader::FunctionIndexBitcodeReader(
5179 LLVMContext &Context, DiagnosticHandlerFunction DiagnosticHandler,
5180 bool IsLazy, bool CheckFuncSummaryPresenceOnly)
5181 : Context(Context),
5182 DiagnosticHandler(getDiagHandler(DiagnosticHandler, Context)),
5183 Buffer(nullptr),
5184 IsLazy(IsLazy),
5185 CheckFuncSummaryPresenceOnly(CheckFuncSummaryPresenceOnly) {}
5186
5187 void FunctionIndexBitcodeReader::freeState() { Buffer = nullptr; }
5188
5189 void FunctionIndexBitcodeReader::releaseBuffer() { Buffer.release(); }
5190
5191 // Specialized value symbol table parser used when reading function index
5192 // blocks where we don't actually create global values.
5193 // At the end of this routine the function index is populated with a map
5194 // from function name to FunctionInfo. The function info contains
5195 // the function block's bitcode offset as well as the offset into the
5196 // function summary section.
5197 std::error_code FunctionIndexBitcodeReader::parseValueSymbolTable() {
5198 if (Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID))
5199 return error("Invalid record");
5200
5201 SmallVector Record;
5202
5203 // Read all the records for this value table.
5204 SmallString<128> ValueName;
5205 while (1) {
5206 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
5207
5208 switch (Entry.Kind) {
5209 case BitstreamEntry::SubBlock: // Handled for us already.
5210 case BitstreamEntry::Error:
5211 return error("Malformed block");
5212 case BitstreamEntry::EndBlock:
5213 return std::error_code();
5214 case BitstreamEntry::Record:
5215 // The interesting case.
5216 break;
5217 }
5218
5219 // Read a record.
5220 Record.clear();
5221 switch (Stream.readRecord(Entry.ID, Record)) {
5222 default: // Default behavior: ignore (e.g. VST_CODE_BBENTRY records).
5223 break;
5224 case bitc::VST_CODE_FNENTRY: {
5225 // VST_FNENTRY: [valueid, offset, namechar x N]
5226 if (convertToString(Record, 2, ValueName))
5227 return error("Invalid record");
5228 unsigned ValueID = Record[0];
5229 uint64_t FuncOffset = Record[1];
5230 std::unique_ptr FuncInfo =
5231 llvm::make_unique(FuncOffset);
5232 if (foundFuncSummary() && !IsLazy) {
5233 DenseMap>::iterator SMI =
5234 SummaryMap.find(ValueID);
5235 assert(SMI != SummaryMap.end() && "Summary info not found");
5236 FuncInfo->setFunctionSummary(std::move(SMI->second));
5237 }
5238 TheIndex->addFunctionInfo(ValueName, std::move(FuncInfo));
5239
5240 ValueName.clear();
5241 break;
5242 }
5243 case bitc::VST_CODE_COMBINED_FNENTRY: {
5244 // VST_FNENTRY: [offset, namechar x N]
5245 if (convertToString(Record, 1, ValueName))
5246 return error("Invalid record");
5247 uint64_t FuncSummaryOffset = Record[0];
5248 std::unique_ptr FuncInfo =
5249 llvm::make_unique(FuncSummaryOffset);
5250 if (foundFuncSummary() && !IsLazy) {
5251 DenseMap>::iterator SMI =
5252 SummaryMap.find(FuncSummaryOffset);
5253 assert(SMI != SummaryMap.end() && "Summary info not found");
5254 FuncInfo->setFunctionSummary(std::move(SMI->second));
5255 }
5256 TheIndex->addFunctionInfo(ValueName, std::move(FuncInfo));
5257
5258 ValueName.clear();
5259 break;
5260 }
5261 }
5262 }
5263 }
5264
5265 // Parse just the blocks needed for function index building out of the module.
5266 // At the end of this routine the function Index is populated with a map
5267 // from function name to FunctionInfo. The function info contains
5268 // either the parsed function summary information (when parsing summaries
5269 // eagerly), or just to the function summary record's offset
5270 // if parsing lazily (IsLazy).
5271 std::error_code FunctionIndexBitcodeReader::parseModule() {
5272 if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
5273 return error("Invalid record");
5274
5275 // Read the function index for this module.
5276 while (1) {
5277 BitstreamEntry Entry = Stream.advance();
5278
5279 switch (Entry.Kind) {
5280 case BitstreamEntry::Error:
5281 return error("Malformed block");
5282 case BitstreamEntry::EndBlock:
5283 return std::error_code();
5284
5285 case BitstreamEntry::SubBlock:
5286 if (CheckFuncSummaryPresenceOnly) {
5287 if (Entry.ID == bitc::FUNCTION_SUMMARY_BLOCK_ID)
5288 SeenFuncSummary = true;
5289 if (Stream.SkipBlock()) return error("Invalid record");
5290 // No need to parse the rest since we found the summary.
5291 return std::error_code();
5292 }
5293 switch (Entry.ID) {
5294 default: // Skip unknown content.
5295 if (Stream.SkipBlock()) return error("Invalid record");
5296 break;
5297 case bitc::BLOCKINFO_BLOCK_ID:
5298 // Need to parse these to get abbrev ids (e.g. for VST)
5299 if (Stream.ReadBlockInfoBlock()) return error("Malformed block");
5300 break;
5301 case bitc::VALUE_SYMTAB_BLOCK_ID:
5302 if (std::error_code EC = parseValueSymbolTable()) return EC;
5303 break;
5304 case bitc::FUNCTION_SUMMARY_BLOCK_ID:
5305 SeenFuncSummary = true;
5306 if (IsLazy) {
5307 // Lazy parsing of summary info, skip it.
5308 if (Stream.SkipBlock()) return error("Invalid record");
5309 } else if (std::error_code EC = parseEntireSummary())
5310 return EC;
5311 break;
5312 case bitc::MODULE_STRTAB_BLOCK_ID:
5313 if (std::error_code EC = parseModuleStringTable()) return EC;
5314 break;
5315 }
5316 continue;
5317
5318 case BitstreamEntry::Record:
5319 Stream.skipRecord(Entry.ID);
5320 continue;
5321 }
5322 }
5323 }
5324
5325 // Eagerly parse the entire function summary block (i.e. for all functions
5326 // in the index). This populates the FunctionSummary objects in
5327 // the index.
5328 std::error_code FunctionIndexBitcodeReader::parseEntireSummary() {
5329 if (Stream.EnterSubBlock(bitc::FUNCTION_SUMMARY_BLOCK_ID))
5330 return error("Invalid record");
5331
5332 SmallVector Record;
5333
5334 while (1) {
5335 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
5336
5337 switch (Entry.Kind) {
5338 case BitstreamEntry::SubBlock: // Handled for us already.
5339 case BitstreamEntry::Error:
5340 return error("Malformed block");
5341 case BitstreamEntry::EndBlock:
5342 return std::error_code();
5343 case BitstreamEntry::Record:
5344 // The interesting case.
5345 break;
5346 }
5347
5348 // Read a record. The record format depends on whether this
5349 // is a per-module index or a combined index file. In the per-module
5350 // case the records contain the associated value's ID for correlation
5351 // with VST entries. In the combined index the correlation is done
5352 // via the bitcode offset of the summary records (which were saved
5353 // in the combined index VST entries). The records also contain
5354 // information used for ThinLTO renaming and importing.
5355 Record.clear();
5356 uint64_t CurRecordBit = Stream.GetCurrentBitNo();
5357 switch (Stream.readRecord(Entry.ID, Record)) {
5358 default: // Default behavior: ignore.
5359 break;
5360 // FS_PERMODULE_ENTRY: [valueid, islocal, instcount]
5361 case bitc::FS_CODE_PERMODULE_ENTRY: {
5362 unsigned ValueID = Record[0];
5363 bool IsLocal = Record[1];
5364 unsigned InstCount = Record[2];
5365 std::unique_ptr FS =
5366 llvm::make_unique(InstCount);
5367 FS->setLocalFunction(IsLocal);
5368 // The module path string ref set in the summary must be owned by the
5369 // index's module string table. Since we don't have a module path
5370 // string table section in the per-module index, we create a single
5371 // module path string table entry with an empty (0) ID to take
5372 // ownership.
5373 FS->setModulePath(
5374 TheIndex->addModulePath(Buffer->getBufferIdentifier(), 0));
5375 SummaryMap[ValueID] = std::move(FS);
5376 }
5377 // FS_COMBINED_ENTRY: [modid, instcount]
5378 case bitc::FS_CODE_COMBINED_ENTRY: {
5379 uint64_t ModuleId = Record[0];
5380 unsigned InstCount = Record[1];
5381 std::unique_ptr FS =
5382 llvm::make_unique(InstCount);
5383 FS->setModulePath(ModuleIdMap[ModuleId]);
5384 SummaryMap[CurRecordBit] = std::move(FS);
5385 }
5386 }
5387 }
5388 llvm_unreachable("Exit infinite loop");
5389 }
5390
5391 // Parse the module string table block into the Index.
5392 // This populates the ModulePathStringTable map in the index.
5393 std::error_code FunctionIndexBitcodeReader::parseModuleStringTable() {
5394 if (Stream.EnterSubBlock(bitc::MODULE_STRTAB_BLOCK_ID))
5395 return error("Invalid record");
5396
5397 SmallVector Record;
5398
5399 SmallString<128> ModulePath;
5400 while (1) {
5401 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
5402
5403 switch (Entry.Kind) {
5404 case BitstreamEntry::SubBlock: // Handled for us already.
5405 case BitstreamEntry::Error:
5406 return error("Malformed block");
5407 case BitstreamEntry::EndBlock:
5408 return std::error_code();
5409 case BitstreamEntry::Record:
5410 // The interesting case.
5411 break;
5412 }
5413
5414 Record.clear();
5415 switch (Stream.readRecord(Entry.ID, Record)) {
5416 default: // Default behavior: ignore.
5417 break;
5418 case bitc::MST_CODE_ENTRY: {
5419 // MST_ENTRY: [modid, namechar x N]
5420 if (convertToString(Record, 1, ModulePath))
5421 return error("Invalid record");
5422 uint64_t ModuleId = Record[0];
5423 StringRef ModulePathInMap =
5424 TheIndex->addModulePath(ModulePath, ModuleId);
5425 ModuleIdMap[ModuleId] = ModulePathInMap;
5426 ModulePath.clear();
5427 break;
5428 }
5429 }
5430 }
5431 llvm_unreachable("Exit infinite loop");
5432 }
5433
5434 // Parse the function info index from the bitcode streamer into the given index.
5435 std::error_code FunctionIndexBitcodeReader::parseSummaryIndexInto(
5436 std::unique_ptr Streamer, FunctionInfoIndex *I) {
5437 TheIndex = I;
5438
5439 if (std::error_code EC = initStream(std::move(Streamer))) return EC;
5440
5441 // Sniff for the signature.
5442 if (!hasValidBitcodeHeader(Stream)) return error("Invalid bitcode signature");
5443
5444 // We expect a number of well-defined blocks, though we don't necessarily
5445 // need to understand them all.
5446 while (1) {
5447 if (Stream.AtEndOfStream()) {
5448 // We didn't really read a proper Module block.
5449 return error("Malformed block");
5450 }
5451
5452 BitstreamEntry Entry =
5453 Stream.advance(BitstreamCursor::AF_DontAutoprocessAbbrevs);
5454
5455 if (Entry.Kind != BitstreamEntry::SubBlock) return error("Malformed block");
5456
5457 // If we see a MODULE_BLOCK, parse it to find the blocks needed for
5458 // building the function summary index.
5459 if (Entry.ID == bitc::MODULE_BLOCK_ID) return parseModule();
5460
5461 if (Stream.SkipBlock()) return error("Invalid record");
5462 }
5463 }
5464
5465 // Parse the function information at the given offset in the buffer into
5466 // the index. Used to support lazy parsing of function summaries from the
5467 // combined index during importing.
5468 // TODO: This function is not yet complete as it won't have a consumer
5469 // until ThinLTO function importing is added.
5470 std::error_code FunctionIndexBitcodeReader::parseFunctionSummary(
5471 std::unique_ptr Streamer, FunctionInfoIndex *I,
5472 size_t FunctionSummaryOffset) {
5473 TheIndex = I;
5474
5475 if (std::error_code EC = initStream(std::move(Streamer))) return EC;
5476
5477 // Sniff for the signature.
5478 if (!hasValidBitcodeHeader(Stream)) return error("Invalid bitcode signature");
5479
5480 Stream.JumpToBit(FunctionSummaryOffset);
5481
5482 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
5483
5484 switch (Entry.Kind) {
5485 default:
5486 return error("Malformed block");
5487 case BitstreamEntry::Record:
5488 // The expected case.
5489 break;
5490 }
5491
5492 // TODO: Read a record. This interface will be completed when ThinLTO
5493 // importing is added so that it can be tested.
5494 SmallVector Record;
5495 switch (Stream.readRecord(Entry.ID, Record)) {
5496 default:
5497 return error("Invalid record");
5498 }
5499
5500 return std::error_code();
5501 }
5502
5503 std::error_code FunctionIndexBitcodeReader::initStream(
5504 std::unique_ptr Streamer) {
5505 if (Streamer) return initLazyStream(std::move(Streamer));
5506 return initStreamFromBuffer();
5507 }
5508
5509 std::error_code FunctionIndexBitcodeReader::initStreamFromBuffer() {
5510 const unsigned char *BufPtr = (const unsigned char *)Buffer->getBufferStart();
5511 const unsigned char *BufEnd = BufPtr + Buffer->getBufferSize();
5512
5513 if (Buffer->getBufferSize() & 3) return error("Invalid bitcode signature");
5514
5515 // If we have a wrapper header, parse it and ignore the non-bc file contents.
5516 // The magic number is 0x0B17C0DE stored in little endian.
5517 if (isBitcodeWrapper(BufPtr, BufEnd))
5518 if (SkipBitcodeWrapperHeader(BufPtr, BufEnd, true))
5519 return error("Invalid bitcode wrapper header");
5520
5521 StreamFile.reset(new BitstreamReader(BufPtr, BufEnd));
5522 Stream.init(&*StreamFile);
5523
5524 return std::error_code();
5525 }
5526
5527 std::error_code FunctionIndexBitcodeReader::initLazyStream(
5528 std::unique_ptr Streamer) {
5529 // Check and strip off the bitcode wrapper; BitstreamReader expects never to
5530 // see it.
5531 auto OwnedBytes =
5532 llvm::make_unique(std::move(Streamer));
5533 StreamingMemoryObject &Bytes = *OwnedBytes;
5534 StreamFile = llvm::make_unique(std::move(OwnedBytes));
5535 Stream.init(&*StreamFile);
5536
5537 unsigned char buf[16];
5538 if (Bytes.readBytes(buf, 16, 0) != 16)
5539 return error("Invalid bitcode signature");
5540
5541 if (!isBitcode(buf, buf + 16)) return error("Invalid bitcode signature");
5542
5543 if (isBitcodeWrapper(buf, buf + 4)) {
5544 const unsigned char *bitcodeStart = buf;
5545 const unsigned char *bitcodeEnd = buf + 16;
5546 SkipBitcodeWrapperHeader(bitcodeStart, bitcodeEnd, false);
5547 Bytes.dropLeadingBytes(bitcodeStart - buf);
5548 Bytes.setKnownObjectSize(bitcodeEnd - bitcodeStart);
5549 }
5550 return std::error_code();
5551 }
5552
50625553 namespace {
50635554 class BitcodeErrorCategoryType : public std::error_category {
50645555 const char *name() const LLVM_NOEXCEPT override {
51805671 return "";
51815672 return Triple.get();
51825673 }
5674
5675 // Parse the specified bitcode buffer, returning the function info index.
5676 // If IsLazy is false, parse the entire function summary into
5677 // the index. Otherwise skip the function summary section, and only create
5678 // an index object with a map from function name to function summary offset.
5679 // The index is used to perform lazy function summary reading later.
5680 ErrorOr> llvm::getFunctionInfoIndex(
5681 MemoryBufferRef Buffer, LLVMContext &Context,
5682 DiagnosticHandlerFunction DiagnosticHandler, bool IsLazy) {
5683 std::unique_ptr Buf = MemoryBuffer::getMemBuffer(Buffer, false);
5684 FunctionIndexBitcodeReader R(Buf.get(), Context, DiagnosticHandler, IsLazy);
5685
5686 std::unique_ptr Index =
5687 llvm::make_unique();
5688
5689 auto cleanupOnError = [&](std::error_code EC) {
5690 R.releaseBuffer(); // Never take ownership on error.
5691 return EC;
5692 };
5693
5694 if (std::error_code EC = R.parseSummaryIndexInto(nullptr, Index.get()))
5695 return cleanupOnError(EC);
5696
5697 Buf.release(); // The FunctionIndexBitcodeReader owns it now.
5698 return std::move(Index);
5699 }
5700
5701 // Check if the given bitcode buffer contains a function summary block.
5702 bool llvm::hasFunctionSummary(MemoryBufferRef Buffer, LLVMContext &Context,
5703 DiagnosticHandlerFunction DiagnosticHandler) {
5704 std::unique_ptr Buf = MemoryBuffer::getMemBuffer(Buffer, false);
5705 FunctionIndexBitcodeReader R(Buf.get(), Context, DiagnosticHandler, false,
5706 true);
5707
5708 auto cleanupOnError = [&](std::error_code EC) {
5709 R.releaseBuffer(); // Never take ownership on error.
5710 return false;
5711 };
5712
5713 if (std::error_code EC = R.parseSummaryIndexInto(nullptr, nullptr))
5714 return cleanupOnError(EC);
5715
5716 Buf.release(); // The FunctionIndexBitcodeReader owns it now.
5717 return R.foundFuncSummary();
5718 }
5719
5720 // This method supports lazy reading of function summary data from the combined
5721 // index during ThinLTO function importing. When reading the combined index
5722 // file, getFunctionInfoIndex is first invoked with IsLazy=true.
5723 // Then this method is called for each function considered for importing,
5724 // to parse the summary information for the given function name into
5725 // the index.
5726 std::error_code llvm::readFunctionSummary(
5727 MemoryBufferRef Buffer, LLVMContext &Context,
5728 DiagnosticHandlerFunction DiagnosticHandler, StringRef FunctionName,
5729 std::unique_ptr Index) {
5730 std::unique_ptr Buf = MemoryBuffer::getMemBuffer(Buffer, false);
5731 FunctionIndexBitcodeReader R(Buf.get(), Context, DiagnosticHandler);
5732
5733 auto cleanupOnError = [&](std::error_code EC) {
5734 R.releaseBuffer(); // Never take ownership on error.
5735 return EC;
5736 };
5737
5738 // Lookup the given function name in the FunctionMap, which may
5739 // contain a list of function infos in the case of a COMDAT. Walk through
5740 // and parse each function summary info at the function summary offset
5741 // recorded when parsing the value symbol table.
5742 for (const auto &FI : Index->getFunctionInfoList(FunctionName)) {
5743 size_t FunctionSummaryOffset = FI->bitcodeIndex();
5744 if (std::error_code EC =
5745 R.parseFunctionSummary(nullptr, Index.get(), FunctionSummaryOffset))
5746 return cleanupOnError(EC);
5747 }
5748
5749 Buf.release(); // The FunctionIndexBitcodeReader owns it now.
5750 return std::error_code();
5751 }
1212
1313 #include "llvm/Bitcode/ReaderWriter.h"
1414 #include "ValueEnumerator.h"
15 #include "llvm/ADT/STLExtras.h"
1516 #include "llvm/ADT/Triple.h"
1617 #include "llvm/Bitcode/BitstreamWriter.h"
1718 #include "llvm/Bitcode/LLVMBitCodes.h"
2223 #include "llvm/IR/InlineAsm.h"
2324 #include "llvm/IR/Instructions.h"
2425 #include "llvm/IR/LLVMContext.h"
26 #include "llvm/IR/IntrinsicInst.h"
2527 #include "llvm/IR/Module.h"
2628 #include "llvm/IR/Operator.h"
2729 #include "llvm/IR/UseListOrder.h"
21862188 const ValueSymbolTable &VST, const ValueEnumerator &VE,
21872189 BitstreamWriter &Stream, uint64_t VSTOffsetPlaceholder = 0,
21882190 uint64_t BitcodeStartBit = 0,
2189 DenseMapuint64_t> *FunctionIndex = nullptr) {
2191 DenseMapstd::unique_ptr> *FunctionIndex =
2192 nullptr) {
21902193 if (VST.empty()) {
21912194 // WriteValueSymbolTableForwardDecl should have returned early as
21922195 // well. Ensure this handling remains in sync by asserting that
22812284 // Save the word offset of the function (from the start of the
22822285 // actual bitcode written to the stream).
22832286 assert(FunctionIndex->count(F) == 1);
2284 uint64_t BitcodeIndex = (*FunctionIndex)[F] - BitcodeStartBit;
2287 uint64_t BitcodeIndex =
2288 (*FunctionIndex)[F]->bitcodeIndex() - BitcodeStartBit;
22852289 assert((BitcodeIndex & 31) == 0 && "function block not 32-bit aligned");
22862290 NameVals.push_back(BitcodeIndex / 32);
22872291
22992303 AbbrevToUse = VST_ENTRY_7_ABBREV;
23002304 }
23012305
2302 for (const char *P = Name.getKeyData(),
2303 *E = Name.getKeyData()+Name.getKeyLength(); P != E; ++P)
2304 NameVals.push_back((unsigned char)*P);
2306 for (const auto P : Name.getKey()) NameVals.push_back((unsigned char)P);
23052307
23062308 // Emit the finished record.
23072309 Stream.EmitRecord(Code, NameVals, AbbrevToUse);
23082310 NameVals.clear();
2311 }
2312 Stream.ExitBlock();
2313 }
2314
2315 /// Emit function names and summary offsets for the combined index
2316 /// used by ThinLTO.
2317 static void WriteCombinedValueSymbolTable(const FunctionInfoIndex *Index,
2318 BitstreamWriter &Stream) {
2319 Stream.EnterSubblock(bitc::VALUE_SYMTAB_BLOCK_ID, 4);
2320
2321 // 8-bit fixed-width VST_COMBINED_FNENTRY function strings.
2322 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2323 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_COMBINED_FNENTRY));
2324 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id
2325 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // funcoffset
2326 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2327 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
2328 unsigned FnEntry8BitAbbrev = Stream.EmitAbbrev(Abbv);
2329
2330 // 7-bit fixed width VST_COMBINED_FNENTRY function strings.
2331 Abbv = new BitCodeAbbrev();
2332 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_COMBINED_FNENTRY));
2333 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id
2334 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // funcoffset
2335 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2336 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
2337 unsigned FnEntry7BitAbbrev = Stream.EmitAbbrev(Abbv);
2338
2339 // 6-bit char6 VST_COMBINED_FNENTRY function strings.
2340 Abbv = new BitCodeAbbrev();
2341 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_COMBINED_FNENTRY));
2342 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id
2343 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // funcoffset
2344 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2345 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
2346 unsigned FnEntry6BitAbbrev = Stream.EmitAbbrev(Abbv);
2347
2348 // FIXME: We know if the type names can use 7-bit ascii.
2349 SmallVector NameVals;
2350
2351 for (const auto &FII : *Index) {
2352 for (const auto &FI : FII.getValue()) {
2353 NameVals.push_back(FI->bitcodeIndex());
2354
2355 StringRef FuncName = FII.first();
2356
2357 // Figure out the encoding to use for the name.
2358 StringEncoding Bits = getStringEncoding(FuncName.data(), FuncName.size());
2359
2360 // VST_COMBINED_FNENTRY: [funcsumoffset, namechar x N]
2361 unsigned AbbrevToUse = FnEntry8BitAbbrev;
2362 if (Bits == SE_Char6)
2363 AbbrevToUse = FnEntry6BitAbbrev;
2364 else if (Bits == SE_Fixed7)
2365 AbbrevToUse = FnEntry7BitAbbrev;
2366
2367 for (const auto P : FuncName) NameVals.push_back((unsigned char)P);
2368
2369 // Emit the finished record.
2370 Stream.EmitRecord(bitc::VST_CODE_COMBINED_FNENTRY, NameVals, AbbrevToUse);
2371 NameVals.clear();
2372 }
23092373 }
23102374 Stream.ExitBlock();
23112375 }
23442408 Stream.ExitBlock();
23452409 }
23462410
2347 /// WriteFunction - Emit a function body to the module stream.
2348 static void WriteFunction(const Function &F, ValueEnumerator &VE,
2349 BitstreamWriter &Stream,
2350 DenseMap &FunctionIndex) {
2411 /// \brief Save information for the given function into the function index.
2412 ///
2413 /// At a minimum this saves the bitcode index of the function record that
2414 /// was just written. However, if we are emitting function summary information,
2415 /// for example for ThinLTO, then a \a FunctionSummary object is created
2416 /// to hold the provided summary information.
2417 static void SaveFunctionInfo(
2418 const Function &F,
2419 DenseMap> &FunctionIndex,
2420 unsigned NumInsts, uint64_t BitcodeIndex, bool EmitFunctionSummary) {
2421 std::unique_ptr FuncSummary;
2422 if (EmitFunctionSummary) {
2423 FuncSummary = llvm::make_unique(NumInsts);
2424 FuncSummary->setLocalFunction(F.hasLocalLinkage());
2425 }
2426 FunctionIndex[&F] =
2427 llvm::make_unique(BitcodeIndex, std::move(FuncSummary));
2428 }
2429
2430 /// Emit a function body to the module stream.
2431 static void WriteFunction(
2432 const Function &F, ValueEnumerator &VE, BitstreamWriter &Stream,
2433 DenseMap> &FunctionIndex,
2434 bool EmitFunctionSummary) {
23512435 // Save the bitcode index of the start of this function block for recording
23522436 // in the VST.
23532437 uint64_t BitcodeIndex = Stream.GetCurrentBitNo();
2354 FunctionIndex[&F] = BitcodeIndex;
23552438
23562439 Stream.EnterSubblock(bitc::FUNCTION_BLOCK_ID, 4);
23572440 VE.incorporateFunction(F);
23782461 bool NeedsMetadataAttachment = F.hasMetadata();
23792462
23802463 DILocation *LastDL = nullptr;
2464 unsigned NumInsts = 0;
23812465
23822466 // Finally, emit all the instructions, in order.
23832467 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
23842468 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
23852469 I != E; ++I) {
23862470 WriteInstruction(*I, InstID, VE, Stream, Vals);
2471
2472 if (!isa(I)) ++NumInsts;
23872473
23882474 if (!I->getType()->isVoidTy())
23892475 ++InstID;
24212507 WriteUseListBlock(&F, VE, Stream);
24222508 VE.purgeFunction();
24232509 Stream.ExitBlock();
2510
2511 SaveFunctionInfo(F, FunctionIndex, NumInsts, BitcodeIndex,
2512 EmitFunctionSummary);
24242513 }
24252514
24262515 // Emit blockinfo, which defines the standard abbreviations etc.
25982687 Stream.ExitBlock();
25992688 }
26002689
2690 /// Write the module path strings, currently only used when generating
2691 /// a combined index file.
2692 static void WriteModStrings(const FunctionInfoIndex *I,
2693 BitstreamWriter &Stream) {
2694 Stream.EnterSubblock(bitc::MODULE_STRTAB_BLOCK_ID, 3);
2695
2696 // TODO: See which abbrev sizes we actually need to emit
2697
2698 // 8-bit fixed-width MST_ENTRY strings.
2699 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2700 Abbv->Add(BitCodeAbbrevOp(bitc::MST_CODE_ENTRY));
2701 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
2702 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2703 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
2704 unsigned Abbrev8Bit = Stream.EmitAbbrev(Abbv);
2705
2706 // 7-bit fixed width MST_ENTRY strings.
2707 Abbv = new BitCodeAbbrev();
2708 Abbv->Add(BitCodeAbbrevOp(bitc::MST_CODE_ENTRY));
2709 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
2710 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2711 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
2712 unsigned Abbrev7Bit = Stream.EmitAbbrev(Abbv);
2713
2714 // 6-bit char6 MST_ENTRY strings.
2715 Abbv = new BitCodeAbbrev();
2716 Abbv->Add(BitCodeAbbrevOp(bitc::MST_CODE_ENTRY));
2717 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
2718 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2719 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
2720 unsigned Abbrev6Bit = Stream.EmitAbbrev(Abbv);
2721
2722 SmallVector NameVals;
2723 for (const StringMapEntry &MPSE : I->modPathStringEntries()) {
2724 StringEncoding Bits =
2725 getStringEncoding(MPSE.getKey().data(), MPSE.getKey().size());
2726 unsigned AbbrevToUse = Abbrev8Bit;
2727 if (Bits == SE_Char6)
2728 AbbrevToUse = Abbrev6Bit;
2729 else if (Bits == SE_Fixed7)
2730 AbbrevToUse = Abbrev7Bit;
2731
2732 NameVals.push_back(MPSE.getValue());
2733
2734 for (const auto P : MPSE.getKey()) NameVals.push_back((unsigned char)P);
2735
2736 // Emit the finished record.
2737 Stream.EmitRecord(bitc::MST_CODE_ENTRY, NameVals, AbbrevToUse);
2738 NameVals.clear();
2739 }
2740 Stream.ExitBlock();
2741 }
2742
2743 // Helper to emit a single function summary record.
2744 static void WritePerModuleFunctionSummaryRecord(
2745 SmallVector &NameVals, FunctionSummary *FS, unsigned ValueID,
2746 unsigned FSAbbrev, BitstreamWriter &Stream) {
2747 assert(FS);
2748 NameVals.push_back(ValueID);
2749 NameVals.push_back(FS->isLocalFunction());
2750 NameVals.push_back(FS->instCount());
2751
2752 // Emit the finished record.
2753 Stream.EmitRecord(bitc::FS_CODE_PERMODULE_ENTRY, NameVals, FSAbbrev);
2754 NameVals.clear();
2755 }
2756
2757 /// Emit the per-module function summary section alongside the rest of
2758 /// the module's bitcode.
2759 static void WritePerModuleFunctionSummary(
2760 DenseMap> &FunctionIndex,
2761 const Module *M, const ValueEnumerator &VE, BitstreamWriter &Stream) {
2762 Stream.EnterSubblock(bitc::FUNCTION_SUMMARY_BLOCK_ID, 3);
2763
2764 // Abbrev for FS_CODE_PERMODULE_ENTRY.
2765 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2766 Abbv->Add(BitCodeAbbrevOp(bitc::FS_CODE_PERMODULE_ENTRY));
2767 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
2768 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // islocal
2769 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // instcount
2770 unsigned FSAbbrev = Stream.EmitAbbrev(Abbv);
2771
2772 SmallVector NameVals;
2773 for (auto &I : FunctionIndex) {
2774 // Skip anonymous functions. We will emit a function summary for
2775 // any aliases below.
2776 if (!I.first->hasName()) continue;
2777
2778 WritePerModuleFunctionSummaryRecord(
2779 NameVals, I.second->functionSummary(),
2780 VE.getValueID(M->getValueSymbolTable().lookup(I.first->getName())),
2781 FSAbbrev, Stream);
2782 }
2783
2784 for (const GlobalAlias &A : M->aliases()) {
2785 if (!A.getBaseObject()) continue;
2786 const Function *F = dyn_cast(A.getBaseObject());
2787 if (!F || F->isDeclaration()) continue;
2788
2789 assert(FunctionIndex.count(F) == 1);
2790 WritePerModuleFunctionSummaryRecord(
2791 NameVals, FunctionIndex[F]->functionSummary(),
2792 VE.getValueID(M->getValueSymbolTable().lookup(A.getName())), FSAbbrev,
2793 Stream);
2794 }
2795
2796 Stream.ExitBlock();
2797 }
2798
2799 /// Emit the combined function summary section into the combined index
2800 /// file.
2801 static void WriteCombinedFunctionSummary(const FunctionInfoIndex *I,
2802 BitstreamWriter &Stream) {
2803 Stream.EnterSubblock(bitc::FUNCTION_SUMMARY_BLOCK_ID, 3);
2804
2805 // Abbrev for FS_CODE_COMBINED_ENTRY.
2806 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2807 Abbv->Add(BitCodeAbbrevOp(bitc::FS_CODE_COMBINED_ENTRY));
2808 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // modid
2809 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // instcount
2810 unsigned FSAbbrev = Stream.EmitAbbrev(Abbv);
2811
2812 SmallVector NameVals;
2813 for (const auto &FII : *I) {
2814 for (auto &FI : FII.getValue()) {
2815 FunctionSummary *FS = FI->functionSummary();
2816 assert(FS);
2817
2818 NameVals.push_back(I->getModuleId(FS->modulePath()));
2819 NameVals.push_back(FS->instCount());
2820
2821 // Record the starting offset of this summary entry for use
2822 // in the VST entry. Add the current code size since the
2823 // reader will invoke readRecord after the abbrev id read.
2824 FI->setBitcodeIndex(Stream.GetCurrentBitNo() + Stream.GetAbbrevIDWidth());
2825
2826 // Emit the finished record.
2827 Stream.EmitRecord(bitc::FS_CODE_COMBINED_ENTRY, NameVals, FSAbbrev);
2828 NameVals.clear();
2829 }
2830 }
2831
2832 Stream.ExitBlock();
2833 }
2834
26012835 /// WriteModule - Emit the specified module to the bitstream.
26022836 static void WriteModule(const Module *M, BitstreamWriter &Stream,
26032837 bool ShouldPreserveUseListOrder,
2604 uint64_t BitcodeStartBit) {
2838 uint64_t BitcodeStartBit, bool EmitFunctionSummary) {
26052839 Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3);
26062840
26072841 SmallVector Vals;
26462880 WriteOperandBundleTags(M, Stream);
26472881
26482882 // Emit function bodies.
2649 DenseMapuint64_t> FunctionIndex;
2883 DenseMapstd::unique_ptr> FunctionIndex;
26502884 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F)
26512885 if (!F->isDeclaration())
2652 WriteFunction(*F, VE, Stream, FunctionIndex);
2886 WriteFunction(*F, VE, Stream, FunctionIndex, EmitFunctionSummary);
2887
2888 // Need to write after the above call to WriteFunction which populates
2889 // the summary information in the index.
2890 if (EmitFunctionSummary)
2891 WritePerModuleFunctionSummary(FunctionIndex, M, VE, Stream);
26532892
26542893 WriteValueSymbolTable(M->getValueSymbolTable(), VE, Stream,
26552894 VSTOffsetPlaceholder, BitcodeStartBit, &FunctionIndex);
27272966 Buffer.push_back(0);
27282967 }
27292968
2969 /// Helper to write the header common to all bitcode files.
2970 static void WriteBitcodeHeader(BitstreamWriter &Stream) {
2971 // Emit the file header.
2972 Stream.Emit((unsigned)'B', 8);
2973 Stream.Emit((unsigned)'C', 8);
2974 Stream.Emit(0x0, 4);
2975 Stream.Emit(0xC, 4);
2976 Stream.Emit(0xE, 4);
2977 Stream.Emit(0xD, 4);
2978 }
2979
27302980 /// WriteBitcodeToFile - Write the specified module to the specified output
27312981 /// stream.
27322982 void llvm::WriteBitcodeToFile(const Module *M, raw_ostream &Out,
2733 bool ShouldPreserveUseListOrder) {
2983 bool ShouldPreserveUseListOrder,
2984 bool EmitFunctionSummary) {
27342985 SmallVector Buffer;
27352986 Buffer.reserve(256*1024);
27362987
27503001 uint64_t BitcodeStartBit = Stream.GetCurrentBitNo();
27513002
27523003 // Emit the file header.
2753 Stream.Emit((unsigned)'B', 8);
2754 Stream.Emit((unsigned)'C', 8);
2755 Stream.Emit(0x0, 4);
2756 Stream.Emit(0xC, 4);
2757 Stream.Emit(0xE, 4);
2758 Stream.Emit(0xD, 4);
3004 WriteBitcodeHeader(Stream);
27593005
27603006 // Emit the module.
2761 WriteModule(M, Stream, ShouldPreserveUseListOrder, BitcodeStartBit);
3007 WriteModule(M, Stream, ShouldPreserveUseListOrder, BitcodeStartBit,
3008 EmitFunctionSummary);
27623009 }
27633010
27643011 if (TT.isOSDarwin())
27673014 // Write the generated bitstream to "Out".
27683015 Out.write((char*)&Buffer.front(), Buffer.size());
27693016 }
3017
3018 // Write the specified function summary index to the given raw output stream,
3019 // where it will be written in a new bitcode block. This is used when
3020 // writing the combined index file for ThinLTO.
3021 void llvm::WriteFunctionSummaryToFile(const FunctionInfoIndex *Index,
3022 raw_ostream &Out) {
3023 SmallVector Buffer;
3024 Buffer.reserve(256 * 1024);
3025
3026 BitstreamWriter Stream(Buffer);
3027
3028 // Emit the bitcode header.
3029 WriteBitcodeHeader(Stream);
3030
3031 Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3);
3032
3033 SmallVector Vals;
3034 unsigned CurVersion = 1;
3035 Vals.push_back(CurVersion);
3036 Stream.EmitRecord(bitc::MODULE_CODE_VERSION, Vals);
3037
3038 // Write the module paths in the combined index.
3039 WriteModStrings(Index, Stream);
3040
3041 // Write the function summary combined index records.
3042 WriteCombinedFunctionSummary(Index, Stream);
3043
3044 // Need a special VST writer for the combined index (we don't have a
3045 // real VST and real values when this is invoked).
3046 WriteCombinedValueSymbolTable(Index, Stream);
3047
3048 Stream.ExitBlock();
3049
3050 Out.write((char *)&Buffer.front(), Buffer.size());
3051 }
1818 using namespace llvm;
1919
2020 PreservedAnalyses BitcodeWriterPass::run(Module &M) {
21 WriteBitcodeToFile(&M, OS, ShouldPreserveUseListOrder);
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 EmitFunctionSummary;
2930
3031 public:
3132 static char ID; // Pass identification, replacement for typeid
32 explicit WriteBitcodePass(raw_ostream &o, bool ShouldPreserveUseListOrder)
33 explicit WriteBitcodePass(raw_ostream &o, bool ShouldPreserveUseListOrder,
34 bool EmitFunctionSummary)
3335 : ModulePass(ID), OS(o),
34 ShouldPreserveUseListOrder(ShouldPreserveUseListOrder) {}
36 ShouldPreserveUseListOrder(ShouldPreserveUseListOrder),
37 EmitFunctionSummary(EmitFunctionSummary) {}
3538
3639 const char *getPassName() const override { return "Bitcode Writer"; }
3740
3841 bool runOnModule(Module &M) override {
39 WriteBitcodeToFile(&M, OS, ShouldPreserveUseListOrder);
42 WriteBitcodeToFile(&M, OS, ShouldPreserveUseListOrder,
43 EmitFunctionSummary);
4044 return false;
4145 }
4246 };
4549 char WriteBitcodePass::ID = 0;
4650
4751 ModulePass *llvm::createBitcodeWriterPass(raw_ostream &Str,
48 bool ShouldPreserveUseListOrder) {
49 return new WriteBitcodePass(Str, ShouldPreserveUseListOrder);
52 bool ShouldPreserveUseListOrder,
53 bool EmitFunctionSummary) {
54 return new WriteBitcodePass(Str, ShouldPreserveUseListOrder,
55 EmitFunctionSummary);
5056 }
3838 PassManager.cpp
3939 PassRegistry.cpp
4040 Statepoint.cpp
41 FunctionInfo.cpp
4142 Type.cpp
4243 TypeFinder.cpp
4344 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 function info 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 function 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 &OtherFuncInfoLists : *Other) {
25 StringRef FuncName = OtherFuncInfoLists.getKey();
26 FunctionInfoList &List = OtherFuncInfoLists.second;
27
28 // Assert that the func 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 // Add the module path string ref for this module if we haven't already
34 // saved a reference to it.
35 if (ModPath.empty())
36 ModPath =
37 addModulePath(Info->functionSummary()->modulePath(), NextModuleId);
38 else
39 assert(ModPath == Info->functionSummary()->modulePath() &&
40 "Each module in the combined map should have a unique ID");
41
42 // Note the module path string ref was copied above and is still owned by
43 // the original per-module index. Reset it to the new module path
44 // string reference owned by the combined index.
45 Info->functionSummary()->setModulePath(ModPath);
46
47 // If it is a local function, rename it.
48 if (Info->functionSummary()->isLocalFunction()) {
49 // Any local functions are virtually renamed when being added to the
50 // combined index map, to disambiguate from other functions with
51 // the same name. The symbol table created for the combined index
52 // file should contain the renamed symbols.
53 FuncName =
54 FunctionInfoIndex::getGlobalNameForLocal(FuncName, NextModuleId);
55 }
56
57 // Add new function info to existing list. There may be duplicates when
58 // combining FunctionMap entries, due to COMDAT functions. Any local
59 // functions were virtually renamed above.
60 addFunctionInfo(FuncName, std::move(Info));
61 }
62 }
1515 RecordStreamer.cpp
1616 SymbolicFile.cpp
1717 SymbolSize.cpp
18 FunctionIndexObjectFile.cpp
1819
1920 ADDITIONAL_HEADER_DIRS
2021 ${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 FunctionIndexObjectFile::findBitcodeInObject(
35 const ObjectFile &Obj) {
36 for (const SectionRef &Sec : Obj.sections()) {
37 StringRef SecName;
38 if (std::error_code EC = Sec.getName(SecName)) return EC;
39 if (SecName == ".llvmbc") {
40 StringRef SecContents;
41 if (std::error_code EC = Sec.getContents(SecContents)) return EC;
42 return MemoryBufferRef(SecContents, Obj.getFileName());
43 }
44 }
45
46 return object_error::bitcode_section_not_found;
47 }
48
49 ErrorOr FunctionIndexObjectFile::findBitcodeInMemBuffer(
50 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) return ObjFile.getError();
61 return findBitcodeInObject(*ObjFile->get());
62 }
63 default:
64 return object_error::invalid_file_type;
65 }
66 }
67
68 // Looks for function index in the given memory buffer.
69 // returns true if found, else false.
70 bool FunctionIndexObjectFile::hasFunctionSummaryInMemBuffer(
71 MemoryBufferRef Object, LLVMContext &Context) {
72 ErrorOr BCOrErr = findBitcodeInMemBuffer(Object);
73 if (!BCOrErr) return false;
74
75 return hasFunctionSummary(BCOrErr.get(), Context, nullptr);
76 }
77
78 // Parse function index in the given memory buffer.
79 // Return new FunctionIndexObjectFile instance containing parsed
80 // function summary/index.
81 ErrorOr>
82 FunctionIndexObjectFile::create(MemoryBufferRef Object, LLVMContext &Context,
83 bool IsLazy) {
84 std::unique_ptr Index;
85
86 ErrorOr BCOrErr = findBitcodeInMemBuffer(Object);
87 if (!BCOrErr) return BCOrErr.getError();
88
89 ErrorOr> IOrErr =
90 getFunctionInfoIndex(BCOrErr.get(), Context, nullptr, IsLazy);
91
92 if (std::error_code EC = IOrErr.getError()) return EC;
93
94 Index = std::move(IOrErr.get());
95
96 return llvm::make_unique(Object, std::move(Index));
97 }
98
99 // Parse the function summary information for function with the
100 // given name out of the given buffer. Parsed information is
101 // stored on the index object saved in this object.
102 std::error_code FunctionIndexObjectFile::findFunctionSummaryInMemBuffer(
103 MemoryBufferRef Object, LLVMContext &Context, StringRef FunctionName) {
104 sys::fs::file_magic Type = sys::fs::identify_magic(Object.getBuffer());
105 switch (Type) {
106 case sys::fs::file_magic::bitcode: {
107 return readFunctionSummary(Object, Context, nullptr, FunctionName,
108 std::move(Index));
109 }
110 default:
111 return object_error::invalid_file_type;
112 }
113 }
0 ; RUN: llvm-as -function-summary < %s | llvm-bcanalyzer -dump | FileCheck %s -check-prefix=BC
1 ; Check for function summary block/records.
2
3 ; BC:
4 ; BC-NEXT:
5 ; BC-NEXT:
6 ; BC-NEXT:
7 ; BC-NEXT:
8
9 ; RUN: llvm-as -function-summary < %s | llvm-dis | FileCheck %s
10 ; Check that this round-trips correctly.
11
12 ; ModuleID = ''
13 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
14 target triple = "x86_64-unknown-linux-gnu"
15
16 ; CHECK: define i32 @foo()
17
18 ; Function Attrs: nounwind uwtable
19 define i32 @foo() #0 {
20 entry:
21 ret i32 1
22 }
23
24 ; CHECK: define i32 @bar(i32 %x)
25
26 ; Function Attrs: nounwind uwtable
27 define i32 @bar(i32 %x) #0 {
28 entry:
29 ret i32 %x
30 }
31
32 ; Check an anonymous function as well, since in that case only the alias
33 ; ends up in the value symbol table and having a summary.
34 @f = alias void (), void ()* @0 ; [#uses=0]
35 @h = external global void ()* ; [#uses=0]
36
37 define internal void @0() nounwind {
38 entry:
39 store void()* @0, void()** @h
40 br label %return
41
42 return: ; preds = %entry
43 ret void
44 }
0 define void @g() {
1 entry:
2 ret void
3 }
0 ; RUN: llvm-as -function-summary %s -o %t.o
1 ; RUN: llvm-as -function-summary %p/Inputs/thinlto.ll -o %t2.o
2
3 ; RUN: %gold -plugin %llvmshlibdir/LLVMgold.so \
4 ; RUN: --plugin-opt=thinlto \
5 ; RUN: -shared %t.o %t2.o -o %t3
6 ; RUN: llvm-bcanalyzer -dump %t3.thinlto.bc | FileCheck %s --check-prefix=COMBINED
7 ; RUN: not test -e %t3
8
9 ; COMBINED:
10 ; COMBINED-NEXT:
11 ; COMBINED-NEXT:
12 ; COMBINED-NEXT:
13 ; COMBINED-NEXT:
14 ; COMBINED-NEXT:
15 ; COMBINED-NEXT:
16 ; COMBINED-NEXT:
17
18 define void @f() {
19 entry:
20 ret void
21 }
3131 #include "llvm/Linker/Linker.h"
3232 #include "llvm/MC/SubtargetFeature.h"
3333 #include "llvm/Object/IRObjectFile.h"
34 #include "llvm/Object/FunctionIndexObjectFile.h"
3435 #include "llvm/Support/raw_ostream.h"
3536 #include "llvm/Support/Host.h"
3637 #include "llvm/Support/ManagedStatic.h"
102103 static std::string extra_library_path;
103104 static std::string triple;
104105 static std::string mcpu;
106 // When the thinlto plugin option is specified, only read the function
107 // the information from intermediate files and write a combined
108 // global index for the ThinLTO backends.
109 static bool thinlto = false;
105110 // Additional options to pass into the code generator.
106111 // Note: This array will contain all plugin options which are not claimed
107112 // as plugin exclusive to pass to the code generator.
131136 TheOutputType = OT_SAVE_TEMPS;
132137 } else if (opt == "disable-output") {
133138 TheOutputType = OT_DISABLE;
139 } else if (opt == "thinlto") {
140 thinlto = true;
134141 } else if (opt.size() == 2 && opt[0] == 'O') {
135142 if (opt[1] < '0' || opt[1] > '3')
136143 message(LDPL_FATAL, "Optimization level must be between 0 and 3");
374381 claimed_file &cf = Modules.back();
375382
376383 cf.handle = file->handle;
384
385 // If we are doing ThinLTO compilation, don't need to process the symbols.
386 // Later we simply build a combined index file after all files are claimed.
387 if (options::thinlto) return LDPS_OK;
377388
378389 for (auto &Sym : Obj->symbols()) {
379390 uint32_t Symflags = Sym.getFlags();
588599 free(Sym.comdat_key);
589600 Sym.name = nullptr;
590601 Sym.comdat_key = nullptr;
602 }
603
604 static std::unique_ptr getFunctionIndexForFile(
605 LLVMContext &Context, claimed_file &F, ld_plugin_input_file &Info) {
606
607 if (get_symbols(F.handle, F.syms.size(), &F.syms[0]) != LDPS_OK)
608 message(LDPL_FATAL, "Failed to get symbol information");
609
610 const void *View;
611 if (get_view(F.handle, &View) != LDPS_OK)
612 message(LDPL_FATAL, "Failed to get a view of file");
613
614 MemoryBufferRef BufferRef(StringRef((const char *)View, Info.filesize),
615 Info.name);
616 ErrorOr> ObjOrErr =
617 object::FunctionIndexObjectFile::create(BufferRef, Context);
618
619 if (std::error_code EC = ObjOrErr.getError())
620 message(LDPL_FATAL, "Could not read function index bitcode from file : %s",
621 EC.message().c_str());
622
623 object::FunctionIndexObjectFile &Obj = **ObjOrErr;
624
625 return Obj.takeIndex();
591626 }
592627
593628 static std::unique_ptr
856891 LLVMContext Context;
857892 Context.setDiagnosticHandler(diagnosticHandler, nullptr, true);
858893
894 // If we are doing ThinLTO compilation, simply build the combined
895 // function index/summary and emit it. We don't need to parse the modules
896 // and link them in this case.
897 if (options::thinlto) {
898 std::unique_ptr CombinedIndex(new FunctionInfoIndex());
899 uint64_t NextModuleId = 0;
900 for (claimed_file &F : Modules) {
901 ld_plugin_input_file File;
902 if (get_input_file(F.handle, &File) != LDPS_OK)
903 message(LDPL_FATAL, "Failed to get file information");
904
905 std::unique_ptr Index =
906 getFunctionIndexForFile(Context, F, File);
907 CombinedIndex->mergeFrom(std::move(Index), ++NextModuleId);
908 }
909
910 std::error_code EC;
911 raw_fd_ostream OS(output_name + ".thinlto.bc", EC,
912 sys::fs::OpenFlags::F_None);
913 if (EC)
914 message(LDPL_FATAL, "Unable to open %s.thinlto.bc for writing: %s",
915 output_name.data(), EC.message().c_str());
916 WriteFunctionSummaryToFile(CombinedIndex.get(), OS);
917 OS.close();
918
919 cleanup_hook();
920 exit(0);
921 }
922
859923 std::unique_ptr Combined(new Module("ld-temp.o", Context));
860924 Linker L(Combined.get());
861925
4444 DisableOutput("disable-output", cl::desc("Disable output"), cl::init(false));
4545
4646 static cl::opt
47 EmitFunctionSummary("function-summary", cl::desc("Emit function summary index"),
48 cl::init(false));
49
50 static cl::opt
4751 DumpAsm("d", cl::desc("Print assembly as parsed"), cl::Hidden);
4852
4953 static cl::opt
7680 }
7781
7882 if (Force || !CheckBitcodeOutputToConsole(Out->os(), true))
79 WriteBitcodeToFile(M, Out->os(), PreserveBitcodeUseListOrder);
83 WriteBitcodeToFile(M, Out->os(), PreserveBitcodeUseListOrder,
84 EmitFunctionSummary);
8085
8186 // Declare success.
8287 Out->keep();
113113 case bitc::METADATA_BLOCK_ID: return "METADATA_BLOCK";
114114 case bitc::METADATA_ATTACHMENT_ID: return "METADATA_ATTACHMENT_BLOCK";
115115 case bitc::USELIST_BLOCK_ID: return "USELIST_BLOCK_ID";
116 case bitc::FUNCTION_SUMMARY_BLOCK_ID:
117 return "FUNCTION_SUMMARY_BLOCK";
118 case bitc::MODULE_STRTAB_BLOCK_ID: return "MODULE_STRTAB_BLOCK";
116119 }
117120 }
118121
267270 STRINGIFY_CODE(VST_CODE, ENTRY)
268271 STRINGIFY_CODE(VST_CODE, BBENTRY)
269272 STRINGIFY_CODE(VST_CODE, FNENTRY)
273 STRINGIFY_CODE(VST_CODE, COMBINED_FNENTRY)
274 }
275 case bitc::MODULE_STRTAB_BLOCK_ID:
276 switch (CodeID) {
277 default: return nullptr;
278 STRINGIFY_CODE(MST_CODE, ENTRY)
279 }
280 case bitc::FUNCTION_SUMMARY_BLOCK_ID:
281 switch (CodeID) {
282 default: return nullptr;
283 STRINGIFY_CODE(FS_CODE, PERMODULE_ENTRY)
284 STRINGIFY_CODE(FS_CODE, COMBINED_ENTRY)
270285 }
271286 case bitc::METADATA_ATTACHMENT_ID:
272287 switch(CodeID) {