Skip to content

Commit 8e26983

Browse files
committed
pull stats out of SharedCrateContext
shared mutable state is bad
1 parent f227187 commit 8e26983

File tree

2 files changed

+64
-47
lines changed

2 files changed

+64
-47
lines changed

src/librustc_trans/base.rs

+40-30
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ use common::CrateContext;
5757
use common::{type_is_zero_size, val_ty};
5858
use common;
5959
use consts;
60-
use context::{self, LocalCrateContext, SharedCrateContext};
60+
use context::{self, LocalCrateContext, SharedCrateContext, Stats};
6161
use debuginfo;
6262
use declare;
6363
use machine;
@@ -1115,21 +1115,25 @@ pub fn trans_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
11151115

11161116
let symbol_map = Rc::new(symbol_map);
11171117

1118+
let mut all_stats = Stats::default();
11181119
let modules: Vec<ModuleTranslation> = codegen_units
11191120
.into_iter()
11201121
.map(|cgu| {
11211122
let dep_node = cgu.work_product_dep_node();
1122-
tcx.dep_graph.with_task(dep_node,
1123-
AssertDepGraphSafe(&shared_ccx),
1124-
AssertDepGraphSafe((cgu, symbol_map.clone())),
1125-
module_translation)
1123+
let (stats, module) =
1124+
tcx.dep_graph.with_task(dep_node,
1125+
AssertDepGraphSafe(&shared_ccx),
1126+
AssertDepGraphSafe((cgu, symbol_map.clone())),
1127+
module_translation);
1128+
all_stats.extend(stats);
1129+
module
11261130
})
11271131
.collect();
11281132

11291133
fn module_translation<'a, 'tcx>(
11301134
scx: AssertDepGraphSafe<&SharedCrateContext<'a, 'tcx>>,
11311135
args: AssertDepGraphSafe<(CodegenUnit<'tcx>, Rc<SymbolMap<'tcx>>)>)
1132-
-> ModuleTranslation
1136+
-> (Stats, ModuleTranslation)
11331137
{
11341138
// FIXME(#40304): We ought to be using the id as a key and some queries, I think.
11351139
let AssertDepGraphSafe(scx) = scx;
@@ -1161,12 +1165,19 @@ pub fn trans_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
11611165
}
11621166
});
11631167

1164-
let source = if let Some(buf) = previous_work_product {
1168+
if let Some(buf) = previous_work_product {
11651169
// Don't need to translate this module.
1166-
ModuleSource::Preexisting(buf.clone())
1167-
} else {
1168-
// Instantiate translation items without filling out definitions yet...
1169-
let lcx = LocalCrateContext::new(scx, cgu, symbol_map.clone());
1170+
let module = ModuleTranslation {
1171+
name: cgu_name,
1172+
symbol_name_hash,
1173+
source: ModuleSource::Preexisting(buf.clone())
1174+
};
1175+
return (Stats::default(), module);
1176+
}
1177+
1178+
// Instantiate translation items without filling out definitions yet...
1179+
let lcx = LocalCrateContext::new(scx, cgu, symbol_map.clone());
1180+
let module = {
11701181
let ccx = CrateContext::new(scx, &lcx);
11711182
let trans_items = ccx.codegen_unit()
11721183
.items_in_deterministic_order(ccx.tcx(), &symbol_map);
@@ -1214,38 +1225,37 @@ pub fn trans_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
12141225
debuginfo::finalize(&ccx);
12151226
}
12161227

1217-
ModuleSource::Translated(ModuleLlvm {
1218-
llcx: ccx.llcx(),
1219-
llmod: ccx.llmod(),
1220-
})
1228+
ModuleTranslation {
1229+
name: cgu_name,
1230+
symbol_name_hash,
1231+
source: ModuleSource::Translated(ModuleLlvm {
1232+
llcx: ccx.llcx(),
1233+
llmod: ccx.llmod(),
1234+
})
1235+
}
12211236
};
12221237

