Commit 871851ec authored by Tobias Koppers's avatar Tobias Koppers

turbo-tasks: fix review findings in order dependency implementation

- Guard add_order_dependency with should_track_dependencies() early return
- Assert dependency != reader to give a clear error on self-referential calls
- Skip order dep edges on immutable dependency tasks (they never invalidate,
  so leaf distance propagation from them is irrelevant)
- Extract push_leaf_distance_update_if_needed() helper, eliminating the
  duplicated leaf-distance comparison block in try_read_task_output
- Add comment to the Initial else-arm of in_parent() explaining the invariant
- Clarify the CellDependent TODO in leaf_distance_update.rs: OrderDependent
  doesn't share the cycle problem since order deps are manually set
Co-Authored-By: 's avatarClaude <noreply@anthropic.com>
parent dff42064
......@@ -66,7 +66,7 @@ use crate::{
backing_storage::{BackingStorage, SnapshotItem, compute_task_type_hash},
data::{
ActivenessState, CellRef, CollectibleRef, CollectiblesRef, Dirtyness, InProgressCellState,
InProgressState, InProgressStateInner, OutputValue, TransientTask,
InProgressState, InProgressStateInner, LeafDistance, OutputValue, TransientTask,
},
error::TaskError,
utils::{
......@@ -78,6 +78,26 @@ use crate::{
},
};
/// Pushes a leaf-distance update for `reader` onto `queue` if the dependency's leaf
/// distance is not strictly less than the reader's current leaf distance.
///
/// Call this immediately after successfully adding a new dependent edge to ensure the
/// reader's leaf distance is monotonically increasing relative to its dependency.
fn push_leaf_distance_update_if_needed(
dep_leaf_distance: LeafDistance,
reader: TaskId,
reader_leaf_distance: LeafDistance,
queue: &mut LeafDistanceUpdateQueue,
) {
if reader_leaf_distance.distance <= dep_leaf_distance.distance {
queue.push(
reader,
dep_leaf_distance.distance,
dep_leaf_distance.max_distance_in_buffer,
);
}
}
/// Threshold for parallelizing making dependent tasks dirty.
/// If the number of dependent tasks exceeds this threshold,
/// the operation will be parallelized.
......@@ -751,17 +771,12 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
let mut queue = LeafDistanceUpdateQueue::new();
let reader = reader.unwrap();
if task.add_output_dependent(reader) {
// Ensure that dependent leaf distance is strictly monotonic increasing
let leaf_distance = task.get_leaf_distance().copied().unwrap_or_default();
let reader_leaf_distance =
reader_task.get_leaf_distance().copied().unwrap_or_default();
if reader_leaf_distance.distance <= leaf_distance.distance {
queue.push(
reader,
leaf_distance.distance,
leaf_distance.max_distance_in_buffer,
);
}
push_leaf_distance_update_if_needed(
task.get_leaf_distance().copied().unwrap_or_default(),
reader,
reader_task.get_leaf_distance().copied().unwrap_or_default(),
&mut queue,
);
}
drop(task);
......@@ -3140,22 +3155,35 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
reader: TaskId,
turbo_tasks: &dyn TurboTasksBackendApi<TurboTasksBackend<B>>,
) {
if !self.should_track_dependencies() {
return;
}
assert_ne!(
dependency, reader,
"add_order_dependency called with the same task as both dependency and reader; \
self-referential order dependencies are not allowed"
);
let mut ctx = self.execute_context(turbo_tasks);
let (mut dep_task, mut reader_task) =
ctx.task_pair(dependency, reader, TaskDataCategory::Data);
if dep_task.immutable() && !cfg!(feature = "verify_immutable") {
// Immutable tasks never re-execute and thus never invalidate their dependents.
// Order dependencies on them serve no purpose, as invalidation ordering is
// irrelevant when no invalidation can ever occur.
return;
}
let mut queue = LeafDistanceUpdateQueue::new();
if dep_task.add_order_dependent(reader) {
// Ensure that dependent leaf distance is strictly monotonic increasing
let leaf_distance = dep_task.get_leaf_distance().copied().unwrap_or_default();
let reader_leaf_distance = reader_task.get_leaf_distance().copied().unwrap_or_default();
if reader_leaf_distance.distance <= leaf_distance.distance {
queue.push(
reader,
leaf_distance.distance,
leaf_distance.max_distance_in_buffer,
);
}
push_leaf_distance_update_if_needed(
dep_task.get_leaf_distance().copied().unwrap_or_default(),
reader,
reader_task.get_leaf_distance().copied().unwrap_or_default(),
&mut queue,
);
}
drop(dep_task);
......
......@@ -161,9 +161,10 @@ impl LeafDistanceUpdateQueue {
// We are within the buffer zone, keep the max as is
leaf_distance.distance = dependencies_distance + 1;
}
// TODO Technically CellDependent is also needed, but there are cycles in the CellDependent
// graph. So we need to handle that properly first. When enabling this, make sure to also
// call the leaf update queue when adding CellDependents.
// TODO CellDependent is also needed here, but there are cycles in the CellDependent graph
// that must be handled first. When enabling this, also call the leaf update queue when
// adding CellDependents. Note: OrderDependent does not share this cycle problem since
// order dependencies are set manually and cannot be created implicitly through reads.
for dependent_id in task
.iter_output_dependent()
.chain(task.iter_order_dependent())
......
......@@ -477,6 +477,9 @@ impl TaskExecutionOrder {
leaf_distance: *leaf_distance,
}
} else {
// The parent is Initial (not yet part of any invalidation wave), so
// there is no ordering context to inherit. Keep this task's own
// execution order unchanged.
*self
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment