]>
Commit | Line | Data |
---|---|---|
532ac7d7 | 1 | use crate::interface::{Compiler, Result}; |
60c5eb7d | 2 | use crate::passes::{self, BoxedResolver, QueryContext}; |
532ac7d7 | 3 | |
3dfed10e | 4 | use rustc_ast as ast; |
ba9703b0 | 5 | use rustc_codegen_ssa::traits::CodegenBackend; |
5e7ed085 | 6 | use rustc_codegen_ssa::CodegenResults; |
29967ef6 | 7 | use rustc_data_structures::svh::Svh; |
f9f354fc | 8 | use rustc_data_structures::sync::{Lrc, OnceCell, WorkerLocal}; |
dfeec247 XL |
9 | use rustc_hir::def_id::LOCAL_CRATE; |
10 | use rustc_incremental::DepGraphFuture; | |
11 | use rustc_lint::LintStore; | |
ba9703b0 XL |
12 | use rustc_middle::arena::Arena; |
13 | use rustc_middle::dep_graph::DepGraph; | |
136023e0 | 14 | use rustc_middle::ty::{GlobalCtxt, TyCtxt}; |
6a06907d | 15 | use rustc_query_impl::Queries as TcxQueries; |
29967ef6 | 16 | use rustc_session::config::{self, OutputFilenames, OutputType}; |
ba9703b0 XL |
17 | use rustc_session::{output::find_crate_name, Session}; |
18 | use rustc_span::symbol::sym; | |
532ac7d7 | 19 | use std::any::Any; |
dfeec247 | 20 | use std::cell::{Ref, RefCell, RefMut}; |
dfeec247 | 21 | use std::rc::Rc; |
532ac7d7 XL |
22 | |
23 | /// Represent the result of a query. | |
fc512014 XL |
24 | /// |
25 | /// This result can be stolen with the [`take`] method and generated with the [`compute`] method. | |
26 | /// | |
27 | /// [`take`]: Self::take | |
28 | /// [`compute`]: Self::compute | |
532ac7d7 XL |
29 | pub struct Query<T> { |
30 | result: RefCell<Option<Result<T>>>, | |
31 | } | |
32 | ||
33 | impl<T> Query<T> { | |
34 | fn compute<F: FnOnce() -> Result<T>>(&self, f: F) -> Result<&Query<T>> { | |
35 | let mut result = self.result.borrow_mut(); | |
36 | if result.is_none() { | |
37 | *result = Some(f()); | |
38 | } | |
39 | result.as_ref().unwrap().as_ref().map(|_| self).map_err(|err| *err) | |
40 | } | |
41 | ||
42 | /// Takes ownership of the query result. Further attempts to take or peek the query | |
60c5eb7d | 43 | /// result will panic unless it is generated by calling the `compute` method. |
532ac7d7 | 44 | pub fn take(&self) -> T { |
dfeec247 | 45 | self.result.borrow_mut().take().expect("missing query result").unwrap() |
532ac7d7 XL |
46 | } |
47 | ||
532ac7d7 XL |
48 | /// Borrows the query result using the RefCell. Panics if the result is stolen. |
49 | pub fn peek(&self) -> Ref<'_, T> { | |
50 | Ref::map(self.result.borrow(), |r| { | |
51 | r.as_ref().unwrap().as_ref().expect("missing query result") | |
52 | }) | |
53 | } | |
54 | ||
55 | /// Mutably borrows the query result using the RefCell. Panics if the result is stolen. | |
56 | pub fn peek_mut(&self) -> RefMut<'_, T> { | |
57 | RefMut::map(self.result.borrow_mut(), |r| { | |
58 | r.as_mut().unwrap().as_mut().expect("missing query result") | |
59 | }) | |
60 | } | |
61 | } | |
62 | ||
63 | impl<T> Default for Query<T> { | |
64 | fn default() -> Self { | |
dfeec247 | 65 | Query { result: RefCell::new(None) } |
532ac7d7 XL |
66 | } |
67 | } | |
68 | ||
60c5eb7d XL |
69 | pub struct Queries<'tcx> { |
70 | compiler: &'tcx Compiler, | |
f9f354fc | 71 | gcx: OnceCell<GlobalCtxt<'tcx>>, |
6a06907d | 72 | queries: OnceCell<TcxQueries<'tcx>>, |
60c5eb7d | 73 | |
60c5eb7d | 74 | arena: WorkerLocal<Arena<'tcx>>, |
ba9703b0 | 75 | hir_arena: WorkerLocal<rustc_ast_lowering::Arena<'tcx>>, |
60c5eb7d | 76 | |
532ac7d7 XL |
77 | dep_graph_future: Query<Option<DepGraphFuture>>, |
78 | parse: Query<ast::Crate>, | |
79 | crate_name: Query<String>, | |
60c5eb7d | 80 | register_plugins: Query<(ast::Crate, Lrc<LintStore>)>, |
136023e0 | 81 | expansion: Query<(Rc<ast::Crate>, Rc<RefCell<BoxedResolver>>, Lrc<LintStore>)>, |
532ac7d7 | 82 | dep_graph: Query<DepGraph>, |
532ac7d7 | 83 | prepare_outputs: Query<OutputFilenames>, |
60c5eb7d | 84 | global_ctxt: Query<QueryContext<'tcx>>, |
532ac7d7 | 85 | ongoing_codegen: Query<Box<dyn Any>>, |
532ac7d7 XL |
86 | } |
87 | ||
60c5eb7d XL |
88 | impl<'tcx> Queries<'tcx> { |
89 | pub fn new(compiler: &'tcx Compiler) -> Queries<'tcx> { | |
90 | Queries { | |
91 | compiler, | |
f9f354fc | 92 | gcx: OnceCell::new(), |
6a06907d | 93 | queries: OnceCell::new(), |
60c5eb7d | 94 | arena: WorkerLocal::new(|_| Arena::default()), |
ba9703b0 | 95 | hir_arena: WorkerLocal::new(|_| rustc_ast_lowering::Arena::default()), |
60c5eb7d XL |
96 | dep_graph_future: Default::default(), |
97 | parse: Default::default(), | |
98 | crate_name: Default::default(), | |
99 | register_plugins: Default::default(), | |
100 | expansion: Default::default(), | |
101 | dep_graph: Default::default(), | |
60c5eb7d XL |
102 | prepare_outputs: Default::default(), |
103 | global_ctxt: Default::default(), | |
104 | ongoing_codegen: Default::default(), | |
105 | } | |
106 | } | |
107 | ||
108 | fn session(&self) -> &Lrc<Session> { | |
109 | &self.compiler.sess | |
110 | } | |
111 | fn codegen_backend(&self) -> &Lrc<Box<dyn CodegenBackend>> { | |
c295e0f8 | 112 | self.compiler.codegen_backend() |
60c5eb7d XL |
113 | } |
114 | ||
136023e0 | 115 | fn dep_graph_future(&self) -> Result<&Query<Option<DepGraphFuture>>> { |
60c5eb7d | 116 | self.dep_graph_future.compute(|| { |
136023e0 XL |
117 | let sess = self.session(); |
118 | Ok(sess.opts.build_dep_graph().then(|| rustc_incremental::load_dep_graph(sess))) | |
532ac7d7 XL |
119 | }) |
120 | } | |
121 | ||
122 | pub fn parse(&self) -> Result<&Query<ast::Crate>> { | |
60c5eb7d | 123 | self.parse.compute(|| { |
5e7ed085 FG |
124 | passes::parse(self.session(), &self.compiler.input) |
125 | .map_err(|mut parse_error| parse_error.emit()) | |
532ac7d7 XL |
126 | }) |
127 | } | |
128 | ||
60c5eb7d XL |
129 | pub fn register_plugins(&self) -> Result<&Query<(ast::Crate, Lrc<LintStore>)>> { |
130 | self.register_plugins.compute(|| { | |
532ac7d7 XL |
131 | let crate_name = self.crate_name()?.peek().clone(); |
132 | let krate = self.parse()?.take(); | |
133 | ||
dfeec247 | 134 | let empty: &(dyn Fn(&Session, &mut LintStore) + Sync + Send) = &|_, _| {}; |
c295e0f8 | 135 | let (krate, lint_store) = passes::register_plugins( |
532ac7d7 | 136 | self.session(), |
e74abb32 | 137 | &*self.codegen_backend().metadata_loader(), |
f9f354fc | 138 | self.compiler.register_lints.as_deref().unwrap_or_else(|| empty), |
532ac7d7 XL |
139 | krate, |
140 | &crate_name, | |
17df50a5 | 141 | )?; |
e74abb32 XL |
142 | |
143 | // Compute the dependency graph (in the background). We want to do | |
144 | // this as early as possible, to give the DepGraph maximum time to | |
145 | // load before dep_graph() is called, but it also can't happen | |
146 | // until after rustc_incremental::prepare_session_directory() is | |
147 | // called, which happens within passes::register_plugins(). | |
148 | self.dep_graph_future().ok(); | |
149 | ||
c295e0f8 | 150 | Ok((krate, Lrc::new(lint_store))) |
532ac7d7 XL |
151 | }) |
152 | } | |
153 | ||
154 | pub fn crate_name(&self) -> Result<&Query<String>> { | |
60c5eb7d | 155 | self.crate_name.compute(|| { |
fc512014 XL |
156 | Ok({ |
157 | let parse_result = self.parse()?; | |
158 | let krate = parse_result.peek(); | |
159 | // parse `#[crate_name]` even if `--crate-name` was passed, to make sure it matches. | |
160 | find_crate_name(self.session(), &krate.attrs, &self.compiler.input) | |
e74abb32 | 161 | }) |
532ac7d7 XL |
162 | }) |
163 | } | |
164 | ||
165 | pub fn expansion( | |
dfeec247 | 166 | &self, |
136023e0 | 167 | ) -> Result<&Query<(Rc<ast::Crate>, Rc<RefCell<BoxedResolver>>, Lrc<LintStore>)>> { |
3dfed10e | 168 | tracing::trace!("expansion"); |
60c5eb7d | 169 | self.expansion.compute(|| { |
532ac7d7 | 170 | let crate_name = self.crate_name()?.peek().clone(); |
60c5eb7d | 171 | let (krate, lint_store) = self.register_plugins()?.take(); |
dfeec247 | 172 | let _timer = self.session().timer("configure_and_expand"); |
136023e0 XL |
173 | let sess = self.session(); |
174 | let mut resolver = passes::create_resolver( | |
175 | sess.clone(), | |
e74abb32 | 176 | self.codegen_backend().metadata_loader(), |
136023e0 | 177 | &krate, |
532ac7d7 | 178 | &crate_name, |
136023e0 XL |
179 | ); |
180 | let krate = resolver.access(|resolver| { | |
c295e0f8 | 181 | passes::configure_and_expand(sess, &lint_store, krate, &crate_name, resolver) |
136023e0 XL |
182 | })?; |
183 | Ok((Rc::new(krate), Rc::new(RefCell::new(resolver)), lint_store)) | |
532ac7d7 XL |
184 | }) |
185 | } | |
186 | ||
136023e0 | 187 | fn dep_graph(&self) -> Result<&Query<DepGraph>> { |
60c5eb7d | 188 | self.dep_graph.compute(|| { |
136023e0 XL |
189 | let sess = self.session(); |
190 | let future_opt = self.dep_graph_future()?.take(); | |
191 | let dep_graph = future_opt | |
192 | .and_then(|future| { | |
532ac7d7 | 193 | let (prev_graph, prev_work_products) = |
136023e0 | 194 | sess.time("blocked_on_dep_graph_loading", || future.open().open(sess)); |
532ac7d7 | 195 | |
136023e0 XL |
196 | rustc_incremental::build_dep_graph(sess, prev_graph, prev_work_products) |
197 | }) | |
198 | .unwrap_or_else(DepGraph::new_disabled); | |
199 | Ok(dep_graph) | |
532ac7d7 XL |
200 | }) |
201 | } | |
202 | ||
203 | pub fn prepare_outputs(&self) -> Result<&Query<OutputFilenames>> { | |
60c5eb7d | 204 | self.prepare_outputs.compute(|| { |
136023e0 | 205 | let (krate, boxed_resolver, _) = &*self.expansion()?.peek(); |
17df50a5 | 206 | let crate_name = self.crate_name()?.peek(); |
60c5eb7d | 207 | passes::prepare_outputs( |
dfeec247 XL |
208 | self.session(), |
209 | self.compiler, | |
136023e0 XL |
210 | krate, |
211 | &*boxed_resolver, | |
dfeec247 | 212 | &crate_name, |
60c5eb7d | 213 | ) |
532ac7d7 XL |
214 | }) |
215 | } | |
216 | ||
60c5eb7d XL |
217 | pub fn global_ctxt(&'tcx self) -> Result<&Query<QueryContext<'tcx>>> { |
218 | self.global_ctxt.compute(|| { | |
532ac7d7 XL |
219 | let crate_name = self.crate_name()?.peek().clone(); |
220 | let outputs = self.prepare_outputs()?.peek().clone(); | |
74b04a01 | 221 | let dep_graph = self.dep_graph()?.peek().clone(); |
136023e0 | 222 | let (krate, resolver, lint_store) = self.expansion()?.take(); |
532ac7d7 | 223 | Ok(passes::create_global_ctxt( |
60c5eb7d | 224 | self.compiler, |
e74abb32 | 225 | lint_store, |
74b04a01 XL |
226 | krate, |
227 | dep_graph, | |
136023e0 | 228 | resolver, |
532ac7d7 | 229 | outputs, |
60c5eb7d | 230 | &crate_name, |
6a06907d | 231 | &self.queries, |
60c5eb7d | 232 | &self.gcx, |
60c5eb7d | 233 | &self.arena, |
136023e0 | 234 | &self.hir_arena, |
60c5eb7d | 235 | )) |
532ac7d7 XL |
236 | }) |
237 | } | |
238 | ||
60c5eb7d XL |
239 | pub fn ongoing_codegen(&'tcx self) -> Result<&Query<Box<dyn Any>>> { |
240 | self.ongoing_codegen.compute(|| { | |
532ac7d7 XL |
241 | let outputs = self.prepare_outputs()?; |
242 | self.global_ctxt()?.peek_mut().enter(|tcx| { | |
17df50a5 | 243 | tcx.analysis(()).ok(); |
532ac7d7 XL |
244 | |
245 | // Don't do code generation if there were any errors | |
246 | self.session().compile_status()?; | |
247 | ||
5869c6ff | 248 | // Hook for UI tests. |
ba9703b0 XL |
249 | Self::check_for_rustc_errors_attr(tcx); |
250 | ||
dfeec247 | 251 | Ok(passes::start_codegen(&***self.codegen_backend(), tcx, &*outputs.peek())) |
532ac7d7 XL |
252 | }) |
253 | }) | |
254 | } | |
255 | ||
ba9703b0 | 256 | /// Check for the `#[rustc_error]` annotation, which forces an error in codegen. This is used |
5869c6ff | 257 | /// to write UI tests that actually test that compilation succeeds without reporting |
ba9703b0 XL |
258 | /// an error. |
259 | fn check_for_rustc_errors_attr(tcx: TyCtxt<'_>) { | |
5e7ed085 | 260 | let Some((def_id, _)) = tcx.entry_fn(()) else { return }; |
04454e1e | 261 | for attr in tcx.get_attrs(def_id, sym::rustc_error) { |
ba9703b0 XL |
262 | match attr.meta_item_list() { |
263 | // Check if there is a `#[rustc_error(delay_span_bug_from_inside_query)]`. | |
264 | Some(list) | |
265 | if list.iter().any(|list_item| { | |
266 | matches!( | |
267 | list_item.ident().map(|i| i.name), | |
268 | Some(sym::delay_span_bug_from_inside_query) | |
269 | ) | |
270 | }) => | |
271 | { | |
272 | tcx.ensure().trigger_delay_span_bug(def_id); | |
273 | } | |
274 | ||
275 | // Bare `#[rustc_error]`. | |
276 | None => { | |
277 | tcx.sess.span_fatal( | |
278 | tcx.def_span(def_id), | |
279 | "fatal error triggered by #[rustc_error]", | |
280 | ); | |
281 | } | |
282 | ||
283 | // Some other attribute. | |
284 | Some(_) => { | |
285 | tcx.sess.span_warn( | |
286 | tcx.def_span(def_id), | |
287 | "unexpected annotation used with `#[rustc_error(...)]!", | |
288 | ); | |
289 | } | |
290 | } | |
291 | } | |
292 | } | |
293 | ||
60c5eb7d | 294 | pub fn linker(&'tcx self) -> Result<Linker> { |
60c5eb7d XL |
295 | let sess = self.session().clone(); |
296 | let codegen_backend = self.codegen_backend().clone(); | |
532ac7d7 | 297 | |
17df50a5 XL |
298 | let dep_graph = self.dep_graph()?.peek().clone(); |
299 | let prepare_outputs = self.prepare_outputs()?.take(); | |
300 | let crate_hash = self.global_ctxt()?.peek_mut().enter(|tcx| tcx.crate_hash(LOCAL_CRATE)); | |
301 | let ongoing_codegen = self.ongoing_codegen()?.take(); | |
302 | ||
60c5eb7d XL |
303 | Ok(Linker { |
304 | sess, | |
60c5eb7d | 305 | codegen_backend, |
17df50a5 XL |
306 | |
307 | dep_graph, | |
308 | prepare_outputs, | |
309 | crate_hash, | |
310 | ongoing_codegen, | |
532ac7d7 XL |
311 | }) |
312 | } | |
60c5eb7d XL |
313 | } |
314 | ||
315 | pub struct Linker { | |
17df50a5 | 316 | // compilation inputs |
60c5eb7d | 317 | sess: Lrc<Session>, |
17df50a5 XL |
318 | codegen_backend: Lrc<Box<dyn CodegenBackend>>, |
319 | ||
320 | // compilation outputs | |
60c5eb7d XL |
321 | dep_graph: DepGraph, |
322 | prepare_outputs: OutputFilenames, | |
29967ef6 | 323 | crate_hash: Svh, |
60c5eb7d | 324 | ongoing_codegen: Box<dyn Any>, |
60c5eb7d XL |
325 | } |
326 | ||
327 | impl Linker { | |
328 | pub fn link(self) -> Result<()> { | |
a2a8927a XL |
329 | let (codegen_results, work_products) = self.codegen_backend.join_codegen( |
330 | self.ongoing_codegen, | |
331 | &self.sess, | |
332 | &self.prepare_outputs, | |
333 | )?; | |
29967ef6 XL |
334 | |
335 | self.sess.compile_status()?; | |
336 | ||
337 | let sess = &self.sess; | |
dfeec247 | 338 | let dep_graph = self.dep_graph; |
29967ef6 | 339 | sess.time("serialize_work_products", || { |
c295e0f8 | 340 | rustc_incremental::save_work_product_index(sess, &dep_graph, work_products) |
29967ef6 XL |
341 | }); |
342 | ||
343 | let prof = self.sess.prof.clone(); | |
dfeec247 | 344 | prof.generic_activity("drop_dep_graph").run(move || drop(dep_graph)); |
74b04a01 | 345 | |
29967ef6 XL |
346 | // Now that we won't touch anything in the incremental compilation directory |
347 | // any more, we can finalize it (which involves renaming it) | |
348 | rustc_incremental::finalize_session_directory(&self.sess, self.crate_hash); | |
349 | ||
74b04a01 XL |
350 | if !self |
351 | .sess | |
352 | .opts | |
353 | .output_types | |
354 | .keys() | |
355 | .any(|&i| i == OutputType::Exe || i == OutputType::Metadata) | |
356 | { | |
357 | return Ok(()); | |
358 | } | |
29967ef6 XL |
359 | |
360 | if sess.opts.debugging_opts.no_link { | |
5e7ed085 | 361 | let encoded = CodegenResults::serialize_rlink(&codegen_results); |
29967ef6 | 362 | let rlink_file = self.prepare_outputs.with_extension(config::RLINK_EXT); |
5e7ed085 | 363 | std::fs::write(&rlink_file, encoded).map_err(|err| { |
29967ef6 XL |
364 | sess.fatal(&format!("failed to write file {}: {}", rlink_file.display(), err)); |
365 | })?; | |
366 | return Ok(()); | |
367 | } | |
368 | ||
5869c6ff | 369 | let _timer = sess.prof.verbose_generic_activity("link_crate"); |
74b04a01 | 370 | self.codegen_backend.link(&self.sess, codegen_results, &self.prepare_outputs) |
60c5eb7d XL |
371 | } |
372 | } | |
373 | ||
374 | impl Compiler { | |
375 | pub fn enter<F, T>(&self, f: F) -> T | |
dfeec247 XL |
376 | where |
377 | F: for<'tcx> FnOnce(&'tcx Queries<'tcx>) -> T, | |
60c5eb7d | 378 | { |
dfeec247 | 379 | let mut _timer = None; |
c295e0f8 | 380 | let queries = Queries::new(self); |
60c5eb7d XL |
381 | let ret = f(&queries); |
382 | ||
5869c6ff XL |
383 | // NOTE: intentionally does not compute the global context if it hasn't been built yet, |
384 | // since that likely means there was a parse error. | |
385 | if let Some(Ok(gcx)) = &mut *queries.global_ctxt.result.borrow_mut() { | |
386 | // We assume that no queries are run past here. If there are new queries | |
387 | // after this point, they'll show up as "<unknown>" in self-profiling data. | |
388 | { | |
389 | let _prof_timer = | |
390 | queries.session().prof.generic_activity("self_profile_alloc_query_strings"); | |
6a06907d | 391 | gcx.enter(rustc_query_impl::alloc_self_profile_query_strings); |
5869c6ff XL |
392 | } |
393 | ||
cdc7bbd5 XL |
394 | self.session() |
395 | .time("serialize_dep_graph", || gcx.enter(rustc_incremental::save_dep_graph)); | |
60c5eb7d XL |
396 | } |
397 | ||
dfeec247 XL |
398 | _timer = Some(self.session().timer("free_global_ctxt")); |
399 | ||
60c5eb7d XL |
400 | ret |
401 | } | |
532ac7d7 | 402 | } |