Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/coreclr/interpreter/compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5596,7 +5596,14 @@ void InterpCompiler::EmitCall(CORINFO_RESOLVED_TOKEN* pConstrainedToken, bool re
}
else
{
contextParamVar = EmitGenericHandle(&resolvedCallToken, GenericHandleEmbedOptions::VarOnly | GenericHandleEmbedOptions::EmbedParent).var;
if (pConstrainedToken != nullptr && pConstrainedToken->hClass == exactClassHandle)
{
contextParamVar = EmitGenericHandle(pConstrainedToken, GenericHandleEmbedOptions::VarOnly).var;
}
else
{
contextParamVar = EmitGenericHandle(&resolvedCallToken, GenericHandleEmbedOptions::VarOnly | GenericHandleEmbedOptions::EmbedParent).var;
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/coreclr/jit/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -5415,6 +5415,7 @@ class Compiler
IL_OFFSET rawILOffset);

GenTree* impGetInstParamArg(CORINFO_RESOLVED_TOKEN* pResolvedToken,
CORINFO_RESOLVED_TOKEN* pConstrainedResolvedToken,
CORINFO_CALL_INFO* callInfo,
CORINFO_CONTEXT_HANDLE exactContextHnd,
bool exactContextNeedsRuntimeLookup,
Expand Down
15 changes: 15 additions & 0 deletions src/coreclr/jit/eeinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,14 @@ void Compiler::eePrintField(StringPrinter* printer, CORINFO_FIELD_HANDLE fld, bo
const char* Compiler::eeGetMethodFullName(
CORINFO_METHOD_HANDLE hnd, bool includeReturnType, bool includeThisSpecifier, char* buffer, size_t bufferSize)
{
// A CT_USER_FUNC call can carry a null method handle, such as a direct call to the interface
// dispatch helper produced by LowerVirtualStubCall. Dump and disassembly paths must not pass
// it to getMethodClass, whose host can assert on a null handle.
if (hnd == nullptr)
{
return "<null method handle>";
}

CorInfoHelpFunc helper = eeGetHelperNum(hnd);
if (helper != CORINFO_HELP_UNDEF)
{
Expand Down Expand Up @@ -523,6 +531,13 @@ const char* Compiler::eeGetMethodFullName(
//
const char* Compiler::eeGetMethodName(CORINFO_METHOD_HANDLE methHnd, char* buffer, size_t bufferSize)
{
// See eeGetMethodFullName: a null method handle reaches here from dumping a CT_USER_FUNC call
// that carries no handle, such as the lowered interface dispatch helper call.
if (methHnd == nullptr)
{
return "<null method handle>";
}

StringPrinter p(getAllocator(CMK_DebugOnly), buffer, bufferSize);
bool success = eeRunFunctorWithSPMIErrorTrap([&]() {
eePrintMethod(&p, NO_CLASS_HANDLE, methHnd,
Expand Down
13 changes: 10 additions & 3 deletions src/coreclr/jit/gentree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15157,11 +15157,18 @@ void Compiler::gtDispTree(GenTree* tree,

if (call->gtCallType != CT_INDIRECT)
{
auto disp = [&]() {
if ((call->gtCallMethHnd == nullptr) && (call->gtDirectCallAddress != nullptr))
{
// LowerVirtualStubCall rewrites an interface dispatch into a direct call to the
// dispatch helper and clears the method handle. Describe it by its role and the
// helper target rather than the absent method.
printf(" [stub dispatch helper @ %p]", dspPtr(call->gtDirectCallAddress));
}
else
{
char buffer[256];
printf(" %s", eeGetMethodFullName(call->gtCallMethHnd, true, true, buffer, sizeof(buffer)));
};
disp();
}
}

if (call->IsAsync())
Expand Down
22 changes: 17 additions & 5 deletions src/coreclr/jit/importercalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
//
// Arguments:
// pResolvedToken - resolved token for the call target
// pConstrainedResolvedToken - resolved constrained type token, if present
// callInfo - EE supplied info for the call
// exactContextHnd - the exact (method or class) context for the call
// exactContextNeedsRuntimeLookup - whether the exact context needs a runtime lookup
Expand All @@ -29,6 +30,7 @@
// can carry a type arg but is otherwise outside the main hasTypeArg() handling.
//
GenTree* Compiler::impGetInstParamArg(CORINFO_RESOLVED_TOKEN* pResolvedToken,
CORINFO_RESOLVED_TOKEN* pConstrainedResolvedToken,
CORINFO_CALL_INFO* callInfo,
CORINFO_CONTEXT_HANDLE exactContextHnd,
bool exactContextNeedsRuntimeLookup,
Expand Down Expand Up @@ -114,7 +116,16 @@ GenTree* Compiler::impGetInstParamArg(CORINFO_RESOLVED_TOKEN* pResolvedToken,
}
else
{
instParam = impParentClassTokenToHandle(pResolvedToken, &runtimeLookup);
if ((pConstrainedResolvedToken != nullptr) && (pConstrainedResolvedToken->hClass == exactClassHandle))
{
// The original method token can name an interface, while the implementation
// needs the constrained value type's context.
instParam = impTokenToHandle(pConstrainedResolvedToken, &runtimeLookup);
}
else
{
instParam = impParentClassTokenToHandle(pResolvedToken, &runtimeLookup);
}
if (instParam == nullptr)
{
assert(compDonotInline());
Expand Down Expand Up @@ -717,8 +728,9 @@ var_types Compiler::impImportCall(OPCODE opcode,
}
else
{
wasmInstParam = impGetInstParamArg(pResolvedToken, callInfo, exactContextHnd,
exactContextNeedsRuntimeLookup, clsFlags, isReadonlyCall);
wasmInstParam =
impGetInstParamArg(pResolvedToken, pConstrainedResolvedToken, callInfo, exactContextHnd,
exactContextNeedsRuntimeLookup, clsFlags, isReadonlyCall);
if (wasmInstParam == nullptr)
{
assert(compDonotInline());
Expand Down Expand Up @@ -986,8 +998,8 @@ var_types Compiler::impImportCall(OPCODE opcode,

assert(opcode != CEE_CALLI);

instParam = impGetInstParamArg(pResolvedToken, callInfo, exactContextHnd, exactContextNeedsRuntimeLookup,
clsFlags, isReadonlyCall);
instParam = impGetInstParamArg(pResolvedToken, pConstrainedResolvedToken, callInfo, exactContextHnd,
exactContextNeedsRuntimeLookup, clsFlags, isReadonlyCall);
if (instParam == nullptr)
{
assert(compDonotInline());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,21 @@ private void ImportCall(ILOpcode opcode, MethodDesc method, MethodDesc runtimeDe

bool allowInstParam = opcode != ILOpcode.ldvirtftn && opcode != ILOpcode.ldftn;

if (directCall && resolvedConstraint && (exactContextNeedsRuntimeLookup || forceUseRuntimeLookup))
// A resolved non-generic method on the constrained value type needs only that type's
// context, which RyuJIT obtains from the constrained token. Keep the call direct and
// reserve the constrained type's handle as the instantiation argument, matching the
// getCallInfo decision and the compile-time generic lookup.
bool canPassConstrainedTypeArg = false;
if (allowInstParam && resolvedConstraint && !forceUseRuntimeLookup &&
!targetMethod.HasInstantiation && exactType.IsValueType)
{
TypeDesc canonConstrained = _constrained.IsRuntimeDeterminedSubtype
? _constrained.ConvertToCanonForm(CanonicalFormKind.Specific)
: _constrained;
canPassConstrainedTypeArg = exactType == canonConstrained;
}

if (directCall && resolvedConstraint && !canPassConstrainedTypeArg && (exactContextNeedsRuntimeLookup || forceUseRuntimeLookup))
{
// We want to do a direct call to a shared method on a valuetype. We need to provide
// a generic context, but the JitInterface doesn't provide a way for us to do it from here.
Expand Down Expand Up @@ -901,7 +915,19 @@ private void ImportCall(ILOpcode opcode, MethodDesc method, MethodDesc runtimeDe
}
else if (exactContextNeedsRuntimeLookup)
{
if (targetMethod.IsSharedByGenericInstantiations && !resolvedConstraint && !referencingArrayAddressMethod)
if (canPassConstrainedTypeArg)
{
// The instantiation argument is the constrained value type's handle, looked
// up from the caller's dictionary, exactly as the compile-time JIT requests
// it from the constrained token.
if (_canonMethod.IsSharedByGenericInstantiations && targetMethod.RequiresInstArg())
{
_dependencies.Add(GetGenericLookupHelper(ReadyToRunHelperId.TypeHandle, _constrained), reason);
}

_dependencies.Add(_factory.CanonicalEntrypoint(targetMethod), reason);
}
else if (targetMethod.IsSharedByGenericInstantiations && !resolvedConstraint && !referencingArrayAddressMethod)
{
ISymbolNode instParam = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2176,6 +2176,7 @@ private void ceeInfoGetCallInfo(

bool resolvedConstraint = false;
bool forceUseRuntimeLookup = false;
bool resolvedOnConstrainedValueType = false;

MethodDesc methodAfterConstraintResolution = originalMethod;
if (constrainedType == null)
Expand Down Expand Up @@ -2248,6 +2249,12 @@ private void ceeInfoGetCallInfo(
exactType = (isStaticVirtual && dimResolution == DefaultInterfaceMethodResolution.DefaultImplementation)
? directMethod.OwningType
: constrainedType;

// Capture whether the implementation is a non-generic method on the constrained
// value type itself before constrainedType is cleared for static virtuals below.
resolvedOnConstrainedValueType = !forceUseRuntimeLookup && !directMethod.HasInstantiation &&
constrainedType.IsValueType && exactType == constrainedType;

if (isStaticVirtual)
{
pResolvedToken.tokenType = CorInfoTokenKind.CORINFO_TOKENKIND_ResolvedStaticVirtualMethod;
Expand Down Expand Up @@ -2418,7 +2425,13 @@ private void ceeInfoGetCallInfo(
// which is generic numerics which will always resolve to exact valuetypes, it is not expected that
// the complexity involved would be worth the risk. Other scenarios are not expected to be as performance
// sensitive.
if (isStaticVirtual && pResult->exactContextNeedsRuntimeLookup)
// A resolved non-generic method on the constrained value type needs only that type's
// context, which RyuJIT obtains from the constrained token. Keep the call direct and
// inlinable instead of a fat function pointer, matching the CoreCLR VM. The resolved
// target is in the version bubble, so the direct call stays version resilient.
bool canPassConstrainedTypeArg = allowInstParam && resolvedConstraint && resolvedOnConstrainedValueType;

if (isStaticVirtual && pResult->exactContextNeedsRuntimeLookup && !canPassConstrainedTypeArg)
{
allowInstParam = false;
}
Expand Down Expand Up @@ -2457,7 +2470,7 @@ private void ceeInfoGetCallInfo(
// to have an unlimited number of constructors to cover stuff like "int[][][][][][]..."
pResult->kind = CORINFO_CALL_KIND.CORINFO_CALL;
}
else if ((pResult->exactContextNeedsRuntimeLookup && useInstantiatingStub && (!allowInstParam || resolvedConstraint)) || forceUseRuntimeLookup)
else if ((pResult->exactContextNeedsRuntimeLookup && useInstantiatingStub && (!allowInstParam || (resolvedConstraint && !canPassConstrainedTypeArg))) || forceUseRuntimeLookup)
{
if (unresolvedLdVirtFtn)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1375,7 +1375,14 @@ private void getCallInfo(ref CORINFO_RESOLVED_TOKEN pResolvedToken, CORINFO_RESO
ThrowHelper.ThrowBadImageFormatException();
}

if (directCall && resolvedConstraint && (pResult->exactContextNeedsRuntimeLookup || forceUseRuntimeLookup))
// A resolved non-generic method on the constrained value type needs only that type's
// context, which RyuJIT obtains from the constrained token. Keep the call direct and
// inlinable instead of falling back to a fat function pointer, matching the CoreCLR VM.
bool canPassConstrainedTypeArg = allowInstParam && resolvedConstraint && !forceUseRuntimeLookup &&
!targetMethod.HasInstantiation && exactType.IsValueType &&
exactType == constrainedType;

if (directCall && resolvedConstraint && !canPassConstrainedTypeArg && (pResult->exactContextNeedsRuntimeLookup || forceUseRuntimeLookup))
{
// We want to do a direct call to a shared method on a type. We need to provide
// a generic context, but the JitInterface doesn't provide a way for us to do it from here.
Expand Down
20 changes: 14 additions & 6 deletions src/coreclr/vm/jitinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5160,9 +5160,15 @@ void CEEInfo::getCallInfo(

bool allowInstParam = (flags & CORINFO_CALLINFO_ALLOWINSTPARAM);

// A resolved non-generic method on the constrained value type needs only that
// type's context. The JIT can obtain it from the constrained type token.
bool canPassConstrainedTypeArg = allowInstParam && fResolvedConstraint && !fForceUseRuntimeLookup &&
!pTargetMD->HasMethodInstantiation() && exactType.IsValueType() &&
exactType == constrainedType;

// If the target method is resolved via constrained static virtual dispatch
// And it requires an instParam, we do not have the generic dictionary infrastructure
// to load the correct generic context arg via EmbedGenericHandle.
// and needs a context other than the constrained type, we do not have the generic
// dictionary infrastructure to load the correct generic context arg via EmbedGenericHandle.
// Instead, force the call to go down the CORINFO_CALL_CODE_POINTER code path
// which should have somewhat inferior performance. This should only actually happen in the case
// of shared generic code calling a shared generic implementation method, which should be rare.
Expand All @@ -5178,7 +5184,8 @@ void CEEInfo::getCallInfo(
// which is generic numerics which will always resolve to exact valuetypes, it is not expected that
// the complexity involved would be worth the risk. Other scenarios are not expected to be as performance
// sensitive.
if (IsMdStatic(dwTargetMethodAttrs) && constrainedType != NULL && pResult->exactContextNeedsRuntimeLookup)
if (IsMdStatic(dwTargetMethodAttrs) && constrainedType != NULL && pResult->exactContextNeedsRuntimeLookup &&
!canPassConstrainedTypeArg)
Comment thread
NinoFloris marked this conversation as resolved.
Resolved
{
allowInstParam = FALSE;
}
Expand Down Expand Up @@ -5206,10 +5213,11 @@ void CEEInfo::getCallInfo(
// (a) some JITs may call instantiating stubs (it makes the JIT simpler) and
// (b) if the method is a remote stub then the EE will force the
// call through an instantiating stub and
// (c) constraint calls that require runtime context lookup are never resolved
// to underlying shared generic code
// (c) constrained calls that need a context other than the constrained type
// still require a runtime lookup of the instantiating stub

if (((pResult->exactContextNeedsRuntimeLookup && pTargetMD->IsInstantiatingStub() && (!allowInstParam || fResolvedConstraint)) || fForceUseRuntimeLookup))
if (((pResult->exactContextNeedsRuntimeLookup && pTargetMD->IsInstantiatingStub() &&
(!allowInstParam || (fResolvedConstraint && !canPassConstrainedTypeArg))) || fForceUseRuntimeLookup))
{
pResult->kind = CORINFO_CALL_CODE_POINTER;

Expand Down
Loading
Loading