Skip to content

Commit af87662

Browse files
committed
Add test for copying aggregates.
1 parent 76f5bc6 commit af87662

5 files changed

+221
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
- // MIR for `foo` before DataflowConstProp
2+
+ // MIR for `foo` after DataflowConstProp
3+
4+
fn foo() -> u32 {
5+
let mut _0: u32;
6+
let _1: (u32, u32);
7+
let mut _4: bool;
8+
let mut _5: u32;
9+
scope 1 {
10+
debug a => _1;
11+
let _2: (u32, u32);
12+
scope 2 {
13+
debug b => _2;
14+
let _3: u32;
15+
scope 3 {
16+
debug c => _3;
17+
}
18+
}
19+
}
20+
21+
bb0: {
22+
StorageLive(_1);
23+
_1 = const Foo;
24+
StorageLive(_2);
25+
_2 = _1;
26+
StorageLive(_3);
27+
_3 = (_2.1: u32);
28+
StorageLive(_4);
29+
StorageLive(_5);
30+
_5 = _3;
31+
_4 = Ge(move _5, const 2_u32);
32+
switchInt(move _4) -> [0: bb2, otherwise: bb1];
33+
}
34+
35+
bb1: {
36+
StorageDead(_5);
37+
_0 = (_2.0: u32);
38+
goto -> bb3;
39+
}
40+
41+
bb2: {
42+
StorageDead(_5);
43+
_0 = const 13_u32;
44+
goto -> bb3;
45+
}
46+
47+
bb3: {
48+
StorageDead(_4);
49+
StorageDead(_3);
50+
StorageDead(_2);
51+
StorageDead(_1);
52+
return;
53+
}
54+
}
55+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//! Verify that we manage to propagate the value of aggregate `a` even without directly mentioning
2+
//! the contained scalars.
3+
//@ test-mir-pass: DataflowConstProp
4+
5+
const Foo: (u32, u32) = (5, 3);
6+
7+
fn foo() -> u32 {
8+
// CHECK-LABEL: fn foo(
9+
// CHECK: debug a => [[a:_.*]];
10+
// CHECK: debug b => [[b:_.*]];
11+
// CHECK: debug c => [[c:_.*]];
12+
13+
// CHECK:bb0: {
14+
// CHECK: [[a]] = const Foo;
15+
// CHECK: [[b]] = [[a]];
16+
// CHECK: [[c]] = ([[b]].1: u32);
17+
// CHECK: switchInt(move {{_.*}}) -> [0: bb2, otherwise: bb1];
18+
19+
// CHECK:bb1: {
20+
// CHECK: _0 = ([[b]].0: u32);
21+
// CHECK: goto -> bb3;
22+
23+
// CHECK:bb2: {
24+
// CHECK: _0 = const 13_u32;
25+
// CHECK: goto -> bb3;
26+
27+
let a = Foo;
28+
// This copies the struct in `a`. We want to ensure that we do track the contents of `a`
29+
// because we need to read `b` later.
30+
let b = a;
31+
let c = b.1;
32+
if c >= 2 { b.0 } else { 13 }
33+
}
34+
35+
fn main() {
36+
// CHECK-LABEL: fn main(
37+
foo();
38+
}
39+
40+
// EMIT_MIR aggregate_copy.foo.DataflowConstProp.diff
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
- // MIR for `aggregate_copy` before JumpThreading
2+
+ // MIR for `aggregate_copy` after JumpThreading
3+
4+
fn aggregate_copy() -> u32 {
5+
let mut _0: u32;
6+
let _1: (u32, u32);
7+
let mut _4: bool;
8+
let mut _5: u32;
9+
scope 1 {
10+
debug a => _1;
11+
let _2: (u32, u32);
12+
scope 2 {
13+
debug b => _2;
14+
let _3: u32;
15+
scope 3 {
16+
debug c => _3;
17+
}
18+
}
19+
}
20+
21+
bb0: {
22+
StorageLive(_1);
23+
_1 = const aggregate_copy::Foo;
24+
StorageLive(_2);
25+
_2 = _1;
26+
StorageLive(_3);
27+
_3 = (_2.1: u32);
28+
StorageLive(_4);
29+
StorageLive(_5);
30+
_5 = _3;
31+
_4 = Eq(move _5, const 2_u32);
32+
switchInt(move _4) -> [0: bb2, otherwise: bb1];
33+
}
34+
35+
bb1: {
36+
StorageDead(_5);
37+
_0 = (_2.0: u32);
38+
goto -> bb3;
39+
}
40+
41+
bb2: {
42+
StorageDead(_5);
43+
_0 = const 13_u32;
44+
goto -> bb3;
45+
}
46+
47+
bb3: {
48+
StorageDead(_4);
49+
StorageDead(_3);
50+
StorageDead(_2);
51+
StorageDead(_1);
52+
return;
53+
}
54+
}
55+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
- // MIR for `aggregate_copy` before JumpThreading
2+
+ // MIR for `aggregate_copy` after JumpThreading
3+
4+
fn aggregate_copy() -> u32 {
5+
let mut _0: u32;
6+
let _1: (u32, u32);
7+
let mut _4: bool;
8+
let mut _5: u32;
9+
scope 1 {
10+
debug a => _1;
11+
let _2: (u32, u32);
12+
scope 2 {
13+
debug b => _2;
14+
let _3: u32;
15+
scope 3 {
16+
debug c => _3;
17+
}
18+
}
19+
}
20+
21+
bb0: {
22+
StorageLive(_1);
23+
_1 = const aggregate_copy::Foo;
24+
StorageLive(_2);
25+
_2 = _1;
26+
StorageLive(_3);
27+
_3 = (_2.1: u32);
28+
StorageLive(_4);
29+
StorageLive(_5);
30+
_5 = _3;
31+
_4 = Eq(move _5, const 2_u32);
32+
switchInt(move _4) -> [0: bb2, otherwise: bb1];
33+
}
34+
35+
bb1: {
36+
StorageDead(_5);
37+
_0 = (_2.0: u32);
38+
goto -> bb3;
39+
}
40+
41+
bb2: {
42+
StorageDead(_5);
43+
_0 = const 13_u32;
44+
goto -> bb3;
45+
}
46+
47+
bb3: {
48+
StorageDead(_4);
49+
StorageDead(_3);
50+
StorageDead(_2);
51+
StorageDead(_1);
52+
return;
53+
}
54+
}
55+

tests/mir-opt/jump_threading.rs

+16
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,21 @@ fn assume(a: u8, b: bool) -> u8 {
506506
}
507507
}
508508

509+
/// Verify that jump threading succeeds seeing through copies of aggregates.
510+
fn aggregate_copy() -> u32 {
511+
// CHECK-LABEL: fn aggregate_copy(
512+
// CHECK: switchInt(
513+
514+
const Foo: (u32, u32) = (5, 3);
515+
516+
let a = Foo;
517+
// This copies a tuple, we want to ensure that the threading condition on `b.1` propagates to a
518+
// condition on `a.1`.
519+
let b = a;
520+
let c = b.1;
521+
if c == 2 { b.0 } else { 13 }
522+
}
523+
509524
fn main() {
510525
// CHECK-LABEL: fn main(
511526
too_complex(Ok(0));
@@ -534,3 +549,4 @@ fn main() {
534549
// EMIT_MIR jump_threading.disappearing_bb.JumpThreading.diff
535550
// EMIT_MIR jump_threading.aggregate.JumpThreading.diff
536551
// EMIT_MIR jump_threading.assume.JumpThreading.diff
552+
// EMIT_MIR jump_threading.aggregate_copy.JumpThreading.diff

0 commit comments

Comments
 (0)