Shaders: LDS reads and writes of a byte or half #632

Closed
itsRevela wants to merge 1 commit from upstream-pr/632 into main
itsRevela commented 2026-11-24 05:57:38 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by itsRevela on 2026-11-24T05:57:38Z.


What

LDS accesses of a byte or a half, which stopped translation with an LDS write of bytes or halves / an LDS read of bytes or halves:

  • ds_write_b8, ds_write_b16, ds_write_b8_d16_hi, ds_write_b16_d16_hi: write only their part of the dword. The _d16_hi forms write the data's high half. An atomic AND clears the part, then an atomic OR sets it, so lanes writing the other parts of the same dword keep theirs.
  • ds_read_u8, ds_read_i8, ds_read_u16, ds_read_i16: load the dword, shift the part down, and zero- or sign-extend it.
  • The d16 reads (ds_read_u8_d16, ..._d16_hi, ...) are still refused. They write half of the register and keep the other half, and the IR records a DS destination as written only, never read. They now fail with an LDS read into half a register.

As for whole dwords, addresses are taken as aligned to the access size.

Tests:

  • emit::valu_tests: build_valu_cs gains an LDS-size variant (build_cs) and a DS encoder, and run_cases a variant that takes the hosts to run on (run_cases_on).

Why

In the opening cutscene, 2 programs per run stopped at these instructions; one of them has one ds_write_b16 and nine ds_read_u16, so writes alone would only have moved the failure to the reads. Each dropped about 130 draws a frame. I found no issue or PR covering them.

One semantic difference: if two lanes write different values to the same byte in one instruction, the clear/set pair can leave the OR of the values, where the hardware keeps one lane's value. Lanes writing different bytes or halves of one dword (byte arrays, packed halves) give the hardware's result exactly.

Tested

Windows 11 Pro 10.0.26200, NVIDIA GeForce RTX 3070 Ti (driver 32.0.16.1088), Rust 1.98.1, on d2a4bae.

  • New emit::valu_tests::lds_bytes_and_halves_match_rdna2 (real GPU). A one-thread program writes a dword to LDS, then:

    • each byte or half write at several offsets, read back as a dword;
    • each byte or half read at several offsets.

    Results are checked against expected values on three input sets, with sign bits set and clear. It runs on hardware only: WARP refuses the compute pipeline of any program with LDS from this harness, a plain ds_write_b32/ds_read_b32 one too, so that's not new here.

  • New emit::lower::tests::lds_bytes_and_halves_lower and lds_reads_into_half_a_register_are_refused. They need no GPU, so CI runs them.

  • The fix matters. All three new tests fail with the two new branches replaced by the old refusals.

  • Crate checks:

    • cargo test -p victory-shader: 72 passed.
    • cargo clippy -p victory-shader --tests: nothing in the changed files.
    • cargo fmt -p victory-shader --check: clean.
    • cargo xtask policy: ok.
  • With the title (diag build, Windows Sandbox vGPU): both programs that failed on main translate. The run logs no LDS byte or half failures; untranslated programs went from 382 to 376 and translated ones from 1,634 to 1,647, with run-to-run variation.

AI-assisted (Claude Code). The title runs used my own copy; no game data is in this PR.

