calyx_opt/passes/
infer_share.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
use crate::analysis::{DominatorMap, ShareSet};
use crate::traversal::{
    Action, ConstructVisitor, Named, Order, ParseVal, PassOpt, VisResult,
    Visitor,
};
use calyx_ir as ir;
use calyx_utils::{CalyxResult, OutputFile};

/// This pass checks if components are (state) shareable. Here is the process it
/// goes through: if a component uses any ref cells, or non-shareable cells then it
/// is automatically not shareable. Otherwise, check if each read of a stateful
/// cell is guaranteed to be dominated by a write to the same cell-- we check this
/// by building a domination map. If so, component is state shareable.
pub struct InferShare {
    print_dmap: Option<OutputFile>,
    print_static_analysis: Option<OutputFile>,
    state_shareable: ShareSet,
    shareable: ShareSet,
    //name of main (so we can skip it)
    main: ir::Id,
}

impl Named for InferShare {
    fn name() -> &'static str {
        "infer-share"
    }

    fn description() -> &'static str {
        "Infer User Defined Components as Shareable"
    }

    fn opts() -> Vec<PassOpt> {
        vec![
            PassOpt::new(
                "print-dmap",
                "Print the domination map",
                ParseVal::OutStream(OutputFile::Null),
                PassOpt::parse_outstream,
            ),
            PassOpt::new(
                "print-static-analysis",
                "Prints the domination analysis for static dmaps",
                ParseVal::OutStream(OutputFile::Null),
                PassOpt::parse_outstream,
            ),
        ]
    }
}

impl ConstructVisitor for InferShare {
    fn from(ctx: &ir::Context) -> CalyxResult<Self>
    where
        Self: Sized + Named,
    {
        let opts = Self::get_opts(ctx);

        let state_shareable = ShareSet::from_context::<true>(ctx);
        let shareable = ShareSet::from_context::<false>(ctx);

        Ok(InferShare {
            print_dmap: opts[&"print-dmap"].not_null_outstream(),
            print_static_analysis: opts[&"print-static-analysis"]
                .not_null_outstream(),
            state_shareable,
            shareable,
            main: ctx.entrypoint,
        })
    }

    fn clear_data(&mut self) {}
}

impl Visitor for InferShare {
    fn iteration_order() -> Order {
        Order::Post
    }
    fn start(
        &mut self,
        comp: &mut ir::Component,
        _sigs: &ir::LibrarySignatures,
        _comps: &[ir::Component],
    ) -> VisResult {
        //if the component is main, then we can stop checking
        if comp.name == self.main {
            return Ok(Action::Stop);
        }

        // closure to determine if cell is type ThisComponent or Constant
        let const_or_this = |cell: &ir::RRC<ir::Cell>| -> bool {
            matches!(
                cell.borrow().prototype,
                ir::CellType::ThisComponent | ir::CellType::Constant { .. }
            )
        };

        // returns true if cell is shareble, state_shareable, Constant, or This component
        let type_is_shareable = |cell: &ir::RRC<ir::Cell>| -> bool {
            const_or_this(cell)
                || self.shareable.is_shareable_component(cell)
                || self.state_shareable.is_shareable_component(cell)
        };

        // cannot contain any external cells, or any cells of a "non-shareable" type
        // (i.e. not shareable, state_shareable, const or This component)
        if comp.cells.iter().any(|cell| {
            !type_is_shareable(cell) && !cell.borrow().is_reference()
        }) {
            return Ok(Action::Stop);
        }

        // build the domination map
        let mut dmap =
            DominatorMap::new(&mut comp.control.borrow_mut(), comp.name);

        // print the domination map if command line argument says so
        if let Some(s) = &mut self.print_dmap {
            write!(s.get_write(), "{dmap:?}").unwrap();
        }
        if let Some(s) = &mut self.print_static_analysis {
            write!(s.get_write(), "{:?}", dmap.static_par_domination).unwrap();
        }

        for (node, dominators) in dmap.map.iter_mut() {
            //get the reads
            let reads =
                DominatorMap::get_node_reads(node, comp, &self.state_shareable);

            //if read and write occur in same group/invoke, then we cannot label it
            //shareable. So we remove node from its dominators
            dominators.remove(node);
            for cell_name in reads {
                if !DominatorMap::key_written_guaranteed(
                    cell_name, dominators, comp,
                ) {
                    return Ok(Action::Stop);
                }
            }
        }
        comp.attributes.insert(ir::BoolAttr::StateShare, 1);
        self.state_shareable.add(comp.name);
        Ok(Action::Stop)
    }
}