Skip to content

Commit

Permalink
comb components in clk and reset insertion
Browse files Browse the repository at this point in the history
  • Loading branch information
rachitnigam committed Jul 26, 2023
1 parent 68ca746 commit dc10209
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 24 deletions.
39 changes: 28 additions & 11 deletions calyx-opt/src/passes/clk_insertion.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::traversal::{Action, Named, VisResult, Visitor};
use calyx_ir::{self as ir, LibrarySignatures};
use calyx_utils::Error;
use std::rc::Rc;

#[derive(Default)]
Expand All @@ -25,22 +26,38 @@ impl Visitor for ClkInsertion {
_comps: &[ir::Component],
) -> VisResult {
let builder = ir::Builder::new(comp, sigs);
// Find @clk port in the component. If it doesn't exist,
// then we don't need to do anything.
let clk = builder
.component
.signature
.borrow()
.get_with_attr(ir::BoolAttr::Clk);
.find_with_attr(ir::BoolAttr::Clk);

for cell_ref in builder.component.cells.iter() {
let cell = cell_ref.borrow();
if let Some(port) = cell.find_with_attr(ir::BoolAttr::Clk) {
builder.component.continuous_assignments.push(
builder.build_assignment(
port,
Rc::clone(&clk),
ir::Guard::True,
),
)
if let Some(clk) = clk {
for cell_ref in builder.component.cells.iter() {
let cell = cell_ref.borrow();
if let Some(port) = cell.find_with_attr(ir::BoolAttr::Clk) {
builder.component.continuous_assignments.push(
builder.build_assignment(
port,
Rc::clone(&clk),
ir::Guard::True,
),
)
}
}
} else {
for cell_ref in builder.component.cells.iter() {
let cell = cell_ref.borrow();
if cell.find_with_attr(ir::BoolAttr::Clk).is_some() {
return Err(Error::malformed_structure(format!(
"Cell `{}' in component `{}' has a clk port, \
but the component does not have a clk port.",
cell.name(),
builder.component.name
)));
}
}
}

Expand Down
40 changes: 27 additions & 13 deletions calyx-opt/src/passes/reset_insertion.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::rc::Rc;

use crate::traversal::{Action, Named, VisResult, Visitor};
use calyx_ir::{self as ir, LibrarySignatures};
use calyx_utils::Error;
use std::rc::Rc;

#[derive(Default)]
/// Adds assignments from a components `reset` port to every
Expand Down Expand Up @@ -30,18 +30,32 @@ impl Visitor for ResetInsertion {
.component
.signature
.borrow()
.get_with_attr(ir::BoolAttr::Reset);
.find_with_attr(ir::BoolAttr::Reset);

for cell_ref in builder.component.cells.iter() {
let cell = cell_ref.borrow();
if let Some(port) = cell.find_with_attr(ir::BoolAttr::Reset) {
builder.component.continuous_assignments.push(
builder.build_assignment(
port,
Rc::clone(&reset),
ir::Guard::True,
),
)
if let Some(reset) = reset {
for cell_ref in builder.component.cells.iter() {
let cell = cell_ref.borrow();
if let Some(port) = cell.find_with_attr(ir::BoolAttr::Reset) {
builder.component.continuous_assignments.push(
builder.build_assignment(
port,
Rc::clone(&reset),
ir::Guard::True,
),
)
}
}
} else {
for cell_ref in builder.component.cells.iter() {
let cell = cell_ref.borrow();
if cell.find_with_attr(ir::BoolAttr::Reset).is_some() {
return Err(Error::malformed_structure(format!(
"Cell `{}' in component `{}' has a reset port, \
but the component does not have a reset port.",
cell.name(),
builder.component.name
)));
}
}
}

Expand Down
21 changes: 21 additions & 0 deletions tests/passes/clk-insertion.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import "primitives/core.futil";
comb component layout_hw0() -> (flat_port_addr0: 4) {
cells {
add_0 = std_add(4);
}
wires {
flat_port_addr0 = add_0.out;
add_0.left = 4'd1;
add_0.right = 4'd2;
}
}
component main(@clk clk: 1, @go go: 1, @reset reset: 1) -> (out: 32, @done done: 1) {
cells {
r = std_reg(32);
}
wires {
out = r.out;
r.clk = clk;
}
control {}
}
22 changes: 22 additions & 0 deletions tests/passes/clk-insertion.futil
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// -p well-formed -p clk-insertion
import "primitives/core.futil";
comb component layout_hw0() -> (flat_port_addr0: 4) {
cells {
add_0 = std_add(4);
}
wires {
flat_port_addr0 = add_0.out;
add_0.left = 4'd1;
add_0.right = 4'd2;
}
}

component main(@clk clk: 1) -> (out: 32) {
cells {
r = std_reg(32);
}
wires {
out = r.out;
}
control {}
}

0 comments on commit dc10209

Please sign in to comment.