calyx_opt/passes/
add_guard.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
use crate::traversal::{Action, Named, VisResult, Visitor};
use calyx_ir as ir;

#[derive(Default)]
pub struct AddGuard;

impl Named for AddGuard {
    fn name() -> &'static str {
        "add-guard"
    }

    fn description() -> &'static str {
        "Add guard %[0: n] where n is latency of static component for each assignment in the static enable of static component."
    }
}

impl Visitor for AddGuard {
    fn start_static_control(
        &mut self,
        s: &mut ir::StaticControl,
        comp: &mut ir::Component,
        _sigs: &ir::LibrarySignatures,
        _comps: &[ir::Component],
    ) -> VisResult {
        if let Some(latency) = comp.latency {
            let lat_u64: u64 = latency.into();
            if lat_u64 > 1 {
                if let ir::StaticControl::Enable(sen) = s {
                    for assign in sen.group.borrow_mut().assignments.iter_mut()
                    {
                        let g = ir::Guard::Info(ir::StaticTiming::new((
                            0, lat_u64,
                        )));
                        let new_g = ir::Guard::And(
                            Box::new(g),
                            std::mem::replace(
                                &mut assign.guard,
                                Box::new(ir::Guard::True),
                            ),
                        );
                        assign.guard = Box::new(new_g);
                    }
                } else {
                    unreachable!(
                        "Non-Enable Static Control should have been compiled away. Run {} to do this",
                        crate::passes::StaticInliner::name()
                    );
                }
            }
        }
        Ok(Action::Continue)
    }
}