[php-src] PR #18720: Tail call VM [2]
| From: | arnaud-lb | Date: | Sat, 31 May 2025 12:24:38 +0000 |
| Subject: | [php-src] PR #18720: Tail call VM [2] | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-31897@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/18720
Author: arnaud-lb
Related:
* This is the second part of https://github.com/php/php-src/pull/17849
* First part was https://github.com/php/php-src/pull/17952
This part takes tail-calling and
preserve_none from https://github.com/php/php-src/pull/17849:
* Opcode handlers dispatch directly to the next handler by tail-calling, which reduces function
call overhead and avoids returning to the executor loop
* preserve_none reduces register saving overhead in opcode handlers
This also implements JIT support.
#### Non-dispatching opcode handlers
JIT needs non-dispatching opcode handlers (opcode handlers that return instead of calling the next
one). I've tried two approaches for this:
* Generate a second, non-dispatching, variant of each handler. Use the variant as
call_handler
* Use indirect dispatch similarly to the hybrid VM: zend_op->handler is a function
that calls the real handler and dispatches.
I've tried both approach (the first one in this branch, and the second one in https://github.com/php/php-src/compare/master...arnaud-lb:php-src:hybrid-tailcall.
The second approach resulted in a slightly slower VM due to indirect dispatching, and JIT generated
more spilling when calling handlers as they are preserve_none functions.
Therefore I've taken the first approach in this PR.
A 3rd approach would be to control dispatching via an additional handler parameter, or to pass a
dispatch function to handlers, but I suspect this would have been slower.
#### Fixed regs and preserved regs
Thanks to the preserve_none convention, JIT'ed code only has to preserve
rbp, which reduces the size of prologue/epilogue. Instead of preserving it, I add it to
the set of fixed registers, so it's not used. This results in faster code.
Also, quite conveniently, preserve_none receives its first arguments via registers that
are callee-saved in sysv. Therefore we can use the arg1 and arg2 regs as our fixed registers. This
avoids moving arg1 and arg2 to SP/IP in prologue, or setting arg1/arg2 when tail-calling other
handlers.
### Benchmarks:
| Benchmark | Mode | vs base | vs gcc | vs valgrind |
|---------------|----------|---------|--------|-------------|
| bench.php | JIT | -4% | -0% | -5% |
| bench.php | Non-JIT | -44% | +3% | |
| symfony demo | JIT | -0% | -0% | |
| symfony demo | Non-JIT | -2.8% | -0% | |
base: Clang build of master
gcc: GCC build of master
valgrind: Clang build of master, valgrind instructions
Conclusion: Clang builds are now as fast GCC builds on the Symfony Demo benchmark in both JIT and
non-JIT modes.
### Issues
The preserve_none calling convention is documented as unstable. JIT would break if it
changed. I suggest checking this at build time, and disabling this optimization if
preserve_none changed.
### TODO
* [ ] x86 (disable, probably)
* [ ] aarch64
* [ ] IR PRs
* [ ] preserve_none configure check