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

Simplify for loop desugar #90352

Merged
merged 4 commits into from
Nov 22, 2021
Merged

Simplify for loop desugar #90352

merged 4 commits into from
Nov 22, 2021

Conversation

camsteffen
Copy link
Contributor

Basically two intermediate bindings are inlined. I could have left one intermediate binding in place as this would simplify some diagnostic logic, but I think the difference in that regard would be negligible, so it is better to have a minimal HIR.

For checking that the pattern is irrefutable, I added a special case when the match is found to be non-exhaustive.

The reordering of the arms is purely stylistic. I don't think there are any perf implications.

  match IntoIterator::into_iter($head) {
      mut iter => {
          $label: loop {
-             let mut __next;
              match Iterator::next(&mut iter) {
-                 Some(val) => __next = val,
                  None => break,
+                 Some($pat) => $block,
              }
-             let $pat = __next;
-             $block
          }
      }
  }

@rust-highfive
Copy link
Collaborator

Some changes occurred in src/tools/clippy.

cc @rust-lang/clippy

@rust-highfive
Copy link
Collaborator

r? @oli-obk

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 27, 2021
@Mark-Simulacrum
Copy link
Member

@bors try @rust-timer queue

@bors
Copy link
Contributor

bors commented Oct 27, 2021

⌛ Trying commit f91e0b1f918aa3b7f9ad897a742730fe50f5eee4 with merge 52f2d9b64faebfa33ae0fd45fcbf21bdc4154fa1...

@rust-log-analyzer

This comment has been minimized.

@camsteffen
Copy link
Contributor Author

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

bors commented Oct 27, 2021

⌛ Trying commit b44eed913416e05c2a5f889c4a10d9ab3421e035 with merge 261bc72edbd828cafa27c3450ef92a59b5e7bacb...

@petrochenkov
Copy link
Contributor

@camsteffen
I strongly suggest looking through git history and checking why these bindings were introduced in the first place.
Most likely it was done for a reason, e.g. to extend lifetimes or something like that.

@camsteffen
Copy link
Contributor Author

camsteffen commented Oct 27, 2021

@petrochenkov It looks like the key points of history are #42265 and #42634. I think these are workarounds for typeck/borrowck issues that no longer exist, especially with NLL?

@cjgillot cjgillot self-assigned this Oct 27, 2021
@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Contributor

bors commented Oct 27, 2021

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

@rust-timer
Copy link
Collaborator

Queued 261bc72edbd828cafa27c3450ef92a59b5e7bacb with parent dd757b9, future comparison URL.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (261bc72edbd828cafa27c3450ef92a59b5e7bacb): comparison url.

Summary: This change led to very large relevant mixed results 🤷 in compiler performance.

  • Very large improvement in instruction counts (up to -6.0% on incr-full builds of clap-rs)
  • Large regression in instruction counts (up to 2.6% on full builds of regex)

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

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 led to changes in compiler perf.

Next Steps: If you can justify the regressions found in this try perf run, please indicate this with @rustbot label: +perf-regression-triaged along with sufficient written justification. If you cannot justify the regressions please fix the regressions and do another perf run. If the next run shows neutral or positive results, the label will be automatically removed.

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

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Oct 28, 2021
@apiraino apiraino added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Oct 28, 2021
@@ -183,7 +183,7 @@ pub fn add_loop_label_to_break() {
#[cfg(not(any(cfail1,cfail4)))]
#[rustc_clean(cfg="cfail2", except="hir_owner_nodes")]
#[rustc_clean(cfg="cfail3")]
#[rustc_clean(cfg="cfail5", except="hir_owner_nodes")]
#[rustc_clean(cfg="cfail5", except="hir_owner_nodes, optimized_mir")]
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is optimized_mir dirty?

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 don't know. Do you know how to see what changed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Perhaps it's an optimization of

match next {
    None => break,
    Some(..) => break,
}

compiler/rustc_mir_build/src/thir/pattern/check_match.rs Outdated Show resolved Hide resolved
@memoryruins
Copy link
Contributor

for's std docs mentions its expansion. Should we update it here as well?

/// A `for` loop expands as shown:

@JohnCSimon JohnCSimon added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 16, 2021
@camsteffen camsteffen force-pushed the for-loop-desugar branch 2 times, most recently from c3c982d to a60977b Compare November 16, 2021 20:15
@camsteffen
Copy link
Contributor Author

@bors r=oli-obk

@bors
Copy link
Contributor

bors commented Nov 21, 2021

📌 Commit 21a53d1b1ba896d06534207402885fb93ffd1da9 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 Nov 21, 2021
@bors
Copy link
Contributor

bors commented Nov 21, 2021

⌛ Testing commit 21a53d1b1ba896d06534207402885fb93ffd1da9 with merge 22453953cf17d8d767864fd79f96aeae798d6542...

@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Contributor

bors commented Nov 21, 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 Nov 21, 2021
@camsteffen
Copy link
Contributor Author

@bors r=oli-obk

@bors
Copy link
Contributor

bors commented Nov 21, 2021

📌 Commit 66da8fa 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 Nov 21, 2021
@bors
Copy link
Contributor

bors commented Nov 21, 2021

⌛ Testing commit 66da8fa with merge cebd2dd...

@bors
Copy link
Contributor

bors commented Nov 22, 2021

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

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Nov 22, 2021
@bors bors merged commit cebd2dd into rust-lang:master Nov 22, 2021
@rustbot rustbot added this to the 1.58.0 milestone Nov 22, 2021
@camsteffen camsteffen deleted the for-loop-desugar branch November 22, 2021 01:39
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (cebd2dd): comparison url.

Summary: This change led to very large relevant mixed results 🤷 in compiler performance.

  • Very large improvement in instruction counts (up to -6.2% on incr-full builds of clap-rs)
  • Large regression in instruction counts (up to 2.3% on full builds of regex)

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

Next Steps: If you can justify the regressions found in this perf run, please indicate this with @rustbot label: +perf-regression-triaged along with sufficient written justification. If you cannot justify the regressions please open an issue or create a new PR that fixes the regressions, add a comment linking to the newly created issue or PR, and then add the perf-regression-triaged label to this PR.

@rustbot label: +perf-regression

@oli-obk
Copy link
Contributor

oli-obk commented Nov 22, 2021

The regressions seem to all be in -opt builds and solely part of the time spent in LLVM, so I'm hoping it's that more optimizations apply now (and worst case some optimizations require more work but don't result in better code).

@rustbot label: +perf-regression-triaged

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. perf-regression Performance regression. perf-regression-triaged The performance regression has been triaged. 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.