vyre-foundation 0.6.1

Foundation layer: IR, type system, memory model, wire format. Zero application semantics. Part of the vyre GPU compiler.
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
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
//! GPU-resident e-graph substrate.
//!
//! The CPU-side `eqsat::EGraph` materialises rewrite candidates,
//! union-find merges, and cost-based extraction in a single
//! sequential walker. For wide rewrite families (algebraic
//! identities, peephole tables, pattern-match-heavy primitives)
//! the per-iteration cost grows with the e-graph size, and the
//! hash-cons table becomes the bottleneck. This module ships the
//! GPU-resident representation: a flattened, columnar mirror of
//! the EGraph that can be uploaded to a GPU buffer and walked in
//! parallel by warp-cooperative passes.
//!
//! The mirror is additive: CPU passes keep using `EGraph::saturate`,
//! while GPU-aware passes use `GpuEGraphSnapshot::from_egraph_with`
//! to materialise the columnar arrays and merge discovered equivalences
//! back through `apply_equivalences_to_egraph`.
//!
//! Soundness: the snapshot is read-only. Any equivalence the GPU
//! discovers is merged through the same `EGraph::merge` API the
//! CPU uses, so the EGraph's saturation invariants hold by
//! construction.
//!
//! ## Why the columnar layout
//!
//! Each row of the snapshot is `(eclass_id, language_op_id,
//! children_offset, children_len)`. The children indices live in
//! a separate `children: Vec<u32>` column. This layout fits a
//! GPU's coalesced-memory access pattern: a warp reading 32
//! consecutive rows touches one cache line per column (4 columns
//! × 4 bytes × 32 lanes = 512 bytes per warp).

use rustc_hash::{FxHashMap, FxHashSet};
use std::fmt;
use std::sync::Arc;

use super::eqsat::{EClassId, EGraph, ENodeLang};

/// GPU-resident snapshot row: one entry per node in the e-graph.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
pub struct SnapshotRow {
    /// E-class id this node belongs to (post-canonicalisation).
    pub eclass_id: u32,
    /// Stable language-op id (e.g. `BinOp::Add` → 1, `Load` → 2).
    /// The `OpIdRegistry` maintains the assignment.
    pub language_op_id: u32,
    /// Offset into the snapshot's `children` column where this
    /// node's child eclass ids start.
    pub children_offset: u32,
    /// Number of children (consecutive in the `children` column).
    pub children_len: u32,
}

/// One discovered equivalence (e-class merge candidate) produced by a
/// saturation pass. The CPU merges these back into the `EGraph`.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
pub struct Equivalence {
    /// Left e-class id.
    pub left: u32,
    /// Right e-class id (to be merged with left).
    pub right: u32,
}

/// Columnar GPU-uploadable mirror of an e-graph.
#[derive(Clone, Debug, Default)]
pub struct GpuEGraphSnapshot {
    /// Per-node rows in `(eclass_id, language_op_id, offset, len)` form.
    pub rows: Vec<SnapshotRow>,
    /// Flat children column. `rows[i]` references children at
    /// `children[rows[i].children_offset..rows[i].children_offset + rows[i].children_len]`.
    pub children: Vec<u32>,
    /// Op-id assignment used by `language_op_id`. Stable for the
    /// life of the snapshot.
    pub op_ids: OpIdRegistry,
}

/// Error returned when a CPU e-graph cannot be represented by the current
/// 32-bit GPU snapshot ABI.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct GpuEGraphSnapshotError {
    context: &'static str,
    value: usize,
}

impl GpuEGraphSnapshotError {
    fn new(context: &'static str, value: usize) -> Self {
        Self { context, value }
    }

    /// Human-readable conversion context.
    #[must_use]
    pub const fn context(&self) -> &'static str {
        self.context
    }

    /// Host-side value that could not fit the GPU snapshot ABI.
    #[must_use]
    pub const fn value(&self) -> usize {
        self.value
    }
}

impl fmt::Display for GpuEGraphSnapshotError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "GPU e-graph snapshot {} value {} exceeds the u32 column ABI. Fix: shard the e-graph snapshot or widen the GPU snapshot ABI before upload.",
            self.context, self.value
        )
    }
}

impl std::error::Error for GpuEGraphSnapshotError {}

/// Error returned when a GPU e-graph snapshot is structurally malformed.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct GpuEGraphSnapshotIntegrityError {
    context: &'static str,
    row: usize,
    value: u32,
}

impl GpuEGraphSnapshotIntegrityError {
    fn new(context: &'static str, row: usize, value: u32) -> Self {
        Self {
            context,
            row,
            value,
        }
    }

    /// Human-readable validation context.
    #[must_use]
    pub const fn context(&self) -> &'static str {
        self.context
    }

    /// Snapshot row that failed validation.
    #[must_use]
    pub const fn row(&self) -> usize {
        self.row
    }

    /// Row-local value that failed validation.
    #[must_use]
    pub const fn value(&self) -> u32 {
        self.value
    }
}

impl fmt::Display for GpuEGraphSnapshotIntegrityError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "GPU e-graph snapshot integrity error at row {}: {} value {} is invalid. Fix: rebuild the snapshot from canonical e-graph rows before upload.",
            self.row, self.context, self.value
        )
    }
}

impl std::error::Error for GpuEGraphSnapshotIntegrityError {}

