Reapply "[PGO] Fix name encoding for ObjC-like functions"
authorVedant Kumar <vsk@apple.com>
Mon, 28 Mar 2016 20:12:07 +0000 (20:12 +0000)
committerVedant Kumar <vsk@apple.com>
Mon, 28 Mar 2016 20:12:07 +0000 (20:12 +0000)
Function names in ObjC can have spaces in them. This interacts poorly
with name compression, which uses spaces to separate PGO names. Fix the
issue by using a different separator and update a test.

I chose "\01" as the separator because 1) it's non-printable, 2) we
strip it from PGO names, and 3) it's the next natural choice once "\00"
is discarded (that one's overloaded).

This reverts the revert commit beaf3d18. What's changed?

- I fixed up the covmap-V2 binary format tests using a linux VM.
- I updated the expected counts in instrprof-comdat.h to account for
  the fact that there have been bugfixes to clang coverage.
- I added an assert to make sure we don't get bitten by this again.

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

llvm-svn: 264641

12 files changed:
llvm/include/llvm/ProfileData/InstrProf.h
llvm/lib/ProfileData/InstrProf.cpp
llvm/test/tools/llvm-cov/Inputs/binary-formats.v2.linux32l
llvm/test/tools/llvm-cov/Inputs/binary-formats.v2.linux64l
llvm/test/tools/llvm-cov/Inputs/elf_binary_comdat.profdata
llvm/test/tools/llvm-cov/Inputs/instrprof-comdat.h
llvm/test/tools/llvm-profdata/Inputs/c-general.profraw
llvm/test/tools/llvm-profdata/raw-32-bits-be.test
llvm/test/tools/llvm-profdata/raw-32-bits-le.test
llvm/test/tools/llvm-profdata/raw-64-bits-be.test
llvm/test/tools/llvm-profdata/raw-64-bits-le.test
llvm/unittests/ProfileData/InstrProfTest.cpp

index 3bf0aa3..31fcffe 100644 (file)
@@ -151,6 +151,9 @@ inline StringRef getInstrProfFileOverriderFuncName() {
   return "__llvm_profile_override_default_filename";
 }
 
+/// Return the marker used to separate PGO names during serialization.
+inline StringRef getInstrProfNameSeparator() { return "\01"; }
+
 /// Return the modified name for function \c F suitable to be
 /// used the key for profile lookup.
 std::string getPGOFuncName(const Function &F,
index 2446521..1be3df4 100644 (file)
@@ -158,9 +158,15 @@ void InstrProfSymtab::create(const Module &M) {
 
 int 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;
   std::string UncompressedNameStrings =
-      join(NameStrs.begin(), NameStrs.end(), StringRef(" "));
+      join(NameStrs.begin(), NameStrs.end(), getInstrProfNameSeparator());
+
+  assert(StringRef(UncompressedNameStrings)
+                 .count(getInstrProfNameSeparator()) == (NameStrs.size() - 1) &&
+         "PGO name is invalid (contains separator token)");
 
   unsigned EncLen = encodeULEB128(UncompressedNameStrings.length(), P);
   P += EncLen;
@@ -238,7 +244,7 @@ int readPGOFuncNameStrings(StringRef NameStrings, InstrProfSymtab &Symtab) {
     }
     // Now parse the name strings.
     SmallVector<StringRef, 0> Names;
-    NameStrings.split(Names, ' ');
+    NameStrings.split(Names, getInstrProfNameSeparator());
     for (StringRef &Name : Names)
       Symtab.addFuncName(Name);
 
index 624483b..6ea83e5 100755 (executable)
Binary files a/llvm/test/tools/llvm-cov/Inputs/binary-formats.v2.linux32l and b/llvm/test/tools/llvm-cov/Inputs/binary-formats.v2.linux32l differ
index af4d39c..9c6835c 100755 (executable)
Binary files a/llvm/test/tools/llvm-cov/Inputs/binary-formats.v2.linux64l and b/llvm/test/tools/llvm-cov/Inputs/binary-formats.v2.linux64l differ
index 8566704..ef80e56 100644 (file)
Binary files a/llvm/test/tools/llvm-cov/Inputs/elf_binary_comdat.profdata and b/llvm/test/tools/llvm-cov/Inputs/elf_binary_comdat.profdata differ
index e122716..a701504 100644 (file)
@@ -13,7 +13,12 @@ template <class T> T FOO<T>::DoIt(T ti) { // HEADER:  2| [[@LINE]]|template
     t += I;                               // HEADER: 20| [[@LINE]]|    t += I;
     if (I > ti / 2)                       // HEADER: 20| [[@LINE]]|    if (I > ti 
       t -= 1;                             // HEADER:  8| [[@LINE]]|      t -= 1;
-  }                                       // HEADER: 10| [[@LINE]]|  }
-                                          // HEADER:  1| [[@LINE]]|
-  return t;                               // HEADER:  1| [[@LINE]]|  return t;
+  }                                       // HEADER: 20| [[@LINE]]|  }
+                                          // HEADER:  2| [[@LINE]]|
+  return t;                               // HEADER:  2| [[@LINE]]|  return t;
 }
+
+// To generate the binaries which correspond to this file, you must first
+// compile a program with two calls to Foo<int>::DoIt(10) for each desired
+// architecture. Collect a raw profile from any one of these binaries, index
+// it, and check it in along with the executables.
index 6028383..34448c1 100644 (file)
Binary files a/llvm/test/tools/llvm-profdata/Inputs/c-general.profraw and b/llvm/test/tools/llvm-profdata/Inputs/c-general.profraw differ
index d3def18..bc6ec02 100644 (file)
@@ -28,7 +28,7 @@ RUN: printf '\0\0\0\0\0\0\0\0' >> %t
 RUN: printf '\0\0\0\0\0\0\0\023' >> %t
 RUN: printf '\0\0\0\0\0\0\0\067' >> %t
 RUN: printf '\0\0\0\0\0\0\0\101' >> %t
-RUN: printf '\7\0foo bar\0\0\0\0\0\0\0' >> %t
+RUN: printf '\7\0foo\1bar\0\0\0\0\0\0\0' >> %t
 
 RUN: llvm-profdata show %t -all-functions -counts | FileCheck %s
 
index b19c74f..5ee8b33 100644 (file)
@@ -28,7 +28,7 @@ RUN: printf '\0\0\0\0\0\0\0\0' >> %t
 RUN: printf '\023\0\0\0\0\0\0\0' >> %t
 RUN: printf '\067\0\0\0\0\0\0\0' >> %t
 RUN: printf '\101\0\0\0\0\0\0\0' >> %t
-RUN: printf '\7\0foo bar\0\0\0\0\0\0\0' >> %t
+RUN: printf '\7\0foo\1bar\0\0\0\0\0\0\0' >> %t
 
 RUN: llvm-profdata show %t -all-functions -counts | FileCheck %s
 
index cb59f9a..9778f06 100644 (file)
@@ -26,7 +26,7 @@ RUN: printf '\0\0\0\02\0\0\0\0' >> %t
 RUN: printf '\0\0\0\0\0\0\0\023' >> %t
 RUN: printf '\0\0\0\0\0\0\0\067' >> %t
 RUN: printf '\0\0\0\0\0\0\0\101' >> %t
-RUN: printf '\7\0foo bar\0\0\0\0\0\0\0' >> %t
+RUN: printf '\7\0foo\1bar\0\0\0\0\0\0\0' >> %t
 
 RUN: llvm-profdata show %t -all-functions -counts | FileCheck %s
 
index 86748b1..670bdb3 100644 (file)
@@ -26,7 +26,7 @@ RUN: printf '\02\0\0\0\0\0\0\0' >> %t
 RUN: printf '\023\0\0\0\0\0\0\0' >> %t
 RUN: printf '\067\0\0\0\0\0\0\0' >> %t
 RUN: printf '\101\0\0\0\0\0\0\0' >> %t
-RUN: printf '\7\0foo bar\0\0\0\0\0\0\0' >> %t
+RUN: printf '\7\0foo\1bar\0\0\0\0\0\0\0' >> %t
 
 RUN: llvm-profdata show %t -all-functions -counts | FileCheck %s
 
index 13d1e05..7dd072c 100644 (file)
@@ -893,7 +893,7 @@ TEST_P(MaybeSparseInstrProfTest, instr_prof_symtab_compression_test) {
     OS << "func_" << I;
     FuncNames1.push_back(OS.str());
     str.clear();
-    OS << "fooooooooooooooo_" << I;
+    OS << "f oooooooooooooo_" << I;
     FuncNames1.push_back(OS.str());
     str.clear();
     OS << "BAR_" << I;
@@ -931,7 +931,7 @@ TEST_P(MaybeSparseInstrProfTest, instr_prof_symtab_compression_test) {
       StringRef R = Symtab.getFuncName(IndexedInstrProf::ComputeHash(FuncNames1[0]));
       ASSERT_EQ(StringRef("func_0"), R);
       R = Symtab.getFuncName(IndexedInstrProf::ComputeHash(FuncNames1[1]));
-      ASSERT_EQ(StringRef("fooooooooooooooo_0"), R);
+      ASSERT_EQ(StringRef("f oooooooooooooo_0"), R);
       for (int I = 0; I < 3; I++) {
         std::string N[4];
         N[0] = FuncNames1[2 * I];