LTO support

php.internals

zeriyoshi

4 years ago
At present, PHP cannot be built using LTO (Link Time Optimization). LTO is a compiler feature that can improve performance by optimizing at link time. Chromium is also using this feature. https://gcc.gnu.org/onlinedocs/gccint/LTO-Overview.html The reason for the LTO failure appears to be the Zend Engine's use of GCC's global register variables. Conversely, it is otherwise compatible with LTO. (except: opcache). I modified toolchain too to enable and validate LTOs outside of Zend Engine and OPcache: https://github.com/zeriyoshi/php-src/commit/b25c237837fec2f82d268d8dbd45ec886baf474f I tested the following compilation options: ``` gcc: (Debian 10.2.1-6) 10.2.1 20210110 CFLAGS="-fstack-protector-strong -fpic -fpie -O3 -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64" CXXFLAGS="${CFLAGS}" LDFLAGS="-Wl,-O1 -pie" $ ./sapi/cli/php ./Zend/bench.php simple 0.010 simplecall 0.005 simpleucall 0.010 simpleudcall 0.013 mandel 0.052 mandel2 0.049 ackermann(7) 0.012 ary(50000) 0.003 ary2(50000) 0.002 ary3(2000) 0.020 fibo(30) 0.035 hash1(50000) 0.004 hash2(500) 0.004 heapsort(20000) 0.012 matrix(20) 0.012 nestedloop(12) 0.010 sieve(30) 0.006 strcat(200000) 0.002 ------------------------ Total 0.261 ``` vs ``` gcc: (Debian 10.2.1-6) 10.2.1 20210110 CFLAGS="-fstack-protector-strong -fpic -fpie -O3 -flto -fuse-linker-plugin -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64" CXXFLAGS="${CFLAGS}" LDFLAGS="-Wl,-O1 -pie -flto -fuse-linker-plugin" $ ./sapi/cli/php ./Zend/bench.php simple 0.006 simplecall 0.003 simpleucall 0.010 simpleudcall 0.014 mandel 0.050 mandel2 0.049 ackermann(7) 0.012 ary(50000) 0.003 ary2(50000) 0.002 ary3(2000) 0.020 fibo(30) 0.035 hash1(50000) 0.004 hash2(500) 0.004 heapsort(20000) 0.012 matrix(20) 0.011 nestedloop(12) 0.010 sieve(30) 0.007 strcat(200000) 0.002 ------------------------ Total 0.255 ``` In fact, it does not seem to work very well. However, it may be effective when large numbers of extensions are built into PHP. Is anyone interested in supporting this?

Thomas Krüger

4 years ago
> In fact, it does not seem to work very well.
Hi Go Kudo, I read about the LTO feature from GCC in the past, and in my opinion, LTO works have many years of improvement in the GCC, it works pretty good nowadays. But programmers of Open Source projects were really clever the last twenty years. I read about bench tests where larger projects changes from normal compiling to -lto in GCC and after large benchmarking, they do not have much performance benefits from LTO. They said in these reports that mostly the bottleneck which -lto solves, were not existing in these projects: Developers for example move critical codes in the past from the .c-files into the .h-headers, so GCC can inline them without having such a feature like -lto. Or there use parts of "single-file" approaches for projects in C which have same compiling results as with the feature -lto. So, I think LTO works fine now, but you need projects which have these kind of bottlenecks to benefit from LTO. :) Best regards, Thomas ps: Sorry, for two e-mails, Go, I was not able to press the right answer button in my email program. ^^ Am 12.03.2022 19:17 schrieb Go Kudo: