Shaders: LDS halfwords straddling two dwords, and hardware tests of byte and halfword LDS accesses #1795

Open
github-bot wants to merge 2 commits from github-bot/gh-1795 into main AGit
Owner

@⁠dragoso on GitHub · 2026-11-24 22:44 UTC · original
dragoso:shader/lds-sub-dword → main · +562 −1 · 3 files · 2 commits · open

What

main now lowers byte and halfword LDS reads and writes itself (0b35b7f), with the same approach this PR had (shift and extend, the d16 halves, and/or atomics for writes). This PR is now just what that implementation doesn't cover:

  • Halfwords at byte 3 of a dword take their high byte from byte 0 of the next one, as the hardware does. An RDNA2 measurement by @⁠Lyrnc in #116 (closed; it carried an earlier version of this lowering) showed ds_read_u16 at address 3 reads bytes 3-4 across the dword boundary, and the writes do the same. Before this, the lowering took halfwords as aligned and read or wrote the wrong bytes there. Reads now OR in the next dword's byte 0. Writes clear and set it with one more and/or atomic pair, selected so other halfwords touch nothing past their own dword. No branch or loop. A halfword at the last byte of the program's LDS has no next dword: its high byte reads 0 and its write is dropped, as out-of-bounds accesses do. A new lds_straddle helper sits next to lds_shift.
  • The same-byte shortcut: comment now says what the hardware keeps: the highest lane's value, measured on RDNA2. The lowering still leaves the OR of the values. A compare-exchange loop would be exact, but extra divergent loops have hung the AMD driver before.

Tests

emit/lds_tests.rs: synthetic GFX10 compute programs (hand-encoded, nothing from any title), lowered, validated by dxil.dll when present, and run on the hardware GPU and WARP against a model of the console's LDS:

  • every read form, with a preloaded VGPR for the d16 halves, and every write form, at every byte address from 2 to 7 (odd and straddling halfwords included), with a non-zero instruction offset; writes are read back through the dword holding them and the next one;
  • 64 lanes writing neighbouring bytes/halfwords of shared dwords (none is lost);
  • a halfword at the last byte of a 512-byte allocation (high byte 0, its write dropped);
  • a decoder check of the opcodes.

They take the GPU tests' one-at-a-time lock (valu_tests::ONE_AT_A_TIME). On main they fail only for the straddling halfwords.

Tested on Windows 11, NVIDIA RTX 5060 Ti and WARP: the LDS tests pass. In cargo test --workspace --no-fail-fast, the only failures are 5 that fail the same way on main 70564ae on this machine: victory-shader stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride, stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros and synthetic_tests::gathers_at_a_level_match_the_hardware, and victory-gpu descriptor::host::tests::dynamic_slots_are_held_until_released and memory::tests::lists_share_a_table_and_copies_keep_their_views. cargo clippy -p victory-shader --all-targets is clean, and cargo xtask policy is ok. Not run on AMD at this head.

🤖 Generated with Claude Code

