Skip to content

Commit f32d62e

Browse files
authored
Rollup merge of rust-lang#58679 - Zoxc:passes-refactor, r=michaelwoerister
Refactor passes and pass execution to be more parallel For `syntex_syntax` (with 16 threads and 8 cores): - Cuts `misc checking 1` from `0.096s` to `0.08325s`. - Cuts `misc checking 2` from `0.3575s` to `0.2545s`. - Cuts `misc checking 3` from `0.34625s` to `0.21375s`. - Cuts `wf checking` from `0.3085s` to `0.05025s`. Reduces overall execution time for `syntex_syntax` (with 8 threads and cores) from `4.92s` to `4.34s`. Subsumes rust-lang#58494 Blocked on rust-lang#58250 r? @michaelwoerister
2 parents 3f872b2 + 7985c6f commit f32d62e

File tree

31 files changed

+305
-197
lines changed

31 files changed

+305
-197
lines changed

src/librustc/dep_graph/dep_node.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,7 @@ define_dep_nodes!( <'tcx>
456456
[eval_always] CoherenceInherentImplOverlapCheck,
457457
[] CoherenceCheckTrait(DefId),
458458
[eval_always] PrivacyAccessLevels(CrateNum),
459+
[eval_always] CheckPrivateInPublic(CrateNum),
459460
[eval_always] Analysis(CrateNum),
460461

461462
// Represents the MIR for a fn; also used as the task node for

src/librustc/hir/check_attr.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -344,12 +344,6 @@ impl<'a, 'tcx> Visitor<'tcx> for CheckAttrVisitor<'a, 'tcx> {
344344
}
345345
}
346346

