jiang1997 opened PR #11840 from jiang1997:x64-store-imm-zero-optimization to bytecodealliance:main:
Add support for generating efficient
mov [mem], imminstructions for immediate values, eliminating the need for temporary registers when storing constants.Changes:
- Add
Inst::store_imm()function supporting immediate values for I8/I16/I32/I64 types with proper range checking- Update stack probing to use direct immediate store
- Add comprehensive test coverage for all integer types and edge cases:
- I8: full range (-128 to 127)
- I16: full range (-32768 to 32767)
- I32: full range (i32::MIN to i32::MAX)
- I64: sign-extended i32 range (hardware limitation)
The I64 type is limited to sign-extended i32 values (-2^31 to 2^31-1) due to x86-64 MOV instruction encoding constraints. Values outside this range will panic with a clear error message.
Before: mov r10, 0x42; mov [rsp], r10 (2 instructions, needs register)
After: mov dword ptr [rsp], 0x42 (1 instruction, no register)This addresses the TODO comment in abi.rs about wanting to store immediate values directly to memory without temporary registers.
<!--
Please make sure you include the following information:
If this work has been discussed elsewhere, please include a link to that
conversation. If it was discussed in an issue, just mention "issue #...".Explain why this change is needed. If the details are in an issue already,
this can be brief.Our development process is documented in the Wasmtime book:
https://docs.wasmtime.dev/contributing-development-process.htmlPlease ensure all communication follows the code of conduct:
https://github.com/bytecodealliance/wasmtime/blob/main/CODE_OF_CONDUCT.md
-->
jiang1997 requested wasmtime-compiler-reviewers for a review on PR #11840.
jiang1997 requested alexcrichton for a review on PR #11840.
jiang1997 updated PR #11840.
jiang1997 updated PR #11840.
jiang1997 updated PR #11840.
alexcrichton submitted PR review:
Thanks for the PR! What I might recommend instead though is that we've generally been moving away from
Inst-style helpers to construct instructions with the advent of the x64 assembler crate. The one use case forInst::store_imm, for example, can unconditionally use themovl_mivariant and there's no need for encoding tests or making API design decisions about whether out-of-bounds integers panic or such.What would you think about replacing the store on the stack here with
movl_midirectly, and dropping the newstore_immhelper?
jiang1997 updated PR #11840.
alexcrichton submitted PR review.
alexcrichton has enabled auto merge for PR #11840.
jiang1997 edited PR #11840:
- Switch the probestack store to mov [rsp], 0 via movl_mi, still touching the page without writing %rsp back to memory.
- Updated the corresponding inline probestack filetests.
jiang1997 commented on PR #11840:
@alexcrichton
Hey, just noticed that changing frommov [rsp], esptomov [rsp], 0increases the instruction size from 3 bytes to 7 bytes.
Is this code size increase acceptable? Alternatively, we could keepmov [rsp], rspand just document it in a comment (resolves the TODO while avoiding the size increase).
Last updated: Dec 06 2025 at 06:05 UTC