Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move splitting primitives to serialize library #407

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Comment on lines -1558 to -1570
Copy link
Member Author

@saulshanabrook saulshanabrook Aug 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed this helper function since it wasn't really doing much

}

pub(crate) fn print_msg(&mut self, msg: String) {
self.msgs.push(msg);
}
Expand Down
61 changes: 31 additions & 30 deletions src/main.rs
Original file line number Diff line number Diff line change
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,
Comment on lines +46 to +48
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the ability to inline leaves via the egglog CLI when making visualizations/json

}

// 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()
};
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved all this processing into one branch, so it would only be serialized once for JSON or for visualization.

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
Loading