Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mir-opt] Run SimplifyLocals to a fixedpoint and handle most rvalues #70755

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 30 additions & 12 deletions src/librustc_mir/transform/simplify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,21 +368,39 @@ impl<'a, 'tcx> Visitor<'tcx> for DeclMarker<'a, 'tcx> {
if location.statement_index != block.statements.len() {
let stmt = &block.statements[location.statement_index];

fn can_skip_constant(c: &ty::Const<'tcx>) -> bool {
// Keep assignments from unevaluated constants around, since the
// evaluation may report errors, even if the use of the constant
// is dead code.
!matches!(c.val, ty::ConstKind::Unevaluated(..))
}

fn can_skip_operand(o: &Operand<'_>) -> bool {
match o {
Operand::Copy(p) | Operand::Move(p) => !p.is_indirect(),
oli-obk marked this conversation as resolved.
Show resolved Hide resolved
Operand::Constant(c) => can_skip_constant(c.literal),
}
}

if let StatementKind::Assign(box (dest, rvalue)) = &stmt.kind {
if !dest.is_indirect() && dest.local == *local {
if let Rvalue::Use(Operand::Constant(c)) = rvalue {
match c.literal.val {
// Keep assignments from unevaluated constants around, since the
// evaluation may report errors, even if the use of the constant
// is dead code.
ty::ConstKind::Unevaluated(..) => {}
_ => {
trace!("skipping store of const value {:?} to {:?}", c, dest);
return;
}
let can_skip = match rvalue {
Rvalue::Use(op) => can_skip_operand(op),
Rvalue::Discriminant(_) => true,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this use !place.is_indirect()?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think eliminating a read of an indirect place's discriminant is ok in the same way removing a read of its address is. Is there a situation you're thinking of where that wouldn't be true?

Rvalue::BinaryOp(_, l, r) | Rvalue::CheckedBinaryOp(_, l, r) => {
can_skip_operand(l) && can_skip_operand(r)
}
} else if let Rvalue::Discriminant(d) = rvalue {
trace!("skipping store of discriminant value {:?} to {:?}", d, dest);
Rvalue::Repeat(op, c) => can_skip_operand(op) && can_skip_constant(c),
Rvalue::AddressOf(_, _) => true,
Rvalue::Len(_) => true,
Rvalue::UnaryOp(_, op) => can_skip_operand(op),
Rvalue::Aggregate(_, operands) => operands.iter().all(can_skip_operand),

_ => false,
};

if can_skip {
trace!("skipping store of {:?} to {:?}", rvalue, dest);
return;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,30 +30,30 @@ fn main() -> () {
}

alloc0 (static: FOO, size: 4, align: 4) {
alloc10+0╼ │ ╾──╼
alloc9+0─╼ │ ╾──╼
}

alloc10 (size: 168, align: 1) {
alloc9 (size: 168, align: 1) {
0x00 │ ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab │ ................
0x10 │ ab ab ab ab ab ab ab ab ab ab ab ab ╾alloc5+0─╼ │ ............╾──╼
0x10 │ ab ab ab ab ab ab ab ab ab ab ab ab ╾alloc4+0─╼ │ ............╾──╼
0x20 │ 01 ef cd ab 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
0x30 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
0x40 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
0x50 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
0x60 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
0x70 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
0x80 │ 00 00 00 00 00 00 00 00 00 00 ╾alloc7+0─╼ 00 00 │ ..........╾──╼..
0x90 │ ╾alloc8+99╼ 00 00 00 00 00 00 00 00 00 00 00 00 │ ╾──╼............
0x80 │ 00 00 00 00 00 00 00 00 00 00 ╾alloc6+0─╼ 00 00 │ ..........╾──╼..
0x90 │ ╾alloc7+99╼ 00 00 00 00 00 00 00 00 00 00 00 00 │ ╾──╼............
0xa0 │ 00 00 00 00 00 00 00 00 │ ........
}

alloc5 (size: 4, align: 4) {
alloc4 (size: 4, align: 4) {
2a 00 00 00 │ *...
}

alloc7 (fn: main)
alloc6 (fn: main)

alloc8 (size: 100, align: 1) {
alloc7 (size: 100, align: 1) {
0x00 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
0x10 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
0x20 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,31 @@ fn main() -> () {
}

alloc0 (static: FOO, size: 8, align: 8) {
╾──────alloc10+0──────╼ │ ╾──────╼
╾──────alloc9+0───────╼ │ ╾──────╼
}

alloc10 (size: 180, align: 1) {
alloc9 (size: 180, align: 1) {
0x00 │ ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab │ ................
0x10 │ ab ab ab ab ab ab ab ab ab ab ab ab ╾─alloc5+0─ │ ............╾───
0x10 │ ab ab ab ab ab ab ab ab ab ab ab ab ╾─alloc4+0─ │ ............╾───
0x20 │ ──────────╼ 01 ef cd ab 00 00 00 00 00 00 00 00 │ ───╼............
0x30 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
0x40 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
0x50 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
0x60 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
0x70 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
0x80 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ╾──── │ ..............╾─
0x90 │ ────alloc7+0────╼ 00 00 ╾──────alloc8+99──────╼ │ ─────╼..╾──────╼
0x90 │ ────alloc6+0────╼ 00 00 ╾──────alloc7+99──────╼ │ ─────╼..╾──────╼
0xa0 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
0xb0 │ 00 00 00 00 │ ....
}

alloc5 (size: 4, align: 4) {
alloc4 (size: 4, align: 4) {
2a 00 00 00 │ *...
}

alloc7 (fn: main)
alloc6 (fn: main)

alloc8 (size: 100, align: 1) {
alloc7 (size: 100, align: 1) {
0x00 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
0x10 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
0x20 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
fn main() -> () {
let mut _0: (); // return place in scope 0 at $DIR/optimizes_into_variable.rs:11:11: 11:11
let _1: i32; // in scope 0 at $DIR/optimizes_into_variable.rs:12:9: 12:10
let mut _3: [i32; 6]; // in scope 0 at $DIR/optimizes_into_variable.rs:13:13: 13:31
scope 1 {
debug x => _1; // in scope 1 at $DIR/optimizes_into_variable.rs:12:9: 12:10
let _2: i32; // in scope 1 at $DIR/optimizes_into_variable.rs:13:9: 13:10
scope 2 {
debug y => _2; // in scope 2 at $DIR/optimizes_into_variable.rs:13:9: 13:10
let _4: u32; // in scope 2 at $DIR/optimizes_into_variable.rs:14:9: 14:10
let _3: u32; // in scope 2 at $DIR/optimizes_into_variable.rs:14:9: 14:10
scope 3 {
debug z => _4; // in scope 3 at $DIR/optimizes_into_variable.rs:14:9: 14:10
debug z => _3; // in scope 3 at $DIR/optimizes_into_variable.rs:14:9: 14:10
}
}
}
Expand All @@ -26,70 +25,31 @@ fn main() -> () {
// + span: $DIR/optimizes_into_variable.rs:12:13: 12:18
// + literal: Const { ty: i32, val: Value(Scalar(0x00000004)) }
StorageLive(_2); // bb0[2]: scope 1 at $DIR/optimizes_into_variable.rs:13:9: 13:10
StorageLive(_3); // bb0[3]: scope 1 at $DIR/optimizes_into_variable.rs:13:13: 13:31
_3 = [const 0i32, const 1i32, const 2i32, const 3i32, const 4i32, const 5i32]; // bb0[4]: scope 1 at $DIR/optimizes_into_variable.rs:13:13: 13:31
// ty::Const
// + ty: i32
// + val: Value(Scalar(0x00000000))
// mir::Constant
// + span: $DIR/optimizes_into_variable.rs:13:14: 13:15
// + literal: Const { ty: i32, val: Value(Scalar(0x00000000)) }
// ty::Const
// + ty: i32
// + val: Value(Scalar(0x00000001))
// mir::Constant
// + span: $DIR/optimizes_into_variable.rs:13:17: 13:18
// + literal: Const { ty: i32, val: Value(Scalar(0x00000001)) }
// ty::Const
// + ty: i32
// + val: Value(Scalar(0x00000002))
// mir::Constant
// + span: $DIR/optimizes_into_variable.rs:13:20: 13:21
// + literal: Const { ty: i32, val: Value(Scalar(0x00000002)) }
// ty::Const
// + ty: i32
// + val: Value(Scalar(0x00000003))
// mir::Constant
// + span: $DIR/optimizes_into_variable.rs:13:23: 13:24
// + literal: Const { ty: i32, val: Value(Scalar(0x00000003)) }
// ty::Const
// + ty: i32
// + val: Value(Scalar(0x00000004))
// mir::Constant
// + span: $DIR/optimizes_into_variable.rs:13:26: 13:27
// + literal: Const { ty: i32, val: Value(Scalar(0x00000004)) }
// ty::Const
// + ty: i32
// + val: Value(Scalar(0x00000005))
// mir::Constant
// + span: $DIR/optimizes_into_variable.rs:13:29: 13:30
// + literal: Const { ty: i32, val: Value(Scalar(0x00000005)) }
_2 = const 3i32; // bb0[5]: scope 1 at $DIR/optimizes_into_variable.rs:13:13: 13:34
_2 = const 3i32; // bb0[3]: scope 1 at $DIR/optimizes_into_variable.rs:13:13: 13:34
// ty::Const
// + ty: i32
// + val: Value(Scalar(0x00000003))
// mir::Constant
// + span: $DIR/optimizes_into_variable.rs:13:13: 13:34
// + literal: Const { ty: i32, val: Value(Scalar(0x00000003)) }
StorageDead(_3); // bb0[6]: scope 1 at $DIR/optimizes_into_variable.rs:13:34: 13:35
StorageLive(_4); // bb0[7]: scope 2 at $DIR/optimizes_into_variable.rs:14:9: 14:10
_4 = const 42u32; // bb0[8]: scope 2 at $DIR/optimizes_into_variable.rs:14:13: 14:38
StorageLive(_3); // bb0[4]: scope 2 at $DIR/optimizes_into_variable.rs:14:9: 14:10
_3 = const 42u32; // bb0[5]: scope 2 at $DIR/optimizes_into_variable.rs:14:13: 14:38
// ty::Const
// + ty: u32
// + val: Value(Scalar(0x0000002a))
// mir::Constant
// + span: $DIR/optimizes_into_variable.rs:14:13: 14:38
// + literal: Const { ty: u32, val: Value(Scalar(0x0000002a)) }
_0 = const (); // bb0[9]: scope 0 at $DIR/optimizes_into_variable.rs:11:11: 15:2
_0 = const (); // bb0[6]: scope 0 at $DIR/optimizes_into_variable.rs:11:11: 15:2
// ty::Const
// + ty: ()
// + val: Value(Scalar(<ZST>))
// mir::Constant
// + span: $DIR/optimizes_into_variable.rs:11:11: 15:2
// + literal: Const { ty: (), val: Value(Scalar(<ZST>)) }
StorageDead(_4); // bb0[10]: scope 2 at $DIR/optimizes_into_variable.rs:15:1: 15:2
StorageDead(_2); // bb0[11]: scope 1 at $DIR/optimizes_into_variable.rs:15:1: 15:2
StorageDead(_1); // bb0[12]: scope 0 at $DIR/optimizes_into_variable.rs:15:1: 15:2
return; // bb0[13]: scope 0 at $DIR/optimizes_into_variable.rs:15:2: 15:2
StorageDead(_3); // bb0[7]: scope 2 at $DIR/optimizes_into_variable.rs:15:1: 15:2
StorageDead(_2); // bb0[8]: scope 1 at $DIR/optimizes_into_variable.rs:15:1: 15:2
StorageDead(_1); // bb0[9]: scope 0 at $DIR/optimizes_into_variable.rs:15:1: 15:2
return; // bb0[10]: scope 0 at $DIR/optimizes_into_variable.rs:15:2: 15:2
}
}
Loading