Skip to content

Fix GH-25744: DFA pass turning a float + 0.0 into a copy that keeps -0.0 - #25608

Open
ChrisAbner wants to merge 2 commits into
php:PHP-8.4from
ChrisAbner:fix-dfa-add-zero-negative-zero
Open

ChrisAbner wants to merge 2 commits into
php:PHP-8.4from
ChrisAbner:fix-dfa-add-zero-negative-zero

Conversation

@ChrisAbner

@ChrisAbner ChrisAbner commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Fixes GH-25744.

The DFA pass replaces $x + 0.0 and $x - -0.0 with a copy of $x when $x is a float. That is wrong for -0.0: -0.0 + 0.0 is 0.0, but the copy keeps -0.0. $x + 0 is affected too, because the int 0 is turned into 0.0 first.

Adding zero is the usual way to drop the -0 that round() can leave, and with OPcache it no longer works:

function amount(float $v): float { return round($v, 2) + 0.0; }
var_dump(amount(-0.004)); // float(0) without OPcache, float(-0) with it

PhpSpreadsheet does this in Formatter.php ("avoid weird '-0' problem"). With OPcache, a -0.0 value with the 0% format is shown as -0%.

The fix keeps the rewrite only when it is exact: ADD with -0.0 and SUB with 0.0, checked with signbit(). Ints are unchanged. So $x - 0.0 and $x + -0.0 are still turned into a copy, and only $x + 0.0 stays 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 treated 0.0 and -0.0 as 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

Case, with $x = -0.0 Without OPcache Before After
$x + 0, 0 + $x, $x + 0.0, $x - -0.0 float(0) float(-0) float(0)
round(-0.004, 2) + 0.0 float(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.phpt fails without the fix and passes with it, without the JIT, with tracing and with function JIT.

On PHP-8.4, Zend/tests passes with and without OPcache. ext/opcache/tests has the same failures as the base commit in my environment (gh8739 and gh17733, plus two preload_restart_* tests with tracing). They restart OPcache or start a sub-process. On master, Zend/tests and ext/opcache/tests pass.

Not covered: I looked for the same mistake in the rest of the optimizer. block_pass.c and sccp.c only fold an addition when both sides are constants, so they compute the real result. I found no other place that treats 0.0 and -0.0 as the same.

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.
@ChrisAbner
ChrisAbner requested a review from dstogov as a code owner November 19, 2026 18:31
@ChrisAbner ChrisAbner changed the title Fix DFA pass turning a float + 0.0 into a copy that keeps -0.0 Fix GH-25744: DFA pass turning a float + 0.0 into a copy that keeps -0.0 Nov 25, 2026
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; }

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread Zend/Optimizer/dfa_pass.c Outdated
&& 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)))) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@iliaal iliaal left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good now, thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants