Skip to content

Commit 63f3dce

Browse files
committed
copy-prop: Add test case for missed optimization due const prop failure
Currently a failure to completely propagate a constant inhibits propagation of any subsequent locals. Add a test case demonstrating the problem before resolving it.
1 parent 90b1f5a commit 63f3dce

File tree

2 files changed

+219
-0
lines changed

2 files changed

+219
-0
lines changed
Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
- // MIR for `maybe` before CopyPropagation
2+
+ // MIR for `maybe` after CopyPropagation
3+
4+
fn maybe(_1: &[bool], _2: String) -> () {
5+
debug m => _1; // in scope 0 at $DIR/copy_propagation_arg.rs:34:10: 34:11
6+
debug s1 => _2; // in scope 0 at $DIR/copy_propagation_arg.rs:34:22: 34:24
7+
let mut _0: (); // return place in scope 0 at $DIR/copy_propagation_arg.rs:34:34: 34:34
8+
let mut _3: bool; // in scope 0 at $DIR/copy_propagation_arg.rs:35:8: 35:12
9+
let _4: usize; // in scope 0 at $DIR/copy_propagation_arg.rs:35:10: 35:11
10+
let mut _5: usize; // in scope 0 at $DIR/copy_propagation_arg.rs:35:8: 35:12
11+
let mut _6: bool; // in scope 0 at $DIR/copy_propagation_arg.rs:35:8: 35:12
12+
let _7: std::string::String; // in scope 0 at $DIR/copy_propagation_arg.rs:36:13: 36:15
13+
let mut _8: bool; // in scope 0 at $DIR/copy_propagation_arg.rs:37:12: 37:16
14+
let _9: usize; // in scope 0 at $DIR/copy_propagation_arg.rs:37:14: 37:15
15+
let mut _10: usize; // in scope 0 at $DIR/copy_propagation_arg.rs:37:12: 37:16
16+
let mut _11: bool; // in scope 0 at $DIR/copy_propagation_arg.rs:37:12: 37:16
17+
let mut _13: bool; // in scope 0 at $DIR/copy_propagation_arg.rs:39:16: 39:20
18+
let _14: usize; // in scope 0 at $DIR/copy_propagation_arg.rs:39:18: 39:19
19+
let mut _15: usize; // in scope 0 at $DIR/copy_propagation_arg.rs:39:16: 39:20
20+
let mut _16: bool; // in scope 0 at $DIR/copy_propagation_arg.rs:39:16: 39:20
21+
let _18: &std::string::String; // in scope 0 at $DIR/copy_propagation_arg.rs:41:17: 41:20
22+
let mut _19: bool; // in scope 0 at $DIR/copy_propagation_arg.rs:45:1: 45:2
23+
let mut _20: bool; // in scope 0 at $DIR/copy_propagation_arg.rs:44:5: 44:6
24+
let mut _21: bool; // in scope 0 at $DIR/copy_propagation_arg.rs:43:9: 43:10
25+
scope 1 {
26+
debug s2 => _7; // in scope 1 at $DIR/copy_propagation_arg.rs:36:13: 36:15
27+
let _12: std::string::String; // in scope 1 at $DIR/copy_propagation_arg.rs:38:17: 38:19
28+
scope 2 {
29+
debug s3 => _12; // in scope 2 at $DIR/copy_propagation_arg.rs:38:17: 38:19
30+
let _17: std::string::String; // in scope 2 at $DIR/copy_propagation_arg.rs:40:21: 40:23
31+
scope 3 {
32+
debug s4 => _17; // in scope 3 at $DIR/copy_propagation_arg.rs:40:21: 40:23
33+
}
34+
}
35+
}
36+
37+
bb0: {
38+
_21 = const false; // scope 0 at $DIR/copy_propagation_arg.rs:35:8: 35:12
39+
_19 = const false; // scope 0 at $DIR/copy_propagation_arg.rs:35:8: 35:12
40+
_20 = const false; // scope 0 at $DIR/copy_propagation_arg.rs:35:8: 35:12
41+
_19 = const true; // scope 0 at $DIR/copy_propagation_arg.rs:35:8: 35:12
42+
StorageLive(_3); // scope 0 at $DIR/copy_propagation_arg.rs:35:8: 35:12
43+
- StorageLive(_4); // scope 0 at $DIR/copy_propagation_arg.rs:35:10: 35:11
44+
+ nop; // scope 0 at $DIR/copy_propagation_arg.rs:35:10: 35:11
45+
_4 = const 0_usize; // scope 0 at $DIR/copy_propagation_arg.rs:35:10: 35:11
46+
_5 = Len((*_1)); // scope 0 at $DIR/copy_propagation_arg.rs:35:8: 35:12
47+
_6 = Lt(const 0_usize, _5); // scope 0 at $DIR/copy_propagation_arg.rs:35:8: 35:12
48+
assert(move _6, "index out of bounds: the len is {} but the index is {}", move _5, const 0_usize) -> [success: bb2, unwind: bb17]; // scope 0 at $DIR/copy_propagation_arg.rs:35:8: 35:12
49+
}
50+
51+
bb1 (cleanup): {
52+
resume; // scope 0 at $DIR/copy_propagation_arg.rs:34:1: 45:2
53+
}
54+
55+
bb2: {
56+
_3 = (*_1)[_4]; // scope 0 at $DIR/copy_propagation_arg.rs:35:8: 35:12
57+
- StorageDead(_4); // scope 0 at $DIR/copy_propagation_arg.rs:35:11: 35:12
58+
+ nop; // scope 0 at $DIR/copy_propagation_arg.rs:35:11: 35:12
59+
switchInt(_3) -> [false: bb3, otherwise: bb4]; // scope 0 at $DIR/copy_propagation_arg.rs:35:5: 44:6
60+
}
61+
62+
bb3: {
63+
_0 = const (); // scope 0 at $DIR/copy_propagation_arg.rs:35:5: 44:6
64+
goto -> bb14; // scope 0 at $DIR/copy_propagation_arg.rs:35:5: 44:6
65+
}
66+
67+
bb4: {
68+
StorageLive(_7); // scope 0 at $DIR/copy_propagation_arg.rs:36:13: 36:15
69+
_19 = const false; // scope 0 at $DIR/copy_propagation_arg.rs:36:18: 36:20
70+
_20 = const true; // scope 0 at $DIR/copy_propagation_arg.rs:36:18: 36:20
71+
_7 = move _2; // scope 0 at $DIR/copy_propagation_arg.rs:36:18: 36:20
72+
StorageLive(_8); // scope 1 at $DIR/copy_propagation_arg.rs:37:12: 37:16
73+
StorageLive(_9); // scope 1 at $DIR/copy_propagation_arg.rs:37:14: 37:15
74+
_9 = const 1_usize; // scope 1 at $DIR/copy_propagation_arg.rs:37:14: 37:15
75+
_10 = Len((*_1)); // scope 1 at $DIR/copy_propagation_arg.rs:37:12: 37:16
76+
_11 = Lt(const 1_usize, _10); // scope 1 at $DIR/copy_propagation_arg.rs:37:12: 37:16
77+
assert(move _11, "index out of bounds: the len is {} but the index is {}", move _10, const 1_usize) -> [success: bb5, unwind: bb19]; // scope 1 at $DIR/copy_propagation_arg.rs:37:12: 37:16
78+
}
79+
80+
bb5: {
81+
_8 = (*_1)[_9]; // scope 1 at $DIR/copy_propagation_arg.rs:37:12: 37:16
82+
StorageDead(_9); // scope 1 at $DIR/copy_propagation_arg.rs:37:15: 37:16
83+
switchInt(_8) -> [false: bb6, otherwise: bb7]; // scope 1 at $DIR/copy_propagation_arg.rs:37:9: 43:10
84+
}
85+
86+
bb6: {
87+
_0 = const (); // scope 1 at $DIR/copy_propagation_arg.rs:37:9: 43:10
88+
goto -> bb25; // scope 1 at $DIR/copy_propagation_arg.rs:37:9: 43:10
89+
}
90+
91+
bb7: {
92+
StorageLive(_12); // scope 1 at $DIR/copy_propagation_arg.rs:38:17: 38:19
93+
_20 = const false; // scope 1 at $DIR/copy_propagation_arg.rs:38:22: 38:24
94+
_21 = const true; // scope 1 at $DIR/copy_propagation_arg.rs:38:22: 38:24
95+
_12 = move _7; // scope 1 at $DIR/copy_propagation_arg.rs:38:22: 38:24
96+
StorageLive(_13); // scope 2 at $DIR/copy_propagation_arg.rs:39:16: 39:20
97+
StorageLive(_14); // scope 2 at $DIR/copy_propagation_arg.rs:39:18: 39:19
98+
_14 = const 2_usize; // scope 2 at $DIR/copy_propagation_arg.rs:39:18: 39:19
99+
_15 = Len((*_1)); // scope 2 at $DIR/copy_propagation_arg.rs:39:16: 39:20
100+
_16 = Lt(const 2_usize, _15); // scope 2 at $DIR/copy_propagation_arg.rs:39:16: 39:20
101+
assert(move _16, "index out of bounds: the len is {} but the index is {}", move _15, const 2_usize) -> [success: bb8, unwind: bb21]; // scope 2 at $DIR/copy_propagation_arg.rs:39:16: 39:20
102+
}
103+
104+
bb8: {
105+
_13 = (*_1)[_14]; // scope 2 at $DIR/copy_propagation_arg.rs:39:16: 39:20
106+
StorageDead(_14); // scope 2 at $DIR/copy_propagation_arg.rs:39:19: 39:20
107+
switchInt(_13) -> [false: bb9, otherwise: bb10]; // scope 2 at $DIR/copy_propagation_arg.rs:39:13: 42:14
108+
}
109+
110+
bb9: {
111+
_0 = const (); // scope 2 at $DIR/copy_propagation_arg.rs:39:13: 42:14
112+
goto -> bb23; // scope 2 at $DIR/copy_propagation_arg.rs:39:13: 42:14
113+
}
114+
115+
bb10: {
116+
StorageLive(_17); // scope 2 at $DIR/copy_propagation_arg.rs:40:21: 40:23
117+
_21 = const false; // scope 2 at $DIR/copy_propagation_arg.rs:40:26: 40:28
118+
_17 = move _12; // scope 2 at $DIR/copy_propagation_arg.rs:40:26: 40:28
119+
StorageLive(_18); // scope 3 at $DIR/copy_propagation_arg.rs:41:17: 41:20
120+
_18 = &_17; // scope 3 at $DIR/copy_propagation_arg.rs:41:17: 41:20
121+
StorageDead(_18); // scope 3 at $DIR/copy_propagation_arg.rs:41:20: 41:21
122+
_0 = const (); // scope 2 at $DIR/copy_propagation_arg.rs:39:21: 42:14
123+
drop(_17) -> [return: bb11, unwind: bb21]; // scope 2 at $DIR/copy_propagation_arg.rs:42:13: 42:14
124+
}
125+
126+
bb11: {
127+
StorageDead(_17); // scope 2 at $DIR/copy_propagation_arg.rs:42:13: 42:14
128+
goto -> bb23; // scope 2 at $DIR/copy_propagation_arg.rs:39:13: 42:14
129+
}
130+
131+
bb12: {
132+
_21 = const false; // scope 1 at $DIR/copy_propagation_arg.rs:43:9: 43:10
133+
StorageDead(_12); // scope 1 at $DIR/copy_propagation_arg.rs:43:9: 43:10
134+
StorageDead(_13); // scope 1 at $DIR/copy_propagation_arg.rs:43:9: 43:10
135+
goto -> bb25; // scope 1 at $DIR/copy_propagation_arg.rs:37:9: 43:10
136+
}
137+
138+
bb13: {
139+
_20 = const false; // scope 0 at $DIR/copy_propagation_arg.rs:44:5: 44:6
140+
StorageDead(_7); // scope 0 at $DIR/copy_propagation_arg.rs:44:5: 44:6
141+
StorageDead(_8); // scope 0 at $DIR/copy_propagation_arg.rs:44:5: 44:6
142+
goto -> bb14; // scope 0 at $DIR/copy_propagation_arg.rs:35:5: 44:6
143+
}
144+
145+
bb14: {
146+
StorageDead(_3); // scope 0 at $DIR/copy_propagation_arg.rs:45:1: 45:2
147+
switchInt(_19) -> [false: bb15, otherwise: bb26]; // scope 0 at $DIR/copy_propagation_arg.rs:45:1: 45:2
148+
}
149+
150+
bb15: {
151+
return; // scope 0 at $DIR/copy_propagation_arg.rs:45:2: 45:2
152+
}
153+
154+
bb16 (cleanup): {
155+
_19 = const false; // scope 0 at $DIR/copy_propagation_arg.rs:45:1: 45:2
156+
drop(_2) -> bb1; // scope 0 at $DIR/copy_propagation_arg.rs:45:1: 45:2
157+
}
158+
159+
bb17 (cleanup): {
160+
switchInt(_19) -> [false: bb1, otherwise: bb16]; // scope 0 at $DIR/copy_propagation_arg.rs:45:1: 45:2
161+
}
162+
163+
bb18 (cleanup): {
164+
_20 = const false; // scope 0 at $DIR/copy_propagation_arg.rs:44:5: 44:6
165+
drop(_7) -> bb17; // scope 0 at $DIR/copy_propagation_arg.rs:44:5: 44:6
166+
}
167+
168+
bb19 (cleanup): {
169+
switchInt(_20) -> [false: bb17, otherwise: bb18]; // scope 0 at $DIR/copy_propagation_arg.rs:44:5: 44:6
170+
}
171+
172+
bb20 (cleanup): {
173+
_21 = const false; // scope 1 at $DIR/copy_propagation_arg.rs:43:9: 43:10
174+
drop(_12) -> bb19; // scope 1 at $DIR/copy_propagation_arg.rs:43:9: 43:10
175+
}
176+
177+
bb21 (cleanup): {
178+
switchInt(_21) -> [false: bb19, otherwise: bb20]; // scope 1 at $DIR/copy_propagation_arg.rs:43:9: 43:10
179+
}
180+
181+
bb22: {
182+
_21 = const false; // scope 1 at $DIR/copy_propagation_arg.rs:43:9: 43:10
183+
drop(_12) -> [return: bb12, unwind: bb19]; // scope 1 at $DIR/copy_propagation_arg.rs:43:9: 43:10
184+
}
185+
186+
bb23: {
187+
switchInt(_21) -> [false: bb12, otherwise: bb22]; // scope 1 at $DIR/copy_propagation_arg.rs:43:9: 43:10
188+
}
189+
190+
bb24: {
191+
_20 = const false; // scope 0 at $DIR/copy_propagation_arg.rs:44:5: 44:6
192+
drop(_7) -> [return: bb13, unwind: bb17]; // scope 0 at $DIR/copy_propagation_arg.rs:44:5: 44:6
193+
}
194+
195+
bb25: {
196+
switchInt(_20) -> [false: bb13, otherwise: bb24]; // scope 0 at $DIR/copy_propagation_arg.rs:44:5: 44:6
197+
}
198+
199+
bb26: {
200+
_19 = const false; // scope 0 at $DIR/copy_propagation_arg.rs:45:1: 45:2
201+
drop(_2) -> [return: bb15, unwind: bb1]; // scope 0 at $DIR/copy_propagation_arg.rs:45:1: 45:2
202+
}
203+
}
204+

src/test/mir-opt/copy_propagation_arg.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,25 @@ fn arg_src(mut x: i32) -> i32 {
3030
y
3131
}
3232

33+
// EMIT_MIR copy_propagation_arg.maybe.CopyPropagation.diff
34+
fn maybe(m: &[bool], s1: String) {
35+
if m[0] {
36+
let s2 = s1;
37+
if m[1] {
38+
let s3 = s2;
39+
if m[2] {
40+
let s4 = s3;
41+
&s4;
42+
}
43+
}
44+
}
45+
}
46+
3347
fn main() {
3448
// Make sure the function actually gets instantiated.
3549
foo(0);
3650
bar(0);
3751
baz(0);
3852
arg_src(0);
53+
maybe(&[true, false, true], "Hello!".to_owned());
3954
}

0 commit comments

Comments
 (0)