Skip to content

Commit 6bf7c02

Browse files
authored
Enable GDV with multiple guesses for NativeAOT (#87380)
1 parent 188b25e commit 6bf7c02

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

src/coreclr/jit/compiler.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7150,6 +7150,28 @@ class Compiler
71507150
unsigned classAttr,
71517151
unsigned likelihood);
71527152

7153+
int getGDVMaxTypeChecks()
7154+
{
7155+
int typeChecks = JitConfig.JitGuardedDevirtualizationMaxTypeChecks();
7156+
if (typeChecks < 0)
7157+
{
7158+
// Negative value means "it's up to JIT to decide"
7159+
if (IsTargetAbi(CORINFO_NATIVEAOT_ABI) && !opts.jitFlags->IsSet(JitFlags::JIT_FLAG_SIZE_OPT))
7160+
{
7161+
return 3;
7162+
}
7163+
7164+
// We plan to use 3 for CoreCLR too, but we need to make sure it doesn't regress performance
7165+
// as CoreCLR heavily relies on Dynamic PGO while for NativeAOT we *usually* don't have it and
7166+
// can only perform the "exact" devirtualization.
7167+
return 1;
7168+
}
7169+
7170+
// MAX_GDV_TYPE_CHECKS is the upper limit. The constant can be changed, we just suspect that even
7171+
// 4 type checks is already too much.
7172+
return min(MAX_GDV_TYPE_CHECKS, typeChecks);
7173+
}
7174+
71537175
bool doesMethodHaveExpRuntimeLookup()
71547176
{
71557177
return (optMethodFlags & OMF_HAS_EXPRUNTIMELOOKUP) != 0;

src/coreclr/jit/importercalls.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5728,7 +5728,7 @@ void Compiler::pickGDV(GenTreeCall* call,
57285728
// Prefer class guess as it is cheaper
57295729
if (numberOfClasses > 0)
57305730
{
5731-
const int maxNumberOfGuesses = min(MAX_GDV_TYPE_CHECKS, JitConfig.JitGuardedDevirtualizationMaxTypeChecks());
5731+
const int maxNumberOfGuesses = getGDVMaxTypeChecks();
57325732
if (maxNumberOfGuesses == 0)
57335733
{
57345734
// DOTNET_JitGuardedDevirtualizationMaxTypeChecks=0 means we don't want to do any guarded devirtualization
@@ -5929,7 +5929,7 @@ void Compiler::considerGuardedDevirtualization(GenTreeCall* call,
59295929
{
59305930
pickGDV(call, ilOffset, isInterface, likelyClasses, likelyMethodes, &candidatesCount, likelihoods);
59315931
assert((unsigned)candidatesCount <= MAX_GDV_TYPE_CHECKS);
5932-
assert((unsigned)candidatesCount <= (unsigned)JitConfig.JitGuardedDevirtualizationMaxTypeChecks());
5932+
assert((unsigned)candidatesCount <= (unsigned)getGDVMaxTypeChecks());
59335933
if (candidatesCount == 0)
59345934
{
59355935
hasPgoData = false;
@@ -5942,7 +5942,7 @@ void Compiler::considerGuardedDevirtualization(GenTreeCall* call,
59425942
// from both.
59435943
if (!hasPgoData && (baseClass != NO_CLASS_HANDLE) && JitConfig.JitEnableExactDevirtualization())
59445944
{
5945-
int maxTypeChecks = min(JitConfig.JitGuardedDevirtualizationMaxTypeChecks(), MAX_GDV_TYPE_CHECKS);
5945+
int maxTypeChecks = min(getGDVMaxTypeChecks(), MAX_GDV_TYPE_CHECKS);
59465946

59475947
CORINFO_CLASS_HANDLE exactClasses[MAX_GDV_TYPE_CHECKS];
59485948
int numExactClasses = info.compCompHnd->getExactClasses(baseClass, MAX_GDV_TYPE_CHECKS, exactClasses);

src/coreclr/jit/jitconfigvalues.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -528,8 +528,8 @@ CONFIG_INTEGER(JitEnableGuardedDevirtualization, W("JitEnableGuardedDevirtualiza
528528

529529
#define MAX_GDV_TYPE_CHECKS 5
530530
// Number of types to probe for polymorphic virtual call-sites to devirtualize them,
531-
// Max number is MAX_GDV_TYPE_CHECKS defined above ^
532-
CONFIG_INTEGER(JitGuardedDevirtualizationMaxTypeChecks, W("JitGuardedDevirtualizationMaxTypeChecks"), 1)
531+
// Max number is MAX_GDV_TYPE_CHECKS defined above ^. -1 means it's up to JIT to decide
532+
CONFIG_INTEGER(JitGuardedDevirtualizationMaxTypeChecks, W("JitGuardedDevirtualizationMaxTypeChecks"), -1)
533533

534534
// Various policies for GuardedDevirtualization
535535
CONFIG_INTEGER(JitGuardedDevirtualizationChainLikelihood, W("JitGuardedDevirtualizationChainLikelihood"), 0x4B) // 75

0 commit comments

Comments
 (0)