RFC: PHP JIT/arm64 port

php.internals

Hao Sun

5 years ago
Hi Internals, Currently PHP JIT only supports x86 and x86_64 CPUs on POSIX platforms and Windows.[1] With the prevalence of PHP language and the notable growth of ARM-based servers market, we believe JIT/arm64 would be in urgent need in the near future. As an initial effort to enable PHP JIT/arm64, we (ARM) have supported the basic functionality, and (partially) implemented the compilation for several opcodes. Currently a number of simple JIT test cases from PHP test framework can be passed on ARM-based machine. There are still a lot of missing parts, such as hot loops, class/object/array operations, exception handling, etc, and we will continue working on them. We would like to share our work with you (See the draft patch https://github.com/shqking/php-src/commit/6aaf935). Any feedback would be greatly appreciated, and please let we know if anyone wants to contribute to this port. Thanks, Hao SUN Email: hao.sun@arm.com<mailto:hao.sun@arm.com> ------------------------------------------------------- Main updates: 1. JIT backend for AArch64 A new alternative, i.e. AArch64, was added while building PHP JIT. See the updates in the following files. Note that we adopt capstone[2] for disassembly on AArch64. build/Makefile.global ext/opcache/config.m4 ext/opcache/config.w32 ext/opcache/jit/Makefile.frag ext/opcache/jit/zend_jit.c ext/opcache/jit/zend_jit_vm_helpers.c ext/opcache/jit/zend_jit_disasm_arm64.c ext/opcache/jit/zend_jit_gdb.c ext/opcache/jit/zend_jit_perf_dump.c 2. DynASM library PHP JIT uses DynASM[3] (developed for LuaJIT project) to generate native code on the fly. We added two useful but missing features, global label reference and dynamic register names, into DynASM/arm64. See the updates in files: ext/opcache/jit/dynasm/dasm_arm64.h ext/opcache/jit/dynasm/dasm_arm64.lua Note that these two features are available on DynASM/x86. 3. compilation for opcodes on AArch64 Our main work falls in the following files. ext/opcache/jit/zend_jit_arm64.h ext/opcache/jit/zend_jit_arm64.dasc ext/opcache/jit/zend_jit_internal.h Zend/zend_vm_opcodes.h * AArch64 registers and calling conventions are defined. * Instruction cache must be flushed for the JIT-ed code on AArch64. See macro JIT_CACHE_FLUSH in file 'zend_jit_internal.h'. * We have (partially) implemented the compilation for several opcodes, mainly for the function-based JIT (with opcache.jit=1203). Currently, test cases involving internal function call (e.g. var_dump), additions with integers/floating-point numbers, integer overflows and simple exception, can be supported now. See our newly added test cases under directory 'ext/opcache/tests/jit/arm64/'. * Trace counter stubs are implemented for tracing JIT (with opcache.jit=1255). See zend_jit_hybrid_trace_counter_stub() and zend_jit_hybrid_hot_trace_stub() in file 'zend_jit_arm64.dasc'. Hot functions can be recognized and compiled successfully. See the test case 'hot_func_002.phpt'. How to build and test: Our local test environment is an ARM-based server with Ubuntu 20.04 and GCC-10. We follow the building commands as shown in the readme file [4]. Note that library capstone should be installed in advance. We suggest running the JIT test cases using the following command. In our local test, 59 out of all 128 cases can be passed currently. $ make test TESTS='-d opcache.jit=1203 ext/opcache/tests/jit/' [1] https://wiki.php.net/rfc/jit [2] https://www.capstone-engine.org/ [3] https://luajit.org/dynasm.html [4] https://github.com/php/php-src

Calvin Buckley

5 years ago
This is great work! I assume you've only tested much on Linux, but I do have an M1 Mac here to test. I'm curious how much effort this took you, in terms of figuring out the CPU-specific parts of the JIT, and what needed untangling from machine- specific code and what didn't. I've been curious what it'd take to write another JIT backend (for a supported CPU in DynASM). I noticed the changes you've made to generic parts of the JIT were minimal; only improvements that'd be optional to dasm, minor detanglement in generic code/the build system, and adding Capstone. The current state of the JIT being x86 only made it hard to determine what was specific to x86 and what wasn't. The ARM backend here also seems pretty small compared to the x86 one, though that might just be lack of feature parity. (Regardless, better JIT docs would be great!) On Wed, 2021-03-17 at 03:27 +0000, Hao Sun wrote:

Hao Sun

5 years ago
Thanks for your reply and your interest. Yes. Only Linux platform is tested on my side. I spent about two months for this initial task. To be honest, I haven't had a very clear overview of this CPU-specific JIT parts so far, but I simply added the arm64 supports where necessary to make JIT/arm64 runnable. Yes. Agree with you that JIT docs would be very helpful. Yes your are right. Currently this JIT/arm64 is much smaller than JIT/x86. As mentioned in my previous email, only very simple functionalities are (partially) supported. I'm now working on more features. Thanks again for your comments. Hao -----Original Message----- From: Calvin Buckley <calvin@cmpct.info> Sent: Thursday, March 18, 2021 4:18 AM To: Hao Sun <Hao.Sun@arm.com>; internals@lists.php.net Cc: nd <nd@arm.com> Subject: Re: [PHP-DEV] RFC: PHP JIT/arm64 port This is great work! I assume you've only tested much on Linux, but I do have an M1 Mac here to test. I'm curious how much effort this took you, in terms of figuring out the CPU-specific parts of the JIT, and what needed untangling from machine- specific code and what didn't. I've been curious what it'd take to write another JIT backend (for a supported CPU in DynASM). I noticed the changes you've made to generic parts of the JIT were minimal; only improvements that'd be optional to dasm, minor detanglement in generic code/the build system, and adding Capstone. The current state of the JIT being x86 only made it hard to determine what was specific to x86 and what wasn't. The ARM backend here also seems pretty small compared to the x86 one, though that might just be lack of feature parity. (Regardless, better JIT docs would be great!) On Wed, 2021-03-17 at 03:27 +0000, Hao Sun wrote:

Dmitry Stogov

5 years ago
Hi Hao, I'm the author of JIT for PHP. Please CC me next time, I read @internals only from time to time... I'll try to take a look into your implementation next week. I'm not sure how much time I'll be able to invest yet, I'll have to discuss this with my management. Thanks. Dmitry. On Wed, Mar 17, 2021 at 6:28 AM Hao Sun <Hao.Sun@arm.com> wrote:

Hao Sun

5 years ago
Hi Dmitry, Thanks for your reply. Yes. Sure. I will CC you next time. As I mentioned in the previous email, this is our (ARM) initial effort to enable JIT/arm64 port and there might exist some potential bugs in implementation or something we haven’t considered/designed soundly. Please let us know if you have any question. Thanks, Hao From: Dmitry Stogov <dmitrystogov@gmail.com> Sent: Friday, March 19, 2021 9:35 PM To: Hao Sun <Hao.Sun@arm.com> Cc: internals@lists.php.net; nd <nd@arm.com> Subject: Re: [PHP-DEV] RFC: PHP JIT/arm64 port Hi Hao, I'm the author of JIT for PHP. Please CC me next time, I read @internals only from time to time... I'll try to take a look into your implementation next week. I'm not sure how much time I'll be able to invest yet, I'll have to discuss this with my management. Thanks. Dmitry. On Wed, Mar 17, 2021 at 6:28 AM Hao Sun <Hao.Sun@arm.com<mailto:Hao.Sun@arm.com>> wrote: Hi Internals, Currently PHP JIT only supports x86 and x86_64 CPUs on POSIX platforms and Windows.[1] With the prevalence of PHP language and the notable growth of ARM-based servers market, we believe JIT/arm64 would be in urgent need in the near future. As an initial effort to enable PHP JIT/arm64, we (ARM) have supported the basic functionality, and (partially) implemented the compilation for several opcodes. Currently a number of simple JIT test cases from PHP test framework can be passed on ARM-based machine. There are still a lot of missing parts, such as hot loops, class/object/array operations, exception handling, etc, and we will continue working on them. We would like to share our work with you (See the draft patch https://github.com/shqking/php-src/commit/6aaf935). Any feedback would be greatly appreciated, and please let we know if anyone wants to contribute to this port. Thanks, Hao SUN Email: hao.sun@arm.com<mailto:hao.sun@arm.com><mailto:hao.sun@arm.com<mailto:hao.sun@arm.com>> ------------------------------------------------------- Main updates: 1. JIT backend for AArch64 A new alternative, i.e. AArch64, was added while building PHP JIT. See the updates in the following files. Note that we adopt capstone[2] for disassembly on AArch64. build/Makefile.global ext/opcache/config.m4 ext/opcache/config.w32 ext/opcache/jit/Makefile.frag ext/opcache/jit/zend_jit.c ext/opcache/jit/zend_jit_vm_helpers.c ext/opcache/jit/zend_jit_disasm_arm64.c ext/opcache/jit/zend_jit_gdb.c ext/opcache/jit/zend_jit_perf_dump.c 2. DynASM library PHP JIT uses DynASM[3] (developed for LuaJIT project) to generate native code on the fly. We added two useful but missing features, global label reference and dynamic register names, into DynASM/arm64. See the updates in files: ext/opcache/jit/dynasm/dasm_arm64.h ext/opcache/jit/dynasm/dasm_arm64.lua Note that these two features are available on DynASM/x86. 3. compilation for opcodes on AArch64 Our main work falls in the following files. ext/opcache/jit/zend_jit_arm64.h ext/opcache/jit/zend_jit_arm64.dasc ext/opcache/jit/zend_jit_internal.h Zend/zend_vm_opcodes.h * AArch64 registers and calling conventions are defined. * Instruction cache must be flushed for the JIT-ed code on AArch64. See macro JIT_CACHE_FLUSH in file 'zend_jit_internal.h'. * We have (partially) implemented the compilation for several opcodes, mainly for the function-based JIT (with opcache.jit=1203). Currently, test cases involving internal function call (e.g. var_dump), additions with integers/floating-point numbers, integer overflows and simple exception, can be supported now. See our newly added test cases under directory 'ext/opcache/tests/jit/arm64/'. * Trace counter stubs are implemented for tracing JIT (with opcache.jit=1255). See zend_jit_hybrid_trace_counter_stub() and zend_jit_hybrid_hot_trace_stub() in file 'zend_jit_arm64.dasc'. Hot functions can be recognized and compiled successfully. See the test case 'hot_func_002.phpt'. How to build and test: Our local test environment is an ARM-based server with Ubuntu 20.04 and GCC-10. We follow the building commands as shown in the readme file [4]. Note that library capstone should be installed in advance. We suggest running the JIT test cases using the following command. In our local test, 59 out of all 128 cases can be passed currently. $ make test TESTS='-d opcache.jit=1203 ext/opcache/tests/jit/' [1] https://wiki.php.net/rfc/jit [2] https://www.capstone-engine.org/ [3] https://luajit.org/dynasm.html [4] https://github.com/php/php-src

Dmitry Stogov

5 years ago
Yeah, I suppose this is going to be a big task and we will have to collaborate a lot. anyway, next week... It's already Friday evening. Thanks. Dmitry. On Fri, Mar 19, 2021 at 5:20 PM Hao Sun <Hao.Sun@arm.com> wrote: