Skip to content

Commit

Permalink
JIT: Support for devirtualizing array interface methods (#108153)
Browse files Browse the repository at this point in the history
Update JIT and runtime to devirtualize interface calls on arrays
over non-shared element types.

Shared types are not (yet) handled.

Add intrinsic and inlining attributes to key methods in the BCL.
This allows the JIT to devirtualize and inline enumerator creation
and devirtualize and inline all methods that access the enumerator.

And this in turn allows the enumerator to be stack allocated in some
simple cases.

However, the enumerator fields are not (yet) physically promoted,
because of an optimization in the BCL to return a static empty
array enumerator. So the object being accessed later is ambiguous.

Alse ensure that since GDV resolves the virtual call twice, and
expects to get similar results both times, things work for the array
case by keeping track of the initial devirtualization inputs.

Progress towards #62457.
  • Loading branch information
AndyAyersMS authored Oct 2, 2024
1 parent 8611665 commit 6906730
Show file tree
Hide file tree
Showing 32 changed files with 597 additions and 216 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,8 @@ private SZArrayHelper()
Debug.Fail("Hey! How'd I get here?");
}

[Intrinsic]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal IEnumerator<T> GetEnumerator<T>()
{
// ! Warning: "this" is an array, not an SZArrayHelper. See comments above
Expand Down
13 changes: 11 additions & 2 deletions src/coreclr/inc/corinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -1510,18 +1510,21 @@ struct CORINFO_DEVIRTUALIZATION_INFO
// [Out] results of resolveVirtualMethod.
// - devirtualizedMethod is set to MethodDesc of devirt'ed method iff we were able to devirtualize.
// invariant is `resolveVirtualMethod(...) == (devirtualizedMethod != nullptr)`.
// - requiresInstMethodTableArg is set to TRUE if the devirtualized method requires a type handle arg.
// - exactContext is set to wrapped CORINFO_CLASS_HANDLE of devirt'ed method table.
// - details on the computation done by the jit host
// - If pResolvedTokenDevirtualizedMethod is not set to NULL and targeting an R2R image
// use it as the parameter to getCallInfo
// - requiresInstMethodTableArg is set to TRUE if the devirtualized method requires a type handle arg.
// - wasArrayInterfaceDevirt is set TRUE for array interface method devirtualization
// (in which case the method handle and context will be a generic method)
//
CORINFO_METHOD_HANDLE devirtualizedMethod;
bool requiresInstMethodTableArg;
CORINFO_CONTEXT_HANDLE exactContext;
CORINFO_DEVIRTUALIZATION_DETAIL detail;
CORINFO_RESOLVED_TOKEN resolvedTokenDevirtualizedMethod;
CORINFO_RESOLVED_TOKEN resolvedTokenDevirtualizedUnboxedMethod;
bool requiresInstMethodTableArg;
bool wasArrayInterfaceDevirt;
};

//----------------------------------------------------------------------------
Expand Down Expand Up @@ -2123,6 +2126,12 @@ class ICorStaticInfo
CORINFO_CLASS_HANDLE elemType
) = 0;

// Given T, return the type of the SZArrayHelper enumerator
// Returns null if the type can't be determined exactly.
virtual CORINFO_CLASS_HANDLE getSZArrayHelperEnumeratorClass(
CORINFO_CLASS_HANDLE elemType
) = 0;

