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

Add some parallelism before TyCtxt #67965

Closed
wants to merge 6 commits into from
Closed
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
3 changes: 1 addition & 2 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3295,8 +3295,7 @@ dependencies = [
[[package]]
name = "rustc-rayon-core"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ea2427831f0053ea3ea73559c8eabd893133a51b251d142bacee53c62a288cb3"
source = "git+https://github.com/Zoxc/rayon.git?branch=single-lifetime-scope#f5d9d8b52dbfc64728d3a42ed3f7292e2eb6ee6b"
dependencies = [
"crossbeam-deque",
"crossbeam-queue",
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ cargo = { path = "src/tools/cargo" }
# that we're shipping as well (to ensure that the rustfmt in RLS and the
# `rustfmt` executable are the same exact version).
rustfmt-nightly = { path = "src/tools/rustfmt" }
rustc-rayon-core = { git = "https://github.com/Zoxc/rayon.git", branch = "single-lifetime-scope" }

# See comments in `src/tools/rustc-workspace-hack/README.md` for what's going on
# here
Expand Down
19 changes: 13 additions & 6 deletions src/librustc/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::middle::cstore::CrateStoreDyn;
use crate::ty::query::Providers;
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::svh::Svh;
use rustc_data_structures::sync::FlexScope;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::def_id::{DefId, DefIndex, LocalDefId, CRATE_DEF_INDEX};
use rustc_hir::itemlikevisit::ItemLikeVisitor;
Expand Down Expand Up @@ -1235,20 +1236,26 @@ pub fn map_crate<'hir>(
collector.finalize_and_compute_crate_hash(crate_disambiguator, cstore, cmdline_args)
};

let map = Map {
Map {
forest,
dep_graph: forest.dep_graph.clone(),
crate_hash,
map,
hir_to_node_id,
definitions,
};
}
}

sess.time("validate HIR map", || {
hir_id_validator::check_crate(&map);
pub fn validate_map<'hir>(
sess: &'hir rustc_session::Session,
map: &'hir Map<'hir>,
scope: &'hir FlexScope<'hir>,
) {
scope.spawn(move || {
sess.time("validate HIR map", || {
hir_id_validator::check_crate(&map);
})
});

map
}

/// Identical to the `PpAnn` implementation for `hir::Crate`,
Expand Down
6 changes: 5 additions & 1 deletion src/librustc/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ use rustc_data_structures::sharded::ShardedHashMap;
use rustc_data_structures::stable_hasher::{
hash_stable_hashmap, HashStable, StableHasher, StableVec,
};
use rustc_data_structures::sync::{Lock, Lrc, WorkerLocal};
use rustc_data_structures::sync::{FlexScope, Lock, Lrc, WorkerLocal};
use rustc_index::vec::{Idx, IndexVec};
use rustc_macros::HashStable;
use rustc_session::node_id::NodeMap;
Expand Down Expand Up @@ -942,6 +942,8 @@ pub struct GlobalCtxt<'tcx> {

interners: CtxtInterners<'tcx>,

pub scope: &'tcx FlexScope<'tcx>,

cstore: Box<CrateStoreDyn>,

pub sess: &'tcx Session,
Expand Down Expand Up @@ -1125,6 +1127,7 @@ impl<'tcx> TyCtxt<'tcx> {
on_disk_query_result_cache: query::OnDiskCache<'tcx>,
crate_name: &str,
output_filenames: &OutputFilenames,
scope: &'tcx FlexScope<'tcx>,
) -> GlobalCtxt<'tcx> {
let data_layout = TargetDataLayout::parse(&s.target.target).unwrap_or_else(|err| {
s.fatal(&err);
Expand Down Expand Up @@ -1174,6 +1177,7 @@ impl<'tcx> TyCtxt<'tcx> {
lint_store,
cstore,
arena,
scope,
interners,
dep_graph,
prof: s.prof.clone(),
Expand Down
1 change: 1 addition & 0 deletions src/librustc_data_structures/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#![feature(integer_atomics)]
#![feature(test)]
#![feature(associated_type_bounds)]
#![feature(wait_until)]
#![cfg_attr(unix, feature(libc))]
#![allow(rustc::default_hash_types)]

Expand Down
23 changes: 23 additions & 0 deletions src/librustc_data_structures/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ use std::ops::{Deref, DerefMut};
pub use std::sync::atomic::Ordering;
pub use std::sync::atomic::Ordering::SeqCst;

pub mod future;

cfg_if! {
if #[cfg(not(parallel_compiler))] {
pub auto trait Send {}
Expand Down Expand Up @@ -160,6 +162,26 @@ cfg_if! {
(oper_a(), oper_b())
}

pub struct FlexScope<'scope> {
marker: PhantomData<fn(&'scope ()) -> &'scope ()>,
}

impl<'scope> FlexScope<'scope> {
pub fn new() -> Self {
Self {
marker: PhantomData,
}
}

pub fn activate<R>(&self, f: impl FnOnce() -> R) -> R {
f()
}

pub fn spawn(&self, f: impl FnOnce() + 'scope) {
f();
}
}

pub struct SerialScope;

impl SerialScope {
Expand Down Expand Up @@ -362,6 +384,7 @@ cfg_if! {
use std;
use std::thread;
pub use rayon::{join, scope};
pub use rayon_core::FlexScope;

/// Runs a list of blocks in parallel. The first block is executed immediately on
/// the current thread. Use that for the longest running block.
Expand Down
57 changes: 57 additions & 0 deletions src/librustc_data_structures/sync/future.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use crate::sync::{self, FlexScope};
use std::mem::ManuallyDrop;
use std::panic::{self, AssertUnwindSafe};
use std::sync::Arc;
use std::sync::Condvar;
use std::sync::Mutex;
use std::thread;

struct FutureData<'a, R> {
// ManuallyDrop is needed here to ensure the destructor of FutureData cannot refer to 'a
func: Mutex<ManuallyDrop<Option<Box<dyn FnOnce() -> R + sync::Send + 'a>>>>,
result: Mutex<Option<thread::Result<R>>>,
waiter: Condvar,
}
pub struct Future<'a, R> {
data: Arc<FutureData<'a, R>>,
}

impl<'a, R: sync::Send + 'a> Future<'a, R> {
pub fn spawn_in_scope(scope: &FlexScope<'a>, f: impl FnOnce() -> R + sync::Send + 'a) -> Self {
let data = Arc::new(FutureData {
func: Mutex::new(ManuallyDrop::new(Some(Box::new(f)))),
result: Mutex::new(None),
waiter: Condvar::new(),
});
let result = Self { data: data.clone() };
scope.spawn(move || {
if let Some(func) = data.func.lock().unwrap().take() {
// Execute the function if it has not yet been joined
let r = panic::catch_unwind(AssertUnwindSafe(func));
*data.result.lock().unwrap() = Some(r);
data.waiter.notify_all();
}
});
result
}

pub fn join(self) -> R {
if let Some(func) = self.data.func.lock().unwrap().take() {
// The function was not executed yet by Rayon, just run it
func()
} else {
// The function has started executing, wait for it to complete
let mut result = self
.data
.waiter
.wait_until(self.data.result.lock().unwrap(), |result| result.is_some())
.unwrap();
match result.take().unwrap() {
Ok(r) => {
return r;
}
Err(err) => panic::resume_unwind(err),
}
}
}
}
8 changes: 7 additions & 1 deletion src/librustc_driver/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,17 +388,23 @@ pub fn run_compiler(
// (needed by the RLS)
})?;
} else {
// Drop AST after creating GlobalCtxt to free memory
// Drop a reference to the AST
mem::drop(queries.expansion()?.take());
}

queries.global_ctxt()?;

// Drop a reference to the AST by waiting on the lint future.
queries.lower_to_hir()?.take().1.join();

queries.global_ctxt()?.peek_mut().enter(|tcx| tcx.analysis(LOCAL_CRATE))?;

if callbacks.after_analysis(compiler, queries) == Compilation::Stop {
return early_exit();
}

if sess.opts.debugging_opts.save_analysis {
// Drop AST to free memory
mem::drop(queries.expansion()?.take());
}

Expand Down
71 changes: 43 additions & 28 deletions src/librustc_interface/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ use rustc_builtin_macros;
use rustc_codegen_ssa::back::link::emit_metadata;
use rustc_codegen_utils::codegen_backend::CodegenBackend;
use rustc_codegen_utils::link::filename_for_metadata;
use rustc_data_structures::sync::{par_iter, Lrc, Once, ParallelIterator, WorkerLocal};
use rustc_data_structures::sync::future::Future;
use rustc_data_structures::sync::{par_iter, FlexScope, Lrc, Once, ParallelIterator, WorkerLocal};
use rustc_data_structures::{box_region_allow_access, declare_box_region_type, parallel};
use rustc_errors::PResult;
use rustc_expand::base::ExtCtxt;
Expand Down Expand Up @@ -401,19 +402,24 @@ fn configure_and_expand_inner<'a>(
println!("{}", json::as_json(&krate));
}

sess.time("name resolution", || {
resolver.resolve_crate(&krate);
});

// Needs to go *after* expansion to be able to check the results of macro expansion.
sess.time("complete gated feature checking", || {
syntax::feature_gate::check_crate(
&krate,
&sess.parse_sess,
&sess.features_untracked(),
sess.opts.unstable_features,
);
});
parallel!(
{
sess.time("name resolution", || {
resolver.resolve_crate(&krate);
});
},
{
// Needs to go *after* expansion to be able to check the results of macro expansion.
sess.time("complete gated feature checking", || {
syntax::feature_gate::check_crate(
&krate,
&sess.parse_sess,
&sess.features_untracked(),
sess.opts.unstable_features,
);
})
}
);

// Add all buffered lints from the `ParseSess` to the `Session`.
sess.parse_sess.buffered_lints.with_lock(|buffered_lints| {
Expand All @@ -428,12 +434,13 @@ fn configure_and_expand_inner<'a>(

pub fn lower_to_hir<'res, 'tcx>(
sess: &'tcx Session,
lint_store: &lint::LintStore,
lint_store: Lrc<lint::LintStore>,
resolver: &'res mut Resolver<'_>,
dep_graph: &'res DepGraph,
krate: &'res ast::Crate,
krate: Lrc<ast::Crate>,
arena: &'tcx Arena<'tcx>,
) -> Result<map::Forest<'tcx>> {
scope: &'tcx FlexScope<'tcx>,
) -> Result<(map::Forest<'tcx>, Future<'tcx, ()>)> {
// Lower AST to HIR.
let hir_forest = sess.time("lowering AST -> HIR", || {
let hir_crate = rustc_ast_lowering::lower_crate(
Expand All @@ -452,23 +459,27 @@ pub fn lower_to_hir<'res, 'tcx>(
map::Forest::new(hir_crate, &dep_graph)
});

sess.time("early lint checks", || {
rustc_lint::check_ast_crate(
sess,
lint_store,
&krate,
false,
Some(std::mem::take(resolver.lint_buffer())),
rustc_lint::BuiltinCombinedEarlyLintPass::new(),
)
let lint_buffer = std::mem::take(resolver.lint_buffer());

let lints = Future::spawn_in_scope(scope, move || {
sess.time("early lint checks", || {
rustc_lint::check_ast_crate(
sess,
&lint_store,
&krate,
false,
Some(lint_buffer),
rustc_lint::BuiltinCombinedEarlyLintPass::new(),
)
})
});

// Discard hygiene data, which isn't required after lowering to HIR.
if !sess.opts.debugging_opts.keep_hygiene_data {
rustc_span::hygiene::clear_syntax_context_map();
}

Ok(hir_forest)
Ok((hir_forest, lints))
}

// Returns all the paths that correspond to generated files.
Expand Down Expand Up @@ -721,6 +732,7 @@ pub fn create_global_ctxt<'tcx>(
global_ctxt: &'tcx Once<GlobalCtxt<'tcx>>,
all_arenas: &'tcx AllArenas,
arena: &'tcx WorkerLocal<Arena<'tcx>>,
scope: &'tcx FlexScope<'tcx>,
) -> QueryContext<'tcx> {
let sess = &compiler.session();
let defs = mem::take(&mut resolver_outputs.definitions);
Expand Down Expand Up @@ -759,11 +771,14 @@ pub fn create_global_ctxt<'tcx>(
query_result_on_disk_cache,
&crate_name,
&outputs,
scope,
)
});

// Do some initialization of the DepGraph that can only be done with the tcx available.
ty::tls::enter_global(&gcx, |tcx| {
map::validate_map(sess, tcx.hir(), scope);

// Do some initialization of the DepGraph that can only be done with the tcx available.
tcx.sess.time("dep graph tcx init", || rustc_incremental::dep_graph_tcx_init(tcx));
});

Expand Down
Loading