Skip to content
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

use par_for_each_in in par_body_owners and collect_crate_mono_items #99457

Merged
merged 1 commit into from
Jul 19, 2022
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
2 changes: 1 addition & 1 deletion compiler/rustc_data_structures/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ cfg_if! {
t.into_iter()
}

pub fn par_for_each_in<T: IntoIterator>(t: T, for_each: impl Fn(T::Item) + Sync + Send) {
pub fn par_for_each_in<T: IntoIterator>(t: T, mut for_each: impl FnMut(T::Item) + Sync + Send) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't this break the parallel compiler? The requirement is relaxed and then if we switch to parallel rustc it would fail to compile.

Copy link
Member Author

@SparrowLii SparrowLii Jul 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't break the compiler from actual testing.

This function signature change is only required in non-parallel mode, and the one in parallel mode does not need to be changed. I think this is because the internal implementation of IntoParallelIterator does some kind of encapsulation of this(not sure).
#[Edit] See the new explanation below

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we want to maintain consistency in both modes, we should change Fn in the signature in parallel mode to FnMut. I'm not sure if this is necessary.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we can do that, if we run it in parallel then the function would be called from multiple threads thus requiring it to be called without having exclusive access.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense. So I think we can keep the Fn in the signature in parallel mode, as it doesn't cause compilation errors

Copy link
Member Author

@SparrowLii SparrowLii Jul 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I revisited the implementation of both patterns. The place to call this interface is in collect.rs. Because the MTRef type is not a &mut reference in parallel mode, the Fn type can be used without causing a compilation error.

// We catch panics here ensuring that all the loop iterations execute.
// This makes behavior consistent with the parallel compiler.
let mut panic = None;
Expand Down
4 changes: 1 addition & 3 deletions compiler/rustc_middle/src/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,9 +491,7 @@ impl<'hir> Map<'hir> {
}

pub fn par_body_owners<F: Fn(LocalDefId) + Sync + Send>(self, f: F) {
use rustc_data_structures::sync::{par_iter, ParallelIterator};

par_iter(&self.tcx.hir_crate_items(()).body_owners[..]).for_each(|&def_id| f(def_id));
par_for_each_in(&self.tcx.hir_crate_items(()).body_owners[..], |&def_id| f(def_id));
}

pub fn ty_param_owner(self, def_id: LocalDefId) -> LocalDefId {
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_monomorphize/src/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@
//! regardless of whether it is actually needed or not.

use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_data_structures::sync::{par_iter, MTLock, MTRef, ParallelIterator};
use rustc_data_structures::sync::{par_for_each_in, MTLock, MTRef};
use rustc_hir as hir;
use rustc_hir::def::DefKind;
use rustc_hir::def_id::{DefId, DefIdMap, LocalDefId};
Expand Down Expand Up @@ -346,7 +346,7 @@ pub fn collect_crate_mono_items(
let inlining_map: MTRef<'_, _> = &mut inlining_map;

tcx.sess.time("monomorphization_collector_graph_walk", || {
par_iter(roots).for_each(|root| {
par_for_each_in(roots, |root| {
let mut recursion_depths = DefIdMap::default();
collect_items_rec(
tcx,
Expand Down
8 changes: 7 additions & 1 deletion src/test/ui/privacy/privacy2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ LL | pub fn foo() {}

error: requires `sized` lang_item

error: aborting due to 3 previous errors
error: requires `sized` lang_item

error: requires `sized` lang_item

error: requires `sized` lang_item

error: aborting due to 6 previous errors

Some errors have detailed explanations: E0432, E0603.
For more information about an error, try `rustc --explain E0432`.
8 changes: 7 additions & 1 deletion src/test/ui/privacy/privacy3.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ LL | use bar::gpriv;

error: requires `sized` lang_item

error: aborting due to 2 previous errors
error: requires `sized` lang_item

error: requires `sized` lang_item

error: requires `sized` lang_item

error: aborting due to 5 previous errors

For more information about this error, try `rustc --explain E0432`.
11 changes: 10 additions & 1 deletion src/test/ui/type_length_limit.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,14 @@ LL | pub fn drop<T>(_x: T) {}
= note: the full type name has been written to '$TEST_BUILD_DIR/type_length_limit/type_length_limit.long-type.txt'
= help: consider adding a `#![type_length_limit="8"]` attribute to your crate

error: aborting due to previous error
error: reached the type-length limit while instantiating `<[closure@std::rt::lang_start<()...e<()>>::call_once - shim(vtable)`
--> $SRC_DIR/core/src/ops/function.rs:LL:COL
|
LL | extern "rust-call" fn call_once(self, args: Args) -> Self::Output;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: the full type name has been written to '$TEST_BUILD_DIR/type_length_limit/type_length_limit.long-type.txt'
= help: consider adding a `#![type_length_limit="8"]` attribute to your crate

error: aborting due to 2 previous errors