From 7035bf16a4364b84af1a2602979b8e8084275c8c Mon Sep 17 00:00:00 2001 From: Benjamin Woodruff Date: Wed, 14 Aug 2024 16:02:09 -0400 Subject: [PATCH] Add stubs for RawVc::TaskOutput --- turbopack/crates/turbo-tasks/src/id.rs | 60 ++++++++------------- turbopack/crates/turbo-tasks/src/manager.rs | 34 +++++++++++- turbopack/crates/turbo-tasks/src/raw_vc.rs | 56 ++++++++++++++++--- 3 files changed, 103 insertions(+), 47 deletions(-) diff --git a/turbopack/crates/turbo-tasks/src/id.rs b/turbopack/crates/turbo-tasks/src/id.rs index 26f1f712c0ebc..7fbb8d945e5da 100644 --- a/turbopack/crates/turbo-tasks/src/id.rs +++ b/turbopack/crates/turbo-tasks/src/id.rs @@ -10,8 +10,16 @@ use serde::{de::Visitor, Deserialize, Serialize}; use crate::{registry, TaskPersistence}; macro_rules! define_id { - ($name:ident : $primitive:ty $(,derive($($derive:ty),*))?) => { + ( + $name:ident : $primitive:ty + $(,derive($($derive:ty),*))? + $(,serde($serde:tt))? + $(,doc = $doc:literal)* + $(,)? + ) => { + $(#[doc = $doc])* #[derive(Hash, Clone, Copy, PartialEq, Eq, PartialOrd, Ord $($(,$derive)*)? )] + $(#[serde($serde)])? pub struct $name { id: NonZero<$primitive>, } @@ -64,13 +72,24 @@ macro_rules! define_id { }; } -define_id!(TaskId: u32); +define_id!(TaskId: u32, derive(Serialize, Deserialize), serde(transparent)); define_id!(FunctionId: u32); define_id!(ValueTypeId: u32); define_id!(TraitTypeId: u32); define_id!(BackendJobId: u32); define_id!(ExecutionId: u64, derive(Debug)); -define_id!(LocalCellId: u32, derive(Debug)); +define_id!( + LocalCellId: u32, + derive(Debug), + doc = "Represents the nth call to `Vc::cell()` with `local_cells` inside of the parent ", + doc = "non-local task.", +); +define_id!( + LocalOutputId: u32, + derive(Debug, Serialize, Deserialize), + serde(transparent), + doc = "Represents the nth `local_cells` function call inside a task.", +); impl Debug for TaskId { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { @@ -160,38 +179,3 @@ make_serializable!( registry::get_trait_type_id_by_global_name, TraitTypeVisitor ); - -impl Serialize for TaskId { - fn serialize(&self, serializer: S) -> Result - where - S: serde::Serializer, - { - serializer.serialize_u32(**self) - } -} - -impl<'de> Deserialize<'de> for TaskId { - fn deserialize(deserializer: D) -> Result - where - D: serde::Deserializer<'de>, - { - struct V; - - impl Visitor<'_> for V { - type Value = TaskId; - - fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - write!(f, "task id") - } - - fn visit_u32(self, v: u32) -> Result - where - E: serde::de::Error, - { - Ok(TaskId::from(v)) - } - } - - deserializer.deserialize_u32(V) - } -} diff --git a/turbopack/crates/turbo-tasks/src/manager.rs b/turbopack/crates/turbo-tasks/src/manager.rs index 6ae251fe4fe4d..a1c883fb4166f 100644 --- a/turbopack/crates/turbo-tasks/src/manager.rs +++ b/turbopack/crates/turbo-tasks/src/manager.rs @@ -31,7 +31,10 @@ use crate::{ }, capture_future::{self, CaptureFuture}, event::{Event, EventListener}, - id::{BackendJobId, ExecutionId, FunctionId, LocalCellId, TraitTypeId, TRANSIENT_TASK_BIT}, + id::{ + BackendJobId, ExecutionId, FunctionId, LocalCellId, LocalOutputId, TraitTypeId, + TRANSIENT_TASK_BIT, + }, id_factory::{IdFactory, IdFactoryWithReuse}, magic_any::MagicAny, raw_vc::{CellId, RawVc}, @@ -144,6 +147,26 @@ pub trait TurboTasksApi: TurboTasksCallApi + Sync + Send { index: CellId, ) -> Result>; + fn try_read_local_output( + &self, + _task_id: TaskId, + _local_output_id: LocalOutputId, + _consistency: ReadConsistency, + ) -> Result> { + todo!("bgw: local outputs"); + } + + /// INVALIDATION: Be careful with this, it will not track dependencies, so + /// using it could break cache invalidation. + fn try_read_local_output_untracked( + &self, + _task: TaskId, + _local_output_id: LocalOutputId, + _consistency: ReadConsistency, + ) -> Result> { + todo!("bgw: local outputs"); + } + fn read_task_collectibles(&self, task: TaskId, trait_id: TraitTypeId) -> TaskCollectiblesMap; fn emit_collectible(&self, trait_type: TraitTypeId, collectible: RawVc); @@ -1952,6 +1975,15 @@ pub(crate) fn read_local_cell( }) } +pub(crate) async fn read_local_output( + _this: &dyn TurboTasksApi, + _task_id: TaskId, + _local_output_id: LocalOutputId, + _consistency: ReadConsistency, +) -> Result { + todo!("bgw: local outputs"); +} + /// Panics if the [`ExecutionId`] does not match the current task's /// `execution_id`. pub(crate) fn assert_execution_id(execution_id: ExecutionId) { diff --git a/turbopack/crates/turbo-tasks/src/raw_vc.rs b/turbopack/crates/turbo-tasks/src/raw_vc.rs index a844df4d47723..095216635c142 100644 --- a/turbopack/crates/turbo-tasks/src/raw_vc.rs +++ b/turbopack/crates/turbo-tasks/src/raw_vc.rs @@ -15,10 +15,10 @@ use thiserror::Error; use crate::{ backend::{CellContent, TypedCellContent}, event::EventListener, - id::{ExecutionId, LocalCellId}, + id::{ExecutionId, LocalCellId, LocalOutputId}, manager::{ - assert_execution_id, current_task, read_local_cell, read_task_cell, read_task_output, - TurboTasksApi, + assert_execution_id, current_task, read_local_cell, read_local_output, read_task_cell, + read_task_output, TurboTasksApi, }, registry::{self, get_value_type}, turbo_tasks, CollectiblesSource, ReadConsistency, TaskId, TraitTypeId, ValueType, ValueTypeId, @@ -58,6 +58,7 @@ impl Display for CellId { pub enum RawVc { TaskOutput(TaskId), TaskCell(TaskId, CellId), + LocalOutput(TaskId, LocalOutputId), #[serde(skip)] LocalCell(ExecutionId, LocalCellId), } @@ -67,6 +68,7 @@ impl RawVc { match self { RawVc::TaskOutput(_) => false, RawVc::TaskCell(_, _) => true, + RawVc::LocalOutput(_, _) => false, RawVc::LocalCell(_, _) => false, } } @@ -75,6 +77,7 @@ impl RawVc { match self { RawVc::TaskOutput(_) => false, RawVc::TaskCell(_, _) => false, + RawVc::LocalOutput(_, _) => true, RawVc::LocalCell(_, _) => true, } } @@ -162,6 +165,12 @@ impl RawVc { return Err(ResolveTypeError::NoContent); } } + RawVc::LocalOutput(task_id, local_cell_id) => { + current = + read_local_output(&*tt, task_id, local_cell_id, ReadConsistency::Eventual) + .await + .map_err(|source| ResolveTypeError::TaskError { source })?; + } RawVc::LocalCell(execution_id, local_cell_id) => { let shared_reference = read_local_cell(execution_id, local_cell_id); return Ok( @@ -193,16 +202,23 @@ impl RawVc { let tt = turbo_tasks(); let mut current = self; let mut notified = false; + let mut lazily_notify = || { + if !notified { + tt.notify_scheduled_tasks(); + notified = true; + } + }; loop { match current { RawVc::TaskOutput(task) => { - if !notified { - tt.notify_scheduled_tasks(); - notified = true; - } + lazily_notify(); current = read_task_output(&*tt, task, consistency).await?; } RawVc::TaskCell(_, _) => return Ok(current), + RawVc::LocalOutput(task_id, local_cell_id) => { + lazily_notify(); + current = read_local_output(&*tt, task_id, local_cell_id, consistency).await?; + } RawVc::LocalCell(execution_id, local_cell_id) => { let shared_reference = read_local_cell(execution_id, local_cell_id); let value_type = get_value_type(shared_reference.0); @@ -219,7 +235,7 @@ impl RawVc { pub fn get_task_id(&self) -> TaskId { match self { - RawVc::TaskOutput(t) | RawVc::TaskCell(t, _) => *t, + RawVc::TaskOutput(t) | RawVc::TaskCell(t, _) | RawVc::LocalOutput(t, _) => *t, RawVc::LocalCell(execution_id, _) => { assert_execution_id(*execution_id); current_task("RawVc::get_task_id") @@ -367,6 +383,30 @@ impl Future for ReadRawVcFuture { Err(err) => return Poll::Ready(Err(err)), } } + RawVc::LocalOutput(task_id, local_output_id) => { + let read_result = if this.untracked { + this.turbo_tasks.try_read_local_output_untracked( + task_id, + local_output_id, + this.consistency, + ) + } else { + this.turbo_tasks.try_read_local_output( + task_id, + local_output_id, + this.consistency, + ) + }; + match read_result { + Ok(Ok(vc)) => { + this.consistency = ReadConsistency::Eventual; + this.current = vc; + continue 'outer; + } + Ok(Err(listener)) => listener, + Err(err) => return Poll::Ready(Err(err)), + } + } RawVc::LocalCell(execution_id, local_cell_id) => { return Poll::Ready(Ok(read_local_cell(execution_id, local_cell_id).into())); }