42
Building a JIT compiler for PHP in 2 days Nuno Lopes [email protected] Instituto Superior Técnico Technical University of Lisbon

Building a JIT compiler for PHP in 2 days - LLVM · Building a JIT compiler for PHP in 2 days Nuno Lopes [email protected] ... Do not rewrite the whole VM from scratch Have a

  • Upload
    others

  • View
    23

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Building a JIT compiler for PHP in 2 days - LLVM · Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt ... Do not rewrite the whole VM from scratch Have a

Building a JIT compiler for PHP in 2 days

Nuno [email protected]

Instituto Superior TécnicoTechnical University of Lisbon

Page 2: Building a JIT compiler for PHP in 2 days - LLVM · Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt ... Do not rewrite the whole VM from scratch Have a

Outline

Overview of the Zend VMDesign RationaleImplementationResultsFuture Work

Page 3: Building a JIT compiler for PHP in 2 days - LLVM · Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt ... Do not rewrite the whole VM from scratch Have a

Overview of the Zend VM

Page 4: Building a JIT compiler for PHP in 2 days - LLVM · Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt ... Do not rewrite the whole VM from scratch Have a

Overview of the Zend VM

Syntax-directed translation

Page 5: Building a JIT compiler for PHP in 2 days - LLVM · Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt ... Do not rewrite the whole VM from scratch Have a

Overview of the Zend VM

Syntax-directed translationInterprets bytecode

Page 6: Building a JIT compiler for PHP in 2 days - LLVM · Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt ... Do not rewrite the whole VM from scratch Have a

Overview of the Zend VM

Syntax-directed translationInterprets bytecodeNo code optimizations

Page 7: Building a JIT compiler for PHP in 2 days - LLVM · Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt ... Do not rewrite the whole VM from scratch Have a
Page 8: Building a JIT compiler for PHP in 2 days - LLVM · Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt ... Do not rewrite the whole VM from scratch Have a
Page 9: Building a JIT compiler for PHP in 2 days - LLVM · Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt ... Do not rewrite the whole VM from scratch Have a

PHP bytecode

Page 10: Building a JIT compiler for PHP in 2 days - LLVM · Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt ... Do not rewrite the whole VM from scratch Have a

PHP bytecode

Memory based (vs register or stack based)

Page 11: Building a JIT compiler for PHP in 2 days - LLVM · Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt ... Do not rewrite the whole VM from scratch Have a

PHP bytecode

Memory based (vs register or stack based) No standard representation

Page 12: Building a JIT compiler for PHP in 2 days - LLVM · Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt ... Do not rewrite the whole VM from scratch Have a

PHP bytecode

Memory based (vs register or stack based) No standard representationDesigned to be executed and discarded

Page 13: Building a JIT compiler for PHP in 2 days - LLVM · Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt ... Do not rewrite the whole VM from scratch Have a

PHP bytecode

Memory based (vs register or stack based) No standard representationDesigned to be executed and discardedSome information is not stored in bytecode (e.g. class definitions)

Page 14: Building a JIT compiler for PHP in 2 days - LLVM · Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt ... Do not rewrite the whole VM from scratch Have a

<?phpif (1 > 2) $a = 2 * 3;else $a = 2 * 4;

echo $a;?>

Page 15: Building a JIT compiler for PHP in 2 days - LLVM · Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt ... Do not rewrite the whole VM from scratch Have a

Design Rationale

Page 16: Building a JIT compiler for PHP in 2 days - LLVM · Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt ... Do not rewrite the whole VM from scratch Have a

Design Rationale

Do not rewrite the whole VM from scratch

Page 17: Building a JIT compiler for PHP in 2 days - LLVM · Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt ... Do not rewrite the whole VM from scratch Have a

Design Rationale

Do not rewrite the whole VM from scratchHave a proof-of-concept working ASAP

Page 18: Building a JIT compiler for PHP in 2 days - LLVM · Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt ... Do not rewrite the whole VM from scratch Have a

Design Rationale

Do not rewrite the whole VM from scratchHave a proof-of-concept working ASAPLeave room for future optimizations

Page 19: Building a JIT compiler for PHP in 2 days - LLVM · Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt ... Do not rewrite the whole VM from scratch Have a

Implementation

Page 20: Building a JIT compiler for PHP in 2 days - LLVM · Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt ... Do not rewrite the whole VM from scratch Have a

Implementation

Works as a Zend VM extension("a speedup plugin")

Page 21: Building a JIT compiler for PHP in 2 days - LLVM · Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt ... Do not rewrite the whole VM from scratch Have a

Implementation

Works as a Zend VM extension("a speedup plugin")Hooks as the bytecode executor

Page 22: Building a JIT compiler for PHP in 2 days - LLVM · Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt ... Do not rewrite the whole VM from scratch Have a

Implementation

Works as a Zend VM extension("a speedup plugin")Hooks as the bytecode executorUpdates the state of the VM

Page 23: Building a JIT compiler for PHP in 2 days - LLVM · Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt ... Do not rewrite the whole VM from scratch Have a

Implementation

