calyx_opt/passes/
add_guard.rs1use crate::traversal::{Action, Named, VisResult, Visitor};
2use calyx_ir as ir;
3
4#[derive(Default)]
5pub struct AddGuard;
6
7impl Named for AddGuard {
8 fn name() -> &'static str {
9 "add-guard"
10 }
11
12 fn description() -> &'static str {
13 "Add guard %[0: n] where n is latency of static component for each assignment in the static enable of static component."
14 }
15}
16
17impl Visitor for AddGuard {
18 fn start_static_control(
19 &mut self,
20 s: &mut ir::StaticControl,
21 comp: &mut ir::Component,
22 _sigs: &ir::LibrarySignatures,
23 _comps: &[ir::Component],
24 ) -> VisResult {
25 if let Some(latency) = comp.latency {
26 let lat_u64: u64 = latency.into();
27 if lat_u64 > 1 {
28 if let ir::StaticControl::Enable(sen) = s {
29 for assign in sen.group.borrow_mut().assignments.iter_mut()
30 {
31 let g = ir::Guard::Info(ir::StaticTiming::new((
32 0, lat_u64,
33 )));
34 let new_g = ir::Guard::And(
35 Box::new(g),
36 std::mem::replace(
37 &mut assign.guard,
38 Box::new(ir::Guard::True),
39 ),
40 );
41 assign.guard = Box::new(new_g);
42 }
43 } else {
44 unreachable!(
45 "Non-Enable Static Control should have been compiled away. Run {} to do this",
46 crate::passes::StaticInliner::name()
47 );
48 }
49 }
50 }
51 Ok(Action::Continue)
52 }
53}