target::runs_on beside target::has (amends ADR-371)Open and answer this issue on GitHub.
decision, future · opened 2026-11-29 · updated 2026-11-29 · 0 comments
May a program ask, while it runs, whether the machine has an instruction, and pick a faster path from the answer? Today it may not: ADR-371 D6 lets a program ask only the CPU level the build names (target::has in a comptime, --cpu), and its §4 rejects “a query of the CPU the program runs on: a run-time dispatch the compiler cannot fold, and a binary that carries both paths”.
Found while building SHA-256 with x86-64’s SHA extensions (the prototype on branch ccr-f235699b-y12s5g, commit “Prototype: SHA-256 in hardware chosen by the build’s CPU level”): target::Instruction::Sha256 is in no named x86-64 level (x86-64-v2 to -v4), so only --cpu=native turns it on, and a binary built for the baseline never uses it. That includes the released nikaia itself, which hashes its build cache, proofs file and ledger.
What the prototype measured (256 MiB, every program printing the same digest; times move from run to run on this machine and are only a hint):
| path | time per run |
|---|---|
Nikaia, --cpu=native (SHA extensions) |
175-191 ms |
| Nikaia, baseline (its own rounds) | 751-770 ms |
A - keep ADR-371 as it is. A program built for the baseline never uses an instruction its level lacks. Nothing to build. Against: a distributed binary is nearly always built for the baseline, so the gain reaches only those who build with --cpu=native.
B - a run-time question beside the build-time one, target::runs_on(i). The name says what is asked: the machine the program runs on, where target::has names the build’s level.
use std::target
comptime SHA_BUILT_IN: bool = target::has(target::Instruction::Sha256)
fn digest(data: ref Vec[u8]) -> Vec[u8] {
// At `--cpu=native` the question is answered while building and the other
// path is not in the binary; otherwise it is asked once per digest.
let hardware = SHA_BUILT_IN || target::runs_on(target::Instruction::Sha256)
...
}
Conditions in the amended ADR:
hash::sha256_block_hardware, ADR-platform-layer-v1) may be called only after target::has or target::runs_on said yes for its instruction.C - a question per domain, hash::sha256_hardware(). What the first prototype had. Against: every instruction would need its own function, and the name hides that the answer is the running machine’s.
B. It costs one branch per digest, a few hundred bytes for the second path, and one ADR amendment.
target::runs_on(i: target::Instruction) -> bool in std.contracts and crates/nikaia-std/src/target.rs (the detection on_this_machine already does for --cpu=native builds).crates/unsafe/sha256-ni: a second entry for a run-time path beside block, sound only after the run-time check, with the same #[inline(never)] on compress (the SHA instructions have only a legacy SSE encoding; inlined after 256-bit code each paid the AVX-SSE transition, a digest 100 times slower in the prototype)..nika test of target::runs_on and one that forces the portable path.A program built without --cpu takes the SHA extensions on a machine that has them and its own rounds elsewhere, with the same digest, and the ADR says so.