optimize all ZST allocations into one single allocation
This commit is contained in:
parent
1720b1f4af
commit
594f1d79da
3 changed files with 50 additions and 7 deletions
|
@ -423,8 +423,8 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
|
||||||
"__rust_reallocate" => {
|
"__rust_reallocate" => {
|
||||||
let ptr = self.memory.read_ptr(args[0])?;
|
let ptr = self.memory.read_ptr(args[0])?;
|
||||||
let size = self.memory.read_usize(args[2])?;
|
let size = self.memory.read_usize(args[2])?;
|
||||||
self.memory.reallocate(ptr, size as usize)?;
|
let new_ptr = self.memory.reallocate(ptr, size as usize)?;
|
||||||
self.memory.write_ptr(dest, ptr)?;
|
self.memory.write_ptr(dest, new_ptr)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
"memcmp" => {
|
"memcmp" => {
|
||||||
|
|
|
@ -39,8 +39,18 @@ pub struct Pointer {
|
||||||
|
|
||||||
impl Pointer {
|
impl Pointer {
|
||||||
pub fn offset(self, i: isize) -> Self {
|
pub fn offset(self, i: isize) -> Self {
|
||||||
|
// FIXME: prevent offsetting ZST ptrs in tracing mode
|
||||||
Pointer { offset: (self.offset as isize + i) as usize, ..self }
|
Pointer { offset: (self.offset as isize + i) as usize, ..self }
|
||||||
}
|
}
|
||||||
|
pub fn points_to_zst(&self) -> bool {
|
||||||
|
self.alloc_id.0 == 0
|
||||||
|
}
|
||||||
|
fn zst_ptr() -> Self {
|
||||||
|
Pointer {
|
||||||
|
alloc_id: AllocId(0),
|
||||||
|
offset: 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq)]
|
#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq)]
|
||||||
|
@ -68,13 +78,25 @@ pub struct Memory<'a, 'tcx> {
|
||||||
|
|
||||||
impl<'a, 'tcx> Memory<'a, 'tcx> {
|
impl<'a, 'tcx> Memory<'a, 'tcx> {
|
||||||
pub fn new(layout: &'a TargetDataLayout) -> Self {
|
pub fn new(layout: &'a TargetDataLayout) -> Self {
|
||||||
Memory {
|
let mut mem = Memory {
|
||||||
alloc_map: HashMap::new(),
|
alloc_map: HashMap::new(),
|
||||||
functions: HashMap::new(),
|
functions: HashMap::new(),
|
||||||
function_alloc_cache: HashMap::new(),
|
function_alloc_cache: HashMap::new(),
|
||||||
next_id: AllocId(0),
|
next_id: AllocId(1),
|
||||||
layout: layout,
|
layout: layout,
|
||||||
}
|
};
|
||||||
|
// alloc id 0 is reserved for ZSTs, this is an optimization to prevent ZST
|
||||||
|
// (e.g. function pointers, (), [], ...) from requiring memory
|
||||||
|
let alloc = Allocation {
|
||||||
|
bytes: Vec::new(),
|
||||||
|
relocations: BTreeMap::new(),
|
||||||
|
undef_mask: UndefMask::new(0),
|
||||||
|
};
|
||||||
|
mem.alloc_map.insert(AllocId(0), alloc);
|
||||||
|
// check that additional zst allocs work
|
||||||
|
debug_assert!(mem.allocate(0).points_to_zst());
|
||||||
|
debug_assert!(mem.get(AllocId(0)).is_ok());
|
||||||
|
mem
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn allocations<'b>(&'b self) -> ::std::collections::hash_map::Iter<'b, AllocId, Allocation> {
|
pub fn allocations<'b>(&'b self) -> ::std::collections::hash_map::Iter<'b, AllocId, Allocation> {
|
||||||
|
@ -105,6 +127,9 @@ impl<'a, 'tcx> Memory<'a, 'tcx> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn allocate(&mut self, size: usize) -> Pointer {
|
pub fn allocate(&mut self, size: usize) -> Pointer {
|
||||||
|
if size == 0 {
|
||||||
|
return Pointer::zst_ptr();
|
||||||
|
}
|
||||||
let alloc = Allocation {
|
let alloc = Allocation {
|
||||||
bytes: vec![0; size],
|
bytes: vec![0; size],
|
||||||
relocations: BTreeMap::new(),
|
relocations: BTreeMap::new(),
|
||||||
|
@ -121,7 +146,14 @@ impl<'a, 'tcx> Memory<'a, 'tcx> {
|
||||||
|
|
||||||
// TODO(solson): Track which allocations were returned from __rust_allocate and report an error
|
// TODO(solson): Track which allocations were returned from __rust_allocate and report an error
|
||||||
// when reallocating/deallocating any others.
|
// when reallocating/deallocating any others.
|
||||||
pub fn reallocate(&mut self, ptr: Pointer, new_size: usize) -> EvalResult<'tcx, ()> {
|
pub fn reallocate(&mut self, ptr: Pointer, new_size: usize) -> EvalResult<'tcx, Pointer> {
|
||||||
|
if ptr.points_to_zst() {
|
||||||
|
if new_size != 0 {
|
||||||
|
return Ok(self.allocate(new_size));
|
||||||
|
} else {
|
||||||
|
return Ok(ptr);
|
||||||
|
}
|
||||||
|
}
|
||||||
if ptr.offset != 0 {
|
if ptr.offset != 0 {
|
||||||
// TODO(solson): Report error about non-__rust_allocate'd pointer.
|
// TODO(solson): Report error about non-__rust_allocate'd pointer.
|
||||||
return Err(EvalError::Unimplemented(format!("bad pointer offset: {}", ptr.offset)));
|
return Err(EvalError::Unimplemented(format!("bad pointer offset: {}", ptr.offset)));
|
||||||
|
@ -141,11 +173,14 @@ impl<'a, 'tcx> Memory<'a, 'tcx> {
|
||||||
alloc.undef_mask.truncate(new_size);
|
alloc.undef_mask.truncate(new_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(ptr)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO(solson): See comment on `reallocate`.
|
// TODO(solson): See comment on `reallocate`.
|
||||||
pub fn deallocate(&mut self, ptr: Pointer) -> EvalResult<'tcx, ()> {
|
pub fn deallocate(&mut self, ptr: Pointer) -> EvalResult<'tcx, ()> {
|
||||||
|
if ptr.points_to_zst() {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
if ptr.offset != 0 {
|
if ptr.offset != 0 {
|
||||||
// TODO(solson): Report error about non-__rust_allocate'd pointer.
|
// TODO(solson): Report error about non-__rust_allocate'd pointer.
|
||||||
return Err(EvalError::Unimplemented(format!("bad pointer offset: {}", ptr.offset)));
|
return Err(EvalError::Unimplemented(format!("bad pointer offset: {}", ptr.offset)));
|
||||||
|
|
|
@ -1,3 +1,9 @@
|
||||||
|
// the following flag prevents this test from running on the host machine
|
||||||
|
// this should only be run on miri, because rust doesn't (yet?) optimize ZSTs of different types
|
||||||
|
// into the same memory location
|
||||||
|
// ignore-test
|
||||||
|
|
||||||
|
|
||||||
#[derive(PartialEq, Debug)]
|
#[derive(PartialEq, Debug)]
|
||||||
struct A;
|
struct A;
|
||||||
|
|
||||||
|
@ -13,4 +19,6 @@ fn use_zst() -> A {
|
||||||
fn main() {
|
fn main() {
|
||||||
assert_eq!(zst_ret(), A);
|
assert_eq!(zst_ret(), A);
|
||||||
assert_eq!(use_zst(), A);
|
assert_eq!(use_zst(), A);
|
||||||
|
assert_eq!(&A as *const A as *const (), &() as *const _);
|
||||||
|
assert_eq!(&A as *const A, &A as *const A);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue