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
use crate::{
analysis::GraphAnalysis,
errors::Error,
ir::traversal::{Action, Named, VisResult, Visitor},
ir::{self, LibrarySignatures},
passes::TopDownCompileControl,
structure,
};
use ir::RRC;
use std::{collections::HashMap, rc::Rc};
#[derive(Default)]
pub struct Inliner;
impl Named for Inliner {
fn name() -> &'static str {
"hole-inliner"
}
fn description() -> &'static str {
"inlines holes"
}
}
type Store = HashMap<(ir::Id, ir::Id), (RRC<ir::Port>, ir::Guard)>;
fn fixed_point(graph: &GraphAnalysis, map: &mut Store) {
let mut worklist = Vec::new();
let has_holes = |guard: &ir::Guard| {
guard
.all_ports()
.iter()
.map(|p| p.borrow().is_hole())
.any(|e| e)
};
for (key, (_, guard)) in map.iter() {
if !has_holes(guard) {
worklist.push(key.clone())
}
}
while !worklist.is_empty() {
let hole_key = worklist.pop().unwrap_or_else(|| unreachable!());
let (hole, new_guard) = map[&hole_key].clone();
for read in graph
.reads_from(&hole.borrow())
.filter(|p| p.borrow().is_hole())
{
let key = read.borrow().canonical();
map.entry(read.borrow().canonical())
.and_modify(|(_, guard)| {
guard.for_each(&|port| {
if port.borrow().canonical() == hole_key {
Some(new_guard.clone())
} else {
None
}
})
});
if !has_holes(&map[&key].1) {
worklist.push(key)
}
}
}
}
impl Visitor for Inliner {
fn start(
&mut self,
comp: &mut ir::Component,
sigs: &LibrarySignatures,
) -> VisResult {
let top_level = match &*comp.control.borrow() {
ir::Control::Empty(_) => return Ok(Action::Stop),
ir::Control::Enable(en) => Rc::clone(&en.group),
_ => return Err(Error::MalformedControl(format!(
"The hole inliner requires control to be a single enable. Try running `{}` before inlining.",
TopDownCompileControl::name()))
)
};
let this_comp = Rc::clone(&comp.signature);
let mut builder = ir::Builder::new(comp, sigs);
let mut asgns = vec![
builder.build_assignment(
top_level.borrow().get("go"),
this_comp.borrow().get_with_attr("go"),
ir::Guard::True,
),
builder.build_assignment(
this_comp.borrow().get_with_attr("done"),
top_level.borrow().get("done"),
ir::Guard::True,
),
];
builder.component.continuous_assignments.append(&mut asgns);
let analysis = GraphAnalysis::from(&*builder.component);
let subgraph = analysis
.edge_induced_subgraph(|src, dst| src.is_hole() || dst.is_hole());
if subgraph.has_cycles() {
return Err(Error::MalformedStructure(
"Cyclic hole definition.".to_string(),
));
}
let mut map: Store = HashMap::new();
let mut assignments = vec![];
for group in builder.component.groups.iter() {
let mut group = group.borrow_mut();
assignments.append(&mut group.assignments.drain(..).collect());
}
assignments.append(
&mut builder.component.continuous_assignments.drain(..).collect(),
);
for asgn in &mut assignments {
let dst = asgn.dst.borrow();
if dst.is_hole() {
map.entry(dst.canonical())
.and_modify(|(_, val)| {
*val = val.clone().or(asgn
.guard
.clone()
.and(ir::Guard::port(Rc::clone(&asgn.src))));
})
.or_insert((
Rc::clone(&asgn.dst),
asgn.guard
.clone()
.and(ir::Guard::port(Rc::clone(&asgn.src))),
));
}
}
fixed_point(&subgraph, &mut map);
assignments.retain(|asgn| !asgn.dst.borrow().is_hole());
structure!(
builder;
let signal_on = constant(1, 1);
);
assignments.iter_mut().for_each(|mut asgn| {
if asgn.src.borrow().is_hole() {
let and_guard = ir::Guard::port(Rc::clone(&asgn.src));
*asgn.guard &= and_guard;
asgn.src = signal_on.borrow().get("out");
}
});
for asgn in &mut assignments {
asgn.guard.for_each(&|port| {
if port.borrow().is_hole() {
Some(map[&port.borrow().canonical()].1.clone())
} else {
None
}
})
}
comp.continuous_assignments = assignments;
comp.groups.clear();
Ok(Action::Change(ir::Control::empty()))
}
}