Skip to content

[r2r][wasm] Don't root implementations of non-generic interface slots from virtual function pointer lookups - #137271

Closed
lewing wants to merge 1 commit into
dotnet:mainfrom
lewing:lewing-narrow-wasm-slot-use-rooting
Closed

lewing wants to merge 1 commit into
dotnet:mainfrom
lewing:lewing-narrow-wasm-slot-use-rooting

Conversation

@lewing

@lewing lewing commented Nov 10, 2026

Copy link
Copy Markdown
Member

The Wasm JIT calls CORINFO_HELP_VIRTUAL_FUNC_PTR for virtual and interface lookups instead of using a VirtualEntry cell, so #136769 added recordWasmVirtualFunctionPointerLookup, which adds the cell's dependencies (VirtualMethodUse, GVM dependencies and the cross-module compile dependency) for each lookup that survives lowering. This change narrows that notification.

Cause

A non-generic method on a non-generic interface is a single global slot. One IEnumerable.GetEnumerator, IEnumerator.MoveNext/get_Current, IDisposable.Dispose, ICollection.* or IList.* lookup anywhere marks the slot used, and the implementation is then rooted on every instantiation of every implementing type: List<X>, ImmutableArray<X>, LINQ iterators over value types, and so on. Those implementations reference further instantiations, and in a composite image every method of each new generic instantiation is compiled (AllMethodsOnType), so the closure keeps growing.

Rule

In recordWasmVirtualFunctionPointerLookup, skip the VirtualEntry dependencies when the method's owning type is a non-generic interface and the method itself is not generic. Those implementations are left to the runtime, as they were before #136769. Lookups through generic interfaces (for example IList<T>, IEnumerable<T>), generic virtual methods (including GVMs on non-generic interfaces) and class virtuals keep their dependencies. There is no JIT or JIT-EE change.

Size

CI-shaped browser composite (MicroBenchmarks plus its whole closure, built with the SDK browser publish crossgen2 options, framework from the same snapshot, crossgen2 from current main):

Image Compiled methods
Before #136769 35.65 MB 113,139
main 56.50 MB 176,173
This PR 36.04 MB 117,396

The intended wins from #136769 are kept: SZArrayHelper.get_Item/get_Count/Contains/GetEnumerator<int> and List<int>'s IEnumerable<T>.GetEnumerator are still compiled.

Browser System.Private.CoreLib.wasm (./build.sh clr.nativecorelib -os browser -c Release):

Raw Brotli-11
main 17,537,240 3,307,882
This PR 17,221,840 3,282,429

Tests

  • NonGVM.cs gains Test14: Test14A<int> explicitly implements both a non-generic ITest14 and a generic ITest14<int>, reached only through those interfaces via a non-generic derived class (so no type handle fixup roots all of its methods). On Wasm, ITest14<T>.Test14GenericMethod is compiled and ITest14.Test14Method is not. On other targets both are compiled. The new Wasm assertion fails without the crossgen2 change.
  • Coverage change: NonGVM Test3 (ITest3WithDim<int>'s ITest3Base.Test3Method DIM, reached only through the non-generic ITest3Base) is no longer precompiled on Wasm. Its assertion moved from the shared AssertNonGVMImplementationsCompiled helper to the non-Wasm test.
  • GVM tests are unchanged and pass.

R2R modes exercised: single-assembly non-composite with --generate-unboxing-stubs (NonGVM, GVM), composite with CoreLib unrooted (ArrayInterfaceMethods), plus the rest of ILCompiler.ReadyToRun.Tests.

  • browser-wasm (after ./build.sh clr+libs -os browser -c Release): dotnet build src/coreclr/tools/aot/ILCompiler.ReadyToRun.Tests/ILCompiler.ReadyToRun.Tests.csproj -c Release -p:TargetOS=browser -p:TargetArchitecture=wasm then dotnet test artifacts/bin/ILCompiler.ReadyToRun.Tests/Release/Release_CLR/browser-wasm/ILCompiler.ReadyToRun.Tests.dll: passed, 284 passed, 0 failed, 42 skipped.
  • osx-arm64 (after ./build.sh clr+libs -c Release): dotnet build src/coreclr/tools/aot/ILCompiler.ReadyToRun.Tests/ILCompiler.ReadyToRun.Tests.csproj -c Release then dotnet test artifacts/bin/ILCompiler.ReadyToRun.Tests/Release/Release_CLR/osx-arm64/ILCompiler.ReadyToRun.Tests.dll: passed, 310 passed, 0 failed, 18 skipped.

@davidwrighton, could you review this? It narrows the dependencies added in #136769.

Note

This PR description was generated with the help of GitHub Copilot.

… from virtual function pointer lookups

A non-generic method on a non-generic interface is a single global slot, so
reporting it from recordWasmVirtualFunctionPointerLookup roots its
implementation on every instantiation of every implementing type. In a
composite image those implementations reference further instantiations,
which are then compiled in full. Skip such slots and leave their
implementations to the runtime. Generic interfaces and generic virtual
methods keep their dependencies.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@azure-pipelines

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

@lewing
lewing requested a review from davidwrighton November 10, 2026 18:55
@lewing lewing added the arch-wasm WebAssembly architecture label Nov 10, 2026
@lewing
lewing marked this pull request as draft November 10, 2026 19:19
@lewing lewing closed this Nov 10, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/crossgen-contrib
See info in area-owners.md if you want to be subscribed.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to 'arch-wasm': @lewing, @pavelsavara
See info in area-owners.md if you want to be subscribed.

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

Labels

arch-wasm WebAssembly architecture area-ReadyToRun

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant