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

Capture precise paths in THIR and MIR #79553

Merged
merged 7 commits into from
Dec 12, 2020

Conversation

arora-aman
Copy link
Member

@arora-aman arora-aman commented Nov 30, 2020

This PR allows THIR and MIR to use the result of the new capture analysis to actually capture precise paths

To achieve we:

  • Writeback min capture results to TypeckResults
  • Move handling upvars to PlaceBuilder in mir_build
  • Lower precise paths in THIR build by reading min_captures
  • Search for ancestors in min_capture when trying to build a MIR place which starts off of an upvar

Closes: rust-lang/project-rfc-2229#10

Partly implements: rust-lang/project-rfc-2229#18

Work that remains (not in this PR):

r? @ghost

@joshtriplett
Copy link
Member

@bors try @rust-timer queue

@rust-timer
Copy link
Collaborator

Awaiting bors try build completion

@bors
Copy link
Contributor

bors commented Nov 30, 2020

⌛ Trying commit 3ac3ff2 with merge f76f72250e36c9ad4eb3bd3f08770228cc07da6c...

@bors
Copy link
Contributor

bors commented Nov 30, 2020

☀️ Try build successful - checks-actions
Build commit: f76f72250e36c9ad4eb3bd3f08770228cc07da6c (f76f72250e36c9ad4eb3bd3f08770228cc07da6c)

@rust-timer
Copy link
Collaborator

Queued f76f72250e36c9ad4eb3bd3f08770228cc07da6c with parent 28b86e0, future comparison URL.

@rust-timer
Copy link
Collaborator

Finished benchmarking try commit (f76f72250e36c9ad4eb3bd3f08770228cc07da6c): comparison url.

Benchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. Please note that if the perf results are neutral, you should likely undo the rollup=never given below by specifying rollup- to bors.

Importantly, though, if the results of this run are non-neutral do not roll this PR up -- it will mask other regressions or improvements in the roll up.

@bors rollup=never
@rustbot modify labels: +S-waiting-on-review -S-waiting-on-perf

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Nov 30, 2020
@nikomatsakis
Copy link
Contributor

r? @nikomatsakis

Copy link
Contributor

@nikomatsakis nikomatsakis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some initial comments

compiler/rustc_mir_build/src/build/expr/as_place.rs Outdated Show resolved Hide resolved
compiler/rustc_mir_build/src/build/expr/as_place.rs Outdated Show resolved Hide resolved
compiler/rustc_mir_build/src/build/expr/as_place.rs Outdated Show resolved Hide resolved
compiler/rustc_mir_build/src/build/expr/as_place.rs Outdated Show resolved Hide resolved
compiler/rustc_mir_build/src/build/expr/as_place.rs Outdated Show resolved Hide resolved
compiler/rustc_mir_build/src/build/expr/as_place.rs Outdated Show resolved Hide resolved
compiler/rustc_mir_build/src/build/expr/as_place.rs Outdated Show resolved Hide resolved
@camelid camelid added A-closures Area: closures (`|args| { .. }`) A-mir Area: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.html labels Dec 1, 2020
@arora-aman arora-aman force-pushed the mir_min_cap_writeback branch 4 times, most recently from 4c2eabd to 097e490 Compare December 2, 2020 01:11
compiler/rustc_mir_build/src/build/expr/as_place.rs Outdated Show resolved Hide resolved
@@ -2179,6 +2179,15 @@ impl<'tcx> TyS<'tcx> {
}
}

/// Get the `i`-th element of a tuple.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/// Computes the index of a capture within the desugared closure provided the closure's
/// `closure_min_captures` and the capture's index of the capture in the
/// `ty::MinCaptureList` of the root variable `var_hir_id`.
fn compute_capture_idx<'tcx>(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can probably be optimized by moving it into the min_capture map itself, so we move the table from looking like

root variable -> list of captures

to

root variarble -> { start_idx, list of captures }

where the start_idx is essentially the index in min_capture map for the first capture in the list of captures for the root variable.

@arora-aman
Copy link
Member Author

@nikomatsakis updated

arora-aman and others added 2 commits December 6, 2020 15:48
- Derive TypeFoldable on `hir::place::Place` and associated
  structs, to them to be written into typeck results.

Co-authored-by: Jennifer Wills <wills.jenniferg@gmail.com>
Co-authored-by: Logan Mosier <logmosier@gmail.com>
- final_upvar_tys now reads types from places instead of using `node_ty`

Co-authored-by: Roxane Fruytier <roxane.fruytier@hotmail.com>
- This allows us to delay figuring out the index of a capture
  in the closure structure when all projections to atleast form
  a capture have been applied to the builder

