Skip to content

Commit a0d98ff

Browse files
committed
Auto merge of rust-lang#132356 - jieyouxu:unsound-simplify_aggregate_to_copy, r=cjgillot,DianQK
Mark `simplify_aggregate_to_copy` mir-opt as unsound Mark the `simplify_aggregate_to_copy` mir-opt added in rust-lang#128299 as unsound as it seems to miscompile the MCVE reported in rust-lang#132353. The mir-opt can be re-enabled once this case is fixed. ```rs fn pop_min(mut score2head: Vec<Option<usize>>) -> Option<usize> { loop { if let Some(col) = score2head[0] { score2head[0] = None; return Some(col); } } } fn main() { let min = pop_min(vec![Some(1)]); println!("min: {:?}", min); // panic happens here on beta in release mode // but not in debug mode min.unwrap(); } ``` This MCVE is included as a `run-pass` ui regression test in the first commit. I built the ui test with a nightly manually, and can reproduce the behavioral difference with `-C opt-level=0` and `-C opt-level=1`. Locally, this ui test will fail unless it was run on a compiler built with the second commit marking the mir-opt as unsound thus disabling it by default. This PR **partially reverts** commit e7386b3, reversing changes made to 02b1be1. The mir-opt implementation is just marked as unsound but **not** reverted to make reland reviews easier. Test changes are **reverted if they were not pure additions**. Tests added by the original PR received `-Z unsound-mir-opts` compile-flags. cc `@DianQK` `@cjgillot` (PR author and reviewer of rust-lang#128299)
2 parents 20c909f + cfb4c05 commit a0d98ff

14 files changed

+192
-43
lines changed

compiler/rustc_mir_transform/src/gvn.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1082,7 +1082,9 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
10821082
}
10831083
}
10841084

