Open and answer this issue on GitHub.
performance, decision, wip, future · opened 2026-11-09 · updated 2026-11-19 · 5 comments
The owner decided this ADR (2026-11-09). It is written in as ADR-failure-return-1 and built in steps; D2, D4 and D5 wait for their measurements.
Probe (today’s state, from §2 of the ADR): a non-failing call to fn one(n: i64) -> i64 throws, not inlined, comes back through memory (sret, 16 B), and with Thrown<Code> pays two pushes and a 168-byte frame (13 instructions instead of 10), because throwing is not cold. crates/nikaia-std/src/error.rs only asserts the size.
To build
docs/specification/adr/adr-failure-return-1.md with the text below, unchanged apart from fixes to its links and the header block TEMPLATE.md asks for (**Status:** Accepted, **Supersedes / Related:**). Records are named by topic since ADR-decision-records-1; failure-return is a new topic: add it to Topics in use and the record to the index.throwing and every envelope constructor on the throw path in crates/nikaia-std/src/error.rs #[cold] and #[inline(never)] (existing runtime Rust, an attribute change; the specification-first exception in CLAUDE.md applies). Do not mark failing functions themselves as not inlinable.fn one(n: i64) -> i64 throws touches no stack memory. Write it as a nikaia test program (tests/compiler/tests/), not a new #[test]. If checking the compiled code cannot be said there yet, that is a finding: report it Result: blocked with a probe, and do not write a Rust test.docs/measuring.md says (callgrind, instruction counts), and put the figures in the report.lower-std, the benchmarks and tools/*.nika. The figure goes into the ADR’s §6 and into a comment here. It decides whether D2 (the two-scalar lowering, with shims at Rust crossings) is filed as a build issue.Done when
>` (`Code(u64)`) | 16 B | memory (sret) | 13 |
| `Result<(), Thrown>` | 16 B | registers | – |
| `Result<&str, Thrown>`, `Result<(i64, i64), …>` | 24 B | memory (sret) | – |
| pair `(i64, usize)`: second word 0 = success, else the tail word | 16 B | registers | 5 |
Findings:
* **Today ADR-023 D10 holds for no failing call that returns a value.** The tag
sits in the error's own byte, and Rust returns such an enum through memory
even at 16 bytes. `crates/nikaia-std/src/error.rs` asserts the size, not the
way it is returned.
* **The two-scalar pair** saves 4 instructions a call net (21.4 M against
17.4 M over 10⁶ calls) and the whole memory round trip.
* **`throwing` is not cold.** For `Thrown` LLVM inlines the trace
machinery, and every call, failing or not, pays two pushes and a 168-byte
frame (13 against 10 instructions).
* **Cranelift with its own convention (`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.
* **An inlined call has no ABI.** Everything above concerns calls LLVM does not
inline: across crates without LTO, recursion, large functions. How common
those are in real programs is **not measured yet**.
## 3. Options
* **A as it is**: keep `Result<T, Thrown>`. Breaks ADR-023 D10 as measured.
* **A with a two-scalar lowering**: in-value status, but written by the
lowering as two plain words Rust returns in registers. Holds ADR-023 D10 for
`T` up to one word; cannot hold it for a two-word `T`.
* **B, a status register**: on a backend that owns its convention. Holds
ADR-023 D10 for `T` up to two words.
* **B in the carry flag**: one register and one instruction fewer than B on
x86-64; reachable only where the throw and check sequences are hand-written.
## 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>`. 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
* **The guarantee was not met, and nobody saw it**, because the test checks
size, not convention. D1's test closes that.
* **D1 costs nothing and helps inlining.** The common small failing function
gets faster by being inlined, which is better than any return convention.
* **D2 is the most Rust can give.** A two-word `T` plus a status is three
words; Rust's convention has two registers for it. Only B gets past that.
* **D4 follows from issue #1235.** Designing the direct path's convention
without the status would mean redesigning it later.
* **D5 keeps the question honest.** callgrind counts instructions, not µops;
`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.
## 6. Open
* **How many failing calls are not inlined** in our programs (the compiler
through `lower-std`, the benchmarks, `tools/*.nika`), counted as
`docs/measuring.md` says. Gates D2.
* **The carry flag against the register**, issue #1235, step 7. Decides D5.
* **Whether the ledger narrows a call site's set to empty** where a proof makes
the only `throw` unreachable; such a call could take the plain convention.
## 7. Considered and rejected
* **Forbidding inlining of failing functions.** It would put every small
failing call back across a return convention, the one place the cost lives.
* **A as it is, ADR-023 D10 unchanged.** A normative guarantee that is not met
is worse than a precise one that is.
* **The carry flag on Cranelift or LLVM.** Neither models a flag across a
call; D4's register is their form of B.
* **A wider pair (three scalars) on the Rust backend.** Rust returns three
words through memory; nothing is gained over today.
## 8. Consequences
* `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.
* ADR-023: D10 rewritten per D3; §3.1 marked answered; §4's carry-flag entry
points to D5.
* ADR-280 §6: the in-value versus out-of-band question marked answered.
* Issue #1235: the direct path's convention carries the status register (D4);
step 7 adds the carry-flag measurement (D5).
* After §6's count: the two-scalar lowering (D2), with shims at Rust crossings.
</details>
---
_Generated by [Claude Code](https://claude.ai/code)_
## Comments
### keywan-ghadami, [2026-11-18](https://github.com/Nikaia-Foundation/Nikaia/issues/1525#issuecomment-6967664496)
Driver: #1525 is not queued, its form misses what a builder needs (`nikaia run scripts/issue-check.nika -- 1525`, docs/issues.md, #2003):
- possible missing Needs #1235: #1235 is open and named in *To build*, but no `Needs`, `Part of` or `Found while` line names it (docs/issues.md I4)
Label swapped from `ready` to `decision`: add what is missing (a `Needs #M` line for an issue it waits for, or `Part of #M`), then set `ready` again.
---
_Generated by [Claude Code](https://claude.ai/code)_
### keywan-ghadami, [2026-11-19](https://github.com/Nikaia-Foundation/Nikaia/issues/1525#issuecomment-6995940721)
Brief queued: issue-1525
---
agent: builder
model: opus
max_turns: 200
budget_usd: 30
effort: medium
---
#### Issue #1525
Your work order is issue #1525, inlined below with its comments. There is no
brief written around it: plan your own steps from its *To build* list, build
them in order, commit after each finished step, and end when its *Done when*
holds (or at the limits of `builder.md`, reporting what is left). The
facts in your prompt are *Core* of `docs/builder-facts.md`; more on a topic:
`nikaia run scripts/syntax.nika -- KEYWORD`, a probe: `nikaia run scripts/probe.nika -- SNIPPET`.
Measure the issue against `docs/issues.md` while you work: what it lacked,
what was wrong in it. That answer goes into the report's `Issue:` field.
A finding, a changed plan or a finished item also goes into the issue as a
comment (`gh api repos/Nikaia-Foundation/Nikaia/issues/1525/comments -F body=@file`,
ending with the Claude Code footer), rule I13.
#### Rules (CLAUDE.md; shared checkout)
* Read the facts in your prompt first. `develop` in the main checkout; other agents' uncommitted files are not yours: never touch or stage them.
* Commit locally after each step with `target/fast/nikaia run scripts/commit.nika -- REPORT` (it adds exactly the report's `Files:`); the driver pushes.
* Before committing, cheap checks only: `cargo build --profile fast -p nikaia`, `lower-std` and its fixpoint when a `tools/*.nika` changed, `cargo fmt`, `cargo clippy --all-targets -- -D warnings`, `nikaia run scripts/pre-push.nika`, the tests the issue names and the ones your change touches.
* Version lines, `CHANGELOG.md`, badges and NK codes are the supervisor's. New tests in Nikaia only: a Rust test file an older issue names is built as a `tests/compiler/tests/` program, no `Deviation:` and no `Issue:` point (I9, #1671).
* A defect you meet on the way to the issue's goal (a compiler or lowering bug in the path) you fix, with a test, as its own commit; so is one off the path but inside the issue's area that fits this run (owner, 2026-11-06), named in `Body:`. `Found:` is only for what lies outside the area, cannot be fixed this run or needs a decision; the driver files it as an issue.
* A form Nikaia, `std` or the toolchain lacks stops the work, unless an ADR or Part I-III refuses it (name it): `Result: blocked`, `Found:` the gap with a probe, `Needs: supervisor decision`.
* Disk: `NIKAIA_CACHE_LIMIT=2G`; at the end delete your own clone's `target/` and `target/release/nikaia-scratch`.
* A cause the issue marks `suspected`, or does not mark (`docs/issues.md` I7), is a hypothesis: run the check that settles it (`nikaia run scripts/where.nika -- NAME`, the probe with a trace, callgrind for a cost) before you build on it (owner, 2026-11-16). Shown false: say so in `Issue:` as `wrong I7: ` (the driver comments it on the issue) and follow the real cause.
* No cause after about 40 turns: stop, `Result: partial` with what you ruled out, the probe, and the suspected file and function.
#### Report (at most 15 lines, to exactly `/home/user/Nikaia/docs/briefs/performance/issue-1525.md.report.md`)
Fields: Result, Files, Commit, Body (optional), Fixes, Pushed, Tests, Gate, Deviation, Found, Needs, Changelog, Issue, Owner (a question for the owner), Comment (`#N `, what to say on another issue; the driver posts it, one line each), Friction, Disk freed, and last `Attribution:` with your commit trailer lines below it.
* `Result:`, `Gate:` (`green` or `red `), `Deviation:`, `Found:` and the other fields with nothing to say hold the bare word: `done`, `none`. Anything after it reads as content.
* `Result: done` only when the issue's *Done when* holds; `partial` with the items left otherwise.
* `Deviation:` only for an item of the issue built otherwise than written, or not built. `Found:` only for a defect outside your area, or one this run cannot fix: one defect per `Found:` line, with its probe and the output it gives, only the shapes that fail today, and the suspected file and function or `cause unknown` (#1695); a by-design behaviour is none (#1677). A correct program your own change refuses (a fail-closed check's false refusal) is no `Deviation:` but one `Found:` line per shape, with its probe (#1680). Everything else goes into `Body:`; how you verified goes into `Tests:`.
* `Issue:` by `docs/issues.md`, *How a run judges its issue*: `complete`, or `broke I: ...`, `rules-gap: ...`, `wrong I: ...`, joined with `;`; a point your own commit built gets `(built)` after its kind (`rules-gap (built): ...`), and the driver does not file it. `complete` is the bare word: prose after it (`complete; the earlier runs ...`) is filed as an improvement issue (#1931); history or a note for your own issue goes into `Body:` or `Comment: #N `.
* Final answer, one line: `done , report ` (or `BLOCKED: ...` / `PUSH REJECTED ...`).
---
_Generated by [Claude Code](https://claude.ai/code)_
### keywan-ghadami, [2026-11-19](https://github.com/Nikaia-Foundation/Nikaia/issues/1525#issuecomment-6996367722)
Run: issue-1525, Result: done
Tokens: input 140, cache read 5300127, cache write 117881, output 42420
Cost: 2.99 USD, time 23 min 34 s, turns 70
#### Report
Result: done
Files: docs/specification/adr/adr-failure-return-1.md docs/specification/adr/README.md docs/specification/adr/adr-023.md docs/specification/adr/adr-280.md crates/nikaia-std/src/error.rs tests/compiler/tests/a_failing_call_that_succeeds_touches_no_stack.nika tests/compiler/tests/a_failing_call_that_succeeds_touches_no_stack.stdout
Commit: three local commits: "ADR-failure-return-1: a failure's status travels beside the value, and the throw path is cold; ADR-023 D10 per backend", "The throw path is cold: throwing and the envelope constructors are never inlined (ADR-failure-return-1 D1)", "ADR-failure-return-1 §6: non-inlined failing calls counted, too few for the two-scalar lowering (D2)"
Body: ADR written in with links fixed, topic `failure-return` and an Errors index row added (Built: no, since only D1 is built); ADR-023 D10 rewritten per D3 with a Changed-by note, §3.1 marked answered, §4's carry-flag entry points to D5; ADR-280 §6 marked answered. `throwing`, `raise`, `Site::refill` and `From for Thrown` in error.rs are `#[cold] #[inline(never)]` (attributes only, the specification-first exception). The new test builds a project at opt-level 3, codegen-units 1, and counts with objdump the stack instructions (push/pop/%rsp) from entry to first ret of two `@inline(never)` failing functions. Step 5, callgrind, 10^6 non-inlined calls built against nikaia-std: fieldless error 10 -> 6 instructions a call (23.4 M -> 19.4 M total), one-word payload 12 -> 7 (31.4 M -> 26.4 M). Step 6 count, in the ADR §6: 462 failing tools functions, 177 survive out of line, 176 direct call sites of 8 477 to lowered functions; callgrind `check libraries.nika` (first 116 G Ir) 9 490 failing calls of 202 M lowered calls, `lower benches/template.nika` 3 975 of 184 k; benchmarks: fasta 3, k-nucleotide 7 calls a run. So D2 is not filed. The commits say Part of #1525 and none says Fixes: the issue is finished once the driver posts the two Comment lines; the supervisor closes it.
Fixes: #1525
Pushed: local 04b9c9eb
Tests: new a_failing_call_that_succeeds_touches_no_stack passes, and failed with error.rs from HEAD (one: 4, coded: 6 stack instructions); cargo test -p nikaia-std --lib error:: 18 passed; clippy -p nikaia-std --all-targets clean; cargo fmt --check clean; check-adr-refs green; pre-push green for each commit
Gate: green
Deviation: D1's test checks that the callee's success path has no frame and no stack instruction of its own; a value returned through memory (sret, `coded`'s 16-byte Result) is still stored to the caller's slot, which only D2 removes, so "touches no stack memory" is taken to mean the function's own stack
Found: none
Needs: none
Changelog: A call to a failing function that does not fail pays no frame of its own: the throw path is cold and never inlined (ADR-failure-return-1 D1)
Issue: wrong I3: D1's test as worded ("a non-failing call touches no stack memory") cannot pass before D2, because a 16-byte Result<i64, Thrown> with a payload still comes back through sret memory after D1 (the ADR's own §2 table says so); the test checks the callee's own frame instead; broke I4: #1235 is named in To build without a Part of/Needs line (the driver's comment already says so)
Comment: #1525 Measured (callgrind, 10^6 calls not inlined, opt-level 3): fieldless error 10 -> 6 instructions a call, one-word payload 12 -> 7, with the throw path cold. Count of non-inlined failing calls (ADR-failure-return-1 §6): 462 failing functions in tools/*.nika, 177 survive out of line, reached from 176 direct call sites against 8 477 call sites of lowered functions; `nikaia check compiler/libraries.nika` (first 116 G Ir) made 9 490 failing calls of 202 M lowered calls (0.005 %), `nikaia lower benches/template.nika` 3 975 of 184 k; benchmarks fail outside main only in fasta (3 calls a run) and k-nucleotide (7). D2 (the two-scalar lowering) would save under 0.02 % and is not filed.
Comment: #1235 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.
Owner: none
Friction: a callgrind run of `nikaia check crates/nikaia-std/src/compiler/libraries.nika` ran over 11 minutes (116 G Ir and still going), where docs/measuring.md and builder.md quote about 102 M instructions for the same command, so the cost figure in those docs is stale
Disk freed: scratch probe target and binaries (about 40 MB); target/nikaia-project-tests 1.8G, / 6.9G free, no new cache entry over 100 MB
Attribution:
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UVxJDukWgopUzVU2M1TjK7
#### Debrief
1. **Most turns:** making the D1 test fail before the fix. With two throw sites for one error type, LLVM already kept `throwing` out of line, so the test passed on the old code. A single throw site per error type fixed it. Close behind was a callgrind run of `check libraries.nika` that I killed after 11 minutes (116 G instructions); I used that partial sample for the count.
2. **Looked up:**
- Test programs build at opt-level 0 unless their manifest sets one. The pattern to copy was `list_push_keeps_its_growth_out_of_line`.
- A probe lowering needs `impl Error` on the error type, or it is refused with NK1161.
- How lowered symbols appear in `objdump -C`, so the count could separate failing functions from the rest.
3. **Checks run:** the new test, against the fixed and the unfixed `error.rs`; `error::` unit tests (18 passed); clippy and fmt on nikaia-std; ADR refs; pre-push for each commit; callgrind on a small rustc program plus two compiler runs.
- **Wasted:** most of the `libraries.nika` callgrind run, since `lower benches/template.nika` (132 M instructions) told the same story. Also the first call-site count, which wrongly counted jumps inside function bodies.
4. **One change:** the cost figure in builder.md / `docs/measuring.md` for `check libraries.nika` (about 102 M instructions) is wrong. It is over 116 G under callgrind. Name a small command to count instead.
5. **What the issue lacked:**
- `wrong I3`: D1's test text, "touches no stack memory", cannot pass before D2. The 16-byte Result with a payload still returns through sret. I tested the callee's own frame instead (push, pop, rsp).
- `broke I4`: #1235 is named in *To build* without `Part of` or `Needs`.
- `knowledge: Core | test programs build at opt-level 0 unless their manifest sets [build.] opt-level, so a codegen test builds a nested project.`
- **Not mine, but flagged:**
- The ADR's §2 table gives 13 instructions for `Code(u64)`. My before-fix run measured 12.
- My report has 21 lines, over the brief's 15.
6. `Fact: Core | a fails-before test of inlining needs a single call site per callee, because LLVM keeps a twice-used cold-ish function out of line on its own.`
`Fact: Core | callgrind of `nikaia check compiler/libraries.nika` runs past 100 G instructions (over 10 minutes); count on `nikaia lower benches/template.nika` instead.`
---
_Generated by [Claude Code](https://claude.ai/code)_
### keywan-ghadami, [2026-11-19](https://github.com/Nikaia-Foundation/Nikaia/issues/1525#issuecomment-6996368308)
From the report of the run of `issue-1525` (#1525):
Measured (callgrind, 10^6 calls not inlined, opt-level 3): fieldless error 10 -> 6 instructions a call, one-word payload 12 -> 7, with the throw path cold. Count of non-inlined failing calls (ADR-failure-return-1 §6): 462 failing functions in tools/*.nika, 177 survive out of line, reached from 176 direct call sites against 8 477 call sites of lowered functions; `nikaia check compiler/libraries.nika` (first 116 G Ir) made 9 490 failing calls of 202 M lowered calls (0.005 %), `nikaia lower benches/template.nika` 3 975 of 184 k; benchmarks fail outside main only in fasta (3 calls a run) and k-nucleotide (7). D2 (the two-scalar lowering) would save under 0.02 % and is not filed.
---
_Generated by [Claude Code](https://claude.ai/code)_
### keywan-ghadami, [2026-11-19](https://github.com/Nikaia-Foundation/Nikaia/issues/1525#issuecomment-6996369911)
The report of the run of `issue-1525` (#1525) judged this issue against `docs/issues.md`; the author fixes the issue by the rule it names (*How a run judges its issue*):
- Issue: wrong I3: D1's test as worded ("a non-failing call touches no stack memory") cannot pass before D2, because a 16-byte Result<i64, Thrown> with a payload still comes back through sret memory after D1 (the ADR's own §2 table says so); the test checks the callee's own frame instead
- Issue: broke I4: #1235 is named in To build without a Part of/Needs line (the driver's comment already says so)
---
_Generated by [Claude Code](https://claude.ai/code)_
[Back to Open Issues](/issues.html#decision)