GPU: AVX2 fast-path for merge_piece, first_difference, and last_difference #1
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from DexNom/VIctory. Originally posted by altuux on 2026-11-25T15:10:03Z.
Summary
Adds runtime-dispatched AVX2 (32 bytes/iter) fast paths to
merge_piece,first_difference, andlast_differenceincrates/victory-gpu/src/shadow.rs(zero new dependencies, existing scalar/u64fallbacks preserved):merge_piece(AVX2 + skip-unchanged fast path): Skips reading/writingtitleand avoids dirtyingheldcachelines on untouched 32-byte blocks (now == held), reducing DRAM traffic from 5 streams to 2 streams on untouched spans.first_difference&last_difference(AVX2 32 B/iter): Scans 32 bytes per iteration via_mm256_cmpeq_epi8+_mm256_movemask_epi8+trailing_zeros/leading_zeros.difference_teststo cover 31/32/33/64/65/97-byte boundaries and addedmerge_piece_matches_scalar_across_lengthsverifying bit-for-bit agreement withmerge_piece_scalaracross lengths0..4096, including overlapping CPU+GPU writes to the same byte.Benchmark notes (i5-12400)
merge_piecespeedup depends strongly on change density inside the[first..=last]span:first_difference/last_difference: up to ~1.5–2× faster on long matching prefixes/suffixes; neutral when a difference appears early.Imported from DexNom/VIctory. Originally posted by itsonewi on 2026-11-25T16:14:19Z.
Review: COMMENTED
Checked the SIMD paths against the scalar ones they replace in
crates/victory-gpu/src/shadow.rsonmain, and they are equivalent - including the part that is easiest to get wrong._mm256_blendv_epi8(v_n, v_t, eq)takesv_twhere the bytes are equal andv_nwhere they differ, which is exactly the scalar(*t & !wrote) | (*n & wrote)forwrote = 0xffwhenn != h. The skipped all-equal chunk is a no-op for both slices:titlewould be stored its own value andheldits own value.diff_acc |= xor(merged, v_t)is the scalar'sdiffers |= merged ^ *t, so the returned bool keeps meaning "titlechanged", not "the GPU wrote something".first_difference_avx2:movemaskbit k is byte k, so(!mask).trailing_zeros()gives the first differing byte, and thei + 32 <= lenbound means no bit can come from beyond the slice - the classic false positive of a masked tail is not there.last_difference_avx2is symmetric: highest differing byte is31 - (!mask).leading_zeros()on the reversed walk, therpositiontail runs first so ordering is kept, andwhile i >= 32 { i -= 32 }cannot underflow.is_x86_feature_detected!plus#[target_feature(enable = "avx2")]on anunsafe fnis the right shape, and the loads areloadu, so no alignment requirement leaks into the callers.Nit, not a bug:
let len = now.len().min(held.len()).min(title.len())cannot be shorter than the callers' slices by contract, but taking the min is right forzipequivalence, so leave it.Not built or run here (no checkout and no AVX2 machine), so this is a reading of the patch, not a test run; the added
merge_piece_matches_scalar_across_lengthssweep over 0..4096 is what would catch a disagreement anyway.AI use: read the patch and the scalar functions on
mainthrough the GitHub API. No code was written by an agent here and no build was run.Imported from DexNom/VIctory. Originally posted by molodayamama on 2026-11-25T18:54:01Z.
Verified on AMD Ryzen 5 5600 (Zen 3, AVX2) + NVIDIA GeForce RTX 4060 8 GB / Windows 10 22H2 (
19045.5794) / Rust1.98.1(x86_64-pc-windows-msvc):main(2e01ab4)cargo test --locked -p xtask: 4 passed, 0 failed (191 files ok)cargo clippy --workspace --all-targets --locked -- -D warnings(default &--all-features): 0 warningscargo test --locked -p victory-gpu --lib shadow::: 21 passed, 0 failed (includingmerge_piece_matches_scalar_across_lengthsanddifferences_are_found_as_byte_by_byteon Zen 3 AVX2)cargo test --locked -p victory-gpu --libon RTX 4060 (merged ontomain+ Agility SDK1.614.1via #12): 189 passed, 0 failedImported from DexNom/VIctory. Originally posted by andrewDoing on 2026-11-26T23:40:19Z.
Independent release verification at
a902405on Ryzen 7 5700X3D: 131,104 AVX2 length/alignment oracle trials passed forfirst_difference,last_difference, andmerge_piece, covering lengths 0–4096 and offsets 0–31. This is synthetic CPU coverage; no gameplay was tested.Verification was AI-assisted with GPT-6.2 Astra.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.