Skip to content

Translator: model TG_SIZE_EN for compute shaders - #7165

Closed
ddaikodaiko wants to merge 1 commit into
ProjectLeonida:mainfrom
ddaikodaiko:feat/track1-rdna2-sysvals
Closed

ddaikodaiko wants to merge 1 commit into
ProjectLeonida:mainfrom
ddaikodaiko:feat/track1-rdna2-sysvals

Conversation

@ddaikodaiko

Copy link
Copy Markdown

What and why

Compute shaders whose COMPUTE_PGM_RSRC2 has TG_SIZE_EN [10] set were rejected with Unsupported. They now
translate.

Mesa puts one tg_size SGPR right after the enabled workgroup ids (src/amd/vulkan/radv_shader_args.c:
workgroup_ids[0..2], then tg_size), and src/amd/common/nir/ac_nir_lower_intrinsics_to_args.c reads from it:

  • [5:0] = waves in the group (load_num_subgroups)
  • [24:20] = this wave's id in the group on GFX10.3 (load_subgroup_id)
  • [11:6] = the ordered-append id, which Mesa reads as the wave id on GFX6–10 because ORDERED_APPEND is off
    in the dispatch initiator

The translator stores waves | id << 6 | id << 20, with waves = ceil(group / W) and
id = LocalInvocationIndex / W (W = 32 or 64). A first draft stored only the wave count, so every wave of a
multi-wave group read wave id 0; that is what tests/test_translate.py now guards.

Not modelled: first_wave [31] from the ISA's CS SGPR table. No Mesa user and no corpus confirmation, so it stays
out rather than being guessed — it is called out in the contract table.

The sample needs a group size other than the harness default (64×1×1), so runtime/test/shaders/build.py and
tests/conftest.py learn two leading ; rsrc2: / ; group: headers. They default to the previous values.

Tested

This machine, Linux, no work folder and no llvm-mc / spirv-val / ninja installed, so the toolchain-gated
tests skip:

  • python -m compileall -q make_game.py tools tests — clean
  • python -m pytest tests — 926 passed, 70 skipped, 1 xfailed, 1466 subtests

From the development session on the same machine:

  • runtime/test/shaders/build.py — 26/26 samples translated and spirv-val-valid
  • GPU: AMD Renoir, RADV (Mesa), wave64 hardware. vk_compute_test cs_tgsize.spv 2 dump →
    4 1048644 2097284 3145924 in both groups. That confirms the translation produces what the model says, not the
    hardware layout, which still rests on Mesa (Renoir is GFX9 with 64-wide subgroups, so this is not a native
    wave32 RDNA2 run).

Not done here: the corpus count (rdna2spirv.py --limit/--shard before and after). No work/ on this machine,
so I could not measure how many CS set TG_SIZE_EN or which bits of the SGPR they read. Worth doing before merge.

Checklist

  • No game data, keys or decryption, including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed (Mesa sources,
    unit test over all 128 threads of a 16×4×2 group, and the GPU sample).

AI-assisted.

Shaders whose COMPUTE_PGM_RSRC2 has bit 10 set were rejected. Mesa puts one
tg_size SGPR right after the enabled workgroup ids
(radv_shader_args.c: workgroup_ids[0..2], then tg_size), and
ac_nir_lower_intrinsics_to_args.c reads from it: [5:0] = waves in the group,
[24:20] = this wave's id on GFX10.3, [11:6] = the ordered-append id, which
Mesa reads as the wave id on GFX6-10 because ORDERED_APPEND is off in the
dispatch initiator.

The translator now stores waves | id << 6 | id << 20, with
waves = ceil(group / W) and id = LocalInvocationIndex / W (W = 32 or 64).
A first draft stored only the wave count, so every wave of a multi-wave group
read wave id 0.

Not modelled: first_wave [31] from the ISA's CS SGPR table (no Mesa user).

