-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Conversation
r? @cramertj (rust-highfive has picked a reviewer for you, use r? to override) |
@bors try @rust-timer queue - though I doubt this will have any significant effect, seems good to know |
Awaiting bors try build completion |
⌛ 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...
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.) |
(I think the post-try push may prevent the bors notification for try completion, so a manual rust-timer build invocation may become necessary). |
@rust-timer build 196ca16 |
Queued 196ca161be8363503d2d2445bab9840f0ce2bba4 with parent 7efc097, future comparison URL. |
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 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 |
/// 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")] |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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. :)
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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
,
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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.)
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))] |
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would have thought so too, but https://github.com/rust-lang/rust/runs/1548179565
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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?
☔ 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:
|
@scottmcm any update? |
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`~~.
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`~~.
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.