Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
322 changes: 191 additions & 131 deletions compiler/rustc_mir_transform/src/gvn.rs

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion tests/mir-opt/gvn.dereference_indexing.GVN.panic-abort.diff
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
+ nop;
StorageLive(_8);
StorageLive(_9);
_9 = copy (*_3);
- _9 = copy (*_3);
+ _9 = copy _1[_4];
_8 = opaque::<u8>(move _9) -> [return: bb2, unwind unreachable];
}

Expand Down
3 changes: 2 additions & 1 deletion tests/mir-opt/gvn.dereference_indexing.GVN.panic-unwind.diff
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
+ nop;
StorageLive(_8);
StorageLive(_9);
_9 = copy (*_3);
- _9 = copy (*_3);
+ _9 = copy _1[_4];
_8 = opaque::<u8>(move _9) -> [return: bb2, unwind continue];
}

Expand Down
4 changes: 2 additions & 2 deletions tests/mir-opt/gvn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1065,8 +1065,8 @@ fn dereference_indexing(array: [u8; 2], index: usize) {
&array[i]
};

// CHECK-NOT: [{{.*}}]
// CHECK: [[tmp:_.*]] = copy (*[[a]]);
// CHECK-NOT: StorageDead([[i]]);
// CHECK: [[tmp:_.*]] = copy _1[[[i]]];
// CHECK: opaque::<u8>(move [[tmp]])
opaque(*a);
}
Expand Down
15 changes: 15 additions & 0 deletions tests/mir-opt/gvn_repeat.index_place.GVN.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
- // MIR for `index_place` before GVN
+ // MIR for `index_place` after GVN

fn index_place(_1: usize, _2: usize, _3: [i32; 5]) -> i32 {
let mut _0: i32;
let mut _4: &i32;

bb0: {
_4 = &_3[_1];
_1 = copy _2;
_0 = copy (*_4);
return;
}
}

3 changes: 1 addition & 2 deletions tests/mir-opt/gvn_repeat.repeat_local.GVN.diff
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
_4 = [copy _3; 5];
_5 = &_4[_1];
_1 = copy _2;
- _0 = copy (*_5);
+ _0 = copy _3;
_0 = copy (*_5);
return;
}
}
Expand Down
21 changes: 20 additions & 1 deletion tests/mir-opt/gvn_repeat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,23 @@

use std::intrinsics::mir::*;

// EMIT_MIR gvn_repeat.index_place.GVN.diff
#[custom_mir(dialect = "runtime")]
pub fn index_place(mut idx1: usize, idx2: usize, array: [i32; 5]) -> i32 {
// CHECK-LABEL: fn index_place(
// CHECK: let mut [[ELEM:.*]]: &i32;
// CHECK: _0 = copy (*[[ELEM]])
mir! {
let elem;
{
elem = &array[idx1];
idx1 = idx2;
RET = *elem;
Return()
}
}
}

// EMIT_MIR gvn_repeat.repeat_place.GVN.diff
#[custom_mir(dialect = "runtime")]
pub fn repeat_place(mut idx1: usize, idx2: usize, val: &i32) -> i32 {
Expand All @@ -29,7 +46,8 @@ pub fn repeat_place(mut idx1: usize, idx2: usize, val: &i32) -> i32 {
#[custom_mir(dialect = "runtime")]
pub fn repeat_local(mut idx1: usize, idx2: usize, val: i32) -> i32 {
// CHECK-LABEL: fn repeat_local(
// CHECK: _0 = copy _3
// CHECK: let mut [[ELEM:.*]]: &i32;
// CHECK: _0 = copy (*[[ELEM]]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we leave a FIXME here?

Copy link
Contributor Author

@cjgillot cjgillot Oct 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new result is more correct, so I'd rather not. The current annotation was more of a bug: idx1 is not SSA, so we should never have been able to refer to &array[idx1].

mir! {
let array;
let elem;
Expand All @@ -44,6 +62,7 @@ pub fn repeat_local(mut idx1: usize, idx2: usize, val: i32) -> i32 {
}

fn main() {
assert_eq!(index_place(0, 5, [0; 5]), 0);
assert_eq!(repeat_place(0, 5, &0), 0);
assert_eq!(repeat_local(0, 5, 0), 0);
}
Loading