Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deterministic FxHashSet wrapper #94188

Closed
wants to merge 4 commits into from
Closed
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
15 changes: 6 additions & 9 deletions compiler/rustc_codegen_ssa/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -910,17 +910,14 @@ pub fn provide(providers: &mut Providers) {
};

let (defids, _) = tcx.collect_and_partition_mono_items(cratenum);
for id in &*defids {
if defids.any(|x| {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if defids.any(|x| {
if defids.any(|id| {

let CodegenFnAttrs { optimize, .. } = tcx.codegen_fn_attrs(*id);
match optimize {
attr::OptimizeAttr::None => continue,
attr::OptimizeAttr::Size => continue,
attr::OptimizeAttr::Speed => {
return for_speed;
}
}
optimize == attr::OptimizeAttr::Speed
}) {
for_speed
} else {
tcx.sess.opts.optimize
}
tcx.sess.opts.optimize
};
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_data_structures/src/fx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ pub type FxIndexSet<V> = indexmap::IndexSet<V, BuildHasherDefault<FxHasher>>;
macro_rules! define_id_collections {
($map_name:ident, $set_name:ident, $key:ty) => {
pub type $map_name<T> = $crate::fx::FxHashMap<$key, T>;
pub type $set_name = $crate::fx::FxHashSet<$key>;
pub type $set_name = $crate::stable_set::StableSet<$key>;
};
}
1 change: 1 addition & 0 deletions compiler/rustc_data_structures/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ pub mod svh;
pub use ena::snapshot_vec;
pub mod memmap;
pub mod sorted_map;
pub mod stable_iterator;
pub mod stable_set;
#[macro_use]
pub mod stable_hasher;
Expand Down
15 changes: 1 addition & 14 deletions compiler/rustc_data_structures/src/stable_hasher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -544,19 +544,6 @@ where
}
}

impl<K, R, HCX> HashStable<HCX> for ::std::collections::HashSet<K, R>
where
K: ToStableHashKey<HCX> + Eq,
R: BuildHasher,
{
fn hash_stable(&self, hcx: &mut HCX, hasher: &mut StableHasher) {
stable_hash_reduce(hcx, hasher, self.iter(), self.len(), |hasher, hcx, key| {
let key = key.to_stable_hash_key(hcx);
key.hash_stable(hcx, hasher);
});
}
}

impl<K, V, HCX> HashStable<HCX> for ::std::collections::BTreeMap<K, V>
where
K: ToStableHashKey<HCX>,
Expand All @@ -583,7 +570,7 @@ where
}
}

