calyx_opt/analysis/
control_id.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
use calyx_ir as ir;

const NODE_ID: ir::Attribute =
    ir::Attribute::Internal(ir::InternalAttr::NODE_ID);
const BEGIN_ID: ir::Attribute =
    ir::Attribute::Internal(ir::InternalAttr::BEGIN_ID);
const END_ID: ir::Attribute = ir::Attribute::Internal(ir::InternalAttr::END_ID);

/// Adding "NODE_ID", "BEGIN_ID", and "END_ID" attribute to control statement
pub struct ControlId;

impl ControlId {
    fn compute_unique_ids_static(
        scon: &mut ir::StaticControl,
        mut cur_state: u64,
        two_if_ids: bool,
    ) -> u64 {
        match scon {
            ir::StaticControl::Empty(_) => cur_state,
            ir::StaticControl::Enable(ir::StaticEnable {
                attributes, ..
            })
            | ir::StaticControl::Invoke(ir::StaticInvoke {
                attributes, ..
            }) => {
                attributes.insert(NODE_ID, cur_state);
                cur_state + 1
            }
            ir::StaticControl::Repeat(ir::StaticRepeat {
                attributes,
                body,
                ..
            }) => {
                attributes.insert(NODE_ID, cur_state);
                cur_state += 1;
                Self::compute_unique_ids_static(body, cur_state, two_if_ids)
            }
            ir::StaticControl::Par(ir::StaticPar {
                stmts, attributes, ..
            })
            | ir::StaticControl::Seq(ir::StaticSeq {
                stmts, attributes, ..
            }) => {
                attributes.insert(NODE_ID, cur_state);
                cur_state += 1;
                stmts.iter_mut().for_each(|stmt| {
                    let new_state = Self::compute_unique_ids_static(
                        stmt, cur_state, two_if_ids,
                    );
                    cur_state = new_state;
                });
                cur_state
            }
            ir::StaticControl::If(ir::StaticIf {
                tbranch,
                fbranch,
                attributes,
                ..
            }) => {
                if two_if_ids {
                    attributes.insert(BEGIN_ID, cur_state);
                    cur_state += 1;
                    cur_state = Self::compute_unique_ids_static(
                        tbranch, cur_state, two_if_ids,
                    );
                    cur_state = Self::compute_unique_ids_static(
                        fbranch, cur_state, two_if_ids,
                    );
                    attributes.insert(END_ID, cur_state);
                    cur_state + 1
                } else {
                    attributes.insert(NODE_ID, cur_state);
                    cur_state += 1;
                    cur_state = Self::compute_unique_ids_static(
                        tbranch, cur_state, two_if_ids,
                    );
                    cur_state = Self::compute_unique_ids_static(
                        fbranch, cur_state, two_if_ids,
                    );
                    cur_state + 1
                }
            }
        }
    }

    /// Adds the @NODE_ID attribute to all control stmts except emtpy ones.
    /// If two_if_ids is true, then if statements get a BEGIN_ID and END_ID instead
    /// of a NODE_ID
    ///
    /// ## Example:
    /// ```
    /// seq { A; if cond {X} else{Y}; par { C; D; }; E }
    /// ```
    ///
    /// gets the labels (if two_if_ids is):
    ///
    /// ```
    /// @NODE_ID(0)seq {
    ///   @NODE_ID(1) A;
    ///   @BEGIN_ID(2) @END_ID(5) if cond {
    ///     @NODE_ID(3) X
    ///   }
    ///   else{
    ///     @NODE_ID(4) Y
    ///   }
    ///   @NODE_ID(6) par {
    ///     @NODE_ID(7) C;
    ///     @NODE_ID(8) D;
    ///   }
    ///   @NODE_ID(9) E;
    /// }
    /// ```
    /// if two_if_ids were false, the if statement would just get a single NODE_ID
    pub fn compute_unique_ids(
        con: &mut ir::Control,
        mut cur_state: u64,
        two_if_ids: bool,
    ) -> u64 {
        match con {
            ir::Control::Enable(ir::Enable { attributes, .. })
            | ir::Control::Invoke(ir::Invoke { attributes, .. }) => {
                attributes.insert(NODE_ID, cur_state);
                cur_state + 1
            }
            ir::Control::Par(ir::Par {
                stmts, attributes, ..
            })
            | ir::Control::Seq(ir::Seq {
                stmts, attributes, ..
            }) => {
                attributes.insert(NODE_ID, cur_state);
                cur_state += 1;
                stmts.iter_mut().for_each(|stmt| {
                    let new_state =
                        Self::compute_unique_ids(stmt, cur_state, two_if_ids);
                    cur_state = new_state;
                });
                cur_state
            }
            ir::Control::If(ir::If {
                tbranch,
                fbranch,
                attributes,
                ..
            }) => {
                if two_if_ids {
                    attributes.insert(BEGIN_ID, cur_state);
                    cur_state += 1;
                    cur_state = Self::compute_unique_ids(
                        tbranch, cur_state, two_if_ids,
                    );
                    cur_state = Self::compute_unique_ids(
                        fbranch, cur_state, two_if_ids,
                    );
                    attributes.insert(END_ID, cur_state);
                    cur_state + 1
                } else {
                    attributes.insert(NODE_ID, cur_state);
                    cur_state += 1;
                    cur_state = Self::compute_unique_ids(
                        tbranch, cur_state, two_if_ids,
                    );
                    cur_state = Self::compute_unique_ids(
                        fbranch, cur_state, two_if_ids,
                    );
                    cur_state + 1
                }
            }
            ir::Control::While(ir::While {
                body, attributes, ..
            })
            | ir::Control::Repeat(ir::Repeat {
                body, attributes, ..
            }) => {
                attributes.insert(NODE_ID, cur_state);
                cur_state += 1;
                Self::compute_unique_ids(body, cur_state, two_if_ids)
            }
            ir::Control::Static(s) => {
                Self::compute_unique_ids_static(s, cur_state, two_if_ids)
            }
            ir::Control::Empty(_) => cur_state,
        }
    }

