Skip to content

Commit 382cc90

Browse files
committed
Make the check for cache opt-in.
1 parent facf1e0 commit 382cc90

File tree

4 files changed

+57
-13
lines changed

4 files changed

+57
-13
lines changed

compiler/rustc_middle/src/ty/query.rs

+39-1
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ pub struct TyCtxtEnsure<'tcx> {
9999
pub tcx: TyCtxt<'tcx>,
100100
}
101101

102+
#[derive(Copy, Clone)]
103+
pub struct TyCtxtEnsureWithValue<'tcx> {
104+
pub tcx: TyCtxt<'tcx>,
105+
}
106+
102107
impl<'tcx> TyCtxt<'tcx> {
103108
/// Returns a transparent wrapper for `TyCtxt`, which ensures queries
104109
/// are executed instead of just returning their results.
@@ -107,6 +112,15 @@ impl<'tcx> TyCtxt<'tcx> {
107112
TyCtxtEnsure { tcx: self }
108113
}
109114

115+
/// Returns a transparent wrapper for `TyCtxt`, which ensures queries
116+
/// are executed instead of just returning their results.
117+
///
118+
/// This version verifies that the computed result exists in the cache before returning.
119+
#[inline(always)]
120+
pub fn ensure_with_value(self) -> TyCtxtEnsureWithValue<'tcx> {
121+
TyCtxtEnsureWithValue { tcx: self }
122+
}
123+
110124
/// Returns a transparent wrapper for `TyCtxt` which uses
111125
/// `span` as the location of queries performed through it.
112126
#[inline(always)]
@@ -314,7 +328,31 @@ macro_rules! define_callbacks {
314328

315329
match try_get_cached(self.tcx, &self.tcx.query_system.caches.$name, &key) {
316330
Some(_) => return,
317-
None => self.tcx.queries.$name(self.tcx, DUMMY_SP, key, QueryMode::Ensure),
331+
None => self.tcx.queries.$name(
332+
self.tcx,
333+
DUMMY_SP,
334+
key,
335+
QueryMode::Ensure { check_cache: false },
336+
),
337+
};
338+
})*
339+
}
340+
341+
impl<'tcx> TyCtxtEnsureWithValue<'tcx> {
342+
$($(#[$attr])*
343+
#[inline(always)]
344+
pub fn $name(self, key: query_helper_param_ty!($($K)*)) {
345+
let key = key.into_query_param();
346+
opt_remap_env_constness!([$($modifiers)*][key]);
347+
348+
match try_get_cached(self.tcx, &self.tcx.query_system.caches.$name, &key) {
349+
Some(_) => return,
350+
None => self.tcx.queries.$name(
351+
self.tcx,
352+
DUMMY_SP,
353+
key,
354+
QueryMode::Ensure { check_cache: true },
355+
),
318356
};
319357
})*
320358
}

compiler/rustc_mir_build/src/build/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@ fn mir_build(tcx: TyCtxt<'_>, def: ty::WithOptConstParam<LocalDefId>) -> Body<'_
5050
// Ensure unsafeck and abstract const building is ran before we steal the THIR.
5151
match def {
5252
ty::WithOptConstParam { did, const_param_did: Some(const_param_did) } => {
53-
tcx.ensure().thir_check_unsafety_for_const_arg((did, const_param_did));
54-
tcx.ensure().thir_abstract_const_of_const_arg((did, const_param_did));
53+
tcx.ensure_with_value().thir_check_unsafety_for_const_arg((did, const_param_did));
54+
tcx.ensure_with_value().thir_abstract_const_of_const_arg((did, const_param_did));
5555
}
5656
ty::WithOptConstParam { did, const_param_did: None } => {
57-
tcx.ensure().thir_check_unsafety(did);
58-
tcx.ensure().thir_abstract_const(did);
57+
tcx.ensure_with_value().thir_check_unsafety(did);
58+
tcx.ensure_with_value().thir_abstract_const(did);
5959
}
6060
}
6161

compiler/rustc_mir_transform/src/lib.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -278,14 +278,14 @@ fn mir_const(tcx: TyCtxt<'_>, def: ty::WithOptConstParam<LocalDefId>) -> &Steal<
278278
// Unsafety check uses the raw mir, so make sure it is run.
279279
if !tcx.sess.opts.unstable_opts.thir_unsafeck {
280280
if let Some(param_did) = def.const_param_did {
281-
tcx.ensure().unsafety_check_result_for_const_arg((def.did, param_did));
281+
tcx.ensure_with_value().unsafety_check_result_for_const_arg((def.did, param_did));
282282
} else {
283-
tcx.ensure().unsafety_check_result(def.did);
283+
tcx.ensure_with_value().unsafety_check_result(def.did);
284284
}
285285
}
286286

287287
// has_ffi_unwind_calls query uses the raw mir, so make sure it is run.
288-
tcx.ensure().has_ffi_unwind_calls(def.did);
288+
tcx.ensure_with_value().has_ffi_unwind_calls(def.did);
289289

290290
let mut body = tcx.mir_built(def).steal();
291291

@@ -433,7 +433,7 @@ fn mir_drops_elaborated_and_const_checked(
433433
if tcx.sess.opts.unstable_opts.drop_tracking_mir
434434
&& let DefKind::Generator = tcx.def_kind(def.did)
435435
{
436-
tcx.ensure().mir_generator_witnesses(def.did);
436+
tcx.ensure_with_value().mir_generator_witnesses(def.did);
437437
}
438438
let mir_borrowck = tcx.mir_borrowck_opt_const_arg(def);
439439

@@ -613,7 +613,7 @@ fn inner_optimized_mir(tcx: TyCtxt<'_>, did: LocalDefId) -> Body<'_> {
613613
// Run the `mir_for_ctfe` query, which depends on `mir_drops_elaborated_and_const_checked`
614614
// which we are going to steal below. Thus we need to run `mir_for_ctfe` first, so it
615615
// computes and caches its result.
616-
Some(hir::ConstContext::ConstFn) => tcx.ensure().mir_for_ctfe(did),
616+
Some(hir::ConstContext::ConstFn) => tcx.ensure_with_value().mir_for_ctfe(did),
617617
None => {}
618618
Some(other) => panic!("do not use `optimized_mir` for constants: {:?}", other),
619619
}

compiler/rustc_query_system/src/query/plumbing.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,7 @@ fn ensure_must_run<Q, Qcx>(
711711
query: Q,
712712
qcx: Qcx,
713713
key: &Q::Key,
714+
check_cache: bool,
714715
) -> (bool, Option<DepNode<Qcx::DepKind>>)
715716
where
716717
Q: QueryConfig<Qcx>,
@@ -743,14 +744,19 @@ where
743744
}
744745
};
745746

747+
// We do not need the value at all, so do not check the cache.
748+
if !check_cache {
749+
return (false, None);
750+
}
751+
746752
let loadable = query.loadable_from_disk(qcx, key, serialized_dep_node_index);
747753
(!loadable, Some(dep_node))
748754
}
749755

750756
#[derive(Debug)]
751757
pub enum QueryMode {
752758
Get,
753-
Ensure,
759+
Ensure { check_cache: bool },
754760
}
755761

756762
#[inline(always)]
@@ -765,8 +771,8 @@ where
765771
Q: QueryConfig<Qcx>,
766772
Qcx: QueryContext,
767773
{
768-
let dep_node = if let QueryMode::Ensure = mode {
769-
let (must_run, dep_node) = ensure_must_run(query, qcx, &key);
774+
let dep_node = if let QueryMode::Ensure { check_cache } = mode {
775+
let (must_run, dep_node) = ensure_must_run(query, qcx, &key, check_cache);
770776
if !must_run {
771777
return None;
772778
}

0 commit comments

Comments
 (0)