Skip to content

Commit b99513b

Browse files
committed
Have Queries own the GlobalCtxt.
The construction of the GlobalCtxt is moved from a generator's stack to the Queries struct. Since the GlobalCtxt requires the HIR Forest and the arenas to live longer, those are moved into Queries the same way. The resulting handling of objects is more brittle, because consumers of the Once objects need to be careful of their initialisation.
1 parent 58a9c73 commit b99513b

File tree

3 files changed

+57
-54
lines changed

3 files changed

+57
-54
lines changed

src/librustc_interface/passes.rs

Lines changed: 21 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use rustc_codegen_ssa::back::link::emit_metadata;
2222
use rustc_codegen_utils::codegen_backend::CodegenBackend;
2323
use rustc_codegen_utils::link::filename_for_metadata;
2424
use rustc_data_structures::{box_region_allow_access, declare_box_region_type, parallel};
25-
use rustc_data_structures::sync::{Lrc, ParallelIterator, par_iter};
25+
use rustc_data_structures::sync::{Lrc, Once, ParallelIterator, par_iter};
2626
use rustc_errors::PResult;
2727
use rustc_incremental;
2828
use rustc_metadata::cstore;
@@ -740,44 +740,41 @@ pub fn default_provide_extern(providers: &mut ty::query::Providers<'_>) {
740740
rustc_codegen_ssa::provide_extern(providers);
741741
}
742742

