Skip to content

Commit 3689ca8

Browse files
committed
Auto merge of rust-lang#128265 - DianQK:instsimplify-before-inline, r=<try>
Perform instsimplify before inline to eliminate some trivial calls I am currently working on rust-lang#128081. In the current pipeline, we can get the following clone statements ([godbolt](https://rust.godbolt.org/z/931316fhP)): ``` bb0: { StorageLive(_2); _2 = ((*_1).0: i32); StorageLive(_3); _3 = ((*_1).1: u64); _0 = Foo { a: move _2, b: move _3 }; StorageDead(_3); StorageDead(_2); return; } ``` Analyzing such statements will be simple and fast. We don't need to consider branches or some interfering statements. However, this requires us to run `InstSimplify`, `ReferencePropagation`, and `SimplifyCFG` at least once. I can introduce a new pass, but I think the best place for it would be within `InstSimplify`. I put `InstSimplify` before `Inline`, which takes some of the burden away from `Inline`. r? `@saethlin`
2 parents a526d7c + 5dc8f5f commit 3689ca8

File tree

85 files changed

+231
-186
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+231
-186
lines changed

Diff for: compiler/rustc_mir_transform/src/instsimplify.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,25 @@ use rustc_span::sym;
1313
use rustc_span::symbol::Symbol;
1414
use rustc_target::spec::abi::Abi;
1515

16-
pub struct InstSimplify;
16+
pub enum InstSimplify {
17+
BeforeInline,
18+
AfterSimplifyCfg,
19+
}
20+
21+
impl InstSimplify {
22+
pub fn name(&self) -> &'static str {
23+
match self {
24+
InstSimplify::BeforeInline => "InstSimplify-before-inline",
25+
InstSimplify::AfterSimplifyCfg => "InstSimplify-after-simplifycfg",
26+
}
27+
}
28+
}
1729

1830
impl<'tcx> MirPass<'tcx> for InstSimplify {
31+
fn name(&self) -> &'static str {
32+
self.name()
33+
}
34+
1935
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
2036
sess.mir_opt_level() > 0
2137
}

Diff for: compiler/rustc_mir_transform/src/lib.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,8 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
572572
// Has to be done before inlining, otherwise actual call will be almost always inlined.
573573
// Also simple, so can just do first
574574
&lower_slice_len::LowerSliceLenCalls,
575+
// Perform instsimplify before inline to eliminate some trivial calls (like clone).
576+
&instsimplify::InstSimplify::BeforeInline,
575577
// Perform inlining, which may add a lot of code.
576578
&inline::Inline,
577579
// Code from other crates may have storage markers, so this needs to happen after inlining.
@@ -591,7 +593,8 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
591593
&match_branches::MatchBranchSimplification,
592594
// inst combine is after MatchBranchSimplification to clean up Ne(_1, false)
593595
&multiple_return_terminators::MultipleReturnTerminators,
594-
&instsimplify::InstSimplify,
596+
// After simplifycfg, it allows us to discover new opportunities for peephole optimizations.
597+
&instsimplify::InstSimplify::AfterSimplifyCfg,
595598
&simplify::SimplifyLocals::BeforeConstProp,
596599
&dead_store_elimination::DeadStoreElimination::Initial,
597600
&gvn::GVN,

Diff for: compiler/rustc_mir_transform/src/shim.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ fn make_shim<'tcx>(tcx: TyCtxt<'tcx>, instance: ty::InstanceKind<'tcx>) -> Body<
154154
&deref_separator::Derefer,
155155
&remove_noop_landing_pads::RemoveNoopLandingPads,
156156
&simplify::SimplifyCfg::MakeShim,
157-
&instsimplify::InstSimplify,
157+
&instsimplify::InstSimplify::BeforeInline,
158158
&abort_unwinding_calls::AbortUnwindingCalls,
159159
&add_call_guards::CriticalCallEdges,
160160
],

Diff for: tests/mir-opt/const_prop/slice_len.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//@ test-mir-pass: GVN
2-
//@ compile-flags: -Zmir-enable-passes=+InstSimplify -Zdump-mir-exclude-alloc-bytes
2+
//@ compile-flags: -Zmir-enable-passes=+InstSimplify-before-inline -Zdump-mir-exclude-alloc-bytes
33
// EMIT_MIR_FOR_EACH_PANIC_STRATEGY
44
// EMIT_MIR_FOR_EACH_BIT_WIDTH
55

Diff for: tests/mir-opt/dataflow-const-prop/slice_len.main.DataflowConstProp.32bit.panic-abort.diff

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
StorageLive(_3);
3131
StorageLive(_4);
3232
_14 = const main::promoted[0];
33-
_4 = _14;
34-
_3 = _4;
33+
_4 = &(*_14);
34+
_3 = &(*_4);
3535
_2 = move _3 as &[u32] (PointerCoercion(Unsize));
3636
StorageDead(_3);
3737
StorageLive(_6);

Diff for: tests/mir-opt/dataflow-const-prop/slice_len.main.DataflowConstProp.32bit.panic-unwind.diff

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
StorageLive(_3);
3131
StorageLive(_4);
3232
_14 = const main::promoted[0];
33-
_4 = _14;
34-
_3 = _4;
33+
_4 = &(*_14);
34+
_3 = &(*_4);
3535
_2 = move _3 as &[u32] (PointerCoercion(Unsize));
3636
StorageDead(_3);
3737
StorageLive(_6);

Diff for: tests/mir-opt/dataflow-const-prop/slice_len.main.DataflowConstProp.64bit.panic-abort.diff

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
StorageLive(_3);
3131
StorageLive(_4);
3232
_14 = const main::promoted[0];
33-
_4 = _14;
34-
_3 = _4;
33+
_4 = &(*_14);
34+
_3 = &(*_4);
3535
_2 = move _3 as &[u32] (PointerCoercion(Unsize));
3636
StorageDead(_3);
3737
StorageLive(_6);

Diff for: tests/mir-opt/dataflow-const-prop/slice_len.main.DataflowConstProp.64bit.panic-unwind.diff

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
StorageLive(_3);
3131
StorageLive(_4);
3232
_14 = const main::promoted[0];
33-
_4 = _14;
34-
_3 = _4;
33+
_4 = &(*_14);
34+
_3 = &(*_4);
3535
_2 = move _3 as &[u32] (PointerCoercion(Unsize));
3636
StorageDead(_3);
3737
StorageLive(_6);

Diff for: tests/mir-opt/inline/dyn_trait.get_query.Inline.panic-abort.diff

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@
2121
bb0: {
2222
StorageLive(_2);
2323
StorageLive(_3);
24-
_3 = &(*_1);
24+
_3 = _1;
2525
_2 = <Q as Query>::cache::<T>(move _3) -> [return: bb1, unwind unreachable];
2626
}
2727

