Skip to content

BUILD-1553: Keep descriptor stride metadata consistent with VGPR writes - #5102

Open
Hiiva wants to merge 1 commit into
ItsNotRexed:mainfrom
Hiiva:fix/vgpr-stride-writes
Open

Hiiva wants to merge 1 commit into
ItsNotRexed:mainfrom
Hiiva:fix/vgpr-stride-writes

Conversation

@Hiiva

@Hiiva Hiiva commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

What and why

BUILD-1553, original claim and returning-atomic scope extension.

Descriptor-stride metadata can retain a VGPR's old scaling after instructions overwrite that register. A synthetic shader computes index * 32, overwrites the register with 48, and copies it through v_readfirstlane into a descriptor offset. The translated shader uses 48 while the metadata still advertises stride 32, which can misdirect descriptor registration.

Track both destinations of v_swap_b32 by exchanging their existing facts. Invalidate VGPR stride facts for M0-dependent relative writes because their targets are not tracked. Invalidate only the words written by returning global, buffer, and image atomics: one word for 32-bit operations, two for supported x2 operations. Preserve no-return atomic inputs, comparison-only words, source-only relative reads, and stride facts already copied into SGPRs. Update the design contract with the evidence and limits. Translation arithmetic is unchanged.

Current upstream validation

Rebased onto 561b65a832218fbb9e6d2effd392a8eef093a3f8 at 12689237d696641c20aa4e2394bf18ecd1439552. The production patch is unchanged. The design-table conflict preserves every upstream row. Image-atomic test assertions now allow upstream's hoisted register pointer definitions while still requiring destination stores after the atomic.

All 72 focused metadata/VGPR/sampler tests pass. Combined with rebased #5031 and unchanged #5352 (da093a8a), all 83 focused integration methods pass with SDK SPIR-V validation active. Independent source/range review and the affected atomic-method rerun are clear. compileall, isolated make_game.py --help, and diff checks pass.

Original validation

Windows 11, Python 3.14.6, SPIR-V Tools from Vulkan SDK 1.4.363.0, upstream 3faac557b1584d39004357918e60325b4d4ff0fe:

  • 10 new semantic test methods pass, exercising 51 synthetic translated modules; all 51 pass spirv-val for Vulkan 1.2.
  • The original swap/relative-write cases failed in eight subcases before their production fix. The added returning-atomic cases failed in eleven subcases before the atomic fix.
  • Swap, relative-write, and global/buffer atomic values execute through the existing CPU workgroup interpreter. Image atomic tests inspect actual emitted destination stores and metadata, including the input-only comparison word; the interpreter does not execute storage images.
  • Positive controls cover self-swaps, both swap destinations, SGPR copies preserved across relative writes, subsequent rescaling, direct overwrite, no-return atomics, and x2 return widths.
  • Existing metadata suite: 19 pass. Operand tests: 5 pass. Text line-ending checks: 3 pass. compileall, CLI help, and git diff --check pass.
  • Independent review reran all 10 new tests and found no actionable issue.

CPU-only validation; no GPU/driver or game-corpus measurement. All inputs are synthetic. No game files accessed or included. AI-assisted: yes.

@shdann

shdann commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested 1aa4ebc8 on Windows 11, CPython 3.14, CPU only (no GPU, no game files): pass.

All 10 tests pass with py -3.14 -I -m unittest discover -s tests/semantic -p test_shader_meta_vgpr_writes.py. The same test file reports 19 failing cases, including subtests, against 3faac557. The existing suites also pass: tests/test_shader_meta.py (19 tests) and tests/test_shader_meta_operands.py (5 tests).

I also tested two consecutive swaps. With a stride of 32 in v4 and a constant 48 in v6, I ran v_swap_b32 v4, v6 followed by v_swap_b32 v6, v4. The interpreter writes 32 as the offset. The metadata reports descriptor_strides=[32].

AI-assisted.

@gertify

gertify commented Nov 23, 2026

Copy link
Copy Markdown

Tested 1aa4ebc on Linux (Ubuntu 24.04, Python 3.12.3), CPU only, no game files: pass.

  • tests/test_shader_meta.py: 19 tests, OK. tests/test_shader_meta_operands.py: 5 tests, OK. tests/semantic test_shader_meta_vgpr_writes.py (new): 10 tests, OK, no skips. git diff --check: clean. py_compile of tools/shader_meta.py: exit 0.

The new test file does not invoke spirv-val, so the 51-module spirv-val claim, the failing-before-fix control and the line-ending and CLI help checks were not reproduced here.

AI-assisted.

@SweatierKey

Copy link
Copy Markdown
Contributor

Independent CPU + SPIRV-Tools verification on Linux (Python 3.13.5, spirv-as/spirv-val with LEONIDA_REQUIRE_SPIRV_TOOLS=1, head 1aa4ebc8, base main 3faac557):

  • python3 -I -m py_compile tools/shader_meta.py tests/semantic/test_shader_meta_vgpr_writes.py — clean.
  • python3 -I -m unittest discover -s tests/semantic -p "test_shader_meta_vgpr_writes.py" -v — 10/10 OK (0.129s).
  • tests/test_docs_design_rows.py (4/4) and tests/test_docs_table_rows.py (2/2) — OK; git diff --check clean.
  • Diff review: clean handling of v_movrel*/v_swaprel_b32, v_swap_b32, and returning glc atomics (preserving the untouched comparison word on 32-bit cmpswap and invalidating both words on _x2).
  • Small non-blocking note: NO_SGPR_DST (tools/shader_meta.py:26) also lists "flat_atomic" alongside "global_atomic" and "buffer_atomic"; adding "flat_atomic_" to the returning-atomic tuple at line 285 would keep that family consistent too.

