Skip to content

Commit

Permalink
Consume resolutions for lowering separately.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjgillot committed Jun 15, 2022
1 parent 8ffbd81 commit ae5959f
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 36 deletions.
9 changes: 4 additions & 5 deletions compiler/rustc_ast_lowering/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use rustc_hir::def_id::{LocalDefId, CRATE_DEF_ID};
use rustc_hir::definitions::Definitions;
use rustc_hir::PredicateOrigin;
use rustc_index::vec::{Idx, IndexVec};
use rustc_middle::ty::ResolverOutputs;
use rustc_middle::ty::{ResolverAstLowering, ResolverOutputs};
use rustc_session::cstore::CrateStoreDyn;
use rustc_session::Session;
use rustc_span::source_map::DesugaringKind;
Expand All @@ -30,7 +30,8 @@ pub(super) struct ItemLowerer<'a, 'hir> {
pub(super) sess: &'a Session,
pub(super) definitions: &'a mut Definitions,
pub(super) cstore: &'a CrateStoreDyn,
pub(super) resolver: &'a mut ResolverOutputs,
pub(super) resolutions: &'a ResolverOutputs,
pub(super) resolver: &'a mut ResolverAstLowering,
pub(super) arena: &'hir Arena<'hir>,
pub(super) ast_index: &'a IndexVec<LocalDefId, AstOwner<'a>>,
pub(super) owners: &'a mut IndexVec<LocalDefId, hir::MaybeOwner<&'hir hir::OwnerInfo<'hir>>>,
Expand Down Expand Up @@ -62,12 +63,12 @@ impl<'a, 'hir> ItemLowerer<'a, 'hir> {
owner: NodeId,
f: impl FnOnce(&mut LoweringContext<'_, 'hir>) -> hir::OwnerNode<'hir>,
) {
let next_node_id = self.resolver.next_node_id;
let mut lctx = LoweringContext {
// Pseudo-globals.
sess: &self.sess,
definitions: self.definitions,
cstore: self.cstore,
resolutions: self.resolutions,
resolver: self.resolver,
arena: self.arena,

Expand All @@ -80,8 +81,6 @@ impl<'a, 'hir> ItemLowerer<'a, 'hir> {
node_id_to_local_id: Default::default(),
local_id_to_def_id: SortedMap::new(),
trait_map: Default::default(),
local_node_id_to_def_id: FxHashMap::default(),
next_node_id,

// Lowering state.
catch_scope: None,
Expand Down
33 changes: 14 additions & 19 deletions compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ use rustc_hir::def_id::{LocalDefId, CRATE_DEF_ID};
use rustc_hir::definitions::{DefPathData, Definitions};
use rustc_hir::{ConstArg, GenericArg, ItemLocalId, ParamName, TraitCandidate};
use rustc_index::vec::{Idx, IndexVec};
use rustc_middle::ty::ResolverOutputs;
use rustc_middle::ty::{ResolverAstLowering, ResolverOutputs};
use rustc_query_system::ich::StableHashingContext;
use rustc_session::cstore::CrateStoreDyn;
use rustc_session::parse::feature_err;
Expand Down Expand Up @@ -91,7 +91,8 @@ struct LoweringContext<'a, 'hir: 'a> {

definitions: &'a mut Definitions,
cstore: &'a CrateStoreDyn,
resolver: &'a mut ResolverOutputs,
resolutions: &'a ResolverOutputs,
resolver: &'a mut ResolverAstLowering,

/// Used to allocate HIR nodes.
arena: &'hir Arena<'hir>,
Expand Down Expand Up @@ -133,10 +134,6 @@ struct LoweringContext<'a, 'hir: 'a> {
/// NodeIds that are lowered inside the current HIR owner.
node_id_to_local_id: FxHashMap<NodeId, hir::ItemLocalId>,

// The next_node_id is reset for each item.
next_node_id: ast::NodeId,
local_node_id_to_def_id: FxHashMap<ast::NodeId, LocalDefId>,

allow_try_trait: Option<Lrc<[Symbol]>>,
allow_gen_future: Option<Lrc<[Symbol]>>,
allow_into_future: Option<Lrc<[Symbol]>>,
Expand Down Expand Up @@ -174,7 +171,7 @@ trait ResolverAstLoweringExt {
fn decl_macro_kind(&self, def_id: LocalDefId) -> MacroKind;
}

impl ResolverAstLoweringExt for ResolverOutputs {
impl ResolverAstLoweringExt for ResolverAstLowering {
fn legacy_const_generic_args(&self, expr: &Expr) -> Option<Vec<usize>> {
if let ExprKind::Path(None, path) = &expr.kind {
// Don't perform legacy const generics rewriting if the path already
Expand Down Expand Up @@ -415,7 +412,8 @@ pub fn lower_crate<'hir>(
krate: &Crate,
definitions: &mut Definitions,
cstore: &CrateStoreDyn,
resolver: &mut ResolverOutputs,
resolutions: &ResolverOutputs,
mut resolver: ResolverAstLowering,
arena: &'hir Arena<'hir>,
) -> &'hir hir::Crate<'hir> {
let _prof_timer = sess.prof.verbose_generic_activity("hir_lowering");
Expand All @@ -430,15 +428,16 @@ pub fn lower_crate<'hir>(
sess,
definitions,
cstore,
resolver,
resolutions,
resolver: &mut resolver,
arena,
ast_index: &ast_index,
owners: &mut owners,
}
.lower_node(def_id);
}

let hir_hash = compute_hir_hash(sess, definitions, cstore, resolver, &owners);
let hir_hash = compute_hir_hash(sess, definitions, cstore, resolutions, &owners);
let krate = hir::Crate { owners, hir_hash };
arena.alloc(krate)
}
Expand All @@ -464,7 +463,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
self.sess,
self.definitions,
self.cstore,
&self.resolver.source_span,
&self.resolutions.source_span,
)
}

Expand All @@ -489,25 +488,21 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
// we don't need a mapping from `NodeId` to `LocalDefId`.
if node_id != ast::DUMMY_NODE_ID {
debug!("create_def: def_id_to_node_id[{:?}] <-> {:?}", def_id, node_id);
self.local_node_id_to_def_id.insert(node_id, def_id);
self.resolver.node_id_to_def_id.insert(node_id, def_id);
}

def_id
}

fn next_node_id(&mut self) -> NodeId {
let start = self.next_node_id;
let start = self.resolver.next_node_id;
let next = start.as_u32().checked_add(1).expect("input too large; ran out of NodeIds");
self.next_node_id = ast::NodeId::from_u32(next);
self.resolver.next_node_id = ast::NodeId::from_u32(next);
start
}

fn opt_local_def_id(&self, node: NodeId) -> Option<LocalDefId> {
if node <= self.resolver.next_node_id {
self.resolver.node_id_to_def_id.get(&node).copied()
} else {
self.local_node_id_to_def_id.get(&node).copied()
}
self.resolver.node_id_to_def_id.get(&node).copied()
}

fn local_def_id(&self, node: NodeId) -> LocalDefId {
Expand Down
32 changes: 24 additions & 8 deletions compiler/rustc_interface/src/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use rustc_metadata::{encode_metadata, EncodedMetadata};
use rustc_middle::arena::Arena;
use rustc_middle::dep_graph::DepGraph;
use rustc_middle::ty::query::{ExternProviders, Providers};
use rustc_middle::ty::{self, GlobalCtxt, RegisteredTools, ResolverOutputs, TyCtxt};
use rustc_middle::ty::{self, GlobalCtxt, RegisteredTools, TyCtxt};
use rustc_mir_build as mir_build;
use rustc_parse::{parse_crate_from_file, parse_crate_from_source_str, validate_attr};
use rustc_passes::{self, hir_stats, layout_test};
Expand Down Expand Up @@ -139,7 +139,8 @@ mod boxed_resolver {

pub fn to_resolver_outputs(
resolver: Rc<RefCell<BoxedResolver>>,
) -> (Definitions, Box<CrateStoreDyn>, ResolverOutputs) {
) -> (Definitions, Box<CrateStoreDyn>, ty::ResolverOutputs, ty::ResolverAstLowering)
{
match Rc::try_unwrap(resolver) {
Ok(resolver) => {
let mut resolver = resolver.into_inner();
Expand Down Expand Up @@ -485,13 +486,21 @@ fn lower_to_hir<'tcx>(
sess: &Session,
definitions: &mut Definitions,
cstore: &CrateStoreDyn,
resolver: &mut ResolverOutputs,
resolutions: &ty::ResolverOutputs,
resolver: ty::ResolverAstLowering,
krate: Rc<ast::Crate>,
arena: &'tcx rustc_ast_lowering::Arena<'tcx>,
) -> &'tcx Crate<'tcx> {
// Lower AST to HIR.
let hir_crate =
rustc_ast_lowering::lower_crate(sess, &krate, definitions, cstore, resolver, arena);
let hir_crate = rustc_ast_lowering::lower_crate(
sess,
&krate,
definitions,
cstore,
resolutions,
resolver,
arena,
);

// Drop AST to free memory
sess.time("drop_ast", || std::mem::drop(krate));
Expand Down Expand Up @@ -829,14 +838,21 @@ pub fn create_global_ctxt<'tcx>(
// incr. comp. yet.
dep_graph.assert_ignored();

let (mut definitions, cstore, mut resolver_outputs) =
let (mut definitions, cstore, resolver_outputs, resolver_for_lowering) =
BoxedResolver::to_resolver_outputs(resolver);

let sess = &compiler.session();

// Lower AST to HIR.
let krate =
lower_to_hir(sess, &mut definitions, &*cstore, &mut resolver_outputs, krate, hir_arena);
let krate = lower_to_hir(
sess,
&mut definitions,
&*cstore,
&resolver_outputs,
resolver_for_lowering,
krate,
hir_arena,
);

let query_result_on_disk_cache = rustc_incremental::load_query_result_cache(sess);

Expand Down
5 changes: 5 additions & 0 deletions compiler/rustc_middle/src/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,12 @@ pub struct ResolverOutputs {
/// exist under `std`. For example, wrote `str::from_utf8` instead of `std::str::from_utf8`.
pub confused_type_with_std_module: FxHashMap<Span, Span>,
pub registered_tools: RegisteredTools,
}

/// Resolutions that should only be used for lowering.
/// This struct is meant to be consumed by lowering.
#[derive(Debug)]
pub struct ResolverAstLowering {
pub legacy_const_generic_args: FxHashMap<DefId, Option<Vec<usize>>>,

/// Resolutions for nodes that have a single resolution.
Expand Down
16 changes: 12 additions & 4 deletions compiler/rustc_resolve/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1392,7 +1392,9 @@ impl<'a> Resolver<'a> {
Default::default()
}

pub fn into_outputs(self) -> (Definitions, Box<CrateStoreDyn>, ResolverOutputs) {
pub fn into_outputs(
self,
) -> (Definitions, Box<CrateStoreDyn>, ResolverOutputs, ty::ResolverAstLowering) {
let proc_macros = self.proc_macros.iter().map(|id| self.local_def_id(*id)).collect();
let definitions = self.definitions;
let cstore = Box::new(self.crate_loader.into_cstore());
Expand Down Expand Up @@ -1429,6 +1431,8 @@ impl<'a> Resolver<'a> {
proc_macros,
confused_type_with_std_module,
registered_tools: self.registered_tools,
};
let resolutions_lowering = ty::ResolverAstLowering {
legacy_const_generic_args: self.legacy_const_generic_args,
partial_res_map: self.partial_res_map,
import_res_map: self.import_res_map,
Expand All @@ -1441,10 +1445,12 @@ impl<'a> Resolver<'a> {
trait_map: self.trait_map,
builtin_macro_kinds: self.builtin_macro_kinds,
};
(definitions, cstore, resolutions)
(definitions, cstore, resolutions, resolutions_lowering)
}

pub fn clone_outputs(&self) -> (Definitions, Box<CrateStoreDyn>, ResolverOutputs) {
pub fn clone_outputs(
&self,
) -> (Definitions, Box<CrateStoreDyn>, ResolverOutputs, ty::ResolverAstLowering) {
let proc_macros = self.proc_macros.iter().map(|id| self.local_def_id(*id)).collect();
let definitions = self.definitions.clone();
let cstore = Box::new(self.cstore().clone());
Expand All @@ -1469,6 +1475,8 @@ impl<'a> Resolver<'a> {
confused_type_with_std_module: self.confused_type_with_std_module.clone(),
registered_tools: self.registered_tools.clone(),
access_levels: self.access_levels.clone(),
};
let resolutions_lowering = ty::ResolverAstLowering {
legacy_const_generic_args: self.legacy_const_generic_args.clone(),
partial_res_map: self.partial_res_map.clone(),
import_res_map: self.import_res_map.clone(),
Expand All @@ -1481,7 +1489,7 @@ impl<'a> Resolver<'a> {
trait_map: self.trait_map.clone(),
builtin_macro_kinds: self.builtin_macro_kinds.clone(),
};
(definitions, cstore, resolutions)
(definitions, cstore, resolutions, resolutions_lowering)
}

fn create_stable_hashing_context(&self) -> StableHashingContext<'_> {
Expand Down

0 comments on commit ae5959f

Please sign in to comment.