Skip to content

Commit

Permalink
only allocate bytes within AllocRange
Browse files Browse the repository at this point in the history
  • Loading branch information
ouz-a committed Aug 9, 2023
1 parent c41339a commit 8f1ea57
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 34 deletions.
30 changes: 5 additions & 25 deletions compiler/rustc_smir/src/rustc_smir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@

use crate::rustc_internal::{self, opaque};
use crate::stable_mir::mir::{CopyNonOverlapping, UserTypeProjection, VariantIdx};
use crate::stable_mir::ty::{new_allocation, FloatTy, IntTy, Movability, RigidTy, TyKind, UintTy};
use crate::stable_mir::ty::{
allocation_filter, new_allocation, FloatTy, IntTy, Movability, RigidTy, TyKind, UintTy,
};
use crate::stable_mir::{self, Context};
use rustc_hir as hir;
use rustc_middle::mir::coverage::CodeRegion;
use rustc_middle::mir::interpret::alloc_range;
use rustc_middle::mir::{self, ConstantKind};
use rustc_middle::ty::{self, Ty, TyCtxt, Variance};
use rustc_span::def_id::{CrateNum, DefId, LOCAL_CRATE};
Expand Down Expand Up @@ -1080,30 +1083,7 @@ impl<'tcx> Stable<'tcx> for mir::interpret::Allocation {
type T = stable_mir::ty::Allocation;

fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
let size = self.size();
let mut bytes: Vec<Option<u8>> = self
.inspect_with_uninit_and_ptr_outside_interpreter(0..size.bytes_usize())
.iter()
.copied()
.map(Some)
.collect();
for (i, b) in bytes.iter_mut().enumerate() {
if !self.init_mask().get(rustc_target::abi::Size::from_bytes(i)) {
*b = None;
}
}
stable_mir::ty::Allocation {
bytes: bytes,
provenance: {
let mut ptrs = Vec::new();
for (size, prov) in self.provenance().ptrs().iter() {
ptrs.push((size.bytes_usize(), opaque(prov)));
}
stable_mir::ty::ProvenanceMap { ptrs }
},
align: self.align.bytes(),
mutability: self.mutability.stable(tables),
}
allocation_filter(self, alloc_range(rustc_target::abi::Size::ZERO, self.size()), tables)
}
}

Expand Down
51 changes: 42 additions & 9 deletions compiler/rustc_smir/src/stable_mir/ty.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use rustc_middle::mir::interpret::{alloc_range, ConstValue, Pointer};
use rustc_middle::mir::interpret::{alloc_range, AllocRange, ConstValue, Pointer};

use super::{mir::Mutability, mir::Safety, with, DefId};
use crate::{
rustc_internal::Opaque,
rustc_internal::{opaque, Opaque},
rustc_smir::{Stable, Tables},
};

Expand Down Expand Up @@ -353,17 +353,50 @@ pub fn new_allocation<'tcx>(
.layout_of(rustc_middle::ty::ParamEnv::reveal_all().and(const_kind.ty()))
.unwrap()
.size;
let bytes = alloc.0.get_bytes_unchecked(alloc_range(offset, ty_size));
let offset_allocation = rustc_middle::mir::interpret::Allocation::from_bytes(
bytes,
alloc.0.align,
alloc.0.mutability,
);
offset_allocation.stable(tables)
allocation_filter(&alloc.0, alloc_range(offset, ty_size), tables)
}
}
}

/// Creates an `Allocation` only from information within the `AllocRange`.
pub fn allocation_filter<'tcx>(
alloc: &rustc_middle::mir::interpret::Allocation,
alloc_range: AllocRange,
tables: &mut Tables<'tcx>,
) -> Allocation {
let mut bytes: Vec<Option<u8>> = alloc
.inspect_with_uninit_and_ptr_outside_interpreter(
alloc_range.start.bytes_usize()..alloc_range.end().bytes_usize(),
)
.iter()
.copied()
.map(Some)
.collect();
for (i, b) in bytes.iter_mut().enumerate() {
if !alloc
.init_mask()
.get(rustc_target::abi::Size::from_bytes(i + alloc_range.start.bytes_usize()))
{
*b = None;
}
}
let mut ptrs = Vec::new();
for (offset, prov) in alloc
.provenance()
.ptrs()
.iter()
.filter(|a| a.0 >= alloc_range.start && a.0 <= alloc_range.end())
{
ptrs.push((offset.bytes_usize() - alloc_range.start.bytes_usize(), opaque(prov)));
}
Allocation {
bytes: bytes,
provenance: ProvenanceMap { ptrs },
align: alloc.align.bytes(),
mutability: alloc.mutability.stable(tables),
}
}

#[derive(Clone, Debug)]
pub enum ConstantKind {
Allocated(Allocation),
Expand Down

0 comments on commit 8f1ea57

Please sign in to comment.