diff options
| author | Nicholas Nethercote <n.nethercote@gmail.com> | 2022-02-10 00:47:48 +1100 |
|---|---|---|
| committer | Nicholas Nethercote <n.nethercote@gmail.com> | 2022-02-23 10:18:49 +1100 |
| commit | 36b495f3cf23a1f235482ce7f81f0f4be614bb85 (patch) | |
| tree | bb651118e987cbe7467f088e84811ae93a676954 /compiler/rustc_mir_dataflow | |
| parent | 523a1b1d388bfe82a5d0540b876d9428b6dccc9c (diff) | |
| download | rust-36b495f3cf23a1f235482ce7f81f0f4be614bb85.tar.gz rust-36b495f3cf23a1f235482ce7f81f0f4be614bb85.zip | |
Introduce `ChunkedBitSet` and use it for some dataflow analyses.
This reduces peak memory usage significantly for some programs with very large functions, such as: - `keccak`, `unicode_normalization`, and `match-stress-enum`, from the `rustc-perf` benchmark suite; - `http-0.2.6` from crates.io. The new type is used in the analyses where the bitsets can get huge (e.g. 10s of thousands of bits): `MaybeInitializedPlaces`, `MaybeUninitializedPlaces`, and `EverInitializedPlaces`. Some refactoring was required in `rustc_mir_dataflow`. All existing analysis domains are either `BitSet` or a trivial wrapper around `BitSet`, and access in a few places is done via `Borrow<BitSet>` or `BorrowMut<BitSet>`. Now that some of these domains are `ClusterBitSet`, that no longer works. So this commit replaces the `Borrow`/`BorrowMut` usage with a new trait `BitSetExt` containing the needed bitset operations. The impls just forward these to the underlying bitset type. This required fiddling with trait bounds in a few places. The commit also: - Moves `static_assert_size` from `rustc_data_structures` to `rustc_index` so it can be used in the latter; the former now re-exports it so existing users are unaffected. - Factors out some common "clear excess bits in the final word" functionality in `bit_set.rs`. - Uses `fill` in a few places instead of loops.
Diffstat (limited to 'compiler/rustc_mir_dataflow')
| -rw-r--r-- | compiler/rustc_mir_dataflow/src/framework/cursor.rs | 12 | ||||
| -rw-r--r-- | compiler/rustc_mir_dataflow/src/framework/engine.rs | 9 | ||||
| -rw-r--r-- | compiler/rustc_mir_dataflow/src/framework/fmt.rs | 15 | ||||
| -rw-r--r-- | compiler/rustc_mir_dataflow/src/framework/lattice.rs | 35 | ||||
| -rw-r--r-- | compiler/rustc_mir_dataflow/src/framework/mod.rs | 62 | ||||
| -rw-r--r-- | compiler/rustc_mir_dataflow/src/impls/mod.rs | 14 | ||||
| -rw-r--r-- | compiler/rustc_mir_dataflow/src/rustc_peek.rs | 7 |
7 files changed, 121 insertions, 33 deletions
diff --git a/compiler/rustc_mir_dataflow/src/framework/cursor.rs b/compiler/rustc_mir_dataflow/src/framework/cursor.rs index ba6b566a304..2de5a9d6991 100644 --- a/compiler/rustc_mir_dataflow/src/framework/cursor.rs +++ b/compiler/rustc_mir_dataflow/src/framework/cursor.rs @@ -1,10 +1,12 @@ //! Random access inspection of the results of a dataflow analysis. +use crate::framework::BitSetExt; + use std::borrow::Borrow; use std::cmp::Ordering; +#[cfg(debug_assertions)] use rustc_index::bit_set::BitSet; -use rustc_index::vec::Idx; use rustc_middle::mir::{self, BasicBlock, Location}; use super::{Analysis, Direction, Effect, EffectIndex, Results}; @@ -209,13 +211,13 @@ where } } -impl<'mir, 'tcx, A, R, T> ResultsCursor<'mir, 'tcx, A, R> +impl<'mir, 'tcx, A, R> ResultsCursor<'mir, 'tcx, A, R> where - A: Analysis<'tcx, Domain = BitSet<T>>, - T: Idx, + A: crate::GenKillAnalysis<'tcx>, + A::Domain: BitSetExt<A::Idx>, R: Borrow<Results<'tcx, A>>, { - pub fn contains(&self, elem: T) -> bool { + pub fn contains(&self, elem: A::Idx) -> bool { self.get().contains(elem) } } diff --git a/compiler/rustc_mir_dataflow/src/framework/engine.rs b/compiler/rustc_mir_dataflow/src/framework/engine.rs index e20358ba989..88ed0128a1f 100644 --- a/compiler/rustc_mir_dataflow/src/framework/engine.rs +++ b/compiler/rustc_mir_dataflow/src/framework/engine.rs @@ -1,6 +1,7 @@ //! A solver for dataflow problems. -use std::borrow::BorrowMut; +use crate::framework::BitSetExt; + use std::ffi::OsString; use std::path::PathBuf; @@ -91,7 +92,7 @@ where impl<'a, 'tcx, A, D, T> Engine<'a, 'tcx, A> where A: GenKillAnalysis<'tcx, Idx = T, Domain = D>, - D: Clone + JoinSemiLattice + GenKill<T> + BorrowMut<BitSet<T>>, + D: Clone + JoinSemiLattice + GenKill<T> + BitSetExt<T>, T: Idx, { /// Creates a new `Engine` to solve a gen-kill dataflow problem. @@ -106,7 +107,7 @@ where // Otherwise, compute and store the cumulative transfer function for each block. - let identity = GenKillSet::identity(analysis.bottom_value(body).borrow().domain_size()); + let identity = GenKillSet::identity(analysis.bottom_value(body).domain_size()); let mut trans_for_block = IndexVec::from_elem(identity, body.basic_blocks()); for (block, block_data) in body.basic_blocks().iter_enumerated() { @@ -115,7 +116,7 @@ where } let apply_trans = Box::new(move |bb: BasicBlock, state: &mut A::Domain| { - trans_for_block[bb].apply(state.borrow_mut()); + trans_for_block[bb].apply(state); }); Self::new(tcx, body, analysis, Some(apply_trans as Box<_>)) diff --git a/compiler/rustc_mir_dataflow/src/framework/fmt.rs b/compiler/rustc_mir_dataflow/src/framework/fmt.rs index 1d1553bbbd9..99735673f4d 100644 --- a/compiler/rustc_mir_dataflow/src/framework/fmt.rs +++ b/compiler/rustc_mir_dataflow/src/framework/fmt.rs @@ -1,7 +1,7 @@ //! Custom formatting traits used when outputting Graphviz diagrams with the results of a dataflow //! analysis. -use rustc_index::bit_set::{BitSet, HybridBitSet}; +use rustc_index::bit_set::{BitSet, ChunkedBitSet, HybridBitSet}; use rustc_index::vec::Idx; use std::fmt; @@ -133,6 +133,19 @@ where } } +impl<T, C> DebugWithContext<C> for ChunkedBitSet<T> +where + T: Idx + DebugWithContext<C>, +{ + fn fmt_with(&self, _ctxt: &C, _f: &mut fmt::Formatter<'_>) -> fmt::Result { + unimplemented!("implement when/if needed"); + } + + fn fmt_diff_with(&self, _old: &Self, _ctxt: &C, _f: &mut fmt::Formatter<'_>) -> fmt::Result { + unimplemented!("implement when/if needed"); + } +} + impl<T, C> DebugWithContext<C> for &'_ T where T: DebugWithContext<C>, diff --git a/compiler/rustc_mir_dataflow/src/framework/lattice.rs b/compiler/rustc_mir_dataflow/src/framework/lattice.rs index f937b31f4cf..9a462f6e1a4 100644 --- a/compiler/rustc_mir_dataflow/src/framework/lattice.rs +++ b/compiler/rustc_mir_dataflow/src/framework/lattice.rs @@ -38,7 +38,8 @@ //! [Hasse diagram]: https://en.wikipedia.org/wiki/Hasse_diagram //! [poset]: https://en.wikipedia.org/wiki/Partially_ordered_set -use rustc_index::bit_set::BitSet; +use crate::framework::BitSetExt; +use rustc_index::bit_set::{BitSet, ChunkedBitSet, HybridBitSet}; use rustc_index::vec::{Idx, IndexVec}; use std::iter; @@ -145,6 +146,18 @@ impl<T: Idx> MeetSemiLattice for BitSet<T> { } } +impl<T: Idx> JoinSemiLattice for ChunkedBitSet<T> { + fn join(&mut self, other: &Self) -> bool { + self.union(other) + } +} + +impl<T: Idx> MeetSemiLattice for ChunkedBitSet<T> { + fn meet(&mut self, other: &Self) -> bool { + self.intersect(other) + } +} + /// The counterpart of a given semilattice `T` using the [inverse order]. /// /// The dual of a join-semilattice is a meet-semilattice and vice versa. For example, the dual of a @@ -155,15 +168,21 @@ impl<T: Idx> MeetSemiLattice for BitSet<T> { #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct Dual<T>(pub T); -impl<T> std::borrow::Borrow<T> for Dual<T> { - fn borrow(&self) -> &T { - &self.0 +impl<T: Idx> BitSetExt<T> for Dual<BitSet<T>> { + fn domain_size(&self) -> usize { + self.0.domain_size() + } + + fn contains(&self, elem: T) -> bool { + self.0.contains(elem) + } + + fn union(&mut self, other: &HybridBitSet<T>) { + self.0.union(other); } -} -impl<T> std::borrow::BorrowMut<T> for Dual<T> { - fn borrow_mut(&mut self) -> &mut T { - &mut self.0 + fn subtract(&mut self, other: &HybridBitSet<T>) { + self.0.subtract(other); } } diff --git a/compiler/rustc_mir_dataflow/src/framework/mod.rs b/compiler/rustc_mir_dataflow/src/framework/mod.rs index dac6720a6e5..c51dd06de25 100644 --- a/compiler/rustc_mir_dataflow/src/framework/mod.rs +++ b/compiler/rustc_mir_dataflow/src/framework/mod.rs @@ -30,10 +30,9 @@ //! //! [gen-kill]: https://en.wikipedia.org/wiki/Data-flow_analysis#Bit_vector_problems -use std::borrow::BorrowMut; use std::cmp::Ordering; -use rustc_index::bit_set::{BitSet, HybridBitSet}; +use rustc_index::bit_set::{BitSet, ChunkedBitSet, HybridBitSet}; use rustc_index::vec::Idx; use rustc_middle::mir::{self, BasicBlock, Location}; use rustc_middle::ty::TyCtxt; @@ -52,6 +51,51 @@ pub use self::engine::{Engine, Results}; pub use self::lattice::{JoinSemiLattice, MeetSemiLattice}; pub use self::visitor::{visit_results, ResultsVisitable, ResultsVisitor}; +/// Analysis domains are all bitsets of various kinds. This trait holds +/// operations needed by all of them. +pub trait BitSetExt<T> { + fn domain_size(&self) -> usize; + fn contains(&self, elem: T) -> bool; + fn union(&mut self, other: &HybridBitSet<T>); + fn subtract(&mut self, other: &HybridBitSet<T>); +} + +impl<T: Idx> BitSetExt<T> for BitSet<T> { + fn domain_size(&self) -> usize { + self.domain_size() + } + + fn contains(&self, elem: T) -> bool { + self.contains(elem) + } + + fn union(&mut self, other: &HybridBitSet<T>) { + self.union(other); + } + + fn subtract(&mut self, other: &HybridBitSet<T>) { + self.subtract(other); + } +} + +impl<T: Idx> BitSetExt<T> for ChunkedBitSet<T> { + fn domain_size(&self) -> usize { + self.domain_size() + } + + fn contains(&self, elem: T) -> bool { + self.contains(elem) + } + + fn union(&mut self, other: &HybridBitSet<T>) { + self.union(other); + } + + fn subtract(&mut self, other: &HybridBitSet<T>) { + self.subtract(other); + } +} + /// Define the domain of a dataflow problem. /// /// This trait specifies the lattice on which this analysis operates (the domain) as well as its @@ -303,7 +347,7 @@ pub trait GenKillAnalysis<'tcx>: Analysis<'tcx> { impl<'tcx, A> Analysis<'tcx> for A where A: GenKillAnalysis<'tcx>, - A::Domain: GenKill<A::Idx> + BorrowMut<BitSet<A::Idx>>, + A::Domain: GenKill<A::Idx> + BitSetExt<A::Idx>, { fn apply_statement_effect( &self, @@ -435,7 +479,7 @@ impl<T: Idx> GenKillSet<T> { } } - pub fn apply(&self, state: &mut BitSet<T>) { + pub fn apply(&self, state: &mut impl BitSetExt<T>) { state.union(&self.gen); state.subtract(&self.kill); } @@ -463,6 +507,16 @@ impl<T: Idx> GenKill<T> for BitSet<T> { } } +impl<T: Idx> GenKill<T> for ChunkedBitSet<T> { + fn gen(&mut self, elem: T) { + self.insert(elem); + } + + fn kill(&mut self, elem: T) { + self.remove(elem); + } +} + impl<T: Idx> GenKill<T> for lattice::Dual<BitSet<T>> { fn gen(&mut self, elem: T) { self.0.insert(elem); diff --git a/compiler/rustc_mir_dataflow/src/impls/mod.rs b/compiler/rustc_mir_dataflow/src/impls/mod.rs index 63d935db8ca..9502c5d57d6 100644 --- a/compiler/rustc_mir_dataflow/src/impls/mod.rs +++ b/compiler/rustc_mir_dataflow/src/impls/mod.rs @@ -2,7 +2,7 @@ //! bitvectors attached to each basic block, represented via a //! zero-sized structure. -use rustc_index::bit_set::BitSet; +use rustc_index::bit_set::{BitSet, ChunkedBitSet}; use rustc_index::vec::Idx; use rustc_middle::mir::visit::{MirVisitable, Visitor}; use rustc_middle::mir::{self, Body, Location}; @@ -286,12 +286,12 @@ impl<'a, 'tcx> DefinitelyInitializedPlaces<'a, 'tcx> { } impl<'tcx> AnalysisDomain<'tcx> for MaybeInitializedPlaces<'_, 'tcx> { - type Domain = BitSet<MovePathIndex>; + type Domain = ChunkedBitSet<MovePathIndex>; const NAME: &'static str = "maybe_init"; fn bottom_value(&self, _: &mir::Body<'tcx>) -> Self::Domain { // bottom = uninitialized - BitSet::new_empty(self.move_data().move_paths.len()) + ChunkedBitSet::new_empty(self.move_data().move_paths.len()) } fn initialize_start_block(&self, _: &mir::Body<'tcx>, state: &mut Self::Domain) { @@ -417,13 +417,13 @@ impl<'tcx> GenKillAnalysis<'tcx> for MaybeInitializedPlaces<'_, 'tcx> { } impl<'tcx> AnalysisDomain<'tcx> for MaybeUninitializedPlaces<'_, 'tcx> { - type Domain = BitSet<MovePathIndex>; + type Domain = ChunkedBitSet<MovePathIndex>; const NAME: &'static str = "maybe_uninit"; fn bottom_value(&self, _: &mir::Body<'tcx>) -> Self::Domain { // bottom = initialized (start_block_effect counters this at outset) - BitSet::new_empty(self.move_data().move_paths.len()) + ChunkedBitSet::new_empty(self.move_data().move_paths.len()) } // sets on_entry bits for Arg places @@ -606,13 +606,13 @@ impl<'tcx> GenKillAnalysis<'tcx> for DefinitelyInitializedPlaces<'_, 'tcx> { } impl<'tcx> AnalysisDomain<'tcx> for EverInitializedPlaces<'_, 'tcx> { - type Domain = BitSet<InitIndex>; + type Domain = ChunkedBitSet<InitIndex>; const NAME: &'static str = "ever_init"; fn bottom_value(&self, _: &mir::Body<'tcx>) -> Self::Domain { // bottom = no initialized variables by default - BitSet::new_empty(self.move_data().inits.len()) + ChunkedBitSet::new_empty(self.move_data().inits.len()) } fn initialize_start_block(&self, body: &mir::Body<'tcx>, state: &mut Self::Domain) { diff --git a/compiler/rustc_mir_dataflow/src/rustc_peek.rs b/compiler/rustc_mir_dataflow/src/rustc_peek.rs index 1746d5ee38b..51ab5b43bff 100644 --- a/compiler/rustc_mir_dataflow/src/rustc_peek.rs +++ b/compiler/rustc_mir_dataflow/src/rustc_peek.rs @@ -1,5 +1,3 @@ -use std::borrow::Borrow; - use rustc_ast::ast; use rustc_span::symbol::sym; use rustc_span::Span; @@ -10,6 +8,7 @@ use rustc_middle::mir::MirPass; use rustc_middle::mir::{self, Body, Local, Location}; use rustc_middle::ty::{self, Ty, TyCtxt}; +use crate::framework::BitSetExt; use crate::impls::{ DefinitelyInitializedPlaces, MaybeInitializedPlaces, MaybeLiveLocals, MaybeUninitializedPlaces, }; @@ -248,7 +247,7 @@ pub trait RustcPeekAt<'tcx>: Analysis<'tcx> { impl<'tcx, A, D> RustcPeekAt<'tcx> for A where A: Analysis<'tcx, Domain = D> + HasMoveData<'tcx>, - D: JoinSemiLattice + Clone + Borrow<BitSet<MovePathIndex>>, + D: JoinSemiLattice + Clone + BitSetExt<MovePathIndex>, { fn peek_at( &self, @@ -259,7 +258,7 @@ where ) { match self.move_data().rev_lookup.find(place.as_ref()) { LookupResult::Exact(peek_mpi) => { - let bit_state = flow_state.borrow().contains(peek_mpi); + let bit_state = flow_state.contains(peek_mpi); debug!("rustc_peek({:?} = &{:?}) bit_state: {}", call.arg, place, bit_state); if !bit_state { tcx.sess.span_err(call.span, "rustc_peek: bit not set"); |
