Skip to content

Apply existing constrained call target refinements to static abstract interface methods - #135169

Open
NinoFloris wants to merge 11 commits into
dotnet:mainfrom
NinoFloris:fix/static-abstract-interface-target-opts
Open

NinoFloris wants to merge 11 commits into
dotnet:mainfrom
NinoFloris:fix/static-abstract-interface-target-opts

Conversation

@NinoFloris

@NinoFloris NinoFloris commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

Existing constrained call resolution refinements weren’t applied to static abstract interface methods: static calls were handled by an early-return path before reaching them. This moves that path after the refinements and extends the existing logic to handle static members where necessary.

This fixes #135168, and a few other deficiencies (see regression cases).

Tested on macOS arm64 Release, tiered compilation disabled. Before the change, all five static calls use a generic-dictionary lookup and an indirect call. Afterward, each implementation is inlined, with no lookup or call remaining.

Call Case Before After Inlined body
CallUnique Original FuncImpl case 100 bytes 20 bytes mov w0, w2 — return the tuple's integer field
CallExactGeneric Exact generic struct, ExactGenericFunc<int> 84 bytes 20 bytes add w0, w2, #5
CallDefault Default interface implementation 84 bytes 20 bytes mov w0, #7
CallCovariantUnique Unique covariant interface implementation 60 bytes 28 bytes Load the constant typeof(string) reference
CallGenericExact Generic method with exact int argument 84 bytes 28 bytes Load the constant typeof(int) reference

Address values shown as 0xD1FFAB1E are placeholders emitted by JitDisasmDiffable.

CallUnique: 100 → 20 bytes

Before:

