Bet 3: schedules: speed without touching meaning

Claim. You can write a numeric kernel once, as plain math, and make it fast in a separate file that cannot change what the program computes. (This is Halide's idea, applied to a general-purpose language.)

Kill criterion (CHARTER.md). Matmul demo: a layout change plus vectorization gives byte-identical output and a measured speedup. Result: passed.

What the programmer writes

# kernels.tr: meaning only
fn matmul(a, b):
  let n = len(a)
  let k = len(b)
  let m = len(b[0])
  grid(n, m, fn(i, j) => sum(k, 0.0, fn(t) => a[i][t] * b[t][j]))
# kernels.sched: speed only (picked up automatically by `trine build`)
matmul:
  layout b: transposed     # rows | flat | transposed
  order i t j              # any permutation of the loop variables
  tile i 32, j 128
  vectorize j 4            # widths 2, 4, 8

grid and sum are ordinary pure builtins. They run unchanged in the interpreter and the VM. The schedule only affects native builds.

Why results cannot change

  1. Only pure, total kernels are accepted. The body must be Float arithmetic plus indexing by loop variables, with no calls, no effects and nothing that can fail midway. Any other body gets the reference code, and the reason is printed.
  2. Guarded specialization. The compiled kernel first checks shapes at run time: the matrices must be rectangular, the extents must agree, and every index must be in range. If any check fails, the call falls back to the generic rt_grid. Wrong-shaped input therefore fails with the interpreter's exact error message and location.
  3. The reduction order is fixed. Each output element still sums t = 0, 1, …, k-1 from init, in that order. Floating-point addition is not associative, so a schedule may reorder which element is computed when, but never the order of additions inside an element. Vectorization runs across j, which means separate elements, never across t. The C compiler is run with -ffp-contract=off, so it cannot fuse multiply-adds either.
  4. Invalid schedules are rejected, not guessed. Examples: an order that isn't a permutation of the loop variables, a vector width other than 2/4/8, vectorizing t, or a layout for a name that isn't a matrix the kernel indexes. Each is reported with schedule f: NOT applied (reason), and the reference code runs instead.

Evidence

Speed (matmul, Float, this machine; all outputs byte-identical)

n = 512:

varianttimevs reference
reference grid/sum (closure per element)3.753 s1×
same algorithm as imperative loops0.740 s5.1×
compiled kernel, empty schedule0.164 s23×
order i t j + vectorize j 40.079 s48×
layout b: transposed0.100 s38×
tiled + flat + vectorize j 80.097 s39×
tiled + -march=native0.056 s67×
naive Rust (triple loop, Vec<Vec<f64>>)0.180 s

n = 1024:

varianttime
compiled kernel, empty schedule1.469 s
order i t j + vectorize j 40.639 s
tiled1.123 s
order i t j + -march=native0.454 s
tiled + -march=native0.648 s
naive Rust1.950 s

A one-line schedule beats a naive Rust triple loop by 2.3–3×, from the same source that runs in the interpreter.

Honest notes

On bench/matmul_kernel.tr, Perceus now removes 99.5% of refcount overhead, and the small kernels.tr goes from 50% to 100%.