Skip to content

Shaders: reads of uninitialized SGPRs read as zero, as VGPRs do - #432

Merged
Marvisak merged 1 commit into
Marvisak:mainfrom
Proudnoob4:fix/uninitialized-sgprs
Nov 24, 2026
Merged

Marvisak merged 1 commit into
Marvisak:mainfrom
Proudnoob4:fix/uninitialized-sgprs

Conversation

@Proudnoob4

Copy link
Copy Markdown

What

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 supported and 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, per ir.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 main at 0x3e0 with this reason. Instrumented io.rs/lower.rs locally (temporary prints, not in the diff):

rsrc2=0x00000090 scratch_en=0 user_sgprs=8 ids=[Some(8), None, None] info=None threads=[64,1,1]
missing: S(37), S(38)

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, while label_00CC reaches the 0x3c8 read. With this change all three translate, validate and get pipelines on an RTX 4080 (driver 621.01):

program main d2a4bae this change
3b471c122be79b3e meshlet culling not translated pipeline created
fc5e66f8f873d3bb meshlet culling, prev not translated pipeline created
764ca63daec9c616 meshlet culling, paraboloid shadows not translated pipeline created

A local build combining this with #136's image_gather4_l hunks 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 Windows 10: the host device stops at shader model 6.5 (needs the D3D12 Agility SDK) #84).
  • New test initialized_sgprs_covers_user_system_and_scratch covers user + group ids, the info SGPR, and the scratch offset.
  • rustfmt --check on the three changed files and git diff --check pass.

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.

Compute programs read SGPRs the hardware does not initialize when two
branches make the paths look like they allow a read before a write, even
though the branch that skips the write does not reach the read. That stops
translation with 'a hardware SGPR input is not supported' and drops the
program's dispatches.

ComputeIo::initialized_sgprs() counts the user SGPRs, the enabled system
ones and the scratch wave offset. A read of a general SGPR (0..=105) at or
above that count holds whatever the previous wave left, so no program can
depend on its value: read it as zero, as unset VGPRs already are. Reserved
registers (VCC, trap temporaries, M0, EXEC) keep their current handling.
@DaviTostes

Copy link
Copy Markdown

Verified f4438b3 on 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).

@mercuryVM mercuryVM left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested f4438b3 on Windows 11, RTX 5070, MSVC: cargo test --workspace --no-fail-fast passes (265 passed, 0 failed). victory translate <title exe> --validate --pipelines gives the same counts before and after: 12 of 12 programs emitted, 12 validated, 12 pipelines. The 12 embedded programs I have are all vertex and pixel, though, so this local check does not exercise the compute path this PR changes. I have not reproduced the meshlet-culling results in the table.

  • initialized_sgprs matches system_sgprs: the user SGPRs, then group ids x/y/z (rsrc2 bits 7–9), then the info SGPR (bit 10), then the scratch wave offset when SCRATCH_EN (bit 0) is set. I checked the three test values against the bit layout.
  • Only general SGPRs (n <= 105) at or above that count read as zero. VCC, M0, EXEC and the trap temporaries keep their current handling, and non-compute stages are unchanged (None).
  • One risk, not blocking: if the hardware ever initializes an SGPR that system_sgprs does not model, a read of it would now quietly become zero instead of failing with "a hardware SGPR input". A tracing::debug! when this zero substitution happens would make that easy to spot.

AI-assisted (Claude Code).

mercuryVM added a commit to mercuryVM/VIctory that referenced this pull request Nov 24, 2026
… read as zero, as VGPRs do

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Ortomanic pushed a commit to Ortomanic/VIctory that referenced this pull request Nov 24, 2026
Compute programs read SGPRs the hardware does not initialize when two
branches make the paths look like they allow a read before a write, even
though the branch that skips the write does not reach the read. That stops
translation with 'a hardware SGPR input is not supported' and drops the
program's dispatches.

ComputeIo::initialized_sgprs() counts the user SGPRs, the enabled system
ones and the scratch wave offset. A read of a general SGPR (0..=105) at or
above that count holds whatever the previous wave left, so no program can
depend on its value: read it as zero, as unset VGPRs already are. Reserved
registers (VCC, trap temporaries, M0, EXEC) keep their current handling.

