BRAIDGROUP
RESEARCH & DEV
41. Documentation

Advanced Topics

Supercompilation (Turchin's Algorithm)

Braid's compiler includes a supercompilation pass that performs automatic program specialization at compile time. Based on Turchin's supercompilation algorithm, the run_supercompiler() pass analyzes the AST and partially evaluates known branches, unfolds recursive calls with constant arguments, and specializes polymorphic functions. This eliminates dynamic dispatch overhead and enables aggressive constant folding across function boundaries.

// Supercompiler specializes this at compile time
fn compute(n: int) -> int {
    if n == 0 {
        return 1
    }
    return n * compute(n - 1)
}

// The call compute(5) is fully evaluated to 120 at compile time
let result = compute(5)

Autocatalytic Recursion

Autocatalytic recursion is a self-referential code optimization technique where the compiler identifies recursive patterns that can bootstrap their own optimization. The compiler detects structurally recursive functions whose return values depend on themselves in a way that enables staged compilation — each recursion level generates progressively more optimized code. This is used in the Braid tensor compiler to generate fused kernel pipelines automatically.

Runtime Code Generation (compiler.eval)

Braid supports runtime code generation through the compiler.eval() FFI, which compiles and executes Braid source strings at runtime. This enables JIT compilation patterns, dynamic code specialization, and metaprogramming use cases where the code shape depends on runtime data.

let expr = "fn(x: int) -> int { return x * 2 }"
let double_fn = compiler.eval(expr)
let result = double_fn(21)  // 42

Tensor Graph Compilation for ML Workloads

Braid's MLIR backend compiles tensor computation graphs into fused GPU kernels. The compiler traces tensor operations, builds a computation graph, applies graph-level optimizations (op fusion, layout transformation, memory planning), and emits optimized MLIR or LLVM IR. The polyhedral optimization pass (run_polyhedral_opt()) applies loop tiling, vectorization, and parallelization to tensor operations.

@autograd fn train_step(model: Model, batch: Tensor) -> float {
    let pred = model.forward(batch)
    let loss = cross_entropy(pred, batch.labels)
    loss.backward()
    return loss.item()
}

// The tensor graph compiler fuses:
// forward -> loss -> backward into a single kernel

DTS Multi-Dimensional Branching

Distinction Tree (DTS) multi-dimensional branching extends Braid's match expressions with multi-axis pattern matching. DTS organizes conditions into a tree of distinctions, enabling simultaneous matching across multiple dimensions (type, value, shape, provenance). This is used in the compiler's type checker for efficient type unification and in DTS-based routing for the Junction web framework.

// DTS branching across multiple dimensions
match tensor {
    (float32, cpu, 2d) => render_cpu_2d(tensor),
    (float32, cuda, 2d) => render_gpu_2d(tensor),
    (float16, cuda, 3d) => render_gpu_3d(tensor),
    _ => fallback_render(tensor)
}

Polyhedral Optimization

The run_polyhedral_opt() pass analyzes loop nests in tensor computations and applies polyhedral compilation techniques: automatic parallelization, cache locality optimization, and systolic array mapping for GPU targets.

Related Pages