Skip to content

Commit c385f72

Browse files
committed
Auto merge of #62166 - Zoxc:const_eval_raw, r=<try>
Store const_eval_raw results to disk Based on #59722. r? @oli-obk
2 parents a6b5d22 + 6a5afdd commit c385f72

File tree

8 files changed

+132
-184
lines changed

8 files changed

+132
-184
lines changed

src/librustc/dep_graph/graph.rs

+13-24
Original file line numberDiff line numberDiff line change
@@ -842,31 +842,20 @@ impl DepGraph {
842842
// This method will only load queries that will end up in the disk cache.
843843
// Other queries will not be executed.
844844
pub fn exec_cache_promotions<'tcx>(&self, tcx: TyCtxt<'tcx>) {
845-
let green_nodes: Vec<DepNode> = {
846-
let data = self.data.as_ref().unwrap();
847-
data.colors.values.indices().filter_map(|prev_index| {
848-
match data.colors.get(prev_index) {
849-
Some(DepNodeColor::Green(_)) => {
850-
let dep_node = data.previous.index_to_node(prev_index);
851-
if dep_node.cache_on_disk(tcx) {
852-
Some(dep_node)
853-
} else {
854-
None
855-
}
856-
}
857-
None |
858-
Some(DepNodeColor::Red) => {
859-
// We can skip red nodes because a node can only be marked
860-
// as red if the query result was recomputed and thus is
861-
// already in memory.
862-
None
863-
}
845+
let data = self.data.as_ref().unwrap();
846+
for prev_index in data.colors.values.indices() {
847+
match data.colors.get(prev_index) {
848+
Some(DepNodeColor::Green(_)) => {
849+
let dep_node = data.previous.index_to_node(prev_index);
850+
dep_node.try_load_from_on_disk_cache(tcx);
864851
}
865-
}).collect()
866-
};
867-
868-
for dep_node in green_nodes {
869-
dep_node.load_from_on_disk_cache(tcx);
852+
None |
853+
Some(DepNodeColor::Red) => {
854+
// We can skip red nodes because a node can only be marked
855+
// as red if the query result was recomputed and thus is
856+
// already in memory.
857+
}
858+
}
870859
}
871860
}
872861

src/librustc/mir/interpret/error.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use errors::DiagnosticBuilder;
1818
use syntax_pos::{Pos, Span};
1919
use syntax::symbol::Symbol;
2020

21-
#[derive(Debug, Copy, Clone, PartialEq, Eq, HashStable)]
21+
#[derive(Debug, Copy, Clone, PartialEq, Eq, HashStable, RustcEncodable, RustcDecodable)]
2222
pub enum ErrorHandled {
2323
/// Already reported a lint or an error for this evaluation.
2424
Reported,

src/librustc/query/mod.rs

+39-23
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::ty::query::QueryDescription;
22
use crate::ty::query::queries;
33
use crate::ty::{self, ParamEnvAnd, Ty, TyCtxt};
44
use crate::ty::subst::SubstsRef;
5-
use crate::dep_graph::SerializedDepNodeIndex;
5+
use crate::dep_graph::{RecoverKey,DepKind, DepNode, SerializedDepNodeIndex};
66
use crate::hir::def_id::{CrateNum, DefId, DefIndex};
77
use crate::mir;
88
use crate::mir::interpret::GlobalId;
@@ -33,13 +33,13 @@ rustc_queries! {
3333
Other {
3434
/// Records the type of every item.
3535
query type_of(key: DefId) -> Ty<'tcx> {
36-
cache { key.is_local() }
36+
cache_on_disk_if { key.is_local() }
3737
}
3838

3939
/// Maps from the `DefId` of an item (trait/struct/enum/fn) to its
4040
/// associated generics.
4141
query generics_of(key: DefId) -> &'tcx ty::Generics {
42-
cache { key.is_local() }
42+
cache_on_disk_if { key.is_local() }
4343
load_cached(tcx, id) {
4444
let generics: Option<ty::Generics> = tcx.queries.on_disk_cache
4545
.try_load_query_result(tcx, id);
@@ -62,7 +62,9 @@ rustc_queries! {
6262
/// predicate gets in the way of some checks, which are intended
6363
/// to operate over only the actual where-clauses written by the
6464
/// user.)
65-
query predicates_of(_: DefId) -> &'tcx ty::GenericPredicates<'tcx> {}
65+
query predicates_of(key: DefId) -> &'tcx ty::GenericPredicates<'tcx> {
66+
cache_on_disk_if { key.is_local() }
67+
}
6668

6769
query native_libraries(_: CrateNum) -> Lrc<Vec<NativeLibrary>> {
6870
desc { "looking up the native libraries of a linked crate" }
@@ -93,7 +95,7 @@ rustc_queries! {
9395
/// of the MIR qualify_consts pass. The actual meaning of
9496
/// the value isn't known except to the pass itself.
9597
query mir_const_qualif(key: DefId) -> (u8, &'tcx BitSet<mir::Local>) {
96-
cache { key.is_local() }
98+
cache_on_disk_if { key.is_local() }
9799
}
98100

99101
/// Fetch the MIR for a given `DefId` right after it's built - this includes
@@ -115,7 +117,7 @@ rustc_queries! {
115117
/// MIR after our optimization passes have run. This is MIR that is ready
116118
/// for codegen. This is also the only query that can fetch non-local MIR, at present.
117119
query optimized_mir(key: DefId) -> &'tcx mir::Body<'tcx> {
118-
cache { key.is_local() }
120+
cache_on_disk_if { key.is_local() }
119121
load_cached(tcx, id) {
120122
let mir: Option<crate::mir::Body<'tcx>> = tcx.queries.on_disk_cache
121123
.try_load_query_result(tcx, id);
@@ -285,7 +287,9 @@ rustc_queries! {
285287

286288
TypeChecking {
287289
/// The result of unsafety-checking this `DefId`.
288-
query unsafety_check_result(_: DefId) -> mir::UnsafetyCheckResult {}
290+
query unsafety_check_result(key: DefId) -> mir::UnsafetyCheckResult {
291+
cache_on_disk_if { key.is_local() }
292+
}
289293

290294
/// HACK: when evaluated, this reports a "unsafe derive on repr(packed)" error
291295
query unsafe_derive_on_repr_packed(_: DefId) -> () {}
@@ -348,7 +352,7 @@ rustc_queries! {
348352
}
349353

350354
query typeck_tables_of(key: DefId) -> &'tcx ty::TypeckTables<'tcx> {
351-
cache { key.is_local() }
355+
cache_on_disk_if { key.is_local() }
352356
load_cached(tcx, id) {
353357
let typeck_tables: Option<ty::TypeckTables<'tcx>> = tcx
354358
.queries.on_disk_cache
@@ -360,7 +364,9 @@ rustc_queries! {
360364
}
361365

362366
Other {
363-
query used_trait_imports(_: DefId) -> &'tcx DefIdSet {}
367+
query used_trait_imports(key: DefId) -> &'tcx DefIdSet {
368+
cache_on_disk_if { key.is_local() }
369+
}
364370
}
365371

366372
TypeChecking {
@@ -372,11 +378,15 @@ rustc_queries! {
372378
}
373379

374380
BorrowChecking {
375-
query borrowck(_: DefId) -> &'tcx BorrowCheckResult {}
381+
query borrowck(key: DefId) -> &'tcx BorrowCheckResult {
382+
cache_on_disk_if { key.is_local() }
383+
}
376384

377385
/// Borrow-checks the function body. If this is a closure, returns
378386
/// additional requirements that the closure's creator must verify.
379-
query mir_borrowck(_: DefId) -> mir::BorrowCheckResult<'tcx> {}
387+
query mir_borrowck(key: DefId) -> mir::BorrowCheckResult<'tcx> {
388+
cache_on_disk_if(tcx, _) { key.is_local() && tcx.is_closure(key) }
389+
}
380390
}
381391

382392
TypeChecking {
@@ -412,9 +422,9 @@ rustc_queries! {
412422
"const-evaluating `{}`",
413423
tcx.def_path_str(key.value.instance.def.def_id())
414424
}
415-
cache { true }
416-
load_cached(tcx, id) {
417-
tcx.queries.on_disk_cache.try_load_query_result(tcx, id).map(Ok)
425+
cache_on_disk_if(_, opt_result) {
426+
// Only store results without errors
427+
opt_result.map_or(true, |r| r.is_ok())
418428
}
419429
}
420430

@@ -427,9 +437,9 @@ rustc_queries! {
427437
"const-evaluating + checking `{}`",
428438
tcx.def_path_str(key.value.instance.def.def_id())
429439
}
430-
cache { true }
431-
load_cached(tcx, id) {
432-
tcx.queries.on_disk_cache.try_load_query_result(tcx, id).map(Ok)
440+
cache_on_disk_if(_, opt_result) {
441+
// Only store results without errors
442+
opt_result.map_or(true, |r| r.is_ok())
433443
}
434444
}
435445

@@ -453,7 +463,9 @@ rustc_queries! {
453463
}
454464

455465
TypeChecking {
456-
query check_match(_: DefId) -> () {}
466+
query check_match(key: DefId) -> () {
467+
cache_on_disk_if { key.is_local() }
468+
}
457469

458470
/// Performs part of the privacy check and computes "access levels".
459471
query privacy_access_levels(_: CrateNum) -> &'tcx AccessLevels {
@@ -483,7 +495,7 @@ rustc_queries! {
483495
query symbol_name(key: ty::Instance<'tcx>) -> ty::SymbolName {
484496
no_force
485497
desc { "computing the symbol for `{}`", key }
486-
cache { true }
498+
cache_on_disk_if { true }
487499
}
488500

489501
query def_kind(_: DefId) -> Option<DefKind> {}
@@ -501,7 +513,9 @@ rustc_queries! {
501513
}
502514

503515
Codegen {
504-
query codegen_fn_attrs(_: DefId) -> CodegenFnAttrs {}
516+
query codegen_fn_attrs(_: DefId) -> CodegenFnAttrs {
517+
cache_on_disk_if { true }
518+
}
505519
}
506520

507521
Other {
@@ -519,7 +533,7 @@ rustc_queries! {
519533
"const checking if rvalue is promotable to static `{}`",
520534
tcx.def_path_str(key)
521535
}
522-
cache { true }
536+
cache_on_disk_if { true }
523537
}
524538
query rvalue_promotable_map(key: DefId) -> &'tcx ItemLocalSet {
525539
desc { |tcx|
@@ -548,7 +562,7 @@ rustc_queries! {
548562
key: (ty::ParamEnv<'tcx>, ty::PolyTraitRef<'tcx>)
549563
) -> Vtable<'tcx, ()> {
550564
no_force
551-
cache { true }
565+
cache_on_disk_if { true }
552566
desc { |tcx|
553567
"checking if `{}` fulfills its obligations",
554568
tcx.def_path_str(key.1.def_id())
@@ -560,7 +574,9 @@ rustc_queries! {
560574
query trait_impls_of(key: DefId) -> &'tcx ty::trait_def::TraitImpls {
561575
desc { |tcx| "trait impls of `{}`", tcx.def_path_str(key) }
562576
}
563-
query specialization_graph_of(_: DefId) -> &'tcx specialization_graph::Graph {}
577+
query specialization_graph_of(_: DefId) -> &'tcx specialization_graph::Graph {
578+
cache_on_disk_if { true }
579+
}
564580
query is_object_safe(key: DefId) -> bool {
565581
desc { |tcx| "determine object safety of trait `{}`", tcx.def_path_str(key) }
566582
}

src/librustc/ty/query/config.rs

+1-31
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ pub(crate) trait QueryDescription<'tcx>: QueryAccessors<'tcx> {
5050
fn describe(tcx: TyCtxt<'_>, key: Self::Key) -> Cow<'static, str>;
5151

5252
#[inline]
53-
fn cache_on_disk(_: TyCtxt<'tcx>, _: Self::Key) -> bool {
53+
fn cache_on_disk(_: TyCtxt<'tcx>, _: Self::Key, _: Option<&Self::Value>) -> bool {
5454
false
5555
}
5656

@@ -75,33 +75,3 @@ impl<'tcx> QueryDescription<'tcx> for queries::analysis<'tcx> {
7575
"running analysis passes on this crate".into()
7676
}
7777
}
78-
79-
macro_rules! impl_disk_cacheable_query(
80-
($query_name:ident, |$tcx:tt, $key:tt| $cond:expr) => {
81-
impl<'tcx> QueryDescription<'tcx> for queries::$query_name<'tcx> {
82-
#[inline]
83-
fn cache_on_disk($tcx: TyCtxt<'tcx>, $key: Self::Key) -> bool {
84-
$cond
85-
}
86-
87-
#[inline]
88-
fn try_load_from_disk(tcx: TyCtxt<'tcx>,
89-
id: SerializedDepNodeIndex)
90-
-> Option<Self::Value> {
91-
tcx.queries.on_disk_cache.try_load_query_result(tcx, id)
92-
}
93-
}
94-
}
95-
);
96-
97-
impl_disk_cacheable_query!(mir_borrowck, |tcx, def_id| {
98-
def_id.is_local() && tcx.is_closure(def_id)
99-
});
100-
101-
impl_disk_cacheable_query!(unsafety_check_result, |_, def_id| def_id.is_local());
102-
impl_disk_cacheable_query!(borrowck, |_, def_id| def_id.is_local());
103-
impl_disk_cacheable_query!(check_match, |_, def_id| def_id.is_local());
104-
impl_disk_cacheable_query!(predicates_of, |_, def_id| def_id.is_local());
105-
impl_disk_cacheable_query!(used_trait_imports, |_, def_id| def_id.is_local());
106-
impl_disk_cacheable_query!(codegen_fn_attrs, |_, _| true);
107-
impl_disk_cacheable_query!(specialization_graph_of, |_, _| true);

src/librustc/ty/query/on_disk_cache.rs

+3-21
Original file line numberDiff line numberDiff line change
@@ -221,26 +221,8 @@ impl<'sess> OnDiskCache<'sess> {
221221
encode_query_results::<check_match<'_>, _>(tcx, enc, qri)?;
222222
encode_query_results::<codegen_fn_attrs<'_>, _>(tcx, enc, qri)?;
223223
encode_query_results::<specialization_graph_of<'_>, _>(tcx, enc, qri)?;
224-
225-
// const eval is special, it only encodes successfully evaluated constants
226-
use crate::ty::query::QueryAccessors;
227-
let cache = const_eval::query_cache(tcx).borrow();
228-
assert!(cache.active.is_empty());
229-
for (key, entry) in cache.results.iter() {
230-
use crate::ty::query::config::QueryDescription;
231-
if const_eval::cache_on_disk(tcx, key.clone()) {
232-
if let Ok(ref value) = entry.value {
233-
let dep_node = SerializedDepNodeIndex::new(entry.index.index());
234-
235-
// Record position of the cache entry
236-
qri.push((dep_node, AbsoluteBytePos::new(enc.position())));
237-
238-
// Encode the type check tables with the SerializedDepNodeIndex
239-
// as tag.
240-
enc.encode_tagged(dep_node, value)?;
241-
}
242-
}
243-
}
224+
encode_query_results::<const_eval_raw<'_>, _>(tcx, enc, qri)?;
225+
encode_query_results::<const_eval<'_>, _>(tcx, enc, qri)?;
244226

245227
Ok(())
246228
})?;
@@ -1090,7 +1072,7 @@ where
10901072
let map = Q::query_cache(tcx).borrow();
10911073
assert!(map.active.is_empty());
10921074
for (key, entry) in map.results.iter() {
1093-
if Q::cache_on_disk(tcx, key.clone()) {
1075+
if Q::cache_on_disk(tcx, key.clone(), Some(&entry.value)) {
10941076
let dep_node = SerializedDepNodeIndex::new(entry.index.index());
10951077

10961078
// Record position of the cache entry

src/librustc/ty/query/plumbing.rs

+1-64
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ impl<'tcx> TyCtxt<'tcx> {
444444
debug_assert!(self.dep_graph.is_green(dep_node));
445445

446446
// First we try to load the result from the on-disk cache
447-
let result = if Q::cache_on_disk(self.global_tcx(), key.clone()) &&
447+
let result = if Q::cache_on_disk(self.global_tcx(), key.clone(), None) &&
448448
self.sess.opts.debugging_opts.incremental_queries {
449449
self.sess.profiler(|p| p.incremental_load_result_start(Q::NAME));
450450
let result = Q::try_load_from_disk(self.global_tcx(), prev_dep_node_index);
@@ -1210,66 +1210,3 @@ pub fn force_from_dep_node<'tcx>(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> bool
12101210

12111211
true
12121212
}
1213-
1214-
1215-
// FIXME(#45015): Another piece of boilerplate code that could be generated in
1216-
// a combined define_dep_nodes!()/define_queries!() macro.
1217-
macro_rules! impl_load_from_cache {
1218-
($($dep_kind:ident => $query_name:ident,)*) => {
1219-
impl DepNode {
1220-
// Check whether the query invocation corresponding to the given
1221-
// DepNode is eligible for on-disk-caching.
1222-
pub fn cache_on_disk(&self, tcx: TyCtxt<'_>) -> bool {
1223-
use crate::ty::query::queries;
1224-
use crate::ty::query::QueryDescription;
1225-
1226-
match self.kind {
1227-
$(DepKind::$dep_kind => {
1228-
let def_id = self.extract_def_id(tcx).unwrap();
1229-
queries::$query_name::cache_on_disk(tcx.global_tcx(), def_id)
1230-
})*
1231-
_ => false
1232-
}
1233-
}
1234-
1235-
// This is method will execute the query corresponding to the given
1236-
// DepNode. It is only expected to work for DepNodes where the
1237-
// above `cache_on_disk` methods returns true.
1238-
// Also, as a sanity check, it expects that the corresponding query
1239-
// invocation has been marked as green already.
1240-
pub fn load_from_on_disk_cache(&self, tcx: TyCtxt<'_>) {
1241-
match self.kind {
1242-
$(DepKind::$dep_kind => {
1243-
debug_assert!(tcx.dep_graph
1244-
.node_color(self)
1245-
.map(|c| c.is_green())
1246-
.unwrap_or(false));
1247-
1248-
let def_id = self.extract_def_id(tcx).unwrap();
1249-
let _ = tcx.$query_name(def_id);
1250-
})*
1251-
_ => {
1252-
bug!()
1253-
}
1254-
}
1255-
}
1256-
}
1257-
}
1258-
}
1259-
1260-
impl_load_from_cache!(
1261-
typeck_tables_of => typeck_tables_of,
1262-
optimized_mir => optimized_mir,
1263-
unsafety_check_result => unsafety_check_result,
1264-
borrowck => borrowck,
1265-
mir_borrowck => mir_borrowck,
1266-
mir_const_qualif => mir_const_qualif,
1267-
const_is_rvalue_promotable_to_static => const_is_rvalue_promotable_to_static,
1268-
check_match => check_match,
1269-
type_of => type_of,
1270-
generics_of => generics_of,
1271-
predicates_of => predicates_of,
1272-
used_trait_imports => used_trait_imports,
1273-
codegen_fn_attrs => codegen_fn_attrs,
1274-
specialization_graph_of => specialization_graph_of,
1275-
);

0 commit comments

Comments
 (0)