From 0ab4d52b0ad3b1d16238aed4c3f8d50fa8bf8b40 Mon Sep 17 00:00:00 2001 From: Mehdi Amini Date: Thu, 19 Nov 2015 05:52:29 +0000 Subject: [PATCH] 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 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@253540 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Bitcode/ReaderWriter.h | 18 ++++----- include/llvm/Object/FunctionIndexObjectFile.h | 15 ++++--- lib/Bitcode/Reader/BitcodeReader.cpp | 39 ++++++++----------- lib/Object/FunctionIndexObjectFile.cpp | 14 ++++--- tools/llvm-link/llvm-link.cpp | 2 +- tools/llvm-lto/llvm-lto.cpp | 34 ++++++++++++++-- 6 files changed, 71 insertions(+), 51 deletions(-) diff --git a/include/llvm/Bitcode/ReaderWriter.h b/include/llvm/Bitcode/ReaderWriter.h index dc039557ae3..1c08ded1656 100644 --- a/include/llvm/Bitcode/ReaderWriter.h +++ b/include/llvm/Bitcode/ReaderWriter.h @@ -67,7 +67,7 @@ namespace llvm { DiagnosticHandlerFunction DiagnosticHandler = nullptr); /// Check if the given bitcode buffer contains a function summary block. - bool hasFunctionSummary(MemoryBufferRef Buffer, LLVMContext &Context, + bool hasFunctionSummary(MemoryBufferRef Buffer, DiagnosticHandlerFunction DiagnosticHandler); /// Parse the specified bitcode buffer, returning the function info index. @@ -77,11 +77,9 @@ namespace llvm { /// the index. Otherwise skip the function summary section, and only create /// an index object with a map from function name to function summary offset. /// The index is used to perform lazy function summary reading later. - ErrorOr> - getFunctionInfoIndex(MemoryBufferRef Buffer, LLVMContext &Context, - DiagnosticHandlerFunction DiagnosticHandler, - const Module *ExportingModule = nullptr, - bool IsLazy = false); + ErrorOr> getFunctionInfoIndex( + MemoryBufferRef Buffer, DiagnosticHandlerFunction DiagnosticHandler, + const Module *ExportingModule = nullptr, bool IsLazy = false); /// This method supports lazy reading of function summary data from the /// combined index during function importing. When reading the combined index @@ -89,11 +87,9 @@ namespace llvm { /// Then this method is called for each function considered for importing, /// to parse the summary information for the given function name into /// the index. - std::error_code - readFunctionSummary(MemoryBufferRef Buffer, LLVMContext &Context, - DiagnosticHandlerFunction DiagnosticHandler, - StringRef FunctionName, - std::unique_ptr Index); + std::error_code readFunctionSummary( + MemoryBufferRef Buffer, DiagnosticHandlerFunction DiagnosticHandler, + StringRef FunctionName, std::unique_ptr Index); /// \brief Write the specified module to the specified raw output stream. /// diff --git a/include/llvm/Object/FunctionIndexObjectFile.h b/include/llvm/Object/FunctionIndexObjectFile.h index 12057447f5c..92b670db49a 100644 --- a/include/llvm/Object/FunctionIndexObjectFile.h +++ b/include/llvm/Object/FunctionIndexObjectFile.h @@ -14,6 +14,7 @@ #ifndef LLVM_OBJECT_FUNCTIONINDEXOBJECTFILE_H #define LLVM_OBJECT_FUNCTIONINDEXOBJECTFILE_H +#include "llvm/IR/DiagnosticInfo.h" #include "llvm/Object/SymbolicFile.h" namespace llvm { @@ -78,22 +79,24 @@ public: /// \brief Looks for function summary in the given memory buffer, /// returns true if found, else false. - static bool hasFunctionSummaryInMemBuffer(MemoryBufferRef Object, - LLVMContext &Context); + static bool + hasFunctionSummaryInMemBuffer(MemoryBufferRef Object, + DiagnosticHandlerFunction DiagnosticHandler); /// \brief Parse function index in the given memory buffer. /// Return new FunctionIndexObjectFile instance containing parsed function /// summary/index. static ErrorOr> - create(MemoryBufferRef Object, LLVMContext &Context, + create(MemoryBufferRef Object, DiagnosticHandlerFunction DiagnosticHandler, const Module *ExportingModule = nullptr, bool IsLazy = false); /// \brief Parse the function summary information for function with the /// given name out of the given buffer. Parsed information is /// stored on the index object saved in this object. - std::error_code findFunctionSummaryInMemBuffer(MemoryBufferRef Object, - LLVMContext &Context, - StringRef FunctionName); + std::error_code + findFunctionSummaryInMemBuffer(MemoryBufferRef Object, + DiagnosticHandlerFunction DiagnosticHandler, + StringRef FunctionName); }; } } diff --git a/lib/Bitcode/Reader/BitcodeReader.cpp b/lib/Bitcode/Reader/BitcodeReader.cpp index 9210042e6ed..36bf1e8ef63 100644 --- a/lib/Bitcode/Reader/BitcodeReader.cpp +++ b/lib/Bitcode/Reader/BitcodeReader.cpp @@ -470,12 +470,11 @@ public: std::error_code error(BitcodeError E); std::error_code error(const Twine &Message); - FunctionIndexBitcodeReader(MemoryBuffer *Buffer, LLVMContext &Context, + FunctionIndexBitcodeReader(MemoryBuffer *Buffer, DiagnosticHandlerFunction DiagnosticHandler, bool IsLazy = false, bool CheckFuncSummaryPresenceOnly = false); - FunctionIndexBitcodeReader(LLVMContext &Context, - DiagnosticHandlerFunction DiagnosticHandler, + FunctionIndexBitcodeReader(DiagnosticHandlerFunction DiagnosticHandler, bool IsLazy = false, bool CheckFuncSummaryPresenceOnly = false); ~FunctionIndexBitcodeReader() { freeState(); } @@ -5406,18 +5405,15 @@ std::error_code FunctionIndexBitcodeReader::error(BitcodeError E) { } FunctionIndexBitcodeReader::FunctionIndexBitcodeReader( - MemoryBuffer *Buffer, LLVMContext &Context, - DiagnosticHandlerFunction DiagnosticHandler, bool IsLazy, - bool CheckFuncSummaryPresenceOnly) - : DiagnosticHandler(getDiagHandler(DiagnosticHandler, Context)), - Buffer(Buffer), IsLazy(IsLazy), + MemoryBuffer *Buffer, DiagnosticHandlerFunction DiagnosticHandler, + bool IsLazy, bool CheckFuncSummaryPresenceOnly) + : DiagnosticHandler(DiagnosticHandler), Buffer(Buffer), IsLazy(IsLazy), CheckFuncSummaryPresenceOnly(CheckFuncSummaryPresenceOnly) {} FunctionIndexBitcodeReader::FunctionIndexBitcodeReader( - LLVMContext &Context, DiagnosticHandlerFunction DiagnosticHandler, - bool IsLazy, bool CheckFuncSummaryPresenceOnly) - : DiagnosticHandler(getDiagHandler(DiagnosticHandler, Context)), - Buffer(nullptr), IsLazy(IsLazy), + DiagnosticHandlerFunction DiagnosticHandler, bool IsLazy, + bool CheckFuncSummaryPresenceOnly) + : DiagnosticHandler(DiagnosticHandler), Buffer(nullptr), IsLazy(IsLazy), CheckFuncSummaryPresenceOnly(CheckFuncSummaryPresenceOnly) {} void FunctionIndexBitcodeReader::freeState() { Buffer = nullptr; } @@ -5941,11 +5937,11 @@ llvm::getBitcodeProducerString(MemoryBufferRef Buffer, LLVMContext &Context, // an index object with a map from function name to function summary offset. // The index is used to perform lazy function summary reading later. ErrorOr> -llvm::getFunctionInfoIndex(MemoryBufferRef Buffer, LLVMContext &Context, +llvm::getFunctionInfoIndex(MemoryBufferRef Buffer, DiagnosticHandlerFunction DiagnosticHandler, const Module *ExportingModule, bool IsLazy) { std::unique_ptr Buf = MemoryBuffer::getMemBuffer(Buffer, false); - FunctionIndexBitcodeReader R(Buf.get(), Context, DiagnosticHandler, IsLazy); + FunctionIndexBitcodeReader R(Buf.get(), DiagnosticHandler, IsLazy); std::unique_ptr Index = llvm::make_unique(ExportingModule); @@ -5963,11 +5959,10 @@ llvm::getFunctionInfoIndex(MemoryBufferRef Buffer, LLVMContext &Context, } // Check if the given bitcode buffer contains a function summary block. -bool llvm::hasFunctionSummary(MemoryBufferRef Buffer, LLVMContext &Context, +bool llvm::hasFunctionSummary(MemoryBufferRef Buffer, DiagnosticHandlerFunction DiagnosticHandler) { std::unique_ptr Buf = MemoryBuffer::getMemBuffer(Buffer, false); - FunctionIndexBitcodeReader R(Buf.get(), Context, DiagnosticHandler, false, - true); + FunctionIndexBitcodeReader R(Buf.get(), DiagnosticHandler, false, true); auto cleanupOnError = [&](std::error_code EC) { R.releaseBuffer(); // Never take ownership on error. @@ -5987,13 +5982,11 @@ bool llvm::hasFunctionSummary(MemoryBufferRef Buffer, LLVMContext &Context, // Then this method is called for each function considered for importing, // to parse the summary information for the given function name into // the index. -std::error_code -llvm::readFunctionSummary(MemoryBufferRef Buffer, LLVMContext &Context, - DiagnosticHandlerFunction DiagnosticHandler, - StringRef FunctionName, - std::unique_ptr Index) { +std::error_code llvm::readFunctionSummary( + MemoryBufferRef Buffer, DiagnosticHandlerFunction DiagnosticHandler, + StringRef FunctionName, std::unique_ptr Index) { std::unique_ptr Buf = MemoryBuffer::getMemBuffer(Buffer, false); - FunctionIndexBitcodeReader R(Buf.get(), Context, DiagnosticHandler); + FunctionIndexBitcodeReader R(Buf.get(), DiagnosticHandler); auto cleanupOnError = [&](std::error_code EC) { R.releaseBuffer(); // Never take ownership on error. diff --git a/lib/Object/FunctionIndexObjectFile.cpp b/lib/Object/FunctionIndexObjectFile.cpp index ee65990c528..f4b2b5562a3 100644 --- a/lib/Object/FunctionIndexObjectFile.cpp +++ b/lib/Object/FunctionIndexObjectFile.cpp @@ -72,19 +72,20 @@ FunctionIndexObjectFile::findBitcodeInMemBuffer(MemoryBufferRef Object) { // Looks for function index in the given memory buffer. // returns true if found, else false. bool FunctionIndexObjectFile::hasFunctionSummaryInMemBuffer( - MemoryBufferRef Object, LLVMContext &Context) { + MemoryBufferRef Object, DiagnosticHandlerFunction DiagnosticHandler) { ErrorOr BCOrErr = findBitcodeInMemBuffer(Object); if (!BCOrErr) return false; - return hasFunctionSummary(BCOrErr.get(), Context, nullptr); + return hasFunctionSummary(BCOrErr.get(), DiagnosticHandler); } // Parse function index in the given memory buffer. // Return new FunctionIndexObjectFile instance containing parsed // function summary/index. ErrorOr> -FunctionIndexObjectFile::create(MemoryBufferRef Object, LLVMContext &Context, +FunctionIndexObjectFile::create(MemoryBufferRef Object, + DiagnosticHandlerFunction DiagnosticHandler, const Module *ExportingModule, bool IsLazy) { std::unique_ptr Index; @@ -93,7 +94,7 @@ FunctionIndexObjectFile::create(MemoryBufferRef Object, LLVMContext &Context, return BCOrErr.getError(); ErrorOr> IOrErr = getFunctionInfoIndex( - BCOrErr.get(), Context, nullptr, ExportingModule, IsLazy); + BCOrErr.get(), DiagnosticHandler, ExportingModule, IsLazy); if (std::error_code EC = IOrErr.getError()) return EC; @@ -107,11 +108,12 @@ FunctionIndexObjectFile::create(MemoryBufferRef Object, LLVMContext &Context, // given name out of the given buffer. Parsed information is // stored on the index object saved in this object. std::error_code FunctionIndexObjectFile::findFunctionSummaryInMemBuffer( - MemoryBufferRef Object, LLVMContext &Context, StringRef FunctionName) { + MemoryBufferRef Object, DiagnosticHandlerFunction DiagnosticHandler, + StringRef FunctionName) { sys::fs::file_magic Type = sys::fs::identify_magic(Object.getBuffer()); switch (Type) { case sys::fs::file_magic::bitcode: { - return readFunctionSummary(Object, Context, nullptr, FunctionName, + return readFunctionSummary(Object, DiagnosticHandler, FunctionName, std::move(Index)); } default: diff --git a/tools/llvm-link/llvm-link.cpp b/tools/llvm-link/llvm-link.cpp index 8d0ce0a040f..c539f75702b 100644 --- a/tools/llvm-link/llvm-link.cpp +++ b/tools/llvm-link/llvm-link.cpp @@ -148,7 +148,7 @@ loadIndex(LLVMContext &Context, const Module *ExportingModule = nullptr) { return EC; MemoryBufferRef BufferRef = (FileOrErr.get())->getMemBufferRef(); ErrorOr> ObjOrErr = - object::FunctionIndexObjectFile::create(BufferRef, Context, + object::FunctionIndexObjectFile::create(BufferRef, diagnosticHandler, ExportingModule); EC = ObjOrErr.getError(); if (EC) diff --git a/tools/llvm-lto/llvm-lto.cpp b/tools/llvm-lto/llvm-lto.cpp index c74353282b6..cdf91f9e377 100644 --- a/tools/llvm-lto/llvm-lto.cpp +++ b/tools/llvm-lto/llvm-lto.cpp @@ -15,6 +15,7 @@ #include "llvm/ADT/StringSet.h" #include "llvm/Bitcode/ReaderWriter.h" #include "llvm/CodeGen/CommandFlags.h" +#include "llvm/IR/DiagnosticPrinter.h" #include "llvm/IR/LLVMContext.h" #include "llvm/LTO/LTOCodeGenerator.h" #include "llvm/LTO/LTOModule.h" @@ -119,6 +120,32 @@ static void handleDiagnostics(lto_codegen_diagnostic_severity_t Severity, errs() << Msg << "\n"; } +static void diagnosticHandler(const DiagnosticInfo &DI) { + raw_ostream &OS = errs(); + OS << "llvm-lto: "; + switch (DI.getSeverity()) { + case DS_Error: + OS << "error: "; + break; + case DS_Warning: + OS << "warning: "; + break; + case DS_Remark: + OS << "remark: "; + break; + case DS_Note: + OS << "note: "; + break; + } + + DiagnosticPrinterRawOStream DP(OS); + DI.print(DP); + OS << '\n'; + + if (DI.getSeverity() == DS_Error) + exit(1); +} + static std::unique_ptr getLocalLTOModule(StringRef Path, std::unique_ptr &Buffer, const TargetOptions &Options, std::string &Error) { @@ -163,7 +190,7 @@ static int listSymbols(StringRef Command, const TargetOptions &Options) { /// index object if found, or nullptr if not. static std::unique_ptr getFunctionIndexForFile(StringRef Path, std::string &Error, - LLVMContext &Context) { + DiagnosticHandlerFunction DiagnosticHandler) { std::unique_ptr Buffer; ErrorOr> BufferOrErr = MemoryBuffer::getFile(Path); @@ -174,7 +201,7 @@ getFunctionIndexForFile(StringRef Path, std::string &Error, Buffer = std::move(BufferOrErr.get()); ErrorOr> ObjOrErr = object::FunctionIndexObjectFile::create(Buffer->getMemBufferRef(), - Context); + DiagnosticHandler); if (std::error_code EC = ObjOrErr.getError()) { Error = EC.message(); return nullptr; @@ -187,13 +214,12 @@ getFunctionIndexForFile(StringRef Path, std::string &Error, /// This is meant to enable testing of ThinLTO combined index generation, /// currently available via the gold plugin via -thinlto. static int createCombinedFunctionIndex(StringRef Command) { - LLVMContext Context; FunctionInfoIndex CombinedIndex; uint64_t NextModuleId = 0; for (auto &Filename : InputFilenames) { std::string Error; std::unique_ptr Index = - getFunctionIndexForFile(Filename, Error, Context); + getFunctionIndexForFile(Filename, Error, diagnosticHandler); if (!Index) { errs() << Command << ": error loading file '" << Filename << "': " << Error << "\n"; -- 2.34.1