Skip to content

Commit fdaf8b0

Browse files
committed
feat: Add array linearisation pass
1 parent 03d5320 commit fdaf8b0

File tree

7 files changed

+541
-18
lines changed

7 files changed

+541
-18
lines changed

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

hugr-core/src/std_extensions/collections/array/array_kind.rs

+27-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
use std::sync::Arc;
22

3+
use crate::std_extensions::collections::array::op_builder::GenericArrayOpBuilder;
34
use crate::{
5+
builder::{BuildError, Dataflow},
46
extension::{ExtensionId, SignatureError, TypeDef},
57
ops::constant::ValueName,
68
types::{CustomType, Type, TypeArg, TypeName},
7-
Extension,
9+
Extension, Wire,
810
};
911

1012
/// Trait capturing a concrete array implementation in an extension.
@@ -90,4 +92,28 @@ pub trait ArrayKind:
9092
) -> Result<Type, SignatureError> {
9193
Self::instantiate_ty(Self::type_def(), size, element_ty)
9294
}
95+
96+
/// Adds a operation to a dataflow graph that clones an array of copyable values.
97+
///
98+
/// The default implementation uses the array clone operation.
99+
fn build_clone<D: Dataflow>(
100+
builder: &mut D,
101+
elem_ty: Type,
102+
size: u64,
103+
arr: Wire,
104+
) -> Result<(Wire, Wire), BuildError> {
105+
builder.add_generic_array_clone::<Self>(elem_ty, size, arr)
106+
}
107+
108+
/// Adds a operation to a dataflow graph that clones an array of copyable values.
109+
///
110+
/// The default implementation uses the array clone operation.
111+
fn build_discard<D: Dataflow>(
112+
builder: &mut D,
113+
elem_ty: Type,
114+
size: u64,
115+
arr: Wire,
116+
) -> Result<(), BuildError> {
117+
builder.add_generic_array_discard::<Self>(elem_ty, size, arr)
118+
}
93119
}

hugr-core/src/std_extensions/collections/value_array.rs

+18
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,24 @@ impl ArrayKind for ValueArray {
5151
fn type_def() -> &'static TypeDef {
5252
EXTENSION.get_type(&VALUE_ARRAY_TYPENAME).unwrap()
5353
}
54+
55+
fn build_clone<D: Dataflow>(
56+
_builder: &mut D,
57+
_elem_ty: Type,
58+
_size: u64,
59+
arr: Wire,
60+
) -> Result<(Wire, Wire), BuildError> {
61+
Ok((arr, arr))
62+
}
63+
64+
fn build_discard<D: Dataflow>(
65+
_builder: &mut D,
66+
_elem_ty: Type,
67+
_size: u64,
68+
_arr: Wire,
69+
) -> Result<(), BuildError> {
70+
Ok(())
71+
}
5472
}
5573

5674
/// Value array operation definitions.

hugr-passes/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ lazy_static = { workspace = true }
2525
paste = { workspace = true }
2626
thiserror = { workspace = true }
2727
petgraph = { workspace = true }
28+
strum = { workspace = true }
2829

2930
[features]
3031
extension_inference = ["hugr-core/extension_inference"]

hugr-passes/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ mod dead_funcs;
1111
pub use dead_funcs::{remove_dead_funcs, RemoveDeadFuncsError, RemoveDeadFuncsPass};
1212
pub mod force_order;
1313
mod half_node;
14+
pub mod linearize_array;
15+
pub use linearize_array::LinearizeArrayPass;
1416
pub mod lower;
1517
pub mod merge_bbs;
1618
mod monomorphize;

0 commit comments

Comments
 (0)