calyx_opt/passes/
component_inliner.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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
use crate::analysis;
use crate::traversal::{
    Action, ConstructVisitor, Named, Order, ParseVal, PassOpt, VisResult,
    Visitor,
};
use calyx_ir::{self as ir, GetAttributes, LibrarySignatures, RRC, rewriter};
use calyx_utils::Error;
use ir::Nothing;
use itertools::Itertools;
use linked_hash_map::LinkedHashMap;
use std::collections::{HashMap, HashSet};
use std::rc::Rc;

/// Inlines all sub-components marked with the `@inline` attribute.
/// Cannot inline components when they:
///   1. Are primitives
///   2. Are invoked structurally
///   3. Invoked using `invoke`-`with` statements
///
/// For each component that needs to be inlined, we need to:
///   1. Inline all cells defined by that instance.
///   2. Inline all groups defined by that instance.
///   3. Inline the control program for every `invoke` statement referring to the
///      instance.
pub struct ComponentInliner {
    /// Force inlining of all components. Parsed from the command line.
    always_inline: bool,
    /// Generate new_fsms for the componnent we generate. Helpful if you don't
    /// want the fsms to get too many states
    new_fsms: bool,
    /// Map from the name of an instance to its associated control program.
    control_map: HashMap<ir::Id, ir::Control>,
    /// Mapping for ports on cells that have been inlined.
    interface_rewrites: rewriter::PortRewriteMap,
    /// Cells that have been inlined. We retain these so that references within
    /// the control program of the parent are valid.
    inlined_cells: Vec<RRC<ir::Cell>>,
}

impl Named for ComponentInliner {
    fn name() -> &'static str {
        "inline"
    }

    fn description() -> &'static str {
        "inline all component instances marked with @inline attribute"
    }

    fn opts() -> Vec<PassOpt> {
        vec![
            PassOpt::new(
                "always",
                "Attempt to inline all components into the main component",
                ParseVal::Bool(false),
                PassOpt::parse_bool,
            ),
            PassOpt::new(
                "new-fsms",
                "Instantiate new FSM for each inlined component",
                ParseVal::Bool(false),
                PassOpt::parse_bool,
            ),
        ]
    }
}

impl ComponentInliner {
    /// Equivalent to a default method but not automatically derived because
    /// it conflicts with the autogeneration of `ConstructVisitor`.
    fn new(always_inline: bool, new_fsms: bool) -> Self {
        ComponentInliner {
            always_inline,
            new_fsms,
            control_map: HashMap::default(),
            interface_rewrites: LinkedHashMap::default(),
            inlined_cells: Vec::default(),
        }
    }
}

impl ConstructVisitor for ComponentInliner {
    fn from(ctx: &ir::Context) -> calyx_utils::CalyxResult<Self>
    where
        Self: Sized,
    {
        let opts = Self::get_opts(ctx);
        Ok(ComponentInliner::new(
            opts[&"always"].bool(),
            opts[&"new-fsms"].bool(),
        ))
    }

    fn clear_data(&mut self) {
        *self = ComponentInliner::new(self.always_inline, self.new_fsms);
    }
}

impl ComponentInliner {
    /// Inline a cell definition into the component associated with the `builder`.
    fn inline_cell(
        builder: &mut ir::Builder,
        cell_ref: &RRC<ir::Cell>,
    ) -> (ir::Id, RRC<ir::Cell>) {
        let cell = cell_ref.borrow();
        let cn = cell.name();
        let new_cell = match &cell.prototype {
            ir::CellType::Primitive {
                name,
                param_binding,
                ..
            } => builder.add_primitive(
                cn,
                *name,
                &param_binding.iter().map(|(_, v)| *v).collect_vec(),
            ),
            ir::CellType::Component { name } => {
                builder.add_component(cn, *name, cell.get_signature())
            }
            ir::CellType::Constant { val, width } => {
                builder.add_constant(*val, *width)
            }
            ir::CellType::ThisComponent => unreachable!(),
        };
        (cn, new_cell)
    }

    /// Rewrite assignments using a [CellMap], [PortMap], and an optional new group.
    /// Attempts the following rewrites in order:
    /// 1. Using the [CellMap] to get the same port on a new [Cell].
    /// 2. Using the [PortMap] to a new [Port].
    /// 3. Using `new_group` to rewrite use of a group hole if the port is a hole.
    fn rewrite_assigns(
        assigns: &mut [ir::Assignment<Nothing>],
        port_rewrite: &ir::Rewriter,
        new_group: Option<&RRC<ir::Group>>,
    ) {
        assigns.iter_mut().for_each(|assign| {
            assign.for_each_port(|port| {
                port_rewrite.get(port).or_else(|| {
                    if let Some(grp) = new_group {
                        if port.borrow().is_hole() {
                            return Some(grp.borrow().get(&port.borrow().name));
                        }
                    }
                    None
                })
            });
        })
    }

    /// Rewrite assignments using a [CellMap], [PortMap], and an optional new group.
    /// Attempts the following rewrites in order:
    /// 1. Using the [CellMap] to get the same port on a new [Cell].
    /// 2. Using the [PortMap] to a new [Port].
    /// 3. Using `new_group` to rewrite use of a group hole if the port is a hole.
    fn rewrite_assigns_static(
        assigns: &mut [ir::Assignment<ir::StaticTiming>],
        port_rewrite: &ir::Rewriter,
        new_group: Option<&RRC<ir::StaticGroup>>,
    ) {
        assigns.iter_mut().for_each(|assign| {
            assign.for_each_port(|port| {
                port_rewrite.get(port).or_else(|| {
                    if let Some(grp) = new_group {
                        if port.borrow().is_hole() {
                            return Some(grp.borrow().get(&port.borrow().name));
                        }
                    }
                    None
                })
            });
        })
    }

    /// Rewrites vec based on self.interface_rewrites Used as a helper function
    /// for rewrite_invoke_ports
    fn rewrite_vec(&self, v: &mut [(ir::Id, RRC<ir::Port>)]) {
        v.iter_mut().for_each(|(_, port)| {
            let key = port.borrow().canonical();
            if let Some(rewrite) = self.interface_rewrites.get(&key) {
                *port = Rc::clone(rewrite);
            }
        })
    }

    /// Rewrites the input/output ports of the invoke based on self.interface_rewrites
    fn rewrite_invoke_ports(&self, invoke: &mut ir::Invoke) {
        self.rewrite_vec(&mut invoke.inputs);
        self.rewrite_vec(&mut invoke.outputs);
    }

    /// Inline a group definition from a component into the component associated
    /// with the `builder`.
    fn inline_group(
        builder: &mut ir::Builder,
        port_rewrite: &ir::Rewriter,
        gr: &RRC<ir::Group>,
    ) -> (ir::Id, RRC<ir::Group>) {
        let group = gr.borrow();
        let new_group = builder.add_group(group.name());
        new_group.borrow_mut().attributes = group.attributes.clone();

        // Rewrite assignments
        let mut asgns = group.assignments.clone();
        Self::rewrite_assigns(&mut asgns, port_rewrite, Some(&new_group));
        new_group.borrow_mut().assignments = asgns;
        (group.name(), new_group)
    }

    /// Inline a group definition from a component into the component associated
    /// with the `builder`.
    fn inline_static_group(
        builder: &mut ir::Builder,
        port_rewrite: &ir::Rewriter,
        gr: &RRC<ir::StaticGroup>,
    ) -> (ir::Id, RRC<ir::StaticGroup>) {
        let group = gr.borrow();
        let new_group =
            builder.add_static_group(group.name(), group.get_latency());
        new_group.borrow_mut().attributes = group.attributes.clone();

        // Rewrite assignments
        let mut asgns = group.assignments.clone();
        Self::rewrite_assigns_static(
            &mut asgns,
            port_rewrite,
            Some(&new_group),
        );
        new_group.borrow_mut().assignments = asgns;
        (group.name(), new_group)
    }