**@⁠dragoso** on GitHub · 2026-11-24 22:44 UTC · [original](https://github.com/Marvisak/VIctory/pull/1795) `dragoso:shader/lds-sub-dword` → `main` · +562 −1 · 3 files · 2 commits · open ## What `main` now lowers byte and halfword LDS reads and writes itself (0b35b7f), with the same approach this PR had (shift and extend, the d16 halves, `and`/`or` atomics for writes). This PR is now just what that implementation doesn't cover: - **Halfwords at byte 3 of a dword** take their high byte from byte 0 of the next one, as the hardware does. An RDNA2 measurement by @⁠Lyrnc in #116 (closed; it carried an earlier version of this lowering) showed `ds_read_u16` at address 3 reads bytes 3-4 across the dword boundary, and the writes do the same. Before this, the lowering took halfwords as aligned and read or wrote the wrong bytes there. Reads now OR in the next dword's byte 0. Writes clear and set it with one more `and`/`or` atomic pair, selected so other halfwords touch nothing past their own dword. No branch or loop. A halfword at the last byte of the program's LDS has no next dword: its high byte reads 0 and its write is dropped, as out-of-bounds accesses do. A new `lds_straddle` helper sits next to `lds_shift`. - The same-byte `shortcut:` comment now says what the hardware keeps: the highest lane's value, measured on RDNA2. The lowering still leaves the OR of the values. A compare-exchange loop would be exact, but extra divergent loops have hung the AMD driver before. ## Tests `emit/lds_tests.rs`: synthetic GFX10 compute programs (hand-encoded, nothing from any title), lowered, validated by `dxil.dll` when present, and run on the hardware GPU and WARP against a model of the console's LDS: - every read form, with a preloaded VGPR for the d16 halves, and every write form, at every byte address from 2 to 7 (odd and straddling halfwords included), with a non-zero instruction offset; writes are read back through the dword holding them and the next one; - 64 lanes writing neighbouring bytes/halfwords of shared dwords (none is lost); - a halfword at the last byte of a 512-byte allocation (high byte 0, its write dropped); - a decoder check of the opcodes. They take the GPU tests' one-at-a-time lock (`valu_tests::ONE_AT_A_TIME`). On `main` they fail only for the straddling halfwords. Tested on Windows 11, NVIDIA RTX 5060 Ti and WARP: the LDS tests pass. In `cargo test --workspace --no-fail-fast`, the only failures are 5 that fail the same way on `main` 70564ae on this machine: `victory-shader` `stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride`, `stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros` and `synthetic_tests::gathers_at_a_level_match_the_hardware`, and `victory-gpu` `descriptor::host::tests::dynamic_slots_are_held_until_released` and `memory::tests::lists_share_a_table_and_copies_keep_their_views`. `cargo clippy -p victory-shader --all-targets` is clean, and `cargo xtask policy` is ok. Not run on AMD at this head. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
A halfword at byte 3 of a dword takes its high byte from byte 0 of the
next one: an RDNA2 measurement showed the hardware reads and writes the
two bytes at the address across the dword boundary, where the byte and
halfword LDS lowering took halfwords as aligned. Reads now OR in the
next dword's byte 0, and writes clear and set it with one more and/or
atomic pair, selected so other halfwords touch nothing past their own
dword. No branch or loop.

emit/lds_tests.rs runs synthetic GFX10 compute programs (hand-encoded,
nothing from any title) on the host GPU and WARP against a model of the
console's LDS: every read form (with the d16 halves) and write form at
every byte address from 2 to 7, and 64 lanes writing neighbouring bytes
and halfwords of shared dwords. They take the GPU tests' one-at-a-time
lock. On main they fail only for the straddling halfwords.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
lds_straddle indexed the dword after a halfword at byte 3 without a
bound, so a halfword at the last byte of LDS read and wrote one element
past the group-shared array. It now straddles only when that dword is
in the allocation; past the end the high byte reads 0 and its write is
dropped, as out-of-bounds LDS accesses do on the console.

The tests now take halfwords at byte 7 too (the straddle into dword 2),
and a new one reads and writes a halfword at byte 511 of a 512-byte
allocation.

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

@⁠rminzao on GitHub · 2026-11-24 23:09 UTC · original

lds_straddle has no bound on index + 1

In lds_straddle, after = index + 1 is used as an element index into the LDS array with no check against the allocation. A compute program with 512 bytes of LDS doing ds_read_u16 / ds_write_b16 at byte 511 then reads or writes element 128 of a [128 x i32] group-shared array. That's undefined and may hit a neighbouring variable. On hardware the high byte would read 0 and the write's high byte would be dropped.

This also matters with #1769: that PR bounds accesses that go through dword, but lds_straddle builds its second element directly, so even with both merged the straddling dword stays unbounded. Whichever lands second probably needs to give p2 its own in-range check (read 0, drop the write).

Small test gap: lds_tests.rs tests halfwords from byte 2 to 6, so the straddle at byte 7 (dword 1 into dword 2) isn't covered.

Static review (AI-assisted), read against main at 70564ae; nothing was run on a GPU.

**@⁠rminzao** on GitHub · 2026-11-24 23:09 UTC · [original](https://github.com/Marvisak/VIctory/pull/1795#issuecomment-7147073870) **`lds_straddle` has no bound on `index + 1`** In `lds_straddle`, `after = index + 1` is used as an element index into the LDS array with no check against the allocation. A compute program with 512 bytes of LDS doing `ds_read_u16` / `ds_write_b16` at byte 511 then reads or writes element 128 of a `[128 x i32]` group-shared array. That's undefined and may hit a neighbouring variable. On hardware the high byte would read 0 and the write's high byte would be dropped. This also matters with #1769: that PR bounds accesses that go through `dword`, but `lds_straddle` builds its second element directly, so even with both merged the straddling dword stays unbounded. Whichever lands second probably needs to give `p2` its own in-range check (read 0, drop the write). Small test gap: `lds_tests.rs` tests halfwords from byte 2 to 6, so the straddle at byte 7 (dword 1 into dword 2) isn't covered. _Static review (AI-assisted), read against `main` at 70564ae; nothing was run on a GPU._
Author
Owner

@⁠dragoso on GitHub · 2026-11-24 23:12 UTC · original

@⁠rminzao thanks, both fixed in 30b4479:

  • Bound on index + 1: a halfword now counts as straddling only when the next dword is in the program's LDS (index + 1 < lds_dwords). Past the end its high byte reads 0 and its write is dropped, as you describe for the hardware. The low byte is read and written as before, so this doesn't depend on #1769: whichever lands second, p2 is never out of range.
  • Byte 7: halfwords are now tested at every byte from 2 to 7, including the straddle from dword 1 into dword 2.
  • New test a_halfword_at_the_end_of_lds: ds_read_u16 and ds_write_b16 at byte 511 of a 512-byte allocation. One limitation: on the RTX 5060 Ti and WARP it also passes without the bound, because those devices happen to read 0 and drop the write out of range. So it pins the expected result, but on those devices it doesn't prove the bound. The bound comes from the lowering itself.

@⁠catminou4 thanks for the macOS run.

Tested on Windows 11, RTX 5060 Ti and WARP: the 8 LDS tests pass. cargo test -p victory-shader has the same 3 failures as main 70564ae on this machine (two stride_tests heap-buffer tests and gathers_at_a_level_match_the_hardware). cargo clippy -p victory-shader --all-targets is clean.

AI use: Claude Code (Claude Opus 5.5) made this change, ran the tests and posted this comment.

**@⁠dragoso** on GitHub · 2026-11-24 23:12 UTC · [original](https://github.com/Marvisak/VIctory/pull/1795#issuecomment-7147139746) @⁠rminzao thanks, both fixed in `30b4479`: - **Bound on `index + 1`:** a halfword now counts as straddling only when the next dword is in the program's LDS (`index + 1 < lds_dwords`). Past the end its high byte reads 0 and its write is dropped, as you describe for the hardware. The low byte is read and written as before, so this doesn't depend on #1769: whichever lands second, `p2` is never out of range. - **Byte 7:** halfwords are now tested at every byte from 2 to 7, including the straddle from dword 1 into dword 2. - **New test** `a_halfword_at_the_end_of_lds`: `ds_read_u16` and `ds_write_b16` at byte 511 of a 512-byte allocation. One limitation: on the RTX 5060 Ti and WARP it also passes without the bound, because those devices happen to read 0 and drop the write out of range. So it pins the expected result, but on those devices it doesn't prove the bound. The bound comes from the lowering itself. @⁠catminou4 thanks for the macOS run. Tested on Windows 11, RTX 5060 Ti and WARP: the 8 LDS tests pass. `cargo test -p victory-shader` has the same 3 failures as `main` 70564ae on this machine (two `stride_tests` heap-buffer tests and `gathers_at_a_level_match_the_hardware`). `cargo clippy -p victory-shader --all-targets` is clean. AI use: Claude Code (Claude Opus 5.5) made this change, ran the tests and posted this comment.
Author
Owner

@⁠dragoso on GitHub · 2026-11-25 00:05 UTC · original

Ran on NVIDIA at 87c86f0: all 7 LDS tests pass. The 3 failures in the full crate also fail on main; they are the ones already reported in #1835.

  • Hardware: NVIDIA GeForce RTX 5060 Ti (Blackwell, 32-lane waves), driver 32.0.16.1742, Windows 11 Pro (build 26300), Rust 1.98.1 MSVC. DXGI adapter 0 is the RTX 5060 Ti, so the default device the tests create is this card, not the AMD iGPU on the same machine.

  • cargo test -p victory-shader --lib lds: 7 passed, 0 failed. That covers the opcodes, the byte and halfword reads and writes, and the straddling halfwords.

  • cargo test -p victory-shader --lib (default parallel threads): 210 passed, 3 failed, 2 ignored. The 3 failures are:

    • stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride
    • stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros
    • synthetic_tests::gathers_at_a_level_match_the_hardware

    All three fail identically on main 70564ae on this card. Per #1835, the view heap they create is larger than NVIDIA's 1,000,000 limit, so they are unrelated to this PR.

  • No crash with the parallel test threads.

AI-assisted (Claude Code); run on our machine.

**@⁠dragoso** on GitHub · 2026-11-25 00:05 UTC · [original](https://github.com/Marvisak/VIctory/pull/1795#issuecomment-7148132532) **Ran on NVIDIA at `87c86f0`: all 7 LDS tests pass.** The 3 failures in the full crate also fail on `main`; they are the ones already reported in #1835. - **Hardware:** NVIDIA GeForce RTX 5060 Ti (Blackwell, 32-lane waves), driver 32.0.16.1742, Windows 11 Pro (build 26300), Rust 1.98.1 MSVC. DXGI adapter 0 is the RTX 5060 Ti, so the default device the tests create is this card, not the AMD iGPU on the same machine. - **`cargo test -p victory-shader --lib lds`:** 7 passed, 0 failed. That covers the opcodes, the byte and halfword reads and writes, and the straddling halfwords. - **`cargo test -p victory-shader --lib`** (default parallel threads): 210 passed, 3 failed, 2 ignored. The 3 failures are: - `stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride` - `stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros` - `synthetic_tests::gathers_at_a_level_match_the_hardware` All three fail identically on `main` `70564ae` on this card. Per #1835, the view heap they create is larger than NVIDIA's 1,000,000 limit, so they are unrelated to this PR. - No crash with the parallel test threads. *AI-assisted (Claude Code); run on our machine.*
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 06:25 UTC · original · edited 2026-11-25 22:18 UTC

Automated triage for the maintainers.

Needs rebase. This PR no longer merges cleanly into main (conflict in crates/victory-shader/src/emit/lower.rs). It will be reviewed again once it is rebased.

From the earlier needs-work note, still open: say how the straddling LDS halfword behaviour was measured on RDNA2 (tool, GPU, driver), or give console evidence or documentation for the alignment mode the console uses.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 06:25 UTC · [original](https://github.com/Marvisak/VIctory/pull/1795#issuecomment-7154783434) · edited 2026-11-25 22:18 UTC <!-- victory-triage v1 head=d035b13c6d7e184bb4c3396af5c065cad110ba17 verdict=needs-rebase at=2026-11-25T22:21:35Z --> Automated triage for the maintainers. **Needs rebase.** This PR no longer merges cleanly into `main` (conflict in `crates/victory-shader/src/emit/lower.rs`). It will be reviewed again once it is rebased. From the earlier needs-work note, still open: say how the straddling LDS halfword behaviour was measured on RDNA2 (tool, GPU, driver), or give console evidence or documentation for the alignment mode the console uses. --- _Generated by [Claude Code](https://claude.ai/code)_
This pull request has changes conflicting with the target branch.
  • crates/victory-shader/src/emit/lower.rs
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

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

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1795'
git switch 'github-bot/gh-1795'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1795'
git switch 'github-bot/gh-1795'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1795'
git switch 'main'
git merge --squash 'github-bot/gh-1795'
git switch 'main'
git merge --ff-only 'github-bot/gh-1795'
git switch 'main'
git merge 'github-bot/gh-1795'
git push origin 'main'
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!1795
No description provided.