Issue #1235: Our own optimiser and Cranelift: faster in compile and run time against every combination

Open and answer this issue on GitHub.

compiler, note, future · opened 2026-11-04 · updated 2026-11-19 · 2 comments

The goal

Faster in compile time and in run time at once, against every combination:

  compile time run time
Nikaia over LLVM faster faster
Rust over LLVM faster faster
Rust over Cranelift faster faster

The means: our own optimiser in the compiler, which uses what only Nikaia knows, and Cranelift as the backend behind it.

This is not needed for the current focus. It is filed now so the idea is not lost, and labelled future.

Why it can work

LLVM spends much of its time rediscovering facts: whether an index is in range, whether two references overlap, whether a call has side effects, whether an addition can overflow. Our compiler already has many of these facts as proofs (the ledger, touch sets, proven indices, ranges). Cranelift is fast because it does few optimisations of its own; whatever we hand it, it gains in full.

So the plan is not “Cranelift instead of LLVM”, but: the optimisations that matter are done once, by us, from facts we already have, cheaply because we do not have to rediscover them, and Cranelift turns the result into machine code quickly.

The hard cell

Run time against Nikaia over LLVM is the hardest of the six. If LLVM gets the same facts from us, it still has its own passes on top. Beating it means our optimiser has to cover what LLVM’s passes give a Nikaia program, in particular:

and add what LLVM cannot derive at all: merging calls of pure functions, removing work whose result is unused because the effect system says it touches nothing, fusing iterator chains whose steps are proved independent, laying out data by how it is touched.

The compile-time question

Today the compiler emits Rust and hands it to rustc (ADR-004). Even with Cranelift as rustc’s backend, rustc’s own front end (type check, borrow check, MIR) stays in every build. Much of what it checks, our compiler has already proved. Beating every combination in compile time likely needs a path past that front end. The section below records three; choosing one would change ADR-004 and is a decision of its own.

Paths past rustc’s front end

Three ways are on the table. They are not exclusive.

Emitting Cranelift IR directly

The compiler writes Cranelift IR itself, with no rustc in the build.

Linking our code with Rust code (hybrid)

Our functions are compiled straight to machine code; Rust crates are compiled by rustc as before; both meet in the linker through a thin layer of C-ABI glue functions.

Handing MIR to rustc (a custom driver)

A small driver links against rustc (rustc_private, on nightly), as Miri does, and replaces the query that builds a function’s MIR: for our functions it returns the MIR our compiler wrote, instead of parsing, type-checking and borrow-checking a Rust body.

The price of the type declarations

None of this is measured yet; step 2 below measures it.

The real split: does a program need Rust’s crates? (owner, 2026-11-06)

Whether a program starts fast or runs fast is not the line that decides the path. The line is whether it needs compatibility with Rust, because it uses crates of the Rust ecosystem.

So the path is a property of the dependency graph, not a switch for “scripts” or “release”. A short script that pulls in a crate takes the rustc path; a large program of pure Nikaia can take the direct one. Two questions follow:

One shared backend, not one per use. Running scripts without rustc and the direct Cranelift path above want the same thing: one form after the checker, from which we emit either Rust or machine code. That form is not an intermediate representation of our own (owner, 2026-11-14): Nikaia has none on purpose. See the next section. A second backend for scripts alone would be a third path to keep in step; it has to earn that, see below.

Reduce and optimise in the checked tree, not in an IR (owner, 2026-11-14)

Our optimiser does not lower to an intermediate representation and optimise there. It reduces and optimises the checked tree itself, by rewriting tree to tree:

Because every reduced tree is still Nikaia:

Limits to keep in mind: data-flow analyses across branches are clumsier on a tree than on a control-flow graph (workable because Nikaia is structured and has no goto); register allocation does not belong in the tree (the stencil variants do it for copy-and-patch, LLVM for the Rust path); rewrite rules can block each other or blow the tree up (equality saturation over e-graphs is one answer); spans must keep pointing at the source after a rewrite; shared subexpressions want a DAG rather than a pure tree.

Copy-and-patch as the backend (owner, 2026-11-06)

Copy-and-patch builds machine code by copying precompiled pieces (“stencils”, one per operation and operand shape) and patching the holes in them: constants, addresses, jump targets. It is known for compiling much faster than Cranelift, with code somewhat slower than an optimising backend.

The thought, to be proven: copy-and-patch could also beat Cranelift in run time, if

If that holds, copy-and-patch would be the shared backend: compile time near zero for scripts, run time at least that of Cranelift. If it does not, Cranelift stays the candidate.

Whether it pays is shown against other scripting languages, in a hard benchmark comparison, not against Rust alone. For a script, start-up plus compile plus run is what the user waits for, and the competition is Python, Lua and LuaJIT, JavaScript (Node, Deno) and Ruby, not rustc. Counted as docs/measuring.md says.

To build

  1. Measure where we stand, as docs/measuring.md says (counted instructions), on the benchmarks and on our own compiler: the six cells above, with rustc_codegen_cranelift as the Cranelift column for now. This shows the size of each gap before any work.
  2. Measure what the bodies cost rustc, without nightly: take the Rust our compiler emits for a real program (the compiler itself through lower-std, and an example), and count cargo check once in full and once with every function body replaced by loop {}. The difference is what handing MIR to rustc could save in the front end; the rest is the price of the declarations. Together with a full build over Cranelift and over LLVM this shows how much of the compile time each path can win at all.
  3. List which LLVM passes account for the run-time gap between the Cranelift and LLVM columns on our programs.
  4. Our optimiser, pass by pass, as rewrites of the checked tree onto a subset of Nikaia (section above), ordered by the gap each pass closes; each pass measured before and after against all columns, and its output re-checked by the checker and the prover in the tests.
  5. A decision paper on the path past rustc’s front end (direct Cranelift IR with the hybrid link, copy-and-patch, or MIR through a custom driver), against ADR-004 and the withdrawal of the nightly path, once 1–4 and 6–8 show what rustc’s front end costs and whether the run-time side holds. It also decides how a program’s dependency graph chooses its path.
  6. Measure the reach of the direct path: what share of a nikaia run of a short script is rustc today, and how many of our tools/*.nika, scripts and packages need no Rust crate.
  7. A copy-and-patch prototype with register holes on a handful of benchmark kernels, against Cranelift: compile time and run time, once with and once without front-end optimisations. This proves or refutes the run-time claim above.
  8. The scripting benchmark: the same set of short programs in Nikaia (each path) and in Python, Lua and LuaJIT, Node or Deno, and Ruby; start-up plus compile plus run counted together, and run alone for longer programs.

Generated by Claude Code

Comments

keywan-ghadami, 2026-11-14

Plan changed (owner, 2026-11-14), recorded in the issue text above:


Generated by Claude Code

keywan-ghadami, 2026-11-19

From the report of the run of issue-1525 (#1525):

ADR-failure-return-1 D4: the direct path’s Nikaia-internal calling convention returns a failure’s status beside the value, in a register of its own (in Cranelift a further return value under its own convention), the value keeping every return register it has without throws; Rust and C crossings meet through a shim. D5: step 7 adds a measurement of the carry flag (stc; ret / jc stencils, clc on success unless carry is known clear) against that register on the same kernels; adopted on x86-64 only if it gains measurably, ARM64 and RISC-V keep the register.


Generated by Claude Code

Back to Open Issues