Skip to content

The QBE backend

The qbe backend lowers the shared typed-SSA IR to QBE IL: the compact intermediate language of the QBE compiler backend. The external qbe tool turns that IL into native assembly, which the C compiler then assembles into an object file. QBE follows the platform C ABI, so the objects are call-compatible with C and link with the same pp_<name> export-wrapper translation unit the C backend uses, with no glue or forward declarations (spec §9.1).

It consumes the same IR as the C backend. The output is held to one rule: byte-identical to C wherever the arithmetic is exact. That equivalence is the backend's correctness oracle.

Pipeline

IR Module
  → emit_module  →  QBE IL (.ssa)
  → qbe -o out.s out.ssa      (IL → native assembly)
  → cc -O2 -fPIC -c out.s     (assemble → .o)
  → link with the C export wrappers (+ ipow runtime if used) → .so / .dylib

QBEBackend implements the backend protocol: emit returns the IL text, compile_object runs qbe then hands the .s to the toolchain's compile_c, and is_available is true when the qbe binary and a C compiler are both on PATH (QBE needs cc to assemble its output and to build the export wrappers). The source_ext is .ssa.

Select it per build:

ppc build kernel.py --backend qbe

The POSTPILE_BACKEND=qbe environment variable and the backend="qbe" API argument select it too; the default backend remains c.

Coverage

QBE covers the full scalar + array kernel subset the C backend supports:

  • int/float/bool arithmetic including floor-div, mod, float **, abs, casts, and select;
  • array load/store/dim/stride/alloc and len();
  • mutable locals and output-by-pointer stores (via stack slots);
  • the NumPy generalized-ufunc inner loop.

What it does not lower stays on the C path:

Not lowered Why
Complex64 / Complex128 QBE has no complex type
Float16 QBE has no half-float arithmetic
Str / Bytes pointer types not modeled
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 qbe on a Complex128 program fails with error: dtype Complex128 not supported by the QBE backend, and --ext-module fails with error: the 'qbe' 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 QBEUnsupportedError, so the build fails on an unsupported dtype (qbe_type), an aggregate GetField/SetField, integer ** on a non-integer dtype, or an unhandled instruction. This is the project's guiding rule: reject unsupported semantics, never miscompile.

Type mapping

QBE's type system is minimal: w (32-bit int), l (64-bit int/pointer), s (f32), d (f64), plus b/h in memory for narrow loads and stores.

POST Python dtype QBE base type
Bool, Int8/16/32, UInt8/16/32 w
Int64, UInt64 l
Float32 s
Float64 d

Because QBE keeps the full w after arithmetic while C truncates on assignment, sub-word integer results are re-narrowed to their declared width (extsb, extuh, …), and QBE has no implicit promotion, so mixed-type operands get explicit coercions following C's usual-arithmetic-conversion rules.

ABI and the integer-power runtime

Float ** routes through libm (powf/pow), resolved at link time. For integer and unsigned **, the backend calls __pp_ipow_i64 / __pp_ipow_u64 instead, a tiny C runtime (IPOW_RUNTIME_C) that is an extern copy of the C backend's static ipow helpers, so the result matches bit for bit. The backend's runtime_sources emits that unit (compiled as C) only when a module actually uses integer **, keeping every object on the platform C ABI.

Optimization passes

