Skip to content

Commit 4dd4d0b

Browse files
authored
Rollup merge of rust-lang#82376 - tmiasko:inline-options, r=oli-obk
Add option to enable MIR inlining independently of mir-opt-level Add `-Zinline-mir` option that enables MIR inlining independently of the current MIR opt level. The primary use-case is enabling MIR inlining on the default MIR opt level. Turn inlining thresholds into optional values to make it possible to configure different defaults depending on the current mir-opt-level (although thresholds are yet to be used in such a manner).
2 parents 754be63 + 5c546be commit 4dd4d0b

File tree

4 files changed

+29
-21
lines changed

4 files changed

+29
-21
lines changed

compiler/rustc_interface/src/tests.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -557,8 +557,9 @@ fn test_debugging_options_tracking_hash() {
557557
tracked!(function_sections, Some(false));
558558
tracked!(human_readable_cgu_names, true);
559559
tracked!(inline_in_all_cgus, Some(true));
560-
tracked!(inline_mir_threshold, 123);
561-
tracked!(inline_mir_hint_threshold, 123);
560+
tracked!(inline_mir, Some(true));
561+
tracked!(inline_mir_threshold, Some(123));
562+
tracked!(inline_mir_hint_threshold, Some(123));
562563
tracked!(insert_sideeffect, true);
563564
tracked!(instrument_coverage, true);
564565
tracked!(instrument_mcount, true);

compiler/rustc_mir/src/transform/inline.rs

+21-15
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,27 @@ struct CallSite<'tcx> {
3737
source_info: SourceInfo,
3838
}
3939

40+
/// Returns true if MIR inlining is enabled in the current compilation session.
41+
crate fn is_enabled(tcx: TyCtxt<'_>) -> bool {
42+
if tcx.sess.opts.debugging_opts.instrument_coverage {
43+
// Since `Inline` happens after `InstrumentCoverage`, the function-specific coverage
44+
// counters can be invalidated, such as by merging coverage counter statements from
45+
// a pre-inlined function into a different function. This kind of change is invalid,
46+
// so inlining must be skipped. Note: This check is performed here so inlining can
47+
// be disabled without preventing other optimizations (regardless of `mir_opt_level`).
48+
return false;
49+
}
50+
51+
if let Some(enabled) = tcx.sess.opts.debugging_opts.inline_mir {
52+
return enabled;
53+
}
54+
55+
tcx.sess.opts.debugging_opts.mir_opt_level >= 2
56+
}
57+
4058
impl<'tcx> MirPass<'tcx> for Inline {
4159
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
42-
// If you change this optimization level, also change the level in
43-
// `mir_drops_elaborated_and_const_checked` for the call to `mir_inliner_callees`.
44-
// Otherwise you will get an ICE about stolen MIR.
45-
if tcx.sess.opts.debugging_opts.mir_opt_level < 2 {
46-
return;
47-
}
48-
49-
if tcx.sess.opts.debugging_opts.instrument_coverage {
50-
// Since `Inline` happens after `InstrumentCoverage`, the function-specific coverage
51-
// counters can be invalidated, such as by merging coverage counter statements from
52-
// a pre-inlined function into a different function. This kind of change is invalid,
53-
// so inlining must be skipped. Note: This check is performed here so inlining can
54-
// be disabled without preventing other optimizations (regardless of `mir_opt_level`).
60+
if !is_enabled(tcx) {
5561
return;
5662
}
5763

@@ -310,9 +316,9 @@ impl Inliner<'tcx> {
310316
}
311317

312318
let mut threshold = if hinted {
313-
self.tcx.sess.opts.debugging_opts.inline_mir_hint_threshold
319+
self.tcx.sess.opts.debugging_opts.inline_mir_hint_threshold.unwrap_or(100)
314320
} else {
315-
self.tcx.sess.opts.debugging_opts.inline_mir_threshold
321+
self.tcx.sess.opts.debugging_opts.inline_mir_threshold.unwrap_or(50)
316322
};
317323

318324
if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::NAKED) {

compiler/rustc_mir/src/transform/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -429,8 +429,7 @@ fn mir_drops_elaborated_and_const_checked<'tcx>(
429429
let def = ty::WithOptConstParam::unknown(did);
430430

431431
// Do not compute the mir call graph without said call graph actually being used.
432-
// Keep this in sync with the mir inliner's optimization level.
433-
if tcx.sess.opts.debugging_opts.mir_opt_level >= 2 {
432+
if inline::is_enabled(tcx) {
434433
let _ = tcx.mir_inliner_callees(ty::InstanceDef::Item(def));
435434
}
436435
}

compiler/rustc_session/src/options.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -957,9 +957,11 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
957957
(default: no)"),
958958
incremental_verify_ich: bool = (false, parse_bool, [UNTRACKED],
959959
"verify incr. comp. hashes of green query instances (default: no)"),
960-
inline_mir_threshold: usize = (50, parse_uint, [TRACKED],
960+
inline_mir: Option<bool> = (None, parse_opt_bool, [TRACKED],
961+
"enable MIR inlining (default: no)"),
962+
inline_mir_threshold: Option<usize> = (None, parse_opt_uint, [TRACKED],
961963
"a default MIR inlining threshold (default: 50)"),
962-
inline_mir_hint_threshold: usize = (100, parse_uint, [TRACKED],
964+
inline_mir_hint_threshold: Option<usize> = (None, parse_opt_uint, [TRACKED],
963965
"inlining threshold for functions with inline hint (default: 100)"),
964966
inline_in_all_cgus: Option<bool> = (None, parse_opt_bool, [TRACKED],
965967
"control whether `#[inline]` functions are in all CGUs"),

0 commit comments

Comments
 (0)