Skip to content

Access Session and OnDiskCache in rustc_query_system #79035

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4084,6 +4084,7 @@ dependencies = [
"rustc_index",
"rustc_macros",
"rustc_serialize",
"rustc_session",
"rustc_span",
"smallvec 1.4.2",
"tracing",
Expand Down
4 changes: 1 addition & 3 deletions compiler/rustc_incremental/src/persist/save.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
58 changes: 15 additions & 43 deletions compiler/rustc_middle/src/dep_graph/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
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;

mod dep_node;

Expand Down Expand Up @@ -89,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) {
Expand All @@ -101,12 +102,19 @@ 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 on_disk_cache(&self) -> Option<&OnDiskCache<'tcx>> {
self.queries.on_disk_cache.as_ref()
}
fn debug_dep_node(&self) -> bool {
self.sess.opts.debugging_opts.incremental_info
|| self.sess.opts.debugging_opts.query_dep_graph

#[inline(always)]
fn profiler(&self) -> &SelfProfilerRef {
&self.prof
}

#[inline(always)]
fn sess(&self) -> &Session {
self.sess
}

fn try_force_from_dep_node(&self, dep_node: &DepNode) -> bool {
Expand Down Expand Up @@ -156,46 +164,10 @@ 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)
}

fn load_diagnostics(&self, prev_dep_node_index: SerializedDepNodeIndex) -> Vec<Diagnostic> {
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<Diagnostic>) {
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<Diagnostic>,
) {
if let Some(c) = self.queries.on_disk_cache.as_ref() {
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 {
Expand Down
16 changes: 0 additions & 16 deletions compiler/rustc_middle/src/ty/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
9 changes: 4 additions & 5 deletions compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ 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};
use rustc_session::Session;
Expand Down Expand Up @@ -1335,11 +1337,8 @@ impl<'tcx> TyCtxt<'tcx> {
}
}

pub fn serialize_query_result_cache<E>(self, encoder: &mut E) -> Result<(), E::Error>
where
E: ty::codec::OpaqueEncoder,
{
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
Expand Down
86 changes: 50 additions & 36 deletions compiler/rustc_middle/src/ty/query/on_disk_cache.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -239,13 +239,12 @@ impl<'sess> OnDiskCache<'sess> {
def_path_hash_to_def_id_cache: Default::default(),
}
}
}

pub fn serialize<'tcx, E>(&self, tcx: TyCtxt<'tcx>, encoder: &mut E) -> Result<(), E::Error>
where
E: OpaqueEncoder,
{
impl<'tcx> rustc_query_system::dep_graph::OnDiskCache<TyCtxt<'tcx>> 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();
Expand Down Expand Up @@ -297,7 +296,7 @@ impl<'sess> OnDiskCache<'sess> {
macro_rules! encode_queries {
($($query:ident,)*) => {
$(
encode_query_results::<ty::query::queries::$query<'_>, _>(
encode_query_results::<ty::query::queries::$query<'_>>(
tcx,
enc,
qri
Expand Down Expand Up @@ -417,11 +416,12 @@ impl<'sess> OnDiskCache<'sess> {
cnums.dedup();
cnums
}
})
});
ret.unwrap()
}

/// Loads a diagnostic emitted during the previous compilation session.
pub fn load_diagnostics(
fn load_diagnostics(
&self,
tcx: TyCtxt<'_>,
dep_node_index: SerializedDepNodeIndex,
Expand All @@ -437,16 +437,32 @@ impl<'sess> OnDiskCache<'sess> {
/// the next compilation session.
#[inline(never)]
#[cold]
pub fn store_diagnostics(
fn store_diagnostics(&self, dep_node_index: DepNodeIndex, diagnostics: ThinVec<Diagnostic>) {
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<Diagnostic>,
) {
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::<Vec<_>>::into(diagnostics));
}
}

impl<'sess> OnDiskCache<'sess> {
fn get_raw_def_id(&self, hash: &DefPathHash) -> Option<RawDefId> {
self.foreign_def_path_hashes.get(hash).copied()
}
Expand Down Expand Up @@ -499,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]
pub fn store_diagnostics_for_anon_node(
&self,
dep_node_index: DepNodeIndex,
diagnostics: ThinVec<Diagnostic>,
) {
let mut current_diagnostics = self.current_diagnostics.borrow_mut();

let x = current_diagnostics.entry(dep_node_index).or_insert(Vec::new());

x.extend(Into::<Vec<_>>::into(diagnostics));
}

fn load_indexed<'tcx, T>(
&self,
tcx: TyCtxt<'tcx>,
Expand Down Expand Up @@ -936,6 +934,23 @@ impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> 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>,
Expand Down Expand Up @@ -1135,7 +1150,7 @@ where
struct IntEncodedWithFixedSize(u64);

impl IntEncodedWithFixedSize {
pub const ENCODED_SIZE: usize = 8;
const ENCODED_SIZE: usize = 8;
}

impl Encodable<opaque::Encoder> for IntEncodedWithFixedSize {
Expand Down Expand Up @@ -1167,15 +1182,14 @@ impl<'a> Decodable<opaque::Decoder<'a>> 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<TyCtxt<'tcx>> + super::QueryAccessors<TyCtxt<'tcx>>,
Q::Value: Encodable<CacheEncoder<'a, 'tcx, E>>,
E: 'a + OpaqueEncoder,
Q::Value: Encodable<CacheEncoder<'a, 'tcx, opaque::Encoder>>,
{
let _timer = tcx
.sess
Expand Down
7 changes: 0 additions & 7 deletions compiler/rustc_middle/src/ty/query/plumbing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_query_system/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
5 changes: 4 additions & 1 deletion compiler/rustc_query_system/src/dep_graph/dep_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,10 @@ impl<K: DepKind> DepNode<K> {

#[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));
}
}
Expand Down
Loading