Skip to content

Commit

Permalink
A refactoring suggestion for Kount's AggresiveOptimization change
Browse files Browse the repository at this point in the history
  • Loading branch information
noahfalk committed Sep 19, 2018
1 parent 6517a35 commit 697bc9e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
12 changes: 8 additions & 4 deletions src/vm/codeversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ bool NativeCodeVersion::operator!=(const NativeCodeVersion & rhs) const { return
#define CORPROF_E_RUNTIME_SUSPEND_REQUIRED 0x80131381

#ifndef DACCESS_COMPILE
NativeCodeVersionNode::NativeCodeVersionNode(NativeCodeVersionId id, MethodDesc* pMethodDesc, ReJITID parentId) :
NativeCodeVersionNode::NativeCodeVersionNode(NativeCodeVersionId id, MethodDesc* pMethodDesc, ReJITID parentId, OptimizationTier tier) :
m_pNativeCode(NULL),
m_pMethodDesc(pMethodDesc),
m_parentId(parentId),
m_pNextMethodDescSibling(NULL),
m_id(id),
m_optTier(NativeCodeVersion::OptimizationTier0),
m_optTier(tier),
m_flags(0)
{}
#endif
Expand Down Expand Up @@ -336,7 +336,7 @@ NativeCodeVersion::OptimizationTier NativeCodeVersion::GetOptimizationTier() con
}
else
{
return NativeCodeVersion::OptimizationTier0;
return TieredCompilationManager::GetDefaultOptimizationTier(GetMethodDesc());
}
}

Expand Down Expand Up @@ -2112,7 +2112,11 @@ HRESULT CodeVersionManager::AddNativeCodeVersion(ILCodeVersion ilCodeVersion, Me
}

NativeCodeVersionId newId = pMethodVersioningState->AllocateVersionId();
NativeCodeVersionNode* pNativeCodeVersionNode = new (nothrow) NativeCodeVersionNode(newId, pClosedMethodDesc, ilCodeVersion.GetVersionId());

//PERF: this call could be hoisted higher so that the case which allocates new non-default tier code versions (TieredCompilationManager::AsyncPromoteMethodToTier1) can pass the tier as an input argument rather
//than overwriting the default tier after paying the perf cost to compute it. However I suspect the difference would be neglible and not worth the code complexity.
NativeCodeVersion::OptimizationTier optTier = TieredCompilationManager::GetDefaultOptimizationTier(pClosedMethodDesc);
NativeCodeVersionNode* pNativeCodeVersionNode = new (nothrow) NativeCodeVersionNode(newId, pClosedMethodDesc, ilCodeVersion.GetVersionId(), optTier);
if (pNativeCodeVersionNode == NULL)
{
return E_OUTOFMEMORY;
Expand Down
11 changes: 6 additions & 5 deletions src/vm/prestub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1745,19 +1745,20 @@ PCODE MethodDesc::DoPrestub(MethodTable *pDispatchingMT)
// When the TieredCompilationManager has received enough call notifications
// for this method only then do we back-patch it.
BOOL fCanBackpatchPrestub = TRUE;
BOOL fEligibleForCallCounting = FALSE;
BOOL fNeedsCallCounting = FALSE;
#ifdef FEATURE_TIERED_COMPILATION
TieredCompilationManager* pTieredCompilationManager = nullptr;
BOOL fEligibleForTieredCompilation = IsEligibleForTieredCompilation();
BOOL fWasPromotedToTier1 = FALSE;
if (fEligibleForTieredCompilation)
{
fEligibleForCallCounting = g_pConfig->TieredCompilation_CallCounting();
if (fEligibleForCallCounting)
fNeedsCallCounting = TieredCompilationManager::MethodRequiresCallCounting(this);
if (fNeedsCallCounting)
{
pTieredCompilationManager = GetAppDomain()->GetTieredCompilationManager();
CallCounter * pCallCounter = GetCallCounter();
BOOL fWasPromotedToTier1 = FALSE;
pCallCounter->OnMethodCalled(this, pTieredCompilationManager, &fCanBackpatchPrestub, &fWasPromotedToTier1);
fNeedsCallCounting = !fWasPromotedToTier1;
}
}
#endif
Expand All @@ -1771,7 +1772,7 @@ PCODE MethodDesc::DoPrestub(MethodTable *pDispatchingMT)
{
pCode = GetCodeVersionManager()->PublishVersionableCodeIfNecessary(this, fCanBackpatchPrestub);

if (pTieredCompilationManager != nullptr && fEligibleForCallCounting && fCanBackpatchPrestub && pCode != NULL && !fWasPromotedToTier1)
if (pTieredCompilationManager != nullptr && fNeedsCallCounting && fCanBackpatchPrestub && pCode != NULL)
{
pTieredCompilationManager->OnMethodCallCountingStoppedWithoutTier1Promotion(this);
}
Expand Down
24 changes: 24 additions & 0 deletions src/vm/tieredcompilation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -791,4 +791,28 @@ CORJIT_FLAGS TieredCompilationManager::GetJitFlags(NativeCodeVersion nativeCodeV
return flags;
}

// static
// Given a method that is eligible for tiered compilation, which Tier should it start at by default?
NativeCodeVersion::OptimizationTier TieredCompilationManager::GetDefaultOptimizationTier(MethodDesc* pMethod)
{
_ASSERTE(pMethod->IsEligibleForTieredCompilation());
if (pMethod->RequestedAggresiveOptimization())
{
return NativeCodeVersion::OptimizationTier::Tier1;
}
else
{
return NativeCodeVersion::OptimizationTier::Tier0;
}
}

// static
// Given a method that is eligible for tiered compilation, do we need to do call counting in the prestub?
// Note: This is invariant for the lifetime of a method, and will continue to return true regardless of call count
BOOL TieredCompilationManager::MethodRequiresCallCounting(MethodDesc* pMethod)
{
_ASSERTE(pMethod->IsEligibleForTieredCompilation());
return g_pConfig->TieredCompilation_CallCounting() && GetDefaultOptimizationTier(pMethod) == NativeCodeVersion::OptimizationTier::Tier0;
}

#endif // FEATURE_TIERED_COMPILATION

0 comments on commit 697bc9e

Please sign in to comment.