ADR-failure-return-1: A Failure’s Status Travels Beside the Value, and the Throw Path Is Cold

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

1. Question

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.

2. What was measured

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:

3. Options

4. Decision

D1 — The throw path is cold, and the failing function stays inlinable.

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.

D2 — On the Rust backend, a failing call returns two scalars where the value and the error fit one word each.

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.

D3 — ADR-023 D10 is stated per backend.

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.

D4 — On the direct backend, the status is a register of its own (ADR-023 §3.1: B).

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.

D5 — The carry flag is a measurement, not a decision.

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.

5. Reasons

6. Open

7. Considered and rejected

8. Consequences

This site is open source. Improve this page.