Skip to content

Commit fbcf758

Browse files
Add a few dest-prop MIR tests
1 parent 8f77e36 commit fbcf758

File tree

8 files changed

+362
-0
lines changed

8 files changed

+362
-0
lines changed

src/test/mir-opt/dest-prop/branch.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//! Tests that assignment in both branches of an `if` are eliminated.
2+
3+
fn val() -> i32 {
4+
1
5+
}
6+
7+
fn cond() -> bool {
8+
true
9+
}
10+
11+
// EMIT_MIR rustc.main.DestinationPropagation.diff
12+
fn main() {
13+
let x = val();
14+
15+
let y = if cond() {
16+
x
17+
} else {
18+
val();
19+
x
20+
};
21+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
- // MIR for `main` before DestinationPropagation
2+
+ // MIR for `main` after DestinationPropagation
3+
4+
fn main() -> () {
5+
let mut _0: (); // return place in scope 0 at $DIR/branch.rs:12:11: 12:11
6+
let _1: i32; // in scope 0 at $DIR/branch.rs:13:9: 13:10
7+
let mut _3: bool; // in scope 0 at $DIR/branch.rs:15:16: 15:22
8+
let _4: i32; // in scope 0 at $DIR/branch.rs:18:9: 18:14
9+
scope 1 {
10+
- debug x => _1; // in scope 1 at $DIR/branch.rs:13:9: 13:10
11+
+ debug x => _2; // in scope 1 at $DIR/branch.rs:13:9: 13:10
12+
let _2: i32; // in scope 1 at $DIR/branch.rs:15:9: 15:10
13+
scope 2 {
14+
debug y => _2; // in scope 2 at $DIR/branch.rs:15:9: 15:10
15+
}
16+
}
17+
18+
bb0: {
19+
- StorageLive(_1); // scope 0 at $DIR/branch.rs:13:9: 13:10
20+
- _1 = const val() -> bb1; // scope 0 at $DIR/branch.rs:13:13: 13:18
21+
+ nop; // scope 0 at $DIR/branch.rs:13:9: 13:10
22+
+ _2 = const val() -> bb1; // scope 0 at $DIR/branch.rs:13:13: 13:18
23+
// ty::Const
24+
// + ty: fn() -> i32 {val}
25+
// + val: Value(Scalar(<ZST>))
26+
// mir::Constant
27+
// + span: $DIR/branch.rs:13:13: 13:16
28+
// + literal: Const { ty: fn() -> i32 {val}, val: Value(Scalar(<ZST>)) }
29+
}
30+
31+
bb1: {
32+
- StorageLive(_2); // scope 1 at $DIR/branch.rs:15:9: 15:10
33+
+ nop; // scope 1 at $DIR/branch.rs:15:9: 15:10
34+
StorageLive(_3); // scope 1 at $DIR/branch.rs:15:16: 15:22
35+
_3 = const cond() -> bb2; // scope 1 at $DIR/branch.rs:15:16: 15:22
36+
// ty::Const
37+
// + ty: fn() -> bool {cond}
38+
// + val: Value(Scalar(<ZST>))
39+
// mir::Constant
40+
// + span: $DIR/branch.rs:15:16: 15:20
41+
// + literal: Const { ty: fn() -> bool {cond}, val: Value(Scalar(<ZST>)) }
42+
}
43+
44+
bb2: {
45+
switchInt(_3) -> [false: bb3, otherwise: bb4]; // scope 1 at $DIR/branch.rs:15:13: 20:6
46+
}
47+
48+
bb3: {
49+
StorageLive(_4); // scope 1 at $DIR/branch.rs:18:9: 18:14
50+
_4 = const val() -> bb5; // scope 1 at $DIR/branch.rs:18:9: 18:14
51+
// ty::Const
52+
// + ty: fn() -> i32 {val}
53+
// + val: Value(Scalar(<ZST>))
54+
// mir::Constant
55+
// + span: $DIR/branch.rs:18:9: 18:12
56+
// + literal: Const { ty: fn() -> i32 {val}, val: Value(Scalar(<ZST>)) }
57+
}
58+
59+
bb4: {
60+
- _2 = _1; // scope 1 at $DIR/branch.rs:16:9: 16:10
61+
+ nop; // scope 1 at $DIR/branch.rs:16:9: 16:10
62+
goto -> bb6; // scope 1 at $DIR/branch.rs:15:13: 20:6
63+
}
64+
65+
bb5: {
66+
StorageDead(_4); // scope 1 at $DIR/branch.rs:18:14: 18:15
67+
- _2 = _1; // scope 1 at $DIR/branch.rs:19:9: 19:10
68+
+ nop; // scope 1 at $DIR/branch.rs:19:9: 19:10
69+
goto -> bb6; // scope 1 at $DIR/branch.rs:15:13: 20:6
70+
}
71+
72+
bb6: {
73+
StorageDead(_3); // scope 1 at $DIR/branch.rs:20:6: 20:7
74+
_0 = const (); // scope 0 at $DIR/branch.rs:12:11: 21:2
75+
// ty::Const
76+
// + ty: ()
77+
// + val: Value(Scalar(<ZST>))
78+
// mir::Constant
79+
// + span: $DIR/branch.rs:12:11: 21:2
80+
// + literal: Const { ty: (), val: Value(Scalar(<ZST>)) }
81+
- StorageDead(_2); // scope 1 at $DIR/branch.rs:21:1: 21:2
82+
- StorageDead(_1); // scope 0 at $DIR/branch.rs:21:1: 21:2
83+
+ nop; // scope 1 at $DIR/branch.rs:21:1: 21:2
84+
+ nop; // scope 0 at $DIR/branch.rs:21:1: 21:2
85+
return; // scope 0 at $DIR/branch.rs:21:2: 21:2
86+
}
87+
}
88+

src/test/mir-opt/dest-prop/cycle.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//! Tests that cyclic assignments don't hang DestinationPropagation, and result in reasonable code.
2+
3+
fn val() -> i32 {
4+
1
5+
}
6+
7+
// EMIT_MIR rustc.main.DestinationPropagation.diff
8+
fn main() {
9+
let mut x = val();
10+
let y = x;
11+
let z = y;
12+
x = z;
13+
14+
drop(x);
15+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
- // MIR for `main` before DestinationPropagation
2+
+ // MIR for `main` after DestinationPropagation
3+
4+
fn main() -> () {
5+
let mut _0: (); // return place in scope 0 at $DIR/cycle.rs:8:11: 8:11
6+
let mut _1: i32; // in scope 0 at $DIR/cycle.rs:9:9: 9:14
7+
let mut _4: i32; // in scope 0 at $DIR/cycle.rs:12:9: 12:10
8+
let _5: (); // in scope 0 at $DIR/cycle.rs:14:5: 14:12
9+
let mut _6: i32; // in scope 0 at $DIR/cycle.rs:14:10: 14:11
10+
scope 1 {
11+
- debug x => _1; // in scope 1 at $DIR/cycle.rs:9:9: 9:14
12+
+ debug x => _4; // in scope 1 at $DIR/cycle.rs:9:9: 9:14
13+
let _2: i32; // in scope 1 at $DIR/cycle.rs:10:9: 10:10
14+
scope 2 {
15+
- debug y => _2; // in scope 2 at $DIR/cycle.rs:10:9: 10:10
16+
+ debug y => _4; // in scope 2 at $DIR/cycle.rs:10:9: 10:10
17+
let _3: i32; // in scope 2 at $DIR/cycle.rs:11:9: 11:10
18+
scope 3 {
19+
- debug z => _3; // in scope 3 at $DIR/cycle.rs:11:9: 11:10
20+
+ debug z => _4; // in scope 3 at $DIR/cycle.rs:11:9: 11:10
21+
scope 4 {
22+
debug _x => _6; // in scope 4 at $SRC_DIR/libcore/mem/mod.rs:LL:COL
23+
}
24+
}
25+
}
26+
}
27+
28+
bb0: {
29+
- StorageLive(_1); // scope 0 at $DIR/cycle.rs:9:9: 9:14
30+
- _1 = const val() -> bb1; // scope 0 at $DIR/cycle.rs:9:17: 9:22
31+
+ nop; // scope 0 at $DIR/cycle.rs:9:9: 9:14
32+
+ _4 = const val() -> bb1; // scope 0 at $DIR/cycle.rs:9:17: 9:22
33+
// ty::Const
34+
// + ty: fn() -> i32 {val}
35+
// + val: Value(Scalar(<ZST>))
36+
// mir::Constant
37+
// + span: $DIR/cycle.rs:9:17: 9:20
38+
// + literal: Const { ty: fn() -> i32 {val}, val: Value(Scalar(<ZST>)) }
39+
}
40+
41+
bb1: {
42+
- StorageLive(_2); // scope 1 at $DIR/cycle.rs:10:9: 10:10
43+
- _2 = _1; // scope 1 at $DIR/cycle.rs:10:13: 10:14
44+
- StorageLive(_3); // scope 2 at $DIR/cycle.rs:11:9: 11:10
45+
- _3 = _2; // scope 2 at $DIR/cycle.rs:11:13: 11:14
46+
- StorageLive(_4); // scope 3 at $DIR/cycle.rs:12:9: 12:10
47+
- _4 = _3; // scope 3 at $DIR/cycle.rs:12:9: 12:10
48+
- _1 = move _4; // scope 3 at $DIR/cycle.rs:12:5: 12:10
49+
- StorageDead(_4); // scope 3 at $DIR/cycle.rs:12:9: 12:10
50+
+ nop; // scope 1 at $DIR/cycle.rs:10:9: 10:10
51+
+ nop; // scope 1 at $DIR/cycle.rs:10:13: 10:14
52+
+ nop; // scope 2 at $DIR/cycle.rs:11:9: 11:10
53+
+ nop; // scope 2 at $DIR/cycle.rs:11:13: 11:14
54+
+ nop; // scope 3 at $DIR/cycle.rs:12:9: 12:10
55+
+ nop; // scope 3 at $DIR/cycle.rs:12:9: 12:10
56+
+ nop; // scope 3 at $DIR/cycle.rs:12:5: 12:10
57+
+ nop; // scope 3 at $DIR/cycle.rs:12:9: 12:10
58+
StorageLive(_5); // scope 3 at $DIR/cycle.rs:14:5: 14:12
59+
StorageLive(_6); // scope 3 at $DIR/cycle.rs:14:10: 14:11
60+
- _6 = _1; // scope 3 at $DIR/cycle.rs:14:10: 14:11
61+
+ _6 = _4; // scope 3 at $DIR/cycle.rs:14:10: 14:11
62+
_5 = const (); // scope 4 at $SRC_DIR/libcore/mem/mod.rs:LL:COL
63+
// ty::Const
64+
// + ty: ()
65+
// + val: Value(Scalar(<ZST>))
66+
// mir::Constant
67+
// + span: $SRC_DIR/libcore/mem/mod.rs:LL:COL
68+
// + literal: Const { ty: (), val: Value(Scalar(<ZST>)) }
69+
drop(_6) -> bb2; // scope 4 at $SRC_DIR/libcore/mem/mod.rs:LL:COL
70+
}
71+
72+
bb2: {
73+
StorageDead(_6); // scope 3 at $DIR/cycle.rs:14:11: 14:12
74+
StorageDead(_5); // scope 3 at $DIR/cycle.rs:14:12: 14:13
75+
_0 = const (); // scope 0 at $DIR/cycle.rs:8:11: 15:2
76+
// ty::Const
77+
// + ty: ()
78+
// + val: Value(Scalar(<ZST>))
79+
// mir::Constant
80+
// + span: $DIR/cycle.rs:8:11: 15:2
81+
// + literal: Const { ty: (), val: Value(Scalar(<ZST>)) }
82+
- StorageDead(_3); // scope 2 at $DIR/cycle.rs:15:1: 15:2
83+
- StorageDead(_2); // scope 1 at $DIR/cycle.rs:15:1: 15:2
84+
- StorageDead(_1); // scope 0 at $DIR/cycle.rs:15:1: 15:2
85+
+ nop; // scope 2 at $DIR/cycle.rs:15:1: 15:2
86+
+ nop; // scope 1 at $DIR/cycle.rs:15:1: 15:2
87+
+ nop; // scope 0 at $DIR/cycle.rs:15:1: 15:2
88+
return; // scope 0 at $DIR/cycle.rs:15:2: 15:2
89+
}
90+
}
91+

src/test/mir-opt/dest-prop/simple.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//! Copy of `nrvo-simple.rs`, to ensure that full dest-prop handles it too.
2+
3+
// EMIT_MIR rustc.nrvo.DestinationPropagation.diff
4+
fn nrvo(init: fn(&mut [u8; 1024])) -> [u8; 1024] {
5+
let mut buf = [0; 1024];
6+
init(&mut buf);
7+
buf
8+
}
9+
10+
fn main() {
11+
let _ = nrvo(|buf| {
12+
buf[4] = 4;
13+
});
14+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
- // MIR for `nrvo` before DestinationPropagation
2+
+ // MIR for `nrvo` after DestinationPropagation
3+
4+
fn nrvo(_1: for<'r> fn(&'r mut [u8; 1024])) -> [u8; 1024] {
5+
debug init => _1; // in scope 0 at $DIR/simple.rs:4:9: 4:13
6+
let mut _0: [u8; 1024]; // return place in scope 0 at $DIR/simple.rs:4:39: 4:49
7+
let mut _2: [u8; 1024]; // in scope 0 at $DIR/simple.rs:5:9: 5:16
8+
let _3: (); // in scope 0 at $DIR/simple.rs:6:5: 6:19
9+
let mut _4: for<'r> fn(&'r mut [u8; 1024]); // in scope 0 at $DIR/simple.rs:6:5: 6:9
10+
let mut _5: &mut [u8; 1024]; // in scope 0 at $DIR/simple.rs:6:10: 6:18
11+
let mut _6: &mut [u8; 1024]; // in scope 0 at $DIR/simple.rs:6:10: 6:18
12+
scope 1 {
13+
- debug buf => _2; // in scope 1 at $DIR/simple.rs:5:9: 5:16
14+
+ debug buf => _0; // in scope 1 at $DIR/simple.rs:5:9: 5:16
15+
}
16+
17+
bb0: {
18+
- StorageLive(_2); // scope 0 at $DIR/simple.rs:5:9: 5:16
19+
- _2 = [const 0u8; 1024]; // scope 0 at $DIR/simple.rs:5:19: 5:28
20+
+ nop; // scope 0 at $DIR/simple.rs:5:9: 5:16
21+
+ _0 = [const 0u8; 1024]; // scope 0 at $DIR/simple.rs:5:19: 5:28
22+
// ty::Const
23+
// + ty: u8
24+
// + val: Value(Scalar(0x00))
25+
// mir::Constant
26+
// + span: $DIR/simple.rs:5:20: 5:21
27+
// + literal: Const { ty: u8, val: Value(Scalar(0x00)) }
28+
StorageLive(_3); // scope 1 at $DIR/simple.rs:6:5: 6:19
29+
StorageLive(_4); // scope 1 at $DIR/simple.rs:6:5: 6:9
30+
_4 = _1; // scope 1 at $DIR/simple.rs:6:5: 6:9
31+
- StorageLive(_5); // scope 1 at $DIR/simple.rs:6:10: 6:18
32+
- StorageLive(_6); // scope 1 at $DIR/simple.rs:6:10: 6:18
33+
- _6 = &mut _2; // scope 1 at $DIR/simple.rs:6:10: 6:18
34+
- _5 = move _6; // scope 1 at $DIR/simple.rs:6:10: 6:18
35+
+ nop; // scope 1 at $DIR/simple.rs:6:10: 6:18
36+
+ nop; // scope 1 at $DIR/simple.rs:6:10: 6:18
37+
+ _5 = &mut _0; // scope 1 at $DIR/simple.rs:6:10: 6:18
38+
+ nop; // scope 1 at $DIR/simple.rs:6:10: 6:18
39+
_3 = move _4(move _5) -> bb1; // scope 1 at $DIR/simple.rs:6:5: 6:19
40+
}
41+
42+
bb1: {
43+
- StorageDead(_5); // scope 1 at $DIR/simple.rs:6:18: 6:19
44+
+ nop; // scope 1 at $DIR/simple.rs:6:18: 6:19
45+
StorageDead(_4); // scope 1 at $DIR/simple.rs:6:18: 6:19
46+
- StorageDead(_6); // scope 1 at $DIR/simple.rs:6:19: 6:20
47+
+ nop; // scope 1 at $DIR/simple.rs:6:19: 6:20
48+
StorageDead(_3); // scope 1 at $DIR/simple.rs:6:19: 6:20
49+
- _0 = _2; // scope 1 at $DIR/simple.rs:7:5: 7:8
50+
- StorageDead(_2); // scope 0 at $DIR/simple.rs:8:1: 8:2
51+
+ nop; // scope 1 at $DIR/simple.rs:7:5: 7:8
52+
+ nop; // scope 0 at $DIR/simple.rs:8:1: 8:2
53+
return; // scope 0 at $DIR/simple.rs:8:2: 8:2
54+
}
55+
}
56+

src/test/mir-opt/dest-prop/union.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//! Tests that projections through unions cancel `DestinationPropagation`.
2+
3+
fn val() -> u32 {
4+
1
5+
}
6+
7+
// EMIT_MIR rustc.main.DestinationPropagation.diff
8+
fn main() {
9+
union Un {
10+
us: u32,
11+
}
12+
13+
let un = Un { us: val() };
14+
15+
drop(unsafe { un.us });
16+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
- // MIR for `main` before DestinationPropagation
2+
+ // MIR for `main` after DestinationPropagation
3+
4+
fn main() -> () {
5+
let mut _0: (); // return place in scope 0 at $DIR/union.rs:8:11: 8:11
6+
let _1: main::Un; // in scope 0 at $DIR/union.rs:13:9: 13:11
7+
let mut _2: u32; // in scope 0 at $DIR/union.rs:13:23: 13:28
8+
let _3: (); // in scope 0 at $DIR/union.rs:15:5: 15:27
9+
let mut _4: u32; // in scope 0 at $DIR/union.rs:15:10: 15:26
10+
scope 1 {
11+
debug un => _1; // in scope 1 at $DIR/union.rs:13:9: 13:11
12+
scope 2 {
13+
}
14+
scope 3 {
15+
debug _x => _4; // in scope 3 at $SRC_DIR/libcore/mem/mod.rs:LL:COL
16+
}
17+
}
18+
19+
bb0: {
20+
StorageLive(_1); // scope 0 at $DIR/union.rs:13:9: 13:11
21+
StorageLive(_2); // scope 0 at $DIR/union.rs:13:23: 13:28
22+
_2 = const val() -> bb1; // scope 0 at $DIR/union.rs:13:23: 13:28
23+
// ty::Const
24+
// + ty: fn() -> u32 {val}
25+
// + val: Value(Scalar(<ZST>))
26+
// mir::Constant
27+
// + span: $DIR/union.rs:13:23: 13:26
28+
// + literal: Const { ty: fn() -> u32 {val}, val: Value(Scalar(<ZST>)) }
29+
}
30+
31+
bb1: {
32+
(_1.0: u32) = move _2; // scope 0 at $DIR/union.rs:13:14: 13:30
33+
StorageDead(_2); // scope 0 at $DIR/union.rs:13:29: 13:30
34+
StorageLive(_3); // scope 1 at $DIR/union.rs:15:5: 15:27
35+
StorageLive(_4); // scope 1 at $DIR/union.rs:15:10: 15:26
36+
_4 = (_1.0: u32); // scope 2 at $DIR/union.rs:15:19: 15:24
37+
_3 = const (); // scope 3 at $SRC_DIR/libcore/mem/mod.rs:LL:COL
38+
// ty::Const
39+
// + ty: ()
40+
// + val: Value(Scalar(<ZST>))
41+
// mir::Constant
42+
// + span: $SRC_DIR/libcore/mem/mod.rs:LL:COL
43+
// + literal: Const { ty: (), val: Value(Scalar(<ZST>)) }
44+
drop(_4) -> bb2; // scope 3 at $SRC_DIR/libcore/mem/mod.rs:LL:COL
45+
}
46+
47+
bb2: {
48+
StorageDead(_4); // scope 1 at $DIR/union.rs:15:26: 15:27
49+
StorageDead(_3); // scope 1 at $DIR/union.rs:15:27: 15:28
50+
_0 = const (); // scope 0 at $DIR/union.rs:8:11: 16:2
51+
// ty::Const
52+
// + ty: ()
53+
// + val: Value(Scalar(<ZST>))
54+
// mir::Constant
55+
// + span: $DIR/union.rs:8:11: 16:2
56+
// + literal: Const { ty: (), val: Value(Scalar(<ZST>)) }
57+
StorageDead(_1); // scope 0 at $DIR/union.rs:16:1: 16:2
58+
return; // scope 0 at $DIR/union.rs:16:2: 16:2
59+
}
60+
}
61+

0 commit comments

Comments
 (0)