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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
use core::panic;
use std::collections::{HashMap, HashSet};

use crate::traversal::{Action, ConstructVisitor, Named, VisResult, Visitor};
use calyx_ir::{self as ir, BoolAttr, Guard, Id, Nothing, NumAttr};
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 {}

/// Mapping group names to constructs (groups/primitives/cells) that the group enabled,
/// along with the guard that was involved in the assignment.
type CallsFromGroupMap<T> = HashMap<Id, Vec<(Id, ir::Guard<T>)>>;

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) {}
}

/// Creates probe cells and assignments pertaining to standard groups.
fn group(comp: &mut ir::Component, sigs: &ir::LibrarySignatures) {
    // groups to groups that they enabled
    let mut structural_enable_map: CallsFromGroupMap<Nothing> = HashMap::new();
    // groups to cells (from non-primitive components) that they invoked
    let mut cell_invoke_map: CallsFromGroupMap<Nothing> = HashMap::new();
    // groups to primitives that they invoked
    let mut primitive_invoke_map: CallsFromGroupMap<Nothing> = HashMap::new();
    let group_names = comp
        .groups
        .iter()
        .map(|group| group.borrow().name())
        .collect::<Vec<_>>();

    // Dynamic groups: iterate and check for structural enables, cell invokes, and primitive enables
    for group_ref in comp.groups.iter() {
        let group = &group_ref.borrow();
        // set to prevent adding multiple probes for a combinational primitive enabled by the group
        let mut comb_primitives_covered = HashSet::new();
        let mut primitive_vec: Vec<(Id, ir::Guard<Nothing>)> = Vec::new();
        for assignment_ref in group.assignments.iter() {
            let dst_borrow = assignment_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 = *(assignment_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)],
                            );
                        }
                    }
                }
            }
            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();
                        if is_comb {
                            // collecting primitives for area utilization; we want to avoid adding the same primitive twice!
                            if comb_primitives_covered.insert(cell_name) {
                                primitive_vec.push((cell_name, Guard::True));
                            }
                        } else if dst_borrow.has_attribute(NumAttr::Go) {
                            // non-combinational primitives
                            let guard = Guard::and(
                                *(assignment_ref.guard.clone()),
                                Guard::port(ir::rrc(
                                    assignment_ref.src.borrow().clone(),
                                )),
                            );
                            primitive_vec.push((cell_name, guard));
                        }
                    }
                    calyx_ir::CellType::Component { name: _ } => {
                        if dst_borrow.has_attribute(NumAttr::Go) {
                            let cell_name = cell_ref.upgrade().borrow().name();
                            let guard = *(assignment_ref.guard.clone());
                            match cell_invoke_map.get_mut(&group.name()) {
                                Some(vec_ref) => {
                                    vec_ref.push((cell_name, guard));
                                }
                                None => {
                                    cell_invoke_map.insert(
                                        group.name(),
                                        vec![(cell_name, guard)],
                                    );
                                }
                            }
                        }
                    }
                    _ => (),
                }
            }
        }
        primitive_invoke_map.insert(group_ref.borrow().name(), primitive_vec);
    }

    // create probe cells and assignments
    let group_name_assign_and_cell = create_assignments(
        comp,
        sigs,
        &group_names,
        Some(structural_enable_map),
        Some(cell_invoke_map),
        Some(primitive_invoke_map),
    );

    // Add created assignments to each group and their corresponding probe cells
    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());
            }
        }
    }
}

/// Creates probe cells and assignments pertaining to combinational groups.
fn combinational_group(comp: &mut ir::Component, sigs: &ir::LibrarySignatures) {
    // NOTE: combinational groups cannot structurally enable other groups

    // groups to cells (from non-primitive components) that they invoked
    let mut cell_invoke_map: CallsFromGroupMap<Nothing> = HashMap::new();
    // groups to primitives that they invoked
    let mut primitive_invoke_map: CallsFromGroupMap<Nothing> = HashMap::new();

    let group_names = comp
        .comb_groups
        .iter()
        .map(|group| group.borrow().name())
        .collect::<Vec<_>>();

    for group_ref in comp.comb_groups.iter() {
        let group = &group_ref.borrow();
        let mut comb_primitives_covered = HashSet::new();
        let mut comb_cells_covered = HashSet::new();

        for assignment_ref in group.assignments.iter() {
            let dst_borrow = assignment_ref.dst.borrow();
            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();
                        if is_comb {
                            // collecting primitives for area utilization; we want to avoid adding the same primitive twice!
                            if comb_primitives_covered.insert(cell_name) {
                                match primitive_invoke_map
                                    .get_mut(&group.name())
                                {
                                    Some(vec_ref) => {
                                        vec_ref.push((cell_name, Guard::True));
                                    }
                                    None => {
                                        primitive_invoke_map.insert(
                                            group.name(),
                                            vec![(cell_name, Guard::True)],
                                        );
                                    }
                                }
                            }
                        } else if dst_borrow.has_attribute(NumAttr::Go) {
                            panic!(
                                "Non-combinational primitive {} invoked inside of combinational group {}!",
                                dst_borrow.canonical(),
                                group.name()
                            )
                        }
                    }
                    calyx_ir::CellType::Component { name: _ } => {
                        let cell_name = cell_ref.upgrade().borrow().name();
                        if dst_borrow.name == "go" {
                            panic!(
                                "Non-combinational cell {} invoked inside of combinational group {}!",
                                cell_name,
                                group.name()
                            );
                        } else if comb_cells_covered.insert(cell_name) {
                            let guard = *(assignment_ref.guard.clone());
                            match cell_invoke_map.get_mut(&group.name()) {
                                Some(vec_ref) => {
                                    vec_ref.push((cell_name, guard));
                                }
                                None => {
                                    cell_invoke_map.insert(
                                        group.name(),
                                        vec![(cell_name, guard)],
                                    );
                                }
                            }
                        }
                    }
                    _ => (),
                }
            }
        }
    }

    let group_name_asgn_and_cell = create_assignments(
        comp,
        sigs,
        &group_names,
        None, // assuming no structural enables within comb groups
        Some(cell_invoke_map),
        Some(primitive_invoke_map),
    );

    // Comb: Add created assignments to each group
    for comb_group in comp.comb_groups.iter() {
        for (comb_group_name, asgn, cell) in group_name_asgn_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());
            }
        }
    }
}

