Skip to content

Commit

Permalink
Rub come caching on caller_location
Browse files Browse the repository at this point in the history
  • Loading branch information
saethlin committed Sep 20, 2024
1 parent 98e524c commit b046470
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
11 changes: 10 additions & 1 deletion compiler/rustc_const_eval/src/util/caller_location.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::hash_map::Entry;

use rustc_hir::LangItem;
use rustc_middle::query::TyCtxtAt;
use rustc_middle::ty::layout::LayoutOf;
Expand Down Expand Up @@ -57,6 +59,11 @@ pub(crate) fn const_caller_location_provider(
col: u32,
) -> mir::ConstValue<'_> {
trace!("const_caller_location: {}:{}:{}", file, line, col);
let mut cache = tcx.caller_location_cache.lock();
let entry = match cache.entry((file, line, col)) {
Entry::Occupied(oe) => return *oe.get(),
Entry::Vacant(ve) => ve,
};
let mut ecx = mk_eval_cx_to_read_const_val(
tcx.tcx,
tcx.span,
Expand All @@ -68,5 +75,7 @@ pub(crate) fn const_caller_location_provider(
if intern_const_alloc_recursive(&mut ecx, InternKind::Constant, &loc_place).is_err() {
bug!("intern_const_alloc_recursive should not error in this case")
}
mir::ConstValue::Scalar(Scalar::from_maybe_pointer(loc_place.ptr(), &tcx))
let val = mir::ConstValue::Scalar(Scalar::from_maybe_pointer(loc_place.ptr(), &tcx));
entry.insert(val);
val
}
5 changes: 4 additions & 1 deletion compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ use crate::metadata::ModChild;
use crate::middle::codegen_fn_attrs::CodegenFnAttrs;
use crate::middle::{resolve_bound_vars, stability};
use crate::mir::interpret::{self, Allocation, ConstAllocation};
use crate::mir::{Body, Local, Place, PlaceElem, ProjectionKind, Promoted};
use crate::mir::{Body, ConstValue, Local, Place, PlaceElem, ProjectionKind, Promoted};
use crate::query::plumbing::QuerySystem;
use crate::query::{IntoQueryParam, LocalCrate, Providers, TyCtxtAt};
use crate::thir::Thir;
Expand Down Expand Up @@ -1297,6 +1297,8 @@ pub struct GlobalCtxt<'tcx> {
/// Stores memory for globals (statics/consts).
pub(crate) alloc_map: Lock<interpret::AllocMap<'tcx>>,

pub caller_location_cache: Lock<FxHashMap<(Symbol, u32, u32), ConstValue<'tcx>>>,

current_gcx: CurrentGcx,
}

Expand Down Expand Up @@ -1527,6 +1529,7 @@ impl<'tcx> TyCtxt<'tcx> {
canonical_param_env_cache: Default::default(),
data_layout,
alloc_map: Lock::new(interpret::AllocMap::new()),
caller_location_cache: Default::default(),
current_gcx,
}
}
Expand Down

0 comments on commit b046470

Please sign in to comment.