Skip to content

Commit

Permalink
fix all warnings (inc Machine::new() -> impl Default)
Browse files Browse the repository at this point in the history
  • Loading branch information
acl-cqc committed Sep 2, 2024
1 parent 7ffa179 commit 6cac41a
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 52 deletions.
5 changes: 1 addition & 4 deletions hugr-passes/src/const_fold2/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,7 @@ impl<H: HugrView> TotalContext<ValueHandle> for HugrValueContext<H> {
)]
}
OpType::ExtensionOp(op) => {
let ins = ins
.into_iter()
.map(|(p, v)| (*p, v.clone()))
.collect::<Vec<_>>();
let ins = ins.iter().map(|(p, v)| (*p, v.clone())).collect::<Vec<_>>();
op.constant_fold(&ins).map_or(Vec::new(), |outs| {
outs.into_iter()
.map(|(p, v)| (p, ValueHandle::new(ValueKey::Node(n), Arc::new(v))))
Expand Down
29 changes: 18 additions & 11 deletions hugr-passes/src/dataflow/datalog.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
#![allow(
clippy::clone_on_copy,
clippy::unused_enumerate_index,
clippy::collapsible_if
)]

use ascent::lattice::BoundedLattice;
use hugr_core::extension::prelude::{MakeTuple, UnpackTuple};
use std::collections::HashMap;
Expand Down Expand Up @@ -149,18 +155,17 @@ fn propagate_leaf_op<V: AbstractValue>(
// Handle basics here. I guess (given the current interface) we could allow
// DFContext to handle these but at the least we'd want these impls to be
// easily available for reuse.
op if op.cast::<MakeTuple>().is_some() => Some(ValueRow::from_iter([PV::variant(
0,
ins.into_iter().cloned(),
)])),
op if op.cast::<MakeTuple>().is_some() => {
Some(ValueRow::from_iter([PV::variant(0, ins.iter().cloned())]))
}
op if op.cast::<UnpackTuple>().is_some() => {
let [tup] = ins.into_iter().collect::<Vec<_>>().try_into().unwrap();
let [tup] = ins.iter().collect::<Vec<_>>().try_into().unwrap();
tup.variant_values(0, utils::value_outputs(c.as_ref(), n).count())
.map(ValueRow::from_iter)
}
OpType::Tag(t) => Some(ValueRow::from_iter([PV::variant(
t.tag,
ins.into_iter().cloned(),
ins.iter().cloned(),
)])),
OpType::Input(_) | OpType::Output(_) => None, // handled by parent
// It'd be nice to convert these to [(IncomingPort, Value)] to pass to the context,
Expand All @@ -170,22 +175,24 @@ fn propagate_leaf_op<V: AbstractValue>(
}
}

// TODO This should probably be called 'Analyser' or something
pub struct Machine<V: AbstractValue, C: DFContext<V>>(
AscentProgram<V, C>,
Option<HashMap<Wire, PV<V>>>,
);

/// derived-Default requires the context to be Defaultable, which is unnecessary
impl<V: AbstractValue, C: DFContext<V>> Default for Machine<V, C> {
fn default() -> Self {
Self(Default::default(), None)
}
}

/// Usage:
/// 1. [Self::new()]
/// 2. Zero or more [Self::propolutate_out_wires] with initial values
/// 3. Exactly one [Self::run] to do the analysis
/// 4. Results then available via [Self::read_out_wire_partial_value] and [Self::read_out_wire_value]
impl<V: AbstractValue, C: DFContext<V>> Machine<V, C> {
pub fn new() -> Self {
Self(Default::default(), None)
}

pub fn propolutate_out_wires(&mut self, wires: impl IntoIterator<Item = (Wire, PV<V>)>) {
assert!(self.1.is_none());
self.0
Expand Down
20 changes: 10 additions & 10 deletions hugr-passes/src/dataflow/datalog/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fn test_make_tuple() {
let v3 = builder.make_tuple([v1, v2]).unwrap();
let hugr = builder.finish_hugr(&EMPTY_REG).unwrap();

let mut machine = Machine::new();
let mut machine = Machine::default();
machine.run(HugrValueContext::new(&hugr));

let x = machine.read_out_wire_value(&hugr, v3).unwrap();
Expand All @@ -41,7 +41,7 @@ fn test_unpack_tuple() {
.outputs_arr();
let hugr = builder.finish_hugr(&EMPTY_REG).unwrap();

let mut machine = Machine::new();
let mut machine = Machine::default();
machine.run(HugrValueContext::new(&hugr));

let o1_r = machine.read_out_wire_value(&hugr, o1).unwrap();
Expand All @@ -60,7 +60,7 @@ fn test_unpack_const() {
.outputs_arr();
let hugr = builder.finish_hugr(&EMPTY_REG).unwrap();

let mut machine = Machine::new();
let mut machine = Machine::default();
machine.run(HugrValueContext::new(&hugr));

let o_r = machine.read_out_wire_value(&hugr, o).unwrap();
Expand All @@ -86,7 +86,7 @@ fn test_tail_loop_never_iterates() {
let [tl_o] = tail_loop.outputs_arr();
let hugr = builder.finish_hugr(&EMPTY_REG).unwrap();

let mut machine = Machine::new();
let mut machine = Machine::default();
machine.run(HugrValueContext::new(&hugr));
// dbg!(&machine.tail_loop_io_node);
// dbg!(&machine.out_wire_value);
Expand Down Expand Up @@ -118,7 +118,7 @@ fn test_tail_loop_always_iterates() {
let [tl_o1, tl_o2] = tail_loop.outputs_arr();
let hugr = builder.finish_hugr(&EMPTY_REG).unwrap();

let mut machine = Machine::new();
let mut machine = Machine::default();
machine.run(HugrValueContext::new(&hugr));

let o_r1 = machine.read_out_wire_partial_value(tl_o1).unwrap();
Expand Down Expand Up @@ -168,15 +168,15 @@ fn test_tail_loop_iterates_twice() {
// we should be able to propagate their values
let [o_w1, o_w2] = tail_loop.outputs_arr();

let mut machine = Machine::new();
let mut machine = Machine::default();
machine.run(HugrValueContext::new(&hugr));
// dbg!(&machine.tail_loop_io_node);
// dbg!(&machine.out_wire_value);

// TODO these hould be the propagated values for now they will bt join(true,false)
let o_r1 = machine.read_out_wire_partial_value(o_w1).unwrap();
let _ = machine.read_out_wire_partial_value(o_w1).unwrap();
// assert_eq!(o_r1, PartialValue::top());
let o_r2 = machine.read_out_wire_partial_value(o_w2).unwrap();
let _ = machine.read_out_wire_partial_value(o_w2).unwrap();
// assert_eq!(o_r2, Value::true_val());
assert_eq!(
TailLoopTermination::Top,
Expand Down Expand Up @@ -212,7 +212,7 @@ fn conditional() {
let case2 = case2_b.finish_with_outputs([false_w, c2a]).unwrap();

let case3_b = cond_builder.case_builder(2).unwrap();
let [c3_1, c3_2] = case3_b.input_wires_arr();
let [c3_1, _c3_2] = case3_b.input_wires_arr();
let case3 = case3_b.finish_with_outputs([c3_1, false_w]).unwrap();

let cond = cond_builder.finish_sub_container().unwrap();
Expand All @@ -221,7 +221,7 @@ fn conditional() {

let hugr = builder.finish_hugr(&EMPTY_REG).unwrap();

let mut machine = Machine::new();
let mut machine = Machine::default();
let arg_pv =
PartialValue::variant(1, []).join(PartialValue::variant(2, [PartialValue::variant(0, [])]));
machine.propolutate_out_wires([(arg_w, arg_pv.into())]);
Expand Down
34 changes: 10 additions & 24 deletions hugr-passes/src/dataflow/partial_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ impl<V: PartialEq> PartialOrd for PartialSum<V> {
return None;
}
for (k, lhs) in &self.0 {
let Some(rhs) = other.0.get(&k) else {
let Some(rhs) = other.0.get(k) else {
unreachable!()
};
match lhs.partial_cmp(rhs) {
Expand Down Expand Up @@ -192,8 +192,10 @@ impl<V: AbstractValue> PartialValue<V> {
self.assert_invariants();
match &*self {
Self::Top => return false,
Self::Value(v) if v == &vh => return false,
Self::Value(v) => {
if v == &vh {
return false;
};
*self = Self::Top;
}
Self::PartialSum(_) => match vh.into() {
Expand Down Expand Up @@ -277,7 +279,7 @@ impl<V: AbstractValue> PartialValue<V> {
impl<V: AbstractValue> Lattice for PartialValue<V> {
fn join_mut(&mut self, other: Self) -> bool {
// println!("join {self:?}\n{:?}", &other);
let changed = match (&*self, other) {
match (&*self, other) {
(Self::Top, _) => false,
(_, other @ Self::Top) => {
*self = other;
Expand Down Expand Up @@ -316,15 +318,7 @@ impl<V: AbstractValue> Lattice for PartialValue<V> {
self.join_mut_value_handle(old_self)
}
(_, Self::Value(h)) => self.join_mut_value_handle(h),
// (new_self, _) => {
// **new_self = Self::Top;
// false
// }
};
// if changed {
// println!("join new self: {:?}", s);
// }
changed
}
}

fn meet(mut self, other: Self) -> Self {
Expand All @@ -333,7 +327,7 @@ impl<V: AbstractValue> Lattice for PartialValue<V> {
}

fn meet_mut(&mut self, other: Self) -> bool {
let changed = match (&*self, other) {
match (&*self, other) {
(Self::Bottom, _) => false,
(_, other @ Self::Bottom) => {
*self = other;
Expand Down Expand Up @@ -372,15 +366,7 @@ impl<V: AbstractValue> Lattice for PartialValue<V> {
self.meet_mut_value_handle(old_self)
}
(Self::PartialSum(_), Self::Value(h)) => self.meet_mut_value_handle(h),
// (new_self, _) => {
// **new_self = Self::Bottom;
// false
// }
};
// if changed {
// println!("join new self: {:?}", s);
// }
changed
}
}
}

Expand Down Expand Up @@ -519,8 +505,7 @@ mod test {
}

impl TestSumType {
const UNIT: TestSumLeafType = TestSumLeafType::Unit;

#[allow(unused)] // ALAN ?
fn leaf(v: Type) -> Self {
TestSumType::Leaf(TestSumLeafType::Int(v))
}
Expand All @@ -543,6 +528,7 @@ mod test {
}
}

#[allow(unused)] // ALAN ?
fn is_leaf(&self) -> bool {
self.depth() == 0
}
Expand Down
6 changes: 3 additions & 3 deletions hugr-passes/src/dataflow/total_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ impl<V: AbstractValue> PartialSum<V> {
if v.len() != r.len() {
return Err(self);
}
match zip_eq(v.into_iter(), r.into_iter())
match zip_eq(v, r.iter())
.map(|(v, t)| v.clone().try_into_value(t))
.collect::<Result<Vec<_>, _>>()
{
Ok(vs) => V2::try_new_sum(*k, vs, &st).map_err(|_| self),
Ok(vs) => V2::try_new_sum(*k, vs, st).map_err(|_| self),
Err(_) => Err(self),
}
}
Expand All @@ -90,7 +90,7 @@ impl<V: AbstractValue, T: TotalContext<V>> DFContext<V> for T {
let sig = op.dataflow_signature()?;
let known_ins = sig
.input_types()
.into_iter()
.iter()
.enumerate()
.zip(ins.iter())
.filter_map(|((i, ty), pv)| {
Expand Down

0 comments on commit 6cac41a

Please sign in to comment.