Skip to content

Commit

Permalink
JIT_CountProfile: Avoid BSR instruction for low counts (dotnet#110258)
Browse files Browse the repository at this point in the history
The JIT_CountProfile32 and JIT_CountProfile64 functions used BitScanReverse functions to compute the log2 of counters, and then compare that to a threshold. This changes it so that the counter is directly compared to `1<<threshold` instead.
  • Loading branch information
sfiruch authored and mikelle-rogers committed Dec 4, 2024
1 parent e0d39b0 commit 7ad2bce
Showing 1 changed file with 14 additions and 20 deletions.
34 changes: 14 additions & 20 deletions src/coreclr/vm/jithelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3681,20 +3681,17 @@ HCIMPL1(void, JIT_CountProfile32, volatile LONG* pCounter)
LONG delta = 1;
DWORD threshold = g_pConfig->TieredPGO_ScalableCountThreshold();

if (count > 0)
if (count >= (LONG)(1 << threshold))
{
DWORD logCount = 0;
DWORD logCount;
BitScanReverse(&logCount, count);

if (logCount >= threshold)
delta = 1 << (logCount - (threshold - 1));
const unsigned rand = HandleHistogramProfileRand();
const bool update = (rand & (delta - 1)) == 0;
if (!update)
{
delta = 1 << (logCount - (threshold - 1));
const unsigned rand = HandleHistogramProfileRand();
const bool update = (rand & (delta - 1)) == 0;
if (!update)
{
return;
}
return;
}
}

Expand All @@ -3711,20 +3708,17 @@ HCIMPL1(void, JIT_CountProfile64, volatile LONG64* pCounter)
LONG64 delta = 1;
DWORD threshold = g_pConfig->TieredPGO_ScalableCountThreshold();

if (count > 0)
if (count >= (LONG64)(1LL << threshold))
{
DWORD logCount = 0;
DWORD logCount;
BitScanReverse64(&logCount, count);

if (logCount >= threshold)
delta = 1LL << (logCount - (threshold - 1));
const unsigned rand = HandleHistogramProfileRand();
const bool update = (rand & (delta - 1)) == 0;
if (!update)
{
delta = 1LL << (logCount - (threshold - 1));
const unsigned rand = HandleHistogramProfileRand();
const bool update = (rand & (delta - 1)) == 0;
if (!update)
{
return;
}
return;
}
}

Expand Down

0 comments on commit 7ad2bce

Please sign in to comment.