[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 14:22:32 PST 2024


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

>From 6b38657879355c495faf77dcf0d452ac98d7b3fc 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 handling the 0th percentile case in
the getEntryForPercentile method.  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 | 9 +++++++--
 llvm/test/Analysis/ProfileSummary/basic.ll     | 8 ++++++++
 2 files changed, 15 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/ProfileData/ProfileSummaryBuilder.cpp b/llvm/lib/ProfileData/ProfileSummaryBuilder.cpp
index 3a45113b0a2eae..99de89c8902362 100644
--- a/llvm/lib/ProfileData/ProfileSummaryBuilder.cpp
+++ b/llvm/lib/ProfileData/ProfileSummaryBuilder.cpp
@@ -58,8 +58,7 @@ cl::opt<unsigned> ProfileSummaryLargeWorkingSetSizeThreshold(
 // are useful for debugging purposes.
 cl::opt<uint64_t> ProfileSummaryHotCount(
     "profile-summary-hot-count", cl::ReallyHidden,
-    cl::desc("A fixed hot count that overrides the count derived from"
-             " profile-summary-cutoff-hot"));
+    cl::desc("A fixed hot count that overrides the count derived from" " profile-summary-cutoff-hot"));
 
 cl::opt<uint64_t> ProfileSummaryColdCount(
     "profile-summary-cold-count", cl::ReallyHidden,
@@ -77,9 +76,15 @@ static const uint32_t DefaultCutoffsData[] = {
 const ArrayRef<uint32_t> ProfileSummaryBuilder::DefaultCutoffs =
     DefaultCutoffsData;
 
+static const ProfileSummaryEntry ZeroCutoffEntry = {0, UINT64_MAX, 0};
+
 const ProfileSummaryEntry &
 ProfileSummaryBuilder::getEntryForPercentile(const SummaryEntryVector &DS,
                                              uint64_t Percentile) {
+  if (Percentile == 0) {
+    return ZeroCutoffEntry;
+  }
+
   auto It = partition_point(DS, [=](const ProfileSummaryEntry &Entry) {
     return Entry.Cutoff < Percentile;
   });
diff --git a/llvm/test/Analysis/ProfileSummary/basic.ll b/llvm/test/Analysis/ProfileSummary/basic.ll
index 966a1117c47d14..c4f48ccafde86d 100644
--- a/llvm/test/Analysis/ProfileSummary/basic.ll
+++ b/llvm/test/Analysis/ProfileSummary/basic.ll
@@ -2,12 +2,16 @@
 ; RUN: opt < %s -disable-output -profile-summary-hot-count=500 -passes=print-profile-summary -S 2>&1 | FileCheck %s -check-prefixes=OVERRIDE-HOT
 ; RUN: opt < %s -disable-output -profile-summary-cold-count=0 -passes=print-profile-summary -S 2>&1 | FileCheck %s -check-prefixes=OVERRIDE-COLD
 ; RUN: opt < %s -disable-output -profile-summary-cold-count=200 -profile-summary-hot-count=1000 -passes=print-profile-summary -S 2>&1 | FileCheck %s -check-prefixes=OVERRIDE-BOTH
+; RUN: opt < %s -disable-output -profile-summary-cutoff-hot=0 -passes=print-profile-summary -S 2>&1 | FileCheck %s -check-prefixes=HOT-CUTOFF-0
+; RUN: opt < %s -disable-output -profile-summary-cutoff-cold=0 -profile-summary-hot-count=18446744073709551615 -passes=print-profile-summary -S 2>&1 | FileCheck %s -check-prefixes=COLD-CUTOFF-0
 
 define void @f1() !prof !20 {
 ; CHECK-LABEL: f1 :hot
 ; OVERRIDE-HOT-LABEL: f1
 ; OVERRIDE-COLD-LABEL: f1 :hot
 ; OVERRIDE-BOTH-LABEL: f1
+; HOT-CUTOFF-0-LABEL: f1{{$}}
+; COLD-CUTOFF-0-LABEL: f1 :cold
 
   ret void
 }
@@ -17,6 +21,8 @@ define void @f2() !prof !21 {
 ; OVERRIDE-HOT-LABEL: f2 :cold
 ; OVERRIDE-COLD-LABEL: f2
 ; OVERRIDE-BOTH-LABEL: f2
+; HOT-CUTOFF-0-LABEL: f2 :cold
+; COLD-CUTOFF-0-LABEL: f2 :cold
 
   ret void
 }
@@ -26,6 +32,8 @@ define void @f3() !prof !22 {
 ; OVERRIDE-HOT-LABEL: f3
 ; OVERRIDE-COLD-LABEL: f3
 ; OVERRIDE-BOTH-LABEL: f3
+; HOT-CUTOFF-0-LABEL: f3{{$}}
+; COLD-CUTOFF-0-LABEL: f3 :cold
 
   ret void
 }



More information about the llvm-commits mailing list