Skip to content

cranelift-wasm: call out_of_gas/new_epoch without clobbering registers - #15507

Open
0xbe7a wants to merge 1 commit into
bytecodealliance:mainfrom
0xbe7a:fuel-epoch-preserve-all
Open

0xbe7a wants to merge 1 commit into
bytecodealliance:mainfrom
0xbe7a:fuel-epoch-preserve-all

Conversation

@0xbe7a

@0xbe7a 0xbe7a commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Fuel and epoch checks currently call out_of_gas and new_epoch through the regular tail-ABI builtin trampoline. Even though those calls are almost never taken, they sit at every function entry and loop header, which forces regalloc to treat all caller-saved registers as clobbered and spill loop-carried values on every iteration.

This PR routes both calls through the preserve_all builtin trampoline (previously only used by breakpoints) and marks the out_of_gas block cold. Because preserve_all calls can't return values, epoch_check_full now reloads epoch_deadline from VMStoreContext after the call instead of using new_epoch's return value (new_epoch already updates that field; Winch is untouched and still uses the regular ABI and return value).

Since this trampoline is no longer breakpoint-specific, I renamed PatchableToBuiltinTrampoline / Abi::Patchable to PreserveAllToBuiltinTrampoline / Abi::PreserveAll while keeping the same encodings. Patchability is still controlled per call site via ExtFuncData::patchable, so only breakpoint sites get NOP'd. Happy to split the rename into a separate PR if preferred. A couple of small cleanups came along with this: builtin trampolines are now deduplicated by FuncKey instead of builtin index, since one builtin can now have two trampolines (normal and preserve_all), and the call-site signature marks vmctx as ArgumentPurpose::VMContext to match the callee.

Everything around traps, safepoints, and async yields works the same as it does for breakpoints today: all backends already implement preserve_all (including saving full 128-bit vector registers on aarch64), and when yielding to the host the saved registers live in the trampoline frame on the fiber stack until resumption.

Benchmarks

We hit this using consume_fuel as a compute limit for libjpeg-turbo and PDFium, where fuel overhead was much higher than the counter updates alone explained, especially on aarch64. Measured with the equivalent change on Wasmtime 51 (same fuel/epoch codegen as main), relative to a no-fuel build:

x86-64 (Zen 5) aarch64 (M3)
libjpeg-turbo entropy decode, cycles +10% → +4% +18% → +5%
same with epochs instead of fuel +12% → +5% +33% → +4%
PDFium photo-page render, wall time +11.6% → +7.8% +21.3% → +14.7%

In libjpeg-turbo's hottest decode loop, stack loads per iteration drop from 88 to 5 on x86-64 and from 13 to 2 on aarch64. aarch64 sees a bigger win because Zen 5 hides much of the spill latency, whereas call-heavy code benefits less since we still store and reload the fuel counter around calls.

Testing

Existing fuel and epoch tests keep very little live across loop headers, so they wouldn't catch a broken preserve_all prologue on the returning paths (fuel refuel or epoch Continue/Yield). I added three tests covering fuel yields, epoch Continue(0), and epoch Yield(0) at every loop header with 16 i64, 16 v128, and 8 f64 values live across the header, checking the result against an uninstrumented run. Because host code between yield and resume doesn't reliably touch all of those registers, the tests also clobber every caller-saved integer and vector register via inline asm on x86-64 and aarch64 before resuming and inside the epoch callback. As a sanity check, skipping vector saves in preserve_all on x86-64 makes all three new tests fail while all 144 existing fuel/epoch/debug tests still pass (and without the host-side clobbering, the fuel test missed it too).

I also added disas tests ({fuel,epoch}-loop-live-values-{x64,aarch64}.wat) to pin the spill reduction—e.g. the aarch64 epoch loop goes from 26 loads and 11 stores down to 10 and 1—and updated existing fuel/epoch expectations for the new signature, cold block, and deadline reload.

Same approach as #14975 which was closed without technical review
Related #4109

@0xbe7a
0xbe7a requested review from a team as code owners December 2, 2026 09:32
@0xbe7a
0xbe7a requested review from cfallin and removed request for a team December 2, 2026 09:32
@0xbe7a 0xbe7a changed the title Call out_of_gas and new_epoch through PreserveAll trampolines cranelift-wasm: call out_of_gas/new_epoch without clobbering registers Dec 2, 2026
@github-actions

github-actions Bot commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Thanks @0xbe7a for your contribution to this repository, but this automated bot is closing this PR.

