Skip to content

Commit 0182293

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 1bcbb7c + 29b7077 commit 0182293

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

compiler/rustc_const_eval/src/interpret/eval_context.rs

+7
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@ 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;
1012
use rustc_middle::mir::interpret::{ErrorHandled, InvalidMetaKind, ReportedErrorInfo};
13+
use rustc_middle::mir::ConstValue;
1114
use rustc_middle::query::TyCtxtAt;
1215
use rustc_middle::ty::layout::{
1316
self, FnAbiError, FnAbiOfHelpers, FnAbiRequest, LayoutError, LayoutOf, LayoutOfHelpers,
@@ -47,6 +50,9 @@ pub struct InterpCx<'mir, 'tcx, M: Machine<'mir, 'tcx>> {
4750

4851
/// The recursion limit (cached from `tcx.recursion_limit(())`)
4952
pub recursion_limit: Limit,
53+
54+
pub const_cache:
55+
Lock<FxHashMap<(ConstValue<'tcx>, TyAndLayout<'tcx>, Span), OpTy<'tcx, M::Provenance>>>,
5056
}
5157

5258
// The Phantomdata exists to prevent this type from being `Send`. If it were sent across a thread
@@ -440,6 +446,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
440446
param_env,
441447
memory: Memory::new(),
442448
recursion_limit: tcx.recursion_limit(),
449+
const_cache: Lock::new(FxHashMap::default()),
443450
}
444451
}
445452

compiler/rustc_const_eval/src/interpret/operand.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,10 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
748748
})
749749
};
750750
let layout = from_known_layout(self.tcx, self.param_env, layout, || self.layout_of(ty))?;
751+
let span = self.cur_span();
752+
if let Some(op) = self.const_cache.lock().get(&(val_val, layout, span)).cloned() {
753+
return Ok(op);
754+
}
751755
let imm = match val_val {
752756
mir::ConstValue::Indirect { alloc_id, offset } => {
753757
// We rely on mutability being set correctly in that allocation to prevent writes
@@ -764,7 +768,9 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
764768
Immediate::new_slice(self.global_base_pointer(ptr)?.into(), meta, self)
765769
}
766770
};
767-
Ok(OpTy { op: Operand::Immediate(imm), layout })
771+
let res = OpTy { op: Operand::Immediate(imm), layout };
772+
self.const_cache.lock().insert((val_val, layout, span), res.clone());
773+
Ok(res)
768774
}
769775
}
770776

0 commit comments

Comments
 (0)