Status: Accepted
Date: November 9, 2026
Answers: ADR-023 §3.1 (where the success/failure decision lives), and how
ADR-023 D10’s “the success path pays no memory round trip” is kept on each
backend
Supersedes / Related:
ADR-023 D10 (the cost guarantee; made precise here), §3.1
(answered here), §4 (the literal carry flag; reopened for one backend);
ADR-280 D24, D25 (the one-word tail whose non-zero word this
uses), §6 (in-value versus out-of-band status; answered here);
ADR-tether-v1 (views, the two-word return this cares about);
ADR-004 (the Rust backend);
issue #1235 (our own
optimiser, Cranelift, copy-and-patch);
docs/measuring.md
ADR-023 D10 promises, normatively, that a call which can fail pays no memory round trip on success. ADR-023 §3.1 left open how: the status in the value (A, a tag in a niche, what Rust gives) or beside it (B, a register of its own, for a backend that owns its calling convention). Issue #1235 plans exactly such a backend. So the question is no longer theoretical, and it has to be measured against what the Rust backend actually does today.
nikaia-std as it stands, rustc 1.97, opt-level = 3, x86-64, callgrind;
the functions #[inline(never)] so the call boundary is visible. The shapes
are what nikaia lower writes for fn one(n: i64) -> i64 throws and its
neighbours.
| Return on success | Size | Comes back in | Callee instructions |
|---|---|---|---|
Result<i64, Thrown<Kind>> (fieldless error) |
16 B | memory (sret) | 10 |
Result<i64, Thrown<Code>> (Code(u64)) |
16 B | memory (sret) | 13 |
Result<(), Thrown<Kind>> |
16 B | registers | – |
Result<&str, Thrown<Kind>>, Result<(i64, i64), …> |
24 B | memory (sret) | – |
pair (i64, usize): second word 0 = success, else the tail word |
16 B | registers | 5 |
Findings:
crates/nikaia-std/src/error.rs asserts the size, not the
way it is returned.throwing is not cold. For Thrown<Code> LLVM inlines the trace
machinery, and every call, failing or not, pays two pushes and a 168-byte
frame (13 against 10 instructions).tail) returns pointer, length and
status in rax, rcx, rdx; the caller is call; test; jnz, no memory.
With System V the status goes to memory.Result<T, Thrown<E>>. Breaks ADR-023 D10 as measured.T up to one word; cannot hold it for a two-word T.T up to two words.throwing and every constructor of an envelope on the throw path are
#[cold] and #[inline(never)]. A function that can fail is not kept from
inlining: its body shrinks to a compare, a branch and a cold call, which makes
it a better inlining candidate, and an inlined call has no return convention
to pay for at all.
A test checks the code a failing call compiles to, not only its size: a
non-failing call to fn one(n: i64) -> i64 throws touches no stack memory.
Where T fits one word and E’s payload fits one word, the lowering writes
(T-or-payload, tail) instead of Result<T, Thrown<E>>. The tail word is
never zero (ADR-280 D25); zero is success. A crossing into Rust code the
program did not lower converts back to Result in a shim, which ADR-023 §3.1
already counts as ordinary at a boundary.
Built only after the measurement in §6 shows non-inlined failing calls carry real weight in our programs and benchmarks.
ADR-023 D10’s guarantee reads: on the Rust backend, a failing call returning a value of up to one word pays no memory round trip; a two-word value (a view, a pair) does, until the direct backend exists. On the direct backend, the guarantee holds up to two words. The gap is named, not hidden.
The Nikaia-internal calling convention of the direct path in issue #1235
returns the status beside the value, in a register: in Cranelift a further
return value under its own convention. The value keeps every return register
it would have without throws. Calls to and from Rust and C use their
conventions and meet through a shim; a program of Nikaia and std only has no
such boundary.
ADR-023 §4 rejected the carry flag because no backend we used could reach it.
Copy-and-patch can: the two sequences involved (stc; ret and jc after the
call) are short enough to write by hand as stencils, and nothing between ret
and jc touches the flags. A successful return then needs clc unless the
last flag write is known to leave carry clear (xor, test, an unsigned
operation proven or checked not to overflow).
The prototype in issue #1235, step 7, measures it against D4’s register on the same kernels. It is adopted on x86-64 only if it gains measurably; ARM64 and RISC-V keep the register.
T plus a status is three
words; Rust’s convention has two registers for it. Only B gets past that.test and jnz fuse on current cores, so the carry flag’s real gain is a
register and perhaps nothing more. Only a measured prototype says.lower-std, the benchmarks, tools/*.nika), counted as
docs/measuring.md says. Gates D2.
Counted (November 2026), too few for D2 to pay: the compiler
(target/fast/nikaia, after D1) holds 462 failing functions in
tools/*.nika; 177 survive as functions of their own, reached from 176
direct call sites, against 8 477 direct call sites of lowered functions in
all. Under callgrind, nikaia check of compiler/libraries.nika (the first
116 G instructions) made 9 490 calls to a failing lowered function out of
202 M calls to lowered functions (0.005 %); nikaia lower of
benches/template.nika made 3 975 of 184 k in 132 M instructions. At D2’s
4 instructions a call that is under 0.02 % of either run. The benchmarks
fail outside main only in game/fasta (3 calls a run) and
game/k-nucleotide (7). D2 stays unbuilt until a program shows weight.throw unreachable; such a call could take the plain convention.crates/nikaia-std/src/error.rs: #[cold] and #[inline(never)] on the
throw-path constructors; a test on the compiled call, beside the size
assertions.