-
-
Notifications
You must be signed in to change notification settings - Fork 15.2k
Resolver: Parallelize the import resolution loop #158845
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
LorrensP-2158466
wants to merge
2
commits into
rust-lang:main
Choose a base branch
from
LorrensP-2158466:parallel-import-resolution
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,8 @@ use rustc_ast::{ | |
| StmtKind, TraitAlias, TyAlias, | ||
| }; | ||
| use rustc_attr_parsing::AttributeParser; | ||
| use rustc_data_structures::fx::FxIndexMap; | ||
| use rustc_data_structures::sync::WriteGuard; | ||
| use rustc_expand::base::{ResolverExpand, SyntaxExtension, SyntaxExtensionKind}; | ||
| use rustc_hir::Attribute; | ||
| use rustc_hir::attrs::{AttributeKind, MacroUseArgs}; | ||
|
|
@@ -32,7 +34,7 @@ use tracing::debug; | |
| use crate::Namespace::{MacroNS, TypeNS, ValueNS}; | ||
| use crate::def_collector::DefCollector; | ||
| use crate::error_helper::{OnUnknownData, StructCtor}; | ||
| use crate::imports::{ImportData, ImportKind}; | ||
| use crate::imports::{ImportData, ImportKind, NameResolutionRef}; | ||
| use crate::macros::{MacroRulesDecl, MacroRulesScope, MacroRulesScopeRef}; | ||
| use crate::ref_mut::CmCell; | ||
| use crate::{ | ||
|
|
@@ -75,46 +77,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { | |
| self.plant_decl_into_local_module(ident, orig_ident.span, ns, decl); | ||
| } | ||
|
|
||
| /// Create a name definition from the given components, and put it into the extern module. | ||
| fn define_extern( | ||
| &self, | ||
| parent: ExternModule<'ra>, | ||
| ident: IdentKey, | ||
| orig_ident_span: Span, | ||
| ns: Namespace, | ||
| child_index: usize, | ||
| res: Res, | ||
| vis: Visibility<DefId>, | ||
| span: Span, | ||
| expansion: LocalExpnId, | ||
| ambiguity: Option<(Decl<'ra>, bool)>, | ||
| ) { | ||
| let decl = self.arenas.alloc_decl(DeclData { | ||
| kind: DeclKind::Def(res), | ||
| ambiguity: CmCell::new(ambiguity), | ||
| initial_vis: vis, | ||
| ambiguity_vis_max: CmCell::new(None), | ||
| ambiguity_vis_min: CmCell::new(None), | ||
| span, | ||
| expansion, | ||
| parent_module: Some(parent.to_module()), | ||
| }); | ||
| // Even if underscore names cannot be looked up, we still need to add them to modules, | ||
| // because they can be fetched by glob imports from those modules, and bring traits | ||
| // into scope both directly and through glob imports. | ||
| let key = | ||
| BindingKey::new_disambiguated(ident, ns, || (child_index + 1).try_into().unwrap()); // 0 indicates no underscore | ||
| if self | ||
| .resolution_or_default(parent.to_module(), key, orig_ident_span) | ||
| .borrow_mut_unchecked() | ||
| .non_glob_decl | ||
| .replace(decl) | ||
| .is_some() | ||
| { | ||
| span_bug!(span, "an external binding was already defined"); | ||
| } | ||
| } | ||
|
|
||
| /// Walks up the tree of definitions starting at `def_id`, | ||
| /// stopping at the first encountered module. | ||
| /// Parent block modules for arbitrary def-ids are not recorded for the local crate, | ||
|
|
@@ -151,38 +113,59 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { | |
| match def_id.as_local() { | ||
| Some(local_def_id) => self.local_module_map.get(&local_def_id).map(|m| m.to_module()), | ||
| None => { | ||
| if let module @ Some(..) = self.extern_module_map.borrow().get(&def_id) { | ||
| if let module @ Some(..) = self.extern_module_map.read().get(&def_id) { | ||
| return module.map(|m| m.to_module()); | ||
| } | ||
| // We need the lock on the extern_module_map for the entire duration of this call. | ||
| // It is otherwise entirely possible 2 different threads will create and allocate | ||
| // the exact same module during speculative resolution. | ||
| // FIXME(parallel_import_resolution): We lock the entire map to make sure | ||
| // no 2+ threads try to create the exact same module. Could it be possible to | ||
| // only "lock on" `def_id`? | ||
| let mut lock = self.extern_module_map.write(); | ||
| // No reentrant locking possible, so do a recurisve call with lock | ||
| // passed as argument. | ||
| self.get_extern_module_with_lock(def_id, &mut lock) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Query `def_kind` is not used because query system overhead is too expensive here. | ||
| let def_kind = self.cstore().def_kind_untracked(def_id); | ||
| if def_kind.is_module_like() { | ||
| let parent = self.tcx.opt_parent(def_id).map(|parent_id| { | ||
| self.get_nearest_non_block_module(parent_id).expect_extern() | ||
| }); | ||
| // Query `expn_that_defined` is not used because | ||
| // hashing spans in its result is expensive. | ||
| let expn_id = self.cstore().expn_that_defined_untracked(self.tcx, def_id); | ||
| let module = self.new_extern_module( | ||
| parent, | ||
| ModuleKind::Def( | ||
| def_kind, | ||
| def_id, | ||
| DUMMY_NODE_ID, | ||
| Some(self.tcx.item_name(def_id)), | ||
| ), | ||
| expn_id, | ||
| self.def_span(def_id), | ||
| // FIXME: Account for `#[no_implicit_prelude]` attributes. | ||
| parent.is_some_and(|module| module.no_implicit_prelude), | ||
| ); | ||
| return Some(module.to_module()); | ||
| fn get_extern_module_with_lock( | ||
| &self, | ||
| def_id: DefId, | ||
| map_lock: &mut WriteGuard<'_, FxIndexMap<DefId, ExternModule<'ra>>>, | ||
| ) -> Option<Module<'ra>> { | ||
| if let module @ Some(..) = map_lock.get(&def_id) { | ||
| return module.map(|m| m.to_module()); | ||
| } | ||
| // Query `def_kind` is not used because query system overhead is too expensive here. | ||
| let def_kind = self.cstore().def_kind_untracked(def_id); | ||
| if def_kind.is_module_like() { | ||
| let parent = self.tcx.opt_parent(def_id).map(|mut parent_id| { | ||
| loop { | ||
| match self.get_extern_module_with_lock(parent_id, map_lock) { | ||
| Some(module) => break module.expect_extern(), | ||
| None => parent_id = self.tcx.parent(parent_id), | ||
| } | ||
| } | ||
|
|
||
| None | ||
| } | ||
| }); | ||
| // Query `expn_that_defined` is not used because | ||
| // hashing spans in its result is expensive. | ||
| let expn_id = self.cstore().expn_that_defined_untracked(self.tcx, def_id); | ||
| let module = ExternModule::new( | ||
| parent, | ||
| ModuleKind::Def(def_kind, def_id, DUMMY_NODE_ID, Some(self.tcx.item_name(def_id))), | ||
| self.tcx.visibility(def_id), | ||
| expn_id, | ||
| self.def_span(def_id), | ||
| parent.is_some_and(|module| module.no_implicit_prelude), | ||
| self.arenas, | ||
| ); | ||
| map_lock.insert(def_id, module); | ||
| return Some(module.to_module()); | ||
| } | ||
|
|
||
| None | ||
| } | ||
|
|
||
| pub(crate) fn expn_def_scope(&self, expn_id: ExpnId) -> Module<'ra> { | ||
|
|
@@ -347,11 +330,22 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { | |
| } | ||
| } | ||
|
|
||
| pub(crate) fn build_reduced_graph_external(&self, module: ExternModule<'ra>) { | ||
| #[must_use = "This does not insert the resolutions in to the module itself"] | ||
| pub(crate) fn build_reduced_graph_external( | ||
| &self, | ||
| module: ExternModule<'ra>, | ||
| ) -> FxIndexMap<BindingKey, NameResolutionRef<'ra>> { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changes like this can be merged before the main parallelization PR. |
||
| let mut resolutions = FxIndexMap::default(); | ||
| let def_id = module.def_id(); | ||
| let children = self.tcx.module_children(def_id); | ||
| for (i, child) in children.iter().enumerate() { | ||
| self.build_reduced_graph_for_external_crate_res(child, module, i, None) | ||
| self.build_reduced_graph_for_external_crate_res( | ||
| child, | ||
| module, | ||
| i, | ||
| None, | ||
| &mut resolutions, | ||
| ) | ||
| } | ||
| for (i, child) in | ||
| self.cstore().ambig_module_children_untracked(self.tcx, def_id).enumerate() | ||
|
|
@@ -361,8 +355,10 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { | |
| module, | ||
| children.len() + i, | ||
| Some(&child.second), | ||
| &mut resolutions, | ||
| ) | ||
| } | ||
| resolutions | ||
| } | ||
|
|
||
| /// Builds the reduced graph for a single item in an external crate. | ||
|
|
@@ -372,6 +368,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { | |
| parent: ExternModule<'ra>, | ||
| child_index: usize, | ||
| ambig_child: Option<&ModChild>, | ||
| resolutions: &mut FxIndexMap<BindingKey, NameResolutionRef<'ra>>, | ||
| ) { | ||
| let child_span = |this: &Self, reexport_chain: &[Reexport], res: def::Res<_>| { | ||
| this.def_span( | ||
|
|
@@ -395,19 +392,30 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { | |
| }); | ||
|
|
||
| // Record primary definitions. | ||
| let define_extern = |ns| { | ||
| self.define_extern( | ||
| parent, | ||
| ident, | ||
| orig_ident.span, | ||
| ns, | ||
| child_index, | ||
| res, | ||
| vis, | ||
| let mut define_extern = |ns| { | ||
| let orig_ident_span = orig_ident.span; | ||
| let decl = self.arenas.alloc_decl(DeclData { | ||
| kind: DeclKind::Def(res), | ||
| ambiguity: CmCell::new(ambig), | ||
| initial_vis: vis, | ||
| ambiguity_vis_max: CmCell::new(None), | ||
| ambiguity_vis_min: CmCell::new(None), | ||
| span, | ||
| expansion, | ||
| ambig, | ||
| ) | ||
| parent_module: Some(parent.to_module()), | ||
| }); | ||
| let key = | ||
| BindingKey::new_disambiguated(ident, ns, || (child_index + 1).try_into().unwrap()); | ||
| if resolutions | ||
| .entry(key) | ||
| .or_insert_with(|| self.arenas.alloc_name_resolution(orig_ident_span)) | ||
| .borrow_mut_unchecked() // only 1 thread builds extern tables | ||
| .non_glob_decl | ||
| .replace(decl) | ||
| .is_some() | ||
| { | ||
| span_bug!(span, "an external binding was already defined"); | ||
| } | ||
| }; | ||
| match res { | ||
| Res::Def( | ||
|
|
@@ -736,7 +744,9 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> { | |
| if let PathResult::Module(ModuleOrUniformRoot::Module(module)) = path_res { | ||
| self.r.prelude = Some(module); | ||
| } else { | ||
| self.r.dcx().span_err(use_tree.span(), "cannot resolve a prelude import"); | ||
| self.r | ||
| .dcx() | ||
| .span_err(use_tree.span(), format!("cannot resolve a prelude import")); | ||
| } | ||
| } | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.