Skip to content

Commit

Permalink
Rollup merge of rust-lang#66991 - Nashenas88:body_cache_cleanup, r=eddyb
Browse files Browse the repository at this point in the history
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 rust-lang#65947)

cc @eddyb @oli-obk
  • Loading branch information
Centril authored Dec 8, 2019
2 parents 56f830e + a5e144b commit 56c0bea
Show file tree
Hide file tree
Showing 58 changed files with 226 additions and 224 deletions.
8 changes: 4 additions & 4 deletions src/librustc/arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
54 changes: 25 additions & 29 deletions src/librustc/mir/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
}
}
Expand All @@ -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);
}
Expand All @@ -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>> {
Expand All @@ -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,
}
}

Expand All @@ -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
}
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions src/librustc/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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>>,
Expand Down Expand Up @@ -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`.
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/mir/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>
};
}

Expand Down
18 changes: 9 additions & 9 deletions src/librustc/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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()) }
}
Expand Down
12 changes: 6 additions & 6 deletions src/librustc/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
}

Expand Down
4 changes: 2 additions & 2 deletions src/librustc/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_codegen_ssa/mir/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ impl<'a, 'tcx> TerminatorCodegenHelper<'tcx> {
// a loop.
fn maybe_sideeffect<Bx: BuilderMethods<'a, 'tcx>>(
&self,
mir: mir::ReadOnlyBodyCache<'tcx, 'tcx>,
mir: mir::ReadOnlyBodyAndCache<'tcx, 'tcx>,
bx: &mut Bx,
targets: &[mir::BasicBlock],
) {
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_codegen_ssa/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use self::operand::{OperandRef, OperandValue};
pub struct FunctionCx<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> {
instance: Instance<'tcx>,

mir: mir::ReadOnlyBodyCache<'tcx, 'tcx>,
mir: mir::ReadOnlyBodyAndCache<'tcx, 'tcx>,

debug_context: Option<FunctionDebugContext<Bx::DIScope>>,

Expand Down Expand Up @@ -159,7 +159,7 @@ pub fn codegen_mir<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
}).collect();

let (landing_pads, funclets) = create_funclets(&mir, &mut bx, &cleanup_kinds, &block_bxs);
let mir_body: &mir::Body<'_> = mir.body();
let mir_body: &mir::Body<'_> = *mir;
let mut fx = FunctionCx {
instance,
mir,
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_metadata/rmeta/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::svh::Svh;
use rustc::dep_graph::{self, DepNodeIndex};
use rustc::middle::lang_items;
use rustc::mir::{self, BodyCache, interpret, Promoted};
use rustc::mir::{self, BodyAndCache, interpret, Promoted};
use rustc::mir::interpret::{AllocDecodingSession, AllocDecodingState};
use rustc::session::Session;
use rustc::ty::{self, Ty, TyCtxt};
Expand Down Expand Up @@ -1079,7 +1079,7 @@ impl<'a, 'tcx> CrateMetadata {
self.root.per_def.mir.get(self, id).is_some()
}

fn get_optimized_mir(&self, tcx: TyCtxt<'tcx>, id: DefIndex) -> BodyCache<'tcx> {
fn get_optimized_mir(&self, tcx: TyCtxt<'tcx>, id: DefIndex) -> BodyAndCache<'tcx> {
let mut cache = self.root.per_def.mir.get(self, id)
.filter(|_| !self.is_proc_macro(id))
.unwrap_or_else(|| {
Expand All @@ -1094,7 +1094,7 @@ impl<'a, 'tcx> CrateMetadata {
&self,
tcx: TyCtxt<'tcx>,
id: DefIndex,
) -> IndexVec<Promoted, BodyCache<'tcx>> {
) -> IndexVec<Promoted, BodyAndCache<'tcx>> {
let mut cache = self.root.per_def.promoted_mir.get(self, id)
.filter(|_| !self.is_proc_macro(id))
.unwrap_or_else(|| {
Expand Down
Loading

0 comments on commit 56c0bea

Please sign in to comment.