Skip to content

Commit c8943c6

Browse files
committed
Add flags customizing behaviour of MIR inlining
* `-Zinline-mir-threshold` to change the default threshold. * `-Zinline-mir-hint-threshold` to change the threshold used by functions with inline hint.
1 parent cf9cf7c commit c8943c6

File tree

5 files changed

+86
-4
lines changed

5 files changed

+86
-4
lines changed

compiler/rustc_interface/src/tests.rs

+2
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,8 @@ fn test_debugging_options_tracking_hash() {
554554
tracked!(function_sections, Some(false));
555555
tracked!(human_readable_cgu_names, true);
556556
tracked!(inline_in_all_cgus, Some(true));
557+
tracked!(inline_mir_threshold, 123);
558+
tracked!(inline_mir_hint_threshold, 123);
557559
tracked!(insert_sideeffect, true);
558560
tracked!(instrument_coverage, true);
559561
tracked!(instrument_mcount, true);

compiler/rustc_mir/src/transform/inline.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ use crate::transform::MirPass;
1616
use std::iter;
1717
use std::ops::{Range, RangeFrom};
1818

19-
const DEFAULT_THRESHOLD: usize = 50;
20-
const HINT_THRESHOLD: usize = 100;
21-
2219
const INSTR_COST: usize = 5;
2320
const CALL_PENALTY: usize = 25;
2421
const LANDINGPAD_PENALTY: usize = 50;
@@ -248,7 +245,11 @@ impl Inliner<'tcx> {
248245
}
249246
}
250247

251-
let mut threshold = if hinted { HINT_THRESHOLD } else { DEFAULT_THRESHOLD };
248+
let mut threshold = if hinted {
249+
self.tcx.sess.opts.debugging_opts.inline_mir_hint_threshold
250+
} else {
251+
self.tcx.sess.opts.debugging_opts.inline_mir_threshold
252+
};
252253

253254
// Significantly lower the threshold for inlining cold functions
254255
if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::COLD) {

compiler/rustc_session/src/options.rs

+4
Original file line numberDiff line numberDiff line change
@@ -929,6 +929,10 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
929929
(default: no)"),
930930
incremental_verify_ich: bool = (false, parse_bool, [UNTRACKED],
931931
"verify incr. comp. hashes of green query instances (default: no)"),
932+
inline_mir_threshold: usize = (50, parse_uint, [TRACKED],
933+
"a default MIR inlining threshold (default: 50)"),
934+
inline_mir_hint_threshold: usize = (100, parse_uint, [TRACKED],
935+
"inlining threshold for functions with inline hint (default: 100)"),
932936
inline_in_all_cgus: Option<bool> = (None, parse_opt_bool, [TRACKED],
933937
"control whether `#[inline]` functions are in all CGUs"),
934938
input_stats: bool = (false, parse_bool, [UNTRACKED],
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Checks that inlining threshold can be controlled with
2+
// inline-mir-threshold and inline-hint-threshold options.
3+
//
4+
// compile-flags: -Zinline-mir-threshold=90
5+
// compile-flags: -Zinline-mir-hint-threshold=50
6+
7+
// EMIT_MIR inline_options.main.Inline.after.mir
8+
fn main() {
9+
not_inlined();
10+
inlined::<u32>();
11+
}
12+
13+
// Cost is approximately 3 * 25 + 5 = 80.
14+
#[inline]
15+
pub fn not_inlined() { g(); g(); g(); }
16+
pub fn inlined<T>() { g(); g(); g(); }
17+
18+
#[inline(never)]
19+
fn g() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// MIR for `main` after Inline
2+
3+
fn main() -> () {
4+
let mut _0: (); // return place in scope 0 at $DIR/inline-options.rs:8:11: 8:11
5+
let _1: (); // in scope 0 at $DIR/inline-options.rs:9:5: 9:18
6+
let _2: (); // in scope 0 at $DIR/inline-options.rs:10:5: 10:21
7+
scope 1 (inlined inlined::<u32>) { // at $DIR/inline-options.rs:10:5: 10:21
8+
let _3: (); // in scope 1 at $DIR/inline-options.rs:10:5: 10:21
9+
let _4: (); // in scope 1 at $DIR/inline-options.rs:10:5: 10:21
10+
let _5: (); // in scope 1 at $DIR/inline-options.rs:10:5: 10:21
11+
}
12+
13+
bb0: {
14+
StorageLive(_1); // scope 0 at $DIR/inline-options.rs:9:5: 9:18
15+
_1 = not_inlined() -> bb1; // scope 0 at $DIR/inline-options.rs:9:5: 9:18
16+
// mir::Constant
17+
// + span: $DIR/inline-options.rs:9:5: 9:16
18+
// + literal: Const { ty: fn() {not_inlined}, val: Value(Scalar(<ZST>)) }
19+
}
20+
21+
bb1: {
22+
StorageDead(_1); // scope 0 at $DIR/inline-options.rs:9:18: 9:19
23+
StorageLive(_2); // scope 0 at $DIR/inline-options.rs:10:5: 10:21
24+
StorageLive(_3); // scope 1 at $DIR/inline-options.rs:10:5: 10:21
25+
_3 = g() -> bb2; // scope 1 at $DIR/inline-options.rs:10:5: 10:21
26+
// mir::Constant
27+
// + span: $DIR/inline-options.rs:10:5: 10:21
28+
// + literal: Const { ty: fn() {g}, val: Value(Scalar(<ZST>)) }
29+
}
30+
31+
bb2: {
32+
StorageDead(_3); // scope 1 at $DIR/inline-options.rs:10:5: 10:21
33+
StorageLive(_4); // scope 1 at $DIR/inline-options.rs:10:5: 10:21
34+
_4 = g() -> bb3; // scope 1 at $DIR/inline-options.rs:10:5: 10:21
35+
// mir::Constant
36+
// + span: $DIR/inline-options.rs:10:5: 10:21
37+
// + literal: Const { ty: fn() {g}, val: Value(Scalar(<ZST>)) }
38+
}
39+
40+
bb3: {
41+
StorageDead(_4); // scope 1 at $DIR/inline-options.rs:10:5: 10:21
42+
StorageLive(_5); // scope 1 at $DIR/inline-options.rs:10:5: 10:21
43+
_5 = g() -> bb4; // scope 1 at $DIR/inline-options.rs:10:5: 10:21
44+
// mir::Constant
45+
// + span: $DIR/inline-options.rs:10:5: 10:21
46+
// + literal: Const { ty: fn() {g}, val: Value(Scalar(<ZST>)) }
47+
}
48+
49+
bb4: {
50+
StorageDead(_5); // scope 1 at $DIR/inline-options.rs:10:5: 10:21
51+
_2 = const (); // scope 1 at $DIR/inline-options.rs:10:5: 10:21
52+
StorageDead(_2); // scope 0 at $DIR/inline-options.rs:10:21: 10:22
53+
_0 = const (); // scope 0 at $DIR/inline-options.rs:8:11: 11:2
54+
return; // scope 0 at $DIR/inline-options.rs:11:2: 11:2
55+
}
56+
}

0 commit comments

Comments
 (0)