laddu-compile 0.23.0

Amplitude analysis tools for Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
use std::collections::HashMap;

use laddu_expr::{
    ExprGraph, ExprId, ExprNode, P4Component, ValueKind,
    parameters::{ParamId, ParamLayout},
};
use laddu_kernel::ir::{
    CacheKernelIr, KernelInstruction, KernelValue, KernelValueClass, KernelValueId,
    KernelValueKind, ScalarKernelIr,
};

use crate::{CachePlan, CompileResult, CompiledModel, graph_utils::mark_reachable};

/// Specialized plan for reading one component of an event-dependent linear solve.
#[derive(Copy, Clone, Debug)]
pub struct SolveComponentPlan {
    rhs: ExprId,
    row_slot: usize,
    dimension: usize,
}

/// Typed event columns required to materialize the executable cache.
///
/// This is backend-ready plan metadata: consumers do not need to inspect the
/// optimized expression graph to decide how to bind event storage.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum CacheInput {
    /// A named scalar event column.
    Scalar(String),
    /// A named four-momentum component.
    P4(String, P4Component),
}

impl SolveComponentPlan {
    /// Returns the solve right-hand-side node.
    pub fn rhs(self) -> ExprId {
        self.rhs
    }

    /// Returns the cached factorization row slot.
    pub fn row_slot(self) -> usize {
        self.row_slot
    }

    /// Returns the square matrix dimension.
    pub fn dimension(self) -> usize {
        self.dimension
    }
}

/// Cached row layout for one event-dependent solve matrix.
#[derive(Clone, Debug)]
pub struct SolveRowMatrixPlan {
    matrix: ExprId,
    dimension: usize,
    rows: Vec<(usize, usize)>,
}

#[derive(Clone, Debug)]
struct NodeBindings {
    parameter_slots: Vec<Option<ParamId>>,
    cache_slots: Vec<Option<usize>>,
    evaluation_nodes: Vec<ExprId>,
    value_slots: Vec<Option<usize>>,
}

#[derive(Clone, Debug)]
struct SolvePlan {
    components: Vec<Option<SolveComponentPlan>>,
    rhs_elements: Vec<Option<Vec<ExprId>>>,
    row_matrices: Vec<SolveRowMatrixPlan>,
    row_keys: Vec<(ExprId, usize, usize)>,
}

#[derive(Clone, Debug)]
struct FactorizationPlan {
    event_slots: Vec<Option<usize>>,
    event_matrices: Vec<(ExprId, usize)>,
    constant_slots: Vec<Option<usize>>,
    constant_matrices: Vec<(ExprId, usize)>,
}

impl SolveRowMatrixPlan {
    /// Returns the matrix graph node.
    pub fn matrix(&self) -> ExprId {
        self.matrix
    }

    /// Returns the square matrix dimension.
    pub fn dimension(&self) -> usize {
        self.dimension
    }

    /// Returns `(row_index, cache_slot)` pairs.
    pub fn rows(&self) -> &[(usize, usize)] {
        &self.rows
    }
}

/// Backend-neutral schedule, cache layout, and lowered kernel IR for a model.
#[derive(Clone, Debug)]
pub struct ExecutablePlan {
    graph: ExprGraph,
    params: ParamLayout,
    cache_plan: CachePlan,
    bindings: NodeBindings,
    scalar_kernel: Option<ScalarKernelIr>,
    cache_kernel: Option<CacheKernelIr>,
    cache_input_nodes: Vec<ExprId>,
    cache_materialization_nodes: Vec<ExprId>,
    solve: SolvePlan,
    factorization: FactorizationPlan,
}

impl ExecutablePlan {
    /// Lowers a compiled model into an executable plan.
    ///
    /// # Errors
    ///
    /// Returns [`CompileError`](crate::CompileError) when the model cannot be
    /// lowered to valid scalar or cache kernel IR.
    pub fn from_model(model: &CompiledModel) -> CompileResult<Self> {
        Self::from_model_with_solve_rows(model, true)
    }

    /// Build an executable plan without the CPU-oriented cached solve-row specialization.
    ///
    /// Backends with inexpensive fused solves can use this form to keep the original `Solve`
    /// instruction and consume an ordinarily cached event-dependent matrix directly.
    ///
    /// # Errors
    ///
    /// Returns [`CompileError`](crate::CompileError) when the model cannot be
    /// lowered to valid scalar or cache kernel IR.
    pub fn from_model_without_solve_rows(model: &CompiledModel) -> CompileResult<Self> {
        Self::from_model_with_solve_rows(model, false)
    }

    fn from_model_with_solve_rows(
        model: &CompiledModel,
        specialize_solve_rows: bool,
    ) -> CompileResult<Self> {
        PlanBuilder::new(model, specialize_solve_rows).build()
    }

    /// Returns the optimized expression graph.
    pub fn graph(&self) -> &ExprGraph {
        &self.graph
    }

    /// Returns the model parameter layout.
    pub fn params(&self) -> &ParamLayout {
        &self.params
    }

    /// Maps graph nodes to parameter identifiers.
    pub fn parameter_slots(&self) -> &[Option<ParamId>] {
        &self.bindings.parameter_slots
    }

    /// Returns the ordinary event-cache plan.
    pub fn cache_plan(&self) -> &CachePlan {
        &self.cache_plan
    }

    /// Maps graph nodes to ordinary cache slots.
    pub fn cache_slots(&self) -> &[Option<usize>] {
        &self.bindings.cache_slots
    }

    /// Returns nodes evaluated by the scalar schedule.
    pub fn evaluation_nodes(&self) -> &[ExprId] {
        &self.bindings.evaluation_nodes
    }

    /// Maps graph nodes to scalar schedule value slots.
    pub fn value_slots(&self) -> &[Option<usize>] {
        &self.bindings.value_slots
    }

    /// Returns lowered scalar kernel IR, if the plan has scalar work.
    pub fn scalar_kernel(&self) -> Option<&ScalarKernelIr> {
        self.scalar_kernel.as_ref()
    }

    /// Returns lowered cache-materialization kernel IR, if required.
    pub fn cache_kernel(&self) -> Option<&CacheKernelIr> {
        self.cache_kernel.as_ref()
    }

    /// Returns graph nodes supplied as inputs to the cache kernel.
    pub fn cache_input_nodes(&self) -> &[ExprId] {
        &self.cache_input_nodes
    }

    /// Returns typed event-column bindings for cache materialization.
    ///
    /// # Errors
    ///
    /// Returns [`CompileError::InvalidExecutablePlan`](crate::CompileError::InvalidExecutablePlan)
    /// if a cache input is not a direct event column.
    pub fn cache_inputs(&self) -> CompileResult<Vec<CacheInput>> {
        self.cache_input_nodes
            .iter()
            .map(|node| match self.graph.node(*node) {
                Some(ExprNode::EventScalar(name)) => Ok(CacheInput::Scalar(name.to_string())),
                Some(ExprNode::EventP4Component { name, component }) => {
                    Ok(CacheInput::P4(name.to_string(), *component))
                }
                node => Err(crate::CompileError::InvalidExecutablePlan(format!(
                    "cache input is not an event column: {node:?}"
                ))),
            })
            .collect()
    }

    /// Returns nodes evaluated while materializing all caches.
    pub fn cache_materialization_nodes(&self) -> &[ExprId] {
        &self.cache_materialization_nodes
    }

    /// Maps graph nodes to specialized solve-component plans.
    pub fn solve_components(&self) -> &[Option<SolveComponentPlan>] {
        &self.solve.components
    }

    /// Maps solve nodes to flattened right-hand-side element nodes.
    pub fn solve_rhs_elements(&self) -> &[Option<Vec<ExprId>>] {
        &self.solve.rhs_elements
    }

    /// Returns event-dependent matrices using cached solve rows.
    pub fn solve_row_matrices(&self) -> &[SolveRowMatrixPlan] {
        &self.solve.row_matrices
    }

    /// Returns keys identifying specialized matrix rows.
    pub fn solve_row_keys(&self) -> &[(ExprId, usize, usize)] {
        &self.solve.row_keys
    }

    /// Maps event-dependent matrix nodes to factorization slots.
    pub fn factor_matrix_slots(&self) -> &[Option<usize>] {
        &self.factorization.event_slots
    }

    /// Returns event-dependent matrices to factor per event.
    pub fn factor_matrices(&self) -> &[(ExprId, usize)] {
        &self.factorization.event_matrices
    }

    /// Maps invariant matrix nodes to constant factorization slots.
    pub fn constant_factor_slots(&self) -> &[Option<usize>] {
        &self.factorization.constant_slots
    }

    /// Returns invariant matrices to factor once.
    pub fn constant_factor_matrices(&self) -> &[(ExprId, usize)] {
        &self.factorization.constant_matrices
    }

    fn solve_component_plans(model: &CompiledModel) -> CompileResult<SolvePlan> {
        let node_count = model.graph().nodes().len();
        let mut components = vec![None; node_count];
        let mut rhs_elements = vec![None; node_count];
        let mut row_slots = HashMap::<(ExprId, usize), usize>::new();
        let mut row_keys = Vec::new();
        let mut matrix_slots = HashMap::<ExprId, usize>::new();
        let mut matrices = Vec::<SolveRowMatrixPlan>::new();

        for (node_index, node) in model.graph().nodes().iter().enumerate() {
            let ExprNode::Component { input, index } = node else {
                continue;
            };
            let Some(ExprNode::Solve { matrix, rhs }) = model.graph().node(*input) else {
                continue;
            };
            let Some(matrix_facts) = model.node_facts(*matrix) else {
                continue;
            };
            let matrix_dependency = matrix_facts.dependency;
            if !matrix_dependency.depends_on_event
                || matrix_dependency.depends_on_free_params
                || matrix_dependency.depends_on_fixed_params
            {
                continue;
            }
            let Some(rhs_facts) = model.node_facts(*rhs) else {
                continue;
            };
            let rhs_dependency = rhs_facts.dependency;
            if !rhs_dependency.depends_on_free_params && !rhs_dependency.depends_on_fixed_params {
                continue;
            }
            let ValueKind::Matrix { rows, cols } = matrix_facts.value_kind else {
                continue;
            };
            let ValueKind::Vector { len } = rhs_facts.value_kind else {
                continue;
            };
            if rows == 0 || rows != cols || rows != len || *index >= rows {
                continue;
            }

            let row_slot = if let Some(slot) = row_slots.get(&(*matrix, *index)) {
                *slot
            } else {
                let slot = row_keys.len();
                row_slots.insert((*matrix, *index), slot);
                row_keys.push((*matrix, *index, rows));
                let matrix_slot = if let Some(slot) = matrix_slots.get(matrix) {
                    *slot
                } else {
                    let slot = matrices.len();
                    matrix_slots.insert(*matrix, slot);
                    matrices.push(SolveRowMatrixPlan {
                        matrix: *matrix,
                        dimension: rows,
                        rows: Vec::new(),
                    });
                    slot
                };
                matrices[matrix_slot].rows.push((slot, *index));
                slot
            };
            components[node_index] = Some(SolveComponentPlan {
                rhs: *rhs,
                row_slot,
                dimension: rows,
            });
            if let Some(ExprNode::Vector { elements }) = model.graph().node(*rhs) {
                rhs_elements[rhs.index()] = Some(elements.clone());
            }
        }

        SolvePlan::new(node_count, components, rhs_elements, matrices, row_keys)
    }

    fn scalar_kernel_ir(
        model: &CompiledModel,
        evaluation_nodes: &[ExprId],
        cache_slots: &[Option<usize>],
        parameter_slots: &[Option<ParamId>],
        solve_components: &[Option<SolveComponentPlan>],
        solve_rhs_elements: &[Option<Vec<ExprId>>],
    ) -> CompileResult<Option<ScalarKernelIr>> {
        let mut boundary = ScalarBoundary {
            cache_slots,
            parameter_slots,
            solve_components,
            solve_rhs_elements,
        };
        let lowered = KernelLowerer::new(model, evaluation_nodes).lower(&mut boundary)?;
        let LoweringOutcome::Lowered(lowered) = lowered else {
            return Ok(None);
        };
        let root = lowered.value_ids[model.graph().root().index()].ok_or_else(|| {
            crate::CompileError::InvalidExecutablePlan("graph root is not scheduled".into())
        })?;
        if !matches!(
            lowered.values[root.index()].kind,
            KernelValueKind::Real | KernelValueKind::Complex
        ) {
            return Ok(None);
        }
        Ok(Some(ScalarKernelIr::new(lowered.values, root)?))
    }

    fn cache_kernel_ir(
        model: &CompiledModel,
        nodes: &[ExprId],
    ) -> CompileResult<(Option<CacheKernelIr>, Vec<ExprId>)> {
        if model.cache_plan().is_empty() {
            return Ok((None, Vec::new()));
        }
        let mut boundary = CacheBoundary::default();
        let lowered = KernelLowerer::new(model, nodes).lower(&mut boundary)?;
        let LoweringOutcome::Lowered(lowered) = lowered else {
            return Ok((None, Vec::new()));
        };
        let outputs = model
            .cache_plan()
            .entries()
            .iter()
            .map(|entry| {
                lowered.value_ids[entry.node().index()].ok_or_else(|| {
                    crate::CompileError::InvalidExecutablePlan(format!(
                        "cache output node {} is not scheduled",
                        entry.node().index()
                    ))
                })
            })
            .collect::<CompileResult<Vec<_>>>()?;
        Ok((
            Some(CacheKernelIr::new(lowered.values, outputs)?),
            boundary.inputs,
        ))
    }

    fn evaluation_schedule(
        graph: &ExprGraph,
        cache_slots: &[Option<usize>],
        solve_components: &[Option<SolveComponentPlan>],
        solve_rhs_elements: &[Option<Vec<ExprId>>],
    ) -> CompileResult<(Vec<ExprId>, Vec<Option<usize>>)> {
        let mut required = vec![false; graph.nodes().len()];
        Self::mark_evaluation_node(
            graph,
            graph.root(),
            cache_slots,
            solve_components,
            solve_rhs_elements,
            &mut required,
        );
        let nodes = Self::ids_from_flags(required)?;
        let mut value_slots = vec![None; graph.nodes().len()];
        for (slot, id) in nodes.iter().enumerate() {
            value_slots[id.index()] = Some(slot);
        }
        Ok((nodes, value_slots))
    }

    fn mark_evaluation_node(
        graph: &ExprGraph,
        id: ExprId,
        cache_slots: &[Option<usize>],
        solve_components: &[Option<SolveComponentPlan>],
        solve_rhs_elements: &[Option<Vec<ExprId>>],
        required: &mut [bool],
    ) {
        let mut stack = vec![id];
        while let Some(id) = stack.pop() {
            if required[id.index()] {
                continue;
            }
            required[id.index()] = true;
            if cache_slots[id.index()].is_some() {
                continue;
            }
            if let Some(plan) = solve_components[id.index()] {
                if let Some(elements) = &solve_rhs_elements[plan.rhs.index()] {
                    stack.extend(elements.iter().rev().copied());
                } else {
                    stack.push(plan.rhs);
                }
                continue;
            }
            if let Some(node) = graph.node(id) {
                stack.extend(node.children().rev());
            }
        }
    }

