Skip to content

Commit 2eada58

Browse files
committed
rustc: Remove another global map from trans
This commit removes the `crate_trans_items` field from the `CrateContext` of trans. This field, a big map, was calculated during partioning and was a set of all translation items. This isn't quite incremental-friendly because the map may change a lot but not have much effect on downstream consumers. Instead a new query was added for the one location this map was needed, along with a new comment explaining what the location is doing!
1 parent 19727c8 commit 2eada58

File tree

6 files changed

+76
-31
lines changed

6 files changed

+76
-31
lines changed

src/librustc/dep_graph/dep_node.rs

+1
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,7 @@ define_dep_nodes!( <'tcx>
579579
[] CollectAndPartitionTranslationItems,
580580
[] ExportName(DefId),
581581
[] ContainsExternIndicator(DefId),
582+
[] IsTranslatedFunction(DefId),
582583
);
583584

584585
trait DepNodeParams<'a, 'gcx: 'tcx + 'a, 'tcx: 'a> : fmt::Debug {

src/librustc/ty/maps.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use middle::resolve_lifetime::{Region, ObjectLifetimeDefault};
2424
use middle::stability::{self, DeprecationEntry};
2525
use middle::lang_items::{LanguageItems, LangItem};
2626
use middle::exported_symbols::SymbolExportLevel;
27-
use middle::trans::{TransItem, CodegenUnit};
27+
use middle::trans::CodegenUnit;
2828
use mir;
2929
use mir::transform::{MirSuite, MirPassIndex};
3030
use session::CompileResult;
@@ -1391,9 +1391,10 @@ define_maps! { <'tcx>
13911391
-> Arc<Vec<(String, DefId, SymbolExportLevel)>>,
13921392
[] fn collect_and_partition_translation_items:
13931393
collect_and_partition_translation_items_node(CrateNum)
1394-
-> (Arc<FxHashSet<TransItem<'tcx>>>, Vec<Arc<CodegenUnit<'tcx>>>),
1394+
-> (Arc<DefIdSet>, Arc<Vec<Arc<CodegenUnit<'tcx>>>>),
13951395
[] fn export_name: ExportName(DefId) -> Option<Symbol>,
13961396
[] fn contains_extern_indicator: ContainsExternIndicator(DefId) -> bool,
1397+
[] fn is_translated_function: IsTranslatedFunction(DefId) -> bool,
13971398
}
13981399

13991400
fn type_param_predicates<'tcx>((item_id, param_id): (DefId, DefId)) -> DepConstructor<'tcx> {

src/librustc_trans/base.rs

+34-14
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use back::write::{self, OngoingCrateTranslation};
3535
use llvm::{ContextRef, ModuleRef, ValueRef, Vector, get_param};
3636
use llvm;
3737
use metadata;
38-
use rustc::hir::def_id::{CrateNum, LOCAL_CRATE};
38+
use rustc::hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
3939
use rustc::middle::lang_items::StartFnLangItem;
4040
use rustc::middle::trans::{Linkage, Visibility};
4141
use rustc::middle::cstore::{EncodedMetadata, EncodedMetadataHashes};
@@ -75,7 +75,7 @@ use trans_item::{TransItem, TransItemExt, DefPathBasedNames};
7575
use type_::Type;
7676
use type_of;
7777
use value::Value;
78-
use rustc::util::nodemap::{NodeSet, FxHashMap, FxHashSet};
78+
use rustc::util::nodemap::{NodeSet, FxHashMap, FxHashSet, DefIdSet};
7979
use CrateInfo;
8080

8181
use libc::c_uint;
@@ -990,8 +990,9 @@ pub fn trans_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
990990

991991
// Run the translation item collector and partition the collected items into
992992
// codegen units.
993-
let (translation_items, codegen_units) =
994-
shared_ccx.tcx().collect_and_partition_translation_items(LOCAL_CRATE);
993+
let codegen_units =
994+
shared_ccx.tcx().collect_and_partition_translation_items(LOCAL_CRATE).1;
995+
let codegen_units = (*codegen_units).clone();
995996

996997
assert!(codegen_units.len() <= 1 || !tcx.sess.lto());
997998

@@ -1076,8 +1077,7 @@ pub fn trans_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
10761077
let ((stats, module), _) =
10771078
tcx.dep_graph.with_task(dep_node,
10781079
AssertDepGraphSafe(&shared_ccx),
1079-
AssertDepGraphSafe((cgu,
1080-
translation_items.clone())),
1080+
AssertDepGraphSafe(cgu),
10811081
module_translation);
10821082
all_stats.extend(stats);
10831083

@@ -1118,13 +1118,12 @@ pub fn trans_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
11181118

11191119
fn module_translation<'a, 'tcx>(
11201120
scx: AssertDepGraphSafe<&SharedCrateContext<'a, 'tcx>>,
1121-
args: AssertDepGraphSafe<(Arc<CodegenUnit<'tcx>>,
1122-
Arc<FxHashSet<TransItem<'tcx>>>)>)
1121+
args: AssertDepGraphSafe<Arc<CodegenUnit<'tcx>>>)
11231122
-> (Stats, ModuleTranslation)
11241123
{
11251124
// FIXME(#40304): We ought to be using the id as a key and some queries, I think.
11261125
let AssertDepGraphSafe(scx) = scx;
1127-
let AssertDepGraphSafe((cgu, crate_trans_items)) = args;
1126+
let AssertDepGraphSafe(cgu) = args;
11281127

11291128
let cgu_name = cgu.name().to_string();
11301129
let cgu_id = cgu.work_product_id();
@@ -1164,7 +1163,7 @@ pub fn trans_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
11641163
}
11651164

11661165
// Instantiate translation items without filling out definitions yet...
1167-
let lcx = LocalCrateContext::new(scx, cgu, crate_trans_items);
1166+
let lcx = LocalCrateContext::new(scx, cgu);
11681167
let module = {
11691168
let ccx = CrateContext::new(scx, &lcx);
11701169
let trans_items = ccx.codegen_unit()
@@ -1353,7 +1352,7 @@ fn assert_symbols_are_distinct<'a, 'tcx, I>(tcx: TyCtxt<'a, 'tcx, 'tcx>, trans_i
13531352
fn collect_and_partition_translation_items<'a, 'tcx>(
13541353
tcx: TyCtxt<'a, 'tcx, 'tcx>,
13551354
cnum: CrateNum,
1356-
) -> (Arc<FxHashSet<TransItem<'tcx>>>, Vec<Arc<CodegenUnit<'tcx>>>)
1355+
) -> (Arc<DefIdSet>, Arc<Vec<Arc<CodegenUnit<'tcx>>>>)
13571356
{
13581357
assert_eq!(cnum, LOCAL_CRATE);
13591358
let time_passes = tcx.sess.time_passes();
@@ -1404,7 +1403,12 @@ fn collect_and_partition_translation_items<'a, 'tcx>(
14041403
assert!(tcx.sess.opts.cg.codegen_units == codegen_units.len() ||
14051404
tcx.sess.opts.debugging_opts.incremental.is_some());
14061405

1407-
let translation_items: FxHashSet<TransItem<'tcx>> = items.iter().cloned().collect();
1406+
let translation_items: DefIdSet = items.iter().filter_map(|trans_item| {
1407+
match *trans_item {
1408+
TransItem::Fn(ref instance) => Some(instance.def_id()),
1409+
_ => None,
1410+
}
1411+
}).collect();
14081412

14091413
if tcx.sess.opts.debugging_opts.print_trans_items.is_some() {
14101414
let mut item_to_cgus = FxHashMap();
@@ -1459,7 +1463,7 @@ fn collect_and_partition_translation_items<'a, 'tcx>(
14591463
}
14601464
}
14611465

1462-
(Arc::new(translation_items), codegen_units)
1466+
(Arc::new(translation_items), Arc::new(codegen_units))
14631467
}
14641468

14651469
impl CrateInfo {
@@ -1505,9 +1509,25 @@ impl CrateInfo {
15051509
}
15061510
}
15071511

1508-
pub fn provide(providers: &mut Providers) {
1512+
fn is_translated_function(tcx: TyCtxt, id: DefId) -> bool {
1513+
// FIXME(#42293) needs red/green tracking to avoid failing a bunch of
1514+
// existing tests
1515+
tcx.dep_graph.with_ignore(|| {
1516+
let (all_trans_items, _) =
1517+
tcx.collect_and_partition_translation_items(LOCAL_CRATE);
1518+
all_trans_items.contains(&id)
1519+
})
1520+
}
1521+
1522+
pub fn provide_local(providers: &mut Providers) {
15091523
providers.collect_and_partition_translation_items =
15101524
collect_and_partition_translation_items;
1525+
1526+
providers.is_translated_function = is_translated_function;
1527+
}
1528+
1529+
pub fn provide_extern(providers: &mut Providers) {
1530+
providers.is_translated_function = is_translated_function;
15111531
}
15121532

15131533
pub fn linkage_to_llvm(linkage: Linkage) -> llvm::Linkage {

src/librustc_trans/callee.rs

+34-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ use monomorphize::{self, Instance};
2323
use rustc::hir::def_id::DefId;
2424
use rustc::ty::TypeFoldable;
2525
use rustc::ty::subst::Substs;
26-
use trans_item::TransItem;
2726
use type_of;
2827

2928
/// Translates a reference to a fn/method item, monomorphizing and
@@ -109,10 +108,43 @@ pub fn get_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
109108
attributes::unwind(llfn, true);
110109
}
111110

111+
// Apply an appropriate linkage/visibility value to our item that we
112+
// just declared.
113+
//
114+
// This is sort of subtle. Inside our codegen unit we started off
115+
// compilation by predefining all our own `TransItem` instances. That
116+
// is, everything we're translating ourselves is already defined. That
117+
// means that anything we're actually translating ourselves will have
118+
// hit the above branch in `get_declared_value`. As a result, we're
119+
// guaranteed here that we're declaring a symbol that won't get defined,
120+
// or in other words we're referencing a foreign value.
121+
//
122+
// So because this is a foreign value we blanket apply an external
123+
// linkage directive because it's coming from a different object file.
124+
// The visibility here is where it gets tricky. This symbol could be
125+
// referencing some foreign crate or foreign library (an `extern`
126+
// block) in which case we want to leave the default visibility. We may
127+
// also, though, have multiple codegen units.
128+
//
129+
// In the situation of multiple codegen units this function may be
130+
// referencing a function from another codegen unit. If we're
131+
// indeed referencing a symbol in another codegen unit then we're in one
132+
// of two cases:
133+
//
134+
// * This is a symbol defined in a foreign crate and we're just
135+
// monomorphizing in another codegen unit. In this case this symbols
136+
// is for sure not exported, so both codegen units will be using
137+
// hidden visibility. Hence, we apply a hidden visibility here.
138+
//
139+
// * This is a symbol defined in our local crate. If the symbol in the
140+
// other codegen unit is also not exported then like with the foreign
141+
// case we apply a hidden visibility. If the symbol is exported from
142+
// the foreign object file, however, then we leave this at the
143+
// default visibility as we'll just import it naturally.
112144
unsafe {
113145
llvm::LLVMRustSetLinkage(llfn, llvm::Linkage::ExternalLinkage);
114146

115-
if ccx.crate_trans_items().contains(&TransItem::Fn(instance)) {
147+
if ccx.tcx().is_translated_function(instance_def_id) {
116148
if instance_def_id.is_local() {
117149
if !ccx.tcx().is_exported_symbol(instance_def_id) {
118150
llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden);

src/librustc_trans/context.rs

+2-12
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,13 @@ use declare;
2222
use monomorphize::Instance;
2323

2424
use partitioning::CodegenUnit;
25-
use trans_item::TransItem;
2625
use type_::Type;
2726
use rustc_data_structures::base_n;
2827
use rustc::session::config::{self, NoDebugInfo, OutputFilenames};
2928
use rustc::session::Session;
3029
use rustc::ty::{self, Ty, TyCtxt};
3130
use rustc::ty::layout::{LayoutCx, LayoutError, LayoutTyper, TyLayout};
32-
use rustc::util::nodemap::{FxHashMap, FxHashSet};
31+
use rustc::util::nodemap::FxHashMap;
3332

3433
use std::ffi::{CStr, CString};
3534
use std::cell::{Cell, RefCell};
@@ -94,9 +93,6 @@ pub struct LocalCrateContext<'a, 'tcx: 'a> {
9493
stats: Stats,
9594
codegen_unit: Arc<CodegenUnit<'tcx>>,
9695

97-
/// The translation items of the whole crate.
98-
crate_trans_items: Arc<FxHashSet<TransItem<'tcx>>>,
99-
10096
/// Cache instances of monomorphic and polymorphic items
10197
instances: RefCell<FxHashMap<Instance<'tcx>, ValueRef>>,
10298
/// Cache generated vtables
@@ -349,8 +345,7 @@ impl<'b, 'tcx> SharedCrateContext<'b, 'tcx> {
349345

350346
impl<'a, 'tcx> LocalCrateContext<'a, 'tcx> {
351347
pub fn new(shared: &SharedCrateContext<'a, 'tcx>,
352-
codegen_unit: Arc<CodegenUnit<'tcx>>,
353-
crate_trans_items: Arc<FxHashSet<TransItem<'tcx>>>)
348+
codegen_unit: Arc<CodegenUnit<'tcx>>)
354349
-> LocalCrateContext<'a, 'tcx> {
355350
unsafe {
356351
// Append ".rs" to LLVM module identifier.
@@ -382,7 +377,6 @@ impl<'a, 'tcx> LocalCrateContext<'a, 'tcx> {
382377
llcx,
383378
stats: Stats::default(),
384379
codegen_unit,
385-
crate_trans_items,
386380
instances: RefCell::new(FxHashMap()),
387381
vtables: RefCell::new(FxHashMap()),
388382
const_cstr_cache: RefCell::new(FxHashMap()),
@@ -489,10 +483,6 @@ impl<'b, 'tcx> CrateContext<'b, 'tcx> {
489483
&self.local().codegen_unit
490484
}
491485

492-
pub fn crate_trans_items(&self) -> &FxHashSet<TransItem<'tcx>> {
493-
&self.local().crate_trans_items
494-
}
495-
496486
pub fn td(&self) -> llvm::TargetDataRef {
497487
unsafe { llvm::LLVMRustGetModuleDataLayout(self.llmod()) }
498488
}

src/librustc_trans/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -251,10 +251,11 @@ __build_diagnostic_array! { librustc_trans, DIAGNOSTICS }
251251
pub fn provide_local(providers: &mut Providers) {
252252
back::symbol_names::provide(providers);
253253
back::symbol_export::provide_local(providers);
254-
base::provide(providers);
254+
base::provide_local(providers);
255255
}
256256

257257
pub fn provide_extern(providers: &mut Providers) {
258258
back::symbol_names::provide(providers);
259259
back::symbol_export::provide_extern(providers);
260+
base::provide_extern(providers);
260261
}

0 commit comments

Comments
 (0)