Skip to content

Commit 8bc005c

Browse files
Don't recompute SymbolExportLevel for upstream crates.
1 parent aec6c85 commit 8bc005c

File tree

7 files changed

+91
-90
lines changed

7 files changed

+91
-90
lines changed

src/librustc/middle/exported_symbols.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use ty;
1717
/// kind of crate, including cdylibs which export very few things.
1818
/// `Rust` will only be exported if the crate produced is a Rust
1919
/// dylib.
20-
#[derive(Eq, PartialEq, Debug, Copy, Clone)]
20+
#[derive(Eq, PartialEq, Debug, Copy, Clone, RustcEncodable, RustcDecodable)]
2121
pub enum SymbolExportLevel {
2222
C,
2323
Rust,
@@ -39,7 +39,7 @@ impl SymbolExportLevel {
3939
}
4040
}
4141

42-
#[derive(Eq, PartialEq, Debug, Copy, Clone)]
42+
#[derive(Eq, PartialEq, Debug, Copy, Clone, RustcEncodable, RustcDecodable)]
4343
pub enum ExportedSymbol {
4444
NonGeneric(DefId),
4545
NoDefId(ty::SymbolName),

src/librustc/ty/context.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2460,4 +2460,12 @@ pub fn provide(providers: &mut ty::maps::Providers) {
24602460
assert_eq!(cnum, LOCAL_CRATE);
24612461
Lrc::new(tcx.sess.features_untracked().clone())
24622462
};
2463+
providers.is_panic_runtime = |tcx, cnum| {
2464+
assert_eq!(cnum, LOCAL_CRATE);
2465+
attr::contains_name(tcx.hir.krate_attrs(), "panic_runtime")
2466+
};
2467+
providers.is_compiler_builtins = |tcx, cnum| {
2468+
assert_eq!(cnum, LOCAL_CRATE);
2469+
attr::contains_name(tcx.hir.krate_attrs(), "compiler_builtins")
2470+
};
24632471
}

src/librustc_metadata/cstore_impl.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use rustc::ty::maps::QueryConfig;
1818
use rustc::middle::cstore::{CrateStore, DepKind,
1919
MetadataLoader, LinkMeta,
2020
LoadedMacro, EncodedMetadata, NativeLibraryKind};
21+
use rustc::middle::exported_symbols::ExportedSymbol;
2122
use rustc::middle::stability::DeprecationEntry;
2223
use rustc::hir::def;
2324
use rustc::session::{CrateDisambiguator, Session};
@@ -31,6 +32,7 @@ use rustc::util::nodemap::DefIdMap;
3132

3233
use std::any::Any;
3334
use rustc_data_structures::sync::Lrc;
35+
use std::sync::Arc;
3436

3537
use syntax::ast;
3638
use syntax::attr;
@@ -176,7 +178,21 @@ provide! { <'tcx> tcx, def_id, other, cdata,
176178
extern_crate => { Lrc::new(cdata.extern_crate.get()) }
177179
is_no_builtins => { cdata.is_no_builtins(tcx.sess) }
178180
impl_defaultness => { cdata.get_impl_defaultness(def_id.index) }
179-
reachable_non_generics => { Lrc::new(cdata.reachable_non_generics()) }
181+
reachable_non_generics => {
182+
let reachable_non_generics = tcx
183+
.exported_symbols(cdata.cnum)
184+
.iter()
185+
.filter_map(|&(exported_symbol, _)| {
186+
if let ExportedSymbol::NonGeneric(def_id) = exported_symbol {
187+
return Some(def_id)
188+
} else {
189+
None
190+
}
191+
})
192+
.collect();
193+
194+
Lrc::new(reachable_non_generics)
195+
}
180196
native_libraries => { Lrc::new(cdata.get_native_libraries(tcx.sess)) }
181197
plugin_registrar_fn => {
182198
cdata.root.plugin_registrar_fn.map(|index| {
@@ -235,6 +251,20 @@ provide! { <'tcx> tcx, def_id, other, cdata,
235251

236252
has_copy_closures => { cdata.has_copy_closures(tcx.sess) }
237253
has_clone_closures => { cdata.has_clone_closures(tcx.sess) }
254+
255+
exported_symbols => {
256+
let cnum = cdata.cnum;
257+
assert!(cnum != LOCAL_CRATE);
258+
259+
// If this crate is a plugin and/or a custom derive crate, then
260+
// we're not even going to link those in so we skip those crates.
261+
if cdata.root.plugin_registrar_fn.is_some() ||
262+
cdata.root.macro_derive_registrar.is_some() {
263+
return Arc::new(Vec::new())
264+
}
265+
266+
Arc::new(cdata.exported_symbols())
267+
}
238268
}
239269

240270
pub fn provide<'tcx>(providers: &mut Providers<'tcx>) {

src/librustc_metadata/decoder.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use rustc::hir::map::{DefKey, DefPath, DefPathData, DefPathHash};
1818
use rustc::hir;
1919
use rustc::middle::cstore::{LinkagePreference, ExternConstBody,
2020
ExternBodyNestedBodies};
21+
use rustc::middle::exported_symbols::{ExportedSymbol, SymbolExportLevel};
2122
use rustc::hir::def::{self, Def, CtorKind};
2223
use rustc::hir::def_id::{CrateNum, DefId, DefIndex,
2324
CRATE_DEF_INDEX, LOCAL_CRATE};
@@ -27,7 +28,6 @@ use rustc::mir;
2728
use rustc::session::Session;
2829
use rustc::ty::{self, Ty, TyCtxt};
2930
use rustc::ty::codec::TyDecoder;
30-
use rustc::util::nodemap::DefIdSet;
3131
use rustc::mir::Mir;
3232

3333
use std::cell::Ref;
@@ -1006,11 +1006,10 @@ impl<'a, 'tcx> CrateMetadata {
10061006
arg_names.decode(self).collect()
10071007
}
10081008

1009-
pub fn reachable_non_generics(&self) -> DefIdSet {
1009+
pub fn exported_symbols(&self) -> Vec<(ExportedSymbol, SymbolExportLevel)> {
10101010
self.root
1011-
.reachable_non_generics
1011+
.exported_symbols
10121012
.decode(self)
1013-
.map(|index| self.local_def_id(index))
10141013
.collect()
10151014
}
10161015

src/librustc_metadata/encoder.rs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,15 @@ use rustc::hir::def_id::{CrateNum, CRATE_DEF_INDEX, DefIndex, DefId, LOCAL_CRATE
2020
use rustc::hir::map::definitions::DefPathTable;
2121
use rustc::ich::Fingerprint;
2222
use rustc::middle::dependency_format::Linkage;
23+
use rustc::middle::exported_symbols::{ExportedSymbol, SymbolExportLevel};
2324
use rustc::middle::lang_items;
2425
use rustc::mir;
2526
use rustc::traits::specialization_graph;
2627
use rustc::ty::{self, Ty, TyCtxt, ReprOptions};
2728
use rustc::ty::codec::{self as ty_codec, TyEncoder};
2829

2930
use rustc::session::config::{self, CrateTypeProcMacro};
30-
use rustc::util::nodemap::{FxHashMap, DefIdSet};
31+
use rustc::util::nodemap::FxHashMap;
3132

3233
use rustc_data_structures::stable_hasher::StableHasher;
3334
use rustc_serialize::{Encodable, Encoder, SpecializedEncoder, opaque};
@@ -394,11 +395,11 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
394395

395396
// Encode exported symbols info.
396397
i = self.position();
397-
let reachable_non_generics = self.tcx.reachable_non_generics(LOCAL_CRATE);
398-
let reachable_non_generics = self.tracked(
399-
IsolatedEncoder::encode_reachable_non_generics,
400-
&reachable_non_generics);
401-
let reachable_non_generics_bytes = self.position() - i;
398+
let exported_symbols = self.tcx.exported_symbols(LOCAL_CRATE);
399+
let exported_symbols = self.tracked(
400+
IsolatedEncoder::encode_exported_symbols,
401+
&exported_symbols);
402+
let exported_symbols_bytes = self.position() - i;
402403

403404
// Encode and index the items.
404405
i = self.position();
@@ -442,7 +443,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
442443
codemap,
443444
def_path_table,
444445
impls,
445-
reachable_non_generics,
446+
exported_symbols,
446447
index,
447448
});
448449

@@ -462,7 +463,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
462463
println!(" native bytes: {}", native_lib_bytes);
463464
println!(" codemap bytes: {}", codemap_bytes);
464465
println!(" impl bytes: {}", impl_bytes);
465-
println!(" exp. symbols bytes: {}", reachable_non_generics_bytes);
466+
println!(" exp. symbols bytes: {}", exported_symbols_bytes);
466467
println!(" def-path table bytes: {}", def_path_table_bytes);
467468
println!(" item bytes: {}", item_bytes);
468469
println!(" index bytes: {}", index_bytes);
@@ -1388,13 +1389,10 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
13881389
// middle::reachable module but filters out items that either don't have a
13891390
// symbol associated with them (they weren't translated) or if they're an FFI
13901391
// definition (as that's not defined in this crate).
1391-
fn encode_reachable_non_generics(&mut self,
1392-
reachable_non_generics: &DefIdSet)
1393-
-> LazySeq<DefIndex> {
1394-
self.lazy_seq(reachable_non_generics.iter().map(|def_id| {
1395-
debug_assert!(def_id.is_local());
1396-
def_id.index
1397-
}))
1392+
fn encode_exported_symbols(&mut self,
1393+
exported_symbols: &[(ExportedSymbol, SymbolExportLevel)])
1394+
-> LazySeq<(ExportedSymbol, SymbolExportLevel)> {
1395+
self.lazy_seq(exported_symbols.iter().cloned())
13981396
}
13991397

14001398
fn encode_dylib_dependency_formats(&mut self, _: ()) -> LazySeq<Option<LinkagePreference>> {

src/librustc_metadata/schema.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use rustc::hir::def::{self, CtorKind};
1616
use rustc::hir::def_id::{DefIndex, DefId, CrateNum};
1717
use rustc::ich::StableHashingContext;
1818
use rustc::middle::cstore::{DepKind, LinkagePreference, NativeLibrary};
19+
use rustc::middle::exported_symbols::{ExportedSymbol, SymbolExportLevel};
1920
use rustc::middle::lang_items;
2021
use rustc::mir;
2122
use rustc::session::CrateDisambiguator;
@@ -202,7 +203,8 @@ pub struct CrateRoot {
202203
pub codemap: LazySeq<syntax_pos::FileMap>,
203204
pub def_path_table: Lazy<hir::map::definitions::DefPathTable>,
204205
pub impls: LazySeq<TraitImpls>,
205-
pub reachable_non_generics: LazySeq<DefIndex>,
206+
pub exported_symbols: LazySeq<(ExportedSymbol, SymbolExportLevel)>,
207+
206208
pub index: LazySeq<index::Index>,
207209
}
208210

src/librustc_trans/back/symbol_export.rs

Lines changed: 31 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,9 @@ fn reachable_non_generics_provider<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
7878
let reachable_non_generics = tcx
7979
.exported_symbols(LOCAL_CRATE)
8080
.iter()
81-
.filter_map(|&(exported_symbol, _)| {
81+
.filter_map(|&(exported_symbol, level)| {
8282
if let ExportedSymbol::NonGeneric(def_id) = exported_symbol {
83-
if tcx.symbol_export_level(def_id)
84-
.is_below_threshold(export_threshold) {
83+
if level.is_below_threshold(export_threshold) {
8584
return Some(def_id)
8685
}
8786
}
@@ -110,6 +109,16 @@ fn exported_symbols_provider_local<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
110109
return Arc::new(vec![])
111110
}
112111

112+
// Check to see if this crate is a "special runtime crate". These
113+
// crates, implementation details of the standard library, typically
114+
// have a bunch of `pub extern` and `#[no_mangle]` functions as the
115+
// ABI between them. We don't want their symbols to have a `C`
116+
// export level, however, as they're just implementation details.
117+
// Down below we'll hardwire all of the symbols to the `Rust` export
118+
// level instead.
119+
let special_runtime_crate = tcx.is_panic_runtime(LOCAL_CRATE) ||
120+
tcx.is_compiler_builtins(LOCAL_CRATE);
121+
113122
let mut reachable_non_generics: DefIdSet = tcx.reachable_set(LOCAL_CRATE).0
114123
.iter()
115124
.filter_map(|&node_id| {
@@ -176,7 +185,25 @@ fn exported_symbols_provider_local<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
176185
let mut symbols: Vec<_> = reachable_non_generics
177186
.iter()
178187
.map(|&def_id| {
179-
let export_level = tcx.symbol_export_level(def_id);
188+
let export_level = if special_runtime_crate {
189+
let name = tcx.symbol_name(Instance::mono(tcx, def_id));
190+
// We can probably do better here by just ensuring that
191+
// it has hidden visibility rather than public
192+
// visibility, as this is primarily here to ensure it's
193+
// not stripped during LTO.
194+
//
195+
// In general though we won't link right if these
196+
// symbols are stripped, and LTO currently strips them.
197+
if &*name == "rust_eh_personality" ||
198+
&*name == "rust_eh_register_frames" ||
199+
&*name == "rust_eh_unregister_frames" {
200+
SymbolExportLevel::C
201+
} else {
202+
SymbolExportLevel::Rust
203+
}
204+
} else {
205+
tcx.symbol_export_level(def_id)
206+
};
180207
debug!("EXPORTED SYMBOL (local): {} ({:?})",
181208
tcx.symbol_name(Instance::mono(tcx, def_id)),
182209
export_level);
@@ -222,70 +249,7 @@ pub fn provide(providers: &mut Providers) {
222249
providers.symbol_export_level = symbol_export_level_provider;
223250
}
224251

225-
fn exported_symbols_provider_extern<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
226-
cnum: CrateNum)
227-
-> Arc<Vec<(ExportedSymbol,
228-
SymbolExportLevel)>>
229-
{
230-
// If this crate is a plugin and/or a custom derive crate, then
231-
// we're not even going to link those in so we skip those crates.
232-
if tcx.plugin_registrar_fn(cnum).is_some() ||
233-
tcx.derive_registrar_fn(cnum).is_some() {
234-
return Arc::new(Vec::new())
235-
}
236-
237-
// Check to see if this crate is a "special runtime crate". These
238-
// crates, implementation details of the standard library, typically
239-
// have a bunch of `pub extern` and `#[no_mangle]` functions as the
240-
// ABI between them. We don't want their symbols to have a `C`
241-
// export level, however, as they're just implementation details.
242-
// Down below we'll hardwire all of the symbols to the `Rust` export
243-
// level instead.
244-
let special_runtime_crate =
245-
tcx.is_panic_runtime(cnum) || tcx.is_compiler_builtins(cnum);
246-
247-
let mut crate_exports: Vec<_> = tcx
248-
.reachable_non_generics(cnum)
249-
.iter()
250-
.map(|&def_id| {
251-
let export_level = if special_runtime_crate {
252-
let name = tcx.symbol_name(Instance::mono(tcx, def_id));
253-
// We can probably do better here by just ensuring that
254-
// it has hidden visibility rather than public
255-
// visibility, as this is primarily here to ensure it's
256-
// not stripped during LTO.
257-
//
258-
// In general though we won't link right if these
259-
// symbols are stripped, and LTO currently strips them.
260-
if &*name == "rust_eh_personality" ||
261-
&*name == "rust_eh_register_frames" ||
262-
&*name == "rust_eh_unregister_frames" {
263-
SymbolExportLevel::C
264-
} else {
265-
SymbolExportLevel::Rust
266-
}
267-
} else {
268-
tcx.symbol_export_level(def_id)
269-
};
270-
271-
debug!("EXPORTED SYMBOL (re-export): {} ({:?})",
272-
tcx.symbol_name(Instance::mono(tcx, def_id)),
273-
export_level);
274-
275-
(ExportedSymbol::NonGeneric(def_id), export_level)
276-
})
277-
.collect();
278-
279-
// Sort so we get a stable incr. comp. hash.
280-
crate_exports.sort_unstable_by(|&(ref symbol1, ..), &(ref symbol2, ..)| {
281-
symbol1.compare_stable(tcx, symbol2)
282-
});
283-
284-
Arc::new(crate_exports)
285-
}
286-
287252
pub fn provide_extern(providers: &mut Providers) {
288-
providers.exported_symbols = exported_symbols_provider_extern;
289253
providers.is_reachable_non_generic = is_reachable_non_generic_provider;
290254
providers.symbol_export_level = symbol_export_level_provider;
291255
}

0 commit comments

Comments
 (0)