743-
declare_box_region_type!(
744-
pub BoxedGlobalCtxt,
745-
for('tcx),
746-
(&'tcx GlobalCtxt<'tcx>) -> ((), ())
747-
);
743+
pub struct BoxedGlobalCtxt<'tcx>(&'tcx GlobalCtxt<'tcx>);
748744

749-
impl BoxedGlobalCtxt {
745+
impl<'gcx> BoxedGlobalCtxt<'gcx> {
750746
pub fn enter<F, R>(&mut self, f: F) -> R
751747
where
752748
F: for<'tcx> FnOnce(TyCtxt<'tcx>) -> R,
753749
{
754-
self.access(|gcx| ty::tls::enter_global(gcx, |tcx| f(tcx)))
750+
ty::tls::enter_global(self.0, |tcx| f(tcx))
751+
}
752+
753+
pub fn print_stats(&self) {
754+
self.0.queries.print_stats()
755755
}
756756
}
757757

758-
pub fn create_global_ctxt(
759-
compiler: &Compiler,
758+
pub fn create_global_ctxt<'gcx>(
759+
compiler: &'gcx Compiler,
760760
lint_store: Lrc<lint::LintStore>,
761-
mut hir_forest: hir::map::Forest,
761+
hir_forest: &'gcx hir::map::Forest,
762762
mut resolver_outputs: ResolverOutputs,
763763
outputs: OutputFilenames,
764764
crate_name: &str,
765-
) -> BoxedGlobalCtxt {
766-
let sess = compiler.session().clone();
765+
global_ctxt: &'gcx Once<GlobalCtxt<'gcx>>,
766+
arenas: &'gcx Once<AllArenas>,
767+
) -> BoxedGlobalCtxt<'gcx> {
768+
let sess = &compiler.session();
767769
let codegen_backend = compiler.codegen_backend().clone();
768-
let crate_name = crate_name.to_string();
769770
let defs = mem::take(&mut resolver_outputs.definitions);
770771
let override_queries = compiler.override_queries;
771772

772-
let ((), result) = BoxedGlobalCtxt::new(static move || {
773-
let sess = &*sess;
774-
775-
let global_ctxt: Option<GlobalCtxt<'_>>;
776-
let arenas = AllArenas::new();
773+
let arenas = arenas.init_locking(|| AllArenas::new());
777774

778775
// Construct the HIR map.
779776
let hir_map = time(sess, "indexing HIR", || {
780-
hir::map::map_crate(sess, &*resolver_outputs.cstore, &mut hir_forest, defs)
777+
hir::map::map_crate(sess, &*resolver_outputs.cstore, &hir_forest, defs)
781778
});
782779

783780
let query_result_on_disk_cache = time(sess, "load query result cache", || {
@@ -796,7 +793,7 @@ pub fn create_global_ctxt(
796793
callback(sess, &mut local_providers, &mut extern_providers);
797794
}
798795

799-
let gcx = TyCtxt::create_global_ctxt(
796+
let gcx = global_ctxt.init_locking(move || TyCtxt::create_global_ctxt(
800797
sess,
801798
lint_store,
802799
local_providers,
@@ -807,26 +804,15 @@ pub fn create_global_ctxt(
807804
query_result_on_disk_cache,
808805
&crate_name,
809806
&outputs
810-
);
807+
));
811808

812-
global_ctxt = Some(gcx);
813-
let gcx = global_ctxt.as_ref().unwrap();
814-
815-
ty::tls::enter_global(gcx, |tcx| {
809+
ty::tls::enter_global(&gcx, |tcx| {
816810
// Do some initialization of the DepGraph that can only be done with the
817811
// tcx available.
818812
time(tcx.sess, "dep graph tcx init", || rustc_incremental::dep_graph_tcx_init(tcx));
819813
});
820814

821-
yield BoxedGlobalCtxt::initial_yield(());
822-
box_region_allow_access!(for('tcx), (&'tcx GlobalCtxt<'tcx>), (gcx));
823-
824-
if sess.opts.debugging_opts.query_stats {
825-
gcx.queries.print_stats();
826-
}
827-
});
828-
829-
result
815+
BoxedGlobalCtxt(gcx)
830816
}
831817

832818
/// Runs the resolution, type-checking, region checking and other

src/librustc_interface/queries.rs

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::interface::{Compiler, Result};
22
use crate::passes::{self, BoxedResolver, BoxedGlobalCtxt};
33

44
use rustc_incremental::DepGraphFuture;
5-
use rustc_data_structures::sync::Lrc;
5+
use rustc_data_structures::sync::{Lrc, Once};
66
use rustc_codegen_utils::codegen_backend::CodegenBackend;
77
use rustc::session::config::{OutputFilenames, OutputType};
88
use rustc::util::common::{time, ErrorReported};
@@ -12,7 +12,7 @@ use rustc::session::Session;
1212
use rustc::lint::LintStore;
1313
use rustc::hir::def_id::LOCAL_CRATE;
1414
use rustc::ty::steal::Steal;
15-
use rustc::ty::ResolverOutputs;
15+
use rustc::ty::{AllArenas, ResolverOutputs, GlobalCtxt};
1616
use rustc::dep_graph::DepGraph;
1717
use std::cell::{Ref, RefMut, RefCell};
1818
use std::rc::Rc;
@@ -70,23 +70,29 @@ impl<T> Default for Query<T> {
7070

7171
pub struct Queries<'comp> {
7272
compiler: &'comp Compiler,
73+
gcx: Once<GlobalCtxt<'comp>>,
74+
arenas: Once<AllArenas>,
75+
forest: Once<hir::map::Forest>,
7376

7477
dep_graph_future: Query<Option<DepGraphFuture>>,
7578
parse: Query<ast::Crate>,
7679
crate_name: Query<String>,
7780
register_plugins: Query<(ast::Crate, Lrc<LintStore>)>,
7881
expansion: Query<(ast::Crate, Steal<Rc<RefCell<BoxedResolver>>>, Lrc<LintStore>)>,
7982
dep_graph: Query<DepGraph>,
80-
lower_to_hir: Query<(Steal<hir::map::Forest>, Steal<ResolverOutputs>)>,
83+
lower_to_hir: Query<(&'comp hir::map::Forest, Steal<ResolverOutputs>)>,
8184
prepare_outputs: Query<OutputFilenames>,
82-
global_ctxt: Query<BoxedGlobalCtxt>,
85+
global_ctxt: Query<BoxedGlobalCtxt<'comp>>,
8386
ongoing_codegen: Query<Box<dyn Any>>,
8487
}
8588

8689
impl<'comp> Queries<'comp> {
8790
pub fn new(compiler: &'comp Compiler) -> Queries<'comp> {
8891
Queries {
8992
compiler,
93+
gcx: Once::new(),
94+
arenas: Once::new(),
95+
forest: Once::new(),
9096
dep_graph_future: Default::default(),
9197
parse: Default::default(),
9298
crate_name: Default::default(),
@@ -209,23 +215,24 @@ impl<'comp> Queries<'comp> {
209215
}
210216

211217
pub fn lower_to_hir(
212-
&self,
213-
) -> Result<&Query<(Steal<hir::map::Forest>, Steal<ResolverOutputs>)>> {
218+
&'comp self,
219+
) -> Result<&Query<(&'comp hir::map::Forest, Steal<ResolverOutputs>)>> {
214220
self.lower_to_hir.compute(|| {
215221
let expansion_result = self.expansion()?;
216222
let peeked = expansion_result.peek();
217223
let krate = &peeked.0;
218224
let resolver = peeked.1.steal();
219225
let lint_store = &peeked.2;
220-
let hir = Steal::new(resolver.borrow_mut().access(|resolver| {
226+
let hir = resolver.borrow_mut().access(|resolver| {
221227
passes::lower_to_hir(
222228
self.session(),
223229
lint_store,
224230
resolver,
225231
&*self.dep_graph()?.peek(),
226232
&krate
227233
)
228-
})?);
234+
})?;
235+
let hir = self.forest.init_locking(|| hir);
229236
Ok((hir, Steal::new(BoxedResolver::to_resolver_outputs(resolver))))
230237
})
231238
}
@@ -242,25 +249,27 @@ impl<'comp> Queries<'comp> {
242249
})
243250
}
244251

245-
pub fn global_ctxt(&self) -> Result<&Query<BoxedGlobalCtxt>> {
252+
pub fn global_ctxt(&'comp self) -> Result<&Query<BoxedGlobalCtxt<'comp>>> {
246253
self.global_ctxt.compute(|| {
247254
let crate_name = self.crate_name()?.peek().clone();
248255
let outputs = self.prepare_outputs()?.peek().clone();
249256
let lint_store = self.expansion()?.peek().2.clone();
250-
let hir = self.lower_to_hir()?;
251-
let hir = hir.peek();
252-
let (hir_forest, resolver_outputs) = &*hir;
257+
let hir = self.lower_to_hir()?.peek();
258+
let (ref hir_forest, ref resolver_outputs) = &*hir;
253259
Ok(passes::create_global_ctxt(
254260
self.compiler,
255261
lint_store,
256-
hir_forest.steal(),
262+
hir_forest,
257263
resolver_outputs.steal(),
258264
outputs,
259-
&crate_name))
265+
&crate_name,
266+
&self.gcx,
267+
&self.arenas,
268+
))
260269
})
261270
}
262271

263-
pub fn ongoing_codegen(&self) -> Result<&Query<Box<dyn Any>>> {
272+
pub fn ongoing_codegen(&'comp self) -> Result<&Query<Box<dyn Any>>> {
264273
self.ongoing_codegen.compute(|| {
265274
let outputs = self.prepare_outputs()?;
266275
self.global_ctxt()?.peek_mut().enter(|tcx| {
@@ -278,7 +287,7 @@ impl<'comp> Queries<'comp> {
278287
})
279288
}
280289

281-
pub fn linker(&self) -> Result<Linker> {
290+
pub fn linker(&'comp self) -> Result<Linker> {
282291
let dep_graph = self.dep_graph()?;
283292
let prepare_outputs = self.prepare_outputs()?;
284293
let ongoing_codegen = self.ongoing_codegen()?;
@@ -317,10 +326,18 @@ impl Linker {
317326

318327
impl Compiler {
319328
pub fn enter<F, T>(&self, f: F) -> T
320-
where F: FnOnce(&Queries<'_>) -> T
329+
where F: for<'tcx> FnOnce(&'tcx Queries<'tcx>) -> T
321330
{
322331
let queries = Queries::new(&self);
323-
f(&queries)
332+
let ret = f(&queries);
333+
334+
if self.session().opts.debugging_opts.query_stats {
335+
if let Ok(gcx) = queries.global_ctxt() {
336+
gcx.peek().print_stats();
337+
}
338+
}
339+
340+
ret
324341
}
325342

326343
// This method is different to all the other methods in `Compiler` because

src/librustdoc/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ pub fn run(options: Options) -> i32 {
8888
let tests = interface::run_compiler(config, |compiler| compiler.enter(|queries| {
8989
let lower_to_hir = queries.lower_to_hir()?;
9090

91-
let mut opts = scrape_test_config(lower_to_hir.peek().0.borrow().krate());
91+
let mut opts = scrape_test_config(lower_to_hir.peek().0.krate());
9292
opts.display_warnings |= options.display_warnings;
9393
let enable_per_target_ignores = options.enable_per_target_ignores;
9494
let mut collector = Collector::new(

0 commit comments

Comments
 (0)