Skip to content

Commit a3b873c

Browse files
committed
Auto merge of rust-lang#115105 - cjgillot:dest-prop-default, r=<try>
Enable DestinationPropagation by default. ~~Based on rust-lang#115291 This PR proposes to enable the destination propagation pass by default. This pass is meant to reduce the amount of copies present in MIR. At the same time, this PR removes the `RenameReturnPlace` pass, as it is currently unsound. `DestinationPropagation` is not limited to `_0`, but does not handle borrowed locals.
2 parents 25f8d01 + dca02d6 commit a3b873c

16 files changed

+320
-580
lines changed

compiler/rustc_mir_transform/src/dest_prop.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ impl<'tcx> MirPass<'tcx> for DestinationPropagation {
159159
// 2. Despite being an overall perf improvement, this still causes a 30% regression in
160160
// keccak. We can temporarily fix this by bounding function size, but in the long term
161161
// we should fix this by being smarter about invalidating analysis results.
162-
sess.mir_opt_level() >= 3
162+
sess.mir_opt_level() >= 2
163163
}
164164

165165
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {

compiler/rustc_mir_transform/src/lib.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ mod lower_slice_len;
9494
mod match_branches;
9595
mod multiple_return_terminators;
9696
mod normalize_array_len;
97-
mod nrvo;
9897
mod prettify;
9998
mod promote_consts;
10099
mod ref_prop;
@@ -609,13 +608,12 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
609608
&jump_threading::JumpThreading,
610609
&early_otherwise_branch::EarlyOtherwiseBranch,
611610
&simplify_comparison_integral::SimplifyComparisonIntegral,
612-
&dest_prop::DestinationPropagation,
613611
&o1(simplify_branches::SimplifyConstCondition::Final),
614612
&o1(remove_noop_landing_pads::RemoveNoopLandingPads),
615613
&o1(simplify::SimplifyCfg::Final),
616614
&copy_prop::CopyProp,
617615
&dead_store_elimination::DeadStoreElimination::Final,
618-
&nrvo::RenameReturnPlace,
616+
&dest_prop::DestinationPropagation,
619617
&simplify::SimplifyLocals::Final,
620618
&multiple_return_terminators::MultipleReturnTerminators,
621619
&deduplicate_blocks::DeduplicateBlocks,

compiler/rustc_mir_transform/src/nrvo.rs

-237
This file was deleted.

tests/mir-opt/nrvo_simple.nrvo.RenameReturnPlace.panic-abort.diff renamed to tests/mir-opt/dest-prop/nrvo_borrowed.nrvo.DestinationPropagation.panic-abort.diff

+16-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
- // MIR for `nrvo` before RenameReturnPlace
2-
+ // MIR for `nrvo` after RenameReturnPlace
1+
- // MIR for `nrvo` before DestinationPropagation
2+
+ // MIR for `nrvo` after DestinationPropagation
33

44
fn nrvo(_1: for<'a> fn(&'a mut [u8; 1024])) -> [u8; 1024] {
55
debug init => _1;
@@ -10,32 +10,33 @@
1010
let mut _5: &mut [u8; 1024];
1111
let mut _6: &mut [u8; 1024];
1212
scope 1 {
13-
- debug buf => _2;
14-
+ debug buf => _0;
13+
debug buf => _2;
1514
}
1615

1716
bb0: {
18-
- StorageLive(_2);
19-
- _2 = [const 0_u8; 1024];
20-
+ _0 = [const 0_u8; 1024];
17+
StorageLive(_2);
18+
_2 = [const 0_u8; 1024];
2119
StorageLive(_3);
22-
StorageLive(_4);
23-
_4 = _1;
20+
- StorageLive(_4);
21+
- _4 = _1;
22+
+ nop;
23+
+ nop;
2424
StorageLive(_5);
2525
StorageLive(_6);
26-
- _6 = &mut _2;
27-
+ _6 = &mut _0;
26+
_6 = &mut _2;
2827
_5 = &mut (*_6);
29-
_3 = move _4(move _5) -> [return: bb1, unwind unreachable];
28+
- _3 = move _4(move _5) -> [return: bb1, unwind unreachable];
29+
+ _3 = move _1(move _5) -> [return: bb1, unwind unreachable];
3030
}
3131

3232
bb1: {
3333
StorageDead(_5);
34-
StorageDead(_4);
34+
- StorageDead(_4);
35+
+ nop;
3536
StorageDead(_6);
3637
StorageDead(_3);
37-
- _0 = _2;
38-
- StorageDead(_2);
38+
_0 = _2;
39+
StorageDead(_2);
3940
return;
4041
}
4142
}

