A group of developers has developed a compiler for the Python language that compile type-annotated code for optimized machine code.
LPython supports several backends for LLVM, C, C++, WASM, Julia and x86 with quick compilation and runtime performance. It also offers Just-In-Time (JIT) compilation and seamless interoperability with CPython.
Based on the novel Abstract Semantic Representation (ASR) shared with LFortran, LPython’s intermediate optimizations are independent of the backends and frontends. The two compilers, LPython and LFortran, benefits from improvements at the ASR level to produce a compiler that both runs fast and generates fast code.
First, input code is transformed into an Abstract Syntax Tree (AST) using parsers. The AST is then transformed into an Abstract Semantic Representation (ASR), which preserves all semantic information present in the input code. ASR contains all information required by all backends in a form that is not specific to any particular backend.
Machine Independent Code Optimisations
LPython implements several machine-independent optimisations via ASR-to-ASR passes. This includes loop unrolling, loop vectorisation, dead code removal and function call inlining as well as transforming division to multiplication operation and fused multiplication and addition. All the optimizations are applied via one command-line argument, –fast.
LPython naturally acts as a Python compiler. By default, if no backend is provided it compiles type-annotated user input code to LLVM, which generates binary final output. Consider the following small example:
Interoperability with CPython
LPython can call functions in CPython libraries which permits “break-out” to TensorFlow, PyTorch, and even to matplotlib. The break-outs will run at ordinary (slow) Python speeds, but the compiler accelerates the mathematical portions to near maximum speed.
“The main takeaway is that LPython/LFortran generate fast code by default. Our benchmarks show that it’s straightforward to write high-speed LPython code,” say the team. “We hope to raise expectations that LPython output will be in general at least as fast as the equivalent C++ code. Users love Python because of its many productivity advantages: great tooling, easy syntax, and rich data structures like lists, dicts, sets, and arrays. Because any LPython program is also an ordinary Python program, all the tools – debuggers and profilers, for instance – just work. Then, LPython delivers run-time speeds, even with rich data structures at least as short as alternatives in most cases.”
- API for System on Modules simplifies Edge AI/ML
- Interview – Is Python the solution to verification challenges?
Features under development include interactive, Jupyter support where LPython can be used as a Jupyter kernel, allowing Python/Julia-style rapid prototyping and an exploratory workflow. It can also be used from the command-line with an interactive prompt (REPL).
Support for diverse hardware LLVM makes it possible to run LPython on CPUs of all architectures, including x86, ARM, and POWER, GPUs from Nvidia, AMD, and Intel and TPU tensor processors from Google.