Skip to content

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

Open
VlREX wants to merge 4 commits into
Marvisak:mainfrom
VlREX:perf/avx2-shadow-merge
Open

VlREX wants to merge 4 commits into
Marvisak:mainfrom
VlREX:perf/avx2-shadow-merge

Conversation

@VlREX

@VlREX VlREX commented Nov 24, 2026

Copy link
Copy Markdown

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).

@upipibi

upipibi commented Nov 25, 2026

Copy link
Copy Markdown

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).

@catminou4 catminou4 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Marvisak commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

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

@upipibi

upipibi commented Nov 26, 2026

Copy link
Copy Markdown

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 (70564ae), PR = merge at ea57fd9.

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants