Skip to content

Commit 3c4fab2

Browse files
Rollup merge of #163425 - ojuschugh1:jump-threading-fix, r=saethlin
Treat union fields as aliasing each other in value_analysis Fixes #161898 The problem was that `for_each_aliasing_place` only considered enum variants and the discriminant as aliasing, so writing to one union field left the state of the other fields alone. JumpThreading then threaded the second `switchInt` on `u.a` using the value from before `u.b = 5`, and DataflowConstProp folded the read of `u.a` to a stale constant in the same way. Union field places have only been in the map since #127087 began collecting places syntactically; before that `iter_fields` skipped unions. I made `for_each_aliasing_place` flood everything under a union as soon as the walk reaches it, since all non-ZST union fields start at offset 0 and overlap. The alternative of not registering places below unions would also work but loses threading on union reads with no write in between. Added a mir-opt test for each pass.
2 parents 0093837 + 49e02a2 commit 3c4fab2

7 files changed

Lines changed: 253 additions & 0 deletions

‎compiler/rustc_mir_dataflow/src/value_analysis.rs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,12 @@ impl<'tcx> Map<'tcx> {
786786
};
787787
let elems = place.projection.iter().map(|&elem| elem.try_into()).chain(tail_elem.map(Ok));
788788
for elem in elems {
789+
// All fields of a union alias each other.
790+
if self.places[index].ty.is_union() {
791+
self.for_each_value_inside(index, f);
792+
return;
793+
}
794+
789795
// A field aliases the parent place.
790796
if let Some(vi) = self.places[index].value_index {
791797
f(vi);
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
- // MIR for `read_other_field` before DataflowConstProp
2+
+ // MIR for `read_other_field` after DataflowConstProp
3+
4+
fn read_other_field() -> u8 {
5+
let mut _0: u8;
6+
let mut _1: U;
7+
scope 1 {
8+
debug u => _1;
9+
}
10+
11+
bb0: {
12+
StorageLive(_1);
13+
_1 = U { a: const 0_u8 };
14+
(_1.0: u8) = const 1_u8;
15+
(_1.1: u8) = const 5_u8;
16+
_0 = copy (_1.0: u8);
17+
StorageDead(_1);
18+
return;
19+
}
20+
}
21+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
- // MIR for `read_other_field` before DataflowConstProp
2+
+ // MIR for `read_other_field` after DataflowConstProp
3+
4+
fn read_other_field() -> u8 {
5+
let mut _0: u8;
6+
let mut _1: U;
7+
scope 1 {
8+
debug u => _1;
9+
}
10+
11+
bb0: {
12+
StorageLive(_1);
13+
_1 = U { a: const 0_u8 };
14+
(_1.0: u8) = const 1_u8;
15+
(_1.1: u8) = const 5_u8;
16+
_0 = copy (_1.0: u8);
17+
StorageDead(_1);
18+
return;
19+
}
20+
}
21+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// EMIT_MIR_FOR_EACH_PANIC_STRATEGY
2+
// Writing to one union field must also invalidate what is known about the other fields,
3+
// since they share the same storage.
4+
5+
//@ test-mir-pass: DataflowConstProp
6+
//@ compile-flags: --crate-type=lib
7+
8+
union U {
9+
a: u8,
10+
b: u8,
11+
}
12+
13+
// EMIT_MIR union_field.read_other_field.DataflowConstProp.diff
14+
pub fn read_other_field() -> u8 {
15+
// CHECK-LABEL: fn read_other_field(
16+
// CHECK: _0 = copy ({{_.*}}.0: u8);
17+
let mut u = U { a: 0 };
18+
u.a = 1;
19+
u.b = 5;
20+
unsafe { u.a }
21+
}

‎tests/mir-opt/jump_threading.rs‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,42 @@ fn chained_conditions() -> u8 {
656656
}
657657
}
658658

659+
union U {
660+
a: u8,
661+
b: u8,
662+
}
663+
664+
fn union_field(x: u8) -> u8 {
665+
// CHECK-LABEL: fn union_field(
666+
// CHECK: debug u => [[u:_.*]];
667+
// CHECK: bb0: {
668+
// CHECK: switchInt(copy ([[u]].0: u8)) -> [0: bb2, otherwise: bb1];
669+
// CHECK: bb1: {
670+
// CHECK: goto -> bb3;
671+
// CHECK: bb2: {
672+
// CHECK: goto -> bb3;
673+
// CHECK: bb3: {
674+
// CHECK: ([[u]].1: u8) = const 5_u8;
675+
// CHECK: switchInt(copy ([[u]].0: u8)) -> [0: bb5, otherwise: bb4];
676+
677+
// Test for #161898, where the write to `u.b` did not invalidate what was known about `u.a`.
678+
let mut u = U { a: x };
679+
let r = unsafe {
680+
match u.a {
681+
0 => 100,
682+
_ => 200,
683+
}
684+
};
685+
u.b = 5;
686+
let r2 = unsafe {
687+
match u.a {
688+
0 => 1,
689+
_ => 2,
690+
}
691+
};
692+
r + r2
693+
}
694+
659695
fn main() {
660696
// CHECK-LABEL: fn main(
661697
too_complex(Ok(0));
@@ -673,6 +709,7 @@ fn main() {
673709
floats();
674710
bitwise_not();
675711
logical_not();
712+
union_field(0);
676713
}
677714

678715
// EMIT_MIR jump_threading.too_complex.JumpThreading.diff
@@ -693,3 +730,4 @@ fn main() {
693730
// EMIT_MIR jump_threading.bitwise_not.JumpThreading.diff
694731
// EMIT_MIR jump_threading.logical_not.JumpThreading.diff
695732
// EMIT_MIR jump_threading.chained_conditions.JumpThreading.diff
733+
// EMIT_MIR jump_threading.union_field.JumpThreading.diff
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
- // MIR for `union_field` before JumpThreading
2+
+ // MIR for `union_field` after JumpThreading
3+
4+
fn union_field(_1: u8) -> u8 {
5+
debug x => _1;
6+
let mut _0: u8;
7+
let mut _2: U;
8+
let mut _3: u8;
9+
let mut _6: u8;
10+
let mut _7: u8;
11+
scope 1 {
12+
debug u => _2;
13+
let _4: u8;
14+
scope 2 {
15+
debug r => _4;
16+
let _5: u8;
17+
scope 3 {
18+
debug r2 => _5;
19+
}
20+
}
21+
}
22+
23+
bb0: {
24+
StorageLive(_2);
25+
StorageLive(_3);
26+
_3 = copy _1;
27+
_2 = U { a: move _3 };
28+
StorageDead(_3);
29+
StorageLive(_4);
30+
switchInt(copy (_2.0: u8)) -> [0: bb2, otherwise: bb1];
31+
}
32+
33+
bb1: {
34+
_4 = const 200_u8;
35+
goto -> bb3;
36+
}
37+
38+
bb2: {
39+
_4 = const 100_u8;
40+
goto -> bb3;
41+
}
42+
43+
bb3: {
44+
(_2.1: u8) = const 5_u8;
45+
StorageLive(_5);
46+
switchInt(copy (_2.0: u8)) -> [0: bb5, otherwise: bb4];
47+
}
48+
49+
bb4: {
50+
_5 = const 2_u8;
51+
goto -> bb6;
52+
}
53+
54+
bb5: {
55+
_5 = const 1_u8;
56+
goto -> bb6;
57+
}
58+
59+
bb6: {
60+
StorageLive(_6);
61+
_6 = copy _4;
62+
StorageLive(_7);
63+
_7 = copy _5;
64+
_0 = Add(move _6, move _7);
65+
StorageDead(_7);
66+
StorageDead(_6);
67+
StorageDead(_5);
68+
StorageDead(_4);
69+
StorageDead(_2);
70+
return;
71+
}
72+
}
73+
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
- // MIR for `union_field` before JumpThreading
2+
+ // MIR for `union_field` after JumpThreading
3+
4+
fn union_field(_1: u8) -> u8 {
5+
debug x => _1;
6+
let mut _0: u8;
7+
let mut _2: U;
8+
let mut _3: u8;
9+
let mut _6: u8;
10+
let mut _7: u8;
11+
scope 1 {
12+
debug u => _2;
13+
let _4: u8;
14+
scope 2 {
15+
debug r => _4;
16+
let _5: u8;
17+
scope 3 {
18+
debug r2 => _5;
19+
}
20+
}
21+
}
22+
23+
bb0: {
24+
StorageLive(_2);
25+
StorageLive(_3);
26+
_3 = copy _1;
27+
_2 = U { a: move _3 };
28+
StorageDead(_3);
29+
StorageLive(_4);
30+
switchInt(copy (_2.0: u8)) -> [0: bb2, otherwise: bb1];
31+
}
32+
33+
bb1: {
34+
_4 = const 200_u8;
35+
goto -> bb3;
36+
}
37+
38+
bb2: {
39+
_4 = const 100_u8;
40+
goto -> bb3;
41+
}
42+
43+
bb3: {
44+
(_2.1: u8) = const 5_u8;
45+
StorageLive(_5);
46+
switchInt(copy (_2.0: u8)) -> [0: bb5, otherwise: bb4];
47+
}
48+
49+
bb4: {
50+
_5 = const 2_u8;
51+
goto -> bb6;
52+
}
53+
54+
bb5: {
55+
_5 = const 1_u8;
56+
goto -> bb6;
57+
}
58+
59+
bb6: {
60+
StorageLive(_6);
61+
_6 = copy _4;
62+
StorageLive(_7);
63+
_7 = copy _5;
64+
_0 = Add(move _6, move _7);
65+
StorageDead(_7);
66+
StorageDead(_6);
67+
StorageDead(_5);
68+
StorageDead(_4);
69+
StorageDead(_2);
70+
return;
71+
}
72+
}
73+

0 commit comments

Comments
 (0)