Repository navigation
Tail call VM [2] - #18720
Tail call VM [2]#18720arnaud-lb wants to merge 24 commits into
Conversation
There was a problem hiding this comment.
Tail-calling was not possible due to the extra arg in zend_jit_trace_counter_helper, so I removed it.
Alternative would be to define these handlers in IR, as we do for the hybrid JIT.
There was a problem hiding this comment.
I didn't understand why extra_arg became a problem
There was a problem hiding this comment.
Clang ˋmusttailˋ disallows calling functions with a different signature. Presumably this is to garantee portability. This is discussed here: https://blog.reverberate.org/2025/02/10/tail-call-updates.html
dstogov
left a comment
There was a problem hiding this comment.
The idea makes sense and the benchmark results look promising.
It's also interesting how it affects the VM code size.
Unfortunately at this moment I can't review a huge path like this in all details.
Anyway, I think it makes sense to finalize and land it.
There was a problem hiding this comment.
fixed_regs are the registers reserved for the register variables.
I remember there were some problems with RBP usage, but I don't remember the exact problem.
There was a problem hiding this comment.
In this case I add the register only to prevent the RA from using it. This is less expensive than adding it to the set of preserved registers. The same thing is done for the HYBRID VM a few lines below.
There was a problem hiding this comment.
I didn't understand why extra_arg became a problem
c8cb9d5 to
e334ae4
Compare
|
@dstogov I've addressed all issues, and all necessary IR changes have been merged, so this branch is now ready to be reviewed again, and hopefully merged :) Since your last review I've made the following notable change: As adding |
There was a problem hiding this comment.
Repeatable redefinition of the same macro looks very weird.
Can't we redefine it once - between generating code for TAIL_CALL and regular CALL handlers?
There was a problem hiding this comment.
We need this before VM helpers that take extra arguments, because __attribute__((musttail)) doesn't allow them to tailcall to helpers or handlers with a different number of args.
Currently, VM helpers with extra arguments are interleaved with normal VM helpers and opcode handlers.
Avoiding the repeated redefinition would require to group VM helpers with extra args together, however this may change code locality.
WDYT?
There was a problem hiding this comment.
I think that all TAIL_CALL handlers and helpers should be generated after/before all CALL handlers and helpers.
There was a problem hiding this comment.
I've made changes so that we generate handlers in this order:
- CALL handlers
- TAILCALL handlers and helpers without extra args
- TAILCALL helpers with extra args
The undef/define is used only once before the last group.
I feared this would affect the location of these function in memory, but it doesn't, and doesn't affect benchmarks.
Was a left over of relying on IR for preserve_none support
This reverts commit f0d41731921711cddea8dcc1d56ed16af5c2098b.
…n is not necessary anymore
dstogov
left a comment
There was a problem hiding this comment.
Technically everything is fine.
See ideas about cleaning and reduction of amount of changes.
dstogov
left a comment
There was a problem hiding this comment.
I don't have objections anymore
|
@arnaud-lb you've worked magic on this one. Since LTO works flawlessly without global register variables, Clang (21) actually consistently beats GCC (14) now, for Great job! |
|
@henderkes Awesome! I hadn't tested with LTO yet, although CPython reported great results with LTO as well. Could you describe how you compile PHP with LTO? |
|
Sure, I'm maintaining https://github.com/crazywhalecc/static-php-cli and https://github.com/static-php/spc-packages and switched over to zig cc (which bundles a platform agnostic cross-compiling Clang 21) a while ago, until I noticed that it produced much slower binaries than gcc. The compilation logic can be found in https://github.com/crazywhalecc/static-php-cli, but in a nutshell, we compile static .a libraries and then later use them to compile php and extensions. There isn't really anything special going on for -flto, as it works just fine when global register variables are not used. The exact CFLAGS/LDFLAGS used with zig are: With gcc we add Currently, results are as follows: gcc 8.4.13 NTS (system package, uses -O2, otherwise same flags) gcc 8.5.dev ZTS: zig 8.5.dev ZTS It's 1010-1020k without LTO. I really wanted to benchmark with gcc 15 as well, but the homebrew/linuxbrew version is broken and redhat hasn't published a gcc-toolset yet. Some dependent libraries also don't compiled under -std=gnu23 yet. |
Related:
This part takes tail-calling and
preserve_nonefrom #17849:preserve_nonereduces register saving overhead in opcode handlersThis 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:
call_handlerzend_op->handleris 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 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 clobber all registers.
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_noneconvention, JIT'ed code only has to preserverbp, 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_nonereceives 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:
base: Clang build of master, wall time
gcc: GCC build of master, wall time
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
preserve_nonecalling convention is documented as unstable. JIT would break if it changed. I suggest checking this at build time, and disabling this optimization (tailcalling + preserve_none) ifpreserve_nonechanged. Edit: added a configure check.preserve_noneand ASAN, therefore we disable this if ASAN is enabled. Edit: This seems to be fixed in recent Clang versions.UPGRADING
TODO