Skip to content
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

Only unpack tupled args in inliner if we expect args to be unpacked #110833

Merged
merged 4 commits into from
Aug 4, 2023
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Explicitly don't inline user-written rust-call fns
compiler-errors committed Aug 3, 2023
commit 3c9549b349a2168182035e7a7ae0022d36d63e43
9 changes: 8 additions & 1 deletion compiler/rustc_mir_transform/src/inline.rs
Original file line number Diff line number Diff line change
@@ -222,7 +222,14 @@ impl<'tcx> Inliner<'tcx> {
trace!(?output_type, ?destination_ty);
return Err("failed to normalize return type");
}
if callsite.fn_sig.abi() == Abi::RustCall && callee_body.spread_arg.is_none() {
if callsite.fn_sig.abi() == Abi::RustCall {
// FIXME: Don't inline user-written `extern "rust-call"` functions,
// since this is generally perf-negative on rustc, and we hope that
// LLVM will inline these functions instead.
if callee_body.spread_arg.is_some() {
return Err("do not inline user-written rust-call functions");
}

let (self_arg, arg_tuple) = match &args[..] {
[arg_tuple] => (None, arg_tuple),
[self_arg, arg_tuple] => (Some(self_arg), arg_tuple),
8 changes: 1 addition & 7 deletions tests/mir-opt/inline/cycle.g.Inline.panic-abort.diff
Original file line number Diff line number Diff line change
@@ -5,13 +5,10 @@
let mut _0: ();
let _1: ();
+ let mut _2: fn() {main};
+ let mut _5: ();
+ scope 1 (inlined f::<fn() {main}>) {
+ debug g => _2;
+ let mut _3: &fn() {main};
+ let _4: ();
+ scope 2 (inlined <fn() {main} as Fn<()>>::call - shim(fn() {main})) {
+ }
+ }

bb0: {
@@ -22,9 +19,7 @@
+ StorageLive(_4);
+ StorageLive(_3);
+ _3 = &_2;
+ StorageLive(_5);
+ _5 = const ();
+ _4 = move (*_3)() -> [return: bb2, unwind unreachable];
+ _4 = <fn() {main} as Fn<()>>::call(move _3, const ()) -> [return: bb2, unwind unreachable];
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

huh, I guess this also has the side-effect of not inlining fndef call shims

}

bb1: {
@@ -36,7 +31,6 @@
+ }
+
+ bb2: {
+ StorageDead(_5);
+ StorageDead(_3);
+ drop(_2) -> [return: bb1, unwind unreachable];
}
20 changes: 7 additions & 13 deletions tests/mir-opt/inline/cycle.g.Inline.panic-unwind.diff
Original file line number Diff line number Diff line change
@@ -5,13 +5,10 @@
let mut _0: ();
let _1: ();
+ let mut _2: fn() {main};
+ let mut _5: ();
+ scope 1 (inlined f::<fn() {main}>) {
+ debug g => _2;
+ let mut _3: &fn() {main};
+ let _4: ();
+ scope 2 (inlined <fn() {main} as Fn<()>>::call - shim(fn() {main})) {
+ }
+ }

bb0: {
@@ -22,9 +19,7 @@
+ StorageLive(_4);
+ StorageLive(_3);
+ _3 = &_2;
+ StorageLive(_5);
+ _5 = const ();
+ _4 = move (*_3)() -> [return: bb4, unwind: bb2];
+ _4 = <fn() {main} as Fn<()>>::call(move _3, const ()) -> [return: bb2, unwind: bb3];
}

bb1: {
@@ -35,18 +30,17 @@
return;
+ }
+
+ bb2 (cleanup): {
+ drop(_2) -> [return: bb3, unwind terminate];
+ bb2: {
+ StorageDead(_3);
+ drop(_2) -> [return: bb1, unwind continue];
+ }
+
+ bb3 (cleanup): {
+ resume;
+ drop(_2) -> [return: bb4, unwind terminate];
+ }
+
+ bb4: {
+ StorageDead(_5);
+ StorageDead(_3);
+ drop(_2) -> [return: bb1, unwind continue];
+ bb4 (cleanup): {
+ resume;
}
}

18 changes: 1 addition & 17 deletions tests/mir-opt/inline/cycle.main.Inline.panic-abort.diff
Original file line number Diff line number Diff line change
@@ -5,21 +5,10 @@
let mut _0: ();
let _1: ();
+ let mut _2: fn() {g};
+ let mut _5: ();
+ scope 1 (inlined f::<fn() {g}>) {
+ debug g => _2;
+ let mut _3: &fn() {g};
+ let _4: ();
+ scope 2 (inlined <fn() {g} as Fn<()>>::call - shim(fn() {g})) {
+ scope 3 (inlined g) {
+ scope 4 (inlined f::<fn() {main}>) {
+ debug g => main;
+ let _6: ();
+ scope 5 (inlined <fn() {main} as Fn<()>>::call - shim(fn() {main})) {
+ }
+ }
+ }
+ }
+ }

bb0: {
@@ -30,10 +19,7 @@
+ StorageLive(_4);
+ StorageLive(_3);
+ _3 = &_2;
+ StorageLive(_5);
+ _5 = const ();
+ StorageLive(_6);
+ _6 = main() -> [return: bb2, unwind unreachable];
+ _4 = <fn() {g} as Fn<()>>::call(move _3, const ()) -> [return: bb2, unwind unreachable];
}

bb1: {
@@ -45,8 +31,6 @@
+ }
+
+ bb2: {
+ StorageDead(_6);
+ StorageDead(_5);
+ StorageDead(_3);
+ drop(_2) -> [return: bb1, unwind unreachable];
}
30 changes: 7 additions & 23 deletions tests/mir-opt/inline/cycle.main.Inline.panic-unwind.diff
Original file line number Diff line number Diff line change
@@ -5,21 +5,10 @@
let mut _0: ();
let _1: ();
+ let mut _2: fn() {g};
+ let mut _5: ();
+ scope 1 (inlined f::<fn() {g}>) {
+ debug g => _2;
+ let mut _3: &fn() {g};
+ let _4: ();
+ scope 2 (inlined <fn() {g} as Fn<()>>::call - shim(fn() {g})) {
+ scope 3 (inlined g) {
+ scope 4 (inlined f::<fn() {main}>) {
+ debug g => main;
+ let _6: ();
+ scope 5 (inlined <fn() {main} as Fn<()>>::call - shim(fn() {main})) {
+ }
+ }
+ }
+ }
+ }

bb0: {
@@ -30,10 +19,7 @@
+ StorageLive(_4);
+ StorageLive(_3);
+ _3 = &_2;
+ StorageLive(_5);
+ _5 = const ();
+ StorageLive(_6);
+ _6 = main() -> [return: bb4, unwind: bb2];
+ _4 = <fn() {g} as Fn<()>>::call(move _3, const ()) -> [return: bb2, unwind: bb3];
}

bb1: {
@@ -44,19 +30,17 @@
return;
+ }
+
+ bb2 (cleanup): {
+ drop(_2) -> [return: bb3, unwind terminate];
+ bb2: {
+ StorageDead(_3);
+ drop(_2) -> [return: bb1, unwind continue];
+ }
+
+ bb3 (cleanup): {
+ resume;
+ drop(_2) -> [return: bb4, unwind terminate];
+ }
+
+ bb4: {
+ StorageDead(_6);
+ StorageDead(_5);
+ StorageDead(_3);
+ drop(_2) -> [return: bb1, unwind continue];
+ bb4 (cleanup): {
+ resume;
}
}

Original file line number Diff line number Diff line change
@@ -7,40 +7,23 @@
let mut _0: ();
let mut _3: &mut std::boxed::Box<dyn std::ops::FnMut<I, Output = ()>>;
let mut _4: I;
+ scope 1 (inlined <Box<dyn FnMut<I, Output = ()>> as FnMut<I>>::call_mut) {
+ debug self => _3;
+ debug args => _4;
+ let mut _5: &mut dyn std::ops::FnMut<I, Output = ()>;
+ let mut _6: std::boxed::Box<dyn std::ops::FnMut<I, Output = ()>>;
+ let mut _7: *const dyn std::ops::FnMut<I, Output = ()>;
+ }

bb0: {
StorageLive(_3);
_3 = &mut _1;
StorageLive(_4);
_4 = move _2;
- _0 = <Box<dyn FnMut<I, Output = ()>> as FnMut<I>>::call_mut(move _3, move _4) -> [return: bb1, unwind unreachable];
+ StorageLive(_5);
+ _6 = deref_copy (*_3);
+ _7 = (((_6.0: std::ptr::Unique<dyn std::ops::FnMut<I, Output = ()>>).0: std::ptr::NonNull<dyn std::ops::FnMut<I, Output = ()>>).0: *const dyn std::ops::FnMut<I, Output = ()>);
+ _5 = &mut (*_7);
+ _0 = <dyn FnMut<I, Output = ()> as FnMut<I>>::call_mut(move _5, move _4) -> [return: bb2, unwind unreachable];
_0 = <Box<dyn FnMut<I, Output = ()>> as FnMut<I>>::call_mut(move _3, move _4) -> [return: bb1, unwind unreachable];
}

bb1: {
- StorageDead(_4);
- StorageDead(_3);
- drop(_1) -> [return: bb2, unwind unreachable];
+ return;
StorageDead(_4);
StorageDead(_3);
drop(_1) -> [return: bb2, unwind unreachable];
}

bb2: {
- return;
+ StorageDead(_5);
+ StorageDead(_4);
+ StorageDead(_3);
+ drop(_1) -> [return: bb1, unwind unreachable];
return;
}
}

30 changes: 6 additions & 24 deletions tests/mir-opt/inline/inline_box_fn.call.Inline.diff
Original file line number Diff line number Diff line change
@@ -7,44 +7,26 @@
let _2: ();
let mut _3: &std::boxed::Box<dyn std::ops::Fn(i32)>;
let mut _4: (i32,);
+ scope 1 (inlined <Box<dyn Fn(i32)> as Fn<(i32,)>>::call) {
+ debug self => _3;
+ debug args => _4;
+ let mut _5: &dyn std::ops::Fn(i32);
+ let mut _6: std::boxed::Box<dyn std::ops::Fn(i32)>;
+ let mut _7: *const dyn std::ops::Fn(i32);
+ }

bb0: {
StorageLive(_2);
StorageLive(_3);
_3 = &_1;
StorageLive(_4);
_4 = (const 1_i32,);
- _2 = <Box<dyn Fn(i32)> as Fn<(i32,)>>::call(move _3, move _4) -> [return: bb1, unwind unreachable];
+ StorageLive(_5);
+ _6 = deref_copy (*_3);
+ _7 = (((_6.0: std::ptr::Unique<dyn std::ops::Fn(i32)>).0: std::ptr::NonNull<dyn std::ops::Fn(i32)>).0: *const dyn std::ops::Fn(i32));
+ _5 = &(*_7);
+ _2 = <dyn Fn(i32) as Fn<(i32,)>>::call(move _5, move _4) -> [return: bb2, unwind unreachable];
_2 = <Box<dyn Fn(i32)> as Fn<(i32,)>>::call(move _3, move _4) -> [return: bb1, unwind unreachable];
}

bb1: {
+ return;
+ }
+
+ bb2: {
+ StorageDead(_5);
StorageDead(_4);
StorageDead(_3);
StorageDead(_2);
_0 = const ();
- drop(_1) -> [return: bb2, unwind unreachable];
- }
-
- bb2: {
- return;
+ drop(_1) -> [return: bb1, unwind unreachable];
drop(_1) -> [return: bb2, unwind unreachable];
}

bb2: {
return;
}
}

18 changes: 1 addition & 17 deletions tests/mir-opt/inline/inline_cycle.two.Inline.panic-abort.diff
Original file line number Diff line number Diff line change
@@ -5,20 +5,9 @@
let mut _0: ();
let _1: ();
+ let mut _2: fn() {f};
+ let mut _4: ();
+ scope 1 (inlined call::<fn() {f}>) {
+ debug f => _2;
+ let _3: ();
+ scope 2 (inlined <fn() {f} as FnOnce<()>>::call_once - shim(fn() {f})) {
+ scope 3 (inlined f) {
+ scope 4 (inlined call::<fn() {f}>) {
+ debug f => f;
+ let _5: ();
+ scope 5 (inlined <fn() {f} as FnOnce<()>>::call_once - shim(fn() {f})) {
+ }
+ }
+ }
+ }
+ }

bb0: {
@@ -27,15 +16,10 @@
+ StorageLive(_2);
+ _2 = f;
+ StorageLive(_3);
+ StorageLive(_4);
+ _4 = const ();
+ StorageLive(_5);
+ _5 = f() -> [return: bb1, unwind unreachable];
+ _3 = <fn() {f} as FnOnce<()>>::call_once(move _2, const ()) -> [return: bb1, unwind unreachable];
}

bb1: {
+ StorageDead(_5);
+ StorageDead(_4);
+ StorageDead(_3);
+ StorageDead(_2);
StorageDead(_1);
18 changes: 1 addition & 17 deletions tests/mir-opt/inline/inline_cycle.two.Inline.panic-unwind.diff
Original file line number Diff line number Diff line change
@@ -5,20 +5,9 @@
let mut _0: ();
let _1: ();
+ let mut _2: fn() {f};
+ let mut _4: ();
+ scope 1 (inlined call::<fn() {f}>) {
+ debug f => _2;
+ let _3: ();
+ scope 2 (inlined <fn() {f} as FnOnce<()>>::call_once - shim(fn() {f})) {
+ scope 3 (inlined f) {
+ scope 4 (inlined call::<fn() {f}>) {
+ debug f => f;
+ let _5: ();
+ scope 5 (inlined <fn() {f} as FnOnce<()>>::call_once - shim(fn() {f})) {
+ }
+ }
+ }
+ }
+ }

bb0: {
@@ -27,15 +16,10 @@
+ StorageLive(_2);
+ _2 = f;
+ StorageLive(_3);
+ StorageLive(_4);
+ _4 = const ();
+ StorageLive(_5);
+ _5 = f() -> [return: bb1, unwind continue];
+ _3 = <fn() {f} as FnOnce<()>>::call_once(move _2, const ()) -> [return: bb1, unwind continue];
Copy link
Member Author

@compiler-errors compiler-errors Jun 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this also has the side-effect of not inlining FnDef call shims... but that doesn't affect perf it seems?

}

bb1: {
+ StorageDead(_5);
+ StorageDead(_4);
+ StorageDead(_3);
+ StorageDead(_2);
StorageDead(_1);
Loading