shader: lower binary16 add, sub, subrev and mul in direct compute #2964

Merged
SimSalabimse merged 1 commit from upstream-pr/2964 into cpp-migration 2026-11-27 00:32:23 +00:00
SimSalabimse commented 2026-11-26 23:55:00 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by SimSalabimse on 2026-11-26T23:55:00Z.


Change

Fixes #2945.

This targets cpp-migration. Head under test: 5b580cb3, based on e3670100.

The decoder already names the VOP2 forms v_add_f16, v_sub_f16, v_subrev_f16 and v_mul_f16, but the direct C++ compute path had no rule for them and refused each by name. This change lowers the VOP2 and _e64 spellings:

  • Each source is the low 16 bits. abs clears bit 15 of that half, then neg toggles it. Inline floats use the binary16 rounding of the f32 encoding.
  • The operation is an f32 add, sub or mul (v_subrev_f16 swaps the sources). An f32 significand is wide enough for a correctly rounded binary16 add, sub or mul.
  • The result is rounded with the same integer nearest-even conversion v_cvt_f16_f32 uses. The low 16 bits are written and the high half is zero, matching RDNA 2.

SDWA and DPP forms, clamp and omod, a high-half op_sel, and a binary16 rounding or denormal mode other than the implemented contract (round-to-nearest-even, f16 denormals kept on input and output, FP16_OVFL clear) are refused with a named diagnostic. s_setreg_b32 of those MODE bits is refused because the source is a register. A guest branch together with a MODE write makes the contract unprovable and is refused the same way. s_round_mode and s_denorm_mode that leave the f16 fields at that contract, and that leave the f32 fields unchanged, are accepted. The handler returns without lowering when the stage is graphics, so a graphics program keeps the generic direct-lowering refusal.

v_fmac_f16 and v_min_f16 stay on the generic diagnostic, as do the other forms listed as out of scope in #2945.

Verification

Linux, after the rebase onto e3670100:

python3 -B tests/native_f16_alu_check.py model

The model check passed. The python-linux list passed 211 checks, and the pixel-binary list passed 136 checks.

Windows, dev preset, on the test PC, at 5b580cb3:

cmake --preset dev
cmake --build --preset dev
ctest --preset dev --no-tests=error -R "^native_f16_alu_(ir|hardware)$" -V -j 1

Both of two runs: 2/2 passed, ctest exit 0, no skips.

  • native_f16_alu_ir: 82 results match the model; 20 refusals carry the named diagnostic (1.68 s, 1.53 s).
  • native_f16_alu_hardware: 164 words matched the model, high half zero (1.66 s, 0.87 s).

A ^native_ regression sweep on the same build ran once:

ctest --preset dev --no-tests=error -R "^native_" -V -j 1

ctest exit 0. 44 passed, 1 skipped, 0 failed, 142.93 s. The skip was native_comparison_pso_warp (compute requires wave32: WaveOps=1 range=4..4), which is WARP's 4-lane wave limit; it is not a skip of the new f16 tests. native_f16_alu_hardware uses WaveSize=32 and ran on the hardware adapter in this environment, so it did not take the skip code 77.

Evidence and follow-up

