diff --git a/crates/nodes/src/function/compile.rs b/crates/nodes/src/function/compile.rs index f5d62aa8..d8efa31f 100644 --- a/crates/nodes/src/function/compile.rs +++ b/crates/nodes/src/function/compile.rs @@ -210,7 +210,8 @@ fn mark_literals(program: &mut Program, mut arguments: BTreeSet<&str>) -> super: if let Some(call) = &mut op.call { let bound = call.inputs.iter().zip(&op.inputs).filter(|(_, index)| literal(index)).map(|(name, _)| name.as_str()).collect(); let lanes = mark_literals(&mut call.program, bound); - if !call.exports && lanes.iter().any(Option::is_some) { + // Returned products read the call's evaluated registers after its assignments have run. + if lanes.iter().any(Option::is_some) { returned.insert(index, lanes); } } else { diff --git a/crates/nodes/src/function/product_sum.rs b/crates/nodes/src/function/product_sum.rs index 1ede9edd..cc79e586 100644 --- a/crates/nodes/src/function/product_sum.rs +++ b/crates/nodes/src/function/product_sum.rs @@ -7,10 +7,9 @@ enum Addend { Literal(Value), } -struct Retained { - source: Source, - addend: Addend, - subtract: bool, +enum Retained { + Argument { source: Source, addend: Addend, subtract: bool }, + Lanes(super::returned_product::Sum), } pub(super) struct ProductSum { @@ -25,25 +24,32 @@ pub(super) struct ProductSum { impl ProductSum { /// Evaluate the original call and vector arguments before reading the factors they used. - pub(super) fn returned(inputs: &[usize], product_on_right: bool, source: Source) -> Self { - let addend = Addend::Argument(usize::from(!product_on_right)); - let retained = Retained { source, addend, subtract: false }; - Self { inputs: inputs.to_vec(), product_on_right, negated: false, operation: "mul", literal_divisor: false, retained: Some(Box::new(retained)) } + pub(super) fn returned(inputs: &[usize], terms: super::returned_product::Sum) -> Self { + Self { + inputs: inputs.to_vec(), + product_on_right: false, + negated: false, + operation: "mul", + literal_divisor: false, + retained: Some(Box::new(Retained::Lanes(terms))), + } } pub(super) fn evaluate(&self, arguments: &[Value], subtract: bool, state: &EvaluationState) -> Result { - let subtract = subtract || self.retained.as_ref().is_some_and(|term| term.subtract); - let (a, b, c, blend) = if let Some(term) = &self.retained { - let (a, b) = term.source.factors(state)?; - let c = match &term.addend { + if let Some(Retained::Lanes(terms)) = self.retained.as_deref() { + return super::returned_product::evaluate_sum(terms, arguments, state); + } + let (a, b, c, blend, subtract) = if let Some(Retained::Argument { source, addend, subtract }) = self.retained.as_deref() { + let (a, b) = source.factors(state)?; + let c = match addend { Addend::Argument(i) => arguments.get(*i).ok_or("missing product addend")?, Addend::Literal(value) => value, }; - (a, b, c, None) + (a, b, c, None, *subtract) } else { let [first, second, third, rest @ ..] = arguments else { return Err("missing product-sum operands".into()) }; let (a, b, c) = if self.product_on_right { (second, third, first) } else { (first, second, third) }; - (a, b, c, rest.first()) + (a, b, c, rest.first(), subtract) }; let width = value_type::dimensions(a); if width != value_type::dimensions(c) || (width != value_type::dimensions(b) && !(self.operation == "mulscalar" && value_type::dimensions(b) == 1)) { @@ -114,7 +120,7 @@ fn called_sum(op: &Instruction, instructions: &[Instruction], usage: &[ProductUs return None; } let &[a, b] = product.inputs.as_slice() else { return None }; - let retained = Retained { source: Source::local([a, b]), addend: addend.clone(), subtract: sum.definition.id == "sub" }; + let retained = Retained::Argument { source: Source::local([a, b]), addend: addend.clone(), subtract: sum.definition.id == "sub" }; Some(ProductSum { inputs: op.inputs.clone(), product_on_right, diff --git a/crates/nodes/src/function/returned_product.rs b/crates/nodes/src/function/returned_product.rs index 86f6ed1b..09d89405 100644 --- a/crates/nodes/src/function/returned_product.rs +++ b/crates/nodes/src/function/returned_product.rs @@ -1,17 +1,18 @@ -use super::{EvaluationState, Program, instruction::Instruction, product_sum::ProductSum, value_type}; +use super::{EvaluationState, Program, arithmetic::flush_subnormal, instruction::Instruction, product_sum::ProductSum, value_type}; use materialcraft_core::Value; use std::collections::BTreeMap; -/// Registers of the factors before the scalar product rounded, in the call that evaluated it. +/// Registers and components of the factors in the call that evaluated a product. #[derive(Clone, PartialEq, Eq, PartialOrd, Ord)] pub(super) struct Source { calls: Vec, factors: [usize; 2], + components: [usize; 2], } impl Source { pub(super) fn local(factors: [usize; 2]) -> Self { - Self { calls: Vec::new(), factors } + Self { calls: Vec::new(), factors, components: [0, 0] } } pub(super) fn factors<'a>(&self, mut state: &'a EvaluationState) -> Result<(&'a Value, &'a Value), String> { @@ -24,6 +25,37 @@ impl Source { } } +pub(super) struct Term { + source: Source, + on_right: bool, +} + +pub(super) type Sum = [Option; 4]; + +pub(super) fn evaluate_sum(terms: &Sum, arguments: &[Value], state: &EvaluationState) -> Result { + let [left, right] = arguments else { return Err("missing addition operands".into()) }; + let width = value_type::dimensions(left); + if width != value_type::dimensions(right) { + return Err("float operands must have the same dimensions".into()); + } + let left = value_type::floats(left)?.map(flush_subnormal); + let right = value_type::floats(right)?.map(flush_subnormal); + let mut result = [0.0; 4]; + for (((output, term), left), right) in result.iter_mut().zip(terms).zip(left).zip(right).take(width) { + let sum = if let Some(term) = term { + let (a, b) = term.source.factors(state)?; + let [i, j] = term.source.components; + let a = *value_type::floats(a)?.get(i).ok_or("invalid product component")?; + let b = *value_type::floats(b)?.get(j).ok_or("invalid product component")?; + flush_subnormal(a).mul_add(flush_subnormal(b), if term.on_right { left } else { right }) + } else { + left + right + }; + *output = flush_subnormal(sum); + } + value_type::intermediate_float_value(result.get(..width).ok_or("invalid addition dimensions")?) +} + pub(super) type Lanes = [Option; 4]; type Origins = [Option; 4]; @@ -66,7 +98,8 @@ pub(super) fn identify(program: &mut Program, mut returned: BTreeMap 0 && !op.literal)) { return empty(); } @@ -85,25 +118,28 @@ pub(super) fn identify(program: &mut Program, mut returned: BTreeMap 0 + && op.pixel_product_sum.is_none() + && let &[left, right] = op.inputs.as_slice() + { + let sides = [left, right].map(|i| origins.get(i).copied().unwrap_or([None; 4])); + if sides.iter().flatten().any(Option::is_some) { + sums.push((index, sides)); } } let mut lanes = [None; 4]; - if let ("mul", 256, false, &[a, b]) = (op.definition.id, op.type_code, op.literal, op.inputs.as_slice()) { - lanes[0] = Some(products.len()); - products.push((Source::local([a, b]), 0)); + if matches!(op.definition.id, "mul" | "mulscalar") + && !op.literal + && let &[a, b] = op.inputs.as_slice() + { + for (i, lane) in lanes.iter_mut().take(width).enumerate() { + *lane = Some(products.len()); + let j = if op.definition.id == "mulscalar" { 0 } else { i }; + products.push((Source { components: [i, j], ..Source::local([a, b]) }, 0)); + } } else if let Some(exports) = returned.remove(&index) { // Two returned lanes can reference the same product. Count their downstream reads together. let mut distinct = BTreeMap::new(); @@ -131,11 +167,19 @@ pub(super) fn identify(program: &mut Program, mut returned: BTreeMap