    fn ids_from_flags(flags: Vec<bool>) -> CompileResult<Vec<ExprId>> {
        flags
            .into_iter()
            .enumerate()
            .filter(|&(_index, required)| required)
            .map(|(index, _required)| Ok(ExprId::from_index(index)))
            .collect()
    }
}

struct PlanBuilder<'a> {
    model: &'a CompiledModel,
    specialize_solve_rows: bool,
}

impl<'a> PlanBuilder<'a> {
    fn new(model: &'a CompiledModel, specialize_solve_rows: bool) -> Self {
        Self {
            model,
            specialize_solve_rows,
        }
    }

    fn build(self) -> CompileResult<ExecutablePlan> {
        let graph = self.model.graph().clone();
        let params = self.model.params().clone();
        let cache_plan = self.model.cache_plan().clone();
        let solve = if self.specialize_solve_rows {
            ExecutablePlan::solve_component_plans(self.model)?
        } else {
            SolvePlan::empty(graph.nodes().len())
        };
        let bindings = NodeBindings::build(self.model, &solve)?;
        let scalar_kernel = ExecutablePlan::scalar_kernel_ir(
            self.model,
            &bindings.evaluation_nodes,
            &bindings.cache_slots,
            &bindings.parameter_slots,
            &solve.components,
            &solve.rhs_elements,
        )?;

        let mut cache_required_nodes = vec![false; graph.nodes().len()];
        mark_reachable(
            &graph,
            cache_plan
                .materialization_nodes()
                .iter()
                .copied()
                .chain(solve.row_matrices.iter().map(|plan| plan.matrix)),
            &mut cache_required_nodes,
        );
        let cache_materialization_nodes = ExecutablePlan::ids_from_flags(cache_required_nodes)?;
        let (cache_kernel, cache_input_nodes) =
            ExecutablePlan::cache_kernel_ir(self.model, &cache_materialization_nodes)?;
        let factorization = FactorizationPlan::build(self.model, &bindings.value_slots)?;

        Ok(ExecutablePlan {
            graph,
            params,
            cache_plan,
            bindings,
            scalar_kernel,
            cache_kernel,
            cache_input_nodes,
            cache_materialization_nodes,
            solve,
            factorization,
        })
    }
}

impl NodeBindings {
    fn build(model: &CompiledModel, solve: &SolvePlan) -> CompileResult<Self> {
        let graph = model.graph();
        let node_count = graph.nodes().len();
        let parameter_slots = graph
            .nodes()
            .iter()
            .map(|node| match node {
                ExprNode::ScalarParam(parameter) => model.params().id(parameter.name()),
                _ => None,
            })
            .collect::<Vec<_>>();
        let mut cache_slots = vec![None; node_count];
        for (slot, entry) in model.cache_plan().entries().iter().enumerate() {
            let entry_slot = cache_slots.get_mut(entry.node().index()).ok_or_else(|| {
                crate::CompileError::InvalidExecutablePlan(format!(
                    "cache entry node {} is out of bounds for {node_count} nodes",
                    entry.node().index()
                ))
            })?;
            *entry_slot = Some(slot);
        }
        let (evaluation_nodes, value_slots) = ExecutablePlan::evaluation_schedule(
            graph,
            &cache_slots,
            &solve.components,
            &solve.rhs_elements,
        )?;
        let bindings = Self {
            parameter_slots,
            cache_slots,
            evaluation_nodes,
            value_slots,
        };
        bindings.validate(node_count)?;
        Ok(bindings)
    }

    fn validate(&self, node_count: usize) -> CompileResult<()> {
        for (name, len) in [
            ("parameter slots", self.parameter_slots.len()),
            ("cache slots", self.cache_slots.len()),
            ("value slots", self.value_slots.len()),
        ] {
            if len != node_count {
                return Err(crate::CompileError::InvalidExecutablePlan(format!(
                    "{name} length {len} does not match graph length {node_count}"
                )));
            }
        }
        for (expected_slot, id) in self.evaluation_nodes.iter().enumerate() {
            if self.value_slots.get(id.index()).copied().flatten() != Some(expected_slot) {
                return Err(crate::CompileError::InvalidExecutablePlan(format!(
                    "evaluation node {} is not bound to value slot {expected_slot}",
                    id.index()
                )));
            }
        }
        Ok(())
    }
}

impl SolvePlan {
    fn empty(node_count: usize) -> Self {
        Self {
            components: vec![None; node_count],
            rhs_elements: vec![None; node_count],
            row_matrices: Vec::new(),
            row_keys: Vec::new(),
        }
    }

