Skip to content

JIT: cache the wasm image base in a local#131369

Open
AndyAyersMS wants to merge 1 commit into
dotnet:mainfrom
AndyAyersMS:wasm-imagebase-hoist
Open

JIT: cache the wasm image base in a local#131369
AndyAyersMS wants to merge 1 commit into
dotnet:mainfrom
AndyAyersMS:wasm-imagebase-hoist

Conversation

@AndyAyersMS

Copy link
Copy Markdown
Member

Materializing the image base costs 6 bytes each time, since the global index is a padded relocation. When a wasm function needs it 3 or more times, cache it in a local instead.

SPC R2R code section: 21,820,681 -> 21,010,052 (-810,629 bytes, -3.71%).

Materializing the image base costs 6 bytes each time, since the global index is a
padded relocation. When a wasm function needs it 3 or more times, cache it in a
local instead.

SPC R2R code section: 21,820,681 -> 21,010,052 (-810,629 bytes, -3.71%).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings July 25, 2026 14:38
@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 Jul 25, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 6 pipeline(s).
10 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.

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 the WebAssembly JIT backend to optionally cache the module image base in a wasm local (per function/funclet) when it’s used multiple times, reducing repeated global.get materializations and improving code size.

Changes:

  • Track per-funclet counts of relocatable handle constants to estimate repeated image base materializations.
  • Reserve and initialize an extra wasm local for the image base when the use-count threshold is met, and emit local.get for subsequent uses.
  • Plumb a new per-funclet local index (funWasmImageBaseLocalIndex) through funclet initialization, regalloc, prolog generation, and the wasm emitter.

Reviewed changes

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

Show a summary per file
File Description
src/coreclr/jit/regallocwasm.h Adds per-funclet tracking state for counting image base materializations.
src/coreclr/jit/regallocwasm.cpp Counts image base materializations and conditionally reserves a local slot + records its index during allocation.
src/coreclr/jit/flowgraph.cpp Initializes the new wasm image base local index field for all FuncInfoDsc entries.
src/coreclr/jit/emitwasm.h Adds an emitter helper for explicitly reading image base from the global.
src/coreclr/jit/emitwasm.cpp Splits global vs cached-local emission paths for the image base.
src/coreclr/jit/compiler.h Adds funWasmImageBaseLocalIndex to wasm-specific FuncInfoDsc state.
src/coreclr/jit/codegenwasm.cpp Initializes the cached image base local in root/funclet prologs when present.
src/coreclr/jit/codegen.h Declares the new wasm-only prolog helper for image base local initialization.

}

//------------------------------------------------------------------------
// emitImageBase: Emit the module base (imageBase global) onto the stack.
@AndyAyersMS

Copy link
Copy Markdown
Member Author

@adamperlin PTAL
fyi @dotnet/wasm-contrib

We could save even more space (another 300kb or so) by having __image_base be a small indexed fixed local rather than a reloc; if we decide to go that route we'd want to undo this PR.

@SingleAccretion

SingleAccretion commented Jul 25, 2026

Copy link
Copy Markdown
Contributor

__image_base be a small indexed fixed local rather than a reloc; if we decide to go that route we'd want to undo this PR.

I think the work of "code shrinking" in object writer would subsume this change. Hardcoding the global index is also an option in the interim, though less durable in the long term (since it will be made redundant by shrinking).

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