Shaders: LDS byte and half writes as one bounded compare-exchange, not AND then OR #913

Open
github-bot wants to merge 3 commits from github-bot/gh-913 into main AGit
Owner

@⁠SpaghettiMaster545 on GitHub · 2026-11-25 12:34 UTC · original
SpaghettiMaster545:pr/lds-byte-cas → main · +92 −7 · 2 files · 3 commits · open

What was wrong

ds_write_b8/ds_write_b16 and their _d16_hi forms wrote their part of an LDS dword in two atomics: an AND that cleared the part, then an OR that set it. Lanes writing the same byte in one instruction ORed their values together (a value no lane wrote), and a lane reading the dword between the two atomics saw the part cleared.

How it shows

Programs that pack bytes or halves of neighbouring lanes into shared dwords read back mixed bits, so whatever they build from them (masks, counters, packed indices) comes out wrong on the host GPU.

What the change does

The part now replaces its bits in one compare-exchange, retried while another lane changes the dword in between, bounded at 65536 tries so a fault cannot spin the GPU. Lanes writing other parts of the dword all land, and one value of the lanes writing the same part stays, as on the console.

How it was tested

  • cargo test --locked -p victory-shader --lib passes on this branch (215 passed, 3 ignored); cargo clippy -p victory-shader --all-targets --locked -- -D warnings is clean. (A workspace-wide clippy fails on main already, in victory-gpu, which this change does not touch.)
  • In our tree, GPU tests of neighbouring lanes' byte and half writes into one dword, the _d16_hi writes, masked byte writes that keep the other bytes, and every byte/half read form are exact with this change. Their harness is not in this fork, so they are not part of this PR.
  • Played with this change in our integration branch.
  • GPU and driver: NVIDIA GeForce RTX 5070 Ti, driver 621.40, Windows 11; GPU tests also on an AMD Radeon iGPU, driver 24.10.24.

Developed with AI assistance (Claude Code).