/// Creates probe cells and assignments pertaining to static groups.
fn static_group(comp: &mut ir::Component, sigs: &ir::LibrarySignatures) {
    let group_names = comp
        .static_groups
        .iter()
        .map(|group| group.borrow().name())
        .collect::<Vec<_>>();

    // groups to groups that they enabled
    let mut structural_enable_map: CallsFromGroupMap<ir::StaticTiming> =
        HashMap::new();
    // groups to cells (from non-primitive components) that they invoked
    let mut cell_invoke_map: CallsFromGroupMap<ir::StaticTiming> =
        HashMap::new();
    // groups to primitives that they invoked
    let mut primitive_invoke_map: CallsFromGroupMap<ir::StaticTiming> =
        HashMap::new();

    for group_ref in comp.static_groups.iter() {
        let group = &group_ref.borrow();
        // set to prevent adding multiple probes for a combinational primitive enabled by the group
        let mut comb_primitives_covered = HashSet::new();
        let mut primitive_vec: Vec<(Id, ir::Guard<ir::StaticTiming>)> =
            Vec::new();
        for assignment_ref in group.assignments.iter() {
            let dst_borrow = assignment_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 = *(assignment_ref.guard).clone();
                    structural_enable_map
                        .entry(invoked_group_name)
                        .or_default()
                        .push((group.name(), guard));
                }
            }
            if let ir::PortParent::Cell(cell_ref) = &dst_borrow.parent {
                match cell_ref.upgrade().borrow().prototype.clone() {
                    calyx_ir::CellType::Primitive { is_comb, .. } => {
                        let cell_name = cell_ref.upgrade().borrow().name();
                        if is_comb {
                            // collecting primitives for area utilization; we want to avoid adding the same primitive twice!
                            if comb_primitives_covered.insert(cell_name) {
                                primitive_vec.push((cell_name, Guard::True));
                            }
                        } else if dst_borrow.has_attribute(NumAttr::Go) {
                            // non-combinational primitives
                            let guard = Guard::and(
                                *(assignment_ref.guard).clone(),
                                Guard::port(ir::rrc(
                                    assignment_ref.src.borrow().clone(),
                                )),
                            );
                            primitive_vec.push((cell_name, guard));
                        }
                    }
                    calyx_ir::CellType::Component { name: _ } => {
                        if dst_borrow.has_attribute(NumAttr::Go) {
                            let cell_name = cell_ref.upgrade().borrow().name();
                            let guard = *(assignment_ref.guard.clone());
                            cell_invoke_map
                                .entry(group.name())
                                .or_default()
                                .push((cell_name, guard));
                        }
                    }
                    _ => (),
                }
            }
        }
        primitive_invoke_map.insert(group_ref.borrow().name(), primitive_vec);
    }

    let group_name_assign_and_cell = create_assignments(
        comp,
        sigs,
        &group_names,
        Some(structural_enable_map),
        Some(cell_invoke_map),
        Some(primitive_invoke_map),
    );

    // Add created assignments to each group
    for static_group in comp.static_groups.iter() {
        for (static_group_name, asgn, cell) in 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());
            }
        }
    }
}

