Skip to content

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

Draft
evilclanker9000 wants to merge 2 commits into
R0Y00:mainfrom
evilclanker9000:pixel-aot-pkrtz-alu
Draft

evilclanker9000 wants to merge 2 commits into
R0Y00:mainfrom
evilclanker9000:pixel-aot-pkrtz-alu

Conversation

@evilclanker9000

Copy link
Copy Markdown
Contributor

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 shader: lower quad-local DPP16 and DPP8 conversion ops in pixel_aot #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 shader: make the pixel_aot f32 denormal mode explicit and accept NaN by class #363.
  • Compressed export of a word that is not a direct e32 VGPR pack (such as a v_pk_*_f16 result) is still refused.

v_cvt_pkrtz_f16_f32 was accepted only as the input of a compressed MRT0
export. Lower the VOP2 and VOP3 forms as an ordinary 32-bit result through
an integer round-toward-zero helper, so later instructions can read the
packed word. A finite value past the binary16 range gives the largest
finite half, as LLVM folds llvm.amdgcn.cvt.pkrtz with rmTowardZero. The
e64 form takes neg and abs; clamp, output modifiers, op_sel and SDWA are
refused. The compressed export keeps its existing path.

A draw reads the packed word byte by byte for both encodings and five
constant sets, with round-to-nearest-even, swapped-half, helper-swap,
dropped-modifier and exchanged-operand controls.
The negative control renamed every 'pack16_rtz(' to 'pack16_rne(', including the helper's own definition, and then prepended the real pack16_rne helper. The shader defined pack16_rne twice and DXC rejected it (redefinition of 'pack16_rne'), so pixel_aot_predicate_rounding_* failed before any draw. Only the call sites are renamed now, and the real helper is added unless the program already defines it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant