Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create compilation target versions of ::alloc::Layout #64299

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 19 additions & 21 deletions src/librustc/mir/interpret/allocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::{
};

use crate::mir;
use crate::ty::layout::{Size, Align};
use crate::ty::layout::{Size, MemoryPosition, Align};

use rustc_data_structures::sorted_map::SortedMap;
use rustc_target::abi::HasDataLayout;
Expand Down Expand Up @@ -39,10 +39,9 @@ pub struct Allocation<Tag = (),Extra = ()> {
relocations: Relocations<Tag>,
/// Denotes which part of this allocation is initialized.
undef_mask: UndefMask,
/// The size of the allocation. Currently, must always equal `bytes.len()`.
pub size: Size,
/// The alignment of the allocation to detect unaligned reads.
pub align: Align,
/// The position of the allocation.
/// Currently, the size must always equal `bytes.len()`.
pub mem_pos: MemoryPosition,
/// `true` if the allocation is mutable.
/// Also used by codegen to determine if a static should be put into mutable memory,
/// which happens for `static mut` and `static` with interior mutability.
Expand Down Expand Up @@ -101,12 +100,12 @@ impl<Tag> Allocation<Tag> {
pub fn from_bytes<'a>(slice: impl Into<Cow<'a, [u8]>>, align: Align) -> Self {
let bytes = slice.into().into_owned();
let size = Size::from_bytes(bytes.len() as u64);
let mem_pos = MemoryPosition::new(size, align);
Self {
bytes,
relocations: Relocations::new(),
undef_mask: UndefMask::new(size, true),
size,
align,
mem_pos,
mutability: Mutability::Immutable,
extra: (),
}
Expand All @@ -116,14 +115,13 @@ impl<Tag> Allocation<Tag> {
Allocation::from_bytes(slice, Align::from_bytes(1).unwrap())
}

pub fn undef(size: Size, align: Align) -> Self {
assert_eq!(size.bytes() as usize as u64, size.bytes());
pub fn undef(mem_pos: MemoryPosition) -> Self {
assert_eq!(mem_pos.size.bytes() as usize as u64, mem_pos.size.bytes());
Allocation {
bytes: vec![0; size.bytes() as usize],
bytes: vec![0; mem_pos.size.bytes() as usize],
relocations: Relocations::new(),
undef_mask: UndefMask::new(size, false),
size,
align,
undef_mask: UndefMask::new(mem_pos.size, false),
mem_pos: mem_pos,
mutability: Mutability::Mutable,
extra: (),
}
Expand All @@ -139,7 +137,6 @@ impl Allocation<(), ()> {
) -> Allocation<T, E> {
Allocation {
bytes: self.bytes,
size: self.size,
relocations: Relocations::from_presorted(
self.relocations.iter()
// The allocations in the relocations (pointers stored *inside* this allocation)
Expand All @@ -151,7 +148,7 @@ impl Allocation<(), ()> {
.collect()
),
undef_mask: self.undef_mask,
align: self.align,
mem_pos: self.mem_pos,
mutability: self.mutability,
extra,
}
Expand All @@ -161,7 +158,7 @@ impl Allocation<(), ()> {
/// Raw accessors. Provide access to otherwise private bytes.
impl<Tag, Extra> Allocation<Tag, Extra> {
pub fn len(&self) -> usize {
self.size.bytes() as usize
self.mem_pos.size.bytes() as usize
}

/// Looks at a slice which may describe undefined bytes or describe a relocation. This differs
Expand Down Expand Up @@ -398,7 +395,7 @@ impl<'tcx, Tag: Copy, Extra: AllocationExtra<Tag>> Allocation<Tag, Extra> {
// Now we do the actual reading.
let bits = read_target_uint(cx.data_layout().endian, bytes).unwrap();
// See if we got a pointer.
if size != cx.data_layout().pointer_size {
if size != cx.data_layout().pointer_pos.size {
// *Now*, we better make sure that the inside is free of relocations too.
self.check_relocations(cx, ptr, size)?;
} else {
Expand All @@ -424,7 +421,7 @@ impl<'tcx, Tag: Copy, Extra: AllocationExtra<Tag>> Allocation<Tag, Extra> {
ptr: Pointer<Tag>,
) -> InterpResult<'tcx, ScalarMaybeUndef<Tag>>
{
self.read_scalar(cx, ptr, cx.data_layout().pointer_size)
self.read_scalar(cx, ptr, cx.data_layout().pointer_pos.size)
}

/// Writes a *non-ZST* scalar.
Expand Down Expand Up @@ -486,7 +483,7 @@ impl<'tcx, Tag: Copy, Extra: AllocationExtra<Tag>> Allocation<Tag, Extra> {
val: ScalarMaybeUndef<Tag>
) -> InterpResult<'tcx>
{
let ptr_size = cx.data_layout().pointer_size;
let ptr_size = cx.data_layout().pointer_pos.size;
self.write_scalar(cx, ptr.into(), val, ptr_size)
}
}
Expand All @@ -500,9 +497,10 @@ impl<'tcx, Tag: Copy, Extra> Allocation<Tag, Extra> {
ptr: Pointer<Tag>,
size: Size,
) -> &[(Size, (Tag, AllocId))] {
let ptr_pos = cx.data_layout().pointer_pos;
// We have to go back `pointer_size - 1` bytes, as that one would still overlap with
// the beginning of this range.
let start = ptr.offset.bytes().saturating_sub(cx.data_layout().pointer_size.bytes() - 1);
let start = ptr.offset.bytes().saturating_sub(ptr_pos.size.bytes() - 1);
let end = ptr.offset + size; // This does overflow checking.
self.relocations.range(Size::from_bytes(start)..end)
}
Expand Down Expand Up @@ -543,7 +541,7 @@ impl<'tcx, Tag: Copy, Extra> Allocation<Tag, Extra> {
}

(relocations.first().unwrap().0,
relocations.last().unwrap().0 + cx.data_layout().pointer_size)
relocations.last().unwrap().0 + cx.data_layout().pointer_pos.size)
};
let start = ptr.offset;
let end = start + size;
Expand Down
8 changes: 4 additions & 4 deletions src/librustc/mir/interpret/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::hir;
use crate::hir::map::definitions::DefPathData;
use crate::mir;
use crate::ty::{self, Ty, layout};
use crate::ty::layout::{Size, Align, LayoutError};
use crate::ty::layout::{Size, MemoryPosition, Align, LayoutError};
use crate::ty::query::TyCtxtAt;

use backtrace::Backtrace;
Expand Down Expand Up @@ -438,7 +438,7 @@ pub enum UnsupportedOpInfo<'tcx> {
DeallocatedWrongMemoryKind(String, String),
ReallocateNonBasePtr,
DeallocateNonBasePtr,
IncorrectAllocationInformation(Size, Size, Align, Align),
IncorrectAllocationInformation(MemoryPosition, MemoryPosition),
HeapAllocZeroBytes,
HeapAllocNonPowerOfTwoAlignment(u64),
ReadFromReturnPointer,
Expand Down Expand Up @@ -484,10 +484,10 @@ impl fmt::Debug for UnsupportedOpInfo<'tcx> {
write!(f, "expected primitive type, got {}", ty),
PathNotFound(ref path) =>
write!(f, "cannot find path {:?}", path),
IncorrectAllocationInformation(size, size2, align, align2) =>
IncorrectAllocationInformation(expect, got) =>
write!(f, "incorrect alloc info: expected size {} and align {}, \
got size {} and align {}",
size.bytes(), align.bytes(), size2.bytes(), align2.bytes()),
expect.size.bytes(), expect.align.bytes(), got.size.bytes(), got.align.bytes()),
InvalidMemoryAccess =>
write!(f, "tried to access memory through an invalid pointer"),
DanglingPointerDeref =>
Expand Down
9 changes: 7 additions & 2 deletions src/librustc/mir/interpret/pointer.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::fmt::{self, Display};

use crate::mir;
use crate::ty::layout::{self, HasDataLayout, Size};
use crate::ty::layout::{self, HasDataLayout, LayoutPositionPref, Size};
use rustc_macros::HashStable;

use super::{AllocId, InterpResult};
Expand Down Expand Up @@ -35,9 +35,14 @@ impl Display for CheckInAllocMsg {
pub trait PointerArithmetic: layout::HasDataLayout {
// These are not supposed to be overridden.

#[inline(always)]
fn pointer_pos(&self) -> LayoutPositionPref {
self.data_layout().pointer_pos
}

#[inline(always)]
fn pointer_size(&self) -> Size {
self.data_layout().pointer_size
self.pointer_pos().size
}

/// Helper function: truncate given value-"overflowed flag" pair to pointer size and
Expand Down
14 changes: 7 additions & 7 deletions src/librustc/mir/interpret/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ impl<'tcx, Tag> Scalar<Tag> {
pub fn ptr_null(cx: &impl HasDataLayout) -> Self {
Scalar::Raw {
data: 0,
size: cx.data_layout().pointer_size.bytes() as u8,
size: cx.data_layout().pointer_pos.size.bytes() as u8,
}
}

Expand All @@ -205,7 +205,7 @@ impl<'tcx, Tag> Scalar<Tag> {
let dl = cx.data_layout();
match self {
Scalar::Raw { data, size } => {
assert_eq!(size as u64, dl.pointer_size.bytes());
assert_eq!(size as u64, dl.pointer_pos.size.bytes());
Ok(Scalar::Raw {
data: dl.offset(data as u64, i.bytes())? as u128,
size,
Expand All @@ -220,7 +220,7 @@ impl<'tcx, Tag> Scalar<Tag> {
let dl = cx.data_layout();
match self {
Scalar::Raw { data, size } => {
assert_eq!(size as u64, dl.pointer_size.bytes());
assert_eq!(size as u64, dl.pointer_pos.size.bytes());
Scalar::Raw {
data: dl.overflowing_offset(data as u64, i.bytes()).0 as u128,
size,
Expand Down Expand Up @@ -250,7 +250,7 @@ impl<'tcx, Tag> Scalar<Tag> {
let dl = cx.data_layout();
match self {
Scalar::Raw { data, size } => {
assert_eq!(size as u64, dl.pointer_size.bytes());
assert_eq!(size as u64, dl.pointer_pos.size.bytes());
Scalar::Raw {
data: dl.overflowing_signed_offset(data as u64, i128::from(i)).0 as u128,
size,
Expand Down Expand Up @@ -342,7 +342,7 @@ impl<'tcx, Tag> Scalar<Tag> {
Ok(data)
}
Scalar::Ptr(ptr) => {
assert_eq!(target_size, cx.data_layout().pointer_size);
assert_eq!(target_size, cx.data_layout().pointer_pos.size);
Err(ptr)
}
}
Expand Down Expand Up @@ -440,7 +440,7 @@ impl<'tcx, Tag> Scalar<Tag> {
}

pub fn to_machine_usize(self, cx: &impl HasDataLayout) -> InterpResult<'static, u64> {
let b = self.to_bits(cx.data_layout().pointer_size)?;
let b = self.to_bits(cx.data_layout().pointer_pos.size)?;
Ok(b as u64)
}

Expand All @@ -466,7 +466,7 @@ impl<'tcx, Tag> Scalar<Tag> {
}

pub fn to_machine_isize(self, cx: &impl HasDataLayout) -> InterpResult<'static, i64> {
let sz = cx.data_layout().pointer_size;
let sz = cx.data_layout().pointer_pos.size;
let b = self.to_bits(sz)?;
let b = sign_extend(b, sz) as i128;
Ok(b as i64)
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1502,7 +1502,7 @@ impl<'tcx> TerminatorKind<'tcx> {
SwitchInt { ref values, switch_ty, .. } => ty::tls::with(|tcx| {
let param_env = ty::ParamEnv::empty();
let switch_ty = tcx.lift(&switch_ty).unwrap();
let size = tcx.layout_of(param_env.and(switch_ty)).unwrap().size;
let size = tcx.layout_of(param_env.and(switch_ty)).unwrap().pref_pos.size;
values
.iter()
.map(|&u| {
Expand Down
9 changes: 4 additions & 5 deletions src/librustc/session/code_stats.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rustc_target::abi::{Align, Size};
use rustc_target::abi::{MemoryPosition, Size};
use rustc_data_structures::fx::{FxHashSet};
use std::cmp::{self, Ordering};
use rustc_data_structures::sync::Lock;
Expand Down Expand Up @@ -54,8 +54,7 @@ impl CodeStats {
pub fn record_type_size<S: ToString>(&self,
kind: DataTypeKind,
type_desc: S,
align: Align,
overall_size: Size,
mem_pos: MemoryPosition,
packed: bool,
opt_discr_size: Option<Size>,
mut variants: Vec<VariantInfo>) {
Expand All @@ -68,8 +67,8 @@ impl CodeStats {
let info = TypeSizeInfo {
kind,
type_description: type_desc.to_string(),
align: align.bytes(),
overall_size: overall_size.bytes(),
align: mem_pos.align.bytes(),
overall_size: mem_pos.size.bytes(),
packed: packed,
opt_discr_size: opt_discr_size.map(|s| s.bytes()),
variants,
Expand Down
Loading