Skip to content

Commit 152a85f

Browse files
committed
slightly broken inlining
1 parent cb2462c commit 152a85f

File tree

8 files changed

+70
-17
lines changed

8 files changed

+70
-17
lines changed

compiler/rustc_metadata/src/rmeta/decoder.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,6 +1195,15 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
11951195
}
11961196
}
11971197

1198+
fn get_is_trivial_mir(&self, tcx: TyCtxt<'tcx>, id: DefIndex) -> bool {
1199+
self.root
1200+
.tables
1201+
.is_trivial_mir
1202+
.get(self, id)
1203+
.filter(|_| !self.is_proc_macro(id))
1204+
.map_or(false, |v| v.decode((self, tcx)))
1205+
}
1206+
11981207
fn get_optimized_mir(&self, tcx: TyCtxt<'tcx>, id: DefIndex) -> Body<'tcx> {
11991208
self.root
12001209
.tables

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ provide! { <'tcx> tcx, def_id, other, cdata,
111111
bug!("coerce_unsized_info: `{:?}` is missing its info", def_id);
112112
})
113113
}
114+
is_trivial_mir => { *tcx.arena.alloc(cdata.get_is_trivial_mir(tcx, def_id.index)) }
114115
optimized_mir => { tcx.arena.alloc(cdata.get_optimized_mir(tcx, def_id.index)) }
115116
promoted_mir => { tcx.arena.alloc(cdata.get_promoted_mir(tcx, def_id.index)) }
116117
mir_abstract_const => { cdata.get_mir_abstract_const(tcx, def_id.index) }

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,6 +1160,7 @@ impl EncodeContext<'a, 'tcx> {
11601160
fn encode_optimized_mir(&mut self, def_id: LocalDefId) {
11611161
debug!("EntryBuilder::encode_mir({:?})", def_id);
11621162
if self.tcx.mir_keys(LOCAL_CRATE).contains(&def_id) {
1163+
record!(self.tables.is_trivial_mir[def_id.to_def_id()] <- self.tcx.is_trivial_mir(def_id));
11631164
record!(self.tables.mir[def_id.to_def_id()] <- self.tcx.optimized_mir(def_id));
11641165

11651166
let unused = self.tcx.unused_generic_params(def_id);

compiler/rustc_metadata/src/rmeta/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ define_tables! {
300300
super_predicates: Table<DefIndex, Lazy!(ty::GenericPredicates<'tcx>)>,
301301
// As an optimization, a missing entry indicates an empty `&[]`.
302302
explicit_item_bounds: Table<DefIndex, Lazy!([(ty::Predicate<'tcx>, Span)])>,
303+
is_trivial_mir: Table<DefIndex, Lazy<bool>>,
303304
mir: Table<DefIndex, Lazy!(mir::Body<'tcx>)>,
304305
promoted_mir: Table<DefIndex, Lazy!(IndexVec<mir::Promoted, mir::Body<'tcx>>)>,
305306
mir_abstract_consts: Table<DefIndex, Lazy!(&'tcx [mir::abstract_const::Node<'tcx>])>,

compiler/rustc_middle/src/query/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,10 @@ rustc_queries! {
325325
}
326326
}
327327

328+
query is_trivial_mir(key: DefId) -> bool {
329+
desc { |tcx| "checking if MIR for `{}` is trivial", tcx.def_path_str(key) }
330+
}
331+
328332
/// MIR after our optimization passes have run. This is MIR that is ready
329333
/// for codegen. This is also the only query that can fetch non-local MIR, at present.
330334
query optimized_mir(key: DefId) -> &'tcx mir::Body<'tcx> {

compiler/rustc_mir/src/transform/inline.rs

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ struct CallSite<'tcx> {
3838

3939
impl<'tcx> MirPass<'tcx> for Inline {
4040
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
41-
if tcx.sess.opts.debugging_opts.mir_opt_level >= 2 {
41+
if tcx.sess.opts.debugging_opts.mir_opt_level >= 1 {
4242
if tcx.sess.opts.debugging_opts.instrument_coverage {
4343
// The current implementation of source code coverage injects code region counters
4444
// into the MIR, and assumes a 1-to-1 correspondence between MIR and source-code-
@@ -106,7 +106,12 @@ impl Inliner<'tcx> {
106106
continue;
107107
}
108108

109-
let callee_body = if let Some(callee_def_id) = callsite.callee.as_local() {
109+
let callee_body = if self.tcx.is_trivial_mir(callsite.callee) {
110+
self.tcx.optimized_mir(callsite.callee)
111+
} else if self.tcx.sess.opts.debugging_opts.mir_opt_level < 2 {
112+
// Only inline trivial functions by default.
113+
continue;
114+
} else if let Some(callee_def_id) = callsite.callee.as_local() {
110115
let callee_hir_id = self.tcx.hir().local_def_id_to_hir_id(callee_def_id);
111116
// Avoid a cycle here by only using `optimized_mir` only if we have
112117
// a lower `HirId` than the callee. This ensures that the callee will
@@ -843,3 +848,44 @@ impl<'a, 'tcx> MutVisitor<'tcx> for Integrator<'a, 'tcx> {
843848
*scope = self.scope_map[*scope];
844849
}
845850
}
851+
852+
struct FunctionCallFinder {
853+
found: bool,
854+
}
855+
856+
impl FunctionCallFinder {
857+
fn new() -> Self {
858+
FunctionCallFinder { found: false }
859+
}
860+
}
861+
862+
impl<'tcx> Visitor<'tcx> for FunctionCallFinder {
863+
fn visit_terminator(&mut self, terminator: &Terminator<'tcx>, _location: Location) {
864+
if let TerminatorKind::Call { .. } = terminator.kind {
865+
self.found = true;
866+
}
867+
}
868+
}
869+
870+
pub fn is_trivial_mir(tcx: TyCtxt<'tcx>, did: DefId) -> bool {
871+
debug!("is_trivial_mir({:?})", did);
872+
if tcx.is_constructor(did) {
873+
debug!("is_trivial_mir = true (constructor)");
874+
return true;
875+
}
876+
877+
if let Some(did) = did.as_local() {
878+
let body = tcx
879+
.mir_drops_elaborated_and_const_checked(ty::WithOptConstParam::unknown(did))
880+
.borrow();
881+
let mut finder = FunctionCallFinder::new();
882+
finder.visit_body(&body);
883+
debug!("is_trivial_mir = {}", !finder.found);
884+
!finder.found
885+
} else {
886+
// This branch is only taken if no `optimized_mir` is available for
887+
// an extern crate, as `is_trivial_mir` has otherwise been encoded.
888+
debug!("is_trivial_mir = false (no MIR available)");
889+
false
890+
}
891+
}

compiler/rustc_mir/src/transform/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ pub(crate) fn provide(providers: &mut Providers) {
6868
},
6969
mir_promoted,
7070
mir_drops_elaborated_and_const_checked,
71+
is_trivial_mir: inline::is_trivial_mir,
7172
optimized_mir,
7273
optimized_mir_of_const_arg,
7374
is_mir_available,
@@ -478,6 +479,8 @@ fn inner_optimized_mir(tcx: TyCtxt<'_>, def: ty::WithOptConstParam<LocalDefId>)
478479
return shim::build_adt_ctor(tcx, def.did.to_def_id());
479480
}
480481

482+
// `is_trivial_mir` uses `mir_drops_elaborated_and_const_checked` so run that first.
483+
tcx.ensure().is_trivial_mir(def.did.to_def_id());
481484
let mut body = tcx.mir_drops_elaborated_and_const_checked(def).steal();
482485
run_optimization_passes(tcx, &mut body);
483486

library/core/src/iter/range.rs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::char;
22
use crate::convert::TryFrom;
33
use crate::mem;
4-
use crate::ops::{self, Add, Sub, Try};
4+
use crate::ops::{self, Try};
55

66
use super::{FusedIterator, TrustedLen};
77

@@ -201,24 +201,12 @@ macro_rules! step_identical_methods {
201201

202202
#[inline]
203203
fn forward(start: Self, n: usize) -> Self {
204-
// In debug builds, trigger a panic on overflow.
205-
// This should optimize completely out in release builds.
206-
if Self::forward_checked(start, n).is_none() {
207-
let _ = Add::add(Self::MAX, 1);
208-
}
209-
// Do wrapping math to allow e.g. `Step::forward(-128i8, 255)`.
210-
start.wrapping_add(n as Self)
204+
start + (n as Self)
211205
}
212206

213207
#[inline]
214208
fn backward(start: Self, n: usize) -> Self {
215-
// In debug builds, trigger a panic on overflow.
216-
// This should optimize completely out in release builds.
217-
if Self::backward_checked(start, n).is_none() {
218-
let _ = Sub::sub(Self::MIN, 1);
219-
}
220-
// Do wrapping math to allow e.g. `Step::backward(127i8, 255)`.
221-
start.wrapping_sub(n as Self)
209+
start - (n as Self)
222210
}
223211
};
224212
}

0 commit comments

Comments
 (0)