[llvm] 18ffb5d - [InstrProf] Prevent duplicate functions in correlated data

Ellis Hoag via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 28 14:21:05 PST 2021


Author: Ellis Hoag
Date: 2021-12-28T14:20:59-08:00
New Revision: 18ffb5dc2503c81df53274d6a2e1945ac08c56d2

URL: https://github.com/llvm/llvm-project/commit/18ffb5dc2503c81df53274d6a2e1945ac08c56d2
DIFF: https://github.com/llvm/llvm-project/commit/18ffb5dc2503c81df53274d6a2e1945ac08c56d2.diff

LOG: [InstrProf] Prevent duplicate functions in correlated data

When using debug info for profile correlation, avoid adding duplicate
functions in the synthetic Data section.

Before this patch, n duplicate function entries in the Data section would
cause counter values to be a factor of n larger. I built instrumented
clang with and without debug info correlation and got these summaries.

```
# With Debug Info Correlate
$ llvm-profdata show default.profdata
Instrumentation level: IR  entry_first = 0
Total functions: 182530
Maximum function count: 52034
Maximum internal block count: 5763

# Without
$ llvm-profdata show default.profdata
Instrumentation level: IR  entry_first = 0
Total functions: 183212
Maximum function count: 52034
Maximum internal block count: 5766
```

The slight difference in counts seem to be mostly from FileSystem and
Map functions and the difference in the number of instrumented functions
seems to come from missing debug info like destructors without source.

Reviewed By: kyulee

Differential Revision: https://reviews.llvm.org/D116051

Added: 
    compiler-rt/test/profile/Inputs/instrprof-debug-info-correlate-bar.h
    compiler-rt/test/profile/Inputs/instrprof-debug-info-correlate-foo.cpp
    compiler-rt/test/profile/Inputs/instrprof-debug-info-correlate-main.cpp

Modified: 
    compiler-rt/test/profile/Darwin/instrprof-debug-info-correlate.c
    compiler-rt/test/profile/Linux/instrprof-debug-info-correlate.c
    llvm/include/llvm/ProfileData/InstrProfCorrelator.h
    llvm/lib/ProfileData/InstrProfCorrelator.cpp

Removed: 
    


################################################################################
diff  --git a/compiler-rt/test/profile/Darwin/instrprof-debug-info-correlate.c b/compiler-rt/test/profile/Darwin/instrprof-debug-info-correlate.c
index b35cfe85956a5..837e1f428d1e5 100644
--- a/compiler-rt/test/profile/Darwin/instrprof-debug-info-correlate.c
+++ b/compiler-rt/test/profile/Darwin/instrprof-debug-info-correlate.c
@@ -1,33 +1,12 @@
 // REQUIRES: zlib
 
 // Value profiling is currently not supported in lightweight mode.
-// RUN: %clang_pgogen -o %t.normal -mllvm --disable-vp=true %s
-// RUN: env LLVM_PROFILE_FILE=%t.profraw %run %t.normal
-// RUN: llvm-profdata merge -o %t.normal.profdata %t.profraw
-
-// RUN: %clang_pgogen -o %t -g -mllvm --debug-info-correlate -mllvm --disable-vp=true %s
+// RUN: %clang_pgogen -o %t -g -mllvm --debug-info-correlate -mllvm --disable-vp=true %S/../Inputs/instrprof-debug-info-correlate-main.cpp %S/../Inputs/instrprof-debug-info-correlate-foo.cpp
 // RUN: env LLVM_PROFILE_FILE=%t.proflite %run %t
 // RUN: llvm-profdata merge -o %t.profdata --debug-info=%t.dSYM %t.proflite
 
-// RUN: 
diff  %t.normal.profdata %t.profdata
-
-int foo(int a) {
-  if (a % 2)
-    return 4 * a + 1;
-  return 0;
-}
-
-int bar(int a) {
-  while (a > 100)
-    a /= 2;
-  return a;
-}
-
-typedef int (*FP)(int);
-FP Fps[3] = {foo, bar};
+// RUN: %clang_pgogen -o %t.normal -mllvm --disable-vp=true %S/../Inputs/instrprof-debug-info-correlate-main.cpp %S/../Inputs/instrprof-debug-info-correlate-foo.cpp
+// RUN: env LLVM_PROFILE_FILE=%t.profraw %run %t.normal
+// RUN: llvm-profdata merge -o %t.normal.profdata %t.profraw
 
-int main() {
-  for (int i = 0; i < 5; i++)
-    Fps[i % 2](i);
-  return 0;
-}
+// RUN: 
diff  %t.normal.profdata %t.profdata

diff  --git a/compiler-rt/test/profile/Inputs/instrprof-debug-info-correlate-bar.h b/compiler-rt/test/profile/Inputs/instrprof-debug-info-correlate-bar.h
new file mode 100644
index 0000000000000..4ee9cdacc8359
--- /dev/null
+++ b/compiler-rt/test/profile/Inputs/instrprof-debug-info-correlate-bar.h
@@ -0,0 +1,7 @@
+int foo(int);
+
+inline int bar(int a) {
+  while (a > 100)
+    a /= 2;
+  return a;
+}

diff  --git a/compiler-rt/test/profile/Inputs/instrprof-debug-info-correlate-foo.cpp b/compiler-rt/test/profile/Inputs/instrprof-debug-info-correlate-foo.cpp
new file mode 100644
index 0000000000000..e9f8e7a5570e0
--- /dev/null
+++ b/compiler-rt/test/profile/Inputs/instrprof-debug-info-correlate-foo.cpp
@@ -0,0 +1,7 @@
+#include "instrprof-debug-info-correlate-bar.h"
+
+int foo(int a) {
+  if (a % 2)
+    return 4 * a + 1;
+  return bar(a);
+}

