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: Extend LowerTypes pass to linearize by inserting copy/discard #2018

Open
wants to merge 103 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
103 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
28f2470
Move private utility classes below the pub ones; add comments
acl-cqc Mar 21, 2025
bef90b0
Comments - all callbacks return Option
acl-cqc Mar 24, 2025
85df7ff
test: Rename lower_types to lowerer
acl-cqc Mar 25, 2025
507f027
Extend loop_const test
acl-cqc Mar 25, 2025
bae0ebe
Add copy/dup linearization stuff
acl-cqc Mar 25, 2025
8fa79bd
do_copy_chain => insert_copy_discard
acl-cqc Mar 21, 2025
6a186a5
Move insert_copy_discard into Linearizer
acl-cqc Mar 25, 2025
6d8a89b
Add LinearizeError
acl-cqc Mar 21, 2025
3f0f1e6
pass Linearizer to callbacks
acl-cqc Mar 21, 2025
974a25b
comments
acl-cqc Mar 24, 2025
2bbf663
first test, don't linearize root node outputs, reject nonlocal edges
acl-cqc Mar 21, 2025
290a8ae
copy_fn/discard_fn return Result
acl-cqc Mar 23, 2025
19b1442
linearize takes &TypeDef not TypeDef
acl-cqc Mar 23, 2025
269f0f1
OpReplacement::add(&self, ...) -> add_hugr(self, ...)
acl-cqc Mar 24, 2025
f6a1af5
Drop an else-continue
acl-cqc Mar 25, 2025
ac9adac
Add OpReplacement::add for builder
acl-cqc Mar 24, 2025
65eaf52
Handle Sum Types in copy_op/discard_op
acl-cqc Mar 25, 2025
354a89c
drop redundant allow-missing_docs
acl-cqc Mar 25, 2025
1b31631
Refactor test code, fix --all-features
acl-cqc Mar 23, 2025
d5cbd58
Test copying+discarding an option of two elements
acl-cqc Mar 25, 2025
63bdd3b
tidy imports
acl-cqc Mar 25, 2025
1ac3728
Combine lower_consts(_parametric) with lower_(parametric_)type
acl-cqc Mar 25, 2025
e768814
Test sig checking
acl-cqc Mar 25, 2025
506570b
No, remove sig checking
acl-cqc Mar 25, 2025
0958e2a
Test funky partial replace
acl-cqc Mar 25, 2025
74cfdb1
common up just_elem_type
acl-cqc Mar 25, 2025
dc79c93
comment spacing
acl-cqc Mar 25, 2025
63d24b0
RIP SignatureMismatch
acl-cqc Mar 25, 2025
ba63fbe
Reinstate separate lower_consts/lower_consts_parametric
acl-cqc Mar 26, 2025
2c7bb80
Remove the boxes
acl-cqc Mar 26, 2025
08d3227
drop comment re. validation_level
acl-cqc Mar 26, 2025
dedc3d5
rename module lower_types -> replace_types
acl-cqc Mar 26, 2025
9993467
rename LowerTypes=>ReplaceTypes, ChangeTypeError => ReplaceTypesError
acl-cqc Mar 26, 2025
cc81d87
rename methods, parametric => parametrized
acl-cqc Mar 26, 2025
74c6492
doc notes
acl-cqc Mar 26, 2025
18316de
comment/doc updates
acl-cqc Mar 26, 2025
520e414
fmt
acl-cqc Mar 26, 2025
0814bf2
callbacks for Const take &ReplaceTypes
acl-cqc Mar 26, 2025
0a82fef
Replace that break 'changed block with a match, + map_or_else to avoi…
acl-cqc Mar 26, 2025
94c0ff3
Add Type::as_extension
acl-cqc Mar 26, 2025
e9ff81c
wip
acl-cqc Mar 26, 2025
cf6451f
add list_const handler, use in test
acl-cqc Mar 26, 2025
9a355d5
Clarify replace_consts_parametrized callback
acl-cqc Mar 26, 2025
ec2d0ba
const callbacks report errors via Result
acl-cqc Mar 26, 2025
c9d7033
fmt+fix
acl-cqc Mar 26, 2025
812ac89
comment tweaks
acl-cqc Mar 26, 2025
ae7bd6e
Merge remote-tracking branch 'origin/main' into acl/lower_types
acl-cqc Mar 26, 2025
e0937ee
docs
acl-cqc Mar 26, 2025
36df03c
and some more - warn on missing_docs except ReplaceTypesError
acl-cqc Mar 26, 2025
71f8c0b
Merge remote-tracking branch 'origin/main' into acl/lower_types
acl-cqc Mar 26, 2025
dc0c7dd
Merge branch 'acl/lower_types' into acl/lower_types_linearize
acl-cqc Mar 26, 2025
31ba4e0
docs, make pub, more errors
acl-cqc Mar 26, 2025
86c73ed
Test copyable element inside Sum - breaks test
acl-cqc Mar 26, 2025
92d7a51
Error on copyable; handle copyable elements of sums - fixes
acl-cqc Mar 26, 2025
582b9f1
register errors with type; panic on function as already ruled out
acl-cqc Mar 26, 2025
95da97d
more docs - no overriding copy/discard of non-extension types
acl-cqc Mar 26, 2025
c7e56e6
pub discard_op
acl-cqc Mar 27, 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
100 changes: 97 additions & 3 deletions hugr-passes/src/replace_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,30 @@
//! Parametrized types and ops will be reparametrized taking into account the replacements,
//! but any ops taking/returning the replaced types *not* as a result of parametrization,
//! will also need to be replaced - see [ReplaceTypes::replace_op]. (Similarly [Const]s.)
use std::borrow::Cow;
use std::collections::HashMap;
use std::sync::Arc;

