8000 Make AST->HIR lowering incremental by cjgillot · Pull Request #88186 · rust-lang/rust · GitHub
[go: up one dir, main page]

Skip to content

Make AST->HIR lowering incremental #88186

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 16 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Separate Definitions and CrateStore from ResolverOutputs.
  • Loading branch information
cjgillot committed Apr 30, 2022
commit 21ec51d98e4df09114d5858e116476336c1aab27
11 changes: 8 additions & 3 deletions compiler/rustc_interface/src/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use rustc_data_structures::temp_dir::MaybeTempDir;
use rustc_errors::{Applicability, ErrorGuaranteed, MultiSpan, PResult};
use rustc_expand::base::{ExtCtxt, LintStoreExpand, ResolverExpand};
use rustc_hir::def_id::{StableCrateId, LOCAL_CRATE};
use rustc_hir::definitions::Definitions;
use rustc_hir::Crate;
use rustc_lint::{EarlyCheckNode, LintStore};
use rustc_metadata::creader::CStore;
Expand All @@ -29,7 +30,7 @@ use rustc_query_impl::{OnDiskCache, Queries as TcxQueries};
use rustc_resolve::{Resolver, ResolverArenas};
use rustc_serialize::json;
use rustc_session::config::{CrateType, Input, OutputFilenames, OutputType};
use rustc_session::cstore::{MetadataLoader, MetadataLoaderDyn};
use rustc_session::cstore::{CrateStoreDyn, MetadataLoader, MetadataLoaderDyn};
use rustc_session::output::{filename_for_input, filename_for_metadata};
use rustc_session::search_paths::PathKind;
use rustc_session::{Limit, Session};
Expand Down Expand Up @@ -141,7 +142,9 @@ mod boxed_resolver {
f((&mut *resolver).as_mut().unwrap())
}

pub fn to_resolver_outputs(resolver: Rc<RefCell<BoxedResolver>>) -> ResolverOutputs {
pub fn to_resolver_outputs(
resolver: Rc<RefCell<BoxedResolver>>,
) -> (Definitions, Box<CrateStoreDyn>, ResolverOutputs) {
match Rc::try_unwrap(resolver) {
Ok(resolver) => {
let mut resolver = resolver.into_inner();
Expand Down Expand Up @@ -841,7 +844,7 @@ pub fn create_global_ctxt<'tcx>(
let sess = &compiler.session();
let krate =
resolver.borrow_mut().access(|resolver| lower_to_hir(sess, resolver, krate, hir_arena));
let resolver_outputs = BoxedResolver::to_resolver_outputs(resolver);
let (definitions, cstore, resolver_outputs) = BoxedResolver::to_resolver_outputs(resolver);

let query_result_on_disk_cache = rustc_incremental::load_query_result_cache(sess);

Expand All @@ -866,6 +869,8 @@ pub fn create_global_ctxt<'tcx>(
sess,
lint_store,
arena,
definitions,
cstore,
resolver_outputs,
krate,
dep_graph,
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_metadata/src/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
}

fn encode_def_path_table(&mut self) {
let table = self.tcx.resolutions(()).definitions.def_path_table();
let table = self.tcx.definitions_untracked().def_path_table();
if self.is_proc_macro {
for def_index in std::iter::once(CRATE_DEF_INDEX)
.chain(self.tcx.resolutions(()).proc_macros.iter().map(|p| p.local_def_index))
Expand All @@ -476,7 +476,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {

fn encode_def_path_hash_map(&mut self) -> Lazy<DefPathHashMapRef<'tcx>> {
self.lazy(DefPathHashMapRef::BorrowedFromTcx(
self.tcx.resolutions(()).definitions.def_path_hash_to_def_index_map(),
self.tcx.definitions_untracked().def_path_hash_to_def_index_map(),
))
}

Expand Down
12 changes: 6 additions & 6 deletions compiler/rustc_middle/src/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ impl<'hir> Map<'hir> {

pub fn def_key(self, def_id: LocalDefId) -> DefKey {
// Accessing the DefKey is ok, since it is part of DefPathHash.
self.tcx.untracked_resolutions.definitions.def_key(def_id)
self.tcx.definitions_untracked().def_key(def_id)
}

pub fn def_path_from_hir_id(self, id: HirId) -> Option<DefPath> {
Expand All @@ -176,13 +176,13 @@ impl<'hir> Map<'hir> {

pub fn def_path(self, def_id: LocalDefId) -> DefPath {
// Accessing the DefPath is ok, since it is part of DefPathHash.
self.tcx.untracked_resolutions.definitions.def_path(def_id)
self.tcx.definitions_untracked().def_path(def_id)
}

#[inline]
pub fn def_path_hash(self, def_id: LocalDefId) -> DefPathHash {
// Accessing the DefPathHash is ok, it is incr. comp. stable.
self.tcx.untracked_resolutions.definitions.def_path_hash(def_id)
self.tcx.definitions_untracked().def_path_hash(def_id)
}

#[inline]
Expand Down Expand Up @@ -219,7 +219,7 @@ impl<'hir> Map<'hir> {
// Create a dependency to the crate to be sure we re-execute this when the amount of
// definitions change.
self.tcx.ensure().hir_crate(());
self.tcx.untracked_resolutions.definitions.iter_local_def_id()
self.tcx.definitions_untracked().iter_local_def_id()
}

pub fn opt_def_kind(self, local_def_id: LocalDefId) -> Option<DefKind> {
Expand Down Expand Up @@ -1104,7 +1104,7 @@ pub(super) fn crate_hash(tcx: TyCtxt<'_>, crate_num: CrateNum) -> Svh {
upstream_crates.hash_stable(&mut hcx, &mut stable_hasher);
source_file_names.hash_stable(&mut hcx, &mut stable_hasher);
if tcx.sess.opts.debugging_opts.incremental_relative_spans {
let definitions = &tcx.untracked_resolutions.definitions;
let definitions = &tcx.definitions_untracked();
let mut owner_spans: Vec<_> = krate
.owners
.iter_enumerated()
Expand Down Expand Up @@ -1135,7 +1135,7 @@ fn upstream_crates(tcx: TyCtxt<'_>) -> Vec<(StableCrateId, Svh)> {
.crates(())
.iter()
.map(|&cnum| {
let stable_crate_id = tcx.resolutions(()).cstore.stable_crate_id(cnum);
let stable_crate_id = tcx.stable_crate_id(cnum);
let hash = tcx.crate_hash(cnum);
(stable_crate_id, hash)
})
Expand Down
15 changes: 6 additions & 9 deletions compiler/rustc_middle/src/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub mod nested_filter;
pub mod place;

use crate::ty::query::Providers;
use crate::ty::{ImplSubject, TyCtxt};
use crate::ty::{DefIdTree, ImplSubject, TyCtxt};
use rustc_data_structures::fingerprint::Fingerprint;
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_hir::def_id::{DefId, LocalDefId};
Expand Down Expand Up @@ -105,22 +105,19 @@ pub fn provide(providers: &mut Providers) {
providers.hir_owner_nodes = |tcx, id| tcx.hir_crate(()).owners[id].map(|i| &i.nodes);
providers.hir_owner_parent = |tcx, id| {
// Accessing the def_key is ok since its value is hashed as part of `id`'s DefPathHash.
let parent = tcx.untracked_resolutions.definitions.def_key(id).parent;
let parent = parent.map_or(CRATE_HIR_ID, |local_def_index| {
let def_id = LocalDefId { local_def_index };
let mut parent_hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
tcx.local_parent(id).map_or(CRATE_HIR_ID, |parent| {
let mut parent_hir_id = tcx.hir().local_def_id_to_hir_id(parent);
if let Some(local_id) =
tcx.hir_crate(()).owners[parent_hir_id.owner].unwrap().parenting.get(&id)
{
parent_hir_id.local_id = *local_id;
}
parent_hir_id
});
parent
})
};
providers.hir_attrs =
|tcx, id| tcx.hir_crate(()).owners[id].as_owner().map_or(AttributeMap::EMPTY, |o| &o.attrs);
providers.source_span = |tcx, def_id| tcx.resolutions(()).definitions.def_span(def_id);
providers.source_span = |tcx, def_id| tcx.definitions_untracked().def_span(def_id);
providers.def_span = |tcx, def_id| tcx.hir().span_if_local(def_id).unwrap_or(DUMMY_SP);
providers.fn_arg_names = |tcx, id| {
let hir = tcx.hir();
Expand All @@ -141,7 +138,7 @@ pub fn provide(providers: &mut Providers) {
providers.all_local_trait_impls = |tcx, ()| &tcx.resolutions(()).trait_impls;
providers.expn_that_defined = |tcx, id| {
let id = id.expect_local();
tcx.resolutions(()).definitions.expansion_that_defined(id)
tcx.definitions_untracked().expansion_that_defined(id)
};
providers.in_scope_traits_map =
|tcx, id| tcx.hir_crate(()).owners[id].as_owner().map(|owner_info| &owner_info.trait_map);
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_middle/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ rustc_queries! {
/// This span is meant for dep-tracking rather than diagnostics. It should not be used outside
/// of rustc_middle::hir::source_map.
query source_span(key: LocalDefId) -> Span {
eval_always
desc { "get the source span" }
}

Expand Down
74 changes: 35 additions & 39 deletions compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ use rustc_middle::mir::FakeReadCause;
use rustc_query_system::ich::StableHashingContext;
use rustc_serialize::opaque::{FileEncodeResult, FileEncoder};
use rustc_session::config::{BorrowckMode, CrateType, OutputFilenames};
use rustc_session::cstore::CrateStoreDyn;
use rustc_session::lint::{Level, Lint};
use rustc_session::Limit;
use rustc_session::Session;
Expand Down Expand Up @@ -150,7 +151,8 @@ impl<'tcx> CtxtInterners<'tcx> {
&self,
kind: TyKind<'tcx>,
sess: &Session,
resolutions: &ty::ResolverOutputs,
definitions: &rustc_hir::definitions::Definitions,
cstore: &CrateStoreDyn,
) -> Ty<'tcx> {
Ty(Interned::new_unchecked(
self.type_
Expand All @@ -165,11 +167,7 @@ impl<'tcx> CtxtInterners<'tcx> {
Fingerprint::ZERO
} else {
let mut hasher = StableHasher::new();
let mut hcx = StableHashingContext::ignore_spans(
sess,
&resolutions.definitions,
&*resolutions.cstore,
);
let mut hcx = StableHashingContext::ignore_spans(sess, definitions, cstore);
kind.hash_stable(&mut hcx, &mut hasher);
hasher.finish()
};
Expand Down Expand Up @@ -900,9 +898,10 @@ impl<'tcx> CommonTypes<'tcx> {
fn new(
interners: &CtxtInterners<'tcx>,
sess: &Session,
resolutions: &ty::ResolverOutputs,
definitions: &rustc_hir::definitions::Definitions,
cstore: &CrateStoreDyn,
) -> CommonTypes<'tcx> {
let mk = |ty| interners.intern_ty(ty, sess, resolutions);
let mk = |ty| interners.intern_ty(ty, sess, definitions, cstore);

CommonTypes {
unit: mk(Tuple(List::empty())),
Expand Down Expand Up @@ -1023,6 +1022,9 @@ pub struct GlobalCtxt<'tcx> {
/// Common consts, pre-interned for your convenience.
pub consts: CommonConsts<'tcx>,

definitions: rustc_hir::definitions::Definitions,
cstore: Box<CrateStoreDyn>,

/// Output of the resolver.
pub(crate) untracked_resolutions: ty::ResolverOutputs,

Expand Down Expand Up @@ -1163,7 +1165,9 @@ impl<'tcx> TyCtxt<'tcx> {
s: &'tcx Session,
lint_store: Lrc<dyn Any + sync::Send + sync::Sync>,
arena: &'tcx WorkerLocal<Arena<'tcx>>,
resolutions: ty::ResolverOutputs,
definitions: rustc_hir::definitions::Definitions,
cstore: Box<CrateStoreDyn>,
untracked_resolutions: ty::ResolverOutputs,
krate: &'tcx hir::Crate<'tcx>,
dep_graph: DepGraph,
on_disk_cache: Option<&'tcx dyn OnDiskCache<'tcx>>,
Expand All @@ -1176,7 +1180,7 @@ impl<'tcx> TyCtxt<'tcx> {
s.fatal(&err);
});
let interners = CtxtInterners::new(arena);
let common_types = CommonTypes::new(&interners, s, &resolutions);
let common_types = CommonTypes::new(&interners, s, &definitions, &*cstore);
let common_lifetimes = CommonLifetimes::new(&interners);
let common_consts = CommonConsts::new(&interners, &common_types);

Expand All @@ -1186,7 +1190,9 @@ impl<'tcx> TyCtxt<'tcx> {
arena,
interners,
dep_graph,
untracked_resolutions: resolutions,
definitions,
cstore,
untracked_resolutions,
prof: s.prof.clone(),
types: common_types,
lifetimes: common_lifetimes,
Expand Down Expand Up @@ -1287,9 +1293,9 @@ impl<'tcx> TyCtxt<'tcx> {
pub fn def_key(self, id: DefId) -> rustc_hir::definitions::DefKey {
// Accessing the DefKey is ok, since it is part of DefPathHash.
if let Some(id) = id.as_local() {
self.untracked_resolutions.definitions.def_key(id)
self.definitions.def_key(id)
} else {
self.untracked_resolutions.cstore.def_key(id)
self.cstore.def_key(id)
}
}

Expand All @@ -1301,19 +1307,19 @@ impl<'tcx> TyCtxt<'tcx> {
pub fn def_path(self, id: DefId) -> rustc_hir::definitions::DefPath {
// Accessing the DefPath is ok, since it is part of DefPathHash.
if let Some(id) = id.as_local() {
self.untracked_resolutions.definitions.def_path(id)
self.definitions.def_path(id)
} else {
self.untracked_resolutions.cstore.def_path(id)
self.cstore.def_path(id)
}
}

#[inline]
pub fn def_path_hash(self, def_id: DefId) -> rustc_hir::definitions::DefPathHash {
// Accessing the DefPathHash is ok, it is incr. comp. stable.
if let Some(def_id) = def_id.as_local() {
self.untracked_resolutions.definitions.def_path_hash(def_id)
self.definitions.def_path_hash(def_id)
} else {
self.untracked_resolutions.cstore.def_path_hash(def_id)
self.cstore.def_path_hash(def_id)
}
}

Expand All @@ -1322,7 +1328,7 @@ impl<'tcx> TyCtxt<'tcx> {
if crate_num == LOCAL_CRATE {
self.sess.local_stable_crate_id()
} else {
self.untracked_resolutions.cstore.stable_crate_id(crate_num)
self.cstore.stable_crate_id(crate_num)
}
}

Expand All @@ -1333,7 +1339,7 @@ impl<'tcx> TyCtxt<'tcx> {
if stable_crate_id == self.sess.local_stable_crate_id() {
LOCAL_CRATE
} else {
self.untracked_resolutions.cstore.stable_crate_id_to_crate_num(stable_crate_id)
self.cstore.stable_crate_id_to_crate_num(stable_crate_id)
}
}

Expand All @@ -1348,16 +1354,12 @@ impl<'tcx> TyCtxt<'tcx> {
// If this is a DefPathHash from the local crate, we can look up the
// DefId in the tcx's `Definitions`.
if stable_crate_id == self.sess.local_stable_crate_id() {
self.untracked_resolutions
.definitions
.local_def_path_hash_to_def_id(hash, err)
.to_def_id()
self.definitions.local_def_path_hash_to_def_id(hash, err).to_def_id()
} else {
// If this is a DefPathHash from an upstream crate, let the CrateStore map
// it to a DefId.
let cstore = &self.untracked_resolutions.cstore;
let cnum = cstore.stable_crate_id_to_crate_num(stable_crate_id);
cstore.def_path_hash_to_def_id(cnum, hash)
let cnum = self.cstore.stable_crate_id_to_crate_num(stable_crate_id);
self.cstore.def_path_hash_to_def_id(cnum, hash)
}
}

Expand All @@ -1369,7 +1371,7 @@ impl<'tcx> TyCtxt<'tcx> {
let (crate_name, stable_crate_id) = if def_id.is_local() {
(self.crate_name, self.sess.local_stable_crate_id())
} else {
let cstore = &self.untracked_resolutions.cstore;
let cstore = &self.cstore;
(cstore.crate_name(def_id.krate), cstore.stable_crate_id(def_id.krate))
};

Expand All @@ -1385,30 +1387,24 @@ impl<'tcx> TyCtxt<'tcx> {

/// Note that this is *untracked* and should only be used within the query
/// system if the result is otherwise tracked through queries
pub fn cstore_untracked(self) -> &'tcx ty::CrateStoreDyn {
&*self.untracked_resolutions.cstore
pub fn cstore_untracked(self) -> &'tcx CrateStoreDyn {
&*self.cstore
}

/// Note that this is *untracked* and should only be used within the query
/// system if the result is otherwise tracked through queries
pub fn definitions_untracked(self) -> &'tcx hir::definitions::Definitions {
&self.untracked_resolutions.definitions
&self.definitions
}

#[inline(always)]
pub fn create_stable_hashing_context(self) -> StableHashingContext<'tcx> {
let resolutions = &self.gcx.untracked_resolutions;
StableHashingContext::new(self.sess, &resolutions.definitions, &*resolutions.cstore)
StableHashingContext::new(self.sess, &self.definitions, &*self.cstore)
}

#[inline(always)]
pub fn create_no_span_stable_hashing_context(self) -> StableHashingContext<'tcx> {
let resolutions = &self.gcx.untracked_resolutions;
StableHashingContext::ignore_spans(
self.sess,
&resolutions.definitions,
&*resolutions.cstore,
)
StableHashingContext::ignore_spans(self.sess, &self.definitions, &*self.cstore)
}

pub fn serialize_query_result_cache(self, encoder: &mut FileEncoder) -> FileEncodeResult {
Expand Down Expand Up @@ -2237,7 +2233,7 @@ impl<'tcx> TyCtxt<'tcx> {
#[allow(rustc::usage_of_ty_tykind)]
#[inline]
pub fn mk_ty(self, st: TyKind<'tcx>) -> Ty<'tcx> {
self.interners.intern_ty(st, self.sess, &self.gcx.untracked_resolutions)
self.interners.intern_ty(st, self.sess, &self.definitions, &*self.cstore)
}

#[inline]
Expand Down
3 changes: 0 additions & 3 deletions compiler/rustc_middle/src/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LocalDefIdMap};
use rustc_hir::Node;
use rustc_macros::HashStable;
use rustc_query_system::ich::StableHashingContext;
use rustc_session::cstore::CrateStoreDyn;
use rustc_span::symbol::{kw, sym, Ident, Symbol};
use rustc_span::Span;
use rustc_target::abi::Align;
Expand Down Expand Up @@ -128,8 +127,6 @@ pub type RegisteredTools = FxHashSet<Ident>;

#[derive(Debug)]
pub struct ResolverOutputs {
pub definitions: rustc_hir::definitions::Definitions,
pub cstore: Box<CrateStoreDyn>,
pub visibilities: FxHashMap<LocalDefId, Visibility>,
/// This field is used to decide whether we should make `PRIVATE_IN_PUBLIC` a hard error.
pub has_pub_restricted: bool,
Expand Down
Loading
0