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

mir-opt: Replace clone on primitives with copy #94276

Merged
merged 5 commits into from
Mar 11, 2022

Conversation

scottmcm
Copy link
Member

We can't do it for everything, but it would be nice to at least stop making calls to clone methods in debug from things like derived-clones.

r? @ghost

@scottmcm scottmcm added the A-mir-opt Area: MIR optimizations label Feb 23, 2022
@rustbot rustbot added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Feb 23, 2022
@scottmcm scottmcm force-pushed the primitive-clone branch 2 times, most recently from b7293c8 to 05db2c2 Compare February 23, 2022 07:59
@scottmcm
Copy link
Member 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 Feb 23, 2022
@bors
Copy link
Contributor

bors commented Feb 23, 2022

⌛ Trying commit 05db2c29b137029c3db4d1dc0653fa3bd4d261bb with merge e3ab4694509bf757e2b190ca236e525005c34285...

@bors
Copy link
Contributor

bors commented Feb 23, 2022

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

@rust-timer
Copy link
Collaborator

Queued e3ab4694509bf757e2b190ca236e525005c34285 with parent bafe8d0, future comparison URL.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (e3ab4694509bf757e2b190ca236e525005c34285): comparison url.

Summary: This benchmark run shows 1 relevant improvement 🎉 to instruction counts.

  • Largest improvement in instruction counts: -2.3% on incr-patched: b9b3e592dd cherry picked builds of style-servo debug

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.

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

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Feb 23, 2022
@scottmcm
Copy link
Member Author

scottmcm commented Feb 23, 2022

Since this is green in perf (not even just neutral, to my surprise) and it avoids function calls for clones in debug (unlike today, where you can see things like call core::clone::impls::<impl core::clone::Clone for u32>::clone), I'd like to get one of the expert's opinions of if this is worth doing:

r? rust-lang/mir-opt

Copy link
Contributor

@oli-obk oli-obk left a comment

Choose a reason for hiding this comment

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

We're missing probably the largest chunk of things, and that is calling generic functions that clone their generic parameters. MIR opts will not work there, as they only see the generic function, and the Clone bounds don't allow switching to Copy in that case. This is a recurring problem with other MIR opts, too.

That said, the effects on a real world benchmark are neat though I do wonder how much of that is these benchmarks being old and not having had clippy applied to them (which will tell you to do this transformation in user space, and even on generic things).

}
}

fn is_trivially_pure_copy<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> bool {
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 live next to is_trivially_sized and should probably be used to speed up is_copy_modulo_regions by avoiding a query call.

@scottmcm
Copy link
Member Author

scottmcm commented Mar 2, 2022

I do wonder how much of that is these benchmarks being old and not having had clippy applied to them (which will tell you to do this transformation in user space, and even on generic things).

That's why the test I used here is #[derive(Clone)], since that's a case where one will regularly have clone calls on primitive types that a human can't change to a deref.

It's definitely true that this is something that LLVM will reliably inline, so only hitting the obvious things in the generic MIR is ok by me.

I'll go do the move you mentioned in the other comment
@rustbot author

@rustbot rustbot 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 Mar 2, 2022
@scottmcm scottmcm force-pushed the primitive-clone branch 2 times, most recently from 08905f9 to 8e242d7 Compare March 6, 2022 03:31
@scottmcm
Copy link
Member Author

scottmcm commented Mar 6, 2022

Well, I tried short-circuiting in is_copy_modulo_regions, but that somehow makes it unable to prove u32: Copy in a test:

error: internal compiler error: broken MIR in DefId(0:3 ~ params_on_registers[6840]::entry_function) (NoSolution): could not prove Binder(TraitPredicate(<u32 as Copy>, polarity:Positive), [])
   |
   = note: delayed at compiler\rustc_borrowck\src\type_check\canonical.rs:150:13

So I left that out of the change. I definitely don't know enough about queries and such to have any idea why that's happening. It fails in ui\cmse-nonsecure\cmse-nonsecure-entry\params-on-stack.rs having only called my new is_trivially_pure_clone_copy on u32, not on anything else. I opened a zulip thread about it.

@rustbot ready

@rustbot rustbot 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 Mar 6, 2022
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Contributor

bors commented Mar 10, 2022

📌 Commit 705b880 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-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Mar 10, 2022
@bors
Copy link
Contributor

bors commented Mar 10, 2022

⌛ Testing commit 705b880 with merge a7c0d7302bbc247a044776a9fd08c0cd20a82bab...

@bors
Copy link
Contributor

bors commented Mar 10, 2022

💔 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 Mar 10, 2022
@rust-log-analyzer
Copy link
Collaborator

A job failed! Check out the build log: (web) (plain)

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

@matthiaskrgr
Copy link
Member

@bors retry auto (dist-apple-various didn't start

@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 Mar 10, 2022
@bors
Copy link
Contributor

bors commented Mar 10, 2022

⌛ Testing commit 705b880 with merge f8311752f15ee207d04dd52ad61c693c0d2944df...

@rust-log-analyzer
Copy link
Collaborator

A job failed! Check out the build log: (web) (plain)

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

@bors
Copy link
Contributor

bors commented Mar 10, 2022

💔 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 Mar 10, 2022
@scottmcm
Copy link
Member Author

@bors retry -- this time it's dist-x86_64-apple that didn't start

@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 Mar 10, 2022
@bors
Copy link
Contributor

bors commented Mar 11, 2022

⌛ Testing commit 705b880 with merge c5a43b8...

@bors
Copy link
Contributor

bors commented Mar 11, 2022

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

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Mar 11, 2022
@bors bors merged commit c5a43b8 into rust-lang:master Mar 11, 2022
@rustbot rustbot added this to the 1.61.0 milestone Mar 11, 2022
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (c5a43b8): comparison url.

Summary: This benchmark run did not return any relevant results. 38 results were found to be statistically significant but too small to be relevant.

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

@rustbot label: -perf-regression

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-mir-opt Area: MIR optimizations 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.

9 participants