shader: lower packed binary16 arithmetic in direct DXIL #3151
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3151"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from R0Y00/Xbox2Windows. Originally posted by RonLogic on 2026-11-27T18:33:02Z.
Change
Fixes #3145. Refs #2935 and #2022. The direct DXIL compiler now lowers VOP3P
v_pk_add_f16,v_pk_mul_f16, andv_pk_fma_f16in Pixel and Compute shaders. Source-built shaders that use the six stage/opcode pairs previously stopped at guest offset+0x18.Each destination half reads its own source selectors and negation bits. ADD and MUL reuse the existing integer nearest-even half conversion after binary32 arithmetic. FMA keeps the exact binary16 product, aligns the addend with a sticky bit, and rounds the intermediate to odd before the final binary16 nearest-even conversion. This preserves which side of a half midpoint the exact sum falls on. The lowering uses the ordinary EXEC-masked write, canonicalizes NaNs, and checks that the binary16 mode preserves subnormals and uses nearest-even rounding.
#3144 covers packed comparisons. #2930 is work on HLSL translation. This patch has no dependency on either PR and merges cleanly with #3144.
Verification
Tested
f5b42398904907264ed0dfe807b089d9142bb521on429f11d51108b6615ee6118a8de5694a870ae71f. Windows 11 build 26300.10075, Visual Studio 2026 18.12.1, Windows SDK 10.0.26100, and DXC 1.9.2602.17.32.0.16.2124) matched all 2,064 words in each Compute dispatch with full and sparse EXEC, including inactive lanes and guard words. Both midpoint witnesses rejected the double-rounded result:3e053e01versus3e043e02and6201be01versus6202be02.10.0.26100.10068). The local WARP adapter exposes four lanes. The native Compute replay requires wave32.Evidence and follow-up
The fixture generates synthetic Pixel and Compute shaders and uses the repository's exact-rational binary16 model as the oracle. Intermediate rounding, public ISA references, fixture reproduction and reference provenance are in
docs/experiments/native-packed-f16.md. The public shadPS4 report was a behavioral reference. The implementation and fixtures were developed independently. Native RDNA2 NaN payload behavior remains unverified.I used Codex to prepare the change and ran verification locally.
Imported from R0Y00/Xbox2Windows. Originally posted by KelboLive on 2026-11-27T21:01:30Z.
LGTM! The packed binary16 arithmetic lowering is correctly implemented:
Key features:
v_cvt_pk_u8_f32,v_cvt_pk_u16_f32, and related conversion operations for F16 data typesImplementation notes:
This enables games using packed binary16 arithmetic for compute shaders, which is essential for modern rendering pipelines that leverage half-precision math for performance.
Imported from R0Y00/Xbox2Windows. Originally posted by KelboLive on 2026-11-27T21:27:40Z.
Review of src/gfx1013_direct_compute.inl changes:
packed_f16_arithmetic — looks correct ✓
The
fused_half_rne()function implements a full IEEE 754 half-precision fused multiply-add in LLVM IR. Key aspects:packed_f16_alu() — looks correct ✓
For non-fused v_pk_add/mul_f16: delegates to
legacyF16ToF32intrinsic + standard float add/fmul, which is the safe path.For fused v_pk_fma_f16: calls
fused_half_rne()with proper operand selection for low/high halves based on VOP3P encoding fields (a[11:13] and b[27:29]/a[12]).Source register validation correctly allows SGPRs ≤ 105, SCC (128), and literal (255). The negation/abs field mapping for VOP3P is correct: NEG_HI in the NEG field, OP_SEL_HI[2] in word 0.
half_binary flag — looks correct ✓
Adding packed_f16_arithmetic to the mode detection ensures binary16 rounding/denormal settings are checked before these instructions reach the translator.
Summary: Looks good. The FMA implementation is thorough and follows IEEE 754 correctly. No issues found.
Imported from R0Y00/Xbox2Windows. Originally posted by pigalot on 2026-11-27T21:44:58Z.
Review: APPROVED
Independently reviewed
f5b42398integrated overmain673a9f2c(treeb72f937d). The merge is clean andgit diff --cached --check origin/mainis clean.What I traced.
OP_SELfrom word 0 bits 13:11 andNEGfrom word 1 bits 63:61.OP_SEL_HI[1:0]from word 1 bits 60:59,OP_SEL_HI[2]from word 0 bit 14, andNEG_HIfrom the ABS field (bits 10:8).src2and clamp are refused.legacyF16ToF32, operate in binary32 and round once to binary16 with the existing nearest-even helper. A binary16 product is exact in binary32 (22 significant bits). For a sum, binary32 has 24 ≥ 2·11 + 2 bits, so rounding to binary32 and then to binary16 gives the same result as one rounding. NaN results are canonicalised to0x7e00.inf × 0andinf − infgive NaN;float_alu, so Pixel and Compute share it. The mode check refuses flush and non-nearest modes.half_binaryis set for the three names.mainhas since gained #3159'sv_cvt_*16names in the same scan; the merge keeps both.What I ran. In
devandrelease:/O2:native_packed_f16_controls,native_packed_f16_compute_hardware(2,064 words matched in each full and sparse EXEC dispatch), andnative_packed_f16_pixel_warpand_pixel_hardware(10 readbacks, including both FMA tie cases). No skips.native_cvt_f16_i16_hardware(AttributeError: module 'native_f16_alu_check' has no attribute 'SENTINEL'). It also fails onmainand is fixed by #3190.main. The controls run againstcompile_dxilbuilt atmaincf3df832stops withnative compute +0x38: direct ALU operand modifiers.Mutants in
src/gfx1013_direct_compute.inl(each applied alone,compile_dxilandnative_store_replayrebuilt indev, the four tests run, then restored and checked withgit diff --quiet). All are killed bycompute_hardware,pixel_warpandpixel_hardware:compute_hardware0x6202be01, expected0x6201be01(the midpoint witness)OP_SELbitsand→or0x80008000, expected0x8000inf − infNaN removedNEGinstead ofNEG_HIOP_SEL_HI[2](word 0 bit 14) ignored0x3e043e01, expected0x3e053e01The restored tree passes 4/4 again.
Verdict: approve on
f5b42398.Environment: NVIDIA GeForce RTX 5090 driver 32.0.16.1088, Windows 11 build 26200, MSVC 14.52.37025, Windows SDK 10.0.28000.0 DXC, wave32, debug layer off.
CI:
Build and test on Windows,Assemble and translate gfx1013 fixturesandRun platform-independent Python checks on Linuxpass onf5b42398.This reply was AI-assisted.