Skip to content

rdna2spirv: write the compute thread-group SGPR (TG_SIZE_EN) - #6446

Open
FIocker wants to merge 2 commits into
ItsNotRexed:mainfrom
FIocker:feat/cs-tg-size
Open

FIocker wants to merge 2 commits into
ItsNotRexed:mainfrom
FIocker:feat/cs-tg-size

Conversation

@FIocker

@FIocker FIocker commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Claimed in #5781 (comment).

What

RSRC2 TG_SIZE_EN (bit 10) adds one SGPR after the user data and the enabled workgroup ids. Until now translate() refused every compute shader that sets it ("CS thread-group-size SGPR"). None of the 2,689 carved CS sets it. 91 of the 2,684 compute programs of the pipeline library in shaders.rpf do (docs/shader_library.md).

The prologue now writes that SGPR:

  • bits [5:0]: the number of waves in the group, ceil(threads / W)
  • bits [11:6]: this wave's index in the group, LocalInvocationIndex / W (the rule s_get_waveid_in_workgroup already uses)
  • all other bits 0

W is 32 or 64, from the shader's wave size.

Why this layout

  • Mesa, src/amd/common/nir/ac_nir_lower_intrinsics_to_args.c, for GFX6-10:
    • load_num_subgroups reads tg_size[5:0].
    • load_subgroup_id reads tg_size[11:6].
    • A wave64 compute shader's local invocation index is mbcnt(~0, tg_size & 0xfc0).
  • The library programs read the register the same way. In all 91, the first read of it or of a copy is s_and_b32 sN, sTG, 0xfc0. Most then do v_mbcnt_lo_u32_b32 vX, -1, sN.
  • GFX10.3 also has a wave id in bits [24:20]. Bits above 11 are left 0 here; gfx1013 is GFX10.1. One library program extracts bits [24:20] with s_bfe_u32 ..., 0x50014, but I couldn't confirm the source operand is this register.

Not measured on hardware. I have no AMD GPU here. A run through tools/hw_oracle on gfx103x would settle it: dispatch with TG_SIZE_EN and dump the SGPR.

Tests (CPU only, no game data)

  • tests/semantic/test_cs_tg_size.py (new) translates hand-written listings and runs every invocation with run_workgroup:
    • the local index through the 0xfc0 field: wave64 with v_mbcnt_lo/hi, wave32 with >> 1, for 1-D, 2-D and 3-D groups;
    • the wave count, including a partial last wave (100 threads in wave64 gives 2; 6×6 in wave32 gives 2);
    • the whole value;
    • the register's position after three workgroup ids (s7);
    • no write without the bit.
  • tests/test_translate.py::test_compute_thread_group_size_sgpr_is_written and tests/semantic/test_lds_basic_alloc.py::test_tg_size_en_accepted: these two pinned the refusal and now check that the shader translates.
  • tests/test_translate.py: 633 passed (llvm-mc 21.1.8, spirv-tools on PATH).
  • The full tests/semantic suite: 6,154 passed, 1 skipped, 1 xfailed, 75,101 subtests passed, no failures (llvm-mc 21.1.8, spirv-tools on PATH, Windows 11).

Effect on the library (counts only)

The 91 TG_SIZE_EN programs, disassembled with llvm-mc 21.1.8 for gfx1013:

main 561b65a8 this PR
translate and pass spirv-val 0 86
refused for the TG SGPR 90 0
refused for something else 1 5

The other 5 are three s_flbit_i32_b64 on a lane mask, one ds_append and one printed-encoding difference (v_fma_mix_f32).

The carved corpus is unchanged: no carved CS sets the bit.

docs/design.md (Compute shaders row) describes the layout. docs/shader_library.md gets its updated counts in the separate follow-up for the #4343 review.

AI-assisted (Claude Code). Windows 11, Python 3.12, no GPU used.

🤖 Generated with Claude Code

RSRC2 bit 10 adds an SGPR after the workgroup ids. The translator refused every compute
shader that sets it. None of the 2,689 carved CS does, but 91 compute programs of the
pipeline library in shaders.rpf do (docs/shader_library.md), and they read it as
s_and_b32 sN, sTG, 0xfc0, mostly followed by v_mbcnt_lo_u32_b32.

