Repository navigation
Fix GH-25744: DFA pass turning a float + 0.0 into a copy that keeps -0.0 - #25608
ChrisAbner wants to merge 2 commits into
Conversation
The DFA pass replaced x + 0.0, 0.0 + x and x - 0.0 with a copy of x when x is known to be a double. The check Z_DVAL_P(zv) == 0.0 accepts both zeros, but only x + -0.0 and x - 0.0 give back x for every double: -0.0 + 0.0 and -0.0 - -0.0 are 0.0. The long 0 in $x + 0 is turned into 0.0 first, so that form was affected too. So round($v, 2) + 0.0, the usual way to get rid of a negative zero, kept -0.0 with OPcache. Only rewrite a double ADD when the constant is -0.0, and a double SUB when it is 0.0.
| function prop_add_zero(C $c) { return $c->p + 0; } | ||
|
|
||
| /* These were already correct and must stay so */ | ||
| function sub_zero(float $x) { return $x - 0.0; } |
There was a problem hiding this comment.
This checks the runtime values, so it catches the broken folds ($x + 0.0, 0 + $x, $x - -0.0), but it does not verify that the kept identity folds ($x - 0.0, $x - 0, $x + -0.0, -0.0 + $x) still optimize to QM_ASSIGN. Could you add a companion test with opcache.opt_debug_level=0x20000 to pin both sides?
There was a problem hiding this comment.
Added add_sub_zero_signed_zero_opt.phpt with opt_debug_level=0x20000. The folds that could turn -0.0 into 0.0 keep the ADD/SUB, and $x - 0.0, $x - 0, $x + -0.0 and -0.0 + $x still become QM_ASSIGN. It fails on PHP-8.4 without the fix and passes with it, with and without JIT.
| && Z_DVAL_P(zv) == 0.0)) { | ||
| && Z_DVAL_P(zv) == 0.0 | ||
| /* -0.0 + 0.0 and -0.0 - -0.0 are 0.0, so only x + -0.0 and x - 0.0 are x */ | ||
| && (opline->opcode == ZEND_ADD) == !!signbit(Z_DVAL_P(zv)))) { |
There was a problem hiding this comment.
Instead of Z_DVAL_P(zv) == 0.0 with signbit() / !!signbit(), consider comparing bitwise with memcmp() against static const double 0.0 and -0.0, like sccp.c does since GH-24063. That checks both value and sign in one step, avoids the !! normalization, and avoids introducing the first signbit() call in Zend:
static const double neg_zero = -0.0;
static const double pos_zero = 0.0;
memcmp(&Z_DVAL_P(zv), opline->opcode == ZEND_ADD ? &neg_zero : &pos_zero, sizeof(double)) == 0(And &neg_zero directly in the op1 ZEND_ADD check above.)
There was a problem hiding this comment.
Thanks, done in 77222c1. Both checks now use memcmp() against static const -0.0/0.0, like sccp.c, so there is no signbit() left.
Use memcmp() against static -0.0 and 0.0 constants instead of == 0.0 with signbit(), as sccp.c does since phpGH-24063. This checks the value and the sign in one step. Add a test that dumps the optimized opcodes, so it checks both sides: the folds that could turn -0.0 into 0.0 keep the ADD or SUB, and x - 0.0, x - 0, x + -0.0 and -0.0 + x still become a QM_ASSIGN.
Fixes GH-25744.
The DFA pass replaces
$x + 0.0and$x - -0.0with a copy of$xwhen$xis a float. That is wrong for-0.0:-0.0 + 0.0is0.0, but the copy keeps-0.0.$x + 0is affected too, because the int0is turned into0.0first.Adding zero is the usual way to drop the
-0thatround()can leave, and with OPcache it no longer works:PhpSpreadsheet does this in
Formatter.php("avoid weird '-0' problem"). With OPcache, a-0.0value with the0%format is shown as-0%.The fix keeps the rewrite only when it is exact:
ADDwith-0.0andSUBwith0.0, checked withsignbit(). Ints are unchanged. So$x - 0.0and$x + -0.0are still turned into a copy, and only$x + 0.0stays an addition. I did not remove the float case completely, because those two forms are correct and would lose the optimization for no reason. This is the same family as GH-24063, where SCCP treated0.0and-0.0as the same value.Introduced by 3024d9c in 8.0.0, confirmed by bisect. Reproduced on 8.0.30, 8.1.34, 8.4.28, 8.5.13 and 8.6.0. The patch applies cleanly on PHP-8.5, PHP-8.6 and master.
Verification
$x = -0.0$x + 0,0 + $x,$x + 0.0,$x - -0.0float(0)float(-0)float(0)round(-0.004, 2) + 0.0float(0)float(-0)float(0)$x - 0.0,$x + -0.0(already correct)float(-0)float(-0)float(-0)The new test
ext/opcache/tests/opt/add_sub_zero_signed_zero.phptfails without the fix and passes with it, without the JIT, with tracing and with function JIT.On PHP-8.4,
Zend/testspasses with and without OPcache.ext/opcache/testshas the same failures as the base commit in my environment (gh8739andgh17733, plus twopreload_restart_*tests with tracing). They restart OPcache or start a sub-process. On master,Zend/testsandext/opcache/testspass.Not covered: I looked for the same mistake in the rest of the optimizer.
block_pass.candsccp.conly fold an addition when both sides are constants, so they compute the real result. I found no other place that treats0.0and-0.0as the same.