Skip to content

Commit 9aac5fc

Browse files
committed
refactor away trans::symbol_map
1 parent a517343 commit 9aac5fc

File tree

5 files changed

+70
-168
lines changed

5 files changed

+70
-168
lines changed

src/librustc_trans/back/symbol_export.rs

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,11 @@
1010

1111
use context::SharedCrateContext;
1212
use monomorphize::Instance;
13-
use symbol_map::SymbolMap;
1413
use util::nodemap::FxHashMap;
1514
use rustc::hir::def_id::{DefId, CrateNum, LOCAL_CRATE};
1615
use rustc::session::config;
1716
use rustc::ty::TyCtxt;
1817
use syntax::attr;
19-
use trans_item::TransItem;
2018

2119
/// The SymbolExportLevel of a symbols specifies from which kinds of crates
2220
/// the symbol will be exported. `C` symbols will be exported from any
@@ -35,24 +33,21 @@ pub struct ExportedSymbols {
3533
}
3634

3735
impl ExportedSymbols {
38-
3936
pub fn empty() -> ExportedSymbols {
4037
ExportedSymbols {
4138
exports: FxHashMap(),
4239
}
4340
}
4441

45-
pub fn compute_from<'a, 'tcx>(scx: &SharedCrateContext<'a, 'tcx>,
46-
symbol_map: &SymbolMap<'tcx>)
47-
-> ExportedSymbols {
42+
pub fn compute<'a, 'tcx>(scx: &SharedCrateContext<'a, 'tcx>) -> ExportedSymbols {
4843
let mut local_crate: Vec<_> = scx
4944
.exported_symbols()
5045
.iter()
5146
.map(|&node_id| {
5247
scx.tcx().hir.local_def_id(node_id)
5348
})
5449
.map(|def_id| {
55-
let name = symbol_for_def_id(scx.tcx(), def_id, symbol_map);
50+
let name = scx.tcx().symbol_name(Instance::mono(scx.tcx(), def_id));
5651
let export_level = export_level(scx, def_id);
5752
debug!("EXPORTED SYMBOL (local): {} ({:?})", name, export_level);
5853
(str::to_owned(&name), export_level)
@@ -212,20 +207,3 @@ pub fn is_below_threshold(level: SymbolExportLevel,
212207
level == SymbolExportLevel::C
213208
}
214209
}
215-
216-
fn symbol_for_def_id<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
217-
def_id: DefId,
218-
symbol_map: &SymbolMap<'tcx>)
219-
-> String {
220-
// Just try to look things up in the symbol map. If nothing's there, we
221-
// recompute.
222-
if let Some(node_id) = tcx.hir.as_local_node_id(def_id) {
223-
if let Some(sym) = symbol_map.get(TransItem::Static(node_id)) {
224-
return sym.to_owned();
225-
}
226-
}
227-
228-
let instance = Instance::mono(tcx, def_id);
229-
230-
str::to_owned(&tcx.symbol_name(instance))
231-
}

src/librustc_trans/base.rs

Lines changed: 54 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ use meth;
6565
use mir;
6666
use monomorphize::{self, Instance};
6767
use partitioning::{self, PartitioningStrategy, CodegenUnit};
68-
use symbol_map::SymbolMap;
6968
use symbol_names_test;
7069
use trans_item::{TransItem, DefPathBasedNames};
7170
use type_::Type;
@@ -803,7 +802,6 @@ fn internalize_symbols<'a, 'tcx>(sess: &Session,
803802
scx: &SharedCrateContext<'a, 'tcx>,
804803
translation_items: &FxHashSet<TransItem<'tcx>>,
805804
llvm_modules: &[ModuleLlvm],
806-
symbol_map: &SymbolMap<'tcx>,
807805
exported_symbols: &ExportedSymbols) {
808806
let export_threshold =
809807
symbol_export::crates_export_threshold(&sess.crate_types.borrow());
@@ -855,7 +853,7 @@ fn internalize_symbols<'a, 'tcx>(sess: &Session,
855853
let mut linkage_fixed_explicitly = FxHashSet();
856854

857855
for trans_item in translation_items {
858-
let symbol_name = symbol_map.get_or_compute(scx, *trans_item);
856+
let symbol_name = str::to_owned(&trans_item.symbol_name(tcx));
859857
if trans_item.explicit_linkage(tcx).is_some() {
860858
linkage_fixed_explicitly.insert(symbol_name.clone());
861859
}
@@ -1109,7 +1107,7 @@ pub fn trans_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
11091107

11101108
// Run the translation item collector and partition the collected items into
11111109
// codegen units.
1112-
let (translation_items, codegen_units, symbol_map) =
1110+
let (translation_items, codegen_units) =
11131111
collect_and_partition_translation_items(&shared_ccx);
11141112

11151113
let mut all_stats = Stats::default();
@@ -1269,8 +1267,7 @@ pub fn trans_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
12691267

12701268
let sess = shared_ccx.sess();
12711269

1272-
let exported_symbols = ExportedSymbols::compute_from(&shared_ccx,
1273-
&symbol_map);
1270+
let exported_symbols = ExportedSymbols::compute(&shared_ccx);
12741271

12751272
// Get the list of llvm modules we created. We'll do a few wacky
12761273
// transforms on them now.
@@ -1290,7 +1287,6 @@ pub fn trans_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
12901287
&shared_ccx,
12911288
&translation_items,
12921289
&llvm_modules,
1293-
&symbol_map,
12941290
&exported_symbols);
12951291
});
12961292

@@ -1516,10 +1512,57 @@ fn gather_type_sizes<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
15161512
}
15171513
}
15181514

1515+
#[inline(never)] // give this a place in the profiler
1516+
fn assert_symbols_are_distinct<'a, 'tcx, I>(tcx: TyCtxt<'a, 'tcx, 'tcx>, trans_items: I)
1517+
where I: Iterator<Item=&'a TransItem<'tcx>>
1518+
{
1519+
let mut symbols: Vec<_> = trans_items.map(|trans_item| {
1520+
(trans_item, trans_item.symbol_name(tcx))
1521+
}).collect();
1522+
1523+
(&mut symbols[..]).sort_by(|&(_, ref sym1), &(_, ref sym2)|{
1524+
sym1.cmp(sym2)
1525+
});
1526+
1527+
for pair in (&symbols[..]).windows(2) {
1528+
let sym1 = &pair[0].1;
1529+
let sym2 = &pair[1].1;
1530+
1531+
if *sym1 == *sym2 {
1532+
let trans_item1 = pair[0].0;
1533+
let trans_item2 = pair[1].0;
1534+
1535+
let span1 = trans_item1.local_span(tcx);
1536+
let span2 = trans_item2.local_span(tcx);
1537+
1538+
// Deterministically select one of the spans for error reporting
1539+
let span = match (span1, span2) {
1540+
(Some(span1), Some(span2)) => {
1541+
Some(if span1.lo.0 > span2.lo.0 {
1542+
span1
1543+
} else {
1544+
span2
1545+
})
1546+
}
1547+
(Some(span), None) |
1548+
(None, Some(span)) => Some(span),
1549+
_ => None
1550+
};
1551+
1552+
let error_message = format!("symbol `{}` is already defined", sym1);
1553+
1554+
if let Some(span) = span {
1555+
tcx.sess.span_fatal(span, &error_message)
1556+
} else {
1557+
tcx.sess.fatal(&error_message)
1558+
}
1559+
}
1560+
}
1561+
}
1562+
15191563
fn collect_and_partition_translation_items<'a, 'tcx>(scx: &SharedCrateContext<'a, 'tcx>)
15201564
-> (FxHashSet<TransItem<'tcx>>,
1521-
Vec<CodegenUnit<'tcx>>,
1522-
SymbolMap<'tcx>) {
1565+
Vec<CodegenUnit<'tcx>>) {
15231566
let time_passes = scx.sess().time_passes();
15241567

15251568
let collection_mode = match scx.sess().opts.debugging_opts.print_trans_items {
@@ -1547,7 +1590,7 @@ fn collect_and_partition_translation_items<'a, 'tcx>(scx: &SharedCrateContext<'a
15471590
collector::collect_crate_translation_items(&scx, collection_mode)
15481591
});
15491592

1550-
let symbol_map = SymbolMap::build(scx, items.iter().cloned());
1593+
assert_symbols_are_distinct(scx.tcx(), items.iter());
15511594

15521595
let strategy = if scx.sess().opts.debugging_opts.incremental.is_some() {
15531596
PartitioningStrategy::PerModule
@@ -1620,5 +1663,5 @@ fn collect_and_partition_translation_items<'a, 'tcx>(scx: &SharedCrateContext<'a
16201663
}
16211664
}
16221665

1623-
(translation_items, codegen_units, symbol_map)
1666+
(translation_items, codegen_units)
16241667
}

src/librustc_trans/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ mod meth;
125125
mod mir;
126126
mod monomorphize;
127127
mod partitioning;
128-
mod symbol_map;
129128
mod symbol_names_test;
130129
mod trans_item;
131130
mod tvec;

src/librustc_trans/symbol_map.rs

Lines changed: 0 additions & 131 deletions
This file was deleted.

src/librustc_trans/trans_item.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ use rustc::hir;
2828
use rustc::hir::def_id::DefId;
2929
use rustc::ty::{self, Ty, TyCtxt, TypeFoldable};
3030
use rustc::ty::subst::Substs;
31-
use syntax_pos::symbol::Symbol;
3231
use syntax::ast::{self, NodeId};
3332
use syntax::attr;
33+
use syntax_pos::Span;
34+
use syntax_pos::symbol::Symbol;
3435
use type_of;
3536
use std::fmt::Write;
3637
use std::iter;
@@ -200,6 +201,18 @@ impl<'a, 'tcx> TransItem<'tcx> {
200201
}
201202
}
202203

204+
pub fn local_span(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Option<Span> {
205+
match *self {
206+
TransItem::Fn(Instance { def, .. }) => {
207+
tcx.hir.as_local_node_id(def.def_id())
208+
}
209+
TransItem::Static(node_id) |
210+
TransItem::GlobalAsm(node_id) => {
211+
Some(node_id)
212+
}
213+
}.map(|node_id| tcx.hir.span(node_id))
214+
}
215+
203216
pub fn instantiation_mode(&self,
204217
tcx: TyCtxt<'a, 'tcx, 'tcx>)
205218
-> InstantiationMode {

0 commit comments

Comments
 (0)