Skip to content

Commit 07806ad

Browse files
committed
Auto merge of #59647 - Zoxc:test-rayon-tp, r=<try>
[do not merge] Use a Rayon thread pool Checking performance overhead of doing so.
2 parents 7641873 + c47118e commit 07806ad

File tree

2 files changed

+16
-78
lines changed

2 files changed

+16
-78
lines changed

src/librustc/session/config.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1235,7 +1235,7 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
12351235
"prints the llvm optimization passes being run"),
12361236
ast_json: bool = (false, parse_bool, [UNTRACKED],
12371237
"print the AST as JSON and halt"),
1238-
threads: Option<usize> = (None, parse_opt_uint, [UNTRACKED],
1238+
threads: Option<usize> = (Some(1), parse_opt_uint, [UNTRACKED],
12391239
"use a thread pool with N threads"),
12401240
ast_json_noexpand: bool = (false, parse_bool, [UNTRACKED],
12411241
"print the pre-expansion AST as JSON and halt"),

src/librustc_interface/util.rs

+15-77
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use rustc::session::CrateDisambiguator;
55
use rustc::ty;
66
use rustc::lint;
77
use rustc_codegen_utils::codegen_backend::CodegenBackend;
8-
#[cfg(parallel_compiler)]
98
use rustc_data_structures::jobserver;
109
use rustc_data_structures::sync::{Lock, Lrc};
1110
use rustc_data_structures::stable_hasher::StableHasher;
@@ -38,8 +37,6 @@ use syntax::util::lev_distance::find_best_match_for_name;
3837
use syntax::source_map::{FileLoader, RealFileLoader, SourceMap};
3938
use syntax::symbol::Symbol;
4039
use syntax::{self, ast, attr};
41-
#[cfg(not(parallel_compiler))]
42-
use std::{thread, panic};
4340

4441
pub fn diagnostics_registry() -> Registry {
4542
let mut all_errors = Vec::new();
@@ -140,54 +137,6 @@ impl Write for Sink {
140137
fn flush(&mut self) -> io::Result<()> { Ok(()) }
141138
}
142139

143-
#[cfg(not(parallel_compiler))]
144-
pub fn scoped_thread<F: FnOnce() -> R + Send, R: Send>(cfg: thread::Builder, f: F) -> R {
145-
struct Ptr(*mut ());
146-
unsafe impl Send for Ptr {}
147-
unsafe impl Sync for Ptr {}
148-
149-
let mut f = Some(f);
150-
let run = Ptr(&mut f as *mut _ as *mut ());
151-
let mut result = None;
152-
let result_ptr = Ptr(&mut result as *mut _ as *mut ());
153-
154-
let thread = cfg.spawn(move || {
155-
let run = unsafe { (*(run.0 as *mut Option<F>)).take().unwrap() };
156-
let result = unsafe { &mut *(result_ptr.0 as *mut Option<R>) };
157-
*result = Some(run());
158-
});
159-
160-
match thread.unwrap().join() {
161-
Ok(()) => result.unwrap(),
162-
Err(p) => panic::resume_unwind(p),
163-
}
164-
}
165-
166-
#[cfg(not(parallel_compiler))]
167-
pub fn spawn_thread_pool<F: FnOnce() -> R + Send, R: Send>(
168-
_threads: Option<usize>,
169-
stderr: &Option<Arc<Mutex<Vec<u8>>>>,
170-
f: F,
171-
) -> R {
172-
let mut cfg = thread::Builder::new().name("rustc".to_string());
173-
174-
if let Some(size) = get_stack_size() {
175-
cfg = cfg.stack_size(size);
176-
}
177-
178-
scoped_thread(cfg, || {
179-
syntax::with_globals( || {
180-
ty::tls::GCX_PTR.set(&Lock::new(0), || {
181-
if let Some(stderr) = stderr {
182-
io::set_panic(Some(box Sink(stderr.clone())));
183-
}
184-
ty::tls::with_thread_locals(|| f())
185-
})
186-
})
187-
})
188-
}
189-
190-
#[cfg(parallel_compiler)]
191140
pub fn spawn_thread_pool<F: FnOnce() -> R + Send, R: Send>(
192141
threads: Option<usize>,
193142
stderr: &Option<Arc<Mutex<Vec<u8>>>>,
@@ -197,44 +146,33 @@ pub fn spawn_thread_pool<F: FnOnce() -> R + Send, R: Send>(
197146
use syntax;
198147
use syntax_pos;
199148

200-
let gcx_ptr = &Lock::new(0);
201-
202149
let mut config = ThreadPoolBuilder::new()
203150
.acquire_thread_handler(jobserver::acquire_thread)
204151
.release_thread_handler(jobserver::release_thread)
205-
.num_threads(Session::threads_from_count(threads))
206-
.deadlock_handler(|| unsafe { ty::query::handle_deadlock() });
152+
.num_threads(Session::threads_from_count(threads));
207153

208154
if let Some(size) = get_stack_size() {
209155
config = config.stack_size(size);
210156
}
211157

212158
let with_pool = move |pool: &ThreadPool| pool.install(move || f());
213159

214-
syntax::with_globals(|| {
215-
syntax::GLOBALS.with(|syntax_globals| {
216-
syntax_pos::GLOBALS.with(|syntax_pos_globals| {
217-
// The main handler runs for each Rayon worker thread and sets up
218-
// the thread local rustc uses. syntax_globals and syntax_pos_globals are
219-
// captured and set on the new threads. ty::tls::with_thread_locals sets up
220-
// thread local callbacks from libsyntax
221-
let main_handler = move |worker: &mut dyn FnMut()| {
222-
syntax::GLOBALS.set(syntax_globals, || {
223-
syntax_pos::GLOBALS.set(syntax_pos_globals, || {
224-
if let Some(stderr) = stderr {
225-
io::set_panic(Some(box Sink(stderr.clone())));
226-
}
227-
ty::tls::with_thread_locals(|| {
228-
ty::tls::GCX_PTR.set(gcx_ptr, || worker())
229-
})
230-
})
231-
})
232-
};
233-
234-
ThreadPool::scoped_pool(config, main_handler, with_pool).unwrap()
160+
// The main handler runs for each Rayon worker thread and sets up
161+
// the thread local rustc uses. syntax_globals and syntax_pos_globals are
162+
// captured and set on the new threads. ty::tls::with_thread_locals sets up
163+
// thread local callbacks from libsyntax
164+
let main_handler = move |worker: &mut dyn FnMut()| {
165+
syntax::with_globals(|| {
166+
if let Some(stderr) = stderr {
167+
io::set_panic(Some(box Sink(stderr.clone())));
168+
}
169+
ty::tls::with_thread_locals(|| {
170+
ty::tls::GCX_PTR.set(&Lock::new(0), || worker())
235171
})
236172
})
237-
})
173+
};
174+
175+
ThreadPool::scoped_pool(config, main_handler, with_pool).unwrap()
238176
}
239177

240178
fn load_backend_from_dylib(path: &Path) -> fn() -> Box<dyn CodegenBackend> {

0 commit comments

Comments
 (0)