calyx_opt/passes/group_to_invoke.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 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
use crate::analysis::AssignmentAnalysis;
use crate::traversal::{Action, ConstructVisitor, Named, VisResult, Visitor};
use calyx_ir::{self as ir};
use calyx_ir::{GetAttributes, RRC};
use calyx_utils::CalyxResult;
use ir::Nothing;
use itertools::Itertools;
use std::collections::{HashMap, HashSet};
use std::rc::Rc;
/// Transform groups that are structurally invoking components into equivalent
/// [ir::Invoke] statements.
///
/// For a group to meet the requirements of this pass, it must
/// 1. Only write to one non-combinational component (all other writes must be
/// to combinational primitives)
/// 2. That component is *not* a ref cell, nor does it have the external attribute,
/// nor is it This Component
/// 3. Assign component.go = 1'd1
/// 4. Assign group[done] = component.done
pub struct GroupToInvoke {
/// Primitives that have multiple @go-@done signals
blacklist: HashSet<ir::Id>,
/// Maps names of group to the invokes that will replace them
group_invoke_map: HashMap<ir::Id, ir::Control>,
}
impl ConstructVisitor for GroupToInvoke {
fn from(ctx: &ir::Context) -> CalyxResult<Self>
where
Self: Sized,
{
// Construct list of primitives that have multiple go-done signals
let blacklist = ctx
.lib
.signatures()
.filter(|p| p.find_all_with_attr(ir::NumAttr::Go).count() > 1)
.map(|p| p.name)
.collect();
Ok(Self {
blacklist,
group_invoke_map: HashMap::new(),
})
}
fn clear_data(&mut self) {
self.group_invoke_map = HashMap::new();
}
}
impl Named for GroupToInvoke {
fn name() -> &'static str {
"group2invoke"
}
fn description() -> &'static str {
"covert groups that structurally invoke one component into invoke statements"
}
}
/// Construct an [ir::Invoke] from an [ir::Group] that has been validated by this pass.
fn construct_invoke(
assigns: &[ir::Assignment<Nothing>],
comp: RRC<ir::Cell>,
builder: &mut ir::Builder,
) -> ir::Control {
// Check if port's parent is a combinational primitive
let parent_is_comb = |port: &ir::Port| -> bool {
if !port.is_hole() {
if let ir::CellType::Primitive { is_comb, .. } =
port.cell_parent().borrow().prototype
{
return is_comb;
}
}
false
};
// Check if port's parent is equal to comp
let parent_is_cell = |port: &ir::Port| -> bool {
match &port.parent {
ir::PortParent::Cell(cell_wref) => {
Rc::ptr_eq(&cell_wref.upgrade(), &comp)
}
_ => false,
}
};
let mut inputs = Vec::new();
let mut comb_assigns = Vec::new();
let mut wire_map: HashMap<ir::Id, ir::RRC<ir::Port>> = HashMap::new();
for assign in assigns {
// We know that all assignments in this group should write to either a)
// a combinational component or b) comp or c) the group's done port-- we
// should have checked for this condition before calling this function
// If a combinational component's port is being used as a dest, add
// it to comb_assigns
if parent_is_comb(&assign.dst.borrow()) {
comb_assigns.push(assign.clone());
}
// If the cell's port is being used as a dest, add the source to
// inputs. we can ignore the cell.go assignment, since that is not
// going to be part of the `invoke`.
else if parent_is_cell(&assign.dst.borrow())
&& assign.dst
!= comp.borrow().get_unique_with_attr(ir::NumAttr::Go).unwrap()
{
let name = assign.dst.borrow().name;
if assign.guard.is_true() {
inputs.push((name, Rc::clone(&assign.src)));
} else {
// assign has a guard condition,so need a wire
// We first check whether we have already built a wire
// for this port or not.
let wire_in = match wire_map.get(&assign.dst.borrow().name) {
Some(w) => {
// Already built a wire, so just need to return the
// wire's input port (which wire_map stores)
Rc::clone(w)
}
None => {
// Need to create a new wire
let width = assign.dst.borrow().width;
let wire = builder.add_primitive(
format!("{}_guarded_wire", name),
"std_wire",
&[width],
);
// Insert the wire's input port into wire_map
let wire_in = wire.borrow().get("in");
wire_map.insert(
assign.dst.borrow().name,
Rc::clone(&wire_in),
);
// add the wire's output port to the inputs of the
// invoke statement we are building
inputs.push((name, wire.borrow().get("out")));
// return wire_in
wire_in
}
};
// Use wire_in to add another assignment to combinational group
let asmt = builder.build_assignment(
wire_in,
Rc::clone(&assign.src),
*assign.guard.clone(),
);
comb_assigns.push(asmt);
}
}
}
let comb_group = if comb_assigns.is_empty() {
None
} else {
let comb_group_ref = builder.add_comb_group("comb_invoke");
comb_group_ref
.borrow_mut()
.assignments
.append(&mut comb_assigns);
Some(comb_group_ref)
};
ir::Control::Invoke(ir::Invoke {
comp,
inputs,
outputs: Vec::new(),
comb_group,
attributes: ir::Attributes::default(),
ref_cells: Vec::new(),
})
}
impl Visitor for GroupToInvoke {
fn start(
&mut self,
comp: &mut ir::Component,
sigs: &ir::LibrarySignatures,
_comps: &[ir::Component],
) -> VisResult {
let groups = comp.get_groups_mut().drain().collect_vec();
let static_groups = comp.get_static_groups_mut().drain().collect_vec();
let mut builder = ir::Builder::new(comp, sigs);
for g in &groups {
self.analyze_group(
&mut builder,
g.borrow().name(),
&g.borrow().assignments,
&g.borrow().get("done"),
)
}
// Not transforming static groups rn
/*for g in &static_groups {
self.analyze_group(
&mut builder,
g.borrow().name(),
&g.borrow().assignments,
&g.borrow().get(ir::NumAttr::Done),
)
}*/
comp.get_groups_mut().append(groups.into_iter());
comp.get_static_groups_mut()
.append(static_groups.into_iter());
Ok(Action::Continue)
}
fn enable(
&mut self,
s: &mut ir::Enable,
_comp: &mut ir::Component,
_sigs: &ir::LibrarySignatures,
_comps: &[ir::Component],
) -> VisResult {
match self.group_invoke_map.get(&s.group.borrow().name()) {
None => Ok(Action::Continue),
Some(invoke) => {
let mut inv = ir::Cloner::control(invoke);
let attrs = std::mem::take(&mut s.attributes);
*inv.get_mut_attributes() = attrs;
Ok(Action::Change(Box::new(inv)))
}
}
}
}
impl GroupToInvoke {
// if g is able to be turned into invoke, then add to self.group_invoke_map
fn analyze_group(
&mut self,
builder: &mut ir::Builder,
group_name: ir::Id,
assigns: &[ir::Assignment<Nothing>],
group_done_port: &ir::RRC<ir::Port>,
) {
let mut writes = assigns
.iter()
.analysis()
.cell_writes()
.filter(|cell| match cell.borrow().prototype {
ir::CellType::Primitive { is_comb, .. } => !is_comb,
_ => true,
})
.collect_vec();
// Excluding writes to combinational components, should write to exactly
// one cell
if writes.len() != 1 {
return;
}
// If component is ThisComponent, Reference, or External, don't turn into invoke
let cr = writes.pop().unwrap();
let cell = cr.borrow();
match &cell.prototype {
ir::CellType::Primitive { name, .. }
if self.blacklist.contains(name) =>
{
return;
}
ir::CellType::ThisComponent => return,
_ => {}
}
if cell.is_reference() || cell.attributes.has(ir::BoolAttr::External) {
return;
}
// Component must define exactly one @go/@done interface
let Ok(Some(go_port)) = cell.find_unique_with_attr(ir::NumAttr::Go)
else {
return;
};
let Ok(Some(done_port)) = cell.find_unique_with_attr(ir::NumAttr::Done)
else {
return;
};
let mut go_wr_cnt = 0;
let mut done_wr_cnt = 0;
'assigns: for assign in assigns {
// @go port should have exactly one write and the src should be 1.
if assign.dst == go_port {
if go_wr_cnt > 0 {
log::info!(
"Cannot transform `{}` due to multiple writes to @go port",
group_name,
);
return;
} else if !assign.guard.is_true() {
log::info!(
"Cannot transform `{}` due to guarded write to @go port: {}",
group_name,
ir::Printer::assignment_to_str(assign)
);
return;
} else if assign.src.borrow().is_constant(1, 1) {
go_wr_cnt += 1;
} else {
// if go port's guard is not true, src is not (1,1), then
// Continue
continue 'assigns;
}
}
// @done port should have exactly one read and the dst should be
// group's done signal.
if assign.src == done_port {
if done_wr_cnt > 0 {
log::info!(
"Cannot transform `{}` due to multiple writes to @done port",
group_name,
);
return;
} else if !assign.guard.is_true() {
log::info!(
"Cannot transform `{}` due to guarded write to @done port: {}",
group_name,
ir::Printer::assignment_to_str(assign)
);
return;
} else if assign.dst == *group_done_port {
done_wr_cnt += 1;
} else {
// If done port's guard is not true and does not write to group's done
// then Continue
continue 'assigns;
}
}
}
drop(cell);
if go_wr_cnt != 1 {
log::info!(
"Cannot transform `{}` because there are no writes to @go port",
group_name
);
return;
} else if done_wr_cnt != 1 {
log::info!(
"Cannot transform `{}` because there are no writes to @done port",
group_name
);
return;
}
self.group_invoke_map
.insert(group_name, construct_invoke(assigns, cr, builder));
}
}