tests/mir-opt/nrvo_simple.nrvo.RenameReturnPlace.panic-unwind.diff renamed to tests/mir-opt/dest-prop/nrvo_borrowed.nrvo.DestinationPropagation.panic-unwind.diff

+16-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
- // MIR for `nrvo` before RenameReturnPlace
2-
+ // MIR for `nrvo` after RenameReturnPlace
1+
- // MIR for `nrvo` before DestinationPropagation
2+
+ // MIR for `nrvo` after DestinationPropagation
33

44
fn nrvo(_1: for<'a> fn(&'a mut [u8; 1024])) -> [u8; 1024] {
55
debug init => _1;
@@ -10,32 +10,33 @@
1010
let mut _5: &mut [u8; 1024];
1111
let mut _6: &mut [u8; 1024];
1212
scope 1 {
13-
- debug buf => _2;
14-
+ debug buf => _0;
13+
debug buf => _2;
1514
}
1615

1716
bb0: {
18-
- StorageLive(_2);
19-
- _2 = [const 0_u8; 1024];
20-
+ _0 = [const 0_u8; 1024];
17+
StorageLive(_2);
18+
_2 = [const 0_u8; 1024];
2119
StorageLive(_3);
22-
StorageLive(_4);
23-
_4 = _1;
20+
- StorageLive(_4);
21+
- _4 = _1;
22+
+ nop;
23+
+ nop;
2424
StorageLive(_5);
2525
StorageLive(_6);
26-
- _6 = &mut _2;
27-
+ _6 = &mut _0;
26+
_6 = &mut _2;
2827
_5 = &mut (*_6);
29-
_3 = move _4(move _5) -> [return: bb1, unwind continue];
28+
- _3 = move _4(move _5) -> [return: bb1, unwind continue];
29+
+ _3 = move _1(move _5) -> [return: bb1, unwind continue];
3030
}
3131

3232
bb1: {
3333
StorageDead(_5);
34-
StorageDead(_4);
34+
- StorageDead(_4);
35+
+ nop;
3536
StorageDead(_6);
3637
StorageDead(_3);
37-
- _0 = _2;
38-
- StorageDead(_2);
38+
_0 = _2;
39+
StorageDead(_2);
3940
return;
4041
}
4142
}

tests/mir-opt/nrvo_simple.rs renamed to tests/mir-opt/dest-prop/nrvo_borrowed.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// skip-filecheck
22
// EMIT_MIR_FOR_EACH_PANIC_STRATEGY
3-
// unit-test: RenameReturnPlace
3+
// unit-test: DestinationPropagation
44

5-
// EMIT_MIR nrvo_simple.nrvo.RenameReturnPlace.diff
5+
// EMIT_MIR nrvo_borrowed.nrvo.DestinationPropagation.diff
66
fn nrvo(init: fn(&mut [u8; 1024])) -> [u8; 1024] {
77
let mut buf = [0; 1024];
88
init(&mut buf);

tests/mir-opt/dest-prop/union.main.DestinationPropagation.panic-abort.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
bb0: {
2121
StorageLive(_1);
2222
StorageLive(_2);
23-
_2 = const 1_u32;
23+
nop;
2424
_1 = Un { us: const 1_u32 };
2525
StorageDead(_2);
2626
StorageLive(_3);

0 commit comments

Comments
 (0)