-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Allocate HIR on an arena 1/4 #66931
Allocate HIR on an arena 1/4 #66931
Conversation
cc @rust-lang/compiler |
r? @eddyb |
src/librustc/arena.rs
Outdated
@@ -123,6 +122,10 @@ macro_rules! arena_types { | |||
[few] crate_variances: rustc::ty::CrateVariancesMap<'tcx>, | |||
[few] inferred_outlives_crate: rustc::ty::CratePredicatesMap<'tcx>, | |||
[] upvars: rustc_data_structures::fx::FxIndexMap<rustc::hir::HirId, rustc::hir::Upvar>, | |||
// HIR nodes arenas |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe call this HIR types
and put a newline above it to visually separate it from the rest.
visitor.visit_mod(&krate.module, krate.span, CRATE_HIR_ID); | ||
walk_list!(visitor, visit_attribute, &krate.attrs); | ||
walk_list!(visitor, visit_macro_def, &krate.exported_macros); | ||
walk_list!(visitor, visit_attribute, krate.attrs); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm curious, why didn't &krate.attrs
still work?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed the HirVec<T>
to be &'hir [T]
. Using &krate.attrs
used to make a &[T]
, and now makes a &&[T]
.
src/librustc/hir/lowering.rs
Outdated
/// Full-crate AST visitor that inserts into a fresh | ||
/// `LoweringContext` any information that may be | ||
/// needed from arbitrary locations in the crate, | ||
/// e.g., the number of lifetime generic parameters | ||
/// declared for every type and trait definition. | ||
struct MiscCollector<'tcx, 'interner> { | ||
lctx: &'tcx mut LoweringContext<'interner>, | ||
struct MiscCollector<'tcx, 'interner, 'hir> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The names of these lifetimes are quite misleading. I think they should be named MiscCollector<'misc, 'lowering, 'hir>
instead.
src/librustc/hir/lowering/item.rs
Outdated
@@ -25,11 +25,11 @@ use syntax_pos::Span; | |||
|
|||
use rustc_error_codes::*; | |||
|
|||
pub(super) struct ItemLowerer<'tcx, 'interner> { | |||
pub(super) lctx: &'tcx mut LoweringContext<'interner>, | |||
pub(super) struct ItemLowerer<'tcx, 'interner, 'hir> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could be named ItemLowerer<'a, 'lowering, 'hir>
.
src/librustc/hir/print.rs
Outdated
@@ -107,7 +107,7 @@ pub const INDENT_UNIT: usize = 4; | |||
/// it can scan the input text for comments to copy forward. | |||
pub fn print_crate<'a>(cm: &'a SourceMap, | |||
sess: &ParseSess, | |||
krate: &hir::Crate, | |||
krate: &hir::Crate<'a>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could probably be &hir::Crate<'_>
?
ImplTraitContext::Disallowed(ImplTraitPosition::Binding) | ||
} | ||
), | ||
self.arena.alloc(ty.into_inner()), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This feels noisy and repetitive. Could we bake self.arena.alloc(ty.into_inner())
into lower_ty
or if not, for whatever reason, add a lower_ty_arena
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd expect that to happen on the 3rd PR which moves Ty
to the arena. I guess @cjgillot moved the fields of each type to the arena too so these show up as temporary changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There doesn't seem to be that many fields that's moved to &'hir Ty
though. I'd just keep P<Ty>
for this PR to avoid unnecessary changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed, a lot of those disappear in #66942. I plan to do the final cleanup in PR 4/4.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After discussing with @pnkfelix, I remembered another reason to migrate eagerly the P<Ty>
into &'hir Ty
. If I do not do that, I get unused lifetime parameter errors in intermediate commits (ex: TraitItemKind
). This keeps me from splitting in compiling commits.
src/librustc/arena.rs
Outdated
@@ -125,7 +125,12 @@ macro_rules! arena_types { | |||
// HIR nodes arenas | |||
[few] hir_forest: rustc::hir::map::Forest<$tcx>, | |||
[] attribute: syntax::ast::Attribute, | |||
[] global_asm: rustc::hir::GlobalAsm, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could have a few
attribute since they're likely to be rare.
src/librustc/arena.rs
Outdated
// HIR nodes arenas | ||
[few] hir_forest: rustc::hir::map::Forest<$tcx>, | ||
[] attribute: syntax::ast::Attribute, | ||
[] macro_def: rustc::hir::MacroDef, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should probably have a few
attribute too.
src/librustc/hir/lowering/item.rs
Outdated
let def_id = self.resolver.definitions().local_def_id(i.id); | ||
hir::ForeignItem { | ||
hir_id: self.lower_node_id(i.id), | ||
ident: i.ident, | ||
attrs: self.lower_attrs(&i.attrs), | ||
attrs: self.lower_attrs_extendable(&i.attrs), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems a bit odd. Shouldn't lower_attrs_extendable
still return a Vec
?
src/librustc/hir/lowering.rs
Outdated
.iter() | ||
.map(|a| self.lower_attr(a)) | ||
.collect() | ||
fn lower_attrs_extendable(&mut self, attrs: &[Attribute]) -> &'hir [Attribute] { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should still return Vec<Attribute>
since callers may want to extend it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lower_attrs
is the function which should return &'hir [Attribute]
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest you add a lower_attrs_arena
variant which returns &'hir [Attribute]
which can be removed when all the attributes are migrated over.
src/librustc/hir/lowering/item.rs
Outdated
@@ -1042,7 +1042,7 @@ impl LoweringContext<'_, 'hir> { | |||
|
|||
fn lower_body( | |||
&mut self, | |||
f: impl FnOnce(&mut LoweringContext<'_, '_>) -> (HirVec<hir::Param>, hir::Expr), | |||
f: impl for<'tcx> FnOnce(&mut LoweringContext<'_, 'tcx>) -> (&'tcx [hir::Param], hir::Expr), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't want to use for<'tcx>
here.
☔ The latest upstream changes (presumably #66944) made this pull request unmergeable. Please resolve the merge conflicts. |
e2ad73e
to
8985208
Compare
Rebased with review comments. |
bf0c6fb
to
15b76ce
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
r=me if no concerns remain from @rust-lang/compiler (also, feel free to ping/PM me on Discord or Zulip in the future, when each PR is ready)
☔ The latest upstream changes (presumably #66984) made this pull request unmergeable. Please resolve the merge conflicts. |
15b76ce
to
0c4b8ee
Compare
My take is that we should land this. Maybe we can nominate for a quick 👍 at triage meeting? Seems like it would be good to get some kind of perf numbers, too. Let's kick that off. @bors try |
⌛ Trying commit 0c4b8eee9c9e63f818861bec48e6fd356daf4166 with merge 963a1868bdc29bd60b42fc3c9705b6b186b61927... |
f5ce0e7
to
baa49b2
Compare
Rebased. |
@bors retry |
@bors r=eddyb |
📌 Commit baa49b2 has been approved by |
🌲 The tree is currently closed for pull requests below priority 100, this pull request will be tested once the tree is reopened |
@bors p=100 |
Allocate HIR on an arena 1/4 This PR is the first in a series of 4, aiming at allocating the HIR on an arena, as a memory optimisation. 1. This first PR lays the groundwork and migrates some low-hanging fruits. 2. The second PR will migrate `hir::Expr`, `hir::Pat` and related. 3. The third PR will migrate `hir::Ty` and related. 4. The final PR will be dedicated to eventual cleanups. In order to make the transition as gradual as possible, some lowering routines receive `Box`-allocated data and move it into the arena. This is a bit wasteful, but hopefully temporary. Nonetheless, special care should be taken to avoid double arena allocations. Work mentored by @Zoxc.
☀️ Test successful - checks-azure |
Rustup to rust-lang/rust#66931 changelog: none
Allocate HIR on an arena 2/4 -- Expr & Pat This is the second PR in the series started by #66931 This time, commits don't really make sense on their own. They are mostly split by type of compile error. The additional diff is here: cjgillot/rust@hirene-preamble...hirene-expr
Allocate HIR on an arena 3/4 -- Ty This is the third PR in the series started by #66931 and #66936 Once again, commits don't really make sense on their own. They are mostly split by type of compile error. The additional diff is here: cjgillot/rust@hirene-expr...hirene-ty
Allocate HIR on an arena 4/4 This is the fourth and last PR in the series started by #66931, #66936 and #66942. The last commits should compile on their own. The difference with the previous PR is given by cjgillot/rust@hirene-ty...hirene A few more cleanups may be necessary, please tell me. r? @eddyb like the other cc @Zoxc
This PR is the first in a series of 4, aiming at allocating the HIR on an arena, as a memory optimisation.
hir::Expr
,hir::Pat
and related.hir::Ty
and related.In order to make the transition as gradual as possible, some lowering routines receive
Box
-allocated data and move it into the arena. This is a bit wasteful, but hopefully temporary.Nonetheless, special care should be taken to avoid double arena allocations.
Work mentored by @Zoxc.