Looks good to merge.

@Kam471 Kam471 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ran this at 1aa4ebc84 on Windows 11: tests/semantic/test_shader_meta_vgpr_writes.py -> 10 tests, OK; tests/test_shader_meta.py -> 19 tests, OK; git diff --check clean.

A write to a VGPR that held a descriptor-stride value now invalidates that metadata (rather than leaving the old stride attached to the new value), and the docs row states it; the added cases cover the clobbering write, the untouched value and the re-derived stride. No blocking issue.

@MichaelL401

Copy link
Copy Markdown
Contributor

Tested 1aa4ebc8 on Linux, CPU only (no GPU), Python 3.12.16: pass.

  • python -m unittest discover -s tests/semantic -p test_shader_meta_vgpr_writes.py: 10 tests OK. Negative control: the same tests with main's tools/shader_meta.py give 19 failures.
  • python -I tests/test_shader_meta.py and python -I tests/test_shader_meta_operands.py: OK.
  • Full semantic suite (397 files): same result as main 3faac557. The only failures are the two already on main: test_loop_ballot_barrier (tests/semantic: run test_loop_ballot_barrier's emulated cases through the structured form #5024) and test_vcmp16_opsel (needs spirv-as). No SPIR-V tools here, so spirv-val was not run.

Code read: the v_swap_b32 exchange, the M0-relative clear and the returning-atomic word counts match the description; no-return atomics keep their inputs.

AI-assisted (Claude).

@Kev-Ball

Copy link
Copy Markdown

Checked the metadata impact of 1aa4ebc8 against its parent on the Xbox 1.0.2.1 DXBC corpus (CachyOS, CPU). I scanned all 26,503 disassemblies for the swap, relative-move, and global/buffer/image atomic opcode families touched by this change. All 1,684 candidate shaders reached metadata collection. I ran both versions of analyse_instructions on each candidate's same basic blocks, successors and SGPR inputs.

All 1,684 returned identical metadata across every field, including descriptor_strides. The synthetic regression fixtures remain the evidence for the corrected behavior; the retail check establishes that the change preserves the observed metadata on this corpus. The comparison stopped at metadata collection, before SPIR-V emission.

@Hiiva

Hiiva commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

I checked the flat_atomic family separately. The decoder and some analysis passes recognize it, but the current translator has no flat-atomic lowering and rejects those instructions. This fix therefore covers the global, buffer, and image atomic operations that can currently write translated VGPR values. Adding a prefix alone would not provide a translator-backed regression for flat atomics. I am keeping that extension with eventual flat-atomic lowering, where its return-value writes can be tested directly.

@kacper-serewis kacper-serewis left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Windows CPU comparison at 1aa4ebc: parent and head translated and validated 200/200; all modules and complete index entries matched, including every descriptor_strides field. This cache was selected for scalar u64 comparisons. Ten synthetic tests passed on head and recorded 19 failing assertions on parent, covering overwritten registers, swaps, preserved SGPR copies and atomic returns. AI-assisted.

@Atomic258

Copy link
Copy Markdown
Contributor

Tested 1aa4ebc on Windows Server 2022, CPython 3.13.12: pass

  • python -I -m unittest discover -s tests/semantic -p "test_shader_meta_vgpr_writes.py" -> Ran 10 tests, OK
  • Negative control (tools/shader_meta.py swapped to upstream/main): FAILED (failures=19)

git diff --check clean; python -m compileall -q exit 0. No game files, no GPU. AI-assisted (Devin).

@kotekompotek

Copy link
Copy Markdown

Independent Windows CPU verification on 1aa4ebc8:

  • python -m pytest tests/semantic/test_shader_meta_vgpr_writes.py --basetemp D:\temp\pytest_tmp -q: 10 passed in 3.22s

@Hiiva

Hiiva commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto 561b65a832218fbb9e6d2effd392a8eef093a3f8 at 12689237d696641c20aa4e2394bf18ecd1439552. The production patch is unchanged. The design-table conflict preserves every upstream row. Image-atomic test assertions now allow upstream's hoisted register pointer definitions while still requiring destination stores after the atomic.

All 72 focused metadata/VGPR/sampler tests pass. Combined with rebased #5031 and unchanged #5352 (da093a8a), all 83 focused integration methods pass with SDK SPIR-V validation active. Independent source/range review and the affected atomic-method rerun are clear. compileall, isolated make_game.py --help, and diff checks pass.

All inputs are synthetic. No game files accessed. AI-assisted: yes.

@kapitche

Copy link
Copy Markdown
Contributor

Tested 1268923 merged with main 561b65a on Linux (WSL2), Python 3.12.16 -I: pass

tests/semantic/test_shader_meta_vgpr_writes.py: 10 run, 10 passed; tests/test_shader_meta.py: 27 run, 27 passed; tests/test_shader_meta_operands.py: 5 run, 5 passed; tests/test_docs_*.py: 11 files, 11 passed; 0 failed. Negative control: with tools/shader_meta.py taken from main the new test file fails (19 failures reported across its 10 tests; file restored afterwards). Not run on Windows.

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.