startup: lower s_bitreplicate_b64_b32 in gfx2hlsl #3030

Merged
R0D4X merged 3 commits from upstream-pr/3030 into main 2026-11-27 20:04:37 +00:00
R0D4X commented 2026-11-27 08:50:44 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0D4X on 2026-11-27T08:50:44Z.


What this changes

Adds lowering for the 64-bit scalar bit-replicate SOP1 instruction s_bitreplicate_b64_b32 in tools/startup/gfx2hlsl.py (already decoded by tools/disasm_rdna2.py and handled in tools/pixel_aot.py).

  • Semantics: Replicates each bit of the 32-bit source S0 twice into the 64-bit destination SGPR pair s[D:D+1] (D[2*i] = D[2*i+1] = S0[i]), leaving SCC unchanged.
  • On-demand helper & folding: Folds known constants (null, inline integers, 32-bit literals, and constant-tracked SGPRs) at translation time without emitting a helper. For dynamic SGPR sources, emits a compact bitreplicate16(uint x) HLSL helper only when used. When S0 aliases the low word of D (s[D:D+1], sD), writes s(D+1) before sD so the source is read intact.
  • Safety checks: Refuses bad operand counts, modifiers, lane masks (including masks / mask_high on either half of D or on S0), descriptor-bearing SGPRs, VGPRs, odd or out-of-range SGPR pairs, SGPR-pair sources, floats, m0, and src_scc.

Tests added

  • tests/startup_gfx2hlsl_s_bitreplicate_check.py: SOP1 binary decode via G.decode(), disjoint and aliased (s[4:5], s4 and s[4:5], s5) evaluation, constant folding, uniformity tracking, on-demand helper emission/omission, refusal cases, unlowered control, and FXC ps_5_1 + DXC ps_6_0 GPU readback draws (including a negative-control draw).
  • Registered via tests/cmake/startup_gfx2hlsl_s_bitreplicate.cmake and tests/checks/python-linux/startup_gfx2hlsl_s_bitreplicate.txt.

Verification

  • Commit: 2c234a21 (on upstream/main 3b6c38a8)
  • Host: Windows 11 x64 (10.0.26300.9974), MSVC 19.44.35230, Windows SDK 10.0.26100.0, FXC + DXC 1.9.2602.17, D3D12 debug layer enabled
  • Adapters: WARP (1414:008c, driver 10.0.26100.9818) and NVIDIA GeForce RTX 5080 (10de:2c02, driver 32.0.16.2124)

Results:

  • ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_s_bitreplicate' -> 3/3 passed, 0 skipped (debug_errors=0 debug_warnings=0 on all draws; startup_tools_lint also passed)
  • ctest --preset release --no-tests=error -R '^startup_gfx2hlsl_s_bitreplicate' -> 3/3 passed, 0 skipped
  • Repo & neighbor checks (docs_links_check.py, bidi_controls_check.py, scalar/arity/null/refusal checks, git diff --check) -> all passed
  • 5 live mutations on gfx2hlsl.py (breaking s[D:D+1], sD alias order, wrong high-half shift, dropping helper gate, dropping mask_high guard, wrong constant fold) -> 5/5 caught (rc=1)

