Skip to content

php-in-php: JIT IteratorHelper — route foreach iterator protocol through VmIterator PHP not ~725-line LLVM (#1492) #10080

Description

@PurHur

Category

php-in-php · refs #1492

Problem

lib/JIT/IteratorHelper.php (~725 lines) implements foreach iterator protocol, by-ref foreach guards, object-key traversal, and hashtable index slots directly in LLVM. VM already walks arrays/objects/generators in PHP (lib/VM.php opcode handlers + iterator state).

Duplicate logic causes foreach drift (by-ref on iterators, IteratorAggregate, generator re-entry) between VM and JIT.

php-src reference

Repro

test/repro/foreach_jit_vm_agree.php:

<?php
declare(strict_types=1);

class Agg implements IteratorAggregate {
    public function getIterator(): Generator {
        yield 'k' => 'v';
    }
}

$out = [];
foreach (new Agg() as $k => $v) {
    $out[] = "$k=$v";
}
echo implode(',', $out), "\n";

$g = (function (): Generator { yield 1; yield 2; })();
$sum = 0;
foreach ($g as $n) { $sum += $n; }
echo "sum=$sum\n";
./script/docker-exec.sh -- bash -lc 'source script/php-env.sh
php bin/vm.php test/repro/foreach_jit_vm_agree.php
php bin/jit.php test/repro/foreach_jit_vm_agree.php'

Expected both: k=v and sum=3. JIT mismatch = bug.

Scope (PHP-in-PHP first)

Path Work
New/existing lib/VM/IteratorSupport.php Shared FE_FETCH semantics for VM + JIT callback
lib/JIT/IteratorHelper.php Trampoline to compiled PHP helper; delete per-opcode LLVM
lib/JIT/IteratorProtocolHelper.php Fold into PHP path where duplicated
Tests test/compliance/cases/language/foreach_jit_vm_agree.phpt

Done when

Verification

./script/docker-exec.sh -- bash -lc 'source script/php-env.sh && vendor/bin/phpunit --filter ForeachJit'

Related

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:compilerCompiler / CFG / JITenhancementNew feature or requestimplementation-readySpec complete: repro, php-src ref, done-when — safe for workers to claimphase-0:FoundationPhase 0 – foundation & DevExphase-2:languagePhase 2 – language features

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions