Skip to content

Commit 555d7a2

Browse files
committed
Auto merge of #63580 - wesleywiser:move_promoted_out, r=oli-obk
Move promoted MIR out of `mir::Body` r? @oli-obk
2 parents e2b4165 + d6bf776 commit 555d7a2

Some content is hidden

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

58 files changed

+519
-369
lines changed

src/librustc/arena.rs

+10
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ macro_rules! arena_types {
2525
[] adt_def: rustc::ty::AdtDef,
2626
[] steal_mir: rustc::ty::steal::Steal<rustc::mir::Body<$tcx>>,
2727
[] mir: rustc::mir::Body<$tcx>,
28+
[] steal_promoted: rustc::ty::steal::Steal<
29+
rustc_data_structures::indexed_vec::IndexVec<
30+
rustc::mir::Promoted,
31+
rustc::mir::Body<$tcx>
32+
>
33+
>,
34+
[] promoted: rustc_data_structures::indexed_vec::IndexVec<
35+
rustc::mir::Promoted,
36+
rustc::mir::Body<$tcx>
37+
>,
2838
[] tables: rustc::ty::TypeckTables<$tcx>,
2939
[] const_allocs: rustc::mir::interpret::Allocation,
3040
[] vtable_method: Option<(

src/librustc/mir/mod.rs

+65-20
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,6 @@ pub struct Body<'tcx> {
108108
/// needn't) be tracked across crates.
109109
pub source_scope_local_data: ClearCrossCrate<IndexVec<SourceScope, SourceScopeLocalData>>,
110110

111-
/// Rvalues promoted from this function, such as borrows of constants.
112-
/// Each of them is the Body of a constant with the fn's type parameters
113-
/// in scope, but a separate set of locals.
114-
pub promoted: IndexVec<Promoted, Body<'tcx>>,
115-
116111
/// Yields type of the function, if it is a generator.
117112
pub yield_ty: Option<Ty<'tcx>>,
118113

@@ -174,7 +169,6 @@ impl<'tcx> Body<'tcx> {
174169
basic_blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>,
175170
source_scopes: IndexVec<SourceScope, SourceScopeData>,
176171
source_scope_local_data: ClearCrossCrate<IndexVec<SourceScope, SourceScopeLocalData>>,
177-
promoted: IndexVec<Promoted, Body<'tcx>>,
178172
yield_ty: Option<Ty<'tcx>>,
179173
local_decls: LocalDecls<'tcx>,
180174
user_type_annotations: CanonicalUserTypeAnnotations<'tcx>,
@@ -196,7 +190,6 @@ impl<'tcx> Body<'tcx> {
196190
basic_blocks,
197191
source_scopes,
198192
source_scope_local_data,
199-
promoted,
200193
yield_ty,
201194
generator_drop: None,
202195
generator_layout: None,
@@ -418,7 +411,6 @@ impl_stable_hash_for!(struct Body<'tcx> {
418411
basic_blocks,
419412
source_scopes,
420413
source_scope_local_data,
421-
promoted,
422414
yield_ty,
423415
generator_drop,
424416
generator_layout,
@@ -1737,23 +1729,25 @@ pub enum PlaceBase<'tcx> {
17371729
}
17381730

17391731
/// We store the normalized type to avoid requiring normalization when reading MIR
1740-
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, RustcEncodable, RustcDecodable)]
1732+
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, RustcEncodable, RustcDecodable)]
17411733
pub struct Static<'tcx> {
17421734
pub ty: Ty<'tcx>,
1743-
pub kind: StaticKind,
1735+
pub kind: StaticKind<'tcx>,
1736+
pub def_id: DefId,
17441737
}
17451738

17461739
#[derive(
1747-
Clone, PartialEq, Eq, PartialOrd, Ord, Hash, HashStable, RustcEncodable, RustcDecodable,
1740+
Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, HashStable, RustcEncodable, RustcDecodable,
17481741
)]
1749-
pub enum StaticKind {
1750-
Promoted(Promoted),
1751-
Static(DefId),
1742+
pub enum StaticKind<'tcx> {
1743+
Promoted(Promoted, SubstsRef<'tcx>),
1744+
Static,
17521745
}
17531746

17541747
impl_stable_hash_for!(struct Static<'tcx> {
17551748
ty,
1756-
kind
1749+
kind,
1750+
def_id
17571751
});
17581752

17591753
/// The `Projection` data structure defines things of the form `base.x`, `*b` or `b[index]`.
@@ -2114,10 +2108,12 @@ impl Debug for PlaceBase<'_> {
21142108
fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
21152109
match *self {
21162110
PlaceBase::Local(id) => write!(fmt, "{:?}", id),
2117-
PlaceBase::Static(box self::Static { ty, kind: StaticKind::Static(def_id) }) => {
2111+
PlaceBase::Static(box self::Static { ty, kind: StaticKind::Static, def_id }) => {
21182112
write!(fmt, "({}: {:?})", ty::tls::with(|tcx| tcx.def_path_str(def_id)), ty)
21192113
}
2120-
PlaceBase::Static(box self::Static { ty, kind: StaticKind::Promoted(promoted) }) => {
2114+
PlaceBase::Static(box self::Static {
2115+
ty, kind: StaticKind::Promoted(promoted, _), def_id: _
2116+
}) => {
21212117
write!(fmt, "({:?}: {:?})", promoted, ty)
21222118
}
21232119
}
@@ -3032,7 +3028,6 @@ BraceStructTypeFoldableImpl! {
30323028
basic_blocks,
30333029
source_scopes,
30343030
source_scope_local_data,
3035-
promoted,
30363031
yield_ty,
30373032
generator_drop,
30383033
generator_layout,
@@ -3226,13 +3221,63 @@ impl<'tcx> TypeFoldable<'tcx> for Terminator<'tcx> {
32263221
impl<'tcx> TypeFoldable<'tcx> for Place<'tcx> {
32273222
fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
32283223
Place {
3229-
base: self.base.clone(),
3224+
base: self.base.fold_with(folder),
32303225
projection: self.projection.fold_with(folder),
32313226
}
32323227
}
32333228

32343229
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
3235-
self.projection.visit_with(visitor)
3230+
self.base.visit_with(visitor) || self.projection.visit_with(visitor)
3231+
}
3232+
}
3233+
3234+
impl<'tcx> TypeFoldable<'tcx> for PlaceBase<'tcx> {
3235+
fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
3236+
match self {
3237+
PlaceBase::Local(local) => PlaceBase::Local(local.fold_with(folder)),
3238+
PlaceBase::Static(static_) => PlaceBase::Static(static_.fold_with(folder)),
3239+
}
3240+
}
3241+
3242+
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
3243+
match self {
3244+
PlaceBase::Local(local) => local.visit_with(visitor),
3245+
PlaceBase::Static(static_) => (**static_).visit_with(visitor),
3246+
}
3247+
}
3248+
}
3249+
3250+
impl<'tcx> TypeFoldable<'tcx> for Static<'tcx> {
3251+
fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
3252+
Static {
3253+
ty: self.ty.fold_with(folder),
3254+
kind: self.kind.fold_with(folder),
3255+
def_id: self.def_id,
3256+
}
3257+
}
3258+
3259+
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
3260+
let Static { ty, kind, def_id: _ } = self;
3261+
3262+
ty.visit_with(visitor) || kind.visit_with(visitor)
3263+
}
3264+
}
3265+
3266+
impl<'tcx> TypeFoldable<'tcx> for StaticKind<'tcx> {
3267+
fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
3268+
match self {
3269+
StaticKind::Promoted(promoted, substs) =>
3270+
StaticKind::Promoted(promoted.fold_with(folder), substs.fold_with(folder)),
3271+
StaticKind::Static => StaticKind::Static
3272+
}
3273+
}
3274+
3275+
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
3276+
match self {
3277+
StaticKind::Promoted(promoted, substs) =>
3278+
promoted.visit_with(visitor) || substs.visit_with(visitor),
3279+
StaticKind::Static => { false }
3280+
}
32363281
}
32373282
}
32383283

