startup: fold s_brev_b32 of a known constant in gfx2hlsl #2311

Merged
denn0x merged 2 commits from upstream-pr/2319 into main 2026-11-25 12:30:12 +00:00
denn0x commented 2026-11-25 12:09:00 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by denn0x on 2026-11-25T12:09:00Z.


What

An inline sampler descriptor can be built from scalar constants with s_movk_i32, s_bfm_b32, s_mov_b32 and s_brev_b32. For example, s_brev_b32 sN, 1 sets only bit 31. The first three already gave known words in tools/startup/gfx2hlsl.py. s_brev_b32 was emitted as a run-time reversebits, so a descriptor with one such word was refused as descriptor operand s[24:27] holds ('const', 0).

Stage.scalar now folds the operations listed in SCALAR_CONST_FOLD when every operand is a known constant (an inline integer or an SGPR that holds a constant). Only s_brev_b32 is listed (AMD RDNA 2 ISA: D[31:0] = S0[0:31], no SCC write). A run-time operand keeps reversebits.

Cache revision. The change alters the HLSL emitted for stages that already translated: reversebits(c) becomes the folded word. The shader cache revision therefore moves to x2w-startup-shader-13. The new value appears in:

  • d3d12_draw.inl;
  • pipeline_key and compute_key;
  • startup_indirect_draw_indexed.cpp;
  • the compute key literal in startup_compute_request_offline_check.py;
  • docs/startup-runtime.md.

I skipped 12 because open #2308 takes it. Whichever PR lands second will need a rebase of the revision lines.

Test

tests/startup_gfx2hlsl_scalar_brev_fold_check.py (new). It is registered in CMake as startup_gfx2hlsl_scalar_brev_fold, in the Python CI list and in AGENTS.md. The fixture is a hand-assembled pixel stage, and the decoder reads it back. The expected values are computed bit by bit in the test, not with the translator's table.

  • Folded: s_brev_b32 s1, 1 gives 0x80000000, and a brev of the folded s_bfm_b32 s0, 2, 22 gives 0x00000300. Both are known constants, and the emitted lines are the folded words.
  • Sampler: the recorded startup_image_3d_check stage, with its fourth sampler word written by s_brev_b32 s27, 0, now samples through the constant sampler smp 0 0x0 0x0 0x0 0x0.
  • Controls:
    • a source that is not known (user SGPR s5) keeps s2 = reversebits(s5);;
    • with the fold table emptied, the brev results are not constants and the sampler is refused as before.
python -B tests/startup_gfx2hlsl_scalar_brev_fold_check.py
scalar const fold: s_brev_b32 1 = 0x80000000, brev of the folded s_bfm_b32 2, 22 (0x00c00000) = 0x00000300; controls: unknown operand keeps reversebits, no brev constants without the table; a sampler word from s_brev_b32 is a constant descriptor (refused without the table)

On main, the check fails at the first constant (AssertionError: (1, None)).

Other checks on this branch:

  • every tests/startup_gfx2hlsl_*_check.py, tests/startup_image_*_check.py, startup_translator_check.py, startup_flat_param_check.py, startup_inline_sampler_check.py and startup_compute_request_offline_check.py passes;
  • the runtime key checks pass on WARP against the rebuilt startup_stream_consumer: startup_stream_check.py and startup_compute_request_check.py, plus its disabled negative control, which prints the expected FAIL;
  • startup_indirect_draw_indexed warp and warp draw pass.

Environment

  • Build: all builds (Ninja Release, MSVC Build Tools 18).
  • Software: Windows 10.0.26300.9974, WARP.
  • GPU: no hardware runs, no capability skips.

