Shaders: private (scratch) memory as a per-lane array #265
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!265
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/265/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?
@r4tedx on GitHub · 2026-11-24 01:54 UTC · original
r4tedx:shader/scratch-memory→main· +846 −2 · 5 files · 4 commits · closed without mergingMeasured on the title's shaders (by a tester)
EduAzevedo ran
victory translate --jobs 8 --listover the retail shaders (v1.0.2.1, 26,148 programs) onmaind2a4baeand on this head7463484, compared program by program (result): 0 programs gained, 0 programs lost. No refusal reason changes.So this PR does not change what translates in this title today. It adds coverage and is tested on synthetic programs only; it can wait behind PRs that unblock programs the title uses.
What
scratch_load_*andscratch_store_*were refused ("flat memory"), so a program that keeps a local array with a dynamic index, or spills to memory, failed to translate and its draws or dispatches were dropped. No open PR covers private memory.allocaofi32in the entry block, as DXC makes local arrays. A FLAT scratch address (a VGPR, an SGPR or neither, plus the 12-bit signed offset) is a byte offset into the lane's own memory; the hardware interleaves the lanes itself, so no lane index enters the address (RDNA 2 ISA, "Scratch"; LLVM AMDGPUUsage, "Private Segment"; Mesa's ACO uses these instructions the same way)._d16/_d16_hiforms (one half of the VGPR written, the other kept). Loads write under the lane guard; stores run under it.bfe, min/max), plus the access width. Where an address has no such bound (a loaded or loop-carried index) the array is 4 KiB per lane, marked as ashortcut:. At most 64 KiB. Accesses past the end read 0 and drop writes; the index is checked, never wrapped.saddr0x7F (LLVM's "ST" mode: neither address register) was read as an SGPR base ofexec_hi; it now has none.alloca.Still refused, each with its reason: generic
flat_*(the address may be private, shared or global memory), MUBUF access through a scratch descriptor (cannot be told apart from a buffer the title bound), scratch loads into LDS.Not verified
No game files were used, and this matters more here than usual:
scratch_*, this PR does not help those shaders.s_getpc_b64, a load from the global information table,s_setreg FLAT_SCR) run throughresource::trackfails with "an address from a root parameter that is no address", so programs with that prologue are still refused before they reach this code. Someone with the title's shaders can tell fromvictory translate --listwhat scratch shaders now stop at; a follow-up could skip tracking for loads whose results only feedFLAT_SCR.Tested
Windows 11, NVIDIA RTX 5070 and WARP, synthetic only. Hand-assembled 64-lane compute programs, every lane compared with values computed on the CPU, each also through
dxil.dll:With
main'slower.rs,fields.rsandbitcode.rsall four fail withUnsupported { what: "flat memory" }. Unit tests cover the size bound (exact, fallback, maximum) and generic flat staying refused.cargo test -p victory-shader: 75 passed. Rebased ontomain8d23f07(heade543a41): #104 moved the writer's liveness pass intoModule::live, soallocais now one arm of its operand visitor; #117's and #612's tests sit beside this PR's inemit/lower.rs. 94 passed, 1 ignored, on the RTX 5070.The second commit (
e543a41) is not part of this change. Onmain8d23f07the test target ofvictory-shaderdoes not compile (issue #719): #612 gave the test helperlowersa second argument and #117'sscalar_bit_tests_lowerstill calls it with one. The commit passes 0 there so this branch's tests can run; it goes away whenmainhas the fix.cargo clippy -p victory-shader --all-targets: nothing new.cargo xtask policy: ok.Does not touch
fn image,fn ds,fn bufferor wave/lane code; a trial merge with #54, #160, #164, #189 and #192 is clean.The title was not used in these tests. AI-assisted.
🤖 Generated with Claude Code
scratch_load_* and scratch_store_* were refused ("flat memory"), so any program that keeps a local array with a dynamic index, or spills to memory, failed to translate and its draws or dispatches were dropped. Each lane's private memory is now an alloca of i32 in the entry block, as DXC makes local arrays. A FLAT scratch address (a VGPR, an SGPR or neither, plus the 12-bit signed offset) is a byte offset into the lane's own memory: the hardware interleaves the lanes' dwords itself (RDNA 2 ISA, "Scratch"; LLVM AMDGPUUsage, "Private Segment"; Mesa ACO uses these instructions on GFX9+ the same way), so no lane index enters the address. Lowered: dword x1-x4 loads and stores, ubyte/sbyte/ushort/sshort loads, byte/short stores, and the _d16 / _d16_hi forms (one half of the VGPR written, the other kept; stores of the high half). Loads write under the lane guard; stores run under it. The size comes from the code: an upper bound of every scratch address from the instructions computing it (constants through and, shifts, adds, multiplies, selects, bfe), plus the access width. Where an address has no such bound (a loaded or loop-carried index) the array is 4 KiB per lane, marked as a shortcut: the program metadata read so far holds no scratch size. At most 64 KiB, the D3D11 limit of indexable temporaries. Accesses past the end read 0 and drop writes; the index is checked, never wrapped. The decoder read GFX10 scratch with saddr 0x7F (EXEC_HI, LLVM's "ST" mode: neither address register) as an SGPR base of exec_hi; it now has none. The bitcode writer gains alloca (INST_ALLOCA, explicit type). Still refused: generic flat_* (an aperture address that may be private, shared or global memory), MUBUF access through a scratch descriptor (not told apart from a buffer the title bound), and scratch loads into LDS. Tests (emit/scratch_tests.rs) run hand-assembled 64-lane compute programs on the hardware GPU and WARP and compare every lane with values computed on the CPU: a per-lane array indexed by another lane's index, x2/x3/x4 accesses through an SGPR base partly and wholly out of range and with a wrapping offset, byte and short loads with sign extension and D16 halves, sub-dword stores, and a store under a lane guard false for some or all lanes. Each program also passes dxil.dll validation. Unit tests cover the size bound and generic flat memory staying refused. On main every GPU test fails in lower with "flat memory". Co-Authored-By: Claude Opus 5.5 <[email protected]>@r4tedx on GitHub · 2026-11-24 03:15 UTC · original
A limit of this PR, confirmed by a probe while reviewing the open shader PRs with the same scrutiny (RTX 5070): when a scratch address cannot be bounded from the code, the per-lane array is 4 KiB, and accesses past it read 0 and drop writes. A store and load of 37 through a run-time address of 8192 returns 0, although a program with a larger scratch allocation would be within its bounds there (the RDNA 2 ISA bounds scratch by the allocation's size). The description says the size is a
shortcut:; this is what it looks like when it is hit. Nothing in the program metadata read so far gives the real size. If a tester sees shaders with large or unbounded scratch indices (victory shader-infoon a shader that usesscratch_*with a loaded or loop-carried index), raising the fallback or finding the size in the metadata is the follow-up.From synthetic probes only; I have no copy of the title.
@DeerSilver on GitHub · 2026-11-24 11:01 UTC · original
Tested head
e543a41merged ontomainffe310eon NVIDIA GeForce RTX 5060 Laptop GPU (Blackwell), driver 621.24, Windows 11 26300, Rust 1.98.1. No game files: the test suite only.Merged onto current
main, the test build fails.6638098("per-lane descriptors chosen by control flow", merged with #566 at 09:53) added a fourth argument toresource::track:There's no text conflict, so GitHub still shows the PR as mergeable, but merging it as it is would break
cargo testonmain. The call this PR adds needs the fourth argument.main's own tests passSome(&st);Nonekeeps the old three-argument behaviour.With
, Noneadded locally to that call, everything passes:no SM 6.6 deviceskip:private_memory_is_as_large_as_its_addresses_reachgeneric_flat_memory_is_refuseda_private_array_is_per_lane_and_dynamically_indexedwide_accesses_through_an_sgpr_base_and_out_of_rangebytes_and_shorts_extend_and_keep_the_other_halfa_store_in_lanes_off_does_not_happencargo test --workspace --locked: 348 passed, 0 failed, 6 ignored, against 342 passed / 6 ignored onmain.cargo clippy --workspace --all-targets --locked -- -D warnings: clean.AI-assisted (Claude Code).
@r4tedx on GitHub · 2026-11-24 11:11 UTC · original
The test-build issue reported on #164 also affected this branch. Fixed at
1e923c9againstmainatffe310e. The scratch test helper now passes its structured program to descriptor tracking. Local Windows checks passed: 348 workspace tests, 6 ignored, and all-target, all-feature Clippy with warnings denied. The 4 KiB fallback for unbounded scratch indices is still in effect.AI-assisted (Codex).
@r4tedx on GitHub · 2026-11-24 11:17 UTC · original
CI exited with
0xc0000005in the shader test process on1e923c9. The scratch tests could dispatch on WARP concurrently with the VALU and waterfall suites. Atd9c04c6they acquire the same device lock as both suites. The change is confined to the test harness.Local verification: three consecutive runs of the full shader library suite with
VICTORY_WARP=1(126 passed, 1 ignored each). The full workspace passed with the default adapter selection, and both CI Clippy configurations passed. The hosted check on the new head is pending.AI-assisted (Codex).
@DeerSilver on GitHub · 2026-11-24 14:04 UTC · original
Re-tested the new head
964e3admerged ontomainffe310e(clean merge) on NVIDIA GeForce RTX 5060 Laptop GPU (Blackwell), driver 621.24, Windows 11 26300, Rust 1.98.1. No game files: the test suite only. Pass, with no local change:scratch_tests.rsnow passes the fourthresource::trackargument, so the build break I reported earlier is gone.no SM 6.6 deviceskip:private_memory_is_as_large_as_its_addresses_reachgeneric_flat_memory_is_refuseda_private_array_is_per_lane_and_dynamically_indexedwide_accesses_through_an_sgpr_base_and_out_of_rangebytes_and_shorts_extend_and_keep_the_other_halfa_store_in_lanes_off_does_not_happencargo test --workspace --locked: 348 passed, 0 failed, 6 ignored, against 342 / 6 onmain.cargo clippy --workspace --all-targets --locked -- -D warnings: clean.AI-assisted (Claude Code).
@Yaekai on GitHub · 2026-11-24 16:21 UTC · original
Independent verification on Windows 11 + NVIDIA RTX 3070 (Ampere, Wave32, Rust 1.98.1) at head
964e3ad:cargo test -p victory-shader --lib -- --skip valu_tests --skip waterfall_tests --skip scratch_tests: 104 passed, 0 failed.cargo test -p victory-shader --lib scratch_tests: 4 passed, 0 failed on live D3D12/WARP (bytes_and_shorts_extend_and_keep_the_other_half,a_private_array_is_per_lane_and_dynamically_indexed,wide_accesses_through_an_sgpr_base_and_out_of_range,a_store_in_lanes_off_does_not_happen).cargo clippy -p victory-shader -- -D warnings: 0 warnings.1.0.2.1,--console series-s, 19-min world run): no regressions. Ready to merge!@465d6a0eac on GitHub · 2026-11-24 17:02 UTC · original · edited 2026-11-24 17:20 UTC
Thank you for this, @r4tedx! Many pull requests here are still waiting to be merged, so I've ported this one to 465d6a0eac/AgentRecompVI, a VIctory fork where AI agents review and merge each other's work: https://github.com/465d6a0eac/AgentRecompVI/pull/34. You're credited as the author there. This pull request stays the original.
Test results
cargo clippy(both variants) andcargo test --workspacepass (407 passed, 0 failed), including the five scratch tests. One fix was needed to build it on top of #233/#439: its test now fills thetarget,console_cfandper_lane_onlyfields oflower::Parts.@Marvisak on GitHub · 2026-11-25 00:24 UTC · original
Automated triage for the maintainers.
Needs rebase. This PR does not merge cleanly into current
main(ac2525d). Conflicting files:crates/victory-shader/src/emit/lower.rs,crates/victory-shader/src/emit/mod.rs.The content has not been reviewed yet; it will be once the branch is rebased onto
main.Generated by Claude Code
@r4tedx on GitHub · 2026-11-25 04:38 UTC · original
Closing this. The change is not wrong; this title does not use it, and bringing it up to
main(319 commits on, conflicting inemit/lower.rsandemit/mod.rs) would clear no program.The numbers. Local title data (v1.0.2.1, the main shader archive), offline, CPU only; counts and reasons only.
main's own binary at940d32f(shader-info --disasm) over the archive's 26,503 shader containers: none holds ascratch_load_*orscratch_store_*instruction. All 26,084 programs in it decode, so the pass sees every instruction.main940d32f(translate --validate --list, 25,889 programs counted): 23,918 validate; the translator refuses 298, under 10 reasons. None is "flat memory" and none names private memory.main: 0 gained, 0 lost.Limits. The pass covers that one archive, not shaders anywhere else in the title, and it looked for the FLAT
scratch_*forms this PR lowers (not the scalars_scratch_*ones, which this PR does not lower either).mainstill refuses these instructions (Fields::Flat => Err("flat memory")inemit/lower.rs), so the code is coverage for a program that keeps a local array or spills to memory. The branchshader/scratch-memorystays on the fork at964e3ad; if such a program turns up, in this title or another, I will reopen this on currentmain.Thank you @DeerSilver and @Yaekai for the runs on
964e3ad(RTX 5060 Laptop, RTX 3070), and @EduAzevedo for the count that first showed it gains nothing here.AI-assisted
Pull request closed