Skip to content

Commit 5cfde26

Browse files
committed
Auto merge of #155022 - tmandry:remove-dead-drops, r=<try>
Remove redundant drop terminators ahead of mir_coroutine_witnesses
2 parents 9004856 + 26833b6 commit 5cfde26

40 files changed

Lines changed: 635 additions & 896 deletions

File tree

compiler/rustc_middle/src/queries.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,11 @@ rustc_queries! {
672672
desc { "promoting constants in MIR for `{}`", tcx.def_path_str(key) }
673673
}
674674

675+
query mir_post_borrowck_cleanup(key: LocalDefId) -> &'tcx Steal<mir::Body<'tcx>> {
676+
no_hash
677+
desc { "post borrowck cleanup of MIR for `{}`", tcx.def_path_str(key) }
678+
}
679+
675680
query closure_typeinfo(key: LocalDefId) -> ty::ClosureTypeInfo<'tcx> {
676681
desc {
677682
"finding symbols for captures of closure `{}`",

compiler/rustc_mir_transform/src/coroutine.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1392,7 +1392,7 @@ pub(crate) fn mir_coroutine_witnesses<'tcx>(
13921392
tcx: TyCtxt<'tcx>,
13931393
def_id: LocalDefId,
13941394
) -> Option<CoroutineLayout<'tcx>> {
1395-
let (body, _) = tcx.mir_promoted(def_id);
1395+
let body = tcx.mir_post_borrowck_cleanup(def_id);
13961396
let body = body.borrow();
13971397
let body = &*body;
13981398

