Skip to content

Commit

Permalink
Use Symbol for codegen unit names.
Browse files Browse the repository at this point in the history
This is a straightforward replacement except for two places where we
have to convert to `LocalInternedString` to get a stable sort.
  • Loading branch information
nnethercote committed Oct 21, 2019
1 parent dddacf1 commit 2da7a9c
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 51 deletions.
4 changes: 2 additions & 2 deletions src/librustc/dep_graph/dep_node.rs
Expand Up @@ -59,7 +59,7 @@ use crate::ich::{Fingerprint, StableHashingContext};
use rustc_data_structures::stable_hasher::{StableHasher, HashStable};
use std::fmt;
use std::hash::Hash;
use syntax_pos::symbol::InternedString;
use syntax_pos::symbol::Symbol;
use crate::traits;
use crate::traits::query::{
CanonicalProjectionGoal, CanonicalTyGoal, CanonicalTypeOpAscribeUserTypeGoal,
Expand Down Expand Up @@ -426,7 +426,7 @@ rustc_dep_node_append!([define_dep_nodes!][ <'tcx>

[anon] TraitSelect,

[] CompileCodegenUnit(InternedString),
[] CompileCodegenUnit(Symbol),

[eval_always] Analysis(CrateNum),
]);
Expand Down
20 changes: 10 additions & 10 deletions src/librustc/mir/mono.rs
@@ -1,6 +1,6 @@
use crate::hir::def_id::{DefId, CrateNum, LOCAL_CRATE};
use crate::hir::HirId;
use syntax::symbol::{InternedString, Symbol};
use syntax::symbol::Symbol;
use syntax::attr::InlineAttr;
use syntax::source_map::Span;
use crate::ty::{Instance, InstanceDef, TyCtxt, SymbolName, subst::InternalSubsts};
Expand Down Expand Up @@ -246,7 +246,7 @@ pub struct CodegenUnit<'tcx> {
/// name be unique amongst **all** crates. Therefore, it should
/// contain something unique to this crate (e.g., a module path)
/// as well as the crate name and disambiguator.
name: InternedString,
name: Symbol,
items: FxHashMap<MonoItem<'tcx>, (Linkage, Visibility)>,
size_estimate: Option<usize>,
}
Expand Down Expand Up @@ -294,19 +294,19 @@ impl_stable_hash_for!(enum self::Visibility {
});

impl<'tcx> CodegenUnit<'tcx> {
pub fn new(name: InternedString) -> CodegenUnit<'tcx> {
pub fn new(name: Symbol) -> CodegenUnit<'tcx> {
CodegenUnit {
name: name,
items: Default::default(),
size_estimate: None,
}
}

pub fn name(&self) -> &InternedString {
&self.name
pub fn name(&self) -> Symbol {
self.name
}

pub fn set_name(&mut self, name: InternedString) {
pub fn set_name(&mut self, name: Symbol) {
self.name = name;
}

Expand Down Expand Up @@ -474,7 +474,7 @@ impl CodegenUnitNameBuilder<'tcx> {
cnum: CrateNum,
components: I,
special_suffix: Option<S>)
-> InternedString
-> Symbol
where I: IntoIterator<Item=C>,
C: fmt::Display,
S: fmt::Display,
Expand All @@ -487,7 +487,7 @@ impl CodegenUnitNameBuilder<'tcx> {
cgu_name
} else {
let cgu_name = &cgu_name.as_str()[..];
InternedString::intern(&CodegenUnit::mangle_name(cgu_name))
Symbol::intern(&CodegenUnit::mangle_name(cgu_name))
}
}

Expand All @@ -497,7 +497,7 @@ impl CodegenUnitNameBuilder<'tcx> {
cnum: CrateNum,
components: I,
special_suffix: Option<S>)
-> InternedString
-> Symbol
where I: IntoIterator<Item=C>,
C: fmt::Display,
S: fmt::Display,
Expand Down Expand Up @@ -543,6 +543,6 @@ impl CodegenUnitNameBuilder<'tcx> {
write!(cgu_name, ".{}", special_suffix).unwrap();
}

