Repository navigation
Fix optimizer turning $i++ < $n into ++$i <= $n for non-integer operands - #25606
Open
ChrisAbner wants to merge 1 commit into
Open
ChrisAbner wants to merge 1 commit into
ChrisAbner wants to merge 1 commit into
Conversation
The DFA pass rewrote $i++ < $n as ++$i <= $n (and $n < $i-- as $n <= --$i) whenever the counter was int or float, without looking at the other operand. The two forms are only equivalent for integers that do not overflow. With a float, numeric string, null or bool bound, or a float counter, the loop ran one iteration less. With $n = PHP_INT_MAX the optimized loop never ended. Only rewrite when the counter, the incremented value and the other operand are all int. Type inference already marks the incremented value as int|float when it may overflow.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
The DFA pass rewrites
$i++ < $nas++$i <= $n, and$n < $i--as$n <= --$i. It only checked that the result of the++/--is int or float. It did not look at the other operand of<. The two forms are only equivalent when both sides are integers and the increment does not overflow.With OPcache, without the JIT and at the default optimization level, this goes wrong in two ways. A loop with a non-integer bound runs one iteration less.
function half(array $a) { $n = count($a) / 2; $i = 0; $o = []; while ($i++ < $n) { $o[] = $a[$i - 1]; } return $o; }returns[1,2,3]for[1,2,3,4,5]without OPcache and[1,2]with it. The same happens with a numeric string,nullorboolbound, with a float counter, and with$n < $i--and a float$n. And on 64-bit,while ($i++ < PHP_INT_MAX)never ends.++$ioverflows to the float2^63, and2^63 <= PHP_INT_MAXis true when compared as doubles.The fix only rewrites when the counter, the incremented value and the other operand of
<can only be int. Type inference already gives the incremented valueint|floatwhen it may overflow (the/* may overflow */case ofPOST_INC/POST_DECinzend_inference.c), so checking it rules out the overflow. The other operand is read with_ssa_op2_info()/_ssa_op1_info(), which also covers constants. Checking only the bound was the other option. It fixes the fractional cases but keeps the endless loop. The cost is that$i++ < $nwith anint $nof unknown range is no longer rewritten. The rewrite only saves a copy to a temporary. In a release build,for ($i = 0; $i++ < $n;)with anint $nover 30M iterations took 0.086 s before and 0.085 s after without the JIT (median of 7 runs). A literal bound like$i++ < 3is still rewritten. This follows #25074, which also restricted a rewrite in the same function to the types where it is equivalent.Introduced by de78786 ("Optimize out result value of ASSIGN, ASSIGN_OP and INC/DEC opcodes, if possible"), first released in 8.0.1. Confirmed by bisect: its parent c2fbab3 gets all the cases below right, de78786 gets 9 of them wrong. Reproduced on the official images of 8.0.30, 8.1.34, 8.2.35, 8.3.36, 8.4.28, 8.5.13 and 8.6.0. The patch applies cleanly on PHP-8.5, PHP-8.6 and master.
Verification
New test
ext/opcache/tests/opt/inc_dec_is_smaller.phpt. Results with OPcache and without the JIT:half([1, 2, 3, 4, 5])[1,2,3][1,2][1,2,3]floatbound2.5"2.5"$x = 0.5; while ($x++ < 3)$min < $i--with$min = 0.5$i++ < $nwith$i = -1,$n = nullfalsetruefalse$i++ < $nwith$i = -1,$n = falsefalsetruefalsePHP_INT_MAX - 1up toPHP_INT_MAX(capped at 5)PHP_INT_MIN + 1down toPHP_INT_MIN(capped at 5)intbound, literal bound,int$minwith$i--The test fails without the fix and passes with it, without the JIT, with
opcache.jit=tracingand withopcache.jit=function. It also passes withopcache.file_cache_onlyand on a 32-bit build. On 32-bit the overflow cases already pass on the base, becausePHP_INT_MAXfits exactly in a double there. The other cases fail on the base.On PHP-8.4 (ab05b44),
Zend/testsruns with 0 failures without OPcache (4953 passed) and with it (4957 passed).ext/opcache/testshas 2 failures without the JIT (gh8739,gh17733) and 4 with tracing (the same two pluspreload_restart_hot_countersandpreload_restart_function_jit). They start a sub-process or restart OPcache, and they fail the same way on the base commit in my environment. On master (fc4f122),Zend/testsandext/opcache/testsrun with 0 failures, with OPcache and with tracing.Not covered:
$i++ < 3.0is no longer rewritten. It was correct, but a float literal bound is rare.block_pass.cturns!(a < b)intob <= a, which is the known NaN case (GH-15773), so I left it out.