Scope: Covers synthetic gfx1013 SOP1 fixtures translated by gfx2hlsl.py and run on WARP and RTX 5080; does not establish full-title startup or frame rate.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:5523825692 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3030). Originally posted by [R0D4X](https://github.com/R0D4X) on 2026-11-27T08:50:44Z. --- ## What this changes Adds lowering for the 64-bit scalar bit-replicate SOP1 instruction `s_bitreplicate_b64_b32` in `tools/startup/gfx2hlsl.py` (already decoded by `tools/disasm_rdna2.py` and handled in `tools/pixel_aot.py`). - **Semantics:** Replicates each bit of the 32-bit source `S0` twice into the 64-bit destination SGPR pair `s[D:D+1]` (`D[2*i] = D[2*i+1] = S0[i]`), leaving `SCC` unchanged. - **On-demand helper & folding:** Folds known constants (`null`, inline integers, 32-bit literals, and constant-tracked SGPRs) at translation time without emitting a helper. For dynamic SGPR sources, emits a compact `bitreplicate16(uint x)` HLSL helper only when used. When `S0` aliases the low word of `D` (`s[D:D+1], sD`), writes `s(D+1)` before `sD` so the source is read intact. - **Safety checks:** Refuses bad operand counts, modifiers, lane masks (including `masks` / `mask_high` on either half of `D` or on `S0`), descriptor-bearing SGPRs, VGPRs, odd or out-of-range SGPR pairs, SGPR-pair sources, floats, `m0`, and `src_scc`. ## Tests added - `tests/startup_gfx2hlsl_s_bitreplicate_check.py`: SOP1 binary decode via `G.decode()`, disjoint and aliased (`s[4:5], s4` and `s[4:5], s5`) evaluation, constant folding, uniformity tracking, on-demand helper emission/omission, refusal cases, unlowered control, and FXC `ps_5_1` + DXC `ps_6_0` GPU readback draws (including a negative-control draw). - Registered via `tests/cmake/startup_gfx2hlsl_s_bitreplicate.cmake` and `tests/checks/python-linux/startup_gfx2hlsl_s_bitreplicate.txt`. ## Verification - **Commit:** `2c234a21` (on `upstream/main` `3b6c38a8`) - **Host:** Windows 11 x64 (`10.0.26300.9974`), MSVC `19.44.35230`, Windows SDK `10.0.26100.0`, FXC + DXC `1.9.2602.17`, D3D12 debug layer enabled - **Adapters:** WARP (`1414:008c`, driver `10.0.26100.9818`) and NVIDIA GeForce RTX 5080 (`10de:2c02`, driver `32.0.16.2124`) Results: - `ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_s_bitreplicate'` -> **3/3 passed, 0 skipped** (`debug_errors=0 debug_warnings=0` on all draws; `startup_tools_lint` also passed) - `ctest --preset release --no-tests=error -R '^startup_gfx2hlsl_s_bitreplicate'` -> **3/3 passed, 0 skipped** - Repo & neighbor checks (`docs_links_check.py`, `bidi_controls_check.py`, scalar/arity/null/refusal checks, `git diff --check`) -> **all passed** - 5 live mutations on `gfx2hlsl.py` (breaking `s[D:D+1], sD` alias order, wrong high-half shift, dropping helper gate, dropping `mask_high` guard, wrong constant fold) -> **5/5 caught (`rc=1`)** *Scope:* Covers synthetic gfx1013 SOP1 fixtures translated by `gfx2hlsl.py` and run on WARP and RTX 5080; does not establish full-title startup or frame rate.
tedin7 commented 2026-11-27 10:05:52 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-27T10:05:52Z.

Review: COMMENTED


Review of head 335f050a (comment only).

Read the diff of tools/startup/gfx2hlsl.py (75 lines): op_s_bitreplicate_b64_b32 (operand checks, lane-mask refusals, constant folding, a bitreplicate16 HLSL helper emitted once via uses_bitreplicate, and the aliasing order: when S0 is the low word of the destination the high word is written first). The helper's bit-spread constants are the standard 16-to-32 interleave and match the ISA text in the comment.

The PR's check has the real thing a lowering needs: startup_gfx2hlsl_s_bitreplicate_warp / _hardware draw the translated shader and compare the readback with a CPU reference, plus a _controls test.

Ran locally (Linux): python3 -B tests/startup_gfx2hlsl_s_bitreplicate_check.py (controls): ok.

  • Mutation A: one interleave constant 0x33333333 changed to 0x33333332 in the constant folder: the controls fail ("const 1 folds").
  • Mutation B: aliasing branch disabled (src_reg == d never true): the controls fail ("aliased s[4:5], s4 (12 cases)").

No defect found. It only lacks a Windows receipt on this head: ctest -R "startup_gfx2hlsl_s_bitreplicate" (dev + release).

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6086482014 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3030#pullrequestreview-6086482014). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-27T10:05:52Z. **Review:** COMMENTED --- Review of head 335f050a (comment only). Read the diff of `tools/startup/gfx2hlsl.py` (75 lines): `op_s_bitreplicate_b64_b32` (operand checks, lane-mask refusals, constant folding, a `bitreplicate16` HLSL helper emitted once via `uses_bitreplicate`, and the aliasing order: when S0 is the low word of the destination the high word is written first). The helper's bit-spread constants are the standard 16-to-32 interleave and match the ISA text in the comment. The PR's check has the real thing a lowering needs: `startup_gfx2hlsl_s_bitreplicate_warp` / `_hardware` draw the translated shader and compare the readback with a CPU reference, plus a `_controls` test. Ran locally (Linux): `python3 -B tests/startup_gfx2hlsl_s_bitreplicate_check.py` (controls): ok. - Mutation A: one interleave constant `0x33333333` changed to `0x33333332` in the constant folder: the controls fail ("const 1 folds"). - Mutation B: aliasing branch disabled (`src_reg == d` never true): the controls fail ("aliased s[4:5], s4 (12 cases)"). No defect found. It only lacks a Windows receipt on this head: `ctest -R "startup_gfx2hlsl_s_bitreplicate"` (dev + release). AI-assisted; commands listed were executed locally.
tedin7 commented 2026-11-27 18:15:21 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-27T18:15:21Z.

Review: COMMENTED


Review of 8e0cb550da (the change from bf058e96, updated with main; the PR's own diff is unchanged). Comment only.

op_s_bitreplicate_b64_b32 refuses modifiers, odd or out-of-range destinations, lane masks and non-scalar data, folds constants with the same interleave as the emitted bitreplicate16 helper, and writes the high word first when S0 is the low word of the destination, so S0 is read before it is overwritten. SCC is left unchanged. The helper is emitted once per pipeline through uses_bitreplicate, which is reset with the other uses_* flags. The check draws the translated shader on WARP and compares with an independent CPU reference. No other open or merged PR lowers this opcode in gfx2hlsl.py.

Windows results are in the comment below.

Commands: git diff origin/main...pull/3030/head (read), plus the Windows commands listed in the comment.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6091986895 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3030#pullrequestreview-6091986895). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-27T18:15:21Z. **Review:** COMMENTED --- Review of 8e0cb550da0d3e6275c0ce0272d93e9fb9a8d5af (the change from bf058e96, updated with main; the PR's own diff is unchanged). Comment only. `op_s_bitreplicate_b64_b32` refuses modifiers, odd or out-of-range destinations, lane masks and non-scalar data, folds constants with the same interleave as the emitted `bitreplicate16` helper, and writes the high word first when S0 is the low word of the destination, so S0 is read before it is overwritten. SCC is left unchanged. The helper is emitted once per pipeline through `uses_bitreplicate`, which is reset with the other `uses_*` flags. The check draws the translated shader on WARP and compares with an independent CPU reference. No other open or merged PR lowers this opcode in `gfx2hlsl.py`. Windows results are in the comment below. Commands: `git diff origin/main...pull/3030/head` (read), plus the Windows commands listed in the comment. AI-assisted; commands listed were executed locally.
tedin7 commented 2026-11-27 18:15:22 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-27T18:15:22Z.


Windows Server 2025 verification of 8e0cb550da0d3e6275c0ce0272d93e9fb9a8d5af (D3D12 WARP, MSVC 14.44.35207):

  • dev: cmake --build --preset dev --target offscreen_check; ctest --preset dev -R '^startup_gfx2hlsl_': 379/379 passed, including startup_gfx2hlsl_s_bitreplicate_controls, _warp and _hardware
  • release: cmake --build --preset release --target offscreen_check; ctest --preset release -R '^startup_gfx2hlsl_': 379/379 passed
  • Windows Python checks selected for tools/startup/gfx2hlsl.py: all ok; tests/startup_run_sh_check.py skips (77) on Windows

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7225296335 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3030#issuecomment-7225296335). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-27T18:15:22Z. --- Windows Server 2025 verification of `8e0cb550da0d3e6275c0ce0272d93e9fb9a8d5af` (D3D12 WARP, MSVC 14.44.35207): - dev: `cmake --build --preset dev --target offscreen_check`; `ctest --preset dev -R '^startup_gfx2hlsl_'`: 379/379 passed, including `startup_gfx2hlsl_s_bitreplicate_controls`, `_warp` and `_hardware` - release: `cmake --build --preset release --target offscreen_check`; `ctest --preset release -R '^startup_gfx2hlsl_'`: 379/379 passed - Windows Python checks selected for `tools/startup/gfx2hlsl.py`: all ok; `tests/startup_run_sh_check.py` skips (77) on Windows AI-assisted; commands listed were executed locally.
tedin7 commented 2026-11-27 20:04:22 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-27T20:04:22Z.


Windows Server 2025 re-verification of 5a5746dc4e036bcb6947d78e0785e4959d822b82 (the same change, updated with main again; D3D12 WARP, MSVC 14.44.35207):

  • dev: cmake --build --preset dev --target offscreen_check; ctest --preset dev -R '^startup_gfx2hlsl_': 379/379 passed, including startup_gfx2hlsl_s_bitreplicate_controls, _warp and _hardware
  • release: cmake --build --preset release --target offscreen_check; ctest --preset release -R '^startup_gfx2hlsl_': 379/379 passed
  • Windows Python checks selected for tools/startup/gfx2hlsl.py: all ok; tests/startup_run_sh_check.py skips (77) on Windows

A first run of this head had one release failure in an unrelated test (startup_gfx2hlsl_v_pk_mul_mad_16_hardware): PermissionError while renaming a file in the test host's shared DXC compile cache, raised before any shader ran. The rerun above passed.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7227146851 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3030#issuecomment-7227146851). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-27T20:04:22Z. --- Windows Server 2025 re-verification of `5a5746dc4e036bcb6947d78e0785e4959d822b82` (the same change, updated with main again; D3D12 WARP, MSVC 14.44.35207): - dev: `cmake --build --preset dev --target offscreen_check`; `ctest --preset dev -R '^startup_gfx2hlsl_'`: 379/379 passed, including `startup_gfx2hlsl_s_bitreplicate_controls`, `_warp` and `_hardware` - release: `cmake --build --preset release --target offscreen_check`; `ctest --preset release -R '^startup_gfx2hlsl_'`: 379/379 passed - Windows Python checks selected for `tools/startup/gfx2hlsl.py`: all ok; `tests/startup_run_sh_check.py` skips (77) on Windows A first run of this head had one release failure in an unrelated test (`startup_gfx2hlsl_v_pk_mul_mad_16_hardware`): `PermissionError` while renaming a file in the test host's shared DXC compile cache, raised before any shader ran. The rerun above passed. AI-assisted; commands listed were executed locally.
pigalot commented 2026-11-27 20:07:25 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by pigalot on 2026-11-27T20:07:25Z.

Review: CHANGES_REQUESTED


I reviewed 5a5746dc merged into main 454fc0ed (tree 66c61640, clean merge).

What I traced.

  • The fold and the HLSL helper are the standard bit-interleave, with masks 00ff00ff, 0f0f0f0f, 33333333, 55555555 and a final x | x << 1. The high word is bitreplicate16(s >> 16).
  • The alias order is right in both cases. With s[D:D+1], sD, the high word is written first. With s[D:D+1], sD+1, the low word is written first, from the source that hasn't been overwritten yet.
  • SCC is never written. Pairs that are odd, VCC or EXEC, lane masks, and non-ud symbols are refused.

What I ran. I built offscreen_check in dev and release, then ran:
ctest --test-dir out/build/<preset> --no-tests=error -j 1 -V -R '^startup_gfx2hlsl_s_bitreplicate'
3/3 passed in each preset, with no skips. The draws ran on WARP and an RTX 5090, with debug_errors=0 debug_warnings=0.

Gap: the GPU draws never see source bits 11–14 or 27–30.

  • The HLSL helper is checked only by the draws. The controls evaluate the emitted expression with the test's own Python _bitreplicate16.
  • GPU_VECTORS keeps bits 11–14 and 27–30 clear so that XOR 0x3f800000 lands on a normal float.
  • So with this mutant, every test still passes: ' x &= 0xffffu;' changed to ' x &= 0x87ffu;' in the emitted helper only, which drops those 8 source bits.

Suggested fix (tested locally). Choose the transport XOR per result word, so that the exponent always reads 0x7f whatever the bits are:

def transport(r):
    return (r & 0xff800000) ^ 0x3f800000   # result ^ transport(result) has sign 0, exponent 0x7f

Use transport(r0) / transport(r1) from ref_bitreplicate(val) in place of DRAW_TRANSPORT_XOR, both in ps_words(val) (for s8/s4 and s9/s5) and in raw_exp. Then add vectors such as 0x78007800 and 0xffffffff.

With that change:

  • the PR's helper passes all 3 tests (WARP and hardware, FXC and DXC);
  • the mutant above fails at fxc warp bits_11_14_27_30 expected=3fc00000,… actual=00000000,….

A wrong result can still land on a NaN or a denormal, but either way it differs from the expected normal value, so the comparison still catches it.

Verdict: request changes on 5a5746dc, for the fixture gap only. The lowering itself looks right to me.

Environment: NVIDIA GeForce RTX 5090, driver 32.0.16.1088; WARP 10.0.26100.9818; Windows 11 25H2 build 26200.9974; MSVC 19.52.37029; DXC 1.9.2602.17; Python 3.12.10; debug layer on.
CI: Build and test on Windows passes on 5a5746dc. There are no review threads.

This reply was AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6092920330 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3030#pullrequestreview-6092920330). Originally posted by [pigalot](https://github.com/pigalot) on 2026-11-27T20:07:25Z. **Review:** CHANGES_REQUESTED --- I reviewed `5a5746dc` merged into `main` `454fc0ed` (tree `66c61640`, clean merge). **What I traced.** - The fold and the HLSL helper are the standard bit-interleave, with masks `00ff00ff`, `0f0f0f0f`, `33333333`, `55555555` and a final `x | x << 1`. The high word is `bitreplicate16(s >> 16)`. - The alias order is right in both cases. With `s[D:D+1], sD`, the high word is written first. With `s[D:D+1], sD+1`, the low word is written first, from the source that hasn't been overwritten yet. - SCC is never written. Pairs that are odd, VCC or EXEC, lane masks, and non-`ud` symbols are refused. **What I ran.** I built `offscreen_check` in `dev` and `release`, then ran: `ctest --test-dir out/build/<preset> --no-tests=error -j 1 -V -R '^startup_gfx2hlsl_s_bitreplicate'` 3/3 passed in each preset, with no skips. The draws ran on WARP and an RTX 5090, with `debug_errors=0 debug_warnings=0`. **Gap: the GPU draws never see source bits 11–14 or 27–30.** - The HLSL helper is checked only by the draws. The controls evaluate the emitted expression with the test's own Python `_bitreplicate16`. - `GPU_VECTORS` keeps bits 11–14 and 27–30 clear so that `XOR 0x3f800000` lands on a normal float. - So with this mutant, every test still passes: `' x &= 0xffffu;'` changed to `' x &= 0x87ffu;'` in the emitted helper only, which drops those 8 source bits. **Suggested fix (tested locally).** Choose the transport XOR per result word, so that the exponent always reads `0x7f` whatever the bits are: ```python def transport(r): return (r & 0xff800000) ^ 0x3f800000 # result ^ transport(result) has sign 0, exponent 0x7f ``` Use `transport(r0)` / `transport(r1)` from `ref_bitreplicate(val)` in place of `DRAW_TRANSPORT_XOR`, both in `ps_words(val)` (for `s8`/`s4` and `s9`/`s5`) and in `raw_exp`. Then add vectors such as `0x78007800` and `0xffffffff`. With that change: - the PR's helper passes all 3 tests (WARP and hardware, FXC and DXC); - the mutant above fails at `fxc warp bits_11_14_27_30 expected=3fc00000,… actual=00000000,…`. A wrong result can still land on a NaN or a denormal, but either way it differs from the expected normal value, so the comparison still catches it. **Verdict:** request changes on `5a5746dc`, for the fixture gap only. The lowering itself looks right to me. Environment: NVIDIA GeForce RTX 5090, driver 32.0.16.1088; WARP 10.0.26100.9818; Windows 11 25H2 build 26200.9974; MSVC 19.52.37029; DXC 1.9.2602.17; Python 3.12.10; debug layer on. CI: `Build and test on Windows` passes on `5a5746dc`. There are no review threads. This reply was AI-assisted.
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!3030
No description provided.