Shaders: lower ds_add{_rtn}_f32 and ds_cmpst{_rtn}_f32 LDS atomics #1487

Open
AscilCH wants to merge 1 commit from upstream-pr/1488 into main
AscilCH commented 2026-11-24 18:22:24 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by AscilCH on 2026-11-24T18:22:24Z.


Summary

Lower GFX10 floating-point LDS atomics ds_add_f32 (0x015), ds_add_rtn_f32 (0x055), ds_cmpst_f32 (0x011), and ds_cmpst_rtn_f32 (0x031) in Lower::ds() (crates/victory-shader/src/emit/lower.rs).

(Complementary to #164 which covers integer LDS atomics and leaves out float atomics, and disjoint from #628 which covers ds_min_f32/ds_max_f32.)

Reproduction (Before Fix)

Lowering a synthetic compute shader containing ds_add_f32, ds_add_rtn_f32, ds_cmpst_f32, or ds_cmpst_rtn_f32 failed with:

lower: Unsupported { offset: 40, what: "an LDS operation" }

Implementation

  • ds_add_f32 / ds_add_rtn_f32: Emits a guarded cmpxchg CAS loop on the LDS dword: reads cur, computes next = bitcast(bitcast(cur, f32) + bitcast(src, f32), i32), and retries cmpxchg cur -> next until the previous word equals cur (handling both normal updates and NaN/no-op termination cleanly).
  • ds_cmpst_f32 / ds_cmpst_rtn_f32: Emits a guarded cmpxchg loop using IEEE-754 ordered float equality (fcmp oeq) against the loaded word — matching RDNA2's floating-point compare-and-swap semantics where +0.0 == -0.0 succeeds and NaN == NaN fails (leaving LDS untouched and returning the old bits for _rtn).

Test

Added synthetic end-to-end test emit::valu_tests::ds_add_f32_and_ds_cmpst_f32_match_rdna2 (64-thread two-wave compute shaders, validated by dxil.dll and executed on hardware GPU + WARP):

  • Single-lane (v_cmpx_eq_u32 EXEC-guarded) ds_add_rtn_f32 and ds_add_f32 old/new values and NaN add termination.
  • ds_cmpst_rtn_f32 and ds_cmpst_f32 covering matching values, mismatching values, +0.0 == -0.0, -0.0 == +0.0, and NaN != NaN.
  • 64-lane contended ds_add_f32 accumulation across two waves verifying the exact sum 1 + 2 + ... + 64 = 2080.0.

Verification

  • rustfmt --edition 2024 --check crates/victory-shader/src/emit/valu_tests.rs and modified lines of lower.rs: clean
  • cargo test -p victory-shader --locked ds_add_f32_and_ds_cmpst_f32_match_rdna2 (hardware + WARP + dxil.dll): passed
  • VICTORY_WARP=1 cargo test -p victory-shader --locked -- --test-threads=1: 121 passed, 0 failed
  • cargo xtask policy: 154 files ok
  • cargo clippy --workspace --all-targets --locked -- -D warnings: clean
  • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings: clean
<!-- greyfinch-sync:Marvisak/VIctory:issue:6588562809 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1488). Originally posted by [AscilCH](https://github.com/AscilCH) on 2026-11-24T18:22:24Z. --- ## Summary Lower GFX10 floating-point LDS atomics `ds_add_f32` (`0x015`), `ds_add_rtn_f32` (`0x055`), `ds_cmpst_f32` (`0x011`), and `ds_cmpst_rtn_f32` (`0x031`) in `Lower::ds()` (`crates/victory-shader/src/emit/lower.rs`). *(Complementary to #164 which covers integer LDS atomics and leaves out float atomics, and disjoint from #628 which covers `ds_min_f32`/`ds_max_f32`.)* ## Reproduction (Before Fix) Lowering a synthetic compute shader containing `ds_add_f32`, `ds_add_rtn_f32`, `ds_cmpst_f32`, or `ds_cmpst_rtn_f32` failed with: ```text lower: Unsupported { offset: 40, what: "an LDS operation" } ``` ## Implementation - **`ds_add_f32` / `ds_add_rtn_f32`**: Emits a guarded `cmpxchg` CAS loop on the LDS dword: reads `cur`, computes `next = bitcast(bitcast(cur, f32) + bitcast(src, f32), i32)`, and retries `cmpxchg cur -> next` until the previous word equals `cur` (handling both normal updates and NaN/no-op termination cleanly). - **`ds_cmpst_f32` / `ds_cmpst_rtn_f32`**: Emits a guarded `cmpxchg` loop using IEEE-754 ordered float equality (`fcmp oeq`) against the loaded word — matching RDNA2's floating-point compare-and-swap semantics where `+0.0 == -0.0` succeeds and `NaN == NaN` fails (leaving LDS untouched and returning the old bits for `_rtn`). ## Test Added synthetic end-to-end test `emit::valu_tests::ds_add_f32_and_ds_cmpst_f32_match_rdna2` (64-thread two-wave compute shaders, validated by `dxil.dll` and executed on hardware GPU + WARP): - Single-lane (`v_cmpx_eq_u32` EXEC-guarded) `ds_add_rtn_f32` and `ds_add_f32` old/new values and NaN add termination. - `ds_cmpst_rtn_f32` and `ds_cmpst_f32` covering matching values, mismatching values, `+0.0 == -0.0`, `-0.0 == +0.0`, and `NaN != NaN`. - 64-lane contended `ds_add_f32` accumulation across two waves verifying the exact sum `1 + 2 + ... + 64 = 2080.0`. ## Verification - `rustfmt --edition 2024 --check crates/victory-shader/src/emit/valu_tests.rs` and modified lines of `lower.rs`: clean - `cargo test -p victory-shader --locked ds_add_f32_and_ds_cmpst_f32_match_rdna2` (hardware + WARP + `dxil.dll`): passed - `VICTORY_WARP=1 cargo test -p victory-shader --locked -- --test-threads=1`: 121 passed, 0 failed - `cargo xtask policy`: 154 files ok - `cargo clippy --workspace --all-targets --locked -- -D warnings`: clean - `cargo clippy --workspace --all-targets --all-features --locked -- -D warnings`: clean
DenRakEiw commented 2026-11-24 19:03:37 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DenRakEiw on 2026-11-24T18:28:02Z.

Review: APPROVED


Verified locally merged onto origin/main (Windows 11 x64, NVIDIA GeForce RTX 5090 + WARP, Rust 1.98.1 MSVC):

  • cargo test -p victory-shader --lib ds_add_f32_and_ds_cmpst_f32_match_rdna2 — passed on RTX 5090 hardware and under VICTORY_D3D12_WARP=1
  • cargo clippy -p victory-shader --all-targets — 0 warnings
  • cargo xtask policy — ok

Clean CAS-loop lowering for ds_add{_rtn}_f32 and ds_cmpst{_rtn}_f32 with end-to-end GPU+WARP verification. Looks good.

AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:Marvisak/VIctory:review:6045043668 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1488#pullrequestreview-6045043668). Originally posted by [DenRakEiw](https://github.com/DenRakEiw) on 2026-11-24T18:28:02Z. **Review:** APPROVED --- Verified locally merged onto `origin/main` (Windows 11 x64, NVIDIA GeForce RTX 5090 + WARP, Rust 1.98.1 MSVC): - `cargo test -p victory-shader --lib ds_add_f32_and_ds_cmpst_f32_match_rdna2` — passed on RTX 5090 hardware and under `VICTORY_D3D12_WARP=1` - `cargo clippy -p victory-shader --all-targets` — 0 warnings - `cargo xtask policy` — ok Clean CAS-loop lowering for `ds_add{_rtn}_f32` and `ds_cmpst{_rtn}_f32` with end-to-end GPU+WARP verification. Looks good. AI-assisted (Gemini 4 Neon).
catminou4 commented 2026-11-24 19:03:37 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-24T18:42:10Z.

Review: APPROVED


Tested locally on macOS arm64 (rustc 1.98.1): PR head merged cleanly into current main (ffe310e), cargo test -p victory-shader --locked → 96 passed, 0 failed. Execution/valu tests are #[cfg(all(test, windows))] and were not run here; lowering/structure coverage only.

ds_add_f32/ds_cmpst_f32 (+_rtn) as CAS loops is the right lowering since DXIL has no float LDS atomic:

  • add: load → CAS loop on the i32 bits, retry with the returned old value; NaN terminates because cmpxchg compares bits;
  • cmpst: OEQ float compare against the memory value, cmpxchg only on match, old value returned either way — +0.0/-0.0 match and NaN!=NaN behave like hardware;
  • _rtn handled by the existing name normalization.

The valu tests are strong: per-case memory/result checks, NaN no-spin cases, and a 64-lane contended ds_add_f32 expecting exactly 2080.0 on hardware and WARP. Windows-only here — please run before merge.

<!-- greyfinch-sync:Marvisak/VIctory:review:6045233150 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1488#pullrequestreview-6045233150). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-24T18:42:10Z. **Review:** APPROVED --- Tested locally on macOS arm64 (rustc 1.98.1): PR head merged cleanly into current main (ffe310e), `cargo test -p victory-shader --locked` → 96 passed, 0 failed. Execution/valu tests are `#[cfg(all(test, windows))]` and were not run here; lowering/structure coverage only. `ds_add_f32`/`ds_cmpst_f32` (+`_rtn`) as CAS loops is the right lowering since DXIL has no float LDS atomic: - add: load → CAS loop on the i32 bits, retry with the returned old value; NaN terminates because cmpxchg compares bits; - cmpst: OEQ float compare against the memory value, cmpxchg only on match, old value returned either way — +0.0/-0.0 match and NaN!=NaN behave like hardware; - `_rtn` handled by the existing name normalization. The valu tests are strong: per-case memory/result checks, NaN no-spin cases, and a 64-lane contended `ds_add_f32` expecting exactly 2080.0 on hardware and WARP. Windows-only here — please run before merge.
marquezinii commented 2026-11-24 19:04:26 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by marquezinii on 2026-11-24T18:48:10Z.


Ran on an RTX 5070 (desktop, driver 621.40, the only adapter), Windows 11 26300, Rust 1.98.1 at 62c4ded. cargo test -p victory-shader --lib -- --test-threads=1 --nocapture gives 121 passed, 0 failed, 1 ignored, with the new LDS float atomic test passing on the hardware adapter and WARP. Running the compare-exchange loop on the raw bits (prev == cur as integers) means a NaN already stored can't make the ds_add_f32 loop spin, which was the part I wanted to see. On this hardware the loop also completes when the lanes of a wave contend for one address.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7141172059 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1488#issuecomment-7141172059). Originally posted by [marquezinii](https://github.com/marquezinii) on 2026-11-24T18:48:10Z. --- Ran on an RTX 5070 (desktop, driver 621.40, the only adapter), Windows 11 26300, Rust 1.98.1 at `62c4ded`. `cargo test -p victory-shader --lib -- --test-threads=1 --nocapture` gives 121 passed, 0 failed, 1 ignored, with the new LDS float atomic test passing on the hardware adapter and WARP. Running the compare-exchange loop on the raw bits (`prev == cur` as integers) means a NaN already stored can't make the `ds_add_f32` loop spin, which was the part I wanted to see. On this hardware the loop also completes when the lanes of a wave contend for one address.
wwwwwwww3333333 commented 2026-11-24 20:03:04 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by wwwwwwww3333333 on 2026-11-24T19:15:19Z.


Native Windows run on AMD RDNA2: RX 6750 XT (RDNA2, gfx1031), AMD driver 32.0.21045.29003, i5-13400F, Windows 11, Rust 1.98.1 MSVC. Head 62c4ded on main ffe310e (clean merge).

cargo test -p victory-shader --lib -- --test-threads=1: 121 passed, 0 failed, 1 ignored (main alone: 120 passed). ds_add_f32_and_ds_cmpst_f32_match_rdna2 ran on the hardware adapter and then on WARP, and passed on both. This adds an AMD result to the NVIDIA runs already posted.

Synthetic tests only, no game files. AI-assisted (Claude picked the PRs and wrote the batch script; I ran it on my machine).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7141829249 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1488#issuecomment-7141829249). Originally posted by [wwwwwwww3333333](https://github.com/wwwwwwww3333333) on 2026-11-24T19:15:19Z. --- Native Windows run on AMD RDNA2: RX 6750 XT (RDNA2, gfx1031), AMD driver 32.0.21045.29003, i5-13400F, Windows 11, Rust 1.98.1 MSVC. Head 62c4ded on main ffe310e (clean merge). `cargo test -p victory-shader --lib -- --test-threads=1`: **121 passed, 0 failed**, 1 ignored (main alone: 120 passed). `ds_add_f32_and_ds_cmpst_f32_match_rdna2` ran on the hardware adapter and then on WARP, and passed on both. This adds an AMD result to the NVIDIA runs already posted. Synthetic tests only, no game files. AI-assisted (Claude picked the PRs and wrote the batch script; I ran it on my machine).
sinan-cmyk commented 2026-11-24 21:03:24 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by sinan-cmyk on 2026-11-24T20:13:16Z.


Offline measurement on a retail dump, and the shader tests on Ada

Result: no count change against main.

Offline. I merged head 62c4ded onto main ffe310e (clean merge) and ran victory translate --validate --list on shaders.rpf (1.0.2.1), with dxil.dll 1.8.2502.11. I compared the result with main program by program (25,889 programs). Validated: 21,724 on main, 21,724 with this PR.

No program changes its failure reason either. That is the expected result for a fix whose instructions sit behind the earlier failures on main. The count shows that the change rejects nothing. It does not show whether the output is right.

Tests: with the same merge, cargo test --locked -p victory-shader --lib passed 121, failed 0. The bitcode benchmark is ignored, as on main, where 120 pass. The run was on an RTX 4070 SUPER (Ada, 32-lane waves, driver 32.0.16.1088), the default adapter, with VICTORY_WARP unset.

No shader names or data are included, per the asset policy. AI-assisted (Claude Code).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7143209540 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1488#issuecomment-7143209540). Originally posted by [sinan-cmyk](https://github.com/sinan-cmyk) on 2026-11-24T20:13:16Z. --- ### Offline measurement on a retail dump, and the shader tests on Ada **Result: no count change against `main`.** **Offline.** I merged head 62c4ded onto `main` ffe310e (clean merge) and ran `victory translate --validate --list` on `shaders.rpf` (1.0.2.1), with dxil.dll 1.8.2502.11. I compared the result with `main` program by program (25,889 programs). Validated: 21,724 on `main`, 21,724 with this PR. No program changes its failure reason either. That is the expected result for a fix whose instructions sit behind the earlier failures on `main`. The count shows that the change rejects nothing. It does not show whether the output is right. **Tests:** with the same merge, `cargo test --locked -p victory-shader --lib` passed 121, failed 0. The bitcode benchmark is ignored, as on `main`, where 120 pass. The run was on an RTX 4070 SUPER (Ada, 32-lane waves, driver 32.0.16.1088), the default adapter, with `VICTORY_WARP` unset. No shader names or data are included, per the asset policy. AI-assisted (Claude Code).
Marvisak commented 2026-11-25 02:06:07 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T01:29:44Z.


Automated triage for the maintainers.

Needs rebase. This pull request conflicts with main (conflicting: crates/victory-shader/src/emit/valu_tests.rs). It will be reviewed once it is rebased onto the current main.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7149610304 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1488#issuecomment-7149610304). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T01:29:44Z. --- <!-- victory-triage v1 head=62c4ded0ffbac3cb42f73aa7ddf66f4d49d43bad verdict=needs-rebase at=2026-11-25T01:29:43Z --> Automated triage for the maintainers. **Needs rebase.** This pull request conflicts with `main` (conflicting: `crates/victory-shader/src/emit/valu_tests.rs`). It will be reviewed once it is rebased onto the current `main`. --- _Generated by [Claude Code](https://claude.ai/code)_
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/1488:upstream-pr/1488'
git switch 'upstream-pr/1488'

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/1488'
git switch 'upstream-pr/1488'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/1488'
git switch 'upstream-pr/1488'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/1488'
git switch 'main'
git merge --squash 'upstream-pr/1488'
git switch 'main'
git merge --ff-only 'upstream-pr/1488'
git switch 'main'
git merge 'upstream-pr/1488'
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!1487
No description provided.