Co-authored-by: Roxane Fruytier <roxane.fruytier@hotmail.com>
compiler/rustc_middle/src/ty/sty.rs Outdated Show resolved Hide resolved
compiler/rustc_mir_build/src/build/expr/as_place.rs Outdated Show resolved Hide resolved
compiler/rustc_mir_build/src/build/expr/as_place.rs Outdated Show resolved Hide resolved
compiler/rustc_mir_build/src/build/expr/as_place.rs Outdated Show resolved Hide resolved
compiler/rustc_mir_build/src/build/expr/as_place.rs Outdated Show resolved Hide resolved
compiler/rustc_mir_build/src/build/expr/as_place.rs Outdated Show resolved Hide resolved
compiler/rustc_mir_build/src/build/expr/as_place.rs Outdated Show resolved Hide resolved
compiler/rustc_mir_build/src/build/expr/as_rvalue.rs Outdated Show resolved Hide resolved
compiler/rustc_mir_build/src/thir/cx/expr.rs Show resolved Hide resolved
@nikomatsakis
Copy link
Contributor

Hey! Left a bunch of comments. Mostly nits and small suggestions, though there are a few points where I was confused. This is looking really good. :)

@arora-aman
Copy link
Member Author

I pushed changed in the same commit because there weren't any logical changes. Just the diff is here: https://github.com/sexxi-goose/rust/compare/ef71437..ad36cd2

/// start off of the same root variable.
///
/// Eg: 1. `foo.x` which is represented using `projections=[Field(x)]` is an ancestor of
/// `foo.x.y` which is represented using `prjections=[Field(x), Field(y)]`.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alignment

proj_possible_ancestor: &Vec<HirProjectionKind>,
proj_capture: &Vec<HirProjectionKind>,
) -> bool {
// We want to make sure `is_ancestor_of_capture("x.0", "x.0.0")` not return true.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is_ancestor_or_same_capture("x.0.0", "x.0")

Copy link
Contributor

@nikomatsakis nikomatsakis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking very good. I am wondering if we want to do more tests.

arora-aman and others added 3 commits December 9, 2020 22:33
- Use closure_min_capture maps to capture precise paths
- PlaceBuilder now searches for ancestors in min_capture list
- Add API to `Ty` to allow access to the n-th element in a
  tuple in O(1) time.

Co-authored-by: Roxane Fruytier <roxane.fruytier@hotmail.com>
- Closures now use closure_min_captures to figure out captured paths
- Build upvar_mutbls using closure_min_captures
- Change logic in limit_capture_mutability to differentiate b/w
  capturing parent's local variable or capturing a variable that is
  captured by the parent (in case of nested closure) using PlaceBase.

Co-authored-by: Roxane Fruytier <roxane.fruytier@hotmail.com>
- Use closure_min_captures to generate the Upvar structure that
  stores information for diagnostics and information about
  mutability of captures.
@arora-aman
Copy link
Member Author

@nikomatsakis updated.

I added some more test cases:

@nikomatsakis
Copy link
Contributor

@bors r+

@bors
Copy link
Contributor

bors commented Dec 11, 2020

📌 Commit 01df563 has been approved by nikomatsakis

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

bors commented Dec 12, 2020

⌛ Testing commit 01df563 with merge 5bd9b60...

@bors
Copy link
Contributor

bors commented Dec 12, 2020

☀️ Test successful - checks-actions
Approved by: nikomatsakis
Pushing 5bd9b60 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Dec 12, 2020
@bors bors merged commit 5bd9b60 into rust-lang:master Dec 12, 2020
@rustbot rustbot added this to the 1.50.0 milestone Dec 12, 2020
@arora-aman arora-aman deleted the mir_min_cap_writeback branch December 12, 2020 04:49
/// DefId of the closure
closure_def_id: DefId,
/// The trait closure implements, `Fn`, `FnMut`, `FnOnce`
closure_kind: ty::ClosureKind },
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, how did rustfmt accept this formatting???

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that...is a good question :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've no idea whether this is intentional, but believe this file would be skipped entirely due the ignore settings specified in the rustfmt file since it's under a build directory

Compare the build directory entry in rustfmt.toml vs the gitignore entry where the latter is limited to the root.

As I recall there were also some constraints added around which files get formatted and how due to the nature of some of the content within the repo. It's formatted fine when running rustfmt directly, so may want to check whether x.py fmt is actually running rustfmt on these files

cc @Mark-Simulacrum

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah seems like a bug. Would be great to fix in a PR.

@rylev
Copy link
Member

rylev commented Dec 15, 2020

@arora-aman @nikomatsakis This changes seems to have caused a moderate regression in the clap benchmark. It looks like typck is taking longer than before (37.5% of compilation time vs. 35.5%).

@arora-aman
Copy link
Member Author

@rylev I had brought this in t-performance a while back, it's likely that this is because of extra data now being stored in TypeckResults, which adds more data that needs to get hashed in incr-compile mode.

We are working de duplicating some of the information in TypeckResults, to hopefully being this back down :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-closures Area: closures (`|args| { .. }`) A-mir Area: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.html 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.

Rewrite HIR->MIR lowering to use Places