Skip to content

Commit 3d8c1c1

Browse files
committed
Auto merge of #144477 - cjgillot:gvn-index, r=dianqk
GVN: Use a VnIndex in Address projection. The current implementation of address projections is inconsistent. Indexing semantically relies on the index' value, but the implementation uses the index' place. This PR fixes that by using `ProjectionElem<VnIndex, Ty<'tcx>>` instead of the raw `PlaceElem<'tcx>`. This is a more principled fix than the workaround in #145030.
2 parents 8111a2d + 3d96e54 commit 3d8c1c1

File tree

7 files changed

+233
-138
lines changed

7 files changed

+233
-138
lines changed

compiler/rustc_mir_transform/src/gvn.rs

Lines changed: 191 additions & 131 deletions
Large diffs are not rendered by default.

tests/mir-opt/gvn.dereference_indexing.GVN.panic-abort.diff

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@
4343
+ nop;
4444
StorageLive(_8);
4545
StorageLive(_9);
46-
_9 = copy (*_3);
46+
- _9 = copy (*_3);
47+
+ _9 = copy _1[_4];
4748
_8 = opaque::<u8>(move _9) -> [return: bb2, unwind unreachable];
4849
}
4950

tests/mir-opt/gvn.dereference_indexing.GVN.panic-unwind.diff

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@
4343
+ nop;
4444
StorageLive(_8);
4545
StorageLive(_9);
46-
_9 = copy (*_3);
46+
- _9 = copy (*_3);
47+
+ _9 = copy _1[_4];
4748
_8 = opaque::<u8>(move _9) -> [return: bb2, unwind continue];
4849
}
4950

tests/mir-opt/gvn.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,8 +1065,8 @@ fn dereference_indexing(array: [u8; 2], index: usize) {
10651065
&array[i]
10661066
};
10671067

1068-
// CHECK-NOT: [{{.*}}]
1069-
// CHECK: [[tmp:_.*]] = copy (*[[a]]);
1068+
// CHECK-NOT: StorageDead([[i]]);
1069+
// CHECK: [[tmp:_.*]] = copy _1[[[i]]];
10701070
// CHECK: opaque::<u8>(move [[tmp]])
10711071
opaque(*a);
10721072
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
- // MIR for `index_place` before GVN
2+
+ // MIR for `index_place` after GVN
3+
4+
fn index_place(_1: usize, _2: usize, _3: [i32; 5]) -> i32 {
5+
let mut _0: i32;
6+
let mut _4: &i32;
7+
8+
bb0: {
9+
_4 = &_3[_1];
10+
_1 = copy _2;
11+
_0 = copy (*_4);
12+
return;
13+
}
14+
}
15+

tests/mir-opt/gvn_repeat.repeat_local.GVN.diff

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
_4 = [copy _3; 5];
1111
_5 = &_4[_1];
1212
_1 = copy _2;
13-
- _0 = copy (*_5);
14-
+ _0 = copy _3;
13+
_0 = copy (*_5);
1514
return;
1615
}
1716
}

tests/mir-opt/gvn_repeat.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,23 @@
66

77
use std::intrinsics::mir::*;
88

9+
// EMIT_MIR gvn_repeat.index_place.GVN.diff
10+
#[custom_mir(dialect = "runtime")]
11+
pub fn index_place(mut idx1: usize, idx2: usize, array: [i32; 5]) -> i32 {
12+
// CHECK-LABEL: fn index_place(
13+
// CHECK: let mut [[ELEM:.*]]: &i32;
14+
// CHECK: _0 = copy (*[[ELEM]])
15+
mir! {
16+
let elem;
17+
{
18+
elem = &array[idx1];
19+
idx1 = idx2;
20+
RET = *elem;
21+
Return()
22+
}
23+
}
24+
}
25+
926
// EMIT_MIR gvn_repeat.repeat_place.GVN.diff
1027
#[custom_mir(dialect = "runtime")]
1128
pub fn repeat_place(mut idx1: usize, idx2: usize, val: &i32) -> i32 {
@@ -29,7 +46,8 @@ pub fn repeat_place(mut idx1: usize, idx2: usize, val: &i32) -> i32 {
2946
#[custom_mir(dialect = "runtime")]
3047
pub fn repeat_local(mut idx1: usize, idx2: usize, val: i32) -> i32 {
3148
// CHECK-LABEL: fn repeat_local(
32-
// CHECK: _0 = copy _3
49+
// CHECK: let mut [[ELEM:.*]]: &i32;
50+
// CHECK: _0 = copy (*[[ELEM]]);
3351
mir! {
3452
let array;
3553
let elem;
@@ -44,6 +62,7 @@ pub fn repeat_local(mut idx1: usize, idx2: usize, val: i32) -> i32 {
4462
}
4563

4664
fn main() {
65+
assert_eq!(index_place(0, 5, [0; 5]), 0);
4766
assert_eq!(repeat_place(0, 5, &0), 0);
4867
assert_eq!(repeat_local(0, 5, 0), 0);
4968
}

0 commit comments

Comments
 (0)