diff options
| author | Ralf Jung <post@ralfj.de> | 2024-12-09 08:52:23 +0100 |
|---|---|---|
| committer | Ralf Jung <post@ralfj.de> | 2024-12-09 15:12:33 +0100 |
| commit | 73dc95dad19e2fa513ea8ac2b76231474398c8ec (patch) | |
| tree | 76e9fef0593af5d1f0aeead0b7f3ed9d0d67da04 /compiler/rustc_const_eval/src/interpret | |
| parent | f33a8c6426074b7ce8d08740e9805fdca96ee150 (diff) | |
| download | rust-73dc95dad19e2fa513ea8ac2b76231474398c8ec.tar.gz rust-73dc95dad19e2fa513ea8ac2b76231474398c8ec.zip | |
interpret: clean up deduplicating allocation functions
Diffstat (limited to 'compiler/rustc_const_eval/src/interpret')
| -rw-r--r-- | compiler/rustc_const_eval/src/interpret/place.rs | 44 |
1 files changed, 17 insertions, 27 deletions
diff --git a/compiler/rustc_const_eval/src/interpret/place.rs b/compiler/rustc_const_eval/src/interpret/place.rs index f54a932e1b6..810e9356b26 100644 --- a/compiler/rustc_const_eval/src/interpret/place.rs +++ b/compiler/rustc_const_eval/src/interpret/place.rs @@ -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); - - // 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) - } + 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), + 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); |