The prologue now writes it: waves in the group in [5:0], this wave's index in [11:6], 0
above. That is the GFX6-10 layout Mesa reads (ac_nir_lower_intrinsics_to_args.c:
load_num_subgroups = tg_size[5:0], load_subgroup_id = tg_size[11:6], wave64 local
invocation index = mbcnt(~0, tg_size & 0xfc0)). Thread i is in wave i / W, as for
s_get_waveid_in_workgroup. Not measured on hardware.

tests/semantic/test_cs_tg_size.py runs it on the CPU (local index through the 0xfc0 field
in wave32 and wave64, 1-D and 3-D groups, the wave count with a partial wave, the whole
value, the register after three workgroup ids). The two tests that pinned the refusal now
check that it translates.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@thehollowedone

Copy link
Copy Markdown

Independent Windows check of standalone head 56200058. Windows 11 x64, RTX 3080 Ti / 621.35, MSVC 19.44, Vulkan SDK 1.4.363.0.

All six stock test_cs_tg_size.py tests passed. I used its synthetic inputs to run four translated compute cases on Vulkan. Native wave32 covered 128×1×1 and 6×6×1 groups. Emulated wave64 covered 128×1×1 and 100×1×1 groups. All 392 packed wave-count and wave-index values matched, and all 152 guard words stayed unchanged. All four modules passed spirv-val. Vulkan core and synchronization validation reported zero errors.

This checks the translated Vulkan results on NVIDIA. Synthetic inputs only. AI-assisted.

@SirKingg SirKingg 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.

Reviewed the TG_SIZE_EN prologue, SGPR placement, wave32/64 arithmetic, limits and contract row. No blocking issue found. Mesa's argument declarations put tg_size after enabled workgroup IDs; its intrinsic lowering supports the cited fields for GFX6-10, subject to the ordered-append dispatch condition.

Windows, Python 3.11, no game files:

  • python -m unittest discover -s tests\semantic -p test_cs_tg_size.py -v: 6 passed.
  • python -m unittest discover -s tests\semantic -p test_lds_basic_alloc.py -v: 13 passed.
  • Independent synthetic check: 64 combinations of wave32/64, all 8 TGID masks and group sizes 1, 33, 7x5x3 and 1024; full words matched. Six generated modules assembled and passed spirv-val --target-env vulkan1.2.
  • python -m pytest tests\test_translate.py -q -rs (Python 3.14, pinned dependencies): 633 skipped, llvm-mc unavailable. The full semantic suite was started but interrupted after more than 11 minutes; no completed result is claimed.

No GPU dispatch or native RDNA2 measurement was performed; the upper bits and hardware behaviour remain unverified. Review assisted by AI. No merge requested.

@theswiss24

Copy link
Copy Markdown

Tested 56200058e on four GPUs, Linux, to add Mesa ANV and Linux NVIDIA to the Windows RTX 3080 Ti check above. Full scripts/run_gpu_tests.py with LEONIDA_W64_EMULATE=1, PR merged onto main 4e42451d5, compared with main at the same commit and test programs built from it.

Result: pass, with no change against main. The pass/skip/fail list is identical test by test on all four GPUs.

GPU main PR
GTX 1650 (595.99.02, Ubuntu 24.04) 217/235 passed, 17 skipped 217/235 passed, 17 skipped
UHD 630 (Mesa ANV 25.2.8, Ubuntu 24.04) 200/235 passed, 33 skipped 200/235 passed, 33 skipped
MX150 (580.178.04, Linux Mint 22.3) 207/235 passed, 28 skipped 207/235 passed, 28 skipped
UHD 630 (Mesa ANV 25.2.8, Linux Mint 22.3) 200/235 passed, 33 skipped 200/235 passed, 33 skipped

This PR adds no GPU fixture of its own; on these drivers, writing the TG_SIZE_EN thread-group SGPR changes no existing fixture's result.

Tested and written by Claude (Claude Code).

@wasonidas

Copy link
Copy Markdown

Native gfx1036 hardware check of 56200058eabe7b820b14e29726d2150531081847, Windows 11 build 26300, Ryzen 7 9800X3D integrated Radeon, display driver 32.0.21042.62, HIP runtime 10.0.3652.0. I used the existing tools/hw_oracle launcher and synthetic wave32 and wave64 kernels with .amdhsa_system_sgpr_workgroup_info 1: 100, 128 and 133 threads at each wave width. Two sequential runs matched, with zero failures in the wave-count, wave-index, lane-ID and guard checks.

