Just announced LPython - a high performance compiler for Python with multiple backends.
LPython is a novel, fast, and retargetable Python compiler that can compile type-annotated Python code to optimized machine code. It offers several backends such as LLVM, C, C , WASM, Julia, and x86. LPython features quick compilation and runtime performance, as demonstrated by the benchmarks in their blog.
LPython also offers Just-In-Time (JIT) compilation and seamless interoperability with CPython. It is designed to be compatible with existing Python libraries and tools. LPython is released as an alpha version, meaning that users may encounter bugs when using it.
One of the key features of LPython is its ability to generate optimized machine code from type-annotated Python code. It uses the Abstract Semantic Representation (ASR) shared with LFortran, which allows for independent optimizations at the ASR level. The goal of LPython is to produce a compiler that runs exceptionally fast and generates fast code.
LPython provides several backends, including LLVM, C, C , and WASM. It uses a multi-phase compilation process, transforming input code into an Abstract Syntax Tree (AST), then into an Abstract Semantic Representation (ASR), which preserves all semantic information. The ASR undergoes several ASR-to-ASR passes, where abstract operations are transformed into concrete statements. Finally, the final ASR is sent to the selected backend to generate code.
LPython supports Ahead-of-Time (AoT) compilation, which allows users to compile their Python code to optimized machine code before execution. It also supports Just-In-Time (JIT) compilation, where functions can be decorated with
@lpython to enable JIT compilation. Currently, only C backend is supported for JIT compilation, but support for LLVM and other backends is planned for the future.
LPython offers interoperability with CPython, allowing users to access any library implemented using CPython. This is achieved through the @pythoncall decorator, which allows calling functions in CPython libraries. Users can also access libraries like NumPy, TensorFlow, PyTorch, and Matplotlib using LPython.
The benchmarks provided in the blog demonstrate the performance of LPython in various scenarios. For example, LPython's JIT compilation is compared to Numba on tasks like summation of elements in an array, pointwise multiplication of arrays, insertion sort on lists, and the Dijkstra shortest-path algorithm. LPython shows competitive performance in all these scenarios.
LPython looks like a promising project to aid applications which require high performance, such as AI. Mojo is also adopting a similar approach, however they are not only building a compiled Python, but also extending it to offer systems programming constructs as first class features. It would be interesting to compare the two.
Here's the full blog post covering their alpha release:
lpython.org/blog/2023/07/lpy…