Skip to content

Raise MAX_SPLITS_PER_SPILLSET from 2 to 4 - #269

Closed
EvanCarroll wants to merge 1 commit into
bytecodealliance:mainfrom
EvanCarroll:split-limit-4
Closed

EvanCarroll wants to merge 1 commit into
bytecodealliance:mainfrom
EvanCarroll:split-limit-4

Conversation

@EvanCarroll

Copy link
Copy Markdown

Raise MAX_SPLITS_PER_SPILLSET from 2 to 4

Fixes #268.

With a limit of 2, a bundle that spans a loop containing calls on x64, where
every vector register is a clobber, is split twice and then reduced to minimal
bundles. All its gaps then live in a spill bundle that crosses the calls, so a
value updated in a call-free inner loop is loaded and stored every iteration.
Four splits are enough to keep it in a register, and the fallback still bounds
the cost on inputs needing O(|bundle|) splits, as intended in #59.

The constant gets a doc comment saying what it trades off.

Testing

  • cargo test: passes.

  • cargo fuzz run ion (checker on), 10 minutes, 6 workers: about 138,600
    inputs, no failure.

  • Cranelift filetests with this patched in ([patch.crates-io] in Wasmtime
    main): all 1378 pass. 5 precise-output tests change, all x64:

    • i128.clif, ishl.clif, sshr.clif, ushr.clif each lose 2–4 lines (moves);
    • fma-call.clif changes register names only (21 lines for 21).

    No test gains a spill: there are 8 stack-referencing lines on each side of the
    diff, all in fma-call.clif.

  • Wasmtime's SIMD spec tests (cargo test --test wast -- simd): pass.

Measurements

i7-1165G7, 8 cores, Linux; Wasmtime main (dcfbea188d) built with regalloc2
0.15.2 at limit 2 against limit 4, everything else equal.

The function from the issue: the inner-loop spill disappears and the
function's stack references drop from 171 to 99. The kernel is 3–12% faster
(table in the issue).

Known pathological inputs: compile time and peak RSS, wasmtime compile
pinned to one core, three runs each:

input limit 2 limit 4
bytecodealliance/wasmtime#4291 extract.wasm 0.37 s, 163 MiB 0.37–0.39 s, 160–162 MiB
#218 example.wasm 0.59–0.60 s, 43–45 MiB 0.60–0.61 s, 42–44 MiB

Compile time: Sightglass, --measure callgrind, compilation phase, one
iteration per engine. Instruction counts are deterministic: two runs of bz2 agree
to 0.001%.

compile instructions, limit 4 against limit 2
geomean, 33 benchmarks (default.suite + bz2, pulldown-cmark, spidermonkey-json, blake3-simd) +0.40%
spidermonkey-json / starlingmonkey-markdown +0.76% / +0.76% (52.7 G / 76.6 G instructions at limit 2)
largest increases shootout-keccak +3.56%, tinygo-regex +1.85%; all others ≤ +0.78%
decreases 6 benchmarks, down to −0.31% (libsodium-siphashx24)

The known pathological inputs, wasmtime compile -C parallel-compilation=n under
callgrind: bytecodealliance/wasmtime#4291 extract.wasm 4,723.7 M → 4,716.4 M
instructions (−0.15%); #218 example.wasm 4,612.4 M → 4,647.7 M (+0.77%). So the
limit stays a bound: neither case gets anywhere near the 10× blow-up the fallback
exists to prevent.

