Skip to content

Commit a98ddb6

Browse files
committed
Enable inlining of diverging functions
1 parent 2a628bd commit a98ddb6

9 files changed

+557
-7
lines changed

compiler/rustc_mir_transform/src/inline.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -424,13 +424,6 @@ impl<'tcx> Inliner<'tcx> {
424424
debug!(" final inline threshold = {}", threshold);
425425

426426
// FIXME: Give a bonus to functions with only a single caller
427-
let diverges = matches!(
428-
callee_body.basic_blocks[START_BLOCK].terminator().kind,
429-
TerminatorKind::Unreachable | TerminatorKind::Call { target: None, .. }
430-
);
431-
if diverges && !matches!(callee_attrs.inline, InlineAttr::Always) {
432-
return Err("callee diverges unconditionally");
433-
}
434427

435428
let mut checker = CostChecker {
436429
tcx: self.tcx,
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#![crate_type = "lib"]
2+
#![feature(unchecked_math)]
3+
4+
// ignore-debug: the debug assertions prevent the inlining we are testing for
5+
// compile-flags: -Zmir-opt-level=2 -Zinline-mir
6+
7+
// EMIT_MIR unchecked_shifts.unchecked_shl_unsigned_smaller.Inline.diff
8+
// EMIT_MIR unchecked_shifts.unchecked_shl_unsigned_smaller.PreCodegen.after.mir
9+
pub unsafe fn unchecked_shl_unsigned_smaller(a: u16, b: u32) -> u16 {
10+
a.unchecked_shl(b)
11+
}
12+
13+
// EMIT_MIR unchecked_shifts.unchecked_shr_signed_smaller.Inline.diff
14+
// EMIT_MIR unchecked_shifts.unchecked_shr_signed_smaller.PreCodegen.after.mir
15+
pub unsafe fn unchecked_shr_signed_smaller(a: i16, b: u32) -> i16 {
16+
a.unchecked_shr(b)
17+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
- // MIR for `unchecked_shl_unsigned_smaller` before Inline
2+
+ // MIR for `unchecked_shl_unsigned_smaller` after Inline
3+
4+
fn unchecked_shl_unsigned_smaller(_1: u16, _2: u32) -> u16 {
5+
debug a => _1; // in scope 0 at $DIR/unchecked_shifts.rs:+0:46: +0:47
6+
debug b => _2; // in scope 0 at $DIR/unchecked_shifts.rs:+0:54: +0:55
7+
let mut _0: u16; // return place in scope 0 at $DIR/unchecked_shifts.rs:+0:65: +0:68
8+
let mut _3: u16; // in scope 0 at $DIR/unchecked_shifts.rs:+1:5: +1:6
9+
let mut _4: u32; // in scope 0 at $DIR/unchecked_shifts.rs:+1:21: +1:22
10+
+ scope 1 (inlined core::num::<impl u16>::unchecked_shl) { // at $DIR/unchecked_shifts.rs:10:7: 10:23
11+
+ debug self => _3; // in scope 1 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
12+
+ debug rhs => _4; // in scope 1 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
13+
+ let mut _5: u16; // in scope 1 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
14+
+ let mut _6: std::option::Option<u16>; // in scope 1 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
15+
+ let mut _7: std::result::Result<u16, std::num::TryFromIntError>; // in scope 1 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
16+
+ scope 2 {
17+
+ scope 3 (inlined Result::<u16, TryFromIntError>::ok) { // at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
18+
+ debug self => _7; // in scope 3 at $SRC_DIR/core/src/result.rs:LL:COL
19+
+ let mut _8: isize; // in scope 3 at $SRC_DIR/core/src/result.rs:LL:COL
20+
+ let _9: u16; // in scope 3 at $SRC_DIR/core/src/result.rs:LL:COL
21+
+ scope 4 {
22+
+ debug x => _9; // in scope 4 at $SRC_DIR/core/src/result.rs:LL:COL
23+
+ }
24+
+ scope 5 {
25+
+ scope 6 {
26+
+ debug x => const TryFromIntError(()); // in scope 6 at $SRC_DIR/core/src/result.rs:LL:COL
27+
+ }
28+
+ }
29+
+ }
30+
+ scope 7 (inlined #[track_caller] Option::<u16>::unwrap_unchecked) { // at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
31+
+ debug self => _6; // in scope 7 at $SRC_DIR/core/src/option.rs:LL:COL
32+
+ let mut _10: &std::option::Option<u16>; // in scope 7 at $SRC_DIR/core/src/option.rs:LL:COL
33+
+ let mut _11: isize; // in scope 7 at $SRC_DIR/core/src/option.rs:LL:COL
34+
+ scope 8 {
35+
+ debug val => _5; // in scope 8 at $SRC_DIR/core/src/option.rs:LL:COL
36+
+ }
37+
+ scope 9 {
38+
+ scope 11 (inlined unreachable_unchecked) { // at $SRC_DIR/core/src/option.rs:LL:COL
39+
+ scope 12 {
40+
+ scope 13 (inlined unreachable_unchecked::runtime) { // at $SRC_DIR/core/src/intrinsics.rs:LL:COL
41+
+ }
42+
+ }
43+
+ }
44+
+ }
45+
+ scope 10 (inlined Option::<u16>::is_some) { // at $SRC_DIR/core/src/option.rs:LL:COL
46+
+ debug self => _10; // in scope 10 at $SRC_DIR/core/src/option.rs:LL:COL
47+
+ }
48+
+ }
49+
+ }
50+
+ }
51+
52+
bb0: {
53+
StorageLive(_3); // scope 0 at $DIR/unchecked_shifts.rs:+1:5: +1:6
54+
_3 = _1; // scope 0 at $DIR/unchecked_shifts.rs:+1:5: +1:6
55+
StorageLive(_4); // scope 0 at $DIR/unchecked_shifts.rs:+1:21: +1:22
56+
_4 = _2; // scope 0 at $DIR/unchecked_shifts.rs:+1:21: +1:22
57+
- _0 = core::num::<impl u16>::unchecked_shl(move _3, move _4) -> bb1; // scope 0 at $DIR/unchecked_shifts.rs:+1:5: +1:23
58+
+ StorageLive(_5); // scope 2 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
59+
+ StorageLive(_6); // scope 2 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
60+
+ StorageLive(_7); // scope 2 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
61+
+ _7 = <u32 as TryInto<u16>>::try_into(_4) -> bb1; // scope 2 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
62+
// mir::Constant
63+
- // + span: $DIR/unchecked_shifts.rs:10:7: 10:20
64+
- // + literal: Const { ty: unsafe fn(u16, u32) -> u16 {core::num::<impl u16>::unchecked_shl}, val: Value(<ZST>) }
65+
+ // + span: $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
66+
+ // + literal: Const { ty: fn(u32) -> Result<u16, <u32 as TryInto<u16>>::Error> {<u32 as TryInto<u16>>::try_into}, val: Value(<ZST>) }
67+
}
68+
69+
bb1: {
70+
+ StorageLive(_9); // scope 2 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
71+
+ _8 = discriminant(_7); // scope 3 at $SRC_DIR/core/src/result.rs:LL:COL
72+
+ switchInt(move _8) -> [0: bb6, 1: bb4, otherwise: bb5]; // scope 3 at $SRC_DIR/core/src/result.rs:LL:COL
73+
+ }
74+
+
75+
+ bb2: {
76+
+ StorageDead(_9); // scope 2 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
77+
+ StorageDead(_7); // scope 2 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
78+
+ StorageLive(_10); // scope 2 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
79+
+ _11 = discriminant(_6); // scope 7 at $SRC_DIR/core/src/option.rs:LL:COL
80+
+ switchInt(move _11) -> [1: bb7, otherwise: bb5]; // scope 7 at $SRC_DIR/core/src/option.rs:LL:COL
81+
+ }
82+
+
83+
+ bb3: {
84+
+ StorageDead(_5); // scope 2 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
85+
StorageDead(_4); // scope 0 at $DIR/unchecked_shifts.rs:+1:22: +1:23
86+
StorageDead(_3); // scope 0 at $DIR/unchecked_shifts.rs:+1:22: +1:23
87+
return; // scope 0 at $DIR/unchecked_shifts.rs:+2:2: +2:2
88+
+ }
89+
+
90+
+ bb4: {
91+
+ _6 = Option::<u16>::None; // scope 6 at $SRC_DIR/core/src/result.rs:LL:COL
92+
+ goto -> bb2; // scope 5 at $SRC_DIR/core/src/result.rs:LL:COL
93+
+ }
94+
+
95+
+ bb5: {
96+
+ unreachable; // scope 3 at $SRC_DIR/core/src/result.rs:LL:COL
97+
+ }
98+
+
99+
+ bb6: {
100+
+ _9 = move ((_7 as Ok).0: u16); // scope 3 at $SRC_DIR/core/src/result.rs:LL:COL
101+
+ _6 = Option::<u16>::Some(move _9); // scope 4 at $SRC_DIR/core/src/result.rs:LL:COL
102+
+ goto -> bb2; // scope 3 at $SRC_DIR/core/src/result.rs:LL:COL
103+
+ }
104+
+
105+
+ bb7: {
106+
+ _5 = move ((_6 as Some).0: u16); // scope 7 at $SRC_DIR/core/src/option.rs:LL:COL
107+
+ StorageDead(_10); // scope 2 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
108+
+ StorageDead(_6); // scope 2 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
109+
+ _0 = unchecked_shl::<u16>(_3, move _5) -> bb3; // scope 2 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
110+
+ // mir::Constant
111+
+ // + span: $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
112+
+ // + literal: Const { ty: unsafe extern "rust-intrinsic" fn(u16, u16) -> u16 {unchecked_shl::<u16>}, val: Value(<ZST>) }
113+
}
114+
}
115+
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// MIR for `unchecked_shl_unsigned_smaller` after PreCodegen
2+
3+
fn unchecked_shl_unsigned_smaller(_1: u16, _2: u32) -> u16 {
4+
debug a => _1; // in scope 0 at $DIR/unchecked_shifts.rs:+0:46: +0:47
5+
debug b => _2; // in scope 0 at $DIR/unchecked_shifts.rs:+0:54: +0:55
6+
let mut _0: u16; // return place in scope 0 at $DIR/unchecked_shifts.rs:+0:65: +0:68
7+
scope 1 (inlined core::num::<impl u16>::unchecked_shl) { // at $DIR/unchecked_shifts.rs:10:7: 10:23
8+
debug self => _1; // in scope 1 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
9+
debug rhs => _2; // in scope 1 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
10+
let mut _3: u16; // in scope 1 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
11+
let mut _4: std::option::Option<u16>; // in scope 1 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
12+
let mut _5: std::result::Result<u16, std::num::TryFromIntError>; // in scope 1 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
13+
scope 2 {
14+
scope 3 (inlined Result::<u16, TryFromIntError>::ok) { // at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
15+
debug self => _5; // in scope 3 at $SRC_DIR/core/src/result.rs:LL:COL
16+
let mut _6: isize; // in scope 3 at $SRC_DIR/core/src/result.rs:LL:COL
17+
let _7: u16; // in scope 3 at $SRC_DIR/core/src/result.rs:LL:COL
18+
scope 4 {
19+
debug x => _7; // in scope 4 at $SRC_DIR/core/src/result.rs:LL:COL
20+
}
21+
scope 5 {
22+
scope 6 {
23+
debug x => const TryFromIntError(()); // in scope 6 at $SRC_DIR/core/src/result.rs:LL:COL
24+
}
25+
}
26+
}
27+
scope 7 (inlined #[track_caller] Option::<u16>::unwrap_unchecked) { // at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
28+
debug self => _4; // in scope 7 at $SRC_DIR/core/src/option.rs:LL:COL
29+
let mut _8: &std::option::Option<u16>; // in scope 7 at $SRC_DIR/core/src/option.rs:LL:COL
30+
let mut _9: isize; // in scope 7 at $SRC_DIR/core/src/option.rs:LL:COL
31+
scope 8 {
32+
debug val => _3; // in scope 8 at $SRC_DIR/core/src/option.rs:LL:COL
33+
}
34+
scope 9 {
35+
scope 11 (inlined unreachable_unchecked) { // at $SRC_DIR/core/src/option.rs:LL:COL
36+
scope 12 {
37+
scope 13 (inlined unreachable_unchecked::runtime) { // at $SRC_DIR/core/src/intrinsics.rs:LL:COL
38+
}
39+
}
40+
}
41+
}
42+
scope 10 (inlined Option::<u16>::is_some) { // at $SRC_DIR/core/src/option.rs:LL:COL
43+
debug self => _8; // in scope 10 at $SRC_DIR/core/src/option.rs:LL:COL
44+
}
45+
}
46+
}
47+
}
48+
49+
bb0: {
50+
StorageLive(_3); // scope 2 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
51+
StorageLive(_4); // scope 2 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
52+
StorageLive(_5); // scope 2 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
53+
_5 = <u32 as TryInto<u16>>::try_into(_2) -> bb1; // scope 2 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
54+
// mir::Constant
55+
// + span: $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
56+
// + literal: Const { ty: fn(u32) -> Result<u16, <u32 as TryInto<u16>>::Error> {<u32 as TryInto<u16>>::try_into}, val: Value(<ZST>) }
57+
}
58+
59+
bb1: {
60+
StorageLive(_7); // scope 2 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
61+
_6 = discriminant(_5); // scope 3 at $SRC_DIR/core/src/result.rs:LL:COL
62+
switchInt(move _6) -> [0: bb6, 1: bb4, otherwise: bb5]; // scope 3 at $SRC_DIR/core/src/result.rs:LL:COL
63+
}
64+
65+
bb2: {
66+
StorageDead(_7); // scope 2 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
67+
StorageDead(_5); // scope 2 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
68+
StorageLive(_8); // scope 2 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
69+
_9 = discriminant(_4); // scope 7 at $SRC_DIR/core/src/option.rs:LL:COL
70+
switchInt(move _9) -> [1: bb7, otherwise: bb5]; // scope 7 at $SRC_DIR/core/src/option.rs:LL:COL
71+
}
72+
73+
bb3: {
74+
StorageDead(_3); // scope 2 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
75+
return; // scope 0 at $DIR/unchecked_shifts.rs:+2:2: +2:2
76+
}
77+
78+
bb4: {
79+
_4 = Option::<u16>::None; // scope 6 at $SRC_DIR/core/src/result.rs:LL:COL
80+
goto -> bb2; // scope 5 at $SRC_DIR/core/src/result.rs:LL:COL
81+
}
82+
83+
bb5: {
84+
unreachable; // scope 3 at $SRC_DIR/core/src/result.rs:LL:COL
85+
}
86+
87+
bb6: {
88+
_7 = move ((_5 as Ok).0: u16); // scope 3 at $SRC_DIR/core/src/result.rs:LL:COL
89+
_4 = Option::<u16>::Some(move _7); // scope 4 at $SRC_DIR/core/src/result.rs:LL:COL
90+
goto -> bb2; // scope 3 at $SRC_DIR/core/src/result.rs:LL:COL
91+
}
92+
93+
bb7: {
94+
_3 = move ((_4 as Some).0: u16); // scope 7 at $SRC_DIR/core/src/option.rs:LL:COL
95+
StorageDead(_8); // scope 2 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
96+
StorageDead(_4); // scope 2 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
97+
_0 = unchecked_shl::<u16>(_1, move _3) -> bb3; // scope 2 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
98+
// mir::Constant
99+
// + span: $SRC_DIR/core/src/num/uint_macros.rs:LL:COL
100+
// + literal: Const { ty: unsafe extern "rust-intrinsic" fn(u16, u16) -> u16 {unchecked_shl::<u16>}, val: Value(<ZST>) }
101+
}
102+
}

0 commit comments

Comments
 (0)