[CSSPGO] Report zero-count probe in profile instead of dangling probes.
authorHongtao Yu <hoy@fb.com>
Tue, 15 Jun 2021 22:59:06 +0000 (15:59 -0700)
committerHongtao Yu <hoy@fb.com>
Wed, 16 Jun 2021 18:45:29 +0000 (11:45 -0700)
Previously dangling samples were represented by INT64_MAX in sample profile while probes never executed were not reported. This was based on an observation that dangling probes were only at a smaller portion than zero-count probes. However, with compiler optimizations, dangling probes end up becoming at large portion of all probes in general and reporting them does not make sense from profile size point of view. This change flips sample reporting by reporting zero-count probes instead. This enabled dangling probe to be represented by none (missing entry in profile). This has a couple benefits:

1. Reducing sample profile size in optimize mode, even when the number of non-executed probes outperform the number of dangling probes, since INT64_MAX takes more space over 0 to encode.

2. Binary size savings. No need to encode dangling probe anymore, since missing probes are treated as dangling in the profile reader.

3. Reducing compiler work to track dangling probes. However, for probes that are real dead and removed, we still need the compiler to identify them so that they can be reported as zero-count, instead of mistreated as dangling probes.

4. Improving counts quality by respecting the counts already collected on the non-dangling copy of a probe. A probe, when duplicated, gets two copies at runtime. If one of them is dangling while the other is not, merging the two probes at profile generation time will cause the real samples collected on the non-dangling one to be discarded. Not reporting the dangling counterpart will keep the real samples.

5. Better readability.

6. Be consistent with non-CS dwarf line number based profile. Zero counts are trusted by the compiler counts inferencer while missing counts will be inferred by the compiler.

Note that the current patch does include any work for #3. There will be follow-up changes.

For #1, I've seen for a large Facebook service, the text profile is reduced by 7%. For extbinary profile, the size of  LBRProfileSection is reduced by 35%.

For #4, I have seen general counts quality for SPEC2017 is improved by 10%.

Reviewed By: wenlei, wlei, wmi

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

llvm/include/llvm/ProfileData/SampleProf.h
llvm/lib/ProfileData/ProfileSummaryBuilder.cpp
llvm/lib/ProfileData/SampleProf.cpp
llvm/test/Transforms/SampleProfile/Inputs/pseudo-probe-inline.prof
llvm/test/tools/llvm-profgen/fname-canonicalization.test
llvm/test/tools/llvm-profgen/inline-cs-dangling-pseudoprobe.test
llvm/test/tools/llvm-profgen/inline-cs-pseudoprobe.test
llvm/test/tools/llvm-profgen/merge-cold-profile.test
llvm/test/tools/llvm-profgen/noinline-cs-pseudoprobe.test
llvm/test/tools/llvm-profgen/truncated-pseudoprobe.test
llvm/tools/llvm-profgen/ProfileGenerator.cpp

index 4861cdb..2f71bbc 100644 (file)
@@ -598,21 +598,9 @@ public:
   ErrorOr<uint64_t> findSamplesAt(uint32_t LineOffset,
                                   uint32_t Discriminator) const {
     const auto &ret = BodySamples.find(LineLocation(LineOffset, Discriminator));
-    if (ret == BodySamples.end()) {
-      // For CSSPGO, in order to conserve profile size, we no longer write out
-      // locations profile for those not hit during training, so we need to
-      // treat them as zero instead of error here.
-      if (FunctionSamples::ProfileIsCS || FunctionSamples::ProfileIsProbeBased)
-        return 0;
+    if (ret == BodySamples.end())
       return std::error_code();
-    } else {
-      // Return error for an invalid sample count which is usually assigned to
-      // dangling probe.
-      if (FunctionSamples::ProfileIsProbeBased &&
-          ret->second.getSamples() == FunctionSamples::InvalidProbeCount)
-        return std::error_code();
-      return ret->second.getSamples();
-    }
+    return ret->second.getSamples();
   }
 
   /// Returns the call target map collected at a given location.
