Skip to content

Wasm: omit frame header for optimized methods that make no calls - #137770

Merged
AndyAyersMS merged 1 commit into
dotnet:mainfrom
AndyAyersMS:wasm-leaf-no-frame
Nov 18, 2026
Merged

AndyAyersMS merged 1 commit into
dotnet:mainfrom
AndyAyersMS:wasm-leaf-no-frame

Conversation

@AndyAyersMS

@AndyAyersMS AndyAyersMS commented Nov 17, 2026 •

Copy link
Copy Markdown
Member

When optimizing a Wasm method with no EH, omit the shadow-stack frame header (virtual IP and function index slots) if codegen cannot emit a call.

Today the no-EH path in fgWasmVirtualIP allocates the frame header whenever the method has any nonempty block. For leaf methods such as Int32.IsPow2 that header is never written or read, but it still forces a dead SP adjustment in the prolog (local.get; i32.const 16; i32.sub; local.set) and a nonzero unwind frame size. A leaf method can never be on the shadow stack during a stack walk: GC, exceptions and unwinding only happen at calls, and null/bounds/divide checks branch to throw-helper blocks, which contain calls.

fgWasmMayEmitCall scans the LIR for anything codegen may turn into a call:

  • GT_CALL, which includes throw-helper block calls (materialized by StackLevelSetter before this phase)
  • GT_STOREIND needing a write barrier (same predicate codegen uses)
  • GT_STORE_BLK other than the native-opcode and loop forms (the CpObj path calls CHECKED_ASSIGN_REF)
  • GT_LCLHEAP, excluded conservatively (Wasm localloc codegen does not call)

The scan only runs when optimizing, so minopts throughput is unaffected. Stack locals still get frame space; only the header goes away. An assert in ensureCurrentFuncIsUnwindable catches any call source the scan misses.

Measurements

SuperPMI, Wasm cross-JIT (-target_os browser -target_arch wasm), Checked asmdiffs; no asserts:

Collection Size delta Contexts improved Regressed
coreclr_tests.crossgen2 -115,606 bytes (-0.04%) 16,518 0
libraries.crossgen2 -47,932 bytes (-0.07%) 6,874 0

Release tpdiff: -0.00% (coreclr_tests), +0.02% (libraries).

Blazor Havit composite R2R (same inputs and Crossgen2; only the JIT differs): code section -7,402 bytes (1,057 methods, 7 bytes each); file -7,392 bytes. Compressed sizes go up slightly (Brotli +5,455, GZip +3,236); likely because the removed sequences were identical across methods and compressed well. Browser startup and walkthrough latency on Havit showed no measurable change, and V8's Wasm inlining decisions were essentially unchanged.

Contributes to #135985 (Prolog/Epilog: drop frame setup for methods that don't need unwinding).

Note

This PR description was drafted with GitHub Copilot.

When optimizing a method with no EH, scan the LIR for calls and stores
that may emit helper calls. Conservatively exclude localloc as well, even
though its Wasm codegen does not emit a call.

When neither is present, omit the virtual IP and function index frame
header. This removes the dead SP adjustment in leaf methods like
Int32.IsPow2 while retaining space for stack locals.

Add an assert in ensureCurrentFuncIsUnwindable to catch call sources the
scan misses.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: bfdc6001-97e4-4f7a-9de1-f9a04223c8ff
@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 17, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 6 pipeline(s).
9 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

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

@AndyAyersMS

Copy link
Copy Markdown
Member Author

@adamperlin PTAL
fyi @dotnet/wasm-contrib

Another uncompressed smaller / compressed larger change, but the smaller Wasm should be beneficial at runtime too.

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.

🟢 Approval recommended

The review reports no active issues with the Wasm frame-header optimization.

No open issues 🎉

What changed in this PR

This PR reduces unnecessary Wasm frame setup in the JIT by omitting the shadow-stack frame header for optimized, non-NativeAOT methods without exception handling when code generation cannot emit calls.

Changes:

  • Adds a conservative LIR scan for explicit calls and operations that may emit helper calls.
  • Uses the scan to omit unused frame headers while preserving stack-local space and existing minopts behavior.
  • Adds an assertion to catch call-emitting paths that lack a frame header.
File Description
src/​coreclr/​jit/​fgwasm.cpp Detects potential calls and conditionally omits the frame header.
src/​coreclr/​jit/​compiler.h Declares the new call-detection helper.
src/​coreclr/​jit/​codegenwasm.cpp Asserts that unwindable main methods have a frame header.

Review effort: Balanced

@AndyAyersMS

Copy link
Copy Markdown
Member Author

/ba-g osx libraries job stopped responding mid build

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.

3 participants