AoT Compiler for Python

Question:

I want to get my Python script working on a bare metal device like microcontroller WITHOUT the need for an interpreter. I know there are already JIT compilers for Python like PyPy, and interpreters like CPython.

However, existing interpreters I’ve seen (such as CPython) take up large memory (in MB range).

Is there an AOT compiler for Python (i.e. compiling directly to native hardware through intermediary like LLVM)?

I assume such a compiler would enable Python to run much faster compared to existing implementations AND with lower memory footprint. If there is, I wonder why that solution hasn’t been popularized.

Asked By: Ravit Sharma

||

Answers:

As you already mentioned Cython is an option (However, it is true that the result is big due since the C runtime need to implement the Python functionality together with your program).

With regards to LLVM there was a project by Google named unladen swallow. However, that project is mostly abandoned. You can find some information about it here

Basically it was an attempt to bring LLVM optimizations into the runtime of Cython. E.g JITTING Python code.

Another old alternative was shed skin which compiles Python to C++. Some information about it can be found here.

Yet another option similar to shed skin is to restrict yourself to a subset of the Python language and use micropython.

Answered By: JKRT

An alternative would be to use GraalVM with Truffle AOT with Python.
It’s basically Python running on an optimized AOT for jvm.

The project looks promising. You can chek this link here:

https://www.graalvm.org/22.2/graalvm-as-a-platform/language-implementation-framework/AOT/

Answered By: Louis Hancquart

Recently came across codon,

Codon is a high-performance Python compiler that compiles Python code to native machine code without any runtime overhead. Typical speedups over Python are on the order of 10-100x or more, on a single thread. Codon’s performance is typically on par with (and sometimes better than) that of C/C++.

Answered By: tenshi