Skip to content

Commit

Permalink
Add ability to "saturate" inlining (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
saulshanabrook authored Nov 9, 2023
1 parent 61a0cd4 commit e682702
Show file tree
Hide file tree
Showing 13 changed files with 43,807 additions and 22 deletions.
41 changes: 35 additions & 6 deletions src/algorithms.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use crate::EGraph;

pub const MISSING_ARG_VALUE: &str = "·";

impl EGraph {
/// Inline all leaves (e-classes with a single node that has no children) into their parents, so that they
/// are added to the function name like f(10, ·)
pub fn inline_leaves(&mut self) {
/// are added to the function name like f(10, ·).
/// Returns the number of leaves inlined.
pub fn inline_leaves(&mut self) -> usize {
// 1. Create mapping of eclass to nodes as well as nodes to their parents
let mut eclass_to_nodes = std::collections::HashMap::new();
let mut node_to_parents = std::collections::HashMap::new();
Expand Down Expand Up @@ -56,28 +59,54 @@ impl EGraph {
if leaf_children.contains(child) {
leave_to_op.get(child).unwrap()
} else {
"·"
MISSING_ARG_VALUE
}
})
.collect::<Vec<_>>()
.join(", ");
.collect::<Vec<_>>();
// Remove leaf children from children
parent_node
.children
.retain(|child| !leaf_children.contains(child));
let new_op = format!("{}({})", parent_node.op, args);
// If the parent node already had some children replaced, then just replace the remaining children
// otherwise, replace the entire op
let new_op = if parent_node.op.matches(MISSING_ARG_VALUE).count() == args.len() {
// Replace all instances of MISSING_ARG_VALUE with the corresponding arg by interleaving
// the op split by MISSING_ARG_VALUE with the args
parent_node
.op
.split(MISSING_ARG_VALUE)
.enumerate()
.flat_map(|(i, s)| {
if i == args.len() {
vec![s.to_string()]
} else {
vec![s.to_string(), args[i].to_string()]
}
})
.collect::<String>()
} else {
format!("{}({})", parent_node.op, args.join(", "))
};
parent_node.op = new_op;
parent_node.cost += additional_cost;
}
let mut n_inlined = 0;
// 5. Remove leaf nodes from egraph, class data, and root eclasses
for (eclass, node_id) in &leaves {
// If this node has no parents, don't remove it, since it wasn't inlined
if node_to_parents.get(node_id).is_none() {
continue;
}
n_inlined += 1;
self.nodes.remove(node_id);
self.class_data.remove(eclass);
self.root_eclasses.retain(|root| root != eclass);
}
n_inlined
}

/// Inline all leaves (e-classes with a single node that has no children) into their parents, recursively.
pub fn saturate_inline_leaves(&mut self) {
while self.inline_leaves() > 0 {}
}
}
24 changes: 12 additions & 12 deletions tests-viz/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@

This is a list of all the tests in the `tests` directory. Each test is a JSON file that is loaded into an EGraph and then rendered as an SVG.

