1
Fork 0

Revert "Simplify unscheduling of drops after moves"

This reverts commit b766abc88f.
This commit is contained in:
Felix S. Klock II 2021-01-21 22:44:02 -05:00
parent bed69c6134
commit dac354fc32
5 changed files with 80 additions and 31 deletions

View file

@ -83,7 +83,7 @@ that contains only loops and breakable blocks. It tracks where a `break`,
use crate::build::{BlockAnd, BlockAndExtension, BlockFrame, Builder, CFG}; use crate::build::{BlockAnd, BlockAndExtension, BlockFrame, Builder, CFG};
use crate::thir::{Expr, ExprRef, LintLevel}; use crate::thir::{Expr, ExprRef, LintLevel};
use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use rustc_data_structures::fx::FxHashMap;
use rustc_index::vec::IndexVec; use rustc_index::vec::IndexVec;
use rustc_middle::middle::region; use rustc_middle::middle::region;
use rustc_middle::mir::*; use rustc_middle::mir::*;
@ -120,6 +120,8 @@ struct Scope {
/// end of the vector (top of the stack) first. /// end of the vector (top of the stack) first.
drops: Vec<DropData>, drops: Vec<DropData>,
moved_locals: Vec<Local>,
/// The drop index that will drop everything in and below this scope on an /// The drop index that will drop everything in and below this scope on an
/// unwind path. /// unwind path.
cached_unwind_block: Option<DropIdx>, cached_unwind_block: Option<DropIdx>,
@ -403,6 +405,7 @@ impl<'tcx> Scopes<'tcx> {
region_scope: region_scope.0, region_scope: region_scope.0,
region_scope_span: region_scope.1.span, region_scope_span: region_scope.1.span,
drops: vec![], drops: vec![],
moved_locals: vec![],
cached_unwind_block: None, cached_unwind_block: None,
cached_generator_drop_block: None, cached_generator_drop_block: None,
}); });
@ -890,19 +893,20 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
assert_eq!(scope.region_scope, local_scope, "local scope is not the topmost scope!",); assert_eq!(scope.region_scope, local_scope, "local scope is not the topmost scope!",);
// look for moves of a local variable, like `MOVE(_X)` // look for moves of a local variable, like `MOVE(_X)`
let locals_moved = operands let locals_moved = operands.iter().flat_map(|operand| match operand {
.iter() Operand::Copy(_) | Operand::Constant(_) => None,
.filter_map(|operand| match operand { Operand::Move(place) => place.as_local(),
Operand::Copy(_) | Operand::Constant(_) => None, });
Operand::Move(place) => place.as_local(),
})
.collect::<FxHashSet<_>>();
// Remove the drops for the moved operands. for local in locals_moved {
scope // check if we have a Drop for this operand and -- if so
.drops // -- add it to the list of moved operands. Note that this
.retain(|drop| drop.kind == DropKind::Storage || !locals_moved.contains(&drop.local)); // local might not have been an operand created for this
scope.invalidate_cache(); // call, it could come from other places too.
if scope.drops.iter().any(|drop| drop.local == local && drop.kind == DropKind::Value) {
scope.moved_locals.push(local);
}
}
} }
// Other // Other
@ -1147,6 +1151,14 @@ fn build_scope_drops<'tcx>(
debug_assert_eq!(unwind_drops.drops[unwind_to].0.kind, drop_data.kind); debug_assert_eq!(unwind_drops.drops[unwind_to].0.kind, drop_data.kind);
unwind_to = unwind_drops.drops[unwind_to].1; unwind_to = unwind_drops.drops[unwind_to].1;
// If the operand has been moved, and we are not on an unwind
// path, then don't generate the drop. (We only take this into
// account for non-unwind paths so as not to disturb the
// caching mechanism.)
if scope.moved_locals.iter().any(|&o| o == local) {
continue;
}
unwind_drops.add_entry(block, unwind_to); unwind_drops.add_entry(block, unwind_to);
let next = cfg.start_new_block(); let next = cfg.start_new_block();

View file

@ -14,7 +14,7 @@ fn main() -> () {
StorageLive(_1); // scope 0 at $DIR/box_expr.rs:7:9: 7:10 StorageLive(_1); // scope 0 at $DIR/box_expr.rs:7:9: 7:10
StorageLive(_2); // scope 0 at $DIR/box_expr.rs:7:13: 7:25 StorageLive(_2); // scope 0 at $DIR/box_expr.rs:7:13: 7:25
_2 = Box(S); // scope 0 at $DIR/box_expr.rs:7:13: 7:25 _2 = Box(S); // scope 0 at $DIR/box_expr.rs:7:13: 7:25
(*_2) = S::new() -> [return: bb1, unwind: bb6]; // scope 0 at $DIR/box_expr.rs:7:17: 7:25 (*_2) = S::new() -> [return: bb1, unwind: bb7]; // scope 0 at $DIR/box_expr.rs:7:17: 7:25
// mir::Constant // mir::Constant
// + span: $DIR/box_expr.rs:7:17: 7:23 // + span: $DIR/box_expr.rs:7:17: 7:23
// + literal: Const { ty: fn() -> S {S::new}, val: Value(Scalar(<ZST>)) } // + literal: Const { ty: fn() -> S {S::new}, val: Value(Scalar(<ZST>)) }
@ -49,14 +49,18 @@ fn main() -> () {
} }
bb5 (cleanup): { bb5 (cleanup): {
drop(_1) -> bb7; // scope 0 at $DIR/box_expr.rs:9:1: 9:2 drop(_4) -> bb6; // scope 1 at $DIR/box_expr.rs:8:11: 8:12
} }
bb6 (cleanup): { bb6 (cleanup): {
drop(_2) -> bb7; // scope 0 at $DIR/box_expr.rs:7:24: 7:25 drop(_1) -> bb8; // scope 0 at $DIR/box_expr.rs:9:1: 9:2
} }
bb7 (cleanup): { bb7 (cleanup): {
drop(_2) -> bb8; // scope 0 at $DIR/box_expr.rs:7:24: 7:25
}
bb8 (cleanup): {
resume; // scope 0 at $DIR/box_expr.rs:6:1: 9:2 resume; // scope 0 at $DIR/box_expr.rs:6:1: 9:2
} }
} }

View file

