Shaders: LDS byte and half accesses #571
No reviewers
Labels
No labels
accessibility
area: build
area: formats
area: gpu
area: loader
area: runtime
area: shader
bug
documentation
duplicate
enhancement
good first issue
gpu: amd
gpu: intel
gpu: nvidia
help wanted
host: wine
invalid
out of scope
question
request
run report
triage: confirmed
triage: has patch
triage: merge candidate
triage: needs info
triage: needs rebase
triage: needs work
triage: proposed close
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Marvisak--VIctory!571
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/571/head"
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?
@SAitken91 on GitHub · 2026-11-24 05:08 UTC · original
SAitken91:shader/lds-bytes-halves→main· +304 −4 · 2 files · 5 commits · closed without mergingds_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
ds_read_{u8,i8,u16,i16} and ds_write_{b8,b16} were refused ("an LDS read/write of bytes or halves"), with their _d16/_d16_hi forms. LDS is an array of dwords here; such an access now works on the dword holding it, at bit 8 * (address & 3): - reads shift and mask, or sign-extend the i8/i16 forms; _d16 fills the register's low half (_d16_hi its high half) and keeps the other; - writes clear the bits with an atomic and, then set them with an atomic or, so lanes writing other bytes of the same dword keep theirs; _d16_hi takes the data's high half. translate --validate on shaders.rpf: compute 1670 -> 1686 validated; both errors are gone from the list. Co-Authored-By: Claude Opus 5.5 <[email protected]>@KoLolX3 on GitHub · 2026-11-24 05:31 UTC · original
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.
@ghost on GitHub · 2026-11-24 05:50 UTC · original
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.@iurysf on GitHub · 2026-11-24 06:17 UTC · original
Tested offline against main
d2a4baewithvictory translate --list --validateover the title'sshaders.rpf+ executable (26,096 programs), dxil.dll 1.9.2602.17:AI-assisted.
@Dathinel on GitHub · 2026-11-24 06:54 UTC · original · edited 2026-11-24 07:09 UTC
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.@SAitken91 on GitHub · 2026-11-24 07:40 UTC · original
@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-address
ds_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 on d27cb78, the expected values would be0x9882/0x677d.@Dathinel on GitHub · 2026-11-24 07:47 UTC · original
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.@slint6614-dev on GitHub · 2026-11-24 09:24 UTC · original
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.
@akimfomin on GitHub · 2026-11-24 10:01 UTC · original · edited 2026-11-24 10:02 UTC
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 d27cb78960ed
AI-assisted with Codex.
@SAitken91 on GitHub · 2026-11-24 10:46 UTC · original
@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.@MrCloudy2 on GitHub · 2026-11-24 10:59 UTC · original
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).@SAitken91 on GitHub · 2026-11-24 13:42 UTC · original
@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.@adamdotpy on GitHub · 2026-11-24 15:58 UTC · original
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.
@SAitken91 on GitHub · 2026-11-24 16:42 UTC · original
@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").@adamdotpy on GitHub · 2026-11-24 16:45 UTC · original
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.
@Daniel333313131 on GitHub · 2026-11-24 22:18 UTC · original
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.
@Marvisak on GitHub · 2026-11-25 00:25 UTC · original
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