Skip to content

Commit 1cfb628

Browse files
committed
Auto merge of #51383 - Zoxc:parallel-stuff, r=nikomatsakis
Run some stuff in parallel Requires #50699 to actually work correctly. r? @nikomatsakis
2 parents ed39523 + fe1cb88 commit 1cfb628

File tree

11 files changed

+103
-43
lines changed

11 files changed

+103
-43
lines changed

src/librustc/ty/mod.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ use std::cmp::{self, Ordering};
4444
use std::fmt;
4545
use std::hash::{Hash, Hasher};
4646
use std::ops::Deref;
47-
use rustc_data_structures::sync::Lrc;
47+
use rustc_data_structures::sync::{self, Lrc, ParallelIterator, par_iter};
4848
use std::slice;
4949
use std::vec::IntoIter;
5050
use std::mem;
@@ -2436,6 +2436,12 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
24362436
.map(move |&body_id| self.hir.body_owner_def_id(body_id))
24372437
}
24382438

2439+
pub fn par_body_owners<F: Fn(DefId) + sync::Sync + sync::Send>(self, f: F) {
2440+
par_iter(&self.hir.krate().body_ids).for_each(|&body_id| {
2441+
f(self.hir.body_owner_def_id(body_id))
2442+
});
2443+
}
2444+
24392445
pub fn expr_span(self, id: NodeId) -> Span {
24402446
match self.hir.find(id) {
24412447
Some(hir_map::NodeExpr(e)) => {

src/librustc_borrowck/borrowck/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ pub struct LoanDataFlowOperator;
6767
pub type LoanDataFlow<'a, 'tcx> = DataFlowContext<'a, 'tcx, LoanDataFlowOperator>;
6868

6969
pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
70-
for body_owner_def_id in tcx.body_owners() {
70+
tcx.par_body_owners(|body_owner_def_id| {
7171
tcx.borrowck(body_owner_def_id);
72-
}
72+
});
7373
}
7474

7575
pub fn provide(providers: &mut Providers) {

src/librustc_borrowck/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#![feature(from_ref)]
1818
#![feature(quote)]
1919

20+
#![recursion_limit="256"]
21+
2022
#[macro_use] extern crate log;
2123
extern crate syntax;
2224
extern crate syntax_pos;

src/librustc_data_structures/sync.rs

+37-8
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
//!
2727
//! `MTLock` is a mutex which disappears if cfg!(parallel_queries) is false.
2828
//!
29+
//! `MTRef` is a immutable refernce if cfg!(parallel_queries), and an mutable reference otherwise.
30+
//!
2931
//! `rustc_erase_owner!` erases a OwningRef owner into Erased or Erased + Send + Sync
3032
//! depending on the value of cfg!(parallel_queries).
3133
@@ -126,6 +128,8 @@ cfg_if! {
126128
}
127129
}
128130

131+
pub type MTRef<'a, T> = &'a mut T;
132+
129133
#[derive(Debug)]
130134
pub struct MTLock<T>(T);
131135

@@ -151,13 +155,8 @@ cfg_if! {
151155
}
152156

153157
#[inline(always)]
154-
pub fn borrow(&self) -> &T {
155-
&self.0
156-
}
157-
158-
#[inline(always)]
159-
pub fn borrow_mut(&self) -> &T {
160-
&self.0
158+
pub fn lock_mut(&mut self) -> &mut T {
159+
&mut self.0
161160
}
162161
}
163162

@@ -221,7 +220,37 @@ cfg_if! {
221220
pub use std::sync::Arc as Lrc;
222221
pub use std::sync::Weak as Weak;
223222

224-
pub use self::Lock as MTLock;
223+
pub type MTRef<'a, T> = &'a T;
224+
225+
#[derive(Debug)]
226+
pub struct MTLock<T>(Lock<T>);
227+
228+
impl<T> MTLock<T> {
229+
#[inline(always)]
230+
pub fn new(inner: T) -> Self {
231+
MTLock(Lock::new(inner))
232+
}
233+
234+
#[inline(always)]
235+
pub fn into_inner(self) -> T {
236+
self.0.into_inner()
237+
}
238+
239+
#[inline(always)]
240+
pub fn get_mut(&mut self) -> &mut T {
241+
self.0.get_mut()
242+
}
243+
244+
#[inline(always)]
245+
pub fn lock(&self) -> LockGuard<T> {
246+
self.0.lock()
247+
}
248+
249+
#[inline(always)]
250+
pub fn lock_mut(&self) -> LockGuard<T> {
251+
self.lock()
252+
}
253+
}
225254

226255
use parking_lot::Mutex as InnerLock;
227256
use parking_lot::RwLock as InnerRwLock;

src/librustc_driver/driver.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -1272,11 +1272,9 @@ where
12721272

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

1275-
time(sess, "MIR borrow checking", || {
1276-
for def_id in tcx.body_owners() {
1277-
tcx.mir_borrowck(def_id);
1278-
}
1279-
});
1275+
time(sess,
1276+
"MIR borrow checking",
1277+
|| tcx.par_body_owners(|def_id| { tcx.mir_borrowck(def_id); }));
12801278

12811279
time(sess, "dumping chalk-like clauses", || {
12821280
rustc_traits::lowering::dump_program_clauses(tcx);

src/librustc_incremental/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#![feature(fs_read_write)]
1818
#![feature(specialization)]
1919

20+
#![recursion_limit="256"]
21+
2022
extern crate graphviz;
2123
#[macro_use] extern crate rustc;
2224
extern crate rustc_data_structures;

src/librustc_incremental/persist/save.rs

+14-8
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use rustc::session::Session;
1313
use rustc::ty::TyCtxt;
1414
use rustc::util::common::time;
1515
use rustc_data_structures::fx::FxHashMap;
16+
use rustc_data_structures::sync::join;
1617
use rustc_serialize::Encodable as RustcEncodable;
1718
use rustc_serialize::opaque::Encoder;
1819
use std::io::{self, Cursor};
@@ -33,23 +34,28 @@ pub fn save_dep_graph<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
3334
return;
3435
}
3536

36-
time(sess, "persist query result cache", || {
37-
save_in(sess,
38-
query_cache_path(sess),
39-
|e| encode_query_cache(tcx, e));
40-
});
37+
let query_cache_path = query_cache_path(sess);
38+
let dep_graph_path = dep_graph_path(sess);
4139

42-
if tcx.sess.opts.debugging_opts.incremental_queries {
40+
join(move || {
41+
if tcx.sess.opts.debugging_opts.incremental_queries {
42+
time(sess, "persist query result cache", || {
43+
save_in(sess,
44+
query_cache_path,
45+
|e| encode_query_cache(tcx, e));
46+
});
47+
}
48+
}, || {
4349
time(sess, "persist dep-graph", || {
4450
save_in(sess,
45-
dep_graph_path(sess),
51+
dep_graph_path,
4652
|e| {
4753
time(sess, "encode dep-graph", || {
4854
encode_dep_graph(tcx, e)
4955
})
5056
});
5157
});
52-
}
58+
});
5359

5460
dirty_clean::check_dirty_clean_annotations(tcx);
5561
})

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

+29-17
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::{MTRef, MTLock, ParallelIterator, par_iter};
214216

215217
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)]
216218
pub enum MonoItemCollectionMode {
@@ -298,22 +300,32 @@ 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);
308+
309+
let mut visited = MTLock::new(FxHashSet());
310+
let mut inlining_map = MTLock::new(InliningMap::new());
311+
312+
{
313+
let visited: MTRef<'_, _> = &mut visited;
314+
let inlining_map: MTRef<'_, _> = &mut inlining_map;
315+
316+
time(tcx.sess, "collecting mono items", || {
317+
par_iter(roots).for_each(|root| {
318+
let mut recursion_depths = DefIdMap();
319+
collect_items_rec(tcx,
320+
root,
321+
visited,
322+
&mut recursion_depths,
323+
inlining_map);
324+
});
325+
});
314326
}
315327