/// Error returned when a GPU e-graph snapshot cannot be packed for upload.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum GpuEGraphDeviceImageError {
    /// Snapshot rows are structurally malformed.
    Integrity(GpuEGraphSnapshotIntegrityError),
    /// Snapshot or derived index columns exceed the current u32 device ABI.
    Layout(GpuEGraphSnapshotError),
}

impl fmt::Display for GpuEGraphDeviceImageError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Integrity(error) => error.fmt(f),
            Self::Layout(error) => error.fmt(f),
        }
    }
}

impl std::error::Error for GpuEGraphDeviceImageError {}

impl From<GpuEGraphSnapshotIntegrityError> for GpuEGraphDeviceImageError {
    fn from(error: GpuEGraphSnapshotIntegrityError) -> Self {
        Self::Integrity(error)
    }
}

impl From<GpuEGraphSnapshotError> for GpuEGraphDeviceImageError {
    fn from(error: GpuEGraphSnapshotError) -> Self {
        Self::Layout(error)
    }
}

/// Contiguous span inside a packed GPU e-graph device image.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct GpuEGraphDeviceSpan {
    offset: usize,
    len: usize,
}

impl GpuEGraphDeviceSpan {
    const fn new(offset: usize, len: usize) -> Self {
        Self { offset, len }
    }

    /// Word offset of the span inside the packed u32 slab.
    #[must_use]
    pub const fn offset(&self) -> usize {
        self.offset
    }

    /// Number of u32 words in the span.
    #[must_use]
    pub const fn len(&self) -> usize {
        self.len
    }

    /// `true` iff the span contains no words.
    #[must_use]
    pub const fn is_empty(&self) -> bool {
        self.len == 0
    }

    fn slice<'a>(&self, words: &'a [u32]) -> &'a [u32] {
        &words[self.offset..self.offset + self.len]
    }
}

/// Segment table for a packed GPU e-graph device image.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct GpuEGraphDeviceLayout {
    row_count: usize,
    child_count: usize,
    eclass_group_count: usize,
    row_eclass_ids: GpuEGraphDeviceSpan,
    row_language_op_ids: GpuEGraphDeviceSpan,
    row_children_offsets: GpuEGraphDeviceSpan,
    row_children_lens: GpuEGraphDeviceSpan,
    row_signatures: GpuEGraphDeviceSpan,
    children: GpuEGraphDeviceSpan,
    group_eclass_ids: GpuEGraphDeviceSpan,
    group_offsets: GpuEGraphDeviceSpan,
    group_rows: GpuEGraphDeviceSpan,
}

impl GpuEGraphDeviceLayout {
    /// Number of snapshot rows packed into the device image.
    #[must_use]
    pub const fn row_count(&self) -> usize {
        self.row_count
    }

    /// Number of child references packed into the device image.
    #[must_use]
    pub const fn child_count(&self) -> usize {
        self.child_count
    }

    /// Number of e-class row groups in the device image.
    #[must_use]
    pub const fn eclass_group_count(&self) -> usize {
        self.eclass_group_count
    }

    /// Span containing one e-class id per snapshot row.
    #[must_use]
    pub const fn row_eclass_ids(&self) -> GpuEGraphDeviceSpan {
        self.row_eclass_ids
    }

    /// Span containing one language op id per snapshot row.
    #[must_use]
    pub const fn row_language_op_ids(&self) -> GpuEGraphDeviceSpan {
        self.row_language_op_ids
    }

    /// Span containing one child-column offset per snapshot row.
    #[must_use]
    pub const fn row_children_offsets(&self) -> GpuEGraphDeviceSpan {
        self.row_children_offsets
    }

    /// Span containing one child count per snapshot row.
    #[must_use]
    pub const fn row_children_lens(&self) -> GpuEGraphDeviceSpan {
        self.row_children_lens
    }

    /// Span containing one structural row signature per snapshot row.
    #[must_use]
    pub const fn row_signatures(&self) -> GpuEGraphDeviceSpan {
        self.row_signatures
    }

    /// Span containing the flat child e-class column.
    #[must_use]
    pub const fn children(&self) -> GpuEGraphDeviceSpan {
        self.children
    }

    /// Span containing sorted e-class ids for row groups.
    #[must_use]
    pub const fn group_eclass_ids(&self) -> GpuEGraphDeviceSpan {
        self.group_eclass_ids
    }

    /// Span containing prefix offsets into [`Self::group_rows`].
    #[must_use]
    pub const fn group_offsets(&self) -> GpuEGraphDeviceSpan {
        self.group_offsets
    }

    /// Span containing row indices grouped by e-class.
    #[must_use]
    pub const fn group_rows(&self) -> GpuEGraphDeviceSpan {
        self.group_rows
    }
}

/// Validated, single-slab u32 image ready for backend upload.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct GpuEGraphDeviceImage {
    words: Vec<u32>,
    layout: GpuEGraphDeviceLayout,
}

impl GpuEGraphDeviceImage {
    /// Packed u32 words. Backends can upload this slab with one host-to-device copy.
    #[must_use]
    pub fn words(&self) -> &[u32] {
        &self.words
    }

    /// Segment table describing the packed word slab.
    #[must_use]
    pub const fn layout(&self) -> GpuEGraphDeviceLayout {
        self.layout
    }

    /// One e-class id per snapshot row.
    #[must_use]
    pub fn row_eclass_ids(&self) -> &[u32] {
        self.layout.row_eclass_ids.slice(&self.words)
    }

