Auto merge of #66329 - ktrianta:mir-opt-unreachable-propagation, r=oli-obk

Add unreachable propagation mir optimization pass

@oli-obk suggested we create a MIR pass that optimizes away basic blocks that lead only to basic blocks with terminator kind **unreachable**. This is a first take on this, which we started with @gilescope at RustFest Impl Days.

The test currently fails when the compiled program runs (undefined behaviour). Is there a way to avoid running the compiled program?
This commit is contained in:
bors 2020-01-15 05:01:10 +00:00
commit 632387f38d
8 changed files with 453 additions and 71 deletions

View file

@ -47,25 +47,22 @@ fn main() {
// }
// bb0: {
// _5 = discriminant(_1);
// switchInt(move _5) -> [0isize: bb4, 1isize: bb2, otherwise: bb1];
// switchInt(move _5) -> [0isize: bb3, otherwise: bb1];
// }
// bb1: {
// unreachable;
// }
// bb2: {
// _6 = ((_1 as Err).0: i32);
// ((_0 as Err).0: i32) = move _6;
// discriminant(_0) = 1;
// goto -> bb3;
// goto -> bb2;
// }
// bb3: {
// bb2: {
// return;
// }
// bb4: {
// bb3: {
// _10 = ((_1 as Ok).0: u32);
// ((_0 as Ok).0: u32) = move _10;
// discriminant(_0) = 0;
// goto -> bb3;
// goto -> bb2;
// }
// }
// END rustc.try_identity.SimplifyArmIdentity.before.mir
@ -109,25 +106,22 @@ fn main() {
// }
// bb0: {
// _5 = discriminant(_1);
// switchInt(move _5) -> [0isize: bb4, 1isize: bb2, otherwise: bb1];
// switchInt(move _5) -> [0isize: bb3, otherwise: bb1];
// }
// bb1: {
// unreachable;
// _0 = move _1;
// nop;
// nop;
// goto -> bb2;
// }
// bb2: {
// _0 = move _1;
// nop;
// nop;
// goto -> bb3;
// }
// bb3: {
// return;
// }
// bb4: {
// bb3: {
// _0 = move _1;
// nop;
// nop;
// goto -> bb3;
// goto -> bb2;
// }
// }
// END rustc.try_identity.SimplifyArmIdentity.after.mir

View file

@ -45,53 +45,47 @@ fn main() {
// StorageLive(_2);
// _2 = Test1::C;
// _3 = discriminant(_2);
// switchInt(move _3) -> [0isize: bb3, 1isize: bb4, 2isize: bb1, otherwise: bb2];
// switchInt(move _3) -> [0isize: bb2, 1isize: bb3, otherwise: bb1];
// }
// bb1: {
// StorageLive(_5);
// _5 = const "C";
// _1 = &(*_5);
// StorageDead(_5);
// goto -> bb5;
// goto -> bb4;
// }
// bb2: {
// unreachable;
// _1 = const "A(Empty)";
// goto -> bb4;
// }
// bb3: {
// _1 = const "A(Empty)";
// goto -> bb5;
// }
// bb4: {
// StorageLive(_4);
// _4 = const "B(Empty)";
// _1 = &(*_4);
// StorageDead(_4);
// goto -> bb5;
// goto -> bb4;
// }
// bb5: {
// bb4: {
// StorageDead(_2);
// StorageDead(_1);
// StorageLive(_6);
// StorageLive(_7);
// _7 = Test2::D;
// _8 = discriminant(_7);
// switchInt(move _8) -> [4isize: bb8, 5isize: bb6, otherwise: bb7];
// switchInt(move _8) -> [4isize: bb6, otherwise: bb5];
// }
// bb6: {
// bb5: {
// StorageLive(_9);
// _9 = const "E";
// _6 = &(*_9);
// StorageDead(_9);
// goto -> bb9;
// goto -> bb7;
// }
// bb6: {
// _6 = const "D";
// goto -> bb7;
// }
// bb7: {
// unreachable;
// }
// bb8: {
// _6 = const "D";
// goto -> bb9;
// }
// bb9: {
// StorageDead(_7);
// StorageDead(_6);
// _0 = ();
@ -114,53 +108,47 @@ fn main() {
// StorageLive(_2);
// _2 = Test1::C;
// _3 = discriminant(_2);
// switchInt(move _3) -> [2isize: bb1, otherwise: bb2];
// switchInt(move _3) -> bb1;
// }
// bb1: {
// StorageLive(_5);
// _5 = const "C";
// _1 = &(*_5);
// StorageDead(_5);
// goto -> bb5;
// goto -> bb4;
// }
// bb2: {
// unreachable;
// _1 = const "A(Empty)";
// goto -> bb4;
// }
// bb3: {
// _1 = const "A(Empty)";
// goto -> bb5;
// }
// bb4: {
// StorageLive(_4);
// _4 = const "B(Empty)";
// _1 = &(*_4);
// StorageDead(_4);
// goto -> bb5;
// goto -> bb4;
// }
// bb5: {
// bb4: {
// StorageDead(_2);
// StorageDead(_1);
// StorageLive(_6);
// StorageLive(_7);
// _7 = Test2::D;
// _8 = discriminant(_7);
// switchInt(move _8) -> [4isize: bb8, 5isize: bb6, otherwise: bb7];
// switchInt(move _8) -> [4isize: bb6, otherwise: bb5];
// }
// bb6: {
// bb5: {
// StorageLive(_9);
// _9 = const "E";
// _6 = &(*_9);
// StorageDead(_9);
// goto -> bb9;
// goto -> bb7;
// }
// bb6: {
// _6 = const "D";
// goto -> bb7;
// }
// bb7: {
// unreachable;
// }
// bb8: {
// _6 = const "D";
// goto -> bb9;
// }
// bb9: {
// StorageDead(_7);
// StorageDead(_6);
// _0 = ();
@ -183,9 +171,6 @@ fn main() {
// StorageLive(_2);
// _2 = Test1::C;
// _3 = discriminant(_2);
// switchInt(move _3) -> [2isize: bb1, otherwise: bb2];
// }
// bb1: {
// StorageLive(_5);
// _5 = const "C";
// _1 = &(*_5);
@ -196,26 +181,20 @@ fn main() {
// StorageLive(_7);
// _7 = Test2::D;
// _8 = discriminant(_7);
// switchInt(move _8) -> [4isize: bb5, 5isize: bb3, otherwise: bb4];
// switchInt(move _8) -> [4isize: bb2, otherwise: bb1];
// }
// bb2: {
// unreachable;
// }
// bb3: {
// bb1: {
// StorageLive(_9);
// _9 = const "E";
// _6 = &(*_9);
// StorageDead(_9);
// goto -> bb6;
// goto -> bb3;
// }
// bb4: {
// unreachable;
// }
// bb5: {
// bb2: {
// _6 = const "D";
// goto -> bb6;
// goto -> bb3;
// }
// bb6: {
// bb3: {
// StorageDead(_7);
// StorageDead(_6);
// _0 = ();

View file

@ -0,0 +1,78 @@
enum Empty {}
fn empty() -> Option<Empty> {
None
}
fn main() {
if let Some(_x) = empty() {
let mut _y;
if true {
_y = 21;
} else {
_y = 42;
}
match _x { }
}
}
// END RUST SOURCE
// START rustc.main.UnreachablePropagation.before.mir
// bb0: {
// StorageLive(_1);
// _1 = const empty() -> bb1;
// }
// bb1: {
// _2 = discriminant(_1);
// switchInt(move _2) -> [1isize: bb3, otherwise: bb2];
// }
// bb2: {
// _0 = ();
// StorageDead(_1);
// return;
// }
// bb3: {
// StorageLive(_3);
// _3 = move ((_1 as Some).0: Empty);
// StorageLive(_4);
// StorageLive(_5);
// StorageLive(_6);
// _6 = const true;
// switchInt(_6) -> [false: bb4, otherwise: bb5];
// }
// bb4: {
// _4 = const 42i32;
// _5 = ();
// goto -> bb6;
// }
// bb5: {
// _4 = const 21i32;
// _5 = ();
// goto -> bb6;
// }
// bb6: {
// StorageDead(_6);
// StorageDead(_5);
// StorageLive(_7);
// unreachable;
// }
// }
// END rustc.main.UnreachablePropagation.before.mir
// START rustc.main.UnreachablePropagation.after.mir
// bb0: {
// StorageLive(_1);
// _1 = const empty() -> bb1;
// }
// bb1: {
// _2 = discriminant(_1);
// goto -> bb2;
// }
// bb2: {
// _0 = ();
// StorageDead(_1);
// return;
// }
// }
// END rustc.main.UnreachablePropagation.after.mir

View file

@ -0,0 +1,72 @@
// ignore-tidy-linelength
#![feature(asm)]
enum Empty {}
fn empty() -> Option<Empty> {
None
}
fn main() {
if let Some(_x) = empty() {
let mut _y;
if true {
_y = 21;
} else {
_y = 42;
}
// asm instruction stops unreachable propagation to if else blocks bb4 and bb5.
unsafe { asm!("NOP"); }
match _x { }
}
}
// END RUST SOURCE
// START rustc.main.UnreachablePropagation.before.mir
// bb4: {
// _4 = const 42i32;
// _5 = ();
// goto -> bb6;
// }
// bb5: {
// _4 = const 21i32;
// _5 = ();
// goto -> bb6;
// }
// bb6: {
// StorageDead(_6);
// StorageDead(_5);
// StorageLive(_7);
// asm!(InlineAsmInner { asm: "NOP", asm_str_style: Cooked, outputs: [], inputs: [], clobbers: [], volatile: true, alignstack: false, dialect: Att } : [] : []);
// _7 = ();
// StorageDead(_7);
// StorageLive(_8);
// unreachable;
// }
// }
// END rustc.main.UnreachablePropagation.before.mir
// START rustc.main.UnreachablePropagation.after.mir
// bb4: {
// _4 = const 42i32;
// _5 = ();
// goto -> bb6;
// }
// bb5: {
// _4 = const 21i32;
// _5 = ();
// goto -> bb6;
// }
// bb6: {
// StorageDead(_6);
// StorageDead(_5);
// StorageLive(_7);
// asm!(InlineAsmInner { asm: "NOP", asm_str_style: Cooked, outputs: [], inputs: [], clobbers: [], volatile: true, alignstack: false, dialect: Att } : [] : []);
// _7 = ();
// StorageDead(_7);
// StorageLive(_8);
// unreachable;
// }
// }
// END rustc.main.UnreachablePropagation.after.mir

View file

@ -0,0 +1,84 @@
// ignore-tidy-linelength
#![feature(asm)]
enum Empty {}
fn empty() -> Option<Empty> {
None
}
fn main() {
if let Some(_x) = empty() {
let mut _y;
if true {
// asm instruction stops unreachable propagation to block bb3.
unsafe { asm!("NOP"); }
_y = 21;
} else {
// asm instruction stops unreachable propagation to block bb3.
unsafe { asm!("NOP"); }
_y = 42;
}
match _x { }
}
}
// END RUST SOURCE
// START rustc.main.UnreachablePropagation.before.mir
// bb3: {
// ...
// switchInt(_6) -> [false: bb4, otherwise: bb5];
// }
// bb4: {
// StorageLive(_8);
// asm!(InlineAsmInner { asm: "NOP", asm_str_style: Cooked, outputs: [], inputs: [], clobbers: [], volatile: true, alignstack: false, dialect: Att } : [] : []);
// _8 = ();
// StorageDead(_8);
// _4 = const 42i32;
// _5 = ();
// goto -> bb6;
// }
// bb5: {
// StorageLive(_7);
// asm!(InlineAsmInner { asm: "NOP", asm_str_style: Cooked, outputs: [], inputs: [], clobbers: [], volatile: true, alignstack: false, dialect: Att } : [] : []);
// _7 = ();
// StorageDead(_7);
// _4 = const 21i32;
// _5 = ();
// goto -> bb6;
// }
// bb6: {
// StorageDead(_6);
// StorageDead(_5);
// StorageLive(_9);
// unreachable;
// }
// }
// END rustc.main.UnreachablePropagation.before.mir
// START rustc.main.UnreachablePropagation.after.mir
// bb3: {
// ...
// switchInt(_6) -> [false: bb4, otherwise: bb5];
// }
// bb4: {
// StorageLive(_8);
// asm!(InlineAsmInner { asm: "NOP", asm_str_style: Cooked, outputs: [], inputs: [], clobbers: [], volatile: true, alignstack: false, dialect: Att } : [] : []);
// _8 = ();
// StorageDead(_8);
// _4 = const 42i32;
// _5 = ();
// unreachable;
// }
// bb5: {
// StorageLive(_7);
// asm!(InlineAsmInner { asm: "NOP", asm_str_style: Cooked, outputs: [], inputs: [], clobbers: [], volatile: true, alignstack: false, dialect: Att } : [] : []);
// _7 = ();
// StorageDead(_7);
// _4 = const 21i32;
// _5 = ();
// unreachable;
// }
// }
// END rustc.main.UnreachablePropagation.after.mir

View file

@ -0,0 +1,65 @@
pub enum Empty {}
fn empty() -> Option<Empty> {
None
}
fn loop_forever() {
loop {}
}
fn main() {
let x = true;
if let Some(bomb) = empty() {
if x {
loop_forever()
}
match bomb {}
}
}
// END RUST SOURCE
// START rustc.main.UnreachablePropagation.before.mir
// bb3: {
// StorageLive(_4);
// _4 = move ((_2 as Some).0: Empty);
// StorageLive(_5);
// StorageLive(_6);
// _6 = _1;
// switchInt(_6) -> [false: bb4, otherwise: bb5];
// }
// bb4: {
// _5 = ();
// goto -> bb6;
// }
// bb5: {
// _5 = const loop_forever() -> bb6;
// }
// bb6: {
// StorageDead(_6);
// StorageDead(_5);
// StorageLive(_7);
// unreachable;
// }
// }
// END rustc.main.UnreachablePropagation.before.mir
// START rustc.main.UnreachablePropagation.after.mir
// bb3: {
// StorageLive(_4);
// _4 = move ((_2 as Some).0: Empty);
// StorageLive(_5);
// StorageLive(_6);
// _6 = _1;
// goto -> bb4;
// }
// bb4: {
// _5 = const loop_forever() -> bb5;
// }
// bb5: {
// StorageDead(_6);
// StorageDead(_5);
// StorageLive(_7);
// unreachable;
// }
// }
// END rustc.main.UnreachablePropagation.after.mir