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
Prev Previous commit
Next Next commit
Allow to create definitions inside the query system.
  • Loading branch information
cjgillot committed Apr 30, 2022
commit 24be7ce18e1416994d24bd4dbbdc2c79731e267c
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,9 @@ impl<'tcx> UniqueTypeId<'tcx> {
/// Right now this takes the form of a hex-encoded opaque hash value.
pub fn generate_unique_id_string(self, tcx: TyCtxt<'tcx>) -> String {
let mut hasher = StableHasher::new();
let mut hcx = tcx.create_stable_hashing_context();
hcx.while_hashing_spans(false, |hcx| self.hash_stable(hcx, &mut hasher));
tcx.with_stable_hashing_context(|mut hcx| {
hcx.while_hashing_spans(false, |hcx| self.hash_stable(hcx, &mut hasher))
});
hasher.finish::<Fingerprint>().to_hex()
}

Expand Down
10 changes: 6 additions & 4 deletions compiler/rustc_codegen_ssa/src/debuginfo/type_names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -701,12 +701,14 @@ fn push_const_param<'tcx>(tcx: TyCtxt<'tcx>, ct: ty::Const<'tcx>, output: &mut S
// If we cannot evaluate the constant to a known type, we fall back
// to emitting a stable hash value of the constant. This isn't very pretty
// but we get a deterministic, virtually unique value for the constant.
let hcx = &mut tcx.create_stable_hashing_context();
let mut hasher = StableHasher::new();
hcx.while_hashing_spans(false, |hcx| ct.val().hash_stable(hcx, &mut hasher));
//
// Let's only emit 64 bits of the hash value. That should be plenty for
// avoiding collisions and will make the emitted type names shorter.
let hash: u64 = hasher.finish();
let hash: u64 = tcx.with_stable_hashing_context(|mut hcx| {
let mut hasher = StableHasher::new();
hcx.while_hashing_spans(false, |hcx| ct.val().hash_stable(hcx, &mut hasher));
hasher.finish()
});

if cpp_like_debuginfo(tcx) {
write!(output, "CONST${:x}", hash)
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_data_structures/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#![feature(associated_type_bounds)]
#![feature(auto_traits)]
#![feature(bool_to_option)]
#![feature(cell_leak)]
#![feature(control_flow_enum)]
#![feature(core_intrinsics)]
#![feature(extend_one)]
Expand Down
27 changes: 27 additions & 0 deletions compiler/rustc_data_structures/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,33 @@ impl<T> RwLock<T> {
pub fn borrow_mut(&self) -> WriteGuard<'_, T> {
self.write()
}

#[cfg(not(parallel_compiler))]
#[inline(always)]
pub fn clone_guard<'a>(rg: &ReadGuard<'a, T>) -> ReadGuard<'a, T> {
ReadGuard::clone(rg)
}

#[cfg(parallel_compiler)]
#[inline(always)]
pub fn clone_guard<'a>(rg: &ReadGuard<'a, T>) -> ReadGuard<'a, T> {
ReadGuard::rwlock(&rg).read()
}

#[cfg(not(parallel_compiler))]
#[inline(always)]
pub fn leak(&self) -> &T {
ReadGuard::leak(self.read())
}

#[cfg(parallel_compiler)]
#[inline(always)]
pub fn leak(&self) -> &T {
let guard = self.read();
let ret = unsafe { &*(&*guard as *const T) };
std::mem::forget(guard);
ret
}
}

// FIXME: Probably a bad idea
Expand Down
23 changes: 11 additions & 12 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.definitions_untracked().def_path_table();
let table = self.tcx.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 @@ -475,9 +475,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
}

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

fn encode_source_map(&mut self) -> Lazy<[rustc_span::SourceFile]> {
Expand Down Expand Up @@ -663,7 +661,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
};

