Skip to content

Commit 4db3d12

Browse files
committed
Auto merge of rust-lang#128265 - DianQK:instsimplify-before-inline, r=saethlin
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 56c698c + ae681c9 commit 4db3d12

File tree

83 files changed

+226
-182
lines changed

Some content is hidden

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

83 files changed

+226
-182
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_target::spec::abi::Abi;
1313
use crate::simplify::simplify_duplicate_switch_targets;
1414
use crate::take_array;
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
@@ -571,6 +571,8 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
571571
// Has to be done before inlining, otherwise actual call will be almost always inlined.
572572
// Also simple, so can just do first
573573
&lower_slice_len::LowerSliceLenCalls,
574+
// Perform instsimplify before inline to eliminate some trivial calls (like clone shims).
575+
&instsimplify::InstSimplify::BeforeInline,
574576
// Perform inlining, which may add a lot of code.
575577
&inline::Inline,
576578
// Code from other crates may have storage markers, so this needs to happen after inlining.
@@ -590,7 +592,8 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
590592
&match_branches::MatchBranchSimplification,
591593
// inst combine is after MatchBranchSimplification to clean up Ne(_1, false)
592594
&multiple_return_terminators::MultipleReturnTerminators,
593-
&instsimplify::InstSimplify,
595+
// After simplifycfg, it allows us to discover new opportunities for peephole optimizations.
596+
&instsimplify::InstSimplify::AfterSimplifyCfg,
594597
&simplify::SimplifyLocals::BeforeConstProp,
595598
&dead_store_elimination::DeadStoreElimination::Initial,
596599
&gvn::GVN,

compiler/rustc_mir_transform/src/shim.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ fn make_shim<'tcx>(tcx: TyCtxt<'tcx>, instance: ty::InstanceKind<'tcx>) -> Body<
155155
&deref_separator::Derefer,
156156
&remove_noop_landing_pads::RemoveNoopLandingPads,
157157
&simplify::SimplifyCfg::MakeShim,
158-
&instsimplify::InstSimplify,
158+
&instsimplify::InstSimplify::BeforeInline,
159159
&abort_unwinding_calls::AbortUnwindingCalls,
160160
&add_call_guards::CriticalCallEdges,
161161
],

tests/incremental/hashes/call_expressions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ pub fn change_to_ufcs() {
162162
}
163163

164164
#[cfg(not(any(cfail1,cfail4)))]
165-
#[rustc_clean(cfg="cfail2", except="opt_hir_owner_nodes,typeck")]
165+
#[rustc_clean(cfg="cfail2", except="opt_hir_owner_nodes,optimized_mir,typeck")]
166166
#[rustc_clean(cfg="cfail3")]
167167
#[rustc_clean(cfg="cfail5", except="opt_hir_owner_nodes,optimized_mir,typeck")]
168168
#[rustc_clean(cfg="cfail6")]

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-after-simplifycfg -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.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// EMIT_MIR_FOR_EACH_PANIC_STRATEGY
22
//@ test-mir-pass: DataflowConstProp
3-
//@ compile-flags: -Zmir-enable-passes=+InstSimplify
3+
//@ compile-flags: -Zmir-enable-passes=+InstSimplify-after-simplifycfg
44
// EMIT_MIR_FOR_EACH_BIT_WIDTH
55

66
// EMIT_MIR slice_len.main.DataflowConstProp.diff

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));

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

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

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

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

0 commit comments

Comments
 (0)