Commit dff42064 authored by Tobias Koppers's avatar Tobias Koppers

turbo-tasks: implement order dependencies for scheduling order control

Add a new `Vc::add_order_dependency()` API that lets a task declare it should
be scheduled after another task during invalidation. Unlike output/cell
dependencies, order dependencies do NOT trigger invalidation when the
depended-on task's output changes — they only affect leaf distance propagation
and scheduling priority.

Implementation follows the same bidirectional edge pattern as output
dependencies: `order_dependent` (reverse, on depended-upon task) and
`order_dependencies` (forward, on dependent task), with an
`outdated_order_dependencies` transient snapshot for lifecycle management.
Co-Authored-By: 's avatarClaude <noreply@anthropic.com>
parent 0ed0cec5
...@@ -1975,6 +1975,9 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> { ...@@ -1975,6 +1975,9 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
let outdated_output_dependencies = task.iter_output_dependencies().collect(); let outdated_output_dependencies = task.iter_output_dependencies().collect();
task.set_outdated_output_dependencies(outdated_output_dependencies); task.set_outdated_output_dependencies(outdated_output_dependencies);
let outdated_order_dependencies = task.iter_order_dependencies().collect();
task.set_outdated_order_dependencies(outdated_order_dependencies);
} }
} }
...@@ -2307,6 +2310,10 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> { ...@@ -2307,6 +2310,10 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
task.iter_outdated_output_dependencies() task.iter_outdated_output_dependencies()
.map(OutdatedEdge::OutputDependency), .map(OutdatedEdge::OutputDependency),
); );
old_edges.extend(
task.iter_outdated_order_dependencies()
.map(OutdatedEdge::OrderDependency),
);
} }
// Check if output need to be updated // Check if output need to be updated
...@@ -3127,6 +3134,40 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> { ...@@ -3127,6 +3134,40 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
ConnectChildOperation::run(parent_task, task, None, self.execute_context(turbo_tasks)); ConnectChildOperation::run(parent_task, task, None, self.execute_context(turbo_tasks));
} }
fn add_order_dependency(
&self,
dependency: TaskId,
reader: TaskId,
turbo_tasks: &dyn TurboTasksBackendApi<TurboTasksBackend<B>>,
) {
let mut ctx = self.execute_context(turbo_tasks);
let (mut dep_task, mut reader_task) =
ctx.task_pair(dependency, reader, TaskDataCategory::Data);
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,
);
}
}
drop(dep_task);
if !reader_task.remove_outdated_order_dependencies(&dependency) {
let _ = reader_task.add_order_dependencies(dependency);
}
drop(reader_task);
queue.execute(&mut ctx);
}
fn create_transient_task(&self, task_type: TransientTaskType) -> TaskId { fn create_transient_task(&self, task_type: TransientTaskType) -> TaskId {
let task_id = self.transient_task_id_factory.get(); let task_id = self.transient_task_id_factory.get();
{ {
...@@ -3650,6 +3691,15 @@ impl<B: BackingStorage> Backend for TurboTasksBackend<B> { ...@@ -3650,6 +3691,15 @@ impl<B: BackingStorage> Backend for TurboTasksBackend<B> {
self.0.connect_task(task, parent_task, turbo_tasks); self.0.connect_task(task, parent_task, turbo_tasks);
} }
fn add_order_dependency(
&self,
dependency: TaskId,
reader: TaskId,
turbo_tasks: &dyn TurboTasksBackendApi<Self>,
) {
self.0.add_order_dependency(dependency, reader, turbo_tasks);
}
fn create_transient_task( fn create_transient_task(
&self, &self,
task_type: TransientTaskType, task_type: TransientTaskType,
......
...@@ -41,6 +41,7 @@ pub enum OutdatedEdge { ...@@ -41,6 +41,7 @@ pub enum OutdatedEdge {
Collectible(CollectibleRef, i32), Collectible(CollectibleRef, i32),
CellDependency(CellRef, Option<u64>), CellDependency(CellRef, Option<u64>),
OutputDependency(TaskId), OutputDependency(TaskId),
OrderDependency(TaskId),
CollectiblesDependency(CollectiblesRef), CollectiblesDependency(CollectiblesRef),
} }
...@@ -193,6 +194,16 @@ impl Operation for CleanupOldEdgesOperation { ...@@ -193,6 +194,16 @@ impl Operation for CleanupOldEdgesOperation {
task.remove_output_dependencies(&output_task_id); task.remove_output_dependencies(&output_task_id);
} }
} }
OutdatedEdge::OrderDependency(order_task_id) => {
{
let mut task = ctx.task(order_task_id, TaskDataCategory::Data);
task.remove_order_dependent(&task_id);
}
{
let mut task = ctx.task(task_id, TaskDataCategory::Data);
task.remove_order_dependencies(&order_task_id);
}
}
OutdatedEdge::CollectiblesDependency(CollectiblesRef { OutdatedEdge::CollectiblesDependency(CollectiblesRef {
collectible_type, collectible_type,
task: dependent_task_id, task: dependent_task_id,
......
...@@ -164,7 +164,10 @@ impl LeafDistanceUpdateQueue { ...@@ -164,7 +164,10 @@ impl LeafDistanceUpdateQueue {
// TODO Technically CellDependent is also needed, but there are cycles in the CellDependent // 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 // graph. So we need to handle that properly first. When enabling this, make sure to also
// call the leaf update queue when adding CellDependents. // call the leaf update queue when adding CellDependents.
for dependent_id in task.iter_output_dependent() { for dependent_id in task
.iter_output_dependent()
.chain(task.iter_order_dependent())
{
self.push( self.push(
dependent_id, dependent_id,
leaf_distance.distance, leaf_distance.distance,
......
...@@ -77,6 +77,12 @@ struct TaskStorageSchema { ...@@ -77,6 +77,12 @@ struct TaskStorageSchema {
#[field(storage = "auto_set", category = "data", inline, filter_transient)] #[field(storage = "auto_set", category = "data", inline, filter_transient)]
output_dependent: AutoSet<TaskId>, output_dependent: AutoSet<TaskId>,
/// Tasks that have an order dependency on this task.
/// Order dependencies only affect leaf distance propagation and scheduling order,
/// not invalidation.
#[field(storage = "auto_set", category = "data", filter_transient)]
order_dependent: AutoSet<TaskId>,
/// The task's output value. /// The task's output value.
/// Filtered during serialization to skip transient outputs (referencing transient tasks). /// Filtered during serialization to skip transient outputs (referencing transient tasks).
#[field(storage = "direct", category = "meta", inline, filter_transient)] #[field(storage = "direct", category = "meta", inline, filter_transient)]
...@@ -248,6 +254,18 @@ struct TaskStorageSchema { ...@@ -248,6 +254,18 @@ struct TaskStorageSchema {
)] )]
collectibles_dependencies: AutoSet<CollectiblesRef>, collectibles_dependencies: AutoSet<CollectiblesRef>,
/// Tasks this task has order dependencies on.
/// Order dependencies only affect leaf distance propagation and scheduling order,
/// not invalidation.
#[field(
storage = "auto_set",
category = "data",
filter_transient,
shrink_on_completion,
drop_on_completion_if_immutable
)]
order_dependencies: AutoSet<TaskId>,
/// Outdated output dependencies to be cleaned up (transient). /// Outdated output dependencies to be cleaned up (transient).
#[field(storage = "auto_set", category = "transient", shrink_on_completion)] #[field(storage = "auto_set", category = "transient", shrink_on_completion)]
outdated_output_dependencies: AutoSet<TaskId>, outdated_output_dependencies: AutoSet<TaskId>,
...@@ -260,6 +278,10 @@ struct TaskStorageSchema { ...@@ -260,6 +278,10 @@ struct TaskStorageSchema {
#[field(storage = "auto_set", category = "transient", shrink_on_completion)] #[field(storage = "auto_set", category = "transient", shrink_on_completion)]
outdated_collectibles_dependencies: AutoSet<CollectiblesRef>, outdated_collectibles_dependencies: AutoSet<CollectiblesRef>,
/// Outdated order dependencies to be cleaned up (transient).
#[field(storage = "auto_set", category = "transient", shrink_on_completion)]
outdated_order_dependencies: AutoSet<TaskId>,
// ========================================================================= // =========================================================================
// DEPENDENTS - Tasks that depend on this task's cells // DEPENDENTS - Tasks that depend on this task's cells
// ========================================================================= // =========================================================================
......
...@@ -291,6 +291,10 @@ impl TurboTasksApi for VcStorage { ...@@ -291,6 +291,10 @@ impl TurboTasksApi for VcStorage {
// no-op // no-op
} }
fn add_order_dependency(&self, _task: TaskId) {
// no-op
}
fn mark_own_task_as_finished(&self, _task: TaskId) { fn mark_own_task_as_finished(&self, _task: TaskId) {
// no-op // no-op
} }
......
...@@ -631,6 +631,17 @@ pub trait Backend: Sync + Send { ...@@ -631,6 +631,17 @@ pub trait Backend: Sync + Send {
turbo_tasks: &dyn TurboTasksBackendApi<Self>, turbo_tasks: &dyn TurboTasksBackendApi<Self>,
); );
/// Registers an order dependency: `reader` should be scheduled after `dependency`.
/// This affects leaf distance propagation only, not invalidation.
fn add_order_dependency(
&self,
_dependency: TaskId,
_reader: TaskId,
_turbo_tasks: &dyn TurboTasksBackendApi<Self>,
) {
// Default: no-op
}
fn mark_own_task_as_finished( fn mark_own_task_as_finished(
&self, &self,
_task: TaskId, _task: TaskId,
......
...@@ -183,6 +183,10 @@ pub trait TurboTasksApi: TurboTasksCallApi + Sync + Send { ...@@ -183,6 +183,10 @@ pub trait TurboTasksApi: TurboTasksCallApi + Sync + Send {
fn connect_task(&self, task: TaskId); fn connect_task(&self, task: TaskId);
/// Registers an order dependency from the currently executing task to the given task.
/// This only affects leaf distance propagation and scheduling order, not invalidation.
fn add_order_dependency(&self, task: TaskId);
/// Wraps the given future in the current task. /// Wraps the given future in the current task.
/// ///
/// Beware: this method is not safe to use in production code. It is only intended for use in /// Beware: this method is not safe to use in production code. It is only intended for use in
...@@ -1637,6 +1641,12 @@ impl<B: Backend + 'static> TurboTasksApi for TurboTasks<B> { ...@@ -1637,6 +1641,12 @@ impl<B: Backend + 'static> TurboTasksApi for TurboTasks<B> {
.connect_task(task, current_task_if_available("connecting task"), self); .connect_task(task, current_task_if_available("connecting task"), self);
} }
fn add_order_dependency(&self, task: TaskId) {
if let Some(reader) = current_task_if_available("add_order_dependency") {
self.backend.add_order_dependency(task, reader, self);
}
}
fn mark_own_task_as_finished(&self, task: TaskId) { fn mark_own_task_as_finished(&self, task: TaskId) {
self.backend.mark_own_task_as_finished(task, self); self.backend.mark_own_task_as_finished(task, self);
} }
......
...@@ -177,6 +177,18 @@ impl RawVc { ...@@ -177,6 +177,18 @@ impl RawVc {
tt.connect_task(*task_id); tt.connect_task(*task_id);
} }
pub(crate) fn add_order_dependency(&self) {
let tt = turbo_tasks();
match self {
RawVc::TaskOutput(task_id) | RawVc::TaskCell(task_id, ..) => {
tt.add_order_dependency(*task_id);
}
RawVc::LocalOutput(..) => {
// Local outputs are in the same task, so order dependencies are meaningless.
}
}
}
pub fn try_get_task_id(&self) -> Option<TaskId> { pub fn try_get_task_id(&self) -> Option<TaskId> {
match self { match self {
RawVc::TaskOutput(t) | RawVc::TaskCell(t, ..) => Some(*t), RawVc::TaskOutput(t) | RawVc::TaskCell(t, ..) => Some(*t),
......
...@@ -414,6 +414,15 @@ where ...@@ -414,6 +414,15 @@ where
Ok(()) Ok(())
} }
/// Registers an order dependency from the currently executing task to this Vc's task.
///
/// This affects only scheduling order during invalidation: the current task will be
/// scheduled after this Vc's task. It does NOT cause invalidation when this Vc's
/// output changes — use `.await` or `.resolve()` for that.
pub fn add_order_dependency(self) {
self.node.add_order_dependency();
}
/// Do not use this: Use [`Vc::to_resolved`] instead. If you must have a resolved [`Vc`] type /// Do not use this: Use [`Vc::to_resolved`] instead. If you must have a resolved [`Vc`] type
/// and not a [`ResolvedVc`] type, simply deref the result of [`Vc::to_resolved`]. /// and not a [`ResolvedVc`] type, simply deref the result of [`Vc::to_resolved`].
pub fn resolve(self) -> ResolveVcFuture<T> { pub fn resolve(self) -> ResolveVcFuture<T> {
......
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