diff options
| author | Camille GILLOT <gillot.camille@gmail.com> | 2023-10-07 09:19:37 +0000 |
|---|---|---|
| committer | Camille GILLOT <gillot.camille@gmail.com> | 2023-10-25 06:46:48 +0000 |
| commit | ebc87bf5672fd7f7a899599d2ee5a219e1d07d07 (patch) | |
| tree | d5b3581b3eba257764a95f2a8e0193bd54494c5f /compiler/rustc_const_eval | |
| parent | 8162dc2433dac09966ae23bb4422d966a7aeab53 (diff) | |
| download | rust-ebc87bf5672fd7f7a899599d2ee5a219e1d07d07.tar.gz rust-ebc87bf5672fd7f7a899599d2ee5a219e1d07d07.zip | |
Directly intern values instead of copying them.
Diffstat (limited to 'compiler/rustc_const_eval')
| -rw-r--r-- | compiler/rustc_const_eval/src/interpret/intern.rs | 44 | ||||
| -rw-r--r-- | compiler/rustc_const_eval/src/interpret/mod.rs | 4 |
2 files changed, 47 insertions, 1 deletions
diff --git a/compiler/rustc_const_eval/src/interpret/intern.rs b/compiler/rustc_const_eval/src/interpret/intern.rs index fd89e34204f..4cac00c1be2 100644 --- a/compiler/rustc_const_eval/src/interpret/intern.rs +++ b/compiler/rustc_const_eval/src/interpret/intern.rs @@ -450,6 +450,50 @@ pub fn intern_const_alloc_recursive< Ok(()) } +/// Intern `ret`, checking it references no other allocation. +#[instrument(level = "debug", skip(ecx))] +pub fn intern_const_alloc_for_constprop< + 'mir, + 'tcx: 'mir, + T, + M: CompileTimeMachine<'mir, 'tcx, T>, +>( + ecx: &mut InterpCx<'mir, 'tcx, M>, + ret: &MPlaceTy<'tcx>, +) -> InterpResult<'tcx, ()> { + let Some((size, _align)) = ecx.size_and_align_of_mplace(ret)? else { + throw_inval!(ConstPropNonsense) + }; + + let alloc_ref = ecx.get_ptr_alloc(ret.ptr(), size)?.unwrap(); + // Do not try interning a value that contains provenance. + if alloc_ref.has_provenance() { + throw_inval!(ConstPropNonsense) + } + + // remove allocation + let alloc_id = ret.ptr().provenance.unwrap(); + let Some((_, mut alloc)) = ecx.memory.alloc_map.remove(&alloc_id) else { + // Pointer not found in local memory map. It is either a pointer to the global + // map, or dangling. + if ecx.tcx.try_get_global_alloc(alloc_id).is_none() { + throw_ub!(DeadLocal) + } + // The constant is already in global memory. Do nothing. + return Ok(()); + }; + + alloc.mutability = Mutability::Not; + + // link the alloc id to the actual allocation + assert!(alloc.provenance().ptrs().is_empty()); + + let alloc = ecx.tcx.mk_const_alloc(alloc); + ecx.tcx.set_alloc_id_memory(alloc_id, alloc); + + Ok(()) +} + impl<'mir, 'tcx: 'mir, M: super::intern::CompileTimeMachine<'mir, 'tcx, !>> InterpCx<'mir, 'tcx, M> { diff --git a/compiler/rustc_const_eval/src/interpret/mod.rs b/compiler/rustc_const_eval/src/interpret/mod.rs index 13664456987..7d286d103ad 100644 --- a/compiler/rustc_const_eval/src/interpret/mod.rs +++ b/compiler/rustc_const_eval/src/interpret/mod.rs @@ -21,7 +21,9 @@ mod visitor; pub use rustc_middle::mir::interpret::*; // have all the `interpret` symbols in one place: here pub use self::eval_context::{Frame, FrameInfo, InterpCx, StackPopCleanup}; -pub use self::intern::{intern_const_alloc_recursive, InternKind}; +pub use self::intern::{ + intern_const_alloc_for_constprop, intern_const_alloc_recursive, InternKind, +}; pub use self::machine::{compile_time_machine, AllocMap, Machine, MayLeak, StackPopJump}; pub use self::memory::{AllocKind, AllocRef, AllocRefMut, FnVal, Memory, MemoryKind}; pub use self::operand::{ImmTy, Immediate, OpTy, Readable}; |
