Skip to content

[Wasm RyuJit] Call Codegen Fixes for R2R#126778

Open
adamperlin wants to merge 19 commits intodotnet:mainfrom
adamperlin:adamperlin/wasm-fix-call-codegen
Open

[Wasm RyuJit] Call Codegen Fixes for R2R#126778
adamperlin wants to merge 19 commits intodotnet:mainfrom
adamperlin:adamperlin/wasm-fix-call-codegen

Conversation

@adamperlin
Copy link
Copy Markdown
Contributor

@adamperlin adamperlin commented Apr 10, 2026

Initially cherry-picked commit from #126662;

This PR:

  • Adds explicit handling in lowering for managed calls using the PEP calling convention to pass the additional PEP parameter.
  • Removes some dead code around handling of WellKnownArg::R2RIndireictionCell from wasm code gen.
  • Adds fixes for helper call codegen to account for the extra level of indirection needed to load a helper call target from a PEP (Portable Entrypoint).

Lowering Transformation Details

The lowering transformation introduces a temp local (call it pep) to hold the PEP, which we then add to a managed call as a final parameter with associated ABI info. We then rewrite the control expr of the call to be *pep. This ensures that, conceptually, any call with an entrypoint of access type PVALUE will be lowered to something roughly like the following, in C notation:

void* pep = *(void**)(<value>);
(*(void**)pep)(arg0, arg1, ..., argN, pep)

davidwrighton and others added 3 commits April 9, 2026 13:44
Copilot AI review requested due to automatic review settings April 10, 2026 23:33
@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 Apr 10, 2026
@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.

@adamperlin adamperlin changed the title [Wasm RyuJit] Call Codegen Fixes [Wasm RyuJit] Call Codegen Fixes for R2R Apr 10, 2026
@adamperlin adamperlin requested review from AndyAyersMS and kg April 10, 2026 23:34
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 adjusts WebAssembly RyuJIT call lowering/emission for ReadyToRun (R2R) so call targets and PortableEntryPoints (PEPs) are loaded through the correct number of indirections when the host exposes symbols as indirection cells (indirection cell -> PEP -> target).

Changes:

  • Add an extra indirection when materializing the R2R indirection-cell argument on WASM so the value passed reflects the PEP rather than the cell itself.
  • Update WASM direct-call lowering for IAT_PVALUE to dereference through indirection cell -> PEP -> target.
  • Update WASM helper-call emission to load the PEP from the indirection cell (for managed helpers) and to load the final call target via an additional dereference.

Reviewed changes

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

File Description
src/coreclr/jit/morph.cpp Adds a WASM-specific extra indirection when preparing the R2R indirection-cell argument so the runtime sees the expected PEP pointer.
src/coreclr/jit/lower.cpp Ensures WASM IAT_PVALUE direct call targets are lowered with an additional dereference to reach the actual dispatch target.
src/coreclr/jit/codegenwasm.cpp Fixes helper-call emission to load the PEP from the indirection cell and to load the indirect call target by dereferencing the PEP.

// Push the call target onto the wasm evaluation stack by dereferencing the PEP.
// Push the call target onto the wasm evaluation stack by dereferencing the indirection cell
// and then the PEP pointed to by the indirection cell.
assert(helperFunction.accessType == IAT_PVALUE);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

See if it's possible to use the new ADDR_REL reloc type for this to avoid the i32.const <address>; i32.load pattern.

@AndyAyersMS
Copy link
Copy Markdown
Member

Maybe a picture would be useful here?

I imagine it something like this: We have an indirection cell that refers to a PEP which refers to a function index, which we then call indirectly.

Comment thread src/coreclr/jit/lower.cpp Outdated
Comment thread src/coreclr/jit/lower.cpp Outdated
Comment thread src/coreclr/jit/lower.cpp Outdated
Comment thread src/coreclr/jit/morph.cpp Outdated
@adamperlin
Copy link
Copy Markdown
Contributor Author

adamperlin commented Apr 13, 2026

Maybe a picture would be useful here?

I imagine it something like this: We have an indirection cell that refers to a PEP which refers to a function index, which we then call indirectly.

This is the right mental model! I am attaching a diagram that I created though of calling a function F from generated code:
1776127500818-892a2a89-e310-4dd1-b6d1-5d38bbee3929_1

@jkotas
Copy link
Copy Markdown
Member

jkotas commented Apr 13, 2026

I am attaching a diagram that I created though of calling a function F from generated code

This picture is missing the part that passes portable entrypoint address (the value fetched from R2R import table) as the hidden argument that it somewhat important detail.

@adamperlin
Copy link
Copy Markdown
Contributor Author

I am attaching a diagram that I created though of calling a function F from generated code

This picture is missing the part that passes portable entrypoint address (the value fetched from R2R import table) as the hidden argument that it somewhat important detail.

Ah I wasn't sure whether to include that bit, I've updated the diagram to include it!

Comment thread src/coreclr/jit/lower.cpp Outdated
Comment thread src/coreclr/jit/morph.cpp Outdated
Comment thread src/coreclr/jit/lower.cpp Outdated
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 5 out of 5 changed files in this pull request and generated 2 comments.

Comment thread src/coreclr/jit/codegenwasm.cpp
Comment thread src/coreclr/jit/lower.cpp Outdated
@adamperlin adamperlin marked this pull request as ready for review April 24, 2026 21:13
Copilot AI review requested due to automatic review settings April 24, 2026 21:13
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 5 out of 5 changed files in this pull request and generated 3 comments.

Comment thread src/coreclr/jit/lower.cpp Outdated
Comment thread src/coreclr/jit/codegenwasm.cpp Outdated
Comment thread src/coreclr/jit/lower.cpp Outdated
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings April 24, 2026 21:48
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 5 out of 5 changed files in this pull request and generated no new comments.

@adamperlin
Copy link
Copy Markdown
Contributor Author

adamperlin commented Apr 27, 2026

Could I get another review when you have a moment @jkotas @jakobbotsch? I've updated the PR based on your feedback to explicitly handle the PEP calling convention transformation in lower. Thinking about this further exposed some dead case handling in Wasm codegen around the handling of the indirection cell arg which will now not be materialized on wasm. This also exposed a bug in the stackifier that I'm currently working through here: #127412
Also cc'ing @kg due to wasm call codegen changes!

@adamperlin adamperlin force-pushed the adamperlin/wasm-fix-call-codegen branch from 5c9ee85 to 3981152 Compare April 27, 2026 17:46
Comment thread src/coreclr/jit/lower.cpp Outdated
Comment thread src/coreclr/jit/codegenwasm.cpp Outdated
Comment thread src/coreclr/jit/morph.cpp Outdated
Copilot AI review requested due to automatic review settings April 27, 2026 20:00
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 1 comment.

Comment thread src/coreclr/jit/lowerwasm.cpp Outdated
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings April 27, 2026 20:46
Comment thread src/coreclr/jit/morph.cpp
Copy link
Copy Markdown
Member

@jakobbotsch jakobbotsch left a comment

Choose a reason for hiding this comment

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

LGTM, feel free to address the ifdef nit in a follow up

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

Labels

area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants