Skip to content

Compute liveness constraints in location-sensitive polonius #134670

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

Merged
merged 8 commits into from
Dec 30, 2024

Conversation

lqd
Copy link
Member

@lqd lqd commented Dec 23, 2024

This continues the location-sensitive prototype. In this episode, we build the liveness constraints.

Reminder of the approach we're taking: we need variance data to create liveness edges in the forward/backward/both directions (respectively in the cases of covariance, contravariance, invariance) in the localized constraint graph.

This PR:

  • introduces the holder for that, and for the liveness data in the correct shape: the transpose of what we're using today, "live regions per points".
  • records use/drop live region variance during tracing
  • records regular live region variance at the end of liveness
  • records the correctly shaped live region per point matrix
  • uses all of the above to compute the liveness constraints

(There's still technically one tiny part of the liveness owl left to do, but I'll leave it for a future PR: we also need to disable the NLL optimization that avoids computing liveness for locals whose types contain a region outliving a free region -- the existing constraints make it effectively live at all points; this doesn't work under polonius)

r? @jackh726 cc @matthewjasper

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Dec 23, 2024
@lqd
Copy link
Member Author

lqd commented Dec 23, 2024

Again this should be gated and deactivated, but let's check if the changes have any impact:

@bors try @rust-timer queue

@rust-timer

This comment was marked as resolved.

bors added a commit to rust-lang-ci/rust that referenced this pull request Dec 23, 2024
Compute liveness constraints in location-sensitive polonius

This continues the location-sensitive prototype. In this episode, we build the liveness constraints.

Reminder of the approach we're taking: we need variance data to create liveness edges in the forward/backward/both directions (respectively in the cases of covariance, contravariance, invariance) in the localized constraint graph.

This PR:
- introduces the holder for that, and for the liveness data in the correct shape: the transpose of what we're using today, "live regions per points".
- records use/drop live region variance during tracing
- records regular live region variance at the end of liveness
- records the correctly shaped live region per point matrix
- uses all of the above to compute the liveness constraints

(There's still technically one tiny part of the liveness owl left to do, but I'll leave it for a future PR: we also need to disable the NLL optimization that avoids computing liveness for locals whose types contain a region outliving a free region -- the existing constraints make it effectively live at all points; this doesn't work under polonius)

r? `@jackh726` cc `@matthewjasper`
@bors
Copy link
Collaborator

bors commented Dec 23, 2024

⌛ Trying commit 47d46ae with merge df62f54...

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Dec 23, 2024
@bors
Copy link
Collaborator

bors commented Dec 23, 2024

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

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (df62f54): comparison URL.

Overall result: ❌ regressions - no action needed

Benchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR may lead to changes in compiler perf.

@bors rollup=never
@rustbot label: -S-waiting-on-perf -perf-regression

Instruction count

This is the most reliable metric that we have; it was used to determine the overall result at the top of this comment. However, even this metric can sometimes exhibit noise.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
0.3% [0.2%, 0.4%] 3
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Max RSS (memory usage)

This benchmark run did not return any relevant results for this metric.

Cycles

Results (secondary -0.9%)

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
2.0% [2.0%, 2.0%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-3.9% [-3.9%, -3.9%] 1
All ❌✅ (primary) - - 0

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 763.813s -> 762.245s (-0.21%)
Artifact size: 330.68 MiB -> 330.70 MiB (0.01%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Dec 23, 2024
@lqd lqd force-pushed the polonius-next-episode-4 branch from 47d46ae to 94fa197 Compare December 23, 2024 08:41
@lqd lqd force-pushed the polonius-next-episode-4 branch from 94fa197 to 0fd4615 Compare December 24, 2024 15:54
Copy link
Member

@jackh726 jackh726 left a comment

Choose a reason for hiding this comment

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

First commit

@lqd lqd force-pushed the polonius-next-episode-4 branch from 0fd4615 to f4e292a Compare December 25, 2024 14:41
Copy link
Member

@jackh726 jackh726 left a comment

Choose a reason for hiding this comment

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

Two small thoughts, but r=me with or without addressing them.

if let Some(polonius_context) = typeck.polonius_context.as_mut() {
let num_regions = infcx.num_region_vars();
let points_per_live_region = typeck.constraints.liveness_constraints.points();
polonius_context.record_live_regions_per_point(num_regions, points_per_live_region);
Copy link
Member

Choose a reason for hiding this comment

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

I wonder instead of using an Option in PoloniusContext, if it's better to just use a different struct for MirTypeckResults...

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, however I'd like to do something similar in a follow-up if you don't mind: what we eventually want to do I think is compute the localized constraints earlier (as they should only depend on mir typeck + liveness), and only store loan liveness in the context instead, with the optional debugging data that the polonius mir dump needs.


if region.is_bound() || region.is_erased() {
// ignore these
let Some(region) = self.universal_regions.try_to_region_vid(region) else {
Copy link
Member

Choose a reason for hiding this comment

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

Hmm, so this seems interesting. Where would we be seeing a Bound or Erased region? Seems curious to me that there's some subset of regions that we're essentially just ignoring.

Copy link
Member

Choose a reason for hiding this comment

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

Could be worth adding a comment explaining the cases this might happen.

Copy link
Member Author

@lqd lqd Dec 27, 2024

Choose a reason for hiding this comment

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

This came about like so:

  • I wanted the variance for vars and free regions and could only get it by duplicating the Generalizer's trick with TypeRelation, which unfortunately also yields regions that can't be converted to vids.
  • so I just ignored these ICE causing kinds and saw no particular changes in the tests (though there are still 6 failures left to diagnose and they could hiding in there): as far as I know there were no live regions that lacked variance due to this. There's also similarly unexpected kinds reaching borrowck, due to issues upstream and/or the ongoing effort of continuing compilation of code as deep as possible, that could be involved. To see whether we should be trying to e.g. recurse after skipping or discharging the binder in a different way that the binders callback is doing maybe, I casually brought it up with Niko who said "seems fine to ignore for now" and we didn't think more of it since then.

Since then I also discovered our test coverage is actually very weak in that area, so I'll ask you what is the correct thing to do here?

Copy link
Member

Choose a reason for hiding this comment

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

Personally, I would prefer to just fail to compile if we ever hit this (as we do for the examples you linked).

Really, I think there's an underlying problem here that we should investigate.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure, I'll put the ICEs back in then, and we can recheck and investigate later.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, for sure we can always come back to it.

Copy link
Member Author

@lqd lqd Dec 27, 2024

Choose a reason for hiding this comment

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

Sure, I'll put the ICEs back in then, and we can recheck and investigate later.

Actually maybe not, I forgot there's too many of them and I don't believe they really matter here: we're not ignoring regions in the graph because of the (transparent to reachability) fallback if no variance is found for a region. As for 'erased though, it was in this test that involves GCE amongst others, so imho it's fine to ignore for now.

A fixme like you initially suggested looks best under these circumstances. I'll ping you on zulip to discuss.

Copy link
Member

Choose a reason for hiding this comment

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

A FIXME is fine.

Copy link
Member Author

@lqd lqd Dec 29, 2024

Choose a reason for hiding this comment

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

Ok cool, I've pushed a fixme in the last commit, explaining why it's fine to not have variance data for these for now but we still need to look into it. Let me know what you think.

lqd added 8 commits December 29, 2024 17:47
This context struct will hold data to help creating localized
constraints:
- the live regions, with the shape matching a CFG walk, indexed per
  point
- the variance of these live regions, represented as the direction we'll
  add the appropriate

We also add this structure to the mir typeck to record liveness data,
and make it responsible for localized constraint creation.
Following the Generalizer's structure, relating e.g. a type with itself
will allow tracking the variance wrt the contained regions.
records the variance of:
- use live types
- drop live generic args
it missed the index and bounds info
transpose liveness matrix and record live regions at the end of MIR typeck
- add a FIXME when looking for the region variance of unexpected regions
- drive-by: fix a doc comment link
- drive-by: simplify the variance match using exported variants instead
@lqd lqd force-pushed the polonius-next-episode-4 branch from f4e292a to 089c525 Compare December 29, 2024 18:01
@jackh726
Copy link
Member

@bors r+

@bors
Copy link
Collaborator

bors commented Dec 29, 2024

📌 Commit 089c525 has been approved by jackh726

It is now in the queue for this repository.

@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 29, 2024
@bors
Copy link
Collaborator

bors commented Dec 30, 2024

⌛ Testing commit 089c525 with merge 8cdc67e...

@bors
Copy link
Collaborator

bors commented Dec 30, 2024

☀️ Test successful - checks-actions
Approved by: jackh726
Pushing 8cdc67e to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Dec 30, 2024
@bors bors merged commit 8cdc67e into rust-lang:master Dec 30, 2024
7 checks passed
@rustbot rustbot added this to the 1.85.0 milestone Dec 30, 2024
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (8cdc67e): comparison URL.

Overall result: ❌ regressions - no action needed

@rustbot label: -perf-regression

Instruction count

This is the most reliable metric that we have; it was used to determine the overall result at the top of this comment. However, even this metric can sometimes exhibit noise.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
0.1% [0.1%, 0.1%] 3
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Max RSS (memory usage)

Results (secondary -2.8%)

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-2.8% [-4.8%, -1.1%] 3
All ❌✅ (primary) - - 0

Cycles

Results (primary -0.8%, secondary -2.0%)

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.8% [-0.8%, -0.8%] 1
Improvements ✅
(secondary)
-2.0% [-2.0%, -2.0%] 1
All ❌✅ (primary) -0.8% [-0.8%, -0.8%] 1

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 761.218s -> 760.358s (-0.11%)
Artifact size: 325.50 MiB -> 325.48 MiB (-0.00%)

@lqd lqd deleted the polonius-next-episode-4 branch December 30, 2024 06:39
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. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants