-
Notifications
You must be signed in to change notification settings - Fork 21
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
Fix fence on non-x86 arch and miri #16
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -445,8 +445,11 @@ impl<T> fmt::Display for PushError<T> { | |
|
||
/// Equivalent to `atomic::fence(Ordering::SeqCst)`, but in some cases faster. | ||
#[inline] | ||
fn full_fence() { | ||
if cfg!(any(target_arch = "x86", target_arch = "x86_64")) { | ||
fn full_fence_for_load<T>(load_op: impl FnOnce() -> T) -> T { | ||
if cfg!(all( | ||
any(target_arch = "x86", target_arch = "x86_64"), | ||
not(miri) | ||
)) { | ||
// HACK(stjepang): On x86 architectures there are two different ways of executing | ||
// a `SeqCst` fence. | ||
// | ||
|
@@ -461,7 +464,11 @@ fn full_fence() { | |
// x86 platforms is going to optimize this away. | ||
let a = AtomicUsize::new(0); | ||
let _ = a.compare_exchange(0, 1, Ordering::SeqCst, Ordering::SeqCst); | ||
// On x86, `lock cmpxchg; mov` is fine. See also https://www.cl.cam.ac.uk/~pes20/cpp/cpp0xmappings.html. | ||
load_op() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FWIW, this is still Rust code -- so if Miri complains when running this branch of the code (which I suspect it will, since a SC RMW before a load cannot replace a fence after a load), then this code is still wrong. When you write Rust code, the hardware memory model is all but irrelevant for program correctness. Only the Rust memory model counts. EDIT: Oh I see this got reverted in #18. |
||
} else { | ||
let res = load_op(); | ||
atomic::fence(Ordering::SeqCst); | ||
res | ||
} | ||
} |
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 fact that you are hoping that "sane" compilers for particular targets are going to treat the memory model differently, is a big red flag. The memory model is target-independent, and a whole bunch of optimizations run on this code (including its use of atomics) before any target-specific concerns are applied.
Inline assembly is the only correct choice here.
EDIT: Oh I see this got reverted in #18.