Skip to content

Commit 11d9959

Browse files
committed
Auto merge of #48864 - oli-obk:miri_incremental_regression, r=eddyb
Cache const eval queries fixes #48846 (I think, still running more perf tests, but tuple-stress stops recomputing any constants) r? @michaelwoerister
2 parents d089fe9 + 0d88db1 commit 11d9959

File tree

4 files changed

+38
-3
lines changed

4 files changed

+38
-3
lines changed

src/librustc/ty/maps/config.rs

+12
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,18 @@ impl<'tcx> QueryDescription<'tcx> for queries::const_eval<'tcx> {
178178
fn describe(tcx: TyCtxt, key: ty::ParamEnvAnd<'tcx, GlobalId<'tcx>>) -> String {
179179
format!("const-evaluating `{}`", tcx.item_path_str(key.value.instance.def.def_id()))
180180
}
181+
182+
#[inline]
183+
fn cache_on_disk(_key: Self::Key) -> bool {
184+
true
185+
}
186+
187+
#[inline]
188+
fn try_load_from_disk<'a>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
189+
id: SerializedDepNodeIndex)
190+
-> Option<Self::Value> {
191+
tcx.on_disk_query_result_cache.try_load_query_result(tcx, id).map(Ok)
192+
}
181193
}
182194

183195
impl<'tcx> QueryDescription<'tcx> for queries::mir_keys<'tcx> {

src/librustc/ty/maps/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ pub use self::on_disk_cache::OnDiskCache;
8686
// the driver creates (using several `rustc_*` crates).
8787
//
8888
// The result of query must implement Clone. They must also implement ty::maps::values::Value
89-
// which produces an appropiate error value if the query resulted in a query cycle.
89+
// which produces an appropriate error value if the query resulted in a query cycle.
9090
// Queries marked with `fatal_cycle` do not need that implementation
9191
// as they will raise an fatal error on query cycles instead.
9292
define_maps! { <'tcx>

src/librustc/ty/maps/on_disk_cache.rs

+19
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,24 @@ impl<'sess> OnDiskCache<'sess> {
221221
encode_query_results::<symbol_name, _>(tcx, enc, qri)?;
222222
encode_query_results::<check_match, _>(tcx, enc, qri)?;
223223
encode_query_results::<trans_fn_attrs, _>(tcx, enc, qri)?;
224+
225+
// const eval is special, it only encodes successfully evaluated constants
226+
use ty::maps::plumbing::GetCacheInternal;
227+
for (key, entry) in const_eval::get_cache_internal(tcx).map.iter() {
228+
use ty::maps::config::QueryDescription;
229+
if const_eval::cache_on_disk(key.clone()) {
230+
if let Ok(ref value) = entry.value {
231+
let dep_node = SerializedDepNodeIndex::new(entry.index.index());
232+
233+
// Record position of the cache entry
234+
qri.push((dep_node, AbsoluteBytePos::new(enc.position())));
235+
236+
// Encode the type check tables with the SerializedDepNodeIndex
237+
// as tag.
238+
enc.encode_tagged(dep_node, value)?;
239+
}
240+
}
241+
}
224242
}
225243

226244
// Encode diagnostics
@@ -563,6 +581,7 @@ impl<'a, 'tcx, 'x> SpecializedDecoder<interpret::AllocId> for CacheDecoder<'a, '
563581
tcx.interpret_interner.intern_at_reserved(alloc_id, allocation);
564582

565583
if let Some(glob) = Option::<DefId>::decode(self)? {
584+
trace!("connecting alloc {:?} with {:?}", alloc_id, glob);
566585
tcx.interpret_interner.cache(glob, alloc_id);
567586
}
568587

src/librustc_mir/interpret/const_eval.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ fn eval_body_and_ecx<'a, 'mir, 'tcx>(
110110
span = mir.span;
111111
let layout = ecx.layout_of(mir.return_ty().subst(tcx, cid.instance.substs))?;
112112
let alloc = tcx.interpret_interner.get_cached(cid.instance.def_id());
113+
let is_static = tcx.is_static(cid.instance.def_id()).is_some();
113114
let alloc = match alloc {
114115
Some(alloc) => {
115116
assert!(cid.promoted.is_none());
@@ -123,7 +124,7 @@ fn eval_body_and_ecx<'a, 'mir, 'tcx>(
123124
layout.align,
124125
None,
125126
)?;
126-
if tcx.is_static(cid.instance.def_id()).is_some() {
127+
if is_static {
127128
tcx.interpret_interner.cache(cid.instance.def_id(), ptr.alloc_id);
128129
}
129130
let internally_mutable = !layout.ty.is_freeze(tcx, param_env, mir.span);
@@ -151,8 +152,11 @@ fn eval_body_and_ecx<'a, 'mir, 'tcx>(
151152
}
152153
};
153154
let ptr = MemoryPointer::new(alloc, 0).into();
155+
// always try to read the value and report errors
154156
let value = match ecx.try_read_value(ptr, layout.align, layout.ty)? {
155-
Some(val) => val,
157+
// if it's a constant (so it needs no address, directly compute its value)
158+
Some(val) if !is_static => val,
159+
// point at the allocation
156160
_ => Value::ByRef(ptr, layout.align),
157161
};
158162
Ok((value, ptr, layout.ty))

0 commit comments

Comments
 (0)