Repository navigation
JIT: use shorter add/sub constant encodings on Wasm - #138666
Open
AndyAyersMS wants to merge 1 commit into
Open
AndyAyersMS wants to merge 1 commit into
AndyAyersMS wants to merge 1 commit into
Conversation
When negating an add or sub constant encodes it in fewer bytes (e.g. 64), emit the opposite operation with the negated constant. Do the same in the crossgen2 Wasm thunk and stub generators, which also no longer add a zero offset. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: bfdc6001-97e4-4f7a-9de1-f9a04223c8ff
|
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. |
Contributor
|
Tagging subscribers to this area: @dotnet/crossgen-contrib |
Member
Author
|
@adamperlin ptal Starting to scrape the bottom of the wasm-opt barrel... |
AndyAyersMS
requested review from
adamperlin
and
a balanced review from Copilot
December 3, 2026 01:45
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues are identified for the WebAssembly constant-arithmetic optimizations.
No open issues 🎉
What changed in this PR
Reduces generated WebAssembly code size in the JIT and AOT stubs and thunks, supporting the size improvements tracked by #135985.
Changes:
- Rewrites integer add/subtract operations when negating the constant produces a shorter signed LEB128 encoding.
- Adds shared constant-arithmetic helpers that also omit operations with zero.
- Uses these helpers in Wasm stubs and interpreter transition thunks.
| File | Description |
|---|---|
| src/coreclr/tools/Common/Compiler/ObjectWriter/WasmInstructions.cs | Adds optimized AddConst and SubConst helpers. |
| src/coreclr/tools/aot/ILCompiler.ReadyToRun/Compiler/DependencyAnalysis/ReadyToRun/WasmUnboxingStubNode.cs | Uses helpers for object and entry-point offsets. |
| src/coreclr/tools/aot/ILCompiler.ReadyToRun/Compiler/DependencyAnalysis/ReadyToRun/WasmR2RToInterpreterThunkNode.cs | Uses helpers for stack and buffer offsets. |
| src/coreclr/tools/aot/ILCompiler.ReadyToRun/Compiler/DependencyAnalysis/ReadyToRun/WasmInterpreterToR2RThunkNode.cs | Uses helpers for frame allocation and argument offsets. |
| src/coreclr/tools/aot/ILCompiler.ReadyToRun/Compiler/DependencyAnalysis/ReadyToRun/WasmImportThunk.cs | Uses helpers for stack adjustments and argument offsets. |
| src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/Target_Wasm/WasmUnboxingStubNode.cs | Uses the addition helper for the unboxing offset. |
| src/coreclr/jit/emitwasm.cpp | Adds shorter constant-encoding rewrites for i32/i64 arithmetic. |
Review effort: Balanced
This was referenced Dec 3, 2026
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Take advantage of LEB128 encoding quirks to save some code size.
For example
i32.const 64is three bytes, buti32.const -64only two. Sox + 64is shorter asx - (-64), andx - 64asx + (-64); the same holds for 8192 and other constants of the form 2^(7k-1).const c; add/subtoconst -c; sub/addwhen-cencodes in fewer bytes.I32.AddConst/I32.SubConsthelpers do likewise, and also optimize out adds or subtracts of zero.Impact:
coreclr_tests): -17,361 bytes, 14,584 contexts improved, none regressed.+ 0sites are gone, saving 6,333 bytes of code (uncompressed; compressed size is essentially unchanged).Contributes to #135985.
Note
This PR description was generated with GitHub Copilot.