Skip to content

Wasm import thunk corrections - #126973

Merged
davidwrighton merged 8 commits into
dotnet:mainfrom
davidwrighton:WasmImportThunkCorrections
Apr 20, 2026
Merged

davidwrighton merged 8 commits into
dotnet:mainfrom
davidwrighton:WasmImportThunkCorrections

Conversation

@davidwrighton

@davidwrighton davidwrighton commented Apr 15, 2026 •

Copy link
Copy Markdown
Member
  • Rewrite WasmImportThunk generation in terms of the TransitionBlock and the interpreter calling convention
  • Update the ArgIterator in crossgen2 to handle the interpreter calling convention, or at least something pretty close to it.
  • Move the adjustment of the stack pointer global to the DelayLoad_MethodCall function

NOTE: We have not yet handled all of the variation between the interpreter and R2R calling conventions, but this is close enough to make good progress.

…rator to match the ArgIterator behavior in the runtime

- Notably, the ArgIterator in the runtime follows interpreter semantics, so this logic adjusts the way we stash memory into the stack to match the interpreter behavior
- And the WasmImportThunk code now utilizes the ArgIterator to do its layout.
- We may need to reconcile behavior differences between the interpreter/RyuJIT abi, but they are fairly close, so this is probably reasonable to do for now. Alternatively, we can remove the whole GCRefMap infra in favor of a small bit of conservative reporting, and a completely different format.
- Move the save/restore adjustment to the actual helper logic in the runtime
Copilot AI review requested due to automatic review settings April 15, 2026 23:17
@github-actions github-actions Bot added the area-crossgen2-coreclr only use for closed issues label Apr 15, 2026

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.

Pull request overview

This PR updates WASM ReadyToRun import thunk plumbing to better match the interpreter calling convention and the VM’s TransitionBlock expectations, enabling DelayLoad_MethodCall to be implemented on WASM rather than stubbed out.

Changes:

  • Implemented DelayLoad_MethodCall for WASM using a naked wrapper that temporarily adjusts __stack_pointer and delegates to a C++ implementation.
  • Reworked WasmImportThunk to compute argument save/restore offsets via ArgIterator/TransitionBlock, and to pass module-fixup RVA into the helper.
  • Updated WASM32 calling convention modeling in the R2R compiler (TransitionBlock, ArgIterator) and refactored GCRefMapBuilder to share ArgIterator construction.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
src/coreclr/vm/wasm/helpers.cpp Adds WASM implementation of DelayLoad_MethodCall and helper implementation function.
src/coreclr/vm/cgensys.h Adjusts DelayLoad_MethodCall declaration for WASM to match the new C++ signature/return type.
src/coreclr/tools/aot/ILCompiler.ReadyToRun/Compiler/DependencyAnalysis/ReadyToRun/WasmImportThunk.cs Rewrites thunk emission to use TransitionBlock/ArgIterator offsets and pass module-fixup RVA; updates helper dispatch load.
src/coreclr/tools/aot/ILCompiler.ReadyToRun/Compiler/DependencyAnalysis/ReadyToRun/TransitionBlock.cs Updates WASM32 transition block sizing/alignment assumptions for interpreter slot sizing.
src/coreclr/tools/aot/ILCompiler.ReadyToRun/Compiler/DependencyAnalysis/ReadyToRun/GCRefMapBuilder.cs Factors ArgIterator construction into a reusable helper used by both GC map building and WASM thunk emission.
src/coreclr/tools/aot/ILCompiler.ReadyToRun/Compiler/DependencyAnalysis/ReadyToRun/ArgIterator.cs Updates WASM32 argument sizing/alignment and initial stack offset calculations toward interpreter slot conventions.

Add copilot suggestions

Co-authored-by: Copilot <[email protected]>
Copilot AI review requested due to automatic review settings April 16, 2026 23:22

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.

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.

Comment thread src/coreclr/vm/wasm/helpers.cpp Resolved
davidwrighton and others added 3 commits April 16, 2026 16:37
…init

- Align total allocation (args + transition block) to 16 bytes instead
  of only aligning the args portion
- Use interpreter stack slot size (8) instead of pointer size (4) for
  hidden argument offset calculations in ArgIterator
- Initialize m_ReturnAddress to 0 and store caller frame pointer in the
  transition block header

Co-authored-by: Copilot <[email protected]>
- Replace magic constant 8 with StackElemSize(PointerSize) for Wasm32
  initial stack offset
- Use StackElemSize for byref arg stack size in SizeOfFrameArgumentArray
- Align arg stack size to slot size instead of pointer size

Co-authored-by: Copilot <[email protected]>
Copilot AI review requested due to automatic review settings April 17, 2026 00:07

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.

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated no new comments.

@adamperlin

Copy link
Copy Markdown
Contributor

Overall this looks good to me, just a few questions + suggestions for comment clarifications.

Copilot AI review requested due to automatic review settings April 17, 2026 23:53

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.

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.

Comment thread src/coreclr/vm/wasm/helpers.cpp Resolved
@davidwrighton

Copy link
Copy Markdown
Member Author

@jkotas @adamperlin I believe all feedback has been addressed, and build analysis is happy. Could one of you signoff?

@adamperlin adamperlin 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.

I think this looks good to me!

@davidwrighton
davidwrighton merged commit ded86df into dotnet:main Apr 20, 2026
127 of 134 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators May 21, 2026
@pavelsavara pavelsavara added the arch-wasm WebAssembly architecture label Jun 16, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

arch-wasm WebAssembly architecture area-crossgen2-coreclr only use for closed issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants