Skip to content

Commit

Permalink
regrouped assignments outside of always_comb case block (#2379)
Browse files Browse the repository at this point in the history
  • Loading branch information
parthsarkar17 authored Dec 18, 2024
1 parent 2dedfb4 commit a99bdee
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 24 deletions.
22 changes: 12 additions & 10 deletions calyx-backend/src/verilog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ fn emit_fsm<F: io::Write>(fsm: &RRC<ir::FSM>, f: &mut F) -> io::Result<()> {

// dump assignments to enable in this state
emit_fsm_dependent_assignments(
&fsm.borrow().assignments,
fsm.borrow().merge_assignments(),
&state_reg,
reg_bitwidth,
f,
Expand Down Expand Up @@ -527,14 +527,15 @@ fn emit_fsm_assignments<F: io::Write>(
}

fn emit_fsm_dependent_assignments<F: io::Write>(
assignments: &Vec<Vec<ir::Assignment<Nothing>>>,
assignments: Vec<Vec<(usize, ir::Assignment<Nothing>)>>,
fsm_out: &String,
reg_bitwidth: u64,
f: &mut F,
) -> io::Result<()> {
for (case, assigns) in assignments.iter().enumerate() {
for assign in assigns.iter() {
let dst_ref = &assign.dst;
for collection in assignments.iter() {
let dst_ref = &collection.first().unwrap().1.dst;
writeln!(f, "assign {} =", VerilogPortRef(dst_ref))?;
for (i, (case, assign)) in collection.iter().enumerate() {
let case_guard =
format!("{} == {}'d{}", fsm_out, reg_bitwidth, case);

Expand All @@ -557,15 +558,16 @@ fn emit_fsm_dependent_assignments<F: io::Write>(
format!("{}'d0", dst_ref.borrow().width)
};

// emit assignment dependent on both case and the assignment's original guard
writeln!(
f,
"assign {} = {} ? {} : {};",
VerilogPortRef(dst_ref),
" {} ? {} :",
case_guarded_assign_guard,
VerilogPortRef(&assign.src),
guard_unmet_value
VerilogPortRef(&assign.src)
)?;

if i + 1 == collection.len() {
writeln!(f, " {guard_unmet_value};")?;
}
}
}
io::Result::Ok(())
Expand Down
16 changes: 14 additions & 2 deletions calyx-ir/src/structure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -897,13 +897,25 @@ impl FSM {

pub fn merge_assignments(&self) -> Vec<Vec<(usize, Assignment<Nothing>)>> {
let mut gathered_assigns: HashMap<
Id,
String,
Vec<(usize, Assignment<Nothing>)>,
> = HashMap::new();
for (case, assigns_at_state) in self.assignments.iter().enumerate() {
for assign in assigns_at_state.iter() {
let port = assign.dst.borrow();
let dest = match &port.parent {
PortParent::Cell(cell) => {
format!(
"{}_{}",
cell.upgrade().borrow().name,
port.name
)
}
_ => unreachable!(),
};

gathered_assigns
.entry(assign.dst.borrow().name)
.entry(dest)
.and_modify(|gathered| {
gathered.push((case, assign.clone()));
})
Expand Down
15 changes: 3 additions & 12 deletions calyx-opt/src/passes/dyn_fsm_allocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,16 +427,11 @@ impl<'b, 'a> Schedule<'b, 'a> {
.drain()
.sorted_by(|(s1, _), (s2, _)| s1.cmp(s2))
.map(|(state, mut cond_dsts)| {
let mut assigns = match self.fsm_enables.get(&(state - 1)) {
let assigns = match self.fsm_enables.get(&(state - 1)) {
None => vec![],
Some(assigns) => assigns.clone(),
};
let signal_off = self.builder.add_constant(0, 1);
let true_guard = ir::Guard::True;
let not_done = build_assignments!(self.builder;
fsm["done"] = true_guard ? signal_off["out"];
);
assigns.extend(not_done);

// self-loop if all other guards are not met;
// should be at the end of the conditional destinations vec!
cond_dsts.push((ir::Guard::True, state));
Expand All @@ -447,11 +442,7 @@ impl<'b, 'a> Schedule<'b, 'a> {

// insert transition condition from 0 to 1
let true_guard = ir::Guard::True;
let signal_off = self.builder.add_constant(0, 1);
let not_done = build_assignments!(self.builder;
fsm["done"] = true_guard ? signal_off["out"];
);
assignments.push_front(not_done.to_vec());
assignments.push_front(vec![]);
transitions.push_front(ir::Transition::Conditional(vec![
(guard!(fsm["start"]), 1),
(true_guard.clone(), 0),
Expand Down

0 comments on commit a99bdee

Please sign in to comment.