Skip to content

Commit 8e9bbc8

Browse files
committed
Move some code from Compiler::enter to GlobalCtxt::finish
1 parent bec24a2 commit 8e9bbc8

File tree

11 files changed

+64
-32
lines changed

11 files changed

+64
-32
lines changed

compiler/rustc_incremental/src/lib.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,15 @@ mod persist;
1616

1717
pub use persist::{
1818
LoadResult, copy_cgu_workproduct_to_incr_comp_cache_dir, finalize_session_directory,
19-
in_incr_comp_dir, in_incr_comp_dir_sess, load_query_result_cache, save_dep_graph,
20-
save_work_product_index, setup_dep_graph,
19+
in_incr_comp_dir, in_incr_comp_dir_sess, load_query_result_cache, save_work_product_index,
20+
setup_dep_graph,
2121
};
22+
use rustc_middle::util::Providers;
23+
24+
#[allow(missing_docs)]
25+
pub fn provide(providers: &mut Providers) {
26+
providers.hooks.save_dep_graph =
27+
|tcx| tcx.sess.time("serialize_dep_graph", || persist::save_dep_graph(tcx.tcx));
28+
}
2229

2330
rustc_fluent_macro::fluent_messages! { "../messages.ftl" }

compiler/rustc_incremental/src/persist/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ mod work_product;
1212

1313
pub use fs::{finalize_session_directory, in_incr_comp_dir, in_incr_comp_dir_sess};
1414
pub use load::{LoadResult, load_query_result_cache, setup_dep_graph};
15-
pub use save::{save_dep_graph, save_work_product_index};
15+
pub(crate) use save::save_dep_graph;
16+
pub use save::save_work_product_index;
1617
pub use work_product::copy_cgu_workproduct_to_incr_comp_cache_dir;

