Shaders: reads of uninitialized SGPRs read as zero, as VGPRs do #432
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!432
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/432/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?
@Proudnoob4 on GitHub · 2026-11-24 03:17 UTC · original
Proudnoob4:fix/uninitialized-sgprs→main· +47 −1 · 3 files · 1 commits · merged 2026-11-24 08:14 UTCWhat
Some compute programs read SGPRs that the hardware never initializes, because two branches make it look as if a read can happen before the write — but the branch that skips the write does not reach the read. VIctory stops at
a hardware SGPR input is not supportedand drops the program's dispatches.ComputeIo::initialized_sgprs()counts the SGPRs the hardware does load: user SGPRs, enabled system ones and the scratch wave offset (rsrc2 bit 0). A read of a general SGPR (codes 0..=105, perir.rs) at or above that count holds whatever the last wave left, so no correct program depends on its value. Read it as zero, as already done for unset VGPRs (lower.rs"Registers no one set read as zero"). VCC, trap temporaries, M0 and EXEC keep their present handling.Evidence
Three real meshlet-culling programs fail on
mainat 0x3e0 with this reason. Instrumentedio.rs/lower.rslocally (temporary prints, not in the diff):They are written at 0xec/0xf4 on the path from
s_cbranch_vccz label_00CC; the fall-through at 0xb0 writes them and branches to 0x100, whilelabel_00CCreaches the 0x3c8 read. With this change all three translate, validate and get pipelines on an RTX 4080 (driver 621.01):main d2a4baeA local build combining this with #136's
image_gather4_lhunks took the seven known meshlet-culling programs from 0/7 → 4/7 → 7/7 emitted, validated and pipelined.Tests
cargo test -p victory-shader -- --skip emit::valu_tests: 63 passed, 0 failed (the 7 skipped ones are the known Windows 10 "no SM 6.6 device" failures in this test binary, discussed in #84).initialized_sgprs_covers_user_system_and_scratchcovers user + group ids, the info SGPR, and the scratch offset.rustfmt --checkon the three changed files andgit diff --checkpass.Caveat
This does not fix the NVIDIA world hang (#71): with all seven programs enabled the device was still removed, and a little earlier than without them. It only restores the dropped dispatches. I posted that correction on #71.
AI-assisted (Claude Code). I ran the tests on Windows 10, RTX 4080; the reasoning and code are mine.
@DaviTostes on GitHub · 2026-11-24 03:37 UTC · original
Verified
f4438b3on Arch Linux x86_64 (rustc 1.98.1,cargo test -p victory-shader): 57 passed, 0 failed, 0 ignored.Not covered: GPU execution of the new zero-read paths on Windows/Wine and title behaviour (no game data here).
@upipibi on GitHub · 2026-11-24 10:19 UTC · original
A check after the merge, on
mainffe310e(which hasf4438b3) with #521 merged in for its test fixture that takesCOMPUTE_PGM_RSRC2, and one thing that may be worth a follow-up.Run on hardware (RTX 3070, driver 616.56, and WARP; the compute fixture of
valu_tests.rswith 6 user SGPRs, storing the SGPR it reads):COMPUTE_PGM_RSRC2s6(first above the count)s20s6(the scratch wave offset)a hardware SGPR inputs7s6(the group id)s7(the scratch wave offset)s6(the info SGPR)So the boundary is where
initialized_sgprssays, and the initialized system SGPRs without a value are still refused. The order it assumes (group ids, group info, scratch wave offset last) is the one in LLVM'sAMDGPUUsage, "Initial Kernel Execution State".A read with no write anywhere now also reads zero. The case in the description has a write on another path (
s37/s38written at 0xec/0xf4). The rule is wider:v_mov_b32 v0, s20in a program that never writess20translates and gives 0 (second row). A register that is read and written nowhere is not a path artefact; it is more likely an input the translator does not know about (a system SGPR in a position we have wrong, or a count decoded short), and before this change that showed up as a refusal in the census. Two ways to keep that signal:I have not seen a title program of the second kind (no game files here); this is from the code and the probe.
Not verified:
user_sgprs()takes five bits (rsrc2 >> 1 & 0x1F). If the console sets a high bit for more than 31 user SGPRs, the count comes out 32 short. Reads through the root layout are loaded before this arm, so they would not be zeroed, but the system SGPR positions would be off. I could not find a reference for where that bit is inCOMPUTE_PGM_RSRC2on GFX10, so treat this as a question.AI-assisted (Claude Code).
@Proudnoob4 on GitHub · 2026-11-24 12:07 UTC · original
@upipibi thanks for checking after the merge. I have the title's files here, so I measured both points on its own programs.
1. Reads of SGPRs no instruction writes. I ran
victory translate --listonmainffe310eover the title's shaders:shaders.rpfplus the executable, 26,503 containers and 26,084 programs, 2,582 of them compute. The comparison rows used a local-only switch ininputs():mainThe 4 are the
meshlet_culling_CS_BatchCullMeshlets*variants, readings37with 9 SGPRs initialized.I also logged every live-in read above the initialized SGPRs, whether or not the value is used. 12 distinct programs have one:
s37,s38s46s83,s84Each of these registers is written elsewhere in its program. No title program reads an SGPR that nothing writes. So your first option costs nothing here and keeps the signal.
I have that change ready with two CPU-only tests: branch
fix/sgpr-zero-only-writtenon my fork (d493a13). It refuses the never-written case asan SGPR no instruction writes, above the initialized ones, so tables can tell it apart froma hardware SGPR input. I'll open it as a PR once external PRs reopen (#712).2.
user_sgprs()taking five bits. For compute it can't come out short. GFX10 has 16 compute user-data registers (COMPUTE_USER_DATA_0to_15), andCOMPUTE_PGM_RSRC2has no MSB field. Its fields are:SCRATCH_ENUSER_SGPRTRAP_PRESENTTGID_X/Y/Z_ENTG_SIZE_ENTIDIG_COMP_CNTEXCP_EN_MSBLDS_SIZEEXCP_ENUSER_SGPR_MSB(bit 27) exists only in the graphics registersSPI_SHADER_PGM_RSRC2_{VS,PS,GS,GS_VS,HS}. This is from Mesa's register database,src/amd/registers/gfx10.json. LLVM's AMDGPUUsage agrees: the user SGPRs requested "must not exceed 16".On the title, compute user-SGPR counts range from 4 to 14. In VIctory only compute decodes the count from
rsrc2(native.rs:304). Graphics stages map user data through the root layout, so bit 27 is never involved.Counts and program names only; no game data attached. AI-assisted (Claude Code); I ran the measurements on my machine.