Skip to content

Commit 5b36a0a

Browse files
dianqkcuviper
authored andcommitted
Add miscompiled test cases
(cherry picked from commit 64c023b)
1 parent 00c0648 commit 5b36a0a

File tree

3 files changed

+180
-0
lines changed

3 files changed

+180
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
- // MIR for `loop_deref_mut` before GVN
2+
+ // MIR for `loop_deref_mut` after GVN
3+
4+
fn loop_deref_mut(_1: &mut Value) -> Value {
5+
debug val => _1;
6+
let mut _0: Value;
7+
let _2: &Value;
8+
let _3: &Value;
9+
let mut _4: &Value;
10+
let mut _6: !;
11+
let mut _8: isize;
12+
let mut _9: !;
13+
let mut _10: ();
14+
let mut _12: i32;
15+
let _13: ();
16+
let mut _14: bool;
17+
let mut _15: !;
18+
let mut _16: Value;
19+
scope 1 {
20+
debug val_alias => _2;
21+
let mut _5: bool;
22+
scope 2 {
23+
debug stop => _5;
24+
let _7: i32;
25+
scope 3 {
26+
debug v => _7;
27+
let _11: Value;
28+
scope 4 {
29+
debug v => _11;
30+
}
31+
}
32+
}
33+
}
34+
35+
bb0: {
36+
StorageLive(_2);
37+
- StorageLive(_3);
38+
+ nop;
39+
StorageLive(_4);
40+
_4 = &(*_1);
41+
_3 = get::<Value>(move _4) -> [return: bb1, unwind unreachable];
42+
}
43+
44+
bb1: {
45+
_2 = &(*_3);
46+
StorageDead(_4);
47+
- StorageDead(_3);
48+
+ nop;
49+
StorageLive(_5);
50+
_5 = const false;
51+
- _8 = discriminant((*_2));
52+
+ _8 = discriminant((*_3));
53+
switchInt(move _8) -> [0: bb3, otherwise: bb2];
54+
}
55+
56+
bb2: {
57+
unreachable;
58+
}
59+
60+
bb3: {
61+
- StorageLive(_7);
62+
- _7 = copy (((*_2) as V0).0: i32);
63+
+ nop;
64+
+ _7 = copy (((*_3) as V0).0: i32);
65+
StorageLive(_9);
66+
goto -> bb4;
67+
}
68+
69+
bb4: {
70+
StorageLive(_11);
71+
StorageLive(_12);
72+
_12 = copy _7;
73+
- _11 = Value::V0(move _12);
74+
+ _11 = copy (*_3);
75+
StorageDead(_12);
76+
StorageLive(_13);
77+
StorageLive(_14);
78+
_14 = copy _5;
79+
switchInt(move _14) -> [0: bb6, otherwise: bb5];
80+
}
81+
82+
bb5: {
83+
_0 = move _11;
84+
StorageDead(_14);
85+
StorageDead(_13);
86+
StorageDead(_11);
87+
StorageDead(_9);
88+
- StorageDead(_7);
89+
+ nop;
90+
StorageDead(_5);
91+
StorageDead(_2);
92+
return;
93+
}
94+
95+
bb6: {
96+
_13 = const ();
97+
StorageDead(_14);
98+
StorageDead(_13);
99+
_5 = const true;
100+
StorageLive(_16);
101+
- _16 = Value::V1;
102+
- (*_1) = move _16;
103+
+ _16 = const Value::V1;
104+
+ (*_1) = const Value::V1;
105+
StorageDead(_16);
106+
_10 = const ();
107+
StorageDead(_11);
108+
goto -> bb4;
109+
}
110+
+ }
111+
+
112+
+ ALLOC0 (size: 8, align: 4) {
113+
+ 01 00 00 00 __ __ __ __ │ ....░░░░
114+
}
115+

tests/mir-opt/gvn_loop.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// skip-filecheck
2+
//@ test-mir-pass: GVN
3+
4+
#![crate_type = "lib"]
5+
#![feature(core_intrinsics, rustc_attrs)]
6+
7+
pub enum Value {
8+
V0(i32),
9+
V1,
10+
}
11+
12+
// EMIT_MIR gvn_loop.loop_deref_mut.GVN.diff
13+
fn loop_deref_mut(val: &mut Value) -> Value {
14+
let val_alias: &Value = get(val);
15+
let mut stop = false;
16+
let Value::V0(v) = *val_alias else { unsafe { core::intrinsics::unreachable() } };
17+
loop {
18+
let v = Value::V0(v);
19+
if stop {
20+
return v;
21+
}
22+
stop = true;
23+
*val = Value::V1;
24+
}
25+
}
26+
27+
#[inline(never)]
28+
#[rustc_nounwind]
29+
fn get<T>(v: &T) -> &T {
30+
v
31+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//@ compile-flags: -O
2+
//@ run-fail
3+
4+
pub enum Value {
5+
V0(i32),
6+
V1,
7+
}
8+
9+
fn set_discriminant(val: &mut Value) -> Value {
10+
let val_alias: &Value = get(val);
11+
let mut stop = false;
12+
let Value::V0(v) = *val_alias else {
13+
unreachable!();
14+
};
15+
loop {
16+
let v = Value::V0(v);
17+
if stop {
18+
return v;
19+
}
20+
stop = true;
21+
*val = Value::V1;
22+
}
23+
}
24+
25+
fn main() {
26+
let mut v = Value::V0(1);
27+
let v = set_discriminant(&mut v);
28+
assert!(matches!(v, Value::V0(1)));
29+
}
30+
31+
#[inline(never)]
32+
fn get<T>(v: &T) -> &T {
33+
v
34+
}

0 commit comments

Comments
 (0)