diff  --git a/compiler-rt/test/profile/Inputs/instrprof-debug-info-correlate-main.cpp b/compiler-rt/test/profile/Inputs/instrprof-debug-info-correlate-main.cpp
new file mode 100644
index 0000000000000..13a8cf4f8d95a
--- /dev/null
+++ b/compiler-rt/test/profile/Inputs/instrprof-debug-info-correlate-main.cpp
@@ -0,0 +1,10 @@
+#include "instrprof-debug-info-correlate-bar.h"
+
+typedef int (*FP)(int);
+FP Fps[2] = {foo, bar};
+
+int main() {
+  for (int i = 0; i < 5; i++)
+    Fps[i % 2](i);
+  return 0;
+}

diff  --git a/compiler-rt/test/profile/Linux/instrprof-debug-info-correlate.c b/compiler-rt/test/profile/Linux/instrprof-debug-info-correlate.c
index df3a94837ed33..c78614f28b11e 100644
--- a/compiler-rt/test/profile/Linux/instrprof-debug-info-correlate.c
+++ b/compiler-rt/test/profile/Linux/instrprof-debug-info-correlate.c
@@ -1,33 +1,12 @@
 // REQUIRES: zlib
 
 // Value profiling is currently not supported in lightweight mode.
-// RUN: %clang_pgogen -o %t.normal -mllvm --disable-vp=true %s
-// RUN: env LLVM_PROFILE_FILE=%t.profraw %run %t.normal
-// RUN: llvm-profdata merge -o %t.normal.profdata %t.profraw
-
-// RUN: %clang_pgogen -o %t -g -mllvm --debug-info-correlate -mllvm --disable-vp=true %s
+// RUN: %clang_pgogen -o %t -g -mllvm --debug-info-correlate -mllvm --disable-vp=true %S/../Inputs/instrprof-debug-info-correlate-main.cpp %S/../Inputs/instrprof-debug-info-correlate-foo.cpp
 // RUN: env LLVM_PROFILE_FILE=%t.proflite %run %t
 // RUN: llvm-profdata merge -o %t.profdata --debug-info=%t %t.proflite
 
-// RUN: 
diff  %t.normal.profdata %t.profdata
-
-int foo(int a) {
-  if (a % 2)
-    return 4 * a + 1;
-  return 0;
-}
-
-int bar(int a) {
-  while (a > 100)
-    a /= 2;
-  return a;
-}
-
-typedef int (*FP)(int);
-FP Fps[3] = {foo, bar};
+// RUN: %clang_pgogen -o %t.normal -mllvm --disable-vp=true %S/../Inputs/instrprof-debug-info-correlate-main.cpp %S/../Inputs/instrprof-debug-info-correlate-foo.cpp
+// RUN: env LLVM_PROFILE_FILE=%t.profraw %run %t.normal
+// RUN: llvm-profdata merge -o %t.normal.profdata %t.profraw
 
-int main() {
-  for (int i = 0; i < 5; i++)
-    Fps[i % 2](i);
-  return 0;
-}
+// RUN: 
diff  %t.normal.profdata %t.profdata

diff  --git a/llvm/include/llvm/ProfileData/InstrProfCorrelator.h b/llvm/include/llvm/ProfileData/InstrProfCorrelator.h
index eae7b4e0322c9..81ecbc2813ab1 100644
--- a/llvm/include/llvm/ProfileData/InstrProfCorrelator.h
+++ b/llvm/include/llvm/ProfileData/InstrProfCorrelator.h
@@ -12,6 +12,7 @@
 #ifndef LLVM_PROFILEDATA_INSTRPROFCORRELATOR_H
 #define LLVM_PROFILEDATA_INSTRPROFCORRELATOR_H
 
+#include "llvm/ADT/DenseSet.h"
 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
 #include "llvm/Object/Binary.h"
 #include "llvm/Object/ObjectFile.h"
@@ -110,6 +111,7 @@ class InstrProfCorrelatorImpl : public InstrProfCorrelator {
                           std::unique_ptr<InstrProfCorrelator::Context> Ctx)
       : InstrProfCorrelator(Kind, std::move(Ctx)){};
   std::vector<std::string> Names;
+  llvm::DenseSet<IntPtrT> CounterOffsets;
 
   // Byte-swap the value if necessary.
   template <class T> T maybeSwap(T Value) const {

diff  --git a/llvm/lib/ProfileData/InstrProfCorrelator.cpp b/llvm/lib/ProfileData/InstrProfCorrelator.cpp
index f9c113027da2b..8be2cbff3a20c 100644
--- a/llvm/lib/ProfileData/InstrProfCorrelator.cpp
+++ b/llvm/lib/ProfileData/InstrProfCorrelator.cpp
@@ -129,6 +129,7 @@ Error InstrProfCorrelatorImpl<IntPtrT>::correlateProfileData() {
   correlateProfileDataImpl();
   auto Result =
       collectPGOFuncNameStrings(Names, /*doCompression=*/true, CompressedNames);
+  CounterOffsets.clear();
   Names.clear();
   return Result;
 }
@@ -139,6 +140,9 @@ void InstrProfCorrelatorImpl<IntPtrT>::addProbe(StringRef FunctionName,
                                                 IntPtrT CounterOffset,
                                                 IntPtrT FunctionPtr,
                                                 uint32_t NumCounters) {
+  // Check if a probe was already added for this counter offset.
+  if (!CounterOffsets.insert(CounterOffset).second)
+    return;
   Data.push_back({
       maybeSwap<uint64_t>(IndexedInstrProf::ComputeHash(FunctionName)),
       maybeSwap<uint64_t>(CFGHash),


        


More information about the llvm-commits mailing list