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
use crate::analysis;
use crate::errors::{CalyxResult, Error};
use crate::ir::traversal::{
Action, ConstructVisitor, Named, VisResult, Visitor,
};
use crate::ir::{self, CloneName, LibrarySignatures};
use itertools::Itertools;
use std::collections::{HashMap, HashSet};
type ReadTogether = (ir::Id, HashSet<ir::Id>);
pub struct Papercut {
write_together: HashMap<ir::Id, Vec<HashSet<ir::Id>>>,
read_together: HashMap<ir::Id, Vec<ReadTogether>>,
}
fn write_together_specs<'a>(
primitives: impl Iterator<Item = &'a ir::Primitive>,
) -> HashMap<ir::Id, Vec<HashSet<ir::Id>>> {
let mut write_together = HashMap::new();
for prim in primitives {
let writes: Vec<HashSet<ir::Id>> = prim
.find_all_with_attr("write_together")
.into_iter()
.map(|pd| {
(
pd.attributes.get("write_together").unwrap(),
pd.name.clone(),
)
})
.into_group_map()
.into_values()
.map(|writes| writes.into_iter().collect::<HashSet<_>>())
.collect();
if !writes.is_empty() {
write_together.insert(prim.name.clone(), writes);
}
}
write_together
}
fn read_together_specs<'a>(
primitives: impl Iterator<Item = &'a ir::Primitive>,
) -> CalyxResult<HashMap<ir::Id, Vec<ReadTogether>>> {
let mut read_together = HashMap::new();
for prim in primitives {
let reads: Vec<ReadTogether> = prim
.find_all_with_attr("read_together")
.into_iter()
.map(|pd| (pd.attributes.get("read_together").unwrap(), pd))
.into_group_map()
.into_values()
.map(|ports| {
let (outputs, inputs): (Vec<_>, Vec<_>) =
ports.into_iter().partition(|&port| {
matches!(port.direction, ir::Direction::Output)
});
if outputs.len() != 1 {
return Err(Error::Papercut(format!("Invalid @read_together specification for primitive `{}`. Each specification groups is only allowed to have one output port specified.", prim.name), prim.name.clone()))
}
assert!(outputs.len() == 1);
Ok((
outputs[0].name.clone(),
inputs
.into_iter()
.map(|port| port.name.clone())
.collect::<HashSet<_>>(),
))
})
.collect::<CalyxResult<_>>()?;
if !reads.is_empty() {
read_together.insert(prim.name.clone(), reads);
}
}
Ok(read_together)
}
impl ConstructVisitor for Papercut {
fn from(ctx: &ir::Context) -> CalyxResult<Self> {
let write_together = write_together_specs(ctx.lib.sigs.values());
let read_together = read_together_specs(ctx.lib.sigs.values())?;
Ok(Papercut {
write_together,
read_together,
})
}
fn clear_data(&mut self) {
}
}
impl Named for Papercut {
fn name() -> &'static str {
"papercut"
}
fn description() -> &'static str {
"Detect various common made mistakes"
}
}
fn port_information(
port_ref: ir::RRC<ir::Port>,
) -> Option<((ir::Id, ir::Id), ir::Id)> {
let port = port_ref.borrow();
if let ir::PortParent::Cell(cell_wref) = &port.parent {
let cell_ref = cell_wref.upgrade();
let cell = cell_ref.borrow();
if let ir::CellType::Primitive { name, .. } = &cell.prototype {
return Some((
(cell.name().clone(), name.clone()),
port.name.clone(),
));
}
}
None
}
impl Visitor for Papercut {
fn start(
&mut self,
comp: &mut ir::Component,
_ctx: &LibrarySignatures,
) -> VisResult {
if let ir::Control::Empty(..) = *comp.control.borrow() {
let done_use =
comp.continuous_assignments.iter().find(|assign_ref| {
let assign = assign_ref.dst.borrow();
assign.name == "done" && !assign.is_hole()
});
if done_use.is_none() {
return Err(Error::Papercut(format!("Component `{}` has an empty control program and does not assign to the `done` port. Without an assignment to the `done`, the component cannot return control flow.", comp.name.clone()), comp.name.clone()));
}
}
for group_ref in comp.groups.iter() {
let group = group_ref.borrow();
let all_writes =
analysis::ReadWriteSet::port_write_set(&group.assignments)
.filter_map(port_information)
.into_grouping_map()
.collect::<HashSet<_>>();
let all_reads =
analysis::ReadWriteSet::port_read_set(&group.assignments)
.filter_map(port_information)
.into_grouping_map()
.collect::<HashSet<_>>();
for ((inst, comp_type), reads) in all_reads {
if let Some(spec) = self.read_together.get(&comp_type) {
let empty = HashSet::new();
let writes = all_writes
.get(&(inst.clone(), comp_type.clone()))
.unwrap_or(&empty);
for (read, required) in spec {
if reads.contains(read)
&& matches!(
required.difference(writes).next(),
Some(_)
)
{
let missing = required
.difference(writes)
.sorted()
.map(|port| {
format!("{}.{}", inst.clone(), port)
})
.join(", ");
let msg =
format!("Required signal not driven inside the group.\
\nWhen read the port `{}.{}', the ports [{}] must be written to.\
\nThe primitive type `{}' requires this invariant.",
inst,
read,
missing,
comp_type);
return Err(Error::Papercut(
msg,
group.clone_name(),
));
}
}
}
}
for ((inst, comp_type), writes) in all_writes {
if let Some(spec) = self.write_together.get(&comp_type) {
for required in spec {
let mut diff = required - &writes;
if !diff.is_empty() && diff != *required {
let first = writes.iter().sorted().next().unwrap();
let missing = diff
.drain()
.sorted()
.map(|port| format!("{}.{}", inst, port))
.join(", ");
let msg =
format!("Required signal not driven inside the group.\
\nWhen writing to the port `{}.{}', the ports [{}] must also be written to.\
\nThe primitive type `{}' requires this invariant.",
inst,
first,
missing,
comp_type);
return Err(Error::Papercut(
msg,
group.clone_name(),
));
}
}
}
}
}
Ok(Action::Stop)
}
}