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

Closed
github-bot wants to merge 1 commit from gh-pr/632/head into gh-pr/632/base
Owner

@⁠itsRevela on GitHub · 2026-11-24 05:57 UTC · original
itsRevela:shaders/lds-bytes-halves → main · +241 −6 · 2 files · 1 commits · closed without merging

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

**@⁠itsRevela** on GitHub · 2026-11-24 05:57 UTC · [original](https://github.com/Marvisak/VIctory/pull/632) `itsRevela:shaders/lds-bytes-halves` → `main` · +241 −6 · 2 files · 1 commits · closed without merging ## 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)
ds_write_b8, ds_write_b16 and their d16_hi forms replace only their part
of the dword: an atomic AND clears it and 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 and ds_read_i16 load the dword and zero- or
sign-extend their part. The d16 reads, which write half of the register
and keep the other, are still refused, now as "an LDS read into half a
register".

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:10:44 +00:00
github-bot closed this pull request 2026-11-26 04:10:44 +00:00
Author
Owner

@⁠slint6614-dev on GitHub · 2026-11-24 07:47 UTC · original

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.
**@⁠slint6614-dev** on GitHub · 2026-11-24 07:47 UTC · [original](https://github.com/Marvisak/VIctory/pull/632#issuecomment-7125372120) 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.
Author
Owner

@⁠catminou4 on GitHub · 2026-11-24 11:04 UTC · original

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.

**@⁠catminou4** on GitHub · 2026-11-24 11:04 UTC · [original](https://github.com/Marvisak/VIctory/pull/632#issuecomment-7129630318) 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.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 00:26 UTC · original

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.

**@⁠Marvisak** on GitHub · 2026-11-25 00:26 UTC · [original](https://github.com/Marvisak/VIctory/pull/632#issuecomment-7148514600) 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

This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!632
No description provided.