Skip to content

Commit e9f1fad

Browse files
committed
Parallelize passes
1 parent b15d6b0 commit e9f1fad

File tree

18 files changed

+66
-47
lines changed

18 files changed

+66
-47
lines changed

src/librustc/hir/itemlikevisit.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,17 @@ pub trait IntoVisitor<'hir> {
101101
fn into_visitor(&self) -> Self::Visitor;
102102
}
103103

104+
#[derive(Clone)]
105+
pub struct ClonableVisitor<V>(pub V);
106+
107+
impl<'hir, V: Visitor<'hir> + Clone> IntoVisitor<'hir> for ClonableVisitor<V> {
108+
type Visitor = V;
109+
110+
fn into_visitor(&self) -> V {
111+
self.clone().0
112+
}
113+
}
114+
104115
pub struct ParDeepVisitor<V>(pub V);
105116

106117
impl<'hir, V> ParItemLikeVisitor<'hir> for ParDeepVisitor<V>

src/librustc/hir/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,13 @@ impl Crate {
746746
});
747747
}
748748

749+
pub fn par_deep_visit_items<'hir, V>(&'hir self, visitor: V)
750+
where V: intravisit::Visitor<'hir> + Clone + Sync + Send
751+
{
752+
let visitor = itemlikevisit::ClonableVisitor(visitor);
753+
self.par_visit_all_item_likes(&itemlikevisit::ParDeepVisitor(visitor));
754+
}
755+
749756
pub fn body(&self, id: BodyId) -> &Body {
750757
&self.bodies[&id]
751758
}

src/librustc/middle/intrinsicck.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,10 @@ use hir::intravisit::{self, Visitor, NestedVisitorMap};
1919
use hir;
2020

2121
pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
22-
let mut visitor = ItemVisitor {
23-
tcx,
24-
};
25-
tcx.hir.krate().visit_all_item_likes(&mut visitor.as_deep_visitor());
22+
tcx.hir.krate().par_deep_visit_items(ItemVisitor { tcx });
2623
}
2724

25+
#[derive(Clone)]
2826
struct ItemVisitor<'a, 'tcx: 'a> {
2927
tcx: TyCtxt<'a, 'tcx, 'tcx>
3028
}

src/librustc/middle/stability.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
324324
}
325325
}
326326

327+
#[derive(Clone)]
327328
struct MissingStabilityAnnotations<'a, 'tcx: 'a> {
328329
tcx: TyCtxt<'a, 'tcx, 'tcx>,
329330
access_levels: &'a AccessLevels,
@@ -466,8 +467,7 @@ impl<'a, 'tcx> Index<'tcx> {
466467
/// Cross-references the feature names of unstable APIs with enabled
467468
/// features and possibly prints errors.
468469
pub fn check_unstable_api_usage<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
469-
let mut checker = Checker { tcx: tcx };
470-
tcx.hir.krate().visit_all_item_likes(&mut checker.as_deep_visitor());
470+
tcx.hir.krate().par_deep_visit_items(Checker { tcx: tcx });
471471
}
472472

473473
/// Check whether an item marked with `deprecated(since="X")` is currently
@@ -494,6 +494,7 @@ pub fn deprecation_in_effect(since: &str) -> bool {
494494
}
495495
}
496496

497+
#[derive(Clone)]
497498
struct Checker<'a, 'tcx: 'a> {
498499
tcx: TyCtxt<'a, 'tcx, 'tcx>,
499500
}
@@ -807,7 +808,7 @@ pub fn check_unused_or_stable_features<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
807808
};
808809
missing.check_missing_stability(ast::CRATE_NODE_ID, krate.span);
809810
intravisit::walk_crate(&mut missing, krate);
810-
krate.visit_all_item_likes(&mut missing.as_deep_visitor());
811+
krate.par_deep_visit_items(missing);
811812
}
812813

813814
let ref declared_lib_features = tcx.features().declared_lib_features;

src/librustc/traits/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ use ty::error::{ExpectedFound, TypeError};
2828
use infer::{InferCtxt};
2929

3030
use rustc_data_structures::sync::Lrc;
31-
use std::rc::Rc;
3231
use syntax::ast;
3332
use syntax_pos::{Span, DUMMY_SP};
3433

@@ -254,7 +253,7 @@ pub struct DerivedObligationCause<'tcx> {
254253
parent_trait_ref: ty::PolyTraitRef<'tcx>,
255254

256255
/// The parent trait had this cause
257-
parent_code: Rc<ObligationCauseCode<'tcx>>
256+
parent_code: Lrc<ObligationCauseCode<'tcx>>
258257
}
259258

260259
pub type Obligations<'tcx, O> = Vec<Obligation<'tcx, O>>;

src/librustc/traits/select.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,12 @@ use ty::relate::TypeRelation;
4444
use middle::lang_items;
4545
use mir::interpret::{GlobalId};
4646

47-
use rustc_data_structures::sync::Lock;
47+
use rustc_data_structures::sync::{Lrc, Lock};
4848
use rustc_data_structures::bitvec::BitVector;
4949
use std::iter;
5050
use std::cmp;
5151
use std::fmt;
5252
use std::mem;
53-
use std::rc::Rc;
5453
use rustc_target::spec::abi::Abi;
5554
use hir;
5655
use util::nodemap::{FxHashMap, FxHashSet};
@@ -3400,7 +3399,7 @@ impl<'tcx> TraitObligation<'tcx> {
34003399
if obligation.recursion_depth >= 0 {
34013400
let derived_cause = DerivedObligationCause {
34023401
parent_trait_ref: obligation.predicate.to_poly_trait_ref(),
3403-
parent_code: Rc::new(obligation.cause.code.clone())
3402+
parent_code: Lrc::new(obligation.cause.code.clone())
34043403
};
34053404
let derived_code = variant(derived_cause);
34063405
ObligationCause::new(obligation.cause.span, obligation.cause.body_id, derived_code)

src/librustc/traits/structural_impls.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use ty::{self, Lift, TyCtxt};
1515
use ty::fold::{TypeFoldable, TypeFolder, TypeVisitor};
1616

1717
use std::fmt;
18-
use std::rc::Rc;
18+
use rustc_data_structures::sync::Lrc;
1919

2020
// structural impls for the structs in traits
2121

@@ -254,7 +254,7 @@ impl<'a, 'tcx> Lift<'tcx> for traits::DerivedObligationCause<'a> {
254254
tcx.lift(&*self.parent_code).map(|code| {
255255
traits::DerivedObligationCause {
256256
parent_trait_ref: trait_ref,
257-
parent_code: Rc::new(code)
257+
parent_code: Lrc::new(code)
258258
}
259259
})
260260
})

src/librustc/ty/structural_impls.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use rustc_data_structures::sync::Lrc;
2222
use mir::interpret;
2323

2424
use std::rc::Rc;
25+
use std::sync::Arc;
2526

2627
///////////////////////////////////////////////////////////////////////////
2728
// Atomic structs
@@ -699,6 +700,16 @@ EnumTypeFoldableImpl! {
699700
} where T: TypeFoldable<'tcx>
700701
}
701702

703+
impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Arc<T> {
704+
fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
705+
Arc::new((**self).fold_with(folder))
706+
}
707+
708+
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
709+
(**self).visit_with(visitor)
710+
}
711+
}
712+
702713
impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Rc<T> {
703714
fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
704715
Rc::new((**self).fold_with(folder))

src/librustc_mir/hair/pattern/check_match.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ use syntax::ast;
3737
use syntax::ptr::P;
3838
use syntax_pos::{Span, DUMMY_SP};
3939

40+
#[derive(Clone)]
4041
struct OuterVisitor<'a, 'tcx: 'a> { tcx: TyCtxt<'a, 'tcx, 'tcx> }
4142

