Skip to content

Commit 8422d83

Browse files
committed
Auto merge of rust-lang#118336 - saethlin:const-to-op-cache, r=<try>
Add a cache for const_val_to_op r? `@ghost`
2 parents 6eb9524 + c57773d commit 8422d83

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

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

+8-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ use std::{fmt, mem};
44
use either::{Either, Left, Right};
55

66
use hir::CRATE_HIR_ID;
7+
use rustc_data_structures::fx::FxHashMap;
8+
use rustc_data_structures::sync::Lock;
79
use rustc_hir::{self as hir, def_id::DefId, definitions::DefPathData};
810
use rustc_index::IndexVec;
911
use rustc_middle::mir;
@@ -20,9 +22,9 @@ use rustc_span::Span;
2022
use rustc_target::abi::{call::FnAbi, Align, HasDataLayout, Size, TargetDataLayout};
2123

2224
use super::{
23-
AllocId, GlobalId, Immediate, InterpErrorInfo, InterpResult, MPlaceTy, Machine, MemPlace,
24-
MemPlaceMeta, Memory, MemoryKind, OpTy, Operand, Place, PlaceTy, Pointer, PointerArithmetic,
25-
Projectable, Provenance, Scalar, StackPopJump,
25+
AllocId, ConstAllocation, GlobalId, Immediate, InterpErrorInfo, InterpResult, MPlaceTy,
26+
Machine, MemPlace, MemPlaceMeta, Memory, MemoryKind, OpTy, Operand, Place, PlaceTy, Pointer,
27+
PointerArithmetic, Projectable, Provenance, Scalar, StackPopJump,
2628
};
2729
use crate::errors;
2830
use crate::util;
@@ -47,6 +49,8 @@ pub struct InterpCx<'mir, 'tcx, M: Machine<'mir, 'tcx>> {
4749

4850
/// The recursion limit (cached from `tcx.recursion_limit(())`)
4951
pub recursion_limit: Limit,
52+
53+
pub const_cache: Lock<FxHashMap<(ConstAllocation<'tcx>, u64, Span), Immediate<M::Provenance>>>,
5054
}
5155

5256
// The Phantomdata exists to prevent this type from being `Send`. If it were sent across a thread
@@ -440,6 +444,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
440444
param_env,
441445
memory: Memory::new(),
442446
recursion_limit: tcx.recursion_limit(),
447+
const_cache: Lock::new(FxHashMap::default()),
443448
}
444449
}
445450

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

+22-6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//! All high-level functions to read from memory work on operands as sources.
33
44
use std::assert_matches::assert_matches;
5+
use std::collections::hash_map::Entry;
56

67
use either::{Either, Left, Right};
78

@@ -13,9 +14,9 @@ use rustc_middle::{mir, ty};
1314
use rustc_target::abi::{self, Abi, HasDataLayout, Size};
1415

1516
use super::{
16-
alloc_range, from_known_layout, mir_assign_valid_types, AllocId, Frame, InterpCx, InterpResult,
17-
MPlaceTy, Machine, MemPlace, MemPlaceMeta, OffsetMode, PlaceTy, Pointer, Projectable,
18-
Provenance, Scalar,
17+
alloc_range, from_known_layout, mir_assign_valid_types, AllocId, ConstAllocation, Frame,
18+
InterpCx, InterpResult, MPlaceTy, Machine, MemPlace, MemPlaceMeta, OffsetMode, PlaceTy,
19+
Pointer, Projectable, Provenance, Scalar,
1920
};
2021

2122
/// An `Immediate` represents a single immediate self-contained Rust value.
@@ -757,14 +758,29 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
757758
}
758759
mir::ConstValue::Scalar(x) => adjust_scalar(x)?.into(),
759760
mir::ConstValue::ZeroSized => Immediate::Uninit,
760-
mir::ConstValue::Slice { data, meta } => {
761+
mir::ConstValue::Slice { data, meta } => self.const_slice_to_op(data, meta)?,
762+
};
763+
Ok(OpTy { op: Operand::Immediate(imm), layout })
764+
}
765+
766+
fn const_slice_to_op(
767+
&self,
768+
data: ConstAllocation<'tcx>,
769+
meta: u64,
770+
) -> InterpResult<'tcx, Immediate<M::Provenance>> {
771+
let span = self.cur_span();
772+
let imm = match self.const_cache.lock().entry((data, meta, span)) {
773+
Entry::Occupied(e) => e.get().clone(),
774+
Entry::Vacant(e) => {
761775
// We rely on mutability being set correctly in `data` to prevent writes
762776
// where none should happen.
763777
let ptr = Pointer::new(self.tcx.reserve_and_set_memory_alloc(data), Size::ZERO);
764-
Immediate::new_slice(self.global_base_pointer(ptr)?.into(), meta, self)
778+
let imm = Immediate::new_slice(self.global_base_pointer(ptr)?.into(), meta, self);
779+
e.insert(imm.clone());
780+
imm
765781
}
766782
};
767-
Ok(OpTy { op: Operand::Immediate(imm), layout })
783+
Ok(imm)
768784
}
769785
}
770786

0 commit comments

Comments
 (0)