Repository navigation
feat(shader): implement v_div_scale/fmas/fixup_f32 and s_denorm_mode (f32 division macro) - #264
Conversation
6b9e06d to
d4498b6
Compare
d4498b6 to
217b68d
Compare
217b68d to
de02e86
Compare
|
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. |
|
Thanks, both points were right. Rebased onto main and changed:
|
de02e86 to
87151db
Compare
…f32 and s_denorm_mode These make up the precise f32 division macro compilers emit for a / b. No reference emulator implements them and the ISA pseudocode disagrees with the hardware, so the rules were measured on an RDNA2 GPU: - div_scale decides from the exponent fields only: 2^64 or 2^-64 scaling and VCC for differences >= 96 or <= -96, a denormal or huge (>= 253) denominator and a numerator exponent <= 24; a zero input gives 0xffc00000. - div_fmas scales by 2^64 when the exponent of S2 is >= 128, else 2^-64, before the single rounding (the ISA says a fixed 2^32). - div_fixup follows the ISA, and a NaN quotient with finite inputs becomes +-inf. s_denorm_mode is accepted and ignored; listed in TechnicalDebt. Co-Authored-By: Claude <noreply@anthropic.com>
…nd refuse s_denorm_mode v_div_scale_f32 returns a NaN source unchanged instead of scaling it, and v_div_fmas_f32 quiets the first NaN input or returns the default NaN 0xffc00000, so results no longer depend on the driver's NaNs. The test compares NaN results bit for bit. s_denorm_mode throws: the recompiler does not pin a denormal mode, so no value matches what it models. Co-Authored-By: Claude <noreply@anthropic.com>
87151db to
1a55076
Compare
|
Thanks, both points are fixed.
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 |
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 fix(shader): keep fused multiply-adds fused and v_mad/v_mac_f32 rounded twice #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)