GPU: merge shadow write-back bytes in AVX2 vectors with scalar fallback #1865

Open
VlREX wants to merge 4 commits from upstream-pr/1867 into main
VlREX commented 2026-11-24 23:49:40 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by VlREX on 2026-11-24T23:49:40Z.


Vectorize CPU-side GPU shadow write-back merging with AVX2, processing 32 bytes per iteration with runtime CPU detection and scalar fallback. Native C AVX2 equivalent verified against all 16,777,216 possible byte triples and 4,416 alignment/length cases, zero mismatches. Three Rust regression tests included. Native Rust integration and GPU performance benchmarks pending. No new dependencies or game assets. AI-assisted (ChatGPT).

<!-- greyfinch-sync:Marvisak/VIctory:issue:6593801945 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1867). Originally posted by [VlREX](https://github.com/VlREX) on 2026-11-24T23:49:40Z. --- Vectorize CPU-side GPU shadow write-back merging with AVX2, processing 32 bytes per iteration with runtime CPU detection and scalar fallback. Native C AVX2 equivalent verified against all 16,777,216 possible byte triples and 4,416 alignment/length cases, zero mismatches. Three Rust regression tests included. Native Rust integration and GPU performance benchmarks pending. No new dependencies or game assets. AI-assisted (ChatGPT).
catminou4 commented 2026-11-25 00:32:39 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-25T00:32:39Z.

Review: COMMENTED


Static review only: victory-gpu is Windows-only (D3D12/windows_sys) — nothing compiles or runs on macOS; source review of the diff.

Shadow write-back bytes merged in AVX2 (docs+gpu): SIMD merge of shadow ranges — pairs with #1382's batched barriers.

<!-- greyfinch-sync:Marvisak/VIctory:review:6049696360 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1867#pullrequestreview-6049696360). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-25T00:32:39Z. **Review:** COMMENTED --- Static review only: victory-gpu is Windows-only (D3D12/windows_sys) — nothing compiles or runs on macOS; source review of the diff. Shadow write-back bytes merged in AVX2 (docs+gpu): SIMD merge of shadow ranges — pairs with #1382's batched barriers.
upipibi commented 2026-11-25 01:07:35 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by upipibi on 2026-11-25T00:10:55Z.


Ran on Windows 11 with an RTX 3070 (Ampere, driver 616.56), debug build, at eabd870: cargo test -p victory-gpu --locked gives 79 passed, 2 failed, 1 ignored. The two failures are the NVIDIA heap-cap ones main also has (descriptor::host::tests::dynamic_slots_are_held_until_released and memory::tests::lists_share_a_table_and_copies_keep_their_views, #1744), so there is nothing new on this hardware. Not run: a title.

AI-assisted (Claude Code).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7148235385 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1867#issuecomment-7148235385). Originally posted by [upipibi](https://github.com/upipibi) on 2026-11-25T00:10:55Z. --- Ran on Windows 11 with an RTX 3070 (Ampere, driver 616.56), debug build, at `eabd870`: `cargo test -p victory-gpu --locked` gives 79 passed, 2 failed, 1 ignored. The two failures are the NVIDIA heap-cap ones `main` also has (`descriptor::host::tests::dynamic_slots_are_held_until_released` and `memory::tests::lists_share_a_table_and_copies_keep_their_views`, #1744), so there is nothing new on this hardware. Not run: a title. AI-assisted (Claude Code).
Marvisak commented 2026-11-25 08:06:31 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T07:18:41Z.


Automated triage for the maintainers.

Needs rebase. This PR no longer merges cleanly into main (conflict in crates/victory-gpu/src/shadow.rs). It will be reviewed again once it is rebased.

From the earlier needs-work note, still open: post before/after copy-back timings from a release build, and update the title, description and test name, which still describe AVX2 vectors.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7155720106 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1867#issuecomment-7155720106). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T07:18:41Z. --- <!-- victory-triage v1 head=ea57fd91c0fb76bdf4b6017ba5d8bf16be2d526c verdict=needs-rebase at=2026-11-25T22:22:40Z --> Automated triage for the maintainers. **Needs rebase.** This PR no longer merges cleanly into `main` (conflict in `crates/victory-gpu/src/shadow.rs`). It will be reviewed again once it is rebased. From the earlier needs-work note, still open: post before/after copy-back timings from a release build, and update the title, description and test name, which still describe AVX2 vectors. --- _Generated by [Claude Code](https://claude.ai/code)_
upipibi commented 2026-11-26 17:46:30 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by upipibi on 2026-11-26T17:46:30Z.


Release-build copy-back timings: std-only bench, bodies copied verbatim, workspace [profile.release]. Ns per call net of a memcpy baseline, median of 41 interleaved samples. Scalar = the loop this PR replaces (70564ae0), PR = merge at ea57fd91.

case (aligned / misaligned) scalar PR speedup
4 KiB 0 % 1197 / 1200 145 / 285 8.3x / 4.2x
4 KiB 1 % 1383 / 1357 146 / 282 9.5x / 4.8x
4 KiB 50 % 2095 / 2085 146 / 287 14x / 7.3x
64 KiB 1 % 23943 / 32050 2502 / 5641 9.6x / 5.7x
64 KiB 50 % 158875 / 146669 3311 / 4624 48x / 32x
1 MiB 0 % 304511 / 303017 47577 / 78692 6.4x / 3.9x
1 MiB 1 % 396556 / 399141 47758 / 78950 8.3x / 5.1x
1 MiB 50 % 2705576 / 2825431 47733 / 80571 57x / 35x

Asm (baseline SSE2): the old loop is only partly vectorized. LLVM does the compares 16 bytes at a time, but it scalarizes the conditional *t = *h into 16 test/branch/byte-store sequences per block. merge compiles to a branch-free pcmpeqb/pand/pandn/por loop.

However, main already has a branch-free merge_writeback (#2054). It runs 2-8 % faster than this PR's merge in 35 of 36 session-case pairs, so after a rebase this PR adds no speed.

A second session kept the ratios, but its absolute times ran up to 64 % higher (loaded machine).

Ryzen 9 5950X with AVX2, rustc 1.98.1, high priority, pinned to one core. Inputs are synthetic.

AI-assisted (Claude Code, Claude Opus 5.5).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7199622295 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1867#issuecomment-7199622295). Originally posted by [upipibi](https://github.com/upipibi) on 2026-11-26T17:46:30Z. --- Release-build copy-back timings: std-only bench, bodies copied verbatim, workspace `[profile.release]`. Ns per call net of a memcpy baseline, median of 41 interleaved samples. Scalar = the loop this PR replaces (70564ae0), PR = `merge` at ea57fd91. | case (aligned / misaligned) | scalar | PR | speedup | |---|---|---|---| | 4 KiB 0 % | 1197 / 1200 | 145 / 285 | 8.3x / 4.2x | | 4 KiB 1 % | 1383 / 1357 | 146 / 282 | 9.5x / 4.8x | | 4 KiB 50 % | 2095 / 2085 | 146 / 287 | 14x / 7.3x | | 64 KiB 1 % | 23943 / 32050 | 2502 / 5641 | 9.6x / 5.7x | | 64 KiB 50 % | 158875 / 146669 | 3311 / 4624 | 48x / 32x | | 1 MiB 0 % | 304511 / 303017 | 47577 / 78692 | 6.4x / 3.9x | | 1 MiB 1 % | 396556 / 399141 | 47758 / 78950 | 8.3x / 5.1x | | 1 MiB 50 % | 2705576 / 2825431 | 47733 / 80571 | 57x / 35x | Asm (baseline SSE2): the old loop is only partly vectorized. LLVM does the compares 16 bytes at a time, but it scalarizes the conditional `*t = *h` into 16 test/branch/byte-store sequences per block. `merge` compiles to a branch-free pcmpeqb/pand/pandn/por loop. However, `main` already has a branch-free `merge_writeback` (#2054). It runs 2-8 % faster than this PR's `merge` in 35 of 36 session-case pairs, so after a rebase this PR adds no speed. A second session kept the ratios, but its absolute times ran up to 64 % higher (loaded machine). Ryzen 9 5950X with AVX2, rustc 1.98.1, high priority, pinned to one core. Inputs are synthetic. AI-assisted (Claude Code, Claude Opus 5.5).
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/1867:upstream-pr/1867'
git switch 'upstream-pr/1867'

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.

git switch 'main'
git merge --no-ff 'upstream-pr/1867'
git switch 'upstream-pr/1867'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/1867'
git switch 'upstream-pr/1867'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/1867'
git switch 'main'
git merge --squash 'upstream-pr/1867'
git switch 'main'
git merge --ff-only 'upstream-pr/1867'
git switch 'main'
git merge 'upstream-pr/1867'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
Marvisak/VIctory!1865
No description provided.