Remove interior mutability in mir predecessors cache
This commit is contained in:
parent
4af3ee8ee2
commit
c16ef6b21d
4 changed files with 27 additions and 18 deletions
|
|
@ -1,5 +1,4 @@
|
|||
use rustc_index::vec::IndexVec;
|
||||
use rustc_data_structures::sync::{RwLock, MappedReadGuard, ReadGuard};
|
||||
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
|
||||
use rustc_serialize::{Encodable, Encoder, Decodable, Decoder};
|
||||
use crate::ich::StableHashingContext;
|
||||
|
|
@ -7,7 +6,7 @@ use crate::mir::{Body, BasicBlock};
|
|||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Cache {
|
||||
predecessors: RwLock<Option<IndexVec<BasicBlock, Vec<BasicBlock>>>>
|
||||
predecessors: Option<IndexVec<BasicBlock, Vec<BasicBlock>>>
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -32,24 +31,29 @@ impl<'a> HashStable<StableHashingContext<'a>> for Cache {
|
|||
impl Cache {
|
||||
pub fn new() -> Self {
|
||||
Cache {
|
||||
predecessors: RwLock::new(None)
|
||||
predecessors: None
|
||||
}
|
||||
}
|
||||
|
||||
pub fn invalidate(&self) {
|
||||
pub fn invalidate(&mut self) {
|
||||
// FIXME: consider being more fine-grained
|
||||
*self.predecessors.borrow_mut() = None;
|
||||
self.predecessors = None;
|
||||
}
|
||||
|
||||
pub fn predecessors(
|
||||
&self,
|
||||
pub fn predecessors_ref(&self) -> &IndexVec<BasicBlock, Vec<BasicBlock>> {
|
||||
assert!(self.predecessors.is_some());
|
||||
self.predecessors.as_ref().unwrap()
|
||||
}
|
||||
|
||||
pub fn predecessors_mut(
|
||||
&mut self,
|
||||
body: &Body<'_>
|
||||
) -> MappedReadGuard<'_, IndexVec<BasicBlock, Vec<BasicBlock>>> {
|
||||
if self.predecessors.borrow().is_none() {
|
||||
*self.predecessors.borrow_mut() = Some(calculate_predecessors(body));
|
||||
) -> &mut IndexVec<BasicBlock, Vec<BasicBlock>> {
|
||||
if self.predecessors.is_none() {
|
||||
self.predecessors = Some(calculate_predecessors(body));
|
||||
}
|
||||
|
||||
ReadGuard::map(self.predecessors.borrow(), |p| p.as_ref().unwrap())
|
||||
self.predecessors.as_mut().unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -25,7 +25,6 @@ use rustc_data_structures::graph::dominators::{dominators, Dominators};
|
|||
use rustc_data_structures::graph::{self, GraphPredecessors, GraphSuccessors};
|
||||
use rustc_index::vec::{Idx, IndexVec};
|
||||
use rustc_data_structures::sync::Lrc;
|
||||
use rustc_data_structures::sync::MappedReadGuard;
|
||||
use rustc_macros::HashStable;
|
||||
use rustc_serialize::{Encodable, Decodable};
|
||||
use smallvec::SmallVec;
|
||||
|
|
@ -218,13 +217,19 @@ impl<'tcx> Body<'tcx> {
|
|||
}
|
||||
|
||||
#[inline]
|
||||
pub fn predecessors(&self) -> MappedReadGuard<'_, IndexVec<BasicBlock, Vec<BasicBlock>>> {
|
||||
self.cache.predecessors(self)
|
||||
pub fn predecessors_ref(&self) -> &IndexVec<BasicBlock, Vec<BasicBlock>> {
|
||||
self.cache.predecessors_ref()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn predecessors_for(&self, bb: BasicBlock) -> MappedReadGuard<'_, Vec<BasicBlock>> {
|
||||
MappedReadGuard::map(self.predecessors(), |p| &p[bb])
|
||||
pub fn predecessors_mut(&mut self) -> &mut IndexVec<BasicBlock, Vec<BasicBlock>> {
|
||||
// TODO(nashenas88) figure out a way to get rid of this clone
|
||||
self.cache.predecessors_mut(&self.clone())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn predecessors_for(&self, bb: BasicBlock) -> &Vec<BasicBlock> {
|
||||
&self.predecessors_ref()[bb]
|
||||
}
|
||||
|
||||
#[inline]
|
||||
|
|
|
|||
|
|
@ -39,7 +39,7 @@ impl<'tcx> MirPass<'tcx> for AddCallGuards {
|
|||
impl AddCallGuards {
|
||||
pub fn add_call_guards(&self, body: &mut Body<'_>) {
|
||||
let pred_count: IndexVec<_, _> =
|
||||
body.predecessors().iter().map(|ps| ps.len()).collect();
|
||||
body.predecessors_mut().iter().map(|ps| ps.len()).collect();
|
||||
|
||||
// We need a place to store the new blocks generated
|
||||
let mut new_blocks = Vec::new();
|
||||
|
|
|
|||
|
|
@ -94,7 +94,7 @@ pub fn liveness_of_locals(
|
|||
dirty_queue.insert(bb);
|
||||
}
|
||||
|
||||
let predecessors = body.predecessors();
|
||||
let predecessors = body.predecessors_ref();
|
||||
|
||||
while let Some(bb) = dirty_queue.pop() {
|
||||
// bits = use ∪ (bits - def)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue