Skip to content

Commit 904994e

Browse files
authored
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`
2 parents c6710d1 + 1da1348 commit 904994e

File tree

5 files changed

+43
-61
lines changed

5 files changed

+43
-61
lines changed

compiler/rustc_codegen_ssa/src/back/write.rs

+22-34
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ use jobserver::{Acquired, Client};
1111
use rustc_ast::attr;
1212
use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
1313
use rustc_data_structures::memmap::Mmap;
14-
use rustc_data_structures::profiling::SelfProfilerRef;
15-
use rustc_data_structures::profiling::TimingGuard;
16-
use rustc_data_structures::profiling::VerboseTimingGuard;
14+
use rustc_data_structures::profiling::{SelfProfilerRef, VerboseTimingGuard};
1715
use rustc_data_structures::sync::Lrc;
1816
use rustc_errors::emitter::Emitter;
1917
use rustc_errors::{translation::Translate, DiagnosticId, FatalError, Handler, Level};
@@ -705,20 +703,6 @@ impl<B: WriteBackendMethods> WorkItem<B> {
705703
}
706704
}
707705

708-
fn start_profiling<'a>(&self, cgcx: &'a CodegenContext<B>) -> TimingGuard<'a> {
709-
match *self {
710-
WorkItem::Optimize(ref m) => {
711-
cgcx.prof.generic_activity_with_arg("codegen_module_optimize", &*m.name)
712-
}
713-
WorkItem::CopyPostLtoArtifacts(ref m) => cgcx
714-
.prof
715-
.generic_activity_with_arg("codegen_copy_artifacts_from_incr_cache", &*m.name),
716-
WorkItem::LTO(ref m) => {
717-
cgcx.prof.generic_activity_with_arg("codegen_module_perform_lto", m.name())
718-
}
719-
}
720-
}
721-
722706
/// Generate a short description of this work item suitable for use as a thread name.
723707
fn short_description(&self) -> String {
724708
// `pthread_setname()` on *nix is limited to 15 characters and longer names are ignored.
@@ -759,21 +743,6 @@ pub enum FatLTOInput<B: WriteBackendMethods> {
759743
InMemory(ModuleCodegen<B::Module>),
760744
}
761745

762-
fn execute_work_item<B: ExtraBackendMethods>(
763-
cgcx: &CodegenContext<B>,
764-
work_item: WorkItem<B>,
765-
) -> Result<WorkItemResult<B>, FatalError> {
766-
let module_config = cgcx.config(work_item.module_kind());
767-
768-
match work_item {
769-
WorkItem::Optimize(module) => execute_optimize_work_item(cgcx, module, module_config),
770-
WorkItem::CopyPostLtoArtifacts(module) => {
771-
Ok(execute_copy_from_cache_work_item(cgcx, module, module_config))
772-
}
773-
WorkItem::LTO(module) => execute_lto_work_item(cgcx, module, module_config),
774-
}
775-
}
776-
777746
/// Actual LTO type we end up choosing based on multiple factors.
778747
pub enum ComputedLtoType {
779748
No,
@@ -1706,8 +1675,27 @@ fn spawn_work<B: ExtraBackendMethods>(cgcx: CodegenContext<B>, work: WorkItem<B>
17061675
// as a diagnostic was already sent off to the main thread - just
17071676
// surface that there was an error in this worker.
17081677
bomb.result = {
1709-
let _prof_timer = work.start_profiling(&cgcx);
1710-
Some(execute_work_item(&cgcx, work))
1678+
let module_config = cgcx.config(work.module_kind());
1679+
1680+
Some(match work {
1681+
WorkItem::Optimize(m) => {
1682+
let _timer =
1683+
cgcx.prof.generic_activity_with_arg("codegen_module_optimize", &*m.name);
1684+
execute_optimize_work_item(&cgcx, m, module_config)
1685+
}
1686+
WorkItem::CopyPostLtoArtifacts(m) => {
1687+
let _timer = cgcx.prof.generic_activity_with_arg(
1688+
"codegen_copy_artifacts_from_incr_cache",
1689+
&*m.name,
1690+
);
1691+
Ok(execute_copy_from_cache_work_item(&cgcx, m, module_config))
1692+
}
1693+
WorkItem::LTO(m) => {
1694+
let _timer =
1695+
cgcx.prof.generic_activity_with_arg("codegen_module_perform_lto", m.name());
1696+
execute_lto_work_item(&cgcx, m, module_config)
1697+
}
1698+
})
17111699
};
17121700
})
17131701
.expect("failed to spawn thread");

compiler/rustc_driver_impl/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ fn run_compiler(
424424
return early_exit();
425425
}
426426

427-
queries.ongoing_codegen()?;
427+
let ongoing_codegen = queries.ongoing_codegen()?;
428428

429429
if sess.opts.unstable_opts.print_type_sizes {
430430
sess.code_stats.print_type_sizes();
@@ -437,7 +437,7 @@ fn run_compiler(
437437
sess.code_stats.print_vtable_sizes(crate_name);
438438
}
439439

440-
let linker = queries.linker()?;
440+
let linker = queries.linker(ongoing_codegen)?;
441441
Ok(Some(linker))
442442
})?;
443443

compiler/rustc_interface/src/passes.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -740,8 +740,8 @@ pub fn create_global_ctxt<'tcx>(
740740
})
741741
}
742742

743-
/// Runs the resolution, type-checking, region checking and other
744-
/// miscellaneous analysis passes on the crate.
743+
/// Runs the type-checking, region checking and other miscellaneous analysis
744+
/// passes on the crate.
745745
fn analysis(tcx: TyCtxt<'_>, (): ()) -> Result<()> {
746746
rustc_passes::hir_id_validator::check_crate(tcx);
747747

compiler/rustc_interface/src/queries.rs

+11-18
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ pub struct Queries<'tcx> {
9393
dep_graph: Query<DepGraph>,
9494
// This just points to what's in `gcx_cell`.
9595
gcx: Query<&'tcx GlobalCtxt<'tcx>>,
96-
ongoing_codegen: Query<Box<dyn Any>>,
9796
}
9897

9998
impl<'tcx> Queries<'tcx> {
@@ -110,7 +109,6 @@ impl<'tcx> Queries<'tcx> {
110109
register_plugins: Default::default(),
111110
dep_graph: Default::default(),
112111
gcx: Default::default(),
113-
ongoing_codegen: Default::default(),
114112
}
115113
}
116114

@@ -249,23 +247,19 @@ impl<'tcx> Queries<'tcx> {
249247
})
250248
}
251249

252-
pub fn ongoing_codegen(&'tcx self) -> Result<QueryResult<'_, Box<dyn Any>>> {
253-
self.ongoing_codegen.compute(|| {
254-
self.global_ctxt()?.enter(|tcx| {
255-
tcx.analysis(()).ok();
250+
pub fn ongoing_codegen(&'tcx self) -> Result<Box<dyn Any>> {
251+
self.global_ctxt()?.enter(|tcx| {
252+
// Don't do code generation if there were any errors
253+
self.session().compile_status()?;
256254

257-
// Don't do code generation if there were any errors
258-
self.session().compile_status()?;
255+
// If we have any delayed bugs, for example because we created TyKind::Error earlier,
256+
// it's likely that codegen will only cause more ICEs, obscuring the original problem
257+
self.session().diagnostic().flush_delayed();
259258

260-
// If we have any delayed bugs, for example because we created TyKind::Error earlier,
261-
// it's likely that codegen will only cause more ICEs, obscuring the original problem
262-
self.session().diagnostic().flush_delayed();
259+
// Hook for UI tests.
260+
Self::check_for_rustc_errors_attr(tcx);
263261

264-
// Hook for UI tests.
265-
Self::check_for_rustc_errors_attr(tcx);
266-
267-
Ok(passes::start_codegen(&***self.codegen_backend(), tcx))
268-
})
262+
Ok(passes::start_codegen(&***self.codegen_backend(), tcx))
269263
})
270264
}
271265

@@ -303,7 +297,7 @@ impl<'tcx> Queries<'tcx> {
303297
}
304298
}
305299

306-
pub fn linker(&'tcx self) -> Result<Linker> {
300+
pub fn linker(&'tcx self, ongoing_codegen: Box<dyn Any>) -> Result<Linker> {
307301
let sess = self.session().clone();
308302
let codegen_backend = self.codegen_backend().clone();
309303

@@ -314,7 +308,6 @@ impl<'tcx> Queries<'tcx> {
314308
tcx.dep_graph.clone(),
315309
)
316310
});
317-
let ongoing_codegen = self.ongoing_codegen()?.steal();
318311

319312
Ok(Linker {
320313
sess,

tests/run-make-fulldeps/issue-19371/foo.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,11 @@ fn compile(code: String, output: PathBuf, sysroot: PathBuf) {
6363
};
6464

6565
interface::run_compiler(config, |compiler| {
66-
// This runs all the passes prior to linking, too.
67-
let linker = compiler.enter(|queries| queries.linker());
68-
if let Ok(linker) = linker {
69-
linker.link();
70-
}
66+
let linker = compiler.enter(|queries| {
67+
queries.global_ctxt()?.enter(|tcx| tcx.analysis(()))?;
68+
let ongoing_codegen = queries.ongoing_codegen()?;
69+
queries.linker(ongoing_codegen)
70+
});
71+
linker.unwrap().link();
7172
});
7273
}

0 commit comments

Comments
 (0)