Skip to content

Commit

Permalink
Rollup merge of rust-lang#76899 - wesleywiser:experimental_unsound_mi…
Browse files Browse the repository at this point in the history
…r_opts_flag, r=oli-obk

[mir-opt] Introduce a new flag to enable experimental/unsound mir opts

This implements part of rust-lang/compiler-team#319. The exact name of this flag was not decided as part of that MCP and some people expressed that it should include "unsound" in some way.

I've chosen to use `enable-experimental-unsound-mir-opts` as the name. While long, I don't think that matters too much as really it will only be used by some mir-opt tests. If you object or have a better name, please leave a comment!

r? @oli-obk
cc @rust-lang/wg-mir-opt @RalfJung
  • Loading branch information
Dylan-DPC committed Sep 22, 2020
2 parents 0b0a98a + 3498511 commit aa2dcb3
Show file tree
Hide file tree
Showing 13 changed files with 221 additions and 170 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-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
28 changes: 18 additions & 10 deletions src/test/mir-opt/inline/inline_any_operand.bar.Inline.after.mir
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ fn bar() -> bool {
let mut _0: bool; // return place in scope 0 at $DIR/inline-any-operand.rs:10:13: 10:17
let _1: fn(i32, i32) -> bool {foo}; // in scope 0 at $DIR/inline-any-operand.rs:11:9: 11:10
let mut _2: fn(i32, i32) -> bool {foo}; // in scope 0 at $DIR/inline-any-operand.rs:12:5: 12:6
let mut _3: i32; // in scope 0 at $DIR/inline-any-operand.rs:12:5: 12:13
let mut _4: i32; // in scope 0 at $DIR/inline-any-operand.rs:12:5: 12:13
let mut _5: i32; // in scope 0 at $DIR/inline-any-operand.rs:12:5: 12:13
let mut _6: i32; // in scope 0 at $DIR/inline-any-operand.rs:12:5: 12:13
scope 1 {
debug f => _1; // in scope 1 at $DIR/inline-any-operand.rs:11:9: 11:10
scope 2 {
debug x => _3; // in scope 2 at $DIR/inline-any-operand.rs:16:8: 16:9
debug y => _4; // in scope 2 at $DIR/inline-any-operand.rs:16:16: 16:17
debug x => _5; // in scope 2 at $DIR/inline-any-operand.rs:16:8: 16:9
debug y => _6; // in scope 2 at $DIR/inline-any-operand.rs:16:16: 16:17
let mut _3: i32; // in scope 2 at $DIR/inline-any-operand.rs:12:5: 12:13
let mut _4: i32; // in scope 2 at $DIR/inline-any-operand.rs:12:5: 12:13
}
}

Expand All @@ -22,13 +24,19 @@ fn bar() -> bool {
// + literal: Const { ty: fn(i32, i32) -> bool {foo}, val: Value(Scalar(<ZST>)) }
StorageLive(_2); // scope 1 at $DIR/inline-any-operand.rs:12:5: 12:6
_2 = _1; // scope 1 at $DIR/inline-any-operand.rs:12:5: 12:6
StorageLive(_3); // scope 1 at $DIR/inline-any-operand.rs:12:5: 12:13
_3 = const 1_i32; // scope 1 at $DIR/inline-any-operand.rs:12:5: 12:13
StorageLive(_4); // scope 1 at $DIR/inline-any-operand.rs:12:5: 12:13
_4 = const -1_i32; // scope 1 at $DIR/inline-any-operand.rs:12:5: 12:13
StorageLive(_5); // scope 1 at $DIR/inline-any-operand.rs:12:5: 12:13
_5 = const 1_i32; // scope 1 at $DIR/inline-any-operand.rs:12:5: 12:13
StorageLive(_6); // scope 1 at $DIR/inline-any-operand.rs:12:5: 12:13
_6 = const -1_i32; // scope 1 at $DIR/inline-any-operand.rs:12:5: 12:13
StorageLive(_3); // scope 2 at $DIR/inline-any-operand.rs:17:5: 17:6
_3 = _5; // scope 2 at $DIR/inline-any-operand.rs:17:5: 17:6
StorageLive(_4); // scope 2 at $DIR/inline-any-operand.rs:17:10: 17:11
_4 = _6; // scope 2 at $DIR/inline-any-operand.rs:17:10: 17:11
_0 = Eq(move _3, move _4); // scope 2 at $DIR/inline-any-operand.rs:17:5: 17:11
StorageDead(_4); // scope 1 at $DIR/inline-any-operand.rs:12:5: 12:13
StorageDead(_3); // scope 1 at $DIR/inline-any-operand.rs:12:5: 12:13
StorageDead(_4); // scope 2 at $DIR/inline-any-operand.rs:17:10: 17:11
StorageDead(_3); // scope 2 at $DIR/inline-any-operand.rs:17:10: 17:11
StorageDead(_6); // scope 1 at $DIR/inline-any-operand.rs:12:5: 12:13
StorageDead(_5); // scope 1 at $DIR/inline-any-operand.rs:12:5: 12:13
StorageDead(_2); // scope 1 at $DIR/inline-any-operand.rs:12:12: 12:13
StorageDead(_1); // scope 0 at $DIR/inline-any-operand.rs:13:1: 13:2
return; // scope 0 at $DIR/inline-any-operand.rs:13:2: 13:2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ fn test2(_1: &dyn X) -> bool {
let mut _3: &dyn X; // in scope 0 at $DIR/inline-trait-method_2.rs:5:10: 5:11
scope 1 {
debug x => _2; // in scope 1 at $DIR/inline-trait-method_2.rs:9:9: 9:10
let mut _4: &dyn X; // in scope 1 at $DIR/inline-trait-method_2.rs:5:5: 5:12
}

bb0: {
Expand All @@ -15,13 +16,16 @@ fn test2(_1: &dyn X) -> bool {
_3 = &(*_1); // scope 0 at $DIR/inline-trait-method_2.rs:5:10: 5:11
_2 = move _3 as &dyn X (Pointer(Unsize)); // scope 0 at $DIR/inline-trait-method_2.rs:5:10: 5:11
StorageDead(_3); // scope 0 at $DIR/inline-trait-method_2.rs:5:10: 5:11
_0 = <dyn X as X>::y(move _2) -> bb1; // scope 1 at $DIR/inline-trait-method_2.rs:10:5: 10:10
StorageLive(_4); // scope 1 at $DIR/inline-trait-method_2.rs:10:5: 10:6
_4 = _2; // scope 1 at $DIR/inline-trait-method_2.rs:10:5: 10:6
_0 = <dyn X as X>::y(move _4) -> bb1; // scope 1 at $DIR/inline-trait-method_2.rs:10:5: 10:10
// mir::Constant
// + span: $DIR/inline-trait-method_2.rs:10:7: 10:8
// + literal: Const { ty: for<'r> fn(&'r dyn X) -> bool {<dyn X as X>::y}, val: Value(Scalar(<ZST>)) }
}

bb1: {
StorageDead(_4); // scope 1 at $DIR/inline-trait-method_2.rs:10:9: 10:10
StorageDead(_2); // scope 0 at $DIR/inline-trait-method_2.rs:5:11: 5:12
return; // scope 0 at $DIR/inline-trait-method_2.rs:6:2: 6:2
}
Expand Down
Loading

0 comments on commit aa2dcb3

Please sign in to comment.