Skip to content

Refactor basic blocks control flow caches #106975

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

Merged
merged 1 commit into from
Jan 23, 2023
Merged
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
98 changes: 74 additions & 24 deletions compiler/rustc_middle/src/mir/basic_blocks.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,44 @@
use crate::mir::graph_cyclic_cache::GraphIsCyclicCache;
use crate::mir::predecessors::{PredecessorCache, Predecessors};
use crate::mir::switch_sources::{SwitchSourceCache, SwitchSources};
use crate::mir::traversal::PostorderCache;
use crate::mir::{BasicBlock, BasicBlockData, Successors, START_BLOCK};
use crate::mir::traversal::Postorder;
use crate::mir::{BasicBlock, BasicBlockData, Successors, Terminator, TerminatorKind, START_BLOCK};

use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::graph;
use rustc_data_structures::graph::dominators::{dominators, Dominators};
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_data_structures::sync::OnceCell;
use rustc_index::vec::IndexVec;
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
use smallvec::SmallVec;

#[derive(Clone, TyEncodable, TyDecodable, Debug, HashStable, TypeFoldable, TypeVisitable)]
pub struct BasicBlocks<'tcx> {
basic_blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>,
predecessor_cache: PredecessorCache,
switch_source_cache: SwitchSourceCache,
is_cyclic: GraphIsCyclicCache,
postorder_cache: PostorderCache,
cache: Cache,
}

// Typically 95%+ of basic blocks have 4 or fewer predecessors.
pub type Predecessors = IndexVec<BasicBlock, SmallVec<[BasicBlock; 4]>>;

pub type SwitchSources = FxHashMap<(BasicBlock, BasicBlock), SmallVec<[Option<u128>; 1]>>;

#[derive(Clone, Default, Debug)]
struct Cache {
predecessors: OnceCell<Predecessors>,
switch_sources: OnceCell<SwitchSources>,
is_cyclic: OnceCell<bool>,
postorder: OnceCell<Vec<BasicBlock>>,
}

impl<'tcx> BasicBlocks<'tcx> {
#[inline]
pub fn new(basic_blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>) -> Self {
BasicBlocks {
basic_blocks,
predecessor_cache: PredecessorCache::new(),
switch_source_cache: SwitchSourceCache::new(),
is_cyclic: GraphIsCyclicCache::new(),
postorder_cache: PostorderCache::new(),
}
BasicBlocks { basic_blocks, cache: Cache::default() }
}

/// Returns true if control-flow graph contains a cycle reachable from the `START_BLOCK`.
#[inline]
pub fn is_cfg_cyclic(&self) -> bool {
self.is_cyclic.is_cyclic(self)
*self.cache.is_cyclic.get_or_init(|| graph::is_cyclic(self))
}

#[inline]
Expand All @@ -43,20 +49,46 @@ impl<'tcx> BasicBlocks<'tcx> {
/// Returns predecessors for each basic block.
#[inline]
pub fn predecessors(&self) -> &Predecessors {
self.predecessor_cache.compute(&self.basic_blocks)
self.cache.predecessors.get_or_init(|| {
let mut preds = IndexVec::from_elem(SmallVec::new(), &self.basic_blocks);
for (bb, data) in self.basic_blocks.iter_enumerated() {
if let Some(term) = &data.terminator {
for succ in term.successors() {
preds[succ].push(bb);
}
}
}
preds
})
}

/// Returns basic blocks in a postorder.
#[inline]
pub fn postorder(&self) -> &[BasicBlock] {
self.postorder_cache.compute(&self.basic_blocks)
self.cache.postorder.get_or_init(|| {
Postorder::new(&self.basic_blocks, START_BLOCK).map(|(bb, _)| bb).collect()
})
}

/// `switch_sources()[&(target, switch)]` returns a list of switch
/// values that lead to a `target` block from a `switch` block.
#[inline]
pub fn switch_sources(&self) -> &SwitchSources {
self.switch_source_cache.compute(&self.basic_blocks)
self.cache.switch_sources.get_or_init(|| {
let mut switch_sources: SwitchSources = FxHashMap::default();
for (bb, data) in self.basic_blocks.iter_enumerated() {
if let Some(Terminator {
kind: TerminatorKind::SwitchInt { targets, .. }, ..
}) = &data.terminator
{
for (value, target) in targets.iter() {
switch_sources.entry((target, bb)).or_default().push(Some(value));
}
switch_sources.entry((targets.otherwise(), bb)).or_default().push(None);
}
}
switch_sources
})
}

/// Returns mutable reference to basic blocks. Invalidates CFG cache.
Expand Down Expand Up @@ -88,10 +120,7 @@ impl<'tcx> BasicBlocks<'tcx> {
/// All other methods that allow you to mutate the basic blocks also call this method
/// themselves, thereby avoiding any risk of accidentally cache invalidation.
pub fn invalidate_cfg_cache(&mut self) {
self.predecessor_cache.invalidate();
self.switch_source_cache.invalidate();
self.is_cyclic.invalidate();
self.postorder_cache.invalidate();
self.cache = Cache::default();
}
}

Expand Down Expand Up @@ -145,3 +174,24 @@ impl<'tcx> graph::WithPredecessors for BasicBlocks<'tcx> {
self.predecessors()[node].iter().copied()
}
}

TrivialTypeTraversalAndLiftImpls! {
Cache,
}

impl<S: Encoder> Encodable<S> for Cache {
#[inline]
fn encode(&self, _s: &mut S) {}
}

impl<D: Decoder> Decodable<D> for Cache {
#[inline]
fn decode(_: &mut D) -> Self {
Default::default()
}
}

impl<CTX> HashStable<CTX> for Cache {
#[inline]
fn hash_stable(&self, _: &mut CTX, _: &mut StableHasher) {}
}
63 changes: 0 additions & 63 deletions compiler/rustc_middle/src/mir/graph_cyclic_cache.rs

This file was deleted.

3 changes: 0 additions & 3 deletions compiler/rustc_middle/src/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,15 @@ mod basic_blocks;
pub mod coverage;
mod generic_graph;
pub mod generic_graphviz;
mod graph_cyclic_cache;
pub mod graphviz;
pub mod interpret;
pub mod mono;
pub mod patch;
mod predecessors;
pub mod pretty;
mod query;
pub mod spanview;
mod syntax;
pub use syntax::*;
mod switch_sources;
pub mod tcx;
pub mod terminator;
pub use terminator::*;
Expand Down
78 changes: 0 additions & 78 deletions compiler/rustc_middle/src/mir/predecessors.rs

This file was deleted.

78 changes: 0 additions & 78 deletions compiler/rustc_middle/src/mir/switch_sources.rs

This file was deleted.

Loading