Skip to content

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

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

dragoso wants to merge 3 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 in this PR's comments 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 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 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

@ghost

ghost commented Nov 24, 2026 •

Copy link
Copy Markdown

Correction (later the same night): with more runs, main crashes too. cargo test -q -p victory-shader --lib on main 9aef3d9 gave 0xC0000005 in 3 of 25 runs on this card, so the crash itself is already on main (parallel GPU tests on AMD). This PR's two tests make it far more frequent (4 of 5 runs with the lds filter) but don't cause it. The table below stands; its "0 crashes in 12 runs" row was just too few runs.

AMD run: the new tests pass one at a time, but the test process crashes (0xC0000005, access violation) when byte_and_halfword_writes and byte_and_halfword_reads run at the same time, which is cargo's default.

Head 8504777 as is (it conflicts with current main 9aef3d9), on an AMD Radeon RX 9060 XT (RDNA4), driver 32.0.32029.2010, Ryzen 7 5800X3D, Windows 11 Pro 26200, Rust 1.98.1, debug:

Run (cargo test -q -p victory-shader …) Exit codes
lds (default threads) c0000005 0 c0000005 c0000005 c0000005
lds -- --test-threads=1 0 0 0 0 0
each of the 4 lds_tests alone (--exact), 3 runs each all 0
--exact writes + reads c0000005 0 0 c0000005 0 0
--exact writes + lanes_share_dwords all 0 (6 runs)
--exact reads + lanes_share_dwords all 0 (6 runs)
--lib on this head 2 crashes in 9 runs
--lib on your base 8fe0b74 0 crashes in 6 runs
--lib and valu_tests on main 9aef3d9 (7 GPU tests also in parallel) 0 crashes in 12 runs

So it comes from these two tests running side by side, not from parallel GPU tests in general. The output stops after the non-GPU tests print ok, so it dies inside the GPU part. Each test makes its own hardware and WARP Host, so I couldn't tell which of the two crashes. A one-at-a-time run of cargo test --workspace passes (250 / 0 / 3 on this head), but the default parallel run is what CI and other testers will hit.

AI use

  • Claude Code (Claude Opus 5.5) ran the commands and wrote this report from the logs; I checked it before posting.

@DaviTostes

Copy link
Copy Markdown

Tested 8504777 on Arch Linux x86_64, rustc 1.98.1:

No game data used. AI-assisted (DeepSeek V4.1 Flash coding agent).

@Lyrnc

Lyrnc commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

RDNA2 hardware measurement of the read forms and of the two shortcut: cases: every read form matches. The two shortcut cases behave differently on the hardware, so here is what it does in case you want to close them.

