Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions Zend/Optimizer/dfa_pass.c
Original file line number Diff line number Diff line change
Expand Up @@ -1034,6 +1034,10 @@ static bool zend_dfa_try_to_replace_result(zend_op_array *op_array, zend_ssa *ss
return 0;
}

/* Compared bitwise with memcmp(), so that 0.0 and -0.0 are told apart */
static const double neg_zero = -0.0;
static const double pos_zero = 0.0;

void zend_dfa_optimize_op_array(zend_op_array *op_array, zend_optimizer_ctx *ctx, zend_ssa *ssa, zend_call_info **call_map)
{
if (ctx->debug_level & ZEND_DUMP_BEFORE_DFA_PASS) {
Expand Down Expand Up @@ -1160,9 +1164,11 @@ void zend_dfa_optimize_op_array(zend_op_array *op_array, zend_optimizer_ctx *ctx
&& Z_LVAL_P(zv) == 0)
|| ((OP2_INFO() & (MAY_BE_ANY|MAY_BE_UNDEF)) == MAY_BE_DOUBLE
&& Z_TYPE_INFO_P(zv) == IS_DOUBLE
&& Z_DVAL_P(zv) == 0.0)) {
/* 0.0 + -0.0 is 0.0, so only -0.0 is the identity */
&& memcmp(&Z_DVAL_P(zv), &neg_zero, sizeof(double)) == 0)) {

// op_1: #v.? = ADD 0, #?.? [double,long] => #v.? = QM_ASSIGN #?.?
// op_1: #v.? = ADD 0, #?.? [long] => #v.? = QM_ASSIGN #?.?
// op_1: #v.? = ADD -0.0, #?.? [double] => #v.? = QM_ASSIGN #?.?

opline->opcode = ZEND_QM_ASSIGN;
opline->op1_type = opline->op2_type;
Expand Down Expand Up @@ -1211,9 +1217,12 @@ void zend_dfa_optimize_op_array(zend_op_array *op_array, zend_optimizer_ctx *ctx
&& Z_LVAL_P(zv) == 0)
|| ((OP1_INFO() & (MAY_BE_ANY|MAY_BE_UNDEF)) == MAY_BE_DOUBLE
&& Z_TYPE_INFO_P(zv) == IS_DOUBLE
&& 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 */
&& memcmp(&Z_DVAL_P(zv), opline->opcode == ZEND_ADD ? &neg_zero : &pos_zero, sizeof(double)) == 0)) {

// op_1: #v.? = ADD #?.? [double,long], 0 => #v.? = QM_ASSIGN #?.?
// op_1: #v.? = ADD #?.? [long], 0 => #v.? = QM_ASSIGN #?.?
// op_1: #v.? = ADD #?.? [double], -0.0 => #v.? = QM_ASSIGN #?.?
// op_1: #v.? = SUB #?.? [double], 0.0 => #v.? = QM_ASSIGN #?.?

opline->opcode = ZEND_QM_ASSIGN;
opline->op2_type = IS_UNUSED;
Expand Down
76 changes: 76 additions & 0 deletions ext/opcache/tests/opt/add_sub_zero_signed_zero.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
--TEST--
DFA pass must not drop a float + 0.0 that turns -0.0 into 0.0
--EXTENSIONS--
opcache
--INI--
opcache.enable=1
opcache.enable_cli=1
opcache.optimization_level=-1
--FILE--
<?php

function add_int_zero(float $x) { return $x + 0; }
function add_zero_left(float $x) { return 0 + $x; }
function add_float_zero(float $x) { return $x + 0.0; }
function add_float_zero_left(float $x) { return 0.0 + $x; }
function sub_neg_zero(float $x) { return $x - -0.0; }
function round_add_zero($x) { return round($x, 2) + 0.0; }
function cast_add_zero($x) { return 0 + (float) $x; }

class C { public float $p = -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; }

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.

function sub_int_zero(float $x) { return $x - 0; }
function add_neg_zero(float $x) { return $x + -0.0; }
function add_neg_zero_left(float $x) { return -0.0 + $x; }
function int_add_zero(int $x) { return $x + 0; }

foreach ([
'add_int_zero', 'add_zero_left', 'add_float_zero', 'add_float_zero_left', 'sub_neg_zero',
'sub_zero', 'sub_int_zero', 'add_neg_zero', 'add_neg_zero_left',
] as $f) {
echo $f, ': ';
var_dump($f(-0.0), $f(0.0), $f(1.5));
}
echo "round_add_zero: "; var_dump(round_add_zero(-0.004));
echo "cast_add_zero: "; var_dump(cast_add_zero("-0"));
echo "prop_add_zero: "; var_dump(prop_add_zero(new C));
echo "int_add_zero: "; var_dump(int_add_zero(-3));
echo json_encode(round_add_zero(-0.004)), "\n";

?>
--EXPECT--
add_int_zero: float(0)
float(0)
float(1.5)
add_zero_left: float(0)
float(0)
float(1.5)
add_float_zero: float(0)
float(0)
float(1.5)
add_float_zero_left: float(0)
float(0)
float(1.5)
sub_neg_zero: float(0)
float(0)
float(1.5)
sub_zero: float(-0)
float(0)
float(1.5)
sub_int_zero: float(-0)
float(0)
float(1.5)
add_neg_zero: float(-0)
float(0)
float(1.5)
add_neg_zero_left: float(-0)
float(0)
float(1.5)
round_add_zero: float(0)
cast_add_zero: float(0)
prop_add_zero: float(0)
int_add_zero: int(-3)
0
114 changes: 114 additions & 0 deletions ext/opcache/tests/opt/add_sub_zero_signed_zero_opt.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
--TEST--
DFA pass must keep a float + 0.0 that can turn -0.0 into 0.0, and still drop the real identities
--INI--
opcache.enable=1
opcache.enable_cli=1
opcache.optimization_level=-1
opcache.opt_debug_level=0x20000
opcache.preload=
zend_test.observer.enabled=0
--EXTENSIONS--
opcache
--FILE--
<?php
/* x + 0.0 can turn -0.0 into 0.0, so these keep the ADD/SUB */
function add_int_zero(float $x) { return $x + 0; }
function add_zero_left(float $x) { return 0 + $x; }
function add_float_zero(float $x) { return $x + 0.0; }
function add_float_zero_left(float $x) { return 0.0 + $x; }
function sub_neg_zero(float $x) { return $x - -0.0; }

/* These give back x for every float and still become a copy */
function sub_zero(float $x) { return $x - 0.0; }
function sub_int_zero(float $x) { return $x - 0; }
function add_neg_zero(float $x) { return $x + -0.0; }
function add_neg_zero_left(float $x) { return -0.0 + $x; }
function int_add_zero(int $x) { return $x + 0; }
?>
--EXPECTF--

$_main:
; (lines=1, args=0, vars=0, tmps=0)
; (after optimizer)
; %sadd_sub_zero_signed_zero_opt.php:1-16
0000 RETURN int(1)

add_int_zero:
; (lines=3, args=1, vars=1, tmps=1)
; (after optimizer)
; %sadd_sub_zero_signed_zero_opt.php:3-3
0000 CV0($x) = RECV 1
0001 T1 = ADD CV0($x) float(0)
0002 RETURN T1

add_zero_left:
; (lines=3, args=1, vars=1, tmps=1)
; (after optimizer)
; %sadd_sub_zero_signed_zero_opt.php:4-4
0000 CV0($x) = RECV 1
0001 T1 = ADD CV0($x) float(0)
0002 RETURN T1

add_float_zero:
; (lines=3, args=1, vars=1, tmps=1)
; (after optimizer)
; %sadd_sub_zero_signed_zero_opt.php:5-5
0000 CV0($x) = RECV 1
0001 T1 = ADD CV0($x) float(0)
0002 RETURN T1

add_float_zero_left:
; (lines=3, args=1, vars=1, tmps=1)
; (after optimizer)
; %sadd_sub_zero_signed_zero_opt.php:6-6
0000 CV0($x) = RECV 1
0001 T1 = ADD CV0($x) float(0)
0002 RETURN T1

sub_neg_zero:
; (lines=3, args=1, vars=1, tmps=1)
; (after optimizer)
; %sadd_sub_zero_signed_zero_opt.php:7-7
0000 CV0($x) = RECV 1
0001 T1 = SUB CV0($x) float(-0)
0002 RETURN T1

sub_zero:
; (lines=3, args=1, vars=1, tmps=1)
; (after optimizer)
; %sadd_sub_zero_signed_zero_opt.php:10-10
0000 CV0($x) = RECV 1
0001 T1 = QM_ASSIGN CV0($x)
0002 RETURN T1

sub_int_zero:
; (lines=3, args=1, vars=1, tmps=1)
; (after optimizer)
; %sadd_sub_zero_signed_zero_opt.php:11-11
0000 CV0($x) = RECV 1
0001 T1 = QM_ASSIGN CV0($x)
0002 RETURN T1

add_neg_zero:
; (lines=3, args=1, vars=1, tmps=1)
; (after optimizer)
; %sadd_sub_zero_signed_zero_opt.php:12-12
0000 CV0($x) = RECV 1
0001 T1 = QM_ASSIGN CV0($x)
0002 RETURN T1

add_neg_zero_left:
; (lines=3, args=1, vars=1, tmps=1)
; (after optimizer)
; %sadd_sub_zero_signed_zero_opt.php:13-13
0000 CV0($x) = RECV 1
0001 T1 = QM_ASSIGN CV0($x)
0002 RETURN T1

int_add_zero:
; (lines=3, args=1, vars=1, tmps=1)
; (after optimizer)
; %sadd_sub_zero_signed_zero_opt.php:14-14
0000 CV0($x) = RECV 1
0001 T1 = QM_ASSIGN CV0($x)
0002 RETURN T1
Loading