-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #116505 - saethlin:infer-inline, r=<try>
Automatically enable cross-crate inlining for small functions This is a work-in-progress. For example I have not thought at all about the cost model and I am sure that the threshold is too high. But I'm curious to know how this looks in perf. It certainly has some unique effects on codegen.
- Loading branch information
Showing
23 changed files
with
381 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
use rustc_attr::InlineAttr; | ||
use rustc_hir::def_id::LocalDefId; | ||
use rustc_middle::query::Providers; | ||
use rustc_middle::ty::TyCtxt; | ||
use rustc_session::config::OptLevel; | ||
|
||
pub fn provide(providers: &mut Providers) { | ||
providers.cross_crate_inlinable = cross_crate_inlinable; | ||
} | ||
|
||
fn cross_crate_inlinable(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool { | ||
if tcx.sess.opts.incremental.is_some() { | ||
return false; | ||
} | ||
|
||
match tcx.codegen_fn_attrs(def_id).inline { | ||
InlineAttr::Never => return false, | ||
InlineAttr::Hint | InlineAttr::Always => return true, | ||
_ => {} | ||
} | ||
|
||
if matches!(tcx.sess.opts.optimize, OptLevel::No | OptLevel::Default) { | ||
return false; | ||
} | ||
|
||
match tcx.hir().body_const_context(def_id) { | ||
Some(rustc_hir::ConstContext::ConstFn) | None => {} | ||
_ => return false, | ||
} | ||
|
||
if tcx.lang_items().iter().any(|(_, lang_def_id)| lang_def_id == def_id.into()) { | ||
return false; | ||
} | ||
|
||
let mir = tcx.optimized_mir(def_id); | ||
let mut checker = CostChecker { tcx, cost: 0, callee_body: mir }; | ||
checker.visit_body(mir); | ||
checker.cost <= tcx.sess.opts.unstable_opts.cross_crate_inline_threshold.unwrap_or(100) | ||
} | ||
|
||
use rustc_middle::mir::visit::Visitor; | ||
use rustc_middle::mir::*; | ||
|
||
const INSTR_COST: usize = 5; | ||
const CALL_PENALTY: usize = 25; | ||
const LANDINGPAD_PENALTY: usize = 50; | ||
const RESUME_PENALTY: usize = 45; | ||
|
||
struct CostChecker<'b, 'tcx> { | ||
tcx: TyCtxt<'tcx>, | ||
cost: usize, | ||
callee_body: &'b Body<'tcx>, | ||
} | ||
|
||
impl<'tcx> Visitor<'tcx> for CostChecker<'_, 'tcx> { | ||
fn visit_statement(&mut self, statement: &Statement<'tcx>, _: Location) { | ||
// Don't count StorageLive/StorageDead in the inlining cost. | ||
match statement.kind { | ||
StatementKind::StorageLive(_) | ||
| StatementKind::StorageDead(_) | ||
| StatementKind::Deinit(_) | ||
| StatementKind::Nop => {} | ||
_ => self.cost += INSTR_COST, | ||
} | ||
} | ||
|
||
fn visit_terminator(&mut self, terminator: &Terminator<'tcx>, _: Location) { | ||
let tcx = self.tcx; | ||
match terminator.kind { | ||
TerminatorKind::Drop { ref place, unwind, .. } => { | ||
let ty = place.ty(self.callee_body, tcx).ty; | ||
if !ty.is_trivially_pure_clone_copy() { | ||
self.cost += CALL_PENALTY; | ||
if let UnwindAction::Cleanup(_) = unwind { | ||
self.cost += LANDINGPAD_PENALTY; | ||
} | ||
} | ||
} | ||
TerminatorKind::Call { unwind, .. } => { | ||
self.cost += CALL_PENALTY; | ||
if let UnwindAction::Cleanup(_) = unwind { | ||
self.cost += LANDINGPAD_PENALTY; | ||
} | ||
} | ||
TerminatorKind::Assert { unwind, .. } => { | ||
self.cost += CALL_PENALTY; | ||
if let UnwindAction::Cleanup(_) = unwind { | ||
self.cost += LANDINGPAD_PENALTY; | ||
} | ||
} | ||
TerminatorKind::UnwindResume => self.cost += RESUME_PENALTY, | ||
TerminatorKind::InlineAsm { unwind, .. } => { | ||
self.cost += INSTR_COST; | ||
if let UnwindAction::Cleanup(_) = unwind { | ||
self.cost += LANDINGPAD_PENALTY; | ||
} | ||
} | ||
_ => self.cost += INSTR_COST, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -66,7 +66,7 @@ | |
} | ||
+ } | ||
+ | ||
+ alloc15 (size: 8, align: 4) { | ||
+ alloc14 (size: 8, align: 4) { | ||
+ 02 00 00 00 05 20 00 00 │ ..... .. | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -66,7 +66,7 @@ | |
} | ||
+ } | ||
+ | ||
+ alloc14 (size: 8, align: 4) { | ||
+ alloc15 (size: 8, align: 4) { | ||
+ 05 20 00 00 01 00 00 00 │ . ...... | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.