// Encode the proc macro data. This affects 'tables',
// so we need to do this before we encode the tables
// so we need to do this before we encode the tables.
// This overwrites def_keys, so it must happen after encode_def_path_table.
i = self.position();
let proc_macro_data = self.encode_proc_macros();
let proc_macro_data_bytes = self.position() - i;
Expand Down Expand Up @@ -990,8 +989,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
return;
}
let tcx = self.tcx;
let hir = tcx.hir();
for local_id in hir.iter_local_def_id() {
for local_id in tcx.iter_local_def_id() {
let def_id = local_id.to_def_id();
let def_kind = tcx.opt_def_kind(local_id);
let Some(def_kind) = def_kind else { continue };
Expand Down Expand Up @@ -1843,12 +1841,13 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
debug!("EncodeContext::encode_traits_and_impls()");
empty_proc_macro!(self);
let tcx = self.tcx;
let mut ctx = tcx.create_stable_hashing_context();
let mut all_impls: Vec<_> = tcx.crate_inherent_impls(()).incoherent_impls.iter().collect();
all_impls.sort_by_cached_key(|&(&simp, _)| {
let mut hasher = StableHasher::new();
simp.hash_stable(&mut ctx, &mut hasher);
hasher.finish::<Fingerprint>();
tcx.with_stable_hashing_context(|mut ctx| {
all_impls.sort_by_cached_key(|&(&simp, _)| {
let mut hasher = StableHasher::new();
simp.hash_stable(&mut ctx, &mut hasher);
hasher.finish::<Fingerprint>()
})
});
let all_impls: Vec<_> = all_impls
.into_iter()
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_middle/src/dep_graph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ impl<'tcx> DepContext for TyCtxt<'tcx> {
type DepKind = DepKind;

#[inline]
fn create_stable_hashing_context(&self) -> StableHashingContext<'_> {
TyCtxt::create_stable_hashing_context(*self)
fn with_stable_hashing_context<R>(&self, f: impl FnOnce(StableHashingContext<'_>) -> R) -> R {
TyCtxt::with_stable_hashing_context(*self, f)
}

#[inline]
Expand Down
66 changes: 30 additions & 36 deletions compiler/rustc_middle/src/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,13 +215,6 @@ impl<'hir> Map<'hir> {
self.tcx.local_def_id_to_hir_id(def_id)
}

pub fn iter_local_def_id(self) -> impl Iterator<Item = LocalDefId> + '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.definitions_untracked().iter_local_def_id()
}

pub fn opt_def_kind(self, local_def_id: LocalDefId) -> Option<DefKind> {
let hir_id = self.local_def_id_to_hir_id(local_def_id);
let def_kind = match self.find(hir_id)? {
Expand Down Expand Up @@ -1098,35 +1091,36 @@ pub(super) fn crate_hash(tcx: TyCtxt<'_>, crate_num: CrateNum) -> Svh {

source_file_names.sort_unstable();

let mut hcx = tcx.create_stable_hashing_context();
let mut stable_hasher = StableHasher::new();
hir_body_hash.hash_stable(&mut hcx, &mut stable_hasher);
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.definitions_untracked();
let mut owner_spans: Vec<_> = krate
.owners
.iter_enumerated()
.filter_map(|(def_id, info)| {
let _ = info.as_owner()?;
let def_path_hash = definitions.def_path_hash(def_id);
let span = definitions.def_span(def_id);
debug_assert_eq!(span.parent(), None);
Some((def_path_hash, span))
})
.collect();
owner_spans.sort_unstable_by_key(|bn| bn.0);
owner_spans.hash_stable(&mut hcx, &mut stable_hasher);
}
tcx.sess.opts.dep_tracking_hash(true).hash_stable(&mut hcx, &mut stable_hasher);
tcx.sess.local_stable_crate_id().hash_stable(&mut hcx, &mut stable_hasher);
// Hash visibility information since it does not appear in HIR.
let resolutions = tcx.resolutions(());
resolutions.visibilities.hash_stable(&mut hcx, &mut stable_hasher);
resolutions.has_pub_restricted.hash_stable(&mut hcx, &mut stable_hasher);

let crate_hash: Fingerprint = stable_hasher.finish();
let crate_hash: Fingerprint = tcx.with_stable_hashing_context(|mut hcx| {
let mut stable_hasher = StableHasher::new();
hir_body_hash.hash_stable(&mut hcx, &mut stable_hasher);
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.definitions_untracked();
let mut owner_spans: Vec<_> = krate
.owners
.iter_enumerated()
.filter_map(|(def_id, info)| {
let _ = info.as_owner()?;
let def_path_hash = definitions.def_path_hash(def_id);
let span = definitions.def_span(def_id);
debug_assert_eq!(span.parent(), None);
Some((def_path_hash, span))
})
.collect();
owner_spans.sort_unstable_by_key(|bn| bn.0);
owner_spans.hash_stable(&mut hcx, &mut stable_hasher);
}
tcx.sess.opts.dep_tracking_hash(true).hash_stable(&mut hcx, &mut stable_hasher);
tcx.sess.local_stable_crate_id().hash_stable(&mut hcx, &mut stable_hasher);
// Hash visibility information since it does not appear in HIR.
let resolutions = tcx.resolutions(());
resolutions.visibilities.hash_stable(&mut hcx, &mut stable_hasher);
resolutions.has_pub_restricted.hash_stable(&mut hcx, &mut stable_hasher);
stable_hasher.finish()
});

Svh::new(crate_hash.to_smaller_hash())
}

Expand Down
6 changes: 6 additions & 0 deletions compiler/rustc_middle/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ rustc_queries! {
desc { "trigger a delay span bug" }
}

/// Create a new definition within the incr. comp. engine.
query register_def(_: ty::RawLocalDefId) -> LocalDefId {
eval_always
desc { "register a DefId with the incr. comp. engine" }
}

query resolutions(_: ()) -> &'tcx ty::ResolverOutputs {
eval_always
no_hash
Expand Down
Loading
0