Skip to content

[Wasm/Ryujit] Fix if stack depth; fix pep call ordering#128242

Merged
AndyAyersMS merged 3 commits into
dotnet:mainfrom
AndyAyersMS:FixWasmJumpDepthUnderIf
May 17, 2026
Merged

[Wasm/Ryujit] Fix if stack depth; fix pep call ordering#128242
AndyAyersMS merged 3 commits into
dotnet:mainfrom
AndyAyersMS:FixWasmJumpDepthUnderIf

Conversation

@AndyAyersMS
Copy link
Copy Markdown
Member

Account for additional control flow stack depth added during codegen when using if/end.

Fix argument eval ordering for PEP calls so the PEP is pushed as the last arg, and then pushed and indirected to get the function index.

Account for additional control flow stack depth added during codegen
when using `if/end`.

Fix argument eval ordering for PEP calls so the PEP is pushed as the
last arg, and then pushed and indirected to get the function index.
Copilot AI review requested due to automatic review settings May 15, 2026 02:25
@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 May 15, 2026
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 updates the Wasm RyuJIT backend to better handle Wasm control-flow depth for generated if/end blocks and to adjust Portable Entry Point indirect-call lowering so the PEP argument is evaluated before the final call target load.

Changes:

  • Adds Wasm if/end emission helpers that track extra control-flow depth for branch depth calculation.
  • Replaces several direct if/end emissions with the new helpers.
  • Reorders PEP call target lowering so the indirect call target is pushed immediately before the call.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
src/coreclr/jit/lowerwasm.cpp Reorders PEP control-expression load placement before the call.
src/coreclr/jit/codegenwasm.cpp Tracks extra Wasm control-flow depth and routes generated if/end through helpers.
src/coreclr/jit/codegen.h Adds Wasm codegen state and helper declarations.

Comment thread src/coreclr/jit/codegenwasm.cpp Outdated
@am11 am11 added the arch-wasm WebAssembly architecture label May 15, 2026
@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.

@AndyAyersMS
Copy link
Copy Markdown
Member Author

FYI @dotnet/jit-contrib
@kg PTAL

For a PEP call we were emitting the control target before the pep instead of after. For late-introduced control flow we were not accounting for the extra depth.

@davidwrighton this should fix the two issues you were seeing.

Seems like we should be able to use a wasm local for the pep / target instead of storing it on the shadow stack, but will tackle that later. Eg the sequence is now

 ;; set up shadow stack addr
IN0020: 000050      local.get 0
IN0021: 000052      i32.const 8      ;; V04
IN0022: 000054      i32.add    

;; fetch pep     
IN0023: 000055      global.get 1
IN0024: 000057      i32.const_address 4611686018431713328
IN0025: 00005D      i32.add
IN0026: 00005E      i32.load 0 0

;; save pep to shadow stack
IN0027: 000061      i32.store 0 0

;;; set up pep arg
IN0028: 000064      local.get 0
IN0029: 000066      i32.load 0 8      ;; V04

;; set up function index
IN002a: 000069      local.get 0
IN002b: 00006B      i32.load 0 8      ;; V04
IN002c: 00006E      i32.load 0 0

;; call
IN002d: 000071      call_indirect 4611686018431713336 0      ;; CORINFO_HELP_OVERFLOW

and it could be simply

;; fetch pep     
IN0023: 000055      global.get 1
IN0024: 000057      i32.const_address 4611686018431713328
IN0025: 00005D      i32.add
IN0026: 00005E      i32.load 0 0

;; save it to temp and leave on stack (sets up pep arg)
                    i32.tee $temp

;; set up function index

                    local.get $temp
                    i32.load

;; call
                    call_indirect ....

Copilot AI review requested due to automatic review settings May 16, 2026 15:02
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 3 out of 3 changed files in this pull request and generated 1 comment.

Comment thread src/coreclr/jit/lowerwasm.cpp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

arch-wasm WebAssembly architecture 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.

4 participants