Skip to content

Commit

Permalink
Merge branch 'main' into feat/validate-resources
Browse files Browse the repository at this point in the history
  • Loading branch information
aborgna-q authored Aug 9, 2023
2 parents be673cb + fa3b1ec commit d325859
Show file tree
Hide file tree
Showing 13 changed files with 94 additions and 57 deletions.
2 changes: 1 addition & 1 deletion src/algorithm/half_node.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::hash::Hash;

use super::nest_cfgs::CfgView;
use crate::hugr::view::HugrView;
use crate::hugr::views::HugrView;
use crate::ops::OpTag;
use crate::ops::OpTrait;
use crate::{Direction, Node};
Expand Down
6 changes: 3 additions & 3 deletions src/algorithm/nest_cfgs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ use std::hash::Hash;

use itertools::Itertools;

use crate::hugr::view::HugrView;
use crate::hugr::views::HugrView;
use crate::ops::OpTag;
use crate::ops::OpTrait;
use crate::{Direction, Node};
Expand Down Expand Up @@ -398,7 +398,7 @@ impl<T: Copy + Clone + PartialEq + Eq + Hash> EdgeClassifier<T> {
pub(crate) mod test {
use super::*;
use crate::builder::{BuildError, CFGBuilder, Container, DataflowSubContainer, HugrBuilder};
use crate::hugr::hierarchical_views::{HierarchyView, SiblingGraph};
use crate::hugr::views::{HierarchyView, SiblingGraph};
use crate::ops::handle::{BasicBlockID, ConstID, NodeHandle};
use crate::ops::Const;
use crate::types::{ClassicType, SimpleType};
Expand Down Expand Up @@ -447,7 +447,7 @@ pub(crate) mod test {
let (split, merge, head, tail) = (split.node(), merge.node(), head.node(), tail.node());
// There's no need to use a FlatRegionView here but we do so just to check
// that we *can* (as we'll need to for "real" module Hugr's).
let v = SiblingGraph::new(&h, h.root());
let v: SiblingGraph = SiblingGraph::new(&h, h.root());
let edge_classes = EdgeClassifier::get_edge_classes(&SimpleCfgView::new(&v));
let [&left,&right] = edge_classes.keys().filter(|(s,_)| *s == split).map(|(_,t)|t).collect::<Vec<_>>()[..] else {panic!("Split node should have two successors");};

Expand Down
2 changes: 1 addition & 1 deletion src/builder/build_traits.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::hugr::validate::InterGraphEdgeError;
use crate::hugr::view::HugrView;
use crate::hugr::views::HugrView;
use crate::hugr::{Node, NodeMetadata, Port, ValidationError};
use crate::ops::{self, LeafOp, OpTrait, OpType};

Expand Down
2 changes: 1 addition & 1 deletion src/builder/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::{
BasicBlockID, BuildError, CfgID, Container, Dataflow, HugrBuilder, Wire,
};

use crate::hugr::view::HugrView;
use crate::hugr::views::HugrView;
use crate::ops::handle::NodeHandle;
use crate::ops::{self, BasicBlock, OpType};
use crate::types::AbstractSignature;
Expand Down
2 changes: 1 addition & 1 deletion src/builder/conditional.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::hugr::view::HugrView;
use crate::hugr::views::HugrView;
use crate::types::{AbstractSignature, ClassicRow, SimpleRow};

use crate::ops;
Expand Down
2 changes: 1 addition & 1 deletion src/builder/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::{
};

use crate::{
hugr::{view::HugrView, ValidationError},
hugr::{views::HugrView, ValidationError},
ops,
types::{simple::TypeTag, PrimType, SimpleType},
};
Expand Down
2 changes: 1 addition & 1 deletion src/builder/tail_loop.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::ops::{self, OpType};

use crate::hugr::{view::HugrView, NodeType};
use crate::hugr::{views::HugrView, NodeType};
use crate::types::{AbstractSignature, ClassicRow, SimpleRow};
use crate::{Hugr, Node};

Expand Down
5 changes: 2 additions & 3 deletions src/hugr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

mod hugrmut;

pub mod hierarchical_views;
pub mod rewrite;
pub mod serialize;
pub mod validate;
pub mod view;
pub mod views;

use std::collections::VecDeque;
use std::iter;
Expand All @@ -24,7 +23,7 @@ use thiserror::Error;
#[cfg(feature = "pyo3")]
use pyo3::prelude::*;

pub use self::view::HugrView;
pub use self::views::HugrView;
use crate::ops::{OpTag, OpTrait, OpType};
use crate::resource::ResourceSet;
use crate::types::{AbstractSignature, Signature};
Expand Down
2 changes: 1 addition & 1 deletion src/hugr/rewrite/simple_replace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ mod test {
BuildError, Container, DFGBuilder, Dataflow, DataflowHugr, DataflowSubContainer,
HugrBuilder, ModuleBuilder,
};
use crate::hugr::view::HugrView;
use crate::hugr::views::HugrView;
use crate::hugr::{Hugr, Node};
use crate::ops::OpTag;
use crate::ops::{LeafOp, OpTrait, OpType};
Expand Down
7 changes: 3 additions & 4 deletions src/hugr/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ use crate::resource::validate::{ResourceError, ResourceValidator};
use crate::types::{ClassicType, EdgeKind, SimpleType};
use crate::{Direction, Hugr, Node, Port};

use super::hierarchical_views::{HierarchyView, SiblingGraph};
use super::view::HugrView;
use super::views::{HierarchyView, HugrView, SiblingGraph};
use super::NodeType;

/// Structure keeping track of pre-computed information used in the validation
Expand Down Expand Up @@ -76,7 +75,7 @@ impl<'a> ValidationContext<'a> {
/// The results of this computation should be cached in `self.dominators`.
/// We don't do it here to avoid mutable borrows.
fn compute_dominator(&self, parent: Node) -> Dominators<Node> {
let region = SiblingGraph::new(self.hugr, parent);
let region: SiblingGraph = SiblingGraph::new(self.hugr, parent);
let entry_node = self.hugr.children(parent).next().unwrap();
dominators::simple_fast(&region, entry_node)
}
Expand Down Expand Up @@ -336,7 +335,7 @@ impl<'a> ValidationContext<'a> {
return Ok(());
};

let region = SiblingGraph::new(self.hugr, parent);
let region: SiblingGraph = SiblingGraph::new(self.hugr, parent);
let entry_node = self.hugr.children(parent).next().unwrap();

let postorder = DfsPostOrder::new(&region, entry_node);
Expand Down
11 changes: 5 additions & 6 deletions src/hugr/view.rs → src/hugr/views.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#![allow(unused)]
//! A Trait for "read-only" HUGRs.
//! Read-only access into HUGR graphs and subgraphs.

use std::iter::FusedIterator;
use std::ops::Deref;
pub mod hierarchy;

use context_iterators::{ContextIterator, IntoContextIterator, MapCtx, MapWithCtx, WithCtx};
pub use hierarchy::{DescendantsGraph, HierarchyView, SiblingGraph};

use context_iterators::{ContextIterator, IntoContextIterator, MapWithCtx};
use itertools::{Itertools, MapInto};
use portgraph::dot::{DotFormat, EdgeStyle, NodeStyle, PortStyle};
use portgraph::{multiportgraph, LinkView, MultiPortGraph, PortView};
Expand Down Expand Up @@ -299,7 +299,6 @@ where
#[inline]
fn get_io(&self, node: Node) -> Option<[Node; 2]> {
let op = self.get_nodetype(node);
let dfp = OpTag::DataflowParent;
if op.tag().is_superset(OpTag::DataflowParent) {
self.children(node).take(2).collect_vec().try_into().ok()
} else {
Expand Down
61 changes: 44 additions & 17 deletions src/hugr/hierarchical_views.rs → src/hugr/views/hierarchy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ use context_iterators::{ContextIterator, IntoContextIterator, MapWithCtx};
use itertools::{Itertools, MapInto};
use portgraph::{LinkView, PortIndex, PortView};

use crate::ops::handle::NodeHandle;
use crate::ops::OpTrait;
use crate::{hugr::NodeType, hugr::OpType, Direction, Hugr, Node, Port};

use super::view::sealed::HugrInternals;
use super::{HugrView, NodeMetadata};
use super::{sealed::HugrInternals, HugrView, NodeMetadata};

type FlatRegionGraph<'g, Base> =
portgraph::view::FlatRegion<'g, <Base as HugrInternals>::Portgraph>;
Expand All @@ -36,7 +37,7 @@ type FlatRegionGraph<'g, Base> =
/// Includes only the root node and its direct children.
///
/// For a view that includes all the descendants of the root, see [`DescendantsGraph`].
pub struct SiblingGraph<'g, Base = Hugr>
pub struct SiblingGraph<'g, Root = Node, Base = Hugr>
where
Base: HugrInternals,
{
Expand All @@ -48,19 +49,24 @@ where

/// The rest of the HUGR.
hugr: &'g Base,

/// The operation type of the root node.
_phantom: std::marker::PhantomData<Root>,
}

impl<'g, Base> Clone for SiblingGraph<'g, Base>
impl<'g, Root, Base> Clone for SiblingGraph<'g, Root, Base>
where
Root: NodeHandle,
Base: HugrInternals + HugrView + Clone,
{
fn clone(&self) -> Self {
SiblingGraph::new(self.hugr, self.root)
}
}

impl<'g, Base> HugrView for SiblingGraph<'g, Base>
impl<'g, Root, Base> HugrView for SiblingGraph<'g, Root, Base>
where
Root: NodeHandle,
Base: HugrInternals + HugrView,
{
type Nodes<'a> = iter::Chain<iter::Once<Node>, MapInto<portgraph::hierarchy::Children<'a>, Node>>
Expand Down Expand Up @@ -200,7 +206,7 @@ type RegionGraph<'g, Base> = portgraph::view::Region<'g, <Base as HugrInternals>
/// For a view that includes only the direct children of the root, see
/// [`SiblingGraph`]. Prefer using [`SiblingGraph`] over this type when
/// possible, as it is more efficient.
pub struct DescendantsGraph<'g, Base>
pub struct DescendantsGraph<'g, Root = Node, Base = Hugr>
where
Base: HugrInternals,
{
Expand All @@ -212,19 +218,24 @@ where

/// The node hierarchy.
hugr: &'g Base,

/// The operation handle of the root node.
_phantom: std::marker::PhantomData<Root>,
}

impl<'g, Base: Clone> Clone for DescendantsGraph<'g, Base>
impl<'g, Root, Base: Clone> Clone for DescendantsGraph<'g, Root, Base>
where
Root: NodeHandle,
Base: HugrInternals + HugrView,
{
fn clone(&self) -> Self {
DescendantsGraph::new(self.hugr, self.root)
}
}

impl<'g, Base> HugrView for DescendantsGraph<'g, Base>
impl<'g, Root, Base> HugrView for DescendantsGraph<'g, Root, Base>
where
Root: NodeHandle,
Base: HugrInternals + HugrView,
{
type Nodes<'a> = MapInto<<RegionGraph<'g, Base> as PortView>::Nodes<'a>, Node>
Expand Down Expand Up @@ -345,7 +356,7 @@ where
}
}

/// A common trait for views of a hugr region.
/// A common trait for views of a HUGR hierarchical subgraph.
pub trait HierarchyView<'a>:
HugrView
+ pv::GraphBase<NodeId = Node>
Expand All @@ -359,20 +370,26 @@ pub trait HierarchyView<'a>:
where
for<'g> &'g Self: pv::IntoNeighborsDirected + pv::IntoNodeIdentifiers,
{
/// The base from which the region is derived.
/// The base from which the subgraph is derived.
type Base;

/// Create a region view of a HUGR given a root node.
/// Create a hierarchical view of a HUGR given a root node.
fn new(hugr: &'a Self::Base, root: Node) -> Self;
}

impl<'a, Base> HierarchyView<'a> for SiblingGraph<'a, Base>
impl<'a, Root, Base> HierarchyView<'a> for SiblingGraph<'a, Root, Base>
where
Root: NodeHandle,
Base: HugrInternals + HugrView,
{
type Base = Base;

fn new(hugr: &'a Base, root: Node) -> Self {
let root_tag = hugr.get_optype(root).tag();
if !Root::TAG.is_superset(root_tag) {
// TODO: Return an error
panic!("Root node must have the correct operation type tag.")
}
Self {
root,
graph: FlatRegionGraph::<Base>::new_flat_region(
Expand All @@ -381,17 +398,24 @@ where
root.index,
),
hugr,
_phantom: std::marker::PhantomData,
}
}
}

impl<'a, Base> HierarchyView<'a> for DescendantsGraph<'a, Base>
impl<'a, Root, Base> HierarchyView<'a> for DescendantsGraph<'a, Root, Base>
where
Root: NodeHandle,
Base: HugrInternals + HugrView,
{
type Base = Base;

fn new(hugr: &'a Base, root: Node) -> Self {
let root_tag = hugr.get_optype(root).tag();
if !Root::TAG.is_superset(root_tag) {
// TODO: Return an error
panic!("Root node must have the correct operation type tag.")
}
Self {
root,
graph: RegionGraph::<Base>::new_region(
Expand All @@ -400,12 +424,14 @@ where
root.index,
),
hugr,
_phantom: std::marker::PhantomData,
}
}
}

impl<'g, Base> super::view::sealed::HugrInternals for SiblingGraph<'g, Base>
impl<'g, Root, Base> super::sealed::HugrInternals for SiblingGraph<'g, Root, Base>
where
Root: NodeHandle,
Base: HugrInternals,
{
type Portgraph = FlatRegionGraph<'g, Base>;
Expand All @@ -421,8 +447,9 @@ where
}
}

impl<'g, Base> super::view::sealed::HugrInternals for DescendantsGraph<'g, Base>
impl<'g, Root, Base> super::sealed::HugrInternals for DescendantsGraph<'g, Root, Base>
where
Root: NodeHandle,
Base: HugrInternals,
{
type Portgraph = RegionGraph<'g, Base>;
Expand Down Expand Up @@ -490,7 +517,7 @@ mod test {
fn flat_region() -> Result<(), Box<dyn std::error::Error>> {
let (hugr, def, inner) = make_module_hgr()?;

let region = SiblingGraph::new(&hugr, def);
let region: SiblingGraph = SiblingGraph::new(&hugr, def);

assert_eq!(region.node_count(), 5);
assert!(region
Expand All @@ -505,7 +532,7 @@ mod test {
fn full_region() -> Result<(), Box<dyn std::error::Error>> {
let (hugr, def, inner) = make_module_hgr()?;

let region = DescendantsGraph::new(&hugr, def);
let region: DescendantsGraph = DescendantsGraph::new(&hugr, def);

assert_eq!(region.node_count(), 7);
assert!(region.nodes().all(|n| n == def
Expand Down
Loading

0 comments on commit d325859

Please sign in to comment.