From 85fc539ee41e1bca95a884396d9b653fcad95841 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Thu, 12 Nov 2020 20:48:37 +0100 Subject: [PATCH 1/5] Access the session directly from DepContext. --- Cargo.lock | 1 + compiler/rustc_middle/src/dep_graph/mod.rs | 25 ++++++------------- .../rustc_middle/src/ty/query/plumbing.rs | 7 ------ compiler/rustc_query_system/Cargo.toml | 1 + .../src/dep_graph/dep_node.rs | 5 +++- .../rustc_query_system/src/dep_graph/graph.rs | 6 ++--- .../rustc_query_system/src/dep_graph/mod.rs | 13 +++------- compiler/rustc_query_system/src/query/mod.rs | 3 --- .../rustc_query_system/src/query/plumbing.rs | 2 +- 9 files changed, 22 insertions(+), 41 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9fef7fc1e3528..2f12c54e30717 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4084,6 +4084,7 @@ dependencies = [ "rustc_index", "rustc_macros", "rustc_serialize", + "rustc_session", "rustc_span", "smallvec 1.4.2", "tracing", diff --git a/compiler/rustc_middle/src/dep_graph/mod.rs b/compiler/rustc_middle/src/dep_graph/mod.rs index e641c1cd77bdb..92254537813e2 100644 --- a/compiler/rustc_middle/src/dep_graph/mod.rs +++ b/compiler/rustc_middle/src/dep_graph/mod.rs @@ -6,6 +6,7 @@ use rustc_data_structures::sync::Lock; use rustc_data_structures::thin_vec::ThinVec; use rustc_errors::Diagnostic; use rustc_hir::def_id::{DefPathHash, LocalDefId}; +use rustc_session::Session; mod dep_node; @@ -101,12 +102,14 @@ impl<'tcx> DepContext for TyCtxt<'tcx> { TyCtxt::create_stable_hashing_context(*self) } - fn debug_dep_tasks(&self) -> bool { - self.sess.opts.debugging_opts.dep_tasks + #[inline(always)] + fn profiler(&self) -> &SelfProfilerRef { + &self.prof } - fn debug_dep_node(&self) -> bool { - self.sess.opts.debugging_opts.incremental_info - || self.sess.opts.debugging_opts.query_dep_graph + + #[inline(always)] + fn sess(&self) -> &Session { + self.sess } fn try_force_from_dep_node(&self, dep_node: &DepNode) -> bool { @@ -156,14 +159,6 @@ impl<'tcx> DepContext for TyCtxt<'tcx> { ty::query::force_from_dep_node(*self, dep_node) } - fn has_errors_or_delayed_span_bugs(&self) -> bool { - self.sess.has_errors_or_delayed_span_bugs() - } - - fn diagnostic(&self) -> &rustc_errors::Handler { - self.sess.diagnostic() - } - // Interactions with on_disk_cache fn try_load_from_on_disk_cache(&self, dep_node: &DepNode) { try_load_from_on_disk_cache(*self, dep_node) @@ -192,10 +187,6 @@ impl<'tcx> DepContext for TyCtxt<'tcx> { c.store_diagnostics_for_anon_node(dep_node_index, diagnostics) } } - - fn profiler(&self) -> &SelfProfilerRef { - &self.prof - } } fn def_id_corresponds_to_hir_dep_node(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool { diff --git a/compiler/rustc_middle/src/ty/query/plumbing.rs b/compiler/rustc_middle/src/ty/query/plumbing.rs index d0730bd121c98..4d0c79c66a148 100644 --- a/compiler/rustc_middle/src/ty/query/plumbing.rs +++ b/compiler/rustc_middle/src/ty/query/plumbing.rs @@ -19,13 +19,6 @@ use rustc_span::Span; impl QueryContext for TyCtxt<'tcx> { type Query = Query<'tcx>; - fn incremental_verify_ich(&self) -> bool { - self.sess.opts.debugging_opts.incremental_verify_ich - } - fn verbose(&self) -> bool { - self.sess.verbose() - } - fn def_path_str(&self, def_id: DefId) -> String { TyCtxt::def_path_str(*self, def_id) } diff --git a/compiler/rustc_query_system/Cargo.toml b/compiler/rustc_query_system/Cargo.toml index f38d62dec0046..d97156df6fb29 100644 --- a/compiler/rustc_query_system/Cargo.toml +++ b/compiler/rustc_query_system/Cargo.toml @@ -16,6 +16,7 @@ rustc_errors = { path = "../rustc_errors" } rustc_macros = { path = "../rustc_macros" } rustc_index = { path = "../rustc_index" } rustc_serialize = { path = "../rustc_serialize" } +rustc_session = { path = "../rustc_session" } rustc_span = { path = "../rustc_span" } parking_lot = "0.11" smallvec = { version = "1.0", features = ["union", "may_dangle"] } diff --git a/compiler/rustc_query_system/src/dep_graph/dep_node.rs b/compiler/rustc_query_system/src/dep_graph/dep_node.rs index 09e5dc857a751..3a96a8eeaa7c0 100644 --- a/compiler/rustc_query_system/src/dep_graph/dep_node.rs +++ b/compiler/rustc_query_system/src/dep_graph/dep_node.rs @@ -88,7 +88,10 @@ impl DepNode { #[cfg(debug_assertions)] { - if !kind.can_reconstruct_query_key() && tcx.debug_dep_node() { + if !kind.can_reconstruct_query_key() + && (tcx.sess().opts.debugging_opts.incremental_info + || tcx.sess().opts.debugging_opts.query_dep_graph) + { tcx.dep_graph().register_dep_node_debug_str(dep_node, || arg.to_debug_str(tcx)); } } diff --git a/compiler/rustc_query_system/src/dep_graph/graph.rs b/compiler/rustc_query_system/src/dep_graph/graph.rs index 956d476d973ef..62a8fa2f0d85b 100644 --- a/compiler/rustc_query_system/src/dep_graph/graph.rs +++ b/compiler/rustc_query_system/src/dep_graph/graph.rs @@ -258,7 +258,7 @@ impl DepGraph { task_deps.map(|lock| lock.into_inner()), ); - let print_status = cfg!(debug_assertions) && cx.debug_dep_tasks(); + let print_status = cfg!(debug_assertions) && cx.sess().opts.debugging_opts.dep_tasks; // Determine the color of the new DepNode. if let Some(prev_index) = data.previous.node_to_index_opt(&key) { @@ -645,7 +645,7 @@ impl DepGraph { return None; } None => { - if !tcx.has_errors_or_delayed_span_bugs() { + if !tcx.sess().has_errors_or_delayed_span_bugs() { panic!( "try_mark_previous_green() - Forcing the DepNode \ should have set its color" @@ -753,7 +753,7 @@ impl DepGraph { // Promote the previous diagnostics to the current session. tcx.store_diagnostics(dep_node_index, diagnostics.clone().into()); - let handle = tcx.diagnostic(); + let handle = tcx.sess().diagnostic(); for diagnostic in diagnostics { handle.emit_diagnostic(&diagnostic); diff --git a/compiler/rustc_query_system/src/dep_graph/mod.rs b/compiler/rustc_query_system/src/dep_graph/mod.rs index 3b4b62ad6be88..0d9e84500c624 100644 --- a/compiler/rustc_query_system/src/dep_graph/mod.rs +++ b/compiler/rustc_query_system/src/dep_graph/mod.rs @@ -15,6 +15,7 @@ use rustc_data_structures::profiling::SelfProfilerRef; use rustc_data_structures::sync::Lock; use rustc_data_structures::thin_vec::ThinVec; use rustc_errors::Diagnostic; +use rustc_session::Session; use rustc_span::def_id::DefPathHash; use std::fmt; @@ -27,20 +28,11 @@ pub trait DepContext: Copy { /// Create a hashing context for hashing new results. fn create_stable_hashing_context(&self) -> Self::StableHashingContext; - fn debug_dep_tasks(&self) -> bool; - fn debug_dep_node(&self) -> bool; - /// Try to force a dep node to execute and see if it's green. fn try_force_from_dep_node(&self, dep_node: &DepNode) -> bool; fn register_reused_dep_path_hash(&self, hash: DefPathHash); - /// Return whether the current session is tainted by errors. - fn has_errors_or_delayed_span_bugs(&self) -> bool; - - /// Return the diagnostic handler. - fn diagnostic(&self) -> &rustc_errors::Handler; - /// Load data from the on-disk cache. fn try_load_from_on_disk_cache(&self, dep_node: &DepNode); @@ -59,6 +51,9 @@ pub trait DepContext: Copy { /// Access the profiler. fn profiler(&self) -> &SelfProfilerRef; + + /// Access the compiler session. + fn sess(&self) -> &Session; } /// Describe the different families of dependency nodes. diff --git a/compiler/rustc_query_system/src/query/mod.rs b/compiler/rustc_query_system/src/query/mod.rs index da45565dbe6bd..dc6297b55c257 100644 --- a/compiler/rustc_query_system/src/query/mod.rs +++ b/compiler/rustc_query_system/src/query/mod.rs @@ -26,9 +26,6 @@ use rustc_span::def_id::DefId; pub trait QueryContext: DepContext { type Query: Clone + HashStable; - fn incremental_verify_ich(&self) -> bool; - fn verbose(&self) -> bool; - /// Get string representation from DefPath. fn def_path_str(&self, def_id: DefId) -> String; diff --git a/compiler/rustc_query_system/src/query/plumbing.rs b/compiler/rustc_query_system/src/query/plumbing.rs index 426f5bb41d6f2..4ce2dec56de03 100644 --- a/compiler/rustc_query_system/src/query/plumbing.rs +++ b/compiler/rustc_query_system/src/query/plumbing.rs @@ -530,7 +530,7 @@ where // If `-Zincremental-verify-ich` is specified, re-hash results from // the cache and make sure that they have the expected fingerprint. - if unlikely!(tcx.incremental_verify_ich()) { + if unlikely!(tcx.sess().opts.debugging_opts.incremental_verify_ich) { incremental_verify_ich(tcx, &result, dep_node, dep_node_index, query); } From 3b3513a5aa1f80856e39d34657d292be832bc167 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Tue, 20 Oct 2020 22:55:39 +0200 Subject: [PATCH 2/5] Monomorphize query serialisation. --- compiler/rustc_middle/src/ty/context.rs | 6 ++---- .../src/ty/query/on_disk_cache.rs | 20 +++++++++---------- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index a8d007c0be27d..5f5eb86d442e6 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -47,6 +47,7 @@ use rustc_hir::{ }; use rustc_index::vec::{Idx, IndexVec}; use rustc_macros::HashStable; +use rustc_serialize::opaque; use rustc_session::config::{BorrowckMode, CrateType, OutputFilenames}; use rustc_session::lint::{Level, Lint}; use rustc_session::Session; @@ -1335,10 +1336,7 @@ impl<'tcx> TyCtxt<'tcx> { } } - pub fn serialize_query_result_cache(self, encoder: &mut E) -> Result<(), E::Error> - where - E: ty::codec::OpaqueEncoder, - { + pub fn serialize_query_result_cache(self, encoder: &mut opaque::Encoder) -> Result<(), !> { self.queries.on_disk_cache.as_ref().map(|c| c.serialize(self, encoder)).unwrap_or(Ok(())) } diff --git a/compiler/rustc_middle/src/ty/query/on_disk_cache.rs b/compiler/rustc_middle/src/ty/query/on_disk_cache.rs index e006dfeb66336..446f887185511 100644 --- a/compiler/rustc_middle/src/ty/query/on_disk_cache.rs +++ b/compiler/rustc_middle/src/ty/query/on_disk_cache.rs @@ -240,10 +240,11 @@ impl<'sess> OnDiskCache<'sess> { } } - pub fn serialize<'tcx, E>(&self, tcx: TyCtxt<'tcx>, encoder: &mut E) -> Result<(), E::Error> - where - E: OpaqueEncoder, - { + pub fn serialize<'tcx>( + &self, + tcx: TyCtxt<'tcx>, + encoder: &mut opaque::Encoder, + ) -> Result<(), !> { // Serializing the `DepGraph` should not modify it. tcx.dep_graph.with_ignore(|| { // Allocate `SourceFileIndex`es. @@ -297,7 +298,7 @@ impl<'sess> OnDiskCache<'sess> { macro_rules! encode_queries { ($($query:ident,)*) => { $( - encode_query_results::, _>( + encode_query_results::>( tcx, enc, qri @@ -1167,15 +1168,14 @@ impl<'a> Decodable> for IntEncodedWithFixedSize { } } -fn encode_query_results<'a, 'tcx, Q, E>( +fn encode_query_results<'a, 'tcx, Q>( tcx: TyCtxt<'tcx>, - encoder: &mut CacheEncoder<'a, 'tcx, E>, + encoder: &mut CacheEncoder<'a, 'tcx, opaque::Encoder>, query_result_index: &mut EncodedQueryResultIndex, -) -> Result<(), E::Error> +) -> Result<(), !> where Q: super::QueryDescription> + super::QueryAccessors>, - Q::Value: Encodable>, - E: 'a + OpaqueEncoder, + Q::Value: Encodable>, { let _timer = tcx .sess From 20ecbbb21bd7e653d71ddc2763918b774982c363 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Wed, 21 Oct 2020 21:31:10 +0200 Subject: [PATCH 3/5] Make OpaqueEncoder private. --- compiler/rustc_middle/src/ty/codec.rs | 16 ---------------- .../src/ty/query/on_disk_cache.rs | 19 ++++++++++++++++++- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/compiler/rustc_middle/src/ty/codec.rs b/compiler/rustc_middle/src/ty/codec.rs index 1983af17dd076..cd099a2ad27e3 100644 --- a/compiler/rustc_middle/src/ty/codec.rs +++ b/compiler/rustc_middle/src/ty/codec.rs @@ -50,22 +50,6 @@ impl<'tcx, E: TyEncoder<'tcx>> EncodableWithShorthand<'tcx, E> for ty::Predicate } } -pub trait OpaqueEncoder: Encoder { - fn opaque(&mut self) -> &mut rustc_serialize::opaque::Encoder; - fn encoder_position(&self) -> usize; -} - -impl OpaqueEncoder for rustc_serialize::opaque::Encoder { - #[inline] - fn opaque(&mut self) -> &mut rustc_serialize::opaque::Encoder { - self - } - #[inline] - fn encoder_position(&self) -> usize { - self.position() - } -} - pub trait TyEncoder<'tcx>: Encoder { const CLEAR_CROSS_CRATE: bool; diff --git a/compiler/rustc_middle/src/ty/query/on_disk_cache.rs b/compiler/rustc_middle/src/ty/query/on_disk_cache.rs index 446f887185511..959e1c4b164d7 100644 --- a/compiler/rustc_middle/src/ty/query/on_disk_cache.rs +++ b/compiler/rustc_middle/src/ty/query/on_disk_cache.rs @@ -1,7 +1,7 @@ use crate::dep_graph::{DepNodeIndex, SerializedDepNodeIndex}; use crate::mir::interpret::{AllocDecodingSession, AllocDecodingState}; use crate::mir::{self, interpret}; -use crate::ty::codec::{OpaqueEncoder, RefDecodable, TyDecoder, TyEncoder}; +use crate::ty::codec::{RefDecodable, TyDecoder, TyEncoder}; use crate::ty::context::TyCtxt; use crate::ty::{self, Ty}; use rustc_data_structures::fingerprint::{Fingerprint, FingerprintDecoder, FingerprintEncoder}; @@ -937,6 +937,23 @@ impl<'a, 'tcx> Decodable> for &'tcx [Span] { //- ENCODING ------------------------------------------------------------------- +/// This trait is a hack to work around specialization bug #55243. +trait OpaqueEncoder: Encoder { + fn opaque(&mut self) -> &mut opaque::Encoder; + fn encoder_position(&self) -> usize; +} + +impl OpaqueEncoder for opaque::Encoder { + #[inline] + fn opaque(&mut self) -> &mut opaque::Encoder { + self + } + #[inline] + fn encoder_position(&self) -> usize { + self.position() + } +} + /// An encoder that can write the incr. comp. cache. struct CacheEncoder<'a, 'tcx, E: OpaqueEncoder> { tcx: TyCtxt<'tcx>, From 268ab6d65827ab386487f17f23fe0513ed7009db Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Wed, 21 Oct 2020 21:48:19 +0200 Subject: [PATCH 4/5] Reduce visibilities. --- compiler/rustc_middle/src/ty/query/on_disk_cache.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_middle/src/ty/query/on_disk_cache.rs b/compiler/rustc_middle/src/ty/query/on_disk_cache.rs index 959e1c4b164d7..dab7bfe4a598e 100644 --- a/compiler/rustc_middle/src/ty/query/on_disk_cache.rs +++ b/compiler/rustc_middle/src/ty/query/on_disk_cache.rs @@ -240,7 +240,7 @@ impl<'sess> OnDiskCache<'sess> { } } - pub fn serialize<'tcx>( + crate fn serialize<'tcx>( &self, tcx: TyCtxt<'tcx>, encoder: &mut opaque::Encoder, @@ -422,7 +422,7 @@ impl<'sess> OnDiskCache<'sess> { } /// Loads a diagnostic emitted during the previous compilation session. - pub fn load_diagnostics( + crate fn load_diagnostics( &self, tcx: TyCtxt<'_>, dep_node_index: SerializedDepNodeIndex, @@ -438,7 +438,7 @@ impl<'sess> OnDiskCache<'sess> { /// the next compilation session. #[inline(never)] #[cold] - pub fn store_diagnostics( + crate fn store_diagnostics( &self, dep_node_index: DepNodeIndex, diagnostics: ThinVec, @@ -506,7 +506,7 @@ impl<'sess> OnDiskCache<'sess> { /// 1:1 relationship between query-key and `DepNode`. #[inline(never)] #[cold] - pub fn store_diagnostics_for_anon_node( + crate fn store_diagnostics_for_anon_node( &self, dep_node_index: DepNodeIndex, diagnostics: ThinVec, @@ -1153,7 +1153,7 @@ where struct IntEncodedWithFixedSize(u64); impl IntEncodedWithFixedSize { - pub const ENCODED_SIZE: usize = 8; + const ENCODED_SIZE: usize = 8; } impl Encodable for IntEncodedWithFixedSize { From 203e1ced8bc3df001c901fa0c44d70d24bbe4c3a Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Thu, 12 Nov 2020 21:34:25 +0100 Subject: [PATCH 5/5] Introduce a OnDiskCache trait. --- .../rustc_incremental/src/persist/save.rs | 4 +- compiler/rustc_middle/src/dep_graph/mod.rs | 33 +++-------- compiler/rustc_middle/src/ty/context.rs | 5 +- .../src/ty/query/on_disk_cache.rs | 55 +++++++++---------- .../rustc_query_system/src/dep_graph/graph.rs | 11 +++- .../rustc_query_system/src/dep_graph/mod.rs | 35 ++++++++---- .../rustc_query_system/src/query/plumbing.rs | 10 +++- 7 files changed, 77 insertions(+), 76 deletions(-) diff --git a/compiler/rustc_incremental/src/persist/save.rs b/compiler/rustc_incremental/src/persist/save.rs index 102a77e8e79a7..dcf77774b6ea6 100644 --- a/compiler/rustc_incremental/src/persist/save.rs +++ b/compiler/rustc_incremental/src/persist/save.rs @@ -235,7 +235,5 @@ fn encode_work_product_index( } fn encode_query_cache(tcx: TyCtxt<'_>, encoder: &mut Encoder) { - tcx.sess.time("incr_comp_serialize_result_cache", || { - tcx.serialize_query_result_cache(encoder).unwrap(); - }) + tcx.sess.time("incr_comp_serialize_result_cache", || tcx.serialize_query_result_cache(encoder)) } diff --git a/compiler/rustc_middle/src/dep_graph/mod.rs b/compiler/rustc_middle/src/dep_graph/mod.rs index 92254537813e2..cd7ed4f7ca0aa 100644 --- a/compiler/rustc_middle/src/dep_graph/mod.rs +++ b/compiler/rustc_middle/src/dep_graph/mod.rs @@ -1,10 +1,9 @@ use crate::ich::StableHashingContext; use crate::ty::query::try_load_from_on_disk_cache; +use crate::ty::query::OnDiskCache; use crate::ty::{self, TyCtxt}; use rustc_data_structures::profiling::SelfProfilerRef; use rustc_data_structures::sync::Lock; -use rustc_data_structures::thin_vec::ThinVec; -use rustc_errors::Diagnostic; use rustc_hir::def_id::{DefPathHash, LocalDefId}; use rustc_session::Session; @@ -90,6 +89,7 @@ impl rustc_query_system::dep_graph::DepKind for DepKind { impl<'tcx> DepContext for TyCtxt<'tcx> { type DepKind = DepKind; + type OnDiskCache = OnDiskCache<'tcx>; type StableHashingContext = StableHashingContext<'tcx>; fn register_reused_dep_path_hash(&self, hash: DefPathHash) { @@ -102,6 +102,11 @@ impl<'tcx> DepContext for TyCtxt<'tcx> { TyCtxt::create_stable_hashing_context(*self) } + #[inline(always)] + fn on_disk_cache(&self) -> Option<&OnDiskCache<'tcx>> { + self.queries.on_disk_cache.as_ref() + } + #[inline(always)] fn profiler(&self) -> &SelfProfilerRef { &self.prof @@ -163,30 +168,6 @@ impl<'tcx> DepContext for TyCtxt<'tcx> { fn try_load_from_on_disk_cache(&self, dep_node: &DepNode) { try_load_from_on_disk_cache(*self, dep_node) } - - fn load_diagnostics(&self, prev_dep_node_index: SerializedDepNodeIndex) -> Vec { - self.queries - .on_disk_cache - .as_ref() - .map(|c| c.load_diagnostics(*self, prev_dep_node_index)) - .unwrap_or_default() - } - - fn store_diagnostics(&self, dep_node_index: DepNodeIndex, diagnostics: ThinVec) { - if let Some(c) = self.queries.on_disk_cache.as_ref() { - c.store_diagnostics(dep_node_index, diagnostics) - } - } - - fn store_diagnostics_for_anon_node( - &self, - dep_node_index: DepNodeIndex, - diagnostics: ThinVec, - ) { - if let Some(c) = self.queries.on_disk_cache.as_ref() { - c.store_diagnostics_for_anon_node(dep_node_index, diagnostics) - } - } } fn def_id_corresponds_to_hir_dep_node(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool { diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index 5f5eb86d442e6..f1ef6dc36d597 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -47,6 +47,7 @@ use rustc_hir::{ }; use rustc_index::vec::{Idx, IndexVec}; use rustc_macros::HashStable; +use rustc_query_system::dep_graph::OnDiskCache as _; use rustc_serialize::opaque; use rustc_session::config::{BorrowckMode, CrateType, OutputFilenames}; use rustc_session::lint::{Level, Lint}; @@ -1336,8 +1337,8 @@ impl<'tcx> TyCtxt<'tcx> { } } - pub fn serialize_query_result_cache(self, encoder: &mut opaque::Encoder) -> Result<(), !> { - self.queries.on_disk_cache.as_ref().map(|c| c.serialize(self, encoder)).unwrap_or(Ok(())) + pub fn serialize_query_result_cache(self, encoder: &mut opaque::Encoder) { + self.queries.on_disk_cache.as_ref().map(|c| c.serialize(self, encoder)).unwrap_or(()) } /// If `true`, we should use the MIR-based borrowck, but also diff --git a/compiler/rustc_middle/src/ty/query/on_disk_cache.rs b/compiler/rustc_middle/src/ty/query/on_disk_cache.rs index dab7bfe4a598e..0fd54bd2582fb 100644 --- a/compiler/rustc_middle/src/ty/query/on_disk_cache.rs +++ b/compiler/rustc_middle/src/ty/query/on_disk_cache.rs @@ -239,14 +239,12 @@ impl<'sess> OnDiskCache<'sess> { def_path_hash_to_def_id_cache: Default::default(), } } +} - crate fn serialize<'tcx>( - &self, - tcx: TyCtxt<'tcx>, - encoder: &mut opaque::Encoder, - ) -> Result<(), !> { +impl<'tcx> rustc_query_system::dep_graph::OnDiskCache> for OnDiskCache<'tcx> { + fn serialize(&self, tcx: TyCtxt<'tcx>, encoder: &mut opaque::Encoder) { // Serializing the `DepGraph` should not modify it. - tcx.dep_graph.with_ignore(|| { + let ret: Result<(), !> = tcx.dep_graph.with_ignore(|| { // Allocate `SourceFileIndex`es. let (file_to_file_index, file_index_to_stable_id) = { let files = tcx.sess.source_map().files(); @@ -418,11 +416,12 @@ impl<'sess> OnDiskCache<'sess> { cnums.dedup(); cnums } - }) + }); + ret.unwrap() } /// Loads a diagnostic emitted during the previous compilation session. - crate fn load_diagnostics( + fn load_diagnostics( &self, tcx: TyCtxt<'_>, dep_node_index: SerializedDepNodeIndex, @@ -438,16 +437,32 @@ impl<'sess> OnDiskCache<'sess> { /// the next compilation session. #[inline(never)] #[cold] - crate fn store_diagnostics( + fn store_diagnostics(&self, dep_node_index: DepNodeIndex, diagnostics: ThinVec) { + let mut current_diagnostics = self.current_diagnostics.borrow_mut(); + let prev = current_diagnostics.insert(dep_node_index, diagnostics.into()); + debug_assert!(prev.is_none()); + } + + /// Stores a diagnostic emitted during computation of an anonymous query. + /// Since many anonymous queries can share the same `DepNode`, we aggregate + /// them -- as opposed to regular queries where we assume that there is a + /// 1:1 relationship between query-key and `DepNode`. + #[inline(never)] + #[cold] + fn store_diagnostics_for_anon_node( &self, dep_node_index: DepNodeIndex, diagnostics: ThinVec, ) { let mut current_diagnostics = self.current_diagnostics.borrow_mut(); - let prev = current_diagnostics.insert(dep_node_index, diagnostics.into()); - debug_assert!(prev.is_none()); + + let x = current_diagnostics.entry(dep_node_index).or_insert(Vec::new()); + + x.extend(Into::>::into(diagnostics)); } +} +impl<'sess> OnDiskCache<'sess> { fn get_raw_def_id(&self, hash: &DefPathHash) -> Option { self.foreign_def_path_hashes.get(hash).copied() } @@ -500,24 +515,6 @@ impl<'sess> OnDiskCache<'sess> { self.load_indexed(tcx, dep_node_index, &self.query_result_index, "query result") } - /// Stores a diagnostic emitted during computation of an anonymous query. - /// Since many anonymous queries can share the same `DepNode`, we aggregate - /// them -- as opposed to regular queries where we assume that there is a - /// 1:1 relationship between query-key and `DepNode`. - #[inline(never)] - #[cold] - crate fn store_diagnostics_for_anon_node( - &self, - dep_node_index: DepNodeIndex, - diagnostics: ThinVec, - ) { - let mut current_diagnostics = self.current_diagnostics.borrow_mut(); - - let x = current_diagnostics.entry(dep_node_index).or_insert(Vec::new()); - - x.extend(Into::>::into(diagnostics)); - } - fn load_indexed<'tcx, T>( &self, tcx: TyCtxt<'tcx>, diff --git a/compiler/rustc_query_system/src/dep_graph/graph.rs b/compiler/rustc_query_system/src/dep_graph/graph.rs index 62a8fa2f0d85b..a48da0196f9ff 100644 --- a/compiler/rustc_query_system/src/dep_graph/graph.rs +++ b/compiler/rustc_query_system/src/dep_graph/graph.rs @@ -21,7 +21,7 @@ use super::debug::EdgeFilter; use super::prev::PreviousDepGraph; use super::query::DepGraphQuery; use super::serialized::{SerializedDepGraph, SerializedDepNodeIndex}; -use super::{DepContext, DepKind, DepNode, WorkProductId}; +use super::{DepContext, DepKind, DepNode, OnDiskCache, WorkProductId}; #[derive(Clone)] pub struct DepGraph { @@ -703,7 +703,10 @@ impl DepGraph { // FIXME: Store the fact that a node has diagnostics in a bit in the dep graph somewhere // Maybe store a list on disk and encode this fact in the DepNodeState - let diagnostics = tcx.load_diagnostics(prev_dep_node_index); + let diagnostics = tcx + .on_disk_cache() + .map(|c| c.load_diagnostics(tcx, prev_dep_node_index)) + .unwrap_or(Vec::new()); #[cfg(not(parallel_compiler))] debug_assert!( @@ -751,7 +754,9 @@ impl DepGraph { mem::drop(emitting); // Promote the previous diagnostics to the current session. - tcx.store_diagnostics(dep_node_index, diagnostics.clone().into()); + if let Some(c) = tcx.on_disk_cache() { + c.store_diagnostics(dep_node_index, diagnostics.clone().into()); + } let handle = tcx.sess().diagnostic(); diff --git a/compiler/rustc_query_system/src/dep_graph/mod.rs b/compiler/rustc_query_system/src/dep_graph/mod.rs index 0d9e84500c624..26a96879aa161 100644 --- a/compiler/rustc_query_system/src/dep_graph/mod.rs +++ b/compiler/rustc_query_system/src/dep_graph/mod.rs @@ -15,6 +15,7 @@ use rustc_data_structures::profiling::SelfProfilerRef; use rustc_data_structures::sync::Lock; use rustc_data_structures::thin_vec::ThinVec; use rustc_errors::Diagnostic; +use rustc_serialize::opaque; use rustc_session::Session; use rustc_span::def_id::DefPathHash; @@ -23,6 +24,7 @@ use std::hash::Hash; pub trait DepContext: Copy { type DepKind: self::DepKind; + type OnDiskCache: self::OnDiskCache; type StableHashingContext; /// Create a hashing context for hashing new results. @@ -36,24 +38,37 @@ pub trait DepContext: Copy { /// Load data from the on-disk cache. fn try_load_from_on_disk_cache(&self, dep_node: &DepNode); - /// Load diagnostics associated to the node in the previous session. - fn load_diagnostics(&self, prev_dep_node_index: SerializedDepNodeIndex) -> Vec; + /// Access the on_disk_cache. + fn on_disk_cache(&self) -> Option<&Self::OnDiskCache>; - /// Register diagnostics for the given node, for use in next session. + /// Access the profiler. + fn profiler(&self) -> &SelfProfilerRef; + + /// Access the compiler session. + fn sess(&self) -> &Session; +} + +pub trait OnDiskCache { + fn serialize(&self, tcx: CTX, encoder: &mut opaque::Encoder); + + /// Loads a diagnostic emitted during the previous compilation session. + fn load_diagnostics(&self, tcx: CTX, dep_node_index: SerializedDepNodeIndex) + -> Vec; + + /// Stores a diagnostic emitted during the current compilation session. + /// Anything stored like this will be available via `load_diagnostics` in + /// the next compilation session. fn store_diagnostics(&self, dep_node_index: DepNodeIndex, diagnostics: ThinVec); - /// Register diagnostics for the given node, for use in next session. + /// Stores a diagnostic emitted during computation of an anonymous query. + /// Since many anonymous queries can share the same `DepNode`, we aggregate + /// them -- as opposed to regular queries where we assume that there is a + /// 1:1 relationship between query-key and `DepNode`. fn store_diagnostics_for_anon_node( &self, dep_node_index: DepNodeIndex, diagnostics: ThinVec, ); - - /// Access the profiler. - fn profiler(&self) -> &SelfProfilerRef; - - /// Access the compiler session. - fn sess(&self) -> &Session; } /// Describe the different families of dependency nodes. diff --git a/compiler/rustc_query_system/src/query/plumbing.rs b/compiler/rustc_query_system/src/query/plumbing.rs index 4ce2dec56de03..e7d31ee2b717e 100644 --- a/compiler/rustc_query_system/src/query/plumbing.rs +++ b/compiler/rustc_query_system/src/query/plumbing.rs @@ -2,7 +2,7 @@ //! generate the actual methods on tcx which find and execute the provider, //! manage the caches, and so forth. -use crate::dep_graph::{DepKind, DepNode}; +use crate::dep_graph::{DepKind, DepNode, OnDiskCache}; use crate::dep_graph::{DepNodeIndex, SerializedDepNodeIndex}; use crate::query::caches::QueryCache; use crate::query::config::{QueryDescription, QueryVtable, QueryVtableExt}; @@ -440,7 +440,9 @@ where tcx.dep_graph().read_index(dep_node_index); if unlikely!(!diagnostics.is_empty()) { - tcx.store_diagnostics_for_anon_node(dep_node_index, diagnostics); + if let Some(c) = tcx.on_disk_cache() { + c.store_diagnostics_for_anon_node(dep_node_index, diagnostics); + } } return job.complete(result, dep_node_index); @@ -613,7 +615,9 @@ where prof_timer.finish_with_query_invocation_id(dep_node_index.into()); if unlikely!(!diagnostics.is_empty()) && dep_node.kind != DepKind::NULL { - tcx.store_diagnostics(dep_node_index, diagnostics); + if let Some(c) = tcx.on_disk_cache() { + c.store_diagnostics(dep_node_index, diagnostics); + } } let result = job.complete(result, dep_node_index);