calyx_opt/passes/
remove_ids.rs1use crate::traversal::{Action, Named, VisResult, Visitor};
2use calyx_ir::{self as ir, GetAttributes, LibrarySignatures};
3
4const NODE_ID: ir::Attribute =
5 ir::Attribute::Internal(ir::InternalAttr::NODE_ID);
6const BEGIN_ID: ir::Attribute =
7 ir::Attribute::Internal(ir::InternalAttr::BEGIN_ID);
8const END_ID: ir::Attribute = ir::Attribute::Internal(ir::InternalAttr::END_ID);
9
10#[derive(Default)]
11pub struct RemoveIds;
13
14impl Named for RemoveIds {
15 fn name() -> &'static str {
16 "remove-ids"
17 }
18
19 fn description() -> &'static str {
20 "removes the NODE_ID, BEGIN_ID, and END_ID from the control flow"
21 }
22}
23
24impl Visitor for RemoveIds {
25 fn start(
26 &mut self,
27 comp: &mut ir::Component,
28 _ctx: &LibrarySignatures,
29 _comps: &[ir::Component],
30 ) -> VisResult {
31 remove_ids(&mut comp.control.borrow_mut());
32 Ok(Action::Stop)
33 }
34}
35
36fn remove_ids_static(sc: &mut ir::StaticControl) {
37 let atts = sc.get_mut_attributes();
38 atts.remove(BEGIN_ID);
39 atts.remove(END_ID);
40 atts.remove(NODE_ID);
41 match sc {
42 ir::StaticControl::Empty(_) | ir::StaticControl::Invoke(_) => (),
43 ir::StaticControl::Enable(ir::StaticEnable { group, .. }) => {
44 group.borrow_mut().remove_attribute(NODE_ID)
45 }
46 ir::StaticControl::Repeat(ir::StaticRepeat { body, .. }) => {
47 remove_ids_static(body)
48 }
49 ir::StaticControl::Seq(ir::StaticSeq { stmts, .. })
50 | ir::StaticControl::Par(ir::StaticPar { stmts, .. }) => {
51 for stmt in stmts {
52 remove_ids_static(stmt);
53 }
54 }
55 ir::StaticControl::If(ir::StaticIf {
56 tbranch, fbranch, ..
57 }) => {
58 remove_ids_static(tbranch);
59 remove_ids_static(fbranch);
60 }
61 }
62}
63
64fn remove_ids(c: &mut ir::Control) {
65 let atts = c.get_mut_attributes();
66 atts.remove(BEGIN_ID);
67 atts.remove(END_ID);
68 atts.remove(NODE_ID);
69 match c {
70 ir::Control::Empty(_) | ir::Control::Invoke(_) => (),
71 ir::Control::Enable(ir::Enable { group, .. }) => {
72 group.borrow_mut().remove_attribute(NODE_ID)
73 }
74 ir::Control::While(ir::While { body, .. })
75 | ir::Control::Repeat(ir::Repeat { body, .. }) => {
76 remove_ids(body);
77 }
78 ir::Control::If(ir::If {
79 tbranch, fbranch, ..
80 }) => {
81 remove_ids(tbranch);
82 remove_ids(fbranch);
83 }
84 ir::Control::Seq(ir::Seq { stmts, .. })
85 | ir::Control::Par(ir::Par { stmts, .. }) => {
86 for stmt in stmts {
87 remove_ids(stmt);
88 }
89 }
90 ir::Control::Static(sc) => remove_ids_static(sc),
91 ir::Control::FSMEnable(_) => {
92 todo!("should not encounter fsm nodes")
93 }
94 }
95}