Skip to content

Commit 64714b6

Browse files
committed
close parallel compilation for Incremental
1 parent f08cea4 commit 64714b6

File tree

10 files changed

+39
-27
lines changed

10 files changed

+39
-27
lines changed

compiler/rustc_data_structures/src/sync.rs

+13-17
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,13 @@ mod worker_local;
5353

5454
pub use std::sync::atomic::Ordering;
5555
pub use std::sync::atomic::Ordering::SeqCst;
56-
use std::sync::{Mutex, MutexGuard};
5756

5857
pub use vec::{AppendOnlyIndexVec, AppendOnlyVec};
5958

6059
mod vec;
6160
use parking_lot::lock_api::RawMutex as _;
6261
use parking_lot::lock_api::RawRwLock as _;
63-
use parking_lot::{RawMutex, RawRwLock};
62+
use parking_lot::{Mutex, MutexGuard, RawMutex, RawRwLock};
6463

6564
mod mode {
6665
use super::Ordering;
@@ -644,7 +643,7 @@ impl<T> Lock<T> {
644643
}
645644
}
646645

647-
#[inline(never)]
646+
#[inline]
648647
fn lock_raw(&self) {
649648
if likely(self.single_thread) {
650649
assert!(!self.borrow.replace(true));
@@ -660,7 +659,7 @@ impl<T> Lock<T> {
660659
LockGuard { lock: &self, marker: PhantomData }
661660
}
662661

663-
#[inline(never)]
662+
#[inline]
664663
pub(crate) fn with_mt_lock<F: FnOnce(&mut T) -> R, R>(&self, f: F) -> R {
665664
unsafe {
666665
self.mutex.lock();
@@ -683,7 +682,7 @@ impl<T> Lock<T> {
683682
}
684683
}
685684

686-
#[inline(never)]
685+
#[inline]
687686
fn with_mt_borrow<F: FnOnce(&T) -> R, R>(&self, f: F) -> R {
688687
unsafe {
689688
self.mutex.lock();
@@ -751,7 +750,7 @@ impl<T> DerefMut for LockGuard<'_, T> {
751750
}
752751
}
753752

754-
#[inline(never)]
753+
#[inline]
755754
fn unlock_mt<T>(guard: &mut LockGuard<'_, T>) {
756755
unsafe { guard.lock.mutex.unlock() }
757756
}
@@ -842,26 +841,23 @@ impl<T> LockLike<T> for Mutex<T> {
842841

843842
#[inline]
844843
fn into_inner(self) -> T {
845-
self.into_inner().unwrap()
844+
self.into_inner()
846845
}
847846

848847
#[inline]
849848
fn get_mut(&mut self) -> &mut T {
850-
self.get_mut().unwrap()
849+
self.get_mut()
851850
}
852851

853852
#[inline]
854853
fn try_lock(&self) -> Option<MutexGuard<'_, T>> {
855-
self.try_lock().ok()
854+
self.try_lock()
856855
}
857856

858857
#[inline(always)]
859858
#[track_caller]
860859
fn lock(&self) -> MutexGuard<'_, T> {
861-
self.lock().unwrap_or_else(|e| {
862-
self.clear_poison();
863-
e.into_inner()
864-
})
860+
self.lock()
865861
}
866862
}
867863

@@ -1095,7 +1091,7 @@ impl<T> RwLock<T> {
10951091
self.data.get_mut()
10961092
}
10971093

1098-
#[inline(never)]
1094+
#[inline]
10991095
fn mt_read(&self) -> ReadGuard<'_, T> {
11001096
self.raw.raw.lock_shared();
11011097
ReadGuard { rwlock: self, marker: PhantomData }
@@ -1113,7 +1109,7 @@ impl<T> RwLock<T> {
11131109
}
11141110
}
11151111

1116-
#[inline(never)]
1112+
#[inline]
11171113
fn with_mt_read_lock<F: FnOnce(&T) -> R, R>(&self, f: F) -> R {
11181114
self.raw.raw.lock_shared();
11191115
let r = unsafe { f(&*self.data.get()) };
@@ -1157,7 +1153,7 @@ impl<T> RwLock<T> {
11571153
}
11581154
}
11591155

1160-
#[inline(never)]
1156+
#[inline]
11611157
fn mt_write(&self) -> WriteGuard<'_, T> {
11621158
self.raw.raw.lock_exclusive();
11631159
WriteGuard { rwlock: self, marker: PhantomData }
@@ -1173,7 +1169,7 @@ impl<T> RwLock<T> {
11731169
}
11741170
}
11751171

1176-
#[inline(never)]
1172+
#[inline]
11771173
pub fn with_mt_write_lock<F: FnOnce(&mut T) -> R, R>(&self, f: F) -> R {
11781174
self.raw.raw.lock_exclusive();
11791175
unsafe {

compiler/rustc_driver_impl/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ fn run_compiler(
257257
let sopts = config::build_session_options(&matches);
258258

259259
// Set parallel mode before thread pool creation, which will create `Lock`s.
260-
interface::set_parallel_mode(&sopts.unstable_opts);
260+
interface::set_parallel_mode(&sopts.unstable_opts, &sopts.cg);
261261

262262
if let Some(ref code) = matches.opt_str("explain") {
263263
handle_explain(diagnostics_registry(), code, sopts.error_format);

compiler/rustc_interface/src/interface.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,18 @@ impl Compiler {
6161
}
6262

6363
#[allow(rustc::bad_opt_access)]
64-
pub fn set_parallel_mode(sopts: &config::UnstableOptions) {
65-
rustc_data_structures::sync::set(sopts.threads > 1);
64+
pub fn set_parallel_mode(sopts1: &config::UnstableOptions, sopts2: &config::CodegenOptions) {
65+
let parallel = if sopts1.threads <= 1 {
66+
false
67+
} else {
68+
if let Some(path) = &sopts2.incremental {
69+
if matches!(std::fs::try_exists(PathBuf::from(path)), Ok(false)) { true } else { false }
70+
} else {
71+
true
72+
}
73+
};
74+
75+
rustc_data_structures::sync::set(parallel);
6676
}
6777

6878
/// Converts strings provided as `--cfg [cfgspec]` into a `crate_cfg`.

compiler/rustc_interface/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#![feature(thread_spawn_unchecked)]
55
#![feature(lazy_cell)]
66
#![feature(try_blocks)]
7+
#![feature(fs_try_exists)]
78
#![recursion_limit = "256"]
89
#![allow(rustc::potential_query_instability)]
910
#![deny(rustc::untranslatable_diagnostic)]

compiler/rustc_interface/src/util.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ pub(crate) fn run_in_thread_pool_with_globals<F: FnOnce() -> R + Send, R: Send>(
134134
f: F,
135135
) -> R {
136136
#[cfg(parallel_compiler)]
137-
if _threads > 1 {
137+
if rustc_data_structures::sync::active() {
138138
return run_in_threads_pool_with_globals(edition, _threads, f);
139139
}
140140
// The "thread pool" is a single spawned thread in the non-parallel

compiler/rustc_metadata/src/rmeta/encoder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2233,7 +2233,7 @@ pub fn encode_metadata(tcx: TyCtxt<'_>, path: &Path) {
22332233
join(
22342234
|| encode_metadata_impl(tcx, path),
22352235
|| {
2236-
if tcx.sess.threads() == 1 {
2236+
if !rustc_data_structures::sync::active() {
22372237
return;
22382238
}
22392239
// Prefetch some queries used by metadata encoding.

compiler/rustc_query_system/src/query/job.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ pub(crate) fn report_cycle<'a, D: DepKind>(
612612
}
613613

614614
pub fn print_query_stack<Qcx: QueryContext>(
615-
qcx: Qcx,
615+
_qcx: Qcx,
616616
mut current_query: Option<QueryJobId>,
617617
handler: &Handler,
618618
num_frames: Option<usize>,
@@ -621,7 +621,12 @@ pub fn print_query_stack<Qcx: QueryContext>(
621621
// a panic hook, which means that the global `Handler` may be in a weird
622622
// state if it was responsible for triggering the panic.
623623
let mut i = 0;
624-
let query_map = qcx.try_collect_active_jobs();
624+
625+
#[cfg(not(parallel_compiler))]
626+
let query_map = _qcx.try_collect_active_jobs();
627+
628+
#[cfg(parallel_compiler)]
629+
let query_map: Option<QueryMap<Qcx::DepKind>> = None;
625630

626631
while let Some(query) = current_query {
627632
if Some(i) == num_frames {

compiler/rustc_query_system/src/query/plumbing.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ where
304304
// re-executing the query since `try_start` only checks that the query is not currently
305305
// executing, but another thread may have already completed the query and stores it result
306306
// in the query cache.
307-
if cfg!(parallel_compiler) && qcx.dep_context().sess().threads() > 1 {
307+
if cfg!(parallel_compiler) && rustc_data_structures::sync::active() {
308308
if let Some((value, index)) = query.look_up(qcx, &key) {
309309
qcx.dep_context().profiler().query_cache_hit(index.into());
310310
return (value, Some(index));

compiler/rustc_session/src/options.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1748,7 +1748,7 @@ options! {
17481748
/// in the future. Note that -Zthreads=0 is the way to get
17491749
/// the num_cpus behavior.
17501750
#[rustc_lint_opt_deny_field_access("use `Session::threads` instead of this field")]
1751-
threads: usize = (1, parse_threads, [UNTRACKED],
1751+
threads: usize = (2, parse_threads, [UNTRACKED],
17521752
"use a thread pool with N threads"),
17531753
time_llvm_passes: bool = (false, parse_bool, [UNTRACKED],
17541754
"measure time of each LLVM pass (default: no)"),

src/librustdoc/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,7 @@ fn main_args(at_args: &[String]) -> MainResult {
738738
};
739739

740740
// Set parallel mode before error handler creation, which will create `Lock`s.
741-
interface::set_parallel_mode(&options.unstable_opts);
741+
interface::set_parallel_mode(&options.unstable_opts, &options.codegen_options);
742742

743743
let diag = core::new_handler(
744744
options.error_format,

0 commit comments

Comments
 (0)