Skip to main content

polydat_core/compile/
mod.rs

1// Copyright 2024-2026 Jonathan Shook
2// SPDX-License-Identifier: Apache-2.0
3
4//! Kernel compilation: assembled DAG → fast executable kernel.
5//!
6//! Everything in this module is on the path between
7//! [`assembly::PolydatAssembler`] and an executable kernel. The
8//! pipeline:
9//!
10//! ```text
11//! PolydatAssembler ──resolve──▶ ResolvedDag ──compile_with(Engine)──┐
12//!   (fusion, adapters,                                              │
13//!    round-trip lint,          ┌────────────────────────────────────┤
14//!    topo sort)                ▼                  ▼                 ▼
15//!                  Interpreter(JitMode)  Closures(Provenance)  Native(Provenance)
16//!                  cone::extract_jit_cones  closures::           hybrid::
17//!                  → PolydatKernel          CompiledKernel*      HybridKernel*
18//! ```
19//!
20//! The host names the engine ([`select::Engine`]); under
21//! `Provenance::Auto` the selector picks the provenance mode from the
22//! graph's shape. Pure native code (`jit::JitKernel*`) is the
23//! differential tier behind the hybrid kernel.
24//!
25//! - [`assembly`]: the public construction surface
26//!   ([`assembly::PolydatAssembler`] + [`assembly::WireRef`]) and
27//!   the per-engine compile paths.
28//! - [`fusion`]: graph-level subgraph fusion pass; runs during
29//!   assembly after wiring resolution.
30//! - [`roundtrip_lint`]: the structural type-round-trip lint run at
31//!   resolution.
32//! - [`cone`]: cone-level JIT inside the interpreter kernel
33//!   (SRD-105), under a [`cone::JitMode`].
34//! - [`lattice`]: the engine-mix report of a compiled program.
35//! - [`select`]: the engine and provenance enums, and the heuristic
36//!   that picks a provenance mode under `Provenance::Auto`.
37//! - [`closures`]: the closure tier, one generated op per node over
38//!   a flat u64 slot buffer, by-reference outputs as `Ref2` pairs.
39//! - [`hybrid`]: the native engine (native segments + closure steps
40//!   sharing a flat u64 buffer).
41//! - [`marshal`]: the boundary marshalling between slots and `Value`s.
42//! - `externs`: extern inputs and `shared` cells on the compiled
43//!   engines.
44//! - [`simd_plan`], `simd_tier1`: scalar-flow SIMD promotion.
45//! - `jit`: Cranelift lowering and the pure native kernels
46//!   (feature-gated on `jit`).
47
48pub mod assembly;
49pub mod closures;
50pub mod cone;
51#[cfg(all(test, feature = "jit"))]
52mod cone_tests;
53pub(crate) mod externs;
54pub mod fusion;
55#[cfg(feature = "jit")]
56pub(crate) mod fusion_units;
57pub mod hybrid;
58#[cfg(feature = "jit")]
59pub mod jit;
60pub mod lattice;
61/// The boundary marshalling a compiled node kit reads its arguments
62/// and writes its outputs through (compiled_handles.md §4): a node
63/// crate's kits use it as the core's own do.
64pub mod marshal;
65pub mod roundtrip_lint;
66pub mod select;
67pub mod simd_plan;
68#[cfg(feature = "jit")]
69pub mod simd_tier1;
70
71/// Axiom S2 typed accessors, shared by the P2 and hybrid kernel
72/// types (both expose `self.core.ref_entry(slot)`). Each returns
73/// a borrow whose lifetime ties to `&self`, so the borrow checker
74/// statically prevents holding a slice across the next
75/// `eval(&mut self)` — stale Ref reads are compile errors.
76macro_rules! ref_readers {
77    () => {
78        /// Borrow a `vec_f32` output's current contents.
79        pub fn read_vec_f32(&self, slot: usize) -> &[f32] {
80            match self.core.ref_entry(slot) {
81                crate::ast::ScratchBuf::F32(v) => v,
82                other => panic!("slot {slot} is not f32-lane scratch: {other:?}"),
83            }
84        }
85        /// Borrow a `vec_f64` output's current contents.
86        pub fn read_vec_f64(&self, slot: usize) -> &[f64] {
87            match self.core.ref_entry(slot) {
88                crate::ast::ScratchBuf::F64(v) => v,
89                other => panic!("slot {slot} is not f64-lane scratch: {other:?}"),
90            }
91        }
92        /// Borrow a `vec_f16` output's current contents.
93        pub fn read_vec_f16(&self, slot: usize) -> &[half::f16] {
94            match self.core.ref_entry(slot) {
95                crate::ast::ScratchBuf::F16(v) => v,
96                other => panic!("slot {slot} is not f16-lane scratch: {other:?}"),
97            }
98        }
99        /// Borrow a `vec_i8` output's current contents.
100        pub fn read_vec_i8(&self, slot: usize) -> &[i8] {
101            match self.core.ref_entry(slot) {
102                crate::ast::ScratchBuf::I8(v) => v,
103                other => panic!("slot {slot} is not i8-lane scratch: {other:?}"),
104            }
105        }
106        /// Borrow a `vec_i16` output's current contents.
107        pub fn read_vec_i16(&self, slot: usize) -> &[i16] {
108            match self.core.ref_entry(slot) {
109                crate::ast::ScratchBuf::I16(v) => v,
110                other => panic!("slot {slot} is not i16-lane scratch: {other:?}"),
111            }
112        }
113        /// Borrow a `vec_i32` output's current contents.
114        pub fn read_vec_i32(&self, slot: usize) -> &[i32] {
115            match self.core.ref_entry(slot) {
116                crate::ast::ScratchBuf::I32(v) => v,
117                other => panic!("slot {slot} is not i32-lane scratch: {other:?}"),
118            }
119        }
120        /// Borrow a `vec_i64` output's current contents.
121        pub fn read_vec_i64(&self, slot: usize) -> &[i64] {
122            match self.core.ref_entry(slot) {
123                crate::ast::ScratchBuf::I64(v) => v,
124                other => panic!("slot {slot} is not i64-lane scratch: {other:?}"),
125            }
126        }
127    };
128}
129pub(crate) use ref_readers;
130
131/// The accessors every compiled kernel type carries, whichever tier it
132/// belongs to: reading an output by name or by slot, the externs and
133/// the cursors it declares, and applying the coordinates a `Kernel`
134/// caller left pending before a pull or an eval. Every one of them
135/// delegates to `self.core`, so none decides anything about evaluation
136/// — which is why seven types can share one copy.
137///
138/// `$set_coords` is the coordinate writer the type uses: the provenance
139/// modes that track a changed-input mask apply coordinates through
140/// `set_inputs`, the rest through `set_coords`. It is the only thing
141/// that varies, and the closure tier and the hybrid had a macro each to
142/// vary it.
143macro_rules! kernel_accessors {
144    ($set_coords:ident) => {
145        /// How many inputs are coordinates. (The core's `coord_count`
146        /// field counts the buffer slots all inputs occupy.)
147        pub fn coord_count(&self) -> usize {
148            self.core.externs.coordinate_count()
149        }
150
151        /// The slot of a named output.
152        pub fn resolve_output(&self, name: &str) -> Option<usize> {
153            self.core.output_map.get(name).copied()
154        }
155
156        /// Read an output by pre-resolved slot index. Panics on
157        /// Ref2-colored slots (axiom S2) — use `read_vec_*`.
158        #[inline]
159        pub fn get_slot(&self, slot: usize) -> u64 {
160            self.core.guard_ref_slot(slot);
161            self.core.buffer[slot]
162        }
163
164        /// Read a named output variate after `eval()`. Panics on
165        /// Ref2-colored outputs (axiom S2) — use `read_vec_*`.
166        #[inline]
167        pub fn get(&self, name: &str) -> u64 {
168            let slot = self.core.output_map[name];
169            self.core.guard_ref_slot(slot);
170            self.core.buffer[slot]
171        }
172
173        /// The named output as a typed `Value`, decoded by its port
174        /// type: a `Ref2` output is copied out through its pair
175        /// (compiled_handles.md §4), so the caller never holds a
176        /// pointer; a slot that holds `None` reads as `None`.
177        pub fn get_value(&self, name: &str) -> crate::ast::Value {
178            self.core.value_of(name)
179        }
180
181        /// A named output, its cone run if a write is pending.
182        pub fn pull_output(&mut self, name: &str) -> crate::ast::Value {
183            self.core.pull_named(name)
184        }
185
186        /// The named output through the `Kernel` trait: the pending
187        /// coordinates are applied, a round begins if a write is pending, and
188        /// only the output's cone runs.
189        fn pull_value(&mut self, name: &str) -> crate::ast::Value {
190            self.apply_pending_coords();
191            self.pull_output(name)
192        }
193
194        /// [`Self::pull_value`] by output index.
195        fn pull_value_at(&mut self, index: usize) -> crate::ast::Value {
196            self.apply_pending_coords();
197            self.core.pull_at(index)
198        }
199
200        /// The coordinates of a pending write, applied once: a pull
201        /// after the first in a round finds nothing written and skips
202        /// the comparison. The round itself begins in the core, which
203        /// clears the pending flag.
204        #[inline]
205        fn apply_pending_coords(&mut self) {
206            if self.core.drive.stale {
207                let coords = std::mem::take(&mut self.core.drive.coords);
208                self.$set_coords(&coords);
209                self.core.drive.coords = coords;
210            }
211        }
212
213        /// `eval` through the `Kernel` trait: the pending coordinates,
214        /// then every step.
215        fn eval_pending(&mut self) {
216            let coords = std::mem::take(&mut self.core.drive.coords);
217            self.eval(&coords);
218            self.core.drive.coords = coords;
219        }
220
221        /// The kernel's externs by name and declared type.
222        pub fn externs(&self) -> Vec<(&str, crate::ast::PortType)> {
223            self.core.externs.names()
224        }
225
226        /// The cursors the program declares, with the partitions the
227        /// compiler resolved where its `over` clause and extent were
228        /// constant, as `PolydatProgram::cursor_schemas` reports them.
229        pub fn cursor_schemas(&self) -> &[crate::iteration::source::SourceSchema] {
230            self.core.externs.cursor_schemas()
231        }
232
233        /// Narrow a cursor to one partition, as `narrow_cursor` does on
234        /// the interpreter: its `Ext` slot and six scalar projections
235        /// are set as externs.
236        pub fn set_cursor(
237            &mut self,
238            name: &str,
239            partition: &crate::iteration::cursor_partition::Partition,
240        ) -> Result<(), crate::kernel::WriteError> {
241            for (slot, value) in self.core.externs.cursor_writes(name, partition)? {
242                self.set_input(&slot, value)?;
243            }
244            Ok(())
245        }
246
247        crate::compile::ref_readers!();
248    };
249}
250pub(crate) use kernel_accessors;
251
252/// SRD-74's fusion rule: whether a node may join the run of fused
253/// code being formed, as far as `None` is concerned. The one predicate
254/// both fusers apply — the interpreter's cone planner and the hybrid's
255/// segment batcher — so that what one admits the other admits.
256///
257/// Fused code answers a `None` on a boundary input by making every one
258/// of its outputs `None`, because native code cannot carry one. That is
259/// SRD-74 Rule 1 and it is the right answer for a node that propagates
260/// a `None`. It is the wrong answer for a node that *consumes* one and
261/// keeps going — `to_json` writes `null`, a `printf` with an `Option`
262/// argument writes its own text — so such a node may join only when
263/// every one of its inputs comes from inside, where a `None` cannot
264/// arrive: either no boundary input is `None` and the run proceeds
265/// normally, or one is and the whole run answers `None` without any
266/// member running at all.
267///
268/// `eligible` is indexed by node and filled in topological order, so a
269/// node's producers are decided before it is.
270#[cfg(feature = "jit")]
271pub(crate) fn none_rule_admits(
272    accepts_none: bool,
273    wiring: &[crate::kernel::WireSource],
274    eligible: &[bool],
275) -> bool {
276    !accepts_none
277        || wiring
278            .iter()
279            .all(|src| matches!(src, crate::kernel::WireSource::NodeOutput(j, _) if eligible[*j]))
280}
281
282/// The highest tier a node can reach, given the types of the wires
283/// feeding it: the one answer to a question four places were asking
284/// separately.
285///
286/// The order is the one every builder walks. Native first, since a node
287/// with a lowering joins a segment; then the compiled forms, in the
288/// order `assembly::node_step_op` tries them — a copy step, the scalar
289/// `compiled_u64`, the node's own slot kit; then the interpreter.
290///
291/// The wire types are not optional. `compiled_slot` is offered per call
292/// site with the types the kernel fixed, so a caller without them can
293/// only ask the first two questions, and the three callers that
294/// reported a tier rather than selecting one did exactly that — they
295/// asked `compiled_u64().is_some()` and called a node with a slot kit
296/// `Phase1`, which is what the binary printed for `printf`.
297pub fn node_tier(
298    node: &dyn crate::ast::PolydatNode,
299    wire_types: &[crate::ast::PortType],
300) -> crate::ast::CompileLevel {
301    #[cfg(feature = "jit")]
302    if !matches!(
303        crate::compile::jit::classify_node_typed(node, wire_types),
304        crate::compile::jit::JitOp::Fallback
305    ) {
306        return crate::ast::CompileLevel::Phase3;
307    }
308    let meta = node.meta();
309    let is_copy =
310        (meta.name == "identity" || meta.name.starts_with("__port_")) && meta.outs.len() == 1;
311    let has_kit = node
312        .compiled_slot(
313            wire_types,
314            crate::compile::select::Engine::Closures(crate::compile::select::Provenance::Auto),
315        )
316        .is_some();
317    if is_copy || node.compiled_u64().is_some() || has_kit {
318        crate::ast::CompileLevel::Phase2
319    } else {
320        crate::ast::CompileLevel::Phase1
321    }
322}
323
324/// The provenance of every buffer slot: which input slots reach it,
325/// as an exact multi-word mask, for the pull-side cone guard of every
326/// compiled kernel. `input_dependents` is indexed by input slot (a
327/// multi-slot input repeats its list per slot) and lists the steps
328/// downstream of that slot; `step_output_slots` gives each step's
329/// output slots, which all take the step's mask. A coordinate slot's
330/// provenance is itself.
331pub(crate) fn slot_provenance(
332    coord_count: usize,
333    total_slots: usize,
334    step_output_slots: &[&[usize]],
335    input_dependents: &[Vec<usize>],
336) -> Vec<crate::kernel::ProvMask> {
337    use crate::kernel::ProvMask;
338    let step_count = step_output_slots.len();
339    let mut step_prov: Vec<ProvMask> = (0..step_count).map(|_| ProvMask::empty()).collect();
340    for (input_slot, deps) in input_dependents.iter().enumerate() {
341        for &step in deps {
342            if step < step_count {
343                step_prov[step].set(input_slot);
344            }
345        }
346    }
347    let mut slots: Vec<ProvMask> = (0..total_slots).map(|_| ProvMask::empty()).collect();
348    for (i, slot) in slots.iter_mut().enumerate().take(coord_count) {
349        slot.set(i);
350    }
351    for (step, outs) in step_output_slots.iter().enumerate() {
352        for &slot in outs.iter() {
353            if slot < slots.len() {
354                slots[slot] = step_prov[step].clone();
355            }
356        }
357    }
358    slots
359}
360
361/// The coordinates a host set last on a compiled kernel and whether
362/// they have been evaluated: what the [`Kernel`](crate::kernel::Kernel)
363/// trait's `set_inputs` and `pull` keep between calls.
364#[derive(Clone, Default)]
365pub(crate) struct Drive {
366    pub(crate) coords: Vec<u64>,
367    pub(crate) stale: bool,
368}
369
370/// The slot surface of a compiled kernel: the extended API, over and
371/// above the [`Kernel`](crate::kernel::Kernel) trait every engine
372/// answers.
373///
374/// Every compiled engine lays its program out over one flat `u64` slot
375/// buffer (engines.md §6). That layout is an implementation detail, and
376/// this trait is where it is admitted: a slot index instead of an
377/// output name, a raw `u64` instead of a `Value`, a borrow into the
378/// scratch a by-reference output writes. The interpreter does not
379/// implement it and cannot — its buffers are typed `Value`s and it has
380/// no slot to name — which is the point: the shape of this trait *is*
381/// the thing the compiled tiers share and the interpreter does not.
382///
383/// **This is not the surface for running a program.** Driving a kernel
384/// is `Kernel`, on every engine, and a host that never names an engine
385/// never sees this trait. Reach for it when the implementation detail
386/// is the subject: a differential test asserting on what was laid out,
387/// a benchmark measuring a tier without the `Value` construction and
388/// the name lookup a `pull` pays, a diagnostic reporting on a slot.
389///
390/// It is a subtrait rather than a wider `Kernel`, so it is opt-in at
391/// the import: a caller who does not write `use SlotKernel` does not
392/// have these methods on their kernel at all. And it is reachable
393/// without naming a kernel type, through
394/// [`PolydatAssembler::compile_slots`](crate::compile::assembly::PolydatAssembler::compile_slots),
395/// which hands back a `Box<dyn SlotKernel>` that upcasts to
396/// `Box<dyn Kernel>` wherever the ordinary surface will do.
397pub trait SlotKernel: crate::kernel::Kernel {
398    /// The buffer slot a named output writes, resolved once so a
399    /// caller reading the same output every cycle pays no lookup.
400    fn resolve_output(&self, name: &str) -> Option<usize>;
401
402    /// The raw `u64` in `slot`, as it stands: no evaluation, no
403    /// decoding. Panics on a `Ref2` slot (axiom S2), which has no
404    /// scalar to read — use the `read_vec_*` borrows.
405    fn get_slot(&self, slot: usize) -> u64;
406
407    /// [`Self::get_slot`] by output name.
408    fn get(&self, name: &str) -> u64;
409
410    /// A named output decoded by its port type, a `Ref2` output copied
411    /// out through its pair so the caller never holds a pointer. Reads
412    /// what is there; [`Kernel::pull`](crate::kernel::Kernel::pull)
413    /// evaluates first.
414    fn get_value(&self, name: &str) -> crate::ast::Value;
415
416    /// Set the coordinates, evaluate what `slot` needs, and return its
417    /// raw `u64`. The whole read in one call and one `u64`, which is
418    /// what a tier benchmark wants: `pull_at` gives the same value
419    /// through a `Value` it has to construct.
420    fn eval_for_slot(&mut self, coords: &[u64], slot: usize) -> u64;
421
422    /// Set the coordinates and run every step, the whole program in
423    /// one call. [`Kernel::eval`](crate::kernel::Kernel::eval) is the
424    /// same evaluation over coordinates already written with
425    /// `set_inputs`; this is the form that takes them, which is what a
426    /// loop over a coordinate range wants.
427    fn eval_at(&mut self, coords: &[u64]);
428
429    /// Borrow a `vec_f32` output's current contents. The borrow ties to
430    /// `&self`, so holding one across the next evaluation is a compile
431    /// error rather than a stale read (axiom S2).
432    fn read_vec_f32(&self, slot: usize) -> &[f32];
433    /// Borrow a `vec_f64` output's current contents.
434    fn read_vec_f64(&self, slot: usize) -> &[f64];
435    /// Borrow a `vec_f16` output's current contents.
436    fn read_vec_f16(&self, slot: usize) -> &[half::f16];
437    /// Borrow a `vec_i8` output's current contents.
438    fn read_vec_i8(&self, slot: usize) -> &[i8];
439    /// Borrow a `vec_i16` output's current contents.
440    fn read_vec_i16(&self, slot: usize) -> &[i16];
441    /// Borrow a `vec_i32` output's current contents.
442    fn read_vec_i32(&self, slot: usize) -> &[i32];
443    /// Borrow a `vec_i64` output's current contents.
444    fn read_vec_i64(&self, slot: usize) -> &[i64];
445}
446
447/// [`SlotKernel`] for a compiled kernel, forwarding to the inherent
448/// methods the type already has. The trait is the surface; the
449/// inherent copies are what it forwards to and what this crate calls.
450macro_rules! impl_slot_kernel {
451    ($ty:ident) => {
452        impl crate::compile::SlotKernel for $ty {
453            fn resolve_output(&self, name: &str) -> Option<usize> {
454                $ty::resolve_output(self, name)
455            }
456            fn get_slot(&self, slot: usize) -> u64 {
457                $ty::get_slot(self, slot)
458            }
459            fn get(&self, name: &str) -> u64 {
460                $ty::get(self, name)
461            }
462            fn get_value(&self, name: &str) -> crate::ast::Value {
463                $ty::get_value(self, name)
464            }
465            fn eval_for_slot(&mut self, coords: &[u64], slot: usize) -> u64 {
466                $ty::eval_for_slot(self, coords, slot)
467            }
468            fn eval_at(&mut self, coords: &[u64]) {
469                $ty::eval(self, coords)
470            }
471            fn read_vec_f32(&self, slot: usize) -> &[f32] {
472                $ty::read_vec_f32(self, slot)
473            }
474            fn read_vec_f64(&self, slot: usize) -> &[f64] {
475                $ty::read_vec_f64(self, slot)
476            }
477            fn read_vec_f16(&self, slot: usize) -> &[half::f16] {
478                $ty::read_vec_f16(self, slot)
479            }
480            fn read_vec_i8(&self, slot: usize) -> &[i8] {
481                $ty::read_vec_i8(self, slot)
482            }
483            fn read_vec_i16(&self, slot: usize) -> &[i16] {
484                $ty::read_vec_i16(self, slot)
485            }
486            fn read_vec_i32(&self, slot: usize) -> &[i32] {
487                $ty::read_vec_i32(self, slot)
488            }
489            fn read_vec_i64(&self, slot: usize) -> &[i64] {
490                $ty::read_vec_i64(self, slot)
491            }
492        }
493    };
494}
495pub(crate) use impl_slot_kernel;
496
497/// The [`Kernel`](crate::kernel::Kernel) impl every compiled kernel
498/// shares: the type's inherent `eval_pending`, `pull_value`,
499/// `pull_value_at`, `set_input`, `set_input_at`, `set_cursor`,
500/// `mark_all_dirty`, and a `core` with `drive`, `externs`,
501/// `coord_count`, `output_types`, `output_map`, `buffer`,
502/// `traversals`, and `plan`/`invalidate_all`/`attach_cell`/
503/// `slot_value`.
504macro_rules! impl_kernel_trait {
505    ($ty:ident) => {
506        impl crate::kernel::Kernel for $ty {
507            fn engine(&self) -> crate::compile::select::Engine {
508                self.core.engine
509            }
510            fn set_inputs(&mut self, coords: &[u64]) {
511                self.core.drive.coords.clear();
512                self.core.drive.coords.extend_from_slice(coords);
513                self.core.drive.stale = true;
514            }
515            fn set_input(
516                &mut self,
517                name: &str,
518                value: crate::ast::Value,
519            ) -> Result<(), crate::kernel::WriteError> {
520                if self.core.externs.is_const_name(name) {
521                    return Err(crate::kernel::WriteError::ConstSlot {
522                        slot: name.to_string(),
523                    });
524                }
525                self.core.drive.stale = true;
526                $ty::set_input(self, name, value)
527            }
528            fn const_inits(&self) -> &[crate::kernel::ConstInit] {
529                self.core.externs.const_inits()
530            }
531            fn init_input_at(
532                &mut self,
533                index: usize,
534                value: crate::ast::Value,
535            ) -> Result<(), crate::kernel::WriteError> {
536                self.core.drive.stale = true;
537                $ty::set_input_at(self, index, value)
538            }
539            fn set_cursor(
540                &mut self,
541                name: &str,
542                partition: &crate::iteration::cursor_partition::Partition,
543            ) -> Result<(), crate::kernel::WriteError> {
544                self.core.drive.stale = true;
545                $ty::set_cursor(self, name, partition)
546            }
547            fn eval(&mut self) {
548                self.eval_pending();
549                self.core.drive.stale = false;
550            }
551            fn pull(&mut self, name: &str) -> crate::ast::Value {
552                self.pull_value(name)
553            }
554            fn input_names(&self) -> Vec<String> {
555                self.core.externs.input_names().to_vec()
556            }
557            /// In declaration order, as the interpreter lists them: the
558            /// assembler sets them on every compiled kernel.
559            fn output_names(&self) -> Vec<String> {
560                self.core.externs.output_names().to_vec()
561            }
562            fn output_type(&self, name: &str) -> Option<crate::ast::PortType> {
563                self.core.output_types.get(name).copied()
564            }
565            fn externs(&self) -> Vec<(String, crate::ast::PortType)> {
566                self.core
567                    .externs
568                    .names()
569                    .into_iter()
570                    .map(|(n, t)| (n.to_string(), t))
571                    .collect()
572            }
573            fn cursor_schemas(&self) -> &[crate::iteration::source::SourceSchema] {
574                self.core.externs.cursor_schemas()
575            }
576            fn input_value(&self, name: &str) -> Option<crate::ast::Value> {
577                self.core.externs.value(name).or_else(|| {
578                    let i = self
579                        .core
580                        .externs
581                        .input_names()
582                        .iter()
583                        .position(|n| n == name)?;
584                    if i < self.core.externs.coordinate_count() {
585                        let pending = self.core.drive.coords.get(i).copied();
586                        Some(crate::ast::Value::U64(
587                            pending.unwrap_or(self.core.buffer[i]),
588                        ))
589                    } else {
590                        None
591                    }
592                })
593            }
594            fn traversals(&self) -> &[crate::dsl::traversal::Traversal] {
595                &self.core.traversals
596            }
597            fn plan(&self) -> crate::EnginePlan {
598                self.core.plan()
599            }
600            fn input_index(&self, name: &str) -> Option<usize> {
601                self.core
602                    .externs
603                    .input_names()
604                    .iter()
605                    .position(|n| n == name)
606            }
607            fn set_input_at(
608                &mut self,
609                index: usize,
610                value: crate::ast::Value,
611            ) -> Result<(), crate::kernel::WriteError> {
612                if self.core.externs.is_const_index(index) {
613                    return Err(crate::kernel::WriteError::ConstSlot {
614                        slot: self.core.externs.input_names()[index].clone(),
615                    });
616                }
617                self.core.drive.stale = true;
618                $ty::set_input_at(self, index, value)
619            }
620            fn output_index(&self, name: &str) -> Option<usize> {
621                self.core
622                    .externs
623                    .output_names()
624                    .iter()
625                    .position(|n| n == name)
626            }
627            fn pull_at(&mut self, index: usize) -> crate::ast::Value {
628                self.pull_value_at(index)
629            }
630            fn traverse(&mut self, index: usize) -> Result<crate::kernel::TraversalStream, String> {
631                let traversal = self.core.traversals.get(index).cloned().ok_or_else(|| {
632                    format!(
633                        "no traversal at index {index}; the program declares {}",
634                        self.core.traversals.len()
635                    )
636                })?;
637                crate::kernel::activation::open_traversal(self, traversal)
638            }
639            fn invalidate_all(&mut self) {
640                self.mark_all_dirty();
641                self.core.invalidate_all();
642            }
643            fn shared_cells(&self) -> Vec<crate::kernel::SharedCellEntry> {
644                self.core.externs.shared_cells()
645            }
646            fn output_cell(&self, name: &str) -> Option<crate::kernel::SharedCell> {
647                self.core.output_cell_for(name)
648            }
649            fn output_modifier(&self, name: &str) -> crate::dsl::ast::BindingModifier {
650                self.core.externs.output_modifier(name)
651            }
652            fn cells_in_scope(&self) -> Vec<crate::kernel::SharedCellEntry> {
653                self.core.externs.cells_in_scope()
654            }
655            fn set_transit_cells(&mut self, cells: Vec<crate::kernel::SharedCellEntry>) {
656                self.core.externs.set_transit_cells(cells);
657            }
658            fn input_port_type(&self, name: &str) -> Option<crate::ast::PortType> {
659                self.core.externs.input_port_type(name)
660            }
661            fn bind_input_cell(&mut self, name: &str, cell: crate::kernel::SharedCell) -> bool {
662                let Some(slot) = self.core.externs.bind_cell(name, cell) else {
663                    return false;
664                };
665                // The slot's value is the cell's from the next refresh,
666                // so everything downstream of it reruns.
667                self.core.dirty_input(slot);
668                true
669            }
670            fn attach_shared_cell(
671                &mut self,
672                name: &str,
673                cell: crate::kernel::SharedCell,
674            ) -> Result<(), String> {
675                self.core.attach_cell(name, cell)
676            }
677            fn into_program(
678                mut self: Box<Self>,
679            ) -> std::sync::Arc<dyn crate::kernel::KernelProgram> {
680                self.mark_all_dirty();
681                self.core.drive.stale = true;
682                std::sync::Arc::new(crate::kernel::SharedKernel(*self))
683            }
684            fn ledger(&self) -> &std::sync::Arc<crate::kernel::CompileLedger> {
685                self.core.externs.ledger()
686            }
687            fn coord_count(&self) -> usize {
688                self.core.externs.coordinate_count()
689            }
690            fn input_value_at(&self, index: usize) -> Option<crate::ast::Value> {
691                if index < self.core.externs.coordinate_count() {
692                    let pending = self.core.drive.coords.get(index).copied();
693                    return Some(crate::ast::Value::U64(
694                        pending.unwrap_or(self.core.buffer[index]),
695                    ));
696                }
697                self.core.externs.value_at(index)
698            }
699            fn input_default_at(&self, index: usize) -> Option<crate::ast::Value> {
700                if index < self.core.externs.coordinate_count() {
701                    return Some(crate::ast::Value::U64(0));
702                }
703                self.core.externs.default_at(index)
704            }
705            fn input_is_cell_bound(&self, index: usize) -> bool {
706                self.core.externs.is_cell_bound_at(index)
707            }
708            fn reset_inputs(&mut self) {
709                let count = self.core.externs.input_names().len();
710                for index in self.core.externs.coordinate_count()..count {
711                    if self.core.externs.is_cell_bound_at(index) {
712                        continue;
713                    }
714                    let (Some(now), Some(default)) = (
715                        self.core.externs.value_at(index),
716                        self.core.externs.default_at(index),
717                    ) else {
718                        continue;
719                    };
720                    if now != default {
721                        // The typed write, so what depends on the input
722                        // is marked as any write marks it. A declared
723                        // default satisfies its own slot.
724                        let _ = crate::kernel::Kernel::set_input_at(self, index, default);
725                    }
726                }
727            }
728            fn fork(&self) -> Box<dyn crate::kernel::Kernel> {
729                // A clone is a new state of the same program with this
730                // one's values: shared slots keep their cells, transit
731                // cells travel, and broadcast cells stay with the
732                // original, which is what descendants are bound to.
733                Box::new(self.clone())
734            }
735            fn publish_broadcasts(&mut self) {
736                if !self.core.externs.broadcasts() {
737                    return;
738                }
739                let names: Vec<String> = self.core.externs.output_names().to_vec();
740                for name in names {
741                    let Some(&slot) = self.core.output_map.get(&name) else {
742                        continue;
743                    };
744                    if self.core.externs.published_output(slot).is_none() {
745                        continue;
746                    }
747                    // The pull by name publishes through the cell. A
748                    // failure is left for the pull that needs the value.
749                    let _ = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
750                        self.pull_value(&name);
751                    }));
752                }
753            }
754            fn commit_write_throughs(&mut self) -> Result<(), String> {
755                let pairs = self.core.externs.write_throughs().to_vec();
756                let mut pending = Vec::with_capacity(pairs.len());
757                for (export, source) in &pairs {
758                    let Some(slot_type) = self.core.externs.input_port_type(export) else {
759                        continue;
760                    };
761                    let value = self.pull_value(source);
762                    let value =
763                        crate::kernel::check_write_through_type(export, source, slot_type, value)?;
764                    pending.push((export.clone(), value));
765                }
766                for (export, value) in pending {
767                    crate::kernel::Kernel::set_input(self, &export, value)
768                        .map_err(|e| format!("write-through into `{export}`: {e}"))?;
769                }
770                Ok(())
771            }
772            fn program_id(&self) -> crate::kernel::ProgramId {
773                crate::kernel::ProgramId(self.core.program_identity())
774            }
775            fn input_type_origin(&self, name: &str) -> Option<crate::kernel::TypeOrigin> {
776                self.core.externs.input_type_origin(name)
777            }
778        }
779
780        impl crate::kernel::KernelInternals for $ty {
781            fn set_write_throughs(&mut self, pairs: Vec<(String, String)>) {
782                self.core.externs.set_write_throughs(pairs);
783            }
784            /// A compiled kernel keeps the traversals; each carries the
785            /// comprehension its producer resolved to at compile time.
786            fn set_traversals(
787                &mut self,
788                traversals: Vec<crate::dsl::traversal::Traversal>,
789                _producers: Vec<crate::dsl::traversal::Producer>,
790            ) {
791                self.core.traversals = traversals.into();
792            }
793            fn slot_value(&self, slot: usize, ty: crate::ast::PortType) -> crate::ast::Value {
794                self.core.slot_value(slot, ty)
795            }
796            fn folded_value(&self, name: &str) -> Option<crate::ast::Value> {
797                let slot = *self.core.output_map.get(name)?;
798                let ty = *self.core.output_types.get(name)?;
799                Some(self.core.slot_value(slot, ty))
800            }
801            fn set_cursor_extent(&mut self, index: usize, extent: u64) {
802                self.core.externs.set_cursor_extent(index, extent);
803            }
804            fn reset_to_program(&mut self) {
805                self.core.externs.reset_to_program(&mut self.core.buffer);
806                self.mark_all_dirty();
807            }
808        }
809    };
810}
811pub(crate) use impl_kernel_trait;
812
813/// The bookkeeping every compiled engine keeps, whatever its steps
814/// are: the evaluation round and what ran in it, the clean flags and
815/// what a write dirties, the extern writes and the cell refresh, the
816/// reference pairs a step publishes, and reading an output back.
817///
818/// Both compiled cores carry the same fields for these and, until this
819/// macro, the same eighteen method bodies byte for byte. None of them
820/// touches the step list, which is the one thing the two tiers
821/// genuinely differ about: a step on the closure tier is always a
822/// closure, and on the native tier it is a closure or a run of native
823/// code. That difference lives in the run loops, which stay per tier.
824macro_rules! shared_core_methods {
825    () => {
826        /// Axiom S9: every reference pair in the buffer names the
827        /// scratch entry that owns it. A slot is skipped when nothing
828        /// has been published into it — its step has not run, or it
829        /// carries `None`.
830        ///
831        /// The two tiers wrote this assertion separately and their
832        /// skip predicates had drifted apart: one skipped a `None`
833        /// slot only when a step owned it, the other whenever the
834        /// slot was `None`. Nothing is published either way, so the
835        /// looser test is the right one and is now the only one.
836        ///
837        /// Axiom S2 typed accessor core: resolve a Ref pair's first
838        /// slot to its kernel-owned scratch entry. The returned
839        /// borrow ties to `&self`, so holding it across the next
840        /// `eval(&mut self)` is a compile error — stale reads are
841        /// statically impossible.
842        fn ref_entry(&self, slot: usize) -> &crate::ast::ScratchBuf {
843            match self.ref_scratch.iter().find(|(s, _)| *s == slot) {
844                Some(&(_, idx)) => &self.scratch[idx],
845                None if self.ref_slots.get(slot).copied().unwrap_or(false) => panic!(
846                    "slot {slot} is a Ref pair owned by the CALLER (a kernel \
847                     input) — read it on the caller side"
848                ),
849                None => panic!("slot {slot} is not a Ref2-colored slot"),
850            }
851        }
852
853        /// Run `body` with the failure path armed: a panic inside a
854        /// step is recorded quietly and re-raised enriched, as the
855        /// interpreter re-raises a node's (A7), and every reference
856        /// pair is checked afterwards in a debug build.
857        ///
858        /// `#[inline]` is load-bearing: this wraps every `eval`, and
859        /// without it the native rung of the ladder pays a call and
860        /// about seven percent.
861        #[inline]
862        fn run_guarded(&mut self, body: impl FnOnce(&mut Self)) {
863            let capture = crate::kernel::engines::EvalPanicCaptureGuard::arm();
864            let outcome = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| body(self)));
865            drop(capture);
866            if let Err(payload) = outcome {
867                let sites = std::sync::Arc::clone(&self.sites);
868                let node = self.failing_node();
869                sites.reraise(payload, node, &self.buffer, Some(&self.none));
870            }
871            #[cfg(debug_assertions)]
872            self.validate_refs();
873        }
874
875        /// Gated to `debug_assertions` to match its call sites, which
876        /// compile out in release.
877        #[cfg(debug_assertions)]
878        fn validate_refs(&self) {
879            for &(slot, idx) in &self.ref_scratch {
880                let unpublished = self.none[slot]
881                    || matches!(self.slot_step.get(slot), Some(Some(step)) if self.ran[*step] == 0);
882                if unpublished {
883                    continue;
884                }
885                let (p, l) = self.scratch[idx].ptr_len();
886                assert!(
887                    self.buffer[slot] == p && self.buffer[slot + 1] == l,
888                    "S9 ref-validator: slot pair ({slot}, {}) = ({:#x}, {}) \
889                     does not match scratch[{idx}] = ({p:#x}, {l}) — a slot \
890                     op failed to republish or wrote the wrong slots",
891                    slot + 1,
892                    self.buffer[slot],
893                    self.buffer[slot + 1],
894                );
895            }
896        }
897
898        fn attach_cell(
899            &mut self,
900            name: &str,
901            cell: crate::kernel::SharedCell,
902        ) -> Result<(), String> {
903            let slot = self.externs.attach_cell(name, cell)?;
904            self.dirty_input(slot);
905            self.drive.stale = true;
906            Ok(())
907        }
908
909        #[inline]
910        fn begin_epoch(&mut self) {
911            if self.externs.cells_dirty() {
912                self.externs.refresh_cells(&mut self.buffer);
913            }
914            self.dirty_refreshed();
915            self.epoch += 1;
916            self.all_ran = false;
917            for &i in self.volatile_steps.iter() {
918                self.clean[i] = false;
919            }
920            self.drive.stale = false;
921        }
922
923        /// A read that begins no round still re-evaluates every volatile
924        /// step its cone reaches, once, and the steps downstream of it
925        /// (runtime_model.md R1.v); steps upstream of a volatile step
926        /// keep their currency. A new round already leaves every
927        /// volatile step unrun. Most programs have no volatile step and
928        /// pay the emptiness check.
929        #[inline]
930        fn rearm_volatile(&mut self) {
931            if self.volatile_steps.is_empty() {
932                return;
933            }
934            for &i in self.volatile_steps.iter() {
935                self.ran[i] = 0;
936            }
937            self.all_ran = false;
938        }
939
940        #[inline]
941        fn dirty_input(&mut self, slot: usize) {
942            if let Some(deps) = self.dirty.get(slot) {
943                for &i in deps {
944                    self.clean[i] = false;
945                }
946            }
947        }
948
949        /// Inlined into every evaluation, so only the check lives here:
950        /// a cell refresh changing a slot is rare, and its work is kept
951        /// out of line where it does not grow the hot path.
952        #[inline]
953        fn dirty_refreshed(&mut self) {
954            if self.externs.has_changed() {
955                self.dirty_refreshed_slots();
956            }
957        }
958
959        #[cold]
960        #[inline(never)]
961        fn dirty_refreshed_slots(&mut self) {
962            let changed = self.externs.take_changed();
963            for &slot in &changed {
964                // The cell's value is the slot's now: a slot that was
965                // unset (an extern with no default, bound to a parent's
966                // cell) holds a value, and one the cell cleared holds
967                // none, as a direct write would leave it.
968                if let Some(mask) = self.none.get_mut(slot) {
969                    *mask = self.externs.slot_is_unset(slot);
970                }
971                if let Some(deps) = self.plan.input_dependents.get(slot) {
972                    for &i in deps {
973                        self.ran[i] = 0;
974                        self.clean[i] = false;
975                    }
976                    self.all_ran = false;
977                }
978            }
979            self.externs.return_changed(changed);
980            let was = self.any_none;
981            self.any_none = self.externs.any_unset();
982            if was && !self.any_none {
983                self.none.fill(false);
984            }
985        }
986
987        #[inline]
988        fn eval_all(&mut self) {
989            let fresh = self.drive.stale;
990            if fresh {
991                self.begin_epoch();
992            } else {
993                self.refresh_cells();
994                self.rearm_volatile();
995            }
996            if fresh && !self.use_clean && !self.any_none {
997                self.run_guarded(|core| core.run_fresh());
998            } else {
999                let all = std::sync::Arc::clone(&self.all);
1000                self.run_steps(&all);
1001            }
1002        }
1003
1004        fn extern_written(&mut self, slot: usize, unset: bool) {
1005            self.none[slot] = unset;
1006            let was = self.any_none;
1007            self.any_none = self.externs.any_unset();
1008            if was && !self.any_none {
1009                self.none.fill(false);
1010            }
1011            self.dirty_input(slot);
1012            self.drive.stale = true;
1013        }
1014
1015        #[inline]
1016        fn guard_ref_slot(&self, slot: usize) {
1017            if self.ref_slots.get(slot).copied().unwrap_or(false) {
1018                panic!(
1019                    "S2 pointer containment: slot {slot} is Ref2-colored; raw u64 readers \
1020                     would leak an interior address. Use the typed borrow-checked accessor \
1021                     (read_vec_*), the boundary decode, or copy out."
1022                );
1023            }
1024        }
1025
1026        fn invalidate_all(&mut self) {
1027            self.clean.fill(false);
1028            self.all_ran = false;
1029            self.drive.stale = true;
1030        }
1031
1032        /// A pull by index publishes as a pull by name does, so a child
1033        /// bound to this output reads what the parent computed. The flag
1034        /// is checked before the pull rather than after it: holding the
1035        /// value across the check cost the native rungs 6 percent.
1036        #[inline]
1037        fn pull_at(&mut self, index: usize) -> crate::ast::Value {
1038            if self.externs.broadcasts() {
1039                return self.pull_at_publishing(index);
1040            }
1041            self.pull_at_value(index)
1042        }
1043
1044        /// `pull_at` under a descendant: the pull, then the publish.
1045        #[cold]
1046        #[inline(never)]
1047        fn pull_at_publishing(&mut self, index: usize) -> crate::ast::Value {
1048            let value = self.pull_at_value(index);
1049            let slot = self.resolved_outputs[index]
1050                .as_ref()
1051                .expect("resolved by the pull")
1052                .0;
1053            self.publish_slot(slot, &value);
1054            value
1055        }
1056
1057        #[inline(always)]
1058        fn pull_at_value(&mut self, index: usize) -> crate::ast::Value {
1059            if self.resolved_outputs.len() <= index {
1060                self.resolved_outputs.resize(index + 1, None);
1061            }
1062            if self.resolved_outputs[index].is_none() {
1063                let name = self
1064                    .externs
1065                    .output_names()
1066                    .get(index)
1067                    .cloned()
1068                    .unwrap_or_else(|| {
1069                        panic!(
1070                            "no output at index {index}; this kernel declares {}",
1071                            self.externs.output_names().len()
1072                        )
1073                    });
1074                let slot = self.output_map[&name];
1075                let ty = self
1076                    .output_types
1077                    .get(&name)
1078                    .copied()
1079                    .unwrap_or(crate::ast::PortType::U64);
1080                let cone = self
1081                    .plan
1082                    .cones
1083                    .get(&name)
1084                    .map(|c| std::sync::Arc::<[usize]>::from(c.as_slice()));
1085                let can_fail = cone
1086                    .as_ref()
1087                    .is_some_and(|c| c.iter().any(|&i| self.step_can_fail(i)));
1088                self.resolved_outputs[index] = Some((slot, ty, cone, can_fail));
1089            }
1090            if self.drive.stale {
1091                self.begin_epoch();
1092            } else {
1093                self.refresh_cells();
1094                self.rearm_volatile();
1095            }
1096            let resolved = self.resolved_outputs[index]
1097                .as_ref()
1098                .expect("resolved above");
1099            let (slot, ty, can_fail) = (resolved.0, resolved.1, resolved.3);
1100            if let Some(order) = &resolved.2 {
1101                // Borrowed across the run rather than cloned: the order
1102                // lives behind an `Arc` this state holds, and running
1103                // steps never touches the resolved outputs.
1104                let order: *const [usize] = &**order;
1105                let order = unsafe { &*order };
1106                if can_fail {
1107                    self.run_steps(order);
1108                } else {
1109                    // No step of the cone can fail, so there is no
1110                    // failure to capture and attribute.
1111                    self.run_order(order);
1112                }
1113            }
1114            self.slot_value(slot, ty)
1115        }
1116
1117        /// Publish the value at `slot` through its broadcast cell, if a
1118        /// descendant asked for one.
1119        fn publish_slot(&self, slot: usize, value: &crate::ast::Value) {
1120            if let Some(cell) = self.externs.published_output(slot) {
1121                cell.publish(value.clone());
1122            }
1123        }
1124
1125        fn pull_named(&mut self, name: &str) -> crate::ast::Value {
1126            if self.drive.stale {
1127                self.begin_epoch();
1128            } else {
1129                self.refresh_cells();
1130                self.rearm_volatile();
1131            }
1132            let plan = std::sync::Arc::clone(&self.plan);
1133            if let Some(order) = plan.cones.get(name) {
1134                self.run_steps(order);
1135            }
1136            let value = self.value_of(name);
1137            self.broadcast(name, &value);
1138            value
1139        }
1140
1141        /// Publish a freshly computed output through its broadcast cell,
1142        /// if a descendant asked for one, so a child that bound its
1143        /// matching input slot to the same cell reads the new value
1144        /// (cross_fiber_invalidation.md §3.1, "broadcast outputs").
1145        ///
1146        /// A program nobody built a subscope under has no cells at all,
1147        /// and pays the emptiness check.
1148        #[inline]
1149        fn broadcast(&mut self, name: &str, value: &crate::ast::Value) {
1150            if !self.externs.broadcasts() {
1151                return;
1152            }
1153            if let Some(&slot) = self.output_map.get(name) {
1154                self.publish_slot(slot, value);
1155            }
1156        }
1157
1158        /// The broadcast cell for a named output, created on the first
1159        /// ask. The interpreter seeds one per output at construction;
1160        /// a compiled kernel makes them only when a descendant binds to
1161        /// one, so a program with no subscope under it allocates none.
1162        ///
1163        /// Keyed by the output's slot, which is what `output_map`
1164        /// answers and what the buffer is indexed by, so the vector is
1165        /// as long as the buffer rather than as long as the output list.
1166        fn output_cell_for(&self, name: &str) -> Option<crate::kernel::SharedCell> {
1167            let slot = *self.output_map.get(name)?;
1168            // An output no step has computed yet holds its type's zero
1169            // in the buffer; the cell starts at `None`, as the
1170            // interpreter's does, until the first pull publishes.
1171            let uncomputed = !self.all_ran
1172                && matches!(self.slot_step.get(slot), Some(Some(step)) if self.ran[*step] == 0);
1173            let initial = if uncomputed {
1174                crate::ast::Value::None
1175            } else {
1176                self.value_of(name)
1177            };
1178            Some(self.externs.output_cell(slot, initial))
1179        }
1180
1181        #[inline]
1182        fn refresh_cells(&mut self) {
1183            if self.externs.cells_dirty() {
1184                self.externs.refresh_cells(&mut self.buffer);
1185                self.dirty_refreshed();
1186            }
1187        }
1188
1189        fn republish_refs(&mut self) {
1190            for &(slot, idx) in &self.ref_scratch {
1191                let (p, l) = self.scratch[idx].ptr_len();
1192                self.buffer[slot] = p;
1193                self.buffer[slot + 1] = l;
1194            }
1195            self.externs.seed(&mut self.buffer, None);
1196        }
1197
1198        #[inline]
1199        fn run_steps(&mut self, order: &[usize]) {
1200            self.run_guarded(|core| core.run_order(order));
1201        }
1202
1203        /// `run_steps` for the build-time constant fold: the same steps
1204        /// under the same guard, but a failure comes back as the
1205        /// message [`Attribution::reraise`] would have raised. A step
1206        /// that fails here fails before any kernel exists, so it is an
1207        /// error the builder returns rather than a panic out of a
1208        /// constructor.
1209        fn fold_steps(&mut self, order: &[usize]) -> Result<(), crate::KernelError> {
1210            let capture = crate::kernel::engines::EvalPanicCaptureGuard::arm();
1211            let outcome =
1212                std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| self.run_order(order)));
1213            drop(capture);
1214            if let Err(payload) = outcome {
1215                let sites = std::sync::Arc::clone(&self.sites);
1216                let node = self.failing_node();
1217                return Err(crate::KernelError::ConstantFold {
1218                    reason: sites.describe(payload, node, &self.buffer, Some(&self.none)),
1219                });
1220            }
1221            Ok(())
1222        }
1223
1224        fn set_extern(
1225            &mut self,
1226            name: &str,
1227            value: crate::ast::Value,
1228        ) -> Result<usize, crate::kernel::WriteError> {
1229            let (slot, unset) = self.externs.set(name, value, &mut self.buffer)?;
1230            self.extern_written(slot, unset);
1231            Ok(slot)
1232        }
1233
1234        fn set_extern_at(
1235            &mut self,
1236            index: usize,
1237            value: crate::ast::Value,
1238        ) -> Result<usize, crate::kernel::WriteError> {
1239            let (slot, unset) = self.externs.set_at(index, value, &mut self.buffer)?;
1240            self.extern_written(slot, unset);
1241            Ok(slot)
1242        }
1243
1244        fn slot_value(&self, slot: usize, ty: crate::ast::PortType) -> crate::ast::Value {
1245            if self.none.get(slot).copied().unwrap_or(false) {
1246                return crate::ast::Value::None;
1247            }
1248            crate::compile::marshal::decode_output(&self.buffer, slot, ty)
1249        }
1250
1251        fn value_of(&self, name: &str) -> crate::ast::Value {
1252            let slot = self.output_map[name];
1253            let ty = self
1254                .output_types
1255                .get(name)
1256                .copied()
1257                .unwrap_or(crate::ast::PortType::U64);
1258            self.slot_value(slot, ty)
1259        }
1260    };
1261}
1262pub(crate) use shared_core_methods;
1263
1264/// The dirty-register plan of a compiled kernel: which steps each input
1265/// slot invalidates when it changes, and which steps each named output
1266/// needs. The evaluation loops consume only this; provenance derives it
1267/// today, and a host that knows its write and read patterns may supply
1268/// a narrower plan later without touching the loops
1269/// (docs/design/engines.md §3.1).
1270pub(crate) struct Invalidation {
1271    /// Per input slot (coordinates and externs alike), the steps that
1272    /// depend on it, transitively.
1273    pub(crate) input_dependents: Vec<Vec<usize>>,
1274    /// Per named output, the steps of its cone in evaluation order.
1275    pub(crate) cones: std::collections::HashMap<String, Vec<usize>>,
1276}
1277
1278impl Invalidation {
1279    /// The plan provenance gives: every step downstream of an input is
1280    /// invalidated by it, and every step upstream of an output is in
1281    /// its cone. `inputs` and `outputs` are each step's slots;
1282    /// `output_slots` names the outputs.
1283    pub(crate) fn from_provenance(
1284        input_dependents: Vec<Vec<usize>>,
1285        step_inputs: &[&[usize]],
1286        step_outputs: &[&[usize]],
1287        output_slots: &std::collections::HashMap<String, usize>,
1288        total_slots: usize,
1289    ) -> Self {
1290        let step_count = step_inputs.len();
1291        let mut slot_step: Vec<Option<usize>> = vec![None; total_slots];
1292        for (i, outs) in step_outputs.iter().enumerate() {
1293            for &s in outs.iter() {
1294                slot_step[s] = Some(i);
1295            }
1296        }
1297        let cones = output_slots
1298            .iter()
1299            .map(|(name, &slot)| {
1300                let mut wanted = vec![false; step_count];
1301                let mut stack: Vec<usize> = slot_step[slot].into_iter().collect();
1302                while let Some(i) = stack.pop() {
1303                    if wanted[i] {
1304                        continue;
1305                    }
1306                    wanted[i] = true;
1307                    stack.extend(step_inputs[i].iter().filter_map(|&s| slot_step[s]));
1308                }
1309                (
1310                    name.clone(),
1311                    (0..step_count).filter(|&i| wanted[i]).collect(),
1312                )
1313            })
1314            .collect();
1315        Self {
1316            input_dependents,
1317            cones,
1318        }
1319    }
1320}
1321
1322/// Where each compiled step came from, for the failure path only
1323/// (engines.md §3.4). A step's panic is caught at the step
1324/// boundary and re-raised enriched exactly as the interpreter enriches
1325/// a node's: the node's name, the outputs it feeds, the program's
1326/// diagnostic context, and its input values decoded from the buffer
1327/// where the slot types allow. `sites` is indexed by program node:
1328/// the closure and pure-native kernels have one step per node, and
1329/// the hybrid kernel names the failing member of a segment through
1330/// its tracker slot.
1331#[derive(Default)]
1332pub(crate) struct Attribution {
1333    pub(crate) sites: Vec<NodeSite>,
1334    /// The program's diagnostic context (`PolydatProgram::context`).
1335    pub(crate) context: String,
1336}
1337
1338/// One node's identity for the failure path.
1339pub(crate) struct NodeSite {
1340    pub(crate) name: String,
1341    /// The declared outputs the node feeds, sorted.
1342    pub(crate) outputs: Vec<String>,
1343    /// `(first slot, port type)` of every input port, in port order.
1344    pub(crate) inputs: Vec<(usize, crate::ast::PortType)>,
1345}
1346
1347impl Attribution {
1348    /// The inputs of `step` as diagnostic text, from the buffer, each
1349    /// copied out and printed as the interpreter prints the same value:
1350    /// `None` where the mask says so, and the port type alone where the
1351    /// slot cannot be decoded, so the report itself never fails.
1352    fn inputs_of(&self, step: usize, buffer: &[u64], none: Option<&[bool]>) -> Vec<String> {
1353        let Some(site) = self.sites.get(step) else {
1354            return Vec::new();
1355        };
1356        let _quiet = crate::kernel::engines::EvalPanicCaptureGuard::arm();
1357        site.inputs
1358            .iter()
1359            .map(|&(slot, ty)| {
1360                if none.is_some_and(|m| m.get(slot).copied().unwrap_or(false)) {
1361                    return "None".to_string();
1362                }
1363                std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
1364                    crate::kernel::engines::format_value_for_diag(&marshal::decode_output(
1365                        buffer, slot, ty,
1366                    ))
1367                }))
1368                .unwrap_or_else(|_| format!("{ty:?}"))
1369            })
1370            .collect()
1371    }
1372
1373    /// Re-raise a step's panic enriched as the interpreter enriches a
1374    /// node's (`kernel::engines::enrich_panic`). `step` beyond the
1375    /// sites (native code that failed before naming a step) reports an
1376    /// unknown node, as the interpreter does for an index it lacks.
1377    pub(crate) fn reraise(
1378        &self,
1379        payload: Box<dyn std::any::Any + Send>,
1380        step: usize,
1381        buffer: &[u64],
1382        none: Option<&[bool]>,
1383    ) -> ! {
1384        crate::kernel::engines::reraise_enriched(self.describe(payload, step, buffer, none))
1385    }
1386
1387    /// The same message [`Self::reraise`] raises, returned instead. The
1388    /// build-time constant fold uses it: a step that fails there fails
1389    /// before any kernel exists, so it is an error the builder returns
1390    /// and not a panic out of a constructor.
1391    pub(crate) fn describe(
1392        &self,
1393        payload: Box<dyn std::any::Any + Send>,
1394        step: usize,
1395        buffer: &[u64],
1396        none: Option<&[bool]>,
1397    ) -> String {
1398        let site = self.sites.get(step);
1399        let name = site
1400            .map(|s| s.name.clone())
1401            .unwrap_or_else(|| format!("<unknown node #{step}>"));
1402        let outputs: Vec<&str> = site
1403            .map(|s| s.outputs.iter().map(String::as_str).collect())
1404            .unwrap_or_default();
1405        let inputs = self.inputs_of(step, buffer, none);
1406        crate::kernel::engines::enrich_panic(payload, &name, &outputs, &self.context, &inputs)
1407    }
1408}