Skip to content

Conversation

@AndyAyersMS
Copy link
Member

Determine how to emit Wasm control flow from the JIT's control flow graph.

Relies on loop-aware RPO to determine the block order. Currently only handles the main method. Assumes irreducible loops have been fixed upstream (which is not yet guaranteed; bails out if not so).

Doesn't actually do any emission, just prints a textual description in the JIT dump (along with a dot markup version).

Uses only LOOP and BLOCK. Tries to limit the extent of BLOCK.

Run for now as an optional phase even if not targeting Wasm, to do some stress testing.

Contributes to #121178

Determine how to emit Wasm control flow from the JIT's control flow graph.

Relies on loop-aware RPO to determine the block order. Currently only
handles the main method. Assumes irreducible loops have been fixed
upstream (which is not yet guaranteed; bails out if not so).

Doesn't actually do any emission, just prints a textual description in
the JIT dump (along with a dot markup version).

Uses only LOOP and BLOCK. Tries to limit the extent of BLOCK.

Run for now as an optional phase even if not targeting Wasm, to
do some stress testing.

Contributes to dotnet#121178
@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 Nov 6, 2025
@AndyAyersMS
Copy link
Member Author

AndyAyersMS commented Nov 6, 2025

FYI @dotnet/jit-contrib @SingleAccretion a first cut at wasm control flow. Eventually the text emission will end up being part of emitwasm (and in binary, with text disasm) and not part of this phase.

Left: sample control flow graph. Black are Wasm blocks, red are Wasm loops.

Right: sample Wasm instruction stream (block contents not shown, just BBxx).

image (49) BB01
FALLTHROUGH

BLOCK (13)
BLOCK (12)
BLOCK (8)
BLOCK (4)
BB04
BR_IF 0 (4)
FALLTHROUGH

LOOP (4)
BB33
BR_IF 2 (8)
FALLTHROUGH

BB07
BR_IF 0 (2)be
FALLTHROUGH

END (4) LOOP
END (4)
BLOCK (7)
BB09
BR_IF 0 (7)
FALLTHROUGH

BB10
BR_IF 1 (8)
FALLTHROUGH

BB12
FALLTHROUGH

END (7)
BB19
BR 1 (12)

END (8)
BB21

BB22
FALLTHROUGH

BB16
FALLTHROUGH-inv
BR_IF-inv 1 (13)

BB30
FALLTHROUGH

END (12)
BB02
RETURN

END (13)
BB29
THROW

@AndyAyersMS
Copy link
Member Author

The

FALLTHROUGH-inv
BR_IF-inv 1 (13)

above is a reminder we need to invert the branch condition (not yet done).

@AndyAyersMS
Copy link
Member Author

Think this is ready for review. There could still be bugs in the control flow, but hard to be sure until we can actually generate code.

Errors seem to be all known or timeouts.

@AndyAyersMS AndyAyersMS marked this pull request as ready for review November 6, 2025 23:48
Copilot AI review requested due to automatic review settings November 6, 2025 23:48
Copy link
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 adds a new experimental Wasm control flow simulation phase to the JIT compiler. The phase analyzes the control flow graph and simulates how Wasm BLOCK/END and LOOP/END control structures would be emitted for the method being compiled.

  • Introduces a new fgWasmControlFlow phase that transforms the control flow graph into Wasm-style nested control structures
  • Adds a JitWasmControlFlow configuration option to enable/disable this experimental feature (DEBUG builds only)
  • Implements an interval-based algorithm to properly nest blocks and loops according to Wasm requirements

Reviewed Changes

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

Show a summary per file
File Description
src/coreclr/jit/jitconfigvalues.h Adds the JitWasmControlFlow configuration integer to control the new phase
src/coreclr/jit/fgwasm.cpp New file implementing the Wasm control flow analysis and simulation algorithm
src/coreclr/jit/compphases.h Registers the PHASE_WASM_CONTROL_FLOW phase
src/coreclr/jit/compmemkind.h Adds CMK_Wasm memory kind for allocations
src/coreclr/jit/compiler.h Declares the fgWasmControlFlow method
src/coreclr/jit/compiler.cpp Invokes the new phase conditionally in DEBUG builds after loop alignment
src/coreclr/jit/CMakeLists.txt Adds fgwasm.cpp to the build sources

Co-authored-by: Copilot <[email protected]>
@AndyAyersMS AndyAyersMS requested a review from a team November 7, 2025 15:31
@AndyAyersMS
Copy link
Member Author

@dotnet/jit-contrib PTAL

@am11 am11 added the arch-wasm WebAssembly architecture label Nov 7, 2025
@dotnet-policy-service
Copy link
Contributor

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

@AndyAyersMS
Copy link
Member Author

@dotnet/jit-contrib ping

@kg
Copy link
Member

kg commented Nov 11, 2025

LGTM but I'll leave the green check to someone who knows the JIT better

@AndyAyersMS
Copy link
Member Author

I should do a run where I enable this in release just to see the TP impact... will do that locally and report back.

@AndyAyersMS
Copy link
Member Author

I should do a run where I enable this in release just to see the TP impact... will do that locally and report back.

Enabling this in release, it has about a 0.5-0.9% TP hit. Worst-case was benchmaks.pgo at 0.92.

Restricting the analysis to running just on methods with optimization (as it will be doing mostly, R2R code is optimized) drops this to 0.55%.

Breaking it down by stages

DFS          0.14%
+ Loops      0.22%
+ LaRPO      0.27%
+ Intervals  0.36%
+ Conflicts  0.50%
+ Sort       0.53%
+ "codegen"  0.55%

so no clear bottleneck.

@AndyAyersMS
Copy link
Member Author

The new phase is now disabled. I will create a wasm test pipeline where we can enable this.

@AndyAyersMS
Copy link
Member Author

/ba-g one test failed with Half-way through waiting for remote process. Memory load: 18

Unrelated

@AndyAyersMS AndyAyersMS merged commit b435b40 into dotnet:main Nov 14, 2025
109 of 112 checks passed
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.

5 participants