calyx_opt/passes/profiler_instrumentation.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 353 354 355 356 357 358 359 360 361 362 363
use std::collections::HashMap;
use crate::traversal::{Action, ConstructVisitor, Named, VisResult, Visitor};
use calyx_ir::{
self as ir, BoolAttr, Guard, Id, Nothing, NumAttr, StaticTiming,
};
use calyx_utils::CalyxResult;
/// Adds probe wires to each group (includes static groups and comb groups) to detect when a group is active.
/// Used by the profiler.
pub struct ProfilerInstrumentation {}
impl Named for ProfilerInstrumentation {
fn name() -> &'static str {
"profiler-instrumentation"
}
fn description() -> &'static str {
"Add instrumentation for profiling"
}
fn opts() -> Vec<crate::traversal::PassOpt> {
vec![]
}
}
impl ConstructVisitor for ProfilerInstrumentation {
fn from(_ctx: &ir::Context) -> CalyxResult<Self>
where
Self: Sized + Named,
{
Ok(ProfilerInstrumentation {})
}
fn clear_data(&mut self) {}
}
impl Visitor for ProfilerInstrumentation {
fn start(
&mut self,
comp: &mut ir::Component,
sigs: &ir::LibrarySignatures,
_comps: &[ir::Component],
) -> VisResult {
let delimiter = "___";
let mut acc = 0;
let comp_name = comp.name;
let mut structural_enable_map: HashMap<
Id,
Vec<(Id, ir::Guard<Nothing>)>,
> = HashMap::new();
// groups to cells (from non-primitive components) that they invoked
let mut cell_invoke_map: HashMap<Id, Vec<Id>> = HashMap::new();
// groups to primitives that they invoked
let mut primitive_invoke_map: HashMap<
Id,
Vec<(Id, ir::Guard<Nothing>)>,
> = HashMap::new();
// child_group --> [(parent_group, Guard)]
let group_names = comp
.groups
.iter()
.map(|group| group.borrow().name())
.collect::<Vec<_>>();
let comb_group_names = comp
.comb_groups
.iter()
.map(|group| group.borrow().name())
.collect::<Vec<_>>();
let static_group_names = comp
.static_groups
.iter()
.map(|group| group.borrow().name())
.collect::<Vec<_>>();
// Dynamic groups: iterate and check for structural enables and for cell invokes
for group_ref in comp.groups.iter() {
let group = &group_ref.borrow();
let mut primitive_vec: Vec<(Id, ir::Guard<Nothing>)> = Vec::new();
for assigment_ref in group.assignments.iter() {
let dst_borrow = assigment_ref.dst.borrow();
if let ir::PortParent::Group(parent_group_ref) =
&dst_borrow.parent
{
if dst_borrow.name == "go" {
// found an invocation of go
let invoked_group_name =
parent_group_ref.upgrade().borrow().name();
let guard = *(assigment_ref.guard.clone());
match structural_enable_map.get_mut(&invoked_group_name)
{
Some(vec_ref) => {
vec_ref.push((group.name(), guard))
}
None => {
structural_enable_map.insert(
invoked_group_name,
vec![(group.name(), guard)],
);
}
}
acc += 1; // really sad hack
}
}
if let ir::PortParent::Cell(cell_ref) = &dst_borrow.parent {
match cell_ref.upgrade().borrow().prototype.clone() {
calyx_ir::CellType::Primitive {
name: _,
param_binding: _,
is_comb,
latency: _,
} => {
let cell_name = cell_ref.upgrade().borrow().name();
// don't need to profile for combinational primitives, and if the port isn't a go port.
if !is_comb & dst_borrow.has_attribute(NumAttr::Go)
{
let guard = Guard::and(
*(assigment_ref.guard.clone()),
Guard::port(ir::rrc(
assigment_ref.src.borrow().clone(),
)),
);
primitive_vec.push((cell_name, guard));
}
}
calyx_ir::CellType::Component { name: _ } => {
if dst_borrow.name == "go" {
let cell_name =
cell_ref.upgrade().borrow().name();
match cell_invoke_map.get_mut(&group.name()) {
Some(vec_ref) => {
vec_ref.push(cell_name);
}
None => {
cell_invoke_map.insert(
group.name(),
vec![cell_name],
);
}
}
}
}
_ => (),
}
}
}
primitive_invoke_map
.insert(group_ref.borrow().name(), primitive_vec);
}
// build probe and assignments for every group (dynamic and static) + all structural invokes
let mut builder = ir::Builder::new(comp, sigs);
let one = builder.add_constant(1, 1);
let mut group_name_assign_and_cell = Vec::with_capacity(acc);
let mut comb_group_name_assign_and_cell =
Vec::with_capacity(comb_group_names.len());
let mut static_group_name_assign_and_cell =
Vec::with_capacity(static_group_names.len()); // TODO: adjust when we figure out how to get primitives w/in static groups
{
// Comb: probe and assignments for group (this group is currently active)
for comb_group_name in comb_group_names.into_iter() {
// store group and component name (differentiate between groups of the same name under different components)
let name = format!(
"{}{}{}_group_probe",
comb_group_name, delimiter, comp_name
);
let probe_cell = builder.add_primitive(name, "std_wire", &[1]);
let probe_asgn: ir::Assignment<Nothing> = builder
.build_assignment(
probe_cell.borrow().get("in"),
one.borrow().get("out"),
Guard::True,
);
// the probes should be @control because they should have value 0 whenever the corresponding group is not active.
probe_cell.borrow_mut().add_attribute(BoolAttr::Control, 1);
probe_cell
.borrow_mut()
.add_attribute(BoolAttr::Protected, 1);
comb_group_name_assign_and_cell.push((
comb_group_name,
probe_asgn,
probe_cell,
));
}
// Static: probe and assignments for group (this group is currently active)
for static_group_name in static_group_names.into_iter() {
// store group and component name (differentiate between groups of the same name under different components)
let name = format!(
"{}{}{}_group_probe",
static_group_name, delimiter, comp_name
);
let probe_cell = builder.add_primitive(name, "std_wire", &[1]);
let probe_asgn: ir::Assignment<StaticTiming> = builder
.build_assignment(
probe_cell.borrow().get("in"),
one.borrow().get("out"),
Guard::True,
);
// the probes should be @control because they should have value 0 whenever the corresponding group is not active.
probe_cell.borrow_mut().add_attribute(BoolAttr::Control, 1);
probe_cell
.borrow_mut()
.add_attribute(BoolAttr::Protected, 1);
static_group_name_assign_and_cell.push((
static_group_name,
probe_asgn,
probe_cell,
));
}
// Dynamic: probe and assignments for group (this group is currently active)
for group_name in group_names.into_iter() {
// store group and component name (differentiate between groups of the same name under different components)
let name = format!(
"{}{}{}_group_probe",
group_name, delimiter, comp_name
);
let probe_cell = builder.add_primitive(name, "std_wire", &[1]);
let probe_asgn: ir::Assignment<Nothing> = builder
.build_assignment(
probe_cell.borrow().get("in"),
one.borrow().get("out"),
Guard::True,
);
// the probes should be @control because they should have value 0 whenever the corresponding group is not active.
probe_cell.borrow_mut().add_attribute(BoolAttr::Control, 1);
probe_cell
.borrow_mut()
.add_attribute(BoolAttr::Protected, 1);
group_name_assign_and_cell
.push((group_name, probe_asgn, probe_cell));
}
// probe and assignments for primitive invocations (this group is activating a primitive)
for (group, primitive_invs) in primitive_invoke_map.iter() {
for (primitive_cell_name, guard) in primitive_invs.iter() {
let probe_cell_name = format!(
"{}{}{}{}{}_primitive_probe",
primitive_cell_name,
delimiter,
group,
delimiter,
comp_name
);
let probe_cell = builder.add_primitive(
probe_cell_name,
"std_wire",
&[1],
);
probe_cell.borrow_mut().add_attribute(BoolAttr::Control, 1);
probe_cell
.borrow_mut()
.add_attribute(BoolAttr::Protected, 1);
let probe_asgn: ir::Assignment<Nothing> = builder
.build_assignment(
probe_cell.borrow().get("in"),
one.borrow().get("out"),
guard.clone(),
);
group_name_assign_and_cell
.push((*group, probe_asgn, probe_cell));
}
}
// probe and assignments for structural enables (this group is structurally enabling a child group)
for (invoked_group_name, parent_groups) in
structural_enable_map.iter()
{
for (parent_group, guard) in parent_groups.iter() {
let probe_cell_name = format!(
"{}{}{}{}{}_se_probe",
invoked_group_name,
delimiter,
parent_group,
delimiter,
comp_name
);
let probe_cell = builder.add_primitive(
probe_cell_name,
"std_wire",
&[1],
);
probe_cell.borrow_mut().add_attribute(BoolAttr::Control, 1);
probe_cell
.borrow_mut()
.add_attribute(BoolAttr::Protected, 1);
let probe_asgn: ir::Assignment<Nothing> = builder
.build_assignment(
probe_cell.borrow().get("in"),
one.borrow().get("out"),
guard.clone(),
);
group_name_assign_and_cell.push((
*parent_group,
probe_asgn,
probe_cell,
));
}
}
// probe cell and assignments for structural cell invocations (the group is structurally invoking a cell.)
for (invoker_group, invoked_cells) in cell_invoke_map.iter() {
for invoked_cell in invoked_cells {
let probe_cell_name = format!(
"{}{}{}{}{}_cell_probe",
invoked_cell,
delimiter,
invoker_group,
delimiter,
comp_name
);
let probe_cell = builder.add_primitive(
probe_cell_name,
"std_wire",
&[1],
);
probe_cell.borrow_mut().add_attribute(BoolAttr::Control, 1);
probe_cell
.borrow_mut()
.add_attribute(BoolAttr::Protected, 1);
// NOTE: this probe is active for the duration of the whole group. Hence, it may be active even when the cell itself is inactive.
let probe_asgn: ir::Assignment<Nothing> = builder
.build_assignment(
probe_cell.borrow().get("in"),
one.borrow().get("out"),
Guard::True,
);
group_name_assign_and_cell.push((
*invoker_group,
probe_asgn,
probe_cell,
));
}
}
}
// Dynamic: Add created assignments to each group
for group in comp.groups.iter() {
for (group_name, asgn, cell) in group_name_assign_and_cell.iter() {
if group.borrow().name() == group_name {
group.borrow_mut().assignments.push(asgn.clone());
comp.cells.add(cell.to_owned());
}
}
}
// Comb: Add created assignments to each group
for comb_group in comp.comb_groups.iter() {
for (comb_group_name, asgn, cell) in
comb_group_name_assign_and_cell.iter()
{
if comb_group.borrow().name() == comb_group_name {
comb_group.borrow_mut().assignments.push(asgn.clone());
comp.cells.add(cell.to_owned());
}
}
}
// Static: Add created assignments to each group
for static_group in comp.static_groups.iter() {
for (static_group_name, asgn, cell) in
static_group_name_assign_and_cell.iter()
{
if static_group.borrow().name() == static_group_name {
static_group.borrow_mut().assignments.push(asgn.clone());
comp.cells.add(cell.to_owned());
}
}
}
Ok(Action::Continue)
}
}