The translated result matched the low 12 bits in all 722 invocations, but differed from all 722 full native words. For example, the four native wave32 words at 128 threads were 80000004 00100044 00200084 003000c4. In these gfx1036 HIP dispatches, bit 31 marked the first wave and bits [24:20] carried the wave index. The PR synthesizes neither field. The six stock CPU fixture tests passed, and main 1cc6e3c1 refused all six dispatch shapes with the TG SGPR error.

Please document the implemented value as the low 12 bits and record the upper fields observed. These measurements are specific to these HIP dispatches on gfx1036. They do not establish the upper fields on gfx1013 or the low-field behavior under other dispatch settings.

Synthetic inputs only. AI-assisted.

…wave bit (gfx1036 measurement)

@wasonidas measured the TG_SIZE_EN SGPR on gfx1036 through tools/hw_oracle (ItsNotRexed#6446): the low 12
bits matched, and the hardware also sets the wave index in [24:20] (Mesa's GFX10.3 field) and
bit 31 in the group's first wave (wave32 x 128: 80000004 00100044 00200084 003000c4). The prologue
now writes both. One pipeline-library program reads [24:20]; programs that mask with 0xfc0 or 0x3f
see the same value as before. Not measured on gfx1013.

test_cs_tg_size.py: the whole value with the new fields, and the four measured gfx1036 words.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@FIocker

FIocker commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

@wasonidas thank you, that settles the layout better than Mesa alone. Pushed fb40ed3: the prologue now writes the full word you measured, so it is no longer just the low 12 bits:

  • waves in the group in [5:0];
  • the wave index in [11:6] and again in [24:20];
  • bit 31 set in the group's first wave.

test_cs_tg_size.py gains test_matches_the_gfx1036_measurement: wave32 × 128 gives exactly 80000004 00100044 00200084 003000c4. test_whole_value checks the wave64 form.

The upper fields also matter for the console code. One of the 91 pipeline-library programs extracts [24:20] (s_bfe_u32 …, 0x50014). Programs that read the word through & 0xfc0 / & 0x3f see the same value as before. The 91 programs still give 86 translated and passing spirv-val.

The comment and docs/design.md now say that these fields are measured on gfx1036 and not on gfx1013.

AI-assisted (Claude Code).

@tylerstraub

Copy link
Copy Markdown
Contributor

Tested fb40ed369897fe858fecd77977a0200d8a4756c9 on NVIDIA GeForce RTX 5090, 595.99.02 (Linux).

Verified the updated TG_SIZE_EN word on Linux with synthetic wave32 Vulkan shaders. Each invocation stored the complete SGPR at its flattened local index. All 1,258 words matched across workgroups of 128×1×1, 7×5×3, 32×32×1 and 1×1×1. All four modules passed SPIR-V validation and all trailing guard words retained their initial values.

The 128-thread group returned 80000004 00100044 00200084 003000c4 across its four waves, matching the native gfx1036 reference above. The partial fourth wave in the 105-thread group also returned 003000c4. The last wave of the 1,024-thread group returned 01f007e0, exercising index 31 in both fields and wave count 32. The single invocation returned 80000001. This is a check of the translated Vulkan path on NVIDIA.

AI-assisted verification.

@Stalkerino

Copy link
Copy Markdown

Verified on the 42 distinct native compute programs requested during startup of my local 1.0.2.1 copy, after the zlib path from #7180. With the previous translator, 38/42 compile. With #6446 at fb40ed3, 42/42 compile in both native-wave64 and emulated-wave64 mode. Each set independently passes spirv-val --target-env vulkan1.2. The four additional programs are the ones using TG_SIZE_EN. The original 38 modules remain byte-identical within each mode.

I then ran the title offline under CachyOS, Wine 11.22, RX 9070 XT / Mesa RADV 26.3.1. With the 29 existing FSPX modules plus these 42 programs in the runtime catalogue, Xbox device slot 67 returned S_OK and a non-null pipeline object on all 42 calls. The comparison with the base catalogue had 42 E_INVALIDARG results. This run reaches later graphics/resource initialization and stops before the first presentation. Shader inputs and outputs stayed local. AI-assisted verification.

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.

7 participants