qbe does register allocation and mem2reg but no loop optimization (and little CSE); the assemble-only cc -c step cannot add it back. So the backend does its own IR-level passes, each verified to keep the output byte-identical to C:

  1. Local CSE (cse). The frontend lowers every a[i] to a fresh dim/stride/i*stride/base+idx/load chain, so x[i] * x[i] recomputes the whole index and loads the element twice. A per-block value-numbering pass folds identical pure computations to one definition and a repeated load of the same address to a single load, unless a store or call has run since, so it stays correct under aliasing and in-place ops. Runs on a copy, leaving the shared IR intact for the other backends.

  2. Array-metadata LICM (_hoist_array_meta). Array parameters are immutable pointers available in the entry block, which dominates every loop, so their data+offset base, per-axis strides, and shape are computed once in entry and reused; correct by construction, no dominator analysis needed.

  3. Ufunc-loop strength reduction + view hoisting (emit_ufunc_loop). Each ufunc argument pointer becomes a loop induction variable (p += step), dropping the per-element i*step multiply, and each argument's __pp_array view has its invariant fields (shape, strides, ndim, offset) built once in @start; only the data pointer moves per iteration.
  4. Scalar-kernel fusion (_inline_scalar_kernel). A single straight-line scalar kernel is spliced directly into the ufunc loop, which saves a call per element; its IR is cloned with every value name prefixed so it cannot collide with loop temps. Multi-block kernels fall back to a call.
  5. Kernel-loop strength reduction (_plan_strength_reduction). The one pass that needs real analysis: it builds the CFG, computes dominators, finds natural loops, identifies each loop's counter, and rewrites array accesses as induction pointers bumped by the counter's stride. It decomposes a byte index into a sum of var*stride products, so matmul's nested a[i][p] (i*s0 + p*s1) reduces too. A stride is either an axis stride read from the array's strides[] descriptor at runtime (so non-contiguous Fortran-order / reversed inputs stay correct) or a constant the frontend folds for a contiguous a[i] (dim*itemsize), recognizing the latter extends induction-pointer walks (and then unrolling) from the ufunc path to plain whole-array for i in range(n) kernels.
  6. Loop unrolling ×4 (_plan_unroll). Counted reduction loops whose body is one straight-line block, whose array accesses are all strength-reduced, and whose counter is used only in those indices are unrolled by 4. The accumulator stays threaded through its stack slot, preserving the exact serial fadd order (hence byte-identical), with a remainder loop for the leftover iterations (trip count rounded down with n & -4).

Correctness

Verification rests on the byte-identical oracle: each kernel is built through both backends and driven through the real NumPy generalized-ufunc ABI via ctypes; the two native outputs must agree bit-for-bit. Fuzz harnesses compare QBE with C across random inputs, dtypes, and byte strides taken straight from NumPy. When both backends behave identically-wrongly, the test itself is at fault.

Performance

On the four ufunc kernels of python -m benchmarks run (2026-11-11, Apple M1, Python 3.14, the minimum of 40 adjacent rounds), QBE takes 1.18× the C backend's time by geometric mean, and CPython about 55× QBE's.

Kernel QBE's time ÷ the C backend's
square (element-wise, memory-bandwidth-bound) 2.13
dot (a reduction) 1.03
gaussian (exp-bound) 1.10
matmul (4×4, nested loops) 0.80

square is the low point. The C and LLVM backends emit a unit-stride fast path for cc's and LLVM's vectorisers; QBE has no vectoriser to feed, so it did not get the port. QBE's instruction selection also emits ldr [x0]; add x0, 8 where a post-increment load would do. matmul is where QBE's hand-rolled unrolling of the nested reduction beats the C compiler at -O2. The residual gap on reductions is not SIMD or FMA: cc -O2 emits neither for FP reductions, because both would reorder the summation and change the bits (they are gated behind -ffast-math, which this project does not adopt, as it would break the oracle). The gap that unrolling closed was loop-branch overhead and load-latency hiding, done bit-exactly at the IR level.

Reproduce the numbers

See Benchmarking for the harness and methodology, and the toolchain page for the qbe and cc tools the backend drives.

On whole programs the gap is wider. Over ADR 0067's thirteen programs, each compared with the same program written in C (python -m benchmarks.wholeprog, 2026-11-10), QBE takes 4.83× C's time with the default growing int and 3.23× with the 64-bit one, where the LLVM backend takes 2.17× and 1.26× (the table). QBE has no speed target; it is kept as a third backend built independently of the other two and held byte for byte to them.

See also