316-
(visited, inlining_map)
328+
(visited.into_inner(), inlining_map.into_inner())
317329
}
318330

319331
// Find all non-generic items by walking the HIR. These items serve as roots to
@@ -354,10 +366,10 @@ fn collect_roots<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
354366
// Collect all monomorphized items reachable from `starting_point`
355367
fn collect_items_rec<'a, 'tcx: 'a>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
356368
starting_point: MonoItem<'tcx>,
357-
visited: &mut FxHashSet<MonoItem<'tcx>>,
369+
visited: MTRef<'_, MTLock<FxHashSet<MonoItem<'tcx>>>>,
358370
recursion_depths: &mut DefIdMap<usize>,
359-
inlining_map: &mut InliningMap<'tcx>) {
360-
if !visited.insert(starting_point.clone()) {
371+
inlining_map: MTRef<'_, MTLock<InliningMap<'tcx>>>) {
372+
if !visited.lock_mut().insert(starting_point.clone()) {
361373
// We've been here already, no need to search again.
362374
return;
363375
}
@@ -428,7 +440,7 @@ fn collect_items_rec<'a, 'tcx: 'a>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
428440
fn record_accesses<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
429441
caller: MonoItem<'tcx>,
430442
callees: &[MonoItem<'tcx>],
431-
inlining_map: &mut InliningMap<'tcx>) {
443+
inlining_map: MTRef<'_, MTLock<InliningMap<'tcx>>>) {
432444
let is_inlining_candidate = |mono_item: &MonoItem<'tcx>| {
433445
mono_item.instantiation_mode(tcx) == InstantiationMode::LocalCopy
434446
};
@@ -438,7 +450,7 @@ fn record_accesses<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
438450
(*mono_item, is_inlining_candidate(mono_item))
439451
});
440452

441-
inlining_map.record_accesses(caller, accesses);
453+
inlining_map.lock_mut().record_accesses(caller, accesses);
442454
}
443455

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

src/librustc_typeck/check/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -702,9 +702,9 @@ fn typeck_item_bodies<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, crate_num: CrateNum
702702
{
703703
debug_assert!(crate_num == LOCAL_CRATE);
704704
Ok(tcx.sess.track_errors(|| {
705-
for body_owner_def_id in tcx.body_owners() {
705+
tcx.par_body_owners(|body_owner_def_id| {
706706
ty::query::queries::typeck_tables_of::ensure(tcx, body_owner_def_id);
707-
}
707+
});
708708
})?)
709709
}
710710

src/librustc_typeck/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ This API is completely unstable and subject to change.
8383
#![feature(slice_sort_by_cached_key)]
8484
#![feature(never_type)]
8585

86+
#![recursion_limit="256"]
87+
8688
#[macro_use] extern crate log;
8789
#[macro_use] extern crate syntax;
8890
extern crate syntax_pos;

0 commit comments

Comments
 (0)