Skip to content

Commit

Permalink
wrap-main instantiated old toplevel component in main (#1590)
Browse files Browse the repository at this point in the history
  • Loading branch information
rachitnigam authored Jul 8, 2023
1 parent 03bac33 commit 2d6cd16
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## Unreleased
- Fix: `wrap-main` correctly instantiates the original `"toplevel"` component in the generated `main` component.


## 0.3.0
- `ir::Component` takes a `has_interface` argument and ensures that interface ports are present when it is true.
- The `Visitor` trait supports new `start_context` and `finish_context` methods which allow the pass to affect the context before and after the components are visited respectively.
Expand Down
23 changes: 23 additions & 0 deletions calyx-opt/src/passes/wrap_main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::borrow::BorrowMut;

use crate::traversal::{Action, Named, VisResult, Visitor};
use calyx_ir as ir;
use ir::build_assignments;

#[derive(Default)]
/// If the top-level component is not named `main`, adds a new `main` component
Expand Down Expand Up @@ -54,6 +55,11 @@ impl Visitor for WrapMain {
);
return Ok(Action::Stop);
}
let entry_name = entry.name;
let mut ports = sig.get_signature();
ports
.iter_mut()
.for_each(|pd| pd.direction = pd.direction.reverse());
drop(sig);

// Remove top-level attribute from previous component
Expand All @@ -66,6 +72,23 @@ impl Visitor for WrapMain {
main.borrow_mut()
.attributes
.insert(ir::BoolAttr::TopLevel, 1);

// Add the original top-level component as a cell to the main component.
{
let mut builder = ir::Builder::new(&mut main, &ctx.lib);
let comp = builder.add_component(entry_name, entry_name, ports);
let main_sig = builder.component.signature.clone();
let cont_assigns = build_assignments!(builder;
comp["go"] = ? main_sig["go"];
main_sig["done"] = ? comp["done"];
);
builder
.component
.continuous_assignments
.extend(cont_assigns);
}

// Update the context entrypoint to be the main component
ctx.entrypoint = main.name;
ctx.components.push(main);

Expand Down
9 changes: 7 additions & 2 deletions tests/passes/wrap-main.expect
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ component foo(@go go: 1, @clk clk: 1, @reset reset: 1) -> (@done done: 1) {
control {}
}
component main<"toplevel"=1>(@go go: 1, @clk clk: 1, @reset reset: 1) -> (@done done: 1) {
cells {}
wires {}
cells {
@generated foo = foo();
}
wires {
foo.go = go;
done = foo.done;
}
control {}
}

0 comments on commit 2d6cd16

Please sign in to comment.