Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[new] Validate IO nodes have resources matching parent signatures #375

Merged
merged 3 commits into from
Aug 9, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions src/hugr/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,42 @@ impl<'a> ValidationContext<'a> {
Ok(())
}

/// Check that input and output nodes match up with the signature of their parents
/// This must be done after the `gather_resources` step
fn validate_io_resources(
&self,
hugr: &impl HugrView,
parent: Node,
) -> Result<(), ValidationError> {
if let Some([input, output]) = hugr.get_io(parent) {
let parent_input_resources =
self.resources.get(&(parent, Direction::Incoming)).unwrap();
let parent_output_resources =
self.resources.get(&(parent, Direction::Outgoing)).unwrap();
for dir in Direction::BOTH {
let input_resources = self.resources.get(&(input, dir)).unwrap();
let output_resources = self.resources.get(&(output, dir)).unwrap();
if parent_input_resources != input_resources {
return Err(ValidationError::ParentIOResourceMismatch {
parent,
parent_resources: parent_input_resources.clone(),
child: input,
child_resources: input_resources.clone(),
});
};
if parent_output_resources != output_resources {
return Err(ValidationError::ParentIOResourceMismatch {
parent,
parent_resources: parent_output_resources.clone(),
child: output,
child_resources: output_resources.clone(),
});
};
}
};
Ok(())
}

/// Use the signature supplied by a dataflow node to work out the
/// resource requirements for all of its input and output edges, then put
/// those requirements in the ValidationContext
Expand Down Expand Up @@ -161,6 +197,10 @@ impl<'a> ValidationContext<'a> {
// Check operation-specific constraints
self.validate_operation(node, op_type)?;

// If this is a DataflowParent, check that the I/O nodes
// match the parent's signature
self.validate_io_resources(&self.hugr, node)?;

Ok(())
}

Expand Down Expand Up @@ -669,6 +709,13 @@ pub enum ValidationError {
},
#[error("Missing input resources for node {0:?}")]
MissingInputResources(Node),
#[error("IO nodes don't match parent signature")]
ParentIOResourceMismatch {
parent: Node,
parent_resources: ResourceSet,
child: Node,
child_resources: ResourceSet,
},
}

#[cfg(feature = "pyo3")]
Expand Down