startup: lower SDWA output modifiers on float-result conversions #2055

Merged
consolelogkyo merged 2 commits from upstream-pr/2062 into main 2026-11-25 00:16:00 +00:00
consolelogkyo commented 2026-11-24 23:47:35 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by consolelogkyo on 2026-11-24T23:47:35Z.


Summary

Lowers output modifiers (clamp, mul:2, mul:4, div:2) on SDWA forms of the float-result conversions (VEC_U_TO_F: v_cvt_f32_u32, v_cvt_f32_i32, v_cvt_f32_ubyte0..3, v_cvt_f32_f16) in tools/startup/gfx2hlsl.py (Part of #2023, item E).

Semantics & Implementation

In GFX10 VOP_SDWA (VOP_SDWA9Ae in LLVM llvm/lib/Target/AMDGPU/VOPInstructions.td and SIDefines.h; S_000504_* in Mesa src/amd/common/sid.h and SDWA_Instruction in src/amd/compiler/aco_assembler.cpp), control word w1 carries dst_sel ([10:8]), dst_unused ([12:11]), clamp ([13]), omod ([15:14]: 0 none, 1 mul:2, 2 mul:4, 3 div:2), src0_sel ([18:16]), and src0_sext ([19]).

For float-result conversions (VEC_U_TO_F), the hardware pipeline order is:

  1. Extract src0_sel (zero-extended, or sign-extended to 32 bits under sext(...)) and convert to IEEE binary32 float.
  2. Apply omod scaling (* 2.0, * 4.0, or * 0.5).
  3. Apply clamp (saturate to [0.0, 1.0]) after omod, even though disassembly prints clamp before mul:2 / mul:4 / div:2.
  4. Reinterpret the modified float as a 32-bit word (U(...) / asuint(...)) and place its low 8 / 16 / 32 bits into dst_sel according to dst_unused (UNUSED_PAD or UNUSED_PRESERVE).

Stage.f_mods() is factored out of Stage.set_vf() so VOP3 and SDWA share one implementation of omod + clamp ordering. Without output modifiers, the emitted HLSL text is unchanged. Because these SDWA output-modifier forms previously raised Unsupported (writing .fail rather than a manifest or .cso), existing shader caches hold no stale entries for them and the startup cache revision stays unchanged.

Refusal Boundaries Preserved

  • Integer and uint-result SDWA operations in VEC_U (v_cvt_f16_f32_sdwa, v_cvt_u32_f32_sdwa, v_add_nc_u32_sdwa, etc.) keep refusing clamp and omod.
  • Non-conversion VEC_F SDWA operations (v_rcp_f32_sdwa, v_add_f32_sdwa, etc.) remain refused.
  • Source neg / abs modifiers on v_cvt_f32_f16_sdwa and dst_unused:UNUSED_SEXT remain refused.

Verification

  • Pure-Python translation, CPU bit-model & negative controls (tests/startup_gfx2hlsl_sdwa_cvt_check.py):
    • Evaluates the emitted HLSL statements with an independent 32-bit HLSL uint / int / IEEE-754 binary32 evaluator across 2,376 combinations (7 VEC_U_TO_F conversions x 4 omod modes x 2 clamp states x sub-dword source/destination selections x sext x test bit patterns) against an independent RDNA 2 ISA reference, plus refusal checks and a live monkeypatch negative control.
    • Added to the python-linux CI job and AGENTS.md.
  • 16 x 16 desktop GPU pixel readback (startup_gfx2hlsl_sdwa_cvt_warp & startup_gfx2hlsl_sdwa_cvt_hardware):
    • Verified on commit base 06cdc47d, Windows 11 26H2 (build 26300.9974), D3D12 debug layer enabled (debug_errors=0 debug_warnings=0), compiled with both FXC (ps_5_1) and DXC (ps_6_0), across:
      • Microsoft Basic Render Driver (1414:008c, driver 10.0.26100.9818)
      • AMD Radeon RX 9070 XT (1002:7550, driver 32.0.32029.2010)
    • All 1,024 exported 32-bit words (256 pixels x 4 channels covering v_cvt_f32_u32_sdwa ... mul:2, v_cvt_f32_u32_sdwa ... clamp div:2, v_cvt_f32_i32_sdwa ... sext(...) mul:4, and v_cvt_f32_f16_sdwa ... clamp div:2 dst_sel:WORD_0 dst_unused:UNUSED_PRESERVE) match the ISA reference, while all 5 mutated HLSL negative controls fail against the reference and match their own models.
  • All tests/startup_gfx2hlsl_*_check.py, tests/startup_translator_check.py, and ctest --preset pr pass.
cmake --build --preset dev --target offscreen_check
ctest --preset dev --no-tests=error -R '^(startup_gfx2hlsl_sdwa_cvt(_warp|_hardware)?|startup_gfx2hlsl_vcvt_omod|startup_translator)$' -V

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6593774529 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2062). Originally posted by [consolelogkyo](https://github.com/consolelogkyo) on 2026-11-24T23:47:35Z. --- ## Summary Lowers output modifiers (`clamp`, `mul:2`, `mul:4`, `div:2`) on SDWA forms of the float-result conversions (`VEC_U_TO_F`: `v_cvt_f32_u32`, `v_cvt_f32_i32`, `v_cvt_f32_ubyte0..3`, `v_cvt_f32_f16`) in `tools/startup/gfx2hlsl.py` (`Part of #2023`, item E). ### Semantics & Implementation In GFX10 `VOP_SDWA` (`VOP_SDWA9Ae` in LLVM `llvm/lib/Target/AMDGPU/VOPInstructions.td` and `SIDefines.h`; `S_000504_*` in Mesa `src/amd/common/sid.h` and `SDWA_Instruction` in `src/amd/compiler/aco_assembler.cpp`), control word `w1` carries `dst_sel` (`[10:8]`), `dst_unused` (`[12:11]`), `clamp` (`[13]`), `omod` (`[15:14]`: `0` none, `1` `mul:2`, `2` `mul:4`, `3` `div:2`), `src0_sel` (`[18:16]`), and `src0_sext` (`[19]`). For float-result conversions (`VEC_U_TO_F`), the hardware pipeline order is: 1. Extract `src0_sel` (zero-extended, or sign-extended to 32 bits under `sext(...)`) and convert to IEEE binary32 `float`. 2. Apply `omod` scaling (`* 2.0`, `* 4.0`, or `* 0.5`). 3. Apply `clamp` (`saturate` to `[0.0, 1.0]`) **after** `omod`, even though disassembly prints `clamp` before `mul:2` / `mul:4` / `div:2`. 4. Reinterpret the modified `float` as a 32-bit word (`U(...)` / `asuint(...)`) and place its low `8` / `16` / `32` bits into `dst_sel` according to `dst_unused` (`UNUSED_PAD` or `UNUSED_PRESERVE`). `Stage.f_mods()` is factored out of `Stage.set_vf()` so VOP3 and SDWA share one implementation of `omod` + `clamp` ordering. Without output modifiers, the emitted HLSL text is unchanged. Because these SDWA output-modifier forms previously raised `Unsupported` (writing `.fail` rather than a manifest or `.cso`), existing shader caches hold no stale entries for them and the startup cache revision stays unchanged. ### Refusal Boundaries Preserved - Integer and uint-result SDWA operations in `VEC_U` (`v_cvt_f16_f32_sdwa`, `v_cvt_u32_f32_sdwa`, `v_add_nc_u32_sdwa`, etc.) keep refusing `clamp` and `omod`. - Non-conversion `VEC_F` SDWA operations (`v_rcp_f32_sdwa`, `v_add_f32_sdwa`, etc.) remain refused. - Source `neg` / `abs` modifiers on `v_cvt_f32_f16_sdwa` and `dst_unused:UNUSED_SEXT` remain refused. ## Verification - **Pure-Python translation, CPU bit-model & negative controls (`tests/startup_gfx2hlsl_sdwa_cvt_check.py`):** - Evaluates the emitted HLSL statements with an independent 32-bit HLSL `uint` / `int` / IEEE-754 binary32 evaluator across `2,376` combinations (`7` `VEC_U_TO_F` conversions x `4` `omod` modes x `2` `clamp` states x sub-dword source/destination selections x `sext` x test bit patterns) against an independent RDNA 2 ISA reference, plus refusal checks and a live monkeypatch negative control. - Added to the `python-linux` CI job and `AGENTS.md`. - **16 x 16 desktop GPU pixel readback (`startup_gfx2hlsl_sdwa_cvt_warp` & `startup_gfx2hlsl_sdwa_cvt_hardware`):** - Verified on commit base `06cdc47d`, Windows 11 26H2 (build `26300.9974`), D3D12 debug layer enabled (`debug_errors=0 debug_warnings=0`), compiled with both FXC (`ps_5_1`) and DXC (`ps_6_0`), across: - **Microsoft Basic Render Driver** (`1414:008c`, driver `10.0.26100.9818`) - **AMD Radeon RX 9070 XT** (`1002:7550`, driver `32.0.32029.2010`) - All `1,024` exported 32-bit words (`256` pixels x `4` channels covering `v_cvt_f32_u32_sdwa ... mul:2`, `v_cvt_f32_u32_sdwa ... clamp div:2`, `v_cvt_f32_i32_sdwa ... sext(...) mul:4`, and `v_cvt_f32_f16_sdwa ... clamp div:2 dst_sel:WORD_0 dst_unused:UNUSED_PRESERVE`) match the ISA reference, while all `5` mutated HLSL negative controls fail against the reference and match their own models. - All `tests/startup_gfx2hlsl_*_check.py`, `tests/startup_translator_check.py`, and `ctest --preset pr` pass. ```powershell cmake --build --preset dev --target offscreen_check ctest --preset dev --no-tests=error -R '^(startup_gfx2hlsl_sdwa_cvt(_warp|_hardware)?|startup_gfx2hlsl_vcvt_omod|startup_translator)$' -V ``` AI-assisted.
upstream-sync closed this pull request 2026-11-25 01:01:24 +00:00
ChazY11 commented 2026-11-25 01:02:00 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by ChazY11 on 2026-11-25T00:04:32Z.


Ran the draw check on both adapters here; three production mutations, caught on each backend

dxc and a built offscreen_check exist on this machine, so the draw half of this PR is executable:

out/pe-env/Scripts/python.exe -B tests/startup_gfx2hlsl_sdwa_cvt_check.py draw \
    "<Windows Kits>/10/bin/10.0.26100.0/x64/dxc.exe" out/build/dev/offscreen_check.exe warp
dxc (warp) negative control (dropped WORD_0 dst_sel) fails against reference: ok
dxc (warp) negative control (dropped WORD_0 dst_sel) matches its own model: ok
PASS                                                                   exit 0

and hardware:

adapter=NVIDIA GeForce RTX 5080 Laptop GPU driver=32.0.15.8142   PASS   exit 0

Windows 11 build 26300.9974, DXC 10.0.26100.0, debug layer enabled.

Your negative controls are the right shape — and they are not the same as mutating the production code

The check mutates its own model and confirms the reference catches each variant (early clamp before div:2, dropped mul:4, dropped div:2 on f16, dropped WORD_0 dst_sel). That proves the comparison is sensitive. It does not prove the translator is connected to it, so I mutated
tools/startup/gfx2hlsl.py and let the draw run decide:

mutation warp hardware
float_result = base in VEC_U_TO_F → True (integer forms get the float output modifiers) caught caught
dst_sel dropped from the accepted SDWA keys caught caught
an unknown SDWA modifier silently ignored instead of refused caught caught

All three fail through the pixel comparison, not a compile error, on both backends.

The third is the one I would keep: silently ignoring an unknown modifier is the failure mode where the shader still runs and still produces a colour,
so nothing but a value comparison can see it.

What I could not check

dst_unused UNUSED_SEXT being refused is stated as "no lowering"; a draw fixture cannot reach a refusal, so it stays a comment. If the refusal is
meant to be pinned, it belongs in the form check beside the other Unsupported rows.

Notes

  • AI-assisted review; both adapters were executed locally.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7148110222 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2062#issuecomment-7148110222). Originally posted by [ChazY11](https://github.com/ChazY11) on 2026-11-25T00:04:32Z. --- ## Ran the draw check on both adapters here; three production mutations, caught on each backend `dxc` and a built `offscreen_check` exist on this machine, so the draw half of this PR is executable: ```powershell out/pe-env/Scripts/python.exe -B tests/startup_gfx2hlsl_sdwa_cvt_check.py draw \ "<Windows Kits>/10/bin/10.0.26100.0/x64/dxc.exe" out/build/dev/offscreen_check.exe warp ``` ```text dxc (warp) negative control (dropped WORD_0 dst_sel) fails against reference: ok dxc (warp) negative control (dropped WORD_0 dst_sel) matches its own model: ok PASS exit 0 ``` and `hardware`: ```text adapter=NVIDIA GeForce RTX 5080 Laptop GPU driver=32.0.15.8142 PASS exit 0 ``` Windows 11 build 26300.9974, DXC 10.0.26100.0, debug layer enabled. ## Your negative controls are the right shape — and they are not the same as mutating the production code The check mutates its **own model** and confirms the reference catches each variant (`early clamp before div:2`, `dropped mul:4`, `dropped div:2 on f16`, `dropped WORD_0 dst_sel`). That proves the comparison is sensitive. It does not prove the *translator* is connected to it, so I mutated `tools/startup/gfx2hlsl.py` and let the draw run decide: | mutation | warp | hardware | |---|---|---| | `float_result = base in VEC_U_TO_F` → `True` (integer forms get the float output modifiers) | **caught** | **caught** | | `dst_sel` dropped from the accepted SDWA keys | **caught** | **caught** | | an unknown SDWA modifier silently ignored instead of refused | **caught** | **caught** | All three fail through the pixel comparison, not a compile error, on both backends. The third is the one I would keep: silently ignoring an unknown modifier is the failure mode where the shader still runs and still produces *a* colour, so nothing but a value comparison can see it. ## What I could not check `dst_unused UNUSED_SEXT` being refused is stated as "no lowering"; a draw fixture cannot reach a refusal, so it stays a comment. If the refusal is meant to be pinned, it belongs in the form check beside the other `Unsupported` rows. ## Notes - AI-assisted review; both adapters were executed locally.
R0Y00 commented 2026-11-25 01:02:03 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-25T00:12:30Z.


I verified the source-built 16 by 16 draw on WARP and RTX 4060 Laptop, with both FXC and DXC and no debug errors or warnings. Each of the five mutations differed from the reference and matched its mutated model. I pushed a documentation fix to make clear that existing integer and uint-result SDWA forms continue to work without output modifiers.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7148264072 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2062#issuecomment-7148264072). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-25T00:12:30Z. --- I verified the source-built 16 by 16 draw on WARP and RTX 4060 Laptop, with both FXC and DXC and no debug errors or warnings. Each of the five mutations differed from the reference and matched its mutated model. I pushed a documentation fix to make clear that existing integer and uint-result SDWA forms continue to work without output modifiers.
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!2055
No description provided.