Skip to content

Switch interpreter dispatch to musttail (become) threading#382

Open
kvpanch wants to merge 7 commits into
masterfrom
kvpanch/musttail
Open

Switch interpreter dispatch to musttail (become) threading#382
kvpanch wants to merge 7 commits into
masterfrom
kvpanch/musttail

Conversation

@kvpanch
Copy link
Copy Markdown
Contributor

@kvpanch kvpanch commented May 6, 2026

Replace the central loop in InterpretedInstance::run_impl with direct-threaded dispatch: each handler now ends in become dispatch(self, next_off) and dispatch (inlined) tail-jumps to the next handler. This collapses the per-instruction call/ret pair into a single indirect tail jump, and moves the indirect-branch site from one shared loop to the tail of every handler so the BTB learns per-edge transition patterns.

Requires nightly + #![feature(explicit_tail_calls)]. The recompiler is untouched.

Measured deltas on bench-pinky / bench-prime-sieve / bench-memset / bench-minimal (host: x86_64, best-of-N runs):

Benchmark Baseline Patched Delta
runtime/memset/32 43.66 ms 37.99 ms -13.0%
runtime/memset/64 45.02 ms 37.40 ms -16.9%
runtime/minimal/64 15.60 us 12.07 us -22.6%
runtime/pinky/32 128.05 ms 97.69 ms -23.7%
runtime/pinky/64 122.43 ms 91.45 ms -25.3%
runtime/prime-sieve/32 96.20 ms 60.76 ms -36.8%
runtime/prime-sieve/64 75.67 ms 49.90 ms -34.1%
interpreter runtime mean -20.3%
compiler runtime mean (ctrl) -0.6%

Replace the central loop in `InterpretedInstance::run_impl` with
direct-threaded dispatch: each handler now ends in `become dispatch(self,
next_off)` and `dispatch` (inlined) tail-jumps to the next handler. This
collapses the per-instruction call/ret pair into a single indirect tail
jump, and moves the indirect-branch site from one shared loop to the tail
of every handler so the BTB learns per-edge transition patterns.

Requires nightly + `#![feature(explicit_tail_calls)]`. The recompiler is
untouched.

Measured deltas on bench-pinky / bench-prime-sieve / bench-memset /
bench-minimal (host: x86_64, best-of-N runs):

| Benchmark                    | Baseline   | Patched    | Delta   |
|------------------------------|------------|------------|---------|
| runtime/memset/32            |   43.66 ms |   37.99 ms | -13.0%  |
| runtime/memset/64            |   45.02 ms |   37.40 ms | -16.9%  |
| runtime/minimal/64           |   15.60 us |   12.07 us | -22.6%  |
| runtime/pinky/32             |  128.05 ms |   97.69 ms | -23.7%  |
| runtime/pinky/64             |  122.43 ms |   91.45 ms | -25.3%  |
| runtime/prime-sieve/32       |   96.20 ms |   60.76 ms | -36.8%  |
| runtime/prime-sieve/64       |   75.67 ms |   49.90 ms | -34.1%  |
| interpreter runtime mean     |            |            | -20.3%  |
| compiler runtime mean (ctrl) |            |            |  -0.6%  |
@kvpanch kvpanch requested a review from koute May 6, 2026 15:16
@kvpanch kvpanch force-pushed the kvpanch/musttail branch from e88205f to a9fc0af Compare May 6, 2026 18:27
@kvpanch kvpanch force-pushed the kvpanch/musttail branch from a9fc0af to ee767b7 Compare May 6, 2026 19:24
@kvpanch kvpanch marked this pull request as ready for review May 8, 2026 15:37
@kvpanch
Copy link
Copy Markdown
Contributor Author

kvpanch commented May 12, 2026

@koute gentle ping

Comment on lines +7 to +9
if [ "$(cat /proc/sys/vm/unprivileged_userfaultfd)" != "1" ]; then
echo "1" | sudo tee /proc/sys/vm/unprivileged_userfaultfd > /dev/null
fi
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This shouldn't be here (and it's not needed for interpreter-only tests.)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Yes, because those are recompiler tests.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I see your comment down below to drop some tests. I should be able to remove this line

Comment thread ci/jobs/build-and-test-nightly-musttail.sh Outdated
Comment thread crates/polkavm/Cargo.toml Outdated
Comment thread crates/polkavm/src/interpreter.rs Outdated
@kvpanch
Copy link
Copy Markdown
Contributor Author

kvpanch commented May 19, 2026

@koute gentle ping

Comment thread crates/polkavm/src/interpreter.rs Outdated
Comment thread crates/polkavm/src/interpreter.rs Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants