Skip to content

Commit db9b05a

Browse files
committed
Auto merge of #71044 - ecstatic-morse:body-predecessor-cache, r=oli-obk
Remove `BodyAndCache` ...returning to the original approach using interior mutability within `Body`. This simplifies the API at the cost of some uncontended mutex locks when the parallel compiler is enabled. The current API requires you to either have a mutable reference to `Body` (`&mut BodyAndCache`), or to compute the predecessor graph ahead of time by creating a `ReadOnlyBodyAndCache`. This is not a good fit for, e.g., the dataflow framework, which 1. does not mutate the MIR 2. only sometimes needs the predecessor graph (for backward dataflow problems)
2 parents b2e36e6 + 4e7469e commit db9b05a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+454
-684
lines changed

src/librustc_codegen_ssa/mir/analyze.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ impl<Bx: BuilderMethods<'a, 'tcx>> LocalAnalyzer<'mir, 'a, 'tcx, Bx> {
120120
};
121121
if is_consume {
122122
let base_ty =
123-
mir::Place::ty_from(place_ref.local, proj_base, *self.fx.mir, cx.tcx());
123+
mir::Place::ty_from(place_ref.local, proj_base, self.fx.mir, cx.tcx());
124124
let base_ty = self.fx.monomorphize(&base_ty);
125125

126126
// ZSTs don't require any actual memory access.

src/librustc_codegen_ssa/mir/block.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ impl<'a, 'tcx> TerminatorCodegenHelper<'tcx> {
152152
// a loop.
153153
fn maybe_sideeffect<Bx: BuilderMethods<'a, 'tcx>>(
154154
&self,
155-
mir: mir::ReadOnlyBodyAndCache<'tcx, 'tcx>,
155+
mir: &'tcx mir::Body<'tcx>,
156156
bx: &mut Bx,
157157
targets: &[mir::BasicBlock],
158158
) {
@@ -306,7 +306,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
306306
target: mir::BasicBlock,
307307
unwind: Option<mir::BasicBlock>,
308308
) {
309-
let ty = location.ty(*self.mir, bx.tcx()).ty;
309+
let ty = location.ty(self.mir, bx.tcx()).ty;
310310
let ty = self.monomorphize(&ty);
311311
let drop_fn = Instance::resolve_drop_in_place(bx.tcx(), ty);
312312

@@ -572,7 +572,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
572572
let extra_args = extra_args
573573
.iter()
574574
.map(|op_arg| {
575-
let op_ty = op_arg.ty(*self.mir, bx.tcx());
575+
let op_ty = op_arg.ty(self.mir, bx.tcx());
576576
self.monomorphize(&op_ty)
577577
})
578578
.collect::<Vec<_>>();

src/librustc_codegen_ssa/mir/mod.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use self::operand::{OperandRef, OperandValue};
2121
pub struct FunctionCx<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> {
2222
instance: Instance<'tcx>,
2323

24-
mir: mir::ReadOnlyBodyAndCache<'tcx, 'tcx>,
24+
mir: &'tcx mir::Body<'tcx>,
2525

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

@@ -169,7 +169,6 @@ pub fn codegen_mir<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
169169
.collect();
170170

171171
let (landing_pads, funclets) = create_funclets(&mir, &mut bx, &cleanup_kinds, &block_bxs);
172-
let mir_body: &mir::Body<'_> = *mir;
173172
let mut fx = FunctionCx {
174173
instance,
175174
mir,
@@ -197,7 +196,7 @@ pub fn codegen_mir<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
197196
let args = arg_local_refs(&mut bx, &mut fx, &memory_locals);
198197

199198
let mut allocate_local = |local| {
200-
let decl = &mir_body.local_decls[local];
199+
let decl = &mir.local_decls[local];
201200
let layout = bx.layout_of(fx.monomorphize(&decl.ty));
202201
assert!(!layout.ty.has_erasable_regions());
203202

@@ -223,7 +222,7 @@ pub fn codegen_mir<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
223222
let retptr = allocate_local(mir::RETURN_PLACE);
224223
iter::once(retptr)
225224
.chain(args.into_iter())
226-
.chain(mir_body.vars_and_temps_iter().map(allocate_local))
225+
.chain(mir.vars_and_temps_iter().map(allocate_local))
227226
.collect()
228227
};
229228

@@ -235,8 +234,8 @@ pub fn codegen_mir<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
235234
bx.br(fx.blocks[mir::START_BLOCK]);
236235
}
237236

238-
let rpo = traversal::reverse_postorder(&mir_body);
239-
let mut visited = BitSet::new_empty(mir_body.basic_blocks().len());
237+
let rpo = traversal::reverse_postorder(&mir);
238+
let mut visited = BitSet::new_empty(mir.basic_blocks().len());
240239

241240
// Codegen the body of each block using reverse postorder
242241
for (bb, _) in rpo {
@@ -246,7 +245,7 @@ pub fn codegen_mir<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
246245

247246
// Remove blocks that haven't been visited, or have no
248247
// predecessors.
249-
for bb in mir_body.basic_blocks().indices() {
248+
for bb in mir.basic_blocks().indices() {
250249
// Unreachable block
251250
if !visited.contains(bb.index()) {
252251
debug!("codegen_mir: block {:?} was not visited", bb);

src/librustc_codegen_ssa/mir/place.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
497497

498498
pub fn monomorphized_place_ty(&self, place_ref: mir::PlaceRef<'tcx>) -> Ty<'tcx> {
499499
let tcx = self.cx.tcx();
500-
let place_ty = mir::Place::ty_from(place_ref.local, place_ref.projection, *self.mir, tcx);
500+
let place_ty = mir::Place::ty_from(place_ref.local, place_ref.projection, self.mir, tcx);
501501
self.monomorphize(&place_ty.ty)
502502
}
503503
}

src/librustc_codegen_ssa/mir/rvalue.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
473473
}
474474

475475
mir::Rvalue::Discriminant(ref place) => {
476-
let discr_ty = rvalue.ty(*self.mir, bx.tcx());
476+
let discr_ty = rvalue.ty(self.mir, bx.tcx());
477477
let discr = self
478478
.codegen_place(&mut bx, place.as_ref())
479479
.codegen_get_discr(&mut bx, discr_ty);
@@ -529,7 +529,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
529529
mir::Rvalue::Repeat(..) | mir::Rvalue::Aggregate(..) => {
530530
// According to `rvalue_creates_operand`, only ZST
531531
// aggregate rvalues are allowed to be operands.
532-
let ty = rvalue.ty(*self.mir, self.cx.tcx());
532+
let ty = rvalue.ty(self.mir, self.cx.tcx());
533533
let operand =
534534
OperandRef::new_zst(&mut bx, self.cx.layout_of(self.monomorphize(&ty)));
535535
(bx, operand)
@@ -749,7 +749,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
749749
true,
750750
mir::Rvalue::Repeat(..) |
751751
mir::Rvalue::Aggregate(..) => {
752-
let ty = rvalue.ty(*self.mir, self.cx.tcx());
752+
let ty = rvalue.ty(self.mir, self.cx.tcx());
753753
let ty = self.monomorphize(&ty);
754754
self.cx.spanned_layout_of(ty, span).is_zst()
755755
}

src/librustc_metadata/rmeta/decoder.rs

+7-19
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use rustc_middle::middle::cstore::{CrateSource, ExternCrate};
2626
use rustc_middle::middle::cstore::{ForeignModule, LinkagePreference, NativeLibrary};
2727
use rustc_middle::middle::exported_symbols::{ExportedSymbol, SymbolExportLevel};
2828
use rustc_middle::mir::interpret::{AllocDecodingSession, AllocDecodingState};
29-
use rustc_middle::mir::{self, interpret, BodyAndCache, Promoted};
29+
use rustc_middle::mir::{self, interpret, Body, Promoted};
3030
use rustc_middle::ty::codec::TyDecoder;
3131
use rustc_middle::ty::{self, Ty, TyCtxt};
3232
use rustc_middle::util::common::record_time;
@@ -1099,40 +1099,28 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
10991099
!self.is_proc_macro(id) && self.root.tables.mir.get(self, id).is_some()
11001100
}
11011101

1102-
fn get_optimized_mir(&self, tcx: TyCtxt<'tcx>, id: DefIndex) -> BodyAndCache<'tcx> {
1103-
let mut cache = self
1104-
.root
1102+
fn get_optimized_mir(&self, tcx: TyCtxt<'tcx>, id: DefIndex) -> Body<'tcx> {
1103+
self.root
11051104
.tables
11061105
.mir
11071106
.get(self, id)
11081107
.filter(|_| !self.is_proc_macro(id))
11091108
.unwrap_or_else(|| {
11101109
bug!("get_optimized_mir: missing MIR for `{:?}`", self.local_def_id(id))
11111110
})
1112-
.decode((self, tcx));
1113-
cache.ensure_predecessors();
1114-
cache
1111+
.decode((self, tcx))
11151112
}
11161113

1117-
fn get_promoted_mir(
1118-
&self,
1119-
tcx: TyCtxt<'tcx>,
1120-
id: DefIndex,
1121-
) -> IndexVec<Promoted, BodyAndCache<'tcx>> {
1122-
let mut cache = self
1123-
.root
1114+
fn get_promoted_mir(&self, tcx: TyCtxt<'tcx>, id: DefIndex) -> IndexVec<Promoted, Body<'tcx>> {
1115+
self.root
11241116
.tables
11251117
.promoted_mir
11261118
.get(self, id)
11271119
.filter(|_| !self.is_proc_macro(id))
11281120
.unwrap_or_else(|| {
11291121
bug!("get_promoted_mir: missing MIR for `{:?}`", self.local_def_id(id))
11301122
})
1131-
.decode((self, tcx));
1132-
for body in cache.iter_mut() {
1133-
body.ensure_predecessors();
1134-
}
1135-
cache
1123+
.decode((self, tcx))
11361124
}
11371125

11381126
fn mir_const_qualif(&self, id: DefIndex) -> mir::ConstQualifs {

src/librustc_metadata/rmeta/encoder.rs

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ macro_rules! encoder_methods {
6868
impl<'tcx> Encoder for EncodeContext<'tcx> {
6969
type Error = <opaque::Encoder as Encoder>::Error;
7070

71+
#[inline]
7172
fn emit_unit(&mut self) -> Result<(), Self::Error> {
7273
Ok(())
7374
}

src/librustc_metadata/rmeta/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,8 @@ define_tables! {
275275
// Also, as an optimization, a missing entry indicates an empty `&[]`.
276276
inferred_outlives: Table<DefIndex, Lazy!(&'tcx [(ty::Predicate<'tcx>, Span)])>,
277277
super_predicates: Table<DefIndex, Lazy!(ty::GenericPredicates<'tcx>)>,
278-
mir: Table<DefIndex, Lazy!(mir::BodyAndCache<'tcx>)>,
279-
promoted_mir: Table<DefIndex, Lazy!(IndexVec<mir::Promoted, mir::BodyAndCache<'tcx>>)>,
278+
mir: Table<DefIndex, Lazy!(mir::Body<'tcx>)>,
279+
promoted_mir: Table<DefIndex, Lazy!(IndexVec<mir::Promoted, mir::Body<'tcx>>)>,
280280
}
281281

282282
#[derive(Copy, Clone, RustcEncodable, RustcDecodable)]

src/librustc_middle/arena.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@ macro_rules! arena_types {
1515
[] generics: rustc_middle::ty::Generics,
1616
[] trait_def: rustc_middle::ty::TraitDef,
1717
[] adt_def: rustc_middle::ty::AdtDef,
18-
[] steal_mir: rustc_middle::ty::steal::Steal<rustc_middle::mir::BodyAndCache<$tcx>>,
19-
[] mir: rustc_middle::mir::BodyAndCache<$tcx>,
18+
[] steal_mir: rustc_middle::ty::steal::Steal<rustc_middle::mir::Body<$tcx>>,
19+
[] mir: rustc_middle::mir::Body<$tcx>,
2020
[] steal_promoted: rustc_middle::ty::steal::Steal<
2121
rustc_index::vec::IndexVec<
2222
rustc_middle::mir::Promoted,
23-
rustc_middle::mir::BodyAndCache<$tcx>
23+
rustc_middle::mir::Body<$tcx>
2424
>
2525
>,
2626
[] promoted: rustc_index::vec::IndexVec<
2727
rustc_middle::mir::Promoted,
28-
rustc_middle::mir::BodyAndCache<$tcx>
28+
rustc_middle::mir::Body<$tcx>
2929
>,
3030
[decode] tables: rustc_middle::ty::TypeckTables<$tcx>,
3131
[decode] borrowck_result: rustc_middle::mir::BorrowCheckResult<$tcx>,

0 commit comments

Comments
 (0)