[llvm] r253540 - Do not require a Context to extract the FunctionIndex from Bitcode (NFC)

Mehdi Amini via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 18 21:52:29 PST 2015


Author: mehdi_amini
Date: Wed Nov 18 23:52:29 2015
New Revision: 253540

URL: http://llvm.org/viewvc/llvm-project?rev=253540&view=rev
Log:
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 at apple.com>

Modified:
    llvm/trunk/include/llvm/Bitcode/ReaderWriter.h
    llvm/trunk/include/llvm/Object/FunctionIndexObjectFile.h
    llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp
    llvm/trunk/lib/Object/FunctionIndexObjectFile.cpp
    llvm/trunk/tools/llvm-link/llvm-link.cpp
    llvm/trunk/tools/llvm-lto/llvm-lto.cpp

Modified: llvm/trunk/include/llvm/Bitcode/ReaderWriter.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Bitcode/ReaderWriter.h?rev=253540&r1=253539&r2=253540&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Bitcode/ReaderWriter.h (original)
+++ llvm/trunk/include/llvm/Bitcode/ReaderWriter.h Wed Nov 18 23:52:29 2015
@@ -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<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
@@ -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<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.
   ///

Modified: llvm/trunk/include/llvm/Object/FunctionIndexObjectFile.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Object/FunctionIndexObjectFile.h?rev=253540&r1=253539&r2=253540&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Object/FunctionIndexObjectFile.h (original)
+++ llvm/trunk/include/llvm/Object/FunctionIndexObjectFile.h Wed Nov 18 23:52:29 2015
@@ -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<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);
 };
 }
 }

Modified: llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp?rev=253540&r1=253539&r2=253540&view=diff
==============================================================================
--- llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp (original)
+++ llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp Wed Nov 18 23:52:29 2015
@@ -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 FunctionIndexBitcodeRead
 }
 
 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(MemoryBuf
 // 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 @@ llvm::getFunctionInfoIndex(MemoryBufferR
 }
 
 // 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 @@ bool llvm::hasFunctionSummary(MemoryBuff
 // 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.

Modified: llvm/trunk/lib/Object/FunctionIndexObjectFile.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Object/FunctionIndexObjectFile.cpp?rev=253540&r1=253539&r2=253540&view=diff
==============================================================================
--- llvm/trunk/lib/Object/FunctionIndexObjectFile.cpp (original)
+++ llvm/trunk/lib/Object/FunctionIndexObjectFile.cpp Wed Nov 18 23:52:29 2015
@@ -72,19 +72,20 @@ FunctionIndexObjectFile::findBitcodeInMe
 // 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;
 
@@ -93,7 +94,7 @@ FunctionIndexObjectFile::create(MemoryBu
     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 @@ FunctionIndexObjectFile::create(MemoryBu
 // 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:

Modified: llvm/trunk/tools/llvm-link/llvm-link.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-link/llvm-link.cpp?rev=253540&r1=253539&r2=253540&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-link/llvm-link.cpp (original)
+++ llvm/trunk/tools/llvm-link/llvm-link.cpp Wed Nov 18 23:52:29 2015
@@ -148,7 +148,7 @@ loadIndex(LLVMContext &Context, const Mo
     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)

Modified: llvm/trunk/tools/llvm-lto/llvm-lto.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-lto/llvm-lto.cpp?rev=253540&r1=253539&r2=253540&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-lto/llvm-lto.cpp (original)
+++ llvm/trunk/tools/llvm-lto/llvm-lto.cpp Wed Nov 18 23:52:29 2015
@@ -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_codege
   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 @@ static int listSymbols(StringRef Command
 /// 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 @@ getFunctionIndexForFile(StringRef Path,
   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 @@ getFunctionIndexForFile(StringRef Path,
 /// 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";




More information about the llvm-commits mailing list