[llvm] r221120 - Use ErrorOr for the ::create factory on instrumented and sample profilers.

Diego Novillo dnovillo at google.com
Sun Nov 2 16:51:45 PST 2014


Author: dnovillo
Date: Sun Nov  2 18:51:45 2014
New Revision: 221120

URL: http://llvm.org/viewvc/llvm-project?rev=221120&view=rev
Log:
Use ErrorOr for the ::create factory on instrumented and sample profilers.

Summary:
As discussed in
http://lists.cs.uiuc.edu/pipermail/llvm-commits/Week-of-Mon-20141027/242445.html,
the creation of reader and writer instances is better done using
ErrorOr. There are no functional changes, but several callers needed to
be adjusted.

Reviewers: bogner

Subscribers: llvm-commits

Differential Revision: http://reviews.llvm.org/D6076

Modified:
    llvm/trunk/include/llvm/ProfileData/InstrProfReader.h
    llvm/trunk/include/llvm/ProfileData/SampleProfReader.h
    llvm/trunk/include/llvm/ProfileData/SampleProfWriter.h
    llvm/trunk/lib/ProfileData/InstrProfReader.cpp
    llvm/trunk/lib/ProfileData/SampleProfReader.cpp
    llvm/trunk/lib/ProfileData/SampleProfWriter.cpp
    llvm/trunk/lib/Transforms/Scalar/SampleProfile.cpp
    llvm/trunk/tools/llvm-profdata/llvm-profdata.cpp

Modified: llvm/trunk/include/llvm/ProfileData/InstrProfReader.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ProfileData/InstrProfReader.h?rev=221120&r1=221119&r2=221120&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ProfileData/InstrProfReader.h (original)
+++ llvm/trunk/include/llvm/ProfileData/InstrProfReader.h Sun Nov  2 18:51:45 2014
@@ -18,6 +18,7 @@
 #include "llvm/ADT/ArrayRef.h"
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/ProfileData/InstrProf.h"
+#include "llvm/Support/ErrorOr.h"
 #include "llvm/Support/LineIterator.h"
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/EndianStream.h"
@@ -94,8 +95,7 @@ public:
 
   /// Factory method to create an appropriately typed reader for the given
   /// instrprof file.
-  static std::error_code create(std::string Path,
-                                std::unique_ptr<InstrProfReader> &Result);
+  static ErrorOr<std::unique_ptr<InstrProfReader>> create(std::string Path);
 };
 
 /// Reader for the simple text based instrprof format.

Modified: llvm/trunk/include/llvm/ProfileData/SampleProfReader.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ProfileData/SampleProfReader.h?rev=221120&r1=221119&r2=221120&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ProfileData/SampleProfReader.h (original)
+++ llvm/trunk/include/llvm/ProfileData/SampleProfReader.h Sun Nov  2 18:51:45 2014
@@ -92,9 +92,8 @@ public:
   }
 
   /// \brief Create a sample profile reader appropriate to the file format.
-  static std::error_code create(StringRef Filename,
-                                std::unique_ptr<SampleProfileReader> &Reader,
-                                LLVMContext &C);
+  static ErrorOr<std::unique_ptr<SampleProfileReader>>
+  create(StringRef Filename, LLVMContext &C);
 
 protected:
   /// \brief Map every function to its associated profile.

Modified: llvm/trunk/include/llvm/ProfileData/SampleProfWriter.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ProfileData/SampleProfWriter.h?rev=221120&r1=221119&r2=221120&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ProfileData/SampleProfWriter.h (original)
+++ llvm/trunk/include/llvm/ProfileData/SampleProfWriter.h Sun Nov  2 18:51:45 2014
@@ -17,6 +17,7 @@
 #include "llvm/IR/Function.h"
 #include "llvm/IR/Module.h"
 #include "llvm/ProfileData/SampleProf.h"
+#include "llvm/Support/ErrorOr.h"
 #include "llvm/Support/FileSystem.h"
 #include "llvm/Support/raw_ostream.h"
 
@@ -71,9 +72,8 @@ public:
 
   /// \brief Profile writer factory. Create a new writer based on the value of
   /// \p Format.