Checked by tests/test_translate.py (the stored word for all 128 threads of a
16x4x2 group, wave32 and wave64) and by runtime/test/shaders/cs_tgsize.asm
(64x2x1, 2 groups -> 4 1048644 2097284 3145924 per group on RADV Renoir).
The GPU run confirms the translation, not the hardware layout, which rests on
Mesa. build.py and conftest.py learn the '; rsrc2:' / '; group:' headers the
new sample needs.

AI-assisted.
@tikD1337

Copy link
Copy Markdown
Contributor

The new lowering is internally consistent for the documented wave-count and wave-ID fields. I found no arithmetic or SGPR-placement defect in the full patch at 968cd6d15e340b37910bf3431bbb090ae95b7382. However, the verification record is incomplete, and the remaining hardware assumptions need to stay explicit before this is treated as complete TG_SIZE_EN support.

The implementation replaces the previous rejection with actual system-register initialization:

  • The extra SGPR is placed at user + len(tgid), after only the enabled workgroup IDs. Existing workgroup-ID registers retain their positions. This ordering agrees with Mesa’s compute argument declaration.
  • The entry block computes the linear invocation index as x + lx*y + lx*ly*z, then divides by the source wave width using a shift. This correctly accounts for multidimensional groups and does not depend on which local-ID VGPR components the source shader enables.
  • The count uses ceiling division, (lx*ly*lz + W - 1) // W, so a partially occupied final wave is included.
  • The stored word is waves | (wave_id << 6) | (wave_id << 20). Mesa extracts the subgroup count from bits [5:0], the GFX10.3 subgroup ID from [24:20], and the older ordered ID from [11:6]. Its use of that ordered ID as a wave ID explicitly depends on the dispatch initiator’s ordered-append settings being zero. See Mesa’s intrinsic lowering.
  • With the existing 1–1024-thread limit, the modeled fields cannot overlap: wave32 needs at most 32 waves and IDs 0–31; wave64 needs at most 16 waves and IDs 0–15. Registering the new constants before assembly also avoids references to constants absent from the emitted module.

The test integration is appropriate. The new header comments let the synthetic fixture request its actual RSRC2 and group dimensions in both build paths. cs_tgsize.asm preserves the V# in s[0:3], checks that the workgroup ID remains in s4, and writes the size word from s5 for four logical waves in each of two groups. The translator test evaluates the emitted initialization for every invocation of a 16×4×2 group in both wave modes, which would catch the earlier “every wave has ID 0” mistake.

There are still material limits to the claim:

  1. Bit 31 remains unmodeled. The emitted word always clears the documented first_wave bit. The documentation acknowledges this, but removing the blanket rejection also accepts shaders that could inspect that bit. Please establish whether any affected corpus shader consumes it before claiming compatibility for all TG_SIZE_EN shaders. Likewise, Mesa’s ordered-append configuration is evidence for Mesa’s interpretation, not proof of the Xbox dispatch configuration.
  2. Wave width still comes from the existing instruction heuristic. This PR uses that decision consistently, but the wave64 test deliberately inserts s_mov_b64 exec, -1. It therefore verifies the detected-wave64 path; it does not establish that every source wave64 shader contains a detectable marker.
  3. The committed test misses ceiling-division boundaries. Both tested groups contain 128 threads, divisible by both wave widths. Add a partial-wave case such as 33 or 65 threads. No-enabled-TGID and sparse-TGID cases would also strengthen coverage of the appended register position. These are coverage improvements; the current formulas themselves are correct.

Verification on Windows 11, CPython 3.13.14, without game files:

Check Result
git diff HEAD^ HEAD --check PASS
python -m compileall -q make_game.py tools tests runtime/test/shaders PASS
python -I make_game.py --help PASS
python -I -m unittest discover -s tests/semantic -v PASS — 335 tests
python -I tests/test_shader_checks.py PASS — 4 tests; subprocess/GPU results are mocked
Repository CI’s tracked forbidden-extension check PASS — zero matching tracked files

The originally reported scripts/run_semantic_tests.py exit 2 was a missing-entry-point error: no semantic tests executed through that command. The documented discovery command above runs successfully.

