Skip to content

Commit

Permalink
Add code to dump egraphs in egraph-serialize format
Browse files Browse the repository at this point in the history
  • Loading branch information
pavpanchekha committed Sep 3, 2024
1 parent fc30cd4 commit 9b28ebc
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 7 deletions.
128 changes: 123 additions & 5 deletions egg-herbie/Cargo.lock

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

1 change: 1 addition & 0 deletions egg-herbie/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2021"

[dependencies]
egg = { git = "https://github.com/egraphs-good/egg.git", rev = "c11d03db646aca5f55df2632927eec958e1f4d4d" }
egraph-serialize = "0.1.0"

log = "0.4"
indexmap = "1"
Expand Down
7 changes: 7 additions & 0 deletions egg-herbie/main.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
egraph_is_unsound_detected
egraph_get_times_applied
egraph_get_proof
egraph_dump_to_file
(struct-out iteration-data)
(struct-out FFIRule)
make-ffi-rule)
Expand Down Expand Up @@ -299,3 +300,9 @@
_pointer ;; name of the rule
->
_uint))

(define-eggmath egraph_dump_to_file
(_fun _egraph-pointer
_string ;; file name
->
_void))
34 changes: 34 additions & 0 deletions egg-herbie/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use std::mem::{self, ManuallyDrop};
use std::os::raw::c_char;
use std::time::Duration;
use std::{slice, sync::atomic::Ordering};
use std::path::Path;

pub struct Context {
iteration: usize,
Expand Down Expand Up @@ -481,3 +482,36 @@ pub unsafe extern "C" fn egraph_get_size(ptr: *mut Context) -> u32 {
.map(|iteration| iteration.egraph_nodes as u32)
.unwrap_or_default()
}

#[no_mangle]
pub unsafe extern "C" fn egraph_dump_to_file(ptr : *mut Context, path_str : *mut c_char) {
let context = ManuallyDrop::new(Box::from_raw(ptr));
let path_cstr = CStr::from_ptr(path_str).to_str().expect("Non-UTF-8 path");
let path = Path::new(&path_cstr);

use egraph_serialize::*;
let mut out = EGraph::default();
for class in context.runner.egraph.classes() {
for (i, node) in class.nodes.iter().enumerate() {
out.add_node(
format!("{}.{}", class.id, i),
Node {
op: node.to_string(),
children: node
.children()
.iter()
.map(|id| NodeId::from(format!("{}.0", id)))
.collect(),
eclass: ClassId::from(format!("{}", class.id)),
cost: Cost::new(1.0).unwrap(),
},
)
}
}

for root in context.runner.roots.clone() {
out.root_eclasses.push(ClassId::from(format!("{}", context.runner.egraph.find(root))));
}

out.to_json_file(path).expect("Failed to write file");
}
6 changes: 4 additions & 2 deletions src/config.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
numerics
special
bools
branches)]))
branches)]
[dump . (egg)]))

(define default-flags
#hash([precision . ()]
Expand All @@ -36,7 +37,8 @@
numerics
special
bools
branches)]))
branches)]
[dump . ()]))

(define (check-flag-deprecated! category flag)
(match* (category flag)
Expand Down
9 changes: 9 additions & 0 deletions src/core/egg-herbie.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -1251,6 +1251,15 @@
(define ctx (egg-runner-ctx runner))
(define-values (root-ids egg-graph)
(egraph-run-schedule (egg-runner-exprs runner) (egg-runner-schedule runner) ctx))
(when (and (flag-set? 'dump 'egg) (set-member? '(single multi) (car cmd)))
(define dump-dir "dump-egg")
(unless (directory-exists? dump-dir)
(make-directory dump-dir))
(define name
(for/first ([i (in-naturals)]
#:unless (file-exists? (build-path dump-dir (format "~a.json" i))))
(build-path dump-dir (format "~a.json" i))))
(egraph_dump_to_file (egraph-data-egraph-pointer egg-graph) (path->string name)))
; Perform extraction
(match cmd
[`(single . ,extractor) ; single expression extraction
Expand Down

0 comments on commit 9b28ebc

Please sign in to comment.