Skip to content

Commit 7381465

Browse files
committed
Move promoted out of mir::Body
1 parent 666180c commit 7381465

File tree

21 files changed

+253
-126
lines changed

21 files changed

+253
-126
lines changed

src/librustc/arena.rs

Lines changed: 10 additions & 0 deletions
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

Lines changed: 0 additions & 9 deletions
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,
@@ -3032,7 +3024,6 @@ BraceStructTypeFoldableImpl! {
30323024
basic_blocks,
30333025
source_scopes,
30343026
source_scope_local_data,
3035-
promoted,
30363027
yield_ty,
30373028
generator_drop,
30383029
generator_layout,

src/librustc/query/mod.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ rustc_queries! {
110110
no_hash
111111
}
112112

113-
query mir_validated(_: DefId) -> &'tcx Steal<mir::Body<'tcx>> {
113+
query mir_validated(_: DefId) -> (&'tcx Steal<mir::Body<'tcx>>, &'tcx Steal<IndexVec<mir::Promoted, mir::Body<'tcx>>>) {
114114
no_hash
115115
}
116116

@@ -125,7 +125,17 @@ rustc_queries! {
125125
}
126126
}
127127

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

131141
TypeChecking {

src/librustc/ty/context.rs

Lines changed: 11 additions & 1 deletion
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_metadata/cstore_impl.rs

Lines changed: 9 additions & 0 deletions
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
}

src/librustc_metadata/decoder.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use crate::cstore::{self, CrateMetadata, MetadataBlob, NativeLibrary, ForeignModule, FullProcMacro};
44
use crate::schema::*;
55

6+
use rustc_data_structures::indexed_vec::IndexVec;
67
use rustc_data_structures::sync::{Lrc, ReadGuard};
78
use rustc::hir::map::{DefKey, DefPath, DefPathData, DefPathHash};
89
use rustc::hir;
@@ -17,7 +18,7 @@ use rustc::mir::interpret::AllocDecodingSession;
1718
use rustc::session::Session;
1819
use rustc::ty::{self, Ty, TyCtxt};
1920
use rustc::ty::codec::TyDecoder;
20-
use rustc::mir::Body;
21+
use rustc::mir::{Body, Promoted};
2122
use rustc::util::captures::Captures;
2223

2324
use std::io;
@@ -923,6 +924,13 @@ impl<'a, 'tcx> CrateMetadata {
923924
}
924925
}
925926

927+
pub fn maybe_get_promoted_mir(&self, tcx: TyCtxt<'tcx>, id: DefIndex) -> Option<IndexVec<Promoted, Body<'tcx>>> {
928+
match self.is_proc_macro(id) {
929+
true => None,
930+
false => self.entry(id).promoted_mir.map(|promoted| promoted.decode((self, tcx)),)
931+
}
932+
}
933+
926934
pub fn mir_const_qualif(&self, id: DefIndex) -> u8 {
927935
match self.entry(id).kind {
928936
EntryKind::Const(qualif, _) |

src/librustc_metadata/encoder.rs

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use rustc::hir::def_id::{CrateNum, CRATE_DEF_INDEX, DefIndex, DefId, LocalDefId,
88
use rustc::hir::GenericParamKind;
99
use rustc::hir::map::definitions::DefPathTable;
1010
use rustc_data_structures::fingerprint::Fingerprint;
11+
use rustc_data_structures::indexed_vec::IndexVec;
1112
use rustc::middle::dependency_format::Linkage;
1213
use rustc::middle::exported_symbols::{ExportedSymbol, SymbolExportLevel,
1314
metadata_symbol_name};
@@ -623,6 +624,7 @@ impl EncodeContext<'tcx> {
623624
predicates_defined_on: None,
624625

625626
mir: self.encode_optimized_mir(def_id),
627+
promoted_mir: self.encode_promoted_mir(def_id),
626628
}
627629
}
628630

@@ -677,6 +679,7 @@ impl EncodeContext<'tcx> {
677679
predicates_defined_on: None,
678680

679681
mir: self.encode_optimized_mir(def_id),
682+
promoted_mir: self.encode_promoted_mir(def_id),
680683
}
681684
}
682685

@@ -713,7 +716,8 @@ impl EncodeContext<'tcx> {
713716
predicates: None,
714717
predicates_defined_on: None,
715718

716-
mir: None
719+
mir: None,
720+
promoted_mir: None,
717721
}
718722
}
719723

@@ -748,6 +752,7 @@ impl EncodeContext<'tcx> {
748752
predicates_defined_on: None,
749753

750754
mir: None,
755+
promoted_mir: None,
751756
}
752757
}
753758

@@ -808,6 +813,7 @@ impl EncodeContext<'tcx> {
808813
predicates_defined_on: None,
809814

810815
mir: self.encode_optimized_mir(def_id),
816+
promoted_mir: self.encode_promoted_mir(def_id),
811817
}
812818
}
813819

@@ -923,6 +929,7 @@ impl EncodeContext<'tcx> {
923929
predicates_defined_on: None,
924930

925931
mir: self.encode_optimized_mir(def_id),
932+
promoted_mir: self.encode_promoted_mir(def_id),
926933
}
927934
}
928935

@@ -1022,6 +1029,7 @@ impl EncodeContext<'tcx> {
10221029
predicates_defined_on: None,
10231030

10241031
mir: if mir { self.encode_optimized_mir(def_id) } else { None },
1032+
promoted_mir: if mir { self.encode_promoted_mir(def_id) } else { None },
10251033
}
10261034
}
10271035