| Test | Image | Inlined Leaves Image |
| ---- | ----- | -------------------- |
| [`diff_power_simple`](../tests/diff_power_simple.json) | ![svg file](./diff_power_simple.svg) | ![inlined leaves svg file](./diff_power_simple-inlined.svg) |
| [`eqsolve`](../tests/eqsolve.json) | ![svg file](./eqsolve.svg) | ![inlined leaves svg file](./eqsolve-inlined.svg) |
| [`fibonacci-demand`](../tests/fibonacci-demand.json) | ![svg file](./fibonacci-demand.svg) | ![inlined leaves svg file](./fibonacci-demand-inlined.svg) |
| [`fibonacci`](../tests/fibonacci.json) | ![svg file](./fibonacci.svg) | ![inlined leaves svg file](./fibonacci-inlined.svg) |
| [`lambda_if`](../tests/lambda_if.json) | ![svg file](./lambda_if.svg) | ![inlined leaves svg file](./lambda_if-inlined.svg) |
| [`list_list_hard_test_ellisk_2019-02-15T11.26.41--bench005_it7`](../tests/list_list_hard_test_ellisk_2019-02-15T11.26.41--bench005_it7.json) | ![svg file](./list_list_hard_test_ellisk_2019-02-15T11.26.41--bench005_it7.svg) | ![inlined leaves svg file](./list_list_hard_test_ellisk_2019-02-15T11.26.41--bench005_it7-inlined.svg) |
| [`math_powers`](../tests/math_powers.json) | ![svg file](./math_powers.svg) | ![inlined leaves svg file](./math_powers-inlined.svg) |
| [`path`](../tests/path.json) | ![svg file](./path.svg) | ![inlined leaves svg file](./path-inlined.svg) |
| [`physics_scientific_unsolved_4h_ellisk_2019-07-20T18.05.46--bench000_it0`](../tests/physics_scientific_unsolved_4h_ellisk_2019-07-20T18.05.46--bench000_it0.json) | ![svg file](./physics_scientific_unsolved_4h_ellisk_2019-07-20T18.05.46--bench000_it0.svg) | ![inlined leaves svg file](./physics_scientific_unsolved_4h_ellisk_2019-07-20T18.05.46--bench000_it0-inlined.svg) |
| [`tiny`](../tests/tiny.json) | ![svg file](./tiny.svg) | ![inlined leaves svg file](./tiny-inlined.svg) |
| Test | Image | Inlined Leaves Image | Inlined Leaves Saturated Image |
| ---- | ----- | -------------------- | -------------------------- |
| [`diff_power_simple`](../tests/diff_power_simple.json) | ![svg file](./diff_power_simple.svg) | ![inlined leaves svg file](./diff_power_simple-inlined.svg) | ![inlined leaves saturated svg file](./diff_power_simple-inlined-saturated.svg) |
| [`eqsolve`](../tests/eqsolve.json) | ![svg file](./eqsolve.svg) | ![inlined leaves svg file](./eqsolve-inlined.svg) | ![inlined leaves saturated svg file](./eqsolve-inlined-saturated.svg) |
| [`fibonacci-demand`](../tests/fibonacci-demand.json) | ![svg file](./fibonacci-demand.svg) | ![inlined leaves svg file](./fibonacci-demand-inlined.svg) | ![inlined leaves saturated svg file](./fibonacci-demand-inlined-saturated.svg) |
| [`fibonacci`](../tests/fibonacci.json) | ![svg file](./fibonacci.svg) | ![inlined leaves svg file](./fibonacci-inlined.svg) | ![inlined leaves saturated svg file](./fibonacci-inlined-saturated.svg) |
| [`lambda_if`](../tests/lambda_if.json) | ![svg file](./lambda_if.svg) | ![inlined leaves svg file](./lambda_if-inlined.svg) | ![inlined leaves saturated svg file](./lambda_if-inlined-saturated.svg) |
| [`list_list_hard_test_ellisk_2019-02-15T11.26.41--bench005_it7`](../tests/list_list_hard_test_ellisk_2019-02-15T11.26.41--bench005_it7.json) | ![svg file](./list_list_hard_test_ellisk_2019-02-15T11.26.41--bench005_it7.svg) | ![inlined leaves svg file](./list_list_hard_test_ellisk_2019-02-15T11.26.41--bench005_it7-inlined.svg) | ![inlined leaves saturated svg file](./list_list_hard_test_ellisk_2019-02-15T11.26.41--bench005_it7-inlined-saturated.svg) |
| [`math_powers`](../tests/math_powers.json) | ![svg file](./math_powers.svg) | ![inlined leaves svg file](./math_powers-inlined.svg) | ![inlined leaves saturated svg file](./math_powers-inlined-saturated.svg) |
| [`path`](../tests/path.json) | ![svg file](./path.svg) | ![inlined leaves svg file](./path-inlined.svg) | ![inlined leaves saturated svg file](./path-inlined-saturated.svg) |
| [`physics_scientific_unsolved_4h_ellisk_2019-07-20T18.05.46--bench000_it0`](../tests/physics_scientific_unsolved_4h_ellisk_2019-07-20T18.05.46--bench000_it0.json) | ![svg file](./physics_scientific_unsolved_4h_ellisk_2019-07-20T18.05.46--bench000_it0.svg) | ![inlined leaves svg file](./physics_scientific_unsolved_4h_ellisk_2019-07-20T18.05.46--bench000_it0-inlined.svg) | ![inlined leaves saturated svg file](./physics_scientific_unsolved_4h_ellisk_2019-07-20T18.05.46--bench000_it0-inlined-saturated.svg) |
| [`tiny`](../tests/tiny.json) | ![svg file](./tiny.svg) | ![inlined leaves svg file](./tiny-inlined.svg) | ![inlined leaves saturated svg file](./tiny-inlined-saturated.svg) |
Loading

0 comments on commit e682702

Please sign in to comment.