Skip to content

PPy

A compiler for the statically analyzable part of Python

It reads ordinary Python files and builds them into native code, C, CUDA, or StableHLO.

Get started Examples Performance

A .ppy file is valid Python. You can run it with python and no compiler installed, and that run is the reference answer. The compiler reads the same file, proves what it can about the types and effects, lowers it through a canonical IR, and hands it to a backend.

import ppy


@ppy.pure
@ppy.opt(3)
def longest(limit: int) -> int:
    best: int = 0
    for start in range(1, limit):
        n: int = start
        steps: int = 0
        while n != 1:
            n = n // 2 if n % 2 == 0 else 3 * n + 1
            steps += 1
        best = max(best, steps)
    return best


print(longest(ppy.input[int]()))
echo 300000 | python    collatz.ppy      # plain CPython                 1068.8 ms
echo 300000 | ppy run   collatz.ppy      # LLVM, Python's integers         41.7 ms
echo 300000 | ppy build collatz.ppy -o dist && ./dist/collatz   # native    30.8 ms

The same loop in C takes 41.9 ms with gcc -O3 and 32.8 ms with clang -O3. These are ten fresh processes each on one machine. The performance page has the full table, including Numba, Codon, PyPy, Cython, mypyc, and Nuitka.

Install

uv add "ppy-lang[llvm]"      # or: pip install "ppy-lang[llvm]"
uv run ppy doctor            # reports the compiler, LLVM, the C toolchain, plugin runtimes

Then follow Getting started, which goes from this file to a native build in about ten minutes.

Highlights

  • Three paths, one answer. python f.ppy runs plain CPython, ppy f.ppy runs the optimized Python backend, and ppy run f.ppy runs LLVM native code. All three must print the same output. A difference is a compiler bug, and every example is checked for it.
  • Falls back instead of guessing. Code outside the supported subset is reported with its location. A guard that fails at runtime, such as an overflowing multiply, returns to the Python body and finishes there.
  • Ahead-of-time builds. ppy build produces a launcher and a library that keep working after the compiler is uninstalled. ppy build --standalone produces a native executable with no CPython inside.
  • Seven backends off one IR. LLVM, C11, C++17, CUDA, HIP, NVVM/PTX, and StableHLO, across 19 IR dialects. Other packages can register their own backend.
  • Strings, classes, and collections. str, lists, dicts, and sets, classes with inheritance, the standard library's random, math, heapq, collections, and functools, and Vec, HashMap, TreeMap, and six more compile to native code, reference counted with cycles collected, and give the same answers as CPython.
  • Existing Python as it is. Without strict mode, ppy run types unannotated parameters from the project's calls, defaults, doctests, argparse options, and operators, types unannotated fields from what the program stores into them, and runs what the analysis cannot follow on CPython (Types from call sites).
  • Eight library plugins. NumPy, PyTorch, JAX/Flax, pydantic, FastAPI/Uvicorn, SciPy, pandas, and PyArrow.
  • A cheap call boundary. A native call with two int arguments costs 35 ns from Python, against 33 ns for CPython's own call. Lists, dicts, sets, and objects cross in the same generated C wrapper, what a call writes comes back into the caller's objects, and objects Python passes again and again stay resident in native memory between calls.
  • Tested. 1543 test functions on Python 3.12, 3.13, and 3.14, with 67% statement coverage. 82 diagnostic codes, each documented in one place.

Documentation

Getting started Install, run a file three ways, check it, build it.
Guide The language, one topic per page: the subset, directives, native memory, SIMD, threads, parallel loops, derivatives, coroutines, GPU kernels, XLA, generics, regular expressions.
Examples 56 folders and 66 programs with their commands and output. 18 of them are measured against Numba, Cython, NumPy, numexpr, JAX, PyTorch, CuPy, Triton, Taichi, Mojo, Codon, Rust, C, pandas, polars, asyncio, and uvloop; the results are collected on the comparisons page.
Reference The command line, diagnostics, configuration, performance, compatibility, and the Python API.
Internals The pipeline, the IR and its dialects, the cache, the solver.

Limitations

PPy accepts Python's syntax and works with its ecosystem, but it does not support every dynamic behavior. exec and eval, monkey-patching, dynamic namespace mutation, and unrestricted runtime reflection are restricted, or isolated behind an explicit ppy.dynamic boundary. That is the trade for analysis and native code you can rely on.

To run an existing Python project, turn strict mode off (strict = false in [tool.ppy]): what the analysis cannot follow is a warning and runs on CPython. To bring it over to strict PPy, use ppy migrate (how a real project went).