Skip to content

Commit eb1754e

Browse files
authored
Rollup merge of #74122 - nnethercote:startup-cleanup, r=petrochenkov
Start-up clean-up r? @petrochenkov
2 parents 8efa197 + bf70786 commit eb1754e

File tree

4 files changed

+53
-55
lines changed

4 files changed

+53
-55
lines changed

src/librustc_interface/interface.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,7 @@ pub struct Config {
159159
pub registry: Registry,
160160
}
161161

162-
pub fn run_compiler_in_existing_thread_pool<R>(
163-
config: Config,
164-
f: impl FnOnce(&Compiler) -> R,
165-
) -> R {
162+
pub fn create_compiler_and_run<R>(config: Config, f: impl FnOnce(&Compiler) -> R) -> R {
166163
let registry = &config.registry;
167164
let (sess, codegen_backend) = util::create_session(
168165
config.opts,
@@ -204,17 +201,20 @@ pub fn run_compiler_in_existing_thread_pool<R>(
204201
pub fn run_compiler<R: Send>(mut config: Config, f: impl FnOnce(&Compiler) -> R + Send) -> R {
205202
log::trace!("run_compiler");
206203
let stderr = config.stderr.take();
207-
util::spawn_thread_pool(
204+
util::setup_callbacks_and_run_in_thread_pool_with_globals(
208205
config.opts.edition,
209206
config.opts.debugging_opts.threads,
210207
&stderr,
211-
|| run_compiler_in_existing_thread_pool(config, f),
208+
|| create_compiler_and_run(config, f),
212209
)
213210
}
214211

215-
pub fn default_thread_pool<R: Send>(edition: edition::Edition, f: impl FnOnce() -> R + Send) -> R {
212+
pub fn setup_callbacks_and_run_in_default_thread_pool_with_globals<R: Send>(
213+
edition: edition::Edition,
214+
f: impl FnOnce() -> R + Send,
215+
) -> R {
216216
// the 1 here is duplicating code in config.opts.debugging_opts.threads
217217
// which also defaults to 1; it ultimately doesn't matter as the default
218218
// isn't threaded, and just ignores this parameter
219-
util::spawn_thread_pool(edition, 1, &None, f)
219+
util::setup_callbacks_and_run_in_thread_pool_with_globals(edition, 1, &None, f)
220220
}

src/librustc_interface/util.rs

+17-14
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ impl Write for Sink {
102102
}
103103
}
104104

105+
/// Like a `thread::Builder::spawn` followed by a `join()`, but avoids the need
106+
/// for `'static` bounds.
105107
#[cfg(not(parallel_compiler))]
106108
pub fn scoped_thread<F: FnOnce() -> R + Send, R: Send>(cfg: thread::Builder, f: F) -> R {
107109
struct Ptr(*mut ());
@@ -126,7 +128,7 @@ pub fn scoped_thread<F: FnOnce() -> R + Send, R: Send>(cfg: thread::Builder, f:
126128
}
127129

128130
#[cfg(not(parallel_compiler))]
129-
pub fn spawn_thread_pool<F: FnOnce() -> R + Send, R: Send>(
131+
pub fn setup_callbacks_and_run_in_thread_pool_with_globals<F: FnOnce() -> R + Send, R: Send>(
130132
edition: Edition,
131133
_threads: usize,
132134
stderr: &Option<Arc<Mutex<Vec<u8>>>>,
@@ -140,7 +142,7 @@ pub fn spawn_thread_pool<F: FnOnce() -> R + Send, R: Send>(
140142

141143
crate::callbacks::setup_callbacks();
142144

143-
scoped_thread(cfg, || {
145+
let main_handler = move || {
144146
rustc_ast::with_session_globals(edition, || {
145147
ty::tls::GCX_PTR.set(&Lock::new(0), || {
146148
if let Some(stderr) = stderr {
@@ -149,22 +151,21 @@ pub fn spawn_thread_pool<F: FnOnce() -> R + Send, R: Send>(
149151
f()
150152
})
151153
})
152-
})
154+
};
155+
156+
scoped_thread(cfg, main_handler)
153157
}
154158

155159
#[cfg(parallel_compiler)]
156-
pub fn spawn_thread_pool<F: FnOnce() -> R + Send, R: Send>(
160+
pub fn setup_callbacks_and_run_in_thread_pool_with_globals<F: FnOnce() -> R + Send, R: Send>(
157161
edition: Edition,
158162
threads: usize,
159163
stderr: &Option<Arc<Mutex<Vec<u8>>>>,
160164
f: F,
161165
) -> R {
162-
use rayon::{ThreadBuilder, ThreadPool, ThreadPoolBuilder};
163-
164-
let gcx_ptr = &Lock::new(0);
165166
crate::callbacks::setup_callbacks();
166167

167-
let mut config = ThreadPoolBuilder::new()
168+
let mut config = rayon::ThreadPoolBuilder::new()
168169
.thread_name(|_| "rustc".to_string())
169170
.acquire_thread_handler(jobserver::acquire_thread)
170171
.release_thread_handler(jobserver::release_thread)
@@ -175,7 +176,7 @@ pub fn spawn_thread_pool<F: FnOnce() -> R + Send, R: Send>(
175176
config = config.stack_size(size);
176177
}
177178

178-
let with_pool = move |pool: &ThreadPool| pool.install(move || f());
179+
let with_pool = move |pool: &rayon::ThreadPool| pool.install(move || f());
179180

180181
rustc_ast::with_session_globals(edition, || {
181182
rustc_ast::SESSION_GLOBALS.with(|ast_session_globals| {
@@ -185,13 +186,15 @@ pub fn spawn_thread_pool<F: FnOnce() -> R + Send, R: Send>(
185186
// span_session_globals are captured and set on the new
186187
// threads. ty::tls::with_thread_locals sets up thread local
187188
// callbacks from librustc_ast.
188-
let main_handler = move |thread: ThreadBuilder| {
189+
let main_handler = move |thread: rayon::ThreadBuilder| {
189190
rustc_ast::SESSION_GLOBALS.set(ast_session_globals, || {
190191
rustc_span::SESSION_GLOBALS.set(span_session_globals, || {
191-
if let Some(stderr) = stderr {
192-
io::set_panic(Some(box Sink(stderr.clone())));
193-
}
194-
ty::tls::GCX_PTR.set(gcx_ptr, || thread.run())
192+
ty::tls::GCX_PTR.set(&Lock::new(0), || {
193+
if let Some(stderr) = stderr {
194+
io::set_panic(Some(box Sink(stderr.clone())));
195+
}
196+
thread.run()
197+
})
195198
})
196199
})
197200
};

src/librustdoc/core.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ pub fn run_core(options: RustdocOptions) -> (clean::Crate, RenderInfo, RenderOpt
376376
registry: rustc_driver::diagnostics_registry(),
377377
};
378378

379-
interface::run_compiler_in_existing_thread_pool(config, |compiler| {
379+
interface::create_compiler_and_run(config, |compiler| {
380380
compiler.enter(|queries| {
381381
let sess = compiler.session();
382382

src/librustdoc/lib.rs

+27-32
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,10 @@ fn main_args(args: &[String]) -> i32 {
437437
Ok(opts) => opts,
438438
Err(code) => return code,
439439
};
440-
rustc_interface::interface::default_thread_pool(options.edition, move || main_options(options))
440+
rustc_interface::interface::setup_callbacks_and_run_in_default_thread_pool_with_globals(
441+
options.edition,
442+
move || main_options(options),
443+
)
441444
}
442445

443446
fn wrap_return(diag: &rustc_errors::Handler, res: Result<(), String>) -> i32 {
@@ -471,7 +474,29 @@ fn main_options(options: config::Options) -> i32 {
471474
// but we can't crates the Handler ahead of time because it's not Send
472475
let diag_opts = (options.error_format, options.edition, options.debugging_options.clone());
473476
let show_coverage = options.show_coverage;
474-
rust_input(options, move |out| {
477+
478+
// First, parse the crate and extract all relevant information.
479+
info!("starting to run rustc");
480+
481+
// Interpret the input file as a rust source file, passing it through the
482+
// compiler all the way through the analysis passes. The rustdoc output is
483+
// then generated from the cleaned AST of the crate. This runs all the
484+
// plug/cleaning passes.
485+
let result = rustc_driver::catch_fatal_errors(move || {
486+
let crate_name = options.crate_name.clone();
487+
let crate_version = options.crate_version.clone();
488+
let (mut krate, renderinfo, renderopts) = core::run_core(options);
489+
490+
info!("finished with rustc");
491+
492+
if let Some(name) = crate_name {
493+
krate.name = name
494+
}
495+
496+
krate.version = crate_version;
497+
498+
let out = Output { krate, renderinfo, renderopts };
499+
475500
if show_coverage {
476501
// if we ran coverage, bail early, we don't need to also generate docs at this point
477502
// (also we didn't load in any of the useful passes)
@@ -491,36 +516,6 @@ fn main_options(options: config::Options) -> i32 {
491516
rustc_driver::EXIT_FAILURE
492517
}
493518
}
494-
})
495-
}
496-
497-
/// Interprets the input file as a rust source file, passing it through the
498-
/// compiler all the way through the analysis passes. The rustdoc output is then
499-
/// generated from the cleaned AST of the crate.
500-
///
501-
/// This form of input will run all of the plug/cleaning passes
502-
fn rust_input<R, F>(options: config::Options, f: F) -> R
503-
where
504-
R: 'static + Send,
505-
F: 'static + Send + FnOnce(Output) -> R,
506-
{
507-
// First, parse the crate and extract all relevant information.
508-
info!("starting to run rustc");
509-
510-
let result = rustc_driver::catch_fatal_errors(move || {
511-
let crate_name = options.crate_name.clone();
512-
let crate_version = options.crate_version.clone();
513-
let (mut krate, renderinfo, renderopts) = core::run_core(options);
514-
515-
info!("finished with rustc");
516-
517-
if let Some(name) = crate_name {
518-
krate.name = name
519-
}
520-
521-
krate.version = crate_version;
522-
523-
f(Output { krate, renderinfo, renderopts })
524519
});
525520

526521
match result {

0 commit comments

Comments
 (0)