Skip to content

clarify compiler_fence (and fence) docs#157380

Open
RalfJung wants to merge 2 commits into
rust-lang:mainfrom
RalfJung:fence
Open

clarify compiler_fence (and fence) docs#157380
RalfJung wants to merge 2 commits into
rust-lang:mainfrom
RalfJung:fence

Conversation

@RalfJung
Copy link
Copy Markdown
Member

@RalfJung RalfJung commented Jun 3, 2026

Also see rust-lang/libs-team#794 for more discussion.

Cc @Amanieu @m-ou-se

@rustbot rustbot added 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 Jun 3, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Jun 3, 2026

r? @jhpratt

rustbot has assigned @jhpratt.
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

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: libs
  • libs expanded to 8 candidates
  • Random selection from Mark-Simulacrum, jhpratt

/// memory operations around it.
/// threads. It can be helpful to think of a fence as preventing the compiler and CPU from
/// reordering certain types of memory operations around it, but that is a simplified model which
/// fails to capture some of the nuances.
Copy link
Copy Markdown
Member Author

@RalfJung RalfJung Jun 3, 2026

Choose a reason for hiding this comment

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

To give an example where saying "it prevents reordering" is failing to capture some nuance:

// ptr: *mut i32

ptr.write(0);
fence(Release);
ptr.write(1);
fence(Release);

In this example, the compiler is totally allowed to reorder the first write down across the release fence. (In fact the compiler is allowed to entirely remove the first fence, it is subsumed by the second fence.)

View changes since the review

Comment thread library/core/src/sync/atomic.rs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants