[PATCH] D14794: Do not require a Context to extract the FunctionIndex from Bitcode
Justin Bogner via llvm-commits
llvm-commits at lists.llvm.org
Wed Nov 18 16:50:20 PST 2015
Mehdi AMINI via llvm-commits <llvm-commits at lists.llvm.org> writes:
> joker.eph created this revision.
> joker.eph added a reviewer: tejohnson.
> joker.eph added subscribers: dexonsmith, llvm-commits.
>
> The LLVMContext was only used for Diagnostic. Pass a DiagnosticHandler
> instead.
Seems obvious. LGTM.
Mehdi AMINI via llvm-commits <llvm-commits at lists.llvm.org> writes:
> joker.eph updated this revision to Diff 40578.
> joker.eph added a comment.
>
> Remove no longer used LLVMContext in createCombinedFunctionIndex
>
>
> http://reviews.llvm.org/D14794
>
> Files:
> include/llvm/Bitcode/ReaderWriter.h
> include/llvm/Object/FunctionIndexObjectFile.h
> lib/Bitcode/Reader/BitcodeReader.cpp
> lib/Object/FunctionIndexObjectFile.cpp
> tools/llvm-link/llvm-link.cpp
> tools/llvm-lto/llvm-lto.cpp
>
> 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<LTOModule>
> getLocalLTOModule(StringRef Path, std::unique_ptr<MemoryBuffer> &Buffer,
> const TargetOptions &Options, std::string &Error) {
> @@ -163,7 +190,7 @@
> /// index object if found, or nullptr if not.
> static std::unique_ptr<FunctionInfoIndex>
> getFunctionIndexForFile(StringRef Path, std::string &Error,
> - LLVMContext &Context) {
> + DiagnosticHandlerFunction DiagnosticHandler) {
> std::unique_ptr<MemoryBuffer> Buffer;
> ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
> MemoryBuffer::getFile(Path);
> @@ -174,7 +201,7 @@
> Buffer = std::move(BufferOrErr.get());
> ErrorOr<std::unique_ptr<object::FunctionIndexObjectFile>> 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<FunctionInfoIndex> Index =
> - getFunctionIndexForFile(Filename, Error, Context);
> + getFunctionIndexForFile(Filename, Error, diagnosticHandler);
> if (!Index) {
> errs() << Command << ": error loading file '" << Filename
> << "': " << Error << "\n";
> 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<std::unique_ptr<object::FunctionIndexObjectFile>> ObjOrErr =
> - object::FunctionIndexObjectFile::create(BufferRef, Context,
> + object::FunctionIndexObjectFile::create(BufferRef, diagnosticHandler,
> ExportingModule);
> EC = ObjOrErr.getError();
> if (EC)
> Index: lib/Object/FunctionIndexObjectFile.cpp
> ===================================================================
> --- lib/Object/FunctionIndexObjectFile.cpp
> +++ lib/Object/FunctionIndexObjectFile.cpp
> @@ -72,28 +72,29 @@
> // 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<MemoryBufferRef> 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<std::unique_ptr<FunctionIndexObjectFile>>
> -FunctionIndexObjectFile::create(MemoryBufferRef Object, LLVMContext &Context,
> +FunctionIndexObjectFile::create(MemoryBufferRef Object,
> + DiagnosticHandlerFunction DiagnosticHandler,
> const Module *ExportingModule, bool IsLazy) {
> std::unique_ptr<FunctionInfoIndex> Index;
>
> ErrorOr<MemoryBufferRef> BCOrErr = findBitcodeInMemBuffer(Object);
> if (!BCOrErr)
> return BCOrErr.getError();
>
> ErrorOr<std::unique_ptr<FunctionInfoIndex>> 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: 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<std::unique_ptr<FunctionInfoIndex>>
> -llvm::getFunctionInfoIndex(MemoryBufferRef Buffer, LLVMContext &Context,
> +llvm::getFunctionInfoIndex(MemoryBufferRef Buffer,
> DiagnosticHandlerFunction DiagnosticHandler,
> const Module *ExportingModule, bool IsLazy) {
> std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Buffer, false);
> - FunctionIndexBitcodeReader R(Buf.get(), Context, DiagnosticHandler, IsLazy);
> + FunctionIndexBitcodeReader R(Buf.get(), DiagnosticHandler, IsLazy);
>
> std::unique_ptr<FunctionInfoIndex> Index =
> llvm::make_unique<FunctionInfoIndex>(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<MemoryBuffer> 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<FunctionInfoIndex> Index) {
> +std::error_code llvm::readFunctionSummary(
> + MemoryBufferRef Buffer, DiagnosticHandlerFunction DiagnosticHandler,
> + StringRef FunctionName, std::unique_ptr<FunctionInfoIndex> Index) {
> std::unique_ptr<MemoryBuffer> 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: 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<std::unique_ptr<FunctionIndexObjectFile>>
> - 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: 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,23 +77,19 @@
> /// 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<std::unique_ptr<FunctionInfoIndex>>
> - getFunctionInfoIndex(MemoryBufferRef Buffer, LLVMContext &Context,
> - DiagnosticHandlerFunction DiagnosticHandler,
> - const Module *ExportingModule = nullptr,
> - bool IsLazy = false);
> + ErrorOr<std::unique_ptr<FunctionInfoIndex>> 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
> /// file, getFunctionInfoIndex is first invoked with IsLazy=true.
> /// 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<FunctionInfoIndex> Index);
> + std::error_code readFunctionSummary(
> + MemoryBufferRef Buffer, DiagnosticHandlerFunction DiagnosticHandler,
> + StringRef FunctionName, std::unique_ptr<FunctionInfoIndex> Index);
>
> /// \brief Write the specified module to the specified raw output stream.
> ///
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits
More information about the llvm-commits
mailing list