Skip to content
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

Remove AggressiveOpt from CastHelpers.LdelemaRef/StelemRef #90412

Merged
merged 10 commits into from
Aug 16, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,6 @@ internal struct ArrayElement
[DebuggerHidden]
[StackTraceHidden]
[DebuggerStepThrough]
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
private static ref object? LdelemaRef(Array array, nint index, void* type)
{
// this will throw appropriate exceptions if array is null or access is out of range.
Expand All @@ -427,7 +426,6 @@ internal struct ArrayElement
[DebuggerHidden]
[StackTraceHidden]
[DebuggerStepThrough]
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
private static void StelemRef(Array array, nint index, object? obj)
{
// this will throw appropriate exceptions if array is null or access is out of range.
Expand Down
19 changes: 2 additions & 17 deletions src/coreclr/vm/ecall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,27 +137,12 @@ void ECall::PopulateManagedCastHelpers()
pDest = pMD->GetMultiCallableAddrOfCode();
SetJitHelperFunction(CORINFO_HELP_UNBOX, pDest);

// Array element accessors are more perf sensitive than other managed helpers and indirection
// costs introduced by PreStub could be noticeable (7% to 30% depending on platform).
// Other helpers are either more complex, less common, or have their trivial case inlined by the JIT,
// so indirection is not as big concern.
// We JIT-compile the following helpers eagerly here to avoid indirection costs.

//TODO: revise if this specialcasing is still needed when crossgen supports tailcall optimizations
// see: https://github.com/dotnet/runtime/issues/5857

pMD = CoreLibBinder::GetMethod((BinderMethodID)(METHOD__CASTHELPERS__STELEMREF));
pMD->DoPrestub(NULL);
// This helper is marked AggressiveOptimization and its native code is in its final form.
// Get the code directly to avoid PreStub indirection.
pDest = pMD->GetNativeCode();
pDest = pMD->GetMultiCallableAddrOfCode();
SetJitHelperFunction(CORINFO_HELP_ARRADDR_ST, pDest);

pMD = CoreLibBinder::GetMethod((BinderMethodID)(METHOD__CASTHELPERS__LDELEMAREF));
pMD->DoPrestub(NULL);
// This helper is marked AggressiveOptimization and its native code is in its final form.
// Get the code directly to avoid PreStub indirection.
pDest = pMD->GetNativeCode();
pDest = pMD->GetMultiCallableAddrOfCode();
SetJitHelperFunction(CORINFO_HELP_LDELEMA_REF, pDest);
}

Expand Down
37 changes: 36 additions & 1 deletion src/coreclr/vm/jitinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10614,6 +10614,14 @@ void* CEEJitInfo::getHelperFtn(CorInfoHelpFunc ftnNum, /* IN */
}
#endif

// Check if we already have a cached address of the final target
static LPVOID hlpFinalTierAddrTable[DYNAMIC_CORINFO_HELP_COUNT] = {};
LPVOID finalTierAddr = hlpFinalTierAddrTable[dynamicFtnNum];
if (finalTierAddr != NULL)
{
return finalTierAddr;
}

if (dynamicFtnNum == DYNAMIC_CORINFO_HELP_ISINSTANCEOFINTERFACE ||
dynamicFtnNum == DYNAMIC_CORINFO_HELP_ISINSTANCEOFANY ||
dynamicFtnNum == DYNAMIC_CORINFO_HELP_ISINSTANCEOFARRAY ||
Expand All @@ -10623,10 +10631,37 @@ void* CEEJitInfo::getHelperFtn(CorInfoHelpFunc ftnNum, /* IN */
dynamicFtnNum == DYNAMIC_CORINFO_HELP_CHKCASTINTERFACE ||
dynamicFtnNum == DYNAMIC_CORINFO_HELP_CHKCASTCLASS ||
dynamicFtnNum == DYNAMIC_CORINFO_HELP_CHKCASTCLASS_SPECIAL ||
dynamicFtnNum == DYNAMIC_CORINFO_HELP_UNBOX)
dynamicFtnNum == DYNAMIC_CORINFO_HELP_UNBOX ||
dynamicFtnNum == DYNAMIC_CORINFO_HELP_ARRADDR_ST ||
dynamicFtnNum == DYNAMIC_CORINFO_HELP_LDELEMA_REF)
{
Precode* pPrecode = Precode::GetPrecodeFromEntryPoint((PCODE)hlpDynamicFuncTable[dynamicFtnNum].pfnHelper);
kouvel marked this conversation as resolved.
Show resolved Hide resolved
_ASSERTE(pPrecode->GetType() == PRECODE_FIXUP);

MethodDesc* helperMD = pPrecode->GetMethodDesc();
_ASSERT(helperMD != nullptr);

// Check if the target MethodDesc is already jitted to its final Tier
// so we no longer need to use indirections and can emit a direct call instead.
NativeCodeVersion::OptimizationTier tier;

{
CodeVersionManager::LockHolder codeVersioningLockHolder;
EgorBo marked this conversation as resolved.
Show resolved Hide resolved
EgorBo marked this conversation as resolved.
Show resolved Hide resolved
NativeCodeVersion activeCodeVersion = helperMD->GetCodeVersionManager()->GetActiveILCodeVersion(helperMD)
.GetActiveNativeCodeVersion(helperMD);
tier = activeCodeVersion.GetOptimizationTier();
}

if (tier == NativeCodeVersion::OptimizationTier::OptimizationTier1 ||
tier == NativeCodeVersion::OptimizationTier::OptimizationTierOptimized)
{
finalTierAddr = (LPVOID)GetEEFuncEntryPoint(hlpDynamicFuncTable[dynamicFtnNum].pfnHelper);
_ASSERT(finalTierAddr != NULL);
// Cache it for future uses to avoid taking the lock again.
hlpFinalTierAddrTable[dynamicFtnNum] = finalTierAddr;
return finalTierAddr;
}

*ppIndirection = ((FixupPrecode*)pPrecode)->GetTargetSlot();
return NULL;
}
Expand Down