    /// One language op id per snapshot row.
    #[must_use]
    pub fn row_language_op_ids(&self) -> &[u32] {
        self.layout.row_language_op_ids.slice(&self.words)
    }

    /// One child-column offset per snapshot row.
    #[must_use]
    pub fn row_children_offsets(&self) -> &[u32] {
        self.layout.row_children_offsets.slice(&self.words)
    }

    /// One child count per snapshot row.
    #[must_use]
    pub fn row_children_lens(&self) -> &[u32] {
        self.layout.row_children_lens.slice(&self.words)
    }

    /// One structural signature per snapshot row.
    #[must_use]
    pub fn row_signatures(&self) -> &[u32] {
        self.layout.row_signatures.slice(&self.words)
    }

    /// Flat child e-class column.
    #[must_use]
    pub fn children(&self) -> &[u32] {
        self.layout.children.slice(&self.words)
    }

    /// Sorted e-class ids for row groups.
    #[must_use]
    pub fn group_eclass_ids(&self) -> &[u32] {
        self.layout.group_eclass_ids.slice(&self.words)
    }

    /// Prefix offsets into [`Self::group_rows`].
    #[must_use]
    pub fn group_offsets(&self) -> &[u32] {
        self.layout.group_offsets.slice(&self.words)
    }

    /// Row indices grouped by e-class.
    #[must_use]
    pub fn group_rows(&self) -> &[u32] {
        self.layout.group_rows.slice(&self.words)
    }
}

/// Stable language-op id assignment used inside snapshot rows.
#[derive(Clone, Debug, Default)]
pub struct OpIdRegistry {
    by_name: FxHashMap<Arc<str>, u32>,
    names: Vec<Arc<str>>,
}

impl OpIdRegistry {
    /// Intern a language-op name and return its stable id.
    /// Repeated calls with the same name return the same id.
    pub fn intern(&mut self, name: &str) -> u32 {
        self.try_intern(name)
            .unwrap_or_else(|error| panic!("{error}"))
    }

    /// Fallible form of [`Self::intern`] for GPU snapshot builders that must
    /// reject over-wide op dictionaries instead of silently saturating ids.
    pub fn try_intern(&mut self, name: &str) -> Result<u32, GpuEGraphSnapshotError> {
        if let Some(&id) = self.by_name.get(name) {
            return Ok(id);
        }
        let id = u32_len(self.names.len(), "op-id registry")?;
        let name: Arc<str> = Arc::from(name);
        self.names.push(Arc::clone(&name));
        self.by_name.insert(name, id);
        Ok(id)
    }

    /// Resolve an op-id back to its name, or `None` if unknown.
    #[must_use]
    pub fn name_of(&self, id: u32) -> Option<&str> {
        self.names.get(id as usize).map(AsRef::as_ref)
    }

    /// Number of registered op names.
    #[must_use]
    pub fn len(&self) -> usize {
        self.names.len()
    }

    /// `true` iff zero op names registered.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.names.is_empty()
    }
}

impl GpuEGraphSnapshot {
    /// Build a snapshot from a sequence of `(eclass_id, op_name,
    /// children: &[u32])` triples. Caller-driven construction so
    /// this module doesn't depend on the exact `eqsat::EGraph`
    /// internal shape; the `EGraph` crate's adapter calls this
    /// builder to materialise the GPU mirror.
    #[must_use]
    pub fn build<'a, I>(rows: I) -> Self
    where
        I: IntoIterator<Item = (u32, &'a str, &'a [u32])>,
    {
        Self::try_build(rows).unwrap_or_else(|error| panic!("{error}"))
    }

    /// Fallible form of [`Self::build`] that rejects snapshots too large for
    /// the current 32-bit GPU column ABI.
    pub fn try_build<'a, I>(rows: I) -> Result<Self, GpuEGraphSnapshotError>
    where
        I: IntoIterator<Item = (u32, &'a str, &'a [u32])>,
    {
        let mut snapshot = Self::default();
        let rows = rows.into_iter();
        let (lower_bound, _) = rows.size_hint();
        snapshot.rows.reserve(lower_bound);
        for (eclass_id, op_name, kids) in rows {
            let language_op_id = snapshot.op_ids.try_intern(op_name)?;
            let children_offset = u32_len(snapshot.children.len(), "GPU egraph children offset")?;
            let children_len = u32_len(kids.len(), "GPU egraph row child count")?;
            snapshot.children.extend_from_slice(kids);
            snapshot.rows.push(SnapshotRow {
                eclass_id,
                language_op_id,
                children_offset,
                children_len,
            });
        }
        Ok(snapshot)
    }

    /// Materialise a snapshot directly from the CPU `EGraph`.
    ///
    /// The caller supplies the stable operation-name projection because
    /// `ENodeLang` is intentionally domain-generic and does not require
    /// `Debug` or a string identity. Child ids are canonicalized during the
    /// copy so the GPU columns match the CPU graph's current union-find state.
    #[must_use]
    pub fn from_egraph_with<L, F, S>(egraph: &EGraph<L>, mut op_name: F) -> Self
    where
        L: ENodeLang,
        F: FnMut(&L) -> S,
        S: AsRef<str>,
    {
        Self::try_from_egraph_with(egraph, &mut op_name).unwrap_or_else(|error| panic!("{error}"))
    }