**@⁠SpaghettiMaster545** on GitHub · 2026-11-25 12:34 UTC · [original](https://github.com/fxceless/victory-fork/pull/913) `SpaghettiMaster545:pr/lds-byte-cas` → `main` · +92 −7 · 2 files · 3 commits · open ## What was wrong `ds_write_b8`/`ds_write_b16` and their `_d16_hi` forms wrote their part of an LDS dword in two atomics: an AND that cleared the part, then an OR that set it. Lanes writing the same byte in one instruction ORed their values together (a value no lane wrote), and a lane reading the dword between the two atomics saw the part cleared. ## How it shows Programs that pack bytes or halves of neighbouring lanes into shared dwords read back mixed bits, so whatever they build from them (masks, counters, packed indices) comes out wrong on the host GPU. ## What the change does The part now replaces its bits in one compare-exchange, retried while another lane changes the dword in between, bounded at 65536 tries so a fault cannot spin the GPU. Lanes writing other parts of the dword all land, and one value of the lanes writing the same part stays, as on the console. ## How it was tested - `cargo test --locked -p victory-shader --lib` passes on this branch (215 passed, 3 ignored); `cargo clippy -p victory-shader --all-targets --locked -- -D warnings` is clean. (A workspace-wide clippy fails on `main` already, in `victory-gpu`, which this change does not touch.) - In our tree, GPU tests of neighbouring lanes' byte and half writes into one dword, the `_d16_hi` writes, masked byte writes that keep the other bytes, and every byte/half read form are exact with this change. Their harness is not in this fork, so they are not part of this PR. - Played with this change in our integration branch. - GPU and driver: NVIDIA GeForce RTX 5070 Ti, driver 621.40, Windows 11; GPU tests also on an AMD Radeon iGPU, driver 24.10.24. Developed with AI assistance (Claude Code).
ds_write_b8/b16 and their _d16_hi forms cleared their part of the LDS
dword and set it in two atomics: lanes writing the same byte in one
instruction OR their values together, and a reader between the two sees
the part cleared. The part now replaces its bits in one compare-exchange,
retried while another lane changes the dword in between (bounded at
65536 tries so a fault cannot spin the GPU): lanes writing other parts
of the dword all land and one value of lanes writing the same part
stays, as on the console.

Tested with GPU tests in our tree (lds_*), exact on the RTX 5070 Ti and
the Radeon iGPU.

(cherry picked from commit ab349a78ce1fcd9f681055aff0c7c14ebbcd005b)
lds_merge was inserted between begin_guarded's doc comment and the
function, so the comment documented lds_merge (noted in review).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:53:43 +00:00
Author
Owner

@⁠avt0m4t48 on GitHub · 2026-11-25 12:51 UTC · original

Independent Windows Server 2025 WARP check at 5cf45f7. I added a source-built 64-thread regression covering the byte and half writes and both _d16_hi forms. For each opcode, overlapping lanes must leave one of the two supplied values, and lanes writing adjacent fields must preserve both values and the other bits.

All eight cases passed with the PR. With just the production change removed, the first overlapping-byte case returned 3 from lanes supplying 1 or 2. The job sets VICTORY_WARP=1, so the first backend label in the negative-control log also refers to WARP.

The test-only commit is available to cherry-pick. Windows run

AI-assisted. Synthetic shader and inputs only.

**@⁠avt0m4t48** on GitHub · 2026-11-25 12:51 UTC · [original](https://github.com/fxceless/victory-fork/pull/913#issuecomment-7162954837) Independent Windows Server 2025 WARP check at `5cf45f7`. I added a source-built 64-thread regression covering the byte and half writes and both `_d16_hi` forms. For each opcode, overlapping lanes must leave one of the two supplied values, and lanes writing adjacent fields must preserve both values and the other bits. All eight cases passed with the PR. With just the production change removed, the first overlapping-byte case returned 3 from lanes supplying 1 or 2. The job sets `VICTORY_WARP=1`, so the first backend label in the negative-control log also refers to WARP. The [test-only commit](https://github.com/avt0m4t48/victory-fork/commit/51a20b5ffd931d918dd1ffa9b1d1ced0ceb2e01a) is available to cherry-pick. [Windows run](https://github.com/avt0m4t48/victory-fork/actions/runs/45617261602) AI-assisted. Synthetic shader and inputs only.
Author
Owner

@⁠eazyves on GitHub · 2026-11-25 13:07 UTC · original

Run on real RDNA4 hardware: RX 9070 XT, driver 32.0.32029.2010, Windows 11 26200, Rust 1.98.1, with avt0m4t48's test (51a20b5) on this PR's head 5cf45f7.

  • With the PR: ds_subword_writes_preserve_neighboring_bits_and_one_lanes_value passes, 4 of 4 runs.
  • With lower.rs put back to the merge base a253706: fails on its first case, with the test's own hardware label: op 0x1e, got 0xa5c3035a, so the byte is 3 from lanes that wrote 1 or 2.

So the merged value is not a WARP effect; the card shows it too, and this change removes it. One run takes about 50 s in a debug build.

A small thing in the diff: lds_merge sits between begin_guarded and that function's doc comment, so "Starts code only the lanes where guard holds run ..." now documents lds_merge.

AI-assisted (Claude Code); run on my machine, synthetic shader only, no game data.

**@⁠eazyves** on GitHub · 2026-11-25 13:07 UTC · [original](https://github.com/fxceless/victory-fork/pull/913#issuecomment-7163325504) Run on real RDNA4 hardware: RX 9070 XT, driver 32.0.32029.2010, Windows 11 26200, Rust 1.98.1, with avt0m4t48's test (51a20b5) on this PR's head `5cf45f7`. - With the PR: `ds_subword_writes_preserve_neighboring_bits_and_one_lanes_value` passes, 4 of 4 runs. - With `lower.rs` put back to the merge base `a253706`: fails on its first case, with the test's own `hardware` label: op `0x1e`, got `0xa5c3035a`, so the byte is 3 from lanes that wrote 1 or 2. So the merged value is not a WARP effect; the card shows it too, and this change removes it. One run takes about 50 s in a debug build. A small thing in the diff: `lds_merge` sits between `begin_guarded` and that function's doc comment, so "Starts code only the lanes where `guard` holds run ..." now documents `lds_merge`. AI-assisted (Claude Code); run on my machine, synthetic shader only, no game data.
Author
Owner

@⁠SpaghettiMaster545 on GitHub · 2026-11-25 15:34 UTC · original

Thanks to both of you. I took the test commit as it is (51a20b5). ds_subword_writes_preserve_neighboring_bits_and_one_lanes_value passes here on the RTX 5070 Ti (NVIDIA 621.40, Windows 11). I also moved begin_guarded's doc comment back onto begin_guarded (847ad17). cargo clippy -p victory-shader --all-targets --locked -- -D warnings is clean.

**@⁠SpaghettiMaster545** on GitHub · 2026-11-25 15:34 UTC · [original](https://github.com/fxceless/victory-fork/pull/913#issuecomment-7167055752) Thanks to both of you. I took the test commit as it is (`51a20b5`). `ds_subword_writes_preserve_neighboring_bits_and_one_lanes_value` passes here on the RTX 5070 Ti (NVIDIA 621.40, Windows 11). I also moved `begin_guarded`'s doc comment back onto `begin_guarded` (`847ad17`). `cargo clippy -p victory-shader --all-targets --locked -- -D warnings` is clean.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/913/head:github-bot/gh-913'
git switch 'github-bot/gh-913'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-913'
git switch 'github-bot/gh-913'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-913'
git switch 'github-bot/gh-913'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-913'
git switch 'main'
git merge --squash 'github-bot/gh-913'
git switch 'main'
git merge --ff-only 'github-bot/gh-913'
git switch 'main'
git merge 'github-bot/gh-913'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.