Unverified Commit 4e4632c2 authored by Benjamin Woodruff's avatar Benjamin Woodruff Committed by GitHub

refactor(turbo-tasks): Add TaskPersistence enum for task creation functions (#68866)

Builds on top of the `is_transient` boolean arguments from #68436, but
uses an enum to provide a third variant of
`TaskPersistence::LocalCells`.

The branches for this are `todo!()` right now, but the idea is that
we'll intercept these in the manager and schedule them inside the
manager instead of creating a unique task with its own `TaskId` on the
backend.

## Testing

Some of the `local_cell` tests are broken with this change, as functions
annotated with `#[turbo_tasks::function(local_cells)]` now hit the
`todo!()` blocks added in this PR. Those tests are disabled for now, and
I'll fix this in a later PR. *(This is okay, nothing is using local
cells yet, aside from those tests)*
parent 2865e182
use std::collections::HashSet;
use proc_macro2::{Ident, Span, TokenStream};
use quote::{quote, quote_spanned};
use quote::{quote, quote_spanned, ToTokens};
use syn::{
parenthesized,
parse::{Parse, ParseStream},
......@@ -24,6 +24,8 @@ pub struct TurboFn {
inputs: Vec<Input>,
/// Should we check that the return type contains a `ResolvedValue`?
resolved: Option<Span>,
/// Should this function use `TaskPersistence::LocalCells`?
local_cells: bool,
}
#[derive(Debug)]
......@@ -257,6 +259,7 @@ impl TurboFn {
this,
inputs,
resolved: args.resolved,
local_cells: args.local_cells.is_some(),
})
}
......@@ -301,17 +304,26 @@ impl TurboFn {
}
}
fn inputs(&self) -> Vec<&Ident> {
self.inputs
.iter()
.map(|Input { ident, .. }| ident)
.collect()
fn input_idents(&self) -> impl Iterator<Item = &Ident> {
self.inputs.iter().map(|Input { ident, .. }| ident)
}
pub fn input_types(&self) -> Vec<&Type> {
self.inputs.iter().map(|Input { ty, .. }| ty).collect()
}
pub fn persistence(&self) -> impl ToTokens {
if self.local_cells {
quote! {
turbo_tasks::TaskPersistence::LocalCells
}
} else {
quote! {
turbo_tasks::macro_helpers::get_non_local_persistence_from_inputs(&*inputs)
}
}
}
fn converted_this(&self) -> Option<Expr> {
self.this.as_ref().map(|Input { ty: _, ident }| {
parse_quote! {
......@@ -344,31 +356,33 @@ impl TurboFn {
/// The block of the exposed function for a dynamic dispatch call to the
/// given trait.
pub fn dynamic_block(&self, trait_type_id_ident: &Ident) -> Block {
let Some(converted_this) = self.converted_this() else {
return parse_quote! {
{
unimplemented!("trait methods without self are not yet supported")
}
};
};
let ident = &self.ident;
let output = &self.output;
let assertions = self.get_assertions();
if let Some(converted_this) = self.converted_this() {
let inputs = self.inputs();
parse_quote! {
{
#assertions
let turbo_tasks_transient = #( turbo_tasks::TaskInput::is_transient(&#inputs) ||)* false;
<#output as turbo_tasks::task::TaskOutput>::try_from_raw_vc(
turbo_tasks::trait_call(
*#trait_type_id_ident,
std::borrow::Cow::Borrowed(stringify!(#ident)),
#converted_this,
Box::new((#(#inputs,)*)) as Box<dyn turbo_tasks::MagicAny>,
turbo_tasks_transient,
)
let inputs = self.input_idents();
let persistence = self.persistence();
parse_quote! {
{
#assertions
let inputs = std::boxed::Box::new((#(#inputs,)*));
let persistence = #persistence;
<#output as turbo_tasks::task::TaskOutput>::try_from_raw_vc(
turbo_tasks::trait_call(
*#trait_type_id_ident,
std::borrow::Cow::Borrowed(stringify!(#ident)),
#converted_this,
inputs as std::boxed::Box<dyn turbo_tasks::MagicAny>,
persistence,
)
}
}
} else {
parse_quote! {
{
unimplemented!("trait methods without self are not yet supported")
}
)
}
}
}
......@@ -377,19 +391,21 @@ impl TurboFn {
/// given native function.
pub fn static_block(&self, native_function_id_ident: &Ident) -> Block {
let output = &self.output;
let inputs = self.inputs();
let inputs = self.input_idents();
let persistence = self.persistence();
let assertions = self.get_assertions();
if let Some(converted_this) = self.converted_this() {
parse_quote! {
{
#assertions
let turbo_tasks_transient = #( turbo_tasks::TaskInput::is_transient(&#inputs) ||)* false;
let inputs = std::boxed::Box::new((#(#inputs,)*));
let persistence = #persistence;
<#output as turbo_tasks::task::TaskOutput>::try_from_raw_vc(
turbo_tasks::dynamic_this_call(
*#native_function_id_ident,
#converted_this,
Box::new((#(#inputs,)*)) as Box<dyn turbo_tasks::MagicAny>,
turbo_tasks_transient
inputs as std::boxed::Box<dyn turbo_tasks::MagicAny>,
persistence,
)
)
}
......@@ -398,12 +414,13 @@ impl TurboFn {
parse_quote! {
{
#assertions
let turbo_tasks_transient = #( turbo_tasks::TaskInput::is_transient(&#inputs) ||)* false;
let inputs = std::boxed::Box::new((#(#inputs,)*));
let persistence = #persistence;
<#output as turbo_tasks::task::TaskOutput>::try_from_raw_vc(
turbo_tasks::dynamic_call(
*#native_function_id_ident,
Box::new((#(#inputs,)*)) as Box<dyn turbo_tasks::MagicAny>,
turbo_tasks_transient,
inputs as std::boxed::Box<dyn turbo_tasks::MagicAny>,
persistence,
)
)
}
......
......@@ -810,7 +810,7 @@ impl Task {
native_fn_id,
*this,
&**arg,
self.id.is_transient(),
self.id.persistence(),
turbo_tasks,
));
drop(entered);
......@@ -833,7 +833,7 @@ impl Task {
name,
*this,
&**arg,
self.id.is_transient(),
self.id.persistence(),
turbo_tasks,
));
drop(entered);
......
../../turbo-tasks-testing/tests/local_cell.rs
\ No newline at end of file
......@@ -20,8 +20,8 @@ use turbo_tasks::{
registry,
test_helpers::with_turbo_tasks_for_testing,
util::{SharedError, StaticOrArc},
CellId, ExecutionId, InvalidationReason, MagicAny, RawVc, TaskId, TraitTypeId, TurboTasksApi,
TurboTasksCallApi,
CellId, ExecutionId, InvalidationReason, MagicAny, RawVc, TaskId, TaskPersistence, TraitTypeId,
TurboTasksApi, TurboTasksCallApi,
};
pub use crate::run::{run, run_without_cache_check, Registration};
......@@ -92,7 +92,7 @@ impl TurboTasksCallApi for VcStorage {
&self,
func: turbo_tasks::FunctionId,
arg: Box<dyn MagicAny>,
_is_transient: bool,
_persistence: TaskPersistence,
) -> RawVc {
self.dynamic_call(func, None, arg)
}
......@@ -102,7 +102,7 @@ impl TurboTasksCallApi for VcStorage {
func: turbo_tasks::FunctionId,
this_arg: RawVc,
arg: Box<dyn MagicAny>,
_is_transient: bool,
_persistence: TaskPersistence,
) -> RawVc {
self.dynamic_call(func, Some(this_arg), arg)
}
......@@ -111,7 +111,7 @@ impl TurboTasksCallApi for VcStorage {
&self,
_func: turbo_tasks::FunctionId,
_arg: Box<dyn MagicAny>,
_is_transient: bool,
_persistence: TaskPersistence,
) -> RawVc {
unreachable!()
}
......@@ -121,7 +121,7 @@ impl TurboTasksCallApi for VcStorage {
_func: turbo_tasks::FunctionId,
_this: RawVc,
_arg: Box<dyn MagicAny>,
_is_transient: bool,
_persistence: TaskPersistence,
) -> RawVc {
unreachable!()
}
......@@ -132,7 +132,7 @@ impl TurboTasksCallApi for VcStorage {
_trait_fn_name: Cow<'static, str>,
_this: RawVc,
_arg: Box<dyn MagicAny>,
_is_transient: bool,
_persistence: TaskPersistence,
) -> RawVc {
unreachable!()
}
......
#![feature(arbitrary_self_types)]
use anyhow::Result;
use turbo_tasks::{
debug::ValueDebug, test_helpers::current_task_for_testing, ResolvedValue, ValueDefault, Vc,
};
......@@ -14,8 +15,8 @@ struct Wrapper(u32);
struct TransparentWrapper(u32);
#[tokio::test]
async fn test_store_and_read() {
run(&REGISTRATION, async {
async fn test_store_and_read() -> Result<()> {
run(&REGISTRATION, || async {
let a: Vc<u32> = Vc::local_cell(42);
assert_eq!(*a.await.unwrap(), 42);
......@@ -24,13 +25,15 @@ async fn test_store_and_read() {
let c = TransparentWrapper(42).local_cell();
assert_eq!(*c.await.unwrap(), 42);
Ok(())
})
.await
}
#[tokio::test]
async fn test_store_and_read_generic() {
run(&REGISTRATION, async {
async fn test_store_and_read_generic() -> Result<()> {
run(&REGISTRATION, || async {
// `Vc<Vec<Vc<T>>>` is stored as `Vc<Vec<Vc<()>>>` and requires special
// transmute handling
let cells: Vc<Vec<Vc<u32>>> =
......@@ -42,6 +45,8 @@ async fn test_store_and_read_generic() {
}
assert_eq!(output, vec![1, 2, 3]);
Ok(())
})
.await
}
......@@ -53,10 +58,12 @@ async fn returns_resolved_local_vc() -> Vc<u32> {
cell.resolve().await.unwrap()
}
#[ignore]
#[tokio::test]
async fn test_return_resolved() {
run(&REGISTRATION, async {
async fn test_return_resolved() -> Result<()> {
run(&REGISTRATION, || async {
assert_eq!(*returns_resolved_local_vc().await.unwrap(), 42);
Ok(())
})
.await
}
......@@ -65,8 +72,8 @@ async fn test_return_resolved() {
trait UnimplementedTrait {}
#[tokio::test]
async fn test_try_resolve_sidecast() {
run(&REGISTRATION, async {
async fn test_try_resolve_sidecast() -> Result<()> {
run(&REGISTRATION, || async {
let trait_vc: Vc<Box<dyn ValueDebug>> = Vc::upcast(Vc::<u32>::local_cell(42));
// `u32` is both a `ValueDebug` and a `ValueDefault`, so this sidecast is valid
......@@ -80,13 +87,15 @@ async fn test_try_resolve_sidecast() {
.await
.unwrap();
assert!(wrongly_sidecast_vc.is_none());
Ok(())
})
.await
}
#[tokio::test]
async fn test_try_resolve_downcast_type() {
run(&REGISTRATION, async {
async fn test_try_resolve_downcast_type() -> Result<()> {
run(&REGISTRATION, || async {
let trait_vc: Vc<Box<dyn ValueDebug>> = Vc::upcast(Vc::<u32>::local_cell(42));
let downcast_vc: Vc<u32> = Vc::try_resolve_downcast_type(trait_vc)
......@@ -98,16 +107,19 @@ async fn test_try_resolve_downcast_type() {
let wrongly_downcast_vc: Option<Vc<i64>> =
Vc::try_resolve_downcast_type(trait_vc).await.unwrap();
assert!(wrongly_downcast_vc.is_none());
Ok(())
})
.await
}
#[tokio::test]
async fn test_get_task_id() {
run(&REGISTRATION, async {
async fn test_get_task_id() -> Result<()> {
run(&REGISTRATION, || async {
// the task id as reported by the RawVc
let vc_task_id = Vc::into_raw(Vc::<()>::local_cell(())).get_task_id();
assert_eq!(vc_task_id, current_task_for_testing());
Ok(())
})
.await
}
......@@ -139,25 +151,31 @@ async fn get_untracked_local_cell() -> Vc<Untracked> {
.unwrap()
}
#[ignore]
#[tokio::test]
#[should_panic(expected = "Local Vcs must only be accessed within their own task")]
async fn test_panics_on_local_cell_escape_read() {
run(&REGISTRATION, async {
run(&REGISTRATION, || async {
get_untracked_local_cell()
.await
.unwrap()
.cell
.await
.unwrap();
Ok(())
})
.await
.unwrap()
}
#[ignore]
#[tokio::test]
#[should_panic(expected = "Local Vcs must only be accessed within their own task")]
async fn test_panics_on_local_cell_escape_get_task_id() {
run(&REGISTRATION, async {
run(&REGISTRATION, || async {
Vc::into_raw(get_untracked_local_cell().await.unwrap().cell).get_task_id();
Ok(())
})
.await
.unwrap()
}
......@@ -23,8 +23,8 @@ use crate::{
task::shared_reference::TypedSharedReference,
trait_helpers::{get_trait_method, has_trait, traits},
triomphe_utils::unchecked_sidecast_triomphe_arc,
FunctionId, RawVc, ReadRef, SharedReference, TaskId, TaskIdSet, TraitRef, TraitTypeId,
ValueTypeId, VcRead, VcValueTrait, VcValueType,
FunctionId, RawVc, ReadRef, SharedReference, TaskId, TaskIdSet, TaskPersistence, TraitRef,
TraitTypeId, ValueTypeId, VcRead, VcValueTrait, VcValueType,
};
type TransientTaskRoot =
......@@ -608,7 +608,7 @@ impl CachedTaskType {
fn_id: FunctionId,
mut this: Option<RawVc>,
arg: &dyn MagicAny,
is_transient: bool,
persistence: TaskPersistence,
turbo_tasks: Arc<dyn TurboTasksBackendApi<B>>,
) -> Result<RawVc> {
if let Some(this) = this.as_mut() {
......@@ -616,9 +616,9 @@ impl CachedTaskType {
}
let arg = registry::get_function(fn_id).arg_meta.resolve(arg).await?;
Ok(if let Some(this) = this {
turbo_tasks.this_call(fn_id, this, arg, is_transient)
turbo_tasks.this_call(fn_id, this, arg, persistence)
} else {
turbo_tasks.native_call(fn_id, arg, is_transient)
turbo_tasks.native_call(fn_id, arg, persistence)
})
}
......@@ -636,7 +636,7 @@ impl CachedTaskType {
name: Cow<'static, str>,
this: RawVc,
arg: &dyn MagicAny,
is_transient: bool,
persistence: TaskPersistence,
turbo_tasks: Arc<dyn TurboTasksBackendApi<B>>,
) -> Result<RawVc> {
let this = this.resolve().await?;
......@@ -647,7 +647,7 @@ impl CachedTaskType {
.arg_meta
.resolve(arg)
.await?;
Ok(turbo_tasks.dynamic_this_call(native_fn, this, arg, is_transient))
Ok(turbo_tasks.dynamic_this_call(native_fn, this, arg, persistence))
}
/// Shared helper used by [`Self::resolve_trait_method`] and
......
......@@ -7,7 +7,7 @@ use std::{
use serde::{de::Visitor, Deserialize, Serialize};
use crate::registry;
use crate::{registry, TaskPersistence};
macro_rules! define_id {
($name:ident : $primitive:ty $(,derive($($derive:ty),*))?) => {
......@@ -84,6 +84,14 @@ impl TaskId {
pub fn is_transient(&self) -> bool {
**self & TRANSIENT_TASK_BIT != 0
}
pub fn persistence(&self) -> TaskPersistence {
// tasks with `TaskPersistence::LocalCells` have no `TaskId`, so we can ignore that case
if self.is_transient() {
TaskPersistence::Transient
} else {
TaskPersistence::Persistent
}
}
}
macro_rules! make_serializable {
......
......@@ -90,8 +90,8 @@ pub use magic_any::MagicAny;
pub use manager::{
dynamic_call, dynamic_this_call, emit, get_invalidator, mark_finished, mark_stateful,
prevent_gc, run_once, run_once_with_reason, spawn_blocking, spawn_thread, trait_call,
turbo_tasks, CurrentCellRef, Invalidator, TurboTasks, TurboTasksApi, TurboTasksBackendApi,
TurboTasksCallApi, Unused, UpdateInfo,
turbo_tasks, CurrentCellRef, Invalidator, TaskPersistence, TurboTasks, TurboTasksApi,
TurboTasksBackendApi, TurboTasksCallApi, Unused, UpdateInfo,
};
pub use native_function::{FunctionMeta, NativeFunction};
pub use raw_vc::{CellId, RawVc, ReadRawVcFuture, ResolveTypeError};
......
......@@ -8,7 +8,7 @@ pub use super::{
magic_any::MagicAny,
manager::{find_cell_by_type, notify_scheduled_tasks, spawn_detached_for_testing},
};
use crate::debug::ValueDebugFormatString;
use crate::{debug::ValueDebugFormatString, TaskInput, TaskPersistence};
#[inline(never)]
pub async fn value_debug_format_field(value: ValueDebugFormatString<'_>) -> String {
......@@ -21,6 +21,14 @@ pub async fn value_debug_format_field(value: ValueDebugFormatString<'_>) -> Stri
}
}
pub fn get_non_local_persistence_from_inputs(inputs: &impl TaskInput) -> TaskPersistence {
if inputs.is_transient() {
TaskPersistence::Transient
} else {
TaskPersistence::Persistent
}
}
#[macro_export]
macro_rules! stringify_path {
($path:path) => {
......
This diff is collapsed.
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