Skip to content

Commit

Permalink
Reorder MIR encoding checks
Browse files Browse the repository at this point in the history
Start from least expensive checks when deciding whether to encode MIR or not.

No functional changes intended.
  • Loading branch information
tmiasko committed Jan 12, 2021
1 parent 704e47f commit 1bf3949
Showing 1 changed file with 20 additions and 25 deletions.
45 changes: 20 additions & 25 deletions compiler/rustc_metadata/src/rmeta/encoder.rs
Expand Up @@ -66,6 +66,11 @@ pub(super) struct EncodeContext<'a, 'tcx> {
required_source_files: Option<GrowableBitSet<usize>>,
is_proc_macro: bool,
hygiene_ctxt: &'a HygieneEncodeContext,

// Determines if MIR used for code generation will be included in the crate
// metadata. When emitting only metadata (e.g., cargo check), we can avoid
// generating optimized MIR altogether.
emit_codegen_mir: bool,
}

/// If the current crate is a proc-macro, returns early with `Lazy:empty()`.
Expand Down Expand Up @@ -1032,11 +1037,6 @@ impl EncodeContext<'a, 'tcx> {
}
}

fn metadata_output_only(&self) -> bool {
// MIR optimisation can be skipped when we're just interested in the metadata.
!self.tcx.sess.opts.output_types.should_codegen()
}

fn encode_info_for_impl_item(&mut self, def_id: DefId) {
debug!("EncodeContext::encode_info_for_impl_item({:?})", def_id);
let tcx = self.tcx;
Expand Down Expand Up @@ -1105,13 +1105,12 @@ impl EncodeContext<'a, 'tcx> {
let (mir, mir_const) = match ast_item.kind {
hir::ImplItemKind::Const(..) => (false, true),
hir::ImplItemKind::Fn(ref sig, _) => {
let generics = self.tcx.generics_of(def_id);
let needs_inline = (generics.requires_monomorphization(self.tcx)
|| tcx.codegen_fn_attrs(def_id).requests_inline())
&& !self.metadata_output_only();
let opt_mir = tcx.sess.opts.debugging_opts.always_encode_mir
|| (self.emit_codegen_mir
&& (tcx.generics_of(def_id).requires_monomorphization(tcx)
|| tcx.codegen_fn_attrs(def_id).requests_inline()));
let is_const_fn = sig.header.constness == hir::Constness::Const;
let always_encode_mir = self.tcx.sess.opts.debugging_opts.always_encode_mir;
(needs_inline || always_encode_mir, is_const_fn)
(opt_mir, is_const_fn)
}
hir::ImplItemKind::TyAlias(..) => (false, false),
};
Expand Down Expand Up @@ -1433,16 +1432,13 @@ impl EncodeContext<'a, 'tcx> {
let (mir, const_mir) = match item.kind {
hir::ItemKind::Static(..) | hir::ItemKind::Const(..) => (false, true),
hir::ItemKind::Fn(ref sig, ..) => {
let generics = tcx.generics_of(def_id);
let needs_inline = (generics.requires_monomorphization(tcx)
|| tcx.codegen_fn_attrs(def_id).requests_inline())
&& !self.metadata_output_only();

let opt_mir = self.tcx.sess.opts.debugging_opts.always_encode_mir
|| (self.emit_codegen_mir
&& (tcx.generics_of(def_id).requires_monomorphization(tcx)
|| tcx.codegen_fn_attrs(def_id).requests_inline()));
let is_const_fn = sig.header.constness == hir::Constness::Const;
let always_encode_mir = self.tcx.sess.opts.debugging_opts.always_encode_mir;
let mir = needs_inline || always_encode_mir;
// We don't need the optimized MIR for const fns.
(mir, is_const_fn)
(opt_mir, is_const_fn)
}
_ => (false, false),
};
Expand Down Expand Up @@ -2008,10 +2004,9 @@ impl<'tcx, 'v> ParItemLikeVisitor<'v> for PrefetchVisitor<'tcx> {
}
hir::ItemKind::Fn(ref sig, ..) => {
let def_id = tcx.hir().local_def_id(item.hir_id);
let generics = tcx.generics_of(def_id.to_def_id());
let needs_inline = generics.requires_monomorphization(tcx)
let opt_mir = tcx.generics_of(def_id.to_def_id()).requires_monomorphization(tcx)
|| tcx.codegen_fn_attrs(def_id.to_def_id()).requests_inline();
if needs_inline {
if opt_mir {
self.prefetch_mir(def_id)
}
if sig.header.constness == hir::Constness::Const {
Expand Down Expand Up @@ -2045,11 +2040,10 @@ impl<'tcx, 'v> ParItemLikeVisitor<'v> for PrefetchVisitor<'tcx> {
}
hir::ImplItemKind::Fn(ref sig, _) => {
let def_id = tcx.hir().local_def_id(impl_item.hir_id);
let generics = tcx.generics_of(def_id.to_def_id());
let needs_inline = generics.requires_monomorphization(tcx)
let opt_mir = tcx.generics_of(def_id.to_def_id()).requires_monomorphization(tcx)
|| tcx.codegen_fn_attrs(def_id.to_def_id()).requests_inline();
let is_const_fn = sig.header.constness == hir::Constness::Const;
if needs_inline {
if opt_mir {
self.prefetch_mir(def_id)
}
if is_const_fn {
Expand Down Expand Up @@ -2148,6 +2142,7 @@ fn encode_metadata_impl(tcx: TyCtxt<'_>) -> EncodedMetadata {
required_source_files,
is_proc_macro: tcx.sess.crate_types().contains(&CrateType::ProcMacro),
hygiene_ctxt: &hygiene_ctxt,
emit_codegen_mir: tcx.sess.opts.output_types.should_codegen(),
};

// Encode the rustc version string in a predictable location.
Expand Down

0 comments on commit 1bf3949

Please sign in to comment.