calyx_opt/analysis/
share_set.rs1use calyx_ir as ir;
2use calyx_ir::RRC;
3use std::collections::HashSet;
4
5#[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 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 pub fn add(&mut self, id: ir::Id) {
51 self.shareable.insert(id);
52 }
53
54 pub fn contains(&self, id: &ir::Id) -> bool {
56 self.shareable.contains(id)
57 }
58
59 pub fn is_state_share(&self) -> bool {
61 self.is_state_share
62 }
63
64 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}