Skip to content

Commit 3689ca8

Browse files
committed
Auto merge of #128265 - DianQK:instsimplify-before-inline, r=<try>
Perform instsimplify before inline to eliminate some trivial calls I am currently working on #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

compiler/rustc_mir_transform/src/instsimplify.rs

Lines changed: 17 additions & 1 deletion
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
}

compiler/rustc_mir_transform/src/lib.rs

Lines changed: 4 additions & 1 deletion
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,

compiler/rustc_mir_transform/src/shim.rs

Lines changed: 1 addition & 1 deletion
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
],

tests/mir-opt/const_prop/slice_len.rs

Lines changed: 1 addition & 1 deletion
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

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

Lines changed: 2 additions & 2 deletions
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);

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

Lines changed: 2 additions & 2 deletions
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);

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

Lines changed: 2 additions & 2 deletions
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);

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

Lines changed: 2 additions & 2 deletions
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);

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

Lines changed: 2 additions & 2 deletions
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));

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

Lines changed: 2 additions & 2 deletions
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));

0 commit comments

Comments
 (0)