Skip to content

Commit f071b50

Browse files
committed
Introduce should_encode_mir.
1 parent 85e355e commit f071b50

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,43 @@ fn should_encode_stability(def_kind: DefKind) -> bool {
785785
}
786786
}
787787

788+
/// Whether we should encode MIR.
789+
///
790+
/// Return a pair, resp. for CTFE and for LLVM.
791+
fn should_encode_mir(tcx: TyCtxt<'_>, def_id: LocalDefId) -> (bool, bool) {
792+
match tcx.def_kind(def_id) {
793+
// Constructors
794+
DefKind::Ctor(_, _) => {
795+
let mir_opt_base = tcx.sess.opts.output_types.should_codegen()
796+
|| tcx.sess.opts.debugging_opts.always_encode_mir;
797+
(true, mir_opt_base)
798+
}
799+
// Constants
800+
DefKind::AnonConst | DefKind::AssocConst | DefKind::Static | DefKind::Const => {
801+
(true, false)
802+
}
803+
// Closures and functions
804+
DefKind::Closure | DefKind::AssocFn | DefKind::Fn => {
805+
let generics = tcx.generics_of(def_id);
806+
let needs_inline = (generics.requires_monomorphization(tcx)
807+
|| tcx.codegen_fn_attrs(def_id).requests_inline())
808+
&& tcx.sess.opts.output_types.should_codegen();
809+
// Only check the presence of the `const` modifier.
810+
let is_const_fn = tcx.is_const_fn_raw(def_id.to_def_id());
811+
let always_encode_mir = tcx.sess.opts.debugging_opts.always_encode_mir;
812+
(is_const_fn, needs_inline || is_const_fn || always_encode_mir)
813+
}
814+
// Generators require optimized MIR to compute layout.
815+
DefKind::Generator => {
816+
// Only check the presence of the `const` modifier.
817+
let is_const_fn = tcx.is_const_fn_raw(def_id.to_def_id());
818+
(is_const_fn, true)
819+
}
820+
// The others don't have MIR.
821+
_ => (false, false),
822+
}
823+
}
824+
788825
impl EncodeContext<'a, 'tcx> {
789826
fn encode_def_ids(&mut self) {
790827
if self.is_proc_macro {

0 commit comments

Comments
 (0)