shader: lower the SDWA forms of binary16 add, sub, subrev and mul in direct compute #2973

Merged
Leyukaka merged 1 commit from upstream-pr/2973 into cpp-migration 2026-11-27 01:01:09 +00:00
Leyukaka commented 2026-11-27 00:47:44 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Leyukaka on 2026-11-27T00:47:44Z.


Fixes #2963 (it needs closing by hand, since the base is not the default branch). Part of #2935. Target branch cpp-migration. Builds on #2964.

Change (src/gfx1013_compute_native.cpp, src/gfx1013_direct_compute.inl): the SDWA forms of v_add_f16, v_sub_f16, v_subrev_f16 and v_mul_f16, which #2964 refused, are lowered in direct compute, following tools/startup/gfx2hlsl.py sdwa_half().

  • Sources: two VGPRs. src*_sel WORD_0 or WORD_1 picks the half, and DWORD reads the low half. abs/neg are allowed.
  • Arithmetic: each half goes through the same binary32 path as #2964's plain forms and is rounded once to binary16 with packed_half_rne. For add, sub and mul of binary16 inputs, binary32 has enough precision (24 >= 2*11 + 2) that this matches a direct binary16 rounding. This assumes the default FP mode (round to nearest even, binary16 denormals kept), as #2964 and sdwa_half() do.
  • Destination: dst_sel WORD_0 or WORD_1, placed with place_subword. UNUSED_PAD zeroes the other half, and UNUSED_PRESERVE keeps the old register's other half.
  • Refused:
    • byte selections, sext;
    • SGPR and constant sources;
    • dst_sel:DWORD, UNUSED_SEXT;
    • clamp and omod.
  • The v_min_f16/v_max_f16 SDWA forms stay refused, and so do the DPP forms of these ops.
  • tests/native_f16_alu_check.py drops the one control that expected these SDWA forms to be refused. docs/experiments.md also gets an index entry for #2964's plain binary16 experiment next to this one, and f16-alu-direct.md links to the new note.

Tests. The setup was Windows 11 with an NVIDIA GeForce RTX 5080 and DXC from the Windows SDK 10.0.26100, on cpp-migration 2b678fd3 plus this commit.

  • native_sdwa_f16_ir passed: 17 results evaluated from the DXIL text with #2964's interpreter, and 10 refusals.
  • native_sdwa_f16_hardware passed: 34 results in two workgroups, with 2,030 guard words intact.
  • The cases are each op under the measured shapes:
    • dst_sel:WORD_0 UNUSED_PRESERVE src0_sel:WORD_1 src1_sel:DWORD;
    • dst_sel:WORD_0 UNUSED_PAD src0_sel:DWORD src1_sel:WORD_0;
    • a dst_sel:WORD_1 case with abs and neg on one source;
    • neg on one source and abs on the other: -(1.0) + |-2.0| = 1.0. Without either modifier, the sum would differ.
  • Values: a rounding tie, a subnormal product that rounds, overflow to infinity, and a NaN input, compared as NaN.
  • native_f16_alu_ir, native_f16_alu_hardware and native_bytewin_store_hardware still pass.

Mutation checks on the lowering, run by hand; each fails both the IR and the hardware check:

  • reading the other half of a WORD_0/WORD_1 source;
  • writing the result without its SDWA placement;
  • dropping abs;
  • dropping neg.

