Skip to content

Commit dbd96d0

Browse files
committed
Workaround
1 parent 6972b6b commit dbd96d0

File tree

2 files changed

+35
-32
lines changed

2 files changed

+35
-32
lines changed

compiler/rustc_data_structures/src/sync/table/sync_table.rs

+17-12
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use super::raw::bitmask::BitMask;
2323
use super::raw::imp::Group;
2424
use super::scopeguard::guard;
2525
use super::util::{cold_path, make_insert_hash};
26+
use crate::sync::{DynSend, DynSync};
2627

2728
mod code;
2829
mod tests;
@@ -627,7 +628,10 @@ struct DestroyTable<T> {
627628
}
628629

629630
unsafe impl<T> Sync for DestroyTable<T> {}
630-
unsafe impl<T: Send> Send for DestroyTable<T> {}
631+
632+
// FIXME: Unsound
633+
//unsafe impl<T: Send> Send for DestroyTable<T> {}
634+
unsafe impl<T: DynSend> Send for DestroyTable<T> {}
631635

632636
impl<T> DestroyTable<T> {
633637
unsafe fn run(&self) {
@@ -653,8 +657,9 @@ unsafe impl<#[may_dangle] K, #[may_dangle] V, S> Drop for SyncTable<K, V, S> {
653657
}
654658
}
655659

656-
unsafe impl<K: Send, V: Send, S: Send> Send for SyncTable<K, V, S> {}
660+
unsafe impl<K: DynSend, V: DynSend, S: DynSend> DynSend for SyncTable<K, V, S> {}
657661
unsafe impl<K: Sync, V: Sync, S: Sync> Sync for SyncTable<K, V, S> {}
662+
unsafe impl<K: DynSync, V: DynSync, S: DynSync> DynSync for SyncTable<K, V, S> {}
658663

659664
impl<K, V, S: Default> Default for SyncTable<K, V, S> {
660665
#[inline]
@@ -944,7 +949,7 @@ impl<'a, K, V, S> Write<'a, K, V, S> {
944949
}
945950
}
946951

947-
impl<'a, K: Send, V: Send + Clone, S: BuildHasher> Write<'a, K, V, S> {
952+
impl<'a, K: DynSend, V: DynSend + Clone, S: BuildHasher> Write<'a, K, V, S> {
948953
/// Removes an element from the table, and returns a reference to it if was present.
949954
#[inline]
950955
pub fn remove<Q>(&mut self, key: &Q, hash: Option<u64>) -> Option<(&'a K, &'a V)>
@@ -970,7 +975,7 @@ impl<'a, K: Send, V: Send + Clone, S: BuildHasher> Write<'a, K, V, S> {
970975
}
971976
}
972977

973-
impl<'a, K: Hash + Eq + Send + Clone, V: Send + Clone, S: BuildHasher> Write<'a, K, V, S> {
978+
impl<'a, K: Hash + Eq + DynSend + Clone, V: DynSend + Clone, S: BuildHasher> Write<'a, K, V, S> {
974979
/// Inserts a element into the table.
975980
/// Returns `false` if it already exists and doesn't update the value.
976981
#[inline]
@@ -995,7 +1000,7 @@ impl<'a, K: Hash + Eq + Send + Clone, V: Send + Clone, S: BuildHasher> Write<'a,
9951000
}
9961001
}
9971002

998-
impl<'a, K: Hash + Send + Clone, V: Send + Clone, S: BuildHasher> Write<'a, K, V, S> {
1003+
impl<'a, K: Hash + DynSend + Clone, V: DynSend + Clone, S: BuildHasher> Write<'a, K, V, S> {
9991004
/// Inserts a new element into the table, and returns a reference to it.
10001005
///
10011006
/// This does not check if the given element already exists in the table.
@@ -1065,7 +1070,7 @@ impl<'a, K: Hash + Send + Clone, V: Send + Clone, S: BuildHasher> Write<'a, K, V
10651070
}
10661071
}
10671072

1068-
impl<K: Hash + Send, V: Send, S: BuildHasher> Write<'_, K, V, S> {
1073+
impl<K: Hash + DynSend, V: DynSend, S: BuildHasher> Write<'_, K, V, S> {
10691074
fn replace_table(&mut self, new_table: TableRef<(K, V)>) {
10701075
let table = self.table.current();
10711076

@@ -1111,8 +1116,8 @@ impl<K: Hash + Send, V: Send, S: BuildHasher> Write<'_, K, V, S> {
11111116
}
11121117
}
11131118