    /// Fallible form of [`Self::from_egraph_with`] that rejects CPU e-graphs
    /// whose node or child-column counts exceed the current 32-bit GPU ABI.
    pub fn try_from_egraph_with<L, F, S>(
        egraph: &EGraph<L>,
        mut op_name: F,
    ) -> Result<Self, GpuEGraphSnapshotError>
    where
        L: ENodeLang,
        F: FnMut(&L) -> S,
        S: AsRef<str>,
    {
        let mut snapshot = Self::default();
        snapshot.rows.reserve(egraph.class_count());
        for (eclass_id, node) in egraph.iter_nodes() {
            let language_op_id = snapshot.op_ids.try_intern(op_name(node).as_ref())?;
            let children = node.children();
            let children_offset = u32_len(snapshot.children.len(), "GPU egraph children offset")?;
            let children_len = u32_len(children.len(), "GPU egraph row child count")?;
            snapshot
                .children
                .extend(children.iter().map(|child| egraph.find_immut(*child).0));
            snapshot.rows.push(SnapshotRow {
                eclass_id: egraph.find_immut(eclass_id).0,
                language_op_id,
                children_offset,
                children_len,
            });
        }
        Ok(snapshot)
    }

    /// Number of nodes in the snapshot.
    #[must_use]
    pub fn node_count(&self) -> usize {
        self.rows.len()
    }

    /// `true` iff the snapshot contains no nodes.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.rows.is_empty()
    }

    /// Total number of children references across all rows.
    #[must_use]
    pub fn child_count(&self) -> usize {
        self.children.len()
    }

    /// Children of the row at `row_idx`, or `None` if the snapshot row
    /// references an invalid range.
    #[must_use]
    pub fn children_of(&self, row_idx: usize) -> Option<&[u32]> {
        let row = self.rows.get(row_idx)?;
        let start = row.children_offset as usize;
        let end = start.checked_add(row.children_len as usize)?;
        self.children.get(start..end)
    }

    /// Group rows by their `eclass_id`, returning a map of
    /// `eclass_id → Vec<row_idx>`. Useful for the GPU saturation
    /// kernel's per-eclass passes.
    #[must_use]
    pub fn rows_by_eclass(&self) -> FxHashMap<u32, Vec<usize>> {
        let mut out: FxHashMap<u32, Vec<usize>> =
            FxHashMap::with_capacity_and_hasher(self.rows.len(), Default::default());
        for (i, row) in self.rows.iter().enumerate() {
            out.entry(row.eclass_id).or_default().push(i);
        }
        out
    }

    /// Validate that the columnar snapshot is safe to upload to a GPU kernel.
    ///
    /// This checks every row's operation id, child-column range, and child
    /// e-class references. It is intentionally stricter than construction:
    /// callers may still build partial test fixtures, but production upload
    /// paths can require this gate before device execution.
    ///
    /// # Errors
    ///
    /// Returns [`GpuEGraphSnapshotIntegrityError`] when a row references an
    /// unknown op id, points outside the child column, or names a child e-class
    /// not present in the snapshot.
    pub fn validate_integrity(&self) -> Result<(), GpuEGraphSnapshotIntegrityError> {
        let mut eclasses: FxHashSet<u32> =
            FxHashSet::with_capacity_and_hasher(self.rows.len(), Default::default());
        for row in &self.rows {
            eclasses.insert(row.eclass_id);
        }
        for (row_idx, row) in self.rows.iter().enumerate() {
            if self.op_ids.name_of(row.language_op_id).is_none() {
                return Err(GpuEGraphSnapshotIntegrityError::new(
                    "unknown language_op_id",
                    row_idx,
                    row.language_op_id,
                ));
            }
            let start = row.children_offset as usize;
            let end = start
                .checked_add(row.children_len as usize)
                .ok_or_else(|| {
                    GpuEGraphSnapshotIntegrityError::new(
                        "children range overflow",
                        row_idx,
                        row.children_len,
                    )
                })?;
            if end > self.children.len() {
                return Err(GpuEGraphSnapshotIntegrityError::new(
                    "children range end",
                    row_idx,
                    row.children_len,
                ));
            }
            for &child in &self.children[start..end] {
                if !eclasses.contains(&child) {
                    return Err(GpuEGraphSnapshotIntegrityError::new(
                        "dangling child eclass",
                        row_idx,
                        child,
                    ));
                }
            }
        }
        Ok(())
    }

    /// Pack the validated snapshot into one backend-uploadable u32 slab.
    ///
    /// The image contains row metadata columns, a structural row-signature
    /// column, the flat child column, and a deterministic e-class-to-row prefix
    /// index. CUDA and other backends can upload the returned
    /// [`GpuEGraphDeviceImage::words`] slice in one copy and pass the spans
    /// from [`GpuEGraphDeviceImage::layout`] as kernel parameters.
    ///
    /// # Errors
    ///
    /// Returns [`GpuEGraphDeviceImageError`] if the snapshot fails integrity
    /// validation or if a derived row/group index exceeds the u32 device ABI.
    pub fn try_pack_device_image(&self) -> Result<GpuEGraphDeviceImage, GpuEGraphDeviceImageError> {
        self.validate_integrity()?;

        let mut groups: FxHashMap<u32, Vec<u32>> =
            FxHashMap::with_capacity_and_hasher(self.rows.len(), Default::default());
        for (row_idx, row) in self.rows.iter().enumerate() {
            groups
                .entry(row.eclass_id)
                .or_default()
                .push(u32_len(row_idx, "GPU egraph grouped row index")?);
        }

        let mut group_eclass_ids = groups.keys().copied().collect::<Vec<_>>();
        group_eclass_ids.sort_unstable();

        let mut group_offsets = Vec::with_capacity(group_eclass_ids.len() + 1);
        let mut group_rows = Vec::with_capacity(self.rows.len());
        for eclass_id in &group_eclass_ids {
            group_offsets.push(u32_len(group_rows.len(), "GPU egraph group row offset")?);
            let Some(rows) = groups.get(eclass_id) else {
                return Err(GpuEGraphSnapshotIntegrityError::new(
                    "missing grouped eclass key",
                    0,
                    *eclass_id,
                )
                .into());
            };
            group_rows.extend_from_slice(rows);
        }
        group_offsets.push(u32_len(
            group_rows.len(),
            "GPU egraph group row terminal offset",
        )?);

        let row_signatures = self
            .rows
            .iter()
            .map(|row| {
                let start = row.children_offset as usize;
                let end = start + row.children_len as usize;
                egraph_row_signature(row, &self.children[start..end])
            })
            .collect::<Vec<_>>();
        let mut words = Vec::with_capacity(
            self.rows.len() * 5
                + self.children.len()
                + group_eclass_ids.len()
                + group_offsets.len()
                + group_rows.len(),
        );
        let row_eclass_ids = append_words(&mut words, self.rows.iter().map(|row| row.eclass_id));
        let row_language_op_ids =
            append_words(&mut words, self.rows.iter().map(|row| row.language_op_id));
        let row_children_offsets =
            append_words(&mut words, self.rows.iter().map(|row| row.children_offset));
        let row_children_lens =
            append_words(&mut words, self.rows.iter().map(|row| row.children_len));
        let row_signatures = append_words(&mut words, row_signatures);
        let children = append_words(&mut words, self.children.iter().copied());
        let group_eclass_ids_span = append_words(&mut words, group_eclass_ids);
        let group_offsets = append_words(&mut words, group_offsets);
        let group_rows = append_words(&mut words, group_rows);

        Ok(GpuEGraphDeviceImage {
            words,
            layout: GpuEGraphDeviceLayout {
                row_count: self.rows.len(),
                child_count: self.children.len(),
                eclass_group_count: groups.len(),
                row_eclass_ids,
                row_language_op_ids,
                row_children_offsets,
                row_children_lens,
                row_signatures,
                children,
                group_eclass_ids: group_eclass_ids_span,
                group_offsets,
                group_rows,
            },
        })
    }

    /// Panic-on-error form of [`Self::try_pack_device_image`].
    #[must_use]
    pub fn pack_device_image(&self) -> GpuEGraphDeviceImage {
        self.try_pack_device_image()
            .unwrap_or_else(|error| panic!("{error}"))
    }
}

