Skip to content

Commit

Permalink
Unrolled build for rust-lang#127184
Browse files Browse the repository at this point in the history
Rollup merge of rust-lang#127184 - bjorn3:interface_refactor2, r=Nadrieril

More refactorings to rustc_interface

Follow up to rust-lang#126834
  • Loading branch information
rust-timer authored Jul 4, 2024
2 parents aa1d4f6 + f276459 commit 0f56949
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 47 deletions.
21 changes: 4 additions & 17 deletions compiler/rustc_driver_impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use rustc_errors::{
};
use rustc_feature::find_gated_cfg;
use rustc_interface::util::{self, get_codegen_backend};
use rustc_interface::{interface, passes, Queries};
use rustc_interface::{interface, passes, Linker, Queries};
use rustc_lint::unerased_lint_store;
use rustc_metadata::creader::MetadataLoader;
use rustc_metadata::locator;
Expand All @@ -41,7 +41,6 @@ use rustc_session::getopts::{self, Matches};
use rustc_session::lint::{Lint, LintId};
use rustc_session::output::collect_crate_types;
use rustc_session::{config, filesearch, EarlyDiagCtxt, Session};
use rustc_span::def_id::LOCAL_CRATE;
use rustc_span::source_map::FileLoader;
use rustc_span::symbol::sym;
use rustc_span::FileName;
Expand Down Expand Up @@ -448,21 +447,9 @@ fn run_compiler(
return early_exit();
}

let linker = queries.codegen_and_build_linker()?;

// This must run after monomorphization so that all generic types
// have been instantiated.
if sess.opts.unstable_opts.print_type_sizes {
sess.code_stats.print_type_sizes();
}

if sess.opts.unstable_opts.print_vtable_sizes {
let crate_name = queries.global_ctxt()?.enter(|tcx| tcx.crate_name(LOCAL_CRATE));

sess.code_stats.print_vtable_sizes(crate_name);
}

Ok(Some(linker))
queries.global_ctxt()?.enter(|tcx| {
Ok(Some(Linker::codegen_and_build_linker(tcx, &*compiler.codegen_backend)?))
})
})?;

// Linking is done outside the `compiler.enter()` so that the
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_interface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub mod util;
pub use callbacks::setup_callbacks;
pub use interface::{run_compiler, Config};
pub use passes::DEFAULT_QUERY_PROVIDERS;
pub use queries::Queries;
pub use queries::{Linker, Queries};

#[cfg(test)]
mod tests;
Expand Down
61 changes: 34 additions & 27 deletions compiler/rustc_interface/src/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,6 @@ impl<'a, 'tcx> QueryResult<'a, &'tcx GlobalCtxt<'tcx>> {
}
}

impl<T> Default for Query<T> {
fn default() -> Self {
Query { result: RefCell::new(None) }
}
}

pub struct Queries<'tcx> {
compiler: &'tcx Compiler,
gcx_cell: OnceLock<GlobalCtxt<'tcx>>,
Expand All @@ -90,8 +84,8 @@ impl<'tcx> Queries<'tcx> {
gcx_cell: OnceLock::new(),
arena: WorkerLocal::new(|_| Arena::default()),
hir_arena: WorkerLocal::new(|_| rustc_hir::Arena::default()),
parse: Default::default(),
gcx: Default::default(),
parse: Query { result: RefCell::new(None) },
gcx: Query { result: RefCell::new(None) },
}
}

Expand All @@ -116,23 +110,6 @@ impl<'tcx> Queries<'tcx> {
)
})
}

pub fn codegen_and_build_linker(&'tcx self) -> Result<Linker> {
self.global_ctxt()?.enter(|tcx| {
let ongoing_codegen = passes::start_codegen(&*self.compiler.codegen_backend, tcx)?;

Ok(Linker {
dep_graph: tcx.dep_graph.clone(),
output_filenames: tcx.output_filenames(()).clone(),
crate_hash: if tcx.needs_crate_hash() {
Some(tcx.crate_hash(LOCAL_CRATE))
} else {
None
},
ongoing_codegen,
})
})
}
}

pub struct Linker {
Expand All @@ -144,6 +121,36 @@ pub struct Linker {
}

impl Linker {
pub fn codegen_and_build_linker(
tcx: TyCtxt<'_>,
codegen_backend: &dyn CodegenBackend,
) -> Result<Linker> {
let ongoing_codegen = passes::start_codegen(codegen_backend, tcx)?;

// This must run after monomorphization so that all generic types
// have been instantiated.
if tcx.sess.opts.unstable_opts.print_type_sizes {
tcx.sess.code_stats.print_type_sizes();
}

if tcx.sess.opts.unstable_opts.print_vtable_sizes {
let crate_name = tcx.crate_name(LOCAL_CRATE);

tcx.sess.code_stats.print_vtable_sizes(crate_name);
}

Ok(Linker {
dep_graph: tcx.dep_graph.clone(),
output_filenames: tcx.output_filenames(()).clone(),
crate_hash: if tcx.needs_crate_hash() {
Some(tcx.crate_hash(LOCAL_CRATE))
} else {
None
},
ongoing_codegen,
})
}

pub fn link(self, sess: &Session, codegen_backend: &dyn CodegenBackend) -> Result<()> {
let (codegen_results, work_products) =
codegen_backend.join_codegen(self.ongoing_codegen, sess, &self.output_filenames);
Expand Down Expand Up @@ -197,7 +204,7 @@ impl Compiler {
F: for<'tcx> FnOnce(&'tcx Queries<'tcx>) -> T,
{
// Must declare `_timer` first so that it is dropped after `queries`.
let mut _timer = None;
let _timer;
let queries = Queries::new(self);
let ret = f(&queries);

Expand All @@ -220,7 +227,7 @@ impl Compiler {

// The timer's lifetime spans the dropping of `queries`, which contains
// the global context.
_timer = Some(self.sess.timer("free_global_ctxt"));
_timer = self.sess.timer("free_global_ctxt");
if let Err((path, error)) = queries.finish() {
self.sess.dcx().emit_fatal(errors::FailedWritingFile { path: &path, error });
}
Expand Down
7 changes: 5 additions & 2 deletions tests/ui-fulldeps/run-compiler-twice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ extern crate rustc_span;

use std::path::{Path, PathBuf};

use rustc_interface::Linker;
use rustc_interface::interface;
use rustc_session::config::{Input, Options, OutFileName, OutputType, OutputTypes};
use rustc_span::FileName;
Expand Down Expand Up @@ -78,8 +79,10 @@ fn compile(code: String, output: PathBuf, sysroot: PathBuf, linker: Option<&Path

interface::run_compiler(config, |compiler| {
let linker = compiler.enter(|queries| {
queries.global_ctxt()?.enter(|tcx| tcx.analysis(()))?;
queries.codegen_and_build_linker()
queries.global_ctxt()?.enter(|tcx| {
tcx.analysis(())?;
Linker::codegen_and_build_linker(tcx, &*compiler.codegen_backend)
})
});
linker.unwrap().link(&compiler.sess, &*compiler.codegen_backend).unwrap();
});
Expand Down

0 comments on commit 0f56949

Please sign in to comment.