-  static std::error_code create(StringRef Filename,
-                                std::unique_ptr<SampleProfileWriter> &Result,
-                                SampleProfileFormat Format);
+  static ErrorOr<std::unique_ptr<SampleProfileWriter>>
+  create(StringRef Filename, SampleProfileFormat Format);
 
 protected:
   /// \brief Output stream where to emit the profile to.

Modified: llvm/trunk/lib/ProfileData/InstrProfReader.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/ProfileData/InstrProfReader.cpp?rev=221120&r1=221119&r2=221120&view=diff
==============================================================================
--- llvm/trunk/lib/ProfileData/InstrProfReader.cpp (original)
+++ llvm/trunk/lib/ProfileData/InstrProfReader.cpp Sun Nov  2 18:51:45 2014
@@ -21,32 +21,34 @@
 
 using namespace llvm;
 
-static std::error_code
-setupMemoryBuffer(std::string Path, std::unique_ptr<MemoryBuffer> &Buffer) {
+static ErrorOr<std::unique_ptr<MemoryBuffer>>
+setupMemoryBuffer(std::string Path) {
   ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
       MemoryBuffer::getFileOrSTDIN(Path);
   if (std::error_code EC = BufferOrErr.getError())
     return EC;
-  Buffer = std::move(BufferOrErr.get());
+  auto Buffer = std::move(BufferOrErr.get());
 
   // Sanity check the file.
   if (Buffer->getBufferSize() > std::numeric_limits<unsigned>::max())
     return instrprof_error::too_large;
-  return instrprof_error::success;
+  return std::move(Buffer);
 }
 
 static std::error_code initializeReader(InstrProfReader &Reader) {
   return Reader.readHeader();
 }
 
-std::error_code
-InstrProfReader::create(std::string Path,
-                        std::unique_ptr<InstrProfReader> &Result) {
+ErrorOr<std::unique_ptr<InstrProfReader>>
+InstrProfReader::create(std::string Path) {
   // Set up the buffer to read.
-  std::unique_ptr<MemoryBuffer> Buffer;
-  if (std::error_code EC = setupMemoryBuffer(Path, Buffer))
+  auto BufferOrError = setupMemoryBuffer(Path);
+  if (std::error_code EC = BufferOrError.getError())
     return EC;
 
+  auto Buffer = std::move(BufferOrError.get());
+  std::unique_ptr<InstrProfReader> Result;
+
   // Create the reader.
   if (IndexedInstrProfReader::hasFormat(*Buffer))
     Result.reset(new IndexedInstrProfReader(std::move(Buffer)));
@@ -58,16 +60,20 @@ InstrProfReader::create(std::string Path
     Result.reset(new TextInstrProfReader(std::move(Buffer)));
 
   // Initialize the reader and return the result.
-  return initializeReader(*Result);
+  if (std::error_code EC = initializeReader(*Result))
+    return EC;
+
+  return std::move(Result);
 }
 
 std::error_code IndexedInstrProfReader::create(
     std::string Path, std::unique_ptr<IndexedInstrProfReader> &Result) {
   // Set up the buffer to read.
-  std::unique_ptr<MemoryBuffer> Buffer;
-  if (std::error_code EC = setupMemoryBuffer(Path, Buffer))
+  auto BufferOrError = setupMemoryBuffer(Path);
+  if (std::error_code EC = BufferOrError.getError())
     return EC;
 
+  auto Buffer = std::move(BufferOrError.get());
   // Create the reader.
   if (!IndexedInstrProfReader::hasFormat(*Buffer))
     return instrprof_error::bad_magic;

Modified: llvm/trunk/lib/ProfileData/SampleProfReader.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/ProfileData/SampleProfReader.cpp?rev=221120&r1=221119&r2=221120&view=diff
==============================================================================
--- llvm/trunk/lib/ProfileData/SampleProfReader.cpp (original)
+++ llvm/trunk/lib/ProfileData/SampleProfReader.cpp Sun Nov  2 18:51:45 2014
@@ -356,18 +356,18 @@ bool SampleProfileReaderBinary::hasForma
 /// \brief Prepare a memory buffer for the contents of \p Filename.
 ///
 /// \returns an error code indicating the status of the buffer.
-static std::error_code
-setupMemoryBuffer(std::string Filename, std::unique_ptr<MemoryBuffer> &Buffer) {
+static ErrorOr<std::unique_ptr<MemoryBuffer>>
+setupMemoryBuffer(std::string Filename) {
   auto BufferOrErr = MemoryBuffer::getFileOrSTDIN(Filename);
   if (std::error_code EC = BufferOrErr.getError())
     return EC;
-  Buffer = std::move(BufferOrErr.get());
+  auto Buffer = std::move(BufferOrErr.get());
 
   // Sanity check the file.
   if (Buffer->getBufferSize() > std::numeric_limits<unsigned>::max())
     return sampleprof_error::too_large;
 
-  return sampleprof_error::success;
+  return std::move(Buffer);
 }
 
 /// \brief Create a sample profile reader based on the format of the input file.
@@ -379,18 +379,21 @@ setupMemoryBuffer(std::string Filename,
 /// \param C The LLVM context to use to emit diagnostics.
 ///
 /// \returns an error code indicating the status of the created reader.
-std::error_code
-SampleProfileReader::create(StringRef Filename,
-                            std::unique_ptr<SampleProfileReader> &Reader,
-                            LLVMContext &C) {
-  std::unique_ptr<MemoryBuffer> Buffer;
-  if (std::error_code EC = setupMemoryBuffer(Filename, Buffer))
+ErrorOr<std::unique_ptr<SampleProfileReader>>
+SampleProfileReader::create(StringRef Filename, LLVMContext &C) {
+  auto BufferOrError = setupMemoryBuffer(Filename);
+  if (std::error_code EC = BufferOrError.getError())
     return EC;
 
+  auto Buffer = std::move(BufferOrError.get());
+  std::unique_ptr<SampleProfileReader> Reader;
   if (SampleProfileReaderBinary::hasFormat(*Buffer))
     Reader.reset(new SampleProfileReaderBinary(std::move(Buffer), C));
   else
     Reader.reset(new SampleProfileReaderText(std::move(Buffer), C));
 
-  return Reader->readHeader();
+  if (std::error_code EC = Reader->readHeader())
+    return EC;
+
+  return std::move(Reader);
 }

Modified: llvm/trunk/lib/ProfileData/SampleProfWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/ProfileData/SampleProfWriter.cpp?rev=221120&r1=221119&r2=221120&view=diff
==============================================================================
--- llvm/trunk/lib/ProfileData/SampleProfWriter.cpp (original)
+++ llvm/trunk/lib/ProfileData/SampleProfWriter.cpp Sun Nov  2 18:51:45 2014
@@ -107,11 +107,10 @@ bool SampleProfileWriterBinary::write(St
 /// \param Format Encoding format for the profile file.
 ///
 /// \returns an error code indicating the status of the created writer.
-std::error_code
-SampleProfileWriter::create(StringRef Filename,
-                            std::unique_ptr<SampleProfileWriter> &Writer,
-                            SampleProfileFormat Format) {
+ErrorOr<std::unique_ptr<SampleProfileWriter>>
+SampleProfileWriter::create(StringRef Filename, SampleProfileFormat Format) {
   std::error_code EC;
+  std::unique_ptr<SampleProfileWriter> Writer;
 
   if (Format == SPF_Binary)
     Writer.reset(new SampleProfileWriterBinary(Filename, EC));
@@ -120,5 +119,8 @@ SampleProfileWriter::create(StringRef Fi
   else
     EC = sampleprof_error::unrecognized_format;
 
-  return EC;
+  if (EC)
+    return EC;
+
+  return std::move(Writer);
 }

Modified: llvm/trunk/lib/Transforms/Scalar/SampleProfile.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/SampleProfile.cpp?rev=221120&r1=221119&r2=221120&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/SampleProfile.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/SampleProfile.cpp Sun Nov  2 18:51:45 2014
@@ -737,12 +737,13 @@ INITIALIZE_PASS_END(SampleProfileLoader,
                     "Sample Profile loader", false, false)
 
 bool SampleProfileLoader::doInitialization(Module &M) {
-  if (std::error_code EC =
-          SampleProfileReader::create(Filename, Reader, M.getContext())) {
+  auto ReaderOrErr = SampleProfileReader::create(Filename, M.getContext());
+  if (std::error_code EC = ReaderOrErr.getError()) {
     std::string Msg = "Could not open profile: " + EC.message();
     M.getContext().diagnose(DiagnosticInfoSampleProfile(Filename.data(), Msg));
     return false;
   }
+  Reader = std::move(ReaderOrErr.get());
   ProfileIsValid = (Reader->read() == sampleprof_error::success);
   return true;
 }

Modified: llvm/trunk/tools/llvm-profdata/llvm-profdata.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-profdata/llvm-profdata.cpp?rev=221120&r1=221119&r2=221120&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-profdata/llvm-profdata.cpp (original)
+++ llvm/trunk/tools/llvm-profdata/llvm-profdata.cpp Sun Nov  2 18:51:45 2014
@@ -49,10 +49,11 @@ void mergeInstrProfile(cl::list<std::str
 
   InstrProfWriter Writer;
   for (const auto &Filename : Inputs) {
-    std::unique_ptr<InstrProfReader> Reader;
-    if (std::error_code ec = InstrProfReader::create(Filename, Reader))
+    auto ReaderOrErr = InstrProfReader::create(Filename);
+    if (std::error_code ec = ReaderOrErr.getError())
       exitWithError(ec.message(), Filename);
 
+    auto Reader = std::move(ReaderOrErr.get());
     for (const auto &I : *Reader)
       if (std::error_code EC =
               Writer.addFunctionCounts(I.Name, I.Hash, I.Counts))
@@ -66,18 +67,19 @@ void mergeInstrProfile(cl::list<std::str
 void mergeSampleProfile(cl::list<std::string> Inputs, StringRef OutputFilename,
                         sampleprof::SampleProfileFormat OutputFormat) {
   using namespace sampleprof;
-  std::unique_ptr<SampleProfileWriter> Writer;
-  if (std::error_code EC = SampleProfileWriter::create(OutputFilename.data(),
-                                                       Writer, OutputFormat))
+  auto WriterOrErr = SampleProfileWriter::create(OutputFilename, OutputFormat);
+  if (std::error_code EC = WriterOrErr.getError())
     exitWithError(EC.message(), OutputFilename);
 
+  auto Writer = std::move(WriterOrErr.get());
   StringMap<FunctionSamples> ProfileMap;
   for (const auto &Filename : Inputs) {
-    std::unique_ptr<SampleProfileReader> Reader;
-    if (std::error_code EC =
-            SampleProfileReader::create(Filename, Reader, getGlobalContext()))
+    auto ReaderOrErr =
+        SampleProfileReader::create(Filename, getGlobalContext());
+    if (std::error_code EC = ReaderOrErr.getError())
       exitWithError(EC.message(), Filename);
 
+    auto Reader = std::move(ReaderOrErr.get());
     if (std::error_code EC = Reader->read())
       exitWithError(EC.message(), Filename);
 
@@ -129,10 +131,11 @@ int merge_main(int argc, const char *arg
 int showInstrProfile(std::string Filename, bool ShowCounts,
                      bool ShowAllFunctions, std::string ShowFunction,
                      raw_fd_ostream &OS) {
-  std::unique_ptr<InstrProfReader> Reader;
-  if (std::error_code EC = InstrProfReader::create(Filename, Reader))
+  auto ReaderOrErr = InstrProfReader::create(Filename);
+  if (std::error_code EC = ReaderOrErr.getError())
     exitWithError(EC.message(), Filename);
 
+  auto Reader = std::move(ReaderOrErr.get());
   uint64_t MaxFunctionCount = 0, MaxBlockCount = 0;
   size_t ShownFunctions = 0, TotalFunctions = 0;
   for (const auto &Func : *Reader) {
@@ -182,11 +185,11 @@ int showSampleProfile(std::string Filena
                       bool ShowAllFunctions, std::string ShowFunction,
                       raw_fd_ostream &OS) {
   using namespace sampleprof;
-  std::unique_ptr<SampleProfileReader> Reader;
-  if (std::error_code EC =
-          SampleProfileReader::create(Filename, Reader, getGlobalContext()))
+  auto ReaderOrErr = SampleProfileReader::create(Filename, getGlobalContext());
+  if (std::error_code EC = ReaderOrErr.getError())
     exitWithError(EC.message(), Filename);
 
+  auto Reader = std::move(ReaderOrErr.get());
   Reader->read();
   if (ShowAllFunctions || ShowFunction.empty())
     Reader->dump(OS);





More information about the llvm-commits mailing list