Skip to content

Commit d86eb78

Browse files
committed
Parallelize trans item collection
1 parent 71c26b3 commit d86eb78

File tree

2 files changed

+27
-18
lines changed

2 files changed

+27
-18
lines changed

src/librustc_mir/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ Rust MIR: a lowered representation of Rust. Also: an experiment!
3434
#![feature(specialization)]
3535
#![feature(try_trait)]
3636

37+
#![recursion_limit="256"]
38+
3739
extern crate arena;
40+
3841
#[macro_use]
3942
extern crate bitflags;
4043
#[macro_use] extern crate log;

src/librustc_mir/monomorphize/collector.rs

+24-18
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,12 @@ use rustc::mir::interpret::{Scalar, GlobalId, AllocType};
207207

208208
use monomorphize::{self, Instance};
209209
use rustc::util::nodemap::{FxHashSet, FxHashMap, DefIdMap};
210+
use rustc::util::common::time;
210211

211212
use monomorphize::item::{MonoItemExt, DefPathBasedNames, InstantiationMode};
212213

213214
use rustc_data_structures::bitvec::BitVector;
215+
use rustc_data_structures::sync::{ParallelIterator, par_iter, Lock};
214216

215217
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)]
216218
pub enum MonoItemCollectionMode {
@@ -298,22 +300,26 @@ pub fn collect_crate_mono_items<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
298300
mode: MonoItemCollectionMode)
299301
-> (FxHashSet<MonoItem<'tcx>>,
300302
InliningMap<'tcx>) {
301-
let roots = collect_roots(tcx, mode);
303+
let roots = time(tcx.sess, "collecting roots", || {
304+
collect_roots(tcx, mode)
305+
});
302306

303307
debug!("Building mono item graph, beginning at roots");
304-
let mut visited = FxHashSet();
305-
let mut recursion_depths = DefIdMap();
306-
let mut inlining_map = InliningMap::new();
307-
308-
for root in roots {
309-
collect_items_rec(tcx,
310-
root,
311-
&mut visited,
312-
&mut recursion_depths,
313-
&mut inlining_map);
314-
}
308+
let visited = Lock::new(FxHashSet());
309+
let inlining_map = Lock::new(InliningMap::new());
310+
311+
time(tcx.sess, "collecting mono items", || {
312+
par_iter(roots).for_each(|root| {
313+
let mut recursion_depths = DefIdMap();
314+
collect_items_rec(tcx,
315+
root,
316+
&visited,
317+
&mut recursion_depths,
318+
&inlining_map);
319+
});
320+
});
315321

316-
(visited, inlining_map)
322+
(visited.into_inner(), inlining_map.into_inner())
317323
}
318324

319325
// Find all non-generic items by walking the HIR. These items serve as roots to
@@ -354,10 +360,10 @@ fn collect_roots<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
354360
// Collect all monomorphized items reachable from `starting_point`
355361
fn collect_items_rec<'a, 'tcx: 'a>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
356362
starting_point: MonoItem<'tcx>,
357-
visited: &mut FxHashSet<MonoItem<'tcx>>,
363+
visited: &Lock<FxHashSet<MonoItem<'tcx>>>,
358364
recursion_depths: &mut DefIdMap<usize>,
359-
inlining_map: &mut InliningMap<'tcx>) {
360-
if !visited.insert(starting_point.clone()) {
365+
inlining_map: &Lock<InliningMap<'tcx>>) {
366+
if !visited.lock().insert(starting_point.clone()) {
361367
// We've been here already, no need to search again.
362368
return;
363369
}
@@ -428,7 +434,7 @@ fn collect_items_rec<'a, 'tcx: 'a>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
428434
fn record_accesses<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
429435
caller: MonoItem<'tcx>,
430436
callees: &[MonoItem<'tcx>],
431-
inlining_map: &mut InliningMap<'tcx>) {
437+
inlining_map: &Lock<InliningMap<'tcx>>) {
432438
let is_inlining_candidate = |mono_item: &MonoItem<'tcx>| {
433439
mono_item.instantiation_mode(tcx) == InstantiationMode::LocalCopy
434440
};
@@ -438,7 +444,7 @@ fn record_accesses<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
438444
(*mono_item, is_inlining_candidate(mono_item))
439445
});
440446

441-
inlining_map.record_accesses(caller, accesses);
447+
inlining_map.lock().record_accesses(caller, accesses);
442448
}
443449

444450
fn check_recursion_limit<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,

0 commit comments

Comments
 (0)