/// Report returned after applying discovered equivalences to an `EGraph`.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]

pub struct ApplyEquivalencesReport {
    /// Input equivalence count.
    pub requested: usize,
    /// Equivalences whose e-class ids existed in the target `EGraph`.
    pub valid: usize,
    /// Direct union operations that changed the union-find root.
    pub merged: usize,
    /// Additional unions discovered during `EGraph::rebuild`.
    pub rebuild_unions: usize,
}

/// Apply a batch of GPU-discovered equivalences to a CPU-side
/// merge sink. The `merger` closure receives `(left, right)` and
/// performs the canonical `EGraph` merge. Returns the number of
/// merges that actually changed the union-find state (the merger
/// returns `true` for a state-changing merge, `false` for a no-op
/// where left and right were already in the same e-class).
pub fn apply_equivalences<F>(equivalences: &[Equivalence], mut merger: F) -> usize
where
    F: FnMut(u32, u32) -> bool,
{
    let mut applied = 0usize;
    for eq in equivalences {
        if merger(eq.left, eq.right) {
            applied += 1;
        }
    }
    applied
}

/// Apply discovered equivalences to the CPU `EGraph` and rebuild it once.
///
/// Invalid e-class ids are counted as requested but not applied; user input
/// must not be able to panic the optimizer by returning an out-of-range merge.
pub fn apply_equivalences_to_egraph<L>(
    egraph: &mut EGraph<L>,
    equivalences: &[Equivalence],
) -> ApplyEquivalencesReport
where
    L: ENodeLang,
{
    let mut report = ApplyEquivalencesReport {
        requested: equivalences.len(),
        ..ApplyEquivalencesReport::default()
    };
    let Ok(class_count) = u32_len(egraph.class_count(), "CPU egraph class count") else {
        return report;
    };
    for eq in equivalences {
        if eq.left >= class_count || eq.right >= class_count {
            continue;
        }
        report.valid += 1;
        let left = EClassId(eq.left);
        let right = EClassId(eq.right);
        if egraph.find(left) != egraph.find(right) {
            egraph.union(left, right);
            report.merged += 1;
        }
    }
    report.rebuild_unions = egraph.rebuild();
    report
}

#[inline]
fn u32_len(value: usize, context: &'static str) -> Result<u32, GpuEGraphSnapshotError> {
    u32::try_from(value).map_err(|_| GpuEGraphSnapshotError::new(context, value))
}

