Skip to content

Commit

Permalink
Auto merge of rust-lang#121089 - oli-obk:create_def_feed, r=<try>
Browse files Browse the repository at this point in the history
Remove `feed_local_def_id`

best reviewed commit by commit

Basically I returned `TyCtxtFeed` from `create_def` and then preserved that in the local caches

based on rust-lang#121084

r? `@petrochenkov`
  • Loading branch information
bors committed Feb 19, 2024
2 parents e29a153 + a0aeca0 commit 9b35aff
Show file tree
Hide file tree
Showing 13 changed files with 193 additions and 82 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ pub fn lower_to_hir(tcx: TyCtxt<'_>, (): ()) -> hir::Crate<'_> {
tcx.ensure_with_value().early_lint_checks(());
tcx.ensure_with_value().debugger_visualizers(LOCAL_CRATE);
tcx.ensure_with_value().get_lang_items(());
let (mut resolver, krate) = tcx.resolver_for_lowering(()).steal();
let (mut resolver, krate) = tcx.resolver_for_lowering(()).0.steal();

let ast_index = index_crate(&resolver.node_id_to_def_id, &krate);
let mut owners = IndexVec::from_fn_n(
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_driver_impl/src/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ impl<'tcx> PrintExtra<'tcx> {
{
match self {
PrintExtra::AfterParsing { krate, .. } => f(krate),
PrintExtra::NeedsAstMap { tcx } => f(&tcx.resolver_for_lowering(()).borrow().1),
PrintExtra::NeedsAstMap { tcx } => f(&tcx.resolver_for_lowering(()).0.borrow().1),
}
}

Expand Down Expand Up @@ -279,7 +279,7 @@ pub fn print<'tcx>(sess: &Session, ppm: PpMode, ex: PrintExtra<'tcx>) {
}
AstTreeExpanded => {
debug!("pretty-printing expanded AST");
format!("{:#?}", ex.tcx().resolver_for_lowering(()).borrow().1)
format!("{:#?}", ex.tcx().resolver_for_lowering(()).0.borrow().1)
}
Hir(s) => {
debug!("pretty printing HIR {:?}", s);
Expand Down
21 changes: 16 additions & 5 deletions compiler/rustc_interface/src/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::interface::{Compiler, Result};
use crate::proc_macro_decls;
use crate::util;

use ast::expand::StrippedCfgItem;
use rustc_ast::{self as ast, visit};
use rustc_borrowck as mir_borrowck;
use rustc_codegen_ssa::traits::CodegenBackend;
Expand All @@ -18,6 +19,7 @@ use rustc_lint::{unerased_lint_store, BufferedEarlyLint, EarlyCheckNode, LintSto
use rustc_metadata::creader::CStore;
use rustc_middle::arena::Arena;
use rustc_middle::dep_graph::DepGraph;
use rustc_middle::query::LocalCrate;
use rustc_middle::ty::{self, GlobalCtxt, RegisteredTools, TyCtxt};
use rustc_middle::util::Providers;
use rustc_mir_build as mir_build;
Expand Down Expand Up @@ -280,7 +282,7 @@ fn configure_and_expand(

fn early_lint_checks(tcx: TyCtxt<'_>, (): ()) {
let sess = tcx.sess;
let (resolver, krate) = &*tcx.resolver_for_lowering(()).borrow();
let (resolver, krate) = &*tcx.resolver_for_lowering(()).0.borrow();
let mut lint_buffer = resolver.lint_buffer.steal();

if sess.opts.unstable_opts.input_stats {
Expand Down Expand Up @@ -534,7 +536,7 @@ fn write_out_deps(tcx: TyCtxt<'_>, outputs: &OutputFilenames, out_filenames: &[P
fn resolver_for_lowering<'tcx>(
tcx: TyCtxt<'tcx>,
(): (),
) -> &'tcx Steal<(ty::ResolverAstLowering, Lrc<ast::Crate>)> {
) -> (&'tcx Steal<(ty::ResolverAstLowering, Lrc<ast::Crate>)>, &'tcx ty::ResolverGlobalCtxt) {
let arenas = Resolver::arenas();
let _ = tcx.registered_tools(()); // Uses `crate_for_resolver`.
let (krate, pre_configured_attrs) = tcx.crate_for_resolver(()).steal();
Expand All @@ -549,9 +551,16 @@ fn resolver_for_lowering<'tcx>(
ast_lowering: untracked_resolver_for_lowering,
} = resolver.into_outputs();

let feed = tcx.feed_unit_query();
feed.resolutions(tcx.arena.alloc(untracked_resolutions));
tcx.arena.alloc(Steal::new((untracked_resolver_for_lowering, Lrc::new(krate))))
let resolutions = tcx.arena.alloc(untracked_resolutions);
(tcx.arena.alloc(Steal::new((untracked_resolver_for_lowering, Lrc::new(krate)))), resolutions)
}

fn stripped_cfg_items(tcx: TyCtxt<'_>, _: LocalCrate) -> &[StrippedCfgItem] {
tcx.arena.alloc_from_iter(tcx.resolutions(()).stripped_cfg_items.steal())
}

fn resolutions(tcx: TyCtxt<'_>, _: ()) -> &ty::ResolverGlobalCtxt {
tcx.resolver_for_lowering(()).1
}

pub(crate) fn write_dep_info(tcx: TyCtxt<'_>) {
Expand Down Expand Up @@ -608,6 +617,8 @@ pub static DEFAULT_QUERY_PROVIDERS: LazyLock<Providers> = LazyLock::new(|| {
providers.analysis = analysis;
providers.hir_crate = rustc_ast_lowering::lower_to_hir;
providers.resolver_for_lowering = resolver_for_lowering;
providers.stripped_cfg_items = stripped_cfg_items;
providers.resolutions = resolutions;
providers.early_lint_checks = early_lint_checks;
proc_macro_decls::provide(providers);
rustc_const_eval::provide(providers);
Expand Down
12 changes: 3 additions & 9 deletions compiler/rustc_interface/src/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ use rustc_codegen_ssa::CodegenResults;
use rustc_data_structures::steal::Steal;
use rustc_data_structures::svh::Svh;
use rustc_data_structures::sync::{AppendOnlyIndexVec, FreezeLock, OnceLock, WorkerLocal};
use rustc_hir::def::DefKind;
use rustc_hir::def_id::{StableCrateId, CRATE_DEF_ID, LOCAL_CRATE};
use rustc_hir::def_id::{StableCrateId, LOCAL_CRATE};
use rustc_hir::definitions::Definitions;
use rustc_incremental::setup_dep_graph;
use rustc_metadata::creader::CStore;
Expand Down Expand Up @@ -144,10 +143,8 @@ impl<'tcx> Queries<'tcx> {
stable_crate_id,
)) as _);
let definitions = FreezeLock::new(Definitions::new(stable_crate_id));
let source_span = AppendOnlyIndexVec::new();
let _id = source_span.push(krate.spans.inner_span);
debug_assert_eq!(_id, CRATE_DEF_ID);
let untracked = Untracked { cstore, source_span, definitions };
let untracked =
Untracked { cstore, source_span: AppendOnlyIndexVec::new(), definitions };

let qcx = passes::create_global_ctxt(
self.compiler,
Expand All @@ -172,9 +169,6 @@ impl<'tcx> Queries<'tcx> {
)));
feed.crate_for_resolver(tcx.arena.alloc(Steal::new((krate, pre_configured_attrs))));
feed.output_filenames(Arc::new(outputs));

let feed = tcx.feed_local_def_id(CRATE_DEF_ID);
feed.def_kind(DefKind::Mod);
});
Ok(qcx)
})
Expand Down
4 changes: 1 addition & 3 deletions compiler/rustc_middle/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,11 @@ rustc_queries! {
}

query resolutions(_: ()) -> &'tcx ty::ResolverGlobalCtxt {
feedable
no_hash
desc { "getting the resolver outputs" }
}

query resolver_for_lowering(_: ()) -> &'tcx Steal<(ty::ResolverAstLowering, Lrc<ast::Crate>)> {
query resolver_for_lowering(_: ()) -> (&'tcx Steal<(ty::ResolverAstLowering, Lrc<ast::Crate>)>, &'tcx ty::ResolverGlobalCtxt) {
eval_always
no_hash
desc { "getting the resolver for lowering" }
Expand Down Expand Up @@ -2211,7 +2210,6 @@ rustc_queries! {
/// Should not be called for the local crate before the resolver outputs are created, as it
/// is only fed there.
query stripped_cfg_items(cnum: CrateNum) -> &'tcx [StrippedCfgItem] {
feedable
desc { "getting cfg-ed out item names" }
separate_provide_extern
}
Expand Down
65 changes: 62 additions & 3 deletions compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ use rustc_session::config::CrateType;
use rustc_session::cstore::{CrateStoreDyn, Untracked};
use rustc_session::lint::Lint;
use rustc_session::{Limit, MetadataKind, Session};
use rustc_span::def_id::{DefPathHash, StableCrateId};
use rustc_span::def_id::{DefPathHash, StableCrateId, CRATE_DEF_ID};
use rustc_span::symbol::{kw, sym, Ident, Symbol};
use rustc_span::{Span, DUMMY_SP};
use rustc_target::abi::{FieldIdx, Layout, LayoutS, TargetDataLayout, VariantIdx};
Expand All @@ -76,6 +76,7 @@ use std::cmp::Ordering;
use std::fmt;
use std::hash::{Hash, Hasher};
use std::iter;
use std::marker::PhantomData;
use std::mem;
use std::ops::{Bound, Deref};

Expand Down Expand Up @@ -522,14 +523,55 @@ pub struct TyCtxtFeed<'tcx, KEY: Copy> {
key: KEY,
}

/// Never return a `Feed` from a query. Only queries that create a `DefId` are
/// allowed to feed queries for that `DefId`.
impl<KEY: Copy, CTX> !HashStable<CTX> for TyCtxtFeed<'_, KEY> {}

/// The same as `TyCtxtFeed`, but does not contain a `TyCtxt`.
/// Use this to pass around when you have a `TyCtxt` elsewhere.
/// Just an optimization to save space and not store hundreds of
/// `TyCtxtFeed` in the resolver.
#[derive(Copy, Clone)]
pub struct Feed<'tcx, KEY: Copy> {
_tcx: PhantomData<TyCtxt<'tcx>>,
// Do not allow direct access, as downstream code must not mutate this field.
key: KEY,
}

/// Never return a `Feed` from a query. Only queries that create a `DefId` are
/// allowed to feed queries for that `DefId`.
impl<KEY: Copy, CTX> !HashStable<CTX> for Feed<'_, KEY> {}

impl<T: fmt::Debug + Copy> fmt::Debug for Feed<'_, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.key.fmt(f)
}
}

/// Some workarounds to use cases that cannot use `create_def`.
/// Do not add new ways to create `TyCtxtFeed` without consulting
/// with T-compiler and making an analysis about why your addition
/// does not cause incremental compilation issues.
impl<'tcx> TyCtxt<'tcx> {
/// Can only be fed before queries are run, and is thus exempt from any
/// incremental issues. Do not use except for the initial query feeding.
pub fn feed_unit_query(self) -> TyCtxtFeed<'tcx, ()> {
self.dep_graph.assert_ignored();
TyCtxtFeed { tcx: self, key: () }
}

/// Can only be fed before queries are run, and is thus exempt from any
/// incremental issues. Do not use except for the initial query feeding.
pub fn feed_local_crate(self) -> TyCtxtFeed<'tcx, CrateNum> {
self.dep_graph.assert_ignored();
TyCtxtFeed { tcx: self, key: LOCAL_CRATE }
}
pub fn feed_local_def_id(self, key: LocalDefId) -> TyCtxtFeed<'tcx, LocalDefId> {

