Rollup merge of #66991 - Nashenas88:body_cache_cleanup, r=eddyb

Cleanup BodyCache

After this PR:

- `BodyCache` is renamed to `BodyAndCache`
- `ReadOnlyBodyCache` is renamed to `ReadOnlyBodyAndCache`
- `ReadOnlyBodyAndCache::body` fn is removed and all calls to it are replaced by a deref (possible due to fix of its `Deref` imp in #65947)

cc @eddyb @oli-obk
This commit is contained in:
Mazdak Farrokhzad 2019-12-08 03:39:45 +01:00 committed by GitHub
commit 56c0bea390
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
58 changed files with 226 additions and 224 deletions

View file

@ -23,17 +23,17 @@ macro_rules! arena_types {
[] generics: rustc::ty::Generics,
[] trait_def: rustc::ty::TraitDef,
[] adt_def: rustc::ty::AdtDef,
[] steal_mir: rustc::ty::steal::Steal<rustc::mir::BodyCache<$tcx>>,
[] mir: rustc::mir::BodyCache<$tcx>,
[] steal_mir: rustc::ty::steal::Steal<rustc::mir::BodyAndCache<$tcx>>,
[] mir: rustc::mir::BodyAndCache<$tcx>,
[] steal_promoted: rustc::ty::steal::Steal<
rustc_index::vec::IndexVec<
rustc::mir::Promoted,
rustc::mir::BodyCache<$tcx>
rustc::mir::BodyAndCache<$tcx>
>
>,
[] promoted: rustc_index::vec::IndexVec<
rustc::mir::Promoted,
rustc::mir::BodyCache<$tcx>
rustc::mir::BodyAndCache<$tcx>
>,
[] tables: rustc::ty::TypeckTables<$tcx>,
[] const_allocs: rustc::mir::interpret::Allocation,

View file

@ -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,7 +163,7 @@ 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> {
@ -171,13 +171,13 @@ impl<'tcx> Index<BasicBlock> for BodyCache<'tcx> {
}
}
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 {
@ -185,26 +185,26 @@ impl<'tcx> Deref for BodyCache<'tcx> {
}
}
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>;
fn deref(&self) -> &Self::Target {

View file

@ -38,7 +38,7 @@ use syntax::symbol::Symbol;
use syntax_pos::{Span, DUMMY_SP};
pub use crate::mir::interpret::AssertMessage;
pub use crate::mir::cache::{BodyCache, ReadOnlyBodyCache};
pub use crate::mir::cache::{BodyAndCache, ReadOnlyBodyAndCache};
pub use crate::read_only;
mod cache;
@ -108,7 +108,7 @@ pub struct Body<'tcx> {
pub yield_ty: Option<Ty<'tcx>>,
/// Generator drop glue.
pub generator_drop: Option<Box<BodyCache<'tcx>>>,
pub generator_drop: Option<Box<BodyAndCache<'tcx>>>,
/// The layout of a generator. Produced by the state transformation.
pub generator_layout: Option<GeneratorLayout<'tcx>>,
@ -2597,7 +2597,7 @@ impl Location {
pub fn is_predecessor_of<'tcx>(
&self,
other: Location,
body: ReadOnlyBodyCache<'_, 'tcx>
body: ReadOnlyBodyAndCache<'_, 'tcx>
) -> bool {
// If we are in the same block as the other location and are an earlier statement
// then we are a predecessor of `other`.

View file

@ -67,10 +67,10 @@ use syntax_pos::Span;
macro_rules! body_cache_type {
(mut $a:lifetime, $tcx:lifetime) => {
&mut BodyCache<$tcx>
&mut BodyAndCache<$tcx>
};
($a:lifetime, $tcx:lifetime) => {
ReadOnlyBodyCache<$a, $tcx>
ReadOnlyBodyAndCache<$a, $tcx>
};
}

View file

@ -106,30 +106,30 @@ rustc_queries! {
/// Fetch the MIR for a given `DefId` right after it's built - this includes
/// unreachable code.
query mir_built(_: DefId) -> &'tcx Steal<mir::BodyCache<'tcx>> {}
query mir_built(_: DefId) -> &'tcx Steal<mir::BodyAndCache<'tcx>> {}
/// Fetch the MIR for a given `DefId` up till the point where it is
/// ready for const evaluation.
///
/// See the README for the `mir` module for details.
query mir_const(_: DefId) -> &'tcx Steal<mir::BodyCache<'tcx>> {
query mir_const(_: DefId) -> &'tcx Steal<mir::BodyAndCache<'tcx>> {
no_hash
}
query mir_validated(_: DefId) ->
(
&'tcx Steal<mir::BodyCache<'tcx>>,
&'tcx Steal<IndexVec<mir::Promoted, mir::BodyCache<'tcx>>>
&'tcx Steal<mir::BodyAndCache<'tcx>>,
&'tcx Steal<IndexVec<mir::Promoted, mir::BodyAndCache<'tcx>>>
) {
no_hash
}
/// MIR after our optimization passes have run. This is MIR that is ready
/// for codegen. This is also the only query that can fetch non-local MIR, at present.
query optimized_mir(key: DefId) -> &'tcx mir::BodyCache<'tcx> {
query optimized_mir(key: DefId) -> &'tcx mir::BodyAndCache<'tcx> {
cache_on_disk_if { key.is_local() }
load_cached(tcx, id) {
let mir: Option<crate::mir::BodyCache<'tcx>>
let mir: Option<crate::mir::BodyAndCache<'tcx>>
= tcx.queries.on_disk_cache.try_load_query_result(tcx, id);
mir.map(|x| {
let cache = tcx.arena.alloc(x);
@ -139,13 +139,13 @@ rustc_queries! {
}
}
query promoted_mir(key: DefId) -> &'tcx IndexVec<mir::Promoted, mir::BodyCache<'tcx>> {
query promoted_mir(key: DefId) -> &'tcx IndexVec<mir::Promoted, mir::BodyAndCache<'tcx>> {
cache_on_disk_if { key.is_local() }
load_cached(tcx, id) {
let promoted: Option<
rustc_index::vec::IndexVec<
crate::mir::Promoted,
crate::mir::BodyCache<'tcx>
crate::mir::BodyAndCache<'tcx>
>> = tcx.queries.on_disk_cache.try_load_query_result(tcx, id);
promoted.map(|p| {
let cache = tcx.arena.alloc(p);
@ -512,7 +512,7 @@ rustc_queries! {
/// in the case of closures, this will be redirected to the enclosing function.
query region_scope_tree(_: DefId) -> &'tcx region::ScopeTree {}
query mir_shims(key: ty::InstanceDef<'tcx>) -> &'tcx mir::BodyCache<'tcx> {
query mir_shims(key: ty::InstanceDef<'tcx>) -> &'tcx mir::BodyAndCache<'tcx> {
no_force
desc { |tcx| "generating MIR shim for `{}`", tcx.def_path_str(key.def_id()) }
}

View file

@ -23,7 +23,7 @@ use crate::middle::cstore::EncodedMetadata;
use crate::middle::lang_items;
use crate::middle::resolve_lifetime::{self, ObjectLifetimeDefault};
use crate::middle::stability;
use crate::mir::{BodyCache, Field, interpret, Local, Place, PlaceElem, ProjectionKind, Promoted};
use crate::mir::{BodyAndCache, Field, interpret, Local, Place, PlaceElem, ProjectionKind, Promoted};
use crate::mir::interpret::{ConstValue, Allocation, Scalar};
use crate::ty::subst::{GenericArg, InternalSubsts, SubstsRef, Subst};
use crate::ty::ReprOptions;
@ -1084,17 +1084,17 @@ impl<'tcx> TyCtxt<'tcx> {
&self.hir_map
}
pub fn alloc_steal_mir(self, mir: BodyCache<'tcx>) -> &'tcx Steal<BodyCache<'tcx>> {
pub fn alloc_steal_mir(self, mir: BodyAndCache<'tcx>) -> &'tcx Steal<BodyAndCache<'tcx>> {
self.arena.alloc(Steal::new(mir))
}
pub fn alloc_steal_promoted(self, promoted: IndexVec<Promoted, BodyCache<'tcx>>) ->
&'tcx Steal<IndexVec<Promoted, BodyCache<'tcx>>> {
pub fn alloc_steal_promoted(self, promoted: IndexVec<Promoted, BodyAndCache<'tcx>>) ->
&'tcx Steal<IndexVec<Promoted, BodyAndCache<'tcx>>> {
self.arena.alloc(Steal::new(promoted))
}
pub fn intern_promoted(self, promoted: IndexVec<Promoted, BodyCache<'tcx>>) ->
&'tcx IndexVec<Promoted, BodyCache<'tcx>> {
pub fn intern_promoted(self, promoted: IndexVec<Promoted, BodyAndCache<'tcx>>) ->
&'tcx IndexVec<Promoted, BodyAndCache<'tcx>> {
self.arena.alloc(promoted)
}

View file

@ -18,7 +18,7 @@ use crate::infer::canonical::Canonical;
use crate::middle::cstore::CrateStoreDyn;
use crate::middle::lang_items::{FnTraitLangItem, FnMutTraitLangItem, FnOnceTraitLangItem};
use crate::middle::resolve_lifetime::ObjectLifetimeDefault;
use crate::mir::ReadOnlyBodyCache;
use crate::mir::ReadOnlyBodyAndCache;
use crate::mir::interpret::{GlobalId, ErrorHandled};
use crate::mir::GeneratorLayout;
use crate::session::CrateDisambiguator;
@ -2981,7 +2981,7 @@ impl<'tcx> TyCtxt<'tcx> {
}
/// Returns the possibly-auto-generated MIR of a `(DefId, Subst)` pair.
pub fn instance_mir(self, instance: ty::InstanceDef<'tcx>) -> ReadOnlyBodyCache<'tcx, 'tcx> {
pub fn instance_mir(self, instance: ty::InstanceDef<'tcx>) -> ReadOnlyBodyAndCache<'tcx, 'tcx> {
match instance {
ty::InstanceDef::Item(did) => {
self.optimized_mir(did).unwrap_read_only()