InternedString::intern(&cgu_name[..])
Symbol::intern(&cgu_name[..])
}
}
4 changes: 2 additions & 2 deletions src/librustc/query/mod.rs
Expand Up @@ -15,7 +15,7 @@ use crate::traits::query::{
};

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
// `Providers` struct for requesting a value of that type, and a method
Expand Down Expand Up @@ -924,7 +924,7 @@ rustc_queries! {
desc { "collect_and_partition_mono_items" }
}
query is_codegened_item(_: DefId) -> bool {}
query codegen_unit(_: InternedString) -> Arc<CodegenUnit<'tcx>> {
query codegen_unit(_: Symbol) -> Arc<CodegenUnit<'tcx>> {
no_force
desc { "codegen_unit" }
}
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/ty/query/keys.rs
Expand Up @@ -11,7 +11,7 @@ use crate::mir;
use std::fmt::Debug;
use std::hash::Hash;
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
/// for a query.
Expand Down Expand Up @@ -190,7 +190,7 @@ impl<'tcx> Key for traits::Environment<'tcx> {
}
}

impl Key for InternedString {
impl Key for Symbol {
fn query_crate(&self) -> CrateNum {
LOCAL_CRATE
}
Expand Down
1 change: 0 additions & 1 deletion src/librustc/ty/query/mod.rs
Expand Up @@ -55,7 +55,6 @@ use std::ops::Deref;
use std::sync::Arc;
use std::any::type_name;
use syntax_pos::{Span, DUMMY_SP};
use syntax_pos::symbol::InternedString;
use syntax::attr;
use syntax::ast;
use syntax::feature_gate;
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_codegen_llvm/base.rs
Expand Up @@ -36,7 +36,7 @@ use rustc_codegen_ssa::back::write::submit_codegened_module_to_llvm;

use std::ffi::CString;
use std::time::Instant;
use syntax_pos::symbol::InternedString;
use syntax_pos::symbol::Symbol;
use rustc::hir::CodegenFnAttrs;

use crate::value::Value;
Expand Down Expand Up @@ -105,7 +105,7 @@ pub fn iter_globals(llmod: &'ll llvm::Module) -> ValueIter<'ll> {

pub fn compile_codegen_unit(
tcx: TyCtxt<'tcx>,
cgu_name: InternedString,
cgu_name: Symbol,
tx_to_llvm_workers: &std::sync::mpsc::Sender<Box<dyn std::any::Any + Send>>,
) {
let prof_timer = tcx.prof.generic_activity("codegen_module");
Expand All @@ -131,7 +131,7 @@ pub fn compile_codegen_unit(

fn module_codegen(
tcx: TyCtxt<'_>,
cgu_name: InternedString,
cgu_name: Symbol,
) -> ModuleCodegen<ModuleLlvm> {
let cgu = tcx.codegen_unit(cgu_name);
// Instantiate monomorphizations without filling out definitions yet...
Expand Down
3 changes: 1 addition & 2 deletions src/librustc_codegen_llvm/lib.rs
Expand Up @@ -50,7 +50,6 @@ use rustc_codegen_ssa::CompiledModule;
use errors::{FatalError, Handler};
use rustc::dep_graph::WorkProduct;
use syntax_expand::allocator::AllocatorKind;
use syntax_pos::symbol::InternedString;
pub use llvm_util::target_features;
use std::any::Any;
use std::sync::Arc;
Expand Down Expand Up @@ -123,7 +122,7 @@ impl ExtraBackendMethods for LlvmCodegenBackend {
}
fn compile_codegen_unit(
&self, tcx: TyCtxt<'_>,
cgu_name: InternedString,
cgu_name: Symbol,
tx: &std::sync::mpsc::Sender<Box<dyn Any + Send>>,
) {
base::compile_codegen_unit(tcx, cgu_name, tx);
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_codegen_ssa/base.rs
Expand Up @@ -515,7 +515,7 @@ pub fn codegen_crate<B: ExtraBackendMethods>(
// unnecessarily.
if tcx.dep_graph.is_fully_enabled() {
for cgu in &codegen_units {
tcx.codegen_unit(cgu.name().clone());
tcx.codegen_unit(cgu.name());
}
}

Expand Down Expand Up @@ -603,7 +603,7 @@ pub fn codegen_crate<B: ExtraBackendMethods>(
match cgu_reuse {
CguReuse::No => {
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();
false
}
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_codegen_ssa/traits/backend.rs
Expand Up @@ -10,7 +10,7 @@ use rustc_codegen_utils::codegen_backend::CodegenBackend;
use std::sync::Arc;
use std::sync::mpsc;
use syntax_expand::allocator::AllocatorKind;
use syntax_pos::symbol::InternedString;
use syntax_pos::symbol::Symbol;

pub trait BackendTypes {
type Value: CodegenObject;
Expand Down Expand Up @@ -50,7 +50,7 @@ pub trait ExtraBackendMethods: CodegenBackend + WriteBackendMethods + Sized + Se
fn compile_codegen_unit(
&self,
tcx: TyCtxt<'_>,
cgu_name: InternedString,
cgu_name: Symbol,
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
Expand Down
8 changes: 4 additions & 4 deletions src/librustc_incremental/assert_module_sources.rs
Expand Up @@ -27,7 +27,7 @@ use rustc::mir::mono::CodegenUnitNameBuilder;
use rustc::ty::TyCtxt;
use std::collections::BTreeSet;
use syntax::ast;
use syntax::symbol::{InternedString, Symbol, sym};
use syntax::symbol::{Symbol, sym};
use rustc::ich::{ATTR_PARTITION_REUSED, ATTR_PARTITION_CODEGENED,
ATTR_EXPECTED_CGU_REUSE};

Expand All @@ -45,8 +45,8 @@ pub fn assert_module_sources(tcx: TyCtxt<'_>) {
.collect_and_partition_mono_items(LOCAL_CRATE)
.1
.iter()
.map(|cgu| *cgu.name())
.collect::<BTreeSet<InternedString>>();
.map(|cgu| cgu.name())
.collect::<BTreeSet<Symbol>>();

let ams = AssertModuleSource {
tcx,
Expand All @@ -61,7 +61,7 @@ pub fn assert_module_sources(tcx: TyCtxt<'_>) {

struct AssertModuleSource<'tcx> {
tcx: TyCtxt<'tcx>,
available_cgus: BTreeSet<InternedString>,
available_cgus: BTreeSet<Symbol>,
}

impl AssertModuleSource<'tcx> {
Expand Down
39 changes: 18 additions & 21 deletions src/librustc_mir/monomorphize/partitioning.rs
Expand Up @@ -96,7 +96,7 @@ use std::collections::hash_map::Entry;
use std::cmp;
use std::sync::Arc;

use syntax::symbol::InternedString;
use syntax::symbol::Symbol;
use rustc::hir::CodegenFnAttrFlags;
use rustc::hir::def::DefKind;
use rustc::hir::def_id::{CrateNum, DefId, LOCAL_CRATE, CRATE_DEF_INDEX};
Expand All @@ -121,7 +121,7 @@ pub enum PartitioningStrategy {
}

// 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"))
}

Expand Down Expand Up @@ -185,9 +185,7 @@ where
internalization_candidates: _,
} = post_inlining;

result.sort_by(|cgu1, cgu2| {
cgu1.name().cmp(cgu2.name())
});
result.sort_by_cached_key(|cgu| cgu.name().as_str());

result
}
Expand All @@ -203,7 +201,7 @@ struct PreInliningPartitioning<'tcx> {
/// to keep track of that.
#[derive(Clone, PartialEq, Eq, Debug)]
enum MonoItemPlacement {
SingleCgu { cgu_name: InternedString },
SingleCgu { cgu_name: Symbol },
MultipleCgus,
}

Expand Down Expand Up @@ -251,8 +249,8 @@ where
None => fallback_cgu_name(cgu_name_builder),
};

let codegen_unit = codegen_units.entry(codegen_unit_name.clone())
.or_insert_with(|| CodegenUnit::new(codegen_unit_name.clone()));
let codegen_unit = codegen_units.entry(codegen_unit_name)
.or_insert_with(|| CodegenUnit::new(codegen_unit_name));

let mut can_be_internalized = true;
let (linkage, visibility) = mono_item_linkage_and_visibility(
Expand All @@ -273,8 +271,7 @@ where
// crate with just types (for example), we could wind up with no CGU.
if codegen_units.is_empty() {
let codegen_unit_name = fallback_cgu_name(cgu_name_builder);
codegen_units.insert(codegen_unit_name.clone(),
CodegenUnit::new(codegen_unit_name.clone()));
codegen_units.insert(codegen_unit_name, CodegenUnit::new(codegen_unit_name));
}

PreInliningPartitioning {
Expand Down Expand Up @@ -492,7 +489,7 @@ fn merge_codegen_units<'tcx>(
// 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
// 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.
while codegen_units.len() > target_cgu_count {
Expand Down Expand Up @@ -537,7 +534,7 @@ fn place_inlined_mono_items<'tcx>(initial_partitioning: PreInliningPartitioning<
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.
for mono_item in reachable {
Expand All @@ -564,16 +561,16 @@ fn place_inlined_mono_items<'tcx>(initial_partitioning: PreInliningPartitioning<
Entry::Occupied(e) => {
let placement = e.into_mut();
debug_assert!(match *placement {
MonoItemPlacement::SingleCgu { ref cgu_name } => {
*cgu_name != *new_codegen_unit.name()
MonoItemPlacement::SingleCgu { cgu_name } => {
cgu_name != new_codegen_unit.name()
}
MonoItemPlacement::MultipleCgus => true,
});
*placement = MonoItemPlacement::MultipleCgus;
}
Entry::Vacant(e) => {
e.insert(MonoItemPlacement::SingleCgu {
cgu_name: new_codegen_unit.name().clone()
cgu_name: new_codegen_unit.name()
});
}
}
Expand Down Expand Up @@ -638,7 +635,7 @@ fn internalize_symbols<'tcx>(
// accessed from outside its defining codegen unit.
for cgu in &mut partitioning.codegen_units {
let home_cgu = MonoItemPlacement::SingleCgu {
cgu_name: cgu.name().clone()
cgu_name: cgu.name()
};

for (accessee, linkage_and_visibility) in cgu.items_mut() {
Expand Down Expand Up @@ -717,15 +714,15 @@ 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(
tcx: TyCtxt<'_>,
name_builder: &mut CodegenUnitNameBuilder<'_>,
def_id: DefId,
volatile: bool,
cache: &mut CguNameCache,
) -> InternedString {
) -> Symbol {
// Find the innermost module that is not nested within a function.
let mut current_def_id = def_id;
let mut cgu_def_id = None;
Expand Down Expand Up @@ -777,7 +774,7 @@ fn compute_codegen_unit_name(
fn numbered_codegen_unit_name(
name_builder: &mut CodegenUnitNameBuilder<'_>,
index: usize,
) -> InternedString {
) -> Symbol {
name_builder.build_cgu_name_no_mangle(LOCAL_CRATE, &["cgu"], Some(index))
}

Expand Down Expand Up @@ -929,7 +926,7 @@ fn collect_and_partition_mono_items(
for (&mono_item, &linkage) in cgu.items() {
item_to_cgus.entry(mono_item)
.or_default()
.push((cgu.name().clone(), linkage));
.push((cgu.name(), linkage));
}
}

Expand Down Expand Up @@ -991,7 +988,7 @@ pub fn provide(providers: &mut Providers<'_>) {
providers.codegen_unit = |tcx, name| {
let (_, all) = tcx.collect_and_partition_mono_items(LOCAL_CRATE);
all.iter()
.find(|cgu| *cgu.name() == name)
.find(|cgu| cgu.name() == name)
.cloned()
.unwrap_or_else(|| panic!("failed to find cgu with name {:?}", name))
};
Expand Down

0 comments on commit 2da7a9c

Please sign in to comment.