Bytecode Alliance projects have been overwhelmed with AI-generated PRs that monopolize reviewer time, and this bot is an attempt to push back. Please discuss this change on zulip with maintainers first before jumping straight into making a PR. After discussion, a maintainer may reopen this PR.

AI usage must abide by the Bytecode Alliance's AI Tool Use Policy, and we ask that new contributors engage with maintainers beforehand as humans to ensure that what you're working on is reasonable for a newcomer to tackle.

For more information on this bot, see the contributing docs.

If you believe this PR was caught by this bot erroneously, reach out to us on zulip.

@github-actions github-actions Bot closed this Dec 2, 2026
@0xbe7a

0xbe7a commented Dec 2, 2026

Copy link
Copy Markdown
Contributor Author

@alexcrichton

Copy link
Copy Markdown
Member

/vouch

seems like a reasonable feature, implementation, and presented well enough, so reopening.

@alexcrichton alexcrichton reopened this Dec 2, 2026
@cfallin

cfallin commented Dec 2, 2026

Copy link
Copy Markdown
Member

/vouch

Author has talked with us on Zulip, and I'm willing to review.

@cfallin

cfallin commented Dec 2, 2026

Copy link
Copy Markdown
Member

(oh, I missed Alex doing the same -- in any case, halfway through reviewing now)

@cfallin cfallin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks!

I'll echo what Alex said on Zulip that it'd be great to see a Sightglass run for both fuel vs baseline and epochs vs baseline, before and after this change; e.g. the pca-20-min.suite suite. That's our canonical way of measuring perf of Wasm execution.

/// Runs `live_values_across_loop_header_wat` with an epoch deadline that is
/// always reached, so every loop header calls `new_epoch` and returns into
/// the loop with all the live values; the checksum must not change.
async fn epoch_preserves_values_live_across_loop_header(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I appreciate the intention here but I think the existing epoch tests should be fine to catch any issues (along with all other uses of preserve_all -- you're basically just testing that the ABI works properly), so we can probably remove both this and the below tests, and the new tests in fuel.rs too (which are... impressive... with the inline assembly to actually clobber all, but again redundant wrt our existing runs).

/// `PreserveAll` trampoline clobbers nothing, so values stay in whatever
/// registers they are in. The trampoline returns nothing and raises the
/// builtin's trap itself.
pub(crate) fn out_of_gas_preserve_all(&mut self, func: &mut Function) -> ir::FuncRef {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is there any reason that we add new libcalls with _preserve_all suffixes, rather than just changing the ABI for the libcall (as we do for breakpoint)?

Comment on lines -1665 to +1666
Abi::Patchable => unreachable!(
"We should not be compiling a patchable-ABI trampoline for a component function"
Abi::PreserveAll => unreachable!(
"We should not be compiling a preserve_all-ABI trampoline for a component function"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Refactorings like this FWIW are generally fine to include in PRs, but where possible we prefer them to be separated out commit-wise to make review easier. No problem this time, but if you're interested in making future PRs we'd appreciate that.

/// `PreserveAll` trampoline clobbers nothing, so values stay in whatever
/// registers they are in. The trampoline returns nothing and raises the
/// builtin's trap itself.
pub(crate) fn out_of_gas_preserve_all(&mut self, func: &mut Function) -> ir::FuncRef {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@cfallin question for you, did you have a particular reason for doing this style of getting the breakpoint intrisnic vs an alternative approach along the lines of https://github.com/bytecodealliance/wasmtime/pull/14975/changes#diff-f4e65f6eab2ed0d2568b98f7090476dab5cf37bbf7e7d5ef10a6a2ca9e97c33e where internally libcalls are special-cased?

Personally I'm partial to the latter approach as it makes so all usage of the libcall is guaranteed to have a single ABI as opposed to accidentally using the wrong helper to get the wrong ABI. Even more ideally I'd love like a #[abi_preserve_all] on the builtins macro itself so we can just annotate some libcalls over others, but that might be a bit overkill to parse when it's only needed in one location here.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It's ok to skip the tests in this file. These aren't erally adding much and the tests themselves are pretty complicated

Comment thread tests/all/fuel.rs

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Similar to the previous file, the tests here are pretty overkill to go so far as to use inline assembly to forcibly clobber things. I think it's ok to primarily rely on preexisting functional tests and disas/*.wat for this change

@alexcrichton

Copy link
Copy Markdown
Member

(er sorry for the race!)

@github-actions github-actions Bot added the wasmtime:api Related to the API of the `wasmtime` crate itself label Dec 2, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

wasmtime:api Related to the API of the `wasmtime` crate itself

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants