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