Skip to content
Merged
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
44 changes: 34 additions & 10 deletions components/salsa-macro-rules/src/setup_tracked_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,21 +254,45 @@ macro_rules! setup_tracked_fn {
struct_index
}
};
let memo_ingredient_indices = From::from((zalsa, struct_index, first_index));

let fn_ingredient = <$zalsa::function::IngredientImpl<$Configuration>>::new(
first_index,
memo_ingredient_indices,
$lru,
zalsa.views().downcaster_for::<dyn $Db>()
);
$zalsa::macro_if! { $needs_interner =>
let intern_ingredient = <$zalsa::interned::IngredientImpl<$Configuration>>::new(
first_index.successor(0)
);
}

let intern_ingredient_memo_types = $zalsa::macro_if! {
if $needs_interner {
Some($zalsa::Ingredient::memo_table_types(&intern_ingredient))
} else {
None
}
};
// SAFETY: We call with the correct memo types.
let memo_ingredient_indices = unsafe {
$zalsa::NewMemoIngredientIndices::create(
zalsa,
struct_index,
first_index,
$zalsa::function::MemoEntryType::of::<$zalsa::function::Memo<$Configuration>>(),
intern_ingredient_memo_types,
)
};

// SAFETY: We pass the MemoEntryType for this Configuration, and we lookup the memo types table correctly.
let fn_ingredient = unsafe {
<$zalsa::function::IngredientImpl<$Configuration>>::new(
first_index,
memo_ingredient_indices,
$lru,
zalsa.views().downcaster_for::<dyn $Db>(),
)
};
$zalsa::macro_if! {
if $needs_interner {
vec![
Box::new(fn_ingredient),
Box::new(<$zalsa::interned::IngredientImpl<$Configuration>>::new(
first_index.successor(0)
)),
Box::new(intern_ingredient),
]
} else {
vec![
Expand Down
6 changes: 6 additions & 0 deletions src/accumulator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ use std::any::{Any, TypeId};
use std::fmt;
use std::marker::PhantomData;
use std::panic::UnwindSafe;
use std::sync::Arc;

use accumulated::{Accumulated, AnyAccumulated};

use crate::function::VerifyResult;
use crate::ingredient::{fmt_index, Ingredient, Jar};
use crate::plumbing::IngredientIndices;
use crate::table::memo::MemoTableTypes;
use crate::zalsa::{IngredientIndex, Zalsa};
use crate::{Database, Id, Revision};

Expand Down Expand Up @@ -110,6 +112,10 @@ impl<A: Accumulator> Ingredient for IngredientImpl<A> {
fn debug_name(&self) -> &'static str {
A::DEBUG_NAME
}

fn memo_table_types(&self) -> Arc<MemoTableTypes> {
unreachable!("accumulator does not allocate pages")
}
}

impl<A> std::fmt::Debug for IngredientImpl<A>
Expand Down
13 changes: 12 additions & 1 deletion src/function.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::any::Any;
use std::fmt;
use std::ptr::NonNull;
use std::sync::Arc;

pub(crate) use maybe_changed_after::VerifyResult;

Expand All @@ -11,6 +12,7 @@ use crate::ingredient::{fmt_index, Ingredient};
use crate::key::DatabaseKeyIndex;
use crate::plumbing::MemoIngredientMap;
use crate::salsa_struct::SalsaStructInDb;
use crate::table::memo::MemoTableTypes;
use crate::table::sync::ClaimResult;
use crate::table::Table;
use crate::views::DatabaseDownCaster;
Expand All @@ -30,6 +32,8 @@ mod maybe_changed_after;
mod memo;
mod specify;

pub type Memo<C> = memo::Memo<<C as Configuration>::Output<'static>>;

pub trait Configuration: Any {
const DEBUG_NAME: &'static str;

Expand Down Expand Up @@ -142,7 +146,10 @@ impl<C> IngredientImpl<C>
where
C: Configuration,
{
pub fn new(
/// # Safety
///
/// `memo_type` and `memo_table_types` must be correct.
pub unsafe fn new(
index: IngredientIndex,
memo_ingredient_indices: <C::SalsaStruct<'static> as SalsaStructInDb>::MemoIngredientMap,
lru: usize,
Expand Down Expand Up @@ -314,6 +321,10 @@ where
C::DEBUG_NAME
}

fn memo_table_types(&self) -> Arc<MemoTableTypes> {
unreachable!("function does not allocate pages")
}

fn cycle_recovery_strategy(&self) -> CycleRecoveryStrategy {
C::CYCLE_STRATEGY
}
Expand Down
6 changes: 3 additions & 3 deletions src/function/memo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::cycle::{CycleHeads, CycleRecoveryStrategy, EMPTY_CYCLE_HEADS};
use crate::function::{Configuration, IngredientImpl};
use crate::key::DatabaseKeyIndex;
use crate::revision::AtomicRevision;
use crate::table::memo::MemoTable;
use crate::table::memo::MemoTableWithTypesMut;
use crate::zalsa::{MemoIngredientIndex, Zalsa};
use crate::zalsa_local::{QueryOrigin, QueryRevisions};
use crate::{Event, EventKind, Id, Revision};
Expand Down Expand Up @@ -84,7 +84,7 @@ impl<C: Configuration> IngredientImpl<C> {
/// with an equivalent memo that has no value. If the memo is untracked, FixpointInitial,
/// or has values assigned as output of another query, this has no effect.
pub(super) fn evict_value_from_memo_for(
table: &mut MemoTable,
table: MemoTableWithTypesMut<'_>,
memo_ingredient_index: MemoIngredientIndex,
) {
let map = |memo: &mut Memo<C::Output<'static>>| {
Expand Down Expand Up @@ -120,7 +120,7 @@ impl<C: Configuration> IngredientImpl<C> {
}

#[derive(Debug)]
pub(super) struct Memo<V> {
pub struct Memo<V> {
/// The result of the query, if we decide to memoize it.
pub(super) value: Option<V>,

Expand Down
4 changes: 4 additions & 0 deletions src/ingredient.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use std::any::{Any, TypeId};
use std::fmt;
use std::sync::Arc;

use crate::accumulator::accumulated_map::{AccumulatedMap, InputAccumulatedValues};
use crate::cycle::CycleRecoveryStrategy;
use crate::function::VerifyResult;
use crate::plumbing::IngredientIndices;
use crate::table::memo::MemoTableTypes;
use crate::table::Table;
use crate::zalsa::{transmute_data_mut_ptr, transmute_data_ptr, IngredientIndex, Zalsa};
use crate::zalsa_local::QueryOrigin;
Expand Down Expand Up @@ -132,6 +134,8 @@ pub trait Ingredient: Any + std::fmt::Debug + Send + Sync {
);
}

fn memo_table_types(&self) -> Arc<MemoTableTypes>;

fn fmt_index(&self, index: crate::Id, fmt: &mut fmt::Formatter<'_>) -> fmt::Result;
// Function ingredient methods

Expand Down
11 changes: 9 additions & 2 deletions src/input.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::any::{Any, TypeId};
use std::fmt;
use std::ops::DerefMut;
use std::sync::Arc;

pub mod input_field;
pub mod setter;
Expand All @@ -14,7 +15,7 @@ use crate::ingredient::{fmt_index, Ingredient};
use crate::input::singleton::{Singleton, SingletonChoice};
use crate::key::DatabaseKeyIndex;
use crate::plumbing::{Jar, Stamp};
use crate::table::memo::MemoTable;
use crate::table::memo::{MemoTable, MemoTableTypes};
use crate::table::sync::SyncTable;
use crate::table::{Slot, Table};
use crate::zalsa::{IngredientIndex, Zalsa};
Expand Down Expand Up @@ -72,6 +73,7 @@ impl<C: Configuration> Jar for JarImpl<C> {
pub struct IngredientImpl<C: Configuration> {
ingredient_index: IngredientIndex,
singleton: C::Singleton,
memo_table_types: Arc<MemoTableTypes>,
_phantom: std::marker::PhantomData<C::Struct>,
}

Expand All @@ -80,6 +82,7 @@ impl<C: Configuration> IngredientImpl<C> {
Self {
ingredient_index: index,
singleton: Default::default(),
memo_table_types: Arc::new(MemoTableTypes::default()),
_phantom: std::marker::PhantomData,
}
}
Expand All @@ -100,7 +103,7 @@ impl<C: Configuration> IngredientImpl<C> {
let (zalsa, zalsa_local) = db.zalsas();

let id = self.singleton.with_scope(|| {
zalsa_local.allocate(zalsa.table(), self.ingredient_index, |_| Value::<C> {
zalsa_local.allocate(zalsa, self.ingredient_index, |_| Value::<C> {
fields,
stamps,
memos: Default::default(),
Expand Down Expand Up @@ -219,6 +222,10 @@ impl<C: Configuration> Ingredient for IngredientImpl<C> {
fn debug_name(&self) -> &'static str {
C::DEBUG_NAME
}

fn memo_table_types(&self) -> Arc<MemoTableTypes> {
self.memo_table_types.clone()
}
}

impl<C: Configuration> std::fmt::Debug for IngredientImpl<C> {
Expand Down
6 changes: 6 additions & 0 deletions src/input/input_field.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use std::fmt;
use std::marker::PhantomData;
use std::sync::Arc;

use crate::function::VerifyResult;
use crate::ingredient::{fmt_index, Ingredient};
use crate::input::{Configuration, IngredientImpl, Value};
use crate::table::memo::MemoTableTypes;
use crate::zalsa::IngredientIndex;
use crate::{Database, Id, Revision};

Expand Down Expand Up @@ -69,6 +71,10 @@ where
fn debug_name(&self) -> &'static str {
C::FIELD_DEBUG_NAMES[self.field_index]
}

fn memo_table_types(&self) -> Arc<MemoTableTypes> {
unreachable!("input fields do not allocate pages")
}
}

impl<C> std::fmt::Debug for FieldIngredientImpl<C>
Expand Down
17 changes: 11 additions & 6 deletions src/interned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::hash::FxDashMap;
use crate::ingredient::{fmt_index, Ingredient};
use crate::plumbing::{IngredientIndices, Jar};
use crate::revision::AtomicRevision;
use crate::table::memo::MemoTable;
use crate::table::memo::{MemoTable, MemoTableTypes};
use crate::table::sync::SyncTable;
use crate::table::Slot;
use crate::zalsa::{IngredientIndex, Zalsa};
Expand Down Expand Up @@ -62,6 +62,8 @@ pub struct IngredientImpl<C: Configuration> {
///
/// Deadlock requirement: We access `value_map` while holding lock on `key_map`, but not vice versa.
key_map: FxDashMap<C::Fields<'static>, Id>,

memo_table_types: Arc<MemoTableTypes>,
}

/// Struct storing the interned fields.
Expand Down Expand Up @@ -132,6 +134,7 @@ where
Self {
ingredient_index,
key_map: Default::default(),
memo_table_types: Arc::new(MemoTableTypes::default()),
}
}

Expand Down Expand Up @@ -279,16 +282,14 @@ where

// We won any races so should intern the data
Err(slot) => {
let table = zalsa.table();

// Record the durability of the current query on the interned value.
let durability = zalsa_local
.active_query()
.map(|(_, stamp)| stamp.durability)
// If there is no active query this durability does not actually matter.
.unwrap_or(Durability::MAX);

let id = zalsa_local.allocate(table, self.ingredient_index, |id| Value::<C> {
let id = zalsa_local.allocate(zalsa, self.ingredient_index, |id| Value::<C> {
fields: unsafe { self.to_internal_data(assemble(id, key)) },
memos: Default::default(),
syncs: Default::default(),
Expand All @@ -298,7 +299,7 @@ where
last_interned_at: AtomicRevision::from(current_revision),
});

let value = table.get::<Value<C>>(id);
let value = zalsa.table().get::<Value<C>>(id);

let slot_value = (value.fields.clone(), SharedValue::new(id));
unsafe { lock.insert_in_slot(data_hash, slot, slot_value) };
Expand All @@ -307,7 +308,7 @@ where
data_hash,
self.key_map
.hasher()
.hash_one(table.get::<Value<C>>(id).fields.clone())
.hash_one(zalsa.table().get::<Value<C>>(id).fields.clone())
);

// Record a dependency on this value.
Expand Down Expand Up @@ -409,6 +410,10 @@ where
fn debug_name(&self) -> &'static str {
C::DEBUG_NAME
}

fn memo_table_types(&self) -> Arc<MemoTableTypes> {
self.memo_table_types.clone()
}
}

impl<C> std::fmt::Debug for IngredientImpl<C>
Expand Down
6 changes: 5 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ pub mod plumbing {
pub use crate::key::DatabaseKeyIndex;
pub use crate::memo_ingredient_indices::{
IngredientIndices, MemoIngredientIndices, MemoIngredientMap, MemoIngredientSingletonIndex,
NewMemoIngredientIndices,
};
pub use crate::revision::Revision;
pub use crate::runtime::{stamp, Runtime, Stamp, StampedValue};
Expand Down Expand Up @@ -118,7 +119,10 @@ pub mod plumbing {
}

pub mod function {
pub use crate::function::{Configuration, IngredientImpl};
pub use crate::function::Configuration;
pub use crate::function::IngredientImpl;
pub use crate::function::Memo;
pub use crate::table::memo::MemoEntryType;
}

pub mod tracked_struct {
Expand Down
Loading