Skip to content

Commit

Permalink
[Cider 2] Misc fixes + fud2 integration (#2054)
Browse files Browse the repository at this point in the history
* Refactor program counter to avoid `std::mem::take`

* add a helper method

* Refactor code to use `lookup_string` method for retrieving names in the environment

* Refactor code to use `lookup_string` method for retrieving names in the environment

* Minor tweak

* stab at fud2 integration

* bump MSRVs to make clippy stop bugging me

* my life is a never ending litany of suffering

* snapshotsssss

* more snappéd shots
  • Loading branch information
EclecticGriffin authored May 24, 2024
1 parent a27bb66 commit deee510
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 17 deletions.
37 changes: 37 additions & 0 deletions fud2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,11 @@ pub fn build_driver(bld: &mut DriverBuilder) {
"cider.exe",
"$calyx-base/target/debug/cider",
)?;
e.config_var_or(
"cider-converter",
"cider-converter.exe",
"$calyx-base/target/debug/cider-data-converter",
)?;
e.rule(
"cider",
"$cider-exe -l $calyx-base --raw --data data.json $in > $out",
Expand All @@ -311,6 +316,21 @@ pub fn build_driver(bld: &mut DriverBuilder) {
&["$sim_data"],
&["interp-dat.py"],
)?;

e.rule(
"cider2",
"$cider-exe -l $calyx-base --data data.dump $in flat > $out",
)?;

e.rule("dump-to-interp", "$cider-converter --to cider $in > $out")?;
e.rule("interp-to-dump", "$cider-converter --to json $in > $out")?;
e.build_cmd(
&["data.dump"],
"dump-to-interp",
&["$sim_data"],
&["$cider-converter"],
)?;

Ok(())
});
bld.op(
Expand All @@ -330,6 +350,23 @@ pub fn build_driver(bld: &mut DriverBuilder) {
Ok(())
},
);
bld.op(
"interp-flat",
&[sim_setup, calyx_setup, cider_setup],
calyx,
dat,
|e, input, output| {
let out_file = "interp_out.dump";
e.build_cmd(&[out_file], "cider2", &[input], &["data.dump"])?;
e.build_cmd(
&[output],
"interp-to-dump",
&[out_file],
&["$sim_data", "$cider-converter"],
)?;
Ok(())
},
);
bld.op(
"debug",
&[sim_setup, calyx_setup, cider_setup],
Expand Down
8 changes: 8 additions & 0 deletions fud2/tests/snapshots/tests__emit@calyx_debug.snap
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ rule calyx-pass

# Cider interpreter
cider-exe = $calyx-base/target/debug/cider
cider-converter = $calyx-base/target/debug/cider-data-converter
rule cider
command = $cider-exe -l $calyx-base --raw --data data.json $in > $out
rule cider-debug
Expand All @@ -43,6 +44,13 @@ rule dat-to-interp
rule interp-to-dat
command = $python interp-dat.py --from-interp $in $sim_data > $out
build data.json: dat-to-interp $sim_data | interp-dat.py
rule cider2
command = $cider-exe -l $calyx-base --data data.dump $in flat > $out
rule dump-to-interp
command = $cider-converter --to cider $in > $out
rule interp-to-dump
command = $cider-converter --to json $in > $out
build data.dump: dump-to-interp $sim_data | $cider-converter

# build targets
build _pseudo_debug: cider-debug stdin | data.json
Expand Down
8 changes: 8 additions & 0 deletions fud2/tests/snapshots/tests__emit@calyx_interp_dat.snap
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ rule calyx-pass

# Cider interpreter
cider-exe = $calyx-base/target/debug/cider
cider-converter = $calyx-base/target/debug/cider-data-converter
rule cider
command = $cider-exe -l $calyx-base --raw --data data.json $in > $out
rule cider-debug
Expand All @@ -43,6 +44,13 @@ rule dat-to-interp
rule interp-to-dat
command = $python interp-dat.py --from-interp $in $sim_data > $out
build data.json: dat-to-interp $sim_data | interp-dat.py
rule cider2
command = $cider-exe -l $calyx-base --data data.dump $in flat > $out
rule dump-to-interp
command = $cider-converter --to cider $in > $out
rule interp-to-dump
command = $cider-converter --to json $in > $out
build data.dump: dump-to-interp $sim_data | $cider-converter

# build targets
build interp_out.json: cider stdin | data.json
Expand Down
2 changes: 1 addition & 1 deletion interp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "interp"
version = "0.1.1"
authors = ["The Calyx authors"]
edition = "2021"
rust-version.workspace = true
rust-version = "1.73"

[lib]
doctest = false # Don't run doc tests
Expand Down
33 changes: 19 additions & 14 deletions interp/src/flatten/structures/environment/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,17 @@ impl CellLedger {
}

#[must_use]
pub(crate) fn as_primitive(&self) -> Option<&dyn Primitive> {
if let Self::Primitive { cell_dyn } = self {
Some(&**cell_dyn)
} else {
None
pub fn as_primitive(&self) -> Option<&dyn Primitive> {
match self {
Self::Primitive { cell_dyn } => Some(&**cell_dyn),
_ => None,
}
}

pub fn unwrap_primitive(&self) -> &dyn Primitive {
self.as_primitive()
.expect("Unwrapped cell ledger as primitive but received component")
}
}

impl Debug for CellLedger {
Expand Down Expand Up @@ -485,6 +489,10 @@ impl<'a> Simulator<'a> {
&self.env.ports[port_idx]
}

pub(crate) fn get_root_component(&self) -> &ComponentLedger {
self.env.cells.first().unwrap().as_comp().unwrap()
}

/// Attempt to find the parent cell for a port. If no such cell exists (i.e.
/// it is a hole port, then it returns None)
fn _get_parent_cell(
Expand Down Expand Up @@ -578,10 +586,10 @@ impl<'a> Simulator<'a> {
// program counter as the size
let mut leaf_nodes = vec![];

let mut par_map = std::mem::take(self.env.pc.par_map_mut());
let mut new_nodes = vec![];
let (vecs, par_map) = self.env.pc.mut_refs();

self.env.pc.vec_mut().retain_mut(|node| {
vecs.retain_mut(|node| {
// just considering a single node case for the moment
match &self.env.ctx.primary[node.control_node_idx] {
ControlNode::Seq(seq) => {
Expand Down Expand Up @@ -702,8 +710,6 @@ impl<'a> Simulator<'a> {

// insert all the new nodes from the par into the program counter
self.env.pc.vec_mut().extend(new_nodes);
// return the par map to the program counter
*self.env.pc.par_map_mut() = par_map;

self.undef_all_ports();

Expand Down Expand Up @@ -939,15 +945,15 @@ impl<'a> Simulator<'a> {
let entrypoint_secondary = &ctx.secondary[ctx.entry_point];

let mut dump = DataDump::new_empty_with_top_level(
ctx.secondary[entrypoint_secondary.name].clone(),
ctx.lookup_string(entrypoint_secondary.name).clone(),
);

let root = self.env.cells.first().unwrap().as_comp().unwrap();
let root = self.get_root_component();

for (offset, idx) in entrypoint_secondary.cell_offset_map.iter() {
let cell_info = &ctx.secondary[*idx];
let cell_index = &root.index_bases + offset;
let name = ctx.secondary[cell_info.name].clone();
let name = ctx.lookup_string(cell_info.name).clone();
if let CellPrototype::Memory { width, dims, .. } =
&cell_info.prototype
{
Expand All @@ -957,8 +963,7 @@ impl<'a> Simulator<'a> {
dims.size(),
dims.as_serializing_dim(),
self.env.cells[cell_index]
.as_primitive()
.unwrap()
.unwrap_primitive()
.dump_memory_state()
.unwrap(),
)
Expand Down
13 changes: 12 additions & 1 deletion interp/src/flatten/structures/environment/program_counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,13 +369,24 @@ impl ProgramCounter {
&mut self.vec
}

pub fn par_map_mut(&mut self) -> &mut HashMap<ControlPoint, ChildCount> {
pub fn _par_map_mut(&mut self) -> &mut HashMap<ControlPoint, ChildCount> {
&mut self.par_map
}

pub fn _par_map(&self) -> &HashMap<ControlPoint, ChildCount> {
&self.par_map
}

/// returns mutable references to both vec and par_map.
/// This is useful for when you need to mutate both at the same time.
pub fn mut_refs(
&mut self,
) -> (
&mut Vec<ControlPoint>,
&mut HashMap<ControlPoint, ChildCount>,
) {
(&mut self.vec, &mut self.par_map)
}
}

impl<'a> IntoIterator for &'a ProgramCounter {
Expand Down
2 changes: 1 addition & 1 deletion tools/cider-data-converter/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "cider-data-converter"
authors.workspace = true
rust-version.workspace = true
rust-version = "1.73"
edition.workspace = true
version = "0.1.0"

Expand Down

0 comments on commit deee510

Please sign in to comment.