All 33 benchmarks
benchmark limit 2 limit 4 change
libsodium/libsodium-siphashx24 309.4 M 308.4 M -0.31%
libsodium/libsodium-core5 303.5 M 302.9 M -0.21%
libsodium/libsodium-keygen 319.6 M 319.0 M -0.19%
libsodium/libsodium-hash3 336.5 M 336.2 M -0.08%
splay/splay 36.6 M 36.6 M -0.03%
hashset/benchmark 1,266.5 M 1,266.2 M -0.03%
wat-bytecode-interp/wat-bytecode-interp 25.9 M 25.9 M +0.00%
kotlin-richards/kotlin-richards 598.7 M 598.7 M +0.01%
richards/benchmark 112.9 M 112.9 M +0.02%
cm-online-stats/cm-online-stats 54.8 M 54.9 M +0.07%
hoot-earley-boyer/hoot-earley-boyer 8,679.4 M 8,685.8 M +0.07%
rust-protobuf/benchmark 717.1 M 718.1 M +0.14%
java-navier-stokes/java-navier-stokes 276.8 M 277.2 M +0.15%
dart-deltablue/dart-deltablue 570.0 M 571.0 M +0.19%
shootout/shootout-switch 440.7 M 441.6 M +0.21%
ocaml-raytrace/ocaml-raytrace 1,406.4 M 1,409.6 M +0.23%
rust-json/benchmark 1,179.1 M 1,182.0 M +0.24%
hex-simd/benchmark 495.1 M 496.4 M +0.25%
shootout/shootout-heapsort 102.5 M 102.7 M +0.28%
shootout/shootout-memmove 217.1 M 217.9 M +0.37%
pulldown-cmark/benchmark 1,482.5 M 1,488.5 M +0.40%
shootout/shootout-sieve 215.1 M 216.0 M +0.41%
shootout/shootout-xchacha20 232.8 M 233.9 M +0.49%
dotnet-raytracer/dotnet-raytracer 6,033.0 M 6,062.8 M +0.49%
rust-wasmprinter/rust-wasmprinter 3,007.1 M 3,025.0 M +0.60%
blake3-simd/benchmark 360.1 M 362.3 M +0.60%
gcc-loops/benchmark 1,998.8 M 2,011.0 M +0.61%
intgemm-simd/benchmark 1,987.2 M 2,000.8 M +0.68%
spidermonkey/spidermonkey-json 52,745.7 M 53,146.3 M +0.76%
starlingmonkey/starlingmonkey-markdown 76,600.6 M 77,183.9 M +0.76%
bz2/benchmark 773.3 M 779.4 M +0.78%
tinygo/tinygo-regex 4,529.1 M 4,612.8 M +1.85%
shootout/shootout-keccak 337.4 M 349.5 M +3.56%

Execution: Sightglass cycles, --pin, on a machine that was not quiet:
default.suite (29 benchmarks, 5 iterations), bz2/pulldown-cmark/spidermonkey-json
(10) and blake3-simd/hex-simd/intgemm-simd (20) show no significant difference in
execution geomean. The only significant per-benchmark result is hashset, faster
with limit 4 (1.01×–1.35×, p = 0.008). With per-benchmark noise of up to ±20%,
these runs only rule out large execution changes.

So the trade is a small, measurable compile-time cost (+0.40% geomean, +3.56%
worst) for keeping long-lived values in registers in call-free loops (3–12% on the
kernel in the issue). Whether that is the right balance is a judgment for the
maintainers; 3 does not fix the issue's function, 4 is the smallest value that does.

With a limit of 2, a bundle that spans a loop containing calls on x64, where
every vector register is a clobber, is split twice and then falls back to
`split_into_minimal_bundles`. That puts every gap between uses in the spill
bundle, and the spill bundle crosses the calls, so it is given a stack slot:
an accumulator updated in a call-free inner loop is then loaded and stored on
every iteration. Allowing four splits keeps such values in registers in the
inner loop, while still bounding the cost on inputs that need O(|bundle|)
splits, which is what the fallback was added for in bytecodealliance#59.
@EvanCarroll

Copy link
Copy Markdown
Author

This PR is not good. I either need to find out why this patch works so well in my use case. Or find out why it fails so badly in the other code here. For those reasons for the time being, I'm withdrawing, but I do think the bug report is solid.

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.

A bundle split twice around call clobbers spills in the call-free loops it spans

1 participant