Skip to content

Fix rounding error in getLikelyClassesOrMethods #86965

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 1, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/coreclr/jit/likelyclass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ static unsigned getLikelyClassesOrMethods(LikelyClassMethodRecord*
int32_t ilOffset,
bool types)
{
if (maxLikelyClasses == 0)
{
return 0;
}

ICorJitInfo::PgoInstrumentationKind histogramKind =
types ? ICorJitInfo::PgoInstrumentationKind::HandleHistogramTypes
: ICorJitInfo::PgoInstrumentationKind::HandleHistogramMethods;
Expand Down Expand Up @@ -235,6 +240,12 @@ static unsigned getLikelyClassesOrMethods(LikelyClassMethodRecord*
}
}

if (knownHandles == 0)
{
// We don't have known handles
return 0;
}

// sort by m_count (descending)
jitstd::sort(sortedEntries, sortedEntries + knownHandles,
[](const LikelyClassMethodHistogramEntry& h1,
Expand All @@ -244,12 +255,27 @@ static unsigned getLikelyClassesOrMethods(LikelyClassMethodRecord*

const UINT32 numberOfClasses = min(knownHandles, maxLikelyClasses);

UINT32 totalLikelihood = 0;
for (size_t hIdx = 0; hIdx < numberOfClasses; hIdx++)
{
LikelyClassMethodHistogramEntry const hc = sortedEntries[hIdx];
pLikelyEntries[hIdx].handle = hc.m_handle;
pLikelyEntries[hIdx].likelihood = hc.m_count * 100 / h.m_totalCount;
totalLikelihood += pLikelyEntries[hIdx].likelihood;
}

assert(totalLikelihood <= 100);

// Distribute the rounding error and just apply it to the first entry.
// Assume that there is no error If we have unknown handles.
if (numberOfClasses == h.m_totalCount)
{
assert(numberOfClasses > 0);
assert(totalLikelihood > 0);
pLikelyEntries[0].likelihood += 100 - totalLikelihood;
assert(pLikelyEntries[0].likelihood <= 100);
}

return numberOfClasses;
}
}
Expand Down