shader: lower gfx1013 v_cvt_pkrtz_f16_f32 as an ALU result in pixel_aot #522

Open
Vonsek wants to merge 2 commits from upstream-pr/528 into main
Vonsek commented 2026-11-22 09:30:22 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Vonsek on 2026-11-22T09:30:22Z.


Change

v_cvt_pkrtz_f16_f32 was only accepted as the direct input of a compressed MRT0 export: the translator kept the two source registers and rounded them in export16. Any other read of the packed word was refused. This PR lowers the VOP2 (_e32) and VOP3 (_e64) forms as an ordinary 32-bit ALU result:

  • pack16_rtz converts each half in integer arithmetic, rounding toward zero whatever the MODE rounding field says. DXC f32tof16 rounds to nearest even, so it is not used here, and desktop denormal handling cannot change the result.
  • A finite value past the binary16 range gives the largest finite half (±65504), as LLVM folds llvm.amdgcn.cvt.pkrtz with rmTowardZero. Infinity stays infinity, a binary16 subnormal result is kept, and a binary32 subnormal input gives a signed zero. A NaN keeps its sign and top payload bits and gets the quiet bit. NaN is not drawn.
  • The e32 S0 may be a VGPR, an SGPR, an inline constant or a literal, and S1 must be a VGPR. The e64 form accepts neg and abs. clamp, output modifiers (mul:2), op_sel and the SDWA form are refused. DPP forms are left to #421.
  • The compressed export path does not change: an e32 pack of two VGPRs still exports through export16, so existing fixtures produce the same export. Any other word, including a moved copy of a pack, is refused by the export as before.
  • tools/scan_unsupported_shaders.py lists the widened e32 operands and the e64 form as supported.

v_cvt_f32_f16 is already on main and v_cvt_f16_f32 is in open #324, so this PR leaves them alone. #421 adds a DPP-only pack16_rtz helper that returns infinity for finite overflow; if both land, keep one helper. This PR's overflow behavior follows LLVM's constant folding.

A second commit fixes the round-to-nearest-even negative control: it renamed the pack16_rtz definition along with its call sites and then prepended the real pack16_rne, so DXC rejected the shader with redefinition of 'pack16_rne' and both pixel_aot_predicate_rounding_* tests failed on Windows before any draw. Only the call sites are renamed now.

Verification

  • New fixtures pkrtz_body("e32") and pkrtz_body("e64") are assembled with llvm-mc for gfx1013, round-trip through the binary check and translate to the same HLSL as the source text.
  • check_pkrtz_controls checks the emitted operands (including (-params[0].w) and abs(params[1].x) for e64) and rejects clamp, mul:2, op_sel, SDWA, v_cvt_f16_u16_e32, a missing operand, an e32 SGPR in S1, pending-sample reads/writes, and compressed exports of an SGPR-sourced or e64 pack. It asserts the CPU reference separates RTZ from RNE and from the swapped-half word in every set.
  • The existing control that rejected v_mov_b32 v20, v16 after a pack now checks that the move is a bit copy and that exporting the copy is still refused.
  • check_pkrtz_draws (in pixel_aot_predicate_rounding_*) reads the packed word as four bytes through v_cvt_f32_ubyte0..3. It draws e32 (S0, S1) and e64 (-S0, |S1|) with five constant sets covering ties and near-ties, 2^100 and -65520 past the range, subnormal halves, -1.5·2^-25 → -0, and -infinity. Negative controls must report pixel_mismatch: an RNE or swapped-half reference, the shader with pack16_rtz replaced by pack16_rne, and e64 programs that drop neg, drop abs or exchange operands.
  • tests/form_coverage_check.py asserts VOP2 v_cvt_pkrtz_f16_f32 and VOP3 v_cvt_pkrtz_f16_f32_e64 are covered in both gfx1013 and rdna2, and the rejected SDWA form and v_cvt_f16_u16 are not.
  • The HLSL helper, transliterated to C, matches an independent floor-based reference for all 2^32 binary32 inputs.
  • Linux, LLVM 18: pixel_aot_check (binary and controls) and form_coverage_check pass, as do the sampled_coverage, depth_mask, compute_store, mesh_indexed, mesh_entry, ffx_dispatch_aot, rdna2_tools and pso_readiness checks. With tools/pixel_aot.py reverted to main, pixel_aot_check and form_coverage_check fail on Unsupported instruction 21: v_cvt_pkrtz_f16_f32_e32 v20, s44, v19.
  • D3D12 draws, measured on Windows: every e32 and e64 draw matches the RTZ reference for all five constant sets, and every negative control (RNE and swapped-half references, the pack16_rne shader, and the e64 programs with neg or abs dropped or operands exchanged) reports pixel_mismatch, on the RTX 5080 and on WARP.
  • Windows: ctest --preset dev --no-tests=error -R '^pixel_aot_(predicate_rounding_)?(hardware|warp)$' -V at 2e286dd on Windows 11 build 26200.9974, VS Build Tools 2022 17.14 (MSVC 14.44.35207), Windows SDK 10.0.26100, DXC 1.9.2602.17: 4/4 passed. On an NVIDIA GeForce RTX 5080 (driver 32.0.16.1692) pixel_aot_predicate_rounding_hardware took 36.6 s; on WARP pixel_aot_predicate_rounding_warp took 8.4 s, both under the 60 s timeout. At 775eed8 (before the control fix) both pixel_aot_predicate_rounding_* tests failed with redefinition of 'pack16_rne'. The D3D12 debug layer was not available. The full ctest --preset dev --no-tests=error passed 236/236, with bvh_wave64_hardware, bvh_wave64_warp and wave_shuffle_warp skipped.