Setup. AMD Radeon iGPU of a Ryzen 7 9800X3D, gfx1036 (RDNA2), AMD driver 32.0.21042.62, Windows 11 Pro 26200. A HIP kernel (assembled by the AMD driver's amd_comgr), one wave of 32: 16 lanes store distinct dwords to LDS, then lane i reads at byte address i with each form. The d16 destinations are preset to 0xaaaa5555. Then all 32 lanes write the same byte, and the same halfword, at once. RDNA2 desktop silicon in HIP's LDS alignment mode, not necessarily the console's mode. No game data.

Form Little-endian model (zero/sign extension, d16 keeps the other half)
ds_read_u8, ds_read_i8 32/32, 32/32
ds_read_u16, ds_read_i16 32/32, 32/32
ds_read_u8_d16, ds_read_u8_d16_hi, ds_read_i8_d16 32/32 each
ds_read_u16_d16, ds_read_u16_d16_hi 32/32 each

So the extension and the kept half are as in this PR.

The two shortcut cases:

  • Halfwords at odd addresses. The 32 addresses include 1, 3, 5, …; 3, 7, 11, … straddle two dwords. The hardware read the two bytes at the address in every case, across the dword boundary too (ds_read_u16 at 3 gives bytes 3-4). Under the "2-aligned" shortcut an odd address reads the wrong bytes. This may depend on the LDS alignment mode the console sets, which I can't see from here.
  • Lanes writing the same bytes at once. 32 lanes each wrote lane + 1 to byte 0x101 with ds_write_b8: the dword became 0x00002000, so byte 1 = 0x20, the highest lane's value. Or-ed together it would be 0x3f. Same with ds_write_b16 of (lane + 1) * 0x0101 to 0x202: 0x20200200, lane 31 again. So the console-side answer for the same-byte race is "highest active lane wins". A per-lane compare-exchange would match it, at the cost of the loop you wanted to avoid.

AI use: Claude Code (Claude Opus 5.5) wrote the probes, ran them on the GPU above and drafted this comment; the numbers come from the hardware run.

@Exorion1er

Copy link
Copy Markdown
Contributor

Three PRs implement LDS byte and halfword accesses, and they differ in coverage:

d16 / d16_hi reads halves straddling a dword tests vs main
this PR yes, and the destination is marked as read in ir.rs no (its comment says aligned halves never straddle) new lds_tests.rs, 4 tests conflicts
#571 yes, but no ir.rs change (so the old VGPR may not be kept) yes (64-bit load and shift) none clean
#632 no (only _d16_hi writes) no 1 test conflicts

So they're alternatives, not copies: this PR is the most complete and best tested, and #571 is the only one handling straddling halves. They all edit the same code in lower.rs.

Checked by reading the diffs. Not run. AI-assisted (Claude Code).

@catminou4

Copy link
Copy Markdown
Contributor

Triage note: same feature as #101 and #632 — byte/halfword LDS reads and writes were refused with "an LDS read/write of bytes or halves".

This one currently has mergeable=false against main and additionally changes ir.rs/mod.rs — worth a look at whether that broader surface is needed, or whether #101's narrower change suffices. Flagging the triple overlap so review effort isn't tripled.

ds_read_{u,i}{8,16}, their _d16/_d16_hi forms, and ds_write_b{8,16}
(and _d16_hi) went untranslated. Reads load the dword holding the data,
shift it down and extend it; the d16 forms fill one half of the VGPR and
keep the other, which the IR now records as a read of the destination.
Writes clear the bytes and set them with two atomics, so lanes writing
other bytes of the same dword at once keep theirs.

Synthetic GFX10 compute programs check every form against a model of
the console's LDS on the host GPU and WARP, including 64 lanes writing
neighbouring bytes of shared dwords.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The IR already records every vector instruction's destination as a
read (for the lanes it leaves alone), so a ds_read_*_d16* sees the half
it keeps without a rule of its own. The d16 tests pass on the GPU and
WARP without it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A halfword at byte 3 of a dword takes its high byte from byte 0 of the
next one: an RDNA2 measurement on the PR showed the hardware reads (and
writes) the two bytes at the address across the dword boundary, where
the lowering took halfwords as 2-aligned. Reads now also load the next
dword and writes clear and set its byte with two more atomics; neither
adds a branch or loop.

The tests run every form at every byte address from 2 to 7, odd and
straddling halfwords included, over three dwords of LDS, and the write
test reads back the next dword as well. They fail on the previous
lowering and pass on the GPU and WARP.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@dragoso

dragoso commented Nov 24, 2026 •

Copy link
Copy Markdown
Author

Thanks for the runs and the review. Where things stand at c853a6a, rebased on current main with no conflicts:

  • Parallel crash on AMD (@nullmarsh): the LDS tests now take the same one-at-a-time GPU lock as the VALU and waterfall tests (valu_tests::ONE_AT_A_TIME, from 74a9d33), so they no longer dispatch at the same time as each other or the other GPU tests. cargo test --workspace passes with the default parallel threads.
  • Straddling halfwords (@Lyrnc): fixed in c853a6a. A halfword at byte 3 now takes its high byte from byte 0 of the next dword, for reads and writes, without a branch or loop. The tests now cover every byte address from 2 to 7, odd and straddling halves included. They fail on the previous lowering and pass on the GPU and WARP.
  • Same-byte race (@Lyrnc): kept loop-free. The shortcut: comment now says the hardware keeps the highest lane's value (your measurement), not one arbitrary lane's.
  • ir.rs surface (@catminou4): that change is gone. The IR already records a vector destination as a read, so the d16 forms keep the other half without it. The PR now touches only lower.rs, mod.rs (test module) and the new lds_tests.rs.
  • Overlap with Shaders: lower sub-dword LDS reads and writes #101 / Shaders: LDS byte and half accesses #571 / Shaders: LDS reads and writes of a byte or half #632: this one now covers the d16 forms, straddling halves and tests, so it should cover everything the other three do.

Tested on Windows 11, NVIDIA RTX 5060 Ti and WARP: cargo test --workspace ok, cargo clippy -p victory-shader --all-targets clean, cargo xtask policy ok. Not run on AMD.

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

@ghost

ghost commented Nov 24, 2026

Copy link
Copy Markdown

@dragoso c853a6a on the card that crashed before: RX 9060 XT (RDNA 4), Adrenalin 32.0.32029.2010, Windows 11. I ran the victory-shader test binary with its default parallel test threads: 125 passed, 0 failed, 1 ignored. All four LDS tests passed: opcodes, byte_and_halfword_reads, byte_and_halfword_writes and lanes_share_dwords. The earlier 0xC0000005 when the LDS tests overlapped did not occur in 1 run.

Not tested: the full cargo test --workspace.

AI-assisted (Claude Code, Claude Opus 5.5); no game data.

@Marvisak Marvisak closed this Nov 24, 2026
@dragoso dragoso changed the title Shaders: byte and halfword LDS reads and writes Shaders: LDS halfwords straddling two dwords, and hardware tests of byte and halfword LDS accesses Nov 24, 2026
@dragoso

dragoso commented Nov 24, 2026

Copy link
Copy Markdown
Author

Rescoped at 87c86f0. main now has its own byte and halfword LDS lowering (0b35b7f), the same approach as this PR, so I dropped this PR's version and rebased what's left onto main 70564ae:

  • the straddling halfwords at byte 3 (@Lyrnc's RDNA2 measurement), on top of main's code;
  • lds_tests.rs, ported to range::build and the new HostShader fields.

On plain main the tests fail only for the straddling halfwords; with this PR they pass on the RTX 5060 Ti and WARP. That also makes #101, #571 and #632 mostly redundant with main. Only #571 handled the straddling case.

@ghost: thanks for the RX 9060 XT run. That was on the previous head. The tests haven't changed in substance, but this head isn't tested on AMD.

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

@dragoso

dragoso commented Nov 24, 2026

Copy link
Copy Markdown
Author

The rescoped change (straddling halfwords on top of main's own byte/halfword LDS lowering, plus the hardware tests) is now in #1795.

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.

6 participants