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

Use copy bound in atomic operations to generate simpler MIR #70088

Merged
merged 1 commit into from
Mar 19, 2020

Conversation

tmiasko
Copy link
Contributor

@tmiasko tmiasko commented Mar 17, 2020

No description provided.

@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 @sfackler (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 Mar 17, 2020
@sfackler
Copy link
Member

r? @rust-lang/compiler

@eddyb
Copy link
Member

eddyb commented Mar 18, 2020

@tmiasko Can you add them to the atomic intrinsics as well? Assuming it works on them.

Either way, this PR seems correct in that using the atomic intrinsics with non-Copy types is effectively like using ptr::read to "move out" a value, which is likely UB for any shared memory.

cc @RalfJung @Amanieu

@tmiasko
Copy link
Contributor Author

tmiasko commented Mar 18, 2020

I was looking at this from the angle: what would it take for MIR transforms to optimize a.store(v, Ordering::Relaxed); to just an intrinsic call. This change helps, but inlining cost is still above the threshold for store operation in particular (there are costly panic calls, and even replacing them with an abort intrinsic it is merely sufficient to remove one layer of indirection and inline atomic_store function into the method).

Extending this to intrinsics should work. I could add them with understanding that it would break generic code that uses them without similar bounds, like for example this one here (before changes). In a separate PR preferably.

@oli-obk
Copy link
Contributor

oli-obk commented Mar 18, 2020

what would it take for MIR transforms to optimize a.store(v, Ordering::Relaxed); to just an intrinsic call.

We could extend the #[inline] syntax to have conditions like

#[inline(always, if_arg_is_const(order))]

To get a poor man's version of partial specialization (after which you most often can properly inline).

In general I'm fine with any kind of intrinsic wrapper just becoming #[inline(always)]

@Centril
Copy link
Contributor

Centril commented Mar 18, 2020

Either way, this PR seems correct in that using the atomic intrinsics with non-Copy types is effectively like using ptr::read to "move out" a value, which is likely UB for any shared memory.

N.B: It's only UB if the function is actually reached at runtime.

@RalfJung
Copy link
Member

ptr::read behavior does not depend on whether T is Copy or not. So I don't see any relation to UB here.

But until we have a good example for a non-Copy type one might want to use here, there's still no reason not to do this I think. :D

@eddyb
Copy link
Member

eddyb commented Mar 18, 2020

N.B: It's only UB if the function is actually reached at runtime.

Well, yeah, I meant that using them to duplicate/"move out" a value is likely UB, not just having around the function definitions.

So I don't see any relation to UB here.

What I mean is an atomic load of e.g. a Box<T> value is likely prone to UB if the underlying memory is actually shared cross-thread, as it duplicates a non-Copy value.
ptr::read on non-Copy types requires some form of ownership to prevent later UB, which is mostly antithetical to situations atomics are used in.

@eddyb
Copy link
Member

eddyb commented Mar 18, 2020

We could extend the #[inline] syntax to have conditions like

#[inline(always, if_arg_is_const(order))]

Heh, this is the sort of thing I was telling @wesleywiser we should compute instead of "inline costs" - conditions under which the function would become trivial.

bors added a commit that referenced this pull request Mar 18, 2020
Add copy bound to atomic & numeric intrinsics

Includes changes from #70088.
@tmiasko
Copy link
Contributor Author

tmiasko commented Mar 18, 2020

The changes here don't affect any public API (the other PR is more interesting in that respect).

r? @eddyb

@eddyb
Copy link
Member

eddyb commented Mar 18, 2020

@bors r+

@bors
Copy link
Contributor

bors commented Mar 18, 2020

📌 Commit af0d6fc has been approved by eddyb

@bors
Copy link
Contributor

bors commented Mar 18, 2020

🌲 The tree is currently closed for pull requests below priority 5, this pull request will be tested once the tree is reopened

@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 18, 2020
Centril added a commit to Centril/rust that referenced this pull request Mar 19, 2020
Use copy bound in atomic operations to generate simpler MIR
bors added a commit that referenced this pull request Mar 19, 2020
Rollup of 9 pull requests

Successful merges:

 - #69036 (rustc: don't resolve Instances which would produce malformed shims.)
 - #69443 (tidy: Better license checks.)
 - #69814 (Smaller and more correct generator codegen)
 - #69929 (Regenerate tables for Unicode 13.0.0)
 - #69959 (std: Don't abort process when printing panics in tests)
 - #69969 (unix: Set a guard page at the end of signal stacks)
 - #70005 ([rustdoc] Improve visibility for code blocks warnings)
 - #70088 (Use copy bound in atomic operations to generate simpler MIR)
 - #70095 (Implement -Zlink-native-libraries)

Failed merges:

r? @ghost
bors added a commit that referenced this pull request Mar 19, 2020
Rollup of 9 pull requests

Successful merges:

 - #68941 (Properly handle Spans that reference imported SourceFiles)
 - #69036 (rustc: don't resolve Instances which would produce malformed shims.)
 - #69443 (tidy: Better license checks.)
 - #69814 (Smaller and more correct generator codegen)
 - #69929 (Regenerate tables for Unicode 13.0.0)
 - #69959 (std: Don't abort process when printing panics in tests)
 - #69969 (unix: Set a guard page at the end of signal stacks)
 - #70005 ([rustdoc] Improve visibility for code blocks warnings)
 - #70088 (Use copy bound in atomic operations to generate simpler MIR)

Failed merges:

r? @ghost
@bors bors merged commit 605bb61 into rust-lang:master Mar 19, 2020
@tmiasko tmiasko deleted the atomic-copy branch March 19, 2020 09:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants