Skip to content

Commit 73dc95d

Browse files
committed
interpret: clean up deduplicating allocation functions
1 parent f33a8c6 commit 73dc95d

File tree

8 files changed

+24
-138
lines changed

8 files changed

+24
-138
lines changed

Diff for: compiler/rustc_const_eval/src/interpret/place.rs

+17-27
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
use std::assert_matches::assert_matches;
66

77
use either::{Either, Left, Right};
8-
use rustc_abi::{Align, BackendRepr, HasDataLayout, Size};
9-
use rustc_ast::Mutability;
8+
use rustc_abi::{BackendRepr, HasDataLayout, Size};
109
use rustc_middle::ty::Ty;
1110
use rustc_middle::ty::layout::{LayoutOf, TyAndLayout};
1211
use rustc_middle::{bug, mir, span_bug};
@@ -1018,40 +1017,31 @@ where
10181017
self.allocate_dyn(layout, kind, MemPlaceMeta::None)
10191018
}
10201019

1021-
/// Allocates a sequence of bytes in the interpreter's memory.
1022-
/// For immutable allocations, uses deduplication to reuse existing memory.
1023-
/// For mutable allocations, creates a new unique allocation.
1024-
pub fn allocate_bytes(
1020+
/// Allocates a sequence of bytes in the interpreter's memory with alignment 1.
1021+
/// This is allocated in immutable global memory and deduplicated.
1022+
pub fn allocate_bytes_dedup(
10251023
&mut self,
10261024
bytes: &[u8],
1027-
align: Align,
1028-
kind: MemoryKind<M::MemoryKind>,
1029-
mutbl: Mutability,
10301025
) -> InterpResult<'tcx, Pointer<M::Provenance>> {
1031-
// Use cache for immutable strings.
1032-
if mutbl.is_not() {
1033-
// Use dedup'd allocation function.
1034-
let salt = M::get_global_alloc_salt(self, None);
1035-
let id = self.tcx.allocate_bytes_dedup(bytes, salt);
1036-
1037-
// Turn untagged "global" pointers (obtained via `tcx`) into the machine pointer to the allocation.
1038-
M::adjust_alloc_root_pointer(&self, Pointer::from(id), Some(kind))
1039-
} else {
1040-
// Allocate new memory for mutable data.
1041-
self.allocate_bytes_ptr(bytes, align, kind, mutbl)
1042-
}
1026+
let salt = M::get_global_alloc_salt(self, None);
1027+
let id = self.tcx.allocate_bytes_dedup(bytes, salt);
1028+
1029+
// Turn untagged "global" pointers (obtained via `tcx`) into the machine pointer to the allocation.
1030+
M::adjust_alloc_root_pointer(
1031+
&self,
1032+
Pointer::from(id),
1033+
M::GLOBAL_KIND.map(MemoryKind::Machine),
1034+
)
10431035
}
10441036

1045-
/// Allocates a string in the interpreter's memory with metadata for length.
1046-
/// Uses `allocate_bytes` internally but adds string-specific metadata handling.
1047-
pub fn allocate_str(
1037+
/// Allocates a string in the interpreter's memory, returning it as a (wide) place.
1038+
/// This is allocated in immutable global memory and deduplicated.
1039+
pub fn allocate_str_dedup(
10481040
&mut self,
10491041
str: &str,
1050-
kind: MemoryKind<M::MemoryKind>,
1051-
mutbl: Mutability,
10521042
) -> InterpResult<'tcx, MPlaceTy<'tcx, M::Provenance>> {
10531043
let bytes = str.as_bytes();
1054-
let ptr = self.allocate_bytes(bytes, Align::ONE, kind, mutbl)?;
1044+
let ptr = self.allocate_bytes_dedup(bytes)?;
10551045

10561046
// Create length metadata for the string.
10571047
let meta = Scalar::from_target_usize(u64::try_from(bytes.len()).unwrap(), self);

Diff for: compiler/rustc_const_eval/src/util/caller_location.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use rustc_hir::LangItem;
22
use rustc_middle::query::TyCtxtAt;
33
use rustc_middle::ty::layout::LayoutOf;
4-
use rustc_middle::ty::{self, Mutability};
4+
use rustc_middle::ty::{self};
55
use rustc_middle::{bug, mir};
66
use rustc_span::symbol::Symbol;
77
use tracing::trace;
@@ -20,12 +20,9 @@ fn alloc_caller_location<'tcx>(
2020
// This can fail if rustc runs out of memory right here. Trying to emit an error would be
2121
// pointless, since that would require allocating more memory than these short strings.
2222
let file = if loc_details.file {
23-
ecx.allocate_str(filename.as_str(), MemoryKind::CallerLocation, Mutability::Not).unwrap()
23+
ecx.allocate_str_dedup(filename.as_str()).unwrap()
2424
} else {
25-
// FIXME: This creates a new allocation each time. It might be preferable to
26-
// perform this allocation only once, and re-use the `MPlaceTy`.
27-
// See https://github.com/rust-lang/rust/pull/89920#discussion_r730012398
28-
ecx.allocate_str("<redacted>", MemoryKind::CallerLocation, Mutability::Not).unwrap()
25+
ecx.allocate_str_dedup("<redacted>").unwrap()
2926
};
3027
let file = file.map_provenance(CtfeProvenance::as_immutable);
3128
let line = if loc_details.line { Scalar::from_u32(line) } else { Scalar::from_u32(0) };

Diff for: compiler/rustc_middle/src/ty/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1479,7 +1479,7 @@ impl<'tcx> TyCtxt<'tcx> {
14791479
self.mk_adt_def_from_data(ty::AdtDefData::new(self, did, kind, variants, repr))
14801480
}
14811481

1482-
/// Allocates a read-only byte or string literal for `mir::interpret`.
1482+
/// Allocates a read-only byte or string literal for `mir::interpret` with alignment 1.
14831483
/// Returns the same `AllocId` if called again with the same bytes.
14841484
pub fn allocate_bytes_dedup(self, bytes: &[u8], salt: usize) -> interpret::AllocId {
14851485
// Create an allocation that just contains these bytes.

Diff for: src/tools/miri/src/shims/backtrace.rs

+1-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use rustc_abi::{ExternAbi, Size};
2-
use rustc_ast::ast::Mutability;
32
use rustc_middle::ty::layout::LayoutOf as _;
43
use rustc_middle::ty::{self, Instance, Ty};
54
use rustc_span::{BytePos, Loc, Symbol, hygiene};
@@ -179,14 +178,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
179178

180179
match flags {
181180
0 => {
182-
// These are "mutable" allocations as we consider them to be owned by the callee.
183-
let name_alloc =
184-
this.allocate_str(&name, MiriMemoryKind::Rust.into(), Mutability::Mut)?;
185-
let filename_alloc =
186-
this.allocate_str(&filename, MiriMemoryKind::Rust.into(), Mutability::Mut)?;
187-
188-
this.write_immediate(name_alloc.to_ref(this), &this.project_field(dest, 0)?)?;
189-
this.write_immediate(filename_alloc.to_ref(this), &this.project_field(dest, 1)?)?;
181+
throw_unsup_format!("miri_resolve_frame: v0 is not supported any more");
190182
}
191183
1 => {
192184
this.write_scalar(

Diff for: src/tools/miri/src/shims/panic.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
//! metadata we remembered when pushing said frame.
1313
1414
use rustc_abi::ExternAbi;
15-
use rustc_ast::Mutability;
1615
use rustc_middle::{mir, ty};
1716
use rustc_target::spec::PanicStrategy;
1817

@@ -161,7 +160,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
161160
let this = self.eval_context_mut();
162161

163162
// First arg: message.
164-
let msg = this.allocate_str(msg, MiriMemoryKind::Machine.into(), Mutability::Not)?;
163+
let msg = this.allocate_str_dedup(msg)?;
165164

166165
// Call the lang item.
167166
let panic = this.tcx.lang_items().panic_fn().unwrap();
@@ -180,7 +179,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
180179
let this = self.eval_context_mut();
181180

182181
// First arg: message.
183-
let msg = this.allocate_str(msg, MiriMemoryKind::Machine.into(), Mutability::Not)?;
182+
let msg = this.allocate_str_dedup(msg)?;
184183

185184
// Call the lang item.
186185
let panic = this.tcx.lang_items().panic_nounwind().unwrap();

Diff for: src/tools/miri/tests/pass/backtrace/backtrace-api-v0.rs

-69
This file was deleted.

Diff for: src/tools/miri/tests/pass/backtrace/backtrace-api-v0.stderr

-18
This file was deleted.

Diff for: src/tools/miri/tests/pass/backtrace/backtrace-api-v0.stdout

-5
This file was deleted.

0 commit comments

Comments
 (0)