Skip to content

Commit 468729d

Browse files
committed
Auto merge of #64016 - nnethercote:Compiler-fiddling, r=<try>
Streamline `Compiler` A few commits to clean up `Compiler`. r? @Zoxc
2 parents ef54f57 + 92a0cbd commit 468729d

File tree

4 files changed

+37
-66
lines changed

4 files changed

+37
-66
lines changed

src/librustc_driver/lib.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -383,17 +383,12 @@ pub fn run_compiler(
383383
mem::drop(compiler.expansion()?.take());
384384
}
385385

386-
compiler.ongoing_codegen()?;
387-
388-
// Drop GlobalCtxt after starting codegen to free memory
389-
mem::drop(compiler.global_ctxt()?.take());
386+
compiler.codegen_and_link()?;
390387

391388
if sess.opts.debugging_opts.print_type_sizes {
392389
sess.code_stats.borrow().print_type_sizes();
393390
}
394391

395-
compiler.link()?;
396-
397392
if sess.opts.debugging_opts.perf_stats {
398393
sess.print_perf_stats();
399394
}

src/librustc_interface/passes.rs

-4
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,6 @@ pub struct PluginInfo {
223223
}
224224

225225
pub fn register_plugins<'a>(
226-
compiler: &Compiler,
227226
sess: &'a Session,
228227
cstore: &'a CStore,
229228
mut krate: ast::Crate,
@@ -263,9 +262,6 @@ pub fn register_plugins<'a>(
263262
});
264263
}
265264

266-
// If necessary, compute the dependency graph (in the background).
267-
compiler.dep_graph_future().ok();
268-
269265
time(sess, "recursion limit", || {
270266
middle::recursion_limit::update_limits(sess, &krate);
271267
});

src/librustc_interface/queries.rs

+35-55
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@ use crate::interface::{Compiler, Result};
22
use crate::passes::{self, BoxedResolver, ExpansionResult, BoxedGlobalCtxt, PluginInfo};
33

44
use rustc_incremental::DepGraphFuture;
5-
use rustc::session::config::{OutputFilenames, OutputType};
5+
use rustc::session::config::OutputFilenames;
66
use rustc::util::common::{time, ErrorReported};
77
use rustc::hir;
88
use rustc::hir::def_id::LOCAL_CRATE;
99
use rustc::ty::steal::Steal;
1010
use rustc::dep_graph::DepGraph;
11+
use std::any::Any;
1112
use std::cell::{Ref, RefMut, RefCell};
13+
use std::mem;
1214
use std::rc::Rc;
1315
use std::sync::mpsc;
14-
use std::any::Any;
15-
use std::mem;
1616
use syntax::{self, ast};
1717

1818
/// Represent the result of a query.
@@ -83,8 +83,7 @@ pub(crate) struct Queries {
8383
codegen_channel: Query<(Steal<mpsc::Sender<Box<dyn Any + Send>>>,
8484
Steal<mpsc::Receiver<Box<dyn Any + Send>>>)>,
8585
global_ctxt: Query<BoxedGlobalCtxt>,
86-
ongoing_codegen: Query<Box<dyn Any>>,
87-
link: Query<()>,
86+
codegen_and_link: Query<()>,
8887
}
8988

9089
impl Compiler {
@@ -114,29 +113,38 @@ impl Compiler {
114113
let crate_name = self.crate_name()?.peek().clone();
115114
let krate = self.parse()?.take();
116115

117-
passes::register_plugins(
118-
self,
116+
let result = passes::register_plugins(
119117
self.session(),
120118
self.cstore(),
121119
krate,
122120
&crate_name,
123-
)
121+
);
122+
123+
// Compute the dependency graph (in the background). We want to do
124+
// this as early as possible, to give the DepGraph maximum time to
125+
// load before dep_graph() is called, but it also can't happen
126+
// until after rustc_incremental::prepare_session_directory() is
127+
// called, which happens within passes::register_plugins().
128+
self.dep_graph_future().ok();
129+
130+
result
124131
})
125132
}
126133

