interpret: clean up deduplicating allocation functions
This commit is contained in:
parent
f33a8c6426
commit
73dc95dad1
8 changed files with 23 additions and 137 deletions
|
@ -5,8 +5,7 @@
|
|||
use std::assert_matches::assert_matches;
|
||||
|
||||
use either::{Either, Left, Right};
|
||||
use rustc_abi::{Align, BackendRepr, HasDataLayout, Size};
|
||||
use rustc_ast::Mutability;
|
||||
use rustc_abi::{BackendRepr, HasDataLayout, Size};
|
||||
use rustc_middle::ty::Ty;
|
||||
use rustc_middle::ty::layout::{LayoutOf, TyAndLayout};
|
||||
use rustc_middle::{bug, mir, span_bug};
|
||||
|
@ -1018,40 +1017,31 @@ where
|
|||
self.allocate_dyn(layout, kind, MemPlaceMeta::None)
|
||||
}
|
||||
|
||||
/// Allocates a sequence of bytes in the interpreter's memory.
|
||||
/// For immutable allocations, uses deduplication to reuse existing memory.
|
||||
/// For mutable allocations, creates a new unique allocation.
|
||||
pub fn allocate_bytes(
|
||||
/// Allocates a sequence of bytes in the interpreter's memory with alignment 1.
|
||||
/// This is allocated in immutable global memory and deduplicated.
|
||||
pub fn allocate_bytes_dedup(
|
||||
&mut self,
|
||||
bytes: &[u8],
|
||||
align: Align,
|
||||
kind: MemoryKind<M::MemoryKind>,
|
||||
mutbl: Mutability,
|
||||
) -> InterpResult<'tcx, Pointer<M::Provenance>> {
|
||||
// Use cache for immutable strings.
|
||||
if mutbl.is_not() {
|
||||
// Use dedup'd allocation function.
|
||||
let salt = M::get_global_alloc_salt(self, None);
|
||||
let id = self.tcx.allocate_bytes_dedup(bytes, salt);
|
||||
let salt = M::get_global_alloc_salt(self, None);
|
||||
let id = self.tcx.allocate_bytes_dedup(bytes, salt);
|
||||
|
||||
// Turn untagged "global" pointers (obtained via `tcx`) into the machine pointer to the allocation.
|
||||
M::adjust_alloc_root_pointer(&self, Pointer::from(id), Some(kind))
|
||||
} else {
|
||||
// Allocate new memory for mutable data.
|
||||
self.allocate_bytes_ptr(bytes, align, kind, mutbl)
|
||||
}
|
||||
// Turn untagged "global" pointers (obtained via `tcx`) into the machine pointer to the allocation.
|
||||
M::adjust_alloc_root_pointer(
|
||||
&self,
|
||||
Pointer::from(id),
|
||||
M::GLOBAL_KIND.map(MemoryKind::Machine),
|
||||
)
|
||||
}
|
||||
|
||||
/// Allocates a string in the interpreter's memory with metadata for length.
|
||||
/// Uses `allocate_bytes` internally but adds string-specific metadata handling.
|
||||
pub fn allocate_str(
|
||||
/// Allocates a string in the interpreter's memory, returning it as a (wide) place.
|
||||
/// This is allocated in immutable global memory and deduplicated.
|
||||
pub fn allocate_str_dedup(
|
||||
&mut self,
|
||||
str: &str,
|
||||
kind: MemoryKind<M::MemoryKind>,
|
||||
mutbl: Mutability,
|
||||
) -> InterpResult<'tcx, MPlaceTy<'tcx, M::Provenance>> {
|
||||
let bytes = str.as_bytes();
|
||||
let ptr = self.allocate_bytes(bytes, Align::ONE, kind, mutbl)?;
|
||||
let ptr = self.allocate_bytes_dedup(bytes)?;
|
||||
|
||||
// Create length metadata for the string.
|
||||
let meta = Scalar::from_target_usize(u64::try_from(bytes.len()).unwrap(), self);
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
use rustc_hir::LangItem;
|
||||
use rustc_middle::query::TyCtxtAt;
|
||||
use rustc_middle::ty::layout::LayoutOf;
|
||||
use rustc_middle::ty::{self, Mutability};
|
||||
use rustc_middle::ty::{self};
|
||||
use rustc_middle::{bug, mir};
|
||||
use rustc_span::symbol::Symbol;
|
||||
use tracing::trace;
|
||||
|
@ -20,12 +20,9 @@ fn alloc_caller_location<'tcx>(
|
|||
// This can fail if rustc runs out of memory right here. Trying to emit an error would be
|
||||
// pointless, since that would require allocating more memory than these short strings.
|
||||
let file = if loc_details.file {
|
||||
ecx.allocate_str(filename.as_str(), MemoryKind::CallerLocation, Mutability::Not).unwrap()
|
||||
ecx.allocate_str_dedup(filename.as_str()).unwrap()
|
||||
} else {
|
||||
// FIXME: This creates a new allocation each time. It might be preferable to
|
||||
// perform this allocation only once, and re-use the `MPlaceTy`.
|
||||
// See https://github.com/rust-lang/rust/pull/89920#discussion_r730012398
|
||||
ecx.allocate_str("<redacted>", MemoryKind::CallerLocation, Mutability::Not).unwrap()
|
||||
ecx.allocate_str_dedup("<redacted>").unwrap()
|
||||
};
|
||||
let file = file.map_provenance(CtfeProvenance::as_immutable);
|
||||
let line = if loc_details.line { Scalar::from_u32(line) } else { Scalar::from_u32(0) };
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue