Skip to content

Commit

Permalink
Move splitting primitives to serialize library
Browse files Browse the repository at this point in the history
  • Loading branch information
saulshanabrook committed Aug 6, 2024
1 parent ecc3be5 commit dc00449
Show file tree
Hide file tree
Showing 11 changed files with 200 additions and 118 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ _scratch.egg

# racket
scripts/compiled
tests/*.json
54 changes: 32 additions & 22 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ smallvec = "1.11"

generic_symbolic_expressions = "5.0.4"

egraph-serialize = { version = "0.1.0", features = ["serde", "graphviz"] }
egraph-serialize = { git = "https://github.com/saulshanabrook/egraph-serialize", rev = "1c205fcc6d3426800b828e9264dbadbd4a5ef6e9", features = ["serde", "graphviz"] }
serde_json = { optional = true, version = "1.0.100", features = [
"preserve_order",
] }
Expand Down
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,13 @@ ${DIST_WASM}: ${RUST_SRC}

graphs: $(patsubst %.egg,%.svg,$(filter-out $(wildcard tests/repro-*.egg),$(wildcard tests/*.egg)))

json: $(patsubst %.egg,%.json,$(filter-out $(wildcard tests/repro-*.egg),$(wildcard tests/*.egg)))

%.svg: %.egg
cargo run --release -- --to-dot --to-svg $^

%.json: %.egg
cargo run --release -- --to-json $^

rm-graphs:
rm -f tests/*.dot tests/*.svg
19 changes: 1 addition & 18 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use hashbrown::hash_map::Entry;
use index::ColumnIndex;
use instant::{Duration, Instant};
pub use serialize::SerializeConfig;
pub use serialize::SerializedNode;
use sort::*;
pub use termdag::{Term, TermDag, TermId};
use thiserror::Error;
Expand Down Expand Up @@ -1552,24 +1553,6 @@ impl EGraph {
&self.overall_run_report
}

/// Serializes the egraph for export to graphviz.
///
/// This will limit the total number of nodes so that visualization does not blow up.
pub fn serialize_for_graphviz(
&self,
split_primitive_outputs: bool,
max_functions: usize,
max_calls_per_function: usize,
) -> egraph_serialize::EGraph {
let config = SerializeConfig {
split_primitive_outputs,
max_functions: Some(max_functions),
max_calls_per_function: Some(max_calls_per_function),
..Default::default()
};
self.serialize(config)
}

pub(crate) fn print_msg(&mut self, msg: String) {
self.msgs.push(msg);
}
Expand Down
63 changes: 32 additions & 31 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use clap::Parser;
use egglog::{EGraph, Error, RunMode, SerializeConfig};
use egglog::{EGraph, Error, RunMode, SerializeConfig, SerializedNode};
use std::io::{self, BufRead, BufReader};
use std::path::PathBuf;

Expand Down Expand Up @@ -43,6 +43,9 @@ struct Args {
/// Maximum number of calls per function to render in dot/svg output
#[clap(long, default_value = "40")]
max_calls_per_function: usize,
/// Number of times to inline leaves
#[clap(long, default_value = "0")]
serialize_n_inline_leaves: usize,
}

// test if the current command should be evaluated
Expand Down Expand Up @@ -209,31 +212,25 @@ fn main() {
std::process::exit(1)
}
}
// if we are splitting primitive outputs, add `-split` to the end of the file name
let serialize_filename = if args.serialize_split_primitive_outputs {
input.with_file_name(format!(
"{}-split",
input.file_stem().unwrap().to_str().unwrap()
))
} else {
input.clone()
};
if args.to_json {
let json_path = serialize_filename.with_extension("json");
let config = SerializeConfig {
split_primitive_outputs: args.serialize_split_primitive_outputs,
..SerializeConfig::default()
};
let serialized = egraph.serialize(config);
serialized.to_json_file(json_path).unwrap();
}

if args.to_dot || args.to_svg {
let serialized = egraph.serialize_for_graphviz(
args.serialize_split_primitive_outputs,
args.max_functions,
args.max_calls_per_function,
);
if args.to_json || args.to_dot || args.to_svg {
let mut serialized = egraph.serialize(SerializeConfig::default());
if args.serialize_split_primitive_outputs {
serialized.split_e_classes(|id, _| egraph.from_node_id(id).is_primitive())
}
for _ in 0..args.serialize_n_inline_leaves {
serialized.inline_leaves();
}

// if we are splitting primitive outputs, add `-split` to the end of the file name
let serialize_filename = if args.serialize_split_primitive_outputs {
input.with_file_name(format!(
"{}-split",
input.file_stem().unwrap().to_str().unwrap()
))
} else {
input.clone()
};
if args.to_dot {
let dot_path = serialize_filename.with_extension("dot");
serialized.to_dot_file(dot_path).unwrap()
Expand All @@ -242,6 +239,10 @@ fn main() {
let svg_path = serialize_filename.with_extension("svg");
serialized.to_svg_file(svg_path).unwrap()
}
if args.to_json {
let json_path = serialize_filename.with_extension("json");
serialized.to_json_file(json_path).unwrap();
}
}
// no need to drop the egraph if we are going to exit
if idx == args.inputs.len() - 1 {
Expand All @@ -259,12 +260,12 @@ mod tests {
#[rustfmt::skip]
let test_cases = vec![
vec![
"(extract",
"\"1",
")",
"(",
")))",
"\"",
"(extract",
"\"1",
")",
"(",
")))",
"\"",
";; )",
")"
],
Expand Down
Loading

0 comments on commit dc00449

Please sign in to comment.