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