[llvm] [PGO] Fix incorrect count threshold calculation when 0% cutoff (PR #117359)
via llvm-commits
llvm-commits at lists.llvm.org
Fri Nov 22 10:36:34 PST 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-pgo
Author: Ken Matsui (ken-matsui)
<details>
<summary>Changes</summary>
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.
---
Full diff: https://github.com/llvm/llvm-project/pull/117359.diff
2 Files Affected:
- (modified) llvm/lib/ProfileData/ProfileSummaryBuilder.cpp (+11-1)
- (modified) llvm/tools/llvm-profdata/llvm-profdata.cpp (+2-2)
``````````diff
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;
``````````
</details>
https://github.com/llvm/llvm-project/pull/117359
More information about the llvm-commits
mailing list