    /// Inline a group definition from a component into the component associated
    /// with the `builder`.
    fn inline_comb_group(
        builder: &mut ir::Builder,
        port_rewrite: &ir::Rewriter,
        gr: &RRC<ir::CombGroup>,
    ) -> (ir::Id, RRC<ir::CombGroup>) {
        let group = gr.borrow();
        let new_group = builder.add_comb_group(group.name());
        new_group.borrow_mut().attributes = group.attributes.clone();

        // Rewrite assignments
        let mut asgns = group.assignments.clone();
        Self::rewrite_assigns(&mut asgns, port_rewrite, None);
        new_group.borrow_mut().assignments = asgns;
        (group.name(), new_group)
    }

    /// Adds wires that can hold the values written to various output ports.
    fn inline_interface(
        builder: &mut ir::Builder,
        comp: &ir::Component,
        name: ir::Id,
    ) -> rewriter::PortRewriteMap {
        // For each output port, generate a wire that will store its value
        comp.signature
            .borrow()
            .ports
            .iter()
            .map(|port_ref| {
                let port = port_ref.borrow();
                let wire_name = format!("{}_{}", name, port.name);
                let wire_ref =
                    builder.add_primitive(wire_name, "std_wire", &[port.width]);
                let wire = wire_ref.borrow();
                let pn = match port.direction {
                    ir::Direction::Input => "in",
                    ir::Direction::Output => "out",
                    ir::Direction::Inout => unreachable!(),
                };
                (port.canonical(), wire.get(pn))
            })
            .collect()
    }

    /// Inline component `comp` into the parent component attached to `builder`.
    /// Returns:
    /// 1. The control program associated with the component being inlined,
    ///    rewritten for the specific instance.
    /// 2. A [PortMap] (in form of an iterator) to be used in the parent component to rewrite uses
    ///    of interface ports of the component being inlined.
    fn inline_component(
        builder: &mut ir::Builder,
        mut cell_map: rewriter::RewriteMap<ir::Cell>,
        comp: &ir::Component,
        name: ir::Id,
    ) -> (
        ir::Control,
        impl Iterator<Item = (ir::Canonical, RRC<ir::Port>)>,
    ) {
        // For each cell in the component, create a new cell in the parent
        // of the same type and build a rewrite map using it.
        cell_map.extend(comp.cells.iter().filter_map(|cell_ref| {
            if !cell_ref.borrow().is_reference() {
                Some(Self::inline_cell(builder, cell_ref))
            } else {
                None
            }
        }));

        // Rewrites to inline the interface.
        let interface_map = Self::inline_interface(builder, comp, name);
        let mut rewrite = ir::Rewriter {
            cell_map,
            port_map: interface_map,
            ..Default::default()
        };

        // For each group, create a new group and rewrite all assignments within
        // it using the `rewrite_map`.
        rewrite.group_map = comp
            .get_groups()
            .iter()
            .map(|gr| Self::inline_group(builder, &rewrite, gr))
            .collect();
        rewrite.static_group_map = comp
            .get_static_groups()
            .iter()
            .map(|gr| Self::inline_static_group(builder, &rewrite, gr))
            .collect();
        rewrite.comb_group_map = comp
            .comb_groups
            .iter()
            .map(|gr| Self::inline_comb_group(builder, &rewrite, gr))
            .collect();

        // Rewrite continuous assignments
        let mut cont_assigns = comp.continuous_assignments.clone();
        Self::rewrite_assigns(&mut cont_assigns, &rewrite, None);
        builder
            .component
            .continuous_assignments
            .extend(cont_assigns);

        // Generate a control program associated with this instance
        let mut con = ir::Cloner::control(&comp.control.borrow());
        rewrite.rewrite_control(&mut con);

        // Generate interface map for use in the parent cell.
        // Return as an iterator because it's immediately merged into the global rewrite map.
        let rev_interface_map =
            rewrite.port_map.into_iter().map(move |(cp, pr)| {
                let ir::Canonical { port: p, .. } = cp;
                let port = pr.borrow();
                let np = match port.name.id.as_str() {
                    "in" => "out",
                    "out" => "in",
                    _ => unreachable!(),
                };
                (
                    ir::Canonical::new(name, p),
                    port.cell_parent().borrow().get(np),
                )
            });

        (con, rev_interface_map)
    }
}

