gpu: vectorize merge_writeback with 64-bit word chunks #3079
No reviewers
Labels
No labels
accessibility
area: build
area: formats
area: gpu
area: loader
area: runtime
area: shader
bug
documentation
duplicate
enhancement
good first issue
gpu: amd
gpu: intel
gpu: nvidia
help wanted
host: wine
invalid
out of scope
question
request
run report
triage: confirmed
triage: has patch
triage: merge candidate
triage: needs info
triage: needs rebase
triage: needs work
triage: proposed close
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Marvisak--VIctory!3079
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-3079"
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?
@AdvaitPalve11 on GitHub · 2026-11-26 06:52 UTC · original
AdvaitPalve11:perf/gpu-shadow-writeback→main· +29 −1 · 1 files · 2 commits · openProblem:
In console UMA-to-discrete shadow buffer synchronization (crates/victory-gpu/src/shadow.rs), merge_writeback is called after every GPU submission to scan altered title memory ranges and copy GPU writes back into title RAM.
Previously, merge_writeback iterated through all memory byte-by-byte in a scalar loop. In typical frames, over 95% of 8-byte words are identical (host_word == old_word), meaning the scalar byte loop spent significant CPU cycles doing branchless byte math over completely unchanged data.
Solution:
Process slices in 8-byte (u64) chunks using chunks_exact_mut(8).
Fast-path: When h_word == o_word, the GPU did not alter any byte in that 8-byte chunk. If h_word != t_word, the host buffer simply takes the title word, fast-skipping all 8 bytes in a single comparison without scalar loop overhead.
Fall-back: Run byte-level lane bitwise merging only on 8-byte chunks where alterations occurred, as well as on any trailing remainder bytes (< 8).
Verified against the existing test oracle writeback_merge_matches_bytewise_oracle across all test lengths [0, 1, 63, 64, 65, 193, 65536] and stride intervals with zero mismatches.
@Dukestar69 on GitHub · 2026-11-26 06:55 UTC · original · reviewed
Tested locally merged onto
origin/main(63bcca4) on Windows 11 x64 (Rust 1.98.1 MSVC, NVIDIA GeForce RTX 5090 + WARP):cargo test -p victory-gpu shadow::tests -- --test-threads=1(RTX 5090 & WARP): 9 passed, 0 failed on both.cargo clippy -p victory-gpu --all-targets -- -D warnings: fails with 3 errors (clippy::chunks_exact_to_as_chunkson Rust 1.98.1):Switching to
as_chunks_mut::<8>()/as_chunks::<8>()will fix all three clippy errors and also eliminate the.try_into().unwrap()conversions.Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
@oni-swr on GitHub · 2026-11-26 07:02 UTC · original
Independent CPU-only verification of #3079 at
2371f8de832ab5e66ef0b2ed9ddb81e3f8bda941against its exact parent63bcca43c6a00a758510398fa43096ce7e4f7df0. This is the scope claimed in #1633. State and head were rechecked before and after the run: open, head unchanged. #1633 has no other claim on this PR. It is a synthetic scalar-oracle comparison only; it does not repeat the shadow suite or the clippy findings already on the PR.Result: no difference found. The old and new
merge_writebackproduced identical title bytes, host bytes and returned flag in every case, and both matched an independent per-byte specification (a byte the GPU changed,host != old, takes the GPU value; otherwise the title/CPU byte stays; then host = title).Method. Both functions were extracted verbatim from the two commits into a standalone harness, so no other code could differ. Every case ran on slices placed at byte offsets 0 to 7 inside guard-padded buffers filled with a sentinel. The guards and the
oldbuffer were checked for stray writes. 4,452,170 of the new-implementation calls started at a non-8-aligned address.That is 5,403,891 comparisons in total.
Counters. The only observable output besides the bytes is the returned
bool, which gateswritten += nand the write-back to title memory in the two callers. It matched in every case, including the false cases where the CPU changed bytes and the GPU didn't, and where the GPU wrote the value the CPU already held.Control. Two deliberate mutations of the new function were both caught: making the fast path skip the host-takes-title copy, and dropping the tail remainder loop. The harness can fail.
Note, not a defect. The old loop used
zip, which stops at the shortest of the three slices. The new remainder step pairs three separate remainders, so it would skip the tail bytes if the lengths ever differed. I did not test unequal lengths. Both callers inshadow.rspass equal lengths (nfor all three), so this can't happen today. Adebug_assert_eq!on the lengths would make that contract explicit if the author wants it.Environment. Windows 11, Rust 1.98.1, one scratch cargo project with
CARGO_TARGET_DIR=D:\victory-3079-oracle-target,--locked -j 4. No D3D12, GPU, game files, dumps, shader names or hashes. The repo checkout was never modified and is clean at the parent commit. The target directory was removed and I confirmed that it no longer exists.Disclosure. AI-assisted: Claude Code (Claude Sonnet 5.5) ran the verification, coordinated by Codex (gpt-6.2-sol). The author keeps ownership of the change; I have posted no competing PR.
@fuzeofficlal on GitHub · 2026-11-26 08:00 UTC · original · requested changes
Tested locally on Windows 11 x64 (
rustc 1.98.1, RTX 5090 + WARP):cargo clippy -p victory-gpu --all-targets --locked -- -D warnings(default and--all-features): clean (as_chunks_mut::<8>()resolved the Rust 1.98.1 clippy lints and stays within workspace MSRV 1.88).shadow::writeback_merge_matches_bytewise_oracle(1 passed) andshadow::tests(9 passed on RTX 5090, 9 passed on WARP with--features diagnostics);cargo xtask policy: 191 files ok.However, benchmarking this change in release mode (
rustc -C opt-level=3, allowingmerge_writeback_bytesto inline) and inspecting the generated x86-64 assembly shows a regression across every workload, including the 0% and 5% dirty cases:main, LLVM already auto-vectorizes the branchless byte loop into a 32-byte/iteration SSE2 loop (movdqu/pxor/pcmpeqb/pandn/por, zero branches in the hot loop).if h_word == o_wordbranch disables auto-vectorization: the fast path runs scalar 8-bytecmpq/jne/je, and the dirty fallback unrollsmerge_writeback_bytesinto ~130 scalar shift/cmov instructions with heavy stack spilling (subq $136, %rsp).Measured averages (64 KiB pages over 20,000 iterations; 193 B over 200,000 iterations, same-binary A/B):
main#3079Since
merge_writebackis called on pages wherehost != old(or marked GPU-written pages), this makes the hot path 2×–10× slower in release builds. Recommend holding off on merging unless a revised implementation demonstrates an actual release speedup overmain's auto-vectorized SSE2 loop (for example, keeping branchless byte-lane math intact on wider 16/32-byte chunks or verifying with a release benchmark that a coarse skip beats SSE2 without degrading dirty pages).@Marvisak on GitHub · 2026-11-26 08:22 UTC · original
Automated triage for the maintainers.
Needs work. By my reading the new
merge_writebackinshadow.rsis equivalent to the old one: an unchanged word leaves title alone and sets host to title, as before, and slicing to the shortest length keeps the oldzipbehaviour. The existing oracle test passes. CI: passing.The PR is a performance change with no measurement, though. The old loop is branchless over three zipped byte slices, which LLVM normally vectorizes already. The new version adds a data-dependent branch per 8 bytes, so it could just as well be slower. Please add a before/after timing of
copy_backfrom a release build, either from a run (theCompareUscounter incopystat) or from a micro-benchmark on realistic data. That would change the verdict. The "over 95 % of words identical" figure should also be backed by a measurement.Generated by Claude Code
@cloph-dsp on GitHub · 2026-11-26 10:53 UTC · original
Independent local verification of head
4077544on Windows 11, Rust 1.100.0/MSVC:cargo test --locked -p victory-gpu --lib shadow::writeback_merge_matches_bytewise_oracle— 1 passed, 0 failed.git diff --check origin/main HEAD— clean.CPU-only bytewise oracle test; no D3D12 device needed. Used a worktree-local
CARGO_TARGET_DIR. Synthetic inputs only; no game assets or DRM material used. AI-assisted (Hy4 flash).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.