Skip to content

Commit

Permalink
Modified dead group removal to retain references to group ports withi…
Browse files Browse the repository at this point in the history
…n FSMs
  • Loading branch information
parthsarkar17 committed Nov 26, 2024
1 parent 7313dee commit e64c316
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
50 changes: 50 additions & 0 deletions calyx-ir/src/structure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,56 @@ impl FSM {
.expect(&msg)
.extend(assigns);
}

pub fn get_called_groups(&self) -> Vec<Id> {
// groups that are used in the transitions section of this FSM
let mut called_groups = self
.transitions
.iter()
.flat_map(|trans| match trans {
Transition::Unconditional(_) => vec![],
Transition::Conditional(conds) => {
conds.iter().flat_map(|(guard, _)| {
let mut group_names = vec![];
guard.all_ports().iter().for_each(|port| {
if let PortParent::Group(group_wref) =
&port.borrow().parent
{
group_names
.push(group_wref.upgrade().borrow().name());
}
});
group_names
})
}
.collect(),
})
.collect_vec();

// groups that are used in the assignments section

called_groups.extend(
self.assignments
.iter()
.flat_map(|assigns| {
let mut groups_accessed = vec![];
for assign in assigns.iter() {
assign.iter_ports().for_each(|port| {
if let PortParent::Group(group_wref) =
&port.borrow().parent
{
groups_accessed
.push(group_wref.upgrade().borrow().name());
}
});
}
groups_accessed
})
.collect_vec(),
);

called_groups
}
}

impl GetName for Cell {
Expand Down
17 changes: 17 additions & 0 deletions calyx-opt/src/passes/dead_group_removal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ impl Visitor for DeadGroupRemoval {
Ok(Action::Continue)
}

fn fsm_enable(
&mut self,
s: &mut calyx_ir::FSMEnable,
_comp: &mut calyx_ir::Component,
_sigs: &LibrarySignatures,
_comps: &[calyx_ir::Component],
) -> VisResult {
// add all groups that are assigned to / read from, by the parent FSM
self.used_groups.extend(s.fsm.borrow().get_called_groups());
Ok(Action::Continue)
}

fn static_enable(
&mut self,
s: &mut ir::StaticEnable,
Expand Down Expand Up @@ -117,6 +129,11 @@ impl Visitor for DeadGroupRemoval {
}
}

// for now, add all groups invoked by each fsm
for fsm in comp.get_fsms().iter() {
self.used_groups.extend(fsm.borrow().get_called_groups());
}

// Remove Groups that are not used
comp.get_groups_mut()
.retain(|g| self.used_groups.contains(&g.borrow().name()));
Expand Down

0 comments on commit e64c316

Please sign in to comment.