Repository navigation
Fix GH-25744: DFA pass turning a float + 0.0 into a copy that keeps -0.0 #25608
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ChrisAbner
wants to merge
2
commits into
php:PHP-8.4
Choose a base branch
from
ChrisAbner:fix-dfa-add-zero-negative-zero
base: PHP-8.4
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; } | ||
| 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
114
ext/opcache/tests/opt/add_sub_zero_signed_zero_opt.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 toQM_ASSIGN. Could you add a companion test withopcache.opt_debug_level=0x20000to pin both sides?There was a problem hiding this comment.
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.phptwithopt_debug_level=0x20000. The folds that could turn-0.0into0.0keep theADD/SUB, and$x - 0.0,$x - 0,$x + -0.0and-0.0 + $xstill becomeQM_ASSIGN. It fails on PHP-8.4 without the fix and passes with it, with and without JIT.