|
1 | | -use std::collections::HashMap; |
| 1 | +use itertools::Itertools; |
2 | 2 |
|
3 | 3 | use super::DenseIndexInfo; |
4 | 4 | use crate::{self as ir, Ctx}; |
5 | 5 |
|
6 | 6 | /// Dataflow graph of a component |
7 | 7 | #[derive(Clone)] |
8 | 8 | pub struct Dataflow { |
9 | | - preds: HashMap<ir::Port, Vec<ir::Port>>, |
10 | | - succs: HashMap<ir::Port, Vec<ir::Port>>, |
| 9 | + pub preds: DenseIndexInfo<ir::Port, Vec<ir::PortIdx>>, |
| 10 | + pub succs: DenseIndexInfo<ir::Port, Vec<ir::PortIdx>>, |
11 | 11 | } |
12 | 12 |
|
13 | 13 | impl From<&ir::Component> for Dataflow { |
14 | 14 | fn from(comp: &ir::Component) -> Self { |
15 | | - let mut preds = DenseIndexInfo::with_default(comp.ports().len()); |
16 | | - let mut succs = DenseIndexInfo::with_default(comp.ports().len()); |
| 15 | + let mut preds: DenseIndexInfo<ir::Port, Vec<_>> = |
| 16 | + DenseIndexInfo::with_default(comp.ports().len()); |
| 17 | + let mut succs: DenseIndexInfo<ir::Port, Vec<_>> = |
| 18 | + DenseIndexInfo::with_default(comp.ports().len()); |
17 | 19 |
|
18 | 20 | for cmd in &comp.cmds { |
19 | 21 | match cmd { |
20 | 22 | crate::Command::Invoke(idx) => { |
21 | | - let inv = comp.get(*idx); |
22 | | - for port in &inv.ports { |
23 | | - for &pred in &port.pred { |
24 | | - preds.insert(*port, pred); |
| 23 | + let inputs = idx.inputs(comp); |
| 24 | + let outputs = idx.outputs(comp).collect_vec(); |
| 25 | + |
| 26 | + for pred in inputs { |
| 27 | + for succ in &outputs { |
| 28 | + preds.get_mut(*succ).push(pred); |
| 29 | + succs.get_mut(pred).push(*succ); |
25 | 30 | } |
26 | 31 | } |
27 | 32 | } |
28 | | - crate::Command::Instance(idx) => todo!(), |
29 | | - crate::Command::BundleDef(idx) => todo!(), |
30 | | - crate::Command::Connect(connect) => todo!(), |
31 | | - crate::Command::Let(_) => todo!(), |
32 | | - crate::Command::ForLoop(_) => todo!(), |
33 | | - crate::Command::If(_) => todo!(), |
34 | | - crate::Command::Fact(fact) => todo!(), |
35 | | - crate::Command::Exists(exists) => todo!(), |
| 33 | + crate::Command::Connect(ir::Connect { src, dst, .. }) => { |
| 34 | + assert!(src.is_port(comp) && dst.is_port(comp), "Bundles should be resolved before constructing dataflow"); |
| 35 | + |
| 36 | + preds.get_mut(dst.port).push(src.port); |
| 37 | + succs.get_mut(src.port).push(dst.port); |
| 38 | + } |
| 39 | + crate::Command::BundleDef(_) |
| 40 | + | crate::Command::ForLoop(_) |
| 41 | + | crate::Command::If(_) => { |
| 42 | + unreachable!( |
| 43 | + "Components should be monomorphic and bundle-free" |
| 44 | + ) |
| 45 | + } |
| 46 | + crate::Command::Instance(_) |
| 47 | + | crate::Command::Let(_) |
| 48 | + | crate::Command::Fact(_) |
| 49 | + | crate::Command::Exists(_) => {} |
36 | 50 | } |
37 | 51 | } |
38 | 52 |
|
|
0 commit comments