(cherry picked from commit f4438b3)

Ported from Marvisak#432 (@Proudnoob4); the io.rs conflict
with linux-bringup's per-vertex rows kept both fields.
@Marvisak
Marvisak merged commit ce42e77 into Marvisak:main Nov 24, 2026
BastienRodz added a commit to BastienRodz/VIctory that referenced this pull request Nov 24, 2026
… the tests compile

cargo test -p victory-shader does not compile on main (cf0bb43):

    error[E0061]: this function takes 2 arguments but 1 argument was supplied
    --> crates/victory-shader/src/emit/lower.rs:6954:13

25b0f88 (Marvisak#432) gave the test helper lowers() an rsrc2 argument and
updated its callers; 3c86053 (Marvisak#117), merged in the same wave, added the
s_bitcmp test with a call of the old form. The helper used rsrc2 0
before, so the call passes 0. One argument, nothing else changes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
MfDev0 pushed a commit to MfDev0/victory that referenced this pull request Nov 24, 2026
Compute programs read SGPRs the hardware does not initialize when two
branches make the paths look like they allow a read before a write, even
though the branch that skips the write does not reach the read. That stops
translation with 'a hardware SGPR input is not supported' and drops the
program's dispatches.

ComputeIo::initialized_sgprs() counts the user SGPRs, the enabled system
ones and the scratch wave offset. A read of a general SGPR (0..=105) at or
above that count holds whatever the previous wave left, so no program can
depend on its value: read it as zero, as unset VGPRs already are. Reserved
registers (VCC, trap temporaries, M0, EXEC) keep their current handling.

(cherry picked from commit f4438b3, upstream Marvisak#432 by
Proudnoob4)

Co-authored-by: arena-agent <297053741+arena-agent@users.noreply.github.com>
@upipibi

upipibi commented Nov 24, 2026

Copy link
Copy Markdown

A check after the merge, on main ffe310e (which has f4438b3) with #521 merged in for its test fixture that takes COMPUTE_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.rs with 6 user SGPRs, storing the SGPR it reads):

COMPUTE_PGM_RSRC2 read result
6 user SGPRs s6 (first above the count) 0
6 user SGPRs s20 0
+ scratch (bit 0) s6 (the scratch wave offset) refused: a hardware SGPR input
+ scratch s7 0
+ group id X + scratch s6 (the group id) 0 (one group is dispatched)
+ group id X + scratch s7 (the scratch wave offset) refused
+ thread-group info s6 (the info SGPR) refused (with #521's reason for it)

So the boundary is where initialized_sgprs says, 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's AMDGPUUsage, "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/s38 written at 0xec/0xf4). The rule is wider: v_mov_b32 v0, s20 in a program that never writes s20 translates 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 in COMPUTE_PGM_RSRC2 on GFX10, so treat this as a question.

AI-assisted (Claude Code).

@Proudnoob4

Copy link
Copy Markdown
Author

@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 --list on main ffe310e over the title's shaders: shaders.rpf plus the executable, 26,503 containers and 26,084 programs, 2,582 of them compute. The comparison rows used a local-only switch in inputs():

Rule Programs whose outcome differs from main
your first option (zero only if some instruction writes the register; refuse the rest) 0
refuse every read above the initialized SGPRs (the behaviour before #432) 4

The 4 are the meshlet_culling_CS_BatchCullMeshlets* variants, reading s37 with 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:

Register Initialized SGPRs Programs
s37, s38 9 4
s46 14 7
s83, s84 14 1

Each 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-written on my fork (d493a13). It refuses the never-written case as an SGPR no instruction writes, above the initialized ones, so tables can tell it apart from a 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_0 to _15), and COMPUTE_PGM_RSRC2 has no MSB field. Its fields are:

Bits Field
0 SCRATCH_EN
5:1 USER_SGPR
6 TRAP_PRESENT
9:7 TGID_X/Y/Z_EN
10 TG_SIZE_EN
12:11 TIDIG_COMP_CNT
14:13 EXCP_EN_MSB
23:15 LDS_SIZE
30:24 EXCP_EN

USER_SGPR_MSB (bit 27) exists only in the graphics registers SPI_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.

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.

5 participants