@@ -1052,6 +1060,16 @@ impl EncodeContext<'tcx> {
10521060
}
10531061
}
10541062

1063+
fn encode_promoted_mir(&mut self, def_id: DefId) -> Option<Lazy<IndexVec<mir::Promoted, mir::Body<'tcx>>>> {
1064+
debug!("EncodeContext::encode_promoted_mir({:?})", def_id);
1065+
if self.tcx.mir_keys(LOCAL_CRATE).contains(&def_id) {
1066+
let promoted = self.tcx.promoted_mir(def_id);
1067+
Some(self.lazy(promoted))
1068+
} else {
1069+
None
1070+
}
1071+
}
1072+
10551073
// Encodes the inherent implementations of a structure, enumeration, or trait.
10561074
fn encode_inherent_implementations(&mut self, def_id: DefId) -> Lazy<[DefIndex]> {
10571075
debug!("EncodeContext::encode_inherent_implementations({:?})", def_id);
@@ -1202,6 +1220,20 @@ impl EncodeContext<'tcx> {
12021220
hir::ItemKind::Use(..) => bug!("cannot encode info for item {:?}", item),
12031221
};
12041222

1223+
let mir = match item.node {
1224+
hir::ItemKind::Static(..) | hir::ItemKind::Const(..) => true,
1225+
hir::ItemKind::Fn(_, header, ..) => {
1226+
let generics = tcx.generics_of(def_id);
1227+
let needs_inline =
1228+
(generics.requires_monomorphization(tcx) ||
1229+
tcx.codegen_fn_attrs(def_id).requests_inline()) &&
1230+
!self.metadata_output_only();
1231+
let always_encode_mir = self.tcx.sess.opts.debugging_opts.always_encode_mir;
1232+
needs_inline || header.constness == hir::Constness::Const || always_encode_mir
1233+
}
1234+
_ => false,
1235+
};
1236+
12051237
Entry {
12061238
kind,
12071239
visibility: self.lazy(ty::Visibility::from_hir(&item.vis, item.hir_id, tcx)),
@@ -1301,29 +1333,8 @@ impl EncodeContext<'tcx> {
13011333
_ => None, // not *wrong* for other kinds of items, but not needed
13021334
},
13031335

1304-
mir: match item.node {
1305-
hir::ItemKind::Static(..) => {
1306-
self.encode_optimized_mir(def_id)
1307-
}
1308-
hir::ItemKind::Const(..) => self.encode_optimized_mir(def_id),
1309-
hir::ItemKind::Fn(_, header, ..) => {
1310-
let generics = tcx.generics_of(def_id);
1311-
let needs_inline =
1312-
(generics.requires_monomorphization(tcx) ||
1313-
tcx.codegen_fn_attrs(def_id).requests_inline()) &&
1314-
!self.metadata_output_only();
1315-
let always_encode_mir = self.tcx.sess.opts.debugging_opts.always_encode_mir;
1316-
if needs_inline
1317-
|| header.constness == hir::Constness::Const
1318-
|| always_encode_mir
1319-
{
1320-
self.encode_optimized_mir(def_id)
1321-
} else {
1322-
None
1323-
}
1324-
}
1325-
_ => None,
1326-
},
1336+
mir: if mir { self.encode_optimized_mir(def_id) } else { None },
1337+
promoted_mir: if mir { self.encode_promoted_mir(def_id) } else { None },
13271338
}
13281339
}
13291340

@@ -1350,6 +1361,7 @@ impl EncodeContext<'tcx> {
13501361
predicates: None,
13511362
predicates_defined_on: None,
13521363
mir: None,
1364+
promoted_mir: None,
13531365
}
13541366
}
13551367

@@ -1376,6 +1388,7 @@ impl EncodeContext<'tcx> {
13761388
predicates_defined_on: None,
13771389

13781390
mir: None,
1391+
promoted_mir: None,
13791392
}
13801393
}
13811394

@@ -1436,6 +1449,7 @@ impl EncodeContext<'tcx> {
14361449
predicates_defined_on: None,
14371450

14381451
mir: self.encode_optimized_mir(def_id),
1452+
promoted_mir: self.encode_promoted_mir(def_id),
14391453
}
14401454
}
14411455

@@ -1464,6 +1478,7 @@ impl EncodeContext<'tcx> {
14641478
predicates_defined_on: None,
14651479

14661480
mir: self.encode_optimized_mir(def_id),
1481+
promoted_mir: self.encode_promoted_mir(def_id),
14671482
}
14681483
}
14691484

@@ -1675,6 +1690,7 @@ impl EncodeContext<'tcx> {
16751690
predicates_defined_on: None,
16761691

16771692
mir: None,
1693+
promoted_mir: None,
16781694
}
16791695
}
16801696
}

src/librustc_metadata/schema.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use rustc::session::CrateDisambiguator;
1111
use rustc::session::config::SymbolManglingVersion;
1212
use rustc::ty::{self, Ty, ReprOptions};
1313
use rustc_target::spec::{PanicStrategy, TargetTriple};
14+
use rustc_data_structures::indexed_vec::IndexVec;
1415
use rustc_data_structures::svh::Svh;
1516

1617
use syntax::{ast, attr};
@@ -231,6 +232,7 @@ pub struct Entry<'tcx> {
231232
pub predicates_defined_on: Option<Lazy<ty::GenericPredicates<'tcx>>>,
232233

233234
pub mir: Option<Lazy<mir::Body<'tcx>>>,
235+
pub promoted_mir: Option<Lazy<IndexVec<mir::Promoted, mir::Body<'tcx>>>>,
234236
}
235237

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

0 commit comments

Comments
 (0)