Remove Queries::ongoing_codegen.

There's no need to store it in `Queries`. We can just use a local
variable, because it's always used shortly after it's produced.

The commit also removes the `tcx.analysis()` call in `ongoing_codegen`,
because it's easy to ensure that's done beforehand.

All this makes the dataflow within `run_compiler` easier to follow, at
the cost of making one test slightly more verbose, which I think is a
good tradeoff.
This commit is contained in:
Nicholas Nethercote 2023-06-21 11:26:49 +10:00
parent c696307a87
commit 1da1348924
4 changed files with 21 additions and 27 deletions

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 @@ pub fn new(compiler: &'tcx Compiler) -> 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,11 +247,8 @@ pub fn global_ctxt(&'tcx self) -> Result<QueryResult<'_, &'tcx GlobalCtxt<'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| {
tcx.analysis(()).ok();
// Don't do code generation if there were any errors // Don't do code generation if there were any errors
self.session().compile_status()?; self.session().compile_status()?;
@ -266,7 +261,6 @@ pub fn ongoing_codegen(&'tcx self) -> Result<QueryResult<'_, Box<dyn Any>>> {
Ok(passes::start_codegen(&***self.codegen_backend(), tcx)) Ok(passes::start_codegen(&***self.codegen_backend(), tcx))
}) })
})
} }
/// Check for the `#[rustc_error]` annotation, which forces an error in codegen. This is used /// Check for the `#[rustc_error]` annotation, which forces an error in codegen. This is used
@ -303,7 +297,7 @@ fn check_for_rustc_errors_attr(tcx: TyCtxt<'_>) {
} }
} }
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 @@ pub fn linker(&'tcx self) -> Result<Linker> {
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();
}); });
} }