Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 47 additions & 1 deletion components/salsa-macro-rules/src/maybe_backdate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,53 @@ macro_rules! maybe_backdate {
$zalsa:ident,
) => {
if $maybe_update(std::ptr::addr_of_mut!($old_field_place), $new_field_place) {
$revision_place = $current_revision;
$revision_place.store($current_revision);
}
};
}

/// Conditionally update field value and backdate revisions
#[macro_export]
macro_rules! maybe_backdate_late {
(
($return_mode:ident, no_backdate, $maybe_default:ident),
$maybe_update:tt,
$old_field_place:expr,
$new_field_place:expr,
$revision_place:expr,
$current_revision:expr,
$zalsa:ident,

) => {
$zalsa::always_update(
&mut $revision_place,
$current_revision,
&mut $old_field_place,
$new_field_place,
);
};

(
($return_mode:ident, backdate, $maybe_default:ident),
$maybe_update:tt,
$old_field_place:expr,
$new_field_place:expr,
$revision_place:expr,
$current_revision:expr,
$deps_changed_at:expr,
$zalsa:ident,
) => {
$revision_place.non_atomic_store(
match $zalsa::LateField::maybe_update(
&mut $old_field_place,
$new_field_place,
$maybe_update,
$revision_place.load(),
) {
$zalsa::late_field::UpdateResult::Dirty => $current_revision,
$zalsa::late_field::UpdateResult::Update => $deps_changed_at,
$zalsa::late_field::UpdateResult::Backdate(rev) => rev,
},
)
};
}
146 changes: 126 additions & 20 deletions components/salsa-macro-rules/src/setup_tracked_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,18 @@ macro_rules! setup_tracked_struct {
// Tracked field names.
tracked_ids: [$($tracked_id:ident),*],

// Non late field names.
non_late_ids: [$($non_late_id:ident),*],

// Visibility and names of tracked fields.
tracked_getters: [$($tracked_getter_vis:vis $tracked_getter_id:ident),*],

// Visibility and names of untracked fields.
untracked_getters: [$($untracked_getter_vis:vis $untracked_getter_id:ident),*],

// Names of setters for late fields.
tracked_setters: [$($tracked_setter_id:ident),*],

// Field types, may reference `db_lt`.
field_tys: [$($field_ty:ty),*],

Expand All @@ -38,6 +44,9 @@ macro_rules! setup_tracked_struct {
// Untracked field types.
untracked_tys: [$($untracked_ty:ty),*],

// Non late field types.
non_late_tys: [$($non_late_ty:ty),*],

// Indices for each field from 0..N -- must be unsuffixed (e.g., `0`, `1`).
field_indices: [$($field_index:tt),*],

Expand All @@ -56,12 +65,17 @@ macro_rules! setup_tracked_struct {
// Untracked field types.
untracked_maybe_updates: [$($untracked_maybe_update:tt),*],

// If tracked field can be set after new.
field_is_late: [$($field_is_late:tt),*],
tracked_is_late: [$($tracked_is_late:tt),*],

// A set of "field options" for each tracked field.
//
// Each field option is a tuple `(return_mode, maybe_backdate)` where:
//
// * `return_mode` is an identifier as specified in `salsa_macros::options::Option::returns`
// * `maybe_backdate` is either the identifier `backdate` or `no_backdate`
// * `maybe_backdate` is either the identifier `backdate` or `no_backdate`
//
// These are used to drive conditional logic for each field via recursive macro invocation
// (see e.g. @return_mode below).
Expand Down Expand Up @@ -116,6 +130,8 @@ macro_rules! setup_tracked_struct {

type $Configuration = $Struct<'static>;



impl $zalsa_struct::Configuration for $Configuration {
const LOCATION: $zalsa::Location = $zalsa::Location {
file: file!(),
Expand All @@ -131,9 +147,9 @@ macro_rules! setup_tracked_struct {
$($relative_tracked_index,)*
];

type Fields<$db_lt> = ($($field_ty,)*);
type Fields<$db_lt> = ($($zalsa::macro_if!(if $field_is_late { $zalsa::LateField<$field_ty> } else { $field_ty }),)*);

type Revisions = [$Revision; $N];
type Revisions = [$zalsa::MaybeAtomicRevision; $N];

type Struct<$db_lt> = $Struct<$db_lt>;

Expand All @@ -142,27 +158,78 @@ macro_rules! setup_tracked_struct {
}

fn new_revisions(current_revision: $Revision) -> Self::Revisions {
[current_revision; $N]
std::array::from_fn(|_| $zalsa::MaybeAtomicRevision::from(current_revision))
}

#[inline(always)]
fn field_revision_raw(data: &Self::Revisions, i: usize) -> &$zalsa::MaybeAtomicRevision {
&data[i]
}

#[inline(always)]
fn field_revision(data: &Self::Revisions, i: usize) -> $Revision {
let raw = Self::field_revision_raw(data, i);

if Self::field_is_late(i) {
raw.load()
} else {
// SAFETY: there is no writes to non-late field revision
unsafe {
raw.non_atomic_load()
}
}
}

#[inline(always)]
fn field_durability(base: $zalsa::Durability, relative_tracked_index: usize) -> $zalsa::Durability {
if Self::field_is_late(relative_tracked_index) {
$zalsa::Durability::LOW
} else {
base
}
}

fn field_is_late(relative_tracked_index: usize) -> bool {
$(if $tracked_is_late && (relative_tracked_index == $relative_tracked_index) {
return true;
})*
false
}

unsafe fn update_fields<$db_lt>(
current_revision: $Revision,
deps_changed_at: $Revision,
revisions: &mut Self::Revisions,
old_fields: *mut Self::Fields<$db_lt>,
new_fields: Self::Fields<$db_lt>,
) -> bool {
use $zalsa::UpdateFallback as _;
unsafe {
$(
$crate::maybe_backdate!(
$tracked_option,
$tracked_maybe_update,
(*old_fields).$absolute_tracked_index,
new_fields.$absolute_tracked_index,
revisions[$relative_tracked_index],
current_revision,
$zalsa,
);
$zalsa::macro_if! {
if $tracked_is_late {
$crate::maybe_backdate_late!(
$tracked_option,
$tracked_maybe_update,
(*old_fields).$absolute_tracked_index,
new_fields.$absolute_tracked_index,
revisions[$relative_tracked_index],
current_revision,
deps_changed_at,
$zalsa,
);
} else {
$crate::maybe_backdate!(
$tracked_option,
$tracked_maybe_update,
(*old_fields).$absolute_tracked_index,
new_fields.$absolute_tracked_index,
revisions[$relative_tracked_index],
deps_changed_at,
$zalsa,
);
}
}
)*;

// If any untracked field has changed, return `true`, indicating that the tracked struct
Expand Down Expand Up @@ -254,14 +321,15 @@ macro_rules! setup_tracked_struct {
}

impl<$db_lt> $Struct<$db_lt> {
pub fn $new_fn<$Db>(db: &$db_lt $Db, $($field_id: $field_ty),*) -> Self
pub fn $new_fn<$Db>(db: &$db_lt $Db, $($non_late_id: $non_late_ty),*) -> Self
where
// FIXME(rust-lang/rust#65991): The `db` argument *should* have the type `dyn Database`
$Db: ?Sized + $zalsa::Database,
{
$Configuration::ingredient(db.as_dyn_database()).new_struct(
db.as_dyn_database(),
($($field_id,)*)
let db = db.as_dyn_database();
$Configuration::ingredient(db).new_struct(
db,
($($zalsa::macro_if!(if $field_is_late {$zalsa::LateField::new()} else {$field_id}),)*)
)
}

Expand All @@ -274,11 +342,49 @@ macro_rules! setup_tracked_struct {
{
let db = db.as_dyn_database();
let fields = $Configuration::ingredient(db).tracked_field(db, self, $relative_tracked_index);
$crate::return_mode_expression!(
$tracked_option,
$tracked_ty,
&fields.$absolute_tracked_index,
$zalsa::macro_if! { if $tracked_is_late {
$crate::return_mode_expression!(
$tracked_option,
$tracked_ty,
&fields
.$absolute_tracked_index
.get().expect("can't get late field without initialization"),
)
} else {
$crate::return_mode_expression!(
$tracked_option,
$tracked_ty,
&fields.$absolute_tracked_index,
)
}
}
}
)*

$(
$zalsa::macro_if! { if $tracked_is_late {
pub fn $tracked_setter_id<$Db>(
self,
db: &$db_lt $Db,
value: $tracked_ty,
)
where
// FIXME(rust-lang/rust#65991): The `db` argument *should* have the type `dyn Database`
$Db: ?Sized + $zalsa::Database,
{
let db = db.as_dyn_database();
let ingredient = $Configuration::ingredient(db);
// Safety: we only update late fields
unsafe {
ingredient.update_late_field(db, $zalsa::AsId::as_id(&self),
&ingredient.tracked_field(db, self, $relative_tracked_index).$absolute_tracked_index,
value,
$tracked_maybe_update,
$relative_tracked_index
);
}
}
} else {}
}
)*

Expand Down
49 changes: 49 additions & 0 deletions components/salsa-macros/src/salsa_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ pub(crate) struct SalsaField<'s> {
pub(crate) returns: syn::Ident,
pub(crate) has_no_eq_attr: bool,
pub(crate) maybe_update_attr: Option<(syn::Path, syn::Expr)>,
pub(crate) has_late_attr: bool,
get_name: syn::Ident,
set_name: syn::Ident,
unknown_attrs: Vec<&'s syn::Attribute>,
Expand All @@ -83,6 +84,11 @@ pub(crate) const FIELD_OPTION_ATTRIBUTES: &[(
ef.has_tracked_attr = true;
Ok(())
}),
("late", |_, ef| {
ef.has_tracked_attr = true;
ef.has_late_attr = true;
Ok(())
}),
("default", |_, ef| {
ef.has_default_attr = true;
Ok(())
Expand Down Expand Up @@ -139,6 +145,7 @@ where
this.maybe_disallow_maybe_update_fields()?;
this.maybe_disallow_tracked_fields()?;
this.maybe_disallow_default_fields()?;
// this.disallow_late_id_fields()?;

this.check_generics()?;

Expand Down Expand Up @@ -337,6 +344,12 @@ where
.collect()
}

pub(crate) fn tracked_setter_ids(&self) -> Vec<&syn::Ident> {
self.tracked_fields_iter()
.map(|(_, f)| &f.set_name)
.collect()
}

pub(crate) fn untracked_getter_ids(&self) -> Vec<&syn::Ident> {
self.untracked_fields_iter()
.map(|(_, f)| &f.get_name)
Expand Down Expand Up @@ -431,6 +444,33 @@ where
.enumerate()
.filter(|(_, f)| !f.has_tracked_attr)
}

pub fn non_late_iter(&self) -> impl Iterator<Item = (usize, &SalsaField<'s>)> {
self.fields
.iter()
.enumerate()
.filter(|(_, f)| !f.has_late_attr)
}

pub(crate) fn field_is_late(&self) -> Vec<TokenStream> {
self.fields.iter().map(|f| f.is_late()).collect()
}

pub(crate) fn tracked_is_late(&self) -> Vec<TokenStream> {
self.tracked_fields_iter()
.map(|(_, f)| f.is_late())
.collect()
}

pub(crate) fn non_late_ids(&self) -> Vec<&syn::Ident> {
self.non_late_iter()
.map(|(_, f)| f.field.ident.as_ref().unwrap())
.collect()
}

pub(crate) fn non_late_tys(&self) -> Vec<&syn::Type> {
self.non_late_iter().map(|(_, f)| &f.field.ty).collect()
}
}

impl<'s> SalsaField<'s> {
Expand All @@ -453,6 +493,7 @@ impl<'s> SalsaField<'s> {
returns,
has_default_attr: false,
has_no_eq_attr: false,
has_late_attr: false,
maybe_update_attr: None,
get_name,
set_name,
Expand Down Expand Up @@ -488,6 +529,14 @@ impl<'s> SalsaField<'s> {
Ok(result)
}

fn is_late(&self) -> TokenStream {
if self.has_late_attr {
quote! { true }
} else {
quote! { false }
}
}

fn options(&self) -> TokenStream {
let returns = &self.returns;

Expand Down
Loading
Loading