Repository navigation
Apply existing constrained call target refinements to static abstract interface methods - #135169
NinoFloris wants to merge 11 commits into
Conversation
|
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. |
|
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch |
8d3a4c9 to
9729caa
Compare
|
Failure is #134825 |
9729caa to
a75b0b1
Compare
|
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. |
|
@MichalStrehovsky who could review this on the CoreCLR side? |
@davidwrighton would be the best |
There was a problem hiding this comment.
🟡 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
a75b0b1 to
76e3488
Compare
76e3488 to
8e95748
Compare
There was a problem hiding this comment.
🔵 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.
d3a7042 to
3244621
Compare
There was a problem hiding this comment.
🟡 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.


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.
CallUniqueFuncImplcasemov w0, w2— return the tuple's integer fieldCallExactGenericExactGenericFunc<int>add w0, w2, #5CallDefaultmov w0, #7CallCovariantUniquetypeof(string)referenceCallGenericExactintargumenttypeof(int)referenceAddress values shown as
0xD1FFAB1Eare placeholders emitted byJitDisasmDiffable.CallUnique: 100 → 20 bytes
Before:
After:
CallExactGeneric: 84 → 20 bytes
Before:
After:
CallDefault: 84 → 20 bytes
Before:
After:
CallCovariantUnique: 60 → 28 bytes
Before:
After:
CallGenericExact: 84 → 28 bytes
Before:
After: