Skip to content

winch: Simplify constant handling, part 2/N #10989

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

saulecabrera
Copy link
Member

This patch is best reviewed commit by commit.

This is a follow-up to #10888

Scratch registers are used throughout Winch's codebase to handle constant values that cannot be directly encoded in specific instructions. Since the encoding rules are ISA-specific, selecting instructions for these operations often requires scratch registers. Ideally, these temporary registers should be acquired without adding any register pressure to prevent value spilling.

The objective of this change is to make it generally safer to work with scratch registers and prevent accidental clobbering of such registers.

This commit introduces a register allocator for scratch registers. The
objective of this change is to make it generally safer to work with
scratch registers and prevent accidental clobbering of said registers.

This approach also has the advantage that allows for a more natural
abstraction over ISA-dependent scratch register definitions, e.g., we
can easily encode that fact that in aarch64 x16 and x17 are considered
scratch registers, while in x64 Winch's ABI defines a single global
scratch register.
This commit makes use of the scratch register allocator in both the
x64 and aarch64 backends for immediate value loading.

Given that the MacroAssembler is the boundary between ISA-agnostic
code and ISA-dependent code, it seems to be the natural location for
this allocator to live.

The allocator gives exclusive access to a scratch register of a
particular class, through the `Masm::with_scratch` method. Note that
the semantics of this allocator don't involve spilling or any other
form of register availability resolution. If a register is requested
and it's not available, this method will panic.
Even though the entire change doens't contain major funcitonal
changes, a side effect of improving the constant handling in aarch64
is that we perform better instruction selection for instructions that
deal with immediattes, improving the generated code in some cases.
@saulecabrera saulecabrera requested review from a team as code owners June 9, 2025 18:41
@saulecabrera saulecabrera requested review from fitzgen and removed request for a team June 9, 2025 18:41
Copy link
Member

@alexcrichton alexcrichton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This all look sound and good to me, definitely makes handling scratch registers less brittle!

@github-actions github-actions bot added the winch Winch issues or pull requests label Jun 9, 2025
Copy link

github-actions bot commented Jun 9, 2025

Subscribe to Label Action

cc @saulecabrera

This issue or pull request has been labeled: "winch"

Thus the following users have been cc'd because of the following labels:

  • saulecabrera: winch

To subscribe or unsubscribe from this label, edit the .github/subscribe-to-label.json configuration file.

Learn more.

@saulecabrera saulecabrera enabled auto-merge June 9, 2025 22:44
@saulecabrera saulecabrera added this pull request to the merge queue Jun 9, 2025
Merged via the queue into bytecodealliance:main with commit dacd33b Jun 9, 2025
41 checks passed
@saulecabrera saulecabrera deleted the winch-isa-dependent-imm branch June 9, 2025 23:15
saulecabrera added a commit to saulecabrera/wasmtime that referenced this pull request Jun 10, 2025
This commit is a follow-up to
bytecodealliance#10989; it migrates
all the uses of the scratch register to use `Masm::with_scratch`. This
commit also updates the tests that can fail / are ignored, since many
of the bugs were introduced by accidental clobbers to scratch
registers.

Even though this change doesn't introduce any functional changes, the
disassembly changes in aarch64 are due to the usage of x17 as an extra
scratch registers. The updates in x64 dissembly are related to offset changes.
saulecabrera added a commit to saulecabrera/wasmtime that referenced this pull request Jun 10, 2025
This commit is a follow-up to
bytecodealliance#10989; it migrates
all the uses of the scratch register to use `Masm::with_scratch`. This
commit also updates the tests that can fail / are ignored, since many
of the bugs were introduced by accidental clobbers to scratch
registers.

Even though this change doesn't introduce any functional changes, the
disassembly changes in aarch64 are due to the usage of x17 as an extra
scratch registers. The updates in x64 dissembly are related to offset changes.
github-merge-queue bot pushed a commit that referenced this pull request Jun 10, 2025
* winch: Use `Masm::with_scratch` in ISA-agnostic code

This commit is a follow-up to
#10989; it migrates
all the uses of the scratch register to use `Masm::with_scratch`. This
commit also updates the tests that can fail / are ignored, since many
of the bugs were introduced by accidental clobbers to scratch
registers.

Even though this change doesn't introduce any functional changes, the
disassembly changes in aarch64 are due to the usage of x17 as an extra
scratch registers. The updates in x64 dissembly are related to offset changes.

* Use `with_scratch!` in `pop_to_addr`

* Remove dead code

* Apply cargo fmt

* Remove `imports.wast` from ignore list

* Use the new version of `Masm::with_scratch`

* Introduce `Masm::with_scratch_for`

Helper to derive the register class needed for a particular Wasm value type

* Format
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
winch Winch issues or pull requests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants