Skip to content

Commit

Permalink
Auto merge of #45476 - Xanewok:fingerprint-disambiguator, r=michaelwo…
Browse files Browse the repository at this point in the history
…erister

Use 128 bit instead of Symbol for crate disambiguator

As discussed on gitter, this changes `crate_disambiguator` from Strings to what they are represented as, a 128 bit number.

There's also one bit I think also needs to change, but wasn't 100% sure how: [create_root_def](https://github.com/rust-lang/rust/blob/f338dba29705e144bad8b2a675284538dd514896/src/librustc/hir/map/definitions.rs#L468-L482). Should I change `DefKey::root_parent_stable_hash` to accept `Fingerprint` as crate_disambiguator to quickly combine the hash of `crate_name` with the new 128 bit hash instead of a string for a disambiguator?

r? @michaelwoerister

EDIT: Are those 3 tests `mir-opt` failing, because the hash is different, because we calculate it a little bit differently (storing directly instead of hashing the hex-string representation)? Should it be updated like in #45319?
  • Loading branch information
bors committed Oct 25, 2017
2 parents b247805 + 7fa64bc commit f764eaf
Show file tree
Hide file tree
Showing 22 changed files with 94 additions and 65 deletions.
5 changes: 3 additions & 2 deletions src/librustc/hir/map/collector.rs
Expand Up @@ -12,6 +12,7 @@ use super::*;

use dep_graph::{DepGraph, DepKind, DepNodeIndex};
use hir::intravisit::{Visitor, NestedVisitorMap};
use session::CrateDisambiguator;
use std::iter::repeat;
use syntax::ast::{NodeId, CRATE_NODE_ID};
use syntax_pos::Span;
Expand Down Expand Up @@ -118,7 +119,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
}

pub(super) fn finalize_and_compute_crate_hash(self,
crate_disambiguator: &str)
crate_disambiguator: CrateDisambiguator)
-> Vec<MapEntry<'hir>> {
let mut node_hashes: Vec<_> = self
.hir_body_nodes
Expand All @@ -133,7 +134,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {

self.dep_graph.with_task(DepNode::new_no_params(DepKind::Krate),
&self.hcx,
(node_hashes, crate_disambiguator),
(node_hashes, crate_disambiguator.to_fingerprint()),
identity_fn);
self.map
}
Expand Down
5 changes: 4 additions & 1 deletion src/librustc/hir/map/def_collector.rs
Expand Up @@ -10,6 +10,7 @@

use hir::map::definitions::*;
use hir::def_id::{CRATE_DEF_INDEX, DefIndex, DefIndexAddressSpace};
use session::CrateDisambiguator;

use syntax::ast::*;
use syntax::ext::hygiene::Mark;
Expand Down Expand Up @@ -43,7 +44,9 @@ impl<'a> DefCollector<'a> {
}
}

