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

feat: LowerTypes pass allows replacing extension types and ops #1989

Open
wants to merge 46 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
937cd14
Add TypeTransformer and (Sum/Function)Type(Arg/Row)::transform
acl-cqc Mar 11, 2025
815536b
trait Transformable to common up all the 'let mut any_change = false'…
acl-cqc Mar 12, 2025
cc1e8f2
Transformable v2: implement for [E], works without iter_mut/etc. in a…
acl-cqc Mar 12, 2025
4888184
first test, fix CustomType bound caching
acl-cqc Mar 17, 2025
7002e4d
Second test, fix SumType caching
acl-cqc Mar 17, 2025
6cc7cea
Make clippy happy
acl-cqc Mar 17, 2025
d43784b
Add HugrMut::optype_mut (v2, allow mutating root if RootHandle == Node)
acl-cqc Mar 11, 2025
422c496
WIP add hugr-passes/src/lower_types.rs (w/ change_node, subst_ty)
acl-cqc Mar 5, 2025
76ed391
Add def_arc
acl-cqc Mar 11, 2025
ed5b5dd
change_node, change_type, and the rest
acl-cqc Mar 10, 2025
adcbbf6
Use TypeTransformer framework, removing most type_stuff from lower_ty…
acl-cqc Mar 11, 2025
d84ae4e
Add a load of copy/discard lowering stuff, OpReplacement
acl-cqc Mar 12, 2025
bfa52cf
OpHashWrapper
acl-cqc Mar 12, 2025
b373571
Parametrized type support
acl-cqc Mar 17, 2025
a8e613a
remove copy_discard stuff
acl-cqc Mar 17, 2025
24ed15c
Assume less in OpReplacement::replace
acl-cqc Mar 17, 2025
9153fcf
parametrized ops
acl-cqc Mar 17, 2025
a89879c
Comments, renaming, use const_fn
acl-cqc Mar 17, 2025
c78d88a
Comment const_fn TODO
acl-cqc Mar 17, 2025
bf6a9a4
Test panics on unexpected argument - simpler, better
acl-cqc Mar 18, 2025
5ecd9e6
test functiontype
acl-cqc Mar 18, 2025
d9a6d29
clippy that new test
acl-cqc Mar 18, 2025
83e798e
Merge remote-tracking branch 'origin/main' into acl/type_transform
acl-cqc Mar 18, 2025
84fe82d
WIP setup for test
acl-cqc Mar 18, 2025
6168353
First test
acl-cqc Mar 18, 2025
fcb85a2
read only makes sense for Copyables
acl-cqc Mar 18, 2025
0f9aa17
Extend test to Calls of polyfunc; comments, monomorphize first
acl-cqc Mar 18, 2025
74c6775
no, instantiate the calls with types being lowered
acl-cqc Mar 18, 2025
00fd284
Consts: HashMap keyed by either, add lower_ methods. Test TailLoop an…
acl-cqc Mar 18, 2025
9f02acf
clippy, turn off type-complexity
acl-cqc Mar 18, 2025
a0ac6d6
Actual Error for check_sig, add setter method
acl-cqc Mar 19, 2025
6ac9efc
docs
acl-cqc Mar 19, 2025
044ff32
Test variable, boundednat; use list_type
acl-cqc Mar 19, 2025
b539e2f
Yet Another ValidationLevel interface
acl-cqc Mar 19, 2025
2c7e035
Merge remote-tracking branch 'origin/main' into acl/type_transform
acl-cqc Mar 19, 2025
6b190a6
Merge branch 'acl/type_transform' into acl/lower_types
acl-cqc Mar 19, 2025
bd24d1a
clippy
acl-cqc Mar 19, 2025
a5d8b65
doclinxs
acl-cqc Mar 19, 2025
6b1438c
pub re-export
acl-cqc Mar 19, 2025
d1036bc
fix const_loop for extensions
acl-cqc Mar 19, 2025
d19dc5a
fix other test for extensions, but turn off extension validation afte…
acl-cqc Mar 19, 2025
d0fddde
Add another test of Conditional + Case
acl-cqc Mar 21, 2025
3494887
common up read_op
acl-cqc Mar 21, 2025
ffdcaf2
test tidies
acl-cqc Mar 21, 2025
6f8f43c
No need to validate, run() does it for us
acl-cqc Mar 21, 2025
e3da259
check_sig: use Option::unzip to tuple-ize
acl-cqc Mar 21, 2025
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
21 changes: 18 additions & 3 deletions hugr-core/src/hugr/internal.rs
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@ use itertools::Itertools;
use portgraph::{LinkMut, LinkView, MultiPortGraph, PortMut, PortOffset, PortView};

