Skip to content

Commit 3a6bf35

Browse files
committed
Auto merge of #119677 - cjgillot:early-cfg-opt, r=oli-obk
Reorder early post-inlining passes. `RemoveZsts`, `RemoveUnneededDrops` and `UninhabitedEnumBranching` only depend on types, so they should be executed together early after MIR inlining introduces those types. This does not change the end-result, but this makes the pipeline a bit more consistent.
2 parents 65b323b + a8c4d43 commit 3a6bf35

9 files changed

+27
-44
lines changed

compiler/rustc_mir_transform/src/lib.rs

+17-6
Original file line numberDiff line numberDiff line change
@@ -566,17 +566,28 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
566566
body,
567567
&[
568568
&check_alignment::CheckAlignment,
569-
&lower_slice_len::LowerSliceLenCalls, // has to be done before inlining, otherwise actual call will be almost always inlined. Also simple, so can just do first
569+
// Before inlining: trim down MIR with passes to reduce inlining work.
570+
571+
// Has to be done before inlining, otherwise actual call will be almost always inlined.
572+
// Also simple, so can just do first
573+
&lower_slice_len::LowerSliceLenCalls,
574+
// Perform inlining, which may add a lot of code.
570575
&inline::Inline,
571-
// Substitutions during inlining may introduce switch on enums with uninhabited branches.
576+
// Code from other crates may have storage markers, so this needs to happen after inlining.
577+
&remove_storage_markers::RemoveStorageMarkers,
578+
// Inlining and substitution may introduce ZST and useless drops.
579+
&remove_zsts::RemoveZsts,
580+
&remove_unneeded_drops::RemoveUnneededDrops,
581+
// Type substitution may create uninhabited enums.
572582
&uninhabited_enum_branching::UninhabitedEnumBranching,
573583
&unreachable_prop::UnreachablePropagation,
574584
&o1(simplify::SimplifyCfg::AfterUninhabitedEnumBranching),
575-
&remove_storage_markers::RemoveStorageMarkers,
576-
&remove_zsts::RemoveZsts,
577-
&normalize_array_len::NormalizeArrayLen, // has to run after `slice::len` lowering
585+
// Inlining may have introduced a lot of redundant code and a large move pattern.
586+
// Now, we need to shrink the generated MIR.
587+
588+
// Has to run after `slice::len` lowering
589+
&normalize_array_len::NormalizeArrayLen,
578590
&const_goto::ConstGoto,
579-
&remove_unneeded_drops::RemoveUnneededDrops,
580591
&ref_prop::ReferencePropagation,
581592
&sroa::ScalarReplacementOfAggregates,
582593
&match_branches::MatchBranchSimplification,

tests/mir-opt/pre-codegen/optimizes_into_variable.main.ScalarReplacementOfAggregates.32bit.panic-abort.diff

-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@
6161
+ StorageDead(_10);
6262
+ StorageDead(_11);
6363
+ nop;
64-
nop;
6564
StorageDead(_8);
6665
StorageDead(_3);
6766
StorageDead(_1);

tests/mir-opt/pre-codegen/optimizes_into_variable.main.ScalarReplacementOfAggregates.32bit.panic-unwind.diff

-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@
6161
+ StorageDead(_10);
6262
+ StorageDead(_11);
6363
+ nop;
64-
nop;
6564
StorageDead(_8);
6665
StorageDead(_3);
6766
StorageDead(_1);

tests/mir-opt/pre-codegen/optimizes_into_variable.main.ScalarReplacementOfAggregates.64bit.panic-abort.diff

-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@
6161
+ StorageDead(_10);
6262
+ StorageDead(_11);
6363
+ nop;
64-
nop;
6564
StorageDead(_8);
6665
StorageDead(_3);
6766
StorageDead(_1);

tests/mir-opt/pre-codegen/optimizes_into_variable.main.ScalarReplacementOfAggregates.64bit.panic-unwind.diff

-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@
6161
+ StorageDead(_10);
6262
+ StorageDead(_11);
6363
+ nop;
64-
nop;
6564
StorageDead(_8);
6665
StorageDead(_3);
6766
StorageDead(_1);

tests/mir-opt/simplify_if.main.SimplifyConstCondition-after-const-prop.panic-abort.diff

+2-10
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,15 @@
66
let _1: ();
77

88
bb0: {
9-
- switchInt(const false) -> [0: bb3, otherwise: bb1];
10-
+ goto -> bb3;
9+
- switchInt(const false) -> [0: bb2, otherwise: bb1];
10+
+ goto -> bb2;
1111
}
1212

1313
bb1: {
1414
_1 = noop() -> [return: bb2, unwind unreachable];
1515
}
1616

1717
bb2: {
18-
goto -> bb4;
19-
}
20-
21-
bb3: {
22-
goto -> bb4;
23-
}
24-
25-
bb4: {
2618
return;
2719
}
2820
}

tests/mir-opt/simplify_if.main.SimplifyConstCondition-after-const-prop.panic-unwind.diff

+2-10
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,15 @@
66
let _1: ();
77

88
bb0: {
9-
- switchInt(const false) -> [0: bb3, otherwise: bb1];
10-
+ goto -> bb3;
9+
- switchInt(const false) -> [0: bb2, otherwise: bb1];
10+
+ goto -> bb2;
1111
}
1212

1313
bb1: {
1414
_1 = noop() -> [return: bb2, unwind continue];
1515
}
1616

1717
bb2: {
18-
goto -> bb4;
19-
}
20-
21-
bb3: {
22-
goto -> bb4;
23-
}
24-
25-
bb4: {
2618
return;
2719
}
2820
}

tests/mir-opt/simplify_match.main.GVN.panic-abort.diff

+3-7
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,15 @@
1111

1212
bb0: {
1313
_2 = const false;
14-
- switchInt(_2) -> [0: bb1, otherwise: bb2];
15-
+ switchInt(const false) -> [0: bb1, otherwise: bb2];
14+
- switchInt(_2) -> [0: bb2, otherwise: bb1];
15+
+ switchInt(const false) -> [0: bb2, otherwise: bb1];
1616
}
1717

1818
bb1: {
19-
goto -> bb3;
19+
_0 = noop() -> [return: bb2, unwind unreachable];
2020
}
2121

2222
bb2: {
23-
_0 = noop() -> [return: bb3, unwind unreachable];
24-
}
25-
26-
bb3: {
2723
return;
2824
}
2925
}

tests/mir-opt/simplify_match.main.GVN.panic-unwind.diff

+3-7
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,15 @@
1111

1212
bb0: {
1313
_2 = const false;
14-
- switchInt(_2) -> [0: bb1, otherwise: bb2];
15-
+ switchInt(const false) -> [0: bb1, otherwise: bb2];
14+
- switchInt(_2) -> [0: bb2, otherwise: bb1];
15+
+ switchInt(const false) -> [0: bb2, otherwise: bb1];
1616
}
1717

1818
bb1: {
19-
goto -> bb3;
19+
_0 = noop() -> [return: bb2, unwind continue];
2020
}
2121

2222
bb2: {
23-
_0 = noop() -> [return: bb3, unwind continue];
24-
}
25-
26-
bb3: {
2723
return;
2824
}
2925
}

0 commit comments

Comments
 (0)