1114-
impl<K: Eq + Hash + Clone + Send, V: Clone + Send, S: BuildHasher + Default> FromIterator<(K, V)>
1115-
for SyncTable<K, V, S>
1119+
impl<K: Eq + Hash + Clone + DynSend, V: Clone + DynSend, S: BuildHasher + Default>
1120+
FromIterator<(K, V)> for SyncTable<K, V, S>
11161121
{
11171122
fn from_iter<I: IntoIterator<Item = (K, V)>>(iter: I) -> Self {
11181123
let iter = iter.into_iter();
@@ -1221,7 +1226,7 @@ impl<'a> PotentialSlot<'a> {
12211226
///
12221227
/// This does not check if the given element already exists in the table.
12231228
#[inline]
1224-
pub fn insert_new<'b, K: Hash + Send + Clone, V: Send + Clone, S: BuildHasher>(
1229+
pub fn insert_new<'b, K: Hash + DynSend + Clone, V: DynSend + Clone, S: BuildHasher>(
12251230
self,
12261231
table: &mut Write<'b, K, V, S>,
12271232
key: K,
@@ -1485,9 +1490,9 @@ impl<T> RawIterRange<T> {
14851490
}
14861491
}
14871492

1488-
// We make raw iterators unconditionally Send and Sync, and let the PhantomData
1489-
// in the actual iterator implementations determine the real Send/Sync bounds.
1490-
unsafe impl<T> Send for RawIterRange<T> {}
1493+
// We make raw iterators unconditionally DynSend and Sync, and let the PhantomData
1494+
// in the actual iterator implementations determine the real DynSend/Sync bounds.
1495+
unsafe impl<T> DynSend for RawIterRange<T> {}
14911496
unsafe impl<T> Sync for RawIterRange<T> {}
14921497

14931498
impl<T> Clone for RawIterRange<T> {

compiler/rustc_query_system/src/query/caches.rs

+18-20
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use std::fmt::Debug;
2-
use std::hash::Hash;
2+
use std::hash::{BuildHasherDefault, Hash};
33
use std::sync::OnceLock;
44

5-
use rustc_data_structures::fx::FxHashMap;
6-
use rustc_data_structures::sharded::{self, Sharded};
5+
use rustc_data_structures::fx::FxHasher;
6+
use rustc_data_structures::sync::collect::{self, pin};
7+
use rustc_data_structures::sync::{DynSend, SyncTable};
78
pub use rustc_data_structures::vec_cache::VecCache;
89
use rustc_hir::def_id::LOCAL_CRATE;
910
use rustc_index::Idx;
@@ -36,46 +37,43 @@ pub trait QueryCache: Sized {
3637
/// In-memory cache for queries whose keys aren't suitable for any of the
3738
/// more specialized kinds of cache. Backed by a sharded hashmap.
3839
pub struct DefaultCache<K, V> {
39-
cache: Sharded<FxHashMap<K, (V, DepNodeIndex)>>,
40+
cache: SyncTable<K, (V, DepNodeIndex), BuildHasherDefault<FxHasher>>,
4041
}
4142

4243
impl<K, V> Default for DefaultCache<K, V> {
4344
fn default() -> Self {
44-
DefaultCache { cache: Default::default() }
45+
DefaultCache { cache: SyncTable::new_with(BuildHasherDefault::default(), 0) }
4546
}
4647
}
4748

4849
impl<K, V> QueryCache for DefaultCache<K, V>
4950
where
50-
K: Eq + Hash + Copy + Debug,
51-
V: Copy,
51+
K: Eq + Hash + Copy + Debug + DynSend,
52+
V: Copy + DynSend,
5253
{
5354
type Key = K;
5455
type Value = V;
5556

5657
#[inline(always)]
5758
fn lookup(&self, key: &K) -> Option<(V, DepNodeIndex)> {
58-
let key_hash = sharded::make_hash(key);
59-
let lock = self.cache.lock_shard_by_hash(key_hash);
60-
let result = lock.raw_entry().from_key_hashed_nocheck(key_hash, key);
61-
62-
if let Some((_, value)) = result { Some(*value) } else { None }
59+
pin(|pin| {
60+
let result = self.cache.read(pin).get(key, None);
61+
if let Some((_, value)) = result { Some(*value) } else { None }
62+
})
6363
}
6464

6565
#[inline]
6666
fn complete(&self, key: K, value: V, index: DepNodeIndex) {
67-
let mut lock = self.cache.lock_shard_by_value(&key);
68-
// We may be overwriting another value. This is all right, since the dep-graph
69-
// will check that the fingerprint matches.
70-
lock.insert(key, (value, index));
67+
self.cache.lock().insert_new(key, (value, index), None);
68+
collect::collect();
7169
}
7270

7371
fn iter(&self, f: &mut dyn FnMut(&Self::Key, &Self::Value, DepNodeIndex)) {
74-
for shard in self.cache.lock_shards() {
75-
for (k, v) in shard.iter() {
72+
pin(|pin| {
73+
for (k, v) in self.cache.read(pin).iter() {
7674
f(k, &v.0, v.1);
7775
}
78-
}
76+
})
7977
}
8078
}
8179

@@ -134,7 +132,7 @@ impl<V> Default for DefIdCache<V> {
134132

135133
impl<V> QueryCache for DefIdCache<V>
136134
where
137-
V: Copy,
135+
V: Copy + DynSend,
138136
{
139137
type Key = DefId;
140138
type Value = V;

0 commit comments

Comments
 (0)