use crate::ops::handle::NodeHandle;
use crate::ops::OpTrait;
use crate::ops::{OpTag, OpTrait};
use crate::{Direction, Hugr, Node};

use super::hugrmut::{panic_invalid_node, panic_invalid_non_root};
@@ -309,6 +309,22 @@ pub trait HugrMutInternals: RootTagged<Node = Node> {
}
self.hugr_mut().replace_op(node, op)
}

/// Gets a mutable reference to the optype.
///
/// Changing this may invalidate the ports, which may need to be resized to
/// match the OpType signature.
///
/// Will panic for the root node unless [`Self::RootHandle`](RootTagged::RootHandle)
/// is [OpTag::Any], as mutation could invalidate the bound.
fn optype_mut(&mut self, node: Node) -> &mut OpType {
if Self::RootHandle::TAG.is_superset(OpTag::Any) {
panic_invalid_node(self, node);
} else {
panic_invalid_non_root(self, node);
}
self.hugr_mut().op_types.get_mut(node.pg_index())
}
}

/// Impl for non-wrapped Hugrs. Overwrites the recursive default-impls to directly use the hugr.
@@ -406,8 +422,7 @@ impl<T: RootTagged<RootHandle = Node, Node = Node> + AsMut<Hugr>> HugrMutInterna

fn replace_op(&mut self, node: Node, op: impl Into<OpType>) -> Result<OpType, HugrError> {
// We know RootHandle=Node here so no need to check
let cur = self.hugr_mut().op_types.get_mut(node.pg_index());
Ok(std::mem::replace(cur, op.into()))
Ok(std::mem::replace(self.optype_mut(node), op.into()))
}
}

6 changes: 6 additions & 0 deletions hugr-core/src/ops/custom.rs
Original file line number Diff line number Diff line change
@@ -85,6 +85,12 @@ impl ExtensionOp {
self.def.as_ref()
}

/// Gets an Arc to the [`OpDef`] of this instance, i.e. usable to create
/// new instances.
pub fn def_arc(&self) -> &Arc<OpDef> {
&self.def
}

/// Attempt to evaluate this operation. See [`OpDef::constant_fold`].
pub fn constant_fold(&self, consts: &[(IncomingPort, ops::Value)]) -> ConstFoldResult {
self.def().constant_fold(self.args(), consts)
2 changes: 1 addition & 1 deletion hugr-core/src/types/poly_func.rs
Original file line number Diff line number Diff line change
@@ -119,7 +119,7 @@ impl<RV: MaybeRV> PolyFuncTypeBase<RV> {
/// # Errors
/// If there is not exactly one [TypeArg] for each binder ([Self::params]),
/// or an arg does not fit into its corresponding [TypeParam]
pub(crate) fn instantiate(&self, args: &[TypeArg]) -> Result<FuncTypeBase<RV>, SignatureError> {
pub fn instantiate(&self, args: &[TypeArg]) -> Result<FuncTypeBase<RV>, SignatureError> {
// Check that args are applicable, and that we have a value for each binder,
// i.e. each possible free variable within the body.
check_type_args(args, &self.params)?;
2 changes: 2 additions & 0 deletions hugr-passes/src/lib.rs
Original file line number Diff line number Diff line change
@@ -28,6 +28,8 @@ pub use monomorphize::remove_polyfuncs;
#[allow(deprecated)]
pub use monomorphize::monomorphize;
pub use monomorphize::{MonomorphizeError, MonomorphizePass};
pub mod lower_types;
pub use lower_types::LowerTypes;
pub mod nest_cfgs;
pub mod non_local;
pub mod validation;
711 changes: 711 additions & 0 deletions hugr-passes/src/lower_types.rs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion hugr-passes/src/validation.rs
Original file line number Diff line number Diff line change
@@ -23,7 +23,7 @@ pub enum ValidationLevel {
WithExtensions,
}

#[derive(Error, Debug)]
#[derive(Error, Debug, PartialEq)]
#[allow(missing_docs)]
pub enum ValidatePassError {
#[error("Failed to validate input HUGR: {err}\n{pretty_hugr}")]