Shaders: LDS byte and half accesses #571
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/571"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from Marvisak/VIctory. Originally posted by SAitken91 on 2026-11-24T05:08:41Z.
ds_read_{u8,i8,u16,i16}andds_write_{b8,b16}, with their_d16/_d16_hiforms, were refused ("an LDS read/write of bytes or halves"). That blocked 16 compute programs in shaders.rpf, mostlycompute_graph_opsreductions and transposes.Change
LDS is an array of dwords in the translation, so these accesses now work on the dword holding them, at bit
8 * (address & 3). They are naturally aligned, so they never straddle two dwords._d16fills the register's low half and_d16_hiits high half, keeping the other half._d16_hitakes the data's high half.Results
victory translate --validate shaders.rpf: compute validated 1670 → 1686 (all 16). Both errors are gone from the list.Testing
cargo test -p victory-shader: 69 passed. Clippy and rustfmt are clean.This is independent of #539, #554, #563, #565 and #569.
Written with the help of Claude (AI).
🤖 Generated with Claude Code
Imported from Marvisak/VIctory. Originally posted by KoLolX3 on 2026-11-24T05:31:27Z.
Check on Linux, not Windows: pass. Head
6e981fb26merged ontomaind2a4bae1f(clean merge).core,formats,game,loader,pe,pipeline,shader): PR92 passed, 7 failed,main92 passed, 7 failed.cargo xwin check --workspace --target x86_64-pc-windows-msvc, coversvictory-gpu,victory-hleand the app): PR ok,mainok.main168.mainhere as well (not this PR): 7 tests invictory-loaderandvictory-pethat parse their own test binary as a PE image, which is an ELF file on Linux.victory-gpu/victory-hletests and the game itself.On: AMD Ryzen 7 5700, 62 GB RAM, Artix Linux 7.3.4-artix1-2, rustc 1.98.1 (the repo's pinned toolchain), cargo-xwin 0.24.1. No game data. Automated check only: the code itself was not reviewed.
Imported from Marvisak/VIctory. Originally posted by TrashTalker3460 on 2026-11-24T05:50:56Z.
Review note on head
6e981fb: fords_read_*_d16/_d16_hi,crates/victory-shader/src/emit/lower.rs:2135keeps the destination's other half by reading its old value withinst.read(Reg::V(reg)), and falls back to 0 when that isNone.On
main,ir.rseffects()(theFields::Dsarm, around line 842) adds ads_readdestination only to the written registers, andInst::readonly searches the read list. So for these readsinst.readis alwaysNoneand the kept half is always zeroed: a_d16read clears the high 16 bits, a_d16_hiread clears the low 16.#116 fixes this in
effects():This PR would need the same, plus a test that writes a known value to the destination first and checks the other half survives.
Read-only review, nothing built. AI-assisted (Claude Code); the claim above was checked against this head and
mainatd2a4bae. Context: the triage in #366.Imported from Marvisak/VIctory. Originally posted by iurysf on 2026-11-24T06:17:05Z.
Tested offline against main
d2a4baewithvictory translate --list --validateover the title'sshaders.rpf+ executable (26,096 programs), dxil.dll 1.9.2602.17:AI-assisted.
Imported from Marvisak/VIctory. Originally posted by Dathinel on 2026-11-24T06:54:36Z.
Follow-up: the translated readback is now available. Claude Code ran the test on our Windows 11 machine using #571 at
6e981fb2on based2a4bae1(test merge16b4d6a). On both RTX 3050 Laptop and WARP, all 64 lanes agreed. With the first fixture the aligned value was0x8281, but the crossing read returned0x0082(little-endian two-byte reference0x9882). With complemented inputs, the aligned value was0x7d7eand the crossing read was0x007d(reference0x677d). Both crossing results retained just the last byte of the first dword. This supplies the requested host measurement; the console alignment mode remains unknown.One remaining distinction is a halfword read across a dword boundary. In the native RDNA2 experiment on #116, an odd-address
ds_read_u16read the two bytes at that address in the tested desktop HIP mode. Our translated readbacks used aligned halfwords.If anyone already has this PR built on Windows with D3D12, could you report the translated result for one aligned control and one crossing read? In the synthetic test module, inside
byte_and_halfword_reads, keep only theds_read_u16entry and replacefor at in addresses(name)withfor at in [0u32, 1]. The instruction adds 2. For the first fixture the effective byte addresses are 2 and 3, both inside the eight initialized bytes. The little-endian reference values are0x8281and0x9882.The setup and Cargo command are here. Filter on
emit::vic13_lds::byte_and_halfword_readsto run just this test. The PR SHA, adapter, and result (or first assertion showing actual/expected) are enough. This would tell us how the translation compares with that measured desktop mode. The console's alignment mode remains an open question. AI-assisted.Imported from Marvisak/VIctory. Originally posted by SAitken91 on 2026-11-24T07:40:12Z.
@TrashTalker3460 thanks for the careful read. I checked it on this head:
ir.rsalready makes every VGPR write also read the register's old value (effects(), theif vectorblock: "Lanes disabled in EXEC keep the old value"), and DS instructions take that path, soinst.read(Reg::V(reg))isSomefords_read_u16_d16/_d16_hi. A quick IR test (ds_read_u16_d16 v2/_d16_hi v3) passes without theeffects()change, so the kept half isn't zeroed here. Happy to add that test if it's useful.@Dathinel thanks for the readback — that one was a real gap: I had assumed halves never straddle. Pushed
d27cb78: a 16-bit access at byte 3 now uses the next dword too (reads shift the two dwords as one 64-bit value; writes clear and set bits in both, no-ops on the next dword when nothing straddles; the next index is clamped to the last LDS dword). It matches the odd-addressds_read_u16behaviour measured on native RDNA2; the console's alignment mode is still an open question. Validation is unchanged (compute 1686). If you can rerun your crossing fixture ond27cb78, the expected values would be0x9882/0x677d.Imported from Marvisak/VIctory. Originally posted by Dathinel on 2026-11-24T07:47:34Z.
Confirmed on your exact head
d27cb78960ed. The sameds_read_u16fixture now returns0x9882and0x677dat byte 3, on both the RTX 3050 Laptop and WARP. On6e981fb2the corresponding results were0x0082and0x007d. The aligned control at byte 2 still returns0x8281and0x7d7e. All 64 lanes agreed in every dispatch.I reran
cargo test --frozen -p victory-shader --features host --lib emit::vic13_lds::byte_and_halfword_reads -- --exact --nocapturewith the same synthetic module on Windows 11. This verifies the crossing read against the measured desktop RDNA2 behavior. Thanks for fixing it. AI-assisted.Imported from Marvisak/VIctory. Originally posted by slint6614-dev on 2026-11-24T09:24:21Z.
Measured on the full retail shader pack (26,503 containers) with
victory translate --validate --pipelines(pipelines created on an AMD RX 9070, Windows 11, dxil.dll from SDK 10.0.26100). Counts are emitted / validated / pipelines. This PR was merged locally with current mainc7a5f49first (main: VS/PS 12224 / 12148, CS 1440 / 1417, MS 8163 / 8101; it includes #612).CS 1456 / 1433 (+16 validated): all 7 "LDS read of bytes or halves" and 9 "LDS write of bytes or halves" refusals are gone, and the 16 programs validate. VS/PS and MS unchanged. Same result as #101 on the same pack.
Imported from Marvisak/VIctory. Originally posted by akimfomin on 2026-11-24T10:01:11Z.
I reran two focused LDS regressions on
d27cb78960edusing synthetic D3D12 compute shaders. Both passed on an RTX 4070 Ti SUPER and WARP. Both failed with the previous emitter.ds_read_u16check:ds_read_u16_d16_hiat byte 3, with LDS words0xAB123456and0xDEADCDBEand initial destination0xCAFE1357, returned0xBEAB1357.PortBridge surfaced XCL #544 for the partial-destination cases and Continued #507 for the single-writer fixture correction. Would these be useful in your
valu_tests.rsharness?Test-only patch for
d27cb78960AI-assisted with Codex.
Imported from Marvisak/VIctory. Originally posted by alexander-src on 2026-11-24T10:06:14Z.
Review: COMMENTED
NVIDIA Blackwell (
--validate --pipelines) verification and code review ofd27cb78merged cleanly ontomain8e79c80:621.24), Rust1.98.1(x86_64-pc-windows-msvc), Windows SDK10.0.26100.0dxil.dll.cargo clippy --workspace --all-targets --locked -- -D warnings: passcargo clippy --workspace --all-targets --all-features --locked -- -D warnings: passcargo test -p victory-shader --locked -- --test-threads=1: 89 passed, 0 failed, 1 ignoredvictory translate --validate --pipelines, 26,599 containers / 26,148 programs):an LDS write of bytes or halves9 → 0,an LDS read of bytes or halves7 → 0).1,440 / 1,417 / 1,121→1,456 (+16) / 1,433 (+16) / 1,123 (+2)(all 16 newly emitted programs validate withdxil.dll; the 2 Wave32 programs create PSOs on RTX 5090, while the other 14 are Wave64 and hit NVIDIA's 32-lane wave limit as expected). VS/PS and MS are unchanged; zero regressions.One edge-case bug in the straddling-halfword path (
crates/victory-shader/src/emit/lower.rs):When a 16-bit access lands at byte 3 of the final LDS dword (
index == lds_dwords - 1,byte == 3,sh == 24),n = select(past, last, index + 1)clampsqback ontop(&lds[last]):ds_write_b16): the second loop iteration(q, true)hasmv = 0x0000_00FFandxv = (x >> 8) & 0xFF, soatomicrmw and/oronq == poverwrites byte 0 oflds[last]with the high byte instead of dropping the out-of-bounds byte.ds_read_u16/i16):hi = load(q)reads byte 0 oflds[last]into the upper byte of the result instead of0.Masking
hion read (select(past, 0, hi)) and masking(mv, xv)to(0, 0)whenpastis true on write (reusing thepastpredicate already computed at line 289) fixes both cases cleanly.AI-assisted code review and local verification on this machine; no game data.
Imported from Marvisak/VIctory. Originally posted by SAitken91 on 2026-11-24T10:46:25Z.
@alexander-src confirmed and fixed in
ce45c67: at byte 3 of the last LDS dword the clamped next dword is the same dword, so the read now zeroes its high byte (select(past, 0, hi)) and the write's mask and data for the next dword are 0 whenpast, matching out-of-bounds LDS behaviour. Validation unchanged (CS 1686 ond2a4bae). @akimfomin thanks for the extra D16_HI / crossing regressions.Imported from Marvisak/VIctory. Originally posted by MrCloudy2 on 2026-11-24T10:59:58Z.
Tester result: +2 of 339 previously failing world programs now emit (compute 32 → 34 of 272; the two that logged
an LDS write of bytes or halves is not supported). Merged together with #160 and #539 the gains add up exactly: vertex/pixel 11, compute 41.How measured: 339 shader containers from my own 1.0.2.1 copy, the distinct programs that logged
shader does not translatein two world runs, each matched to its container by the BLAKE3 VIctory logs. Each PR was merged on its own onto one base:main8e79c80+ #151 #190 #454 #493 #579 + #177's key fix + the wave64 stack via #439. I then ranvictory translate <folder>(diag build, CPU only) and compared DXIL emitted per stage against the base (vertex/pixel 0 of 25, compute 32 of 272, mesh 0 of 13). Linux, Proton-CachyOS wine, cross-built with xwin, RTX 3060 host (only matters for the wave64 part of the base). Translation counts only, not image correctness. AI-assisted (Claude Code).Imported from Marvisak/VIctory. Originally posted by mercuryVM on 2026-11-24T11:46:21Z.
Review: COMMENTED
Tested
ce45c67on Windows 11, RTX 5070, MSVC:cargo test -p victory-shader -- --test-threads=169 passed, andcargo clippy -p victory-shader --all-targetsis clean.victory translate --validate --pipelineson the local title executable still reports 12 of 12 programs (none of them use these ops). The PR adds no tests.The lowering reads correctly to me:
8 * (addr & 3), then mask or sign-extend. The_d16/_d16_hiforms merge into the low or high half and keep the other half.Before approving, I'd like it pinned by a GPU test in
valu_tests.rs(the harness already runs compute programs with LDS). Cover:ds_write_b8from four lanes into one dword, followed byds_read_b32: checks the clear-then-set path doesn't lose neighbours.ds_read_i8/ds_read_i16of a negative value: sign extension.ds_read_u16_d16_hiinto a register with a known low half: the merge.The byte-lane arithmetic is easy to get wrong in ways these 16 programs would only show as bad data.
AI-assisted (Claude Code).
Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-24T12:44:24Z.
Review: COMMENTED
Careful lowering for the LDS sub-dword family: byte/half reads with sign extension for the
_ivariants,_d16/_d16_hipartial-register semantics, the straddling half at byte 3 handled via a 64-bit pair with out-of-bounds clamping, and writes done as atomic And+Or pairs so lanes writing other bytes of the dword keep theirs.One question on the write path: the And+Or pair is two separate atomics, so it is not atomic as a whole. Within one wave the lanes are lockstep enough that the comment's reasoning holds, but a full-dword
ds_write(or another sub-dword write to overlapping bits) from a different workgroup could land between the And and the Or and be clobbered. Nativeds_write_b8/b16is a single instruction with byte enables, so hardware doesn't have this window. Is the race acceptable for the titles in scope, or should the write go through a single atomic RMW (e.g. oneatom.add-style read-modify-write with a mask)?Also: no test added — neither a lowering unit test nor a
valu_testsexecution case for these instructions, so the 96 green macOS tests don't exercise any of this. A host-execution test with mixed byte/half/dword writes to the same dword would be the real proof.Test-merge with current main (
ffe310e) clean;cargo test -p victory-shader --locked(macOS arm64, rustc 1.98.1) → 96 passed / 0 failed (no regression, new path untested).Imported from Marvisak/VIctory. Originally posted by SAitken91 on 2026-11-24T13:42:31Z.
@catminou4 good question. LDS is private to one thread group, so a write from a different workgroup can't land in the window. Within a group the window is real but narrow: an unsynchronized full-dword
ds_writefrom another wave between the And and the Or would leave the OR of both values in that byte, where hardware would keep one or the other. That's already an unordered race on the console (no barrier between the waves), but the result can differ, so a single CAS loop (cmpxchguntil the old value matches) would close it if the maintainer prefers exact behaviour over two atomics. On tests: agreed — the coverage so far is hardware readbacks on other machines (Dathinel, akimfomin, for the straddle and D16_HI cases); a host-executionvalu_testscase with mixed byte/half/dword writes to one dword would be the right regression.Imported from Marvisak/VIctory. Originally posted by adamdotpy on 2026-11-24T15:58:01Z.
Reviewing this against #383, which covers some of the same LDS ground, since the two will have to be squared with each other before either lands.
Where this is better than #383. #383 documents the halfword at byte 3 as a known shortcut: it keeps the write inside one dword and loses the byte that spills into the next. This PR implements the straddle properly — a 64-bit shift of the two dwords for the read (
flags::INT64), a 64-bit mask/data pair for the write, and the past-the-end case clamped to the last dword with reads seeing 0 and the dropped byte not written.lds_halves_straddle_two_dwordscovers it, and @akimfomin's0xBEAB1357regression exercises the same path end to end. For LDS alone this is the PR I would land, and #383 would go back for a rebase keeping only itsimage_sample_oand descriptor-data work.Verified here (Windows,
d24c6e4):cargo test -p victory-shader— 123 passed, 1 ignored, including the four new LDS tests; clippy--all-targets --all-features --locked -D warningsclean; release build ofvictoryfine; the file isrustfmt --edition 2024 --checkclean. I did not run the retail shader pack, so I am relying on @slint6614-dev's +16 validated CS containers and @MrCloudy2's +2 of 339 for the translation-rate evidence.One thing worth a test: the kept half comes from
inst.read(Reg::V(dst)). Inlower.rsthe_d16/_d16_hiread path doesand
ir.rs::effects()for the DS form feedsaddranddatainto the read set whiledstonly goes into the write set, so the destination register is not declared as read at all. It works in the tests becausevdstand the register holding the value coincide. Two experiments on the branch:data0field fromv0tov1inlds_sub_dword_reads_extend_and_keep_halvesleaves the test green, which confirms the value is not taken from the data field — it really isReg::V(dst).v2withv0as the data field andv2preloaded fromv3gave0x2211aa99where console semantics want0x6655d0c0(kept half0xd0c0from the destination's previous value, read half0x6655from LDS). I encoded the preload by hand withvop1, so the preload itself may be what is wrong rather than the emitter — but either way the casevdst≠ the value the emitter resolves for it is untested, and a test with the destination's previous value distinct from the data field would pin it down. If #116 changes this, a// fixed by #116note here saves the next reader a day.Smaller points.
self.s.lds_dwords - 1assumes LDS is non-empty; that is true for every legal allocation, but asaturating_subor a debug assert would make the assumption explicit iflds_dwordsever becomes configuration-driven.base == nameis the full-dword case and theelsebranch is what everything else falls back to; naming the condition (is_full_dword) would help a reader who lands on thelet x = if base == name { x }line cold.Nothing here is a request for a change to the straddle logic — it looks right, and the outside-in validation is unusually strong. The asked-for item is only the destination-register test.
Imported from Marvisak/VIctory. Originally posted by SAitken91 on 2026-11-24T16:42:55Z.
@adamdotpy thanks for the careful pass. Added the case you asked for in
8ad9d9f:lds_d16_hi_keeps_the_destinations_own_low_halfreadsds_read_u16_d16_hiinto v6 (loaded from v4 first), with v2 in the data field and v5 as the address; v6 ends upv4.lo | lds.lo << 16, passing on an RX 6950 XT. The kept half does come from the destination:ir.rseffects()ends with "lanes disabled in EXEC keep the old value", which adds every VGPR write to the read set, DS included — soinst.read(Reg::V(dst))isSome(TrashTalker3460 raised the same point earlier; an IR test confirmed it). On your second experiment: with v3 =0xCCBBAA99as the preload and0x44332211in the LDS dword,0x2211aa99is the expected result (kept0xaa99, read0x2211), so I think the preload/expectation was what differed rather than the emitter. Thelds_dwords - 1is guarded: an LDS access in a program that allocates none is refused earlier ("LDS the program does not allocate").Imported from Marvisak/VIctory. Originally posted by adamdotpy on 2026-11-24T16:45:27Z.
Verified
8ad9d9fhere (Windows 11 / Rust 1.98.1, isolated target dir):cargo test -p victory-shader --lib124 passed / 1 ignored, withlds_d16_hi_keeps_the_destinations_own_low_halfrunning on the hardware GPU and passing; clippy--all-targets --all-features --locked -D warningsclean.Point by point:
The destination-register question — you were right and my review was wrong, and here is the evidence. I re-read
ir.rs::effects()and missed its tail: after the per-fields match, for every vector instruction it doesr.extend(w.iter().filter(|w| matches!(w, Reg::V(_))))under the comment "Lanes disabled in EXEC keep the old value". So every VGPR write, DS destinations included, is also in the read set, andinst.read(Reg::V(dst))isSome— the kept half does come from the destination's own previous value by construction, not by the tests' coincidence. My "effects()declaresdstonly as written" claim was based on the DS arm alone and is corrected here; the new test pins it empirically anyway (destination v6 preloaded from v4, data field v2, expectedv4.lo | lds.lo << 16, which differs from v2's half in every case row, so a data-field read would fail).My
0x2211aa99experiment — agreed, that was my expectation being wrong, not the emitter: with the LDS dword0x44332211and the kept half0xaa99(v3's low half, i.e. the destination's preload did land),0x2211aa99is exactly kept-from-destination plus read-from-LDS. I had the read half backwards in my expected value. With the tail ofeffects()in view, that run was consistent with correct behaviour all along.lds_dwords - 1— noted, guarded by the earlier "LDS the program does not allocate" refusal. Mysaturating_subsuggestion was belt-and-braces for a case that cannot be reached; no change needed.The straddle and two-atomics points stand as reviewed. With this test added, the LDS half of the branch has both the hardware readbacks from other machines and a host-execution case where the destination is distinct from the data field — nothing left open from my side on #571.
Imported from Marvisak/VIctory. Originally posted by Daniel333313131 on 2026-11-24T22:18:36Z.
Translation census for this PR and the two others that lower byte and half LDS accesses (#116, #101). Each was merged alone onto
mainffe310e.Setup: release build,
victory translate <dump>/shaders.rpf --list(26,084 programs, no--validate, no--pipelines), Windows 11, RX 7900 XTX.mainffe310e8ad9d9fc853a6a9fbd7e9AI use: Claude Code (Claude Opus 5.5) merged and built the commits, ran the census, compared the outputs and wrote this comment. The numbers come from the runs above.
Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T00:25:17Z.
Automated triage for the maintainers. Closing because this change is already in
main, ormainhas since replaced the code it patches.LDS byte and half accesses. In main since today (lower.rs:3044-3141). Same as #116.
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