Skip to content

Commit b15d6b0

Browse files
committed
Parallel code
1 parent f5646ed commit b15d6b0

File tree

12 files changed

+71
-57
lines changed

12 files changed

+71
-57
lines changed

src/librustc_borrowck/borrowck/mod.rs

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 0 deletions
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_driver/driver.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,21 +1195,20 @@ where
11951195

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

1198-
time(sess, "MIR borrow checking", || {
1199-
for def_id in tcx.body_owners() {
1200-
tcx.mir_borrowck(def_id);
1201-
}
1202-
});
1198+
time(sess,
1199+
"MIR borrow checking",
1200+
|| tcx.par_body_owners(|def_id| { tcx.mir_borrowck(def_id); }));
12031201

12041202
time(sess, "dumping chalk-like clauses", || {
12051203
rustc_traits::lowering::dump_program_clauses(tcx);
12061204
});
12071205

1208-
time(sess, "MIR effect checking", || {
1209-
for def_id in tcx.body_owners() {
1210-
mir::transform::check_unsafety::check_unsafety(tcx, def_id)
1211-
}
1212-
});
1206+
time(sess,
1207+
"MIR effect checking",
1208+
|| tcx.par_body_owners(|def_id| {
1209+
mir::transform::check_unsafety::check_unsafety(tcx.global_tcx(), def_id)
1210+
}));
1211+
12131212
// Avoid overwhelming user with errors if type checking failed.
12141213
// I'm not sure how helpful this is, to be honest, but it avoids
12151214
// a

src/librustc_typeck/check/mod.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ use syntax::util::lev_distance::find_best_match_for_name;
129129
use syntax_pos::{self, BytePos, Span, MultiSpan};
130130

131131
use rustc::hir::intravisit::{self, Visitor, NestedVisitorMap};
132-
use rustc::hir::itemlikevisit::ItemLikeVisitor;
132+
use rustc::hir::itemlikevisit::ParItemLikeVisitor;
133133
use rustc::hir::map::Node;
134134
use rustc::hir::{self, PatKind};
135135
use rustc::middle::lang_items;
@@ -681,12 +681,12 @@ impl<'a, 'gcx, 'tcx> Inherited<'a, 'gcx, 'tcx> {
681681

682682
struct CheckItemTypesVisitor<'a, 'tcx: 'a> { tcx: TyCtxt<'a, 'tcx, 'tcx> }
683683

684-
impl<'a, 'tcx> ItemLikeVisitor<'tcx> for CheckItemTypesVisitor<'a, 'tcx> {
685-
fn visit_item(&mut self, i: &'tcx hir::Item) {
684+
impl<'a, 'tcx> ParItemLikeVisitor<'tcx> for CheckItemTypesVisitor<'a, 'tcx> {
685+
fn visit_item(&self, i: &'tcx hir::Item) {
686686
check_item_type(self.tcx, i);
687687
}
688-
fn visit_trait_item(&mut self, _: &'tcx hir::TraitItem) { }
689-
fn visit_impl_item(&mut self, _: &'tcx hir::ImplItem) { }
688+
fn visit_trait_item(&self, _: &'tcx hir::TraitItem) { }
689+
fn visit_impl_item(&self, _: &'tcx hir::ImplItem) { }
690690
}
691691

692692
pub fn check_wf_new<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Result<(), ErrorReported> {
@@ -698,7 +698,7 @@ pub fn check_wf_new<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Result<(), ErrorRe
698698

699699
pub fn check_item_types<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Result<(), ErrorReported> {
700700
tcx.sess.track_errors(|| {
701-
tcx.hir.krate().visit_all_item_likes(&mut CheckItemTypesVisitor { tcx });
701+
tcx.hir.krate().par_visit_all_item_likes(&CheckItemTypesVisitor { tcx });
702702
})
703703
}
704704

@@ -711,9 +711,9 @@ fn typeck_item_bodies<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, crate_num: CrateNum
711711
{
712712
debug_assert!(crate_num == LOCAL_CRATE);
713713
Ok(tcx.sess.track_errors(|| {
714-
for body_owner_def_id in tcx.body_owners() {
714+
tcx.par_body_owners(|body_owner_def_id| {
715715
ty::maps::queries::typeck_tables_of::ensure(tcx, body_owner_def_id);
716-
}
716+
});
717717
})?)
718718
}
719719

src/librustc_typeck/coherence/inherent_impls_overlap.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use namespace::Namespace;
1212
use rustc::hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
1313
use rustc::hir;
14-
use rustc::hir::itemlikevisit::ItemLikeVisitor;
14+
use rustc::hir::itemlikevisit::ParItemLikeVisitor;
1515
use rustc::traits::{self, IntercrateMode};
1616
use rustc::ty::TyCtxt;
1717

@@ -21,7 +21,7 @@ pub fn crate_inherent_impls_overlap_check<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
2121
crate_num: CrateNum) {
2222
assert_eq!(crate_num, LOCAL_CRATE);
2323
let krate = tcx.hir.krate();
24-
krate.visit_all_item_likes(&mut InherentOverlapChecker { tcx });
24+
krate.par_visit_all_item_likes(&InherentOverlapChecker { tcx });
2525
}
2626

2727
struct InherentOverlapChecker<'a, 'tcx: 'a> {
@@ -119,8 +119,8 @@ impl<'a, 'tcx> InherentOverlapChecker<'a, 'tcx> {
119119
}
120120
}
121121

122-
impl<'a, 'tcx, 'v> ItemLikeVisitor<'v> for InherentOverlapChecker<'a, 'tcx> {
123-
fn visit_item(&mut self, item: &'v hir::Item) {
122+
impl<'a, 'tcx, 'v> ParItemLikeVisitor<'v> for InherentOverlapChecker<'a, 'tcx> {
123+
fn visit_item(&self, item: &'v hir::Item) {
124124
match item.node {
125125
hir::ItemEnum(..) |
126126
hir::ItemStruct(..) |
@@ -133,9 +133,9 @@ impl<'a, 'tcx, 'v> ItemLikeVisitor<'v> for InherentOverlapChecker<'a, 'tcx> {
133133
}
134134
}
135135

136-
fn visit_trait_item(&mut self, _trait_item: &hir::TraitItem) {
136+
fn visit_trait_item(&self, _trait_item: &hir::TraitItem) {
137137
}
138138

139-
fn visit_impl_item(&mut self, _impl_item: &hir::ImplItem) {
139+
fn visit_impl_item(&self, _impl_item: &hir::ImplItem) {
140140
}
141141
}

src/librustc_typeck/coherence/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use hir::def_id::{DefId, LOCAL_CRATE};
1919
use rustc::traits;
2020
use rustc::ty::{self, TyCtxt, TypeFoldable};
2121
use rustc::ty::maps::Providers;
22+
use rustc_data_structures::sync::{ParallelIterator, par_iter};
2223

2324
use syntax::ast;
2425

@@ -126,9 +127,9 @@ fn coherent_trait<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) {
126127
}
127128

128129
pub fn check_coherence<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
129-
for &trait_def_id in tcx.hir.krate().trait_impls.keys() {
130+
par_iter(&tcx.hir.krate().trait_impls).for_each(|(&trait_def_id, _)| {
130131
ty::maps::queries::coherent_trait::ensure(tcx, trait_def_id);
131-
}
132+
});
132133

133134
unsafety::check(tcx);
134135
orphan::check(tcx);

src/librustc_typeck/coherence/orphan.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,25 @@
1313
1414
use rustc::traits;
1515
use rustc::ty::{self, TyCtxt};
16-
use rustc::hir::itemlikevisit::ItemLikeVisitor;
16+
use rustc::hir::itemlikevisit::ParItemLikeVisitor;
1717
use rustc::hir;
1818

1919
pub fn check<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
20-
let mut orphan = OrphanChecker { tcx: tcx };
21-
tcx.hir.krate().visit_all_item_likes(&mut orphan);
20+
let orphan = OrphanChecker { tcx: tcx };
21+
tcx.hir.krate().par_visit_all_item_likes(&orphan);
2222
}
2323

2424
struct OrphanChecker<'cx, 'tcx: 'cx> {
2525
tcx: TyCtxt<'cx, 'tcx, 'tcx>,
2626
}
2727

28-
impl<'cx, 'tcx, 'v> ItemLikeVisitor<'v> for OrphanChecker<'cx, 'tcx> {
28+
impl<'cx, 'tcx, 'v> ParItemLikeVisitor<'v> for OrphanChecker<'cx, 'tcx> {
2929
/// Checks exactly one impl for orphan rules and other such
3030
/// restrictions. In this fn, it can happen that multiple errors
3131
/// apply to a specific impl, so just return after reporting one
3232
/// to prevent inundating the user with a bunch of similar error
3333
/// reports.
34-
fn visit_item(&mut self, item: &hir::Item) {
34+
fn visit_item(&self, item: &hir::Item) {
3535
let def_id = self.tcx.hir.local_def_id(item.id);
3636
match item.node {
3737
hir::ItemImpl(.., Some(_), _, _) => {
@@ -161,9 +161,9 @@ impl<'cx, 'tcx, 'v> ItemLikeVisitor<'v> for OrphanChecker<'cx, 'tcx> {
161161
}
162162
}
163163

164-
fn visit_trait_item(&mut self, _trait_item: &hir::TraitItem) {
164+
fn visit_trait_item(&self, _trait_item: &hir::TraitItem) {
165165
}
166166

167-
fn visit_impl_item(&mut self, _impl_item: &hir::ImplItem) {
167+
fn visit_impl_item(&self, _impl_item: &hir::ImplItem) {
168168
}
169169
}

src/librustc_typeck/coherence/unsafety.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,20 @@
1212
//! crate or pertains to a type defined in this crate.
1313
1414
use rustc::ty::TyCtxt;
15-
use rustc::hir::itemlikevisit::ItemLikeVisitor;
15+
use rustc::hir::itemlikevisit::ParItemLikeVisitor;
1616
use rustc::hir::{self, Unsafety};
1717

1818
pub fn check<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
19-
let mut unsafety = UnsafetyChecker { tcx: tcx };
20-
tcx.hir.krate().visit_all_item_likes(&mut unsafety);
19+
let unsafety = UnsafetyChecker { tcx: tcx };
20+
tcx.hir.krate().par_visit_all_item_likes(&unsafety);
2121
}
2222

2323
struct UnsafetyChecker<'cx, 'tcx: 'cx> {
2424
tcx: TyCtxt<'cx, 'tcx, 'tcx>,
2525
}
2626

2727
impl<'cx, 'tcx, 'v> UnsafetyChecker<'cx, 'tcx> {
28-
fn check_unsafety_coherence(&mut self,
28+
fn check_unsafety_coherence(&self,
2929
item: &'v hir::Item,
3030
impl_generics: Option<&hir::Generics>,
3131
unsafety: hir::Unsafety,
@@ -78,8 +78,8 @@ impl<'cx, 'tcx, 'v> UnsafetyChecker<'cx, 'tcx> {
7878
}
7979
}
8080

81-
impl<'cx, 'tcx, 'v> ItemLikeVisitor<'v> for UnsafetyChecker<'cx, 'tcx> {
82-
fn visit_item(&mut self, item: &'v hir::Item) {
81+
impl<'cx, 'tcx, 'v> ParItemLikeVisitor<'v> for UnsafetyChecker<'cx, 'tcx> {
82+
fn visit_item(&self, item: &'v hir::Item) {
8383
match item.node {
8484
hir::ItemImpl(unsafety, polarity, _, ref generics, ..) => {
8585
self.check_unsafety_coherence(item, Some(generics), unsafety, polarity);
@@ -88,9 +88,9 @@ impl<'cx, 'tcx, 'v> ItemLikeVisitor<'v> for UnsafetyChecker<'cx, 'tcx> {
8888
}
8989
}
9090

91-
fn visit_trait_item(&mut self, _trait_item: &hir::TraitItem) {
91+
fn visit_trait_item(&self, _trait_item: &hir::TraitItem) {
9292
}
9393

94-
fn visit_impl_item(&mut self, _impl_item: &hir::ImplItem) {
94+
fn visit_impl_item(&self, _impl_item: &hir::ImplItem) {
9595
}
9696
}

src/librustc_typeck/collect.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,16 @@ use syntax_pos::{Span, DUMMY_SP};
5050

5151
use rustc::hir::{self, map as hir_map, TransFnAttrs, TransFnAttrFlags, Unsafety};
5252
use rustc::hir::intravisit::{self, Visitor, NestedVisitorMap};
53+
use rustc::hir::itemlikevisit::{IntoVisitor, ParDeepVisitor};
5354
use rustc::hir::def::{Def, CtorKind};
5455
use rustc::hir::def_id::{DefId, LOCAL_CRATE};
5556

5657
///////////////////////////////////////////////////////////////////////////
5758
// Main entry point
5859

5960
pub fn collect_item_types<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
60-
let mut visitor = CollectItemTypesVisitor { tcx: tcx };
61-
tcx.hir.krate().visit_all_item_likes(&mut visitor.as_deep_visitor());
61+
let visitor = CollectItemTypesVisitor { tcx: tcx };
62+
tcx.hir.krate().par_visit_all_item_likes(&ParDeepVisitor(visitor));
6263
}
6364

6465
pub fn provide(providers: &mut Providers) {
@@ -98,10 +99,19 @@ pub struct ItemCtxt<'a,'tcx:'a> {
9899

99100
///////////////////////////////////////////////////////////////////////////
100101

102+
#[derive(Clone)]
101103
struct CollectItemTypesVisitor<'a, 'tcx: 'a> {
102104
tcx: TyCtxt<'a, 'tcx, 'tcx>
103105
}
104106

107+
impl<'a, 'tcx: 'a> IntoVisitor<'tcx> for CollectItemTypesVisitor<'a, 'tcx> {
108+
type Visitor = Self;
109+
110+
fn into_visitor(&self) -> Self {
111+
self.clone()
112+
}
113+
}
114+
105115
impl<'a, 'tcx> Visitor<'tcx> for CollectItemTypesVisitor<'a, 'tcx> {
106116
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
107117
NestedVisitorMap::OnlyBodies(&self.tcx.hir)

src/librustc_typeck/impl_wf_check.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
2121
use constrained_type_params as ctp;
2222
use rustc::hir;
23-
use rustc::hir::itemlikevisit::ItemLikeVisitor;
23+
use rustc::hir::itemlikevisit::ParItemLikeVisitor;
2424
use rustc::hir::def_id::DefId;
2525
use rustc::ty::{self, TyCtxt};
2626
use rustc::util::nodemap::{FxHashMap, FxHashSet};
@@ -62,15 +62,15 @@ pub fn impl_wf_check<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
6262
// We will tag this as part of the WF check -- logically, it is,
6363
// but it's one that we must perform earlier than the rest of
6464
// WfCheck.
65-
tcx.hir.krate().visit_all_item_likes(&mut ImplWfCheck { tcx: tcx });
65+
tcx.hir.krate().par_visit_all_item_likes(&ImplWfCheck { tcx: tcx });
6666
}
6767

6868
struct ImplWfCheck<'a, 'tcx: 'a> {
6969
tcx: TyCtxt<'a, 'tcx, 'tcx>,
7070
}
7171

72-
impl<'a, 'tcx> ItemLikeVisitor<'tcx> for ImplWfCheck<'a, 'tcx> {
73-
fn visit_item(&mut self, item: &'tcx hir::Item) {
72+
impl<'a, 'tcx> ParItemLikeVisitor<'tcx> for ImplWfCheck<'a, 'tcx> {
73+
fn visit_item(&self, item: &'tcx hir::Item) {
7474
match item.node {
7575
hir::ItemImpl(.., ref generics, _, _, ref impl_item_refs) => {
7676
let impl_def_id = self.tcx.hir.local_def_id(item.id);
@@ -84,9 +84,9 @@ impl<'a, 'tcx> ItemLikeVisitor<'tcx> for ImplWfCheck<'a, 'tcx> {
8484
}
8585
}
8686

87-
fn visit_trait_item(&mut self, _trait_item: &'tcx hir::TraitItem) { }
87+
fn visit_trait_item(&self, _trait_item: &'tcx hir::TraitItem) { }
8888

89-
fn visit_impl_item(&mut self, _impl_item: &'tcx hir::ImplItem) { }
89+
fn visit_impl_item(&self, _impl_item: &'tcx hir::ImplItem) { }
9090
}
9191

9292
fn enforce_impl_params_are_constrained<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,

src/librustc_typeck/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ This API is completely unstable and subject to change.
8585
#![feature(slice_sort_by_cached_key)]
8686
#![feature(never_type)]
8787

88+
#![recursion_limit="256"]
89+
8890
#[macro_use] extern crate log;
8991
#[macro_use] extern crate syntax;
9092
extern crate syntax_pos;

src/librustc_typeck/variance/test.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@
99
// except according to those terms.
1010

1111
use rustc::hir;
12-
use rustc::hir::itemlikevisit::ItemLikeVisitor;
12+
use rustc::hir::itemlikevisit::ParItemLikeVisitor;
1313
use rustc::ty::TyCtxt;
1414

1515
pub fn test_variance<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
16-
tcx.hir.krate().visit_all_item_likes(&mut VarianceTest { tcx });
16+
tcx.hir.krate().par_visit_all_item_likes(&VarianceTest { tcx });
1717
}
1818

1919
struct VarianceTest<'a, 'tcx: 'a> {
2020
tcx: TyCtxt<'a, 'tcx, 'tcx>
2121
}
2222

23-
impl<'a, 'tcx> ItemLikeVisitor<'tcx> for VarianceTest<'a, 'tcx> {
24-
fn visit_item(&mut self, item: &'tcx hir::Item) {
23+
impl<'a, 'tcx> ParItemLikeVisitor<'tcx> for VarianceTest<'a, 'tcx> {
24+
fn visit_item(&self, item: &'tcx hir::Item) {
2525
let item_def_id = self.tcx.hir.local_def_id(item.id);
2626

2727
// For unit testing: check for a special "rustc_variance"
@@ -36,6 +36,6 @@ impl<'a, 'tcx> ItemLikeVisitor<'tcx> for VarianceTest<'a, 'tcx> {
3636
}
3737
}
3838

39-
fn visit_trait_item(&mut self, _: &'tcx hir::TraitItem) { }
40-
fn visit_impl_item(&mut self, _: &'tcx hir::ImplItem) { }
39+
fn visit_trait_item(&self, _: &'tcx hir::TraitItem) { }
40+
fn visit_impl_item(&self, _: &'tcx hir::ImplItem) { }
4141
}

0 commit comments

Comments
 (0)