Use Symbol for codegen unit names.

This is a straightforward replacement except for two places where we
have to convert to `LocalInternedString` to get a stable sort.
This commit is contained in:
Nicholas Nethercote 2019-10-21 17:14:03 +11:00
parent dddacf1eb3
commit 2da7a9c0d9
11 changed files with 46 additions and 51 deletions

View file

@ -59,7 +59,7 @@ use crate::ich::{Fingerprint, StableHashingContext};
use rustc_data_structures::stable_hasher::{StableHasher, HashStable}; use rustc_data_structures::stable_hasher::{StableHasher, HashStable};
use std::fmt; use std::fmt;
use std::hash::Hash; use std::hash::Hash;
use syntax_pos::symbol::InternedString; use syntax_pos::symbol::Symbol;
use crate::traits; use crate::traits;
use crate::traits::query::{ use crate::traits::query::{
CanonicalProjectionGoal, CanonicalTyGoal, CanonicalTypeOpAscribeUserTypeGoal, CanonicalProjectionGoal, CanonicalTyGoal, CanonicalTypeOpAscribeUserTypeGoal,
@ -426,7 +426,7 @@ rustc_dep_node_append!([define_dep_nodes!][ <'tcx>
[anon] TraitSelect, [anon] TraitSelect,
[] CompileCodegenUnit(InternedString), [] CompileCodegenUnit(Symbol),
[eval_always] Analysis(CrateNum), [eval_always] Analysis(CrateNum),
]); ]);

View file

@ -1,6 +1,6 @@
use crate::hir::def_id::{DefId, CrateNum, LOCAL_CRATE}; use crate::hir::def_id::{DefId, CrateNum, LOCAL_CRATE};
use crate::hir::HirId; use crate::hir::HirId;
use syntax::symbol::{InternedString, Symbol}; use syntax::symbol::Symbol;
use syntax::attr::InlineAttr; use syntax::attr::InlineAttr;
use syntax::source_map::Span; use syntax::source_map::Span;
use crate::ty::{Instance, InstanceDef, TyCtxt, SymbolName, subst::InternalSubsts}; use crate::ty::{Instance, InstanceDef, TyCtxt, SymbolName, subst::InternalSubsts};
@ -246,7 +246,7 @@ pub struct CodegenUnit<'tcx> {
/// name be unique amongst **all** crates. Therefore, it should /// name be unique amongst **all** crates. Therefore, it should
/// contain something unique to this crate (e.g., a module path) /// contain something unique to this crate (e.g., a module path)
/// as well as the crate name and disambiguator. /// as well as the crate name and disambiguator.
name: InternedString, name: Symbol,
items: FxHashMap<MonoItem<'tcx>, (Linkage, Visibility)>, items: FxHashMap<MonoItem<'tcx>, (Linkage, Visibility)>,
size_estimate: Option<usize>, size_estimate: Option<usize>,
} }
@ -294,7 +294,7 @@ impl_stable_hash_for!(enum self::Visibility {
}); });
impl<'tcx> CodegenUnit<'tcx> { impl<'tcx> CodegenUnit<'tcx> {
pub fn new(name: InternedString) -> CodegenUnit<'tcx> { pub fn new(name: Symbol) -> CodegenUnit<'tcx> {
CodegenUnit { CodegenUnit {
name: name, name: name,
items: Default::default(), items: Default::default(),
@ -302,11 +302,11 @@ impl<'tcx> CodegenUnit<'tcx> {
} }
} }
pub fn name(&self) -> &InternedString { pub fn name(&self) -> Symbol {
&self.name self.name
} }
pub fn set_name(&mut self, name: InternedString) { pub fn set_name(&mut self, name: Symbol) {
self.name = name; self.name = name;
} }
@ -474,7 +474,7 @@ impl CodegenUnitNameBuilder<'tcx> {
cnum: CrateNum, cnum: CrateNum,
components: I, components: I,
special_suffix: Option<S>) special_suffix: Option<S>)
-> InternedString -> Symbol
where I: IntoIterator<Item=C>, where I: IntoIterator<Item=C>,
C: fmt::Display, C: fmt::Display,
S: fmt::Display, S: fmt::Display,
@ -487,7 +487,7 @@ impl CodegenUnitNameBuilder<'tcx> {
cgu_name cgu_name
} else { } else {
let cgu_name = &cgu_name.as_str()[..]; let cgu_name = &cgu_name.as_str()[..];
InternedString::intern(&CodegenUnit::mangle_name(cgu_name)) Symbol::intern(&CodegenUnit::mangle_name(cgu_name))
} }
} }
@ -497,7 +497,7 @@ impl CodegenUnitNameBuilder<'tcx> {
cnum: CrateNum, cnum: CrateNum,
components: I, components: I,
special_suffix: Option<S>) special_suffix: Option<S>)
-> InternedString -> Symbol
where I: IntoIterator<Item=C>, where I: IntoIterator<Item=C>,
C: fmt::Display, C: fmt::Display,
S: fmt::Display, S: fmt::Display,
@ -543,6 +543,6 @@ impl CodegenUnitNameBuilder<'tcx> {
write!(cgu_name, ".{}", special_suffix).unwrap(); write!(cgu_name, ".{}", special_suffix).unwrap();
} }
InternedString::intern(&cgu_name[..]) Symbol::intern(&cgu_name[..])
} }
} }

View file

@ -15,7 +15,7 @@ use crate::traits::query::{
}; };
use std::borrow::Cow; use std::borrow::Cow;
use syntax_pos::symbol::InternedString; use syntax_pos::symbol::Symbol;
// Each of these queries corresponds to a function pointer field in the // Each of these queries corresponds to a function pointer field in the
// `Providers` struct for requesting a value of that type, and a method // `Providers` struct for requesting a value of that type, and a method
@ -924,7 +924,7 @@ rustc_queries! {
desc { "collect_and_partition_mono_items" } desc { "collect_and_partition_mono_items" }
} }
query is_codegened_item(_: DefId) -> bool {} query is_codegened_item(_: DefId) -> bool {}
query codegen_unit(_: InternedString) -> Arc<CodegenUnit<'tcx>> { query codegen_unit(_: Symbol) -> Arc<CodegenUnit<'tcx>> {
no_force no_force
desc { "codegen_unit" } desc { "codegen_unit" }
} }

View file

@ -11,7 +11,7 @@ use crate::mir;
use std::fmt::Debug; use std::fmt::Debug;
use std::hash::Hash; use std::hash::Hash;
use syntax_pos::{Span, DUMMY_SP}; use syntax_pos::{Span, DUMMY_SP};
use syntax_pos::symbol::InternedString; use syntax_pos::symbol::Symbol;
/// The `Key` trait controls what types can legally be used as the key /// The `Key` trait controls what types can legally be used as the key
/// for a query. /// for a query.
@ -190,7 +190,7 @@ impl<'tcx> Key for traits::Environment<'tcx> {
} }
} }
impl Key for InternedString { impl Key for Symbol {
fn query_crate(&self) -> CrateNum { fn query_crate(&self) -> CrateNum {
LOCAL_CRATE LOCAL_CRATE
} }

View file

@ -55,7 +55,6 @@ use std::ops::Deref;
use std::sync::Arc; use std::sync::Arc;
use std::any::type_name; use std::any::type_name;
use syntax_pos::{Span, DUMMY_SP}; use syntax_pos::{Span, DUMMY_SP};
use syntax_pos::symbol::InternedString;
use syntax::attr; use syntax::attr;
use syntax::ast; use syntax::ast;
use syntax::feature_gate; use syntax::feature_gate;

View file

@ -36,7 +36,7 @@ use rustc_codegen_ssa::back::write::submit_codegened_module_to_llvm;
use std::ffi::CString; use std::ffi::CString;
use std::time::Instant; use std::time::Instant;
use syntax_pos::symbol::InternedString; use syntax_pos::symbol::Symbol;
use rustc::hir::CodegenFnAttrs; use rustc::hir::CodegenFnAttrs;
use crate::value::Value; use crate::value::Value;
@ -105,7 +105,7 @@ pub fn iter_globals(llmod: &'ll llvm::Module) -> ValueIter<'ll> {
pub fn compile_codegen_unit( pub fn compile_codegen_unit(
tcx: TyCtxt<'tcx>, tcx: TyCtxt<'tcx>,
cgu_name: InternedString, cgu_name: Symbol,
tx_to_llvm_workers: &std::sync::mpsc::Sender<Box<dyn std::any::Any + Send>>, tx_to_llvm_workers: &std::sync::mpsc::Sender<Box<dyn std::any::Any + Send>>,
) { ) {
let prof_timer = tcx.prof.generic_activity("codegen_module"); let prof_timer = tcx.prof.generic_activity("codegen_module");
@ -131,7 +131,7 @@ pub fn compile_codegen_unit(
fn module_codegen( fn module_codegen(
tcx: TyCtxt<'_>, tcx: TyCtxt<'_>,
cgu_name: InternedString, cgu_name: Symbol,
) -> ModuleCodegen<ModuleLlvm> { ) -> ModuleCodegen<ModuleLlvm> {
let cgu = tcx.codegen_unit(cgu_name); let cgu = tcx.codegen_unit(cgu_name);
// Instantiate monomorphizations without filling out definitions yet... // Instantiate monomorphizations without filling out definitions yet...

View file

@ -50,7 +50,6 @@ use rustc_codegen_ssa::CompiledModule;
use errors::{FatalError, Handler}; use errors::{FatalError, Handler};
use rustc::dep_graph::WorkProduct; use rustc::dep_graph::WorkProduct;
use syntax_expand::allocator::AllocatorKind; use syntax_expand::allocator::AllocatorKind;
use syntax_pos::symbol::InternedString;
pub use llvm_util::target_features; pub use llvm_util::target_features;
use std::any::Any; use std::any::Any;
use std::sync::Arc; use std::sync::Arc;
@ -123,7 +122,7 @@ impl ExtraBackendMethods for LlvmCodegenBackend {
} }
fn compile_codegen_unit( fn compile_codegen_unit(
&self, tcx: TyCtxt<'_>, &self, tcx: TyCtxt<'_>,
cgu_name: InternedString, cgu_name: Symbol,
tx: &std::sync::mpsc::Sender<Box<dyn Any + Send>>, tx: &std::sync::mpsc::Sender<Box<dyn Any + Send>>,
) { ) {
base::compile_codegen_unit(tcx, cgu_name, tx); base::compile_codegen_unit(tcx, cgu_name, tx);

View file

@ -515,7 +515,7 @@ pub fn codegen_crate<B: ExtraBackendMethods>(
// unnecessarily. // unnecessarily.
if tcx.dep_graph.is_fully_enabled() { if tcx.dep_graph.is_fully_enabled() {
for cgu in &codegen_units { for cgu in &codegen_units {
tcx.codegen_unit(cgu.name().clone()); tcx.codegen_unit(cgu.name());
} }
} }
@ -603,7 +603,7 @@ pub fn codegen_crate<B: ExtraBackendMethods>(
match cgu_reuse { match cgu_reuse {
CguReuse::No => { CguReuse::No => {
let start_time = Instant::now(); let start_time = Instant::now();
backend.compile_codegen_unit(tcx, *cgu.name(), &ongoing_codegen.coordinator_send); backend.compile_codegen_unit(tcx, cgu.name(), &ongoing_codegen.coordinator_send);
total_codegen_time += start_time.elapsed(); total_codegen_time += start_time.elapsed();
false false
} }

View file

@ -10,7 +10,7 @@ use rustc_codegen_utils::codegen_backend::CodegenBackend;
use std::sync::Arc; use std::sync::Arc;
use std::sync::mpsc; use std::sync::mpsc;
use syntax_expand::allocator::AllocatorKind; use syntax_expand::allocator::AllocatorKind;
use syntax_pos::symbol::InternedString; use syntax_pos::symbol::Symbol;
pub trait BackendTypes { pub trait BackendTypes {
type Value: CodegenObject; type Value: CodegenObject;
@ -50,7 +50,7 @@ pub trait ExtraBackendMethods: CodegenBackend + WriteBackendMethods + Sized + Se
fn compile_codegen_unit( fn compile_codegen_unit(
&self, &self,
tcx: TyCtxt<'_>, tcx: TyCtxt<'_>,
cgu_name: InternedString, cgu_name: Symbol,
tx_to_llvm_workers: &mpsc::Sender<Box<dyn std::any::Any + Send>>, tx_to_llvm_workers: &mpsc::Sender<Box<dyn std::any::Any + Send>>,
); );
// If find_features is true this won't access `sess.crate_types` by assuming // If find_features is true this won't access `sess.crate_types` by assuming

View file

@ -27,7 +27,7 @@ use rustc::mir::mono::CodegenUnitNameBuilder;
use rustc::ty::TyCtxt; use rustc::ty::TyCtxt;
use std::collections::BTreeSet; use std::collections::BTreeSet;
use syntax::ast; use syntax::ast;
use syntax::symbol::{InternedString, Symbol, sym}; use syntax::symbol::{Symbol, sym};
use rustc::ich::{ATTR_PARTITION_REUSED, ATTR_PARTITION_CODEGENED, use rustc::ich::{ATTR_PARTITION_REUSED, ATTR_PARTITION_CODEGENED,
ATTR_EXPECTED_CGU_REUSE}; ATTR_EXPECTED_CGU_REUSE};
@ -45,8 +45,8 @@ pub fn assert_module_sources(tcx: TyCtxt<'_>) {
.collect_and_partition_mono_items(LOCAL_CRATE) .collect_and_partition_mono_items(LOCAL_CRATE)
.1 .1
.iter() .iter()
.map(|cgu| *cgu.name()) .map(|cgu| cgu.name())
.collect::<BTreeSet<InternedString>>(); .collect::<BTreeSet<Symbol>>();
let ams = AssertModuleSource { let ams = AssertModuleSource {
tcx, tcx,
@ -61,7 +61,7 @@ pub fn assert_module_sources(tcx: TyCtxt<'_>) {
struct AssertModuleSource<'tcx> { struct AssertModuleSource<'tcx> {
tcx: TyCtxt<'tcx>, tcx: TyCtxt<'tcx>,
available_cgus: BTreeSet<InternedString>, available_cgus: BTreeSet<Symbol>,
} }
impl AssertModuleSource<'tcx> { impl AssertModuleSource<'tcx> {

View file

@ -96,7 +96,7 @@ use std::collections::hash_map::Entry;
use std::cmp; use std::cmp;
use std::sync::Arc; use std::sync::Arc;
use syntax::symbol::InternedString; use syntax::symbol::Symbol;
use rustc::hir::CodegenFnAttrFlags; use rustc::hir::CodegenFnAttrFlags;
use rustc::hir::def::DefKind; use rustc::hir::def::DefKind;
use rustc::hir::def_id::{CrateNum, DefId, LOCAL_CRATE, CRATE_DEF_INDEX}; use rustc::hir::def_id::{CrateNum, DefId, LOCAL_CRATE, CRATE_DEF_INDEX};
@ -121,7 +121,7 @@ pub enum PartitioningStrategy {
} }
// Anything we can't find a proper codegen unit for goes into this. // Anything we can't find a proper codegen unit for goes into this.
fn fallback_cgu_name(name_builder: &mut CodegenUnitNameBuilder<'_>) -> InternedString { fn fallback_cgu_name(name_builder: &mut CodegenUnitNameBuilder<'_>) -> Symbol {
name_builder.build_cgu_name(LOCAL_CRATE, &["fallback"], Some("cgu")) name_builder.build_cgu_name(LOCAL_CRATE, &["fallback"], Some("cgu"))
} }
@ -185,9 +185,7 @@ where
internalization_candidates: _, internalization_candidates: _,
} = post_inlining; } = post_inlining;
result.sort_by(|cgu1, cgu2| { result.sort_by_cached_key(|cgu| cgu.name().as_str());
cgu1.name().cmp(cgu2.name())
});
result result
} }
@ -203,7 +201,7 @@ struct PreInliningPartitioning<'tcx> {
/// to keep track of that. /// to keep track of that.
#[derive(Clone, PartialEq, Eq, Debug)] #[derive(Clone, PartialEq, Eq, Debug)]
enum MonoItemPlacement { enum MonoItemPlacement {
SingleCgu { cgu_name: InternedString }, SingleCgu { cgu_name: Symbol },
MultipleCgus, MultipleCgus,
} }
@ -251,8 +249,8 @@ where
None => fallback_cgu_name(cgu_name_builder), None => fallback_cgu_name(cgu_name_builder),
}; };
let codegen_unit = codegen_units.entry(codegen_unit_name.clone()) let codegen_unit = codegen_units.entry(codegen_unit_name)
.or_insert_with(|| CodegenUnit::new(codegen_unit_name.clone())); .or_insert_with(|| CodegenUnit::new(codegen_unit_name));
let mut can_be_internalized = true; let mut can_be_internalized = true;
let (linkage, visibility) = mono_item_linkage_and_visibility( let (linkage, visibility) = mono_item_linkage_and_visibility(
@ -273,8 +271,7 @@ where
// crate with just types (for example), we could wind up with no CGU. // crate with just types (for example), we could wind up with no CGU.
if codegen_units.is_empty() { if codegen_units.is_empty() {
let codegen_unit_name = fallback_cgu_name(cgu_name_builder); let codegen_unit_name = fallback_cgu_name(cgu_name_builder);
codegen_units.insert(codegen_unit_name.clone(), codegen_units.insert(codegen_unit_name, CodegenUnit::new(codegen_unit_name));
CodegenUnit::new(codegen_unit_name.clone()));
} }
PreInliningPartitioning { PreInliningPartitioning {
@ -492,7 +489,7 @@ fn merge_codegen_units<'tcx>(
// smallest into each other) we're sure to start off with a deterministic // smallest into each other) we're sure to start off with a deterministic
// order (sorted by name). This'll mean that if two cgus have the same size // order (sorted by name). This'll mean that if two cgus have the same size
// the stable sort below will keep everything nice and deterministic. // the stable sort below will keep everything nice and deterministic.
codegen_units.sort_by_key(|cgu| *cgu.name()); codegen_units.sort_by_cached_key(|cgu| cgu.name().as_str());
// Merge the two smallest codegen units until the target size is reached. // Merge the two smallest codegen units until the target size is reached.
while codegen_units.len() > target_cgu_count { while codegen_units.len() > target_cgu_count {
@ -537,7 +534,7 @@ fn place_inlined_mono_items<'tcx>(initial_partitioning: PreInliningPartitioning<
follow_inlining(*root, inlining_map, &mut reachable); follow_inlining(*root, inlining_map, &mut reachable);
} }
let mut new_codegen_unit = CodegenUnit::new(old_codegen_unit.name().clone()); let mut new_codegen_unit = CodegenUnit::new(old_codegen_unit.name());
// Add all monomorphizations that are not already there. // Add all monomorphizations that are not already there.
for mono_item in reachable { for mono_item in reachable {
@ -564,8 +561,8 @@ fn place_inlined_mono_items<'tcx>(initial_partitioning: PreInliningPartitioning<
Entry::Occupied(e) => { Entry::Occupied(e) => {
let placement = e.into_mut(); let placement = e.into_mut();
debug_assert!(match *placement { debug_assert!(match *placement {
MonoItemPlacement::SingleCgu { ref cgu_name } => { MonoItemPlacement::SingleCgu { cgu_name } => {
*cgu_name != *new_codegen_unit.name() cgu_name != new_codegen_unit.name()
} }
MonoItemPlacement::MultipleCgus => true, MonoItemPlacement::MultipleCgus => true,
}); });
@ -573,7 +570,7 @@ fn place_inlined_mono_items<'tcx>(initial_partitioning: PreInliningPartitioning<
} }
Entry::Vacant(e) => { Entry::Vacant(e) => {
e.insert(MonoItemPlacement::SingleCgu { e.insert(MonoItemPlacement::SingleCgu {
cgu_name: new_codegen_unit.name().clone() cgu_name: new_codegen_unit.name()
}); });
} }
} }
@ -638,7 +635,7 @@ fn internalize_symbols<'tcx>(
// accessed from outside its defining codegen unit. // accessed from outside its defining codegen unit.
for cgu in &mut partitioning.codegen_units { for cgu in &mut partitioning.codegen_units {
let home_cgu = MonoItemPlacement::SingleCgu { let home_cgu = MonoItemPlacement::SingleCgu {
cgu_name: cgu.name().clone() cgu_name: cgu.name()
}; };
for (accessee, linkage_and_visibility) in cgu.items_mut() { for (accessee, linkage_and_visibility) in cgu.items_mut() {
@ -717,7 +714,7 @@ fn characteristic_def_id_of_mono_item<'tcx>(
} }
} }
type CguNameCache = FxHashMap<(DefId, bool), InternedString>; type CguNameCache = FxHashMap<(DefId, bool), Symbol>;
fn compute_codegen_unit_name( fn compute_codegen_unit_name(
tcx: TyCtxt<'_>, tcx: TyCtxt<'_>,
@ -725,7 +722,7 @@ fn compute_codegen_unit_name(
def_id: DefId, def_id: DefId,
volatile: bool, volatile: bool,
cache: &mut CguNameCache, cache: &mut CguNameCache,
) -> InternedString { ) -> Symbol {
// Find the innermost module that is not nested within a function. // Find the innermost module that is not nested within a function.
let mut current_def_id = def_id; let mut current_def_id = def_id;
let mut cgu_def_id = None; let mut cgu_def_id = None;
@ -777,7 +774,7 @@ fn compute_codegen_unit_name(
fn numbered_codegen_unit_name( fn numbered_codegen_unit_name(
name_builder: &mut CodegenUnitNameBuilder<'_>, name_builder: &mut CodegenUnitNameBuilder<'_>,
index: usize, index: usize,
) -> InternedString { ) -> Symbol {
name_builder.build_cgu_name_no_mangle(LOCAL_CRATE, &["cgu"], Some(index)) name_builder.build_cgu_name_no_mangle(LOCAL_CRATE, &["cgu"], Some(index))
} }
@ -929,7 +926,7 @@ fn collect_and_partition_mono_items(
for (&mono_item, &linkage) in cgu.items() { for (&mono_item, &linkage) in cgu.items() {
item_to_cgus.entry(mono_item) item_to_cgus.entry(mono_item)
.or_default() .or_default()
.push((cgu.name().clone(), linkage)); .push((cgu.name(), linkage));
} }
} }
@ -991,7 +988,7 @@ pub fn provide(providers: &mut Providers<'_>) {
providers.codegen_unit = |tcx, name| { providers.codegen_unit = |tcx, name| {
let (_, all) = tcx.collect_and_partition_mono_items(LOCAL_CRATE); let (_, all) = tcx.collect_and_partition_mono_items(LOCAL_CRATE);
all.iter() all.iter()
.find(|cgu| *cgu.name() == name) .find(|cgu| cgu.name() == name)
.cloned() .cloned()
.unwrap_or_else(|| panic!("failed to find cgu with name {:?}", name)) .unwrap_or_else(|| panic!("failed to find cgu with name {:?}", name))
}; };