// Given resolved token that corresponds to an intrinsic classified to
// get a raw handle (NI_System_Activator_AllocatorOf etc.), fetch the
// handle associated with the token. If this is not possible at
Expand Down
3 changes: 3 additions & 0 deletions src/coreclr/inc/icorjitinfoimpl_generated.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ CORINFO_CLASS_HANDLE getDefaultComparerClass(
CORINFO_CLASS_HANDLE getDefaultEqualityComparerClass(
CORINFO_CLASS_HANDLE elemType) override;

CORINFO_CLASS_HANDLE getSZArrayHelperEnumeratorClass(
CORINFO_CLASS_HANDLE elemType) override;

void expandRawHandleIntrinsic(
CORINFO_RESOLVED_TOKEN* pResolvedToken,
CORINFO_METHOD_HANDLE callerHandle,
Expand Down
10 changes: 5 additions & 5 deletions src/coreclr/inc/jiteeversionguid.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ typedef const GUID *LPCGUID;
#define GUID_DEFINED
#endif // !GUID_DEFINED

constexpr GUID JITEEVersionIdentifier = { /* b75a5475-ff22-4078-9551-2024ce03d383 */
0xb75a5475,
0xff22,
0x4078,
{0x95, 0x51, 0x20, 0x24, 0xce, 0x03, 0xd3, 0x83}
constexpr GUID JITEEVersionIdentifier = { /* 9b8ef809-94d4-41b6-9d4c-dd61379abbe0 */
0x9b8ef809,
0x94d4,
0x41b6,
{0x9d, 0x4c, 0xdd, 0x61, 0x37, 0x9a, 0xbb, 0xe0}
};

//////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down
1 change: 1 addition & 0 deletions src/coreclr/jit/ICorJitInfo_names_generated.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ DEF_CLR_API(resolveVirtualMethod)
DEF_CLR_API(getUnboxedEntry)
DEF_CLR_API(getDefaultComparerClass)
DEF_CLR_API(getDefaultEqualityComparerClass)
DEF_CLR_API(getSZArrayHelperEnumeratorClass)
DEF_CLR_API(expandRawHandleIntrinsic)
DEF_CLR_API(isIntrinsicType)
DEF_CLR_API(getUnmanagedCallConv)
Expand Down
9 changes: 9 additions & 0 deletions src/coreclr/jit/ICorJitInfo_wrapper_generated.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,15 @@ CORINFO_CLASS_HANDLE WrapICorJitInfo::getDefaultEqualityComparerClass(
return temp;
}

CORINFO_CLASS_HANDLE WrapICorJitInfo::getSZArrayHelperEnumeratorClass(
CORINFO_CLASS_HANDLE elemType)
{
API_ENTER(getSZArrayHelperEnumeratorClass);
CORINFO_CLASS_HANDLE temp = wrapHnd->getSZArrayHelperEnumeratorClass(elemType);
API_LEAVE(getSZArrayHelperEnumeratorClass);
return temp;
}

void WrapICorJitInfo::expandRawHandleIntrinsic(
CORINFO_RESOLVED_TOKEN* pResolvedToken,
CORINFO_METHOD_HANDLE callerHandle,
Expand Down
4 changes: 3 additions & 1 deletion src/coreclr/jit/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -7573,7 +7573,9 @@ class Compiler
CORINFO_CONTEXT_HANDLE contextHandle,
unsigned methodAttr,
unsigned classAttr,
unsigned likelihood);
unsigned likelihood,
bool arrayInterface,
CORINFO_CONTEXT_HANDLE originalContextHandle);

int getGDVMaxTypeChecks()
{
Expand Down
8 changes: 4 additions & 4 deletions src/coreclr/jit/fginline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ bool Compiler::IsDisallowedRecursiveInline(InlineContext* ancestor, InlineInfo*
{
// We disallow inlining the exact same instantiation.
if ((ancestor->GetCallee() == inlineInfo->fncHandle) &&
(ancestor->GetRuntimeContext() == inlineInfo->inlineCandidateInfo->exactContextHnd))
(ancestor->GetRuntimeContext() == inlineInfo->inlineCandidateInfo->exactContextHandle))
{
JITDUMP("Call site is trivially recursive\n");
return true;
Expand All @@ -80,7 +80,7 @@ bool Compiler::IsDisallowedRecursiveInline(InlineContext* ancestor, InlineInfo*
// involved this can quickly consume a large amount of resources, so try to
// verify that we aren't inlining recursively with complex contexts.
if (info.compCompHnd->haveSameMethodDefinition(inlineInfo->fncHandle, ancestor->GetCallee()) &&
ContextComplexityExceeds(inlineInfo->inlineCandidateInfo->exactContextHnd, 64))
ContextComplexityExceeds(inlineInfo->inlineCandidateInfo->exactContextHandle, 64))
{
JITDUMP("Call site is recursive with a complex generic context\n");
return true;
Expand Down Expand Up @@ -1300,7 +1300,7 @@ void Compiler::fgInvokeInlineeCompiler(GenTreeCall* call, InlineResult* inlineRe
->NewContext(pParam->inlineInfo->inlineCandidateInfo->inlinersContext, pParam->inlineInfo->iciStmt,
pParam->inlineInfo->iciCall);
pParam->inlineInfo->argCnt = pParam->inlineCandidateInfo->methInfo.args.totalILArgs();
pParam->inlineInfo->tokenLookupContextHandle = pParam->inlineCandidateInfo->exactContextHnd;
pParam->inlineInfo->tokenLookupContextHandle = pParam->inlineCandidateInfo->exactContextHandle;

JITLOG_THIS(pParam->pThis,
(LL_INFO100000, "INLINER: inlineInfo.tokenLookupContextHandle for %s set to 0x%p:\n",
Expand Down Expand Up @@ -2042,7 +2042,7 @@ Statement* Compiler::fgInlinePrependStatements(InlineInfo* inlineInfo)

if (inlineInfo->inlineCandidateInfo->initClassResult & CORINFO_INITCLASS_USE_HELPER)
{
CORINFO_CLASS_HANDLE exactClass = eeGetClassFromContext(inlineInfo->inlineCandidateInfo->exactContextHnd);
CORINFO_CLASS_HANDLE exactClass = eeGetClassFromContext(inlineInfo->inlineCandidateInfo->exactContextHandle);

tree = fgGetSharedCCtor(exactClass);
newStmt = gtNewStmt(tree, callDI);
Expand Down
7 changes: 4 additions & 3 deletions src/coreclr/jit/gentree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12981,9 +12981,10 @@ void Compiler::gtDispTree(GenTree* tree,
{
inlineInfo = call->GetSingleInlineCandidateInfo();
}
if ((inlineInfo != nullptr) && (inlineInfo->exactContextHnd != nullptr))

if ((inlineInfo != nullptr) && (inlineInfo->exactContextHandle != nullptr))
{
printf(" (exactContextHnd=0x%p)", dspPtr(inlineInfo->exactContextHnd));
printf(" (exactContextHandle=0x%p)", dspPtr(inlineInfo->exactContextHandle));
}
}

Expand Down Expand Up @@ -19048,7 +19049,7 @@ CORINFO_CLASS_HANDLE Compiler::gtGetClassHandle(GenTree* tree, bool* pIsExact, b
// of the inlinee.
if (eeIsSharedInst(objClass))
{
CORINFO_CONTEXT_HANDLE context = inlInfo->exactContextHnd;
CORINFO_CONTEXT_HANDLE context = inlInfo->exactContextHandle;

if (context != nullptr)
{
Expand Down
Loading

0 comments on commit 6906730

Please sign in to comment.