perf(shader): treat f32 fract, sin and cos results as denormal-free #4737
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/4737"
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 karanvora2599 on 2026-10-12T20:11:03Z.
What
In the float modes that flush f32 denormals, the result flush after
v_fract_f32,v_sin_f32andv_cos_f32is now dropped when their input was already flushed.Stacked on #4490: the first commit is #4490's, the second is this change.
Background.
FLOAT_MODEbits 5:4 = 0, as in titles' shaders; #1850),f32DenormalFlushForgives these three instructions 3:floatUnaryflushes their input, andwriteOperandflushes their result.DenormalFlushEliminatordrops a flush whose value it can show is never a denormal. It can followfloatUnary's selects andunaryFloatSpecials' picks when IEEE mode is off, but it had no rule forFPFract32,FPSinorFPCos. So the result flush stayed, although the input had just been flushed.How.
DenormalFreeValues::computetreatsFPFract32,FPSinandFPCosas denormal-free when their operand is, in the same case as the min/max opcodes from #4490.Why results don't change. For an input that is not a denormal, none of the three emitters returns a denormal:
EmitFPFract32returnsfract(|x|)or1 - fract(|x|).fract(|x|)is|x|itself below 1. From 1 up it is a multiple of the input's ulp, so 0 or at least 2^-23.1 - fract(|x|)is at least 2^-24, becausefract(|x|)is at most 1 - 2^-24.EmitFPSinandEmitFPCosreduce the input withx - roundEven(x), which is exact. The cycle is then 0 or at least 2^-126 in magnitude.|cycle|or0.5 - |cycle|for sin,0.25 - |cycle|for cos). That subtraction is exact whenever the distance is below 1/8.TrigLinearNearZeroreturns the distance times about 2π (0x40c90fdbor0x40c90fd5), so 0 or more than 2^-124.SeriesWhenFarreturns the series value, or the driver's value when it is within 4 ulps of the series. The series is then at leastsin(2π · 2^-12), about 1.5e-3.floatUnaryreplaces with its own NaN anyway.Limit: IEEE mode. As in #4490,
floatUnaryquiets a NaN input with anorwhen IEEE mode is on, which the eliminator can't follow. These results keep their flush there.Before / after on an RTX 4060 Laptop (Ada, driver 591.86). SASS instructions per instruction come from the
APS5_PIPELINE_STATSbinary size over a chain of 16 instructions, withFLOAT_MODE0xc0 and IEEE mode off, compared against #4490 alone:v_fract_f32v_sin_f32v_cos_f32v_sin_f32811,v_fract_f32760,v_cos_f32263. That's about 3,700 fewer SASS instructions.Tested
Windows 11, MinGW-w64 GCC 15.2.0, Release, this branch. Devices:
GALLIUM_OVERRIDE_CPU_CAPS=avx.The sweep and the full runs below were on
mainat682ef0c17. The branch is now rebased onto9742ec12c, where I reranagc_denormal_flush_eliminationand the sevenagc_driver_*tests listed under Results on the three devices. They pass.agc_denormal_flush_elimination, one new case: a fract, sin or cos of a flushed value loses its flush, and one of an unknown value keeps it. With the three opcodes taken out of the rule, the case fails.v_fract_f32,v_sin_f32orv_cos_f32with f32 denormals flushed and IEEE mode off, so the change applies to them. They pass on the three devices:agc_driver_sin_cos_near_zero, whose rows include ±2^-126 and inputs one ulp from 0.25 and 0.5 cycles, the cases closest to the bounds above;agc_driver_sin_cos_reduction,agc_driver_nan_quieting,agc_driver_dx10_clampandagc_driver_f32_result_clamp.agc_driver_vop1_float_unaryandagc_driver_f32_denormal_flushalso pass.agc_driver_*,agc_shader_*,agc_spirv_*,vector_*,agc_masked_select,agc_validate_program,agc_denormal*,agc_float16_*; 351 tests):agc_driver_flat_loads*,agc_driver_global_loads*,agc_driver_flat_apertures*,agc_driver_pixel_interlock.agc_driver_ds_swizzle, which fails the same way onmain.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 xXPBSXx on 2026-10-13T03:27:07Z.
Windows test (MinGW GCC 15.2, Release, Windows 10, RTX 4080 SUPER). Built on current main
0c3dc9ca2together with eight other open GPU PRs (their own commits cherry-picked, no merge commits), so only the tests that belong to this PR say something about it:tests/DenormalFlushElimination.cpp(agc_denormal_flush_elimination): 3 of 3 runs passed.Not tested: no game run. The batch was built once, not per PR; it had no failures, so no bisect was needed.
AI-assisted (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by karanvora2599 on 2026-10-13T21:53:59Z.
Thanks @xXPBSXx for the RTX 4080 SUPER run. This one is stacked on #4490, so it needs that one merged first.
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T02:38:26Z.
Closing: no title is known to reach this. With the queue at 700+ open PRs, please hold off on new PRs that don't name a title needing the change until it's down. If a title stops on this on current main, reopen with the title ID and the log line.
Pull request closed