Skip to content

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

Open
dragoso wants to merge 2 commits into
Marvisak:mainfrom
dragoso:shader/lds-sub-dword
Open

dragoso wants to merge 2 commits into
Marvisak:mainfrom
dragoso:shader/lds-sub-dword

Conversation

@dragoso

@dragoso dragoso commented Nov 24, 2026 •

Copy link
Copy Markdown

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 Shaders: LDS halfwords straddling two dwords, and hardware tests of byte and halfword LDS accesses #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

@catminou4 catminou4 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested locally on macOS arm64 (rustc 1.98.1): PR head merged cleanly into current main (70564ae), cargo test -p victory-shader --locked → 118 passed, 0 failed. GPU execution tests (valu_tests/host) are #[cfg(all(test, windows))] and run only on Windows; macOS covers lowering/structure unit tests.

LDS halfwords straddling two dwords + hardware tests of byte/halfword access: build_cs/run/halfword/model_read harness, halfword() classifier. 21K diff — the straddling-dword case is the classic LDS aliasing bug; good that it has hardware-side tests (Windows-gated).

@rminzao

rminzao commented Nov 24, 2026

Copy link
Copy Markdown

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.

@dragoso

dragoso commented Nov 24, 2026

Copy link
Copy Markdown
Author

@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 Shaders: LDS accesses outside the program's allocation read 0 and drop their writes #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

dragoso commented Nov 25, 2026

Copy link
Copy Markdown
Author

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 NVIDIA D3D12 (70564ae): stride_tests & synthetic_tests fail due to HEAP_VIEWS (2M > 1M NVIDIA limit) and Stride::Structured with rawBufferLoad #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
dragoso force-pushed the shader/lds-sub-dword branch from 30b4479 to 949aff8 Compare November 25, 2026 00:09
dragoso and others added 2 commits November 24, 2026 21:40
…yte and halfword LDS accesses

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 <noreply@anthropic.com>
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 <noreply@anthropic.com>
@dragoso
dragoso force-pushed the shader/lds-sub-dword branch from 949aff8 to 8da3572 Compare November 25, 2026 00:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants