Skip to content

JIT: fold wasm R2R address constants into load memarg offsets#131353

Merged
AndyAyersMS merged 1 commit into
dotnet:mainfrom
AndyAyersMS:wasm-memarg-fold
Jul 25, 2026
Merged

JIT: fold wasm R2R address constants into load memarg offsets#131353
AndyAyersMS merged 1 commit into
dotnet:mainfrom
AndyAyersMS:wasm-memarg-fold

Conversation

@AndyAyersMS

Copy link
Copy Markdown
Member

Contain a relocatable address constant under a load and emit the relocation in the memarg offset instead of a separate i32.const/i32.add. Saves 814,971 bytes (3.60%) of the System.Private.CoreLib wasm code section.

Contain a relocatable address constant under a load and emit the
relocation in the memarg offset instead of a separate i32.const/i32.add.
Saves 814,971 bytes (3.60%) of the System.Private.CoreLib wasm code
section.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings July 24, 2026 22:56
@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 24, 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 WASM JIT lowering + codegen/emitter pipeline so that certain relocatable address constants (icon handles) can be contained under GT_IND loads and then emitted as a relocation in the wasm load’s memarg offset, avoiding the separate i32.const <reloc>; i32.add sequence.

Changes:

  • Teach Lowering::ContainCheckIndir (WASM) to contain eligible relocatable icon-handle address constants for GT_IND loads (excluding SIMD12 and multiply-used cases).
  • Add emitter helpers to (a) emit the module base (imageBase) and (b) emit a memarg whose offset is a relocatable address constant.
  • Update genCodeForIndir to recognize the contained icon-handle address case and emit imageBase + load(memarg offset=<reloc>).

Reviewed changes

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

File Description
src/coreclr/jit/lowerwasm.cpp Adds containment for relocatable icon-handle addresses under eligible GT_IND loads so codegen can fold them into memarg offsets.
src/coreclr/jit/emitwasm.h Declares new emitter helpers (emitImageBase, emitIns_MemargAddress) used by WASM codegen.
src/coreclr/jit/emitwasm.cpp Implements emitImageBase / emitIns_MemargAddress and switches IF_MEMARG emission to use relocation-aware constant output when needed.
src/coreclr/jit/codegenwasm.cpp Uses the new contained-address pattern in genCodeForIndir to emit imageBase plus a memarg relocation for loads.

@AndyAyersMS

Copy link
Copy Markdown
Member Author

@adamperlin PTAL
fyi @dotnet/wasm-contrib

Handles folding offsets into global loads (where we know overflow is not possible).

Size

System.Private.CoreLib, crossgen2 -O, --targetos:browser --targetarch:wasm:

code section
baseline 22,635,652
this change 21,820,681
-814,971 (-3.60 %)

271,657 sites folded, each saves 3 bytes.

Example

System.Text.DecoderExceptionFallback.CreateFallbackBuffer goes from 83 -> 83 bytes

;; before                                    ;; after
0026  local.get 0                            0026  local.get 0
0028  global.get 1      ;; $imageBase        0028  global.get 1                    ;; $imageBase
002e  i32.const 7385676 ;; reloc             002e  i32.load align=0 offset=7385676 ;; reloc
0034  i32.add
0035  i32.load align=0 offset=0
0038  global.get 1                           0035  global.get 1
003e  i32.const 7292488 ;; reloc             003b  i32.load align=0 offset=7292488 ;; reloc
0044  i32.add
0045  i32.load align=0 offset=0
0048  local.set 3                            0042  local.set 3

Comment thread src/coreclr/jit/lowerwasm.cpp
@AndyAyersMS
AndyAyersMS merged commit 9904543 into dotnet:main Jul 25, 2026
138 checks passed
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