pub fn collect_root(&mut self, crate_name: &str, crate_disambiguator: &str) {
pub fn collect_root(&mut self,
crate_name: &str,
crate_disambiguator: CrateDisambiguator) {
let root = self.definitions.create_root_def(crate_name,
crate_disambiguator);
assert_eq!(root, CRATE_DEF_INDEX);
Expand Down
7 changes: 5 additions & 2 deletions src/librustc/hir/map/definitions.rs
Expand Up @@ -22,6 +22,7 @@ use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::indexed_vec::IndexVec;
use rustc_data_structures::stable_hasher::StableHasher;
use serialize::{Encodable, Decodable, Encoder, Decoder};
use session::CrateDisambiguator;
use std::fmt::Write;
use std::hash::Hash;
use syntax::ast;
Expand Down Expand Up @@ -231,7 +232,9 @@ impl DefKey {
DefPathHash(hasher.finish())
}

fn root_parent_stable_hash(crate_name: &str, crate_disambiguator: &str) -> DefPathHash {
fn root_parent_stable_hash(crate_name: &str,
crate_disambiguator: CrateDisambiguator)
-> DefPathHash {
let mut hasher = StableHasher::new();
// Disambiguate this from a regular DefPath hash,
// see compute_stable_hash() above.
Expand Down Expand Up @@ -467,7 +470,7 @@ impl Definitions {
/// Add a definition with a parent definition.
pub fn create_root_def(&mut self,
crate_name: &str,
crate_disambiguator: &str)
crate_disambiguator: CrateDisambiguator)
-> DefIndex {
let key = DefKey {
parent: None,
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/hir/map/mod.rs
Expand Up @@ -1014,8 +1014,8 @@ pub fn map_crate<'hir>(sess: &::session::Session,
hcx);
intravisit::walk_crate(&mut collector, &forest.krate);

let crate_disambiguator = sess.local_crate_disambiguator().as_str();
collector.finalize_and_compute_crate_hash(&crate_disambiguator)
let crate_disambiguator = sess.local_crate_disambiguator();
collector.finalize_and_compute_crate_hash(crate_disambiguator)
};

if log_enabled!(::log::LogLevel::Debug) {
Expand Down
6 changes: 3 additions & 3 deletions src/librustc/middle/cstore.rs
Expand Up @@ -30,7 +30,7 @@ use hir::map::definitions::{Definitions, DefKey, DefPathTable};
use hir::svh::Svh;
use ich;
use ty::{self, TyCtxt};
use session::Session;
use session::{Session, CrateDisambiguator};
use session::search_paths::PathKind;
use util::nodemap::NodeSet;

Expand Down Expand Up @@ -267,7 +267,7 @@ pub trait CrateStore {
fn export_macros_untracked(&self, cnum: CrateNum);
fn dep_kind_untracked(&self, cnum: CrateNum) -> DepKind;
fn crate_name_untracked(&self, cnum: CrateNum) -> Symbol;
fn crate_disambiguator_untracked(&self, cnum: CrateNum) -> Symbol;
fn crate_disambiguator_untracked(&self, cnum: CrateNum) -> CrateDisambiguator;
fn crate_hash_untracked(&self, cnum: CrateNum) -> Svh;
fn struct_field_names_untracked(&self, def: DefId) -> Vec<ast::Name>;
fn item_children_untracked(&self, did: DefId, sess: &Session) -> Vec<def::Export>;
Expand Down Expand Up @@ -338,7 +338,7 @@ impl CrateStore for DummyCrateStore {
fn dep_kind_untracked(&self, cnum: CrateNum) -> DepKind { bug!("is_explicitly_linked") }
fn export_macros_untracked(&self, cnum: CrateNum) { bug!("export_macros") }
fn crate_name_untracked(&self, cnum: CrateNum) -> Symbol { bug!("crate_name") }
fn crate_disambiguator_untracked(&self, cnum: CrateNum) -> Symbol {
fn crate_disambiguator_untracked(&self, cnum: CrateNum) -> CrateDisambiguator {
bug!("crate_disambiguator")
}
fn crate_hash_untracked(&self, cnum: CrateNum) -> Svh { bug!("crate_hash") }
Expand Down
42 changes: 33 additions & 9 deletions src/librustc/session/mod.rs
Expand Up @@ -12,6 +12,7 @@ pub use self::code_stats::{CodeStats, DataTypeKind, FieldInfo};
pub use self::code_stats::{SizeKind, TypeSizeInfo, VariantInfo};

use hir::def_id::{CrateNum, DefIndex};
use ich::Fingerprint;

use lint;
use middle::allocator::AllocatorKind;
Expand All @@ -29,7 +30,6 @@ use syntax::json::JsonEmitter;
use syntax::feature_gate;
use syntax::parse;
use syntax::parse::ParseSess;
use syntax::symbol::Symbol;
use syntax::{ast, codemap};
use syntax::feature_gate::AttributeType;
use syntax_pos::{Span, MultiSpan};
Expand Down Expand Up @@ -83,12 +83,12 @@ pub struct Session {
pub plugin_attributes: RefCell<Vec<(String, AttributeType)>>,
pub crate_types: RefCell<Vec<config::CrateType>>,
pub dependency_formats: RefCell<dependency_format::Dependencies>,
/// The crate_disambiguator is constructed out of all the `-C metadata`
/// The crate_disambiguator is constructed out of all the `-C metadata`
/// arguments passed to the compiler. Its value together with the crate-name
/// forms a unique global identifier for the crate. It is used to allow
/// multiple crates with the same name to coexist. See the
/// trans::back::symbol_names module for more information.
pub crate_disambiguator: RefCell<Option<Symbol>>,
pub crate_disambiguator: RefCell<Option<CrateDisambiguator>>,
pub features: RefCell<feature_gate::Features>,

/// The maximum recursion limit for potentially infinitely recursive
Expand Down Expand Up @@ -165,9 +165,9 @@ enum DiagnosticBuilderMethod {
}

impl Session {
pub fn local_crate_disambiguator(&self) -> Symbol {
pub fn local_crate_disambiguator(&self) -> CrateDisambiguator {
match *self.crate_disambiguator.borrow() {
Some(sym) => sym,
Some(value) => value,
None => bug!("accessing disambiguator before initialization"),
}
}
Expand Down Expand Up @@ -471,14 +471,18 @@ impl Session {

/// Returns the symbol name for the registrar function,
/// given the crate Svh and the function DefIndex.
pub fn generate_plugin_registrar_symbol(&self, disambiguator: Symbol, index: DefIndex)
pub fn generate_plugin_registrar_symbol(&self, disambiguator: CrateDisambiguator,
index: DefIndex)
-> String {
format!("__rustc_plugin_registrar__{}_{}", disambiguator, index.as_usize())
format!("__rustc_plugin_registrar__{}_{}", disambiguator.to_fingerprint().to_hex(),
index.as_usize())
}

pub fn generate_derive_registrar_symbol(&self, disambiguator: Symbol, index: DefIndex)
pub fn generate_derive_registrar_symbol(&self, disambiguator: CrateDisambiguator,
index: DefIndex)
-> String {
format!("__rustc_derive_registrar__{}_{}", disambiguator, index.as_usize())
format!("__rustc_derive_registrar__{}_{}", disambiguator.to_fingerprint().to_hex(),
index.as_usize())
}

pub fn sysroot<'a>(&'a self) -> &'a Path {
Expand Down Expand Up @@ -838,6 +842,26 @@ pub fn build_session_(sopts: config::Options,
sess
}

/// Hash value constructed out of all the `-C metadata` arguments passed to the
/// compiler. Together with the crate-name forms a unique global identifier for
/// the crate.
#[derive(Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Clone, Copy, RustcEncodable, RustcDecodable)]
pub struct CrateDisambiguator(Fingerprint);

impl CrateDisambiguator {
pub fn to_fingerprint(self) -> Fingerprint {
self.0
}
}

impl From<Fingerprint> for CrateDisambiguator {
fn from(fingerprint: Fingerprint) -> CrateDisambiguator {
CrateDisambiguator(fingerprint)
}
}

impl_stable_hash_for!(tuple_struct CrateDisambiguator { fingerprint });

/// Holds data on the current incremental compilation session, if there is one.
#[derive(Debug)]
pub enum IncrCompSession {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/ty/context.rs
Expand Up @@ -1249,7 +1249,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
crate_name,
// Don't print the whole crate disambiguator. That's just
// annoying in debug output.
&(crate_disambiguator.as_str())[..4],
&(crate_disambiguator.to_fingerprint().to_hex())[..4],
self.def_path(def_id).to_string_no_crate())
}

Expand Down
4 changes: 2 additions & 2 deletions src/librustc/ty/maps/mod.rs
Expand Up @@ -29,7 +29,7 @@ use middle::lang_items::{LanguageItems, LangItem};
use middle::exported_symbols::SymbolExportLevel;
use middle::trans::{CodegenUnit, Stats};
use mir;
use session::CompileResult;
use session::{CompileResult, CrateDisambiguator};
use session::config::OutputFilenames;
use traits::Vtable;
use traits::specialization_graph;
Expand Down Expand Up @@ -285,7 +285,7 @@ define_maps! { <'tcx>
[] fn native_libraries: NativeLibraries(CrateNum) -> Rc<Vec<NativeLibrary>>,
[] fn plugin_registrar_fn: PluginRegistrarFn(CrateNum) -> Option<DefId>,
[] fn derive_registrar_fn: DeriveRegistrarFn(CrateNum) -> Option<DefId>,
[] fn crate_disambiguator: CrateDisambiguator(CrateNum) -> Symbol,
[] fn crate_disambiguator: CrateDisambiguator(CrateNum) -> CrateDisambiguator,
[] fn crate_hash: CrateHash(CrateNum) -> Svh,
[] fn original_crate_name: OriginalCrateName(CrateNum) -> Symbol,

Expand Down
3 changes: 2 additions & 1 deletion src/librustc/ty/mod.rs
Expand Up @@ -26,6 +26,7 @@ use middle::privacy::AccessLevels;
use middle::resolve_lifetime::ObjectLifetimeDefault;
use mir::Mir;
use mir::GeneratorLayout;
use session::CrateDisambiguator;
use traits;
use ty;
use ty::subst::{Subst, Substs};
Expand Down Expand Up @@ -2556,7 +2557,7 @@ fn param_env<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
}

fn crate_disambiguator<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
crate_num: CrateNum) -> Symbol {
crate_num: CrateNum) -> CrateDisambiguator {
assert_eq!(crate_num, LOCAL_CRATE);
tcx.sess.local_crate_disambiguator()
}
Expand Down
20 changes: 9 additions & 11 deletions src/librustc_driver/driver.rs
Expand Up @@ -14,7 +14,7 @@ use rustc::hir::lowering::lower_crate;
use rustc::ich::Fingerprint;
use rustc_data_structures::stable_hasher::StableHasher;
use rustc_mir as mir;
use rustc::session::{Session, CompileResult};
use rustc::session::{Session, CompileResult, CrateDisambiguator};
use rustc::session::CompileIncomplete;
use rustc::session::config::{self, Input, OutputFilenames, OutputType};
use rustc::session::search_paths::PathKind;
Expand Down Expand Up @@ -58,7 +58,6 @@ use syntax::{ast, diagnostics, visit};
use syntax::attr;
use syntax::ext::base::ExtCtxt;
use syntax::parse::{self, PResult};
use syntax::symbol::Symbol;
use syntax::util::node_count::NodeCounter;
use syntax;
use syntax_ext;
Expand Down Expand Up @@ -633,12 +632,12 @@ pub fn phase_2_configure_and_expand<F>(sess: &Session,

*sess.crate_types.borrow_mut() = collect_crate_types(sess, &krate.attrs);

let disambiguator = Symbol::intern(&compute_crate_disambiguator(sess));
let disambiguator = compute_crate_disambiguator(sess);
*sess.crate_disambiguator.borrow_mut() = Some(disambiguator);
rustc_incremental::prepare_session_directory(
sess,
&crate_name,
&disambiguator.as_str(),
disambiguator,
);

let dep_graph = if sess.opts.build_dep_graph() {
Expand Down Expand Up @@ -1312,16 +1311,13 @@ pub fn collect_crate_types(session: &Session, attrs: &[ast::Attribute]) -> Vec<c
.collect()
}

pub fn compute_crate_disambiguator(session: &Session) -> String {
pub fn compute_crate_disambiguator(session: &Session) -> CrateDisambiguator {
use std::hash::Hasher;

// The crate_disambiguator is a 128 bit hash. The disambiguator is fed
// into various other hashes quite a bit (symbol hashes, incr. comp. hashes,
// debuginfo type IDs, etc), so we don't want it to be too wide. 128 bits
// should still be safe enough to avoid collisions in practice.
// FIXME(mw): It seems that the crate_disambiguator is used everywhere as
// a hex-string instead of raw bytes. We should really use the
// smaller representation.
let mut hasher = StableHasher::<Fingerprint>::new();

let mut metadata = session.opts.cg.metadata.clone();
Expand All @@ -1340,11 +1336,13 @@ pub fn compute_crate_disambiguator(session: &Session) -> String {
hasher.write(s.as_bytes());
}

// If this is an executable, add a special suffix, so that we don't get
// symbol conflicts when linking against a library of the same name.
// Also incorporate crate type, so that we don't get symbol conflicts when
// linking against a library of the same name, if this is an executable.
let is_exe = session.crate_types.borrow().contains(&config::CrateTypeExecutable);
hasher.write(if is_exe { b"exe" } else { b"lib" });

CrateDisambiguator::from(hasher.finish())

format!("{}{}", hasher.finish().to_hex(), if is_exe { "-exe" } else {""})
}

pub fn build_output_filenames(input: &Input,
Expand Down
18 changes: 7 additions & 11 deletions src/librustc_incremental/persist/fs.rs
Expand Up @@ -115,7 +115,7 @@
//! implemented.

use rustc::hir::svh::Svh;
use rustc::session::Session;
use rustc::session::{Session, CrateDisambiguator};
use rustc::util::fs as fs_util;
use rustc_data_structures::{flock, base_n};
use rustc_data_structures::fx::{FxHashSet, FxHashMap};
Expand Down Expand Up @@ -188,7 +188,7 @@ pub fn in_incr_comp_dir(incr_comp_session_dir: &Path, file_name: &str) -> PathBu
/// The garbage collection will take care of it.
pub fn prepare_session_directory(sess: &Session,
crate_name: &str,
crate_disambiguator: &str) {
crate_disambiguator: CrateDisambiguator) {
if sess.opts.incremental.is_none() {
return
}
Expand Down Expand Up @@ -614,21 +614,17 @@ fn string_to_timestamp(s: &str) -> Result<SystemTime, ()> {

fn crate_path(sess: &Session,
crate_name: &str,
crate_disambiguator: &str)
crate_disambiguator: CrateDisambiguator)
-> PathBuf {
use std::hash::{Hasher, Hash};
use std::collections::hash_map::DefaultHasher;

let incr_dir = sess.opts.incremental.as_ref().unwrap().clone();

// The full crate disambiguator is really long. A hash of it should be
// The full crate disambiguator is really long. 64 bits of it should be
// sufficient.
let mut hasher = DefaultHasher::new();
crate_disambiguator.hash(&mut hasher);
let crate_disambiguator = crate_disambiguator.to_fingerprint().to_smaller_hash();
let crate_disambiguator = base_n::encode(crate_disambiguator, INT_ENCODE_BASE);

let crate_name = format!("{}-{}",
crate_name,
base_n::encode(hasher.finish(), INT_ENCODE_BASE));
let crate_name = format!("{}-{}", crate_name, crate_disambiguator);
incr_dir.join(crate_name)
}

Expand Down
4 changes: 2 additions & 2 deletions src/librustc_metadata/creader.rs
Expand Up @@ -19,7 +19,7 @@ use rustc::hir::def_id::{CrateNum, DefIndex, CRATE_DEF_INDEX};
use rustc::hir::svh::Svh;
use rustc::middle::allocator::AllocatorKind;
use rustc::middle::cstore::DepKind;
use rustc::session::Session;
use rustc::session::{Session, CrateDisambiguator};
use rustc::session::config::{Sanitizer, self};
use rustc_back::PanicStrategy;
use rustc::session::search_paths::PathKind;
Expand Down Expand Up @@ -626,7 +626,7 @@ impl<'a> CrateLoader<'a> {
pub fn find_plugin_registrar(&mut self,
span: Span,
name: &str)
-> Option<(PathBuf, Symbol, DefIndex)> {
-> Option<(PathBuf, CrateDisambiguator, DefIndex)> {
let ekrate = self.read_extension_crate(span, &ExternCrateInfo {
name: Symbol::intern(name),
ident: Symbol::intern(name),
Expand Down
3 changes: 2 additions & 1 deletion src/librustc_metadata/cstore.rs
Expand Up @@ -17,6 +17,7 @@ use rustc::hir::def_id::{CRATE_DEF_INDEX, CrateNum, DefIndex};
use rustc::hir::map::definitions::DefPathTable;
use rustc::hir::svh::Svh;
use rustc::middle::cstore::{DepKind, ExternCrate, MetadataLoader};
use rustc::session::CrateDisambiguator;
use rustc_back::PanicStrategy;
use rustc_data_structures::indexed_vec::IndexVec;
use rustc::util::nodemap::{FxHashMap, FxHashSet, NodeMap};
Expand Down Expand Up @@ -171,7 +172,7 @@ impl CrateMetadata {
pub fn hash(&self) -> Svh {
self.root.hash
}
pub fn disambiguator(&self) -> Symbol {
pub fn disambiguator(&self) -> CrateDisambiguator {
self.root.disambiguator
}

Expand Down

0 comments on commit f764eaf

Please sign in to comment.