Repository navigation
shader: lower v_div_fmas_f32, reject unverified v_fma_f16 and v_div_scale_f32 - #704
Conversation
5a29dde to
fc10172
Compare
|
Review of A source-only reproducer, from the PR checkout: python3 -B - <<'PY'
import sys
sys.path[:0] = ['tests', 'tools']
import pixel_aot_check as p
hlsl = p.translate(p.uncompressed_export([
'v_mov_b32_e32 v19, 1.0',
'v_mov_b32_e32 v20, 0x71800000',
'v_div_scale_f32 v12, vcc_lo, v19, v19, v20',
'v_cndmask_b32_e64 v13, 0, 1.0, vcc_lo',
]))
print('\n'.join(line for line in hlsl.splitlines()
if 'bool cmp' in line or ('precise float alu' in line and 'cmp' in line)))
PYHere 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. 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 |
|
Thanks @Joacoorandom for the detailed review, ISA reference, and reproducer! Merged Holastor#2 into the branch. Taking the conservative approach of keeping |
e12704d to
7054700
Compare
|
The revised head now refuses |
|
Thanks @R0Y00 for the detailed review and guidance! We have updated the PR to address both points:
|
|
I ran the exact halfway case from the review through the translated shader on I loaded On both backends, pixel Windows 10 22H2 AI-assisted. All reported tests ran locally. |
|
@rwZephyr Thanks for the thorough investigation and hardware/WARP verification, @rwZephyr! Your readback results confirm exactly what the halfway control in
This matches the established convention across We can gladly wire this halfway case into an executable GPU/WARP draw test (asserting |
5b76958 to
cb2b0d1
Compare
|
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 |
cb2b0d1 to
88c75f3
Compare
|
@R0Y00 Updated the PR per your feedback:
|
R0Y00
left a comment
There was a problem hiding this comment.
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).
- Supply a,b,c as 0x3f800001, 0x3f7ffffe, 0xbf800000 from the constant buffer. The exact product plus addend is -2^-46. WARP read 0 for both VCC states, where the fused bits are 0xa8800000 (VCC false) and 0xb8800000 (true). RTX read the expected bits. HLSL mad permits unfused results even with precise. https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/mad
- With VCC true, inputs 0x00800000, 0x33000000, 0 produce 2^-151 before scale and 2^-119 after it under the documented fused-scale formula. Both backends return 0 rather than 0x04000000 because the current separate scale follows rounding to f32. Include this in the native RDNA check if the target behaves differently from the documented formula. https://docs.amd.com/api/khub/documents/Et~wpu9g~Ffl7d9q0QZ~Og/content
Please 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.
88c75f3 to
3f0ba2d
Compare
|
@R0Y00 Thank you for the detailed diagnosis regarding unfused HLSL We have updated the lowering and test suite at
Local CTest |
|
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. |
…cale_f32 Lower v_div_fmas_f32 using double precision with Knuth 2Sum remainder tracking to guarantee strict single-rounding, avoiding double-rounding midpoint tie errors and preventing premature underflow before scaling by 2^32. Reject unverified v_fma_f16 and v_div_scale_f32 instructions until native RDNA2 readback confirms binary16 rounding, high-half preservation, and prescaling behavior. Add WARP draw tests with integer readback covering midpoint single-rounding (0x3fc00001 / 0x4fc00001), cancellation (-2^-46), pre-scale underflow (2^-151 -> 2^-119), denormal scale boundary (2^-127 -> 2^-95), and normal arithmetic with clamp, accompanied by four negative mutation controls.
3f0ba2d to
9fff1ee
Compare
|
@R0Y00 Thank you for the exact midpoint tie counterexample ( We have updated the implementation and test suite at
Local CTest |
|
The normal midpoint now has a dedicated regression. I found a separate subnormal midpoint failure on 9fff1ee using 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. |
Resolved on 6292c35 after bitwise WARP and RTX checks of cancellation, underflow and normal/subnormal midpoints. Unverified opcodes remain rejected.
|
I fast-forwarded your branch to 6292c35 with 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. |
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.