Skip to content

Commit e8a76d8

Browse files
committed
Auto merge of #44529 - alexcrichton:trans-query, r=michaelwoerister
Refactor translation unit partitioning/collection as a query This commit is targeted at #44486 with the ultimate goal of making the `collect_and_partition_translation_items` function a query. This mostly just involved query-ifying a few other systems along with plumbing the tcx instead of `SharedCrateContext` in a few locations. Currently this only tackles the first bullet of #44486 and doesn't add a dedicated query for a particular codegen unit. I wasn't quite sure how to do that yet but figured this was good to put up. Closes #44486
2 parents cfcac37 + 6d614dd commit e8a76d8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1452
-1247
lines changed

src/librustc/dep_graph/dep_node.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ use rustc_data_structures::stable_hasher::{StableHasher, HashStable};
7171
use ich::StableHashingContext;
7272
use std::fmt;
7373
use std::hash::Hash;
74+
use syntax_pos::symbol::InternedString;
7475

7576
// erase!() just makes tokens go away. It's used to specify which macro argument
7677
// is repeated (i.e. which sub-expression of the macro we are in) but don't need
@@ -535,7 +536,7 @@ define_dep_nodes!( <'tcx>
535536
[] GetPanicStrategy(CrateNum),
536537
[] IsNoBuiltins(CrateNum),
537538
[] ImplDefaultness(DefId),
538-
[] ExportedSymbols(CrateNum),
539+
[] ExportedSymbolIds(CrateNum),
539540
[] NativeLibraries(CrateNum),
540541
[] PluginRegistrarFn(CrateNum),
541542
[] DeriveRegistrarFn(CrateNum),
@@ -575,6 +576,14 @@ define_dep_nodes!( <'tcx>
575576
[] MaybeUnusedExternCrates,
576577
[] StabilityIndex,
577578
[] AllCrateNums,
579+
[] ExportedSymbols(CrateNum),
580+
[] CollectAndPartitionTranslationItems,
581+
[] ExportName(DefId),
582+
[] ContainsExternIndicator(DefId),
583+
[] IsTranslatedFunction(DefId),
584+
[] CodegenUnit(InternedString),
585+
[] CompileCodegenUnit(InternedString),
586+
[] OutputFilenames,
578587
);
579588

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

src/librustc/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ pub mod middle {
9292
pub mod dependency_format;
9393
pub mod effect;
9494
pub mod entry;
95+
pub mod exported_symbols;
9596
pub mod free_region;
9697
pub mod intrinsicck;
9798
pub mod lang_items;
@@ -103,6 +104,7 @@ pub mod middle {
103104
pub mod recursion_limit;
104105
pub mod resolve_lifetime;
105106
pub mod stability;
107+
pub mod trans;
106108
pub mod weak_lang_items;
107109
}
108110

src/librustc/middle/cstore.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -366,8 +366,9 @@ pub trait CrateLoader {
366366
// In order to get this left-to-right dependency ordering, we perform a
367367
// topological sort of all crates putting the leaves at the right-most
368368
// positions.
369-
pub fn used_crates<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
370-
prefer: LinkagePreference) -> Vec<(CrateNum, LibSource)> {
369+
pub fn used_crates(tcx: TyCtxt, prefer: LinkagePreference)
370+
-> Vec<(CrateNum, LibSource)>
371+
{
371372
let mut libs = tcx.crates()
372373
.iter()
373374
.cloned()
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
/// The SymbolExportLevel of a symbols specifies from which kinds of crates
12+
/// the symbol will be exported. `C` symbols will be exported from any
13+
/// kind of crate, including cdylibs which export very few things.
14+
/// `Rust` will only be exported if the crate produced is a Rust
15+
/// dylib.
16+
#[derive(Eq, PartialEq, Debug, Copy, Clone)]
17+
pub enum SymbolExportLevel {
18+
C,
19+
Rust,
20+
}
21+
22+
impl SymbolExportLevel {
23+
pub fn is_below_threshold(self, threshold: SymbolExportLevel) -> bool {
24+
if threshold == SymbolExportLevel::Rust {
25+
// We export everything from Rust dylibs
26+
true
27+
} else {
28+
self == SymbolExportLevel::C
29+
}
30+
}
31+
}

src/librustc/middle/reachable.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,8 @@ impl<'a, 'tcx> ReachableContext<'a, 'tcx> {
233233
} else {
234234
false
235235
};
236-
let is_extern = attr::contains_extern_indicator(&self.tcx.sess.diagnostic(),
237-
&item.attrs);
236+
let def_id = self.tcx.hir.local_def_id(item.id);
237+
let is_extern = self.tcx.contains_extern_indicator(def_id);
238238
if reachable || is_extern {
239239
self.reachable_symbols.insert(search_item);
240240
}
@@ -369,10 +369,6 @@ impl<'a, 'tcx: 'a> ItemLikeVisitor<'tcx> for CollectPrivateImplItemsVisitor<'a,
369369
}
370370
}
371371

372-
pub fn find_reachable<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Rc<NodeSet> {
373-
tcx.reachable_set(LOCAL_CRATE)
374-
}
375-
376372
fn reachable_set<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, crate_num: CrateNum) -> Rc<NodeSet> {
377373
debug_assert!(crate_num == LOCAL_CRATE);
378374

src/librustc/middle/trans.rs

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use syntax::ast::NodeId;
12+
use syntax::symbol::InternedString;
13+
use ty::Instance;
14+
use util::nodemap::FxHashMap;
15+
16+
#[derive(PartialEq, Eq, Clone, Copy, Debug, Hash)]
17+
pub enum TransItem<'tcx> {
18+
Fn(Instance<'tcx>),
19+
Static(NodeId),
20+
GlobalAsm(NodeId),
21+
}
22+
23+
pub struct CodegenUnit<'tcx> {
24+
/// A name for this CGU. Incremental compilation requires that
25+
/// name be unique amongst **all** crates. Therefore, it should
26+
/// contain something unique to this crate (e.g., a module path)
27+
/// as well as the crate name and disambiguator.
28+
name: InternedString,
29+
items: FxHashMap<TransItem<'tcx>, (Linkage, Visibility)>,
30+
}
31+
32+
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
33+
pub enum Linkage {
34+
External,
35+
AvailableExternally,
36+
LinkOnceAny,
37+
LinkOnceODR,
38+
WeakAny,
39+
WeakODR,
40+
Appending,
41+
Internal,
42+
Private,
43+
ExternalWeak,
44+
Common,
45+
}
46+
47+
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
48+
pub enum Visibility {
49+
Default,
50+
Hidden,
51+
Protected,
52+
}
53+
54+
impl<'tcx> CodegenUnit<'tcx> {
55+
pub fn new(name: InternedString) -> CodegenUnit<'tcx> {
56+
CodegenUnit {
57+
name: name,
58+
items: FxHashMap(),
59+
}
60+
}
61+
62+
pub fn name(&self) -> &InternedString {
63+
&self.name
64+
}
65+
66+
pub fn set_name(&mut self, name: InternedString) {
67+
self.name = name;
68+
}
69+
70+
pub fn items(&self) -> &FxHashMap<TransItem<'tcx>, (Linkage, Visibility)> {
71+
&self.items
72+
}
73+
74+
pub fn items_mut(&mut self)
75+
-> &mut FxHashMap<TransItem<'tcx>, (Linkage, Visibility)>
76+
{
77+
&mut self.items
78+
}
79+
}
80+
81+
#[derive(Clone, Default)]
82+
pub struct Stats {
83+
pub n_glues_created: usize,
84+
pub n_null_glues: usize,
85+
pub n_real_glues: usize,
86+
pub n_fns: usize,
87+
pub n_inlines: usize,
88+
pub n_closures: usize,
89+
pub n_llvm_insns: usize,
90+
pub llvm_insns: FxHashMap<String, usize>,
91+
// (ident, llvm-instructions)
92+
pub fn_stats: Vec<(String, usize)>,
93+
}
94+
95+
impl Stats {
96+
pub fn extend(&mut self, stats: Stats) {
97+
self.n_glues_created += stats.n_glues_created;
98+
self.n_null_glues += stats.n_null_glues;
99+
self.n_real_glues += stats.n_real_glues;
100+
self.n_fns += stats.n_fns;
101+
self.n_inlines += stats.n_inlines;
102+
self.n_closures += stats.n_closures;
103+
self.n_llvm_insns += stats.n_llvm_insns;
104+
105+
for (k, v) in stats.llvm_insns {
106+
*self.llvm_insns.entry(k).or_insert(0) += v;
107+
}
108+
self.fn_stats.extend(stats.fn_stats);
109+
}
110+
}

src/librustc/ty/context.rs

+21
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use dep_graph::DepGraph;
1414
use errors::DiagnosticBuilder;
1515
use session::Session;
16+
use session::config::OutputFilenames;
1617
use middle;
1718
use hir::{TraitCandidate, HirId, ItemLocalId};
1819
use hir::def::{Def, Export};
@@ -64,6 +65,8 @@ use std::mem;
6465
use std::ops::Deref;
6566
use std::iter;
6667
use std::rc::Rc;
68+
use std::sync::mpsc;
69+
use std::sync::Arc;
6770
use syntax::abi;
6871
use syntax::ast::{self, Name, NodeId};
6972
use syntax::attr;
@@ -901,6 +904,16 @@ pub struct GlobalCtxt<'tcx> {
901904
/// error reporting, and so is lazily initialized and generally
902905
/// shouldn't taint the common path (hence the RefCell).
903906
pub all_traits: RefCell<Option<Vec<DefId>>>,
907+
908+
/// A general purpose channel to throw data out the back towards LLVM worker
909+
/// threads.
910+
///
911+
/// This is intended to only get used during the trans phase of the compiler
912+
/// when satisfying the query for a particular codegen unit. Internally in
913+
/// the query it'll send data along this channel to get processed later.
914+
pub tx_to_llvm_workers: mpsc::Sender<Box<Any + Send>>,
915+
916+
output_filenames: Arc<OutputFilenames>,
904917
}
905918

906919
impl<'tcx> GlobalCtxt<'tcx> {
@@ -1025,6 +1038,8 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
10251038
named_region_map: resolve_lifetime::NamedRegionMap,
10261039
hir: hir_map::Map<'tcx>,
10271040
crate_name: &str,
1041+
tx: mpsc::Sender<Box<Any + Send>>,
1042+
output_filenames: &OutputFilenames,
10281043
f: F) -> R
10291044
where F: for<'b> FnOnce(TyCtxt<'b, 'tcx, 'tcx>) -> R
10301045
{
@@ -1145,6 +1160,8 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
11451160
derive_macros: RefCell::new(NodeMap()),
11461161
stability_interner: RefCell::new(FxHashSet()),
11471162
all_traits: RefCell::new(None),
1163+
tx_to_llvm_workers: tx,
1164+
output_filenames: Arc::new(output_filenames.clone()),
11481165
}, f)
11491166
}
11501167

@@ -2218,4 +2235,8 @@ pub fn provide(providers: &mut ty::maps::Providers) {
22182235
assert_eq!(cnum, LOCAL_CRATE);
22192236
Rc::new(tcx.cstore.postorder_cnums_untracked())
22202237
};
2238+
providers.output_filenames = |tcx, cnum| {
2239+
assert_eq!(cnum, LOCAL_CRATE);
2240+
tcx.output_filenames.clone()
2241+
};
22212242
}

0 commit comments

Comments
 (0)