    fn new(
        node_count: usize,
        components: Vec<Option<SolveComponentPlan>>,
        rhs_elements: Vec<Option<Vec<ExprId>>>,
        row_matrices: Vec<SolveRowMatrixPlan>,
        row_keys: Vec<(ExprId, usize, usize)>,
    ) -> CompileResult<Self> {
        if components.len() != node_count || rhs_elements.len() != node_count {
            return Err(crate::CompileError::InvalidExecutablePlan(
                "solve plan node maps do not match the graph length".into(),
            ));
        }
        for component in components.iter().flatten() {
            if component.rhs.index() >= node_count
                || component.row_slot >= row_keys.len()
                || row_keys[component.row_slot].2 != component.dimension
            {
                return Err(crate::CompileError::InvalidExecutablePlan(
                    "solve component references an invalid RHS node or row slot".into(),
                ));
            }
        }
        if rhs_elements
            .iter()
            .flatten()
            .flatten()
            .any(|id| id.index() >= node_count)
            || row_keys.iter().any(|(matrix, row, dimension)| {
                matrix.index() >= node_count || *dimension == 0 || *row >= *dimension
            })
        {
            return Err(crate::CompileError::InvalidExecutablePlan(
                "solve plan contains an invalid graph node or row key".into(),
            ));
        }
        for matrix in &row_matrices {
            if matrix.matrix.index() >= node_count
                || matrix.rows.iter().any(|(slot, row)| {
                    *slot >= row_keys.len()
                        || *row >= matrix.dimension
                        || row_keys[*slot] != (matrix.matrix, *row, matrix.dimension)
                })
            {
                return Err(crate::CompileError::InvalidExecutablePlan(
                    "solve row matrix contains an invalid node, row, or slot".into(),
                ));
            }
        }
        Ok(Self {
            components,
            rhs_elements,
            row_matrices,
            row_keys,
        })
    }
}

impl FactorizationPlan {
    fn build(model: &CompiledModel, value_slots: &[Option<usize>]) -> CompileResult<Self> {
        let node_count = model.graph().nodes().len();
        if value_slots.len() != node_count {
            return Err(crate::CompileError::InvalidExecutablePlan(
                "factorization value slots do not match the graph length".into(),
            ));
        }
        let mut plan = Self {
            event_slots: vec![None; node_count],
            event_matrices: Vec::new(),
            constant_slots: vec![None; node_count],
            constant_matrices: Vec::new(),
        };
        for (index, node) in model.graph().nodes().iter().enumerate() {
            let ExprNode::Solve { matrix, .. } = node else {
                continue;
            };
            if value_slots[index].is_none() {
                continue;
            }
            let Some(facts) = model.node_facts(*matrix) else {
                continue;
            };
            if facts.dependency.depends_on_free_params || facts.dependency.depends_on_fixed_params {
                continue;
            }
            let ValueKind::Matrix { rows, cols } = facts.value_kind else {
                continue;
            };
            if rows != cols {
                continue;
            }
            let (slots, matrices) = if facts.dependency.depends_on_event {
                (&mut plan.event_slots, &mut plan.event_matrices)
            } else {
                (&mut plan.constant_slots, &mut plan.constant_matrices)
            };
            if slots[matrix.index()].is_none() {
                let slot = matrices.len();
                slots[matrix.index()] = Some(slot);
                matrices.push((*matrix, rows));
            }
        }
        plan.validate(node_count)?;
        Ok(plan)
    }

    fn validate(&self, node_count: usize) -> CompileResult<()> {
        for (slots, matrices, name) in [
            (&self.event_slots, &self.event_matrices, "event"),
            (&self.constant_slots, &self.constant_matrices, "constant"),
        ] {
            if slots.len() != node_count {
                return Err(crate::CompileError::InvalidExecutablePlan(format!(
                    "{name} factor slots do not match the graph length"
                )));
            }
            if slots.iter().flatten().any(|slot| *slot >= matrices.len()) {
                return Err(crate::CompileError::InvalidExecutablePlan(format!(
                    "{name} factor slots contain an invalid matrix slot"
                )));
            }
            for (slot, (matrix, _dimension)) in matrices.iter().enumerate() {
                if slots.get(matrix.index()).copied().flatten() != Some(slot) {
                    return Err(crate::CompileError::InvalidExecutablePlan(format!(
                        "{name} factor matrix {} is not bound to slot {slot}",
                        matrix.index()
                    )));
                }
            }
        }
        Ok(())
    }
}

#[derive(Copy, Clone, Debug)]
enum LoweringFailure {
    EventLeaf,
    ParameterLeaf,
}

enum LoweringOutcome<T> {
    Lowered(T),
    Unsupported(LoweringFailure),
}

struct LoweredKernel {
    values: Vec<KernelValue>,
    value_ids: Vec<Option<KernelValueId>>,
}

trait LoweringBoundary {
    fn context(&self) -> &'static str;

    fn scheduled_context(&self) -> &'static str {
        self.context()
    }

    fn cached(&self, _id: ExprId) -> Option<KernelInstruction> {
        None
    }

    fn parameter(&mut self, id: ExprId) -> CompileResult<LoweringOutcome<KernelInstruction>>;

    fn event(&mut self, id: ExprId) -> LoweringOutcome<KernelInstruction>;

    fn component(
        &mut self,
        id: ExprId,
        input: ExprId,
        index: usize,
        operand: &dyn Fn(ExprId) -> CompileResult<KernelValueId>,
    ) -> CompileResult<LoweringOutcome<KernelInstruction>>;
}

struct ScalarBoundary<'a> {
    cache_slots: &'a [Option<usize>],
    parameter_slots: &'a [Option<ParamId>],
    solve_components: &'a [Option<SolveComponentPlan>],
    solve_rhs_elements: &'a [Option<Vec<ExprId>>],
}