/// Creates all probe cells and assignments for a certain kind of .
/// Returns a Vec where each element is (GROUP, ASGN, CELL) where
/// GROUP is the group to write the assignment in,
/// ASGN is the probe assignment to insert into the group,
/// CELL is the generated probe wire to add to cells
fn create_assignments<T: Clone>(
    comp: &mut ir::Component,
    sigs: &ir::LibrarySignatures,
    group_names: &[Id],
    structural_enable_map_opt: Option<CallsFromGroupMap<T>>,
    cell_invoke_map_opt: Option<CallsFromGroupMap<T>>,
    primitive_invoke_map_opt: Option<CallsFromGroupMap<T>>,
) -> Vec<(
    Id,
    calyx_ir::Assignment<T>,
    std::rc::Rc<std::cell::RefCell<calyx_ir::Cell>>,
)> {
    let delimiter = "___";
    let comp_name = comp.name;
    // 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);

    // (group name, assignment to insert, probe cell to insert) for each probe we want to insert
    // we assume that each probe cell will only have one assignment.
    let mut group_name_assign_and_cell = Vec::new();

    // probe and assignments for group enable (this group is currently active)
    for group_name in group_names.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<T> = 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));
    }

    if let Some(sem) = structural_enable_map_opt {
        // probe and assignments for structural enables (this group is structurally enabling a child group)
        for (invoked_group_name, parent_groups) in sem.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<T> = 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,
                ));
            }
        }
    }

    if let Some(cell_invoke_map) = cell_invoke_map_opt {
        // 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, guard) 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<T> = builder.build_assignment(
                    probe_cell.borrow().get("in"),
                    one.borrow().get("out"),
                    guard.clone(),
                );
                group_name_assign_and_cell.push((
                    *invoker_group,
                    probe_asgn,
                    probe_cell,
                ));
            }
        }
    }

    if let Some(primitive_invoke_map) = primitive_invoke_map_opt {
        // 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<T> = 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));
            }
        }
    }

    group_name_assign_and_cell
}

/// Creates probes for continuous assignments outside of groups. For every cell
/// or primitive involved in a continuous assignment, this function will generate
/// "contprimitive" and "contcell" wires as probes.
fn continuous_assignments(
    comp: &mut ir::Component,
    sigs: &ir::LibrarySignatures,
) {
    // vector of cells (non-primitives) invoked
    let mut cell_invoke_vec: Vec<(Id, ir::Guard<Nothing>)> = Vec::new();
    // vector of primitives invoked
    let mut primitive_invoke_vec: Vec<(Id, ir::Guard<Nothing>)> = Vec::new();

    // set to prevent adding multiple probes for a combinational primitive
    let mut comb_primitives_covered = HashSet::new();
    let mut comb_cells_covered = HashSet::new();
    for assignment_ref in comp.continuous_assignments.iter() {
        let dst_borrow = assignment_ref.dst.borrow();
        let guard = *(assignment_ref.guard).clone();
        if let ir::PortParent::Cell(cell_ref) = &dst_borrow.parent {
            match cell_ref.upgrade().borrow().prototype.clone() {
                calyx_ir::CellType::Primitive { .. } => {
                    let cell_name = cell_ref.upgrade().borrow().name();
                    // collecting primitives for area utilization; we want to avoid adding the same primitive twice!
                    if comb_primitives_covered.insert(cell_name) {
                        primitive_invoke_vec.push((cell_name, guard));
                    }
                }
                calyx_ir::CellType::Component { .. } => {
                    let cell_name = cell_ref.upgrade().borrow().name();
                    if comb_cells_covered.insert(cell_name) {
                        cell_invoke_vec.push((cell_name, guard));
                    }
                }
                _ => (),
            }
        }
    }

    // add probes for primitives in continuous assignment
    let delimiter = "___";
    let comp_name = comp.name;
    let mut builder = ir::Builder::new(comp, sigs);
    let one = builder.add_constant(1, 1);
    let mut assign_and_cell = Vec::new();
    for (primitive_cell_name, guard) in primitive_invoke_vec.iter() {
        let probe_cell_name = format!(
            "{primitive_cell_name}{delimiter}{comp_name}_contprimitive_probe"
        );
        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(),
        );
        assign_and_cell.push((probe_asgn, probe_cell));
    }
    // add probes for cells (non-primitives) in continuous assignment
    for (cell_name, guard) in cell_invoke_vec.iter() {
        let probe_cell_name =
            format!("{cell_name}{delimiter}{comp_name}_contcell_probe");
        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(),
        );
        assign_and_cell.push((probe_asgn, probe_cell));
    }

    // Add created assignments to continuous assignments
    for (asgn, cell) in assign_and_cell.iter() {
        comp.continuous_assignments.push(asgn.clone());
        comp.cells.add(cell.to_owned());
    }
}

impl Visitor for ProfilerInstrumentation {
    fn start(
        &mut self,
        comp: &mut ir::Component,
        sigs: &ir::LibrarySignatures,
        _comps: &[ir::Component],
    ) -> VisResult {
        group(comp, sigs);
        combinational_group(comp, sigs);
        static_group(comp, sigs);
        continuous_assignments(comp, sigs);
        Ok(Action::Continue)
    }
}