1
Fork 0

Don't reset cast kind without also updating the operand in simplify_cast

This commit is contained in:
Michael Goulet 2025-02-02 18:57:59 +00:00
parent c215e80aa9
commit de7d4a840e
4 changed files with 15 additions and 13 deletions

View file

@ -1367,16 +1367,17 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
fn simplify_cast( fn simplify_cast(
&mut self, &mut self,
kind: &mut CastKind, initial_kind: &mut CastKind,
operand: &mut Operand<'tcx>, initial_operand: &mut Operand<'tcx>,
to: Ty<'tcx>, to: Ty<'tcx>,
location: Location, location: Location,
) -> Option<VnIndex> { ) -> Option<VnIndex> {
use CastKind::*; use CastKind::*;
use rustc_middle::ty::adjustment::PointerCoercion::*; use rustc_middle::ty::adjustment::PointerCoercion::*;
let mut from = operand.ty(self.local_decls, self.tcx); let mut from = initial_operand.ty(self.local_decls, self.tcx);
let mut value = self.simplify_operand(operand, location)?; let mut kind = *initial_kind;
let mut value = self.simplify_operand(initial_operand, location)?;
if from == to { if from == to {
return Some(value); return Some(value);
} }
@ -1400,7 +1401,7 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
&& to.is_unsafe_ptr() && to.is_unsafe_ptr()
&& self.pointers_have_same_metadata(from, to) && self.pointers_have_same_metadata(from, to)
{ {
*kind = PtrToPtr; kind = PtrToPtr;
was_updated_this_iteration = true; was_updated_this_iteration = true;
} }
@ -1443,7 +1444,7 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
to: inner_to, to: inner_to,
} = *self.get(value) } = *self.get(value)
{ {
let new_kind = match (inner_kind, *kind) { let new_kind = match (inner_kind, kind) {
// Even if there's a narrowing cast in here that's fine, because // Even if there's a narrowing cast in here that's fine, because
// things like `*mut [i32] -> *mut i32 -> *const i32` and // things like `*mut [i32] -> *mut i32 -> *const i32` and
// `*mut [i32] -> *const [i32] -> *const i32` can skip the middle in MIR. // `*mut [i32] -> *const [i32] -> *const i32` can skip the middle in MIR.
@ -1471,7 +1472,7 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
_ => None, _ => None,
}; };
if let Some(new_kind) = new_kind { if let Some(new_kind) = new_kind {
*kind = new_kind; kind = new_kind;
from = inner_from; from = inner_from;
value = inner_value; value = inner_value;
was_updated_this_iteration = true; was_updated_this_iteration = true;
@ -1489,10 +1490,11 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
} }
if was_ever_updated && let Some(op) = self.try_as_operand(value, location) { if was_ever_updated && let Some(op) = self.try_as_operand(value, location) {
*operand = op; *initial_operand = op;
*initial_kind = kind;
} }
Some(self.insert(Value::Cast { kind: *kind, value, from, to })) Some(self.insert(Value::Cast { kind, value, from, to }))
} }
fn simplify_len(&mut self, place: &mut Place<'tcx>, location: Location) -> Option<VnIndex> { fn simplify_len(&mut self, place: &mut Place<'tcx>, location: Location) -> Option<VnIndex> {

View file

@ -4,7 +4,7 @@
// EMIT_MIR dont_reset_cast_kind_without_updating_operand.test.GVN.diff // EMIT_MIR dont_reset_cast_kind_without_updating_operand.test.GVN.diff
fn test() { fn test() {
let vp_ctx: &Box<()>= &Box::new(()); let vp_ctx: &Box<()> = &Box::new(());
let slf: *const () = &raw const **vp_ctx; let slf: *const () = &raw const **vp_ctx;
let bytes = std::ptr::slice_from_raw_parts(slf, 1); let bytes = std::ptr::slice_from_raw_parts(slf, 1);
let _x = foo(bytes); let _x = foo(bytes);
@ -12,4 +12,4 @@ fn test() {
fn foo(bytes: *const [()]) -> *mut () { fn foo(bytes: *const [()]) -> *mut () {
bytes as *mut () bytes as *mut ()
} }

View file

@ -141,7 +141,7 @@
StorageLive(_8); StorageLive(_8);
_8 = copy _5; _8 = copy _5;
- _7 = copy _8 as *mut () (PtrToPtr); - _7 = copy _8 as *mut () (PtrToPtr);
+ _7 = copy _5 as *mut () (Transmute); + _7 = copy _5 as *mut () (PtrToPtr);
StorageDead(_8); StorageDead(_8);
StorageDead(_7); StorageDead(_7);
- StorageDead(_5); - StorageDead(_5);

View file

@ -66,7 +66,7 @@
StorageLive(_8); StorageLive(_8);
_8 = copy _5; _8 = copy _5;
- _7 = copy _8 as *mut () (PtrToPtr); - _7 = copy _8 as *mut () (PtrToPtr);
+ _7 = copy _5 as *mut () (Transmute); + _7 = copy _5 as *mut () (PtrToPtr);
StorageDead(_8); StorageDead(_8);
StorageDead(_7); StorageDead(_7);
- StorageDead(_5); - StorageDead(_5);