The LLVM backend¶
The llvm backend lowers the shared typed-SSA IR to LLVM IR text, which llvmlite assembles to a native object in process: no external llc. It consumes the same IR as the C and QBE backends and follows the platform C ABI, so the objects link against the same pp_<name> export-wrapper translation unit, with no glue or forward declarations (spec §9.1).
The output is held to the same rule as QBE's: byte-identical to C wherever the arithmetic is exact. That equivalence is the backend's correctness oracle.
It is modelled on the QBE backend. Both are typed-SSA text emitters below C, and the package mirrors QBE's layout (types · convert · arithmetic · arrays · instructions · assemble). But it diverges from QBE in one decisive way: it does no hand-written optimization (see below).
Pipeline¶
IR Module
→ emit_module → LLVM IR text (.ll)
→ llvmlite.binding: parse_assembly → verify → target machine (reloc=pic, opt=2)
→ the -O2 module pass pipeline → emit_object (all in process)
→ link with the C export wrappers (+ ipow runtime if used) → .so / .dylib
LLVMBackend implements the backend protocol: emit returns the IR text, compile_object drives llvmlite.binding to produce the .o, and is_available is true when llvmlite is importable and a C compiler is on PATH (the export wrappers and the ipow runtime are C). The source_ext is .ll.
Select it per build:
or with the POSTPILE_BACKEND=llvm environment variable or the backend="llvm" API argument. The default backend remains c.
llvmlite is an optional dependency
Install it with the llvm extra (pip install postpile[llvm], or
uv sync --extra llvm for a dev checkout). The backend imports llvmlite
lazily (only inside compile_object and is_available), so it registers
without error when the extra is absent; it simply reports itself
unavailable until llvmlite is present.
Coverage¶
LLVM covers the same scalar + array kernel subset as QBE:
- int/float/bool arithmetic including floor-div, mod, float and integer
**,abs, casts, andselect; - array load/store/dim/stride/alloc and
len(); - sub-word integers;
- mutable locals and output-by-pointer stores;
- the NumPy generalized-ufunc inner loop.
What it does not lower stays on the C path:
| Not lowered | Why |
|---|---|
Complex64 / Complex128 |
not modelled (kept on C, which has native complex) |
Float16 |
no half-float arithmetic in the emitter |
Str / Bytes |
pointer types not modelled |
| CPython extension modules | C-API registration glue is C by nature |
The backend's capabilities are the defaults: complex=False, ext_module=False, float16=False. The build driver rejects what a backend cannot do; it does not silently route around it. ppc build z.py --backend llvm on a Complex128 program fails with error: dtype Complex128 not supported by the LLVM backend, and --ext-module fails with error: the 'llvm' backend does not build CPython extension modules. Choosing the C backend for those is the user's call; the diagnostic says so.
Fail loud, never miscompile
Any IR construct the backend cannot lower raises LLVMUnsupportedError,
so the build fails on an unsupported dtype (llvm_type), an aggregate
GetField/SetField, or an unhandled instruction. This is the project's
guiding rule: reject unsupported semantics, never miscompile.
Type mapping¶
LLVM's integer types are exact-width, so each POST dtype maps to its natural type:
| POST Python dtype | LLVM type |
|---|---|
Bool |
i1 |
Int8 / UInt8 |
i8 |
Int16 / UInt16 |
i16 |
Int32 / UInt32 |
i32 |
Int64 / UInt64 |
i64 |
Float32 |
float |
Float64 |
double |
Because sub-word integers are stored at their true width, add/sub/mul wrap at that width natively, matching POST's truncate-to-width semantics with no explicit narrowing pass (where QBE, keeping a 32-bit w, must re-narrow). Mixed-type and sub-word comparisons still promote their operands to C's usual-arithmetic-conversion common type first. Signedness belongs to the operation (sdiv vs udiv, sext vs zext, fptosi vs fptoui), since LLVM integer types are sign-agnostic.
Constants are emitted in LLVM's hex bit-pattern form (double 0x...): it is exact for any value, whereas LLVM rejects an inexact decimal float literal.
ABI and declarations¶
Float ** routes through libm (powf/pow); integer and unsigned ** call __pp_ipow_i64 / __pp_ipow_u64, the shared IPOW_RUNTIME_C unit (an extern copy of the C backend's static ipow helpers, bit-identical), emitted by runtime_sources only when a module uses integer **. This runtime lives in backends/runtime.py, shared with QBE so neither backend imports the other.
LLVM IR requires a declare for every called function. Each callee's signature is recorded as it is emitted. The internal libm helpers (floor, fmod, fabs and the others used by floor-div, mod and abs) come from a fixed table; the other callees (libm called from POST code like exp/sqrt, and cross-module POST functions) are synthesised from the call site. emit_module then emits a declaration for the symbols the module does not itself define.
No hand-written passes¶
This is where the backend departs from QBE. QBE does register allocation but no loop optimization, so the QBE backend adds IR-level passes (local CSE, metadata LICM, ufunc strength reduction, kernel fusion, loop strength reduction, ×4 unrolling) to stay competitive. LLVM brings its own optimizer, so this backend takes the opposite route:
- Emit naive IR and let
-O2optimize. Array accesses recompute their addresses, loops are plain counted loops. LLVM's pass pipeline does the LICM, strength reduction, unrolling, and vectorization, so nopassesmodule exists. - One hint the optimizer can't infer:
!tbaa. The naive-IR bet needs exactly one piece of metadata. An__pp_arraydescriptor and the element buffer it points at are separate objects, but-O2can't prove an element store (out[i] = …) doesn't clobber the struct, so LICM won't hoist the per-access descriptor loads: it reloadsdata/offset/shape/stridesat each iteration (~3× slower on a bandwidth-bound loop). Tagging descriptor loads and element loads/stores with distinct TBAA types (the same thing clang emits) supplies the disjointness and the loads hoist. Unlike!invariant.loadit claims no constant memory, so it stays correct when the same array is passed twice or a ufunc rebinds a view's data pointer between calls. This gap was caught by the cross-backend benchmark'ssquarekernel. alloca+mem2regfor SSA construction. Reassigned locals get an entry-blockallocawith naive load/store, whichmem2reg/SROA in-O2promote to registers, constructing SSA form, so the emitter never writes aphiitself. (Scalar outputs store directly through the caller's pointer, which is their slot.)- Strict IEEE floats: no fast-math.
-O2never reassociates floating-point withoutreassoc/fast, so reductions keep their exact serial order and results stay bit-identical tocc -O2. The backend sets no fast-math flags.
Correctness¶
Verification rests on the byte-identical oracle: each kernel is built through the C and LLVM backends and driven through the real NumPy generalized-ufunc ABI via ctypes; the two native outputs must agree bit-for-bit. The LLVM backend is held to the same golden-program corpus as C and QBE. It joins the parity oracle automatically by registering as available. When two backends behave identically-wrongly, the test itself is at fault.
Performance¶
Because it emits naive IR and lets -O2 do the work, the LLVM backend runs at parity with the C backend on the ufunc kernels: 0.99× the C backend's time by geometric mean over square, dot, gaussian and matmul (python -m benchmarks run, 2026-11-11, Apple M1, Python 3.14), all kernels within 2%, where QBE, whose hand-rolled passes this backend skips, takes 1.18×. python -m benchmarks cross the same day put it at 1.00× C over the four whole-array kernels.
On whole programs it is close to the C backend and behind the same programs written in C: over ADR 0067's thirteen programs (python -m benchmarks.wholeprog, 2026-11-10) it takes 2.17× C's time with the default growing int and 1.26× with the 64-bit one, against 1.97× and 1.17× for the C backend; matmul2d is its widest gap to the C backend (2.73 against 1.47). The table lists all the programs. cc -O2 and LLVM at -O2 both leave FP reductions without SIMD or FMA, since either would reorder the summation and change the bits.
Reproduce the numbers
See Benchmarking for the harness (the
postpile-llvm runtime and its geomean-vs-C line) and the methodology.
See also¶
- Writing a backend: the protocol LLVM implements.
- The QBE backend: the sibling text emitter this one is modelled on, and the contrast in optimization strategy.
- The C backend: the correctness oracle both are measured against.
- IR reference: the input all three backends consume.