From 100ddcf055a28f281e8fb19b70a6079a82ded87f Mon Sep 17 00:00:00 2001 From: chasse20 Date: Sun, 22 Nov 2026 10:18:32 -0500 Subject: [PATCH] Preserve weighted component products through loop accumulation --- crates/nodes/src/function/returned_product.rs | 30 ++++++++++-- crates/nodes/src/function/tests.rs | 46 +++++++++++++++++++ docs/parity-fixes.md | 41 +++++++++++++++++ docs/sbs-root-nodes.md | 4 +- 4 files changed, 114 insertions(+), 7 deletions(-) diff --git a/crates/nodes/src/function/returned_product.rs b/crates/nodes/src/function/returned_product.rs index 3a277d30..efd4380b 100644 --- a/crates/nodes/src/function/returned_product.rs +++ b/crates/nodes/src/function/returned_product.rs @@ -22,6 +22,26 @@ impl Source { Self { calls: Vec::new(), factors, components: [0, 0], operation: Operation::Multiply } } + fn live_for_sum(&self, program: &Program, loops: &[usize], sum: usize) -> bool { + if loops.is_empty() { + return true; + } + if !self.calls.is_empty() { + return false; + } + let mut inside = false; + for &index in loops { + let members = &program.instructions[index].control_registers; + if members.binary_search(&sum).is_ok() { + inside = true; + } else if self.factors.iter().any(|factor| members.binary_search(factor).is_ok()) { + // A separate loop can clear a factor while the packed vector still holds its old value. + return false; + } + } + inside + } + pub(super) fn factors<'a>(&self, mut state: &'a EvaluationState) -> Result<(&'a Value, &'a Value), String> { for call in &self.calls { state = state.calls.get(call).ok_or("missing evaluated product call")?; @@ -127,13 +147,11 @@ pub(super) fn forwarded(op: &Instruction, instructions: &[Instruction], pub(super) fn identify(program: &mut Program, mut returned: BTreeMap) -> Lanes { let empty = || std::array::from_fn(|_| None); - // Loop iterations invalidate individual registers. Only retain factors for straight-line lifetimes. - if program.instructions.iter().any(|op| op.definition.id == "while") - || (returned.is_empty() && !program.instructions.iter().any(|op| op.product_operation().is_some() && float_width(op.type_code) > 0 && !op.literal)) - { + if returned.is_empty() && !program.instructions.iter().any(|op| op.product_operation().is_some() && float_width(op.type_code) > 0 && !op.literal) { return empty(); } + let loops: Vec<_> = program.instructions.iter().enumerate().filter_map(|(i, op)| (op.definition.id == "while").then_some(i)).collect(); let mut origins: Vec = Vec::with_capacity(program.instructions.len()); let mut products: Vec<(Source, usize)> = Vec::new(); let mut sums = Vec::new(); @@ -193,6 +211,8 @@ pub(super) fn identify(program: &mut Program, mut returned: BTreeMap