shader: lower v_div_fmas_f32, reject unverified v_fma_f16 and v_div_scale_f32 #698
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/704"
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 Holastor on 2026-11-22T21:26:16Z.
### Summary
This PR adds translation lowering and scanner patterns for
v_div_fmas_f32:v_div_fmas_f32: Vector divide fused multiply-add with scale, implicitly reading the condition mask (vcc/vcc_lo) and applying the2^32(4294967296.0) scale when non-zero, with support forclamp.div_fmas32_core/div_fmas32/div_fmas32_clamp) to guarantee strict single-rounding across all backends, correctly resolving float32 midpoint ties (avoiding double-rounding errors on ties like1.5 + 3 * 2^-24 - 2^-100) and preventing premature float32 underflow before the2^32scale is applied.v_fma_f16andv_div_scale_f32until source-built native RDNA readback settles binary16 rounding, destination high-half preservation, and prescaling quotient bits.Tests
check_valu_div_fmas_draw) rendering withinteger_export=Trueon runtime constant-buffer inputs across five test cases:0x3f800001, 0x3fc00000, 0x8d800000): exact sum1.5 + 3 * 2^-24 - 2^-100is strictly below midpoint, rounding down to0x3fc00001(0x4fc00001scaled by2^32), whereas uncorrected double rounding rounds to even (0x3fc00002/0x4fc00002).0x3f800001, 0x3f7ffffe, 0xbf800000): evaluates exact product + addend-2^-46(0xa8800000unscaled,0xb8800000scaled by2^32), where unfused float32madrounds1 - 2^-46to 1.0 producing zero.0x00800000, 0x33000000, 0):2^-126 * 2^-25 = 2^-151, which avoids early float32 underflow to zero and scales to2^-119(0x04000000).0x00800000, 0.5, 0):2^-126 * 0.5 = 2^-127(0x00400000) before scaling by2^32to2^-95(0x10000000).2.0, 0.5, 1.0): unscaled 2.0 (0x40000000), scaled2.0 * 2^32(0x50000000), clamped 1.0 (0x3f800000).--check-pixel-case:2^32scale, detected by--check-pixel-case.mad/ early rounding control: substitutes unfused float32mad, detected by--check-pixel-case.--check-pixel-case.mad32, detected by--check-pixel-case.tests/pixel_aot_check.pyandtests/rdna2_tools_check.py.docs/experiments.md.Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-23T12:09:57Z.
Review: DISMISSED
The DIV_FMAS-only scope and the scale/flush controls are useful. I ran two additional runtime-operand cases at 88c75f3 on Windows 26200.9974 with the debug layer (WARP 10.0.26100.9818 and RTX 4060 Laptop 32.0.16.1656).
wpu9gFfl7d9q0QZ~Og/contentPlease add the cancellation and pre-scale-underflow cases with exact integer readback. A general lowering needs fusion in both VCC paths and must avoid early rounding before scaling. A narrower first slice can refuse operands for which the supported result cannot be proven. Preserve the existing scale/FTZ negatives and the refusal of FMA_F16 and DIV_SCALE.
Imported from R0Y00/Xbox2Windows. Originally posted by Joacoorandom on 2026-11-22T21:37:09Z.
Review of
fc10172ac862e99d315f7617278ed4aca32083af: the newv_div_scale_f32lowering loses numerator-dependent scaling and VCC.A source-only reproducer, from the PR checkout:
Here SRC0=SRC1=1 and SRC2=2^100. The emitted predicate tests only SRC1 against the two thresholds, evaluates false, and produces v12=1, v13=0. AMD's RDNA 2 ISA, section 12.12, opcode 365, takes the exponent-gap >=96 branch: v12=2^64, VCC=true, so v13 should be 1. SRC2 is parsed but never used in the emitted arithmetic. The scale branch also uses 2^32, whereas this pre-scale operation specifies ±64 exponent adjustments.
AMD RDNA 2 ISA
Please implement the numerator/denominator cases and flag separately, or keep this instruction rejected until verified. Add an observable uncompressed draw with an independent expected result and a VCC-derived channel. This report compares emitted HLSL with the published ISA; I have not run the case on native AMD hardware.
Implementation: commit 35fb92f, submitted directly against this PR's
shader/valu-fma-divbranch as Holastor/Xbox2Windows#2. It takes the conservative option described above: explicitly reject unverified prescaling, keep the opcode recognized but unsupported, and add detecting rejection controls. It does not claim a complete pre-scale implementation. All four CI jobs in the linked follow-up passed: LLVM 15 assembly/translation, Linux Python checks, Windows development/WARP, and Windows release/WARP at /O2. The follow-up is ready for review.Imported from R0Y00/Xbox2Windows. Originally posted by Holastor on 2026-11-22T22:24:57Z.
Thanks @Joacoorandom for the detailed review, ISA reference, and reproducer! Merged https://github.com/Holastor/Xbox2Windows/pull/2 into the branch. Taking the conservative approach of keeping
v_div_scale_f32explicitly rejected with comprehensive rejection controls until full numerator/denominator semantics and VCC flags are verified is definitely the right call.Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-22T22:58:01Z.
The revised head now refuses
v_div_scale_f32as requested. The remainingv_fma_f16branch always emitspack16_rne, even when the tracked MODE selects a different half rounding mode. Please reject unsupported modes for this profile or use the established mode-specific packer, and add an independent case where RNE and RTZ differ. A fused-half halfway case should also distinguish rounding the fused result once from rounding to float32 first. For thev_div_fmas_f32slice, please keep the VCC true/false controls and include a scaling boundary that would catch intermediate underflow before multiplying the fused result by the scale.Imported from R0Y00/Xbox2Windows. Originally posted by Holastor on 2026-11-22T23:27:31Z.
Thanks @R0Y00 for the detailed review and guidance!
We have updated the PR to address both points:
v_fma_f16Rounding Constraints & Halfway Controls:tools/pixel_aot.py, explicitly reject unsupported rounding modes (if half_rounding != HALF_ROUNDING["0"]). Addedv_fma_f16andv_fma_f16_e64tocheck_mode_register_controls()intests/pixel_aot_check.pyto assert rejection underHALF_RTZ(HW_REG_MODE[3:2] = 3).check_controls()where RNE and RTZ differ:val = 1.0 + 2^-11 + 2^-12rounds up to0x3c01in RNE and truncates to0x3c00in RTZ.check_controls()distinguishing single rounding to binary16 from rounding to float32 first:Inputs
a = 0x3c69(1.1025390625),b = 0x3faa(1.916015625),c = 0x89d1(-0.00017750263214111328), yielding exacta * b + c = 2.11230456829071044921875. Since exact is strictly less than the binary16 midpoint2.1123046875, single rounding to binary16 rounds down to0x4039. In contrast, rounding to float32 first yields2.1123046875(0x40073000), exactly on the binary16 tie, breaking to even mantissa0x403a.v_div_fmas_f32Scaling Boundary & VCC Controls:valu_div_fmas_program().valu_div_fmas_program()andcheck_controls()using normal float32 edge input0x00800000(2^{-126}) multiplied by0.5(2^{-1}), producing intermediate subnormal2^{-127}. With VCC set, scaling by2^{32}yields2^{-95}, testing the exponent boundary and catching intermediate underflow before multiplication by the scale factor.Documentation:
docs/experiments.mddetailing the rounding mode constraints, halfway case behavior, and division scaling boundary controls.Imported from R0Y00/Xbox2Windows. Originally posted by rwZephyr on 2026-11-22T23:42:07Z.
I ran the exact halfway case from the review through the translated shader on
5b76958ce3e2c77bdef1c0acefe69e2a1322644a. The stockpython -B tests/pixel_aot_check.pycontrols passed.I loaded
0x3c69,0x3faa, and0x89d1from runtime constants, translatedv_fma_f16throughtranslate_int, and compiled with DXC-Gis -denorm preserve -T ps_6_2 -E main. I ranoffscreen_check <hardware|warp> --check-pixel-case <shader> <case>with aUINTreadback. The exact result rounds once to0x4039. Rounding through float32 gives0x403A.On both backends, pixel
(0, 0)read0x5AA5403Aagainst expected0x5AA54039, preserving the0x5AA5high-half marker. The single-rounding reference failed twice per backend. The double-rounded reference matched all 4,096 pixels twice per backend. The1 × 2 + 0sanity case passed. No skips.Windows 10 22H2
19045.6466, RTX 3060 Ti driver621.24(32.0.16.2124via DXGI), in-box WARP10.0.19041.5794, DXC1.8.2502.11. D3D12 debug layer unavailable on both. This case would make a useful GPU regression test alongside the Python reference.AI-assisted. All reported tests ran locally.
Imported from R0Y00/Xbox2Windows. Originally posted by Holastor on 2026-11-23T00:03:51Z.
@rwZephyr Thanks for the thorough investigation and hardware/WARP verification, @rwZephyr!
Your readback results confirm exactly what the halfway control in
check_controls()documents:A \times B + Cwith inputs0x3c69,0x3faa,0x89d1yields exact2.11230456829071044921875.pixel_aot(widening tofloat32, evaluatingmad, and narrowing viapack16_rne), the intermediatefloat32sum rounds to2.1123046875(0x40073000). This lands exactly on the binary16 halfway tie, breaking to even mantissa0x403A.0x4039because the exact mathematical result is strictly less than the midpoint.0x403A) across all 4,096 pixels on both RTX 3060 Ti and WARP, and correctly fails the single-rounding expectation.This matches the established convention across
pixel_aotfor binary16 arithmetic (such asv_pk_fma_f16andv_fma_mixlo_f16), which evaluates in binary32 to remain portable across desktop targets without requiring native 16-bit (float16_t) or 64-bit (double) shader capabilities.We can gladly wire this halfway case into an executable GPU/WARP draw test (asserting
0x403Aunder the binary32 lowering with a negative check against single-rounded0x4039) if @R0Y00 prefers having it as a regression test in this PR alongside the existing Python control anddocs/experiments.mddocumentation.Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-23T11:02:38Z.
Thanks for the exact RTX/WARP readback and for refusing the unverified divide-scale path. Keep the halfway case as a discriminator: the translated result
0x403aand the single-rounding reference0x4039differ at observable bit 0. Before enablingv_fma_f16for general inputs, use a source-built native RDNA readback to settle that case and which destination half is defined, or implement the verified result with a regression that fails on the present widen/mad/pack sequence. The existing packed/mix paths do not establish the non-packed opcode's behavior. A focusedv_div_fmas_f32patch can move separately. Its current source check only finds the expression; please run the two VCC branches with integer readback, including the2^-127intermediate to2^-95output case, and use a control that loses the scale or flushes the intermediate.Imported from R0Y00/Xbox2Windows. Originally posted by Holastor on 2026-11-23T12:06:52Z.
@R0Y00 Updated the PR per your feedback:
Focused scope:
v_div_fmas_f32lowering intools/pixel_aot.pyand the scanner.v_fma_f16(raisingValueError("Unverified v_fma_f16 binary16 FMA semantics")) and retained the rejection ofv_div_scale_f32.0x403awhile theoretical single-rounding evaluates to0x4039(differing at bit 0).Executable WARP integer readback draw test (
check_valu_div_fmas_draw):integer_export=Trueto read back exact bit patterns:2.0 * 0.5 + 1.0 = 2.0->0x40000000.saturate(2.0) = 1.0->0x3f800000.2^32 * (1.0 * 2.0 + 3.0) = 5.0 * 2^32->0x50a00000.2^-126 * 0.5 = 2^-127(0x00400000intermediate subnormal) scaled by2^32->2^-95(0x10000000), verifying-denorm preservein DXC.Negative mutation controls:
asfloat(0x4f800000u) * mad(withmad(;--check-pixel-casecatches the mismatch on channels 2 and 3 and exits 1.mad(withmad32((which flushes subnormals viaftz32), forcing intermediate2^-127 -> 0.0;--check-pixel-casecatches0x00000000 != 0x10000000on channel 3 and exits 1.Verification:
tests/pixel_aot_check.py,tests/rdna2_tools_check.py,tests/docs_links_check.py,tests/bidi_controls_check.py).ctest --preset dev -R '^pixel_aot_e64_warp$' -Vpassed locally (100%).docs/experiments.mdwith test definitions, observed bit values, and reproduction commands.Imported from R0Y00/Xbox2Windows. Originally posted by Holastor on 2026-11-23T12:37:01Z.
@R0Y00 Thank you for the detailed diagnosis regarding unfused HLSL
madon WARP and pre-scale underflow!We have updated the lowering and test suite at
3f0ba2dto address both points:Double-precision FMA lowering (
div_fmas32/div_fmas32_clamp):tools/pixel_aot.pynow lowersv_div_fmas_f32by promoting operands todoublein the preamble helper:hlsl float div_fmas32(float s0, float s1, float s2, bool cond) { double prod = (double)s0 * (double)s1 + (double)s2; return (float)(cond ? (prod * 4294967296.0) : prod); } float div_fmas32_clamp(float s0, float s1, float s2, bool cond) { double prod = (double)s0 * (double)s1 + (double)s2; double scaled = cond ? (prod * 4294967296.0) : prod; return (float)clamp(scaled, 0.0, 1.0); }double(div_fmas32_clamp) prior to casting down tofloatto prevent clamp instructions from flushing intermediate subnormals.2^-151do not prematurely underflow to zero before the2^32scale is applied.Runtime Constant Draw Tests (
check_valu_div_fmas_draw):s44,s45,s46) into the WARP integer readback test, validating:(0x3f800001, 0x3f7ffffe, 0xbf800000)evaluate(1 + 2^-23)(1 - 2^-23) - 1.0 = -2^-46, reading exact fused bit patterns0xa8800000(VCC=false) and0xb8800000(VCC=true) on WARP.(0x00800000, 0x33000000, 0)evaluate2^-126 * 2^-25 = 2^-151, which avoids early float32 rounding and scales by2^32to yield2^-119(0x04000000) on WARP.(0x00800000, 0x3f000000, 0)evaluate2^-126 * 0.5 = 2^-127(0x00400000), scaling to2^-95(0x10000000).(0x40000000, 0x3f000000, 0x3f800000)verify unscaled2.0(0x40000000), scaled2.0 * 2^32(0x50000000), and clamped1.0(0x3f800000).Three Negative Mutation Controls:
2^32scale fails--check-pixel-case.madfails--check-pixel-case(catching cancellation and underflow loss).mad32) fails--check-pixel-case.Rejection of Unverified Paths Preserved:
v_fma_f16andv_div_scale_f32, and retained the halfway discriminator test in Python controls.Local CTest
pixel_aot_e64_warpand all pure-Python checks pass.Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-23T12:50:51Z.
I retested 3f0ba2d. The new double intermediate still has a finite double-rounding counterexample, on both WARP and RTX 4060 Laptop. In the new integer-export fixture, add runtime words (0x3f800001, 0x3fc00000, 0x8d800000). The exact product is 1.5 + 3*2^-24, the midpoint between binary32 words 0x3fc00001 and 0x3fc00002. The addend is -2^-100, so the exact sum rounds to 0x3fc00001. It is lost in the binary64 addition, and the subsequent tie-to-even conversion chooses 0x3fc00002.
Both backends returned 0x3fc00002 with VCC clear and 0x4fc00002 with VCC set. The independently computed single-rounding references are 0x3fc00001 and 0x4fc00001. The expected-bit fixture fails on the first pixel, and the double-rounded control matches. Windows 26200.9974, WARP 10.0.26100.9818, RTX driver 32.0.16.1656, debug layer enabled.
Please add this runtime-input row to the bitwise fixture before claiming single-rounding. A correction must retain which side of a binary32 midpoint the exact sum lies on, or reject inputs for which the candidate cannot prove that result. Keep the existing cancellation and pre-scaling rows.
Imported from R0Y00/Xbox2Windows. Originally posted by Holastor on 2026-11-23T13:11:02Z.
@R0Y00 Thank you for the exact midpoint tie counterexample (
0x3f800001,0x3fc00000,0x8d800000)!We have updated the implementation and test suite at
9fff1ee:Knuth 2Sum Remainder Tracking & Midpoint Tie Resolution:
tools/pixel_aot.pynow tracks the exact remaindererr = (a * b + c) - sof the binary64 addition using Knuth's 2Sum algorithm underprecise.s_sclands on a binary32 midpoint (where bits below the guard bit are zero, i.e.(u0 & 0x1fffffffu) == 0x10000000ufor normal floats or the corresponding shifted mask for subnormals) anderr_sc != 0.0, the representation is nudged in the direction oferr_sc:floatcorrectly breaks towards the side of the midpoint that the exact real sum lies on.Added Midpoint Row to Bitwise Readback Fixture (
check_valu_div_fmas_draw):(0x3f800001, 0x3fc00000, 0x8d800000)now verifies exact single-rounded readback:0x3fc000010x4fc00001-2^-46->0xa8800000/0xb8800000), pre-scale underflow (2^-151->0x04000000), denormal scale boundary (2^-127->0x10000000), and normal arithmetic / clamp rows.Four Negative Mutation Controls:
--check-pixel-casecatches the double-rounding error (0x3fc00002/0x4fc00002 != 0x3fc00001 / 0x4fc00001) and exits 1.mad, and FTZ negative controls.Local CTest
pixel_aot_e64_warpand all pure-Python checks pass.Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-23T13:22:01Z.
The normal midpoint now has a dedicated regression. I found a separate subnormal midpoint failure on
9fff1eeusing runtime operand words (0x1a000001, 0x19fffffe, 0x00400001). The exact product is 2^-150 minus 2^-196, so adding the third operand lands just below the midpoint between subnormal words 0x00400001 and 0x00400002. The expected unscaled bits are 0x00400001. WARP 10.0.26100.9818 and RTX 4060 Laptop 32.0.16.1656 both gave 0x00400002 in the unclamped and clamped channels. Both returned the correct scaled 0x10000003. Windows 26200.9974, debug layer enabled.For subnormals the result grid stays at 2^-149, so decreasing the binary64 exponent increases the number of discarded significand bits. The current mask shifts the other way. The zero-to-least-subnormal midpoint also has biased exponent 873, where the guard bit is implicit. Please retain the original normal midpoint and check this subnormal boundary too.
Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-23T13:32:11Z.
I fast-forwarded your branch to
6292c35with the subnormal midpoint fix. The guard now follows the fixed binary32 subnormal spacing across both binary64 words. The two new runtime rows cover a guard in each word. The normal and both subnormal rows fail when the midpoint correction is disabled.WARP and RTX 4060 Laptop passed the original and new integer readback cases and all mutation controls. LLVM 15 and form-coverage checks passed. The double helper is emitted only when this operation is used, and the draw has a standalone CTest. The unverified opcodes still reject. GitHub CI is running.