Skip to content

Commit

Permalink
Auto merge of rust-lang#118336 - saethlin:const-to-op-cache, r=<try>
Browse files Browse the repository at this point in the history
Add a cache for const_val_to_op

r? `@ghost`
  • Loading branch information
bors committed Nov 27, 2023
2 parents 6eb9524 + c57773d commit 8422d83
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
11 changes: 8 additions & 3 deletions compiler/rustc_const_eval/src/interpret/eval_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use std::{fmt, mem};
use either::{Either, Left, Right};

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

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

/// The recursion limit (cached from `tcx.recursion_limit(())`)
pub recursion_limit: Limit,

pub const_cache: Lock<FxHashMap<(ConstAllocation<'tcx>, u64, Span), Immediate<M::Provenance>>>,
}

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

Expand Down
28 changes: 22 additions & 6 deletions compiler/rustc_const_eval/src/interpret/operand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//! All high-level functions to read from memory work on operands as sources.

use std::assert_matches::assert_matches;
use std::collections::hash_map::Entry;

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

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

use super::{
alloc_range, from_known_layout, mir_assign_valid_types, AllocId, Frame, InterpCx, InterpResult,
MPlaceTy, Machine, MemPlace, MemPlaceMeta, OffsetMode, PlaceTy, Pointer, Projectable,
Provenance, Scalar,
alloc_range, from_known_layout, mir_assign_valid_types, AllocId, ConstAllocation, Frame,
InterpCx, InterpResult, MPlaceTy, Machine, MemPlace, MemPlaceMeta, OffsetMode, PlaceTy,
Pointer, Projectable, Provenance, Scalar,
};

/// An `Immediate` represents a single immediate self-contained Rust value.
Expand Down Expand Up @@ -757,14 +758,29 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
}
mir::ConstValue::Scalar(x) => adjust_scalar(x)?.into(),
mir::ConstValue::ZeroSized => Immediate::Uninit,
mir::ConstValue::Slice { data, meta } => {
mir::ConstValue::Slice { data, meta } => self.const_slice_to_op(data, meta)?,
};
Ok(OpTy { op: Operand::Immediate(imm), layout })
}

fn const_slice_to_op(
&self,
data: ConstAllocation<'tcx>,
meta: u64,
) -> InterpResult<'tcx, Immediate<M::Provenance>> {
let span = self.cur_span();
let imm = match self.const_cache.lock().entry((data, meta, span)) {
Entry::Occupied(e) => e.get().clone(),
Entry::Vacant(e) => {
// We rely on mutability being set correctly in `data` to prevent writes
// where none should happen.
let ptr = Pointer::new(self.tcx.reserve_and_set_memory_alloc(data), Size::ZERO);
Immediate::new_slice(self.global_base_pointer(ptr)?.into(), meta, self)
let imm = Immediate::new_slice(self.global_base_pointer(ptr)?.into(), meta, self);
e.insert(imm.clone());
imm
}
};
Ok(OpTy { op: Operand::Immediate(imm), layout })
Ok(imm)
}
}

Expand Down

0 comments on commit 8422d83

Please sign in to comment.