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
4 changes: 2 additions & 2 deletions components/salsa-macro-rules/src/setup_tracked_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,8 @@ macro_rules! setup_tracked_struct {
}

impl $zalsa::TrackedStructInDb for $Struct<'_> {
fn database_key_index(zalsa: &$zalsa::Zalsa, id: $zalsa::Id) -> $zalsa::DatabaseKeyIndex {
$Configuration::ingredient_(zalsa).database_key_index(id)
fn ingredient_index(zalsa: &$zalsa::Zalsa, id: $zalsa::Id) -> $zalsa::IngredientIndex {
$Configuration::ingredient_(zalsa).ingredient_index()
}
}

Expand Down
29 changes: 18 additions & 11 deletions src/active_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use crate::accumulator::{
accumulated_map::{AccumulatedMap, AtomicInputAccumulatedValues, InputAccumulatedValues},
Accumulator,
};
use crate::hash::FxIndexSet;
use crate::key::DatabaseKeyIndex;
use crate::runtime::Stamp;
use crate::sync::atomic::AtomicBool;
Expand All @@ -17,6 +16,7 @@ use crate::{
Id,
};
use crate::{durability::Durability, tracked_struct::Identity};
use crate::{hash::FxIndexSet, IngredientIndex};

#[derive(Debug)]
pub(crate) struct ActiveQuery {
Expand Down Expand Up @@ -146,8 +146,9 @@ impl ActiveQuery {
}

/// Adds a key to our list of outputs.
pub(super) fn add_output(&mut self, key: DatabaseKeyIndex) {
self.input_outputs.insert(QueryEdge::output(key));
pub(super) fn add_output(&mut self, key: Id, ingredient_index: IngredientIndex) {
self.input_outputs
.insert(QueryEdge::output(key, ingredient_index));
}

/// True if the given key was output by this query.
Expand Down Expand Up @@ -417,7 +418,8 @@ pub(crate) struct CompletedQuery {
}

struct CapturedQuery {
database_key_index: DatabaseKeyIndex,
id: Id,
ingredient_index: IngredientIndex,
durability: Durability,
changed_at: Revision,
cycle_heads: CycleHeads,
Expand All @@ -428,7 +430,8 @@ impl fmt::Debug for CapturedQuery {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut debug_struct = f.debug_struct("CapturedQuery");
debug_struct
.field("database_key_index", &self.database_key_index)
.field("id", &self.id)
.field("ingredient_index", &self.ingredient_index)
.field("durability", &self.durability)
.field("changed_at", &self.changed_at);
if !self.cycle_heads.is_empty() {
Expand All @@ -445,13 +448,17 @@ pub struct Backtrace(Box<[CapturedQuery]>);
impl Backtrace {
pub fn capture() -> Option<Self> {
crate::with_attached_database(|db| {
db.zalsa_local().try_with_query_stack(|stack| {
let (zalsa, zalsa_local) = db.zalsas();
zalsa_local.try_with_query_stack(|stack| {
Backtrace(
stack
.iter()
.rev()
.map(|query| CapturedQuery {
database_key_index: query.database_key_index,
id: query.database_key_index.key_index(),
ingredient_index: query
.database_key_index
.ingredient_index_with_zalsa(zalsa),
durability: query.durability,
changed_at: query.changed_at,
cycle_heads: query.cycle_heads.clone(),
Expand Down Expand Up @@ -486,14 +493,16 @@ impl fmt::Display for Backtrace {
for (
idx,
&CapturedQuery {
database_key_index,
id,
ingredient_index,
durability,
changed_at,
ref cycle_heads,
iteration_count,
},
) in self.0.iter().enumerate()
{
let database_key_index = DatabaseKeyIndex::new_non_interned(ingredient_index, id);
write!(fmt, "{idx:>4}: {database_key_index:?}")?;
if full {
write!(fmt, " -> ({changed_at:?}, {durability:#?}")?;
Expand All @@ -504,9 +513,7 @@ impl fmt::Display for Backtrace {
}
writeln!(fmt)?;
crate::attach::with_attached_database(|db| {
let ingredient = db
.zalsa()
.lookup_ingredient(database_key_index.ingredient_index());
let ingredient = db.zalsa().lookup_ingredient(ingredient_index);
let loc = ingredient.location();
writeln!(fmt, "{indent}at {}:{}", loc.file, loc.line)?;
if !cycle_heads.is_empty() {
Expand Down
24 changes: 15 additions & 9 deletions src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ where

#[inline]
pub fn database_key_index(&self, key: Id) -> DatabaseKeyIndex {
DatabaseKeyIndex::new(self.index, key)
DatabaseKeyIndex::new_non_interned(self.index, key)
}

pub fn set_capacity(&mut self, capacity: usize) {
Expand Down Expand Up @@ -350,7 +350,7 @@ where
continue;
}

let dependency = zalsa.lookup_ingredient(edge.key().ingredient_index());
let dependency = zalsa.lookup_ingredient(edge.key().ingredient_index_with_zalsa(zalsa));
dependency.collect_minimum_serialized_edges(
zalsa,
*edge,
Expand Down Expand Up @@ -527,7 +527,9 @@ where

// We only serialize the query if there are any memos associated with it.
for entry in <C::SalsaStruct<'_> as SalsaStructInDb>::entries(zalsa) {
let memo_ingredient_index = self.memo_ingredient_indices.get(entry.ingredient_index());
let memo_ingredient_index = self
.memo_ingredient_indices
.get(entry.ingredient_index_with_zalsa(zalsa));

let memo =
self.get_memo_from_table_for(zalsa, entry.key_index(), memo_ingredient_index);
Expand Down Expand Up @@ -614,7 +616,7 @@ mod persistence {
.filter(|entry| {
let memo_ingredient_index = ingredient
.memo_ingredient_indices
.get(entry.ingredient_index());
.get(entry.ingredient_index_with_zalsa(zalsa));

let memo = ingredient.get_memo_from_table_for(
zalsa,
Expand All @@ -634,7 +636,7 @@ mod persistence {
for entry in <C::SalsaStruct<'_> as SalsaStructInDb>::entries(zalsa) {
let memo_ingredient_index = ingredient
.memo_ingredient_indices
.get(entry.ingredient_index());
.get(entry.ingredient_index_with_zalsa(zalsa));

let memo = ingredient.get_memo_from_table_for(
zalsa,
Expand Down Expand Up @@ -666,14 +668,18 @@ mod persistence {
QueryOrigin::derived_untracked(flattened_edges.drain(..).collect())
}
QueryOriginRef::Assigned(key) => {
let dependency = zalsa.lookup_ingredient(key.ingredient_index());
let dependency =
zalsa.lookup_ingredient(key.ingredient_index_with_zalsa(zalsa));
assert!(
dependency.is_persistable(),
"specified query `{}` must be persistable",
dependency.debug_name()
);

QueryOrigin::assigned(key)
QueryOrigin::assigned(
key.key_index(),
key.ingredient_index_with_zalsa(zalsa),
)
}
QueryOriginRef::FixpointInitial => unreachable!(
"`should_serialize` returns `false` for provisional queries"
Expand All @@ -685,7 +691,7 @@ mod persistence {
// TODO: Group structs by ingredient index into a nested map.
let key = format!(
"{}:{}",
entry.ingredient_index().as_u32(),
entry.ingredient_index_with_zalsa(self.zalsa).as_u32(),
entry.key_index().as_bits()
);

Expand All @@ -707,7 +713,7 @@ mod persistence {
flattened_edges: &mut FxIndexSet<QueryEdge>,
) {
for &edge in edges {
let dependency = zalsa.lookup_ingredient(edge.key().ingredient_index());
let dependency = zalsa.lookup_ingredient(edge.key().ingredient_index_with_zalsa(zalsa));

if dependency.is_persistable() {
// If the dependency will be serialized, we can serialize the edge directly.
Expand Down
2 changes: 1 addition & 1 deletion src/function/accumulated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ where
continue;
}

let ingredient = zalsa.lookup_ingredient(k.ingredient_index());
let ingredient = zalsa.lookup_ingredient(k.ingredient_index_with_zalsa(zalsa));
// Extend `output` with any values accumulated by `k`.
// SAFETY: `db` owns the `ingredient`
let (accumulated_map, input) =
Expand Down
2 changes: 1 addition & 1 deletion src/function/diff_outputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ fn diff_outputs_on_revision(
// Note that tracked structs are not stored as direct query outputs, but they are still outputs
// that need to be reported as stale.
for (identity, id) in &completed_query.stale_tracked_structs {
let output = DatabaseKeyIndex::new(identity.ingredient_index(), *id);
let output = DatabaseKeyIndex::new_non_interned(identity.ingredient_index(), *id);
report_stale_output(zalsa, key, output);
}

Expand Down
28 changes: 17 additions & 11 deletions src/function/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ where
let database_key_index = claim_guard.database_key_index();
let zalsa = claim_guard.zalsa();

let id = database_key_index.key_index();
let id = claim_guard.key_index();
let memo_ingredient_index = self.memo_ingredient_index(zalsa, id);

crate::tracing::info!("{:?}: executing query", database_key_index);
Expand All @@ -61,6 +61,7 @@ where
db,
zalsa,
zalsa_local.push_query(database_key_index, IterationCount::initial()),
id,
opt_old_memo,
);
(new_value, active_query.pop())
Expand All @@ -70,6 +71,7 @@ where
db,
zalsa,
zalsa_local.push_query(database_key_index, IterationCount::initial()),
id,
opt_old_memo,
);

Expand Down Expand Up @@ -166,7 +168,7 @@ where
let database_key_index = claim_guard.database_key_index();
let zalsa = claim_guard.zalsa();

let id = database_key_index.key_index();
let id = claim_guard.key_index();

// Our provisional value from the previous iteration, when doing fixpoint iteration.
// This is different from `opt_old_memo` which might be from a different revision.
Expand Down Expand Up @@ -219,6 +221,7 @@ where
db,
zalsa,
active_query,
id,
last_provisional_memo_opt.or(opt_old_memo),
);

Expand Down Expand Up @@ -377,6 +380,7 @@ where
db: &'db C::DbView,
zalsa: &'db Zalsa,
active_query: ActiveQueryGuard<'db>,
key_index: Id,
opt_old_memo: Option<&Memo<'db, C>>,
) -> (C::Output<'db>, ActiveQueryGuard<'db>) {
if let Some(old_memo) = opt_old_memo {
Expand All @@ -398,10 +402,7 @@ where

// Query was not previously executed, or value is potentially
// stale, or value is absent. Let's execute!
let new_value = C::execute(
db,
C::id_to_input(zalsa, active_query.database_key_index.key_index()),
);
let new_value = C::execute(db, C::id_to_input(zalsa, key_index));

(new_value, active_query)
}
Expand Down Expand Up @@ -492,7 +493,8 @@ fn outer_cycle(
cycle_heads
.iter_not_eq(current_key)
.rfind(|head| {
let ingredient = zalsa.lookup_ingredient(head.database_key_index.ingredient_index());
let ingredient =
zalsa.lookup_ingredient(head.database_key_index.ingredient_index_with_zalsa(zalsa));

matches!(
ingredient.wait_for(zalsa, head.database_key_index.key_index()),
Expand Down Expand Up @@ -531,7 +533,8 @@ fn collect_all_cycle_heads(
max_iteration_count = max_iteration_count.max(head.iteration_count.load());
depends_on_self |= head.database_key_index == database_key_index;

let ingredient = zalsa.lookup_ingredient(head.database_key_index.ingredient_index());
let ingredient =
zalsa.lookup_ingredient(head.database_key_index.ingredient_index_with_zalsa(zalsa));

let provisional_status = ingredient
.provisional_status(zalsa, head.database_key_index.key_index())
Expand Down Expand Up @@ -654,7 +657,8 @@ fn try_complete_cycle_head(
let converged = this_converged
&& cycle_heads.iter_not_eq(me).all(|head| {
let database_key_index = head.database_key_index;
let ingredient = zalsa.lookup_ingredient(database_key_index.ingredient_index());
let ingredient =
zalsa.lookup_ingredient(database_key_index.ingredient_index_with_zalsa(zalsa));

let converged = ingredient.cycle_converged(zalsa, database_key_index.key_index());

Expand All @@ -673,7 +677,8 @@ fn try_complete_cycle_head(
// Set the nested cycles as verified. This is necessary because
// `validate_provisional` doesn't follow cycle heads recursively (and the memos now depend on all cycle heads).
for head in cycle_heads.iter_not_eq(me) {
let ingredient = zalsa.lookup_ingredient(head.database_key_index.ingredient_index());
let ingredient =
zalsa.lookup_ingredient(head.database_key_index.ingredient_index_with_zalsa(zalsa));
ingredient.finalize_cycle_head(zalsa, head.database_key_index.key_index());
}

Expand All @@ -700,7 +705,8 @@ fn try_complete_cycle_head(

// Update the iteration count of nested cycles.
for head in cycle_heads.iter_not_eq(me) {
let ingredient = zalsa.lookup_ingredient(head.database_key_index.ingredient_index());
let ingredient =
zalsa.lookup_ingredient(head.database_key_index.ingredient_index_with_zalsa(zalsa));

ingredient.set_cycle_iteration_count(
zalsa,
Expand Down
6 changes: 5 additions & 1 deletion src/function/maybe_changed_after.rs
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,11 @@ fn validate_provisional(
for cycle_head in cycle_heads {
// Test if our cycle heads (with the same revision) are now finalized.
let Some(kind) = zalsa
.lookup_ingredient(cycle_head.database_key_index.ingredient_index())
.lookup_ingredient(
cycle_head
.database_key_index
.ingredient_index_with_zalsa(zalsa),
)
.provisional_status(zalsa, cycle_head.database_key_index.key_index())
else {
return false;
Expand Down
4 changes: 2 additions & 2 deletions src/function/memo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ where
}

for (identity, id) in self.revisions.tracked_struct_ids() {
let key = DatabaseKeyIndex::new(identity.ingredient_index(), *id);
let key = DatabaseKeyIndex::new_non_interned(identity.ingredient_index(), *id);
key.remove_stale_output(zalsa, executor);
}
}
Expand Down Expand Up @@ -447,7 +447,7 @@ impl<'me> Iterator for TryClaimCycleHeadsIter<'me> {
let head_key_index = head_database_key.key_index();
let ingredient = self
.zalsa
.lookup_ingredient(head_database_key.ingredient_index());
.lookup_ingredient(head_database_key.ingredient_index_with_zalsa(self.zalsa));

match ingredient.wait_for(self.zalsa, head_key_index) {
WaitForResult::Cycle { .. } => {
Expand Down
13 changes: 8 additions & 5 deletions src/function/specify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ where
// * Q4 invokes Q2 and then Q1
//
// Now, if We invoke Q3 first, We get one result for Q2, but if We invoke Q4 first, We get a different value. That's no good.
let database_key_index = <C::Input<'db>>::database_key_index(zalsa, key);
if !zalsa_local.is_tracked_struct_of_active_query(database_key_index) {
let ingredient_index = <C::Input<'db>>::ingredient_index(zalsa, key);
if !zalsa_local.is_tracked_struct_of_active_query(key, ingredient_index) {
panic!("can only use `specify` on salsa structs created during the current tracked fn");
}
let database_key_index = DatabaseKeyIndex::new_non_interned(ingredient_index, key);

// Subtle: we treat the "input" to a set query as if it were
// volatile.
Expand All @@ -68,7 +69,10 @@ where
revisions: QueryRevisions {
changed_at: current_deps.changed_at,
durability: current_deps.durability,
origin: QueryOrigin::assigned(active_query_key),
origin: QueryOrigin::assigned(
active_query_key.key_index(),
active_query_key.ingredient_index_with_zalsa(zalsa),
),
#[cfg(feature = "accumulator")]
accumulated_inputs: Default::default(),
verified_final: AtomicBool::new(true),
Expand Down Expand Up @@ -102,8 +106,7 @@ where
self.insert_memo(zalsa, key, memo, memo_ingredient_index);

// Record that the current query *specified* a value for this cell.
let database_key_index = self.database_key_index(key);
zalsa_local.add_output(database_key_index);
zalsa_local.add_output(key, self.index);
}

/// Invoked when the query `executor` has been validated as having green inputs
Expand Down
Loading
Loading