fn stable_hash_reduce<HCX, I, C, F>(
pub fn stable_hash_reduce<HCX, I, C, F>(
hcx: &mut HCX,
hasher: &mut StableHasher,
mut collection: C,
Expand Down
73 changes: 73 additions & 0 deletions compiler/rustc_data_structures/src/stable_iterator.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use std::iter::Chain;

use crate::stable_hasher::ToStableHashKey;

pub struct StableIterator<I: Iterator> {
inner: I,
}

impl<T, I: Iterator<Item = T>> StableIterator<I> {
#[inline]
pub fn map<U, F: Fn(T) -> U>(self, f: F) -> StableIterator<impl Iterator<Item = U>> {
StableIterator { inner: self.inner.map(f) }
}

#[inline]
pub fn into_sorted<HCX>(self, hcx: &HCX) -> Vec<T>
where
T: ToStableHashKey<HCX>,
{
let mut items: Vec<T> = self.inner.collect();
items.sort_by_cached_key(|x| x.to_stable_hash_key(hcx));
items
}

#[inline]
pub fn any<F: Fn(T) -> bool>(&mut self, f: F) -> bool {
self.inner.any(f)
}

#[inline]
pub fn all<F: Fn(T) -> bool>(&mut self, f: F) -> bool {
self.inner.all(f)
}

#[inline]
pub fn chain<J: Iterator<Item = I::Item>>(self, other: StableIterator<J>) -> StableChain<I, J> {
self.inner.chain(other.inner).into()
}
}

pub trait IntoStableIterator {
type IntoIter: Iterator;
fn into_stable_iter(self) -> StableIterator<Self::IntoIter>;
}

impl<I: Iterator, S: IntoIterator<Item = I::Item, IntoIter = I>> IntoStableIterator for S {
type IntoIter = I;

#[inline]
fn into_stable_iter(self) -> StableIterator<I> {
StableIterator { inner: self.into_iter() }
}
}

pub struct StableChain<I: Iterator, J: Iterator> {
inner: Chain<I, J>,
}

impl<I: Iterator, J: Iterator<Item = I::Item>> IntoStableIterator for StableChain<I, J> {
type IntoIter = Chain<I, J>;

#[inline]
fn into_stable_iter(self) -> StableIterator<Self::IntoIter> {
self.inner.into_stable_iter()
}
}

impl<I: Iterator, J: Iterator> From<Chain<I, J>> for StableChain<I, J> {
#[inline]
fn from(inner: Chain<I, J>) -> Self {
Self { inner }
}
}
102 changes: 97 additions & 5 deletions compiler/rustc_data_structures/src/stable_set.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
pub use rustc_hash::FxHashSet;
use std::borrow::Borrow;
use std::collections::hash_set;
use std::fmt;
use std::hash::Hash;

use crate::stable_hasher::{stable_hash_reduce, HashStable, StableHasher, ToStableHashKey};
use crate::stable_iterator::{IntoStableIterator, StableIterator};

/// A deterministic wrapper around FxHashSet that does not provide iteration support.
///
/// It supports insert, remove, get functions from FxHashSet.
/// It also allows to convert hashset to a sorted vector with the method `into_sorted_vector()`.
#[derive(Clone)]
pub struct StableSet<T> {
#[derive(Clone, Encodable, Decodable)]
pub struct StableSet<T>
where
T: Eq + Hash,
{
base: FxHashSet<T>,
}

impl<T> Default for StableSet<T>
where
T: Eq + Hash,
{
#[inline]
fn default() -> StableSet<T> {
StableSet::new()
}
Expand All @@ -25,6 +33,7 @@ impl<T> fmt::Debug for StableSet<T>
where
T: Eq + Hash + fmt::Debug,
{
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}", self.base)
}
Expand All @@ -34,6 +43,7 @@ impl<T> PartialEq<StableSet<T>> for StableSet<T>
where
T: Eq + Hash,
{
#[inline]
fn eq(&self, other: &StableSet<T>) -> bool {
self.base == other.base
}
Expand All @@ -42,19 +52,32 @@ where
impl<T> Eq for StableSet<T> where T: Eq + Hash {}

impl<T: Hash + Eq> StableSet<T> {
#[inline]
pub fn new() -> StableSet<T> {
StableSet { base: FxHashSet::default() }
}

pub fn into_sorted_vector(self) -> Vec<T>
#[inline]
pub fn into_sorted_vector<HCX>(self, hcx: &HCX) -> Vec<T>
where
T: Ord,
T: ToStableHashKey<HCX>,
{
let mut vector = self.base.into_iter().collect::<Vec<_>>();
vector.sort_unstable();
vector.sort_by_cached_key(|x| x.to_stable_hash_key(hcx));
vector
}

#[inline]
pub fn sorted_vector<HCX>(&self, hcx: &HCX) -> Vec<&T>
where
T: ToStableHashKey<HCX>,
{
let mut vector = self.base.iter().collect::<Vec<_>>();
vector.sort_by_cached_key(|x| x.to_stable_hash_key(hcx));
vector
}

#[inline]
pub fn get<Q: ?Sized>(&self, value: &Q) -> Option<&T>
where
T: Borrow<Q>,
Expand All @@ -63,15 +86,84 @@ impl<T: Hash + Eq> StableSet<T> {
self.base.get(value)
}

#[inline]
pub fn insert(&mut self, value: T) -> bool {
self.base.insert(value)
}

#[inline]
pub fn remove<Q: ?Sized>(&mut self, value: &Q) -> bool
where
T: Borrow<Q>,
Q: Hash + Eq,
{
self.base.remove(value)
}

#[inline]
pub fn contains(&self, value: &T) -> bool {
self.base.contains(value)
}

pub fn stable_iter<'a>(&'a self) -> StableIterator<hash_set::Iter<'a, T>> {
(&self).into_stable_iter()
}
}

impl<T, HCX> HashStable<HCX> for StableSet<T>
where
T: ToStableHashKey<HCX> + Eq + Hash,
{
#[inline]
fn hash_stable(&self, hcx: &mut HCX, hasher: &mut StableHasher) {
stable_hash_reduce(hcx, hasher, self.base.iter(), self.base.len(), |hasher, hcx, key| {
let key = key.to_stable_hash_key(hcx);
key.hash_stable(hcx, hasher);
});
}
}

impl<T> FromIterator<T> for StableSet<T>
where
T: Eq + Hash,
{
#[inline]
fn from_iter<Collection: IntoIterator<Item = T>>(iter: Collection) -> Self {
Self { base: iter.into_iter().collect() }
}
}

impl<T: Eq + Hash> IntoStableIterator for StableSet<T> {
type IntoIter = hash_set::IntoIter<T>;
#[inline]
fn into_stable_iter(self) -> StableIterator<Self::IntoIter> {
self.base.into_stable_iter()
}
}

impl<'a, T: Eq + Hash> IntoStableIterator for &'a StableSet<T> {
type IntoIter = hash_set::Iter<'a, T>;
#[inline]
fn into_stable_iter(self) -> StableIterator<Self::IntoIter> {
self.base.iter().into_stable_iter()
}
}

impl<T> From<FxHashSet<T>> for StableSet<T>
where
T: Eq + Hash,
{
fn from(base: FxHashSet<T>) -> Self {
Self { base: base }
}
}

impl<T> Extend<T> for StableSet<T>
where
T: Eq + Hash,
{
#[inline]
fn extend<Iter: IntoIterator<Item = T>>(&mut self, iter: Iter) {
self.base.extend(iter)
}
}
2 changes: 1 addition & 1 deletion compiler/rustc_metadata/src/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1742,7 +1742,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
empty_proc_macro!(self);
let tcx = self.tcx;
let lib_features = tcx.lib_features(());
self.lazy(lib_features.to_vec())
self.lazy(lib_features.to_vec(tcx))
}

fn encode_diagnostic_items(&mut self) -> Lazy<[(Symbol, DefIndex)]> {
Expand Down
11 changes: 7 additions & 4 deletions compiler/rustc_middle/src/middle/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,26 @@ pub mod dependency_format;
pub mod exported_symbols;
pub mod lang_items;
pub mod lib_features {
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_data_structures::{fx::FxHashMap, stable_set::StableSet};
use rustc_span::symbol::Symbol;

use crate::ty::TyCtxt;

#[derive(HashStable, Debug)]
pub struct LibFeatures {
// A map from feature to stabilisation version.
pub stable: FxHashMap<Symbol, Symbol>,
pub unstable: FxHashSet<Symbol>,
pub unstable: StableSet<Symbol>,
}

impl LibFeatures {
pub fn to_vec(&self) -> Vec<(Symbol, Option<Symbol>)> {
pub fn to_vec(&self, tcx: TyCtxt<'_>) -> Vec<(Symbol, Option<Symbol>)> {
let hcx = tcx.create_stable_hashing_context();
let mut all_features: Vec<_> = self
.stable
.iter()
.map(|(f, s)| (*f, Some(*s)))
.chain(self.unstable.iter().map(|f| (*f, None)))
.chain(self.unstable.sorted_vector(&hcx).into_iter().map(|f| (*f, None)))
Copy link
Member

@bjorn3 bjorn3 Mar 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same because of the sort below.

.collect();
all_features.sort_unstable_by(|a, b| a.0.as_str().partial_cmp(b.0.as_str()).unwrap());
all_features
Expand Down
5 changes: 3 additions & 2 deletions compiler/rustc_middle/src/middle/resolve_lifetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

use crate::ty;

use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::stable_set::StableSet;
use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_hir::ItemLocalId;
use rustc_macros::HashStable;
Expand Down Expand Up @@ -63,7 +64,7 @@ pub struct ResolveLifetimes {
/// Set of lifetime def ids that are late-bound; a region can
/// be late-bound if (a) it does NOT appear in a where-clause and
/// (b) it DOES appear in the arguments.
pub late_bound: FxHashMap<LocalDefId, FxHashSet<ItemLocalId>>,
pub late_bound: FxHashMap<LocalDefId, StableSet<ItemLocalId>>,

pub late_bound_vars: FxHashMap<LocalDefId, FxHashMap<ItemLocalId, Vec<ty::BoundVariableKind>>>,
}
Loading