Derive extent forwarding for level-preserving passthroughs

This commit is contained in:
Dennis Kobert
2026-08-22 15:34:22 +00:00
parent 13cd718c90
commit 0b2aa3798a
9 changed files with 67 additions and 35 deletions

View File

@@ -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<graphene_std::raster_types::Raster<graphene_std::raster_types::CPU>>)).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::<graphene_std::list::List<graphene_std::raster_types::Raster<graphene_std::raster_types::CPU>>>()
.unwrap();
let handle = executor.tree().get(NodeId(1)).unwrap();
let layout = handle.layout().clone();
let edge = handle.duplicate().downcast_record::<f64>().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::<f64>(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<graphene_std::raster_types::Raster<graphene_std::raster_types::CPU>>)).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<List<Raster<CPU>>>", 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::<graphene_std::list::List<graphene_std::raster::color::Color>>()
.unwrap();

View File

@@ -75,6 +75,13 @@ where
fn eval(&self, input: &C) -> crate::gpoll::GPoll<crate::record::RecordValue<'e>> {
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::<T>(),
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())

View File

@@ -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 = &regular_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.

View File

@@ -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<usize> {
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

View File

@@ -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<T>(
ctx: impl Ctx + DeriveCtx,
/// The data to pass through, evaluated with the stripped down context.
@@ -14,3 +15,7 @@ fn context_modification<T>(
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<Extent> {
value.at(level)
}

View File

@@ -31,6 +31,6 @@ fn unwrap_option<T: Default>(_: impl Ctx, #[implementations(Option<f64>, Option<
/// Clones the element out of its record wire.
#[node_macro::node(category("Debug"))]
fn clone<T: Clone>(_: impl Ctx, #[implementations(List<Raster<CPU>>)] value: &T) -> T {
fn clone<T: Clone>(_: impl Ctx, #[implementations(Raster<CPU>, f64)] value: &T) -> T {
value.clone()
}

View File

@@ -99,7 +99,7 @@ fn frame_memo_extent(content: ExtentIn<'_>, level: LevelIn) -> GPoll<Extent> {
type MonitorValue = Arc<Mutex<Option<IORecord<CtxSnapshot, RecordCapture>>>>;
/// 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<Extent> {
content.at(level)
}
fn serialize_monitor(io: &MonitorValue) -> Option<Arc<dyn std::any::Any + Send + Sync>> {
let io = io.lock().unwrap();
io.as_ref().map(|io| Arc::new(io.clone()) as Arc<dyn std::any::Any + Send + Sync>)

View File

@@ -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<T: Send>(_: impl Ctx, content: T) -> T {
content
}
fn passthrough_extent(content: core_types::extent::ExtentIn<'_>, level: core_types::extent::LevelIn) -> core_types::gpoll::GPoll<core_types::gpoll::Extent> {
content.at(level)
}
#[node_macro::node(category(""), skip_impl)]
fn into<T: Send + Into<O>, O: Send>(_: impl Ctx, value: T, #[data] _out_ty: PhantomData<O>) -> O {
value.into()

View File

@@ -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::<List<Vector>, _>::new(IndexProbe);
let layout = Node::<ContextImpl>::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);