perf(shader): drop the input flush that rcp, rsq, sqrt, exp and log results never use #4705
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/4705"
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-12T19:22:34Z.
What
v_rcp_f32,v_rsq_f32,v_sqrt_f32,v_exp_f32andv_log_f32no longer flush their input a second time in the SPIR-V backend. The result for a denormal input already comes from the translator's special-value selects.Background.
FPRecip32,FPRecipSqrt32,FPSqrt,FPExp2andFPLog2flushed a denormal input to its signed zero (EmitFlushF32DenormToSignedZero) before the host operation.unaryFloatSpecialstreats every input below 2^-126 as a zero of the same sign for these opcodes. #537 measured that on RDNA2: "f32 denormal inputs ofv_rcp,v_rcp_iflag,v_rsq,v_sqrt,v_logandv_expcount as zero with their sign, in every mode". It replaces the host result for those inputs:rcpandrsq: a signed infinity;sqrt: the signed zero;log: -inf;exp: 1.0.floatUnaryandfloat16Unaryboth go through it. So the flushed input only changed values that are thrown away.v_rcp_iflag_f32(FPRecipIFlag32) gets the same specials and never had the backend flush.How.
EmitFlushF32DenormToSignedZero, which has no other user, is removed.FPRecip32doesn't go throughunaryFloatSpecials: the pixel prologue'sPositionW, the reciprocal ofFragCoord.w. The prologue now flushesFragCoord.win the IR before the reciprocal, so it gives the same result as before.Why results don't change.
zeroselect inunaryFloatSpecialsis the last one applied to the value, so the host result is discarded.negativeexcludes those inputs, so the NaN select doesn't pick them either.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:v_rcp_f32v_rsq_f32v_sqrt_f32v_exp_f32v_log_f32v_rcp_f16v_rsq_f16v_sqrt_f16v_exp_f16v_log_f16v_rcp_iflag_f32, which has the same special values and never had the flush, is 8.0 on both.v_rcp_f323,547,v_sqrt_f321,855,v_rsq_f321,758,v_exp_f32927,v_log_f32502. With the counts above, that's about 27,000 fewer SASS instructions. The f16 forms don't appear in the census.ands, two compares, a logicalandand a select) on every GPU.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
mainat31ade9abc. The branch is now rebased onto682ef0c17, where I reranagc_driver_vop1_float_unary,agc_driver_f32_denormal_flush,agc_driver_float16_misc,agc_driver_relative_stage_inputsandagc_driver_relative_ancillaryon the three devices. They pass.agc_driver_vop1_float_unaryruns the five instructions over rows that include 0x007fffff and 0x807fffff, with the hardware results from #537. It passes on the three devices, and so doagc_driver_f32_denormal_flushandagc_driver_float16_misc.zeroresult selects inunaryFloatSpecialsnarrowed to exact zeros, it fails on lavapipe:sqrt_f32 v4 is 0xffc00000, expected 0x80000000. lavapipe keeps the denormal, so on that host the special values are now the only thing that gives these results. NVIDIA and Intel still pass with that change, because they flush the input themselves.zerorule itself narrowed to exact zeros, it fails on all three devices with the same message.PositionW:agc_driver_relative_stage_inputsandagc_driver_relative_ancillaryload it, and pass on the three devices.agc_driver_*,agc_shader_*,agc_spirv_*,vector_*,agc_masked_select,agc_validate_program,agc_denormal*,agc_float16_*; 350 tests):agc_driver_flat_loads*,agc_driver_global_loads*,agc_driver_flat_apertures*,agc_driver_pixel_interlock.agc_driver_ds_swizzleandagc_driver_f32_dot2_rounding, which fail 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 adriadam10 on 2026-10-13T15:17:17Z.
Checked the diff against
unaryFloatSpecials: for |x| < 0x00800000 thezeroselect is the last one applied, so the host result for denormal inputs is discarded, and for every other input the removed flush was an identity. ThePositionWreciprocal keeps its flush in the IR. Measuredv_rcp/rsq/sqrt/exp/log_f32on the HW oracle (IEEE=0, DENORM=0) over 0x007fffff, 0x807fffff, 0x00000001, 0x80000001, 0 and the smallest normals: denormals behave as signed zero, matching the selects.agc_driver_vop1_float_unary,agc_driver_f32_denormal_flush,agc_driver_float16_miscandagc_driver_relative_stage_inputspass; merging once the full Build is green.Imported from boykopovar/AnyPS5. Originally posted by karanvora2599 on 2026-10-13T21:53:56Z.
Thanks @adriadam10 for checking the denormal inputs on the hardware oracle.