Skip to content

Automatic #[inline] for trivial wrappers #116898

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions compiler/rustc_mir_transform/src/cross_crate_inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,18 @@ fn cross_crate_inlinable(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
let mut checker =
CostChecker { tcx, callee_body: mir, calls: 0, statements: 0, landing_pads: 0, resumes: 0 };
checker.visit_body(mir);
checker.calls == 0
let is_leaf = checker.calls == 0
&& checker.resumes == 0
&& checker.landing_pads == 0
&& checker.statements <= threshold
&& checker.statements <= threshold;

let is_wrapper = checker.calls == 1
&& checker.resumes == 0
&& checker.landing_pads == 0
&& checker.statements == 0
&& mir.basic_blocks.len() == 2;

is_leaf || is_wrapper
}

struct CostChecker<'b, 'tcx> {
Expand Down
1 change: 1 addition & 0 deletions tests/codegen-units/item-collection/implicit-panic-call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ impl Div for i32 {
}

#[allow(unconditional_panic)]
#[inline(never)]
pub fn foo() {
// This implicitly generates a panic call.
let _ = 1 / 0;
Expand Down
1 change: 1 addition & 0 deletions tests/codegen/call-metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#![crate_type = "lib"]

#[inline(never)]
pub fn test() {
// CHECK: call noundef i8 @some_true(){{( #[0-9]+)?}}, !range [[R0:![0-9]+]]
// CHECK: [[R0]] = !{i8 0, i8 3}
Expand Down
1 change: 1 addition & 0 deletions tests/codegen/cffi/ffi-const.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#![crate_type = "lib"]
#![feature(ffi_const)]

#[inline(never)]
pub fn bar() { unsafe { foo() } }

extern "C" {
Expand Down
1 change: 1 addition & 0 deletions tests/codegen/cffi/ffi-pure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#![crate_type = "lib"]
#![feature(ffi_pure)]

#[inline(never)]
pub fn bar() { unsafe { foo() } }

extern "C" {
Expand Down
1 change: 1 addition & 0 deletions tests/codegen/cffi/ffi-returns-twice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#![crate_type = "lib"]
#![feature(ffi_returns_twice)]

#[inline(never)]
pub fn bar() { unsafe { foo() } }

extern "C" {
Expand Down
5 changes: 5 additions & 0 deletions tests/codegen/cross-crate-inlining/auxiliary/leaf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ pub fn stem_fn() -> String {
inner()
}

// This function contains multiple calls.
pub fn multi_stem_fn() -> String {
inner() + &inner()
}

#[inline(never)]
fn inner() -> String {
String::from("test")
Expand Down
11 changes: 9 additions & 2 deletions tests/codegen/cross-crate-inlining/leaf-inlining.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,16 @@ pub fn leaf_outer() -> String {
leaf::leaf_fn()
}

// Check that we do not inline a non-leaf cross-crate call
// Check that we inline a cross-crate call where the callee contains a single call
#[no_mangle]
pub fn stem_outer() -> String {
// CHECK: call {{.*}}stem_fn
// CHECK-NOT: call {{.*}}stem_fn
leaf::stem_fn()
}

// Check that we do not inline a cross-crate call where the callee contains multiple calls
#[no_mangle]
pub fn multi_stem_outer() -> String {
// CHECK: call {{.*}}multi_stem_fn
leaf::multi_stem_fn()
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,23 @@

#![crate_type="lib"]

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

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

#[inline(never)]
pub fn baz(f: fn(i32, i32, i32) -> i32, arg1: i32, arg2: i32, arg3: i32) -> i32 {
// CHECK-LABEL: define{{.*}}baz
// CHECK-SAME: {{.*}}!type ![[TYPE3:[0-9]+]] !type !{{[0-9]+}} !type !{{[0-9]+}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,23 @@

#![crate_type="lib"]

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

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

#[inline(never)]
pub fn baz(f: fn(i32, i32, i32) -> i32, arg1: i32, arg2: i32, arg3: i32) -> i32 {
// CHECK-LABEL: define{{.*}}baz
// CHECK-SAME: {{.*}}![[TYPE3:[0-9]+]]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,23 @@

#![crate_type="lib"]

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

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

#[inline(never)]
pub fn baz(f: fn(i32, i32, i32) -> i32, arg1: i32, arg2: i32, arg3: i32) -> i32 {
// CHECK-LABEL: define{{.*}}baz
// CHECK-SAME: {{.*}}!type ![[TYPE3:[0-9]+]] !type !{{[0-9]+}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,23 @@

#![crate_type="lib"]

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

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

#[inline(never)]
pub fn baz(f: fn(i32, i32, i32) -> i32, arg1: i32, arg2: i32, arg3: i32) -> i32 {
// CHECK-LABEL: define{{.*}}baz
// CHECK-SAME: {{.*}}!type ![[TYPE3:[0-9]+]] !type !{{[0-9]+}} !type !{{[0-9]+}} !type !{{[0-9]+}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,23 @@ trait Copy { }

impl Copy for i32 {}

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

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

#[inline(never)]
pub fn baz(f: fn(i32, i32, i32) -> i32, arg1: i32, arg2: i32, arg3: i32) -> i32 {
// CHECK-LABEL: define{{.*}}baz
// CHECK-SAME: {{.*}}!{{<unknown kind #36>|kcfi_type}} ![[TYPE3:[0-9]+]]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,23 @@ trait Copy { }

impl Copy for i32 {}

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

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

#[inline(never)]
pub fn baz(f: fn(i32, i32, i32) -> i32, arg1: i32, arg2: i32, arg3: i32) -> i32 {
// CHECK-LABEL: define{{.*}}baz
// CHECK-SAME: {{.*}}!{{<unknown kind #36>|kcfi_type}} ![[TYPE3:[0-9]+]]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,23 @@ trait Copy { }

impl Copy for i32 {}

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

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

#[inline(never)]
pub fn baz(f: fn(i32, i32, i32) -> i32, arg1: i32, arg2: i32, arg3: i32) -> i32 {
// CHECK-LABEL: define{{.*}}baz
// CHECK-SAME: {{.*}}!{{<unknown kind #36>|kcfi_type}} ![[TYPE3:[0-9]+]]
Expand Down
1 change: 1 addition & 0 deletions tests/run-make/optimization-remarks-dir/foo.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#[inline(never)]
pub fn bar() {}

#[inline(never)]
pub fn foo() {
bar();
}