feat(shader): implement v_div_scale/fmas/fixup_f32 and s_denorm_mode (f32 division macro) #264
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/264"
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 boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-01T20:54:15Z.
What
Implements the three helpers of the precise f32 division macro and
s_denorm_mode, which the macro uses. LLVM emits this for everya / bthat is not fast-math, so these were a hard stop for ordinary shaders:KytyPS5, shadPS4 and fpPS4 don't implement them. The RDNA ISA pseudocode does not match the hardware, so I measured the behaviour on an RDNA2 GPU (Radeon 660M, gfx1035, same VALU as gfx1013) with raw kernels through the ROCm HSA runtime:
v_div_scale_f32: decided by the exponent fields only. The rules below matched the hardware on 12000/12000 random inputs with S0 ∈ {S1, S2}, which is how the macro uses it:e2 - e1 >= 96: VCC=1, and the denominator call scales ×2^64.e1 >= 253: ife2 - e1 <= -96, VCC=1 and the denominator call scales ×2^-64; otherwise both calls scale ×2^-64.e2 - e1 <= -96: VCC=1, and S0 scales ×2^64 unless it is the denominator.e2 <= 24(the ISA says 23): both calls scale ×2^64.0xffc00000; VCC still follows the rules above.v_div_fmas_f32: with VCC, the scale is 2^64 when exponent(S2) >= 128 and 2^-64 otherwise (the ISA says a fixed 2^32). It is applied before the single rounding, so an fma that would overflow can still scale back into range. The translation multiplies the rounded fma by the power of two, which is exact when it stays normal. When it doesn't (scaling up from zero/denormal, or down from inf), it scales the factor that can't underflow or overflow instead.v_div_fixup_f32: the ISA pseudocode, plus one case it misses: a NaN quotient with finite inputs gives ±inf. 8000/8000.s_denorm_mode 12) on 20000/20000 inputs.s_denorm_modeis accepted and ignored, because denormals follow the Vulkan float controls. It is listed under silent stubs in TechnicalDebt.The VOP3B form of
v_div_scale_f32writes VCC through the SDST field likev_mad_u64_u32and acceptsneg.fmas/fixuptake abs/neg; clamp and omod stay rejected.Test
agc_driver_division, two hand-assembled shaders (words fromllvm-mc -mcpu=gfx1013):div_scaleresult and VCC,fmasandfixup, all captured on the RDNA2 GPU. They cover every scale branch plus inf/NaN/zero/max inputs.div_scaleis checked only where S0 ∈ {S1, S2}.llc -mcpu=gfx1013onfdiv float) on 256 (a, b) pairs, with the quotient captured on the GPU: specials, exponent spreads ≥ 90, huge denominators, tiny numerators.Like #263, fma fusion can't be required in Vulkan, and Ubuntu's lavapipe (CI) splits it.
v_div_fmas_f32and the macro depend on a fused fma, so the test first runs onev_fma_f32on an input where fused and unfused differ. If the device splits it, those checks are skipped with a message;div_scaleanddiv_fixupare exact and always checked.Rows involving denormal inputs or results, and quotients in the denormal range, are left out, since their handling depends on the Vulkan driver's float controls.
Tested
Linux, full ctest (100) passes.
v_mul_f32and the following fma, so this PR is stacked on #263.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-01T20:54:42Z.
Progress report (
2cf901b→1a55076)📈 GPU shader instructions: 80.62% (+0.35%, +4 implemented instructions)
✅ 4 implemented
S_DENORM_MODEV_DIV_FIXUP_F32V_DIV_FMAS_F32V_DIV_SCALE_F32Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-03T09:44:42Z.
Rebased on main. #263 is no longer needed as a base: its commit landed with #251, so #263 is closed and this PR is a single commit now. In the VOP3 modifier check,
v_div_scale_f32's neg exception now sits next to main's VOPC float-compare exception.Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-04T05:39:24Z.
Thanks for measuring all of this on hardware; the div_scale thresholds and the NaN quotient case in div_fixup are really useful. One thing I noticed: v_div_scale_f32 scales a NaN source with an FPMul by 2^64 or 2^-64, so the NaN that comes back is whatever the driver returns. Your own vector at Division.cpp line 62 (S0 = S1 = 0x7f800001) expects the signalling NaN back unchanged, but an x86 lavapipe multiply gives 0x7fc00001, and the test still passes because Same() accepts any NaN for any NaN. v_div_fmas_f32 has the same issue through FPFma32. In #474 the f64 versions choose NaNs on the bits (div_scale returns a NaN source unchanged), so doing the same here, and comparing NaN results bit for bit in Division.cpp, would make the two consistent and exact. Separately, s_denorm_mode goes into the silent stubs list. CONTRIBUTING keeps silent stubs to UI-only behaviour that unblocks a title, and a denormal mode change alters results, so could it translate only the mode the recompiler already assumes as a no-op and throw for any other value, as on #333 and #436? Main's existing s_setreg_b32 no-op is better recorded under "Functional" in TechnicalDebt.md than under silent stubs.
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-04T06:33:14Z.
Thanks, both points were right. Rebased onto main and changed:
v_div_scale_f32returns a NaN source unchanged instead of multiplying it by 2^±64.v_div_fmas_f32returns the first NaN input (a, b, c) quieted, or the default NaN 0xffc00000 when the fma itself is invalid, so it no longer depends on the driver's NaN either. This matches the f64 versions in #474 and the hardware rows.Same()is gone). Without the div_scale change, lavapipe fails on vector 13 (0x7fc00001, expected0x7f800001). The default-NaN select only shows up on NVIDIA, because RADV and x86 lavapipe already produce 0xffc00000. Passes on RADV, local lavapipe and CI's lavapipe (Mesa 25.2.8).s_denorm_mode: the recompiler doesn't set any float controls (no DenormPreserve/FlushToZero), so denormals follow the driver and no immediate matches what it models. It now throws for every value, and the silent stubs line is gone. In the macro test the twos_denorm_modewords are nows_nop 0. Translating the macro's 15/12 pair exactly would need DenormPreserve32 where the device supports it, which I'd rather do as a separate PR.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-04T10:06:20Z.
Rebased on current main (conflicts with the f64 ops of #446 and
v_mad_i64_i32of #435 in the opcode enum/classifier, the VOP3 table and the VOP3b sdst/float-modifier lists; both sides kept). Division passes on RADV, local lavapipe and CI's lavapipe; DivFixupF16 and Vop3MadI64 still pass.Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-04T22:37:46Z.
Thanks, both points are fixed.
v_div_scale_f32now leaves a NaN source alone, so vector 13 gets 0x7f800001 back instead of whatever the driver's multiply makes of it, andv_div_fmas_f32picks its NaN on the bits: the first NaN of a, b, c quieted, otherwise 0xffc00000 when the fma itself is invalid, the same rule as the f64 versions in #474. WithSame()gone the test compares every result bit for bit.agc_driver_divisionpasses on my NVIDIA machine, built on current main together with the other open shader PRs, and it fails on main's code (the decoder refuses the opcodes).s_denorm_modethrowing for every value is fine with me: main didn't decode it at all, so nothing that worked before stops working, and you're right that no immediate matches what the recompiler models while it sets no float controls. DenormPreserve as a separate PR sounds good.One thing for whoever lands second: this and #436 both add a SOPP opcode next to each other (0x25 here, 0x24 there), so the second one needs a one-line rebase in
RdnaScalarOpDecoder.cppandScalarInstructions.cpp. These are new opcodes that main refused, so it's ready to merge from my side.