@@ -890,10 +878,6 @@ public:
       const DILocation *DIL,
       SampleProfileReaderItaniumRemapper *Remapper = nullptr) const;
 
-  // The invalid sample count is used to represent samples collected for a
-  // dangling probe.
-  static constexpr uint64_t InvalidProbeCount = UINT64_MAX;
-
   static bool ProfileIsProbeBased;
 
   static bool ProfileIsCS;
index 6def44e..2ab0f0c 100644 (file)
@@ -113,8 +113,6 @@ void SampleProfileSummaryBuilder::addRecord(
   }
   for (const auto &I : FS.getBodySamples()) {
     uint64_t Count = I.second.getSamples();
-    if (!sampleprof::FunctionSamples::ProfileIsProbeBased ||
-        (Count != sampleprof::FunctionSamples::InvalidProbeCount))
       addCount(Count);
   }
   for (const auto &I : FS.getCallsiteSamples())
index b6abf6e..b2b786a 100644 (file)
@@ -119,16 +119,7 @@ raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS,
 sampleprof_error SampleRecord::merge(const SampleRecord &Other,
                                      uint64_t Weight) {
   sampleprof_error Result;
-  // With pseudo probes, merge a dangling sample with a non-dangling sample
-  // should result in a dangling sample.
-  if (FunctionSamples::ProfileIsProbeBased &&
-      (getSamples() == FunctionSamples::InvalidProbeCount ||
-       Other.getSamples() == FunctionSamples::InvalidProbeCount)) {
-    NumSamples = FunctionSamples::InvalidProbeCount;
-    Result = sampleprof_error::success;
-  } else {
-    Result = addSamples(Other.getSamples(), Weight);
-  }
+  Result = addSamples(Other.getSamples(), Weight);
   for (const auto &I : Other.getCallTargets()) {
     MergeResult(Result, addCalledTarget(I.first(), I.second, Weight));
   }
index fd3ff77..78ac810 100644 (file)
@@ -6,6 +6,9 @@
  1: 23
  2: 382920
  3: 382915
+ 4: 0
+ 5: 0
+ 6: 0
  !CFGChecksum: 138828622701
 [bar]:23:23
  1: 23
@@ -15,4 +18,7 @@
  1: 23
  2: 382920
  3: 382915
+ 4: 0
+ 5: 0
+ 6: 0
  !CFGChecksum: 138828622701
\ No newline at end of file
index 9fbf41a..5e7ec51 100644 (file)
@@ -22,8 +22,6 @@
 ; CHECK-PROBE-FNAME: !CFGChecksum: 563088904013236
 ; CHECK-PROBE-FNAME:[main:2 @ foo:8 @ _ZL3barii.__uniq.26267048767521081047744692097241227776]:30:15
 ; CHECK-PROBE-FNAME: 1: 15
-; CHECK-PROBE-FNAME: 2: 18446744073709551615
-; CHECK-PROBE-FNAME: 3: 18446744073709551615
 ; CHECK-PROBE-FNAME: 4: 15
 ; CHECK-PROBE-FNAME: !CFGChecksum: 72617220756
 
index 0ac51c1..801de3b 100644 (file)
@@ -2,14 +2,17 @@
 ; RUN: FileCheck %s --input-file %t
 
 ; CHECK:     [main:2 @ foo]:58:0
+; CHECK-NEXT: 1: 0
 ; CHECK-NEXT: 2: 15
 ; CHECK-NEXT: 3: 14
+; CHECK-NEXT: 4: 0
 ; CHECK-NEXT: 5: 14
 ; CHECK-NEXT: 6: 15
+; CHECK-NEXT: 7: 0
+; CHECK-NEXT: 9: 0
 ; CHECK-NEXT: !CFGChecksum: 138950591924
 ; CHECK:[main:2 @ foo:8 @ bar]:1:0
-; CHECK-NEXT: 2: 18446744073709551615
-; CHECK-NEXT: 3: 18446744073709551615
+; CHECK-NEXT: 1: 0
 ; CHECK-NEXT: 4: 1
 ; CHECK-NEXT: !CFGChecksum: 72617220756
 
index d1d6a9c..2d23ff4 100644 (file)
@@ -2,17 +2,18 @@
 ; RUN: FileCheck %s --input-file %t
 
 ; CHECK:     [main:2 @ foo]:74:0
+; CHECK-NEXT: 1: 0
 ; CHECK-NEXT: 2: 15
 ; CHECK-NEXT: 3: 15
 ; CHECK-NEXT: 4: 14
 ; CHECK-NEXT: 5: 1
 ; CHECK-NEXT: 6: 15
+; CHECK-NEXT: 7: 0
 ; CHECK-NEXT: 8: 14 bar:14
+; CHECK-NEXT: 9: 0
 ; CHECK-NEXT: !CFGChecksum: 138950591924
 ; CHECK:[main:2 @ foo:8 @ bar]:28:14
 ; CHECK-NEXT: 1: 14
-; CHECK-NEXT: 2: 18446744073709551615
-; CHECK-NEXT: 3: 18446744073709551615
 ; CHECK-NEXT: 4: 14
 ; CHECK-NEXT: !CFGChecksum: 72617220756
 
index 3d749db..7e975a9 100644 (file)
 
 ; CHECK:     [fa]:14:4
 ; CHECK-NEXT: 1: 4
-; CHECK-NEXT: 2: 18446744073709551615
 ; CHECK-NEXT: 3: 4
 ; CHECK-NEXT: 4: 2
 ; CHECK-NEXT: 5: 1
+; CHECK-NEXT: 6: 0
 ; CHECK-NEXT: 7: 2 fb:2
 ; CHECK-NEXT: 8: 1 fa:1
 ; CHECK-NEXT: !CFGChecksum: 120515930909
@@ -28,6 +28,7 @@
 ; CHECK-NEXT: 1: 4
 ; CHECK-NEXT: 2: 3
 ; CHECK-NEXT: 3: 1
+; CHECK-NEXT: 4: 0
 ; CHECK-NEXT: 5: 4 fb:4
 ; CHECK-NEXT: 6: 1 fa:1
 ; CHECK-NEXT: !CFGChecksum: 72617220756
 ; CHECK-KEEP-COLD-NEXT: 1: 6
 ; CHECK-KEEP-COLD-NEXT: 2: 3
 ; CHECK-KEEP-COLD-NEXT: 3: 3
+; CHECK-KEEP-COLD-NEXT: 4: 0
 ; CHECK-KEEP-COLD-NEXT: 5: 4 fb:4
 ; CHECK-KEEP-COLD-NEXT: 6: 3 fa:3
 ; CHECK-KEEP-COLD-NEXT: !CFGChecksum: 72617220756
 ; CHECK-KEEP-COLD-NEXT: !Attributes: 0
 ; CHECK-KEEP-COLD-NEXT:[fa]:14:4
 ; CHECK-KEEP-COLD-NEXT: 1: 4
-; CHECK-KEEP-COLD-NEXT: 2: 18446744073709551615
 ; CHECK-KEEP-COLD-NEXT: 3: 4
 ; CHECK-KEEP-COLD-NEXT: 4: 2
 ; CHECK-KEEP-COLD-NEXT: 5: 1
+; CHECK-KEEP-COLD-NEXT: 6: 0
 ; CHECK-KEEP-COLD-NEXT: 7: 2 fb:2
 ; CHECK-KEEP-COLD-NEXT: 8: 1 fa:1
 ; CHECK-KEEP-COLD-NEXT: !CFGChecksum: 120515930909
@@ -54,6 +56,7 @@
 ; CHECK-UNMERGED-NEXT: 1: 4
 ; CHECK-UNMERGED-NEXT: 2: 3
 ; CHECK-UNMERGED-NEXT: 3: 1
+; CHECK-UNMERGED-NEXT: 4: 0
 ; CHECK-UNMERGED-NEXT: 5: 4 fb:4
 ; CHECK-UNMERGED-NEXT: 6: 1 fa:1
 ; CHECK-UNMERGED-NEXT: !CFGChecksum: 72617220756
 ; CHECK-COLD-CONTEXT-LENGTH-NEXT: 1: 4
 ; CHECK-COLD-CONTEXT-LENGTH-NEXT: 2: 3
 ; CHECK-COLD-CONTEXT-LENGTH-NEXT: 3: 1
+; CHECK-COLD-CONTEXT-LENGTH-NEXT: 4: 0
 ; CHECK-COLD-CONTEXT-LENGTH-NEXT: 5: 4 fb:4
 ; CHECK-COLD-CONTEXT-LENGTH-NEXT: 6: 1 fa:1
 ; CHECK-COLD-CONTEXT-LENGTH-NEXT: !CFGChecksum: 72617220756
 ; CHECK-COLD-CONTEXT-LENGTH-NEXT: !Attributes: 0
 ; CHECK-COLD-CONTEXT-LENGTH-NEXT:[fb:6 @ fa]:10:3
 ; CHECK-COLD-CONTEXT-LENGTH-NEXT: 1: 3
-; CHECK-COLD-CONTEXT-LENGTH-NEXT: 2: 18446744073709551615
 ; CHECK-COLD-CONTEXT-LENGTH-NEXT: 3: 3
 ; CHECK-COLD-CONTEXT-LENGTH-NEXT: 4: 1
 ; CHECK-COLD-CONTEXT-LENGTH-NEXT: 5: 1
+; CHECK-COLD-CONTEXT-LENGTH-NEXT: 6: 0
 ; CHECK-COLD-CONTEXT-LENGTH-NEXT: 7: 1 fb:1
 ; CHECK-COLD-CONTEXT-LENGTH-NEXT: 8: 1 fa:1
 ; CHECK-COLD-CONTEXT-LENGTH-NEXT: !CFGChecksum: 120515930909
 ; CHECK-COLD-CONTEXT-LENGTH-NEXT: !Attributes: 0
 ; CHECK-COLD-CONTEXT-LENGTH-NEXT:[fa:7 @ fb]:6:2
 ; CHECK-COLD-CONTEXT-LENGTH-NEXT: 1: 2
+; CHECK-COLD-CONTEXT-LENGTH-NEXT: 2: 0
 ; CHECK-COLD-CONTEXT-LENGTH-NEXT: 3: 2
+; CHECK-COLD-CONTEXT-LENGTH-NEXT: 4: 0
+; CHECK-COLD-CONTEXT-LENGTH-NEXT: 5: 0
 ; CHECK-COLD-CONTEXT-LENGTH-NEXT: 6: 2 fa:2
 ; CHECK-COLD-CONTEXT-LENGTH-NEXT: !CFGChecksum: 72617220756
 ; CHECK-COLD-CONTEXT-LENGTH-NEXT: !Attributes: 0
 ; CHECK-COLD-CONTEXT-LENGTH-NEXT:[fa:8 @ fa]:4:1
 ; CHECK-COLD-CONTEXT-LENGTH-NEXT: 1: 1
-; CHECK-COLD-CONTEXT-LENGTH-NEXT: 2: 18446744073709551615
 ; CHECK-COLD-CONTEXT-LENGTH-NEXT: 3: 1
 ; CHECK-COLD-CONTEXT-LENGTH-NEXT: 4: 1
+; CHECK-COLD-CONTEXT-LENGTH-NEXT: 5: 0
+; CHECK-COLD-CONTEXT-LENGTH-NEXT: 6: 0
 ; CHECK-COLD-CONTEXT-LENGTH-NEXT: 7: 1 fb:1
+; CHECK-COLD-CONTEXT-LENGTH-NEXT: 8: 0
 ; CHECK-COLD-CONTEXT-LENGTH-NEXT: !CFGChecksum: 120515930909
 ; CHECK-COLD-CONTEXT-LENGTH-NEXT: !Attributes: 0
 
index 8eda5d5..7dc968e 100644 (file)
@@ -2,16 +2,18 @@
 ; RUN: FileCheck %s --input-file %t
 
 ; CHECK:     [main:2 @ foo]:75:0
+; CHECK-NEXT: 1: 0
 ; CHECK-NEXT: 2: 15
 ; CHECK-NEXT: 3: 15
 ; CHECK-NEXT: 4: 15
+; CHECK-NEXT: 5: 0
 ; CHECK-NEXT: 6: 15
+; CHECK-NEXT: 7: 0
 ; CHECK-NEXT: 8: 15 bar:15
+; CHECK-NEXT: 9: 0
 ; CHECK-NEXT: !CFGChecksum: 138950591924
 ; CHECK:[main:2 @ foo:8 @ bar]:30:15
 ; CHECK-NEXT: 1: 15
-; CHECK-NEXT: 2: 18446744073709551615
-; CHECK-NEXT: 3: 18446744073709551615
 ; CHECK-NEXT: 4: 15
 ; CHECK-NEXT: !CFGChecksum: 72617220756
 
index c29e2ad..b46479e 100644 (file)
@@ -2,17 +2,19 @@
 ; RUN: FileCheck %s --input-file %t
 
 ; CHECK:     [foo]:75:0
+; CHECK-NEXT:  1: 0
 ; CHECK-NEXT:  2: 15
 ; CHECK-NEXT:  3: 15
 ; CHECK-NEXT:  4: 15
+; CHECK-NEXT:  5: 0
 ; CHECK-NEXT:  6: 15
+; CHECK-NEXT:  7: 0
 ; CHECK-NEXT:  8: 15 bar:15
+; CHECK-NEXT:  9: 0
 ; CHECK-NEXT:  !CFGChecksum: 563088904013236
 ; CHECK-NEXT:  !Attributes: 0
 ; CHECK:     [foo:8 @ bar]:30:15
 ; CHECK-NEXT:  1: 15
-; CHECK-NEXT:  2: 18446744073709551615
-; CHECK-NEXT:  3: 18446744073709551615
 ; CHECK-NEXT:  4: 15
 ; CHECK-NEXT:  !CFGChecksum: 72617220756
 ; CHECK-NEXT:  !Attributes: 1
index c2d1753..0528a77 100644 (file)
@@ -555,19 +555,14 @@ void PseudoProbeCSProfileGenerator::populateBodySamplesWithProbes(
       }
     }
 
-    // Report dangling probes for frames that have real samples collected.
-    // Dangling probes are the probes associated to an empty block. With this
-    // place holder, sample count on a dangling probe will not be trusted by the
-    // compiler and we will rely on the counts inference algorithm to get the
-    // probe a reasonable count. Use InvalidProbeCount to mark sample count for
-    // a dangling probe.
+    // Assign zero count for remaining probes without sample hits to
+    // differentiate from probes optimized away, of which the counts are unknown
+    // and will be inferred by the compiler.
     for (auto &I : FrameSamples) {
       auto *FunctionProfile = I.second;
       for (auto *Probe : I.first->getProbes()) {
-        if (Probe->isDangling()) {
-          FunctionProfile->addBodySamplesForProbe(
-              Probe->Index, FunctionSamples::InvalidProbeCount);
-        }
+        if (!Probe->isDangling())
+          FunctionProfile->addBodySamplesForProbe(Probe->Index, 0);
       }
     }
   }