1
Fork 0

Rollup merge of #112830 - nnethercote:more-codegen-cleanups, r=oli-obk

More codegen cleanups

Some additional cleanups I found while looking closely at this code, following up from #112827.

r= `@oli-obk`
This commit is contained in:
Nilstrieb 2023-06-21 07:37:03 +02:00 committed by GitHub
commit 904994e101
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 43 additions and 61 deletions

View file

@ -11,9 +11,7 @@ use jobserver::{Acquired, Client};
use rustc_ast::attr; use rustc_ast::attr;
use rustc_data_structures::fx::{FxHashMap, FxIndexMap}; use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
use rustc_data_structures::memmap::Mmap; use rustc_data_structures::memmap::Mmap;
use rustc_data_structures::profiling::SelfProfilerRef; use rustc_data_structures::profiling::{SelfProfilerRef, VerboseTimingGuard};
use rustc_data_structures::profiling::TimingGuard;
use rustc_data_structures::profiling::VerboseTimingGuard;
use rustc_data_structures::sync::Lrc; use rustc_data_structures::sync::Lrc;
use rustc_errors::emitter::Emitter; use rustc_errors::emitter::Emitter;
use rustc_errors::{translation::Translate, DiagnosticId, FatalError, Handler, Level}; use rustc_errors::{translation::Translate, DiagnosticId, FatalError, Handler, Level};
@ -705,20 +703,6 @@ impl<B: WriteBackendMethods> WorkItem<B> {
} }
} }
fn start_profiling<'a>(&self, cgcx: &'a CodegenContext<B>) -> TimingGuard<'a> {
match *self {
WorkItem::Optimize(ref m) => {
cgcx.prof.generic_activity_with_arg("codegen_module_optimize", &*m.name)
}
WorkItem::CopyPostLtoArtifacts(ref m) => cgcx
.prof
.generic_activity_with_arg("codegen_copy_artifacts_from_incr_cache", &*m.name),
WorkItem::LTO(ref m) => {
cgcx.prof.generic_activity_with_arg("codegen_module_perform_lto", m.name())
}
}
}
/// Generate a short description of this work item suitable for use as a thread name. /// Generate a short description of this work item suitable for use as a thread name.
fn short_description(&self) -> String { fn short_description(&self) -> String {
// `pthread_setname()` on *nix is limited to 15 characters and longer names are ignored. // `pthread_setname()` on *nix is limited to 15 characters and longer names are ignored.
@ -759,21 +743,6 @@ pub enum FatLTOInput<B: WriteBackendMethods> {
InMemory(ModuleCodegen<B::Module>), InMemory(ModuleCodegen<B::Module>),
} }
fn execute_work_item<B: ExtraBackendMethods>(
cgcx: &CodegenContext<B>,
work_item: WorkItem<B>,
) -> Result<WorkItemResult<B>, FatalError> {
let module_config = cgcx.config(work_item.module_kind());
match work_item {
WorkItem::Optimize(module) => execute_optimize_work_item(cgcx, module, module_config),
WorkItem::CopyPostLtoArtifacts(module) => {
Ok(execute_copy_from_cache_work_item(cgcx, module, module_config))
}
WorkItem::LTO(module) => execute_lto_work_item(cgcx, module, module_config),
}
}
/// Actual LTO type we end up choosing based on multiple factors. /// Actual LTO type we end up choosing based on multiple factors.
pub enum ComputedLtoType { pub enum ComputedLtoType {
No, No,
@ -1706,8 +1675,27 @@ fn spawn_work<B: ExtraBackendMethods>(cgcx: CodegenContext<B>, work: WorkItem<B>
// as a diagnostic was already sent off to the main thread - just // as a diagnostic was already sent off to the main thread - just
// surface that there was an error in this worker. // surface that there was an error in this worker.
bomb.result = { bomb.result = {
let _prof_timer = work.start_profiling(&cgcx); let module_config = cgcx.config(work.module_kind());
Some(execute_work_item(&cgcx, work))
Some(match work {
WorkItem::Optimize(m) => {
let _timer =
cgcx.prof.generic_activity_with_arg("codegen_module_optimize", &*m.name);
execute_optimize_work_item(&cgcx, m, module_config)
}
WorkItem::CopyPostLtoArtifacts(m) => {
let _timer = cgcx.prof.generic_activity_with_arg(
"codegen_copy_artifacts_from_incr_cache",
&*m.name,
);
Ok(execute_copy_from_cache_work_item(&cgcx, m, module_config))
}
WorkItem::LTO(m) => {
let _timer =
cgcx.prof.generic_activity_with_arg("codegen_module_perform_lto", m.name());
execute_lto_work_item(&cgcx, m, module_config)
}
})
}; };
}) })
.expect("failed to spawn thread"); .expect("failed to spawn thread");

View file

