Skip to content

Commit f587784

Browse files
authored
Rollup merge of #143657 - LorrensP-2158466:split-macro-map, r=petrochenkov
Resolver: refact macro map into external and local maps Puts `MacroData` inside of the `ResolverArena` and splits `macro_map` into 2 maps: `local_macro_map` and `external_macro_map`. This way `get_macro_by_def_id` can take a `&Resolver` instead of a mutable one. Part of [#gsoc > Project: Parallel Macro Expansion](https://rust-lang.zulipchat.com/#narrow/channel/421156-gsoc/topic/Project.3A.20Parallel.20Macro.20Expansion/with/516965603) r? `@petrochenkov`
2 parents bdf1941 + e566783 commit f587784

File tree

5 files changed

+52
-32
lines changed

5 files changed

+52
-32
lines changed

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -162,28 +162,30 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
162162
}
163163
}
164164

165-
pub(crate) fn get_macro(&mut self, res: Res) -> Option<&MacroData> {
165+
pub(crate) fn get_macro(&self, res: Res) -> Option<&'ra MacroData> {
166166
match res {
167167
Res::Def(DefKind::Macro(..), def_id) => Some(self.get_macro_by_def_id(def_id)),
168-
Res::NonMacroAttr(_) => Some(&self.non_macro_attr),
168+
Res::NonMacroAttr(_) => Some(self.non_macro_attr),
169169
_ => None,
170170
}
171171
}
172172

173-
pub(crate) fn get_macro_by_def_id(&mut self, def_id: DefId) -> &MacroData {
174-
if self.macro_map.contains_key(&def_id) {
175-
return &self.macro_map[&def_id];
176-
}
177-
178-
let loaded_macro = self.cstore().load_macro_untracked(def_id, self.tcx);
179-
let macro_data = match loaded_macro {
180-
LoadedMacro::MacroDef { def, ident, attrs, span, edition } => {
181-
self.compile_macro(&def, ident, &attrs, span, ast::DUMMY_NODE_ID, edition)
182-
}
183-
LoadedMacro::ProcMacro(ext) => MacroData::new(Arc::new(ext)),
184-
};
173+
pub(crate) fn get_macro_by_def_id(&self, def_id: DefId) -> &'ra MacroData {
174+
// Local macros are always compiled.
175+
match def_id.as_local() {
176+
Some(local_def_id) => self.local_macro_map[&local_def_id],
177+
None => *self.extern_macro_map.borrow_mut().entry(def_id).or_insert_with(|| {
178+
let loaded_macro = self.cstore().load_macro_untracked(def_id, self.tcx);
179+
let macro_data = match loaded_macro {
180+
LoadedMacro::MacroDef { def, ident, attrs, span, edition } => {
181+
self.compile_macro(&def, ident, &attrs, span, ast::DUMMY_NODE_ID, edition)
182+
}
183+
LoadedMacro::ProcMacro(ext) => MacroData::new(Arc::new(ext)),
184+
};
185185

186-
self.macro_map.entry(def_id).or_insert(macro_data)
186+
self.arenas.alloc_macro(macro_data)
187+
}),
188+
}
187189
}
188190