4243
impl<'a, 'tcx> Visitor<'tcx> for OuterVisitor<'a, 'tcx> {
@@ -52,7 +53,7 @@ impl<'a, 'tcx> Visitor<'tcx> for OuterVisitor<'a, 'tcx> {
5253
}
5354

5455
pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
55-
tcx.hir.krate().visit_all_item_likes(&mut OuterVisitor { tcx: tcx }.as_deep_visitor());
56+
tcx.hir.krate().par_deep_visit_items(OuterVisitor { tcx: tcx });
5657
tcx.sess.abort_if_errors();
5758
}
5859

src/librustc_passes/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
#![feature(rustc_diagnostic_macros)]
2222

23+
#![recursion_limit="256"]
24+
2325
#[macro_use]
2426
extern crate rustc;
2527
extern crate rustc_mir;

src/librustc_passes/rvalue_promotion.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,7 @@ pub fn provide(providers: &mut Providers) {
5050
}
5151

5252
pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
53-
for &body_id in &tcx.hir.krate().body_ids {
54-
let def_id = tcx.hir.body_owner_def_id(body_id);
55-
tcx.const_is_rvalue_promotable_to_static(def_id);
56-
}
53+
tcx.par_body_owners(|def_id| { tcx.const_is_rvalue_promotable_to_static(def_id); });
5754
tcx.sess.abort_if_errors();
5855
}
5956

src/librustc_trans/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
#![feature(optin_builtin_traits)]
3232
#![feature(inclusive_range_methods)]
3333

34+
#![recursion_limit="256"]
35+
3436
use rustc::dep_graph::WorkProduct;
3537
use syntax_pos::symbol::Symbol;
3638

src/librustc_trans_utils/symbol_names_test.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,12 @@ pub fn report_symbol_names<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
3333

3434
tcx.dep_graph.with_ignore(|| {
3535
let mut visitor = SymbolNamesTest { tcx: tcx };
36+
// FIXME: Try parallel version
3637
tcx.hir.krate().visit_all_item_likes(&mut visitor);
3738
})
3839
}
3940

41+
#[derive(Clone)]
4042
struct SymbolNamesTest<'a, 'tcx:'a> {
4143
tcx: TyCtxt<'a, 'tcx, 'tcx>,
4244
}

src/librustc_typeck/check/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -691,8 +691,8 @@ impl<'a, 'tcx> ParItemLikeVisitor<'tcx> for CheckItemTypesVisitor<'a, 'tcx> {
691691

692692
pub fn check_wf_new<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Result<(), ErrorReported> {
693693
tcx.sess.track_errors(|| {
694-
let mut visit = wfcheck::CheckTypeWellFormedVisitor::new(tcx);
695-
tcx.hir.krate().visit_all_item_likes(&mut visit.as_deep_visitor());
694+
let visit = wfcheck::CheckTypeWellFormedVisitor::new(tcx);
695+
tcx.hir.krate().par_deep_visit_items(visit);
696696
})
697697
}
698698

src/librustc_typeck/check/wfcheck.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,7 @@ fn reject_shadowing_type_parameters(tcx: TyCtxt, def_id: DefId) {
660660
}
661661
}
662662

663+
#[derive(Clone)]
663664
pub struct CheckTypeWellFormedVisitor<'a, 'tcx: 'a> {
664665
tcx: TyCtxt<'a, 'tcx, 'tcx>,
665666
}

src/librustc_typeck/check_unused.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use syntax::ast;
1515
use syntax_pos::{Span, DUMMY_SP};
1616

1717
use rustc::hir::def_id::LOCAL_CRATE;
18-
use rustc::hir::itemlikevisit::ItemLikeVisitor;
18+
use rustc::hir::itemlikevisit::ParItemLikeVisitor;
1919
use rustc::hir;
2020
use rustc::util::nodemap::DefIdSet;
2121

@@ -45,8 +45,8 @@ impl<'a, 'tcx> CheckVisitor<'a, 'tcx> {
4545
}
4646
}
4747