2828
bb1: {
2929
StorageDead(_3);
3030
StorageLive(_4);
31-
_4 = &(*_2);
31+
_4 = _2;
3232
- _0 = try_execute_query::<<Q as Query>::C>(move _4) -> [return: bb2, unwind unreachable];
3333
+ StorageLive(_5);
3434
+ _5 = _4 as &dyn Cache<V = <Q as Query>::V> (PointerCoercion(Unsize));

Diff for: tests/mir-opt/inline/dyn_trait.get_query.Inline.panic-unwind.diff

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@
2121
bb0: {
2222
StorageLive(_2);
2323
StorageLive(_3);
24-
_3 = &(*_1);
24+
_3 = _1;
2525
_2 = <Q as Query>::cache::<T>(move _3) -> [return: bb1, unwind continue];
2626
}
2727

2828
bb1: {
2929
StorageDead(_3);
3030
StorageLive(_4);
31-
_4 = &(*_2);
31+
_4 = _2;
3232
- _0 = try_execute_query::<<Q as Query>::C>(move _4) -> [return: bb2, unwind continue];
3333
+ StorageLive(_5);
3434
+ _5 = _4 as &dyn Cache<V = <Q as Query>::V> (PointerCoercion(Unsize));

Diff for: tests/mir-opt/inline/dyn_trait.mk_cycle.Inline.panic-abort.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
bb0: {
1010
StorageLive(_2);
11-
_2 = &(*_1);
11+
_2 = _1;
1212
_0 = <dyn Cache<V = V> as Cache>::store_nocache(move _2) -> [return: bb1, unwind unreachable];
1313
}
1414

Diff for: tests/mir-opt/inline/dyn_trait.mk_cycle.Inline.panic-unwind.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
bb0: {
1010
StorageLive(_2);
11-
_2 = &(*_1);
11+
_2 = _1;
1212
_0 = <dyn Cache<V = V> as Cache>::store_nocache(move _2) -> [return: bb1, unwind continue];
1313
}
1414

Diff for: tests/mir-opt/inline/dyn_trait.try_execute_query.Inline.panic-abort.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
bb0: {
1414
StorageLive(_2);
1515
StorageLive(_3);
16-
_3 = &(*_1);
16+
_3 = _1;
1717
_2 = move _3 as &dyn Cache<V = <C as Cache>::V> (PointerCoercion(Unsize));
1818
StorageDead(_3);
1919
- _0 = mk_cycle::<<C as Cache>::V>(move _2) -> [return: bb1, unwind unreachable];

Diff for: tests/mir-opt/inline/dyn_trait.try_execute_query.Inline.panic-unwind.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
bb0: {
1414
StorageLive(_2);
1515
StorageLive(_3);
16-
_3 = &(*_1);
16+
_3 = _1;
1717
_2 = move _3 as &dyn Cache<V = <C as Cache>::V> (PointerCoercion(Unsize));
1818
StorageDead(_3);
1919
- _0 = mk_cycle::<<C as Cache>::V>(move _2) -> [return: bb1, unwind continue];

Diff for: tests/mir-opt/inline/inline_closure_borrows_arg.foo.Inline.after.mir

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ fn foo(_1: T, _2: &i32) -> i32 {
2626
_4 = &_3;
2727
StorageLive(_5);
2828
StorageLive(_6);
29-
_6 = &(*_2);
29+
_6 = _2;
3030
StorageLive(_7);
31-
_7 = &(*_2);
31+
_7 = _2;
3232
_5 = (move _6, move _7);
3333
StorageLive(_8);
3434
_8 = move (_5.0: &i32);

Diff for: tests/mir-opt/inline/inline_retag.bar.Inline.after.mir

+4-4
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ fn bar() -> bool {
3131
StorageLive(_4);
3232
_10 = const bar::promoted[1];
3333
Retag(_10);
34-
_4 = &(*_10);
35-
_3 = &(*_4);
34+
_4 = _10;
35+
_3 = _4;
3636
StorageLive(_6);
3737
StorageLive(_7);
3838
_9 = const bar::promoted[0];
3939
Retag(_9);
40-
_7 = &(*_9);
41-
_6 = &(*_7);
40+
_7 = _9;
41+
_6 = _7;
4242
Retag(_3);
4343
Retag(_6);
4444
StorageLive(_11);

Diff for: tests/mir-opt/inline/inline_trait_method.test.Inline.after.panic-abort.mir

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ fn test(_1: &dyn X) -> u32 {
77

88
bb0: {
99
StorageLive(_2);
10-
_2 = &(*_1);
10+
_2 = _1;
1111
_0 = <dyn X as X>::y(move _2) -> [return: bb1, unwind unreachable];
1212
}
1313

Diff for: tests/mir-opt/inline/inline_trait_method.test.Inline.after.panic-unwind.mir

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ fn test(_1: &dyn X) -> u32 {
77

88
bb0: {
99
StorageLive(_2);
10-
_2 = &(*_1);
10+
_2 = _1;
1111
_0 = <dyn X as X>::y(move _2) -> [return: bb1, unwind continue];
1212
}
1313

Diff for: tests/mir-opt/inline/inline_trait_method_2.test2.Inline.after.panic-abort.mir

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ fn test2(_1: &dyn X) -> bool {
1212
bb0: {
1313
StorageLive(_2);
1414
StorageLive(_3);
15-
_3 = &(*_1);
16-
_2 = move _3 as &dyn X (PointerCoercion(Unsize));
15+
_3 = _1;
16+
_2 = move _3;
1717
StorageDead(_3);
1818
_0 = <dyn X as X>::y(move _2) -> [return: bb1, unwind unreachable];
1919
}

Diff for: tests/mir-opt/inline/inline_trait_method_2.test2.Inline.after.panic-unwind.mir

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ fn test2(_1: &dyn X) -> bool {
1212
bb0: {
1313
StorageLive(_2);
1414
StorageLive(_3);
15-
_3 = &(*_1);
16-
_2 = move _3 as &dyn X (PointerCoercion(Unsize));
15+
_3 = _1;
16+
_2 = move _3;
1717
StorageDead(_3);
1818
_0 = <dyn X as X>::y(move _2) -> [return: bb1, unwind continue];
1919
}

Diff for: tests/mir-opt/inline/issue_58867_inline_as_ref_as_mut.a.Inline.after.mir

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ fn a(_1: &mut [T]) -> &mut [T] {
1414
StorageLive(_2);
1515
StorageLive(_3);
1616
StorageLive(_4);
17-
_4 = &mut (*_1);
17+
_4 = _1;
1818
_3 = _4;
19-
_2 = &mut (*_3);
19+
_2 = _3;
2020
StorageDead(_4);
21-
_0 = &mut (*_2);
21+
_0 = _2;
2222
StorageDead(_3);
2323
StorageDead(_2);
2424
return;

Diff for: tests/mir-opt/inline/issue_58867_inline_as_ref_as_mut.b.Inline.after.mir

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@ fn b(_1: &mut Box<T>) -> &mut T {
1616
StorageLive(_2);
1717
StorageLive(_3);
1818
StorageLive(_4);
19-
_4 = &mut (*_1);
19+
_4 = _1;
2020
StorageLive(_5);
2121
StorageLive(_6);
2222
_5 = (*_4);
2323
_6 = (((_5.0: std::ptr::Unique<T>).0: std::ptr::NonNull<T>).0: *const T);
2424
_3 = &mut (*_6);
2525
StorageDead(_6);
2626
StorageDead(_5);
27-
_2 = &mut (*_3);
27+
_2 = _3;
2828
StorageDead(_4);
29-
_0 = &mut (*_2);
29+
_0 = _2;
3030
StorageDead(_3);
3131
StorageDead(_2);
3232
return;

Diff for: tests/mir-opt/inline/issue_58867_inline_as_ref_as_mut.c.Inline.after.mir

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ fn c(_1: &[T]) -> &[T] {
1212
bb0: {
1313
StorageLive(_2);
1414
StorageLive(_3);
15-
_3 = &(*_1);
15+
_3 = _1;
1616
_2 = _3;
17-
_0 = &(*_2);
17+
_0 = _2;
1818
StorageDead(_3);
1919
StorageDead(_2);
2020
return;

Diff for: tests/mir-opt/inline/issue_58867_inline_as_ref_as_mut.d.Inline.after.mir

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ fn d(_1: &Box<T>) -> &T {
1414
bb0: {
1515
StorageLive(_2);
1616
StorageLive(_3);
17-
_3 = &(*_1);
17+
_3 = _1;
1818
StorageLive(_4);
1919
StorageLive(_5);
2020
_4 = (*_3);
2121
_5 = (((_4.0: std::ptr::Unique<T>).0: std::ptr::NonNull<T>).0: *const T);
2222
_2 = &(*_5);
2323
StorageDead(_5);
2424
StorageDead(_4);
25-
_0 = &(*_2);
25+
_0 = _2;
2626
StorageDead(_3);
2727
StorageDead(_2);
2828
return;

Diff for: tests/mir-opt/instsimplify/bool_compare.eq_false.InstSimplify.diff renamed to tests/mir-opt/instsimplify/bool_compare.eq_false.InstSimplify-before-inline.diff

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
- // MIR for `eq_false` before InstSimplify
2-
+ // MIR for `eq_false` after InstSimplify
1+
- // MIR for `eq_false` before InstSimplify-before-inline
2+
+ // MIR for `eq_false` after InstSimplify-before-inline
33

44
fn eq_false(_1: bool) -> u32 {
55
debug x => _1;

Diff for: tests/mir-opt/instsimplify/bool_compare.eq_true.InstSimplify.diff renamed to tests/mir-opt/instsimplify/bool_compare.eq_true.InstSimplify-before-inline.diff

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
- // MIR for `eq_true` before InstSimplify
2-
+ // MIR for `eq_true` after InstSimplify
1+
- // MIR for `eq_true` before InstSimplify-before-inline
2+
+ // MIR for `eq_true` after InstSimplify-before-inline
33

44
fn eq_true(_1: bool) -> u32 {
55
debug x => _1;

Diff for: tests/mir-opt/instsimplify/bool_compare.false_eq.InstSimplify.diff renamed to tests/mir-opt/instsimplify/bool_compare.false_eq.InstSimplify-before-inline.diff

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
- // MIR for `false_eq` before InstSimplify
2-
+ // MIR for `false_eq` after InstSimplify
1+
- // MIR for `false_eq` before InstSimplify-before-inline
2+
+ // MIR for `false_eq` after InstSimplify-before-inline
33

44
fn false_eq(_1: bool) -> u32 {
55
debug x => _1;

Diff for: tests/mir-opt/instsimplify/bool_compare.false_ne.InstSimplify.diff renamed to tests/mir-opt/instsimplify/bool_compare.false_ne.InstSimplify-before-inline.diff

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
- // MIR for `false_ne` before InstSimplify
2-
+ // MIR for `false_ne` after InstSimplify
1+
- // MIR for `false_ne` before InstSimplify-before-inline
2+
+ // MIR for `false_ne` after InstSimplify-before-inline
33

44
fn false_ne(_1: bool) -> u32 {
55
debug x => _1;

Diff for: tests/mir-opt/instsimplify/bool_compare.ne_false.InstSimplify.diff renamed to tests/mir-opt/instsimplify/bool_compare.ne_false.InstSimplify-before-inline.diff

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
- // MIR for `ne_false` before InstSimplify
2-
+ // MIR for `ne_false` after InstSimplify
1+
- // MIR for `ne_false` before InstSimplify-before-inline
2+
+ // MIR for `ne_false` after InstSimplify-before-inline
33

44
fn ne_false(_1: bool) -> u32 {
55
debug x => _1;

Diff for: tests/mir-opt/instsimplify/bool_compare.ne_true.InstSimplify.diff renamed to tests/mir-opt/instsimplify/bool_compare.ne_true.InstSimplify-before-inline.diff

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
- // MIR for `ne_true` before InstSimplify
2-
+ // MIR for `ne_true` after InstSimplify
1+
- // MIR for `ne_true` before InstSimplify-before-inline
2+
+ // MIR for `ne_true` after InstSimplify-before-inline
33

44
fn ne_true(_1: bool) -> u32 {
55
debug x => _1;

0 commit comments

Comments
 (0)