@ -424,7 +424,7 @@ fn run_compiler(
return early_exit(); return early_exit();
} }
queries.ongoing_codegen()?; let ongoing_codegen = queries.ongoing_codegen()?;
if sess.opts.unstable_opts.print_type_sizes { if sess.opts.unstable_opts.print_type_sizes {
sess.code_stats.print_type_sizes(); sess.code_stats.print_type_sizes();
@ -437,7 +437,7 @@ fn run_compiler(
sess.code_stats.print_vtable_sizes(crate_name); sess.code_stats.print_vtable_sizes(crate_name);
} }
let linker = queries.linker()?; let linker = queries.linker(ongoing_codegen)?;
Ok(Some(linker)) Ok(Some(linker))
})?; })?;

View file

@ -740,8 +740,8 @@ pub fn create_global_ctxt<'tcx>(
}) })
} }
/// Runs the resolution, type-checking, region checking and other /// Runs the type-checking, region checking and other miscellaneous analysis
/// miscellaneous analysis passes on the crate. /// passes on the crate.
fn analysis(tcx: TyCtxt<'_>, (): ()) -> Result<()> { fn analysis(tcx: TyCtxt<'_>, (): ()) -> Result<()> {
rustc_passes::hir_id_validator::check_crate(tcx); rustc_passes::hir_id_validator::check_crate(tcx);

View file

@ -93,7 +93,6 @@ pub struct Queries<'tcx> {
dep_graph: Query<DepGraph>, dep_graph: Query<DepGraph>,
// This just points to what's in `gcx_cell`. // This just points to what's in `gcx_cell`.
gcx: Query<&'tcx GlobalCtxt<'tcx>>, gcx: Query<&'tcx GlobalCtxt<'tcx>>,
ongoing_codegen: Query<Box<dyn Any>>,
} }
impl<'tcx> Queries<'tcx> { impl<'tcx> Queries<'tcx> {
@ -110,7 +109,6 @@ impl<'tcx> Queries<'tcx> {
register_plugins: Default::default(), register_plugins: Default::default(),
dep_graph: Default::default(), dep_graph: Default::default(),
gcx: Default::default(), gcx: Default::default(),
ongoing_codegen: Default::default(),
} }
} }
@ -249,23 +247,19 @@ impl<'tcx> Queries<'tcx> {
}) })
} }
pub fn ongoing_codegen(&'tcx self) -> Result<QueryResult<'_, Box<dyn Any>>> { pub fn ongoing_codegen(&'tcx self) -> Result<Box<dyn Any>> {
self.ongoing_codegen.compute(|| { self.global_ctxt()?.enter(|tcx| {
self.global_ctxt()?.enter(|tcx| { // Don't do code generation if there were any errors
tcx.analysis(()).ok(); self.session().compile_status()?;
// Don't do code generation if there were any errors // If we have any delayed bugs, for example because we created TyKind::Error earlier,
self.session().compile_status()?; // it's likely that codegen will only cause more ICEs, obscuring the original problem
self.session().diagnostic().flush_delayed();
// If we have any delayed bugs, for example because we created TyKind::Error earlier, // Hook for UI tests.
// it's likely that codegen will only cause more ICEs, obscuring the original problem Self::check_for_rustc_errors_attr(tcx);
self.session().diagnostic().flush_delayed();
// Hook for UI tests. Ok(passes::start_codegen(&***self.codegen_backend(), tcx))
Self::check_for_rustc_errors_attr(tcx);
Ok(passes::start_codegen(&***self.codegen_backend(), tcx))
})
}) })
} }
@ -303,7 +297,7 @@ impl<'tcx> Queries<'tcx> {
} }
} }
pub fn linker(&'tcx self) -> Result<Linker> { pub fn linker(&'tcx self, ongoing_codegen: Box<dyn Any>) -> Result<Linker> {
let sess = self.session().clone(); let sess = self.session().clone();
let codegen_backend = self.codegen_backend().clone(); let codegen_backend = self.codegen_backend().clone();
@ -314,7 +308,6 @@ impl<'tcx> Queries<'tcx> {
tcx.dep_graph.clone(), tcx.dep_graph.clone(),
) )
}); });
let ongoing_codegen = self.ongoing_codegen()?.steal();
Ok(Linker { Ok(Linker {
sess, sess,

View file

@ -63,10 +63,11 @@ fn compile(code: String, output: PathBuf, sysroot: PathBuf) {
}; };
interface::run_compiler(config, |compiler| { interface::run_compiler(config, |compiler| {
// This runs all the passes prior to linking, too. let linker = compiler.enter(|queries| {
let linker = compiler.enter(|queries| queries.linker()); queries.global_ctxt()?.enter(|tcx| tcx.analysis(()))?;
if let Ok(linker) = linker { let ongoing_codegen = queries.ongoing_codegen()?;
linker.link(); queries.linker(ongoing_codegen)
} });
linker.unwrap().link();
}); });
} }