Skip to content

Commit

Permalink
just take tcx where we can
Browse files Browse the repository at this point in the history
The more we can things dependent on just tcx, the easier it will
be to make queries etc later on.
  • Loading branch information
nikomatsakis committed Apr 21, 2017
1 parent 39a58c3 commit c446cb0
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 66 deletions.
12 changes: 6 additions & 6 deletions src/librustc_trans/back/symbol_export.rs
Expand Up @@ -53,7 +53,7 @@ impl ExportedSymbols {
scx.tcx().hir.local_def_id(node_id)
})
.map(|def_id| {
let name = symbol_for_def_id(scx, def_id, symbol_map);
let name = symbol_for_def_id(scx.tcx(), def_id, symbol_map);
let export_level = export_level(scx, def_id);
debug!("EXPORTED SYMBOL (local): {} ({:?})", name, export_level);
(name, export_level)
Expand Down Expand Up @@ -108,7 +108,7 @@ impl ExportedSymbols {
.exported_symbols(cnum)
.iter()
.map(|&def_id| {
let name = symbol_name(Instance::mono(scx.tcx(), def_id), scx);
let name = symbol_name(Instance::mono(scx.tcx(), def_id), scx.tcx());
let export_level = if special_runtime_crate {
// We can probably do better here by just ensuring that
// it has hidden visibility rather than public
Expand Down Expand Up @@ -214,21 +214,21 @@ pub fn is_below_threshold(level: SymbolExportLevel,
}
}

fn symbol_for_def_id<'a, 'tcx>(scx: &SharedCrateContext<'a, 'tcx>,
fn symbol_for_def_id<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
def_id: DefId,
symbol_map: &SymbolMap<'tcx>)
-> String {
// Just try to look things up in the symbol map. If nothing's there, we
// recompute.
if let Some(node_id) = scx.tcx().hir.as_local_node_id(def_id) {
if let Some(node_id) = tcx.hir.as_local_node_id(def_id) {
if let Some(sym) = symbol_map.get(TransItem::Static(node_id)) {
return sym.to_owned();
}
}

let instance = Instance::mono(scx.tcx(), def_id);
let instance = Instance::mono(tcx, def_id);

symbol_map.get(TransItem::Fn(instance))
.map(str::to_owned)
.unwrap_or_else(|| symbol_name(instance, scx))
.unwrap_or_else(|| symbol_name(instance, tcx))
}
53 changes: 25 additions & 28 deletions src/librustc_trans/back/symbol_names.rs
Expand Up @@ -97,13 +97,12 @@
//! virtually impossible. Thus, symbol hash generation exclusively relies on
//! DefPaths which are much more robust in the face of changes to the code base.

use common::SharedCrateContext;
use monomorphize::Instance;

use rustc::middle::weak_lang_items;
use rustc::hir::def_id::DefId;
use rustc::hir::map as hir_map;
use rustc::ty::{self, Ty, TypeFoldable};
use rustc::ty::{self, Ty, TyCtxt, TypeFoldable};
use rustc::ty::fold::TypeVisitor;
use rustc::ty::item_path::{self, ItemPathBuffer, RootMode};
use rustc::ty::subst::Substs;
Expand All @@ -113,7 +112,7 @@ use rustc::util::common::record_time;
use syntax::attr;
use syntax::symbol::{Symbol, InternedString};

fn get_symbol_hash<'a, 'tcx>(scx: &SharedCrateContext<'a, 'tcx>,
fn get_symbol_hash<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,

// the DefId of the item this name is for
def_id: Option<DefId>,
Expand All @@ -130,8 +129,6 @@ fn get_symbol_hash<'a, 'tcx>(scx: &SharedCrateContext<'a, 'tcx>,
-> String {
debug!("get_symbol_hash(def_id={:?}, parameters={:?})", def_id, substs);

let tcx = scx.tcx();

let mut hasher = ty::util::TypeIdHasher::<u64>::new(tcx);

record_time(&tcx.sess.perf_stats.symbol_hash_time, || {
Expand All @@ -157,8 +154,8 @@ fn get_symbol_hash<'a, 'tcx>(scx: &SharedCrateContext<'a, 'tcx>,
// in case the same instances is emitted in two crates of the same
// project.
if substs.types().next().is_some() {
hasher.hash(scx.tcx().crate_name.as_str());
hasher.hash(scx.sess().local_crate_disambiguator().as_str());
hasher.hash(tcx.crate_name.as_str());
hasher.hash(tcx.sess.local_crate_disambiguator().as_str());
}
}
});
Expand All @@ -168,37 +165,37 @@ fn get_symbol_hash<'a, 'tcx>(scx: &SharedCrateContext<'a, 'tcx>,
}

pub fn symbol_name<'a, 'tcx>(instance: Instance<'tcx>,
scx: &SharedCrateContext<'a, 'tcx>) -> String {
tcx: TyCtxt<'a, 'tcx, 'tcx>) -> String {
let def_id = instance.def_id();
let substs = instance.substs;

debug!("symbol_name(def_id={:?}, substs={:?})",
def_id, substs);

let node_id = scx.tcx().hir.as_local_node_id(def_id);
let node_id = tcx.hir.as_local_node_id(def_id);

if let Some(id) = node_id {
if scx.sess().plugin_registrar_fn.get() == Some(id) {
if tcx.sess.plugin_registrar_fn.get() == Some(id) {
let idx = def_id.index;
let disambiguator = scx.sess().local_crate_disambiguator();
return scx.sess().generate_plugin_registrar_symbol(disambiguator, idx);
let disambiguator = tcx.sess.local_crate_disambiguator();
return tcx.sess.generate_plugin_registrar_symbol(disambiguator, idx);
}
if scx.sess().derive_registrar_fn.get() == Some(id) {
if tcx.sess.derive_registrar_fn.get() == Some(id) {
let idx = def_id.index;
let disambiguator = scx.sess().local_crate_disambiguator();
return scx.sess().generate_derive_registrar_symbol(disambiguator, idx);
let disambiguator = tcx.sess.local_crate_disambiguator();
return tcx.sess.generate_derive_registrar_symbol(disambiguator, idx);
}
}

// FIXME(eddyb) Precompute a custom symbol name based on attributes.
let attrs = scx.tcx().get_attrs(def_id);
let attrs = tcx.get_attrs(def_id);
let is_foreign = if let Some(id) = node_id {
match scx.tcx().hir.get(id) {
match tcx.hir.get(id) {
hir_map::NodeForeignItem(_) => true,
_ => false
}
} else {
scx.sess().cstore.is_foreign_item(def_id)
tcx.sess.cstore.is_foreign_item(def_id)
};

if let Some(name) = weak_lang_items::link_name(&attrs) {
Expand All @@ -210,17 +207,17 @@ pub fn symbol_name<'a, 'tcx>(instance: Instance<'tcx>,
return name.to_string();
}
// Don't mangle foreign items.
return scx.tcx().item_name(def_id).as_str().to_string();
return tcx.item_name(def_id).as_str().to_string();
}

if let Some(name) = attr::find_export_name_attr(scx.sess().diagnostic(), &attrs) {
if let Some(name) = attr::find_export_name_attr(tcx.sess.diagnostic(), &attrs) {
// Use provided name
return name.to_string();
}

if attr::contains_name(&attrs, "no_mangle") {
// Don't mangle
return scx.tcx().item_name(def_id).as_str().to_string();
return tcx.item_name(def_id).as_str().to_string();
}

// We want to compute the "type" of this item. Unfortunately, some
Expand All @@ -230,11 +227,11 @@ pub fn symbol_name<'a, 'tcx>(instance: Instance<'tcx>,
let mut ty_def_id = def_id;
let instance_ty;
loop {
let key = scx.tcx().def_key(ty_def_id);
let key = tcx.def_key(ty_def_id);
match key.disambiguated_data.data {
DefPathData::TypeNs(_) |
DefPathData::ValueNs(_) => {
instance_ty = scx.tcx().item_type(ty_def_id);
instance_ty = tcx.item_type(ty_def_id);
break;
}
_ => {
Expand All @@ -251,16 +248,16 @@ pub fn symbol_name<'a, 'tcx>(instance: Instance<'tcx>,

// Erase regions because they may not be deterministic when hashed
// and should not matter anyhow.
let instance_ty = scx.tcx().erase_regions(&instance_ty);
let instance_ty = tcx.erase_regions(&instance_ty);

let hash = get_symbol_hash(scx, Some(def_id), instance_ty, Some(substs));
let hash = get_symbol_hash(tcx, Some(def_id), instance_ty, Some(substs));

let mut buffer = SymbolPathBuffer {
names: Vec::new()
};

item_path::with_forced_absolute_paths(|| {
scx.tcx().push_item_path(&mut buffer, def_id);
tcx.push_item_path(&mut buffer, def_id);
});

mangle(buffer.names.into_iter(), &hash)
Expand All @@ -281,11 +278,11 @@ impl ItemPathBuffer for SymbolPathBuffer {
}
}

pub fn exported_name_from_type_and_prefix<'a, 'tcx>(scx: &SharedCrateContext<'a, 'tcx>,
pub fn exported_name_from_type_and_prefix<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
t: Ty<'tcx>,
prefix: &str)
-> String {
let hash = get_symbol_hash(scx, None, t, None);
let hash = get_symbol_hash(tcx, None, t, None);
let path = [Symbol::intern(prefix).as_str()];
mangle(path.iter().cloned(), &hash)
}
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_trans/base.rs
Expand Up @@ -1139,7 +1139,7 @@ pub fn trans_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,

let cgu_name = String::from(cgu.name());
let cgu_id = cgu.work_product_id();
let symbol_cache = SymbolCache::new(scx);
let symbol_cache = SymbolCache::new(scx.tcx());
let symbol_name_hash = cgu.compute_symbol_name_hash(scx, &symbol_cache);

// Check whether there is a previous work-product we can
Expand Down Expand Up @@ -1239,7 +1239,7 @@ pub fn trans_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,

assert_module_sources::assert_module_sources(tcx, &modules);

symbol_names_test::report_symbol_names(&shared_ccx);
symbol_names_test::report_symbol_names(tcx);

if shared_ccx.sess().trans_stats() {
println!("--- trans stats ---");
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_trans/consts.rs
Expand Up @@ -113,7 +113,7 @@ pub fn get_static(ccx: &CrateContext, def_id: DefId) -> ValueRef {
hir_map::NodeForeignItem(&hir::ForeignItem {
ref attrs, span, node: hir::ForeignItemStatic(..), ..
}) => {
let sym = symbol_names::symbol_name(instance, ccx.shared());
let sym = symbol_names::symbol_name(instance, ccx.tcx());
let g = if let Some(name) =
attr::first_attr_value_str_by_name(&attrs, "linkage") {
// If this is a static with a linkage specified, then we need to handle
Expand Down Expand Up @@ -173,7 +173,7 @@ pub fn get_static(ccx: &CrateContext, def_id: DefId) -> ValueRef {

g
} else {
let sym = symbol_names::symbol_name(instance, ccx.shared());
let sym = symbol_names::symbol_name(instance, ccx.tcx());

// FIXME(nagisa): perhaps the map of externs could be offloaded to llvm somehow?
// FIXME(nagisa): investigate whether it can be changed into define_global
Expand Down
14 changes: 7 additions & 7 deletions src/librustc_trans/partitioning.rs
Expand Up @@ -177,7 +177,7 @@ impl<'tcx> CodegenUnit<'tcx> {
pub fn compute_symbol_name_hash<'a>(&self,
scx: &SharedCrateContext<'a, 'tcx>,
symbol_cache: &SymbolCache<'a, 'tcx>)
-> u64 {
-> u64 {
let mut state = IchHasher::new();
let exported_symbols = scx.exported_symbols();
let all_items = self.items_in_deterministic_order(scx.tcx(), symbol_cache);
Expand Down Expand Up @@ -272,14 +272,14 @@ pub fn partition<'a, 'tcx, I>(scx: &SharedCrateContext<'a, 'tcx>,
let mut initial_partitioning = place_root_translation_items(scx,
trans_items);

debug_dump(scx, "INITIAL PARTITONING:", initial_partitioning.codegen_units.iter());
debug_dump(tcx, "INITIAL PARTITONING:", initial_partitioning.codegen_units.iter());

// If the partitioning should produce a fixed count of codegen units, merge
// until that count is reached.
if let PartitioningStrategy::FixedUnitCount(count) = strategy {
merge_codegen_units(&mut initial_partitioning, count, &tcx.crate_name.as_str());

debug_dump(scx, "POST MERGING:", initial_partitioning.codegen_units.iter());
debug_dump(tcx, "POST MERGING:", initial_partitioning.codegen_units.iter());
}

// In the next step, we use the inlining map to determine which addtional
Expand All @@ -289,7 +289,7 @@ pub fn partition<'a, 'tcx, I>(scx: &SharedCrateContext<'a, 'tcx>,
let post_inlining = place_inlined_translation_items(initial_partitioning,
inlining_map);

debug_dump(scx, "POST INLINING:", post_inlining.0.iter());
debug_dump(tcx, "POST INLINING:", post_inlining.0.iter());

// Finally, sort by codegen unit name, so that we get deterministic results
let mut result = post_inlining.0;
Expand Down Expand Up @@ -529,15 +529,15 @@ fn numbered_codegen_unit_name(crate_name: &str, index: usize) -> InternedString
Symbol::intern(&format!("{}{}{}", crate_name, NUMBERED_CODEGEN_UNIT_MARKER, index)).as_str()
}

fn debug_dump<'a, 'b, 'tcx, I>(scx: &SharedCrateContext<'a, 'tcx>,
fn debug_dump<'a, 'b, 'tcx, I>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
label: &str,
cgus: I)
where I: Iterator<Item=&'b CodegenUnit<'tcx>>,
'tcx: 'a + 'b
{
if cfg!(debug_assertions) {
debug!("{}", label);
let symbol_cache = SymbolCache::new(scx);
let symbol_cache = SymbolCache::new(tcx);
for cgu in cgus {
debug!("CodegenUnit {}:", cgu.name);

Expand All @@ -548,7 +548,7 @@ fn debug_dump<'a, 'b, 'tcx, I>(scx: &SharedCrateContext<'a, 'tcx>,
.unwrap_or("<no hash>");

debug!(" - {} [{:?}] [{}]",
trans_item.to_string(scx.tcx()),
trans_item.to_string(tcx),
linkage,
symbol_hash);
}
Expand Down
10 changes: 5 additions & 5 deletions src/librustc_trans/symbol_cache.rs
Expand Up @@ -8,9 +8,9 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use context::SharedCrateContext;
use std::cell::RefCell;
use std::rc::Rc;
use rustc::ty::TyCtxt;
use trans_item::TransItem;
use util::nodemap::FxHashMap;

Expand All @@ -21,22 +21,22 @@ use util::nodemap::FxHashMap;
// Thus they can always be recomputed if needed.

pub struct SymbolCache<'a, 'tcx: 'a> {
scx: &'a SharedCrateContext<'a, 'tcx>,
tcx: TyCtxt<'a, 'tcx, 'tcx>,
index: RefCell<FxHashMap<TransItem<'tcx>, Rc<String>>>,
}

impl<'a, 'tcx> SymbolCache<'a, 'tcx> {
pub fn new(scx: &'a SharedCrateContext<'a, 'tcx>) -> Self {
pub fn new(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Self {
SymbolCache {
scx,
tcx: tcx,
index: RefCell::new(FxHashMap())
}
}

pub fn get(&self, trans_item: TransItem<'tcx>) -> Rc<String> {
let mut index = self.index.borrow_mut();
index.entry(trans_item)
.or_insert_with(|| Rc::new(trans_item.compute_symbol_name(self.scx)))
.or_insert_with(|| Rc::new(trans_item.compute_symbol_name(self.tcx)))
.clone()
}
}
5 changes: 3 additions & 2 deletions src/librustc_trans/symbol_map.rs
Expand Up @@ -34,8 +34,9 @@ impl<'tcx> SymbolMap<'tcx> {
where I: Iterator<Item=TransItem<'tcx>>
{
// Check for duplicate symbol names
let tcx = scx.tcx();
let mut symbols: Vec<_> = trans_items.map(|trans_item| {
(trans_item, trans_item.compute_symbol_name(scx))
(trans_item, trans_item.compute_symbol_name(tcx))
}).collect();

(&mut symbols[..]).sort_by(|&(_, ref sym1), &(_, ref sym2)|{
Expand Down Expand Up @@ -124,7 +125,7 @@ impl<'tcx> SymbolMap<'tcx> {
if let Some(sym) = self.get(trans_item) {
Cow::from(sym)
} else {
Cow::from(trans_item.compute_symbol_name(scx))
Cow::from(trans_item.compute_symbol_name(scx.tcx()))
}
}
}

0 comments on commit c446cb0

Please sign in to comment.