impl LoweringBoundary for ScalarBoundary<'_> {
    fn context(&self) -> &'static str {
        "node"
    }

    fn scheduled_context(&self) -> &'static str {
        "scheduled node"
    }

    fn cached(&self, id: ExprId) -> Option<KernelInstruction> {
        self.cache_slots[id.index()].map(KernelInstruction::Cached)
    }

    fn parameter(&mut self, id: ExprId) -> CompileResult<LoweringOutcome<KernelInstruction>> {
        let parameter = self.parameter_slots[id.index()].ok_or_else(|| {
            crate::CompileError::InvalidExecutablePlan(format!(
                "parameter node {} is not bound",
                id.index()
            ))
        })?;
        Ok(LoweringOutcome::Lowered(KernelInstruction::Parameter(
            parameter,
        )))
    }

    fn event(&mut self, _id: ExprId) -> LoweringOutcome<KernelInstruction> {
        LoweringOutcome::Unsupported(LoweringFailure::EventLeaf)
    }

    fn component(
        &mut self,
        id: ExprId,
        input: ExprId,
        index: usize,
        operand: &dyn Fn(ExprId) -> CompileResult<KernelValueId>,
    ) -> CompileResult<LoweringOutcome<KernelInstruction>> {
        let instruction = if let Some(solve) = self.solve_components[id.index()]
            && let Some(elements) = self.solve_rhs_elements[solve.rhs.index()].as_ref()
        {
            KernelInstruction::SolveRow {
                row_slot: solve.row_slot,
                rhs: elements
                    .iter()
                    .map(|element| operand(*element))
                    .collect::<CompileResult<_>>()?,
            }
        } else {
            KernelInstruction::Component {
                input: operand(input)?,
                index,
            }
        };
        Ok(LoweringOutcome::Lowered(instruction))
    }
}

#[derive(Default)]
struct CacheBoundary {
    inputs: Vec<ExprId>,
}

impl LoweringBoundary for CacheBoundary {
    fn context(&self) -> &'static str {
        "cache node"
    }

    fn parameter(&mut self, _id: ExprId) -> CompileResult<LoweringOutcome<KernelInstruction>> {
        Ok(LoweringOutcome::Unsupported(LoweringFailure::ParameterLeaf))
    }

    fn event(&mut self, id: ExprId) -> LoweringOutcome<KernelInstruction> {
        let slot = self.inputs.len();
        self.inputs.push(id);
        LoweringOutcome::Lowered(KernelInstruction::Cached(slot))
    }

    fn component(
        &mut self,
        _id: ExprId,
        input: ExprId,
        index: usize,
        operand: &dyn Fn(ExprId) -> CompileResult<KernelValueId>,
    ) -> CompileResult<LoweringOutcome<KernelInstruction>> {
        Ok(LoweringOutcome::Lowered(KernelInstruction::Component {
            input: operand(input)?,
            index,
        }))
    }
}

struct KernelLowerer<'a> {
    model: &'a CompiledModel,
    nodes: &'a [ExprId],
}

impl<'a> KernelLowerer<'a> {
    fn new(model: &'a CompiledModel, nodes: &'a [ExprId]) -> Self {
        Self { model, nodes }
    }

