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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
//! Optimal term extraction: pull the lowest-cost term from an e-class.
use crate::ast::Span;
use crate::ast::Term;
use crate::canon::{MSetCanon, VarCanon};
use crate::config::EGraphConfig;
use crate::containers::DenseId;
use crate::egraph::EGraph;
use crate::literal::LitVal;
use crate::multiplicity::MultiplicityLike;
/// Why [`extract_best`] could not produce a term.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ExtractError {
/// The class has nodes, but every one of them belongs to an op declared
/// `:unextractable`, so the class denotes no extractable term.
AllUnextractable {
/// The class's representative id, as a plain index.
class: usize,
/// The op names of the class's nodes, deduplicated, in first-seen order.
ops: Vec<String>,
},
/// No node in the class has a fully costed child set: the class is reachable only
/// through cycles, or through classes that are themselves not extractable.
NoGroundTerm {
/// The class's representative id, as a plain index.
class: usize,
},
}
impl std::fmt::Display for ExtractError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ExtractError::AllUnextractable { class, ops } => write!(
f,
"e-class {class} has no extractable node: every node is an :unextractable op ({})",
ops.join(", ")
),
ExtractError::NoGroundTerm { class } => write!(
f,
"e-class {class} has no ground term: no node has a fully extractable child set"
),
}
}
}
impl std::error::Error for ExtractError {}
/// Extract the cheapest term from the e-class containing `root`.
///
/// Cost = the sum of the per-node costs of the term's nodes, where a node costs its op's
/// declared `:cost` (default 1, so an undeclared program keeps the historical AST-size cost
/// model) and an AC child of multiplicity k counts k times. Nodes whose op is declared
/// `:unextractable` are never selected.
pub fn extract_best<Cfg, L, const T: bool, const P: bool>(
eg: &EGraph<Cfg, L, T, P>,
root: Cfg::G,
) -> Result<Term, ExtractError>
where
Cfg: EGraphConfig,
L: LitVal,
MSetCanon: VarCanon<Cfg::G, Cfg::C>,
{
let n = eg.len();
// Per-op cost and extractability, indexed by op id: the fixpoint below revisits every
// node on every pass, and a registry lookup per visit would pay for the same two fields
// repeatedly.
let meta = eg.ops().meta_table();
// Both tables are indexed by class id rather than hashed. Ids here are
// dense by construction — the scan below is over `eg.node_ids()` and
// every representative is one of those ids — so a hash map's key storage,
// hashing and probing all buy nothing over a direct index.
//
// `UNSET` doubles as the "no entry yet" marker for *both* tables: they are
// only ever written together, and a saturated cost of `UNSET` is never
// recorded (it cannot be `<` the incumbent, `UNSET` included), so
// `best_cost[i] != UNSET` holds exactly when class `i` has a best node.
// That is the same condition the previous map-based code expressed as
// `get(..).unwrap_or(usize::MAX)`.
const UNSET: usize = usize::MAX;
let mut best_cost: Vec<usize> = vec![UNSET; n];
let mut best_node: Vec<Cfg::G> = vec![Cfg::G::default(); n];
loop {
let mut changed = false;
for id in eg.node_ids() {
// `:unextractable` is a filter on candidate nodes, not a cost: an excluded node
// never becomes a class's best, but the class stays extractable through its
// other nodes.
let m = meta[eg.node_op(id).to_usize()];
if m.unextractable {
continue;
}
let repr = eg.find_const(id);
let mut total: usize = m.cost as usize;
let mut ok = true;
eg.for_each_child(id, |child, mult| {
if !ok {
return;
}
let c = best_cost[eg.find_const(child).to_usize()];
if c == UNSET {
ok = false;
} else {
total = total.saturating_add(c.saturating_mul(mult.to_usize()));
}
});
if !ok {
continue;
}
let slot = repr.to_usize();
if total < best_cost[slot] {
best_cost[slot] = total;
best_node[slot] = id;
changed = true;
}
}
if !changed {
break;
}
}
let root_repr = eg.find_const(root);
if best_cost[root_repr.to_usize()] == UNSET {
return Err(diagnose(eg, &meta, root_repr));
}
Ok(reconstruct(eg, &best_node, root_repr))
}
/// Explain an empty result: name the class, and say whether it was excluded by
/// `:unextractable` or is simply not grounded. Runs once, only on the failure path.
fn diagnose<Cfg, L, const T: bool, const P: bool>(
eg: &EGraph<Cfg, L, T, P>,
meta: &[crate::registry::OpMeta],
repr: Cfg::G,
) -> ExtractError
where
Cfg: EGraphConfig,
L: LitVal,
MSetCanon: VarCanon<Cfg::G, Cfg::C>,
{
let class = repr.to_usize();
let mut ops: Vec<String> = Vec::new();
let mut any = false;
let mut all_unextractable = true;
for id in eg.node_ids() {
if eg.find_const(id) != repr {
continue;
}
any = true;
if meta[eg.node_op(id).to_usize()].unextractable {
let name = eg.node_op_name(id);
if !ops.iter().any(|o| o == name) {
ops.push(name.to_string());
}
} else {
all_unextractable = false;
}
}
if any && all_unextractable {
ExtractError::AllUnextractable { class, ops }
} else {
ExtractError::NoGroundTerm { class }
}
}
fn reconstruct<Cfg, L, const T: bool, const P: bool>(
eg: &EGraph<Cfg, L, T, P>,
best_node: &[Cfg::G],
repr: Cfg::G,
) -> Term
where
Cfg: EGraphConfig,
L: LitVal,
MSetCanon: VarCanon<Cfg::G, Cfg::C>,
{
let id = best_node[repr.to_usize()];
let name = eg.node_op_name(id).to_string();
if let Some(val) = eg.get_lit_val(id) {
return Term::Lit(val.to_string(), Span::Dummy);
}
let mut children = Vec::new();
eg.for_each_child(id, |child, mult| {
if mult == Cfg::M::ZERO {
return;
}
let t = reconstruct(eg, best_node, eg.find_const(child));
// `mult - 1` clones and then move the original in, rather than `mult`
// clones followed by dropping it. At the overwhelmingly common `mult == 1`
// that is one deep copy saved per child, and deep copies compound: the
// previous form made extraction of a depth-`d` chain O(d^2) node copies
// for a `d`-node result, because each level copied the whole subterm its
// recursive call had just built and then threw the original away.
for _ in 1..mult.to_usize() {
children.push(t.clone());
}
children.push(t);
});
Term::App {
op: name,
children,
span: Span::Dummy,
}
}