Skip to content

Commit

Permalink
refactor: use DataflowParent to common up validation
Browse files Browse the repository at this point in the history
  • Loading branch information
ss2165 committed Jan 4, 2024
1 parent fcb6342 commit aeff10a
Showing 1 changed file with 5 additions and 75 deletions.
80 changes: 5 additions & 75 deletions src/ops/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ use itertools::Itertools;
use portgraph::{NodeIndex, PortOffset};
use thiserror::Error;

use crate::types::{FunctionType, Type, TypeRow};
use crate::types::TypeRow;

use super::controlflow::BasicBlock;
use super::dataflow::DataflowParent;
use super::{impl_validate_op, Exit, OpTag, OpTrait, OpType, ValidateOp, DFB};
use super::{impl_validate_op, Exit, OpTag, OpTrait, OpType, ValidateOp};

/// A set of property flags required for an operation.
#[non_exhaustive]
Expand Down Expand Up @@ -63,50 +63,6 @@ impl ValidateOp for super::Module {
}
}

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_op_children<'a>(
&self,
children: impl DoubleEndedIterator<Item = (NodeIndex, &'a OpType)>,
) -> Result<(), ChildrenValidationError> {
// We check type-variables are declared in `validate_subtree`, so here
// we can just assume all type variables are valid regardless of binders.
let FunctionType { input, output, .. } = self.inner_signature();
validate_io_nodes(&input, &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_op_children<'a>(
&self,
children: impl DoubleEndedIterator<Item = (NodeIndex, &'a OpType)>,
) -> Result<(), ChildrenValidationError> {
let sig = self.dataflow_signature().unwrap_or_default();
validate_io_nodes(&sig.input, &sig.output, "nested graph", children)
}
}

impl ValidateOp for super::Conditional {
fn validity_flags(&self) -> OpValidityFlags {
OpValidityFlags {
Expand Down Expand Up @@ -287,35 +243,8 @@ pub struct ChildrenEdgeData {
/// Target port.
pub target_port: PortOffset,
}
impl ValidateOp for DFB {
/// 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_op_children<'a>(
&self,
children: impl DoubleEndedIterator<Item = (NodeIndex, &'a OpType)>,
) -> Result<(), ChildrenValidationError> {
let tuple_sum_type = Type::new_tuple_sum(self.tuple_sum_rows.clone());
let node_outputs: TypeRow = [&[tuple_sum_type], self.other_outputs.as_ref()]
.concat()
.into();
validate_io_nodes(&self.inputs, &node_outputs, "basic block graph", children)
}
}

impl ValidateOp for Exit {}

impl ValidateOp for super::Case {
impl<T: DataflowParent> ValidateOp for T {
/// Returns the set of allowed parent operation types.
fn validity_flags(&self) -> OpValidityFlags {
OpValidityFlags {
Expand All @@ -334,7 +263,7 @@ impl ValidateOp for super::Case {
children: impl DoubleEndedIterator<Item = (NodeIndex, &'a OpType)>,
) -> Result<(), ChildrenValidationError> {
let sig = self.inner_signature();
validate_io_nodes(&sig.input, &sig.output, "Conditional", children)
validate_io_nodes(&sig.input, &sig.output, "DataflowParent", children)
}
}

Expand Down Expand Up @@ -488,3 +417,4 @@ impl_validate_op!(Call);
impl_validate_op!(LoadConstant);
impl_validate_op!(CallIndirect);
impl_validate_op!(LeafOp);
impl_validate_op!(Exit);

0 comments on commit aeff10a

Please sign in to comment.