Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 4 additions & 4 deletions hugr-passes/src/linearize_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ impl LinearizeArrayPass {

#[cfg(test)]
mod test {
use hugr_core::builder::{FunctionBuilder, ModuleBuilder};
use hugr_core::builder::ModuleBuilder;
use hugr_core::extension::prelude::{ConstUsize, Noop};
use hugr_core::ops::handle::NodeHandle;
use hugr_core::ops::{Const, OpType};
Expand Down Expand Up @@ -287,7 +287,7 @@ mod test {
),
};
let sig = Signature::new(src, tgt);
let mut builder = FunctionBuilder::new("main", sig).unwrap();
let mut builder = DFGBuilder::new(sig).unwrap();
let [arr] = builder.input_wires_arr();
let op: OpType = match dir {
INTO => VArrayToArray::new(elem_ty.clone(), size).into(),
Expand All @@ -313,7 +313,7 @@ mod test {
#[case(value_array_type(2, Type::new_tuple(vec![usize_t(), value_array_type(4, usize_t())])))]
fn implicit_clone(#[case] array_ty: Type) {
let sig = Signature::new(array_ty.clone(), vec![array_ty; 2]);
let mut builder = FunctionBuilder::new("main", sig).unwrap();
let mut builder = DFGBuilder::new(sig).unwrap();
let [arr] = builder.input_wires_arr();
builder.set_outputs(vec![arr, arr]).unwrap();

Expand All @@ -329,7 +329,7 @@ mod test {
#[case(value_array_type(2, Type::new_tuple(vec![usize_t(), value_array_type(4, usize_t())])))]
fn implicit_discard(#[case] array_ty: Type) {
let sig = Signature::new(array_ty, Type::EMPTY_TYPEROW);
let mut builder = FunctionBuilder::new("main", sig).unwrap();
let mut builder = DFGBuilder::new(sig).unwrap();
builder.set_outputs(vec![]).unwrap();

let mut hugr = builder.finish_hugr().unwrap();
Expand Down
32 changes: 26 additions & 6 deletions hugr-passes/src/replace_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ pub struct ReplaceTypes {
ParametricType,
Arc<dyn Fn(&OpaqueValue, &ReplaceTypes) -> Result<Option<Value>, ReplaceTypesError>>,
>,
regions: Option<Vec<Node>>,
}

impl Default for ReplaceTypes {
Expand Down Expand Up @@ -298,6 +299,7 @@ impl ReplaceTypes {
param_ops: Default::default(),
consts: Default::default(),
param_consts: Default::default(),
regions: None,
}
}

Expand Down Expand Up @@ -435,6 +437,14 @@ impl ReplaceTypes {
self.param_consts.insert(src_ty.into(), Arc::new(const_fn));
}

/// Set the regions of the Hugr to which this pass should be applied.
///
/// If not set, the pass is applied to the whole Hugr.
/// Each call to overwrites any previous calls to `set_regions`.
pub fn set_regions(&mut self, regions: impl IntoIterator<Item = Node>) {
Copy link
Member

Choose a reason for hiding this comment

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

chaining with_regions might be better?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OIC - as in with_regions(mut self, regions....) -> Self. Yeah, but this is more consistent with the other config methods.

Thought you meant the behaviour where multiple calls override rather than accumulate....accumulation leads to the first call being special, i.e. replacing the default of "the module root"

self.regions = Some(regions.into_iter().collect());
}

fn change_node(
&self,
hugr: &mut impl HugrMut<Node = Node>,
Expand Down Expand Up @@ -600,11 +610,21 @@ impl<H: HugrMut<Node = Node>> ComposablePass<H> for ReplaceTypes {
type Result = bool;

fn run(&self, hugr: &mut H) -> Result<bool, ReplaceTypesError> {
let temp: Vec<Node>; // keep alive
let regions = match self.regions {
Some(ref regs) => regs,
None => {
temp = vec![hugr.module_root()];
&temp
}
};
let mut changed = false;
for n in hugr.entry_descendants().collect::<Vec<_>>() {
changed |= self.change_node(hugr, n)?;
if n != hugr.entrypoint() && changed {
self.linearize_outputs(hugr, n)?;
for region_root in regions {
for n in hugr.descendants(*region_root).collect::<Vec<_>>() {
changed |= self.change_node(hugr, n)?;
if n != hugr.entrypoint() && changed {
self.linearize_outputs(hugr, n)?;
}
}
}
Ok(changed)
Expand Down Expand Up @@ -1117,8 +1137,8 @@ mod test {
where
GenericArrayValue<AK>: CustomConst,
{
let sig = inout_sig(type_row![], AK::ty(vals.len() as _, usize_t()));
let mut dfb = FunctionBuilder::new("main", sig).unwrap();
let mut dfb =
DFGBuilder::new(inout_sig(type_row![], AK::ty(vals.len() as _, usize_t()))).unwrap();
let c = dfb.add_load_value(GenericArrayValue::<AK>::new(
usize_t(),
vals.iter().map(|u| ConstUsize::new(*u).into()),
Expand Down
4 changes: 2 additions & 2 deletions hugr-passes/src/replace_types/linearize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ mod test {

use hugr_core::builder::{
BuildError, Container, DFGBuilder, Dataflow, DataflowHugr, DataflowSubContainer,
FunctionBuilder, HugrBuilder, inout_sig,
HugrBuilder, inout_sig,
};

use hugr_core::extension::prelude::{option_type, qb_t, usize_t};
Expand Down Expand Up @@ -912,7 +912,7 @@ mod test {
);

let build_hugr = |ty: Type| {
let mut dfb = FunctionBuilder::new("main", Signature::new(ty.clone(), vec![])).unwrap();
let mut dfb = DFGBuilder::new(Signature::new(ty.clone(), vec![])).unwrap();
let [inp] = dfb.input_wires_arr();
let drop_op = drop_ext
.instantiate_extension_op("drop", [ty.into()])
Expand Down
Loading