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