1085-
if let AggregateTy::Def(_, _) = ty
1085+
// unsound: https://github.com/rust-lang/rust/issues/132353
1086+
if tcx.sess.opts.unstable_opts.unsound_mir_opts
1087+
&& let AggregateTy::Def(_, _) = ty
10861088
&& let Some(value) =
10871089
self.simplify_aggregate_to_copy(rvalue, location, &fields, variant_index)
10881090
{

tests/codegen/clone_as_copy.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
//@ revisions: DEBUGINFO NODEBUGINFO
2+
//@ compile-flags: -Zunsound-mir-opts
3+
// FIXME: see <https://github.com/rust-lang/rust/issues/132353>
24
//@ compile-flags: -O -Cno-prepopulate-passes
35
//@ [DEBUGINFO] compile-flags: -Cdebuginfo=full
46

tests/codegen/try_question_mark_nop.rs

+4-10
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
//@ compile-flags: -O -Z merge-functions=disabled --edition=2021
22
//@ only-x86_64
33
// FIXME: Remove the `min-llvm-version`.
4-
//@ revisions: NINETEEN TWENTY
5-
//@[NINETEEN] min-llvm-version: 19
6-
//@[NINETEEN] ignore-llvm-version: 20-99
7-
//@[TWENTY] min-llvm-version: 20
4+
//@ min-llvm-version: 19
85

96
#![crate_type = "lib"]
107
#![feature(try_blocks)]
@@ -16,12 +13,9 @@ use std::ptr::NonNull;
1613
#[no_mangle]
1714
pub fn option_nop_match_32(x: Option<u32>) -> Option<u32> {
1815
// CHECK: start:
19-
// NINETEEN-NEXT: [[TRUNC:%.*]] = trunc nuw i32 %0 to i1
20-
// NINETEEN-NEXT: [[FIRST:%.*]] = select i1 [[TRUNC]], i32 %0
21-
// NINETEEN-NEXT: insertvalue { i32, i32 } poison, i32 [[FIRST]], 0
22-
// TWENTY-NEXT: insertvalue { i32, i32 } poison, i32 %0, 0
23-
// CHECK-NEXT: insertvalue { i32, i32 }
24-
// CHECK-NEXT: ret { i32, i32 }
16+
// CHECK-NEXT: [[REG1:%.*]] = insertvalue { i32, i32 } poison, i32 %0, 0
17+
// CHECK-NEXT: [[REG2:%.*]] = insertvalue { i32, i32 } [[REG1]], i32 %1, 1
18+
// CHECK-NEXT: ret { i32, i32 } [[REG2]]
2519
match x {
2620
Some(x) => Some(x),
2721
None => None,

tests/mir-opt/gvn_clone.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//@ compile-flags: -Zunsound-mir-opts
2+
// FIXME: see <https://github.com/rust-lang/rust/issues/132353>
13
//@ test-mir-pass: GVN
24
//@ compile-flags: -Zmir-enable-passes=+InstSimplify-before-inline
35

tests/mir-opt/gvn_clone.{impl#0}-clone.GVN.diff

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
- // MIR for `<impl at $DIR/gvn_clone.rs:12:10: 12:15>::clone` before GVN
2-
+ // MIR for `<impl at $DIR/gvn_clone.rs:12:10: 12:15>::clone` after GVN
1+
- // MIR for `<impl at $DIR/gvn_clone.rs:14:10: 14:15>::clone` before GVN
2+
+ // MIR for `<impl at $DIR/gvn_clone.rs:14:10: 14:15>::clone` after GVN
33

4-
fn <impl at $DIR/gvn_clone.rs:12:10: 12:15>::clone(_1: &AllCopy) -> AllCopy {
4+
fn <impl at $DIR/gvn_clone.rs:14:10: 14:15>::clone(_1: &AllCopy) -> AllCopy {
55
debug self => _1;
66
let mut _0: AllCopy;
77
let mut _2: i32;

tests/mir-opt/gvn_copy_aggregate.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//@ compile-flags: -Zunsound-mir-opts
2+
// FIXME: see <https://github.com/rust-lang/rust/issues/132353.
13
//@ test-mir-pass: GVN
24
//@ compile-flags: -Cpanic=abort
35

tests/mir-opt/pre-codegen/clone_as_copy.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//@ compile-flags: -Zunsound-mir-opts
2+
// FIXME: see <https://github.com/rust-lang/rust/issues/132353>
13
//@ compile-flags: -Cdebuginfo=full
24

35
// Check if we have transformed the nested clone to the copy in the complete pipeline.

tests/mir-opt/pre-codegen/no_inlined_clone.{impl#0}-clone.PreCodegen.after.mir

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@
33
fn <impl at $DIR/no_inlined_clone.rs:9:10: 9:15>::clone(_1: &Foo) -> Foo {
44
debug self => _1;
55
let mut _0: Foo;
6+
let mut _2: i32;
67

78
bb0: {
8-
_0 = copy (*_1);
9+
StorageLive(_2);
10+
_2 = copy ((*_1).0: i32);
11+
_0 = Foo { a: move _2 };
12+
StorageDead(_2);
913
return;
1014
}
1115
}

tests/mir-opt/pre-codegen/try_identity.old.PreCodegen.after.mir

+4-4
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ fn old(_1: Result<T, E>) -> Result<T, E> {
1919
}
2020

2121
bb1: {
22-
_3 = copy ((_1 as Ok).0: T);
23-
_0 = copy _1;
22+
_3 = move ((_1 as Ok).0: T);
23+
_0 = Result::<T, E>::Ok(copy _3);
2424
goto -> bb3;
2525
}
2626

2727
bb2: {
28-
_4 = copy ((_1 as Err).0: E);
29-
_0 = copy _1;
28+
_4 = move ((_1 as Err).0: E);
29+
_0 = Result::<T, E>::Err(copy _4);
3030
goto -> bb3;
3131
}
3232

tests/mir-opt/pre-codegen/vec_deref.vec_deref_to_slice.PreCodegen.after.panic-abort.mir

+18-12
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ fn vec_deref_to_slice(_1: &Vec<u8>) -> &[u8] {
77
debug self => _1;
88
scope 2 (inlined Vec::<u8>::as_slice) {
99
debug self => _1;
10-
let mut _6: usize;
10+
let mut _7: usize;
1111
scope 3 (inlined Vec::<u8>::as_ptr) {
1212
debug self => _1;
1313
let mut _2: &alloc::raw_vec::RawVec<u8>;
@@ -16,6 +16,7 @@ fn vec_deref_to_slice(_1: &Vec<u8>) -> &[u8] {
1616
let mut _3: &alloc::raw_vec::RawVecInner;
1717
scope 5 (inlined alloc::raw_vec::RawVecInner::ptr::<u8>) {
1818
debug self => _3;
19+
let mut _6: std::ptr::NonNull<u8>;
1920
scope 6 (inlined alloc::raw_vec::RawVecInner::non_null::<u8>) {
2021
debug self => _3;
2122
let mut _4: std::ptr::NonNull<u8>;
@@ -31,20 +32,20 @@ fn vec_deref_to_slice(_1: &Vec<u8>) -> &[u8] {
3132
}
3233
}
3334
scope 10 (inlined Unique::<u8>::as_non_null_ptr) {
34-
debug ((self: Unique<u8>).0: std::ptr::NonNull<u8>) => _4;
35+
debug ((self: Unique<u8>).0: std::ptr::NonNull<u8>) => _6;
3536
debug ((self: Unique<u8>).1: std::marker::PhantomData<u8>) => const PhantomData::<u8>;
3637
}
3738
}
3839
scope 11 (inlined NonNull::<u8>::as_ptr) {
39-
debug self => _4;
40+
debug self => _6;
4041
}
4142
}
4243
}
4344
}
4445
scope 12 (inlined std::slice::from_raw_parts::<'_, u8>) {
4546
debug data => _5;
46-
debug len => _6;
47-
let _7: *const [u8];
47+
debug len => _7;
48+
let _8: *const [u8];
4849
scope 13 (inlined core::ub_checks::check_language_ub) {
4950
scope 14 (inlined core::ub_checks::check_language_ub::runtime) {
5051
}
@@ -55,10 +56,10 @@ fn vec_deref_to_slice(_1: &Vec<u8>) -> &[u8] {
5556
}
5657
scope 17 (inlined slice_from_raw_parts::<u8>) {
5758
debug data => _5;
58-
debug len => _6;
59+
debug len => _7;
5960
scope 18 (inlined std::ptr::from_raw_parts::<[u8], u8>) {
6061
debug data_pointer => _5;
61-
debug metadata => _6;
62+
debug metadata => _7;
6263
}
6364
}
6465
}
@@ -70,17 +71,22 @@ fn vec_deref_to_slice(_1: &Vec<u8>) -> &[u8] {
7071
_2 = &((*_1).0: alloc::raw_vec::RawVec<u8>);
7172
StorageLive(_3);
7273
_3 = &(((*_1).0: alloc::raw_vec::RawVec<u8>).0: alloc::raw_vec::RawVecInner);
74+
StorageLive(_6);
75+
StorageLive(_4);
7376
_4 = copy (((((*_1).0: alloc::raw_vec::RawVec<u8>).0: alloc::raw_vec::RawVecInner).0: std::ptr::Unique<u8>).0: std::ptr::NonNull<u8>);
7477
_5 = copy (_4.0: *const u8);
78+
_6 = NonNull::<u8> { pointer: copy _5 };
79+
StorageDead(_4);
80+
StorageDead(_6);
7581
StorageDead(_3);
7682
StorageDead(_2);
77-
StorageLive(_6);
78-
_6 = copy ((*_1).1: usize);
7983
StorageLive(_7);
80-
_7 = *const [u8] from (copy _5, copy _6);
81-
_0 = &(*_7);
84+
_7 = copy ((*_1).1: usize);
85+
StorageLive(_8);
86+
_8 = *const [u8] from (copy _5, copy _7);
87+
_0 = &(*_8);
88+
StorageDead(_8);
8289
StorageDead(_7);
83-
StorageDead(_6);
8490
return;
8591
}
8692
}

tests/mir-opt/pre-codegen/vec_deref.vec_deref_to_slice.PreCodegen.after.panic-unwind.mir

+18-12
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ fn vec_deref_to_slice(_1: &Vec<u8>) -> &[u8] {
77
debug self => _1;
88
scope 2 (inlined Vec::<u8>::as_slice) {
99
debug self => _1;
10-
let mut _6: usize;
10+
let mut _7: usize;
1111
scope 3 (inlined Vec::<u8>::as_ptr) {
1212
debug self => _1;
1313
let mut _2: &alloc::raw_vec::RawVec<u8>;
@@ -16,6 +16,7 @@ fn vec_deref_to_slice(_1: &Vec<u8>) -> &[u8] {
1616
let mut _3: &alloc::raw_vec::RawVecInner;
1717
scope 5 (inlined alloc::raw_vec::RawVecInner::ptr::<u8>) {
1818
debug self => _3;
19+
let mut _6: std::ptr::NonNull<u8>;
1920
scope 6 (inlined alloc::raw_vec::RawVecInner::non_null::<u8>) {
2021
debug self => _3;
2122
let mut _4: std::ptr::NonNull<u8>;
@@ -31,20 +32,20 @@ fn vec_deref_to_slice(_1: &Vec<u8>) -> &[u8] {
3132
}
3233
}
3334
scope 10 (inlined Unique::<u8>::as_non_null_ptr) {
34-
debug ((self: Unique<u8>).0: std::ptr::NonNull<u8>) => _4;
35+
debug ((self: Unique<u8>).0: std::ptr::NonNull<u8>) => _6;
3536
debug ((self: Unique<u8>).1: std::marker::PhantomData<u8>) => const PhantomData::<u8>;
3637
}
3738
}
3839
scope 11 (inlined NonNull::<u8>::as_ptr) {
39-
debug self => _4;
40+
debug self => _6;
4041
}
4142
}
4243
}
4344
}
4445
scope 12 (inlined std::slice::from_raw_parts::<'_, u8>) {
4546
debug data => _5;
46-
debug len => _6;
47-
let _7: *const [u8];
47+
debug len => _7;
48+
let _8: *const [u8];
4849
scope 13 (inlined core::ub_checks::check_language_ub) {
4950
scope 14 (inlined core::ub_checks::check_language_ub::runtime) {
5051
}
@@ -55,10 +56,10 @@ fn vec_deref_to_slice(_1: &Vec<u8>) -> &[u8] {
5556
}
5657
scope 17 (inlined slice_from_raw_parts::<u8>) {
5758
debug data => _5;
58-
debug len => _6;
59+
debug len => _7;
5960
scope 18 (inlined std::ptr::from_raw_parts::<[u8], u8>) {
6061
debug data_pointer => _5;
61-
debug metadata => _6;
62+
debug metadata => _7;
6263
}
6364
}
6465
}
@@ -70,17 +71,22 @@ fn vec_deref_to_slice(_1: &Vec<u8>) -> &[u8] {
7071
_2 = &((*_1).0: alloc::raw_vec::RawVec<u8>);
7172
StorageLive(_3);
7273
_3 = &(((*_1).0: alloc::raw_vec::RawVec<u8>).0: alloc::raw_vec::RawVecInner);
74+
StorageLive(_6);
75+
StorageLive(_4);
7376
_4 = copy (((((*_1).0: alloc::raw_vec::RawVec<u8>).0: alloc::raw_vec::RawVecInner).0: std::ptr::Unique<u8>).0: std::ptr::NonNull<u8>);
7477
_5 = copy (_4.0: *const u8);
78+
_6 = NonNull::<u8> { pointer: copy _5 };
79+
StorageDead(_4);
80+
StorageDead(_6);
7581
StorageDead(_3);
7682
StorageDead(_2);
77-
StorageLive(_6);
78-
_6 = copy ((*_1).1: usize);
7983
StorageLive(_7);
80-
_7 = *const [u8] from (copy _5, copy _6);
81-
_0 = &(*_7);
84+
_7 = copy ((*_1).1: usize);
85+
StorageLive(_8);
86+
_8 = *const [u8] from (copy _5, copy _7);
87+
_0 = &(*_8);
88+
StorageDead(_8);
8289
StorageDead(_7);
83-
StorageDead(_6);
8490
return;
8591
}
8692
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
- // MIR for `foo` before GVN
2+
+ // MIR for `foo` after GVN
3+
4+
fn foo(_1: &mut Option<i32>) -> Option<i32> {
5+
debug v => _1;
6+
let mut _0: std::option::Option<i32>;
7+
let mut _2: &std::option::Option<i32>;
8+
let mut _3: &std::option::Option<i32>;
9+
let _4: &&mut std::option::Option<i32>;
10+
let mut _5: isize;
11+
let mut _7: !;
12+
let mut _8: std::option::Option<i32>;
13+
let mut _9: i32;
14+
let mut _10: !;
15+
let mut _11: &mut std::option::Option<i32>;
16+
scope 1 {
17+
debug col => _6;
18+
let _6: i32;
19+
}
20+
21+
bb0: {
22+
- StorageLive(_2);
23+
+ nop;
24+
StorageLive(_3);
25+
StorageLive(_4);
26+
_4 = &_1;
27+
- _11 = deref_copy (*_4);
28+
- _3 = &(*_11);
29+
+ _11 = copy _1;
30+
+ _3 = &(*_1);
31+
_2 = get(move _3) -> [return: bb1, unwind unreachable];
32+
}
33+
34+
bb1: {
35+
StorageDead(_3);
36+
_5 = discriminant((*_2));
37+
switchInt(move _5) -> [1: bb2, otherwise: bb3];
38+
}
39+
40+
bb2: {
41+
- StorageLive(_6);
42+
+ nop;
43+
_6 = copy (((*_2) as Some).0: i32);
44+
StorageLive(_8);
45+
- _8 = Option::<i32>::None;
46+
- (*_1) = move _8;
47+
+ _8 = const Option::<i32>::None;
48+
+ (*_1) = const Option::<i32>::None;
49+
StorageDead(_8);
50+
StorageLive(_9);
51+
_9 = copy _6;
52+
- _0 = Option::<i32>::Some(move _9);
53+
+ _0 = copy (*_2);
54+
StorageDead(_9);
55+
- StorageDead(_6);
56+
+ nop;
57+
StorageDead(_4);
58+
- StorageDead(_2);
59+
+ nop;
60+
return;
61+
}
62+
63+
bb3: {
64+
StorageLive(_10);
65+
unreachable;
66+
}
67+
+ }
68+
+
69+
+ ALLOC0 (size: 8, align: 4) {
70+
+ 00 00 00 00 __ __ __ __ │ ....░░░░
71+
}
72+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//! The `simplify_aggregate_to_copy` mir-opt introduced in
2+
//! <https://github.com/rust-lang/rust/pull/128299> caused a miscompile because the initial
3+
//! implementation
4+
//!
5+
//! > introduce[d] new dereferences without checking for aliasing
6+
//!
7+
//! This test demonstrates the behavior, and should be adjusted or removed when fixing and relanding
8+
//! the mir-opt.
9+
#![crate_type = "lib"]
10+
// skip-filecheck
11+
//@ compile-flags: -O -Zunsound-mir-opts
12+
//@ test-mir-pass: GVN
13+
#![allow(internal_features)]
14+
#![feature(rustc_attrs, core_intrinsics)]
15+
16+
// EMIT_MIR simplify_aggregate_to_copy_miscompile.foo.GVN.diff
17+
#[no_mangle]
18+
fn foo(v: &mut Option<i32>) -> Option<i32> {
19+
if let &Some(col) = get(&v) {
20+
*v = None;
21+
return Some(col);
22+
} else {
23+
unsafe { std::intrinsics::unreachable() }
24+
}
25+
}
26+
27+
#[no_mangle]
28+
#[inline(never)]
29+
#[rustc_nounwind]
30+
fn get(v: &Option<i32>) -> &Option<i32> {
31+
v
32+
}

0 commit comments

Comments
 (0)