[PATCH] D25687: [PGO] fix bogus warning for merging empty llvm profile file

Rong Xu via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 17 11:22:20 PDT 2016


xur created this revision.
xur added a reviewer: davidxl.
xur added a subscriber: llvm-commits.

Profile runtime can generate an empty profile (when there is no function in the shared library). This empty profile is treated as a text format profile.  A test format profile without the flag of "#IR" is thought to be a clang generated profile.  So in llvm profile merging, we will get a bogus warning of "Merge IR generated profile with Clang generated profile."

The fix here is to skip the empty profile (when the buffer size is 0) by returning nullptr in InstrProfReader::create().

This patch also fixes a use-after-move instance in loadInput() in llvm-profdata.cpp.


https://reviews.llvm.org/D25687

Files:
  include/llvm/ProfileData/InstrProfReader.h
  lib/ProfileData/InstrProfReader.cpp
  tools/llvm-profdata/llvm-profdata.cpp


Index: tools/llvm-profdata/llvm-profdata.cpp
===================================================================
--- tools/llvm-profdata/llvm-profdata.cpp
+++ tools/llvm-profdata/llvm-profdata.cpp
@@ -144,6 +144,9 @@
     return;
 
   auto Reader = std::move(ReaderOrErr.get());
+  // Nullptr Reader means the file is empty. Skip it.
+  if (!Reader)
+    return;
   bool IsIRProfile = Reader->isIRLevelProfile();
   if (WC->Writer.setIsIRLevelProfile(IsIRProfile)) {
     WC->Err = make_error<StringError>(
@@ -153,6 +156,7 @@
   }
 
   for (auto &I : *Reader) {
+    const StringRef FuncName = I.Name;
     if (Error E = WC->Writer.addRecord(std::move(I), Input.Weight)) {
       // Only show hint the first time an error occurs.
       instrprof_error IPE = InstrProfError::take(std::move(E));
@@ -159,7 +163,7 @@
       std::unique_lock<std::mutex> ErrGuard{WC->ErrLock};
       bool firstTime = WC->WriterErrorCodes.insert(IPE).second;
       handleMergeWriterError(make_error<InstrProfError>(IPE), Input.Filename,
-                             I.Name, firstTime);
+                             FuncName, firstTime);
     }
   }
   if (Reader->hasError())
Index: lib/ProfileData/InstrProfReader.cpp
===================================================================
--- lib/ProfileData/InstrProfReader.cpp
+++ lib/ProfileData/InstrProfReader.cpp
@@ -46,6 +46,10 @@
   if (Buffer->getBufferSize() > std::numeric_limits<unsigned>::max())
     return make_error<InstrProfError>(instrprof_error::too_large);
 
+  // Return nullptr for an empty file.
+  if (Buffer->getBufferSize() == 0)
+    return std::unique_ptr<InstrProfReader>{};
+
   std::unique_ptr<InstrProfReader> Result;
   // Create the reader.
   if (IndexedInstrProfReader::hasFormat(*Buffer))
Index: include/llvm/ProfileData/InstrProfReader.h
===================================================================
--- include/llvm/ProfileData/InstrProfReader.h
+++ include/llvm/ProfileData/InstrProfReader.h
@@ -104,7 +104,7 @@
   }
 
   /// Factory method to create an appropriately typed reader for the given
-  /// instrprof file.
+  /// instrprof file. Nullptr will be returned for an empty profile file.
   static Expected<std::unique_ptr<InstrProfReader>> create(const Twine &Path);
 
   static Expected<std::unique_ptr<InstrProfReader>>


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D25687.74873.patch
Type: text/x-patch
Size: 2306 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20161017/6c0f46c5/attachment.bin>


More information about the llvm-commits mailing list