Works as a Zend VM extension("a speedup plugin")Hooks as the bytecode executorUpdates the state of the VMCan be used along with the old interpreter

Page 24: Building a JIT compiler for PHP in 2 days - LLVM · Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt ... Do not rewrite the whole VM from scratch Have a

Implementation #2

Page 25: Building a JIT compiler for PHP in 2 days - LLVM · Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt ... Do not rewrite the whole VM from scratch Have a

Implementation #2

Offline compilation of Zend VM bytecode handlers to LLVM

Page 26: Building a JIT compiler for PHP in 2 days - LLVM · Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt ... Do not rewrite the whole VM from scratch Have a

Implementation #2

Offline compilation of Zend VM bytecode handlers to LLVM Translation of bytecodes to handler calls

Page 27: Building a JIT compiler for PHP in 2 days - LLVM · Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt ... Do not rewrite the whole VM from scratch Have a

Implementation #2

Offline compilation of Zend VM bytecode handlers to LLVM Translation of bytecodes to handler callsJIT compilation of one function at a time

Page 28: Building a JIT compiler for PHP in 2 days - LLVM · Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt ... Do not rewrite the whole VM from scratch Have a

Implementation #2

Offline compilation of Zend VM bytecode handlers to LLVM Translation of bytecodes to handler callsJIT compilation of one function at a timePerforms simple optimizations (including inlining)

Page 29: Building a JIT compiler for PHP in 2 days - LLVM · Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt ... Do not rewrite the whole VM from scratch Have a

Implementation #2

Offline compilation of Zend VM bytecode handlers to LLVM Translation of bytecodes to handler callsJIT compilation of one function at a timePerforms simple optimizations (including inlining)Uses a small runtime "library"

Page 30: Building a JIT compiler for PHP in 2 days - LLVM · Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt ... Do not rewrite the whole VM from scratch Have a
Page 31: Building a JIT compiler for PHP in 2 days - LLVM · Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt ... Do not rewrite the whole VM from scratch Have a

zend_execute()

while (1) { int ret;

if ((ret = EX(opline)->handler(data)) > 0) { switch (ret) { ... } }}

Page 32: Building a JIT compiler for PHP in 2 days - LLVM · Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt ... Do not rewrite the whole VM from scratch Have a

<?phpif (1 > 2) $a = 2 * 3;else $a = 2 * 4;

echo $a;?>

Page 33: Building a JIT compiler for PHP in 2 days - LLVM · Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt ... Do not rewrite the whole VM from scratch Have a

LLVM bitcode

op_block: %execute_data = call @phpllvm_get_execute_data(%1) %execute_result = call @ZEND_IS_SMALLER_HANDLER(%execute_data) switch i32 %execute_result, label %op_block1 [ i32 1, label %pre_vm_return i32 2, label %pre_vm_enter i32 3, label %pre_vm_leave]

Page 34: Building a JIT compiler for PHP in 2 days - LLVM · Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt ... Do not rewrite the whole VM from scratch Have a

LLVM bitcodeop_block1: %execute_data = call @phpllvm_get_execute_data(%1) %execute_result = call @ZEND_JMPZ_HANDLER(%execute_data) %current = call i32 @phpllvm_get_opline_number(%1) switch i32 %current, label %ret [ i32 5, label %op_block5 i32 2, label %op_block2]

Page 35: Building a JIT compiler for PHP in 2 days - LLVM · Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt ... Do not rewrite the whole VM from scratch Have a

Results of "Hello World"

Vanilla: 0.03sJIT Debug: 2.5sJIT Release: 0.68sJIT Release+no asserts: 0.64s

Slowdown: 21x

Page 36: Building a JIT compiler for PHP in 2 days - LLVM · Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt ... Do not rewrite the whole VM from scratch Have a

Results

Page 37: Building a JIT compiler for PHP in 2 days - LLVM · Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt ... Do not rewrite the whole VM from scratch Have a

Future Work

Page 38: Building a JIT compiler for PHP in 2 days - LLVM · Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt ... Do not rewrite the whole VM from scratch Have a

Future Work

Compiled code caching and sharing

Page 39: Building a JIT compiler for PHP in 2 days - LLVM · Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt ... Do not rewrite the whole VM from scratch Have a

Future Work

Compiled code caching and sharingSelf-executable apps ("normal", GTK, etc..)

Page 40: Building a JIT compiler for PHP in 2 days - LLVM · Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt ... Do not rewrite the whole VM from scratch Have a

Future Work

Compiled code caching and sharingSelf-executable apps ("normal", GTK, etc..)Self-contained webapps (with e.g. Apache)

Page 41: Building a JIT compiler for PHP in 2 days - LLVM · Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt ... Do not rewrite the whole VM from scratch Have a

Future Work

Compiled code caching and sharingSelf-executable apps ("normal", GTK, etc..)Self-contained webapps (with e.g. Apache)Optimizations (lots of them :)

Page 42: Building a JIT compiler for PHP in 2 days - LLVM · Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt ... Do not rewrite the whole VM from scratch Have a

Questions?