victory-simd: shared AVX2 kernels for changed_runs and the DSP convolution, bit-exact, runtime-switched #4471

Closed
github-bot wants to merge 2 commits from gh-pr/4470/head into gh-pr/4470/base
Owner

@⁠kewb on GitHub · 2026-11-30 20:45 UTC · original
kewb:avx2-acceleration → main · +1344 −29 · 20 files · 2 commits · closed without merging

What it does

Adds crates/victory-simd: SIMD kernels shared across crates, each next to the scalar code it must match. Which kernel set runs is chosen once at run time from CPUID and environment switches.

  • victory-gpu shadow::changed_runs: the byte scans (common prefix, then differing prefix) compare 32 bytes per step with AVX2. The runs come out byte for byte the same as the old loop, which stays as the scalar version.
  • victory-hle dsp::convolve: the multiply-accumulate, the FFT stages with at least 8 butterflies per group (twiddles laid out per stage at start-up) and the output scaling use AVX. The output is bit-exact to the scalar code, including NaN, infinities and subnormals, because each lane does the same operations in the same order. FMA is opt-in and off by default.
  • Switches (read once, documented in docs/simd.md):
    • VICTORY_SIMD=auto|baseline|avx2: baseline runs every kernel of this crate on its scalar version.
    • VICTORY_SIMD_MEMORY=0 and VICTORY_SIMD_AUDIO=0 turn off one area each.
    • VICTORY_SIMD_FMA=1 turns on fused multiply-add in the convolution.
    • VICTORY_SIMD_VERIFY=1 runs the scalar version beside each vector kernel and warns at the first difference.
  • Startup log: one SIMD kernels line listing the CPU's extensions and the choices made.

The existing hand-written AVX2 (safecopy, torn, shadow word runs, indirect) is left alone. What was left out on purpose, and why, is in docs/simd.md.

Also in this PR: CI's clippy steps are green again

Both cargo clippy gates fail on main (e1251c31). This PR fixes them:

  • Shadow::ranged and copystat's mb had no callers.
  • Fft::run is now test-only.
  • The relay test uses is_multiple_of.
  • xmem uses unwrap_or_default, with the same behaviour.
  • Lowered::loop_caps is test-only plumbing that no test reads yet, so it gets an allow with a reason.

Relation to #4434

Both PRs change convolve.rs, so they will conflict. #4434 changes the algorithm (real-input transforms, about half the work) and makes the low bits of the output differ. This PR keeps the algorithm and the output bits, adds runtime switches and a verify mode, and also covers changed_runs. The two ideas can be combined: the real-input packing could run on these kernels.

