[php-src] PR #17952: Pass opline as argument to opcode handlers in CALL VM
| From: | arnaud-lb | Date: | Fri, 28 Feb 2025 13:25:57 +0000 |
| Subject: | [php-src] PR #17952: Pass opline as argument to opcode handlers in CALL VM | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-30941@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/17952
Author: arnaud-lb
Related:
* https://github.com/php/php-src/pull/17849
This extracts the part of #17849 that passes
opline as opcode handler argument in the
Call VM. This should reduce the size of #17849, and also unify the Hybrid and Call VMs slightly.
Currently we have two VMs:
* Hybrid: Used when compiling with GCC, execute_data and opline are
global register variables
* Call: Used when compiling with something else, execute_data is passed as opcode
handler arg, and opline is loaded/stored into execute_data->opline.
The Call VM looks like this:
``` c
while (1) {
ret = opline->handler(execute_data);
if (UNEXPECTED(ret != 0)) {
if (ret > 0) { // returned by ZEND_VM_ENTER() / ZEND_VM_LEAVE()
execute_data = EG(current_execute_data);
} else { // returned by ZEND_VM_RETURN()
return;
}
}
}
// example op handler
int ZEND_INIT_FCALL_SPEC_CONST_HANDLER(zend_execute_data *execute_data) {
// load opline
const zend_op *opline = execute_data->opline;
// instruction execution
// dispatch
// ZEND_VM_NEXT_OPCODE():
execute_data->opline++;
return 0; // ZEND_VM_CONTINUE()
}
```
### Changes in this PR
* Pass opline as opcode handler argument
* Return opline from opcode handlers
* ZEND_VM_ENTER / ZEND_VM_LEAVE return opline | (1<<63) to signal that
execute_data must be reloaded from EG(current_execute_data)
This gives us:
``` c
while (1) {
opline = opline->handler(execute_data, opline);
if (UNEXPECTED((intptr_t) opline <= 0) {
if (opline != 0) { // returned by ZEND_VM_ENTER() / ZEND_VM_LEAVE()
opline = opline & ~(1<<63);
execute_data = EG(current_execute_data);
} else { // returned by ZEND_VM_RETURN()
return;
}
}
}
// example op handler
const zend_op * ZEND_INIT_FCALL_SPEC_CONST_HANDLER(zend_execute_data *execute_data, const zend_op
*opline) {
// opline already loaded
// instruction execution
// dispatch
// ZEND_VM_NEXT_OPCODE():
return ++opline;
}
```
In addition to making the changes of #17849 smaller and to unifying VMs slightly, this improve
performances of the Call VM:
bench.php is 23% faster:
```
; hyperfine -L php no-opline,opline-arg '/tmp/{php}/sapi/cli/php -n -d
zend_extension=opcache.so -d opcache.enable_cli=1 --repeat 3 Zend/bench.php'
Benchmark 1: /tmp/no-opline/sapi/cli/php -n -d zend_extension=opcache.so -d opcache.enable_cli=1
--repeat 3 Zend/bench.php
Time (mean ± σ): 549.4 ms ± 4.7 ms [User: 536.3 ms, System: 11.8 ms]
Range (min … max): 542.2 ms … 558.6 ms 10 runs
Benchmark 2: /tmp/opline-arg/sapi/cli/php -n -d zend_extension=opcache.so -d opcache.enable_cli=1
--repeat 3 Zend/bench.php
Time (mean ± σ): 445.5 ms ± 2.7 ms [User: 432.5 ms, System: 12.0 ms]
Range (min … max): 441.3 ms … 449.0 ms 10 runs
Summary
/tmp/opline-arg/sapi/cli/php -n -d zend_extension=opcache.so -d opcache.enable_cli=1 --repeat 3
Zend/bench.php ran
1.23 ± 0.01 times faster than /tmp/no-opline/sapi/cli/php -n -d zend_extension=opcache.so -d
opcache.enable_cli=1 --repeat 3 Zend/bench.php
```
Symfony Demo is 2.8% faster:
```
base: mean: 0.5373; stddev: 0.0004; diff: -0.00%
opline-arg: mean: 0.5222; stddev: 0.0008; diff: -2.82%
```
### JIT
When using the Hybrid VM, JIT stores execute_data/opline in callee-saved registers and rarely
touches EX(opline), just like the VM.
Since the registers are callee-saved, the JIT'ed code doesn't have to save them before
calling other functions, and can assume they always contain execute_data/opline. The code also
avoids saving/restoring them in prologue/epilogue, as execute_ex takes care of that
(JIT'ed code is called exclusively from there).
When using the Call VM, we can do that, too, except that we can't rely on
execute_ex to save the registers for us, as it may use these registers it self. So we
have to save/restore the two registers in JIT'ed code prologue/epilogue.
### TODO
* [ ] Test x86
* [ ] Test aarch64