🤖 Generated with Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:issue:6575651672 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/632). Originally posted by [itsRevela](https://github.com/itsRevela) on 2026-11-24T05:57:38Z. --- ## What LDS accesses of a byte or a half, which stopped translation with `an LDS write of bytes or halves` / `an LDS read of bytes or halves`: - **`ds_write_b8`, `ds_write_b16`, `ds_write_b8_d16_hi`, `ds_write_b16_d16_hi`**: write only their part of the dword. The `_d16_hi` forms write the data's high half. An atomic `AND` clears the part, then an atomic `OR` sets it, so lanes writing the other parts of the same dword keep theirs. - **`ds_read_u8`, `ds_read_i8`, `ds_read_u16`, `ds_read_i16`**: load the dword, shift the part down, and zero- or sign-extend it. - **The `d16` reads** (`ds_read_u8_d16`, `..._d16_hi`, ...) are still refused. They write half of the register and keep the other half, and the IR records a DS destination as written only, never read. They now fail with `an LDS read into half a register`. As for whole dwords, addresses are taken as aligned to the access size. Tests: - `emit::valu_tests`: `build_valu_cs` gains an LDS-size variant (`build_cs`) and a DS encoder, and `run_cases` a variant that takes the hosts to run on (`run_cases_on`). ## Why In the opening cutscene, 2 programs per run stopped at these instructions; one of them has one `ds_write_b16` and nine `ds_read_u16`, so writes alone would only have moved the failure to the reads. Each dropped about 130 draws a frame. I found no issue or PR covering them. One semantic difference: if two lanes write **different values to the same byte** in one instruction, the clear/set pair can leave the OR of the values, where the hardware keeps one lane's value. Lanes writing different bytes or halves of one dword (byte arrays, packed halves) give the hardware's result exactly. ## Tested Windows 11 Pro 10.0.26200, NVIDIA GeForce RTX 3070 Ti (driver 32.0.16.1088), Rust 1.98.1, on `d2a4bae`. - **New `emit::valu_tests::lds_bytes_and_halves_match_rdna2` (real GPU).** A one-thread program writes a dword to LDS, then: - each byte or half write at several offsets, read back as a dword; - each byte or half read at several offsets. Results are checked against expected values on three input sets, with sign bits set and clear. It runs on hardware only: WARP refuses the compute pipeline of any program with LDS from this harness, a plain `ds_write_b32`/`ds_read_b32` one too, so that's not new here. - **New `emit::lower::tests::lds_bytes_and_halves_lower` and `lds_reads_into_half_a_register_are_refused`.** They need no GPU, so CI runs them. - **The fix matters.** All three new tests fail with the two new branches replaced by the old refusals. - **Crate checks:** - `cargo test -p victory-shader`: 72 passed. - `cargo clippy -p victory-shader --tests`: nothing in the changed files. - `cargo fmt -p victory-shader --check`: clean. - `cargo xtask policy`: ok. - **With the title** (`diag` build, Windows Sandbox vGPU): both programs that failed on `main` translate. The run logs no LDS byte or half failures; untranslated programs went from 382 to 376 and translated ones from 1,634 to 1,647, with run-to-run variation. AI-assisted (Claude Code). The title runs used my own copy; no game data is in this PR. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
slint6614-dev commented 2026-11-24 08:02:56 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by slint6614-dev on 2026-11-24T07:47:20Z.


Measured on the full retail shader pack (26,503 containers) with victory translate --validate, against main d2a4bae (emitted / validated). Windows 11, dxil.dll from SDK 10.0.26100.

VS/PS CS MS sub-dword LDS refusals (7 reads, 9 writes on main) tests
main 12272 / 12172 1693 / 1670 8191 / 8129 16 ok
#101 same 1709 / 1686 same 0 ok
#632 same 1707 / 1684 same 0, but 2 new "an LDS read into half a register" ok
#383 12361 / 12252 1720 / 1697 8220 / 8158 0 see below
  • #101 turns all 16 into validated compute programs; #632 gets 14 of them.
  • #383 (wider PR) also clears image_sample_o, image_sample_d_cl, the 108 biased 16-bit samples and the 573 "scalar memory read as data", but those mostly move to other reasons ("a descriptor's address or format read as data" 71 → 595, "an image address" 2 → 27), and the signature mismatches go 21 → 29 (input) and 25 → 26 (output).
  • #383: cargo test -p victory-shader --release crashed with STATUS_ACCESS_VIOLATION in 2 of 6 parallel runs (0 of 6 on main; --test-threads=1 always passes, 76 tests). Possibly a race in one of the new tests.
<!-- greyfinch-sync:Marvisak/VIctory:comment:7125372120 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/632#issuecomment-7125372120). Originally posted by [slint6614-dev](https://github.com/slint6614-dev) on 2026-11-24T07:47:20Z. --- Measured on the full retail shader pack (26,503 containers) with `victory translate --validate`, against main d2a4bae (emitted / validated). Windows 11, dxil.dll from SDK 10.0.26100. | | VS/PS | CS | MS | sub-dword LDS refusals (7 reads, 9 writes on main) | tests | |---|---|---|---|---|---| | main | 12272 / 12172 | 1693 / 1670 | 8191 / 8129 | 16 | ok | | #101 | same | 1709 / 1686 | same | 0 | ok | | #632 | same | 1707 / 1684 | same | 0, but 2 new "an LDS read into half a register" | ok | | #383 | 12361 / 12252 | 1720 / 1697 | 8220 / 8158 | 0 | see below | - #101 turns all 16 into validated compute programs; #632 gets 14 of them. - #383 (wider PR) also clears `image_sample_o`, `image_sample_d_cl`, the 108 biased 16-bit samples and the 573 "scalar memory read as data", but those mostly move to other reasons ("a descriptor's address or format read as data" 71 → 595, "an image address" 2 → 27), and the signature mismatches go 21 → 29 (input) and 25 → 26 (output). - #383: `cargo test -p victory-shader --release` crashed with `STATUS_ACCESS_VIOLATION` in 2 of 6 parallel runs (0 of 6 on main; `--test-threads=1` always passes, 76 tests). Possibly a race in one of the new tests.
catminou4 commented 2026-11-24 12:04:23 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-24T11:04:55Z.


Triage note: this overlaps two other open PRs for the same feature — sub-dword LDS accesses (ds_read_{u,i}{8,16}/_d16/_d16_hi, ds_write_b{8,16}/_d16_hi):

  • #101 (Yaekai) — lower.rs only, I reviewed and approved it: merges cleanly with current main and cargo test -p victory-shader --locked is green on the merged tree (macOS arm64, rustc 1.98.1).
  • #116 (dragoso) — also touches ir.rs/mod.rs/lds_tests.rs; currently conflicts with main (mergeable=false).

This PR (#632) also conflicts with main. Three competing implementations of the same lowering — whichever the maintainer picks, the other two will need rebases. No objection to the approach itself; flagging so the overlap is visible before review time is spent three times.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7129630318 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/632#issuecomment-7129630318). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-24T11:04:55Z. --- Triage note: this overlaps two other open PRs for the same feature — sub-dword LDS accesses (`ds_read_{u,i}{8,16}`/`_d16`/`_d16_hi`, `ds_write_b{8,16}`/`_d16_hi`): - **#101** (Yaekai) — lower.rs only, I reviewed and approved it: merges cleanly with current main and `cargo test -p victory-shader --locked` is green on the merged tree (macOS arm64, rustc 1.98.1). - **#116** (dragoso) — also touches `ir.rs`/`mod.rs`/`lds_tests.rs`; currently conflicts with main (`mergeable=false`). This PR (#632) also conflicts with main. Three competing implementations of the same lowering — whichever the maintainer picks, the other two will need rebases. No objection to the approach itself; flagging so the overlap is visible before review time is spent three times.
DenRakEiw commented 2026-11-24 14:04:18 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DenRakEiw on 2026-11-24T13:46:29Z.

Review: APPROVED


Tested head 3bd8232 (fast-forward on main ffe310e) on Windows 11 x64 (Rust 1.98.1 MSVC) with NVIDIA GeForce RTX 5090 (Blackwell):

  • cargo test -p victory-shader lds_bytes_and_halves_match_rdna2: passed on hardware (exercising all 6 sub-dword write variants ds_write_{b8,b16} / _d16_hi and all 6 sub-dword read variants ds_read_{u8,i8,u16,i16} across 3 bit-pattern cases)
  • cargo clippy -p victory-shader --all-targets: 0 warnings
  • cargo xtask policy: ok

Code review looks clean and well-structured: sub-dword LDS writes use paired Rmw::And + Rmw::Or atomics inside begin_guarded/end_guarded so concurrent lanes writing distinct bytes/halves of the same dword do not clobber each other, and reads properly zero-/sign-extend via (word >> shift) << (32 - bits) >> (32 - bits). Approving.

AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:Marvisak/VIctory:review:6040317252 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/632#pullrequestreview-6040317252). Originally posted by [DenRakEiw](https://github.com/DenRakEiw) on 2026-11-24T13:46:29Z. **Review:** APPROVED --- Tested head `3bd8232` (fast-forward on `main` `ffe310e`) on Windows 11 x64 (Rust 1.98.1 MSVC) with **NVIDIA GeForce RTX 5090** (Blackwell): - `cargo test -p victory-shader lds_bytes_and_halves_match_rdna2`: **passed** on hardware (exercising all 6 sub-dword write variants `ds_write_{b8,b16}` / `_d16_hi` and all 6 sub-dword read variants `ds_read_{u8,i8,u16,i16}` across 3 bit-pattern cases) - `cargo clippy -p victory-shader --all-targets`: **0 warnings** - `cargo xtask policy`: ok Code review looks clean and well-structured: sub-dword LDS writes use paired `Rmw::And` + `Rmw::Or` atomics inside `begin_guarded`/`end_guarded` so concurrent lanes writing distinct bytes/halves of the same dword do not clobber each other, and reads properly zero-/sign-extend via `(word >> shift) << (32 - bits) >> (32 - bits)`. Approving. AI-assisted (Gemini 4 Neon).
upstream-sync closed this pull request 2026-11-25 01:04:34 +00:00
Marvisak commented 2026-11-25 01:07:48 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T00:26:36Z.


Automated triage for the maintainers. Closing because this change is already in main, or main has since replaced the code it patches.

Byte and half LDS accesses are in main's ds(), including the d16 reads this PR still refuses.

If this is mistaken, reply here with what was missed (or push a change and say so) and it will be looked at again. Thank you for the contribution.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7148514600 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/632#issuecomment-7148514600). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T00:26:36Z. --- Automated triage for the maintainers. Closing because this change is already in `main`, or `main` has since replaced the code it patches. Byte and half LDS accesses are in main's ds(), including the d16 reads this PR still refuses. If this is mistaken, reply here with what was missed (or push a change and say so) and it will be looked at again. Thank you for the contribution.

Pull request closed

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!632
No description provided.