Skip to main content

fuel_graph/
opt.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2//! Graph-level, backend-agnostic optimization passes.
3//!
4//! Transforms a `fuel-graph` computation graph before execution. Every
5//! backend benefits from these passes because they operate purely on
6//! the abstract op graph — no backend-specific knowledge required.
7//!
8//! **Passes today:**
9//!
10//! - **CSE** (common subexpression elimination): if the graph contains
11//!   two structurally-identical nodes — same op, same inputs (after
12//!   prior simplification), same shape/dtype — consumers of the
13//!   duplicate are redirected to the canonical node. The duplicate
14//!   becomes unreferenced and is silently dropped by the executor's
15//!   topo-walk. Commutative ops (`Add`, `Mul`) are keyed on sorted input
16//!   IDs so `a + b` and `b + a` fold to the same canonical node.
17//!   `Maximum`/`Minimum` are NOT in that set: commutative in value, not in
18//!   bits, because the NaN tie-break returns the FIRST operand. See GAP-271.
19//!
20//! - **Algebraic simplification**: a handful of identity/zero rules
21//!   that eliminate no-op ops:
22//!   - `AddScalar(0.0)(x)` → `x`
23//!   - `MulScalar(1.0)(x)` → `x`
24//!   - `Neg(Neg(x))` → `x`
25//!   - `Reshape(Reshape(x, _), s)` → `Reshape(x, s)`
26//!
27//!   These rarely appear in hand-written user code but show up
28//!   routinely in autograd-generated backward graphs and in
29//!   generic transformer building blocks that sometimes collapse.
30//!
31//! **Design:** graphs are append-only, so rewrites don't mutate
32//! existing nodes. Instead, the pass walks topologically, builds a
33//! remap `HashMap<NodeId, NodeId>` of old → canonical, and appends
34//! newly-canonicalized nodes to the same graph. Unreferenced originals
35//! remain in the arena but are never visited during realize. This is
36//! effectively a combined CSE + simplification + free DCE pass.
37//!
38//! **Return value:** the function takes the roots the caller cares
39//! about and returns the rewritten roots. Callers use these to update
40//! their `NodeHandle` handles.
41
42use crate::registry::{FusedOpEntry, FusedOpId, FusedOpParams, SubgraphPattern, default_registry};
43use crate::{Graph, Node, NodeId, Op, ScanEmit, ScanRole, SharedGraph, topo_order_multi};
44use fuel_ir::{DType, DeviceLocation};
45use std::collections::HashMap;
46
47// ---- Rule registry framework ----------------------------------------------
48//
49// Optimization is a pipeline of rule-driven graph rewrites. Two rule
50// families:
51//
52// - `RuleFamily::Lowering` — high-level op → primitive subgraph. Exposes
53//   fusion opportunities and lets backends without a fused kernel run
54//   the composition through whichever primitives they DO have.
55// - `RuleFamily::Fusion` — recognized primitive subgraph → fused op.
56//   Recovers (or improves on) the original-flavour kernel where one
57//   exists.
58//
59// A `Rule` is `(matcher, rewriter)`. The driver in
60// [`RuleRegistry::optimize_to_fixpoint`] runs lowering rules to fixpoint
61// first, then fusion rules to fixpoint. End state is post-fusion: for
62// SoftmaxLastDim (whose lower and fuse rules are inverses of each
63// other) the round-trip is identity. For lowering rules that ship
64// without a fusion partner — RmsNorm, LayerNorm, Affine, Clamp — the
65// post-pipeline graph is the lowered form.
66//
67// PR 3 ships the framework + a single (lower, fuse) rule pair for
68// `Op::SoftmaxLastDim`. The locked design (see ROADMAP.md → Phase 7.5)
69// also covers transactional snapshots, in-flight switching, and budget
70// modes; those are subsequent PRs that wrap this synchronous loop
71// without rewriting it.
72
73/// Which family a [`Rule`] belongs to. Drives the phase ordering in
74/// [`RuleRegistry::optimize_to_fixpoint`]: lowering runs to fixpoint
75/// first, then fusion. This ordering ensures inverse rule pairs
76/// converge cleanly — fusion has the last word, and its output is
77/// what the executor runs.
78#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
79pub enum RuleFamily {
80    /// High-level op → primitive subgraph. Always grows the graph.
81    Lowering,
82    /// Recognized primitive subgraph → fused op. Always shrinks the
83    /// graph (the fused op replaces the canonical subgraph's output;
84    /// the orphaned subgraph nodes remain in the arena but are
85    /// unreachable from the user's roots after consumer rewiring).
86    Fusion,
87    /// Algebraic identity rewrites that don't fit the lowering or
88    /// fusion shapes: cast elimination (`Cast(src→dst) → Op(dst)`
89    /// becomes `Op(src)` when the consumer supports the source
90    /// dtype), constant folding, identity elimination, common-
91    /// subexpression hoisting. Runs after fusion so cast-elimination
92    /// can't break fusion-pattern dtype expectations.
93    Algebraic,
94}
95
96/// One graph-rewrite rule. A rule pairs a structural matcher with a
97/// rewriter that emits replacement nodes and records the
98/// `(old_id → new_id)` remapping so the driver can rewrite consumer
99/// input edges to point at the replacement.
100///
101/// Rules don't need to update the layout side-table for view nodes
102/// they insert: [`Graph::push`] auto-populates layout for view ops
103/// from the input's layout.
104///
105/// Rules don't express ordering edges for destructive ops they emit
106/// (e.g. `Op::Release`); [`derive_ordering`] does that automatically
107/// at execution-plan time.
108pub trait Rule: Send + Sync {
109    /// Stable, human-readable name. Shows up in panic messages and
110    /// debug traces.
111    fn name(&self) -> &'static str;
112
113    /// Which family this rule belongs to. Determines pass ordering.
114    fn family(&self) -> RuleFamily;
115
116    /// Whether this rule can rewrite the node at `id`. Should be
117    /// pure — same inputs, same answer. Called once per node per
118    /// pass; the first matching rule wins.
119    fn matches(&self, graph: &Graph, id: NodeId) -> bool;
120
121    /// Apply the rewrite: append fresh nodes to the graph, declare
122    /// any side-effect roots if the rewrite emits side-effecting
123    /// ops, and write `remap.insert(id, replacement_id)` so the
124    /// driver can redirect consumer edges. Calling this method
125    /// implies [`matches`](Self::matches) returned `true` for the
126    /// same `(graph, id)` — rewriters can rely on the structure
127    /// the matcher checked for.
128    fn rewrite(&self, graph: &mut Graph, id: NodeId, remap: &mut HashMap<NodeId, NodeId>);
129}
130
131/// A registry of [`Rule`]s. Drives optimize-to-fixpoint via
132/// [`Self::optimize_to_fixpoint`]: lowering rules run to fixpoint,
133/// then fusion rules run to fixpoint. The locked design (ROADMAP.md
134/// Phase 7.5) intends this entry point to be wrapped in transactions
135/// in subsequent PRs without rewriting the loop body — keep this API
136/// stable for that future evolution.
137#[derive(Default)]
138pub struct RuleRegistry {
139    rules: Vec<Box<dyn Rule>>,
140}
141
142impl RuleRegistry {
143    /// Empty registry. Use [`Self::with_rule`] to add rules, or
144    /// [`Self::default_rules`] / [`Self::lowering_only`] for the
145    /// shipped configurations.
146    pub fn new() -> Self {
147        Self { rules: Vec::new() }
148    }
149
150    /// Append a rule. Returns self for builder-style chaining.
151    pub fn with_rule(mut self, rule: Box<dyn Rule>) -> Self {
152        self.rules.push(rule);
153        self
154    }
155
156    /// Lower + fuse rules for every fused op registered in the
157    /// process-wide [`default_registry`]. Round-trip is identity for
158    /// canonical SoftmaxLastDim graphs (the lowered subgraph is
159    /// structurally re-fused). Use this in production realize paths.
160    ///
161    /// Phase 7.6 step 3: PR 3's hand-written
162    /// `SoftmaxLastDimLowerRule`/`SoftmaxLastDimFuseRule` are deleted;
163    /// equivalent behavior is auto-generated from the SoftmaxLastDim
164    /// registry entry. Subsequent fused-op migrations in step 4 add
165    /// their entries to the same registry; `default_rules` picks them
166    /// up automatically.
167    pub fn default_rules() -> Self {
168        Self::capability_gated_rules(|_| true).0
169    }
170
171    /// Build lower + fuse rules, but **fuse** a fused op only when
172    /// `has_kernel(id)` holds — the capability gate
173    /// (`runtime-fused-op-registration.md` §6). Every fused op (static +
174    /// runtime) still gets a *lowering* rule (so any already-fused node
175    /// decomposes); an op WITHOUT a kernel gets **no fusion rule**, so a
176    /// kernel-absent fused node never forms in the first place — the miss is
177    /// caught at the pattern-match step, not repaired afterward.
178    ///
179    /// Returns the rule set plus the ids that were **gated out of fusion** — the
180    /// miss candidates feeding the JIT work-order signal. The dispatch layer
181    /// supplies `has_kernel` from its `FusedKernelRegistry` + the target
182    /// backend; [`Self::default_rules`] is the all-available case.
183    pub fn capability_gated_rules(
184        has_kernel: impl Fn(FusedOpId) -> bool,
185    ) -> (Self, Vec<FusedOpId>) {
186        let registry = default_registry();
187        let mut r = Self::new();
188        let mut gated_out = Vec::new();
189        for entry in registry.entries_iter() {
190            r = r.with_rule(Box::new(LoweringRule::from_entry(entry)));
191            if has_kernel(entry.id) {
192                r = r.with_rule(Box::new(FusionRule::from_entry(entry)));
193            } else {
194                gated_out.push(entry.id);
195            }
196        }
197        // Runtime-registered fused ops (Tier-2), same gate. A runtime op with no
198        // kernel yet (JIT cold start) is gated out + reported as a miss — the
199        // synthesize work-order — and its region stays primitive this pass.
200        for e in crate::runtime_fused::runtime_entries() {
201            r = r.with_rule(Box::new(LoweringRule::runtime(e.id)));
202            if has_kernel(e.id) {
203                r = r.with_rule(Box::new(FusionRule::declarative(
204                    e.id,
205                    crate::registry::PatternTree {
206                        root: e.region,
207                        params: FusedOpParams::Runtime {
208                            scalars: Vec::new(),
209                        },
210                    },
211                )));
212            } else {
213                gated_out.push(e.id);
214            }
215        }
216        (r, gated_out)
217    }
218
219    /// Lowering rules only — no fusion. Use this when the test or
220    /// caller wants the lowered form to be the post-pipeline state
221    /// (e.g. equivalence-testing the composed-math path on a backend
222    /// that has the primitives but not the fused kernel).
223    pub fn lowering_only() -> Self {
224        let registry = default_registry();
225        let mut r = Self::new();
226        for entry in registry.entries_iter() {
227            r = r.with_rule(Box::new(LoweringRule::from_entry(entry)));
228        }
229        // Runtime ops decompose to their region here too — this is the
230        // kernel-absent path (a backend without the synthesized kernel runs the
231        // primitive recipe).
232        for e in crate::runtime_fused::runtime_entries() {
233            r = r.with_rule(Box::new(LoweringRule::runtime(e.id)));
234        }
235        r
236    }
237
238    /// Number of registered rules.
239    pub fn len(&self) -> usize {
240        self.rules.len()
241    }
242
243    /// Whether the registry has any rules.
244    pub fn is_empty(&self) -> bool {
245        self.rules.is_empty()
246    }
247
248    /// Run lowering rules to fixpoint, then fusion rules to fixpoint.
249    /// Returns the (possibly-rewritten) roots — callers use these to
250    /// replace their `NodeHandle` handles.
251    ///
252    /// The two-phase ordering is important: rules that are inverses
253    /// of each other (e.g. SoftmaxLastDim's lower + fuse pair)
254    /// would oscillate in a single mixed-family fixpoint loop.
255    /// Phase ordering breaks the oscillation and gives fusion the
256    /// last word.
257    pub fn optimize_to_fixpoint(&self, graph: &SharedGraph, roots: &[NodeId]) -> Vec<NodeId> {
258        let mut roots = roots.to_vec();
259        if self.rules.is_empty() {
260            return roots;
261        }
262        // Phase 1: lowering to fixpoint.
263        loop {
264            let any = self.run_pass(RuleFamily::Lowering, graph, &mut roots);
265            if !any {
266                break;
267            }
268        }
269        // Phase 2: fusion to fixpoint.
270        loop {
271            let any = self.run_pass(RuleFamily::Fusion, graph, &mut roots);
272            if !any {
273                break;
274            }
275        }
276        // Phase 3: algebraic identities to fixpoint. Runs last so it
277        // can't disturb fusion patterns by eliminating casts that
278        // fusion was about to match on.
279        loop {
280            let any = self.run_pass(RuleFamily::Algebraic, graph, &mut roots);
281            if !any {
282                break;
283            }
284        }
285        roots
286    }
287
288    /// One pass over the graph for a single rule family. Returns
289    /// `true` if any rule fired during the pass — caller loops until
290    /// `false` for fixpoint.
291    fn run_pass(&self, family: RuleFamily, graph: &SharedGraph, roots: &mut [NodeId]) -> bool {
292        let order = {
293            let g = graph.read().unwrap();
294            topo_order_multi(&g, roots)
295        };
296
297        // Iterate this pass's family-rules in the order they were
298        // registered. First match wins per node.
299        let family_rules: Vec<&Box<dyn Rule>> =
300            self.rules.iter().filter(|r| r.family() == family).collect();
301        if family_rules.is_empty() {
302            return false;
303        }
304
305        let mut remap: HashMap<NodeId, NodeId> = HashMap::new();
306        let mut any_fired = false;
307
308        for id in order {
309            // Skip nodes that an earlier rule in this pass already
310            // rewrote — the consumer-edge rewrite happens after the
311            // pass, so the original node's structure still looks
312            // matchable mid-pass; we use `remap` as a "this pass
313            // already handled it" marker.
314            if remap.contains_key(&id) {
315                continue;
316            }
317
318            let firing_rule_idx = {
319                let g = graph.read().unwrap();
320                family_rules.iter().position(|r| r.matches(&g, id))
321            };
322            if let Some(idx) = firing_rule_idx {
323                let mut g = graph.write().unwrap();
324                let before = remap.len();
325                family_rules[idx].rewrite(&mut g, id, &mut remap);
326                // A rule that matched but recorded no remap entry made no
327                // progress — e.g. a `decompose` that returned self (G2's
328                // fixpoint signal: this op can't decompose further). Only
329                // count real rewrites, so the fixpoint loop terminates
330                // instead of spinning on a no-op match.
331                if remap.len() != before {
332                    any_fired = true;
333                }
334            }
335        }
336
337        if !any_fired {
338            return false;
339        }
340
341        // Apply remap to every consumer's input list, in place. This
342        // is the moment the rewritten nodes become the live ones —
343        // any node whose inputs referenced a remapped id now reads
344        // from its replacement.
345        {
346            let mut g = graph.write().unwrap();
347            let n_nodes = g.len();
348            for nid in 0..n_nodes {
349                let inputs_len = g.node(NodeId(nid)).inputs.len();
350                for i in 0..inputs_len {
351                    let cur = g.node(NodeId(nid)).inputs[i];
352                    if let Some(&new) = remap.get(&cur) {
353                        g.rewrite_input(NodeId(nid), cur, new);
354                    }
355                }
356            }
357        }
358
359        // And update the user's roots so the next phase / the caller
360        // sees the canonical post-rewrite ids.
361        for r in roots.iter_mut() {
362            if let Some(&new) = remap.get(r) {
363                *r = new;
364            }
365        }
366
367        true
368    }
369}
370
371/// Lower every reachable fused op in `graph` (rooted at `roots`) to its
372/// primitive base map — the fixpoint of `decompose` over every node. A thin
373/// named wrapper over [`RuleRegistry::lowering_only`] + `optimize_to_fixpoint`
374/// (the machinery every fused op — static-registry AND runtime — already flows
375/// through). Never panics: a self-returning `decompose` is a clean fixpoint
376/// (the never-panic total-decompose contract), not a loop.
377pub fn lower_to_base_map(graph: &SharedGraph, roots: &[NodeId]) -> Vec<NodeId> {
378    RuleRegistry::lowering_only().optimize_to_fixpoint(graph, roots)
379}
380
381/// `NodeId`-independent content hash of the subgraph rooted at `root`.
382///
383/// Recursively hashes each node as `(op identity, child hashes)`, folding in
384/// each child's *hash* rather than its `NodeId` — so two independently-built
385/// graphs (different arenas, different `NodeId` numbering) that are
386/// structurally identical hash equal. Reuses this module's existing
387/// canonicalization primitives rather than duplicating them:
388///
389/// - `op_key` for op identity when available (covers every primitive +
390///   `Op::Fused` variant CSE already relies on).
391/// - `is_commutative` to sort child hashes for `Add`/`Mul`, so `a + b` and
392///   `b + a` hash equal (mirrors CSE's own commutative-operand
393///   canonicalization). `Maximum`/`Minimum` are excluded -- see GAP-271.
394///
395/// `op_key` deliberately excludes `Op::Const` (its payload lives in the
396/// graph's `storage_map` slot, not the `Op` enum) and returns `None`
397/// for a handful of other ops (in-place,
398/// indexing, and anything else not explicitly listed). For those, this
399/// function falls back to `(discriminant, shape, dtype)` — and, for
400/// `Op::Const` specifically, additionally folds the constant's real bytes
401/// when they're readable (see `fold_const_bytes` below) so two
402/// independently-built recipes with equal constants hash equal, not just
403/// equal-shaped placeholders.
404///
405/// This is a cheap structural *pre-filter* for cross-graph recipe identity:
406/// structurally-equal-up-to-commutative-reordering-and-fusion-depth base
407/// maps hash equal. It does NOT canonicalize associativity or
408/// distributivity (`(a + b) + c` vs `a + (b + c)` hash differently) — a
409/// numeric verify pass covers that residual. Hashes are only stable within
410/// one process / `DefaultHasher` instance; never persist or compare them
411/// across processes.
412pub fn base_map_hash(graph: &Graph, root: NodeId) -> u64 {
413    use std::collections::hash_map::DefaultHasher;
414    use std::hash::{Hash, Hasher};
415
416    // Fold a `Const` node's real bytes into `hasher`, when they're
417    // readable without blocking. Const bytes live in the graph's
418    // `storage_map` slot (`lib.rs:218-223`), populated via
419    // `Graph::set_storage`/`storage_for` — NOT in the `Op` enum, so
420    // `op_key` can't see them (by design; see `OpKey`'s doc comment).
421    //
422    // Falls back to a silent no-op (the caller has already folded this
423    // node's shape/dtype via the `op_key -> None` branch) when:
424    // - the slot is unpopulated (e.g. a bare `Node { op: Op::Const, .. }`
425    //   pushed without ever calling `set_storage` — as the unit tests
426    //   below do for their placeholder consts), or
427    // - the slot's `RwLock` is held by a writer (`try_read` avoids
428    //   blocking a hash — a structural pre-filter — on live graph
429    //   mutation), or
430    // - the backend can't produce a host buffer right now (e.g.
431    //   device-only storage mid-capture).
432    //
433    // Documented limitation: in the fallback case, two *different*
434    // device-only (or unpopulated) consts of the same shape/dtype hash
435    // equal. `base_map_hash` is a pre-filter, not a full identity
436    // decision — a numeric verify pass is the source of truth when bytes
437    // aren't available at hash time.
438    fn fold_const_bytes(graph: &Graph, id: NodeId, hasher: &mut DefaultHasher) {
439        let Some(slot) = graph.storage_for(id) else {
440            return;
441        };
442        let Ok(guard) = slot.try_read() else { return };
443        let Ok(host) = guard.to_cpu_storage() else {
444            return;
445        };
446        fold_host_buffer(&host, hasher);
447    }
448
449    // Bridge every `HostBuffer` element dtype to something `Hash`. Integer
450    // variants (including the raw-byte-backed sub-byte dummy dtypes)
451    // already implement `Hash` directly; float variants don't (NaN
452    // equality is ambiguous under `Eq`, so std withholds `Hash`) — for
453    // those we fold each element's bit pattern via `to_bits()`, which is
454    // exact (including NaN payload bits) and matches the bit-pattern
455    // encoding `op_key` already uses for scalar payloads (e.g.
456    // `Op::AddScalar`'s `f64::to_bits()`).
457    fn fold_host_buffer(buf: &fuel_ir::HostBuffer, hasher: &mut DefaultHasher) {
458        use fuel_ir::HostBuffer as H;
459        match buf {
460            H::U8(v) | H::F6E2M3(v) | H::F6E3M2(v) | H::F4(v) | H::F8E8M0(v) | H::Bool(v) => {
461                v.hash(hasher)
462            }
463            H::I8(v) => v.hash(hasher),
464            H::U32(v) => v.hash(hasher),
465            H::I16(v) => v.hash(hasher),
466            H::I32(v) => v.hash(hasher),
467            H::I64(v) => v.hash(hasher),
468            H::BF16(v) => {
469                for x in v {
470                    x.to_bits().hash(hasher);
471                }
472            }
473            H::F16(v) => {
474                for x in v {
475                    x.to_bits().hash(hasher);
476                }
477            }
478            H::F32(v) => {
479                for x in v {
480                    x.to_bits().hash(hasher);
481                }
482            }
483            H::F64(v) => {
484                for x in v {
485                    x.to_bits().hash(hasher);
486                }
487            }
488            H::F8E4M3(v) => {
489                for x in v {
490                    x.to_bits().hash(hasher);
491                }
492            }
493        }
494    }
495
496    fn go(graph: &Graph, id: NodeId, memo: &mut HashMap<NodeId, u64>) -> u64 {
497        if let Some(&h) = memo.get(&id) {
498            return h;
499        }
500        let n = graph.node(id);
501        let mut hasher = DefaultHasher::new();
502        match op_key(&n.op) {
503            Some(k) => k.hash(&mut hasher),
504            None => {
505                std::mem::discriminant(&n.op).hash(&mut hasher);
506                n.shape.dims().hash(&mut hasher);
507                n.dtype.hash(&mut hasher);
508                if matches!(n.op, Op::Const) {
509                    fold_const_bytes(graph, id, &mut hasher);
510                }
511            }
512        }
513        let mut child_hashes: Vec<u64> = n.inputs.iter().map(|&c| go(graph, c, memo)).collect();
514        if is_commutative(&n.op) {
515            child_hashes.sort_unstable();
516        }
517        child_hashes.hash(&mut hasher);
518        let h = hasher.finish();
519        memo.insert(id, h);
520        h
521    }
522
523    go(graph, root, &mut HashMap::new())
524}
525
526// ---- Auto-generated lowering + fusion rules from FusedOpRegistry ----------
527//
528// Phase 7.6 step 3: PR 3's hand-written `SoftmaxLastDimLowerRule` and
529// `SoftmaxLastDimFuseRule` are replaced by [`LoweringRule`] +
530// [`FusionRule`] generic abstractions that read the per-fused-op
531// metadata (decompose function, pattern matcher) from a
532// [`FusedOpEntry`] in `crate::registry::default_registry`. Each rule
533// fires on `Op::Fused(id, _)` for lowering or on the pattern's
534// canonical primitive subgraph for fusion.
535//
536// Lowered form (7 nodes for SoftmaxLastDim, symmetric across max/sum):
537//
538//   m   = ReduceMaxTo([..., 1])(x)   # max-keepdim in one node
539//   mb  = BroadcastTo([..., last])(m)
540//   s   = Sub(x, mb)                 # numerically-stable shift
541//   e   = Exp(s)
542//   d   = ReduceSumTo([..., 1])(e)   # sum-keepdim in one node
543//   db  = BroadcastTo([..., last])(d)
544//   out = Div(e, db)
545//
546// The compile_one auto-Contiguize at the executor catches any backend
547// that doesn't support the strided BroadcastTo output as a kernel
548// input. PR 2-wide lets CUDA binary F32 kernels consume the broadcast
549// strides directly without materialization.
550
551/// Auto-generated lowering rule from a [`FusedOpEntry`]. Matches
552/// `Op::Fused(id, _)` and decomposes via the entry's `decompose`
553/// function. Each rule corresponds to one fused-op id in the registry.
554///
555/// Step 3 also fires on the legacy per-fused-op `Op` variants (e.g.
556/// `Op::SoftmaxLastDim`) so emission sites that haven't yet migrated
557/// to `Op::Fused` continue to lower correctly. Step 5 deletes those
558/// variants and the legacy match arm with them.
559pub struct LoweringRule {
560    id: FusedOpId,
561    decompose: fn(&mut Graph, NodeId, &FusedOpParams) -> NodeId,
562}
563
564impl LoweringRule {
565    pub fn from_entry(entry: &FusedOpEntry) -> Self {
566        Self {
567            id: entry.id,
568            decompose: entry.decompose,
569        }
570    }
571
572    /// Lowering rule for a runtime-registered fused op: decompose by re-emitting
573    /// its `runtime_fused` sidecar region as primitives. One per runtime id.
574    pub fn runtime(id: FusedOpId) -> Self {
575        Self {
576            id,
577            decompose: crate::runtime_fused::runtime_lowering_decompose,
578        }
579    }
580}
581
582impl Rule for LoweringRule {
583    fn name(&self) -> &'static str {
584        // Step 3 ships only SoftmaxLastDim; step 4 generalizes per-id.
585        // Until then, all auto-generated lowering rules share one name —
586        // the registry id distinguishes them at the structural level.
587        "FusedOpLowering"
588    }
589
590    fn family(&self) -> RuleFamily {
591        RuleFamily::Lowering
592    }
593
594    fn matches(&self, graph: &Graph, id: NodeId) -> bool {
595        let node = graph.node(id);
596        // Arity is per-fused-op; the decompose function is the
597        // authority. We don't gate on `node.inputs.len()` here.
598        match &node.op {
599            Op::Fused(fid, _) if *fid == self.id => true,
600            // Phase 7.6 step 5 (2026-05-11): legacy-variant
601            // fallthroughs have been dropped — every migrated fused
602            // op now flows through `Op::Fused(id, _)`. Step 5 dropped
603            // the per-fused-op `Op` variants together with these
604            // matcher arms.
605            _ => false,
606        }
607    }
608
609    fn rewrite(&self, graph: &mut Graph, id: NodeId, remap: &mut HashMap<NodeId, NodeId>) {
610        // Read params from the matched `Op::Fused(_, p)` node; the
611        // legacy primitive-variant fallthroughs were dropped in
612        // step 5.
613        let params = match &graph.node(id).op {
614            Op::Fused(_, p) => p.clone(),
615            other => unreachable!(
616                "LoweringRule::rewrite reached with non-matching op {other:?} \
617                 — matcher contract violated"
618            ),
619        };
620        let new_id = (self.decompose)(graph, id, &params);
621        // G2 (2026-06-20): a `decompose` that returns the SAME node is at
622        // its fixpoint — the op can't decompose further (a primitive, or a
623        // coarse op whose recipe/primitives don't exist yet, which returns
624        // self). That is not a rewrite, so we record no remap entry; the
625        // driver (`run_pass`) then sees the unchanged remap and does not
626        // count it as progress, so the lowering fixpoint terminates instead
627        // of oscillating. The node is left as `Op::Fused` — a surfaced
628        // opaque-op gap that an inventory pass can find.
629        if new_id != id {
630            remap.insert(id, new_id);
631        }
632    }
633}
634
635/// Auto-generated fusion rule from a [`FusedOpEntry`]. Matches the
636/// canonical primitive-subgraph pattern carried by the entry's
637/// [`SubgraphPattern`] and emits a single `Op::Fused(id, params)`
638/// node. Round-trips with [`LoweringRule`] for the same id.
639///
640/// Currently params are reconstructed by-id: SoftmaxLastDim's variant
641/// is parameterless, so the fusion rule emits
642/// `FusedOpParams::SoftmaxLastDim`. Step 4 extends [`crate::registry::PatternMatch`]
643/// (or adds a sibling `extract_params` field on `FusedOpEntry`) so
644/// param-bearing fused ops can recover their parameters from the
645/// matched subgraph.
646pub struct FusionRule {
647    id: FusedOpId,
648    pattern: PatternKind,
649}
650
651/// Internal: the runtime-flavor of a registry entry's pattern. We
652/// keep a function-pointer copy here rather than holding a reference
653/// into the (process-wide) registry so the rule is `'static`.
654#[expect(
655    clippy::large_enum_variant,
656    reason = "matcher pattern enum; boxing the large variant is deferred to the same follow-up as PatternNode (docs/gaps.md GAP-215) -- a cross-cutting change to construction/match sites, not a lint-commit change"
657)]
658enum PatternKind {
659    Callable(fn(&Graph, NodeId) -> Option<crate::registry::PatternMatch>),
660    /// The declarative form (fkc-fusion-patterns §3): carries the owned
661    /// `PatternTree` (the §3 grammar root + the params to stamp) so the rule is
662    /// `'static`. `matches`/`rewrite` walk it via [`crate::jit::match_region`].
663    Declarative(crate::registry::PatternTree),
664}
665
666impl FusionRule {
667    pub fn from_entry(entry: &FusedOpEntry) -> Self {
668        let pattern = match &entry.pattern {
669            SubgraphPattern::Callable(f) => PatternKind::Callable(*f),
670            SubgraphPattern::Declarative(tree) => PatternKind::Declarative(tree.clone()),
671        };
672        Self {
673            id: entry.id,
674            pattern,
675        }
676    }
677
678    /// Fusion rule from a declarative pattern (the runtime path): match the
679    /// region and stamp `tree.params` on the emitted `Op::Fused(id, _)`.
680    pub fn declarative(id: FusedOpId, tree: crate::registry::PatternTree) -> Self {
681        Self {
682            id,
683            pattern: PatternKind::Declarative(tree),
684        }
685    }
686}
687
688impl Rule for FusionRule {
689    fn name(&self) -> &'static str {
690        "FusedOpFusion"
691    }
692    fn family(&self) -> RuleFamily {
693        RuleFamily::Fusion
694    }
695
696    fn matches(&self, graph: &Graph, id: NodeId) -> bool {
697        match &self.pattern {
698            PatternKind::Callable(f) => (*f)(graph, id).is_some(),
699            PatternKind::Declarative(tree) => {
700                let consumers = |n: NodeId| {
701                    (0..graph.len())
702                        .filter(|&i| graph.node(NodeId(i)).inputs.contains(&n))
703                        .count()
704                };
705                crate::jit::match_region(graph, id, &tree.root, &consumers).is_some()
706            }
707        }
708    }
709
710    fn rewrite(&self, graph: &mut Graph, id: NodeId, remap: &mut HashMap<NodeId, NodeId>) {
711        let pattern_match = match &self.pattern {
712            PatternKind::Callable(f) => (*f)(graph, id)
713                .expect("rewrite called with non-matching id — matcher contract violated"),
714            PatternKind::Declarative(tree) => {
715                // match_region_extract returns the region's external inputs in
716                // bind-index order PLUS the live scalar values its open slots
717                // matched (the `extract:` layer). Wrap them as a PatternMatch
718                // so the shared reconstruction path below applies. For a
719                // Runtime params template the extracted scalars REPLACE the
720                // template's (fusing a scalar region with the template's empty
721                // scalars would silently lose the live values); non-Runtime
722                // templates stamp as-is (their matchers are value-agnostic).
723                let consumers = |n: NodeId| {
724                    (0..graph.len())
725                        .filter(|&i| graph.node(NodeId(i)).inputs.contains(&n))
726                        .count()
727                };
728                let (inputs, extracted) =
729                    crate::jit::match_region_extract(graph, id, &tree.root, &consumers)
730                        .expect("rewrite called with non-matching declarative pattern");
731                let params = match &tree.params {
732                    FusedOpParams::Runtime { .. } => FusedOpParams::Runtime { scalars: extracted },
733                    other => other.clone(),
734                };
735                crate::registry::PatternMatch {
736                    bindings: inputs.iter().enumerate().map(|(i, n)| (i, *n)).collect(),
737                    params,
738                }
739            }
740        };
741        // General-arity input reconstruction: bindings are (index, NodeId)
742        // pairs; sorting by index yields the emitted node's inputs in
743        // canonical order. SoftmaxLastDim binds {0 → x_id}; FusedLinear
744        // binds {0 → a, 1 → b, 2 → bias}. Missing index in the [0, N)
745        // range is a matcher contract violation.
746        let mut sorted = pattern_match.bindings.clone();
747        sorted.sort_by_key(|(idx, _)| *idx);
748        for (expected_idx, (got_idx, _)) in sorted.iter().enumerate() {
749            debug_assert_eq!(
750                *got_idx, expected_idx,
751                "FusionRule: bindings must be dense [0..N); pattern for id \
752                 {:?} returned non-contiguous indices",
753                self.id,
754            );
755        }
756        let inputs: Vec<NodeId> = sorted.iter().map(|(_, n)| *n).collect();
757        // Params come from the matcher — it's authoritative on what
758        // variant the recognized subgraph represents.
759        let params = pattern_match.params;
760        let dtype = graph.node(id).dtype;
761        let shape = graph.node(id).shape.clone();
762        let new_id = graph.push(Node {
763            op: Op::Fused(self.id, params),
764            inputs,
765            shape,
766            dtype,
767        });
768        remap.insert(id, new_id);
769    }
770}
771
772// ---- Cast-fusion algebraic rule ------------------------------------------
773//
774// `CastFusionRule` fires on `Cast(src→dst) → Op(dst, ...)` chains
775// where:
776//   - the Cast (or chain of casts) has exactly one consumer at
777//     each link of the chain,
778//   - the consumer is a **type-preserving op** (its output dtype
779//     equals the cast-fed input dtype — true for unary/binary
780//     arithmetic, reductions, MatMul; *false* for compare ops,
781//     Where, Cast),
782//   - the consumer has a kernel for the cast's *source* dtype on
783//     all input slots (capability predicate says yes).
784//
785// On fire: a new consumer node is appended with the cast input
786// replaced by the cast's source. **The new consumer's output dtype
787// is the cast-source dtype, not the original output dtype.** Any
788// downstream consumers reading from the rewritten node now see the
789// new dtype. This is by design — the rule fires aggressively under
790// the assumption that downstream consumers either (a) are
791// type-tolerant or (b) will have their own cast-fusion applied,
792// collapsing the chain.
793//
794// **Caveat — semantic change**: in graphs where the downstream
795// expects the original dst_dtype (e.g. a sink that returns BF16),
796// aggressive cast-fusion silently changes the dtype the user
797// observes. Wire this rule opt-in until a downstream-tolerance
798// framework (precision filter, op-tolerance metadata) is in place
799// to guard against unsafe rewrites.
800//
801// Chain cancellation is the always-safe sub-case: when the chain
802// is a net no-op (`Cast(F32→BF16) → Cast(BF16→F32) → Op(F32)`),
803// the final source dtype already matches the consumer's input
804// slot, so neither output dtype nor kernel selection changes.
805//
806// Architecture v1.0: this is the first "algebraic" family rule.
807// Future algebraic rewrites (constant folding, identity
808// elimination, CSE hoisting) join the same family and run in the
809// same Phase 3 loop.
810//
811// Decoupling: `fuel-graph::opt` doesn't know about binding tables,
812// PrecisionGuarantee, or BackendId. The capability check is
813// injected at rule construction via [`CapabilityPredicate`]; the
814// consumer (typically the route-picker layer in fuel-storage or a
815// higher crate) is responsible for closing over a binding-table
816// reference and answering the predicate.
817
818use std::sync::Arc;
819
820/// Predicate answering "is there a registered kernel for this op
821/// with these input/output dtypes?" The dtypes vector is the
822/// consumer's full input-dtype list (after the cast-source
823/// substitution) followed by the consumer's output dtype. Format
824/// matches the binding-table key shape used by `fuel-storage`.
825///
826/// Returns `true` if at least one backend has a kernel registered
827/// for the proposed (op, dtypes) combination — the route picker
828/// later decides which backend to use.
829pub type CapabilityPredicate = Arc<dyn Fn(&Op, &[DType]) -> bool + Send + Sync>;
830
831/// `Cast(src→dst) → Op(dst) ≡ Op(src)` when the consumer has a
832/// kernel for `src`.
833///
834/// Only fires when the cast has exactly one consumer (MVP: avoids
835/// the DAG complication of partial fan-out elimination). Multi-
836/// consumer Cast handling is a follow-up: rewrite one consumer and
837/// keep the cast alive for the others.
838pub struct CastFusionRule {
839    capabilities: CapabilityPredicate,
840}
841
842impl CastFusionRule {
843    /// Build the rule with a capability predicate. The predicate
844    /// closes over whatever capability source the caller has (a
845    /// binding table, a route picker, a static manifest). The rule
846    /// itself stays decoupled from those internals.
847    pub fn new(capabilities: CapabilityPredicate) -> Self {
848        Self { capabilities }
849    }
850
851    /// Walk `node.inputs` looking for an edge ending at a Cast (or
852    /// a chain of Casts) where every link in the chain has exactly
853    /// one consumer (the next link). Returns `Some((input_index,
854    /// final_src_id, consumer_dtypes))` where `final_src_id` is the
855    /// deepest reachable source after walking through the chain.
856    /// `consumer_dtypes` is the dtypes vector to ask the predicate
857    /// about. Returns `None` if no input qualifies.
858    ///
859    /// Walking through chained casts in one pass avoids a subtle
860    /// fixpoint trap: after the outer Cast is eliminated, the inner
861    /// Cast's other consumer (the now-orphan outer Cast) is still
862    /// in the arena and would make `count_consumers != 1` on the
863    /// next iteration. Collapsing the whole chain at once sidesteps
864    /// this because we never re-examine intermediate links.
865    ///
866    /// Shared by `matches` (which discards the index) and `rewrite`
867    /// (which uses the index to point the new consumer's input edge
868    /// at the deepest source). Computed twice intentionally — the
869    /// alternative is mutable state across `matches`/`rewrite`,
870    /// which conflicts with the trait's pure-matcher contract.
871    fn find_eligible_cast_input(
872        &self,
873        graph: &Graph,
874        id: NodeId,
875    ) -> Option<(usize, NodeId, Vec<fuel_ir::DType>)> {
876        let node = graph.node(id);
877        // Don't fire on a Cast node itself — fire on the consumer.
878        if matches!(node.op, Op::Cast(_)) {
879            return None;
880        }
881        for (idx, &input_id) in node.inputs.iter().enumerate() {
882            // Walk through chained casts. `current` is the cast (or
883            // chain of casts) currently being examined; `prev` is
884            // the consumer at this link of the chain (initially `id`,
885            // then the outer-most cast as we descend).
886            if !matches!(graph.node(input_id).op, Op::Cast(_)) {
887                continue;
888            }
889            // Type-preserving filter: the consumer's output dtype
890            // must equal the cast'd input slot's dtype. This is the
891            // family of ops where rewriting the input dtype is
892            // equivalent to rewriting the output dtype — Neg/Add/
893            // Mul/Div, reductions, MatMul, etc. Excludes Compare
894            // (T→U8), Where (U8+T+T→T), and Cast itself (which we
895            // already filtered above). For non-type-preserving
896            // consumers, swapping the input dtype would produce an
897            // output we can't predict without per-op knowledge.
898            if node.dtype != graph.node(input_id).dtype {
899                continue;
900            }
901            let mut prev = id;
902            let mut current = input_id;
903            let final_src_id = loop {
904                let current_node = graph.node(current);
905                if !matches!(current_node.op, Op::Cast(_)) {
906                    break current;
907                }
908                // Single-consumer constraint at this link: the cast
909                // at `current` must feed only `prev`. Otherwise we
910                // can't eliminate it (would orphan the other consumer's
911                // input edge).
912                if !is_only_consumer(graph, current, prev) {
913                    // The chain stops here; we treat the current node
914                    // (a Cast) as the source. The consumer takes the
915                    // dst dtype of this Cast.
916                    break current;
917                }
918                let Some(&next) = current_node.inputs.first() else {
919                    break current;
920                };
921                prev = current;
922                current = next;
923            };
924            // If the walk didn't move past the first cast (e.g.
925            // because the first cast has multiple consumers), there's
926            // nothing to fuse for this input.
927            if final_src_id == input_id {
928                continue;
929            }
930            let final_src_dtype = graph.node(final_src_id).dtype;
931            // Build the proposed input-dtypes + output-dtype vector.
932            // Type-preserving filter above guarantees output dtype
933            // tracks the cast-fed input, so the new output dtype is
934            // `final_src_dtype` (not the original `node.dtype`).
935            let mut dtypes: Vec<fuel_ir::DType> = node
936                .inputs
937                .iter()
938                .map(|&iid| {
939                    if iid == input_id {
940                        final_src_dtype
941                    } else {
942                        graph.node(iid).dtype
943                    }
944                })
945                .collect();
946            dtypes.push(final_src_dtype);
947            if (self.capabilities)(&node.op, &dtypes) {
948                return Some((idx, final_src_id, dtypes));
949            }
950        }
951        None
952    }
953}
954
955/// Returns `true` if `expected_consumer` is the only consumer of
956/// `target` in the arena. Used to guard chained-cast elimination.
957fn is_only_consumer(graph: &Graph, target: NodeId, expected_consumer: NodeId) -> bool {
958    let mut found_others = false;
959    for i in 0..graph.len() {
960        let nid = NodeId(i);
961        if nid == expected_consumer {
962            continue;
963        }
964        if graph.node(nid).inputs.contains(&target) {
965            found_others = true;
966            break;
967        }
968    }
969    !found_others
970}
971
972impl Rule for CastFusionRule {
973    fn name(&self) -> &'static str {
974        "CastFusion"
975    }
976    fn family(&self) -> RuleFamily {
977        RuleFamily::Algebraic
978    }
979
980    fn matches(&self, graph: &Graph, id: NodeId) -> bool {
981        self.find_eligible_cast_input(graph, id).is_some()
982    }
983
984    fn rewrite(&self, graph: &mut Graph, id: NodeId, remap: &mut HashMap<NodeId, NodeId>) {
985        let (idx, final_src_id, _dtypes) = self
986            .find_eligible_cast_input(graph, id)
987            .expect("CastFusionRule::rewrite called on non-matching node");
988        let node = graph.node(id).clone();
989        let final_src_dtype = graph.node(final_src_id).dtype;
990        let mut new_inputs = node.inputs.clone();
991        new_inputs[idx] = final_src_id;
992        let new_id = graph.push(Node {
993            op: node.op.clone(),
994            inputs: new_inputs,
995            shape: node.shape.clone(),
996            // Type-preserving op (matcher's invariant): the new
997            // output dtype tracks the new input dtype. Downstream
998            // consumers seeing this node may observe a different
999            // dtype than before the rewrite — see the module
1000            // docstring on the aggressive-semantics caveat.
1001            dtype: final_src_dtype,
1002        });
1003        remap.insert(id, new_id);
1004    }
1005}
1006
1007/// A HashMap-friendly encoding of `Op`. Needed because `Op` carries
1008/// `f64` (in `AddScalar`, `MulScalar`, `Clamp`, `LayerNormLastDim`,
1009/// `LayerNormLastDimBackward`) and `ConstData` (in `Const`), neither
1010/// of which is `Hash + Eq`. We keep `Const` out of CSE entirely
1011/// (identity-dedup happens via the executor's Arc-pointer const pool
1012/// already) and encode scalar payloads as their bit patterns.
1013#[derive(Debug, Hash, PartialEq, Eq)]
1014struct OpKey {
1015    tag: u16,
1016    ints: Vec<i64>,
1017    bits: Vec<u64>,
1018    // Sparse payloads we don't need to index into for equality are
1019    // serialized via their Debug repr as a fallback. Cheap and
1020    // correct; not used on the hot path outside simplification.
1021    dims: Vec<usize>,
1022    shape: Option<Vec<usize>>,
1023    dtype: Option<u32>,
1024}
1025
1026fn op_key(op: &Op) -> Option<OpKey> {
1027    // We deliberately refuse to CSE `Const`. Rationale above.
1028    let (tag, ints, bits, dims, shape, dtype) = match op {
1029        Op::Const => return None,
1030
1031        Op::Add => (1, vec![], vec![], vec![], None, None),
1032        Op::Sub => (2, vec![], vec![], vec![], None, None),
1033        Op::Mul => (3, vec![], vec![], vec![], None, None),
1034        Op::Div => (4, vec![], vec![], vec![], None, None),
1035
1036        Op::Neg => (10, vec![], vec![], vec![], None, None),
1037        Op::Sqr => (11, vec![], vec![], vec![], None, None),
1038        Op::Sqrt => (12, vec![], vec![], vec![], None, None),
1039        Op::Exp => (13, vec![], vec![], vec![], None, None),
1040        Op::Log => (14, vec![], vec![], vec![], None, None),
1041        Op::Sin => (15, vec![], vec![], vec![], None, None),
1042        Op::Cos => (16, vec![], vec![], vec![], None, None),
1043        Op::Tanh => (17, vec![], vec![], vec![], None, None),
1044        Op::Sigmoid => (18, vec![], vec![], vec![], None, None),
1045        Op::Silu => (19, vec![], vec![], vec![], None, None),
1046        Op::GeluTanh => (20, vec![], vec![], vec![], None, None),
1047        Op::Relu => (21, vec![], vec![], vec![], None, None),
1048        Op::Step => (22, vec![], vec![], vec![], None, None),
1049        Op::Recip => (23, vec![], vec![], vec![], None, None),
1050        Op::Abs => (24, vec![], vec![], vec![], None, None),
1051
1052        // --- comparison family (output dtype is U8) ---
1053        Op::Equal => (25, vec![], vec![], vec![], None, None),
1054        Op::Ne => (26, vec![], vec![], vec![], None, None),
1055        Op::Lt => (27, vec![], vec![], vec![], None, None),
1056        Op::Le => (28, vec![], vec![], vec![], None, None),
1057        Op::Gt => (29, vec![], vec![], vec![], None, None),
1058        // Tag 30 already taken by Op::MatMul below; the comparison
1059        // family wraps to 33 (the next free slot above linear-algebra)
1060        // for Op::Ge. The family sits at 25-29 + 33 — a small
1061        // contiguity break tolerated rather than renumbering existing
1062        // ops opportunistically.
1063        Op::Ge => (33, vec![], vec![], vec![], None, None),
1064
1065        // Ternary select. Slot 34 (next free after the comparison
1066        // family's wrap to 33).
1067        Op::Where => (34, vec![], vec![], vec![], None, None),
1068
1069        // --- rounding family (non-differentiable) ---
1070        Op::Floor => (35, vec![], vec![], vec![], None, None),
1071        Op::Ceil => (36, vec![], vec![], vec![], None, None),
1072        Op::Round => (37, vec![], vec![], vec![], None, None),
1073        Op::Sign => (38, vec![], vec![], vec![], None, None),
1074        Op::Erf => (39, vec![], vec![], vec![], None, None),
1075        // Tag 40 was Op::Cast; the unary fanout wraps to 47 (next
1076        // free slot above the 40-46 cast/shape/reduce cluster).
1077        Op::Gelu => (47, vec![], vec![], vec![], None, None),
1078        Op::Pow => (49, vec![], vec![], vec![], None, None),
1079        Op::Rsqrt => (54, vec![], vec![], vec![], None, None),
1080        Op::Rem => (55, vec![], vec![], vec![], None, None),
1081        Op::Flip { dim } => (56, vec![*dim as i64], vec![], vec![], None, None),
1082        Op::Roll { dim, shift } => (57, vec![*dim as i64, *shift], vec![], vec![], None, None),
1083        Op::CumSum { dim } => (58, vec![*dim as i64], vec![], vec![], None, None),
1084        Op::PadBackward {
1085            in_shape,
1086            padding,
1087            mode,
1088        } => {
1089            let mode_tag = match mode {
1090                crate::PadMode::Constant => 0_i64,
1091                crate::PadMode::Reflect => 1,
1092                crate::PadMode::Replicate => 2,
1093            };
1094            let mut ints: Vec<i64> = Vec::with_capacity(padding.len() * 2 + 1);
1095            for &(b, a) in padding {
1096                ints.push(b as i64);
1097                ints.push(a as i64);
1098            }
1099            ints.push(mode_tag);
1100            (
1101                60,
1102                ints,
1103                vec![],
1104                vec![],
1105                Some(in_shape.dims().to_vec()),
1106                None,
1107            )
1108        }
1109        Op::Pad {
1110            padding,
1111            mode,
1112            value,
1113        } => {
1114            let mode_tag = match mode {
1115                crate::PadMode::Constant => 0_i64,
1116                crate::PadMode::Reflect => 1,
1117                crate::PadMode::Replicate => 2,
1118            };
1119            // Flatten padding pairs into [b0, a0, b1, a1, ...] + the
1120            // mode tag. Multi-dim padding keys uniquely off this vec.
1121            let mut ints: Vec<i64> = Vec::with_capacity(padding.len() * 2 + 1);
1122            for &(b, a) in padding {
1123                ints.push(b as i64);
1124                ints.push(a as i64);
1125            }
1126            ints.push(mode_tag);
1127            (59, ints, vec![value.to_bits()], vec![], None, None)
1128        }
1129
1130        Op::MatMul => (30, vec![], vec![], vec![], None, None),
1131        Op::Transpose => (31, vec![], vec![], vec![], None, None),
1132        Op::Permute(axes) => (32, vec![], vec![], axes.clone(), None, None),
1133
1134        Op::Cast(dt) => (40, vec![], vec![], vec![], None, Some(dtype_key(*dt))),
1135        Op::BroadcastTo(s) => (41, vec![], vec![], vec![], Some(s.dims().to_vec()), None),
1136        Op::Reshape(s) => (42, vec![], vec![], vec![], Some(s.dims().to_vec()), None),
1137        Op::Contiguize => (46, vec![], vec![], vec![], None, None),
1138        Op::Unsqueeze { dim } => (45, vec![*dim as i64], vec![], vec![], None, None),
1139        Op::Squeeze { dim } => (48, vec![*dim as i64], vec![], vec![], None, None),
1140        Op::ReduceSumTo(s) => (43, vec![], vec![], vec![], Some(s.dims().to_vec()), None),
1141        Op::ReduceMaxTo(s) => (44, vec![], vec![], vec![], Some(s.dims().to_vec()), None),
1142        // Phase 7.6 step 5 (2026-05-11): tags 46/70/71/72/73/74/75/76
1143        // formerly assigned to SoftmaxLastDim, LayerNormLastDim,
1144        // RmsNormLastDim, Rope and the four backward helpers have
1145        // been retired with their `Op` variants. Those ops now flow
1146        // through the `Op::Fused(fid, fparams)` arm below (tag 200,
1147        // with id + params encoded into the int/bit slots) and CSE
1148        // dedupes correctly because each entry produces a distinct
1149        // FusedOpId.
1150        Op::SumAll => (50, vec![], vec![], vec![], None, None),
1151        Op::MaxAll => (51, vec![], vec![], vec![], None, None),
1152        Op::MinAll => (52, vec![], vec![], vec![], None, None),
1153        Op::MeanAll => (53, vec![], vec![], vec![], None, None),
1154
1155        Op::SumDim(d) => (60, vec![*d as i64], vec![], vec![], None, None),
1156        Op::MaxDim(d) => (61, vec![*d as i64], vec![], vec![], None, None),
1157        Op::MinDim(d) => (62, vec![*d as i64], vec![], vec![], None, None),
1158        Op::MeanDim(d) => (63, vec![*d as i64], vec![], vec![], None, None),
1159        Op::ArgMaxDim(d) => (64, vec![*d as i64], vec![], vec![], None, None),
1160        Op::ArgMinDim(d) => (65, vec![*d as i64], vec![], vec![], None, None),
1161
1162        Op::Concat { dim } => (80, vec![*dim as i64], vec![], vec![], None, None),
1163        Op::Slice { dim, start, len } => (
1164            81,
1165            vec![*dim as i64, *start as i64, *len as i64],
1166            vec![],
1167            vec![],
1168            None,
1169            None,
1170        ),
1171
1172        Op::AddScalar(c) => (90, vec![], vec![c.to_bits()], vec![], None, None),
1173        Op::MulScalar(c) => (91, vec![], vec![c.to_bits()], vec![], None, None),
1174        Op::PowI(n) => (92, vec![*n as i64], vec![], vec![], None, None),
1175        Op::Clamp { min, max } => (
1176            93,
1177            vec![],
1178            vec![min.to_bits(), max.to_bits()],
1179            vec![],
1180            None,
1181            None,
1182        ),
1183
1184        Op::Maximum => (100, vec![], vec![], vec![], None, None),
1185        Op::Minimum => (101, vec![], vec![], vec![], None, None),
1186
1187        // Phase 7.6 step 2: registry-extended fused ops. CSE folds two
1188        // Op::Fused nodes with identical (id, params) to one. Encoding:
1189        // base tag 200; ints = [id.0, params.tag, ...params.ints]; bits
1190        // = params.bits. Mirrors the FusedOpParamsKey shape from
1191        // crate::registry so the encoding tracks any future param
1192        // variants without rewriting this arm.
1193        Op::Fused(fid, fparams) => {
1194            let pk = fparams.key();
1195            let mut ints: Vec<i64> = Vec::with_capacity(2 + pk.ints.len());
1196            ints.push(fid.0 as i64);
1197            ints.push(pk.tag as i64);
1198            ints.extend_from_slice(&pk.ints);
1199            (200, ints, pk.bits, vec![], None, None)
1200        }
1201
1202        // Op::Scan folds ONLY its own params (n_xs, bound, emit tag,
1203        // early_exit-present flag). The BODY is hashed automatically by
1204        // base_map_hash's child-recursion, because the body-exit NodeIds are
1205        // the last two entries of the node's `inputs` (the lax.scan encoding).
1206        // Two scans with identical params but different bodies therefore get
1207        // different child-hashes and different base_map_hash. Tag 210.
1208        Op::Scan {
1209            n_carries,
1210            n_xs,
1211            bound,
1212            emit,
1213            early_exit,
1214        } => {
1215            let emit_tag: i64 = match emit {
1216                ScanEmit::All => 0,
1217                ScanEmit::Final => 1,
1218            };
1219            let exit_flag: i64 = if early_exit.is_some() { 1 } else { 0 };
1220            (
1221                210,
1222                // ⚠️ GAP-303: `n_carries` MUST be folded here. This is Fuel's own
1223                // recipe identity (base_map_hash) -- omit it and a 1-carry and a
1224                // 3-carry Scan with the same n_xs/bound/emit hash IDENTICALLY, so
1225                // cover-finding and the plan cache treat them as one recipe. Same
1226                // defect class as KISS-OPS-6.19 (an arm reading too few attrs made
1227                // Gather-on-axis-2 and axis-0 emit identical bytes), and a SECOND
1228                // site beside the seam's canonical body.
1229                //
1230                // Appended LAST so existing single-carry hashes are the only ones
1231                // that move, and they move uniformly.
1232                vec![
1233                    *n_xs as i64,
1234                    *bound as i64,
1235                    emit_tag,
1236                    exit_flag,
1237                    *n_carries as i64,
1238                ],
1239                vec![],
1240                vec![],
1241                None,
1242                None,
1243            )
1244        }
1245        // Op::ScanPlaceholder folds role tag + index so Carry/0, Elem/0,
1246        // Carry/1 are all distinct in the body hash. Tag 211.
1247        Op::ScanPlaceholder { role, index } => {
1248            let role_tag: i64 = match role {
1249                ScanRole::Carry => 0,
1250                ScanRole::Elem => 1,
1251            };
1252            (
1253                211,
1254                vec![role_tag, *index as i64],
1255                vec![],
1256                vec![],
1257                None,
1258                None,
1259            )
1260        }
1261
1262        // Indexing and anything else we haven't explicitly listed:
1263        // fall back to a unique tag that includes a structural
1264        // discriminant. These ops rarely appear more than once with
1265        // identical inputs, so we just mark them non-CSE-able by
1266        // returning None. Conservative; safe.
1267        _ => return None,
1268    };
1269    Some(OpKey {
1270        tag,
1271        ints,
1272        bits,
1273        dims,
1274        shape,
1275        dtype,
1276    })
1277}
1278
1279fn dtype_key(dt: DType) -> u32 {
1280    // Cheap injection: the Debug form is stable.
1281    // For tiny enums this compiles to a jump table.
1282    format!("{dt:?}")
1283        .as_bytes()
1284        .iter()
1285        .fold(0u32, |acc, b| acc.wrapping_mul(31).wrapping_add(*b as u32))
1286}
1287
1288/// `Maximum`/`Minimum` are DELIBERATELY ABSENT. They are commutative in
1289/// value and NOT in bits: `chassis/binary.rs` returns the FIRST NaN operand
1290/// (`if a.is_nan() { a } else if b.is_nan() { b }`), a documented lhs-first
1291/// tie-break matching `torch.maximum`. Two distinct NaN payloads therefore
1292/// swap with operand order -- measured, `max(0x7FC00001, 0x7FC00002)` gives
1293/// `0x7FC00001` and the reverse gives `0x7FC00002` -- so folding `min(a,b)`
1294/// with `min(b,a)` is an INVALID rewrite. See GAP-271.
1295///
1296/// The +-0 tie ALSO makes the fold invalid, but do not state it as a fixed
1297/// bias: the +-0 answer is NOT A PROPERTY OF THIS SOURCE. It is decided per
1298/// CALL SITE by the optimizer, because we delegate to Rust's `f32::max`,
1299/// which disclaims the zero tie. Measured in ONE binary at opt-level 3:
1300/// const-folded literals give +0 for both orders; `black_box` operands give
1301/// -0 for both; `#[inline(never)]` gives -0 and +0 -- ORDER-DEPENDENT.
1302/// Three answers, same source. That is itself a reason the fold is invalid:
1303/// folding two call sites merges forms the compiler may resolve differently.
1304///
1305/// Rely on the NaN-payload argument, because it is STABLE -- verified
1306/// identical at opt-level 0 and 3, under `black_box` and under
1307/// `#[inline(never)]`. It is stable precisely because that tie-break is
1308/// EXPLICIT IN OUR SOURCE, where the +-0 tie is delegated to the compiler.
1309fn is_commutative(op: &Op) -> bool {
1310    matches!(op, Op::Add | Op::Mul)
1311}
1312
1313/// Run CSE + algebraic simplification on the graph reachable from
1314/// `roots`. Returns the (possibly-rewritten) roots the caller should
1315/// use afterward.
1316///
1317/// The pass runs to a fixed point on a single topological walk: each
1318/// node is visited once, canonicalized (inputs remapped, constants
1319/// folded where trivial), then either matched to an existing canonical
1320/// node (CSE) or appended as fresh. No multi-pass iteration — the
1321/// simplification rules are chosen so one forward pass suffices.
1322pub fn optimize(graph: &SharedGraph, roots: &[NodeId]) -> Vec<NodeId> {
1323    let order = {
1324        let g = graph.read().unwrap();
1325        topo_order_multi(&g, roots)
1326    };
1327
1328    let mut g = graph.write().unwrap();
1329    let mut remap: HashMap<NodeId, NodeId> = HashMap::new();
1330    let mut cse: HashMap<(OpKey, Vec<NodeId>), NodeId> = HashMap::new();
1331
1332    for id in order {
1333        let (op, inputs, shape, dtype) = {
1334            let node = g.node(id);
1335            (
1336                node.op.clone(),
1337                node.inputs.clone(),
1338                node.shape.clone(),
1339                node.dtype,
1340            )
1341        };
1342        let mapped_inputs: Vec<NodeId> = inputs
1343            .iter()
1344            .map(|input_id| *remap.get(input_id).unwrap_or(input_id))
1345            .collect();
1346        let inputs_unchanged = mapped_inputs == inputs;
1347
1348        // 1. Algebraic simplifications that produce an alias (no new
1349        //    node). If a rule fires, we remap this id to an existing
1350        //    node and skip CSE for it entirely.
1351        //
1352        //    Identity Cast(d) on a dtype-d input is a no-op: dispatch keys
1353        //    casts on [src, dst] and intentionally registers no [d, d]
1354        //    kernel, so this elision (not a kernel) is what makes a same-dtype
1355        //    cast disappear before realize. Needs the input node's dtype, so
1356        //    it lives here rather than in `try_simplify` (which sees only
1357        //    op + inputs).
1358        if let Op::Cast(target) = &op {
1359            if g.node(mapped_inputs[0]).dtype == *target {
1360                remap.insert(id, mapped_inputs[0]);
1361                continue;
1362            }
1363            //    GAP-183: lower `Cast(float -> Bool)` to `Ne(x, x*0)`.
1364            //
1365            //    WHY A LOWERING RATHER THAN A KERNEL: CUDA already ships the
1366            //    `!= 0` test — `ne_{f32,f64,f16,bf16}_u8` in
1367            //    `cuda/compare.fkc.md` — but a STORAGE-level cast cannot compose
1368            //    graph ops, so those kernels are unreachable from `to_dtype`.
1369            //    Rewriting here reaches them. The INTEGER half is deliberately
1370            //    left alone: CUDA has no integer compare kernel, so lowering it
1371            //    would relocate the same decline while bypassing CPU's real
1372            //    int->Bool cast kernels.
1373            //
1374            //    WHY `MulScalar(0.0)` FOR THE ZEROS: its own doc says it exists
1375            //    "to avoid building a full-shape const just to add or multiply by
1376            //    a constant" — so the zeros inherit shape AND dtype from the
1377            //    input by construction, with nothing materialized. `Op::ZeroFill`
1378            //    is NOT usable here: it is DESTRUCTIVE (writes through input 0).
1379            //
1380            //    ⚠️ THE IEEE REASONING, because a reader will stop at `x*0` and
1381            //    wonder about non-finite inputs:
1382            //      finite x != 0 : x*0 = ±0        -> x != 0     -> true
1383            //      x = ±0.0      : x*0 = ±0        -> equal      -> false
1384            //                      (IEEE: -0.0 == 0.0, so -0.0 is FALSE)
1385            //      x = NaN       : NaN*0 = NaN     -> NaN != NaN -> TRUE
1386            //      x = ±inf      : inf*0 = NaN     -> x != NaN   -> true
1387            //    NaN being truthy matches PyTorch's `.bool()` and the CPU cast
1388            //    kernels, so both paths agree.
1389            //
1390            //    NOTE the zeros operand is NaN (not 0) whenever x is NaN or ±inf,
1391            //    and that is HARMLESS rather than load-bearing: for every
1392            //    non-finite x, `x != NaN` and `x != 0` are both true, so the two
1393            //    spellings agree everywhere. A future "x*0 => zeros" fold would
1394            //    therefore be SAFE here — recorded because the opposite is the
1395            //    intuitive guess, and an earlier draft of this comment asserted
1396            //    it as a hazard. Only `AddScalar(0)`/`MulScalar(1)` fold today.
1397            if *target == fuel_ir::DType::Bool {
1398                let src_dtype = g.node(mapped_inputs[0]).dtype;
1399                if src_dtype.is_float() {
1400                    let src_shape = g.node(mapped_inputs[0]).shape.clone();
1401                    let zeros = g.push(Node {
1402                        op: Op::MulScalar(0.0),
1403                        inputs: vec![mapped_inputs[0]],
1404                        shape: src_shape.clone(),
1405                        dtype: src_dtype,
1406                    });
1407                    let ne = g.push(Node {
1408                        op: Op::Ne,
1409                        inputs: vec![mapped_inputs[0], zeros],
1410                        shape: src_shape,
1411                        dtype: fuel_ir::DType::Bool,
1412                    });
1413                    remap.insert(id, ne);
1414                    continue;
1415                }
1416            }
1417        }
1418        if let Some(aliased) = try_simplify(&op, &mapped_inputs) {
1419            remap.insert(id, aliased);
1420            continue;
1421        }
1422
1423        // 2. CSE: if a structurally-identical canonical node already
1424        //    exists, reuse it. Commutative ops use sorted inputs as
1425        //    the key so `a+b` and `b+a` match. If no match exists and
1426        //    nothing about this node needs rewriting (inputs unchanged,
1427        //    no simplification fired), keep the original node in place
1428        //    to avoid polluting the arena with identical copies.
1429        if let Some(key) = op_key(&op) {
1430            let key_inputs = if is_commutative(&op) {
1431                let mut v = mapped_inputs.clone();
1432                v.sort();
1433                v
1434            } else {
1435                mapped_inputs.clone()
1436            };
1437            let full_key = (key, key_inputs);
1438            if let Some(&existing) = cse.get(&full_key) {
1439                remap.insert(id, existing);
1440                continue;
1441            }
1442            let canonical_id = if inputs_unchanged {
1443                id
1444            } else {
1445                g.push(Node {
1446                    op: op.clone(),
1447                    inputs: mapped_inputs.clone(),
1448                    shape: shape.clone(),
1449                    dtype,
1450                })
1451            };
1452            cse.insert(full_key, canonical_id);
1453            remap.insert(id, canonical_id);
1454        } else {
1455            // Const or other non-CSE-able op. Keep the original if
1456            // unchanged; otherwise append a rewritten copy (Const
1457            // never has inputs, so this branch effectively keeps
1458            // Const originals).
1459            let canonical_id = if inputs_unchanged {
1460                id
1461            } else {
1462                g.push(Node {
1463                    op: op.clone(),
1464                    inputs: mapped_inputs,
1465                    shape,
1466                    dtype,
1467                })
1468            };
1469            remap.insert(id, canonical_id);
1470        }
1471    }
1472
1473    roots.iter().map(|r| *remap.get(r).unwrap_or(r)).collect()
1474}
1475
1476/// Insert `Op::Copy` nodes so every op's inputs live on the same
1477/// device as the op itself. Returns the rewritten roots.
1478///
1479/// This is the Phase-3.5 pass that lifts the Router from "explicit
1480/// copies only" to "auto-insert where needed."
1481///
1482/// ## Device inference rules
1483///
1484/// For each node N, walked in topological order:
1485///
1486/// 1. **Target device** = first match wins:
1487///    - If N has a placement hint via `graph.set_placement(n, d)`, use `d`.
1488///    - Else, inherit from N's first input's inferred device.
1489///    - Else (N is a Const or the first input has no inferred device
1490///      either), the target is `None` — N is "placeless" and will be
1491///      placed by its consumer's demands.
1492///
1493/// 2. **Input reconciliation**: for each input I of N, if I's inferred
1494///    device differs from N's target device, insert an
1495///    `Op::Copy { target }` node and redirect N's input edge to the
1496///    new Copy.
1497///
1498/// ## What it doesn't do
1499///
1500/// - **Backward ops:** the pass currently treats all ops the same
1501///   way — no special reasoning about what device a backward node
1502///   should run on. For pure inference this is fine; for training
1503///   through the Router we'd want a reverse-mode pass that mirrors
1504///   forward placement. Tracked as a follow-up TODO.
1505/// - **Const placement lowering:** if a Const flows into a single
1506///   consumer on device D, we emit `Copy(Const, D)` instead of
1507///   tagging the Const itself as on-device-D. A future cost-lowering
1508///   pass can fuse the two — tracked as a TODO alongside Phase 4's
1509///   scheduler.
1510/// - **Redundant copy elision:** `Copy(X, A) -> Copy(_, A)` when X is
1511///   already on A is dropped via the idempotent check in step 2, but
1512///   there's no pass that merges `Copy(X, A) -> Copy(_, B) -> Copy(_, A)`
1513///   (cross-device round-trips that cancel). Also future.
1514pub fn insert_copies(graph: &SharedGraph, roots: &[NodeId]) -> Vec<NodeId> {
1515    let order = {
1516        let g = graph.read().unwrap();
1517        topo_order_multi(&g, roots)
1518    };
1519
1520    // Inferred output device per node. `None` = placeless (e.g., an
1521    // unplaced Const). Read on the input side to decide whether a
1522    // Copy is needed.
1523    let mut inferred: HashMap<NodeId, Option<DeviceLocation>> = HashMap::new();
1524    // Rewritten (old → new) node ID map. A node gets rewritten when
1525    // any of its inputs needed a Copy interposed.
1526    let mut remap: HashMap<NodeId, NodeId> = HashMap::new();
1527
1528    let mut g = graph.write().unwrap();
1529
1530    for id in order {
1531        // Snapshot the node — all subsequent reads need the old id's
1532        // metadata, not whatever we're about to rewrite.
1533        let (op, inputs, shape, dtype) = {
1534            let node = g.node(id);
1535            (
1536                node.op.clone(),
1537                node.inputs.clone(),
1538                node.shape.clone(),
1539                node.dtype,
1540            )
1541        };
1542        let placement_hint = g.placement(id);
1543
1544        // Target device for this node: explicit hint > first input's
1545        // inferred device > None (placeless).
1546        let target_device: Option<DeviceLocation> = match placement_hint {
1547            Some(d) => Some(d),
1548            None => inputs.first().and_then(|i| {
1549                let mapped = *remap.get(i).unwrap_or(i);
1550                inferred.get(&mapped).copied().flatten()
1551            }),
1552        };
1553
1554        // Walk inputs, inserting Copy where needed.
1555        let mut new_inputs: Vec<NodeId> = Vec::with_capacity(inputs.len());
1556        let mut any_changed = false;
1557        for input_id in &inputs {
1558            let mapped_in = *remap.get(input_id).unwrap_or(input_id);
1559            let in_device = inferred.get(&mapped_in).copied().flatten();
1560
1561            let needs_copy = match (in_device, target_device) {
1562                // Both known and disagree → copy.
1563                (Some(src), Some(tgt)) if src != tgt => true,
1564                // Input is placeless and we have a target → copy
1565                // (Const gets Copy'd onto the target device; the
1566                // future cost-lowering pass can fuse this).
1567                (None, Some(_)) => true,
1568                // Everything else (match, or no target) → no copy.
1569                _ => false,
1570            };
1571
1572            if needs_copy {
1573                let tgt = target_device.expect("needs_copy implies target is Some");
1574                // A Copy preserves the INPUT's shape/dtype, not the
1575                // outer consumer's. Read them from the source node.
1576                let (in_shape, in_dtype) = {
1577                    let n = g.node(mapped_in);
1578                    (n.shape.clone(), n.dtype)
1579                };
1580                let copy_id = g.push(Node {
1581                    op: Op::Copy { target: tgt },
1582                    inputs: vec![mapped_in],
1583                    shape: in_shape,
1584                    dtype: in_dtype,
1585                });
1586                inferred.insert(copy_id, Some(tgt));
1587                new_inputs.push(copy_id);
1588                any_changed = true;
1589            } else {
1590                if mapped_in != *input_id {
1591                    any_changed = true;
1592                }
1593                new_inputs.push(mapped_in);
1594            }
1595        }
1596
1597        // If nothing changed and this isn't a node whose inference
1598        // output device we need to record fresh, keep the original.
1599        let canonical_id = if any_changed {
1600            let new_id = g.push(Node {
1601                op: op.clone(),
1602                inputs: new_inputs,
1603                shape,
1604                dtype,
1605            });
1606            // Carry placement hint to the rewritten node so downstream
1607            // consumers see the same target.
1608            if let Some(d) = placement_hint {
1609                g.set_placement(new_id, d);
1610            }
1611            new_id
1612        } else {
1613            id
1614        };
1615
1616        // Record this node's inferred output device:
1617        //   - Op::Copy / Op::Move: output is the transfer target.
1618        //   - Else if we have a target_device, that's the output
1619        //     device (it matches all inputs post-reconciliation).
1620        //   - Else None (placeless forward).
1621        let out_device = match &op {
1622            Op::Copy { target } | Op::Move { target } => Some(*target),
1623            _ => target_device,
1624        };
1625        inferred.insert(canonical_id, out_device);
1626        remap.insert(id, canonical_id);
1627    }
1628
1629    roots.iter().map(|r| *remap.get(r).unwrap_or(r)).collect()
1630}
1631
1632/// Lower `Const` placements: if every consumer of an unplaced Const
1633/// has the same placement hint, tag the Const with that placement.
1634///
1635/// This is a pre-pass for [`insert_copies`]. Without it, a model's
1636/// weight Consts (unplaced at graph-build time) flow into ops tagged
1637/// for a specific device; insert_copies then emits a Copy for each
1638/// such Const every forward. Lowering the Const's placement directly
1639/// tells the Router to upload it straight to the target device, and
1640/// insert_copies skips the Copy.
1641///
1642/// Conservative: a Const whose consumers disagree on device stays
1643/// unplaced. A future replication pass could clone the Const to each
1644/// target, but that needs to weigh replication cost against transfer
1645/// cost — scheduler territory (Phase 4).
1646///
1647/// Returns the number of Const placements set. Mutates `graph`
1648/// placements in place.
1649pub fn lower_const_placement(graph: &SharedGraph, roots: &[NodeId]) -> usize {
1650    // Reverse edges: for each node, list its consumers. Only walks
1651    // nodes reachable from `roots`, so unused Consts don't waste work.
1652    let order = {
1653        let g = graph.read().unwrap();
1654        topo_order_multi(&g, roots)
1655    };
1656
1657    let mut consumers: HashMap<NodeId, Vec<NodeId>> = HashMap::new();
1658    {
1659        let g = graph.read().unwrap();
1660        for &nid in &order {
1661            let node = g.node(nid);
1662            for &input in &node.inputs {
1663                consumers.entry(input).or_default().push(nid);
1664            }
1665        }
1666    }
1667
1668    let mut lowered = 0;
1669    let mut g = graph.write().unwrap();
1670    for &nid in &order {
1671        // Only consider Const nodes without an explicit placement.
1672        let is_const = matches!(g.node(nid).op, Op::Const);
1673        if !is_const || g.placement(nid).is_some() {
1674            continue;
1675        }
1676        let Some(cs) = consumers.get(&nid) else {
1677            continue; // Const is a root — no consumers to infer from.
1678        };
1679        // Walk consumers; collect their placements. If all are
1680        // Some and agree, adopt that device.
1681        let mut target: Option<DeviceLocation> = None;
1682        let mut unanimous = true;
1683        for &c in cs {
1684            match g.placement(c) {
1685                Some(d) => match target {
1686                    None => target = Some(d),
1687                    Some(prev) if prev == d => {}
1688                    Some(_) => {
1689                        unanimous = false;
1690                        break;
1691                    }
1692                },
1693                None => {
1694                    unanimous = false;
1695                    break;
1696                }
1697            }
1698        }
1699        if unanimous && let Some(d) = target {
1700            g.set_placement(nid, d);
1701            lowered += 1;
1702        }
1703    }
1704    lowered
1705}
1706
1707/// Try to produce an aliasing simplification: "this op is equivalent
1708/// to one of its inputs." Returns the aliased NodeId if so.
1709fn try_simplify(op: &Op, inputs: &[NodeId]) -> Option<NodeId> {
1710    match op {
1711        // AddScalar(0) and MulScalar(1) are no-ops. Route consumers
1712        // straight to the input and drop the op.
1713        Op::AddScalar(c) if *c == 0.0 => Some(inputs[0]),
1714        Op::MulScalar(c) if *c == 1.0 => Some(inputs[0]),
1715        // PowI(1) is a no-op. PowI(0) would be "1" but that needs a
1716        // new const node with the right shape — skip here, let the
1717        // executor handle or another rule add it.
1718        Op::PowI(1) => Some(inputs[0]),
1719        _ => None,
1720    }
1721}
1722
1723// ---- Ordering analysis for destructive ops ---------------------------------
1724//
1725// A destructive op (one whose `Op::destructive_input()` is `Some(i)`)
1726// invalidates its i-th input when it runs. For correctness, every
1727// non-destructive reader of that input must complete BEFORE the
1728// destructive op runs. The graph's data-flow edges don't express this
1729// — they'd let a backend schedule the destructive op any time after
1730// its input is produced, including AHEAD of sibling readers.
1731//
1732// `derive_ordering` analyzes the graph and returns a map of
1733// destructive-op → sibling-readers. The [`execution_plan`] below
1734// consumes this map alongside the data graph in a Kahn's-algorithm
1735// walk to produce an execution order that respects both constraint
1736// kinds.
1737
1738/// Derived ordering edges beyond the data-flow graph. Each entry
1739/// `(nid, deps)` means `nid` must run after every node in `deps`.
1740///
1741/// Produced by [`derive_ordering`] from destructive-op metadata
1742/// ([`Op::destructive_input`]). Consumed by [`execution_plan`].
1743#[derive(Debug, Clone, Default)]
1744pub struct OrderingEdges(pub HashMap<NodeId, Vec<NodeId>>);
1745
1746impl OrderingEdges {
1747    pub fn new() -> Self {
1748        Self(HashMap::new())
1749    }
1750
1751    /// True if the map is empty — no destructive ops in the analyzed
1752    /// subgraph, so the execution plan is just the plain topo order.
1753    pub fn is_empty(&self) -> bool {
1754        self.0.is_empty()
1755    }
1756
1757    /// Get the set of must-run-before nodes for `nid`, if any.
1758    pub fn deps_of(&self, nid: NodeId) -> &[NodeId] {
1759        self.0.get(&nid).map(|v| v.as_slice()).unwrap_or(&[])
1760    }
1761}
1762
1763/// Fuse `MatMul → Add(rank-1 bias)` patterns into `Op::FusedLinear`
1764/// (Phase 6d Track 3). Walks the graph; for each `Add` whose LHS is
1765/// a `MatMul` and whose RHS is a rank-1 bias whose length equals the
1766/// matmul output's last dim, emits a fresh `FusedLinear` node and
1767/// remaps consumers of the `Add` to it.
1768///
1769/// Conservative: only fires when the `Add` is the sole consumer of
1770/// the `MatMul`. Otherwise we'd be creating a duplicate matmul
1771/// computation. CSE doesn't help here because `MatMul` and
1772/// `MatMul-inside-FusedLinear` aren't structurally equal at the IR
1773/// level — backends with truly fused kernels are the ones that
1774/// benefit, so we'd rather skip the fusion than waste the work.
1775///
1776/// Returns the count of fusions applied.
1777pub fn fuse_linear(graph: &SharedGraph, roots: &[NodeId]) -> usize {
1778    let order = {
1779        let g = graph.read().unwrap();
1780        topo_order_multi(&g, roots)
1781    };
1782    // Count consumers of each node (so we can guard "single consumer of matmul").
1783    let mut consumer_count: HashMap<NodeId, usize> = HashMap::new();
1784    {
1785        let g = graph.read().unwrap();
1786        for &nid in &order {
1787            for &input in &g.node(nid).inputs {
1788                *consumer_count.entry(input).or_insert(0) += 1;
1789            }
1790        }
1791        // Also count root references — a root is implicitly a consumer.
1792        for &r in roots {
1793            *consumer_count.entry(r).or_insert(0) += 1;
1794        }
1795    }
1796
1797    let mut g = graph.write().unwrap();
1798    let mut remap: HashMap<NodeId, NodeId> = HashMap::new();
1799    let mut fused = 0usize;
1800
1801    for nid in order {
1802        // Apply already-known remappings to inputs.
1803        let (op, inputs, shape, dtype) = {
1804            let n = g.node(nid);
1805            (n.op.clone(), n.inputs.clone(), n.shape.clone(), n.dtype)
1806        };
1807        let mapped: Vec<NodeId> = inputs.iter().map(|i| *remap.get(i).unwrap_or(i)).collect();
1808        // Pattern: Op::Add { inputs[0]=matmul_node, inputs[1]=rank-1 bias }.
1809        if !matches!(op, Op::Add) || mapped.len() != 2 {
1810            continue;
1811        }
1812        let lhs = mapped[0];
1813        let rhs = mapped[1];
1814        // LHS must be a MatMul.
1815        let lhs_op = g.node(lhs).op.clone();
1816        if !matches!(lhs_op, Op::MatMul) {
1817            continue;
1818        }
1819        // LHS matmul must have only THIS Add as a consumer (otherwise
1820        // fusing would duplicate the matmul computation).
1821        // Note: consumer_count counts pre-remapping references; remap
1822        // only happens for skipped nodes here so it's still valid.
1823        if consumer_count.get(&lhs).copied().unwrap_or(0) != 1 {
1824            continue;
1825        }
1826        // RHS must be a BroadcastTo of a rank-1 bias whose length
1827        // equals the matmul output's last dim. The build-time `Add`
1828        // requires same-shape inputs, so user code typically does:
1829        //     bias[N].broadcast_to([..., M, N]).add(matmul_out)
1830        // Walk through that BroadcastTo to find the rank-1 source.
1831        let mm_dims = g.node(lhs).shape.dims().to_vec();
1832        if mm_dims.is_empty() {
1833            continue;
1834        }
1835        let last_dim = mm_dims[mm_dims.len() - 1];
1836        let rhs_node = g.node(rhs);
1837        let bias_src_id = if matches!(rhs_node.op, Op::BroadcastTo(_)) && rhs_node.inputs.len() == 1
1838        {
1839            *remap
1840                .get(&rhs_node.inputs[0])
1841                .unwrap_or(&rhs_node.inputs[0])
1842        } else {
1843            // Bias broadcast may also have been pre-shaped; allow rank-1
1844            // direct (rare with build-time shape checks but cheap to
1845            // recognize).
1846            rhs
1847        };
1848        let bias_dims = g.node(bias_src_id).shape.dims().to_vec();
1849        if bias_dims.len() != 1 || bias_dims[0] != last_dim {
1850            continue;
1851        }
1852        // Pull the matmul's a, b inputs (apply remap to those too).
1853        let mm_inputs = g.node(lhs).inputs.clone();
1854        if mm_inputs.len() != 2 {
1855            continue;
1856        }
1857        let a = *remap.get(&mm_inputs[0]).unwrap_or(&mm_inputs[0]);
1858        let b = *remap.get(&mm_inputs[1]).unwrap_or(&mm_inputs[1]);
1859        // Phase 7.6 step 4: emit the registry-extended shape. The
1860        // executor's `Op::Fused(FUSED_LINEAR, _)` arm dispatches to
1861        // the same fused-linear kernel as the legacy variant. Step 5
1862        // drops the legacy `Op::FusedLinear` variant entirely.
1863        let new_id = g.push(Node {
1864            op: Op::Fused(
1865                crate::registry::FusedOps::FUSED_LINEAR,
1866                crate::registry::FusedOpParams::FusedLinear,
1867            ),
1868            // FusedLinear takes the *original* rank-1 bias, not the
1869            // BroadcastTo'd one — the kernel broadcasts it internally
1870            // to the matmul output shape.
1871            inputs: vec![a, b, bias_src_id],
1872            shape,
1873            dtype,
1874        });
1875        remap.insert(nid, new_id);
1876        fused += 1;
1877    }
1878
1879    // Apply remap by rewriting any consumer that still references an
1880    // un-fused Add. Using the existing `rewrite_input` helper.
1881    if !remap.is_empty() {
1882        // Collect all node ids; iterate to update inputs that point at
1883        // remapped nodes. We need the mutable borrow; clone the set of
1884        // nodes to iterate without borrow conflicts.
1885        let n_nodes = g.nodes.len();
1886        for nid in 0..n_nodes {
1887            let node = &mut g.nodes[nid];
1888            for input in node.inputs.iter_mut() {
1889                if let Some(&new) = remap.get(input) {
1890                    *input = new;
1891                }
1892            }
1893        }
1894    }
1895    fused
1896}
1897
1898/// Derive ordering edges for every destructive op reachable from
1899/// `roots`. Result: `nid` → list of other readers of `nid`'s
1900/// destroyed input.
1901///
1902/// **Bundle-aware aliasing** (Option C, Session 3): the alias-set
1903/// computation treats `Op::View { slot }` as alias-extending — every
1904/// View of a multi-output producer shares the bundle's storage Arc, so
1905/// a destructive op on the producer (or on any sibling View) must
1906/// run after every reader of every other View of the same producer.
1907/// `Op::ViewOwned` is NOT alias-extending: after its forward memcpy,
1908/// the ViewOwned's output is an independent Storage; it is still
1909/// pinned-after destructive ops on the producer via the regular
1910/// data-dependency edge (`inputs[0] == producer`), which falls out
1911/// of the standard reader analysis below without needing the
1912/// alias-set extension. The full alias-extension rule covers
1913/// `Op::Reshape` / `Op::Contiguize`
1914/// (conditionally-zero-copy contiguize ops), added to close the MLA
1915/// decode multi-round `WriteSlice` ordering defect: a destructive op
1916/// on `X` only pins the readers of nodes actually IN the alias set,
1917/// so any alias-extending op excluded from that set is a hole a
1918/// reader can fall through unpinned. `Op::Reshape`/`Op::Contiguize`
1919/// were exactly that hole (see `collect_alias_set`'s doc for the
1920/// full mechanism and the shipped workaround this makes unnecessary
1921/// for correctness).
1922///
1923/// O(V + E). Does not mutate the graph.
1924pub fn derive_ordering(graph: &crate::Graph, roots: &[NodeId]) -> OrderingEdges {
1925    let order = topo_order_multi(graph, roots);
1926
1927    // Consumer index: tensor NodeId → list of consumers of that tensor.
1928    let mut consumers: HashMap<NodeId, Vec<NodeId>> = HashMap::new();
1929    for &nid in &order {
1930        for &input in &graph.node(nid).inputs {
1931            consumers.entry(input).or_default().push(nid);
1932        }
1933    }
1934
1935    // Alias groups, built ONCE for the whole graph instead of re-derived
1936    // per destructive op (`collect_alias_set` used to re-walk the entire
1937    // `order` from scratch for every one of them — O(destructive_ops *
1938    // order.len())). Every alias-extending node (view/bundle-projection/
1939    // reshape-contiguize — see `alias_group_of`'s doc) points at the SAME
1940    // group id as whatever it extends, in a single forward pass; querying
1941    // a group's members is then an O(1) map lookup. See `alias_group_of`
1942    // for why this produces the identical partition `collect_alias_set`
1943    // did, including the sibling-bundle case.
1944    let (alias_group_of, alias_members) = alias_groups(graph, &order);
1945
1946    let mut ordering: HashMap<NodeId, Vec<NodeId>> = HashMap::new();
1947    for &nid in &order {
1948        let node = graph.node(nid);
1949        let Some(d_idx) = node.op.destructive_input() else {
1950            continue;
1951        };
1952        if d_idx >= node.inputs.len() {
1953            continue;
1954        }
1955        let destroyed = node.inputs[d_idx];
1956        // The alias set of `destroyed`: the destructive op writes
1957        // through `destroyed`'s Storage Arc, and every view-op node
1958        // transitively derived from `destroyed` shares that same Arc
1959        // (executor's view-op materialization wraps the source's Arc
1960        // in a fresh Layout — see `is_view_op` in `lib.rs`). So a
1961        // reader of any alias-set member sees `destroyed`'s bytes
1962        // and must run BEFORE the destructive op. Without this
1963        // walk, `y = x.transpose(); z = relu(y); x.relu_inplace()`
1964        // would not pin `relu(y)` before `relu_inplace(x)`, even
1965        // though they share storage.
1966        let group = alias_group_of.get(&destroyed).copied().unwrap_or(destroyed);
1967        // `destroyed` itself is always a member, even when nothing
1968        // aliases it (the common case: no entry in `alias_members` at
1969        // all) — mirrors `collect_alias_set`'s unconditional seed
1970        // `alias.insert(root)`. Without this fallback a non-aliased
1971        // target's own direct readers would never get visited below.
1972        let singleton = vec![group];
1973        let alias_set = alias_members.get(&group).unwrap_or(&singleton);
1974        for &alias in alias_set {
1975            let Some(readers) = consumers.get(&alias) else {
1976                continue;
1977            };
1978            for &reader in readers {
1979                // Skip the destructive op itself + readers that are
1980                // themselves alias members (those are view ops; their
1981                // own consumers are what we actually need to pin, and
1982                // they show up in subsequent iterations).
1983                if reader != nid && !alias_set.contains(&reader) {
1984                    ordering.entry(nid).or_default().push(reader);
1985                }
1986            }
1987        }
1988    }
1989    OrderingEdges(ordering)
1990}
1991
1992/// Collect the alias set of `root`: every node whose realized Storage
1993/// is the same `Arc<RwLock<Storage>>` as `root`'s.
1994///
1995/// Three families of alias-extending op share the input's Storage Arc
1996/// at realize time:
1997///
1998/// 1. **Single-input view ops** ([`Op::is_view_op`]): the executor
1999///    wraps the source's Arc with a fresh Layout — no copy. Examples:
2000///    `Op::Transpose`, `Op::Permute`, `Op::Slice`, `Op::Unsqueeze`,
2001///    `Op::Squeeze`, `Op::BroadcastTo`, `Op::Flip`.
2002/// 2. **Multi-output projection** (`Op::View { slot }`): the
2003///    realized View clones the producer's bundled storage Arc and
2004///    exposes one slot's window — bytes are shared, the bundle's Arc
2005///    refcount tracks the consumer's lifetime (Session 1 of the
2006///    multi-output Option C design — see
2007///    [`fuel_ir::storage::OutputView`]).
2008/// 3. **Conditionally zero-copy contiguize ops** (`Op::Reshape`,
2009///    `Op::Contiguize`): `fuel-dispatch`'s `WorkItemKind::ContiguizeOf`
2010///    execution arm ADOPTS the input's Storage Arc verbatim — no
2011///    allocation, no copy — whenever the input layout is already
2012///    contiguous at offset 0; only a non-contiguous or offset input
2013///    forces a fresh `auto_contiguize` allocation. Because that check
2014///    is a RUNTIME layout fact `collect_alias_set` cannot evaluate at
2015///    graph-build time (offset-0-contiguity depends on the layouts
2016///    realized upstream), Reshape/Contiguize are treated as
2017///    UNCONDITIONALLY alias-extending here — conservative in the
2018///    (rarer) case they materialize a fresh copy, since an extra
2019///    ordering pin only serializes two ops that didn't actually need
2020///    it; never unsafe, since the alternative (treating them as
2021///    alias-opaque) is exactly the ordering defect this variant
2022///    fixes: a destructive op's OWN direct alias-set members were
2023///    already pinned-after-their-readers correctly, but a
2024///    Reshape/Contiguize hop is a graph-visible node in its own right
2025///    — if it's excluded from the alias set, its consumers are never
2026///    visited by the pin loop below, so a reader on the OTHER side of
2027///    an adopted-Arc Reshape/Contiguize has no ordering guarantee
2028///    against a destructive op reusing the SAME storage. This is the
2029///    MLA decode multi-round `WriteSlice` defect: layer `L`'s
2030///    post-append cache buffer is read once (non-destructively)
2031///    through a `Slice` → `Reshape` (head-split-style) → kernel chain
2032///    that becomes an ancestor of layer `L+1`'s write, and again as
2033///    the destination of layer `L`'s NEXT round's append — see
2034///    `fuel-core/src/lazy_deepseek2.rs`'s
2035///    `DeepSeek2Model::rebind_latent_cache_fresh_graph` doc, whose
2036///    graph-rebinding workaround this fix makes unnecessary for
2037///    CORRECTNESS (rebinding remains a legitimate memory/perf choice —
2038///    it also caps unbounded graph growth across steps — but a
2039///    same-graph multi-step decode no longer silently corrupts or
2040///    hard-errors without it).
2041///
2042/// `Op::ViewOwned` is explicitly NOT alias-extending: at execution
2043/// time it allocates a fresh standalone Storage and memcpys the
2044/// slot's bytes in. After the memcpy runs, no Arc handle on the
2045/// producer's bundle remains in the ViewOwned's chain. ViewOwned is
2046/// still pinned-after destructive readers via the regular
2047/// data-dependency edge (its `inputs[0]` is the producer), which the
2048/// caller already accounts for outside this function.
2049///
2050/// **Sibling-bundle rule**: when `root` is itself an `Op::View`,
2051/// every other `Op::View` of the same producer shares the same
2052/// bundle Arc. To capture them, the function pre-seeds the producer
2053/// into the alias set so the forward walk picks up siblings the same
2054/// way it picks up downstream `is_view_op` derivatives.
2055///
2056/// Implementation: O(|order|) forward walk after a constant-time
2057/// pre-seed. A node enters the alias set iff its op is in the
2058/// alias-extending union AND its `inputs[0]` is already in the set.
2059/// Builds the alias-equivalence partition over the WHOLE `order` in a
2060/// single forward pass, replacing the old `collect_alias_set`'s
2061/// per-destructive-op walk of the entire `order` (O(destructive_ops *
2062/// order.len()) — a second, independent quadratic cost alongside
2063/// `insert_safety_copies`' own, confirmed as the dominant cost in a live
2064/// decode-step profile: ~55% of total step time went to this family of
2065/// per-call graph rebuilds).
2066///
2067/// Every alias-extending node (the same three families as before: single-
2068/// input view ops, `Op::View{slot}` bundle projections, and the
2069/// conservatively-always-aliasing `Reshape`/`Contiguize`) is mapped to the
2070/// SAME group id as whatever it extends; a group's id is always its
2071/// earliest (topologically first) non-alias-extending member. Returns
2072/// `(group_of, members)`: `group_of[n]` is `n`'s group id (only present
2073/// for alias-extending nodes — absent means `n` is its own trivial
2074/// singleton group), and `members[g]` is every node in group `g`,
2075/// `g` itself included.
2076///
2077/// This produces the IDENTICAL partition the old per-call
2078/// `collect_alias_set(root, ...)` computed when called with `root` equal
2079/// to the group's EARLIEST member — the case every existing call site
2080/// exercises (`derive_ordering` always queries with a destructive op's
2081/// direct input, and a destructive op's direct input was never itself
2082/// observed, in this codebase's test suite, to be a multi-hop view
2083/// chain) — INCLUDING the sibling-bundle case (`Op::View{slot}`'s shared
2084/// producer `P`): the first `Op::View` of `P` resolves to group `P`
2085/// (since `P` is never itself inserted as a key, so `.unwrap_or(&P)`
2086/// defaults to `P`); every later `Op::View` of the same `P` resolves to
2087/// the identical group, landing all of them — and `P` itself — in one
2088/// membership list, exactly what `collect_alias_set`'s explicit "root is
2089/// a View ⇒ also seed its producer" special case achieved per-call.
2090///
2091/// ⚠️ NOT proven byte-identical for a query against a NON-earliest member
2092/// of a multi-hop alias chain (e.g. `destroyed` being a `Slice` of a
2093/// `Transpose` of a plain producer): the old per-call walk only ever
2094/// extended FORWARD from its seed, so querying from a late chain member
2095/// could under-report versus querying from the chain's root — a
2096/// pre-existing asymmetry in the old code, not introduced here. This
2097/// version computes the full, symmetric equivalence class regardless of
2098/// which member is queried (matching the function's own doc contract:
2099/// "every node whose realized Storage is the same Arc as root's"), which
2100/// can only ADD members relative to the old behavior, never drop any —
2101/// so if the two ever disagree, this version is the more conservative
2102/// (more copies/pins, never fewer) of the two. All existing
2103/// `insert_safety_copies`/`derive_ordering` tests pass unchanged.
2104fn alias_groups(
2105    graph: &crate::Graph,
2106    order: &[NodeId],
2107) -> (HashMap<NodeId, NodeId>, HashMap<NodeId, Vec<NodeId>>) {
2108    use crate::Op;
2109
2110    let mut group_of: HashMap<NodeId, NodeId> = HashMap::new();
2111    let mut members: HashMap<NodeId, Vec<NodeId>> = HashMap::new();
2112
2113    for &nid in order {
2114        let node = graph.node(nid);
2115        // Op::View shares the producer's bundle Arc — it extends the
2116        // alias set even though it isn't an `is_view_op` in the
2117        // narrower single-storage-strided-view sense. Op::Reshape /
2118        // Op::Contiguize are CONDITIONALLY zero-copy (adopt the input
2119        // Arc iff contiguous at offset 0 — a runtime fact); treated
2120        // unconditionally alias-extending here is the conservative,
2121        // always-safe direction (see the function doc).
2122        let extends_alias = node.op.is_view_op()
2123            || matches!(node.op, Op::View { .. } | Op::Reshape(_) | Op::Contiguize);
2124        if !extends_alias {
2125            continue;
2126        }
2127        let Some(&inp) = node.inputs.first() else {
2128            continue;
2129        };
2130        let group = group_of.get(&inp).copied().unwrap_or(inp);
2131        group_of.insert(nid, group);
2132        members
2133            .entry(group)
2134            .or_insert_with(|| vec![group])
2135            .push(nid);
2136    }
2137    (group_of, members)
2138}
2139
2140/// Insert defensive `Op::Copy` snapshots ahead of destructive ops
2141/// that would otherwise produce a data-flow ↔ ordering-edge cycle.
2142///
2143/// Phase 5 of the in-place ops infrastructure
2144/// (`docs/session-prompts/in-place-ops-infrastructure.md`). The
2145/// view-aware [`derive_ordering`] (Phase 4a) handles the common case
2146/// where a destructive op's target is read only by upstream consumers
2147/// — pinning the destructive op after those reads is enough. The
2148/// case Phase 5 addresses is when a single op reads BOTH the
2149/// in-place target X AND a downstream consumer of the in-place op
2150/// (e.g., residual connections: `y = x.relu_inplace(); z = y + x`).
2151///
2152/// In that case, the data-flow says the reader (`z`) must run after
2153/// the destructive op (because it depends on `y`), and the ordering
2154/// edge says the destructive op must run after the reader (because
2155/// the reader reads `x`). Without intervention, [`execution_plan`]
2156/// detects the cycle and panics.
2157///
2158/// ## Conflict predicate (dependency-based, NOT topo-position-based)
2159///
2160/// A reader `R` of the destructive op `D`'s target needs a safety
2161/// copy iff `R` is not **provably ordered before** `D`. The proof
2162/// obligation is grounded in the executor's actual contract:
2163/// [`execution_plan`] emits a linear extension of the *combined*
2164/// precedence graph — data edges (`input → consumer`) plus
2165/// [`derive_ordering`]'s ordering edges (`reader → destructive op`) —
2166/// and the executor dispatches work items strictly sequentially in
2167/// that order. Therefore:
2168///
2169/// - `R` has a path to `D` in the combined graph AND `D` has no
2170///   path back to `R` → every valid plan runs `R` before `D`'s
2171///   mutation. Safe; **no copy**. (This covers every reader
2172///   `derive_ordering` pins directly, plus view-op readers whose
2173///   consumers are pinned — the pre-gap readers of an evict-chain
2174///   `Op::Move`, for example.)
2175/// - `D` has a path to `R` (e.g., the residual pattern: `R`
2176///   consumes `D`'s output while also reading `D`'s target) → the
2177///   pin edge `R → D` would close a cycle; the executor cannot
2178///   order `R` first. **Copy needed.**
2179/// - Neither direction → no ordering guarantee exists (possible for
2180///   alias-member view readers, which `derive_ordering` deliberately
2181///   does not pin). Safety cannot be proven, so **copy needed** —
2182///   the conservative default: a spurious copy costs bytes, a
2183///   missing copy is silent data corruption.
2184///
2185/// Historical note: this pass originally inferred conflicts from DFS
2186/// topo-order *position* (any reader appearing after `D` in
2187/// `topo_order_multi` order was treated as conflicting). Position in
2188/// a DFS walk carries no dependency information — an independent
2189/// pre-gap reader of a `Op::Move`d tensor could land after the Move
2190/// purely because of root visit order, earning a spurious copy
2191/// stamped onto the destructive op's device (live-GPU failure,
2192/// 2026-06-11).
2193///
2194/// This pass rewrites each conflict by:
2195/// 1. Inserting an `Op::Copy { target: same_device }(X) → X_safe` —
2196///    a same-device byte snapshot independent of `X`'s storage.
2197/// 2. Rewiring the conflicting reader's input from `X` to `X_safe`.
2198///
2199/// After the rewrite, the reader reads the snapshot (pre-mutation
2200/// bytes) while the destructive op proceeds on `X`. No cycle.
2201///
2202/// Scope: detects DIRECT conflicts (reader's input IS the
2203/// destructive op's target). View-mediated conflicts (reader reads
2204/// `view(X)`) fall through and produce the cycle panic from
2205/// `execution_plan` — a clear-but-not-friendly error pointing the
2206/// user at the pattern. View-mediated auto-resolution is a follow-up
2207/// (would need to insert `Copy(X)` then re-derive the view from the
2208/// snapshot for each conflicting reader).
2209///
2210/// Idempotent: calling this twice on the same graph inserts copies
2211/// at most once per conflict (the rewrite removes the conflict by
2212/// redirecting the reader's input edge to the copy).
2213///
2214/// Returns the number of safety copies inserted (for telemetry /
2215/// testing).
2216pub fn insert_safety_copies(graph: &mut crate::Graph, roots: &[NodeId]) -> usize {
2217    /// Default [`DeviceLocation`] for a `BackendId` when the graph has
2218    /// no per-node `placement` set. Single-GPU convention (gpu_id 0) —
2219    /// mirrors `fuel_dispatch::plan::default_device_for`, duplicated
2220    /// here because `fuel-graph` doesn't depend on `fuel-dispatch`.
2221    /// `insert_safety_copies`' own `target_location` fallback is this
2222    /// function's sole caller.
2223    fn device_location_for_backend(backend: fuel_ir::probe::BackendId) -> DeviceLocation {
2224        use fuel_ir::probe::BackendId;
2225        match backend {
2226            BackendId::Cpu => DeviceLocation::Cpu,
2227            BackendId::Cuda => DeviceLocation::Cuda { gpu_id: 0 },
2228            BackendId::Vulkan => DeviceLocation::Vulkan { gpu_id: 0 },
2229            BackendId::Metal => DeviceLocation::Metal { gpu_id: 0 },
2230            _ => DeviceLocation::Cpu,
2231        }
2232    }
2233
2234    let order = topo_order_multi(graph, roots);
2235
2236    // Combined precedence graph: data edges (input → consumer) plus
2237    // derive_ordering's edges (reader → destructive op). This is
2238    // exactly the edge set execution_plan linearizes, so path
2239    // queries here answer "does every valid plan order A before B?".
2240    let ordering = derive_ordering(graph, roots);
2241    let node_set: std::collections::HashSet<NodeId> = order.iter().copied().collect();
2242    let mut succ: HashMap<NodeId, Vec<NodeId>> = HashMap::new();
2243    let mut pred: HashMap<NodeId, Vec<NodeId>> = HashMap::new();
2244    for &nid in &order {
2245        for &input in &graph.node(nid).inputs {
2246            if node_set.contains(&input) {
2247                succ.entry(input).or_default().push(nid);
2248                pred.entry(nid).or_default().push(input);
2249            }
2250        }
2251        for &dep in ordering.deps_of(nid) {
2252            if node_set.contains(&dep) {
2253                succ.entry(dep).or_default().push(nid);
2254                pred.entry(nid).or_default().push(dep);
2255            }
2256        }
2257    }
2258
2259    // All nodes reachable from `start` over `adj` (excluding `start`
2260    // itself unless it sits on a cycle). Iterative DFS; the visited
2261    // set terminates cyclic combined graphs (the very cycles this
2262    // pass exists to break).
2263    fn reach(
2264        start: NodeId,
2265        adj: &HashMap<NodeId, Vec<NodeId>>,
2266    ) -> std::collections::HashSet<NodeId> {
2267        let mut seen: std::collections::HashSet<NodeId> = std::collections::HashSet::new();
2268        let mut stack = vec![start];
2269        while let Some(n) = stack.pop() {
2270            if let Some(next) = adj.get(&n) {
2271                for &m in next {
2272                    if seen.insert(m) {
2273                        stack.push(m);
2274                    }
2275                }
2276            }
2277        }
2278        seen
2279    }
2280
2281    // Snapshot conflicts before mutating the graph: collect
2282    // (destructive_op_id, target, conflicting_readers) tuples.
2283    struct Conflict {
2284        destructive_nid: NodeId,
2285        target: NodeId,
2286        target_shape: crate::Shape,
2287        target_dtype: DType,
2288        conflicting_readers: Vec<NodeId>,
2289    }
2290    let mut conflicts: Vec<Conflict> = Vec::new();
2291
2292    for &nid in &order {
2293        let node = graph.node(nid);
2294        let Some(d_idx) = node.op.destructive_input() else {
2295            continue;
2296        };
2297        if d_idx >= node.inputs.len() {
2298            continue;
2299        }
2300        let target = node.inputs[d_idx];
2301
2302        // Cheap pre-check: skip BOTH `reach()` walks and the
2303        // `order.len()`-sized readers scan below entirely when `target`
2304        // provably has no OTHER reader at all — the common case in a real
2305        // decode graph (KV-cache writes, release markers: nothing besides
2306        // the destructive op itself touches their target). `succ` was
2307        // already built above from EVERY node's `.inputs` edges (plus
2308        // ordering deps) for every node whose INPUT is in `node_set`, so
2309        // when `target` is in `node_set`, `succ[target]` is the COMPLETE
2310        // set of nodes with `target` as a direct input or an ordering
2311        // dependent — a superset of what the scan below finds (ordering
2312        // deps can appear here without a matching direct-input reader, but
2313        // never the reverse). Empty there (besides `nid` itself) therefore
2314        // guarantees the scan would find nothing either, so this cannot
2315        // change which copies get inserted — it only skips redundant work.
2316        // Falls through to the full computation when `target` isn't in
2317        // `node_set` (rare; `succ` doesn't cover it there).
2318        if node_set.contains(&target) {
2319            let any_other_reader = succ
2320                .get(&target)
2321                .is_some_and(|rs| rs.iter().any(|&r| r != nid));
2322            if !any_other_reader {
2323                continue;
2324            }
2325        }
2326
2327        // `forced_before`: nodes with a combined-graph path TO the
2328        // destructive op. `forced_after`: nodes the destructive op
2329        // has a combined-graph path to. A reader is provably safe
2330        // iff it is forced-before AND not forced-after (the latter
2331        // would mean its pin edge closes a cycle the executor cannot
2332        // satisfy). Anything else — cycle members, descendants, or
2333        // readers with no ordering guarantee in either direction —
2334        // gets a safety copy. False positives cost a snapshot;
2335        // false negatives are silent data corruption.
2336        let forced_before = reach(nid, &pred);
2337        let forced_after = reach(nid, &succ);
2338
2339        let mut readers: Vec<NodeId> = Vec::new();
2340        for &maybe_reader in &order {
2341            if maybe_reader == nid {
2342                continue;
2343            }
2344            if !graph.node(maybe_reader).inputs.contains(&target) {
2345                continue;
2346            }
2347            let provably_before =
2348                forced_before.contains(&maybe_reader) && !forced_after.contains(&maybe_reader);
2349            if !provably_before {
2350                readers.push(maybe_reader);
2351            }
2352        }
2353        if readers.is_empty() {
2354            continue;
2355        }
2356
2357        let target_node = graph.node(target);
2358        conflicts.push(Conflict {
2359            destructive_nid: nid,
2360            target,
2361            target_shape: target_node.shape.clone(),
2362            target_dtype: target_node.dtype,
2363            conflicting_readers: readers,
2364        });
2365    }
2366
2367    let inserted = conflicts.len();
2368    for Conflict {
2369        destructive_nid,
2370        target,
2371        target_shape,
2372        target_dtype,
2373        conflicting_readers,
2374    } in conflicts
2375    {
2376        // Pick the copy's target_location. Prefer the target's own
2377        // placement (if any) — that's the device the data lives on.
2378        // Fall back to the destructive op's placement. Most graphs
2379        // populate NEITHER `placement` (a rarely-used explicit pin,
2380        // `Graph::set_placement` / `.on_device(..)`) — ordinary
2381        // single-device `optimize_graph` only stamps the coarser
2382        // `target_backend` (`BackendId`, no gpu_id) — so fall back
2383        // further to a same-device location DERIVED from
2384        // `target_backend` before ever reaching for `Cpu`. Getting
2385        // this wrong silently strands a same-device CUDA/Vulkan
2386        // safety snapshot on the host: the (unrelated) reader this
2387        // copy exists to protect keeps its original device, so the
2388        // optimizer's own backend-follows-data logic then drags the
2389        // reader onto CPU too, and — the capture-specific failure
2390        // mode a same-device CUDA decode graph hits — an ordinary
2391        // same-device snapshot becomes a genuinely cross-device D2H
2392        // that can never be part of a CUDA graph capture (see
2393        // `fuel_dispatch::pipelined::capture_decode`, which only
2394        // ever accepts `Op::Copy` when `target` is same-device Cuda).
2395        // Only truly unplaced AND unbackended nodes (neither hint
2396        // available anywhere) fall all the way through to `Cpu`.
2397        let target_location = graph
2398            .placement(target)
2399            .or_else(|| graph.placement(destructive_nid))
2400            .or_else(|| {
2401                graph
2402                    .target_backend(target)
2403                    .or_else(|| graph.target_backend(destructive_nid))
2404                    .map(device_location_for_backend)
2405            })
2406            .unwrap_or(DeviceLocation::Cpu);
2407        let copy_id = graph.push(crate::Node {
2408            op: crate::Op::Copy {
2409                target: target_location,
2410            },
2411            inputs: vec![target],
2412            shape: target_shape,
2413            dtype: target_dtype,
2414        });
2415        // Propagate target_backend so the executor can look up the
2416        // copy's wrapper. Inherit from the target (the source of the
2417        // copy), falling back to the destructive op's target_backend.
2418        if let Some(backend) = graph
2419            .target_backend(target)
2420            .or_else(|| graph.target_backend(destructive_nid))
2421        {
2422            graph.set_target_backend(copy_id, backend);
2423        }
2424        for reader_id in conflicting_readers {
2425            graph.rewrite_input(reader_id, target, copy_id);
2426        }
2427    }
2428    inserted
2429}
2430
2431/// Walk every multi-output producer in the graph and promote some of
2432/// its `Op::View` consumers to `Op::ViewOwned` when slot lifetimes
2433/// are sufficiently asymmetric that keeping the whole bundle alive
2434/// for the longest-lived slot is wasteful.
2435///
2436/// v1 heuristic (Option C, Session 2): for each multi-output producer
2437/// P, compute the per-slot "last use" position in topo order (the
2438/// max position over each slot's Views and their transitive
2439/// consumers). Slots whose last use is strictly later than at least
2440/// one other slot's last use get all their Views promoted to
2441/// `Op::ViewOwned`. The remaining short-lived slots stay as
2442/// `Op::View` so the bundle drops naturally when their last consumer
2443/// finishes.
2444///
2445/// Concrete example: SelectiveScan's `y` (slot 0) consumed at the
2446/// next layer; `last_state` (slot 1) retained across a barrier into
2447/// the next autoregressive step. `last_state`'s last use is much
2448/// later → it gets promoted to `ViewOwned`, the bundle drops once
2449/// `y`'s consumer finishes, and `last_state`'s standalone copy
2450/// survives across the gap.
2451///
2452/// Implementation: builds a consumer index over the topo order,
2453/// computes `last_use[N] = max(pos[N], last_use[consumers])` by
2454/// reverse-iterating, then per producer makes a per-slot rollup
2455/// and rewrites edges from the promoted Views to fresh
2456/// `Op::ViewOwned` nodes. Returns the number of promotions made.
2457/// Idempotent on already-promoted Views (an `Op::ViewOwned` is a
2458/// fixpoint).
2459///
2460/// **Where this plugs in:** today the multi-output infra has no
2461/// production consumer (Session 2 ships the authoring contract and
2462/// this pass; consumers light up in the
2463/// `selective-scan-ssd-chunk-multi-output-followup` session). The
2464/// pass is a no-op on graphs without any `Op::View` consumers of a
2465/// multi-output producer — the common case until consumers migrate.
2466/// Long-term it likely runs after the ranker/picker pass and before
2467/// `compile_plan`; placement is the picker session's call.
2468pub fn promote_views_for_liveness(graph: &mut crate::Graph, roots: &[NodeId]) -> usize {
2469    use crate::Op;
2470
2471    let order = topo_order_multi(graph, roots);
2472    if order.is_empty() {
2473        return 0;
2474    }
2475    // Forward consumer index: producer NodeId → list of consumer NodeIds.
2476    let mut consumers: HashMap<NodeId, Vec<NodeId>> = HashMap::new();
2477    for &nid in &order {
2478        for &input in &graph.node(nid).inputs {
2479            consumers.entry(input).or_default().push(nid);
2480        }
2481    }
2482
2483    // `depth[N]`: longest-input-chain length feeding into N. Two
2484    // sibling consumers in the same conceptual layer get the same
2485    // depth, so the lifetime comparison below treats them symmetrically
2486    // even when topo *position* puts them in different slots of the
2487    // walk (which depends on roots[] order and traversal direction —
2488    // unstable for our purposes).
2489    let mut depth: HashMap<NodeId, usize> = HashMap::with_capacity(order.len());
2490    for &nid in &order {
2491        let mut d = 0usize;
2492        for &inp in &graph.node(nid).inputs {
2493            if let Some(&di) = depth.get(&inp)
2494                && di + 1 > d
2495            {
2496                d = di + 1;
2497            }
2498        }
2499        depth.insert(nid, d);
2500    }
2501
2502    // `last_use[N]`: max downstream depth reachable from N (including
2503    // N itself). Computed by reverse-iterating the topo order and
2504    // propagating each node's depth back to its inputs.
2505    let mut last_use: HashMap<NodeId, usize> = HashMap::with_capacity(order.len());
2506    for &nid in order.iter().rev() {
2507        let mut lu = depth[&nid];
2508        if let Some(downstream) = consumers.get(&nid) {
2509            for &c in downstream {
2510                if let Some(&cl) = last_use.get(&c)
2511                    && cl > lu
2512                {
2513                    lu = cl;
2514                }
2515            }
2516        }
2517        last_use.insert(nid, lu);
2518    }
2519
2520    // Find every multi-output producer that has any View / ViewOwned
2521    // consumers in this graph. Per producer: bucket the Views by slot
2522    // and compute the per-slot last-use position.
2523    struct ViewEntry {
2524        view_id: NodeId,
2525        slot: u32,
2526        is_owned: bool,
2527    }
2528    let mut by_producer: HashMap<NodeId, Vec<ViewEntry>> = HashMap::new();
2529    for &nid in &order {
2530        let node = graph.node(nid);
2531        let (slot, is_owned) = match node.op {
2532            Op::View { slot } => (slot, false),
2533            Op::ViewOwned { slot } => (slot, true),
2534            _ => continue,
2535        };
2536        // View / ViewOwned have a single input (the producer).
2537        let Some(&producer) = node.inputs.first() else {
2538            continue;
2539        };
2540        // Only meaningful if producer was declared multi-output;
2541        // otherwise it's an authoring bug that the NodeHandle::view
2542        // builder already rejected. Defensive skip.
2543        if !graph.is_multi_output(producer) {
2544            continue;
2545        }
2546        by_producer.entry(producer).or_default().push(ViewEntry {
2547            view_id: nid,
2548            slot,
2549            is_owned,
2550        });
2551    }
2552
2553    // For each producer, decide which slots get promoted and collect
2554    // the per-View promotions. Defer the actual graph mutations to a
2555    // second pass so we can iterate the analysis with `&Graph`.
2556    struct Promotion {
2557        view_id: NodeId,
2558        slot: u32,
2559        view_shape: crate::Shape,
2560        view_dtype: DType,
2561        // Consumers of `view_id` at analysis time — every edge gets
2562        // rewired to the new ViewOwned node.
2563        view_consumers: Vec<NodeId>,
2564    }
2565    let mut promotions: Vec<Promotion> = Vec::new();
2566    for views in by_producer.values() {
2567        // Per-slot last use rollup.
2568        let mut slot_last_use: HashMap<u32, usize> = HashMap::new();
2569        for v in views {
2570            let lu = last_use[&v.view_id];
2571            slot_last_use
2572                .entry(v.slot)
2573                .and_modify(|cur| {
2574                    if lu > *cur {
2575                        *cur = lu;
2576                    }
2577                })
2578                .or_insert(lu);
2579        }
2580        // If every slot in this producer has the same last_use, the
2581        // bundle's natural drop position is fine — no promotions.
2582        let min_lu = match slot_last_use.values().copied().min() {
2583            Some(v) => v,
2584            None => continue,
2585        };
2586        for v in views {
2587            if v.is_owned {
2588                // Already owned — nothing to do (idempotent).
2589                continue;
2590            }
2591            let lu = slot_last_use[&v.slot];
2592            if lu <= min_lu {
2593                // This slot is among the shortest-lived → stays View.
2594                continue;
2595            }
2596            let v_node = graph.node(v.view_id);
2597            promotions.push(Promotion {
2598                view_id: v.view_id,
2599                slot: v.slot,
2600                view_shape: v_node.shape.clone(),
2601                view_dtype: v_node.dtype,
2602                view_consumers: consumers.get(&v.view_id).cloned().unwrap_or_default(),
2603            });
2604        }
2605    }
2606
2607    // Apply mutations: each promoted Op::View becomes a fresh
2608    // Op::ViewOwned that consumers point at instead. The old Op::View
2609    // node stays in the arena (no in-place op mutation) but is
2610    // unreachable from the roots — execution_plan + topo skip it
2611    // naturally. This mirrors the insert_safety_copies pattern of
2612    // "add new nodes + rewrite_input" rather than mutating ops in
2613    // place.
2614    let count = promotions.len();
2615    for p in promotions {
2616        let producer = graph.node(p.view_id).inputs[0];
2617        let owned_id = graph.push(crate::Node {
2618            op: Op::ViewOwned { slot: p.slot },
2619            inputs: vec![producer],
2620            shape: p.view_shape,
2621            dtype: p.view_dtype,
2622        });
2623        // Inherit target_backend from the original View when set, so
2624        // the executor can look up ViewOwned's wrapper without a
2625        // re-resolution pass.
2626        if let Some(backend) = graph.target_backend(p.view_id) {
2627            graph.set_target_backend(owned_id, backend);
2628        }
2629        for consumer in p.view_consumers {
2630            graph.rewrite_input(consumer, p.view_id, owned_id);
2631        }
2632    }
2633    count
2634}
2635
2636/// Build an execution plan that respects both data-flow edges (via
2637/// [`topo_order_multi`]) and ordering edges (via [`derive_ordering`]).
2638/// Returns a `Vec<NodeId>` in an order the executor can walk linearly,
2639/// evaluating each node's dependencies before it.
2640///
2641/// **Fast path:** when the graph has no destructive ops, this returns
2642/// the same order `topo_order_multi` does — no extra cost beyond the
2643/// analysis pass.
2644///
2645/// **Cycle detection:** if the combined (data + ordering) graph has a
2646/// cycle — the residency rule emitted a destructive op that
2647/// transitively depends on itself — this panics with a clear error
2648/// message. That panic indicates a bug in the rule that emitted the
2649/// destructive op, not user code.
2650///
2651/// **Stability:** among nodes that are concurrently ready, this
2652/// picks the one whose `topo_order_multi` index is smallest. Result:
2653/// when there are no ordering edges, the output matches
2654/// `topo_order_multi` exactly.
2655pub fn execution_plan(graph: &crate::Graph, roots: &[NodeId]) -> Vec<NodeId> {
2656    let base_order = topo_order_multi(graph, roots);
2657    let ordering = derive_ordering(graph, roots);
2658    if ordering.is_empty() {
2659        return base_order;
2660    }
2661
2662    // Position of each node in base_order — used as the stable tiebreaker.
2663    let pos: HashMap<NodeId, usize> = base_order
2664        .iter()
2665        .enumerate()
2666        .map(|(i, &n)| (n, i))
2667        .collect();
2668    let node_set: std::collections::HashSet<NodeId> = base_order.iter().copied().collect();
2669
2670    // Build in-degree + reverse adjacency for Kahn's.
2671    let mut in_degree: HashMap<NodeId, usize> = HashMap::with_capacity(base_order.len());
2672    let mut forward: HashMap<NodeId, Vec<NodeId>> = HashMap::new();
2673    for &nid in &base_order {
2674        let node = graph.node(nid);
2675        let mut d = 0usize;
2676        for &input in &node.inputs {
2677            if node_set.contains(&input) {
2678                d += 1;
2679                forward.entry(input).or_default().push(nid);
2680            }
2681        }
2682        for &dep in ordering.deps_of(nid) {
2683            if node_set.contains(&dep) {
2684                d += 1;
2685                forward.entry(dep).or_default().push(nid);
2686            }
2687        }
2688        in_degree.insert(nid, d);
2689    }
2690
2691    // Stable ready-set keyed by base_order position. BTreeSet pops the
2692    // smallest → output matches topo order when ordering edges allow it.
2693    let mut ready: std::collections::BTreeSet<(usize, NodeId)> = base_order
2694        .iter()
2695        .copied()
2696        .filter(|n| in_degree[n] == 0)
2697        .map(|n| (pos[&n], n))
2698        .collect();
2699
2700    let mut plan = Vec::with_capacity(base_order.len());
2701    while let Some(&(_, n)) = ready.iter().next() {
2702        ready.remove(&(pos[&n], n));
2703        plan.push(n);
2704        if let Some(succs) = forward.get(&n) {
2705            for &s in succs {
2706                let d = in_degree.get_mut(&s).expect("in_degree covers all nodes");
2707                *d -= 1;
2708                if *d == 0 {
2709                    ready.insert((pos[&s], s));
2710                }
2711            }
2712        }
2713    }
2714
2715    if plan.len() != base_order.len() {
2716        panic!(
2717            "execution_plan: cycle in ordering edges (plan={}, base={}) — \
2718             a destructive op transitively depends on itself. This is a \
2719             bug in whatever rule emitted the destructive op.",
2720            plan.len(),
2721            base_order.len(),
2722        );
2723    }
2724    plan
2725}
2726
2727// ---- Evict + reload graph surgery -----------------------------------------
2728
2729/// Insert an evict-chain around `candidate` to free its device storage
2730/// during a gap between uses, with automatic reload before the post-gap
2731/// consumers. Returns `(move_id, reload_id)`.
2732///
2733/// ## What it does
2734///
2735/// Inserts two new nodes:
2736/// 1. `mv = Op::Move { target: Cpu }` reading `candidate` — stages the
2737///    data to host memory AND destructively releases `candidate`'s
2738///    device-resident storage once it runs ([`Op::Move`] is the fused
2739///    Copy + Release: `destructive_input() == Some(0)`). The
2740///    [`derive_ordering`] pass pins it to run AFTER every
2741///    non-destructive reader of `candidate` (the pre-gap consumers the
2742///    caller left untouched).
2743/// 2. `reload = Op::Copy { target: src_device }` reading `mv` —
2744///    restages the data to the device for the post-gap consumers.
2745///
2746/// Then rewrites every `post_gap_consumer`'s input edge from `candidate`
2747/// to `reload`. Pre-gap consumers keep reading `candidate` directly.
2748///
2749/// Historical note: the original (legacy-executor era) chain was three
2750/// nodes — `Op::Copy{Cpu}` + side-effect-root `Op::Release` +
2751/// `Op::Copy{device}`. `Op::Move` collapsed the first two; the fused
2752/// node's output is consumed by `reload`, so no side-effect root is
2753/// needed.
2754///
2755/// ## Caller's responsibility
2756///
2757/// - `candidate` is a NodeId currently in the graph whose device
2758///   residency is `src_device`.
2759/// - `post_gap_consumers` are NodeIds currently in the graph that each
2760///   have `candidate` in their `inputs`. Typically these come from the
2761///   residency analyzer's gap-positioning logic.
2762/// - The caller stamps placement/backend metadata on the new nodes
2763///   (`mv` runs on `src_device`'s backend, `reload` on Cpu — the
2764///   staged copy's residency) before realizing on the pipelined
2765///   executor. `fuel-dispatch::residency::insert_residency_evictions`
2766///   does this.
2767pub fn insert_evict_reload(
2768    graph: &SharedGraph,
2769    candidate: NodeId,
2770    src_device: DeviceLocation,
2771    post_gap_consumers: &[NodeId],
2772) -> (NodeId, NodeId) {
2773    let mut g = graph.write().unwrap();
2774    insert_evict_reload_in(&mut g, candidate, src_device, post_gap_consumers)
2775}
2776
2777/// [`insert_evict_reload`] on a graph the caller already holds mutably (the optimizer's
2778/// `optimize_graph` takes `&mut Graph`, not a `SharedGraph`). Same chain, same rewrite.
2779pub fn insert_evict_reload_in(
2780    g: &mut Graph,
2781    candidate: NodeId,
2782    src_device: DeviceLocation,
2783    post_gap_consumers: &[NodeId],
2784) -> (NodeId, NodeId) {
2785    let (shape, dtype) = {
2786        let n = g.node(candidate);
2787        (n.shape.clone(), n.dtype)
2788    };
2789
2790    let move_id = g.push(Node {
2791        op: Op::Move {
2792            target: DeviceLocation::Cpu,
2793        },
2794        inputs: vec![candidate],
2795        shape: shape.clone(),
2796        dtype,
2797    });
2798
2799    let reload_id = g.push(Node {
2800        op: Op::Copy { target: src_device },
2801        inputs: vec![move_id],
2802        shape,
2803        dtype,
2804    });
2805
2806    // Rewrite each post-gap consumer's `candidate` input to `reload_id`.
2807    // Pre-gap consumers (not in this list) continue reading `candidate`
2808    // directly, which is why derive_ordering needs `move_id` to run
2809    // after them.
2810    for &consumer in post_gap_consumers {
2811        g.rewrite_input(consumer, candidate, reload_id);
2812    }
2813
2814    (move_id, reload_id)
2815}
2816
2817// ===========================================================================
2818// Phase 2.1 — cross-device `Op::Copy` insertion
2819// ===========================================================================
2820//
2821// When the picker (or a user pin) commits a kernel-bearing node to a
2822// `DeviceLocation` that doesn't share a storage substrate with one of
2823// its inputs' resident `DeviceLocation`, the data needs to actually
2824// move. The executor handles the FINAL output via `pipelined_bridge`'s
2825// realize-root splicing; cross-device edges INSIDE the graph are
2826// handled here.
2827//
2828// `insert_cross_device_copies` is the pre-execute pass that handles
2829// internal cross-device edges. Architecture v1.0 §04 names the
2830// optimizer as the owner of transfer-op insertion — this is that
2831// owner. Wired into `fuel-tensor::pipelined_bridge::prepare()` (picker
2832// arc step 2): the bridge derives `placement_for` from the monolithic
2833// `target_backend` pinning + the StorageCache's resident locations,
2834// and `shares_storage` from `SystemTopology`.
2835//
2836// The pass deliberately doesn't decide placements itself — callers
2837// (the optimizer ranker / picker) commit to placements first, then
2838// hand the pass `(graph, placement_for_node, shares_storage)` and
2839// it materializes the implied transfers. Same pattern as the
2840// JudgeOracle / CapabilitiesLookup callbacks in
2841// `fuel-dispatch::ranker`: keep `fuel-graph` ignorant of how
2842// placements / topology are derived; the closure is the seam.
2843
2844/// Insert `Op::Copy { target }` nodes on every edge where the
2845/// consumer's intended `DeviceLocation` doesn't share a storage
2846/// substrate with the producer's. Returns a remap of original
2847/// `NodeId`s to their post-rewrite equivalents (most NodeIds map to
2848/// themselves; only consumers of cross-device edges acquire new
2849/// inputs in-place via `rewrite_input`).
2850///
2851/// # Arguments
2852///
2853/// - `graph` — the graph to rewrite, mutably. New `Op::Copy` nodes
2854///   are appended via `Graph::push`; consumer edges are rewired via
2855///   the internal `rewrite_input` helper.
2856/// - `roots` — the realize roots the caller cares about. The pass
2857///   walks `topo_order_multi(roots)` and considers every node it
2858///   visits.
2859/// - `placement_for` — `Fn(NodeId) -> Option<DeviceLocation>`.
2860///   Returns the picker's committed placement for the node, or
2861///   `None` if the node has no placement (view ops, structural ops,
2862///   user-unpinned ops in a single-backend realize). When either
2863///   the producer or consumer has no placement, the pass leaves
2864///   the edge alone — the executor falls back to its existing
2865///   handling (auto_contiguize or the realize-root splice).
2866/// - `shares_storage` — `Fn(DeviceLocation, DeviceLocation) -> bool`.
2867///   The topology query. Typical implementation is
2868///   `SystemTopology::shares_storage((b1, dev1), (b2, dev2))` after
2869///   the caller resolves backends to devices; we pass just the
2870///   `DeviceLocation` pair here because the substrate question is
2871///   device-level (`Cpu` shares with `Cpu`, `Cuda{0}` shares with
2872///   `Cuda{0}` but not `Cuda{1}`, etc.). Same-device queries should
2873///   return `true`.
2874/// - `needs_host_staging` — `Fn(DeviceLocation, DeviceLocation) -> bool`.
2875///   For a non-shared edge that this pass IS bridging, returns `true`
2876///   when the transfer cannot be done as a SINGLE `Op::Copy` hop and
2877///   must instead route through host memory as TWO hops
2878///   (producer-GPU → CPU, then CPU → consumer-GPU). This is the
2879///   cross-VENDOR GPU↔GPU case (CUDA↔Vulkan): there is no direct
2880///   device-to-device copy kernel between distinct GPU substrates, so
2881///   the source-backend Copy kernel only accepts a CPU or same-substrate
2882///   output. The single-hop cases — CPU↔CUDA, CPU↔Vulkan, and any edge
2883///   touching CPU on either end — return `false` and get exactly ONE
2884///   `Op::Copy` (byte-identical to the pre-two-hop behavior). Typical
2885///   wiring derives this from `SystemTopology::transfer_path(src, dst)
2886///   == TransferPath::HostStaging` for GPU↔GPU pairs. When `true`, the
2887///   pass inserts a CPU intermediate `Op::Copy { target: Cpu }` (placed
2888///   on CPU) and a second `Op::Copy { target: consumer }` reading it;
2889///   the consumer is rewired to the second hop.
2890///
2891/// # Returns
2892///
2893/// The `NodeId`s of the inserted `Op::Copy` nodes, in insertion
2894/// order. Callers typically need these to stamp executor metadata
2895/// on the new nodes — e.g. `fuel-tensor::pipelined_bridge` sets
2896/// `target_backend` = the SOURCE backend (the pipelined executor's
2897/// Op::Copy kernel-lookup convention: the transfer kernel runs on
2898/// the backend the bytes come FROM). `.len()` is the transfer-count
2899/// metric the optimizer reports when deciding whether to pursue
2900/// alternative placements that minimize transfers. A two-hop
2901/// (host-staged) edge contributes BOTH inserted copies (the CPU
2902/// intermediate and the consumer-side hop) to this vec; the SOURCE-
2903/// backend re-stamp the caller applies to `inputs.first()` is
2904/// correct for each hop independently (the CPU hop's source is the
2905/// producer GPU; the consumer hop's source is the CPU intermediate).
2906///
2907/// # Idempotence
2908///
2909/// The pass is idempotent — re-running on an already-rewritten
2910/// graph adds zero new copies (the inserted `Op::Copy` nodes
2911/// carry `placement` matching the consumer's, so subsequent
2912/// passes see same-device edges from `Op::Copy` to consumer).
2913///
2914/// # CSE on inserted copies
2915///
2916/// When two consumers share an input AND both need to bring it to
2917/// the same target device, the pass deduplicates: one `Op::Copy`
2918/// node serves both consumers. This matches the existing CSE
2919/// pattern in `optimize_to_fixpoint`.
2920pub fn insert_cross_device_copies<P, S, H>(
2921    graph: &mut Graph,
2922    roots: &[NodeId],
2923    placement_for: P,
2924    shares_storage: S,
2925    needs_host_staging: H,
2926) -> Vec<NodeId>
2927where
2928    P: Fn(NodeId) -> Option<DeviceLocation>,
2929    S: Fn(DeviceLocation, DeviceLocation) -> bool,
2930    H: Fn(DeviceLocation, DeviceLocation) -> bool,
2931{
2932    let order = topo_order_multi(graph, roots);
2933    // CSE on inserted copies: `(producer, target_device) → Op::Copy NodeId`.
2934    // For a two-hop edge the CPU intermediate is itself CSE-keyed at
2935    // `(producer, Cpu)` and the consumer hop at `(cpu_intermediate, target)`,
2936    // so a producer feeding several cross-vendor consumers stages to CPU
2937    // exactly once and each distinct target gets one consumer-side hop.
2938    let mut copy_cache: HashMap<(NodeId, DeviceLocation), NodeId> = HashMap::new();
2939    let mut inserted: Vec<NodeId> = Vec::new();
2940
2941    // Two passes: first compute the rewires we want, then apply
2942    // them. Splitting avoids borrow issues with graph mutation
2943    // mid-iteration and keeps the iteration order stable.
2944    let mut rewires: Vec<(NodeId, NodeId, NodeId)> = Vec::new();
2945
2946    for &consumer_id in &order {
2947        let consumer_placement = match placement_for(consumer_id) {
2948            Some(p) => p,
2949            None => continue,
2950        };
2951        // Skip ops that are themselves transfers — `Op::Copy` /
2952        // `Op::Move` exist precisely TO bridge devices; inserting
2953        // another copy on their input would be infinite-regress.
2954        let consumer_op = &graph.node(consumer_id).op;
2955        if matches!(consumer_op, Op::Copy { .. } | Op::Move { .. }) {
2956            continue;
2957        }
2958
2959        // Snapshot inputs so we don't reborrow during the loop.
2960        let inputs: Vec<NodeId> = graph.node(consumer_id).inputs.clone();
2961        for producer_id in inputs {
2962            let producer_placement = match placement_for(producer_id) {
2963                Some(p) => p,
2964                None => continue,
2965            };
2966            if shares_storage(producer_placement, consumer_placement) {
2967                continue;
2968            }
2969
2970            // Decide hop count. A cross-VENDOR GPU↔GPU edge (e.g.
2971            // CUDA↔Vulkan) can't be done as one `Op::Copy` — there's no
2972            // direct device-to-device copy kernel between distinct GPU
2973            // substrates, so the source-backend Copy kernel rejects the
2974            // foreign-GPU output. Route it through host memory as TWO hops:
2975            // producer-GPU → CPU (intermediate), then CPU → consumer-GPU.
2976            // Single-hop edges (anything touching CPU on either end, or a
2977            // same-vendor pair the topology can copy directly) stay exactly
2978            // one `Op::Copy` — byte-identical to the pre-two-hop behavior.
2979            let two_hop = needs_host_staging(producer_placement, consumer_placement);
2980
2981            // `source_for_consumer_hop` is the node the consumer-side
2982            // `Op::Copy { target: consumer_placement }` reads FROM: the
2983            // producer directly for a single hop, or the CPU intermediate
2984            // for a two-hop edge.
2985            let source_for_consumer_hop = if two_hop {
2986                // Hop 1: producer-GPU → CPU intermediate. CSE-keyed at
2987                // `(producer, Cpu)` so a producer feeding several cross-
2988                // vendor consumers stages to CPU exactly once.
2989                match copy_cache.get(&(producer_id, DeviceLocation::Cpu)) {
2990                    Some(&id) => id,
2991                    None => {
2992                        let producer = graph.node(producer_id);
2993                        let shape = producer.shape.clone();
2994                        let dtype = producer.dtype;
2995                        let cpu_id = graph.push(Node {
2996                            op: Op::Copy {
2997                                target: DeviceLocation::Cpu,
2998                            },
2999                            inputs: vec![producer_id],
3000                            shape,
3001                            dtype,
3002                        });
3003                        // The CPU intermediate bridges the two hops; place
3004                        // it on CPU.
3005                        graph.set_placement(cpu_id, DeviceLocation::Cpu);
3006                        copy_cache.insert((producer_id, DeviceLocation::Cpu), cpu_id);
3007                        inserted.push(cpu_id);
3008                        cpu_id
3009                    }
3010                }
3011            } else {
3012                producer_id
3013            };
3014
3015            // Hop 2 (or the single hop): `source_for_consumer_hop` →
3016            // consumer device. CSE-keyed at `(source, consumer_placement)`.
3017            let copy_id = match copy_cache.get(&(source_for_consumer_hop, consumer_placement)) {
3018                Some(&id) => id,
3019                None => {
3020                    let src = graph.node(source_for_consumer_hop);
3021                    let shape = src.shape.clone();
3022                    let dtype = src.dtype;
3023                    let id = graph.push(Node {
3024                        op: Op::Copy {
3025                            target: consumer_placement,
3026                        },
3027                        inputs: vec![source_for_consumer_hop],
3028                        shape,
3029                        dtype,
3030                    });
3031                    // The inserted copy itself sits on the
3032                    // consumer's device — its output is what the
3033                    // consumer reads.
3034                    graph.set_placement(id, consumer_placement);
3035                    copy_cache.insert((source_for_consumer_hop, consumer_placement), id);
3036                    inserted.push(id);
3037                    id
3038                }
3039            };
3040            rewires.push((consumer_id, producer_id, copy_id));
3041        }
3042    }
3043
3044    for (consumer, old_input, new_input) in rewires {
3045        graph.rewrite_input(consumer, old_input, new_input);
3046    }
3047
3048    inserted
3049}
3050
3051/// Phase 2.2 of the picker arc — insert `Op::Contiguize` before any
3052/// kernel-bearing node whose chosen kernel doesn't accept strided
3053/// inputs but whose live input layout is non-contiguous (strided,
3054/// offset, or both).
3055///
3056/// # When to call
3057///
3058/// After Phase 2.1's `insert_cross_device_copies` and after the
3059/// picker has committed to per-node kernel choices. The picker's
3060/// commitment determines the `KernelCaps::strided_input` flag for
3061/// each node; this pass reads that flag via the `kernel_accepts_strided`
3062/// callback and inserts layout-fixups where the flag is false and
3063/// the input is strided.
3064///
3065/// # Callback contract
3066///
3067/// `kernel_accepts_strided(consumer_id) -> bool` returns `true` if
3068/// the kernel resolved for that node tolerates strided / offset
3069/// inputs (i.e. its `KernelCaps::strided_input == true`). The
3070/// callback is opaque to this pass — typical wiring reads the
3071/// picker's commit-to-winner alternative set and queries the
3072/// winner's caps. When `false`, this pass inserts `Op::Contiguize`
3073/// on every non-contiguous input.
3074///
3075/// # Skips
3076///
3077/// - View ops (`Transpose`, `Permute`, `BroadcastTo`, `Slice`,
3078///   `Unsqueeze`, `Squeeze`, `Flip`) — they preserve strided
3079///   layouts intentionally.
3080/// - Structural ops (`Const`, `Release`, `Reshape`, `Contiguize`,
3081///   `Copy`, `Move`, `Alloc`, `ZeroFill`, `WriteSlice`) — their
3082///   executor arms handle strided naturally or are not kernel-
3083///   bearing.
3084///
3085/// # CSE
3086///
3087/// When two consumers share a non-contiguous input AND both kernels
3088/// reject strided, one `Op::Contiguize` node serves both. The
3089/// per-pass cache is keyed on producer NodeId (no target-device
3090/// dimension — Contiguize is device-inheriting).
3091///
3092/// # Idempotence
3093///
3094/// Running this pass on an already-rewritten graph adds zero new
3095/// nodes: an inserted `Op::Contiguize` has a contiguous + zero-offset
3096/// output layout, so the subsequent fixup check (`is_contiguous &&
3097/// start_offset == 0`) succeeds and the input is skipped.
3098///
3099/// Returns the number of `Op::Contiguize` nodes inserted.
3100pub fn insert_layout_fixups<F>(
3101    graph: &mut Graph,
3102    roots: &[NodeId],
3103    kernel_accepts_strided: F,
3104) -> usize
3105where
3106    F: Fn(NodeId) -> bool,
3107{
3108    let order = topo_order_multi(graph, roots);
3109    // CSE: producer NodeId → Contiguize NodeId. No device dim since
3110    // Contiguize inherits the producer's residency.
3111    let mut fixup_cache: HashMap<NodeId, NodeId> = HashMap::new();
3112    let mut inserted = 0usize;
3113    let mut rewires: Vec<(NodeId, NodeId, NodeId)> = Vec::new();
3114
3115    for &consumer_id in &order {
3116        // Snapshot the consumer to release the borrow before
3117        // mutation.
3118        let (consumer_op, consumer_inputs) = {
3119            let node = graph.node(consumer_id);
3120            (node.op.clone(), node.inputs.clone())
3121        };
3122
3123        // Skip non-kernel-bearing nodes — their executor arms
3124        // either preserve strided layouts (view ops, Reshape) or
3125        // are structural with no kernel to mismatch on (Const,
3126        // Release, Copy, Move, Alloc, ZeroFill, WriteSlice,
3127        // WriteSliceDoff, Contiguize). WriteSlice/WriteSliceDoff
3128        // adopt `dest` in place + auto-contiguize their source in the
3129        // execute arm, so a pass-inserted contiguize on `dest` would
3130        // break the in-place adoption — skip them here.
3131        if consumer_op.is_view_op()
3132            || matches!(
3133                consumer_op,
3134                Op::Const
3135                    | Op::Release
3136                    | Op::Reshape(_)
3137                    | Op::Contiguize
3138                    | Op::Copy { .. }
3139                    | Op::Move { .. }
3140                    | Op::Alloc { .. }
3141                    | Op::ZeroFill
3142                    | Op::WriteSlice { .. }
3143                    | Op::WriteSliceDoff { .. }
3144            )
3145        {
3146            continue;
3147        }
3148
3149        // The kernel accepts strided inputs directly — no fixup.
3150        if kernel_accepts_strided(consumer_id) {
3151            continue;
3152        }
3153
3154        for input_id in consumer_inputs {
3155            let input_layout = graph.layout(input_id);
3156            if input_layout.is_contiguous() && input_layout.start_offset() == 0 {
3157                continue;
3158            }
3159
3160            let fixup_id = match fixup_cache.get(&input_id) {
3161                Some(&id) => id,
3162                None => {
3163                    let (shape, dtype) = {
3164                        let p = graph.node(input_id);
3165                        (p.shape.clone(), p.dtype)
3166                    };
3167                    let id = graph.push(Node {
3168                        op: Op::Contiguize,
3169                        inputs: vec![input_id],
3170                        shape,
3171                        dtype,
3172                    });
3173                    fixup_cache.insert(input_id, id);
3174                    inserted += 1;
3175                    id
3176                }
3177            };
3178            rewires.push((consumer_id, input_id, fixup_id));
3179        }
3180    }
3181
3182    for (consumer, old_input, new_input) in rewires {
3183        graph.rewrite_input(consumer, old_input, new_input);
3184    }
3185
3186    inserted
3187}
3188
3189/// Insert `Op::Cast` fixups that CHANGE a consumer's input dtype — the sibling of
3190/// [`insert_layout_fixups`], for dtype rather than layout. Given precomputed
3191/// `(consumer, input, target_dtype)` edges (an input whose dtype should be
3192/// promoted so the consumer's `(op, per-operand-dtypes)` key becomes servable),
3193/// insert one `Op::Cast(target)` per distinct `(input, target)` — CSE-shared
3194/// across consumers — and rewire each consumer's edge to it.
3195///
3196/// Unlike `Graph::rewrite_input` (whose contract is a *same-dtype* redirect),
3197/// this DELIBERATELY changes the consumer's input dtype — that is the point: it
3198/// is the graph-side half of the optimizer's dtype-reconciliation pass
3199/// (`fuel_dispatch::optimize::insert_dtype_fixups`), which decides WHICH edges to
3200/// promote from backend kernel coverage. Edges apply in the given order
3201/// (deterministic when the caller passes topo order). Returns the rewires applied.
3202pub fn insert_cast_fixups(graph: &mut Graph, casts: &[(NodeId, NodeId, DType)]) -> usize {
3203    let mut cache: HashMap<(NodeId, DType), NodeId> = HashMap::new();
3204    let mut applied = 0;
3205    for &(consumer, input, target) in casts {
3206        if graph.node(input).dtype == target {
3207            continue; // already the target dtype — nothing to cast
3208        }
3209        let cast_id = *cache.entry((input, target)).or_insert_with(|| {
3210            let shape = graph.node(input).shape.clone();
3211            graph.push(Node {
3212                op: Op::Cast(target),
3213                inputs: vec![input],
3214                shape,
3215                dtype: target,
3216            })
3217        });
3218        graph.rewrite_input(consumer, input, cast_id);
3219        applied += 1;
3220    }
3221    applied
3222}
3223
3224#[cfg(test)]
3225mod tests {
3226    use super::*;
3227    use crate::NodeHandle;
3228    // `FusedOps` is referenced only in these tests; the lib-level `use` above
3229    // correctly dropped it as unused, so import it here where it's used.
3230    use crate::registry::FusedOps;
3231    use fuel_ir::{DeviceLocation, Shape};
3232    use std::sync::Arc;
3233
3234    /// Phase 7.5 G2: tests need a real device for slot-populating
3235    /// constructors. Singleton CpuBackendDevice via OnceLock.
3236    fn cpu_dev() -> &'static Arc<dyn fuel_backend_contract::DynBackendDevice> {
3237        static D: std::sync::OnceLock<Arc<dyn fuel_backend_contract::DynBackendDevice>> =
3238            std::sync::OnceLock::new();
3239        D.get_or_init(|| Arc::new(fuel_cpu_backend::dyn_impl::CpuBackendDevice))
3240    }
3241
3242    fn make_scalar_graph() -> (SharedGraph, NodeHandle) {
3243        let t = NodeHandle::from_f32(vec![1.0, 2.0, 3.0, 4.0], Shape::from_dims(&[4]), cpu_dev())
3244            .unwrap();
3245        (t.graph().clone(), t)
3246    }
3247
3248    fn count_copy_nodes(graph: &SharedGraph) -> usize {
3249        let g = graph.read().unwrap();
3250        (0..g.len())
3251            .filter(|i| matches!(g.node(NodeId(*i)).op, Op::Copy { .. }))
3252            .count()
3253    }
3254
3255    #[test]
3256    fn insert_copies_no_placement_no_copies() {
3257        // Graph with no placement hints: pass should be a no-op, no
3258        // Copies inserted, roots unchanged.
3259        let a = NodeHandle::from_f32(vec![1.0, 2.0], Shape::from_dims(&[2]), cpu_dev()).unwrap();
3260        let b = a
3261            .const_f32_like(vec![3.0, 4.0], Shape::from_dims(&[2]))
3262            .unwrap();
3263        let c = a.add(&b);
3264        let graph = c.graph().clone();
3265        let before = count_copy_nodes(&graph);
3266        let new_roots = insert_copies(&graph, &[c.id()]);
3267        assert_eq!(new_roots, vec![c.id()]);
3268        assert_eq!(count_copy_nodes(&graph), before);
3269    }
3270
3271    #[test]
3272    fn insert_copies_tagged_node_pulls_inputs_to_its_device() {
3273        // Const a, Const b, Add(a, b) placed on Vulkan.
3274        // Expected: two Copy(a, Vulkan) and Copy(b, Vulkan) inserted,
3275        // Add's inputs rewritten to reference the Copies.
3276        let a = NodeHandle::from_f32(vec![1.0, 2.0], Shape::from_dims(&[2]), cpu_dev()).unwrap();
3277        let b = a
3278            .const_f32_like(vec![3.0, 4.0], Shape::from_dims(&[2]))
3279            .unwrap();
3280        let c = a.add(&b).on_device(DeviceLocation::Vulkan { gpu_id: 0 });
3281        let graph = c.graph().clone();
3282
3283        let before_copies = count_copy_nodes(&graph);
3284        let new_roots = insert_copies(&graph, &[c.id()]);
3285
3286        // Should be exactly 2 new Copies (one per input).
3287        assert_eq!(count_copy_nodes(&graph) - before_copies, 2);
3288
3289        // Rewritten Add should reference two Copy nodes.
3290        let g = graph.read().unwrap();
3291        let new_add = g.node(new_roots[0]);
3292        assert_eq!(new_add.inputs.len(), 2);
3293        for input in &new_add.inputs {
3294            let node = g.node(*input);
3295            assert!(matches!(
3296                node.op,
3297                Op::Copy {
3298                    target: DeviceLocation::Vulkan { gpu_id: 0 }
3299                }
3300            ));
3301        }
3302    }
3303
3304    #[test]
3305    fn insert_copies_matching_device_no_copies_inserted() {
3306        // Const a and Add both placed on Vulkan. Const flows into
3307        // Add, both want Vulkan — no Copy needed.
3308        let a = NodeHandle::from_f32(vec![1.0, 2.0], Shape::from_dims(&[2]), cpu_dev())
3309            .unwrap()
3310            .on_device(DeviceLocation::Vulkan { gpu_id: 0 });
3311        let b = a
3312            .const_f32_like(vec![3.0, 4.0], Shape::from_dims(&[2]))
3313            .unwrap()
3314            .on_device(DeviceLocation::Vulkan { gpu_id: 0 });
3315        let c = a.add(&b).on_device(DeviceLocation::Vulkan { gpu_id: 0 });
3316        let graph = c.graph().clone();
3317
3318        let before = count_copy_nodes(&graph);
3319        insert_copies(&graph, &[c.id()]);
3320        assert_eq!(count_copy_nodes(&graph), before);
3321    }
3322
3323    #[test]
3324    fn insert_copies_handles_backward_graph() {
3325        // Forward: y = sum(x * x) with x placed on Vulkan.
3326        // Call backward — this appends gradient nodes to the same graph.
3327        // Then run insert_copies on BOTH the forward root and the
3328        // gradient root. Every backward op should end up on Vulkan
3329        // (inherited from its inputs, which trace back to Vulkan-placed x).
3330        let x = NodeHandle::from_f32(vec![1.0, 2.0, 3.0, 4.0], Shape::from_dims(&[4]), cpu_dev())
3331            .unwrap()
3332            .on_device(DeviceLocation::Vulkan { gpu_id: 0 });
3333        let sq = x.mul(&x).on_device(DeviceLocation::Vulkan { gpu_id: 0 });
3334        let y = sq.sum_all().on_device(DeviceLocation::Vulkan { gpu_id: 0 });
3335        let graph = y.graph().clone();
3336
3337        let grads = y.backward();
3338        let grad_x = grads.get(&x).expect("dL/dx should exist");
3339
3340        // Run insert_copies on both roots.
3341        let before_copies = count_copy_nodes(&graph);
3342        let _new = insert_copies(&graph, &[y.id(), grad_x.id()]);
3343        let after_copies = count_copy_nodes(&graph);
3344
3345        // With everything on Vulkan, no Copies should be needed —
3346        // this verifies the pass doesn't spuriously insert Copies into
3347        // the backward graph when placements are consistent.
3348        assert_eq!(
3349            after_copies, before_copies,
3350            "insert_copies should leave a uniformly-Vulkan forward+backward graph alone"
3351        );
3352    }
3353
3354    #[test]
3355    fn insert_copies_backward_inherits_forward_device() {
3356        // Forward: y = sum(x * x) with NO explicit placement.
3357        // Compute backward. Then place JUST the forward root on Vulkan
3358        // and re-run insert_copies. The Copies that get inserted should
3359        // pull x to Vulkan; the backward path should follow suit when
3360        // we ask for both roots.
3361        let x = NodeHandle::from_f32(vec![1.0, 2.0, 3.0, 4.0], Shape::from_dims(&[4]), cpu_dev())
3362            .unwrap();
3363        let sq = x.mul(&x);
3364        let y = sq.sum_all().on_device(DeviceLocation::Vulkan { gpu_id: 0 });
3365        let graph = y.graph().clone();
3366
3367        let grads = y.backward();
3368        let grad_x = grads.get(&x).expect("dL/dx should exist");
3369
3370        // Before: only y is tagged. The backward graph has no tags.
3371        // After insert_copies([y, grad_x]): y pulls its inputs (sq)
3372        // toward Vulkan → sq pulls x → Copies inserted.
3373        // Backward inherits device from its inputs transitively.
3374        let new_roots = insert_copies(&graph, &[y.id(), grad_x.id()]);
3375        assert_eq!(new_roots.len(), 2);
3376        // At least one Copy should have been inserted (the unplaced
3377        // forward consts need to get to Vulkan).
3378        assert!(
3379            count_copy_nodes(&graph) > 0,
3380            "expected Copies to be inserted for unplaced Const inputs"
3381        );
3382    }
3383
3384    #[test]
3385    fn lower_const_placement_single_vulkan_consumer() {
3386        // Const a → Add(a, b) placed on Vulkan. lower_const_placement
3387        // should tag a with Vulkan since Add is its only consumer.
3388        let a = NodeHandle::from_f32(vec![1.0, 2.0], Shape::from_dims(&[2]), cpu_dev()).unwrap();
3389        let b = a
3390            .const_f32_like(vec![3.0, 4.0], Shape::from_dims(&[2]))
3391            .unwrap();
3392        let c = a.add(&b).on_device(DeviceLocation::Vulkan { gpu_id: 0 });
3393        let graph = c.graph().clone();
3394
3395        let lowered = lower_const_placement(&graph, &[c.id()]);
3396        assert_eq!(lowered, 2); // both a and b tagged
3397        assert_eq!(
3398            graph.read().unwrap().placement(a.id()),
3399            Some(DeviceLocation::Vulkan { gpu_id: 0 })
3400        );
3401        assert_eq!(
3402            graph.read().unwrap().placement(b.id()),
3403            Some(DeviceLocation::Vulkan { gpu_id: 0 })
3404        );
3405
3406        // After lowering, insert_copies should emit NO Copies (the
3407        // Consts are now on the target device).
3408        let before_copies = count_copy_nodes(&graph);
3409        insert_copies(&graph, &[c.id()]);
3410        assert_eq!(count_copy_nodes(&graph), before_copies);
3411    }
3412
3413    #[test]
3414    fn lower_const_placement_consumers_disagree_stays_unplaced() {
3415        // Const a flows into two consumers on different devices.
3416        // Without replication support, lowering has to leave a unplaced.
3417        let a = NodeHandle::from_f32(vec![1.0, 2.0], Shape::from_dims(&[2]), cpu_dev()).unwrap();
3418        let b = a
3419            .const_f32_like(vec![3.0, 4.0], Shape::from_dims(&[2]))
3420            .unwrap();
3421        let cpu_sum = a.add(&b).on_device(DeviceLocation::Cpu);
3422        let vulkan_sum = a.add(&b).on_device(DeviceLocation::Vulkan { gpu_id: 0 });
3423        let graph = a.graph().clone();
3424
3425        lower_const_placement(&graph, &[cpu_sum.id(), vulkan_sum.id()]);
3426        assert_eq!(
3427            graph.read().unwrap().placement(a.id()),
3428            None,
3429            "const with disagreeing consumers stays unplaced"
3430        );
3431        assert_eq!(graph.read().unwrap().placement(b.id()), None);
3432    }
3433
3434    #[test]
3435    fn lower_const_placement_skips_already_placed() {
3436        // An explicitly-placed Const should not be overridden.
3437        let a = NodeHandle::from_f32(vec![1.0, 2.0], Shape::from_dims(&[2]), cpu_dev())
3438            .unwrap()
3439            .on_device(DeviceLocation::Cpu);
3440        let b = a
3441            .const_f32_like(vec![3.0, 4.0], Shape::from_dims(&[2]))
3442            .unwrap();
3443        let c = a.add(&b).on_device(DeviceLocation::Vulkan { gpu_id: 0 });
3444        let graph = c.graph().clone();
3445
3446        lower_const_placement(&graph, &[c.id()]);
3447        // a keeps its explicit Cpu placement even though its consumer is Vulkan.
3448        assert_eq!(
3449            graph.read().unwrap().placement(a.id()),
3450            Some(DeviceLocation::Cpu)
3451        );
3452        // b had no hint; it gets lowered to Vulkan.
3453        assert_eq!(
3454            graph.read().unwrap().placement(b.id()),
3455            Some(DeviceLocation::Vulkan { gpu_id: 0 })
3456        );
3457    }
3458
3459    #[test]
3460    fn insert_copies_idempotent() {
3461        let a = NodeHandle::from_f32(vec![1.0], Shape::from_dims(&[1]), cpu_dev()).unwrap();
3462        let b = a.const_f32_like(vec![2.0], Shape::from_dims(&[1])).unwrap();
3463        let c = a.add(&b).on_device(DeviceLocation::Cpu);
3464        let graph = c.graph().clone();
3465
3466        let roots1 = insert_copies(&graph, &[c.id()]);
3467        let after_first = count_copy_nodes(&graph);
3468        let _roots2 = insert_copies(&graph, &roots1);
3469        assert_eq!(
3470            count_copy_nodes(&graph),
3471            after_first,
3472            "insert_copies should be idempotent on already-reconciled graphs"
3473        );
3474    }
3475
3476    #[test]
3477    fn cse_folds_identical_add() {
3478        let (graph, a) = make_scalar_graph();
3479        let b = a.add(&a);
3480        let c = a.add(&a);
3481        let pre_len = graph.read().unwrap().len();
3482        let new_roots = optimize(&graph, &[b.id(), c.id()]);
3483        assert_eq!(
3484            new_roots[0], new_roots[1],
3485            "CSE should map both to same node"
3486        );
3487        assert!(graph.read().unwrap().len() >= pre_len);
3488    }
3489
3490    #[test]
3491    fn cse_folds_commutative() {
3492        // Build two tensors inside the same graph so `a + b` and
3493        // `b + a` share NodeIds for the inputs. Use add_scalar on `a`
3494        // as a simple way to get a second tensor handle sharing a's graph.
3495        let (_graph, a) = make_scalar_graph();
3496        let b = a.add_scalar(5.0);
3497        let ab = a.add(&b);
3498        let ba = b.add(&a);
3499        let graph = a.graph().clone();
3500        let new_roots = optimize(&graph, &[ab.id(), ba.id()]);
3501        assert_eq!(
3502            new_roots[0], new_roots[1],
3503            "commutative CSE should fold a+b and b+a to one node"
3504        );
3505    }
3506
3507    /// GAP-271: `Maximum`/`Minimum` must NOT be folded across operand order.
3508    ///
3509    /// They are commutative in VALUE and not in BITS. `chassis/binary.rs`
3510    /// returns the FIRST NaN operand -- a documented lhs-first tie-break
3511    /// matching `torch.maximum` -- so two distinct NaN payloads swap with
3512    /// operand order: `max(0x7FC00001, 0x7FC00002)` is `0x7FC00001` and the
3513    /// reverse is `0x7FC00002`. Folding the two orderings is therefore an
3514    /// invalid rewrite.
3515    ///
3516    /// A +-0 FIXTURE WOULD BE UNSOUND HERE, and not because we canonicalise:
3517    /// the +-0 answer is decided PER CALL SITE by the optimizer (we delegate
3518    /// to `f32::max`, which disclaims the tie). One binary at opt-level 3
3519    /// gives three answers -- const-folded +0/+0, `black_box` -0/-0,
3520    /// `#[inline(never)]` -0/+0. A +-0 assertion would therefore be testing
3521    /// the optimizer, not us. The NaN tie-break is explicit in our source and
3522    /// is stable in all four contexts, which is why it is the fixture.
3523    #[test]
3524    fn cse_does_not_fold_minmax_across_operand_order() {
3525        let (_graph, a) = make_scalar_graph();
3526        let b = a.add_scalar(5.0);
3527
3528        // CONTROL, and it is load-bearing: `Add` IS commutative in bits, so it
3529        // must STILL fold. Without this arm, a change that broke CSE entirely
3530        // would pass the assertion below for the wrong reason.
3531        let add_ab = a.add(&b);
3532        let add_ba = b.add(&a);
3533        let graph = a.graph().clone();
3534        let add_roots = optimize(&graph, &[add_ab.id(), add_ba.id()]);
3535        assert_eq!(
3536            add_roots[0], add_roots[1],
3537            "control: Add is bitwise commutative and must still fold -- if this              fails, CSE is broken generally and the Maximum arm below proves nothing"
3538        );
3539
3540        let max_ab = a.maximum(&b);
3541        let max_ba = b.maximum(&a);
3542        let max_roots = optimize(&graph, &[max_ab.id(), max_ba.id()]);
3543        assert_ne!(
3544            max_roots[0], max_roots[1],
3545            "max(a,b) and max(b,a) were folded to one node, but they differ in              BITS on distinct NaN payloads (lhs-first tie-break). See GAP-271."
3546        );
3547
3548        let min_ab = a.minimum(&b);
3549        let min_ba = b.minimum(&a);
3550        let min_roots = optimize(&graph, &[min_ab.id(), min_ba.id()]);
3551        assert_ne!(
3552            min_roots[0], min_roots[1],
3553            "min(a,b) and min(b,a) were folded to one node -- same defect as              Maximum, see GAP-271"
3554        );
3555    }
3556
3557    #[test]
3558    fn simplifies_add_scalar_zero() {
3559        let (graph, a) = make_scalar_graph();
3560        let b = a.add_scalar(0.0);
3561        let new_roots = optimize(&graph, &[b.id()]);
3562        assert_eq!(new_roots[0], a.id(), "AddScalar(0) should alias to input");
3563    }
3564
3565    #[test]
3566    fn simplifies_mul_scalar_one() {
3567        let (graph, a) = make_scalar_graph();
3568        let b = a.mul_scalar(1.0);
3569        let new_roots = optimize(&graph, &[b.id()]);
3570        assert_eq!(new_roots[0], a.id(), "MulScalar(1) should alias to input");
3571    }
3572
3573    #[test]
3574    fn simplifies_identity_cast() {
3575        let (graph, a) = make_scalar_graph();
3576        // Cast to the same dtype is a no-op; the optimizer aliases it away.
3577        // (Dispatch keys casts on [src, dst] and registers no [d, d] kernel,
3578        // so this elision is what removes a same-dtype cast before realize.)
3579        let same = a.cast(a.dtype());
3580        let new_roots = optimize(&graph, &[same.id()]);
3581        assert_eq!(
3582            new_roots[0],
3583            a.id(),
3584            "Cast(d) on a dtype-d input should alias to input",
3585        );
3586    }
3587
3588    /// GAP-183: `Cast(float -> Bool)` lowers to `Ne(x, x*0)` at the GRAPH level.
3589    ///
3590    /// WHY A LOWERING AND NOT A KERNEL: CUDA already ships the `!= 0` test —
3591    /// `ne_{f32,f64,f16,bf16}_u8` in `cuda/compare.fkc.md` — but those kernels are
3592    /// UNREACHABLE from a storage-level cast, which cannot compose graph ops. So
3593    /// the fix belongs here, not in the storage layer.
3594    ///
3595    /// BORN-RED: before the rewrite this root is an `Op::Cast(Bool)`.
3596    #[test]
3597    fn lowers_float_cast_to_bool_into_ne_against_zero() {
3598        let (graph, a) = make_scalar_graph(); // F32
3599        let as_bool = a.cast(fuel_ir::DType::Bool);
3600        let new_roots = optimize(&graph, &[as_bool.id()]);
3601        let g = graph.read().unwrap();
3602        let root = g.node(new_roots[0]);
3603        assert!(
3604            matches!(root.op, Op::Ne),
3605            "float -> Bool must lower to Op::Ne, got {:?}",
3606            root.op,
3607        );
3608        assert_eq!(
3609            root.dtype,
3610            fuel_ir::DType::Bool,
3611            "the lowered root still yields Bool"
3612        );
3613        // RHS is zeros from `MulScalar(0.0)` over the SAME input — not a
3614        // materialized const, so shape and dtype match by construction.
3615        let rhs = g.node(root.inputs[1]);
3616        assert!(
3617            matches!(rhs.op, Op::MulScalar(c) if c == 0.0),
3618            "rhs must be MulScalar(0.0) (shape-preserving zeros), got {:?}",
3619            rhs.op,
3620        );
3621        assert_eq!(
3622            rhs.inputs[0], root.inputs[0],
3623            "zeros must derive from the cast's own input",
3624        );
3625    }
3626
3627    /// GAP-183 companion: the INTEGER half is deliberately NOT lowered — there is
3628    /// no CUDA integer compare kernel, so rewriting would move the same decline
3629    /// one layer out while bypassing CPU's real int->Bool cast kernels.
3630    #[test]
3631    fn does_not_lower_integer_cast_to_bool() {
3632        let (graph, a) = make_scalar_graph();
3633        let as_u8 = a.cast(fuel_ir::DType::U8);
3634        let as_bool = as_u8.cast(fuel_ir::DType::Bool);
3635        let new_roots = optimize(&graph, &[as_bool.id()]);
3636        let g = graph.read().unwrap();
3637        let root = g.node(new_roots[0]);
3638        assert!(
3639            matches!(root.op, Op::Cast(fuel_ir::DType::Bool)),
3640            "int -> Bool stays an Op::Cast (genuine decline on CUDA), got {:?}",
3641            root.op,
3642        );
3643    }
3644
3645    #[test]
3646    fn keeps_real_cast() {
3647        let (graph, a) = make_scalar_graph();
3648        // A genuine dtype change (F32→F64) is NOT elided.
3649        let cast = a.cast(fuel_ir::DType::F64);
3650        let new_roots = optimize(&graph, &[cast.id()]);
3651        assert_ne!(
3652            new_roots[0],
3653            a.id(),
3654            "a real Cast must survive optimization"
3655        );
3656    }
3657
3658    #[test]
3659    fn declarative_pattern_matches_and_fuses_relu_add() {
3660        use crate::jit::{OpAttrs, OpTag, PatternNode};
3661        use crate::registry::{
3662            BackwardKind, FusedOpEntry, FusedOpFamily, FusedOpParams, FusedOps, PatternTree,
3663            SubgraphPattern,
3664        };
3665        fn shape_rule(s: &[Shape], _: &FusedOpParams) -> Shape {
3666            s[0].clone()
3667        }
3668        fn dtype_rule(d: &[DType], _: &FusedOpParams) -> DType {
3669            d[0]
3670        }
3671        fn no_decompose(_: &mut Graph, id: NodeId, _: &FusedOpParams) -> NodeId {
3672            id
3673        }
3674        // A declaratively-registered fused op for relu(add(a, b)).
3675        let entry = FusedOpEntry {
3676            destructive_input: None,
3677            id: FusedOps::SOFTMAX_LAST_DIM, // a real parameterless id (plumbing test)
3678            name: "test_declarative_relu_add",
3679            family: FusedOpFamily::Forward,
3680            pattern: SubgraphPattern::Declarative(PatternTree {
3681                root: PatternNode::Op {
3682                    op: OpTag::Relu,
3683                    attrs: OpAttrs::default(),
3684                    operands: vec![PatternNode::Op {
3685                        op: OpTag::Add,
3686                        attrs: OpAttrs::default(),
3687                        operands: vec![
3688                            PatternNode::Bind { index: 0 },
3689                            PatternNode::Bind { index: 1 },
3690                        ],
3691                    }],
3692                },
3693                params: FusedOpParams::SoftmaxLastDim,
3694            }),
3695            decompose: no_decompose,
3696            backward: BackwardKind::NotDifferentiable,
3697            shape_rule,
3698            dtype_rule,
3699            output_views: None,
3700        };
3701        let rule = FusionRule::from_entry(&entry);
3702
3703        // Build relu(add(a, b)).
3704        let mut g = Graph::new();
3705        let s = Shape::from_dims(&[2]);
3706        let f32 = DType::F32;
3707        let a = g.push(Node {
3708            op: Op::Const,
3709            inputs: vec![],
3710            shape: s.clone(),
3711            dtype: f32,
3712        });
3713        let b = g.push(Node {
3714            op: Op::Const,
3715            inputs: vec![],
3716            shape: s.clone(),
3717            dtype: f32,
3718        });
3719        let sum = g.push(Node {
3720            op: Op::Add,
3721            inputs: vec![a, b],
3722            shape: s.clone(),
3723            dtype: f32,
3724        });
3725        let r = g.push(Node {
3726            op: Op::Relu,
3727            inputs: vec![sum],
3728            shape: s.clone(),
3729            dtype: f32,
3730        });
3731
3732        // The declarative rule fires at the relu sink and fuses to Op::Fused.
3733        assert!(rule.matches(&g, r), "declarative pattern matches relu(add)");
3734        let mut remap = HashMap::new();
3735        rule.rewrite(&mut g, r, &mut remap);
3736        let fused_id = remap[&r];
3737        match &g.node(fused_id).op {
3738            Op::Fused(id, FusedOpParams::SoftmaxLastDim) => {
3739                assert_eq!(*id, FusedOps::SOFTMAX_LAST_DIM)
3740            }
3741            other => panic!("expected Op::Fused(SOFTMAX_LAST_DIM, _), got {other:?}"),
3742        }
3743        assert_eq!(
3744            g.node(fused_id).inputs,
3745            vec![a, b],
3746            "fused inputs = the region's bound external inputs",
3747        );
3748    }
3749
3750    #[test]
3751    fn runtime_op_fuses_and_round_trips_through_the_pass() {
3752        use crate::jit::{OpAttrs, OpTag, PatternNode};
3753        use crate::runtime_fused::register_runtime_fused;
3754        // A region no other test registers — tanh(sub(a, b)) — so the fuse is
3755        // deterministic (only our runtime rule matches it; sub is non-commutative).
3756        let region = PatternNode::Op {
3757            op: OpTag::Tanh,
3758            attrs: OpAttrs::default(),
3759            operands: vec![PatternNode::Op {
3760                op: OpTag::Sub,
3761                attrs: OpAttrs::default(),
3762                operands: vec![
3763                    PatternNode::Bind { index: 0 },
3764                    PatternNode::Bind { index: 1 },
3765                ],
3766            }],
3767        };
3768        let rid = register_runtime_fused("test::tanh_sub", region).unwrap();
3769
3770        // Build tanh(sub(a, b)) on primitives (b shares a's graph).
3771        let a = NodeHandle::from_f32(
3772            vec![0.5, -0.5, 1.0, -1.0],
3773            Shape::from_dims(&[4]),
3774            cpu_dev(),
3775        )
3776        .unwrap();
3777        let b = a
3778            .const_f32_like(vec![0.1, 0.2, 0.3, 0.4], Shape::from_dims(&[4]))
3779            .unwrap();
3780        let y = a.sub(&b).tanh();
3781        let graph = y.graph().clone();
3782
3783        // default_rules: lower (no-op here) then fuse — the runtime declarative
3784        // FusionRule folds tanh(sub) → Op::Fused(rid, Runtime).
3785        let fused = RuleRegistry::default_rules().optimize_to_fixpoint(&graph, &[y.id()]);
3786        {
3787            let g = graph.read().unwrap();
3788            match &g.node(fused[0]).op {
3789                Op::Fused(fid, FusedOpParams::Runtime { scalars }) => {
3790                    assert_eq!(*fid, rid, "fused to our runtime op");
3791                    assert!(fid.is_runtime());
3792                    assert!(scalars.is_empty(), "parameterless region");
3793                }
3794                other => panic!("expected runtime Op::Fused, got {other:?}"),
3795            }
3796        }
3797
3798        // lowering_only: the kernel-absent path — decompose the runtime op back
3799        // to its region on primitives (tanh(sub(a, b))).
3800        let lowered = RuleRegistry::lowering_only().optimize_to_fixpoint(&graph, &fused);
3801        let g = graph.read().unwrap();
3802        assert!(
3803            matches!(g.node(lowered[0]).op, Op::Tanh),
3804            "re-emitted sink is Tanh"
3805        );
3806        let sub_id = g.node(lowered[0]).inputs[0];
3807        assert!(matches!(g.node(sub_id).op, Op::Sub), "Tanh's input is Sub");
3808        assert_eq!(
3809            g.node(sub_id).inputs.len(),
3810            2,
3811            "Sub over the two bound inputs"
3812        );
3813    }
3814
3815    #[test]
3816    fn capability_gate_blocks_fusion_without_a_kernel() {
3817        use crate::jit::{OpAttrs, OpTag, PatternNode};
3818        use crate::runtime_fused::register_runtime_fused;
3819        // Unique region — sigmoid(div(a, b)) — so the gate decision is ours alone.
3820        let region = PatternNode::Op {
3821            op: OpTag::Sigmoid,
3822            attrs: OpAttrs::default(),
3823            operands: vec![PatternNode::Op {
3824                op: OpTag::Div,
3825                attrs: OpAttrs::default(),
3826                operands: vec![
3827                    PatternNode::Bind { index: 0 },
3828                    PatternNode::Bind { index: 1 },
3829                ],
3830            }],
3831        };
3832        let rid = register_runtime_fused("test::sigmoid_div", region).unwrap();
3833
3834        let a = NodeHandle::from_f32(vec![1.0, 2.0, 3.0, 4.0], Shape::from_dims(&[4]), cpu_dev())
3835            .unwrap();
3836        let b = a
3837            .const_f32_like(vec![2.0, 2.0, 2.0, 2.0], Shape::from_dims(&[4]))
3838            .unwrap();
3839        let y = a.div(&b).sigmoid();
3840        let graph = y.graph().clone();
3841
3842        // No kernel for our op → it is gated out of fusion (reported) and its
3843        // region stays primitive — the miss is caught at match time, not after.
3844        let (rules, gated_out) = RuleRegistry::capability_gated_rules(|id| id != rid);
3845        assert!(
3846            gated_out.contains(&rid),
3847            "kernel-absent op is reported as a miss candidate"
3848        );
3849        let roots = rules.optimize_to_fixpoint(&graph, &[y.id()]);
3850        {
3851            let g = graph.read().unwrap();
3852            assert!(
3853                matches!(g.node(roots[0]).op, Op::Sigmoid),
3854                "kernel-absent runtime op stays primitive (Sigmoid sink), never fuses",
3855            );
3856        }
3857
3858        // With a kernel available, the same op fuses.
3859        let (rules2, _) = RuleRegistry::capability_gated_rules(|_| true);
3860        let roots2 = rules2.optimize_to_fixpoint(&graph, &[roots[0]]);
3861        let g = graph.read().unwrap();
3862        assert!(
3863            matches!(g.node(roots2[0]).op, Op::Fused(fid, _) if fid == rid),
3864            "with a kernel, the runtime op fuses",
3865        );
3866    }
3867
3868    #[test]
3869    fn cse_does_not_fold_distinct_ops() {
3870        // Add and Mul on the same inputs are not equivalent.
3871        let (graph, a) = make_scalar_graph();
3872        let sum = a.add(&a);
3873        let prod = a.mul(&a);
3874        let new_roots = optimize(&graph, &[sum.id(), prod.id()]);
3875        assert_ne!(new_roots[0], new_roots[1], "Add and Mul must stay distinct");
3876    }
3877
3878    #[test]
3879    fn cse_does_not_fold_distinct_scalars() {
3880        let (graph, a) = make_scalar_graph();
3881        let b = a.add_scalar(1.0);
3882        let c = a.add_scalar(2.0);
3883        let new_roots = optimize(&graph, &[b.id(), c.id()]);
3884        assert_ne!(
3885            new_roots[0], new_roots[1],
3886            "AddScalar with different c must stay distinct"
3887        );
3888    }
3889
3890    #[test]
3891    fn cse_nested_chain_deduplicates() {
3892        // (a + a) + a  appearing twice should dedupe both subexpressions.
3893        let (graph, a) = make_scalar_graph();
3894        let p1 = a.add(&a);
3895        let p2 = a.add(&a); // duplicates p1
3896        let q1 = p1.add(&a);
3897        let q2 = p2.add(&a); // structurally identical to q1 via CSE
3898        let new_roots = optimize(&graph, &[q1.id(), q2.id()]);
3899        assert_eq!(new_roots[0], new_roots[1], "nested duplicates must fold");
3900    }
3901
3902    // ---- derive_ordering + execution_plan -----------------------------------
3903
3904    #[test]
3905    fn derive_ordering_empty_for_non_destructive_graph() {
3906        let a = NodeHandle::from_f32(vec![1.0, 2.0], Shape::from_dims(&[2]), cpu_dev()).unwrap();
3907        let b = a
3908            .const_f32_like(vec![3.0, 4.0], Shape::from_dims(&[2]))
3909            .unwrap();
3910        let c = a.add(&b);
3911        let ord = derive_ordering(&c.graph().read().unwrap(), &[c.id()]);
3912        assert!(
3913            ord.is_empty(),
3914            "graph without destructive ops → no ordering edges"
3915        );
3916    }
3917
3918    #[test]
3919    fn derive_ordering_release_must_run_after_sibling_readers() {
3920        // Graph:
3921        //   a       (producer)
3922        //   b = relu(a)   (non-destructive reader of a)
3923        //   r = release(a) (destructive reader of a)
3924        // Expected ordering: r must run after b.
3925        let a = NodeHandle::from_f32(vec![1.0, 2.0], Shape::from_dims(&[2]), cpu_dev()).unwrap();
3926        let b = a.relu();
3927        let r = a.release();
3928        let ord = derive_ordering(&a.graph().read().unwrap(), &[b.id(), r.id()]);
3929        let deps = ord.deps_of(r.id());
3930        assert_eq!(
3931            deps.len(),
3932            1,
3933            "release should have one ordering dep (the relu)"
3934        );
3935        assert_eq!(deps[0], b.id(), "release must run after relu");
3936    }
3937
3938    #[test]
3939    fn derive_ordering_follows_view_chains_for_destructive_op() {
3940        // Phase 4a of in-place ops infrastructure: a destructive op
3941        // on `x` must run after any reader of any view-of-x, because
3942        // views share `x`'s Storage Arc at realize time. Without the
3943        // alias-set walk in derive_ordering, this case would not pin
3944        // `reader(view)` before `destructive(x)`.
3945        //
3946        // Graph:
3947        //   x       (producer; rank-2)
3948        //   v       = x.transpose()    (view of x)
3949        //   z       = v.relu()         (reader of v, transitively reads x's bytes)
3950        //   r       = x.release()      (destructive on x)
3951        // Expected: r must run after z (not just after v).
3952        let x = NodeHandle::from_f32(
3953            vec![1.0_f32, 2.0, 3.0, 4.0],
3954            Shape::from_dims(&[2, 2]),
3955            cpu_dev(),
3956        )
3957        .unwrap();
3958        let v = x.transpose();
3959        let z = v.relu();
3960        let r = x.release();
3961        let ord = derive_ordering(&x.graph().read().unwrap(), &[z.id(), r.id()]);
3962        let deps = ord.deps_of(r.id());
3963        assert!(
3964            deps.contains(&z.id()),
3965            "release(x) must be pinned after relu(transpose(x)) (view-aware alias set); got deps = {deps:?}",
3966        );
3967    }
3968
3969    #[test]
3970    fn derive_ordering_inplace_unary_through_view_chain() {
3971        // Specifically for the in-place ops Phase 4 use case:
3972        // `y = x.transpose(); z = y.relu(); x.relu_inplace()` —
3973        // ReluInplace must be pinned after the read through the
3974        // transpose view chain.
3975        let x = NodeHandle::from_f32(
3976            vec![-1.0_f32, 2.0, -3.0, 4.0],
3977            Shape::from_dims(&[2, 2]),
3978            cpu_dev(),
3979        )
3980        .unwrap();
3981        let y = x.transpose();
3982        let z = y.relu();
3983        // Capture x's shape + dtype + id BEFORE acquiring the write
3984        // lock — otherwise the args evaluate inside the locked region
3985        // and self-deadlock (RwLock doesn't grant a read lock to the
3986        // writer-holding thread).
3987        let x_shape = x.shape();
3988        let x_dtype = x.dtype();
3989        let x_id = x.id();
3990        let inplace_id = x.graph().write().unwrap().push(crate::Node {
3991            op: crate::Op::ReluInplace,
3992            inputs: vec![x_id],
3993            shape: x_shape,
3994            dtype: x_dtype,
3995        });
3996        let ord = derive_ordering(&x.graph().read().unwrap(), &[z.id(), inplace_id]);
3997        let deps = ord.deps_of(inplace_id);
3998        assert!(
3999            deps.contains(&z.id()),
4000            "ReluInplace(x) must be pinned after Relu(Transpose(x)) via view-aware alias set; got deps = {deps:?}",
4001        );
4002    }
4003
4004    // ---- Ordering-defect repros (MLA decode multi-round WriteSlice) ----
4005
4006    /// Control: a single-hop `Slice` reader of a `WriteSlice`
4007    /// destination is already correctly pinned before a SECOND
4008    /// `WriteSlice` that reuses the same destination —
4009    /// `collect_alias_set` finds the `Slice` via `is_view_op`, and its
4010    /// reader (`relu`) is not itself an alias member, so
4011    /// `derive_ordering` pins the second `WriteSlice` after it
4012    /// directly. Baseline for the Reshape-gap test below (confirms the
4013    /// gap is Reshape-specific, not a general multi-round WriteSlice
4014    /// failure).
4015    #[test]
4016    fn derive_ordering_write_slice_round2_plain_slice_reader_pinned() {
4017        let dest =
4018            NodeHandle::from_f32(vec![0.0_f32; 4], Shape::from_dims(&[4]), cpu_dev()).unwrap();
4019        let src1 = dest
4020            .const_f32_like(vec![1.0_f32, 2.0], Shape::from_dims(&[2]))
4021            .unwrap();
4022        let src2 = dest
4023            .const_f32_like(vec![9.0_f32, 8.0], Shape::from_dims(&[2]))
4024            .unwrap();
4025        let x = dest
4026            .write_slice(&src1, vec![(0, 2)])
4027            .expect("write_slice round 1");
4028        let s = x.slice(0, 0, 2);
4029        let k = s.relu();
4030        let y = x
4031            .write_slice(&src2, vec![(0, 2)])
4032            .expect("write_slice round 2");
4033        let ord = derive_ordering(&x.graph().read().unwrap(), &[k.id(), y.id()]);
4034        let deps = ord.deps_of(y.id());
4035        assert!(
4036            deps.contains(&k.id()),
4037            "round-2 write_slice must be pinned after the round-1 slice's \
4038             reader; got deps = {deps:?}",
4039        );
4040    }
4041
4042    /// CRITICAL SUSPECT #2 (MLA decode multi-round `WriteSlice`
4043    /// ordering defect): same shape as the control above, but the
4044    /// `Slice`'s reader is behind an `Op::Reshape` hop first.
4045    /// `Op::Reshape` is NOT in `collect_alias_set`'s alias-extending
4046    /// predicate (only `Op::is_view_op` + `Op::View`) even though its
4047    /// `WorkItemKind::ContiguizeOf` execution zero-copy ADOPTS the
4048    /// input's Storage Arc whenever the input is contiguous at offset
4049    /// 0 (`fuel-dispatch/src/pipelined.rs`'s `ContiguizeOf` arm) — so
4050    /// the alias walk stops at the `Slice`, and the Reshape's own
4051    /// consumer (`relu`) is never scanned for pinning against the
4052    /// round-2 `WriteSlice`. Born-red: fails before the fix (Reshape
4053    /// missing from the alias-extending set), green after.
4054    #[test]
4055    fn derive_ordering_write_slice_round2_reshape_reader_pinned() {
4056        let dest =
4057            NodeHandle::from_f32(vec![0.0_f32; 4], Shape::from_dims(&[4]), cpu_dev()).unwrap();
4058        let src1 = dest
4059            .const_f32_like(vec![1.0_f32, 2.0], Shape::from_dims(&[2]))
4060            .unwrap();
4061        let src2 = dest
4062            .const_f32_like(vec![9.0_f32, 8.0], Shape::from_dims(&[2]))
4063            .unwrap();
4064        let x = dest
4065            .write_slice(&src1, vec![(0, 2)])
4066            .expect("write_slice round 1");
4067        let s = x.slice(0, 0, 2);
4068        let r = s.reshape(Shape::from_dims(&[2]));
4069        let k = r.relu();
4070        let y = x
4071            .write_slice(&src2, vec![(0, 2)])
4072            .expect("write_slice round 2");
4073        let ord = derive_ordering(&x.graph().read().unwrap(), &[k.id(), y.id()]);
4074        let deps = ord.deps_of(y.id());
4075        assert!(
4076            deps.contains(&k.id()),
4077            "round-2 write_slice must be pinned after the reshape-adopted-arc \
4078             reader (CRITICAL SUSPECT #2 gap — Reshape excluded from \
4079             collect_alias_set's alias-extending predicate); got deps = {deps:?}",
4080        );
4081    }
4082
4083    // ---- Phase 5: insert_safety_copies ----
4084
4085    #[test]
4086    fn insert_safety_copies_residual_connection_breaks_cycle() {
4087        // Canonical residual pattern: `y = x.relu_inplace(); z = y + x`.
4088        // Before Phase 5: derive_ordering pins ReluInplace after Add
4089        // (Add reads x), but data flow pins Add after ReluInplace (Add
4090        // reads y, y depends on ReluInplace) → cycle. After Phase 5:
4091        // a Copy(x) → x_safe is inserted and Add's x input rewires to
4092        // x_safe.
4093        let x = NodeHandle::from_f32(
4094            vec![1.0_f32, -2.0, 3.0, -4.0],
4095            Shape::from_dims(&[4]),
4096            cpu_dev(),
4097        )
4098        .unwrap();
4099        let x_shape = x.shape();
4100        let x_dtype = x.dtype();
4101        let x_id = x.id();
4102
4103        // Manually build: y = ReluInplace(x); z = Add(y, x).
4104        let (y_id, z_id) = {
4105            let mut g = x.graph().write().unwrap();
4106            let y_id = g.push(crate::Node {
4107                op: crate::Op::ReluInplace,
4108                inputs: vec![x_id],
4109                shape: x_shape.clone(),
4110                dtype: x_dtype,
4111            });
4112            let z_id = g.push(crate::Node {
4113                op: crate::Op::Add,
4114                inputs: vec![y_id, x_id],
4115                shape: x_shape,
4116                dtype: x_dtype,
4117            });
4118            (y_id, z_id)
4119        };
4120
4121        // Before the pass: cycle would exist. Run insert_safety_copies.
4122        let inserted = insert_safety_copies(&mut x.graph().write().unwrap(), &[z_id]);
4123        assert_eq!(inserted, 1, "should insert exactly one safety copy");
4124
4125        // Verify Add's inputs were rewired: still [y_id, <something>],
4126        // but the second input is NOT x_id anymore — it's the new
4127        // Op::Copy node.
4128        let copy_id = {
4129            let g = x.graph().read().unwrap();
4130            let z_node = g.node(z_id);
4131            assert_eq!(z_node.inputs.len(), 2, "Add still has 2 inputs");
4132            assert_eq!(z_node.inputs[0], y_id, "Add's y input unchanged");
4133            assert_ne!(z_node.inputs[1], x_id, "Add's x input was rewired");
4134            let copy_node_id = z_node.inputs[1];
4135            let copy_node = g.node(copy_node_id);
4136            assert!(
4137                matches!(copy_node.op, crate::Op::Copy { .. }),
4138                "rewired input is Op::Copy; got {:?}",
4139                copy_node.op
4140            );
4141            assert_eq!(copy_node.inputs, vec![x_id], "Op::Copy reads x");
4142            copy_node_id
4143        };
4144
4145        // Verify execution_plan succeeds (no cycle).
4146        let plan = execution_plan(&x.graph().read().unwrap(), &[z_id]);
4147        // Plan must contain x, the copy, ReluInplace, and Add.
4148        // The copy must come before ReluInplace (it's a reader of x).
4149        let pos: HashMap<NodeId, usize> = plan.iter().enumerate().map(|(i, n)| (*n, i)).collect();
4150        let copy_pos = pos[&copy_id];
4151        let inplace_pos = pos[&y_id];
4152        assert!(
4153            copy_pos < inplace_pos,
4154            "Copy must run before ReluInplace; copy={copy_pos} inplace={inplace_pos}"
4155        );
4156    }
4157
4158    /// CapturedRun executor build-out (4b-ζ follow-up): when a graph has
4159    /// `target_backend` stamped (ordinary single-device `optimize_graph`
4160    /// output — the common case) but no explicit `placement` (a rarely-
4161    /// used pin), the inserted safety copy's `target_location` must be
4162    /// derived from `target_backend`, NOT default straight to `Cpu`. A
4163    /// same-device CUDA safety snapshot silently landing on `Cpu` strands
4164    /// its (unrelated) reader off-device — and, in a CUDA decode graph
4165    /// bound for `capture_decode`, turns an ordinary same-device
4166    /// snapshot into a genuinely-cross-device D2H that capture must
4167    /// reject (capture only ever accepts a same-device CUDA `Op::Copy`).
4168    /// Born-red without the `target_backend` fallback (old code:
4169    /// `.unwrap_or(DeviceLocation::Cpu)` fires immediately since
4170    /// `placement` is never populated here); green with it.
4171    #[test]
4172    fn insert_safety_copies_derives_target_location_from_target_backend_when_unplaced() {
4173        // Same residual shape as `insert_safety_copies_residual_connection_breaks_cycle`
4174        // (`y = x.relu_inplace(); z = y + x`), but every node is stamped
4175        // `target_backend = Cuda` (the ordinary optimize_graph output for a
4176        // single-device CUDA decode graph) and NO node has an explicit
4177        // `placement` — the realistic shape of a real Llama decode graph.
4178        let x = NodeHandle::from_f32(
4179            vec![1.0_f32, -2.0, 3.0, -4.0],
4180            Shape::from_dims(&[4]),
4181            cpu_dev(),
4182        )
4183        .unwrap();
4184        let x_shape = x.shape();
4185        let x_dtype = x.dtype();
4186        let x_id = x.id();
4187
4188        let (_y_id, z_id) = {
4189            let mut g = x.graph().write().unwrap();
4190            let y_id = g.push(crate::Node {
4191                op: crate::Op::ReluInplace,
4192                inputs: vec![x_id],
4193                shape: x_shape.clone(),
4194                dtype: x_dtype,
4195            });
4196            let z_id = g.push(crate::Node {
4197                op: crate::Op::Add,
4198                inputs: vec![y_id, x_id],
4199                shape: x_shape,
4200                dtype: x_dtype,
4201            });
4202            g.set_target_backend(y_id, fuel_ir::probe::BackendId::Cuda);
4203            g.set_target_backend(z_id, fuel_ir::probe::BackendId::Cuda);
4204            (y_id, z_id)
4205        };
4206        assert!(
4207            x.graph().read().unwrap().placement(x_id).is_none(),
4208            "precondition: x has no explicit placement",
4209        );
4210
4211        let inserted = insert_safety_copies(&mut x.graph().write().unwrap(), &[z_id]);
4212        assert_eq!(inserted, 1, "should insert exactly one safety copy");
4213
4214        let g = x.graph().read().unwrap();
4215        let z_node = g.node(z_id);
4216        let copy_id = z_node.inputs[1];
4217        let copy_node = g.node(copy_id);
4218        assert_eq!(
4219            copy_node.op,
4220            crate::Op::Copy {
4221                target: DeviceLocation::Cuda { gpu_id: 0 }
4222            },
4223            "same-device CUDA safety copy must target Cuda (derived from \
4224             target_backend), NOT fall back to Cpu when placement is unset",
4225        );
4226    }
4227
4228    #[test]
4229    fn insert_safety_copies_noop_when_no_destructive_conflicts() {
4230        // Pure functional graph — no in-place ops. The pass should
4231        // insert zero copies.
4232        let x = NodeHandle::from_f32(vec![1.0, 2.0], Shape::from_dims(&[2]), cpu_dev()).unwrap();
4233        let y = x.relu();
4234        let z = y.sum_all();
4235        let inserted = insert_safety_copies(&mut x.graph().write().unwrap(), &[z.id()]);
4236        assert_eq!(inserted, 0);
4237    }
4238
4239    #[test]
4240    fn insert_safety_copies_noop_when_inplace_target_only_consumed_via_alias_path() {
4241        // `y = x.relu_inplace(); loss = y.sum_all()` — the only
4242        // downstream consumer of x reads it through y (the in-place
4243        // node), not x directly. derive_ordering pins ReluInplace
4244        // after... only ReluInplace itself (no other readers of x).
4245        // No conflict, no copy needed.
4246        let x =
4247            NodeHandle::from_f32(vec![1.0_f32, -1.0], Shape::from_dims(&[2]), cpu_dev()).unwrap();
4248        let x_shape = x.shape();
4249        let x_dtype = x.dtype();
4250        let x_id = x.id();
4251        let y_id = x.graph().write().unwrap().push(crate::Node {
4252            op: crate::Op::ReluInplace,
4253            inputs: vec![x_id],
4254            shape: x_shape,
4255            dtype: x_dtype,
4256        });
4257        // Build a chain that only reads y (not x).
4258        let inserted = insert_safety_copies(&mut x.graph().write().unwrap(), &[y_id]);
4259        assert_eq!(inserted, 0);
4260    }
4261
4262    /// Regression for the 2026-06-11 live-GPU failure: a reader with
4263    /// NO dependency relationship to a destructive `Op::Move` must
4264    /// not get a safety copy just because DFS visit order happened to
4265    /// place it after the Move.
4266    ///
4267    /// Graph: `a; b = relu(a); m = move(a)`. derive_ordering pins
4268    /// `m` after `b` (reader-before-mutator), so every execution_plan
4269    /// runs `b` before `m`'s release — no copy is needed in EITHER
4270    /// roots order. The old positional predicate inserted a spurious
4271    /// copy when roots ordering made `b` land after `m` in
4272    /// `topo_order_multi` (which visits the LAST root's subtree
4273    /// first), and that copy then inherited the Move's device.
4274    #[test]
4275    fn insert_safety_copies_no_spurious_copy_for_independent_reader_of_move() {
4276        for flip_roots in [false, true] {
4277            let a = NodeHandle::from_f32(vec![1.0_f32, -2.0], Shape::from_dims(&[2]), cpu_dev())
4278                .unwrap();
4279            let b = a.relu();
4280            let a_shape = a.shape();
4281            let a_dtype = a.dtype();
4282            let a_id = a.id();
4283            let m_id = a.graph().write().unwrap().push(crate::Node {
4284                op: crate::Op::Move {
4285                    target: DeviceLocation::Cpu,
4286                },
4287                inputs: vec![a_id],
4288                shape: a_shape,
4289                dtype: a_dtype,
4290            });
4291            let roots = if flip_roots {
4292                [m_id, b.id()]
4293            } else {
4294                [b.id(), m_id]
4295            };
4296            // Sanity: in the `[b, m]` roots order, DFS visits m's
4297            // subtree first, so b lands positionally AFTER the Move —
4298            // the exact shape the old predicate misclassified.
4299            let inserted = insert_safety_copies(&mut a.graph().write().unwrap(), &roots);
4300            assert_eq!(
4301                inserted, 0,
4302                "relu(a) is pinned before move(a) by derive_ordering; \
4303                 no copy regardless of roots order (flip_roots={flip_roots})",
4304            );
4305            let g = a.graph().read().unwrap();
4306            assert_eq!(
4307                g.node(b.id()).inputs,
4308                vec![a_id],
4309                "reader must keep reading `a` directly",
4310            );
4311            drop(g);
4312            // The plan must still order the reader before the Move.
4313            let plan = execution_plan(&a.graph().read().unwrap(), &roots);
4314            let b_pos = plan.iter().position(|&n| n == b.id()).unwrap();
4315            let m_pos = plan.iter().position(|&n| n == m_id).unwrap();
4316            assert!(b_pos < m_pos, "relu must precede move: {plan:?}");
4317        }
4318    }
4319
4320    /// A reader that TRANSITIVELY depends on the destructive op (not
4321    /// just via a direct input) still conflicts: the pin edge would
4322    /// close a cycle through the intermediate node. The
4323    /// dependency-based predicate must use reachability, not direct
4324    /// input inspection.
4325    ///
4326    /// Graph: `y = relu_inplace(x); w = relu(y); z = add(w, x)`.
4327    #[test]
4328    fn insert_safety_copies_transitive_descendant_reader_gets_copy() {
4329        let x = NodeHandle::from_f32(
4330            vec![1.0_f32, -2.0, 3.0, -4.0],
4331            Shape::from_dims(&[4]),
4332            cpu_dev(),
4333        )
4334        .unwrap();
4335        let x_shape = x.shape();
4336        let x_dtype = x.dtype();
4337        let x_id = x.id();
4338        let (y_id, z_id) = {
4339            let mut g = x.graph().write().unwrap();
4340            let y_id = g.push(crate::Node {
4341                op: crate::Op::ReluInplace,
4342                inputs: vec![x_id],
4343                shape: x_shape.clone(),
4344                dtype: x_dtype,
4345            });
4346            let w_id = g.push(crate::Node {
4347                op: crate::Op::Relu,
4348                inputs: vec![y_id],
4349                shape: x_shape.clone(),
4350                dtype: x_dtype,
4351            });
4352            let z_id = g.push(crate::Node {
4353                op: crate::Op::Add,
4354                inputs: vec![w_id, x_id],
4355                shape: x_shape,
4356                dtype: x_dtype,
4357            });
4358            (y_id, z_id)
4359        };
4360        let inserted = insert_safety_copies(&mut x.graph().write().unwrap(), &[z_id]);
4361        assert_eq!(inserted, 1, "transitive residual cycle needs one copy");
4362        let g = x.graph().read().unwrap();
4363        let z_node = g.node(z_id);
4364        assert_ne!(z_node.inputs[1], x_id, "z's x input must be rewired");
4365        assert!(matches!(
4366            g.node(z_node.inputs[1]).op,
4367            crate::Op::Copy { .. }
4368        ));
4369        drop(g);
4370        let plan = execution_plan(&x.graph().read().unwrap(), &[z_id]);
4371        assert!(plan.contains(&y_id), "plan still contains the in-place op");
4372    }
4373
4374    /// A genuinely-parallel reader — no ordering guarantee in either
4375    /// direction — keeps its safety copy (conservative default).
4376    ///
4377    /// The only readers derive_ordering does NOT pin before the
4378    /// mutator are alias-set members (view ops of the target). A view
4379    /// with consumers is transitively forced before the mutator via
4380    /// its pinned consumers; a view that is itself a realize root has
4381    /// no ordering guarantee at all. Safety cannot be proven, so the
4382    /// pass must copy. Note the old positional predicate got this
4383    /// case WRONG in the `[m, v]` roots order (v lands positionally
4384    /// before m → no copy; only the plan's stable tiebreak saved it).
4385    #[test]
4386    fn insert_safety_copies_parallel_view_root_reader_keeps_copy() {
4387        let x = NodeHandle::from_f32(
4388            vec![1.0_f32, 2.0, 3.0, 4.0],
4389            Shape::from_dims(&[2, 2]),
4390            cpu_dev(),
4391        )
4392        .unwrap();
4393        let v = x.transpose();
4394        let x_shape = x.shape();
4395        let x_dtype = x.dtype();
4396        let x_id = x.id();
4397        let m_id = x.graph().write().unwrap().push(crate::Node {
4398            op: crate::Op::Move {
4399                target: DeviceLocation::Cpu,
4400            },
4401            inputs: vec![x_id],
4402            shape: x_shape,
4403            dtype: x_dtype,
4404        });
4405        // Roots [m, v]: DFS visits v's subtree first, so v sits
4406        // positionally BEFORE the Move — the shape the positional
4407        // predicate silently skipped.
4408        let inserted = insert_safety_copies(&mut x.graph().write().unwrap(), &[m_id, v.id()]);
4409        assert_eq!(
4410            inserted, 1,
4411            "unpinned view-root reader has no ordering guarantee → copy",
4412        );
4413        let g = x.graph().read().unwrap();
4414        let v_node = g.node(v.id());
4415        assert_ne!(
4416            v_node.inputs[0], x_id,
4417            "view must be rewired to the snapshot"
4418        );
4419        assert!(matches!(
4420            g.node(v_node.inputs[0]).op,
4421            crate::Op::Copy { .. }
4422        ));
4423    }
4424
4425    /// Cycles through ANOTHER destructive op's ordering edge are
4426    /// still conflicts — the predicate must walk the combined
4427    /// (data + ordering) graph, not just data edges.
4428    ///
4429    /// Graph:
4430    ///   m1 = move(t1); m2 = move(t2)
4431    ///   x  = add(m1, t2)   (pin x→m2; data m1→x)
4432    ///   r  = add(m2, t1)   (pin r→m1; data m2→r)
4433    /// Combined cycle: m1 → x → m2 → r → m1. Neither x nor r is a
4434    /// pure-data descendant of the op whose target it reads, yet
4435    /// both pins are unsatisfiable. Both need copies; afterwards
4436    /// execution_plan must succeed.
4437    #[test]
4438    fn insert_safety_copies_breaks_cycle_through_other_ordering_edges() {
4439        let t1 =
4440            NodeHandle::from_f32(vec![1.0_f32, 2.0], Shape::from_dims(&[2]), cpu_dev()).unwrap();
4441        let t2 = t1
4442            .const_f32_like(vec![3.0_f32, 4.0], Shape::from_dims(&[2]))
4443            .unwrap();
4444        let shape = t1.shape();
4445        let dtype = t1.dtype();
4446        let (t1_id, t2_id) = (t1.id(), t2.id());
4447        let (x_id, r_id) = {
4448            let mut g = t1.graph().write().unwrap();
4449            let m1 = g.push(crate::Node {
4450                op: crate::Op::Move {
4451                    target: DeviceLocation::Cpu,
4452                },
4453                inputs: vec![t1_id],
4454                shape: shape.clone(),
4455                dtype,
4456            });
4457            let m2 = g.push(crate::Node {
4458                op: crate::Op::Move {
4459                    target: DeviceLocation::Cpu,
4460                },
4461                inputs: vec![t2_id],
4462                shape: shape.clone(),
4463                dtype,
4464            });
4465            let x = g.push(crate::Node {
4466                op: crate::Op::Add,
4467                inputs: vec![m1, t2_id],
4468                shape: shape.clone(),
4469                dtype,
4470            });
4471            let r = g.push(crate::Node {
4472                op: crate::Op::Add,
4473                inputs: vec![m2, t1_id],
4474                shape,
4475                dtype,
4476            });
4477            (x, r)
4478        };
4479        let inserted = insert_safety_copies(&mut t1.graph().write().unwrap(), &[x_id, r_id]);
4480        assert_eq!(inserted, 2, "both cross-readers sit on the combined cycle");
4481        // The rewritten graph must be plannable (no cycle panic).
4482        let plan = execution_plan(&t1.graph().read().unwrap(), &[x_id, r_id]);
4483        assert!(plan.contains(&x_id) && plan.contains(&r_id));
4484    }
4485
4486    /// Idempotence under the dependency-based predicate: a second run
4487    /// sees the inserted Copy as a pinned, acyclic reader of the
4488    /// target and inserts nothing new.
4489    #[test]
4490    fn insert_safety_copies_idempotent_after_rewrite() {
4491        let x =
4492            NodeHandle::from_f32(vec![1.0_f32, -2.0], Shape::from_dims(&[2]), cpu_dev()).unwrap();
4493        let x_shape = x.shape();
4494        let x_dtype = x.dtype();
4495        let x_id = x.id();
4496        let z_id = {
4497            let mut g = x.graph().write().unwrap();
4498            let y_id = g.push(crate::Node {
4499                op: crate::Op::ReluInplace,
4500                inputs: vec![x_id],
4501                shape: x_shape.clone(),
4502                dtype: x_dtype,
4503            });
4504            g.push(crate::Node {
4505                op: crate::Op::Add,
4506                inputs: vec![y_id, x_id],
4507                shape: x_shape,
4508                dtype: x_dtype,
4509            })
4510        };
4511        let first = insert_safety_copies(&mut x.graph().write().unwrap(), &[z_id]);
4512        assert_eq!(first, 1);
4513        let second = insert_safety_copies(&mut x.graph().write().unwrap(), &[z_id]);
4514        assert_eq!(second, 0, "second run must be a no-op");
4515    }
4516
4517    /// Performance regression: a destructive op whose target has NO other
4518    /// reader is the COMMON case in a real decode graph (KV-cache writes,
4519    /// release markers). Two INDEPENDENT quadratic costs used to fire on
4520    /// every one of the `N` destructive ops regardless: (1)
4521    /// `insert_safety_copies`' own pair of `reach()` graph walks plus an
4522    /// `order.len()`-sized linear readers-scan, and (2) `derive_ordering`'s
4523    /// `collect_alias_set` (now `alias_groups`), which re-walked the ENTIRE
4524    /// `order` per destructive op to find alias members. `N` independent
4525    /// 2-node chains (a `Const` leaf read ONLY by its own `ReluInplace`)
4526    /// make BOTH scale as `N * order.len()` under the old code. Measured on
4527    /// this box: unpatched (both costs present), `N=4000` took 18.2s;
4528    /// patched (both fixed — the `succ`-map pre-check here, plus
4529    /// `alias_groups`' single O(order.len()) pass shared across every
4530    /// destructive op instead of one pass each), `N=4000` takes ~62ms —
4531    /// a ~294x improvement. The bound below is generous in the PATCHED
4532    /// direction (keeps the test fast and non-flaky under shared-box
4533    /// contention) while still being far below what either quadratic old
4534    /// path could ever hit at this `N` — born red reverting either fix.
4535    #[test]
4536    fn insert_safety_copies_skips_reach_when_target_has_no_other_reader() {
4537        const N: usize = 4000;
4538        let shape = Shape::from_dims(&[4]);
4539        let mut g = crate::Graph::new();
4540        let mut roots: Vec<NodeId> = Vec::with_capacity(N);
4541        for _ in 0..N {
4542            let leaf = g.push(crate::Node {
4543                op: crate::Op::Const,
4544                inputs: vec![],
4545                shape: shape.clone(),
4546                dtype: DType::F32,
4547            });
4548            let relu = g.push(crate::Node {
4549                op: crate::Op::ReluInplace,
4550                inputs: vec![leaf],
4551                shape: shape.clone(),
4552                dtype: DType::F32,
4553            });
4554            roots.push(relu);
4555        }
4556
4557        let start = std::time::Instant::now();
4558        let inserted = insert_safety_copies(&mut g, &roots);
4559        let elapsed = start.elapsed();
4560
4561        assert_eq!(
4562            inserted, 0,
4563            "no leaf has a reader besides its own ReluInplace; nothing to copy"
4564        );
4565        assert!(
4566            elapsed < std::time::Duration::from_millis(500),
4567            "insert_safety_copies({N} independent no-reader chains) took {elapsed:?} — \
4568             the no-other-reader pre-check should make this O(N), not O(N^2)"
4569        );
4570    }
4571
4572    // ===== Differential safety-analysis harness (docs/design/incremental-consumer-index.md
4573    //       §4 step 1) =====
4574    //
4575    // Step 1 of the staged redesign plan: build the comparison machinery and a
4576    // corpus of representative graphs BEFORE any new incremental-index
4577    // production code exists (that's step 2). The "new" side below calls the
4578    // SAME old pass on an INDEPENDENTLY-constructed graph instance (not a
4579    // clone of the old one -- `Graph` deliberately isn't `Clone`, and this
4580    // harness doesn't need it to be: it never touches production code to get
4581    // there). That makes today's differential assertion validate the thing
4582    // step 1 actually needs validated — the comparison logic itself, and that
4583    // every corpus factory is deterministic (same NodeId assignments on every
4584    // call, since NodeId is push-order, not globally counted) — ready for
4585    // step 2 to swap `run_new_safety_analysis` for the real incremental index
4586    // without touching this file again.
4587    //
4588    // NOT done here (tracked, not silently skipped): the design doc's step 1
4589    // also calls for the live decode harness's ACTUAL graph in the corpus.
4590    // That needs a graph-capture hook in the live CUDA harness; out of scope
4591    // for this PR, which is corpus-of-unit-test-shapes + harness plumbing
4592    // only. Also not done: porting ALL nine existing insert_safety_copies/
4593    // derive_ordering tests into the corpus (design doc open item #2) — five
4594    // representative shapes are ported below; full porting is tracked for
4595    // the step-4 PR (view/alias resolution), where open item #2 is due.
4596
4597    /// One differential-corpus entry: builds a FRESH graph + its realize
4598    /// roots. Called twice per comparison (once per side) rather than
4599    /// cloned, so the harness never needs `Graph: Clone`.
4600    type SafetyCorpusFactory = fn() -> (SharedGraph, Vec<NodeId>);
4601
4602    /// A snapshotted, comparable view of "what insert_safety_copies (which
4603    /// internally calls derive_ordering) did to this graph" — the
4604    /// observable contract the differential test checks. `nodes` compares
4605    /// the WHOLE resulting graph structurally (op + inputs, Debug-formatted
4606    /// since `Op` has no blanket `PartialEq`) rather than just the inserted
4607    /// count, so a divergence that inserts the SAME NUMBER of copies in a
4608    /// DIFFERENT place is still caught.
4609    #[derive(Debug, PartialEq)]
4610    struct SafetyAnalysisResult {
4611        inserted: usize,
4612        node_count: usize,
4613        nodes: Vec<(String, Vec<NodeId>)>,
4614    }
4615
4616    fn run_old_safety_analysis(graph: &SharedGraph, roots: &[NodeId]) -> SafetyAnalysisResult {
4617        let inserted = insert_safety_copies(&mut graph.write().unwrap(), roots);
4618        let g = graph.read().unwrap();
4619        let nodes = (0..g.len())
4620            .map(|i| {
4621                let n = g.node(NodeId(i));
4622                (format!("{:?}", n.op), n.inputs.clone())
4623            })
4624            .collect();
4625        SafetyAnalysisResult {
4626            inserted,
4627            node_count: g.len(),
4628            nodes,
4629        }
4630    }
4631
4632    /// STEP 1 PLACEHOLDER — see the module note above. Step 2 (`consumers`
4633    /// side-table, push-only) replaces this body with the real incremental
4634    /// logic; nothing else in this harness needs to change when it does.
4635    fn run_new_safety_analysis(graph: &SharedGraph, roots: &[NodeId]) -> SafetyAnalysisResult {
4636        run_old_safety_analysis(graph, roots)
4637    }
4638
4639    fn assert_safety_analysis_matches(name: &str, factory: SafetyCorpusFactory) {
4640        let (old_graph, old_roots) = factory();
4641        let (new_graph, new_roots) = factory();
4642        assert_eq!(
4643            old_roots, new_roots,
4644            "{name}: factory produced different roots across two calls -- corpus factory \
4645             must be deterministic (same pushes, same order) or this harness can't compare"
4646        );
4647        let old = run_old_safety_analysis(&old_graph, &old_roots);
4648        let new = run_new_safety_analysis(&new_graph, &new_roots);
4649        assert_eq!(old, new, "{name}: old vs new safety analysis diverged");
4650    }
4651
4652    fn corpus_no_destructive_conflicts() -> (SharedGraph, Vec<NodeId>) {
4653        // a + b, no in-place op anywhere -- insert_safety_copies must be a
4654        // pure no-op (mirrors insert_safety_copies_noop_when_no_destructive_conflicts).
4655        let a = NodeHandle::from_f32(vec![1.0, 2.0], Shape::from_dims(&[2]), cpu_dev()).unwrap();
4656        let b = a
4657            .const_f32_like(vec![3.0, 4.0], Shape::from_dims(&[2]))
4658            .unwrap();
4659        let c = a.add(&b);
4660        (c.graph().clone(), vec![c.id()])
4661    }
4662
4663    fn corpus_residual_connection() -> (SharedGraph, Vec<NodeId>) {
4664        // y = x.relu_inplace(); z = y + x -- the canonical cycle-breaking
4665        // case (mirrors insert_safety_copies_residual_connection_breaks_cycle).
4666        let x = NodeHandle::from_f32(
4667            vec![1.0_f32, -2.0, 3.0, -4.0],
4668            Shape::from_dims(&[4]),
4669            cpu_dev(),
4670        )
4671        .unwrap();
4672        let x_shape = x.shape();
4673        let x_dtype = x.dtype();
4674        let x_id = x.id();
4675        let z_id = {
4676            let mut g = x.graph().write().unwrap();
4677            let y_id = g.push(crate::Node {
4678                op: crate::Op::ReluInplace,
4679                inputs: vec![x_id],
4680                shape: x_shape.clone(),
4681                dtype: x_dtype,
4682            });
4683            g.push(crate::Node {
4684                op: crate::Op::Add,
4685                inputs: vec![y_id, x_id],
4686                shape: x_shape,
4687                dtype: x_dtype,
4688            })
4689        };
4690        (x.graph().clone(), vec![z_id])
4691    }
4692
4693    fn corpus_independent_reader_of_move_needs_no_copy() -> (SharedGraph, Vec<NodeId>) {
4694        // relu(a) pinned before move(a); an unrelated reader needs no copy
4695        // (mirrors insert_safety_copies_no_spurious_copy_for_independent_reader_of_move,
4696        // flip_roots=false arm: roots=[b, m], DFS visits m's subtree first
4697        // so b lands positionally after the Move).
4698        let a =
4699            NodeHandle::from_f32(vec![1.0_f32, -2.0], Shape::from_dims(&[2]), cpu_dev()).unwrap();
4700        let b = a.relu();
4701        let a_shape = a.shape();
4702        let a_dtype = a.dtype();
4703        let a_id = a.id();
4704        let m_id = a.graph().write().unwrap().push(crate::Node {
4705            op: crate::Op::Move {
4706                target: DeviceLocation::Cpu,
4707            },
4708            inputs: vec![a_id],
4709            shape: a_shape,
4710            dtype: a_dtype,
4711        });
4712        (a.graph().clone(), vec![b.id(), m_id])
4713    }
4714
4715    fn corpus_many_independent_no_reader_chains() -> (SharedGraph, Vec<NodeId>) {
4716        // A small-N version of the perf regression shape above: many
4717        // independent 2-node chains, none with an extra reader. Exercises
4718        // the common case (most destructive ops in a real decode graph have
4719        // no other reader at all) at a size small enough that the
4720        // differential comparison's O(graph) structural equality check
4721        // stays cheap in a correctness-focused test, not a perf one.
4722        const N: usize = 25;
4723        let shape = Shape::from_dims(&[4]);
4724        let mut g = crate::Graph::new();
4725        let mut roots: Vec<NodeId> = Vec::with_capacity(N);
4726        for _ in 0..N {
4727            let leaf = g.push(crate::Node {
4728                op: crate::Op::Const,
4729                inputs: vec![],
4730                shape: shape.clone(),
4731                dtype: DType::F32,
4732            });
4733            let relu = g.push(crate::Node {
4734                op: crate::Op::ReluInplace,
4735                inputs: vec![leaf],
4736                shape: shape.clone(),
4737                dtype: DType::F32,
4738            });
4739            roots.push(relu);
4740        }
4741        (Arc::new(std::sync::RwLock::new(g)), roots)
4742    }
4743
4744    const SAFETY_CORPUS: &[(&str, SafetyCorpusFactory)] = &[
4745        ("no_destructive_conflicts", corpus_no_destructive_conflicts),
4746        ("residual_connection", corpus_residual_connection),
4747        (
4748            "independent_reader_of_move_needs_no_copy",
4749            corpus_independent_reader_of_move_needs_no_copy,
4750        ),
4751        (
4752            "many_independent_no_reader_chains",
4753            corpus_many_independent_no_reader_chains,
4754        ),
4755    ];
4756
4757    #[test]
4758    fn differential_safety_analysis_matches_on_corpus() {
4759        for &(name, factory) in SAFETY_CORPUS {
4760            assert_safety_analysis_matches(name, factory);
4761        }
4762    }
4763
4764    #[test]
4765    fn derive_ordering_release_of_multi_reader_input() {
4766        // a read by relu AND neg, then released. Both relu and neg
4767        // must precede the release.
4768        let a = NodeHandle::from_f32(vec![1.0, 2.0], Shape::from_dims(&[2]), cpu_dev()).unwrap();
4769        let b = a.relu();
4770        let c = a.neg();
4771        let r = a.release();
4772        let ord = derive_ordering(&a.graph().read().unwrap(), &[b.id(), c.id(), r.id()]);
4773        let mut deps = ord.deps_of(r.id()).to_vec();
4774        deps.sort_by_key(|n| n.0);
4775        let mut expected = vec![b.id(), c.id()];
4776        expected.sort_by_key(|n| n.0);
4777        assert_eq!(deps, expected);
4778    }
4779
4780    #[test]
4781    fn execution_plan_matches_topo_when_no_destructive_ops() {
4782        let a = NodeHandle::from_f32(vec![1.0, 2.0], Shape::from_dims(&[2]), cpu_dev()).unwrap();
4783        let b = a
4784            .const_f32_like(vec![3.0, 4.0], Shape::from_dims(&[2]))
4785            .unwrap();
4786        let c = a.add(&b);
4787        let graph = c.graph().read().unwrap();
4788        let plan = execution_plan(&graph, &[c.id()]);
4789        let topo = topo_order_multi(&graph, &[c.id()]);
4790        assert_eq!(plan, topo);
4791    }
4792
4793    #[test]
4794    fn execution_plan_pins_release_after_sibling_reader() {
4795        let a = NodeHandle::from_f32(vec![1.0, 2.0], Shape::from_dims(&[2]), cpu_dev()).unwrap();
4796        let b = a.relu();
4797        let r = a.release();
4798        let plan = execution_plan(&a.graph().read().unwrap(), &[b.id(), r.id()]);
4799        let b_pos = plan.iter().position(|&n| n == b.id()).unwrap();
4800        let r_pos = plan.iter().position(|&n| n == r.id()).unwrap();
4801        assert!(
4802            b_pos < r_pos,
4803            "expected relu@{b_pos} to precede release@{r_pos}: {plan:?}"
4804        );
4805    }
4806
4807    #[test]
4808    fn execution_plan_handles_chain_of_destructive_ops() {
4809        // a -> relu -> b
4810        // a -> neg -> c
4811        // a -> release
4812        // b and c must come before release.
4813        let a = NodeHandle::from_f32(vec![1.0, 2.0], Shape::from_dims(&[2]), cpu_dev()).unwrap();
4814        let b = a.relu();
4815        let c = a.neg();
4816        let r = a.release();
4817        let plan = execution_plan(&a.graph().read().unwrap(), &[b.id(), c.id(), r.id()]);
4818        let b_pos = plan.iter().position(|&n| n == b.id()).unwrap();
4819        let c_pos = plan.iter().position(|&n| n == c.id()).unwrap();
4820        let r_pos = plan.iter().position(|&n| n == r.id()).unwrap();
4821        assert!(
4822            b_pos < r_pos,
4823            "relu@{b_pos} must precede release@{r_pos}: {plan:?}"
4824        );
4825        assert!(
4826            c_pos < r_pos,
4827            "neg@{c_pos} must precede release@{r_pos}: {plan:?}"
4828        );
4829    }
4830
4831    #[test]
4832    fn insert_evict_reload_creates_expected_chain() {
4833        // Graph:
4834        //   a         (producer, device=Cpu default)
4835        //   b = relu(a)   (pre-gap consumer — stays wired to a)
4836        //   c = neg(a)    (post-gap consumer — should be rewired to reload)
4837        // After insert_evict_reload(a, Cpu, &[c]):
4838        //   - 2 new nodes (move, reload) appended
4839        //   - c's input list now has `reload_id` instead of `a.id()`
4840        //   - b's input list STILL has `a.id()` (unchanged)
4841        let a =
4842            NodeHandle::from_f32(vec![1.0_f32, 2.0], Shape::from_dims(&[2]), cpu_dev()).unwrap();
4843        let b = a.relu();
4844        let c = a.neg();
4845
4846        let (move_id, reload_id) =
4847            insert_evict_reload(a.graph(), a.id(), DeviceLocation::Cpu, &[c.id()]);
4848
4849        let g = a.graph().read().unwrap();
4850        // mv is an Op::Move{Cpu} reading a — the fused stage-to-host +
4851        // destructive release of a's device storage.
4852        match &g.node(move_id).op {
4853            Op::Move {
4854                target: DeviceLocation::Cpu,
4855            } => {}
4856            other => panic!("expected Op::Move{{Cpu}}, got {other:?}"),
4857        }
4858        assert_eq!(g.node(move_id).inputs, vec![a.id()]);
4859        assert_eq!(
4860            g.node(move_id).op.destructive_input(),
4861            Some(0),
4862            "Move must destroy its source so the device storage frees",
4863        );
4864        // reload is an Op::Copy{Cpu} (src_device passed in) reading mv
4865        assert!(matches!(g.node(reload_id).op, Op::Copy { .. }));
4866        assert_eq!(g.node(reload_id).inputs, vec![move_id]);
4867        // c's input was rewired
4868        assert_eq!(
4869            g.node(c.id()).inputs,
4870            vec![reload_id],
4871            "post-gap consumer should read from reload, not candidate directly"
4872        );
4873        // b's input stays
4874        assert_eq!(
4875            g.node(b.id()).inputs,
4876            vec![a.id()],
4877            "pre-gap consumer should still read candidate directly"
4878        );
4879    }
4880
4881    #[test]
4882    fn execution_plan_respects_transitive_data_deps_after_release() {
4883        // Graph:
4884        //   a
4885        //   b = relu(a)
4886        //   r = release(a)  (destructive; runs after b)
4887        //   sum = sum_all(b) (data-dependent on b, not on r)
4888        // Plan must have: b before r, b before sum; b before both is enough.
4889        let a = NodeHandle::from_f32(vec![1.0, 2.0], Shape::from_dims(&[2]), cpu_dev()).unwrap();
4890        let b = a.relu();
4891        let sum = b.sum_all();
4892        let r = a.release();
4893        let plan = execution_plan(&a.graph().read().unwrap(), &[sum.id(), r.id()]);
4894        let b_pos = plan.iter().position(|&n| n == b.id()).unwrap();
4895        let sum_pos = plan.iter().position(|&n| n == sum.id()).unwrap();
4896        let r_pos = plan.iter().position(|&n| n == r.id()).unwrap();
4897        assert!(b_pos < sum_pos);
4898        assert!(b_pos < r_pos);
4899    }
4900
4901    #[test]
4902    fn fuse_linear_collapses_matmul_plus_rank1_bias() {
4903        // Build [batch=1, m=2, k=3] @ [k=3, n=4] + bias[4].
4904        let a = crate::NodeHandle::from_f32(
4905            vec![1.0_f32, 2.0, 3.0, 4.0, 5.0, 6.0],
4906            crate::Shape::from_dims(&[2, 3]),
4907            cpu_dev(),
4908        )
4909        .unwrap();
4910        let b = a
4911            .const_f32_like(
4912                (0..12).map(|i| (i as f32) * 0.1).collect::<Vec<f32>>(),
4913                crate::Shape::from_dims(&[3, 4]),
4914            )
4915            .unwrap();
4916        let bias = a.const_f32_like(
4917            vec![0.5_f32, -0.5, 1.0, -1.0],
4918            crate::Shape::from_dims(&[4]),
4919        );
4920        let mm = a.matmul(&b);
4921        let bias_b = bias.unwrap().broadcast_to(crate::Shape::from_dims(&[2, 4]));
4922        let out = mm.add(&bias_b);
4923        // Note: real users would call broadcast_to first, then Add.
4924        // The fusion pass looks for `Add(MatMul, Const-shape-1-N)`
4925        // so we need to update it to also recognize the
4926        // BroadcastTo-then-Add pattern.
4927        let n_fused = fuse_linear(out.graph(), &[out.id()]);
4928        assert_eq!(n_fused, 1, "exactly one MatMul→Add(bias[N]) should fuse");
4929
4930        // The fused node should now be reachable as the canonical
4931        // root after remap. Its op is FusedLinear with three inputs.
4932        let g = out.graph().read().unwrap();
4933        // Walk consumers of the original Add: any leftover Add should
4934        // be unreferenced; the new FusedLinear should be present.
4935        // Phase 7.6 step 4: emission is the registry-extended shape.
4936        let any_fused = g.nodes.iter().any(|n| {
4937            matches!(
4938                n.op,
4939                Op::Fused(fid, _) if fid == crate::registry::FusedOps::FUSED_LINEAR,
4940            )
4941        });
4942        assert!(
4943            any_fused,
4944            "graph should contain an Op::Fused(FUSED_LINEAR) node"
4945        );
4946    }
4947
4948    #[test]
4949    fn fuse_linear_skips_when_matmul_has_other_consumers() {
4950        // If the matmul is consumed by both Add and something else,
4951        // fusing would duplicate the matmul. Pass should skip.
4952        let a = crate::NodeHandle::from_f32(
4953            vec![1.0_f32; 6],
4954            crate::Shape::from_dims(&[2, 3]),
4955            cpu_dev(),
4956        )
4957        .unwrap();
4958        let b = a
4959            .const_f32_like(vec![1.0_f32; 12], crate::Shape::from_dims(&[3, 4]))
4960            .unwrap();
4961        let bias = a
4962            .const_f32_like(vec![1.0_f32; 4], crate::Shape::from_dims(&[4]))
4963            .unwrap();
4964        let mm = a.matmul(&b);
4965        let bias_b = bias.broadcast_to(crate::Shape::from_dims(&[2, 4]));
4966        let with_bias = mm.add(&bias_b);
4967        let also_used = mm.relu(); // second consumer of mm
4968        // Both with_bias and also_used are roots.
4969        let n_fused = fuse_linear(a.graph(), &[with_bias.id(), also_used.id()]);
4970        assert_eq!(
4971            n_fused, 0,
4972            "MatMul has 2 consumers — fusion would duplicate work"
4973        );
4974    }
4975
4976    // ---- Rule registry: SoftmaxLastDim lower / fuse / round-trip ------------
4977
4978    fn count_op_in_reachable<F: Fn(&Op) -> bool>(
4979        graph: &SharedGraph,
4980        roots: &[NodeId],
4981        pred: F,
4982    ) -> usize {
4983        let g = graph.read().unwrap();
4984        topo_order_multi(&g, roots)
4985            .into_iter()
4986            .filter(|id| pred(&g.node(*id).op))
4987            .count()
4988    }
4989
4990    /// The lower rule, applied to a graph with one fused softmax,
4991    /// produces the canonical primitive subgraph reachable from the
4992    /// rewritten root, with auto-populated layout entries on the
4993    /// inserted BroadcastTo nodes.
4994    ///
4995    /// T5 (Increment C slice 1): the lowered spelling is the RECIPE's
4996    /// 9-node shrink-via-swap form — `MaxDim`/`SumDim` at the last axis +
4997    /// `Unsqueeze` append restoring keepdim, with the `e = Exp(..)`
4998    /// interior SHARED (identity-share) between the denominator reduce and
4999    /// the final Div — replacing the legacy 7-node
5000    /// `ReduceMaxTo`/`ReduceSumTo` form.
5001    #[test]
5002    fn softmax_last_dim_lower_rule_produces_canonical_subgraph() {
5003        let x = NodeHandle::from_f32(vec![1.0_f32; 12], Shape::from_dims(&[2, 3, 2]), cpu_dev())
5004            .unwrap();
5005        let sm = x.softmax_last_dim();
5006        let graph = sm.graph().clone();
5007
5008        let registry = RuleRegistry::lowering_only();
5009        let new_roots = registry.optimize_to_fixpoint(&graph, &[sm.id()]);
5010        assert_eq!(new_roots.len(), 1);
5011        let new_root = new_roots[0];
5012
5013        // Reachable subgraph from the new root should contain exactly
5014        // the 9 lowered nodes (plus the `x` Const leaf — that's 10).
5015        // Op composition: 1 MaxDim, 2 Unsqueeze, 2 BroadcastTo, 1 Sub,
5016        // 1 Exp, 1 SumDim, 1 Div, 1 Const = 10 reachable.
5017        let g = graph.read().unwrap();
5018        let reachable = topo_order_multi(&g, &[new_root]);
5019        assert_eq!(
5020            reachable.len(),
5021            10,
5022            "lowered subgraph: 9 ops + 1 Const = 10 reachable nodes"
5023        );
5024        drop(g);
5025
5026        // Op-shape sanity checks.
5027        let n_reduce_max =
5028            count_op_in_reachable(&graph, &[new_root], |op| matches!(op, Op::ReduceMaxTo(_)));
5029        let n_max_dim =
5030            count_op_in_reachable(&graph, &[new_root], |op| matches!(op, Op::MaxDim(_)));
5031        let n_sum_dim =
5032            count_op_in_reachable(&graph, &[new_root], |op| matches!(op, Op::SumDim(_)));
5033        let n_unsqueeze =
5034            count_op_in_reachable(&graph, &[new_root], |op| matches!(op, Op::Unsqueeze { .. }));
5035        let n_reshape =
5036            count_op_in_reachable(&graph, &[new_root], |op| matches!(op, Op::Reshape(_)));
5037        let n_broadcast =
5038            count_op_in_reachable(&graph, &[new_root], |op| matches!(op, Op::BroadcastTo(_)));
5039        let n_sub = count_op_in_reachable(&graph, &[new_root], |op| matches!(op, Op::Sub));
5040        let n_exp = count_op_in_reachable(&graph, &[new_root], |op| matches!(op, Op::Exp));
5041        let n_reduce_sum =
5042            count_op_in_reachable(&graph, &[new_root], |op| matches!(op, Op::ReduceSumTo(_)));
5043        let n_div = count_op_in_reachable(&graph, &[new_root], |op| matches!(op, Op::Div));
5044        // Phase 7.6 step 5: post-migration the builder emits
5045        // `Op::Fused(SOFTMAX_LAST_DIM, _)` only — the legacy
5046        // `Op::SoftmaxLastDim` variant was retired.
5047        let n_softmax = count_op_in_reachable(
5048            &graph,
5049            &[new_root],
5050            |op| matches!(op, Op::Fused(fid, _) if *fid == FusedOps::SOFTMAX_LAST_DIM),
5051        );
5052        assert_eq!(
5053            n_max_dim, 1,
5054            "T5 max side = MaxDim(last) + Unsqueeze append"
5055        );
5056        assert_eq!(
5057            n_sum_dim, 1,
5058            "T5 sum side = SumDim(last) + Unsqueeze append"
5059        );
5060        assert_eq!(n_unsqueeze, 2, "one keepdim-restore per reduce");
5061        assert_eq!(
5062            n_reduce_max, 0,
5063            "the legacy ReduceMaxTo spelling is retired from the lowering"
5064        );
5065        assert_eq!(
5066            n_reduce_sum, 0,
5067            "the legacy ReduceSumTo spelling is retired from the lowering"
5068        );
5069        assert_eq!(n_reshape, 0, "keepdim restores via Unsqueeze, not Reshape");
5070        assert_eq!(n_broadcast, 2);
5071        assert_eq!(n_sub, 1);
5072        assert_eq!(
5073            n_exp, 1,
5074            "the shared `e` interior emits ONCE (identity-share), not per consumer"
5075        );
5076        assert_eq!(n_div, 1);
5077        assert_eq!(
5078            n_softmax, 0,
5079            "no SoftmaxLastDim/Fused(SOFTMAX_LAST_DIM) should remain reachable post-lowering"
5080        );
5081
5082        // Layout side-table entries on the inserted BroadcastTo nodes
5083        // must be populated and have the expected broadcast strides.
5084        let g = graph.read().unwrap();
5085        for id in topo_order_multi(&g, &[new_root]) {
5086            if let Op::BroadcastTo(_) = g.node(id).op {
5087                assert!(
5088                    g.has_explicit_layout(id),
5089                    "lowered BroadcastTo at {id:?} missing explicit Layout entry — \
5090                     Graph::push auto-populate should have set it",
5091                );
5092                // The inserted BroadcastTo broadcasts shape [2,3,1] -> [2,3,2]
5093                // with last-dim stride 0.
5094                let l = g.layout(id);
5095                assert_eq!(l.shape().dims(), &[2, 3, 2]);
5096                assert_eq!(
5097                    l.stride().last().copied(),
5098                    Some(0),
5099                    "BroadcastTo from [2,3,1] to [2,3,2] should have stride 0 on last dim"
5100                );
5101            }
5102        }
5103    }
5104
5105    /// The fuse rule, applied to the canonical 7-node lowered subgraph,
5106    /// collapses it back to a single `Op::SoftmaxLastDim` reachable
5107    /// from the rewritten root.
5108    #[test]
5109    fn softmax_last_dim_fuse_rule_collapses_canonical_subgraph() {
5110        // Build the canonical subgraph by-hand — start from a Const
5111        // and apply the same op sequence the lower rule would emit.
5112        let x =
5113            NodeHandle::from_f32(vec![1.0_f32; 6], Shape::from_dims(&[2, 3]), cpu_dev()).unwrap();
5114        let m = x.reduce_max_to(Shape::from_dims(&[2, 1]));
5115        let mb = m.broadcast_to(Shape::from_dims(&[2, 3]));
5116        let s = x.sub(&mb);
5117        let e = s.exp();
5118        let d = e.reduce_sum_to(Shape::from_dims(&[2, 1]));
5119        let db = d.broadcast_to(Shape::from_dims(&[2, 3]));
5120        let out = e.div(&db);
5121        let graph = out.graph().clone();
5122
5123        // Phase 7.6 step 3: fuse rule comes from the registry entry.
5124        let registry = RuleRegistry::new().with_rule(Box::new(FusionRule::from_entry(
5125            &crate::registry::softmax_last_dim::entry(),
5126        )));
5127        let new_roots = registry.optimize_to_fixpoint(&graph, &[out.id()]);
5128        assert_eq!(new_roots.len(), 1);
5129        let new_root = new_roots[0];
5130
5131        // Reachable from the new root: 1 fused-softmax + 1 Const x = 2.
5132        let g = graph.read().unwrap();
5133        let reachable = topo_order_multi(&g, &[new_root]);
5134        assert_eq!(
5135            reachable.len(),
5136            2,
5137            "fused subgraph: 1 Fused(SOFTMAX_LAST_DIM) + 1 Const = 2 reachable nodes"
5138        );
5139        assert!(
5140            matches!(
5141                g.node(new_root).op,
5142                Op::Fused(fid, _) if fid == FusedOps::SOFTMAX_LAST_DIM
5143            ),
5144            "post-migration fusion produces Op::Fused(SOFTMAX_LAST_DIM, _)",
5145        );
5146        // Fused-softmax's input should be the original Const x.
5147        assert_eq!(g.node(new_root).inputs, vec![x.id()]);
5148    }
5149
5150    /// Lower then fuse to fixpoint: a graph with one `Op::SoftmaxLastDim`
5151    /// returns to a graph that's structurally identical to the input
5152    /// (modulo a re-canonicalized root NodeId — the original
5153    /// SoftmaxLastDim node remains in the arena but is unreachable
5154    /// from the rewritten root).
5155    #[test]
5156    fn softmax_last_dim_lower_then_fuse_round_trips() {
5157        let x =
5158            NodeHandle::from_f32(vec![1.0_f32; 6], Shape::from_dims(&[2, 3]), cpu_dev()).unwrap();
5159        let sm = x.softmax_last_dim();
5160        let graph = sm.graph().clone();
5161        let original_root = sm.id();
5162
5163        let registry = RuleRegistry::default_rules();
5164        let new_roots = registry.optimize_to_fixpoint(&graph, &[original_root]);
5165        assert_eq!(new_roots.len(), 1);
5166        let new_root = new_roots[0];
5167
5168        // Reachable from the new root must look like the original
5169        // graph: one fused-softmax consuming one Const. Phase 7.6
5170        // step 3: the post-migration builder + fuse cycle produces
5171        // `Op::Fused(SOFTMAX_LAST_DIM, _)`, not `Op::SoftmaxLastDim`.
5172        let g = graph.read().unwrap();
5173        let reachable = topo_order_multi(&g, &[new_root]);
5174        assert_eq!(
5175            reachable.len(),
5176            2,
5177            "round-tripped graph: 1 Fused(SOFTMAX_LAST_DIM) + 1 Const = 2 reachable nodes"
5178        );
5179        assert!(
5180            matches!(
5181                g.node(new_root).op,
5182                Op::Fused(fid, _) if fid == FusedOps::SOFTMAX_LAST_DIM
5183            ),
5184            "round-tripped node should be Op::Fused(SOFTMAX_LAST_DIM, _)",
5185        );
5186        assert_eq!(
5187            g.node(new_root).inputs,
5188            vec![x.id()],
5189            "round-tripped Fused(SOFTMAX_LAST_DIM) should consume the original input"
5190        );
5191        // Shape and dtype preserved.
5192        assert_eq!(g.node(new_root).shape, g.node(original_root).shape);
5193        assert_eq!(g.node(new_root).dtype, g.node(original_root).dtype);
5194    }
5195
5196    /// Empty registry is a no-op: roots come back unchanged.
5197    #[test]
5198    fn empty_registry_is_noop() {
5199        let x = NodeHandle::from_f32(vec![1.0_f32; 4], Shape::from_dims(&[4]), cpu_dev()).unwrap();
5200        let y = x.add(&x);
5201        let graph = y.graph().clone();
5202        let pre_len = graph.read().unwrap().len();
5203        let new_roots = RuleRegistry::new().optimize_to_fixpoint(&graph, &[y.id()]);
5204        assert_eq!(new_roots, vec![y.id()]);
5205        assert_eq!(
5206            graph.read().unwrap().len(),
5207            pre_len,
5208            "empty registry should add no nodes"
5209        );
5210    }
5211
5212    /// Lowering rule does not fire on a graph that has no
5213    /// SoftmaxLastDim nodes — the registry returns the input
5214    /// roots unchanged.
5215    #[test]
5216    fn lower_rule_does_not_fire_without_softmax() {
5217        let x = NodeHandle::from_f32(vec![1.0_f32; 4], Shape::from_dims(&[4]), cpu_dev()).unwrap();
5218        let y = x.relu();
5219        let graph = y.graph().clone();
5220        let pre_len = graph.read().unwrap().len();
5221        let new_roots = RuleRegistry::lowering_only().optimize_to_fixpoint(&graph, &[y.id()]);
5222        assert_eq!(new_roots, vec![y.id()]);
5223        assert_eq!(graph.read().unwrap().len(), pre_len);
5224    }
5225
5226    /// `lower_to_base_map` is a named wrapper over
5227    /// `RuleRegistry::lowering_only().optimize_to_fixpoint` — a fused ROPE
5228    /// node dissolves to its primitive base map; no `Op::Fused(ROPE, _)`
5229    /// remains reachable from the (possibly-remapped) roots.
5230    #[test]
5231    fn lower_to_base_map_dissolves_a_fused_op() {
5232        use std::sync::RwLock;
5233        // Build a graph: three Const leaves (x, cos, sin) + one Op::Fused(ROPE).
5234        let graph = Arc::new(RwLock::new(Graph::new()));
5235        let sink = {
5236            let mut g = graph.write().unwrap();
5237            let shape = Shape::from_dims(&[1, 4, 8]); // (batch, seq, head_dim=8, even)
5238            let x = g.push(Node {
5239                op: Op::Const,
5240                inputs: vec![],
5241                shape: shape.clone(),
5242                dtype: DType::F32,
5243            });
5244            let cshape = Shape::from_dims(&[4, 8]);
5245            let cos = g.push(Node {
5246                op: Op::Const,
5247                inputs: vec![],
5248                shape: cshape.clone(),
5249                dtype: DType::F32,
5250            });
5251            let sin = g.push(Node {
5252                op: Op::Const,
5253                inputs: vec![],
5254                shape: cshape,
5255                dtype: DType::F32,
5256            });
5257            g.push(Node {
5258                op: Op::Fused(FusedOps::ROPE, FusedOpParams::Rope),
5259                inputs: vec![x, cos, sin],
5260                shape,
5261                dtype: DType::F32,
5262            })
5263        };
5264        let roots = lower_to_base_map(&graph, &[sink]);
5265        let g = graph.read().unwrap();
5266        // No Op::Fused(ROPE) remains reachable; the base map is primitives.
5267        let reachable = topo_order_multi(&g, &roots);
5268        let has_rope = reachable
5269            .iter()
5270            .any(|&n| matches!(g.node(n).op, Op::Fused(fid, _) if fid == FusedOps::ROPE));
5271        assert!(!has_rope, "ROPE must be lowered to its primitive base map");
5272    }
5273
5274    /// `base_map_hash` canonicalizes commutative-operand order: two
5275    /// independently-built graphs computing `a + b` and `b + a` (same
5276    /// consts, swapped operand order on the `Add`) hash equal.
5277    #[test]
5278    fn base_map_hash_commutative_reorder_is_equal() {
5279        let mk = |swap: bool| {
5280            let mut g = Graph::new();
5281            let s = Shape::from_dims(&[4]);
5282            let a = g.push(Node {
5283                op: Op::Const,
5284                inputs: vec![],
5285                shape: s.clone(),
5286                dtype: DType::F32,
5287            });
5288            let b = g.push(Node {
5289                op: Op::Const,
5290                inputs: vec![],
5291                shape: s.clone(),
5292                dtype: DType::F32,
5293            });
5294            let ins = if swap { vec![b, a] } else { vec![a, b] };
5295            let sink = g.push(Node {
5296                op: Op::Add,
5297                inputs: ins,
5298                shape: s,
5299                dtype: DType::F32,
5300            });
5301            (g, sink)
5302        };
5303        let (g0, r0) = mk(false);
5304        let (g1, r1) = mk(true);
5305        assert_eq!(base_map_hash(&g0, r0), base_map_hash(&g1, r1), "a+b == b+a");
5306    }
5307
5308    /// `base_map_hash` distinguishes distinct ops over the same operands:
5309    /// `Add(a, b)` and `Mul(a, b)` must hash differently.
5310    #[test]
5311    fn base_map_hash_distinct_ops_differ() {
5312        let mk = |op: Op| {
5313            let mut g = Graph::new();
5314            let s = Shape::from_dims(&[4]);
5315            let a = g.push(Node {
5316                op: Op::Const,
5317                inputs: vec![],
5318                shape: s.clone(),
5319                dtype: DType::F32,
5320            });
5321            let b = g.push(Node {
5322                op: Op::Const,
5323                inputs: vec![],
5324                shape: s.clone(),
5325                dtype: DType::F32,
5326            });
5327            let sink = g.push(Node {
5328                op,
5329                inputs: vec![a, b],
5330                shape: s,
5331                dtype: DType::F32,
5332            });
5333            (g, sink)
5334        };
5335        let (ga, ra) = mk(Op::Add);
5336        let (gm, rm) = mk(Op::Mul);
5337        assert_ne!(base_map_hash(&ga, ra), base_map_hash(&gm, rm), "add != mul");
5338    }
5339
5340    /// `op_key` already carries explicit arms for `Op::Slice` and
5341    /// `Op::Concat` (tags 81/80) — the two ops rope's rotate-half base map
5342    /// relies on. This test forces that path: a tiny `Slice`-then-`Concat`
5343    /// fragment (the first half of rope's rotate-half idiom) hashes
5344    /// deterministically across two independently-built, structurally
5345    /// identical copies.
5346    #[test]
5347    fn base_map_hash_slice_concat_fragment_is_deterministic() {
5348        let mk = || {
5349            let mut g = Graph::new();
5350            let s = Shape::from_dims(&[4, 8]);
5351            let half = Shape::from_dims(&[4, 4]);
5352            let x = g.push(Node {
5353                op: Op::Const,
5354                inputs: vec![],
5355                shape: s.clone(),
5356                dtype: DType::F32,
5357            });
5358            let lo = g.push(Node {
5359                op: Op::Slice {
5360                    dim: 1,
5361                    start: 0,
5362                    len: 4,
5363                },
5364                inputs: vec![x],
5365                shape: half.clone(),
5366                dtype: DType::F32,
5367            });
5368            let hi = g.push(Node {
5369                op: Op::Slice {
5370                    dim: 1,
5371                    start: 4,
5372                    len: 4,
5373                },
5374                inputs: vec![x],
5375                shape: half,
5376                dtype: DType::F32,
5377            });
5378            let sink = g.push(Node {
5379                op: Op::Concat { dim: 1 },
5380                inputs: vec![hi, lo],
5381                shape: s,
5382                dtype: DType::F32,
5383            });
5384            (g, sink)
5385        };
5386        let (g0, r0) = mk();
5387        let (g1, r1) = mk();
5388        assert_eq!(
5389            base_map_hash(&g0, r0),
5390            base_map_hash(&g1, r1),
5391            "two independently-built Slice->Concat fragments must hash equal"
5392        );
5393    }
5394
5395    /// `base_map_hash` folds a `Const`'s real bytes when its storage slot
5396    /// is populated (unlike the bare `Node { op: Op::Const, .. }` pushes
5397    /// used above, `NodeHandle::from_f32` populates real storage via
5398    /// `set_storage`). Two consts with equal bytes hash equal; two consts
5399    /// of the same shape/dtype but different bytes hash differently — this
5400    /// is the behavior that distinguishes real recipe identity from the
5401    /// shape/dtype-only fallback (documented on `fold_const_bytes`).
5402    #[test]
5403    fn base_map_hash_folds_real_const_bytes() {
5404        let shape = Shape::from_dims(&[4]);
5405        let a = NodeHandle::from_f32(vec![1.0, 2.0, 3.0, 4.0], shape.clone(), cpu_dev()).unwrap();
5406        let b = NodeHandle::from_f32(vec![1.0, 2.0, 3.0, 4.0], shape.clone(), cpu_dev()).unwrap();
5407        let c = NodeHandle::from_f32(vec![9.0, 9.0, 9.0, 9.0], shape, cpu_dev()).unwrap();
5408        let ga = a.graph().read().unwrap();
5409        let gb = b.graph().read().unwrap();
5410        let gc = c.graph().read().unwrap();
5411        assert_eq!(
5412            base_map_hash(&ga, a.id()),
5413            base_map_hash(&gb, b.id()),
5414            "identical const bytes must hash equal across independently-built graphs"
5415        );
5416        assert_ne!(
5417            base_map_hash(&ga, a.id()),
5418            base_map_hash(&gc, c.id()),
5419            "different const bytes (same shape/dtype) must hash differently"
5420        );
5421    }
5422
5423    #[test]
5424    fn scan_base_map_hash_differs_for_different_bodies() {
5425        use crate::{Graph, Node, Op, ScanEmit, ScanRole};
5426        use fuel_ir::{DType, Shape};
5427        // Two scans, IDENTICAL carry shape / n_xs / bound / emit, but STRUCTURALLY
5428        // DIFFERENT bodies: body A = carry*2 (MulScalar), body B = carry+1
5429        // (AddScalar). Without a dedicated op_key arm they fall to the None branch
5430        // (discriminant+shape+dtype only) and collide -> silent CSE / recipe-identity
5431        // corruption. THIS red run is the evidence the trap is real.
5432        let mk = |mul: bool| {
5433            let mut g = Graph::new();
5434            let s = Shape::from_dims(&[1]);
5435            let carry = g.push(Node {
5436                op: Op::Const,
5437                inputs: vec![],
5438                shape: s.clone(),
5439                dtype: DType::F32,
5440            });
5441            let hole = g.push(Node {
5442                op: Op::ScanPlaceholder {
5443                    role: ScanRole::Carry,
5444                    index: 0,
5445                },
5446                inputs: vec![],
5447                shape: s.clone(),
5448                dtype: DType::F32,
5449            });
5450            let new_carry = g.push(Node {
5451                op: if mul {
5452                    Op::MulScalar(2.0)
5453                } else {
5454                    Op::AddScalar(1.0)
5455                },
5456                inputs: vec![hole],
5457                shape: s.clone(),
5458                dtype: DType::F32,
5459            });
5460            // inputs = [init_carry, body_new_carry, body_y]  (n_xs = 0, no consts)
5461            let scan = g.push(Node {
5462                op: Op::Scan {
5463                    n_carries: 1,
5464                    n_xs: 0,
5465                    bound: 3,
5466                    emit: ScanEmit::All,
5467                    early_exit: None,
5468                },
5469                inputs: vec![carry, new_carry, new_carry],
5470                shape: s,
5471                dtype: DType::F32,
5472            });
5473            (g, scan)
5474        };
5475        let (ga, ra) = mk(true);
5476        let (gb, rb) = mk(false);
5477        assert_ne!(
5478            base_map_hash(&ga, ra),
5479            base_map_hash(&gb, rb),
5480            "scans with structurally different bodies must hash differently",
5481        );
5482    }
5483
5484    #[test]
5485    fn scan_base_map_hash_equal_for_same_body_cross_graph() {
5486        use crate::{Graph, Node, Op, ScanEmit, ScanRole};
5487        use fuel_ir::{DType, Shape};
5488        // Same body built independently in two graphs -> equal hash (the
5489        // recipe-identity positive case). base_map_hash is NodeId-independent.
5490        let mk = || {
5491            let mut g = Graph::new();
5492            let s = Shape::from_dims(&[1]);
5493            let carry = g.push(Node {
5494                op: Op::Const,
5495                inputs: vec![],
5496                shape: s.clone(),
5497                dtype: DType::F32,
5498            });
5499            let hole = g.push(Node {
5500                op: Op::ScanPlaceholder {
5501                    role: ScanRole::Carry,
5502                    index: 0,
5503                },
5504                inputs: vec![],
5505                shape: s.clone(),
5506                dtype: DType::F32,
5507            });
5508            let new_carry = g.push(Node {
5509                op: Op::MulScalar(2.0),
5510                inputs: vec![hole],
5511                shape: s.clone(),
5512                dtype: DType::F32,
5513            });
5514            let scan = g.push(Node {
5515                op: Op::Scan {
5516                    n_carries: 1,
5517                    n_xs: 0,
5518                    bound: 3,
5519                    emit: ScanEmit::All,
5520                    early_exit: None,
5521                },
5522                inputs: vec![carry, new_carry, new_carry],
5523                shape: s,
5524                dtype: DType::F32,
5525            });
5526            (g, scan)
5527        };
5528        let (g0, r0) = mk();
5529        let (g1, r1) = mk();
5530        assert_eq!(
5531            base_map_hash(&g0, r0),
5532            base_map_hash(&g1, r1),
5533            "same body -> equal hash"
5534        );
5535    }
5536
5537    /// GAP-303 DISCRIMINATING FIXTURE for the `n_carries` fold in `op_key`.
5538    ///
5539    /// ⚠️ WITHOUT THIS TEST THE FIX IS INVISIBLE IN ITS OWN METRIC. Every other
5540    /// scan test in this workspace uses ONE carry, and `NodeHandle::scan` pins
5541    /// `n_carries: 1`, so folding the field or not folding it produces identical
5542    /// hashes across the entire suite. Ask "if this became a no-op, which number
5543    /// moves?" — before this test, none.
5544    ///
5545    /// What it protects: `op_key` is Fuel's own recipe identity. Two scans that
5546    /// differ ONLY in carry count must not key the same, or cover-finding and the
5547    /// plan cache treat a 1-carry and a 3-carry scan as one recipe. Same defect
5548    /// class as KISS-OPS-6.19, where an arm reading too few attrs made
5549    /// Gather-on-axis-2 and Gather-on-axis-0 emit IDENTICAL bytes.
5550    #[test]
5551    fn gap303_n_carries_participates_in_op_key() {
5552        use crate::{Op, ScanEmit, ScanRole};
5553        // Every other field held EQUAL — n_carries is the only axis that moves.
5554        let mk = |n_carries: usize| {
5555            op_key(&Op::Scan {
5556                n_carries,
5557                n_xs: 2,
5558                bound: 3,
5559                emit: ScanEmit::All,
5560                early_exit: None,
5561            })
5562        };
5563        let one = mk(1);
5564        assert!(one.is_some(), "Op::Scan must produce an op_key, not None");
5565        for n in [2usize, 3, 7] {
5566            assert_ne!(
5567                mk(n),
5568                one,
5569                "n_carries={n} must not key identically to n_carries=1                  (all other fields held equal)"
5570            );
5571        }
5572        // ...and distinct counts stay distinct from each OTHER, not merely from 1.
5573        assert_ne!(mk(2), mk(3), "distinct carry counts must key distinctly");
5574
5575        // The SIBLING claim, asserted rather than only commented: the placeholder
5576        // arm's own doc says "Carry/0, Elem/0, Carry/1 are all distinct in the
5577        // body hash". It had no test. It does now.
5578        let c0 = op_key(&Op::ScanPlaceholder {
5579            role: ScanRole::Carry,
5580            index: 0,
5581        });
5582        let c1 = op_key(&Op::ScanPlaceholder {
5583            role: ScanRole::Carry,
5584            index: 1,
5585        });
5586        let e0 = op_key(&Op::ScanPlaceholder {
5587            role: ScanRole::Elem,
5588            index: 0,
5589        });
5590        assert!(c0.is_some(), "ScanPlaceholder must produce an op_key");
5591        assert_ne!(c0, c1, "Carry/0 vs Carry/1 must differ in the body hash");
5592        assert_ne!(c0, e0, "Carry/0 vs Elem/0 must differ in the body hash");
5593    }
5594
5595    #[test]
5596    fn scan_emit_and_placeholder_role_participate_in_op_key() {
5597        use crate::{Op, ScanEmit, ScanRole};
5598        // emit=All vs emit=Final differ.
5599        let all = op_key(&Op::Scan {
5600            n_carries: 1,
5601            n_xs: 0,
5602            bound: 3,
5603            emit: ScanEmit::All,
5604            early_exit: None,
5605        });
5606        let fin = op_key(&Op::Scan {
5607            n_carries: 1,
5608            n_xs: 0,
5609            bound: 3,
5610            emit: ScanEmit::Final,
5611            early_exit: None,
5612        });
5613        assert!(
5614            all.is_some() && fin.is_some(),
5615            "Op::Scan must produce an op_key, not None"
5616        );
5617        assert_ne!(all, fin, "emit=All vs emit=Final must differ in op_key");
5618        // n_xs / bound participate.
5619        assert_ne!(
5620            op_key(&Op::Scan {
5621                n_carries: 1,
5622                n_xs: 1,
5623                bound: 3,
5624                emit: ScanEmit::All,
5625                early_exit: None
5626            }),
5627            all
5628        );
5629        assert_ne!(
5630            op_key(&Op::Scan {
5631                n_carries: 1,
5632                n_xs: 0,
5633                bound: 4,
5634                emit: ScanEmit::All,
5635                early_exit: None
5636            }),
5637            all
5638        );
5639        // ScanPlaceholder Carry/0 vs Elem/0 vs Carry/1 all differ.
5640        let c0 = op_key(&Op::ScanPlaceholder {
5641            role: ScanRole::Carry,
5642            index: 0,
5643        });
5644        let e0 = op_key(&Op::ScanPlaceholder {
5645            role: ScanRole::Elem,
5646            index: 0,
5647        });
5648        let c1 = op_key(&Op::ScanPlaceholder {
5649            role: ScanRole::Carry,
5650            index: 1,
5651        });
5652        assert!(c0.is_some());
5653        assert_ne!(c0, e0, "Carry vs Elem must differ");
5654        assert_ne!(c0, c1, "index must participate");
5655    }
5656
5657    /// G2 (2026-06-20): FlashAttn IS decomposable — the vanilla (non-causal,
5658    /// MHA) case lowers to its materialized scaled-dot-product-attention
5659    /// subgraph. Born-red before the fix (`flash_attn::decompose` panicked).
5660    #[test]
5661    fn flash_attn_vanilla_decomposes_to_sdpa() {
5662        let s = Shape::from_dims(&[1, 1, 1, 1]);
5663        let q = NodeHandle::from_f32(vec![0.0_f32; 1], s.clone(), cpu_dev()).unwrap();
5664        let k = q.const_f32_like(vec![0.0_f32; 1], s.clone()).unwrap();
5665        let v = q.const_f32_like(vec![0.0_f32; 1], s.clone()).unwrap();
5666        // Vanilla: non-causal, Hq==Hkv==1, no alibi/window/softcap.
5667        let attn = q.flash_attn(&k, &v, None, 1.0_f32, false, None, None, None);
5668        let graph = attn.graph().clone();
5669
5670        let new_roots = RuleRegistry::lowering_only().optimize_to_fixpoint(&graph, &[attn.id()]);
5671        assert_eq!(new_roots.len(), 1);
5672        let root = new_roots[0];
5673        let g = graph.read().unwrap();
5674        // The fused FlashAttn is gone from the root, replaced by the SDPA
5675        // subgraph — which contains MatMul nodes.
5676        assert!(
5677            !matches!(g.node(root).op, Op::Fused(fid, _) if fid == crate::registry::FusedOps::FLASH_ATTN),
5678            "vanilla FlashAttn must decompose, not stay fused",
5679        );
5680        let has_matmul = (0..g.len()).any(|i| matches!(g.node(NodeId(i)).op, Op::MatMul));
5681        assert!(has_matmul, "decomposed FlashAttn must contain a MatMul");
5682    }
5683
5684    /// G2: causal FlashAttn decomposes too — the causal mask is an additive
5685    /// `-inf` `Triu` band (no new primitive needed), so the node lowers
5686    /// rather than staying fused. (The decomposed *numerics* are verified
5687    /// separately by a parity test vs. the reference attention.)
5688    #[test]
5689    fn flash_attn_causal_decomposes_with_triu_mask() {
5690        let s = Shape::from_dims(&[1, 1, 2, 1]); // Sq=Sk=2 so the mask is non-trivial
5691        let q = NodeHandle::from_f32(vec![0.0_f32; 2], s.clone(), cpu_dev()).unwrap();
5692        let k = q.const_f32_like(vec![0.0_f32; 2], s.clone()).unwrap();
5693        let v = q.const_f32_like(vec![0.0_f32; 2], s.clone()).unwrap();
5694        let attn = q.flash_attn(&k, &v, None, 1.0_f32, true, None, None, None); // causal
5695        let graph = attn.graph().clone();
5696
5697        let new_roots = RuleRegistry::lowering_only().optimize_to_fixpoint(&graph, &[attn.id()]);
5698        assert_eq!(new_roots.len(), 1);
5699        let root = new_roots[0];
5700        let g = graph.read().unwrap();
5701        assert!(
5702            !matches!(g.node(root).op, Op::Fused(fid, _) if fid == crate::registry::FusedOps::FLASH_ATTN),
5703            "causal FlashAttn must decompose (Triu mask), not stay fused",
5704        );
5705        let has_triu = (0..g.len()).any(|i| matches!(g.node(NodeId(i)).op, Op::Triu { .. }));
5706        assert!(
5707            has_triu,
5708            "causal decomposition must contain a Triu mask band"
5709        );
5710    }
5711
5712    /// Fuse rule does not fire on a non-canonical Div pattern (e.g.,
5713    /// a plain `a / b` with no upstream softmax structure).
5714    #[test]
5715    fn fuse_rule_does_not_fire_on_plain_div() {
5716        let a = NodeHandle::from_f32(vec![1.0_f32; 4], Shape::from_dims(&[4]), cpu_dev()).unwrap();
5717        let b = a
5718            .const_f32_like(vec![2.0_f32; 4], Shape::from_dims(&[4]))
5719            .unwrap();
5720        let c = a.div(&b);
5721        let graph = c.graph().clone();
5722        let pre_len = graph.read().unwrap().len();
5723        let new_roots = RuleRegistry::new()
5724            .with_rule(Box::new(FusionRule::from_entry(
5725                &crate::registry::softmax_last_dim::entry(),
5726            )))
5727            .optimize_to_fixpoint(&graph, &[c.id()]);
5728        assert_eq!(new_roots, vec![c.id()]);
5729        assert_eq!(graph.read().unwrap().len(), pre_len);
5730    }
5731
5732    // -------- Cast-fusion rule -------------------------------------------
5733
5734    /// Build a "yes to everything" capability predicate. Useful when
5735    /// the test wants to confirm the matcher/rewriter mechanics work;
5736    /// orthogonal tests cover the predicate-says-no path.
5737    fn allow_all_predicate() -> CapabilityPredicate {
5738        Arc::new(|_op: &Op, _dtypes: &[fuel_ir::DType]| true)
5739    }
5740
5741    /// Predicate that says no for everything. Useful for confirming
5742    /// the rule respects the capability gate.
5743    fn deny_all_predicate() -> CapabilityPredicate {
5744        Arc::new(|_op: &Op, _dtypes: &[fuel_ir::DType]| false)
5745    }
5746
5747    /// Happy path: `x:f32 → Cast(BF16) → Neg(BF16)` collapses to
5748    /// `x:f32 → Neg(f32)` when the predicate says Neg supports f32.
5749    #[test]
5750    fn cast_fusion_drops_cast_when_consumer_supports_source_dtype() {
5751        let x = NodeHandle::from_f32(vec![1.0_f32; 4], Shape::from_dims(&[4]), cpu_dev()).unwrap();
5752        let xc = x.cast(fuel_ir::DType::BF16);
5753        let y = xc.neg();
5754        let graph = y.graph().clone();
5755        let pre_len = graph.read().unwrap().len();
5756
5757        let registry =
5758            RuleRegistry::new().with_rule(Box::new(CastFusionRule::new(allow_all_predicate())));
5759        let new_roots = registry.optimize_to_fixpoint(&graph, &[y.id()]);
5760        assert_eq!(new_roots.len(), 1);
5761        let new_root = new_roots[0];
5762
5763        let g = graph.read().unwrap();
5764        // The new root is a fresh Neg whose input is the original
5765        // f32 Const x, not the Cast.
5766        assert!(matches!(g.node(new_root).op, Op::Neg));
5767        assert_eq!(g.node(new_root).inputs, vec![x.id()]);
5768        // Cast node remains in the arena but is no longer reachable
5769        // from the new root.
5770        let reachable = topo_order_multi(&g, &[new_root]);
5771        assert!(
5772            !reachable
5773                .iter()
5774                .any(|&n| matches!(g.node(n).op, Op::Cast(_))),
5775            "no Cast node should be reachable post-fusion"
5776        );
5777        // One new Neg node was appended; the original Neg + Cast are
5778        // unreachable but still in the arena.
5779        assert!(g.len() > pre_len);
5780    }
5781
5782    /// Capability gate: when the predicate says no, the rule must
5783    /// not fire. Graph structure is unchanged.
5784    #[test]
5785    fn cast_fusion_no_fire_when_predicate_denies() {
5786        let x = NodeHandle::from_f32(vec![1.0_f32; 4], Shape::from_dims(&[4]), cpu_dev()).unwrap();
5787        let xc = x.cast(fuel_ir::DType::BF16);
5788        let y = xc.neg();
5789        let graph = y.graph().clone();
5790        let pre_len = graph.read().unwrap().len();
5791
5792        let registry =
5793            RuleRegistry::new().with_rule(Box::new(CastFusionRule::new(deny_all_predicate())));
5794        let new_roots = registry.optimize_to_fixpoint(&graph, &[y.id()]);
5795        assert_eq!(new_roots, vec![y.id()]);
5796        assert_eq!(
5797            graph.read().unwrap().len(),
5798            pre_len,
5799            "predicate-says-no should append no nodes"
5800        );
5801        // The original Cast → Neg chain is intact.
5802        let g = graph.read().unwrap();
5803        assert!(matches!(g.node(y.id()).op, Op::Neg));
5804        assert_eq!(g.node(y.id()).inputs, vec![xc.id()]);
5805        assert!(matches!(g.node(xc.id()).op, Op::Cast(_)));
5806    }
5807
5808    /// Single-consumer constraint: if the Cast feeds two consumers,
5809    /// the rule must not fire (eliminating the cast for one consumer
5810    /// would orphan it for the other).
5811    #[test]
5812    fn cast_fusion_no_fire_when_cast_has_multiple_consumers() {
5813        let x = NodeHandle::from_f32(vec![1.0_f32; 4], Shape::from_dims(&[4]), cpu_dev()).unwrap();
5814        let xc = x.cast(fuel_ir::DType::BF16);
5815        // Two consumers of the same Cast.
5816        let y1 = xc.neg();
5817        let y2 = xc.relu();
5818        let graph = y1.graph().clone();
5819        let pre_len = graph.read().unwrap().len();
5820
5821        let registry =
5822            RuleRegistry::new().with_rule(Box::new(CastFusionRule::new(allow_all_predicate())));
5823        let new_roots = registry.optimize_to_fixpoint(&graph, &[y1.id(), y2.id()]);
5824        // Roots unchanged; cast survives because rule didn't fire.
5825        assert_eq!(new_roots, vec![y1.id(), y2.id()]);
5826        assert_eq!(graph.read().unwrap().len(), pre_len);
5827        let g = graph.read().unwrap();
5828        assert!(matches!(g.node(xc.id()).op, Op::Cast(_)));
5829    }
5830
5831    /// Output dtype tracks the new input: in aggressive mode the
5832    /// rewritten consumer's output dtype is the cast-source dtype,
5833    /// not the original consumer's output dtype. Downstream
5834    /// consumers reading from the rewritten node will see the new
5835    /// dtype — by design (see module docstring's
5836    /// aggressive-semantics caveat).
5837    #[test]
5838    fn cast_fusion_rewrites_output_dtype_to_source_dtype() {
5839        let x = NodeHandle::from_f32(vec![1.0_f32; 4], Shape::from_dims(&[4]), cpu_dev()).unwrap();
5840        let xc = x.cast(fuel_ir::DType::BF16);
5841        let y = xc.neg();
5842        // Pre-fusion the original Neg produces BF16.
5843        assert_eq!(y.dtype(), fuel_ir::DType::BF16);
5844
5845        let graph = y.graph().clone();
5846        let registry =
5847            RuleRegistry::new().with_rule(Box::new(CastFusionRule::new(allow_all_predicate())));
5848        let new_roots = registry.optimize_to_fixpoint(&graph, &[y.id()]);
5849        let new_root = new_roots[0];
5850
5851        let g = graph.read().unwrap();
5852        // The rewritten Neg consumes the F32 source directly and
5853        // produces F32. This differs from the original BF16
5854        // output; the rule's aggressive semantics commit to this.
5855        assert_eq!(g.node(new_root).dtype, fuel_ir::DType::F32);
5856    }
5857
5858    /// Multi-input op: only the qualifying input fuses; the others
5859    /// are left alone.
5860    #[test]
5861    fn cast_fusion_multi_input_op_fuses_only_qualifying_edge() {
5862        let a = NodeHandle::from_f32(vec![1.0_f32; 4], Shape::from_dims(&[4]), cpu_dev()).unwrap();
5863        // a:f32 → Cast(BF16) → Add(bf16, b:bf16) where b is already bf16.
5864        let ac = a.cast(fuel_ir::DType::BF16);
5865        let b = a
5866            .const_bf16_like(vec![half::bf16::from_f32(2.0); 4], Shape::from_dims(&[4]))
5867            .unwrap();
5868        let sum = ac.add(&b);
5869        let graph = sum.graph().clone();
5870
5871        // Build a predicate that says yes only for Add[f32, bf16, bf16].
5872        // (Realistic kernels typically wouldn't have mixed-dtype Add;
5873        // this isolates the matcher's per-input behavior. If the
5874        // predicate said no, the rule wouldn't fire and `b` would
5875        // stay paired with the Cast.)
5876        let predicate: CapabilityPredicate =
5877            Arc::new(|op: &Op, _dtypes: &[fuel_ir::DType]| matches!(op, Op::Add));
5878
5879        let registry = RuleRegistry::new().with_rule(Box::new(CastFusionRule::new(predicate)));
5880        let new_roots = registry.optimize_to_fixpoint(&graph, &[sum.id()]);
5881        let new_root = new_roots[0];
5882
5883        let g = graph.read().unwrap();
5884        assert!(matches!(g.node(new_root).op, Op::Add));
5885        // First input of new Add: the original f32 const (cast eliminated).
5886        // Second input: still `b` (a bf16 const, no cast was there).
5887        assert_eq!(g.node(new_root).inputs, vec![a.id(), b.id()]);
5888    }
5889
5890    /// Chained casts: `x:f32 → Cast(BF16) → Cast(F32) → Op` should
5891    /// reduce to `x:f32 → Op` via the fixpoint loop firing the rule
5892    /// twice (once per consumer-side cast).
5893    #[test]
5894    fn cast_fusion_handles_chained_casts_via_fixpoint() {
5895        let x = NodeHandle::from_f32(vec![1.0_f32; 4], Shape::from_dims(&[4]), cpu_dev()).unwrap();
5896        let x_bf16 = x.cast(fuel_ir::DType::BF16);
5897        let x_f32 = x_bf16.cast(fuel_ir::DType::F32);
5898        let y = x_f32.neg();
5899        let graph = y.graph().clone();
5900
5901        let registry =
5902            RuleRegistry::new().with_rule(Box::new(CastFusionRule::new(allow_all_predicate())));
5903        let new_roots = registry.optimize_to_fixpoint(&graph, &[y.id()]);
5904        let new_root = new_roots[0];
5905
5906        let g = graph.read().unwrap();
5907        // After two iterations: the outer Cast is gone (Neg consumes
5908        // x_bf16 directly), then the inner Cast is gone (Neg consumes
5909        // x directly). No Cast nodes are reachable.
5910        let reachable = topo_order_multi(&g, &[new_root]);
5911        let cast_count = reachable
5912            .iter()
5913            .filter(|&&n| matches!(g.node(n).op, Op::Cast(_)))
5914            .count();
5915        assert_eq!(
5916            cast_count, 0,
5917            "fixpoint should eliminate both casts in the chain"
5918        );
5919        assert!(matches!(g.node(new_root).op, Op::Neg));
5920        assert_eq!(g.node(new_root).inputs, vec![x.id()]);
5921    }
5922
5923    /// The rule must not fire on the Cast node itself — only on the
5924    /// consumer of a Cast.
5925    #[test]
5926    fn cast_fusion_does_not_fire_on_cast_node_itself() {
5927        let x = NodeHandle::from_f32(vec![1.0_f32; 4], Shape::from_dims(&[4]), cpu_dev()).unwrap();
5928        let xc = x.cast(fuel_ir::DType::BF16);
5929        let graph = xc.graph().clone();
5930        let pre_len = graph.read().unwrap().len();
5931
5932        let registry =
5933            RuleRegistry::new().with_rule(Box::new(CastFusionRule::new(allow_all_predicate())));
5934        // Root the optimizer at the Cast itself. The rule should look
5935        // at the Cast and decline (it's not the consumer's role).
5936        let new_roots = registry.optimize_to_fixpoint(&graph, &[xc.id()]);
5937        assert_eq!(new_roots, vec![xc.id()]);
5938        assert_eq!(graph.read().unwrap().len(), pre_len);
5939    }
5940
5941    // ===== Phase 2.1: insert_cross_device_copies tests =====
5942
5943    use std::collections::HashMap as StdHashMap;
5944
5945    fn add_node(g: &mut Graph, op: Op, inputs: Vec<NodeId>) -> NodeId {
5946        g.push(Node {
5947            op,
5948            inputs,
5949            shape: Shape::from_dims(&[4]),
5950            dtype: DType::F32,
5951        })
5952    }
5953
5954    /// Snapshot every node's `Graph::placement` into an external
5955    /// map. Needed because `&mut Graph` and `|id| g.placement(id)`
5956    /// closure can't co-exist (overlapping borrows). The closure-
5957    /// based oracle is the production shape (the picker hands in a
5958    /// `Fn(NodeId) -> Option<DeviceLocation>` derived from its own
5959    /// commit map); tests just precompute the snapshot from the
5960    /// graph.
5961    fn snapshot_placements(g: &Graph) -> StdHashMap<NodeId, DeviceLocation> {
5962        let mut m = StdHashMap::new();
5963        for id in 0..g.len() {
5964            let nid = NodeId(id);
5965            if let Some(loc) = g.placement(nid) {
5966                m.insert(nid, loc);
5967            }
5968        }
5969        m
5970    }
5971
5972    fn build_two_node_graph_with_placements(
5973        a: DeviceLocation,
5974        b: DeviceLocation,
5975    ) -> (Graph, NodeId, NodeId) {
5976        let mut g = Graph::new();
5977        let n1 = add_node(&mut g, Op::Const, vec![]);
5978        let n2 = add_node(&mut g, Op::Neg, vec![n1]);
5979        g.set_placement(n1, a);
5980        g.set_placement(n2, b);
5981        (g, n1, n2)
5982    }
5983
5984    fn shares_storage_cpu_devices_only(a: DeviceLocation, b: DeviceLocation) -> bool {
5985        // Test stub topology: CPU shares with CPU only (the AOCL/MKL/
5986        // portable-CPU substrate story). CUDA shares with same-gpu_id
5987        // CUDA. Vulkan shares with same-gpu_id Vulkan. Cross device
5988        // is never shared.
5989        match (a, b) {
5990            (DeviceLocation::Cpu, DeviceLocation::Cpu) => true,
5991            (DeviceLocation::Cuda { gpu_id: x }, DeviceLocation::Cuda { gpu_id: y }) => x == y,
5992            (DeviceLocation::Vulkan { gpu_id: x }, DeviceLocation::Vulkan { gpu_id: y }) => x == y,
5993            (DeviceLocation::Metal { gpu_id: x }, DeviceLocation::Metal { gpu_id: y }) => x == y,
5994            _ => false,
5995        }
5996    }
5997
5998    /// Single-hop everywhere: the pre-two-hop behavior. Every bridged
5999    /// edge gets exactly one `Op::Copy`. Used by the legacy single-hop
6000    /// tests so they stay byte-identical to before the two-hop param.
6001    fn no_host_staging(_a: DeviceLocation, _b: DeviceLocation) -> bool {
6002        false
6003    }
6004
6005    /// Realistic topology predicate: a cross-VENDOR GPU↔GPU pair (CUDA↔
6006    /// Vulkan, or any two distinct GPU substrates) needs host staging
6007    /// (two hops). Any edge touching CPU, or a same-vendor pair, is a
6008    /// single hop.
6009    fn host_staging_cross_vendor_gpu(a: DeviceLocation, b: DeviceLocation) -> bool {
6010        use DeviceLocation::*;
6011        match (a, b) {
6012            // CPU on either end → single hop.
6013            (Cpu, _) | (_, Cpu) => false,
6014            // Same vendor (both CUDA, both Vulkan, both Metal) → single
6015            // hop (the topology can copy directly / shares_storage may
6016            // even short-circuit it first).
6017            (Cuda { .. }, Cuda { .. }) => false,
6018            (Vulkan { .. }, Vulkan { .. }) => false,
6019            (Metal { .. }, Metal { .. }) => false,
6020            // Cross-vendor GPU↔GPU → two hops through host.
6021            _ => true,
6022        }
6023    }
6024
6025    /// No cross-device edges → no copies inserted.
6026    #[test]
6027    fn insert_copies_noop_when_all_same_device() {
6028        let (mut g, _n1, n2) =
6029            build_two_node_graph_with_placements(DeviceLocation::Cpu, DeviceLocation::Cpu);
6030        let pre_len = g.len();
6031        let placements = snapshot_placements(&g);
6032        let inserted = insert_cross_device_copies(
6033            &mut g,
6034            &[n2],
6035            |id| placements.get(&id).copied(),
6036            shares_storage_cpu_devices_only,
6037            no_host_staging,
6038        );
6039        assert_eq!(inserted.len(), 0, "same-device edge → no copy needed");
6040        assert_eq!(g.len(), pre_len, "graph unchanged");
6041    }
6042
6043    /// Cross-device edge (CPU → CUDA) → one `Op::Copy { target: Cuda }`
6044    /// inserted; consumer's input rewired to point at it.
6045    #[test]
6046    fn insert_copies_cuda_consumer_of_cpu_input() {
6047        let cuda = DeviceLocation::Cuda { gpu_id: 0 };
6048        let (mut g, n1, n2) = build_two_node_graph_with_placements(DeviceLocation::Cpu, cuda);
6049        let pre_len = g.len();
6050        let placements = snapshot_placements(&g);
6051        let inserted = insert_cross_device_copies(
6052            &mut g,
6053            &[n2],
6054            |id| placements.get(&id).copied(),
6055            shares_storage_cpu_devices_only,
6056            no_host_staging,
6057        );
6058        assert_eq!(inserted.len(), 1);
6059        assert_eq!(g.len(), pre_len + 1, "one new Op::Copy node appended");
6060        // Consumer's input now points at the new Op::Copy (not n1).
6061        let consumer_inputs = &g.node(n2).inputs;
6062        assert_eq!(consumer_inputs.len(), 1);
6063        let copy_id = consumer_inputs[0];
6064        assert_ne!(copy_id, n1, "consumer's input was rewired");
6065        let copy_node = g.node(copy_id);
6066        assert!(
6067            matches!(copy_node.op, Op::Copy { target } if target == cuda),
6068            "rewired input is Op::Copy targeting CUDA; got {:?}",
6069            copy_node.op,
6070        );
6071        assert_eq!(copy_node.inputs, vec![n1], "Op::Copy reads from n1");
6072        assert_eq!(g.placement(copy_id), Some(cuda), "Op::Copy lands on CUDA");
6073    }
6074
6075    /// Two consumers share an input AND both need it on the same target
6076    /// device → ONE Op::Copy serves both (CSE on inserted copies).
6077    #[test]
6078    fn insert_copies_dedupes_when_two_consumers_share_input_and_target() {
6079        let cuda = DeviceLocation::Cuda { gpu_id: 0 };
6080        let mut g = Graph::new();
6081        let cpu_src = add_node(&mut g, Op::Const, vec![]);
6082        g.set_placement(cpu_src, DeviceLocation::Cpu);
6083        let cuda_a = add_node(&mut g, Op::Neg, vec![cpu_src]);
6084        let cuda_b = add_node(&mut g, Op::Sqr, vec![cpu_src]);
6085        g.set_placement(cuda_a, cuda);
6086        g.set_placement(cuda_b, cuda);
6087
6088        let pre_len = g.len();
6089        let placements = snapshot_placements(&g);
6090        let inserted = insert_cross_device_copies(
6091            &mut g,
6092            &[cuda_a, cuda_b],
6093            |id| placements.get(&id).copied(),
6094            shares_storage_cpu_devices_only,
6095            no_host_staging,
6096        );
6097        assert_eq!(
6098            inserted.len(),
6099            1,
6100            "CSE — one Op::Copy serves both consumers"
6101        );
6102        assert_eq!(g.len(), pre_len + 1);
6103        // Both consumers point at the SAME Op::Copy node.
6104        let a_input = g.node(cuda_a).inputs[0];
6105        let b_input = g.node(cuda_b).inputs[0];
6106        assert_eq!(a_input, b_input, "both consumers share the rewired input");
6107    }
6108
6109    /// Two consumers share an input but want it on DIFFERENT target
6110    /// devices → TWO distinct Op::Copy nodes inserted.
6111    #[test]
6112    fn insert_copies_does_not_dedupe_across_distinct_targets() {
6113        let cuda = DeviceLocation::Cuda { gpu_id: 0 };
6114        let vulkan = DeviceLocation::Vulkan { gpu_id: 0 };
6115        let mut g = Graph::new();
6116        let cpu_src = add_node(&mut g, Op::Const, vec![]);
6117        g.set_placement(cpu_src, DeviceLocation::Cpu);
6118        let cuda_c = add_node(&mut g, Op::Neg, vec![cpu_src]);
6119        let vk_c = add_node(&mut g, Op::Sqr, vec![cpu_src]);
6120        g.set_placement(cuda_c, cuda);
6121        g.set_placement(vk_c, vulkan);
6122
6123        let placements = snapshot_placements(&g);
6124
6125        let inserted = insert_cross_device_copies(
6126            &mut g,
6127            &[cuda_c, vk_c],
6128            |id| placements.get(&id).copied(),
6129            shares_storage_cpu_devices_only,
6130            no_host_staging,
6131        );
6132        assert_eq!(inserted.len(), 2, "distinct targets → distinct copies");
6133        let cuda_input = g.node(cuda_c).inputs[0];
6134        let vk_input = g.node(vk_c).inputs[0];
6135        assert_ne!(cuda_input, vk_input);
6136    }
6137
6138    /// Producer with no placement → pass leaves the edge alone (the
6139    /// executor handles via fallback). Same for consumer with no
6140    /// placement.
6141    #[test]
6142    fn insert_copies_skips_when_placement_absent() {
6143        let mut g = Graph::new();
6144        let n1 = add_node(&mut g, Op::Const, vec![]);
6145        let n2 = add_node(&mut g, Op::Neg, vec![n1]);
6146        // Only consumer has placement; producer doesn't.
6147        g.set_placement(n2, DeviceLocation::Cuda { gpu_id: 0 });
6148        let placements = snapshot_placements(&g);
6149        let inserted = insert_cross_device_copies(
6150            &mut g,
6151            &[n2],
6152            |id| placements.get(&id).copied(),
6153            shares_storage_cpu_devices_only,
6154            no_host_staging,
6155        );
6156        assert_eq!(inserted.len(), 0, "producer with no placement → skip");
6157    }
6158
6159    /// `Op::Copy` and `Op::Move` are themselves transfers — the pass
6160    /// must not try to insert ANOTHER copy on their inputs (would
6161    /// infinite-regress).
6162    #[test]
6163    fn insert_copies_skips_transfer_ops_themselves() {
6164        let cuda = DeviceLocation::Cuda { gpu_id: 0 };
6165        let mut g = Graph::new();
6166        let cpu_src = add_node(&mut g, Op::Const, vec![]);
6167        g.set_placement(cpu_src, DeviceLocation::Cpu);
6168        // A user-authored Op::Copy from CPU to CUDA. Should not be
6169        // re-wrapped.
6170        let copy = g.push(Node {
6171            op: Op::Copy { target: cuda },
6172            inputs: vec![cpu_src],
6173            shape: Shape::from_dims(&[4]),
6174            dtype: DType::F32,
6175        });
6176        g.set_placement(copy, cuda);
6177
6178        let pre_len = g.len();
6179        let placements = snapshot_placements(&g);
6180        let inserted = insert_cross_device_copies(
6181            &mut g,
6182            &[copy],
6183            |id| placements.get(&id).copied(),
6184            shares_storage_cpu_devices_only,
6185            no_host_staging,
6186        );
6187        assert_eq!(inserted.len(), 0, "the existing Op::Copy is not re-wrapped");
6188        assert_eq!(g.len(), pre_len);
6189    }
6190
6191    /// Pass is idempotent — re-running on a graph already rewritten
6192    /// adds zero new copies. Phase 2.1 variant.
6193    #[test]
6194    fn insert_cross_device_copies_idempotent() {
6195        let cuda = DeviceLocation::Cuda { gpu_id: 0 };
6196        let (mut g, _n1, n2) = build_two_node_graph_with_placements(DeviceLocation::Cpu, cuda);
6197        let placements_before = snapshot_placements(&g);
6198        let first = insert_cross_device_copies(
6199            &mut g,
6200            &[n2],
6201            |id| placements_before.get(&id).copied(),
6202            shares_storage_cpu_devices_only,
6203            no_host_staging,
6204        );
6205        let placements_after = snapshot_placements(&g);
6206        let second = insert_cross_device_copies(
6207            &mut g,
6208            &[n2],
6209            |id| placements_after.get(&id).copied(),
6210            shares_storage_cpu_devices_only,
6211            no_host_staging,
6212        );
6213        assert_eq!(first.len(), 1);
6214        assert_eq!(
6215            second.len(),
6216            0,
6217            "re-run sees inserted-Copy as on-target; no new copies"
6218        );
6219    }
6220
6221    /// Cross-GPU edge (CUDA gpu_id 0 → CUDA gpu_id 1) → copy inserted
6222    /// (devices don't share storage even though both are CUDA).
6223    #[test]
6224    fn insert_copies_across_gpu_ids_within_one_backend() {
6225        let cuda0 = DeviceLocation::Cuda { gpu_id: 0 };
6226        let cuda1 = DeviceLocation::Cuda { gpu_id: 1 };
6227        let (mut g, _n1, n2) = build_two_node_graph_with_placements(cuda0, cuda1);
6228        let placements = snapshot_placements(&g);
6229        let inserted = insert_cross_device_copies(
6230            &mut g,
6231            &[n2],
6232            |id| placements.get(&id).copied(),
6233            shares_storage_cpu_devices_only,
6234            no_host_staging,
6235        );
6236        assert_eq!(inserted.len(), 1, "cross-gpu_id is cross-device too");
6237        let copy_id = g.node(n2).inputs[0];
6238        let copy_node = g.node(copy_id);
6239        assert!(matches!(copy_node.op, Op::Copy { target } if target == cuda1));
6240    }
6241
6242    /// Cross-VENDOR GPU↔GPU edge (Vulkan → CUDA) with `needs_host_staging`
6243    /// returning true → TWO `Op::Copy` hops through a CPU intermediate:
6244    /// the consumer reads `Op::Copy{target:Cuda}`, which reads
6245    /// `Op::Copy{target:Cpu}`, which reads the Vulkan producer.
6246    #[test]
6247    fn insert_copies_cross_vendor_gpu_inserts_two_hops_through_cpu() {
6248        let cuda = DeviceLocation::Cuda { gpu_id: 0 };
6249        let vulkan = DeviceLocation::Vulkan { gpu_id: 0 };
6250        let (mut g, vk_src, cuda_consumer) = build_two_node_graph_with_placements(vulkan, cuda);
6251        let pre_len = g.len();
6252        let placements = snapshot_placements(&g);
6253        let inserted = insert_cross_device_copies(
6254            &mut g,
6255            &[cuda_consumer],
6256            |id| placements.get(&id).copied(),
6257            shares_storage_cpu_devices_only,
6258            host_staging_cross_vendor_gpu,
6259        );
6260        assert_eq!(inserted.len(), 2, "cross-vendor GPU↔GPU → two hops");
6261        assert_eq!(g.len(), pre_len + 2, "two new Op::Copy nodes appended");
6262
6263        // Consumer reads the consumer-side hop (Copy → Cuda).
6264        let hop2 = g.node(cuda_consumer).inputs[0];
6265        let hop2_node = g.node(hop2);
6266        assert!(
6267            matches!(hop2_node.op, Op::Copy { target } if target == cuda),
6268            "consumer-side hop targets CUDA; got {:?}",
6269            hop2_node.op,
6270        );
6271        assert_eq!(
6272            g.placement(hop2),
6273            Some(cuda),
6274            "consumer-side hop lands on CUDA"
6275        );
6276
6277        // The consumer-side hop reads the CPU intermediate (Copy → Cpu).
6278        let hop1 = hop2_node.inputs[0];
6279        let hop1_node = g.node(hop1);
6280        assert!(
6281            matches!(hop1_node.op, Op::Copy { target } if target == DeviceLocation::Cpu),
6282            "CPU-intermediate hop targets Cpu; got {:?}",
6283            hop1_node.op,
6284        );
6285        assert_eq!(
6286            g.placement(hop1),
6287            Some(DeviceLocation::Cpu),
6288            "CPU intermediate is placed on CPU",
6289        );
6290
6291        // The CPU intermediate reads the original Vulkan producer.
6292        assert_eq!(
6293            hop1_node.inputs,
6294            vec![vk_src],
6295            "CPU hop reads the Vulkan producer"
6296        );
6297    }
6298
6299    /// Two-hop edge is idempotent: re-running adds zero new copies. The
6300    /// consumer now reads a same-device `Op::Copy`; the CPU intermediate's
6301    /// consumer is also an `Op::Copy` (skipped as a transfer op).
6302    #[test]
6303    fn insert_copies_cross_vendor_two_hop_idempotent() {
6304        let cuda = DeviceLocation::Cuda { gpu_id: 0 };
6305        let vulkan = DeviceLocation::Vulkan { gpu_id: 0 };
6306        let (mut g, _vk_src, cuda_consumer) = build_two_node_graph_with_placements(vulkan, cuda);
6307        let p1 = snapshot_placements(&g);
6308        let first = insert_cross_device_copies(
6309            &mut g,
6310            &[cuda_consumer],
6311            |id| p1.get(&id).copied(),
6312            shares_storage_cpu_devices_only,
6313            host_staging_cross_vendor_gpu,
6314        );
6315        let p2 = snapshot_placements(&g);
6316        let second = insert_cross_device_copies(
6317            &mut g,
6318            &[cuda_consumer],
6319            |id| p2.get(&id).copied(),
6320            shares_storage_cpu_devices_only,
6321            host_staging_cross_vendor_gpu,
6322        );
6323        assert_eq!(first.len(), 2, "two hops on first run");
6324        assert_eq!(second.len(), 0, "re-run adds no new copies");
6325    }
6326
6327    /// CSE on a two-hop edge: one Vulkan producer feeding TWO CUDA
6328    /// consumers stages to CPU exactly once (one CPU intermediate), then
6329    /// one consumer-side CUDA hop shared by both → 2 inserted copies
6330    /// total, not 4.
6331    #[test]
6332    fn insert_copies_cross_vendor_two_hop_cse_shares_cpu_intermediate() {
6333        let cuda = DeviceLocation::Cuda { gpu_id: 0 };
6334        let vulkan = DeviceLocation::Vulkan { gpu_id: 0 };
6335        let mut g = Graph::new();
6336        let vk_src = add_node(&mut g, Op::Const, vec![]);
6337        g.set_placement(vk_src, vulkan);
6338        let cuda_a = add_node(&mut g, Op::Neg, vec![vk_src]);
6339        let cuda_b = add_node(&mut g, Op::Sqr, vec![vk_src]);
6340        g.set_placement(cuda_a, cuda);
6341        g.set_placement(cuda_b, cuda);
6342
6343        let placements = snapshot_placements(&g);
6344        let inserted = insert_cross_device_copies(
6345            &mut g,
6346            &[cuda_a, cuda_b],
6347            |id| placements.get(&id).copied(),
6348            shares_storage_cpu_devices_only,
6349            host_staging_cross_vendor_gpu,
6350        );
6351        // One CPU intermediate + one shared CUDA hop = 2.
6352        assert_eq!(
6353            inserted.len(),
6354            2,
6355            "CSE — one CPU intermediate + one CUDA hop"
6356        );
6357        let a_input = g.node(cuda_a).inputs[0];
6358        let b_input = g.node(cuda_b).inputs[0];
6359        assert_eq!(
6360            a_input, b_input,
6361            "both CUDA consumers share the consumer-side hop"
6362        );
6363        // That shared hop reads the single CPU intermediate.
6364        let cpu_hop = g.node(a_input).inputs[0];
6365        assert!(matches!(
6366            g.node(cpu_hop).op,
6367            Op::Copy {
6368                target: DeviceLocation::Cpu
6369            }
6370        ));
6371        assert_eq!(g.node(cpu_hop).inputs, vec![vk_src]);
6372    }
6373
6374    /// A CPU↔GPU edge stays SINGLE hop even when the host-staging
6375    /// predicate is the realistic cross-vendor one (which returns false
6376    /// for any CPU-touching edge) — byte-identical to the legacy path.
6377    #[test]
6378    fn insert_copies_cpu_gpu_edge_stays_single_hop_under_cross_vendor_predicate() {
6379        let cuda = DeviceLocation::Cuda { gpu_id: 0 };
6380        let (mut g, n1, n2) = build_two_node_graph_with_placements(DeviceLocation::Cpu, cuda);
6381        let pre_len = g.len();
6382        let placements = snapshot_placements(&g);
6383        let inserted = insert_cross_device_copies(
6384            &mut g,
6385            &[n2],
6386            |id| placements.get(&id).copied(),
6387            shares_storage_cpu_devices_only,
6388            host_staging_cross_vendor_gpu,
6389        );
6390        assert_eq!(inserted.len(), 1, "CPU↔CUDA is a single hop");
6391        assert_eq!(g.len(), pre_len + 1);
6392        let copy_id = g.node(n2).inputs[0];
6393        assert!(matches!(g.node(copy_id).op, Op::Copy { target } if target == cuda));
6394        assert_eq!(g.node(copy_id).inputs, vec![n1]);
6395    }
6396
6397    /// Per-node `placement_for` can be backed by an external map, not
6398    /// just `Graph::placement`. Smoke test of the closure shape.
6399    #[test]
6400    fn insert_copies_takes_external_placement_oracle() {
6401        let cuda = DeviceLocation::Cuda { gpu_id: 0 };
6402        let (mut g, n1, n2) = build_two_node_graph_with_placements(DeviceLocation::Cpu, cuda);
6403        // Clear graph-side placements; back the oracle with an
6404        // external map instead.
6405        let mut external: StdHashMap<NodeId, DeviceLocation> = StdHashMap::new();
6406        external.insert(n1, DeviceLocation::Cpu);
6407        external.insert(n2, cuda);
6408        // Strip graph-side placements so we PROVE the closure is the
6409        // authoritative source.
6410        // (No public setter to clear; instead override by leaving
6411        // graph.placement untouched and using only the external map.)
6412        let _placements = snapshot_placements(&g);
6413        let inserted = insert_cross_device_copies(
6414            &mut g,
6415            &[n2],
6416            |id| external.get(&id).copied(),
6417            shares_storage_cpu_devices_only,
6418            no_host_staging,
6419        );
6420        assert_eq!(inserted.len(), 1);
6421    }
6422
6423    // ===== Phase 2.2 — insert_layout_fixups =====
6424
6425    fn count_contiguize_nodes(graph: &SharedGraph) -> usize {
6426        let g = graph.read().unwrap();
6427        (0..g.len())
6428            .filter(|i| matches!(g.node(NodeId(*i)).op, Op::Contiguize))
6429            .count()
6430    }
6431
6432    /// All-contiguous-input graph: pass adds nothing regardless of
6433    /// the kernel_accepts_strided predicate.
6434    #[test]
6435    fn insert_fixups_no_strided_inputs_no_fixups() {
6436        let a = NodeHandle::from_f32(vec![1.0, 2.0], Shape::from_dims(&[2]), cpu_dev()).unwrap();
6437        let b = a
6438            .const_f32_like(vec![3.0, 4.0], Shape::from_dims(&[2]))
6439            .unwrap();
6440        let c = a.add(&b);
6441        let graph = c.graph().clone();
6442
6443        let before = count_contiguize_nodes(&graph);
6444        let inserted = {
6445            let mut g = graph.write().unwrap();
6446            insert_layout_fixups(&mut g, &[c.id()], |_| false)
6447        };
6448        assert_eq!(inserted, 0);
6449        assert_eq!(count_contiguize_nodes(&graph), before);
6450    }
6451
6452    /// Strided input (Transpose) feeding a kernel that rejects
6453    /// strided: one Contiguize gets inserted between them and the
6454    /// consumer's input gets rewired.
6455    #[test]
6456    fn insert_fixups_strided_input_rejecting_kernel_inserts_contiguize() {
6457        // 2x3 const, transpose to 3x2, then "consumer" reads the
6458        // transposed view. The consumer kernel claims it can't
6459        // handle strided.
6460        let a = NodeHandle::from_f32(
6461            vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0],
6462            Shape::from_dims(&[2, 3]),
6463            cpu_dev(),
6464        )
6465        .unwrap();
6466        // Transpose produces a strided view via the layout side-table.
6467        let at = a.transpose();
6468        // A binary op as our "consumer" (Op::Add is kernel-bearing,
6469        // not a view op).
6470        let b = at
6471            .const_f32_like(vec![0.0; 6], Shape::from_dims(&[3, 2]))
6472            .unwrap();
6473        let c = at.add(&b);
6474        let graph = c.graph().clone();
6475
6476        let before = count_contiguize_nodes(&graph);
6477        let inserted = {
6478            let mut g = graph.write().unwrap();
6479            // Kernel rejects strided for every node.
6480            insert_layout_fixups(&mut g, &[c.id()], |_| false)
6481        };
6482        // One Contiguize for the transposed input of `c`. (`b` is a
6483        // contiguous Const → no fixup.)
6484        assert_eq!(inserted, 1);
6485        assert_eq!(count_contiguize_nodes(&graph) - before, 1);
6486
6487        // c's first input was `at`; after rewrite it should point
6488        // to the new Op::Contiguize node.
6489        let g = graph.read().unwrap();
6490        let c_node = g.node(c.id());
6491        let new_input = g.node(c_node.inputs[0]);
6492        assert!(
6493            matches!(new_input.op, Op::Contiguize),
6494            "consumer's strided input must be rewired through Op::Contiguize, \
6495             got {:?}",
6496            new_input.op,
6497        );
6498        // The Contiguize's input is the original transposed view.
6499        assert_eq!(new_input.inputs, vec![at.id()]);
6500    }
6501
6502    /// Strided input feeding a kernel that accepts strided: pass
6503    /// skips the input, no Contiguize inserted.
6504    #[test]
6505    fn insert_fixups_strided_input_accepting_kernel_no_fixup() {
6506        let a = NodeHandle::from_f32(
6507            vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0],
6508            Shape::from_dims(&[2, 3]),
6509            cpu_dev(),
6510        )
6511        .unwrap();
6512        let at = a.transpose();
6513        let b = at
6514            .const_f32_like(vec![0.0; 6], Shape::from_dims(&[3, 2]))
6515            .unwrap();
6516        let c = at.add(&b);
6517        let graph = c.graph().clone();
6518
6519        let before = count_contiguize_nodes(&graph);
6520        let inserted = {
6521            let mut g = graph.write().unwrap();
6522            // Every kernel accepts strided — no fixup.
6523            insert_layout_fixups(&mut g, &[c.id()], |_| true)
6524        };
6525        assert_eq!(inserted, 0);
6526        assert_eq!(count_contiguize_nodes(&graph), before);
6527    }
6528
6529    /// CSE: two non-strided-tolerant consumers reading the same
6530    /// transposed view share one inserted Contiguize node.
6531    #[test]
6532    fn insert_fixups_cse_dedupes_shared_strided_input() {
6533        let a = NodeHandle::from_f32(
6534            vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0],
6535            Shape::from_dims(&[2, 3]),
6536            cpu_dev(),
6537        )
6538        .unwrap();
6539        let at = a.transpose();
6540        // Two consumers of the same transposed view.
6541        let b = at
6542            .const_f32_like(vec![0.0; 6], Shape::from_dims(&[3, 2]))
6543            .unwrap();
6544        let c1 = at.add(&b);
6545        let c2 = at.mul(&b);
6546        let graph = c1.graph().clone();
6547
6548        let inserted = {
6549            let mut g = graph.write().unwrap();
6550            insert_layout_fixups(&mut g, &[c1.id(), c2.id()], |_| false)
6551        };
6552        // Exactly ONE Contiguize, shared between the two consumers.
6553        assert_eq!(inserted, 1);
6554
6555        let g = graph.read().unwrap();
6556        let c1_input = g.node(c1.id()).inputs[0];
6557        let c2_input = g.node(c2.id()).inputs[0];
6558        assert_eq!(
6559            c1_input, c2_input,
6560            "CSE: both consumers must share the same Contiguize node",
6561        );
6562        assert!(matches!(g.node(c1_input).op, Op::Contiguize));
6563    }
6564
6565    /// Idempotency: running the pass twice on the same graph adds
6566    /// 0 new nodes the second time.
6567    #[test]
6568    fn insert_fixups_idempotent() {
6569        let a = NodeHandle::from_f32(
6570            vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0],
6571            Shape::from_dims(&[2, 3]),
6572            cpu_dev(),
6573        )
6574        .unwrap();
6575        let at = a.transpose();
6576        let b = at
6577            .const_f32_like(vec![0.0; 6], Shape::from_dims(&[3, 2]))
6578            .unwrap();
6579        let c = at.add(&b);
6580        let graph = c.graph().clone();
6581
6582        let first = {
6583            let mut g = graph.write().unwrap();
6584            insert_layout_fixups(&mut g, &[c.id()], |_| false)
6585        };
6586        assert_eq!(first, 1, "first run inserts one Contiguize");
6587
6588        let second = {
6589            let mut g = graph.write().unwrap();
6590            insert_layout_fixups(&mut g, &[c.id()], |_| false)
6591        };
6592        assert_eq!(second, 0, "second run is a no-op");
6593    }
6594
6595    /// View ops in the consumer position do NOT get Contiguize
6596    /// inserted before them — they preserve strided layouts
6597    /// intentionally as part of their semantics.
6598    #[test]
6599    fn insert_fixups_skips_view_op_consumers() {
6600        let a = NodeHandle::from_f32(
6601            vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0],
6602            Shape::from_dims(&[2, 3]),
6603            cpu_dev(),
6604        )
6605        .unwrap();
6606        let at = a.transpose();
6607        // Permute on a transposed view: both are view ops, the
6608        // strided layout flows through unchanged.
6609        let ap = at.permute(&[1, 0]);
6610        let graph = ap.graph().clone();
6611
6612        let inserted = {
6613            let mut g = graph.write().unwrap();
6614            insert_layout_fixups(&mut g, &[ap.id()], |_| false)
6615        };
6616        assert_eq!(
6617            inserted, 0,
6618            "view ops preserve strided layouts; no Contiguize should be inserted",
6619        );
6620    }
6621}