1223-
ModuleTranslation {
1224-
name: cgu_name,
1225-
symbol_name_hash,
1226-
source,
1227-
}
1238+
(lcx.into_stats(), module)
12281239
}
12291240

12301241
assert_module_sources::assert_module_sources(tcx, &modules);
12311242

12321243
symbol_names_test::report_symbol_names(&shared_ccx);
12331244

12341245
if shared_ccx.sess().trans_stats() {
1235-
let stats = shared_ccx.stats();
12361246
println!("--- trans stats ---");
1237-
println!("n_glues_created: {}", stats.n_glues_created.get());
1238-
println!("n_null_glues: {}", stats.n_null_glues.get());
1239-
println!("n_real_glues: {}", stats.n_real_glues.get());
1247+
println!("n_glues_created: {}", all_stats.n_glues_created.get());
1248+
println!("n_null_glues: {}", all_stats.n_null_glues.get());
1249+
println!("n_real_glues: {}", all_stats.n_real_glues.get());
12401250

1241-
println!("n_fns: {}", stats.n_fns.get());
1242-
println!("n_inlines: {}", stats.n_inlines.get());
1243-
println!("n_closures: {}", stats.n_closures.get());
1251+
println!("n_fns: {}", all_stats.n_fns.get());
1252+
println!("n_inlines: {}", all_stats.n_inlines.get());
1253+
println!("n_closures: {}", all_stats.n_closures.get());
12441254
println!("fn stats:");
1245-
stats.fn_stats.borrow_mut().sort_by(|&(_, insns_a), &(_, insns_b)| {
1255+
all_stats.fn_stats.borrow_mut().sort_by(|&(_, insns_a), &(_, insns_b)| {
12461256
insns_b.cmp(&insns_a)
12471257
});
1248-
for tuple in stats.fn_stats.borrow().iter() {
1258+
for tuple in all_stats.fn_stats.borrow().iter() {
12491259
match *tuple {
12501260
(ref name, insns) => {
12511261
println!("{} insns, {}", insns, *name);
@@ -1255,7 +1265,7 @@ pub fn trans_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
12551265
}
12561266

12571267
if shared_ccx.sess().count_llvm_insns() {
1258-
for (k, v) in shared_ccx.stats().llvm_insns.borrow().iter() {
1268+
for (k, v) in all_stats.llvm_insns.borrow().iter() {
12591269
println!("{:7} {}", *v, *k);
12601270
}
12611271
}

src/librustc_trans/context.rs

+24-17
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ use syntax::symbol::InternedString;
4545
use syntax_pos::DUMMY_SP;
4646
use abi::Abi;
4747

48+
#[derive(Clone, Default)]
4849
pub struct Stats {
4950
pub n_glues_created: Cell<usize>,
5051
pub n_null_glues: Cell<usize>,
@@ -58,6 +59,22 @@ pub struct Stats {
5859
pub fn_stats: RefCell<Vec<(String, usize)> >,
5960
}
6061

62+
impl Stats {
63+
pub fn extend(&mut self, stats: Stats) {
64+
self.n_glues_created.set(self.n_glues_created.get() + stats.n_glues_created.get());
65+
self.n_null_glues.set(self.n_null_glues.get() + stats.n_null_glues.get());
66+
self.n_real_glues.set(self.n_real_glues.get() + stats.n_real_glues.get());
67+
self.n_fns.set(self.n_fns.get() + stats.n_fns.get());
68+
self.n_inlines.set(self.n_inlines.get() + stats.n_inlines.get());
69+
self.n_closures.set(self.n_closures.get() + stats.n_closures.get());
70+
self.n_llvm_insns.set(self.n_llvm_insns.get() + stats.n_llvm_insns.get());
71+
self.llvm_insns.borrow_mut().extend(
72+
stats.llvm_insns.borrow().iter()
73+
.map(|(key, value)| (key.clone(), value.clone())));
74+
self.fn_stats.borrow_mut().append(&mut *stats.fn_stats.borrow_mut());
75+
}
76+
}
77+
6178
/// The shared portion of a `CrateContext`. There is one `SharedCrateContext`
6279
/// per crate. The data here is shared between all compilation units of the
6380
/// crate, so it must not contain references to any LLVM data structures
@@ -66,7 +83,6 @@ pub struct SharedCrateContext<'a, 'tcx: 'a> {
6683
exported_symbols: NodeSet,
6784
tcx: TyCtxt<'a, 'tcx, 'tcx>,
6885
empty_param_env: ty::ParameterEnvironment<'tcx>,
69-
stats: Stats,
7086
check_overflow: bool,
7187

7288
use_dll_storage_attrs: bool,
@@ -83,6 +99,7 @@ pub struct SharedCrateContext<'a, 'tcx: 'a> {
8399
pub struct LocalCrateContext<'tcx> {
84100
llmod: ModuleRef,
85101
llcx: ContextRef,
102+
stats: Stats,
86103
codegen_unit: CodegenUnit<'tcx>,
87104
needs_unwind_cleanup_cache: RefCell<FxHashMap<Ty<'tcx>, bool>>,
88105
/// Cache instances of monomorphic and polymorphic items
@@ -366,17 +383,6 @@ impl<'b, 'tcx> SharedCrateContext<'b, 'tcx> {
366383
exported_symbols: exported_symbols,
367384
empty_param_env: tcx.empty_parameter_environment(),
368385
tcx: tcx,
369-
stats: Stats {
370-
n_glues_created: Cell::new(0),
371-
n_null_glues: Cell::new(0),
372-
n_real_glues: Cell::new(0),
373-
n_fns: Cell::new(0),
374-
n_inlines: Cell::new(0),
375-
n_closures: Cell::new(0),
376-
n_llvm_insns: Cell::new(0),
377-
llvm_insns: RefCell::new(FxHashMap()),
378-
fn_stats: RefCell::new(Vec::new()),
379-
},
380386
check_overflow: check_overflow,
381387
use_dll_storage_attrs: use_dll_storage_attrs,
382388
translation_items: RefCell::new(FxHashSet()),
@@ -417,10 +423,6 @@ impl<'b, 'tcx> SharedCrateContext<'b, 'tcx> {
417423
&self.tcx.dep_graph
418424
}
419425

420-
pub fn stats<'a>(&'a self) -> &'a Stats {
421-
&self.stats
422-
}
423-
424426
pub fn use_dll_storage_attrs(&self) -> bool {
425427
self.use_dll_storage_attrs
426428
}
@@ -466,6 +468,7 @@ impl<'tcx> LocalCrateContext<'tcx> {
466468
let local_ccx = LocalCrateContext {
467469
llmod: llmod,
468470
llcx: llcx,
471+
stats: Stats::default(),
469472
codegen_unit: codegen_unit,
470473
needs_unwind_cleanup_cache: RefCell::new(FxHashMap()),
471474
instances: RefCell::new(FxHashMap()),
@@ -537,6 +540,10 @@ impl<'tcx> LocalCrateContext<'tcx> {
537540
local_ccx: &local_ccxs[0]
538541
}
539542
}
543+
544+
pub fn into_stats(self) -> Stats {
545+
self.stats
546+
}
540547
}
541548

542549
impl<'b, 'tcx> CrateContext<'b, 'tcx> {
@@ -651,7 +658,7 @@ impl<'b, 'tcx> CrateContext<'b, 'tcx> {
651658
}
652659

653660
pub fn stats<'a>(&'a self) -> &'a Stats {
654-
&self.shared.stats
661+
&self.local().stats
655662
}
656663

657664
pub fn int_type(&self) -> Type {

0 commit comments

Comments
 (0)