impl Visitor for ComponentInliner {
    // Inlining should proceed bottom-up
    fn iteration_order() -> Order {
        Order::Post
    }

    fn start(
        &mut self,
        comp: &mut ir::Component,
        sigs: &LibrarySignatures,
        comps: &[ir::Component],
    ) -> VisResult {
        // Separate out cells that need to be inlined.
        let (inline_cells, cells): (Vec<_>, Vec<_>) =
            comp.cells.drain().partition(|cr| {
                let cell = cr.borrow();
                // If forced inlining is enabled, attempt to inline every
                // component.
                if self.always_inline {
                    cell.is_component()
                } else {
                    cell.get_attribute(ir::BoolAttr::Inline).is_some()
                }
            });
        comp.cells.append(cells.into_iter());

        // Early exit if there is nothing to inline
        if inline_cells.is_empty() {
            return Ok(Action::Stop);
        }

        // Use analysis to get all bindings for invokes and filter out bindings
        // for inlined cells.
        let invoke_bindings: HashMap<ir::Id, _> =
            analysis::ControlPorts::<true>::from(&*comp.control.borrow())
                .get_all_bindings()
                .into_iter()
                .filter(|(instance, _)| {
                    inline_cells.iter().any(|c| c.borrow().name() == instance)
                })
                .collect();

        // If any invoke has more than one binding, error out:
        for (instance, bindings) in &invoke_bindings {
            if bindings.len() > 1 {
                let bindings_str = bindings
                    .iter()
                    .map(|(cells, ports)| {
                        format!(
                            "[{}]({})",
                            cells
                                .iter()
                                .map(|(c, cell)| format!(
                                    "{c}={}",
                                    cell.borrow().name()
                                ))
                                .join(", "),
                            ports
                                .iter()
                                .map(|(p, port)| format!(
                                    "{p}={}",
                                    port.borrow().canonical()
                                ))
                                .join(", ")
                        )
                    })
                    .join("\n");
                return Err(Error::pass_assumption(
                    Self::name(),
                    format!(
                        "Instance `{}.{instance}` invoked with multiple parameters (currently unsupported):\n{bindings_str}",
                        comp.name,
                    ),
                ));
            }
        }

        // Mapping from component name to component definition
        let comp_map = comps
            .iter()
            .map(|comp| (&comp.name, comp))
            .collect::<HashMap<_, _>>();

        // Rewrites for the interface ports of inlined cells.
        let mut interface_rewrites: rewriter::PortRewriteMap =
            LinkedHashMap::new();
        // Track names of cells that were inlined.
        let mut inlined_cells = HashSet::new();
        let mut builder = ir::Builder::new(comp, sigs);
        for cell_ref in &inline_cells {
            let cell = cell_ref.borrow();
            // Error if the cell is not a component
            if !cell.is_component() {
                let msg = format!(
                    "Cannot inline `{}`. It is a instance of primitive: `{}`",
                    cell.name(),
                    cell.type_name()
                        .unwrap_or_else(|| ir::Id::from("constant"))
                );

                return Err(Error::pass_assumption(Self::name(), msg)
                    .with_pos(&cell.attributes));
            }

            let comp_name = cell.type_name().unwrap();
            let cell_map =
                if let Some(binding) = &invoke_bindings.get(&cell.name()) {
                    let (cell_binds, _) = &binding[0];
                    cell_binds.iter().map(|(k, v)| (*k, v.clone())).collect()
                } else {
                    log::info!(
                        "no binding for `{}` which means instance is unused",
                        cell.name()
                    );
                    HashMap::new()
                };
            let (control, rewrites) = Self::inline_component(
                &mut builder,
                cell_map,
                comp_map[&comp_name],
                cell.name(),
            );
            interface_rewrites.extend(rewrites);
            self.control_map.insert(cell.name(), control);
            inlined_cells.insert(cell.name());
        }

        // XXX: This is unneccessarily iterate over the newly inlined groups.
        // Rewrite all assignment in the component to use interface wires
        // from the inlined instances and check if the `go` or `done` ports
        // on any of the instances was used for structural invokes.
        builder.component.for_each_assignment(|assign| {
            assign.for_each_port(|pr| {
                let port = &pr.borrow();
                let np = interface_rewrites.get(&port.canonical());
                if np.is_some() && (port.name == "go" || port.name == "done") {
                    panic!(
                        "Cannot inline instance. It is structurally structurally invoked: `{}`",
                        port.cell_parent().borrow().name(),
                    );
                }
                np.cloned()
            });
        });

        builder.component.for_each_static_assignment(|assign| {
            assign.for_each_port(|pr| {
                let port = &pr.borrow();
                let np = interface_rewrites.get(&port.canonical());
                if np.is_some() && (port.name == "go" || port.name == "done") {
                    panic!(
                        "Cannot inline instance. It is structurally structurally invoked: `{}`",
                        port.cell_parent().borrow().name(),
                    );
                }
                np.cloned()
            });
        });

        // Ensure that all invokes use the same parameters and inline the parameter assignments.
        for (instance, mut bindings) in invoke_bindings {
            let Some((_, binding)) = bindings.pop() else {
                unreachable!("Instance binding is empty");
            };
            let mut assigns = binding
                .into_iter()
                .filter(|(_, pr)| {
                    let port = pr.borrow();
                    // Skip clk and reset ports
                    !port.attributes.has(ir::BoolAttr::Clk)
                        && !port.attributes.has(ir::BoolAttr::Reset)
                })
                .map(|(name, param)| {
                    let port = Rc::clone(
                        &interface_rewrites
                            [&ir::Canonical::new(instance, name)],
                    );
                    // The parameter can refer to port on a cell that has been
                    // inlined.
                    let name = param.borrow().canonical();
                    let new_param = interface_rewrites
                        .get(&name)
                        .map(Rc::clone)
                        .unwrap_or(param);
                    let dir = port.borrow().direction.clone();
                    match dir {
                        ir::Direction::Input => builder.build_assignment(
                            port,
                            new_param,
                            ir::Guard::True,
                        ),
                        ir::Direction::Output => builder.build_assignment(
                            new_param,
                            port,
                            ir::Guard::True,
                        ),
                        ir::Direction::Inout => unreachable!(),
                    }
                })
                .collect_vec();
            builder
                .component
                .continuous_assignments
                .append(&mut assigns);
        }

        self.interface_rewrites = interface_rewrites;
        // Save inlined cells so that references within the parent control
        // program remain valid.
        self.inlined_cells = inline_cells;

        Ok(Action::Continue)
    }

