Skip to content

Make clearing and restoring the may_leave flag more amenable to opt…#13713

Open
fitzgen wants to merge 1 commit into
bytecodealliance:mainfrom
fitzgen:adapter-may-leave-reset-use-locals
Open

Make clearing and restoring the may_leave flag more amenable to opt…#13713
fitzgen wants to merge 1 commit into
bytecodealliance:mainfrom
fitzgen:adapter-may-leave-reset-use-locals

Conversation

@fitzgen

@fitzgen fitzgen commented Jun 22, 2026

Copy link
Copy Markdown
Member

…imization

The clear-and-restore is now structured so that if translation doesn't actually call any functions (e.g. due to inlining) then a future dead-store elimination pass in Cranelift can remove all the flag juggling entirely (other than trapping when !may_leave):

may_leave = load vmctx+MAY_LEAVE_OFFSET      ;; (0)
trapz may_leave

...

zero = iconst 0
store zero, vmctx+MAY_LEAVE_OFFSET           ;; (1)

...

store may_leave, vmctx+MAY_LEAVE_OFFSET      ;; (2)

First, the dead-store elimination pass will see that the the store at (1): is dead and remove it. Then, the idempotent-store eliminator will recognize that the store at (2) is storing the same value that the memory location already contains and it will also be removed. The more we can reuse locals to make this idempotency obvious, rather than force Cranelift's optimizer to rediscover this information, the better.

…imization

The clear-and-restore is now structured so that if translation doesn't actually
call any functions (e.g. due to inlining) then a future dead-store elimination
pass in Cranelift can remove all the flag juggling entirely (other than trapping
when `!may_leave`):

    may_leave = load vmctx+MAY_LEAVE_OFFSET      ;; (0)
    trapz may_leave

    ...

    zero = iconst 0
    store zero, vmctx+MAY_LEAVE_OFFSET           ;; (1)

    ...

    store may_leave, vmctx+MAY_LEAVE_OFFSET      ;; (2)

First, the dead-store elimination pass will see that the the store at
(1): is dead and remove it. Then, the idempotent-store eliminator will recognize
that the store at (2) is storing the same value that the memory location already
contains and it will also be removed. The more we can reuse locals to make this
idempotency obvious, rather than force Cranelift's optimizer to rediscover this
information, the better.
@fitzgen fitzgen requested review from a team as code owners June 22, 2026 23:29
@fitzgen fitzgen requested review from cfallin and removed request for a team June 22, 2026 23:29
@github-actions github-actions Bot added the wasmtime:api Related to the API of the `wasmtime` crate itself label Jun 23, 2026
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.

1 participant