127134
pub fn crate_name(&self) -> Result<&Query<String>> {
128135
self.queries.crate_name.compute(|| {
129-
let parse_result = self.parse()?;
130-
let krate = parse_result.peek();
131-
let result = match self.crate_name {
136+
Ok(match self.crate_name {
132137
Some(ref crate_name) => crate_name.clone(),
133-
None => rustc_codegen_utils::link::find_crate_name(
134-
Some(self.session()),
135-
&krate.attrs,
136-
&self.input
137-
),
138-
};
139-
Ok(result)
138+
None => {
139+
let parse_result = self.parse()?;
140+
let krate = parse_result.peek();
141+
rustc_codegen_utils::link::find_crate_name(
142+
Some(self.session()),
143+
&krate.attrs,
144+
&self.input
145+
)
146+
}
147+
})
140148
})
141149
}
142150

@@ -194,7 +202,6 @@ impl Compiler {
194202

195203
pub fn prepare_outputs(&self) -> Result<&Query<OutputFilenames>> {
196204
self.queries.prepare_outputs.compute(|| {
197-
self.lower_to_hir()?;
198205
let krate = self.expansion()?;
199206
let krate = krate.peek();
200207
let crate_name = self.crate_name()?;
@@ -230,14 +237,14 @@ impl Compiler {
230237
})
231238
}
232239

233-
pub fn ongoing_codegen(&self) -> Result<&Query<Box<dyn Any>>> {
234-
self.queries.ongoing_codegen.compute(|| {
240+
pub fn codegen_and_link(&self) -> Result<&Query<()>> {
241+
self.queries.codegen_and_link.compute(|| {
235242
let rx = self.codegen_channel()?.peek().1.steal();
236243
let outputs = self.prepare_outputs()?;
237-
self.global_ctxt()?.peek_mut().enter(|tcx| {
244+
let ongoing_codegen = self.global_ctxt()?.peek_mut().enter(|tcx| {
238245
tcx.analysis(LOCAL_CRATE).ok();
239246

240-
// Don't do code generation if there were any errors
247+
// Don't do code generation if there were any errors.
241248
self.session().compile_status()?;
242249

243250
Ok(passes::start_codegen(
@@ -246,46 +253,19 @@ impl Compiler {
246253
rx,
247254
&*outputs.peek()
248255
))
249-
})
250-
})
251-
}
252-
253-
pub fn link(&self) -> Result<&Query<()>> {
254-
self.queries.link.compute(|| {
255-
let sess = self.session();
256+
})?;
256257

257-
let ongoing_codegen = self.ongoing_codegen()?.take();
258+
// Drop GlobalCtxt after starting codegen to free memory.
259+
mem::drop(self.global_ctxt()?.take());
258260

259261
self.codegen_backend().join_codegen_and_link(
260262
ongoing_codegen,
261-
sess,
263+
self.session(),
262264
&*self.dep_graph()?.peek(),
263-
&*self.prepare_outputs()?.peek(),
265+
&*outputs.peek(),
264266
).map_err(|_| ErrorReported)?;
265267

266268
Ok(())
267269
})
268270
}
269-
270-
pub fn compile(&self) -> Result<()> {
271-
self.prepare_outputs()?;
272-
273-
if self.session().opts.output_types.contains_key(&OutputType::DepInfo)
274-
&& self.session().opts.output_types.len() == 1
275-
{
276-
return Ok(())
277-
}
278-
279-
self.global_ctxt()?;
280-
281-
// Drop AST after creating GlobalCtxt to free memory
282-
mem::drop(self.expansion()?.take());
283-
284-
self.ongoing_codegen()?;
285-
286-
// Drop GlobalCtxt after starting codegen to free memory
287-
mem::drop(self.global_ctxt()?.take());
288-
289-
self.link().map(|_| ())
290-
}
291271
}

src/test/run-make-fulldeps/issue-19371/foo.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,6 @@ fn compile(code: String, output: PathBuf, sysroot: PathBuf) {
6262
};
6363

6464
interface::run_compiler(config, |compiler| {
65-
compiler.compile().ok();
65+
compiler.codegen_and_link().ok();
6666
});
6767
}

0 commit comments

Comments
 (0)