Skip to content
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

[WIP] Test performance of running MIR inliner on inline(always) function calls #82280

Closed
Closed
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
19 changes: 3 additions & 16 deletions compiler/rustc_const_eval/src/interpret/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 37 additions & 3 deletions compiler/rustc_mir_transform/src/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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(())
}

Expand Down Expand Up @@ -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);
Expand Down