| 1 |
//! Intermediate representation of Wasm operators to be used with the `egg` |
| 2 |
//! engine |
| 3 |
//! |
| 4 |
|
| 5 |
pub mod analysis; |
| 6 |
pub mod encoder; |
| 7 |
pub mod expr_enumerator; |
| 8 |
pub mod lang; |
| 9 |
|
| 10 |
use self::{analysis::ClassData, lang::Lang}; |
| 11 |
use egg::{Analysis, CostFunction, EClass, EGraph, Id, Language, RecExpr}; |
| 12 |
use std::{cell::RefCell, cmp::Ordering, collections::HashMap}; |
| 13 |
|
| 14 |
/// This struct is a wrapper of egg::Extractor |
| 15 |
/// The majority of the methods are copied and adapted to our needs |
| 16 |
pub struct RandomExtractor<'a, CF: CostFunction<L>, L: Language, N: Analysis<L>> { |
| 17 |
cost_function: CF, |
| 18 |
costs: HashMap<Id, (CF::Cost, usize)>, |
| 19 |
egraph: &'a EGraph<L, N>, |
| 20 |
} |
| 21 |
|
| 22 |
fn cmp<T: PartialOrd>(a: &Option<T>, b: &Option<T>) -> Ordering { |
| 23 |
// None is high |
| 24 |
match (a, b) { |
| 25 |
(None, None) => Ordering::Equal, |
| 26 |
(None, Some(_)) => Ordering::Greater, |
| 27 |
(Some(_), None) => Ordering::Less, |
| 28 |
(Some(a), Some(b)) => a.partial_cmp(b).unwrap(), |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
impl<'a, CF, L, N> RandomExtractor<'a, CF, L, N> |
| 33 |
where |
| 34 |
CF: CostFunction<L>, |
| 35 |
L: Language, |
| 36 |
N: Analysis<L, Data = Option<ClassData>>, // The analysis should return the index of the node in the e-class |
| 37 |
{ |
| 38 |
/// Returns a new Random extractor from an egraph and a custom cost function |
| 39 |
pub fn new(egraph: &'a EGraph<L, N>, cost_function: CF) -> Self { |
| 40 |
let costs = HashMap::default(); |
| 41 |
|
| 42 |
let mut extractor = RandomExtractor { |
| 43 |
costs, |
| 44 |
egraph, |
| 45 |
cost_function, |
| 46 |
}; |
| 47 |
extractor.costs = extractor.find_costs(); |
| 48 |
extractor |
| 49 |
} |
| 50 |
|
| 51 |
fn find_costs(&mut self) -> HashMap<Id, (CF::Cost, usize)> { |
| 52 |
let mut costs = HashMap::new(); |
| 53 |
|
| 54 |
let mut did_something = true; |
| 55 |
while did_something { |
| 56 |
did_something = false; |
| 57 |
|
| 58 |
for class in self.egraph.classes() { |
| 59 |
let pass = self.make_pass(&mut costs, class); |
| 60 |
match (costs.get(&class.id), pass) { |
| 61 |
(None, Some(new)) => { |
| 62 |
costs.insert(class.id, new); |
| 63 |
did_something = true; |
| 64 |
} |
| 65 |
(Some(old), Some(new)) if new.0 < old.0 => { |
| 66 |
costs.insert(class.id, new); |
| 67 |
did_something = true; |
| 68 |
} |
| 69 |
_ => (), |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
costs |
| 75 |
} |
| 76 |
|
| 77 |
fn make_pass( |
| 78 |
&mut self, |
| 79 |
costs: &mut HashMap<Id, (CF::Cost, usize)>, |
| 80 |
eclass: &EClass<L, Option<ClassData>>, |
| 81 |
) -> Option<(CF::Cost, usize)> { |
| 82 |
let (cost, node_idx) = eclass |
| 83 |
.iter() |
| 84 |
.enumerate() |
| 85 |
.map(|(i, n)| (self.node_total_cost(n, costs), i)) |
| 86 |
.min_by(|a, b| cmp(&a.0, &b.0)) |
| 87 |
.unwrap_or_else(|| panic!("Can't extract, eclass is empty: {eclass:#?}")); |
| 88 |
cost.map(|c| (c, node_idx)) |
| 89 |
} |
| 90 |
|
| 91 |
fn node_total_cost( |
| 92 |
&mut self, |
| 93 |
node: &L, |
| 94 |
costs: &mut HashMap<Id, (CF::Cost, usize)>, |
| 95 |
) -> Option<CF::Cost> { |
| 96 |
let egraph = self.egraph; |
| 97 |
let has_cost = |&id| costs.contains_key(&egraph.find(id)); |
| 98 |
if node.children().iter().all(has_cost) { |
| 99 |
let costs = &costs; |
| 100 |
let cost_f = |id| costs[&egraph.find(id)].0.clone(); |
| 101 |
Some(self.cost_function.cost(node, cost_f)) |
| 102 |
} else { |
| 103 |
None |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
/// Do a pre-order traversal of the e-graph. As we visit each e-class, choose |
| 108 |
/// choose the smallest e-node (according to the cost function). |
| 109 |
pub fn extract_smallest( |
| 110 |
&self, |
| 111 |
eclass: Id, |
| 112 |
recexpr: &RefCell<RecExpr<L>>, |
| 113 |
expression_builder: impl for<'b> Fn(Id, &mut RecExpr<L>, &dyn Fn(Id) -> (&'b L, &'b [Id])) -> Id, |
| 114 |
) -> crate::Result<Id> // return the random tree, TODO, improve the way the tree is returned |
| 115 |
{ |
| 116 |
// A map from a node's id to its actual node data. |
| 117 |
let mut id_to_node = vec![]; |
| 118 |
// A map from a parent node id to its child operand node ids. |
| 119 |
let mut operands = vec![]; |
| 120 |
|
| 121 |
// Select a random node in this e-class |
| 122 |
let rootidx = self.costs[&eclass].1; |
| 123 |
let rootnode = &self.egraph[eclass].nodes[rootidx]; |
| 124 |
// The operator index is the same in all eclass nodes |
| 125 |
id_to_node.push(self.egraph[eclass].nodes[rootidx].clone()); |
| 126 |
operands.push(vec![]); |
| 127 |
|
| 128 |
let mut worklist: Vec<_> = rootnode |
| 129 |
.children() |
| 130 |
.iter() |
| 131 |
.rev() |
| 132 |
.map(|id| (0, id, 0)) // (root, operant, depth) |
| 133 |
.collect(); |
| 134 |
|
| 135 |
// The RecExpr can be built directly here following the following rules |
| 136 |
// The childrens of a node are before in the array |
| 137 |
|
| 138 |
while let Some((parentidx, &node, depth)) = worklist.pop() { |
| 139 |
let node_idx = self.costs[&node].1; |
| 140 |
let operand = Id::from(id_to_node.len()); |
| 141 |
let operandidx = id_to_node.len(); |
| 142 |
let last_node_id = parentidx; // id_to_node.len() - 1; |
| 143 |
|
| 144 |
id_to_node.push(self.egraph[node].nodes[node_idx].clone()); |
| 145 |
operands.push(vec![]); |
| 146 |
|
| 147 |
operands[last_node_id].push(operand); |
| 148 |
|
| 149 |
worklist.extend( |
| 150 |
self.egraph[node].nodes[node_idx] |
| 151 |
.children() |
| 152 |
.iter() |
| 153 |
.rev() |
| 154 |
.map(|id| (operandidx, id, depth + 1)), |
| 155 |
); |
| 156 |
} |
| 157 |
// Build the tree with the right language constructor |
| 158 |
let mut to_write_in = recexpr.borrow_mut(); |
| 159 |
let expr = expression_builder(Id::from(0), &mut to_write_in, &|id| { |
| 160 |
(&id_to_node[usize::from(id)], &operands[usize::from(id)]) |
| 161 |
}); |
| 162 |
Ok(expr) |
| 163 |
} |
| 164 |
} |