Skip to content

Commit

Permalink
Do not read a scalar on a non-scalar layout.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjgillot committed Jan 6, 2024
1 parent c6ebff8 commit 88f2fd8
Show file tree
Hide file tree
Showing 4 changed files with 818 additions and 2 deletions.
9 changes: 7 additions & 2 deletions compiler/rustc_mir_transform/src/gvn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -943,8 +943,13 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {

let as_bits = |value| {
let constant = self.evaluated[value].as_ref()?;
let scalar = self.ecx.read_scalar(constant).ok()?;
scalar.to_bits(constant.layout.size).ok()
if layout.abi.is_scalar() {
let scalar = self.ecx.read_scalar(constant).ok()?;
scalar.to_bits(constant.layout.size).ok()
} else {
// `constant` is a wide pointer. Do not evaluate to bits.
None
}
};

// Represent the values as `Ok(bits)` or `Err(VnIndex)`.
Expand Down
23 changes: 23 additions & 0 deletions tests/mir-opt/gvn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,27 @@ fn constant_index_overflow<T: Copy>(x: &[T]) {
opaque(b)
}

fn wide_ptr_ops() {
let a: *const dyn Send = &1 as &dyn Send;
let b: *const dyn Send = &1 as &dyn Send;
let _val = a == b;
let _val = a != b;
let _val = a < b;
let _val = a <= b;
let _val = a > b;
let _val = a >= b;

let a: *const [u8] = unsafe { transmute((1usize, 1usize)) };
let b: *const [u8] = unsafe { transmute((1usize, 2usize)) };

opaque(!(a == b));
opaque(a != b);
opaque(a <= b);
opaque(a < b);
opaque(!(a >= b));
opaque(!(a > b));
}

fn main() {
subexpression_elimination(2, 4, 5);
wrap_unwrap(5);
Expand All @@ -664,6 +685,7 @@ fn main() {
fn_pointers();
indirect_static();
constant_index_overflow(&[5, 3]);
wide_ptr_ops();
}

#[inline(never)]
Expand Down Expand Up @@ -692,3 +714,4 @@ fn identity<T>(x: T) -> T {
// EMIT_MIR gvn.fn_pointers.GVN.diff
// EMIT_MIR gvn.indirect_static.GVN.diff
// EMIT_MIR gvn.constant_index_overflow.GVN.diff
// EMIT_MIR gvn.wide_ptr_ops.GVN.diff
Loading

0 comments on commit 88f2fd8

Please sign in to comment.