Energy Management :: 2013/14 Energy Economics Prof. Tânia Sousa [email protected].
Building a JIT compiler for PHP in 2 days Nuno Lopes [email protected] Instituto Superior...
-
Upload
shanon-blake -
Category
Documents
-
view
212 -
download
0
Transcript of Building a JIT compiler for PHP in 2 days Nuno Lopes [email protected] Instituto Superior...
![Page 1: Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt Instituto Superior Técnico Technical University of Lisbon.](https://reader037.fdocuments.us/reader037/viewer/2022110320/56649cd75503460f9499f4de/html5/thumbnails/1.jpg)
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 Nuno Lopes nuno.lopes@ist.utl.pt Instituto Superior Técnico Technical University of Lisbon.](https://reader037.fdocuments.us/reader037/viewer/2022110320/56649cd75503460f9499f4de/html5/thumbnails/2.jpg)
Outline
• Overview of the Zend VM• Design Rationale• Implementation• Results• Future Work
![Page 3: Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt Instituto Superior Técnico Technical University of Lisbon.](https://reader037.fdocuments.us/reader037/viewer/2022110320/56649cd75503460f9499f4de/html5/thumbnails/3.jpg)
Overview of the Zend VM
![Page 4: Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt Instituto Superior Técnico Technical University of Lisbon.](https://reader037.fdocuments.us/reader037/viewer/2022110320/56649cd75503460f9499f4de/html5/thumbnails/4.jpg)
Overview of the Zend VM
• Syntax-directed translation
![Page 5: Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt Instituto Superior Técnico Technical University of Lisbon.](https://reader037.fdocuments.us/reader037/viewer/2022110320/56649cd75503460f9499f4de/html5/thumbnails/5.jpg)
Overview of the Zend VM
• Syntax-directed translation• Interprets bytecode
![Page 6: Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt Instituto Superior Técnico Technical University of Lisbon.](https://reader037.fdocuments.us/reader037/viewer/2022110320/56649cd75503460f9499f4de/html5/thumbnails/6.jpg)
Overview of the Zend VM
• Syntax-directed translation• Interprets bytecode• No code optimizations
![Page 7: Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt Instituto Superior Técnico Technical University of Lisbon.](https://reader037.fdocuments.us/reader037/viewer/2022110320/56649cd75503460f9499f4de/html5/thumbnails/7.jpg)
![Page 8: Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt Instituto Superior Técnico Technical University of Lisbon.](https://reader037.fdocuments.us/reader037/viewer/2022110320/56649cd75503460f9499f4de/html5/thumbnails/8.jpg)
![Page 9: Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt Instituto Superior Técnico Technical University of Lisbon.](https://reader037.fdocuments.us/reader037/viewer/2022110320/56649cd75503460f9499f4de/html5/thumbnails/9.jpg)
PHP bytecode
![Page 10: Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt Instituto Superior Técnico Technical University of Lisbon.](https://reader037.fdocuments.us/reader037/viewer/2022110320/56649cd75503460f9499f4de/html5/thumbnails/10.jpg)
PHP bytecode
• Memory based (vs register or stack based)
![Page 11: Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt Instituto Superior Técnico Technical University of Lisbon.](https://reader037.fdocuments.us/reader037/viewer/2022110320/56649cd75503460f9499f4de/html5/thumbnails/11.jpg)
PHP bytecode
• Memory based (vs register or stack based) • No standard representation
![Page 12: Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt Instituto Superior Técnico Technical University of Lisbon.](https://reader037.fdocuments.us/reader037/viewer/2022110320/56649cd75503460f9499f4de/html5/thumbnails/12.jpg)
PHP bytecode
• Memory based (vs register or stack based) • No standard representation• Designed to be executed and discarded
![Page 13: Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt Instituto Superior Técnico Technical University of Lisbon.](https://reader037.fdocuments.us/reader037/viewer/2022110320/56649cd75503460f9499f4de/html5/thumbnails/13.jpg)
PHP bytecode
• Memory based (vs register or stack based) • No standard representation• Designed to be executed and discarded• Some information is not stored in bytecode
(e.g. class definitions)
![Page 14: Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt Instituto Superior Técnico Technical University of Lisbon.](https://reader037.fdocuments.us/reader037/viewer/2022110320/56649cd75503460f9499f4de/html5/thumbnails/14.jpg)
<?phpif (1 > 2) $a = 2 * 3;else $a = 2 * 4;
echo $a;?>
![Page 15: Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt Instituto Superior Técnico Technical University of Lisbon.](https://reader037.fdocuments.us/reader037/viewer/2022110320/56649cd75503460f9499f4de/html5/thumbnails/15.jpg)
Design Rationale
![Page 16: Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt Instituto Superior Técnico Technical University of Lisbon.](https://reader037.fdocuments.us/reader037/viewer/2022110320/56649cd75503460f9499f4de/html5/thumbnails/16.jpg)
Design Rationale
• Do not rewrite the whole VM from scratch
![Page 17: Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt Instituto Superior Técnico Technical University of Lisbon.](https://reader037.fdocuments.us/reader037/viewer/2022110320/56649cd75503460f9499f4de/html5/thumbnails/17.jpg)
Design Rationale
• Do not rewrite the whole VM from scratch• Have a proof-of-concept working ASAP
![Page 18: Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt Instituto Superior Técnico Technical University of Lisbon.](https://reader037.fdocuments.us/reader037/viewer/2022110320/56649cd75503460f9499f4de/html5/thumbnails/18.jpg)
Design Rationale
• Do not rewrite the whole VM from scratch• Have a proof-of-concept working ASAP• Leave room for future optimizations
![Page 19: Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt Instituto Superior Técnico Technical University of Lisbon.](https://reader037.fdocuments.us/reader037/viewer/2022110320/56649cd75503460f9499f4de/html5/thumbnails/19.jpg)
Implementation
![Page 20: Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt Instituto Superior Técnico Technical University of Lisbon.](https://reader037.fdocuments.us/reader037/viewer/2022110320/56649cd75503460f9499f4de/html5/thumbnails/20.jpg)
Implementation
• Works as a Zend VM extension("a speedup plugin")
![Page 21: Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt Instituto Superior Técnico Technical University of Lisbon.](https://reader037.fdocuments.us/reader037/viewer/2022110320/56649cd75503460f9499f4de/html5/thumbnails/21.jpg)
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 Nuno Lopes nuno.lopes@ist.utl.pt Instituto Superior Técnico Technical University of Lisbon.](https://reader037.fdocuments.us/reader037/viewer/2022110320/56649cd75503460f9499f4de/html5/thumbnails/22.jpg)
Implementation
• Works as a Zend VM extension("a speedup plugin")
• Hooks as the bytecode executor• Updates the state of the VM
![Page 23: Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt Instituto Superior Técnico Technical University of Lisbon.](https://reader037.fdocuments.us/reader037/viewer/2022110320/56649cd75503460f9499f4de/html5/thumbnails/23.jpg)
Implementation
• Works as a Zend VM extension("a speedup plugin")
• Hooks as the bytecode executor• Updates the state of the VM• Can be used along with the old interpreter
![Page 24: Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt Instituto Superior Técnico Technical University of Lisbon.](https://reader037.fdocuments.us/reader037/viewer/2022110320/56649cd75503460f9499f4de/html5/thumbnails/24.jpg)
Implementation #2
![Page 25: Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt Instituto Superior Técnico Technical University of Lisbon.](https://reader037.fdocuments.us/reader037/viewer/2022110320/56649cd75503460f9499f4de/html5/thumbnails/25.jpg)
Implementation #2
• Offline compilation of Zend VM bytecode handlers to LLVM
![Page 26: Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt Instituto Superior Técnico Technical University of Lisbon.](https://reader037.fdocuments.us/reader037/viewer/2022110320/56649cd75503460f9499f4de/html5/thumbnails/26.jpg)
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 Nuno Lopes nuno.lopes@ist.utl.pt Instituto Superior Técnico Technical University of Lisbon.](https://reader037.fdocuments.us/reader037/viewer/2022110320/56649cd75503460f9499f4de/html5/thumbnails/27.jpg)
Implementation #2
• Offline compilation of Zend VM bytecode handlers to LLVM
• Translation of bytecodes to handler calls• JIT compilation of one function at a time
![Page 28: Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt Instituto Superior Técnico Technical University of Lisbon.](https://reader037.fdocuments.us/reader037/viewer/2022110320/56649cd75503460f9499f4de/html5/thumbnails/28.jpg)
Implementation #2
• Offline compilation of Zend VM bytecode handlers to LLVM
• Translation of bytecodes to handler calls• JIT compilation of one function at a time• Performs simple optimizations (including
inlining)
![Page 29: Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt Instituto Superior Técnico Technical University of Lisbon.](https://reader037.fdocuments.us/reader037/viewer/2022110320/56649cd75503460f9499f4de/html5/thumbnails/29.jpg)
Implementation #2
• Offline compilation of Zend VM bytecode handlers to LLVM
• Translation of bytecodes to handler calls• JIT compilation of one function at a time• Performs simple optimizations (including
inlining)• Uses a small runtime "library"
![Page 30: Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt Instituto Superior Técnico Technical University of Lisbon.](https://reader037.fdocuments.us/reader037/viewer/2022110320/56649cd75503460f9499f4de/html5/thumbnails/30.jpg)
![Page 31: Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt Instituto Superior Técnico Technical University of Lisbon.](https://reader037.fdocuments.us/reader037/viewer/2022110320/56649cd75503460f9499f4de/html5/thumbnails/31.jpg)
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 Nuno Lopes nuno.lopes@ist.utl.pt Instituto Superior Técnico Technical University of Lisbon.](https://reader037.fdocuments.us/reader037/viewer/2022110320/56649cd75503460f9499f4de/html5/thumbnails/32.jpg)
<?phpif (1 > 2) $a = 2 * 3;else $a = 2 * 4;
echo $a;?>
![Page 33: Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt Instituto Superior Técnico Technical University of Lisbon.](https://reader037.fdocuments.us/reader037/viewer/2022110320/56649cd75503460f9499f4de/html5/thumbnails/33.jpg)
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 Nuno Lopes nuno.lopes@ist.utl.pt Instituto Superior Técnico Technical University of Lisbon.](https://reader037.fdocuments.us/reader037/viewer/2022110320/56649cd75503460f9499f4de/html5/thumbnails/34.jpg)
LLVM bitcode
op_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 Nuno Lopes nuno.lopes@ist.utl.pt Instituto Superior Técnico Technical University of Lisbon.](https://reader037.fdocuments.us/reader037/viewer/2022110320/56649cd75503460f9499f4de/html5/thumbnails/35.jpg)
Results of "Hello World"
• Vanilla: 0.03s• JIT Debug: 2.5s• JIT Release: 0.68s• JIT Release+no asserts: 0.64s
Slowdown: 21x
![Page 36: Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt Instituto Superior Técnico Technical University of Lisbon.](https://reader037.fdocuments.us/reader037/viewer/2022110320/56649cd75503460f9499f4de/html5/thumbnails/36.jpg)
Results
![Page 37: Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt Instituto Superior Técnico Technical University of Lisbon.](https://reader037.fdocuments.us/reader037/viewer/2022110320/56649cd75503460f9499f4de/html5/thumbnails/37.jpg)
Future Work
![Page 38: Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt Instituto Superior Técnico Technical University of Lisbon.](https://reader037.fdocuments.us/reader037/viewer/2022110320/56649cd75503460f9499f4de/html5/thumbnails/38.jpg)
Future Work
• Compiled code caching and sharing
![Page 39: Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt Instituto Superior Técnico Technical University of Lisbon.](https://reader037.fdocuments.us/reader037/viewer/2022110320/56649cd75503460f9499f4de/html5/thumbnails/39.jpg)
Future Work
• Compiled code caching and sharing• Self-executable apps ("normal", GTK, etc..)
![Page 40: Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt Instituto Superior Técnico Technical University of Lisbon.](https://reader037.fdocuments.us/reader037/viewer/2022110320/56649cd75503460f9499f4de/html5/thumbnails/40.jpg)
Future Work
• Compiled code caching and sharing• Self-executable apps ("normal", GTK, etc..)• Self-contained webapps (with e.g. Apache)
![Page 41: Building a JIT compiler for PHP in 2 days Nuno Lopes nuno.lopes@ist.utl.pt Instituto Superior Técnico Technical University of Lisbon.](https://reader037.fdocuments.us/reader037/viewer/2022110320/56649cd75503460f9499f4de/html5/thumbnails/41.jpg)
Future Work
• Compiled code caching and sharing• Self-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 Nuno Lopes nuno.lopes@ist.utl.pt Instituto Superior Técnico Technical University of Lisbon.](https://reader037.fdocuments.us/reader037/viewer/2022110320/56649cd75503460f9499f4de/html5/thumbnails/42.jpg)
Questions?