From a552936ce73a3da9acef2d63587b47d4a2b1e1f0 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Wed, 23 Feb 2022 18:52:19 +0100 Subject: [PATCH 1/2] Include source file hash in crate_hash. --- compiler/rustc_middle/src/hir/map/mod.rs | 57 +++++++----------------- 1 file changed, 17 insertions(+), 40 deletions(-) diff --git a/compiler/rustc_middle/src/hir/map/mod.rs b/compiler/rustc_middle/src/hir/map/mod.rs index f36847c778109..c74a6ef0e8ac3 100644 --- a/compiler/rustc_middle/src/hir/map/mod.rs +++ b/compiler/rustc_middle/src/hir/map/mod.rs @@ -13,7 +13,6 @@ use rustc_hir::itemlikevisit::ItemLikeVisitor; use rustc_hir::*; use rustc_index::vec::Idx; use rustc_middle::hir::nested_filter; -use rustc_span::def_id::StableCrateId; use rustc_span::hygiene::MacroKind; use rustc_span::source_map::Spanned; use rustc_span::symbol::{kw, sym, Ident, Symbol}; @@ -1091,56 +1090,21 @@ impl<'hir> intravisit::Map<'hir> for Map<'hir> { pub(super) fn crate_hash(tcx: TyCtxt<'_>, crate_num: CrateNum) -> Svh { debug_assert_eq!(crate_num, LOCAL_CRATE); - let krate = tcx.hir_crate(()); - let hir_body_hash = krate.hir_hash; - - let upstream_crates = upstream_crates(tcx); - // We hash the final, remapped names of all local source files so we // don't have to include the path prefix remapping commandline args. // If we included the full mapping in the SVH, we could only have // reproducible builds by compiling from the same directory. So we just // hash the result of the mapping instead of the mapping itself. - let mut source_file_names: Vec<_> = tcx + let mut source_file_hashes: Vec<_> = tcx .sess .source_map() .files() .iter() .filter(|source_file| source_file.cnum == LOCAL_CRATE) - .map(|source_file| source_file.name_hash) + .map(|source_file| (source_file.name_hash, source_file.src_hash)) .collect(); + source_file_hashes.sort_unstable_by_key(|&(name_hash, _)| name_hash); - 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.untracked_resolutions.definitions; - 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); - - let crate_hash: Fingerprint = stable_hasher.finish(); - Svh::new(crate_hash.to_smaller_hash()) -} - -fn upstream_crates(tcx: TyCtxt<'_>) -> Vec<(StableCrateId, Svh)> { let mut upstream_crates: Vec<_> = tcx .crates(()) .iter() @@ -1151,7 +1115,20 @@ fn upstream_crates(tcx: TyCtxt<'_>) -> Vec<(StableCrateId, Svh)> { }) .collect(); upstream_crates.sort_unstable_by_key(|&(stable_crate_id, _)| stable_crate_id); - upstream_crates + + let mut cfg_opts: Vec<_> = tcx.sess.parse_sess.config.iter().collect(); + cfg_opts.sort_unstable_by_key(|&(symbol, _)| symbol.as_str()); + + let mut hcx = tcx.create_stable_hashing_context(); + let mut stable_hasher = StableHasher::new(); + source_file_hashes.hash_stable(&mut hcx, &mut stable_hasher); + cfg_opts.hash_stable(&mut hcx, &mut stable_hasher); + upstream_crates.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); + + let crate_hash: Fingerprint = stable_hasher.finish(); + Svh::new(crate_hash.to_smaller_hash()) } fn hir_id_to_string(map: Map<'_>, id: HirId) -> String { From 0ab9685a6b2a7178611390489281e9efc475933d Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Thu, 24 Feb 2022 17:54:09 +0100 Subject: [PATCH 2/2] Reintroduce HIR into the mix. --- compiler/rustc_middle/src/hir/map/mod.rs | 26 +++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_middle/src/hir/map/mod.rs b/compiler/rustc_middle/src/hir/map/mod.rs index c74a6ef0e8ac3..78f3e60bfd5c8 100644 --- a/compiler/rustc_middle/src/hir/map/mod.rs +++ b/compiler/rustc_middle/src/hir/map/mod.rs @@ -1090,6 +1090,9 @@ impl<'hir> intravisit::Map<'hir> for Map<'hir> { pub(super) fn crate_hash(tcx: TyCtxt<'_>, crate_num: CrateNum) -> Svh { debug_assert_eq!(crate_num, LOCAL_CRATE); + let krate = tcx.hir_crate(()); + let hir_body_hash = krate.hir_hash; + // We hash the final, remapped names of all local source files so we // don't have to include the path prefix remapping commandline args. // If we included the full mapping in the SVH, we could only have @@ -1116,14 +1119,27 @@ pub(super) fn crate_hash(tcx: TyCtxt<'_>, crate_num: CrateNum) -> Svh { .collect(); upstream_crates.sort_unstable_by_key(|&(stable_crate_id, _)| stable_crate_id); - let mut cfg_opts: Vec<_> = tcx.sess.parse_sess.config.iter().collect(); - cfg_opts.sort_unstable_by_key(|&(symbol, _)| symbol.as_str()); - let mut hcx = tcx.create_stable_hashing_context(); let mut stable_hasher = StableHasher::new(); - source_file_hashes.hash_stable(&mut hcx, &mut stable_hasher); - cfg_opts.hash_stable(&mut hcx, &mut stable_hasher); + hir_body_hash.hash_stable(&mut hcx, &mut stable_hasher); upstream_crates.hash_stable(&mut hcx, &mut stable_hasher); + source_file_hashes.hash_stable(&mut hcx, &mut stable_hasher); + if tcx.sess.opts.debugging_opts.incremental_relative_spans { + let definitions = &tcx.untracked_resolutions.definitions; + 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);