SciPy 2024

LPython: Novel, Fast, Retargetable Python Compiler
07-10, 15:25–15:55 (US/Pacific), Room 315

We are developing a modern open-source Python compiler called LPython
(https://lpython.org/) that can execute user's code interactively in Jupyter to
allow exploratory work (much like CPython, MATLAB or Julia) as well as compile
to binaries with the goal to run user's code on modern architectures such as
multi-core CPU, GPU, as well as unfamiliar, new architectures like GSI's APU,
which features programmable compute-in memory. We aim to provide the best
possible performance for numerical array oriented code. The compiler itself is written in C++ for robustness
and speed.


Background/Motivation

Attendees will learn that it is possible to use Python in a modern interactive
way and yet have execution speed as fast or faster than other compiled
languages such as C++ or Fortran. Python is one of the most used languages
today. For performance applications such as High Performance Computing (HPC) or
any other kind of numerical computing the standard CPython implementation is
often not fast enough and it is difficult to run Python code on GPUs and other
accelerators. To address these issues we have developed LPython, a Python
compiler that can compile Python code to binaries, work interactively, and run
on all platforms.

Methods

LPython is written in C++ and it has multiple backends to generate code
including LLVM [1] and C++. The compiler has been open sourced under BSD
license, available at https://github.com/lcompilers/lpython. The Abstract
Syntax Tree (AST) and the intermediate Abstract Semantic Representation (ASR)
is represented using the ASDL domain-specific language [2], just like CPython's
AST. LPython is designed as a library with separate building blocks (parser,
AST, ASR, semantic phase, codegen) that are all exposed to the user/developer
in a natural way to make it easy to contribute back. LPython is using the same
internal representation (ASR) as LC [3] and LFortran [4], the other frontends in LCompilers [5].
All three frontends are effectively surface languages that share the same middle
end and backends, as well as high and low level optimizations. The speed of LPython
comes from high level optimizations at the ASR level, as well as the low level
optimizations that LLVM can do. In addition it is remarkably easy to customize back ends.

Results

We will show a demo of LPython, examples of Python codes that compile, both
ahead-of-time and interactively in Jupyter. As well as benchmarks against other
Python compilers such as Cython, Pythran, Numba, JAX, PyTorch, Transonic,
Nuitka and others. We will show some of the unique optimizations that LPython
can do, on top of regular LLVM optimizations. We will also show compiler
diagnostic messages (errors, warnings, ...) and the developer experience with
using the compiler.

Conclusions

The intended audience is any user from the Python, Julia, Fortran or MATLAB
scientific or data community that wants to use the productive modern
interactive development workflow in an established language (Python/NumPy), but
also wants the code to run fast using a highly optimizing compiler.

[1] https://llvm.org/

[2] Wang, D. C., Appel, A. W., Korn, J. L., & Serra, C. S. (1997). The Zephyr
Abstract Syntax Description Language. USENIX Conference on Domain-Specific
Languages, (October), 213–228.

[3] https://github.com/lcompilers/lc

[4] https://lfortran.org/

[5] https://lcompilers.org/

Principal Software Engineer at Microsoft, previously GSI Technology, former scientist at Los Alamos National Laboratory.

Ondřej is the original author of SymPy, SymEngine, LFortran and LPython.

Website: https://ondrejcertik.com/