    fn start_if(
        &mut self,
        s: &mut ir::If,
        _comp: &mut ir::Component,
        _sigs: &LibrarySignatures,
        _comps: &[ir::Component],
    ) -> VisResult {
        let name = &s.port.borrow().canonical();
        if let Some(new_port) = self.interface_rewrites.get(name) {
            s.port = Rc::clone(new_port);
        }
        Ok(Action::Continue)
    }

    fn start_while(
        &mut self,
        s: &mut ir::While,
        _comp: &mut ir::Component,
        _sigs: &LibrarySignatures,
        _comps: &[ir::Component],
    ) -> VisResult {
        let name = &s.port.borrow().canonical();
        if let Some(new_port) = self.interface_rewrites.get(name) {
            s.port = Rc::clone(new_port);
        }
        Ok(Action::Continue)
    }

    fn invoke(
        &mut self,
        s: &mut ir::Invoke,
        _comp: &mut ir::Component,
        _sigs: &LibrarySignatures,
        _comps: &[ir::Component],
    ) -> VisResult {
        // Regardless of whether the associated instance has been inlined,
        // we still may need to rewrite the input/output ports
        self.rewrite_invoke_ports(s);

        // If the associated instance has been inlined, replace the invoke with
        // its control program.
        let cell = s.comp.borrow();
        if let Some(con) = self.control_map.get_mut(&cell.name()) {
            if self.new_fsms {
                con.get_mut_attributes().insert(ir::BoolAttr::NewFSM, 1);
            }
            Ok(Action::change(ir::Cloner::control(con)))
        } else {
            Ok(Action::Continue)
        }
    }
}