Skip to content

Commit bd2ff51

Browse files
committed
Move codegen_and_build_linker from Queries to Linker
1 parent 8127461 commit bd2ff51

File tree

4 files changed

+40
-34
lines changed

4 files changed

+40
-34
lines changed

compiler/rustc_driver_impl/src/lib.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use rustc_errors::{
3030
};
3131
use rustc_feature::find_gated_cfg;
3232
use rustc_interface::util::{self, get_codegen_backend};
33-
use rustc_interface::{interface, passes, Queries};
33+
use rustc_interface::{interface, passes, Linker, Queries};
3434
use rustc_lint::unerased_lint_store;
3535
use rustc_metadata::creader::MetadataLoader;
3636
use rustc_metadata::locator;
@@ -447,7 +447,9 @@ fn run_compiler(
447447
return early_exit();
448448
}
449449

450-
Ok(Some(queries.codegen_and_build_linker()?))
450+
queries.global_ctxt()?.enter(|tcx| {
451+
Ok(Some(Linker::codegen_and_build_linker(tcx, &*compiler.codegen_backend)?))
452+
})
451453
})?;
452454

453455
// Linking is done outside the `compiler.enter()` so that the

compiler/rustc_interface/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub mod util;
1616
pub use callbacks::setup_callbacks;
1717
pub use interface::{run_compiler, Config};
1818
pub use passes::DEFAULT_QUERY_PROVIDERS;
19-
pub use queries::Queries;
19+
pub use queries::{Linker, Queries};
2020

2121
#[cfg(test)]
2222
mod tests;

compiler/rustc_interface/src/queries.rs

+30-29
Original file line numberDiff line numberDiff line change
@@ -116,35 +116,6 @@ impl<'tcx> Queries<'tcx> {
116116
)
117117
})
118118
}
119-
120-
pub fn codegen_and_build_linker(&'tcx self) -> Result<Linker> {
121-
self.global_ctxt()?.enter(|tcx| {
122-
let ongoing_codegen = passes::start_codegen(&*self.compiler.codegen_backend, tcx)?;
123-
124-
// This must run after monomorphization so that all generic types
125-
// have been instantiated.
126-
if tcx.sess.opts.unstable_opts.print_type_sizes {
127-
tcx.sess.code_stats.print_type_sizes();
128-
}
129-
130-
if tcx.sess.opts.unstable_opts.print_vtable_sizes {
131-
let crate_name = tcx.crate_name(LOCAL_CRATE);
132-
133-
tcx.sess.code_stats.print_vtable_sizes(crate_name);
134-
}
135-
136-
Ok(Linker {
137-
dep_graph: tcx.dep_graph.clone(),
138-
output_filenames: tcx.output_filenames(()).clone(),
139-
crate_hash: if tcx.needs_crate_hash() {
140-
Some(tcx.crate_hash(LOCAL_CRATE))
141-
} else {
142-
None
143-
},
144-
ongoing_codegen,
145-
})
146-
})
147-
}
148119
}
149120

150121
pub struct Linker {
@@ -156,6 +127,36 @@ pub struct Linker {
156127
}
157128

158129
impl Linker {
130+
pub fn codegen_and_build_linker(
131+
tcx: TyCtxt<'_>,
132+
codegen_backend: &dyn CodegenBackend,
133+
) -> Result<Linker> {
134+
let ongoing_codegen = passes::start_codegen(codegen_backend, tcx)?;
135+
136+
// This must run after monomorphization so that all generic types
137+
// have been instantiated.
138+
if tcx.sess.opts.unstable_opts.print_type_sizes {
139+
tcx.sess.code_stats.print_type_sizes();
140+
}
141+
142+
if tcx.sess.opts.unstable_opts.print_vtable_sizes {
143+
let crate_name = tcx.crate_name(LOCAL_CRATE);
144+
145+
tcx.sess.code_stats.print_vtable_sizes(crate_name);
146+
}
147+
148+
Ok(Linker {
149+
dep_graph: tcx.dep_graph.clone(),
150+
output_filenames: tcx.output_filenames(()).clone(),
151+
crate_hash: if tcx.needs_crate_hash() {
152+
Some(tcx.crate_hash(LOCAL_CRATE))
153+
} else {
154+
None
155+
},
156+
ongoing_codegen,
157+
})
158+
}
159+
159160
pub fn link(self, sess: &Session, codegen_backend: &dyn CodegenBackend) -> Result<()> {
160161
let (codegen_results, work_products) =
161162
codegen_backend.join_codegen(self.ongoing_codegen, sess, &self.output_filenames);

tests/ui-fulldeps/run-compiler-twice.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ extern crate rustc_span;
1717

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

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

7980
interface::run_compiler(config, |compiler| {
8081
let linker = compiler.enter(|queries| {
81-
queries.global_ctxt()?.enter(|tcx| tcx.analysis(()))?;
82-
queries.codegen_and_build_linker()
82+
queries.global_ctxt()?.enter(|tcx| {
83+
tcx.analysis(())?;
84+
Linker::codegen_and_build_linker(tcx, &*compiler.codegen_backend)
85+
})
8386
});
8487
linker.unwrap().link(&compiler.sess, &*compiler.codegen_backend).unwrap();
8588
});

0 commit comments

Comments
 (0)