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::{ ...@@ -66,7 +66,7 @@ use crate::{
backing_storage::{BackingStorage, SnapshotItem, compute_task_type_hash}, backing_storage::{BackingStorage, SnapshotItem, compute_task_type_hash},
data::{ data::{
ActivenessState, CellRef, CollectibleRef, CollectiblesRef, Dirtyness, InProgressCellState, ActivenessState, CellRef, CollectibleRef, CollectiblesRef, Dirtyness, InProgressCellState,
InProgressState, InProgressStateInner, OutputValue, TransientTask, InProgressState, InProgressStateInner, LeafDistance, OutputValue, TransientTask,
}, },
error::TaskError, error::TaskError,
utils::{ utils::{
...@@ -78,6 +78,26 @@ use crate::{ ...@@ -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. /// Threshold for parallelizing making dependent tasks dirty.
/// If the number of dependent tasks exceeds this threshold, /// If the number of dependent tasks exceeds this threshold,
/// the operation will be parallelized. /// the operation will be parallelized.
...@@ -751,17 +771,12 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> { ...@@ -751,17 +771,12 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
let mut queue = LeafDistanceUpdateQueue::new(); let mut queue = LeafDistanceUpdateQueue::new();
let reader = reader.unwrap(); let reader = reader.unwrap();
if task.add_output_dependent(reader) { if task.add_output_dependent(reader) {
// Ensure that dependent leaf distance is strictly monotonic increasing push_leaf_distance_update_if_needed(
let leaf_distance = task.get_leaf_distance().copied().unwrap_or_default(); task.get_leaf_distance().copied().unwrap_or_default(),
let reader_leaf_distance = reader,
reader_task.get_leaf_distance().copied().unwrap_or_default(); reader_task.get_leaf_distance().copied().unwrap_or_default(),
if reader_leaf_distance.distance <= leaf_distance.distance { &mut queue,
queue.push( );
reader,
leaf_distance.distance,
leaf_distance.max_distance_in_buffer,
);
}
} }
drop(task); drop(task);
...@@ -3140,22 +3155,35 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> { ...@@ -3140,22 +3155,35 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
reader: TaskId, reader: TaskId,
turbo_tasks: &dyn TurboTasksBackendApi<TurboTasksBackend<B>>, 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 ctx = self.execute_context(turbo_tasks);
let (mut dep_task, mut reader_task) = let (mut dep_task, mut reader_task) =
ctx.task_pair(dependency, reader, TaskDataCategory::Data); 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(); let mut queue = LeafDistanceUpdateQueue::new();
if dep_task.add_order_dependent(reader) { if dep_task.add_order_dependent(reader) {
// Ensure that dependent leaf distance is strictly monotonic increasing push_leaf_distance_update_if_needed(
let leaf_distance = dep_task.get_leaf_distance().copied().unwrap_or_default(); dep_task.get_leaf_distance().copied().unwrap_or_default(),
let reader_leaf_distance = reader_task.get_leaf_distance().copied().unwrap_or_default(); reader,
if reader_leaf_distance.distance <= leaf_distance.distance { reader_task.get_leaf_distance().copied().unwrap_or_default(),
queue.push( &mut queue,
reader, );
leaf_distance.distance,
leaf_distance.max_distance_in_buffer,
);
}
} }
drop(dep_task); drop(dep_task);
......
...@@ -161,9 +161,10 @@ impl LeafDistanceUpdateQueue { ...@@ -161,9 +161,10 @@ impl LeafDistanceUpdateQueue {
// We are within the buffer zone, keep the max as is // We are within the buffer zone, keep the max as is
leaf_distance.distance = dependencies_distance + 1; leaf_distance.distance = dependencies_distance + 1;
} }
// TODO Technically CellDependent is also needed, but there are cycles in the CellDependent // TODO CellDependent is also needed here, but there are cycles in the CellDependent graph
// graph. So we need to handle that properly first. When enabling this, make sure to also // that must be handled first. When enabling this, also call the leaf update queue when
// call the leaf update queue when adding CellDependents. // 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 for dependent_id in task
.iter_output_dependent() .iter_output_dependent()
.chain(task.iter_order_dependent()) .chain(task.iter_order_dependent())
......
...@@ -477,6 +477,9 @@ impl TaskExecutionOrder { ...@@ -477,6 +477,9 @@ impl TaskExecutionOrder {
leaf_distance: *leaf_distance, leaf_distance: *leaf_distance,
} }
} else { } 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 *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