compiler/rustc_incremental/src/persist/save.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use crate::errors;
2525
///
2626
/// This function should only run after all queries have completed.
2727
/// Trying to execute a query afterwards would attempt to read the result cache we just dropped.
28-
pub fn save_dep_graph(tcx: TyCtxt<'_>) {
28+
pub(crate) fn save_dep_graph(tcx: TyCtxt<'_>) {
2929
debug!("save_dep_graph()");
3030
tcx.dep_graph.with_ignore(|| {
3131
let sess = tcx.sess;

compiler/rustc_interface/src/passes.rs

+2
Original file line numberDiff line numberDiff line change
@@ -704,10 +704,12 @@ pub static DEFAULT_QUERY_PROVIDERS: LazyLock<Providers> = LazyLock::new(|| {
704704
rustc_const_eval::provide(providers);
705705
rustc_middle::hir::provide(providers);
706706
rustc_borrowck::provide(providers);
707+
rustc_incremental::provide(providers);
707708
rustc_mir_build::provide(providers);
708709
rustc_mir_transform::provide(providers);
709710
rustc_monomorphize::provide(providers);
710711
rustc_privacy::provide(providers);
712+
rustc_query_impl::provide(providers);
711713
rustc_resolve::provide(providers);
712714
rustc_hir_analysis::provide(providers);
713715
rustc_hir_typeck::provide(providers);

compiler/rustc_interface/src/queries.rs

+6-24
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,12 @@ use rustc_hir::def_id::LOCAL_CRATE;
1212
use rustc_middle::arena::Arena;
1313
use rustc_middle::dep_graph::DepGraph;
1414
use rustc_middle::ty::{GlobalCtxt, TyCtxt};
15-
use rustc_serialize::opaque::FileEncodeResult;
1615
use rustc_session::Session;
1716
use rustc_session::config::{self, OutputFilenames, OutputType};
1817

1918
use crate::errors::FailedWritingFile;
2019
use crate::interface::{Compiler, Result};
21-
use crate::{errors, passes};
20+
use crate::passes;
2221

2322
/// Represent the result of a query.
2423
///
@@ -90,8 +89,10 @@ impl<'tcx> Queries<'tcx> {
9089
}
9190
}
9291

93-
pub fn finish(&self) -> FileEncodeResult {
94-
if let Some(gcx) = self.gcx_cell.get() { gcx.finish() } else { Ok(0) }
92+
pub fn finish(&'tcx self) {
93+
if let Some(gcx) = self.gcx_cell.get() {
94+
gcx.finish();
95+
}
9596
}
9697

9798
pub fn parse(&self) -> Result<QueryResult<'_, ast::Crate>> {
@@ -209,29 +210,10 @@ impl Compiler {
209210
let queries = Queries::new(self);
210211
let ret = f(&queries);
211212

212-
// NOTE: intentionally does not compute the global context if it hasn't been built yet,
213-
// since that likely means there was a parse error.
214-
if let Some(Ok(gcx)) = &mut *queries.gcx.result.borrow_mut() {
215-
let gcx = gcx.get_mut();
216-
// We assume that no queries are run past here. If there are new queries
217-
// after this point, they'll show up as "<unknown>" in self-profiling data.
218-
{
219-
let _prof_timer =
220-
queries.compiler.sess.prof.generic_activity("self_profile_alloc_query_strings");
221-
gcx.enter(rustc_query_impl::alloc_self_profile_query_strings);
222-
}
223-
224-
self.sess.time("serialize_dep_graph", || gcx.enter(rustc_incremental::save_dep_graph));
225-
226-
gcx.enter(rustc_query_impl::query_key_hash_verify_all);
227-
}
228-
229213
// The timer's lifetime spans the dropping of `queries`, which contains
230214
// the global context.
231215
_timer = self.sess.timer("free_global_ctxt");
232-
if let Err((path, error)) = queries.finish() {
233-
self.sess.dcx().emit_fatal(errors::FailedWritingFile { path: &path, error });
234-
}
216+
queries.finish();
235217

236218
ret
237219
}

compiler/rustc_middle/messages.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ middle_drop_check_overflow =
7373
7474
middle_erroneous_constant = erroneous constant encountered
7575
76+
middle_failed_writing_file =
77+
failed to write file {$path}: {$error}"
78+
7679
middle_layout_references_error =
7780
the type has an unknown layout
7881

compiler/rustc_middle/src/error.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use std::fmt;
2-
use std::path::PathBuf;
1+
use std::path::{Path, PathBuf};
2+
use std::{fmt, io};
33

44
use rustc_errors::codes::*;
55
use rustc_errors::{DiagArgName, DiagArgValue, DiagMessage};
@@ -18,6 +18,13 @@ pub struct DropCheckOverflow<'tcx> {
1818
pub overflow_ty: Ty<'tcx>,
1919
}
2020

21+
#[derive(Diagnostic)]
22+
#[diag(middle_failed_writing_file)]
23+
pub struct FailedWritingFile<'a> {
24+
pub path: &'a Path,
25+
pub error: io::Error,
26+
}
27+
2128
#[derive(Diagnostic)]
2229
#[diag(middle_opaque_hidden_type_mismatch)]
2330
pub struct OpaqueHiddenTypeMismatch<'tcx> {

compiler/rustc_middle/src/hooks/mod.rs

+13
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,19 @@ declare_hooks! {
108108
/// Returns `true` if we should codegen an instance in the local crate, or returns `false` if we
109109
/// can just link to the upstream crate and therefore don't need a mono item.
110110
hook should_codegen_locally(instance: crate::ty::Instance<'tcx>) -> bool;
111+
112+
hook alloc_self_profile_query_strings() -> ();
113+
114+
/// Saves and writes the DepGraph to the file system.
115+
///
116+
/// This function saves both the dep-graph and the query result cache,
117+
/// and drops the result cache.
118+
///
119+
/// This function should only run after all queries have completed.
120+
/// Trying to execute a query afterwards would attempt to read the result cache we just dropped.
121+
hook save_dep_graph() -> ();
122+
123+
hook query_key_hash_verify_all() -> ();
111124
}
112125

113126
#[cold]

compiler/rustc_middle/src/ty/context.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -1365,8 +1365,17 @@ impl<'tcx> GlobalCtxt<'tcx> {
13651365
tls::enter_context(&icx, || f(icx.tcx))
13661366
}
13671367

1368-
pub fn finish(&self) -> FileEncodeResult {
1369-
self.dep_graph.finish_encoding()
1368+
pub fn finish(&'tcx self) {
1369+
// We assume that no queries are run past here. If there are new queries
1370+
// after this point, they'll show up as "<unknown>" in self-profiling data.
1371+
self.enter(|tcx| tcx.alloc_self_profile_query_strings());
1372+
1373+
self.enter(|tcx| tcx.save_dep_graph());
1374+
self.enter(|tcx| tcx.query_key_hash_verify_all());
1375+
1376+
if let Err((path, error)) = self.dep_graph.finish_encoding() {
1377+
self.sess.dcx().emit_fatal(crate::error::FailedWritingFile { path: &path, error });
1378+
}
13701379
}
13711380
}
13721381

compiler/rustc_query_impl/src/lib.rs

+6
Original file line numberDiff line numberDiff line change
@@ -222,3 +222,9 @@ pub fn query_system<'tcx>(
222222
}
223223

224224
rustc_middle::rustc_query_append! { define_queries! }
225+
226+
pub fn provide(providers: &mut rustc_middle::util::Providers) {
227+
providers.hooks.alloc_self_profile_query_strings =
228+
|tcx| alloc_self_profile_query_strings(tcx.tcx);
229+
providers.hooks.query_key_hash_verify_all = |tcx| query_key_hash_verify_all(tcx.tcx);
230+
}

compiler/rustc_query_impl/src/profiling_support.rs

+2
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,8 @@ pub fn alloc_self_profile_query_strings(tcx: TyCtxt<'_>) {
252252
return;
253253
}
254254

255+
let _prof_timer = tcx.sess.prof.generic_activity("self_profile_alloc_query_strings");
256+
255257
let mut string_cache = QueryKeyStringCache::new();
256258

257259
for alloc in super::ALLOC_SELF_PROFILE_QUERY_STRINGS.iter() {

0 commit comments

Comments
 (0)