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

Array .len() MIR optimization pass #86525

Merged
merged 6 commits into from
Oct 7, 2021
Merged

Conversation

shamatar
Copy link
Contributor

This pass kind-of works back the [T; N].len() call that at the moment is first coerced as &[T; N] -> &[T] and then uses &[T].len(). Depends on #86383

@rust-highfive
Copy link
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @LeSeulArtichaut (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 21, 2021
@shamatar
Copy link
Contributor Author

shamatar commented Jun 21, 2021

Due to dependency it's better to look at compiler/rustc_mir/src/transform/normalize_array_len.rs only for now.

The main pain point is that the only way to deal with an expression like this _6 = move _7 as &[u8] (Pointer(Unsize)); in a sound way is either more complex analysis in attempt to patch into _6 = move _7; without unsize cast, or take an approach of extra copy (as done in this PR) assuming that after _6 = move _7 as &[u8] (Pointer(Unsize)); some expression can use _6 being slice or even reassign into it

@LeSeulArtichaut LeSeulArtichaut added S-blocked Status: Marked as blocked ❌ on something else such as an RFC or other implementation work. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 21, 2021
@LeSeulArtichaut
Copy link
Contributor

r? @oli-obk

@rust-log-analyzer

This comment has been minimized.

@shamatar
Copy link
Contributor Author

shamatar commented Jun 21, 2021

There is an alternative approach to run this pass somewhat later, and assume that after previous passes we are only interested in cases when the destination of cast is only touched in the Len and then dead by the end of the block without any other modifications

          _6 = move _7 as &[u8] (Pointer(Unsize));
          StorageDead(_7);
          _5 = Len((*_6)); 
          StorageDead(_6);

Even more alternative approach is to go full scale dataflow analysis and be able to remove a unsize cast by proving that all other potentially dependent statements are ok with _6 being &[T; N]

@shamatar
Copy link
Contributor Author

shamatar commented Jun 21, 2021

@bors try

All tests passed locally, problem was due to not committed files

@bors
Copy link
Contributor

bors commented Jun 21, 2021

@shamatar: 🔑 Insufficient privileges: not in try users

@shamatar
Copy link
Contributor Author

I'll add for completeness

@rustbot label: +S-waiting-on-review

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 21, 2021
@shamatar
Copy link
Contributor Author

shamatar commented Jun 21, 2021

For it to work an operand of Len has to be [T; N], but without this pass it's &[T] in usual situations here. You can see it in the src/test/mir-opt/issue_76432.test.SimplifyComparisonIntegral.diff changes that are a by-product, Len() that was there before was optimized down to constant

- _8 = Lt(_6, _7); // scope 0 at $DIR/slice_len.rs:5:5: 5:33
- assert(move _8, "index out of bounds: the length is {} but the index is {}", move _7, _6) -> bb1; // scope 0 at $DIR/slice_len.rs:5:5: 5:33
+ _7 = const 3_usize; // scope 0 at $DIR/slice_len.rs:5:5: 5:33
+ _8 = const true; // scope 0 at $DIR/slice_len.rs:5:5: 5:33
+ assert(const true, "index out of bounds: the length is {} but the index is {}", const 3_usize, const 1_usize) -> bb1; // scope 0 at $DIR/slice_len.rs:5:5: 5:33
Copy link
Contributor

@klensy klensy Jun 21, 2021

Choose a reason for hiding this comment

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

Hmm, shouldn't be this assert removed, as it const true? Or maybe in other pass, i don't really understand this thing deep enough.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It may be removed, but the test doesn't output the results of other passes, just the diff due to application of ConstProp one

Copy link
Member

Choose a reason for hiding this comment

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

We run a SimplifyBranches pass right after ConstProp which will turn the assert into goto -> bb1

@wesleywiser
Copy link
Member

@bors try @rust-timer queue

@rust-timer
Copy link
Collaborator

Awaiting bors try build completion.

@rustbot label: +S-waiting-on-perf

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jun 22, 2021
@bors
Copy link
Contributor

bors commented Jun 22, 2021

⌛ Trying commit 87797843fb2598134ec8ecc86dc718c0d4657a22 with merge d903a20d696dba2fa37a25fe955133069819febc...

@shamatar
Copy link
Contributor Author

@rustbot label: -S-blocked

@rustbot rustbot removed the S-blocked Status: Marked as blocked ❌ on something else such as an RFC or other implementation work. label Jun 22, 2021
@bors
Copy link
Contributor

bors commented Jun 22, 2021

💔 Test failed - checks-actions

@rust-log-analyzer

This comment has been minimized.

@bors bors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 22, 2021
@rust-log-analyzer

This comment has been minimized.

@shamatar
Copy link
Contributor Author

shamatar commented Oct 6, 2021

@JohnCSimon Done

@fee1-dead fee1-dead added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Oct 6, 2021
@oli-obk
Copy link
Contributor

oli-obk commented Oct 6, 2021

@bors r+

Thanks for bearing with us while we figure out our process for reviewing mir opts with the little review capacity we have in wg-mir-opt.

@bors
Copy link
Contributor

bors commented Oct 6, 2021

📌 Commit a31518f has been approved by oli-obk

@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 Oct 6, 2021
@bors
Copy link
Contributor

bors commented Oct 6, 2021

⌛ Testing commit a31518f with merge 5c402bc869d4e1188ec5974820481d49b1040322...

@bors
Copy link
Contributor

bors commented Oct 6, 2021

💔 Test failed - checks-actions

@bors bors added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Oct 6, 2021
@rust-log-analyzer
Copy link
Collaborator

The job i686-gnu failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)

@shamatar
Copy link
Contributor Author

shamatar commented Oct 7, 2021

Looks like random error during CI execution

@oli-obk
Copy link
Contributor

oli-obk commented Oct 7, 2021

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

bors commented Oct 7, 2021

⌛ Testing commit a31518f with merge 680ff86...

@bors
Copy link
Contributor

bors commented Oct 7, 2021

☀️ Test successful - checks-actions
Approved by: oli-obk
Pushing 680ff86 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Oct 7, 2021
@bors
Copy link
Contributor

bors commented Oct 7, 2021

☀️ Test successful - checks-actions
Approved by: oli-obk
Pushing 680ff86 to master...

@bors bors merged commit 680ff86 into rust-lang:master Oct 7, 2021
@rustbot rustbot added this to the 1.57.0 milestone Oct 7, 2021
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (680ff86): comparison url.

Summary: This benchmark run did not return any relevant changes.

If you disagree with this performance assessment, please file an issue in rust-lang/rustc-perf.

@rustbot label: -perf-regression

@shamatar shamatar deleted the array_len_opt branch February 29, 2024 14:49
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.