Skip to content

Commit 8df9dbc

Browse files
committed
Add miscompiled test cases for GVN
1 parent c8a31b7 commit 8df9dbc

File tree

4 files changed

+98
-0
lines changed

4 files changed

+98
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
- // MIR for `deref_nonssa` before GVN
2+
+ // MIR for `deref_nonssa` after GVN
3+
4+
fn deref_nonssa() -> Single {
5+
let mut _0: Single;
6+
let mut _1: Single;
7+
let mut _4: Single;
8+
let mut _5: u8;
9+
scope 1 {
10+
debug a => _1;
11+
let _2: &Single;
12+
scope 2 {
13+
debug b => _2;
14+
let _3: u8;
15+
scope 3 {
16+
debug c => _3;
17+
}
18+
}
19+
}
20+
21+
bb0: {
22+
StorageLive(_1);
23+
- _1 = Single(const 0_u8);
24+
- StorageLive(_2);
25+
+ _1 = const Single(0_u8);
26+
+ nop;
27+
_2 = &_1;
28+
- StorageLive(_3);
29+
+ nop;
30+
_3 = copy ((*_2).0: u8);
31+
StorageLive(_4);
32+
- _4 = Single(const 1_u8);
33+
- _1 = move _4;
34+
+ _4 = const Single(1_u8);
35+
+ _1 = const Single(1_u8);
36+
StorageDead(_4);
37+
StorageLive(_5);
38+
_5 = copy _3;
39+
- _0 = Single(move _5);
40+
+ _0 = copy (*_2);
41+
StorageDead(_5);
42+
- StorageDead(_3);
43+
- StorageDead(_2);
44+
+ nop;
45+
+ nop;
46+
StorageDead(_1);
47+
return;
48+
}
49+
}
50+

tests/mir-opt/gvn_copy_aggregate.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,3 +259,15 @@ fn remove_storage_dead_from_index(f: fn() -> usize, v: [SameType; 5]) -> SameTyp
259259
}
260260
}
261261
}
262+
263+
pub struct Single(u8);
264+
265+
// EMIT_MIR gvn_copy_aggregate.deref_nonssa.GVN.diff
266+
fn deref_nonssa() -> Single {
267+
let mut a = Single(0);
268+
let b = &a;
269+
let c = (*b).0;
270+
a = Single(1);
271+
// GVN shouldn't replace `Single(c)` with `*b`.
272+
Single(c)
273+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
- // MIR for `copy_overlapping` before GVN
2+
+ // MIR for `copy_overlapping` after GVN
3+
4+
fn copy_overlapping() -> () {
5+
let mut _0: ();
6+
let mut _1: Adt;
7+
let mut _2: u32;
8+
let mut _3: &Adt;
9+
10+
bb0: {
11+
((_1 as variant#1).0: u32) = const 0_u32;
12+
_3 = &_1;
13+
_2 = copy (((*_3) as variant#1).0: u32);
14+
- _1 = Adt::Some(copy _2);
15+
+ _1 = copy (*_3);
16+
return;
17+
}
18+
}
19+

tests/mir-opt/gvn_overlapping.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,23 @@ fn fields(_1: (Adt, Adt)) {
6464
}
6565
}
6666

67+
// EMIT_MIR gvn_overlapping.copy_overlapping.GVN.diff
68+
#[custom_mir(dialect = "runtime")]
69+
fn copy_overlapping() {
70+
mir! {
71+
let _1;
72+
let _2;
73+
let _3;
74+
{
75+
place!(Field(Variant(_1, 1), 0)) = 0u32;
76+
_3 = &_1;
77+
_2 = Field(Variant(*_3, 1), 0);
78+
_1 = Adt::Some(_2);
79+
Return()
80+
}
81+
}
82+
}
83+
6784
fn main() {
6885
overlapping(Adt::Some(0));
6986
}

0 commit comments

Comments
 (0)