Psyco and Unladen Swallow were the first to try to make a just-in-time compiler (JIT) for Python, but these projects have stopped, leaving standard Python with no good JIT solution. So I started investigating how hard would it be to make a JIT for Python using Rpython and LLVM. The results of my first highly experimental implementation of Rpython-to-LLVM show very fast JIT performance: 4x faster than PyPy, 200x faster than Python2, and 260x faster than Python3.
Test Function
def simple_test(a, b):
c = 0
while c < 100000*100000:
c += a + b
return c
The test function is simply a huge loop that adds-to and returns a 64bit integer. The test was performed on a AMD 2.4ghz Quad with 4GB of RAM, average test result times are:
Rpython-to-LLVM = 2 seconds
PyPy1.8 (with warm JIT) = 8 seconds
Python2.7.2 = 400 seconds
Python3.2.2 = 530 seconds
Building The JIT
The first challenge in this project was building the code that traverses the Rpython flow-graph ("flow object space") and converts it into LLVM format. For each Rpython flow-graph block a new LLVM basic-block is created, and for each operation in the block a new LLVM instruction is created. Blocks that loop and modify a variable require some extra work, these mutable variables are treated as stack allocations, and then the LLVM optimization pass PROMOTE_MEMORY_TO_REGISTER replaces the costly stack allocations with fast register memory.
It is interesting to see what LLVM IR looks like for the simple function used in this test, before and after the PROMOTE_MEMORY_TO_REGISTER optimization.
LLVM is more than just a JIT, because LLVM IR is platform independent, it becomes the best solution for making Python extension modules that need to support all platforms and all Python versions. A classic Python extension module is written in C, and must be compiled for each Python version, each OS, and each OS type (32bit and 64bits)! (Python2+Python3+PyPy)*(Linux+OSX+Windows)*(32bits+64bits) = 18 targets. How is anybody supposed to compile their Python extension for all 18 targets?
LLVM IR can be generated on any platform any bit-depth, saved to a file, and later loaded and run on any target that PyLLVM supports. PyLLVM works with Python2 and Python3; and is easily portable to PyPy using cpyext. In other words, LLVM IR can easily hit all 18 targets - no problem.
Extra Advantages:
LLVM easily calls into C libraries
LLVM has a SIMD accelerated vector type
LLVM has powerful optimizations like: PROMOTE_MEMORY_TO_REGISTER
No comment really just want to thank Brent for doing this. I have been working on specs for a Python stack that I think would be a good fit with web2py and pypy
No comment really just want to thank Brent for doing this. I have been working on specs for a Python stack that I think would be a good fit with web2py and pypy
ReplyDeletecheers guys!
sorry I meant Brett
ReplyDeletenice art you create yourself?
Brian, yes that's my artwork before i had learned coding and Python.
Delete