Skip to content
A compiler. For PHP
Branch: master
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
benchmarks get bounds checking working completely, implement 'baisc' error handling Mar 17, 2019
bin A bunch of refactoring, much better extension support Mar 11, 2019
ext Finish refactoring, support is_int/is_float/etc in both modes Mar 11, 2019
lib get bounds checking working completely, implement 'baisc' error handling Mar 17, 2019
php Get basic native arrays working Mar 16, 2019
src A bunch of benchmark tweaks Mar 7, 2019
test Get basic native arrays working Mar 16, 2019
.gitignore
.php_cs Update readme a bit more Mar 4, 2019
CODE_OF_CONDUCT.md
CONTRIBUTING.md Adding missing CONTRIBUTING.md, fixing LICENSE and others (#7) Mar 9, 2019
LICENSE Adding missing CONTRIBUTING.md, fixing LICENSE and others (#7) Mar 9, 2019
README.md
bench.php
build.sh Update readme a bit more Mar 4, 2019
composer.json
composer.lock
demo.debug.c
demo.php
demo.reproduce.c
phpunit.xml.dist Update dependencies, require php 7.4+ and ffi, and introduce php\Mask… Mar 15, 2019

README.md

A compiler for PHP

Ok, so this used to be a dead project. It required calling out to all sorts of hackery to generate PHP extensions, or PHP itself.

Now, thanks to FFI landing in PHP 7.4, the potential for all sorts of crazy is HUGE.

So here we go :)

Installation

Install PHP 7.4, being sure to enable the FFI extension (--with-ffi).

Also, you need to install the system dependency libgccjit. On Ubuntu:

me@local:~$ sudo apt-get install libgccjit-6-dev

Then simply composer install.

Running Code

There are three main ways of using this compiler:

VM - Virtual Machine

This compiler mode implements its own PHP Virtual Machine, just like PHP does. This is effectively a giant switch statement in a loop.

No, seriously. It's literally a giant switch statement...

Practically, it's a REALLY slow way to run your PHP code. Well, it's slow because it's in PHP, and PHP is already running on top of a VM written in C.

But what if we could change that...

JIT - Just In Time

This compiler mode takes PHP code and generates machine code out of it. Then, instead of letting the code run in the VM above, it just calls to the machine code.

It's WAY faster to run (faster than PHP 7.4, when you don't account for compile time).

