Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
GVN: Preserve derefs at unreachable
  • Loading branch information
dianqk committed Oct 14, 2025
commit 28c79602eec8a90ef5af42b5859d3c680cc48d80
4 changes: 3 additions & 1 deletion compiler/rustc_mir_transform/src/gvn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1930,7 +1930,9 @@ impl<'tcx> MutVisitor<'tcx> for VnState<'_, '_, 'tcx> {
// Currently, only preserving derefs for trivial terminators like SwitchInt and Goto.
let safe_to_preserve_derefs = matches!(
terminator.kind,
TerminatorKind::SwitchInt { .. } | TerminatorKind::Goto { .. }
TerminatorKind::SwitchInt { .. }
| TerminatorKind::Goto { .. }
| TerminatorKind::Unreachable
);
if !safe_to_preserve_derefs {
self.invalidate_derefs();
Expand Down
5 changes: 4 additions & 1 deletion tests/mir-opt/pre-codegen/two_unwrap_unchecked.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
// skip-filecheck
//@ compile-flags: -O

#![crate_type = "lib"]

// EMIT_MIR two_unwrap_unchecked.two_unwrap_unchecked.GVN.diff
// EMIT_MIR two_unwrap_unchecked.two_unwrap_unchecked.PreCodegen.after.mir
pub fn two_unwrap_unchecked(v: &Option<i32>) -> i32 {
// CHECK-LABEL: fn two_unwrap_unchecked(
// CHECK: [[DEREF_V:_.*]] = copy (*_1);
// CHECK: [[V1V2:_.*]] = copy (([[DEREF_V]] as Some).0: i32);
// CHECK: _0 = Add(copy [[V1V2]], copy [[V1V2]]);
let v1 = unsafe { v.unwrap_unchecked() };
let v2 = unsafe { v.unwrap_unchecked() };
v1 + v2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,15 @@

bb0: {
- StorageLive(_2);
- StorageLive(_3);
+ nop;
+ nop;
StorageLive(_3);
_3 = copy (*_1);
StorageLive(_8);
- StorageLive(_8);
+ nop;
_8 = discriminant(_3);
switchInt(move _8) -> [0: bb2, 1: bb3, otherwise: bb1];
- switchInt(move _8) -> [0: bb2, 1: bb3, otherwise: bb1];
+ switchInt(copy _8) -> [0: bb2, 1: bb3, otherwise: bb1];
}

bb1: {
Expand All @@ -58,37 +61,43 @@
}

bb3: {
_2 = move ((_3 as Some).0: i32);
StorageDead(_8);
StorageDead(_3);
- StorageLive(_4);
- _2 = move ((_3 as Some).0: i32);
- StorageDead(_8);
- StorageDead(_3);
+ _2 = copy ((_3 as Some).0: i32);
+ nop;
+ nop;
StorageLive(_4);
StorageLive(_5);
_5 = copy (*_1);
- _5 = copy (*_1);
+ _5 = copy _3;
StorageLive(_9);
_9 = discriminant(_5);
switchInt(move _9) -> [0: bb4, 1: bb5, otherwise: bb1];
- _9 = discriminant(_5);
- switchInt(move _9) -> [0: bb4, 1: bb5, otherwise: bb1];
+ _9 = copy _8;
+ switchInt(copy _8) -> [0: bb4, 1: bb5, otherwise: bb1];
}

bb4: {
unreachable;
}

bb5: {
_4 = move ((_5 as Some).0: i32);
- _4 = move ((_5 as Some).0: i32);
+ _4 = copy _2;
StorageDead(_9);
StorageDead(_5);
StorageLive(_6);
_6 = copy _2;
StorageLive(_7);
_7 = copy _4;
- _7 = copy _4;
- _0 = Add(move _6, move _7);
+ _0 = Add(copy _2, copy _4);
+ _7 = copy _2;
+ _0 = Add(copy _2, copy _2);
StorageDead(_7);
StorageDead(_6);
- StorageDead(_4);
StorageDead(_4);
- StorageDead(_2);
+ nop;
+ nop;
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@ fn two_unwrap_unchecked(_1: &Option<i32>) -> i32 {
let mut _0: i32;
let mut _2: std::option::Option<i32>;
let _4: i32;
let mut _5: std::option::Option<i32>;
scope 1 {
debug v1 => _4;
let _7: i32;
scope 2 {
debug v2 => _7;
debug v2 => _4;
}
scope 8 (inlined #[track_caller] Option::<i32>::unwrap_unchecked) {
let mut _6: isize;
scope 9 {
}
scope 10 (inlined #[track_caller] unreachable_unchecked) {
Expand All @@ -37,33 +34,18 @@ fn two_unwrap_unchecked(_1: &Option<i32>) -> i32 {
}

bb0: {
StorageLive(_2);
_2 = copy (*_1);
StorageLive(_3);
_3 = discriminant(_2);
switchInt(move _3) -> [0: bb3, 1: bb1, otherwise: bb3];
switchInt(copy _3) -> [0: bb2, 1: bb1, otherwise: bb2];
}

bb1: {
_4 = move ((_2 as Some).0: i32);
StorageDead(_3);
StorageDead(_2);
StorageLive(_5);
_5 = copy (*_1);
StorageLive(_6);
_6 = discriminant(_5);
switchInt(move _6) -> [0: bb3, 1: bb2, otherwise: bb3];
}

bb2: {
_7 = move ((_5 as Some).0: i32);
StorageDead(_6);
StorageDead(_5);
_0 = Add(copy _4, copy _7);
_4 = copy ((_2 as Some).0: i32);
_0 = Add(copy _4, copy _4);
return;
}

bb3: {
bb2: {
unreachable;
}
}