@ -6,18 +6,21 @@ fn main() -> () {
let mut _2: S; // in scope 0 at $DIR/issue-41110.rs:8:13: 8:14 let mut _2: S; // in scope 0 at $DIR/issue-41110.rs:8:13: 8:14
let mut _3: S; // in scope 0 at $DIR/issue-41110.rs:8:21: 8:27 let mut _3: S; // in scope 0 at $DIR/issue-41110.rs:8:21: 8:27
let mut _4: S; // in scope 0 at $DIR/issue-41110.rs:8:21: 8:22 let mut _4: S; // in scope 0 at $DIR/issue-41110.rs:8:21: 8:22
let mut _5: bool; // in scope 0 at $DIR/issue-41110.rs:8:27: 8:28
scope 1 { scope 1 {
debug x => _1; // in scope 1 at $DIR/issue-41110.rs:8:9: 8:10 debug x => _1; // in scope 1 at $DIR/issue-41110.rs:8:9: 8:10
} }
bb0: { bb0: {
_5 = const false; // scope 0 at $DIR/issue-41110.rs:8:9: 8:10
StorageLive(_1); // scope 0 at $DIR/issue-41110.rs:8:9: 8:10 StorageLive(_1); // scope 0 at $DIR/issue-41110.rs:8:9: 8:10
StorageLive(_2); // scope 0 at $DIR/issue-41110.rs:8:13: 8:14 StorageLive(_2); // scope 0 at $DIR/issue-41110.rs:8:13: 8:14
_5 = const true; // scope 0 at $DIR/issue-41110.rs:8:13: 8:14
_2 = S; // scope 0 at $DIR/issue-41110.rs:8:13: 8:14 _2 = S; // scope 0 at $DIR/issue-41110.rs:8:13: 8:14
StorageLive(_3); // scope 0 at $DIR/issue-41110.rs:8:21: 8:27 StorageLive(_3); // scope 0 at $DIR/issue-41110.rs:8:21: 8:27
StorageLive(_4); // scope 0 at $DIR/issue-41110.rs:8:21: 8:22 StorageLive(_4); // scope 0 at $DIR/issue-41110.rs:8:21: 8:22
_4 = S; // scope 0 at $DIR/issue-41110.rs:8:21: 8:22 _4 = S; // scope 0 at $DIR/issue-41110.rs:8:21: 8:22
_3 = S::id(move _4) -> [return: bb1, unwind: bb3]; // scope 0 at $DIR/issue-41110.rs:8:21: 8:27 _3 = S::id(move _4) -> [return: bb1, unwind: bb4]; // scope 0 at $DIR/issue-41110.rs:8:21: 8:27
// mir::Constant // mir::Constant
// + span: $DIR/issue-41110.rs:8:23: 8:25 // + span: $DIR/issue-41110.rs:8:23: 8:25
// + literal: Const { ty: fn(S) -> S {S::id}, val: Value(Scalar(<ZST>)) } // + literal: Const { ty: fn(S) -> S {S::id}, val: Value(Scalar(<ZST>)) }
@ -25,7 +28,8 @@ fn main() -> () {
bb1: { bb1: {
StorageDead(_4); // scope 0 at $DIR/issue-41110.rs:8:26: 8:27 StorageDead(_4); // scope 0 at $DIR/issue-41110.rs:8:26: 8:27
_1 = S::other(move _2, move _3) -> bb2; // scope 0 at $DIR/issue-41110.rs:8:13: 8:28 _5 = const false; // scope 0 at $DIR/issue-41110.rs:8:13: 8:28
_1 = S::other(move _2, move _3) -> [return: bb2, unwind: bb3]; // scope 0 at $DIR/issue-41110.rs:8:13: 8:28
// mir::Constant // mir::Constant
// + span: $DIR/issue-41110.rs:8:15: 8:20 // + span: $DIR/issue-41110.rs:8:15: 8:20
// + literal: Const { ty: fn(S, S) {S::other}, val: Value(Scalar(<ZST>)) } // + literal: Const { ty: fn(S, S) {S::other}, val: Value(Scalar(<ZST>)) }
@ -33,6 +37,7 @@ fn main() -> () {
bb2: { bb2: {
StorageDead(_3); // scope 0 at $DIR/issue-41110.rs:8:27: 8:28 StorageDead(_3); // scope 0 at $DIR/issue-41110.rs:8:27: 8:28
_5 = const false; // scope 0 at $DIR/issue-41110.rs:8:27: 8:28
StorageDead(_2); // scope 0 at $DIR/issue-41110.rs:8:27: 8:28 StorageDead(_2); // scope 0 at $DIR/issue-41110.rs:8:27: 8:28
_0 = const (); // scope 0 at $DIR/issue-41110.rs:7:11: 9:2 _0 = const (); // scope 0 at $DIR/issue-41110.rs:7:11: 9:2
StorageDead(_1); // scope 0 at $DIR/issue-41110.rs:9:1: 9:2 StorageDead(_1); // scope 0 at $DIR/issue-41110.rs:9:1: 9:2
@ -40,10 +45,26 @@ fn main() -> () {
} }
bb3 (cleanup): { bb3 (cleanup): {
drop(_2) -> bb4; // scope 0 at $DIR/issue-41110.rs:8:27: 8:28 goto -> bb5; // scope 0 at $DIR/issue-41110.rs:8:27: 8:28
} }
bb4 (cleanup): { bb4 (cleanup): {
goto -> bb5; // scope 0 at $DIR/issue-41110.rs:8:26: 8:27
}
bb5 (cleanup): {
goto -> bb8; // scope 0 at $DIR/issue-41110.rs:8:27: 8:28
}
bb6 (cleanup): {
resume; // scope 0 at $DIR/issue-41110.rs:7:1: 9:2 resume; // scope 0 at $DIR/issue-41110.rs:7:1: 9:2
} }
bb7 (cleanup): {
drop(_2) -> bb6; // scope 0 at $DIR/issue-41110.rs:8:27: 8:28
}
bb8 (cleanup): {
switchInt(_5) -> [false: bb6, otherwise: bb7]; // scope 0 at $DIR/issue-41110.rs:8:27: 8:28
}
} }

View file

@ -37,7 +37,7 @@ fn test() -> () {
StorageLive(_5); // scope 2 at $DIR/issue-41110.rs:18:9: 18:10 StorageLive(_5); // scope 2 at $DIR/issue-41110.rs:18:9: 18:10
_6 = const false; // scope 2 at $DIR/issue-41110.rs:18:9: 18:10 _6 = const false; // scope 2 at $DIR/issue-41110.rs:18:9: 18:10
_5 = move _1; // scope 2 at $DIR/issue-41110.rs:18:9: 18:10 _5 = move _1; // scope 2 at $DIR/issue-41110.rs:18:9: 18:10
goto -> bb11; // scope 2 at $DIR/issue-41110.rs:18:5: 18:6 goto -> bb12; // scope 2 at $DIR/issue-41110.rs:18:5: 18:6
} }
bb2: { bb2: {
@ -47,7 +47,7 @@ fn test() -> () {
bb3: { bb3: {
StorageDead(_5); // scope 2 at $DIR/issue-41110.rs:18:9: 18:10 StorageDead(_5); // scope 2 at $DIR/issue-41110.rs:18:9: 18:10
_0 = const (); // scope 0 at $DIR/issue-41110.rs:14:15: 19:2 _0 = const (); // scope 0 at $DIR/issue-41110.rs:14:15: 19:2
drop(_2) -> [return: bb4, unwind: bb8]; // scope 1 at $DIR/issue-41110.rs:19:1: 19:2 drop(_2) -> [return: bb4, unwind: bb9]; // scope 1 at $DIR/issue-41110.rs:19:1: 19:2
} }
bb4: { bb4: {
@ -62,36 +62,40 @@ fn test() -> () {
} }
bb6 (cleanup): { bb6 (cleanup): {
goto -> bb7; // scope 2 at $DIR/issue-41110.rs:18:9: 18:10 goto -> bb8; // scope 2 at $DIR/issue-41110.rs:18:9: 18:10
} }
bb7 (cleanup): { bb7 (cleanup): {
goto -> bb8; // scope 1 at $DIR/issue-41110.rs:19:1: 19:2 goto -> bb8; // scope 2 at $DIR/issue-41110.rs:17:11: 17:12
} }
bb8 (cleanup): { bb8 (cleanup): {
goto -> bb13; // scope 0 at $DIR/issue-41110.rs:19:1: 19:2 goto -> bb9; // scope 1 at $DIR/issue-41110.rs:19:1: 19:2
} }
bb9 (cleanup): { bb9 (cleanup): {
resume; // scope 0 at $DIR/issue-41110.rs:14:1: 19:2 goto -> bb14; // scope 0 at $DIR/issue-41110.rs:19:1: 19:2
} }
bb10 (cleanup): { bb10 (cleanup): {
resume; // scope 0 at $DIR/issue-41110.rs:14:1: 19:2
}
bb11 (cleanup): {
_2 = move _5; // scope 2 at $DIR/issue-41110.rs:18:5: 18:6 _2 = move _5; // scope 2 at $DIR/issue-41110.rs:18:5: 18:6
goto -> bb6; // scope 2 at $DIR/issue-41110.rs:18:5: 18:6 goto -> bb6; // scope 2 at $DIR/issue-41110.rs:18:5: 18:6
} }
bb11: { bb12: {
_2 = move _5; // scope 2 at $DIR/issue-41110.rs:18:5: 18:6 _2 = move _5; // scope 2 at $DIR/issue-41110.rs:18:5: 18:6
goto -> bb2; // scope 2 at $DIR/issue-41110.rs:18:5: 18:6 goto -> bb2; // scope 2 at $DIR/issue-41110.rs:18:5: 18:6
} }
bb12 (cleanup): { bb13 (cleanup): {
drop(_1) -> bb9; // scope 0 at $DIR/issue-41110.rs:19:1: 19:2 drop(_1) -> bb10; // scope 0 at $DIR/issue-41110.rs:19:1: 19:2
} }
bb13 (cleanup): { bb14 (cleanup): {
switchInt(_6) -> [false: bb9, otherwise: bb12]; // scope 0 at $DIR/issue-41110.rs:19:1: 19:2 switchInt(_6) -> [false: bb10, otherwise: bb13]; // scope 0 at $DIR/issue-41110.rs:19:1: 19:2
} }
} }

View file

@ -28,7 +28,7 @@ fn main() -> () {
bb1: { bb1: {
StorageDead(_3); // scope 0 at $DIR/no-spurious-drop-after-call.rs:9:33: 9:34 StorageDead(_3); // scope 0 at $DIR/no-spurious-drop-after-call.rs:9:33: 9:34
_1 = std::mem::drop::<String>(move _2) -> bb2; // scope 0 at $DIR/no-spurious-drop-after-call.rs:9:5: 9:35 _1 = std::mem::drop::<String>(move _2) -> [return: bb2, unwind: bb3]; // scope 0 at $DIR/no-spurious-drop-after-call.rs:9:5: 9:35
// mir::Constant // mir::Constant
// + span: $DIR/no-spurious-drop-after-call.rs:9:5: 9:19 // + span: $DIR/no-spurious-drop-after-call.rs:9:5: 9:19
// + literal: Const { ty: fn(std::string::String) {std::mem::drop::<std::string::String>}, val: Value(Scalar(<ZST>)) } // + literal: Const { ty: fn(std::string::String) {std::mem::drop::<std::string::String>}, val: Value(Scalar(<ZST>)) }
@ -41,4 +41,12 @@ fn main() -> () {
_0 = const (); // scope 0 at $DIR/no-spurious-drop-after-call.rs:8:11: 10:2 _0 = const (); // scope 0 at $DIR/no-spurious-drop-after-call.rs:8:11: 10:2
return; // scope 0 at $DIR/no-spurious-drop-after-call.rs:10:2: 10:2 return; // scope 0 at $DIR/no-spurious-drop-after-call.rs:10:2: 10:2
} }
bb3 (cleanup): {
drop(_2) -> bb4; // scope 0 at $DIR/no-spurious-drop-after-call.rs:9:34: 9:35
}
bb4 (cleanup): {
resume; // scope 0 at $DIR/no-spurious-drop-after-call.rs:8:1: 10:2
}
} }