]> granicus.if.org Git - llvm/commitdiff
[ProfileData] Add error codes for compression failures
authorVedant Kumar <vsk@apple.com>
Tue, 3 May 2016 16:53:17 +0000 (16:53 +0000)
committerVedant Kumar <vsk@apple.com>
Tue, 3 May 2016 16:53:17 +0000 (16:53 +0000)
Be more specific in describing compression failures. Also, check for
this kind of error in emitNameData().

This is part of a series of patches to transition ProfileData over to
the stricter Error/Expected interface.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@268400 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/ProfileData/InstrProf.h
lib/ProfileData/InstrProf.cpp
lib/Transforms/Instrumentation/InstrProfiling.cpp

index 88fc61a9a889a00ced90ea8752f9baadc01d5889..f07a5406339627a680617e83c8335cc55ecc91bd 100644 (file)
@@ -204,17 +204,20 @@ StringRef getFuncNameWithoutPrefix(StringRef PGOFuncName,
 ///  third field is the uncompressed strings; otherwise it is the
 /// compressed string. When the string compression is off, the
 /// second field will have value zero.
-int collectPGOFuncNameStrings(const std::vector<std::string> &NameStrs,
-                              bool doCompression, std::string &Result);
+std::error_code
+collectPGOFuncNameStrings(const std::vector<std::string> &NameStrs,
+                          bool doCompression, std::string &Result);
 /// Produce \c Result string with the same format described above. The input
 /// is vector of PGO function name variables that are referenced.
-int collectPGOFuncNameStrings(const std::vector<GlobalVariable *> &NameVars,
-                              std::string &Result, bool doCompression = true);
+std::error_code
+collectPGOFuncNameStrings(const std::vector<GlobalVariable *> &NameVars,
+                          std::string &Result, bool doCompression = true);
 class InstrProfSymtab;
 /// \c NameStrings is a string composed of one of more sub-strings encoded in
 /// the format described above. The substrings are seperated by 0 or more zero
 /// bytes. This method decodes the string and populates the \c Symtab.
-int readPGOFuncNameStrings(StringRef NameStrings, InstrProfSymtab &Symtab);
+std::error_code readPGOFuncNameStrings(StringRef NameStrings,
+                                       InstrProfSymtab &Symtab);
 
 enum InstrProfValueKind : uint32_t {
 #define VALUE_PROF_KIND(Enumerator, Value) Enumerator = Value,
@@ -272,7 +275,9 @@ enum class instrprof_error {
   hash_mismatch,
   count_mismatch,
   counter_overflow,
-  value_site_count_mismatch
+  value_site_count_mismatch,
+  compress_failed,
+  uncompress_failed
 };
 
 inline std::error_code make_error_code(instrprof_error E) {
@@ -390,9 +395,7 @@ std::error_code InstrProfSymtab::create(StringRef D, uint64_t BaseAddr) {
 }
 
 std::error_code InstrProfSymtab::create(StringRef NameStrings) {
-  if (readPGOFuncNameStrings(NameStrings, *this))
-    return make_error_code(instrprof_error::malformed);
-  return std::error_code();
+  return readPGOFuncNameStrings(NameStrings, *this);
 }
 
 template <typename NameIterRange>
index 7e84a2702bd4ea22ef2567859a2a4dfd4ab895d9..f459d2d54314a7096cf0956496682c9141e9a4c1 100644 (file)
@@ -62,6 +62,10 @@ class InstrProfErrorCategoryType : public std::error_category {
       return "Counter overflow";
     case instrprof_error::value_site_count_mismatch:
       return "Function value site count change detected (counter mismatch)";
+    case instrprof_error::compress_failed:
+      return "Failed to compress data (zlib)";
+    case instrprof_error::uncompress_failed:
+      return "Failed to uncompress data (zlib)";
     }
     llvm_unreachable("A value of instrprof_error has no message.");
   }
@@ -185,8 +189,9 @@ void InstrProfSymtab::create(Module &M, bool InLTO) {
   finalizeSymtab();
 }
 
-int collectPGOFuncNameStrings(const std::vector<std::string> &NameStrs,
-                              bool doCompression, std::string &Result) {
+std::error_code
+collectPGOFuncNameStrings(const std::vector<std::string> &NameStrs,
+                          bool doCompression, std::string &Result) {
   assert(NameStrs.size() && "No name data to emit");
 
   uint8_t Header[16], *P = Header;
@@ -208,7 +213,7 @@ int collectPGOFuncNameStrings(const std::vector<std::string> &NameStrs,
     unsigned HeaderLen = P - &Header[0];
     Result.append(HeaderStr, HeaderLen);
     Result += InputStr;
-    return 0;
+    return make_error_code(instrprof_error::success);
   };
 
   if (!doCompression)
@@ -220,7 +225,7 @@ int collectPGOFuncNameStrings(const std::vector<std::string> &NameStrs,
                      zlib::BestSizeCompression);
 
   if (Success != zlib::StatusOK)
-    return 1;
+    return make_error_code(instrprof_error::compress_failed);
 
   return WriteStringToResult(
       CompressedNameStrings.size(),
@@ -234,8 +239,9 @@ StringRef getPGOFuncNameVarInitializer(GlobalVariable *NameVar) {
   return NameStr;
 }
 
-int collectPGOFuncNameStrings(const std::vector<GlobalVariable *> &NameVars,
-                              std::string &Result, bool doCompression) {
+std::error_code
+collectPGOFuncNameStrings(const std::vector<GlobalVariable *> &NameVars,
+                          std::string &Result, bool doCompression) {
   std::vector<std::string> NameStrs;
   for (auto *NameVar : NameVars) {
     NameStrs.push_back(getPGOFuncNameVarInitializer(NameVar));
@@ -244,7 +250,8 @@ int collectPGOFuncNameStrings(const std::vector<GlobalVariable *> &NameVars,
       NameStrs, zlib::isAvailable() && doCompression, Result);
 }
 
-int readPGOFuncNameStrings(StringRef NameStrings, InstrProfSymtab &Symtab) {
+std::error_code readPGOFuncNameStrings(StringRef NameStrings,
+                                       InstrProfSymtab &Symtab) {
   const uint8_t *P = reinterpret_cast<const uint8_t *>(NameStrings.data());
   const uint8_t *EndP = reinterpret_cast<const uint8_t *>(NameStrings.data() +
                                                           NameStrings.size());
@@ -262,7 +269,7 @@ int readPGOFuncNameStrings(StringRef NameStrings, InstrProfSymtab &Symtab) {
                                       CompressedSize);
       if (zlib::uncompress(CompressedNameStrings, UncompressedNameStrings,
                            UncompressedSize) != zlib::StatusOK)
-        return 1;
+        return make_error_code(instrprof_error::uncompress_failed);
       P += CompressedSize;
       NameStrings = StringRef(UncompressedNameStrings.data(),
                               UncompressedNameStrings.size());
@@ -281,7 +288,7 @@ int readPGOFuncNameStrings(StringRef NameStrings, InstrProfSymtab &Symtab) {
       P++;
   }
   Symtab.finalizeSymtab();
-  return 0;
+  return make_error_code(instrprof_error::success);
 }
 
 instrprof_error InstrProfValueSiteRecord::merge(InstrProfValueSiteRecord &Input,
index 397e64b410e1b95b53698032e0545367d57190d1..930001fd6840cb1387684e5813c7f4f313a38bfd 100644 (file)
@@ -374,8 +374,10 @@ void InstrProfiling::emitNameData() {
     return;
 
   std::string CompressedNameStr;
-  collectPGOFuncNameStrings(ReferencedNames, CompressedNameStr,
-                            DoNameCompression);
+  if (auto EC = collectPGOFuncNameStrings(ReferencedNames, CompressedNameStr,
+                                          DoNameCompression)) {
+    llvm::report_fatal_error(EC.message(), false);
+  }
 
   auto &Ctx = M->getContext();
   auto *NamesVal = llvm::ConstantDataArray::getString(