Measured on a local capture (counts and instruction shape only) with compile_dxil --jobs 8 gfx1013-direct-cs@36 --requests.

  • Of 2,689 compute requests, 388 compiled on 2b678fd3 and 389 with this commit; none that compiled before stops compiling.
  • No request stops first at an SDWA form of these four ops any more; on 8946fb16 (before #2967), 14 did.
  • Those requests now stop at later refusals, mostly "unbound numeric SGPR" and "direct ALU operand modifiers". So this change removes a blocker in front of them but completes only one by itself.

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:5519252259 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2973). Originally posted by [Leyukaka](https://github.com/Leyukaka) on 2026-11-27T00:47:44Z. --- Fixes #2963 (it needs closing by hand, since the base is not the default branch). Part of #2935. Target branch `cpp-migration`. Builds on #2964. **Change** (`src/gfx1013_compute_native.cpp`, `src/gfx1013_direct_compute.inl`): the SDWA forms of `v_add_f16`, `v_sub_f16`, `v_subrev_f16` and `v_mul_f16`, which #2964 refused, are lowered in direct compute, following `tools/startup/gfx2hlsl.py` `sdwa_half()`. - Sources: two VGPRs. `src*_sel` `WORD_0` or `WORD_1` picks the half, and `DWORD` reads the low half. abs/neg are allowed. - Arithmetic: each half goes through the same binary32 path as #2964's plain forms and is rounded once to binary16 with `packed_half_rne`. For add, sub and mul of binary16 inputs, binary32 has enough precision (24 >= 2*11 + 2) that this matches a direct binary16 rounding. This assumes the default FP mode (round to nearest even, binary16 denormals kept), as #2964 and `sdwa_half()` do. - Destination: `dst_sel` `WORD_0` or `WORD_1`, placed with `place_subword`. `UNUSED_PAD` zeroes the other half, and `UNUSED_PRESERVE` keeps the old register's other half. - Refused: - byte selections, `sext`; - SGPR and constant sources; - `dst_sel:DWORD`, `UNUSED_SEXT`; - `clamp` and `omod`. - The `v_min_f16`/`v_max_f16` SDWA forms stay refused, and so do the DPP forms of these ops. - `tests/native_f16_alu_check.py` drops the one control that expected these SDWA forms to be refused. `docs/experiments.md` also gets an index entry for #2964's plain binary16 experiment next to this one, and `f16-alu-direct.md` links to the new note. **Tests.** The setup was Windows 11 with an NVIDIA GeForce RTX 5080 and DXC from the Windows SDK 10.0.26100, on `cpp-migration` `2b678fd3` plus this commit. - `native_sdwa_f16_ir` passed: 17 results evaluated from the DXIL text with #2964's interpreter, and 10 refusals. - `native_sdwa_f16_hardware` passed: 34 results in two workgroups, with 2,030 guard words intact. - The cases are each op under the measured shapes: - `dst_sel:WORD_0 UNUSED_PRESERVE src0_sel:WORD_1 src1_sel:DWORD`; - `dst_sel:WORD_0 UNUSED_PAD src0_sel:DWORD src1_sel:WORD_0`; - a `dst_sel:WORD_1` case with abs and neg on one source; - neg on one source and abs on the other: `-(1.0) + |-2.0| = 1.0`. Without either modifier, the sum would differ. - Values: a rounding tie, a subnormal product that rounds, overflow to infinity, and a NaN input, compared as NaN. - `native_f16_alu_ir`, `native_f16_alu_hardware` and `native_bytewin_store_hardware` still pass. **Mutation checks** on the lowering, run by hand; each fails both the IR and the hardware check: - reading the other half of a `WORD_0`/`WORD_1` source; - writing the result without its SDWA placement; - dropping abs; - dropping neg. **Measured on a local capture** (counts and instruction shape only) with `compile_dxil --jobs 8 gfx1013-direct-cs@36 --requests`. - Of 2,689 compute requests, 388 compiled on `2b678fd3` and 389 with this commit; none that compiled before stops compiling. - No request stops first at an SDWA form of these four ops any more; on `8946fb16` (before #2967), 14 did. - Those requests now stop at later refusals, mostly "unbound numeric SGPR" and "direct ALU operand modifiers". So this change removes a blocker in front of them but completes only one by itself. AI-assisted.
MasterDomino commented 2026-11-27 01:01:07 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by MasterDomino on 2026-11-27T01:01:07Z.

Review: APPROVED


Review of head 2e312dd6, tested merged onto cpp-migration 2b678fd3. It merges cleanly, and CI on the head is green. Approving and merging.

Code read:

  • Sources: WORD_0/WORD_1 pick the half and DWORD reads the low half (sel == 6 ? 4 : sel). Byte selects, SGPR sources and sign extension are refused.
  • Destination: WORD_0/WORD_1 with UNUSED_PAD or UNUSED_PRESERVE, placed through place_subword, while the plain form keeps zeroing the high half.
  • Rounding: the same single binary32 path and packed_half_rne rounding as #2964.
  • Test change: the one control removed from native_f16_alu_check.py is the old "SDWA form refused" expectation that this PR replaces with implementation plus its own refusals, so it is not a weakened test.

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_' gives 53 tests, 52 passed and 1 skipped (77, native_comparison_pso_warp), including native_sdwa_f16_{ir,hardware} and native_f16_alu_{ir,hardware}.

Mutation here: always reading the low half (ignoring WORD_1) fails both native_sdwa_f16_ir ((0, 'add', '0xa55a4600', '0xa55a4200')) and native_sdwa_f16_hardware ((8, 'add', …)). With it restored, both pass.

As with #2964, the default FP mode (RNE, binary16 denormals kept) is assumed rather than read from RSRC1. I will close #2963 by hand.

AI-assisted; the commands above were run locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6082132170 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2973#pullrequestreview-6082132170). Originally posted by [MasterDomino](https://github.com/MasterDomino) on 2026-11-27T01:01:07Z. **Review:** APPROVED --- Review of head `2e312dd6`, tested merged onto `cpp-migration` `2b678fd3`. It merges cleanly, and CI on the head is green. Approving and merging. **Code read:** - **Sources:** `WORD_0`/`WORD_1` pick the half and `DWORD` reads the low half (`sel == 6 ? 4 : sel`). Byte selects, SGPR sources and sign extension are refused. - **Destination:** `WORD_0`/`WORD_1` with `UNUSED_PAD` or `UNUSED_PRESERVE`, placed through `place_subword`, while the plain form keeps zeroing the high half. - **Rounding:** the same single binary32 path and `packed_half_rne` rounding as #2964. - **Test change:** the one control removed from `native_f16_alu_check.py` is the old "SDWA form refused" expectation that this PR replaces with implementation plus its own refusals, so it is not a weakened test. **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_'` gives 53 tests, 52 passed and 1 skipped (77, `native_comparison_pso_warp`), including `native_sdwa_f16_{ir,hardware}` and `native_f16_alu_{ir,hardware}`. **Mutation here:** always reading the low half (ignoring `WORD_1`) fails both `native_sdwa_f16_ir` (`(0, 'add', '0xa55a4600', '0xa55a4200')`) and `native_sdwa_f16_hardware` (`(8, 'add', …)`). With it restored, both pass. As with #2964, the default FP mode (RNE, binary16 denormals kept) is assumed rather than read from RSRC1. I will close #2963 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!2973
No description provided.