calyx_opt/passes/collapse_control.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
use crate::traversal::{Action, Named, VisResult, Visitor};
use calyx_ir::{self as ir, GetAttributes, LibrarySignatures};
#[derive(Default)]
/// Collapses and de-nests control constructs.
///
/// Running this pass removes unnecessary FSM transitions and compilation
/// groups during the lowering phase. If a seq is marked with @new_fsm, then
/// we don't collapse it, since we need that fsm transition to transition
/// from our old fsm to our new one.
///
/// # Example
/// 1. Collapses nested `seq`:
/// ```
/// seq {
/// seq { A; B }
/// C;
/// }
/// ```
/// into
/// ```
/// seq { A; B C; }
/// ```
/// 2. Collapses nested `par`:
/// ```
/// par {
/// par { A; B }
/// C;
/// }
/// ```
/// into
/// ```
/// par { A; B C; }
/// ```
///
/// 3. Collapses nested `static seq` in the same way as 1
/// 4. Collapses nested `static par` in the same way as 2
/// 5. Collapses `static repeat`:
/// ```
/// static repeat 0 { ** body ** }
/// ```
/// into empty control
/// and
/// ```
/// static repeat 1 {** body **}
/// ```
/// into
/// ```
/// ** body **
/// ```
pub struct CollapseControl {}
impl Named for CollapseControl {
fn name() -> &'static str {
"collapse-control"
}
fn description() -> &'static str {
"Collapse nested seq and par."
}
}
impl Visitor for CollapseControl {
/// Collapse seq { seq { A }; B } into seq { A; B }.
fn finish_seq(
&mut self,
s: &mut ir::Seq,
_comp: &mut ir::Component,
_c: &LibrarySignatures,
_comps: &[ir::Component],
) -> VisResult {
if s.stmts.is_empty() {
return Ok(Action::change(ir::Control::empty()));
}
if s.stmts.len() == 1 {
return Ok(Action::change(s.stmts.pop().unwrap()));
}
let mut seqs: Vec<ir::Control> = vec![];
for con in s.stmts.drain(..) {
if con.has_attribute(ir::BoolAttr::NewFSM) {
// if con has attribute new_fsm, then we do *not* want to collapse
seqs.push(con)
} else {
match con {
ir::Control::Seq(mut data) => {
seqs.append(&mut data.stmts);
}
_ => seqs.push(con),
}
}
}
s.stmts = seqs;
Ok(Action::Continue)
}
/// Collapse par { par { A }; B } into par { A; B }.
fn finish_par(
&mut self,
s: &mut ir::Par,
_comp: &mut ir::Component,
_c: &LibrarySignatures,
_comps: &[ir::Component],
) -> VisResult {
if s.stmts.is_empty() {
return Ok(Action::change(ir::Control::empty()));
}
if s.stmts.len() == 1 {
return Ok(Action::change(s.stmts.pop().unwrap()));
}
let mut pars: Vec<ir::Control> = vec![];
for con in s.stmts.drain(..) {
match con {
ir::Control::Par(mut data) => {
pars.append(&mut data.stmts);
}
_ => pars.push(con),
}
}
s.stmts = pars;
Ok(Action::Continue)
}
/// Collapse static par {static par {A; B;}} into static par {A; B; }
fn finish_static_par(
&mut self,
s: &mut ir::StaticPar,
_comp: &mut ir::Component,
_sigs: &LibrarySignatures,
_comps: &[ir::Component],
) -> VisResult {
if s.stmts.is_empty() {
return Ok(Action::static_change(ir::StaticControl::empty()));
}
if s.stmts.len() == 1 {
// Want to preserve @one_hot attribute.
let mut replacement_ctrl = s.stmts.pop().unwrap();
let attrs = std::mem::take(&mut s.attributes);
replacement_ctrl
.get_mut_attributes()
.copy_from(attrs, vec![ir::BoolAttr::OneHot]);
return Ok(Action::static_change(replacement_ctrl));
}
let mut pars: Vec<ir::StaticControl> = vec![];
for con in s.stmts.drain(..) {
match con {
ir::StaticControl::Par(mut data) => {
pars.append(&mut data.stmts);
}
_ => pars.push(con),
}
}
s.stmts = pars;
Ok(Action::Continue)
}
///Collase static seq {static seq {A; B; }} into static seq {A; B;}
fn finish_static_seq(
&mut self,
s: &mut ir::StaticSeq,
_comp: &mut ir::Component,
_sigs: &LibrarySignatures,
_comps: &[ir::Component],
) -> VisResult {
if s.stmts.is_empty() {
return Ok(Action::static_change(ir::StaticControl::empty()));
}
if s.stmts.len() == 1 {
// Want to preserve @one_hot attribute.
let mut replacement_ctrl = s.stmts.pop().unwrap();
let attrs = std::mem::take(&mut s.attributes);
replacement_ctrl
.get_mut_attributes()
.copy_from(attrs, vec![ir::BoolAttr::OneHot]);
return Ok(Action::static_change(replacement_ctrl));
}
let mut seqs: Vec<ir::StaticControl> = vec![];
for con in s.stmts.drain(..) {
match con {
ir::StaticControl::Seq(mut data) => {
seqs.append(&mut data.stmts);
}
_ => seqs.push(con),
}
}
s.stmts = seqs;
Ok(Action::Continue)
}
/// Collapse
/// ```
/// static repeat 0 { ** body ** }
/// ```
/// into empty control
/// and
/// ```
/// static repeat 1 {** body **}
/// into
/// ```
/// ** body **
/// ```
fn finish_static_repeat(
&mut self,
s: &mut ir::StaticRepeat,
_comp: &mut ir::Component,
_sigs: &LibrarySignatures,
_comps: &[ir::Component],
) -> VisResult {
if s.num_repeats == 0 {
return Ok(Action::static_change(ir::StaticControl::empty()));
}
if s.num_repeats == 1 {
return Ok(Action::static_change(s.body.take_static_control()));
}
Ok(Action::Continue)
}
fn finish_repeat(
&mut self,
s: &mut ir::Repeat,
_comp: &mut ir::Component,
_sigs: &LibrarySignatures,
_comps: &[ir::Component],
) -> VisResult {
if s.num_repeats == 0 {
return Ok(Action::change(ir::Control::empty()));
}
if s.num_repeats == 1 {
return Ok(Action::change(s.body.take_control()));
}
Ok(Action::Continue)
}
}