Repository navigation
Conversation
Wasm lowers every interface call to CORINFO_VIRTUALCALL_LDVIRTFTN, which emits a MethodHandle fixup rather than a VirtualEntry dispatch cell. VirtualMethodUse was only recorded for VirtualEntry fixups, so the conditional discovery in InheritedVirtualMethodsNode and ArrayInterfaceMethodsNode never fired on wasm. Interface implementations on generic value-type instantiations (e.g. SZArrayHelper.get_Count<S>, List<S>, Dictionary collections and enumerators) were left to the interpreter, even in composite builds. Mark the slot as used in the wasm interface LDVIRTFTN path. Extend the wasm NonGVM test to assert the interface implementations it already asserts on other targets, and add a composite test covering SZArrayHelper instantiations. Co-authored-by: Copilot App <[email protected]>
The Wasm JIT lowers CORINFO_VIRTUALCALL_LDVIRTFTN through CORINFO_HELP_VIRTUAL_FUNC_PTR instead of the VirtualEntry dynamic helper cell, because DelayLoad_Helper_Obj thunks are not implemented on Wasm. The cell's MethodFixupSignature is what adds the virtual dispatch dependencies, so on Wasm interface calls, generic virtual method calls and delegates created with ldvirtftn lost them, and the implementations were left to the interpreter. Extract those dependencies into MethodFixupSignature.AddVirtualEntryDependencies and apply them at the LDVIRTFTN lowering on Wasm for callers that do not need a runtime lookup, matching the call sites that create a VirtualEntry signature on other targets. This replaces the interface-only VirtualMethodUse from the previous commit, which also registered shared callers. Share the compiled-implementation assertions between the Wasm and non-Wasm VirtualMethodGenerics tests, and add delegate ldvirtftn cases. Co-authored-by: Copilot App <[email protected]>
Implement DelayLoad_Helper_Obj for CoreCLR WebAssembly so the JIT can use CORINFO_HELP_READYTORUN_VIRTUAL_FUNC_PTR (the VirtualEntry dynamic helper cell) like other ReadyToRun targets, instead of CORINFO_HELP_VIRTUAL_FUNC_PTR. Runtime: DelayLoad_Helper_Obj reports the object argument to the GC and, when DynamicHelperWorker leaves its result in the first argument slot, returns a portable entrypoint that hands back that argument. VirtualEntry fixups resolve to loader-heap data whose first word indexes a precompiled (sp, object, pe) -> nint stub that either calls VirtualDispatchHelpers.VirtualFunctionPointer_Dynamic or returns a constant. crossgen2: VirtualEntry cells imported through DelayLoad_Helper_Obj use the fixed (sp, object, pe) -> nint thunk signature. The cell carries the VirtualEntry dependencies, so the explicit dependency call added for the Wasm LDVIRTFTN lowering is removed. JIT: remove the Wasm carve-out in impImportLdvirtftn. Co-authored-by: Copilot App <[email protected]>
|
Azure Pipelines: Successfully started running 5 pipeline(s). 10 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
Contributor
|
Tagging subscribers to this area: @dotnet/crossgen-contrib |
Member
Author
|
Closing. Using the VirtualEntry dynamic helper cell on Wasm adds about 1.5–2 ns to each virtual function pointer lookup. #136769 now gets the cell's dependencies without the cell: the JIT reports each Note This comment was generated with GitHub Copilot. |
lewing
added a commit
that referenced
this pull request
Nov 6, 2026
#136769) On Wasm, crossgen2 never compiled implementations that are reachable only through virtual dispatch from LDVIRTFTN call sites, so they ran in the interpreter. ### Cause For `CORINFO_VIRTUALCALL_LDVIRTFTN`, crossgen2 hands the JIT a dynamic helper cell built from a `VirtualEntry` `MethodFixupSignature`. That signature carries the virtual dispatch dependencies: - `VirtualMethodUse`, which drives `InheritedVirtualMethodsNode` and `ArrayInterfaceMethodsNode`; - `GVMDependencies`; - the cross-module compile dependency. The Wasm JIT doesn't use the cell. It calls `CORINFO_HELP_VIRTUAL_FUNC_PTR` with a method handle instead (`impImportLdvirtftn`), so the dependencies were never added. Wasm lowers every interface call through LDVIRTFTN, so this affected: - interface calls, e.g. `SZArrayHelper` methods for `IList<T>`/`IReadOnlyCollection<T>` over value-type arrays, and `List<T>`/`Dictionary` collections and enumerators; - generic virtual method calls; - delegates created with `ldvirtftn`. ### Change - Extract the `VirtualEntry` dependency rules into `MethodFixupSignature.AddVirtualEntryDependencies`. Non-Wasm behavior is unchanged. - Add a JIT-EE notification, `recordWasmVirtualFunctionPointerLookup(CORINFO_METHOD_HANDLE)`, modeled on `recordWasmManagedCallSig`. - `Lowering::LowerCall` reports each `CORINFO_HELP_VIRTUAL_FUNC_PTR` call that survives to lowering, using the compile-time handle of its `RuntimeMethodHandle` argument. - crossgen2 applies `AddVirtualEntryDependencies` to the reported method, so lookups the JIT removes don't add dependencies. - The notification is a no-op in the VM, NativeAOT and SuperPMI. Using the cell itself (by implementing `DelayLoad_Helper_Obj` on Wasm) was prototyped in #136774 and rejected: each lookup costs ~1.5–2 ns more. No image-format change. ### Testing R2R modes exercised: per-assembly, and composite with CoreLib as an unrooted input. Large version bubble and cross-module inlining were not exercised. - The Wasm `VirtualMethodGenerics` NonGVM and GVM tests now assert the same compiled implementations as the non-Wasm tests; the assertions are shared between them. Both tests failed before this change. - New delegate `ldvirtftn` cases (Test12, Test13). - New composite test covering `SZArrayHelper` instantiations, on all targets. - `ILCompiler.ReadyToRun.Tests` passes on browser-wasm and osx-arm64. ### Size and performance Browser `System.Private.CoreLib.wasm`, built with the standard build flags including MIBC, on the same base: | | Raw | |---|---| | Without this change | 18,396,756 | | With this change | 18,747,603 (+350.8 KB, +1.9%) | Brotli grows by about 25–31 KB (+0.7–0.9%). Brotli-11 size varies by about ±3 KB between rebuilds of identical sources, so the range is approximate. Microbenchmark: interface calls from R2R code over `int[]`, run with `corerun` on Node/V8. | Call | Before | After | |---|---|---| | `IReadOnlyCollection<int>.Count` (already compiled; control) | 1.8 ns | 1.8 ns | | `IList<int>[i]` | ~50 ns | 1.9 ns | | `ICollection<int>.Contains` | ~80 ns | 7.5 ns | | `IList<int>.IndexOf` | ~77 ns | 8.0 ns | Relates to #135345 > [!NOTE] > This PR description was generated with GitHub Copilot. --------- Co-authored-by: Copilot App <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Important
Depends on #136769: the first two commits are that PR. Only the last commit (
[r2r][wasm] Use the VirtualEntry dynamic helper cell for LDVIRTFTN) is new here. Opened as a draft for design feedback from the Wasm and ReadyToRun owners.Summary
On CoreCLR WebAssembly, ReadyToRun code now uses the
VirtualEntrydynamic helper cell (CORINFO_HELP_READYTORUN_VIRTUAL_FUNC_PTR) forLDVIRTFTN, as every other ReadyToRun target does. Today it callsCORINFO_HELP_VIRTUAL_FUNC_PTR(obj, methodHandle, classHandle)instead. To support this, the change implementsDelayLoad_Helper_Obj, which was aPORTABILITY_ASSERTstub.Since #133146, Wasm routes all interface calls, array interface methods and generic virtual methods through
LDVIRTFTN. So this path covers every interface dispatch in Wasm R2R code.Background
DelayLoad_*helper as aPORTABILITY_ASSERTstub.DelayLoad_Helperand precompiled.Sdynamic helpers for generic lookups only.VirtualEntryand the other object-taking fixups stayed under#ifndef TARGET_WASM.Because nothing referenced the cell, its
VirtualEntrydependencies were lost. #136769 adds them explicitly. With the cell in use, the cell'sMethodFixupSignaturecarries the dependencies again, and this change removes that explicit call.Design
impImportLdvirtftn)thisasWellKnownArg::ThisPointer, withGTF_CALL_NULLCHECK, because Wasm has no hardware null faults. Shared code whose exact context needs a runtime lookup still usesCORINFO_HELP_VIRTUAL_FUNC_PTR, as on other targets.WasmImportThunkPortableEntrypoint)DelayLoad_Helper_Obj+VirtualEntryimport is called as(sp, obj, pe) -> nint. It uses the fixed signature already used for generic lookups, not the target method's signature. The check keys on the helper ID: #133146's[VSD] DelayLoad_MethodCallimports also carryVirtualEntrysignatures and must keep the target signature.CorInfoImpl.ReadyToRun.cs)AddVirtualEntryDependenciescall from the WasmLDVIRTFTNlowering. The helper itself stays, becauseMethodFixupSignatureuses it.DelayLoad_Helper_Obj)DelayLoad_Helper, but passesDynamicHelperFrameFlags_ObjectArgso the object in the transition block is GC-reported. WhenDynamicHelperWorkerreturns NULL, it has stored the result in argument slot 0. This always happens for reliable fixups in_DEBUG, and after a swallowed exception. The wrapper then returns a static portable entrypoint forDynamicHelper_ReturnFirstArg, so the generic import thunk needs no special case.DynamicHelperFixup)VirtualEntryis enabled on Wasm. Wasm cannot generate code at runtime, so instead ofCreateHelperWithArg/CreateReturnConstthe cell resolves to loader-heap data{functionIndex, target, classHandle, methodHandle}. The first word selects a precompiled.Sstub with signature(sp, obj, pe) -> nint..Sstubs)DynamicHelper_VirtualEntry_CallHelpercallsVirtualDispatchHelpers.VirtualFunctionPointer_Dynamic(obj, ref args), with the args stored inline in the data block.DynamicHelper_VirtualEntry_ReturnConsthandles non-vtable targets. Both targets are made callable from R2R code withEnsurePortableEntryPointIsCallableFromR2R.Measurements
Environment: Node 26.4 on Apple Silicon, Release runtime. For the A/B, a temporary JIT knob (removed before commit) toggled only the
LDVIRTFTNlowering. CoreLib and a microbenchmark were crossgen'd with the same crossgen2 and run on the same runtime binary. The receiver changes every iteration; otherwise both paths are hoisted out of the loop. Values are ns per iteration (median of 3 processes, best of 7 runs each).G<int>G<string>IList<int>overint[]ldvirtftn+ delegate ctorcall_indirecthop through the stub before the same managedGenericCachelookup (VirtualFunctionPointer_Dynamicinstead ofVirtualFunctionPointer). A CPU profile confirms the stub andVirtualFunctionPointer_Dynamicrun in the cell build.call_indirect, instead of 3 cell loads and a 5-argumentcall_indirect.SZArrayHelper.GetEnumerator<T>instantiations,List<KeyValuePair<__Canon,__Canon>>enumerator members andValueTupleIValueTupleInternal.ToStringEndimplementations.getCallInfotime, including for callsites the JIT later devirtualizes or drops, while the cell only roots callsites present in the final code.Validation
ILCompiler.ReadyToRun.Tests, full suites:dotnet build <csproj> -c Release -p:TargetOS=browser -p:TargetArchitecture=wasm, thendotnet test): 235 passed, 40 skipped, 0 failed.dotnet build <csproj> -c Release, thendotnet test): 257 passed, 17 skipped, 0 failed.WasmVirtualMethodGenericsNonGVM,WasmVirtualMethodGenericsGVMandVirtualMethodGenericsArrayInterfaceMethods.VirtualEntrycells; the microbenchmark and a null-receiver test pass under corerun on Node.NullReferenceExceptionfor interface, GVM andIList<T>-over-array calls.DynamicHelper_ReturnFirstArgpath. The same tests pass.--opt-cross-module:*and--target-allows-runtime-code-generation:false.ReturnConst(non-vtable) stub;Open questions
Is ~1.5–2 ns per interface dispatch acceptable in exchange for:
Ways to recover the cost:
return_call_indirect). The native runtime build doesn't enable Wasm tail calls today.VirtualFunctionPointer_Dynamicdirectly with the args pointer from the resolved cell. This needs a cell ABI change.Should
DelayLoad_Helper_ObjObj(DelegateCtor) and the other object-taking fixups (IsInstanceOf, ChkCast) move to the same scheme here or separately?Compiler::getVirtMethodPointerTree(tail-call-via-helper) still embeds the type handle; see [browser][coreCLR] R2R: take the method-keyed virtual-dispatch fast path on wasm (replace the #130585 workaround) #132412, item 4.Note
This PR description and the change were generated with GitHub Copilot and reviewed before posting.