compiler/rustc_mir_transform/src/lib.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ declare_passes! {
171171
mod promote_consts : PromoteTemps;
172172
mod ref_prop : ReferencePropagation;
173173
mod remove_noop_landing_pads : RemoveNoopLandingPads;
174+
mod remove_dead_drops : RemoveDeadDrops;
174175
mod remove_place_mention : RemovePlaceMention;
175176
mod remove_storage_markers : RemoveStorageMarkers;
176177
mod remove_uninit_drops : RemoveUninitDrops;
@@ -221,6 +222,7 @@ pub fn provide(providers: &mut Providers) {
221222
mir_built,
222223
mir_const_qualif,
223224
mir_promoted,
225+
mir_post_borrowck_cleanup,
224226
mir_drops_elaborated_and_const_checked,
225227
mir_for_ctfe,
226228
mir_coroutine_witnesses: coroutine::mir_coroutine_witnesses,
@@ -485,6 +487,19 @@ fn mir_promoted(
485487
(tcx.alloc_steal_mir(body), tcx.alloc_steal_promoted(promoted))
486488
}
487489

490+
fn mir_post_borrowck_cleanup(tcx: TyCtxt<'_>, def: LocalDefId) -> &Steal<Body<'_>> {
491+
let (body, _) = tcx.mir_promoted(def);
492+
let mut body = body.borrow().clone();
493+
pm::run_passes(
494+
tcx,
495+
&mut body,
496+
&[&remove_dead_drops::RemoveDeadDrops],
497+
None,
498+
pm::Optimizations::Allowed,
499+
);
500+
tcx.alloc_steal_mir(body)
501+
}
502+
488503
/// Compute the MIR that is used during CTFE (and thus has no optimizations run on it)
489504
fn mir_for_ctfe(tcx: TyCtxt<'_>, def_id: LocalDefId) -> &Body<'_> {
490505
debug_assert!(!tcx.is_trivial_const(def_id), "Tried to get mir_for_ctfe of a trivial const");
@@ -543,7 +558,7 @@ fn mir_drops_elaborated_and_const_checked(tcx: TyCtxt<'_>, def: LocalDefId) -> &
543558

544559
tcx.ensure_done().check_liveness(def);
545560

546-
let (body, _) = tcx.mir_promoted(def);
561+
let body = tcx.mir_post_borrowck_cleanup(def);
547562
let mut body = body.steal();
548563

549564
if let Some(error_reported) = tainted_by_errors {
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
use rustc_middle::mir::*;
2+
use rustc_middle::ty::TyCtxt;
3+
use rustc_mir_dataflow::impls::MaybeInitializedPlaces;
4+
use rustc_mir_dataflow::move_paths::{LookupResult, MoveData};
5+
use rustc_mir_dataflow::{Analysis, MaybeReachable};
6+
7+
use super::simplify::simplify_cfg;
8+
9+
pub(crate) struct RemoveDeadDrops;
10+
11+
impl<'tcx> crate::MirPass<'tcx> for RemoveDeadDrops {
12+
fn is_required(&self) -> bool {
13+
true
14+
}
15+
16+
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
17+
let move_data = MoveData::gather_moves(body, tcx, |_| true);
18+
19+
let mut maybe_init_cursor = MaybeInitializedPlaces::new(tcx, body, &move_data)
20+
.iterate_to_fixpoint(tcx, body, None)
21+
.into_results_cursor(body);
22+
23+
let mut dead_drops = Vec::new();
24+
25+
for (block, data) in body.basic_blocks.iter_enumerated() {
26+
if let Some(terminator) = &data.terminator
27+
&& let TerminatorKind::Drop { place, target, .. } = &terminator.kind
28+
{
29+
let LookupResult::Exact(path) = move_data.rev_lookup.find(place.as_ref()) else {
30+
continue;
31+
};
32+
33+
let term_location = Location { block, statement_index: data.statements.len() };
34+
maybe_init_cursor.seek_before_primary_effect(term_location);
35+
36+
let is_dead = match maybe_init_cursor.get() {
37+
MaybeReachable::Unreachable => true,
38+
MaybeReachable::Reachable(maybe_init) => !maybe_init.contains(path),
39+
};
40+
41+
if is_dead {
42+
dead_drops.push((block, *target));
43+
}
44+
}
45+
}
46+
47+
if !dead_drops.is_empty() {
48+
for (block, target) in dead_drops {
49+
if let Some(terminator) = &mut body.basic_blocks.as_mut()[block].terminator {
50+
terminator.kind = TerminatorKind::Goto { target };
51+
}
52+
}
53+
54+
// Removing drop terminators may simplify the CFG, so run cleanup.
55+
simplify_cfg(tcx, body);
56+
}
57+
}
58+
}

tests/mir-opt/basic_assignment.main.ElaborateDrops.diff

Lines changed: 7 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -35,53 +35,23 @@
3535
StorageLive(_5);
3636
StorageLive(_6);
3737
_6 = move _4;
38-
- drop(_5) -> [return: bb1, unwind: bb2];
39-
+ goto -> bb1;
40-
}
41-
42-
bb1: {
4338
_5 = move _6;
44-
- drop(_6) -> [return: bb3, unwind: bb6];
45-
+ goto -> bb3;
46-
}
47-
48-
bb2 (cleanup): {
49-
_5 = move _6;
50-
- drop(_6) -> [return: bb6, unwind terminate(cleanup)];
51-
+ goto -> bb6;
52-
}
53-
54-
bb3: {
5539
StorageDead(_6);
5640
_0 = const ();
57-
drop(_5) -> [return: bb4, unwind: bb7];
41+
- drop(_5) -> [return: bb1, unwind continue];
42+
+ drop(_5) -> [return: bb1, unwind: bb2];
5843
}
5944

60-
bb4: {
45+
bb1: {
6146
StorageDead(_5);
62-
- drop(_4) -> [return: bb5, unwind continue];
63-
+ goto -> bb5;
64-
}
65-
66-
bb5: {
6747
StorageDead(_4);
6848
StorageDead(_2);
6949
StorageDead(_1);
7050
return;
71-
}
72-
73-
bb6 (cleanup): {
74-
- drop(_5) -> [return: bb7, unwind terminate(cleanup)];
75-
+ goto -> bb7;
76-
}
77-
78-
bb7 (cleanup): {
79-
- drop(_4) -> [return: bb8, unwind terminate(cleanup)];
80-
+ goto -> bb8;
81-
}
82-
83-
bb8 (cleanup): {
84-
resume;
51+
+ }
52+
+
53+
+ bb2 (cleanup): {
54+
+ resume;
8555
}
8656
}
8757

tests/mir-opt/box_conditional_drop_allocator.main.ElaborateDrops.diff

Lines changed: 37 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@
3131
_2 = HasDrop;
3232
StorageLive(_3);
3333
_3 = DropAllocator;
34-
_1 = Box::<HasDrop, DropAllocator>::new_in(move _2, move _3) -> [return: bb1, unwind: bb11];
34+
+ _9 = const true;
35+
_1 = Box::<HasDrop, DropAllocator>::new_in(move _2, move _3) -> [return: bb1, unwind continue];
3536
}
3637

3738
bb1: {
38-
+ _9 = const true;
3939
StorageDead(_3);
4040
StorageDead(_2);
4141
StorageLive(_4);
@@ -47,7 +47,7 @@
4747
StorageLive(_5);
4848
StorageLive(_6);
4949
_6 = move (*_1);
50-
_5 = std::mem::drop::<HasDrop>(move _6) -> [return: bb3, unwind: bb9];
50+
_5 = std::mem::drop::<HasDrop>(move _6) -> [return: bb3, unwind: bb8];
5151
}
5252

5353
bb3: {
@@ -75,7 +75,7 @@
7575
bb6: {
7676
StorageDead(_4);
7777
- drop(_1) -> [return: bb7, unwind continue];
78-
+ goto -> bb23;
78+
+ goto -> bb19;
7979
}
8080

8181
bb7: {
@@ -85,102 +85,82 @@
8585
}
8686

8787
bb8 (cleanup): {
88-
- drop(_8) -> [return: bb10, unwind terminate(cleanup)];
89-
+ goto -> bb10;
88+
- drop(_1) -> [return: bb9, unwind terminate(cleanup)];
89+
+ goto -> bb25;
9090
}
9191

9292
bb9 (cleanup): {
93-
- drop(_6) -> [return: bb10, unwind terminate(cleanup)];
94-
+ goto -> bb10;
95-
}
96-
97-
bb10 (cleanup): {
98-
- drop(_1) -> [return: bb13, unwind terminate(cleanup)];
99-
+ goto -> bb29;
100-
}
101-
102-
bb11 (cleanup): {
103-
- drop(_3) -> [return: bb12, unwind terminate(cleanup)];
104-
+ goto -> bb12;
105-
}
106-
107-
bb12 (cleanup): {
108-
- drop(_2) -> [return: bb13, unwind terminate(cleanup)];
109-
+ goto -> bb13;
110-
}
111-
112-
bb13 (cleanup): {
11393
resume;
11494
+ }
11595
+
116-
+ bb14: {
96+
+ bb10: {
11797
+ _9 = const false;
11898
+ goto -> bb7;
11999
+ }
120100
+
121-
+ bb15 (cleanup): {
122-
+ drop((_1.1: DropAllocator)) -> [return: bb13, unwind terminate(cleanup)];
101+
+ bb11 (cleanup): {
102+
+ drop((_1.1: DropAllocator)) -> [return: bb9, unwind terminate(cleanup)];
123103
+ }
124104
+
125-
+ bb16 (cleanup): {
126-
+ switchInt(copy _9) -> [0: bb13, otherwise: bb15];
105+
+ bb12 (cleanup): {
106+
+ switchInt(copy _9) -> [0: bb9, otherwise: bb11];
127107
+ }
128108
+
129-
+ bb17: {
130-
+ drop((_1.1: DropAllocator)) -> [return: bb14, unwind: bb13];
109+
+ bb13: {
110+
+ drop((_1.1: DropAllocator)) -> [return: bb10, unwind: bb9];
131111
+ }
132112
+
133-
+ bb18: {
134-
+ switchInt(copy _9) -> [0: bb14, otherwise: bb17];
113+
+ bb14: {
114+
+ switchInt(copy _9) -> [0: bb10, otherwise: bb13];
135115
+ }
136116
+
137-
+ bb19: {
117+
+ bb15: {
138118
+ _10 = &mut _1;
139-
+ _11 = <Box<HasDrop, DropAllocator> as Drop>::drop(move _10) -> [return: bb18, unwind: bb16];
119+
+ _11 = <Box<HasDrop, DropAllocator> as Drop>::drop(move _10) -> [return: bb14, unwind: bb12];
140120
+ }
141121
+
142-
+ bb20 (cleanup): {
122+
+ bb16 (cleanup): {
143123
+ _12 = &mut _1;
144-
+ _13 = <Box<HasDrop, DropAllocator> as Drop>::drop(move _12) -> [return: bb16, unwind terminate(cleanup)];
124+
+ _13 = <Box<HasDrop, DropAllocator> as Drop>::drop(move _12) -> [return: bb12, unwind terminate(cleanup)];
145125
+ }
146126
+
147-
+ bb21: {
148-
+ goto -> bb19;
127+
+ bb17: {
128+
+ goto -> bb15;
149129
+ }
150130
+
151-
+ bb22: {
131+
+ bb18: {
152132
+ _14 = copy ((_1.0: std::ptr::Unique<HasDrop>).0: std::ptr::NonNull<HasDrop>) as *const HasDrop (Transmute);
153-
+ goto -> bb21;
133+
+ goto -> bb17;
154134
+ }
155135
+
156-
+ bb23: {
157-
+ switchInt(copy _9) -> [0: bb18, otherwise: bb22];
136+
+ bb19: {
137+
+ switchInt(copy _9) -> [0: bb14, otherwise: bb18];
158138
+ }
159139
+
160-
+ bb24 (cleanup): {
161-
+ drop((_1.1: DropAllocator)) -> [return: bb13, unwind terminate(cleanup)];
140+
+ bb20 (cleanup): {
141+
+ drop((_1.1: DropAllocator)) -> [return: bb9, unwind terminate(cleanup)];
162142
+ }
163143
+
164-
+ bb25 (cleanup): {
165-
+ switchInt(copy _9) -> [0: bb13, otherwise: bb24];
144+
+ bb21 (cleanup): {
145+
+ switchInt(copy _9) -> [0: bb9, otherwise: bb20];
166146
+ }
167147
+
168-
+ bb26 (cleanup): {
148+
+ bb22 (cleanup): {
169149
+ _15 = &mut _1;
170-
+ _16 = <Box<HasDrop, DropAllocator> as Drop>::drop(move _15) -> [return: bb25, unwind terminate(cleanup)];
150+
+ _16 = <Box<HasDrop, DropAllocator> as Drop>::drop(move _15) -> [return: bb21, unwind terminate(cleanup)];
171151
+ }
172152
+
173-
+ bb27 (cleanup): {
174-
+ goto -> bb26;
153+
+ bb23 (cleanup): {
154+
+ goto -> bb22;
175155
+ }
176156
+
177-
+ bb28 (cleanup): {
157+
+ bb24 (cleanup): {
178158
+ _17 = copy ((_1.0: std::ptr::Unique<HasDrop>).0: std::ptr::NonNull<HasDrop>) as *const HasDrop (Transmute);
179-
+ goto -> bb27;
159+
+ goto -> bb23;
180160
+ }
181161
+
182-
+ bb29 (cleanup): {
183-
+ switchInt(copy _9) -> [0: bb25, otherwise: bb28];
162+
+ bb25 (cleanup): {
163+
+ switchInt(copy _9) -> [0: bb21, otherwise: bb24];
184164
}
185165
}
186166

0 commit comments

Comments
 (0)