347-
pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
348-
for &module in tcx.hir().krate().modules.keys() {
349-
tcx.ensure().check_mod_attrs(tcx.hir().local_def_id(module));
350-
}
351-
}
352-
353347
fn is_c_like_enum(item: &hir::Item) -> bool {
354348
if let hir::ItemKind::Enum(ref def, _) = item.node {
355349
for variant in &def.variants {

src/librustc/hir/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use syntax::util::parser::ExprPrecedence;
3030
use crate::ty::AdtKind;
3131
use crate::ty::query::Providers;
3232

33-
use rustc_data_structures::sync::{ParallelIterator, par_iter, Send, Sync};
33+
use rustc_data_structures::sync::{par_for_each_in, Send, Sync};
3434
use rustc_data_structures::thin_vec::ThinVec;
3535
use rustc_macros::HashStable;
3636

@@ -779,15 +779,15 @@ impl Crate {
779779
where V: itemlikevisit::ParItemLikeVisitor<'hir> + Sync + Send
780780
{
781781
parallel!({
782-
par_iter(&self.items).for_each(|(_, item)| {
782+
par_for_each_in(&self.items, |(_, item)| {
783783
visitor.visit_item(item);
784784
});
785785
}, {
786-
par_iter(&self.trait_items).for_each(|(_, trait_item)| {
786+
par_for_each_in(&self.trait_items, |(_, trait_item)| {
787787
visitor.visit_trait_item(trait_item);
788788
});
789789
}, {
790-
par_iter(&self.impl_items).for_each(|(_, impl_item)| {
790+
par_for_each_in(&self.impl_items, |(_, impl_item)| {
791791
visitor.visit_impl_item(impl_item);
792792
});
793793
});

src/librustc/middle/intrinsicck.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,6 @@ use syntax_pos::Span;
1010
use crate::hir::intravisit::{self, Visitor, NestedVisitorMap};
1111
use crate::hir;
1212

13-
pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
14-
for &module in tcx.hir().krate().modules.keys() {
15-
tcx.ensure().check_mod_intrinsics(tcx.hir().local_def_id(module));
16-
}
17-
}
18-
1913
fn check_mod_intrinsics<'tcx>(tcx: TyCtxt<'_, 'tcx, 'tcx>, module_def_id: DefId) {
2014
tcx.hir().visit_item_likes_in_module(
2115
module_def_id,

src/librustc/middle/liveness.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -185,12 +185,6 @@ fn check_mod_liveness<'tcx>(tcx: TyCtxt<'_, 'tcx, 'tcx>, module_def_id: DefId) {
185185
tcx.hir().visit_item_likes_in_module(module_def_id, &mut IrMaps::new(tcx).as_deep_visitor());
186186
}
187187

188-
pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
189-
for &module in tcx.hir().krate().modules.keys() {
190-
tcx.ensure().check_mod_liveness(tcx.hir().local_def_id(module));
191-
}
192-
}
193-
194188
pub fn provide(providers: &mut Providers<'_>) {
195189
*providers = Providers {
196190
check_mod_liveness,

src/librustc/middle/stability.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -456,12 +456,6 @@ impl<'a, 'tcx> Index<'tcx> {
456456
}
457457
}
458458

459-
pub fn check_unstable_api_usage<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
460-
for &module in tcx.hir().krate().modules.keys() {
461-
tcx.ensure().check_mod_unstable_api_usage(tcx.hir().local_def_id(module));
462-
}
463-
}
464-
465459
/// Cross-references the feature names of unstable APIs with enabled
466460
/// features and possibly prints errors.
467461
fn check_mod_unstable_api_usage<'tcx>(tcx: TyCtxt<'_, 'tcx, 'tcx>, module_def_id: DefId) {

src/librustc/ty/query/config.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,12 @@ impl<'tcx> QueryDescription<'tcx> for queries::privacy_access_levels<'tcx> {
369369
}
370370
}
371371

372+
impl<'tcx> QueryDescription<'tcx> for queries::check_private_in_public<'tcx> {
373+
fn describe(_: TyCtxt<'_, '_, '_>, _: CrateNum) -> Cow<'static, str> {
374+
"checking for private elements in public interfaces".into()
375+
}
376+
}
377+
372378
impl<'tcx> QueryDescription<'tcx> for queries::typeck_item_bodies<'tcx> {
373379
fn describe(_: TyCtxt<'_, '_, '_>, _: CrateNum) -> Cow<'static, str> {
374380
"type-checking all item bodies".into()

src/librustc/ty/query/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,8 +350,9 @@ define_queries! { <'tcx>
350350
[] fn check_match: CheckMatch(DefId)
351351
-> Result<(), ErrorReported>,
352352

353-
/// Performs the privacy check and computes "access levels".
353+
/// Performs part of the privacy check and computes "access levels".
354354
[] fn privacy_access_levels: PrivacyAccessLevels(CrateNum) -> Lrc<AccessLevels>,
355+
[] fn check_private_in_public: CheckPrivateInPublic(CrateNum) -> (),
355356
},
356357

357358
Other {

src/librustc/ty/query/plumbing.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,6 +1251,7 @@ pub fn force_from_dep_node<'a, 'gcx, 'lcx>(tcx: TyCtxt<'a, 'gcx, 'lcx>,
12511251
force!(crate_inherent_impls_overlap_check, LOCAL_CRATE)
12521252
},
12531253
DepKind::PrivacyAccessLevels => { force!(privacy_access_levels, LOCAL_CRATE); }
1254+
DepKind::CheckPrivateInPublic => { force!(check_private_in_public, LOCAL_CRATE); }
12541255
DepKind::MirBuilt => { force!(mir_built, def_id!()); }
12551256
DepKind::MirConstQualif => { force!(mir_const_qualif, def_id!()); }
12561257
DepKind::MirConst => { force!(mir_const, def_id!()); }

src/librustc_data_structures/sync.rs

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ cfg_if! {
6565
}
6666

6767
use std::ops::Add;
68+
use std::panic::{resume_unwind, catch_unwind, AssertUnwindSafe};
6869

6970
#[derive(Debug)]
7071
pub struct Atomic<T: Copy>(Cell<T>);
@@ -130,7 +131,21 @@ cfg_if! {
130131
#[macro_export]
131132
macro_rules! parallel {
132133
($($blocks:tt),*) => {
133-
$($blocks)*;
134+
// We catch panics here ensuring that all the blocks execute.
135+
// This makes behavior consistent with the parallel compiler.
136+
let mut panic = None;
137+
$(
138+
if let Err(p) = ::std::panic::catch_unwind(
139+
::std::panic::AssertUnwindSafe(|| $blocks)
140+
) {
141+
if panic.is_none() {
142+
panic = Some(p);
143+
}
144+
}
145+
)*
146+
if let Some(panic) = panic {
147+
::std::panic::resume_unwind(panic);
148+
}
134149
}
135150
}
136151

@@ -140,6 +155,26 @@ cfg_if! {
140155
t.into_iter()
141156
}
142157

158+
pub fn par_for_each_in<T: IntoIterator>(
159+
t: T,
160+
for_each:
161+
impl Fn(<<T as IntoIterator>::IntoIter as Iterator>::Item) + Sync + Send
162+
) {
163+
// We catch panics here ensuring that all the loop iterations execute.
164+
// This makes behavior consistent with the parallel compiler.
165+
let mut panic = None;
166+
t.into_iter().for_each(|i| {
167+
if let Err(p) = catch_unwind(AssertUnwindSafe(|| for_each(i))) {
168+
if panic.is_none() {
169+
panic = Some(p);
170+
}
171+
}
172+
});
173+
if let Some(panic) = panic {
174+
resume_unwind(panic);
175+
}
176+
}
177+
143178
pub type MetadataRef = OwningRef<Box<dyn Erased>, [u8]>;
144179

145180
pub use std::rc::Rc as Lrc;
@@ -278,23 +313,26 @@ cfg_if! {
278313
use std::thread;
279314
pub use rayon::{join, scope};
280315

316+
/// Runs a list of blocks in parallel. The first block is executed immediately on
317+
/// the current thread. Use that for the longest running block.
281318
#[macro_export]
282319
macro_rules! parallel {
283-
(impl [$($c:tt,)*] [$block:tt $(, $rest:tt)*]) => {
284-
parallel!(impl [$block, $($c,)*] [$($rest),*])
320+
(impl $fblock:tt [$($c:tt,)*] [$block:tt $(, $rest:tt)*]) => {
321+
parallel!(impl $fblock [$block, $($c,)*] [$($rest),*])
285322
};
286-
(impl [$($blocks:tt,)*] []) => {
323+
(impl $fblock:tt [$($blocks:tt,)*] []) => {
287324
::rustc_data_structures::sync::scope(|s| {
288325
$(
289326
s.spawn(|_| $blocks);
290327
)*
328+
$fblock;
291329
})
292330
};
293-
($($blocks:tt),*) => {
294-
// Reverse the order of the blocks since Rayon executes them in reverse order
331+
($fblock:tt, $($blocks:tt),*) => {
332+
// Reverse the order of the later blocks since Rayon executes them in reverse order
295333
// when using a single thread. This ensures the execution order matches that
296334
// of a single threaded rustc
297-
parallel!(impl [] [$($blocks),*]);
335+
parallel!(impl $fblock [] [$($blocks),*]);
298336
};
299337
}
300338

@@ -307,6 +345,15 @@ cfg_if! {
307345
t.into_par_iter()
308346
}
309347

348+
pub fn par_for_each_in<T: IntoParallelIterator>(
349+
t: T,
350+
for_each: impl Fn(
351+
<<T as IntoParallelIterator>::Iter as ParallelIterator>::Item
352+
) + Sync + Send
353+
) {
354+
t.into_par_iter().for_each(for_each)
355+
}
356+
310357
pub type MetadataRef = OwningRef<Box<dyn Erased + Send + Sync>, [u8]>;
311358

312359
/// This makes locks panic if they are already held.

0 commit comments

Comments
 (0)