From 448815dccd29b93baee6baaa57e7eba74bb46644 Mon Sep 17 00:00:00 2001 From: Wesley Wiser Date: Thu, 30 Sep 2021 22:03:49 -0400 Subject: [PATCH 1/2] Test enabling MIR inliner --- compiler/rustc_mir_transform/src/inline.rs | 40 ++++++++++++++++++++-- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_mir_transform/src/inline.rs b/compiler/rustc_mir_transform/src/inline.rs index d43528a1cf098..d88eac3cf15c6 100644 --- a/compiler/rustc_mir_transform/src/inline.rs +++ b/compiler/rustc_mir_transform/src/inline.rs @@ -43,7 +43,14 @@ crate fn is_enabled(tcx: TyCtxt<'_>) -> bool { return enabled; } - tcx.sess.mir_opt_level() >= 3 + // If you change this optimization level, also change the level in + // `mir_drops_elaborated_and_const_checked` for the call to `mir_inliner_callees`. + // Otherwise you will get an ICE about stolen MIR. + match tcx.sess.mir_opt_level() { + 0 | 1 => false, + 2 => tcx.sess.opts.incremental == None, + _ => true, + } } impl<'tcx> MirPass<'tcx> for Inline { @@ -325,6 +332,17 @@ impl Inliner<'tcx> { } } + // At mir-opt-level=1, only inline `#[inline(always)]` functions + if self.tcx.sess.mir_opt_level() == 1 && callee_attrs.inline != InlineAttr::Always { + return Err("at mir-opt-level=1, only inline(always) is inlined"); + } + + if self.tcx.sess.mir_opt_level() == 1 + && self.tcx.sess.opts.optimize != rustc_session::config::OptLevel::Aggressive + { + return Err("at mir-opt-level=1, only inline if -O is specified"); + } + Ok(()) } @@ -470,8 +488,24 @@ impl Inliner<'tcx> { } if let InlineAttr::Always = callee_attrs.inline { - debug!("INLINING {:?} because inline(always) [cost={}]", callsite, cost); - Ok(()) + if self.tcx.sess.mir_opt_level() == 1 { + if cost <= 25 { + debug!( + "INLINING {:?} because inline(always) and [cost={} <= threshold=25]", + callsite, cost + ); + Ok(()) + } else { + debug!( + "NOT inlining {:?} because inline(always) but [cost={} > threshold=25]", + callsite, cost + ); + Err("cost above threshold") + } + } else { + debug!("INLINING {:?} because inline(always) [cost={}]", callsite, cost); + Ok(()) + } } else { if cost <= threshold { debug!("INLINING {:?} [cost={} <= threshold={}]", callsite, cost, threshold); From d28bda056aa47f79d55c6a9c88117c8911eb5db2 Mon Sep 17 00:00:00 2001 From: Wesley Wiser Date: Thu, 30 Sep 2021 21:58:49 -0400 Subject: [PATCH 2/2] Don't assert polymorphization has taken effect in const eval Const eval no longer runs MIR optimizations so unless this is getting run as part of a MIR optimization like const-prop, there can be unused type parameters even if polymorphization is enabled. --- .../rustc_const_eval/src/interpret/util.rs | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/compiler/rustc_const_eval/src/interpret/util.rs b/compiler/rustc_const_eval/src/interpret/util.rs index eb0fdebb665fa..8685aba393fd1 100644 --- a/compiler/rustc_const_eval/src/interpret/util.rs +++ b/compiler/rustc_const_eval/src/interpret/util.rs @@ -43,22 +43,9 @@ where let is_used = unused_params.contains(index).map_or(true, |unused| !unused); // Only recurse when generic parameters in fns, closures and generators // are used and require substitution. - match (is_used, subst.definitely_needs_subst(self.tcx)) { - // Just in case there are closures or generators within this subst, - // recurse. - (true, true) => return subst.super_visit_with(self), - // Confirm that polymorphization replaced the parameter with - // `ty::Param`/`ty::ConstKind::Param`. - (false, true) if cfg!(debug_assertions) => match subst.unpack() { - ty::subst::GenericArgKind::Type(ty) => { - assert!(matches!(ty.kind(), ty::Param(_))) - } - ty::subst::GenericArgKind::Const(ct) => { - assert!(matches!(ct.val, ty::ConstKind::Param(_))) - } - ty::subst::GenericArgKind::Lifetime(..) => (), - }, - _ => {} + // Just in case there are closures or generators within this subst, recurse. + if is_used && subst.definitely_needs_subst(self.tcx) { + return subst.super_visit_with(self); } } ControlFlow::CONTINUE