1use crate::canon::{MSetCanon, VarCanon};
21use crate::config::EGraphConfig;
22use crate::egraph::EGraph;
23use crate::literal::LitVal;
24use std::collections::HashMap;
25
26type FastMap<K, V> = HashMap<K, V, foldhash::fast::RandomState>;
29
30pub struct CcSnapshot<Cfg: EGraphConfig> {
45 by_op_contains: FastMap<(Cfg::O, Cfg::G), Vec<Cfg::G>>,
47 completion_nodes: Vec<Cfg::G>,
49}
50
51impl<Cfg: EGraphConfig> CcSnapshot<Cfg>
52where
53 MSetCanon: VarCanon<Cfg::G, Cfg::C>,
54{
55 pub fn build<L: LitVal, const TRACK: bool, const PROOFS: bool>(
58 eg: &EGraph<Cfg, L, TRACK, PROOFS>,
59 ) -> Self {
60 let mut by_op_contains: FastMap<(Cfg::O, Cfg::G), Vec<Cfg::G>> = FastMap::default();
61 let mut completion_nodes: Vec<Cfg::G> = Vec::new();
62
63 let inactive = crate::node_types::FLAG_SUBSUMED | crate::node_types::FLAG_AC_COLLAPSED;
67 for gid in eg.node_ids() {
68 if eg.node_flags(gid) & inactive != 0 {
69 continue;
70 }
71 let op = eg.node_op(gid);
72 if !eg.ops().is_mset(op) && !eg.ops().is_set(op) {
73 continue;
74 }
75 completion_nodes.push(gid);
76
77 let mut seen: Vec<Cfg::G> = Vec::new();
79 eg.for_each_child(gid, |child, _mult| {
80 let cr = eg.class_repr(child);
81 if !seen.contains(&cr) {
82 seen.push(cr);
83 by_op_contains.entry((op, cr)).or_default().push(gid);
84 }
85 });
86 }
87
88 for v in by_op_contains.values_mut() {
89 v.sort_unstable();
90 v.dedup();
91 }
92 completion_nodes.sort_unstable();
93 completion_nodes.dedup();
94
95 Self {
96 by_op_contains,
97 completion_nodes,
98 }
99 }
100
101 pub fn completion_nodes(&self) -> &[Cfg::G] {
104 &self.completion_nodes
105 }
106
107 pub fn partners(&self, op: Cfg::O, child_repr: Cfg::G) -> &[Cfg::G] {
110 self.by_op_contains
111 .get(&(op, child_repr))
112 .map(|v| v.as_slice())
113 .unwrap_or(&[])
114 }
115}
116
117#[cfg(test)]
118mod tests {
119 use super::*;
120 use crate::egraph::EGraph31;
121 use crate::literal::NiraLitVal;
122
123 fn ref_partners(
125 eg: &EGraph31<NiraLitVal, false, false>,
126 op: crate::id::OpId,
127 cr: crate::id::ENodeId,
128 ) -> Vec<crate::id::ENodeId> {
129 let mut out = Vec::new();
130 let mut buf = Vec::new();
131 let inactive = crate::node_types::FLAG_SUBSUMED | crate::node_types::FLAG_AC_COLLAPSED;
132 for i in 0..eg.node_count() {
133 let gid = crate::id::ENodeId::new(i as u32);
134 if eg.node_flags(gid) & inactive != 0 {
135 continue;
136 }
137 if eg.node_op(gid) != op || !(eg.ops().is_mset(op) || eg.ops().is_set(op)) {
138 continue;
139 }
140 buf.clear();
141 eg.for_each_child(gid, |c, m| buf.push((c, m)));
142 if buf.iter().any(|&(c, _)| eg.class_repr(c) == cr) {
143 out.push(gid);
144 }
145 }
146 out.sort_unstable();
147 out.dedup();
148 out
149 }
150
151 #[test]
152 fn snapshot_matches_by_contains_variadic_fixture() {
153 let mut eg = EGraph31::<NiraLitVal, false, false>::new();
155 let int = eg.intern_sort("Int");
156 let plus = eg.register_mset("plus", int, int);
157 let x_op = eg.register_op0("x", int);
158 let y_op = eg.register_op0("y", int);
159 let z_op = eg.register_op0("z", int);
160
161 let x = eg.add(x_op, &[]);
162 let y = eg.add(y_op, &[]);
163 let z = eg.add(z_op, &[]);
164 let pxy = eg.add(plus, &[x, y]);
165 let pxz = eg.add(plus, &[x, z]);
166
167 let snap = CcSnapshot::build(&eg);
168
169 let mut cx = snap.partners(plus, x).to_vec();
171 cx.sort_unstable();
172 assert_eq!(cx, vec![pxy.min(pxz), pxy.max(pxz)]);
173
174 assert_eq!(snap.partners(plus, y), &[pxy]);
176
177 assert_eq!(snap.completion_nodes(), &[pxy.min(pxz), pxy.max(pxz)]);
179 }
180
181 #[test]
182 fn snapshot_only_indexes_ac_ops() {
183 let mut eg = EGraph31::<NiraLitVal, false, false>::new();
184 let int = eg.intern_sort("Int");
185 let plus = eg.register_mset("plus", int, int);
186 let f = eg.register_op1("f", int, int); let x_op = eg.register_op0("x", int);
188
189 let x = eg.add(x_op, &[]);
190 let fx = eg.add(f, &[x]);
191 let pfx = eg.add(plus, &[x, fx]);
192
193 let snap = CcSnapshot::build(&eg);
194
195 assert_eq!(snap.completion_nodes(), &[pfx]);
197 assert!(snap.partners(f, eg.class_repr(x)).is_empty());
199 assert_eq!(snap.partners(plus, eg.class_repr(x)), &[pfx]);
201 assert_eq!(snap.partners(plus, eg.class_repr(fx)), &[pfx]);
202 }
203
204 #[test]
205 fn snapshot_agrees_with_brute_force_after_merge() {
206 let mut eg = EGraph31::<NiraLitVal, false, false>::new();
208 let int = eg.intern_sort("Int");
209 let plus = eg.register_mset("plus", int, int);
210 let a_op = eg.register_op0("a", int);
211 let b_op = eg.register_op0("b", int);
212 let d_op = eg.register_op0("d", int);
213
214 let a = eg.add(a_op, &[]);
215 let b = eg.add(b_op, &[]);
216 let d = eg.add(d_op, &[]);
217 let _ab = eg.add(plus, &[a, b]);
218 let _bd = eg.add(plus, &[b, d]);
219 let _abb = eg.add(plus, &[a, b, b]); eg.merge(a, d);
223 eg.rebuild();
224
225 let snap = CcSnapshot::build(&eg);
226
227 for x in [a, b, d] {
229 let cr = eg.class_repr(x);
230 let mut got = snap.partners(plus, cr).to_vec();
231 got.sort_unstable();
232 assert_eq!(
233 got,
234 ref_partners(&eg, plus, cr),
235 "mismatch for child {cr:?}"
236 );
237 }
238 }
239
240 #[test]
241 fn cc_collapsed_leaves_completion_set_but_stays_matchable() {
242 use crate::index::IndexStore;
245 let mut eg = EGraph31::<NiraLitVal, false, false>::new();
246 let int = eg.intern_sort("Int");
247 let plus = eg.register_mset("plus", int, int);
248 let a_op = eg.register_op0("a", int);
249 let b_op = eg.register_op0("b", int);
250 let a = eg.add(a_op, &[]);
251 let b = eg.add(b_op, &[]);
252 let pab = eg.add(plus, &[a, b]);
253
254 assert_eq!(CcSnapshot::build(&eg).completion_nodes(), &[pab]);
256 assert!(IndexStore::build(&eg).nodes_by_op(plus).contains(&pab));
257
258 eg.set_cc_collapsed(pab);
260
261 assert!(
263 CcSnapshot::build(&eg).completion_nodes().is_empty(),
264 "AC-collapsed node must leave the completion active set"
265 );
266 assert!(
268 IndexStore::build(&eg).nodes_by_op(plus).contains(&pab),
269 "AC-collapsed node must stay visible to the matcher (not subsumed)"
270 );
271 assert_eq!(eg.class_repr(pab), eg.class_repr(pab));
272
273 eg.subsume(pab);
275 assert!(
276 !IndexStore::build(&eg).nodes_by_op(plus).contains(&pab),
277 "subsumed node must be hidden from the matcher index"
278 );
279 }
280}