Skip to content

Converge nested loops from their previous loop heads in enclosing loops' passes - #7171

Merged
ondrejmirtes merged 3 commits into
2.3.xfrom
nested-loop-warm-start
Nov 26, 2026
Merged

ondrejmirtes merged 3 commits into
2.3.xfrom
nested-loop-warm-start

Conversation

@ondrejmirtes

@ondrejmirtes ondrejmirtes commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

Loops nested in a top-level loop were walked once per enclosing pass, so a counter an inner loop increments stayed 0|1 and conditions depending on it never became true in the outer loop's passes.

Every loop now converges whenever it is walked, also inside an enclosing loop's passes (Bourdoncle's recursive iteration strategy, as used by IKOS and Astrée). To avoid #6820's cost, the head each nested loop converged to is kept between the enclosing loop's passes (ConvergedLoopHeads) and joined into its entry the next time, so a nested loop whose head is already stable converges in a single pass, and that pass stands in for its final walk. Top-level walks still converge from their own entry.

Analysis time on generated nested-loop cases (seconds, single runs):

case 2.3.x #6820 this PR
heavy-depth3 1.02 2.77 1.79
heavy-depth4 1.32 3.98 3.02
heavy-depth5 1.69 5.57 4.56
wide8 0.78 1.85 1.44
foreach-depth6 0.39 0.64 0.59
mixed-depth6 0.50 0.78 0.54

Converging nested loops inside the enclosing loop's passes exposed a widening bug that 2.3.x already has at top level (integration tests failed for shopware, matomo, glpi, openemr, PMMP, phpstan-dba and phpstan-laravel). Widening a variable between loop passes dropped every tracked expression on it, such as $byHash[$hash] after $byHash[$hash]['list'][] = $x. The next pass then no longer knew the offset exists, the write could start a new array{list: ...}, and array{block: Block, list: list<X>} collapsed to array<'block'|'list', Block|list<X>> with false "Cannot access an offset on Block|list". An expression both passes track is now widened like any other (separate commit; test nsrt/loop-generalize-nested-offset.php fails on 2.3.x). The issue bot found two more snippets this fixes: phpstan/phpstan#14598 (false "Offset 'A'|'B'|'C' might not exist") and phpstan/phpstan#12033, where the loop-built $this->cache[$key] was array<...>|(ArrayAccess&PDOException) and is now array<int<0, max>, ...>. That removes the "PDOException is not a list" and "null is not a list" tips the issue asks about, and the false "Cannot access offset int<0, max> on ...|PDOException" on line 51. The errors that remain are correct: flags is array<int, string> where list<string> is expected.

turbo-ext mirrors the change (native ConvergedLoopHeads, StatementContext slot and methods, the four loop handlers); smoke, signature parity, side-by-side, walk-trace and raw output identity pass.

Closes phpstan/phpstan#15435
Closes phpstan/phpstan#15420
Closes phpstan/phpstan#10666
Closes phpstan/phpstan#14935
Closes phpstan/phpstan#14598
Closes phpstan/phpstan#12033

…ps' passes

Loops nested in a top-level loop were walked only once in each of the
enclosing loop's convergence passes, so a counter incremented by an inner
loop stayed 0|1 and conditions depending on it never became true.

Every loop now converges whenever it is walked, also inside an enclosing
loop's passes (Bourdoncle's recursive iteration strategy, as used by IKOS
and Astrée). To keep the cost close to walking them once, the head a
nested loop converged to is kept between the enclosing loop's passes
(ConvergedLoopHeads) and joined into its entry the next time, so a nested
loop whose head is already stable converges in a single pass. That pass
also stands in for the nested loop's final walk.

- StatementContext: shouldConvergeLoops(), getLoopHeads(),
  enterLoopPass() and withLoopHeads(). Only pass walks record and read
  heads; top-level walks converge from their own entry.
- WhileHandler, DoWhileHandler, ForHandler, ForeachHandler: converge when
  shouldConvergeLoops(), start nested loops from warmStart(), record the
  converged head. Foreach unrolling over constant arrays stays top-level.
- turbo-ext: native ConvergedLoopHeads, the StatementContext slot,
  methods and direct entries, and the four loop handlers.
- bug-10438: the expectation recorded the same bug; an inner foreach
  appends any number of strings, so list<string> is correct.

Amp-Thread-ID: https://ampcode.com/threads/T-01a17eac-1199-74a6-b3f7-dfbd8a084465
- ConvergedLoopHeads caches the warm entry it derived per loop and keeps
  the recorded head object when the new head equals it, so an enclosing
  loop's later passes bringing the same entry get the warm entry without
  joining it again.
- A warm-started loop begins its convergence at the warm entry, skipping
  the first join of the initial body scope into it.
- A loop whose joined entry equals the previous pass's keeps that entry
  instead of recomputing the same join after the convergence loop.
- turbo-ext: the same in ConvergedLoopHeads and the four loop handlers.

Amp-Thread-ID: https://ampcode.com/threads/T-01a17eac-1199-74a6-b3f7-dfbd8a084465
@ondrejmirtes
ondrejmirtes force-pushed the nested-loop-warm-start branch from c3fb17b to e9aecea Compare November 25, 2026 22:30
…track

Widening a variable between loop passes dropped every tracked expression on
it, such as $byHash[$hash] after $byHash[$hash]['list'][] = $x widened
$byHash. The next pass then no longer knew the offset exists, so the write
could start a new array{list: ...} and the shape lost its other keys:

    array{block: Block, list: list<X>} -> array<'block'|'list', Block|list<X>>

with false "Cannot access an offset on Block|list<X>" after it. Nested loops
converging in the enclosing loop's passes hit this in shopware, matomo,
glpi, openemr, pmmp, phpstan-dba and phpstan-laravel.

An expression both passes track is now widened like any other; only one
that the other pass does not track is dropped, as before.

- bug-10438: $meta[$key] after $meta[$key] = [] is list<string> inside the
  inner loop, no longer list<string>|string.
- bug-14598: $tmp[$id] = [] before the inner loop is no longer reported as
  possibly missing after it.
- bug-12033: $this->cache[$key] built in a loop is no longer
  array<...>|(ArrayAccess&PDOException).
- turbo-ext: the same in MutatingScope.cpp.

Amp-Thread-ID: https://ampcode.com/threads/T-01a17eac-1199-74a6-b3f7-dfbd8a084465
@ondrejmirtes
ondrejmirtes force-pushed the nested-loop-warm-start branch from e9aecea to 8e909f7 Compare November 26, 2026 05:28
@ondrejmirtes
ondrejmirtes merged commit 40620d1 into 2.3.x Nov 26, 2026
139 of 147 checks passed
@ondrejmirtes
ondrejmirtes deleted the nested-loop-warm-start branch November 26, 2026 05:31
ondrejmirtes added a commit that referenced this pull request Nov 26, 2026
From https://github.com/phpstan/phpstan-src/actions/runs/45762120236, the
push run of 40620d1 (#7171). Nested loops now converge
inside their enclosing loop's passes, so the analysis benchmarks with
nested loops and the NodeScopeResolver nested-foreach/nested-while cases
do more work.

Amp-Thread-ID: https://ampcode.com/threads/T-01a21657-7f83-77f3-b406-d5c68df62f37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant