diff --git a/compiler/rustc_const_eval/src/util/caller_location.rs b/compiler/rustc_const_eval/src/util/caller_location.rs index eb185bee5c07b..630ee2e0d4542 100644 --- a/compiler/rustc_const_eval/src/util/caller_location.rs +++ b/compiler/rustc_const_eval/src/util/caller_location.rs @@ -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; @@ -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, @@ -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 } diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index cd06d7b8e533e..65b7ccb05c065 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -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; @@ -1297,6 +1297,8 @@ pub struct GlobalCtxt<'tcx> { /// Stores memory for globals (statics/consts). pub(crate) alloc_map: Lock>, + pub caller_location_cache: Lock>>, + current_gcx: CurrentGcx, } @@ -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, } }