Refs #1723

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6604138663 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2319). Originally posted by [denn0x](https://github.com/denn0x) on 2026-11-25T12:09:00Z. --- ## What An inline sampler descriptor can be built from scalar constants with `s_movk_i32`, `s_bfm_b32`, `s_mov_b32` and `s_brev_b32`. For example, `s_brev_b32 sN, 1` sets only bit 31. The first three already gave known words in `tools/startup/gfx2hlsl.py`. `s_brev_b32` was emitted as a run-time `reversebits`, so a descriptor with one such word was refused as `descriptor operand s[24:27] holds ('const', 0)`. `Stage.scalar` now folds the operations listed in `SCALAR_CONST_FOLD` when every operand is a known constant (an inline integer or an SGPR that holds a constant). Only `s_brev_b32` is listed (AMD RDNA 2 ISA: `D[31:0] = S0[0:31]`, no SCC write). A run-time operand keeps `reversebits`. **Cache revision.** The change alters the HLSL emitted for stages that already translated: `reversebits(c)` becomes the folded word. The shader cache revision therefore moves to `x2w-startup-shader-13`. The new value appears in: - `d3d12_draw.inl`; - `pipeline_key` and `compute_key`; - `startup_indirect_draw_indexed.cpp`; - the compute key literal in `startup_compute_request_offline_check.py`; - `docs/startup-runtime.md`. I skipped 12 because open #2308 takes it. Whichever PR lands second will need a rebase of the revision lines. ## Test `tests/startup_gfx2hlsl_scalar_brev_fold_check.py` (new). It is registered in CMake as `startup_gfx2hlsl_scalar_brev_fold`, in the Python CI list and in AGENTS.md. The fixture is a hand-assembled pixel stage, and the decoder reads it back. The expected values are computed bit by bit in the test, not with the translator's table. - **Folded:** `s_brev_b32 s1, 1` gives 0x80000000, and a brev of the folded `s_bfm_b32 s0, 2, 22` gives 0x00000300. Both are known constants, and the emitted lines are the folded words. - **Sampler:** the recorded `startup_image_3d_check` stage, with its fourth sampler word written by `s_brev_b32 s27, 0`, now samples through the constant sampler `smp 0 0x0 0x0 0x0 0x0`. - **Controls:** - a source that is not known (user SGPR s5) keeps `s2 = reversebits(s5);`; - with the fold table emptied, the brev results are not constants and the sampler is refused as before. ``` python -B tests/startup_gfx2hlsl_scalar_brev_fold_check.py scalar const fold: s_brev_b32 1 = 0x80000000, brev of the folded s_bfm_b32 2, 22 (0x00c00000) = 0x00000300; controls: unknown operand keeps reversebits, no brev constants without the table; a sampler word from s_brev_b32 is a constant descriptor (refused without the table) ``` On `main`, the check fails at the first constant (`AssertionError: (1, None)`). **Other checks on this branch:** - every `tests/startup_gfx2hlsl_*_check.py`, `tests/startup_image_*_check.py`, `startup_translator_check.py`, `startup_flat_param_check.py`, `startup_inline_sampler_check.py` and `startup_compute_request_offline_check.py` passes; - the runtime key checks pass on WARP against the rebuilt `startup_stream_consumer`: `startup_stream_check.py` and `startup_compute_request_check.py`, plus its `disabled` negative control, which prints the expected `FAIL`; - `startup_indirect_draw_indexed warp` and `warp draw` pass. **Environment** - **Build:** `all` builds (Ninja Release, MSVC Build Tools 18). - **Software:** Windows 10.0.26300.9974, WARP. - **GPU:** no hardware runs, no capability skips. Refs #1723
upstream-sync closed this pull request 2026-11-25 13:00:45 +00:00
tedin7 commented 2026-11-25 13:00:46 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T12:14:23Z.

Review: COMMENTED


Review of head 194815adf67cb2d48a855e744f3e0445d94049f2 (comment only).

Summary: the fold logic is correct and the new test kills mutations. The PR cannot merge as it stands: it conflicts with main on the shader cache revision.

P1 (blocker): merge conflict, revision now stale. gh pr view reports CONFLICTING. #2308 is merged, so main (31f46857) already carries x2w-startup-shader-12. The PR bumps 11 to 13. git merge-tree origin/main HEAD reports conflicts in src/startup_stubs/d3d12_draw.inl, docs/startup-runtime.md and tests/startup_compute_request_offline_check.py. After the rebase, resolve each place to -13 (and -13cs) and check these all agree: d3d12_draw.inl (both literals), gfx2hlsl.py pipeline_key and compute_key, startup_indirect_draw_indexed.cpp, the offline check literal and the docs line. Keep the docs sentence about the #2308 change as well as the new one.

P2: none.

Nit

  • tools/startup/gfx2hlsl.py (new SCALAR_CONST_FOLD block): the new comment sits between the "shift amount of the scalar shift-and-add forms" comment and SHIFT_ADD, so that comment no longer sits next to the table it describes. Move the new block above it.

Correctness (checked against the diff)

  • s_brev_b32 D = S0 bit-reversed, no SCC write (RDNA 2 ISA). The fold writes no SCC and discards the mask, consistent with that.
  • Operands are masked to 32 bits, so negative inline integers behave. A float inline constant (FLOATS) is not folded. An unknown SGPR keeps reversebits.

Commands run in the worktree (all passed)

  • python3 -B tests/startup_gfx2hlsl_scalar_brev_fold_check.py
  • python3 -B tests/startup_compute_request_offline_check.py
  • python3 -B tests/startup_inline_sampler_check.py
  • python3 -B tests/startup_gfx2hlsl_sopk_signext_check.py
  • python3 tests/docs_links_check.py (448 links)
  • python3 tests/bidi_controls_check.py
  • git diff --check origin/main...HEAD (clean)

Mutations (each reverted with git checkout -- .)

  1. Removed the bit reversal in the fold lambda: test fails with AssertionError: (1, ('const', 1)). Killed.
  2. Disabled the fold branch (if None not in operands to if False): test fails with AssertionError: (1, None). Killed.

CI / hygiene: no checks are reported on the branch (gh pr checks). Title and body are neutral. The test is wired into CMake, windows.yml and AGENTS.md. I did not run the Windows WARP job: the change is Python-only constant folding covered by the offline test. I ran the new test script directly, not through ctest.

Ask: rebase on main, resolve the revision conflicts as above, then push.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6057404827 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2319#pullrequestreview-6057404827). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T12:14:23Z. **Review:** COMMENTED --- Review of head `194815adf67cb2d48a855e744f3e0445d94049f2` (comment only). **Summary:** the fold logic is correct and the new test kills mutations. The PR cannot merge as it stands: it conflicts with main on the shader cache revision. **P1 (blocker): merge conflict, revision now stale.** `gh pr view` reports `CONFLICTING`. #2308 is merged, so main (`31f46857`) already carries `x2w-startup-shader-12`. The PR bumps 11 to 13. `git merge-tree origin/main HEAD` reports conflicts in `src/startup_stubs/d3d12_draw.inl`, `docs/startup-runtime.md` and `tests/startup_compute_request_offline_check.py`. After the rebase, resolve each place to `-13` (and `-13cs`) and check these all agree: `d3d12_draw.inl` (both literals), `gfx2hlsl.py` `pipeline_key` and `compute_key`, `startup_indirect_draw_indexed.cpp`, the offline check literal and the docs line. Keep the docs sentence about the #2308 change as well as the new one. **P2: none.** **Nit** - `tools/startup/gfx2hlsl.py` (new `SCALAR_CONST_FOLD` block): the new comment sits between the "shift amount of the scalar shift-and-add forms" comment and `SHIFT_ADD`, so that comment no longer sits next to the table it describes. Move the new block above it. **Correctness (checked against the diff)** - `s_brev_b32` D = S0 bit-reversed, no SCC write (RDNA 2 ISA). The fold writes no SCC and discards the mask, consistent with that. - Operands are masked to 32 bits, so negative inline integers behave. A float inline constant (`FLOATS`) is not folded. An unknown SGPR keeps `reversebits`. **Commands run in the worktree (all passed)** - `python3 -B tests/startup_gfx2hlsl_scalar_brev_fold_check.py` - `python3 -B tests/startup_compute_request_offline_check.py` - `python3 -B tests/startup_inline_sampler_check.py` - `python3 -B tests/startup_gfx2hlsl_sopk_signext_check.py` - `python3 tests/docs_links_check.py` (448 links) - `python3 tests/bidi_controls_check.py` - `git diff --check origin/main...HEAD` (clean) **Mutations (each reverted with `git checkout -- .`)** 1. Removed the bit reversal in the fold lambda: test fails with `AssertionError: (1, ('const', 1))`. Killed. 2. Disabled the fold branch (`if None not in operands` to `if False`): test fails with `AssertionError: (1, None)`. Killed. **CI / hygiene:** no checks are reported on the branch (`gh pr checks`). Title and body are neutral. The test is wired into CMake, `windows.yml` and `AGENTS.md`. I did not run the Windows WARP job: the change is Python-only constant folding covered by the offline test. I ran the new test script directly, not through ctest. Ask: rebase on main, resolve the revision conflicts as above, then push. AI-assisted; commands listed were executed locally.
R0Y00 commented 2026-11-25 13:00:58 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-25T12:27:46Z.


Merged current main into the PR at 1827682b. The graphics and compute keys and their fixtures now use revision 13 together, and the revision 12 SAVEEXEC fix and regression are still present.

Locally the constant-folding, request-key, SAVEEXEC and translator Python checks passed. The Windows Release stream, compute-request, indirect-draw and folding selection passed all 11 tests after building the separate stream-depth target and rerunning that case. Hosted CI is running on the new head.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7162405341 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2319#issuecomment-7162405341). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-25T12:27:46Z. --- Merged current main into the PR at `1827682b`. The graphics and compute keys and their fixtures now use revision 13 together, and the revision 12 SAVEEXEC fix and regression are still present. Locally the constant-folding, request-key, SAVEEXEC and translator Python checks passed. The Windows Release stream, compute-request, indirect-draw and folding selection passed all 11 tests after building the separate stream-depth target and rerunning that case. Hosted CI is running on the new head.
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!2311
No description provided.