How it was tested

  • Clippy: both CI gates pass (--all-targets, with and without --all-features, -D warnings).
  • Kernel tests: cargo test -p victory-simd covers the dispatch rules and compares every kernel with its scalar version. That means every length up to a few vectors, unaligned starts, a difference at every position, and special float values.
  • Bit-exactness: cargo test -p victory-hle --lib convolve checks both FFT directions and a convolution over several blocks, bit for bit, on both kernel sets. cargo test -p victory-gpu --lib changed_runs checks that the runs are the same.
  • Benchmarks (#[ignore = "benchmark"]), not run for this PR:
    • Convolution block, scalar against vector, at 1, 46 and 187 response blocks: cargo test --release -p victory-hle --lib block_time -- --ignored --nocapture
    • The byte scans over 64 KiB: cargo test --release -p victory-simd --test memory scan_rate -- --ignored --nocapture
    • No speed-up is claimed.
  • Rest of the suite: cargo test --workspace --no-fail-fast fails in files this PR does not change:
    • the asset policy test (a 560-character hex run in victory-shader/src/native.rs)
    • thin::bindings_record_no_work
    • shader_cache::every_switch_of_the_translator_is_in_the_key
    • one GPU-dependent shader test
    • an xmem timing test that passes alone
  • In the game: a release build of this branch (before the squash), run with VICTORY_SIMD_VERIFY=1 on a CPU with AVX2/FMA/BMI2.
    • The startup line showed the vector kernels on for both memory and audio.
    • Verify reported no difference in about 7 minutes of play.
    • The run then ended in a GPU device removal (0x887A0006) while nothing of ours was running on the card. It is not yet known whether that is related. The next check is a run with VICTORY_SIMD=baseline.

🤖 Generated with Claude Code

**@⁠kewb** on GitHub · 2026-11-30 20:45 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/4470) `kewb:avx2-acceleration` → `main` · +1344 −29 · 20 files · 2 commits · closed without merging ## What it does Adds `crates/victory-simd`: SIMD kernels shared across crates, each next to the scalar code it must match. Which kernel set runs is chosen once at run time from CPUID and environment switches. - **`victory-gpu` `shadow::changed_runs`**: the byte scans (common prefix, then differing prefix) compare 32 bytes per step with AVX2. The runs come out byte for byte the same as the old loop, which stays as the scalar version. - **`victory-hle` `dsp::convolve`**: the multiply-accumulate, the FFT stages with at least 8 butterflies per group (twiddles laid out per stage at start-up) and the output scaling use AVX. The output is **bit-exact** to the scalar code, including NaN, infinities and subnormals, because each lane does the same operations in the same order. FMA is opt-in and off by default. - **Switches** (read once, documented in `docs/simd.md`): - `VICTORY_SIMD=auto|baseline|avx2`: `baseline` runs every kernel of this crate on its scalar version. - `VICTORY_SIMD_MEMORY=0` and `VICTORY_SIMD_AUDIO=0` turn off one area each. - `VICTORY_SIMD_FMA=1` turns on fused multiply-add in the convolution. - `VICTORY_SIMD_VERIFY=1` runs the scalar version beside each vector kernel and warns at the first difference. - **Startup log:** one `SIMD kernels` line listing the CPU's extensions and the choices made. The existing hand-written AVX2 (safecopy, torn, shadow word runs, indirect) is left alone. What was left out on purpose, and why, is in `docs/simd.md`. ### Also in this PR: CI's clippy steps are green again Both `cargo clippy` gates fail on `main` (e1251c31). This PR fixes them: - `Shadow::ranged` and copystat's `mb` had no callers. - `Fft::run` is now test-only. - The relay test uses `is_multiple_of`. - xmem uses `unwrap_or_default`, with the same behaviour. - `Lowered::loop_caps` is test-only plumbing that no test reads yet, so it gets an `allow` with a reason. ## Relation to #4434 Both PRs change `convolve.rs`, so they will conflict. #4434 changes the algorithm (real-input transforms, about half the work) and makes the low bits of the output differ. This PR keeps the algorithm and the output bits, adds runtime switches and a verify mode, and also covers `changed_runs`. The two ideas can be combined: the real-input packing could run on these kernels. ## How it was tested - **Clippy:** both CI gates pass (`--all-targets`, with and without `--all-features`, `-D warnings`). - **Kernel tests:** `cargo test -p victory-simd` covers the dispatch rules and compares every kernel with its scalar version. That means every length up to a few vectors, unaligned starts, a difference at every position, and special float values. - **Bit-exactness:** `cargo test -p victory-hle --lib convolve` checks both FFT directions and a convolution over several blocks, bit for bit, on both kernel sets. `cargo test -p victory-gpu --lib changed_runs` checks that the runs are the same. - **Benchmarks (`#[ignore = "benchmark"]`), not run for this PR:** - Convolution block, scalar against vector, at 1, 46 and 187 response blocks: `cargo test --release -p victory-hle --lib block_time -- --ignored --nocapture` - The byte scans over 64 KiB: `cargo test --release -p victory-simd --test memory scan_rate -- --ignored --nocapture` - **No speed-up is claimed.** - **Rest of the suite:** `cargo test --workspace --no-fail-fast` fails in files this PR does not change: - the asset policy test (a 560-character hex run in `victory-shader/src/native.rs`) - `thin::bindings_record_no_work` - `shader_cache::every_switch_of_the_translator_is_in_the_key` - one GPU-dependent shader test - an xmem timing test that passes alone - **In the game:** a release build of this branch (before the squash), run with `VICTORY_SIMD_VERIFY=1` on a CPU with AVX2/FMA/BMI2. - The startup line showed the vector kernels on for both memory and audio. - Verify reported no difference in about 7 minutes of play. - The run then ended in a GPU device removal (`0x887A0006`) while nothing of ours was running on the card. It is not yet known whether that is related. The next check is a run with `VICTORY_SIMD=baseline`. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
New crate with runtime-dispatched SIMD kernels and scalar references:
- byte prefix scans, used by shadow::changed_runs (byte-exact)
- complex MAC, FFT butterflies and scaling, used by dsp::convolve
  (bit-exact; FMA opt-in)

Switches: VICTORY_SIMD=auto|baseline|avx2, VICTORY_SIMD_MEMORY,
VICTORY_SIMD_AUDIO, VICTORY_SIMD_FMA, VICTORY_SIMD_VERIFY.
See docs/simd.md. The Cargo.lock change invalidates shader caches once.
- Fft::run is test-only now that the convolution calls run_with.
- Shadow::ranged and copystat's mb had no callers.
- host.rs relay test: is_multiple_of; xmem: unwrap_or_default.
- lower.rs: Lowered::loop_caps is test plumbing nothing reads yet.
- #[ignore = "benchmark"] timings: a convolution block, scalar against
  vector kernels (1, 46 and 187 response blocks), and the byte scans
  behind changed_runs over 64 KiB.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-30 20:47:07 +00:00
github-bot closed this pull request 2026-11-30 20:47:07 +00:00

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No reviewers
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
github/trustno1-coder--VIctory-NVIDIA!4471
No description provided.