-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathvalidate.rs
527 lines (486 loc) · 18.4 KB
/
validate.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
//! Definitions for validating hugr nodes according to their operation type.
//!
//! Adds a `validity_flags` method to [`OpType`] that returns a series of flags
//! used by the [`crate::hugr::validate`] module.
//!
//! It also defines a `validate_children` method for more complex tests that
//! require traversing the children.
use itertools::Itertools;
use portgraph::{NodeIndex, PortOffset};
use thiserror::Error;
use crate::types::{Type, TypeRow};
use crate::Direction;
use super::{impl_validate_op, BasicBlock, OpTag, OpTrait, OpType, ValidateOp};
/// A set of property flags required for an operation.
#[non_exhaustive]
pub struct OpValidityFlags {
/// The set of valid children operation types.
pub allowed_children: OpTag,
/// Additional restrictions on the first child operation.
///
/// This is checked in addition to the child allowing the parent optype.
pub allowed_first_child: OpTag,
/// Additional restrictions on the second child operation
///
/// This is checked in addition to the child allowing the parent optype.
pub allowed_second_child: OpTag,
/// Whether the operation must have children.
pub requires_children: bool,
/// Whether the children must form a DAG (no cycles).
pub requires_dag: bool,
/// A strict requirement on the number of non-dataflow multiports.
///
/// If not specified, the operation must have exactly one non-dataflow port
/// if the operation type has other_edges, or zero otherwise.
pub non_df_ports: (Option<usize>, Option<usize>),
/// A validation check for edges between children
///
// Enclosed in an `Option` to avoid iterating over the edges if not needed.
pub edge_check: Option<fn(ChildrenEdgeData) -> Result<(), EdgeValidationError>>,
}
impl OpValidityFlags {
/// Get the number of non-dataflow multiports.
///
/// If None, the operation must have exactly one non-dataflow port
/// if the operation type has other_edges, or zero otherwise.
pub fn non_df_port_count(&self, dir: Direction) -> Option<usize> {
match dir {
Direction::Incoming => self.non_df_ports.0,
Direction::Outgoing => self.non_df_ports.1,
}
}
}
impl Default for OpValidityFlags {
fn default() -> Self {
// Defaults to flags valid for non-container operations
Self {
allowed_children: OpTag::None,
allowed_first_child: OpTag::Any,
allowed_second_child: OpTag::Any,
requires_children: false,
requires_dag: false,
non_df_ports: (None, None),
edge_check: None,
}
}
}
impl ValidateOp for super::Module {
fn validity_flags(&self) -> OpValidityFlags {
OpValidityFlags {
allowed_children: OpTag::ModuleOp,
requires_children: false,
..Default::default()
}
}
}
impl ValidateOp for super::FuncDefn {
fn validity_flags(&self) -> OpValidityFlags {
OpValidityFlags {
allowed_children: OpTag::DataflowChild,
allowed_first_child: OpTag::Input,
allowed_second_child: OpTag::Output,
requires_children: true,
requires_dag: true,
..Default::default()
}
}
fn validate_children<'a>(
&self,
children: impl DoubleEndedIterator<Item = (NodeIndex, &'a OpType)>,
) -> Result<(), ChildrenValidationError> {
validate_io_nodes(
&self.signature.input,
&self.signature.output,
"function definition",
children,
)
}
}
impl ValidateOp for super::DFG {
fn validity_flags(&self) -> OpValidityFlags {
OpValidityFlags {
allowed_children: OpTag::DataflowChild,
allowed_first_child: OpTag::Input,
allowed_second_child: OpTag::Output,
requires_children: true,
requires_dag: true,
..Default::default()
}
}
fn validate_children<'a>(
&self,
children: impl DoubleEndedIterator<Item = (NodeIndex, &'a OpType)>,
) -> Result<(), ChildrenValidationError> {
validate_io_nodes(
&self.signature().input,
&self.signature().output,
"nested graph",
children,
)
}
}
impl ValidateOp for super::Conditional {
fn validity_flags(&self) -> OpValidityFlags {
OpValidityFlags {
allowed_children: OpTag::Case,
requires_children: true,
requires_dag: false,
..Default::default()
}
}
fn validate_children<'a>(
&self,
children: impl DoubleEndedIterator<Item = (NodeIndex, &'a OpType)>,
) -> Result<(), ChildrenValidationError> {
let children = children.collect_vec();
// The first input to the ɣ-node is a predicate of Sum type,
// whose arity matches the number of children of the ɣ-node.
if self.predicate_inputs.len() != children.len() {
return Err(ChildrenValidationError::InvalidConditionalPredicate {
child: children[0].0, // Pass an arbitrary child
expected_count: children.len(),
actual_count: self.predicate_inputs.len(),
actual_predicate_rows: self.predicate_inputs.clone(),
});
}
// Each child must have its predicate variant's row and the rest of `inputs` as input,
// and matching output
for (i, (child, optype)) in children.into_iter().enumerate() {
let OpType::Case(case_op) = optype else {
panic!("Child check should have already checked valid ops.")
};
let sig = &case_op.signature;
if sig.input != self.case_input_row(i).unwrap() || sig.output != self.outputs {
return Err(ChildrenValidationError::ConditionalCaseSignature {
child,
optype: optype.clone(),
});
}
}
Ok(())
}
}
impl ValidateOp for super::TailLoop {
fn validity_flags(&self) -> OpValidityFlags {
OpValidityFlags {
allowed_children: OpTag::DataflowChild,
allowed_first_child: OpTag::Input,
allowed_second_child: OpTag::Output,
requires_children: true,
requires_dag: true,
..Default::default()
}
}
fn validate_children<'a>(
&self,
children: impl DoubleEndedIterator<Item = (NodeIndex, &'a OpType)>,
) -> Result<(), ChildrenValidationError> {
validate_io_nodes(
&self.body_input_row(),
&self.body_output_row(),
"tail-controlled loop graph",
children,
)
}
}
impl ValidateOp for super::CFG {
fn validity_flags(&self) -> OpValidityFlags {
OpValidityFlags {
allowed_children: OpTag::ControlFlowChild,
allowed_first_child: OpTag::BasicBlock,
allowed_second_child: OpTag::BasicBlockExit,
requires_children: true,
requires_dag: false,
edge_check: Some(validate_cfg_edge),
..Default::default()
}
}
fn validate_children<'a>(
&self,
children: impl Iterator<Item = (NodeIndex, &'a OpType)>,
) -> Result<(), ChildrenValidationError> {
for (child, optype) in children.dropping(2) {
if optype.tag() == OpTag::BasicBlockExit {
return Err(ChildrenValidationError::InternalExitChildren { child });
}
}
Ok(())
}
}
/// Errors that can occur while checking the children of a node.
#[derive(Debug, Clone, PartialEq, Error)]
#[allow(missing_docs)]
pub enum ChildrenValidationError {
/// An CFG graph has an exit operation as a non-second child.
#[error("Exit basic blocks are only allowed as the second child in a CFG graph")]
InternalExitChildren { child: NodeIndex },
/// An operation only allowed as the first/second child was found as an intermediate child.
#[error("A {optype:?} operation is only allowed as a {expected_position} child")]
InternalIOChildren {
child: NodeIndex,
optype: OpType,
expected_position: &'static str,
},
/// The signature of the contained dataflow graph does not match the one of the container.
#[error("The {node_desc} node of a {container_desc} has a signature of {actual:?}, which differs from the expected type row {expected:?}")]
IOSignatureMismatch {
child: NodeIndex,
actual: TypeRow,
expected: TypeRow,
node_desc: &'static str,
container_desc: &'static str,
},
/// The signature of a child case in a conditional operation does not match the container's signature.
#[error("A conditional case has optype {optype:?}, which differs from the signature of Conditional container")]
ConditionalCaseSignature { child: NodeIndex, optype: OpType },
/// The conditional container's branch predicate does not match the number of children.
#[error("The conditional container's branch predicate input should be a sum with {expected_count} elements, but it had {actual_count} elements. Predicate rows: {actual_predicate_rows:?} ")]
InvalidConditionalPredicate {
child: NodeIndex,
expected_count: usize,
actual_count: usize,
actual_predicate_rows: Vec<TypeRow>,
},
}
impl ChildrenValidationError {
/// Returns the node index of the child that caused the error.
pub fn child(&self) -> NodeIndex {
match self {
ChildrenValidationError::InternalIOChildren { child, .. } => *child,
ChildrenValidationError::InternalExitChildren { child, .. } => *child,
ChildrenValidationError::ConditionalCaseSignature { child, .. } => *child,
ChildrenValidationError::IOSignatureMismatch { child, .. } => *child,
ChildrenValidationError::InvalidConditionalPredicate { child, .. } => *child,
}
}
}
/// Errors that can occur while checking the edges between children of a node.
#[derive(Debug, Clone, PartialEq, Error)]
#[allow(missing_docs)]
pub enum EdgeValidationError {
/// The dataflow signature of two connected basic blocks does not match.
#[error("The dataflow signature of two connected basic blocks does not match. Output signature: {source_op:?}, input signature: {target_op:?}",
source_op = edge.source_op,
target_op = edge.target_op
)]
CFGEdgeSignatureMismatch { edge: ChildrenEdgeData },
}
impl EdgeValidationError {
/// Returns information on the edge that caused the error.
pub fn edge(&self) -> &ChildrenEdgeData {
match self {
EdgeValidationError::CFGEdgeSignatureMismatch { edge } => edge,
}
}
}
/// Auxiliary structure passed as data to [`OpValidityFlags::edge_check`].
#[derive(Debug, Clone, PartialEq)]
pub struct ChildrenEdgeData {
/// Source child.
pub source: NodeIndex,
/// Target child.
pub target: NodeIndex,
/// Operation type of the source child.
pub source_op: OpType,
/// Operation type of the target child.
pub target_op: OpType,
/// Source port.
pub source_port: PortOffset,
/// Target port.
pub target_port: PortOffset,
}
impl ValidateOp for BasicBlock {
/// Returns the set of allowed parent operation types.
fn validity_flags(&self) -> OpValidityFlags {
match self {
BasicBlock::DFB {
predicate_variants, ..
} => OpValidityFlags {
allowed_children: OpTag::DataflowChild,
allowed_first_child: OpTag::Input,
allowed_second_child: OpTag::Output,
requires_children: true,
requires_dag: true,
non_df_ports: (None, Some(predicate_variants.len())),
..Default::default()
},
// Default flags are valid for non-container operations
BasicBlock::Exit { .. } => Default::default(),
}
}
/// Validate the ordered list of children.
fn validate_children<'a>(
&self,
children: impl DoubleEndedIterator<Item = (NodeIndex, &'a OpType)>,
) -> Result<(), ChildrenValidationError> {
match self {
BasicBlock::DFB {
inputs,
predicate_variants,
other_outputs: outputs,
extension_delta: _,
} => {
let predicate_type = Type::new_predicate(predicate_variants.clone());
let node_outputs: TypeRow = [&[predicate_type], outputs.as_ref()].concat().into();
validate_io_nodes(inputs, &node_outputs, "basic block graph", children)
}
// Exit nodes do not have children
BasicBlock::Exit { .. } => Ok(()),
}
}
}
impl ValidateOp for super::Case {
/// Returns the set of allowed parent operation types.
fn validity_flags(&self) -> OpValidityFlags {
OpValidityFlags {
allowed_children: OpTag::DataflowChild,
allowed_first_child: OpTag::Input,
allowed_second_child: OpTag::Output,
requires_children: true,
requires_dag: true,
..Default::default()
}
}
/// Validate the ordered list of children.
fn validate_children<'a>(
&self,
children: impl DoubleEndedIterator<Item = (NodeIndex, &'a OpType)>,
) -> Result<(), ChildrenValidationError> {
validate_io_nodes(
&self.signature.input,
&self.signature.output,
"Conditional",
children,
)
}
}
/// Checks a that the list of children nodes does not contain Input and Output
/// nodes outside of the first and second elements respectively, and that those
/// have the correct signature.
fn validate_io_nodes<'a>(
expected_input: &TypeRow,
expected_output: &TypeRow,
container_desc: &'static str,
mut children: impl Iterator<Item = (NodeIndex, &'a OpType)>,
) -> Result<(), ChildrenValidationError> {
// Check that the signature matches with the Input and Output rows.
let (first, first_optype) = children.next().unwrap();
let (second, second_optype) = children.next().unwrap();
if &first_optype.signature().output != expected_input {
return Err(ChildrenValidationError::IOSignatureMismatch {
child: first,
actual: first_optype.signature().output,
expected: expected_input.clone(),
node_desc: "Input",
container_desc,
});
}
if &second_optype.signature().input != expected_output {
return Err(ChildrenValidationError::IOSignatureMismatch {
child: second,
actual: second_optype.signature().input,
expected: expected_output.clone(),
node_desc: "Output",
container_desc,
});
}
// The first and second children have already been popped from the iterator.
for (child, optype) in children {
match optype.tag() {
OpTag::Input => {
return Err(ChildrenValidationError::InternalIOChildren {
child,
optype: optype.clone(),
expected_position: "first",
})
}
OpTag::Output => {
return Err(ChildrenValidationError::InternalIOChildren {
child,
optype: optype.clone(),
expected_position: "second",
})
}
_ => {}
}
}
Ok(())
}
/// Validate an edge between two basic blocks in a CFG sibling graph.
fn validate_cfg_edge(edge: ChildrenEdgeData) -> Result<(), EdgeValidationError> {
let [source, target]: [&BasicBlock; 2] = [&edge.source_op, &edge.target_op].map(|op| {
let OpType::BasicBlock(block_op) = op else {
panic!("CFG sibling graphs can only contain basic block operations.")
};
block_op
});
if source.successor_input(edge.source_port.index()).as_ref() != Some(target.dataflow_input()) {
return Err(EdgeValidationError::CFGEdgeSignatureMismatch { edge });
}
Ok(())
}
#[cfg(test)]
mod test {
use crate::extension::prelude::USIZE_T;
use crate::{ops, type_row};
use crate::{ops::dataflow::IOTrait, ops::LeafOp};
use cool_asserts::assert_matches;
use super::*;
#[test]
fn test_validate_io_nodes() {
let in_types: TypeRow = type_row![USIZE_T];
let out_types: TypeRow = type_row![USIZE_T, USIZE_T];
let input_node: OpType = ops::Input::new(in_types.clone()).into();
let output_node = ops::Output::new(out_types.clone()).into();
let leaf_node = LeafOp::Noop { ty: USIZE_T }.into();
// Well-formed dataflow sibling nodes. Check the input and output node signatures.
let children = vec![
(0, &input_node),
(1, &output_node),
(2, &leaf_node),
(3, &leaf_node),
];
assert_eq!(
validate_io_nodes(&in_types, &out_types, "test", make_iter(&children)),
Ok(())
);
assert_matches!(
validate_io_nodes(&out_types, &out_types, "test", make_iter(&children)),
Err(ChildrenValidationError::IOSignatureMismatch { child, .. }) if child.index() == 0
);
assert_matches!(
validate_io_nodes(&in_types, &in_types, "test", make_iter(&children)),
Err(ChildrenValidationError::IOSignatureMismatch { child, .. }) if child.index() == 1
);
// Internal I/O nodes
let children = vec![
(0, &input_node),
(1, &output_node),
(42, &leaf_node),
(2, &leaf_node),
(3, &output_node),
];
assert_matches!(
validate_io_nodes(&in_types, &out_types, "test", make_iter(&children)),
Err(ChildrenValidationError::InternalIOChildren { child, .. }) if child.index() == 3
);
}
fn make_iter<'a>(
children: &'a [(usize, &OpType)],
) -> impl DoubleEndedIterator<Item = (NodeIndex, &'a OpType)> {
children.iter().map(|(n, op)| (NodeIndex::new(*n), *op))
}
}
use super::{
AliasDecl, AliasDefn, Call, CallIndirect, Const, FuncDecl, Input, LeafOp, LoadConstant, Output,
};
impl_validate_op!(FuncDecl);
impl_validate_op!(AliasDecl);
impl_validate_op!(AliasDefn);
impl_validate_op!(Input);
impl_validate_op!(Output);
impl_validate_op!(Const);
impl_validate_op!(Call);
impl_validate_op!(LoadConstant);
impl_validate_op!(CallIndirect);
impl_validate_op!(LeafOp);