Open and answer this issue on GitHub.
compiler, note, future · opened 2026-11-04 · updated 2026-11-19 · 2 comments
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.
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.
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.
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.
rustc’s front endThree ways are on the table. They are not exclusive.
The compiler writes Cranelift IR itself, with no rustc in the build.
rustc front end at all; the fastest compile time this plan can reach.rustc’s checks are gone for good, also where we would want them as a second opinion.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.
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.
rustc’s front end that grows with the amount of code.rustc session, so inlining across the crate boundary still works and no glue layer is needed.rustc setting.rustc’s checks run: for example a --verify-rust flag turns the borrow check of our MIR back on, as a check of our own checker in CI, and -Zvalidate-mir checks that the MIR we wrote is well formed. Emitting Cranelift directly loses this control, or needs a second path kept alive to keep it.rustc_private, an interface that changes without notice. This reverses the withdrawal of the nightly toolchain and the rustc_ast path (docs/history/withdrawn-one-way-down.md); a decision paper has to answer that record.rustc and becomes silent misbehaviour; the switch above is the safeguard, so CI runs with it on.rustc still needs the type declarations: structs, enums, traits, the heads of impl blocks and the function signatures. Only the bodies fall away (see the price below).loop {}). New is the mapping from our items to rustc’s internal ids, so that the MIR we write names the right types, fields and trait methods. Building the types directly inside rustc instead of through stubs is much more work and even less stable; the stubs are preferred.rustc still resolves the declarations, the item types and signatures, generics and trait coherence; this grows with the number of items, not with the amount of code. Monomorphisation and the backend stay on every path.rustc’s view of the types exactly (field order, layout, which trait method a call means), and follow the MIR format as it changes between nightlies.None of this is measured yet; step 2 below measures it.
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.
std only needs no rustc front end at all. Our compiler has already checked it and instantiates its generics itself, so it can go straight to machine code.rustc for them: only rustc resolves their generics and traits. It takes the rustc path, the MIR driver, or the hybrid link above, where a crate’s interface fits a C-ABI layer.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:
tools/*.nika, the scripts, the packages) and of typical programs needs no crate at all? That is the reach of the direct path.std is partly Rust today. The direct path needs it prebuilt behind a C ABI, or moved into Nikaia (self-hosting, ADR-294, #125); the two plans meet here.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.
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:
foreach to while, async to a state machine) and GHC’s compilation by transformation are known cases of the approach.for over a list and its accesses are still visible, so a loop is vectorised where the prover shows no overlap, no overflow, indexes in range and a correct remainder. For the copy-and-patch backend this is the only vectorisation, as no LLVM follows it.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 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.
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.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.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.nikaia run of a short script is rustc today, and how many of our tools/*.nika, scripts and packages need no Rust crate.Generated by Claude Code
Plan changed (owner, 2026-11-14), recorded in the issue text above:
Generated by Claude Code
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