calyx_opt/analysis/
share_set.rs

1use calyx_ir as ir;
2use calyx_ir::RRC;
3use std::collections::HashSet;
4
5/// Stores a Hashset that contains the type names of all components and primitives
6/// marked with either "share" or "state_share",depending on what the user wants.
7/// Methods implemented by this struct can
8/// be used to determine whether a given cell is shareable or not
9/// Used by `live_range_analysis.rs`, `cell_share.rs`, and `infer_share.rs`
10#[derive(Default, Clone)]
11pub struct ShareSet {
12    shareable: HashSet<ir::Id>,
13    is_state_share: bool,
14}
15
16impl ShareSet {
17    pub fn new(shareable: HashSet<ir::Id>, is_state_share: bool) -> Self {
18        ShareSet {
19            shareable,
20            is_state_share,
21        }
22    }
23
24    ///Constructs a shareset from the context. Looks for "state_share" types if
25    ///is_state_share is true, and "share" types otherwise.
26    pub fn from_context<const IS_STATE_SHARE: bool>(ctx: &ir::Context) -> Self {
27        let keyword = if IS_STATE_SHARE {
28            ir::BoolAttr::StateShare
29        } else {
30            ir::BoolAttr::Share
31        };
32        let mut shareable = HashSet::new();
33        for prim in ctx.lib.signatures() {
34            if prim.attributes.has(keyword) {
35                shareable.insert(prim.name);
36            }
37        }
38        for comp in &ctx.components {
39            if comp.attributes.has(keyword) {
40                shareable.insert(comp.name);
41            }
42        }
43        ShareSet {
44            shareable,
45            is_state_share: IS_STATE_SHARE,
46        }
47    }
48
49    ///Adds id to self
50    pub fn add(&mut self, id: ir::Id) {
51        self.shareable.insert(id);
52    }
53
54    ///Checks if id contains self
55    pub fn contains(&self, id: &ir::Id) -> bool {
56        self.shareable.contains(id)
57    }
58
59    ///Returns whether or not this instance is state_share
60    pub fn is_state_share(&self) -> bool {
61        self.is_state_share
62    }
63
64    ///Given a set of shareable and a cell, determines whether cell's
65    ///type is shareable or not
66    pub fn is_shareable_component(&self, cell: &RRC<ir::Cell>) -> bool {
67        if let Some(ref type_name) = cell.borrow().type_name() {
68            self.contains(type_name)
69        } else {
70            false
71        }
72    }
73}