Skip to content

Shaders: spill-table loads read the arguments as they were on entry (stacked on #302) - #489

Closed
louketa wants to merge 4 commits into
Marvisak:mainfrom
louketa:spill-clobber
Closed

louketa wants to merge 4 commits into
Marvisak:mainfrom
louketa:spill-clobber

Conversation

@louketa

@louketa louketa commented Nov 24, 2026

Copy link
Copy Markdown

Stacked on #302 (its commit 815178d is the first one here); only the second commit is new.

What

#302 rewrites each spill-table load into s_mov_b32 sK, s(16+k). That reads what s(16+k) holds at that point, not on entry. A program that has already written SGPRs 16 and up, for example as the destination of an earlier table load, then reads the wrong root argument, with no refusal. @r4tedx reported this on #302 with a synthetic repro.

This commit makes the program first copy the user SGPRs its table loads stand for into a block of SGPRs it never reads or writes, and the rewritten loads read that block. If no free block exists below VCC, or a branch can return to the program's entry, nothing is rewritten, so the program fails as it does without #302 instead of binding the wrong argument.

It happens in the title's shaders

I ran the world (opening cutscene) with a local build that saves every container failing at run time. I then translated the 34 programs that fail with an address in an SGPR that is no root parameter offline: before #302, with #302, and with #302 plus this commit. Same stack as in my comment on #302, Windows 11, RTX 3080, v1.0.2.1.

Program #302 #302 + this
3b90165a77d95cdf (largest drop in my world run) an address in an SGPR that is no root parameter passes resource tracking; stops in emit at a root UAV or typed root view
6f6a359d1166bb27 an address from a phi an address from a root parameter that is no address

3b90165a77d95cdf is the case r4tedx described: a 16-dword table load into s[8:23] overwrites what stands for table dwords 0..7 before a second load reads the root UAV address from table dword 2.

Over the 34 programs:

Next failure #302 #302 + this
an address not from a root parameter 12 12
a root UAV or typed root view (emit) 8 11
an address from a root parameter that is no address 6 8
an address in an SGPR that is no root parameter 4 1
an address from a phi 2 0
a select on an untracked mask (emit) 2 2

None of the 34 translate fully yet. The largest remaining group stops at root UAVs in emit, which #294 works on.

Tests

  • New spill_table_loads_read_entry_values: a table load into s[12:19], then a load of table dwords 0..1. It checks that s16 and s17 are read on entry and that the second load's value traces back to s16's entry value. It fails on Shaders: read root arguments the console spills to memory #302 alone (s16 read on entry) and passes here.
  • spill_table_loads_read_user_sgprs now also counts the entry copies.
  • cargo test -p victory-shader --lib: 72 passed. cargo clippy -p victory-shader --all-targets and cargo fmt --check: clean. cargo xtask policy: ok.

Not verified

  • I have not checked that the translated programs produce the right images; nothing here translates fully yet.
  • victory translate still builds without the spill rule (noted on Shaders: read root arguments the console spills to memory #302). For the offline numbers I applied the same spilled condition in apps/victory/src/translate.rs locally; that change is not part of this PR.

No game data attached.

🤖 Generated with Claude Code

@DaviTostes

Copy link
Copy Markdown

Verified 279eac1 on Arch Linux x86_64 (rustc 1.98.1, cargo test -p victory-shader): 59 passed, 0 failed, 0 ignored.

Not covered: the spill-table path on a real GPU and title shaders (no game data).

@marquezinii

Copy link
Copy Markdown
Contributor

Ran the spill-table path on a GPU, since that was the part not covered: NVIDIA RTX 5070 (driver 621.24), Windows 11, the SDK's dxil.dll validating. Three synthetic compute programs whose root layout sits wholly at user data 16 and up (8 root constants at 16..23 holding 100..107, a UAV table at 24), built through ir::build with spill tables on, resource::track, io, structure, lower, validated, and dispatched:

case #302 815178d alone this PR 279eac1
no overlap: s_load_dwordx8 s[32:39], read s36 104, correct 104, correct
one load overlapping its own sources: s_load_dwordx8 s[20:27], s[0:1], 0x0, read s24 100 (table dword 0), expected 104 104, correct
an earlier load into s16 and up, then a load read into s12 104, expected 100 100, correct

The second row is a case beside the one r4tedx reported: within a single rewritten load, the s_mov_b32 run reads registers it has just written when the destination starts above the first source and overlaps it. This PR's entry-value block fixes both. All modules validate, and cargo test -p victory-shader passes here (including spill_table_loads_read_entry_values). So on this evidence #302 should only go in together with this commit.

One question for the gating in emit/mod.rs (inherited from #302): it checks that every root parameter is at user SGPR 16 or above, but not the user SGPR count in rsrc2. With 16 or more user SGPRs the thread-group-id SGPRs land at s16 and up too, io would call them GroupId while resource::param_of calls them root parameters, and lower::inputs lets the root parameter win. Gating on at most 16 user SGPRs would turn that into a refusal. I cannot tell from here whether a title does this.

Not covered: the title (no game data here).

@ghost

ghost commented Nov 24, 2026

Copy link
Copy Markdown

In game on AMD RDNA4 (RX 9060 XT, Windows 11), world / opening cutscene, one run: d2a4bae + #135 + #365 + #318 + #385 + #346 + #305 + #500 + this PR at 279eac1 (with #302's 815178d), diag build plus a local logging patch. The next failures of the 34 programs match your offline #302 + this column exactly (12 / 11 / 8 / 1 / 0 / 2).

Weighted by dropped dispatches of those 34 (about 2,200 per traced frame here, 6 frames):

next failure dispatches/frame
a root UAV or typed root view (11 programs) 834 (38%)
an address from a root parameter that is no address (8) 659 (30%)
still an address in an SGPR … (f6e47efa50226beb alone) 391 (18%)
an address not from a root parameter (12) 322 (15%)
a select on an untracked mask (2) 4

So the one program still on the SGPR rule is the second-largest dropper after 3b90165a77d95cdf. Next here: the same stack with #294.

AI-assisted (Claude Code, Claude Opus 5.5); no game data.

@fuzeofficlal

Copy link
Copy Markdown
Contributor

I checked the GroupId question from @marquezinii's review on 279eac1. With 16 enabled user SGPRs, GroupId.x occupies physical s16 even when the spill table separately provides logical root argument 16. A synthetic compute dispatch reads both values into a fixed output buffer. All modules passed the SDK DXIL validator (1.8.2502.11) before dispatch.

Case RTX 5090 and Radeon iGPU
Group 0 on 279eac1 [100, 100]
Group 0 with the fix [0, 100]
Selected group 1 with the fix [1, 100]

The fix gives spill inputs their own SSA identities and uses the same namespace for a root descriptor's high half. The source commit includes small IR and resource-tracing regressions. The shader suite reports 73 passed. Coverage is the synthetic runtime spill-lowering path. Incremental commit on 279eac1

@upipibi

upipibi commented Nov 24, 2026

Copy link
Copy Markdown

Independent check of head 279eac1 (this PR's commit on top of #302's 815178d), which is based on main d2a4bae. I read the comments above first; this adds to them and repeats nothing as new. I found no defect in what this commit does. The GroupId question raised by @marquezinii and answered with a commit by @fuzeofficlal is still open on this head and I did not re-test it.

Read against the RDNA2 ISA (30-November-2020)

  • 7.2.1 "S_LOAD_DWORD", p. 52: ADDR = SGPR[base] + inst_offset + { M0 or SGPR[offset] or zero }, "the two LSBs are ignored". The rewrite takes only loads with no SGPR offset (soffset = 125) and offset >= 0 && offset % 4 == 0 (ir.rs, spill_loads); anything else stays a load and is refused later as before. That is narrower than the hardware (which would drop the two low bits) but never wrong.
  • That table dword k is user SGPR 16 + k, and that s[0:1] is the table pointer, is the console compiler's convention, not ISA. Nothing I can cite confirms or contradicts it; the title counts posted on Shaders: read root arguments the console spills to memory #302 are the evidence.

Things I looked for and did not find

  • An SGPR writer the tag tracking misses. SOP1/SOP2/SOPK all decode to Fields::Sop with a dst, SMEM writes through data, VALU through dst/carry; those are the only SGPR writers the decoder has. s_movk_i32 s2, 5 between the pointer copy and the load leaves the load a load (probe below).
  • Tags surviving a join. They are cleared at every branch target, and an instruction that is not a target is only reached by falling through, so the linear scan is sound. A load that is itself a branch target, forward or as a loop header, stays a load (probes below).
  • Several instructions with one offset. The rewritten s_mov_b32 run shares the load's offset and the entry copies share the entry's. In build_with that makes ends true for each of them when that offset is a block leader, which sets the block's exit to a jump to itself; the block's real last instruction then overwrites it, so the CFG comes out right. It works, but it depends on that overwrite: worth a comment there, or distinct offsets.
  • The scratch block. It starts above the highest user SGPR read through the table and is checked against every register effects reports, so neither a load's destination nor the pointer copies can land in it. With no free block the original list is returned.

Limits, conservative (still refused, as on main)

  • A pointer copied as a pair, s_mov_b64 s[2:3], s[0:1], is not followed: the load from s[2:3] stays a load.
  • Loads after any branch target are not rewritten, which is where the one remaining an address in an SGPR ... program in the table above probably is.

Run on hardware. NVIDIA RTX 3070, driver 616.56, Windows 11, Rust 1.98.1, dxil.dll from the Windows SDK 10.0.26100 validating; cargo test -p victory-shader -- --test-threads=1:

  • this PR on main: 72 passed, 0 failed (main: 69).

  • with two local tests added: 74 passed, 0 failed.

    • IR probes (build_with(.., spilled = true)), loads left / s_mov_b32 count: load at a forward branch target 1 / 9; pointer copy overwritten by s_movk_i32 1 / 1; s_mov_b64 pointer copy 1 / 0; load at a loop header 1 / 9; a load whose destination covers the pointer copy, then a second load through it 1 / 9. All as they should be.

    • End to end on the GPU and on WARP, a compute program whose whole layout is in the table (8 root constants at user data 16..23 holding 100..107, a UAV table at 24, s0 reused for a literal after the pointer copy), storing the constants it sees:

      loads RTX 3070 WARP
      s_load_dwordx8 s[40:47] 100..107 100..107
      s_load_dwordx8 s[16:23] first, then the same dwords into s[40:47] 100..107 100..107
      s_load_dwordx16 s[12:27] (past the layout), then the two halves, second first 100..107 100..107

    The same three on Shaders: read root arguments the console spills to memory #302 alone are in my note there.

Not checked: the title (no game files here), AMD hardware, the 16-or-more user SGPR case above.

AI-assisted (Claude Code).

@r4tedx

r4tedx commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

@Brazlucas a late answer to your ping on #302: yes, this fixes the entry-value defect I reported there, and thank you for taking it on.

What I ran, on an RTX 5070 and WARP, with 279eac1 merged with #294's 0050ce5 and the SDK's dxil.dll validating. One hand-assembled compute program of the shape you describe for the world's largest dropper: every root argument at user data 16 and up, a 16-dword table load into s[8:23], the root UAV's address taken from table dwords 2 and 3, a built descriptor and a buffer_store.

Build Result
no spill rule refused at the first load: an address in an SGPR that is no root parameter
this PR + #294 translates, validates, and the dispatch writes [120, 135] through the root UAV on both adapters, as expected

So the two PRs only pay off together for a program like that, which matches jeremygold02's and nullmarsh's title results on #294.

Two open points, neither new, both reproduced or read here:

  • GroupId in s16 (raised by marquezinii, with a fix commit from fuzeofficlal above). Reproduced on this head: with 16 user SGPRs and the group id enabled, a program that reads both gets [116, 116] where the console gives [116, 0]. I have not tested fuzeofficlal's commit.
  • The CLI still builds without the spill rule (etcetera-yt on Shaders: read root arguments the console spills to memory #302), so victory translate counts differ from the runtime for these programs.

For the tester tree (integration/testers on my fork): #302 was left out for the defect this fixes. I will take #302 with this PR once the GroupId point is settled in it. #294 is a separate decision, because nullmarsh reports a device hang 16 to 23 s into the world on RDNA4 in every run with it.

Hand-assembled programs only; the title was not used in these tests. AI-assisted.

@NexForever

Copy link
Copy Markdown

Hi! I've been investigating the spill-table handling in #302 and #489, including the proposed Reg::Spill fix by @fuzeofficlal.

During testing, I found an additional issue in ir.rs::spill_loads: the pointer provenance tracker accepts s_and_b32 masks that can clear significant bits of the 40-bit spill-table address.

For example, masks such as 0x7f or 1 may alter the pointer, but the translator still treats subsequent loads as accesses to the original spill table. This can silently produce incorrect root argument values.

Fix: Preserve spill-pointer provenance only when the mask retains all eight significant bits of the address's high dword.

Verification:

  • Regression tests reproduced the issue before the fix.
  • All 5 new regression tests pass after the fix, including inline/literal masks and exhaustive 8-bit mask cases.
  • Shader test suite: 98 passed, 1 ignored.
  • Clippy (-D warnings) and asset-policy checks passed.
  • Four differential tests could not fully execute because optional compiler dependencies were unavailable.
  • No proprietary game data was used.

Fix commit:
NexForever@0c0a3be

Development branch:
https://github.com/NexForever/VIctory/tree/fix/spill-pointer-mask-provenance

The branch includes #302, #489, and @fuzeofficlal's 205b276 work, with attribution preserved. My pointer-provenance correction is isolated in commit 0c0a3be.

Since PR creation is currently restricted to collaborators, would you prefer to cherry-pick this fix or have me prepare a dedicated PR when contributions reopen?

Thanks for your work on VIctory!

Best,
NexForever

@upipibi

upipibi commented Nov 24, 2026

Copy link
Copy Markdown

On @NexForever's mask point: it is right, and my note above missed it. I checked which instructions write the tracked registers, not which mask values keep the tag.

Read at 279eac1, ir.rs spill_loads: an s_and_b32 of a tag-2 register keeps tag 2 for any literal mask <= 0xFFFF and any inline constant 1..=64 ((129..=192).contains(k)), on either side. So s_and_b32 s3, s1, 1 or , 0x7f still counts as the table pointer's high dword, and a following s_load_dword through it is rewritten to the user SGPRs although the address is a different one. Dropping the tag unless the mask keeps every address bit of the high dword is the conservative fix.

Which bits those are depends on the address width, which is the console's convention and nothing I can cite; the title's own masks (the values counted on #302) are the evidence for the set to accept. I have not built or run 0c0a3be.

AI-assisted (Claude Code).

suirentakamura referenced this pull request in suirentakamura/victory-suiren Nov 24, 2026
With 16 user SGPRs the hardware puts the thread-group ids in s16 and up,
where the spill rule reads root arguments, so a program reading both got
the root argument for the group id (reported on #489). Such programs are
now left to fail as they do without the rule.

victory translate now builds with the same rule, so its counts match the
runtime.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
suirentakamura added a commit to suirentakamura/victory-suiren that referenced this pull request Nov 24, 2026
@Marvisak Marvisak closed this Nov 24, 2026
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.