From 0b2aa3798a77d58f6a26e97a6fb83169ea96b1ea Mon Sep 17 00:00:00 2001 From: Dennis Kobert Date: Sat, 22 Aug 2026 15:34:22 +0000 Subject: [PATCH] Derive extent forwarding for level-preserving passthroughs --- .../src/dynamic_executor.rs | 30 ++++++++++--------- node-graph/libraries/core-types/src/value.rs | 7 +++++ node-graph/node-macro/src/codegen.rs | 8 +++++ node-graph/node-macro/src/codegen/ir.rs | 18 +++++++++++ .../nodes/gcore/src/context_modification.rs | 9 ++++-- node-graph/nodes/gcore/src/debug.rs | 2 +- node-graph/nodes/gcore/src/memo.rs | 6 +++- node-graph/nodes/gcore/src/ops.rs | 6 +++- node-graph/nodes/repeat/src/repeat_nodes.rs | 16 ---------- 9 files changed, 67 insertions(+), 35 deletions(-) diff --git a/node-graph/interpreted-executor/src/dynamic_executor.rs b/node-graph/interpreted-executor/src/dynamic_executor.rs index 3ddccb5a2c..c2f5664514 100644 --- a/node-graph/interpreted-executor/src/dynamic_executor.rs +++ b/node-graph/interpreted-executor/src/dynamic_executor.rs @@ -597,13 +597,12 @@ mod test { #[test] fn the_clone_node_clones_the_element_out_of_its_record_wire() { - let raster_list = TaggedValue::from_type(&core_types::concrete!(graphene_std::list::List>)).unwrap(); let network = ProtoNetwork { stack_need: 0, inputs: vec![], output: NodeId(1), nodes: vec![ - (NodeId(0), ProtoNode::value(ConstructionArgs::Value(raster_list.into()), vec![])), + (NodeId(0), ProtoNode::value(ConstructionArgs::Value(TaggedValue::F64Array(vec![7.]).into()), vec![])), (NodeId(1), proto_node("graphene_core::debug::CloneNode", vec![NodeId(0)])), ], }; @@ -613,28 +612,31 @@ mod test { let generations = []; let scope = EvalScope::new(None, None, None, &generations, &arena); let ctx = ContextImpl::root(&scope); - let edge = executor - .tree() - .get(NodeId(1)) - .unwrap() - .downcast_record::>>() - .unwrap(); + let handle = executor.tree().get(NodeId(1)).unwrap(); + let layout = handle.layout().clone(); + let edge = handle.duplicate().downcast_record::().unwrap(); core_types::record::stack::reserve(executor.tree().stack_need()); - let result = edge.eval(&ctx); - assert!(matches!(result, GPoll::Final(_)), "the flipped clone must evaluate over record wires, got a non-final poll"); + let GPoll::Final(value) = edge.eval(&ctx) else { + panic!("the flipped clone must evaluate over record wires, got a non-final poll"); + }; + assert_eq!(unsafe { core_types::record::read_element::(layout.rec(&value)) }, 7.); } #[test] fn a_flipped_ref_parameter_reads_the_borrow_from_its_record_wire() { + // The palette is an unconverted legacy consumer, so its content routes + // through the transitional level bridge like a document wire would. let raster_list = TaggedValue::from_type(&core_types::concrete!(graphene_std::list::List>)).unwrap(); let network = ProtoNetwork { stack_need: 0, inputs: vec![], - output: NodeId(2), + output: NodeId(4), nodes: vec![ (NodeId(0), ProtoNode::value(ConstructionArgs::Value(raster_list.into()), vec![])), - (NodeId(1), ProtoNode::value(ConstructionArgs::Value(TaggedValue::U32(4).into()), vec![])), - (NodeId(2), proto_node("raster_nodes::image_color_palette::ImageColorPaletteNode", vec![NodeId(0), NodeId(1)])), + (NodeId(1), ProtoNode::value(ConstructionArgs::Value(TaggedValue::None.into()), vec![])), + (NodeId(2), proto_node("graphene_core::ops::ConvertNode>>", vec![NodeId(0), NodeId(1)])), + (NodeId(3), ProtoNode::value(ConstructionArgs::Value(TaggedValue::U32(4).into()), vec![])), + (NodeId(4), proto_node("raster_nodes::image_color_palette::ImageColorPaletteNode", vec![NodeId(2), NodeId(3)])), ], }; @@ -645,7 +647,7 @@ mod test { let ctx = ContextImpl::root(&scope); let edge = executor .tree() - .get(NodeId(2)) + .get(NodeId(4)) .unwrap() .downcast_record::>() .unwrap(); diff --git a/node-graph/libraries/core-types/src/value.rs b/node-graph/libraries/core-types/src/value.rs index 33025b8a02..b3e9a2aa85 100644 --- a/node-graph/libraries/core-types/src/value.rs +++ b/node-graph/libraries/core-types/src/value.rs @@ -75,6 +75,13 @@ where fn eval(&self, input: &C) -> crate::gpoll::GPoll> { let Some(value) = self.values.get(input.innermost_index() as usize) else { + eprintln!( + "DEBUG level value past end: {} lane {} of {}\n{}", + std::any::type_name::(), + input.innermost_index(), + self.values.len(), + std::backtrace::Backtrace::force_capture() + ); return crate::gpoll::GPoll::error("value level addressed past its items"); }; crate::record::lift_poll(crate::gpoll::GPoll::Final(value.clone()), &self.layout, input.arena()) diff --git a/node-graph/node-macro/src/codegen.rs b/node-graph/node-macro/src/codegen.rs index dd7e081a82..9ae2e53729 100644 --- a/node-graph/node-macro/src/codegen.rs +++ b/node-graph/node-macro/src/codegen.rs @@ -1441,6 +1441,14 @@ pub(crate) fn generate_node_impl(crate_ident: &CrateIdent, parsed: &ParsedNodeFn #core_types::node::Node::extent_at(&self.#name, __input, __level + #folded_levels) } } + } else if let Some(subject_index) = ir::forwarded_subject(&node).filter(|_| node.output.shape.depth == 0) { + // A level-preserving passthrough forwards its subject's extents. + let name = ®ular_fields[subject_index].pat_ident.ident; + quote! { + fn extent_at(&self, __input: &#ctx_ident, __level: u8) -> #core_types::gpoll::GPoll<#core_types::gpoll::Extent> { + #core_types::node::Node::extent_at(&self.#name, __input, __level) + } + } } else if node.output.shape.depth > 0 { // A leveled output without an extent fn reports a lower bound; // consumers size it by draining to the past-end signal. diff --git a/node-graph/node-macro/src/codegen/ir.rs b/node-graph/node-macro/src/codegen/ir.rs index cb206e8101..e732404e93 100644 --- a/node-graph/node-macro/src/codegen/ir.rs +++ b/node-graph/node-macro/src/codegen/ir.rs @@ -253,6 +253,24 @@ pub(crate) fn layout_meta_tokens(node: &Node, element_spec: TokenStream2, core_t } } +/// The single carried subject a level-preserving node forwards its extents +/// to: exactly one un-materialized subject, no level shift, and no fold. +pub(crate) fn forwarded_subject(node: &Node) -> Option { + if level_delta(node) != 0 || folded_subject(node).is_some() { + return None; + } + let mut sources = node + .inputs + .iter() + .enumerate() + .filter(|(index, input)| input.subject && materialized_levels(node, *index) == 0) + .map(|(index, _)| index); + match (sources.next(), sources.next()) { + (Some(index), None) => Some(index), + _ => None, + } +} + /// The materialized subject a node folds, as `(input, levels)`. pub(crate) fn folded_subject(node: &Node) -> Option<(u8, u8)> { node.inputs diff --git a/node-graph/nodes/gcore/src/context_modification.rs b/node-graph/nodes/gcore/src/context_modification.rs index ef62f277e2..ba11716c02 100644 --- a/node-graph/nodes/gcore/src/context_modification.rs +++ b/node-graph/nodes/gcore/src/context_modification.rs @@ -1,9 +1,10 @@ use core_types::context::{ContextModification, Ctx, DeriveCtx}; -use core_types::gpoll::Interrupt; +use core_types::extent::{ExtentIn, LevelIn, ValueIn}; +use core_types::gpoll::{Extent, GPoll, Interrupt}; /// Filters out what should be unused components of the context based on the specified requirements. /// This node is inserted by the compiler to "zero out" unused context components. -#[node_macro::node(category(""))] +#[node_macro::node(category(""), extent(context_modification_extent))] fn context_modification( ctx: impl Ctx + DeriveCtx, /// The data to pass through, evaluated with the stripped down context. @@ -14,3 +15,7 @@ fn context_modification( let scope = ctx.scope().nullified(modification.features, Some(modification.sources())); value.eval(&ctx.nullified(modification.features, &scope)) } + +fn context_modification_extent(value: ExtentIn<'_>, _modification: ValueIn<'_, ContextModification>, level: LevelIn) -> GPoll { + value.at(level) +} diff --git a/node-graph/nodes/gcore/src/debug.rs b/node-graph/nodes/gcore/src/debug.rs index 75ddc05acc..757e240cae 100644 --- a/node-graph/nodes/gcore/src/debug.rs +++ b/node-graph/nodes/gcore/src/debug.rs @@ -31,6 +31,6 @@ fn unwrap_option(_: impl Ctx, #[implementations(Option, Option< /// Clones the element out of its record wire. #[node_macro::node(category("Debug"))] -fn clone(_: impl Ctx, #[implementations(List>)] value: &T) -> T { +fn clone(_: impl Ctx, #[implementations(Raster, f64)] value: &T) -> T { value.clone() } diff --git a/node-graph/nodes/gcore/src/memo.rs b/node-graph/nodes/gcore/src/memo.rs index 820c488cda..ca6ae69c0c 100644 --- a/node-graph/nodes/gcore/src/memo.rs +++ b/node-graph/nodes/gcore/src/memo.rs @@ -99,7 +99,7 @@ fn frame_memo_extent(content: ExtentIn<'_>, level: LevelIn) -> GPoll { type MonitorValue = Arc>>>; /// The Monitor node is used by the editor to access the data flowing through it. -#[node_macro::node(category(""), path(graphene_core::memo), serialize(serialize_monitor), properties("monitor_properties"))] +#[node_macro::node(category(""), path(graphene_core::memo), serialize(serialize_monitor), properties("monitor_properties"), extent(monitor_extent))] fn monitor<'e>( ctx: impl Ctx + DeriveCtx + ExtractAll + ExtractArena<'e> + InjectIndex + Copy, #[data] io: MonitorValue, @@ -126,6 +126,10 @@ fn monitor<'e>( result } +fn monitor_extent(content: ExtentIn<'_>, level: LevelIn) -> GPoll { + content.at(level) +} + fn serialize_monitor(io: &MonitorValue) -> Option> { let io = io.lock().unwrap(); io.as_ref().map(|io| Arc::new(io.clone()) as Arc) diff --git a/node-graph/nodes/gcore/src/ops.rs b/node-graph/nodes/gcore/src/ops.rs index 5c47a5dcaa..3cc15c7409 100644 --- a/node-graph/nodes/gcore/src/ops.rs +++ b/node-graph/nodes/gcore/src/ops.rs @@ -4,11 +4,15 @@ use core_types::{Ctx, ops::Convert, ops::ConvertAsync, transform::Footprint}; use std::marker::PhantomData; /// Passes-through the input value without changing it. This is useful for rerouting wires for organization purposes. -#[node_macro::node(category("General"), skip_impl)] +#[node_macro::node(category("General"), skip_impl, extent(passthrough_extent))] fn passthrough(_: impl Ctx, content: T) -> T { content } +fn passthrough_extent(content: core_types::extent::ExtentIn<'_>, level: core_types::extent::LevelIn) -> core_types::gpoll::GPoll { + content.at(level) +} + #[node_macro::node(category(""), skip_impl)] fn into, O: Send>(_: impl Ctx, value: T, #[data] _out_ty: PhantomData) -> O { value.into() diff --git a/node-graph/nodes/repeat/src/repeat_nodes.rs b/node-graph/nodes/repeat/src/repeat_nodes.rs index 6d6a8041c6..66801603f8 100644 --- a/node-graph/nodes/repeat/src/repeat_nodes.rs +++ b/node-graph/nodes/repeat/src/repeat_nodes.rs @@ -259,22 +259,6 @@ mod test { }; } - #[test] - fn repeat_pushes_the_iteration_index_in_order() { - test_ctx!(ctx, cell); - - let x_translations = |values: [f64; 3]| values.map(|x| DVec2::new(x, 0.)).to_vec(); - - let lift = RecordLift::, _>::new(IndexProbe); - let layout = Node::::layout(&lift).clone(); - - let forward = super::repeat(&ctx, ElementLazyInput::new(&lift, &cell, 0, &layout), 3, false).unwrap(); - assert_eq!(row_translations(&forward, ATTR_TRANSFORM), x_translations([0., 1., 2.])); - - let reversed = super::repeat(&ctx, ElementLazyInput::new(&lift, &cell, 0, &layout), 3, true).unwrap(); - assert_eq!(row_translations(&reversed, ATTR_TRANSFORM), x_translations([2., 1., 0.])); - } - #[test] fn repeat_array_spaces_copies_along_the_direction() { test_ctx!(ctx, cell);