Skip to content

Commit

Permalink
Merge pull request #527 from nikomatsakis/spindle
Browse files Browse the repository at this point in the history
re-enable parallel tests
  • Loading branch information
nikomatsakis authored Jul 25, 2024
2 parents 2f4f80f + bf636d2 commit e4ce917
Show file tree
Hide file tree
Showing 49 changed files with 1,101 additions and 1,453 deletions.
3 changes: 2 additions & 1 deletion components/salsa-macro-rules/src/setup_accumulator_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ macro_rules! setup_accumulator_impl {
where
Db: ?Sized + $zalsa::Database,
{
$ingredient(db.as_salsa_database()).push(db.runtime(), self);
let db = db.as_salsa_database();
$ingredient(db).push(db, self);
}
}
};
Expand Down
7 changes: 5 additions & 2 deletions components/salsa-macro-rules/src/setup_input_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,11 @@ macro_rules! setup_input_struct {
// FIXME(rust-lang/rust#65991): The `db` argument *should* have the type `dyn Database`
$Db: ?Sized + $zalsa::Database,
{
let runtime = db.runtime();
let fields = $Configuration::ingredient(db.as_salsa_database()).field(runtime, self, $field_index);
let fields = $Configuration::ingredient(db.as_salsa_database()).field(
db.as_salsa_database(),
self,
$field_index,
);
$zalsa::maybe_clone!(
$field_option,
$field_ty,
Expand Down
3 changes: 1 addition & 2 deletions components/salsa-macro-rules/src/setup_interned_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,8 @@ macro_rules! setup_interned_struct {
// FIXME(rust-lang/rust#65991): The `db` argument *should* have the type `dyn Database`
$Db: ?Sized + salsa::Database,
{
let runtime = db.runtime();
let current_revision = $zalsa::current_revision(db);
$Configuration::ingredient(db).intern(runtime, ($($field_id,)*))
$Configuration::ingredient(db).intern(db.as_salsa_database(), ($($field_id,)*))
}

$(
Expand Down
34 changes: 16 additions & 18 deletions components/salsa-macro-rules/src/setup_tracked_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ macro_rules! setup_tracked_fn {
use salsa::plumbing as $zalsa;
let key = $zalsa::macro_if! {
if $needs_interner {
$Configuration::intern_ingredient($db).intern_id($db.runtime(), ($($input_id),*))
$Configuration::intern_ingredient($db).intern_id($db.as_salsa_database(), ($($input_id),*))
} else {
$zalsa::AsId::as_id(&($($input_id),*))
}
Expand Down Expand Up @@ -265,26 +265,24 @@ macro_rules! setup_tracked_fn {
} }
}

$zalsa::attach_database($db, || {
let result = $zalsa::macro_if! {
if $needs_interner {
{
let key = $Configuration::intern_ingredient($db).intern_id($db.runtime(), ($($input_id),*));
$Configuration::fn_ingredient($db).fetch($db, key)
}
} else {
$Configuration::fn_ingredient($db).fetch($db, $zalsa::AsId::as_id(&($($input_id),*)))
let result = $zalsa::macro_if! {
if $needs_interner {
{
let key = $Configuration::intern_ingredient($db).intern_id($db.as_salsa_database(), ($($input_id),*));
$Configuration::fn_ingredient($db).fetch($db, key)
}
};
} else {
$Configuration::fn_ingredient($db).fetch($db, $zalsa::AsId::as_id(&($($input_id),*)))
}
};

$zalsa::macro_if! {
if $return_ref {
result
} else {
<$output_ty as std::clone::Clone>::clone(result)
}
$zalsa::macro_if! {
if $return_ref {
result
} else {
<$output_ty as std::clone::Clone>::clone(result)
}
})
}
}
};
}
5 changes: 2 additions & 3 deletions components/salsa-macro-rules/src/setup_tracked_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ macro_rules! setup_tracked_struct {
$Db: ?Sized + $zalsa::Database,
{
$Configuration::ingredient(db.as_salsa_database()).new_struct(
db.runtime(),
db.as_salsa_database(),
($($field_id,)*)
)
}
Expand All @@ -204,8 +204,7 @@ macro_rules! setup_tracked_struct {
// FIXME(rust-lang/rust#65991): The `db` argument *should* have the type `dyn Database`
$Db: ?Sized + $zalsa::Database,
{
let runtime = db.runtime();
let fields = unsafe { self.0.as_ref() }.field(runtime, $field_index);
let fields = unsafe { self.0.as_ref() }.field(db.as_salsa_database(), $field_index);
$crate::maybe_clone!(
$field_option,
$field_ty,
Expand Down
4 changes: 2 additions & 2 deletions examples/calc/type_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,8 @@ fn fix_bad_variable_in_function() {
"#]],
expect![[r#"
[
"Event: Event { runtime_id: RuntimeId { counter: 0 }, kind: WillExecute { database_key: parse_statements(0) } }",
"Event: Event { runtime_id: RuntimeId { counter: 0 }, kind: WillExecute { database_key: type_check_function(0) } }",
"Event: Event { thread_id: ThreadId(11), kind: WillExecute { database_key: parse_statements(0) } }",
"Event: Event { thread_id: ThreadId(11), kind: WillExecute { database_key: type_check_function(0) } }",
]
"#]],
)],
Expand Down
64 changes: 36 additions & 28 deletions src/accumulator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ use crate::{
hash::FxDashMap,
ingredient::{fmt_index, Ingredient, Jar},
key::DependencyIndex,
runtime::local_state::QueryOrigin,
local_state::{self, LocalState, QueryOrigin},
storage::IngredientIndex,
Database, DatabaseKeyIndex, Event, EventKind, Id, Revision, Runtime,
Database, DatabaseKeyIndex, Event, EventKind, Id, Revision,
};

pub trait Accumulator: Clone + Debug + Send + Sync + 'static + Sized {
Expand Down Expand Up @@ -78,44 +78,48 @@ impl<A: Accumulator> IngredientImpl<A> {
}
}

pub fn push(&self, runtime: &Runtime, value: A) {
let current_revision = runtime.current_revision();
let (active_query, _) = match runtime.active_query() {
Some(pair) => pair,
None => {
panic!("cannot accumulate values outside of an active query")
pub fn push(&self, db: &dyn crate::Database, value: A) {
local_state::attach(db, |state| {
let runtime = db.runtime();
let current_revision = runtime.current_revision();
let (active_query, _) = match state.active_query() {
Some(pair) => pair,
None => {
panic!("cannot accumulate values outside of an active query")
}
};

let mut accumulated_values =
self.map.entry(active_query).or_insert(AccumulatedValues {
values: vec![],
produced_at: current_revision,
});

// When we call `push' in a query, we will add the accumulator to the output of the query.
// If we find here that this accumulator is not the output of the query,
// we can say that the accumulated values we stored for this query is out of date.
if !state.is_output_of_active_query(self.dependency_index()) {
accumulated_values.values.truncate(0);
accumulated_values.produced_at = current_revision;
}
};

let mut accumulated_values = self.map.entry(active_query).or_insert(AccumulatedValues {
values: vec![],
produced_at: current_revision,
});

// When we call `push' in a query, we will add the accumulator to the output of the query.
// If we find here that this accumulator is not the output of the query,
// we can say that the accumulated values we stored for this query is out of date.
if !runtime.is_output_of_active_query(self.dependency_index()) {
accumulated_values.values.truncate(0);
accumulated_values.produced_at = current_revision;
}

runtime.add_output(self.dependency_index());
accumulated_values.values.push(value);
state.add_output(self.dependency_index());
accumulated_values.values.push(value);
})
}

pub(crate) fn produced_by(
&self,
runtime: &Runtime,
current_revision: Revision,
local_state: &LocalState,
query: DatabaseKeyIndex,
output: &mut Vec<A>,
) {
let current_revision = runtime.current_revision();
if let Some(v) = self.map.get(&query) {
// FIXME: We don't currently have a good way to identify the value that was read.
// You can't report is as a tracked read of `query`, because the return value of query is not being read here --
// instead it is the set of values accumuated by `query`.
runtime.report_untracked_read();
local_state.report_untracked_read(current_revision);

let AccumulatedValues {
values,
Expand Down Expand Up @@ -174,7 +178,7 @@ impl<A: Accumulator> Ingredient for IngredientImpl<A> {
assert!(stale_output_key.is_none());
if self.map.remove(&executor).is_some() {
db.salsa_event(Event {
runtime_id: db.runtime().id(),
thread_id: std::thread::current().id(),
kind: EventKind::DidDiscardAccumulated {
executor_key: executor,
accumulator: self.dependency_index(),
Expand All @@ -198,6 +202,10 @@ impl<A: Accumulator> Ingredient for IngredientImpl<A> {
fn fmt_index(&self, index: Option<crate::Id>, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt_index(A::DEBUG_NAME, index, fmt)
}

fn debug_name(&self) -> &'static str {
A::DEBUG_NAME
}
}

impl<A> std::fmt::Debug for IngredientImpl<A>
Expand Down
56 changes: 8 additions & 48 deletions src/runtime/active_query.rs → src/active_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,55 +9,35 @@ use crate::{
use super::local_state::{EdgeKind, QueryEdges, QueryOrigin, QueryRevisions};

#[derive(Debug)]
pub(super) struct ActiveQuery {
pub(crate) struct ActiveQuery {
/// What query is executing
pub(super) database_key_index: DatabaseKeyIndex,
pub(crate) database_key_index: DatabaseKeyIndex,

/// Minimum durability of inputs observed so far.
pub(super) durability: Durability,
pub(crate) durability: Durability,

/// Maximum revision of all inputs observed. If we observe an
/// untracked read, this will be set to the most recent revision.
pub(super) changed_at: Revision,
pub(crate) changed_at: Revision,

/// Inputs: Set of subqueries that were accessed thus far.
/// Outputs: Tracks values written by this query. Could be...
///
/// * tracked structs created
/// * invocations of `specify`
/// * accumulators pushed to
pub(super) input_outputs: FxIndexSet<(EdgeKind, DependencyIndex)>,
input_outputs: FxIndexSet<(EdgeKind, DependencyIndex)>,

/// True if there was an untracked read.
pub(super) untracked_read: bool,
untracked_read: bool,

/// Stores the entire cycle, if one is found and this query is part of it.
pub(super) cycle: Option<Cycle>,
pub(crate) cycle: Option<Cycle>,

/// When new entities are created, their data is hashed, and the resulting
/// hash is added to this map. If it is not present, then the disambiguator is 0.
/// Otherwise it is 1 more than the current value (which is incremented).
pub(super) disambiguator_map: FxIndexMap<u64, Disambiguator>,
}

pub(super) struct SavedQueryState {
database_key_index: DatabaseKeyIndex,
durability: Durability,
changed_at: Revision,
input_outputs_len: usize,
untracked_read: bool,
}

impl SavedQueryState {
fn new(query: &ActiveQuery) -> Self {
Self {
database_key_index: query.database_key_index,
durability: query.durability,
changed_at: query.changed_at,
input_outputs_len: query.input_outputs.len(),
untracked_read: query.untracked_read,
}
}
disambiguator_map: FxIndexMap<u64, Disambiguator>,
}

impl ActiveQuery {
Expand All @@ -73,26 +53,6 @@ impl ActiveQuery {
}
}

pub(super) fn save_query_state(&self) -> SavedQueryState {
SavedQueryState::new(self)
}

pub(super) fn restore_query_state(&mut self, state: SavedQueryState) {
assert_eq!(self.database_key_index, state.database_key_index);

assert!(self.durability <= state.durability);
self.durability = state.durability;

assert!(self.changed_at >= state.changed_at);
self.changed_at = state.changed_at;

assert!(self.input_outputs.len() >= state.input_outputs_len);
self.input_outputs.truncate(state.input_outputs_len);

assert!(self.untracked_read >= state.untracked_read);
self.untracked_read = state.untracked_read;
}

pub(super) fn add_read(
&mut self,
input: DependencyIndex,
Expand Down
4 changes: 2 additions & 2 deletions src/cycle.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{database, key::DatabaseKeyIndex, Database};
use crate::{key::DatabaseKeyIndex, local_state, Database};
use std::{panic::AssertUnwindSafe, sync::Arc};

/// Captures the participants of a cycle that occurred when executing a query.
Expand Down Expand Up @@ -74,7 +74,7 @@ impl Cycle {

impl std::fmt::Debug for Cycle {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
database::with_attached_database(|db| {
local_state::with_attached_database(|db| {
f.debug_struct("UnexpectedCycle")
.field("all_participants", &self.all_participants(db))
.field("unexpected_participants", &self.unexpected_participants(db))
Expand Down
Loading

0 comments on commit e4ce917

Please sign in to comment.