48-
impl<'a, 'tcx, 'v> ItemLikeVisitor<'v> for CheckVisitor<'a, 'tcx> {
49-
fn visit_item(&mut self, item: &hir::Item) {
48+
impl<'a, 'tcx, 'v> ParItemLikeVisitor<'v> for CheckVisitor<'a, 'tcx> {
49+
fn visit_item(&self, item: &hir::Item) {
5050
if item.vis == hir::Public || item.span == DUMMY_SP {
5151
return;
5252
}
@@ -55,10 +55,10 @@ impl<'a, 'tcx, 'v> ItemLikeVisitor<'v> for CheckVisitor<'a, 'tcx> {
5555
}
5656
}
5757

58-
fn visit_trait_item(&mut self, _trait_item: &hir::TraitItem) {
58+
fn visit_trait_item(&self, _trait_item: &hir::TraitItem) {
5959
}
6060

61-
fn visit_impl_item(&mut self, _impl_item: &hir::ImplItem) {
61+
fn visit_impl_item(&self, _impl_item: &hir::ImplItem) {
6262
}
6363
}
6464

@@ -71,8 +71,8 @@ pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
7171
used_trait_imports.extend(imports.iter());
7272
}
7373

74-
let mut visitor = CheckVisitor { tcx, used_trait_imports };
75-
tcx.hir.krate().visit_all_item_likes(&mut visitor);
74+
let visitor = CheckVisitor { tcx, used_trait_imports };
75+
tcx.hir.krate().par_visit_all_item_likes(&visitor);
7676

7777
for &(def_id, span) in tcx.maybe_unused_extern_crates(LOCAL_CRATE).iter() {
7878
// The `def_id` here actually was calculated during resolution (at least

src/librustc_typeck/collect.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,14 @@ 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};
5453
use rustc::hir::def::{Def, CtorKind};
5554
use rustc::hir::def_id::{DefId, LOCAL_CRATE};
5655

5756
///////////////////////////////////////////////////////////////////////////
5857
// Main entry point
5958

6059
pub fn collect_item_types<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
61-
let visitor = CollectItemTypesVisitor { tcx: tcx };
62-
tcx.hir.krate().par_visit_all_item_likes(&ParDeepVisitor(visitor));
60+
tcx.hir.krate().par_deep_visit_items(CollectItemTypesVisitor { tcx: tcx });
6361
}
6462

6563
pub fn provide(providers: &mut Providers) {
@@ -104,14 +102,6 @@ struct CollectItemTypesVisitor<'a, 'tcx: 'a> {
104102
tcx: TyCtxt<'a, 'tcx, 'tcx>
105103
}
106104

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-
115105
impl<'a, 'tcx> Visitor<'tcx> for CollectItemTypesVisitor<'a, 'tcx> {
116106
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
117107
NestedVisitorMap::OnlyBodies(&self.tcx.hir)

src/librustc_typeck/outlives/test.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +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_inferred_outlives<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
16-
tcx.hir
17-
.krate()
18-
.visit_all_item_likes(&mut OutlivesTest { tcx });
16+
tcx.hir.krate().par_visit_all_item_likes(&mut OutlivesTest { tcx });
1917
}
2018

2119
struct OutlivesTest<'a, 'tcx: 'a> {
2220
tcx: TyCtxt<'a, 'tcx, 'tcx>,
2321
}
2422

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

2927
// For unit testing: check for a special "rustc_outlives"
@@ -40,6 +38,6 @@ impl<'a, 'tcx> ItemLikeVisitor<'tcx> for OutlivesTest<'a, 'tcx> {
4038
}
4139
}
4240

43-
fn visit_trait_item(&mut self, _: &'tcx hir::TraitItem) {}
44-
fn visit_impl_item(&mut self, _: &'tcx hir::ImplItem) {}
41+
fn visit_trait_item(&self, _: &'tcx hir::TraitItem) { }
42+
fn visit_impl_item(&self, _: &'tcx hir::ImplItem) { }
4543
}

0 commit comments

Comments
 (0)