Skip to content

Commit d62d3f7

Browse files
committed
Auto merge of #76899 - wesleywiser:experimental_unsound_mir_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`
2 parents 535d27a + c653af8 commit d62d3f7

11 files changed

+263
-216
lines changed

compiler/rustc_mir/src/transform/copy_prop.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@ pub struct CopyPropagation;
3131

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

compiler/rustc_session/src/options.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1115,6 +1115,8 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
11151115
`hir,typed` (HIR with types for each node),
11161116
`hir-tree` (dump the raw HIR),
11171117
`mir` (the MIR), or `mir-cfg` (graphviz formatted MIR)"),
1118+
unsound_mir_opts: bool = (false, parse_bool, [TRACKED],
1119+
"enable unsound and buggy MIR optimizations (default: no)"),
11181120
unstable_options: bool = (false, parse_bool, [UNTRACKED],
11191121
"adds unstable command line options to rustc interface (default: no)"),
11201122
use_ctors_section: Option<bool> = (None, parse_opt_bool, [TRACKED],

src/test/mir-opt/copy_propagation.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// compile-flags: -Zunsound-mir-opts
12
// EMIT_MIR copy_propagation.test.CopyPropagation.diff
23

34
fn test(x: u32) -> u32 {

src/test/mir-opt/copy_propagation.test.CopyPropagation.diff

+9-9
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@
22
+ // MIR for `test` after CopyPropagation
33

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

1212
bb0: {
13-
nop; // scope 0 at $DIR/copy_propagation.rs:4:9: 4:10
14-
_0 = _1; // scope 0 at $DIR/copy_propagation.rs:4:13: 4:14
15-
nop; // scope 1 at $DIR/copy_propagation.rs:5:5: 5:6
16-
nop; // scope 0 at $DIR/copy_propagation.rs:6:1: 6:2
17-
return; // scope 0 at $DIR/copy_propagation.rs:6:2: 6:2
13+
nop; // scope 0 at $DIR/copy_propagation.rs:5:9: 5:10
14+
_0 = _1; // scope 0 at $DIR/copy_propagation.rs:5:13: 5:14
15+
nop; // scope 1 at $DIR/copy_propagation.rs:6:5: 6:6
16+
nop; // scope 0 at $DIR/copy_propagation.rs:7:1: 7:2
17+
return; // scope 0 at $DIR/copy_propagation.rs:7:2: 7:2
1818
}
1919
}
2020

src/test/mir-opt/early_otherwise_branch_68867.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// ignore-tidy-linelength
2-
// compile-flags: -Z mir-opt-level=3
2+
// compile-flags: -Z mir-opt-level=3 -Zunsound-mir-opts
33

44
// example from #68867
55
type CSSFloat = f32;

src/test/mir-opt/inline/inline-closure-borrows-arg.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// compile-flags: -Z span_free_formats
1+
// compile-flags: -Z span_free_formats -Zunsound-mir-opts
22

33
// Tests that MIR inliner can handle closure arguments,
44
// even when (#45894)

src/test/mir-opt/inline/inline_any_operand.bar.Inline.after.mir

+18-10
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ fn bar() -> bool {
44
let mut _0: bool; // return place in scope 0 at $DIR/inline-any-operand.rs:10:13: 10:17
55
let _1: fn(i32, i32) -> bool {foo}; // in scope 0 at $DIR/inline-any-operand.rs:11:9: 11:10
66
let mut _2: fn(i32, i32) -> bool {foo}; // in scope 0 at $DIR/inline-any-operand.rs:12:5: 12:6
7-
let mut _3: i32; // in scope 0 at $DIR/inline-any-operand.rs:12:5: 12:13
8-
let mut _4: i32; // in scope 0 at $DIR/inline-any-operand.rs:12:5: 12:13
7+
let mut _5: i32; // in scope 0 at $DIR/inline-any-operand.rs:12:5: 12:13
8+
let mut _6: i32; // in scope 0 at $DIR/inline-any-operand.rs:12:5: 12:13
99
scope 1 {
1010
debug f => _1; // in scope 1 at $DIR/inline-any-operand.rs:11:9: 11:10
1111
scope 2 {
12-
debug x => _3; // in scope 2 at $DIR/inline-any-operand.rs:16:8: 16:9
13-
debug y => _4; // in scope 2 at $DIR/inline-any-operand.rs:16:16: 16:17
12+
debug x => _5; // in scope 2 at $DIR/inline-any-operand.rs:16:8: 16:9
13+
debug y => _6; // in scope 2 at $DIR/inline-any-operand.rs:16:16: 16:17
14+
let mut _3: i32; // in scope 2 at $DIR/inline-any-operand.rs:12:5: 12:13
15+
let mut _4: i32; // in scope 2 at $DIR/inline-any-operand.rs:12:5: 12:13
1416
}
1517
}
1618

@@ -22,13 +24,19 @@ fn bar() -> bool {
2224
// + literal: Const { ty: fn(i32, i32) -> bool {foo}, val: Value(Scalar(<ZST>)) }
2325
StorageLive(_2); // scope 1 at $DIR/inline-any-operand.rs:12:5: 12:6
2426
_2 = _1; // scope 1 at $DIR/inline-any-operand.rs:12:5: 12:6
25-
StorageLive(_3); // scope 1 at $DIR/inline-any-operand.rs:12:5: 12:13
26-
_3 = const 1_i32; // scope 1 at $DIR/inline-any-operand.rs:12:5: 12:13
27-
StorageLive(_4); // scope 1 at $DIR/inline-any-operand.rs:12:5: 12:13
28-
_4 = const -1_i32; // scope 1 at $DIR/inline-any-operand.rs:12:5: 12:13
27+
StorageLive(_5); // scope 1 at $DIR/inline-any-operand.rs:12:5: 12:13
28+
_5 = const 1_i32; // scope 1 at $DIR/inline-any-operand.rs:12:5: 12:13
29+
StorageLive(_6); // scope 1 at $DIR/inline-any-operand.rs:12:5: 12:13
30+
_6 = const -1_i32; // scope 1 at $DIR/inline-any-operand.rs:12:5: 12:13
31+
StorageLive(_3); // scope 2 at $DIR/inline-any-operand.rs:17:5: 17:6
32+
_3 = _5; // scope 2 at $DIR/inline-any-operand.rs:17:5: 17:6
33+
StorageLive(_4); // scope 2 at $DIR/inline-any-operand.rs:17:10: 17:11
34+
_4 = _6; // scope 2 at $DIR/inline-any-operand.rs:17:10: 17:11
2935
_0 = Eq(move _3, move _4); // scope 2 at $DIR/inline-any-operand.rs:17:5: 17:11
30-
StorageDead(_4); // scope 1 at $DIR/inline-any-operand.rs:12:5: 12:13
31-
StorageDead(_3); // scope 1 at $DIR/inline-any-operand.rs:12:5: 12:13
36+
StorageDead(_4); // scope 2 at $DIR/inline-any-operand.rs:17:10: 17:11
37+
StorageDead(_3); // scope 2 at $DIR/inline-any-operand.rs:17:10: 17:11
38+
StorageDead(_6); // scope 1 at $DIR/inline-any-operand.rs:12:5: 12:13
39+
StorageDead(_5); // scope 1 at $DIR/inline-any-operand.rs:12:5: 12:13
3240
StorageDead(_2); // scope 1 at $DIR/inline-any-operand.rs:12:12: 12:13
3341
StorageDead(_1); // scope 0 at $DIR/inline-any-operand.rs:13:1: 13:2
3442
return; // scope 0 at $DIR/inline-any-operand.rs:13:2: 13:2

src/test/mir-opt/inline/inline_trait_method_2.test2.Inline.after.mir

+5-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ fn test2(_1: &dyn X) -> bool {
77
let mut _3: &dyn X; // in scope 0 at $DIR/inline-trait-method_2.rs:5:10: 5:11
88
scope 1 {
99
debug x => _2; // in scope 1 at $DIR/inline-trait-method_2.rs:9:9: 9:10
10+
let mut _4: &dyn X; // in scope 1 at $DIR/inline-trait-method_2.rs:5:5: 5:12
1011
}
1112

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

2427
bb1: {
28+
StorageDead(_4); // scope 1 at $DIR/inline-trait-method_2.rs:10:9: 10:10
2529
StorageDead(_2); // scope 0 at $DIR/inline-trait-method_2.rs:5:11: 5:12
2630
return; // scope 0 at $DIR/inline-trait-method_2.rs:6:2: 6:2
2731
}

0 commit comments

Comments
 (0)