From 79070a4027800b03cebf7fc73ab0d9395ca34b2c Mon Sep 17 00:00:00 2001 From: Nino Floris Date: Sat, 3 Oct 2026 23:25:13 +0200 Subject: [PATCH 1/4] Pass constrained type context to resolved shared generic calls Allow direct calls to resolved non-generic methods on the constrained value type by passing its type context explicitly. Teach the JIT and interpreter to obtain that context from the constrained token, preserving runtime lookup for unsupported or ambiguous targets. Add functional and codegen regressions covering inlining, context-dependent implementations, mutation, initialization, delegates, generic methods, and return shapes. --- src/coreclr/interpreter/compiler.cpp | 9 +- src/coreclr/jit/compiler.h | 1 + src/coreclr/jit/importercalls.cpp | 22 +- src/coreclr/vm/jitinterface.cpp | 20 +- .../generics/Misc/ConstrainedTypeContext.cs | 206 ++++++++++++++++++ .../Misc/ConstrainedTypeContext.csproj | 15 ++ 6 files changed, 261 insertions(+), 12 deletions(-) create mode 100644 src/tests/Loader/classloader/generics/Misc/ConstrainedTypeContext.cs create mode 100644 src/tests/Loader/classloader/generics/Misc/ConstrainedTypeContext.csproj diff --git a/src/coreclr/interpreter/compiler.cpp b/src/coreclr/interpreter/compiler.cpp index 34c03408e39163..a59c70ce4e711b 100644 --- a/src/coreclr/interpreter/compiler.cpp +++ b/src/coreclr/interpreter/compiler.cpp @@ -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; + } } } } diff --git a/src/coreclr/jit/compiler.h b/src/coreclr/jit/compiler.h index ef9c038fd3ed1b..9db2197522d0fa 100644 --- a/src/coreclr/jit/compiler.h +++ b/src/coreclr/jit/compiler.h @@ -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, diff --git a/src/coreclr/jit/importercalls.cpp b/src/coreclr/jit/importercalls.cpp index a6112f1bbb47ac..a815818a8db171 100644 --- a/src/coreclr/jit/importercalls.cpp +++ b/src/coreclr/jit/importercalls.cpp @@ -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 @@ -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, @@ -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()); @@ -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()); @@ -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()); diff --git a/src/coreclr/vm/jitinterface.cpp b/src/coreclr/vm/jitinterface.cpp index 458084d10a0550..76fbffecc271f1 100644 --- a/src/coreclr/vm/jitinterface.cpp +++ b/src/coreclr/vm/jitinterface.cpp @@ -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. @@ -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) { allowInstParam = FALSE; } @@ -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; diff --git a/src/tests/Loader/classloader/generics/Misc/ConstrainedTypeContext.cs b/src/tests/Loader/classloader/generics/Misc/ConstrainedTypeContext.cs new file mode 100644 index 00000000000000..8e0c801ffd1ac6 --- /dev/null +++ b/src/tests/Loader/classloader/generics/Misc/ConstrainedTypeContext.cs @@ -0,0 +1,206 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System; +using System.Runtime.CompilerServices; +using Xunit; + +public class ConstrainedTypeContext +{ + private interface IOperation + { + int Add(int value); + static abstract int Increment(int value); + } + + private struct Operation : IOperation + { + public int Bias; + public int Add(int value) => value + Bias; + public static int Increment(int value) => value + 1; + } + + private interface IContext + { + Type GetTag(); + static abstract Type GetStaticTag(); + } + + private struct Context : IContext + { + public int Calls; + public Type GetTag() { Calls++; return typeof(T); } + + [MethodImpl(MethodImplOptions.NoInlining)] + public static Type GetStaticTag() => typeof(T); + } + + private struct InitializedContext : IContext + { + private static readonly Type s_tag; + static InitializedContext() => s_tag = typeof(T); + public Type GetTag() => s_tag; + public static Type GetStaticTag() => s_tag; + } + + private interface IEcho + { + T GetValue(); + } + + private struct Echo : IEcho + { + public T Value; + + [MethodImpl(MethodImplOptions.NoInlining)] + public T GetValue() => Value; + } + + private struct Ambiguous : IEcho, IEcho + { + public int Calls; + object IEcho.GetValue() { Calls += 1; return typeof(T); } + string IEcho.GetValue() { Calls += 10; return "string implementation"; } + } + + private interface IGeneric + { + Type GetTypes(); + static abstract Type GetStaticTypes(); + } + + private struct Generic : IGeneric + { + public Type GetTypes() => typeof((T, U)); + public static Type GetStaticTypes() => typeof((T, U)); + } + + private interface IDefault : IContext + { + Type IContext.GetTag() => typeof(T); + static Type IContext.GetStaticTag() => typeof(T); + } + + private struct DefaultContext : IDefault { } + + private class ReferenceContext : IContext + { + public Type GetTag() => typeof(T); + public static Type GetStaticTag() => typeof(T); + } + + [MethodImpl(MethodImplOptions.NoInlining)] + private static int CallInstance(ref T operation, int value) where T : struct, IOperation + { + // ARM64-NOT: {{bl(r)?[[:space:]]}} + // ARM64: ret + // X64-NOT: call + // X64: ret + return operation.Add(value); + } + + [MethodImpl(MethodImplOptions.NoInlining)] + private static int CallStatic(int value) where T : struct, IOperation + { + // ARM64-NOT: {{bl(r)?[[:space:]]}} + // ARM64: ret + // X64-NOT: call + // X64: ret + return T.Increment(value); + } + + [MethodImpl(MethodImplOptions.NoInlining)] + private static Type CallContext(ref T value) where T : IContext => value.GetTag(); + + [MethodImpl(MethodImplOptions.NoInlining)] + private static Type CallStaticContext() where T : IContext => T.GetStaticTag(); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static Type InlineContext(ref T value) where T : IContext => value.GetTag(); + + [MethodImpl(MethodImplOptions.NoInlining)] + private static Type CallConstructedContext() + { + var context = new Context(); + return InlineContext(ref context); + } + + [MethodImpl(MethodImplOptions.NoInlining)] + private static Func GetStaticDelegate() where T : IContext => T.GetStaticTag; + + [MethodImpl(MethodImplOptions.NoInlining)] + private static TValue CallEcho(ref T value) where T : struct, IEcho => value.GetValue(); + + [MethodImpl(MethodImplOptions.NoInlining)] + private static Type CallGeneric(T value) where T : struct, IGeneric => value.GetTypes(); + + [MethodImpl(MethodImplOptions.NoInlining)] + private static Type CallStaticGeneric() where T : struct, IGeneric => T.GetStaticTypes(); + + private class Caller where T : IContext + { + [MethodImpl(MethodImplOptions.NoInlining)] + public Type Call(ref T value) => value.GetTag(); + + [MethodImpl(MethodImplOptions.NoInlining)] + public static Type CallStatic() => T.GetStaticTag(); + } + + [MethodImpl(MethodImplOptions.NoInlining)] + private static void CheckContext() + { + var context = new Context(); + Assert.Equal(typeof(T), CallContext(ref context)); + Assert.Equal(1, context.Calls); + Assert.Equal(typeof(T), CallStaticContext>()); + Assert.Equal(typeof(T), CallConstructedContext()); + Assert.Equal(typeof(T), new Caller>().Call(ref context)); + Assert.Equal(2, context.Calls); + Assert.Equal(typeof(T), Caller>.CallStatic()); + Assert.Equal(typeof(T), GetStaticDelegate>()()); + + var initialized = new InitializedContext(); + Assert.Equal(typeof(T), CallStaticContext>()); + Assert.Equal(typeof(T), CallContext(ref initialized)); + + var defaultContext = new DefaultContext(); + Assert.Equal(typeof(T), CallContext(ref defaultContext)); + Assert.Equal(typeof(T), CallStaticContext>()); + var reference = new ReferenceContext(); + Assert.Equal(typeof(T), CallContext(ref reference)); + Assert.Equal(typeof(T), CallStaticContext>()); + + Assert.Equal(typeof((T, object)), CallGeneric, object>(default)); + Assert.Equal(typeof((T, string)), CallGeneric, string>(default)); + Assert.Equal(typeof((T, int)), CallGeneric, int>(default)); + Assert.Equal(typeof((T, object)), CallStaticGeneric, object>()); + Assert.Equal(typeof((T, string)), CallStaticGeneric, string>()); + Assert.Equal(typeof((T, int)), CallStaticGeneric, int>()); + + var ambiguous = new Ambiguous(); + Assert.Equal(typeof(T), CallEcho, object>(ref ambiguous)); + Assert.Equal("string implementation", CallEcho, string>(ref ambiguous)); + Assert.Equal(11, ambiguous.Calls); + } + + [Fact] + public static void TestEntryPoint() + { + var operation = new Operation { Bias = 3 }; + Assert.Equal(7, CallInstance(ref operation, 4)); + Assert.Equal(5, CallStatic>(4)); + + CheckContext(); + CheckContext(); + CheckContext<(object, string)>(); + CheckContext(); + + object value = new object(); + var echo = new Echo { Value = value }; + Assert.Same(value, CallEcho, object>(ref echo)); + var text = new Echo { Value = "echo" }; + Assert.Equal("echo", CallEcho, string>(ref text)); + var pair = new Echo<(object, int)> { Value = (value, 42) }; + Assert.Equal((value, 42), CallEcho, (object, int)>(ref pair)); + } +} diff --git a/src/tests/Loader/classloader/generics/Misc/ConstrainedTypeContext.csproj b/src/tests/Loader/classloader/generics/Misc/ConstrainedTypeContext.csproj new file mode 100644 index 00000000000000..a08471db483d66 --- /dev/null +++ b/src/tests/Loader/classloader/generics/Misc/ConstrainedTypeContext.csproj @@ -0,0 +1,15 @@ + + + 1 + true + None + true + + + + true + + + + + From d000efa054a26cd99bc2bbfb86e3d55897975914 Mon Sep 17 00:00:00 2001 From: Nino Floris Date: Sat, 21 Nov 2026 01:46:42 +0100 Subject: [PATCH 2/4] Pass constrained type context to resolved shared generic calls in NativeAOT Mirror the CoreCLR getCallInfo decision in the NativeAOT compiler. When a constrained call resolves to a non-generic method on the constrained value type itself, keep the call direct and inlinable and supply the instantiation argument from the constrained token instead of falling back to a fat function pointer through a dictionary slot. NativeAOT precomputes generic dictionary layouts in the scanner, so the scanner must reserve the same lookup the compile-time JIT requests. Teach the scanner to keep the call direct for this case and reserve the constrained type's handle as the instantiation argument, matching getCallInfo and the shared importer that reads the context from the constrained token. --- .../IL/ILImporter.Scanner.cs | 30 +++++++++++++++++-- .../JitInterface/CorInfoImpl.RyuJit.cs | 9 +++++- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/src/coreclr/tools/aot/ILCompiler.Compiler/IL/ILImporter.Scanner.cs b/src/coreclr/tools/aot/ILCompiler.Compiler/IL/ILImporter.Scanner.cs index aac03eb2deaafc..391004f4dfd376 100644 --- a/src/coreclr/tools/aot/ILCompiler.Compiler/IL/ILImporter.Scanner.cs +++ b/src/coreclr/tools/aot/ILCompiler.Compiler/IL/ILImporter.Scanner.cs @@ -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. @@ -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; diff --git a/src/coreclr/tools/aot/ILCompiler.RyuJit/JitInterface/CorInfoImpl.RyuJit.cs b/src/coreclr/tools/aot/ILCompiler.RyuJit/JitInterface/CorInfoImpl.RyuJit.cs index d720daaaf2a8b9..3db3a8471c5195 100644 --- a/src/coreclr/tools/aot/ILCompiler.RyuJit/JitInterface/CorInfoImpl.RyuJit.cs +++ b/src/coreclr/tools/aot/ILCompiler.RyuJit/JitInterface/CorInfoImpl.RyuJit.cs @@ -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. From ec8bf8144e37fb86f0e53e5801ae1bd6a9447a12 Mon Sep 17 00:00:00 2001 From: Nino Floris Date: Sat, 21 Nov 2026 01:46:42 +0100 Subject: [PATCH 3/4] Pass constrained type context to resolved shared generic calls in ReadyToRun Mirror the CoreCLR getCallInfo decision in crossgen2. When a constrained call resolves to a non-generic method on the constrained value type itself, keep the call direct and inlinable and supply the instantiation argument from the constrained token instead of a fat function pointer through a dictionary slot. The constrained type is captured before it is cleared for static virtual calls. Resolution already refuses targets outside the version bubble, so the direct call stays version resilient. Crossgen2 has no separate scanner, so no dictionary layout reservation is needed beyond keeping the call off the code pointer path. --- .../JitInterface/CorInfoImpl.ReadyToRun.cs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/coreclr/tools/aot/ILCompiler.ReadyToRun/JitInterface/CorInfoImpl.ReadyToRun.cs b/src/coreclr/tools/aot/ILCompiler.ReadyToRun/JitInterface/CorInfoImpl.ReadyToRun.cs index 7d6bd89fd29136..e8764df0a6e72f 100644 --- a/src/coreclr/tools/aot/ILCompiler.ReadyToRun/JitInterface/CorInfoImpl.ReadyToRun.cs +++ b/src/coreclr/tools/aot/ILCompiler.ReadyToRun/JitInterface/CorInfoImpl.ReadyToRun.cs @@ -2176,6 +2176,7 @@ private void ceeInfoGetCallInfo( bool resolvedConstraint = false; bool forceUseRuntimeLookup = false; + bool resolvedOnConstrainedValueType = false; MethodDesc methodAfterConstraintResolution = originalMethod; if (constrainedType == null) @@ -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; @@ -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; } @@ -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) { From 49c04619e674366ee9140d1f9d410ab5a1ba93c4 Mon Sep 17 00:00:00 2001 From: Nino Floris Date: Sat, 21 Nov 2026 01:46:42 +0100 Subject: [PATCH 4/4] Describe the dispatch-helper call in dumps instead of asserting LowerVirtualStubCall rewrites an interface dispatch into a direct call to the dispatch helper and clears the method handle, leaving a CT_USER_FUNC call whose gtCallMethHnd is null. The debug dump and disassembly paths passed that handle to eeGetMethodFullName and eeGetMethodName, which call getMethodClass without a null check and assert on hosts that reject a null handle, such as a Checked ILC, so JitDump and JitDisasm tore down the compiler on any surviving interface dispatch. Guard both name helpers so a null handle yields a placeholder rather than a crash. In the tree dump, recognize the lowered dispatch-helper shape, a direct-address call with no method handle, and describe it as the stub dispatch helper with its target address. Normal compilation never takes these dump paths and is unchanged. --- src/coreclr/jit/eeinterface.cpp | 15 +++++++++++++++ src/coreclr/jit/gentree.cpp | 13 ++++++++++--- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/coreclr/jit/eeinterface.cpp b/src/coreclr/jit/eeinterface.cpp index d05bcba0ffbf12..98ed8efa97abf4 100644 --- a/src/coreclr/jit/eeinterface.cpp +++ b/src/coreclr/jit/eeinterface.cpp @@ -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 ""; + } + CorInfoHelpFunc helper = eeGetHelperNum(hnd); if (helper != CORINFO_HELP_UNDEF) { @@ -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 ""; + } + StringPrinter p(getAllocator(CMK_DebugOnly), buffer, bufferSize); bool success = eeRunFunctorWithSPMIErrorTrap([&]() { eePrintMethod(&p, NO_CLASS_HANDLE, methHnd, diff --git a/src/coreclr/jit/gentree.cpp b/src/coreclr/jit/gentree.cpp index aff721c727ef16..ddef46660ec2b0 100644 --- a/src/coreclr/jit/gentree.cpp +++ b/src/coreclr/jit/gentree.cpp @@ -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())