shader: accept 2D/3D and wider thread groups in compute_store_aot #702

Merged
roansystemlabs merged 1 commit from upstream-pr/708 into main 2026-11-22 21:59:11 +00:00
roansystemlabs commented 2026-11-22 21:31:40 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by roansystemlabs on 2026-11-22T21:31:40Z.


Change

compute_store_aot accepted only (1|32|64, 1, 1) thread groups. In the fixed-profile archive rescan in docs/experiments.md, this check is the first CS stop for 1,054 of 2,689 compute records. This PR moves that stop. Refs #701.

  • group_size() reads (x, y, z) from PSV0 and accepts any cs_6_0 group: x and y up to 1024, z up to 64, x·y·z up to 1024. The translator emits [numthreads(x,y,z)] unchanged, so an 8×8 group stays 8×8.
  • Local Y and Z bind to v1 and v2, and only when that group dimension is larger than one. gfx10 compute waves receive the three local IDs in separate VGPRs. In LLVM 15.0.7, SITargetLowering::allocateSpecialEntryInputVGPRs uses v0/v1/v2 unless hasPackedTID() is set. FeaturePackedTID belongs to gfx90a, gfx940 and gfx11, and FeatureISAVersion10_1_3 does not have it.
  • --group-reg-y and --group-reg-z bind the group-ID SGPRs in the same way as --group-reg. Bindings must be distinct and must differ from the table register.
  • A dispatch index is v_lshl_add_u32 vD, s<group A>, log2(size A), v<local A>, for a power-of-two size on axis A.
  • An indexed store takes one of two forms:
    • one idxen axis, when every other group dimension is one;
    • a row axis through idxen and a column axis through offen (the v[n:n+1] pair), with the byte offset from v_lshlrev_b32. The store must fit in one element, and one group's row must fit in the stride.
  • All stores in a program must use the same layout, so no two invocations of one group write the same bytes. A dispatch-thread index can also be stored as data (tid.x, …), which the swapped-axis controls need.
  • tests/compute_check.cpp: the store-case groups word now carries the Y and Z group counts in bits 8–15 and 16–23. Existing files are unchanged. Single-axis cases still print groups=N.
  • tools/shader_profile_audit.py passes the PSV0 size through. For the Y and Z candidates it tries the next one or two SGPRs after the group-X candidate (LLVM allocates group IDs as consecutive system SGPRs in X, Y, Z order, each only when the program reads it). A CPU control checks that a 4×1×8 kernel reading group Z from the SGPR after group X is accepted, and one reading it two SGPRs later is not.
  • Docs: new subsection under "Compute UAV stores from a native container", and the README row.

The five existing cases produce byte-identical HLSL before and after; I compared them against the old module.

Verification

Commit b8150d0 (rebased on 343a3a4). Windows 11 build 26200.9974, D3D12 debug layer enabled, NVIDIA GeForce RTX 3070 (driver 621.01) and WARP (Microsoft Basic Render Driver).

ctest --preset dev --no-tests=error -R "^(compute_store_aot_|compute_mesh_texture_|compute_to_pixel_|offscreen_warp|compute_|shader_metadata_census)" -V
100% tests passed, 0 tests failed out of 12

