Skip to content

Commit d53f39e

Browse files
committed
Auto merge of #38217 - oli-obk:mir-for-all-and-all-for-mir, r=eddyb
add a -Z flag to guarantee that MIR is generated for all functions r? @eddyb cc @solson
2 parents ebf2e7d + d74d153 commit d53f39e

File tree

6 files changed

+53
-12
lines changed

6 files changed

+53
-12
lines changed

src/librustc/middle/cstore.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,11 @@ pub trait CrateStore<'tcx> {
355355
fn get_item_mir<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId) -> Mir<'tcx>;
356356
fn is_item_mir_available(&self, def: DefId) -> bool;
357357

358+
/// Take a look if we need to inline or monomorphize this. If so, we
359+
/// will emit code for this item in the local crate, and thus
360+
/// create a translation item for it.
361+
fn can_have_local_instance<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId) -> bool;
362+
358363
// This is basically a 1-based range of ints, which is a little
359364
// silly - I may fix that.
360365
fn crates(&self) -> Vec<CrateNum>;
@@ -528,6 +533,9 @@ impl<'tcx> CrateStore<'tcx> for DummyCrateStore {
528533
fn is_item_mir_available(&self, def: DefId) -> bool {
529534
bug!("is_item_mir_available")
530535
}
536+
fn can_have_local_instance<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId) -> bool {
537+
bug!("can_have_local_instance")
538+
}
531539

532540
// This is basically a 1-based range of ints, which is a little
533541
// silly - I may fix that.

src/librustc/session/config.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -928,6 +928,8 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
928928
"print some statistics about AST and HIR"),
929929
mir_stats: bool = (false, parse_bool, [UNTRACKED],
930930
"print some statistics about MIR"),
931+
always_encode_mir: bool = (false, parse_bool, [TRACKED],
932+
"encode MIR of all functions into the crate metadata"),
931933
}
932934

933935
pub fn default_lib_output() -> CrateType {

src/librustc_metadata/cstore_impl.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,11 @@ impl<'tcx> CrateStore<'tcx> for cstore::CStore {
527527
self.get_crate_data(def.krate).is_item_mir_available(def.index)
528528
}
529529

530+
fn can_have_local_instance<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId) -> bool {
531+
self.dep_graph.read(DepNode::MetaData(def));
532+
def.is_local() || self.get_crate_data(def.krate).can_have_local_instance(tcx, def.index)
533+
}
534+
530535
fn crates(&self) -> Vec<CrateNum>
531536
{
532537
let mut result = vec![];

src/librustc_metadata/decoder.rs

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,14 @@ impl<'tcx> EntryKind<'tcx> {
504504
EntryKind::Closure(_) => return None,
505505
})
506506
}
507+
fn is_const_fn(&self, meta: &CrateMetadata) -> bool {
508+
let constness = match *self {
509+
EntryKind::Method(data) => data.decode(meta).fn_data.constness,
510+
EntryKind::Fn(data) => data.decode(meta).constness,
511+
_ => hir::Constness::NotConst,
512+
};
513+
constness == hir::Constness::Const
514+
}
507515
}
508516

509517
impl<'a, 'tcx> CrateMetadata {
@@ -839,6 +847,29 @@ impl<'a, 'tcx> CrateMetadata {
839847
self.maybe_entry(id).and_then(|item| item.decode(self).mir).is_some()
840848
}
841849

850+
pub fn can_have_local_instance(&self,
851+
tcx: TyCtxt<'a, 'tcx, 'tcx>,
852+
id: DefIndex) -> bool {
853+
self.maybe_entry(id).map_or(false, |item| {
854+
let item = item.decode(self);
855+
// if we don't have a MIR, then this item was never meant to be locally instantiated
856+
// or we have a bug in the metadata serialization
857+
item.mir.is_some() && (
858+
// items with generics always can have local instances if monomorphized
859+
item.generics.map_or(false, |generics| {
860+
let generics = generics.decode((self, tcx));
861+
generics.parent_types != 0 || !generics.types.is_empty()
862+
}) ||
863+
match item.kind {
864+
EntryKind::Closure(_) => true,
865+
_ => false,
866+
} ||
867+
item.kind.is_const_fn(self) ||
868+
attr::requests_inline(&self.get_attributes(&item))
869+
)
870+
})
871+
}
872+
842873
pub fn maybe_get_item_mir(&self,
843874
tcx: TyCtxt<'a, 'tcx, 'tcx>,
844875
id: DefIndex)
@@ -1051,12 +1082,7 @@ impl<'a, 'tcx> CrateMetadata {
10511082
}
10521083

10531084
pub fn is_const_fn(&self, id: DefIndex) -> bool {
1054-
let constness = match self.entry(id).kind {
1055-
EntryKind::Method(data) => data.decode(self).fn_data.constness,
1056-
EntryKind::Fn(data) => data.decode(self).constness,
1057-
_ => hir::Constness::NotConst,
1058-
};
1059-
constness == hir::Constness::Const
1085+
self.entry(id).kind.is_const_fn(self)
10601086
}
10611087

10621088
pub fn is_foreign_item(&self, id: DefIndex) -> bool {

src/librustc_metadata/encoder.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
577577
let types = generics.parent_types as usize + generics.types.len();
578578
let needs_inline = types > 0 || attr::requests_inline(&ast_item.attrs);
579579
let is_const_fn = sig.constness == hir::Constness::Const;
580-
(is_const_fn, needs_inline || is_const_fn)
580+
let always_encode_mir = self.tcx.sess.opts.debugging_opts.always_encode_mir;
581+
(is_const_fn, needs_inline || is_const_fn || always_encode_mir)
581582
} else {
582583
(false, false)
583584
};
@@ -838,11 +839,13 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
838839
_ => None,
839840
},
840841
mir: match item.node {
842+
hir::ItemStatic(..) |
841843
hir::ItemConst(..) => self.encode_mir(def_id),
842844
hir::ItemFn(_, _, constness, _, ref generics, _) => {
843845
let tps_len = generics.ty_params.len();
844846
let needs_inline = tps_len > 0 || attr::requests_inline(&item.attrs);
845-
if needs_inline || constness == hir::Constness::Const {
847+
let always_encode_mir = self.tcx.sess.opts.debugging_opts.always_encode_mir;
848+
if needs_inline || constness == hir::Constness::Const || always_encode_mir {
846849
self.encode_mir(def_id)
847850
} else {
848851
None

src/librustc_trans/collector.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -706,10 +706,7 @@ impl<'a, 'tcx> MirVisitor<'tcx> for MirNeighborCollector<'a, 'tcx> {
706706
fn can_have_local_instance<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
707707
def_id: DefId)
708708
-> bool {
709-
// Take a look if we have the definition available. If not, we
710-
// will not emit code for this item in the local crate, and thus
711-
// don't create a translation item for it.
712-
def_id.is_local() || tcx.sess.cstore.is_item_mir_available(def_id)
709+
tcx.sess.cstore.can_have_local_instance(tcx, def_id)
713710
}
714711

715712
fn find_drop_glue_neighbors<'a, 'tcx>(scx: &SharedCrateContext<'a, 'tcx>,

0 commit comments

Comments
 (0)