fn append_words<I>(words: &mut Vec<u32>, values: I) -> GpuEGraphDeviceSpan
where
    I: IntoIterator<Item = u32>,
{
    let offset = words.len();
    words.extend(values);
    GpuEGraphDeviceSpan::new(offset, words.len() - offset)
}

/// Structural row signature for packed GPU e-graph columns.
///
/// Matches the CUDA row-signature refresh kernel and initial image packing.
#[must_use]
pub fn gpu_egraph_row_signature(language_op_id: u32, children_len: u32, children: &[u32]) -> u32 {
    let mut hash = mix_egraph_signature(0xA24B_AED4, language_op_id);
    hash = mix_egraph_signature(hash, children_len);
    for &child in children {
        hash = mix_egraph_signature(hash, child);
    }
    hash
}

fn egraph_row_signature(row: &SnapshotRow, children: &[u32]) -> u32 {
    gpu_egraph_row_signature(row.language_op_id, row.children_len, children)
}

fn mix_egraph_signature(hash: u32, value: u32) -> u32 {
    let mixed = hash
        ^ value
            .wrapping_add(0x9E37_79B9)
            .wrapping_add(hash << 6)
            .wrapping_add(hash >> 2);
    mixed.rotate_left(13).wrapping_mul(0x85EB_CA6B)
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::hash::Hash;

    #[derive(Clone, Debug, Eq, Hash, PartialEq)]
    enum TinyLang {
        Lit(u32),
        Add(EClassId, EClassId),
    }

    impl ENodeLang for TinyLang {
        fn children(&self) -> super::super::eqsat::EChildren {
            match self {
                Self::Lit(_) => super::super::eqsat::EChildren::new(),
                Self::Add(left, right) => [*left, *right].into_iter().collect(),
            }
        }

        fn with_children(&self, children: &[EClassId]) -> Self {
            match self {
                Self::Lit(value) => Self::Lit(*value),
                Self::Add(_, _) => Self::Add(children[0], children[1]),
            }
        }
    }

    /// Empty snapshot: zero rows, zero children, registry empty.
    #[test]
    fn empty_snapshot() {
        let snap = GpuEGraphSnapshot::default();
        assert!(snap.is_empty());
        assert_eq!(snap.node_count(), 0);
        assert_eq!(snap.child_count(), 0);
        assert!(snap.op_ids.is_empty());
    }

    /// Build a 3-node snapshot via the iterator builder; assert
    /// row layout + children column line up.
    #[test]
    fn build_three_node_snapshot() {
        let snap = GpuEGraphSnapshot::build([
            (0u32, "lit_u32", &[][..]),
            (1u32, "lit_u32", &[][..]),
            (2u32, "binop_add", &[0u32, 1u32][..]),
        ]);
        assert_eq!(snap.node_count(), 3);
        assert_eq!(snap.child_count(), 2);
        let empty: &[u32] = &[];
        assert_eq!(snap.children_of(0), Some(empty));
        assert_eq!(snap.children_of(1), Some(empty));
        assert_eq!(snap.children_of(2), Some(&[0, 1][..]));
        assert_eq!(snap.children_of(99), None);
    }

    /// `OpIdRegistry::intern` returns the same id for repeated
    /// names.
    #[test]
    fn op_id_intern_dedups() {
        let mut reg = OpIdRegistry::default();
        let a = reg.intern("foo");
        let b = reg.intern("bar");
        let c = reg.intern("foo");
        assert_eq!(a, c);
        assert_ne!(a, b);
        assert_eq!(reg.len(), 2);
        assert_eq!(reg.name_of(a), Some("foo"));
        assert_eq!(reg.name_of(b), Some("bar"));
        assert_eq!(reg.name_of(99), None);
    }

    #[test]
    fn gpu_snapshot_u32_layout_conversion_rejects_overflow() {
        let error = u32_len(u32::MAX as usize + 1, "test overflow")
            .expect_err("Fix: GPU e-graph snapshot must not silently saturate oversized columns");

        assert_eq!(error.context(), "test overflow");
        assert_eq!(error.value(), u32::MAX as usize + 1);
        assert!(
            error.to_string().contains("shard the e-graph snapshot")
                && error.to_string().contains("widen the GPU snapshot ABI"),
            "oversized GPU snapshot errors must explain both viable fixes"
        );
    }

    #[test]
    fn gpu_snapshot_builders_use_fallible_u32_conversion_not_saturation() {
        let source = include_str!("eqsat_gpu.rs");
        let production = source
            .split("#[cfg(test)]")
            .next()
            .expect("Fix: production eqsat_gpu section must exist");
        assert!(
            source.contains("pub fn try_build")
                && source.contains("pub fn try_from_egraph_with")
                && source.contains("snapshot.op_ids.try_intern")
                && source.contains("u32::try_from(value).map_err")
                && !source.contains(concat!("unwrap_or", "(u32::MAX)")),
            "Fix: GPU e-graph snapshots must reject oversized u32 ABI fields instead of saturating them to u32::MAX."
        );
        assert!(
            !production.contains(".expect("),
            "Fix: GPU e-graph snapshot production paths must return structured errors instead of panicking."
        );
    }

    /// `rows_by_eclass` groups multi-row e-classes.
    #[test]
    fn rows_by_eclass_groups_correctly() {
        let snap = GpuEGraphSnapshot::build([
            (0u32, "lit_u32", &[][..]),
            (0u32, "var", &[][..]),
            (1u32, "binop_add", &[0u32][..]),
        ]);
        let groups = snap.rows_by_eclass();
        assert_eq!(groups.len(), 2);
        assert_eq!(groups.get(&0).unwrap().len(), 2);
        assert_eq!(groups.get(&1).unwrap().len(), 1);
    }

    #[test]
    fn generated_snapshot_integrity_accepts_pack_boundaries_and_forward_children() {
        for node_count in [1_usize, 2, 7, 8, 9, 16, 17, 31, 32, 33, 65, 128] {
            let mut rows = Vec::with_capacity(node_count);
            let mut child_storage = Vec::new();
            for row in 0..node_count {
                let start = child_storage.len();
                if row > 0 {
                    child_storage.push((row - 1) as u32);
                }
                if row > 1 && row % 3 == 0 {
                    child_storage.push((row / 2) as u32);
                }
                rows.push((
                    row as u32,
                    if row % 2 == 0 { "lit" } else { "add" },
                    start,
                    child_storage.len() - start,
                ));
            }
            let build_rows = rows
                .iter()
                .map(|&(class, op, start, len)| (class, op, &child_storage[start..start + len]))
                .collect::<Vec<_>>();
            let snapshot = GpuEGraphSnapshot::build(build_rows);

            snapshot
                .validate_integrity()
                .unwrap_or_else(|error| panic!("node_count={node_count}: {error}"));
        }
    }

    #[test]
    fn snapshot_integrity_rejects_unknown_op_id() {
        let mut snapshot = GpuEGraphSnapshot::build([(0u32, "lit", &[][..])]);
        snapshot.rows[0].language_op_id = 99;

        let error = snapshot
            .validate_integrity()
            .expect_err("Fix: malformed GPU snapshot op ids must be rejected before upload.");

        assert_eq!(error.context(), "unknown language_op_id");
        assert_eq!(error.row(), 0);
        assert_eq!(error.value(), 99);
    }

    #[test]
    fn snapshot_integrity_rejects_out_of_bounds_child_range() {
        let mut snapshot = GpuEGraphSnapshot::build([(0u32, "lit", &[][..])]);
        snapshot.rows[0].children_offset = 1;
        snapshot.rows[0].children_len = 1;

        let error = snapshot
            .validate_integrity()
            .expect_err("Fix: malformed GPU snapshot child ranges must be rejected before upload.");

        assert_eq!(error.context(), "children range end");
        assert_eq!(error.row(), 0);
    }

    #[test]
    fn snapshot_integrity_rejects_dangling_child_eclass() {
        let snapshot =
            GpuEGraphSnapshot::build([(0u32, "lit", &[][..]), (1u32, "add", &[0u32, 99u32][..])]);

        let error = snapshot.validate_integrity().expect_err(
            "Fix: malformed GPU snapshot child eclasses must be rejected before upload.",
        );

        assert_eq!(error.context(), "dangling child eclass");
        assert_eq!(error.row(), 1);
        assert_eq!(error.value(), 99);
    }

    #[test]
    fn device_image_packs_single_upload_slab_with_sorted_group_index() {
        let snapshot = GpuEGraphSnapshot::build([
            (2u32, "lit", &[][..]),
            (1u32, "lit", &[][..]),
            (2u32, "add", &[1u32, 2u32][..]),
        ]);

        let image = snapshot
            .try_pack_device_image()
            .expect("Fix: valid GPU e-graph snapshot must pack into a device image");
        let layout = image.layout();

        assert_eq!(layout.row_count(), 3);
        assert_eq!(layout.child_count(), 2);
        assert_eq!(layout.eclass_group_count(), 2);
        assert_eq!(image.row_eclass_ids(), &[2, 1, 2]);
        assert_eq!(image.row_language_op_ids(), &[0, 0, 1]);
        assert_eq!(image.row_children_offsets(), &[0, 0, 0]);
        assert_eq!(image.row_children_lens(), &[0, 0, 2]);
        assert_eq!(image.row_signatures().len(), 3);
        assert_ne!(image.row_signatures()[0], image.row_signatures()[2]);
        assert_eq!(image.children(), &[1, 2]);
        assert_eq!(image.group_eclass_ids(), &[1, 2]);
        assert_eq!(image.group_offsets(), &[0, 1, 3]);
        assert_eq!(image.group_rows(), &[1, 0, 2]);
        assert_eq!(
            image.words().len(),
            layout.row_eclass_ids().len()
                + layout.row_language_op_ids().len()
                + layout.row_children_offsets().len()
                + layout.row_children_lens().len()
                + layout.row_signatures().len()
                + layout.children().len()
                + layout.group_eclass_ids().len()
                + layout.group_offsets().len()
                + layout.group_rows().len()
        );
    }

    #[test]
    fn generated_device_image_pack_accepts_empty_and_power_boundaries() {
        for node_count in [0_usize, 1, 2, 7, 8, 9, 31, 32, 33, 127, 128, 129] {
            let mut rows = Vec::with_capacity(node_count);
            let mut child_storage = Vec::new();
            for row in 0..node_count {
                let start = child_storage.len();
                if row > 0 {
                    child_storage.push((row - 1) as u32);
                }
                rows.push((
                    row as u32,
                    if row & 1 == 0 { "lit" } else { "neg" },
                    start,
                    child_storage.len() - start,
                ));
            }
            let build_rows = rows
                .iter()
                .map(|&(class, op, start, len)| (class, op, &child_storage[start..start + len]))
                .collect::<Vec<_>>();
            let snapshot = GpuEGraphSnapshot::build(build_rows);

            let image = snapshot
                .try_pack_device_image()
                .unwrap_or_else(|error| panic!("node_count={node_count}: {error}"));

            assert_eq!(image.layout().row_count(), node_count);
            assert_eq!(image.row_eclass_ids().len(), node_count);
            assert_eq!(image.row_language_op_ids().len(), node_count);
            assert_eq!(image.row_signatures().len(), node_count);
            assert_eq!(image.group_rows().len(), node_count);
            assert_eq!(image.group_offsets().len(), node_count + 1);
        }
    }

    #[test]
    fn row_signatures_group_structural_duplicates_without_eclass_identity() {
        let snapshot = GpuEGraphSnapshot::build([
            (1u32, "lit", &[][..]),
            (2u32, "lit", &[][..]),
            (10u32, "add", &[1u32, 2u32][..]),
            (11u32, "add", &[1u32, 2u32][..]),
            (12u32, "add", &[2u32, 1u32][..]),
            (13u32, "mul", &[1u32, 2u32][..]),
        ]);

        let image = snapshot
            .try_pack_device_image()
            .expect("Fix: valid duplicate-signature snapshot must pack");

        assert_eq!(image.row_signatures()[2], image.row_signatures()[3]);
        assert_ne!(image.row_signatures()[2], image.row_signatures()[4]);
        assert_ne!(image.row_signatures()[2], image.row_signatures()[5]);
    }

    #[test]
    fn device_image_rejects_malformed_snapshot_before_pack() {
        let mut snapshot = GpuEGraphSnapshot::build([(0u32, "lit", &[][..])]);
        snapshot.rows[0].language_op_id = 42;

        let error = snapshot
            .try_pack_device_image()
            .expect_err("Fix: device image packing must reject malformed snapshots");

        match error {
            GpuEGraphDeviceImageError::Integrity(error) => {
                assert_eq!(error.context(), "unknown language_op_id");
                assert_eq!(error.row(), 0);
                assert_eq!(error.value(), 42);
            }
            GpuEGraphDeviceImageError::Layout(error) => {
                panic!("expected integrity error, got layout error: {error}")
            }
        }
    }

    /// Snapshot directly from the CPU EGraph canonicalizes children and
    /// assigns stable operation ids.
    #[test]
    fn snapshot_from_egraph_uses_canonical_children() {
        let mut egraph = EGraph::new();
        let a = egraph.add(TinyLang::Lit(1));
        let b = egraph.add(TinyLang::Lit(2));
        let add = egraph.add(TinyLang::Add(a, b));
        assert_eq!(add.0, 2);

        let snap = GpuEGraphSnapshot::from_egraph_with(&egraph, |node| match node {
            TinyLang::Lit(_) => "lit",
            TinyLang::Add(_, _) => "add",
        });

        assert_eq!(snap.node_count(), 3);
        assert_eq!(snap.child_count(), 2);
        assert_eq!(snap.op_ids.name_of(0), Some("lit"));
        assert_eq!(snap.op_ids.name_of(1), Some("add"));
        assert_eq!(snap.children_of(2), Some(&[0, 1][..]));
    }

    /// `apply_equivalences` calls the merger for each equivalence
    /// and counts state-changing merges.
    #[test]
    fn apply_equivalences_counts_state_changes() {
        let equivalences = vec![
            Equivalence { left: 0, right: 1 },
            Equivalence { left: 1, right: 0 }, // no-op (already merged)
            Equivalence { left: 2, right: 3 },
        ];
        let mut canonical: FxHashMap<u32, u32> = FxHashMap::default();
        let applied = apply_equivalences(&equivalences, |a, b| {
            let canon_a = *canonical.get(&a).unwrap_or(&a);
            let canon_b = *canonical.get(&b).unwrap_or(&b);
            if canon_a == canon_b {
                false
            } else {
                let (lo, hi) = if canon_a < canon_b {
                    (canon_a, canon_b)
                } else {
                    (canon_b, canon_a)
                };
                canonical.insert(hi, lo);
                canonical.insert(a, lo);
                canonical.insert(b, lo);
                true
            }
        });
        assert_eq!(applied, 2);
    }

    /// Empty equivalence batch is a no-op.
    #[test]
    fn apply_equivalences_empty_batch() {
        let applied = apply_equivalences(&[], |_, _| true);
        assert_eq!(applied, 0);
    }

    /// EGraph merge bridge ignores invalid ids and rebuilds after valid
    /// merges.
    #[test]
    fn apply_equivalences_to_egraph_merges_valid_ids() {
        let mut egraph = EGraph::new();
        let a = egraph.add(TinyLang::Lit(1));
        let b = egraph.add(TinyLang::Lit(2));
        let c = egraph.add(TinyLang::Lit(3));
        let report = apply_equivalences_to_egraph(
            &mut egraph,
            &[
                Equivalence {
                    left: a.0,
                    right: b.0,
                },
                Equivalence {
                    left: c.0,
                    right: 99,
                },
            ],
        );
        assert_eq!(
            report,
            ApplyEquivalencesReport {
                requested: 2,
                valid: 1,
                merged: 1,
                rebuild_unions: 0,
            }
        );
        assert_eq!(egraph.find(a), egraph.find(b));
        assert_ne!(egraph.find(a), egraph.find(c));
    }
}