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 volatile access instead of #[used] for on_tls_callback #121596

Merged
merged 2 commits into from
Feb 29, 2024

Conversation

ChrisDenton
Copy link
Member

@ChrisDenton ChrisDenton commented Feb 25, 2024

The first commit adds a volatile load of p_thread_callback when registering a dtor so that the compiler knows if the callback is used or not. I don't believe the added volatile instruction is otherwise significant in the context. In my testing using the volatile load allowed the compiler to correctly reason about whether on_tls_callback is used or not, allowing it to be omitted entirely in some cases. Admittedly it usually is used due to Thread but that can be avoided (e.g. in DLLs or with custom entry points that avoid the offending APIs). Ideally this would be something the compiler could help a bit more with so we didn't have to use tricks like #[used] or volatile. But alas.

I also used this as an opportunity to clean up the unused lints which I don't think serve a purpose any more.

The second commit removes the volatile load of _tls_used with #cfg[target_thread_local] because #[thread_local] already implies it. And if it ever didn't then #[thread_local] would be broken when there aren't any dtors.

@rustbot
Copy link
Collaborator

rustbot commented Feb 25, 2024

r? @Mark-Simulacrum

rustbot has assigned @Mark-Simulacrum.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added O-windows Operating system: Windows S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Feb 25, 2024
Copy link
Member

@joboet joboet left a comment

Choose a reason for hiding this comment

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

Looks good to me 👍. I whole-heartedly agree to the point about the compiler, I think we could do so much better in that regard... maybe something for #110897?

library/std/src/sys/pal/windows/thread_local_key.rs Outdated Show resolved Hide resolved
@ChrisDenton
Copy link
Member Author

I think we could do so much better in that regard... maybe something for #110897?

Maybe! I'll try to write up the issue on that thread when I have a moment.

@joboet
Copy link
Member

joboet commented Feb 28, 2024

@bors r+
I should be able to do that now, right? (So cool 😄!)

@bors
Copy link
Contributor

bors commented Feb 28, 2024

📌 Commit ad4c4f4 has been approved by joboet

It is now in the queue for this repository.

@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 Feb 28, 2024
@joboet joboet assigned joboet and unassigned Mark-Simulacrum Feb 28, 2024
bors added a commit to rust-lang-ci/rust that referenced this pull request Feb 29, 2024
…llaumeGomez

Rollup of 7 pull requests

Successful merges:

 - rust-lang#119748 (Increase visibility of `join_path` and `split_paths`)
 - rust-lang#120820 (Enable CMPXCHG16B, SSE3, SAHF/LAHF and 128-bit Atomics (in nightly) in Windows x64)
 - rust-lang#121000 (pattern_analysis: rework how we hide empty private fields)
 - rust-lang#121376 (Skip unnecessary comparison with half-open range patterns)
 - rust-lang#121596 (Use volatile access instead of `#[used]` for `on_tls_callback`)
 - rust-lang#121669 (Count stashed errors again)
 - rust-lang#121783 (Emitter cleanups)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit eea8cee into rust-lang:master Feb 29, 2024
11 checks passed
@rustbot rustbot added this to the 1.78.0 milestone Feb 29, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Feb 29, 2024
Rollup merge of rust-lang#121596 - ChrisDenton:tls, r=joboet

Use volatile access instead of `#[used]` for `on_tls_callback`

The first commit adds a volatile load of `p_thread_callback` when registering a dtor so that the compiler knows if the callback is used or not. I don't believe the added volatile instruction is otherwise significant in the context. In my testing using the volatile load allowed the compiler to correctly reason about whether `on_tls_callback` is used or not, allowing it to be omitted entirely in some cases. Admittedly it usually is used due to `Thread` but that can be avoided (e.g. in DLLs or with custom entry points that avoid the offending APIs). Ideally this would be something the compiler could help a bit more with so we didn't have to use tricks like `#[used]` or volatile. But alas.

I also used this as an opportunity to clean up the `unused` lints which I don't think serve a purpose any more.

The second commit removes the volatile load of `_tls_used` with `#cfg[target_thread_local]` because `#[thread_local]` already implies it. And if it ever didn't then `#[thread_local]` would be broken when there aren't any dtors.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
O-windows Operating system: Windows S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.

5 participants