Skip to content

Commit 1dbed32

Browse files
committed
Auto merge of rust-lang#116898 - saethlin:inline-trivial-wrappers, r=<try>
Automatic #[inline] for trivial wrappers r? `@ghost`
2 parents d4c86cf + 3c388ba commit 1dbed32

16 files changed

+51
-4
lines changed

compiler/rustc_mir_transform/src/cross_crate_inline.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,18 @@ fn cross_crate_inlinable(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
6565
let mut checker =
6666
CostChecker { tcx, callee_body: mir, calls: 0, statements: 0, landing_pads: 0, resumes: 0 };
6767
checker.visit_body(mir);
68-
checker.calls == 0
68+
let is_leaf = checker.calls == 0
6969
&& checker.resumes == 0
7070
&& checker.landing_pads == 0
71-
&& checker.statements <= threshold
71+
&& checker.statements <= threshold;
72+
73+
let is_wrapper = checker.calls == 1
74+
&& checker.resumes == 0
75+
&& checker.landing_pads == 0
76+
&& checker.statements == 0
77+
&& mir.basic_blocks.len() == 2;
78+
79+
is_leaf || is_wrapper
7280
}
7381

7482
struct CostChecker<'b, 'tcx> {

tests/codegen-units/item-collection/implicit-panic-call.rs

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ impl Div for i32 {
4848
}
4949

5050
#[allow(unconditional_panic)]
51+
#[inline(never)]
5152
pub fn foo() {
5253
// This implicitly generates a panic call.
5354
let _ = 1 / 0;

tests/codegen/call-metadata.rs

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#![crate_type = "lib"]
77

8+
#[inline(never)]
89
pub fn test() {
910
// CHECK: call noundef i8 @some_true(){{( #[0-9]+)?}}, !range [[R0:![0-9]+]]
1011
// CHECK: [[R0]] = !{i8 0, i8 3}

tests/codegen/cffi/ffi-const.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#![crate_type = "lib"]
33
#![feature(ffi_const)]
44

5+
#[inline(never)]
56
pub fn bar() { unsafe { foo() } }
67

78
extern "C" {

tests/codegen/cffi/ffi-pure.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#![crate_type = "lib"]
33
#![feature(ffi_pure)]
44

5+
#[inline(never)]
56
pub fn bar() { unsafe { foo() } }
67

78
extern "C" {

tests/codegen/cffi/ffi-returns-twice.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#![crate_type = "lib"]
33
#![feature(ffi_returns_twice)]
44

5+
#[inline(never)]
56
pub fn bar() { unsafe { foo() } }
67

78
extern "C" {

tests/codegen/cross-crate-inlining/auxiliary/leaf.rs

+5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ pub fn stem_fn() -> String {
1414
inner()
1515
}
1616

17+
// This function contains multiple calls.
18+
pub fn multi_stem_fn() -> String {
19+
inner() + &inner()
20+
}
21+
1722
#[inline(never)]
1823
fn inner() -> String {
1924
String::from("test")

tests/codegen/cross-crate-inlining/leaf-inlining.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,16 @@ pub fn leaf_outer() -> String {
1212
leaf::leaf_fn()
1313
}
1414

15-
// Check that we do not inline a non-leaf cross-crate call
15+
// Check that we inline a cross-crate call where the callee contains a single call
1616
#[no_mangle]
1717
pub fn stem_outer() -> String {
18-
// CHECK: call {{.*}}stem_fn
18+
// CHECK-NOT: call {{.*}}stem_fn
1919
leaf::stem_fn()
2020
}
21+
22+
// Check that we do not inline a cross-crate call where the callee contains multiple calls
23+
#[no_mangle]
24+
pub fn multi_stem_outer() -> String {
25+
// CHECK: call {{.*}}multi_stem_fn
26+
leaf::multi_stem_fn()
27+
}

tests/codegen/sanitizer/cfi-emit-type-metadata-itanium-cxx-abi-generalized.rs

+3
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,23 @@
55

66
#![crate_type="lib"]
77

8+
#[inline(never)]
89
pub fn foo(f: fn(i32) -> i32, arg: i32) -> i32 {
910
// CHECK-LABEL: define{{.*}}foo
1011
// CHECK-SAME: {{.*}}!type ![[TYPE1:[0-9]+]] !type !{{[0-9]+}} !type !{{[0-9]+}}
1112
// CHECK: call i1 @llvm.type.test(ptr {{%f|%0}}, metadata !"_ZTSFu3i32S_E.generalized")
1213
f(arg)
1314
}
1415

16+
#[inline(never)]
1517
pub fn bar(f: fn(i32, i32) -> i32, arg1: i32, arg2: i32) -> i32 {
1618
// CHECK-LABEL: define{{.*}}bar
1719
// CHECK-SAME: {{.*}}!type ![[TYPE2:[0-9]+]] !type !{{[0-9]+}} !type !{{[0-9]+}}
1820
// CHECK: call i1 @llvm.type.test(ptr {{%f|%0}}, metadata !"_ZTSFu3i32S_S_E.generalized")
1921
f(arg1, arg2)
2022
}
2123

24+
#[inline(never)]
2225
pub fn baz(f: fn(i32, i32, i32) -> i32, arg1: i32, arg2: i32, arg3: i32) -> i32 {
2326
// CHECK-LABEL: define{{.*}}baz
2427
// CHECK-SAME: {{.*}}!type ![[TYPE3:[0-9]+]] !type !{{[0-9]+}} !type !{{[0-9]+}}

tests/codegen/sanitizer/cfi-emit-type-metadata-itanium-cxx-abi-normalized-generalized.rs

+3
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,23 @@
55

66
#![crate_type="lib"]
77

8+
#[inline(never)]
89
pub fn foo(f: fn(i32) -> i32, arg: i32) -> i32 {
910
// CHECK-LABEL: define{{.*}}foo
1011
// CHECK-SAME: {{.*}}![[TYPE1:[0-9]+]]
1112
// CHECK: call i1 @llvm.type.test(ptr {{%f|%0}}, metadata !"_ZTSFu3i32S_E.normalized.generalized")
1213
f(arg)
1314
}
1415

16+
#[inline(never)]
1517
pub fn bar(f: fn(i32, i32) -> i32, arg1: i32, arg2: i32) -> i32 {
1618
// CHECK-LABEL: define{{.*}}bar
1719
// CHECK-SAME: {{.*}}![[TYPE2:[0-9]+]]
1820
// CHECK: call i1 @llvm.type.test(ptr {{%f|%0}}, metadata !"_ZTSFu3i32S_S_E.normalized.generalized")
1921
f(arg1, arg2)
2022
}
2123

24+
#[inline(never)]
2225
pub fn baz(f: fn(i32, i32, i32) -> i32, arg1: i32, arg2: i32, arg3: i32) -> i32 {
2326
// CHECK-LABEL: define{{.*}}baz
2427
// CHECK-SAME: {{.*}}![[TYPE3:[0-9]+]]

tests/codegen/sanitizer/cfi-emit-type-metadata-itanium-cxx-abi-normalized.rs

+3
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,23 @@
55

66
#![crate_type="lib"]
77

8+
#[inline(never)]
89
pub fn foo(f: fn(i32) -> i32, arg: i32) -> i32 {
910
// CHECK-LABEL: define{{.*}}foo
1011
// CHECK-SAME: {{.*}}!type ![[TYPE1:[0-9]+]] !type !{{[0-9]+}}
1112
// CHECK: call i1 @llvm.type.test(ptr {{%f|%0}}, metadata !"_ZTSFu3i32S_E.normalized")
1213
f(arg)
1314
}
1415

16+
#[inline(never)]
1517
pub fn bar(f: fn(i32, i32) -> i32, arg1: i32, arg2: i32) -> i32 {
1618
// CHECK-LABEL: define{{.*}}bar
1719
// CHECK-SAME: {{.*}}!type ![[TYPE2:[0-9]+]] !type !{{[0-9]+}}
1820
// CHECK: call i1 @llvm.type.test(ptr {{%f|%0}}, metadata !"_ZTSFu3i32S_S_E.normalized")
1921
f(arg1, arg2)
2022
}
2123

24+
#[inline(never)]
2225
pub fn baz(f: fn(i32, i32, i32) -> i32, arg1: i32, arg2: i32, arg3: i32) -> i32 {
2326
// CHECK-LABEL: define{{.*}}baz
2427
// CHECK-SAME: {{.*}}!type ![[TYPE3:[0-9]+]] !type !{{[0-9]+}}

tests/codegen/sanitizer/cfi-emit-type-metadata-itanium-cxx-abi.rs

+3
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,23 @@
55

66
#![crate_type="lib"]
77

8+
#[inline(never)]
89
pub fn foo(f: fn(i32) -> i32, arg: i32) -> i32 {
910
// CHECK-LABEL: define{{.*}}foo
1011
// CHECK-SAME: {{.*}}!type ![[TYPE1:[0-9]+]] !type !{{[0-9]+}} !type !{{[0-9]+}} !type !{{[0-9]+}}
1112
// CHECK: call i1 @llvm.type.test(ptr {{%f|%0}}, metadata !"_ZTSFu3i32S_E")
1213
f(arg)
1314
}
1415

16+
#[inline(never)]
1517
pub fn bar(f: fn(i32, i32) -> i32, arg1: i32, arg2: i32) -> i32 {
1618
// CHECK-LABEL: define{{.*}}bar
1719
// CHECK-SAME: {{.*}}!type ![[TYPE2:[0-9]+]] !type !{{[0-9]+}} !type !{{[0-9]+}} !type !{{[0-9]+}}
1820
// CHECK: call i1 @llvm.type.test(ptr {{%f|%0}}, metadata !"_ZTSFu3i32S_S_E")
1921
f(arg1, arg2)
2022
}
2123

24+
#[inline(never)]
2225
pub fn baz(f: fn(i32, i32, i32) -> i32, arg1: i32, arg2: i32, arg3: i32) -> i32 {
2326
// CHECK-LABEL: define{{.*}}baz
2427
// CHECK-SAME: {{.*}}!type ![[TYPE3:[0-9]+]] !type !{{[0-9]+}} !type !{{[0-9]+}} !type !{{[0-9]+}}

tests/codegen/sanitizer/kcfi-emit-kcfi-operand-bundle-itanium-cxx-abi-generalized.rs

+3
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,23 @@ trait Copy { }
1818

1919
impl Copy for i32 {}
2020

21+
#[inline(never)]
2122
pub fn foo(f: fn(i32) -> i32, arg: i32) -> i32 {
2223
// CHECK-LABEL: define{{.*}}foo
2324
// CHECK-SAME: {{.*}}!{{<unknown kind #36>|kcfi_type}} ![[TYPE1:[0-9]+]]
2425
// CHECK: {{%.+}} = call {{(noundef )*}}i32 %f(i32 {{(noundef )*}}%arg){{.*}}[ "kcfi"(i32 233085384) ]
2526
f(arg)
2627
}
2728

29+
#[inline(never)]
2830
pub fn bar(f: fn(i32, i32) -> i32, arg1: i32, arg2: i32) -> i32 {
2931
// CHECK-LABEL: define{{.*}}bar
3032
// CHECK-SAME: {{.*}}!{{<unknown kind #36>|kcfi_type}} ![[TYPE2:[0-9]+]]
3133
// CHECK: {{%.+}} = call {{(noundef )*}}i32 %f(i32 {{(noundef )*}}%arg1, i32 {{(noundef )*}}%arg2){{.*}}[ "kcfi"(i32 435418021) ]
3234
f(arg1, arg2)
3335
}
3436

37+
#[inline(never)]
3538
pub fn baz(f: fn(i32, i32, i32) -> i32, arg1: i32, arg2: i32, arg3: i32) -> i32 {
3639
// CHECK-LABEL: define{{.*}}baz
3740
// CHECK-SAME: {{.*}}!{{<unknown kind #36>|kcfi_type}} ![[TYPE3:[0-9]+]]

tests/codegen/sanitizer/kcfi-emit-kcfi-operand-bundle-itanium-cxx-abi-normalized-generalized.rs

+3
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,23 @@ trait Copy { }
1818

1919
impl Copy for i32 {}
2020

21+
#[inline(never)]
2122
pub fn foo(f: fn(i32) -> i32, arg: i32) -> i32 {
2223
// CHECK-LABEL: define{{.*}}foo
2324
// CHECK-SAME: {{.*}}!{{<unknown kind #36>|kcfi_type}} ![[TYPE1:[0-9]+]]
2425
// CHECK: {{%.+}} = call {{(noundef )*}}i32 %f(i32 {{(noundef )*}}%arg){{.*}}[ "kcfi"(i32 -686570305) ]
2526
f(arg)
2627
}
2728

29+
#[inline(never)]
2830
pub fn bar(f: fn(i32, i32) -> i32, arg1: i32, arg2: i32) -> i32 {
2931
// CHECK-LABEL: define{{.*}}bar
3032
// CHECK-SAME: {{.*}}!{{<unknown kind #36>|kcfi_type}} ![[TYPE2:[0-9]+]]
3133
// CHECK: {{%.+}} = call {{(noundef )*}}i32 %f(i32 {{(noundef )*}}%arg1, i32 {{(noundef )*}}%arg2){{.*}}[ "kcfi"(i32 1281038450) ]
3234
f(arg1, arg2)
3335
}
3436

37+
#[inline(never)]
3538
pub fn baz(f: fn(i32, i32, i32) -> i32, arg1: i32, arg2: i32, arg3: i32) -> i32 {
3639
// CHECK-LABEL: define{{.*}}baz
3740
// CHECK-SAME: {{.*}}!{{<unknown kind #36>|kcfi_type}} ![[TYPE3:[0-9]+]]

tests/codegen/sanitizer/kcfi-emit-kcfi-operand-bundle-itanium-cxx-abi-normalized.rs

+3
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,23 @@ trait Copy { }
1818

1919
impl Copy for i32 {}
2020

21+
#[inline(never)]
2122
pub fn foo(f: fn(i32) -> i32, arg: i32) -> i32 {
2223
// CHECK-LABEL: define{{.*}}foo
2324
// CHECK-SAME: {{.*}}!{{<unknown kind #36>|kcfi_type}} ![[TYPE1:[0-9]+]]
2425
// CHECK: {{%.+}} = call {{(noundef )*}}i32 %f(i32 {{(noundef )*}}%arg){{.*}}[ "kcfi"(i32 -841055669) ]
2526
f(arg)
2627
}
2728

29+
#[inline(never)]
2830
pub fn bar(f: fn(i32, i32) -> i32, arg1: i32, arg2: i32) -> i32 {
2931
// CHECK-LABEL: define{{.*}}bar
3032
// CHECK-SAME: {{.*}}!{{<unknown kind #36>|kcfi_type}} ![[TYPE2:[0-9]+]]
3133
// CHECK: {{%.+}} = call {{(noundef )*}}i32 %f(i32 {{(noundef )*}}%arg1, i32 {{(noundef )*}}%arg2){{.*}}[ "kcfi"(i32 1390819368) ]
3234
f(arg1, arg2)
3335
}
3436

37+
#[inline(never)]
3538
pub fn baz(f: fn(i32, i32, i32) -> i32, arg1: i32, arg2: i32, arg3: i32) -> i32 {
3639
// CHECK-LABEL: define{{.*}}baz
3740
// CHECK-SAME: {{.*}}!{{<unknown kind #36>|kcfi_type}} ![[TYPE3:[0-9]+]]
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#[inline(never)]
22
pub fn bar() {}
33

4+
#[inline(never)]
45
pub fn foo() {
56
bar();
67
}

0 commit comments

Comments
 (0)