The reported check_no_game_data.py: FAIL remains unresolved because its diagnostic was not supplied. The separate tracked-extension check does not explain or supersede that failure. Also, pytest is unavailable in this interpreter, and AMDGPU-capable llvm-mc plus spirv-as/spirv-val were not found on PATH, so I did not execute the new translator/validator test locally. No MSVC/Ninja runtime build or GPU execution was performed. The documented RADV Renoir result is author-reported evidence of the translated shader’s behavior; it does not independently establish the source hardware register layout, and its driver version is unspecified.

The inspected changes contain handwritten assembly and synthetic test construction, with no apparent game-derived data, keys, decryption/protection workaround, or Xbox Live/Rockstar service access. The patch follows fix-don’t-disable by implementing the register initialization and updating the contract table, rather than adding a skip or suppressing validation. Full-corpus translated/validator-valid before-and-after counts are still absent.

I would withhold unconditional approval until the reported data-check failure is explained and the translator/validator tests pass in an equipped environment. The implementation is credible within its stated field model; complete hardware fidelity and corpus compatibility remain unverified.

AI-assisted.

@tylerstraub

Copy link
Copy Markdown
Contributor

Replying to @tikD1337.

There is a native gfx1036 measurement for the first-wave bit. @wasonidas observed 80000004 00100044 00200084 003000c4 for a wave32 group of 128 threads, with bit 31 set on wave 0. The report also covers wave64 and partial groups. Native gfx1036 evidence

I built this PR’s synthetic cs_tgsize fixture at 968cd6d15e340b37910bf3431bbb090ae95b7382, validated the translated module with spirv-val, and dispatched two 64×2×1 groups on Linux, RTX 5090, NVIDIA 595.99.02. Each group returned 00000004 00100044 00200084 003000c4. The only difference from the native four-wave reference is bit 31 on the first word.

The overlapping implementation in #6446 at fb40ed36 already returns the full measured word on this translated path. Please reconcile the lowering and fixture expectation with that PR before merging. The hardware evidence is specific to gfx1036; the bit on gfx1013 remains unverified. Independent translated readback on #6446

AI-assisted.

@ddaikodaiko

Copy link
Copy Markdown
Author

Closing this: #6446 already implements TG_SIZE_EN, and it goes further than this branch does.

What #6446 has that this does not:

  • Hardware measurement. The tools/hw_oracle run on gfx1036 gives 80000004 00100044 00200084 003000c4, which pins bits [24:20] and also first_wave [31] — the field this branch explicitly left unmodelled because Mesa has no user for it and I had no way to confirm it.
  • A corpus count. None of the 2,689 carved CS sets TG_SIZE_EN; 91 compute programs of the pipeline library do. This branch had no work/ folder and could not count at all.
  • Tests in the right place (tests/semantic/test_cs_tg_size.py) rather than a .py unit test plus a hand-written GPU sample.

So there is nothing here worth merging on top. Thanks to @FIocker for the measurement.

AI-assisted, opened by a contributor who did not check the coordination issue first — sorry for the duplicate.

hah346 added a commit to hah346/Leonida that referenced this pull request Nov 23, 2026
Adapt the translator change from ItsNotRexed#7165 to ItsNotRexed#7529 while preserving the FSSM/FSPX dispatch metadata lookup. AI-assisted integration and verification.
@hah346

hah346 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Independent check on Windows build 26300, RTX 2060 (driver 616.64, 32-wide compute subgroups), LLVM MC 23.1.5 and SPIRV-Tools 2026.4. I adapted the translator hunk from 968cd6d onto the #7529 head and retained its shared FSSM/FSPX dispatch-state lookup.

On the unmodified #7529 translator both synthetic TG_SIZE_EN shaders were refused. The integration compiled and validated both. On the RTX 2060, two 64×2×1 workgroups returned 4, 1048644, 2097284, 3145924 per group in wave32 and 2, 1048642 per group in emulated wave64. Every word matched.

Integration source: hah346@95c2aab. A full A/B of the 2,684 retail compute-library programs is still running locally. AI-assisted. No game data or traces shared.

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.

4 participants