Repository navigation
kwemu: branch directly to forward targets within a JIT region - #96
Open
SukharevAndrey wants to merge 1 commit into
Open
SukharevAndrey wants to merge 1 commit into
SukharevAndrey wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
In the baseline JIT, a branch to a later block in the same region sets a member index and goes through the region's
br_tableto reach a target already known at compile time. Emit a Wasmbrto the target'scheckedEntryafter synchronizing registers. Backward branches use the region’s dispatch loop and self-branches use the block's own loop.The direct branch reaches the same checks for the decoded operation index, executable TLB generation, instruction budget, pending interrupts and SMP code epoch. In an instrumented sort, 85.1% of transitions between distinct blocks in a region went forward. In a separate instrumented sort, direct branches avoided 64.7% of executions of the region’s
br_table. This percentage also counts entries from outside the region, which still use the table.Benchmarks
2026-11-29, Apple M1 Max, Node 24.21.0, Alpine 3.24.4 on Linux 7.3.4. The timed emitter computed the target label from the nesting depth. This patch records the same label at emission. Each Linux CPU configuration used eight baseline/direct-branch pairs and six A/A pairs (baseline on both sides). The time columns show separate medians. The last column gives the median change within pairs (negative is faster).
In the Linux guest, sort finished sooner in 31 of 32 comparisons. Across sort configurations the A/A medians ranged from −2.0% to +1.0%. On the repeat archive pass, the direct-branch build finished sooner in only 2/8 pairs on one CPU and 1/8 on two. The median changes (+0.8% and +0.9%) were within the spread of the A/A runs; the archive showed no clear speedup. The kernel configuration and Linux objects matched within each pair. All 224 command results in 56 boots matched independent checksums.
The SQLite 3.50.4 ELF workload used 20,000 indexed inserts in one transaction, followed by updates and joins. The direct-branch build finished sooner in 9/10 first passes and all 10 repeat passes. Both builds returned the same query checksum and retired the same number of guest instructions.
To reproduce the sort workload, run
LC_ALL=C seq 100000 | LC_ALL=C sort -r | md5sumtwice per fresh guest, alternate the patch and baseline order, and compare first and repeat passes separately.Verification
The remap regression test compiles two blocks on separate pages into one region and replaces the destination page. The branch must see the replacement instruction, return at the new syscall PC and retire exactly eight instructions across both runs. The replacement instruction sets
x0 = 7. To check the test itself, redirecting the branch pastcheckedEntryinto the destination body made it fail withexpected 7, found 1: the JIT ran the instruction from the replaced page. The same test passed with the branch targetingcheckedEntry. A second test raises an interrupt during compilation and checks the exact stopping point on the forward edge: both source instructions have retired, the PC is at the target and its first instruction has not run.zig build test -Dkwemu-a64-fuzz=20000completed all 55 build steps, including the native and both Wasm SIMD configurations and 77 guest results matched against QEMU 11.0.3.kernel/build.sh kernelbuilt the UP kernel. The CLI ranrdinit=/usr/bin/kwteston that kernel with JIT enabled and again with--no-jit. Both runs powered off successfully with zero failures.