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
33 changes: 33 additions & 0 deletions src/coreclr/vm/jitinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10614,6 +10614,39 @@ void* CEEJitInfo::getHelperFtn(CorInfoHelpFunc ftnNum, /* IN */
}
#endif

// Array element accessors are more perf sensitive than other managed helpers and indirection
EgorBo marked this conversation as resolved.
Show resolved Hide resolved
// 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 if these helpers already reached Tier1 somewhere, we can safely skip the PreStub indirection.
if (dynamicFtnNum == DYNAMIC_CORINFO_HELP_ARRADDR_ST || dynamicFtnNum == DYNAMIC_CORINFO_HELP_LDELEMA_REF)
{
Precode* pPrecode = Precode::GetPrecodeFromEntryPoint((PCODE)hlpDynamicFuncTable[dynamicFtnNum].pfnHelper);
_ASSERTE(pPrecode->GetType() == PRECODE_FIXUP);

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

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)
{
return (LPVOID)GetEEFuncEntryPoint(hlpDynamicFuncTable[dynamicFtnNum].pfnHelper);
}

// Not in the final tier yet, fallback to the indirection.
*ppIndirection = ((FixupPrecode*)pPrecode)->GetTargetSlot();
return NULL;
}

if (dynamicFtnNum == DYNAMIC_CORINFO_HELP_ISINSTANCEOFINTERFACE ||
dynamicFtnNum == DYNAMIC_CORINFO_HELP_ISINSTANCEOFANY ||
dynamicFtnNum == DYNAMIC_CORINFO_HELP_ISINSTANCEOFARRAY ||
Expand Down