    // Gets attribute s from c, panics otherwise. Should be used when you know
    // that c has attribute s.
    pub fn get_guaranteed_attribute<A>(c: &ir::Control, attr: A) -> u64
    where
        A: Into<ir::Attribute>,
    {
        c.get_attribute(attr.into()).unwrap_or_else(||unreachable!(
          "called get_guaranteed_attribute, meaning we had to be sure it had the attribute"
      ))
    }

    // Gets attribute s from c, panics otherwise. Should be used when you know
    // that c has attribute s.
    pub fn get_guaranteed_attribute_static<A>(
        sc: &ir::StaticControl,
        attr: A,
    ) -> u64
    where
        A: Into<ir::Attribute>,
    {
        sc.get_attribute(attr.into()).unwrap_or_else(||unreachable!(
          "called get_guaranteed_attribute_static, meaning we had to be sure it had the attribute"
      ))
    }

    // Gets attribute NODE_ID from c
    pub fn get_guaranteed_id(c: &ir::Control) -> u64 {
        Self::get_guaranteed_attribute(c, NODE_ID)
    }

    // Gets attribute NODE_ID from c
    pub fn get_guaranteed_id_static(sc: &ir::StaticControl) -> u64 {
        Self::get_guaranteed_attribute_static(sc, NODE_ID)
    }

    // takes in a static control scon, and adds unique id to each static enable.
    // Returns cur_state, i.e., what the next enable should be labeled as
    pub fn add_static_enable_ids_static(
        scon: &mut ir::StaticControl,
        mut cur_state: u64,
    ) -> u64 {
        match scon {
            ir::StaticControl::Enable(se) => {
                se.attributes.insert(NODE_ID, cur_state);
                cur_state + 1
            }
            ir::StaticControl::Invoke(_) | ir::StaticControl::Empty(_) => {
                cur_state
            }
            ir::StaticControl::Par(ir::StaticPar { stmts, .. })
            | ir::StaticControl::Seq(ir::StaticSeq { stmts, .. }) => {
                for stmt in stmts {
                    let new_state =
                        Self::add_static_enable_ids_static(stmt, cur_state);
                    cur_state = new_state
                }
                cur_state
            }
            ir::StaticControl::If(ir::StaticIf {
                tbranch, fbranch, ..
            }) => {
                let mut new_state =
                    Self::add_static_enable_ids_static(tbranch, cur_state);
                cur_state = new_state;
                new_state =
                    Self::add_static_enable_ids_static(fbranch, cur_state);
                new_state
            }
            ir::StaticControl::Repeat(ir::StaticRepeat { body, .. }) => {
                Self::add_static_enable_ids_static(body, cur_state)
            }
        }
    }

    // takes in ir::Control `con`, and adds unique id to every static enable within it.
    // returns u64 `cur_state` that says what the next staticenable should be labeled as.
    pub fn add_static_enable_ids(
        con: &mut ir::Control,
        mut cur_state: u64,
    ) -> u64 {
        match con {
            ir::Control::Enable(_)
            | ir::Control::Invoke(_)
            | ir::Control::Empty(_) => cur_state,
            ir::Control::Par(ir::Par { stmts, .. })
            | ir::Control::Seq(ir::Seq { stmts, .. }) => {
                for stmt in stmts {
                    let new_state =
                        Self::add_static_enable_ids(stmt, cur_state);
                    cur_state = new_state
                }
                cur_state
            }
            ir::Control::If(ir::If {
                tbranch, fbranch, ..
            }) => {
                let mut new_state =
                    Self::add_static_enable_ids(tbranch, cur_state);
                cur_state = new_state;
                new_state = Self::add_static_enable_ids(fbranch, cur_state);
                new_state
            }
            ir::Control::While(ir::While { body, .. })
            | ir::Control::Repeat(ir::Repeat { body, .. }) => {
                Self::add_static_enable_ids(body, cur_state)
            }
            ir::Control::Static(s) => {
                Self::add_static_enable_ids_static(s, cur_state)
            }
        }
    }

    // Gets NODE_ID from StaticEnable se, panics otherwise. Should be used when you know
    // that se has attributes NODE_ID.
    pub fn get_guaranteed_enable_id(se: &ir::StaticEnable) -> u64 {
        se.get_attribute(NODE_ID).unwrap_or_else(||unreachable!(
          "called get_guaranteed_enable_id, meaning we had to be sure it had a NODE_ID attribute"
      ))
    }
}