Evidence and follow-up

  • Scarlett's behavior for finite overflow and NaN payloads in v_cvt_pkrtz_f16_f32 has not been measured; overflow follows LLVM's constant folding.
  • Each packed word stays in a binary32 register; the draw sets keep a normal high half in every word the GPU computes. Words whose high half is ±0, a binary16 subnormal or ±infinity depend on the desktop f32 denormal mode tracked in #363.
  • Compressed export of a word that is not a direct e32 VGPR pack (such as a v_pk_*_f16 result) is still refused.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:5437686329 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/528). Originally posted by [Vonsek](https://github.com/Vonsek) on 2026-11-22T09:30:22Z. --- ## Change `v_cvt_pkrtz_f16_f32` was only accepted as the direct input of a compressed MRT0 export: the translator kept the two source registers and rounded them in `export16`. Any other read of the packed word was refused. This PR lowers the VOP2 (`_e32`) and VOP3 (`_e64`) forms as an ordinary 32-bit ALU result: - `pack16_rtz` converts each half in integer arithmetic, rounding toward zero whatever the MODE rounding field says. DXC `f32tof16` rounds to nearest even, so it is not used here, and desktop denormal handling cannot change the result. - A finite value past the binary16 range gives the largest finite half (±65504), as LLVM folds `llvm.amdgcn.cvt.pkrtz` with `rmTowardZero`. Infinity stays infinity, a binary16 subnormal result is kept, and a binary32 subnormal input gives a signed zero. A NaN keeps its sign and top payload bits and gets the quiet bit. NaN is not drawn. - The e32 S0 may be a VGPR, an SGPR, an inline constant or a literal, and S1 must be a VGPR. The e64 form accepts `neg` and `abs`. `clamp`, output modifiers (`mul:2`), `op_sel` and the SDWA form are refused. DPP forms are left to #421. - The compressed export path does not change: an e32 pack of two VGPRs still exports through `export16`, so existing fixtures produce the same export. Any other word, including a moved copy of a pack, is refused by the export as before. - `tools/scan_unsupported_shaders.py` lists the widened e32 operands and the e64 form as supported. `v_cvt_f32_f16` is already on main and `v_cvt_f16_f32` is in open #324, so this PR leaves them alone. #421 adds a DPP-only `pack16_rtz` helper that returns infinity for finite overflow; if both land, keep one helper. This PR's overflow behavior follows LLVM's constant folding. A second commit fixes the round-to-nearest-even negative control: it renamed the `pack16_rtz` definition along with its call sites and then prepended the real `pack16_rne`, so DXC rejected the shader with `redefinition of 'pack16_rne'` and both `pixel_aot_predicate_rounding_*` tests failed on Windows before any draw. Only the call sites are renamed now. ## Verification - New fixtures `pkrtz_body("e32")` and `pkrtz_body("e64")` are assembled with llvm-mc for gfx1013, round-trip through the binary check and translate to the same HLSL as the source text. - `check_pkrtz_controls` checks the emitted operands (including `(-params[0].w)` and `abs(params[1].x)` for e64) and rejects `clamp`, `mul:2`, `op_sel`, SDWA, `v_cvt_f16_u16_e32`, a missing operand, an e32 SGPR in S1, pending-sample reads/writes, and compressed exports of an SGPR-sourced or e64 pack. It asserts the CPU reference separates RTZ from RNE and from the swapped-half word in every set. - The existing control that rejected `v_mov_b32 v20, v16` after a pack now checks that the move is a bit copy and that exporting the copy is still refused. - `check_pkrtz_draws` (in `pixel_aot_predicate_rounding_*`) reads the packed word as four bytes through `v_cvt_f32_ubyte0..3`. It draws e32 (S0, S1) and e64 (-S0, |S1|) with five constant sets covering ties and near-ties, 2^100 and -65520 past the range, subnormal halves, -1.5·2^-25 → -0, and -infinity. Negative controls must report `pixel_mismatch`: an RNE or swapped-half reference, the shader with `pack16_rtz` replaced by `pack16_rne`, and e64 programs that drop `neg`, drop `abs` or exchange operands. - `tests/form_coverage_check.py` asserts `VOP2 v_cvt_pkrtz_f16_f32` and `VOP3 v_cvt_pkrtz_f16_f32_e64` are covered in both gfx1013 and rdna2, and the rejected SDWA form and `v_cvt_f16_u16` are not. - The HLSL helper, transliterated to C, matches an independent floor-based reference for all 2^32 binary32 inputs. - Linux, LLVM 18: pixel_aot_check (binary and controls) and form_coverage_check pass, as do the sampled_coverage, depth_mask, compute_store, mesh_indexed, mesh_entry, ffx_dispatch_aot, rdna2_tools and pso_readiness checks. With `tools/pixel_aot.py` reverted to main, pixel_aot_check and form_coverage_check fail on `Unsupported instruction 21: v_cvt_pkrtz_f16_f32_e32 v20, s44, v19`. - D3D12 draws, measured on Windows: every e32 and e64 draw matches the RTZ reference for all five constant sets, and every negative control (RNE and swapped-half references, the `pack16_rne` shader, and the e64 programs with `neg` or `abs` dropped or operands exchanged) reports `pixel_mismatch`, on the RTX 5080 and on WARP. - Windows: `ctest --preset dev --no-tests=error -R '^pixel_aot_(predicate_rounding_)?(hardware|warp)$' -V` at 2e286dd on Windows 11 build 26200.9974, VS Build Tools 2022 17.14 (MSVC 14.44.35207), Windows SDK 10.0.26100, DXC 1.9.2602.17: 4/4 passed. On an NVIDIA GeForce RTX 5080 (driver 32.0.16.1692) `pixel_aot_predicate_rounding_hardware` took 36.6 s; on WARP `pixel_aot_predicate_rounding_warp` took 8.4 s, both under the 60 s timeout. At 775eed8 (before the control fix) both `pixel_aot_predicate_rounding_*` tests failed with `redefinition of 'pack16_rne'`. The D3D12 debug layer was not available. The full `ctest --preset dev --no-tests=error` passed 236/236, with `bvh_wave64_hardware`, `bvh_wave64_warp` and `wave_shuffle_warp` skipped. ## Evidence and follow-up - Scarlett's behavior for finite overflow and NaN payloads in `v_cvt_pkrtz_f16_f32` has not been measured; overflow follows LLVM's constant folding. - Each packed word stays in a binary32 register; the draw sets keep a normal high half in every word the GPU computes. Words whose high half is ±0, a binary16 subnormal or ±infinity depend on the desktop f32 denormal mode tracked in #363. - Compressed export of a word that is not a direct e32 VGPR pack (such as a `v_pk_*_f16` result) is still refused.
tedin7 commented 2026-11-27 09:37:38 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-27T09:37:38Z.


This PR now conflicts with main. Conflicting files:

  • tests/form_coverage_check.py
  • tests/pixel_aot_check.py
  • tools/pixel_aot.py
  • tools/scan_unsupported_shaders.py

Could you merge main into your branch (a merge commit, no force push needed) and resolve these? Thank you.

AI-assisted; commands listed were executed locally. (git merge-tree origin/main <head>)

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7214903231 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/528#issuecomment-7214903231). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-27T09:37:38Z. --- This PR now conflicts with `main`. Conflicting files: - `tests/form_coverage_check.py` - `tests/pixel_aot_check.py` - `tools/pixel_aot.py` - `tools/scan_unsupported_shaders.py` Could you merge `main` into your branch (a merge commit, no force push needed) and resolve these? Thank you. AI-assisted; commands listed were executed locally. (`git merge-tree origin/main <head>`)
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/528:upstream-pr/528'
git switch 'upstream-pr/528'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/528'
git switch 'upstream-pr/528'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/528'
git switch 'upstream-pr/528'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/528'
git switch 'main'
git merge --squash 'upstream-pr/528'
git switch 'main'
git merge --ff-only 'upstream-pr/528'
git switch 'main'
git merge 'upstream-pr/528'
git push origin 'main'
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!522
No description provided.