G_M000_IG01:
            stp     fp, lr, [sp, #-0x30]!
            stp     x19, x20, [sp, #0x20]
            mov     fp, sp
            str     x0, [fp, #0x18]
            mov     x19, x1
            mov     w20, w2

G_M000_IG02:
            ldr     x1, [x0, #0x18]
            ldr     x2, [x1, #0x18]
            cbz     x2, G_M000_IG05

G_M000_IG03:
            mov     x0, x19
            mov     w1, w20
            blr     x2

G_M000_IG04:
            ldp     x19, x20, [sp, #0x20]
            ldp     fp, lr, [sp], #0x30
            ret     lr

G_M000_IG05:
            movz    x1, #0xD1FFAB1E
            movk    x1, #0xD1FFAB1E LSL #16
            movk    x1, #1 LSL #32
            movz    x2, #0xD1FFAB1E
            movk    x2, #0xD1FFAB1E LSL #16
            movk    x2, #1 LSL #32
            ldr     x2, [x2]
            blr     x2      // code for CORINFO_HELP_RUNTIMEHANDLE_METHOD
            mov     x2, x0
            b       G_M000_IG03

After:

G_M000_IG01:
            stp     fp, lr, [sp, #-0x10]!
            mov     fp, sp

G_M000_IG02:
            mov     w0, w2

G_M000_IG03:
            ldp     fp, lr, [sp], #0x10
            ret     lr
CallExactGeneric: 84 → 20 bytes

Before:

G_M000_IG01:
            stp     fp, lr, [sp, #-0x30]!
            stp     x19, x20, [sp, #0x20]
            mov     fp, sp
            str     x0, [fp, #0x18]
            mov     x19, x1
            mov     w20, w2

G_M000_IG02:
            ldr     x1, [x0, #0x18]
            ldr     x2, [x1, #0x18]
            cbz     x2, G_M000_IG05

G_M000_IG03:
            mov     x0, x19
            mov     w1, w20
            blr     x2

G_M000_IG04:
            ldp     x19, x20, [sp, #0x20]
            ldp     fp, lr, [sp], #0x30
            ret     lr

G_M000_IG05:
            movz    x1, #0xD1FFAB1E
            movk    x1, #0xD1FFAB1E LSL #16
            movk    x1, #1 LSL #32
            bl      CORINFO_HELP_RUNTIMEHANDLE_METHOD
            mov     x2, x0
            b       G_M000_IG03

After:

G_M000_IG01:
            stp     fp, lr, [sp, #-0x10]!
            mov     fp, sp

G_M000_IG02:
            add     w0, w2, #5

G_M000_IG03:
            ldp     fp, lr, [sp], #0x10
            ret     lr
CallDefault: 84 → 20 bytes

Before:

G_M000_IG01:
            stp     fp, lr, [sp, #-0x30]!
            stp     x19, x20, [sp, #0x20]
            mov     fp, sp
            str     x0, [fp, #0x18]
            mov     x19, x1
            mov     w20, w2

G_M000_IG02:
            ldr     x1, [x0, #0x18]
            ldr     x2, [x1, #0x18]
            cbz     x2, G_M000_IG05

G_M000_IG03:
            mov     x0, x19
            mov     w1, w20
            blr     x2

G_M000_IG04:
            ldp     x19, x20, [sp, #0x20]
            ldp     fp, lr, [sp], #0x30
            ret     lr

G_M000_IG05:
            movz    x1, #0xD1FFAB1E
            movk    x1, #0xD1FFAB1E LSL #16
            movk    x1, #1 LSL #32
            bl      CORINFO_HELP_RUNTIMEHANDLE_METHOD
            mov     x2, x0
            b       G_M000_IG03

After:

G_M000_IG01:
            stp     fp, lr, [sp, #-0x10]!
            mov     fp, sp

G_M000_IG02:
            mov     w0, #7

G_M000_IG03:
            ldp     fp, lr, [sp], #0x10
            ret     lr
CallCovariantUnique: 60 → 28 bytes

Before:

G_M000_IG01:
            stp     fp, lr, [sp, #-0x20]!
            mov     fp, sp
            str     x0, [fp, #0x18]

G_M000_IG02:
            ldr     x1, [x0, #0x18]
            ldr     x2, [x1, #0x18]
            cbz     x2, G_M000_IG05

G_M000_IG03:
            blr     x2

G_M000_IG04:
            ldp     fp, lr, [sp], #0x20
            ret     lr

G_M000_IG05:
            movz    x1, #0xD1FFAB1E
            movk    x1, #0xD1FFAB1E LSL #16
            movk    x1, #1 LSL #32
            bl      CORINFO_HELP_RUNTIMEHANDLE_METHOD
            mov     x2, x0
            b       G_M000_IG03

After:

G_M000_IG01:
            stp     fp, lr, [sp, #-0x10]!
            mov     fp, sp

G_M000_IG02:
            movz    x0, #0xD1FFAB1E
            movk    x0, #0xD1FFAB1E LSL #16
            movk    x0, #1 LSL #32

G_M000_IG03:
            ldp     fp, lr, [sp], #0x10
            ret     lr
CallGenericExact: 84 → 28 bytes

Before:

G_M000_IG01:
            stp     fp, lr, [sp, #-0x30]!
            stp     x19, x20, [sp, #0x20]
            mov     fp, sp
            str     x0, [fp, #0x18]
            mov     x19, x1
            mov     w20, w2

G_M000_IG02:
            ldr     x1, [x0, #0x18]
            ldr     x2, [x1, #0x20]
            cbz     x2, G_M000_IG05

G_M000_IG03:
            mov     x0, x19
            mov     w1, w20
            blr     x2

G_M000_IG04:
            ldp     x19, x20, [sp, #0x20]
            ldp     fp, lr, [sp], #0x30
            ret     lr

G_M000_IG05:
            movz    x1, #0xD1FFAB1E
            movk    x1, #0xD1FFAB1E LSL #16
            movk    x1, #1 LSL #32
            bl      CORINFO_HELP_RUNTIMEHANDLE_METHOD
            mov     x2, x0
            b       G_M000_IG03

After:

G_M000_IG01:
            stp     fp, lr, [sp, #-0x10]!
            mov     fp, sp

G_M000_IG02:
            movz    x0, #0xD1FFAB1E
            movk    x0, #0xD1FFAB1E LSL #16
            movk    x0, #1 LSL #32

G_M000_IG03:
            ldp     fp, lr, [sp], #0x10
            ret     lr

@github-actions github-actions Bot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Oct 3, 2026
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Oct 3, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.

@NinoFloris

Copy link
Copy Markdown
Contributor Author

Failure is #134825

@MichalStrehovsky MichalStrehovsky added area-TypeSystem-coreclr and removed area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI labels Oct 5, 2026
@NinoFloris
NinoFloris force-pushed the fix/static-abstract-interface-target-opts branch from 9729caa to a75b0b1 Compare October 30, 2026 13:15
@NinoFloris

NinoFloris commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor Author

Added another commit allowing target refinement for static abstract calls on reference types. Instance calls retain the existing restriction to value types. Static abstract calls now use the same target refinements for class targets.

The reason this is safe is that static abstract dispatch depends on the constrained type argument, not an object’s runtime type.

This is one part of improving static abstract performance in shared generics when the implementing type family is known (e.g. Node<T>, instantiated with string). We can identify the interface implementation even though the generic argument causes the code to be shared.

@NinoFloris

Copy link
Copy Markdown
Contributor Author

@MihuBot

@NinoFloris

Copy link
Copy Markdown
Contributor Author

@MichalStrehovsky who could review this on the CoreCLR side?

@am11
am11 requested a review from jkotas November 18, 2026 14:50
@MichalStrehovsky

Copy link
Copy Markdown
Member

@MichalStrehovsky who could review this on the CoreCLR side?

@davidwrighton would be the best

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Critical static-interface resolution defects leave generic method arguments unapplied and produce invalid NativeAOT lookups when canonical fallback targets have a different declaring type.

2 open issues
What changed in this PR

This PR extends existing constrained-call target refinements to static abstract interface methods in CoreCLR and the shared compiler type system, enabling more direct calls and inlining.

Changes:

  • Moves static resolution after interface-target refinement while retaining fallback handling.
  • Applies corresponding resolution changes to the managed compiler implementation.
  • Adds functional regressions and ARM64/x64 disassembly checks for generic, default-interface, variant, inherited, and delegate calls.
File Description
src/​tests/​Loader/​classloader/​StaticVirtualMethods/​Regression/​CanonicalInterface.csproj Configures isolated, optimized regression tests with disassembly checks.
src/​tests/​Loader/​classloader/​StaticVirtualMethods/​Regression/​CanonicalInterface.cs Adds constrained-call correctness and code-generation coverage.
src/​coreclr/​vm/​methodtable.cpp Extends CoreCLR target refinement to static interface methods.
src/​coreclr/​tools/​Common/​Compiler/​TypeExtensions.cs Mirrors static-call refinement in the shared compiler type system.

Review effort: Balanced

Comment thread src/coreclr/vm/methodtable.cpp Resolved

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The cross-runtime shared-generic dispatch changes need checked NativeAOT and ReadyToRun regression execution to confirm codegen/lookup agreement and fallback behavior beyond the reported CoreCLR macOS arm64 validation.

No open issues 🎉

2 resolved since last review

Review effort: Balanced

… interface methods

Extend the existing instance target-resolution strategy to constrained static interface calls on value types. Preserve instance handling, static defaults, and reference-type hierarchy resolution without changing dictionary lookup or call generation.

Add functional coverage for shared receivers, generic methods, defaults, variance, and delegates, including the FuncImpl scenario.
Route interface-owned candidates through the static resolver so the declaring type and generic method arguments are applied. Cover exact and shared method arguments, closed inheritance, and generic interface reimplementation through calls and delegates.
…d ReadyToRun

Recovering the runtime-determined constraint method must not conflate different instantiations of the owning interface. The managed resolver gains variant and default-interface candidate resolution on the definition form with unique-resolution enforcement, ambiguity falls back to ConstrainedDirectCall through runtime-determined method recovery, and ReadyToRun validates hierarchy, variant and default-interface selections with the same resolver proof at fixup time.

Both resolvers split into a static and an instance half behind the shared entry point, each a gather and decide pipeline with candidate resolution shared between the scan loop, the exact paths and the static fallback. Non-variant interfaces skip the variant resolvers on the exact and fallback paths, and the optional static resolution is a real discard rather than Unsafe.NullRef, so the fallback's unconditional assignment is always valid.
The ECMA-335 augments specify the variance pass of interface resolution as returning the first variant match in interface order. The runtime resolves variant static matches the same way, the static interface method section just leaves the rule uncodified. The canonical-family filter hid same-definition entries from other families, so a shared body could accept a target that an earlier entry wins at exact instantiations. All same-definition entries now participate in the agreement check and disagreement defers to runtime lookup, which keeps compiled bodies on the interface-order rule without baking a winner. The regression test accordingly asserts agreement between the shared and the exact compilation rather than encoding the order. Codifying the variance pass for static interface methods, mirroring the instance rule, remains a one-sentence spec addition for the owners.
The all-default cross-family shape locks the agreement path for variant default implementations, asserting consistency between the shared and the exact compilation. The contravariant shapes cover the reversed conversion direction, which shares the machinery but had no coverage, including a defaulted form whose result carries the owner context.
The single-use shape-pinning wrappers fold into parameterized check methods that keep one shared generic context per instantiation flavor, following the existing CheckInterfaceInstantiations pattern. Coverage is unchanged, the same constraint tokens compile in the same sharing contexts, exact-side consistency anchors stay in the non-generic caller, and the revisit-after-other-instantiation repetitions are preserved.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The new canonical-match refinement causes native validation to reject an unchanged ReadyToRun body and force JIT compilation, requiring aligned validation or preserved indirect dispatch plus a regression test.

1 open issue

Review effort: Balanced

Bring native constrained resolution in line with the managed resolver. Select candidates on the generic definition before canonicalization merges parameters, require agreement across potentially matching interfaces, and reconstruct the selected declaring owner in the caller context.

Keep method definitions and owners separate during selection and apply the caller method arguments afterward, avoiding premature generic constraint checks. ReadyToRun can validate the shared refinements directly without a capability flag or a separate restriction on managed resolution.

Cover ReadyToRun body acceptance, inherited and default-interface targets, and generic method constraints.
When a value type has no implementation for the selected interface method, try the default implementation on that same interface instantiation. Share the default-method resolver with static constrained calls.

An interface-owned instance target still needs a boxed receiver. Keep the existing fallback until constrained calls can use that target for devirtualization.
Track whether every matching interface uses its declared default while comparing exact targets. For an invariant interface on a concrete value type, a matching constraint on the caller identifies the exact interface context. CoreCLR and NativeAOT can then call the shared body directly.
Pass the interface context for ordinary calls. When a function pointer is required, use the original method token to populate a MethodEntrySlot for the proven default body. This binds its exact owner without repeating constrained dispatch. Leave ambiguous implementations on the constrained lookup path.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-TypeSystem-coreclr community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Try to resolve exact targets for constrained static interface calls

3 participants