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

Stop generating code in mem::forget #79989

Closed
wants to merge 2 commits into from

Conversation

scottmcm
Copy link
Member

Yes, the optimizer can remove it, but there's really no reason to bother emitting it in the first place. Not to mention that it all gets run in debug mode...

Preemptively cc @RalfJung, who probably wouldn't be happy about me doing this.

Tangentially related to #79914, though that's more about used ones where optimizing it away is harder than it is here.

@rust-highfive
Copy link
Collaborator

r? @cramertj

(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 Dec 12, 2020
@Mark-Simulacrum
Copy link
Member

@bors try @rust-timer queue - though I doubt this will have any significant effect, seems good to know

@rust-timer
Copy link
Collaborator

Awaiting bors try build completion

@bors
Copy link
Contributor

bors commented Dec 12, 2020

⌛ Trying commit bb78e10a9662fcaefffdd8b40ebcea4977232c39 with merge 196ca161be8363503d2d2445bab9840f0ce2bba4...

Yes, the optimizer can remove it, but there's really no reason to bother emitting it in the first place.  Not to mention that it all gets run in debug mode...
@scottmcm
Copy link
Member Author

scottmcm commented Dec 12, 2020

Thanks @Mark-Simulacrum -- you beat me to it 😆

(Edit: I was going to wait for CI because I'm way too good at forgetting something.)

@Mark-Simulacrum
Copy link
Member

(I think the post-try push may prevent the bors notification for try completion, so a manual rust-timer build invocation may become necessary).

@jyn514 jyn514 added the T-libs Relevant to the library team, which will review and decide on the PR/issue. label Dec 13, 2020
@rust-lang rust-lang deleted a comment from rust-timer Dec 13, 2020
@scottmcm
Copy link
Member Author

@rust-timer build 196ca16

@rust-timer
Copy link
Collaborator

Queued 196ca161be8363503d2d2445bab9840f0ce2bba4 with parent 7efc097, future comparison URL.

@rust-lang rust-lang deleted a comment from rust-timer Dec 13, 2020
@rust-timer
Copy link
Collaborator

Finished benchmarking try commit (196ca161be8363503d2d2445bab9840f0ce2bba4): 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

/// compiles fine just using `ManuallyDrop` instead.
///
/// As this does literally nothing, it's trivially const-safe.
#[rustc_const_stable(feature = "const_forget_intrinsic", since = "1.50")]
Copy link
Member

Choose a reason for hiding this comment

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

Cc @rust-lang/wg-const-eval for the const stabilization here

Copy link
Member

Choose a reason for hiding this comment

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

@scottmcm could you add a test making sure that this actually works at const-time? From what I can see it actually should not work since this intrinsic is not implemented... you'll probably have to copy the Miri implementation.

Copy link
Member Author

@scottmcm scottmcm Dec 14, 2020

Choose a reason for hiding this comment

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

I added a test, but it seemed to run fine? Please let me know if that's not the right way to test such a thing.

Copy link
Member

Choose a reason for hiding this comment

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

@oli-obk any idea how this test can possibly pass without forget being implemented in this file? I am very confused right now.

Copy link
Member

@RalfJung RalfJung Dec 14, 2020

Choose a reason for hiding this comment

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

The intrinsic does not even seem to be called:

├─7ms  INFO rustc_mir::interpret::step // executing bb1
├─7ms  INFO rustc_mir::interpret::step _0 = forget_arg_and_return_4(move _1) -> bb2
├┐rustc_mir::interpret::eval_context::frame FOUR_THE_HARD_WAY
│└┐rustc_mir::interpret::eval_context::frame forget_arg_and_return_4
│┌┘rustc_mir::interpret::eval_context::frame forget_arg_and_return_4
├┘rustc_mir::interpret::eval_context::frame FOUR_THE_HARD_WAY
├┐rustc_mir::interpret::eval_context::frame FOUR_THE_HARD_WAY
│└┐rustc_mir::interpret::eval_context::frame forget_arg_and_return_4
│ ├─0ms  INFO rustc_mir::interpret::step // executing bb0
│ ├─0ms  INFO rustc_mir::interpret::step StorageLive(_2)
│ ├─0ms  INFO rustc_mir::interpret::step StorageLive(_3)
│ ├─0ms  INFO rustc_mir::interpret::step _3 = move _1
│ ├─0ms  INFO rustc_mir::interpret::step _2 = std::mem::forget::<Vec<i32>>(move _3) -> [return: bb1, unwind: bb2]
│ ├┐rustc_mir::interpret::eval_context::frame forget_arg_and_return_4
│ │└┐rustc_mir::interpret::eval_context::frame std::mem::forget::<Vec<i32>>
│ │ ├─0ms  INFO rustc_mir::interpret::step // executing bb0
│ │ ├─0ms  INFO rustc_mir::interpret::step _0 = const ()
│ │ ├─0ms  INFO rustc_mir::interpret::step return
│ │ ├─0ms  INFO rustc_mir::interpret::eval_context popping stack frame (returning from function)
│ │┌┘rustc_mir::interpret::eval_context::frame std::mem::forget::<Vec<i32>>
│ ├┘rustc_mir::interpret::eval_context::frame forget_arg_and_return_4
│ ├─0ms  INFO rustc_mir::interpret::step // executing bb1
│ ├─0ms  INFO rustc_mir::interpret::step StorageDead(_3)
│ ├─0ms  INFO rustc_mir::interpret::step StorageDead(_2)
│ ├─0ms  INFO rustc_mir::interpret::step _0 = const 4_i32
│ ├─0ms  INFO rustc_mir::interpret::step return
│ ├─0ms  INFO rustc_mir::interpret::eval_context popping stack frame (returning from function)
│┌┘rustc_mir::interpret::eval_context::frame forget_arg_and_return_4
├┘rustc_mir::interpret::eval_context::frame FOUR_THE_HARD_WAY
├─7ms  INFO rustc_mir::interpret::step // executing bb2
├─7ms  INFO rustc_mir::interpret::step StorageDead(_1)
├─7ms  INFO rustc_mir::interpret::step return
├─7ms  INFO rustc_mir::interpret::eval_context popping stack frame (returning from function)
rustc_mir::interpret::eval_context::frame FOUR_THE_HARD_WAY

The body of std::mem::forget seems to consist only of _0 = const ().

Also very strange: the trace indicates two calls to forget_arg_and_return_4?

Copy link
Member

Choose a reason for hiding this comment

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

Hm, the intrinsic didn't have a body before either, so I guess there is no UB change. The intrinsic simply does not need an implementation any more.

@scottmcm you are good then, aside from the cfg(bootstrap) at the test which I think is unnecessary. :)

Copy link
Member

Choose a reason for hiding this comment

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

Turns out you actually do need to add the intrinsic implementation, as @tmiasko pointed out. It is still needed when libstd is built with -Zmir-opt-level=0.

Copy link
Contributor

Choose a reason for hiding this comment

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

It should be possible to test this by calling core::intrinsics::forget directly, instead of core::mem::forget wrapper, with -Zmir-opt-level=0,

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll add the test. Looks like with #80040 it won't need the intrinsic implementation any more, though, right?

Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like with #80040 it won't need the intrinsic implementation any more, though, right?

Yes.

// which there's no reason to do. Please switch it back if things have changed and
// the forget-is-nop codegen test confirms the intrinsic is no longer needed here.

// SAFETY: Forgetting is safe; it's just the intrinsic that isn't.
Copy link
Member

Choose a reason for hiding this comment

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

FWIW, intrinsics can also be marked as safe. (But no need to do that in this PR.)

@RalfJung
Copy link
Member

RalfJung commented Dec 13, 2020

Preemptively cc @RalfJung, who probably wouldn't be happy about me doing this.

Thanks for the heads-up! This is certainly not the nicest reason I have seen to introduce/expand an intrinsic, but I've also seen worse, so I am rather neutral regarding this change. ;)

@@ -137,3 +137,16 @@ fn assume_init_good() {

assert!(TRUE);
}

#[test]
#[cfg(not(bootstrap))]
Copy link
Member

Choose a reason for hiding this comment

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

The test should pass with and without bootstrap, shouldn't it? mem::forget is const-stable already...

Copy link
Member Author

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.

Ah, that's probably because of the missing intrinsic implementation in the bootstrap compiler. Good point.

// ```
// but as of 2020-12-12 that actually codegens the construction of the type,
// which there's no reason to do. Please switch it back if things have changed and
// the forget-is-nop codegen test confirms the intrinsic is no longer needed here.
Copy link
Member

Choose a reason for hiding this comment

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

Should this be a FIXME pointing to the ManuallyDrop issue?

@bors
Copy link
Contributor

bors commented Dec 16, 2020

☔ The latest upstream changes (presumably #79607) made this pull request unmergeable. Please resolve the merge conflicts.

Note that reviewers usually do not review pull requests until merge conflicts are resolved! Once you resolve the conflicts, you should change the labels applied by bors to indicate that your PR is ready for review. Post this as a comment to change the labels:

@rustbot modify labels: +S-waiting-on-review -S-waiting-on-author

@JohnCSimon JohnCSimon 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 Jan 11, 2021
@RalfJung RalfJung mentioned this pull request Jan 18, 2021
3 tasks
@usbalbin usbalbin mentioned this pull request Jan 18, 2021
@camelid camelid 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-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jan 29, 2021
@JohnCSimon JohnCSimon 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-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Feb 14, 2021
@Dylan-DPC-zz
Copy link

@scottmcm any update?

Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request Feb 24, 2021
Make ptr::write const

~~The code in this PR as of right now is not much more than an experiment.~~

~~This should, if I am not mistaken, in theory compile and pass the tests once the bootstraping compiler is updated. Thus the PR is blocked on that which should happen some time after the February the 9th. Also we might want to wait for rust-lang#79989 to avoid regressing performance due to using `mem::forget` over `intrinsics::forget`~~.
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request Feb 25, 2021
Make ptr::write const

~~The code in this PR as of right now is not much more than an experiment.~~

~~This should, if I am not mistaken, in theory compile and pass the tests once the bootstraping compiler is updated. Thus the PR is blocked on that which should happen some time after the February the 9th. Also we might want to wait for rust-lang#79989 to avoid regressing performance due to using `mem::forget` over `intrinsics::forget`~~.
@JohnCSimon JohnCSimon 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-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Mar 7, 2021
@scottmcm scottmcm closed this Mar 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.