The new checks use a source-built compute container and a bit-pattern table (signed zeros, subnormals, infinities, NaN, 65504, and round-to-nearest-even ties and overflow to infinity). No game files or captured programs. The model check fails a mutant that swaps v_sub_f16 and v_subrev_f16, and one that leaves the upper half unzeroed. The 20 IR refusals cover SDWA and DPP for each of the four ops, clamp, omod, a high-half op_sel, a non-default f16 rounding or denormal mode, an SGPR MODE write, an f32 rounding or denormal write that keeps the previous control-instruction refusal, and the out-of-scope v_fmac_f16 and v_min_f16 forms.

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 (Cursor)

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:5518803044 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2964). Originally posted by [SimSalabimse](https://github.com/SimSalabimse) on 2026-11-26T23:55:00Z. --- ## Change Fixes #2945. This targets `cpp-migration`. Head under test: `5b580cb3`, based on `e3670100`. The decoder already names the VOP2 forms `v_add_f16`, `v_sub_f16`, `v_subrev_f16` and `v_mul_f16`, but the direct C++ compute path had no rule for them and refused each by name. This change lowers the VOP2 and `_e64` spellings: - Each source is the low 16 bits. abs clears bit 15 of that half, then neg toggles it. Inline floats use the binary16 rounding of the f32 encoding. - The operation is an f32 add, sub or mul (`v_subrev_f16` swaps the sources). An f32 significand is wide enough for a correctly rounded binary16 add, sub or mul. - The result is rounded with the same integer nearest-even conversion `v_cvt_f16_f32` uses. The low 16 bits are written and the high half is zero, matching RDNA 2. SDWA and DPP forms, clamp and omod, a high-half `op_sel`, and a binary16 rounding or denormal mode other than the implemented contract (round-to-nearest-even, f16 denormals kept on input and output, `FP16_OVFL` clear) are refused with a named diagnostic. `s_setreg_b32` of those MODE bits is refused because the source is a register. A guest branch together with a MODE write makes the contract unprovable and is refused the same way. `s_round_mode` and `s_denorm_mode` that leave the f16 fields at that contract, and that leave the f32 fields unchanged, are accepted. The handler returns without lowering when the stage is graphics, so a graphics program keeps the generic direct-lowering refusal. `v_fmac_f16` and `v_min_f16` stay on the generic diagnostic, as do the other forms listed as out of scope in #2945. ## Verification Linux, after the rebase onto `e3670100`: ``` python3 -B tests/native_f16_alu_check.py model ``` The model check passed. The `python-linux` list passed 211 checks, and the `pixel-binary` list passed 136 checks. Windows, dev preset, on the test PC, at `5b580cb3`: ``` cmake --preset dev cmake --build --preset dev ctest --preset dev --no-tests=error -R "^native_f16_alu_(ir|hardware)$" -V -j 1 ``` Both of two runs: 2/2 passed, ctest exit 0, no skips. - `native_f16_alu_ir`: 82 results match the model; 20 refusals carry the named diagnostic (1.68 s, 1.53 s). - `native_f16_alu_hardware`: 164 words matched the model, high half zero (1.66 s, 0.87 s). A `^native_` regression sweep on the same build ran once: ``` ctest --preset dev --no-tests=error -R "^native_" -V -j 1 ``` ctest exit 0. 44 passed, 1 skipped, 0 failed, 142.93 s. The skip was `native_comparison_pso_warp` (`compute requires wave32: WaveOps=1 range=4..4`), which is WARP's 4-lane wave limit; it is not a skip of the new f16 tests. `native_f16_alu_hardware` uses `WaveSize=32` and ran on the hardware adapter in this environment, so it did not take the skip code 77. ## Evidence and follow-up The new checks use a source-built compute container and a bit-pattern table (signed zeros, subnormals, infinities, NaN, 65504, and round-to-nearest-even ties and overflow to infinity). No game files or captured programs. The model check fails a mutant that swaps `v_sub_f16` and `v_subrev_f16`, and one that leaves the upper half unzeroed. The 20 IR refusals cover SDWA and DPP for each of the four ops, clamp, omod, a high-half `op_sel`, a non-default f16 rounding or denormal mode, an SGPR MODE write, an f32 rounding or denormal write that keeps the previous control-instruction refusal, and the out-of-scope `v_fmac_f16` and `v_min_f16` forms. ## 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 (Cursor)
MasterDomino commented 2026-11-27 00:32:19 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by MasterDomino on 2026-11-27T00:32:19Z.

Review: APPROVED


Review of head 5b580cb3, tested merged onto cpp-migration b260984f. It auto-merges cleanly, and CI on the head is green. Approving and merging.

Code read:

  • Operands: the low 16 bits, then abs (clear bit 15), then neg (toggle). Inline floats use their binary16 encodings.
  • Arithmetic: legacyF16ToF32 widening, one f32 fadd/fsub/fmul, then the existing packed_half_rne, with the high half zeroed. The f32-then-f16 rounding is innocuous for these ops (24 ≥ 2·11+2), and every f16 subnormal and every f16 product is normal in f32, so an f32 denormal flush cannot affect the result.
  • Refusals: SDWA and DPP are named at decode, along with clamp, omod, high-half op_sel, a non-contract f16 MODE, an SGPR s_setreg, and a MODE write together with a guest branch. Graphics stays on the generic refusal.

Run on AMD: Windows 11 26300.9974, AMD Radeon RX 9070 XT (driver 32.0.32029.2010), release.

  • ctest --preset release --no-tests=error -R '^native_': 47 tests, 46 passed and 1 skipped (77, native_comparison_pso_warp), including native_f16_alu_ir and native_f16_alu_hardware.
  • python -B tests/native_f16_alu_check.py model: 82 results match f32-then-round; a sub/subrev swap and a non-zero upper half are rejected.

Mutation here: dropping the v_subrev_f16 operand swap fails both native_f16_alu_ir and native_f16_alu_hardware, each showing the expected sign flips, for example (2, '0xbc00', '0x3c00'). With it restored, both pass.

One assumption to keep in mind, not blocking: the starting MODE is fixed at the contract (RNE, f16 denormals kept, FP16_OVFL clear). On the console the initial f16 round and denormal fields of a compute wave come from COMPUTE_PGM_RSRC1.FLOAT_MODE, which this path does not read from the request. A program launched with a different float mode would therefore be lowered under the wrong contract instead of being refused. If .sa carries RSRC1, checking it against 0xf0 at entry would close that gap.

I will close #2945 by hand.

AI-assisted; the commands above were run locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6081981478 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2964#pullrequestreview-6081981478). Originally posted by [MasterDomino](https://github.com/MasterDomino) on 2026-11-27T00:32:19Z. **Review:** APPROVED --- Review of head `5b580cb3`, tested merged onto `cpp-migration` `b260984f`. It auto-merges cleanly, and CI on the head is green. Approving and merging. **Code read:** - Operands: the low 16 bits, then abs (clear bit 15), then neg (toggle). Inline floats use their binary16 encodings. - Arithmetic: `legacyF16ToF32` widening, one f32 `fadd`/`fsub`/`fmul`, then the existing `packed_half_rne`, with the high half zeroed. The f32-then-f16 rounding is innocuous for these ops (24 ≥ 2·11+2), and every f16 subnormal and every f16 product is normal in f32, so an f32 denormal flush cannot affect the result. - Refusals: SDWA and DPP are named at decode, along with clamp, omod, high-half `op_sel`, a non-contract f16 MODE, an SGPR `s_setreg`, and a MODE write together with a guest branch. Graphics stays on the generic refusal. **Run on AMD:** Windows 11 26300.9974, AMD Radeon RX 9070 XT (driver 32.0.32029.2010), release. - `ctest --preset release --no-tests=error -R '^native_'`: 47 tests, 46 passed and 1 skipped (77, `native_comparison_pso_warp`), including `native_f16_alu_ir` and `native_f16_alu_hardware`. - `python -B tests/native_f16_alu_check.py model`: `82 results match f32-then-round; a sub/subrev swap and a non-zero upper half are rejected`. **Mutation here:** dropping the `v_subrev_f16` operand swap fails both `native_f16_alu_ir` and `native_f16_alu_hardware`, each showing the expected sign flips, for example `(2, '0xbc00', '0x3c00')`. With it restored, both pass. **One assumption to keep in mind, not blocking:** the starting MODE is fixed at the contract (RNE, f16 denormals kept, `FP16_OVFL` clear). On the console the initial f16 round and denormal fields of a compute wave come from `COMPUTE_PGM_RSRC1.FLOAT_MODE`, which this path does not read from the request. A program launched with a different float mode would therefore be lowered under the wrong contract instead of being refused. If `.sa` carries RSRC1, checking it against `0xf0` at entry would close that gap. I will close #2945 by hand. AI-assisted; the commands above were run locally.
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!2964
No description provided.