llvm.org GIT mirror llvm / 0ab4d52
Do not require a Context to extract the FunctionIndex from Bitcode (NFC) The LLVMContext was only used for Diagnostic. Pass a DiagnosticHandler instead. Differential Revision: http://reviews.llvm.org/D14794 From: Mehdi Amini <mehdi.amini@apple.com> git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@253540 91177308-0d34-0410-b5e6-96231b3b80d8 Mehdi Amini 4 years ago
6 changed file(s) with 72 addition(s) and 52 deletion(s). Raw diff Collapse all Expand all
6666 DiagnosticHandlerFunction DiagnosticHandler = nullptr);
6767
6868 /// Check if the given bitcode buffer contains a function summary block.
69 bool hasFunctionSummary(MemoryBufferRef Buffer, LLVMContext &Context,
69 bool hasFunctionSummary(MemoryBufferRef Buffer,
7070 DiagnosticHandlerFunction DiagnosticHandler);
7171
7272 /// Parse the specified bitcode buffer, returning the function info index.
7676 /// the index. Otherwise skip the function summary section, and only create
7777 /// an index object with a map from function name to function summary offset.
7878 /// The index is used to perform lazy function summary reading later.
79 ErrorOr>
80 getFunctionInfoIndex(MemoryBufferRef Buffer, LLVMContext &Context,
81 DiagnosticHandlerFunction DiagnosticHandler,
82 const Module *ExportingModule = nullptr,
83 bool IsLazy = false);
79 ErrorOr> getFunctionInfoIndex(
80 MemoryBufferRef Buffer, DiagnosticHandlerFunction DiagnosticHandler,
81 const Module *ExportingModule = nullptr, bool IsLazy = false);
8482
8583 /// This method supports lazy reading of function summary data from the
8684 /// combined index during function importing. When reading the combined index
8886 /// Then this method is called for each function considered for importing,
8987 /// to parse the summary information for the given function name into
9088 /// the index.
91 std::error_code
92 readFunctionSummary(MemoryBufferRef Buffer, LLVMContext &Context,
93 DiagnosticHandlerFunction DiagnosticHandler,
94 StringRef FunctionName,
95 std::unique_ptr Index);
89 std::error_code readFunctionSummary(
90 MemoryBufferRef Buffer, DiagnosticHandlerFunction DiagnosticHandler,
91 StringRef FunctionName, std::unique_ptr Index);
9692
9793 /// \brief Write the specified module to the specified raw output stream.
9894 ///
1313 #ifndef LLVM_OBJECT_FUNCTIONINDEXOBJECTFILE_H
1414 #define LLVM_OBJECT_FUNCTIONINDEXOBJECTFILE_H
1515
16 #include "llvm/IR/DiagnosticInfo.h"
1617 #include "llvm/Object/SymbolicFile.h"
1718
1819 namespace llvm {
7778
7879 /// \brief Looks for function summary in the given memory buffer,
7980 /// returns true if found, else false.
80 static bool hasFunctionSummaryInMemBuffer(MemoryBufferRef Object,
81 LLVMContext &Context);
81 static bool
82 hasFunctionSummaryInMemBuffer(MemoryBufferRef Object,
83 DiagnosticHandlerFunction DiagnosticHandler);
8284
8385 /// \brief Parse function index in the given memory buffer.
8486 /// Return new FunctionIndexObjectFile instance containing parsed function
8587 /// summary/index.
8688 static ErrorOr>
87 create(MemoryBufferRef Object, LLVMContext &Context,
89 create(MemoryBufferRef Object, DiagnosticHandlerFunction DiagnosticHandler,
8890 const Module *ExportingModule = nullptr, bool IsLazy = false);
8991
9092 /// \brief Parse the function summary information for function with the
9193 /// given name out of the given buffer. Parsed information is
9294 /// stored on the index object saved in this object.
93 std::error_code findFunctionSummaryInMemBuffer(MemoryBufferRef Object,
94 LLVMContext &Context,
95 StringRef FunctionName);
95 std::error_code
96 findFunctionSummaryInMemBuffer(MemoryBufferRef Object,
97 DiagnosticHandlerFunction DiagnosticHandler,
98 StringRef FunctionName);
9699 };
97100 }
98101 }
469469 std::error_code error(BitcodeError E);
470470 std::error_code error(const Twine &Message);
471471
472 FunctionIndexBitcodeReader(MemoryBuffer *Buffer, LLVMContext &Context,
472 FunctionIndexBitcodeReader(MemoryBuffer *Buffer,
473473 DiagnosticHandlerFunction DiagnosticHandler,
474474 bool IsLazy = false,
475475 bool CheckFuncSummaryPresenceOnly = false);
476 FunctionIndexBitcodeReader(LLVMContext &Context,
477 DiagnosticHandlerFunction DiagnosticHandler,
476 FunctionIndexBitcodeReader(DiagnosticHandlerFunction DiagnosticHandler,
478477 bool IsLazy = false,
479478 bool CheckFuncSummaryPresenceOnly = false);
480479 ~FunctionIndexBitcodeReader() { freeState(); }
54055404 }
54065405
54075406 FunctionIndexBitcodeReader::FunctionIndexBitcodeReader(
5408 MemoryBuffer *Buffer, LLVMContext &Context,
5407 MemoryBuffer *Buffer, DiagnosticHandlerFunction DiagnosticHandler,
5408 bool IsLazy, bool CheckFuncSummaryPresenceOnly)
5409 : DiagnosticHandler(DiagnosticHandler), Buffer(Buffer), IsLazy(IsLazy),
5410 CheckFuncSummaryPresenceOnly(CheckFuncSummaryPresenceOnly) {}
5411
5412 FunctionIndexBitcodeReader::FunctionIndexBitcodeReader(
54095413 DiagnosticHandlerFunction DiagnosticHandler, bool IsLazy,
54105414 bool CheckFuncSummaryPresenceOnly)
5411 : DiagnosticHandler(getDiagHandler(DiagnosticHandler, Context)),
5412 Buffer(Buffer), IsLazy(IsLazy),
5413 CheckFuncSummaryPresenceOnly(CheckFuncSummaryPresenceOnly) {}
5414
5415 FunctionIndexBitcodeReader::FunctionIndexBitcodeReader(
5416 LLVMContext &Context, DiagnosticHandlerFunction DiagnosticHandler,
5417 bool IsLazy, bool CheckFuncSummaryPresenceOnly)
5418 : DiagnosticHandler(getDiagHandler(DiagnosticHandler, Context)),
5419 Buffer(nullptr), IsLazy(IsLazy),
5415 : DiagnosticHandler(DiagnosticHandler), Buffer(nullptr), IsLazy(IsLazy),
54205416 CheckFuncSummaryPresenceOnly(CheckFuncSummaryPresenceOnly) {}
54215417
54225418 void FunctionIndexBitcodeReader::freeState() { Buffer = nullptr; }
59405936 // an index object with a map from function name to function summary offset.
59415937 // The index is used to perform lazy function summary reading later.
59425938 ErrorOr>
5943 llvm::getFunctionInfoIndex(MemoryBufferRef Buffer, LLVMContext &Context,
5939 llvm::getFunctionInfoIndex(MemoryBufferRef Buffer,
59445940 DiagnosticHandlerFunction DiagnosticHandler,
59455941 const Module *ExportingModule, bool IsLazy) {
59465942 std::unique_ptr Buf = MemoryBuffer::getMemBuffer(Buffer, false);
5947 FunctionIndexBitcodeReader R(Buf.get(), Context, DiagnosticHandler, IsLazy);
5943 FunctionIndexBitcodeReader R(Buf.get(), DiagnosticHandler, IsLazy);
59485944
59495945 std::unique_ptr Index =
59505946 llvm::make_unique(ExportingModule);
59625958 }
59635959
59645960 // Check if the given bitcode buffer contains a function summary block.
5965 bool llvm::hasFunctionSummary(MemoryBufferRef Buffer, LLVMContext &Context,
5961 bool llvm::hasFunctionSummary(MemoryBufferRef Buffer,
59665962 DiagnosticHandlerFunction DiagnosticHandler) {
59675963 std::unique_ptr Buf = MemoryBuffer::getMemBuffer(Buffer, false);
5968 FunctionIndexBitcodeReader R(Buf.get(), Context, DiagnosticHandler, false,
5969 true);
5964 FunctionIndexBitcodeReader R(Buf.get(), DiagnosticHandler, false, true);
59705965
59715966 auto cleanupOnError = [&](std::error_code EC) {
59725967 R.releaseBuffer(); // Never take ownership on error.
59865981 // Then this method is called for each function considered for importing,
59875982 // to parse the summary information for the given function name into
59885983 // the index.
5989 std::error_code
5990 llvm::readFunctionSummary(MemoryBufferRef Buffer, LLVMContext &Context,
5991 DiagnosticHandlerFunction DiagnosticHandler,
5992 StringRef FunctionName,
5993 std::unique_ptr Index) {
5984 std::error_code llvm::readFunctionSummary(
5985 MemoryBufferRef Buffer, DiagnosticHandlerFunction DiagnosticHandler,
5986 StringRef FunctionName, std::unique_ptr Index) {
59945987 std::unique_ptr Buf = MemoryBuffer::getMemBuffer(Buffer, false);
5995 FunctionIndexBitcodeReader R(Buf.get(), Context, DiagnosticHandler);
5988 FunctionIndexBitcodeReader R(Buf.get(), DiagnosticHandler);
59965989
59975990 auto cleanupOnError = [&](std::error_code EC) {
59985991 R.releaseBuffer(); // Never take ownership on error.
7171 // Looks for function index in the given memory buffer.
7272 // returns true if found, else false.
7373 bool FunctionIndexObjectFile::hasFunctionSummaryInMemBuffer(
74 MemoryBufferRef Object, LLVMContext &Context) {
74 MemoryBufferRef Object, DiagnosticHandlerFunction DiagnosticHandler) {
7575 ErrorOr BCOrErr = findBitcodeInMemBuffer(Object);
7676 if (!BCOrErr)
7777 return false;
7878
79 return hasFunctionSummary(BCOrErr.get(), Context, nullptr);
79 return hasFunctionSummary(BCOrErr.get(), DiagnosticHandler);
8080 }
8181
8282 // Parse function index in the given memory buffer.
8383 // Return new FunctionIndexObjectFile instance containing parsed
8484 // function summary/index.
8585 ErrorOr>
86 FunctionIndexObjectFile::create(MemoryBufferRef Object, LLVMContext &Context,
86 FunctionIndexObjectFile::create(MemoryBufferRef Object,
87 DiagnosticHandlerFunction DiagnosticHandler,
8788 const Module *ExportingModule, bool IsLazy) {
8889 std::unique_ptr Index;
8990
9293 return BCOrErr.getError();
9394
9495 ErrorOr> IOrErr = getFunctionInfoIndex(
95 BCOrErr.get(), Context, nullptr, ExportingModule, IsLazy);
96 BCOrErr.get(), DiagnosticHandler, ExportingModule, IsLazy);
9697
9798 if (std::error_code EC = IOrErr.getError())
9899 return EC;
106107 // given name out of the given buffer. Parsed information is
107108 // stored on the index object saved in this object.
108109 std::error_code FunctionIndexObjectFile::findFunctionSummaryInMemBuffer(
109 MemoryBufferRef Object, LLVMContext &Context, StringRef FunctionName) {
110 MemoryBufferRef Object, DiagnosticHandlerFunction DiagnosticHandler,
111 StringRef FunctionName) {
110112 sys::fs::file_magic Type = sys::fs::identify_magic(Object.getBuffer());
111113 switch (Type) {
112114 case sys::fs::file_magic::bitcode: {
113 return readFunctionSummary(Object, Context, nullptr, FunctionName,
115 return readFunctionSummary(Object, DiagnosticHandler, FunctionName,
114116 std::move(Index));
115117 }
116118 default:
147147 return EC;
148148 MemoryBufferRef BufferRef = (FileOrErr.get())->getMemBufferRef();
149149 ErrorOr> ObjOrErr =
150 object::FunctionIndexObjectFile::create(BufferRef, Context,
150 object::FunctionIndexObjectFile::create(BufferRef, diagnosticHandler,
151151 ExportingModule);
152152 EC = ObjOrErr.getError();
153153 if (EC)
1414 #include "llvm/ADT/StringSet.h"
1515 #include "llvm/Bitcode/ReaderWriter.h"
1616 #include "llvm/CodeGen/CommandFlags.h"
17 #include "llvm/IR/DiagnosticPrinter.h"
1718 #include "llvm/IR/LLVMContext.h"
1819 #include "llvm/LTO/LTOCodeGenerator.h"
1920 #include "llvm/LTO/LTOModule.h"
116117 break;
117118 }
118119 errs() << Msg << "\n";
120 }
121
122 static void diagnosticHandler(const DiagnosticInfo &DI) {
123 raw_ostream &OS = errs();
124 OS << "llvm-lto: ";
125 switch (DI.getSeverity()) {
126 case DS_Error:
127 OS << "error: ";
128 break;
129 case DS_Warning:
130 OS << "warning: ";
131 break;
132 case DS_Remark:
133 OS << "remark: ";
134 break;
135 case DS_Note:
136 OS << "note: ";
137 break;
138 }
139
140 DiagnosticPrinterRawOStream DP(OS);
141 DI.print(DP);
142 OS << '\n';
143
144 if (DI.getSeverity() == DS_Error)
145 exit(1);
119146 }
120147
121148 static std::unique_ptr
162189 /// index object if found, or nullptr if not.
163190 static std::unique_ptr
164191 getFunctionIndexForFile(StringRef Path, std::string &Error,
165 LLVMContext &Context) {
192 DiagnosticHandlerFunction DiagnosticHandler) {
166193 std::unique_ptr Buffer;
167194 ErrorOr> BufferOrErr =
168195 MemoryBuffer::getFile(Path);
173200 Buffer = std::move(BufferOrErr.get());
174201 ErrorOr> ObjOrErr =
175202 object::FunctionIndexObjectFile::create(Buffer->getMemBufferRef(),
176 Context);
203 DiagnosticHandler);
177204 if (std::error_code EC = ObjOrErr.getError()) {
178205 Error = EC.message();
179206 return nullptr;
186213 /// This is meant to enable testing of ThinLTO combined index generation,
187214 /// currently available via the gold plugin via -thinlto.
188215 static int createCombinedFunctionIndex(StringRef Command) {
189 LLVMContext Context;
190216 FunctionInfoIndex CombinedIndex;
191217 uint64_t NextModuleId = 0;
192218 for (auto &Filename : InputFilenames) {
193219 std::string Error;
194220 std::unique_ptr Index =
195 getFunctionIndexForFile(Filename, Error, Context);
221 getFunctionIndexForFile(Filename, Error, diagnosticHandler);
196222 if (!Index) {
197223 errs() << Command << ": error loading file '" << Filename
198224 << "': " << Error << "\n";