Index: include/llvm/Bitcode/ReaderWriter.h =================================================================== --- include/llvm/Bitcode/ReaderWriter.h +++ include/llvm/Bitcode/ReaderWriter.h @@ -67,7 +67,7 @@ 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 @@ /// 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 @@ /// 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. /// Index: include/llvm/Object/FunctionIndexObjectFile.h =================================================================== --- include/llvm/Object/FunctionIndexObjectFile.h +++ 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 @@ /// \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); }; } } Index: lib/Bitcode/Reader/BitcodeReader.cpp =================================================================== --- lib/Bitcode/Reader/BitcodeReader.cpp +++ lib/Bitcode/Reader/BitcodeReader.cpp @@ -470,12 +470,11 @@ 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 @@ } 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 @@ // 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 @@ } // 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 @@ // 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. Index: lib/Object/FunctionIndexObjectFile.cpp =================================================================== --- lib/Object/FunctionIndexObjectFile.cpp +++ lib/Object/FunctionIndexObjectFile.cpp @@ -72,19 +72,20 @@ // 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 @@ 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 @@ // 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: Index: tools/llvm-link/llvm-link.cpp =================================================================== --- tools/llvm-link/llvm-link.cpp +++ tools/llvm-link/llvm-link.cpp @@ -148,7 +148,7 @@ 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) Index: tools/llvm-lto/llvm-lto.cpp =================================================================== --- tools/llvm-lto/llvm-lto.cpp +++ 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 @@ 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 @@ /// 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 @@ 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 @@ /// 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";