Skip to content

add a -Z flag to guarantee that MIR is generated for all functions #38217

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/librustc/middle/cstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,11 @@ pub trait CrateStore<'tcx> {
fn get_item_mir<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId) -> Mir<'tcx>;
fn is_item_mir_available(&self, def: DefId) -> bool;

/// Take a look if we need to inline or monomorphize this. If so, we
/// will emit code for this item in the local crate, and thus
/// create a translation item for it.
fn can_have_local_instance<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId) -> bool;

// This is basically a 1-based range of ints, which is a little
// silly - I may fix that.
fn crates(&self) -> Vec<CrateNum>;
Expand Down Expand Up @@ -528,6 +533,9 @@ impl<'tcx> CrateStore<'tcx> for DummyCrateStore {
fn is_item_mir_available(&self, def: DefId) -> bool {
bug!("is_item_mir_available")
}
fn can_have_local_instance<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId) -> bool {
bug!("can_have_local_instance")
}

// This is basically a 1-based range of ints, which is a little
// silly - I may fix that.
Expand Down
2 changes: 2 additions & 0 deletions src/librustc/session/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -928,6 +928,8 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
"print some statistics about AST and HIR"),
mir_stats: bool = (false, parse_bool, [UNTRACKED],
"print some statistics about MIR"),
always_encode_mir: bool = (false, parse_bool, [TRACKED],
"encode MIR of all functions into the crate metadata"),
}

pub fn default_lib_output() -> CrateType {
Expand Down
5 changes: 5 additions & 0 deletions src/librustc_metadata/cstore_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,11 @@ impl<'tcx> CrateStore<'tcx> for cstore::CStore {
self.get_crate_data(def.krate).is_item_mir_available(def.index)
}

fn can_have_local_instance<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId) -> bool {
self.dep_graph.read(DepNode::MetaData(def));
def.is_local() || self.get_crate_data(def.krate).can_have_local_instance(tcx, def.index)
}

fn crates(&self) -> Vec<CrateNum>
{
let mut result = vec![];
Expand Down
38 changes: 32 additions & 6 deletions src/librustc_metadata/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,14 @@ impl<'tcx> EntryKind<'tcx> {
EntryKind::Closure(_) => return None,
})
}
fn is_const_fn(&self, meta: &CrateMetadata) -> bool {
let constness = match *self {
EntryKind::Method(data) => data.decode(meta).fn_data.constness,
EntryKind::Fn(data) => data.decode(meta).constness,
_ => hir::Constness::NotConst,
};
constness == hir::Constness::Const
}
}

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

pub fn can_have_local_instance(&self,
tcx: TyCtxt<'a, 'tcx, 'tcx>,
id: DefIndex) -> bool {
self.maybe_entry(id).map_or(false, |item| {
let item = item.decode(self);
// if we don't have a MIR, then this item was never meant to be locally instantiated
// or we have a bug in the metadata serialization
item.mir.is_some() && (
// items with generics always can have local instances if monomorphized
item.generics.map_or(false, |generics| {
let generics = generics.decode((self, tcx));
generics.parent_types != 0 || !generics.types.is_empty()
}) ||
match item.kind {
EntryKind::Closure(_) => true,
_ => false,
} ||
item.kind.is_const_fn(self) ||
attr::requests_inline(&self.get_attributes(&item))
)
})
}

pub fn maybe_get_item_mir(&self,
tcx: TyCtxt<'a, 'tcx, 'tcx>,
id: DefIndex)
Expand Down Expand Up @@ -1051,12 +1082,7 @@ impl<'a, 'tcx> CrateMetadata {
}

pub fn is_const_fn(&self, id: DefIndex) -> bool {
let constness = match self.entry(id).kind {
EntryKind::Method(data) => data.decode(self).fn_data.constness,
EntryKind::Fn(data) => data.decode(self).constness,
_ => hir::Constness::NotConst,
};
constness == hir::Constness::Const
self.entry(id).kind.is_const_fn(self)
}

pub fn is_foreign_item(&self, id: DefIndex) -> bool {
Expand Down
7 changes: 5 additions & 2 deletions src/librustc_metadata/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
let types = generics.parent_types as usize + generics.types.len();
let needs_inline = types > 0 || attr::requests_inline(&ast_item.attrs);
let is_const_fn = sig.constness == hir::Constness::Const;
(is_const_fn, needs_inline || is_const_fn)
let always_encode_mir = self.tcx.sess.opts.debugging_opts.always_encode_mir;
(is_const_fn, needs_inline || is_const_fn || always_encode_mir)
} else {
(false, false)
};
Expand Down Expand Up @@ -838,11 +839,13 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
_ => None,
},
mir: match item.node {
hir::ItemStatic(..) |
hir::ItemConst(..) => self.encode_mir(def_id),
hir::ItemFn(_, _, constness, _, ref generics, _) => {
let tps_len = generics.ty_params.len();
let needs_inline = tps_len > 0 || attr::requests_inline(&item.attrs);
if needs_inline || constness == hir::Constness::Const {
let always_encode_mir = self.tcx.sess.opts.debugging_opts.always_encode_mir;
if needs_inline || constness == hir::Constness::Const || always_encode_mir {
self.encode_mir(def_id)
} else {
None
Expand Down
5 changes: 1 addition & 4 deletions src/librustc_trans/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -706,10 +706,7 @@ impl<'a, 'tcx> MirVisitor<'tcx> for MirNeighborCollector<'a, 'tcx> {
fn can_have_local_instance<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
def_id: DefId)
-> bool {
// Take a look if we have the definition available. If not, we
// will not emit code for this item in the local crate, and thus
// don't create a translation item for it.
def_id.is_local() || tcx.sess.cstore.is_item_mir_available(def_id)
tcx.sess.cstore.can_have_local_instance(tcx, def_id)
}

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