src/librustc/mir/visit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,7 @@ macro_rules! make_mir_visitor {
708708
PlaceBase::Local(local) => {
709709
self.visit_local(local, context, location);
710710
}
711-
PlaceBase::Static(box Static { kind: _, ty }) => {
711+
PlaceBase::Static(box Static { kind: _, ty, def_id: _ }) => {
712712
self.visit_ty(& $($mutability)? *ty, TyContext::Location(location));
713713
}
714714
}

src/librustc/query/mod.rs

+16-2
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,11 @@ rustc_queries! {
110110
no_hash
111111
}
112112

113-
query mir_validated(_: DefId) -> &'tcx Steal<mir::Body<'tcx>> {
113+
query mir_validated(_: DefId) ->
114+
(
115+
&'tcx Steal<mir::Body<'tcx>>,
116+
&'tcx Steal<IndexVec<mir::Promoted, mir::Body<'tcx>>>
117+
) {
114118
no_hash
115119
}
116120

@@ -125,7 +129,17 @@ rustc_queries! {
125129
}
126130
}
127131

128-
query promoted_mir(key: DefId) -> &'tcx IndexVec<mir::Promoted, mir::Body<'tcx>> { }
132+
query promoted_mir(key: DefId) -> &'tcx IndexVec<mir::Promoted, mir::Body<'tcx>> {
133+
cache_on_disk_if { key.is_local() }
134+
load_cached(tcx, id) {
135+
let promoted: Option<
136+
rustc_data_structures::indexed_vec::IndexVec<
137+
crate::mir::Promoted,
138+
crate::mir::Body<'tcx>
139+
>> = tcx.queries.on_disk_cache.try_load_query_result(tcx, id);
140+
promoted.map(|p| &*tcx.arena.alloc(p))
141+
}
142+
}
129143
}
130144

