-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
Cleanup BodyCache #66991
Cleanup BodyCache #66991
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -115,16 +115,16 @@ impl Cache { | |
} | ||
|
||
#[derive(Clone, Debug, HashStable, RustcEncodable, RustcDecodable, TypeFoldable)] | ||
pub struct BodyCache<'tcx> { | ||
cache: Cache, | ||
pub struct BodyAndCache<'tcx> { | ||
body: Body<'tcx>, | ||
cache: Cache, | ||
} | ||
|
||
impl BodyCache<'tcx> { | ||
impl BodyAndCache<'tcx> { | ||
pub fn new(body: Body<'tcx>) -> Self { | ||
Self { | ||
cache: Cache::new(), | ||
body, | ||
cache: Cache::new(), | ||
} | ||
} | ||
} | ||
|
@@ -139,7 +139,7 @@ macro_rules! read_only { | |
}; | ||
} | ||
|
||
impl BodyCache<'tcx> { | ||
impl BodyAndCache<'tcx> { | ||
pub fn ensure_predecessors(&mut self) { | ||
self.cache.ensure_predecessors(&self.body); | ||
} | ||
|
@@ -148,8 +148,8 @@ impl BodyCache<'tcx> { | |
self.cache.predecessors(&self.body) | ||
} | ||
|
||
pub fn unwrap_read_only(&self) -> ReadOnlyBodyCache<'_, 'tcx> { | ||
ReadOnlyBodyCache::new(&self.cache, &self.body) | ||
pub fn unwrap_read_only(&self) -> ReadOnlyBodyAndCache<'_, 'tcx> { | ||
ReadOnlyBodyAndCache::new(&self.body, &self.cache) | ||
} | ||
|
||
pub fn basic_blocks_mut(&mut self) -> &mut IndexVec<BasicBlock, BasicBlockData<'tcx>> { | ||
|
@@ -163,48 +163,48 @@ impl BodyCache<'tcx> { | |
} | ||
} | ||
|
||
impl<'tcx> Index<BasicBlock> for BodyCache<'tcx> { | ||
impl<'tcx> Index<BasicBlock> for BodyAndCache<'tcx> { | ||
type Output = BasicBlockData<'tcx>; | ||
|
||
fn index(&self, index: BasicBlock) -> &BasicBlockData<'tcx> { | ||
&self.body[index] | ||
} | ||
} | ||
|
||
impl<'tcx> IndexMut<BasicBlock> for BodyCache<'tcx> { | ||
impl<'tcx> IndexMut<BasicBlock> for BodyAndCache<'tcx> { | ||
fn index_mut(&mut self, index: BasicBlock) -> &mut Self::Output { | ||
&mut self.basic_blocks_mut()[index] | ||
} | ||
} | ||
|
||
impl<'tcx> Deref for BodyCache<'tcx> { | ||
impl<'tcx> Deref for BodyAndCache<'tcx> { | ||
type Target = Body<'tcx>; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
&self.body | ||
} | ||
} | ||
|
||
impl<'tcx> DerefMut for BodyCache<'tcx> { | ||
impl<'tcx> DerefMut for BodyAndCache<'tcx> { | ||
fn deref_mut(&mut self) -> &mut Self::Target { | ||
&mut self.body | ||
} | ||
} | ||
|
||
#[derive(Copy, Clone, Debug)] | ||
pub struct ReadOnlyBodyCache<'a, 'tcx> { | ||
cache: &'a Cache, | ||
pub struct ReadOnlyBodyAndCache<'a, 'tcx> { | ||
body: &'a Body<'tcx>, | ||
cache: &'a Cache, | ||
} | ||
|
||
impl ReadOnlyBodyCache<'a, 'tcx> { | ||
fn new(cache: &'a Cache, body: &'a Body<'tcx>) -> Self { | ||
impl ReadOnlyBodyAndCache<'a, 'tcx> { | ||
fn new(body: &'a Body<'tcx>, cache: &'a Cache) -> Self { | ||
assert!( | ||
cache.predecessors.is_some(), | ||
"Cannot construct ReadOnlyBodyCache without computed predecessors"); | ||
"Cannot construct ReadOnlyBodyAndCache without computed predecessors"); | ||
Self { | ||
cache, | ||
body, | ||
cache, | ||
} | ||
} | ||
|
||
|
@@ -220,10 +220,6 @@ impl ReadOnlyBodyCache<'a, 'tcx> { | |
self.cache.unwrap_predecessor_locations(loc, self.body) | ||
} | ||
|
||
pub fn body(&self) -> &'a Body<'tcx> { | ||
self.body | ||
} | ||
|
||
pub fn basic_blocks(&self) -> &IndexVec<BasicBlock, BasicBlockData<'tcx>> { | ||
&self.body.basic_blocks | ||
} | ||
|
@@ -233,16 +229,16 @@ impl ReadOnlyBodyCache<'a, 'tcx> { | |
} | ||
} | ||
|
||
impl graph::DirectedGraph for ReadOnlyBodyCache<'a, 'tcx> { | ||
impl graph::DirectedGraph for ReadOnlyBodyAndCache<'a, 'tcx> { | ||
type Node = BasicBlock; | ||
} | ||
|
||
impl graph::GraphPredecessors<'graph> for ReadOnlyBodyCache<'a, 'tcx> { | ||
impl graph::GraphPredecessors<'graph> for ReadOnlyBodyAndCache<'a, 'tcx> { | ||
type Item = BasicBlock; | ||
type Iter = IntoIter<BasicBlock>; | ||
} | ||
|
||
impl graph::WithPredecessors for ReadOnlyBodyCache<'a, 'tcx> { | ||
impl graph::WithPredecessors for ReadOnlyBodyAndCache<'a, 'tcx> { | ||
fn predecessors( | ||
&self, | ||
node: Self::Node, | ||
|
@@ -251,19 +247,19 @@ impl graph::WithPredecessors for ReadOnlyBodyCache<'a, 'tcx> { | |
} | ||
} | ||
|
||
impl graph::WithNumNodes for ReadOnlyBodyCache<'a, 'tcx> { | ||
impl graph::WithNumNodes for ReadOnlyBodyAndCache<'a, 'tcx> { | ||
fn num_nodes(&self) -> usize { | ||
self.body.num_nodes() | ||
} | ||
} | ||
|
||
impl graph::WithStartNode for ReadOnlyBodyCache<'a, 'tcx> { | ||
impl graph::WithStartNode for ReadOnlyBodyAndCache<'a, 'tcx> { | ||
fn start_node(&self) -> Self::Node { | ||
self.body.start_node() | ||
} | ||
} | ||
|
||
impl graph::WithSuccessors for ReadOnlyBodyCache<'a, 'tcx> { | ||
impl graph::WithSuccessors for ReadOnlyBodyAndCache<'a, 'tcx> { | ||
fn successors( | ||
&self, | ||
node: Self::Node, | ||
|
@@ -272,13 +268,13 @@ impl graph::WithSuccessors for ReadOnlyBodyCache<'a, 'tcx> { | |
} | ||
} | ||
|
||
impl<'a, 'b, 'tcx> graph::GraphSuccessors<'b> for ReadOnlyBodyCache<'a, 'tcx> { | ||
impl<'a, 'b, 'tcx> graph::GraphSuccessors<'b> for ReadOnlyBodyAndCache<'a, 'tcx> { | ||
type Item = BasicBlock; | ||
type Iter = iter::Cloned<Successors<'b>>; | ||
} | ||
|
||
|
||
impl Deref for ReadOnlyBodyCache<'a, 'tcx> { | ||
impl Deref for ReadOnlyBodyAndCache<'a, 'tcx> { | ||
type Target = &'a Body<'tcx>; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Btw as I mentioned on the original PR, I've made this change in #65947, so I'd prefer landing that first. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I saw that. I didn't plan to merge this until that one was merged first. I just wanted to make that change in this branch too so I could verify that the other changes wouldn't cause any issues. I'll update the main detail of the PR to make note of that. |
||
|
||
fn deref(&self) -> &Self::Target { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@eddyb Thank you so much for pointing on the
&'a
in theDeref
impl. That was the only reason this fn was left around, and it helped clean up a lot of code. CC @oli-obk