/// Only used in the resolver to register the `CRATE_DEF_ID` `DefId` and feed
/// some queries for it. It will panic if used twice.
pub fn create_local_crate_def_id(self, span: Span) -> TyCtxtFeed<'tcx, LocalDefId> {
let key = self.untracked().source_span.push(span);
assert_eq!(key, CRATE_DEF_ID);
TyCtxtFeed { tcx: self, key }
}

Expand All @@ -547,6 +589,23 @@ impl<'tcx, KEY: Copy> TyCtxtFeed<'tcx, KEY> {
pub fn key(&self) -> KEY {
self.key
}

#[inline(always)]
pub fn downgrade(self) -> Feed<'tcx, KEY> {
Feed { _tcx: PhantomData, key: self.key }
}
}

impl<'tcx, KEY: Copy> Feed<'tcx, KEY> {
#[inline(always)]
pub fn key(&self) -> KEY {
self.key
}

#[inline(always)]
pub fn upgrade(self, tcx: TyCtxt<'tcx>) -> TyCtxtFeed<'tcx, KEY> {
TyCtxtFeed { tcx, key: self.key }
}
}

impl<'tcx> TyCtxtFeed<'tcx, LocalDefId> {
Expand Down Expand Up @@ -1091,7 +1150,7 @@ impl<'tcx> TyCtxt<'tcx> {
// needs to be re-evaluated.
self.dep_graph.read_index(DepNodeIndex::FOREVER_RED_NODE);

let feed = self.feed_local_def_id(def_id);
let feed = TyCtxtFeed { tcx: self, key: def_id };
feed.def_kind(def_kind);
// Unique types created for closures participate in type privacy checking.
// They have visibilities inherited from the module they are defined in.
Expand Down
5 changes: 4 additions & 1 deletion compiler/rustc_middle/src/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use crate::ty::fast_reject::SimplifiedType;
use crate::ty::util::Discr;
pub use adt::*;
pub use assoc::*;
use ast::expand::StrippedCfgItem;
pub use generic_args::*;
pub use generics::*;
use rustc_ast as ast;
Expand Down Expand Up @@ -84,7 +85,8 @@ pub use self::consts::{
Const, ConstData, ConstInt, ConstKind, Expr, ScalarInt, UnevaluatedConst, ValTree,
};
pub use self::context::{
tls, CtxtInterners, DeducedParamAttrs, FreeRegionInfo, GlobalCtxt, Lift, TyCtxt, TyCtxtFeed,
tls, CtxtInterners, DeducedParamAttrs, Feed, FreeRegionInfo, GlobalCtxt, Lift, TyCtxt,
TyCtxtFeed,
};
pub use self::instance::{Instance, InstanceDef, ShortInstance, UnusedGenericParams};
pub use self::list::List;
Expand Down Expand Up @@ -187,6 +189,7 @@ pub struct ResolverGlobalCtxt {
pub doc_link_resolutions: FxHashMap<LocalDefId, DocLinkResMap>,
pub doc_link_traits_in_scope: FxHashMap<LocalDefId, Vec<DefId>>,
pub all_macro_rules: FxHashMap<Symbol, Res<ast::NodeId>>,
pub stripped_cfg_items: Steal<Vec<StrippedCfgItem>>,
}

/// Resolutions that should only be used for lowering.
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_passes/src/debugger_visualizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl<'ast> rustc_ast::visit::Visitor<'ast> for DebuggerVisualizerCollector<'_> {

/// Traverses and collects the debugger visualizers for a specific crate.
fn debugger_visualizers(tcx: TyCtxt<'_>, _: LocalCrate) -> Vec<DebuggerVisualizerFile> {
let resolver_and_krate = tcx.resolver_for_lowering(()).borrow();
let resolver_and_krate = tcx.resolver_for_lowering(()).0.borrow();
let krate = &*resolver_and_krate.1;

let mut visitor = DebuggerVisualizerCollector { sess: tcx.sess, visualizers: Vec::new() };
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_passes/src/lang_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ impl<'ast, 'tcx> LanguageItemCollector<'ast, 'tcx> {

/// Traverses and collects all the lang items in all crates.
fn get_lang_items(tcx: TyCtxt<'_>, (): ()) -> LanguageItems {
let resolver = tcx.resolver_for_lowering(()).borrow();
let resolver = tcx.resolver_for_lowering(()).0.borrow();
let (resolver, krate) = &*resolver;

// Initialize the collector.
Expand Down
Loading

0 comments on commit 9b35aff

Please sign in to comment.