But it also takes a long time to compile (compiling is SLOW, because it's being compiled from PHP).

Every time you run it, it compiles again.

That brings us to our final mode:

Compile - Ahead Of Time Compilation

This compiler mode actually generates native machine code, and outputs it to an executable.

This means, that you can take PHP code, and generate a standalone binary. One that's implemented without a VM. That means it's (in theory at least) as fast as native C.

Well, that's not true. But it's pretty dang fast.

Okay, Enough, How can I try?

There are four CLI entrypoints, and all 4 behave (somewhat) like the PHP cli:

  • php bin/vm.php - Run code in a VM
  • php bin/jit.php - Compile all code, and then run it
  • php bin/compile.php - Compile all code, and output a .o file.
  • php bin/print.php - Compile and output CFG and the generated OpCodes (useful for debugging)

Executing Code

Specifying code from STDIN (this works for all 4 entrypoints):

me@local:~$ echo '<?php echo "Hello World\n";' | php bin/vm.php
Hello World

You can also specify on the CLI via -r argument:

me@local:~$ php bin/jit.php -r 'echo "Hello World\n";'
Hello World

And you can specify a file:

me@local:~$ echo '<?php echo "Hello World\n";' > test.php
me@local:~$ php bin/vm.php test.php

When compiling using bin/compile.php, you can also specify an "output file" with -o (this defaults to the input file, with .php removed). This will generate an executable binary on your system, ready to execute

me@local:~$ echo '<?php echo "Hello World\n";' > test.php
me@local:~$ php bin/compile.php -o other test.php
me@local:~$ ./other
Hello World

Or, using the default:

me@local:~$ echo '<?php echo "Hello World\n";' > test.php
me@local:~$ php bin/compile.php test.php
me@local:~$ ./test
Hello World

Linting Code

If you pass the -l parameter, it will not execute the code, but instead just perform the compilation. This will allow you to test to see if the code even will compile (hint: most currently will not).

Debugging

Sometimes, you want to see what's going on. If you do, try the bin/print.php entrypoint. It will output two types of information. The first is the Control Flow Graph, and the second is the compiled opcodes.

me@local:~$ php bin/print.php -r 'echo "Hello World\n";'

Control Flow Graph:

Block#1
    Terminal_Echo
        expr: LITERAL<inferred:string>('Hello World
        ')
    Terminal_Return


OpCodes:

block_0:
  TYPE_ECHO(0, null, null)
  TYPE_RETURN_VOID(null, null, null)

Future Work

Right now, this only supports an EXTREMELY limited subset of PHP. There is no support for dynamic anything. Arrays aren't supported. Neither Object properties nor methods are supported. And the only builtin functions that are supported are var_dump and strlen.

But it's a start...

Debugging

Since this is bleeding edge, debuggability is key. To that vein, both bin/jit.php and bin/compile.php accept a -y flag which will output a pair of debugging files (they default to the prefix of the name of the script, but you can specify another prefix following the flag).

me@local:~$ echo '<?php echo "Hello World\n";' > demo.php
me@local:~$ php bin/compile.php -y demo.php
# Produces: 
#   demo - executable of the code
#   demo.debug.c - A pseudo-c version of the PHP code
#   demo.reproduce.c - All the libgccjit calls needed to reproduct the output

Checkout the committed demo.debug.c and demo.reproduce.c for more info...

Performance

So, is this thing any fast? Well, let's look at the internal benchmarks. You can run them yourself with php bench.php, and it'll give you the following output (running 5 iterations of each test, and averaging the time):

Test Name 7.3 (s) 7.3.NO.OPCACHE (s) 7.4 (s) 7.4.NO.OPCACHE (s) 8.JIT (s) 8.NOJIT (s) bin/jit.php (s) bin/compile.php (s) compiled time (s)
Ack(3,10) 1.1706 1.9168 1.1752 1.9196 0.6796 1.1634 0.5025 0.2939 0.2127
Ack(3,8) 0.0866 0.1057 0.0973 0.1215 0.0534 0.0853 0.3053 0.2943 0.0148
Ack(3,9) 0.3053 0.3709 0.3018 0.3730 0.1776 0.3010 0.3458 0.2937 0.0540
array_access 2.5903 2.5624 2.5958 2.6941 1.6697 2.6075 0.5495 0.2936 0.2685
fibo(30) 0.0747 0.1016 0.0760 0.1035 0.0429 0.0743 0.3065 0.2946 0.0110
mandelbrot 0.0441 0.1348 0.0434 0.1090 0.0323 0.0440 0.3186 0.3075 0.0146
simple 0.0557 0.0786 0.0650 0.0866 0.0391 0.0673 0.3094 0.2988 0.0120

To run the benchmarks yourself, you need to pass a series of ENV vars for each PHP version you want to test. For example, the above chart is generated with::

me@local:~$  PHP_7_3=php-7.3 PHP_7_4=php-7.4 PHP_8_JIT=php-8-jit PHP_8_NOJIT=php-8-nojit PHP_7_3_NO_OPCACHE="php-7.3 -dopcache.enable=0" PHP_7_4_NO_OPCACHE="php-7.4 -dopcache.enable=0" php bench.php

Without opcache doing optimizations, the bin/jit.php is actually able to hang up with ack(3,9) and mandelbrot for 7.3 and 7.4. It's even able to hang with PHP 8's experimental JIT compiler for ack(3,9).

Most other tests are actually WAY slower with the bin/jit.php compiler. That's because the test itself is slower than the baseline time to parse and compile a file (about 0.12 seconds right now).

And note that this is running the compiler on top of PHP. At some point, the goal is to get the compiler to compile itself, hopefully cutting the time to compile down by at least a few hundred percent.

Simply look at the difference between everything and the "compiled time" column (which is the result of the AOT compiler generating a binary). This shows the potential in this compilation approach. If we can solve the overhead of parsing/compiling in PHP for the bin/jit.php examples, then man could this fly...

So yeah, there's definitely potential here... evil grin

You can’t perform that action at this time.