Unverified Commit f7e66622 authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

Turbopack: add verify_determinism feature to check if tasks are deterministic (#85559)

### What?

Enabling the feature will double execute tasks and print to stdout when tasks are updating cells while recomputing

The change will also change the behavior of cell updates during recomputation slightly. Instead of updating the cell and not notifying dependents, it will not update the cell at all ignoring the (unexpected) change.
This means when a task is recomputed without being invalidated, it won't update any cell data, except for the missing cell values.
parent b8a14cd8
...@@ -19,6 +19,7 @@ no_fast_stale = [] ...@@ -19,6 +19,7 @@ no_fast_stale = []
verify_serialization = [] verify_serialization = []
verify_aggregation_graph = [] verify_aggregation_graph = []
verify_immutable = [] verify_immutable = []
verify_determinism = []
trace_aggregation_update = [] trace_aggregation_update = []
trace_find_and_schedule = [] trace_find_and_schedule = []
trace_task_completion = [] trace_task_completion = []
......
...@@ -31,7 +31,7 @@ use turbo_tasks::{ ...@@ -31,7 +31,7 @@ use turbo_tasks::{
TraitTypeId, TurboTasksBackendApi, ValueTypeId, TraitTypeId, TurboTasksBackendApi, ValueTypeId,
backend::{ backend::{
Backend, CachedTaskType, CellContent, TaskExecutionSpec, TransientTaskRoot, Backend, CachedTaskType, CellContent, TaskExecutionSpec, TransientTaskRoot,
TransientTaskType, TurboTasksExecutionError, TypedCellContent, TransientTaskType, TurboTasksExecutionError, TypedCellContent, VerificationMode,
}, },
event::{Event, EventListener}, event::{Event, EventListener},
message_queue::TimingEvent, message_queue::TimingEvent,
...@@ -423,6 +423,8 @@ struct TaskExecutionCompletePrepareResult { ...@@ -423,6 +423,8 @@ struct TaskExecutionCompletePrepareResult {
pub new_children: FxHashSet<TaskId>, pub new_children: FxHashSet<TaskId>,
pub removed_data: Vec<CachedDataItem>, pub removed_data: Vec<CachedDataItem>,
pub is_now_immutable: bool, pub is_now_immutable: bool,
#[cfg(feature = "verify_determinism")]
pub no_output_set: bool,
pub new_output: Option<OutputValue>, pub new_output: Option<OutputValue>,
pub output_dependent_tasks: SmallVec<[TaskId; 4]>, pub output_dependent_tasks: SmallVec<[TaskId; 4]>,
} }
...@@ -1765,6 +1767,8 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> { ...@@ -1765,6 +1767,8 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
new_children, new_children,
mut removed_data, mut removed_data,
is_now_immutable, is_now_immutable,
#[cfg(feature = "verify_determinism")]
no_output_set,
new_output, new_output,
output_dependent_tasks, output_dependent_tasks,
}) = self.task_execution_completed_prepare( }) = self.task_execution_completed_prepare(
...@@ -1809,6 +1813,8 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> { ...@@ -1809,6 +1813,8 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
if self.task_execution_completed_finish( if self.task_execution_completed_finish(
&mut ctx, &mut ctx,
task_id, task_id,
#[cfg(feature = "verify_determinism")]
no_output_set,
new_output, new_output,
&mut removed_data, &mut removed_data,
is_now_immutable, is_now_immutable,
...@@ -1843,6 +1849,8 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> { ...@@ -1843,6 +1849,8 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
new_children: Default::default(), new_children: Default::default(),
removed_data: Default::default(), removed_data: Default::default(),
is_now_immutable: false, is_now_immutable: false,
#[cfg(feature = "verify_determinism")]
no_output_set: false,
new_output: None, new_output: None,
output_dependent_tasks: Default::default(), output_dependent_tasks: Default::default(),
}); });
...@@ -2021,6 +2029,8 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> { ...@@ -2021,6 +2029,8 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
// Check if output need to be updated // Check if output need to be updated
let current_output = get!(task, Output); let current_output = get!(task, Output);
#[cfg(feature = "verify_determinism")]
let no_output_set = current_output.is_none();
let new_output = match result { let new_output = match result {
Ok(RawVc::TaskOutput(output_task_id)) => { Ok(RawVc::TaskOutput(output_task_id)) => {
if let Some(OutputValue::Output(current_task_id)) = current_output if let Some(OutputValue::Output(current_task_id)) = current_output
...@@ -2092,6 +2102,8 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> { ...@@ -2092,6 +2102,8 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
new_children, new_children,
removed_data, removed_data,
is_now_immutable, is_now_immutable,
#[cfg(feature = "verify_determinism")]
no_output_set,
new_output, new_output,
output_dependent_tasks, output_dependent_tasks,
}) })
...@@ -2232,6 +2244,7 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> { ...@@ -2232,6 +2244,7 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
&self, &self,
ctx: &mut impl ExecuteContext<'_>, ctx: &mut impl ExecuteContext<'_>,
task_id: TaskId, task_id: TaskId,
#[cfg(feature = "verify_determinism")] no_output_set: bool,
new_output: Option<OutputValue>, new_output: Option<OutputValue>,
removed_data: &mut Vec<CachedDataItem>, removed_data: &mut Vec<CachedDataItem>,
is_now_immutable: bool, is_now_immutable: bool,
...@@ -2306,7 +2319,8 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> { ...@@ -2306,7 +2319,8 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
None None
}; };
let data_update = if old_dirty_state != new_dirty_state { let dirty_changed = old_dirty_state != new_dirty_state;
let data_update = if dirty_changed {
if let Some(new_dirty_state) = new_dirty_state { if let Some(new_dirty_state) = new_dirty_state {
task.insert(CachedDataItem::Dirty { task.insert(CachedDataItem::Dirty {
value: new_dirty_state, value: new_dirty_state,
...@@ -2353,17 +2367,32 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> { ...@@ -2353,17 +2367,32 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
None None
}; };
drop(task); #[cfg(feature = "verify_determinism")]
drop(old_content); let reschedule = (dirty_changed || no_output_set) && !task_id.is_transient();
#[cfg(not(feature = "verify_determinism"))]
let reschedule = false;
if reschedule {
task.add_new(CachedDataItem::InProgress {
value: InProgressState::Scheduled {
done_event,
reason: TaskExecutionReason::Stale,
},
});
drop(task);
} else {
drop(task);
// Notify dependent tasks that are waiting for this task to finish
done_event.notify(usize::MAX);
}
// Notify dependent tasks that are waiting for this task to finish drop(old_content);
done_event.notify(usize::MAX);
if let Some(data_update) = data_update { if let Some(data_update) = data_update {
AggregationUpdateQueue::run(data_update, ctx); AggregationUpdateQueue::run(data_update, ctx);
} }
false reschedule
} }
fn task_execution_completed_cleanup(&self, ctx: &mut impl ExecuteContext<'_>, task_id: TaskId) { fn task_execution_completed_cleanup(&self, ctx: &mut impl ExecuteContext<'_>, task_id: TaskId) {
...@@ -2652,12 +2681,14 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> { ...@@ -2652,12 +2681,14 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
task_id: TaskId, task_id: TaskId,
cell: CellId, cell: CellId,
content: CellContent, content: CellContent,
verification_mode: VerificationMode,
turbo_tasks: &dyn TurboTasksBackendApi<TurboTasksBackend<B>>, turbo_tasks: &dyn TurboTasksBackendApi<TurboTasksBackend<B>>,
) { ) {
operation::UpdateCellOperation::run( operation::UpdateCellOperation::run(
task_id, task_id,
cell, cell,
content, content,
verification_mode,
self.execute_context(turbo_tasks), self.execute_context(turbo_tasks),
); );
} }
...@@ -3251,9 +3282,11 @@ impl<B: BackingStorage> Backend for TurboTasksBackend<B> { ...@@ -3251,9 +3282,11 @@ impl<B: BackingStorage> Backend for TurboTasksBackend<B> {
task_id: TaskId, task_id: TaskId,
cell: CellId, cell: CellId,
content: CellContent, content: CellContent,
verification_mode: VerificationMode,
turbo_tasks: &dyn TurboTasksBackendApi<Self>, turbo_tasks: &dyn TurboTasksBackendApi<Self>,
) { ) {
self.0.update_task_cell(task_id, cell, content, turbo_tasks); self.0
.update_task_cell(task_id, cell, content, verification_mode, turbo_tasks);
} }
fn mark_own_task_as_finished( fn mark_own_task_as_finished(
......
...@@ -2,6 +2,8 @@ use std::mem::take; ...@@ -2,6 +2,8 @@ use std::mem::take;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use smallvec::SmallVec; use smallvec::SmallVec;
#[cfg(not(feature = "verify_determinism"))]
use turbo_tasks::backend::VerificationMode;
use turbo_tasks::{CellId, TaskId, TypedSharedReference, backend::CellContent}; use turbo_tasks::{CellId, TaskId, TypedSharedReference, backend::CellContent};
#[cfg(feature = "trace_task_dirty")] #[cfg(feature = "trace_task_dirty")]
...@@ -13,7 +15,7 @@ use crate::{ ...@@ -13,7 +15,7 @@ use crate::{
AggregationUpdateQueue, ExecuteContext, Operation, TaskGuard, AggregationUpdateQueue, ExecuteContext, Operation, TaskGuard,
invalidate::make_task_dirty_internal, invalidate::make_task_dirty_internal,
}, },
storage::{get_many, remove}, storage::{get, get_many, remove},
}, },
data::{CachedDataItem, CachedDataItemKey, CellRef}, data::{CachedDataItem, CachedDataItemKey, CellRef},
}; };
...@@ -40,18 +42,61 @@ pub enum UpdateCellOperation { ...@@ -40,18 +42,61 @@ pub enum UpdateCellOperation {
} }
impl UpdateCellOperation { impl UpdateCellOperation {
pub fn run(task_id: TaskId, cell: CellId, content: CellContent, mut ctx: impl ExecuteContext) { pub fn run(
task_id: TaskId,
cell: CellId,
content: CellContent,
#[cfg(feature = "verify_determinism")] verification_mode: VerificationMode,
#[cfg(not(feature = "verify_determinism"))] _verification_mode: VerificationMode,
mut ctx: impl ExecuteContext,
) {
let content = if let CellContent(Some(new_content)) = content {
Some(new_content.into_typed(cell.type_id))
} else {
None
};
let mut task = ctx.task(task_id, TaskDataCategory::All); let mut task = ctx.task(task_id, TaskDataCategory::All);
let is_stateful = task.has_key(&CachedDataItemKey::Stateful {});
// We need to detect recomputation, because here the content has not actually changed (even // We need to detect recomputation, because here the content has not actually changed (even
// if it's not equal to the old content, as not all values implement Eq). We have to // if it's not equal to the old content, as not all values implement Eq). We have to
// assume that tasks are deterministic and pure. // assume that tasks are deterministic and pure.
let should_invalidate = ctx.should_track_dependencies() let assume_unchanged = !ctx.should_track_dependencies()
&& (task.has_key(&CachedDataItemKey::Dirty {}) || || (!task.has_key(&CachedDataItemKey::Dirty {})
// This is a hack for the streaming hack. Stateful tasks are never recomputed, so this forces invalidation for them in case of this hack. // This is a hack for the streaming hack. Stateful tasks are never recomputed, so this forces invalidation for them in case of this hack.
task.has_key(&CachedDataItemKey::Stateful {})); && !is_stateful);
let old_content = get!(task, CellData { cell });
if assume_unchanged {
if old_content.is_some() {
// Never update cells when recomputing if they already have a value.
// It's not expected that content changes during recomputation.
// Check if this assumption holds.
#[cfg(feature = "verify_determinism")]
if !is_stateful
&& matches!(verification_mode, VerificationMode::EqualityCheck)
&& content.as_ref() != old_content
{
let task_description = ctx.get_task_description(task_id);
let cell_type = turbo_tasks::registry::get_value_type(cell.type_id).global_name;
eprintln!(
"Task {} updated cell #{} (type: {}) while recomputing",
task_description, cell.index, cell_type
);
}
return;
} else {
// Initial computation, or computation after a cell has been cleared.
// We can just set the content, but we don't want to notify dependent tasks,
// as we assume that content hasn't changed (deterministic tasks).
}
} else {
// When not recomputing, we need to notify dependent tasks if the content actually
// changes.
if should_invalidate {
let dependent_tasks: SmallVec<[TaskId; 4]> = get_many!( let dependent_tasks: SmallVec<[TaskId; 4]> = get_many!(
task, task,
CellDependent { cell: dependent_cell, task } CellDependent { cell: dependent_cell, task }
...@@ -78,12 +123,6 @@ impl UpdateCellOperation { ...@@ -78,12 +123,6 @@ impl UpdateCellOperation {
drop(task); drop(task);
drop(old_content); drop(old_content);
let content = if let CellContent(Some(new_content)) = content {
Some(new_content.into_typed(cell.type_id))
} else {
None
};
UpdateCellOperation::InvalidateWhenCellDependency { UpdateCellOperation::InvalidateWhenCellDependency {
cell_ref: CellRef { cell_ref: CellRef {
task: task_id, task: task_id,
...@@ -101,8 +140,7 @@ impl UpdateCellOperation { ...@@ -101,8 +140,7 @@ impl UpdateCellOperation {
// Fast path: We don't need to invalidate anything. // Fast path: We don't need to invalidate anything.
// So we can just update the cell content. // So we can just update the cell content.
let old_content = if let CellContent(Some(new_content)) = content { let old_content = if let Some(new_content) = content {
let new_content = new_content.into_typed(cell.type_id);
task.insert(CachedDataItem::CellData { task.insert(CachedDataItem::CellData {
cell, cell,
value: new_content, value: new_content,
......
...@@ -18,6 +18,7 @@ static REGISTRATION: Registration = register!(); ...@@ -18,6 +18,7 @@ static REGISTRATION: Registration = register!();
#[tokio::test(flavor = "multi_thread", worker_threads = 2)] #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_spawns_detached() -> anyhow::Result<()> { async fn test_spawns_detached() -> anyhow::Result<()> {
run_once(&REGISTRATION, || async { run_once(&REGISTRATION, || async {
println!("test_spawns_detached");
// HACK: The watch channel we use has an incorrect implementation of `TraceRawVcs`, just // HACK: The watch channel we use has an incorrect implementation of `TraceRawVcs`, just
// disable GC for the test so this can't cause any problems. // disable GC for the test so this can't cause any problems.
prevent_gc(); prevent_gc();
...@@ -73,7 +74,9 @@ async fn spawns_detached( ...@@ -73,7 +74,9 @@ async fn spawns_detached(
sender: TransientInstance<WatchSenderTaskInput<Option<Vc<u32>>>>, sender: TransientInstance<WatchSenderTaskInput<Option<Vc<u32>>>>,
) -> Vc<()> { ) -> Vc<()> {
tokio::spawn(turbo_tasks().detached_for_testing(Box::pin(async move { tokio::spawn(turbo_tasks().detached_for_testing(Box::pin(async move {
println!("spawns_detached: waiting for notify");
notify.0.notified().await; notify.0.notified().await;
println!("spawns_detached: notified, sending value");
// creating cells after the normal lifetime of the task should be okay, as the parent task // creating cells after the normal lifetime of the task should be okay, as the parent task
// is waiting on us before exiting! // is waiting on us before exiting!
sender.0.send(Some(Vc::cell(42))).unwrap(); sender.0.send(Some(Vc::cell(42))).unwrap();
......
...@@ -18,7 +18,7 @@ use tokio::sync::mpsc::Receiver; ...@@ -18,7 +18,7 @@ use tokio::sync::mpsc::Receiver;
use turbo_tasks::{ use turbo_tasks::{
CellId, ExecutionId, InvalidationReason, LocalTaskId, MagicAny, RawVc, ReadCellOptions, CellId, ExecutionId, InvalidationReason, LocalTaskId, MagicAny, RawVc, ReadCellOptions,
ReadOutputOptions, TaskId, TaskPersistence, TraitTypeId, TurboTasksApi, TurboTasksCallApi, ReadOutputOptions, TaskId, TaskPersistence, TraitTypeId, TurboTasksApi, TurboTasksCallApi,
backend::{CellContent, TaskCollectiblesMap, TypedCellContent}, backend::{CellContent, TaskCollectiblesMap, TypedCellContent, VerificationMode},
event::{Event, EventListener}, event::{Event, EventListener},
message_queue::CompilationEvent, message_queue::CompilationEvent,
test_helpers::with_turbo_tasks_for_testing, test_helpers::with_turbo_tasks_for_testing,
...@@ -261,7 +261,13 @@ impl TurboTasksApi for VcStorage { ...@@ -261,7 +261,13 @@ impl TurboTasksApi for VcStorage {
.into_typed(index.type_id)) .into_typed(index.type_id))
} }
fn update_own_task_cell(&self, task: TaskId, index: CellId, content: CellContent) { fn update_own_task_cell(
&self,
task: TaskId,
index: CellId,
content: CellContent,
_verification_mode: VerificationMode,
) {
let mut map = self.cells.lock().unwrap(); let mut map = self.cells.lock().unwrap();
let cell = map.entry((task, index)).or_default(); let cell = map.entry((task, index)).or_default();
*cell = content; *cell = content;
......
use std::{fmt::Debug, future::Future, sync::Arc}; use std::{env, fmt::Debug, future::Future, sync::Arc};
use anyhow::Result; use anyhow::Result;
use turbo_tasks::{TurboTasksApi, trace::TraceRawVcs}; use turbo_tasks::{TurboTasksApi, trace::TraceRawVcs};
...@@ -98,22 +98,28 @@ where ...@@ -98,22 +98,28 @@ where
F: Future<Output = Result<T>> + Send + 'static, F: Future<Output = Result<T>> + Send + 'static,
T: Debug + PartialEq + Eq + TraceRawVcs + Send + 'static, T: Debug + PartialEq + Eq + TraceRawVcs + Send + 'static,
{ {
let single_run = env::var("SINGLE_RUN").is_ok();
let name = closure_to_name(&fut); let name = closure_to_name(&fut);
let tt = registration.create_turbo_tasks(&name, true); let tt = registration.create_turbo_tasks(&name, true);
println!("Run #1 (without cache)"); println!("Run #1 (without cache)");
let start = std::time::Instant::now(); let start = std::time::Instant::now();
let first = fut(tt.clone()).await?; let first = fut(tt.clone()).await?;
println!("Run #1 took {:?}", start.elapsed()); println!("Run #1 took {:?}", start.elapsed());
for i in 2..10 { if !single_run {
println!("Run #{i} (with memory cache, same TurboTasks instance)"); for i in 2..10 {
let start = std::time::Instant::now(); println!("Run #{i} (with memory cache, same TurboTasks instance)");
let second = fut(tt.clone()).await?; let start = std::time::Instant::now();
println!("Run #{i} took {:?}", start.elapsed()); let second = fut(tt.clone()).await?;
assert_eq!(first, second); println!("Run #{i} took {:?}", start.elapsed());
assert_eq!(first, second);
}
} }
let start = std::time::Instant::now(); let start = std::time::Instant::now();
tt.stop_and_wait().await; tt.stop_and_wait().await;
println!("Stopping TurboTasks took {:?}", start.elapsed()); println!("Stopping TurboTasks took {:?}", start.elapsed());
if single_run {
return Ok(());
}
for i in 10..20 { for i in 10..20 {
let tt = registration.create_turbo_tasks(&name, false); let tt = registration.create_turbo_tasks(&name, false);
println!("Run #{i} (with filesystem cache if available, new TurboTasks instance)"); println!("Run #{i} (with filesystem cache if available, new TurboTasks instance)");
......
...@@ -497,6 +497,11 @@ impl From<anyhow::Error> for TurboTasksExecutionError { ...@@ -497,6 +497,11 @@ impl From<anyhow::Error> for TurboTasksExecutionError {
} }
} }
pub enum VerificationMode {
EqualityCheck,
Skip,
}
pub trait Backend: Sync + Send { pub trait Backend: Sync + Send {
#[allow(unused_variables)] #[allow(unused_variables)]
fn startup(&self, turbo_tasks: &dyn TurboTasksBackendApi<Self>) {} fn startup(&self, turbo_tasks: &dyn TurboTasksBackendApi<Self>) {}
...@@ -621,6 +626,7 @@ pub trait Backend: Sync + Send { ...@@ -621,6 +626,7 @@ pub trait Backend: Sync + Send {
task: TaskId, task: TaskId,
index: CellId, index: CellId,
content: CellContent, content: CellContent,
verification_mode: VerificationMode,
turbo_tasks: &dyn TurboTasksBackendApi<Self>, turbo_tasks: &dyn TurboTasksBackendApi<Self>,
); );
......
...@@ -25,7 +25,7 @@ use crate::{ ...@@ -25,7 +25,7 @@ use crate::{
VcValueTrait, VcValueType, VcValueTrait, VcValueType,
backend::{ backend::{
Backend, CachedTaskType, CellContent, TaskCollectiblesMap, TaskExecutionSpec, Backend, CachedTaskType, CellContent, TaskCollectiblesMap, TaskExecutionSpec,
TransientTaskType, TurboTasksExecutionError, TypedCellContent, TransientTaskType, TurboTasksExecutionError, TypedCellContent, VerificationMode,
}, },
capture_future::CaptureFuture, capture_future::CaptureFuture,
event::{Event, EventListener}, event::{Event, EventListener},
...@@ -155,7 +155,13 @@ pub trait TurboTasksApi: TurboTasksCallApi + Sync + Send { ...@@ -155,7 +155,13 @@ pub trait TurboTasksApi: TurboTasksCallApi + Sync + Send {
index: CellId, index: CellId,
options: ReadCellOptions, options: ReadCellOptions,
) -> Result<TypedCellContent>; ) -> Result<TypedCellContent>;
fn update_own_task_cell(&self, task: TaskId, index: CellId, content: CellContent); fn update_own_task_cell(
&self,
task: TaskId,
index: CellId,
content: CellContent,
verification_mode: VerificationMode,
);
fn mark_own_task_as_finished(&self, task: TaskId); fn mark_own_task_as_finished(&self, task: TaskId);
fn set_own_task_aggregation_number(&self, task: TaskId, aggregation_number: u32); fn set_own_task_aggregation_number(&self, task: TaskId, aggregation_number: u32);
fn mark_own_task_as_session_dependent(&self, task: TaskId); fn mark_own_task_as_session_dependent(&self, task: TaskId);
...@@ -1354,8 +1360,15 @@ impl<B: Backend + 'static> TurboTasksApi for TurboTasks<B> { ...@@ -1354,8 +1360,15 @@ impl<B: Backend + 'static> TurboTasksApi for TurboTasks<B> {
self.try_read_own_task_cell(task, index, options) self.try_read_own_task_cell(task, index, options)
} }
fn update_own_task_cell(&self, task: TaskId, index: CellId, content: CellContent) { fn update_own_task_cell(
self.backend.update_task_cell(task, index, content, self); &self,
task: TaskId,
index: CellId,
content: CellContent,
verification_mode: VerificationMode,
) {
self.backend
.update_task_cell(task, index, content, verification_mode, self);
} }
fn connect_task(&self, task: TaskId) { fn connect_task(&self, task: TaskId) {
...@@ -1769,7 +1782,12 @@ impl CurrentCellRef { ...@@ -1769,7 +1782,12 @@ impl CurrentCellRef {
.ok(); .ok();
let update = functor(cell_content.as_ref().and_then(|cc| cc.1.0.as_ref())); let update = functor(cell_content.as_ref().and_then(|cc| cc.1.0.as_ref()));
if let Some(update) = update { if let Some(update) = update {
tt.update_own_task_cell(self.current_task, self.index, CellContent(Some(update))) tt.update_own_task_cell(
self.current_task,
self.index,
CellContent(Some(update)),
VerificationMode::EqualityCheck,
)
} }
} }
...@@ -1851,7 +1869,7 @@ impl CurrentCellRef { ...@@ -1851,7 +1869,7 @@ impl CurrentCellRef {
} }
/// Unconditionally updates the content of the cell. /// Unconditionally updates the content of the cell.
pub fn update<T>(&self, new_value: T) pub fn update<T>(&self, new_value: T, verification_mode: VerificationMode)
where where
T: VcValueType, T: VcValueType,
{ {
...@@ -1862,6 +1880,7 @@ impl CurrentCellRef { ...@@ -1862,6 +1880,7 @@ impl CurrentCellRef {
CellContent(Some(SharedReference::new(triomphe::Arc::new( CellContent(Some(SharedReference::new(triomphe::Arc::new(
<T::Read as VcRead<T>>::value_to_repr(new_value), <T::Read as VcRead<T>>::value_to_repr(new_value),
)))), )))),
verification_mode,
) )
} }
...@@ -1873,27 +1892,40 @@ impl CurrentCellRef { ...@@ -1873,27 +1892,40 @@ impl CurrentCellRef {
/// ///
/// The [`SharedReference`] is expected to use the `<T::Read as /// The [`SharedReference`] is expected to use the `<T::Read as
/// VcRead<T>>::Repr` type for its representation of the value. /// VcRead<T>>::Repr` type for its representation of the value.
pub fn update_with_shared_reference(&self, shared_ref: SharedReference) { pub fn update_with_shared_reference(
&self,
shared_ref: SharedReference,
verification_mode: VerificationMode,
) {
let tt = turbo_tasks(); let tt = turbo_tasks();
let content = tt let update = if matches!(verification_mode, VerificationMode::EqualityCheck) {
.read_own_task_cell( let content = tt
self.current_task, .read_own_task_cell(
self.index, self.current_task,
ReadCellOptions { self.index,
// INVALIDATION: Reading our own cell must be untracked ReadCellOptions {
tracking: ReadTracking::Untracked, // INVALIDATION: Reading our own cell must be untracked
..Default::default() tracking: ReadTracking::Untracked,
}, ..Default::default()
) },
.ok(); )
let update = if let Some(TypedCellContent(_, CellContent(Some(shared_ref_exp)))) = content { .ok();
// pointer equality (not value equality) if let Some(TypedCellContent(_, CellContent(Some(shared_ref_exp)))) = content {
shared_ref_exp != shared_ref // pointer equality (not value equality)
shared_ref_exp != shared_ref
} else {
true
}
} else { } else {
true true
}; };
if update { if update {
tt.update_own_task_cell(self.current_task, self.index, CellContent(Some(shared_ref))) tt.update_own_task_cell(
self.current_task,
self.index,
CellContent(Some(shared_ref)),
verification_mode,
)
} }
} }
} }
......
use std::{any::type_name, marker::PhantomData}; use std::{any::type_name, marker::PhantomData};
use super::{read::VcRead, traits::VcValueType}; use super::{read::VcRead, traits::VcValueType};
use crate::{RawVc, Vc, manager::find_cell_by_type, task::shared_reference::TypedSharedReference}; use crate::{
RawVc, Vc, backend::VerificationMode, manager::find_cell_by_type,
task::shared_reference::TypedSharedReference,
};
type VcReadTarget<T> = <<T as VcValueType>::Read as VcRead<T>>::Target; type VcReadTarget<T> = <<T as VcValueType>::Read as VcRead<T>>::Target;
type VcReadRepr<T> = <<T as VcValueType>::Read as VcRead<T>>::Repr; type VcReadRepr<T> = <<T as VcValueType>::Read as VcRead<T>>::Repr;
...@@ -37,7 +40,10 @@ where ...@@ -37,7 +40,10 @@ where
{ {
fn cell(inner: VcReadTarget<T>) -> Vc<T> { fn cell(inner: VcReadTarget<T>) -> Vc<T> {
let cell = find_cell_by_type(T::get_value_type_id()); let cell = find_cell_by_type(T::get_value_type_id());
cell.update(<T::Read as VcRead<T>>::target_to_value(inner)); cell.update(
<T::Read as VcRead<T>>::target_to_value(inner),
VerificationMode::Skip,
);
Vc { Vc {
node: cell.into(), node: cell.into(),
_t: PhantomData, _t: PhantomData,
...@@ -47,7 +53,7 @@ where ...@@ -47,7 +53,7 @@ where
fn raw_cell(content: TypedSharedReference) -> RawVc { fn raw_cell(content: TypedSharedReference) -> RawVc {
debug_assert_repr::<T>(&content); debug_assert_repr::<T>(&content);
let cell = find_cell_by_type(content.type_id); let cell = find_cell_by_type(content.type_id);
cell.update_with_shared_reference(content.reference); cell.update_with_shared_reference(content.reference, VerificationMode::Skip);
cell.into() cell.into()
} }
} }
......
...@@ -16,8 +16,8 @@ use serde_json::Value as JsonValue; ...@@ -16,8 +16,8 @@ use serde_json::Value as JsonValue;
use turbo_rcstr::rcstr; use turbo_rcstr::rcstr;
use turbo_tasks::{ use turbo_tasks::{
Completion, Effects, FxIndexMap, NonLocalValue, OperationVc, RawVc, ReadRef, ResolvedVc, Completion, Effects, FxIndexMap, NonLocalValue, OperationVc, RawVc, ReadRef, ResolvedVc,
TaskInput, TryJoinIterExt, Vc, VcValueType, duration_span, fxindexmap, get_effects, TaskInput, TryJoinIterExt, Vc, VcValueType, backend::VerificationMode, duration_span,
mark_finished, prevent_gc, trace::TraceRawVcs, util::SharedError, fxindexmap, get_effects, mark_finished, prevent_gc, trace::TraceRawVcs, util::SharedError,
}; };
use turbo_tasks_bytes::{Bytes, Stream}; use turbo_tasks_bytes::{Bytes, Stream};
use turbo_tasks_env::{EnvMap, ProcessEnv}; use turbo_tasks_env::{EnvMap, ProcessEnv};
...@@ -401,10 +401,10 @@ pub async fn custom_evaluate( ...@@ -401,10 +401,10 @@ pub async fn custom_evaluate(
// We initialize the cell with a stream that is open, but has no values. // We initialize the cell with a stream that is open, but has no values.
// The first [compute_evaluate_stream] pipe call will pick up that stream. // The first [compute_evaluate_stream] pipe call will pick up that stream.
let (sender, receiver) = unbounded(); let (sender, receiver) = unbounded();
cell.update(JavaScriptEvaluation(JavaScriptStream::new_open( cell.update(
vec![], JavaScriptEvaluation(JavaScriptStream::new_open(vec![], Box::new(receiver))),
Box::new(receiver), VerificationMode::Skip,
))); );
let initial = Mutex::new(Some(sender)); let initial = Mutex::new(Some(sender));
// run the evaluation as side effect // run the evaluation as side effect
...@@ -418,10 +418,13 @@ pub async fn custom_evaluate( ...@@ -418,10 +418,13 @@ pub async fn custom_evaluate(
// In cases when only [compute_evaluate_stream] is (re)executed, we need to // In cases when only [compute_evaluate_stream] is (re)executed, we need to
// update the old stream with a new value. // update the old stream with a new value.
let (sender, receiver) = unbounded(); let (sender, receiver) = unbounded();
cell.update(JavaScriptEvaluation(JavaScriptStream::new_open( cell.update(
vec![], JavaScriptEvaluation(JavaScriptStream::new_open(
Box::new(receiver), vec![],
))); Box::new(receiver),
)),
VerificationMode::Skip,
);
sender sender
} }
}), }),
......
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