    fn lower(
        self,
        boundary: &mut impl LoweringBoundary,
    ) -> CompileResult<LoweringOutcome<LoweredKernel>> {
        let mut value_ids = vec![None; self.model.graph().nodes().len()];
        let mut values = Vec::with_capacity(self.nodes.len());
        for id in self.nodes {
            let index = id.index();
            let context = boundary.context();
            let scheduled_context = boundary.scheduled_context();
            let operand = |child: ExprId| {
                value_ids
                    .get(child.index())
                    .copied()
                    .flatten()
                    .ok_or_else(|| {
                        crate::CompileError::InvalidExecutablePlan(format!(
                            "{} {index} depends on unscheduled node {}",
                            context,
                            child.index()
                        ))
                    })
            };
            let instruction = if let Some(instruction) = boundary.cached(*id) {
                LoweringOutcome::Lowered(instruction)
            } else {
                let node = self.model.graph().node(*id).ok_or_else(|| {
                    crate::CompileError::InvalidExecutablePlan(format!(
                        "{} {index} is out of bounds",
                        scheduled_context
                    ))
                })?;
                match node {
                    ExprNode::RealConst(value) => {
                        LoweringOutcome::Lowered(KernelInstruction::RealConstant(*value))
                    }
                    ExprNode::ComplexConst(value) => {
                        LoweringOutcome::Lowered(KernelInstruction::ComplexConstant(*value))
                    }
                    ExprNode::ScalarParam(_) => boundary.parameter(*id)?,
                    ExprNode::EventScalar(_) | ExprNode::EventP4Component { .. } => {
                        boundary.event(*id)
                    }
                    ExprNode::Unary { op, input } => {
                        LoweringOutcome::Lowered(KernelInstruction::Unary {
                            op: *op,
                            input: operand(*input)?,
                        })
                    }
                    ExprNode::Binary { op, lhs, rhs } => {
                        LoweringOutcome::Lowered(KernelInstruction::Binary {
                            op: *op,
                            lhs: operand(*lhs)?,
                            rhs: operand(*rhs)?,
                        })
                    }
                    ExprNode::NaryAdd { terms } => LoweringOutcome::Lowered(
                        KernelInstruction::Add(Self::operands(terms, &operand)?),
                    ),
                    ExprNode::NaryMul { factors } => LoweringOutcome::Lowered(
                        KernelInstruction::Mul(Self::operands(factors, &operand)?),
                    ),
                    ExprNode::Complex { re, im } => {
                        LoweringOutcome::Lowered(KernelInstruction::Complex {
                            re: operand(*re)?,
                            im: operand(*im)?,
                        })
                    }
                    ExprNode::Vector { elements } => LoweringOutcome::Lowered(
                        KernelInstruction::Vector(Self::operands(elements, &operand)?),
                    ),
                    ExprNode::Matrix {
                        rows,
                        cols,
                        elements,
                    } => LoweringOutcome::Lowered(KernelInstruction::Matrix {
                        rows: *rows,
                        cols: *cols,
                        elements: Self::operands(elements, &operand)?,
                    }),
                    ExprNode::Component { input, index } => {
                        boundary.component(*id, *input, *index, &operand)?
                    }
                    ExprNode::MatrixElement { input, row, col } => {
                        LoweringOutcome::Lowered(KernelInstruction::MatrixElement {
                            input: operand(*input)?,
                            row: *row,
                            col: *col,
                        })
                    }
                    ExprNode::MatMul { lhs, rhs } => {
                        LoweringOutcome::Lowered(KernelInstruction::MatMul {
                            lhs: operand(*lhs)?,
                            rhs: operand(*rhs)?,
                        })
                    }
                    ExprNode::MatVec { matrix, vector } => {
                        LoweringOutcome::Lowered(KernelInstruction::MatVec {
                            matrix: operand(*matrix)?,
                            vector: operand(*vector)?,
                        })
                    }
                    ExprNode::Dot { lhs, rhs } => {
                        LoweringOutcome::Lowered(KernelInstruction::Dot {
                            lhs: operand(*lhs)?,
                            rhs: operand(*rhs)?,
                        })
                    }
                    ExprNode::Solve { matrix, rhs } => {
                        LoweringOutcome::Lowered(KernelInstruction::Solve {
                            matrix: operand(*matrix)?,
                            rhs: operand(*rhs)?,
                        })
                    }
                }
            };
            let instruction = match instruction {
                LoweringOutcome::Lowered(instruction) => instruction,
                LoweringOutcome::Unsupported(reason) => {
                    return Ok(LoweringOutcome::Unsupported(reason));
                }
            };
            let facts = self.model.node_facts(*id).ok_or_else(|| {
                crate::CompileError::InvalidExecutablePlan(format!(
                    "facts for {} {index} are missing",
                    scheduled_context
                ))
            })?;
            let kind = match facts.value_kind {
                ValueKind::Real => KernelValueKind::Real,
                ValueKind::Complex => KernelValueKind::Complex,
                ValueKind::Vector { len } => KernelValueKind::Vector { len },
                ValueKind::Matrix { rows, cols } => KernelValueKind::Matrix { rows, cols },
            };
            let class = if facts.dependency.depends_on_event {
                KernelValueClass::Event
            } else {
                KernelValueClass::Invariant
            };
            let kernel_id = KernelValueId::from_index(values.len());
            values.push(KernelValue {
                kind,
                class,
                instruction,
            });
            value_ids[index] = Some(kernel_id);
        }
        Ok(LoweringOutcome::Lowered(LoweredKernel {
            values,
            value_ids,
        }))
    }

    fn operands(
        ids: &[ExprId],
        operand: &dyn Fn(ExprId) -> CompileResult<KernelValueId>,
    ) -> CompileResult<Vec<KernelValueId>> {
        ids.iter().map(|id| operand(*id)).collect()
    }
}

#[cfg(test)]
mod tests {
    use laddu_expr::{
        Expr, P4Component, event_p4_component, event_scalar, matrix, matvec, parameter, solve,
        vector,
    };

    use super::*;

    #[test]
    fn executable_plan_lowers_scalar_kernel_and_cache_boundary() {
        let model = CompiledModel::from_expr(
            &(event_scalar("x") * parameter!("scale") + parameter!("offset")),
        )
        .unwrap();
        let plan = ExecutablePlan::from_model(&model).unwrap();

        assert_eq!(plan.params().n_free(), 2);
        assert!(!plan.cache_plan().is_empty());
        assert_eq!(
            plan.cache_inputs().unwrap(),
            vec![CacheInput::Scalar("x".into())]
        );
        let kernel = plan.scalar_kernel().unwrap();
        assert!(
            kernel
                .values()
                .iter()
                .any(|value| matches!(value.instruction, KernelInstruction::Cached(_)))
        );
        assert!(
            kernel
                .values()
                .iter()
                .any(|value| matches!(value.instruction, KernelInstruction::Parameter(_)))
        );
    }

    #[test]
    fn executable_plan_exposes_typed_p4_cache_inputs() {
        let model = CompiledModel::from_expr(&event_p4_component("p", P4Component::Py)).unwrap();
        let plan = ExecutablePlan::from_model(&model).unwrap();

        assert_eq!(
            plan.cache_inputs().unwrap(),
            vec![CacheInput::P4("p".into(), P4Component::Py)]
        );
    }

