Skip to content

Run some stuff in parallel #51383

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/librustc/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ use std::cmp::{self, Ordering};
use std::fmt;
use std::hash::{Hash, Hasher};
use std::ops::Deref;
use rustc_data_structures::sync::Lrc;
use rustc_data_structures::sync::{self, Lrc, ParallelIterator, par_iter};
use std::slice;
use std::vec::IntoIter;
use std::mem;
Expand Down Expand Up @@ -2436,6 +2436,12 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
.map(move |&body_id| self.hir.body_owner_def_id(body_id))
}

pub fn par_body_owners<F: Fn(DefId) + sync::Sync + sync::Send>(self, f: F) {
par_iter(&self.hir.krate().body_ids).for_each(|&body_id| {
f(self.hir.body_owner_def_id(body_id))
});
}

pub fn expr_span(self, id: NodeId) -> Span {
match self.hir.find(id) {
Some(hir_map::NodeExpr(e)) => {
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_borrowck/borrowck/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ pub struct LoanDataFlowOperator;
pub type LoanDataFlow<'a, 'tcx> = DataFlowContext<'a, 'tcx, LoanDataFlowOperator>;

pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
for body_owner_def_id in tcx.body_owners() {
tcx.par_body_owners(|body_owner_def_id| {
tcx.borrowck(body_owner_def_id);
}
});
}

pub fn provide(providers: &mut Providers) {
Expand Down
2 changes: 2 additions & 0 deletions src/librustc_borrowck/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#![feature(from_ref)]
#![feature(quote)]

#![recursion_limit="256"]

#[macro_use] extern crate log;
extern crate syntax;
extern crate syntax_pos;
Expand Down
45 changes: 37 additions & 8 deletions src/librustc_data_structures/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
//!
//! `MTLock` is a mutex which disappears if cfg!(parallel_queries) is false.
//!
//! `MTRef` is a immutable refernce if cfg!(parallel_queries), and an mutable reference otherwise.
//!
//! `rustc_erase_owner!` erases a OwningRef owner into Erased or Erased + Send + Sync
//! depending on the value of cfg!(parallel_queries).

Expand Down Expand Up @@ -126,6 +128,8 @@ cfg_if! {
}
}

pub type MTRef<'a, T> = &'a mut T;

#[derive(Debug)]
pub struct MTLock<T>(T);

Expand All @@ -151,13 +155,8 @@ cfg_if! {
}

#[inline(always)]
pub fn borrow(&self) -> &T {
&self.0
}

#[inline(always)]
pub fn borrow_mut(&self) -> &T {
&self.0
pub fn lock_mut(&mut self) -> &mut T {
&mut self.0
}
}

Expand Down Expand Up @@ -221,7 +220,37 @@ cfg_if! {
pub use std::sync::Arc as Lrc;
pub use std::sync::Weak as Weak;

pub use self::Lock as MTLock;
pub type MTRef<'a, T> = &'a T;

#[derive(Debug)]
pub struct MTLock<T>(Lock<T>);

impl<T> MTLock<T> {
#[inline(always)]
pub fn new(inner: T) -> Self {
MTLock(Lock::new(inner))
}

#[inline(always)]
pub fn into_inner(self) -> T {
self.0.into_inner()
}

#[inline(always)]
pub fn get_mut(&mut self) -> &mut T {
self.0.get_mut()
}

#[inline(always)]
pub fn lock(&self) -> LockGuard<T> {
self.0.lock()
}

#[inline(always)]
pub fn lock_mut(&self) -> LockGuard<T> {
self.lock()
}
}

use parking_lot::Mutex as InnerLock;
use parking_lot::RwLock as InnerRwLock;
Expand Down
8 changes: 3 additions & 5 deletions src/librustc_driver/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1272,11 +1272,9 @@ where

time(sess, "borrow checking", || borrowck::check_crate(tcx));

time(sess, "MIR borrow checking", || {
for def_id in tcx.body_owners() {
tcx.mir_borrowck(def_id);
}
});
time(sess,
"MIR borrow checking",
|| tcx.par_body_owners(|def_id| { tcx.mir_borrowck(def_id); }));

time(sess, "dumping chalk-like clauses", || {
rustc_traits::lowering::dump_program_clauses(tcx);
Expand Down
2 changes: 2 additions & 0 deletions src/librustc_incremental/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#![feature(fs_read_write)]
#![feature(specialization)]

#![recursion_limit="256"]

extern crate graphviz;
#[macro_use] extern crate rustc;
extern crate rustc_data_structures;
Expand Down
22 changes: 14 additions & 8 deletions src/librustc_incremental/persist/save.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use rustc::session::Session;
use rustc::ty::TyCtxt;
use rustc::util::common::time;
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::sync::join;
use rustc_serialize::Encodable as RustcEncodable;
use rustc_serialize::opaque::Encoder;
use std::io::{self, Cursor};
Expand All @@ -33,23 +34,28 @@ pub fn save_dep_graph<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
return;
}

time(sess, "persist query result cache", || {
save_in(sess,
query_cache_path(sess),
|e| encode_query_cache(tcx, e));
});
let query_cache_path = query_cache_path(sess);
let dep_graph_path = dep_graph_path(sess);

if tcx.sess.opts.debugging_opts.incremental_queries {
join(move || {
if tcx.sess.opts.debugging_opts.incremental_queries {
time(sess, "persist query result cache", || {
save_in(sess,
query_cache_path,
|e| encode_query_cache(tcx, e));
});
}
}, || {
time(sess, "persist dep-graph", || {
save_in(sess,
dep_graph_path(sess),
dep_graph_path,
|e| {
time(sess, "encode dep-graph", || {
encode_dep_graph(tcx, e)
})
});
});
}
});

dirty_clean::check_dirty_clean_annotations(tcx);
})
Expand Down
3 changes: 3 additions & 0 deletions src/librustc_mir/lib.rs
Original file line number Diff line number Diff line change
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
46 changes: 29 additions & 17 deletions src/librustc_mir/monomorphize/collector.rs
Original file line number Diff line number Diff line change
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::{MTRef, MTLock, ParallelIterator, par_iter};

#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)]
pub enum MonoItemCollectionMode {
Expand Down Expand Up @@ -298,22 +300,32 @@ 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 mut visited = MTLock::new(FxHashSet());
let mut inlining_map = MTLock::new(InliningMap::new());

{
let visited: MTRef<'_, _> = &mut visited;
let inlining_map: MTRef<'_, _> = &mut inlining_map;

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 +366,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: MTRef<'_, MTLock<FxHashSet<MonoItem<'tcx>>>>,
recursion_depths: &mut DefIdMap<usize>,
inlining_map: &mut InliningMap<'tcx>) {
if !visited.insert(starting_point.clone()) {
inlining_map: MTRef<'_, MTLock<InliningMap<'tcx>>>) {
if !visited.lock_mut().insert(starting_point.clone()) {
// We've been here already, no need to search again.
return;
}
Expand Down Expand Up @@ -428,7 +440,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: MTRef<'_, MTLock<InliningMap<'tcx>>>) {
let is_inlining_candidate = |mono_item: &MonoItem<'tcx>| {
mono_item.instantiation_mode(tcx) == InstantiationMode::LocalCopy
};
Expand All @@ -438,7 +450,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_mut().record_accesses(caller, accesses);
}

fn check_recursion_limit<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_typeck/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -702,9 +702,9 @@ fn typeck_item_bodies<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, crate_num: CrateNum
{
debug_assert!(crate_num == LOCAL_CRATE);
Ok(tcx.sess.track_errors(|| {
for body_owner_def_id in tcx.body_owners() {
tcx.par_body_owners(|body_owner_def_id| {
ty::query::queries::typeck_tables_of::ensure(tcx, body_owner_def_id);
}
});
})?)
}

Expand Down
2 changes: 2 additions & 0 deletions src/librustc_typeck/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ This API is completely unstable and subject to change.
#![feature(slice_sort_by_cached_key)]
#![feature(never_type)]

#![recursion_limit="256"]

#[macro_use] extern crate log;
#[macro_use] extern crate syntax;
extern crate syntax_pos;
Expand Down