Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
52 changes: 52 additions & 0 deletions hugr-passes/src/composable.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
//! Compiler passes and utilities for composing them

mod scope;

pub use scope::{InScope, PassScope};

use std::{error::Error, marker::PhantomData};

use hugr_core::core::HugrNode;
Expand All @@ -17,6 +21,26 @@ pub trait ComposablePass<H: HugrMut>: Sized {
/// Run the pass on the given HUGR.
fn run(&self, hugr: &mut H) -> Result<Self::Result, Self::Error>;

/// Set the scope configuration used to run the pass.
///
/// See [`PassScope`] for more details.
///
/// In `hugr 0.25.*`, this configuration is only a guidance, and may be
/// ignored by the pass by using the default implementation.
///
/// From `hugr >=0.26.0`, passes must respect the scope configuration.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I'm really unsure about this, see main discussion/reply

//
// For hugr passes, this is tracked by <https://github.com/Quantinuum/hugr/issues/2771>
fn with_scope(self, scope: &PassScope) -> Self {
// Currently passes are not required to respect the scope configuration.
// <https://github.com/Quantinuum/hugr/issues/2771>
//
// deprecated: Remove default implementation in hugr 0.26.0,
// ensure all passes follow the scope configuration.
let _ = scope;
self
}

/// Apply a function to the error type of this pass, returning a new
/// [`ComposablePass`] that has the same result type.
fn map_err<E2: Error>(
Expand All @@ -28,6 +52,10 @@ pub trait ComposablePass<H: HugrMut>: Sized {

/// Returns a [`ComposablePass`] that does "`self` then `other`", so long as
/// `other::Err` can be combined with ours.
///
/// Composed passes may have different configured [`PassScope`]s. Use
/// [`ComposablePass::with_scope`] after the composition to override all the
/// scope configurations if needed.
fn then<P: ComposablePass<H>, E: ErrorCombiner<Self::Error, P::Error>>(
self,
other: P,
Expand All @@ -48,6 +76,14 @@ pub trait ComposablePass<H: HugrMut>: Sized {
let res2 = self.1.run(hugr).map_err(E::from_second)?;
Ok((res1, res2))
}

fn with_scope(self, scope: &PassScope) -> Self {
Self(
self.0.with_scope(scope),
self.1.with_scope(scope),
PhantomData,
)
}
}

Sequence(self, other, PhantomData)
Expand Down Expand Up @@ -110,6 +146,10 @@ impl<P: ComposablePass<H>, H: HugrMut, E: Error, F: Fn(P::Error) -> E> Composabl
fn run(&self, hugr: &mut H) -> Result<P::Result, Self::Error> {
self.0.run(hugr).map_err(&self.1)
}

fn with_scope(self, scope: &PassScope) -> Self {
Self(self.0.with_scope(scope), self.1, PhantomData)
}
}

// ValidatingPass ------------------------------
Expand Down Expand Up @@ -188,6 +228,10 @@ where
})?;
Ok(res)
}

fn with_scope(self, scope: &PassScope) -> Self {
Self(self.0.with_scope(scope), self.1)
}
}

// IfThen ------------------------------
Expand Down Expand Up @@ -225,6 +269,14 @@ impl<
res.then(|| self.1.run(hugr).map_err(ErrorCombiner::from_second))
.transpose()
}

fn with_scope(self, scope: &PassScope) -> Self {
Self(
self.0.with_scope(scope),
self.1.with_scope(scope),
PhantomData,
)
}
}

pub(crate) fn validate_if_test<P: ComposablePass<H>, H: HugrMut>(
Expand Down
Loading
Loading