Skip to content

Commit d329d94

Browse files
committed
fix GVN trying to transmute pointers to integers
1 parent a8f9a32 commit d329d94

File tree

1 file changed

+23
-7
lines changed
  • compiler/rustc_mir_transform/src

1 file changed

+23
-7
lines changed

compiler/rustc_mir_transform/src/gvn.rs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ use rustc_middle::ty::layout::{HasParamEnv, LayoutOf};
103103
use rustc_middle::ty::{self, Ty, TyCtxt};
104104
use rustc_span::DUMMY_SP;
105105
use rustc_span::def_id::DefId;
106-
use rustc_target::abi::{self, Abi, FIRST_VARIANT, FieldIdx, Size, VariantIdx};
106+
use rustc_target::abi::{self, Abi, FIRST_VARIANT, FieldIdx, Primitive, Size, VariantIdx};
107107
use smallvec::SmallVec;
108108
use tracing::{debug, instrument, trace};
109109

@@ -568,13 +568,29 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
568568
CastKind::Transmute => {
569569
let value = self.evaluated[value].as_ref()?;
570570
let to = self.ecx.layout_of(to).ok()?;
571-
// `offset` for immediates only supports scalar/scalar-pair ABIs,
572-
// so bail out if the target is not one.
571+
// `offset` for immediates generally only supports projections that match the
572+
// type of the immediate. However, as a HACK, we exploit that it can also do
573+
// limited transmutes: it only works between types with the same layout, and
574+
// cannot transmute pointers to integers.
573575
if value.as_mplace_or_imm().is_right() {
574-
match (value.layout.abi, to.abi) {
575-
(Abi::Scalar(..), Abi::Scalar(..)) => {}
576-
(Abi::ScalarPair(..), Abi::ScalarPair(..)) => {}
577-
_ => return None,
576+
let can_transmute = match (value.layout.abi, to.abi) {
577+
(Abi::Scalar(s1), Abi::Scalar(s2)) => {
578+
s1.size(&self.ecx) == s2.size(&self.ecx)
579+
&& !matches!(s1.primitive(), Primitive::Pointer(..))
580+
}
581+
(Abi::ScalarPair(a1, b1), Abi::ScalarPair(a2, b2)) => {
582+
a1.size(&self.ecx) == a2.size(&self.ecx) &&
583+
b1.size(&self.ecx) == b2.size(&self.ecx) &&
584+
// The alignment of the second component determines its offset, so that also needs to match.
585+
b1.align(&self.ecx) == b2.align(&self.ecx) &&
586+
// None of the inputs may be a pointer.
587+
!matches!(a1.primitive(), Primitive::Pointer(..))
588+
&& !matches!(b1.primitive(), Primitive::Pointer(..))
589+
}
590+
_ => false,
591+
};
592+
if !can_transmute {
593+
return None;
578594
}
579595
}
580596
value.offset(Size::ZERO, to, &self.ecx).discard_err()?

0 commit comments

Comments
 (0)