131145
TypeChecking {

src/librustc/ty/context.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use crate::middle::cstore::EncodedMetadata;
2121
use crate::middle::lang_items;
2222
use crate::middle::resolve_lifetime::{self, ObjectLifetimeDefault};
2323
use crate::middle::stability;
24-
use crate::mir::{Body, interpret, ProjectionKind};
24+
use crate::mir::{Body, interpret, ProjectionKind, Promoted};
2525
use crate::mir::interpret::{ConstValue, Allocation, Scalar};
2626
use crate::ty::subst::{Kind, InternalSubsts, SubstsRef, Subst};
2727
use crate::ty::ReprOptions;
@@ -1096,6 +1096,16 @@ impl<'tcx> TyCtxt<'tcx> {
10961096
self.arena.alloc(Steal::new(mir))
10971097
}
10981098

1099+
pub fn alloc_steal_promoted(self, promoted: IndexVec<Promoted, Body<'tcx>>) ->
1100+
&'tcx Steal<IndexVec<Promoted, Body<'tcx>>> {
1101+
self.arena.alloc(Steal::new(promoted))
1102+
}
1103+
1104+
pub fn intern_promoted(self, promoted: IndexVec<Promoted, Body<'tcx>>) ->
1105+
&'tcx IndexVec<Promoted, Body<'tcx>> {
1106+
self.arena.alloc(promoted)
1107+
}
1108+
10991109
pub fn alloc_adt_def(
11001110
self,
11011111
did: DefId,

src/librustc_codegen_ssa/mir/block.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -609,17 +609,19 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
609609
mir::Operand::Copy(
610610
Place {
611611
base: PlaceBase::Static(box Static {
612-
kind: StaticKind::Promoted(promoted),
612+
kind: StaticKind::Promoted(promoted, _),
613613
ty,
614+
def_id: _,
614615
}),
615616
projection: None,
616617
}
617618
) |
618619
mir::Operand::Move(
619620
Place {
620621
base: PlaceBase::Static(box Static {
621-
kind: StaticKind::Promoted(promoted),
622+
kind: StaticKind::Promoted(promoted, _),
622623
ty,
624+
def_id: _,
623625
}),
624626
projection: None,
625627
}

src/librustc_codegen_ssa/mir/place.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rustc::ty::{self, Ty};
1+
use rustc::ty::{self, Instance, Ty};
22
use rustc::ty::layout::{self, Align, TyLayout, LayoutOf, VariantIdx, HasTyCtxt};
33
use rustc::mir;
44
use rustc::mir::tcx::PlaceTy;
@@ -454,13 +454,15 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
454454
mir::PlaceRef {
455455
base: mir::PlaceBase::Static(box mir::Static {
456456
ty,
457-
kind: mir::StaticKind::Promoted(promoted),
457+
kind: mir::StaticKind::Promoted(promoted, substs),
458+
def_id,
458459
}),
459460
projection: None,
460461
} => {
461462
let param_env = ty::ParamEnv::reveal_all();
463+
let instance = Instance::new(*def_id, self.monomorphize(substs));
462464
let cid = mir::interpret::GlobalId {
463-
instance: self.instance,
465+
instance: instance,
464466
promoted: Some(*promoted),
465467
};
466468
let layout = cx.layout_of(self.monomorphize(&ty));
@@ -487,7 +489,8 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
487489
mir::PlaceRef {
488490
base: mir::PlaceBase::Static(box mir::Static {
489491
ty,
490-
kind: mir::StaticKind::Static(def_id),
492+
kind: mir::StaticKind::Static,
493+
def_id,
491494
}),
492495
projection: None,
493496
} => {

src/librustc_incremental/persist/dirty_clean.rs

+21-6
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use rustc::hir::itemlikevisit::ItemLikeVisitor;
2424
use rustc::hir::intravisit;
2525
use rustc::ich::{ATTR_DIRTY, ATTR_CLEAN};
2626
use rustc::ty::TyCtxt;
27+
use rustc_data_structures::fingerprint::Fingerprint;
2728
use rustc_data_structures::fx::FxHashSet;
2829
use syntax::ast::{self, Attribute, NestedMetaItem};
2930
use syntax::symbol::{Symbol, sym};
@@ -71,6 +72,7 @@ const BASE_IMPL: &[&str] = &[
7172
/// code, i.e., functions+methods
7273
const BASE_MIR: &[&str] = &[
7374
label_strs::optimized_mir,
75+
label_strs::promoted_mir,
7476
label_strs::mir_built,
7577
];
7678

@@ -472,26 +474,39 @@ impl DirtyCleanVisitor<'tcx> {
472474
fn assert_dirty(&self, item_span: Span, dep_node: DepNode) {
473475
debug!("assert_dirty({:?})", dep_node);
474476

475-
let dep_node_index = self.tcx.dep_graph.dep_node_index_of(&dep_node);
476-
let current_fingerprint = self.tcx.dep_graph.fingerprint_of(dep_node_index);
477+
let current_fingerprint = self.get_fingerprint(&dep_node);
477478
let prev_fingerprint = self.tcx.dep_graph.prev_fingerprint_of(&dep_node);
478479

479-
if Some(current_fingerprint) == prev_fingerprint {
480+
if current_fingerprint == prev_fingerprint {
480481
let dep_node_str = self.dep_node_str(&dep_node);
481482
self.tcx.sess.span_err(
482483
item_span,
483484
&format!("`{}` should be dirty but is not", dep_node_str));
484485
}
485486
}
486487

488+
fn get_fingerprint(&self, dep_node: &DepNode) -> Option<Fingerprint> {
489+
if self.tcx.dep_graph.dep_node_exists(dep_node) {
490+
let dep_node_index = self.tcx.dep_graph.dep_node_index_of(dep_node);
491+
Some(self.tcx.dep_graph.fingerprint_of(dep_node_index))
492+
} else {
493+
None
494+
}
495+
}
496+
487497
fn assert_clean(&self, item_span: Span, dep_node: DepNode) {
488498
debug!("assert_clean({:?})", dep_node);
489499

490-
let dep_node_index = self.tcx.dep_graph.dep_node_index_of(&dep_node);
491-
let current_fingerprint = self.tcx.dep_graph.fingerprint_of(dep_node_index);
500+
let current_fingerprint = self.get_fingerprint(&dep_node);
492501
let prev_fingerprint = self.tcx.dep_graph.prev_fingerprint_of(&dep_node);
493502

494-
if Some(current_fingerprint) != prev_fingerprint {
503+
// if the node wasn't previously evaluated and now is (or vice versa),
504+
// then the node isn't actually clean or dirty.
505+
if (current_fingerprint == None) ^ (prev_fingerprint == None) {
506+
return;
507+
}
508+
509+
if current_fingerprint != prev_fingerprint {
495510
let dep_node_str = self.dep_node_str(&dep_node);
496511
self.tcx.sess.span_err(
497512
item_span,

src/librustc_metadata/cstore_impl.rs

+9
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,15 @@ provide! { <'tcx> tcx, def_id, other, cdata,
136136

137137
mir
138138
}
139+
promoted_mir => {
140+
let promoted = cdata.maybe_get_promoted_mir(tcx, def_id.index).unwrap_or_else(|| {
141+
bug!("get_promoted_mir: missing promoted MIR for `{:?}`", def_id)
142+
});
143+
144+
let promoted = tcx.arena.alloc(promoted);
145+
146+
promoted
147+
}
139148
mir_const_qualif => {
140149
(cdata.mir_const_qualif(def_id.index), tcx.arena.alloc(BitSet::new_empty(0)))
141150
}

0 commit comments

Comments
 (0)