Skip to content

Commit

Permalink
Parallelize trans item collection
Browse files Browse the repository at this point in the history
  • Loading branch information
Zoxc committed Jun 19, 2018
1 parent 71c26b3 commit d86eb78
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
3 changes: 3 additions & 0 deletions src/librustc_mir/lib.rs
Expand Up @@ -34,7 +34,10 @@ Rust MIR: a lowered representation of Rust. Also: an experiment!
#![feature(specialization)]
#![feature(try_trait)]

#![recursion_limit="256"]

extern crate arena;

#[macro_use]
extern crate bitflags;
#[macro_use] extern crate log;
Expand Down
42 changes: 24 additions & 18 deletions src/librustc_mir/monomorphize/collector.rs
Expand Up @@ -207,10 +207,12 @@ use rustc::mir::interpret::{Scalar, GlobalId, AllocType};

use monomorphize::{self, Instance};
use rustc::util::nodemap::{FxHashSet, FxHashMap, DefIdMap};
use rustc::util::common::time;

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

use rustc_data_structures::bitvec::BitVector;
use rustc_data_structures::sync::{ParallelIterator, par_iter, Lock};

#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)]
pub enum MonoItemCollectionMode {
Expand Down Expand Up @@ -298,22 +300,26 @@ pub fn collect_crate_mono_items<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
mode: MonoItemCollectionMode)
-> (FxHashSet<MonoItem<'tcx>>,
InliningMap<'tcx>) {
let roots = collect_roots(tcx, mode);
let roots = time(tcx.sess, "collecting roots", || {
collect_roots(tcx, mode)
});

debug!("Building mono item graph, beginning at roots");
let mut visited = FxHashSet();
let mut recursion_depths = DefIdMap();
let mut inlining_map = InliningMap::new();

for root in roots {
collect_items_rec(tcx,
root,
&mut visited,
&mut recursion_depths,
&mut inlining_map);
}
let visited = Lock::new(FxHashSet());
let inlining_map = Lock::new(InliningMap::new());

time(tcx.sess, "collecting mono items", || {
par_iter(roots).for_each(|root| {
let mut recursion_depths = DefIdMap();
collect_items_rec(tcx,
root,
&visited,
&mut recursion_depths,
&inlining_map);
});
});

(visited, inlining_map)
(visited.into_inner(), inlining_map.into_inner())
}

// Find all non-generic items by walking the HIR. These items serve as roots to
Expand Down Expand Up @@ -354,10 +360,10 @@ fn collect_roots<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
// Collect all monomorphized items reachable from `starting_point`
fn collect_items_rec<'a, 'tcx: 'a>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
starting_point: MonoItem<'tcx>,
visited: &mut FxHashSet<MonoItem<'tcx>>,
visited: &Lock<FxHashSet<MonoItem<'tcx>>>,
recursion_depths: &mut DefIdMap<usize>,
inlining_map: &mut InliningMap<'tcx>) {
if !visited.insert(starting_point.clone()) {
inlining_map: &Lock<InliningMap<'tcx>>) {
if !visited.lock().insert(starting_point.clone()) {
// We've been here already, no need to search again.
return;
}
Expand Down Expand Up @@ -428,7 +434,7 @@ fn collect_items_rec<'a, 'tcx: 'a>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
fn record_accesses<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
caller: MonoItem<'tcx>,
callees: &[MonoItem<'tcx>],
inlining_map: &mut InliningMap<'tcx>) {
inlining_map: &Lock<InliningMap<'tcx>>) {
let is_inlining_candidate = |mono_item: &MonoItem<'tcx>| {
mono_item.instantiation_mode(tcx) == InstantiationMode::LocalCopy
};
Expand All @@ -438,7 +444,7 @@ fn record_accesses<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
(*mono_item, is_inlining_candidate(mono_item))
});

inlining_map.record_accesses(caller, accesses);
inlining_map.lock().record_accesses(caller, accesses);
}

fn check_recursion_limit<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
Expand Down

0 comments on commit d86eb78

Please sign in to comment.