Skip to content

Commit 7d63efd

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

File tree

3 files changed

+31
-15
lines changed

3 files changed

+31
-15
lines changed

compiler/rustc_mir_transform/src/gvn.rs

+23-7
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()?

tests/coverage/closure_macro.cov-map

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,19 @@ Number of file 0 mappings: 6
2323
- Code(Counter(0)) at (prev + 3, 1) to (start + 0, 2)
2424

2525
Function name: closure_macro::main::{closure#0}
26-
Raw bytes (35): 0x[01, 01, 03, 01, 05, 05, 0b, 09, 00, 05, 01, 10, 1c, 03, 21, 05, 04, 11, 01, 27, 02, 03, 11, 00, 16, 00, 00, 17, 00, 1e, 07, 02, 09, 00, 0a]
26+
Raw bytes (35): 0x[01, 01, 03, 01, 05, 05, 0b, 09, 0d, 05, 01, 10, 1c, 03, 21, 05, 04, 11, 01, 27, 02, 03, 11, 00, 16, 0d, 00, 17, 00, 1e, 07, 02, 09, 00, 0a]
2727
Number of files: 1
2828
- file 0 => global file 1
2929
Number of expressions: 3
3030
- expression 0 operands: lhs = Counter(0), rhs = Counter(1)
3131
- expression 1 operands: lhs = Counter(1), rhs = Expression(2, Add)
32-
- expression 2 operands: lhs = Counter(2), rhs = Zero
32+
- expression 2 operands: lhs = Counter(2), rhs = Counter(3)
3333
Number of file 0 mappings: 5
3434
- Code(Counter(0)) at (prev + 16, 28) to (start + 3, 33)
3535
- Code(Counter(1)) at (prev + 4, 17) to (start + 1, 39)
3636
- Code(Expression(0, Sub)) at (prev + 3, 17) to (start + 0, 22)
3737
= (c0 - c1)
38-
- Code(Zero) at (prev + 0, 23) to (start + 0, 30)
38+
- Code(Counter(3)) at (prev + 0, 23) to (start + 0, 30)
3939
- Code(Expression(1, Add)) at (prev + 2, 9) to (start + 0, 10)
40-
= (c1 + (c2 + Zero))
40+
= (c1 + (c2 + c3))
4141

tests/coverage/closure_macro_async.cov-map

+4-4
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,19 @@ Number of file 0 mappings: 6
3131
- Code(Counter(0)) at (prev + 3, 1) to (start + 0, 2)
3232

3333
Function name: closure_macro_async::test::{closure#0}::{closure#0}
34-
Raw bytes (35): 0x[01, 01, 03, 01, 05, 05, 0b, 09, 00, 05, 01, 15, 1c, 03, 21, 05, 04, 11, 01, 27, 02, 03, 11, 00, 16, 00, 00, 17, 00, 1e, 07, 02, 09, 00, 0a]
34+
Raw bytes (35): 0x[01, 01, 03, 01, 05, 05, 0b, 09, 0d, 05, 01, 15, 1c, 03, 21, 05, 04, 11, 01, 27, 02, 03, 11, 00, 16, 0d, 00, 17, 00, 1e, 07, 02, 09, 00, 0a]
3535
Number of files: 1
3636
- file 0 => global file 1
3737
Number of expressions: 3
3838
- expression 0 operands: lhs = Counter(0), rhs = Counter(1)
3939
- expression 1 operands: lhs = Counter(1), rhs = Expression(2, Add)
40-
- expression 2 operands: lhs = Counter(2), rhs = Zero
40+
- expression 2 operands: lhs = Counter(2), rhs = Counter(3)
4141
Number of file 0 mappings: 5
4242
- Code(Counter(0)) at (prev + 21, 28) to (start + 3, 33)
4343
- Code(Counter(1)) at (prev + 4, 17) to (start + 1, 39)
4444
- Code(Expression(0, Sub)) at (prev + 3, 17) to (start + 0, 22)
4545
= (c0 - c1)
46-
- Code(Zero) at (prev + 0, 23) to (start + 0, 30)
46+
- Code(Counter(3)) at (prev + 0, 23) to (start + 0, 30)
4747
- Code(Expression(1, Add)) at (prev + 2, 9) to (start + 0, 10)
48-
= (c1 + (c2 + Zero))
48+
= (c1 + (c2 + c3))
4949

0 commit comments

Comments
 (0)