shader_metadata_census (from #617) imports shader_profile_audit and is included because this PR changes that module.

compute_store_aot_hardware and compute_store_aot_warp each passed 15 full 320-word readbacks with mismatches=0 and debug_errors=0 debug_warnings=0: the 8 existing ones plus 7 new ones. The new readbacks:

Group Dispatches Layout
8×8×1 1×1×1 and 1×2×1 row Y (stride 72), 8-byte column X, stores (tid.x, tid.y)
4×1×8 1×1×1 and 1×1×2 row Z (stride 68), 16-byte column X, offset:4, stores (tid.z, tid.x)
4×4×1 1×1×1 and 3×2×1 row Y (stride 52), 4-byte column X, stores tid.x
256×1×1 1×1×1 idxen X (stride 4), stores tid.x

An independent per-invocation model in the test supplies the expected words. It does not call the translator. On each adapter, every multi-dimensional shape must also fail for:

  • a flipped guard word;
  • the first group count against the larger dispatch's reference;
  • recompiled HLSL with swapped axes;
  • recompiled HLSL that ignores Z (4×1×8 only);
  • recompiled HLSL with half the group X size.

That makes 15 expected failures per adapter, all detected. The two compute_to_pixel_* debug warnings (D3D12 1328, ignored initial state) are pre-existing and outside this change.

CPU controls (controls(), run in both modes) reject:

  • group sizes outside the limits;
  • shifts that no longer match the size;
  • a non-power-of-two size whose shift still matches (12);
  • swapped local or group axes;
  • v1 read in a group whose Y is one;
  • missing, wrong or duplicate group registers;
  • an element or row overflow;
  • idxen alone in a 2D group;
  • offen without idxen;
  • an index and a byte offset from the same axis;
  • two stores with transposed layouts;
  • an unresolved local ID stored as data;
  • an audit candidate whose group Z is not in the SGPR after group X.

Mutation check (local script, not added to the repo): I made 16 single-line mutants of compute_store_aot.py, each run through compute_store_check.py draw on WARP and on the RTX 3070. All 16 were killed. The mutants covered: swapped axes in the HLSL, a flattened 1D group, Y/Z always bound, non-power-of-two sizes, an unchecked local axis, and each removed coverage, row, element, layout, same-axis and duplicate-register check. The rest were relaxed Z or total limits, a dropped column shift, group Y bound as X, and a byte offset from a local ID. A separate review pass added six more mutants on both adapters, all killed (five by the CPU controls before any dispatch, one by the store-address check): Z ignored by the coverage check, the column multiplied instead of shifted, a smaller dispatch shift accepted, every stored index emitted as tid.x, the idxen/offen roles swapped, and the audit reading group Z two SGPRs after X (killed only after the audit control above was added).

Pure-Python checks from AGENTS.md, plus docs_links_check.py: all exit 0.

compute_store_check.py binary needs LLVM 15 with AMDGPU, which I don't have locally. It ran on the fork's Ubuntu job (LLVM 15) with workflow_dispatch at b8150d0, along with the other binary checks in that job. The job passed: https://github.com/roansystemlabs/Xbox2Windows/actions/runs/45071780235. The other jobs in that run also passed: the Windows build and tests, the Windows /O2 build and the Linux Python checks. This includes the new shapes assembled with llvm-mc-15, decoded through libLLVM-15.so.1, and the CLI producing the same HLSL as the line fixtures.

GPU load during the runs (nvidia-smi): 2 % before and after, with about 5.6 GB already in use by other processes on the machine. The tests are short dispatches.

Evidence and follow-up

  • Fixtures: original synthetic gfx1013 kernels written for this test, MIT. No game files.
  • The census numbers are not remeasured. I don't have the archive. Could you rerun the CS rows of the fixed-profile rescan with tools/shader_profile_audit.py on this branch? I expect the 1,054 records to stop at a later instruction, not to pass.
  • Many real 2D kernels will still stop. They compute addresses with ALU forms this profile doesn't lower: v_mad_u32_u24 row pitches, v_lshl_add_u32 with other operands, bounds checks and branches. Reusing the pixel_aot ALU lowering in compute would be a separate design question, and I'd ask on #701 first.
  • Assumptions:
    • The group-ID SGPRs of an original program remain a caller binding, as --group-reg already is.
    • The audit candidates follow LLVM's X, Y, Z order, not a measured console layout.
    • Out-of-range structured-buffer behavior is not modelled. The caller's stride and dispatch must keep every row in range, and the tests stay in range.
  • Not covered: a group whose Y or Z size is 1 dispatched with several groups on that axis, where the kernel reads the group ID directly. 3D groups whose address separates all three axes need ALU forms this profile doesn't lower.
  • The audit still reports CS failures as store lowering. If it would help the rescan, a follow-up could append the first store-profile diagnostic, as the PS rows already do.

Checklist

  • This pull request covers one reviewable behavior.
  • I included the exact command and result for every check I ran.
  • I considered a negative control or failure case for a new fixture.
  • I updated README/docs when a capability or build requirement changed.
  • I did not include game files, dumps, distribution links or secrets.

AI-assisted: yes. Code, tests and this description were prepared with an AI coding agent and checked by running the commands above.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6548052314 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/708). Originally posted by [roansystemlabs](https://github.com/roansystemlabs) on 2026-11-22T21:31:40Z. --- ## Change `compute_store_aot` accepted only `(1|32|64, 1, 1)` thread groups. In the fixed-profile archive rescan in `docs/experiments.md`, this check is the first CS stop for **1,054 of 2,689** compute records. This PR moves that stop. Refs #701. - `group_size()` reads `(x, y, z)` from PSV0 and accepts any `cs_6_0` group: x and y up to 1024, z up to 64, x·y·z up to 1024. The translator emits `[numthreads(x,y,z)]` unchanged, so an 8×8 group stays 8×8. - Local Y and Z bind to `v1` and `v2`, and only when that group dimension is larger than one. gfx10 compute waves receive the three local IDs in separate VGPRs. In LLVM 15.0.7, `SITargetLowering::allocateSpecialEntryInputVGPRs` uses `v0`/`v1`/`v2` unless `hasPackedTID()` is set. `FeaturePackedTID` belongs to gfx90a, gfx940 and gfx11, and `FeatureISAVersion10_1_3` does not have it. - `--group-reg-y` and `--group-reg-z` bind the group-ID SGPRs in the same way as `--group-reg`. Bindings must be distinct and must differ from the table register. - A dispatch index is `v_lshl_add_u32 vD, s<group A>, log2(size A), v<local A>`, for a power-of-two size on axis A. - An indexed store takes one of two forms: - one `idxen` axis, when every other group dimension is one; - a row axis through `idxen` and a column axis through `offen` (the `v[n:n+1]` pair), with the byte offset from `v_lshlrev_b32`. The store must fit in one element, and one group's row must fit in the stride. - All stores in a program must use the same layout, so no two invocations of one group write the same bytes. A dispatch-thread index can also be stored as data (`tid.x`, …), which the swapped-axis controls need. - `tests/compute_check.cpp`: the store-case `groups` word now carries the Y and Z group counts in bits 8–15 and 16–23. Existing files are unchanged. Single-axis cases still print `groups=N`. - `tools/shader_profile_audit.py` passes the PSV0 size through. For the Y and Z candidates it tries the next one or two SGPRs after the group-X candidate (LLVM allocates group IDs as consecutive system SGPRs in X, Y, Z order, each only when the program reads it). A CPU control checks that a 4×1×8 kernel reading group Z from the SGPR after group X is accepted, and one reading it two SGPRs later is not. - Docs: new subsection under "Compute UAV stores from a native container", and the README row. The five existing cases produce byte-identical HLSL before and after; I compared them against the old module. ## Verification Commit `b8150d0` (rebased on `343a3a4`). Windows 11 build 26200.9974, D3D12 debug layer enabled, NVIDIA GeForce RTX 3070 (driver 621.01) and WARP (Microsoft Basic Render Driver). ``` ctest --preset dev --no-tests=error -R "^(compute_store_aot_|compute_mesh_texture_|compute_to_pixel_|offscreen_warp|compute_|shader_metadata_census)" -V 100% tests passed, 0 tests failed out of 12 ``` `shader_metadata_census` (from #617) imports `shader_profile_audit` and is included because this PR changes that module. `compute_store_aot_hardware` and `compute_store_aot_warp` each passed 15 full 320-word readbacks with `mismatches=0` and `debug_errors=0 debug_warnings=0`: the 8 existing ones plus 7 new ones. The new readbacks: | Group | Dispatches | Layout | |---|---|---| | 8×8×1 | 1×1×1 and 1×2×1 | row Y (stride 72), 8-byte column X, stores `(tid.x, tid.y)` | | 4×1×8 | 1×1×1 and 1×1×2 | row Z (stride 68), 16-byte column X, `offset:4`, stores `(tid.z, tid.x)` | | 4×4×1 | 1×1×1 and 3×2×1 | row Y (stride 52), 4-byte column X, stores `tid.x` | | 256×1×1 | 1×1×1 | `idxen` X (stride 4), stores `tid.x` | An independent per-invocation model in the test supplies the expected words. It does not call the translator. On each adapter, every multi-dimensional shape must also fail for: - a flipped guard word; - the first group count against the larger dispatch's reference; - recompiled HLSL with swapped axes; - recompiled HLSL that ignores Z (4×1×8 only); - recompiled HLSL with half the group X size. That makes 15 expected failures per adapter, all detected. The two `compute_to_pixel_*` debug warnings (`D3D12 1328`, ignored initial state) are pre-existing and outside this change. CPU controls (`controls()`, run in both modes) reject: - group sizes outside the limits; - shifts that no longer match the size; - a non-power-of-two size whose shift still matches (12); - swapped local or group axes; - `v1` read in a group whose Y is one; - missing, wrong or duplicate group registers; - an element or row overflow; - `idxen` alone in a 2D group; - `offen` without `idxen`; - an index and a byte offset from the same axis; - two stores with transposed layouts; - an unresolved local ID stored as data; - an audit candidate whose group Z is not in the SGPR after group X. Mutation check (local script, not added to the repo): I made 16 single-line mutants of `compute_store_aot.py`, each run through `compute_store_check.py draw` on WARP and on the RTX 3070. All 16 were killed. The mutants covered: swapped axes in the HLSL, a flattened 1D group, Y/Z always bound, non-power-of-two sizes, an unchecked local axis, and each removed coverage, row, element, layout, same-axis and duplicate-register check. The rest were relaxed Z or total limits, a dropped column shift, group Y bound as X, and a byte offset from a local ID. A separate review pass added six more mutants on both adapters, all killed (five by the CPU controls before any dispatch, one by the store-address check): Z ignored by the coverage check, the column multiplied instead of shifted, a smaller dispatch shift accepted, every stored index emitted as `tid.x`, the `idxen`/`offen` roles swapped, and the audit reading group Z two SGPRs after X (killed only after the audit control above was added). Pure-Python checks from `AGENTS.md`, plus `docs_links_check.py`: all exit 0. `compute_store_check.py binary` needs LLVM 15 with AMDGPU, which I don't have locally. It ran on the fork's Ubuntu job (LLVM 15) with workflow_dispatch at `b8150d0`, along with the other binary checks in that job. The job passed: https://github.com/roansystemlabs/Xbox2Windows/actions/runs/45071780235. The other jobs in that run also passed: the Windows build and tests, the Windows /O2 build and the Linux Python checks. This includes the new shapes assembled with `llvm-mc-15`, decoded through `libLLVM-15.so.1`, and the CLI producing the same HLSL as the line fixtures. GPU load during the runs (`nvidia-smi`): 2 % before and after, with about 5.6 GB already in use by other processes on the machine. The tests are short dispatches. ## Evidence and follow-up - **Fixtures:** original synthetic gfx1013 kernels written for this test, MIT. No game files. - **The census numbers are not remeasured.** I don't have the archive. Could you rerun the CS rows of the fixed-profile rescan with `tools/shader_profile_audit.py` on this branch? I expect the 1,054 records to stop at a later instruction, not to pass. - **Many real 2D kernels will still stop.** They compute addresses with ALU forms this profile doesn't lower: `v_mad_u32_u24` row pitches, `v_lshl_add_u32` with other operands, bounds checks and branches. Reusing the `pixel_aot` ALU lowering in compute would be a separate design question, and I'd ask on #701 first. - **Assumptions:** - The group-ID SGPRs of an original program remain a caller binding, as `--group-reg` already is. - The audit candidates follow LLVM's X, Y, Z order, not a measured console layout. - Out-of-range structured-buffer behavior is not modelled. The caller's stride and dispatch must keep every row in range, and the tests stay in range. - **Not covered:** a group whose Y or Z size is 1 dispatched with several groups on that axis, where the kernel reads the group ID directly. 3D groups whose address separates all three axes need ALU forms this profile doesn't lower. - The audit still reports CS failures as `store lowering`. If it would help the rescan, a follow-up could append the first store-profile diagnostic, as the PS rows already do. ## Checklist - [x] This pull request covers one reviewable behavior. - [x] I included the exact command and result for every check I ran. - [x] I considered a negative control or failure case for a new fixture. - [x] I updated README/docs when a capability or build requirement changed. - [x] I did not include game files, dumps, distribution links or secrets. AI-assisted: yes. Code, tests and this description were prepared with an AI coding agent and checked by running the commands above.
upstream-sync closed this pull request 2026-11-24 06:44:26 +00:00
Joacoorandom commented 2026-11-24 06:48:46 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Joacoorandom on 2026-11-22T21:37:11Z.


Independent CPU-only check of b8150d0c2146dddfbb0664dae56d9b25f3d94e7a on Linux/Python 3.14.4:

python3 -B - <<'PY'
import sys
sys.path[:0] = ['tests', 'tools']
import compute_store_check as c
import compute_store_aot as a
c.controls()
for dims in ((8,8,1), (4,1,8), (16,16,4), (1024,1,1)):
    assert a.checked_group(dims) == dims
for dims in ((1024,2,1), (1,1,65), (0,8,1)):
    try:
        a.checked_group(dims)
    except ValueError:
        pass
    else:
        raise AssertionError(dims)
print('PASS')
PY

Result: PASS, including the PR's grid/axis/layout rejection controls and the additional group-size boundaries above. This checks translator acceptance and generated expressions only; I have not independently run the assembler or Windows/D3D12 readback cases, so this is not a GPU validation or merge approval.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7086187348 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/708#issuecomment-7086187348). Originally posted by [Joacoorandom](https://github.com/Joacoorandom) on 2026-11-22T21:37:11Z. --- Independent CPU-only check of `b8150d0c2146dddfbb0664dae56d9b25f3d94e7a` on Linux/Python 3.14.4: ```sh python3 -B - <<'PY' import sys sys.path[:0] = ['tests', 'tools'] import compute_store_check as c import compute_store_aot as a c.controls() for dims in ((8,8,1), (4,1,8), (16,16,4), (1024,1,1)): assert a.checked_group(dims) == dims for dims in ((1024,2,1), (1,1,65), (0,8,1)): try: a.checked_group(dims) except ValueError: pass else: raise AssertionError(dims) print('PASS') PY ``` Result: PASS, including the PR's grid/axis/layout rejection controls and the additional group-size boundaries above. This checks translator acceptance and generated expressions only; I have not independently run the assembler or Windows/D3D12 readback cases, so this is not a GPU validation or merge approval.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
R0Y00/Xbox2Windows!702
No description provided.