Skip to content
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

Merged
merged 12 commits into from
Dec 22, 2019
Merged

Allocate HIR on an arena 1/4 #66931

merged 12 commits into from
Dec 22, 2019

Conversation

cjgillot
Copy link
Contributor

@cjgillot cjgillot commented Dec 1, 2019

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.

@matthewjasper
Copy link
Contributor

cc @rust-lang/compiler

@Zoxc
Copy link
Contributor

Zoxc commented Dec 1, 2019

r? @eddyb

@rust-highfive rust-highfive assigned eddyb and unassigned matthewjasper Dec 1, 2019
@@ -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
Copy link
Contributor

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);
Copy link
Contributor

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?

Copy link
Contributor Author

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].

/// 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> {
Copy link
Contributor

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.

@@ -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> {
Copy link
Contributor

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>.

@@ -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>,
Copy link
Contributor

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()),
Copy link
Contributor

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?

Copy link
Contributor

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.

Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

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.

@@ -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,
Copy link
Contributor

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.

// HIR nodes arenas
[few] hir_forest: rustc::hir::map::Forest<$tcx>,
[] attribute: syntax::ast::Attribute,
[] macro_def: rustc::hir::MacroDef,
Copy link
Contributor

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.

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),
Copy link
Contributor

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?

.iter()
.map(|a| self.lower_attr(a))
.collect()
fn lower_attrs_extendable(&mut self, attrs: &[Attribute]) -> &'hir [Attribute] {
Copy link
Contributor

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.

Copy link
Contributor

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].

Copy link
Contributor

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.

@@ -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),
Copy link
Contributor

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.

@bors
Copy link
Contributor

bors commented Dec 2, 2019

☔ The latest upstream changes (presumably #66944) made this pull request unmergeable. Please resolve the merge conflicts.

@bors bors added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Dec 2, 2019
@cjgillot
Copy link
Contributor Author

cjgillot commented Dec 3, 2019

Rebased with review comments.

Copy link
Member

@eddyb eddyb left a 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)

@bors
Copy link
Contributor

bors commented Dec 9, 2019

☔ The latest upstream changes (presumably #66984) made this pull request unmergeable. Please resolve the merge conflicts.

@nikomatsakis
Copy link
Contributor

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

@bors
Copy link
Contributor

bors commented Dec 9, 2019

⌛ Trying commit 0c4b8eee9c9e63f818861bec48e6fd356daf4166 with merge 963a1868bdc29bd60b42fc3c9705b6b186b61927...

@cjgillot
Copy link
Contributor Author

Rebased.

@Zoxc
Copy link
Contributor

Zoxc commented Dec 22, 2019

@bors retry

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 22, 2019
@Zoxc
Copy link
Contributor

Zoxc commented Dec 22, 2019

@bors r=eddyb

@bors
Copy link
Contributor

bors commented Dec 22, 2019

📌 Commit baa49b2 has been approved by eddyb

@bors
Copy link
Contributor

bors commented Dec 22, 2019

🌲 The tree is currently closed for pull requests below priority 100, this pull request will be tested once the tree is reopened

@matthewjasper
Copy link
Contributor

@bors p=100

@bors
Copy link
Contributor

bors commented Dec 22, 2019

⌛ Testing commit baa49b2 with merge 26286c7...

bors added a commit that referenced this pull request Dec 22, 2019
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.
@bors
Copy link
Contributor

bors commented Dec 22, 2019

☀️ Test successful - checks-azure
Approved by: eddyb
Pushing 26286c7 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Dec 22, 2019
@bors bors merged commit baa49b2 into rust-lang:master Dec 22, 2019
flip1995 added a commit to flip1995/rust-clippy that referenced this pull request Dec 22, 2019
bors added a commit to rust-lang/rust-clippy that referenced this pull request Dec 22, 2019
@cjgillot cjgillot deleted the hirene-preamble branch December 22, 2019 17:46
bors added a commit that referenced this pull request Dec 27, 2019
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
bors added a commit that referenced this pull request Dec 29, 2019
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
bors added a commit that referenced this pull request Dec 31, 2019
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants