Skip to content

Commit 0d88db1

Browse files
committed
Reuse the query caching infrastructure for const eval
1 parent af099bb commit 0d88db1

File tree

3 files changed

+16
-61
lines changed

3 files changed

+16
-61
lines changed

src/librustc/ty/maps/config.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,15 +158,15 @@ impl<'tcx> QueryDescription<'tcx> for queries::const_eval<'tcx> {
158158
}
159159

160160
#[inline]
161-
fn cache_on_disk(key: Self::Key) -> bool {
162-
key.value.instance.def_id().is_local()
161+
fn cache_on_disk(_key: Self::Key) -> bool {
162+
true
163163
}
164164

165165
#[inline]
166166
fn try_load_from_disk<'a>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
167167
id: SerializedDepNodeIndex)
168168
-> Option<Self::Value> {
169-
tcx.on_disk_query_result_cache.load_constant(tcx, id).map(Ok)
169+
tcx.on_disk_query_result_cache.try_load_query_result(tcx, id).map(Ok)
170170
}
171171
}
172172

src/librustc/ty/maps/on_disk_cache.rs

Lines changed: 7 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,6 @@ pub struct OnDiskCache<'sess> {
7575
// A map from dep-node to the position of any associated diagnostics in
7676
// `serialized_data`.
7777
prev_diagnostics_index: FxHashMap<SerializedDepNodeIndex, AbsoluteBytePos>,
78-
79-
// A map from dep-node to the position of any associated constants in
80-
// `serialized_data`.
81-
prev_constants_index: FxHashMap<SerializedDepNodeIndex, AbsoluteBytePos>,
8278
}
8379

8480
// This type is used only for (de-)serialization.
@@ -88,10 +84,8 @@ struct Footer {
8884
prev_cnums: Vec<(u32, String, CrateDisambiguator)>,
8985
query_result_index: EncodedQueryResultIndex,
9086
diagnostics_index: EncodedQueryResultIndex,
91-
constants_index: EncodedConstantsIndex,
9287
}
9388

94-
type EncodedConstantsIndex = Vec<(SerializedDepNodeIndex, AbsoluteBytePos)>;
9589
type EncodedQueryResultIndex = Vec<(SerializedDepNodeIndex, AbsoluteBytePos)>;
9690
type EncodedDiagnosticsIndex = Vec<(SerializedDepNodeIndex, AbsoluteBytePos)>;
9791
type EncodedDiagnostics = Vec<Diagnostic>;
@@ -145,7 +139,6 @@ impl<'sess> OnDiskCache<'sess> {
145139
current_diagnostics: RefCell::new(FxHashMap()),
146140
query_result_index: footer.query_result_index.into_iter().collect(),
147141
prev_diagnostics_index: footer.diagnostics_index.into_iter().collect(),
148-
prev_constants_index: footer.constants_index.into_iter().collect(),
149142
synthetic_expansion_infos: RefCell::new(FxHashMap()),
150143
}
151144
}
@@ -161,7 +154,6 @@ impl<'sess> OnDiskCache<'sess> {
161154
current_diagnostics: RefCell::new(FxHashMap()),
162155
query_result_index: FxHashMap(),
163156
prev_diagnostics_index: FxHashMap(),
164-
prev_constants_index: FxHashMap(),
165157
synthetic_expansion_infos: RefCell::new(FxHashMap()),
166158
}
167159
}
@@ -229,46 +221,25 @@ impl<'sess> OnDiskCache<'sess> {
229221
encode_query_results::<symbol_name, _>(tcx, enc, qri)?;
230222
encode_query_results::<check_match, _>(tcx, enc, qri)?;
231223
encode_query_results::<trans_fn_attrs, _>(tcx, enc, qri)?;
232-
}
233224

234-
// encode successful constant evaluations
235-
let constants_index = {
236-
let mut constants_index = EncodedConstantsIndex::new();
237-
use ty::maps::queries::const_eval;
225+
// const eval is special, it only encodes successfully evaluated constants
238226
use ty::maps::plumbing::GetCacheInternal;
239-
use ty::maps::config::QueryDescription;
240227
for (key, entry) in const_eval::get_cache_internal(tcx).map.iter() {
241-
if let Ok(ref constant) = entry.value {
242-
if const_eval::cache_on_disk(key.clone()) {
243-
trace!("caching constant {:?} with value {:#?}", key, constant);
228+
use ty::maps::config::QueryDescription;
229+
if const_eval::cache_on_disk(key.clone()) {
230+
if let Ok(ref value) = entry.value {
244231
let dep_node = SerializedDepNodeIndex::new(entry.index.index());
245232

246233
// Record position of the cache entry
247-
constants_index.push((
248-
dep_node,
249-
AbsoluteBytePos::new(encoder.position()),
250-
));
251-
let did = key.value.instance.def_id();
252-
let constant = if key.value.promoted.is_none()
253-
&& tcx.is_static(did).is_some() {
254-
// memorize the allocation for the static, too, so
255-
// we can refer to the static, not just read its value
256-
// since we have had a successful query, the cached value must
257-
// exist, so we can unwrap it
258-
let cached = tcx.interpret_interner.get_cached(did).unwrap();
259-
(constant, Some(cached))
260-
} else {
261-
(constant, None)
262-
};
234+
qri.push((dep_node, AbsoluteBytePos::new(enc.position())));
263235

264236
// Encode the type check tables with the SerializedDepNodeIndex
265237
// as tag.
266-
encoder.encode_tagged(dep_node, &constant)?;
238+
enc.encode_tagged(dep_node, value)?;
267239
}
268240
}
269241
}
270-
constants_index
271-
};
242+
}
272243

273244
// Encode diagnostics
274245
let diagnostics_index = {
@@ -303,7 +274,6 @@ impl<'sess> OnDiskCache<'sess> {
303274
prev_cnums,
304275
query_result_index,
305276
diagnostics_index,
306-
constants_index,
307277
})?;
308278

309279
// Encode the position of the footer as the last 8 bytes of the
@@ -326,25 +296,6 @@ impl<'sess> OnDiskCache<'sess> {
326296
})
327297
}
328298

329-
/// Load a constant emitted during the previous compilation session.
330-
pub fn load_constant<'a, 'tcx>(&self,
331-
tcx: TyCtxt<'a, 'tcx, 'tcx>,
332-
dep_node_index: SerializedDepNodeIndex)
333-
-> Option<&'tcx ty::Const<'tcx>> {
334-
type Encoded<'tcx> = (ty::Const<'tcx>, Option<interpret::AllocId>);
335-
let constant: Option<Encoded<'tcx>> = self.load_indexed(
336-
tcx,
337-
dep_node_index,
338-
&self.prev_constants_index,
339-
"constants");
340-
341-
constant.map(|(c, _alloc_id)| {
342-
// the AllocId decoding already registers the AllocId to its DefId
343-
// so we don't need to take additional actions here
344-
tcx.mk_const(c)
345-
})
346-
}
347-
348299
/// Load a diagnostic emitted during the previous compilation session.
349300
pub fn load_diagnostics<'a, 'tcx>(&self,
350301
tcx: TyCtxt<'a, 'tcx, 'tcx>,

src/librustc_mir/interpret/const_eval.rs

Lines changed: 6 additions & 2 deletions
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)