    #[test]
    fn executable_plan_lowers_computed_scalar_cache_kernel() {
        let model = CompiledModel::from_expr(&event_scalar("x").sin()).unwrap();
        let plan = ExecutablePlan::from_model(&model).unwrap();
        let kernel = plan.cache_kernel().unwrap();

        assert_eq!(plan.cache_input_nodes().len(), 1);
        assert_eq!(kernel.outputs().len(), model.cache_plan().len());
        assert!(kernel.values().iter().any(|value| {
            matches!(
                value.instruction,
                KernelInstruction::Unary {
                    op: laddu_expr::UnaryOp::Sin,
                    ..
                }
            )
        }));
    }

    #[test]
    fn executable_plan_lowers_aggregate_cache_kernel() {
        let cached_matrix = matrix([
            [event_scalar("x"), event_scalar("y")],
            [event_scalar("y") + 1.0, event_scalar("x") - 1.0],
        ]);
        let expression =
            matvec(cached_matrix, vector([parameter!("a"), parameter!("b")])).component(1);
        let model = CompiledModel::from_expr_with_options(
            &expression,
            &crate::CompileOptions::without_optimizations(),
        )
        .unwrap();
        let plan = ExecutablePlan::from_model(&model).unwrap();
        let cache = plan.cache_kernel().unwrap();

        assert!(cache.values().iter().any(|value| {
            matches!(
                value.instruction,
                KernelInstruction::Matrix {
                    rows: 2,
                    cols: 2,
                    ..
                }
            ) && value.kind == KernelValueKind::Matrix { rows: 2, cols: 2 }
        }));
        assert!(plan.scalar_kernel().unwrap().values().iter().any(|value| {
            matches!(value.instruction, KernelInstruction::Cached(_))
                && value.kind == KernelValueKind::Matrix { rows: 2, cols: 2 }
        }));
        assert!(plan.scalar_kernel().unwrap().values().iter().any(|value| {
            matches!(value.instruction, KernelInstruction::MatVec { .. })
                && value.kind == KernelValueKind::Vector { len: 2 }
        }));
    }

    #[test]
    fn executable_plan_specializes_selected_event_solve_rows() {
        let expression = solve(
            matrix([[event_scalar("x") + 2.0]]),
            vector([parameter!("rhs")]),
        )
        .component(0);
        let model = CompiledModel::from_expr(&expression).unwrap();
        let plan = ExecutablePlan::from_model(&model).unwrap();

        assert_eq!(plan.solve_row_matrices().len(), 1);
        assert_eq!(plan.solve_row_matrices()[0].dimension(), 1);
        assert_eq!(plan.solve_row_keys().len(), 1);
        assert!(
            plan.scalar_kernel()
                .unwrap()
                .values()
                .iter()
                .any(|value| matches!(
                    value.instruction,
                    KernelInstruction::SolveRow { row_slot: 0, .. }
                ))
        );
    }

    #[test]
    fn executable_plan_preserves_generic_fallback_without_caches() {
        let model = CompiledModel::from_expr_with_options(
            &event_scalar("x"),
            &crate::CompileOptions::default().with_cache_policy(crate::CachePolicy::Off),
        )
        .unwrap();
        let plan = ExecutablePlan::from_model(&model).unwrap();

        assert!(plan.scalar_kernel().is_none());
    }

    #[test]
    fn scalar_and_cache_boundaries_share_common_node_lowering() {
        let expression = (vector([Expr::from(1.0), Expr::from(2.0)]).component(1) + 3.0).sin();
        let model = CompiledModel::from_expr_with_options(
            &expression,
            &crate::CompileOptions::without_optimizations()
                .with_cache_policy(crate::CachePolicy::Off),
        )
        .unwrap();
        let nodes = (0..model.graph().nodes().len())
            .map(ExprId::from_index)
            .collect::<Vec<_>>();
        let cache_slots = vec![None; nodes.len()];
        let parameter_slots = vec![None; nodes.len()];
        let solve = SolvePlan::empty(nodes.len());

        let mut scalar_boundary = ScalarBoundary {
            cache_slots: &cache_slots,
            parameter_slots: &parameter_slots,
            solve_components: &solve.components,
            solve_rhs_elements: &solve.rhs_elements,
        };
        let LoweringOutcome::Lowered(scalar) = KernelLowerer::new(&model, &nodes)
            .lower(&mut scalar_boundary)
            .unwrap()
        else {
            panic!("common scalar lowering should be supported");
        };

        let mut cache_boundary = CacheBoundary::default();
        let LoweringOutcome::Lowered(cache) = KernelLowerer::new(&model, &nodes)
            .lower(&mut cache_boundary)
            .unwrap()
        else {
            panic!("common cache lowering should be supported");
        };

        assert!(cache_boundary.inputs.is_empty());
        assert_eq!(scalar.values.len(), cache.values.len());
        for (scalar, cache) in scalar.values.iter().zip(&cache.values) {
            assert_eq!(scalar.kind, cache.kind);
            assert_eq!(scalar.class, cache.class);
            assert_eq!(
                format!("{:?}", scalar.instruction),
                format!("{:?}", cache.instruction)
            );
        }
    }
}