Skip to content

Commit

Permalink
[mir-opt] Introduce a new flag to enable experimental/unsound mir opts
Browse files Browse the repository at this point in the history
  • Loading branch information
wesleywiser committed Sep 21, 2020
1 parent 7467d17 commit eb3eb85
Show file tree
Hide file tree
Showing 23 changed files with 287 additions and 277 deletions.
4 changes: 3 additions & 1 deletion compiler/rustc_mir/src/transform/copy_prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ pub struct CopyPropagation;

impl<'tcx> MirPass<'tcx> for CopyPropagation {
fn run_pass(&self, tcx: TyCtxt<'tcx>, _source: MirSource<'tcx>, body: &mut Body<'tcx>) {
let opts = &tcx.sess.opts.debugging_opts;
// We only run when the MIR optimization level is > 1.
// This avoids a slow pass, and messing up debug info.
if tcx.sess.opts.debugging_opts.mir_opt_level <= 1 {
// FIXME(76740): This optimization is buggy and can cause unsoundness.
if opts.mir_opt_level <= 1 || !opts.unsound_mir_opts {
return;
}

Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_mir/src/transform/simplify_try.rs
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ impl<'a, 'tcx> SimplifyBranchSameOptimizationFinder<'a, 'tcx> {
&& bb_l.terminator().kind == bb_r.terminator().kind;
let statement_check = || {
bb_l.statements.iter().zip(&bb_r.statements).try_fold(StatementEquality::TrivialEqual, |acc,(l,r)| {
let stmt_equality = self.statement_equality(*adt_matched_on, &l, bb_l_idx, &r, bb_r_idx, self.tcx.sess.opts.debugging_opts.mir_opt_level);
let stmt_equality = self.statement_equality(*adt_matched_on, &l, bb_l_idx, &r, bb_r_idx, self.tcx);
if matches!(stmt_equality, StatementEquality::NotEqual) {
// short circuit
None
Expand Down Expand Up @@ -672,7 +672,7 @@ impl<'a, 'tcx> SimplifyBranchSameOptimizationFinder<'a, 'tcx> {
x_bb_idx: BasicBlock,
y: &Statement<'tcx>,
y_bb_idx: BasicBlock,
mir_opt_level: usize,
tcx: TyCtxt<'tcx>,
) -> StatementEquality {
let helper = |rhs: &Rvalue<'tcx>,
place: &Place<'tcx>,
Expand All @@ -693,7 +693,7 @@ impl<'a, 'tcx> SimplifyBranchSameOptimizationFinder<'a, 'tcx> {
Rvalue::Use(operand) if operand.place() == Some(adt_matched_on) => {
// FIXME(76803): This logic is currently broken because it does not take into
// account the current discriminant value.
if mir_opt_level > 2 {
if tcx.sess.opts.debugging_opts.unsound_mir_opts {
StatementEquality::ConsideredEqual(side_to_choose)
} else {
StatementEquality::NotEqual
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_session/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1115,6 +1115,8 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
`hir,typed` (HIR with types for each node),
`hir-tree` (dump the raw HIR),
`mir` (the MIR), or `mir-cfg` (graphviz formatted MIR)"),
unsound_mir_opts: bool = (false, parse_bool, [TRACKED],
"enable unsound and buggy MIR optimizations (default: no)"),
unstable_options: bool = (false, parse_bool, [UNTRACKED],
"adds unstable command line options to rustc interface (default: no)"),
use_ctors_section: Option<bool> = (None, parse_opt_bool, [TRACKED],
Expand Down
1 change: 1 addition & 0 deletions src/test/mir-opt/copy_propagation.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// compile-flags: -Zunsound-mir-opts
// EMIT_MIR copy_propagation.test.CopyPropagation.diff

fn test(x: u32) -> u32 {
Expand Down
18 changes: 9 additions & 9 deletions src/test/mir-opt/copy_propagation.test.CopyPropagation.diff
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
+ // MIR for `test` after CopyPropagation

fn test(_1: u32) -> u32 {
debug x => _1; // in scope 0 at $DIR/copy_propagation.rs:3:9: 3:10
let mut _0: u32; // return place in scope 0 at $DIR/copy_propagation.rs:3:20: 3:23
let _2: u32; // in scope 0 at $DIR/copy_propagation.rs:4:9: 4:10
debug x => _1; // in scope 0 at $DIR/copy_propagation.rs:4:9: 4:10
let mut _0: u32; // return place in scope 0 at $DIR/copy_propagation.rs:4:20: 4:23
let _2: u32; // in scope 0 at $DIR/copy_propagation.rs:5:9: 5:10
scope 1 {
debug y => _0; // in scope 1 at $DIR/copy_propagation.rs:4:9: 4:10
debug y => _0; // in scope 1 at $DIR/copy_propagation.rs:5:9: 5:10
}

bb0: {
nop; // scope 0 at $DIR/copy_propagation.rs:4:9: 4:10
_0 = _1; // scope 0 at $DIR/copy_propagation.rs:4:13: 4:14
nop; // scope 1 at $DIR/copy_propagation.rs:5:5: 5:6
nop; // scope 0 at $DIR/copy_propagation.rs:6:1: 6:2
return; // scope 0 at $DIR/copy_propagation.rs:6:2: 6:2
nop; // scope 0 at $DIR/copy_propagation.rs:5:9: 5:10
_0 = _1; // scope 0 at $DIR/copy_propagation.rs:5:13: 5:14
nop; // scope 1 at $DIR/copy_propagation.rs:6:5: 6:6
nop; // scope 0 at $DIR/copy_propagation.rs:7:1: 7:2
return; // scope 0 at $DIR/copy_propagation.rs:7:2: 7:2
}
}

2 changes: 1 addition & 1 deletion src/test/mir-opt/early_otherwise_branch_68867.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// ignore-tidy-linelength
// compile-flags: -Z mir-opt-level=3
// compile-flags: -Z mir-opt-level=3 -Zunsound-mir-opts

// example from #68867
type CSSFloat = f32;
Expand Down
2 changes: 1 addition & 1 deletion src/test/mir-opt/inline/inline-any-operand.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// compile-flags: -Z span_free_formats
// compile-flags: -Z span_free_formats -Zunsound-mir-opts

// Tests that MIR inliner works for any operand

Expand Down
2 changes: 1 addition & 1 deletion src/test/mir-opt/inline/inline-closure-borrows-arg.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// compile-flags: -Z span_free_formats
// compile-flags: -Z span_free_formats -Zunsound-mir-opts

// Tests that MIR inliner can handle closure arguments,
// even when (#45894)
Expand Down
2 changes: 1 addition & 1 deletion src/test/mir-opt/inline/inline-trait-method_2.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// compile-flags: -Z span_free_formats -Z mir-opt-level=3
// compile-flags: -Z span_free_formats -Z mir-opt-level=3 -Zunsound-mir-opts

// EMIT_MIR inline_trait_method_2.test2.Inline.after.mir
fn test2(x: &dyn X) -> bool {
Expand Down
2 changes: 2 additions & 0 deletions src/test/mir-opt/issue-73223.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// compile-flags: -Zunsound-mir-opts

fn main() {
let split = match Some(1) {
Some(v) => v,
Expand Down
32 changes: 16 additions & 16 deletions src/test/mir-opt/issue_73223.main.PreCodegen.32bit.diff
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
+ // MIR for `main` after PreCodegen

fn main() -> () {
let mut _0: (); // return place in scope 0 at $DIR/issue-73223.rs:1:11: 1:11
let _1: i32; // in scope 0 at $DIR/issue-73223.rs:2:9: 2:14
let mut _2: std::option::Option<i32>; // in scope 0 at $DIR/issue-73223.rs:2:23: 2:30
let mut _0: (); // return place in scope 0 at $DIR/issue-73223.rs:3:11: 3:11
let _1: i32; // in scope 0 at $DIR/issue-73223.rs:4:9: 4:14
let mut _2: std::option::Option<i32>; // in scope 0 at $DIR/issue-73223.rs:4:23: 4:30
let mut _4: (&i32, &i32); // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
let mut _5: bool; // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
let mut _6: bool; // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
Expand All @@ -20,10 +20,10 @@
let mut _16: std::fmt::ArgumentV1; // in scope 0 at $SRC_DIR/std/src/macros.rs:LL:COL
let mut _17: std::fmt::ArgumentV1; // in scope 0 at $SRC_DIR/std/src/macros.rs:LL:COL
scope 1 {
debug split => _1; // in scope 1 at $DIR/issue-73223.rs:2:9: 2:14
let _3: std::option::Option<i32>; // in scope 1 at $DIR/issue-73223.rs:7:9: 7:14
debug split => _1; // in scope 1 at $DIR/issue-73223.rs:4:9: 4:14
let _3: std::option::Option<i32>; // in scope 1 at $DIR/issue-73223.rs:9:9: 9:14
scope 3 {
debug _prev => _3; // in scope 3 at $DIR/issue-73223.rs:7:9: 7:14
debug _prev => _3; // in scope 3 at $DIR/issue-73223.rs:9:9: 9:14
scope 4 {
debug left_val => _13; // in scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
debug right_val => _15; // in scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
Expand Down Expand Up @@ -55,21 +55,21 @@
}
}
scope 2 {
debug v => _1; // in scope 2 at $DIR/issue-73223.rs:3:14: 3:15
debug v => _1; // in scope 2 at $DIR/issue-73223.rs:5:14: 5:15
}
scope 7 {
}
scope 9 {
}

bb0: {
StorageLive(_2); // scope 0 at $DIR/issue-73223.rs:2:23: 2:30
((_2 as Some).0: i32) = const 1_i32; // scope 0 at $DIR/issue-73223.rs:2:23: 2:30
discriminant(_2) = 1; // scope 0 at $DIR/issue-73223.rs:2:23: 2:30
_1 = ((_2 as Some).0: i32); // scope 0 at $DIR/issue-73223.rs:3:14: 3:15
StorageDead(_2); // scope 0 at $DIR/issue-73223.rs:5:6: 5:7
((_3 as Some).0: i32) = _1; // scope 1 at $DIR/issue-73223.rs:7:22: 7:27
discriminant(_3) = 1; // scope 1 at $DIR/issue-73223.rs:7:17: 7:28
StorageLive(_2); // scope 0 at $DIR/issue-73223.rs:4:23: 4:30
((_2 as Some).0: i32) = const 1_i32; // scope 0 at $DIR/issue-73223.rs:4:23: 4:30
discriminant(_2) = 1; // scope 0 at $DIR/issue-73223.rs:4:23: 4:30
_1 = ((_2 as Some).0: i32); // scope 0 at $DIR/issue-73223.rs:5:14: 5:15
StorageDead(_2); // scope 0 at $DIR/issue-73223.rs:7:6: 7:7
((_3 as Some).0: i32) = _1; // scope 1 at $DIR/issue-73223.rs:9:22: 9:27
discriminant(_3) = 1; // scope 1 at $DIR/issue-73223.rs:9:17: 9:28
(_4.0: &i32) = &_1; // scope 3 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
(_4.1: &i32) = const main::promoted[1]; // scope 3 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
// ty::Const
Expand All @@ -93,8 +93,8 @@

bb1: {
StorageDead(_5); // scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
_0 = const (); // scope 0 at $DIR/issue-73223.rs:1:11: 9:2
return; // scope 0 at $DIR/issue-73223.rs:9:2: 9:2
_0 = const (); // scope 0 at $DIR/issue-73223.rs:3:11: 11:2
return; // scope 0 at $DIR/issue-73223.rs:11:2: 11:2
}

bb2: {
Expand Down
32 changes: 16 additions & 16 deletions src/test/mir-opt/issue_73223.main.PreCodegen.64bit.diff
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
+ // MIR for `main` after PreCodegen

fn main() -> () {
let mut _0: (); // return place in scope 0 at $DIR/issue-73223.rs:1:11: 1:11
let _1: i32; // in scope 0 at $DIR/issue-73223.rs:2:9: 2:14
let mut _2: std::option::Option<i32>; // in scope 0 at $DIR/issue-73223.rs:2:23: 2:30
let mut _0: (); // return place in scope 0 at $DIR/issue-73223.rs:3:11: 3:11
let _1: i32; // in scope 0 at $DIR/issue-73223.rs:4:9: 4:14
let mut _2: std::option::Option<i32>; // in scope 0 at $DIR/issue-73223.rs:4:23: 4:30
let mut _4: (&i32, &i32); // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
let mut _5: bool; // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
let mut _6: bool; // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
Expand All @@ -20,10 +20,10 @@
let mut _16: std::fmt::ArgumentV1; // in scope 0 at $SRC_DIR/std/src/macros.rs:LL:COL
let mut _17: std::fmt::ArgumentV1; // in scope 0 at $SRC_DIR/std/src/macros.rs:LL:COL
scope 1 {
debug split => _1; // in scope 1 at $DIR/issue-73223.rs:2:9: 2:14
let _3: std::option::Option<i32>; // in scope 1 at $DIR/issue-73223.rs:7:9: 7:14
debug split => _1; // in scope 1 at $DIR/issue-73223.rs:4:9: 4:14
let _3: std::option::Option<i32>; // in scope 1 at $DIR/issue-73223.rs:9:9: 9:14
scope 3 {
debug _prev => _3; // in scope 3 at $DIR/issue-73223.rs:7:9: 7:14
debug _prev => _3; // in scope 3 at $DIR/issue-73223.rs:9:9: 9:14
scope 4 {
debug left_val => _13; // in scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
debug right_val => _15; // in scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
Expand Down Expand Up @@ -55,21 +55,21 @@
}
}
scope 2 {
debug v => _1; // in scope 2 at $DIR/issue-73223.rs:3:14: 3:15
debug v => _1; // in scope 2 at $DIR/issue-73223.rs:5:14: 5:15
}
scope 7 {
}
scope 9 {
}

bb0: {
StorageLive(_2); // scope 0 at $DIR/issue-73223.rs:2:23: 2:30
((_2 as Some).0: i32) = const 1_i32; // scope 0 at $DIR/issue-73223.rs:2:23: 2:30
discriminant(_2) = 1; // scope 0 at $DIR/issue-73223.rs:2:23: 2:30
_1 = ((_2 as Some).0: i32); // scope 0 at $DIR/issue-73223.rs:3:14: 3:15
StorageDead(_2); // scope 0 at $DIR/issue-73223.rs:5:6: 5:7
((_3 as Some).0: i32) = _1; // scope 1 at $DIR/issue-73223.rs:7:22: 7:27
discriminant(_3) = 1; // scope 1 at $DIR/issue-73223.rs:7:17: 7:28
StorageLive(_2); // scope 0 at $DIR/issue-73223.rs:4:23: 4:30
((_2 as Some).0: i32) = const 1_i32; // scope 0 at $DIR/issue-73223.rs:4:23: 4:30
discriminant(_2) = 1; // scope 0 at $DIR/issue-73223.rs:4:23: 4:30
_1 = ((_2 as Some).0: i32); // scope 0 at $DIR/issue-73223.rs:5:14: 5:15
StorageDead(_2); // scope 0 at $DIR/issue-73223.rs:7:6: 7:7
((_3 as Some).0: i32) = _1; // scope 1 at $DIR/issue-73223.rs:9:22: 9:27
discriminant(_3) = 1; // scope 1 at $DIR/issue-73223.rs:9:17: 9:28
(_4.0: &i32) = &_1; // scope 3 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
(_4.1: &i32) = const main::promoted[1]; // scope 3 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
// ty::Const
Expand All @@ -93,8 +93,8 @@

bb1: {
StorageDead(_5); // scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
_0 = const (); // scope 0 at $DIR/issue-73223.rs:1:11: 9:2
return; // scope 0 at $DIR/issue-73223.rs:9:2: 9:2
_0 = const (); // scope 0 at $DIR/issue-73223.rs:3:11: 11:2
return; // scope 0 at $DIR/issue-73223.rs:11:2: 11:2
}

bb2: {
Expand Down
Loading

0 comments on commit eb3eb85

Please sign in to comment.