[llvm] [PGO] Fix incorrect count threshold calculation when 0% cutoff (PR #117359)

Ken Matsui via llvm-commits llvm-commits at lists.llvm.org
Fri Nov 22 10:33:25 PST 2024


https://github.com/ken-matsui created https://github.com/llvm/llvm-project/pull/117359

DefaultCutoffsData does not have an entry for the 0th percentile.  As a result, when the getEntryForPercentile method is called with a percentile argument of 0, it returns a ProfileSummaryEntry for the 1st percentile instead.  This behavior affects the threshold calculations, such as getHotCountThreshold, causing them to incorrectly identify some sample profile counts as hot when they should not be.

This patch addresses the issue by adding an entry for the 0th percentile to DetailedSummary.  This ensures that when the
-profile-summary-cutoff-hot (or -cold) option is set to 0, samples are not incorrectly recognized as hot or cold.

>From be1982496abfcf93024aa804c9af5b0058a55901 Mon Sep 17 00:00:00 2001
From: Ken Matsui <ken.matsui at mediatek.com>
Date: Fri, 22 Nov 2024 10:34:49 -0500
Subject: [PATCH] [PGO] Fix incorrect count threshold calculation when 0%
 cutoff

DefaultCutoffsData does not have an entry for the 0th percentile.  As a
result, when the getEntryForPercentile method is called with a
percentile argument of 0, it returns a ProfileSummaryEntry for the 1st
percentile instead.  This behavior affects the threshold calculations,
such as getHotCountThreshold, causing them to incorrectly identify some
sample profile counts as hot when they should not be.

This patch addresses the issue by adding an entry for the 0th percentile
to DetailedSummary.  This ensures that when the
-profile-summary-cutoff-hot (or -cold) option is set to 0, samples are
not incorrectly recognized as hot or cold.
---
 llvm/lib/ProfileData/ProfileSummaryBuilder.cpp | 12 +++++++++++-
 llvm/tools/llvm-profdata/llvm-profdata.cpp     |  4 ++--
 2 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/llvm/lib/ProfileData/ProfileSummaryBuilder.cpp b/llvm/lib/ProfileData/ProfileSummaryBuilder.cpp
index 3a45113b0a2eae..59a62867a211dd 100644
--- a/llvm/lib/ProfileData/ProfileSummaryBuilder.cpp
+++ b/llvm/lib/ProfileData/ProfileSummaryBuilder.cpp
@@ -70,6 +70,7 @@ cl::opt<uint64_t> ProfileSummaryColdCount(
 // A set of cutoff values. Each value, when divided by ProfileSummary::Scale
 // (which is 1000000) is a desired percentile of total counts.
 static const uint32_t DefaultCutoffsData[] = {
+    0,      /*  0% */
     10000,  /*  1% */
     100000, /* 10% */
     200000, 300000, 400000, 500000, 600000, 700000, 800000,
@@ -134,13 +135,22 @@ void ProfileSummaryBuilder::computeDetailedSummary() {
   if (DetailedSummaryCutoffs.empty())
     return;
   llvm::sort(DetailedSummaryCutoffs);
+
+  size_t StartIdx = 0;
+  if (DetailedSummaryCutoffs.front() == 0) {
+    // Put an entry for the 0th percentile.  Assume there is no UINT64_MAX
+    // sample count.
+    DetailedSummary.emplace_back(0, UINT64_MAX, 0);
+    StartIdx = 1;
+  }
+
   auto Iter = CountFrequencies.begin();
   const auto End = CountFrequencies.end();
 
   uint32_t CountsSeen = 0;
   uint64_t CurrSum = 0, Count = 0;
 
-  for (const uint32_t Cutoff : DetailedSummaryCutoffs) {
+  for (const uint32_t Cutoff : drop_begin(DetailedSummaryCutoffs, StartIdx)) {
     assert(Cutoff <= 999999);
     APInt Temp(128, TotalCount);
     APInt N(128, Cutoff);
diff --git a/llvm/tools/llvm-profdata/llvm-profdata.cpp b/llvm/tools/llvm-profdata/llvm-profdata.cpp
index 7641a80129de35..7a5fb436c73bd7 100644
--- a/llvm/tools/llvm-profdata/llvm-profdata.cpp
+++ b/llvm/tools/llvm-profdata/llvm-profdata.cpp
@@ -1111,8 +1111,8 @@ static void updateInstrProfileEntry(InstrProfileEntry &IFE, bool SetToHot,
   });
 }
 
-const uint64_t ColdPercentileIdx = 15;
-const uint64_t HotPercentileIdx = 11;
+const uint64_t ColdPercentileIdx = 16;
+const uint64_t HotPercentileIdx = 12;
 
 using sampleprof::FSDiscriminatorPass;
 



More information about the llvm-commits mailing list