189191
pub(crate) fn build_reduced_graph(
@@ -1203,7 +1205,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
12031205
fn insert_unused_macro(&mut self, ident: Ident, def_id: LocalDefId, node_id: NodeId) {
12041206
if !ident.as_str().starts_with('_') {
12051207
self.r.unused_macros.insert(def_id, (node_id, ident));
1206-
let nrules = self.r.macro_map[&def_id.to_def_id()].nrules;
1208+
let nrules = self.r.local_macro_map[&def_id].nrules;
12071209
self.r.unused_macro_rules.insert(node_id, DenseBitSet::new_filled(nrules));
12081210
}
12091211
}
@@ -1222,7 +1224,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
12221224
Some((macro_kind, ident, span)) => {
12231225
let res = Res::Def(DefKind::Macro(macro_kind), def_id.to_def_id());
12241226
let macro_data = MacroData::new(self.r.dummy_ext(macro_kind));
1225-
self.r.macro_map.insert(def_id.to_def_id(), macro_data);
1227+
self.r.new_local_macro(def_id, macro_data);
12261228
self.r.proc_macro_stubs.insert(def_id);
12271229
(res, ident, span, false)
12281230
}

compiler/rustc_resolve/src/def_collector.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ impl<'a, 'ra, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'ra, 'tcx> {
165165
self.create_def(i.id, i.kind.ident().map(|ident| ident.name), def_kind, i.span);
166166

167167
if let Some(macro_data) = opt_macro_data {
168-
self.resolver.macro_map.insert(def_id.to_def_id(), macro_data);
168+
self.resolver.new_local_macro(def_id, macro_data);
169169
}
170170

171171
self.with_parent(def_id, |this| {

compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1669,9 +1669,14 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
16691669
let mut all_attrs: UnordMap<Symbol, Vec<_>> = UnordMap::default();
16701670
// We're collecting these in a hashmap, and handle ordering the output further down.
16711671
#[allow(rustc::potential_query_instability)]
1672-
for (def_id, data) in &self.macro_map {
1672+
for (def_id, data) in self
1673+
.local_macro_map
1674+
.iter()
1675+
.map(|(local_id, data)| (local_id.to_def_id(), data))
1676+
.chain(self.extern_macro_map.borrow().iter().map(|(id, d)| (*id, d)))
1677+
{
16731678
for helper_attr in &data.ext.helper_attrs {
1674-
let item_name = self.tcx.item_name(*def_id);
1679+
let item_name = self.tcx.item_name(def_id);
16751680
all_attrs.entry(*helper_attr).or_default().push(item_name);
16761681
if helper_attr == &ident.name {
16771682
derives.push(item_name);

compiler/rustc_resolve/src/lib.rs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,10 +1128,12 @@ pub struct Resolver<'ra, 'tcx> {
11281128
builtin_macros: FxHashMap<Symbol, SyntaxExtensionKind>,
11291129
registered_tools: &'tcx RegisteredTools,
11301130
macro_use_prelude: FxIndexMap<Symbol, NameBinding<'ra>>,
1131-
macro_map: FxHashMap<DefId, MacroData>,
1131+
local_macro_map: FxHashMap<LocalDefId, &'ra MacroData>,
1132+
/// Lazily populated cache of macros loaded from external crates.
1133+
extern_macro_map: RefCell<FxHashMap<DefId, &'ra MacroData>>,
11321134
dummy_ext_bang: Arc<SyntaxExtension>,
11331135
dummy_ext_derive: Arc<SyntaxExtension>,
1134-
non_macro_attr: MacroData,
1136+
non_macro_attr: &'ra MacroData,
11351137
local_macro_def_scopes: FxHashMap<LocalDefId, Module<'ra>>,
11361138
ast_transform_scopes: FxHashMap<LocalExpnId, Module<'ra>>,
11371139
unused_macros: FxIndexMap<LocalDefId, (NodeId, Ident)>,
@@ -1241,6 +1243,7 @@ pub struct ResolverArenas<'ra> {
12411243
imports: TypedArena<ImportData<'ra>>,
12421244
name_resolutions: TypedArena<RefCell<NameResolution<'ra>>>,
12431245
ast_paths: TypedArena<ast::Path>,
1246+
macros: TypedArena<MacroData>,
12441247
dropless: DroplessArena,
12451248
}
12461249

@@ -1287,7 +1290,7 @@ impl<'ra> ResolverArenas<'ra> {
12871290
self.name_resolutions.alloc(Default::default())
12881291
}
12891292
fn alloc_macro_rules_scope(&'ra self, scope: MacroRulesScope<'ra>) -> MacroRulesScopeRef<'ra> {
1290-
Interned::new_unchecked(self.dropless.alloc(Cell::new(scope)))
1293+
self.dropless.alloc(Cell::new(scope))
12911294
}
12921295
fn alloc_macro_rules_binding(
12931296
&'ra self,
@@ -1298,6 +1301,9 @@ impl<'ra> ResolverArenas<'ra> {
12981301
fn alloc_ast_paths(&'ra self, paths: &[ast::Path]) -> &'ra [ast::Path] {
12991302
self.ast_paths.alloc_from_iter(paths.iter().cloned())
13001303
}
1304+
fn alloc_macro(&'ra self, macro_data: MacroData) -> &'ra MacroData {
1305+
self.macros.alloc(macro_data)
1306+
}
13011307
fn alloc_pattern_spans(&'ra self, spans: impl Iterator<Item = Span>) -> &'ra [Span] {
13021308
self.dropless.alloc_from_iter(spans)
13031309
}
@@ -1540,10 +1546,12 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
15401546
builtin_macros: Default::default(),
15411547
registered_tools,
15421548
macro_use_prelude: Default::default(),
1543-
macro_map: FxHashMap::default(),
1549+
local_macro_map: Default::default(),
1550+
extern_macro_map: Default::default(),
15441551
dummy_ext_bang: Arc::new(SyntaxExtension::dummy_bang(edition)),
15451552
dummy_ext_derive: Arc::new(SyntaxExtension::dummy_derive(edition)),
1546-
non_macro_attr: MacroData::new(Arc::new(SyntaxExtension::non_macro_attr(edition))),
1553+
non_macro_attr: arenas
1554+
.alloc_macro(MacroData::new(Arc::new(SyntaxExtension::non_macro_attr(edition)))),
15471555
invocation_parent_scopes: Default::default(),
15481556
output_macro_rules_scopes: Default::default(),
15491557
macro_rules_scopes: Default::default(),
@@ -1616,6 +1624,12 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
16161624
)
16171625
}
16181626

1627+
fn new_local_macro(&mut self, def_id: LocalDefId, macro_data: MacroData) -> &'ra MacroData {
1628+
let mac = self.arenas.alloc_macro(macro_data);
1629+
self.local_macro_map.insert(def_id, mac);
1630+
mac
1631+
}
1632+
16191633
fn next_node_id(&mut self) -> NodeId {
16201634
let start = self.next_node_id;
16211635
let next = start.as_u32().checked_add(1).expect("input too large; ran out of NodeIds");
@@ -1734,7 +1748,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
17341748
f(self, MacroNS);
17351749
}
17361750

1737-
fn is_builtin_macro(&mut self, res: Res) -> bool {
1751+
fn is_builtin_macro(&self, res: Res) -> bool {
17381752
self.get_macro(res).is_some_and(|macro_data| macro_data.ext.builtin_name.is_some())
17391753
}
17401754

compiler/rustc_resolve/src/macros.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use rustc_ast::expand::StrippedCfgItem;
99
use rustc_ast::{self as ast, Crate, NodeId, attr};
1010
use rustc_ast_pretty::pprust;
1111
use rustc_attr_data_structures::StabilityLevel;
12-
use rustc_data_structures::intern::Interned;
1312
use rustc_errors::{Applicability, DiagCtxtHandle, StashKey};
1413
use rustc_expand::base::{
1514
Annotatable, DeriveResolution, Indeterminate, ResolverExpand, SyntaxExtension,
@@ -80,7 +79,7 @@ pub(crate) enum MacroRulesScope<'ra> {
8079
/// This helps to avoid uncontrollable growth of `macro_rules!` scope chains,
8180
/// which usually grow linearly with the number of macro invocations
8281
/// in a module (including derives) and hurt performance.
83-
pub(crate) type MacroRulesScopeRef<'ra> = Interned<'ra, Cell<MacroRulesScope<'ra>>>;
82+
pub(crate) type MacroRulesScopeRef<'ra> = &'ra Cell<MacroRulesScope<'ra>>;
8483

8584
/// Macro namespace is separated into two sub-namespaces, one for bang macros and
8685
/// one for attribute-like macros (attributes, derives).
@@ -354,8 +353,8 @@ impl<'ra, 'tcx> ResolverExpand for Resolver<'ra, 'tcx> {
354353
if unused_arms.is_empty() {
355354
continue;
356355
}
357-
let def_id = self.local_def_id(node_id).to_def_id();
358-
let m = &self.macro_map[&def_id];
356+
let def_id = self.local_def_id(node_id);
357+
let m = &self.local_macro_map[&def_id];
359358
let SyntaxExtensionKind::LegacyBang(ref ext) = m.ext.kind else {
360359
continue;
361360
};
@@ -1132,7 +1131,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
11321131
}
11331132
}
11341133

1135-
pub(crate) fn check_reserved_macro_name(&mut self, ident: Ident, res: Res) {
1134+
pub(crate) fn check_reserved_macro_name(&self, ident: Ident, res: Res) {
11361135
// Reserve some names that are not quite covered by the general check
11371136
// performed on `Resolver::builtin_attrs`.
11381137
if ident.name == sym::cfg || ident.name == sym::cfg_attr {
@@ -1148,7 +1147,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
11481147
///
11491148
/// Possibly replace its expander to a pre-defined one for built-in macros.
11501149
pub(crate) fn compile_macro(
1151-
&mut self,
1150+
&self,
11521151
macro_def: &ast::MacroDef,
11531152
ident: Ident,
11541153
attrs: &[rustc_hir::Attribute],

0 commit comments

Comments
 (0)