use thiserror::Error;

use hugr_core::builder::{BuildError, BuildHandle, Dataflow};
use hugr_core::extension::{ExtensionId, OpDef, SignatureError, TypeDef};
use hugr_core::hugr::hugrmut::HugrMut;
use hugr_core::ops::constant::{OpaqueValue, Sum};
use hugr_core::ops::handle::DataflowOpID;
use hugr_core::ops::{
AliasDefn, Call, CallIndirect, Case, Conditional, Const, DataflowBlock, ExitBlock, ExtensionOp,
FuncDecl, FuncDefn, Input, LoadConstant, LoadFunction, OpType, Output, Tag, TailLoop, Value,
CFG, DFG,
FuncDecl, FuncDefn, Input, LoadConstant, LoadFunction, OpTrait, OpType, Output, Tag, TailLoop,
Value, CFG, DFG,
};
use hugr_core::types::{CustomType, Transformable, Type, TypeArg, TypeEnum, TypeTransformer};
use hugr_core::{Hugr, Node};
use hugr_core::{Hugr, Node, Wire};

use crate::validation::{ValidatePassError, ValidationLevel};

mod linearize;
pub use linearize::{LinearizeError, Linearizer};

/// A thing with which an Op (i.e. node) can be replaced
#[derive(Clone, Debug, PartialEq)]
pub enum OpReplacement {
Expand All @@ -43,6 +49,27 @@ pub enum OpReplacement {
}

impl OpReplacement {
/// Adds this instance to the specified [HugrMut] as a new node or subtree under a
/// given parent, returning the unique new child (of that parent) thus created
pub fn add_hugr(self, hugr: &mut impl HugrMut, parent: Node) -> Node {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These two are pub because other extensions may want them for defining their own copy/discard handlers (that build nested Hugrs that copy elements of collections, etc.)

match self {
OpReplacement::SingleOp(op_type) => hugr.add_node_with_parent(parent, op_type),
OpReplacement::CompoundOp(new_h) => hugr.insert_hugr(parent, *new_h).new_root,
}
}

/// Adds this instance to the specified [Dataflow] builder as a new node or subtree
pub fn add(
self,
dfb: &mut impl Dataflow,
inputs: impl IntoIterator<Item = Wire>,
) -> Result<BuildHandle<DataflowOpID>, BuildError> {
match self {
OpReplacement::SingleOp(opty) => dfb.add_dataflow_op(opty, inputs),
OpReplacement::CompoundOp(h) => dfb.add_hugr_with_wires(*h, inputs),
}
}

fn replace(&self, hugr: &mut impl HugrMut, n: Node) {
assert_eq!(hugr.children(n).count(), 0);
let new_optype = match self.clone() {
Expand All @@ -67,6 +94,7 @@ impl OpReplacement {
pub struct ReplaceTypes {
type_map: HashMap<CustomType, Type>,
param_types: HashMap<ParametricType, Arc<dyn Fn(&[TypeArg]) -> Option<Type>>>,
linearize: Linearizer,
op_map: HashMap<OpHashWrapper, OpReplacement>,
param_ops: HashMap<ParametricOp, Arc<dyn Fn(&[TypeArg]) -> Option<OpReplacement>>>,
consts: HashMap<
Expand Down Expand Up @@ -109,6 +137,8 @@ pub enum ReplaceTypesError {
SignatureError(#[from] SignatureError),
#[error(transparent)]
ValidationError(#[from] ValidatePassError),
#[error(transparent)]
LinearizeError(#[from] LinearizeError),
}

impl ReplaceTypes {
Expand Down Expand Up @@ -157,9 +187,55 @@ impl ReplaceTypes {
// (depending on arguments - i.e. if src's TypeDefBound is anything other than
// `TypeDefBound::Explicit(TypeBound::Copyable)`) but that seems an annoying
// overapproximation. Moreover, these depend upon the *return type* of the Fn.
// It would be too awkward to require:
// dest_fn: impl Fn(&TypeArg) -> (Type,
// Fn(&Linearizer) -> OpReplacement, // copy
// Fn(&Linearizer) -> OpReplacement)` // discard
self.param_types.insert(src.into(), Arc::new(dest_fn));
}

/// Configures this instance that, when an outport of type `src` has other than one connected
/// inport, the specified `copy` and or `discard` ops should be used to wire it to those inports.
/// (`copy` should have exactly one inport, of type `src`, and two outports, of same type;
/// `discard` should have exactly one inport, of type 'src', and no outports.)
///
/// Also applies if `src` is an element of a Sum or other type.
///
/// # Errors
///
/// If `src` is [Copyable], it is returned as an `Err
///
/// [Copyable]: hugr_core::types::TypeBound::Copyable
pub fn linearize(
&mut self,
src: Type,
copy: OpReplacement,
discard: OpReplacement,
) -> Result<(), Type> {
self.linearize.register(src, copy, discard)
}

/// Configures this instance that when lowering produces an outport which
/// * has type an instantiation of the parametric type `src`, and
/// * is not [Copyable](hugr_core::types::TypeBound::Copyable), and
/// * has other than one connected inport,
///
/// ...then these functions should be used to generate `copy` or `discard` ops.
///
/// (That is, this is the equivalent of [Self::linearize] but for parametric types.)
///
/// The [Linearizer] is passed so that the callbacks can use it to generate
/// `copy/`discard` ops for other types (e.g. the elements of a collection),
/// as part of an [OpReplacement::CompoundOp].
pub fn linearize_parametric(
&mut self,
src: &TypeDef,
copy_fn: Box<dyn Fn(&[TypeArg], &Linearizer) -> Result<OpReplacement, LinearizeError>>,
discard_fn: Box<dyn Fn(&[TypeArg], &Linearizer) -> Result<OpReplacement, LinearizeError>>,
) {
self.linearize.register_parametric(src, copy_fn, discard_fn)
}

/// Configures this instance to change occurrences of `src` to `dest`.
/// Note that if `src` is an instance of a *parametrized* [OpDef], this takes
/// precedence over [Self::replace_parametrized_op] where the `src`s overlap. Thus,
Expand Down Expand Up @@ -221,6 +297,24 @@ impl ReplaceTypes {
let mut changed = false;
for n in hugr.nodes().collect::<Vec<_>>() {
changed |= self.change_node(hugr, n)?;
let new_dfsig = hugr.get_optype(n).dataflow_signature();
if let Some(new_sig) = (changed && n != hugr.root())
.then_some(new_dfsig)
.flatten()
.map(Cow::into_owned)
{
for outp in new_sig.output_ports() {
if !new_sig.out_port_type(outp).unwrap().copyable() {
let targets = hugr.linked_inputs(n, outp).collect::<Vec<_>>();
if targets.len() != 1 {
hugr.disconnect(n, outp);
let typ = new_sig.out_port_type(outp).unwrap();
self.linearize
.insert_copy_discard(hugr, n, outp, typ, &targets)?;
}
}
}
}
}
Ok(changed)
}
Expand Down
Loading
Loading