Skip to content

Pre-compute MIR CFG caches for borrowck and other analyses #142540

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions compiler/rustc_borrowck/src/renumber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ pub(crate) fn renumber_mir<'tcx>(
let mut renumberer = RegionRenumberer { infcx };

for body in promoted.iter_mut() {
renumberer.visit_body(body);
renumberer.visit_body_preserves_cfg(body);
}

renumberer.visit_body(body);
renumberer.visit_body_preserves_cfg(body);
}

// The fields are used only for debugging output in `sccs_info`.
Expand Down
17 changes: 13 additions & 4 deletions compiler/rustc_middle/src/mir/basic_blocks.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::sync::OnceLock;
use std::sync::{Arc, OnceLock};

use rustc_data_structures::graph;
use rustc_data_structures::graph::dominators::{Dominators, dominators};
Expand All @@ -14,7 +14,8 @@ use crate::mir::{BasicBlock, BasicBlockData, START_BLOCK};
#[derive(Clone, TyEncodable, TyDecodable, Debug, HashStable, TypeFoldable, TypeVisitable)]
pub struct BasicBlocks<'tcx> {
basic_blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>,
cache: Cache,
/// Use an `Arc` so we can share the cache when we clone the MIR body, as borrowck does.
cache: Arc<Cache>,
}

// Typically 95%+ of basic blocks have 4 or fewer predecessors.
Expand All @@ -38,9 +39,10 @@ struct Cache {
impl<'tcx> BasicBlocks<'tcx> {
#[inline]
pub fn new(basic_blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>) -> Self {
BasicBlocks { basic_blocks, cache: Cache::default() }
BasicBlocks { basic_blocks, cache: Arc::new(Cache::default()) }
}

#[inline]
pub fn dominators(&self) -> &Dominators<BasicBlock> {
self.cache.dominators.get_or_init(|| dominators(self))
}
Expand Down Expand Up @@ -104,7 +106,14 @@ 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.cache = Cache::default();
if let Some(cache) = Arc::get_mut(&mut self.cache) {
// If we only have a single reference to this cache, clear it.
*cache = Cache::default();
} else {
// If we have several references to this cache, overwrite the pointer itself so other
// users can continue to use their (valid) cache.
self.cache = Arc::new(Cache::default());
}
}
}

Expand Down
Loading